其他分享
首页 > 其他分享> > avformat_find_stream_Info

avformat_find_stream_Info

作者:互联网

avformat_find_stream_Info声明:

所属库:libavformat(lavf)
头文件:avformat.h
声明: 从如下声明的解释可以获知以下几点信息
1)该函数将读取媒体文件的音视频包去获取流信息。本函数常用于avformat_open_input()函数之后,在avformat_open_input()函数中会调用输入文件格式的read_header()函数,比如flv格式的flv_read_header()函数来读取文件头,由于flv格式的头很简单,只能知道是否存在音频流和视频流,获取不到流的编码信息,因此,对于flv格式来说,本函数就非常重要,本函数会读取flv文件中的音视频包,从这些包中获知流的编解码信息。对于没有文件头的MPEG格式来说存在同样的情况。
2)本函数还会在MPEG的重复帧模式下计算真实的帧率。
3)本函数不会改变文件的逻辑位置(即程序访问文件时的文件偏移offset),那些读取并用来做检测的数据包将被缓冲,并留作后续处理使用。
4) 本函数的入参AVDictionary **options如果不为空,那么该入参是一个AVDictionary列表,第几个AVDictionary就作用于AVFormatContext.nb_streams的第几个流,如果在对应的流中找不到相应的选项,函数返回时,那么该入参中还会保留着没有找到的选项
5)本函数不会保证打开所有的编解码器,因此,在函数返回时,选项非空是正常行为。
6)本函数通过options来让用户决定哪些信息是需要的,因此,不浪费时间在获取一些用户根本需要的信息。

/**
* 读取媒体文件的数据包以获取流信息。 这个
  * 对于没有标题的文件格式(如 MPEG)很有用。 这个
  * 在 MPEG-2 重复的情况下,函数还计算实际帧率
  * 帧模式。
  * 逻辑文件位置不会被这个函数改变;
  * 检查的数据包可能会被缓冲以供以后处理。
  *
  * @param ic 媒体文件句柄
  * @param options 如果非 NULL,一个 ic.nb_streams 长指针数组
  * 字典,其中第 i 个成员包含以下选项
  * 对应于第 i 个流的编解码器。
  * 返回时,每本词典将填充未找到的选项。
  * @return >=0 如果正常,AVERROR_xxx 出错
  *
  * @note 这个函数不能保证打开所有的编解码器,所以
  * 返回时非空的选项是完全正常的行为。
  *
  * @todo 让用户以某种方式决定需要什么信息,以便
  * 我们不会浪费时间获取用户不需要的东西。
 */
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);

avformat_find_stream_Info源码:

