您的位置:首页 > 其它

ffmpeg学习七:avformat_find_stream_info函数源码分析

2016-12-14 17:33 597 查看
前面两篇文章分析avformat_open_input和avcodec_open2两个函数,我们所做的函数分析工作都是为了能够很好的理解前面一篇博客:ffmpeg学习四:写第一个程序-视频解码中所给的视频解码的程序。avformat_find_stream_info函数也是视频解码程序中必须要有的函数,因此这篇文章主要来分析这个函数。

一、功能简介

先看看avformat_find_stream_info函数的注释:

/**
* Read packets of a media file to get stream information. This
* is useful for file formats with no headers such as MPEG. This
* function also computes the real framerate in case of MPEG-2 repeat
* frame mode.
* The logical file position is not changed by this function;
* examined packets may be buffered for later processing.
*
* @param ic media file handle
* @param options  If non-NULL, an ic.nb_streams long array of pointers to
*                 dictionaries, where i-th member contains options for
*                 codec corresponding to i-th stream.
*                 On return each dictionary will be filled with options that were not found.
* @return >=0 if OK, AVERROR_xxx on error
*
* @note this function isn't guaranteed to open all the codecs, so
*       options being non-empty at return is a perfectly normal behavior.
*
* @todo Let the user decide somehow what information is needed so that
*       we do not waste time getting stuff the user does not need.
*/
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);


从注释来看,avformat_find_stream_info主要是读一些包(packets ),然后从中提取初流的信息。有一些文件格式没有头,比如说MPEG格式的,这个时候,这个函数就很有用,因为它可以从读取到的包中获得到流的信息。在MPEG-2重复帧模式的情况下,该函数还计算真实的帧率。

逻辑文件位置不被此函数更改; 读出来的包会被缓存起来供以后处理。

注释很好的解释了这个函数的功能。我们可以想象一下,既然这个函数的功能是更新流的信息,那么可以猜测它的作用就是更新AVStream这个结构体中的字段了。我们先浏览下这个结构体:

二、AVStream结构体解析

