[Libav-user] How to re-use the same buffer for frame->data for multiple frames?

ggeng george at visby.io
Tue Dec 18 22:40:46 EET 2018


Hello,

I am using my own get_buffer2 callback function inside a class for decoding
the frames of a video, since I need to be able to manage my own memory for
storing the decoded frame data. It generally looks like:

int get_buffer_custom2(AVCodecContext *ctxt, AVFrame *frame, int flags) {
    assert(ctxt->codec_type == AVMEDIA_TYPE_VIDEO);

    *// Determine required buffer size and allocate buffer *
    numBytes = av_image_get_buffer_size(pixFmt, width, height, 1);
    dataBuff = (uint8_t*)(malloc(numBytes * sizeof(uint8_t)));
    
    *// Assign appropriate parts of buffer to image planes in frame *	
    ret = av_image_fill_arrays(frame->data, frame->linesize, dataBuff,
pixFmt, width, height, 1);
    if (ret < 0) 
        return ret;
   
    *// Create buffers and assign to frame->buff*
    frame->buf[0] = av_buffer_create(frame->data[0], frame->linesize[0] *
frame->height, 
        default_buffer_free, NULL, 0);
    frame->buf[1] = av_buffer_create(frame->data[1], frame->linesize[1] *
frame->height / 2, 
        default_buffer_free, NULL, 0);
    frame->buf[2] = av_buffer_create(frame->data[2], frame->linesize[2] *
frame->height / 2, 
        default_buffer_free, NULL, 0);
    return 0;
}

static void default_buffer_free(void *opaque, uint8_t *data) {}

This works, but the bottleneck is the repeated malloc'ing for dataBuff, so I
would like to know if it is possible to pre-allocate the space for dataBuff
once, and then re-use the memory for every frame of the video.

I have tried allocating dataBuff (a class variable) once during
initialization and removing the malloc call inside get_buffer_custom2. While
the code still compiles and runs, the decoded frames are now distorted and
full of artifacts from different frames.

Some things I have tried include memsetting dataBuff and frame->data to all
0s inside get_buffer_custom2, but nothing fixes the problem.

Thank you very much,
G



--
Sent from: http://libav-users.943685.n4.nabble.com/


More information about the Libav-user mailing list