avformat.h 45.4 KB
Newer Older
1 2 3
/*
 * copyright (c) 2001 Fabrice Bellard
 *
4 5 6
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
7 8
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12 13 14 15 16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18 19 20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

21 22
#ifndef AVFORMAT_AVFORMAT_H
#define AVFORMAT_AVFORMAT_H
F
Fabrice Bellard 已提交
23

24
#define LIBAVFORMAT_VERSION_MAJOR 52
25
#define LIBAVFORMAT_VERSION_MINOR 30
26
#define LIBAVFORMAT_VERSION_MICRO  1
27

28 29 30 31 32 33
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                               LIBAVFORMAT_VERSION_MINOR, \
                                               LIBAVFORMAT_VERSION_MICRO)
#define LIBAVFORMAT_VERSION     AV_VERSION(LIBAVFORMAT_VERSION_MAJOR,   \
                                           LIBAVFORMAT_VERSION_MINOR,   \
                                           LIBAVFORMAT_VERSION_MICRO)
M
Michael Niedermayer 已提交
34
#define LIBAVFORMAT_BUILD       LIBAVFORMAT_VERSION_INT
A
Alex Beregszaszi 已提交
35

M
Michael Niedermayer 已提交
36
#define LIBAVFORMAT_IDENT       "Lavf" AV_STRINGIFY(LIBAVFORMAT_VERSION)
37

S
Stefano Sabatini 已提交
38 39 40 41 42
/**
 * Returns the LIBAVFORMAT_VERSION_INT constant.
 */
unsigned avformat_version(void);

43
#include <time.h>
Z
Zdenek Kabelac 已提交
44
#include <stdio.h>  /* FILE */
45
#include "libavcodec/avcodec.h"
F
Fabrice Bellard 已提交
46 47 48

#include "avio.h"

49 50

/*
51
 * Public Metadata API.
52
 * !!WARNING!! This is a work in progress. Don't use outside FFmpeg for now.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
 * The metadata API allows libavformat to export metadata tags to a client
 * application using a sequence of key/value pairs.
 * Important concepts to keep in mind:
 * 1. Keys are unique; there can never be 2 tags with the same key. This is
 *    also meant semantically, i.e., a demuxer should not knowingly produce
 *    several keys that are literally different but semantically identical.
 *    E.g., key=Author5, key=Author6. In this example, all authors must be
 *    placed in the same tag.
 * 2. Metadata is flat, not hierarchical; there are no subtags. If you
 *    want to store, e.g., the email address of the child of producer Alice
 *    and actor Bob, that could have key=alice_and_bobs_childs_email_address.
 * 3. A tag whose value is localized for a particular language is appended
 *    with a dash character ('-') and the ISO 639 3-letter language code.
 *    For example: Author-ger=Michael, Author-eng=Mike
 *    The original/default language is in the unqualified "Author" tag.
68 69 70
 *    A demuxer should set a default if it sets any translated tag.
 */

71
#define AV_METADATA_MATCH_CASE      1
72 73 74 75 76
#define AV_METADATA_IGNORE_SUFFIX   2

typedef struct {
    char *key;
    char *value;
77
}AVMetadataTag;
78

A
Aurelien Jacobs 已提交
79
typedef struct AVMetadata AVMetadata;
80 81

/**
82 83 84 85
 * Gets a metadata element with matching key.
 * @param prev Set to the previous matching element to find the next.
 * @param flags Allows case as well as suffix-insensitive comparisons.
 * @return Found tag or NULL, changing key or value leads to undefined behavior.
86
 */
87
AVMetadataTag *
A
Aurelien Jacobs 已提交
88
av_metadata_get(AVMetadata *m, const char *key, const AVMetadataTag *prev, int flags);
89 90

/**
91 92 93 94
 * Sets the given tag in m, overwriting an existing tag.
 * @param key tag key to add to m (will be av_strduped)
 * @param value tag value to add to m (will be av_strduped)
 * @return >= 0 on success otherwise an error code <0
95
 */
96
int av_metadata_set(AVMetadata **pm, const char *key, const char *value);
97

98
/**
99
 * Frees all the memory allocated for an AVMetadata struct.
100 101 102
 */
void av_metadata_free(AVMetadata **m);

103

F
Fabrice Bellard 已提交
104 105 106
/* packet functions */

typedef struct AVPacket {
M
Michael Niedermayer 已提交
107
    /**
108 109
     * Presentation timestamp in time_base units; the time at which the
     * decompressed packet will be presented to the user.
M
Michael Niedermayer 已提交
110
     * Can be AV_NOPTS_VALUE if it is not stored in the file.
D
Diego Biurrun 已提交
111
     * pts MUST be larger or equal to dts as presentation cannot happen before
M
Michael Niedermayer 已提交
112
     * decompression, unless one wants to view hex dumps. Some formats misuse
113
     * the terms dts and pts/cts to mean something different. Such timestamps
M
Michael Niedermayer 已提交
114 115 116 117
     * must be converted to true pts/dts before they are stored in AVPacket.
     */
    int64_t pts;
    /**
118 119
     * Decompression timestamp in time_base units; the time at which the
     * packet is decompressed.
M
Michael Niedermayer 已提交
120 121 122
     * Can be AV_NOPTS_VALUE if it is not stored in the file.
     */
    int64_t dts;
123
    uint8_t *data;
124 125 126
    int   size;
    int   stream_index;
    int   flags;
127 128 129 130 131
    /**
     * Duration of this packet in time_base units, 0 if unknown.
     * Equals next_pts - this_pts in presentation order.
     */
    int   duration;
132 133
    void  (*destruct)(struct AVPacket *);
    void  *priv;
M
Michael Niedermayer 已提交
134
    int64_t pos;                            ///< byte position in stream, -1 if unknown
135 136

    /**
D
Diego Biurrun 已提交
137
     * Time difference in stream time base units from the pts of this
138
     * packet to the point at which the output from the decoder has converged
D
Diego Biurrun 已提交
139 140 141 142
     * independent from the availability of previous frames. That is, the
     * frames are virtually identical no matter if decoding started from
     * the very first frame or from this keyframe.
     * Is AV_NOPTS_VALUE if unknown.
143
     * This field is not the display duration of the current packet.
144 145 146
     *
     * The purpose of this field is to allow seeking in streams that have no
     * keyframes in the conventional sense. It corresponds to the
D
Diego Biurrun 已提交
147
     * recovery point SEI in H.264 and match_time_delta in NUT. It is also
148 149 150 151
     * essential for some types of subtitle streams to ensure that all
     * subtitles are correctly displayed after seeking.
     */
    int64_t convergence_duration;
152
} AVPacket;
153 154
#define PKT_FLAG_KEY   0x0001

M
Michael Niedermayer 已提交
155
void av_destruct_packet_nofree(AVPacket *pkt);
156 157 158 159

/**
 * Default packet destructor.
 */
160
void av_destruct_packet(AVPacket *pkt);
M
Michael Niedermayer 已提交
161

R
Ramiro Polla 已提交
162
/**
D
Diego Biurrun 已提交
163
 * Initialize optional fields of a packet with default values.
R
Ramiro Polla 已提交
164 165 166
 *
 * @param pkt packet
 */
R
Ramiro Polla 已提交
167
void av_init_packet(AVPacket *pkt);
F
Fabrice Bellard 已提交
168

169
/**
D
Diego Biurrun 已提交
170 171
 * Allocate the payload of a packet and initialize its fields with
 * default values.
172 173 174
 *
 * @param pkt packet
 * @param size wanted payload size
D
Diego Biurrun 已提交
175
 * @return 0 if OK, AVERROR_xxx otherwise
176
 */
