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