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 = s->pos - (s->write_flag ? 0 : buffer_size);
218 if (whence != SEEK_CUR && whence != SEEK_SET)
219 return AVERROR(EINVAL);
221 if (whence == SEEK_CUR) {
222 offset1 = pos + (s->buf_ptr - s->buffer);
228 return AVERROR(EINVAL);
230 offset1 = offset - pos;
231 if (!s->must_flush && (!s->direct || !s->seek) &&
232 offset1 >= 0 && offset1 <= buffer_size - s->write_flag) {
233 /* can do the seek inside the buffer */
234 s->buf_ptr = s->buffer + offset1;
235 } else if ((!s->seekable ||
236 offset1 <= s->buf_end + s->short_seek_threshold - s->buffer) &&
237 !s->write_flag && offset1 >= 0 &&
238 (!s->direct || !s->seek) &&
239 (whence != SEEK_END || force)) {
240 while(s->pos < offset && !s->eof_reached)
244 s->buf_ptr = s->buf_end + offset - s->pos;
245 } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
248 pos -= FFMIN(buffer_size>>1, pos);
249 if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
252 s->buf_ptr = s->buffer;
256 return avio_seek(s, offset, SEEK_SET | force);
264 return AVERROR(EPIPE);
265 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
269 s->buf_end = s->buffer;
270 s->buf_ptr = s->buffer;
277 int64_t avio_skip(AVIOContext *s, int64_t offset)
279 return avio_seek(s, offset, SEEK_CUR);
282 int64_t avio_size(AVIOContext *s)
287 return AVERROR(EINVAL);
290 return AVERROR(ENOSYS);
291 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
293 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
296 s->seek(s->opaque, s->pos, SEEK_SET);
301 int avio_feof(AVIOContext *s)
309 return s->eof_reached;
313 int url_feof(AVIOContext *s)
319 void avio_wl32(AVIOContext *s, unsigned int val)
321 avio_w8(s, (uint8_t) val );
322 avio_w8(s, (uint8_t)(val >> 8 ));
323 avio_w8(s, (uint8_t)(val >> 16));
324 avio_w8(s, val >> 24 );
327 void avio_wb32(AVIOContext *s, unsigned int val)
329 avio_w8(s, val >> 24 );
330 avio_w8(s, (uint8_t)(val >> 16));
331 avio_w8(s, (uint8_t)(val >> 8 ));
332 avio_w8(s, (uint8_t) val );
335 int avio_put_str(AVIOContext *s, const char *str)
340 avio_write(s, (const unsigned char *) str, len);
346 static inline int put_str16(AVIOContext *s, const char *str, const int be)
348 const uint8_t *q = str;
356 GET_UTF8(ch, *q++, goto invalid;)
357 PUT_UTF16(ch, tmp, be ? avio_wb16(s, tmp) : avio_wl16(s, tmp);
361 av_log(s, AV_LOG_ERROR, "Invaid UTF8 sequence in avio_put_str16%s\n", be ? "be" : "le");
362 err = AVERROR(EINVAL);
374 #define PUT_STR16(type, big_endian) \
375 int avio_put_str16 ## type(AVIOContext *s, const char *str) \
377 return put_str16(s, str, big_endian); \
385 int ff_get_v_length(uint64_t val)
395 void ff_put_v(AVIOContext *bc, uint64_t val)
397 int i = ff_get_v_length(val);
400 avio_w8(bc, 128 | (uint8_t)(val >> (7*i)));
402 avio_w8(bc, val & 127);
405 void avio_wl64(AVIOContext *s, uint64_t val)
407 avio_wl32(s, (uint32_t)(val & 0xffffffff));
408 avio_wl32(s, (uint32_t)(val >> 32));
411 void avio_wb64(AVIOContext *s, uint64_t val)
413 avio_wb32(s, (uint32_t)(val >> 32));
414 avio_wb32(s, (uint32_t)(val & 0xffffffff));
417 void avio_wl16(AVIOContext *s, unsigned int val)
419 avio_w8(s, (uint8_t)val);
420 avio_w8(s, (int)val >> 8);
423 void avio_wb16(AVIOContext *s, unsigned int val)
425 avio_w8(s, (int)val >> 8);
426 avio_w8(s, (uint8_t)val);
429 void avio_wl24(AVIOContext *s, unsigned int val)
431 avio_wl16(s, val & 0xffff);
432 avio_w8(s, (int)val >> 16);
435 void avio_wb24(AVIOContext *s, unsigned int val)
437 avio_wb16(s, (int)val >> 8);
438 avio_w8(s, (uint8_t)val);
443 static void fill_buffer(AVIOContext *s)
445 int max_buffer_size = s->max_packet_size ?
446 s->max_packet_size : IO_BUFFER_SIZE;
447 uint8_t *dst = s->buf_end - s->buffer + max_buffer_size < s->buffer_size ?
448 s->buf_end : s->buffer;
449 int len = s->buffer_size - (dst - s->buffer);
451 /* can't fill the buffer without read_packet, just set EOF if appropriate */
452 if (!s->read_packet && s->buf_ptr >= s->buf_end)
455 /* no need to do anything if EOF already reached */
459 if (s->update_checksum && dst == s->buffer) {
460 if (s->buf_end > s->checksum_ptr)
461 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
462 s->buf_end - s->checksum_ptr);
463 s->checksum_ptr = s->buffer;
466 /* make buffer smaller in case it ended up large after probing */
467 if (s->read_packet && s->orig_buffer_size && s->buffer_size > s->orig_buffer_size) {
468 if (dst == s->buffer) {
469 int ret = ffio_set_buf_size(s, s->orig_buffer_size);
471 av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
473 s->checksum_ptr = dst = s->buffer;
475 av_assert0(len >= s->orig_buffer_size);
476 len = s->orig_buffer_size;
480 len = s->read_packet(s->opaque, dst, len);
484 /* do not modify buffer if EOF reached so that a seek back can
485 be done without rereading data */
492 s->buf_end = dst + len;
493 s->bytes_read += len;
497 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
500 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
503 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
506 return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
509 unsigned long ffio_get_checksum(AVIOContext *s)
511 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
512 s->buf_ptr - s->checksum_ptr);
513 s->update_checksum = NULL;
517 void ffio_init_checksum(AVIOContext *s,
518 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
519 unsigned long checksum)
521 s->update_checksum = update_checksum;
522 if (s->update_checksum) {
523 s->checksum = checksum;
524 s->checksum_ptr = s->buf_ptr;
528 /* XXX: put an inline version */
529 int avio_r8(AVIOContext *s)
531 if (s->buf_ptr >= s->buf_end)
533 if (s->buf_ptr < s->buf_end)
534 return *s->buf_ptr++;
538 int avio_read(AVIOContext *s, unsigned char *buf, int size)
544 len = s->buf_end - s->buf_ptr;
547 if (len == 0 || s->write_flag) {
548 if((s->direct || size > s->buffer_size) && !s->update_checksum){
550 len = s->read_packet(s->opaque, buf, size);
552 /* do not modify buffer if EOF reached so that a seek back can
553 be done without rereading data */
560 s->bytes_read += len;
563 s->buf_ptr = s->buffer;
564 s->buf_end = s->buffer/* + len*/;
568 len = s->buf_end - s->buf_ptr;
573 memcpy(buf, s->buf_ptr, len);
580 if (s->error) return s->error;
581 if (avio_feof(s)) return AVERROR_EOF;
586 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
588 int ret = avio_read(s, buf, size);
590 return AVERROR_INVALIDDATA;
594 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
596 if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
602 return avio_read(s, buf, size);
606 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
613 if (s->read_packet && s->write_flag) {
614 len = s->read_packet(s->opaque, buf, size);
620 len = s->buf_end - s->buf_ptr;
622 /* Reset the buf_end pointer to the start of the buffer, to make sure
623 * the fill_buffer call tries to read as much data as fits into the
624 * full buffer, instead of just what space is left after buf_end.
625 * This avoids returning partial packets at the end of the buffer,
626 * for packet based inputs.
628 s->buf_end = s->buf_ptr = s->buffer;
630 len = s->buf_end - s->buf_ptr;
634 memcpy(buf, s->buf_ptr, len);
637 if (s->error) return s->error;
638 if (avio_feof(s)) return AVERROR_EOF;
643 unsigned int avio_rl16(AVIOContext *s)
647 val |= avio_r8(s) << 8;
651 unsigned int avio_rl24(AVIOContext *s)
655 val |= avio_r8(s) << 16;
659 unsigned int avio_rl32(AVIOContext *s)
663 val |= avio_rl16(s) << 16;
667 uint64_t avio_rl64(AVIOContext *s)
670 val = (uint64_t)avio_rl32(s);
671 val |= (uint64_t)avio_rl32(s) << 32;
675 unsigned int avio_rb16(AVIOContext *s)
678 val = avio_r8(s) << 8;
683 unsigned int avio_rb24(AVIOContext *s)
686 val = avio_rb16(s) << 8;
690 unsigned int avio_rb32(AVIOContext *s)
693 val = avio_rb16(s) << 16;
698 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
705 if (c && i < maxlen-1)
707 } while (c != '\n' && c != '\r' && c);
708 if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
715 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
720 return AVERROR(EINVAL);
721 // reserve 1 byte for terminating 0
722 buflen = FFMIN(buflen - 1, maxlen);
723 for (i = 0; i < buflen; i++)
724 if (!(buf[i] = avio_r8(s)))
727 for (; i < maxlen; i++)
733 #define GET_STR16(type, read) \
734 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
739 return AVERROR(EINVAL); \
740 while (ret + 1 < maxlen) {\
743 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
746 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
752 GET_STR16(le, avio_rl16)
753 GET_STR16(be, avio_rb16)
757 uint64_t avio_rb64(AVIOContext *s)
760 val = (uint64_t)avio_rb32(s) << 32;
761 val |= (uint64_t)avio_rb32(s);
765 uint64_t ffio_read_varlen(AVIOContext *bc){
771 val= (val<<7) + (tmp&127);
776 int ffio_fdopen(AVIOContext **s, URLContext *h)
779 int buffer_size, max_packet_size;
781 max_packet_size = h->max_packet_size;
782 if (max_packet_size) {
783 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
785 buffer_size = IO_BUFFER_SIZE;
787 buffer = av_malloc(buffer_size);
789 return AVERROR(ENOMEM);
791 *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
792 (int (*)(void *, uint8_t *, int)) ffurl_read,
793 (int (*)(void *, uint8_t *, int)) ffurl_write,
794 (int64_t (*)(void *, int64_t, int)) ffurl_seek);
797 return AVERROR(ENOMEM);
799 (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
800 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
801 (*s)->max_packet_size = max_packet_size;
803 (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
804 (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
806 (*s)->av_class = &ff_avio_class;
810 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
813 int max_buffer_size = s->max_packet_size ?
814 s->max_packet_size : IO_BUFFER_SIZE;
815 int filled = s->buf_end - s->buffer;
816 ptrdiff_t checksum_ptr_offset = s->checksum_ptr ? s->checksum_ptr - s->buffer : -1;
818 buf_size += s->buf_ptr - s->buffer + max_buffer_size;
820 if (buf_size < filled || s->seekable || !s->read_packet)
822 av_assert0(!s->write_flag);
824 buffer = av_malloc(buf_size);
826 return AVERROR(ENOMEM);
828 memcpy(buffer, s->buffer, filled);
830 s->buf_ptr = buffer + (s->buf_ptr - s->buffer);
831 s->buf_end = buffer + (s->buf_end - s->buffer);
833 s->buffer_size = buf_size;
834 if (checksum_ptr_offset >= 0)
835 s->checksum_ptr = s->buffer + checksum_ptr_offset;
839 int ffio_set_buf_size(AVIOContext *s, int buf_size)
842 buffer = av_malloc(buf_size);
844 return AVERROR(ENOMEM);
848 s->orig_buffer_size =
849 s->buffer_size = buf_size;
851 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
855 static int url_resetbuf(AVIOContext *s, int flags)
857 av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
859 if (flags & AVIO_FLAG_WRITE) {
860 s->buf_end = s->buffer + s->buffer_size;
863 s->buf_end = s->buffer;
869 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
871 int64_t buffer_start;
873 int overlap, new_size, alloc_size;
874 uint8_t *buf = *bufp;
878 return AVERROR(EINVAL);
881 buffer_size = s->buf_end - s->buffer;
883 /* the buffers must touch or overlap */
884 if ((buffer_start = s->pos - buffer_size) > buf_size) {
886 return AVERROR(EINVAL);
889 overlap = buf_size - buffer_start;
890 new_size = buf_size + buffer_size - overlap;
892 alloc_size = FFMAX(s->buffer_size, new_size);
893 if (alloc_size > buf_size)
894 if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
895 return AVERROR(ENOMEM);
897 if (new_size > buf_size) {
898 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
903 s->buf_ptr = s->buffer = buf;
904 s->buffer_size = alloc_size;
906 s->buf_end = s->buf_ptr + buf_size;
913 int avio_open(AVIOContext **s, const char *filename, int flags)
915 return avio_open2(s, filename, flags, NULL, NULL);
918 int avio_open2(AVIOContext **s, const char *filename, int flags,
919 const AVIOInterruptCB *int_cb, AVDictionary **options)
924 err = ffurl_open(&h, filename, flags, int_cb, options);
927 err = ffio_fdopen(s, h);
935 int ffio_open2_wrapper(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags,
936 const AVIOInterruptCB *int_cb, AVDictionary **options)
938 return avio_open2(pb, url, flags, int_cb, options);
941 int avio_close(AVIOContext *s)
950 av_freep(&s->buffer);
952 av_log(s, AV_LOG_DEBUG, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count);
954 av_log(s, AV_LOG_DEBUG, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
956 return ffurl_close(h);
959 int avio_closep(AVIOContext **s)
961 int ret = avio_close(*s);
966 int avio_printf(AVIOContext *s, const char *fmt, ...)
973 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
975 avio_write(s, buf, strlen(buf));
979 int avio_pause(AVIOContext *s, int pause)
982 return AVERROR(ENOSYS);
983 return s->read_pause(s->opaque, pause);
986 int64_t avio_seek_time(AVIOContext *s, int stream_index,
987 int64_t timestamp, int flags)
989 URLContext *h = s->opaque;
992 return AVERROR(ENOSYS);
993 ret = s->read_seek(h, stream_index, timestamp, flags);
996 s->buf_ptr = s->buf_end; // Flush buffer
997 pos = s->seek(h, 0, SEEK_CUR);
1000 else if (pos != AVERROR(ENOSYS))
1006 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1011 ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1012 if (ret == AVERROR_EOF)
1016 av_bprint_append_data(pb, buf, ret);
1017 if (!av_bprint_is_complete(pb))
1018 return AVERROR(ENOMEM);
1024 /* output in a dynamic buffer */
1026 typedef struct DynBuffer {
1027 int pos, size, allocated_size;
1030 uint8_t io_buffer[1];
1033 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1035 DynBuffer *d = opaque;
1036 unsigned new_size, new_allocated_size;
1038 /* reallocate buffer if needed */
1039 new_size = d->pos + buf_size;
1040 new_allocated_size = d->allocated_size;
1041 if (new_size < d->pos || new_size > INT_MAX/2)
1043 while (new_size > new_allocated_size) {
1044 if (!new_allocated_size)
1045 new_allocated_size = new_size;
1047 new_allocated_size += new_allocated_size / 2 + 1;
1050 if (new_allocated_size > d->allocated_size) {
1052 if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1053 d->allocated_size = 0;
1057 d->allocated_size = new_allocated_size;
1059 memcpy(d->buffer + d->pos, buf, buf_size);
1061 if (d->pos > d->size)
1066 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1068 unsigned char buf1[4];
1071 /* packetized write: output the header */
1072 AV_WB32(buf1, buf_size);
1073 ret = dyn_buf_write(opaque, buf1, 4);
1078 return dyn_buf_write(opaque, buf, buf_size);
1081 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1083 DynBuffer *d = opaque;
1085 if (whence == SEEK_CUR)
1087 else if (whence == SEEK_END)
1089 if (offset < 0 || offset > 0x7fffffffLL)
1095 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1098 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1100 if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1102 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1104 return AVERROR(ENOMEM);
1105 d->io_buffer_size = io_buffer_size;
1106 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1107 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1108 max_packet_size ? NULL : dyn_buf_seek);
1111 return AVERROR(ENOMEM);
1113 (*s)->max_packet_size = max_packet_size;
1117 int avio_open_dyn_buf(AVIOContext **s)
1119 return url_open_dyn_buf_internal(s, 0);
1122 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1124 if (max_packet_size <= 0)
1126 return url_open_dyn_buf_internal(s, max_packet_size);
1129 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1133 static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1141 /* don't attempt to pad fixed-size packet buffers */
1142 if (!s->max_packet_size) {
1143 avio_write(s, padbuf, sizeof(padbuf));
1144 padding = FF_INPUT_BUFFER_PADDING_SIZE;
1150 *pbuffer = d->buffer;
1154 return size - padding;
1157 void ffio_free_dyn_buf(AVIOContext **s)
1162 avio_close_dyn_buf(*s, &tmp);
1167 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1169 DynBuffer *d = opaque;
1172 if (d->pos > d->size)
1177 int ffio_open_null_buf(AVIOContext **s)
1179 int ret = url_open_dyn_buf_internal(s, 0);
1181 AVIOContext *pb = *s;
1182 pb->write_packet = null_buf_write;
1187 int ffio_close_null_buf(AVIOContext *s)
1189 DynBuffer *d = s->opaque;