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.

653 lines
20KB

  1. #ifndef AVFORMAT_H
  2. #define AVFORMAT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #define LIBAVFORMAT_BUILD 4617
  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. void av_destruct_packet_nofree(AVPacket *pkt);
  36. /* initialize optional fields of a packet */
  37. static inline void av_init_packet(AVPacket *pkt)
  38. {
  39. pkt->pts = AV_NOPTS_VALUE;
  40. pkt->dts = AV_NOPTS_VALUE;
  41. pkt->duration = 0;
  42. pkt->flags = 0;
  43. pkt->stream_index = 0;
  44. pkt->destruct= av_destruct_packet_nofree;
  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 *, AVPacket *pkt);
  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. int (*interleave_packet)(struct AVFormatContext *, AVPacket *out, AVPacket *in, int flush);
  121. /* private fields */
  122. struct AVOutputFormat *next;
  123. } AVOutputFormat;
  124. typedef struct AVInputFormat {
  125. const char *name;
  126. const char *long_name;
  127. /* size of private data so that it can be allocated in the wrapper */
  128. int priv_data_size;
  129. /* tell if a given file has a chance of being parsing by this format */
  130. int (*read_probe)(AVProbeData *);
  131. /* read the format header and initialize the AVFormatContext
  132. structure. Return 0 if OK. 'ap' if non NULL contains
  133. additionnal paramters. Only used in raw format right
  134. now. 'av_new_stream' should be called to create new streams. */
  135. int (*read_header)(struct AVFormatContext *,
  136. AVFormatParameters *ap);
  137. /* read one packet and put it in 'pkt'. pts and flags are also
  138. set. 'av_new_stream' can be called only if the flag
  139. AVFMTCTX_NOHEADER is used. */
  140. int (*read_packet)(struct AVFormatContext *, AVPacket *pkt);
  141. /* close the stream. The AVFormatContext and AVStreams are not
  142. freed by this function */
  143. int (*read_close)(struct AVFormatContext *);
  144. /* seek at or before a given timestamp (given in AV_TIME_BASE
  145. units) relative to the frames in stream component stream_index */
  146. int (*read_seek)(struct AVFormatContext *,
  147. int stream_index, int64_t timestamp);
  148. /**
  149. * gets the next timestamp in AV_TIME_BASE units.
  150. */
  151. int64_t (*read_timestamp)(struct AVFormatContext *s, int stream_index,
  152. int64_t *pos, int64_t pos_limit);
  153. /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER */
  154. int flags;
  155. /* if extensions are defined, then no probe is done. You should
  156. usually not use extension format guessing because it is not
  157. reliable enough */
  158. const char *extensions;
  159. /* general purpose read only value that the format can use */
  160. int value;
  161. /* start/resume playing - only meaningful if using a network based format
  162. (RTSP) */
  163. int (*read_play)(struct AVFormatContext *);
  164. /* pause playing - only meaningful if using a network based format
  165. (RTSP) */
  166. int (*read_pause)(struct AVFormatContext *);
  167. /* private fields */
  168. struct AVInputFormat *next;
  169. } AVInputFormat;
  170. typedef struct AVIndexEntry {
  171. int64_t pos;
  172. int64_t timestamp;
  173. #define AVINDEX_KEYFRAME 0x0001
  174. /* the following 2 flags indicate that the next/prev keyframe is known, and scaning for it isnt needed */
  175. int flags;
  176. int min_distance; /* min distance between this and the previous keyframe, used to avoid unneeded searching */
  177. } AVIndexEntry;
  178. typedef struct AVStream {
  179. int index; /* stream index in AVFormatContext */
  180. int id; /* format specific stream id */
  181. AVCodecContext codec; /* codec context */
  182. int r_frame_rate; /* real frame rate of the stream */
  183. int r_frame_rate_base;/* real frame rate base of the stream */
  184. void *priv_data;
  185. /* internal data used in av_find_stream_info() */
  186. int64_t codec_info_duration;
  187. int codec_info_nb_frames;
  188. /* encoding: PTS generation when outputing stream */
  189. AVFrac pts;
  190. AVRational time_base;
  191. int pts_wrap_bits; /* number of bits in pts (used for wrapping control) */
  192. /* ffmpeg.c private use */
  193. int stream_copy; /* if TRUE, just copy stream */
  194. /* quality, as it has been removed from AVCodecContext and put in AVVideoFrame
  195. * MN:dunno if thats the right place, for it */
  196. float quality;
  197. /* decoding: position of the first frame of the component, in
  198. AV_TIME_BASE fractional seconds. */
  199. int64_t start_time;
  200. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  201. seconds. */
  202. int64_t duration;
  203. /* av_read_frame() support */
  204. int need_parsing;
  205. struct AVCodecParserContext *parser;
  206. int64_t cur_dts;
  207. int last_IP_duration;
  208. int64_t last_IP_pts;
  209. /* av_seek_frame() support */
  210. AVIndexEntry *index_entries; /* only used if the format does not
  211. support seeking natively */
  212. int nb_index_entries;
  213. int index_entries_allocated_size;
  214. } AVStream;
  215. #define AVFMTCTX_NOHEADER 0x0001 /* signal that no header is present
  216. (streams are added dynamically) */
  217. #define MAX_STREAMS 20
  218. /* format I/O context */
  219. typedef struct AVFormatContext {
  220. AVClass *av_class; /* set by av_alloc_format_context */
  221. /* can only be iformat or oformat, not both at the same time */
  222. struct AVInputFormat *iformat;
  223. struct AVOutputFormat *oformat;
  224. void *priv_data;
  225. ByteIOContext pb;
  226. int nb_streams;
  227. AVStream *streams[MAX_STREAMS];
  228. char filename[1024]; /* input or output filename */
  229. /* stream info */
  230. int64_t timestamp;
  231. char title[512];
  232. char author[512];
  233. char copyright[512];
  234. char comment[512];
  235. char album[512];
  236. int year; /* ID3 year, 0 if none */
  237. int track; /* track number, 0 if none */
  238. char genre[32]; /* ID3 genre */
  239. int ctx_flags; /* format specific flags, see AVFMTCTX_xx */
  240. /* private data for pts handling (do not modify directly) */
  241. /* This buffer is only needed when packets were already buffered but
  242. not decoded, for example to get the codec parameters in mpeg
  243. streams */
  244. struct AVPacketList *packet_buffer;
  245. /* decoding: position of the first frame of the component, in
  246. AV_TIME_BASE fractional seconds. NEVER set this value directly:
  247. it is deduced from the AVStream values. */
  248. int64_t start_time;
  249. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  250. seconds. NEVER set this value directly: it is deduced from the
  251. AVStream values. */
  252. int64_t duration;
  253. /* decoding: total file size. 0 if unknown */
  254. int64_t file_size;
  255. /* decoding: total stream bitrate in bit/s, 0 if not
  256. available. Never set it directly if the file_size and the
  257. duration are known as ffmpeg can compute it automatically. */
  258. int bit_rate;
  259. /* av_read_frame() support */
  260. AVStream *cur_st;
  261. const uint8_t *cur_ptr;
  262. int cur_len;
  263. AVPacket cur_pkt;
  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. /* img2.c */
  339. int img2_init(void);
  340. /* asf.c */
  341. int asf_init(void);
  342. /* avienc.c */
  343. int avienc_init(void);
  344. /* avidec.c */
  345. int avidec_init(void);
  346. /* swf.c */
  347. int swf_init(void);
  348. /* mov.c */
  349. int mov_init(void);
  350. /* movenc.c */
  351. int movenc_init(void);
  352. /* flvenc.c */
  353. int flvenc_init(void);
  354. /* flvdec.c */
  355. int flvdec_init(void);
  356. /* jpeg.c */
  357. int jpeg_init(void);
  358. /* gif.c */
  359. int gif_init(void);
  360. /* au.c */
  361. int au_init(void);
  362. /* amr.c */
  363. int amr_init(void);
  364. /* wav.c */
  365. int ff_wav_init(void);
  366. /* raw.c */
  367. int pcm_read_seek(AVFormatContext *s,
  368. int stream_index, int64_t timestamp);
  369. int raw_init(void);
  370. /* mp3.c */
  371. int mp3_init(void);
  372. /* yuv4mpeg.c */
  373. int yuv4mpeg_init(void);
  374. /* ogg.c */
  375. int ogg_init(void);
  376. /* dv.c */
  377. int ff_dv_init(void);
  378. /* ffm.c */
  379. int ffm_init(void);
  380. /* rtsp.c */
  381. extern AVInputFormat redir_demux;
  382. int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f);
  383. /* 4xm.c */
  384. int fourxm_init(void);
  385. /* psxstr.c */
  386. int str_init(void);
  387. /* idroq.c */
  388. int roq_init(void);
  389. /* ipmovie.c */
  390. int ipmovie_init(void);
  391. /* nut.c */
  392. int nut_init(void);
  393. /* wc3movie.c */
  394. int wc3_init(void);
  395. /* westwood.c */
  396. int westwood_init(void);
  397. /* segafilm.c */
  398. int film_init(void);
  399. /* idcin.c */
  400. int idcin_init(void);
  401. /* flic.c */
  402. int flic_init(void);
  403. /* sierravmd.c */
  404. int vmd_init(void);
  405. /* matroska.c */
  406. int matroska_init(void);
  407. /* sol.c */
  408. int sol_init(void);
  409. #include "rtp.h"
  410. #include "rtsp.h"
  411. /* yuv4mpeg.c */
  412. extern AVOutputFormat yuv4mpegpipe_oformat;
  413. /* utils.c */
  414. void av_register_input_format(AVInputFormat *format);
  415. void av_register_output_format(AVOutputFormat *format);
  416. AVOutputFormat *guess_stream_format(const char *short_name,
  417. const char *filename, const char *mime_type);
  418. AVOutputFormat *guess_format(const char *short_name,
  419. const char *filename, const char *mime_type);
  420. void av_hex_dump(FILE *f, uint8_t *buf, int size);
  421. void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
  422. void av_register_all(void);
  423. typedef struct FifoBuffer {
  424. uint8_t *buffer;
  425. uint8_t *rptr, *wptr, *end;
  426. } FifoBuffer;
  427. int fifo_init(FifoBuffer *f, int size);
  428. void fifo_free(FifoBuffer *f);
  429. int fifo_size(FifoBuffer *f, uint8_t *rptr);
  430. int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr);
  431. void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr);
  432. /* media file input */
  433. AVInputFormat *av_find_input_format(const char *short_name);
  434. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
  435. int av_open_input_stream(AVFormatContext **ic_ptr,
  436. ByteIOContext *pb, const char *filename,
  437. AVInputFormat *fmt, AVFormatParameters *ap);
  438. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
  439. AVInputFormat *fmt,
  440. int buf_size,
  441. AVFormatParameters *ap);
  442. /* no av_open for output, so applications will need this: */
  443. AVFormatContext *av_alloc_format_context(void);
  444. #define AVERROR_UNKNOWN (-1) /* unknown error */
  445. #define AVERROR_IO (-2) /* i/o error */
  446. #define AVERROR_NUMEXPECTED (-3) /* number syntax expected in filename */
  447. #define AVERROR_INVALIDDATA (-4) /* invalid data found */
  448. #define AVERROR_NOMEM (-5) /* not enough memory */
  449. #define AVERROR_NOFMT (-6) /* unknown format */
  450. #define AVERROR_NOTSUPP (-7) /* operation not supported */
  451. int av_find_stream_info(AVFormatContext *ic);
  452. int av_read_packet(AVFormatContext *s, AVPacket *pkt);
  453. int av_read_frame(AVFormatContext *s, AVPacket *pkt);
  454. int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp);
  455. int av_read_play(AVFormatContext *s);
  456. int av_read_pause(AVFormatContext *s);
  457. void av_close_input_file(AVFormatContext *s);
  458. AVStream *av_new_stream(AVFormatContext *s, int id);
  459. void av_set_pts_info(AVStream *s, int pts_wrap_bits,
  460. int pts_num, int pts_den);
  461. int av_find_default_stream_index(AVFormatContext *s);
  462. int av_index_search_timestamp(AVStream *st, int timestamp);
  463. int av_add_index_entry(AVStream *st,
  464. int64_t pos, int64_t timestamp, int distance, int flags);
  465. int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts);
  466. /* media file output */
  467. int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
  468. int av_write_header(AVFormatContext *s);
  469. int av_write_frame(AVFormatContext *s, AVPacket *pkt);
  470. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
  471. int av_write_trailer(AVFormatContext *s);
  472. void dump_format(AVFormatContext *ic,
  473. int index,
  474. const char *url,
  475. int is_output);
  476. int parse_image_size(int *width_ptr, int *height_ptr, const char *str);
  477. int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg);
  478. int64_t parse_date(const char *datestr, int duration);
  479. int64_t av_gettime(void);
  480. /* ffm specific for ffserver */
  481. #define FFM_PACKET_SIZE 4096
  482. offset_t ffm_read_write_index(int fd);
  483. void ffm_write_write_index(int fd, offset_t pos);
  484. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size);
  485. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);
  486. int get_frame_filename(char *buf, int buf_size,
  487. const char *path, int number);
  488. int filename_number_test(const char *filename);
  489. /* grab specific */
  490. int video_grab_init(void);
  491. int audio_init(void);
  492. /* DV1394 */
  493. int dv1394_init(void);
  494. #ifdef HAVE_AV_CONFIG_H
  495. #include "os_support.h"
  496. int strstart(const char *str, const char *val, const char **ptr);
  497. int stristart(const char *str, const char *val, const char **ptr);
  498. void pstrcpy(char *buf, int buf_size, const char *str);
  499. char *pstrcat(char *buf, int buf_size, const char *s);
  500. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem);
  501. #ifdef __GNUC__
  502. #define dynarray_add(tab, nb_ptr, elem)\
  503. do {\
  504. typeof(tab) _tab = (tab);\
  505. typeof(elem) _elem = (elem);\
  506. (void)sizeof(**_tab == _elem); /* check that types are compatible */\
  507. __dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
  508. } while(0)
  509. #else
  510. #define dynarray_add(tab, nb_ptr, elem)\
  511. do {\
  512. __dynarray_add((unsigned long **)(tab), nb_ptr, (unsigned long)(elem));\
  513. } while(0)
  514. #endif
  515. time_t mktimegm(struct tm *tm);
  516. struct tm *brktimegm(time_t secs, 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 *authorization, int authorization_size,
  523. char *hostname, int hostname_size,
  524. int *port_ptr,
  525. char *path, int path_size,
  526. const char *url);
  527. int match_ext(const char *filename, const char *extensions);
  528. #endif /* HAVE_AV_CONFIG_H */
  529. #ifdef __cplusplus
  530. }
  531. #endif
  532. #endif /* AVFORMAT_H */