3 * Copyright (c) 2000,2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/crc.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/log.h"
26 #include "libavutil/opt.h"
29 #include "avio_internal.h"
34 #define IO_BUFFER_SIZE 32768
37 * Do seeks within this distance ahead of the current buffer by skipping
38 * data instead of calling the protocol seek function, for seekable
41 #define SHORT_SEEK_THRESHOLD 4096
43 static void *ffio_url_child_next(void *obj, void *prev)
46 return prev ? NULL : s->opaque;
49 static const AVClass *ffio_url_child_class_next(const AVClass *prev)
51 return prev ? NULL : &ffurl_context_class;
54 static const AVOption ffio_url_options[] = {
58 const AVClass ffio_url_class = {
59 .class_name = "AVIOContext",
60 .item_name = av_default_item_name,
61 .version = LIBAVUTIL_VERSION_INT,
62 .option = ffio_url_options,
63 .child_next = ffio_url_child_next,
64 .child_class_next = ffio_url_child_class_next,
67 static void fill_buffer(AVIOContext *s);
68 static int url_resetbuf(AVIOContext *s, int flags);
70 int ffio_init_context(AVIOContext *s,
71 unsigned char *buffer,
75 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
76 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
77 int64_t (*seek)(void *opaque, int64_t offset, int whence))
80 s->buffer_size = buffer_size;
83 url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
84 s->write_packet = write_packet;
85 s->read_packet = read_packet;
91 s->seekable = AVIO_SEEKABLE_NORMAL;
92 s->max_packet_size = 0;
93 s->update_checksum= NULL;
94 if(!read_packet && !write_flag){
96 s->buf_end = s->buffer + buffer_size;
103 AVIOContext *avio_alloc_context(
104 unsigned char *buffer,
108 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
109 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
110 int64_t (*seek)(void *opaque, int64_t offset, int whence))
112 AVIOContext *s = av_mallocz(sizeof(AVIOContext));
115 ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
116 read_packet, write_packet, seek);
120 static void flush_buffer(AVIOContext *s)
122 if (s->buf_ptr > s->buffer) {
123 if (s->write_packet && !s->error){
124 int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
129 if(s->update_checksum){
130 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
131 s->checksum_ptr= s->buffer;
133 s->pos += s->buf_ptr - s->buffer;
135 s->buf_ptr = s->buffer;
138 void avio_w8(AVIOContext *s, int b)
141 if (s->buf_ptr >= s->buf_end)
145 void ffio_fill(AVIOContext *s, int b, int count)
148 int len = FFMIN(s->buf_end - s->buf_ptr, count);
149 memset(s->buf_ptr, b, len);
152 if (s->buf_ptr >= s->buf_end)
159 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
162 int len = FFMIN(s->buf_end - s->buf_ptr, size);
163 memcpy(s->buf_ptr, buf, len);
166 if (s->buf_ptr >= s->buf_end)
174 void avio_flush(AVIOContext *s)
180 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
184 int force = whence & AVSEEK_FORCE;
185 whence &= ~AVSEEK_FORCE;
188 return AVERROR(EINVAL);
190 pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
192 if (whence != SEEK_CUR && whence != SEEK_SET)
193 return AVERROR(EINVAL);
195 if (whence == SEEK_CUR) {
196 offset1 = pos + (s->buf_ptr - s->buffer);
201 offset1 = offset - pos;
202 if (!s->must_flush &&
203 offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
204 /* can do the seek inside the buffer */
205 s->buf_ptr = s->buffer + offset1;
206 } else if ((!s->seekable ||
207 offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
208 !s->write_flag && offset1 >= 0 &&
209 (whence != SEEK_END || force)) {
210 while(s->pos < offset && !s->eof_reached)
214 s->buf_ptr = s->buf_end + offset - s->pos;
218 #if CONFIG_MUXERS || CONFIG_NETWORK
223 #endif /* CONFIG_MUXERS || CONFIG_NETWORK */
225 return AVERROR(EPIPE);
226 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
229 s->buf_end = s->buffer;
230 s->buf_ptr = s->buffer;
237 int64_t avio_size(AVIOContext *s)
242 return AVERROR(EINVAL);
245 return AVERROR(ENOSYS);
246 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
248 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
251 s->seek(s->opaque, s->pos, SEEK_SET);
256 void avio_wl32(AVIOContext *s, unsigned int val)
259 avio_w8(s, val >> 8);
260 avio_w8(s, val >> 16);
261 avio_w8(s, val >> 24);
264 void avio_wb32(AVIOContext *s, unsigned int val)
266 avio_w8(s, val >> 24);
267 avio_w8(s, val >> 16);
268 avio_w8(s, val >> 8);
272 int avio_put_str(AVIOContext *s, const char *str)
277 avio_write(s, (const unsigned char *) str, len);
283 int avio_put_str16le(AVIOContext *s, const char *str)
285 const uint8_t *q = str;
292 GET_UTF8(ch, *q++, break;)
293 PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
300 int ff_get_v_length(uint64_t val){
309 void ff_put_v(AVIOContext *bc, uint64_t val){
310 int i= ff_get_v_length(val);
313 avio_w8(bc, 128 | (val>>(7*i)));
315 avio_w8(bc, val&127);
318 void avio_wl64(AVIOContext *s, uint64_t val)
320 avio_wl32(s, (uint32_t)(val & 0xffffffff));
321 avio_wl32(s, (uint32_t)(val >> 32));
324 void avio_wb64(AVIOContext *s, uint64_t val)
326 avio_wb32(s, (uint32_t)(val >> 32));
327 avio_wb32(s, (uint32_t)(val & 0xffffffff));
330 void avio_wl16(AVIOContext *s, unsigned int val)
333 avio_w8(s, val >> 8);
336 void avio_wb16(AVIOContext *s, unsigned int val)
338 avio_w8(s, val >> 8);
342 void avio_wl24(AVIOContext *s, unsigned int val)
344 avio_wl16(s, val & 0xffff);
345 avio_w8(s, val >> 16);
348 void avio_wb24(AVIOContext *s, unsigned int val)
350 avio_wb16(s, val >> 8);
356 static void fill_buffer(AVIOContext *s)
358 uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
359 int len= s->buffer_size - (dst - s->buffer);
360 int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
362 /* can't fill the buffer without read_packet, just set EOF if appropiate */
363 if (!s->read_packet && s->buf_ptr >= s->buf_end)
366 /* no need to do anything if EOF already reached */
370 if(s->update_checksum && dst == s->buffer){
371 if(s->buf_end > s->checksum_ptr)
372 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
373 s->checksum_ptr= s->buffer;
376 /* make buffer smaller in case it ended up large after probing */
377 if (s->buffer_size > max_buffer_size) {
378 ffio_set_buf_size(s, max_buffer_size);
380 s->checksum_ptr = dst = s->buffer;
381 len = s->buffer_size;
385 len = s->read_packet(s->opaque, dst, len);
389 /* do not modify buffer if EOF reached so that a seek back can
390 be done without rereading data */
397 s->buf_end = dst + len;
401 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
404 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
407 unsigned long ffio_get_checksum(AVIOContext *s)
409 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
410 s->update_checksum= NULL;
414 void ffio_init_checksum(AVIOContext *s,
415 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
416 unsigned long checksum)
418 s->update_checksum= update_checksum;
419 if(s->update_checksum){
420 s->checksum= checksum;
421 s->checksum_ptr= s->buf_ptr;
425 /* XXX: put an inline version */
426 int avio_r8(AVIOContext *s)
428 if (s->buf_ptr >= s->buf_end)
430 if (s->buf_ptr < s->buf_end)
431 return *s->buf_ptr++;
435 int avio_read(AVIOContext *s, unsigned char *buf, int size)
441 len = s->buf_end - s->buf_ptr;
445 if(size > s->buffer_size && !s->update_checksum){
447 len = s->read_packet(s->opaque, buf, size);
449 /* do not modify buffer if EOF reached so that a seek back can
450 be done without rereading data */
459 s->buf_ptr = s->buffer;
460 s->buf_end = s->buffer/* + len*/;
464 len = s->buf_end - s->buf_ptr;
469 memcpy(buf, s->buf_ptr, len);
476 if (s->error) return s->error;
477 if (s->eof_reached) return AVERROR_EOF;
482 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
489 len = s->buf_end - s->buf_ptr;
492 len = s->buf_end - s->buf_ptr;
496 memcpy(buf, s->buf_ptr, len);
499 if (s->error) return s->error;
500 if (s->eof_reached) return AVERROR_EOF;
505 unsigned int avio_rl16(AVIOContext *s)
509 val |= avio_r8(s) << 8;
513 unsigned int avio_rl24(AVIOContext *s)
517 val |= avio_r8(s) << 16;
521 unsigned int avio_rl32(AVIOContext *s)
525 val |= avio_rl16(s) << 16;
529 uint64_t avio_rl64(AVIOContext *s)
532 val = (uint64_t)avio_rl32(s);
533 val |= (uint64_t)avio_rl32(s) << 32;
537 unsigned int avio_rb16(AVIOContext *s)
540 val = avio_r8(s) << 8;
545 unsigned int avio_rb24(AVIOContext *s)
548 val = avio_rb16(s) << 8;
552 unsigned int avio_rb32(AVIOContext *s)
555 val = avio_rb16(s) << 16;
560 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
567 if (c && i < maxlen-1)
569 } while (c != '\n' && c);
575 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
580 return AVERROR(EINVAL);
581 // reserve 1 byte for terminating 0
582 buflen = FFMIN(buflen - 1, maxlen);
583 for (i = 0; i < buflen; i++)
584 if (!(buf[i] = avio_r8(s)))
587 for (; i < maxlen; i++)
593 #define GET_STR16(type, read) \
594 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
599 return AVERROR(EINVAL); \
600 while (ret + 1 < maxlen) {\
603 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
606 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
612 GET_STR16(le, avio_rl16)
613 GET_STR16(be, avio_rb16)
617 uint64_t avio_rb64(AVIOContext *s)
620 val = (uint64_t)avio_rb32(s) << 32;
621 val |= (uint64_t)avio_rb32(s);
625 uint64_t ffio_read_varlen(AVIOContext *bc){
631 val= (val<<7) + (tmp&127);
636 int ffio_fdopen(AVIOContext **s, URLContext *h)
639 int buffer_size, max_packet_size;
641 max_packet_size = h->max_packet_size;
642 if (max_packet_size) {
643 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
645 buffer_size = IO_BUFFER_SIZE;
647 buffer = av_malloc(buffer_size);
649 return AVERROR(ENOMEM);
651 *s = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
652 ffurl_read, ffurl_write, ffurl_seek);
655 return AVERROR(ENOMEM);
657 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
658 (*s)->max_packet_size = max_packet_size;
660 (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
661 (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
663 (*s)->av_class = &ffio_url_class;
667 int ffio_set_buf_size(AVIOContext *s, int buf_size)
670 buffer = av_malloc(buf_size);
672 return AVERROR(ENOMEM);
676 s->buffer_size = buf_size;
678 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
682 static int url_resetbuf(AVIOContext *s, int flags)
684 assert(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
686 if (flags & AVIO_FLAG_WRITE) {
687 s->buf_end = s->buffer + s->buffer_size;
690 s->buf_end = s->buffer;
696 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
698 int64_t buffer_start;
700 int overlap, new_size, alloc_size;
703 return AVERROR(EINVAL);
705 buffer_size = s->buf_end - s->buffer;
707 /* the buffers must touch or overlap */
708 if ((buffer_start = s->pos - buffer_size) > buf_size)
709 return AVERROR(EINVAL);
711 overlap = buf_size - buffer_start;
712 new_size = buf_size + buffer_size - overlap;
714 alloc_size = FFMAX(s->buffer_size, new_size);
715 if (alloc_size > buf_size)
716 if (!(buf = av_realloc(buf, alloc_size)))
717 return AVERROR(ENOMEM);
719 if (new_size > buf_size) {
720 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
725 s->buf_ptr = s->buffer = buf;
726 s->buffer_size = alloc_size;
728 s->buf_end = s->buf_ptr + buf_size;
735 int avio_open(AVIOContext **s, const char *filename, int flags)
737 return avio_open2(s, filename, flags, NULL, NULL);
740 int avio_open2(AVIOContext **s, const char *filename, int flags,
741 const AVIOInterruptCB *int_cb, AVDictionary **options)
746 err = ffurl_open(&h, filename, flags, int_cb, options);
749 err = ffio_fdopen(s, h);
757 int avio_close(AVIOContext *s)
759 URLContext *h = s->opaque;
763 return ffurl_close(h);
766 int avio_printf(AVIOContext *s, const char *fmt, ...)
773 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
775 avio_write(s, buf, strlen(buf));
779 int avio_pause(AVIOContext *s, int pause)
782 return AVERROR(ENOSYS);
783 return s->read_pause(s->opaque, pause);
786 int64_t avio_seek_time(AVIOContext *s, int stream_index,
787 int64_t timestamp, int flags)
789 URLContext *h = s->opaque;
792 return AVERROR(ENOSYS);
793 ret = s->read_seek(h, stream_index, timestamp, flags);
796 s->buf_ptr = s->buf_end; // Flush buffer
797 pos = s->seek(h, 0, SEEK_CUR);
800 else if (pos != AVERROR(ENOSYS))
806 /* output in a dynamic buffer */
808 typedef struct DynBuffer {
809 int pos, size, allocated_size;
812 uint8_t io_buffer[1];
815 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
817 DynBuffer *d = opaque;
818 unsigned new_size, new_allocated_size;
820 /* reallocate buffer if needed */
821 new_size = d->pos + buf_size;
822 new_allocated_size = d->allocated_size;
823 if(new_size < d->pos || new_size > INT_MAX/2)
825 while (new_size > new_allocated_size) {
826 if (!new_allocated_size)
827 new_allocated_size = new_size;
829 new_allocated_size += new_allocated_size / 2 + 1;
832 if (new_allocated_size > d->allocated_size) {
833 d->buffer = av_realloc(d->buffer, new_allocated_size);
834 if(d->buffer == NULL)
835 return AVERROR(ENOMEM);
836 d->allocated_size = new_allocated_size;
838 memcpy(d->buffer + d->pos, buf, buf_size);
840 if (d->pos > d->size)
845 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
847 unsigned char buf1[4];
850 /* packetized write: output the header */
851 AV_WB32(buf1, buf_size);
852 ret= dyn_buf_write(opaque, buf1, 4);
857 return dyn_buf_write(opaque, buf, buf_size);
860 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
862 DynBuffer *d = opaque;
864 if (whence == SEEK_CUR)
866 else if (whence == SEEK_END)
868 if (offset < 0 || offset > 0x7fffffffLL)
874 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
877 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
879 if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
881 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
883 return AVERROR(ENOMEM);
884 d->io_buffer_size = io_buffer_size;
885 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
886 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
887 max_packet_size ? NULL : dyn_buf_seek);
890 return AVERROR(ENOMEM);
892 (*s)->max_packet_size = max_packet_size;
896 int avio_open_dyn_buf(AVIOContext **s)
898 return url_open_dyn_buf_internal(s, 0);
901 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
903 if (max_packet_size <= 0)
905 return url_open_dyn_buf_internal(s, max_packet_size);
908 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
910 DynBuffer *d = s->opaque;
912 static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
915 /* don't attempt to pad fixed-size packet buffers */
916 if (!s->max_packet_size) {
917 avio_write(s, padbuf, sizeof(padbuf));
918 padding = FF_INPUT_BUFFER_PADDING_SIZE;
923 *pbuffer = d->buffer;
927 return size - padding;