You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1207 lines
42KB

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