从头用脚分析FFmpeg源码 - avformat_write_header
Posted Adonis_sz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从头用脚分析FFmpeg源码 - avformat_write_header相关的知识,希望对你有一定的参考价值。
avformat_write_header 作用
先看FFmpeg怎么说。
/**
* Allocate the stream private data and write the stream header to
* an output media file.
*
* @param s Media file handle, must be allocated with avformat_alloc_context().
* Its oformat field must be set to the desired output format;
* Its pb field must be set to an already opened AVIOContext.
* @param options An AVDictionary filled with AVFormatContext and muxer-private options.
* On return this parameter will be destroyed and replaced with a dict containing
* options that were not found. May be NULL.
*
* @return AVSTREAM_INIT_IN_WRITE_HEADER on success if the codec had not already been fully initialized in avformat_init,
* AVSTREAM_INIT_IN_INIT_OUTPUT on success if the codec had already been fully initialized in avformat_init,
* negative AVERROR on failure.
*
* @see av_opt_find, av_dict_set, avio_open, av_oformat_next, avformat_init_output.
*/
av_warn_unused_result
int avformat_write_header(AVFormatContext *s, AVDictionary **options);
注释说申请流的私有数据然后把流信息写到媒体文件里面。
看源码之后感觉申请数据没发现什么,这个函数最重要的就干了两个事情,第一个是调用avformat_init_output,初始化mutex,实际上大部分都是检查格式,然后写入options。第二个就是调用对应格式的write_header,完成写入packet之前的一些格式上的初始化操作。
avformat_write_header源码
下面详细介绍一下代码。
里面的重点就是avformat_init_output函数和write_header函数。
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
int ret = 0;
int already_initialized = s->internal->initialized;
int streams_already_initialized = s->internal->streams_initialized;
if (!already_initialized)
// 这里初始化,一般来说,正常路径调用avformat_write_header,都会调用到这里
if ((ret = avformat_init_output(s, options)) < 0)
return ret;
// 还未打开输出文件流
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)
// 调用write_header函数,这个也是这里面最重要的函数。
// 比如在写入mp4的时候,会根据stream的extradata初始化一些参数。
ret = s->oformat->write_header(s);
if (ret >= 0 && s->pb && s->pb->error < 0)
ret = s->pb->error;
if (ret < 0)
goto fail;
flush_if_needed(s);
if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_UNKNOWN);
if (!s->internal->streams_initialized)
if ((ret = init_pts(s)) < 0)
goto fail;
return streams_already_initialized;
fail:
deinit_muxer(s);
return ret;
avformat_init_output源码
其实就是init_muxer的一个封装,avformat_init_output在回输出多个文件的格式中会调用多次,比如m3u8格式。
int avformat_init_output(AVFormatContext *s, AVDictionary **options)
int ret = 0;
if ((ret = init_muxer(s, options)) < 0)
return ret;
s->internal->initialized = 1;
s->internal->streams_initialized = ret;
if (s->oformat->init && ret)
if ((ret = init_pts(s)) < 0)
return ret;
return AVSTREAM_INIT_IN_INIT_OUTPUT;
return AVSTREAM_INIT_IN_WRITE_HEADER;
init_muxer源码
这个函数就三个作用,第一个作用是把options设置到AVFormatContext中,第二个作用就是遍历所有的AVStream,然后检测AVStream中的参数是否正确。 第三个就是看当前的AVOutputFormat是否实现了init方法,然后调用init
static int init_muxer(AVFormatContext *s, AVDictionary **options)
int ret = 0, i;
AVStream *st;
AVDictionary *tmp = NULL;
AVCodecParameters *par = NULL;
const AVOutputFormat *of = s->oformat;
const AVCodecDescriptor *desc;
AVDictionaryEntry *e;
if (options)
av_dict_copy(&tmp, *options, 0);
// 设置options到AVFormatContext中
if ((ret = av_opt_set_dict(s, &tmp)) < 0)
goto fail;
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;
#if FF_API_FORMAT_FILENAME
FF_DISABLE_DEPRECATION_WARNINGS
if (!s->url && !(s->url = av_strdup(s->filename)))
FF_ENABLE_DEPRECATION_WARNINGS
#else
if (!s->url && !(s->url = av_strdup("")))
#endif
ret = AVERROR(ENOMEM);
goto fail;
#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))
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");
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;
// 遍历所有的AVStream,然后检测格式
for (i = 0; i < s->nb_streams; i++)
st = s->streams[i];
par = st->codecpar;
#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
//如果没有设置时间戳,就用avpriv_set_pts_info设置时间戳
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;
desc = avcodec_descriptor_get(par->codec_id);
if (desc && desc->props & AV_CODEC_PROP_REORDER)
st->internal->reorder = 1;
st->internal->is_intra_only = ff_is_intra_only(par->codec_id);
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))
const uint32_t otag = av_codec_get_tag(s->oformat->codec_tag, par->codec_id);
av_log(s, AV_LOG_ERROR,
"Tag %s incompatible with output codec id '%d' (%s)\\n",
av_fourcc2str(par->codec_tag), par->codec_id, av_fourcc2str(otag));
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++;
// 检测是否为AVOuputFormat分配上下文空间
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;
/* 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;
// 这里调用init方法
// 在mp4中,根据context中设置的flag初始化一些数据,然后根据AVStream初始化对应的MOVTrack
if (s->oformat->init)
if ((ret = s->oformat->init(s)) < 0)
if (s->oformat->deinit)
s->oformat->deinit(s);
return ret;
return ret == 0;
return 0;
fail:
av_dict_free(&tmp);
return ret;
FFmpeg结构体学习:AVFormatContext分析
前言
统领全局的基本结构体,包含码流参数较多。它的源码位于libavformat/avformat.h
。
源码
/**
* Format I/O context. I/O格式上下文
* sizeof(AVFormatContext) must not be used outside libav*, use
* avformat_alloc_context() to create an AVFormatContext.
*
* Fields can be accessed through AVOptions (av_opt*)
*/
typedef struct AVFormatContext
/**
* A class for logging and @ref avoptions. Set by avformat_alloc_context().
* Exports (de)muxer private options if they exist.
* 一个用于日志记录和引用avoptions的类。由avformat_alloc_context()设置
* 如果(解)复用器私有options存在则导出
*/
const AVClass *av_class;
/**
* The input container format. 输入容器格式结构体
*
* Demuxing only, set by avformat_open_input(). 只用于解复用
*/
struct AVInputFormat *iformat;
/**
* The output container format. 输出容器格式结构体
*
* Muxing only, must be set by the caller before avformat_write_header().
* 只用于复用,必须在调用avformat_write_header()之前设置
*/
struct AVOutputFormat *oformat;
/**
* Format private data. This is an AVOptions-enabled struct
* if and only if iformat/oformat.priv_class is not NULL.
* 格式化私有数据。这是一个AVOptions-enabled结构体
*
* - muxing: set by avformat_write_header() 复用
* - demuxing: set by avformat_open_input() 解复用
*/
void *priv_data;
/**
* I/O context. 输入/输出上下文
*
* - demuxing: either set by the user before avformat_open_input() (then
* the user must close it manually) or set by avformat_open_input().
* 解复用:可以由用户自己设置(在avformat_open_intput()之前,而且必须手动关闭),也可以由avformat_open_input()设置.
* - muxing: set by the user before avformat_write_header(). The caller must
* take care of closing / freeing the IO context.
* 复用:在avformat_write_header()之前由用户设置,调用者必须注意关闭和释放这个上下文
*
* Do NOT set this field if AVFMT_NOFILE flag is set in
* iformat/oformat.flags. In such a case, the (de)muxer will handle
* I/O in some other way and this field will be NULL.
* 如果iformat/oformat设置了AVFMT_NOFILE flag则不要设置这个字段,因为(解)复用器会以其他方式操作io,这个字段将会是NULL
*
*/
AVIOContext *pb;
/* stream info 流信息相关字段*/
/**
* Flags signalling stream properties. A combination of AVFMTCTX_*.
* Set by libavformat.
* 信号流属性标志位。是AVFMTCTX_*的集合。由libavformat设置
*/
int ctx_flags;
/**
* Number of elements in AVFormatContext.streams.
* AVFormatContext.streams中元素的个数,有几路流
*
* Set by avformat_new_stream(), must not be modified by any other code.
* 由avformat_new_stream()设置,不能被其他代码更改
*/
unsigned int nb_streams;
/**
* A list of all streams in the file. New streams are created with
* avformat_new_stream().
* 文件中所有流的列表。新流由avformat_new_stream()创建
*
* - demuxing: streams are created by libavformat in avformat_open_input().
* If AVFMTCTX_NOHEADER is set in ctx_flags, then new streams may also
* appear in av_read_frame().
* 解复用:流是在avformat_open_input()方法里,由libavformat创建的。如果在ctx_flags里面设置了AVFMTCTX_NOHEADER,那么新的流也可能由av_read_frame()创建.
* - muxing: streams are created by the user before avformat_write_header().
* 复用:流由用户在avformat_write_header()之前创建
*
* Freed by libavformat in avformat_free_context().
* 用avformat_free_context()释放
*/
AVStream **streams;
#if FF_API_FORMAT_FILENAME
/**
* input or output filename 文件名,已经弃用,使用url代替
*
* - demuxing: set by avformat_open_input()
* - muxing: may be set by the caller before avformat_write_header()
*
* @deprecated Use url instead.
*/
attribute_deprecated
char filename[1024];
#endif
/**
* input or output URL. Unlike the old filename field, this field has no
* length restriction.
* 输入或者输出的URL,这个字段没有长度限制
*
* - demuxing: set by avformat_open_input(), initialized to an empty
* string if url parameter was NULL in avformat_open_input().
* 解复用:由avformat_open_input()设置,如果url参数为NULL,则初始化为一个空字符串
* - muxing: may be set by the caller before calling avformat_write_header()
* (or avformat_init_output() if that is called first) to a string
* which is freeable by av_free(). Set to an empty string if it
* was NULL in avformat_init_output().
* 复用:在调用avformat_write_header()之前由调用者设置一个可以被av_free()释放的字符串。如果avformat_init_output()中参数为NULL,则设置一个空字符串。
*
* Freed by libavformat in avformat_free_context().
* 由 libavformat 在 avformat_free_context() 中释放。
*/
char *url;
/**
* Position of the first frame of the component, in
* AV_TIME_BASE fractional seconds. NEVER set this value directly:
* It is deduced from the AVStream values.
* 组件中第一帧的位置,单位:微秒(AV_TIME_BASE)。不要直接设置这个值,它是由AVStream的值推导出来的
*
* Demuxing only, set by libavformat.
* 只用于解复用
*/
int64_t start_time;
/**
* Duration of the stream, in AV_TIME_BASE fractional
* seconds. Only set this value if you know none of the individual stream
* durations and also do not set any of them. This is deduced from the
* AVStream values if not set.
* 流的时长,单位:微秒。
*
* Demuxing only, set by libavformat.
* 只用于解复用
*/
int64_t duration;
/**
* Total stream bitrate in bit/s, 0 if not
* available. Never set it directly if the file_size and the
* duration are known as FFmpeg can compute it automatically.
* 全部流的码率,单位bit/s。如果不可用则为0。不要直接设置它,如果文件大小和时长可以被FFmpeg自动计算出来
*/
int64_t bit_rate;
unsigned int packet_size;
int max_delay;
/**
* Flags modifying the (de)muxer behaviour. A combination of AVFMT_FLAG_*.
* Set by the user before avformat_open_input() / avformat_write_header().
* 可以修改(解)复用器行为的标志位。是AVFMT_FLAT_*的组合。
* 在avformat_open_input() / avformat_write_header()之前由用户设置
*/
int flags;
#define AVFMT_FLAG_GENPTS 0x0001 ///< Generate missing pts even if it requires parsing future frames.
#define AVFMT_FLAG_IGNIDX 0x0002 ///< Ignore index.
#define AVFMT_FLAG_NONBLOCK 0x0004 ///< Do not block when reading packets from input.
#define AVFMT_FLAG_IGNDTS 0x0008 ///< Ignore DTS on frames that contain both DTS & PTS
#define AVFMT_FLAG_NOFILLIN 0x0010 ///< Do not infer any values from other values, just return what is stored in the container
#define AVFMT_FLAG_NOPARSE 0x0020 ///< Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fillin code works on frames and no parsing -> no frames. Also seeking to frames can not work if parsing to find frame boundaries has been disabled
#define AVFMT_FLAG_NOBUFFER 0x0040 ///< Do not buffer frames when possible
#define AVFMT_FLAG_CUSTOM_IO 0x0080 ///< The caller has supplied a custom AVIOContext, don't avio_close() it.
#define AVFMT_FLAG_DISCARD_CORRUPT 0x0100 ///< Discard frames marked corrupted
#define AVFMT_FLAG_FLUSH_PACKETS 0x0200 ///< Flush the AVIOContext every packet.
/**
* When muxing, try to avoid writing any random/volatile data to the output.
* This includes any random IDs, real-time timestamps/dates, muxer version, etc.
*
* This flag is mainly intended for testing.
*/
#define AVFMT_FLAG_BITEXACT 0x0400
#define AVFMT_FLAG_MP4A_LATM 0x8000 ///< Enable RTP MP4A-LATM payload
#define AVFMT_FLAG_SORT_DTS 0x10000 ///< try to interleave outputted packets by dts (using this flag can slow demuxing down)
#define AVFMT_FLAG_PRIV_OPT 0x20000 ///< Enable use of private options by delaying codec open (this could be made default once all code is converted)
#if FF_API_LAVF_KEEPSIDE_FLAG
#define AVFMT_FLAG_KEEP_SIDE_DATA 0x40000 ///< Don't merge side data but keep it separate. Deprecated, will be the default.
#endif
#define AVFMT_FLAG_FAST_SEEK 0x80000 ///< Enable fast, but inaccurate seeks for some formats
#define AVFMT_FLAG_SHORTEST 0x100000 ///< Stop muxing when the shortest stream stops.
#define AVFMT_FLAG_AUTO_BSF 0x200000 ///< Wait for packet data before writing a header, and add bitstream filters as requested by the muxer
/**
* Maximum size of the data read from input for determining
* the input container format.
* Demuxing only, set by the caller before avformat_open_input().
*/
int64_t probesize;
/**
* Maximum duration (in AV_TIME_BASE units) of the data read
* from input in avformat_find_stream_info().
* Demuxing only, set by the caller before avformat_find_stream_info().
* Can be set to 0 to let avformat choose using a heuristic.
*/
int64_t max_analyze_duration;
const uint8_t *key;
int keylen;
unsigned int nb_programs;
AVProgram **programs;
/**
* Forced video codec_id.
* Demuxing: Set by user.
*/
enum AVCodecID video_codec_id;
/**
* Forced audio codec_id.
* Demuxing: Set by user.
*/
enum AVCodecID audio_codec_id;
/**
* Forced subtitle codec_id.
* Demuxing: Set by user.
*/
enum AVCodecID subtitle_codec_id;
/**
* Maximum amount of memory in bytes to use for the index of each stream.
* If the index exceeds this size, entries will be discarded as
* needed to maintain a smaller size. This can lead to slower or less
* accurate seeking (depends on demuxer).
* Demuxers for which a full in-memory index is mandatory will ignore
* this.
* - muxing: unused
* - demuxing: set by user
*/
unsigned int max_index_size;
/**
* Maximum amount of memory in bytes to use for buffering frames
* obtained from realtime capture devices.
*/
unsigned int max_picture_buffer;
/**
* Number of chapters in AVChapter array.
* When muxing, chapters are normally written in the file header,
* so nb_chapters should normally be initialized before write_header
* is called. Some muxers (e.g. mov and mkv) can also write chapters
* in the trailer. To write chapters in the trailer, nb_chapters
* must be zero when write_header is called and non-zero when
* write_trailer is called.
* - muxing: set by user
* - demuxing: set by libavformat
*/
unsigned int nb_chapters;
AVChapter **chapters;
/**
* Metadata that applies to the whole file.
*
* - demuxing: set by libavformat in avformat_open_input()
* - muxing: may be set by the caller before avformat_write_header()
*
* Freed by libavformat in avformat_free_context().
*/
AVDictionary *metadata;
/**
* Start time of the stream in real world time, in microseconds
* since the Unix epoch (00:00 1st January 1970). That is, pts=0 in the
* stream was captured at this real world time.
* - muxing: Set by the caller before avformat_write_header(). If set to
* either 0 or AV_NOPTS_VALUE, then the current wall-time will
* be used.
* - demuxing: Set by libavformat. AV_NOPTS_VALUE if unknown. Note that
* the value may become known after some number of frames
* have been received.
*/
int64_t start_time_realtime;
/**
* The number of frames used for determining the framerate in
* avformat_find_stream_info().
* Demuxing only, set by the caller before avformat_find_stream_info().
*/
int fps_probe_size;
/**
* Error recognition; higher values will detect more errors but may
* misdetect some more or less valid parts as errors.
* Demuxing only, set by the caller before avformat_open_input().
*/
int error_recognition;
/**
* Custom interrupt callbacks for the I/O layer.
*
* demuxing: set by the user before avformat_open_input().
* muxing: set by the user before avformat_write_header()
* (mainly useful for AVFMT_NOFILE formats). The callback
* should also be passed to avio_open2() if it's used to
* open the file.
*/
AVIOInterruptCB interrupt_callback;
/**
* Flags to enable debugging.
*/
int debug;
#define FF_FDEBUG_TS 0x0001
/**
* Maximum buffering duration for interleaving.
*
* To ensure all the streams are interleaved correctly,
* av_interleaved_write_frame() will wait until it has at least one packet
* for each stream before actually writing any packets to the output file.
* When some streams are "sparse" (i.e. there are large gaps between
* successive packets), this can result in excessive buffering.
*
* This field specifies the maximum difference between the timestamps of the
* first and the last packet in the muxing queue, above which libavformat
* will output a packet regardless of whether it has queued a packet for all
* the streams.
*
* Muxing only, set by the caller before avformat_write_header().
*/
int64_t max_interleave_delta;
/**
* Allow non-standard and experimental extension
* @see AVCodecContext.strict_std_compliance
*/
int strict_std_compliance;
/**
* Flags for the user to detect events happening on the file. Flags must
* be cleared by the user once the event has been handled.
* A combination of AVFMT_EVENT_FLAG_*.
*/
int event_flags;
#define AVFMT_EVENT_FLAG_METADATA_UPDATED 0x0001 ///< The call resulted in updated metadata.
/**
* Maximum number of packets to read while waiting for the first timestamp.
* Decoding only.
*/
int max_ts_probe;
/**
* Avoid negative timestamps during muxing.
* Any value of the AVFMT_AVOID_NEG_TS_* constants.
* Note, this only works when using av_interleaved_write_frame. (interleave_packet_per_dts is in use)
* - muxing: Set by user
* - demuxing: unused
*/
int avoid_negative_ts;
#define AVFMT_AVOID_NEG_TS_AUTO -1 ///< Enabled when required by target format
#define AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE 1 ///< Shift timestamps so they are non negative
#define AVFMT_AVOID_NEG_TS_MAKE_ZERO 2 ///< Shift timestamps so that they start at 0
/**
* Transport stream id.
* This will be moved into demuxer private options. Thus no API/ABI compatibility
*/
int ts_id;
/**
* Audio preload in microseconds.
* Note, not all formats support this and unpredictable things may happen if it is used when not supported.
* - encoding: Set by user
* - decoding: unused
*/
int audio_preload;
/**
* Max chunk time in microseconds.
* Note, not all formats support this and unpredictable things may happen if it is used when not supported.
* - encoding: Set by user
* - decoding: unused
*/
int max_chunk_duration;
/**
* Max chunk size in bytes
* Note, not all formats support this and unpredictable things may happen if it is used when not supported.
* - encoding: Set by user
* - decoding: unused
*/
int max_chunk_size;
/**
* forces the use of wallclock timestamps as pts/dts of packets
* This has undefined results in the presence of B frames.
* - encoding: unused
* - decoding: Set by user
*/
int use_wallclock_as_timestamps;
/**
* avio flags, used to force AVIO_FLAG_DIRECT.
* - encoding: unused
* - decoding: Set by user
*/
int avio_flags;
/**
* The duration field can be estimated through various ways, and this field can be used
* to know how the duration was estimated.
* - encoding: unused
* - decoding: Read by user
*/
enum AVDurationEstimationMethod duration_estimation_method;
/**
* Skip initial bytes when opening stream
* - encoding: unused
* - decoding: Set by user
*/
int64_t skip_initial_bytes;
/**
* Correct single timestamp overflows
* - encoding: unused
* - decoding: Set by user
*/
unsigned int correct_ts_overflow;
/**
* Force seeking to any (also non key) frames.
* - encoding: unused
* - decoding: Set by user
*/
int seek2any;
/**
* Flush the I/O context after each packet.
* - encoding: Set by user
* - decoding: unused
*/
int flush_packets;
/**
* format probing score.
* The maximal score is AVPROBE_SCORE_MAX, its set when the demuxer probes
* the format.
* - encoding: unused
* - decoding: set by avformat, read by user
*/
int probe_score;
/**
* number of bytes to read maximally to identify format.
* - encoding: unused
* - decoding: set by user
*/
int format_probesize;
/**
* ',' separated list of allowed decoders.
* If NULL then all are allowed
* - encoding: unused
* - decoding: set by user
*/
char *codec_whitelist;
/**
* ',' separated list of allowed demuxers.
* If NULL then all are allowed
* - encoding: unused
* - decoding: set by user
*/
char *format_whitelist;
/**
* An opaque field for libavformat internal usage.
* Must not be accessed in any way by callers.
*/
AVFormatInternal *internal;
/**
* IO repositioned flag.
* This is set by avformat when the underlaying IO context read pointer
* is repositioned, for example when doing byte based seeking.
* Demuxers can use the flag to detect such changes.
*/
int io_repositioned;
/**
* Forced video codec.
* This allows forcing a specific decoder, even when there are multiple with
* the same codec_id.
* Demuxing: Set by user
*/
AVCodec *video_codec;
/**
* Forced audio codec.
* This allows forcing a specific decoder, even when there are multiple with
* the same codec_id.
* Demuxing: Set by user
*/
AVCodec *audio_codec;
/**
* Forced subtitle codec.
* This allows forcing a specific decoder, even when there are multiple with
* the same codec_id.
* Demuxing: Set by user
*/
AVCodec *subtitle_codec;
/**
* Forced data codec.
* This allows forcing a specific decoder, even when there are multiple with
* the same codec_id.
* Demuxing: Set by user
*/
AVCodec *data_codec;
/**
* Number of bytes to be written as padding in a metadata header.
* Demuxing: Unused.
* Muxing: Set by user via av_format_set_metadata_header_padding.
*/
int metadata_header_padding;
/**
* User data.
* This is a place for some private data of the user.
*/
void *opaque;
/**
* Callback used by devices to communicate with application.
*/
av_format_control_message control_message_cb;
/**
* Output timestamp offset, in microseconds.
* Muxing: set by user
*/
int64_t output_ts_offset;
/**
* dump format separator.
* can be ", " or "\\n " or anything else
* - muxing: Set by user.
* - demuxing: Set by user.
*/
uint8_t *dump_separator;
/**
* Forced Data codec_id.
* Demuxing: Set by user.
*/
enum AVCodecID data_codec_id;
#if FF_API_OLD_OPEN_CALLBACKS
/**
* Called to open further IO contexts when needed for demuxing.
*
* This can be set by the user application to perform security checks on
* the URLs before opening them.
* The function should behave like avio_open2(), AVFormatContext is provided
* as contextual information and to reach AVFormatContext.opaque.
*
* If NULL then some simple checks are used together with avio_open2().
*
* Must not be accessed directly from outside avformat.
* @See av_format_set_open_cb()
*
* Demuxing: Set by user.
*
* @deprecated Use io_open and io_close.
*/
attribute_deprecated
int (*open_cb)(struct AVFormatContext *s, AVIOContext **p, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options);
#endif
/**
* ',' separated list of allowed protocols.
* - encoding: unused
* - decoding: set by user
*/
char *protocol_whitelist;
/*
* A callback for opening new IO streams.
*
* Whenever a muxer or a demuxer needs to open an IO stream (typically from
* avformat_open_input() for demuxers, but for certain formats can happen at
* other times as well), it will call this callback to obtain an IO context.
*
* @param s the format context
* @param pb on success, the newly opened IO context should be returned here
* @param url the url to open
* @param flags a combination of AVIO_FLAG_*
* @param options a dictionary of additional options, with the same
* semantics as in avio_open2()
* @return 0 on success, a negative AVERROR code on failure
*
* @note Certain muxers and demuxers do nesting, i.e. they open one or more
* additional internal format contexts. Thus the AVFormatContext pointer
* passed to this callback may be different from the one facing the caller.
* It will, however, have the same 'opaque' field.
*/
int (*io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url,
int flags, AVDictionary **options);
/**
* A callback for closing the streams opened with AVFormatContext.io_open().
*/
void (*io_close)(struct AVFormatContext *s, AVIOContext *pb);
/**
* ',' separated list of disallowed protocols.
* - encoding: unused
* - decoding: set by user
*/
char *protocol_blacklist;
/**
* The maximum number of streams.
* - encoding: unused
* - decoding: set by user
*/
int max_streams;
/**
* Hijack check information.
* Demuxing: Set by user
*/
AVCheckInfo *check_info;
/**
* Whether to exit directly when hijack occurs.
* Demuxing: Set by user
*/
int hijack_exit;
/**
* Hijack error code.
* Demuxing: set by avformat
*/
int hijack_code;
/**
* start position of moov box
* Demuxing: set by avformat
*/
int64_t moov_pos;
/**
* start position of mdat box
* Demuxing: set by avformat
*/
int64_t mdat_pos;
AVFormatContext;
以上是关于从头用脚分析FFmpeg源码 - avformat_write_header的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 avformat (ffmpeg) 写入 Icecast 挂载点?
FFmpeg之avformat_alloc_context()