Jason C Jason C - 14 days ago 4x
C Question

Decoder return of av_find_best_stream vs. avcodec_find_decoder

The docs for libav's

function (libav 11.7, Windows, i686, GPL) specify a parameter that can be used to receive a pointer to an appropriate

decoder_ret - if non-NULL, returns the decoder for the selected stream

There is also the
function which can find an
given an ID.

However, the official demuxing + decoding example uses
to find a stream, but chooses to use
to find the codec in lieu of
's codec return parameter:

ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
stream_index = ret;
st = fmt_ctx->streams[stream_index];
/* find decoder for the stream */
dec = avcodec_find_decoder(st->codecpar->codec_id);

As opposed to something like:

ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);

My question is pretty straightforward: Is there a difference between using
's return parameter vs. using
to find the

The reason I ask is because the example chose to use
rather than the seemingly more convenient return parameter, and I can't tell if the example did that for a specific reason or not. The documentation itself is a little spotty and disjoint, so it's hard to tell if things like this are done for a specific important reason or not. I can't tell if the example is implying that it "should" be done that way, or if the example author did it for some more arbitrary personal reason.


av_find_best_stream uses avcodec_find_decoder internally in pretty much the same way as in your code sample. However there is a change in av_find_best_stream behaviour when decoder is requested from it - namely, it will try to use avcodec_find_decoder on each candidate stream and if it fails then it will discard the candidate and move on to the next one. In the end it will return best stream together with its decoder. If decoder is not requested, it will just return best stream without checking if it can be decoded.

So if you just want to get single video/audio stream and you are not going to write some custom stream selection logic then I'd say there's no downside to using av_find_best_stream to get a decoder.