int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
{
    /*** 第一部分:申明一些局部变量,并初始化 ***/
    int i, count = 0, ret = 0, j;
    int64_t read_size;
    AVStream *st;
    AVCodecContext *avctx;
    AVPacket pkt1, *pkt;
    // 获取已消费完毕的数据对应于文件偏移量,注意不是已读取数据对应文件偏移量
    int64_t old_offset  = avio_tell(ic->pb);
    // new streams might appear, no options for those
    // 在avformat_open_input函数中可能已经new出AVStream,nb_streams也对应流个数
    int orig_nb_streams = ic->nb_streams;
    int flush_codecs;
    // 在AVFormatContext初始化中已经设置该值为默认值0
    int64_t max_analyze_duration = ic->max_analyze_duration;
    int64_t max_stream_analyze_duration;
    int64_t max_subtitle_analyze_duration;
    // 在AVFormatContext初始化中已经设置该值为默认值为5000000
    int64_t probesize = ic->probesize;
    int eof_reached = 0;
    // 若在avformat_open_input分析输入封装格式时,会读取文件头部来看是否存在流来给missing_streams赋值
    int *missing_streams = av_opt_ptr(ic->iformat->priv_class, ic->priv_data, "missing_streams");
 
    flush_codecs = probesize > 0;
 
    av_opt_set(ic, "skip_clear", "1", AV_OPT_SEARCH_CHILDREN);
 
    // 由于max_analyze_duration大概率为0(因为被初始化为0,中间被修改的概率比较小),因此一般情况下,会进入if(){...}语句中执行
    max_stream_analyze_duration = max_analyze_duration;
    max_subtitle_analyze_duration = max_analyze_duration;
    if (!max_analyze_duration) {
        // 分析流的最大时长和分析文件的最大时长均设置为5倍AV_TIME_BASE, AV_TIME_BASE为1000000,而max_stream_analyze_duration和max_analyze_duration单位刚好为微妙,结合起来正好描述为5s重
        max_stream_analyze_duration =
        max_analyze_duration        = 5*AV_TIME_BASE;
        // 同理,分析字幕时的时长为30s                    
        max_subtitle_analyze_duration = 30*AV_TIME_BASE;
        // 如果是flv文件格式,流分析时长延长到90s
        if (!strcmp(ic->iformat->name, "flv"))
            max_stream_analyze_duration = 90*AV_TIME_BASE;
        // 如果是mpeg格式,mpegts格式,流分析时长为7s
        if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
            max_stream_analyze_duration = 7*AV_TIME_BASE;
    }
 
    // 如果AVIOContext已经存在,那么也许已经读取了数据,这儿输出当前io读取情况。
    // avio_tell(ic->pb)输出已读取并处理完的数据偏移位置(文件偏移量),
    // bytes_read为已读取数据字节数,seek_count是已经做过多少次seek操作, nb_streams为流个数
    if (ic->pb)
        av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
               avio_tell(ic->pb), ic->pb->bytes_read, ic->pb->seek_count, ic->nb_streams);
 
    // 第一个for循环,做什么的?
    // 赋值AVStream->AVStreamInternal编码器参数值,并通过打开流的解码器来验证这些参数值是否正确
    for (i = 0; i < ic->nb_streams; i++) {
        const AVCodec *codec;
        AVDictionary *thread_opt = NULL;
        st = ic->streams[i];
        avctx = st->internal->avctx;
 
        // 在文件格式读取文件头的函数中,会解析并给AVStream->time_base时间基赋值
        // 复制时间基到AVStream->AVStreamInternal->AVCodecContext->time_base中
        if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
            st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
/*            if (!st->time_base.num)
                st->time_base = */
            if (!avctx->time_base.num)
                avctx->time_base = st->time_base;
        }
 
        // 外部用户修改了AVStream的AVCodecContext,那么这儿保持AVStream的AVCodecParameters中 
        // codec_id, codec_type, AVStream的AVStreamInternal中的orig_codec_id,与AVStream的 
        // AVCodecContext中的数据保持一致。
        /* check if the caller has overridden the codec id */
#if FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
        if (st->codec->codec_id != st->internal->orig_codec_id) {
            st->codecpar->codec_id   = st->codec->codec_id;
            st->codecpar->codec_type = st->codec->codec_type;
            st->internal->orig_codec_id = st->codec->codec_id;
        }
