c++ffmpegrgbyuvavcodec

avcodec YUV to RGB


I'm trying to convert an YUV frame to RGB using libswscale.

Here is my code :

AVFrame *RGBFrame;
SwsContext *ConversionContext;

ConversionContext = sws_getCachedContext(NULL, FrameWidth, FrameHeight, AV_PIX_FMT_YUV420P, FrameWidth, FrameHeight, AV_PIX_FMT_RGB24, SWS_BILINEAR, 0, 0, 0);
RGBFrame = av_frame_alloc();

avpicture_fill((AVPicture *)RGBFrame, &FillVect[0], AV_PIX_FMT_RGB24, FrameWidth, FrameHeight);

sws_scale(ConversionContext, VideoFrame->data, VideoFrame->linesize, 0, VideoFrame->height, RGBFrame->data, RGBFrame->linesize);

My program do SEGFAULT on the sws_scale function.

VideoFrame is an AVFrame struct who hold my decoded frame.

I think this is because the YUV frame come from avcodec_decode_video2, which return an array like this :

VideoFrame->data[0] // Y array, linesize = frame width
VideoFrame->data[1] // U array, linesize = frame width/2
VideoFrame->data[2] // V array, linesize = frame width/2

While YUV420P have theoretically only one plane (according to Wikipedia, YUV420P is a planar format, then Y, U, V data are grouped together). So, i don't know how to proceed to convert my array where Y, U, V data are separated into RGB24, using swscale.

Please help me, thanks :)


Solution

  • av_frame_alloc only allocates memory for the frame object itself, it does not allocate memory to store the image data. Have you done:

    FillVect.resize( avpicture_get_size( PIX_FMT_RGB24, FrameWidth, FrameHeight ) );

    somewhere in your code before calling avpicture_fill? Or some other way to make sure FillVect allocates enough memory to keep the whole decoded picture?

    Did you try to run it under valgrind to see what exactly trigges SEGFAULT?