F
Fabrice Bellard 已提交
177
int av_new_packet(AVPacket *pkt, int size);
178 179

/**
D
Diego Biurrun 已提交
180 181
 * Allocate and read the payload of a packet and initialize its fields with
 * default values.
182 183
 *
 * @param pkt packet
D
Diego Biurrun 已提交
184 185
 * @param size desired payload size
 * @return >0 (read size) if OK, AVERROR_xxx otherwise
186
 */
M
Michael Niedermayer 已提交
187
int av_get_packet(ByteIOContext *s, AVPacket *pkt, int size);
188

189 190
/**
 * @warning This is a hack - the packet memory allocation stuff is broken. The
D
Diego Biurrun 已提交
191
 * packet is allocated if it was not really allocated.
192
 */
193
int av_dup_packet(AVPacket *pkt);
194 195

/**
D
Diego Biurrun 已提交
196
 * Free a packet.
197 198 199 200 201
 *
 * @param pkt packet to free
 */
static inline void av_free_packet(AVPacket *pkt)
{
202
    if (pkt && pkt->destruct) {
203
        pkt->destruct(pkt);
204
    }
205
}
F
Fabrice Bellard 已提交
206

F
Fabrice Bellard 已提交
207 208 209
/*************************************************/
/* fractional numbers for exact pts handling */

210
/**
D
Diego Biurrun 已提交
211 212
 * The exact value of the fractional number is: 'val + num / den'.
 * num is assumed to be 0 <= num < den.
213
 */
F
Fabrice Bellard 已提交
214
typedef struct AVFrac {
215
    int64_t val, num, den;
216
} AVFrac;
F
Fabrice Bellard 已提交
217

F
Fabrice Bellard 已提交
218
/*************************************************/
219
/* input/output formats */
F
Fabrice Bellard 已提交
220

221 222
struct AVCodecTag;

F
Fabrice Bellard 已提交
223
struct AVFormatContext;
224

D
Diego Biurrun 已提交
225
/** This structure contains the data a format has to probe a file. */
226
typedef struct AVProbeData {
227
    const char *filename;
228 229 230 231
    unsigned char *buf;
    int buf_size;
} AVProbeData;

232
#define AVPROBE_SCORE_MAX 100               ///< maximum score, half of that is used for file-extension-based detection
233
#define AVPROBE_PADDING_SIZE 32             ///< extra allocated bytes at the end of the probe buffer
F
Fabrice Bellard 已提交
234 235

typedef struct AVFormatParameters {
236
    AVRational time_base;
F
Fabrice Bellard 已提交
237 238 239 240
    int sample_rate;
    int channels;
    int width;
    int height;
241
    enum PixelFormat pix_fmt;
D
Diego Biurrun 已提交
242 243 244 245
    int channel; /**< Used to select DV channel. */
    const char *standard; /**< TV standard, NTSC, PAL, SECAM */
    unsigned int mpeg2ts_raw:1;  /**< Force raw MPEG-2 transport stream output, if possible. */
    unsigned int mpeg2ts_compute_pcr:1; /**< Compute exact PCR for each transport
246
                                            stream packet (only meaningful if
D
Diego Biurrun 已提交
247 248 249
                                            mpeg2ts_raw is TRUE). */
    unsigned int initial_pause:1;       /**< Do not begin to play the stream
                                            immediately (RTSP only). */
250
    unsigned int prealloced_context:1;
251
#if LIBAVFORMAT_VERSION_INT < (53<<16)
252 253
    enum CodecID video_codec_id;
    enum CodecID audio_codec_id;
254
#endif
F
Fabrice Bellard 已提交
255 256
} AVFormatParameters;

D
Diego Biurrun 已提交
257
//! Demuxer will use url_fopen, no opened file should be provided by the caller.
258
#define AVFMT_NOFILE        0x0001
D
Diego Biurrun 已提交
259 260 261 262 263 264 265 266
#define AVFMT_NEEDNUMBER    0x0002 /**< Needs '%d' in filename. */
#define AVFMT_SHOW_IDS      0x0008 /**< Show format stream IDs numbers. */
#define AVFMT_RAWPICTURE    0x0020 /**< Format wants AVPicture structure for
                                      raw picture data. */
#define AVFMT_GLOBALHEADER  0x0040 /**< Format wants global header. */
#define AVFMT_NOTIMESTAMPS  0x0080 /**< Format does not need / have any timestamps. */
#define AVFMT_GENERIC_INDEX 0x0100 /**< Use generic index building code. */
#define AVFMT_TS_DISCONT    0x0200 /**< Format allows timestamp discontinuities. */
267
#define AVFMT_VARIABLE_FPS  0x0400 /**< Format allows variable fps. */
268 269

typedef struct AVOutputFormat {
F
Fabrice Bellard 已提交
270
    const char *name;
271 272 273 274 275
    /**
     * Descriptive name for the format, meant to be more human-readable
     * than \p name. You \e should use the NULL_IF_CONFIG_SMALL() macro
     * to define it.
     */
F
Fabrice Bellard 已提交
276 277
    const char *long_name;
    const char *mime_type;
D
Diego Biurrun 已提交
278
    const char *extensions; /**< comma-separated filename extensions */
279
    /** size of private data so that it can be allocated in the wrapper */
280
    int priv_data_size;
F
Fabrice Bellard 已提交
281
    /* output support */
282 283
    enum CodecID audio_codec; /**< default audio codec */
    enum CodecID video_codec; /**< default video codec */
F
Fabrice Bellard 已提交
284
    int (*write_header)(struct AVFormatContext *);
285
    int (*write_packet)(struct AVFormatContext *, AVPacket *pkt);
F
Fabrice Bellard 已提交
286
    int (*write_trailer)(struct AVFormatContext *);
287
    /** can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER */
288
    int flags;
D
Diego Biurrun 已提交
289
    /** Currently only used to set pixel format if not YUV420P. */
290
    int (*set_parameters)(struct AVFormatContext *, AVFormatParameters *);
291 292
    int (*interleave_packet)(struct AVFormatContext *, AVPacket *out,
                             AVPacket *in, int flush);
293 294

    /**
295
     * List of supported codec_id-codec_tag pairs, ordered by "better
296
     * choice first". The arrays are all terminated by CODEC_ID_NONE.
297
     */
298
    const struct AVCodecTag * const *codec_tag;
299

300 301
    enum CodecID subtitle_codec; /**< default subtitle codec */

302 303 304
    /* private fields */
    struct AVOutputFormat *next;
} AVOutputFormat;
F
Fabrice Bellard 已提交
305

