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;

    int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1, best_bitrate = -1, best_multiframe = -1, count, bitrate, multiframe;

    unsigned *program = NULL;

    const AVCodec *decoder = NULL, *best_decoder = NULL;



    if (related_stream >= 0 && wanted_stream_nb < 0) {

        AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream);

        if (p) {

            program    = p->stream_index;

            nb_streams = p->nb_stream_indexes;

        }

    }

    for (i = 0; i < nb_streams; i++) {

        int real_stream_index = program ? program[i] : i;

        AVStream *st          = ic->streams[real_stream_index];

        AVCodecContext *avctx = st->codec;

        if (avctx->codec_type != type)

            continue;

        if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)

            continue;

        if (wanted_stream_nb != real_stream_index &&

            st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED |

                               AV_DISPOSITION_VISUAL_IMPAIRED))

            continue;

        if (type == AVMEDIA_TYPE_AUDIO && !avctx->channels)

            continue;

        if (decoder_ret) {

            decoder = find_decoder(ic, st, st->codec->codec_id);

            if (!decoder) {

                if (ret < 0)

                    ret = AVERROR_DECODER_NOT_FOUND;

                continue;

            }

        }

        count = st->codec_info_nb_frames;

        bitrate = avctx->bit_rate;

        if (!bitrate)

            bitrate = avctx->rc_max_rate;

        multiframe = FFMIN(5, count);

        if ((best_multiframe >  multiframe) ||

            (best_multiframe == multiframe && best_bitrate >  bitrate) ||

            (best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))

            continue;

        best_count   = count;

        best_bitrate = bitrate;

        best_multiframe = multiframe;

        ret          = real_stream_index;

        best_decoder = decoder;

        if (program && i == nb_streams - 1 && ret < 0) {

            program    = NULL;

            nb_streams = ic->nb_streams;

            /* no related stream found, try again with everything */

            i = 0;

        }

    }

    if (decoder_ret)

        *decoder_ret = (AVCodec*)best_decoder;

    return ret;

}

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;

    int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1, best_bitrate = -1, best_multiframe = -1, count, bitrate, multiframe;

    unsigned *program = NULL;

    const AVCodec *decoder = NULL, *best_decoder = NULL;



    if (related_stream >= 0 && wanted_stream_nb < 0) {

        AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream);

        if (p) {

            program    = p->stream_index;

            nb_streams = p->nb_stream_indexes;

        }

    }

    for (i = 0; i < nb_streams; i++) {

        int real_stream_index = program ? program[i] : i;

        AVStream *st          = ic->streams[real_stream_index];

        AVCodecContext *avctx = st->codec;

        if (avctx->codec_type != type)

            continue;

        if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)

            continue;

        if (wanted_stream_nb != real_stream_index &&

            st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED |

                               AV_DISPOSITION_VISUAL_IMPAIRED))

            continue;

        if (type == AVMEDIA_TYPE_AUDIO && !avctx->channels)

            continue;

        if (decoder_ret) {

            decoder = find_decoder(ic, st, st->codec->codec_id);

            if (!decoder) {

                if (ret < 0)

                    ret = AVERROR_DECODER_NOT_FOUND;

                continue;

            }

        }

        count = st->codec_info_nb_frames;

        bitrate = avctx->bit_rate;

        if (!bitrate)

            bitrate = avctx->rc_max_rate;

        multiframe = FFMIN(5, count);

        if ((best_multiframe >  multiframe) ||

            (best_multiframe == multiframe && best_bitrate >  bitrate) ||

            (best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))

            continue;

        best_count   = count;

        best_bitrate = bitrate;

        best_multiframe = multiframe;

        ret          = real_stream_index;

        best_decoder = decoder;

        if (program && i == nb_streams - 1 && ret < 0) {

            program    = NULL;

            nb_streams = ic->nb_streams;

            

            i = 0;

        }

    }

    if (decoder_ret)

        *decoder_ret = (AVCodec*)best_decoder;

    return ret;

}

int FUNC_0(AVFormatContext *VAR_0, enum AVMediaType VAR_1,

                        int VAR_2, int VAR_3,

                        AVCodec **VAR_4, int VAR_5)

