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.

582 lines
21KB

  1. #ifndef AVFORMAT_H
  2. #define AVFORMAT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #define LIBAVFORMAT_VERSION_INT ((50<<16)+(5<<8)+0)
  7. #define LIBAVFORMAT_VERSION 50.5.0
  8. #define LIBAVFORMAT_BUILD LIBAVFORMAT_VERSION_INT
  9. #define LIBAVFORMAT_IDENT "Lavf" AV_STRINGIFY(LIBAVFORMAT_VERSION)
  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 time_base units
  23. int64_t dts; ///< decompression time stamp in time_base units
  24. uint8_t *data;
  25. int size;
  26. int stream_index;
  27. int flags;
  28. int duration; ///< presentation duration in time_base units (0 if not available)
  29. void (*destruct)(struct AVPacket *);
  30. void *priv;
  31. int64_t pos; ///< byte position in stream, -1 if unknown
  32. } AVPacket;
  33. #define PKT_FLAG_KEY 0x0001
  34. void av_destruct_packet_nofree(AVPacket *pkt);
  35. void av_destruct_packet(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->pos = -1;
  42. pkt->duration = 0;
  43. pkt->flags = 0;
  44. pkt->stream_index = 0;
  45. pkt->destruct= av_destruct_packet_nofree;
  46. }
  47. int av_new_packet(AVPacket *pkt, int size);
  48. int av_get_packet(ByteIOContext *s, AVPacket *pkt, int size);
  49. int av_dup_packet(AVPacket *pkt);
  50. /**
  51. * Free a packet
  52. *
  53. * @param pkt packet to free
  54. */
  55. static inline void av_free_packet(AVPacket *pkt)
  56. {
  57. if (pkt && pkt->destruct) {
  58. pkt->destruct(pkt);
  59. }
  60. }
  61. /*************************************************/
  62. /* fractional numbers for exact pts handling */
  63. /* the exact value of the fractional number is: 'val + num / den'. num
  64. is assumed to be such as 0 <= num < den */
  65. typedef struct AVFrac {
  66. int64_t val, num, den;
  67. } AVFrac attribute_deprecated;
  68. /*************************************************/
  69. /* input/output formats */
  70. struct AVFormatContext;
  71. /* this structure contains the data a format has to probe a file */
  72. typedef struct AVProbeData {
  73. const char *filename;
  74. unsigned char *buf;
  75. int buf_size;
  76. } AVProbeData;
  77. #define AVPROBE_SCORE_MAX 100
  78. typedef struct AVFormatParameters {
  79. AVRational time_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; /* video, 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. int prealloced_context:1;
  96. enum CodecID video_codec_id;
  97. enum CodecID audio_codec_id;
  98. } AVFormatParameters;
  99. #define AVFMT_NOFILE 0x0001 /* no file should be opened */
  100. #define AVFMT_NEEDNUMBER 0x0002 /* needs '%d' in filename */
  101. #define AVFMT_SHOW_IDS 0x0008 /* show format stream IDs numbers */
  102. #define AVFMT_RAWPICTURE 0x0020 /* format wants AVPicture structure for
  103. raw picture data */
  104. #define AVFMT_GLOBALHEADER 0x0040 /* format wants global header */
  105. #define AVFMT_NOTIMESTAMPS 0x0080 /* format doesnt need / has any timestamps */
  106. typedef struct AVOutputFormat {
  107. const char *name;
  108. const char *long_name;
  109. const char *mime_type;
  110. const char *extensions; /* comma separated extensions */
  111. /* size of private data so that it can be allocated in the wrapper */
  112. int priv_data_size;
  113. /* output support */
  114. enum CodecID audio_codec; /* default audio codec */
  115. enum CodecID video_codec; /* default video codec */
  116. int (*write_header)(struct AVFormatContext *);
  117. int (*write_packet)(struct AVFormatContext *, AVPacket *pkt);
  118. int (*write_trailer)(struct AVFormatContext *);
  119. /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER */
  120. int flags;
  121. /* currently only used to set pixel format if not YUV420P */
  122. int (*set_parameters)(struct AVFormatContext *, AVFormatParameters *);
  123. int (*interleave_packet)(struct AVFormatContext *, AVPacket *out, AVPacket *in, int flush);
  124. /* private fields */
  125. struct AVOutputFormat *next;
  126. } AVOutputFormat;
  127. typedef struct AVInputFormat {
  128. const char *name;
  129. const char *long_name;
  130. /* size of private data so that it can be allocated in the wrapper */
  131. int priv_data_size;
  132. /* tell if a given file has a chance of being parsing by this format */
  133. int (*read_probe)(AVProbeData *);
  134. /* read the format header and initialize the AVFormatContext
  135. structure. Return 0 if OK. 'ap' if non NULL contains
  136. additionnal paramters. Only used in raw format right
  137. now. 'av_new_stream' should be called to create new streams. */
  138. int (*read_header)(struct AVFormatContext *,
  139. AVFormatParameters *ap);
  140. /* read one packet and put it in 'pkt'. pts and flags are also
  141. set. 'av_new_stream' can be called only if the flag
  142. AVFMTCTX_NOHEADER is used. */
  143. int (*read_packet)(struct AVFormatContext *, AVPacket *pkt);
  144. /* close the stream. The AVFormatContext and AVStreams are not
  145. freed by this function */
  146. int (*read_close)(struct AVFormatContext *);
  147. /**
  148. * seek to a given timestamp relative to the frames in
  149. * stream component stream_index
  150. * @param stream_index must not be -1
  151. * @param flags selects which direction should be preferred if no exact
  152. * match is available
  153. */
  154. int (*read_seek)(struct AVFormatContext *,
  155. int stream_index, int64_t timestamp, int flags);
  156. /**
  157. * gets the next timestamp in AV_TIME_BASE units.
  158. */
  159. int64_t (*read_timestamp)(struct AVFormatContext *s, int stream_index,
  160. int64_t *pos, int64_t pos_limit);
  161. /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER */
  162. int flags;
  163. /* if extensions are defined, then no probe is done. You should
  164. usually not use extension format guessing because it is not
  165. reliable enough */
  166. const char *extensions;
  167. /* general purpose read only value that the format can use */
  168. int value;
  169. /* start/resume playing - only meaningful if using a network based format
  170. (RTSP) */
  171. int (*read_play)(struct AVFormatContext *);
  172. /* pause playing - only meaningful if using a network based format
  173. (RTSP) */
  174. int (*read_pause)(struct AVFormatContext *);
  175. /* private fields */
  176. struct AVInputFormat *next;
  177. } AVInputFormat;
  178. typedef struct AVIndexEntry {
  179. int64_t pos;
  180. int64_t timestamp;
  181. #define AVINDEX_KEYFRAME 0x0001
  182. int flags:2;
  183. int size:30; //yeah trying to keep the size of this small to reduce memory requirements (its 24 vs 32 byte due to possible 8byte align)
  184. int min_distance; /* min distance between this and the previous keyframe, used to avoid unneeded searching */
  185. } AVIndexEntry;
  186. typedef struct AVStream {
  187. int index; /* stream index in AVFormatContext */
  188. int id; /* format specific stream id */
  189. AVCodecContext *codec; /* codec context */
  190. /**
  191. * real base frame rate of the stream.
  192. * for example if the timebase is 1/90000 and all frames have either
  193. * approximately 3600 or 1800 timer ticks then r_frame_rate will be 50/1
  194. */
  195. AVRational r_frame_rate;
  196. void *priv_data;
  197. /* internal data used in av_find_stream_info() */
  198. int64_t codec_info_duration;
  199. int codec_info_nb_frames;
  200. /* encoding: PTS generation when outputing stream */
  201. AVFrac pts;
  202. /**
  203. * this is the fundamental unit of time (in seconds) in terms
  204. * of which frame timestamps are represented. for fixed-fps content,
  205. * timebase should be 1/framerate and timestamp increments should be
  206. * identically 1.
  207. */
  208. AVRational time_base;
  209. int pts_wrap_bits; /* number of bits in pts (used for wrapping control) */
  210. /* ffmpeg.c private use */
  211. int stream_copy; /* if TRUE, just copy stream */
  212. enum AVDiscard discard; ///< selects which packets can be discarded at will and dont need to be demuxed
  213. //FIXME move stuff to a flags field?
  214. /* quality, as it has been removed from AVCodecContext and put in AVVideoFrame
  215. * MN:dunno if thats the right place, for it */
  216. float quality;
  217. /* decoding: position of the first frame of the component, in
  218. AV_TIME_BASE fractional seconds. */
  219. int64_t start_time;
  220. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  221. seconds. */
  222. int64_t duration;
  223. char language[4]; /* ISO 639 3-letter language code (empty string if undefined) */
  224. /* av_read_frame() support */
  225. int need_parsing; ///< 1->full parsing needed, 2->only parse headers dont repack
  226. struct AVCodecParserContext *parser;
  227. int64_t cur_dts;
  228. int last_IP_duration;
  229. int64_t last_IP_pts;
  230. /* av_seek_frame() support */
  231. AVIndexEntry *index_entries; /* only used if the format does not
  232. support seeking natively */
  233. int nb_index_entries;
  234. int index_entries_allocated_size;
  235. int64_t nb_frames; ///< number of frames in this stream if known or 0
  236. #define MAX_REORDER_DELAY 4
  237. int64_t pts_buffer[MAX_REORDER_DELAY+1];
  238. } AVStream;
  239. #define AVFMTCTX_NOHEADER 0x0001 /* signal that no header is present
  240. (streams are added dynamically) */
  241. #define MAX_STREAMS 20
  242. /* format I/O context */
  243. typedef struct AVFormatContext {
  244. const AVClass *av_class; /* set by av_alloc_format_context */
  245. /* can only be iformat or oformat, not both at the same time */
  246. struct AVInputFormat *iformat;
  247. struct AVOutputFormat *oformat;
  248. void *priv_data;
  249. ByteIOContext pb;
  250. int nb_streams;
  251. AVStream *streams[MAX_STREAMS];
  252. char filename[1024]; /* input or output filename */
  253. /* stream info */
  254. int64_t timestamp;
  255. char title[512];
  256. char author[512];
  257. char copyright[512];
  258. char comment[512];
  259. char album[512];
  260. int year; /* ID3 year, 0 if none */
  261. int track; /* track number, 0 if none */
  262. char genre[32]; /* ID3 genre */
  263. int ctx_flags; /* format specific flags, see AVFMTCTX_xx */
  264. /* private data for pts handling (do not modify directly) */
  265. /* This buffer is only needed when packets were already buffered but
  266. not decoded, for example to get the codec parameters in mpeg
  267. streams */
  268. struct AVPacketList *packet_buffer;
  269. /* decoding: position of the first frame of the component, in
  270. AV_TIME_BASE fractional seconds. NEVER set this value directly:
  271. it is deduced from the AVStream values. */
  272. int64_t start_time;
  273. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  274. seconds. NEVER set this value directly: it is deduced from the
  275. AVStream values. */
  276. int64_t duration;
  277. /* decoding: total file size. 0 if unknown */
  278. int64_t file_size;
  279. /* decoding: total stream bitrate in bit/s, 0 if not
  280. available. Never set it directly if the file_size and the
  281. duration are known as ffmpeg can compute it automatically. */
  282. int bit_rate;
  283. /* av_read_frame() support */
  284. AVStream *cur_st;
  285. const uint8_t *cur_ptr;
  286. int cur_len;
  287. AVPacket cur_pkt;
  288. /* av_seek_frame() support */
  289. int64_t data_offset; /* offset of the first packet */
  290. int index_built;
  291. int mux_rate;
  292. int packet_size;
  293. int preload;
  294. int max_delay;
  295. #define AVFMT_NOOUTPUTLOOP -1
  296. #define AVFMT_INFINITEOUTPUTLOOP 0
  297. /* number of times to loop output in formats that support it */
  298. int loop_output;
  299. int flags;
  300. #define AVFMT_FLAG_GENPTS 0x0001 ///< generate pts if missing even if it requires parsing future frames
  301. #define AVFMT_FLAG_IGNIDX 0x0002 ///< ignore index
  302. int loop_input;
  303. /* decoding: size of data to probe; encoding unused */
  304. unsigned int probesize;
  305. } AVFormatContext;
  306. typedef struct AVPacketList {
  307. AVPacket pkt;
  308. struct AVPacketList *next;
  309. } AVPacketList;
  310. extern AVInputFormat *first_iformat;
  311. extern AVOutputFormat *first_oformat;
  312. /* still image support */
  313. struct AVInputImageContext attribute_deprecated;
  314. typedef struct AVInputImageContext AVInputImageContext attribute_deprecated;
  315. typedef struct AVImageInfo {
  316. enum PixelFormat pix_fmt; /* requested pixel format */
  317. int width; /* requested width */
  318. int height; /* requested height */
  319. int interleaved; /* image is interleaved (e.g. interleaved GIF) */
  320. AVPicture pict; /* returned allocated image */
  321. } AVImageInfo attribute_deprecated;
  322. /* AVImageFormat.flags field constants */
  323. #define AVIMAGE_INTERLEAVED 0x0001 /* image format support interleaved output */
  324. typedef struct AVImageFormat {
  325. const char *name;
  326. const char *extensions;
  327. /* tell if a given file has a chance of being parsing by this format */
  328. int (*img_probe)(AVProbeData *);
  329. /* read a whole image. 'alloc_cb' is called when the image size is
  330. known so that the caller can allocate the image. If 'allo_cb'
  331. returns non zero, then the parsing is aborted. Return '0' if
  332. OK. */
  333. int (*img_read)(ByteIOContext *,
  334. int (*alloc_cb)(void *, AVImageInfo *info), void *);
  335. /* write the image */
  336. int supported_pixel_formats; /* mask of supported formats for output */
  337. int (*img_write)(ByteIOContext *, AVImageInfo *);
  338. int flags;
  339. struct AVImageFormat *next;
  340. } AVImageFormat attribute_deprecated;
  341. void av_register_image_format(AVImageFormat *img_fmt) attribute_deprecated;
  342. AVImageFormat *av_probe_image_format(AVProbeData *pd) attribute_deprecated;
  343. AVImageFormat *guess_image_format(const char *filename) attribute_deprecated;
  344. enum CodecID av_guess_image2_codec(const char *filename);
  345. int av_read_image(ByteIOContext *pb, const char *filename,
  346. AVImageFormat *fmt,
  347. int (*alloc_cb)(void *, AVImageInfo *info), void *opaque) attribute_deprecated;
  348. int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img) attribute_deprecated;
  349. extern AVImageFormat *first_image_format attribute_deprecated;
  350. /* XXX: use automatic init with either ELF sections or C file parser */
  351. /* modules */
  352. #include "rtp.h"
  353. #include "rtsp.h"
  354. /* utils.c */
  355. void av_register_input_format(AVInputFormat *format);
  356. void av_register_output_format(AVOutputFormat *format);
  357. AVOutputFormat *guess_stream_format(const char *short_name,
  358. const char *filename, const char *mime_type);
  359. AVOutputFormat *guess_format(const char *short_name,
  360. const char *filename, const char *mime_type);
  361. enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
  362. const char *filename, const char *mime_type, enum CodecType type);
  363. void av_hex_dump(FILE *f, uint8_t *buf, int size);
  364. void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
  365. void av_register_all(void);
  366. typedef struct FifoBuffer {
  367. uint8_t *buffer;
  368. uint8_t *rptr, *wptr, *end;
  369. } FifoBuffer;
  370. int fifo_init(FifoBuffer *f, int size);
  371. void fifo_free(FifoBuffer *f);
  372. int fifo_size(FifoBuffer *f, uint8_t *rptr);
  373. int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr);
  374. void fifo_write(FifoBuffer *f, const uint8_t *buf, int size, uint8_t **wptr_ptr);
  375. int put_fifo(ByteIOContext *pb, FifoBuffer *f, int buf_size, uint8_t **rptr_ptr);
  376. void fifo_realloc(FifoBuffer *f, unsigned int size);
  377. static inline uint8_t fifo_peek(FifoBuffer *f, int offs)
  378. {
  379. return f->buffer[(f->rptr - f->buffer + offs) % (f->end - f->buffer)];
  380. }
  381. static inline void fifo_drain(FifoBuffer *f, int size)
  382. {
  383. f->rptr += size;
  384. if (f->rptr >= f->end)
  385. f->rptr = f->buffer + (f->rptr - f->end);
  386. }
  387. /* media file input */
  388. AVInputFormat *av_find_input_format(const char *short_name);
  389. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
  390. int av_open_input_stream(AVFormatContext **ic_ptr,
  391. ByteIOContext *pb, const char *filename,
  392. AVInputFormat *fmt, AVFormatParameters *ap);
  393. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
  394. AVInputFormat *fmt,
  395. int buf_size,
  396. AVFormatParameters *ap);
  397. /* no av_open for output, so applications will need this: */
  398. AVFormatContext *av_alloc_format_context(void);
  399. #define AVERROR_UNKNOWN (-1) /* unknown error */
  400. #define AVERROR_IO (-2) /* i/o error */
  401. #define AVERROR_NUMEXPECTED (-3) /* number syntax expected in filename */
  402. #define AVERROR_INVALIDDATA (-4) /* invalid data found */
  403. #define AVERROR_NOMEM (-5) /* not enough memory */
  404. #define AVERROR_NOFMT (-6) /* unknown format */
  405. #define AVERROR_NOTSUPP (-7) /* operation not supported */
  406. int av_find_stream_info(AVFormatContext *ic);
  407. int av_read_packet(AVFormatContext *s, AVPacket *pkt);
  408. int av_read_frame(AVFormatContext *s, AVPacket *pkt);
  409. int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags);
  410. int av_read_play(AVFormatContext *s);
  411. int av_read_pause(AVFormatContext *s);
  412. void av_close_input_file(AVFormatContext *s);
  413. AVStream *av_new_stream(AVFormatContext *s, int id);
  414. void av_set_pts_info(AVStream *s, int pts_wrap_bits,
  415. int pts_num, int pts_den);
  416. #define AVSEEK_FLAG_BACKWARD 1 ///< seek backward
  417. #define AVSEEK_FLAG_BYTE 2 ///< seeking based on position in bytes
  418. #define AVSEEK_FLAG_ANY 4 ///< seek to any frame, even non keyframes
  419. int av_find_default_stream_index(AVFormatContext *s);
  420. int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags);
  421. int av_add_index_entry(AVStream *st,
  422. int64_t pos, int64_t timestamp, int size, int distance, int flags);
  423. int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags);
  424. void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp);
  425. /* media file output */
  426. int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
  427. int av_write_header(AVFormatContext *s);
  428. int av_write_frame(AVFormatContext *s, AVPacket *pkt);
  429. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
  430. int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush);
  431. int av_write_trailer(AVFormatContext *s);
  432. void dump_format(AVFormatContext *ic,
  433. int index,
  434. const char *url,
  435. int is_output);
  436. int parse_image_size(int *width_ptr, int *height_ptr, const char *str);
  437. int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg);
  438. int64_t parse_date(const char *datestr, int duration);
  439. int64_t av_gettime(void);
  440. /* ffm specific for ffserver */
  441. #define FFM_PACKET_SIZE 4096
  442. offset_t ffm_read_write_index(int fd);
  443. void ffm_write_write_index(int fd, offset_t pos);
  444. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size);
  445. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);
  446. int av_get_frame_filename(char *buf, int buf_size,
  447. const char *path, int number);
  448. int av_filename_number_test(const char *filename);
  449. /* grab specific */
  450. int video_grab_init(void);
  451. int audio_init(void);
  452. /* DV1394 */
  453. int dv1394_init(void);
  454. int dc1394_init(void);
  455. #ifdef HAVE_AV_CONFIG_H
  456. #include "os_support.h"
  457. int strstart(const char *str, const char *val, const char **ptr);
  458. int stristart(const char *str, const char *val, const char **ptr);
  459. void pstrcpy(char *buf, int buf_size, const char *str);
  460. char *pstrcat(char *buf, int buf_size, const char *s);
  461. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem);
  462. #ifdef __GNUC__
  463. #define dynarray_add(tab, nb_ptr, elem)\
  464. do {\
  465. typeof(tab) _tab = (tab);\
  466. typeof(elem) _elem = (elem);\
  467. (void)sizeof(**_tab == _elem); /* check that types are compatible */\
  468. __dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
  469. } while(0)
  470. #else
  471. #define dynarray_add(tab, nb_ptr, elem)\
  472. do {\
  473. __dynarray_add((unsigned long **)(tab), nb_ptr, (unsigned long)(elem));\
  474. } while(0)
  475. #endif
  476. time_t mktimegm(struct tm *tm);
  477. struct tm *brktimegm(time_t secs, struct tm *tm);
  478. const char *small_strptime(const char *p, const char *fmt,
  479. struct tm *dt);
  480. struct in_addr;
  481. int resolve_host(struct in_addr *sin_addr, const char *hostname);
  482. void url_split(char *proto, int proto_size,
  483. char *authorization, int authorization_size,
  484. char *hostname, int hostname_size,
  485. int *port_ptr,
  486. char *path, int path_size,
  487. const char *url);
  488. int match_ext(const char *filename, const char *extensions);
  489. #endif /* HAVE_AV_CONFIG_H */
  490. #ifdef __cplusplus
  491. }
  492. #endif
  493. #endif /* AVFORMAT_H */