306 307
typedef struct AVInputFormat {
    const char *name;
308 309 310 311 312
    /**
     * Descriptive name for the format, meant to be more human-readable
     * than \p name. You \e should use the NULL_IF_CONFIG_SMALL() macro
     * to define it.
     */
313
    const char *long_name;
D
Diego Biurrun 已提交
314
    /** Size of private data so that it can be allocated in the wrapper. */
315
    int priv_data_size;
316
    /**
317
     * Tell if a given file has a chance of being parsed as this format.
D
Diego Biurrun 已提交
318 319
     * The buffer provided is guaranteed to be AVPROBE_PADDING_SIZE bytes
     * big so you do not have to check for that unless you need more.
320
     */
321
    int (*read_probe)(AVProbeData *);
D
Diego Biurrun 已提交
322 323 324
    /** Read the format header and initialize the AVFormatContext
       structure. Return 0 if OK. 'ap' if non-NULL contains
       additional parameters. Only used in raw format right
325
       now. 'av_new_stream' should be called to create new streams.  */
F
Fabrice Bellard 已提交
326 327
    int (*read_header)(struct AVFormatContext *,
                       AVFormatParameters *ap);
D
Diego Biurrun 已提交
328
    /** Read one packet and put it in 'pkt'. pts and flags are also
329
       set. 'av_new_stream' can be called only if the flag
330
       AVFMTCTX_NOHEADER is used. */
F
Fabrice Bellard 已提交
331
    int (*read_packet)(struct AVFormatContext *, AVPacket *pkt);
D
Diego Biurrun 已提交
332
    /** Close the stream. The AVFormatContext and AVStreams are not
F
Fabrice Bellard 已提交
333 334
       freed by this function */
    int (*read_close)(struct AVFormatContext *);
M
Michael Niedermayer 已提交
335 336

#if LIBAVFORMAT_VERSION_MAJOR < 53
337
    /**
D
Diego Biurrun 已提交
338 339
     * Seek to a given timestamp relative to the frames in
     * stream component stream_index.
340 341 342
     * @param stream_index Must not be -1.
     * @param flags Selects which direction should be preferred if no exact
     *              match is available.
343
     * @return >= 0 on success (but not necessarily the new offset)
344
     */
345
    int (*read_seek)(struct AVFormatContext *,
346
                     int stream_index, int64_t timestamp, int flags);
M
Michael Niedermayer 已提交
347
#endif
348
    /**
D
Diego Biurrun 已提交
349
     * Gets the next timestamp in stream[stream_index].time_base units.
D
Diego Biurrun 已提交
350
     * @return the timestamp or AV_NOPTS_VALUE if an error occurred
351 352 353
     */
    int64_t (*read_timestamp)(struct AVFormatContext *s, int stream_index,
                              int64_t *pos, int64_t pos_limit);
D
Diego Biurrun 已提交
354
    /** Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER. */
F
Fabrice Bellard 已提交
355
    int flags;
D
Diego Biurrun 已提交
356
    /** If extensions are defined, then no probe is done. You should
357 358 359
       usually not use extension format guessing because it is not
       reliable enough */
    const char *extensions;
D
Diego Biurrun 已提交
360
    /** General purpose read-only value that the format can use. */
361
    int value;
362

D
Diego Biurrun 已提交
363 364
    /** Start/resume playing - only meaningful if using a network-based format
       (RTSP). */
365 366
    int (*read_play)(struct AVFormatContext *);

D
Diego Biurrun 已提交
367 368
    /** Pause playing - only meaningful if using a network-based format
       (RTSP). */
369 370
    int (*read_pause)(struct AVFormatContext *);

371
    const struct AVCodecTag * const *codec_tag;
372

M
Michael Niedermayer 已提交
373 374 375 376 377 378
    /**
     * Seek to timestamp ts.
     * Seeking will be done so that the point from which all active streams
     * can be presented successfully will be closest to ts and within min/max_ts.
     * Active streams are all streams that have AVStream.discard < AVDISCARD_ALL.
     */
M
untypo  
Michael Niedermayer 已提交
379
    int (*read_seek2)(struct AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags);
M
Michael Niedermayer 已提交
380

381 382 383
    /* private fields */
    struct AVInputFormat *next;
} AVInputFormat;
F
Fabrice Bellard 已提交
384

A
Aurelien Jacobs 已提交
385 386 387
enum AVStreamParseType {
    AVSTREAM_PARSE_NONE,
    AVSTREAM_PARSE_FULL,       /**< full parsing and repack */
D
Diego Biurrun 已提交
388
    AVSTREAM_PARSE_HEADERS,    /**< Only parse headers, do not repack. */
389
    AVSTREAM_PARSE_TIMESTAMPS, /**< full parsing and interpolation of timestamps for frames not starting on a packet boundary */
A
Aurelien Jacobs 已提交
390 391
};

392 393 394 395
typedef struct AVIndexEntry {
    int64_t pos;
    int64_t timestamp;
#define AVINDEX_KEYFRAME 0x0001
M
Michael Niedermayer 已提交
396
    int flags:2;
D
Diego Biurrun 已提交
397 398
    int size:30; //Yeah, trying to keep the size of this small to reduce memory requirements (it is 24 vs. 32 bytes due to possible 8-byte alignment).
    int min_distance;         /**< Minimum distance between this and the previous keyframe, used to avoid unneeded searching. */
399 400
} AVIndexEntry;

401 402 403 404 405 406 407
#define AV_DISPOSITION_DEFAULT   0x0001
#define AV_DISPOSITION_DUB       0x0002
#define AV_DISPOSITION_ORIGINAL  0x0004
#define AV_DISPOSITION_COMMENT   0x0008
#define AV_DISPOSITION_LYRICS    0x0010
#define AV_DISPOSITION_KARAOKE   0x0020

408 409 410
/**
 * Stream structure.
 * New fields can be added to the end with minor version bumps.
D
Diego Biurrun 已提交
411
 * Removal, reordering and changes to existing fields require a major
412
 * version bump.
D
Diego Biurrun 已提交
413
 * sizeof(AVStream) must not be used outside libav*.
414
 */
