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.

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