代码之家  ›  专栏  ›  技术社区  ›  Community wiki

在OpenGL中显示YUV

  •  12
  • Community wiki  · 技术社区  · 3 年前

    我在显示NV12格式的原始YUV文件时遇到问题。

    我可以显示一个选定的框架,然而,它仍然主要是黑色和白色,并带有一定的粉红色和绿色。

    以下是我的输出 alt text

    无论如何,以下是我的程序的工作原理。(这是在cocoa/objective-c中完成的,但我需要你关于程序算法的专家建议,而不是语法。)

    在程序执行之前,YUV文件存储在一个名为“test.YUV”的二进制文件中。该文件为NV12格式,意味着首先存储Y计划,然后交错UV计划。我的文件提取没有问题,因为我做了很多测试。

    在初始化过程中,我创建了一个查找表,将二进制/8位/a字节/字符转换为相关的Y、U、V浮点值

    对于Y平面,这是我的代码

    -(void)createLookupTableY //creates a lookup table for converting a single byte into a float between 0 and 1
    {
        NSLog(@"YUVFrame: createLookupTableY");
        lookupTableY = new float [256];
        
        for (int i = 0; i < 256; i++)
        {
            lookupTableY[i] = (float) i /255;
            //NSLog(@"lookupTableY[%d]: %f",i,lookupTableY[i]);//prints out the value of each float
        }
    }
    

    U平面查找表

    -(void)createLookupTableU //creates a lookup table for converting a single byte into a float between 0 and 1
    {
        NSLog(@"YUVFrame: createLookupTableU");
        lookupTableU = new float [256];
        
        for (int i = 0; i < 256; i++)
        {
            lookupTableU[i] =  -0.436 + (float) i / 255* (0.436*2);
            NSLog(@"lookupTableU[%d]: %f",i,lookupTableU[i]);//prints out the value of each float
        }
    }
    

    和V查找表

    -(void)createLookupTableV //creates a lookup table for converting a single byte into a float between 0 and 1
    {
        NSLog(@"YUVFrame: createLookupTableV");
        lookupTableV = new float [256];
        
        for (int i = 0; i < 256; i++)
        {
            lookupTableV[i] =  -0.615 + (float) i /255 * (0.615*2);
            NSLog(@"lookupTableV[%d]: %f",i,lookupTableV[i]);//prints out the value of each float
        }
    }
    

    在这一点之后;UV规划并将它们存储到两个缓冲区中,yBuffer&uvBuffer

    此时,我尝试转换YUV数据,并将其存储到一个名为“frameImage”的RGB缓冲区阵列中

    -(void)sortAndConvert//sort the extracted frame data into an array of float
    {
        NSLog(@"YUVFrame: sortAndConvert");
        int frameImageCounter = 0;
        int pixelCounter = 0;
        for (int y = 0; y < YUV_HEIGHT; y++)//traverse through the frame's height
        {
            for ( int x = 0; x < YUV_WIDTH; x++)//traverse through the frame's width
            {
                
                float Y = lookupTableY [yBuffer [y*YUV_WIDTH + x] ];
                float U = lookupTableU [uvBuffer [ ((y / 2) * (YUV_WIDTH / 2) + (x/2)) * 2  ] ]; 
                float V = lookupTableV [uvBuffer [  ((y / 2) * (YUV_WIDTH / 2) + (x/2)) * 2 + 1] ];
            
                float RFormula = Y + 1.13983f * V;
                float GFormula = Y - 0.39465f * U - 0.58060f * V;
                float BFormula = Y + 2.03211f * U;
                
                 frameImage [frameImageCounter++] = [self clampValue:RFormula];
                 frameImage [frameImageCounter++] = [self clampValue:GFormula];
                 frameImage [frameImageCounter++] = [self clampValue:BFormula];
    
            }
        }
    
    }
    

    然后我尝试在OpenGL中绘制图像

    -(void)drawFrame:(int )x
    {
        
        GLuint texture;
        
        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, YUV_WIDTH, YUV_HEIGHT, 0, GL_RGB, GL_FLOAT, frameImage);
        
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, texture);
        glRotatef( 180.0f, 1.0f, 0.0f, 0.0f );
    
        glBegin( GL_QUADS );
        glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
        glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
        glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
        glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
        glEnd();
        
        glFlush();
    }
    

    所以基本上这是我的程序。本质上,我读取二进制YUV文件,将所有数据存储在char数组缓冲区中。然后,我将这些值转换为它们所尊重的YUV浮点值。

    这就是我认为错误的地方:在Y查找表中,我将Y平面标准化为[0,1],在U平面中,我标准化了[-0.435,0.436]之间的值,在V平面中,我们将其标准化为[-0.615,0.615]。我这样做是因为这些是根据维基百科的YUV值范围。

    YUV到RGB的公式与维基百科中的公式相同。我还尝试了其他各种公式,这是唯一一个给出框架粗略前景的公式。任何人都可能大胆猜测为什么我的程序没有正确显示YUV帧数据。我认为这与我的标准化技术有关,但对我来说似乎还可以。

    我做了很多测试,我100%确信错误是由查找表引起的。我认为我的设置公式不正确。

    给所有正在阅读的人的一封信 这在最长的一段时间里,我的身体 显示不正确,因为我 无法提取帧数据 正确地

    当我第一次开始编程时 印象中 比如说30帧,全部30个Y平面数据 首先写入数据文件中, 然后是UV平面数据。

    我通过试验和 错误在于对于YUV数据文件, 特别是NV12,数据文件是 以以下方式存储

    Y(1)UV(1)Y(2)UV(2)。。。

    @nschmidt

    我按照你的建议修改了代码

    float U = lookupTableU [uvBuffer [ (y * (YUV_WIDTH / 4) + (x/4)) * 2 ] ]
    float V = lookupTableU [uvBuffer [ (y * (YUV_WIDTH / 4) + (x/4)) * 2 + 1] ]
    

    这就是我得到的结果 alt text

    这是控制台上的打印行。我打印出Y、U、V和 正在转换并存储在frameImage阵列中的RGB值

    YUV:[0.658824,-0.02227,-0.045824]RGB最终:[0.606593,0.694201,0.613655]

    YUV:[0.643137,-0.02227,-0.045824]RGB最终值:[0.90906,0.678514,0.597969]

    YUV:[0.607843,-0.02227,-0.045824]RGB最终:[0.551612,0.643220,0.562675]

    YUV:[0.592157,-0.02227,-0.048524]RGB最终:[0.539926,0.627534,0.546988]

    YUV:[0.643137,0.025647,0.151941]RGB最终:[0.816324,0.544799,0.695255]

    YUV:0.662745,0.025647,0.151941]RGB最终:[0.835932,0.564406,0.714863]

    YUV:0.690196,0.025647,0.151941]RGB最终:[0.8663383,0.591857,0.742314]

    更新时间:2009年7月13日

    多亏了 nschmidt 。原来我的YUV文件实际上是YUV 4:1:1的格式。我最初被告知它是YUV NV12格式的。无论如何,我想和大家分享我的研究结果。

    这是输出

    alt text

    我的解码代码如下

            float Y = (float) yBuffer [y*YUV_WIDTH + x] ;
            float U = (float) uvBuffer [ ((y / 2) * (YUV_WIDTH  / 2) + (x/2))   ] ; 
            float V = (float) uvBuffer [  ((y / 2) * (YUV_WIDTH  / 2) + (x/2))  + UOffset] ;
    
            float RFormula = (1.164*(Y-16) + (1.596* (V - 128) ));
            float GFormula = ((1.164 * (Y - 16)) - (0.813 * ((V) - 128)) - (0.391 * ((U) - 128)));
            float BFormula = ((1.164 * (Y - 16)) + (2.018 * ((U) - 128)));
    
    
            frameImage [frameImageCounter] = (unsigned char)( (int)[self clampValue:RFormula]);
            frameImageCounter ++;
            frameImage [frameImageCounter] =  (unsigned char)((int)[self clampValue:GFormula]);
            frameImageCounter++;
            frameImage [frameImageCounter] = (unsigned char)((int) [self clampValue:BFormula]);
            frameImageCounter++;
    
    
    
    GLuint texture;
    
    glGenTextures(1, &texture);
    glEnable(GL_TEXTURE_2D);
    
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
    
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, YUV_WIDTH, YUV_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, frameImage);
    
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE_SGIS);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE_SGIS);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    
    glRotatef( 180.0f, 1.0f, 0.0f, 0.0f );
    
    glBegin( GL_QUADS );
    glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
    glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
    glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
    glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
    glEnd();
    
    NSLog(@"YUVFrameView: drawRect complete");
    glFlush();
    

    基本上,我使用NSData进行原始文件提取。存储在字符数组缓冲区中。对于YUV到RGB的转换,我使用了上面的公式,然后,我将值钳制为[0:255]。然后我使用OpenGL中的2DTexture进行显示。

    因此,如果将来要将YUV转换为RGB,请使用上面的公式。如果使用前一篇文章中的YUV到RGB转换公式,则需要在GL_Float中显示纹理,因为RGB的值被钳制在[0:1]之间

    3 回复  |  直到 6 年前
    推荐文章