/**
* Stream structure.
* New fields can be added to the end with minor version bumps.
* Removal, reordering and changes to existing fields require a major
* version bump.
* sizeof(AVStream) must not be used outside libav*.
*/
typedef struct AVStream {
int index;    /**< stream index in AVFormatContext */
/**
* Format-specific stream ID.
* decoding: set by libavformat
* encoding: set by the user, replaced by libavformat if left unset
*/
int id;
#if FF_API_LAVF_AVCTX
/**
* @deprecated use the codecpar struct instead
*/
attribute_deprecated
AVCodecContext *codec;
#endif
void *priv_data;

#if FF_API_LAVF_FRAC
/**
* @deprecated this field is unused
*/
attribute_deprecated
struct AVFrac pts;
#endif

/**
* This is the fundamental unit of time (in seconds) in terms
* of which frame timestamps are represented.
*
* decoding: set by libavformat
* encoding: May be set by the caller before avformat_write_header() to
*           provide a hint to the muxer about the desired timebase. In
*           avformat_write_header(), the muxer will overwrite this field
*           with the timebase that will actually be used for the timestamps
*           written into the file (which may or may not be related to the
*           user-provided one, depending on the format).
*/
AVRational time_base;

/**
* Decoding: pts of the first frame of the stream in presentation order, in stream time base.
* Only set this if you are absolutely 100% sure that the value you set
* it to really is the pts of the first frame.
* This may be undefined (AV_NOPTS_VALUE).
* @note The ASF header does NOT contain a correct start_time the ASF
* demuxer must NOT set this.
*/
int64_t start_time;

/**
* Decoding: duration of the stream, in stream time base.
* If a source file does not specify a duration, but does specify
* a bitrate, this value will be estimated from bitrate and file size.
*/
int64_t duration;

int64_t nb_frames;                 ///< number of frames in this stream if known or 0

int disposition; /**< AV_DISPOSITION_* bit field */

enum AVDiscard discard; ///< Selects which packets can be discarded at will and do not need to be demuxed.

/**
* sample aspect ratio (0 if unknown)
* - encoding: Set by user.
* - decoding: Set by libavformat.
*/
AVRational sample_aspect_ratio;

AVDictionary *metadata;

/**
* Average framerate
*
* - demuxing: May be set by libavformat when creating the stream or in
*             avformat_find_stream_info().
* - muxing: May be set by the caller before avformat_write_header().
*/
AVRational avg_frame_rate;

/**
* For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet
* will contain the attached picture.
*
* decoding: set by libavformat, must not be modified by the caller.
* encoding: unused
*/
AVPacket attached_pic;

/**
* An array of side data that applies to the whole stream (i.e. the
* container does not allow it to change between packets).
*
* There may be no overlap between the side data in this array and side data
* in the packets. I.e. a given side data is either exported by the muxer
* (demuxing) / set by the caller (muxing) in this array, then it never
* appears in the packets, or the side data is exported / sent through
* the packets (always in the first packet where the value becomes known or
* changes), then it does not appear in this array.
*
* - demuxing: Set by libavformat when the stream is created.
* - muxing: May be set by the caller before avformat_write_header().
*
* Freed by libavformat in avformat_free_context().
*
* @see av_format_inject_global_side_data()
*/
AVPacketSideData *side_data;
/**
* The number of elements in the AVStream.side_data array.
*/
int            nb_side_data;

/**
* Flags for the user to detect events happening on the stream. Flags must
* be cleared by the user once the event has been handled.
* A combination of AVSTREAM_EVENT_FLAG_*.
*/
int event_flags;
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED 0x0001 ///< The call resulted in updated metadata.

/*****************************************************************
* All fields below this line are not part of the public API. They
* may not be used outside of libavformat and can be changed and
* removed at will.
* New public fields should be added right above.
*****************************************************************
*/

/**
* Stream information used internally by av_find_stream_info()
*/
#define MAX_STD_TIMEBASES (30*12+30+3+6)
struct {
int64_t last_dts;
int64_t duration_gcd;
int duration_count;
int64_t rfps_duration_sum;
double (*duration_error)[2][MAX_STD_TIMEBASES];
int64_t codec_info_duration;
int64_t codec_info_duration_fields;

/**
* 0  -> decoder has not been searched for yet.
* >0 -> decoder found
* <0 -> decoder with codec_id == -found_decoder has not been found
*/
int found_decoder;

int64_t last_duration;

/**
* Those are used for average framerate estimation.
*/
int64_t fps_first_dts;
int     fps_first_dts_idx;
int64_t fps_last_dts;
int     fps_last_dts_idx;

} *info;

int pts_wrap_bits; /**< number of bits in pts (used for wrapping control) */

// Timestamp generation support:
/**
* Timestamp corresponding to the last dts sync point.
*
* Initialized when AVCodecParserContext.dts_sync_point >= 0 and
* a DTS is received from the underlying container. Otherwise set to
* AV_NOPTS_VALUE by default.
*/
int64_t first_dts;
int64_t cur_dts;
int64_t last_IP_pts;
int last_IP_duration;

/**
* Number of packets to buffer for codec probing
*/
int probe_packets;

/**
* Number of frames that have been demuxed during av_find_stream_info()
*/
int codec_info_nb_frames;

/* av_read_frame() support */
enum AVStreamParseType need_parsing;
struct AVCodecParserContext *parser;

/**
* last packet in packet_buffer for this stream when muxing.
*/
struct AVPacketList *last_in_packet_buffer;
AVProbeData probe_data;
#define MAX_REORDER_DELAY 16
int64_t pts_buffer[MAX_REORDER_DELAY+1];

AVIndexEntry *index_entries; /**< Only used if the format does not
support seeking natively. */
int nb_index_entries;
unsigned int index_entries_allocated_size;

/**
* Real base framerate of the stream.
* This is the lowest framerate with which all timestamps can be
* represented accurately (it is the least common multiple of all
* framerates in the stream). Note, this value is just a guess!
* For example, if the time base is 1/90000 and all frames have either
* approximately 3600 or 1800 timer ticks, then r_frame_rate will be 50/1.
*
* Code outside avformat should access this field using:
* av_stream_get/set_r_frame_rate(stream)
*/
AVRational r_frame_rate;

/**
* Stream Identifier
* This is the MPEG-TS stream identifier +1
* 0 means unknown
*/
int stream_identifier;

int64_t interleaver_chunk_size;
int64_t interleaver_chunk_duration;

/**
* stream probing state
* -1   -> probing finished
*  0   -> no probing requested
* rest -> perform probing with request_probe being the minimum score to accept.
* NOT PART OF PUBLIC API
*/
int request_probe;
/**
* Indicates that everything up to the next keyframe
* should be discarded.
*/
int skip_to_keyframe;

/**
* Number of samples to skip at the start of the frame decoded from the next packet.
*/
int skip_samples;

/**
* If not 0, the number of samples that should be skipped from the start of
* the stream (the samples are removed from packets with pts==0, which also
* assumes negative timestamps do not happen).
* Intended for use with formats such as mp3 with ad-hoc gapless audio
* support.
*/
int64_t start_skip_samples;

/**
* If not 0, the first audio sample that should be discarded from the stream.
* This is broken by design (needs global sample count), but can't be
* avoided for broken by design formats such as mp3 with ad-hoc gapless
* audio support.
*/
int64_t first_discard_sample;

/**
* The sample after last sample that is intended to be discarded after
* first_discard_sample. Works on frame boundaries only. Used to prevent
* early EOF if the gapless info is broken (considered concatenated mp3s).
*/
int64_t last_discard_sample;

/**
* Number of internally decoded frames, used internally in libavformat, do not access
* its lifetime differs from info which is why it is not in that structure.
*/
int nb_decoded_frames;

/**
* Timestamp offset added to timestamps before muxing
* NOT PART OF PUBLIC API
*/
int64_t mux_ts_offset;

/**
* Internal data to check for wrapping of the time stamp
*/
int64_t pts_wrap_reference;

/**
* Options for behavior, when a wrap is detected.
*
* Defined by AV_PTS_WRAP_ values.
*
* If correction is enabled, there are two possibilities:
* If the first time stamp is near the wrap point, the wrap offset
* will be subtracted, which will create negative time stamps.
* Otherwise the offset will be added.
*/
int pts_wrap_behavior;

/**
* Internal data to prevent doing update_initial_durations() twice
*/
int update_initial_durations_done;

/**
* Internal data to generate dts from pts
*/
int64_t pts_reorder_error[MAX_REORDER_DELAY+1];
uint8_t pts_reorder_error_count[MAX_REORDER_DELAY+1];

/**
* Internal data to analyze DTS and detect faulty mpeg streams
*/
int64_t last_dts_for_order_check;
uint8_t dts_ordered;
uint8_t dts_misordered;

/**
* Internal data to inject global side data
*/
int inject_global_side_data;

/**
* String containing paris of key and values describing recommended encoder configuration.
* Paris are separated by ','.
* Keys are separated from values by '='.
*/
char *recommended_encoder_configuration;

/**
* display aspect ratio (0 if unknown)
* - encoding: unused
* - decoding: Set by libavformat to calculate sample_aspect_ratio internally
*/
AVRational display_aspect_ratio;

struct FFFrac *priv_pts;

/**
* An opaque field for libavformat internal usage.
* Must not be accessed in any way by callers.
*/
AVStreamInternal *internal;

/*
* Codec parameters associated with this stream. Allocated and freed by
* libavformat in avformat_new_stream() and avformat_free_context()
* respectively.
*
* - demuxing: filled by libavformat on stream creation or in
*             avformat_find_stream_info()
* - muxing: filled by the caller before avformat_write_header()
*/
AVCodecParameters *codecpar;
} AVStream;


