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