F
Fabrice Bellard 已提交
415
typedef struct AVStream {
416
    int index;    /**< stream index in AVFormatContext */
D
Diego Biurrun 已提交
417
    int id;       /**< format-specific stream ID */
418
    AVCodecContext *codec; /**< codec context */
419
    /**
420 421
     * Real base framerate of the stream.
     * This is the lowest framerate with which all timestamps can be
R
Typos  
Ramiro Polla 已提交
422
     * represented accurately (it is the least common multiple of all
423 424
     * framerates in the stream). Note, this value is just a guess!
     * For example, if the time base is 1/90000 and all frames have either
D
Diego Biurrun 已提交
425
     * approximately 3600 or 1800 timer ticks, then r_frame_rate will be 50/1.
426 427
     */
    AVRational r_frame_rate;
F
Fabrice Bellard 已提交
428
    void *priv_data;
429

430
    /* internal data used in av_find_stream_info() */
431
    int64_t first_dts;
D
Diego Biurrun 已提交
432
    /** encoding: pts generation when outputting stream */
433
    struct AVFrac pts;
434 435

    /**
D
Diego Biurrun 已提交
436 437
     * This is the fundamental unit of time (in seconds) in terms
     * of which frame timestamps are represented. For fixed-fps content,
438
     * time base should be 1/framerate and timestamp increments should be 1.
439
     */
440
    AVRational time_base;
441
    int pts_wrap_bits; /**< number of bits in pts (used for wrapping control) */
F
Fabrice Bellard 已提交
442
    /* ffmpeg.c private use */
D
Diego Biurrun 已提交
443 444
    int stream_copy; /**< If set, just copy stream. */
    enum AVDiscard discard; ///< Selects which packets can be discarded at will and do not need to be demuxed.
445
    //FIXME move stuff to a flags field?
D
Diego Biurrun 已提交
446
    /** Quality, as it has been removed from AVCodecContext and put in AVVideoFrame.
D
Diego Biurrun 已提交
447
     * MN: dunno if that is the right place for it */
448
    float quality;
449
    /**
D
Diego Biurrun 已提交
450 451 452
     * Decoding: pts of the first frame of the stream, in stream time base.
     * Only set this if you are absolutely 100% sure that the value you set
     * it to really is the pts of the first frame.
453
     * This may be undefined (AV_NOPTS_VALUE).
D
Diego Biurrun 已提交
454 455
     * @note The ASF header does NOT contain a correct start_time the ASF
     * demuxer must NOT set this.
456
     */
457
    int64_t start_time;
458
    /**
D
Diego Biurrun 已提交
459
     * Decoding: duration of the stream, in stream time base.
460
     * If a source file does not specify a duration, but does specify
D
Diego Biurrun 已提交
461
     * a bitrate, this value will be estimated from bitrate and file size.
462
     */
463
    int64_t duration;
464

465
    char language[4]; /** ISO 639 3-letter language code (empty string if undefined) */
466

467
    /* av_read_frame() support */
A
Aurelien Jacobs 已提交
468
    enum AVStreamParseType need_parsing;
469
    struct AVCodecParserContext *parser;
470

471
    int64_t cur_dts;
M
Michael Niedermayer 已提交
472 473
    int last_IP_duration;
    int64_t last_IP_pts;
474
    /* av_seek_frame() support */
D
Diego Biurrun 已提交
475 476
    AVIndexEntry *index_entries; /**< Only used if the format does not
                                    support seeking natively. */
477
    int nb_index_entries;
M
Måns Rullgård 已提交
478
    unsigned int index_entries_allocated_size;
479

480
    int64_t nb_frames;                 ///< number of frames in this stream if known or 0
481

482 483 484
#if LIBAVFORMAT_VERSION_INT < (53<<16)
    int64_t unused[4+1];
#endif
485 486

    char *filename; /**< source filename of the stream */
487

D
Diego Biurrun 已提交
488
    int disposition; /**< AV_DISPOSITION_* bit field */
489 490

    AVProbeData probe_data;
491 492
#define MAX_REORDER_DELAY 16
    int64_t pts_buffer[MAX_REORDER_DELAY+1];
493 494 495 496 497 498 499

    /**
     * sample aspect ratio (0 if unknown)
     * - encoding: Set by user.
     * - decoding: Set by libavformat.
     */
    AVRational sample_aspect_ratio;
500

A
Aurelien Jacobs 已提交
501
    AVMetadata *metadata;
502 503 504 505 506

    /* av_read_frame() support */
    const uint8_t *cur_ptr;
    int cur_len;
    AVPacket cur_pkt;
507 508 509 510 511 512 513 514 515 516

    // Timestamp generation support:
    /**
     * Timestamp corresponding to the last dts sync point.
     *
     * Initialized when AVCodecParserContext.dts_sync_point >= 0 and
     * a DTS is received from the underlying container. Otherwise set to
     * AV_NOPTS_VALUE by default.
     */
    int64_t reference_dts;
F
Fabrice Bellard 已提交
517 518
} AVStream;

519 520
#define AV_PROGRAM_RUNNING 1

521 522
/**
 * New fields can be added to the end with minor version bumps.
D
Diego Biurrun 已提交
523
 * Removal, reordering and changes to existing fields require a major
524
 * version bump.
D
Diego Biurrun 已提交
525
 * sizeof(AVProgram) must not be used outside libav*.
526
 */
527 528
typedef struct AVProgram {
    int            id;
D
Diego Biurrun 已提交
529 530
    char           *provider_name; ///< network name for DVB streams
    char           *name;          ///< service name for DVB streams
531 532
    int            flags;
    enum AVDiscard discard;        ///< selects which program to discard and which to feed to the caller
533 534
    unsigned int   *stream_index;
    unsigned int   nb_stream_indexes;
A
Aurelien Jacobs 已提交
535
    AVMetadata *metadata;
536 537
} AVProgram;

538
#define AVFMTCTX_NOHEADER      0x0001 /**< signal that no header is present
539 540
                                         (streams are added dynamically) */

541
typedef struct AVChapter {
D
Diego Biurrun 已提交
542 543
    int id;                 ///< unique ID to identify the chapter
    AVRational time_base;   ///< time base in which the start/end timestamps are specified
M
Michael Niedermayer 已提交
544
    int64_t start, end;     ///< chapter start/end time in time_base units
M
Michael Niedermayer 已提交
545
    char *title;            ///< chapter title
A
Aurelien Jacobs 已提交
546
    AVMetadata *metadata;
547 548
} AVChapter;

F
Fabrice Bellard 已提交
549 550
#define MAX_STREAMS 20

551
/**
D
Diego Biurrun 已提交
552
 * Format I/O context.
553
 * New fields can be added to the end with minor version bumps.
D
Diego Biurrun 已提交
554
 * Removal, reordering and changes to existing fields require a major
555
 * version bump.
D
Diego Biurrun 已提交
556
 * sizeof(AVFormatContext) must not be used outside libav*.
557
 */
