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.

643 lines
20KB

  1. #ifndef AVFORMAT_H
  2. #define AVFORMAT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #define LIBAVFORMAT_BUILD 4611
  7. #define LIBAVFORMAT_VERSION_INT FFMPEG_VERSION_INT
  8. #define LIBAVFORMAT_VERSION FFMPEG_VERSION
  9. #define LIBAVFORMAT_IDENT "FFmpeg" FFMPEG_VERSION "b" AV_STRINGIFY(LIBAVFORMAT_BUILD)
  10. #include <time.h>
  11. #include <stdio.h> /* FILE */
  12. #include "avcodec.h"
  13. #include "avio.h"
  14. /* packet functions */
  15. #ifndef MAXINT64
  16. #define MAXINT64 int64_t_C(0x7fffffffffffffff)
  17. #endif
  18. #ifndef MININT64
  19. #define MININT64 int64_t_C(0x8000000000000000)
  20. #endif
  21. typedef struct AVPacket {
  22. int64_t pts; /* presentation time stamp in AV_TIME_BASE units (or
  23. pts_den units in muxers or demuxers) */
  24. int64_t dts; /* decompression time stamp in AV_TIME_BASE units (or
  25. pts_den units in muxers or demuxers) */
  26. uint8_t *data;
  27. int size;
  28. int stream_index;
  29. int flags;
  30. int duration; /* presentation duration (0 if not available) */
  31. void (*destruct)(struct AVPacket *);
  32. void *priv;
  33. } AVPacket;
  34. #define PKT_FLAG_KEY 0x0001
  35. /* initialize optional fields of a packet */
  36. static inline void av_init_packet(AVPacket *pkt)
  37. {
  38. pkt->pts = AV_NOPTS_VALUE;
  39. pkt->dts = AV_NOPTS_VALUE;
  40. pkt->duration = 0;
  41. pkt->flags = 0;
  42. pkt->stream_index = 0;
  43. }
  44. int av_new_packet(AVPacket *pkt, int size);
  45. int av_dup_packet(AVPacket *pkt);
  46. /**
  47. * Free a packet
  48. *
  49. * @param pkt packet to free
  50. */
  51. static inline void av_free_packet(AVPacket *pkt)
  52. {
  53. if (pkt && pkt->destruct) {
  54. pkt->destruct(pkt);
  55. }
  56. }
  57. /*************************************************/
  58. /* fractional numbers for exact pts handling */
  59. /* the exact value of the fractional number is: 'val + num / den'. num
  60. is assumed to be such as 0 <= num < den */
  61. typedef struct AVFrac {
  62. int64_t val, num, den;
  63. } AVFrac;
  64. void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den);
  65. void av_frac_add(AVFrac *f, int64_t incr);
  66. void av_frac_set(AVFrac *f, int64_t val);
  67. /*************************************************/
  68. /* input/output formats */
  69. struct AVFormatContext;
  70. /* this structure contains the data a format has to probe a file */
  71. typedef struct AVProbeData {
  72. const char *filename;
  73. unsigned char *buf;
  74. int buf_size;
  75. } AVProbeData;
  76. #define AVPROBE_SCORE_MAX 100
  77. typedef struct AVFormatParameters {
  78. int frame_rate;
  79. int frame_rate_base;
  80. int sample_rate;
  81. int channels;
  82. int width;
  83. int height;
  84. enum PixelFormat pix_fmt;
  85. struct AVImageFormat *image_format;
  86. int channel; /* used to select dv channel */
  87. const char *device; /* video4linux, audio or DV device */
  88. const char *standard; /* tv standard, NTSC, PAL, SECAM */
  89. int mpeg2ts_raw:1; /* force raw MPEG2 transport stream output, if possible */
  90. int mpeg2ts_compute_pcr:1; /* compute exact PCR for each transport
  91. stream packet (only meaningful if
  92. mpeg2ts_raw is TRUE */
  93. int initial_pause:1; /* do not begin to play the stream
  94. immediately (RTSP only) */
  95. } AVFormatParameters;
  96. #define AVFMT_NOFILE 0x0001 /* no file should be opened */
  97. #define AVFMT_NEEDNUMBER 0x0002 /* needs '%d' in filename */
  98. #define AVFMT_SHOW_IDS 0x0008 /* show format stream IDs numbers */
  99. #define AVFMT_RAWPICTURE 0x0020 /* format wants AVPicture structure for
  100. raw picture data */
  101. typedef struct AVOutputFormat {
  102. const char *name;
  103. const char *long_name;
  104. const char *mime_type;
  105. const char *extensions; /* comma separated extensions */
  106. /* size of private data so that it can be allocated in the wrapper */
  107. int priv_data_size;
  108. /* output support */
  109. enum CodecID audio_codec; /* default audio codec */
  110. enum CodecID video_codec; /* default video codec */
  111. int (*write_header)(struct AVFormatContext *);
  112. int (*write_packet)(struct AVFormatContext *,
  113. int stream_index,
  114. const uint8_t *buf, int size, int64_t pts);
  115. int (*write_trailer)(struct AVFormatContext *);
  116. /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER */
  117. int flags;
  118. /* currently only used to set pixel format if not YUV420P */
  119. int (*set_parameters)(struct AVFormatContext *, AVFormatParameters *);
  120. /* private fields */
  121. struct AVOutputFormat *next;
  122. } AVOutputFormat;
  123. typedef struct AVInputFormat {
  124. const char *name;
  125. const char *long_name;
  126. /* size of private data so that it can be allocated in the wrapper */
  127. int priv_data_size;
  128. /* tell if a given file has a chance of being parsing by this format */
  129. int (*read_probe)(AVProbeData *);
  130. /* read the format header and initialize the AVFormatContext
  131. structure. Return 0 if OK. 'ap' if non NULL contains
  132. additionnal paramters. Only used in raw format right
  133. now. 'av_new_stream' should be called to create new streams. */
  134. int (*read_header)(struct AVFormatContext *,
  135. AVFormatParameters *ap);
  136. /* read one packet and put it in 'pkt'. pts and flags are also
  137. set. 'av_new_stream' can be called only if the flag
  138. AVFMTCTX_NOHEADER is used. */
  139. int (*read_packet)(struct AVFormatContext *, AVPacket *pkt);
  140. /* close the stream. The AVFormatContext and AVStreams are not
  141. freed by this function */
  142. int (*read_close)(struct AVFormatContext *);
  143. /* seek at or before a given timestamp (given in AV_TIME_BASE
  144. units) relative to the frames in stream component stream_index */
  145. int (*read_seek)(struct AVFormatContext *,
  146. int stream_index, int64_t timestamp);
  147. /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER */
  148. int flags;
  149. /* if extensions are defined, then no probe is done. You should
  150. usually not use extension format guessing because it is not
  151. reliable enough */
  152. const char *extensions;
  153. /* general purpose read only value that the format can use */
  154. int value;
  155. /* start/resume playing - only meaningful if using a network based format
  156. (RTSP) */
  157. int (*read_play)(struct AVFormatContext *);
  158. /* pause playing - only meaningful if using a network based format
  159. (RTSP) */
  160. int (*read_pause)(struct AVFormatContext *);
  161. /* private fields */
  162. struct AVInputFormat *next;
  163. } AVInputFormat;
  164. typedef struct AVIndexEntry {
  165. int64_t pos;
  166. int64_t timestamp;
  167. #define AVINDEX_KEYFRAME 0x0001
  168. /* the following 2 flags indicate that the next/prev keyframe is known, and scaning for it isnt needed */
  169. int flags;
  170. int min_distance; /* min distance between this and the previous keyframe, used to avoid unneeded searching */
  171. } AVIndexEntry;
  172. typedef struct AVStream {
  173. int index; /* stream index in AVFormatContext */
  174. int id; /* format specific stream id */
  175. AVCodecContext codec; /* codec context */
  176. int r_frame_rate; /* real frame rate of the stream */
  177. int r_frame_rate_base;/* real frame rate base of the stream */
  178. void *priv_data;
  179. /* internal data used in av_find_stream_info() */
  180. int64_t codec_info_duration;
  181. int codec_info_nb_frames;
  182. /* encoding: PTS generation when outputing stream */
  183. AVFrac pts;
  184. /* ffmpeg.c private use */
  185. int stream_copy; /* if TRUE, just copy stream */
  186. /* quality, as it has been removed from AVCodecContext and put in AVVideoFrame
  187. * MN:dunno if thats the right place, for it */
  188. float quality;
  189. /* decoding: position of the first frame of the component, in
  190. AV_TIME_BASE fractional seconds. */
  191. int64_t start_time;
  192. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  193. seconds. */
  194. int64_t duration;
  195. /* av_read_frame() support */
  196. int need_parsing;
  197. struct AVCodecParserContext *parser;
  198. int64_t cur_dts;
  199. int last_IP_duration;
  200. /* av_seek_frame() support */
  201. AVIndexEntry *index_entries; /* only used if the format does not
  202. support seeking natively */
  203. int nb_index_entries;
  204. int index_entries_allocated_size;
  205. } AVStream;
  206. #define AVFMTCTX_NOHEADER 0x0001 /* signal that no header is present
  207. (streams are added dynamically) */
  208. #define MAX_STREAMS 20
  209. /* format I/O context */
  210. typedef struct AVFormatContext {
  211. AVClass *av_class; /* set by av_alloc_format_context */
  212. /* can only be iformat or oformat, not both at the same time */
  213. struct AVInputFormat *iformat;
  214. struct AVOutputFormat *oformat;
  215. void *priv_data;
  216. ByteIOContext pb;
  217. int nb_streams;
  218. AVStream *streams[MAX_STREAMS];
  219. char filename[1024]; /* input or output filename */
  220. /* stream info */
  221. int64_t timestamp;
  222. char title[512];
  223. char author[512];
  224. char copyright[512];
  225. char comment[512];
  226. char album[512];
  227. int year; /* ID3 year, 0 if none */
  228. int track; /* track number, 0 if none */
  229. char genre[32]; /* ID3 genre */
  230. int ctx_flags; /* format specific flags, see AVFMTCTX_xx */
  231. /* private data for pts handling (do not modify directly) */
  232. int pts_wrap_bits; /* number of bits in pts (used for wrapping control) */
  233. int pts_num, pts_den; /* value to convert to seconds */
  234. /* This buffer is only needed when packets were already buffered but
  235. not decoded, for example to get the codec parameters in mpeg
  236. streams */
  237. struct AVPacketList *packet_buffer;
  238. /* decoding: position of the first frame of the component, in
  239. AV_TIME_BASE fractional seconds. NEVER set this value directly:
  240. it is deduced from the AVStream values. */
  241. int64_t start_time;
  242. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  243. seconds. NEVER set this value directly: it is deduced from the
  244. AVStream values. */
  245. int64_t duration;
  246. /* decoding: total file size. 0 if unknown */
  247. int64_t file_size;
  248. /* decoding: total stream bitrate in bit/s, 0 if not
  249. available. Never set it directly if the file_size and the
  250. duration are known as ffmpeg can compute it automatically. */
  251. int bit_rate;
  252. /* av_read_frame() support */
  253. AVStream *cur_st;
  254. const uint8_t *cur_ptr;
  255. int cur_len;
  256. AVPacket cur_pkt;
  257. /* the following are used for pts/dts unit conversion */
  258. int64_t last_pkt_stream_pts;
  259. int64_t last_pkt_stream_dts;
  260. int64_t last_pkt_pts;
  261. int64_t last_pkt_dts;
  262. int last_pkt_pts_frac;
  263. int last_pkt_dts_frac;
  264. /* av_seek_frame() support */
  265. int64_t data_offset; /* offset of the first packet */
  266. int index_built;
  267. } AVFormatContext;
  268. typedef struct AVPacketList {
  269. AVPacket pkt;
  270. struct AVPacketList *next;
  271. } AVPacketList;
  272. extern AVInputFormat *first_iformat;
  273. extern AVOutputFormat *first_oformat;
  274. /* still image support */
  275. struct AVInputImageContext;
  276. typedef struct AVInputImageContext AVInputImageContext;
  277. typedef struct AVImageInfo {
  278. enum PixelFormat pix_fmt; /* requested pixel format */
  279. int width; /* requested width */
  280. int height; /* requested height */
  281. int interleaved; /* image is interleaved (e.g. interleaved GIF) */
  282. AVPicture pict; /* returned allocated image */
  283. } AVImageInfo;
  284. /* AVImageFormat.flags field constants */
  285. #define AVIMAGE_INTERLEAVED 0x0001 /* image format support interleaved output */
  286. typedef struct AVImageFormat {
  287. const char *name;
  288. const char *extensions;
  289. /* tell if a given file has a chance of being parsing by this format */
  290. int (*img_probe)(AVProbeData *);
  291. /* read a whole image. 'alloc_cb' is called when the image size is
  292. known so that the caller can allocate the image. If 'allo_cb'
  293. returns non zero, then the parsing is aborted. Return '0' if
  294. OK. */
  295. int (*img_read)(ByteIOContext *,
  296. int (*alloc_cb)(void *, AVImageInfo *info), void *);
  297. /* write the image */
  298. int supported_pixel_formats; /* mask of supported formats for output */
  299. int (*img_write)(ByteIOContext *, AVImageInfo *);
  300. int flags;
  301. struct AVImageFormat *next;
  302. } AVImageFormat;
  303. void av_register_image_format(AVImageFormat *img_fmt);
  304. AVImageFormat *av_probe_image_format(AVProbeData *pd);
  305. AVImageFormat *guess_image_format(const char *filename);
  306. int av_read_image(ByteIOContext *pb, const char *filename,
  307. AVImageFormat *fmt,
  308. int (*alloc_cb)(void *, AVImageInfo *info), void *opaque);
  309. int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img);
  310. extern AVImageFormat *first_image_format;
  311. extern AVImageFormat pnm_image_format;
  312. extern AVImageFormat pbm_image_format;
  313. extern AVImageFormat pgm_image_format;
  314. extern AVImageFormat ppm_image_format;
  315. extern AVImageFormat pam_image_format;
  316. extern AVImageFormat pgmyuv_image_format;
  317. extern AVImageFormat yuv_image_format;
  318. #ifdef CONFIG_ZLIB
  319. extern AVImageFormat png_image_format;
  320. #endif
  321. extern AVImageFormat jpeg_image_format;
  322. extern AVImageFormat gif_image_format;
  323. extern AVImageFormat sgi_image_format;
  324. /* XXX: use automatic init with either ELF sections or C file parser */
  325. /* modules */
  326. /* mpeg.c */
  327. extern AVInputFormat mpegps_demux;
  328. int mpegps_init(void);
  329. /* mpegts.c */
  330. extern AVInputFormat mpegts_demux;
  331. int mpegts_init(void);
  332. /* rm.c */
  333. int rm_init(void);
  334. /* crc.c */
  335. int crc_init(void);
  336. /* img.c */
  337. int img_init(void);
  338. /* asf.c */
  339. int asf_init(void);
  340. /* avienc.c */
  341. int avienc_init(void);
  342. /* avidec.c */
  343. int avidec_init(void);
  344. /* swf.c */
  345. int swf_init(void);
  346. /* mov.c */
  347. int mov_init(void);
  348. /* movenc.c */
  349. int movenc_init(void);
  350. /* flvenc.c */
  351. int flvenc_init(void);
  352. /* flvdec.c */
  353. int flvdec_init(void);
  354. /* jpeg.c */
  355. int jpeg_init(void);
  356. /* gif.c */
  357. int gif_init(void);
  358. /* au.c */
  359. int au_init(void);
  360. /* amr.c */
  361. int amr_init(void);
  362. /* wav.c */
  363. int ff_wav_init(void);
  364. /* raw.c */
  365. int pcm_read_seek(AVFormatContext *s,
  366. int stream_index, int64_t timestamp);
  367. int raw_init(void);
  368. /* mp3.c */
  369. int mp3_init(void);
  370. /* yuv4mpeg.c */
  371. int yuv4mpeg_init(void);
  372. /* ogg.c */
  373. int ogg_init(void);
  374. /* dv.c */
  375. int ff_dv_init(void);
  376. /* ffm.c */
  377. int ffm_init(void);
  378. /* rtsp.c */
  379. extern AVInputFormat redir_demux;
  380. int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f);
  381. /* 4xm.c */
  382. int fourxm_init(void);
  383. /* psxstr.c */
  384. int str_init(void);
  385. /* idroq.c */
  386. int roq_init(void);
  387. /* ipmovie.c */
  388. int ipmovie_init(void);
  389. /* nut.c */
  390. int nut_init(void);
  391. /* wc3movie.c */
  392. int wc3_init(void);
  393. /* westwood.c */
  394. int westwood_init(void);
  395. /* segafilm.c */
  396. int film_init(void);
  397. /* idcin.c */
  398. int idcin_init(void);
  399. /* flic.c */
  400. int flic_init(void);
  401. /* sierravmd.c */
  402. int vmd_init(void);
  403. /* matroska.c */
  404. int matroska_init(void);
  405. #include "rtp.h"
  406. #include "rtsp.h"
  407. /* yuv4mpeg.c */
  408. extern AVOutputFormat yuv4mpegpipe_oformat;
  409. /* utils.c */
  410. void av_register_input_format(AVInputFormat *format);
  411. void av_register_output_format(AVOutputFormat *format);
  412. AVOutputFormat *guess_stream_format(const char *short_name,
  413. const char *filename, const char *mime_type);
  414. AVOutputFormat *guess_format(const char *short_name,
  415. const char *filename, const char *mime_type);
  416. void av_hex_dump(FILE *f, uint8_t *buf, int size);
  417. void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
  418. void av_register_all(void);
  419. typedef struct FifoBuffer {
  420. uint8_t *buffer;
  421. uint8_t *rptr, *wptr, *end;
  422. } FifoBuffer;
  423. int fifo_init(FifoBuffer *f, int size);
  424. void fifo_free(FifoBuffer *f);
  425. int fifo_size(FifoBuffer *f, uint8_t *rptr);
  426. int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr);
  427. void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr);
  428. /* media file input */
  429. AVInputFormat *av_find_input_format(const char *short_name);
  430. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
  431. int av_open_input_stream(AVFormatContext **ic_ptr,
  432. ByteIOContext *pb, const char *filename,
  433. AVInputFormat *fmt, AVFormatParameters *ap);
  434. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
  435. AVInputFormat *fmt,
  436. int buf_size,
  437. AVFormatParameters *ap);
  438. /* no av_open for output, so applications will need this: */
  439. AVFormatContext *av_alloc_format_context(void);
  440. #define AVERROR_UNKNOWN (-1) /* unknown error */
  441. #define AVERROR_IO (-2) /* i/o error */
  442. #define AVERROR_NUMEXPECTED (-3) /* number syntax expected in filename */
  443. #define AVERROR_INVALIDDATA (-4) /* invalid data found */
  444. #define AVERROR_NOMEM (-5) /* not enough memory */
  445. #define AVERROR_NOFMT (-6) /* unknown format */
  446. #define AVERROR_NOTSUPP (-7) /* operation not supported */
  447. int av_find_stream_info(AVFormatContext *ic);
  448. int av_read_packet(AVFormatContext *s, AVPacket *pkt);
  449. int av_read_frame(AVFormatContext *s, AVPacket *pkt);
  450. int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp);
  451. int av_read_play(AVFormatContext *s);
  452. int av_read_pause(AVFormatContext *s);
  453. void av_close_input_file(AVFormatContext *s);
  454. AVStream *av_new_stream(AVFormatContext *s, int id);
  455. void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits,
  456. int pts_num, int pts_den);
  457. int av_find_default_stream_index(AVFormatContext *s);
  458. int av_index_search_timestamp(AVStream *st, int timestamp);
  459. int av_add_index_entry(AVStream *st,
  460. int64_t pos, int64_t timestamp, int distance, int flags);
  461. /* media file output */
  462. int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
  463. int av_write_header(AVFormatContext *s);
  464. int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf,
  465. int size);
  466. int av_write_trailer(AVFormatContext *s);
  467. void dump_format(AVFormatContext *ic,
  468. int index,
  469. const char *url,
  470. int is_output);
  471. int parse_image_size(int *width_ptr, int *height_ptr, const char *str);
  472. int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg);
  473. int64_t parse_date(const char *datestr, int duration);
  474. int64_t av_gettime(void);
  475. /* ffm specific for ffserver */
  476. #define FFM_PACKET_SIZE 4096
  477. offset_t ffm_read_write_index(int fd);
  478. void ffm_write_write_index(int fd, offset_t pos);
  479. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size);
  480. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);
  481. int get_frame_filename(char *buf, int buf_size,
  482. const char *path, int number);
  483. int filename_number_test(const char *filename);
  484. /* grab specific */
  485. int video_grab_init(void);
  486. int audio_init(void);
  487. /* DV1394 */
  488. int dv1394_init(void);
  489. #ifdef HAVE_AV_CONFIG_H
  490. #include "os_support.h"
  491. int strstart(const char *str, const char *val, const char **ptr);
  492. int stristart(const char *str, const char *val, const char **ptr);
  493. void pstrcpy(char *buf, int buf_size, const char *str);
  494. char *pstrcat(char *buf, int buf_size, const char *s);
  495. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem);
  496. #ifdef __GNUC__
  497. #define dynarray_add(tab, nb_ptr, elem)\
  498. do {\
  499. typeof(tab) _tab = (tab);\
  500. typeof(elem) _elem = (elem);\
  501. (void)sizeof(**_tab == _elem); /* check that types are compatible */\
  502. __dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
  503. } while(0)
  504. #else
  505. #define dynarray_add(tab, nb_ptr, elem)\
  506. do {\
  507. __dynarray_add((unsigned long **)(tab), nb_ptr, (unsigned long)(elem));\
  508. } while(0)
  509. #endif
  510. time_t mktimegm(struct tm *tm);
  511. const char *small_strptime(const char *p, const char *fmt,
  512. struct tm *dt);
  513. struct in_addr;
  514. int resolve_host(struct in_addr *sin_addr, const char *hostname);
  515. void url_split(char *proto, int proto_size,
  516. char *hostname, int hostname_size,
  517. int *port_ptr,
  518. char *path, int path_size,
  519. const char *url);
  520. int match_ext(const char *filename, const char *extensions);
  521. #endif /* HAVE_AV_CONFIG_H */
  522. #ifdef __cplusplus
  523. }
  524. #endif
  525. #endif /* AVFORMAT_H */