题
我有显示原始YUV文件,它是在NV12格式麻烦。
我可以显示所选帧,然而,它仍主要在黑色和白色,粉红色的色调某些和绿色。
下面是我的输出如何看起来像
不管怎么说,这是我的计划是如何工作的。 (这是在可可/ Objective-C的工作要做,但我需要在程序算法的专家的意见,而不是语法)。
在此之前的程序执行,该YUV文件存储在名为“test.yuv”的二进制文件。该文件是在NV12格式,这意味着在Y计划首先被存储,然后UV计划交错。因为我做了很多testings我的文件提取有没有问题。
在开始时,创建一个查找表,将二进制/ 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文件,所有的数据存储。然后,我将这些值转换成他们尊敬的YUV的浮点值。
这是我认为该错误可能是:在Y查找表我规范Y平面为[0,1],在U平面I之间标准化[-0.435,0.436]的值,并且在V平面I标准化它bewteen [-0.615,0.615]。我这样做是因为那些根据维基百科是YUV值范围。
和所述YUV到RGB式是从维基百科相同的公式。我也尝试过各种其他公式,这是给框架的粗糙的前景的唯一公式。任何人都可以大胆猜测,为什么我的程序无法正确显示YUV帧数据。我认为这是事做我的标准化技术,但似乎好了给我。
我已经做了很多testings的,我一定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] ]
和这是我得到的结果
这里是从控制台打印线。我是打印出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对于在原始文件提取。存储在字符数组缓冲器。 YUV至RGB转换,我使用的上述式中,事后,我夹紧的值[0:255]。然后我用一个2DTexture在OpenGL用于显示。
因此,如果要在将来转换YUV到RGB,使用公式从上方。如果使用的是YUV到从早期交RGB转换公式,则需要从用于RGB被夹持的值显示在GL_FLOAT纹理[0:1]
解决方案
接着尝试:) 我认为你是紫外缓冲区不交叉存取。它看起来像的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值。而不是:
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] ];
在图像看起来你有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] ]
也许你可以张贴的结果看,还有什么是错的。我发现它总是很难去想它。这是很容易反复地接近这一点。