added dynamic buffer support - added url_fprintf() and url_fgetc()
[libav.git] / libav / aviobuf.c
1 /*
2 * Buffered I/O for ffmpeg system
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 *
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.
9 *
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.
14 *
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
18 */
19 #include "avformat.h"
20 #include <stdarg.h>
21
22 #define IO_BUFFER_SIZE 32768
23
24 int init_put_byte(ByteIOContext *s,
25 unsigned char *buffer,
26 int buffer_size,
27 int write_flag,
28 void *opaque,
29 int (*read_packet)(void *opaque, UINT8 *buf, int buf_size),
30 void (*write_packet)(void *opaque, UINT8 *buf, int buf_size),
31 int (*seek)(void *opaque, offset_t offset, int whence))
32 {
33 s->buffer = buffer;
34 s->buffer_size = buffer_size;
35 s->buf_ptr = buffer;
36 s->write_flag = write_flag;
37 if (!s->write_flag)
38 s->buf_end = buffer;
39 else
40 s->buf_end = buffer + buffer_size;
41 s->opaque = opaque;
42 s->write_packet = write_packet;
43 s->read_packet = read_packet;
44 s->seek = seek;
45 s->pos = 0;
46 s->must_flush = 0;
47 s->eof_reached = 0;
48 s->is_streamed = 0;
49 s->max_packet_size = 0;
50 return 0;
51 }
52
53
54 static void flush_buffer(ByteIOContext *s)
55 {
56 if (s->buf_ptr > s->buffer) {
57 if (s->write_packet)
58 s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
59 s->pos += s->buf_ptr - s->buffer;
60 }
61 s->buf_ptr = s->buffer;
62 }
63
64 void put_byte(ByteIOContext *s, int b)
65 {
66 *(s->buf_ptr)++ = b;
67 if (s->buf_ptr >= s->buf_end)
68 flush_buffer(s);
69 }
70
71 void put_buffer(ByteIOContext *s, unsigned char *buf, int size)
72 {
73 int len;
74
75 while (size > 0) {
76 len = (s->buf_end - s->buf_ptr);
77 if (len > size)
78 len = size;
79 memcpy(s->buf_ptr, buf, len);
80 s->buf_ptr += len;
81
82 if (s->buf_ptr >= s->buf_end)
83 flush_buffer(s);
84
85 buf += len;
86 size -= len;
87 }
88 }
89
90 void put_flush_packet(ByteIOContext *s)
91 {
92 flush_buffer(s);
93 s->must_flush = 0;
94 }
95
96 offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence)
97 {
98 offset_t offset1;
99
100 if (whence != SEEK_CUR && whence != SEEK_SET)
101 return -EINVAL;
102
103 if (s->write_flag) {
104 if (whence == SEEK_CUR) {
105 offset1 = s->pos + (s->buf_ptr - s->buffer);
106 if (offset == 0)
107 return offset1;
108 offset += offset1;
109 }
110 offset1 = offset - s->pos;
111 if (!s->must_flush &&
112 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
113 /* can do the seek inside the buffer */
114 s->buf_ptr = s->buffer + offset1;
115 } else {
116 if (!s->seek)
117 return -EPIPE;
118 flush_buffer(s);
119 s->must_flush = 1;
120 s->buf_ptr = s->buffer;
121 s->seek(s->opaque, offset, SEEK_SET);
122 s->pos = offset;
123 }
124 } else {
125 if (whence == SEEK_CUR) {
126 offset1 = s->pos - (s->buf_end - s->buffer) + (s->buf_ptr - s->buffer);
127 if (offset == 0)
128 return offset1;
129 offset += offset1;
130 }
131 offset1 = offset - (s->pos - (s->buf_end - s->buffer));
132 if (offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
133 /* can do the seek inside the buffer */
134 s->buf_ptr = s->buffer + offset1;
135 } else {
136 if (!s->seek)
137 return -EPIPE;
138 s->buf_ptr = s->buffer;
139 s->buf_end = s->buffer;
140 s->seek(s->opaque, offset, SEEK_SET);
141 s->pos = offset;
142 }
143 s->eof_reached = 0;
144 }
145 return offset;
146 }
147
148 void url_fskip(ByteIOContext *s, offset_t offset)
149 {
150 url_fseek(s, offset, SEEK_CUR);
151 }
152
153 offset_t url_ftell(ByteIOContext *s)
154 {
155 return url_fseek(s, 0, SEEK_CUR);
156 }
157
158 int url_feof(ByteIOContext *s)
159 {
160 return s->eof_reached;
161 }
162
163 void put_le32(ByteIOContext *s, unsigned int val)
164 {
165 put_byte(s, val);
166 put_byte(s, val >> 8);
167 put_byte(s, val >> 16);
168 put_byte(s, val >> 24);
169 }
170
171 void put_be32(ByteIOContext *s, unsigned int val)
172 {
173 put_byte(s, val >> 24);
174 put_byte(s, val >> 16);
175 put_byte(s, val >> 8);
176 put_byte(s, val);
177 }
178
179 void put_le64(ByteIOContext *s, UINT64 val)
180 {
181 put_le32(s, (UINT32)(val & 0xffffffff));
182 put_le32(s, (UINT32)(val >> 32));
183 }
184
185 void put_be64(ByteIOContext *s, UINT64 val)
186 {
187 put_be32(s, (UINT32)(val >> 32));
188 put_be32(s, (UINT32)(val & 0xffffffff));
189 }
190
191 void put_le16(ByteIOContext *s, unsigned int val)
192 {
193 put_byte(s, val);
194 put_byte(s, val >> 8);
195 }
196
197 void put_be16(ByteIOContext *s, unsigned int val)
198 {
199 put_byte(s, val >> 8);
200 put_byte(s, val);
201 }
202
203 void put_tag(ByteIOContext *s, char *tag)
204 {
205 while (*tag) {
206 put_byte(s, *tag++);
207 }
208 }
209
210 /* Input stream */
211
212 static void fill_buffer(ByteIOContext *s)
213 {
214 int len;
215
216 /* no need to do anything if EOF already reached */
217 if (s->eof_reached)
218 return;
219 len = s->read_packet(s->opaque, s->buffer, s->buffer_size);
220 if (len <= 0) {
221 /* do not modify buffer if EOF reached so that a seek back can
222 be done without rereading data */
223 s->eof_reached = 1;
224 } else {
225 s->pos += len;
226 s->buf_ptr = s->buffer;
227 s->buf_end = s->buffer + len;
228 }
229 }
230
231 /* NOTE: return 0 if EOF, so you cannot use it if EOF handling is
232 necessary */
233 /* XXX: put an inline version */
234 int get_byte(ByteIOContext *s)
235 {
236 if (s->buf_ptr < s->buf_end) {
237 return *s->buf_ptr++;
238 } else {
239 fill_buffer(s);
240 if (s->buf_ptr < s->buf_end)
241 return *s->buf_ptr++;
242 else
243 return 0;
244 }
245 }
246
247 /* NOTE: return URL_EOF (-1) if EOF */
248 int url_fgetc(ByteIOContext *s)
249 {
250 if (s->buf_ptr < s->buf_end) {
251 return *s->buf_ptr++;
252 } else {
253 fill_buffer(s);
254 if (s->buf_ptr < s->buf_end)
255 return *s->buf_ptr++;
256 else
257 return URL_EOF;
258 }
259 }
260
261 int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
262 {
263 int len, size1;
264
265 size1 = size;
266 while (size > 0) {
267 len = s->buf_end - s->buf_ptr;
268 if (len > size)
269 len = size;
270 if (len == 0) {
271 fill_buffer(s);
272 len = s->buf_end - s->buf_ptr;
273 if (len == 0)
274 break;
275 } else {
276 memcpy(buf, s->buf_ptr, len);
277 buf += len;
278 s->buf_ptr += len;
279 size -= len;
280 }
281 }
282 return size1 - size;
283 }
284
285 unsigned int get_le16(ByteIOContext *s)
286 {
287 unsigned int val;
288 val = get_byte(s);
289 val |= get_byte(s) << 8;
290 return val;
291 }
292
293 unsigned int get_le32(ByteIOContext *s)
294 {
295 unsigned int val;
296 val = get_byte(s);
297 val |= get_byte(s) << 8;
298 val |= get_byte(s) << 16;
299 val |= get_byte(s) << 24;
300 return val;
301 }
302
303 UINT64 get_le64(ByteIOContext *s)
304 {
305 UINT64 val;
306 val = (UINT64)get_le32(s);
307 val |= (UINT64)get_le32(s) << 32;
308 return val;
309 }
310
311 unsigned int get_be16(ByteIOContext *s)
312 {
313 unsigned int val;
314 val = get_byte(s) << 8;
315 val |= get_byte(s);
316 return val;
317 }
318
319 unsigned int get_be32(ByteIOContext *s)
320 {
321 unsigned int val;
322 val = get_byte(s) << 24;
323 val |= get_byte(s) << 16;
324 val |= get_byte(s) << 8;
325 val |= get_byte(s);
326 return val;
327 }
328
329 UINT64 get_be64(ByteIOContext *s)
330 {
331 UINT64 val;
332 val = (UINT64)get_be32(s) << 32;
333 val |= (UINT64)get_be32(s);
334 return val;
335 }
336
337 /* link with avio functions */
338
339 void url_write_packet(void *opaque, UINT8 *buf, int buf_size)
340 {
341 URLContext *h = opaque;
342 url_write(h, buf, buf_size);
343 }
344
345 int url_read_packet(void *opaque, UINT8 *buf, int buf_size)
346 {
347 URLContext *h = opaque;
348 return url_read(h, buf, buf_size);
349 }
350
351 int url_seek_packet(void *opaque, INT64 offset, int whence)
352 {
353 URLContext *h = opaque;
354 url_seek(h, offset, whence);
355 return 0;
356 }
357
358 int url_fdopen(ByteIOContext *s, URLContext *h)
359 {
360 UINT8 *buffer;
361 int buffer_size, max_packet_size;
362
363
364 max_packet_size = url_get_max_packet_size(h);
365 if (max_packet_size) {
366 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
367 } else {
368 buffer_size = IO_BUFFER_SIZE;
369 }
370 buffer = av_malloc(buffer_size);
371 if (!buffer)
372 return -ENOMEM;
373
374 if (init_put_byte(s, buffer, buffer_size,
375 (h->flags & URL_WRONLY) != 0, h,
376 url_read_packet, url_write_packet, url_seek_packet) < 0) {
377 av_free(buffer);
378 return -EIO;
379 }
380 s->is_streamed = h->is_streamed;
381 s->max_packet_size = max_packet_size;
382 return 0;
383 }
384
385 /* XXX: must be called before any I/O */
386 int url_setbufsize(ByteIOContext *s, int buf_size)
387 {
388 UINT8 *buffer;
389 buffer = av_malloc(buf_size);
390 if (!buffer)
391 return -ENOMEM;
392
393 av_free(s->buffer);
394 s->buffer = buffer;
395 s->buffer_size = buf_size;
396 s->buf_ptr = buffer;
397 if (!s->write_flag)
398 s->buf_end = buffer;
399 else
400 s->buf_end = buffer + buf_size;
401 return 0;
402 }
403
404 /* NOTE: when opened as read/write, the buffers are only used for
405 reading */
406 int url_fopen(ByteIOContext *s, const char *filename, int flags)
407 {
408 URLContext *h;
409 int err;
410
411 err = url_open(&h, filename, flags);
412 if (err < 0)
413 return err;
414 err = url_fdopen(s, h);
415 if (err < 0) {
416 url_close(h);
417 return err;
418 }
419 return 0;
420 }
421
422 int url_fclose(ByteIOContext *s)
423 {
424 URLContext *h = s->opaque;
425
426 av_free(s->buffer);
427 memset(s, 0, sizeof(ByteIOContext));
428 return url_close(h);
429 }
430
431 URLContext *url_fileno(ByteIOContext *s)
432 {
433 return s->opaque;
434 }
435
436 /* XXX: currently size is limited */
437 int url_fprintf(ByteIOContext *s, const char *fmt, ...)
438 {
439 va_list ap;
440 char buf[4096];
441 int ret;
442
443 va_start(ap, fmt);
444 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
445 va_end(ap);
446 put_buffer(s, buf, strlen(buf));
447 return ret;
448 }
449
450 /* note: unlike fgets, the EOL character is not returned and a whole
451 line is parsed. return NULL if first char read was EOF */
452 char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
453 {
454 int c;
455 char *q;
456
457 c = url_fgetc(s);
458 if (c == EOF)
459 return NULL;
460 q = buf;
461 for(;;) {
462 if (c == EOF || c == '\n')
463 break;
464 if ((q - buf) < buf_size - 1)
465 *q++ = c;
466 c = url_fgetc(s);
467 }
468 if (buf_size > 0)
469 *q = '\0';
470 return buf;
471 }
472
473 /*
474 * Return the maximum packet size associated to packetized buffered file
475 * handle. If the file is not packetized (stream like http or file on
476 * disk), then 0 is returned.
477 *
478 * @param h buffered file handle
479 * @return maximum packet size in bytes
480 */
481 int url_fget_max_packet_size(ByteIOContext *s)
482 {
483 return s->max_packet_size;
484 }
485
486 /* buffer handling */
487 int url_open_buf(ByteIOContext *s, UINT8 *buf, int buf_size, int flags)
488 {
489 return init_put_byte(s, buf, buf_size,
490 (flags & URL_WRONLY) != 0, NULL, NULL, NULL, NULL);
491 }
492
493 /* return the written or read size */
494 int url_close_buf(ByteIOContext *s)
495 {
496 put_flush_packet(s);
497 return s->buf_ptr - s->buffer;
498 }
499
500 /* output in a dynamic buffer */
501
502 typedef struct DynBuffer {
503 int pos, size, allocated_size;
504 UINT8 *buffer;
505 int io_buffer_size;
506 UINT8 io_buffer[1];
507 } DynBuffer;
508
509 static void dyn_buf_write(void *opaque, UINT8 *buf, int buf_size)
510 {
511 DynBuffer *d = opaque;
512 int new_size, new_allocated_size;
513 UINT8 *new_buffer;
514
515 /* reallocate buffer if needed */
516 new_size = d->pos + buf_size;
517 new_allocated_size = d->allocated_size;
518 while (new_size > new_allocated_size) {
519 if (!new_allocated_size)
520 new_allocated_size = new_size;
521 else
522 new_allocated_size = (new_allocated_size * 3) / 2;
523 }
524
525 if (new_allocated_size > d->allocated_size) {
526 new_buffer = av_malloc(new_allocated_size);
527 if (!new_buffer)
528 return;
529 memcpy(new_buffer, d->buffer, d->size);
530 av_free(d->buffer);
531 d->buffer = new_buffer;
532 d->allocated_size = new_allocated_size;
533 }
534 memcpy(d->buffer + d->pos, buf, buf_size);
535 d->pos = new_size;
536 if (d->pos > d->size)
537 d->size = d->pos;
538 }
539
540 static void dyn_packet_buf_write(void *opaque, UINT8 *buf, int buf_size)
541 {
542 unsigned char buf1[4];
543
544 /* packetized write: output the header */
545 buf1[0] = (buf_size >> 24);
546 buf1[1] = (buf_size >> 16);
547 buf1[2] = (buf_size >> 8);
548 buf1[3] = (buf_size);
549 dyn_buf_write(opaque, buf1, 4);
550
551 /* then the data */
552 dyn_buf_write(opaque, buf, buf_size);
553 }
554
555 static int dyn_buf_seek(void *opaque, offset_t offset, int whence)
556 {
557 DynBuffer *d = opaque;
558
559 if (whence == SEEK_CUR)
560 offset += d->pos;
561 else if (whence == SEEK_END)
562 offset += d->size;
563 if (offset < 0 || offset > 0x7fffffffLL)
564 return -1;
565 d->pos = offset;
566 return 0;
567 }
568
569 static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size)
570 {
571 DynBuffer *d;
572 int io_buffer_size, ret;
573
574 if (max_packet_size)
575 io_buffer_size = max_packet_size;
576 else
577 io_buffer_size = 1024;
578
579 d = av_malloc(sizeof(DynBuffer) + io_buffer_size);
580 if (!d)
581 return -1;
582 d->io_buffer_size = io_buffer_size;
583 d->buffer = NULL;
584 d->pos = 0;
585 d->size = 0;
586 d->allocated_size = 0;
587 ret = init_put_byte(s, d->io_buffer, io_buffer_size,
588 1, d, NULL,
589 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
590 max_packet_size ? NULL : dyn_buf_seek);
591 if (ret == 0) {
592 s->max_packet_size = max_packet_size;
593 }
594 return ret;
595 }
596
597 /*
598 * Open a write only memory stream.
599 *
600 * @param s new IO context
601 * @return zero if no error.
602 */
603 int url_open_dyn_buf(ByteIOContext *s)
604 {
605 return url_open_dyn_buf_internal(s, 0);
606 }
607
608 /*
609 * Open a write only packetized memory stream with a maximum packet
610 * size of 'max_packet_size'. The stream is stored in a memory buffer
611 * with a big endian 4 byte header giving the packet size in bytes.
612 *
613 * @param s new IO context
614 * @param max_packet_size maximum packet size (must be > 0)
615 * @return zero if no error.
616 */
617 int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size)
618 {
619 if (max_packet_size <= 0)
620 return -1;
621 return url_open_dyn_buf_internal(s, max_packet_size);
622 }
623
624 /*
625 * Return the written size and a pointer to the buffer. The buffer
626 * must be freed with av_free().
627 * @param s IO context
628 * @param pointer to a byte buffer
629 * @return the length of the byte buffer
630 */
631 int url_close_dyn_buf(ByteIOContext *s, UINT8 **pbuffer)
632 {
633 DynBuffer *d = s->opaque;
634 int size;
635
636 put_flush_packet(s);
637
638 *pbuffer = d->buffer;
639 size = d->size;
640 av_free(d);
641 return size;
642 }