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"
29 #include "libavutil/thread.h"
32 #include "avio_internal.h"
34 #include "metacube2.h"
38 #define IO_BUFFER_SIZE 32768
41 * Do seeks within this distance ahead of the current buffer by skipping
42 * data instead of calling the protocol seek function, for seekable
45 #define SHORT_SEEK_THRESHOLD 32768
47 static void *ff_avio_child_next(void *obj, void *prev)
50 return prev ? NULL : s->opaque;
53 static const AVClass *child_class_iterate(void **iter)
55 const AVClass *c = *iter ? NULL : &ffurl_context_class;
56 *iter = (void*)(uintptr_t)c;
60 #define OFFSET(x) offsetof(AVIOContext,x)
61 #define E AV_OPT_FLAG_ENCODING_PARAM
62 #define D AV_OPT_FLAG_DECODING_PARAM
63 static const AVOption ff_avio_options[] = {
64 {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
68 const AVClass ff_avio_class = {
69 .class_name = "AVIOContext",
70 .item_name = av_default_item_name,
71 .version = LIBAVUTIL_VERSION_INT,
72 .option = ff_avio_options,
73 .child_next = ff_avio_child_next,
74 .child_class_iterate = child_class_iterate,
77 static void fill_buffer(AVIOContext *s);
78 static int url_resetbuf(AVIOContext *s, int flags);
80 int ffio_init_context(AVIOContext *s,
81 unsigned char *buffer,
85 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
86 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
87 int64_t (*seek)(void *opaque, int64_t offset, int whence))
89 memset(s, 0, sizeof(AVIOContext));
93 s->buffer_size = buffer_size;
95 s->buf_ptr_max = buffer;
99 url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
101 s->write_packet = write_packet;
102 s->read_packet = read_packet;
107 s->seekable = seek ? AVIO_SEEKABLE_NORMAL : 0;
108 s->min_packet_size = 0;
109 s->max_packet_size = 0;
111 s->update_checksum = NULL;
112 s->short_seek_threshold = SHORT_SEEK_THRESHOLD;
114 if (!read_packet && !write_flag) {
115 s->pos = buffer_size;
116 s->buf_end = s->buffer + buffer_size;
118 s->read_pause = NULL;
121 s->write_data_type = NULL;
122 s->ignore_boundary_point = 0;
123 s->current_type = AVIO_DATA_MARKER_UNKNOWN;
124 s->last_time = AV_NOPTS_VALUE;
125 s->short_seek_get = NULL;
131 AVIOContext *avio_alloc_context(
132 unsigned char *buffer,
136 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
137 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
138 int64_t (*seek)(void *opaque, int64_t offset, int whence))
140 AVIOContext *s = av_malloc(sizeof(AVIOContext));
143 ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
144 read_packet, write_packet, seek);
148 void avio_context_free(AVIOContext **ps)
153 static void writeout(AVIOContext *s, const uint8_t *data, int len)
157 if (s->write_data_type)
158 ret = s->write_data_type(s->opaque, (uint8_t *)data,
162 else if (s->write_packet)
163 ret = s->write_packet(s->opaque, (uint8_t *)data, len);
167 if (s->pos + len > s->written)
168 s->written = s->pos + len;
171 if (s->current_type == AVIO_DATA_MARKER_SYNC_POINT ||
172 s->current_type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
173 s->current_type = AVIO_DATA_MARKER_UNKNOWN;
175 s->last_time = AV_NOPTS_VALUE;
176 s->writeout_count ++;
180 static AVOnce metacube2_crc_once_control = AV_ONCE_INIT;
181 static AVCRC metacube2_crc_table[257];
183 static void metacube2_crc_init_table_once(void)
185 av_assert0(av_crc_init(metacube2_crc_table, 0, 16, 0x8fdb, sizeof(metacube2_crc_table)) >= 0);
188 static uint16_t metacube2_compute_crc(const struct metacube2_block_header *hdr)
190 static const int data_len = sizeof(hdr->size) + sizeof(hdr->flags);
191 const uint8_t *data = (uint8_t *)&hdr->size;
194 ff_thread_once(&metacube2_crc_once_control, metacube2_crc_init_table_once);
196 // Metacube2 specifies a CRC start of 0x1234, but its pycrc-derived CRC
197 // includes a finalization step that is done somewhat differently in av_crc().
198 // 0x1234 alone sent through that finalization becomes 0x394a, and then we
199 // need a byte-swap of the CRC value (both on input and output) to account for
200 // differing conventions.
201 crc = av_crc(metacube2_crc_table, 0x4a39, data, data_len);
202 return av_bswap16(crc);
205 static void finalize_metacube_block_header(AVIOContext *s)
207 struct metacube2_block_header hdr;
208 int len = s->buf_ptr_max - s->buffer;
211 if (s->current_type == AVIO_DATA_MARKER_SYNC_POINT)
212 s->seen_sync_point = 1;
213 else if (s->current_type == AVIO_DATA_MARKER_HEADER)
214 // NOTE: If there are multiple blocks marked METACUBE_FLAGS_HEADER,
215 // only the last one will count. This may become a problem if the
216 // mux flushes halfway through the stream header; if so, we would
217 // need to keep track of and concatenate the different parts.
218 flags |= METACUBE_FLAGS_HEADER;
219 else if (s->seen_sync_point)
220 flags |= METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START;
222 memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
223 AV_WB32(&hdr.size, len - sizeof(hdr));
224 AV_WB16(&hdr.flags, flags);
225 AV_WB16(&hdr.csum, metacube2_compute_crc(&hdr));
226 memcpy(s->buffer, &hdr, sizeof(hdr));
229 static void flush_buffer(AVIOContext *s)
232 s->buf_ptr_max = FFMAX(s->buf_ptr, s->buf_ptr_max);
234 buffer_empty = s->buf_ptr_max <= s->buffer + sizeof(struct metacube2_block_header);
236 buffer_empty = s->buf_ptr_max <= s->buffer;
237 if (s->write_flag && !buffer_empty) {
239 finalize_metacube_block_header(s);
240 writeout(s, s->buffer, s->buf_ptr_max - s->buffer);
241 if (s->update_checksum) {
242 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
243 s->buf_ptr_max - s->checksum_ptr);
244 s->checksum_ptr = s->buffer;
247 s->buf_ptr = s->buf_ptr_max = s->buffer;
249 // Add space for Metacube header.
250 if (s->write_flag && s->metacube)
251 s->buf_ptr += sizeof(struct metacube2_block_header);
253 s->buf_end = s->buffer;
256 void avio_w8(AVIOContext *s, int b)
258 av_assert2(b>=-128 && b<=255);
260 if (s->buf_ptr >= s->buf_end)
264 void ffio_fill(AVIOContext *s, int b, int count)
267 int len = FFMIN(s->buf_end - s->buf_ptr, count);
268 memset(s->buf_ptr, b, len);
271 if (s->buf_ptr >= s->buf_end)
278 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
280 if (s->direct && !s->update_checksum && !s->metacube) {
282 writeout(s, buf, size);
286 int len = FFMIN(s->buf_end - s->buf_ptr, size);
287 memcpy(s->buf_ptr, buf, len);
290 if (s->buf_ptr >= s->buf_end)
298 void avio_flush(AVIOContext *s)
300 int seekback = s->write_flag ? FFMIN(0, s->buf_ptr - s->buf_ptr_max) : 0;
303 avio_seek(s, seekback, SEEK_CUR);
306 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
310 int force = whence & AVSEEK_FORCE;
313 whence &= ~AVSEEK_FORCE;
316 return AVERROR(EINVAL);
318 if ((whence & AVSEEK_SIZE))
319 return s->seek ? s->seek(s->opaque, offset, AVSEEK_SIZE) : AVERROR(ENOSYS);
321 buffer_size = s->buf_end - s->buffer;
322 // pos is the absolute position that the beginning of s->buffer corresponds to in the file
323 pos = s->pos - (s->write_flag ? 0 : buffer_size);
325 if (whence != SEEK_CUR && whence != SEEK_SET)
326 return AVERROR(EINVAL);
328 if (whence == SEEK_CUR) {
329 offset1 = pos + (s->buf_ptr - s->buffer);
331 if (s->metacube && s->write_flag)
332 return offset1 - sizeof(struct metacube2_block_header);
336 if (offset > INT64_MAX - offset1)
337 return AVERROR(EINVAL);
339 } else if (s->metacube && s->write_flag) {
340 offset += sizeof(struct metacube2_block_header);
343 return AVERROR(EINVAL);
345 short_seek = s->short_seek_threshold;
346 if (s->short_seek_get)
347 short_seek = FFMAX(s->short_seek_get(s->opaque), short_seek);
349 offset1 = offset - pos; // "offset1" is the relative offset from the beginning of s->buffer
350 s->buf_ptr_max = FFMAX(s->buf_ptr_max, s->buf_ptr);
351 if ((!s->direct || !s->seek) &&
352 offset1 >= 0 && offset1 <= (s->write_flag ? s->buf_ptr_max - s->buffer : buffer_size)) {
353 /* can do the seek inside the buffer */
354 s->buf_ptr = s->buffer + offset1;
355 } else if ((!(s->seekable & AVIO_SEEKABLE_NORMAL) ||
356 offset1 <= buffer_size + short_seek) &&
357 !s->write_flag && offset1 >= 0 &&
358 (!s->direct || !s->seek) &&
359 (whence != SEEK_END || force)) {
360 while(s->pos < offset && !s->eof_reached)
364 s->buf_ptr = s->buf_end - (s->pos - offset);
365 } else if(!s->write_flag && offset1 < 0 && -offset1 < buffer_size>>1 && s->seek && offset > 0) {
368 pos -= FFMIN(buffer_size>>1, pos);
369 if ((res = s->seek(s->opaque, pos, SEEK_SET)) < 0)
372 s->buf_ptr = s->buffer;
376 return avio_seek(s, offset, SEEK_SET | force);
383 return AVERROR(EPIPE);
384 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
388 s->buf_end = s->buffer;
389 s->buf_ptr = s->buf_ptr_max = s->buffer;
393 if (s->metacube && s->write_flag)
394 return offset - sizeof(struct metacube2_block_header);
399 int64_t avio_skip(AVIOContext *s, int64_t offset)
401 return avio_seek(s, offset, SEEK_CUR);
404 int64_t avio_size(AVIOContext *s)
409 return AVERROR(EINVAL);
415 return AVERROR(ENOSYS);
416 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
418 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
421 s->seek(s->opaque, s->pos, SEEK_SET);
426 int avio_feof(AVIOContext *s)
434 return s->eof_reached;
437 void avio_wl32(AVIOContext *s, unsigned int val)
439 avio_w8(s, (uint8_t) val );
440 avio_w8(s, (uint8_t)(val >> 8 ));
441 avio_w8(s, (uint8_t)(val >> 16));
442 avio_w8(s, val >> 24 );
445 void avio_wb32(AVIOContext *s, unsigned int val)
447 avio_w8(s, val >> 24 );
448 avio_w8(s, (uint8_t)(val >> 16));
449 avio_w8(s, (uint8_t)(val >> 8 ));
450 avio_w8(s, (uint8_t) val );
453 int avio_put_str(AVIOContext *s, const char *str)
458 avio_write(s, (const unsigned char *) str, len);
464 static inline int put_str16(AVIOContext *s, const char *str, const int be)
466 const uint8_t *q = str;
474 GET_UTF8(ch, *q++, goto invalid;)
475 PUT_UTF16(ch, tmp, be ? avio_wb16(s, tmp) : avio_wl16(s, tmp);
479 av_log(s, AV_LOG_ERROR, "Invalid UTF8 sequence in avio_put_str16%s\n", be ? "be" : "le");
480 err = AVERROR(EINVAL);
494 #define PUT_STR16(type, big_endian) \
495 int avio_put_str16 ## type(AVIOContext *s, const char *str) \
497 return put_str16(s, str, big_endian); \
505 void avio_wl64(AVIOContext *s, uint64_t val)
507 avio_wl32(s, (uint32_t)(val & 0xffffffff));
508 avio_wl32(s, (uint32_t)(val >> 32));
511 void avio_wb64(AVIOContext *s, uint64_t val)
513 avio_wb32(s, (uint32_t)(val >> 32));
514 avio_wb32(s, (uint32_t)(val & 0xffffffff));
517 void avio_wl16(AVIOContext *s, unsigned int val)
519 avio_w8(s, (uint8_t)val);
520 avio_w8(s, (int)val >> 8);
523 void avio_wb16(AVIOContext *s, unsigned int val)
525 avio_w8(s, (int)val >> 8);
526 avio_w8(s, (uint8_t)val);
529 void avio_wl24(AVIOContext *s, unsigned int val)
531 avio_wl16(s, val & 0xffff);
532 avio_w8(s, (int)val >> 16);
535 void avio_wb24(AVIOContext *s, unsigned int val)
537 avio_wb16(s, (int)val >> 8);
538 avio_w8(s, (uint8_t)val);
541 void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
543 if (type == AVIO_DATA_MARKER_FLUSH_POINT) {
544 if (s->buf_ptr - s->buffer >= s->min_packet_size)
548 if (!s->write_data_type && !s->metacube)
550 // If ignoring boundary points, just treat it as unknown
551 if (type == AVIO_DATA_MARKER_BOUNDARY_POINT && s->ignore_boundary_point)
552 type = AVIO_DATA_MARKER_UNKNOWN;
553 // Avoid unnecessary flushes if we are already in non-header/trailer
554 // data and setting the type to unknown
555 if (type == AVIO_DATA_MARKER_UNKNOWN &&
556 (s->current_type != AVIO_DATA_MARKER_HEADER &&
557 s->current_type != AVIO_DATA_MARKER_TRAILER))
561 case AVIO_DATA_MARKER_HEADER:
562 case AVIO_DATA_MARKER_TRAILER:
563 // For header/trailer, ignore a new marker of the same type;
564 // consecutive header/trailer markers can be merged.
565 if (type == s->current_type)
570 // If we've reached here, we have a new, noteworthy marker.
571 // Flush the previous data and mark the start of the new data.
573 s->current_type = type;
577 static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
582 return AVERROR(EINVAL);
583 ret = s->read_packet(s->opaque, buf, size);
584 av_assert2(ret || s->max_packet_size);
590 static void fill_buffer(AVIOContext *s)
592 int max_buffer_size = s->max_packet_size ?
593 s->max_packet_size : IO_BUFFER_SIZE;
594 uint8_t *dst = s->buf_end - s->buffer + max_buffer_size <= s->buffer_size ?
595 s->buf_end : s->buffer;
596 int len = s->buffer_size - (dst - s->buffer);
598 /* can't fill the buffer without read_packet, just set EOF if appropriate */
599 if (!s->read_packet && s->buf_ptr >= s->buf_end)
602 /* no need to do anything if EOF already reached */
606 if (s->update_checksum && dst == s->buffer) {
607 if (s->buf_end > s->checksum_ptr)
608 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
609 s->buf_end - s->checksum_ptr);
610 s->checksum_ptr = s->buffer;
613 /* make buffer smaller in case it ended up large after probing */
614 if (s->read_packet && s->orig_buffer_size && s->buffer_size > s->orig_buffer_size && len >= s->orig_buffer_size) {
615 if (dst == s->buffer && s->buf_ptr != dst) {
616 int ret = ffio_set_buf_size(s, s->orig_buffer_size);
618 av_log(s, AV_LOG_WARNING, "Failed to decrease buffer size\n");
620 s->checksum_ptr = dst = s->buffer;
622 len = s->orig_buffer_size;
625 len = read_packet_wrapper(s, dst, len);
626 if (len == AVERROR_EOF) {
627 /* do not modify buffer if EOF reached so that a seek back can
628 be done without rereading data */
630 } else if (len < 0) {
636 s->buf_end = dst + len;
637 s->bytes_read += len;
641 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
644 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
647 unsigned long ff_crcEDB88320_update(unsigned long checksum, const uint8_t *buf,
650 return av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), checksum, buf, len);
653 unsigned long ff_crcA001_update(unsigned long checksum, const uint8_t *buf,
656 return av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), checksum, buf, len);
659 unsigned long ffio_get_checksum(AVIOContext *s)
661 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
662 s->buf_ptr - s->checksum_ptr);
663 s->update_checksum = NULL;
667 void ffio_init_checksum(AVIOContext *s,
668 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
669 unsigned long checksum)
671 s->update_checksum = update_checksum;
672 if (s->update_checksum) {
673 s->checksum = checksum;
674 s->checksum_ptr = s->buf_ptr;
678 /* XXX: put an inline version */
679 int avio_r8(AVIOContext *s)
681 if (s->buf_ptr >= s->buf_end)
683 if (s->buf_ptr < s->buf_end)
684 return *s->buf_ptr++;
688 int avio_read(AVIOContext *s, unsigned char *buf, int size)
694 len = FFMIN(s->buf_end - s->buf_ptr, size);
695 if (len == 0 || s->write_flag) {
696 if((s->direct || size > s->buffer_size) && !s->update_checksum) {
697 // bypass the buffer and read data directly into buf
698 len = read_packet_wrapper(s, buf, size);
699 if (len == AVERROR_EOF) {
700 /* do not modify buffer if EOF reached so that a seek back can
701 be done without rereading data */
704 } else if (len < 0) {
710 s->bytes_read += len;
714 s->buf_ptr = s->buffer;
715 s->buf_end = s->buffer/* + len*/;
719 len = s->buf_end - s->buf_ptr;
724 memcpy(buf, s->buf_ptr, len);
731 if (s->error) return s->error;
732 if (avio_feof(s)) return AVERROR_EOF;
737 int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
739 int ret = avio_read(s, buf, size);
742 if (ret < 0 && ret != AVERROR_EOF)
744 return AVERROR_INVALIDDATA;
747 int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
749 if (s->buf_end - s->buf_ptr >= size && !s->write_flag) {
755 return avio_read(s, buf, size);
759 int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
764 return AVERROR(EINVAL);
766 if (s->read_packet && s->write_flag) {
767 len = read_packet_wrapper(s, buf, size);
773 len = s->buf_end - s->buf_ptr;
776 len = s->buf_end - s->buf_ptr;
780 memcpy(buf, s->buf_ptr, len);
783 if (s->error) return s->error;
784 if (avio_feof(s)) return AVERROR_EOF;
789 unsigned int avio_rl16(AVIOContext *s)
793 val |= avio_r8(s) << 8;
797 unsigned int avio_rl24(AVIOContext *s)
801 val |= avio_r8(s) << 16;
805 unsigned int avio_rl32(AVIOContext *s)
809 val |= avio_rl16(s) << 16;
813 uint64_t avio_rl64(AVIOContext *s)
816 val = (uint64_t)avio_rl32(s);
817 val |= (uint64_t)avio_rl32(s) << 32;
821 unsigned int avio_rb16(AVIOContext *s)
824 val = avio_r8(s) << 8;
829 unsigned int avio_rb24(AVIOContext *s)
832 val = avio_rb16(s) << 8;
836 unsigned int avio_rb32(AVIOContext *s)
839 val = avio_rb16(s) << 16;
844 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
851 if (c && i < maxlen-1)
853 } while (c != '\n' && c != '\r' && c);
854 if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
861 int ff_get_chomp_line(AVIOContext *s, char *buf, int maxlen)
863 int len = ff_get_line(s, buf, maxlen);
864 while (len > 0 && av_isspace(buf[len - 1]))
869 int64_t ff_read_line_to_bprint(AVIOContext *s, AVBPrint *bp)
880 end = (c == '\r' || c == '\n' || c == '\0');
883 } while (!end && len < sizeof(tmp));
884 av_bprint_append_data(bp, tmp, len);
888 if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
894 if (!c && !read && avio_feof(s))
900 int64_t ff_read_line_to_bprint_overwrite(AVIOContext *s, AVBPrint *bp)
905 ret = ff_read_line_to_bprint(s, bp);
909 if (!av_bprint_is_complete(bp))
910 return AVERROR(ENOMEM);
915 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
920 return AVERROR(EINVAL);
921 // reserve 1 byte for terminating 0
922 buflen = FFMIN(buflen - 1, maxlen);
923 for (i = 0; i < buflen; i++)
924 if (!(buf[i] = avio_r8(s)))
927 for (; i < maxlen; i++)
933 #define GET_STR16(type, read) \
934 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
939 return AVERROR(EINVAL); \
940 while (ret + 1 < maxlen) {\
943 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
946 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
952 GET_STR16(le, avio_rl16)
953 GET_STR16(be, avio_rb16)
957 uint64_t avio_rb64(AVIOContext *s)
960 val = (uint64_t)avio_rb32(s) << 32;
961 val |= (uint64_t)avio_rb32(s);
965 uint64_t ffio_read_varlen(AVIOContext *bc){
971 val= (val<<7) + (tmp&127);
976 int ffio_fdopen(AVIOContext **s, URLContext *h)
978 uint8_t *buffer = NULL;
979 int buffer_size, max_packet_size;
981 max_packet_size = h->max_packet_size;
982 if (max_packet_size) {
983 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
985 buffer_size = IO_BUFFER_SIZE;
987 if (!(h->flags & AVIO_FLAG_WRITE) && h->is_streamed) {
988 if (buffer_size > INT_MAX/2)
989 return AVERROR(EINVAL);
992 buffer = av_malloc(buffer_size);
994 return AVERROR(ENOMEM);
996 *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
997 (int (*)(void *, uint8_t *, int)) ffurl_read,
998 (int (*)(void *, uint8_t *, int)) ffurl_write,
999 (int64_t (*)(void *, int64_t, int))ffurl_seek);
1003 (*s)->protocol_whitelist = av_strdup(h->protocol_whitelist);
1004 if (!(*s)->protocol_whitelist && h->protocol_whitelist) {
1008 (*s)->protocol_blacklist = av_strdup(h->protocol_blacklist);
1009 if (!(*s)->protocol_blacklist && h->protocol_blacklist) {
1013 (*s)->direct = h->flags & AVIO_FLAG_DIRECT;
1015 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
1016 (*s)->max_packet_size = max_packet_size;
1017 (*s)->min_packet_size = h->min_packet_size;
1019 (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
1021 (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
1023 if (h->prot->url_read_seek)
1024 (*s)->seekable |= AVIO_SEEKABLE_TIME;
1026 (*s)->short_seek_get = (int (*)(void *))ffurl_get_short_seek;
1027 (*s)->av_class = &ff_avio_class;
1028 (*s)->metacube = h->flags & AVIO_FLAG_METACUBE;
1029 (*s)->seen_sync_point = 0;
1033 return AVERROR(ENOMEM);
1036 URLContext* ffio_geturlcontext(AVIOContext *s)
1041 if (s->opaque && s->read_packet == (int (*)(void *, uint8_t *, int))ffurl_read)
1047 static void update_checksum(AVIOContext *s)
1049 if (s->update_checksum && s->buf_ptr > s->checksum_ptr) {
1050 s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
1051 s->buf_ptr - s->checksum_ptr);
1055 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
1058 int max_buffer_size = s->max_packet_size ?
1059 s->max_packet_size : IO_BUFFER_SIZE;
1060 ptrdiff_t filled = s->buf_end - s->buf_ptr;
1062 if (buf_size <= s->buf_end - s->buf_ptr)
1065 buf_size += max_buffer_size - 1;
1067 if (buf_size + s->buf_ptr - s->buffer <= s->buffer_size || s->seekable || !s->read_packet)
1069 av_assert0(!s->write_flag);
1071 if (buf_size <= s->buffer_size) {
1073 memmove(s->buffer, s->buf_ptr, filled);
1075 buffer = av_malloc(buf_size);
1077 return AVERROR(ENOMEM);
1079 memcpy(buffer, s->buf_ptr, filled);
1082 s->buffer_size = buf_size;
1084 s->buf_ptr = s->buffer;
1085 s->buf_end = s->buffer + filled;
1086 s->checksum_ptr = s->buffer;
1090 int ffio_set_buf_size(AVIOContext *s, int buf_size)
1095 buf_size += sizeof(struct metacube2_block_header);
1097 buffer = av_malloc(buf_size);
1099 return AVERROR(ENOMEM);
1103 s->orig_buffer_size =
1104 s->buffer_size = buf_size;
1105 s->buf_ptr = s->buf_ptr_max = buffer;
1107 // Add space for Metacube header.
1109 s->buf_ptr += sizeof(struct metacube2_block_header);
1111 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
1115 int ffio_realloc_buf(AVIOContext *s, int buf_size)
1120 if (s->metacube && s->write_flag)
1121 buf_size += sizeof(struct metacube2_block_header);
1123 if (!s->buffer_size)
1124 return ffio_set_buf_size(s, buf_size);
1126 if (buf_size <= s->buffer_size)
1129 buffer = av_malloc(buf_size);
1131 return AVERROR(ENOMEM);
1133 data_size = s->write_flag ? (s->buf_ptr - s->buffer) : (s->buf_end - s->buf_ptr);
1135 memcpy(buffer, s->write_flag ? s->buffer : s->buf_ptr, data_size);
1138 s->orig_buffer_size = buf_size;
1139 s->buffer_size = buf_size;
1140 s->buf_ptr = s->write_flag ? (s->buffer + data_size) : s->buffer;
1142 // Add space for Metacube header.
1143 if (s->metacube && s->write_flag && data_size == 0)
1144 s->buf_ptr += sizeof(struct metacube2_block_header);
1147 s->buf_ptr_max = s->buffer + data_size;
1149 s->buf_end = s->write_flag ? (s->buffer + s->buffer_size) : (s->buf_ptr + data_size);
1154 static int url_resetbuf(AVIOContext *s, int flags)
1156 av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
1158 if (flags & AVIO_FLAG_WRITE) {
1159 s->buf_end = s->buffer + s->buffer_size;
1162 s->buf_end = s->buffer;
1168 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **bufp, int buf_size)
1170 int64_t buffer_start;
1172 int overlap, new_size, alloc_size;
1173 uint8_t *buf = *bufp;
1175 if (s->write_flag) {
1177 return AVERROR(EINVAL);
1180 buffer_size = s->buf_end - s->buffer;
1182 /* the buffers must touch or overlap */
1183 if ((buffer_start = s->pos - buffer_size) > buf_size) {
1185 return AVERROR(EINVAL);
1188 overlap = buf_size - buffer_start;
1189 new_size = buf_size + buffer_size - overlap;
1191 alloc_size = FFMAX(s->buffer_size, new_size);
1192 if (alloc_size > buf_size)
1193 if (!(buf = (*bufp) = av_realloc_f(buf, 1, alloc_size)))
1194 return AVERROR(ENOMEM);
1196 if (new_size > buf_size) {
1197 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
1198 buf_size = new_size;
1202 s->buf_ptr = s->buffer = buf;
1203 s->buffer_size = alloc_size;
1205 s->buf_end = s->buf_ptr + buf_size;
1211 int avio_open(AVIOContext **s, const char *filename, int flags)
1213 return avio_open2(s, filename, flags, NULL, NULL);
1216 int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
1217 const AVIOInterruptCB *int_cb, AVDictionary **options,
1218 const char *whitelist, const char *blacklist
1226 err = ffurl_open_whitelist(&h, filename, flags, int_cb, options, whitelist, blacklist, NULL);
1229 err = ffio_fdopen(s, h);
1237 int avio_open2(AVIOContext **s, const char *filename, int flags,
1238 const AVIOInterruptCB *int_cb, AVDictionary **options)
1240 return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL, NULL);
1243 int avio_close(AVIOContext *s)
1254 av_freep(&s->buffer);
1256 av_log(s, AV_LOG_VERBOSE, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count);
1258 av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
1261 avio_context_free(&s);
1263 return ffurl_close(h);
1266 int avio_closep(AVIOContext **s)
1268 int ret = avio_close(*s);
1273 int avio_printf(AVIOContext *s, const char *fmt, ...)
1278 av_bprint_init(&bp, 0, INT_MAX);
1280 av_vbprintf(&bp, fmt, ap);
1282 if (!av_bprint_is_complete(&bp)) {
1283 av_bprint_finalize(&bp, NULL);
1284 s->error = AVERROR(ENOMEM);
1285 return AVERROR(ENOMEM);
1287 avio_write(s, bp.str, bp.len);
1288 av_bprint_finalize(&bp, NULL);
1292 void avio_print_string_array(AVIOContext *s, const char *strings[])
1294 for(; *strings; strings++)
1295 avio_write(s, (const unsigned char *)*strings, strlen(*strings));
1298 int avio_pause(AVIOContext *s, int pause)
1301 return AVERROR(ENOSYS);
1302 return s->read_pause(s->opaque, pause);
1305 int64_t avio_seek_time(AVIOContext *s, int stream_index,
1306 int64_t timestamp, int flags)
1310 return AVERROR(ENOSYS);
1311 ret = s->read_seek(s->opaque, stream_index, timestamp, flags);
1314 s->buf_ptr = s->buf_end; // Flush buffer
1315 pos = s->seek(s->opaque, 0, SEEK_CUR);
1318 else if (pos != AVERROR(ENOSYS))
1324 int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
1329 ret = avio_read(h, buf, FFMIN(max_size, sizeof(buf)));
1330 if (ret == AVERROR_EOF)
1334 av_bprint_append_data(pb, buf, ret);
1335 if (!av_bprint_is_complete(pb))
1336 return AVERROR(ENOMEM);
1342 int avio_accept(AVIOContext *s, AVIOContext **c)
1345 URLContext *sc = s->opaque;
1346 URLContext *cc = NULL;
1347 ret = ffurl_accept(sc, &cc);
1350 return ffio_fdopen(c, cc);
1353 int avio_handshake(AVIOContext *c)
1355 URLContext *cc = c->opaque;
1356 return ffurl_handshake(cc);
1359 /* output in a dynamic buffer */
1361 typedef struct DynBuffer {
1362 int pos, size, allocated_size;
1365 uint8_t io_buffer[1];
1368 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1370 DynBuffer *d = opaque;
1373 /* reallocate buffer if needed */
1374 new_size = (unsigned)d->pos + buf_size;
1375 if (new_size < d->pos || new_size > INT_MAX)
1376 return AVERROR(ERANGE);
1377 if (new_size > d->allocated_size) {
1378 unsigned new_allocated_size = d->allocated_size ? d->allocated_size
1381 while (new_size > new_allocated_size)
1382 new_allocated_size += new_allocated_size / 2 + 1;
1384 new_allocated_size = FFMIN(new_allocated_size, INT_MAX);
1386 if ((err = av_reallocp(&d->buffer, new_allocated_size)) < 0) {
1387 d->allocated_size = 0;
1391 d->allocated_size = new_allocated_size;
1393 memcpy(d->buffer + d->pos, buf, buf_size);
1395 if (d->pos > d->size)
1400 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1402 unsigned char buf1[4];
1405 /* packetized write: output the header */
1406 AV_WB32(buf1, buf_size);
1407 ret = dyn_buf_write(opaque, buf1, 4);
1412 return dyn_buf_write(opaque, buf, buf_size);
1415 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1417 DynBuffer *d = opaque;
1419 if (whence == SEEK_CUR)
1421 else if (whence == SEEK_END)
1424 return AVERROR(EINVAL);
1425 if (offset > INT_MAX)
1426 return AVERROR(ERANGE);
1431 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1434 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1436 if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1437 return AVERROR(ERANGE);
1438 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1440 return AVERROR(ENOMEM);
1441 d->io_buffer_size = io_buffer_size;
1442 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1443 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1444 max_packet_size ? NULL : dyn_buf_seek);
1447 return AVERROR(ENOMEM);
1449 (*s)->max_packet_size = max_packet_size;
1453 int avio_open_dyn_buf(AVIOContext **s)
1455 return url_open_dyn_buf_internal(s, 0);
1458 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1460 if (max_packet_size <= 0)
1461 return AVERROR(EINVAL);
1462 return url_open_dyn_buf_internal(s, max_packet_size);
1465 int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1475 if (!s->error && !d->size) {
1476 *pbuffer = d->io_buffer;
1477 return FFMAX(s->buf_ptr, s->buf_ptr_max) - s->buffer;
1482 *pbuffer = d->buffer;
1487 void ffio_reset_dyn_buf(AVIOContext *s)
1489 DynBuffer *d = s->opaque;
1490 int max_packet_size = s->max_packet_size;
1492 ffio_init_context(s, d->io_buffer, d->io_buffer_size, 1, d, NULL,
1493 s->write_packet, s->seek);
1494 s->max_packet_size = max_packet_size;
1495 d->pos = d->size = 0;
1498 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1502 static const char padbuf[AV_INPUT_BUFFER_PADDING_SIZE] = {0};
1510 /* don't attempt to pad fixed-size packet buffers */
1511 if (!s->max_packet_size) {
1512 avio_write(s, padbuf, sizeof(padbuf));
1513 padding = AV_INPUT_BUFFER_PADDING_SIZE;
1519 *pbuffer = d->buffer;
1523 avio_context_free(&s);
1525 return size - padding;
1528 void ffio_free_dyn_buf(AVIOContext **s)
1538 avio_context_free(s);
1541 static int null_buf_write(void *opaque, uint8_t *buf, int buf_size)
1543 DynBuffer *d = opaque;
1546 if (d->pos > d->size)
1551 int ffio_open_null_buf(AVIOContext **s)
1553 int ret = url_open_dyn_buf_internal(s, 0);
1555 AVIOContext *pb = *s;
1556 pb->write_packet = null_buf_write;
1561 int ffio_close_null_buf(AVIOContext *s)
1563 DynBuffer *d = s->opaque;
1571 avio_context_free(&s);