0024f9e8b22e093190c5fdb79d4995f8bedc7b1d
[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 ffio_get_checksum(AVIOContext *s)
418 {
419 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
420 s->buf_ptr - s->checksum_ptr);
421 s->update_checksum = NULL;
422 return s->checksum;
423 }
424
425 void ffio_init_checksum(AVIOContext *s,
426 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
427 unsigned long checksum)
428 {
429 s->update_checksum = update_checksum;
430 if (s->update_checksum) {
431 s->checksum = checksum;
432 s->checksum_ptr = s->buf_ptr;
433 }
434 }
435
436 /* XXX: put an inline version */
437 int avio_r8(AVIOContext *s)
438 {
439 if (s->buf_ptr >= s->buf_end)
440 fill_buffer(s);
441 if (s->buf_ptr < s->buf_end)
442 return *s->buf_ptr++;
443 return 0;
444 }
445
446 int avio_read(AVIOContext *s, unsigned char *buf, int size)
447 {
448 int len, size1;
449
450 size1 = size;
451 while (size > 0) {
452 len = s->buf_end - s->buf_ptr;
453 if (len > size)
454 len = size;
455 if (len == 0 || s->write_flag) {
456 if(size > s->buffer_size && !s->update_checksum){
457 if(s->read_packet)
458 len = s->read_packet(s->opaque, buf, size);
459 if (len <= 0) {
460 /* do not modify buffer if EOF reached so that a seek back can
461 be done without rereading data */
462 s->eof_reached = 1;
463 if(len<0)
464 s->error= len;
465 break;
466 } else {
467 s->pos += len;
468 size -= len;
469 buf += len;
470 s->buf_ptr = s->buffer;
471 s->buf_end = s->buffer/* + len*/;
472 }
473 } else {
474 fill_buffer(s);
475 len = s->buf_end - s->buf_ptr;
476 if (len == 0)
477 break;
478 }
479 } else {
480 memcpy(buf, s->buf_ptr, len);
481 buf += len;
482 s->buf_ptr += len;
483 size -= len;
484 }
485 }
486 if (size1 == size) {
487 if (s->error) return s->error;
488 if (s->eof_reached) return AVERROR_EOF;
489 }
490 return size1 - size;
491 }
492
493 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
494 {
495 if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
496 *data = s->buf_ptr;
497 s->buf_ptr += size;
498 return size;
499 } else {
500 *data = buf;
501 return avio_read(s, buf, size);
502 }
503 }
504
505 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
506 {
507 int len;
508
509 if (size < 0)
510 return -1;
511
512 if (s->read_packet && s->write_flag) {
513 len = s->read_packet(s->opaque, buf, size);
514 if (len > 0)
515 s->pos += len;
516 return len;
517 }
518
519 len = s->buf_end - s->buf_ptr;
520 if (len == 0) {
521 /* Reset the buf_end pointer to the start of the buffer, to make sure
522 * the fill_buffer call tries to read as much data as fits into the
523 * full buffer, instead of just what space is left after buf_end.
524 * This avoids returning partial packets at the end of the buffer,
525 * for packet based inputs.
526 */
527 s->buf_end = s->buf_ptr = s->buffer;
528 fill_buffer(s);
529 len = s->buf_end - s->buf_ptr;
530 }
531 if (len > size)
532 len = size;
533 memcpy(buf, s->buf_ptr, len);
534 s->buf_ptr += len;
535 if (!len) {
536 if (s->error) return s->error;
537 if (s->eof_reached) return AVERROR_EOF;
538 }
539 return len;
540 }
541
542 unsigned int avio_rl16(AVIOContext *s)
543 {
544 unsigned int val;
545 val = avio_r8(s);
546 val |= avio_r8(s) << 8;
547 return val;
548 }
549
550 unsigned int avio_rl24(AVIOContext *s)
551 {
552 unsigned int val;
553 val = avio_rl16(s);
554 val |= avio_r8(s) << 16;
555 return val;
556 }
557
558 unsigned int avio_rl32(AVIOContext *s)
559 {
560 unsigned int val;
561 val = avio_rl16(s);
562 val |= avio_rl16(s) << 16;
563 return val;
564 }
565
566 uint64_t avio_rl64(AVIOContext *s)
567 {
568 uint64_t val;
569 val = (uint64_t)avio_rl32(s);
570 val |= (uint64_t)avio_rl32(s) << 32;
571 return val;
572 }
573
574 unsigned int avio_rb16(AVIOContext *s)
575 {
576 unsigned int val;
577 val = avio_r8(s) << 8;
578 val |= avio_r8(s);
579 return val;
580 }
581
582 unsigned int avio_rb24(AVIOContext *s)
583 {
584 unsigned int val;
585 val = avio_rb16(s) << 8;
586 val |= avio_r8(s);
587 return val;
588 }
589 unsigned int avio_rb32(AVIOContext *s)
590 {
591 unsigned int val;
592 val = avio_rb16(s) << 16;
593 val |= avio_rb16(s);
594 return val;
595 }
596
597 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
598 {
599 int i = 0;
600 char c;
601
602 do {
603 c = avio_r8(s);
604 if (c && i < maxlen-1)
605 buf[i++] = c;
606 } while (c != '\n' && c);
607
608 buf[i] = 0;
609 return i;
610 }
611
612 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
613 {
614 int i;
615
616 if (buflen <= 0)
617 return AVERROR(EINVAL);
618 // reserve 1 byte for terminating 0
619 buflen = FFMIN(buflen - 1, maxlen);
620 for (i = 0; i < buflen; i++)
621 if (!(buf[i] = avio_r8(s)))
622 return i + 1;
623 buf[i] = 0;
624 for (; i < maxlen; i++)
625 if (!avio_r8(s))
626 return i + 1;
627 return maxlen;
628 }
629
630 #define GET_STR16(type, read) \
631 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
632 {\
633 char* q = buf;\
634 int ret = 0;\
635 if (buflen <= 0) \
636 return AVERROR(EINVAL); \
637 while (ret + 1 < maxlen) {\
638 uint8_t tmp;\
639 uint32_t ch;\
640 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
641 if (!ch)\
642 break;\
643 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
644 }\
645 *q = 0;\
646 return ret;\
647 }\
648
649 GET_STR16(le, avio_rl16)
650 GET_STR16(be, avio_rb16)
651
652 #undef GET_STR16
653
654 uint64_t avio_rb64(AVIOContext *s)
655 {
656 uint64_t val;
657 val = (uint64_t)avio_rb32(s) << 32;
658 val |= (uint64_t)avio_rb32(s);
659 return val;
660 }
661
662 uint64_t ffio_read_varlen(AVIOContext *bc){
663 uint64_t val = 0;
664 int tmp;
665
666 do{
667 tmp = avio_r8(bc);
668 val= (val<<7) + (tmp&127);
669 }while(tmp&128);
670 return val;
671 }
672
673 int ffio_fdopen(AVIOContext **s, URLContext *h)
674 {
675 uint8_t *buffer;
676 int buffer_size, max_packet_size;
677
678 max_packet_size = h->max_packet_size;
679 if (max_packet_size) {
680 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
681 } else {
682 buffer_size = IO_BUFFER_SIZE;
683 }
684 buffer = av_malloc(buffer_size);
685 if (!buffer)
686 return AVERROR(ENOMEM);
687
688 *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
689 ffurl_read, ffurl_write, ffurl_seek);
690 if (!*s) {
691 av_free(buffer);
692 return AVERROR(ENOMEM);
693 }
694 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
695 (*s)->max_packet_size = max_packet_size;
696 if(h->prot) {
697 (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
698 (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
699 }
700 (*s)->av_class = &ffio_url_class;
701 return 0;
702 }
703
704 int ffio_set_buf_size(AVIOContext *s, int buf_size)
705 {
706 uint8_t *buffer;
707 buffer = av_malloc(buf_size);
708 if (!buffer)
709 return AVERROR(ENOMEM);
710
711 av_free(s->buffer);
712 s->buffer = buffer;
713 s->buffer_size = buf_size;
714 s->buf_ptr = buffer;
715 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
716 return 0;
717 }
718
719 static int url_resetbuf(AVIOContext *s, int flags)
720 {
721 assert(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
722
723 if (flags & AVIO_FLAG_WRITE) {
724 s->buf_end = s->buffer + s->buffer_size;
725 s->write_flag = 1;
726 } else {
727 s->buf_end = s->buffer;
728 s->write_flag = 0;
729 }
730 return 0;
731 }
732
733 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
734 {
735 int64_t buffer_start;
736 int buffer_size;
737 int overlap, new_size, alloc_size;
738
739 if (s->write_flag)
740 return AVERROR(EINVAL);
741
742 buffer_size = s->buf_end - s->buffer;
743
744 /* the buffers must touch or overlap */
745 if ((buffer_start = s->pos - buffer_size) > buf_size)
746 return AVERROR(EINVAL);
747
748 overlap = buf_size - buffer_start;
749 new_size = buf_size + buffer_size - overlap;
750
751 alloc_size = FFMAX(s->buffer_size, new_size);
752 if (alloc_size > buf_size)
753 if (!(buf = av_realloc(buf, alloc_size)))
754 return AVERROR(ENOMEM);
755
756 if (new_size > buf_size) {
757 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
758 buf_size = new_size;
759 }
760
761 av_free(s->buffer);
762 s->buf_ptr = s->buffer = buf;
763 s->buffer_size = alloc_size;
764 s->pos = buf_size;
765 s->buf_end = s->buf_ptr + buf_size;
766 s->eof_reached = 0;
767 s->must_flush = 0;
768
769 return 0;
770 }
771
772 int avio_open(AVIOContext **s, const char *filename, int flags)
773 {
774 return avio_open2(s, filename, flags, NULL, NULL);
775 }
776
777 int avio_open2(AVIOContext **s, const char *filename, int flags,
778 const AVIOInterruptCB *int_cb, AVDictionary **options)
779 {
780 URLContext *h;
781 int err;
782
783 err = ffurl_open(&h, filename, flags, int_cb, options);
784 if (err < 0)
785 return err;
786 err = ffio_fdopen(s, h);
787 if (err < 0) {
788 ffurl_close(h);
789 return err;
790 }
791 return 0;
792 }
793
794 int avio_close(AVIOContext *s)
795 {
796 URLContext *h;
797
798 if (!s)
799 return 0;
800
801 avio_flush(s);
802 h = s->opaque;
803 av_freep(&s->buffer);
804 av_free(s);
805 return ffurl_close(h);
806 }
807
808 int avio_closep(AVIOContext **s)
809 {
810 int ret = avio_close(*s);
811 *s = NULL;
812 return ret;
813 }
814
815 int avio_printf(AVIOContext *s, const char *fmt, ...)
816 {
817 va_list ap;
818 char buf[4096];
819 int ret;
820
821 va_start(ap, fmt);
822 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
823 va_end(ap);
824 avio_write(s, buf, strlen(buf));
825 return ret;
826 }
827
828 int avio_pause(AVIOContext *s, int pause)
829 {
830 if (!s->read_pause)
831 return AVERROR(ENOSYS);
832 return s->read_pause(s->opaque, pause);
833 }
834
835 int64_t avio_seek_time(AVIOContext *s, int stream_index,
836 int64_t timestamp, int flags)
837 {
838 URLContext *h = s->opaque;
839 int64_t ret;
840 if (!s->read_seek)
841 return AVERROR(ENOSYS);
842 ret = s->read_seek(h, stream_index, timestamp, flags);
843 if (ret >= 0) {
844 int64_t pos;
845 s->buf_ptr = s->buf_end; // Flush buffer
846 pos = s->seek(h, 0, SEEK_CUR);
847 if (pos >= 0)
848 s->pos = pos;
849 else if (pos != AVERROR(ENOSYS))
850 ret = pos;
851 }
852 return ret;
853 }
854
855 /* output in a dynamic buffer */
856
857 typedef struct DynBuffer {
858 int pos, size, allocated_size;
859 uint8_t *buffer;
860 int io_buffer_size;
861 uint8_t io_buffer[1];
862 } DynBuffer;
863
864 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
865 {
866 DynBuffer *d = opaque;
867 unsigned new_size, new_allocated_size;
868
869 /* reallocate buffer if needed */
870 new_size = d->pos + buf_size;
871 new_allocated_size = d->allocated_size;
872 if (new_size < d->pos || new_size > INT_MAX/2)
873 return -1;
874 while (new_size > new_allocated_size) {
875 if (!new_allocated_size)
876 new_allocated_size = new_size;
877 else
878 new_allocated_size += new_allocated_size / 2 + 1;
879 }
880
881 if (new_allocated_size > d->allocated_size) {
882 int err;
883 if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
884 d->allocated_size = 0;
885 d->size = 0;
886 return err;
887 }
888 d->allocated_size = new_allocated_size;
889 }
890 memcpy(d->buffer + d->pos, buf, buf_size);
891 d->pos = new_size;
892 if (d->pos > d->size)
893 d->size = d->pos;
894 return buf_size;
895 }
896
897 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
898 {
899 unsigned char buf1[4];
900 int ret;
901
902 /* packetized write: output the header */
903 AV_WB32(buf1, buf_size);
904 ret = dyn_buf_write(opaque, buf1, 4);
905 if (ret < 0)
906 return ret;
907
908 /* then the data */
909 return dyn_buf_write(opaque, buf, buf_size);
910 }
911
912 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
913 {
914 DynBuffer *d = opaque;
915
916 if (whence == SEEK_CUR)
917 offset += d->pos;
918 else if (whence == SEEK_END)
919 offset += d->size;
920 if (offset < 0 || offset > 0x7fffffffLL)
921 return -1;
922 d->pos = offset;
923 return 0;
924 }
925
926 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
927 {
928 DynBuffer *d;
929 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
930
931 if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
932 return -1;
933 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
934 if (!d)
935 return AVERROR(ENOMEM);
936 d->io_buffer_size = io_buffer_size;
937 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
938 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
939 max_packet_size ? NULL : dyn_buf_seek);
940 if(!*s) {
941 av_free(d);
942 return AVERROR(ENOMEM);
943 }
944 (*s)->max_packet_size = max_packet_size;
945 return 0;
946 }
947
948 int avio_open_dyn_buf(AVIOContext **s)
949 {
950 return url_open_dyn_buf_internal(s, 0);
951 }
952
953 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
954 {
955 if (max_packet_size <= 0)
956 return -1;
957 return url_open_dyn_buf_internal(s, max_packet_size);
958 }
959
960 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
961 {
962 DynBuffer *d = s->opaque;
963 int size;
964 static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
965 int padding = 0;
966
967 if (!s) {
968 *pbuffer = NULL;
969 return 0;
970 }
971
972 /* don't attempt to pad fixed-size packet buffers */
973 if (!s->max_packet_size) {
974 avio_write(s, padbuf, sizeof(padbuf));
975 padding = FF_INPUT_BUFFER_PADDING_SIZE;
976 }
977
978 avio_flush(s);
979
980 *pbuffer = d->buffer;
981 size = d->size;
982 av_free(d);
983 av_free(s);
984 return size - padding;
985 }
986
987 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
988 {
989 DynBuffer *d = opaque;
990
991 d->pos += buf_size;
992 if (d->pos > d->size)
993 d->size = d->pos;
994 return buf_size;
995 }
996
997 int ffio_open_null_buf(AVIOContext **s)
998 {
999 int ret = url_open_dyn_buf_internal(s, 0);
1000 if (ret >= 0) {
1001 AVIOContext *pb = *s;
1002 pb->write_packet = null_buf_write;
1003 }
1004 return ret;
1005 }
1006
1007 int ffio_close_null_buf(AVIOContext *s)
1008 {
1009 DynBuffer *d = s->opaque;
1010 int size;
1011
1012 avio_flush(s);
1013
1014 size = d->size;
1015 av_free(d);
1016 av_free(s);
1017 return size;
1018 }