FF_ENABLE_DEPRECATION_WARNINGS
#endif
 
        // 查看流分析需不需要解析器,解析器用来从流中分离出单独的视频帧和音频帧
        // only for the split stuff
        if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && st->request_probe <= 0) {
            st->parser = av_parser_init(st->codecpar->codec_id);
            if (st->parser) {
                if (st->need_parsing == AVSTREAM_PARSE_HEADERS) {
                    st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
                } else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
                    st->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
                }
            } else if (st->need_parsing) {
                av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
                       "%s, packets or times may be invalid.\n",
                       avcodec_get_name(st->codecpar->codec_id));
            }
        }
 
 
        // 确保AVStream->AVStreaminternal中关于编解码器的相关参数正确
        if (st->codecpar->codec_id != st->internal->orig_codec_id)
            st->internal->orig_codec_id = st->codecpar->codec_id;
        ret = avcodec_parameters_to_context(avctx, st->codecpar); // 复制编解码器参数
        if (ret < 0)
            goto find_stream_info_err;
        if (st->request_probe <= 0)
            st->internal->avctx_inited = 1; // 设置初始化标志位
 
        // 查找解码器
        codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
 
        // 设置解码线程为1个
        /* Force thread count to 1 since the H.264 decoder will not extract
         * SPS and PPS to extradata during multi-threaded decoding. */
        av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
 
        if (ic->codec_whitelist)
            av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
 
        // 对于字幕流,尝试打开解码器,确保字幕头是否正确的设置
        /* Ensure that subtitle_header is properly set. */
        if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE
            && codec && !avctx->codec) {
            if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
                av_log(ic, AV_LOG_WARNING,
                       "Failed to open codec in %s\n",__FUNCTION__);
        }
 
        // 尝试打开解码器,看是否已获得足够的编解码参数
        // Try to just open decoders, in case this is enough to get parameters.
        if (!has_codec_parameters(st, NULL) && st->request_probe <= 0) {
            if (codec && !avctx->codec)
                if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
                    av_log(ic, AV_LOG_WARNING,
                           "Failed to open codec in %s\n",__FUNCTION__);
        }
        if (!options)
            av_dict_free(&thread_opt);
    }
 
    // 第二个for循环,做什么的?
    // 给AVStream内部使用的info结构体的一些成员赋初始值,这几个成员用于平均帧率估计
    for (i = 0; i < ic->nb_streams; i++) {
#if FF_API_R_FRAME_RATE
        ic->streams[i]->info->last_dts = AV_NOPTS_VALUE;
#endif
        ic->streams[i]->info->fps_first_dts = AV_NOPTS_VALUE;
        ic->streams[i]->info->fps_last_dts  = AV_NOPTS_VALUE;
    }
 
    // 第三个for循环,死循环,做什么的?
    // 
    read_size = 0;
    for (;;) {
        int analyzed_all_streams;
        // 检测是否有外部中断,可以帮助快速退出循环,终止程序运行,ic->interrupt_callback
        // 由用户定义,因此,何时需要终止程序是受用户支配的。
        if (ff_check_interrupt(&ic->interrupt_callback)) {
            ret = AVERROR_EXIT;
            av_log(ic, AV_LOG_DEBUG, "interrupted\n");
            break;
        }
 
        // 
        /* check if one codec still needs to be handled */
        for (i = 0; i < ic->nb_streams; i++) {
            int fps_analyze_framecount = 20;
            int count;
 
            st = ic->streams[i];
            if (!has_codec_parameters(st, NULL))
                break;
            /* If the timebase is coarse (like the usual millisecond precision
             * of mkv), we need to analyze more frames to reliably arrive at
             * the correct fps. */
            if (av_q2d(st->time_base) > 0.0005)
                fps_analyze_framecount *= 2;
            if (!tb_unreliable(st->internal->avctx))
                fps_analyze_framecount = 0;
            if (ic->fps_probe_size >= 0)
                fps_analyze_framecount = ic->fps_probe_size;
            if (st->disposition & AV_DISPOSITION_ATTACHED_PIC)
                fps_analyze_framecount = 0;
            /* variable fps and no guess at the real fps */
            count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
                       st->info->codec_info_duration_fields/2 :
                       st->info->duration_count;
            if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
                st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
                if (count < fps_analyze_framecount)
                    break;
            }
            // Look at the first 3 frames if there is evidence of frame delay
            // but the decoder delay is not set.
            if (st->info->frame_delay_evidence && count < 2 && st->internal->avctx->has_b_frames == 0)
                break;
            if (!st->internal->avctx->extradata &&
                (!st->internal->extract_extradata.inited ||
                 st->internal->extract_extradata.bsf) &&
                extract_extradata_check(st))
                break;
            if (st->first_dts == AV_NOPTS_VALUE &&
                !(ic->iformat->flags & AVFMT_NOTIMESTAMPS) &&
                st->codec_info_nb_frames < ((st->disposition & AV_DISPOSITION_ATTACHED_PIC) ? 1 : ic->max_ts_probe) &&
                (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
                 st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))
                break;
        }
        analyzed_all_streams = 0;
        if (!missing_streams || !*missing_streams)
        if (i == ic->nb_streams) {
            analyzed_all_streams = 1;
            /* NOTE: If the format has no header, then we need to read some
             * packets to get most of the streams, so we cannot stop here. */
            if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
                /* If we found the info for all the codecs, we can stop. */
                ret = count;
                av_log(ic, AV_LOG_DEBUG, "All info found\n");
                flush_codecs = 0;
                break;
            }
        }
        /* We did not get all the codec info, but we read too much data. */
        if (read_size >= probesize) {
            ret = count;
            av_log(ic, AV_LOG_DEBUG,
                   "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
            for (i = 0; i < ic->nb_streams; i++)
                if (!ic->streams[i]->r_frame_rate.num &&
                    ic->streams[i]->info->duration_count <= 1 &&
                    ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
                    strcmp(ic->iformat->name, "image2"))
                    av_log(ic, AV_LOG_WARNING,
                           "Stream #%d: not enough frames to estimate rate; "
                           "consider increasing probesize\n", i);
            break;
        }
 
        /* NOTE: A new stream can be added there if no header in file
         * (AVFMTCTX_NOHEADER). */
        ret = read_frame_internal(ic, &pkt1);
        if (ret == AVERROR(EAGAIN))
            continue;
 
        if (ret < 0) {
            /* EOF or error*/
            eof_reached = 1;
            break;
        }
 
        pkt = &pkt1;
 
        if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
            ret = ff_packet_list_put(&ic->internal->packet_buffer,
                                     &ic->internal->packet_buffer_end,
                                     pkt, 0);
            if (ret < 0)
                goto find_stream_info_err;
        }
 
        st = ic->streams[pkt->stream_index];
        if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
            read_size += pkt->size;
 
        avctx = st->internal->avctx;
        if (!st->internal->avctx_inited) {
            ret = avcodec_parameters_to_context(avctx, st->codecpar);
            if (ret < 0)
                goto find_stream_info_err;
            st->internal->avctx_inited = 1;
        }
 
        if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > 1) {
            /* check for non-increasing dts */
            if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
                st->info->fps_last_dts >= pkt->dts) {
                av_log(ic, AV_LOG_DEBUG,
                       "Non-increasing DTS in stream %d: packet %d with DTS "
                       "%"PRId64", packet %d with DTS %"PRId64"\n",
                       st->index, st->info->fps_last_dts_idx,
                       st->info->fps_last_dts, st->codec_info_nb_frames,
                       pkt->dts);
                st->info->fps_first_dts =
                st->info->fps_last_dts  = AV_NOPTS_VALUE;
            }
            /* Check for a discontinuity in dts. If the difference in dts
             * is more than 1000 times the average packet duration in the
             * sequence, we treat it as a discontinuity. */
            if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
                st->info->fps_last_dts_idx > st->info->fps_first_dts_idx &&
                (pkt->dts - st->info->fps_last_dts) / 1000 >
                (st->info->fps_last_dts     - (uint64_t)st->info->fps_first_dts) /
                (st->info->fps_last_dts_idx - st->info->fps_first_dts_idx)) {
                av_log(ic, AV_LOG_WARNING,
                       "DTS discontinuity in stream %d: packet %d with DTS "
                       "%"PRId64", packet %d with DTS %"PRId64"\n",
                       st->index, st->info->fps_last_dts_idx,
                       st->info->fps_last_dts, st->codec_info_nb_frames,
                       pkt->dts);
                st->info->fps_first_dts =
                st->info->fps_last_dts  = AV_NOPTS_VALUE;
            }
 
            /* update stored dts values */
            if (st->info->fps_first_dts == AV_NOPTS_VALUE) {
                st->info->fps_first_dts     = pkt->dts;
                st->info->fps_first_dts_idx = st->codec_info_nb_frames;
            }
            st->info->fps_last_dts     = pkt->dts;
            st->info->fps_last_dts_idx = st->codec_info_nb_frames;
        }
        if (st->codec_info_nb_frames>1) {
            int64_t t = 0;
            int64_t limit;
 
            if (st->time_base.den > 0)
                t = av_rescale_q(st->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q);
            if (st->avg_frame_rate.num > 0)
                t = FFMAX(t, av_rescale_q(st->codec_info_nb_frames, av_inv_q(st->avg_frame_rate), AV_TIME_BASE_Q));
 
            if (   t == 0
                && st->codec_info_nb_frames>30
                && st->info->fps_first_dts != AV_NOPTS_VALUE
                && st->info->fps_last_dts  != AV_NOPTS_VALUE)
                t = FFMAX(t, av_rescale_q(st->info->fps_last_dts - st->info->fps_first_dts, st->time_base, AV_TIME_BASE_Q));
 
            if (analyzed_all_streams)                                limit = max_analyze_duration;
            else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
            else                                                     limit = max_stream_analyze_duration;
 
            if (t >= limit) {
                av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
                       limit,
                       t, pkt->stream_index);
                if (ic->flags & AVFMT_FLAG_NOBUFFER)
                    av_packet_unref(pkt);
                break;
            }
            if (pkt->duration) {
                if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE && pkt->pts != AV_NOPTS_VALUE && pkt->pts >= st->start_time) {
                    st->info->codec_info_duration = FFMIN(pkt->pts - st->start_time, st->info->codec_info_duration + pkt->duration);
                } else
                    st->info->codec_info_duration += pkt->duration;
                st->info->codec_info_duration_fields += st->parser && st->need_parsing && avctx->ticks_per_frame ==2 ? st->parser->repeat_pict + 1 : 2;
            }
        }
        if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
