cosmetics, split or move too long lines
[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
de6d9b64
FB
210offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
211void url_fskip(ByteIOContext *s, offset_t offset);
212offset_t url_ftell(ByteIOContext *s);
a965c478 213offset_t url_fsize(ByteIOContext *s);
de6d9b64 214int url_feof(ByteIOContext *s);
576ae256 215int url_ferror(ByteIOContext *s);
de6d9b64 216
502bdf68 217int av_url_read_fpause(ByteIOContext *h, int pause);
b075e520 218offset_t av_url_read_fseek(ByteIOContext *h,
e7e4810a
BA
219 int stream_index, int64_t timestamp, int flags);
220
f031df23 221#define URL_EOF (-1)
c3316802 222/** @note return URL_EOF (-1) if EOF */
f031df23 223int url_fgetc(ByteIOContext *s);
a8c5ab27 224
c3316802 225/** @warning currently size is limited */
572f992e 226#ifdef __GNUC__
08f29f82 227int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
572f992e
MN
228#else
229int url_fprintf(ByteIOContext *s, const char *fmt, ...);
230#endif
a8c5ab27 231
c3316802 232/** @note unlike fgets, the EOL character is not returned and a whole
a8c5ab27 233 line is parsed. return NULL if first char read was EOF */
f031df23
FB
234char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
235
de6d9b64
FB
236void put_flush_packet(ByteIOContext *s);
237
238int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
e15dec10 239int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
a8c5ab27 240
c3316802 241/** @note return 0 if EOF, so you cannot use it if EOF handling is
a8c5ab27 242 necessary */
de6d9b64 243int get_byte(ByteIOContext *s);
ea395e8c 244unsigned int get_le24(ByteIOContext *s);
de6d9b64 245unsigned int get_le32(ByteIOContext *s);
0c1a9eda 246uint64_t get_le64(ByteIOContext *s);
de6d9b64
FB
247unsigned int get_le16(ByteIOContext *s);
248
3b4b29dc 249char *get_strz(ByteIOContext *s, char *buf, int maxlen);
de6d9b64 250unsigned int get_be16(ByteIOContext *s);
a254c574 251unsigned int get_be24(ByteIOContext *s);
de6d9b64 252unsigned int get_be32(ByteIOContext *s);
0c1a9eda 253uint64_t get_be64(ByteIOContext *s);
de6d9b64 254
7798b42d 255uint64_t ff_get_v(ByteIOContext *bc);
897d3eef 256
7ac13f0c 257static inline int url_is_streamed(ByteIOContext *s)
de6d9b64
FB
258{
259 return s->is_streamed;
260}
de6d9b64 261
3576f9c6
BA
262/** @note when opened as read/write, the buffers are only used for
263 writing */
899681cd 264int url_fdopen(ByteIOContext **s, URLContext *h);
a8c5ab27 265
c3316802 266/** @warning must be called before any I/O */
de6d9b64 267int url_setbufsize(ByteIOContext *s, int buf_size);
770d9daf
BA
268/** Reset the buffer for reading or writing.
269 * @note Will drop any data currently in the buffer without transmitting it.
270 * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
271 * to set up the buffer for writing. */
272int url_resetbuf(ByteIOContext *s, int flags);
a8c5ab27 273
c3316802 274/** @note when opened as read/write, the buffers are only used for
3576f9c6 275 writing */
899681cd 276int url_fopen(ByteIOContext **s, const char *filename, int flags);
de6d9b64
FB
277int url_fclose(ByteIOContext *s);
278URLContext *url_fileno(ByteIOContext *s);
a8c5ab27 279
c3316802 280/**
a8c5ab27
PI
281 * Return the maximum packet size associated to packetized buffered file
282 * handle. If the file is not packetized (stream like http or file on
283 * disk), then 0 is returned.
284 *
abfe87d4 285 * @param s buffered file handle
a8c5ab27
PI
286 * @return maximum packet size in bytes
287 */
f031df23 288int url_fget_max_packet_size(ByteIOContext *s);
de6d9b64 289
899681cd 290int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags);
a8c5ab27 291
c3316802 292/** return the written or read size */
de6d9b64
FB
293int url_close_buf(ByteIOContext *s);
294
c3316802 295/**
a8c5ab27
PI
296 * Open a write only memory stream.
297 *
298 * @param s new IO context
299 * @return zero if no error.
300 */
899681cd 301int url_open_dyn_buf(ByteIOContext **s);
a8c5ab27 302
c3316802 303/**
a8c5ab27
PI
304 * Open a write only packetized memory stream with a maximum packet
305 * size of 'max_packet_size'. The stream is stored in a memory buffer
306 * with a big endian 4 byte header giving the packet size in bytes.
307 *
308 * @param s new IO context
309 * @param max_packet_size maximum packet size (must be > 0)
310 * @return zero if no error.
311 */
899681cd 312int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size);
a8c5ab27 313
c3316802 314/**
a8c5ab27
PI
315 * Return the written size and a pointer to the buffer. The buffer
316 * must be freed with av_free().
317 * @param s IO context
e8b7c70f 318 * @param pbuffer pointer to a byte buffer
a8c5ab27
PI
319 * @return the length of the byte buffer
320 */
0c1a9eda 321int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
f031df23 322
992e78f5 323unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len);
ee9f36a8
MN
324unsigned long get_checksum(ByteIOContext *s);
325void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);
ee9f36a8 326
de6d9b64 327/* udp.c */
f031df23
FB
328int udp_set_remote_url(URLContext *h, const char *uri);
329int udp_get_local_port(URLContext *h);
330int udp_get_file_handle(URLContext *h);
331
5b21bdab 332#endif /* FFMPEG_AVIO_H */