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.

579 lines
21KB

  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 AVFORMAT_H
  21. #define AVFORMAT_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define LIBAVFORMAT_VERSION_INT ((50<<16)+(5<<8)+0)
  26. #define LIBAVFORMAT_VERSION 50.5.0
  27. #define LIBAVFORMAT_BUILD LIBAVFORMAT_VERSION_INT
  28. #define LIBAVFORMAT_IDENT "Lavf" AV_STRINGIFY(LIBAVFORMAT_VERSION)
  29. #include <time.h>
  30. #include <stdio.h> /* FILE */
  31. #include "avcodec.h"
  32. #include "avio.h"
  33. /* packet functions */
  34. #ifndef MAXINT64
  35. #define MAXINT64 int64_t_C(0x7fffffffffffffff)
  36. #endif
  37. #ifndef MININT64
  38. #define MININT64 int64_t_C(0x8000000000000000)
  39. #endif
  40. typedef struct AVPacket {
  41. int64_t pts; ///< presentation time stamp in time_base units
  42. int64_t dts; ///< decompression time stamp in time_base units
  43. uint8_t *data;
  44. int size;
  45. int stream_index;
  46. int flags;
  47. int duration; ///< presentation duration in time_base units (0 if not available)
  48. void (*destruct)(struct AVPacket *);
  49. void *priv;
  50. int64_t pos; ///< byte position in stream, -1 if unknown
  51. } AVPacket;
  52. #define PKT_FLAG_KEY 0x0001
  53. void av_destruct_packet_nofree(AVPacket *pkt);
  54. void av_destruct_packet(AVPacket *pkt);
  55. /* initialize optional fields of a packet */
  56. static inline void av_init_packet(AVPacket *pkt)
  57. {
  58. pkt->pts = AV_NOPTS_VALUE;
  59. pkt->dts = AV_NOPTS_VALUE;
  60. pkt->pos = -1;
  61. pkt->duration = 0;
  62. pkt->flags = 0;
  63. pkt->stream_index = 0;
  64. pkt->destruct= av_destruct_packet_nofree;
  65. }
  66. int av_new_packet(AVPacket *pkt, int size);
  67. int av_get_packet(ByteIOContext *s, AVPacket *pkt, int size);
  68. int av_dup_packet(AVPacket *pkt);
  69. /**
  70. * Free a packet
  71. *
  72. * @param pkt packet to free
  73. */
  74. static inline void av_free_packet(AVPacket *pkt)
  75. {
  76. if (pkt && pkt->destruct) {
  77. pkt->destruct(pkt);
  78. }
  79. }
  80. /*************************************************/
  81. /* fractional numbers for exact pts handling */
  82. /* the exact value of the fractional number is: 'val + num / den'. num
  83. is assumed to be such as 0 <= num < den */
  84. typedef struct AVFrac {
  85. int64_t val, num, den;
  86. } AVFrac attribute_deprecated;
  87. /*************************************************/
  88. /* input/output formats */
  89. struct AVFormatContext;
  90. /* this structure contains the data a format has to probe a file */
  91. typedef struct AVProbeData {
  92. const char *filename;
  93. unsigned char *buf;
  94. int buf_size;
  95. } AVProbeData;
  96. #define AVPROBE_SCORE_MAX 100 ///< max score, half of that is used for file extension based detection
  97. typedef struct AVFormatParameters {
  98. AVRational time_base;
  99. int sample_rate;
  100. int channels;
  101. int width;
  102. int height;
  103. enum PixelFormat pix_fmt;
  104. struct AVImageFormat *image_format;
  105. int channel; /* used to select dv channel */
  106. const char *device; /* video, audio or DV device */
  107. const char *standard; /* tv standard, NTSC, PAL, SECAM */
  108. int mpeg2ts_raw:1; /* force raw MPEG2 transport stream output, if possible */
  109. int mpeg2ts_compute_pcr:1; /* compute exact PCR for each transport
  110. stream packet (only meaningful if
  111. mpeg2ts_raw is TRUE */
  112. int initial_pause:1; /* do not begin to play the stream
  113. immediately (RTSP only) */
  114. int prealloced_context:1;
  115. enum CodecID video_codec_id;
  116. enum CodecID audio_codec_id;
  117. } AVFormatParameters;
  118. #define AVFMT_NOFILE 0x0001 /* no file should be opened */
  119. #define AVFMT_NEEDNUMBER 0x0002 /* needs '%d' in filename */
  120. #define AVFMT_SHOW_IDS 0x0008 /* show format stream IDs numbers */
  121. #define AVFMT_RAWPICTURE 0x0020 /* format wants AVPicture structure for
  122. raw picture data */
  123. #define AVFMT_GLOBALHEADER 0x0040 /* format wants global header */
  124. #define AVFMT_NOTIMESTAMPS 0x0080 /* format doesnt need / has any timestamps */
  125. typedef struct AVOutputFormat {
  126. const char *name;
  127. const char *long_name;
  128. const char *mime_type;
  129. const char *extensions; /* comma separated extensions */
  130. /* size of private data so that it can be allocated in the wrapper */
  131. int priv_data_size;
  132. /* output support */
  133. enum CodecID audio_codec; /* default audio codec */
  134. enum CodecID video_codec; /* default video codec */
  135. int (*write_header)(struct AVFormatContext *);
  136. int (*write_packet)(struct AVFormatContext *, AVPacket *pkt);
  137. int (*write_trailer)(struct AVFormatContext *);
  138. /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER */
  139. int flags;
  140. /* currently only used to set pixel format if not YUV420P */
  141. int (*set_parameters)(struct AVFormatContext *, AVFormatParameters *);
  142. int (*interleave_packet)(struct AVFormatContext *, AVPacket *out, AVPacket *in, int flush);
  143. /* private fields */
  144. struct AVOutputFormat *next;
  145. } AVOutputFormat;
  146. typedef struct AVInputFormat {
  147. const char *name;
  148. const char *long_name;
  149. /* size of private data so that it can be allocated in the wrapper */
  150. int priv_data_size;
  151. /* tell if a given file has a chance of being parsing by this format */
  152. int (*read_probe)(AVProbeData *);
  153. /* read the format header and initialize the AVFormatContext
  154. structure. Return 0 if OK. 'ap' if non NULL contains
  155. additionnal paramters. Only used in raw format right
  156. now. 'av_new_stream' should be called to create new streams. */
  157. int (*read_header)(struct AVFormatContext *,
  158. AVFormatParameters *ap);
  159. /* read one packet and put it in 'pkt'. pts and flags are also
  160. set. 'av_new_stream' can be called only if the flag
  161. AVFMTCTX_NOHEADER is used. */
  162. int (*read_packet)(struct AVFormatContext *, AVPacket *pkt);
  163. /* close the stream. The AVFormatContext and AVStreams are not
  164. freed by this function */
  165. int (*read_close)(struct AVFormatContext *);
  166. /**
  167. * seek to a given timestamp relative to the frames in
  168. * stream component stream_index
  169. * @param stream_index must not be -1
  170. * @param flags selects which direction should be preferred if no exact
  171. * match is available
  172. */
  173. int (*read_seek)(struct AVFormatContext *,
  174. int stream_index, int64_t timestamp, int flags);
  175. /**
  176. * gets the next timestamp in AV_TIME_BASE units.
  177. */
  178. int64_t (*read_timestamp)(struct AVFormatContext *s, int stream_index,
  179. int64_t *pos, int64_t pos_limit);
  180. /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER */
  181. int flags;
  182. /* if extensions are defined, then no probe is done. You should
  183. usually not use extension format guessing because it is not
  184. reliable enough */
  185. const char *extensions;
  186. /* general purpose read only value that the format can use */
  187. int value;
  188. /* start/resume playing - only meaningful if using a network based format
  189. (RTSP) */
  190. int (*read_play)(struct AVFormatContext *);
  191. /* pause playing - only meaningful if using a network based format
  192. (RTSP) */
  193. int (*read_pause)(struct AVFormatContext *);
  194. /* private fields */
  195. struct AVInputFormat *next;
  196. } AVInputFormat;
  197. typedef struct AVIndexEntry {
  198. int64_t pos;
  199. int64_t timestamp;
  200. #define AVINDEX_KEYFRAME 0x0001
  201. int flags:2;
  202. int size:30; //yeah trying to keep the size of this small to reduce memory requirements (its 24 vs 32 byte due to possible 8byte align)
  203. int min_distance; /* min distance between this and the previous keyframe, used to avoid unneeded searching */
  204. } AVIndexEntry;
  205. typedef struct AVStream {
  206. int index; /* stream index in AVFormatContext */
  207. int id; /* format specific stream id */
  208. AVCodecContext *codec; /* codec context */
  209. /**
  210. * real base frame rate of the stream.
  211. * for example if the timebase is 1/90000 and all frames have either
  212. * approximately 3600 or 1800 timer ticks then r_frame_rate will be 50/1
  213. */
  214. AVRational r_frame_rate;
  215. void *priv_data;
  216. /* internal data used in av_find_stream_info() */
  217. int64_t codec_info_duration;
  218. int codec_info_nb_frames;
  219. /* encoding: PTS generation when outputing stream */
  220. AVFrac pts;
  221. /**
  222. * this is the fundamental unit of time (in seconds) in terms
  223. * of which frame timestamps are represented. for fixed-fps content,
  224. * timebase should be 1/framerate and timestamp increments should be
  225. * identically 1.
  226. */
  227. AVRational time_base;
  228. int pts_wrap_bits; /* number of bits in pts (used for wrapping control) */
  229. /* ffmpeg.c private use */
  230. int stream_copy; /* if TRUE, just copy stream */
  231. enum AVDiscard discard; ///< selects which packets can be discarded at will and dont need to be demuxed
  232. //FIXME move stuff to a flags field?
  233. /* quality, as it has been removed from AVCodecContext and put in AVVideoFrame
  234. * MN:dunno if thats the right place, for it */
  235. float quality;
  236. /* decoding: position of the first frame of the component, in
  237. AV_TIME_BASE fractional seconds. */
  238. int64_t start_time;
  239. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  240. seconds. */
  241. int64_t duration;
  242. char language[4]; /* ISO 639 3-letter language code (empty string if undefined) */
  243. /* av_read_frame() support */
  244. int need_parsing; ///< 1->full parsing needed, 2->only parse headers dont repack
  245. struct AVCodecParserContext *parser;
  246. int64_t cur_dts;
  247. int last_IP_duration;
  248. int64_t last_IP_pts;
  249. /* av_seek_frame() support */
  250. AVIndexEntry *index_entries; /* only used if the format does not
  251. support seeking natively */
  252. int nb_index_entries;
  253. unsigned int index_entries_allocated_size;
  254. int64_t nb_frames; ///< number of frames in this stream if known or 0
  255. #define MAX_REORDER_DELAY 4
  256. int64_t pts_buffer[MAX_REORDER_DELAY+1];
  257. } AVStream;
  258. #define AVFMTCTX_NOHEADER 0x0001 /* signal that no header is present
  259. (streams are added dynamically) */
  260. #define MAX_STREAMS 20
  261. /* format I/O context */
  262. typedef struct AVFormatContext {
  263. const AVClass *av_class; /* set by av_alloc_format_context */
  264. /* can only be iformat or oformat, not both at the same time */
  265. struct AVInputFormat *iformat;
  266. struct AVOutputFormat *oformat;
  267. void *priv_data;
  268. ByteIOContext pb;
  269. int nb_streams;
  270. AVStream *streams[MAX_STREAMS];
  271. char filename[1024]; /* input or output filename */
  272. /* stream info */
  273. int64_t timestamp;
  274. char title[512];
  275. char author[512];
  276. char copyright[512];
  277. char comment[512];
  278. char album[512];
  279. int year; /* ID3 year, 0 if none */
  280. int track; /* track number, 0 if none */
  281. char genre[32]; /* ID3 genre */
  282. int ctx_flags; /* format specific flags, see AVFMTCTX_xx */
  283. /* private data for pts handling (do not modify directly) */
  284. /* This buffer is only needed when packets were already buffered but
  285. not decoded, for example to get the codec parameters in mpeg
  286. streams */
  287. struct AVPacketList *packet_buffer;
  288. /* decoding: position of the first frame of the component, in
  289. AV_TIME_BASE fractional seconds. NEVER set this value directly:
  290. it is deduced from the AVStream values. */
  291. int64_t start_time;
  292. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  293. seconds. NEVER set this value directly: it is deduced from the
  294. AVStream values. */
  295. int64_t duration;
  296. /* decoding: total file size. 0 if unknown */
  297. int64_t file_size;
  298. /* decoding: total stream bitrate in bit/s, 0 if not
  299. available. Never set it directly if the file_size and the
  300. duration are known as ffmpeg can compute it automatically. */
  301. int bit_rate;
  302. /* av_read_frame() support */
  303. AVStream *cur_st;
  304. const uint8_t *cur_ptr;
  305. int cur_len;
  306. AVPacket cur_pkt;
  307. /* av_seek_frame() support */
  308. int64_t data_offset; /* offset of the first packet */
  309. int index_built;
  310. int mux_rate;
  311. int packet_size;
  312. int preload;
  313. int max_delay;
  314. #define AVFMT_NOOUTPUTLOOP -1
  315. #define AVFMT_INFINITEOUTPUTLOOP 0
  316. /* number of times to loop output in formats that support it */
  317. int loop_output;
  318. int flags;
  319. #define AVFMT_FLAG_GENPTS 0x0001 ///< generate pts if missing even if it requires parsing future frames
  320. #define AVFMT_FLAG_IGNIDX 0x0002 ///< ignore index
  321. int loop_input;
  322. /* decoding: size of data to probe; encoding unused */
  323. unsigned int probesize;
  324. } AVFormatContext;
  325. typedef struct AVPacketList {
  326. AVPacket pkt;
  327. struct AVPacketList *next;
  328. } AVPacketList;
  329. extern AVInputFormat *first_iformat;
  330. extern AVOutputFormat *first_oformat;
  331. /* still image support */
  332. struct AVInputImageContext attribute_deprecated;
  333. typedef struct AVInputImageContext AVInputImageContext attribute_deprecated;
  334. typedef struct AVImageInfo {
  335. enum PixelFormat pix_fmt; /* requested pixel format */
  336. int width; /* requested width */
  337. int height; /* requested height */
  338. int interleaved; /* image is interleaved (e.g. interleaved GIF) */
  339. AVPicture pict; /* returned allocated image */
  340. } AVImageInfo attribute_deprecated;
  341. /* AVImageFormat.flags field constants */
  342. #define AVIMAGE_INTERLEAVED 0x0001 /* image format support interleaved output */
  343. typedef struct AVImageFormat {
  344. const char *name;
  345. const char *extensions;
  346. /* tell if a given file has a chance of being parsing by this format */
  347. int (*img_probe)(AVProbeData *);
  348. /* read a whole image. 'alloc_cb' is called when the image size is
  349. known so that the caller can allocate the image. If 'allo_cb'
  350. returns non zero, then the parsing is aborted. Return '0' if
  351. OK. */
  352. int (*img_read)(ByteIOContext *,
  353. int (*alloc_cb)(void *, AVImageInfo *info), void *);
  354. /* write the image */
  355. int supported_pixel_formats; /* mask of supported formats for output */
  356. int (*img_write)(ByteIOContext *, AVImageInfo *);
  357. int flags;
  358. struct AVImageFormat *next;
  359. } AVImageFormat attribute_deprecated;
  360. void av_register_image_format(AVImageFormat *img_fmt) attribute_deprecated;
  361. AVImageFormat *av_probe_image_format(AVProbeData *pd) attribute_deprecated;
  362. AVImageFormat *guess_image_format(const char *filename) attribute_deprecated;
  363. enum CodecID av_guess_image2_codec(const char *filename);
  364. int av_read_image(ByteIOContext *pb, const char *filename,
  365. AVImageFormat *fmt,
  366. int (*alloc_cb)(void *, AVImageInfo *info), void *opaque) attribute_deprecated;
  367. int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img) attribute_deprecated;
  368. extern AVImageFormat *first_image_format attribute_deprecated;
  369. /* XXX: use automatic init with either ELF sections or C file parser */
  370. /* modules */
  371. #include "rtp.h"
  372. #include "rtsp.h"
  373. /* utils.c */
  374. void av_register_input_format(AVInputFormat *format);
  375. void av_register_output_format(AVOutputFormat *format);
  376. AVOutputFormat *guess_stream_format(const char *short_name,
  377. const char *filename, const char *mime_type);
  378. AVOutputFormat *guess_format(const char *short_name,
  379. const char *filename, const char *mime_type);
  380. enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
  381. const char *filename, const char *mime_type, enum CodecType type);
  382. void av_hex_dump(FILE *f, uint8_t *buf, int size);
  383. void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
  384. void av_register_all(void);
  385. /* media file input */
  386. AVInputFormat *av_find_input_format(const char *short_name);
  387. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
  388. int av_open_input_stream(AVFormatContext **ic_ptr,
  389. ByteIOContext *pb, const char *filename,
  390. AVInputFormat *fmt, AVFormatParameters *ap);
  391. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
  392. AVInputFormat *fmt,
  393. int buf_size,
  394. AVFormatParameters *ap);
  395. /* no av_open for output, so applications will need this: */
  396. AVFormatContext *av_alloc_format_context(void);
  397. #define AVERROR_UNKNOWN (-1) /* unknown error */
  398. #define AVERROR_IO (-2) /* i/o error */
  399. #define AVERROR_NUMEXPECTED (-3) /* number syntax expected in filename */
  400. #define AVERROR_INVALIDDATA (-4) /* invalid data found */
  401. #define AVERROR_NOMEM (-5) /* not enough memory */
  402. #define AVERROR_NOFMT (-6) /* unknown format */
  403. #define AVERROR_NOTSUPP (-7) /* operation not supported */
  404. int av_find_stream_info(AVFormatContext *ic);
  405. int av_read_packet(AVFormatContext *s, AVPacket *pkt);
  406. int av_read_frame(AVFormatContext *s, AVPacket *pkt);
  407. int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags);
  408. int av_read_play(AVFormatContext *s);
  409. int av_read_pause(AVFormatContext *s);
  410. void av_close_input_file(AVFormatContext *s);
  411. AVStream *av_new_stream(AVFormatContext *s, int id);
  412. void av_set_pts_info(AVStream *s, int pts_wrap_bits,
  413. int pts_num, int pts_den);
  414. #define AVSEEK_FLAG_BACKWARD 1 ///< seek backward
  415. #define AVSEEK_FLAG_BYTE 2 ///< seeking based on position in bytes
  416. #define AVSEEK_FLAG_ANY 4 ///< seek to any frame, even non keyframes
  417. int av_find_default_stream_index(AVFormatContext *s);
  418. int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags);
  419. int av_add_index_entry(AVStream *st,
  420. int64_t pos, int64_t timestamp, int size, int distance, int flags);
  421. int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags);
  422. void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp);
  423. /* media file output */
  424. int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
  425. int av_write_header(AVFormatContext *s);
  426. int av_write_frame(AVFormatContext *s, AVPacket *pkt);
  427. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
  428. int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush);
  429. int av_write_trailer(AVFormatContext *s);
  430. void dump_format(AVFormatContext *ic,
  431. int index,
  432. const char *url,
  433. int is_output);
  434. int parse_image_size(int *width_ptr, int *height_ptr, const char *str);
  435. int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg);
  436. int64_t parse_date(const char *datestr, int duration);
  437. int64_t av_gettime(void);
  438. /* ffm specific for ffserver */
  439. #define FFM_PACKET_SIZE 4096
  440. offset_t ffm_read_write_index(int fd);
  441. void ffm_write_write_index(int fd, offset_t pos);
  442. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size);
  443. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);
  444. int av_get_frame_filename(char *buf, int buf_size,
  445. const char *path, int number);
  446. int av_filename_number_test(const char *filename);
  447. /* grab specific */
  448. int video_grab_init(void);
  449. int audio_init(void);
  450. /* DV1394 */
  451. int dv1394_init(void);
  452. int dc1394_init(void);
  453. #ifdef HAVE_AV_CONFIG_H
  454. #include "os_support.h"
  455. int strstart(const char *str, const char *val, const char **ptr);
  456. int stristart(const char *str, const char *val, const char **ptr);
  457. void pstrcpy(char *buf, int buf_size, const char *str);
  458. char *pstrcat(char *buf, int buf_size, const char *s);
  459. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem);
  460. #ifdef __GNUC__
  461. #define dynarray_add(tab, nb_ptr, elem)\
  462. do {\
  463. typeof(tab) _tab = (tab);\
  464. typeof(elem) _elem = (elem);\
  465. (void)sizeof(**_tab == _elem); /* check that types are compatible */\
  466. __dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
  467. } while(0)
  468. #else
  469. #define dynarray_add(tab, nb_ptr, elem)\
  470. do {\
  471. __dynarray_add((unsigned long **)(tab), nb_ptr, (unsigned long)(elem));\
  472. } while(0)
  473. #endif
  474. time_t mktimegm(struct tm *tm);
  475. struct tm *brktimegm(time_t secs, struct tm *tm);
  476. const char *small_strptime(const char *p, const char *fmt,
  477. struct tm *dt);
  478. struct in_addr;
  479. int resolve_host(struct in_addr *sin_addr, const char *hostname);
  480. void url_split(char *proto, int proto_size,
  481. char *authorization, int authorization_size,
  482. char *hostname, int hostname_size,
  483. int *port_ptr,
  484. char *path, int path_size,
  485. const char *url);
  486. int match_ext(const char *filename, const char *extensions);
  487. #endif /* HAVE_AV_CONFIG_H */
  488. #ifdef __cplusplus
  489. }
  490. #endif
  491. #endif /* AVFORMAT_H */