ffmpeg:如何从AVframe获取YUV数据并使用opengl绘制它?

时间:2022-08-25 22:05:09

How could I access the YUV data from AVframe struct? by accessing its data[]? And is there any simple way to draw YUV data using opengl instead of creating the shader and draw the Y,U,V image on their own?

我如何从AVframe结构中访问YUV数据?通过访问其数据[]?有没有简单的方法使用opengl绘制YUV数据而不是创建着色器并自己绘制Y,U,V图像?

1 个解决方案

#1


Yes, you use frame->data[]. Typically everyone uses shaders/textures, so you should too. Just upload them as textures, use code like this to upload the texture:

是的,你使用frame-> data []。通常每个人都使用着色器/纹理,所以你也应该这样做。只需将它们作为纹理上传,使用这样的代码上传纹理:

glPixelStorei(GL_UNPACK_ROW_LENGTH, frame->linesize[0])
glActiveTexture(GL_TEXTURE0 + id);
glBindTexture(GL_TEXTURE_2D, texture[id]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, frame->width, frame->height,
             0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame->data[0]);

Upload data[1] and [2] in the same way but apply chroma subsamplings to width/height (e.g. uvwidth = (width + 1) >> 1). This code also assumes that you've created 3 textures in an array called texture[]. In your drawing loop, you can then refer to these texture ids to link them to uniform textures in your shaders, which you eventually use to draw. Shader examples that do YUV-to-RGB conversion can be found virtually anywhere.

以相同的方式上传数据[1]和[2],但将色度子采样应用于宽度/高度(例如,uvwidth =(width + 1)>> 1)。此代码还假设您在名为texture []的数组中创建了3个纹理。在绘图循环中,您可以参考这些纹理ID将它们链接到着色器中的均匀纹理,最终用于绘制。几乎可以在任何地方找到进行YUV到RGB转换的着色器示例。

#1


Yes, you use frame->data[]. Typically everyone uses shaders/textures, so you should too. Just upload them as textures, use code like this to upload the texture:

是的,你使用frame-> data []。通常每个人都使用着色器/纹理,所以你也应该这样做。只需将它们作为纹理上传,使用这样的代码上传纹理:

glPixelStorei(GL_UNPACK_ROW_LENGTH, frame->linesize[0])
glActiveTexture(GL_TEXTURE0 + id);
glBindTexture(GL_TEXTURE_2D, texture[id]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, frame->width, frame->height,
             0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame->data[0]);

Upload data[1] and [2] in the same way but apply chroma subsamplings to width/height (e.g. uvwidth = (width + 1) >> 1). This code also assumes that you've created 3 textures in an array called texture[]. In your drawing loop, you can then refer to these texture ids to link them to uniform textures in your shaders, which you eventually use to draw. Shader examples that do YUV-to-RGB conversion can be found virtually anywhere.

以相同的方式上传数据[1]和[2],但将色度子采样应用于宽度/高度(例如,uvwidth =(width + 1)>> 1)。此代码还假设您在名为texture []的数组中创建了3个纹理。在绘图循环中,您可以参考这些纹理ID将它们链接到着色器中的均匀纹理,最终用于绘制。几乎可以在任何地方找到进行YUV到RGB转换的着色器示例。