Jason C Jason C - 1 month ago 12
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
AVCodec
:


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


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

However, the official demuxing + decoding example uses
av_find_best_stream
to find a stream, but chooses to use
avcodec_find_decoder
to find the codec in lieu of
av_find_best_stream
'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
av_find_best_stream
's return parameter vs. using
avcodec_find_decoder
to find the
AVCodec
?

The reason I ask is because the example chose to use
avcodec_find_decoder
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.

Answer

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.

Comments