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.

1023 lines
36KB

  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 13
  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. #define MAX_STREAMS 20
  343. /**
  344. * format I/O context.
  345. * New fields can be added to the end with minor version bumps.
  346. * Removal, reordering and changes to existing fields require a major
  347. * version bump.
  348. * sizeof(AVFormatContext) must not be used outside libav*.
  349. */
  350. typedef struct AVFormatContext {
  351. const AVClass *av_class; /**< set by av_alloc_format_context */
  352. /* can only be iformat or oformat, not both at the same time */
  353. struct AVInputFormat *iformat;
  354. struct AVOutputFormat *oformat;
  355. void *priv_data;
  356. ByteIOContext *pb;
  357. unsigned int nb_streams;
  358. AVStream *streams[MAX_STREAMS];
  359. char filename[1024]; /**< input or output filename */
  360. /* stream info */
  361. int64_t timestamp;
  362. char title[512];
  363. char author[512];
  364. char copyright[512];
  365. char comment[512];
  366. char album[512];
  367. int year; /**< ID3 year, 0 if none */
  368. int track; /**< track number, 0 if none */
  369. char genre[32]; /**< ID3 genre */
  370. int ctx_flags; /**< format specific flags, see AVFMTCTX_xx */
  371. /* private data for pts handling (do not modify directly) */
  372. /** This buffer is only needed when packets were already buffered but
  373. not decoded, for example to get the codec parameters in mpeg
  374. streams */
  375. struct AVPacketList *packet_buffer;
  376. /** decoding: position of the first frame of the component, in
  377. AV_TIME_BASE fractional seconds. NEVER set this value directly:
  378. it is deduced from the AVStream values. */
  379. int64_t start_time;
  380. /** decoding: duration of the stream, in AV_TIME_BASE fractional
  381. seconds. NEVER set this value directly: it is deduced from the
  382. AVStream values. */
  383. int64_t duration;
  384. /** decoding: total file size. 0 if unknown */
  385. int64_t file_size;
  386. /** decoding: total stream bitrate in bit/s, 0 if not
  387. available. Never set it directly if the file_size and the
  388. duration are known as ffmpeg can compute it automatically. */
  389. int bit_rate;
  390. /* av_read_frame() support */
  391. AVStream *cur_st;
  392. const uint8_t *cur_ptr;
  393. int cur_len;
  394. AVPacket cur_pkt;
  395. /* av_seek_frame() support */
  396. int64_t data_offset; /** offset of the first packet */
  397. int index_built;
  398. int mux_rate;
  399. int packet_size;
  400. int preload;
  401. int max_delay;
  402. #define AVFMT_NOOUTPUTLOOP -1
  403. #define AVFMT_INFINITEOUTPUTLOOP 0
  404. /** number of times to loop output in formats that support it */
  405. int loop_output;
  406. int flags;
  407. #define AVFMT_FLAG_GENPTS 0x0001 ///< generate pts if missing even if it requires parsing future frames
  408. #define AVFMT_FLAG_IGNIDX 0x0002 ///< ignore index
  409. #define AVFMT_FLAG_NONBLOCK 0x0004 ///< do not block when reading packets from input
  410. int loop_input;
  411. /** decoding: size of data to probe; encoding unused */
  412. unsigned int probesize;
  413. /**
  414. * maximum duration in AV_TIME_BASE units over which the input should be analyzed in av_find_stream_info()
  415. */
  416. int max_analyze_duration;
  417. const uint8_t *key;
  418. int keylen;
  419. unsigned int nb_programs;
  420. AVProgram **programs;
  421. /**
  422. * Forced video codec_id.
  423. * demuxing: set by user
  424. */
  425. enum CodecID video_codec_id;
  426. /**
  427. * Forced audio codec_id.
  428. * demuxing: set by user
  429. */
  430. enum CodecID audio_codec_id;
  431. /**
  432. * Forced subtitle codec_id.
  433. * demuxing: set by user
  434. */
  435. enum CodecID subtitle_codec_id;
  436. /**
  437. * Maximum amount of memory in bytes to use per stream for the index.
  438. * If the needed index exceeds this size entries will be discarded as
  439. * needed to maintain a smaller size. This can lead to slower or less
  440. * accurate seeking (depends on demuxer).
  441. * Demuxers for which a full in memory index is mandatory will ignore
  442. * this.
  443. * muxing : unused
  444. * demuxing: set by user
  445. */
  446. unsigned int max_index_size;
  447. /**
  448. * Maximum amount of memory in bytes to use for buffering frames
  449. * obtained from real-time capture devices.
  450. */
  451. unsigned int max_picture_buffer;
  452. } AVFormatContext;
  453. typedef struct AVPacketList {
  454. AVPacket pkt;
  455. struct AVPacketList *next;
  456. } AVPacketList;
  457. #if LIBAVFORMAT_VERSION_INT < (53<<16)
  458. extern AVInputFormat *first_iformat;
  459. extern AVOutputFormat *first_oformat;
  460. #endif
  461. AVInputFormat *av_iformat_next(AVInputFormat *f);
  462. AVOutputFormat *av_oformat_next(AVOutputFormat *f);
  463. enum CodecID av_guess_image2_codec(const char *filename);
  464. /* XXX: use automatic init with either ELF sections or C file parser */
  465. /* modules */
  466. /* utils.c */
  467. void av_register_input_format(AVInputFormat *format);
  468. void av_register_output_format(AVOutputFormat *format);
  469. AVOutputFormat *guess_stream_format(const char *short_name,
  470. const char *filename, const char *mime_type);
  471. AVOutputFormat *guess_format(const char *short_name,
  472. const char *filename, const char *mime_type);
  473. /**
  474. * Guesses the codec id based upon muxer and filename.
  475. */
  476. enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
  477. const char *filename, const char *mime_type, enum CodecType type);
  478. /**
  479. * Send a nice hexadecimal dump of a buffer to the specified file stream.
  480. *
  481. * @param f The file stream pointer where the dump should be sent to.
  482. * @param buf buffer
  483. * @param size buffer size
  484. *
  485. * @see av_hex_dump_log, av_pkt_dump, av_pkt_dump_log
  486. */
  487. void av_hex_dump(FILE *f, uint8_t *buf, int size);
  488. /**
  489. * Send a nice hexadecimal dump of a buffer to the log.
  490. *
  491. * @param avcl A pointer to an arbitrary struct of which the first field is a
  492. * pointer to an AVClass struct.
  493. * @param level The importance level of the message, lower values signifying
  494. * higher importance.
  495. * @param buf buffer
  496. * @param size buffer size
  497. *
  498. * @see av_hex_dump, av_pkt_dump, av_pkt_dump_log
  499. */
  500. void av_hex_dump_log(void *avcl, int level, uint8_t *buf, int size);
  501. /**
  502. * Send a nice dump of a packet to the specified file stream.
  503. *
  504. * @param f The file stream pointer where the dump should be sent to.
  505. * @param pkt packet to dump
  506. * @param dump_payload true if the payload must be displayed too
  507. */
  508. void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
  509. /**
  510. * Send a nice dump of a packet to the log.
  511. *
  512. * @param avcl A pointer to an arbitrary struct of which the first field is a
  513. * pointer to an AVClass struct.
  514. * @param level The importance level of the message, lower values signifying
  515. * higher importance.
  516. * @param pkt packet to dump
  517. * @param dump_payload true if the payload must be displayed too
  518. */
  519. void av_pkt_dump_log(void *avcl, int level, AVPacket *pkt, int dump_payload);
  520. void av_register_all(void);
  521. /** codec tag <-> codec id */
  522. enum CodecID av_codec_get_id(const struct AVCodecTag **tags, unsigned int tag);
  523. unsigned int av_codec_get_tag(const struct AVCodecTag **tags, enum CodecID id);
  524. /* media file input */
  525. /**
  526. * finds AVInputFormat based on input format's short name.
  527. */
  528. AVInputFormat *av_find_input_format(const char *short_name);
  529. /**
  530. * Guess file format.
  531. *
  532. * @param is_opened whether the file is already opened, determines whether
  533. * demuxers with or without AVFMT_NOFILE are probed
  534. */
  535. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
  536. /**
  537. * Allocates all the structures needed to read an input stream.
  538. * This does not open the needed codecs for decoding the stream[s].
  539. */
  540. int av_open_input_stream(AVFormatContext **ic_ptr,
  541. ByteIOContext *pb, const char *filename,
  542. AVInputFormat *fmt, AVFormatParameters *ap);
  543. /**
  544. * Open a media file as input. The codecs are not opened. Only the file
  545. * header (if present) is read.
  546. *
  547. * @param ic_ptr the opened media file handle is put here
  548. * @param filename filename to open.
  549. * @param fmt if non NULL, force the file format to use
  550. * @param buf_size optional buffer size (zero if default is OK)
  551. * @param ap additional parameters needed when opening the file (NULL if default)
  552. * @return 0 if OK. AVERROR_xxx otherwise.
  553. */
  554. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
  555. AVInputFormat *fmt,
  556. int buf_size,
  557. AVFormatParameters *ap);
  558. /**
  559. * Allocate an AVFormatContext.
  560. * Can be freed with av_free() but do not forget to free everything you
  561. * explicitly allocated as well!
  562. */
  563. AVFormatContext *av_alloc_format_context(void);
  564. /**
  565. * Read packets of a media file to get stream information. This
  566. * is useful for file formats with no headers such as MPEG. This
  567. * function also computes the real frame rate in case of mpeg2 repeat
  568. * frame mode.
  569. * The logical file position is not changed by this function;
  570. * examined packets may be buffered for later processing.
  571. *
  572. * @param ic media file handle
  573. * @return >=0 if OK. AVERROR_xxx if error.
  574. * @todo Let user decide somehow what information is needed so we do not waste time getting stuff the user does not need.
  575. */
  576. int av_find_stream_info(AVFormatContext *ic);
  577. /**
  578. * Read a transport packet from a media file.
  579. *
  580. * This function is obsolete and should never be used.
  581. * Use av_read_frame() instead.
  582. *
  583. * @param s media file handle
  584. * @param pkt is filled
  585. * @return 0 if OK. AVERROR_xxx if error.
  586. */
  587. int av_read_packet(AVFormatContext *s, AVPacket *pkt);
  588. /**
  589. * Return the next frame of a stream.
  590. *
  591. * The returned packet is valid
  592. * until the next av_read_frame() or until av_close_input_file() and
  593. * must be freed with av_free_packet. For video, the packet contains
  594. * exactly one frame. For audio, it contains an integer number of
  595. * frames if each frame has a known fixed size (e.g. PCM or ADPCM
  596. * data). If the audio frames have a variable size (e.g. MPEG audio),
  597. * then it contains one frame.
  598. *
  599. * pkt->pts, pkt->dts and pkt->duration are always set to correct
  600. * values in AVStream.timebase units (and guessed if the format cannot
  601. * provided them). pkt->pts can be AV_NOPTS_VALUE if the video format
  602. * has B frames, so it is better to rely on pkt->dts if you do not
  603. * decompress the payload.
  604. *
  605. * @return 0 if OK, < 0 if error or end of file.
  606. */
  607. int av_read_frame(AVFormatContext *s, AVPacket *pkt);
  608. /**
  609. * Seek to the key frame at timestamp.
  610. * 'timestamp' in 'stream_index'.
  611. * @param stream_index If stream_index is (-1), a default
  612. * stream is selected, and timestamp is automatically converted
  613. * from AV_TIME_BASE units to the stream specific time_base.
  614. * @param timestamp timestamp in AVStream.time_base units
  615. * or if there is no stream specified then in AV_TIME_BASE units
  616. * @param flags flags which select direction and seeking mode
  617. * @return >= 0 on success
  618. */
  619. int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags);
  620. /**
  621. * start playing a network based stream (e.g. RTSP stream) at the
  622. * current position
  623. */
  624. int av_read_play(AVFormatContext *s);
  625. /**
  626. * Pause a network based stream (e.g. RTSP stream).
  627. *
  628. * Use av_read_play() to resume it.
  629. */
  630. int av_read_pause(AVFormatContext *s);
  631. /**
  632. * Free a AVFormatContext allocated by av_open_input_stream.
  633. * @param s context to free
  634. */
  635. void av_close_input_stream(AVFormatContext *s);
  636. /**
  637. * Close a media file (but not its codecs).
  638. *
  639. * @param s media file handle
  640. */
  641. void av_close_input_file(AVFormatContext *s);
  642. /**
  643. * Add a new stream to a media file.
  644. *
  645. * Can only be called in the read_header() function. If the flag
  646. * AVFMTCTX_NOHEADER is in the format context, then new streams
  647. * can be added in read_packet too.
  648. *
  649. * @param s media file handle
  650. * @param id file format dependent stream id
  651. */
  652. AVStream *av_new_stream(AVFormatContext *s, int id);
  653. AVProgram *av_new_program(AVFormatContext *s, int id);
  654. /**
  655. * Set the pts for a given stream.
  656. *
  657. * @param s stream
  658. * @param pts_wrap_bits number of bits effectively used by the pts
  659. * (used for wrap control, 33 is the value for MPEG)
  660. * @param pts_num numerator to convert to seconds (MPEG: 1)
  661. * @param pts_den denominator to convert to seconds (MPEG: 90000)
  662. */
  663. void av_set_pts_info(AVStream *s, int pts_wrap_bits,
  664. int pts_num, int pts_den);
  665. #define AVSEEK_FLAG_BACKWARD 1 ///< seek backward
  666. #define AVSEEK_FLAG_BYTE 2 ///< seeking based on position in bytes
  667. #define AVSEEK_FLAG_ANY 4 ///< seek to any frame, even non keyframes
  668. int av_find_default_stream_index(AVFormatContext *s);
  669. /**
  670. * Gets the index for a specific timestamp.
  671. * @param flags if AVSEEK_FLAG_BACKWARD then the returned index will correspond to
  672. * the timestamp which is <= the requested one, if backward is 0
  673. * then it will be >=
  674. * if AVSEEK_FLAG_ANY seek to any frame, only keyframes otherwise
  675. * @return < 0 if no such timestamp could be found
  676. */
  677. int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags);
  678. /**
  679. * Ensures the index uses less memory than the maximum specified in
  680. * AVFormatContext.max_index_size, by discarding entries if it grows
  681. * too large.
  682. * This function is not part of the public API and should only be called
  683. * by demuxers.
  684. */
  685. void ff_reduce_index(AVFormatContext *s, int stream_index);
  686. /**
  687. * Add a index entry into a sorted list updateing if it is already there.
  688. *
  689. * @param timestamp timestamp in the timebase of the given stream
  690. */
  691. int av_add_index_entry(AVStream *st,
  692. int64_t pos, int64_t timestamp, int size, int distance, int flags);
  693. /**
  694. * Does a binary search using av_index_search_timestamp() and AVCodec.read_timestamp().
  695. * This is not supposed to be called directly by a user application, but by demuxers.
  696. * @param target_ts target timestamp in the time base of the given stream
  697. * @param stream_index stream number
  698. */
  699. int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags);
  700. /**
  701. * Updates cur_dts of all streams based on given timestamp and AVStream.
  702. *
  703. * Stream ref_st unchanged, others set cur_dts in their native timebase
  704. * only needed for timestamp wrapping or if (dts not set and pts!=dts).
  705. * @param timestamp new dts expressed in time_base of param ref_st
  706. * @param ref_st reference stream giving time_base of param timestamp
  707. */
  708. void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp);
  709. /**
  710. * Does a binary search using read_timestamp().
  711. * This is not supposed to be called directly by a user application, but by demuxers.
  712. * @param target_ts target timestamp in the time base of the given stream
  713. * @param stream_index stream number
  714. */
  715. 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 ));
  716. /** media file output */
  717. int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
  718. /**
  719. * Allocate the stream private data and write the stream header to an
  720. * output media file.
  721. *
  722. * @param s media file handle
  723. * @return 0 if OK. AVERROR_xxx if error.
  724. */
  725. int av_write_header(AVFormatContext *s);
  726. /**
  727. * Write a packet to an output media file.
  728. *
  729. * The packet shall contain one audio or video frame.
  730. * The packet must be correctly interleaved according to the container specification,
  731. * if not then av_interleaved_write_frame must be used
  732. *
  733. * @param s media file handle
  734. * @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ...
  735. * @return < 0 if error, = 0 if OK, 1 if end of stream wanted.
  736. */
  737. int av_write_frame(AVFormatContext *s, AVPacket *pkt);
  738. /**
  739. * Writes a packet to an output media file ensuring correct interleaving.
  740. *
  741. * The packet must contain one audio or video frame.
  742. * If the packets are already correctly interleaved the application should
  743. * call av_write_frame() instead as it is slightly faster. It is also important
  744. * to keep in mind that completely non-interleaved input will need huge amounts
  745. * of memory to interleave with this, so it is preferable to interleave at the
  746. * demuxer level.
  747. *
  748. * @param s media file handle
  749. * @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ...
  750. * @return < 0 if error, = 0 if OK, 1 if end of stream wanted.
  751. */
  752. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
  753. /**
  754. * Interleave a packet per DTS in an output media file.
  755. *
  756. * Packets with pkt->destruct == av_destruct_packet will be freed inside this function,
  757. * so they cannot be used after it, note calling av_free_packet() on them is still safe.
  758. *
  759. * @param s media file handle
  760. * @param out the interleaved packet will be output here
  761. * @param in the input packet
  762. * @param flush 1 if no further packets are available as input and all
  763. * remaining packets should be output
  764. * @return 1 if a packet was output, 0 if no packet could be output,
  765. * < 0 if an error occurred
  766. */
  767. int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush);
  768. /**
  769. * @brief Write the stream trailer to an output media file and
  770. * free the file private data.
  771. *
  772. * @param s media file handle
  773. * @return 0 if OK. AVERROR_xxx if error.
  774. */
  775. int av_write_trailer(AVFormatContext *s);
  776. void dump_format(AVFormatContext *ic,
  777. int index,
  778. const char *url,
  779. int is_output);
  780. /**
  781. * parses width and height out of string str.
  782. * @deprecated Use av_parse_video_frame_size instead.
  783. */
  784. attribute_deprecated int parse_image_size(int *width_ptr, int *height_ptr, const char *str);
  785. /**
  786. * Converts frame rate from string to a fraction.
  787. * @deprecated Use av_parse_video_frame_rate instead.
  788. */
  789. attribute_deprecated int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg);
  790. /**
  791. * Parses \p datestr and returns a corresponding number of microseconds.
  792. * @param datestr String representing a date or a duration.
  793. * - If a date the syntax is:
  794. * @code
  795. * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]}
  796. * @endcode
  797. * Time is localtime unless Z is appended, in which case it is
  798. * interpreted as UTC.
  799. * If the year-month-day part isn't specified it takes the current
  800. * year-month-day.
  801. * Returns the number of microseconds since 1st of January, 1970 up to
  802. * the time of the parsed date or INT64_MIN if \p datestr cannot be
  803. * successfully parsed.
  804. * - If a duration the syntax is:
  805. * @code
  806. * [-]HH[:MM[:SS[.m...]]]
  807. * [-]S+[.m...]
  808. * @endcode
  809. * Returns the number of microseconds contained in a time interval
  810. * with the specified duration or INT64_MIN if \p datestr cannot be
  811. * successfully parsed.
  812. * @param duration Flag which tells how to interpret \p datestr, if
  813. * not zero \p datestr is interpreted as a duration, otherwise as a
  814. * date.
  815. */
  816. int64_t parse_date(const char *datestr, int duration);
  817. int64_t av_gettime(void);
  818. /* ffm specific for ffserver */
  819. #define FFM_PACKET_SIZE 4096
  820. offset_t ffm_read_write_index(int fd);
  821. void ffm_write_write_index(int fd, offset_t pos);
  822. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size);
  823. /**
  824. * Attempts to find a specific tag in a URL.
  825. *
  826. * syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done.
  827. * Return 1 if found.
  828. */
  829. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);
  830. /**
  831. * Returns in 'buf' the path with '%d' replaced by number.
  832. * Also handles the '%0nd' format where 'n' is the total number
  833. * of digits and '%%'.
  834. *
  835. * @param buf destination buffer
  836. * @param buf_size destination buffer size
  837. * @param path numbered sequence string
  838. * @param number frame number
  839. * @return 0 if OK, -1 if format error.
  840. */
  841. int av_get_frame_filename(char *buf, int buf_size,
  842. const char *path, int number);
  843. /**
  844. * Check whether filename actually is a numbered sequence generator.
  845. *
  846. * @param filename possible numbered sequence string
  847. * @return 1 if a valid numbered sequence string, 0 otherwise.
  848. */
  849. int av_filename_number_test(const char *filename);
  850. /**
  851. * Generate an SDP for an RTP session.
  852. *
  853. * @param ac array of AVFormatContexts describing the RTP streams. If the
  854. * array is composed by only one context, such context can contain
  855. * multiple AVStreams (one AVStream per RTP stream). Otherwise,
  856. * all the contexts in the array (an AVCodecContext per RTP stream)
  857. * must contain only one AVStream
  858. * @param n_files number of AVCodecContexts contained in ac
  859. * @param buff buffer where the SDP will be stored (must be allocated by
  860. * the caller
  861. * @param size the size of the buffer
  862. * @return 0 if OK. AVERROR_xxx if error.
  863. */
  864. int avf_sdp_create(AVFormatContext *ac[], int n_files, char *buff, int size);
  865. #ifdef HAVE_AV_CONFIG_H
  866. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem);
  867. #ifdef __GNUC__
  868. #define dynarray_add(tab, nb_ptr, elem)\
  869. do {\
  870. typeof(tab) _tab = (tab);\
  871. typeof(elem) _elem = (elem);\
  872. (void)sizeof(**_tab == _elem); /* check that types are compatible */\
  873. __dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
  874. } while(0)
  875. #else
  876. #define dynarray_add(tab, nb_ptr, elem)\
  877. do {\
  878. __dynarray_add((unsigned long **)(tab), nb_ptr, (unsigned long)(elem));\
  879. } while(0)
  880. #endif
  881. time_t mktimegm(struct tm *tm);
  882. struct tm *brktimegm(time_t secs, struct tm *tm);
  883. const char *small_strptime(const char *p, const char *fmt,
  884. struct tm *dt);
  885. struct in_addr;
  886. int resolve_host(struct in_addr *sin_addr, const char *hostname);
  887. void url_split(char *proto, int proto_size,
  888. char *authorization, int authorization_size,
  889. char *hostname, int hostname_size,
  890. int *port_ptr,
  891. char *path, int path_size,
  892. const char *url);
  893. int match_ext(const char *filename, const char *extensions);
  894. #endif /* HAVE_AV_CONFIG_H */
  895. #endif /* FFMPEG_AVFORMAT_H */