http://stackoverflow.com/questions/1106741/need-help-with-yuv-display-in-opengl 

I am having trouble displaying a raw YUV file that it is in NV12 format.

I can display a selected frame, however, it is still mainly in black and white with certain shades of pink and green.

Here is how my output looks like 

Anyways, here is how my program works. (This is done in cocoa/objective-c, but I need your expert advice on program algorithm, not on syntax.)

Prior to program execution, the YUV file is stored in a binary file named "test.yuv". The file is in NV12 format, meaning the Y plan is stored first, then the UV plan is interlaced. My file extraction has no problem because I did a lot testings on it.

During initiation, I create a lookup table that will convert binary/8 bites/a byte/chars into respected Y, U, V float values

For the Y plane this is my code

-(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 }}

The U Plane lookup table

-(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 }}

And the V look-up table

-(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 }}

after this point, I extract the Y & UV plan and store them into two buffers, yBuffer & uvBuffer

at this point, I attempt to convert the YUV data and stored it into a RGB buffer array called "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]; } }}

then I tried to draw the Image in 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();}

so basically this my program in a nut shell. essentially i read the binary YUV files, stores all the data in a char array buffer. i then translate these values into their respected YUV float values.

This is where I think the error might be: in the Y lookup table I standardize the Y plane to [0,1], in the U plane I standardized the values between [-0.435,0.436], and in the V plane I standardized it bewteen [-0.615,0.615]. I did this because those are the YUV value ranges according to wikipedia.

And the YUV to RGB formula is the same formula from Wikipedia. I have also tried various other formulas, and this is the only formula that gives the rough outlook of the frame. Anyone might venture to guess to why my program is not correctly displaying the YUV frame data. I think it is something to do with my standardization technique, but it seems alright to me.

I have done a lot of testings, and I am 100% certain that the error is caused by by look up table. I don't think my setting formulas are correct.

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

I changed my code to what you suggested

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

and this is the result that i get 

here is the print line from the console. i am print out the values for Y, U, V and RGB value that are being translated and stored on in the frameImage array

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]

Update July 13, 2009

The problem was finally solved thanks to the recommendation from nschmidt . It turns out that my YUV file was actually in YUV 4:1:1 format. I was originally told that it was in YUV NV12 format. Anyways, I would like to share my results with you.

Here is output

and my code for decode is as follows

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();

essentially, I used NSData for the raw file extraction. stored in a char array buffer. For YUV to RGB conversion, I used the above formula, afterwards, I clamped the values to [0:255]. then I used a 2DTexture in OpenGL for display.

So if you are converting YUV to RGB in the future, use the formula from above. If are using the YUV to RGB conversion formula from the earlier post, then you need to display the texture in GL_Float from the values for RGB are clamped between [0:1]

<div "="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; height: 35px; width: 185px; background: transparent;">

 
 

3 Answers

Next try :) I think you're uv buffer is not interleaved. It looks like the U values come first and then the array of V values. Changing the lines to

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

might indicate if this is really the case.

<div "="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; height: 35px; width: 185px; background: transparent;">

answered Jul 11 '09 at 12:12
nschmidt
1,816919
    
wow, I tested out my program with your adjustment and it finally worked. thanks a lot man.it turns out that you were right, the U & V plane are not interlaced as I was originally told. thank you so much nschmidt. I'm rating you up –  ReachConnection Jul 13 '09 at 15:07
    
no problem :). for the record the first guess about the format being 4:1:1 was actually incorrect. You have 4:2:2 format. The real problem was only your data not being interleaved. –  nschmidt Jul 14 '09 at 17:54

I think you're addressing U and V values incorrectly. Rather than:

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

It should be something along the lines of

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] ];
<div "="" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; height: 35px; width: 185px; background: transparent;">

answered Jul 10 '09 at 11:47
nschmidt
1,816919
    
thanks, I missed this mistake. however, I made the changes and the displayed frame is still mainly in black and white, with pink and green shades here and there. –  ReachConnection Jul 10 '09 at 15:08
    
I just added a image to show how my output looks like –  ReachConnection Jul 10 '09 at 15:33

The picture looks like you have 4:1:1 format. You should change your lines to

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

Maybe you can post the result to see, what else is wrong. I find it always hard to think about it. It's much easier to approach this iteratively.

