我正在尝试将c++中的YUV420p图像转换为RGB24并从C#中的字节数组创建位图。
我的图像大小为1920 w * 1020 h,ffmpeg解码器为我提供了3个平面,行尺寸= {1920,960,960}。但是在sws_scale之后,我得到的是只有一个平面的RGB图片,其中lineize = 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;
}
最佳答案
您认为AVFrame的linesize
字段是数据总量是不正确的。正如变量名所指出的那样,它是单行的长度,而sws_scale
的返回值提供了行数。因此,输出位图的总内存范围大小是linesize
乘以返回值。