sample size to 1 might happen for data, fix GMCMidiaASX.mov, fallback is better than...
[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
1308f273
MN
32/**
33 * URL Context.
34 * New fields can be added to the end with minor version bumps.
8bfb108b 35 * Removal, reordering and changes to existing fields require a major
1308f273 36 * version bump.
8bfb108b 37 * sizeof(URLContext) must not be used outside libav*.
1308f273 38 */
de6d9b64 39struct URLContext {
5acef35f
BA
40#if LIBAVFORMAT_VERSION_MAJOR >= 53
41 const AVClass *av_class; ///< information for av_log(). Set by url_open().
42#endif
de6d9b64 43 struct URLProtocol *prot;
115329f1 44 int flags;
c3316802
PI
45 int is_streamed; /**< true if streamed (no seek possible), default = false */
46 int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
de6d9b64 47 void *priv_data;
c3316802 48 char *filename; /**< specified filename */
de6d9b64
FB
49};
50
de6d9b64
FB
51typedef struct URLContext URLContext;
52
53typedef struct URLPollEntry {
54 URLContext *handle;
55 int events;
56 int revents;
57} URLPollEntry;
58
59#define URL_RDONLY 0
60#define URL_WRONLY 1
f031df23
FB
61#define URL_RDWR 2
62
019ac05a
FB
63typedef int URLInterruptCB(void);
64
de6d9b64
FB
65int url_open(URLContext **h, const char *filename, int flags);
66int url_read(URLContext *h, unsigned char *buf, int size);
67int url_write(URLContext *h, unsigned char *buf, int size);
68offset_t url_seek(URLContext *h, offset_t pos, int whence);
de6d9b64
FB
69int url_close(URLContext *h);
70int url_exist(const char *filename);
71offset_t url_filesize(URLContext *h);
c306748c 72
c3316802 73/**
c306748c 74 * Return the maximum packet size associated to packetized file
8bfb108b 75 * handle. If the file is not packetized (stream like HTTP or file on
c306748c
PI
76 * disk), then 0 is returned.
77 *
78 * @param h file handle
79 * @return maximum packet size in bytes
80 */
f031df23 81int url_get_max_packet_size(URLContext *h);
f746a046
FB
82void url_get_filename(URLContext *h, char *buf, int buf_size);
83
c3316802 84/**
8bfb108b 85 * The callback is called in blocking functions to test regulary if
c3316802
PI
86 * asynchronous interruption is needed. AVERROR(EINTR) is returned
87 * in this case by the interrupted function. 'NULL' means no interrupt
8bfb108b 88 * callback is given.
c3316802 89 */
019ac05a
FB
90void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
91
de6d9b64
FB
92/* not implemented */
93int url_poll(URLPollEntry *poll_table, int n, int timeout);
94
502bdf68
MN
95/**
96 * Pause and resume playing - only meaningful if using a network streaming
97 * protocol (e.g. MMS).
98 * @param pause 1 for pause, 0 for resume
99 */
100int av_url_read_pause(URLContext *h, int pause);
8bfb108b 101
536333a0
BA
102/**
103 * Seek to a given timestamp relative to some component stream.
8bfb108b 104 * Only meaningful if using a network streaming protocol (e.g. MMS.).
536333a0
BA
105 * @param stream_index The stream index that the timestamp is relative to.
106 * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
107 * units from the beginning of the presentation.
108 * If a stream_index >= 0 is used and the protocol does not support
109 * seeking based on component streams, the call will fail with ENOTSUP.
00ef4f58 110 * @param timestamp timestamp in AVStream.time_base units
536333a0
BA
111 * or if there is no stream specified then in AV_TIME_BASE units.
112 * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
113 * and AVSEEK_FLAG_ANY. The protocol may silently ignore
114 * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
115 * fail with ENOTSUP if used and not supported.
116 * @return >= 0 on success
117 * @see AVInputFormat::read_seek
118 */
b075e520 119offset_t av_url_read_seek(URLContext *h,
536333a0
BA
120 int stream_index, int64_t timestamp, int flags);
121
aa38b097 122/**
90b5b51e
DB
123 * Passing this as the "whence" parameter to a seek function causes it to
124 * return the filesize without seeking anywhere. Supporting this is optional.
125 * If it is not supported then the seek function will return <0.
aa38b097 126 */
8e287af0
MN
127#define AVSEEK_SIZE 0x10000
128
de6d9b64
FB
129typedef struct URLProtocol {
130 const char *name;
131 int (*url_open)(URLContext *h, const char *filename, int flags);
132 int (*url_read)(URLContext *h, unsigned char *buf, int size);
133 int (*url_write)(URLContext *h, unsigned char *buf, int size);
134 offset_t (*url_seek)(URLContext *h, offset_t pos, int whence);
135 int (*url_close)(URLContext *h);
de6d9b64 136 struct URLProtocol *next;
502bdf68 137 int (*url_read_pause)(URLContext *h, int pause);
b075e520 138 offset_t (*url_read_seek)(URLContext *h,
536333a0 139 int stream_index, int64_t timestamp, int flags);
de6d9b64
FB
140} URLProtocol;
141
142extern URLProtocol *first_protocol;
019ac05a 143extern URLInterruptCB *url_interrupt_cb;
de6d9b64 144
84be6e72
MN
145URLProtocol *av_protocol_next(URLProtocol *p);
146
de6d9b64
FB
147int register_protocol(URLProtocol *protocol);
148
1308f273
MN
149/**
150 * Bytestream IO Context.
151 * New fields can be added to the end with minor version bumps.
8bfb108b 152 * Removal, reordering and changes to existing fields require a major
1308f273 153 * version bump.
8bfb108b 154 * sizeof(ByteIOContext) must not be used outside libav*.
1308f273 155 */
de6d9b64
FB
156typedef struct {
157 unsigned char *buffer;
158 int buffer_size;
159 unsigned char *buf_ptr, *buf_end;
160 void *opaque;
0c1a9eda 161 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
576ae256 162 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
536ffa59 163 offset_t (*seek)(void *opaque, offset_t offset, int whence);
c3316802
PI
164 offset_t pos; /**< position in the file of the current buffer */
165 int must_flush; /**< true if the next seek should flush */
166 int eof_reached; /**< true if eof reached */
167 int write_flag; /**< true if open for writing */
de6d9b64 168 int is_streamed;
f031df23 169 int max_packet_size;
ee9f36a8
MN
170 unsigned long checksum;
171 unsigned char *checksum_ptr;
172 unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
576ae256 173 int error; ///< contains the error code or 0 if no error happened
502bdf68 174 int (*read_pause)(void *opaque, int pause);
b075e520 175 offset_t (*read_seek)(void *opaque,
e7e4810a 176 int stream_index, int64_t timestamp, int flags);
de6d9b64
FB
177} ByteIOContext;
178
179int init_put_byte(ByteIOContext *s,
180 unsigned char *buffer,
181 int buffer_size,
182 int write_flag,
183 void *opaque,
1e0f3468
RD
184 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
185 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
186 offset_t (*seek)(void *opaque, offset_t offset, int whence));
187ByteIOContext *av_alloc_put_byte(
188 unsigned char *buffer,
189 int buffer_size,
190 int write_flag,
191 void *opaque,
0c1a9eda 192 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
576ae256 193 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
536ffa59 194 offset_t (*seek)(void *opaque, offset_t offset, int whence));
de6d9b64
FB
195
196void put_byte(ByteIOContext *s, int b);
75bdb984 197void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
0c1a9eda
ZK
198void put_le64(ByteIOContext *s, uint64_t val);
199void put_be64(ByteIOContext *s, uint64_t val);
de6d9b64
FB
200void put_le32(ByteIOContext *s, unsigned int val);
201void put_be32(ByteIOContext *s, unsigned int val);
ea395e8c 202void put_le24(ByteIOContext *s, unsigned int val);
a254c574 203void put_be24(ByteIOContext *s, unsigned int val);
de6d9b64
FB
204void put_le16(ByteIOContext *s, unsigned int val);
205void put_be16(ByteIOContext *s, unsigned int val);
0570bf06 206void put_tag(ByteIOContext *s, const char *tag);
de6d9b64 207
3b4b29dc 208void put_strz(ByteIOContext *s, const char *buf);
75bdb984 209
67e21020
MN
210/**
211 * fseek() equivalent for ByteIOContext.
212 * @return new position or AVERROR.
213 */
de6d9b64 214offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
67e21020
MN
215
216/**
217 * Skip given number of bytes forward.
218 * @param offset number of bytes
219 */
de6d9b64 220void url_fskip(ByteIOContext *s, offset_t offset);
67e21020
MN
221
222/**
223 * ftell() equivalent for ByteIOContext.
224 * @return position or AVERROR.
225 */
de6d9b64 226offset_t url_ftell(ByteIOContext *s);
67e21020
MN
227
228/**
229 * Gets the filesize.
230 * @return filesize or AVERROR
231 */
a965c478 232offset_t url_fsize(ByteIOContext *s);
67e21020
MN
233
234/**
235 * feof() equivalent for ByteIOContext.
236 * @return non zero if and only if end of file
237 */
de6d9b64 238int url_feof(ByteIOContext *s);
67e21020 239
576ae256 240int url_ferror(ByteIOContext *s);
de6d9b64 241
502bdf68 242int av_url_read_fpause(ByteIOContext *h, int pause);
b075e520 243offset_t av_url_read_fseek(ByteIOContext *h,
e7e4810a
BA
244 int stream_index, int64_t timestamp, int flags);
245
f031df23 246#define URL_EOF (-1)
c3316802 247/** @note return URL_EOF (-1) if EOF */
f031df23 248int url_fgetc(ByteIOContext *s);
a8c5ab27 249
c3316802 250/** @warning currently size is limited */
572f992e 251#ifdef __GNUC__
08f29f82 252int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
572f992e
MN
253#else
254int url_fprintf(ByteIOContext *s, const char *fmt, ...);
255#endif
a8c5ab27 256
c3316802 257/** @note unlike fgets, the EOL character is not returned and a whole
a8c5ab27 258 line is parsed. return NULL if first char read was EOF */
f031df23
FB
259char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
260
de6d9b64
FB
261void put_flush_packet(ByteIOContext *s);
262
263int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
e15dec10 264int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
a8c5ab27 265
c3316802 266/** @note return 0 if EOF, so you cannot use it if EOF handling is
a8c5ab27 267 necessary */
de6d9b64 268int get_byte(ByteIOContext *s);
ea395e8c 269unsigned int get_le24(ByteIOContext *s);
de6d9b64 270unsigned int get_le32(ByteIOContext *s);
0c1a9eda 271uint64_t get_le64(ByteIOContext *s);
de6d9b64
FB
272unsigned int get_le16(ByteIOContext *s);
273
3b4b29dc 274char *get_strz(ByteIOContext *s, char *buf, int maxlen);
de6d9b64 275unsigned int get_be16(ByteIOContext *s);
a254c574 276unsigned int get_be24(ByteIOContext *s);
de6d9b64 277unsigned int get_be32(ByteIOContext *s);
0c1a9eda 278uint64_t get_be64(ByteIOContext *s);
de6d9b64 279
7798b42d 280uint64_t ff_get_v(ByteIOContext *bc);
897d3eef 281
7ac13f0c 282static inline int url_is_streamed(ByteIOContext *s)
de6d9b64
FB
283{
284 return s->is_streamed;
285}
de6d9b64 286
3576f9c6
BA
287/** @note when opened as read/write, the buffers are only used for
288 writing */
899681cd 289int url_fdopen(ByteIOContext **s, URLContext *h);
a8c5ab27 290
c3316802 291/** @warning must be called before any I/O */
de6d9b64 292int url_setbufsize(ByteIOContext *s, int buf_size);
770d9daf
BA
293/** Reset the buffer for reading or writing.
294 * @note Will drop any data currently in the buffer without transmitting it.
295 * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
296 * to set up the buffer for writing. */
297int url_resetbuf(ByteIOContext *s, int flags);
a8c5ab27 298
c3316802 299/** @note when opened as read/write, the buffers are only used for
3576f9c6 300 writing */
899681cd 301int url_fopen(ByteIOContext **s, const char *filename, int flags);
de6d9b64
FB
302int url_fclose(ByteIOContext *s);
303URLContext *url_fileno(ByteIOContext *s);
a8c5ab27 304
c3316802 305/**
a8c5ab27
PI
306 * Return the maximum packet size associated to packetized buffered file
307 * handle. If the file is not packetized (stream like http or file on
308 * disk), then 0 is returned.
309 *
abfe87d4 310 * @param s buffered file handle
a8c5ab27
PI
311 * @return maximum packet size in bytes
312 */
f031df23 313int url_fget_max_packet_size(ByteIOContext *s);
de6d9b64 314
899681cd 315int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags);
a8c5ab27 316
c3316802 317/** return the written or read size */
de6d9b64
FB
318int url_close_buf(ByteIOContext *s);
319
c3316802 320/**
a8c5ab27
PI
321 * Open a write only memory stream.
322 *
323 * @param s new IO context
324 * @return zero if no error.
325 */
899681cd 326int url_open_dyn_buf(ByteIOContext **s);
a8c5ab27 327
c3316802 328/**
a8c5ab27
PI
329 * Open a write only packetized memory stream with a maximum packet
330 * size of 'max_packet_size'. The stream is stored in a memory buffer
331 * with a big endian 4 byte header giving the packet size in bytes.
332 *
333 * @param s new IO context
334 * @param max_packet_size maximum packet size (must be > 0)
335 * @return zero if no error.
336 */
899681cd 337int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size);
a8c5ab27 338
c3316802 339/**
a8c5ab27
PI
340 * Return the written size and a pointer to the buffer. The buffer
341 * must be freed with av_free().
342 * @param s IO context
e8b7c70f 343 * @param pbuffer pointer to a byte buffer
a8c5ab27
PI
344 * @return the length of the byte buffer
345 */
0c1a9eda 346int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
f031df23 347
992e78f5 348unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len);
ee9f36a8
MN
349unsigned long get_checksum(ByteIOContext *s);
350void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);
ee9f36a8 351
de6d9b64 352/* udp.c */
f031df23
FB
353int udp_set_remote_url(URLContext *h, const char *uri);
354int udp_get_local_port(URLContext *h);
355int udp_get_file_handle(URLContext *h);
356
5b21bdab 357#endif /* FFMPEG_AVIO_H */