[Libav-user] AVIOContext opaque pointer

Wiley Black supergeniuscoyote at hotmail.com
Sun May 8 06:55:07 CEST 2011

I am trying to create a custom AVIOContext so that I can "catch" the output of FFmpeg and direct it where I need it (not a file).  I have done this successfully to the point of generating a playable video file (which I am sending to a file during development, but won't later.)  After the file is generated, however, I run into memory cleanup problems in avio_close().  The crash occurs even if I immediately call avio_close() after avio_alloc_context(), and I think I have an idea why:
I am passing an 'opaque' pointer to the avio_alloc_context() / AVIOContext which points back to my redirecting class/object so that my callback functions know where their object lives.  This certainly appears to work for me, so I was assuming that was the purpose of 'opaque', and I have it pointing to my own object type.
However, when I look at the source code in aviobuf.c, avio_close(), we have:
int avio_close(AVIOContext *s){    URLContext *h = s->opaque;
    av_free(s->buffer);    av_free(s);    return ffurl_close(h);}
Note that opaque is expected to have type 'URLContext *'.  I tracked my memory crash to the ffurl_close() line, so:
int ffurl_close(URLContext *h){    int ret = 0;    if (!h) return 0; /* can happen when ffurl_open fails */
    if (h->is_connected && h->prot->url_close)        ret = h->prot->url_close(h);#if CONFIG_NETWORK    ff_network_close();#endif    if (h->prot->priv_data_size)        av_free(h->priv_data);    av_free(h);    return ret;}
And my problem becomes rather obvious - AVIOContext->opaque is expected to point to a structure of type URLContext for this close routine, not to a custom type to be passed in to my callback routines.  The code crashes when it tries to access h->prot, although all of the ffurl_close() will be problematic unless h (which is AVIOContext->opaque) references a URLContext structure.
I have no idea what the url_open(), ffurl_...(), etc. routines are used for, and can't think of any reason that I need them in my project.  However, I do need the opaque pointer sent to my callback routines.
A workaround would be to avoid calling avio_close() and instead call av_free() directly on the buffer and AVIOContext.  However, before I resort to a workaround, I wanted to check my understanding of the situation against people more familiar with FFmpeg's code?  Can you guys please verify or correct my interpretation of the matter?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20110507/2399ae8d/attachment.html>

More information about the Libav-user mailing list