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.

1063 lines
37KB

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