mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-05-28 21:16:56 +08:00
Implement av_find_best_stream.
Originally committed as revision 26104 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
@ -2458,6 +2458,67 @@ int av_find_stream_info(AVFormatContext *ic)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static AVProgram *find_program_from_stream(AVFormatContext *ic, int s)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < ic->nb_programs; i++)
|
||||
for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
|
||||
if (ic->programs[i]->stream_index[j] == s)
|
||||
return ic->programs[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int av_find_best_stream(AVFormatContext *ic,
|
||||
enum AVMediaType type,
|
||||
int wanted_stream_nb,
|
||||
int related_stream,
|
||||
AVCodec **decoder_ret,
|
||||
int flags)
|
||||
{
|
||||
int i, nb_streams = ic->nb_streams, stream_number = 0;
|
||||
int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
|
||||
unsigned *program = NULL;
|
||||
AVCodec *decoder = NULL, *best_decoder = NULL;
|
||||
|
||||
if (related_stream >= 0 && wanted_stream_nb < 0) {
|
||||
AVProgram *p = find_program_from_stream(ic, related_stream);
|
||||
if (p) {
|
||||
program = p->stream_index;
|
||||
nb_streams = p->nb_stream_indexes;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < nb_streams; i++) {
|
||||
AVStream *st = ic->streams[program ? program[i] : i];
|
||||
AVCodecContext *avctx = st->codec;
|
||||
if (avctx->codec_type != type)
|
||||
continue;
|
||||
if (wanted_stream_nb >= 0 && stream_number++ != wanted_stream_nb)
|
||||
continue;
|
||||
if (decoder_ret) {
|
||||
decoder = avcodec_find_decoder(ic->streams[i]->codec->codec_id);
|
||||
if (!decoder) {
|
||||
if (ret < 0)
|
||||
ret = AVERROR_DECODER_NOT_FOUND;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (best_count >= st->codec_info_nb_frames)
|
||||
continue;
|
||||
best_count = st->codec_info_nb_frames;
|
||||
ret = i;
|
||||
best_decoder = decoder;
|
||||
if (program && i == nb_streams - 1 && ret < 0) {
|
||||
program = NULL;
|
||||
nb_streams = ic->nb_streams;
|
||||
i = 0; /* no related stream found, try again with everything */
|
||||
}
|
||||
}
|
||||
if (decoder_ret)
|
||||
*decoder_ret = best_decoder;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*******************************************************/
|
||||
|
||||
int av_read_play(AVFormatContext *s)
|
||||
|
Reference in New Issue
Block a user