ffmpeg 的 Mux 主要分为 三步操作:
avformat_write_header : 写文件头
av_write_frame/av_interleaved_write_frame: 写packet
av_write_trailer : 写文件尾
本文主要分析 avformat_write_header 。
可额外参考 : http://blog.csdn.net/leixiaohua1020/article/details/44116215
avformat_write_header:
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
{
int ret = 0;
//初始化Mux,一些参数检查和设置
if ((ret = init_muxer(s, options)) < 0)
return ret;
// Write_header
if (!s->oformat->check_bitstream) {
ret = write_header_internal(s);
if (ret < 0)
goto fail;
}
//初始化pts
if ((ret = init_pts(s)) < 0)
goto fail;
if (s->avoid_negative_ts < 0) {
av_assert2(s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO);
if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
s->avoid_negative_ts = 0;
} else
s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE;
}
return 0;
fail:
if (s->oformat->deinit)
s->oformat->deinit(s);
return ret;
}
init_muxer (初始化Mux,参数检查与设置):
static int init_muxer(AVFormatContext *s, AVDictionary **options)
{
int ret = 0, i;
AVStream *st;
AVDictionary *tmp = NULL;
AVCodecParameters *par = NULL;
AVOutputFormat *of = s->oformat;
const AVCodecDescriptor *desc;
AVDictionaryEntry *e;
//通过AVClass接口设置AVFormatContext 的Options参数(一般options为NULL,所以可以忽略)
if (options)
av_dict_copy(&tmp, *options, 0);
if ((ret = av_opt_set_dict(s, &tmp)) < 0)
goto fail;
//通过AVClass接口设置AVFormatContext->priv_data的Options参数。
//(还记得么? priv_data保存的是AVOutputFormat 的预存AVClass接口。例如MOV封装预存的是个MOVMuxContext )
if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
(ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
goto fail;
//新版ffmpeg3.1.4 废除了codec(忽略此部分)
#if FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
if (s->nb_streams && s->streams[0]->codec->flags & AV_CODEC_FLAG_BITEXACT) {
if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
#if FF_API_LAVF_BITEXACT
av_log(s, AV_LOG_WARNING,
"Setting the AVFormatContext to bitexact mode, because "
"the AVCodecContext is in that mode. This behavior will "
"change in the future. To keep the current behavior, set "
"AVFormatContext.flags |= AVFMT_FLAG_BITEXACT.\n");
s->flags |= AVFMT_FLAG_BITEXACT;
#else
av_log(s, AV_LOG_WARNING,
"The AVFormatContext is not in set to bitexact mode, only "
"the AVCodecContext. If this is not intended, set "
"AVFormatContext.flags |= AVFMT_FLAG_BITEXACT.\n");
#endif
}
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif
// some sanity checks
if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
av_log(s, AV_LOG_ERROR, "No streams to mux were specified\n");
ret = AVERROR(EINVAL);
goto fail;
}
//循环处理每个流通道Stream,完成对codecpar ,time_base 的参数检查
for (i = 0; i < s->nb_streams; i++) {
st = s->streams[i];
par = st->codecpar;
//ffmpeg3.1.4 codec 已废除
#if FF_API_LAVF_CODEC_TB && FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
if (!st->time_base.num && st->codec->time_base.num) {
av_log(s, AV_LOG_WARNING, "Using AVStream.codec.time_base as a "
"timebase hint to the muxer is deprecated. Set "
"AVStream.time_base instead.\n");
avpriv_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif
//ffmpeg3.1.4 codec 已废除
#if FF_API_LAVF_AVCTX
FF_DISABLE_DEPRECATION_WARNINGS
if (st->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN &&
st->codec->codec_type != AVMEDIA_TYPE_UNKNOWN) {
av_log(s, AV_LOG_WARNING, "Using AVStream.codec to pass codec "
"parameters to muxers is deprecated, use AVStream.codecpar "
"instead.\n");
ret = avcodec_parameters_from_context(st->codecpar, st->codec);
if (ret < 0)
goto fail;
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif
/* update internal context from codecpar, old bsf api needs this
* FIXME: remove when autobsf uses new bsf API */
//内部使用,无需关注
ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
if (ret < 0)
goto fail;
//检查AVStream->time_base 是否被设置,如果没有:
// 音频设置为: 1/sample_rate ; 视频设置为:1/90000
if (!st->time_base.num) {
/* fall back on the default timebase values */
if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->sample_rate)
avpriv_set_pts_info(st, 64, 1, par->sample_rate);
else
avpriv_set_pts_info(st, 33, 1, 90000);
}
//对音视频参数检查
switch (par->codec_type) {
case AVMEDIA_TYPE_AUDIO:
if (par->sample_rate <= 0) {
av_log(s, AV_LOG_ERROR, "sample rate not set\n");
ret = AVERROR(EINVAL);
goto fail;
}
if (!par->block_align)
par->block_align = par->channels *
av_get_bits_per_sample(par->codec_id) >> 3;
break;
case AVMEDIA_TYPE_VIDEO:
if ((par->width <= 0 || par->height <= 0) &&
!(of->flags & AVFMT_NODIMENSIONS)) {
av_log(s, AV_LOG_ERROR, "dimensions not set\n");
ret = AVERROR(EINVAL);
goto fail;
}
if (av_cmp_q(st->sample_aspect_ratio, par->sample_aspect_ratio)
&& fabs(av_q2d(st->sample_aspect_ratio) - av_q2d(par->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
) {
if (st->sample_aspect_ratio.num != 0 &&
st->sample_aspect_ratio.den != 0 &&
par->sample_aspect_ratio.num != 0 &&
par->sample_aspect_ratio.den != 0) {
av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
"(%d/%d) and encoder layer (%d/%d)\n",
st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
par->sample_aspect_ratio.num,
par->sample_aspect_ratio.den);
ret = AVERROR(EINVAL);
goto fail;
}
}
break;
}
//检查输出封装(AVOutputFormat)是否支持设定的流编码格式。(在AVOutputFormat支持的编码列表中寻找匹配)
desc = avcodec_descriptor_get(par->codec_id);
if (desc && desc->props & AV_CODEC_PROP_REORDER)
st->internal->reorder = 1;
if (of->codec_tag) {
if ( par->codec_tag
&& par->codec_id == AV_CODEC_ID_RAWVIDEO
&& ( av_codec_get_tag(of->codec_tag, par->codec_id) == 0
|| av_codec_get_tag(of->codec_tag, par->codec_id) == MKTAG('r', 'a', 'w', ' '))
&& !validate_codec_tag(s, st)) {
// the current rawvideo encoding system ends up setting
// the wrong codec_tag for avi/mov, we override it here
par->codec_tag = 0;
}
if (par->codec_tag) {
if (!validate_codec_tag(s, st)) {
char tagbuf[32], tagbuf2[32];
av_get_codec_tag_string(tagbuf, sizeof(tagbuf), par->codec_tag);
av_get_codec_tag_string(tagbuf2, sizeof(tagbuf2), av_codec_get_tag(s->oformat->codec_tag, par->codec_id));
av_log(s, AV_LOG_ERROR,
"Tag %s/0x%08x incompatible with output codec id '%d' (%s)\n",
tagbuf, par->codec_tag, par->codec_id, tagbuf2);
ret = AVERROR_INVALIDDATA;
goto fail;
}
} else
par->codec_tag = av_codec_get_tag(of->codec_tag, par->codec_id);
}
if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT)
s->internal->nb_interleaved_streams++;
}
//此处实际上在前面已经做过
if (!s->priv_data && of->priv_data_size > 0) {
s->priv_data = av_mallocz(of->priv_data_size);
if (!s->priv_data) {
ret = AVERROR(ENOMEM);
goto fail;
}
if (of->priv_class) {
*(const AVClass **)s->priv_data = of->priv_class;
av_opt_set_defaults(s->priv_data);
if ((ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
goto fail;
}
}
//metadata 的设置(根据需要设置)
/* set muxer identification string */
if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
} else {
av_dict_set(&s->metadata, "encoder", NULL, 0);
}
for (e = NULL; e = av_dict_get(s->metadata, "encoder-", e, AV_DICT_IGNORE_SUFFIX); ) {
av_dict_set(&s->metadata, e->key, NULL, 0);
}
if (options) {
av_dict_free(options);
*options = tmp;
}
if (s->oformat->init && (ret = s->oformat->init(s)) < 0) {
if (s->oformat->deinit)
s->oformat->deinit(s);
goto fail;
}
return 0;
fail:
av_dict_free(&tmp);
return ret;
}
write_header_internal (写文件头):
static int write_header_internal(AVFormatContext *s)
{
if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_HEADER);
if (s->oformat->write_header) {
int ret = s->oformat->write_header(s);
if (ret >= 0 && s->pb && s->pb->error < 0)
ret = s->pb->error;
s->internal->write_header_ret = ret;
if (ret < 0)
return ret;
if (s->flush_packets && s->pb && s->pb->error >= 0 && s->flags & AVFMT_FLAG_FLUSH_PACKETS)
avio_flush(s->pb);
}
s->internal->header_written = 1;
if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_UNKNOWN);
return 0;
}
可见 writer_header_internal 直接调用匹配的
AVOutputFormat 的 write_header方法。因此,具体的 write_header 方法由各种封装自己定义并实现。(例如MOV封装:
avformat_write_header——>
write_header_internal——>AVOutputFormat(MOV)——>mov_write_header)。
priv_pts (设置AVStream->priv_pts):
static int init_pts(AVFormatContext *s)
{
int i;
AVStream *st;
/* init PTS generation */
for (i = 0; i < s->nb_streams; i++) {
int64_t den = AV_NOPTS_VALUE;
st = s->streams[i];
switch (st->codecpar->codec_type) {
case AVMEDIA_TYPE_AUDIO:
den = (int64_t)st->time_base.num * st->codecpar->sample_rate;
break;
case AVMEDIA_TYPE_VIDEO:
den = (int64_t)st->time_base.num * st->time_base.den;
break;
default:
break;
}
if (!st->priv_pts)
st->priv_pts = av_mallocz(sizeof(*st->priv_pts));
if (!st->priv_pts)
return AVERROR(ENOMEM);
if (den != AV_NOPTS_VALUE) {
if (den <= 0)
return AVERROR_INVALIDDATA;
frac_init(st->priv_pts, 0, 0, den);
}
}
return 0;
}
此处设置的是priv_pts(将后续使用)。
注:关于time的设置很重要,有AVStream->time_base , AVPacket->pts 等,这里也需要留意一下。