int index:该流的标示。

AVCodecContext *codec:该流的编解码器上下文。

AVRational time_base:时基。通过该值可以把PTS,DTS转化为真正的时间。FFMPEG其他结构体中也有这个字段,但是根据我的经验,只有AVStream中的time_base是可用的。PTS*time_base=真正的时间

start_time:流中第一个pts的时间。

nb_frames:这个流中的帧的数目。

AVRational avg_frame_rate;平均帧率。

int64_t duration:该流的时间长度

AVDictionary *metadata:元数据信息

AVPacket attached_pic:附带的图片。比如说一些MP3,AAC音频文件附带的专辑封面。

重要的数据结构框图如下:



下面来看这个函数的代码,源码在libavformat/utils.c文件中。

这个函数非常的长,我们分开来看.

三、函数源码分析

part 1 参数定义

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
int orig_nb_streams = ic->nb_streams;
int flush_codecs;
int64_t max_analyze_duration = ic->max_analyze_duration;
int64_t max_stream_analyze_duration;
int64_t max_subtitle_analyze_duration;
int64_t probesize = ic->probesize;
int eof_reached = 0;

flush_codecs = probesize > 0;
...


这里定义了一些参数:

probesize就是探测的大小,为了获得流的信息,这个函数会尝试的读一些包出来,然后分析这些数据,probesize限制 了最大允许读出的数据的大小。