F
Fabrice Bellard 已提交
558
typedef struct AVFormatContext {
559
    const AVClass *av_class; /**< Set by avformat_alloc_context. */
D
Diego Biurrun 已提交
560
    /* Can only be iformat or oformat, not both at the same time. */
561 562
    struct AVInputFormat *iformat;
    struct AVOutputFormat *oformat;
F
Fabrice Bellard 已提交
563
    void *priv_data;
564
    ByteIOContext *pb;
565
    unsigned int nb_streams;
F
Fabrice Bellard 已提交
566
    AVStream *streams[MAX_STREAMS];
567
    char filename[1024]; /**< input or output filename */
F
Fabrice Bellard 已提交
568
    /* stream info */
569
    int64_t timestamp;
F
Fabrice Bellard 已提交
570 571 572 573
    char title[512];
    char author[512];
    char copyright[512];
    char comment[512];
574
    char album[512];
575 576 577
    int year;  /**< ID3 year, 0 if none */
    int track; /**< track number, 0 if none */
    char genre[32]; /**< ID3 genre */
578

D
Diego Biurrun 已提交
579 580
    int ctx_flags; /**< Format-specific flags, see AVFMTCTX_xx */
    /* private data for pts handling (do not modify directly). */
581
    /** This buffer is only needed when packets were already buffered but
D
Diego Biurrun 已提交
582 583
       not decoded, for example to get the codec parameters in MPEG
       streams. */
584 585
    struct AVPacketList *packet_buffer;

D
Diego Biurrun 已提交
586
    /** Decoding: position of the first frame of the component, in
587
       AV_TIME_BASE fractional seconds. NEVER set this value directly:
D
Diego Biurrun 已提交
588
       It is deduced from the AVStream values.  */
589
    int64_t start_time;
D
Diego Biurrun 已提交
590
    /** Decoding: duration of the stream, in AV_TIME_BASE fractional
591 592 593
       seconds. NEVER set this value directly: it is deduced from the
       AVStream values.  */
    int64_t duration;
D
Diego Biurrun 已提交
594
    /** decoding: total file size, 0 if unknown */
595
    int64_t file_size;
D
Diego Biurrun 已提交
596
    /** Decoding: total stream bitrate in bit/s, 0 if not
597
       available. Never set it directly if the file_size and the
598
       duration are known as FFmpeg can compute it automatically. */
599
    int bit_rate;
600 601 602

    /* av_read_frame() support */
    AVStream *cur_st;
603 604 605 606 607
#if LIBAVFORMAT_VERSION_INT < (53<<16)
    const uint8_t *cur_ptr_deprecated;
    int cur_len_deprecated;
    AVPacket cur_pkt_deprecated;
#endif
608 609

    /* av_seek_frame() support */
610
    int64_t data_offset; /** offset of the first packet */
611
    int index_built;
612

613 614
    int mux_rate;
    int packet_size;
615 616
    int preload;
    int max_delay;
617

618 619
#define AVFMT_NOOUTPUTLOOP -1
#define AVFMT_INFINITEOUTPUTLOOP 0
620
    /** number of times to loop output in formats that support it */
621
    int loop_output;
622

623
    int flags;
624
#define AVFMT_FLAG_GENPTS       0x0001 ///< Generate missing pts even if it requires parsing future frames.
D
Diego Biurrun 已提交
625 626
#define AVFMT_FLAG_IGNIDX       0x0002 ///< Ignore index.
#define AVFMT_FLAG_NONBLOCK     0x0004 ///< Do not block when reading packets from input.
627 628

    int loop_input;
629
    /** decoding: size of data to probe; encoding: unused. */
630
    unsigned int probesize;
631 632

    /**
D
Diego Biurrun 已提交
633 634
     * Maximum time (in AV_TIME_BASE units) during which the input should
     * be analyzed in av_find_stream_info().
635 636
     */
    int max_analyze_duration;
637 638 639

    const uint8_t *key;
    int keylen;
640 641 642

    unsigned int nb_programs;
    AVProgram **programs;
M
Michael Niedermayer 已提交
643 644 645

    /**
     * Forced video codec_id.
D
Diego Biurrun 已提交
646
     * Demuxing: Set by user.
M
Michael Niedermayer 已提交
647 648 649 650
     */
    enum CodecID video_codec_id;
    /**
     * Forced audio codec_id.
D
Diego Biurrun 已提交
651
     * Demuxing: Set by user.
M
Michael Niedermayer 已提交
652 653 654 655
     */
    enum CodecID audio_codec_id;
    /**
     * Forced subtitle codec_id.
D
Diego Biurrun 已提交
656
     * Demuxing: Set by user.
M
Michael Niedermayer 已提交
657 658
     */
    enum CodecID subtitle_codec_id;
659 660

    /**
661 662
     * 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
663 664
     * needed to maintain a smaller size. This can lead to slower or less
     * accurate seeking (depends on demuxer).
D
Diego Biurrun 已提交
665
     * Demuxers for which a full in-memory index is mandatory will ignore
666 667 668 669 670
     * this.
     * muxing  : unused
     * demuxing: set by user
     */
    unsigned int max_index_size;
671 672

    /**
R
Ramiro Polla 已提交
673
     * Maximum amount of memory in bytes to use for buffering frames
D
Diego Biurrun 已提交
674
     * obtained from realtime capture devices.
675 676
     */
    unsigned int max_picture_buffer;
677

678
    unsigned int nb_chapters;
679
    AVChapter **chapters;
680 681

    /**
D
Diego Biurrun 已提交
682
     * Flags to enable debugging.
683 684 685
     */
    int debug;
#define FF_FDEBUG_TS        0x0001
686 687

    /**
D
Diego Biurrun 已提交
688
     * Raw packets from the demuxer, prior to parsing and decoding.
689 690 691 692 693
     * This buffer is used for buffering packets until the codec can
     * be identified, as parsing cannot be done without knowing the
     * codec.
     */
    struct AVPacketList *raw_packet_buffer;
694 695 696
    struct AVPacketList *raw_packet_buffer_end;

    struct AVPacketList *packet_buffer_end;
M
Michael Niedermayer 已提交
697

A
Aurelien Jacobs 已提交
698
    AVMetadata *metadata;
F
Fabrice Bellard 已提交
699 700 701 702 703 704 705
} AVFormatContext;

typedef struct AVPacketList {
    AVPacket pkt;
    struct AVPacketList *next;
} AVPacketList;

706
#if LIBAVFORMAT_VERSION_INT < (53<<16)
707 708
extern AVInputFormat *first_iformat;
extern AVOutputFormat *first_oformat;
709 710
#endif

711 712
/**
 * If f is NULL, returns the first registered input format,
713
 * if f is non-NULL, returns the next registered input format after f
714 715
 * or NULL if f is the last one.
 */
716
AVInputFormat  *av_iformat_next(AVInputFormat  *f);
717 718 719

/**
 * If f is NULL, returns the first registered output format,
720
 * if f is non-NULL, returns the next registered output format after f
721 722
 * or NULL if f is the last one.
 */
723
AVOutputFormat *av_oformat_next(AVOutputFormat *f);
F
Fabrice Bellard 已提交
724

725
enum CodecID av_guess_image2_codec(const char *filename);
726

727 728
/* XXX: Use automatic init with either ELF sections or C file parser */
/* modules. */
F
Fabrice Bellard 已提交
729

730 731 732
/* utils.c */
void av_register_input_format(AVInputFormat *format);
void av_register_output_format(AVOutputFormat *format);
733
AVOutputFormat *guess_stream_format(const char *short_name,
734 735
                                    const char *filename,
                                    const char *mime_type);
736
AVOutputFormat *guess_format(const char *short_name,
737 738
                             const char *filename,
                             const char *mime_type);
739 740

/**
D
Diego Biurrun 已提交
741
 * Guesses the codec ID based upon muxer and filename.
742
 */
743
enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
744 745
                            const char *filename, const char *mime_type,
                            enum CodecType type);
F
Fabrice Bellard 已提交
746

747
/**
748 749 750
 * Send a nice hexadecimal dump of a buffer to the specified file stream.
 *
 * @param f The file stream pointer where the dump should be sent to.
751 752
 * @param buf buffer
 * @param size buffer size
753 754
 *
 * @see av_hex_dump_log, av_pkt_dump, av_pkt_dump_log
755
 */
756
void av_hex_dump(FILE *f, uint8_t *buf, int size);
757 758

/**
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
 * Send a nice hexadecimal dump of a buffer to the log.
 *
 * @param avcl A pointer to an arbitrary struct of which the first field is a
 * pointer to an AVClass struct.
 * @param level The importance level of the message, lower values signifying
 * higher importance.
 * @param buf buffer
 * @param size buffer size
 *
 * @see av_hex_dump, av_pkt_dump, av_pkt_dump_log
 */
void av_hex_dump_log(void *avcl, int level, uint8_t *buf, int size);

/**
 * Send a nice dump of a packet to the specified file stream.
 *
 * @param f The file stream pointer where the dump should be sent to.
776
 * @param pkt packet to dump
D
Diego Biurrun 已提交
777
 * @param dump_payload True if the payload must be displayed, too.
778
 */
779
void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
F
Fabrice Bellard 已提交
780

781 782 783 784 785 786 787 788
/**
 * Send a nice dump of a packet to the log.
 *
 * @param avcl A pointer to an arbitrary struct of which the first field is a
 * pointer to an AVClass struct.
 * @param level The importance level of the message, lower values signifying
 * higher importance.
 * @param pkt packet to dump
D
Diego Biurrun 已提交
789
 * @param dump_payload True if the payload must be displayed, too.
790 791 792
 */
void av_pkt_dump_log(void *avcl, int level, AVPacket *pkt, int dump_payload);

793 794 795 796 797 798 799
/**
 * Initialize libavformat and register all the muxers, demuxers and
 * protocols. If you do not call this function, then you can select
 * exactly which formats you want to support.
 *
 * @see av_register_input_format()
 * @see av_register_output_format()
800
 * @see av_register_protocol()
801
 */
F
Fabrice Bellard 已提交
802
void av_register_all(void);
F
Fabrice Bellard 已提交
803

