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.

642 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. char title[512];
  222. char author[512];
  223. char copyright[512];
  224. char comment[512];
  225. char album[512];
  226. int year; /* ID3 year, 0 if none */
  227. int track; /* track number, 0 if none */
  228. char genre[32]; /* ID3 genre */
  229. int ctx_flags; /* format specific flags, see AVFMTCTX_xx */
  230. /* private data for pts handling (do not modify directly) */
  231. int pts_wrap_bits; /* number of bits in pts (used for wrapping control) */
  232. int pts_num, pts_den; /* value to convert to seconds */
  233. /* This buffer is only needed when packets were already buffered but
  234. not decoded, for example to get the codec parameters in mpeg
  235. streams */
  236. struct AVPacketList *packet_buffer;
  237. /* decoding: position of the first frame of the component, in
  238. AV_TIME_BASE fractional seconds. NEVER set this value directly:
  239. it is deduced from the AVStream values. */
  240. int64_t start_time;
  241. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  242. seconds. NEVER set this value directly: it is deduced from the
  243. AVStream values. */
  244. int64_t duration;
  245. /* decoding: total file size. 0 if unknown */
  246. int64_t file_size;
  247. /* decoding: total stream bitrate in bit/s, 0 if not
  248. available. Never set it directly if the file_size and the
  249. duration are known as ffmpeg can compute it automatically. */
  250. int bit_rate;
  251. /* av_read_frame() support */
  252. AVStream *cur_st;
  253. const uint8_t *cur_ptr;
  254. int cur_len;
  255. AVPacket cur_pkt;
  256. /* the following are used for pts/dts unit conversion */
  257. int64_t last_pkt_stream_pts;
  258. int64_t last_pkt_stream_dts;
  259. int64_t last_pkt_pts;
  260. int64_t last_pkt_dts;
  261. int last_pkt_pts_frac;
  262. int last_pkt_dts_frac;
  263. /* av_seek_frame() support */
  264. int64_t data_offset; /* offset of the first packet */
  265. int index_built;
  266. } AVFormatContext;
  267. typedef struct AVPacketList {
  268. AVPacket pkt;
  269. struct AVPacketList *next;
  270. } AVPacketList;
  271. extern AVInputFormat *first_iformat;
  272. extern AVOutputFormat *first_oformat;
  273. /* still image support */
  274. struct AVInputImageContext;
  275. typedef struct AVInputImageContext AVInputImageContext;
  276. typedef struct AVImageInfo {
  277. enum PixelFormat pix_fmt; /* requested pixel format */
  278. int width; /* requested width */
  279. int height; /* requested height */
  280. int interleaved; /* image is interleaved (e.g. interleaved GIF) */
  281. AVPicture pict; /* returned allocated image */
  282. } AVImageInfo;
  283. /* AVImageFormat.flags field constants */
  284. #define AVIMAGE_INTERLEAVED 0x0001 /* image format support interleaved output */
  285. typedef struct AVImageFormat {
  286. const char *name;
  287. const char *extensions;
  288. /* tell if a given file has a chance of being parsing by this format */
  289. int (*img_probe)(AVProbeData *);
  290. /* read a whole image. 'alloc_cb' is called when the image size is
  291. known so that the caller can allocate the image. If 'allo_cb'
  292. returns non zero, then the parsing is aborted. Return '0' if
  293. OK. */
  294. int (*img_read)(ByteIOContext *,
  295. int (*alloc_cb)(void *, AVImageInfo *info), void *);
  296. /* write the image */
  297. int supported_pixel_formats; /* mask of supported formats for output */
  298. int (*img_write)(ByteIOContext *, AVImageInfo *);
  299. int flags;
  300. struct AVImageFormat *next;
  301. } AVImageFormat;
  302. void av_register_image_format(AVImageFormat *img_fmt);
  303. AVImageFormat *av_probe_image_format(AVProbeData *pd);
  304. AVImageFormat *guess_image_format(const char *filename);
  305. int av_read_image(ByteIOContext *pb, const char *filename,
  306. AVImageFormat *fmt,
  307. int (*alloc_cb)(void *, AVImageInfo *info), void *opaque);
  308. int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img);
  309. extern AVImageFormat *first_image_format;
  310. extern AVImageFormat pnm_image_format;
  311. extern AVImageFormat pbm_image_format;
  312. extern AVImageFormat pgm_image_format;
  313. extern AVImageFormat ppm_image_format;
  314. extern AVImageFormat pam_image_format;
  315. extern AVImageFormat pgmyuv_image_format;
  316. extern AVImageFormat yuv_image_format;
  317. #ifdef CONFIG_ZLIB
  318. extern AVImageFormat png_image_format;
  319. #endif
  320. extern AVImageFormat jpeg_image_format;
  321. extern AVImageFormat gif_image_format;
  322. extern AVImageFormat sgi_image_format;
  323. /* XXX: use automatic init with either ELF sections or C file parser */
  324. /* modules */
  325. /* mpeg.c */
  326. extern AVInputFormat mpegps_demux;
  327. int mpegps_init(void);
  328. /* mpegts.c */
  329. extern AVInputFormat mpegts_demux;
  330. int mpegts_init(void);
  331. /* rm.c */
  332. int rm_init(void);
  333. /* crc.c */
  334. int crc_init(void);
  335. /* img.c */
  336. int img_init(void);
  337. /* asf.c */
  338. int asf_init(void);
  339. /* avienc.c */
  340. int avienc_init(void);
  341. /* avidec.c */
  342. int avidec_init(void);
  343. /* swf.c */
  344. int swf_init(void);
  345. /* mov.c */
  346. int mov_init(void);
  347. /* movenc.c */
  348. int movenc_init(void);
  349. /* flvenc.c */
  350. int flvenc_init(void);
  351. /* flvdec.c */
  352. int flvdec_init(void);
  353. /* jpeg.c */
  354. int jpeg_init(void);
  355. /* gif.c */
  356. int gif_init(void);
  357. /* au.c */
  358. int au_init(void);
  359. /* amr.c */
  360. int amr_init(void);
  361. /* wav.c */
  362. int ff_wav_init(void);
  363. /* raw.c */
  364. int pcm_read_seek(AVFormatContext *s,
  365. int stream_index, int64_t timestamp);
  366. int raw_init(void);
  367. /* mp3.c */
  368. int mp3_init(void);
  369. /* yuv4mpeg.c */
  370. int yuv4mpeg_init(void);
  371. /* ogg.c */
  372. int ogg_init(void);
  373. /* dv.c */
  374. int ff_dv_init(void);
  375. /* ffm.c */
  376. int ffm_init(void);
  377. /* rtsp.c */
  378. extern AVInputFormat redir_demux;
  379. int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f);
  380. /* 4xm.c */
  381. int fourxm_init(void);
  382. /* psxstr.c */
  383. int str_init(void);
  384. /* idroq.c */
  385. int roq_init(void);
  386. /* ipmovie.c */
  387. int ipmovie_init(void);
  388. /* nut.c */
  389. int nut_init(void);
  390. /* wc3movie.c */
  391. int wc3_init(void);
  392. /* westwood.c */
  393. int westwood_init(void);
  394. /* segafilm.c */
  395. int film_init(void);
  396. /* idcin.c */
  397. int idcin_init(void);
  398. /* flic.c */
  399. int flic_init(void);
  400. /* sierravmd.c */
  401. int vmd_init(void);
  402. /* matroska.c */
  403. int matroska_init(void);
  404. #include "rtp.h"
  405. #include "rtsp.h"
  406. /* yuv4mpeg.c */
  407. extern AVOutputFormat yuv4mpegpipe_oformat;
  408. /* utils.c */
  409. void av_register_input_format(AVInputFormat *format);
  410. void av_register_output_format(AVOutputFormat *format);
  411. AVOutputFormat *guess_stream_format(const char *short_name,
  412. const char *filename, const char *mime_type);
  413. AVOutputFormat *guess_format(const char *short_name,
  414. const char *filename, const char *mime_type);
  415. void av_hex_dump(FILE *f, uint8_t *buf, int size);
  416. void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
  417. void av_register_all(void);
  418. typedef struct FifoBuffer {
  419. uint8_t *buffer;
  420. uint8_t *rptr, *wptr, *end;
  421. } FifoBuffer;
  422. int fifo_init(FifoBuffer *f, int size);
  423. void fifo_free(FifoBuffer *f);
  424. int fifo_size(FifoBuffer *f, uint8_t *rptr);
  425. int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr);
  426. void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr);
  427. /* media file input */
  428. AVInputFormat *av_find_input_format(const char *short_name);
  429. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
  430. int av_open_input_stream(AVFormatContext **ic_ptr,
  431. ByteIOContext *pb, const char *filename,
  432. AVInputFormat *fmt, AVFormatParameters *ap);
  433. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
  434. AVInputFormat *fmt,
  435. int buf_size,
  436. AVFormatParameters *ap);
  437. /* no av_open for output, so applications will need this: */
  438. AVFormatContext *av_alloc_format_context(void);
  439. #define AVERROR_UNKNOWN (-1) /* unknown error */
  440. #define AVERROR_IO (-2) /* i/o error */
  441. #define AVERROR_NUMEXPECTED (-3) /* number syntax expected in filename */
  442. #define AVERROR_INVALIDDATA (-4) /* invalid data found */
  443. #define AVERROR_NOMEM (-5) /* not enough memory */
  444. #define AVERROR_NOFMT (-6) /* unknown format */
  445. #define AVERROR_NOTSUPP (-7) /* operation not supported */
  446. int av_find_stream_info(AVFormatContext *ic);
  447. int av_read_packet(AVFormatContext *s, AVPacket *pkt);
  448. int av_read_frame(AVFormatContext *s, AVPacket *pkt);
  449. int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp);
  450. int av_read_play(AVFormatContext *s);
  451. int av_read_pause(AVFormatContext *s);
  452. void av_close_input_file(AVFormatContext *s);
  453. AVStream *av_new_stream(AVFormatContext *s, int id);
  454. void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits,
  455. int pts_num, int pts_den);
  456. int av_find_default_stream_index(AVFormatContext *s);
  457. int av_index_search_timestamp(AVStream *st, int timestamp);
  458. int av_add_index_entry(AVStream *st,
  459. int64_t pos, int64_t timestamp, int distance, int flags);
  460. /* media file output */
  461. int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
  462. int av_write_header(AVFormatContext *s);
  463. int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf,
  464. int size);
  465. int av_write_trailer(AVFormatContext *s);
  466. void dump_format(AVFormatContext *ic,
  467. int index,
  468. const char *url,
  469. int is_output);
  470. int parse_image_size(int *width_ptr, int *height_ptr, const char *str);
  471. int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg);
  472. int64_t parse_date(const char *datestr, int duration);
  473. int64_t av_gettime(void);
  474. /* ffm specific for ffserver */
  475. #define FFM_PACKET_SIZE 4096
  476. offset_t ffm_read_write_index(int fd);
  477. void ffm_write_write_index(int fd, offset_t pos);
  478. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size);
  479. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);
  480. int get_frame_filename(char *buf, int buf_size,
  481. const char *path, int number);
  482. int filename_number_test(const char *filename);
  483. /* grab specific */
  484. int video_grab_init(void);
  485. int audio_init(void);
  486. /* DV1394 */
  487. int dv1394_init(void);
  488. #ifdef HAVE_AV_CONFIG_H
  489. #include "os_support.h"
  490. int strstart(const char *str, const char *val, const char **ptr);
  491. int stristart(const char *str, const char *val, const char **ptr);
  492. void pstrcpy(char *buf, int buf_size, const char *str);
  493. char *pstrcat(char *buf, int buf_size, const char *s);
  494. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem);
  495. #ifdef __GNUC__
  496. #define dynarray_add(tab, nb_ptr, elem)\
  497. do {\
  498. typeof(tab) _tab = (tab);\
  499. typeof(elem) _elem = (elem);\
  500. (void)sizeof(**_tab == _elem); /* check that types are compatible */\
  501. __dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
  502. } while(0)
  503. #else
  504. #define dynarray_add(tab, nb_ptr, elem)\
  505. do {\
  506. __dynarray_add((unsigned long **)(tab), nb_ptr, (unsigned long)(elem));\
  507. } while(0)
  508. #endif
  509. time_t mktimegm(struct tm *tm);
  510. const char *small_strptime(const char *p, const char *fmt,
  511. struct tm *dt);
  512. struct in_addr;
  513. int resolve_host(struct in_addr *sin_addr, const char *hostname);
  514. void url_split(char *proto, int proto_size,
  515. char *hostname, int hostname_size,
  516. int *port_ptr,
  517. char *path, int path_size,
  518. const char *url);
  519. int match_ext(const char *filename, const char *extensions);
  520. #endif /* HAVE_AV_CONFIG_H */
  521. #ifdef __cplusplus
  522. }
  523. #endif
  524. #endif /* AVFORMAT_H */