update forgotten regression test chcksums for
[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 */
f031df23
FB
21#ifndef AVIO_H
22#define AVIO_H
23
de6d9b64
FB
24/* output byte stream handling */
25
0c1a9eda 26typedef int64_t offset_t;
de6d9b64
FB
27
28/* unbuffered I/O */
29
30struct URLContext {
31 struct URLProtocol *prot;
115329f1 32 int flags;
de6d9b64 33 int is_streamed; /* true if streamed (no seek possible), default = false */
f031df23 34 int max_packet_size; /* if non zero, the stream is packetized with this max packet size */
de6d9b64 35 void *priv_data;
62181004
RB
36#if LIBAVFORMAT_VERSION_INT >= (52<<16)
37 char *filename; /* specified filename */
38#else
f746a046 39 char filename[1]; /* specified filename */
62181004 40#endif
de6d9b64
FB
41};
42
de6d9b64
FB
43typedef struct URLContext URLContext;
44
45typedef struct URLPollEntry {
46 URLContext *handle;
47 int events;
48 int revents;
49} URLPollEntry;
50
51#define URL_RDONLY 0
52#define URL_WRONLY 1
f031df23
FB
53#define URL_RDWR 2
54
019ac05a
FB
55typedef int URLInterruptCB(void);
56
de6d9b64
FB
57int url_open(URLContext **h, const char *filename, int flags);
58int url_read(URLContext *h, unsigned char *buf, int size);
59int url_write(URLContext *h, unsigned char *buf, int size);
60offset_t url_seek(URLContext *h, offset_t pos, int whence);
de6d9b64
FB
61int url_close(URLContext *h);
62int url_exist(const char *filename);
63offset_t url_filesize(URLContext *h);
c306748c
PI
64
65/*
66 * Return the maximum packet size associated to packetized file
67 * handle. If the file is not packetized (stream like http or file on
68 * disk), then 0 is returned.
69 *
70 * @param h file handle
71 * @return maximum packet size in bytes
72 */
f031df23 73int url_get_max_packet_size(URLContext *h);
f746a046
FB
74void url_get_filename(URLContext *h, char *buf, int buf_size);
75
019ac05a 76/* the callback is called in blocking functions to test regulary if
8fa36ae0
FR
77 asynchronous interruption is needed. AVERROR(EINTR) is returned
78 in this case by the interrupted function. 'NULL' means no interrupt
019ac05a
FB
79 callback is given. */
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
aa38b097
MN
85/**
86 * passing this as the "whence" parameter to a seek function causes it to
87 * return the filesize without seeking anywhere, supporting this is optional
88 * if its not supprted then the seek function will return <0
89 */
8e287af0
MN
90#define AVSEEK_SIZE 0x10000
91
de6d9b64
FB
92typedef struct URLProtocol {
93 const char *name;
94 int (*url_open)(URLContext *h, const char *filename, int flags);
95 int (*url_read)(URLContext *h, unsigned char *buf, int size);
96 int (*url_write)(URLContext *h, unsigned char *buf, int size);
97 offset_t (*url_seek)(URLContext *h, offset_t pos, int whence);
98 int (*url_close)(URLContext *h);
de6d9b64
FB
99 struct URLProtocol *next;
100} URLProtocol;
101
102extern URLProtocol *first_protocol;
019ac05a 103extern URLInterruptCB *url_interrupt_cb;
de6d9b64
FB
104
105int register_protocol(URLProtocol *protocol);
106
107typedef struct {
108 unsigned char *buffer;
109 int buffer_size;
110 unsigned char *buf_ptr, *buf_end;
111 void *opaque;
0c1a9eda 112 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
576ae256 113 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
536ffa59 114 offset_t (*seek)(void *opaque, offset_t offset, int whence);
de6d9b64
FB
115 offset_t pos; /* position in the file of the current buffer */
116 int must_flush; /* true if the next seek should flush */
117 int eof_reached; /* true if eof reached */
118 int write_flag; /* true if open for writing */
119 int is_streamed;
f031df23 120 int max_packet_size;
ee9f36a8
MN
121 unsigned long checksum;
122 unsigned char *checksum_ptr;
123 unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
576ae256 124 int error; ///< contains the error code or 0 if no error happened
de6d9b64
FB
125} ByteIOContext;
126
127int init_put_byte(ByteIOContext *s,
128 unsigned char *buffer,
129 int buffer_size,
130 int write_flag,
131 void *opaque,
0c1a9eda 132 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
576ae256 133 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
536ffa59 134 offset_t (*seek)(void *opaque, offset_t offset, int whence));
de6d9b64
FB
135
136void put_byte(ByteIOContext *s, int b);
75bdb984 137void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
0c1a9eda
ZK
138void put_le64(ByteIOContext *s, uint64_t val);
139void put_be64(ByteIOContext *s, uint64_t val);
de6d9b64
FB
140void put_le32(ByteIOContext *s, unsigned int val);
141void put_be32(ByteIOContext *s, unsigned int val);
ea395e8c 142void put_le24(ByteIOContext *s, unsigned int val);
a254c574 143void put_be24(ByteIOContext *s, unsigned int val);
de6d9b64
FB
144void put_le16(ByteIOContext *s, unsigned int val);
145void put_be16(ByteIOContext *s, unsigned int val);
0570bf06 146void put_tag(ByteIOContext *s, const char *tag);
de6d9b64 147
3b4b29dc 148void put_strz(ByteIOContext *s, const char *buf);
75bdb984 149
de6d9b64
FB
150offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
151void url_fskip(ByteIOContext *s, offset_t offset);
152offset_t url_ftell(ByteIOContext *s);
a965c478 153offset_t url_fsize(ByteIOContext *s);
de6d9b64 154int url_feof(ByteIOContext *s);
576ae256 155int url_ferror(ByteIOContext *s);
de6d9b64 156
f031df23 157#define URL_EOF (-1)
a8c5ab27 158/* NOTE: return URL_EOF (-1) if EOF */
f031df23 159int url_fgetc(ByteIOContext *s);
a8c5ab27
PI
160
161/* XXX: currently size is limited */
572f992e 162#ifdef __GNUC__
08f29f82 163int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
572f992e
MN
164#else
165int url_fprintf(ByteIOContext *s, const char *fmt, ...);
166#endif
a8c5ab27
PI
167
168/* note: unlike fgets, the EOL character is not returned and a whole
169 line is parsed. return NULL if first char read was EOF */
f031df23
FB
170char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
171
de6d9b64
FB
172void put_flush_packet(ByteIOContext *s);
173
174int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
e15dec10 175int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
a8c5ab27
PI
176
177/* NOTE: return 0 if EOF, so you cannot use it if EOF handling is
178 necessary */
de6d9b64 179int get_byte(ByteIOContext *s);
ea395e8c 180unsigned int get_le24(ByteIOContext *s);
de6d9b64 181unsigned int get_le32(ByteIOContext *s);
0c1a9eda 182uint64_t get_le64(ByteIOContext *s);
de6d9b64
FB
183unsigned int get_le16(ByteIOContext *s);
184
3b4b29dc 185char *get_strz(ByteIOContext *s, char *buf, int maxlen);
de6d9b64 186unsigned int get_be16(ByteIOContext *s);
a254c574 187unsigned int get_be24(ByteIOContext *s);
de6d9b64 188unsigned int get_be32(ByteIOContext *s);
0c1a9eda 189uint64_t get_be64(ByteIOContext *s);
de6d9b64 190
7ac13f0c 191static inline int url_is_streamed(ByteIOContext *s)
de6d9b64
FB
192{
193 return s->is_streamed;
194}
de6d9b64
FB
195
196int url_fdopen(ByteIOContext *s, URLContext *h);
a8c5ab27
PI
197
198/* XXX: must be called before any I/O */
de6d9b64 199int url_setbufsize(ByteIOContext *s, int buf_size);
a8c5ab27
PI
200
201/* NOTE: when opened as read/write, the buffers are only used for
202 reading */
de6d9b64
FB
203int url_fopen(ByteIOContext *s, const char *filename, int flags);
204int url_fclose(ByteIOContext *s);
205URLContext *url_fileno(ByteIOContext *s);
a8c5ab27
PI
206
207/*
208 * Return the maximum packet size associated to packetized buffered file
209 * handle. If the file is not packetized (stream like http or file on
210 * disk), then 0 is returned.
211 *
212 * @param h buffered file handle
213 * @return maximum packet size in bytes
214 */
f031df23 215int url_fget_max_packet_size(ByteIOContext *s);
de6d9b64 216
0c1a9eda 217int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags);
a8c5ab27
PI
218
219/* return the written or read size */
de6d9b64
FB
220int url_close_buf(ByteIOContext *s);
221
a8c5ab27
PI
222/*
223 * Open a write only memory stream.
224 *
225 * @param s new IO context
226 * @return zero if no error.
227 */
f031df23 228int url_open_dyn_buf(ByteIOContext *s);
a8c5ab27
PI
229
230/*
231 * Open a write only packetized memory stream with a maximum packet
232 * size of 'max_packet_size'. The stream is stored in a memory buffer
233 * with a big endian 4 byte header giving the packet size in bytes.
234 *
235 * @param s new IO context
236 * @param max_packet_size maximum packet size (must be > 0)
237 * @return zero if no error.
238 */
f031df23 239int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size);
a8c5ab27
PI
240
241/*
242 * Return the written size and a pointer to the buffer. The buffer
243 * must be freed with av_free().
244 * @param s IO context
245 * @param pointer to a byte buffer
246 * @return the length of the byte buffer
247 */
0c1a9eda 248int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
f031df23 249
ee9f36a8
MN
250unsigned long get_checksum(ByteIOContext *s);
251void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);
ee9f36a8 252
de6d9b64
FB
253/* file.c */
254extern URLProtocol file_protocol;
255extern URLProtocol pipe_protocol;
256
257/* udp.c */
258extern URLProtocol udp_protocol;
f031df23
FB
259int udp_set_remote_url(URLContext *h, const char *uri);
260int udp_get_local_port(URLContext *h);
261int udp_get_file_handle(URLContext *h);
262
263/* tcp.c */
264extern URLProtocol tcp_protocol;
de6d9b64
FB
265
266/* http.c */
267extern URLProtocol http_protocol;
f031df23
FB
268
269#endif
270