137
|
1 /*
|
|
2 * Buffered I/O for ffmpeg system
|
|
3 * Copyright (c) 2000,2001 Fabrice Bellard
|
|
4 *
|
|
5 * This library is free software; you can redistribute it and/or
|
|
6 * modify it under the terms of the GNU Lesser General Public
|
|
7 * License as published by the Free Software Foundation; either
|
|
8 * version 2 of the License, or (at your option) any later version.
|
|
9 *
|
|
10 * This library is distributed in the hope that it will be useful,
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 * Lesser General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU Lesser General Public
|
|
16 * License along with this library; if not, write to the Free Software
|
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 */
|
|
19 #include "avformat.h"
|
|
20 #include "avio.h"
|
|
21 #include <stdarg.h>
|
|
22
|
|
23 #define IO_BUFFER_SIZE 32768
|
|
24
|
|
25 int init_put_byte(ByteIOContext *s,
|
|
26 unsigned char *buffer,
|
|
27 int buffer_size,
|
|
28 int write_flag,
|
|
29 void *opaque,
|
|
30 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
|
|
31 void (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
|
|
32 int (*seek)(void *opaque, offset_t offset, int whence))
|
|
33 {
|
|
34 s->buffer = buffer;
|
|
35 s->buffer_size = buffer_size;
|
|
36 s->buf_ptr = buffer;
|
|
37 s->write_flag = write_flag;
|
|
38 if (!s->write_flag)
|
|
39 s->buf_end = buffer;
|
|
40 else
|
|
41 s->buf_end = buffer + buffer_size;
|
|
42 s->opaque = opaque;
|
|
43 s->write_packet = write_packet;
|
|
44 s->read_packet = read_packet;
|
|
45 s->seek = seek;
|
|
46 s->pos = 0;
|
|
47 s->must_flush = 0;
|
|
48 s->eof_reached = 0;
|
|
49 s->is_streamed = 0;
|
|
50 s->max_packet_size = 0;
|
|
51 return 0;
|
|
52 }
|
|
53
|
|
54 offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence)
|
|
55 {
|
|
56 offset_t offset1;
|
|
57
|
|
58 if (whence != SEEK_CUR && whence != SEEK_SET)
|
|
59 return -EINVAL;
|
|
60
|
|
61 {
|
|
62 if (whence == SEEK_CUR) {
|
|
63 offset1 = s->pos - (s->buf_end - s->buffer) + (s->buf_ptr - s->buffer);
|
|
64 if (offset == 0)
|
|
65 return offset1;
|
|
66 offset += offset1;
|
|
67 }
|
|
68 offset1 = offset - (s->pos - (s->buf_end - s->buffer));
|
|
69 if (offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
|
|
70 /* can do the seek inside the buffer */
|
|
71 s->buf_ptr = s->buffer + offset1;
|
|
72 } else {
|
|
73 if (!s->seek)
|
|
74 return -EPIPE;
|
|
75 s->buf_ptr = s->buffer;
|
|
76 s->buf_end = s->buffer;
|
|
77 s->seek(s->opaque, offset, SEEK_SET);
|
|
78 s->pos = offset;
|
|
79 }
|
|
80 s->eof_reached = 0;
|
|
81 }
|
|
82 return offset;
|
|
83 }
|
|
84
|
|
85 void url_fskip(ByteIOContext *s, offset_t offset)
|
|
86 {
|
|
87 url_fseek(s, offset, SEEK_CUR);
|
|
88 }
|
|
89
|
|
90 offset_t url_ftell(ByteIOContext *s)
|
|
91 {
|
|
92 return url_fseek(s, 0, SEEK_CUR);
|
|
93 }
|
|
94
|
|
95 int url_feof(ByteIOContext *s)
|
|
96 {
|
|
97 return s->eof_reached;
|
|
98 }
|
|
99
|
|
100 /* Input stream */
|
|
101
|
|
102 static void fill_buffer(ByteIOContext *s)
|
|
103 {
|
|
104 int len;
|
|
105
|
|
106 /* no need to do anything if EOF already reached */
|
|
107 if (s->eof_reached)
|
|
108 return;
|
|
109 len = s->read_packet(s->opaque, s->buffer, s->buffer_size);
|
|
110 if (len <= 0) {
|
|
111 /* do not modify buffer if EOF reached so that a seek back can
|
|
112 be done without rereading data */
|
|
113 s->eof_reached = 1;
|
|
114 } else {
|
|
115 s->pos += len;
|
|
116 s->buf_ptr = s->buffer;
|
|
117 s->buf_end = s->buffer + len;
|
|
118 }
|
|
119 }
|
|
120
|
|
121 /* NOTE: return 0 if EOF, so you cannot use it if EOF handling is
|
|
122 necessary */
|
|
123 /* XXX: put an inline version */
|
|
124 int get_byte(ByteIOContext *s)
|
|
125 {
|
|
126 if (s->buf_ptr < s->buf_end) {
|
|
127 return *s->buf_ptr++;
|
|
128 } else {
|
|
129 fill_buffer(s);
|
|
130 if (s->buf_ptr < s->buf_end)
|
|
131 return *s->buf_ptr++;
|
|
132 else
|
|
133 return 0;
|
|
134 }
|
|
135 }
|
|
136
|
|
137 /* NOTE: return URL_EOF (-1) if EOF */
|
|
138 int url_fgetc(ByteIOContext *s)
|
|
139 {
|
|
140 if (s->buf_ptr < s->buf_end) {
|
|
141 return *s->buf_ptr++;
|
|
142 } else {
|
|
143 fill_buffer(s);
|
|
144 if (s->buf_ptr < s->buf_end)
|
|
145 return *s->buf_ptr++;
|
|
146 else
|
|
147 return URL_EOF;
|
|
148 }
|
|
149 }
|
|
150
|
|
151 int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
|
|
152 {
|
|
153 int len, size1;
|
|
154
|
|
155 size1 = size;
|
|
156 while (size > 0) {
|
|
157 len = s->buf_end - s->buf_ptr;
|
|
158 if (len > size)
|
|
159 len = size;
|
|
160 if (len == 0) {
|
|
161 fill_buffer(s);
|
|
162 len = s->buf_end - s->buf_ptr;
|
|
163 if (len == 0)
|
|
164 break;
|
|
165 } else {
|
|
166 memcpy(buf, s->buf_ptr, len);
|
|
167 buf += len;
|
|
168 s->buf_ptr += len;
|
|
169 size -= len;
|
|
170 }
|
|
171 }
|
|
172 return size1 - size;
|
|
173 }
|
|
174
|
|
175 unsigned int get_le16(ByteIOContext *s)
|
|
176 {
|
|
177 unsigned int val;
|
|
178 val = get_byte(s);
|
|
179 val |= get_byte(s) << 8;
|
|
180 return val;
|
|
181 }
|
|
182
|
|
183 unsigned int get_le32(ByteIOContext *s)
|
|
184 {
|
|
185 unsigned int val;
|
|
186 val = get_byte(s);
|
|
187 val |= get_byte(s) << 8;
|
|
188 val |= get_byte(s) << 16;
|
|
189 val |= get_byte(s) << 24;
|
|
190 return val;
|
|
191 }
|
|
192
|
|
193 uint64_t get_le64(ByteIOContext *s)
|
|
194 {
|
|
195 uint64_t val;
|
|
196 val = (uint64_t)get_le32(s);
|
|
197 val |= (uint64_t)get_le32(s) << 32;
|
|
198 return val;
|
|
199 }
|
|
200
|
|
201 unsigned int get_be16(ByteIOContext *s)
|
|
202 {
|
|
203 unsigned int val;
|
|
204 val = get_byte(s) << 8;
|
|
205 val |= get_byte(s);
|
|
206 return val;
|
|
207 }
|
|
208
|
|
209 unsigned int get_be32(ByteIOContext *s)
|
|
210 {
|
|
211 unsigned int val;
|
|
212 val = get_byte(s) << 24;
|
|
213 val |= get_byte(s) << 16;
|
|
214 val |= get_byte(s) << 8;
|
|
215 val |= get_byte(s);
|
|
216 return val;
|
|
217 }
|
|
218
|
|
219 double get_be64_double(ByteIOContext *s)
|
|
220 {
|
|
221 union {
|
|
222 double d;
|
|
223 uint64_t ull;
|
|
224 } u;
|
|
225
|
|
226 u.ull = get_be64(s);
|
|
227 return u.d;
|
|
228 }
|
|
229
|
|
230 char *get_strz(ByteIOContext *s, char *buf, int maxlen)
|
|
231 {
|
|
232 int i = 0;
|
|
233 char c;
|
|
234
|
|
235 while ((c = get_byte(s))) {
|
|
236 if (i < maxlen-1)
|
|
237 buf[i++] = c;
|
|
238 }
|
|
239
|
|
240 buf[i] = 0; /* Ensure null terminated, but may be truncated */
|
|
241
|
|
242 return buf;
|
|
243 }
|
|
244
|
|
245 uint64_t get_be64(ByteIOContext *s)
|
|
246 {
|
|
247 uint64_t val;
|
|
248 val = (uint64_t)get_be32(s) << 32;
|
|
249 val |= (uint64_t)get_be32(s);
|
|
250 return val;
|
|
251 }
|
|
252
|
|
253 /* link with avio functions */
|
|
254
|
|
255 #define url_write_packet NULL
|
|
256
|
|
257 static int url_read_packet(void *opaque, uint8_t *buf, int buf_size)
|
|
258 {
|
|
259 URLContext *h = opaque;
|
|
260 return url_read(h, buf, buf_size);
|
|
261 }
|
|
262
|
|
263 static int url_seek_packet(void *opaque, int64_t offset, int whence)
|
|
264 {
|
|
265 URLContext *h = opaque;
|
|
266 url_seek(h, offset, whence);
|
|
267 return 0;
|
|
268 }
|
|
269
|
|
270 int url_fdopen(ByteIOContext *s, URLContext *h)
|
|
271 {
|
|
272 uint8_t *buffer;
|
|
273 int buffer_size, max_packet_size;
|
|
274
|
|
275
|
|
276 max_packet_size = url_get_max_packet_size(h);
|
|
277 if (max_packet_size) {
|
|
278 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
|
|
279 } else {
|
|
280 buffer_size = IO_BUFFER_SIZE;
|
|
281 }
|
|
282 buffer = malloc(buffer_size);
|
|
283 if (!buffer)
|
|
284 return -ENOMEM;
|
|
285
|
|
286 if (init_put_byte(s, buffer, buffer_size,
|
|
287 (h->flags & URL_WRONLY) != 0, h,
|
|
288 url_read_packet, url_write_packet, url_seek_packet) < 0) {
|
|
289 free(buffer);
|
|
290 return -EIO;
|
|
291 }
|
|
292 s->is_streamed = h->is_streamed;
|
|
293 s->max_packet_size = max_packet_size;
|
|
294 return 0;
|
|
295 }
|
|
296
|
|
297 /* XXX: must be called before any I/O */
|
|
298 int url_setbufsize(ByteIOContext *s, int buf_size)
|
|
299 {
|
|
300 uint8_t *buffer;
|
|
301 buffer = malloc(buf_size);
|
|
302 if (!buffer)
|
|
303 return -ENOMEM;
|
|
304
|
|
305 free(s->buffer);
|
|
306 s->buffer = buffer;
|
|
307 s->buffer_size = buf_size;
|
|
308 s->buf_ptr = buffer;
|
|
309 if (!s->write_flag)
|
|
310 s->buf_end = buffer;
|
|
311 else
|
|
312 s->buf_end = buffer + buf_size;
|
|
313 return 0;
|
|
314 }
|
|
315
|
|
316 /* NOTE: when opened as read/write, the buffers are only used for
|
|
317 reading */
|
|
318 int url_fopen(ByteIOContext *s, const char *filename, int flags)
|
|
319 {
|
|
320 URLContext *h;
|
|
321 int err;
|
|
322
|
|
323 err = url_open(&h, filename, flags);
|
|
324 if (err < 0)
|
|
325 return err;
|
|
326 err = url_fdopen(s, h);
|
|
327 if (err < 0) {
|
|
328 url_close(h);
|
|
329 return err;
|
|
330 }
|
|
331 return 0;
|
|
332 }
|
|
333
|
|
334 int url_fclose(ByteIOContext *s)
|
|
335 {
|
|
336 URLContext *h = s->opaque;
|
|
337
|
|
338 free(s->buffer);
|
|
339 memset(s, 0, sizeof(ByteIOContext));
|
|
340 return url_close(h);
|
|
341 }
|
|
342
|
|
343 URLContext *url_fileno(ByteIOContext *s)
|
|
344 {
|
|
345 return s->opaque;
|
|
346 }
|
|
347
|
|
348 /* note: unlike fgets, the EOL character is not returned and a whole
|
|
349 line is parsed. return NULL if first char read was EOF */
|
|
350 char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
|
|
351 {
|
|
352 int c;
|
|
353 char *q;
|
|
354
|
|
355 c = url_fgetc(s);
|
|
356 if (c == EOF)
|
|
357 return NULL;
|
|
358 q = buf;
|
|
359 for(;;) {
|
|
360 if (c == EOF || c == '\n')
|
|
361 break;
|
|
362 if ((q - buf) < buf_size - 1)
|
|
363 *q++ = c;
|
|
364 c = url_fgetc(s);
|
|
365 }
|
|
366 if (buf_size > 0)
|
|
367 *q = '\0';
|
|
368 return buf;
|
|
369 }
|
|
370
|
|
371 /*
|
|
372 * Return the maximum packet size associated to packetized buffered file
|
|
373 * handle. If the file is not packetized (stream like http or file on
|
|
374 * disk), then 0 is returned.
|
|
375 *
|
|
376 * @param h buffered file handle
|
|
377 * @return maximum packet size in bytes
|
|
378 */
|
|
379 int url_fget_max_packet_size(ByteIOContext *s)
|
|
380 {
|
|
381 return s->max_packet_size;
|
|
382 }
|
|
383
|