avio: Keep track of the amount of data written
[libav.git] / libavformat / aviobuf.c
1 /*
2 * buffered I/O
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
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
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/crc.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/log.h"
26 #include "libavutil/opt.h"
27 #include "avformat.h"
28 #include "avio.h"
29 #include "avio_internal.h"
30 #include "internal.h"
31 #include "url.h"
32 #include <stdarg.h>
33
34 #define IO_BUFFER_SIZE 32768
35
36 /**
37 * Do seeks within this distance ahead of the current buffer by skipping
38 * data instead of calling the protocol seek function, for seekable
39 * protocols.
40 */
41 #define SHORT_SEEK_THRESHOLD 4096
42
43 typedef struct AVIOInternal {
44 const AVClass *class;
45
46 char *protocol_whitelist;
47 char *protocol_blacklist;
48
49 URLContext *h;
50 const URLProtocol **protocols;
51 } AVIOInternal;
52
53 static void *io_priv_child_next(void *obj, void *prev)
54 {
55 AVIOInternal *internal = obj;
56 return prev ? NULL : internal->h;
57 }
58
59 static const AVClass *io_priv_child_class_next(const AVClass *prev)
60 {
61 return prev ? NULL : &ffurl_context_class;
62 }
63
64 #define OFFSET(x) offsetof(AVIOInternal, x)
65 static const AVOption io_priv_options[] = {
66 { "protocol_whitelist", "A comma-separated list of allowed protocols",
67 OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING },
68 { "protocol_blacklist", "A comma-separated list of forbidden protocols",
69 OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING },
70 { NULL },
71 };
72
73 static const AVClass io_priv_class = {
74 .class_name = "AVIOContext",
75 .item_name = av_default_item_name,
76 .version = LIBAVUTIL_VERSION_INT,
77 .option = io_priv_options,
78 .child_next = io_priv_child_next,
79 .child_class_next = io_priv_child_class_next,
80 };
81
82 static void *ff_avio_child_next(void *obj, void *prev)
83 {
84 AVIOContext *s = obj;
85 return prev ? NULL : s->opaque;
86 }
87
88 static const AVClass *ff_avio_child_class_next(const AVClass *prev)
89 {
90 return prev ? NULL : &io_priv_class;
91 }
92
93 static const AVOption ff_avio_options[] = {
94 { NULL },
95 };
96
97 const AVClass ff_avio_class = {
98 .class_name = "AVIOContext",
99 .item_name = av_default_item_name,
100 .version = LIBAVUTIL_VERSION_INT,
101 .option = ff_avio_options,
102 .child_next = ff_avio_child_next,
103 .child_class_next = ff_avio_child_class_next,
104 };
105
106 static void fill_buffer(AVIOContext *s);
107 static int url_resetbuf(AVIOContext *s, int flags);
108
109 int ffio_init_context(AVIOContext *s,
110 unsigned char *buffer,
111 int buffer_size,
112 int write_flag,
113 void *opaque,
114 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
115 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
116 int64_t (*seek)(void *opaque, int64_t offset, int whence))
117 {
118 s->buffer = buffer;
119 s->buffer_size = buffer_size;
120 s->buf_ptr = buffer;
121 s->opaque = opaque;
122
123 url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
124
125 s->write_packet = write_packet;
126 s->read_packet = read_packet;
127 s->seek = seek;
128 s->pos = 0;
129 s->must_flush = 0;
130 s->eof_reached = 0;
131 s->error = 0;
132 s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
133 s->max_packet_size = 0;
134 s->update_checksum = NULL;
135
136 if (!read_packet && !write_flag) {
137 s->pos = buffer_size;
138 s->buf_end = s->buffer + buffer_size;
139 }
140 s->read_pause = NULL;
141 s->read_seek = NULL;
142
143 s->write_data_type = NULL;
144 s->ignore_boundary_point = 0;
145 s->current_type = AVIO_DATA_MARKER_UNKNOWN;
146 s->last_time = AV_NOPTS_VALUE;
147
148 return 0;
149 }
150
151 AVIOContext *avio_alloc_context(
152 unsigned char *buffer,
153 int buffer_size,
154 int write_flag,
155 void *opaque,
156 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
157 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
158 int64_t (*seek)(void *opaque, int64_t offset, int whence))
159 {
160 AVIOContext *s = av_mallocz(sizeof(AVIOContext));
161 if (!s)
162 return NULL;
163 ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
164 read_packet, write_packet, seek);
165 return s;
166 }
167
168 static void flush_buffer(AVIOContext *s)
169 {
170 if (s->buf_ptr > s->buffer) {
171 int size = s->buf_ptr - s->buffer;
172 if (!s->error) {
173 int ret = 0;
174 if (s->write_data_type)
175 ret = s->write_data_type(s->opaque, s->buffer,
176 size,
177 s->current_type,
178 s->last_time);
179 else if (s->write_packet)
180 ret = s->write_packet(s->opaque, s->buffer,
181 size);
182 if (ret < 0) {
183 s->error = ret;
184 } else {
185 if (s->pos + size > s->written)
186 s->written = s->pos + size;
187 }
188 }
189 if (s->current_type == AVIO_DATA_MARKER_SYNC_POINT ||
190 s->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
191 s->current_type = AVIO_DATA_MARKER_UNKNOWN;
192 }
193 s->last_time = AV_NOPTS_VALUE;
194 if (s->update_checksum) {
195 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
196 s->buf_ptr - s->checksum_ptr);
197 s->checksum_ptr = s->buffer;
198 }
199 s->pos += size;
200 }
201 s->buf_ptr = s->buffer;
202 }
203
204 void avio_w8(AVIOContext *s, int b)
205 {
206 *s->buf_ptr++ = b;
207 if (s->buf_ptr >= s->buf_end)
208 flush_buffer(s);
209 }
210
211 void ffio_fill(AVIOContext *s, int b, int count)
212 {
213 while (count > 0) {
214 int len = FFMIN(s->buf_end - s->buf_ptr, count);
215 memset(s->buf_ptr, b, len);
216 s->buf_ptr += len;
217
218 if (s->buf_ptr >= s->buf_end)
219 flush_buffer(s);
220
221 count -= len;
222 }
223 }
224
225 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
226 {
227 while (size > 0) {
228 int len = FFMIN(s->buf_end - s->buf_ptr, size);
229 memcpy(s->buf_ptr, buf, len);
230 s->buf_ptr += len;
231
232 if (s->buf_ptr >= s->buf_end)
233 flush_buffer(s);
234
235 buf += len;
236 size -= len;
237 }
238 }
239
240 void avio_flush(AVIOContext *s)
241 {
242 flush_buffer(s);
243 s->must_flush = 0;
244 }
245
246 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
247 {
248 int64_t offset1;
249 int64_t pos;
250 int force = whence & AVSEEK_FORCE;
251 whence &= ~AVSEEK_FORCE;
252
253 if(!s)
254 return AVERROR(EINVAL);
255
256 pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
257
258 if (whence != SEEK_CUR && whence != SEEK_SET)
259 return AVERROR(EINVAL);
260
261 if (whence == SEEK_CUR) {
262 offset1 = pos + (s->buf_ptr - s->buffer);
263 if (offset == 0)
264 return offset1;
265 offset += offset1;
266 }
267 offset1 = offset - pos;
268 if (!s->must_flush &&
269 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
270 /* can do the seek inside the buffer */
271 s->buf_ptr = s->buffer + offset1;
272 } else if ((!(s->seekable & AVIO_SEEKABLE_NORMAL) ||
273 offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
274 !s->write_flag && offset1 >= 0 &&
275 (whence != SEEK_END || force)) {
276 while(s->pos < offset && !s->eof_reached)
277 fill_buffer(s);
278 if (s->eof_reached)
279 return AVERROR_EOF;
280 s->buf_ptr = s->buf_end + offset - s->pos;
281 } else {
282 int64_t res;
283
284 if (s->write_flag) {
285 flush_buffer(s);
286 s->must_flush = 1;
287 }
288 if (!s->seek)
289 return AVERROR(EPIPE);
290 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
291 return res;
292 if (!s->write_flag)
293 s->buf_end = s->buffer;
294 s->buf_ptr = s->buffer;
295 s->pos = offset;
296 }
297 s->eof_reached = 0;
298 return offset;
299 }
300
301 int64_t avio_size(AVIOContext *s)
302 {
303 int64_t size;
304
305 if (!s)
306 return AVERROR(EINVAL);
307
308 if (s->written)
309 return s->written;
310
311 if (!s->seek)
312 return AVERROR(ENOSYS);
313 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
314 if (size < 0) {
315 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
316 return size;
317 size++;
318 s->seek(s->opaque, s->pos, SEEK_SET);
319 }
320 return size;
321 }
322
323 void avio_wl32(AVIOContext *s, unsigned int val)
324 {
325 avio_w8(s, val);
326 avio_w8(s, val >> 8);
327 avio_w8(s, val >> 16);
328 avio_w8(s, val >> 24);
329 }
330
331 void avio_wb32(AVIOContext *s, unsigned int val)
332 {
333 avio_w8(s, val >> 24);
334 avio_w8(s, val >> 16);
335 avio_w8(s, val >> 8);
336 avio_w8(s, val);
337 }
338
339 int avio_put_str(AVIOContext *s, const char *str)
340 {
341 int len = 1;
342 if (str) {
343 len += strlen(str);
344 avio_write(s, (const unsigned char *) str, len);
345 } else
346 avio_w8(s, 0);
347 return len;
348 }
349
350 #define PUT_STR16(type, write) \
351 int avio_put_str16 ## type(AVIOContext * s, const char *str) \
352 { \
353 const uint8_t *q = str; \
354 int ret = 0; \
355 \
356 while (*q) { \
357 uint32_t ch; \
358 uint16_t tmp; \
359 \
360 GET_UTF8(ch, *q++, break; ) \
361 PUT_UTF16(ch, tmp, write(s, tmp); ret += 2; ) \
362 } \
363 write(s, 0); \
364 ret += 2; \
365 return ret; \
366 }
367
368 PUT_STR16(le, avio_wl16)
369 PUT_STR16(be, avio_wb16)
370
371 #undef PUT_STR16
372
373 int ff_get_v_length(uint64_t val)
374 {
375 int i = 1;
376
377 while (val >>= 7)
378 i++;
379
380 return i;
381 }
382
383 void ff_put_v(AVIOContext *bc, uint64_t val)
384 {
385 int i = ff_get_v_length(val);
386
387 while (--i > 0)
388 avio_w8(bc, 128 | (val >> (7 * i)));
389
390 avio_w8(bc, val & 127);
391 }
392
393 void avio_wl64(AVIOContext *s, uint64_t val)
394 {
395 avio_wl32(s, (uint32_t)(val & 0xffffffff));
396 avio_wl32(s, (uint32_t)(val >> 32));
397 }
398
399 void avio_wb64(AVIOContext *s, uint64_t val)
400 {
401 avio_wb32(s, (uint32_t)(val >> 32));
402 avio_wb32(s, (uint32_t)(val & 0xffffffff));
403 }
404
405 void avio_wl16(AVIOContext *s, unsigned int val)
406 {
407 avio_w8(s, val);
408 avio_w8(s, val >> 8);
409 }
410
411 void avio_wb16(AVIOContext *s, unsigned int val)
412 {
413 avio_w8(s, val >> 8);
414 avio_w8(s, val);
415 }
416
417 void avio_wl24(AVIOContext *s, unsigned int val)
418 {
419 avio_wl16(s, val & 0xffff);
420 avio_w8(s, val >> 16);
421 }
422
423 void avio_wb24(AVIOContext *s, unsigned int val)
424 {
425 avio_wb16(s, val >> 8);
426 avio_w8(s, val);
427 }
428
429 void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
430 {
431 if (!s->write_data_type)
432 return;
433 // If ignoring boundary points, just treat it as unknown
434 if (type == AVIO_DATA_MARKER_BOUNDARY_POINT && s->ignore_boundary_point)
435 type = AVIO_DATA_MARKER_UNKNOWN;
436 // Avoid unnecessary flushes if we are already in non-header/trailer
437 // data and setting the type to unknown
438 if (type == AVIO_DATA_MARKER_UNKNOWN &&
439 (s->current_type != AVIO_DATA_MARKER_HEADER &&
440 s->current_type != AVIO_DATA_MARKER_TRAILER))
441 return;
442
443 switch (type) {
444 case AVIO_DATA_MARKER_HEADER:
445 case AVIO_DATA_MARKER_TRAILER:
446 // For header/trailer, ignore a new marker of the same type;
447 // consecutive header/trailer markers can be merged.
448 if (type == s->current_type)
449 return;
450 break;
451 }
452
453 // If we've reached here, we have a new, noteworthy marker.
454 // Flush the previous data and mark the start of the new data.
455 avio_flush(s);
456 s->current_type = type;
457 s->last_time = time;
458 }
459
460 /* Input stream */
461
462 static void fill_buffer(AVIOContext *s)
463 {
464 uint8_t *dst = !s->max_packet_size &&
465 s->buf_end - s->buffer < s->buffer_size ?
466 s->buf_end : s->buffer;
467 int len = s->buffer_size - (dst - s->buffer);
468 int max_buffer_size = s->max_packet_size ?
469 s->max_packet_size : IO_BUFFER_SIZE;
470
471 /* can't fill the buffer without read_packet, just set EOF if appropriate */
472 if (!s->read_packet && s->buf_ptr >= s->buf_end)
473 s->eof_reached = 1;
474
475 /* no need to do anything if EOF already reached */
476 if (s->eof_reached)
477 return;
478
479 if (s->update_checksum && dst == s->buffer) {
480 if (s->buf_end > s->checksum_ptr)
481 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
482 s->buf_end - s->checksum_ptr);
483 s->checksum_ptr = s->buffer;
484 }
485
486 /* make buffer smaller in case it ended up large after probing */
487 if (s->buffer_size > max_buffer_size) {
488 ffio_set_buf_size(s, max_buffer_size);
489
490 s->checksum_ptr = dst = s->buffer;
491 len = s->buffer_size;
492 }
493
494 if (s->read_packet)
495 len = s->read_packet(s->opaque, dst, len);
496 else
497 len = 0;
498 if (len <= 0) {
499 /* do not modify buffer if EOF reached so that a seek back can
500 be done without rereading data */
501 s->eof_reached = 1;
502 if (len < 0)
503 s->error = len;
504 } else {
505 s->pos += len;
506 s->buf_ptr = dst;
507 s->buf_end = dst + len;
508 }
509 }
510
511 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
512 unsigned int len)
513 {
514 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
515 }
516
517 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
518 unsigned int len)
519 {
520 return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
521 }
522
523 unsigned long ffio_get_checksum(AVIOContext *s)
524 {
525 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
526 s->buf_ptr - s->checksum_ptr);
527 s->update_checksum = NULL;
528 return s->checksum;
529 }
530
531 void ffio_init_checksum(AVIOContext *s,
532 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
533 unsigned long checksum)
534 {
535 s->update_checksum = update_checksum;
536 if (s->update_checksum) {
537 s->checksum = checksum;
538 s->checksum_ptr = s->buf_ptr;
539 }
540 }
541
542 /* XXX: put an inline version */
543 int avio_r8(AVIOContext *s)
544 {
545 if (s->buf_ptr >= s->buf_end)
546 fill_buffer(s);
547 if (s->buf_ptr < s->buf_end)
548 return *s->buf_ptr++;
549 return 0;
550 }
551
552 int avio_read(AVIOContext *s, unsigned char *buf, int size)
553 {
554 int len, size1;
555
556 size1 = size;
557 while (size > 0) {
558 len = s->buf_end - s->buf_ptr;
559 if (len > size)
560 len = size;
561 if (len == 0 || s->write_flag) {
562 if(size > s->buffer_size && !s->update_checksum){
563 if(s->read_packet)
564 len = s->read_packet(s->opaque, buf, size);
565 if (len <= 0) {
566 /* do not modify buffer if EOF reached so that a seek back can
567 be done without rereading data */
568 s->eof_reached = 1;
569 if(len<0)
570 s->error= len;
571 break;
572 } else {
573 s->pos += len;
574 size -= len;
575 buf += len;
576 s->buf_ptr = s->buffer;
577 s->buf_end = s->buffer/* + len*/;
578 }
579 } else {
580 fill_buffer(s);
581 len = s->buf_end - s->buf_ptr;
582 if (len == 0)
583 break;
584 }
585 } else {
586 memcpy(buf, s->buf_ptr, len);
587 buf += len;
588 s->buf_ptr += len;
589 size -= len;
590 }
591 }
592 if (size1 == size) {
593 if (s->error) return s->error;
594 if (s->eof_reached) return AVERROR_EOF;
595 }
596 return size1 - size;
597 }
598
599 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
600 {
601 int ret = avio_read(s, buf, size);
602 if (ret != size)
603 return AVERROR_INVALIDDATA;
604 return ret;
605 }
606
607 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
608 {
609 if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
610 *data = s->buf_ptr;
611 s->buf_ptr += size;
612 return size;
613 } else {
614 *data = buf;
615 return avio_read(s, buf, size);
616 }
617 }
618
619 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
620 {
621 int len;
622
623 if (size < 0)
624 return -1;
625
626 if (s->read_packet && s->write_flag) {
627 len = s->read_packet(s->opaque, buf, size);
628 if (len > 0)
629 s->pos += len;
630 return len;
631 }
632
633 len = s->buf_end - s->buf_ptr;
634 if (len == 0) {
635 /* Reset the buf_end pointer to the start of the buffer, to make sure
636 * the fill_buffer call tries to read as much data as fits into the
637 * full buffer, instead of just what space is left after buf_end.
638 * This avoids returning partial packets at the end of the buffer,
639 * for packet based inputs.
640 */
641 s->buf_end = s->buf_ptr = s->buffer;
642 fill_buffer(s);
643 len = s->buf_end - s->buf_ptr;
644 }
645 if (len > size)
646 len = size;
647 memcpy(buf, s->buf_ptr, len);
648 s->buf_ptr += len;
649 if (!len) {
650 if (s->error) return s->error;
651 if (s->eof_reached) return AVERROR_EOF;
652 }
653 return len;
654 }
655
656 unsigned int avio_rl16(AVIOContext *s)
657 {
658 unsigned int val;
659 val = avio_r8(s);
660 val |= avio_r8(s) << 8;
661 return val;
662 }
663
664 unsigned int avio_rl24(AVIOContext *s)
665 {
666 unsigned int val;
667 val = avio_rl16(s);
668 val |= avio_r8(s) << 16;
669 return val;
670 }
671
672 unsigned int avio_rl32(AVIOContext *s)
673 {
674 unsigned int val;
675 val = avio_rl16(s);
676 val |= avio_rl16(s) << 16;
677 return val;
678 }
679
680 uint64_t avio_rl64(AVIOContext *s)
681 {
682 uint64_t val;
683 val = (uint64_t)avio_rl32(s);
684 val |= (uint64_t)avio_rl32(s) << 32;
685 return val;
686 }
687
688 unsigned int avio_rb16(AVIOContext *s)
689 {
690 unsigned int val;
691 val = avio_r8(s) << 8;
692 val |= avio_r8(s);
693 return val;
694 }
695
696 unsigned int avio_rb24(AVIOContext *s)
697 {
698 unsigned int val;
699 val = avio_rb16(s) << 8;
700 val |= avio_r8(s);
701 return val;
702 }
703 unsigned int avio_rb32(AVIOContext *s)
704 {
705 unsigned int val;
706 val = avio_rb16(s) << 16;
707 val |= avio_rb16(s);
708 return val;
709 }
710
711 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
712 {
713 int i = 0;
714 char c;
715
716 do {
717 c = avio_r8(s);
718 if (c && i < maxlen-1)
719 buf[i++] = c;
720 } while (c != '\n' && c);
721
722 buf[i] = 0;
723 return i;
724 }
725
726 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
727 {
728 int i;
729
730 if (buflen <= 0)
731 return AVERROR(EINVAL);
732 // reserve 1 byte for terminating 0
733 buflen = FFMIN(buflen - 1, maxlen);
734 for (i = 0; i < buflen; i++)
735 if (!(buf[i] = avio_r8(s)))
736 return i + 1;
737 buf[i] = 0;
738 for (; i < maxlen; i++)
739 if (!avio_r8(s))
740 return i + 1;
741 return maxlen;
742 }
743
744 #define GET_STR16(type, read) \
745 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
746 {\
747 char* q = buf;\
748 int ret = 0;\
749 if (buflen <= 0) \
750 return AVERROR(EINVAL); \
751 while (ret + 1 < maxlen) {\
752 uint8_t tmp;\
753 uint32_t ch;\
754 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
755 if (!ch)\
756 break;\
757 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
758 }\
759 *q = 0;\
760 return ret;\
761 }\
762
763 GET_STR16(le, avio_rl16)
764 GET_STR16(be, avio_rb16)
765
766 #undef GET_STR16
767
768 uint64_t avio_rb64(AVIOContext *s)
769 {
770 uint64_t val;
771 val = (uint64_t)avio_rb32(s) << 32;
772 val |= (uint64_t)avio_rb32(s);
773 return val;
774 }
775
776 uint64_t ffio_read_varlen(AVIOContext *bc){
777 uint64_t val = 0;
778 int tmp;
779
780 do{
781 tmp = avio_r8(bc);
782 val= (val<<7) + (tmp&127);
783 }while(tmp&128);
784 return val;
785 }
786
787 static int io_read_packet(void *opaque, uint8_t *buf, int buf_size)
788 {
789 AVIOInternal *internal = opaque;
790 return ffurl_read(internal->h, buf, buf_size);
791 }
792
793 static int io_write_packet(void *opaque, uint8_t *buf, int buf_size)
794 {
795 AVIOInternal *internal = opaque;
796 return ffurl_write(internal->h, buf, buf_size);
797 }
798
799 static int64_t io_seek(void *opaque, int64_t offset, int whence)
800 {
801 AVIOInternal *internal = opaque;
802 return ffurl_seek(internal->h, offset, whence);
803 }
804
805 static int io_read_pause(void *opaque, int pause)
806 {
807 AVIOInternal *internal = opaque;
808 if (!internal->h->prot->url_read_pause)
809 return AVERROR(ENOSYS);
810 return internal->h->prot->url_read_pause(internal->h, pause);
811 }
812
813 static int64_t io_read_seek(void *opaque, int stream_index, int64_t timestamp, int flags)
814 {
815 AVIOInternal *internal = opaque;
816 if (!internal->h->prot->url_read_seek)
817 return AVERROR(ENOSYS);
818 return internal->h->prot->url_read_seek(internal->h, stream_index, timestamp, flags);
819 }
820
821 int ffio_fdopen(AVIOContext **s, URLContext *h)
822 {
823 AVIOInternal *internal = NULL;
824 uint8_t *buffer = NULL;
825 int buffer_size, max_packet_size;
826
827 max_packet_size = h->max_packet_size;
828 if (max_packet_size) {
829 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
830 } else {
831 buffer_size = IO_BUFFER_SIZE;
832 }
833 buffer = av_malloc(buffer_size);
834 if (!buffer)
835 return AVERROR(ENOMEM);
836
837 internal = av_mallocz(sizeof(*internal));
838 if (!internal)
839 goto fail;
840
841 internal->class = &io_priv_class;
842 internal->h = h;
843
844 av_opt_set_defaults(internal);
845
846 *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE,
847 internal, io_read_packet, io_write_packet, io_seek);
848 if (!*s)
849 goto fail;
850
851 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
852 (*s)->max_packet_size = max_packet_size;
853 if(h->prot) {
854 (*s)->read_pause = io_read_pause;
855 (*s)->read_seek = io_read_seek;
856
857 if (h->prot->url_read_seek)
858 (*s)->seekable |= AVIO_SEEKABLE_TIME;
859 }
860 (*s)->av_class = &ff_avio_class;
861 return 0;
862 fail:
863 if (internal)
864 av_opt_free(internal);
865 av_freep(&internal);
866 av_freep(&buffer);
867 return AVERROR(ENOMEM);
868 }
869
870 int ffio_set_buf_size(AVIOContext *s, int buf_size)
871 {
872 uint8_t *buffer;
873 buffer = av_malloc(buf_size);
874 if (!buffer)
875 return AVERROR(ENOMEM);
876
877 av_free(s->buffer);
878 s->buffer = buffer;
879 s->buffer_size = buf_size;
880 s->buf_ptr = buffer;
881 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
882 return 0;
883 }
884
885 static int url_resetbuf(AVIOContext *s, int flags)
886 {
887 assert(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
888
889 if (flags & AVIO_FLAG_WRITE) {
890 s->buf_end = s->buffer + s->buffer_size;
891 s->write_flag = 1;
892 } else {
893 s->buf_end = s->buffer;
894 s->write_flag = 0;
895 }
896 return 0;
897 }
898
899 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
900 {
901 int64_t buffer_start;
902 int buffer_size;
903 int overlap, new_size, alloc_size;
904
905 if (s->write_flag)
906 return AVERROR(EINVAL);
907
908 buffer_size = s->buf_end - s->buffer;
909
910 /* the buffers must touch or overlap */
911 if ((buffer_start = s->pos - buffer_size) > buf_size)
912 return AVERROR(EINVAL);
913
914 overlap = buf_size - buffer_start;
915 new_size = buf_size + buffer_size - overlap;
916
917 alloc_size = FFMAX(s->buffer_size, new_size);
918 if (alloc_size > buf_size)
919 if (!(buf = av_realloc(buf, alloc_size)))
920 return AVERROR(ENOMEM);
921
922 if (new_size > buf_size) {
923 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
924 buf_size = new_size;
925 }
926
927 av_free(s->buffer);
928 s->buf_ptr = s->buffer = buf;
929 s->buffer_size = alloc_size;
930 s->pos = buf_size;
931 s->buf_end = s->buf_ptr + buf_size;
932 s->eof_reached = 0;
933 s->must_flush = 0;
934
935 return 0;
936 }
937
938 int avio_open(AVIOContext **s, const char *filename, int flags)
939 {
940 return avio_open2(s, filename, flags, NULL, NULL);
941 }
942
943 int avio_open2(AVIOContext **s, const char *filename, int flags,
944 const AVIOInterruptCB *int_cb, AVDictionary **options)
945 {
946 AVIOInternal *internal;
947 const URLProtocol **protocols;
948 char *proto_whitelist = NULL, *proto_blacklist = NULL;
949 AVDictionaryEntry *e;
950 URLContext *h;
951 int err;
952
953 if (options) {
954 e = av_dict_get(*options, "protocol_whitelist", NULL, 0);
955 if (e)
956 proto_whitelist = e->value;
957 e = av_dict_get(*options, "protocol_blacklist", NULL, 0);
958 if (e)
959 proto_blacklist = e->value;
960 }
961
962 protocols = ffurl_get_protocols(proto_whitelist, proto_blacklist);
963 if (!protocols)
964 return AVERROR(ENOMEM);
965
966 err = ffurl_open(&h, filename, flags, int_cb, options, protocols, NULL);
967 if (err < 0) {
968 av_freep(&protocols);
969 return err;
970 }
971
972 err = ffio_fdopen(s, h);
973 if (err < 0) {
974 ffurl_close(h);
975 av_freep(&protocols);
976 return err;
977 }
978
979 internal = (*s)->opaque;
980 internal->protocols = protocols;
981
982 if (options) {
983 err = av_opt_set_dict(internal, options);
984 if (err < 0) {
985 avio_closep(s);
986 return err;
987 }
988 }
989
990 return 0;
991 }
992
993 int avio_close(AVIOContext *s)
994 {
995 AVIOInternal *internal;
996 URLContext *h;
997
998 if (!s)
999 return 0;
1000
1001 avio_flush(s);
1002 internal = s->opaque;
1003 h = internal->h;
1004
1005 av_opt_free(internal);
1006
1007 av_freep(&internal->protocols);
1008 av_freep(&s->opaque);
1009 av_freep(&s->buffer);
1010 av_free(s);
1011 return ffurl_close(h);
1012 }
1013
1014 int avio_closep(AVIOContext **s)
1015 {
1016 int ret = avio_close(*s);
1017 *s = NULL;
1018 return ret;
1019 }
1020
1021 int avio_printf(AVIOContext *s, const char *fmt, ...)
1022 {
1023 va_list ap;
1024 char buf[4096];
1025 int ret;
1026
1027 va_start(ap, fmt);
1028 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
1029 va_end(ap);
1030 avio_write(s, buf, strlen(buf));
1031 return ret;
1032 }
1033
1034 int avio_pause(AVIOContext *s, int pause)
1035 {
1036 if (!s->read_pause)
1037 return AVERROR(ENOSYS);
1038 return s->read_pause(s->opaque, pause);
1039 }
1040
1041 int64_t avio_seek_time(AVIOContext *s, int stream_index,
1042 int64_t timestamp, int flags)
1043 {
1044 int64_t ret;
1045 if (!s->read_seek)
1046 return AVERROR(ENOSYS);
1047 ret = s->read_seek(s->opaque, stream_index, timestamp, flags);
1048 if (ret >= 0) {
1049 int64_t pos;
1050 s->buf_ptr = s->buf_end; // Flush buffer
1051 pos = s->seek(s->opaque, 0, SEEK_CUR);
1052 if (pos >= 0)
1053 s->pos = pos;
1054 else if (pos != AVERROR(ENOSYS))
1055 ret = pos;
1056 }
1057 return ret;
1058 }
1059
1060 /* output in a dynamic buffer */
1061
1062 typedef struct DynBuffer {
1063 int pos, size, allocated_size;
1064 uint8_t *buffer;
1065 int io_buffer_size;
1066 uint8_t io_buffer[1];
1067 } DynBuffer;
1068
1069 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1070 {
1071 DynBuffer *d = opaque;
1072 unsigned new_size, new_allocated_size;
1073
1074 /* reallocate buffer if needed */
1075 new_size = d->pos + buf_size;
1076 new_allocated_size = d->allocated_size;
1077 if (new_size < d->pos || new_size > INT_MAX/2)
1078 return -1;
1079 while (new_size > new_allocated_size) {
1080 if (!new_allocated_size)
1081 new_allocated_size = new_size;
1082 else
1083 new_allocated_size += new_allocated_size / 2 + 1;
1084 }
1085
1086 if (new_allocated_size > d->allocated_size) {
1087 int err;
1088 if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1089 d->allocated_size = 0;
1090 d->size = 0;
1091 return err;
1092 }
1093 d->allocated_size = new_allocated_size;
1094 }
1095 memcpy(d->buffer + d->pos, buf, buf_size);
1096 d->pos = new_size;
1097 if (d->pos > d->size)
1098 d->size = d->pos;
1099 return buf_size;
1100 }
1101
1102 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1103 {
1104 unsigned char buf1[4];
1105 int ret;
1106
1107 /* packetized write: output the header */
1108 AV_WB32(buf1, buf_size);
1109 ret = dyn_buf_write(opaque, buf1, 4);
1110 if (ret < 0)
1111 return ret;
1112
1113 /* then the data */
1114 return dyn_buf_write(opaque, buf, buf_size);
1115 }
1116
1117 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1118 {
1119 DynBuffer *d = opaque;
1120
1121 if (whence == SEEK_CUR)
1122 offset += d->pos;
1123 else if (whence == SEEK_END)
1124 offset += d->size;
1125 if (offset < 0 || offset > 0x7fffffffLL)
1126 return -1;
1127 d->pos = offset;
1128 return 0;
1129 }
1130
1131 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1132 {
1133 DynBuffer *d;
1134 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1135
1136 if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1137 return -1;
1138 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1139 if (!d)
1140 return AVERROR(ENOMEM);
1141 d->io_buffer_size = io_buffer_size;
1142 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1143 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1144 max_packet_size ? NULL : dyn_buf_seek);
1145 if(!*s) {
1146 av_free(d);
1147 return AVERROR(ENOMEM);
1148 }
1149 (*s)->max_packet_size = max_packet_size;
1150 return 0;
1151 }
1152
1153 int avio_open_dyn_buf(AVIOContext **s)
1154 {
1155 return url_open_dyn_buf_internal(s, 0);
1156 }
1157
1158 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1159 {
1160 if (max_packet_size <= 0)
1161 return -1;
1162 return url_open_dyn_buf_internal(s, max_packet_size);
1163 }
1164
1165 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1166 {
1167 DynBuffer *d;
1168 int size;
1169 static const char padbuf[AV_INPUT_BUFFER_PADDING_SIZE] = {0};
1170 int padding = 0;
1171
1172 if (!s) {
1173 *pbuffer = NULL;
1174 return 0;
1175 }
1176
1177 /* don't attempt to pad fixed-size packet buffers */
1178 if (!s->max_packet_size) {
1179 avio_write(s, padbuf, sizeof(padbuf));
1180 padding = AV_INPUT_BUFFER_PADDING_SIZE;
1181 }
1182
1183 avio_flush(s);
1184
1185 d = s->opaque;
1186 *pbuffer = d->buffer;
1187 size = d->size;
1188 av_free(d);
1189 av_free(s);
1190 return size - padding;
1191 }
1192
1193 void ffio_free_dyn_buf(AVIOContext **s)
1194 {
1195 uint8_t *tmp;
1196 if (!*s)
1197 return;
1198 avio_close_dyn_buf(*s, &tmp);
1199 av_free(tmp);
1200 *s = NULL;
1201 }
1202
1203 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1204 {
1205 DynBuffer *d = opaque;
1206
1207 d->pos += buf_size;
1208 if (d->pos > d->size)
1209 d->size = d->pos;
1210 return buf_size;
1211 }
1212
1213 int ffio_open_null_buf(AVIOContext **s)
1214 {
1215 int ret = url_open_dyn_buf_internal(s, 0);
1216 if (ret >= 0) {
1217 AVIOContext *pb = *s;
1218 pb->write_packet = null_buf_write;
1219 }
1220 return ret;
1221 }
1222
1223 int ffio_close_null_buf(AVIOContext *s)
1224 {
1225 DynBuffer *d = s->opaque;
1226 int size;
1227
1228 avio_flush(s);
1229
1230 size = d->size;
1231 av_free(d);
1232 av_free(s);
1233 return size;
1234 }