我在显示原始YUV文件时遇到了问题,它处于NV12格式。在OpenGl中需要YUV显示帮助
我可以显示一个选定的帧,但是,它仍然主要是在黑色和白色的某些色调的粉红色和绿色。
这里是我的输出如何看起来像 alt text http://i32.tinypic.com/hv0vih.png
不管怎么说,这是我的计划是如何工作的。 (这是在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 &紫外线计划,并将它们存储到两个缓冲区,yBuffer & uvBuffer
在这一点上,我尝试将YUV数据转换,并将其存储到一个名为RGB缓冲区数组“ frameImage”
-(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文件,将所有数据存储在字符数组缓冲区中。然后我将这些值转换成他们推荐的YUV浮点值。
这就是我认为错误可能出现的地方:在Y查找表中,我将Y平面标准化为[0,1],在U平面中我将标准化[-0.435,0.436]之间的值和我在V平面标准化了[-0.615,0.615]。我这样做是因为这些是根据维基百科的YUV值范围。
YUV到RGB公式是从维基百科相同的公式。我也尝试了各种其他的公式,这是唯一给出框架粗糙外观的公式。任何人都可能冒险猜测为什么我的程序没有正确显示YUV帧数据。我认为这与我的标准化技术有关,但对我来说似乎是好的。
我做了很多测试,并且我100%确定错误是由查找表引起的。我不认为我的设置公式是正确的。
给所有人谁正在阅读 这个。最长的时间,我的帧 显示不正确,因为我 无法正确提取帧数据 。
当我刚开始到节目,我是 的印象是,在 剪辑说30帧,所有30个是平坦的DATAS 首先写入在数据文件中,通过 UV平面DATAS再其次。
我发现了通过试验和 误差是用于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 http://i26.tinypic.com/kdtilg.png
这里是从控制台打印行。我是打印出正在翻译和frameImage阵列中存储在用于Y,U,V和 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格式。无论如何,我想与你分享我的结果。
这里是输出
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, 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]
谢谢,我错过了这个错误。然而,我做出了改变,展出的框架仍然以黑色和白色为主,粉红色和绿色色调在这里和那里。 – ReachConnection 2009-07-10 15:08:29
我刚刚添加了一张图片来显示我的输出结果如何 – ReachConnection 2009-07-10 15:33:00