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.

1237 lines
43KB

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