804
/** codec tag <-> codec id */
805 806
enum CodecID av_codec_get_id(const struct AVCodecTag * const *tags, unsigned int tag);
unsigned int av_codec_get_tag(const struct AVCodecTag * const *tags, enum CodecID id);
807

808
/* media file input */
809 810

/**
D
Diego Biurrun 已提交
811
 * Finds AVInputFormat based on the short name of the input format.
812
 */
813
AVInputFormat *av_find_input_format(const char *short_name);
814 815 816 817

/**
 * Guess file format.
 *
D
Diego Biurrun 已提交
818 819
 * @param is_opened Whether the file is already opened; determines whether
 *                  demuxers with or without AVFMT_NOFILE are probed.
820
 */
F
Fabrice Bellard 已提交
821
AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
822 823 824 825 826

/**
 * Allocates all the structures needed to read an input stream.
 *        This does not open the needed codecs for decoding the stream[s].
 */
827 828
int av_open_input_stream(AVFormatContext **ic_ptr,
                         ByteIOContext *pb, const char *filename,
829
                         AVInputFormat *fmt, AVFormatParameters *ap);
830 831

/**
R
Typos  
Ramiro Polla 已提交
832
 * Open a media file as input. The codecs are not opened. Only the file
833 834
 * header (if present) is read.
 *
D
Diego Biurrun 已提交
835 836 837
 * @param ic_ptr The opened media file handle is put here.
 * @param filename filename to open
 * @param fmt If non-NULL, force the file format to use.
838
 * @param buf_size optional buffer size (zero if default is OK)
839 840
 * @param ap Additional parameters needed when opening the file
 *           (NULL if default).
D
Diego Biurrun 已提交
841
 * @return 0 if OK, AVERROR_xxx otherwise
842
 */
843
int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
844 845 846
                       AVInputFormat *fmt,
                       int buf_size,
                       AVFormatParameters *ap);
847 848 849 850 851 852 853 854

#if LIBAVFORMAT_VERSION_MAJOR < 53
/**
 * @deprecated Use avformat_alloc_context() instead.
 */
attribute_deprecated AVFormatContext *av_alloc_format_context(void);
#endif

855 856
/**
 * Allocate an AVFormatContext.
D
Diego Biurrun 已提交
857
 * Can be freed with av_free() but do not forget to free everything you
858 859
 * explicitly allocated as well!
 */
860
AVFormatContext *avformat_alloc_context(void);
861

862 863 864
/**
 * Read packets of a media file to get stream information. This
 * is useful for file formats with no headers such as MPEG. This
865
 * function also computes the real framerate in case of MPEG-2 repeat
866 867 868 869 870
 * frame mode.
 * The logical file position is not changed by this function;
 * examined packets may be buffered for later processing.
 *
 * @param ic media file handle
D
Diego Biurrun 已提交
871 872 873
 * @return >=0 if OK, AVERROR_xxx on error
 * @todo Let the user decide somehow what information is needed so that
 *       we do not waste time getting stuff the user does not need.
874
 */
875
int av_find_stream_info(AVFormatContext *ic);
876 877 878 879

/**
 * Read a transport packet from a media file.
 *
R
Typos  
Ramiro Polla 已提交
880
 * This function is obsolete and should never be used.
881 882 883 884
 * Use av_read_frame() instead.
 *
 * @param s media file handle
 * @param pkt is filled
D
Diego Biurrun 已提交
885
 * @return 0 if OK, AVERROR_xxx on error
886
 */
F
Fabrice Bellard 已提交
887
int av_read_packet(AVFormatContext *s, AVPacket *pkt);
888 889 890 891 892 893 894 895 896 897 898 899 900

/**
 * Return the next frame of a stream.
 *
 * The returned packet is valid
 * until the next av_read_frame() or until av_close_input_file() and
 * must be freed with av_free_packet. For video, the packet contains
 * exactly one frame. For audio, it contains an integer number of
 * frames if each frame has a known fixed size (e.g. PCM or ADPCM
 * data). If the audio frames have a variable size (e.g. MPEG audio),
 * then it contains one frame.
 *
 * pkt->pts, pkt->dts and pkt->duration are always set to correct
901
 * values in AVStream.time_base units (and guessed if the format cannot
D
Diego Biurrun 已提交
902 903
 * provide them). pkt->pts can be AV_NOPTS_VALUE if the video format
 * has B-frames, so it is better to rely on pkt->dts if you do not
904 905
 * decompress the payload.
 *
D
Diego Biurrun 已提交
906
 * @return 0 if OK, < 0 on error or end of file
907
 */
908
int av_read_frame(AVFormatContext *s, AVPacket *pkt);
909 910

/**
911
 * Seek to the keyframe at timestamp.
912 913 914 915
 * 'timestamp' in 'stream_index'.
 * @param stream_index If stream_index is (-1), a default
 * stream is selected, and timestamp is automatically converted
 * from AV_TIME_BASE units to the stream specific time_base.
D
Diego Biurrun 已提交
916 917
 * @param timestamp Timestamp in AVStream.time_base units
 *        or, if no stream is specified, in AV_TIME_BASE units.
918 919 920
 * @param flags flags which select direction and seeking mode
 * @return >= 0 on success
 */
921 922
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp,
                  int flags);
923

924 925 926 927 928 929
/**
 * Seek to timestamp ts.
 * Seeking will be done so that the point from which all active streams
 * can be presented successfully will be closest to ts and within min/max_ts.
 * Active streams are all streams that have AVStream.discard < AVDISCARD_ALL.
 *
930
 * If flags contain AVSEEK_FLAG_BYTE, then all timestamps are in bytes and
931
 * are the file position (this may not be supported by all demuxers).
932
 * If flags contain AVSEEK_FLAG_FRAME, then all timestamps are in frames
933
 * in the stream with stream_index (this may not be supported by all demuxers).
934 935 936
 * Otherwise all timestamps are in units of the stream selected by stream_index
 * or if stream_index is -1, in AV_TIME_BASE units.
 * If flags contain AVSEEK_FLAG_ANY, then non-keyframes are treated as
937 938
 * keyframes (this may not be supported by all demuxers).
 *
939
 * @param stream_index index of the stream which is used as time base reference
940 941 942 943 944
 * @param min_ts smallest acceptable timestamp
 * @param ts target timestamp
 * @param max_ts largest acceptable timestamp
 * @param flags flags
 * @returns >=0 on success, error code otherwise
945
 *
946 947 948
 * @NOTE This is part of the new seek API which is still under construction.
 *       Thus do not use this yet. It may change at any time, do not expect
 *       ABI compatibility yet!
949 950 951
 */
int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags);

952
/**
953
 * Start playing a network-based stream (e.g. RTSP stream) at the
D
Diego Biurrun 已提交
954
 * current position.
955
 */
956
int av_read_play(AVFormatContext *s);
957 958

/**
959
 * Pause a network-based stream (e.g. RTSP stream).
960 961 962
 *
 * Use av_read_play() to resume it.
 */
963
int av_read_pause(AVFormatContext *s);
964

965 966 967 968 969 970
/**
 * Free a AVFormatContext allocated by av_open_input_stream.
 * @param s context to free
 */
void av_close_input_stream(AVFormatContext *s);

971 972 973 974 975
/**
 * Close a media file (but not its codecs).
 *
 * @param s media file handle
 */
F
Fabrice Bellard 已提交
976
void av_close_input_file(AVFormatContext *s);
977 978 979 980 981 982 983 984 985

