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.

553 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. typedef struct AVOutputFormat {
  121. const char *name;
  122. const char *long_name;
  123. const char *mime_type;
  124. const char *extensions; /* comma separated extensions */
  125. /* size of private data so that it can be allocated in the wrapper */
  126. int priv_data_size;
  127. /* output support */
  128. enum CodecID audio_codec; /* default audio codec */
  129. enum CodecID video_codec; /* default video codec */
  130. int (*write_header)(struct AVFormatContext *);
  131. int (*write_packet)(struct AVFormatContext *, AVPacket *pkt);
  132. int (*write_trailer)(struct AVFormatContext *);
  133. /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER */
  134. int flags;
  135. /* currently only used to set pixel format if not YUV420P */
  136. int (*set_parameters)(struct AVFormatContext *, AVFormatParameters *);
  137. int (*interleave_packet)(struct AVFormatContext *, AVPacket *out, AVPacket *in, int flush);
  138. /**
  139. * list of supported codec_id-codec_tag pairs, ordered by "better choice first"
  140. * the arrays are all CODEC_ID_NONE terminated
  141. */
  142. const struct AVCodecTag **codec_tag;
  143. /* private fields */
  144. struct AVOutputFormat *next;
  145. } AVOutputFormat;
  146. typedef struct AVInputFormat {
  147. const char *name;
  148. const char *long_name;
  149. /* size of private data so that it can be allocated in the wrapper */
  150. int priv_data_size;
  151. /* tell if a given file has a chance of being parsing by this format */
  152. int (*read_probe)(AVProbeData *);
  153. /* read the format header and initialize the AVFormatContext
  154. structure. Return 0 if OK. 'ap' if non NULL contains
  155. additionnal paramters. Only used in raw format right
  156. now. 'av_new_stream' should be called to create new streams. */
  157. int (*read_header)(struct AVFormatContext *,
  158. AVFormatParameters *ap);
  159. /* read one packet and put it in 'pkt'. pts and flags are also
  160. set. 'av_new_stream' can be called only if the flag
  161. AVFMTCTX_NOHEADER is used. */
  162. int (*read_packet)(struct AVFormatContext *, AVPacket *pkt);
  163. /* close the stream. The AVFormatContext and AVStreams are not
  164. freed by this function */
  165. int (*read_close)(struct AVFormatContext *);
  166. /**
  167. * seek to a given timestamp relative to the frames in
  168. * stream component stream_index
  169. * @param stream_index must not be -1
  170. * @param flags selects which direction should be preferred if no exact
  171. * match is available
  172. */
  173. int (*read_seek)(struct AVFormatContext *,
  174. int stream_index, int64_t timestamp, int flags);
  175. /**
  176. * gets the next timestamp in AV_TIME_BASE units.
  177. */
  178. int64_t (*read_timestamp)(struct AVFormatContext *s, int stream_index,
  179. int64_t *pos, int64_t pos_limit);
  180. /* can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER */
  181. int flags;
  182. /* if extensions are defined, then no probe is done. You should
  183. usually not use extension format guessing because it is not
  184. reliable enough */
  185. const char *extensions;
  186. /* general purpose read only value that the format can use */
  187. int value;
  188. /* start/resume playing - only meaningful if using a network based format
  189. (RTSP) */
  190. int (*read_play)(struct AVFormatContext *);
  191. /* pause playing - only meaningful if using a network based format
  192. (RTSP) */
  193. int (*read_pause)(struct AVFormatContext *);
  194. const struct AVCodecTag **codec_tag;
  195. /* private fields */
  196. struct AVInputFormat *next;
  197. } AVInputFormat;
  198. typedef struct AVIndexEntry {
  199. int64_t pos;
  200. int64_t timestamp;
  201. #define AVINDEX_KEYFRAME 0x0001
  202. int flags:2;
  203. 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)
  204. int min_distance; /* min distance between this and the previous keyframe, used to avoid unneeded searching */
  205. } AVIndexEntry;
  206. typedef struct AVStream {
  207. int index; /* stream index in AVFormatContext */
  208. int id; /* format specific stream id */
  209. AVCodecContext *codec; /* codec context */
  210. /**
  211. * real base frame rate of the stream.
  212. * this is the lowest framerate with which all timestamps can be
  213. * represented accurately (its the least common multiple of all
  214. * framerates in the stream), Note, this value is just a guess!
  215. * for example if the timebase is 1/90000 and all frames have either
  216. * approximately 3600 or 1800 timer ticks then r_frame_rate will be 50/1
  217. */
  218. AVRational r_frame_rate;
  219. void *priv_data;
  220. /* internal data used in av_find_stream_info() */
  221. int64_t codec_info_duration;
  222. int codec_info_nb_frames;
  223. /* encoding: PTS generation when outputing stream */
  224. AVFrac pts;
  225. /**
  226. * this is the fundamental unit of time (in seconds) in terms
  227. * of which frame timestamps are represented. for fixed-fps content,
  228. * timebase should be 1/framerate and timestamp increments should be
  229. * identically 1.
  230. */
  231. AVRational time_base;
  232. int pts_wrap_bits; /* number of bits in pts (used for wrapping control) */
  233. /* ffmpeg.c private use */
  234. int stream_copy; /* if TRUE, just copy stream */
  235. enum AVDiscard discard; ///< selects which packets can be discarded at will and dont need to be demuxed
  236. //FIXME move stuff to a flags field?
  237. /* quality, as it has been removed from AVCodecContext and put in AVVideoFrame
  238. * MN:dunno if thats the right place, for it */
  239. float quality;
  240. /* decoding: position of the first frame of the component, in
  241. AV_TIME_BASE fractional seconds. */
  242. int64_t start_time;
  243. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  244. seconds. */
  245. int64_t duration;
  246. char language[4]; /* ISO 639 3-letter language code (empty string if undefined) */
  247. /* av_read_frame() support */
  248. int need_parsing; ///< 1->full parsing needed, 2->only parse headers dont repack
  249. struct AVCodecParserContext *parser;
  250. int64_t cur_dts;
  251. int last_IP_duration;
  252. int64_t last_IP_pts;
  253. /* av_seek_frame() support */
  254. AVIndexEntry *index_entries; /* only used if the format does not
  255. support seeking natively */
  256. int nb_index_entries;
  257. unsigned int index_entries_allocated_size;
  258. int64_t nb_frames; ///< number of frames in this stream if known or 0
  259. #define MAX_REORDER_DELAY 4
  260. int64_t pts_buffer[MAX_REORDER_DELAY+1];
  261. } AVStream;
  262. #define AVFMTCTX_NOHEADER 0x0001 /* signal that no header is present
  263. (streams are added dynamically) */
  264. #define MAX_STREAMS 20
  265. /* format I/O context */
  266. typedef struct AVFormatContext {
  267. const AVClass *av_class; /* set by av_alloc_format_context */
  268. /* can only be iformat or oformat, not both at the same time */
  269. struct AVInputFormat *iformat;
  270. struct AVOutputFormat *oformat;
  271. void *priv_data;
  272. ByteIOContext pb;
  273. unsigned int nb_streams;
  274. AVStream *streams[MAX_STREAMS];
  275. char filename[1024]; /* input or output filename */
  276. /* stream info */
  277. int64_t timestamp;
  278. char title[512];
  279. char author[512];
  280. char copyright[512];
  281. char comment[512];
  282. char album[512];
  283. int year; /* ID3 year, 0 if none */
  284. int track; /* track number, 0 if none */
  285. char genre[32]; /* ID3 genre */
  286. int ctx_flags; /* format specific flags, see AVFMTCTX_xx */
  287. /* private data for pts handling (do not modify directly) */
  288. /* This buffer is only needed when packets were already buffered but
  289. not decoded, for example to get the codec parameters in mpeg
  290. streams */
  291. struct AVPacketList *packet_buffer;
  292. /* decoding: position of the first frame of the component, in
  293. AV_TIME_BASE fractional seconds. NEVER set this value directly:
  294. it is deduced from the AVStream values. */
  295. int64_t start_time;
  296. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  297. seconds. NEVER set this value directly: it is deduced from the
  298. AVStream values. */
  299. int64_t duration;
  300. /* decoding: total file size. 0 if unknown */
  301. int64_t file_size;
  302. /* decoding: total stream bitrate in bit/s, 0 if not
  303. available. Never set it directly if the file_size and the
  304. duration are known as ffmpeg can compute it automatically. */
  305. int bit_rate;
  306. /* av_read_frame() support */
  307. AVStream *cur_st;
  308. const uint8_t *cur_ptr;
  309. int cur_len;
  310. AVPacket cur_pkt;
  311. /* av_seek_frame() support */
  312. int64_t data_offset; /* offset of the first packet */
  313. int index_built;
  314. int mux_rate;
  315. int packet_size;
  316. int preload;
  317. int max_delay;
  318. #define AVFMT_NOOUTPUTLOOP -1
  319. #define AVFMT_INFINITEOUTPUTLOOP 0
  320. /* number of times to loop output in formats that support it */
  321. int loop_output;
  322. int flags;
  323. #define AVFMT_FLAG_GENPTS 0x0001 ///< generate pts if missing even if it requires parsing future frames
  324. #define AVFMT_FLAG_IGNIDX 0x0002 ///< ignore index
  325. int loop_input;
  326. /* decoding: size of data to probe; encoding unused */
  327. unsigned int probesize;
  328. /**
  329. * maximum duration in AV_TIME_BASE units over which the input should be analyzed in av_find_stream_info()
  330. */
  331. int max_analyze_duration;
  332. } AVFormatContext;
  333. typedef struct AVPacketList {
  334. AVPacket pkt;
  335. struct AVPacketList *next;
  336. } AVPacketList;
  337. extern AVInputFormat *first_iformat;
  338. extern AVOutputFormat *first_oformat;
  339. enum CodecID av_guess_image2_codec(const char *filename);
  340. /* XXX: use automatic init with either ELF sections or C file parser */
  341. /* modules */
  342. #include "rtp.h"
  343. #include "rtsp.h"
  344. /* utils.c */
  345. void av_register_input_format(AVInputFormat *format);
  346. void av_register_output_format(AVOutputFormat *format);
  347. AVOutputFormat *guess_stream_format(const char *short_name,
  348. const char *filename, const char *mime_type);
  349. AVOutputFormat *guess_format(const char *short_name,
  350. const char *filename, const char *mime_type);
  351. enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
  352. const char *filename, const char *mime_type, enum CodecType type);
  353. void av_hex_dump(FILE *f, uint8_t *buf, int size);
  354. void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
  355. void av_register_all(void);
  356. /* codec tag <-> codec id */
  357. enum CodecID av_codec_get_id(const struct AVCodecTag **tags, unsigned int tag);
  358. unsigned int av_codec_get_tag(const struct AVCodecTag **tags, enum CodecID id);
  359. /* media file input */
  360. AVInputFormat *av_find_input_format(const char *short_name);
  361. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
  362. int av_open_input_stream(AVFormatContext **ic_ptr,
  363. ByteIOContext *pb, const char *filename,
  364. AVInputFormat *fmt, AVFormatParameters *ap);
  365. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
  366. AVInputFormat *fmt,
  367. int buf_size,
  368. AVFormatParameters *ap);
  369. /* no av_open for output, so applications will need this: */
  370. AVFormatContext *av_alloc_format_context(void);
  371. #define AVERROR_UNKNOWN (-1) /* unknown error */
  372. #define AVERROR_IO (-2) /* i/o error */
  373. #define AVERROR_NUMEXPECTED (-3) /* number syntax expected in filename */
  374. #define AVERROR_INVALIDDATA (-4) /* invalid data found */
  375. #define AVERROR_NOMEM (-5) /* not enough memory */
  376. #define AVERROR_NOFMT (-6) /* unknown format */
  377. #define AVERROR_NOTSUPP (-7) /* operation not supported */
  378. int av_find_stream_info(AVFormatContext *ic);
  379. int av_read_packet(AVFormatContext *s, AVPacket *pkt);
  380. int av_read_frame(AVFormatContext *s, AVPacket *pkt);
  381. int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags);
  382. int av_read_play(AVFormatContext *s);
  383. int av_read_pause(AVFormatContext *s);
  384. void av_close_input_file(AVFormatContext *s);
  385. AVStream *av_new_stream(AVFormatContext *s, int id);
  386. void av_set_pts_info(AVStream *s, int pts_wrap_bits,
  387. int pts_num, int pts_den);
  388. #define AVSEEK_FLAG_BACKWARD 1 ///< seek backward
  389. #define AVSEEK_FLAG_BYTE 2 ///< seeking based on position in bytes
  390. #define AVSEEK_FLAG_ANY 4 ///< seek to any frame, even non keyframes
  391. int av_find_default_stream_index(AVFormatContext *s);
  392. int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags);
  393. int av_add_index_entry(AVStream *st,
  394. int64_t pos, int64_t timestamp, int size, int distance, int flags);
  395. int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags);
  396. void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp);
  397. 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 ));
  398. /* media file output */
  399. int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
  400. int av_write_header(AVFormatContext *s);
  401. int av_write_frame(AVFormatContext *s, AVPacket *pkt);
  402. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
  403. int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush);
  404. int av_write_trailer(AVFormatContext *s);
  405. void dump_format(AVFormatContext *ic,
  406. int index,
  407. const char *url,
  408. int is_output);
  409. int parse_image_size(int *width_ptr, int *height_ptr, const char *str);
  410. int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg);
  411. int64_t parse_date(const char *datestr, int duration);
  412. int64_t av_gettime(void);
  413. /* ffm specific for ffserver */
  414. #define FFM_PACKET_SIZE 4096
  415. offset_t ffm_read_write_index(int fd);
  416. void ffm_write_write_index(int fd, offset_t pos);
  417. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size);
  418. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);
  419. int av_get_frame_filename(char *buf, int buf_size,
  420. const char *path, int number);
  421. int av_filename_number_test(const char *filename);
  422. /* grab specific */
  423. int video_grab_init(void);
  424. int audio_init(void);
  425. /* DV1394 */
  426. int dv1394_init(void);
  427. int dc1394_init(void);
  428. #ifdef HAVE_AV_CONFIG_H
  429. #include "os_support.h"
  430. int strstart(const char *str, const char *val, const char **ptr);
  431. int stristart(const char *str, const char *val, const char **ptr);
  432. void pstrcpy(char *buf, int buf_size, const char *str);
  433. char *pstrcat(char *buf, int buf_size, const char *s);
  434. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem);
  435. #ifdef __GNUC__
  436. #define dynarray_add(tab, nb_ptr, elem)\
  437. do {\
  438. typeof(tab) _tab = (tab);\
  439. typeof(elem) _elem = (elem);\
  440. (void)sizeof(**_tab == _elem); /* check that types are compatible */\
  441. __dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
  442. } while(0)
  443. #else
  444. #define dynarray_add(tab, nb_ptr, elem)\
  445. do {\
  446. __dynarray_add((unsigned long **)(tab), nb_ptr, (unsigned long)(elem));\
  447. } while(0)
  448. #endif
  449. time_t mktimegm(struct tm *tm);
  450. struct tm *brktimegm(time_t secs, struct tm *tm);
  451. const char *small_strptime(const char *p, const char *fmt,
  452. struct tm *dt);
  453. struct in_addr;
  454. int resolve_host(struct in_addr *sin_addr, const char *hostname);
  455. void url_split(char *proto, int proto_size,
  456. char *authorization, int authorization_size,
  457. char *hostname, int hostname_size,
  458. int *port_ptr,
  459. char *path, int path_size,
  460. const char *url);
  461. int match_ext(const char *filename, const char *extensions);
  462. #endif /* HAVE_AV_CONFIG_H */
  463. #ifdef __cplusplus
  464. }
  465. #endif
  466. #endif /* AVFORMAT_H */