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.

649 lines
20KB

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