[Libav-user] Memory allocation and avpicture_fill incorrect?

Stefano Sabatini stefasab at gmail.com
Thu Nov 22 11:37:10 CET 2012

On date Wednesday 2012-11-21 17:12:31 +0530, Navin wrote:
> I was trying out the updated tutorial01 code of
> ffmpeg:https://github.com/mpenkov/ffmpeg-tutorial
> since the program crashed unexpectedly at sws_scale with an "Access
> violation writing location" error, I was trying to figure out what
> went wrong.
> Two questions:

> 1. I looked into the source for avpicture_get_size and saw a lot of
> code that I couldn't understand. Why are such complex operations
> performed in the function with data and linesize, when the width,
> height and pixel format value multiplication should've easily given
> you the number of bytes?

Because that function is generic, and must work with all the supported
pixel formats.

> 2. In avpicture_fill, what exactly are we filling a pFrameRGB with?
> The video isn't yet decoded, so what is getting filled there?
> Looking through the source just left me confused.

That function is used to fill an AVPicture if you have an already
allocated buffer. Alternatively you can use avpicture_alloc() or
av_image_alloc() to allocate *and* fill the data/linesize arrays.

> I'm suspecting that the memory allocation is being done incorrectly,
> which is why sws_scale is crashing. I've written about this in
> another mailing list thread too, titled "get RGB values from ffmpeg
> frame". But this question is specific to understanding the
> allocation and filling.
> The code I'm talking about from tutorial01:
>   pFrameRGB = avcodec_alloc_frame();
>   if (pFrameRGB==NULL)  return -1;
>   // Determine required buffer size and allocate buffer
>   numBytes = avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
> pCodecCtx->height);
>   buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
>   sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
> pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
>   // Assign appropriate parts of buffer to image planes in
> pFrameRGB. Note that pFrameRGB is an AVFrame, but AVFrame is a
> superset of AVPicture
>   avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
> pCodecCtx->width, pCodecCtx->height);

You're not showing the crashing code.
FFmpeg = Freak and Friendly Mean Perfectionist Elastic Governor

More information about the Libav-user mailing list