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.

641 lines
20KB

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