2 * Buffered I/O for ffmpeg system
3 * Copyright (c) 2000,2001 Fabrice Bellard
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define IO_BUFFER_SIZE 32768
25 int init_put_byte(ByteIOContext *s,
26 unsigned char *buffer,
30 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
31 void (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
32 int (*seek)(void *opaque, offset_t offset, int whence))
35 s->buffer_size = buffer_size;
37 s->write_flag = write_flag;
41 s->buf_end = buffer + buffer_size;
43 s->write_packet = write_packet;
44 s->read_packet = read_packet;
50 s->max_packet_size = 0;
51 s->checksum_ptr= NULL;
52 s->update_checksum= NULL;
57 #ifdef CONFIG_ENCODERS
58 static void flush_buffer(ByteIOContext *s)
60 if (s->buf_ptr > s->buffer) {
62 s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
64 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
65 s->checksum_ptr= s->buffer;
67 s->pos += s->buf_ptr - s->buffer;
69 s->buf_ptr = s->buffer;
72 void put_byte(ByteIOContext *s, int b)
75 if (s->buf_ptr >= s->buf_end)
79 void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
84 len = (s->buf_end - s->buf_ptr);
87 memcpy(s->buf_ptr, buf, len);
90 if (s->buf_ptr >= s->buf_end)
98 void put_flush_packet(ByteIOContext *s)
103 #endif //CONFIG_ENCODERS
105 offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence)
109 if (whence != SEEK_CUR && whence != SEEK_SET)
112 #ifdef CONFIG_ENCODERS
114 if (whence == SEEK_CUR) {
115 offset1 = s->pos + (s->buf_ptr - s->buffer);
120 offset1 = offset - s->pos;
121 if (!s->must_flush &&
122 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
123 /* can do the seek inside the buffer */
124 s->buf_ptr = s->buffer + offset1;
130 s->buf_ptr = s->buffer;
131 s->seek(s->opaque, offset, SEEK_SET);
135 #endif //CONFIG_ENCODERS
137 if (whence == SEEK_CUR) {
138 offset1 = s->pos - (s->buf_end - s->buffer) + (s->buf_ptr - s->buffer);
143 offset1 = offset - (s->pos - (s->buf_end - s->buffer));
144 if (offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
145 /* can do the seek inside the buffer */
146 s->buf_ptr = s->buffer + offset1;
150 s->buf_ptr = s->buffer;
151 s->buf_end = s->buffer;
152 s->seek(s->opaque, offset, SEEK_SET);
160 void url_fskip(ByteIOContext *s, offset_t offset)
162 url_fseek(s, offset, SEEK_CUR);
165 offset_t url_ftell(ByteIOContext *s)
167 return url_fseek(s, 0, SEEK_CUR);
170 int url_feof(ByteIOContext *s)
172 return s->eof_reached;
175 #ifdef CONFIG_ENCODERS
176 void put_le32(ByteIOContext *s, unsigned int val)
179 put_byte(s, val >> 8);
180 put_byte(s, val >> 16);
181 put_byte(s, val >> 24);
184 void put_be32(ByteIOContext *s, unsigned int val)
186 put_byte(s, val >> 24);
187 put_byte(s, val >> 16);
188 put_byte(s, val >> 8);
192 /* IEEE format is assumed */
193 void put_be64_double(ByteIOContext *s, double val)
203 void put_strz(ByteIOContext *s, const char *str)
206 put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
211 void put_le64(ByteIOContext *s, uint64_t val)
213 put_le32(s, (uint32_t)(val & 0xffffffff));
214 put_le32(s, (uint32_t)(val >> 32));
217 void put_be64(ByteIOContext *s, uint64_t val)
219 put_be32(s, (uint32_t)(val >> 32));
220 put_be32(s, (uint32_t)(val & 0xffffffff));
223 void put_le16(ByteIOContext *s, unsigned int val)
226 put_byte(s, val >> 8);
229 void put_be16(ByteIOContext *s, unsigned int val)
231 put_byte(s, val >> 8);
235 void put_tag(ByteIOContext *s, const char *tag)
241 #endif //CONFIG_ENCODERS
245 static void fill_buffer(ByteIOContext *s)
249 /* no need to do anything if EOF already reached */
254 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
255 s->checksum_ptr= s->buffer;
258 len = s->read_packet(s->opaque, s->buffer, s->buffer_size);
260 /* do not modify buffer if EOF reached so that a seek back can
261 be done without rereading data */
265 s->buf_ptr = s->buffer;
266 s->buf_end = s->buffer + len;
270 unsigned long get_checksum(ByteIOContext *s){
271 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
272 s->checksum_ptr= NULL;
276 void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum){
277 s->update_checksum= update_checksum;
278 s->checksum= s->update_checksum(checksum, NULL, 0);
279 s->checksum_ptr= s->buf_ptr;
282 /* NOTE: return 0 if EOF, so you cannot use it if EOF handling is
284 /* XXX: put an inline version */
285 int get_byte(ByteIOContext *s)
287 if (s->buf_ptr < s->buf_end) {
288 return *s->buf_ptr++;
291 if (s->buf_ptr < s->buf_end)
292 return *s->buf_ptr++;
298 /* NOTE: return URL_EOF (-1) if EOF */
299 int url_fgetc(ByteIOContext *s)
301 if (s->buf_ptr < s->buf_end) {
302 return *s->buf_ptr++;
305 if (s->buf_ptr < s->buf_end)
306 return *s->buf_ptr++;
312 int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
318 len = s->buf_end - s->buf_ptr;
323 len = s->buf_end - s->buf_ptr;
327 memcpy(buf, s->buf_ptr, len);
336 int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
340 len = s->buf_end - s->buf_ptr;
343 len = s->buf_end - s->buf_ptr;
347 memcpy(buf, s->buf_ptr, len);
352 unsigned int get_le16(ByteIOContext *s)
356 val |= get_byte(s) << 8;
360 unsigned int get_le32(ByteIOContext *s)
364 val |= get_byte(s) << 8;
365 val |= get_byte(s) << 16;
366 val |= get_byte(s) << 24;
370 uint64_t get_le64(ByteIOContext *s)
373 val = (uint64_t)get_le32(s);
374 val |= (uint64_t)get_le32(s) << 32;
378 unsigned int get_be16(ByteIOContext *s)
381 val = get_byte(s) << 8;
386 unsigned int get_be32(ByteIOContext *s)
389 val = get_byte(s) << 24;
390 val |= get_byte(s) << 16;
391 val |= get_byte(s) << 8;
396 double get_be64_double(ByteIOContext *s)
407 char *get_strz(ByteIOContext *s, char *buf, int maxlen)
412 while ((c = get_byte(s))) {
417 buf[i] = 0; /* Ensure null terminated, but may be truncated */
422 uint64_t get_be64(ByteIOContext *s)
425 val = (uint64_t)get_be32(s) << 32;
426 val |= (uint64_t)get_be32(s);
430 /* link with avio functions */
432 #ifdef CONFIG_ENCODERS
433 static void url_write_packet(void *opaque, uint8_t *buf, int buf_size)
435 URLContext *h = opaque;
436 url_write(h, buf, buf_size);
439 #define url_write_packet NULL
440 #endif //CONFIG_ENCODERS
442 static int url_read_packet(void *opaque, uint8_t *buf, int buf_size)
444 URLContext *h = opaque;
445 return url_read(h, buf, buf_size);
448 static int url_seek_packet(void *opaque, int64_t offset, int whence)
450 URLContext *h = opaque;
451 url_seek(h, offset, whence);
455 int url_fdopen(ByteIOContext *s, URLContext *h)
458 int buffer_size, max_packet_size;
461 max_packet_size = url_get_max_packet_size(h);
462 if (max_packet_size) {
463 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
465 buffer_size = IO_BUFFER_SIZE;
467 buffer = av_malloc(buffer_size);
471 if (init_put_byte(s, buffer, buffer_size,
472 (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
473 url_read_packet, url_write_packet, url_seek_packet) < 0) {
477 s->is_streamed = h->is_streamed;
478 s->max_packet_size = max_packet_size;
482 /* XXX: must be called before any I/O */
483 int url_setbufsize(ByteIOContext *s, int buf_size)
486 buffer = av_malloc(buf_size);
492 s->buffer_size = buf_size;
497 s->buf_end = buffer + buf_size;
501 /* NOTE: when opened as read/write, the buffers are only used for
503 int url_fopen(ByteIOContext *s, const char *filename, int flags)
508 err = url_open(&h, filename, flags);
511 err = url_fdopen(s, h);
519 int url_fclose(ByteIOContext *s)
521 URLContext *h = s->opaque;
524 memset(s, 0, sizeof(ByteIOContext));
528 URLContext *url_fileno(ByteIOContext *s)
533 #ifdef CONFIG_ENCODERS
534 /* XXX: currently size is limited */
535 int url_fprintf(ByteIOContext *s, const char *fmt, ...)
542 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
544 put_buffer(s, buf, strlen(buf));
547 #endif //CONFIG_ENCODERS
549 /* note: unlike fgets, the EOL character is not returned and a whole
550 line is parsed. return NULL if first char read was EOF */
551 char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
561 if (c == EOF || c == '\n')
563 if ((q - buf) < buf_size - 1)
573 * Return the maximum packet size associated to packetized buffered file
574 * handle. If the file is not packetized (stream like http or file on
575 * disk), then 0 is returned.
577 * @param h buffered file handle
578 * @return maximum packet size in bytes
580 int url_fget_max_packet_size(ByteIOContext *s)
582 return s->max_packet_size;
585 #ifdef CONFIG_ENCODERS
586 /* buffer handling */
587 int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags)
589 return init_put_byte(s, buf, buf_size,
590 (flags & URL_WRONLY || flags & URL_RDWR),
591 NULL, NULL, NULL, NULL);
594 /* return the written or read size */
595 int url_close_buf(ByteIOContext *s)
598 return s->buf_ptr - s->buffer;
601 /* output in a dynamic buffer */
603 typedef struct DynBuffer {
604 int pos, size, allocated_size;
607 uint8_t io_buffer[1];
610 static void dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
612 DynBuffer *d = opaque;
613 int new_size, new_allocated_size;
615 /* reallocate buffer if needed */
616 new_size = d->pos + buf_size;
617 new_allocated_size = d->allocated_size;
618 while (new_size > new_allocated_size) {
619 if (!new_allocated_size)
620 new_allocated_size = new_size;
622 new_allocated_size = (new_allocated_size * 3) / 2 + 1;
625 if (new_allocated_size > d->allocated_size) {
626 d->buffer = av_realloc(d->buffer, new_allocated_size);
627 if(d->buffer == NULL)
629 d->allocated_size = new_allocated_size;
631 memcpy(d->buffer + d->pos, buf, buf_size);
633 if (d->pos > d->size)
637 static void dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
639 unsigned char buf1[4];
641 /* packetized write: output the header */
642 buf1[0] = (buf_size >> 24);
643 buf1[1] = (buf_size >> 16);
644 buf1[2] = (buf_size >> 8);
645 buf1[3] = (buf_size);
646 dyn_buf_write(opaque, buf1, 4);
649 dyn_buf_write(opaque, buf, buf_size);
652 static int dyn_buf_seek(void *opaque, offset_t offset, int whence)
654 DynBuffer *d = opaque;
656 if (whence == SEEK_CUR)
658 else if (whence == SEEK_END)
660 if (offset < 0 || offset > 0x7fffffffLL)
666 static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size)
669 int io_buffer_size, ret;
672 io_buffer_size = max_packet_size;
674 io_buffer_size = 1024;
676 d = av_malloc(sizeof(DynBuffer) + io_buffer_size);
679 d->io_buffer_size = io_buffer_size;
683 d->allocated_size = 0;
684 ret = init_put_byte(s, d->io_buffer, io_buffer_size,
686 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
687 max_packet_size ? NULL : dyn_buf_seek);
689 s->max_packet_size = max_packet_size;
695 * Open a write only memory stream.
697 * @param s new IO context
698 * @return zero if no error.
700 int url_open_dyn_buf(ByteIOContext *s)
702 return url_open_dyn_buf_internal(s, 0);
706 * Open a write only packetized memory stream with a maximum packet
707 * size of 'max_packet_size'. The stream is stored in a memory buffer
708 * with a big endian 4 byte header giving the packet size in bytes.
710 * @param s new IO context
711 * @param max_packet_size maximum packet size (must be > 0)
712 * @return zero if no error.
714 int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size)
716 if (max_packet_size <= 0)
718 return url_open_dyn_buf_internal(s, max_packet_size);
722 * Return the written size and a pointer to the buffer. The buffer
723 * must be freed with av_free().
724 * @param s IO context
725 * @param pointer to a byte buffer
726 * @return the length of the byte buffer
728 int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
730 DynBuffer *d = s->opaque;
735 *pbuffer = d->buffer;
740 #endif //CONFIG_ENCODERS