orig_nb_streams是这个文件中的流的数量。普通的电影会包含三个流:音频流,视频流和字幕流。

接着往下看。

part 2 设置max_stream_analyze_duration 等

max_stream_analyze_duration = max_analyze_duration;
max_subtitle_analyze_duration = max_analyze_duration;
if (!max_analyze_duration) {
max_stream_analyze_duration =
max_analyze_duration        = 5*AV_TIME_BASE;
max_subtitle_analyze_duration = 30*AV_TIME_BASE;
if (!strcmp(ic->iformat->name, "flv"))
max_stream_analyze_duration = 90*AV_TIME_BASE;
if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
max_stream_analyze_duration = 7*AV_TIME_BASE;
}


这里定义了最大分析时长的限制。max_stream_analyze_duration 等于max_analyze_duration 等于30倍的timebase。此外,如果文件格式为flv或者Mpeg,那么他们会有各自的最大分析时长的限制。

继续往下看:

part 3 第一次遍历流

for (i = 0; i < ic->nb_streams; i++) {
const AVCodec *codec;
AVDictionary *thread_opt = NULL;
st = ic->streams[i];
avctx = st->internal->avctx;

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;
}

/* 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));
}
}

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);

/* 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 av_find_stream_info\n");
}

// 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 av_find_stream_info\n");
}
if (!options)
av_dict_free(&thread_opt);
}


这个函数有很多次遍历流的循环,这里是第一次。第一次循环做了如下事情:

1.获得编码器上下文环境avctx,设置avctx的time_base,code_id,codec_type,orig_codec_id。

2.如果解析器paser为空,那么会初始化解析器。

3.把解析器中的参数对应的拷贝到编解码器上下文环境中。调用的是avcodec_parameters_to_context函数,这个函数如下:

int avcodec_parameters_to_context(AVCodecContext *codec,
const AVCodecParameters *par)
{
codec->codec_type = par->codec_type;
codec->codec_id   = par->codec_id;
codec->codec_tag  = par->codec_tag;

codec->bit_rate              = par->bit_rate;
codec->bits_per_coded_sample = par->bits_per_coded_sample;
codec->bits_per_raw_sample   = par->bits_per_raw_sample;
codec->profile               = par->profile;
codec->level                 = par->level;

switch (par->codec_type) {
case AVMEDIA_TYPE_VIDEO:
codec->pix_fmt                = par->format;
codec->width                  = par->width;
codec->height                 = par->height;
codec->field_order            = par->field_order;
codec->color_range            = par->color_range;
codec->color_primaries        = par->color_primaries;
codec->color_trc              = par->color_trc;
codec->colorspace             = par->color_space;
codec->chroma_sample_location = par->chroma_location;
codec->sample_aspect_ratio    = par->sample_aspect_ratio;
codec->has_b_frames           = par->video_delay;
break;
case AVMEDIA_TYPE_AUDIO:
codec->sample_fmt       = par->format;
codec->channel_layout   = par->channel_layout;
codec->channels         = par->channels;
codec->sample_rate      = par->sample_rate;
codec->block_align      = par->block_align;
codec->frame_size       = par->frame_size;
codec->delay            =
codec->initial_padding  = par->initial_padding;
codec->trailing_padding = par->trailing_padding;
codec->seek_preroll     = par->seek_preroll;
break;
case AVMEDIA_TYPE_SUBTITLE:
codec->width  = par->width;
codec->height = par->height;
break;
}


可见就是根据编码器类型做对应的参数的拷贝。

4根据编码器ID查找编码器.

这里调用的是find_probe_decoder函数,该函数也定义在libavformat/utils.c中。可以看一下查找编解码器的过程:

static const AVCodec *find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
{
const AVCodec *codec;

#if CONFIG_H264_DECODER
/* Other parts of the code assume this decoder to be used for h264,
* so force it if possible. */
if (codec_id == AV_CODEC_ID_H264)
return avcodec_find_decoder_by_name("h264");
#endif

