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.

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