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.

1008 lines
35KB

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