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.

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