Reindent
[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
502bdf68
MN
85/**
86 * Pause and resume playing - only meaningful if using a network streaming
87 * protocol (e.g. MMS).
88 * @param pause 1 for pause, 0 for resume
89 */
90int av_url_read_pause(URLContext *h, int pause);
536333a0
BA
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 */
b075e520 108offset_t av_url_read_seek(URLContext *h,
536333a0
BA
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;
502bdf68 126 int (*url_read_pause)(URLContext *h, int pause);
b075e520 127 offset_t (*url_read_seek)(URLContext *h,
536333a0 128 int stream_index, int64_t timestamp, int flags);
de6d9b64
FB
129} URLProtocol;
130
131extern URLProtocol *first_protocol;
019ac05a 132extern URLInterruptCB *url_interrupt_cb;
de6d9b64 133
84be6e72
MN
134URLProtocol *av_protocol_next(URLProtocol *p);
135
de6d9b64
FB
136int register_protocol(URLProtocol *protocol);
137
138typedef struct {
139 unsigned char *buffer;
140 int buffer_size;
141 unsigned char *buf_ptr, *buf_end;
142 void *opaque;
0c1a9eda 143 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
576ae256 144 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
536ffa59 145 offset_t (*seek)(void *opaque, offset_t offset, int whence);
c3316802
PI
146 offset_t pos; /**< position in the file of the current buffer */
147 int must_flush; /**< true if the next seek should flush */
148 int eof_reached; /**< true if eof reached */
149 int write_flag; /**< true if open for writing */
de6d9b64 150 int is_streamed;
f031df23 151 int max_packet_size;
ee9f36a8
MN
152 unsigned long checksum;
153 unsigned char *checksum_ptr;
154 unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
576ae256 155 int error; ///< contains the error code or 0 if no error happened
502bdf68 156 int (*read_pause)(void *opaque, int pause);
b075e520 157 offset_t (*read_seek)(void *opaque,
e7e4810a 158 int stream_index, int64_t timestamp, int flags);
de6d9b64
FB
159} ByteIOContext;
160
161int init_put_byte(ByteIOContext *s,
162 unsigned char *buffer,
163 int buffer_size,
164 int write_flag,
165 void *opaque,
0c1a9eda 166 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
576ae256 167 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
536ffa59 168 offset_t (*seek)(void *opaque, offset_t offset, int whence));
de6d9b64
FB
169
170void put_byte(ByteIOContext *s, int b);
75bdb984 171void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
0c1a9eda
ZK
172void put_le64(ByteIOContext *s, uint64_t val);
173void put_be64(ByteIOContext *s, uint64_t val);
de6d9b64
FB
174void put_le32(ByteIOContext *s, unsigned int val);
175void put_be32(ByteIOContext *s, unsigned int val);
ea395e8c 176void put_le24(ByteIOContext *s, unsigned int val);
a254c574 177void put_be24(ByteIOContext *s, unsigned int val);
de6d9b64
FB
178void put_le16(ByteIOContext *s, unsigned int val);
179void put_be16(ByteIOContext *s, unsigned int val);
0570bf06 180void put_tag(ByteIOContext *s, const char *tag);
de6d9b64 181
3b4b29dc 182void put_strz(ByteIOContext *s, const char *buf);
75bdb984 183
de6d9b64
FB
184offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
185void url_fskip(ByteIOContext *s, offset_t offset);
186offset_t url_ftell(ByteIOContext *s);
a965c478 187offset_t url_fsize(ByteIOContext *s);
de6d9b64 188int url_feof(ByteIOContext *s);
576ae256 189int url_ferror(ByteIOContext *s);
de6d9b64 190
502bdf68 191int av_url_read_fpause(ByteIOContext *h, int pause);
b075e520 192offset_t av_url_read_fseek(ByteIOContext *h,
e7e4810a
BA
193 int stream_index, int64_t timestamp, int flags);
194
f031df23 195#define URL_EOF (-1)
c3316802 196/** @note return URL_EOF (-1) if EOF */
f031df23 197int url_fgetc(ByteIOContext *s);
a8c5ab27 198
c3316802 199/** @warning currently size is limited */
572f992e 200#ifdef __GNUC__
08f29f82 201int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
572f992e
MN
202#else
203int url_fprintf(ByteIOContext *s, const char *fmt, ...);
204#endif
a8c5ab27 205
c3316802 206/** @note unlike fgets, the EOL character is not returned and a whole
a8c5ab27 207 line is parsed. return NULL if first char read was EOF */
f031df23
FB
208char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
209
de6d9b64
FB
210void put_flush_packet(ByteIOContext *s);
211
212int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
e15dec10 213int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
a8c5ab27 214
c3316802 215/** @note return 0 if EOF, so you cannot use it if EOF handling is
a8c5ab27 216 necessary */
de6d9b64 217int get_byte(ByteIOContext *s);
ea395e8c 218unsigned int get_le24(ByteIOContext *s);
de6d9b64 219unsigned int get_le32(ByteIOContext *s);
0c1a9eda 220uint64_t get_le64(ByteIOContext *s);
de6d9b64
FB
221unsigned int get_le16(ByteIOContext *s);
222
3b4b29dc 223char *get_strz(ByteIOContext *s, char *buf, int maxlen);
de6d9b64 224unsigned int get_be16(ByteIOContext *s);
a254c574 225unsigned int get_be24(ByteIOContext *s);
de6d9b64 226unsigned int get_be32(ByteIOContext *s);
0c1a9eda 227uint64_t get_be64(ByteIOContext *s);
de6d9b64 228
7798b42d 229uint64_t ff_get_v(ByteIOContext *bc);
897d3eef 230
7ac13f0c 231static inline int url_is_streamed(ByteIOContext *s)
de6d9b64
FB
232{
233 return s->is_streamed;
234}
de6d9b64 235
3576f9c6
BA
236/** @note when opened as read/write, the buffers are only used for
237 writing */
899681cd 238int url_fdopen(ByteIOContext **s, URLContext *h);
a8c5ab27 239
c3316802 240/** @warning must be called before any I/O */
de6d9b64 241int url_setbufsize(ByteIOContext *s, int buf_size);
770d9daf
BA
242/** Reset the buffer for reading or writing.
243 * @note Will drop any data currently in the buffer without transmitting it.
244 * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
245 * to set up the buffer for writing. */
246int url_resetbuf(ByteIOContext *s, int flags);
a8c5ab27 247
c3316802 248/** @note when opened as read/write, the buffers are only used for
3576f9c6 249 writing */
899681cd 250int url_fopen(ByteIOContext **s, const char *filename, int flags);
de6d9b64
FB
251int url_fclose(ByteIOContext *s);
252URLContext *url_fileno(ByteIOContext *s);
a8c5ab27 253
c3316802 254/**
a8c5ab27
PI
255 * Return the maximum packet size associated to packetized buffered file
256 * handle. If the file is not packetized (stream like http or file on
257 * disk), then 0 is returned.
258 *
abfe87d4 259 * @param s buffered file handle
a8c5ab27
PI
260 * @return maximum packet size in bytes
261 */
f031df23 262int url_fget_max_packet_size(ByteIOContext *s);
de6d9b64 263
899681cd 264int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags);
a8c5ab27 265
c3316802 266/** return the written or read size */
de6d9b64
FB
267int url_close_buf(ByteIOContext *s);
268
c3316802 269/**
a8c5ab27
PI
270 * Open a write only memory stream.
271 *
272 * @param s new IO context
273 * @return zero if no error.
274 */
899681cd 275int url_open_dyn_buf(ByteIOContext **s);
a8c5ab27 276
c3316802 277/**
a8c5ab27
PI
278 * Open a write only packetized memory stream with a maximum packet
279 * size of 'max_packet_size'. The stream is stored in a memory buffer
280 * with a big endian 4 byte header giving the packet size in bytes.
281 *
282 * @param s new IO context
283 * @param max_packet_size maximum packet size (must be > 0)
284 * @return zero if no error.
285 */
899681cd 286int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size);
a8c5ab27 287
c3316802 288/**
a8c5ab27
PI
289 * Return the written size and a pointer to the buffer. The buffer
290 * must be freed with av_free().
291 * @param s IO context
e8b7c70f 292 * @param pbuffer pointer to a byte buffer
a8c5ab27
PI
293 * @return the length of the byte buffer
294 */
0c1a9eda 295int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
f031df23 296
992e78f5 297unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len);
ee9f36a8
MN
298unsigned long get_checksum(ByteIOContext *s);
299void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);
ee9f36a8 300
de6d9b64 301/* udp.c */
f031df23
FB
302int udp_set_remote_url(URLContext *h, const char *uri);
303int udp_get_local_port(URLContext *h);
304int udp_get_file_handle(URLContext *h);
305
5b21bdab 306#endif /* FFMPEG_AVIO_H */