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