{

    int VAR_6, VAR_7 = VAR_0->VAR_7;

    int VAR_8 = AVERROR_STREAM_NOT_FOUND, VAR_9 = -1, VAR_10 = -1, VAR_11 = -1, VAR_12, VAR_13, VAR_14;

    unsigned *VAR_15 = NULL;

    const AVCodec *VAR_16 = NULL, *best_decoder = NULL;



    if (VAR_3 >= 0 && VAR_2 < 0) {

        AVProgram *p = av_find_program_from_stream(VAR_0, NULL, VAR_3);

        if (p) {

            VAR_15    = p->stream_index;

            VAR_7 = p->nb_stream_indexes;

        }

    }

    for (VAR_6 = 0; VAR_6 < VAR_7; VAR_6++) {

        int VAR_17 = VAR_15 ? VAR_15[VAR_6] : VAR_6;

        AVStream *st          = VAR_0->streams[VAR_17];

        AVCodecContext *avctx = st->codec;

        if (avctx->codec_type != VAR_1)

            continue;

        if (VAR_2 >= 0 && VAR_17 != VAR_2)

            continue;

        if (VAR_2 != VAR_17 &&

            st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED |

                               AV_DISPOSITION_VISUAL_IMPAIRED))

            continue;

        if (VAR_1 == AVMEDIA_TYPE_AUDIO && !avctx->channels)

            continue;

        if (VAR_4) {

            VAR_16 = find_decoder(VAR_0, st, st->codec->codec_id);

            if (!VAR_16) {

                if (VAR_8 < 0)

                    VAR_8 = AVERROR_DECODER_NOT_FOUND;

                continue;

            }

        }

        VAR_12 = st->codec_info_nb_frames;

        VAR_13 = avctx->bit_rate;

        if (!VAR_13)

            VAR_13 = avctx->rc_max_rate;

        VAR_14 = FFMIN(5, VAR_12);

        if ((VAR_11 >  VAR_14) ||

            (VAR_11 == VAR_14 && VAR_10 >  VAR_13) ||

            (VAR_11 == VAR_14 && VAR_10 == VAR_13 && VAR_9 >= VAR_12))

            continue;

        VAR_9   = VAR_12;

        VAR_10 = VAR_13;

        VAR_11 = VAR_14;

        VAR_8          = VAR_17;

        best_decoder = VAR_16;

        if (VAR_15 && VAR_6 == VAR_7 - 1 && VAR_8 < 0) {

            VAR_15    = NULL;

            VAR_7 = VAR_0->VAR_7;

            

            VAR_6 = 0;

        }

    }

    if (VAR_4)

        *VAR_4 = (AVCodec*)best_decoder;

    return VAR_8;

}

int FUNC_0(AVFormatContext *VAR_0, enum AVMediaType VAR_1,
int VAR_2, int VAR_3,
AVCodec **VAR_4, int VAR_5)
{ int VAR_6, VAR_7 = VAR_0->VAR_7; int VAR_8 = AVERROR_STREAM_NOT_FOUND, VAR_9 = -1, VAR_10 = -1, VAR_11 = -1, VAR_12, VAR_13, VAR_14; unsigned *VAR_15 = NULL; const AVCodec *VAR_16 = NULL, *best_decoder = NULL; if (VAR_3 >= 0 && VAR_2 < 0) { AVProgram *p = av_find_program_from_stream(VAR_0, NULL, VAR_3); if (p) { VAR_15    = p->stream_index; VAR_7 = p->nb_stream_indexes; } } for (VAR_6 = 0; VAR_6 < VAR_7; VAR_6++) { int VAR_17 = VAR_15 ? VAR_15[VAR_6] : VAR_6; AVStream *st          = VAR_0->streams[VAR_17]; AVCodecContext *avctx = st->codec; if (avctx->codec_type != VAR_1)
continue; if (VAR_2 >= 0 && VAR_17 != VAR_2)
continue; if (VAR_2 != VAR_17 &&
st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED |
AV_DISPOSITION_VISUAL_IMPAIRED))
continue; if (VAR_1 == AVMEDIA_TYPE_AUDIO && !avctx->channels)
continue; if (VAR_4) { VAR_16 = find_decoder(VAR_0, st, st->codec->codec_id); if (!VAR_16) { if (VAR_8 < 0)
VAR_8 = AVERROR_DECODER_NOT_FOUND; continue; } } VAR_12 = st->codec_info_nb_frames; VAR_13 = avctx->bit_rate; if (!VAR_13)
VAR_13 = avctx->rc_max_rate; VAR_14 = FFMIN(5, VAR_12); if ((VAR_11 >  VAR_14) ||
(VAR_11 == VAR_14 && VAR_10 >  VAR_13) ||
(VAR_11 == VAR_14 && VAR_10 == VAR_13 && VAR_9 >= VAR_12))
continue; VAR_9   = VAR_12; VAR_10 = VAR_13; VAR_11 = VAR_14; VAR_8          = VAR_17; best_decoder = VAR_16; if (VAR_15 && VAR_6 == VAR_7 - 1 && VAR_8 < 0) { VAR_15    = NULL; VAR_7 = VAR_0->VAR_7; VAR_6 = 0; } } if (VAR_4)
*VAR_4 = (AVCodec*)best_decoder; return VAR_8; }