#if FF_API_R_FRAME_RATE
            ff_rfps_add_frame(ic, st, pkt->dts);
#endif
            if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
                st->info->frame_delay_evidence = 1;
        }
        if (!st->internal->avctx->extradata) {
            ret = extract_extradata(st, pkt);
            if (ret < 0)
                goto find_stream_info_err;
        }
 
        /* If still no information, we try to open the codec and to
         * decompress the frame. We try to avoid that in most cases as
         * it takes longer and uses more memory. For MPEG-4, we need to
         * decompress for QuickTime.
         *
         * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
         * least one frame of codec data, this makes sure the codec initializes
         * the channel configuration and does not only trust the values from
         * the container. */
        try_decode_frame(ic, st, pkt,
                         (options && i < orig_nb_streams) ? &options[i] : NULL);
 
        if (ic->flags & AVFMT_FLAG_NOBUFFER)
            av_packet_unref(pkt);
 
        st->codec_info_nb_frames++;
        count++;
    }
 
    if (eof_reached) {
        int stream_index;
        for (stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
            st = ic->streams[stream_index];
            avctx = st->internal->avctx;
            if (!has_codec_parameters(st, NULL)) {
                const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
                if (codec && !avctx->codec) {
                    AVDictionary *opts = NULL;
                    if (ic->codec_whitelist)
                        av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
                    if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0)
                        av_log(ic, AV_LOG_WARNING,
                               "Failed to open codec in %s\n",__FUNCTION__);
                    av_dict_free(&opts);
                }
            }
 
            // EOF already reached while reading the stream above.
            // So continue with reoordering DTS with whatever delay we have.
            if (ic->internal->packet_buffer && !has_decode_delay_been_guessed(st)) {
                update_dts_from_pts(ic, stream_index, ic->internal->packet_buffer);
            }
        }
    }
 
    if (flush_codecs) {
        AVPacket empty_pkt = { 0 };
        int err = 0;
        av_init_packet(&empty_pkt);
 
        for (i = 0; i < ic->nb_streams; i++) {
 
            st = ic->streams[i];
 
            /* flush the decoders */
            if (st->info->found_decoder == 1) {
                do {
                    err = try_decode_frame(ic, st, &empty_pkt,
                                            (options && i < orig_nb_streams)
                                            ? &options[i] : NULL);
                } while (err > 0 && !has_codec_parameters(st, NULL));
 
                if (err < 0) {
                    av_log(ic, AV_LOG_INFO,
                        "decoding for stream %d failed\n", st->index);
                }
            }
        }
    }
 
    ff_rfps_calculate(ic);
 
    for (i = 0; i < ic->nb_streams; i++) {
        st = ic->streams[i];
        avctx = st->internal->avctx;
        if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
            if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
                uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
                if (avpriv_find_pix_fmt(avpriv_get_raw_pix_fmt_tags(), tag) == avctx->pix_fmt)
                    avctx->codec_tag= tag;
            }
 
            /* estimate average framerate if not set by demuxer */
            if (st->info->codec_info_duration_fields &&
                !st->avg_frame_rate.num &&
                st->info->codec_info_duration) {
                int best_fps      = 0;
                double best_error = 0.01;
                AVRational codec_frame_rate = avctx->framerate;
 
                if (st->info->codec_info_duration        >= INT64_MAX / st->time_base.num / 2||
                    st->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
                    st->info->codec_info_duration        < 0)
                    continue;
                av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
                          st->info->codec_info_duration_fields * (int64_t) st->time_base.den,
                          st->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
 
                /* Round guessed framerate to a "standard" framerate if it's
                 * within 1% of the original estimate. */
                for (j = 0; j < MAX_STD_TIMEBASES; j++) {
                    AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
                    double error       = fabs(av_q2d(st->avg_frame_rate) /
                                              av_q2d(std_fps) - 1);
 
                    if (error < best_error) {
                        best_error = error;
                        best_fps   = std_fps.num;
                    }
 
                    if (ic->internal->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
                        error       = fabs(av_q2d(codec_frame_rate) /
                                           av_q2d(std_fps) - 1);
                        if (error < best_error) {
                            best_error = error;
                            best_fps   = std_fps.num;
                        }
                    }
                }
                if (best_fps)
                    av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
                              best_fps, 12 * 1001, INT_MAX);
            }
 
            if (!st->r_frame_rate.num) {
                if (    avctx->time_base.den * (int64_t) st->time_base.num
                    <= avctx->time_base.num * avctx->ticks_per_frame * (int64_t) st->time_base.den) {
                    av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
                              avctx->time_base.den, (int64_t)avctx->time_base.num * avctx->ticks_per_frame, INT_MAX);
                } else {
                    st->r_frame_rate.num = st->time_base.den;
                    st->r_frame_rate.den = st->time_base.num;
                }
            }
            if (st->display_aspect_ratio.num && st->display_aspect_ratio.den) {
                AVRational hw_ratio = { avctx->height, avctx->width };
                st->sample_aspect_ratio = av_mul_q(st->display_aspect_ratio,
                                                   hw_ratio);
            }
        } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
            if (!avctx->bits_per_coded_sample)
                avctx->bits_per_coded_sample =
                    av_get_bits_per_sample(avctx->codec_id);
            // set stream disposition based on audio service type
            switch (avctx->audio_service_type) {
            case AV_AUDIO_SERVICE_TYPE_EFFECTS:
                st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
                break;
            case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
                st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
                break;
            case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
                st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
                break;
            case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
                st->disposition = AV_DISPOSITION_COMMENT;
                break;
            case AV_AUDIO_SERVICE_TYPE_KARAOKE:
                st->disposition = AV_DISPOSITION_KARAOKE;
                break;
            }
        }
    }
 
    if (probesize)
        estimate_timings(ic, old_offset);
 
    av_opt_set(ic, "skip_clear", "0", AV_OPT_SEARCH_CHILDREN);
 
    if (ret >= 0 && ic->nb_streams)
        /* We could not have all the codec parameters before EOF. */
        ret = -1;
    for (i = 0; i < ic->nb_streams; i++) {
        const char *errmsg;
        st = ic->streams[i];
 
        /* if no packet was ever seen, update context now for has_codec_parameters */
        if (!st->internal->avctx_inited) {
            if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
                st->codecpar->format == AV_SAMPLE_FMT_NONE)
                st->codecpar->format = st->internal->avctx->sample_fmt;
            ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
            if (ret < 0)
                goto find_stream_info_err;
        }
        if (!has_codec_parameters(st, &errmsg)) {
            char buf[256];
            avcodec_string(buf, sizeof(buf), st->internal->avctx, 0);
            av_log(ic, AV_LOG_WARNING,
                   "Could not find codec parameters for stream %d (%s): %s\n"
                   "Consider increasing the value for the 'analyzeduration' and 'probesize' options\n",
                   i, buf, errmsg);
        } else {
            ret = 0;
        }
    }
 
    compute_chapters_end(ic);
 
    /* update the stream parameters from the internal codec contexts */
    for (i = 0; i < ic->nb_streams; i++) {
        st = ic->streams[i];
 
        if (st->internal->avctx_inited) {
            int orig_w = st->codecpar->width;
            int orig_h = st->codecpar->height;
            ret = avcodec_parameters_from_context(st->codecpar, st->internal->avctx);
            if (ret < 0)
                goto find_stream_info_err;
#if FF_API_LOWRES
            // The decoder might reduce the video size by the lowres factor.
            if (st->internal->avctx->lowres && orig_w) {
                st->codecpar->width = orig_w;
                st->codecpar->height = orig_h;
            }
#endif
        }
 
#if FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
        ret = avcodec_parameters_to_context(st->codec, st->codecpar);
        if (ret < 0)
            goto find_stream_info_err;
 
#if FF_API_LOWRES
        // The old API (AVStream.codec) "requires" the resolution to be adjusted
        // by the lowres factor.
        if (st->internal->avctx->lowres && st->internal->avctx->width) {
            st->codec->lowres = st->internal->avctx->lowres;
            st->codec->width = st->internal->avctx->width;
            st->codec->height = st->internal->avctx->height;
        }
#endif
 
        if (st->codec->codec_tag != MKTAG('t','m','c','d')) {
            st->codec->time_base = st->internal->avctx->time_base;
            st->codec->ticks_per_frame = st->internal->avctx->ticks_per_frame;
        }
        st->codec->framerate = st->avg_frame_rate;
 
        if (st->internal->avctx->subtitle_header) {
            st->codec->subtitle_header = av_malloc(st->internal->avctx->subtitle_header_size);
            if (!st->codec->subtitle_header)
                goto find_stream_info_err;
            st->codec->subtitle_header_size = st->internal->avctx->subtitle_header_size;
            memcpy(st->codec->subtitle_header, st->internal->avctx->subtitle_header,
                   st->codec->subtitle_header_size);
        }
 
        // Fields unavailable in AVCodecParameters
        st->codec->coded_width = st->internal->avctx->coded_width;
        st->codec->coded_height = st->internal->avctx->coded_height;
        st->codec->properties = st->internal->avctx->properties;
FF_ENABLE_DEPRECATION_WARNINGS
#endif
 
        st->internal->avctx_inited = 0;
    }
 
