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.

532 lines
19KB

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