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;
55 #ifdef CONFIG_ENCODERS
56 static void flush_buffer(ByteIOContext *s)
58 if (s->buf_ptr > s->buffer) {
60 s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
61 s->pos += s->buf_ptr - s->buffer;
63 s->buf_ptr = s->buffer;
66 void put_byte(ByteIOContext *s, int b)
69 if (s->buf_ptr >= s->buf_end)
73 void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
78 len = (s->buf_end - s->buf_ptr);
81 memcpy(s->buf_ptr, buf, len);
84 if (s->buf_ptr >= s->buf_end)
92 void put_flush_packet(ByteIOContext *s)
97 #endif //CONFIG_ENCODERS
99 offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence)
103 if (whence != SEEK_CUR && whence != SEEK_SET)
106 #ifdef CONFIG_ENCODERS
108 if (whence == SEEK_CUR) {
109 offset1 = s->pos + (s->buf_ptr - s->buffer);
114 offset1 = offset - s->pos;
115 if (!s->must_flush &&
116 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
117 /* can do the seek inside the buffer */
118 s->buf_ptr = s->buffer + offset1;
124 s->buf_ptr = s->buffer;
125 s->seek(s->opaque, offset, SEEK_SET);
129 #endif //CONFIG_ENCODERS
131 if (whence == SEEK_CUR) {
132 offset1 = s->pos - (s->buf_end - s->buffer) + (s->buf_ptr - s->buffer);
137 offset1 = offset - (s->pos - (s->buf_end - s->buffer));
138 if (offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
139 /* can do the seek inside the buffer */
140 s->buf_ptr = s->buffer + offset1;
144 s->buf_ptr = s->buffer;
145 s->buf_end = s->buffer;
146 s->seek(s->opaque, offset, SEEK_SET);
154 void url_fskip(ByteIOContext *s, offset_t offset)
156 url_fseek(s, offset, SEEK_CUR);
159 offset_t url_ftell(ByteIOContext *s)
161 return url_fseek(s, 0, SEEK_CUR);
164 int url_feof(ByteIOContext *s)
166 return s->eof_reached;
169 #ifdef CONFIG_ENCODERS
170 void put_le32(ByteIOContext *s, unsigned int val)
173 put_byte(s, val >> 8);
174 put_byte(s, val >> 16);
175 put_byte(s, val >> 24);
178 void put_be32(ByteIOContext *s, unsigned int val)
180 put_byte(s, val >> 24);
181 put_byte(s, val >> 16);
182 put_byte(s, val >> 8);
186 /* IEEE format is assumed */
187 void put_be64_double(ByteIOContext *s, double val)
197 void put_strz(ByteIOContext *s, const char *str)
200 put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
205 void put_le64(ByteIOContext *s, uint64_t val)
207 put_le32(s, (uint32_t)(val & 0xffffffff));
208 put_le32(s, (uint32_t)(val >> 32));
211 void put_be64(ByteIOContext *s, uint64_t val)
213 put_be32(s, (uint32_t)(val >> 32));
214 put_be32(s, (uint32_t)(val & 0xffffffff));
217 void put_le16(ByteIOContext *s, unsigned int val)
220 put_byte(s, val >> 8);
223 void put_be16(ByteIOContext *s, unsigned int val)
225 put_byte(s, val >> 8);
229 void put_tag(ByteIOContext *s, const char *tag)
235 #endif //CONFIG_ENCODERS
239 static void fill_buffer(ByteIOContext *s)
243 /* no need to do anything if EOF already reached */
246 len = s->read_packet(s->opaque, s->buffer, s->buffer_size);
248 /* do not modify buffer if EOF reached so that a seek back can
249 be done without rereading data */
253 s->buf_ptr = s->buffer;
254 s->buf_end = s->buffer + len;
258 /* NOTE: return 0 if EOF, so you cannot use it if EOF handling is
260 /* XXX: put an inline version */
261 int get_byte(ByteIOContext *s)
263 if (s->buf_ptr < s->buf_end) {
264 return *s->buf_ptr++;
267 if (s->buf_ptr < s->buf_end)
268 return *s->buf_ptr++;
274 /* NOTE: return URL_EOF (-1) if EOF */
275 int url_fgetc(ByteIOContext *s)
277 if (s->buf_ptr < s->buf_end) {
278 return *s->buf_ptr++;
281 if (s->buf_ptr < s->buf_end)
282 return *s->buf_ptr++;
288 int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
294 len = s->buf_end - s->buf_ptr;
299 len = s->buf_end - s->buf_ptr;
303 memcpy(buf, s->buf_ptr, len);
312 unsigned int get_le16(ByteIOContext *s)
316 val |= get_byte(s) << 8;
320 unsigned int get_le32(ByteIOContext *s)
324 val |= get_byte(s) << 8;
325 val |= get_byte(s) << 16;
326 val |= get_byte(s) << 24;
330 uint64_t get_le64(ByteIOContext *s)
333 val = (uint64_t)get_le32(s);
334 val |= (uint64_t)get_le32(s) << 32;
338 unsigned int get_be16(ByteIOContext *s)
341 val = get_byte(s) << 8;
346 unsigned int get_be32(ByteIOContext *s)
349 val = get_byte(s) << 24;
350 val |= get_byte(s) << 16;
351 val |= get_byte(s) << 8;
356 double get_be64_double(ByteIOContext *s)
367 char *get_strz(ByteIOContext *s, char *buf, int maxlen)
372 while ((c = get_byte(s))) {
377 buf[i] = 0; /* Ensure null terminated, but may be truncated */
382 uint64_t get_be64(ByteIOContext *s)
385 val = (uint64_t)get_be32(s) << 32;
386 val |= (uint64_t)get_be32(s);
390 /* link with avio functions */
392 #ifdef CONFIG_ENCODERS
393 static void url_write_packet(void *opaque, uint8_t *buf, int buf_size)
395 URLContext *h = opaque;
396 url_write(h, buf, buf_size);
399 #define url_write_packet NULL
400 #endif //CONFIG_ENCODERS
402 static int url_read_packet(void *opaque, uint8_t *buf, int buf_size)
404 URLContext *h = opaque;
405 return url_read(h, buf, buf_size);
408 static int url_seek_packet(void *opaque, int64_t offset, int whence)
410 URLContext *h = opaque;
411 url_seek(h, offset, whence);
415 int url_fdopen(ByteIOContext *s, URLContext *h)
418 int buffer_size, max_packet_size;
421 max_packet_size = url_get_max_packet_size(h);
422 if (max_packet_size) {
423 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
425 buffer_size = IO_BUFFER_SIZE;
427 buffer = av_malloc(buffer_size);
431 if (init_put_byte(s, buffer, buffer_size,
432 (h->flags & URL_WRONLY) != 0, h,
433 url_read_packet, url_write_packet, url_seek_packet) < 0) {
437 s->is_streamed = h->is_streamed;
438 s->max_packet_size = max_packet_size;
442 /* XXX: must be called before any I/O */
443 int url_setbufsize(ByteIOContext *s, int buf_size)
446 buffer = av_malloc(buf_size);
452 s->buffer_size = buf_size;
457 s->buf_end = buffer + buf_size;
461 /* NOTE: when opened as read/write, the buffers are only used for
463 int url_fopen(ByteIOContext *s, const char *filename, int flags)
468 err = url_open(&h, filename, flags);
471 err = url_fdopen(s, h);
479 int url_fclose(ByteIOContext *s)
481 URLContext *h = s->opaque;
484 memset(s, 0, sizeof(ByteIOContext));
488 URLContext *url_fileno(ByteIOContext *s)
493 #ifdef CONFIG_ENCODERS
494 /* XXX: currently size is limited */
495 int url_fprintf(ByteIOContext *s, const char *fmt, ...)
502 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
504 put_buffer(s, buf, strlen(buf));
507 #endif //CONFIG_ENCODERS
509 /* note: unlike fgets, the EOL character is not returned and a whole
510 line is parsed. return NULL if first char read was EOF */
511 char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
521 if (c == EOF || c == '\n')
523 if ((q - buf) < buf_size - 1)
533 * Return the maximum packet size associated to packetized buffered file
534 * handle. If the file is not packetized (stream like http or file on
535 * disk), then 0 is returned.
537 * @param h buffered file handle
538 * @return maximum packet size in bytes
540 int url_fget_max_packet_size(ByteIOContext *s)
542 return s->max_packet_size;
545 #ifdef CONFIG_ENCODERS
546 /* buffer handling */
547 int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags)
549 return init_put_byte(s, buf, buf_size,
550 (flags & URL_WRONLY) != 0, NULL, NULL, NULL, NULL);
553 /* return the written or read size */
554 int url_close_buf(ByteIOContext *s)
557 return s->buf_ptr - s->buffer;
560 /* output in a dynamic buffer */
562 typedef struct DynBuffer {
563 int pos, size, allocated_size;
566 uint8_t io_buffer[1];
569 static void dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
571 DynBuffer *d = opaque;
572 int new_size, new_allocated_size;
574 /* reallocate buffer if needed */
575 new_size = d->pos + buf_size;
576 new_allocated_size = d->allocated_size;
577 while (new_size > new_allocated_size) {
578 if (!new_allocated_size)
579 new_allocated_size = new_size;
581 new_allocated_size = (new_allocated_size * 3) / 2 + 1;
584 if (new_allocated_size > d->allocated_size) {
585 d->buffer = av_realloc(d->buffer, new_allocated_size);
586 if(d->buffer == NULL)
588 d->allocated_size = new_allocated_size;
590 memcpy(d->buffer + d->pos, buf, buf_size);
592 if (d->pos > d->size)
596 static void dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
598 unsigned char buf1[4];
600 /* packetized write: output the header */
601 buf1[0] = (buf_size >> 24);
602 buf1[1] = (buf_size >> 16);
603 buf1[2] = (buf_size >> 8);
604 buf1[3] = (buf_size);
605 dyn_buf_write(opaque, buf1, 4);
608 dyn_buf_write(opaque, buf, buf_size);
611 static int dyn_buf_seek(void *opaque, offset_t offset, int whence)
613 DynBuffer *d = opaque;
615 if (whence == SEEK_CUR)
617 else if (whence == SEEK_END)
619 if (offset < 0 || offset > 0x7fffffffLL)
625 static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size)
628 int io_buffer_size, ret;
631 io_buffer_size = max_packet_size;
633 io_buffer_size = 1024;
635 d = av_malloc(sizeof(DynBuffer) + io_buffer_size);
638 d->io_buffer_size = io_buffer_size;
642 d->allocated_size = 0;
643 ret = init_put_byte(s, d->io_buffer, io_buffer_size,
645 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
646 max_packet_size ? NULL : dyn_buf_seek);
648 s->max_packet_size = max_packet_size;
654 * Open a write only memory stream.
656 * @param s new IO context
657 * @return zero if no error.
659 int url_open_dyn_buf(ByteIOContext *s)
661 return url_open_dyn_buf_internal(s, 0);
665 * Open a write only packetized memory stream with a maximum packet
666 * size of 'max_packet_size'. The stream is stored in a memory buffer
667 * with a big endian 4 byte header giving the packet size in bytes.
669 * @param s new IO context
670 * @param max_packet_size maximum packet size (must be > 0)
671 * @return zero if no error.
673 int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size)
675 if (max_packet_size <= 0)
677 return url_open_dyn_buf_internal(s, max_packet_size);
681 * Return the written size and a pointer to the buffer. The buffer
682 * must be freed with av_free().
683 * @param s IO context
684 * @param pointer to a byte buffer
685 * @return the length of the byte buffer
687 int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
689 DynBuffer *d = s->opaque;
694 *pbuffer = d->buffer;
699 #endif //CONFIG_ENCODERS