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.

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