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