c++ - 如何将 ffmpeg 视频帧转换为 YUV444?

标签 c++ c ffmpeg yuv subsampling

我一直在关注 tutorial关于如何使用ffmpeg和 SDL 制作一个没有音频的简单视频播放器(目前)。在浏览教程时,我意识到它已经过时了,而且它使用的许多函数,包括 ffmpeg 和 SDL,都被弃用了。所以我搜索了一个最新的解决方案并找到了一个 stackoverflow 问题 answer这完成了教程所缺少的内容。

但是,它使用的是低质量的 YUV420。我想实现 YUV444,在研究了一些色度二次采样并查看了 YUV 的不同格式后,我对如何实现它感到困惑。据我了解,YUV420 的质量是 YUV444 的四分之一。 YUV444 意味着每个像素都有自己的色度样本,因此更详细,而 YUV420 意味着像素被分组在一起并具有相同的色度样本,因此不太详细。

根据我的理解,不同格式的 YUV(420, 422, 444) 在排列 y、u 和 v 的方式上有所不同。所有这些都有点让人不知所措,因为我对编解码器的处理不多,转换等。非常感谢任何帮助,如果需要其他信息,请在投票前告诉我。

这是来自 answer 的代码我提到了关于转换为 YUV420 的问题:

texture = SDL_CreateTexture(
        renderer,
        SDL_PIXELFORMAT_YV12,
        SDL_TEXTUREACCESS_STREAMING,
        pCodecCtx->width,
        pCodecCtx->height
        );
    if (!texture) {
        fprintf(stderr, "SDL: could not create texture - exiting\n");
        exit(1);
    }

    // initialize SWS context for software scaling
    sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
        pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
        AV_PIX_FMT_YUV420P,
        SWS_BILINEAR,
        NULL,
        NULL,
        NULL);

    // set up YV12 pixel array (12 bits per pixel)
    yPlaneSz = pCodecCtx->width * pCodecCtx->height;
    uvPlaneSz = pCodecCtx->width * pCodecCtx->height / 4;
    yPlane = (Uint8*)malloc(yPlaneSz);
    uPlane = (Uint8*)malloc(uvPlaneSz);
    vPlane = (Uint8*)malloc(uvPlaneSz);
    if (!yPlane || !uPlane || !vPlane) {
        fprintf(stderr, "Could not allocate pixel buffers - exiting\n");
        exit(1);
    }

    uvPitch = pCodecCtx->width / 2;
    while (av_read_frame(pFormatCtx, &packet) >= 0) {
        // Is this a packet from the video stream?
        if (packet.stream_index == videoStream) {
            // Decode video frame
            avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

            // Did we get a video frame?
            if (frameFinished) {
                AVPicture pict;
                pict.data[0] = yPlane;
                pict.data[1] = uPlane;
                pict.data[2] = vPlane;
                pict.linesize[0] = pCodecCtx->width;
                pict.linesize[1] = uvPitch;
                pict.linesize[2] = uvPitch;

                // Convert the image into YUV format that SDL uses
                sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
                    pFrame->linesize, 0, pCodecCtx->height, pict.data,
                    pict.linesize);

                SDL_UpdateYUVTexture(
                    texture,
                    NULL,
                    yPlane,
                    pCodecCtx->width,
                    uPlane,
                    uvPitch,
                    vPlane,
                    uvPitch
                    );

                SDL_RenderClear(renderer);
                SDL_RenderCopy(renderer, texture, NULL, NULL);
                SDL_RenderPresent(renderer);

            }
        }

        // Free the packet that was allocated by av_read_frame
        av_free_packet(&packet);
        SDL_PollEvent(&event);
        switch (event.type) {
            case SDL_QUIT:
                SDL_DestroyTexture(texture);
                SDL_DestroyRenderer(renderer);
                SDL_DestroyWindow(screen);
                SDL_Quit();
                exit(0);
                break;
            default:
                break;
        }

    }

    // Free the YUV frame
    av_frame_free(&pFrame);
    free(yPlane);
    free(uPlane);
    free(vPlane);

    // Close the codec
    avcodec_close(pCodecCtx);
    avcodec_close(pCodecCtxOrig);

    // Close the video file
    avformat_close_input(&pFormatCtx);

