ffmpeg——avformat_alloc_output_context2
2016-10-21 13:41
423 查看
一:介绍
avformat_alloc_output_context2 负责分配输出 AVFormatContext。ffmpeg有各种各样的 Context ,其功能是管理各种各样的模块。例如有一个输出文件:test.mp4,使用 avformat_alloc_output_context2 函数就可以根据文件名分配合适的 AVFormatContext 管理结构。可额外参考:http://blog.csdn.net/leixiaohua1020/article/details/41198929
二:分析
结构图
假定设定输出文件名:test.mp4,则使用
avformat_alloc_output_context2 就可以分配合适的AVFormatContext 结构。
avformat_alloc_output_context2 在 libavformat/mux.c 中定义:
int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *oformat, const char *format, const char *filename) { //分配AVFormatContext 结构,并设置默认的AVClass接口 AVFormatContext *s = avformat_alloc_context(); int ret = 0; *avctx = NULL; if (!s) goto nomem; //(根据文件名)找到合适的AVOutputFormat 结构 if (!oformat) { if (format) { oformat = av_guess_format(format, NULL, NULL); if (!oformat) { av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not a suitable output format\n", format); ret = AVERROR(EINVAL); goto error; } } else { oformat = av_guess_format(NULL, filename, NULL); if (!oformat) { ret = AVERROR(EINVAL); av_log(s, AV_LOG_ERROR, "Unable to find a suitable output format for '%s'\n", filename); goto error; } } } //拷贝保存AVOutputFormat 的私有AVClass后续使用 s->oformat = oformat; if (s->oformat->priv_data_size > 0) { s->priv_data = av_mallocz(s->oformat->priv_data_size); if (!s->priv_data) goto nomem; if (s->oformat->priv_class) { *(const AVClass**)s->priv_data= s->oformat->priv_class; av_opt_set_defaults(s->priv_data); } } else s->priv_data = NULL; //记录输出文件名 if (filename) av_strlcpy(s->filename, filename, sizeof(s->filename)); *avctx = s; return 0; nomem: av_log(s, AV_LOG_ERROR, "Out of memory\n"); ret = AVERROR(ENOMEM); error: avformat_free_context(s); return ret; }
avformat_alloc_context :(实现AVClass接口)
AVFormatContext *avformat_alloc_context(void) { AVFormatContext *ic; ic = av_malloc(sizeof(AVFormatContext)); if (!ic) return ic; avformat_get_context_defaults(ic); ic->internal = av_mallocz(sizeof(*ic->internal)); if (!ic->internal) { avformat_free_context(ic); return NULL; } ic->internal->offset = AV_NOPTS_VALUE; ic->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE; return ic; }av_guess_format:(在AVOutputFormat链表中寻找合适的项)
AVOutputFormat *av_guess_format(const char *short_name, const char *filename, const char *mime_type) { AVOutputFormat *fmt = NULL, *fmt_found; int score_max, score; /* specific test for image sequences */ #if CONFIG_IMAGE2_MUXER if (!short_name && filename && av_filename_number_test(filename) && ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) { return av_guess_format("image2", NULL, NULL); } #endif /* Find the proper file type. */ fmt_found = NULL; score_max = 0; while ((fmt = av_oformat_next(fmt))) { score = 0; if (fmt->name && short_name && av_match_name(short_name, fmt->name)) score += 100; if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type)) score += 10; if (filename && fmt->extensions && av_match_ext(filename, fmt->extensions)) { score += 5; } if (score > score_max) { score_max = score; fmt_found = fmt; } } return fmt_found; }之后将输出文件名记录,打开文件时 AVIOContext *pb;记录文件操作句柄。
相关文章推荐
- ffmpeg源码简析(五)编码——avformat_alloc_output_context2(),avcodec_encode_video2()
- 解决 ffmpeg 在 avformat_alloc_output_context2 或者 av_guess_format 获取失败的问题
- ffmpeg 3.2 avformat_alloc_context()说明
- FFmpeg源代码简单分析:avformat_alloc_output_context2()
- FFmpeg源代码简单分析:avformat_alloc_output_context2()
- FFmpeg源代码简单分析:avformat_alloc_output_context2()
- ffmpeg源码分析--3.avformat_alloc_context与avformat_open_input
- avformat_alloc_output_context2函数解析
- avformat_alloc_output_context2()
- ffmpeg学习:avformat_alloc_context
- ffmpeg源码分析之avformat_alloc_context
- AVCodecContext,AVStream,AVFormatContext----FFMPEG-数据结构解释
- FFmpeg源代码简单分析:常见结构体的初始化和销毁(AVFormatContext,AVFrame等)
- FFMPEG结构体分析:AVFormatContext
- FFMPEG结构体分析一:AVFormatContext
- ffmpeg框架阅读笔记一:读取数据帧函数 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
- FFMpeg分析3:AVFormatContext和AVIContext、FLVContext等XXXContext
- FFMPEG结构体分析:AVFormatContext
- FFMPEG-数据结构解释(AVCodecContext,AVStream,AVFormatContext)
- FFmpeg源代码简单分析:常见结构体的初始化和销毁(AVFormatContext,AVFrame等)