codec = find_decoder(s, st, codec_id);
if (!codec)
return NULL;

if (codec->capabilities & AV_CODEC_CAP_AVOID_PROBING) {
const AVCodec *probe_codec = NULL;
while (probe_codec = av_codec_next(probe_codec)) {
if (probe_codec->id == codec_id &&
av_codec_is_decoder(probe_codec) &&
!(probe_codec->capabilities & (AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_EXPERIMENTAL))) {
return probe_codec;
}
}
}

return codec;
}


调用find_decoder函数进一步查找:

static const AVCodec *find_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
{
#if FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
if (st->codec->codec)
return st->codec->codec;
FF_ENABLE_DEPRECATION_WARNINGS
#endif

switch (st->codecpar->codec_type) {
case AVMEDIA_TYPE_VIDEO:
if (s->video_codec)    return s->video_codec;
break;
case AVMEDIA_TYPE_AUDIO:
if (s->audio_codec)    return s->audio_codec;
break;
case AVMEDIA_TYPE_SUBTITLE:
if (s->subtitle_codec) return s->subtitle_codec;
break;
}

return avcodec_find_decoder(codec_id);
}


如果编码器已经存在就根据编码器类型返回对应的编解码器,否则就根据id进行查找。avcodec_find_decoder函数还是在utils.c文件中:

AVCodec *avcodec_find_decoder(enum AVCodecID id)
{
return find_encdec(id, 0);
}


find_encdec函数也是在utils.c文件中:

static AVCodec *find_encdec(enum AVCodecID id, int encoder)
{
AVCodec *p, *experimental = NULL;
p = first_avcodec;
id= remap_deprecated_codec_id(id);
while (p) {
if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
p->id == id) {
if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
experimental = p;
} else
return p;
}
p = p->next;
}
return experimental;
}


这个函数的功能就是遍历一个编解码器的链表,其首个结构是first_avcodec。编解码器链表是我们在av_register_all()函数中注册的。这个逐个匹配每一个编解码器的id,找到后返回对应的编解码器。

5.打开编解码器

打开使用的是avcodec_open2函数。这个函数我们在前一篇博客ffmpeg学习六:avcodec_open2函数源码分析一文中已经分析过了。

所以我们可以总结下第一次遍历所有的流所做的事情:初始化time_base,codec_id等参数,初始化解析器paser,然后对于每一个流,根据codec_id找对对应的编解码器,然后打开编解码器。也就是说,第一次遍历流,使得我们对于每一个流,都有一个编解码器可用。

part 4 第二次遍历流

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;
}


第二次遍历流是在编解码器已经打开之后,对于每一个流,设置了一些参数。last_dts 是最有的解码时间。fps_first_dts和fps_last_dts 用于帧率的计算。

part 5 死循环

