4
votes

I'm trying to convert an AVFrame from a JPEG (YUV pixel format) to an RGB24 format using ffmpeg's sws_scale function. I set up the SwsContext as follows:

struct SwsContext *sws_ctx = NULL;
    int frameFinished;
    AVPacket packet;
    // initialize SWS context for software scaling
    sws_ctx = sws_getContext(pCodecCtx->width,
        pCodecCtx->height,
        pCodecCtx->pix_fmt,
        pCodecCtx->width,
        pCodecCtx->height,
        AV_PIX_FMT_RGB24,
        SWS_BICUBIC,
        NULL, NULL, NULL
        );

And then, I perform the sws_scale, with the following command

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

which gives me a segfault, though I'm not sure why. I've tried examining the values through prints and the heights and linesizes and everything all appear to have valid values.

1
At a first glance everything seems right. Double-check if pFrame->data contains 3 planes with YUV (420P?) data, if pFrameRGB->linesize[0] is at least 3 times bigger than pCodecCtx->width, and if pFrameRGB->data contains 1 plane which is at least pFrameRGB->linesize[0] * pCodecCtx->height big. If everything seems ok then see if segfault occurs when reading value or when writing (at least you'll know if it's source or destination frame).Andrey Turkin

1 Answers

2
votes

For anyone who comes on this in the future, my issues was that I was not properly initalizing pFrame and pFrameRGB. The memory first has to be allocated for the frame struct using av_frame_alloc(), then the data buffers must be allocated with av_image_alloc().