/**
 * Add a new stream to a media file.
 *
 * Can only be called in the read_header() function. If the flag
 * AVFMTCTX_NOHEADER is in the format context, then new streams
 * can be added in read_packet too.
 *
 * @param s media file handle
D
Diego Biurrun 已提交
986
 * @param id file-format-dependent stream ID
987
 */
988
AVStream *av_new_stream(AVFormatContext *s, int id);
989
AVProgram *av_new_program(AVFormatContext *s, int id);
990

991 992 993
/**
 * Add a new chapter.
 * This function is NOT part of the public API
D
Diego Biurrun 已提交
994
 * and should ONLY be used by demuxers.
995 996
 *
 * @param s media file handle
D
Diego Biurrun 已提交
997
 * @param id unique ID for this chapter
998 999
 * @param start chapter start time in time_base units
 * @param end chapter end time in time_base units
1000
 * @param title chapter title
1001
 *
D
Diego Biurrun 已提交
1002
 * @return AVChapter or NULL on error
1003
 */
1004 1005
AVChapter *ff_new_chapter(AVFormatContext *s, int id, AVRational time_base,
                          int64_t start, int64_t end, const char *title);
1006

1007 1008 1009 1010 1011 1012 1013 1014 1015
/**
 * Set the pts for a given stream.
 *
 * @param s stream
 * @param pts_wrap_bits number of bits effectively used by the pts
 *        (used for wrap control, 33 is the value for MPEG)
 * @param pts_num numerator to convert to seconds (MPEG: 1)
 * @param pts_den denominator to convert to seconds (MPEG: 90000)
 */
1016
void av_set_pts_info(AVStream *s, int pts_wrap_bits,
F
Fabrice Bellard 已提交
1017
                     int pts_num, int pts_den);
F
Fabrice Bellard 已提交
1018

1019 1020
#define AVSEEK_FLAG_BACKWARD 1 ///< seek backward
#define AVSEEK_FLAG_BYTE     2 ///< seeking based on position in bytes
D
Diego Biurrun 已提交
1021
#define AVSEEK_FLAG_ANY      4 ///< seek to any frame, even non-keyframes
1022

1023
int av_find_default_stream_index(AVFormatContext *s);
1024 1025 1026

/**
 * Gets the index for a specific timestamp.
D
Diego Biurrun 已提交
1027 1028 1029
 * @param flags if AVSEEK_FLAG_BACKWARD then the returned index will correspond
 *                 to the timestamp which is <= the requested one, if backward
 *                 is 0, then it will be >=
1030 1031 1032
 *              if AVSEEK_FLAG_ANY seek to any frame, only keyframes otherwise
 * @return < 0 if no such timestamp could be found
 */
1033
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags);
1034

1035 1036
/**
 * Ensures the index uses less memory than the maximum specified in
1037
 * AVFormatContext.max_index_size by discarding entries if it grows
1038 1039 1040 1041 1042 1043
 * too large.
 * This function is not part of the public API and should only be called
 * by demuxers.
 */
void ff_reduce_index(AVFormatContext *s, int stream_index);

1044
/**
D
Diego Biurrun 已提交
1045 1046
 * Add an index entry into a sorted list. Update the entry if the list
 * already contains it.
1047
 *
D
Diego Biurrun 已提交
1048
 * @param timestamp timestamp in the time base of the given stream
1049
 */
1050 1051
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
                       int size, int distance, int flags);
1052 1053

/**
1054 1055 1056 1057
 * Does a binary search using av_index_search_timestamp() and
 * AVCodec.read_timestamp().
 * This is not supposed to be called directly by a user application,
 * but by demuxers.
1058 1059 1060
 * @param target_ts target timestamp in the time base of the given stream
 * @param stream_index stream number
 */
1061 1062
int av_seek_frame_binary(AVFormatContext *s, int stream_index,
                         int64_t target_ts, int flags);
1063 1064

/**
D
Diego Biurrun 已提交
1065
 * Updates cur_dts of all streams based on the given timestamp and AVStream.
1066
 *
D
Diego Biurrun 已提交
1067 1068
 * Stream ref_st unchanged, others set cur_dts in their native time base.
 * Only needed for timestamp wrapping or if (dts not set and pts!=dts).
1069 1070 1071
 * @param timestamp new dts expressed in time_base of param ref_st
 * @param ref_st reference stream giving time_base of param timestamp
 */
1072
void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp);
1073 1074 1075

/**
 * Does a binary search using read_timestamp().
1076 1077
 * This is not supposed to be called directly by a user application,
 * but by demuxers.
1078 1079 1080
 * @param target_ts target timestamp in the time base of the given stream
 * @param stream_index stream number
 */
1081 1082 1083 1084 1085 1086
int64_t av_gen_search(AVFormatContext *s, int stream_index,
                      int64_t target_ts, int64_t pos_min,
                      int64_t pos_max, int64_t pos_limit,
                      int64_t ts_min, int64_t ts_max,
                      int flags, int64_t *ts_ret,
                      int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ));
1087

1088
/** media file output */
1089
int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
1090 1091

/**
D
Diego Biurrun 已提交
1092 1093
 * Allocate the stream private data and write the stream header to an
 * output media file.
1094 1095
 *
 * @param s media file handle
D
Diego Biurrun 已提交
1096
 * @return 0 if OK, AVERROR_xxx on error
1097
 */
1098
int av_write_header(AVFormatContext *s);
1099 1100 1101 1102 1103

/**
 * Write a packet to an output media file.
 *
 * The packet shall contain one audio or video frame.
1104 1105
 * The packet must be correctly interleaved according to the container
 * specification, if not then av_interleaved_write_frame must be used.
1106 1107
 *
 * @param s media file handle
1108 1109
 * @param pkt The packet, which contains the stream_index, buf/buf_size,
              dts/pts, ...
D
Diego Biurrun 已提交
1110
 * @return < 0 on error, = 0 if OK, 1 if end of stream wanted
1111
 */
1112
int av_write_frame(AVFormatContext *s, AVPacket *pkt);
1113 1114 1115 1116 1117

/**
 * Writes a packet to an output media file ensuring correct interleaving.
 *
 * The packet must contain one audio or video frame.
1118
 * If the packets are already correctly interleaved, the application should
D
Diego Biurrun 已提交
1119 1120 1121 1122
 * call av_write_frame() instead as it is slightly faster. It is also important
 * to keep in mind that completely non-interleaved input will need huge amounts
 * of memory to interleave with this, so it is preferable to interleave at the
 * demuxer level.
1123 1124
 *
 * @param s media file handle
1125 1126
 * @param pkt The packet, which contains the stream_index, buf/buf_size,
              dts/pts, ...
D
Diego Biurrun 已提交
1127
 * @return < 0 on error, = 0 if OK, 1 if end of stream wanted
1128
 */
1129
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
1130 1131

/**
D
Diego Biurrun 已提交
1132
 * Interleave a packet per dts in an output media file.
1133
 *
1134
 * Packets with pkt->destruct == av_destruct_packet will be freed inside this
1135
 * function, so they cannot be used after it. Note that calling av_free_packet()
1136
 * on them is still safe.
1137 1138 1139 1140 1141 1142 1143
 *
 * @param s media file handle
 * @param out the interleaved packet will be output here
 * @param in the input packet
 * @param flush 1 if no further packets are available as input and all
 *              remaining packets should be output
 * @return 1 if a packet was output, 0 if no packet could be output,
D
Diego Biurrun 已提交
1144
 *         < 0 if an error occurred
1145
 */
1146 1147
int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
                                 AVPacket *pkt, int flush);
1148

