与AVFormatContext结构体相关的接口函数声明位于libavformat\avformat.h中
- /**
- * Allocate an AVFormatContext.
- * avformat_free_context() can be used to free the context and everything
- * allocated by the framework within it.
- */
- AVFormatContext *avformat_alloc_context(void);
初始化结构体,为AVFormatContext结构体分配内存,并设置AVFormatContext的字段的默认值。
- /**
- * Free an AVFormatContext and all its streams.
- * @param s context to free
- */
- void avformat_free_context(AVFormatContext *s);
销毁AVFormatContext, 释放内存。
-
- /**
- * Open an input stream and read the header. The codecs are not opened.
- * The stream must be closed with avformat_close_input().
- *
- * @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context).
- * May be a pointer to NULL, in which case an AVFormatContext is allocated by this
- * function and written into ps.
- * Note that a user-supplied AVFormatContext will be freed on failure.
- * @param url URL of the stream to open.
- * @param fmt If non-NULL, this parameter forces a specific input format.
- * Otherwise the format is autodetected.
- * @param options A dictionary filled with AVFormatContext and demuxer-private options.
- * On return this parameter will be destroyed and replaced with a dict containing
- * options that were not found. May be NULL.
- *
- * @return 0 on success, a negative AVERROR on failure.
- *
- * @note If you want to use custom IO, preallocate the format context and set its pb field.
- */
- int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options);
该接口注释已经写的很详细了,中文简单概述下
ps:函数调用成功之后处理过的AVFormatContext结构体。
url:打开的视音频流的URL。
fmt:强制指定AVFormatContext中AVInputFormat的。这个参数一般情况下可以设置为NULL,这样FFmpeg可以自动检测AVInputFormat。
dictionay:附加的一些选项,一般情况下可以设置为NULL。
该接口的功能是打开一个输入媒体文件,读取文件头(如果有的话,MPEG没有format header)并探测视频封装格式,将媒体文件信息存储到ps中。
需要注意的是,有些封装格式并没有header 或者header中并没有存储足够的信息,所以还需要调用avformat_find_stream_info函数来读取并解码一些帧来获取足够的信息。
avformat_open_input 不会打开codec.
具体代码分析可以参见雷神的这篇文章,写的很详细了,不再赘述:FFmpeg源代码简单分析:avformat_open_input()
- /**
- * 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);
该函数通过读取一部分音视频数据来获取流信息,同时能够获取真实的帧率。该函数主要用于给每个媒体流(音频/视频)的AVStream结构体赋值,获取真实的流信息。