2 * Buffered I/O for ffmpeg system
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
25 #define IO_BUFFER_SIZE 32768
27 static void fill_buffer(ByteIOContext *s);
29 int init_put_byte(ByteIOContext *s,
30 unsigned char *buffer,
34 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
35 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
36 offset_t (*seek)(void *opaque, offset_t offset, int whence))
39 s->buffer_size = buffer_size;
41 s->write_flag = write_flag;
45 s->buf_end = buffer + buffer_size;
47 s->write_packet = write_packet;
48 s->read_packet = read_packet;
55 s->max_packet_size = 0;
56 s->update_checksum= NULL;
60 static void flush_buffer(ByteIOContext *s)
62 if (s->buf_ptr > s->buffer) {
63 if (s->write_packet && !s->error){
64 int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
69 if(s->update_checksum){
70 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
71 s->checksum_ptr= s->buffer;
73 s->pos += s->buf_ptr - s->buffer;
75 s->buf_ptr = s->buffer;
78 void put_byte(ByteIOContext *s, int b)
81 if (s->buf_ptr >= s->buf_end)
85 void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
90 len = (s->buf_end - s->buf_ptr);
93 memcpy(s->buf_ptr, buf, len);
96 if (s->buf_ptr >= s->buf_end)
104 void put_flush_packet(ByteIOContext *s)
110 offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence)
113 offset_t pos= s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
115 if (whence != SEEK_CUR && whence != SEEK_SET)
118 if (whence == SEEK_CUR) {
119 offset1 = pos + (s->buf_ptr - s->buffer);
124 offset1 = offset - pos;
125 if (!s->must_flush &&
126 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
127 /* can do the seek inside the buffer */
128 s->buf_ptr = s->buffer + offset1;
129 } else if(s->is_streamed && !s->write_flag &&
130 offset1 >= 0 && offset1 < (s->buf_end - s->buffer) + (1<<16)){
131 while(s->pos < offset && !s->eof_reached)
133 s->buf_ptr = s->buf_end + offset - s->pos;
140 #endif //CONFIG_MUXERS
142 s->buf_end = s->buffer;
144 s->buf_ptr = s->buffer;
145 if (!s->seek || s->seek(s->opaque, offset, SEEK_SET) == (offset_t)-EPIPE)
153 void url_fskip(ByteIOContext *s, offset_t offset)
155 url_fseek(s, offset, SEEK_CUR);
158 offset_t url_ftell(ByteIOContext *s)
160 return url_fseek(s, 0, SEEK_CUR);
163 offset_t url_fsize(ByteIOContext *s)
169 size = s->seek(s->opaque, -1, SEEK_END) + 1;
170 s->seek(s->opaque, s->pos, SEEK_SET);
174 int url_feof(ByteIOContext *s)
176 return s->eof_reached;
179 int url_ferror(ByteIOContext *s)
184 #if defined(CONFIG_MUXERS) || defined(CONFIG_PROTOCOLS)
185 void put_le32(ByteIOContext *s, unsigned int val)
188 put_byte(s, val >> 8);
189 put_byte(s, val >> 16);
190 put_byte(s, val >> 24);
193 void put_be32(ByteIOContext *s, unsigned int val)
195 put_byte(s, val >> 24);
196 put_byte(s, val >> 16);
197 put_byte(s, val >> 8);
201 void put_strz(ByteIOContext *s, const char *str)
204 put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
209 void put_le64(ByteIOContext *s, uint64_t val)
211 put_le32(s, (uint32_t)(val & 0xffffffff));
212 put_le32(s, (uint32_t)(val >> 32));
215 void put_be64(ByteIOContext *s, uint64_t val)
217 put_be32(s, (uint32_t)(val >> 32));
218 put_be32(s, (uint32_t)(val & 0xffffffff));
221 void put_le16(ByteIOContext *s, unsigned int val)
224 put_byte(s, val >> 8);
227 void put_be16(ByteIOContext *s, unsigned int val)
229 put_byte(s, val >> 8);
233 void put_le24(ByteIOContext *s, unsigned int val)
235 put_le16(s, val & 0xffff);
236 put_byte(s, val >> 16);
239 void put_be24(ByteIOContext *s, unsigned int val)
241 put_be16(s, val >> 8);
245 void put_tag(ByteIOContext *s, const char *tag)
251 #endif //CONFIG_MUXERS || CONFIG_PROTOCOLS
255 static void fill_buffer(ByteIOContext *s)
259 /* no need to do anything if EOF already reached */
263 if(s->update_checksum){
264 if(s->buf_end > s->checksum_ptr)
265 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
266 s->checksum_ptr= s->buffer;
269 len = s->read_packet(s->opaque, s->buffer, s->buffer_size);
271 /* do not modify buffer if EOF reached so that a seek back can
272 be done without rereading data */
278 s->buf_ptr = s->buffer;
279 s->buf_end = s->buffer + len;
283 unsigned long get_checksum(ByteIOContext *s){
284 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
285 s->update_checksum= NULL;
289 void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum){
290 s->update_checksum= update_checksum;
291 if(s->update_checksum){
292 s->checksum= checksum;
293 s->checksum_ptr= s->buf_ptr;
297 /* NOTE: return 0 if EOF, so you cannot use it if EOF handling is
299 /* XXX: put an inline version */
300 int get_byte(ByteIOContext *s)
302 if (s->buf_ptr < s->buf_end) {
303 return *s->buf_ptr++;
306 if (s->buf_ptr < s->buf_end)
307 return *s->buf_ptr++;
313 /* NOTE: return URL_EOF (-1) if EOF */
314 int url_fgetc(ByteIOContext *s)
316 if (s->buf_ptr < s->buf_end) {
317 return *s->buf_ptr++;
320 if (s->buf_ptr < s->buf_end)
321 return *s->buf_ptr++;
327 int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
333 len = s->buf_end - s->buf_ptr;
337 if(size > s->buffer_size && !s->update_checksum){
338 len = s->read_packet(s->opaque, buf, size);
340 /* do not modify buffer if EOF reached so that a seek back can
341 be done without rereading data */
350 s->buf_ptr = s->buffer;
351 s->buf_end = s->buffer/* + len*/;
355 len = s->buf_end - s->buf_ptr;
360 memcpy(buf, s->buf_ptr, len);
369 int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
376 len = s->buf_end - s->buf_ptr;
379 len = s->buf_end - s->buf_ptr;
383 memcpy(buf, s->buf_ptr, len);
388 unsigned int get_le16(ByteIOContext *s)
392 val |= get_byte(s) << 8;
396 unsigned int get_le24(ByteIOContext *s)
400 val |= get_byte(s) << 16;
404 unsigned int get_le32(ByteIOContext *s)
408 val |= get_le16(s) << 16;
412 uint64_t get_le64(ByteIOContext *s)
415 val = (uint64_t)get_le32(s);
416 val |= (uint64_t)get_le32(s) << 32;
420 unsigned int get_be16(ByteIOContext *s)
423 val = get_byte(s) << 8;
428 unsigned int get_be24(ByteIOContext *s)
431 val = get_be16(s) << 8;
435 unsigned int get_be32(ByteIOContext *s)
438 val = get_be16(s) << 16;
443 char *get_strz(ByteIOContext *s, char *buf, int maxlen)
448 while ((c = get_byte(s))) {
453 buf[i] = 0; /* Ensure null terminated, but may be truncated */
458 uint64_t get_be64(ByteIOContext *s)
461 val = (uint64_t)get_be32(s) << 32;
462 val |= (uint64_t)get_be32(s);
466 /* link with avio functions */
469 static int url_write_packet(void *opaque, uint8_t *buf, int buf_size)
471 URLContext *h = opaque;
472 return url_write(h, buf, buf_size);
475 #define url_write_packet NULL
476 #endif //CONFIG_MUXERS
478 static int url_read_packet(void *opaque, uint8_t *buf, int buf_size)
480 URLContext *h = opaque;
481 return url_read(h, buf, buf_size);
484 static offset_t url_seek_packet(void *opaque, offset_t offset, int whence)
486 URLContext *h = opaque;
487 return url_seek(h, offset, whence);
491 int url_fdopen(ByteIOContext *s, URLContext *h)
494 int buffer_size, max_packet_size;
497 max_packet_size = url_get_max_packet_size(h);
498 if (max_packet_size) {
499 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
501 buffer_size = IO_BUFFER_SIZE;
503 buffer = av_malloc(buffer_size);
507 if (init_put_byte(s, buffer, buffer_size,
508 (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
509 url_read_packet, url_write_packet, url_seek_packet) < 0) {
513 s->is_streamed = h->is_streamed;
514 s->max_packet_size = max_packet_size;
518 /* XXX: must be called before any I/O */
519 int url_setbufsize(ByteIOContext *s, int buf_size)
522 buffer = av_malloc(buf_size);
528 s->buffer_size = buf_size;
533 s->buf_end = buffer + buf_size;
537 /* NOTE: when opened as read/write, the buffers are only used for
539 int url_fopen(ByteIOContext *s, const char *filename, int flags)
544 err = url_open(&h, filename, flags);
547 err = url_fdopen(s, h);
555 int url_fclose(ByteIOContext *s)
557 URLContext *h = s->opaque;
560 memset(s, 0, sizeof(ByteIOContext));
564 URLContext *url_fileno(ByteIOContext *s)
570 /* XXX: currently size is limited */
571 int url_fprintf(ByteIOContext *s, const char *fmt, ...)
578 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
580 put_buffer(s, buf, strlen(buf));
583 #endif //CONFIG_MUXERS
585 /* note: unlike fgets, the EOL character is not returned and a whole
586 line is parsed. return NULL if first char read was EOF */
587 char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
597 if (c == EOF || c == '\n')
599 if ((q - buf) < buf_size - 1)
609 * Return the maximum packet size associated to packetized buffered file
610 * handle. If the file is not packetized (stream like http or file on
611 * disk), then 0 is returned.
613 * @param h buffered file handle
614 * @return maximum packet size in bytes
616 int url_fget_max_packet_size(ByteIOContext *s)
618 return s->max_packet_size;
622 /* buffer handling */
623 int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags)
625 return init_put_byte(s, buf, buf_size,
626 (flags & URL_WRONLY || flags & URL_RDWR),
627 NULL, NULL, NULL, NULL);
630 /* return the written or read size */
631 int url_close_buf(ByteIOContext *s)
634 return s->buf_ptr - s->buffer;
637 /* output in a dynamic buffer */
639 typedef struct DynBuffer {
640 int pos, size, allocated_size;
643 uint8_t io_buffer[1];
646 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
648 DynBuffer *d = opaque;
649 int new_size, new_allocated_size;
651 /* reallocate buffer if needed */
652 new_size = d->pos + buf_size;
653 new_allocated_size = d->allocated_size;
654 if(new_size < d->pos || new_size > INT_MAX/2)
656 while (new_size > new_allocated_size) {
657 if (!new_allocated_size)
658 new_allocated_size = new_size;
660 new_allocated_size += new_allocated_size / 2 + 1;
663 if (new_allocated_size > d->allocated_size) {
664 d->buffer = av_realloc(d->buffer, new_allocated_size);
665 if(d->buffer == NULL)
667 d->allocated_size = new_allocated_size;
669 memcpy(d->buffer + d->pos, buf, buf_size);
671 if (d->pos > d->size)
676 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
678 unsigned char buf1[4];
681 /* packetized write: output the header */
682 buf1[0] = (buf_size >> 24);
683 buf1[1] = (buf_size >> 16);
684 buf1[2] = (buf_size >> 8);
685 buf1[3] = (buf_size);
686 ret= dyn_buf_write(opaque, buf1, 4);
691 return dyn_buf_write(opaque, buf, buf_size);
694 static offset_t dyn_buf_seek(void *opaque, offset_t offset, int whence)
696 DynBuffer *d = opaque;
698 if (whence == SEEK_CUR)
700 else if (whence == SEEK_END)
702 if (offset < 0 || offset > 0x7fffffffLL)
708 static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size)
711 int io_buffer_size, ret;
714 io_buffer_size = max_packet_size;
716 io_buffer_size = 1024;
718 if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
720 d = av_malloc(sizeof(DynBuffer) + io_buffer_size);
723 d->io_buffer_size = io_buffer_size;
727 d->allocated_size = 0;
728 ret = init_put_byte(s, d->io_buffer, io_buffer_size,
730 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
731 max_packet_size ? NULL : dyn_buf_seek);
733 s->max_packet_size = max_packet_size;
739 * Open a write only memory stream.
741 * @param s new IO context
742 * @return zero if no error.
744 int url_open_dyn_buf(ByteIOContext *s)
746 return url_open_dyn_buf_internal(s, 0);
750 * Open a write only packetized memory stream with a maximum packet
751 * size of 'max_packet_size'. The stream is stored in a memory buffer
752 * with a big endian 4 byte header giving the packet size in bytes.
754 * @param s new IO context
755 * @param max_packet_size maximum packet size (must be > 0)
756 * @return zero if no error.
758 int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size)
760 if (max_packet_size <= 0)
762 return url_open_dyn_buf_internal(s, max_packet_size);
766 * Return the written size and a pointer to the buffer. The buffer
767 * must be freed with av_free().
768 * @param s IO context
769 * @param pointer to a byte buffer
770 * @return the length of the byte buffer
772 int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
774 DynBuffer *d = s->opaque;
779 *pbuffer = d->buffer;
784 #endif //CONFIG_MUXERS