c++ - ffmpeg YUV420 到 RGB24 只转换一行

标签 c++ bitmap ffmpeg swscale

我正在尝试在 C++ 中将我的 YUV420p 图像转换为 RGB24,并在 C# 中从字节数组创建位图。

我的图像尺寸为 1920 w * 1020 h,ffmpeg 解码器为我提供了 3 个平面数据,其线宽 = {1920, 960, 960}。但是在 sws_scale 之后,我得到的 RGB 图片只有一个平面,linesize = 5760。 它看起来不正确:我应该得到 (5760 * h),而不仅仅是一行数据。我做错了什么?

 //c++ part
    if (avcodec_receive_frame(m_decoderContext, pFrame) == 0)
    {
        //RGB
        sws_ctx = sws_getContext(m_decoderContext->width,
            m_decoderContext->height,
            m_decoderContext->pix_fmt,
            m_decoderContext->width,
            m_decoderContext->height,
            AV_PIX_FMT_RGB24,
            SWS_BILINEAR,
            NULL,
            NULL,
            NULL
        );

        sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data, pFrame->linesize,
            0, pFrame->height,
            pFrameRGB->data, pFrameRGB->linesize);


//c# part (im reading data from pipe and its equal to c++ part)------------------------------------------------------------------
        byte[] rgbch = new byte[frameLen];
        for (int i=0; i<frameLen; i++)
        {
            rgbch[i] = Convert.ToByte(pipe.ReadByte());
        }

        if (rgbch.Length > 0)
        {
            var arrayHandle = System.Runtime.InteropServices.GCHandle.Alloc(rgbch,
    System.Runtime.InteropServices.GCHandleType.Pinned);

            var bmp = new Bitmap(1920, 1080,
                3,
                System.Drawing.Imaging.PixelFormat.Format24bppRgb,
                arrayHandle.AddrOfPinnedObject()
            );

            pictureBox1.Image = bmp;
        }

最佳答案

您假设 AVFramelinesize 字段是数据总量不正确。正如变量的名称所示,它是单行的长度,而 sws_scale 的返回值会为您提供行数。因此,输出位图的总内存范围大小是 linesize 乘以返回值。

关于c++ - ffmpeg YUV420 到 RGB24 只转换一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38869961/

相关文章:

c++ - mac build 有 undefined symbol ,而 linux build 没有相同的代码

android - 保存前隐藏多个 View - Android

路径中的 Android 位图

FFMPEG 将缩略图附加到视频的第一帧

android - 如何使用 ffmpeg 和 sdl 为 Android 制作视频播放器?

C++ function_pointer 和 &function_pointer 有什么区别?

c++ - Qt5/C++ 在 mousePressEvent 期间释放鼠标

c++ - 转换运算符重载

c++ - 如何在 XP 样式按钮上显示位图/图标

ffmpeg - 在 docker 容器中录制本地音频