ffmpeg-AVFormatConext解析

AVFormatContext是个贯穿全局的数据结构,很多函数都要用它作为参数。FFmpeg代码中对这个数据结构的注释是 format I/O context,此结构包含了一个视频流的格式内容。其中存AVInputFormat(AVOutputFormat,但是同一时间 AVFormatContext内只能存在其中一).AVStream,AVPacket区儿个重要的数据结构以及一些其他的相关信息,比如 title、Hthor,copyright等.另外,企有一些可能在编解码中会用到的信息,诸如 duration,file size,bit_rate等.

由于 AVFormatConext 结构包含许多信息,因此初始化过程是分步完成的,而且其中有些变量如果没有值可用,也可不初始化。 但是由于一般声明都使用指针,因此分配内存的过程不可少.

如下代码初始化了 AVFormatContext:

1
2
AVFormatContext *pFormatCtx;
pFormatCtx=avformat_alloc_context();

结构体定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//mux:视频/音频封装 复用
//demux:解封装视频/音频 解复用
typedef struct AVFormatContext {
//与logging及avoptions相关的class
const AVClass *av_class;

//输入数据的封装格式。仅解封装用,由avformat_open_input()设置
struct AVInputFormat *iformat;

//输出数据的封装格式。仅封装用,调用者在avformat_write_header()之前设置
struct AVOutputFormat *oformat;

/**
* 私有数据 是一个 AVOptions-enabled struct
* 当且仅当iformat/oformat.priv_class 不为 NULL.
*
* - muxing: set by avformat_write_header()
* - demuxing: set by avformat_open_input()
*/
void *priv_data;

/**
* I/O上下文
* 解封装:由用户在avformat_open_input()之前设置
* (然后用户必须手动关闭它)或通过 avformat_open_input()设置
*
* 封装:由用户在avformat_write_header()之前设置。
* 调用者必须注意关闭/释放IO上下文
* /
AVIOContext *pb;

//流的数量
unsigned int nb_streams;

//文件中所有流的列表 可以通过avformat_new_stream创建新的stream
AVStream **streams;

#if FF_API_FORMAT_FILENAME
/**
* input or output filename
*
* - 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.
*
* - demuxing: set by avformat_open_input(), initialized to an empty
* string if url parameter was NULL in avformat_open_input().
* - 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().
*
* Freed by libavformat in avformat_free_context().
*/
char *url;

/**
* 第一帧的位置
*
* Demuxing only, set by libavformat.
*/
int64_t start_time;

/**
* Stream的时长
*
* Demuxing only, set by libavformat.
*/
int64_t duration;

/**
* 整个流的比特率
*/
int64_t bit_rate;

unsigned int packet_size;
int max_delay;

/**
* Demuxer/Muxer的状态
* Set by the user before avformat_open_input() / avformat_write_header().
*/
int flags;

/**
* 从输入读取的用于确定输入容器格式的数据的最大大小
* 仅封装用,由调用者在avformat_open_input()之前设置
* Demuxing only, set by the caller before avformat_open_input().
*/
int64_t probesize;

/**
* 通过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;

/**
* 字幕 codec_id.
* Demuxing: Set by user.
*/
enum AVCodecID subtitle_codec_id;

/**
* 每条流的最大内存字节数
*/
unsigned int max_index_size;

/**
* buffering frames中的最大的内存字节数
*/
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;

/**
* 整个文件的元数据.
*
* - 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;

/**
* 起始时间,从PST=0开始
*/
int64_t start_time_realtime;

/**
* 在avformat_find_stream_info()中,用于确定帧数.
* Demuxing only, set by the caller before avformat_find_stream_info().
*/
int fps_probe_size;

/**
* 错误检测.
* Demuxing only, set by the caller before avformat_open_input().
*/
int error_recognition;

/**
* 自定义的终端callback
*/
AVIOInterruptCB interrupt_callback;

/**
* 标记是否debugging.
*/
int debug;
#define FF_FDEBUG_TS 0x0001

/**
* 最大交叉Buffering(缓冲数据)时长,在muxing(复用)时使用
* Muxing only, set by the caller before avformat_write_header().
*/
int64_t max_interleave_delta;

int strict_std_compliance;

/**
* 时间flag
*/
int event_flags;
#define AVFMT_EVENT_FLAG_METADATA_UPDATED 0x0001 ///< The call resulted in updated metadata.

/**
* 解码第一帧时读取的最大Packet数目
*/
int max_ts_probe;

/**
* 在muxing时避免无效时间戳
* - 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;

/**
* 音频预加载
* - encoding: Set by user
* - decoding: unused
*/
int audio_preload;

/**
* 最大chunk时长,不是所有格式都支持chunk方式
* - encoding: Set by user
* - decoding: unused
*/
int max_chunk_duration;

/**
* 最大chunk 以字节为单位
* - encoding: Set by user
* - decoding: unused
*/
int max_chunk_size;

/**
* 强制使用wallclock时间戳作为数据包的PTS/DTS,B帧存在时,未定义
* - 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;

/**
* 可以通过不同的方法估计持续时间字段
* - encoding: unused
* - decoding: Read by user
*/
enum AVDurationEstimationMethod duration_estimation_method;

/**
* 在打开流时,跳过初始字节
* - encoding: unused
* - decoding: Set by user
*/
int64_t skip_initial_bytes;

/**
* 纠正单个时间戳溢出
* - encoding: unused
* - decoding: Set by user
*/
unsigned int correct_ts_overflow;

/**
* 强制快进/快退到任意帧
* - encoding: unused
* - decoding: Set by user
*/
int seek2any;

/**
* 刷新 I/O context after each packet.
* - encoding: Set by user
* - decoding: unused
*/
int flush_packets;

/**
* 格式探测评分,最大评分是 AVPROBE_SCORE_MAX
* - encoding: unused
* - decoding: set by avformat, read by user
*/
int probe_score;

/**
* 读取最大的字节数以确定格式.
* - encoding: unused
* - decoding: set by user
*/
int format_probesize;

/**
* ',' 分隔,所有可用的Decoder
*/
char *codec_whitelist;

/**
* ',' 分隔,所有可用的Demuxer
*/
char *format_whitelist;

/**
* An opaque field for libavformat internal usage.
* Must not be accessed in any way by callers.
*/
AVFormatInternal *internal;

/**
* IO 更改的标志
*/
int io_repositioned;

/**
* 特殊解码器或相同codec_id的视频Codec
*/
AVCodec *video_codec;

/**
* 特殊解码器或相同codec_id的音频Codec
*/
AVCodec *audio_codec;

/**
* 特殊解码器或相同codec_id的字幕Codec
*/
AVCodec *subtitle_codec;

/**
* 特殊解码器或相同codec_id的数据Codec
*/
AVCodec *data_codec;

/**
* metadata头设置的padding值
*/
int metadata_header_padding;

/**
* User data.
* This is a place for some private data of the user.
*/
void *opaque;

/**
* 设备和应用通信的callback
*/
av_format_control_message control_message_cb;

/**
* 输出时间戳偏移值
*/
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
/**
* 使用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

/**
* ',' 分隔,协议白名单
*/
char *protocol_whitelist;

/**
* 当IO流打开时,demux操作的回调函数
*/
int (*io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url,
int flags, AVDictionary **options);

/**
* AVFormatContext.io_open() 操作的回调.
*/
void (*io_close)(struct AVFormatContext *s, AVIOContext *pb);

/**
* ','分隔,协议黑名单
*/
char *protocol_blacklist;

/**
* The maximum number of streams.
* - encoding: unused
* - decoding: set by user
*/
int max_streams;

/**
* Skip duration calcuation in estimate_timings_from_pts.
* - encoding: unused
* - decoding: set by user
*/
int skip_estimate_duration_from_pts;
} AVFormatContext;