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.

652 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. 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. /* 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. AVRational time_base;
  190. int pts_wrap_bits; /* number of bits in pts (used for wrapping control) */
  191. /* ffmpeg.c private use */
  192. int stream_copy; /* if TRUE, just copy stream */
  193. /* quality, as it has been removed from AVCodecContext and put in AVVideoFrame
  194. * MN:dunno if thats the right place, for it */
  195. float quality;
  196. /* decoding: position of the first frame of the component, in
  197. AV_TIME_BASE fractional seconds. */
  198. int64_t start_time;
  199. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  200. seconds. */
  201. int64_t duration;
  202. /* av_read_frame() support */
  203. int need_parsing;
  204. struct AVCodecParserContext *parser;
  205. int64_t cur_dts;
  206. int last_IP_duration;
  207. int64_t last_IP_pts;
  208. /* av_seek_frame() support */
  209. AVIndexEntry *index_entries; /* only used if the format does not
  210. support seeking natively */
  211. int nb_index_entries;
  212. int index_entries_allocated_size;
  213. } AVStream;
  214. #define AVFMTCTX_NOHEADER 0x0001 /* signal that no header is present
  215. (streams are added dynamically) */
  216. #define MAX_STREAMS 20
  217. /* format I/O context */
  218. typedef struct AVFormatContext {
  219. AVClass *av_class; /* set by av_alloc_format_context */
  220. /* can only be iformat or oformat, not both at the same time */
  221. struct AVInputFormat *iformat;
  222. struct AVOutputFormat *oformat;
  223. void *priv_data;
  224. ByteIOContext pb;
  225. int nb_streams;
  226. AVStream *streams[MAX_STREAMS];
  227. char filename[1024]; /* input or output filename */
  228. /* stream info */
  229. int64_t timestamp;
  230. char title[512];
  231. char author[512];
  232. char copyright[512];
  233. char comment[512];
  234. char album[512];
  235. int year; /* ID3 year, 0 if none */
  236. int track; /* track number, 0 if none */
  237. char genre[32]; /* ID3 genre */
  238. int ctx_flags; /* format specific flags, see AVFMTCTX_xx */
  239. /* private data for pts handling (do not modify directly) */
  240. /* This buffer is only needed when packets were already buffered but
  241. not decoded, for example to get the codec parameters in mpeg
  242. streams */
  243. struct AVPacketList *packet_buffer;
  244. /* decoding: position of the first frame of the component, in
  245. AV_TIME_BASE fractional seconds. NEVER set this value directly:
  246. it is deduced from the AVStream values. */
  247. int64_t start_time;
  248. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  249. seconds. NEVER set this value directly: it is deduced from the
  250. AVStream values. */
  251. int64_t duration;
  252. /* decoding: total file size. 0 if unknown */
  253. int64_t file_size;
  254. /* decoding: total stream bitrate in bit/s, 0 if not
  255. available. Never set it directly if the file_size and the
  256. duration are known as ffmpeg can compute it automatically. */
  257. int bit_rate;
  258. /* av_read_frame() support */
  259. AVStream *cur_st;
  260. const uint8_t *cur_ptr;
  261. int cur_len;
  262. AVPacket cur_pkt;
  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. /* img2.c */
  338. int img2_init(void);
  339. /* asf.c */
  340. int asf_init(void);
  341. /* avienc.c */
  342. int avienc_init(void);
  343. /* avidec.c */
  344. int avidec_init(void);
  345. /* swf.c */
  346. int swf_init(void);
  347. /* mov.c */
  348. int mov_init(void);
  349. /* movenc.c */
  350. int movenc_init(void);
  351. /* flvenc.c */
  352. int flvenc_init(void);
  353. /* flvdec.c */
  354. int flvdec_init(void);
  355. /* jpeg.c */
  356. int jpeg_init(void);
  357. /* gif.c */
  358. int gif_init(void);
  359. /* au.c */
  360. int au_init(void);
  361. /* amr.c */
  362. int amr_init(void);
  363. /* wav.c */
  364. int ff_wav_init(void);
  365. /* raw.c */
  366. int pcm_read_seek(AVFormatContext *s,
  367. int stream_index, int64_t timestamp);
  368. int raw_init(void);
  369. /* mp3.c */
  370. int mp3_init(void);
  371. /* yuv4mpeg.c */
  372. int yuv4mpeg_init(void);
  373. /* ogg.c */
  374. int ogg_init(void);
  375. /* dv.c */
  376. int ff_dv_init(void);
  377. /* ffm.c */
  378. int ffm_init(void);
  379. /* rtsp.c */
  380. extern AVInputFormat redir_demux;
  381. int redir_open(AVFormatContext **ic_ptr, ByteIOContext *f);
  382. /* 4xm.c */
  383. int fourxm_init(void);
  384. /* psxstr.c */
  385. int str_init(void);
  386. /* idroq.c */
  387. int roq_init(void);
  388. /* ipmovie.c */
  389. int ipmovie_init(void);
  390. /* nut.c */
  391. int nut_init(void);
  392. /* wc3movie.c */
  393. int wc3_init(void);
  394. /* westwood.c */
  395. int westwood_init(void);
  396. /* segafilm.c */
  397. int film_init(void);
  398. /* idcin.c */
  399. int idcin_init(void);
  400. /* flic.c */
  401. int flic_init(void);
  402. /* sierravmd.c */
  403. int vmd_init(void);
  404. /* matroska.c */
  405. int matroska_init(void);
  406. /* sol.c */
  407. int sol_init(void);
  408. #include "rtp.h"
  409. #include "rtsp.h"
  410. /* yuv4mpeg.c */
  411. extern AVOutputFormat yuv4mpegpipe_oformat;
  412. /* utils.c */
  413. void av_register_input_format(AVInputFormat *format);
  414. void av_register_output_format(AVOutputFormat *format);
  415. AVOutputFormat *guess_stream_format(const char *short_name,
  416. const char *filename, const char *mime_type);
  417. AVOutputFormat *guess_format(const char *short_name,
  418. const char *filename, const char *mime_type);
  419. void av_hex_dump(FILE *f, uint8_t *buf, int size);
  420. void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
  421. void av_register_all(void);
  422. typedef struct FifoBuffer {
  423. uint8_t *buffer;
  424. uint8_t *rptr, *wptr, *end;
  425. } FifoBuffer;
  426. int fifo_init(FifoBuffer *f, int size);
  427. void fifo_free(FifoBuffer *f);
  428. int fifo_size(FifoBuffer *f, uint8_t *rptr);
  429. int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr);
  430. void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr);
  431. /* media file input */
  432. AVInputFormat *av_find_input_format(const char *short_name);
  433. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
  434. int av_open_input_stream(AVFormatContext **ic_ptr,
  435. ByteIOContext *pb, const char *filename,
  436. AVInputFormat *fmt, AVFormatParameters *ap);
  437. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
  438. AVInputFormat *fmt,
  439. int buf_size,
  440. AVFormatParameters *ap);
  441. /* no av_open for output, so applications will need this: */
  442. AVFormatContext *av_alloc_format_context(void);
  443. #define AVERROR_UNKNOWN (-1) /* unknown error */
  444. #define AVERROR_IO (-2) /* i/o error */
  445. #define AVERROR_NUMEXPECTED (-3) /* number syntax expected in filename */
  446. #define AVERROR_INVALIDDATA (-4) /* invalid data found */
  447. #define AVERROR_NOMEM (-5) /* not enough memory */
  448. #define AVERROR_NOFMT (-6) /* unknown format */
  449. #define AVERROR_NOTSUPP (-7) /* operation not supported */
  450. int av_find_stream_info(AVFormatContext *ic);
  451. int av_read_packet(AVFormatContext *s, AVPacket *pkt);
  452. int av_read_frame(AVFormatContext *s, AVPacket *pkt);
  453. int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp);
  454. int av_read_play(AVFormatContext *s);
  455. int av_read_pause(AVFormatContext *s);
  456. void av_close_input_file(AVFormatContext *s);
  457. AVStream *av_new_stream(AVFormatContext *s, int id);
  458. void av_set_pts_info(AVStream *s, int pts_wrap_bits,
  459. int pts_num, int pts_den);
  460. int av_find_default_stream_index(AVFormatContext *s);
  461. int av_index_search_timestamp(AVStream *st, int timestamp);
  462. int av_add_index_entry(AVStream *st,
  463. int64_t pos, int64_t timestamp, int distance, int flags);
  464. int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts);
  465. /* media file output */
  466. int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
  467. int av_write_header(AVFormatContext *s);
  468. int av_write_frame(AVFormatContext *s, AVPacket *pkt);
  469. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
  470. int av_write_trailer(AVFormatContext *s);
  471. void dump_format(AVFormatContext *ic,
  472. int index,
  473. const char *url,
  474. int is_output);
  475. int parse_image_size(int *width_ptr, int *height_ptr, const char *str);
  476. int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg);
  477. int64_t parse_date(const char *datestr, int duration);
  478. int64_t av_gettime(void);
  479. /* ffm specific for ffserver */
  480. #define FFM_PACKET_SIZE 4096
  481. offset_t ffm_read_write_index(int fd);
  482. void ffm_write_write_index(int fd, offset_t pos);
  483. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size);
  484. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);
  485. int get_frame_filename(char *buf, int buf_size,
  486. const char *path, int number);
  487. int filename_number_test(const char *filename);
  488. /* grab specific */
  489. int video_grab_init(void);
  490. int audio_init(void);
  491. /* DV1394 */
  492. int dv1394_init(void);
  493. #ifdef HAVE_AV_CONFIG_H
  494. #include "os_support.h"
  495. int strstart(const char *str, const char *val, const char **ptr);
  496. int stristart(const char *str, const char *val, const char **ptr);
  497. void pstrcpy(char *buf, int buf_size, const char *str);
  498. char *pstrcat(char *buf, int buf_size, const char *s);
  499. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem);
  500. #ifdef __GNUC__
  501. #define dynarray_add(tab, nb_ptr, elem)\
  502. do {\
  503. typeof(tab) _tab = (tab);\
  504. typeof(elem) _elem = (elem);\
  505. (void)sizeof(**_tab == _elem); /* check that types are compatible */\
  506. __dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
  507. } while(0)
  508. #else
  509. #define dynarray_add(tab, nb_ptr, elem)\
  510. do {\
  511. __dynarray_add((unsigned long **)(tab), nb_ptr, (unsigned long)(elem));\
  512. } while(0)
  513. #endif
  514. time_t mktimegm(struct tm *tm);
  515. struct tm *brktimegm(time_t secs, struct tm *tm);
  516. const char *small_strptime(const char *p, const char *fmt,
  517. struct tm *dt);
  518. struct in_addr;
  519. int resolve_host(struct in_addr *sin_addr, const char *hostname);
  520. void url_split(char *proto, int proto_size,
  521. char *authorization, int authorization_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 */