1149 1150 1151 1152
/**
 * @brief Write the stream trailer to an output media file and
 *        free the file private data.
 *
1153 1154
 * May only be called after a successful call to av_write_header.
 *
1155
 * @param s media file handle
D
Diego Biurrun 已提交
1156
 * @return 0 if OK, AVERROR_xxx on error
1157
 */
1158
int av_write_trailer(AVFormatContext *s);
F
Fabrice Bellard 已提交
1159 1160

void dump_format(AVFormatContext *ic,
1161
                 int index,
F
Fabrice Bellard 已提交
1162 1163
                 const char *url,
                 int is_output);
1164

1165
#if LIBAVFORMAT_VERSION_MAJOR < 53
1166
/**
D
Diego Biurrun 已提交
1167
 * Parses width and height out of string str.
1168
 * @deprecated Use av_parse_video_frame_size instead.
1169
 */
1170 1171
attribute_deprecated int parse_image_size(int *width_ptr, int *height_ptr,
                                          const char *str);
1172 1173

/**
1174
 * Converts framerate from a string to a fraction.
1175
 * @deprecated Use av_parse_video_frame_rate instead.
1176
 */
1177 1178
attribute_deprecated int parse_frame_rate(int *frame_rate, int *frame_rate_base,
                                          const char *arg);
1179
#endif
1180 1181

/**
1182 1183 1184
 * Parses \p datestr and returns a corresponding number of microseconds.
 * @param datestr String representing a date or a duration.
 * - If a date the syntax is:
1185 1186 1187
 * @code
 *  [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]}
 * @endcode
D
Diego Biurrun 已提交
1188
 * Time is local time unless Z is appended, in which case it is
1189
 * interpreted as UTC.
D
Diego Biurrun 已提交
1190
 * If the year-month-day part is not specified it takes the current
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
 * year-month-day.
 * Returns the number of microseconds since 1st of January, 1970 up to
 * the time of the parsed date or INT64_MIN if \p datestr cannot be
 * successfully parsed.
 * - If a duration the syntax is:
 * @code
 *  [-]HH[:MM[:SS[.m...]]]
 *  [-]S+[.m...]
 * @endcode
 * Returns the number of microseconds contained in a time interval
 * with the specified duration or INT64_MIN if \p datestr cannot be
D
Diego Biurrun 已提交
1202
 * successfully parsed.
1203 1204 1205
 * @param duration Flag which tells how to interpret \p datestr, if
 * not zero \p datestr is interpreted as a duration, otherwise as a
 * date.
1206
 */
1207
int64_t parse_date(const char *datestr, int duration);
F
Fabrice Bellard 已提交
1208

1209
/** Gets the current time in microseconds. */
1210
int64_t av_gettime(void);
F
Fabrice Bellard 已提交
1211

D
Diego Biurrun 已提交
1212
/* ffm-specific for ffserver */
F
Fabrice Bellard 已提交
1213
#define FFM_PACKET_SIZE 4096
1214
int64_t ffm_read_write_index(int fd);
1215
int ffm_write_write_index(int fd, int64_t pos);
1216
void ffm_set_write_index(AVFormatContext *s, int64_t pos, int64_t file_size);
F
Fabrice Bellard 已提交
1217

1218 1219 1220 1221 1222 1223
/**
 * Attempts to find a specific tag in a URL.
 *
 * syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done.
 * Return 1 if found.
 */
F
Fabrice Bellard 已提交
1224 1225
int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);

1226
/**
1227
 * Returns in 'buf' the path with '%d' replaced by a number.
D
Diego Biurrun 已提交
1228
 *
1229 1230 1231 1232 1233 1234
 * Also handles the '%0nd' format where 'n' is the total number
 * of digits and '%%'.
 *
 * @param buf destination buffer
 * @param buf_size destination buffer size
 * @param path numbered sequence string
P
Panagiotis Issaris 已提交
1235
 * @param number frame number
D
Diego Biurrun 已提交
1236
 * @return 0 if OK, -1 on format error
1237
 */
1238 1239
int av_get_frame_filename(char *buf, int buf_size,
                          const char *path, int number);
1240 1241 1242 1243 1244

/**
 * Check whether filename actually is a numbered sequence generator.
 *
 * @param filename possible numbered sequence string
D
Diego Biurrun 已提交
1245
 * @return 1 if a valid numbered sequence string, 0 otherwise
1246
 */
1247
int av_filename_number_test(const char *filename);
1248

L
Luca Abeni 已提交
1249 1250 1251 1252 1253 1254 1255
/**
 * Generate an SDP for an RTP session.
 *
 * @param ac array of AVFormatContexts describing the RTP streams. If the
 *           array is composed by only one context, such context can contain
 *           multiple AVStreams (one AVStream per RTP stream). Otherwise,
 *           all the contexts in the array (an AVCodecContext per RTP stream)
D
Diego Biurrun 已提交
1256
 *           must contain only one AVStream.
1257 1258
 * @param n_files number of AVCodecContexts contained in ac
 * @param buff buffer where the SDP will be stored (must be allocated by
D
Diego Biurrun 已提交
1259
 *             the caller)
1260
 * @param size the size of the buffer
D
Diego Biurrun 已提交
1261
 * @return 0 if OK, AVERROR_xxx on error
L
Luca Abeni 已提交
1262
 */
1263
int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size);
L
Luca Abeni 已提交
1264

F
Fabrice Bellard 已提交
1265
#ifdef HAVE_AV_CONFIG_H
1266

1267
void ff_dynarray_add(intptr_t **tab_ptr, int *nb_ptr, intptr_t elem);
F
Fabrice Bellard 已提交
1268

F
Falk Hüffner 已提交
1269
#ifdef __GNUC__
F
Fabrice Bellard 已提交
1270 1271
#define dynarray_add(tab, nb_ptr, elem)\
do {\
1272 1273
    __typeof__(tab) _tab = (tab);\
    __typeof__(elem) _elem = (elem);\
F
Fabrice Bellard 已提交
1274
    (void)sizeof(**_tab == _elem); /* check that types are compatible */\
1275
    ff_dynarray_add((intptr_t **)_tab, nb_ptr, (intptr_t)_elem);\
F
Fabrice Bellard 已提交
1276
} while(0)
F
Falk Hüffner 已提交
1277 1278 1279
#else
#define dynarray_add(tab, nb_ptr, elem)\
do {\
1280
    ff_dynarray_add((intptr_t **)(tab), nb_ptr, (intptr_t)(elem));\
F
Falk Hüffner 已提交
1281 1282
} while(0)
#endif
F
Fabrice Bellard 已提交
1283

1284
time_t mktimegm(struct tm *tm);
1285
struct tm *brktimegm(time_t secs, struct tm *tm);
1286
const char *small_strptime(const char *p, const char *fmt,
1287 1288
                           struct tm *dt);

F
Fabrice Bellard 已提交
1289 1290 1291 1292
struct in_addr;
int resolve_host(struct in_addr *sin_addr, const char *hostname);

void url_split(char *proto, int proto_size,
1293
               char *authorization, int authorization_size,
F
Fabrice Bellard 已提交
1294 1295 1296 1297 1298
               char *hostname, int hostname_size,
               int *port_ptr,
               char *path, int path_size,
               const char *url);

F
Fabrice Bellard 已提交
1299 1300
int match_ext(const char *filename, const char *extensions);

F
Fabrice Bellard 已提交
1301 1302
#endif /* HAVE_AV_CONFIG_H */

1303
#endif /* AVFORMAT_AVFORMAT_H */