在GLFW窗口中显示FFMPEG解码帧

问题描述 投票:0回答:1

我正在实现游戏的客户端程序,其中服务器将游戏的编码帧(通过UDP)发送给客户端,而客户端则对它们进行解码(通过FFMPEG)并将其显示在GLFW窗口中。我的程序有两个线程:

  1. 线程1:呈现uint8_t *变量dataToRender的内容
  2. 线程2:不断从服务器获取帧,对其进行解码并相应地更新dataToRender

线程1在while循环中进行GLFW窗口的典型渲染。我已经尝试显示一些虚拟帧数据(完全红色的帧),并且可以正常工作:

while (!glfwWindowShouldClose(window)) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    ...

    glBindTexture(GL_TEXTURE_2D, tex_handle);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, window_width, window_height, 0, GL_RGB, GL_UNSIGNED_BYTE, dataToRender);
    ...
    glfwSwapBuffers(window);
}

线程2是我遇到麻烦的地方。我无法正确将解码的帧存储到我的dataToRender变量中。最重要的是,帧数据最初是YUV格式,需要转换为RGB。为此,我使用了FFMPEG的sws_scale,在控制台中也给了我bad dst image pointers错误输出。这是负责该部分的代码片段:

        size_t data_size = frameBuffer.size();  // frameBuffer is a std::vector where I accumulate the frame data chunks
        uint8_t* data = frameBuffer.data();  // convert the vector to a pointer
        picture->format = AV_PIX_FMT_RGB24;
        av_frame_get_buffer(picture, 1);
        while (data_size > 0) {
            int ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
                data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
            if (ret < 0) {
                fprintf(stderr, "Error while parsing\n");
                exit(1);
            }
            data += ret;
            data_size -= ret;

            if (pkt->size) {
                swsContext = sws_getContext(
                    c->width, c->height,
                    AV_PIX_FMT_YUV420P, c->width, c->height,
                    AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL
                );
                uint8_t* rgb24[1] = { data };
                int rgb24_stride[1] = { 3 * c->width };
                sws_scale(swsContext, rgb24, rgb24_stride, 0, c->height, picture->data, picture->linesize);

                decode(c, picture, pkt, outname);
                // TODO: copy content of picture->data[0] to "dataToRender" maybe?
            }
        }

我已经尝试过执行另一个sws_scale来将内容复制到dataToRender,但我无法摆脱bad dst image pointers错误。对问题的任何建议或解决方案将不胜感激,因为我在此问题上待了几天。

c++ ffmpeg decode glfw swscale
1个回答
0
投票

我认为您应该使用OpenGL将YUV转换为RGB。那是非常高效率和简单的。片段着色器如下所示:

precision mediump float;
varying vec2 v_texPo;
uniform sampler2D sampler_y;
uniform sampler2D sampler_u;
uniform sampler2D sampler_v;

void main() {
    float y, u, v;
    vec3 rgb;
    y = texture2D(sampler_y, v_texPo).r;
    u = texture2D(sampler_u, v_texPo).r - 0.5;
    v = texture2D(sampler_v, v_texPo).r - 0.5;
    rgb.r = y + 1.403 * v;
    rgb.g = y - 0.344 * u - 0.714 * v;
    rgb.b = y + 1.770 * u;
    gl_FragColor = vec4(rgb, 1);
}

并且您应该将三个纹理上传到OpenGL。

© www.soinside.com 2019 - 2024. All rights reserved.