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 #define OFFSET(x) offsetof(AVIOContext,x)
57 #define E AV_OPT_FLAG_ENCODING_PARAM
58 #define D AV_OPT_FLAG_DECODING_PARAM
59 static const AVOption ff_avio_options[] = {
60 {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
64 const AVClass ff_avio_class = {
65 .class_name = "AVIOContext",
66 .item_name = av_default_item_name,
67 .version = LIBAVUTIL_VERSION_INT,
68 .option = ff_avio_options,
69 .child_next = ff_avio_child_next,
70 .child_class_next = ff_avio_child_class_next,
73 static void fill_buffer(AVIOContext *s);
74 static int url_resetbuf(AVIOContext *s, int flags);
76 int ffio_init_context(AVIOContext *s,
77 unsigned char *buffer,
81 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
82 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
83 int64_t (*seek)(void *opaque, int64_t offset, int whence))
85 memset(s, 0, sizeof(AVIOContext));
89 s->buffer_size = buffer_size;
91 s->buf_ptr_max = buffer;
95 url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
97 s->write_packet = write_packet;
98 s->read_packet = read_packet;
103 s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
104 s->min_packet_size = 0;
105 s->max_packet_size = 0;
106 s->update_checksum = NULL;
107 s->short_seek_threshold = SHORT_SEEK_THRESHOLD;
109 if (!read_packet && !write_flag) {
110 s->pos = buffer_size;
111 s->buf_end = s->buffer + buffer_size;
113 s->read_pause = NULL;
116 s->write_data_type = NULL;
117 s->ignore_boundary_point = 0;
118 s->current_type = AVIO_DATA_MARKER_UNKNOWN;
119 s->last_time = AV_NOPTS_VALUE;
120 s->short_seek_get = NULL;
126 AVIOContext *avio_alloc_context(
127 unsigned char *buffer,
131 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
132 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
133 int64_t (*seek)(void *opaque, int64_t offset, int whence))
135 AVIOContext *s = av_malloc(sizeof(AVIOContext));
138 ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
139 read_packet, write_packet, seek);
143 void avio_context_free(AVIOContext **ps)
148 static void writeout(AVIOContext *s, const uint8_t *data, int len)
152 if (s->write_data_type)
153 ret = s->write_data_type(s->opaque, (uint8_t *)data,
157 else if (s->write_packet)
158 ret = s->write_packet(s->opaque, (uint8_t *)data, len);
162 if (s->pos + len > s->written)
163 s->written = s->pos + len;
166 if (s->current_type == AVIO_DATA_MARKER_SYNC_POINT ||
167 s->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
168 s->current_type = AVIO_DATA_MARKER_UNKNOWN;
170 s->last_time = AV_NOPTS_VALUE;
171 s->writeout_count ++;
175 static void flush_buffer(AVIOContext *s)
177 s->buf_ptr_max = FFMAX(s->buf_ptr, s->buf_ptr_max);
178 if (s->write_flag && s->buf_ptr_max > s->buffer) {
179 writeout(s, s->buffer, s->buf_ptr_max - s->buffer);
180 if (s->update_checksum) {
181 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
182 s->buf_ptr_max - s->checksum_ptr);
183 s->checksum_ptr = s->buffer;
186 s->buf_ptr = s->buf_ptr_max = s->buffer;
188 s->buf_end = s->buffer;
191 void avio_w8(AVIOContext *s, int b)
193 av_assert2(b>=-128 && b<=255);
195 if (s->buf_ptr >= s->buf_end)
199 void ffio_fill(AVIOContext *s, int b, int count)
202 int len = FFMIN(s->buf_end - s->buf_ptr, count);
203 memset(s->buf_ptr, b, len);
206 if (s->buf_ptr >= s->buf_end)
213 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
215 if (s->direct && !s->update_checksum) {
217 writeout(s, buf, size);
221 int len = FFMIN(s->buf_end - s->buf_ptr, size);
222 memcpy(s->buf_ptr, buf, len);
225 if (s->buf_ptr >= s->buf_end)
233 void avio_flush(AVIOContext *s)
235 int seekback = s->write_flag ? FFMIN(0, s->buf_ptr - s->buf_ptr_max) : 0;
238 avio_seek(s, seekback, SEEK_CUR);
241 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
245 int force = whence & AVSEEK_FORCE;
248 whence &= ~AVSEEK_FORCE;
251 return AVERROR(EINVAL);
253 if ((whence & AVSEEK_SIZE))
254 return s->seek ? s->seek(s->opaque, offset, AVSEEK_SIZE) : AVERROR(ENOSYS);
256 buffer_size = s->buf_end - s->buffer;
257 // pos is the absolute position that the beginning of s->buffer corresponds to in the file
258 pos = s->pos - (s->write_flag ? 0 : buffer_size);
260 if (whence != SEEK_CUR && whence != SEEK_SET)
261 return AVERROR(EINVAL);
263 if (whence == SEEK_CUR) {
264 offset1 = pos + (s->buf_ptr - s->buffer);
267 if (offset > INT64_MAX - offset1)
268 return AVERROR(EINVAL);
272 return AVERROR(EINVAL);
274 if (s->short_seek_get) {
275 short_seek = s->short_seek_get(s->opaque);
276 /* fallback to default short seek */
278 short_seek = s->short_seek_threshold;
280 short_seek = s->short_seek_threshold;
282 offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
283 s->buf_ptr_max = FFMAX(s->buf_ptr_max, s->buf_ptr);
284 if ((!s->direct || !s->seek) &&
285 offset1 >= 0 && offset1 <= (s->write_flag ? s->buf_ptr_max - s->buffer : buffer_size)) {
286 /* can do the seek inside the buffer */
287 s->buf_ptr = s->buffer + offset1;
288 } else if ((!(s->seekable & AVIO_SEEKABLE_NORMAL) ||
289 offset1 <= buffer_size + short_seek) &&
290 !s->write_flag && offset1 >= 0 &&
291 (!s->direct || !s->seek) &&
292 (whence != SEEK_END || force)) {
293 while(s->pos < offset && !s->eof_reached)
297 s->buf_ptr = s->buf_end - (s->pos - offset);
298 } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
301 pos -= FFMIN(buffer_size>>1, pos);
302 if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
305 s->buf_ptr = s->buffer;
309 return avio_seek(s, offset, SEEK_SET | force);
316 return AVERROR(EPIPE);
317 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
321 s->buf_end = s->buffer;
322 s->buf_ptr = s->buf_ptr_max = s->buffer;
329 int64_t avio_skip(AVIOContext *s, int64_t offset)
331 return avio_seek(s, offset, SEEK_CUR);
334 int64_t avio_size(AVIOContext *s)
339 return AVERROR(EINVAL);
345 return AVERROR(ENOSYS);
346 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
348 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
351 s->seek(s->opaque, s->pos, SEEK_SET);
356 int avio_feof(AVIOContext *s)
364 return s->eof_reached;
367 void avio_wl32(AVIOContext *s, unsigned int val)
369 avio_w8(s, (uint8_t) val );
370 avio_w8(s, (uint8_t)(val >> 8 ));
371 avio_w8(s, (uint8_t)(val >> 16));
372 avio_w8(s, val >> 24 );
375 void avio_wb32(AVIOContext *s, unsigned int val)
377 avio_w8(s, val >> 24 );
378 avio_w8(s, (uint8_t)(val >> 16));
379 avio_w8(s, (uint8_t)(val >> 8 ));
380 avio_w8(s, (uint8_t) val );
383 int avio_put_str(AVIOContext *s, const char *str)
388 avio_write(s, (const unsigned char *) str, len);
394 static inline int put_str16(AVIOContext *s, const char *str, const int be)
396 const uint8_t *q = str;
404 GET_UTF8(ch, *q++, goto invalid;)
405 PUT_UTF16(ch, tmp, be ? avio_wb16(s, tmp) : avio_wl16(s, tmp);
409 av_log(s, AV_LOG_ERROR, "Invalid UTF8 sequence in avio_put_str16%s\n", be ? "be" : "le");
410 err = AVERROR(EINVAL);
424 #define PUT_STR16(type, big_endian) \
425 int avio_put_str16 ## type(AVIOContext *s, const char *str) \
427 return put_str16(s, str, big_endian); \
435 int ff_get_v_length(uint64_t val)
445 void ff_put_v(AVIOContext *bc, uint64_t val)
447 int i = ff_get_v_length(val);
450 avio_w8(bc, 128 | (uint8_t)(val >> (7*i)));
452 avio_w8(bc, val & 127);
455 void avio_wl64(AVIOContext *s, uint64_t val)
457 avio_wl32(s, (uint32_t)(val & 0xffffffff));
458 avio_wl32(s, (uint32_t)(val >> 32));
461 void avio_wb64(AVIOContext *s, uint64_t val)
463 avio_wb32(s, (uint32_t)(val >> 32));
464 avio_wb32(s, (uint32_t)(val & 0xffffffff));
467 void avio_wl16(AVIOContext *s, unsigned int val)
469 avio_w8(s, (uint8_t)val);
470 avio_w8(s, (int)val >> 8);
473 void avio_wb16(AVIOContext *s, unsigned int val)
475 avio_w8(s, (int)val >> 8);
476 avio_w8(s, (uint8_t)val);
479 void avio_wl24(AVIOContext *s, unsigned int val)
481 avio_wl16(s, val & 0xffff);
482 avio_w8(s, (int)val >> 16);
485 void avio_wb24(AVIOContext *s, unsigned int val)
487 avio_wb16(s, (int)val >> 8);
488 avio_w8(s, (uint8_t)val);
491 void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
493 if (type == AVIO_DATA_MARKER_FLUSH_POINT) {
494 if (s->buf_ptr - s->buffer >= s->min_packet_size)
498 if (!s->write_data_type)
500 // If ignoring boundary points, just treat it as unknown
501 if (type == AVIO_DATA_MARKER_BOUNDARY_POINT && s->ignore_boundary_point)
502 type = AVIO_DATA_MARKER_UNKNOWN;
503 // Avoid unnecessary flushes if we are already in non-header/trailer
504 // data and setting the type to unknown
505 if (type == AVIO_DATA_MARKER_UNKNOWN &&
506 (s->current_type != AVIO_DATA_MARKER_HEADER &&
507 s->current_type != AVIO_DATA_MARKER_TRAILER))
511 case AVIO_DATA_MARKER_HEADER:
512 case AVIO_DATA_MARKER_TRAILER:
513 // For header/trailer, ignore a new marker of the same type;
514 // consecutive header/trailer markers can be merged.
515 if (type == s->current_type)
520 // If we've reached here, we have a new, noteworthy marker.
521 // Flush the previous data and mark the start of the new data.
523 s->current_type = type;
527 static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
532 return AVERROR(EINVAL);
533 ret = s->read_packet(s->opaque, buf, size);
534 #if FF_API_OLD_AVIO_EOF_0
535 if (!ret && !s->max_packet_size) {
536 av_log(NULL, AV_LOG_WARNING, "Invalid return value 0 for stream protocol\n");
540 av_assert2(ret || s->max_packet_size);
547 static void fill_buffer(AVIOContext *s)
549 int max_buffer_size = s->max_packet_size ?
550 s->max_packet_size : IO_BUFFER_SIZE;
551 uint8_t *dst = s->buf_end - s->buffer + max_buffer_size < s->buffer_size ?
552 s->buf_end : s->buffer;
553 int len = s->buffer_size - (dst - s->buffer);
555 /* can't fill the buffer without read_packet, just set EOF if appropriate */
556 if (!s->read_packet && s->buf_ptr >= s->buf_end)
559 /* no need to do anything if EOF already reached */
563 if (s->update_checksum && dst == s->buffer) {
564 if (s->buf_end > s->checksum_ptr)
565 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
566 s->buf_end - s->checksum_ptr);
567 s->checksum_ptr = s->buffer;
570 /* make buffer smaller in case it ended up large after probing */
571 if (s->read_packet && s->orig_buffer_size && s->buffer_size > s->orig_buffer_size && len >= s->orig_buffer_size) {
572 if (dst == s->buffer && s->buf_ptr != dst) {
573 int ret = ffio_set_buf_size(s, s->orig_buffer_size);
575 av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
577 s->checksum_ptr = dst = s->buffer;
579 len = s->orig_buffer_size;
582 len = read_packet_wrapper(s, dst, len);
583 if (len == AVERROR_EOF) {
584 /* do not modify buffer if EOF reached so that a seek back can
585 be done without rereading data */
587 } else if (len < 0) {
593 s->buf_end = dst + len;
594 s->bytes_read += len;
598 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
601 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
604 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
607 return av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), checksum, buf, len);
610 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
613 return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
616 unsigned long ffio_get_checksum(AVIOContext *s)
618 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
619 s->buf_ptr - s->checksum_ptr);
620 s->update_checksum = NULL;
624 void ffio_init_checksum(AVIOContext *s,
625 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
626 unsigned long checksum)
628 s->update_checksum = update_checksum;
629 if (s->update_checksum) {
630 s->checksum = checksum;
631 s->checksum_ptr = s->buf_ptr;
635 /* XXX: put an inline version */
636 int avio_r8(AVIOContext *s)
638 if (s->buf_ptr >= s->buf_end)
640 if (s->buf_ptr < s->buf_end)
641 return *s->buf_ptr++;
645 int avio_read(AVIOContext *s, unsigned char *buf, int size)
651 len = FFMIN(s->buf_end - s->buf_ptr, size);
652 if (len == 0 || s->write_flag) {
653 if((s->direct || size > s->buffer_size) && !s->update_checksum) {
654 // bypass the buffer and read data directly into buf
655 len = read_packet_wrapper(s, buf, size);
656 if (len == AVERROR_EOF) {
657 /* do not modify buffer if EOF reached so that a seek back can
658 be done without rereading data */
661 } else if (len < 0) {
667 s->bytes_read += len;
671 s->buf_ptr = s->buffer;
672 s->buf_end = s->buffer/* + len*/;
676 len = s->buf_end - s->buf_ptr;
681 memcpy(buf, s->buf_ptr, len);
688 if (s->error) return s->error;
689 if (avio_feof(s)) return AVERROR_EOF;
694 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
696 int ret = avio_read(s, buf, size);
698 return AVERROR_INVALIDDATA;
702 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
704 if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
710 return avio_read(s, buf, size);
714 int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
721 if (s->read_packet && s->write_flag) {
722 len = read_packet_wrapper(s, buf, size);
728 len = s->buf_end - s->buf_ptr;
730 /* Reset the buf_end pointer to the start of the buffer, to make sure
731 * the fill_buffer call tries to read as much data as fits into the
732 * full buffer, instead of just what space is left after buf_end.
733 * This avoids returning partial packets at the end of the buffer,
734 * for packet based inputs.
736 s->buf_end = s->buf_ptr = s->buffer;
738 len = s->buf_end - s->buf_ptr;
742 memcpy(buf, s->buf_ptr, len);
745 if (s->error) return s->error;
746 if (avio_feof(s)) return AVERROR_EOF;
751 unsigned int avio_rl16(AVIOContext *s)
755 val |= avio_r8(s) << 8;
759 unsigned int avio_rl24(AVIOContext *s)
763 val |= avio_r8(s) << 16;
767 unsigned int avio_rl32(AVIOContext *s)
771 val |= avio_rl16(s) << 16;
775 uint64_t avio_rl64(AVIOContext *s)
778 val = (uint64_t)avio_rl32(s);
779 val |= (uint64_t)avio_rl32(s) << 32;
783 unsigned int avio_rb16(AVIOContext *s)
786 val = avio_r8(s) << 8;
791 unsigned int avio_rb24(AVIOContext *s)
794 val = avio_rb16(s) << 8;
798 unsigned int avio_rb32(AVIOContext *s)
801 val = avio_rb16(s) << 16;
806 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
813 if (c && i < maxlen-1)
815 } while (c != '\n' && c != '\r' && c);
816 if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
823 int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
825 int len = ff_get_line(s, buf, maxlen);
826 while (len > 0 && av_isspace(buf[len - 1]))
831 int64_t ff_read_line_to_bprint(AVIOContext *s, AVBPrint *bp)
842 end = (c == '\r' || c == '\n' || c == '\0');
845 } while (!end && len < sizeof(tmp));
846 av_bprint_append_data(bp, tmp, len);
850 if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
856 if (!c && !read && avio_feof(s))
862 int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp)
867 ret = ff_read_line_to_bprint(s, bp);
871 if (!av_bprint_is_complete(bp))
872 return AVERROR(ENOMEM);
877 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
882 return AVERROR(EINVAL);
883 // reserve 1 byte for terminating 0
884 buflen = FFMIN(buflen - 1, maxlen);
885 for (i = 0; i < buflen; i++)
886 if (!(buf[i] = avio_r8(s)))
889 for (; i < maxlen; i++)
895 #define GET_STR16(type, read) \
896 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
901 return AVERROR(EINVAL); \
902 while (ret + 1 < maxlen) {\
905 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
908 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
914 GET_STR16(le, avio_rl16)
915 GET_STR16(be, avio_rb16)
919 uint64_t avio_rb64(AVIOContext *s)
922 val = (uint64_t)avio_rb32(s) << 32;
923 val |= (uint64_t)avio_rb32(s);
927 uint64_t ffio_read_varlen(AVIOContext *bc){
933 val= (val<<7) + (tmp&127);
938 int ffio_fdopen(AVIOContext **s, URLContext *h)
940 uint8_t *buffer = NULL;
941 int buffer_size, max_packet_size;
943 max_packet_size = h->max_packet_size;
944 if (max_packet_size) {
945 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
947 buffer_size = IO_BUFFER_SIZE;
949 buffer = av_malloc(buffer_size);
951 return AVERROR(ENOMEM);
953 *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
954 (int (*)(void *, uint8_t *, int)) ffurl_read,
955 (int (*)(void *, uint8_t *, int)) ffurl_write,
956 (int64_t (*)(void *, int64_t, int))ffurl_seek);
960 (*s)->protocol_whitelist = av_strdup(h->protocol_whitelist);
961 if (!(*s)->protocol_whitelist && h->protocol_whitelist) {
965 (*s)->protocol_blacklist = av_strdup(h->protocol_blacklist);
966 if (!(*s)->protocol_blacklist && h->protocol_blacklist) {
970 (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
972 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
973 (*s)->max_packet_size = max_packet_size;
974 (*s)->min_packet_size = h->min_packet_size;
976 (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
978 (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
980 if (h->prot->url_read_seek)
981 (*s)->seekable |= AVIO_SEEKABLE_TIME;
983 (*s)->short_seek_get = (int (*)(void *))ffurl_get_short_seek;
984 (*s)->av_class = &ff_avio_class;
988 return AVERROR(ENOMEM);
991 URLContext* ffio_geturlcontext(AVIOContext *s)
996 if (s->opaque && s->read_packet == (int (*)(void *, uint8_t *, int))ffurl_read)
1002 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
1005 int max_buffer_size = s->max_packet_size ?
1006 s->max_packet_size : IO_BUFFER_SIZE;
1007 int filled = s->buf_end - s->buffer;
1008 ptrdiff_t checksum_ptr_offset = s->checksum_ptr ? s->checksum_ptr - s->buffer : -1;
1010 buf_size += s->buf_ptr - s->buffer + max_buffer_size;
1012 if (buf_size < filled || s->seekable || !s->read_packet)
1014 av_assert0(!s->write_flag);
1016 buffer = av_malloc(buf_size);
1018 return AVERROR(ENOMEM);
1020 memcpy(buffer, s->buffer, filled);
1022 s->buf_ptr = buffer + (s->buf_ptr - s->buffer);
1023 s->buf_end = buffer + (s->buf_end - s->buffer);
1025 s->buffer_size = buf_size;
1026 if (checksum_ptr_offset >= 0)
1027 s->checksum_ptr = s->buffer + checksum_ptr_offset;
1031 int ffio_set_buf_size(AVIOContext *s, int buf_size)
1034 buffer = av_malloc(buf_size);
1036 return AVERROR(ENOMEM);
1040 s->orig_buffer_size =
1041 s->buffer_size = buf_size;
1042 s->buf_ptr = s->buf_ptr_max = buffer;
1043 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
1047 int ffio_realloc_buf(AVIOContext *s, int buf_size)
1052 if (!s->buffer_size)
1053 return ffio_set_buf_size(s, buf_size);
1055 if (buf_size <= s->buffer_size)
1058 buffer = av_malloc(buf_size);
1060 return AVERROR(ENOMEM);
1062 data_size = s->write_flag ? (s->buf_ptr - s->buffer) : (s->buf_end - s->buf_ptr);
1064 memcpy(buffer, s->write_flag ? s->buffer : s->buf_ptr, data_size);
1067 s->orig_buffer_size = buf_size;
1068 s->buffer_size = buf_size;
1069 s->buf_ptr = s->write_flag ? (s->buffer + data_size) : s->buffer;
1071 s->buf_ptr_max = s->buffer + data_size;
1073 s->buf_end = s->write_flag ? (s->buffer + s->buffer_size) : (s->buf_ptr + data_size);
1078 static int url_resetbuf(AVIOContext *s, int flags)
1080 av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
1082 if (flags & AVIO_FLAG_WRITE) {
1083 s->buf_end = s->buffer + s->buffer_size;
1086 s->buf_end = s->buffer;
1092 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
1094 int64_t buffer_start;
1096 int overlap, new_size, alloc_size;
1097 uint8_t *buf = *bufp;
1099 if (s->write_flag) {
1101 return AVERROR(EINVAL);
1104 buffer_size = s->buf_end - s->buffer;
1106 /* the buffers must touch or overlap */
1107 if ((buffer_start = s->pos - buffer_size) > buf_size) {
1109 return AVERROR(EINVAL);
1112 overlap = buf_size - buffer_start;
1113 new_size = buf_size + buffer_size - overlap;
1115 alloc_size = FFMAX(s->buffer_size, new_size);
1116 if (alloc_size > buf_size)
1117 if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
1118 return AVERROR(ENOMEM);
1120 if (new_size > buf_size) {
1121 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
1122 buf_size = new_size;
1126 s->buf_ptr = s->buffer = buf;
1127 s->buffer_size = alloc_size;
1129 s->buf_end = s->buf_ptr + buf_size;
1135 int avio_open(AVIOContext **s, const char *filename, int flags)
1137 return avio_open2(s, filename, flags, NULL, NULL);
1140 int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
1141 const AVIOInterruptCB *int_cb, AVDictionary **options,
1142 const char *whitelist, const char *blacklist
1150 err = ffurl_open_whitelist(&h, filename, flags, int_cb, options, whitelist, blacklist, NULL);
1153 err = ffio_fdopen(s, h);
1161 int avio_open2(AVIOContext **s, const char *filename, int flags,
1162 const AVIOInterruptCB *int_cb, AVDictionary **options)
1164 return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL, NULL);
1167 int avio_close(AVIOContext *s)
1178 av_freep(&s->buffer);
1180 av_log(s, AV_LOG_VERBOSE, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count);
1182 av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
1185 avio_context_free(&s);
1187 return ffurl_close(h);
1190 int avio_closep(AVIOContext **s)
1192 int ret = avio_close(*s);
1197 int avio_printf(AVIOContext *s, const char *fmt, ...)
1202 av_bprint_init(&bp, 0, INT_MAX);
1204 av_vbprintf(&bp, fmt, ap);
1206 if (!av_bprint_is_complete(&bp)) {
1207 av_bprint_finalize(&bp, NULL);
1208 s->error = AVERROR(ENOMEM);
1209 return AVERROR(ENOMEM);
1211 avio_write(s, bp.str, bp.len);
1212 av_bprint_finalize(&bp, NULL);
1216 void avio_print_string_array(AVIOContext *s, const char *strings[])
1218 for(; *strings; strings++)
1219 avio_write(s, (const unsigned char *)*strings, strlen(*strings));
1222 int avio_pause(AVIOContext *s, int pause)
1225 return AVERROR(ENOSYS);
1226 return s->read_pause(s->opaque, pause);
1229 int64_t avio_seek_time(AVIOContext *s, int stream_index,
1230 int64_t timestamp, int flags)
1234 return AVERROR(ENOSYS);
1235 ret = s->read_seek(s->opaque, stream_index, timestamp, flags);
1238 s->buf_ptr = s->buf_end; // Flush buffer
1239 pos = s->seek(s->opaque, 0, SEEK_CUR);
1242 else if (pos != AVERROR(ENOSYS))
1248 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1253 ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1254 if (ret == AVERROR_EOF)
1258 av_bprint_append_data(pb, buf, ret);
1259 if (!av_bprint_is_complete(pb))
1260 return AVERROR(ENOMEM);
1266 int avio_accept(AVIOContext *s, AVIOContext **c)
1269 URLContext *sc = s->opaque;
1270 URLContext *cc = NULL;
1271 ret = ffurl_accept(sc, &cc);
1274 return ffio_fdopen(c, cc);
1277 int avio_handshake(AVIOContext *c)
1279 URLContext *cc = c->opaque;
1280 return ffurl_handshake(cc);
1283 /* output in a dynamic buffer */
1285 typedef struct DynBuffer {
1286 int pos, size, allocated_size;
1289 uint8_t io_buffer[1];
1292 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1294 DynBuffer *d = opaque;
1295 unsigned new_size, new_allocated_size;
1297 /* reallocate buffer if needed */
1298 new_size = d->pos + buf_size;
1299 new_allocated_size = d->allocated_size;
1300 if (new_size < d->pos || new_size > INT_MAX/2)
1302 while (new_size > new_allocated_size) {
1303 if (!new_allocated_size)
1304 new_allocated_size = new_size;
1306 new_allocated_size += new_allocated_size / 2 + 1;
1309 if (new_allocated_size > d->allocated_size) {
1311 if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1312 d->allocated_size = 0;
1316 d->allocated_size = new_allocated_size;
1318 memcpy(d->buffer + d->pos, buf, buf_size);
1320 if (d->pos > d->size)
1325 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1327 unsigned char buf1[4];
1330 /* packetized write: output the header */
1331 AV_WB32(buf1, buf_size);
1332 ret = dyn_buf_write(opaque, buf1, 4);
1337 return dyn_buf_write(opaque, buf, buf_size);
1340 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1342 DynBuffer *d = opaque;
1344 if (whence == SEEK_CUR)
1346 else if (whence == SEEK_END)
1348 if (offset < 0 || offset > 0x7fffffffLL)
1354 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1357 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1359 if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1361 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1363 return AVERROR(ENOMEM);
1364 d->io_buffer_size = io_buffer_size;
1365 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1366 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1367 max_packet_size ? NULL : dyn_buf_seek);
1370 return AVERROR(ENOMEM);
1372 (*s)->max_packet_size = max_packet_size;
1376 int avio_open_dyn_buf(AVIOContext **s)
1378 return url_open_dyn_buf_internal(s, 0);
1381 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1383 if (max_packet_size <= 0)
1385 return url_open_dyn_buf_internal(s, max_packet_size);
1388 int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1392 if (!s || s->error) {
1399 *pbuffer = d->io_buffer;
1400 return FFMAX(s->buf_ptr, s->buf_ptr_max) - s->buffer;
1405 *pbuffer = d->buffer;
1410 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1414 static const char padbuf[AV_INPUT_BUFFER_PADDING_SIZE] = {0};
1422 /* don't attempt to pad fixed-size packet buffers */
1423 if (!s->max_packet_size) {
1424 avio_write(s, padbuf, sizeof(padbuf));
1425 padding = AV_INPUT_BUFFER_PADDING_SIZE;
1431 *pbuffer = d->buffer;
1435 avio_context_free(&s);
1437 return size - padding;
1440 void ffio_free_dyn_buf(AVIOContext **s)
1450 avio_context_free(s);
1453 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1455 DynBuffer *d = opaque;
1458 if (d->pos > d->size)
1463 int ffio_open_null_buf(AVIOContext **s)
1465 int ret = url_open_dyn_buf_internal(s, 0);
1467 AVIOContext *pb = *s;
1468 pb->write_packet = null_buf_write;
1473 int ffio_close_null_buf(AVIOContext *s)
1475 DynBuffer *d = s->opaque;
1483 avio_context_free(&s);