[libav-api] How to play/decode audio file with libav

Ronald S. Bultje rsbultje at gmail.com
Wed Jun 29 02:05:28 CEST 2011


Hi.

On Mon, Jun 27, 2011 at 11:34 PM, Manuel Reimer
<manuel.spam at nurfuerspam.de> wrote:
> Ronald S. Bultje <rsbultje <at> gmail.com> writes:
>> You set your own I/O through a AVIOContext, which you set in
>> AVFormatContext->pb.
>
> Seems like I should use "av_open_input_stream", as the "avformat_open_input" is
> not supported on most distributions.

It's new API, av_open_input_stream/file will continue to work for a
while, so that's OK.

> So in short my code would look something like:
>
> | AVIOContext *pb;
> | pb = avio_alloc_context(...);
> | pb->is_streamed = 1;
> |
> | AVProbeData probe_data;
> | probe_data.buf_size = xxxxx;
> | probe_data.buf = ....;
> | AVInputFormat *fmt_in = av_probe_input_format(&probe_data, 1);
> |
> | av_open_input_stream(NULL, pb, NULL, fmt_in, NULL);
>
> Means, that I have to fetch a few bytes first to pass them to
> av_probe_input_format. Somewhere I've read that it may even be a good idea to
> try this a few times with increasing buffer sizes and data. Or can I avoid
> av_probe_input_format? Is there a common "input format" for all HTTP streams?
> What is an input format. Does this have something to do with selection of the
> right decoder?

Demuxer, actually. E.g. when you have a streamed .mp4 file or so, it
will select the mp4 demuxer. Same for wav, mp3, etc.

> I didn't find any good documentation for avio_alloc_context. Do I have to set a
> buffer and buffer_size there, if I want to pass a "read_packet" function
> pointer. Will libav provide a buffer for me, if I set both to NULL or is it my
> job to pre-create a buffer and libav then provides my own buffer to my
> read_packet function?

Exactly. It should not be NULL, typically it's one or a few pagesizes,
4k, 8k, 16k, 32k or so would all work fine. You allocate and pass to
avio_alloc_context().

> Can I just pass NULL for write_packet? What about seek,
> which doesn't apply to streams?

Yes, both can be NULL if you don't care about seeking.

> What is "opaque" meant for. May I pass a pointer
> to my "streaming class" here? Seems like it gets passed to my callback functions.

Opaque is anything you like, it's private data for yourself, yes, you
can retrieve it through AVIOContext->opaque so you don't need global
variables to store things like a pointer to your c++ class or
something like that.

Ronald


More information about the libav-api mailing list