[Libav-user] Is it possible to preallocate memory for AVFrame data when calling avcodec_receive_frame?

Mark Thompson sw at jkqxz.net
Tue Dec 18 00:34:30 EET 2018


On 17/12/2018 18:45, ggeng wrote:
> Hi,
> 
> I am using libav to decode a video (following the demo code available
> online), and I am trying to pre-allocate memory that the decoded frame data
> will be written to. Ultimately, I would like to have pFrame->data[0] to be
> in a region of memory I pre-allocate.
> 
> Following examples online, I am able to set pFrame->data as so:
> 
> *// Determine required buffer size and allocate buffer*
> int numBytes;
> numBytes = av_image_get_buffer_size(pixFmt, width, height, 1);
> (uint8_t*) dataBuff = (uint8_t*) malloc (numBytes * sizeof(uint8_t));
> 
> *// Assign buffer to image planes in pFrame*
> av_image_fill_arrays(frame->data, frame->linesize, dataBuff, pixFmt, width,
> height, 1);
> 
> However, I found that this call:
> 
> ret = avcodec_receive_frame(pCodecContext, pFrame)
> 
> will always write the decoded frame data to be somewhere else, and allocate
> its own memory for pFrame->data, completely ignoring the dataBuffer I set to
> pFrame->data before.
> 
> Is there a way to get around this? Either by decoding video without a call
> to avcodec_receive_frame or a way to set the memory for the data?

Have a look at the AVCodecContext.get_buffer2 callback.

- Mark


More information about the Libav-user mailing list