808
|
1 /*
|
|
2 * Buffered I/O for ffmpeg system
|
|
3 * Copyright (c) 2000,2001 Fabrice Bellard
|
|
4 *
|
823
|
5 * This library is free software; you can redistribute it and/or
|
808
|
6 * modify it under the terms of the GNU Lesser General Public
|
|
7 * License as published by the Free Software Foundation; either
|
823
|
8 * version 2 of the License, or (at your option) any later version.
|
808
|
9 *
|
823
|
10 * This library is distributed in the hope that it will be useful,
|
808
|
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
|
823
|
16 * License along with this library; if not, write to the Free Software
|
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
808
|
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),
|
823
|
31 void (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
|
|
32 int (*seek)(void *opaque, offset_t offset, int whence))
|
808
|
33 {
|
|
34 s->buffer = buffer;
|
|
35 s->buffer_size = buffer_size;
|
|
36 s->buf_ptr = buffer;
|
|
37 s->write_flag = write_flag;
|
823
|
38 if (!s->write_flag)
|
808
|
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 }
|
823
|
53
|
808
|
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;
|
823
|
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;
|
808
|
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) {
|
823
|
161 fill_buffer(s);
|
|
162 len = s->buf_end - s->buf_ptr;
|
|
163 if (len == 0)
|
|
164 break;
|
808
|
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
|
828
|
175 int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
|
|
176 {
|
|
177 int len;
|
|
178
|
|
179 if(size<0)
|
|
180 return -1;
|
|
181
|
|
182 len = s->buf_end - s->buf_ptr;
|
|
183 if (len == 0) {
|
|
184 fill_buffer(s);
|
|
185 len = s->buf_end - s->buf_ptr;
|
|
186 }
|
|
187 if (len > size)
|
|
188 len = size;
|
|
189 memcpy(buf, s->buf_ptr, len);
|
|
190 s->buf_ptr += len;
|
|
191 return len;
|
|
192 }
|
|
193
|
808
|
194 unsigned int get_le16(ByteIOContext *s)
|
|
195 {
|
|
196 unsigned int val;
|
|
197 val = get_byte(s);
|
|
198 val |= get_byte(s) << 8;
|
|
199 return val;
|
|
200 }
|
|
201
|
828
|
202 unsigned int get_le24(ByteIOContext *s)
|
|
203 {
|
|
204 unsigned int val;
|
|
205 val = get_le16(s);
|
|
206 val |= get_byte(s) << 16;
|
|
207 return val;
|
|
208 }
|
|
209
|
808
|
210 unsigned int get_le32(ByteIOContext *s)
|
|
211 {
|
|
212 unsigned int val;
|
823
|
213 val = get_byte(s);
|
|
214 val |= get_byte(s) << 8;
|
|
215 val |= get_byte(s) << 16;
|
|
216 val |= get_byte(s) << 24;
|
808
|
217 return val;
|
|
218 }
|
|
219
|
|
220 uint64_t get_le64(ByteIOContext *s)
|
|
221 {
|
|
222 uint64_t val;
|
|
223 val = (uint64_t)get_le32(s);
|
|
224 val |= (uint64_t)get_le32(s) << 32;
|
|
225 return val;
|
|
226 }
|
|
227
|
|
228 unsigned int get_be16(ByteIOContext *s)
|
|
229 {
|
|
230 unsigned int val;
|
|
231 val = get_byte(s) << 8;
|
|
232 val |= get_byte(s);
|
|
233 return val;
|
|
234 }
|
|
235
|
828
|
236 unsigned int get_be24(ByteIOContext *s)
|
|
237 {
|
|
238 unsigned int val;
|
|
239 val = get_be16(s) << 8;
|
|
240 val |= get_byte(s);
|
|
241 return val;
|
|
242 }
|
|
243
|
823
|
244 unsigned int get_be32(ByteIOContext *s)
|
808
|
245 {
|
|
246 unsigned int val;
|
823
|
247 val = get_byte(s) << 24;
|
|
248 val |= get_byte(s) << 16;
|
|
249 val |= get_byte(s) << 8;
|
808
|
250 val |= get_byte(s);
|
|
251 return val;
|
|
252 }
|
823
|
253
|
|
254 double get_be64_double(ByteIOContext *s)
|
808
|
255 {
|
823
|
256 union {
|
|
257 double d;
|
|
258 uint64_t ull;
|
|
259 } u;
|
|
260
|
|
261 u.ull = get_be64(s);
|
|
262 return u.d;
|
808
|
263 }
|
|
264
|
|
265 char *get_strz(ByteIOContext *s, char *buf, int maxlen)
|
|
266 {
|
|
267 int i = 0;
|
|
268 char c;
|
|
269
|
|
270 while ((c = get_byte(s))) {
|
|
271 if (i < maxlen-1)
|
|
272 buf[i++] = c;
|
|
273 }
|
823
|
274
|
808
|
275 buf[i] = 0; /* Ensure null terminated, but may be truncated */
|
|
276
|
|
277 return buf;
|
|
278 }
|
|
279
|
|
280 uint64_t get_be64(ByteIOContext *s)
|
|
281 {
|
|
282 uint64_t val;
|
|
283 val = (uint64_t)get_be32(s) << 32;
|
|
284 val |= (uint64_t)get_be32(s);
|
|
285 return val;
|
|
286 }
|
|
287
|
|
288 /* link with avio functions */
|
|
289
|
823
|
290 #define url_write_packet NULL
|
808
|
291
|
|
292 static int url_read_packet(void *opaque, uint8_t *buf, int buf_size)
|
|
293 {
|
|
294 URLContext *h = opaque;
|
|
295 return url_read(h, buf, buf_size);
|
|
296 }
|
|
297
|
823
|
298 static int url_seek_packet(void *opaque, int64_t offset, int whence)
|
808
|
299 {
|
|
300 URLContext *h = opaque;
|
823
|
301 url_seek(h, offset, whence);
|
|
302 return 0;
|
808
|
303 }
|
|
304
|
|
305 int url_fdopen(ByteIOContext *s, URLContext *h)
|
|
306 {
|
|
307 uint8_t *buffer;
|
|
308 int buffer_size, max_packet_size;
|
823
|
309
|
808
|
310 max_packet_size = url_get_max_packet_size(h);
|
|
311 if (max_packet_size) {
|
|
312 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
|
|
313 } else {
|
|
314 buffer_size = IO_BUFFER_SIZE;
|
|
315 }
|
823
|
316 buffer = av_mallocz(buffer_size);
|
808
|
317 if (!buffer)
|
|
318 return -ENOMEM;
|
|
319
|
823
|
320 if (init_put_byte(s, buffer, buffer_size,
|
|
321 (h->flags & URL_WRONLY) != 0, h,
|
808
|
322 url_read_packet, url_write_packet, url_seek_packet) < 0) {
|
823
|
323 free(buffer);
|
|
324 return -EIO;
|
808
|
325 }
|
|
326 s->is_streamed = h->is_streamed;
|
|
327 s->max_packet_size = max_packet_size;
|
|
328 return 0;
|
|
329 }
|
|
330
|
|
331 /* XXX: must be called before any I/O */
|
|
332 int url_setbufsize(ByteIOContext *s, int buf_size)
|
|
333 {
|
|
334 uint8_t *buffer;
|
|
335 buffer = av_malloc(buf_size);
|
|
336 if (!buffer)
|
|
337 return -ENOMEM;
|
|
338
|
823
|
339 free(s->buffer);
|
808
|
340 s->buffer = buffer;
|
|
341 s->buffer_size = buf_size;
|
|
342 s->buf_ptr = buffer;
|
823
|
343 if (!s->write_flag)
|
808
|
344 s->buf_end = buffer;
|
|
345 else
|
|
346 s->buf_end = buffer + buf_size;
|
|
347 return 0;
|
|
348 }
|
|
349
|
|
350 /* NOTE: when opened as read/write, the buffers are only used for
|
|
351 reading */
|
|
352 int url_fopen(ByteIOContext *s, const char *filename, int flags)
|
|
353 {
|
|
354 URLContext *h;
|
|
355 int err;
|
|
356
|
|
357 err = url_open(&h, filename, flags);
|
|
358 if (err < 0)
|
|
359 return err;
|
|
360 err = url_fdopen(s, h);
|
|
361 if (err < 0) {
|
|
362 url_close(h);
|
|
363 return err;
|
|
364 }
|
|
365 return 0;
|
|
366 }
|
|
367
|
823
|
368 int url_vfdopen(ByteIOContext *s, VFSFile *fd)
|
|
369 {
|
|
370 URLContext *h;
|
|
371 int err;
|
|
372
|
|
373 err = url_vopen(&h, fd);
|
|
374 if (err < 0)
|
|
375 return err;
|
|
376 err = url_fdopen(s, h);
|
|
377 if (err < 0) {
|
|
378 return err;
|
|
379 }
|
|
380 return 0;
|
|
381 }
|
|
382
|
808
|
383 int url_fclose(ByteIOContext *s)
|
|
384 {
|
|
385 URLContext *h = s->opaque;
|
823
|
386
|
|
387 free(s->buffer);
|
808
|
388 memset(s, 0, sizeof(ByteIOContext));
|
|
389 return url_close(h);
|
|
390 }
|
|
391
|
|
392 URLContext *url_fileno(ByteIOContext *s)
|
|
393 {
|
|
394 return s->opaque;
|
|
395 }
|
|
396
|
|
397 /* note: unlike fgets, the EOL character is not returned and a whole
|
|
398 line is parsed. return NULL if first char read was EOF */
|
|
399 char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
|
|
400 {
|
|
401 int c;
|
|
402 char *q;
|
|
403
|
|
404 c = url_fgetc(s);
|
|
405 if (c == EOF)
|
|
406 return NULL;
|
|
407 q = buf;
|
|
408 for(;;) {
|
|
409 if (c == EOF || c == '\n')
|
|
410 break;
|
|
411 if ((q - buf) < buf_size - 1)
|
|
412 *q++ = c;
|
|
413 c = url_fgetc(s);
|
|
414 }
|
|
415 if (buf_size > 0)
|
|
416 *q = '\0';
|
|
417 return buf;
|
|
418 }
|
|
419
|
823
|
420 /*
|
808
|
421 * Return the maximum packet size associated to packetized buffered file
|
|
422 * handle. If the file is not packetized (stream like http or file on
|
|
423 * disk), then 0 is returned.
|
823
|
424 *
|
808
|
425 * @param h buffered file handle
|
|
426 * @return maximum packet size in bytes
|
|
427 */
|
|
428 int url_fget_max_packet_size(ByteIOContext *s)
|
|
429 {
|
|
430 return s->max_packet_size;
|
|
431 }
|
|
432
|
824
|
433 offset_t url_fsize(ByteIOContext *s)
|
|
434 {
|
|
435 offset_t size;
|
|
436
|
|
437 if (!s->seek)
|
|
438 return -EPIPE;
|
|
439 size = s->seek(s->opaque, -1, SEEK_END) + 1;
|
|
440 s->seek(s->opaque, s->pos, SEEK_SET);
|
|
441 return size;
|
|
442 }
|
|
443
|
828
|
444 static void flush_buffer(ByteIOContext *s)
|
|
445 {
|
|
446 if (s->buf_ptr > s->buffer) {
|
|
447 if (s->write_packet){
|
|
448 s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
|
|
449 }
|
|
450 s->pos += s->buf_ptr - s->buffer;
|
|
451 }
|
|
452 s->buf_ptr = s->buffer;
|
|
453 }
|
|
454
|
832
|
455 void put_flush_packet(ByteIOContext *s)
|
|
456 {
|
|
457 flush_buffer(s);
|
|
458 s->must_flush = 0;
|
|
459 }
|
|
460
|
828
|
461 void put_byte(ByteIOContext *s, int b)
|
|
462 {
|
|
463 *(s->buf_ptr)++ = b;
|
|
464 if (s->buf_ptr >= s->buf_end)
|
|
465 flush_buffer(s);
|
|
466 }
|
|
467
|
|
468 void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
|
|
469 {
|
|
470 int len;
|
|
471
|
|
472 while (size > 0) {
|
|
473 len = (s->buf_end - s->buf_ptr);
|
|
474 if (len > size)
|
|
475 len = size;
|
|
476 memcpy(s->buf_ptr, buf, len);
|
|
477 s->buf_ptr += len;
|
|
478
|
|
479 if (s->buf_ptr >= s->buf_end)
|
|
480 flush_buffer(s);
|
|
481
|
|
482 buf += len;
|
|
483 size -= len;
|
|
484 }
|
|
485 }
|
|
486
|
830
|
487 void put_le32(ByteIOContext *s, unsigned int val)
|
|
488 {
|
|
489 put_byte(s, val);
|
|
490 put_byte(s, val >> 8);
|
|
491 put_byte(s, val >> 16);
|
|
492 put_byte(s, val >> 24);
|
|
493 }
|
|
494
|
828
|
495 void put_be32(ByteIOContext *s, unsigned int val)
|
|
496 {
|
|
497 put_byte(s, val >> 24);
|
|
498 put_byte(s, val >> 16);
|
|
499 put_byte(s, val >> 8);
|
|
500 put_byte(s, val);
|
|
501 }
|
|
502
|
|
503 void put_strz(ByteIOContext *s, const char *str)
|
|
504 {
|
|
505 if (str)
|
|
506 put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
|
|
507 else
|
|
508 put_byte(s, 0);
|
|
509 }
|
|
510
|
|
511 void put_le64(ByteIOContext *s, uint64_t val)
|
|
512 {
|
|
513 put_le32(s, (uint32_t)(val & 0xffffffff));
|
|
514 put_le32(s, (uint32_t)(val >> 32));
|
|
515 }
|
|
516
|
|
517 void put_be64(ByteIOContext *s, uint64_t val)
|
|
518 {
|
|
519 put_be32(s, (uint32_t)(val >> 32));
|
|
520 put_be32(s, (uint32_t)(val & 0xffffffff));
|
|
521 }
|
|
522
|
|
523 void put_le16(ByteIOContext *s, unsigned int val)
|
|
524 {
|
|
525 put_byte(s, val);
|
|
526 put_byte(s, val >> 8);
|
|
527 }
|
|
528
|
|
529 void put_be16(ByteIOContext *s, unsigned int val)
|
|
530 {
|
|
531 put_byte(s, val >> 8);
|
|
532 put_byte(s, val);
|
|
533 }
|
|
534
|
|
535 void put_le24(ByteIOContext *s, unsigned int val)
|
|
536 {
|
|
537 put_le16(s, val & 0xffff);
|
|
538 put_byte(s, val >> 16);
|
|
539 }
|
|
540
|
|
541 void put_be24(ByteIOContext *s, unsigned int val)
|
|
542 {
|
|
543 put_be16(s, val >> 8);
|
|
544 put_byte(s, val);
|
|
545 }
|
|
546
|
|
547 void put_tag(ByteIOContext *s, const char *tag)
|
|
548 {
|
|
549 while (*tag) {
|
|
550 put_byte(s, *tag++);
|
|
551 }
|
|
552 }
|
|
553
|