xref: /third_party/ffmpeg/libavformat/internal.h (revision cabdff1a)
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_INTERNAL_H
22#define AVFORMAT_INTERNAL_H
23
24#include <stdint.h>
25
26#include "libavcodec/avcodec.h"
27#include "libavcodec/packet_internal.h"
28
29#include "avformat.h"
30#include "os_support.h"
31
32#define MAX_URL_SIZE 4096
33
34/** size of probe buffer, for guessing file type from file contents */
35#define PROBE_BUF_MIN 2048
36#define PROBE_BUF_MAX (1 << 20)
37
38#ifdef DEBUG
39#    define hex_dump_debug(class, buf, size) av_hex_dump_log(class, AV_LOG_DEBUG, buf, size)
40#else
41#    define hex_dump_debug(class, buf, size) do { if (0) av_hex_dump_log(class, AV_LOG_DEBUG, buf, size); } while(0)
42#endif
43
44/**
45 * For an AVInputFormat with this flag set read_close() needs to be called
46 * by the caller upon read_header() failure.
47 */
48#define FF_FMT_INIT_CLEANUP                             (1 << 0)
49
50typedef struct AVCodecTag {
51    enum AVCodecID id;
52    unsigned int tag;
53} AVCodecTag;
54
55typedef struct CodecMime{
56    char str[32];
57    enum AVCodecID id;
58} CodecMime;
59
60/*************************************************/
61/* fractional numbers for exact pts handling */
62
63/**
64 * The exact value of the fractional number is: 'val + num / den'.
65 * num is assumed to be 0 <= num < den.
66 */
67typedef struct FFFrac {
68    int64_t val, num, den;
69} FFFrac;
70
71
72typedef struct FFFormatContext {
73    /**
74     * The public context.
75     */
76    AVFormatContext pub;
77
78    /**
79     * Number of streams relevant for interleaving.
80     * Muxing only.
81     */
82    int nb_interleaved_streams;
83
84    /**
85     * Whether the timestamp shift offset has already been determined.
86     * -1: disabled, 0: not yet determined, 1: determined.
87     */
88    enum {
89        AVOID_NEGATIVE_TS_DISABLED = -1,
90        AVOID_NEGATIVE_TS_UNKNOWN  = 0,
91        AVOID_NEGATIVE_TS_KNOWN    = 1,
92    } avoid_negative_ts_status;
93#define AVOID_NEGATIVE_TS_ENABLED(status) ((status) >= 0)
94
95    /**
96     * The interleavement function in use. Always set for muxers.
97     */
98    int (*interleave_packet)(struct AVFormatContext *s, AVPacket *pkt,
99                             int flush, int has_packet);
100
101    /**
102     * This buffer is only needed when packets were already buffered but
103     * not decoded, for example to get the codec parameters in MPEG
104     * streams.
105     */
106    PacketList packet_buffer;
107
108    /* av_seek_frame() support */
109    int64_t data_offset; /**< offset of the first packet */
110
111    /**
112     * Raw packets from the demuxer, prior to parsing and decoding.
113     * This buffer is used for buffering packets until the codec can
114     * be identified, as parsing cannot be done without knowing the
115     * codec.
116     */
117    PacketList raw_packet_buffer;
118    /**
119     * Packets split by the parser get queued here.
120     */
121    PacketList parse_queue;
122    /**
123     * The generic code uses this as a temporary packet
124     * to parse packets or for muxing, especially flushing.
125     * For demuxers, it may also be used for other means
126     * for short periods that are guaranteed not to overlap
127     * with calls to av_read_frame() (or ff_read_packet())
128     * or with each other.
129     * It may be used by demuxers as a replacement for
130     * stack packets (unless they call one of the aforementioned
131     * functions with their own AVFormatContext).
132     * Every user has to ensure that this packet is blank
133     * after using it.
134     */
135    AVPacket *parse_pkt;
136
137    /**
138     * Used to hold temporary packets for the generic demuxing code.
139     * When muxing, it may be used by muxers to hold packets (even
140     * permanent ones).
141     */
142    AVPacket *pkt;
143    /**
144     * Sum of the size of packets in raw_packet_buffer, in bytes.
145     */
146    int raw_packet_buffer_size;
147
148#if FF_API_COMPUTE_PKT_FIELDS2
149    int missing_ts_warning;
150#endif
151
152    int inject_global_side_data;
153
154    int avoid_negative_ts_use_pts;
155
156    /**
157     * Timestamp of the end of the shortest stream.
158     */
159    int64_t shortest_end;
160
161    /**
162     * Whether or not avformat_init_output has already been called
163     */
164    int initialized;
165
166    /**
167     * Whether or not avformat_init_output fully initialized streams
168     */
169    int streams_initialized;
170
171    /**
172     * ID3v2 tag useful for MP3 demuxing
173     */
174    AVDictionary *id3v2_meta;
175
176    /*
177     * Prefer the codec framerate for avg_frame_rate computation.
178     */
179    int prefer_codec_framerate;
180
181    /**
182     * Set if chapter ids are strictly monotonic.
183     */
184    int chapter_ids_monotonic;
185
186    /**
187     * Contexts and child contexts do not contain a metadata option
188     */
189    int metafree;
190} FFFormatContext;
191
192static av_always_inline FFFormatContext *ffformatcontext(AVFormatContext *s)
193{
194    return (FFFormatContext*)s;
195}
196
197typedef struct FFStream {
198    /**
199     * The public context.
200     */
201    AVStream pub;
202
203    /**
204     * Set to 1 if the codec allows reordering, so pts can be different
205     * from dts.
206     */
207    int reorder;
208
209    /**
210     * bitstream filter to run on stream
211     * - encoding: Set by muxer using ff_stream_add_bitstream_filter
212     * - decoding: unused
213     */
214    struct AVBSFContext *bsfc;
215
216    /**
217     * Whether or not check_bitstream should still be run on each packet
218     */
219    int bitstream_checked;
220
221    /**
222     * The codec context used by avformat_find_stream_info, the parser, etc.
223     */
224    AVCodecContext *avctx;
225    /**
226     * 1 if avctx has been initialized with the values from the codec parameters
227     */
228    int avctx_inited;
229
230    /* the context for extracting extradata in find_stream_info()
231     * inited=1/bsf=NULL signals that extracting is not possible (codec not
232     * supported) */
233    struct {
234        struct AVBSFContext *bsf;
235        int inited;
236    } extract_extradata;
237
238    /**
239     * Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar)
240     */
241    int need_context_update;
242
243    int is_intra_only;
244
245    FFFrac *priv_pts;
246
247    /**
248     * Stream information used internally by avformat_find_stream_info()
249     */
250    struct FFStreamInfo *info;
251
252    AVIndexEntry *index_entries; /**< Only used if the format does not
253                                    support seeking natively. */
254    int nb_index_entries;
255    unsigned int index_entries_allocated_size;
256
257    int64_t interleaver_chunk_size;
258    int64_t interleaver_chunk_duration;
259
260    /**
261     * stream probing state
262     * -1   -> probing finished
263     *  0   -> no probing requested
264     * rest -> perform probing with request_probe being the minimum score to accept.
265     */
266    int request_probe;
267    /**
268     * Indicates that everything up to the next keyframe
269     * should be discarded.
270     */
271    int skip_to_keyframe;
272
273    /**
274     * Number of samples to skip at the start of the frame decoded from the next packet.
275     */
276    int skip_samples;
277
278    /**
279     * If not 0, the number of samples that should be skipped from the start of
280     * the stream (the samples are removed from packets with pts==0, which also
281     * assumes negative timestamps do not happen).
282     * Intended for use with formats such as mp3 with ad-hoc gapless audio
283     * support.
284     */
285    int64_t start_skip_samples;
286
287    /**
288     * If not 0, the first audio sample that should be discarded from the stream.
289     * This is broken by design (needs global sample count), but can't be
290     * avoided for broken by design formats such as mp3 with ad-hoc gapless
291     * audio support.
292     */
293    int64_t first_discard_sample;
294
295    /**
296     * The sample after last sample that is intended to be discarded after
297     * first_discard_sample. Works on frame boundaries only. Used to prevent
298     * early EOF if the gapless info is broken (considered concatenated mp3s).
299     */
300    int64_t last_discard_sample;
301
302    /**
303     * Number of internally decoded frames, used internally in libavformat, do not access
304     * its lifetime differs from info which is why it is not in that structure.
305     */
306    int nb_decoded_frames;
307
308    /**
309     * Timestamp offset added to timestamps before muxing
310     */
311    int64_t mux_ts_offset;
312
313    /**
314     * Internal data to check for wrapping of the time stamp
315     */
316    int64_t pts_wrap_reference;
317
318    /**
319     * Options for behavior, when a wrap is detected.
320     *
321     * Defined by AV_PTS_WRAP_ values.
322     *
323     * If correction is enabled, there are two possibilities:
324     * If the first time stamp is near the wrap point, the wrap offset
325     * will be subtracted, which will create negative time stamps.
326     * Otherwise the offset will be added.
327     */
328    int pts_wrap_behavior;
329
330    /**
331     * Internal data to prevent doing update_initial_durations() twice
332     */
333    int update_initial_durations_done;
334
335#define MAX_REORDER_DELAY 16
336
337    /**
338     * Internal data to generate dts from pts
339     */
340    int64_t pts_reorder_error[MAX_REORDER_DELAY+1];
341    uint8_t pts_reorder_error_count[MAX_REORDER_DELAY+1];
342
343    int64_t pts_buffer[MAX_REORDER_DELAY+1];
344
345    /**
346     * Internal data to analyze DTS and detect faulty mpeg streams
347     */
348    int64_t last_dts_for_order_check;
349    uint8_t dts_ordered;
350    uint8_t dts_misordered;
351
352    /**
353     * Internal data to inject global side data
354     */
355    int inject_global_side_data;
356
357    /**
358     * display aspect ratio (0 if unknown)
359     * - encoding: unused
360     * - decoding: Set by libavformat to calculate sample_aspect_ratio internally
361     */
362    AVRational display_aspect_ratio;
363
364    AVProbeData probe_data;
365
366    /**
367     * last packet in packet_buffer for this stream when muxing.
368     */
369    PacketListEntry *last_in_packet_buffer;
370
371    int64_t last_IP_pts;
372    int last_IP_duration;
373
374    /**
375     * Number of packets to buffer for codec probing
376     */
377    int probe_packets;
378
379    /* av_read_frame() support */
380    enum AVStreamParseType need_parsing;
381    struct AVCodecParserContext *parser;
382
383    /**
384     * Number of frames that have been demuxed during avformat_find_stream_info()
385     */
386    int codec_info_nb_frames;
387
388    /**
389     * Stream Identifier
390     * This is the MPEG-TS stream identifier +1
391     * 0 means unknown
392     */
393    int stream_identifier;
394
395    // Timestamp generation support:
396    /**
397     * Timestamp corresponding to the last dts sync point.
398     *
399     * Initialized when AVCodecParserContext.dts_sync_point >= 0 and
400     * a DTS is received from the underlying container. Otherwise set to
401     * AV_NOPTS_VALUE by default.
402     */
403    int64_t first_dts;
404    int64_t cur_dts;
405} FFStream;
406
407static av_always_inline FFStream *ffstream(AVStream *st)
408{
409    return (FFStream*)st;
410}
411
412static av_always_inline const FFStream *cffstream(const AVStream *st)
413{
414    return (FFStream*)st;
415}
416
417#ifdef __GNUC__
418#define dynarray_add(tab, nb_ptr, elem)\
419do {\
420    __typeof__(tab) _tab = (tab);\
421    __typeof__(elem) _elem = (elem);\
422    (void)sizeof(**_tab == _elem); /* check that types are compatible */\
423    av_dynarray_add(_tab, nb_ptr, _elem);\
424} while(0)
425#else
426#define dynarray_add(tab, nb_ptr, elem)\
427do {\
428    av_dynarray_add((tab), nb_ptr, (elem));\
429} while(0)
430#endif
431
432
433void ff_flush_packet_queue(AVFormatContext *s);
434
435/**
436 * Automatically create sub-directories
437 *
438 * @param path will create sub-directories by path
439 * @return 0, or < 0 on error
440 */
441int ff_mkdir_p(const char *path);
442
443/**
444 * Write hexadecimal string corresponding to given binary data. The string
445 * is zero-terminated.
446 *
447 * @param buf       the output string is written here;
448 *                  needs to be at least 2 * size + 1 bytes long.
449 * @param src       the input data to be transformed.
450 * @param size      the size (in byte) of src.
451 * @param lowercase determines whether to use the range [0-9a-f] or [0-9A-F].
452 * @return buf.
453 */
454char *ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase);
455
456/**
457 * Parse a string of hexadecimal strings. Any space between the hexadecimal
458 * digits is ignored.
459 *
460 * @param data if non-null, the parsed data is written to this pointer
461 * @param p the string to parse
462 * @return the number of bytes written (or to be written, if data is null)
463 */
464int ff_hex_to_data(uint8_t *data, const char *p);
465
466#define NTP_OFFSET 2208988800ULL
467#define NTP_OFFSET_US (NTP_OFFSET * 1000000ULL)
468
469/** Get the current time since NTP epoch in microseconds. */
470uint64_t ff_ntp_time(void);
471
472/**
473 * Get the NTP time stamp formatted as per the RFC-5905.
474 *
475 * @param ntp_time NTP time in micro seconds (since NTP epoch)
476 * @return the formatted NTP time stamp
477 */
478uint64_t ff_get_formatted_ntp_time(uint64_t ntp_time_us);
479
480/**
481 * Parse the NTP time in micro seconds (since NTP epoch).
482 *
483 * @param ntp_ts NTP time stamp formatted as per the RFC-5905.
484 * @return the time in micro seconds (since NTP epoch)
485 */
486uint64_t ff_parse_ntp_time(uint64_t ntp_ts);
487
488/**
489 * Append the media-specific SDP fragment for the media stream c
490 * to the buffer buff.
491 *
492 * Note, the buffer needs to be initialized, since it is appended to
493 * existing content.
494 *
495 * @param buff the buffer to append the SDP fragment to
496 * @param size the size of the buff buffer
497 * @param st the AVStream of the media to describe
498 * @param idx the global stream index
499 * @param dest_addr the destination address of the media stream, may be NULL
500 * @param dest_type the destination address type, may be NULL
501 * @param port the destination port of the media stream, 0 if unknown
502 * @param ttl the time to live of the stream, 0 if not multicast
503 * @param fmt the AVFormatContext, which might contain options modifying
504 *            the generated SDP
505 * @return 0 on success, a negative error code on failure
506 */
507int ff_sdp_write_media(char *buff, int size, const AVStream *st, int idx,
508                       const char *dest_addr, const char *dest_type,
509                       int port, int ttl, AVFormatContext *fmt);
510
511/**
512 * Read a whole line of text from AVIOContext. Stop reading after reaching
513 * either a \\n, a \\0 or EOF. The returned string is always \\0-terminated,
514 * and may be truncated if the buffer is too small.
515 *
516 * @param s the read-only AVIOContext
517 * @param buf buffer to store the read line
518 * @param maxlen size of the buffer
519 * @return the length of the string written in the buffer, not including the
520 *         final \\0
521 */
522int ff_get_line(AVIOContext *s, char *buf, int maxlen);
523
524/**
525 * Same as ff_get_line but strip the white-space characters in the text tail
526 *
527 * @param s the read-only AVIOContext
528 * @param buf buffer to store the read line
529 * @param maxlen size of the buffer
530 * @return the length of the string written in the buffer
531 */
532int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen);
533
534#define SPACE_CHARS " \t\r\n"
535
536/**
537 * Callback function type for ff_parse_key_value.
538 *
539 * @param key a pointer to the key
540 * @param key_len the number of bytes that belong to the key, including the '='
541 *                char
542 * @param dest return the destination pointer for the value in *dest, may
543 *             be null to ignore the value
544 * @param dest_len the length of the *dest buffer
545 */
546typedef void (*ff_parse_key_val_cb)(void *context, const char *key,
547                                    int key_len, char **dest, int *dest_len);
548/**
549 * Parse a string with comma-separated key=value pairs. The value strings
550 * may be quoted and may contain escaped characters within quoted strings.
551 *
552 * @param str the string to parse
553 * @param callback_get_buf function that returns where to store the
554 *                         unescaped value string.
555 * @param context the opaque context pointer to pass to callback_get_buf
556 */
557void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
558                        void *context);
559
560enum AVCodecID ff_guess_image2_codec(const char *filename);
561
562const AVCodec *ff_find_decoder(AVFormatContext *s, const AVStream *st,
563                               enum AVCodecID codec_id);
564
565/**
566 * Set the time base and wrapping info for a given stream. This will be used
567 * to interpret the stream's timestamps. If the new time base is invalid
568 * (numerator or denominator are non-positive), it leaves the stream
569 * unchanged.
570 *
571 * @param st stream
572 * @param pts_wrap_bits number of bits effectively used by the pts
573 *        (used for wrap control)
574 * @param pts_num time base numerator
575 * @param pts_den time base denominator
576 */
577void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits,
578                         unsigned int pts_num, unsigned int pts_den);
579
580/**
581 * Set the timebase for each stream from the corresponding codec timebase and
582 * print it.
583 */
584int ff_framehash_write_header(AVFormatContext *s);
585
586/**
587 * Frees a stream without modifying the corresponding AVFormatContext.
588 * Must only be called if the latter doesn't matter or if the stream
589 * is not yet attached to an AVFormatContext.
590 */
591void ff_free_stream(AVStream **st);
592/**
593 * Remove a stream from its AVFormatContext and free it.
594 * The stream must be the last stream of the AVFormatContext.
595 */
596void ff_remove_stream(AVFormatContext *s, AVStream *st);
597
598unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id);
599
600enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag);
601
602int ff_is_intra_only(enum AVCodecID id);
603
604/**
605 * Select a PCM codec based on the given parameters.
606 *
607 * @param bps     bits-per-sample
608 * @param flt     floating-point
609 * @param be      big-endian
610 * @param sflags  signed flags. each bit corresponds to one byte of bit depth.
611 *                e.g. the 1st bit indicates if 8-bit should be signed or
612 *                unsigned, the 2nd bit indicates if 16-bit should be signed or
613 *                unsigned, etc... This is useful for formats such as WAVE where
614 *                only 8-bit is unsigned and all other bit depths are signed.
615 * @return        a PCM codec id or AV_CODEC_ID_NONE
616 */
617enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags);
618
619/**
620 * Copy side data from source to destination stream
621 *
622 * @param dst pointer to destination AVStream
623 * @param src pointer to source AVStream
624 * @return >=0 on success, AVERROR code on error
625 */
626int ff_stream_side_data_copy(AVStream *dst, const AVStream *src);
627
628/**
629 * Wrap ffurl_move() and log if error happens.
630 *
631 * @param url_src source path
632 * @param url_dst destination path
633 * @return        0 or AVERROR on failure
634 */
635int ff_rename(const char *url_src, const char *url_dst, void *logctx);
636
637/**
638 * Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end
639 * which is always set to 0.
640 *
641 * Previously allocated extradata in par will be freed.
642 *
643 * @param size size of extradata
644 * @return 0 if OK, AVERROR_xxx on error
645 */
646int ff_alloc_extradata(AVCodecParameters *par, int size);
647
648/**
649 * Copies the whilelists from one context to the other
650 */
651int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src);
652
653/*
654 * A wrapper around AVFormatContext.io_close that should be used
655 * instead of calling the pointer directly.
656 *
657 * @param s AVFormatContext
658 * @param *pb the AVIOContext to be closed and freed. Can be NULL.
659 * @return >=0 on success, negative AVERROR in case of failure
660 */
661int ff_format_io_close(AVFormatContext *s, AVIOContext **pb);
662
663/* Default io_close callback, not to be used directly, use ff_format_io_close
664 * instead. */
665void ff_format_io_close_default(AVFormatContext *s, AVIOContext *pb);
666
667/**
668 * Utility function to check if the file uses http or https protocol
669 *
670 * @param s AVFormatContext
671 * @param filename URL or file name to open for writing
672 */
673int ff_is_http_proto(const char *filename);
674
675struct AVBPrint;
676/**
677 * Finalize buf into extradata and set its size appropriately.
678 */
679int ff_bprint_to_codecpar_extradata(AVCodecParameters *par, struct AVBPrint *buf);
680
681int ff_lock_avformat(void);
682int ff_unlock_avformat(void);
683
684/**
685 * Set AVFormatContext url field to the provided pointer. The pointer must
686 * point to a valid string. The existing url field is freed if necessary. Also
687 * set the legacy filename field to the same string which was provided in url.
688 */
689void ff_format_set_url(AVFormatContext *s, char *url);
690
691void avpriv_register_devices(const AVOutputFormat * const o[], const AVInputFormat * const i[]);
692
693#endif /* AVFORMAT_INTERNAL_H */
694