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.

638 lines
19KB

  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. #define AV_NOPTS_VALUE MININT64
  22. #define AV_TIME_BASE 1000000
  23. typedef struct AVPacket {
  24. int64_t pts; /* presentation time stamp in AV_TIME_BASE units (or
  25. pts_den units in muxers or demuxers) */
  26. int64_t dts; /* decompression time stamp in AV_TIME_BASE units (or
  27. pts_den units in muxers or demuxers) */
  28. uint8_t *data;
  29. int size;
  30. int stream_index;
  31. int flags;
  32. int duration; /* presentation duration (0 if not available) */
  33. void (*destruct)(struct AVPacket *);
  34. void *priv;
  35. } AVPacket;
  36. #define PKT_FLAG_KEY 0x0001
  37. /* initialize optional fields of a packet */
  38. static inline void av_init_packet(AVPacket *pkt)
  39. {
  40. pkt->pts = AV_NOPTS_VALUE;
  41. pkt->dts = AV_NOPTS_VALUE;
  42. pkt->duration = 0;
  43. pkt->flags = 0;
  44. pkt->stream_index = 0;
  45. }
  46. int av_new_packet(AVPacket *pkt, int size);
  47. int av_dup_packet(AVPacket *pkt);
  48. /**
  49. * Free a packet
  50. *
  51. * @param pkt packet to free
  52. */
  53. static inline void av_free_packet(AVPacket *pkt)
  54. {
  55. if (pkt && pkt->destruct) {
  56. pkt->destruct(pkt);
  57. }
  58. }
  59. /*************************************************/
  60. /* fractional numbers for exact pts handling */
  61. /* the exact value of the fractional number is: 'val + num / den'. num
  62. is assumed to be such as 0 <= num < den */
  63. typedef struct AVFrac {
  64. int64_t val, num, den;
  65. } AVFrac;
  66. void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den);
  67. void av_frac_add(AVFrac *f, int64_t incr);
  68. void av_frac_set(AVFrac *f, int64_t val);
  69. /*************************************************/
  70. /* input/output formats */
  71. struct AVFormatContext;
  72. /* this structure contains the data a format has to probe a file */
  73. typedef struct AVProbeData {
  74. const char *filename;
  75. unsigned char *buf;
  76. int buf_size;
  77. } AVProbeData;
  78. #define AVPROBE_SCORE_MAX 100
  79. typedef struct AVFormatParameters {
  80. int frame_rate;
  81. int frame_rate_base;
  82. int sample_rate;
  83. int channels;
  84. int width;
  85. int height;
  86. enum PixelFormat pix_fmt;
  87. struct AVImageFormat *image_format;
  88. int channel; /* used to select dv channel */
  89. const char *device; /* video4linux, audio or DV device */
  90. const char *standard; /* tv standard, NTSC, PAL, SECAM */
  91. int mpeg2ts_raw:1; /* force raw MPEG2 transport stream output, if possible */
  92. int mpeg2ts_compute_pcr:1; /* compute exact PCR for each transport
  93. stream packet (only meaningful if
  94. mpeg2ts_raw is TRUE */
  95. int initial_pause:1; /* do not begin to play the stream
  96. immediately (RTSP only) */
  97. } AVFormatParameters;
  98. #define AVFMT_NOFILE 0x0001 /* no file should be opened */
  99. #define AVFMT_NEEDNUMBER 0x0002 /* needs '%d' in filename */
  100. #define AVFMT_SHOW_IDS 0x0008 /* show format stream IDs numbers */
  101. #define AVFMT_RAWPICTURE 0x0020 /* format wants AVPicture structure for
  102. raw picture data */
  103. typedef struct AVOutputFormat {
  104. const char *name;
  105. const char *long_name;
  106. const char *mime_type;
  107. const char *extensions; /* comma separated extensions */
  108. /* size of private data so that it can be allocated in the wrapper */
  109. int priv_data_size;
  110. /* output support */
  111. enum CodecID audio_codec; /* default audio codec */
  112. enum CodecID video_codec; /* default video codec */
  113. int (*write_header)(struct AVFormatContext *);
  114. int (*write_packet)(struct AVFormatContext *,
  115. int stream_index,
  116. const uint8_t *buf, int size, int64_t pts);
  117. int (*write_trailer)(struct AVFormatContext *);
  118. /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER */
  119. int flags;
  120. /* currently only used to set pixel format if not YUV420P */
  121. int (*set_parameters)(struct AVFormatContext *, AVFormatParameters *);
  122. /* private fields */
  123. struct AVOutputFormat *next;
  124. } AVOutputFormat;
  125. typedef struct AVInputFormat {
  126. const char *name;
  127. const char *long_name;
  128. /* size of private data so that it can be allocated in the wrapper */
  129. int priv_data_size;
  130. /* tell if a given file has a chance of being parsing by this format */
  131. int (*read_probe)(AVProbeData *);
  132. /* read the format header and initialize the AVFormatContext
  133. structure. Return 0 if OK. 'ap' if non NULL contains
  134. additionnal paramters. Only used in raw format right
  135. now. 'av_new_stream' should be called to create new streams. */
  136. int (*read_header)(struct AVFormatContext *,
  137. AVFormatParameters *ap);
  138. /* read one packet and put it in 'pkt'. pts and flags are also
  139. set. 'av_new_stream' can be called only if the flag
  140. AVFMTCTX_NOHEADER is used. */
  141. int (*read_packet)(struct AVFormatContext *, AVPacket *pkt);
  142. /* close the stream. The AVFormatContext and AVStreams are not
  143. freed by this function */
  144. int (*read_close)(struct AVFormatContext *);
  145. /* seek at or before a given timestamp (given in AV_TIME_BASE
  146. units) relative to the frames in stream component stream_index */
  147. int (*read_seek)(struct AVFormatContext *,
  148. int stream_index, int64_t timestamp);
  149. /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER */
  150. int flags;
  151. /* if extensions are defined, then no probe is done. You should
  152. usually not use extension format guessing because it is not
  153. reliable enough */
  154. const char *extensions;
  155. /* general purpose read only value that the format can use */
  156. int value;
  157. /* start/resume playing - only meaningful if using a network based format
  158. (RTSP) */
  159. int (*read_play)(struct AVFormatContext *);
  160. /* pause playing - only meaningful if using a network based format
  161. (RTSP) */
  162. int (*read_pause)(struct AVFormatContext *);
  163. /* private fields */
  164. struct AVInputFormat *next;
  165. } AVInputFormat;
  166. typedef struct AVIndexEntry {
  167. int64_t pos;
  168. int64_t timestamp;
  169. #define AVINDEX_KEYFRAME 0x0001
  170. /* the following 2 flags indicate that the next/prev keyframe is known, and scaning for it isnt needed */
  171. int flags;
  172. int min_distance; /* min distance between this and the previous keyframe, used to avoid unneeded searching */
  173. } AVIndexEntry;
  174. typedef struct AVStream {
  175. int index; /* stream index in AVFormatContext */
  176. int id; /* format specific stream id */
  177. AVCodecContext codec; /* codec context */
  178. int r_frame_rate; /* real frame rate of the stream */
  179. int r_frame_rate_base;/* real frame rate base of the stream */
  180. void *priv_data;
  181. /* internal data used in av_find_stream_info() */
  182. int64_t codec_info_duration;
  183. int codec_info_nb_frames;
  184. /* encoding: PTS generation when outputing stream */
  185. AVFrac pts;
  186. /* ffmpeg.c private use */
  187. int stream_copy; /* if TRUE, just copy stream */
  188. /* quality, as it has been removed from AVCodecContext and put in AVVideoFrame
  189. * MN:dunno if thats the right place, for it */
  190. float quality;
  191. /* decoding: position of the first frame of the component, in
  192. AV_TIME_BASE fractional seconds. */
  193. int64_t start_time;
  194. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  195. seconds. */
  196. int64_t duration;
  197. /* av_read_frame() support */
  198. int need_parsing;
  199. struct AVCodecParserContext *parser;
  200. int64_t cur_dts;
  201. int last_IP_duration;
  202. /* av_seek_frame() support */
  203. AVIndexEntry *index_entries; /* only used if the format does not
  204. support seeking natively */
  205. int nb_index_entries;
  206. int index_entries_allocated_size;
  207. } AVStream;
  208. #define AVFMTCTX_NOHEADER 0x0001 /* signal that no header is present
  209. (streams are added dynamically) */
  210. #define MAX_STREAMS 20
  211. /* format I/O context */
  212. typedef struct AVFormatContext {
  213. /* can only be iformat or oformat, not both at the same time */
  214. struct AVInputFormat *iformat;
  215. struct AVOutputFormat *oformat;
  216. void *priv_data;
  217. ByteIOContext pb;
  218. int nb_streams;
  219. AVStream *streams[MAX_STREAMS];
  220. char filename[1024]; /* input or output filename */
  221. /* stream info */
  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. /* 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 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 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. #include "rtp.h"
  403. #include "rtsp.h"
  404. /* yuv4mpeg.c */
  405. extern AVOutputFormat yuv4mpegpipe_oformat;
  406. /* utils.c */
  407. void av_register_input_format(AVInputFormat *format);
  408. void av_register_output_format(AVOutputFormat *format);
  409. AVOutputFormat *guess_stream_format(const char *short_name,
  410. const char *filename, const char *mime_type);
  411. AVOutputFormat *guess_format(const char *short_name,
  412. const char *filename, const char *mime_type);
  413. void av_hex_dump(FILE *f, uint8_t *buf, int size);
  414. void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
  415. void av_register_all(void);
  416. typedef struct FifoBuffer {
  417. uint8_t *buffer;
  418. uint8_t *rptr, *wptr, *end;
  419. } FifoBuffer;
  420. int fifo_init(FifoBuffer *f, int size);
  421. void fifo_free(FifoBuffer *f);
  422. int fifo_size(FifoBuffer *f, uint8_t *rptr);
  423. int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr);
  424. void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr);
  425. /* media file input */
  426. AVInputFormat *av_find_input_format(const char *short_name);
  427. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
  428. int av_open_input_stream(AVFormatContext **ic_ptr,
  429. ByteIOContext *pb, const char *filename,
  430. AVInputFormat *fmt, AVFormatParameters *ap);
  431. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
  432. AVInputFormat *fmt,
  433. int buf_size,
  434. AVFormatParameters *ap);
  435. #define AVERROR_UNKNOWN (-1) /* unknown error */
  436. #define AVERROR_IO (-2) /* i/o error */
  437. #define AVERROR_NUMEXPECTED (-3) /* number syntax expected in filename */
  438. #define AVERROR_INVALIDDATA (-4) /* invalid data found */
  439. #define AVERROR_NOMEM (-5) /* not enough memory */
  440. #define AVERROR_NOFMT (-6) /* unknown format */
  441. #define AVERROR_NOTSUPP (-7) /* operation not supported */
  442. int av_find_stream_info(AVFormatContext *ic);
  443. int av_read_packet(AVFormatContext *s, AVPacket *pkt);
  444. int av_read_frame(AVFormatContext *s, AVPacket *pkt);
  445. int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp);
  446. int av_read_play(AVFormatContext *s);
  447. int av_read_pause(AVFormatContext *s);
  448. void av_close_input_file(AVFormatContext *s);
  449. AVStream *av_new_stream(AVFormatContext *s, int id);
  450. void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits,
  451. int pts_num, int pts_den);
  452. int av_find_default_stream_index(AVFormatContext *s);
  453. int av_index_search_timestamp(AVStream *st, int timestamp);
  454. int av_add_index_entry(AVStream *st,
  455. int64_t pos, int64_t timestamp, int distance, int flags);
  456. /* media file output */
  457. int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
  458. int av_write_header(AVFormatContext *s);
  459. int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf,
  460. int size);
  461. int av_write_trailer(AVFormatContext *s);
  462. void dump_format(AVFormatContext *ic,
  463. int index,
  464. const char *url,
  465. int is_output);
  466. int parse_image_size(int *width_ptr, int *height_ptr, const char *str);
  467. int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg);
  468. int64_t parse_date(const char *datestr, int duration);
  469. int64_t av_gettime(void);
  470. /* ffm specific for ffserver */
  471. #define FFM_PACKET_SIZE 4096
  472. offset_t ffm_read_write_index(int fd);
  473. void ffm_write_write_index(int fd, offset_t pos);
  474. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size);
  475. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);
  476. int get_frame_filename(char *buf, int buf_size,
  477. const char *path, int number);
  478. int filename_number_test(const char *filename);
  479. /* grab specific */
  480. int video_grab_init(void);
  481. int audio_init(void);
  482. /* DV1394 */
  483. int dv1394_init(void);
  484. #ifdef HAVE_AV_CONFIG_H
  485. #include "os_support.h"
  486. int strstart(const char *str, const char *val, const char **ptr);
  487. int stristart(const char *str, const char *val, const char **ptr);
  488. void pstrcpy(char *buf, int buf_size, const char *str);
  489. char *pstrcat(char *buf, int buf_size, const char *s);
  490. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem);
  491. #ifdef __GNUC__
  492. #define dynarray_add(tab, nb_ptr, elem)\
  493. do {\
  494. typeof(tab) _tab = (tab);\
  495. typeof(elem) _elem = (elem);\
  496. (void)sizeof(**_tab == _elem); /* check that types are compatible */\
  497. __dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
  498. } while(0)
  499. #else
  500. #define dynarray_add(tab, nb_ptr, elem)\
  501. do {\
  502. __dynarray_add((unsigned long **)(tab), nb_ptr, (unsigned long)(elem));\
  503. } while(0)
  504. #endif
  505. time_t mktimegm(struct tm *tm);
  506. const char *small_strptime(const char *p, const char *fmt,
  507. struct tm *dt);
  508. struct in_addr;
  509. int resolve_host(struct in_addr *sin_addr, const char *hostname);
  510. void url_split(char *proto, int proto_size,
  511. char *hostname, int hostname_size,
  512. int *port_ptr,
  513. char *path, int path_size,
  514. const char *url);
  515. int match_ext(const char *filename, const char *extensions);
  516. #endif /* HAVE_AV_CONFIG_H */
  517. #ifdef __cplusplus
  518. }
  519. #endif
  520. #endif /* AVFORMAT_H */