YUV display in OpenGL的更多相关文章

  1. 记录一次安装OpenGL的漫长过程

    尝试codeblock和Dev-C++ 这学期新开了一门计算机图形图像的课,里面涉及到openGL,中午跑到图书馆开始倒腾OpenGL. 因为电脑里本来有codeblock,于是就想不用教材里面所说的 ...

  2. Draw the RGB data from kinect C++ via opengl

    In order to improve my English writing skills,I am going to  write the blogs in English form now! -- ...

  3. LCD framebuffer驱动设计文档

    内容提要:1. android display相关的名词2. 调试LCD驱动需要注意的步骤3. 关于帧缓冲区及I/O内存---------------------------------------- ...

  4. Direct Access to Video Encoding and Decoding

    来源:http://asciiwwdc.com/2014/sessions/513   Direct Access to Video Encoding and Decoding  Session 5 ...

  5. RTSP H264/HEVC 流 Wasm 播放

    本文将介绍 RTSP H264/HEVC 裸流如何于网页前端播放.涉及 WebSocket 代理发送流数据, Wasm 前端解码等. 代码: https://github.com/ikuokuo/rt ...

  6. C++实现数字媒体三维图像变换

    C++实现数字媒体三维图像变换 必备环境 glut.h 头文件 glut32.lib 对象文件库 glut32.dll 动态连接库 程序说明 C++实现了用glut画物体对象的功能.并附带放大缩小,旋 ...

  7. EGL 1.0 学习笔记

    http://hi.baidu.com/leo_xxx/item/b01b1fc29abff355ac00ef5c 基本概念 EGL是OpenGL ES与本地Window系统之间的桥梁.EGL创建渲染 ...

  8. 【2】【MOOC】Python游戏开发入门-北京理工大学【第三部分-游戏开发之机制(屏幕绘制机制)】

    学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=and ...

  9. Starling 2D框架简介(一)

    本系列是对Introducing Starling pdf的翻译,下文是对adobe开发人员中心的一片日志的转载,地址为http://www.adobe.com/cn/devnet/flashplay ...

随机推荐

  1. mysql通过一张表更新另一张表

    在mysql中,通过一张表的列修改另一张关联表中的内容: 1:  修改1列 update student s, city c set s.city_name = c.name where s.city ...

  2. Apache服务器运维笔记(2)----使用apxs来进行编译安装 mod_txt 模块

    mod_txt是一个非常有趣的模块,它实现了文本的输出过滤器,它可以在指定类型的网页上,将你指定的txt文件显示在网页的头部和尾部. 在它的主页 http://apache.webthing.com/ ...

  3. Kafka配额讨论(流量限制)

    Kafka自0.9.0.0版本引入了配额管理(quota management),旨在broker端对clients发送请求进行限流(throttling).目前Kafka支持两大类配额管理: 网络带 ...

  4. idea引入依赖包报错

    今天在更新项目的时候,maven依赖的一个服务一直报错.查了后发现原来是因为缺少依赖包.但是依赖包明明在我本地啊. 又重新下载,依然如故... 搞了半天,发现自己的依赖包类状态都是不可用的.如下图所示 ...

  5. 针对XX系统的可用性方面的相关想法(结合书)

    在开始对此系统进行再次分析之前,再回顾下可用性.首先,可用性是与系统故障有关的一个质量属性,是指系统正常运行的时间的比例,一般通过两次故障之间的时间长度或在系统崩溃情况下能恢复正常运行的速度来衡量,同 ...

  6. web项目开发流程

    对于一个web项目,在实际编码之前,有一些通用的步骤来planning a website: 0.Defining the project (predr0->dr0) 对于外部项目,客户一般会发 ...

  7. Android已上线应用开源分享中(第二季)

    昨天和大家分享了我Android上线的第一个应用,大家还是挺支持的,很高兴,虽然作品没有那么高大上,但是很是有了一点小小的成就感,所以打算继续开源我上线的一些应用,和大家一起交流一下. 我这个作品是一 ...

  8. CentOS release 6.5 yum安装报错

    单独安装如下任何一个包都会报依赖错误,需要一起安装才可以 rpm -ivh yum-plugin-fastestmirror-1.1.30-14.el6.noarch.rpm yum-3.2.29-4 ...

  9. January 26 2017 Week 4 Thursday

    Wasting time is robbing yourself. 浪费时间就是掠夺自己. Wasting time is not only robbing yourself, moreover, i ...

  10. OC NSArray使用

    #import <Foundation/Foundation.h> #import "Student.h" #pragma mark 创建一个数组 void array ...