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.

554 lines
20KB

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