ffmpeg学习之路(一)Demux过程(解复用)1

上一篇文章说了一下媒体文件解码的流程,在该基础上再对其中的第一个过程Demux在ffmpeg中是如何实现的进行说明。

ffmpeg中用来进行Demux的关键函数是avformat_open_input,这里先贴一下源码中的定义:

头文件:avformat.h

/**
 * Open an input stream and read the header. The codecs are not opened.
 * The stream must be closed with av_close_input_file().
 *
 * @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 filename Name 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 *filename, AVInputFormat *fmt, AVDictionary **options);

翻译成中文,大致是这样:

参数ps:用户提供的AVFormatContext类型的指针,该指针可以为NULL,如果为NULL,则在函数中会自动分配内存。

            也可以在调用函数前,使用avformat_alloc_context分配内存。AVFormatContext包含了一切媒体相关的上下文

            结构,有它就有了一切。

            该指针所指向的内存,如果函数执行成功,会填充结构中的部分字段,如果执行失败,会直接被释放。

参数filename:需要解码的媒体文件名称或者url

参数fmt:调用该函数前用户指定的媒体格式,对应命令行中的-f XXX字段。可以为NULL。

             如果为NULL,则在函数中会探测文件的实际格式。如果指定,那么直接用指定的格式,不另外进行探测。

参数options:命令行中的一些其他参数,可以为NULL。因为不影响理解Demux流程,所以这里不做进一步的研究。


练习代码:

int main(int argc, char **argv)
{
	if(argc<3)
	{
		printf("请输入需要解析的媒体文件\n");
		return -1;
	}

    av_register_all();
	AVFormatContext *pAVFormatContext = NULL;
	int ret = avformat_open_input(&pAVFormatContext,argv[2],NULL,NULL);
	if(ret == 0)
		av_dump_format(pAVFormatContext,0,argv[2],0); //dump input information to the stdio

	avformat_close_input(&pAVFormatContext); 
    return 0;
}

这里面有一个重要的数据结构AVFormatContext,稍后做说明。

你可能感兴趣的:(音视频)