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.

540 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)+(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. * this is the lowest framerate with which all timestamps can be
  211. * represented accurately (its the least common multiple of all
  212. * framerates in the stream), Note, this value is just a guess!
  213. * for example if the timebase is 1/90000 and all frames have either
  214. * approximately 3600 or 1800 timer ticks then r_frame_rate will be 50/1
  215. */
  216. AVRational r_frame_rate;
  217. void *priv_data;
  218. /* internal data used in av_find_stream_info() */
  219. int64_t codec_info_duration;
  220. int codec_info_nb_frames;
  221. /* encoding: PTS generation when outputing stream */
  222. AVFrac pts;
  223. /**
  224. * this is the fundamental unit of time (in seconds) in terms
  225. * of which frame timestamps are represented. for fixed-fps content,
  226. * timebase should be 1/framerate and timestamp increments should be
  227. * identically 1.
  228. */
  229. AVRational time_base;
  230. int pts_wrap_bits; /* number of bits in pts (used for wrapping control) */
  231. /* ffmpeg.c private use */
  232. int stream_copy; /* if TRUE, just copy stream */
  233. enum AVDiscard discard; ///< selects which packets can be discarded at will and dont need to be demuxed
  234. //FIXME move stuff to a flags field?
  235. /* quality, as it has been removed from AVCodecContext and put in AVVideoFrame
  236. * MN:dunno if thats the right place, for it */
  237. float quality;
  238. /* decoding: position of the first frame of the component, in
  239. AV_TIME_BASE fractional seconds. */
  240. int64_t start_time;
  241. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  242. seconds. */
  243. int64_t duration;
  244. char language[4]; /* ISO 639 3-letter language code (empty string if undefined) */
  245. /* av_read_frame() support */
  246. int need_parsing; ///< 1->full parsing needed, 2->only parse headers dont repack
  247. struct AVCodecParserContext *parser;
  248. int64_t cur_dts;
  249. int last_IP_duration;
  250. int64_t last_IP_pts;
  251. /* av_seek_frame() support */
  252. AVIndexEntry *index_entries; /* only used if the format does not
  253. support seeking natively */
  254. int nb_index_entries;
  255. unsigned int index_entries_allocated_size;
  256. int64_t nb_frames; ///< number of frames in this stream if known or 0
  257. #define MAX_REORDER_DELAY 4
  258. int64_t pts_buffer[MAX_REORDER_DELAY+1];
  259. } AVStream;
  260. #define AVFMTCTX_NOHEADER 0x0001 /* signal that no header is present
  261. (streams are added dynamically) */
  262. #define MAX_STREAMS 20
  263. /* format I/O context */
  264. typedef struct AVFormatContext {
  265. const AVClass *av_class; /* set by av_alloc_format_context */
  266. /* can only be iformat or oformat, not both at the same time */
  267. struct AVInputFormat *iformat;
  268. struct AVOutputFormat *oformat;
  269. void *priv_data;
  270. ByteIOContext pb;
  271. int nb_streams;
  272. AVStream *streams[MAX_STREAMS];
  273. char filename[1024]; /* input or output filename */
  274. /* stream info */
  275. int64_t timestamp;
  276. char title[512];
  277. char author[512];
  278. char copyright[512];
  279. char comment[512];
  280. char album[512];
  281. int year; /* ID3 year, 0 if none */
  282. int track; /* track number, 0 if none */
  283. char genre[32]; /* ID3 genre */
  284. int ctx_flags; /* format specific flags, see AVFMTCTX_xx */
  285. /* private data for pts handling (do not modify directly) */
  286. /* This buffer is only needed when packets were already buffered but
  287. not decoded, for example to get the codec parameters in mpeg
  288. streams */
  289. struct AVPacketList *packet_buffer;
  290. /* decoding: position of the first frame of the component, in
  291. AV_TIME_BASE fractional seconds. NEVER set this value directly:
  292. it is deduced from the AVStream values. */
  293. int64_t start_time;
  294. /* decoding: duration of the stream, in AV_TIME_BASE fractional
  295. seconds. NEVER set this value directly: it is deduced from the
  296. AVStream values. */
  297. int64_t duration;
  298. /* decoding: total file size. 0 if unknown */
  299. int64_t file_size;
  300. /* decoding: total stream bitrate in bit/s, 0 if not
  301. available. Never set it directly if the file_size and the
  302. duration are known as ffmpeg can compute it automatically. */
  303. int bit_rate;
  304. /* av_read_frame() support */
  305. AVStream *cur_st;
  306. const uint8_t *cur_ptr;
  307. int cur_len;
  308. AVPacket cur_pkt;
  309. /* av_seek_frame() support */
  310. int64_t data_offset; /* offset of the first packet */
  311. int index_built;
  312. int mux_rate;
  313. int packet_size;
  314. int preload;
  315. int max_delay;
  316. #define AVFMT_NOOUTPUTLOOP -1
  317. #define AVFMT_INFINITEOUTPUTLOOP 0
  318. /* number of times to loop output in formats that support it */
  319. int loop_output;
  320. int flags;
  321. #define AVFMT_FLAG_GENPTS 0x0001 ///< generate pts if missing even if it requires parsing future frames
  322. #define AVFMT_FLAG_IGNIDX 0x0002 ///< ignore index
  323. int loop_input;
  324. /* decoding: size of data to probe; encoding unused */
  325. unsigned int probesize;
  326. } AVFormatContext;
  327. typedef struct AVPacketList {
  328. AVPacket pkt;
  329. struct AVPacketList *next;
  330. } AVPacketList;
  331. extern AVInputFormat *first_iformat;
  332. extern AVOutputFormat *first_oformat;
  333. enum CodecID av_guess_image2_codec(const char *filename);
  334. /* XXX: use automatic init with either ELF sections or C file parser */
  335. /* modules */
  336. #include "rtp.h"
  337. #include "rtsp.h"
  338. /* utils.c */
  339. void av_register_input_format(AVInputFormat *format);
  340. void av_register_output_format(AVOutputFormat *format);
  341. AVOutputFormat *guess_stream_format(const char *short_name,
  342. const char *filename, const char *mime_type);
  343. AVOutputFormat *guess_format(const char *short_name,
  344. const char *filename, const char *mime_type);
  345. enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
  346. const char *filename, const char *mime_type, enum CodecType type);
  347. void av_hex_dump(FILE *f, uint8_t *buf, int size);
  348. void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload);
  349. void av_register_all(void);
  350. /* media file input */
  351. AVInputFormat *av_find_input_format(const char *short_name);
  352. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened);
  353. int av_open_input_stream(AVFormatContext **ic_ptr,
  354. ByteIOContext *pb, const char *filename,
  355. AVInputFormat *fmt, AVFormatParameters *ap);
  356. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
  357. AVInputFormat *fmt,
  358. int buf_size,
  359. AVFormatParameters *ap);
  360. /* no av_open for output, so applications will need this: */
  361. AVFormatContext *av_alloc_format_context(void);
  362. #define AVERROR_UNKNOWN (-1) /* unknown error */
  363. #define AVERROR_IO (-2) /* i/o error */
  364. #define AVERROR_NUMEXPECTED (-3) /* number syntax expected in filename */
  365. #define AVERROR_INVALIDDATA (-4) /* invalid data found */
  366. #define AVERROR_NOMEM (-5) /* not enough memory */
  367. #define AVERROR_NOFMT (-6) /* unknown format */
  368. #define AVERROR_NOTSUPP (-7) /* operation not supported */
  369. int av_find_stream_info(AVFormatContext *ic);
  370. int av_read_packet(AVFormatContext *s, AVPacket *pkt);
  371. int av_read_frame(AVFormatContext *s, AVPacket *pkt);
  372. int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags);
  373. int av_read_play(AVFormatContext *s);
  374. int av_read_pause(AVFormatContext *s);
  375. void av_close_input_file(AVFormatContext *s);
  376. AVStream *av_new_stream(AVFormatContext *s, int id);
  377. void av_set_pts_info(AVStream *s, int pts_wrap_bits,
  378. int pts_num, int pts_den);
  379. #define AVSEEK_FLAG_BACKWARD 1 ///< seek backward
  380. #define AVSEEK_FLAG_BYTE 2 ///< seeking based on position in bytes
  381. #define AVSEEK_FLAG_ANY 4 ///< seek to any frame, even non keyframes
  382. int av_find_default_stream_index(AVFormatContext *s);
  383. int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags);
  384. int av_add_index_entry(AVStream *st,
  385. int64_t pos, int64_t timestamp, int size, int distance, int flags);
  386. int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags);
  387. void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp);
  388. 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 ));
  389. /* media file output */
  390. int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap);
  391. int av_write_header(AVFormatContext *s);
  392. int av_write_frame(AVFormatContext *s, AVPacket *pkt);
  393. int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
  394. int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush);
  395. int av_write_trailer(AVFormatContext *s);
  396. void dump_format(AVFormatContext *ic,
  397. int index,
  398. const char *url,
  399. int is_output);
  400. int parse_image_size(int *width_ptr, int *height_ptr, const char *str);
  401. int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg);
  402. int64_t parse_date(const char *datestr, int duration);
  403. int64_t av_gettime(void);
  404. /* ffm specific for ffserver */
  405. #define FFM_PACKET_SIZE 4096
  406. offset_t ffm_read_write_index(int fd);
  407. void ffm_write_write_index(int fd, offset_t pos);
  408. void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size);
  409. int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info);
  410. int av_get_frame_filename(char *buf, int buf_size,
  411. const char *path, int number);
  412. int av_filename_number_test(const char *filename);
  413. /* grab specific */
  414. int video_grab_init(void);
  415. int audio_init(void);
  416. /* DV1394 */
  417. int dv1394_init(void);
  418. int dc1394_init(void);
  419. #ifdef HAVE_AV_CONFIG_H
  420. #include "os_support.h"
  421. int strstart(const char *str, const char *val, const char **ptr);
  422. int stristart(const char *str, const char *val, const char **ptr);
  423. void pstrcpy(char *buf, int buf_size, const char *str);
  424. char *pstrcat(char *buf, int buf_size, const char *s);
  425. void __dynarray_add(unsigned long **tab_ptr, int *nb_ptr, unsigned long elem);
  426. #ifdef __GNUC__
  427. #define dynarray_add(tab, nb_ptr, elem)\
  428. do {\
  429. typeof(tab) _tab = (tab);\
  430. typeof(elem) _elem = (elem);\
  431. (void)sizeof(**_tab == _elem); /* check that types are compatible */\
  432. __dynarray_add((unsigned long **)_tab, nb_ptr, (unsigned long)_elem);\
  433. } while(0)
  434. #else
  435. #define dynarray_add(tab, nb_ptr, elem)\
  436. do {\
  437. __dynarray_add((unsigned long **)(tab), nb_ptr, (unsigned long)(elem));\
  438. } while(0)
  439. #endif
  440. time_t mktimegm(struct tm *tm);
  441. struct tm *brktimegm(time_t secs, struct tm *tm);
  442. const char *small_strptime(const char *p, const char *fmt,
  443. struct tm *dt);
  444. struct in_addr;
  445. int resolve_host(struct in_addr *sin_addr, const char *hostname);
  446. void url_split(char *proto, int proto_size,
  447. char *authorization, int authorization_size,
  448. char *hostname, int hostname_size,
  449. int *port_ptr,
  450. char *path, int path_size,
  451. const char *url);
  452. int match_ext(const char *filename, const char *extensions);
  453. #endif /* HAVE_AV_CONFIG_H */
  454. #ifdef __cplusplus
  455. }
  456. #endif
  457. #endif /* AVFORMAT_H */