mp3dec: decode more data from Info header
[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 static void *ffio_url_child_next(void *obj, void *prev)
44 {
45 AVIOContext *s = obj;
46 return prev ? NULL : s->opaque;
47 }
48
49 static const AVClass *ffio_url_child_class_next(const AVClass *prev)
50 {
51 return prev ? NULL : &ffurl_context_class;
52 }
53
54 static const AVOption ffio_url_options[] = {
55 { NULL },
56 };
57
58 const AVClass ffio_url_class = {
59 .class_name = "AVIOContext",
60 .item_name = av_default_item_name,
61 .version = LIBAVUTIL_VERSION_INT,
62 .option = ffio_url_options,
63 .child_next = ffio_url_child_next,
64 .child_class_next = ffio_url_child_class_next,
65 };
66
67 static void fill_buffer(AVIOContext *s);
68 static int url_resetbuf(AVIOContext *s, int flags);
69
70 int ffio_init_context(AVIOContext *s,
71 unsigned char *buffer,
72 int buffer_size,
73 int write_flag,
74 void *opaque,
75 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
76 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
77 int64_t (*seek)(void *opaque, int64_t offset, int whence))
78 {
79 s->buffer = buffer;
80 s->buffer_size = buffer_size;
81 s->buf_ptr = buffer;
82 s->opaque = opaque;
83
84 url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
85
86 s->write_packet = write_packet;
87 s->read_packet = read_packet;
88 s->seek = seek;
89 s->pos = 0;
90 s->must_flush = 0;
91 s->eof_reached = 0;
92 s->error = 0;
93 s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
94 s->max_packet_size = 0;
95 s->update_checksum = NULL;
96
97 if (!read_packet && !write_flag) {
98 s->pos = buffer_size;
99 s->buf_end = s->buffer + buffer_size;
100 }
101 s->read_pause = NULL;
102 s->read_seek = NULL;
103
104 return 0;
105 }
106
107 AVIOContext *avio_alloc_context(
108 unsigned char *buffer,
109 int buffer_size,
110 int write_flag,
111 void *opaque,
112 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
113 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
114 int64_t (*seek)(void *opaque, int64_t offset, int whence))
115 {
116 AVIOContext *s = av_mallocz(sizeof(AVIOContext));
117 if (!s)
118 return NULL;
119 ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
120 read_packet, write_packet, seek);
121 return s;
122 }
123
124 static void flush_buffer(AVIOContext *s)
125 {
126 if (s->buf_ptr > s->buffer) {
127 if (s->write_packet && !s->error) {
128 int ret = s->write_packet(s->opaque, s->buffer,
129 s->buf_ptr - s->buffer);
130 if (ret < 0) {
131 s->error = ret;
132 }
133 }
134 if (s->update_checksum) {
135 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
136 s->buf_ptr - s->checksum_ptr);
137 s->checksum_ptr = s->buffer;
138 }
139 s->pos += s->buf_ptr - s->buffer;
140 }
141 s->buf_ptr = s->buffer;
142 }
143
144 void avio_w8(AVIOContext *s, int b)
145 {
146 *s->buf_ptr++ = b;
147 if (s->buf_ptr >= s->buf_end)
148 flush_buffer(s);
149 }
150
151 void ffio_fill(AVIOContext *s, int b, int count)
152 {
153 while (count > 0) {
154 int len = FFMIN(s->buf_end - s->buf_ptr, count);
155 memset(s->buf_ptr, b, len);
156 s->buf_ptr += len;
157
158 if (s->buf_ptr >= s->buf_end)
159 flush_buffer(s);
160
161 count -= len;
162 }
163 }
164
165 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
166 {
167 while (size > 0) {
168 int len = FFMIN(s->buf_end - s->buf_ptr, size);
169 memcpy(s->buf_ptr, buf, len);
170 s->buf_ptr += len;
171
172 if (s->buf_ptr >= s->buf_end)
173 flush_buffer(s);
174
175 buf += len;
176 size -= len;
177 }
178 }
179
180 void avio_flush(AVIOContext *s)
181 {
182 flush_buffer(s);
183 s->must_flush = 0;
184 }
185
186 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
187 {
188 int64_t offset1;
189 int64_t pos;
190 int force = whence & AVSEEK_FORCE;
191 whence &= ~AVSEEK_FORCE;
192
193 if(!s)
194 return AVERROR(EINVAL);
195
196 pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
197
198 if (whence != SEEK_CUR && whence != SEEK_SET)
199 return AVERROR(EINVAL);
200
201 if (whence == SEEK_CUR) {
202 offset1 = pos + (s->buf_ptr - s->buffer);
203 if (offset == 0)
204 return offset1;
205 offset += offset1;
206 }
207 offset1 = offset - pos;
208 if (!s->must_flush &&
209 offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
210 /* can do the seek inside the buffer */
211 s->buf_ptr = s->buffer + offset1;
212 } else if ((!s->seekable ||
213 offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
214 !s->write_flag && offset1 >= 0 &&
215 (whence != SEEK_END || force)) {
216 while(s->pos < offset && !s->eof_reached)
217 fill_buffer(s);
218 if (s->eof_reached)
219 return AVERROR_EOF;
220 s->buf_ptr = s->buf_end + offset - s->pos;
221 } else {
222 int64_t res;
223
224 if (s->write_flag) {
225 flush_buffer(s);
226 s->must_flush = 1;
227 }
228 if (!s->seek)
229 return AVERROR(EPIPE);
230 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
231 return res;
232 if (!s->write_flag)
233 s->buf_end = s->buffer;
234 s->buf_ptr = s->buffer;
235 s->pos = offset;
236 }
237 s->eof_reached = 0;
238 return offset;
239 }
240
241 int64_t avio_size(AVIOContext *s)
242 {
243 int64_t size;
244
245 if (!s)
246 return AVERROR(EINVAL);
247
248 if (!s->seek)
249 return AVERROR(ENOSYS);
250 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
251 if (size < 0) {
252 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
253 return size;
254 size++;
255 s->seek(s->opaque, s->pos, SEEK_SET);
256 }
257 return size;
258 }
259
260 void avio_wl32(AVIOContext *s, unsigned int val)
261 {
262 avio_w8(s, val);
263 avio_w8(s, val >> 8);
264 avio_w8(s, val >> 16);
265 avio_w8(s, val >> 24);
266 }
267
268 void avio_wb32(AVIOContext *s, unsigned int val)
269 {
270 avio_w8(s, val >> 24);
271 avio_w8(s, val >> 16);
272 avio_w8(s, val >> 8);
273 avio_w8(s, val);
274 }
275
276 int avio_put_str(AVIOContext *s, const char *str)
277 {
278 int len = 1;
279 if (str) {
280 len += strlen(str);
281 avio_write(s, (const unsigned char *) str, len);
282 } else
283 avio_w8(s, 0);
284 return len;
285 }
286
287 int avio_put_str16le(AVIOContext *s, const char *str)
288 {
289 const uint8_t *q = str;
290 int ret = 0;
291
292 while (*q) {
293 uint32_t ch;
294 uint16_t tmp;
295
296 GET_UTF8(ch, *q++, break;)
297 PUT_UTF16(ch, tmp, avio_wl16(s, tmp); ret += 2;)
298 }
299 avio_wl16(s, 0);
300 ret += 2;
301 return ret;
302 }
303
304 int ff_get_v_length(uint64_t val)
305 {
306 int i = 1;
307
308 while (val >>= 7)
309 i++;
310
311 return i;
312 }
313
314 void ff_put_v(AVIOContext *bc, uint64_t val)
315 {
316 int i = ff_get_v_length(val);
317
318 while (--i > 0)
319 avio_w8(bc, 128 | (val >> (7 * i)));
320
321 avio_w8(bc, val & 127);
322 }
323
324 void avio_wl64(AVIOContext *s, uint64_t val)
325 {
326 avio_wl32(s, (uint32_t)(val & 0xffffffff));
327 avio_wl32(s, (uint32_t)(val >> 32));
328 }
329
330 void avio_wb64(AVIOContext *s, uint64_t val)
331 {
332 avio_wb32(s, (uint32_t)(val >> 32));
333 avio_wb32(s, (uint32_t)(val & 0xffffffff));
334 }
335
336 void avio_wl16(AVIOContext *s, unsigned int val)
337 {
338 avio_w8(s, val);
339 avio_w8(s, val >> 8);
340 }
341
342 void avio_wb16(AVIOContext *s, unsigned int val)
343 {
344 avio_w8(s, val >> 8);
345 avio_w8(s, val);
346 }
347
348 void avio_wl24(AVIOContext *s, unsigned int val)
349 {
350 avio_wl16(s, val & 0xffff);
351 avio_w8(s, val >> 16);
352 }
353
354 void avio_wb24(AVIOContext *s, unsigned int val)
355 {
356 avio_wb16(s, val >> 8);
357 avio_w8(s, val);
358 }
359
360 /* Input stream */
361
362 static void fill_buffer(AVIOContext *s)
363 {
364 uint8_t *dst = !s->max_packet_size &&
365 s->buf_end - s->buffer < s->buffer_size ?
366 s->buf_end : s->buffer;
367 int len = s->buffer_size - (dst - s->buffer);
368 int max_buffer_size = s->max_packet_size ?
369 s->max_packet_size : IO_BUFFER_SIZE;
370
371 /* can't fill the buffer without read_packet, just set EOF if appropriate */
372 if (!s->read_packet && s->buf_ptr >= s->buf_end)
373 s->eof_reached = 1;
374
375 /* no need to do anything if EOF already reached */
376 if (s->eof_reached)
377 return;
378
379 if (s->update_checksum && dst == s->buffer) {
380 if (s->buf_end > s->checksum_ptr)
381 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
382 s->buf_end - s->checksum_ptr);
383 s->checksum_ptr = s->buffer;
384 }
385
386 /* make buffer smaller in case it ended up large after probing */
387 if (s->buffer_size > max_buffer_size) {
388 ffio_set_buf_size(s, max_buffer_size);
389
390 s->checksum_ptr = dst = s->buffer;
391 len = s->buffer_size;
392 }
393
394 if (s->read_packet)
395 len = s->read_packet(s->opaque, dst, len);
396 else
397 len = 0;
398 if (len <= 0) {
399 /* do not modify buffer if EOF reached so that a seek back can
400 be done without rereading data */
401 s->eof_reached = 1;
402 if (len < 0)
403 s->error = len;
404 } else {
405 s->pos += len;
406 s->buf_ptr = dst;
407 s->buf_end = dst + len;
408 }
409 }
410
411 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
412 unsigned int len)
413 {
414 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
415 }
416
417 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
418 unsigned int len)
419 {
420 return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
421 }
422
423 unsigned long ffio_get_checksum(AVIOContext *s)
424 {
425 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
426 s->buf_ptr - s->checksum_ptr);
427 s->update_checksum = NULL;
428 return s->checksum;
429 }
430
431 void ffio_init_checksum(AVIOContext *s,
432 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
433 unsigned long checksum)
434 {
435 s->update_checksum = update_checksum;
436 if (s->update_checksum) {
437 s->checksum = checksum;
438 s->checksum_ptr = s->buf_ptr;
439 }
440 }
441
442 /* XXX: put an inline version */
443 int avio_r8(AVIOContext *s)
444 {
445 if (s->buf_ptr >= s->buf_end)
446 fill_buffer(s);
447 if (s->buf_ptr < s->buf_end)
448 return *s->buf_ptr++;
449 return 0;
450 }
451
452 int avio_read(AVIOContext *s, unsigned char *buf, int size)
453 {
454 int len, size1;
455
456 size1 = size;
457 while (size > 0) {
458 len = s->buf_end - s->buf_ptr;
459 if (len > size)
460 len = size;
461 if (len == 0 || s->write_flag) {
462 if(size > s->buffer_size && !s->update_checksum){
463 if(s->read_packet)
464 len = s->read_packet(s->opaque, buf, size);
465 if (len <= 0) {
466 /* do not modify buffer if EOF reached so that a seek back can
467 be done without rereading data */
468 s->eof_reached = 1;
469 if(len<0)
470 s->error= len;
471 break;
472 } else {
473 s->pos += len;
474 size -= len;
475 buf += len;
476 s->buf_ptr = s->buffer;
477 s->buf_end = s->buffer/* + len*/;
478 }
479 } else {
480 fill_buffer(s);
481 len = s->buf_end - s->buf_ptr;
482 if (len == 0)
483 break;
484 }
485 } else {
486 memcpy(buf, s->buf_ptr, len);
487 buf += len;
488 s->buf_ptr += len;
489 size -= len;
490 }
491 }
492 if (size1 == size) {
493 if (s->error) return s->error;
494 if (s->eof_reached) return AVERROR_EOF;
495 }
496 return size1 - size;
497 }
498
499 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
500 {
501 if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
502 *data = s->buf_ptr;
503 s->buf_ptr += size;
504 return size;
505 } else {
506 *data = buf;
507 return avio_read(s, buf, size);
508 }
509 }
510
511 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
512 {
513 int len;
514
515 if (size < 0)
516 return -1;
517
518 if (s->read_packet && s->write_flag) {
519 len = s->read_packet(s->opaque, buf, size);
520 if (len > 0)
521 s->pos += len;
522 return len;
523 }
524
525 len = s->buf_end - s->buf_ptr;
526 if (len == 0) {
527 /* Reset the buf_end pointer to the start of the buffer, to make sure
528 * the fill_buffer call tries to read as much data as fits into the
529 * full buffer, instead of just what space is left after buf_end.
530 * This avoids returning partial packets at the end of the buffer,
531 * for packet based inputs.
532 */
533 s->buf_end = s->buf_ptr = s->buffer;
534 fill_buffer(s);
535 len = s->buf_end - s->buf_ptr;
536 }
537 if (len > size)
538 len = size;
539 memcpy(buf, s->buf_ptr, len);
540 s->buf_ptr += len;
541 if (!len) {
542 if (s->error) return s->error;
543 if (s->eof_reached) return AVERROR_EOF;
544 }
545 return len;
546 }
547
548 unsigned int avio_rl16(AVIOContext *s)
549 {
550 unsigned int val;
551 val = avio_r8(s);
552 val |= avio_r8(s) << 8;
553 return val;
554 }
555
556 unsigned int avio_rl24(AVIOContext *s)
557 {
558 unsigned int val;
559 val = avio_rl16(s);
560 val |= avio_r8(s) << 16;
561 return val;
562 }
563
564 unsigned int avio_rl32(AVIOContext *s)
565 {
566 unsigned int val;
567 val = avio_rl16(s);
568 val |= avio_rl16(s) << 16;
569 return val;
570 }
571
572 uint64_t avio_rl64(AVIOContext *s)
573 {
574 uint64_t val;
575 val = (uint64_t)avio_rl32(s);
576 val |= (uint64_t)avio_rl32(s) << 32;
577 return val;
578 }
579
580 unsigned int avio_rb16(AVIOContext *s)
581 {
582 unsigned int val;
583 val = avio_r8(s) << 8;
584 val |= avio_r8(s);
585 return val;
586 }
587
588 unsigned int avio_rb24(AVIOContext *s)
589 {
590 unsigned int val;
591 val = avio_rb16(s) << 8;
592 val |= avio_r8(s);
593 return val;
594 }
595 unsigned int avio_rb32(AVIOContext *s)
596 {
597 unsigned int val;
598 val = avio_rb16(s) << 16;
599 val |= avio_rb16(s);
600 return val;
601 }
602
603 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
604 {
605 int i = 0;
606 char c;
607
608 do {
609 c = avio_r8(s);
610 if (c && i < maxlen-1)
611 buf[i++] = c;
612 } while (c != '\n' && c);
613
614 buf[i] = 0;
615 return i;
616 }
617
618 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
619 {
620 int i;
621
622 if (buflen <= 0)
623 return AVERROR(EINVAL);
624 // reserve 1 byte for terminating 0
625 buflen = FFMIN(buflen - 1, maxlen);
626 for (i = 0; i < buflen; i++)
627 if (!(buf[i] = avio_r8(s)))
628 return i + 1;
629 buf[i] = 0;
630 for (; i < maxlen; i++)
631 if (!avio_r8(s))
632 return i + 1;
633 return maxlen;
634 }
635
636 #define GET_STR16(type, read) \
637 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
638 {\
639 char* q = buf;\
640 int ret = 0;\
641 if (buflen <= 0) \
642 return AVERROR(EINVAL); \
643 while (ret + 1 < maxlen) {\
644 uint8_t tmp;\
645 uint32_t ch;\
646 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
647 if (!ch)\
648 break;\
649 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
650 }\
651 *q = 0;\
652 return ret;\
653 }\
654
655 GET_STR16(le, avio_rl16)
656 GET_STR16(be, avio_rb16)
657
658 #undef GET_STR16
659
660 uint64_t avio_rb64(AVIOContext *s)
661 {
662 uint64_t val;
663 val = (uint64_t)avio_rb32(s) << 32;
664 val |= (uint64_t)avio_rb32(s);
665 return val;
666 }
667
668 uint64_t ffio_read_varlen(AVIOContext *bc){
669 uint64_t val = 0;
670 int tmp;
671
672 do{
673 tmp = avio_r8(bc);
674 val= (val<<7) + (tmp&127);
675 }while(tmp&128);
676 return val;
677 }
678
679 int ffio_fdopen(AVIOContext **s, URLContext *h)
680 {
681 uint8_t *buffer;
682 int buffer_size, max_packet_size;
683
684 max_packet_size = h->max_packet_size;
685 if (max_packet_size) {
686 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
687 } else {
688 buffer_size = IO_BUFFER_SIZE;
689 }
690 buffer = av_malloc(buffer_size);
691 if (!buffer)
692 return AVERROR(ENOMEM);
693
694 *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
695 ffurl_read, ffurl_write, ffurl_seek);
696 if (!*s) {
697 av_free(buffer);
698 return AVERROR(ENOMEM);
699 }
700 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
701 (*s)->max_packet_size = max_packet_size;
702 if(h->prot) {
703 (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
704 (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
705 }
706 (*s)->av_class = &ffio_url_class;
707 return 0;
708 }
709
710 int ffio_set_buf_size(AVIOContext *s, int buf_size)
711 {
712 uint8_t *buffer;
713 buffer = av_malloc(buf_size);
714 if (!buffer)
715 return AVERROR(ENOMEM);
716
717 av_free(s->buffer);
718 s->buffer = buffer;
719 s->buffer_size = buf_size;
720 s->buf_ptr = buffer;
721 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
722 return 0;
723 }
724
725 static int url_resetbuf(AVIOContext *s, int flags)
726 {
727 assert(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
728
729 if (flags & AVIO_FLAG_WRITE) {
730 s->buf_end = s->buffer + s->buffer_size;
731 s->write_flag = 1;
732 } else {
733 s->buf_end = s->buffer;
734 s->write_flag = 0;
735 }
736 return 0;
737 }
738
739 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
740 {
741 int64_t buffer_start;
742 int buffer_size;
743 int overlap, new_size, alloc_size;
744
745 if (s->write_flag)
746 return AVERROR(EINVAL);
747
748 buffer_size = s->buf_end - s->buffer;
749
750 /* the buffers must touch or overlap */
751 if ((buffer_start = s->pos - buffer_size) > buf_size)
752 return AVERROR(EINVAL);
753
754 overlap = buf_size - buffer_start;
755 new_size = buf_size + buffer_size - overlap;
756
757 alloc_size = FFMAX(s->buffer_size, new_size);
758 if (alloc_size > buf_size)
759 if (!(buf = av_realloc(buf, alloc_size)))
760 return AVERROR(ENOMEM);
761
762 if (new_size > buf_size) {
763 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
764 buf_size = new_size;
765 }
766
767 av_free(s->buffer);
768 s->buf_ptr = s->buffer = buf;
769 s->buffer_size = alloc_size;
770 s->pos = buf_size;
771 s->buf_end = s->buf_ptr + buf_size;
772 s->eof_reached = 0;
773 s->must_flush = 0;
774
775 return 0;
776 }
777
778 int avio_open(AVIOContext **s, const char *filename, int flags)
779 {
780 return avio_open2(s, filename, flags, NULL, NULL);
781 }
782
783 int avio_open2(AVIOContext **s, const char *filename, int flags,
784 const AVIOInterruptCB *int_cb, AVDictionary **options)
785 {
786 URLContext *h;
787 int err;
788
789 err = ffurl_open(&h, filename, flags, int_cb, options);
790 if (err < 0)
791 return err;
792 err = ffio_fdopen(s, h);
793 if (err < 0) {
794 ffurl_close(h);
795 return err;
796 }
797 return 0;
798 }
799
800 int avio_close(AVIOContext *s)
801 {
802 URLContext *h;
803
804 if (!s)
805 return 0;
806
807 avio_flush(s);
808 h = s->opaque;
809 av_freep(&s->buffer);
810 av_free(s);
811 return ffurl_close(h);
812 }
813
814 int avio_closep(AVIOContext **s)
815 {
816 int ret = avio_close(*s);
817 *s = NULL;
818 return ret;
819 }
820
821 int avio_printf(AVIOContext *s, const char *fmt, ...)
822 {
823 va_list ap;
824 char buf[4096];
825 int ret;
826
827 va_start(ap, fmt);
828 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
829 va_end(ap);
830 avio_write(s, buf, strlen(buf));
831 return ret;
832 }
833
834 int avio_pause(AVIOContext *s, int pause)
835 {
836 if (!s->read_pause)
837 return AVERROR(ENOSYS);
838 return s->read_pause(s->opaque, pause);
839 }
840
841 int64_t avio_seek_time(AVIOContext *s, int stream_index,
842 int64_t timestamp, int flags)
843 {
844 URLContext *h = s->opaque;
845 int64_t ret;
846 if (!s->read_seek)
847 return AVERROR(ENOSYS);
848 ret = s->read_seek(h, stream_index, timestamp, flags);
849 if (ret >= 0) {
850 int64_t pos;
851 s->buf_ptr = s->buf_end; // Flush buffer
852 pos = s->seek(h, 0, SEEK_CUR);
853 if (pos >= 0)
854 s->pos = pos;
855 else if (pos != AVERROR(ENOSYS))
856 ret = pos;
857 }
858 return ret;
859 }
860
861 /* output in a dynamic buffer */
862
863 typedef struct DynBuffer {
864 int pos, size, allocated_size;
865 uint8_t *buffer;
866 int io_buffer_size;
867 uint8_t io_buffer[1];
868 } DynBuffer;
869
870 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
871 {
872 DynBuffer *d = opaque;
873 unsigned new_size, new_allocated_size;
874
875 /* reallocate buffer if needed */
876 new_size = d->pos + buf_size;
877 new_allocated_size = d->allocated_size;
878 if (new_size < d->pos || new_size > INT_MAX/2)
879 return -1;
880 while (new_size > new_allocated_size) {
881 if (!new_allocated_size)
882 new_allocated_size = new_size;
883 else
884 new_allocated_size += new_allocated_size / 2 + 1;
885 }
886
887 if (new_allocated_size > d->allocated_size) {
888 int err;
889 if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
890 d->allocated_size = 0;
891 d->size = 0;
892 return err;
893 }
894 d->allocated_size = new_allocated_size;
895 }
896 memcpy(d->buffer + d->pos, buf, buf_size);
897 d->pos = new_size;
898 if (d->pos > d->size)
899 d->size = d->pos;
900 return buf_size;
901 }
902
903 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
904 {
905 unsigned char buf1[4];
906 int ret;
907
908 /* packetized write: output the header */
909 AV_WB32(buf1, buf_size);
910 ret = dyn_buf_write(opaque, buf1, 4);
911 if (ret < 0)
912 return ret;
913
914 /* then the data */
915 return dyn_buf_write(opaque, buf, buf_size);
916 }
917
918 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
919 {
920 DynBuffer *d = opaque;
921
922 if (whence == SEEK_CUR)
923 offset += d->pos;
924 else if (whence == SEEK_END)
925 offset += d->size;
926 if (offset < 0 || offset > 0x7fffffffLL)
927 return -1;
928 d->pos = offset;
929 return 0;
930 }
931
932 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
933 {
934 DynBuffer *d;
935 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
936
937 if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
938 return -1;
939 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
940 if (!d)
941 return AVERROR(ENOMEM);
942 d->io_buffer_size = io_buffer_size;
943 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
944 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
945 max_packet_size ? NULL : dyn_buf_seek);
946 if(!*s) {
947 av_free(d);
948 return AVERROR(ENOMEM);
949 }
950 (*s)->max_packet_size = max_packet_size;
951 return 0;
952 }
953
954 int avio_open_dyn_buf(AVIOContext **s)
955 {
956 return url_open_dyn_buf_internal(s, 0);
957 }
958
959 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
960 {
961 if (max_packet_size <= 0)
962 return -1;
963 return url_open_dyn_buf_internal(s, max_packet_size);
964 }
965
966 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
967 {
968 DynBuffer *d = s->opaque;
969 int size;
970 static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
971 int padding = 0;
972
973 if (!s) {
974 *pbuffer = NULL;
975 return 0;
976 }
977
978 /* don't attempt to pad fixed-size packet buffers */
979 if (!s->max_packet_size) {
980 avio_write(s, padbuf, sizeof(padbuf));
981 padding = FF_INPUT_BUFFER_PADDING_SIZE;
982 }
983
984 avio_flush(s);
985
986 *pbuffer = d->buffer;
987 size = d->size;
988 av_free(d);
989 av_free(s);
990 return size - padding;
991 }
992
993 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
994 {
995 DynBuffer *d = opaque;
996
997 d->pos += buf_size;
998 if (d->pos > d->size)
999 d->size = d->pos;
1000 return buf_size;
1001 }
1002
1003 int ffio_open_null_buf(AVIOContext **s)
1004 {
1005 int ret = url_open_dyn_buf_internal(s, 0);
1006 if (ret >= 0) {
1007 AVIOContext *pb = *s;
1008 pb->write_packet = null_buf_write;
1009 }
1010 return ret;
1011 }
1012
1013 int ffio_close_null_buf(AVIOContext *s)
1014 {
1015 DynBuffer *d = s->opaque;
1016 int size;
1017
1018 avio_flush(s);
1019
1020 size = d->size;
1021 av_free(d);
1022 av_free(s);
1023 return size;
1024 }