ffmpeg源码分析--av_find_best_stream <转>

1. av_find_best_stream
a. 就是要获取音视频及字幕的stream_index
b.以前没有函数av_find_best_stream时,获取索引可以通过如下

for(i=0; i<is->pFormatCtx->nb_streams; i++)
    { 
        if(is->pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            is->videoindex= i;
        }
        if(is->pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
        {
            is->sndindex= i;
        }
    }

2.video及audio的调用如下:

                       ic     type              wanted_stream_nb            related_stream             decoder_ret flag
st_index[AVMEDIA_TYPE_VIDEO] = 
   av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, st_index[AVMEDIA_TYPE_VIDEO],  -1,                         NULL,      0);
st_index[AVMEDIA_TYPE_AUDIO] =
   av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, st_index[AVMEDIA_TYPE_AUDIO],st_index[AVMEDIA_TYPE_VIDEO], NULL,      0);

3.说明

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) {                  //没看到有什么作用,即参数related_stream在这儿没有用
        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++) {                                   //对于只有音频与视频流的媒体文件来说nb_streams=2
        int real_stream_index = program ? program[i] : i;                //program=NULL,所以real_stream_index=i;
        AVStream *st = ic->streams[real_stream_index];
        AVCodecContext *avctx = st->codec;
        if (avctx->codec_type != type)                                    //以下三个if是过滤条件
            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 && avctx->sample_rate))
            continue;
        if (decoder_ret) {                                          //decoder_ret=NULL,所以下面这个find_decoder也没有调用
            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;                                  //到这儿real_stream_index就是匹配了三个if的index了,不匹配的都continue了
        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;                                                 //返回就完事了
}

av_find_best_stream函数其实跟以前实现思路是一样的,只不过这儿的条件更多一点而己。

转载地址:http://blog.chinaunix.net/uid-26009923-id-5730055.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值