【问题标题】:Display YUV in OpenGL在 OpenGL 中显示 YUV
【发布时间】:2009-07-09 22:20:12
【问题描述】:

我无法显示 NV12 格式的原始 YUV 文件。

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

这是我的输出的样子

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

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

在初始化期间,我创建了一个查找表,它将二进制/8 位/一个字节/字符转换为受尊重的 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
    }
}

在此之后,我提取 Y & 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] ]

这是我得到的结果

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

YUV:[0.658824,-0.022227,-0.045824] RGBFinal:[0.606593,0.694201,0.613655]

YUV:[0.643137,-0.022227,-0.045824] RGBFinal:[0.590906,0.678514,0.597969]

YUV:[0.607843,-0.022227,-0.045824] RGBFinal:[0.555612,0.643220,0.562675]

YUV:[0.592157,-0.022227,-0.045824] RGBFinal:[0.539926,0.627534,0.546988]

YUV:[0.643137,0.025647,0.151941] RGBFinal:[0.816324,0.544799,0.695255]

YUV:[0.662745,0.025647,0.151941] RGBFinal:[0.835932,0.564406,0.714863]

YUV:[0.690196,0.025647,0.151941] RGBFinal:[0.863383,0.591857,0.742314]

2009 年 7 月 13 日更新

感谢nschmidt的推荐,问题终于解决了。事实证明,我的 YUV 文件实际上是 YUV 4:1:1 格式。我最初被告知它是 YUV NV12 格式。不管怎样,我想和你分享我的结果。

这是输出

我的解码代码如下

        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 进行原始文件提取。存储在 char 数组缓冲区中。对于 YUV 到 RGB 的转换,我使用了上面的公式,然后,我将值限制为 [0:255]。然后我在 OpenGL 中使用 2DTexture 进行显示。

因此,如果您将来要将 YUV 转换为 RGB,请使用上面的公式。如果使用前面帖子中的 YUV 到 RGB 转换公式,那么您需要在 GL_Float 中显示纹理,因为 RGB 的值被限制在 [0:1] 之间

【问题讨论】:

  • 您好,可以分享完整的示例代码吗?

标签: objective-c opengl rgb yuv


【解决方案1】:

下一次尝试:) 我认为你的 uv 缓冲区没有交错。看起来 U 值首先出现,然后是 V 值数组。将行更改为

unsigned int voffset = YUV_HEIGHT * YUV_WIDTH / 2;
float U = lookupTableU [uvBuffer [ y * (YUV_WIDTH / 2) + x/2] ]; 
float V = lookupTableV [uvBuffer [ voffset + y * (YUV_WIDTH / 2) + x/2] ];

可能会表明是否确实如此。

【讨论】:

  • 哇,我用你的调整测试了我的程序,它终于奏效了。非常感谢,事实证明你是对的,U & V 平面并没有像我最初被告知的那样交错。非常感谢恩施密特。我给你打分
  • 没问题:)。作为记录,关于格式为 4:1:1 的第一个猜测实际上是不正确的。你有 4:2:2 格式。真正的问题只是您的数据没有被交错。
  • @ReachConnection:你能告诉我[self clampValue:RFormula]这个函数在做什么吗?我打算用你的代码从YUV422获取UIImage。
【解决方案2】:

我认为您对 U 和 V 值的处理不正确。而不是:

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

应该是这样的

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] ];

【讨论】:

  • 谢谢,我错过了这个错误。但是,我进行了更改,显示的框架仍然主要是黑白的,到处都是粉红色和绿色。
【解决方案3】:

图片看起来像 4:1:1 格式。你应该把你的台词改成

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

也许你可以把结果贴出来看看,还有什么问题。我总是觉得很难去想。迭代地处理这个问题要容易得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 2020-01-24
    • 2013-12-04
    相关资源
    最近更新 更多