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.

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