如何在Cocoa OpenGL程序中显示原始YUV帧 [英] How to display a raw YUV frame in a Cocoa OpenGL program

查看:418
本文介绍了如何在Cocoa OpenGL程序中显示原始YUV帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被赋予编写一个程序的任务,该程序采用一个示例原始YUV文件并显示在Cocoa OpenGL程序中。

I have been assigned wit the task to write a program that takes a sample raw YUV file and display it in a Cocoa OpenGL program.

我是实习生我的工作和我有很少或没有线索如何开始。我一直在读维基百科和文章关于YUV,但我找不到任何良好的源代码如何打开原始YUV文件,提取数据,并将其转换为RGB并显示在视图窗口。

I am an intern at my job and I have little or no clue how to start. I have been reading wikipedia & articles on YUV, but I couldn't find any good source code on how to open a raw YUV file, extract the data and convert it into RGB and display it in the view window.

基本上,我需要帮助以下方面的任务
-how从示例YUV文件中提取YUV数据
-how将YUV数据转换为RGB颜色空间
-how在OpenGL中显示RGB颜色空间。 (这一个我想我可以知道时间,但我真的需要帮助的前两个点)

Essentially, I need help with the following aspects of the task -how to extract the YUV data from the sample YUV file -how to convert the YUV data into RGB color space -how to display the RGB color space in OpenGL. (This one I think I can figure out with time, but I really need help with the first two points)

请告诉我要使用的类,或指向我到我可以了解YUV图形/视频显示的地方

please either tell me the classes to use, or point me to places where i can learn about YUV graphic/video display

推荐答案

这个答案不正确,和评论。

This answer is not correct, see the other answers and comments. Original answer left below for posterity.

您无法直接显示。维基百科中收集到的,YUV颜色空间有许多变化。确保您使用的是正确的。

You can't display it directly. You'll need to convert it to an RGB texture. As you may have gathered from Wikipedia, there are a bunch of variations on the YUV color space. Make sure you're using the right one.

对于每个像素,从YUV到RGB的转换是一种直接的线性变换。

For each pixel, the conversion from YUV to RGB is a straightforward linear transformation. You just do the same thing to each pixel independently.

将图像转换为RGB后,您可以通过创建纹理来显示它。您需要致电 glGenTextures() 分配纹理句柄, glBindTexture() 将纹理绑定到渲染上下文, glTexImage2D() 以将纹理数据上传到GPU。要渲染它,你再次调用 glBindTexture(),然后渲染一个纹理坐标设置正确的四边形。

Once you've converted the image to RGB, you can display it by creating a texture. You need to call glGenTextures() to allocate a texture handle, glBindTexture() to bind the texture to the render context, and glTexImage2D() to upload the texture data to the GPU. To render it, you again call glBindTexture(), followed by the rendering of a quad with texture coordinates set up properly.

// parameters: image:  pointer to raw YUV input data
//             width:  image width (must be a power of 2)
//             height: image height (must be a power of 2)
// returns: a handle to the resulting RGB texture
GLuint makeTextureFromYUV(const float *image, int width, int height)
{
    float *rgbImage = (float *)malloc(width * height * 3 * sizeof(float));  // check for NULL
    float *rgbImagePtr = rgbImage;

    // convert from YUV to RGB (floats used here for simplicity; it's a little
    // trickier with 8-bit ints)
    int y, x;
    for(y = 0; y < height; y++)
    {
        for(x = 0; x < width; x++)
        {
            float Y = *image++;
            float U = *image++;
            float V = *image++;
            *rgbImagePtr++ = Y                + 1.13983f * V;  // R
            *rgbImagePtr++ = Y - 0.39465f * U - 0.58060f * V;  // G
            *rgbImagePtr++ = Y + 2.03211f * U;                 // B
        }
    }

    // create texture
    GLuint texture;
    glGenTextures(1, &texture);

    // bind texture to render context
    glBindTexture(GL_TEXTURE_2D, texture);

    // upload texture data
    glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_FLOAT, rgbImage);

    // don't use mipmapping (since we're not creating any mipmaps); the default
    // minification filter uses mipmapping.  Use linear filtering for minification
    // and magnification.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    // free data (it's now been copied onto the GPU) and return texture handle
    free(rgbImage);
    return texture;
}

要呈现:

glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.0f,  0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(64.0f,  0.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(64.0f, 64.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, 64.0f, 0.0f);
glEnd();

别忘了调用 glEnable(GL_TEXTURE_2D)在初始化期间的某个时间点,并在关闭期间调用 glDeleteTextures(1,& texture)

And don't forget to call glEnable(GL_TEXTURE_2D) at some point during initialization, and call glDeleteTextures(1, &texture) during shutdown.

这篇关于如何在Cocoa OpenGL程序中显示原始YUV帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