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