find_stream_info_err:
    for (i = 0; i < ic->nb_streams; i++) {
        st = ic->streams[i];
        if (st->info)
            av_freep(&st->info->duration_error);
        avcodec_close(ic->streams[i]->internal->avctx);
        av_freep(&ic->streams[i]->info);
        av_bsf_free(&ic->streams[i]->internal->extract_extradata.bsf);
        av_packet_free(&ic->streams[i]->internal->extract_extradata.pkt);
    }
    if (ic->pb)
        av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
               avio_tell(ic->pb), ic->pb->bytes_read, ic->pb->seek_count, count);
    return ret;
}

本函数中,先对一部分数据进行声明和初始化,然后有多个类似于for (i = 0; i < ic->nb_streams; i++) {…}的循环处理,还有一个for(;;)死循环的处理。接下来就对整个过程做一个比较详细的分析:
1 局部变量的申明,以及数据初始化:
1)int64_t old_offset = avio_tell(ic->pb); 保存当前文件偏移量,由于avformat_open_input()函数中已经打开文件,并且读取了文件头,因此,文件偏移已经不为0,此时,该变量保存当前的文件偏移
2)int orig_nb_streams = ic->nb_streams; 保存avformat_open_input()读取文件头时获取的文件中流的个数,注意对于flv这种格式这个值为0.
3)int64_t max_analyze_duration = ic->max_analyze_duration; 保存最大的分析时长,ic->max_analyze_duration在构建AVFormatContext过程中被初始化为0,单位微妙
4)int64_t probesize = ic->probesize; 保存进行探测的数据大小,在AVFormatContext初始化中已经设置该值为默认值为5000000,5MB大小。
5)int missing_streams = av_opt_ptr(ic->iformat->priv_class, ic->priv_data, “missing_streams”); 保存文件中是否存在流的指示。avformat_open_input()中分析了flv_read_header()函数中就对该值进行了阐述,该值不为0,表示有音视频流的存在,为0表示不存在音视频流。
6) 流分析最大时长,最大分析时长,字幕分析时长的计算:分析时长的基本单位是微妙,而AV_TIME_BASE是1000000,那么可以由源码得知:通用情况下,流分析最大时长和最大分析时长为5
AV_TIME_BASE,即5s,字幕分析时长为30AV_TIME_BASE,即30s;特殊情况下,比如文件格式为flv格式,那么流分析最大时长更新为90AV_TIME_BASE,即90s,如果文件格式为mpeg或者是mpegts格式,那么流分析最大时长更新为7*AV_TIME_BASE,即7s。
第一次for循环遍历每个流:
1)复制时间基到AVStream->AVStreamInternal->AVCodecContext->time_base: 在文件格式读取文件头的函数中,会解析并给AVStream->time_base时间基赋值,此处将该时间基复制一份到AVStream->AVStreamInternal->AVCodecContext->time_base中,因为后续的解码,解码器使用AVStream->AVStreamInternal中的数据。
2)检查用户是否修改了解码器:外部用户修改了AVStream->AVCodecContext中的编解码器,那么这儿保持AVStream->AVCodecParameters->codec_id, codec_type, AVStream->AVStreamInternal->orig_codec_id与修改后的一致。
3)创建流的解析器:解析器用来从原始码流中分离出单独的一帧视频或音频,按需创建。
4)检查AVStream->AVStreaminternal中的编解码参数都被合理赋值:一方面是AVStream->AVStreaminternal->orig_codec_id要赋值为AVStream->codecpar->codec_id,AVStream->codecpar->codec_id在媒体打开函数avformat_open_input()中已经通过文件格式读取文件头的函数获知到正确的codec_id;另一方面AVStream->AVStreaminternal->AVCodecContext编解码器上下文从AVStream->AVCodecParamters中拷贝一份编解码参数,因为文件格式读取文件头的函数会分析文件头并给AVStream->AVCodecParamters赋值。
5)查找解码器,并打开解码器,从而对上述的解码器参数是否正确进行预判。
6)总结:由上分析可知,第一次for循环遍历流,目的在于给流的AVStream->AVStreamInternal中的编码器相关参数赋值(时间戳time_base,编码器orig_codec_id,编码器上下文结构avctx);然后找到每个流的解码器,通过打开解码器是否成功来验证上述赋值的编解码器参数是否正确。
第二次for循环遍历每个流:
本次遍历是对AVStream->info结构体的部分成员赋初始值,该结构体用于存储流信息,且仅供avformat_find_stream_info()函数使用。此次赋值的几个变量是用来进行平均帧率估计的参数。
** 第三次for循环,for(;;)死循环:**
1) 使用ff_check_interrupt(&ic->interrupt_callback) 检测是否有外部中断,可以帮助快速退出循环,终止程序运行,ic->interrupt_callback由用户定义,并在构建AVFormatContext对象时传入,因此,何时需要终止程序是受用户支配的。

标签:Info,info,st,avformat,codec,AV,ic,avctx,find
来源: https://blog.csdn.net/weixin_42123332/article/details/120724596