use generic 'glbl' atom if extradata is present but no specific method is known
[libav.git] / libavformat / avio.h
CommitLineData
04d7f601
DB
1/*
2 * unbuffered io for ffmpeg system
3 * copyright (c) 2001 Fabrice Bellard
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
04d7f601
DB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
04d7f601 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
04d7f601
DB
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
04d7f601
DB
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
5b21bdab
DB
21#ifndef FFMPEG_AVIO_H
22#define FFMPEG_AVIO_H
f031df23 23
99545457
MR
24#include <stdint.h>
25
de6d9b64
FB
26/* output byte stream handling */
27
0c1a9eda 28typedef int64_t offset_t;
de6d9b64
FB
29
30/* unbuffered I/O */
31
32struct URLContext {
33 struct URLProtocol *prot;
115329f1 34 int flags;
c3316802
PI
35 int is_streamed; /**< true if streamed (no seek possible), default = false */
36 int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
de6d9b64 37 void *priv_data;
c3316802 38 char *filename; /**< specified filename */
de6d9b64
FB
39};
40
de6d9b64
FB
41typedef struct URLContext URLContext;
42
43typedef struct URLPollEntry {
44 URLContext *handle;
45 int events;
46 int revents;
47} URLPollEntry;
48
49#define URL_RDONLY 0
50#define URL_WRONLY 1
f031df23
FB
51#define URL_RDWR 2
52
019ac05a
FB
53typedef int URLInterruptCB(void);
54
de6d9b64
FB
55int url_open(URLContext **h, const char *filename, int flags);
56int url_read(URLContext *h, unsigned char *buf, int size);
57int url_write(URLContext *h, unsigned char *buf, int size);
58offset_t url_seek(URLContext *h, offset_t pos, int whence);
de6d9b64
FB
59int url_close(URLContext *h);
60int url_exist(const char *filename);
61offset_t url_filesize(URLContext *h);
c306748c 62
c3316802 63/**
c306748c
PI
64 * Return the maximum packet size associated to packetized file
65 * handle. If the file is not packetized (stream like http or file on
66 * disk), then 0 is returned.
67 *
68 * @param h file handle
69 * @return maximum packet size in bytes
70 */
f031df23 71int url_get_max_packet_size(URLContext *h);
f746a046
FB
72void url_get_filename(URLContext *h, char *buf, int buf_size);
73
c3316802
PI
74/**
75 * the callback is called in blocking functions to test regulary if
76 * asynchronous interruption is needed. AVERROR(EINTR) is returned
77 * in this case by the interrupted function. 'NULL' means no interrupt
78 * callback is given. i
79 */
019ac05a
FB
80void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
81
de6d9b64
FB
82/* not implemented */
83int url_poll(URLPollEntry *poll_table, int n, int timeout);
84
536333a0
BA
85/** Start playing or resume paused playout. Only meaningful if using a network
86 * streaming protocol (e.g. MMS). */
87int av_url_read_play(URLContext *h);
88/** Pause playing - only meaningful if using a network streaming protocol
89 * (e.g. MMS). */
90int av_url_read_pause(URLContext *h);
91/**
92 * Seek to a given timestamp relative to some component stream.
93 * Only meaningful if using a network streaming protocol (e.g. MMS.)
94 * @param stream_index The stream index that the timestamp is relative to.
95 * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
96 * units from the beginning of the presentation.
97 * If a stream_index >= 0 is used and the protocol does not support
98 * seeking based on component streams, the call will fail with ENOTSUP.
99 * @param time_stamp timestamp timestamp in AVStream.time_base units
100 * or if there is no stream specified then in AV_TIME_BASE units.
101 * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
102 * and AVSEEK_FLAG_ANY. The protocol may silently ignore
103 * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
104 * fail with ENOTSUP if used and not supported.
105 * @return >= 0 on success
106 * @see AVInputFormat::read_seek
107 */
108int av_url_read_seek(URLContext *h,
109 int stream_index, int64_t timestamp, int flags);
110
aa38b097 111/**
90b5b51e
DB
112 * Passing this as the "whence" parameter to a seek function causes it to
113 * return the filesize without seeking anywhere. Supporting this is optional.
114 * If it is not supported then the seek function will return <0.
aa38b097 115 */
8e287af0
MN
116#define AVSEEK_SIZE 0x10000
117
de6d9b64
FB
118typedef struct URLProtocol {
119 const char *name;
120 int (*url_open)(URLContext *h, const char *filename, int flags);
121 int (*url_read)(URLContext *h, unsigned char *buf, int size);
122 int (*url_write)(URLContext *h, unsigned char *buf, int size);
123 offset_t (*url_seek)(URLContext *h, offset_t pos, int whence);
124 int (*url_close)(URLContext *h);
de6d9b64 125 struct URLProtocol *next;
536333a0
BA
126 int (*url_read_play)(URLContext *h);
127 int (*url_read_pause)(URLContext *h);
128 int (*url_read_seek)(URLContext *h,
129 int stream_index, int64_t timestamp, int flags);
de6d9b64
FB
130} URLProtocol;
131
132extern URLProtocol *first_protocol;
019ac05a 133extern URLInterruptCB *url_interrupt_cb;
de6d9b64 134
84be6e72
MN
135URLProtocol *av_protocol_next(URLProtocol *p);
136
de6d9b64
FB
137int register_protocol(URLProtocol *protocol);
138
139typedef struct {
140 unsigned char *buffer;
141 int buffer_size;
142 unsigned char *buf_ptr, *buf_end;
143 void *opaque;
0c1a9eda 144 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
576ae256 145 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
536ffa59 146 offset_t (*seek)(void *opaque, offset_t offset, int whence);
c3316802
PI
147 offset_t pos; /**< position in the file of the current buffer */
148 int must_flush; /**< true if the next seek should flush */
149 int eof_reached; /**< true if eof reached */
150 int write_flag; /**< true if open for writing */
de6d9b64 151 int is_streamed;
f031df23 152 int max_packet_size;
ee9f36a8
MN
153 unsigned long checksum;
154 unsigned char *checksum_ptr;
155 unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
576ae256 156 int error; ///< contains the error code or 0 if no error happened
e7e4810a
BA
157 int (*read_play)(void *opaque);
158 int (*read_pause)(void *opaque);
159 int (*read_seek)(void *opaque,
160 int stream_index, int64_t timestamp, int flags);
de6d9b64
FB
161} ByteIOContext;
162
163int init_put_byte(ByteIOContext *s,
164 unsigned char *buffer,
165 int buffer_size,
166 int write_flag,
167 void *opaque,
0c1a9eda 168 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
576ae256 169 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
536ffa59 170 offset_t (*seek)(void *opaque, offset_t offset, int whence));
de6d9b64
FB
171
172void put_byte(ByteIOContext *s, int b);
75bdb984 173void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
0c1a9eda
ZK
174void put_le64(ByteIOContext *s, uint64_t val);
175void put_be64(ByteIOContext *s, uint64_t val);
de6d9b64
FB
176void put_le32(ByteIOContext *s, unsigned int val);
177void put_be32(ByteIOContext *s, unsigned int val);
ea395e8c 178void put_le24(ByteIOContext *s, unsigned int val);
a254c574 179void put_be24(ByteIOContext *s, unsigned int val);
de6d9b64
FB
180void put_le16(ByteIOContext *s, unsigned int val);
181void put_be16(ByteIOContext *s, unsigned int val);
0570bf06 182void put_tag(ByteIOContext *s, const char *tag);
de6d9b64 183
3b4b29dc 184void put_strz(ByteIOContext *s, const char *buf);
75bdb984 185
de6d9b64
FB
186offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
187void url_fskip(ByteIOContext *s, offset_t offset);
188offset_t url_ftell(ByteIOContext *s);
a965c478 189offset_t url_fsize(ByteIOContext *s);
de6d9b64 190int url_feof(ByteIOContext *s);
576ae256 191int url_ferror(ByteIOContext *s);
de6d9b64 192
e7e4810a
BA
193int av_url_read_fplay(ByteIOContext *h);
194int av_url_read_fpause(ByteIOContext *h);
195int av_url_read_fseek(ByteIOContext *h,
196 int stream_index, int64_t timestamp, int flags);
197
f031df23 198#define URL_EOF (-1)
c3316802 199/** @note return URL_EOF (-1) if EOF */
f031df23 200int url_fgetc(ByteIOContext *s);
a8c5ab27 201
c3316802 202/** @warning currently size is limited */
572f992e 203#ifdef __GNUC__
08f29f82 204int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
572f992e
MN
205#else
206int url_fprintf(ByteIOContext *s, const char *fmt, ...);
207#endif
a8c5ab27 208
c3316802 209/** @note unlike fgets, the EOL character is not returned and a whole
a8c5ab27 210 line is parsed. return NULL if first char read was EOF */
f031df23
FB
211char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
212
de6d9b64
FB
213void put_flush_packet(ByteIOContext *s);
214
215int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
e15dec10 216int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
a8c5ab27 217
c3316802 218/** @note return 0 if EOF, so you cannot use it if EOF handling is
a8c5ab27 219 necessary */
de6d9b64 220int get_byte(ByteIOContext *s);
ea395e8c 221unsigned int get_le24(ByteIOContext *s);
de6d9b64 222unsigned int get_le32(ByteIOContext *s);
0c1a9eda 223uint64_t get_le64(ByteIOContext *s);
de6d9b64
FB
224unsigned int get_le16(ByteIOContext *s);
225
3b4b29dc 226char *get_strz(ByteIOContext *s, char *buf, int maxlen);
de6d9b64 227unsigned int get_be16(ByteIOContext *s);
a254c574 228unsigned int get_be24(ByteIOContext *s);
de6d9b64 229unsigned int get_be32(ByteIOContext *s);
0c1a9eda 230uint64_t get_be64(ByteIOContext *s);
de6d9b64 231
7798b42d 232uint64_t ff_get_v(ByteIOContext *bc);
897d3eef 233
7ac13f0c 234static inline int url_is_streamed(ByteIOContext *s)
de6d9b64
FB
235{
236 return s->is_streamed;
237}
de6d9b64 238
3576f9c6
BA
239/** @note when opened as read/write, the buffers are only used for
240 writing */
899681cd 241int url_fdopen(ByteIOContext **s, URLContext *h);
a8c5ab27 242
c3316802 243/** @warning must be called before any I/O */
de6d9b64 244int url_setbufsize(ByteIOContext *s, int buf_size);
770d9daf
BA
245/** Reset the buffer for reading or writing.
246 * @note Will drop any data currently in the buffer without transmitting it.
247 * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
248 * to set up the buffer for writing. */
249int url_resetbuf(ByteIOContext *s, int flags);
a8c5ab27 250
c3316802 251/** @note when opened as read/write, the buffers are only used for
3576f9c6 252 writing */
899681cd 253int url_fopen(ByteIOContext **s, const char *filename, int flags);
de6d9b64
FB
254int url_fclose(ByteIOContext *s);
255URLContext *url_fileno(ByteIOContext *s);
a8c5ab27 256
c3316802 257/**
a8c5ab27
PI
258 * Return the maximum packet size associated to packetized buffered file
259 * handle. If the file is not packetized (stream like http or file on
260 * disk), then 0 is returned.
261 *
abfe87d4 262 * @param s buffered file handle
a8c5ab27
PI
263 * @return maximum packet size in bytes
264 */
f031df23 265int url_fget_max_packet_size(ByteIOContext *s);
de6d9b64 266
899681cd 267int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags);
a8c5ab27 268
c3316802 269/** return the written or read size */
de6d9b64
FB
270int url_close_buf(ByteIOContext *s);
271
c3316802 272/**
a8c5ab27
PI
273 * Open a write only memory stream.
274 *
275 * @param s new IO context
276 * @return zero if no error.
277 */
899681cd 278int url_open_dyn_buf(ByteIOContext **s);
a8c5ab27 279
c3316802 280/**
a8c5ab27
PI
281 * Open a write only packetized memory stream with a maximum packet
282 * size of 'max_packet_size'. The stream is stored in a memory buffer
283 * with a big endian 4 byte header giving the packet size in bytes.
284 *
285 * @param s new IO context
286 * @param max_packet_size maximum packet size (must be > 0)
287 * @return zero if no error.
288 */
899681cd 289int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size);
a8c5ab27 290
c3316802 291/**
a8c5ab27
PI
292 * Return the written size and a pointer to the buffer. The buffer
293 * must be freed with av_free().
294 * @param s IO context
e8b7c70f 295 * @param pbuffer pointer to a byte buffer
a8c5ab27
PI
296 * @return the length of the byte buffer
297 */
0c1a9eda 298int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
f031df23 299
992e78f5 300unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len);
ee9f36a8
MN
301unsigned long get_checksum(ByteIOContext *s);
302void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);
ee9f36a8 303
de6d9b64 304/* udp.c */
f031df23
FB
305int udp_set_remote_url(URLContext *h, const char *uri);
306int udp_get_local_port(URLContext *h);
307int udp_get_file_handle(URLContext *h);
308
5b21bdab 309#endif /* FFMPEG_AVIO_H */