Rea*_*ion 11 opengl rgb objective-c yuv
我在显示NV12格式的原始YUV文件时遇到问题.
我可以显示一个选定的框架,然而,它仍然主要是黑色和白色,具有一定的粉红色和绿色.
以下是我的输出看起来像 alt文本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
}
}
Run Code Online (Sandbox Code Playgroud)
U Plane查找表
-(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
}
}
Run Code Online (Sandbox Code Playgroud)
和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
}
}
Run Code Online (Sandbox Code Playgroud)
在此之后,我提取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];
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我试图在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();
}
Run Code Online (Sandbox Code Playgroud)
所以基本上我的程序在坚果壳中.本质上我读取二进制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] ]
Run Code Online (Sandbox Code Playgroud)
这就是我得到 alt文本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格式.无论如何,我想和你分享我的成绩.
这是输出
替代文字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();
Run Code Online (Sandbox Code Playgroud)
基本上,我使用NSData进行原始文件提取.存储在char数组缓冲区中.对于YUV到RGB的转换,我使用了上面的公式,之后,我将这些值钳制到[0:255].然后我在OpenGL中使用2DTexture进行显示.
因此,如果您将来将YUV转换为RGB,请使用上面的公式.如果使用早期帖子中的YUV到RGB转换公式,那么你需要在GL_Float中显示纹理,从RGB的值夹在[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] ];
Run Code Online (Sandbox Code Playgroud)
可能表明情况确实如此.
| 归档时间: |
|
| 查看次数: |
10262 次 |
| 最近记录: |