[Libav-user] how to use own buffers for input data with av_read_frame()

Alex Cohn alexcohn at netvision.net.il
Sun Sep 2 14:50:12 CEST 2012


On Thu, Aug 30, 2012 at 9:34 AM, Jan Pohanka <xhpohanka at gmail.com> wrote:
> Hello,
>
> I'm implementing support for hardware video decoder on DM365 and I have some
> problems with buffers. I have simple testing application with following code
> (simplified)
>
> ...
> avformat_open_input(&fctx, filename, NULL, NULL);
> ...
> av_find_stream_info(fctx);
> ...
> codec = avcodec_find_decoder_by_name("libdm365_h264");
> ...
> ret = avcodec_open(avctx, codec)
> ...
> picture = avcodec_alloc_frame();
> ...
> for (i = 0; i < 500; i++) {
>     int nb;
>     char fname[32];
>
>     if (av_read_frame(fctx, &pkt) < 0)
>         break;
>
>     nb = avcodec_decode_video2(avctx, picture, &got_pic, &pkt);
>     if (nb < 0) {
>         av_log(avctx, AV_LOG_ERROR, "error in decoding\n");
>         goto decode_cleanup;
>     }
>     printf("Decoded frame: %d\n", i);
>     sprintf(fname, "frame%02d.jpg", i);
>     save_image(picture, avctx->pix_fmt, avctx->width, avctx->height, fname);
> }
>
> The problem is that the decoder needs to have both input and output data in
> a buffer which is continuous in physical memory. I can fulfill this
> constraint for the output data (AVFrame *picture) because the buffer is
> allocated by codec itself so I can use my own allocator there.
> Unfortunately I do not know how to handle data of AVPacktet pkt, which are
> filled in by av_read_frame() - these are allocated by av_malloc which
> fallbacks to malloc and the buffers probably won't be continuous.
>
> I see two solutions here, but I'm not happy with neither of them:
> - copy the whole packet to the continuous buffer (but this will
> significantly slower the application)
> - provide own av_malloc wrapper that will use my own allocator (many things
> from libav* internals will be also allocated there and this is not exactly
> what I want)
>
>
> Do please someone know some more elegant solution?
>
> with best regards
> Jan
>
> --
> Tato zpráva byla vytvořena převratným poštovním klientem Opery:
> http://www.opera.com/mail/

If I understand your question correctly, you are worried that some an
AVPacket could be using more than one chunk of memory for the
(encoded) bytes read from the h264 stream. To the best of my
knowledge, all packet data that is relevant for decoder is contiguous
(pkt.data); the extra pieces that could be present (pkt.side_data) do
not belong to hardware decoder anyway.

BR,
Alex Cohn


More information about the Libav-user mailing list