接下来的这个死循环代码很长。我们回顾一下之前的工作,我们遍历了两次流,设置好了每个流需要的编解码器和计算帧率的参数,初始化了每个流的解析器paser。在介绍这个函数的功能的时候,我们说这个函数会尝试的读一些数据进来,并解析这些数据,从这些数据中进一步获得详细的流的信息。到目前为止我们并没有读任何数据进来,那么接下来,想必就是读数据进来并解码分析数据流了吧。

read_size = 0;
for (;;) {
int analyzed_all_streams;
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;

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 */
if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
int count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
st->info->codec_info_duration_fields/2 :
st->info->duration_count;
if (count < fps_analyze_framecount)
break;
}
if (st->parser && st->parser->parser->split &&
!st->internal->avctx->extradata)
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 (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 = add_to_pktbuf(&ic->internal->packet_buffer, pkt,
&ic->internal->packet_buffer_end, 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     - 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 FF_API_R_FRAME_RATE
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
ff_rfps_add_frame(ic, st, pkt->dts);
#endif
if (st->parser && st->parser->parser->split && !avctx->extradata) {
int i = st->parser->parser->split(avctx, pkt->data, pkt->size);
if (i > 0 && i < FF_MAX_EXTRADATA_SIZE) {
avctx->extradata_size = i;
avctx->extradata      = av_mallocz(avctx->extradata_size +
AV_INPUT_BUFFER_PADDING_SIZE);
if (!avctx->extradata)
return AVERROR(ENOMEM);
memcpy(avctx->extradata, pkt->data,
avctx->extradata_size);
}
}

/* 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++;
}


这个死循环做了如下事情:

1.检查用户有没有请求中断。如果有中断请求,就调用中断回调方法,并且函数返回。

2.再一次遍历流,检查是不是还有编解码器需要进一步处理。这里会检查编解码器的各个参数,如果这些参数都就绪,那么就会返回了,也就没有必要再做进一步分析了。如果还有些编解码器的信息不全,那么这里会继续向下执行。

3.读一帧数据进来。使用read_frame_internal函数。这个函数很复杂,以后在分析。

4.把读出来的数据添加到缓冲区。使用的是add_to_pktbuf函数。然后更新读到的总的数据的大小:read_size += pkt->size;

5.再次更新编解码器上下文环境的参数。调用的是avcodec_parameters_to_context函数,这个函数我们已经分析过了。

6.检查dts的连续性。 如果dts中的差异大于序列中平均分组持续时间的1000倍,我们将其视为不连续。

代码为:

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     - 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;
}


7.更新存储的dts的值。代码为:

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;
}


8.调用解析器的aplit方法。加入我们的解析器是h.264,那么这个解析器会像下面这样:

AVCodecParser ff_h264_parser = {
.codec_ids      = { AV_CODEC_ID_H264 },
.priv_data_size = sizeof(H264ParseContext),
.parser_init    = init,
.parser_parse   = h264_parse,
.parser_close   = h264_close,
.split          = h264_split,
};


这个split方法暂时看不懂,以后再来解析。

9.如果这个时候,还是没有找到足够的信息,那么就会尝试解压一些数据出来并做分析。

首先看注释:

如果仍然没有信息,我们尝试打开编解码器并且解压缩帧。 我们需要在大多数情况下尽量避免做这样的事情,因为很需要很多的时间,并使用更多的内存。 对于MPEG-4,我们需要解压QuickTime。

如果设置AV_CODEC_CAP_CHANNEL_CONF,这将强制解码至少一帧编解码器数据,这确保编解码器初始化通道配置,并且不仅信任来自容器的值

尝试解压一帧的数据使用的是try_decode_frame函数。有些参数,如vidoe的pix_fmt是需要调用h264_decode_frame才可以获取其pix_fmt的。

总结下这个死循环做的工作:其中会检测是不是所有流的信息都已经完备,如果完备就返回,如果完备,就尝试的解压一帧的数据。并从中获取pix_fmt等必须通过解压一帧的数据才能获得的参数。

part 6 检查是否到达文件尾部

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) {
if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : NULL) < 0)
av_log(ic, AV_LOG_WARNING,
"Failed to open codec in av_find_stream_info\n");
}
}

// 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);
}
}
}


如果到了文件尾部,又会遍历一次流,检测其编解码器参数,如果其参数不完整,就会再次调用avcodec_open2来初始化编解码器的各个参数。

part 7 刷新解码器

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);
}
}
}
}


有一些帧可能在缓存区中,需要把它们flush掉。

part 8 关闭可能可打开的编解码器

for (i = 0; i < ic->nb_streams; i++) {
st = ic->streams[i];
avcodec_close(st->internal->avctx);
}


再次遍历流,逐个调用avcodec_close方法来关闭流。

part 9 计算rfps

ff_rfps_calculate(ic);

part10 再次遍历流

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;

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 (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) {
st->r_frame_rate.num = avctx->time_base.den;
st->r_frame_rate.den = avctx->time_base.num * avctx->ticks_per_frame;
} 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;
}
}
}


这个循环用来处理音频流和视频流。

对视频流而言,首先会获得原始数据的图像格式,然后据此找到对应的编解码器的tag。之后会计算平均帧率。

对音频流而言,基于音频流的服务类型初始化disposition。disposition的作用暂时不知。

part 11 计算时间相关的参数

if (probesize)
estimate_timings(ic, old_offset);


estimate_timings函数如下:

static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
{
int64_t file_size;

/* get the file size, if possible */
if (ic->iformat->flags & AVFMT_NOFILE) {
file_size = 0;
} else {
file_size = avio_size(ic->pb);
file_size = FFMAX(0, file_size);
}

if ((!strcmp(ic->iformat->name, "mpeg") ||
!strcmp(ic->iformat->name, "mpegts")) &&
file_size && ic->pb->seekable) {
/* get accurate estimate from the PTSes */
estimate_timings_from_pts(ic, old_offset);
ic->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
} else if (has_duration(ic)) {
/* at least one component has timings - we use them for all
* the components */
fill_all_stream_timings(ic);
ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
} else {
/* less precise: use bitrate info */
estimate_timings_from_bit_rate(ic);
ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
}
update_stream_timings(ic);

{
int i;
AVStream av_unused *st;
for (i = 0; i < ic->nb_streams; i++) {
st = ic->streams[i];
av_log(ic, AV_LOG_TRACE, "stream %d: start_time: %0.3f duration: %0.3f\n", i,
(double) st->start_time * av_q2d(st->time_base),
(double) st->duration   * av_q2d(st->time_base));
}
av_log(ic, AV_LOG_TRACE,
"format: start_time: %0.3f duration: %0.3f bitrate=%"PRId64" kb/s\n",
(double) ic->start_time / AV_TIME_BASE,
(double) ic->duration   / AV_TIME_BASE,
(int64_t)ic->bit_rate / 1000);
}
}


主要是调用update_stream_timings函数更新流的时间相关的参数,之后的代码块是打印日志。

update_stream_timings函数定义在libavfomat/utils.c中。

/**
* Estimate the stream timings from the one of each components.
*
* Also computes the global bitrate if possible.
*/
static void update_stream_timings(AVFormatContext *ic)
{
int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
int64_t duration, duration1, filesize;
int i;
AVStream *st;
AVProgram *p;

start_time = INT64_MAX;
start_time_text = INT64_MAX;
end_time   = INT64_MIN;
end_time_text   = INT64_MIN;
duration   = INT64_MIN;
for (i = 0; i < ic->nb_streams; i++) {
st = ic->streams[i];
if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
start_time1 = av_rescale_q(st->start_time, st->time_base,
AV_TIME_BASE_Q);
if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
if (start_time1 < start_time_text)
start_time_text = start_time1;
} else
start_time = FFMIN(start_time, start_time1);
end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
AV_TIME_BASE_Q,
AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
end_time1 += start_time1;
if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
end_time_text = FFMAX(end_time_text, end_time1);
else
end_time = FFMAX(end_time, end_time1);
}
for (p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
p->start_time = start_time1;
if (p->end_time < end_time1)
p->end_time = end_time1;
}
}
if (st->duration != AV_NOPTS_VALUE) {
duration1 = av_rescale_q(st->duration, st->time_base,
AV_TIME_BASE_Q);
duration  = FFMAX(duration, duration1);
}
}
if (start_time == INT64_MAX || (start_time > start_time_text && start_time - start_time_text < AV_TIME_BASE))
start_time = start_time_text;
else if (start_time > start_time_text)
av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);

