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