]> git.sesse.net Git - ffmpeg/blob - libavformat/avformat.h
Try to clarify the semantics of AVPacket.duration.
[ffmpeg] / libavformat / avformat.h
1 /*
2  * copyright (c) 2001 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * 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  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * 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  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #ifndef AVFORMAT_AVFORMAT_H
22 #define AVFORMAT_AVFORMAT_H
23
24 #define LIBAVFORMAT_VERSION_MAJOR 52
25 #define LIBAVFORMAT_VERSION_MINOR 21
26 #define LIBAVFORMAT_VERSION_MICRO  0
27
28 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
29                                                LIBAVFORMAT_VERSION_MINOR, \
30                                                LIBAVFORMAT_VERSION_MICRO)
31 #define LIBAVFORMAT_VERSION     AV_VERSION(LIBAVFORMAT_VERSION_MAJOR,   \
32                                            LIBAVFORMAT_VERSION_MINOR,   \
33                                            LIBAVFORMAT_VERSION_MICRO)
34 #define LIBAVFORMAT_BUILD       LIBAVFORMAT_VERSION_INT
35
36 #define LIBAVFORMAT_IDENT       "Lavf" AV_STRINGIFY(LIBAVFORMAT_VERSION)
37
38 /**
39  * Returns the LIBAVFORMAT_VERSION_INT constant.
40  */
41 unsigned avformat_version(void);
42
43 #include <time.h>
44 #include <stdio.h>  /* FILE */
45 #include "libavcodec/avcodec.h"
46
47 #include "avio.h"
48
49 /* packet functions */
50
51 typedef struct AVPacket {
52     /**
53      * Presentation time stamp in time_base units.
54      * This is the time at which the decompressed packet will be presented
55      * to the user.
56      * Can be AV_NOPTS_VALUE if it is not stored in the file.
57      * pts MUST be larger or equal to dts as presentation can not happen before
58      * decompression, unless one wants to view hex dumps. Some formats misuse
59      * the terms dts and pts/cts to mean something different, these timestamps
60      * must be converted to true pts/dts before they are stored in AVPacket.
61      */
62     int64_t pts;
63     /**
64      * Decompression time stamp in time_base units.
65      * This is the time at which the packet is decompressed.
66      * Can be AV_NOPTS_VALUE if it is not stored in the file.
67      */
68     int64_t dts;
69     uint8_t *data;
70     int   size;
71     int   stream_index;
72     int   flags;
73     /**
74      * Duration of this packet in time_base units, 0 if unknown.
75      * Equals next_pts - this_pts in presentation order.
76      */
77     int   duration;
78     void  (*destruct)(struct AVPacket *);
79     void  *priv;
80     int64_t pos;                            ///< byte position in stream, -1 if unknown
81 } AVPacket;
82 #define PKT_FLAG_KEY   0x0001
83
84 void av_destruct_packet_nofree(AVPacket *pkt);
85
86 /**
87  * Default packet destructor.
88  */
89 void av_destruct_packet(AVPacket *pkt);
90
91 /**
92  * Initialize optional fields of a packet to default values.
93  *
94  * @param pkt packet
95  */
96 void av_init_packet(AVPacket *pkt);
97
98 /**
99  * Allocate the payload of a packet and initialize its fields to default values.
100  *
101  * @param pkt packet
102  * @param size wanted payload size
103  * @return 0 if OK. AVERROR_xxx otherwise.
104  */
105 int av_new_packet(AVPacket *pkt, int size);
106
107 /**
108  * Allocate and read the payload of a packet and initialize its fields to default values.
109  *
110  * @param pkt packet
111  * @param size wanted payload size
112  * @return >0 (read size) if OK. AVERROR_xxx otherwise.
113  */
114 int av_get_packet(ByteIOContext *s, AVPacket *pkt, int size);
115
116 /**
117  * @warning This is a hack - the packet memory allocation stuff is broken. The
118  * packet is allocated if it was not really allocated
119  */
120 int av_dup_packet(AVPacket *pkt);
121
122 /**
123  * Free a packet
124  *
125  * @param pkt packet to free
126  */
127 static inline void av_free_packet(AVPacket *pkt)
128 {
129     if (pkt && pkt->destruct) {
130         pkt->destruct(pkt);
131     }
132 }
133
134 /*************************************************/
135 /* fractional numbers for exact pts handling */
136
137 /**
138  * the exact value of the fractional number is: 'val + num / den'.
139  * num is assumed to be such as 0 <= num < den
140  * @deprecated Use AVRational instead
141 */
142 typedef struct AVFrac {
143     int64_t val, num, den;
144 } AVFrac attribute_deprecated;
145
146 /*************************************************/
147 /* input/output formats */
148
149 struct AVCodecTag;
150
151 struct AVFormatContext;
152
153 /** this structure contains the data a format has to probe a file */
154 typedef struct AVProbeData {
155     const char *filename;
156     unsigned char *buf;
157     int buf_size;
158 } AVProbeData;
159
160 #define AVPROBE_SCORE_MAX 100               ///< max score, half of that is used for file extension based detection
161 #define AVPROBE_PADDING_SIZE 32             ///< extra allocated bytes at the end of the probe buffer
162
163 typedef struct AVFormatParameters {
164     AVRational time_base;
165     int sample_rate;
166     int channels;
167     int width;
168     int height;
169     enum PixelFormat pix_fmt;
170     int channel; /**< used to select dv channel */
171     const char *standard; /**< tv standard, NTSC, PAL, SECAM */
172     unsigned int mpeg2ts_raw:1;  /**< force raw MPEG2 transport stream output, if possible */
173     unsigned int mpeg2ts_compute_pcr:1; /**< compute exact PCR for each transport
174                                             stream packet (only meaningful if
175                                             mpeg2ts_raw is TRUE) */
176     unsigned int initial_pause:1;       /**< do not begin to play the stream
177                                             immediately (RTSP only) */
178     unsigned int prealloced_context:1;
179 #if LIBAVFORMAT_VERSION_INT < (53<<16)
180     enum CodecID video_codec_id;
181     enum CodecID audio_codec_id;
182 #endif
183 } AVFormatParameters;
184
185 //! demuxer will use url_fopen, no opened file should be provided by the caller
186 #define AVFMT_NOFILE        0x0001
187 #define AVFMT_NEEDNUMBER    0x0002 /**< needs '%d' in filename */
188 #define AVFMT_SHOW_IDS      0x0008 /**< show format stream IDs numbers */
189 #define AVFMT_RAWPICTURE    0x0020 /**< format wants AVPicture structure for
190                                       raw picture data */
191 #define AVFMT_GLOBALHEADER  0x0040 /**< format wants global header */
192 #define AVFMT_NOTIMESTAMPS  0x0080 /**< format does not need / have any timestamps */
193 #define AVFMT_GENERIC_INDEX 0x0100 /**< use generic index building code */
194 #define AVFMT_TS_DISCONT    0x0200 /**< format allows timestamo discontinuities */
195
196 typedef struct AVOutputFormat {
197     const char *name;
198     /**
199      * Descriptive name for the format, meant to be more human-readable
200      * than \p name. You \e should use the NULL_IF_CONFIG_SMALL() macro
201      * to define it.
202      */
203     const char *long_name;
204     const char *mime_type;
205     const char *extensions; /**< comma separated filename extensions */
206     /** size of private data so that it can be allocated in the wrapper */
207     int priv_data_size;
208     /* output support */
209     enum CodecID audio_codec; /**< default audio codec */
210     enum CodecID video_codec; /**< default video codec */
211     int (*write_header)(struct AVFormatContext *);
212     int (*write_packet)(struct AVFormatContext *, AVPacket *pkt);
213     int (*write_trailer)(struct AVFormatContext *);
214     /** can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER */
215     int flags;
216     /** currently only used to set pixel format if not YUV420P */
217     int (*set_parameters)(struct AVFormatContext *, AVFormatParameters *);
218     int (*interleave_packet)(struct AVFormatContext *, AVPacket *out, AVPacket *in, int flush);
219
220     /**
221      * list of supported codec_id-codec_tag pairs, ordered by "better choice first"
222      * the arrays are all CODEC_ID_NONE terminated
223      */
224     const struct AVCodecTag * const *codec_tag;
225
226     enum CodecID subtitle_codec; /**< default subtitle codec */
227
228     /* private fields */
229     struct AVOutputFormat *next;
230 } AVOutputFormat;
231
232 typedef struct AVInputFormat {
233     const char *name;
234     /**
235      * Descriptive name for the format, meant to be more human-readable
236      * than \p name. You \e should use the NULL_IF_CONFIG_SMALL() macro
237      * to define it.
238      */
239     const char *long_name;
240     /** size of private data so that it can be allocated in the wrapper */
241     int priv_data_size;
242     /**
243      * Tell if a given file has a chance of being parsed by this format.
244      * The buffer provided is guaranteed to be AVPROBE_PADDING_SIZE bytes
245      * big so you do not have to check for that unless you need more.
246      */
247     int (*read_probe)(AVProbeData *);
248     /** read the format header and initialize the AVFormatContext
249        structure. Return 0 if OK. 'ap' if non NULL contains
250        additional paramters. Only used in raw format right
251        now. 'av_new_stream' should be called to create new streams.  */
252     int (*read_header)(struct AVFormatContext *,
253                        AVFormatParameters *ap);
254     /** read one packet and put it in 'pkt'. pts and flags are also
255        set. 'av_new_stream' can be called only if the flag
256        AVFMTCTX_NOHEADER is used. */
257     int (*read_packet)(struct AVFormatContext *, AVPacket *pkt);
258     /** close the stream. The AVFormatContext and AVStreams are not
259        freed by this function */
260     int (*read_close)(struct AVFormatContext *);
261     /**
262      * seek to a given timestamp relative to the frames in
263      * stream component stream_index
264      * @param stream_index must not be -1
265      * @param flags selects which direction should be preferred if no exact
266      *              match is available
267      * @return >= 0 on success (but not necessarily the new offset)
268      */
269     int (*read_seek)(struct AVFormatContext *,
270                      int stream_index, int64_t timestamp, int flags);
271     /**
272      * gets the next timestamp in stream[stream_index].time_base units.
273      * @return the timestamp or AV_NOPTS_VALUE if an error occurred
274      */
275     int64_t (*read_timestamp)(struct AVFormatContext *s, int stream_index,
276                               int64_t *pos, int64_t pos_limit);
277     /** can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER */
278     int flags;
279     /** if extensions are defined, then no probe is done. You should
280        usually not use extension format guessing because it is not
281        reliable enough */
282     const char *extensions;
283     /** general purpose read only value that the format can use */
284     int value;
285
286     /** start/resume playing - only meaningful if using a network based format
287        (RTSP) */
288     int (*read_play)(struct AVFormatContext *);
289
290     /** pause playing - only meaningful if using a network based format
291        (RTSP) */
292     int (*read_pause)(struct AVFormatContext *);
293
294     const struct AVCodecTag * const *codec_tag;
295
296     /* private fields */
297     struct AVInputFormat *next;
298 } AVInputFormat;
299
300 enum AVStreamParseType {
301     AVSTREAM_PARSE_NONE,
302     AVSTREAM_PARSE_FULL,       /**< full parsing and repack */
303     AVSTREAM_PARSE_HEADERS,    /**< only parse headers, don't repack */
304     AVSTREAM_PARSE_TIMESTAMPS, /**< full parsing and interpolation of timestamps for frames not starting on packet boundary */
305 };
306
307 typedef struct AVIndexEntry {
308     int64_t pos;
309     int64_t timestamp;
310 #define AVINDEX_KEYFRAME 0x0001
311     int flags:2;
312     int size:30; //Yeah, trying to keep the size of this small to reduce memory requirements (it is 24 vs 32 byte due to possible 8byte align).
313     int min_distance;         /**< min distance between this and the previous keyframe, used to avoid unneeded searching */
314 } AVIndexEntry;
315
316 #define AV_DISPOSITION_DEFAULT   0x0001
317 #define AV_DISPOSITION_DUB       0x0002
318 #define AV_DISPOSITION_ORIGINAL  0x0004
319 #define AV_DISPOSITION_COMMENT   0x0008
320 #define AV_DISPOSITION_LYRICS    0x0010
321 #define AV_DISPOSITION_KARAOKE   0x0020
322
323 /**
324  * Stream structure.
325  * New fields can be added to the end with minor version bumps.
326  * Removal, reordering and changes to existing fields require a major
327  * version bump.
328  * sizeof(AVStream) must not be used outside libav*.
329  */
330 typedef struct AVStream {
331     int index;    /**< stream index in AVFormatContext */
332     int id;       /**< format specific stream id */
333     AVCodecContext *codec; /**< codec context */
334     /**
335      * Real base frame rate of the stream.
336      * This is the lowest frame rate with which all timestamps can be
337      * represented accurately (it is the least common multiple of all
338      * frame rates in the stream), Note, this value is just a guess!
339      * For example if the timebase is 1/90000 and all frames have either
340      * approximately 3600 or 1800 timer ticks then r_frame_rate will be 50/1.
341      */
342     AVRational r_frame_rate;
343     void *priv_data;
344
345     /* internal data used in av_find_stream_info() */
346     int64_t first_dts;
347     /** encoding: PTS generation when outputing stream */
348     struct AVFrac pts;
349
350     /**
351      * This is the fundamental unit of time (in seconds) in terms
352      * of which frame timestamps are represented. For fixed-fps content,
353      * timebase should be 1/frame rate and timestamp increments should be
354      * identically 1.
355      */
356     AVRational time_base;
357     int pts_wrap_bits; /**< number of bits in pts (used for wrapping control) */
358     /* ffmpeg.c private use */
359     int stream_copy; /**< if set, just copy stream */
360     enum AVDiscard discard; ///< selects which packets can be discarded at will and do not need to be demuxed
361     //FIXME move stuff to a flags field?
362     /** quality, as it has been removed from AVCodecContext and put in AVVideoFrame
363      * MN: dunno if that is the right place for it */
364     float quality;
365     /**
366      * Decoding: pts of the first frame of the stream, in stream time base.
367      * Only set this if you are absolutely 100% sure that the value you set
368      * it to really is the pts of the first frame.
369      * This may be undefined (AV_NOPTS_VALUE).
370      * @note The ASF header does NOT contain a correct start_time the ASF
371      * demuxer must NOT set this.
372      */
373     int64_t start_time;
374     /**
375      * Decoding: duration of the stream, in stream time base.
376      * If a source file does not specify a duration, but does specify
377      * a bitrate, this value will be estimates from bit rate and file size.
378      */
379     int64_t duration;
380
381     char language[4]; /** ISO 639 3-letter language code (empty string if undefined) */
382
383     /* av_read_frame() support */
384     enum AVStreamParseType need_parsing;
385     struct AVCodecParserContext *parser;
386
387     int64_t cur_dts;
388     int last_IP_duration;
389     int64_t last_IP_pts;
390     /* av_seek_frame() support */
391     AVIndexEntry *index_entries; /**< only used if the format does not
392                                     support seeking natively */
393     int nb_index_entries;
394     unsigned int index_entries_allocated_size;
395
396     int64_t nb_frames;                 ///< number of frames in this stream if known or 0
397
398 #if LIBAVFORMAT_VERSION_INT < (53<<16)
399     int64_t unused[4+1];
400 #endif
401
402     char *filename; /**< source filename of the stream */
403
404     int disposition; /**< AV_DISPOSITION_* bitfield */
405
406     AVProbeData probe_data;
407 #define MAX_REORDER_DELAY 16
408     int64_t pts_buffer[MAX_REORDER_DELAY+1];
409
410     /**
411      * sample aspect ratio (0 if unknown)
412      * - encoding: Set by user.
413      * - decoding: Set by libavformat.
414      */
415     AVRational sample_aspect_ratio;
416 } AVStream;
417
418 #define AV_PROGRAM_RUNNING 1
419
420 /**
421  * New fields can be added to the end with minor version bumps.
422  * Removal, reordering and changes to existing fields require a major
423  * version bump.
424  * sizeof(AVProgram) must not be used outside libav*.
425  */
426 typedef struct AVProgram {
427     int            id;
428     char           *provider_name; ///< Network name for DVB streams
429     char           *name;          ///< Service name for DVB streams
430     int            flags;
431     enum AVDiscard discard;        ///< selects which program to discard and which to feed to the caller
432     unsigned int   *stream_index;
433     unsigned int   nb_stream_indexes;
434 } AVProgram;
435
436 #define AVFMTCTX_NOHEADER      0x0001 /**< signal that no header is present
437                                          (streams are added dynamically) */
438
439 typedef struct AVChapter {
440     int id;                 ///< Unique id to identify the chapter
441     AVRational time_base;   ///< Timebase in which the start/end timestamps are specified
442     int64_t start, end;     ///< chapter start/end time in time_base units
443     char *title;            ///< chapter title
444 } AVChapter;
445
446 #define MAX_STREAMS 20
447
448 /**
449  * format I/O context.
450  * New fields can be added to the end with minor version bumps.
451  * Removal, reordering and changes to existing fields require a major
452  * version bump.
453  * sizeof(AVFormatContext) must not be used outside libav*.
454  */
455 typedef struct AVFormatContext {
456     const AVClass *av_class; /**< set by av_alloc_format_context */
457     /* can only be iformat or oformat, not both at the same time */
458     struct AVInputFormat *iformat;
459     struct AVOutputFormat *oformat;
460     void *priv_data;
461     ByteIOContext *pb;
462     unsigned int nb_streams;
463     AVStream *streams[MAX_STREAMS];
464     char filename[1024]; /**< input or output filename */
465     /* stream info */
466     int64_t timestamp;
467     char title[512];
468     char author[512];
469     char copyright[512];
470     char comment[512];
471     char album[512];
472     int year;  /**< ID3 year, 0 if none */
473     int track; /**< track number, 0 if none */
474     char genre[32]; /**< ID3 genre */
475
476     int ctx_flags; /**< format specific flags, see AVFMTCTX_xx */
477     /* private data for pts handling (do not modify directly) */
478     /** This buffer is only needed when packets were already buffered but
479        not decoded, for example to get the codec parameters in mpeg
480        streams */
481     struct AVPacketList *packet_buffer;
482
483     /** decoding: position of the first frame of the component, in
484        AV_TIME_BASE fractional seconds. NEVER set this value directly:
485        it is deduced from the AVStream values.  */
486     int64_t start_time;
487     /** decoding: duration of the stream, in AV_TIME_BASE fractional
488        seconds. NEVER set this value directly: it is deduced from the
489        AVStream values.  */
490     int64_t duration;
491     /** decoding: total file size. 0 if unknown */
492     int64_t file_size;
493     /** decoding: total stream bitrate in bit/s, 0 if not
494        available. Never set it directly if the file_size and the
495        duration are known as ffmpeg can compute it automatically. */
496     int bit_rate;
497
498     /* av_read_frame() support */
499     AVStream *cur_st;
500     const uint8_t *cur_ptr;
501     int cur_len;
502     AVPacket cur_pkt;
503
504     /* av_seek_frame() support */
505     int64_t data_offset; /** offset of the first packet */
506     int index_built;
507
508     int mux_rate;
509     int packet_size;
510     int preload;
511     int max_delay;
512
513 #define AVFMT_NOOUTPUTLOOP -1
514 #define AVFMT_INFINITEOUTPUTLOOP 0
515     /** number of times to loop output in formats that support it */
516     int loop_output;
517
518     int flags;
519 #define AVFMT_FLAG_GENPTS       0x0001 ///< generate pts if missing even if it requires parsing future frames
520 #define AVFMT_FLAG_IGNIDX       0x0002 ///< ignore index
521 #define AVFMT_FLAG_NONBLOCK     0x0004 ///< do not block when reading packets from input
522
523     int loop_input;
524     /** decoding: size of data to probe; encoding unused */
525     unsigned int probesize;
526
527     /**
528      * maximum duration in AV_TIME_BASE units over which the input should be analyzed in av_find_stream_info()
529      */
530     int max_analyze_duration;
531
532     const uint8_t *key;
533     int keylen;
534
535     unsigned int nb_programs;
536     AVProgram **programs;
537
538     /**
539      * Forced video codec_id.
540      * demuxing: set by user
541      */
542     enum CodecID video_codec_id;
543     /**
544      * Forced audio codec_id.
545      * demuxing: set by user
546      */
547     enum CodecID audio_codec_id;
548     /**
549      * Forced subtitle codec_id.
550      * demuxing: set by user
551      */
552     enum CodecID subtitle_codec_id;
553
554     /**
555      * Maximum amount of memory in bytes to use per stream for the index.
556      * If the needed index exceeds this size entries will be discarded as
557      * needed to maintain a smaller size. This can lead to slower or less
558      * accurate seeking (depends on demuxer).
559      * Demuxers for which a full in memory index is mandatory will ignore
560      * this.
561      * muxing  : unused
562      * demuxing: set by user
563      */
564     unsigned int max_index_size;
565
566     /**
567      * Maximum amount of memory in bytes to use for buffering frames
568      * obtained from real-time capture devices.
569      */
570     unsigned int max_picture_buffer;
571
572     unsigned int nb_chapters;
573     AVChapter **chapters;
574
575     /**
576      * Flags to enable debuging.
577      */
578     int debug;
579 #define FF_FDEBUG_TS        0x0001
580
581     /**
582      * raw packets from the demuxer, prior to parsing and decoding.
583      * This buffer is used for buffering packets until the codec can
584      * be identified, as parsing cannot be done without knowing the
585      * codec.
586      */
587     struct AVPacketList *raw_packet_buffer;
588     struct AVPacketList *raw_packet_buffer_end;
589
590     struct AVPacketList *packet_buffer_end;
591 } AVFormatContext;
592
593 typedef struct AVPacketList {
594     AVPacket pkt;
595     struct AVPacketList *next;
596 } AVPacketList;
597
598 #if LIBAVFORMAT_VERSION_INT < (53<<16)
599 extern AVInputFormat *first_iformat;
600 extern AVOutputFormat *first_oformat;
601 #endif
602
603 AVInputFormat  *av_iformat_next(AVInputFormat  *f);
604 AVOutputFormat *av_oformat_next(AVOutputFormat *f);
605
606 enum CodecID av_guess_image2_codec(const char *filename);
607
608 /* XXX: use automatic init with either ELF sections or C file parser */
609 /* modules */
610
611 /* utils.c */
612 void av_register_input_format(AVInputFormat *format);
613 void av_register_output_format(AVOutputFormat *format);
614 AVOutputFormat *guess_stream_format(const char *short_name,
615                                     const char *filename, const char *mime_type);
616 AVOutputFormat *guess_format(const char *short_name,
617                              const char *filename, const char *mime_type);
618
619 /**
620  * Guesses the codec id based upon muxer and filename.
621  */
622 enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
623                             const char *filename, const char *mime_type, enum CodecType type);
624
625 /**
626  * Send a nice hexadecimal dump of a buffer to the specified file stream.
627  *
628  * @param f The file stream pointer where the dump should be sent to.
629  * @param buf buffer
630  * @param size buffer size
631  *
632  * @see av_hex_dump_log, av_pkt_dump, av_pkt_dump_log
633  */
634 void av_hex_dump(FILE *f, uint8_t *buf, int size);
635
636 /**
637  * Send a nice hexadecimal dump of a buffer to the log.
638  *
639  * @param avcl A pointer to an arbitrary struct of which the first field is a
640  * pointer to an AVClass struct.
641  * @param level The importance level of the message, lower values signifying
642  * higher importance.
643  * @param buf buffer
644  * @param size buffer size
645  *
646  * @see av_hex_dump, av_pkt_dump, av_pkt_dump_log
647  */
648 void av_hex_dump_log(void *avcl, int level, uint8_t *buf, int size);
649
650 /**
651  * Send a nice dump of a packet to the specified file stream.
652  *
653  * @param f The file stream pointer where the dump should be sent to.
654  * @param pkt packet to dump
655  * @param dump_payload true if the payload must be displayed too
656  */
657 void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
658
659 /**
660  * Send a nice dump of a packet to the log.
661  *
662  * @param avcl A pointer to an arbitrary struct of which the first field is a
663  * pointer to an AVClass struct.
664  * @param level The importance level of the message, lower values signifying
665  * higher importance.
666  * @param pkt packet to dump
667  * @param dump_payload true if the payload must be displayed too
668  */
669 void av_pkt_dump_log(void *avcl, int level, AVPacket *pkt, int dump_payload);
670
671 void av_register_all(void);
672
673 /** codec tag <-> codec id */
674 enum CodecID av_codec_get_id(const struct AVCodecTag **tags, unsigned int tag);
675 unsigned int av_codec_get_tag(const struct AVCodecTag **tags, enum CodecID id);
676
677 /* media file input */
678
679 /**
680  * finds AVInputFormat based on input format's short name.
681  */
682 AVInputFormat *av_find_input_format(const char *short_name);
683
684 /**
685  * Guess file format.
686  *
687  * @param is_opened whether the file is already opened, determines whether
688  *                  demuxers with or without AVFMT_NOFILE are probed
689  */
690 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
691
692 /**
693  * Allocates all the structures needed to read an input stream.
694  *        This does not open the needed codecs for decoding the stream[s].
695  */
696 int av_open_input_stream(AVFormatContext **ic_ptr,
697                          ByteIOContext *pb, const char *filename,
698                          AVInputFormat *fmt, AVFormatParameters *ap);
699
700 /**
701  * Open a media file as input. The codecs are not opened. Only the file
702  * header (if present) is read.
703  *
704  * @param ic_ptr the opened media file handle is put here
705  * @param filename filename to open.
706  * @param fmt if non NULL, force the file format to use
707  * @param buf_size optional buffer size (zero if default is OK)
708  * @param ap additional parameters needed when opening the file (NULL if default)
709  * @return 0 if OK. AVERROR_xxx otherwise.
710  */
711 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
712                        AVInputFormat *fmt,
713                        int buf_size,
714                        AVFormatParameters *ap);
715 /**
716  * Allocate an AVFormatContext.
717  * Can be freed with av_free() but do not forget to free everything you
718  * explicitly allocated as well!
719  */
720 AVFormatContext *av_alloc_format_context(void);
721
722 /**
723  * Read packets of a media file to get stream information. This
724  * is useful for file formats with no headers such as MPEG. This
725  * function also computes the real frame rate in case of mpeg2 repeat
726  * frame mode.
727  * The logical file position is not changed by this function;
728  * examined packets may be buffered for later processing.
729  *
730  * @param ic media file handle
731  * @return >=0 if OK. AVERROR_xxx if error.
732  * @todo Let user decide somehow what information is needed so we do not waste time getting stuff the user does not need.
733  */
734 int av_find_stream_info(AVFormatContext *ic);
735
736 /**
737  * Read a transport packet from a media file.
738  *
739  * This function is obsolete and should never be used.
740  * Use av_read_frame() instead.
741  *
742  * @param s media file handle
743  * @param pkt is filled
744  * @return 0 if OK. AVERROR_xxx if error.
745  */
746 int av_read_packet(AVFormatContext *s, AVPacket *pkt);
747
748 /**
749  * Return the next frame of a stream.
750  *
751  * The returned packet is valid
752  * until the next av_read_frame() or until av_close_input_file() and
753  * must be freed with av_free_packet. For video, the packet contains
754  * exactly one frame. For audio, it contains an integer number of
755  * frames if each frame has a known fixed size (e.g. PCM or ADPCM
756  * data). If the audio frames have a variable size (e.g. MPEG audio),
757  * then it contains one frame.
758  *
759  * pkt->pts, pkt->dts and pkt->duration are always set to correct
760  * values in AVStream.timebase units (and guessed if the format cannot
761  * provided them). pkt->pts can be AV_NOPTS_VALUE if the video format
762  * has B frames, so it is better to rely on pkt->dts if you do not
763  * decompress the payload.
764  *
765  * @return 0 if OK, < 0 if error or end of file.
766  */
767 int av_read_frame(AVFormatContext *s, AVPacket *pkt);
768
769 /**
770  * Seek to the key frame at timestamp.
771  * 'timestamp' in 'stream_index'.
772  * @param stream_index If stream_index is (-1), a default
773  * stream is selected, and timestamp is automatically converted
774  * from AV_TIME_BASE units to the stream specific time_base.
775  * @param timestamp timestamp in AVStream.time_base units
776  *        or if there is no stream specified then in AV_TIME_BASE units
777  * @param flags flags which select direction and seeking mode
778  * @return >= 0 on success
779  */
780 int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags);
781
782 /**
783  * start playing a network based stream (e.g. RTSP stream) at the
784  * current position
785  */
786 int av_read_play(AVFormatContext *s);
787
788 /**
789  * Pause a network based stream (e.g. RTSP stream).
790  *
791  * Use av_read_play() to resume it.
792  */
793 int av_read_pause(AVFormatContext *s);
794
795 /**
796  * Free a AVFormatContext allocated by av_open_input_stream.
797  * @param s context to free
798  */
799 void av_close_input_stream(AVFormatContext *s);
800
801 /**
802  * Close a media file (but not its codecs).
803  *
804  * @param s media file handle
805  */
806 void av_close_input_file(AVFormatContext *s);
807
808 /**
809  * Add a new stream to a media file.
810  *
811  * Can only be called in the read_header() function. If the flag
812  * AVFMTCTX_NOHEADER is in the format context, then new streams
813  * can be added in read_packet too.
814  *
815  * @param s media file handle
816  * @param id file format dependent stream id
817  */
818 AVStream *av_new_stream(AVFormatContext *s, int id);
819 AVProgram *av_new_program(AVFormatContext *s, int id);
820
821 /**
822  * Add a new chapter.
823  * This function is NOT part of the public API
824  * and should be ONLY used by demuxers.
825  *
826  * @param s media file handle
827  * @param id unique id for this chapter
828  * @param start chapter start time in time_base units
829  * @param end chapter end time in time_base units
830  * @param title chapter title
831  *
832  * @return AVChapter or NULL if error.
833  */
834 AVChapter *ff_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title);
835
836 /**
837  * Set the pts for a given stream.
838  *
839  * @param s stream
840  * @param pts_wrap_bits number of bits effectively used by the pts
841  *        (used for wrap control, 33 is the value for MPEG)
842  * @param pts_num numerator to convert to seconds (MPEG: 1)
843  * @param pts_den denominator to convert to seconds (MPEG: 90000)
844  */
845 void av_set_pts_info(AVStream *s, int pts_wrap_bits,
846                      int pts_num, int pts_den);
847
848 #define AVSEEK_FLAG_BACKWARD 1 ///< seek backward
849 #define AVSEEK_FLAG_BYTE     2 ///< seeking based on position in bytes
850 #define AVSEEK_FLAG_ANY      4 ///< seek to any frame, even non keyframes
851
852 int av_find_default_stream_index(AVFormatContext *s);
853
854 /**
855  * Gets the index for a specific timestamp.
856  * @param flags if AVSEEK_FLAG_BACKWARD then the returned index will correspond to
857  *                 the timestamp which is <= the requested one, if backward is 0
858  *                 then it will be >=
859  *              if AVSEEK_FLAG_ANY seek to any frame, only keyframes otherwise
860  * @return < 0 if no such timestamp could be found
861  */
862 int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags);
863
864 /**
865  * Ensures the index uses less memory than the maximum specified in
866  * AVFormatContext.max_index_size, by discarding entries if it grows
867  * too large.
868  * This function is not part of the public API and should only be called
869  * by demuxers.
870  */
871 void ff_reduce_index(AVFormatContext *s, int stream_index);
872
873 /**
874  * Add a index entry into a sorted list updateing if it is already there.
875  *
876  * @param timestamp timestamp in the timebase of the given stream
877  */
878 int av_add_index_entry(AVStream *st,
879                        int64_t pos, int64_t timestamp, int size, int distance, int flags);
880
881 /**
882  * Does a binary search using av_index_search_timestamp() and AVCodec.read_timestamp().
883  * This is not supposed to be called directly by a user application, but by demuxers.
884  * @param target_ts target timestamp in the time base of the given stream
885  * @param stream_index stream number
886  */
887 int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags);
888
889 /**
890  * Updates cur_dts of all streams based on given timestamp and AVStream.
891  *
892  * Stream ref_st unchanged, others set cur_dts in their native timebase
893  * only needed for timestamp wrapping or if (dts not set and pts!=dts).
894  * @param timestamp new dts expressed in time_base of param ref_st
895  * @param ref_st reference stream giving time_base of param timestamp
896  */
897 void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp);
898
899 /**
900  * Does a binary search using read_timestamp().
901  * This is not supposed to be called directly by a user application, but by demuxers.
902  * @param target_ts target timestamp in the time base of the given stream
903  * @param stream_index stream number
904  */
905 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 ));
906
907 /** media file output */
908 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
909
910 /**
911  * Allocate the stream private data and write the stream header to an
912  * output media file.
913  *
914  * @param s media file handle
915  * @return 0 if OK. AVERROR_xxx if error.
916  */
917 int av_write_header(AVFormatContext *s);
918
919 /**
920  * Write a packet to an output media file.
921  *
922  * The packet shall contain one audio or video frame.
923  * The packet must be correctly interleaved according to the container specification,
924  * if not then av_interleaved_write_frame must be used
925  *
926  * @param s media file handle
927  * @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ...
928  * @return < 0 if error, = 0 if OK, 1 if end of stream wanted.
929  */
930 int av_write_frame(AVFormatContext *s, AVPacket *pkt);
931
932 /**
933  * Writes a packet to an output media file ensuring correct interleaving.
934  *
935  * The packet must contain one audio or video frame.
936  * If the packets are already correctly interleaved the application should
937  * call av_write_frame() instead as it is slightly faster. It is also important
938  * to keep in mind that completely non-interleaved input will need huge amounts
939  * of memory to interleave with this, so it is preferable to interleave at the
940  * demuxer level.
941  *
942  * @param s media file handle
943  * @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ...
944  * @return < 0 if error, = 0 if OK, 1 if end of stream wanted.
945  */
946 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
947
948 /**
949  * Interleave a packet per DTS in an output media file.
950  *
951  * Packets with pkt->destruct == av_destruct_packet will be freed inside this function,
952  * so they cannot be used after it, note calling av_free_packet() on them is still safe.
953  *
954  * @param s media file handle
955  * @param out the interleaved packet will be output here
956  * @param in the input packet
957  * @param flush 1 if no further packets are available as input and all
958  *              remaining packets should be output
959  * @return 1 if a packet was output, 0 if no packet could be output,
960  *         < 0 if an error occurred
961  */
962 int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush);
963
964 /**
965  * @brief Write the stream trailer to an output media file and
966  *        free the file private data.
967  *
968  * @param s media file handle
969  * @return 0 if OK. AVERROR_xxx if error.
970  */
971 int av_write_trailer(AVFormatContext *s);
972
973 void dump_format(AVFormatContext *ic,
974                  int index,
975                  const char *url,
976                  int is_output);
977
978 /**
979  * parses width and height out of string str.
980  * @deprecated Use av_parse_video_frame_size instead.
981  */
982 attribute_deprecated int parse_image_size(int *width_ptr, int *height_ptr, const char *str);
983
984 /**
985  * Converts frame rate from string to a fraction.
986  * @deprecated Use av_parse_video_frame_rate instead.
987  */
988 attribute_deprecated int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg);
989
990 /**
991  * Parses \p datestr and returns a corresponding number of microseconds.
992  * @param datestr String representing a date or a duration.
993  * - If a date the syntax is:
994  * @code
995  *  [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]}
996  * @endcode
997  * Time is localtime unless Z is appended, in which case it is
998  * interpreted as UTC.
999  * If the year-month-day part isn't specified it takes the current
1000  * year-month-day.
1001  * Returns the number of microseconds since 1st of January, 1970 up to
1002  * the time of the parsed date or INT64_MIN if \p datestr cannot be
1003  * successfully parsed.
1004  * - If a duration the syntax is:
1005  * @code
1006  *  [-]HH[:MM[:SS[.m...]]]
1007  *  [-]S+[.m...]
1008  * @endcode
1009  * Returns the number of microseconds contained in a time interval
1010  * with the specified duration or INT64_MIN if \p datestr cannot be
1011  * successfully parsed.
1012  * @param duration Flag which tells how to interpret \p datestr, if
1013  * not zero \p datestr is interpreted as a duration, otherwise as a
1014  * date.
1015  */
1016 int64_t parse_date(const char *datestr, int duration);
1017
1018 int64_t av_gettime(void);
1019
1020 /* ffm specific for ffserver */
1021 #define FFM_PACKET_SIZE 4096
1022 offset_t ffm_read_write_index(int fd);
1023 void ffm_write_write_index(int fd, offset_t pos);
1024 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size);
1025
1026 /**
1027  * Attempts to find a specific tag in a URL.
1028  *
1029  * syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done.
1030  * Return 1 if found.
1031  */
1032 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);
1033
1034 /**
1035  * Returns in 'buf' the path with '%d' replaced by number.
1036
1037  * Also handles the '%0nd' format where 'n' is the total number
1038  * of digits and '%%'.
1039  *
1040  * @param buf destination buffer
1041  * @param buf_size destination buffer size
1042  * @param path numbered sequence string
1043  * @param number frame number
1044  * @return 0 if OK, -1 if format error.
1045  */
1046 int av_get_frame_filename(char *buf, int buf_size,
1047                           const char *path, int number);
1048
1049 /**
1050  * Check whether filename actually is a numbered sequence generator.
1051  *
1052  * @param filename possible numbered sequence string
1053  * @return 1 if a valid numbered sequence string, 0 otherwise.
1054  */
1055 int av_filename_number_test(const char *filename);
1056
1057 /**
1058  * Generate an SDP for an RTP session.
1059  *
1060  * @param ac array of AVFormatContexts describing the RTP streams. If the
1061  *           array is composed by only one context, such context can contain
1062  *           multiple AVStreams (one AVStream per RTP stream). Otherwise,
1063  *           all the contexts in the array (an AVCodecContext per RTP stream)
1064  *           must contain only one AVStream
1065  * @param n_files number of AVCodecContexts contained in ac
1066  * @param buff buffer where the SDP will be stored (must be allocated by
1067  *             the caller
1068  * @param size the size of the buffer
1069  * @return 0 if OK. AVERROR_xxx if error.
1070  */
1071 int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size);
1072
1073 #ifdef HAVE_AV_CONFIG_H
1074
1075 void ff_dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem);
1076
1077 #ifdef __GNUC__
1078 #define dynarray_add(tab, nb_ptr, elem)\
1079 do {\
1080     typeof(tab) _tab = (tab);\
1081     typeof(elem) _elem = (elem);\
1082     (void)sizeof(**_tab == _elem); /* check that types are compatible */\
1083     ff_dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
1084 } while(0)
1085 #else
1086 #define dynarray_add(tab, nb_ptr, elem)\
1087 do {\
1088     ff_dynarray_add((unsigned long **)(tab), nb_ptr, (unsigned long)(elem));\
1089 } while(0)
1090 #endif
1091
1092 time_t mktimegm(struct tm *tm);
1093 struct tm *brktimegm(time_t secs, struct tm *tm);
1094 const char *small_strptime(const char *p, const char *fmt,
1095                            struct tm *dt);
1096
1097 struct in_addr;
1098 int resolve_host(struct in_addr *sin_addr, const char *hostname);
1099
1100 void url_split(char *proto, int proto_size,
1101                char *authorization, int authorization_size,
1102                char *hostname, int hostname_size,
1103                int *port_ptr,
1104                char *path, int path_size,
1105                const char *url);
1106
1107 int match_ext(const char *filename, const char *extensions);
1108
1109 #endif /* HAVE_AV_CONFIG_H */
1110
1111 #endif /* AVFORMAT_AVFORMAT_H */