编辑:

经过更多研究后,我了解到在 YUV420 中,首先存储所有 Y,然后依次存储 U 和 V 字节的组合,如下图所示:
(来源:wikimedia.org)

但是我也了解到 YUV444 是按照 U、Y、V 的顺序存储的,并且像这张图所示的那样重复:

我尝试改变代码中的一些东西:

    // I changed SDL_PIXELFORMAT_YV12 to SDL_PIXELFORMAT_UYVY
    // as to reflect the order of YUV444
    texture = SDL_CreateTexture(
        renderer,
        SDL_PIXELFORMAT_UYVY,
        SDL_TEXTUREACCESS_STREAMING,
        pCodecCtx->width,
        pCodecCtx->height
        );
    if (!texture) {
        fprintf(stderr, "SDL: could not create texture - exiting\n");
        exit(1);
    }

    // Changed AV_PIX_FMT_YUV420P to AV_PIX_FMT_YUV444P
    // for rather obvious reasons
    sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
        pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
        AV_PIX_FMT_YUV444P,
        SWS_BILINEAR,
        NULL,
        NULL,
        NULL);

    // There are as many Y, U and V bytes as pixels I just
    // made yPlaneSz and uvPlaneSz equal to the number of pixels
    yPlaneSz = pCodecCtx->width * pCodecCtx->height;
    uvPlaneSz = pCodecCtx->width * pCodecCtx->height;
    yPlane = (Uint8*)malloc(yPlaneSz);
    uPlane = (Uint8*)malloc(uvPlaneSz);
    vPlane = (Uint8*)malloc(uvPlaneSz);
    if (!yPlane || !uPlane || !vPlane) {
        fprintf(stderr, "Could not allocate pixel buffers - exiting\n");
        exit(1);
    }

    uvPitch = pCodecCtx->width * 2;
    while (av_read_frame(pFormatCtx, &packet) >= 0) {
        // Is this a packet from the video stream?
        if (packet.stream_index == videoStream) {
            // Decode video frame
            avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

            // Rearranged the order of the planes to reflect UYV order
            // then set linesize to the number of Y, U and V bytes
            // per row
            if (frameFinished) {
                AVPicture pict;
                pict.data[0] = uPlane;
                pict.data[1] = yPlane;
                pict.data[2] = vPlane;
                pict.linesize[0] = pCodecCtx->width;
                pict.linesize[1] = pCodecCtx->width;
                pict.linesize[2] = pCodecCtx->width;

                // Convert the image into YUV format that SDL uses
                sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,
                    pFrame->linesize, 0, pCodecCtx->height, pict.data,
                    pict.linesize);

                SDL_UpdateYUVTexture(
                    texture,
                    NULL,
                    yPlane,
                    1,
                    uPlane,
                    uvPitch,
                    vPlane,
                    uvPitch
                    );
//.................................................

但现在我在调用 SDL_UpdateYUVTexture 时遇到了访问冲突...老实说,我不确定哪里出了问题。我认为这可能与 AVPicture pic 的成员 datalinesize 设置不当有关,但我不是肯定的。

最佳答案

在网上搜索可能的答案数小时后,我偶然发现了这个 post其中有人询问 YUV444 对打包或平面模式的支持。我发现的唯一当前格式是打包的 AYUV。

他们得到的答案是所有当前支持的格式列表,其中不包括 AYUV。因此SDL不支持YUV444。

唯一的解决方案是使用支持 AYUV/YUV444 的不同库。

关于c++ - 如何将 ffmpeg 视频帧转换为 YUV444?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49101684/

相关文章:

ffmpeg - 如何在 FFMPEG 中使用多个调色板生成参数

ffmpeg - 使用 FFMPEG 将 YUV 帧转换为 RGBA 帧

c++ - 类有虚方法但非虚析构函数C++

C++ std::mem_fn 和 std::bind 反过来

c# - Pinvoke 返回 int, API

c - 检测连接

c - 不确定是什么导致我的链表数据在函数之间丢失

c++ - 我可以在不推送的情况下向 vector 添加值吗?

从 gcc 4.2 迁移到 4.8 时代码生成段错误

python - 在 Mac 上安装 FFMPEG