if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - end_time < AV_TIME_BASE)) {
end_time = end_time_text;
} else if (end_time < end_time_text) {
av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
}

if (start_time != INT64_MAX) {
ic->start_time = start_time;
if (end_time != INT64_MIN) {
if (ic->nb_programs > 1) {
for (i = 0; i < ic->nb_programs; i++) {
p = ic->programs[i];
if (p->start_time != AV_NOPTS_VALUE && p->end_time > p->start_time)
duration = FFMAX(duration, p->end_time - p->start_time);
}
} else
duration = FFMAX(duration, end_time - start_time);
}
}
if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
ic->duration = duration;
}
if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
/* compute the bitrate */
double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
(double) ic->duration;
if (bitrate >= 0 && bitrate <= INT64_MAX)
ic->bit_rate = bitrate;
}
}


时间相关的计算非常复杂,是在看不懂。这里还计算了波特率: bitrate = (double) filesize * 8.0 * AV_TIME_BASE / (double) ic->duration;

这里很好理解。filesize*8是这个文件的所有bit数。一字节=8bit。

然后比特率=bit数/时间。

这里时间为ic->duration/AV_TIME_BASE 。

part 12 更新数据

/* 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;
// The decoder might reduce the video size by the lowres factor.
if (av_codec_get_lowres(st->internal->avctx) && orig_w) {
st->codecpar->width = orig_w;
st->codecpar->height = orig_h;
}
}

#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;

// The old API (AVStream.codec) "requires" the resolution to be adjusted
// by the lowres factor.
if (av_codec_get_lowres(st->internal->avctx) && st->internal->avctx->width) {
av_codec_set_lowres(st->codec, av_codec_get_lowres(st->internal->avctx));
st->codec->width = st->internal->avctx->width;
st->codec->height = st->internal->avctx->height;
}

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;


最后就是更新各个结构的数据了。AVStream中的AVCodecContext结构体,以及AVStream中AVStreamInternal中的AVCodecContext结构体的数据要统一起来。AVStream中的AVCodecParameters和AVStream中AVStreamInternal中的AVCodecContext结构体的数据也要统一起来。以上主要就是跟新这几个结构体中的数据。

四、总结

最后总结一下吧:

avformat_open_input函数会读文件头,对mp4文件而言,它会解析所有的box。但它知识把读到的结果保存在对应的数据结构下。这个时候,AVStream中的很多字段都是空白的。

avformat_find_stream_info则检测这些重要字段,如果是空白的,就设法填充它们。因为我们解析文件头的时候,已经掌握了大量的信息,avformat_find_stream_info就是通过这些信息来填充自己的成员,当重要的成员都填充完毕后,该函数就返回了。这中情况下,该函数效率很高。但对于某些文件,单纯的从文件头中获取信息是不够的,比如vidoe的pix_fmt是需要调用h264_decode_frame才可以获取其pix_fmt的。因此,这个时候这个函数就会读一些数据进来,然后分析这些数据,并尝试解码这些数据,最终从中提取到所需要的信息。在所有的信息都已经获取到以后,avformat_find_stream_info函数会计算start_time,波特率等信息,其中时间相关的计算很复杂,没能看懂,以后再研究吧。计算结束后会更新相关结构体的数据成员。

虽然由于能力限制,没能够彻底搞懂这个函数的方方面面,但是,通过这次分析,更好的理解了这个函数的功能,对这个函数做的事情有了一定的了解。这也是这次分析的主要收获了,至于那些搞不懂的,只能留着以后慢慢研究了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息