objective-c – 需要在OpenGl中显示YUV的帮助

前端之家收集整理的这篇文章主要介绍了objective-c – 需要在OpenGl中显示YUV的帮助前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法显示NVV格式的原始YUV文件.

我可以显示一个选定的框架,但是它仍然主要是黑色和白色,具有一定的粉红色和绿色.

这是我的输出如何
alt text http://i32.tinypic.com/hv0vih.png

无论如何,这里是我的程序如何工作. (这是在可可/ 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",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",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,GL_RGB,YUV_WIDTH,YUV_HEIGHT,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); 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平面中,I标准化了[-0.435,0.436]之间的值,在V平面I标准化这是[-0.615,0.615].我这样做是因为这些是根据维基百科的YUV值范围.

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

我已经做了很多的测试,我100%确定错误是由查找表造成的.我不认为我的设置公式是正确的.

A note to everyone who is reading
this. For the longest time,my frame
was not displaying properly because I
was not able to extract the frame data
correctly.

When I first started to program,I was
under the impression that in a clip of
say 30 frames,all 30 Y planar datas
are first written in the data file,
followed then by UV plane datas.

What I found out through trial and
error was that for a YUV data file,
specifically NV12,the data file is
stored in the following fashion

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 http://i26.tinypic.com/kdtilg.png

这是从控制台的打印行.我打印出Y,V和的值
正在转换并存储在frameImage数组中的RGB值

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

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

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

YUV:[0.592157,-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.151941] RGBFinal:[0.835932,0.564406,0.714863]

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

更新2009年7月13日

由于nschmidt的建议,问题终于解决了.原来我的YUV文件实际上是YUV 4:1:1格式.我最初被告知是在YUV NV12格式.无论如何,我想和你分享我的结果.

这是输出

alt text http://i32.tinypic.com/35b9xf8.png

我的解码代码如下

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,GL_UNSIGNED_BYTE,frameImage);

//glTexParameteri(GL_TEXTURE_2D,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE_SGIS);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);

glRotatef( 180.0f,0.0f );

glBegin( GL_QUADS );
glTexCoord2d(0.0,-1.0);
glTexCoord2d(1.0,+1.0);
glTexCoord2d(0.0,+1.0);
glEnd();

NSLog(@"YUVFrameView: drawRect complete");
glFlush();

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

因此,如果您将来将YUV转换为RGB,请使用上述公式.如果从前面的帖子使用YUV到RGB转换公式,那么需要从RGB中显示纹理到GL_Float,这些值夹在[0: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] ];

可能会说明是否真的是这样.

猜你在找的C&C++相关文章