3 * Copyright (c) 2000,2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
12 * FFmpeg 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/bprint.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/log.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/avassert.h"
31 #include "avio_internal.h"
36 #define IO_BUFFER_SIZE 32768
39 * Do seeks within this distance ahead of the current buffer by skipping
40 * data instead of calling the protocol seek function, for seekable
43 #define SHORT_SEEK_THRESHOLD 4096
45 static void *ff_avio_child_next(void *obj, void *prev)
48 return prev ? NULL : s->opaque;
51 static const AVClass *ff_avio_child_class_next(const AVClass *prev)
53 return prev ? NULL : &ffurl_context_class;
56 static const AVOption ff_avio_options[] = {
60 const AVClass ff_avio_class = {
61 .class_name = "AVIOContext",
62 .item_name = av_default_item_name,
63 .version = LIBAVUTIL_VERSION_INT,
64 .option = ff_avio_options,
65 .child_next = ff_avio_child_next,
66 .child_class_next = ff_avio_child_class_next,
69 static void fill_buffer(AVIOContext *s);
70 static int url_resetbuf(AVIOContext *s, int flags);
72 int ffio_init_context(AVIOContext *s,
73 unsigned char *buffer,
77 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
78 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
79 int64_t (*seek)(void *opaque, int64_t offset, int whence))
83 s->buffer_size = buffer_size;
88 url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
90 s->write_packet = write_packet;
91 s->read_packet = read_packet;
97 s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
98 s->max_packet_size = 0;
99 s->update_checksum = NULL;
100 s->short_seek_threshold = SHORT_SEEK_THRESHOLD;
102 if (!read_packet && !write_flag) {
103 s->pos = buffer_size;
104 s->buf_end = s->buffer + buffer_size;
106 s->read_pause = NULL;
112 AVIOContext *avio_alloc_context(
113 unsigned char *buffer,
117 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
118 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
119 int64_t (*seek)(void *opaque, int64_t offset, int whence))
121 AVIOContext *s = av_mallocz(sizeof(AVIOContext));
124 ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
125 read_packet, write_packet, seek);
129 static void writeout(AVIOContext *s, const uint8_t *data, int len)
131 if (s->write_packet && !s->error) {
132 int ret = s->write_packet(s->opaque, (uint8_t *)data, len);
137 s->writeout_count ++;
141 static void flush_buffer(AVIOContext *s)
143 if (s->write_flag && s->buf_ptr > s->buffer) {
144 writeout(s, s->buffer, s->buf_ptr - s->buffer);
145 if (s->update_checksum) {
146 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
147 s->buf_ptr - s->checksum_ptr);
148 s->checksum_ptr = s->buffer;
151 s->buf_ptr = s->buffer;
153 s->buf_end = s->buffer;
156 void avio_w8(AVIOContext *s, int b)
158 av_assert2(b>=-128 && b<=255);
160 if (s->buf_ptr >= s->buf_end)
164 void ffio_fill(AVIOContext *s, int b, int count)
167 int len = FFMIN(s->buf_end - s->buf_ptr, count);
168 memset(s->buf_ptr, b, len);
171 if (s->buf_ptr >= s->buf_end)
178 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
180 if (s->direct && !s->update_checksum) {
182 writeout(s, buf, size);
186 int len = FFMIN(s->buf_end - s->buf_ptr, size);
187 memcpy(s->buf_ptr, buf, len);
190 if (s->buf_ptr >= s->buf_end)
198 void avio_flush(AVIOContext *s)
204 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
208 int force = whence & AVSEEK_FORCE;
210 whence &= ~AVSEEK_FORCE;
213 return AVERROR(EINVAL);
215 buffer_size = s->buf_end - s->buffer;
216 // pos is the absolute position that the beginning of s->buffer corresponds to in the file
217 pos = s->pos - (s->write_flag ? 0 : buffer_size);
219 if (whence != SEEK_CUR && whence != SEEK_SET)
220 return AVERROR(EINVAL);
222 if (whence == SEEK_CUR) {
223 offset1 = pos + (s->buf_ptr - s->buffer);
229 return AVERROR(EINVAL);
231 offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
232 if (!s->must_flush && (!s->direct || !s->seek) &&
233 offset1 >= 0 && offset1 <= buffer_size - s->write_flag) {
234 /* can do the seek inside the buffer */
235 s->buf_ptr = s->buffer + offset1;
236 } else if ((!s->seekable ||
237 offset1 <= buffer_size + s->short_seek_threshold) &&
238 !s->write_flag && offset1 >= 0 &&
239 (!s->direct || !s->seek) &&
240 (whence != SEEK_END || force)) {
241 while(s->pos < offset && !s->eof_reached)
245 s->buf_ptr = s->buf_end - (s->pos - offset);
246 } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
249 pos -= FFMIN(buffer_size>>1, pos);
250 if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
253 s->buf_ptr = s->buffer;
257 return avio_seek(s, offset, SEEK_SET | force);
265 return AVERROR(EPIPE);
266 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
270 s->buf_end = s->buffer;
271 s->buf_ptr = s->buffer;
278 int64_t avio_skip(AVIOContext *s, int64_t offset)
280 return avio_seek(s, offset, SEEK_CUR);
283 int64_t avio_size(AVIOContext *s)
288 return AVERROR(EINVAL);
291 return AVERROR(ENOSYS);
292 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
294 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
297 s->seek(s->opaque, s->pos, SEEK_SET);
302 int avio_feof(AVIOContext *s)
310 return s->eof_reached;
314 int url_feof(AVIOContext *s)
320 void avio_wl32(AVIOContext *s, unsigned int val)
322 avio_w8(s, (uint8_t) val );
323 avio_w8(s, (uint8_t)(val >> 8 ));
324 avio_w8(s, (uint8_t)(val >> 16));
325 avio_w8(s, val >> 24 );
328 void avio_wb32(AVIOContext *s, unsigned int val)
330 avio_w8(s, val >> 24 );
331 avio_w8(s, (uint8_t)(val >> 16));
332 avio_w8(s, (uint8_t)(val >> 8 ));
333 avio_w8(s, (uint8_t) val );
336 int avio_put_str(AVIOContext *s, const char *str)
341 avio_write(s, (const unsigned char *) str, len);
347 static inline int put_str16(AVIOContext *s, const char *str, const int be)
349 const uint8_t *q = str;
357 GET_UTF8(ch, *q++, goto invalid;)
358 PUT_UTF16(ch, tmp, be ? avio_wb16(s, tmp) : avio_wl16(s, tmp);
362 av_log(s, AV_LOG_ERROR, "Invaid UTF8 sequence in avio_put_str16%s\n", be ? "be" : "le");
363 err = AVERROR(EINVAL);
375 #define PUT_STR16(type, big_endian) \
376 int avio_put_str16 ## type(AVIOContext *s, const char *str) \
378 return put_str16(s, str, big_endian); \
386 int ff_get_v_length(uint64_t val)
396 void ff_put_v(AVIOContext *bc, uint64_t val)
398 int i = ff_get_v_length(val);
401 avio_w8(bc, 128 | (uint8_t)(val >> (7*i)));
403 avio_w8(bc, val & 127);
406 void avio_wl64(AVIOContext *s, uint64_t val)
408 avio_wl32(s, (uint32_t)(val & 0xffffffff));
409 avio_wl32(s, (uint32_t)(val >> 32));
412 void avio_wb64(AVIOContext *s, uint64_t val)
414 avio_wb32(s, (uint32_t)(val >> 32));
415 avio_wb32(s, (uint32_t)(val & 0xffffffff));
418 void avio_wl16(AVIOContext *s, unsigned int val)
420 avio_w8(s, (uint8_t)val);
421 avio_w8(s, (int)val >> 8);
424 void avio_wb16(AVIOContext *s, unsigned int val)
426 avio_w8(s, (int)val >> 8);
427 avio_w8(s, (uint8_t)val);
430 void avio_wl24(AVIOContext *s, unsigned int val)
432 avio_wl16(s, val & 0xffff);
433 avio_w8(s, (int)val >> 16);
436 void avio_wb24(AVIOContext *s, unsigned int val)
438 avio_wb16(s, (int)val >> 8);
439 avio_w8(s, (uint8_t)val);
444 static void fill_buffer(AVIOContext *s)
446 int max_buffer_size = s->max_packet_size ?
447 s->max_packet_size : IO_BUFFER_SIZE;
448 uint8_t *dst = s->buf_end - s->buffer + max_buffer_size < s->buffer_size ?
449 s->buf_end : s->buffer;
450 int len = s->buffer_size - (dst - s->buffer);
452 /* can't fill the buffer without read_packet, just set EOF if appropriate */
453 if (!s->read_packet && s->buf_ptr >= s->buf_end)
456 /* no need to do anything if EOF already reached */
460 if (s->update_checksum && dst == s->buffer) {
461 if (s->buf_end > s->checksum_ptr)
462 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
463 s->buf_end - s->checksum_ptr);
464 s->checksum_ptr = s->buffer;
467 /* make buffer smaller in case it ended up large after probing */
468 if (s->read_packet && s->orig_buffer_size && s->buffer_size > s->orig_buffer_size) {
469 if (dst == s->buffer) {
470 int ret = ffio_set_buf_size(s, s->orig_buffer_size);
472 av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
474 s->checksum_ptr = dst = s->buffer;
476 av_assert0(len >= s->orig_buffer_size);
477 len = s->orig_buffer_size;
481 len = s->read_packet(s->opaque, dst, len);
485 /* do not modify buffer if EOF reached so that a seek back can
486 be done without rereading data */
493 s->buf_end = dst + len;
494 s->bytes_read += len;
498 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
501 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
504 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
507 return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
510 unsigned long ffio_get_checksum(AVIOContext *s)
512 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
513 s->buf_ptr - s->checksum_ptr);
514 s->update_checksum = NULL;
518 void ffio_init_checksum(AVIOContext *s,
519 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
520 unsigned long checksum)
522 s->update_checksum = update_checksum;
523 if (s->update_checksum) {
524 s->checksum = checksum;
525 s->checksum_ptr = s->buf_ptr;
529 /* XXX: put an inline version */
530 int avio_r8(AVIOContext *s)
532 if (s->buf_ptr >= s->buf_end)
534 if (s->buf_ptr < s->buf_end)
535 return *s->buf_ptr++;
539 int avio_read(AVIOContext *s, unsigned char *buf, int size)
545 len = FFMIN(s->buf_end - s->buf_ptr, size);
546 if (len == 0 || s->write_flag) {
547 if((s->direct || size > s->buffer_size) && !s->update_checksum) {
548 // bypass the buffer and read data directly into buf
550 len = s->read_packet(s->opaque, buf, size);
553 /* do not modify buffer if EOF reached so that a seek back can
554 be done without rereading data */
561 s->bytes_read += len;
565 s->buf_ptr = s->buffer;
566 s->buf_end = s->buffer/* + len*/;
570 len = s->buf_end - s->buf_ptr;
575 memcpy(buf, s->buf_ptr, len);
582 if (s->error) return s->error;
583 if (avio_feof(s)) return AVERROR_EOF;
588 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
590 int ret = avio_read(s, buf, size);
592 return AVERROR_INVALIDDATA;
596 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
598 if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
604 return avio_read(s, buf, size);
608 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
615 if (s->read_packet && s->write_flag) {
616 len = s->read_packet(s->opaque, buf, size);
622 len = s->buf_end - s->buf_ptr;
624 /* Reset the buf_end pointer to the start of the buffer, to make sure
625 * the fill_buffer call tries to read as much data as fits into the
626 * full buffer, instead of just what space is left after buf_end.
627 * This avoids returning partial packets at the end of the buffer,
628 * for packet based inputs.
630 s->buf_end = s->buf_ptr = s->buffer;
632 len = s->buf_end - s->buf_ptr;
636 memcpy(buf, s->buf_ptr, len);
639 if (s->error) return s->error;
640 if (avio_feof(s)) return AVERROR_EOF;
645 unsigned int avio_rl16(AVIOContext *s)
649 val |= avio_r8(s) << 8;
653 unsigned int avio_rl24(AVIOContext *s)
657 val |= avio_r8(s) << 16;
661 unsigned int avio_rl32(AVIOContext *s)
665 val |= avio_rl16(s) << 16;
669 uint64_t avio_rl64(AVIOContext *s)
672 val = (uint64_t)avio_rl32(s);
673 val |= (uint64_t)avio_rl32(s) << 32;
677 unsigned int avio_rb16(AVIOContext *s)
680 val = avio_r8(s) << 8;
685 unsigned int avio_rb24(AVIOContext *s)
688 val = avio_rb16(s) << 8;
692 unsigned int avio_rb32(AVIOContext *s)
695 val = avio_rb16(s) << 16;
700 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
707 if (c && i < maxlen-1)
709 } while (c != '\n' && c != '\r' && c);
710 if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
717 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
722 return AVERROR(EINVAL);
723 // reserve 1 byte for terminating 0
724 buflen = FFMIN(buflen - 1, maxlen);
725 for (i = 0; i < buflen; i++)
726 if (!(buf[i] = avio_r8(s)))
729 for (; i < maxlen; i++)
735 #define GET_STR16(type, read) \
736 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
741 return AVERROR(EINVAL); \
742 while (ret + 1 < maxlen) {\
745 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
748 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
754 GET_STR16(le, avio_rl16)
755 GET_STR16(be, avio_rb16)
759 uint64_t avio_rb64(AVIOContext *s)
762 val = (uint64_t)avio_rb32(s) << 32;
763 val |= (uint64_t)avio_rb32(s);
767 uint64_t ffio_read_varlen(AVIOContext *bc){
773 val= (val<<7) + (tmp&127);
778 int ffio_fdopen(AVIOContext **s, URLContext *h)
781 int buffer_size, max_packet_size;
783 max_packet_size = h->max_packet_size;
784 if (max_packet_size) {
785 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
787 buffer_size = IO_BUFFER_SIZE;
789 buffer = av_malloc(buffer_size);
791 return AVERROR(ENOMEM);
793 *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
794 (int (*)(void *, uint8_t *, int)) ffurl_read,
795 (int (*)(void *, uint8_t *, int)) ffurl_write,
796 (int64_t (*)(void *, int64_t, int)) ffurl_seek);
799 return AVERROR(ENOMEM);
801 (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
802 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
803 (*s)->max_packet_size = max_packet_size;
805 (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
806 (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
808 (*s)->av_class = &ff_avio_class;
812 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
815 int max_buffer_size = s->max_packet_size ?
816 s->max_packet_size : IO_BUFFER_SIZE;
817 int filled = s->buf_end - s->buffer;
818 ptrdiff_t checksum_ptr_offset = s->checksum_ptr ? s->checksum_ptr - s->buffer : -1;
820 buf_size += s->buf_ptr - s->buffer + max_buffer_size;
822 if (buf_size < filled || s->seekable || !s->read_packet)
824 av_assert0(!s->write_flag);
826 buffer = av_malloc(buf_size);
828 return AVERROR(ENOMEM);
830 memcpy(buffer, s->buffer, filled);
832 s->buf_ptr = buffer + (s->buf_ptr - s->buffer);
833 s->buf_end = buffer + (s->buf_end - s->buffer);
835 s->buffer_size = buf_size;
836 if (checksum_ptr_offset >= 0)
837 s->checksum_ptr = s->buffer + checksum_ptr_offset;
841 int ffio_set_buf_size(AVIOContext *s, int buf_size)
844 buffer = av_malloc(buf_size);
846 return AVERROR(ENOMEM);
850 s->orig_buffer_size =
851 s->buffer_size = buf_size;
853 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
857 static int url_resetbuf(AVIOContext *s, int flags)
859 av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
861 if (flags & AVIO_FLAG_WRITE) {
862 s->buf_end = s->buffer + s->buffer_size;
865 s->buf_end = s->buffer;
871 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
873 int64_t buffer_start;
875 int overlap, new_size, alloc_size;
876 uint8_t *buf = *bufp;
880 return AVERROR(EINVAL);
883 buffer_size = s->buf_end - s->buffer;
885 /* the buffers must touch or overlap */
886 if ((buffer_start = s->pos - buffer_size) > buf_size) {
888 return AVERROR(EINVAL);
891 overlap = buf_size - buffer_start;
892 new_size = buf_size + buffer_size - overlap;
894 alloc_size = FFMAX(s->buffer_size, new_size);
895 if (alloc_size > buf_size)
896 if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
897 return AVERROR(ENOMEM);
899 if (new_size > buf_size) {
900 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
905 s->buf_ptr = s->buffer = buf;
906 s->buffer_size = alloc_size;
908 s->buf_end = s->buf_ptr + buf_size;
915 int avio_open(AVIOContext **s, const char *filename, int flags)
917 return avio_open2(s, filename, flags, NULL, NULL);
920 int avio_open2(AVIOContext **s, const char *filename, int flags,
921 const AVIOInterruptCB *int_cb, AVDictionary **options)
926 err = ffurl_open(&h, filename, flags, int_cb, options);
929 err = ffio_fdopen(s, h);
937 int ffio_open2_wrapper(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags,
938 const AVIOInterruptCB *int_cb, AVDictionary **options)
940 return avio_open2(pb, url, flags, int_cb, options);
943 int avio_close(AVIOContext *s)
952 av_freep(&s->buffer);
954 av_log(s, AV_LOG_DEBUG, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count);
956 av_log(s, AV_LOG_DEBUG, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
958 return ffurl_close(h);
961 int avio_closep(AVIOContext **s)
963 int ret = avio_close(*s);
968 int avio_printf(AVIOContext *s, const char *fmt, ...)
971 char buf[4096]; /* update doc entry in avio.h if changed */
975 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
977 avio_write(s, buf, strlen(buf));
981 int avio_pause(AVIOContext *s, int pause)
984 return AVERROR(ENOSYS);
985 return s->read_pause(s->opaque, pause);
988 int64_t avio_seek_time(AVIOContext *s, int stream_index,
989 int64_t timestamp, int flags)
991 URLContext *h = s->opaque;
994 return AVERROR(ENOSYS);
995 ret = s->read_seek(h, stream_index, timestamp, flags);
998 s->buf_ptr = s->buf_end; // Flush buffer
999 pos = s->seek(h, 0, SEEK_CUR);
1002 else if (pos != AVERROR(ENOSYS))
1008 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1013 ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1014 if (ret == AVERROR_EOF)
1018 av_bprint_append_data(pb, buf, ret);
1019 if (!av_bprint_is_complete(pb))
1020 return AVERROR(ENOMEM);
1026 int avio_accept(AVIOContext *s, AVIOContext **c)
1029 URLContext *sc = s->opaque;
1030 URLContext *cc = NULL;
1031 ret = ffurl_accept(sc, &cc);
1034 return ffio_fdopen(c, cc);
1037 int avio_handshake(AVIOContext *c)
1039 URLContext *cc = c->opaque;
1040 return ffurl_handshake(cc);
1043 /* output in a dynamic buffer */
1045 typedef struct DynBuffer {
1046 int pos, size, allocated_size;
1049 uint8_t io_buffer[1];
1052 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1054 DynBuffer *d = opaque;
1055 unsigned new_size, new_allocated_size;
1057 /* reallocate buffer if needed */
1058 new_size = d->pos + buf_size;
1059 new_allocated_size = d->allocated_size;
1060 if (new_size < d->pos || new_size > INT_MAX/2)
1062 while (new_size > new_allocated_size) {
1063 if (!new_allocated_size)
1064 new_allocated_size = new_size;
1066 new_allocated_size += new_allocated_size / 2 + 1;
1069 if (new_allocated_size > d->allocated_size) {
1071 if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1072 d->allocated_size = 0;
1076 d->allocated_size = new_allocated_size;
1078 memcpy(d->buffer + d->pos, buf, buf_size);
1080 if (d->pos > d->size)
1085 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1087 unsigned char buf1[4];
1090 /* packetized write: output the header */
1091 AV_WB32(buf1, buf_size);
1092 ret = dyn_buf_write(opaque, buf1, 4);
1097 return dyn_buf_write(opaque, buf, buf_size);
1100 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1102 DynBuffer *d = opaque;
1104 if (whence == SEEK_CUR)
1106 else if (whence == SEEK_END)
1108 if (offset < 0 || offset > 0x7fffffffLL)
1114 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1117 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1119 if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1121 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1123 return AVERROR(ENOMEM);
1124 d->io_buffer_size = io_buffer_size;
1125 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1126 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1127 max_packet_size ? NULL : dyn_buf_seek);
1130 return AVERROR(ENOMEM);
1132 (*s)->max_packet_size = max_packet_size;
1136 int avio_open_dyn_buf(AVIOContext **s)
1138 return url_open_dyn_buf_internal(s, 0);
1141 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1143 if (max_packet_size <= 0)
1145 return url_open_dyn_buf_internal(s, max_packet_size);
1148 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1152 static const char padbuf[AV_INPUT_BUFFER_PADDING_SIZE] = {0};
1160 /* don't attempt to pad fixed-size packet buffers */
1161 if (!s->max_packet_size) {
1162 avio_write(s, padbuf, sizeof(padbuf));
1163 padding = AV_INPUT_BUFFER_PADDING_SIZE;
1169 *pbuffer = d->buffer;
1173 return size - padding;
1176 void ffio_free_dyn_buf(AVIOContext **s)
1181 avio_close_dyn_buf(*s, &tmp);
1186 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1188 DynBuffer *d = opaque;
1191 if (d->pos > d->size)
1196 int ffio_open_null_buf(AVIOContext **s)
1198 int ret = url_open_dyn_buf_internal(s, 0);
1200 AVIOContext *pb = *s;
1201 pb->write_packet = null_buf_write;
1206 int ffio_close_null_buf(AVIOContext *s)
1208 DynBuffer *d = s->opaque;