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.

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