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.

1136 lines
40KB

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