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