Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 7e1720de

History | View | Annotate | Download (19.9 KB)

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 "crc.h"
24
#include <stdarg.h>
25

    
26
#define IO_BUFFER_SIZE 32768
27

    
28
static void fill_buffer(ByteIOContext *s);
29

    
30
int init_put_byte(ByteIOContext *s,
31
                  unsigned char *buffer,
32
                  int buffer_size,
33
                  int write_flag,
34
                  void *opaque,
35
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
36
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
37
                  offset_t (*seek)(void *opaque, offset_t offset, int whence))
38
{
39
    s->buffer = buffer;
40
    s->buffer_size = buffer_size;
41
    s->buf_ptr = buffer;
42
    url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);
43
    s->opaque = opaque;
44
    s->write_packet = write_packet;
45
    s->read_packet = read_packet;
46
    s->seek = seek;
47
    s->pos = 0;
48
    s->must_flush = 0;
49
    s->eof_reached = 0;
50
    s->error = 0;
51
    s->is_streamed = 0;
52
    s->max_packet_size = 0;
53
    s->update_checksum= NULL;
54
    if(!read_packet && !write_flag){
55
        s->pos = buffer_size;
56
        s->buf_end = s->buffer + buffer_size;
57
    }
58
    s->read_play  = NULL;
59
    s->read_pause = NULL;
60
    s->read_seek  = NULL;
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;
118

    
119
    if(!s)
120
        return AVERROR(EINVAL);
121

    
122
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
123

    
124
    if (whence != SEEK_CUR && whence != SEEK_SET)
125
        return AVERROR(EINVAL);
126

    
127
    if (whence == SEEK_CUR) {
128
        offset1 = pos + (s->buf_ptr - s->buffer);
129
        if (offset == 0)
130
            return offset1;
131
        offset += offset1;
132
    }
133
    offset1 = offset - pos;
134
    if (!s->must_flush &&
135
        offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
136
        /* can do the seek inside the buffer */
137
        s->buf_ptr = s->buffer + offset1;
138
    } else if(s->is_streamed && !s->write_flag &&
139
              offset1 >= 0 && offset1 < (s->buf_end - s->buffer) + (1<<16)){
140
        while(s->pos < offset && !s->eof_reached)
141
            fill_buffer(s);
142
        s->buf_ptr = s->buf_end + offset - s->pos;
143
    } else {
144
        offset_t res = AVERROR(EPIPE);
145

    
146
#if defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK)
147
        if (s->write_flag) {
148
            flush_buffer(s);
149
            s->must_flush = 1;
150
        } else
151
#endif /* defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK) */
152
        {
153
            s->buf_end = s->buffer;
154
        }
155
        s->buf_ptr = s->buffer;
156
        if (!s->seek || (res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
157
            return res;
158
        s->pos = offset;
159
    }
160
    s->eof_reached = 0;
161
    return offset;
162
}
163

    
164
void url_fskip(ByteIOContext *s, offset_t offset)
165
{
166
    url_fseek(s, offset, SEEK_CUR);
167
}
168

    
169
offset_t url_ftell(ByteIOContext *s)
170
{
171
    return url_fseek(s, 0, SEEK_CUR);
172
}
173

    
174
offset_t url_fsize(ByteIOContext *s)
175
{
176
    offset_t size;
177

    
178
    if(!s)
179
        return AVERROR(EINVAL);
180

    
181
    if (!s->seek)
182
        return AVERROR(EPIPE);
183
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
184
    if(size<0){
185
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
186
            return size;
187
        size++;
188
        s->seek(s->opaque, s->pos, SEEK_SET);
189
    }
190
    return size;
191
}
192

    
193
int url_feof(ByteIOContext *s)
194
{
195
    if(!s)
196
        return 0;
197
    return s->eof_reached;
198
}
199

    
200
int url_ferror(ByteIOContext *s)
201
{
202
    if(!s)
203
        return 0;
204
    return s->error;
205
}
206

    
207
void put_le32(ByteIOContext *s, unsigned int val)
208
{
209
    put_byte(s, val);
210
    put_byte(s, val >> 8);
211
    put_byte(s, val >> 16);
212
    put_byte(s, val >> 24);
213
}
214

    
215
void put_be32(ByteIOContext *s, unsigned int val)
216
{
217
    put_byte(s, val >> 24);
218
    put_byte(s, val >> 16);
219
    put_byte(s, val >> 8);
220
    put_byte(s, val);
221
}
222

    
223
void put_strz(ByteIOContext *s, const char *str)
224
{
225
    if (str)
226
        put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
227
    else
228
        put_byte(s, 0);
229
}
230

    
231
void put_le64(ByteIOContext *s, uint64_t val)
232
{
233
    put_le32(s, (uint32_t)(val & 0xffffffff));
234
    put_le32(s, (uint32_t)(val >> 32));
235
}
236

    
237
void put_be64(ByteIOContext *s, uint64_t val)
238
{
239
    put_be32(s, (uint32_t)(val >> 32));
240
    put_be32(s, (uint32_t)(val & 0xffffffff));
241
}
242

    
243
void put_le16(ByteIOContext *s, unsigned int val)
244
{
245
    put_byte(s, val);
246
    put_byte(s, val >> 8);
247
}
248

    
249
void put_be16(ByteIOContext *s, unsigned int val)
250
{
251
    put_byte(s, val >> 8);
252
    put_byte(s, val);
253
}
254

    
255
void put_le24(ByteIOContext *s, unsigned int val)
256
{
257
    put_le16(s, val & 0xffff);
258
    put_byte(s, val >> 16);
259
}
260

    
261
void put_be24(ByteIOContext *s, unsigned int val)
262
{
263
    put_be16(s, val >> 8);
264
    put_byte(s, val);
265
}
266

    
267
void put_tag(ByteIOContext *s, const char *tag)
268
{
269
    while (*tag) {
270
        put_byte(s, *tag++);
271
    }
272
}
273

    
274
/* Input stream */
275

    
276
static void fill_buffer(ByteIOContext *s)
277
{
278
    int len=0;
279

    
280
    /* no need to do anything if EOF already reached */
281
    if (s->eof_reached)
282
        return;
283

    
284
    if(s->update_checksum){
285
        if(s->buf_end > s->checksum_ptr)
286
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
287
        s->checksum_ptr= s->buffer;
288
    }
289

    
290
    if(s->read_packet)
291
        len = s->read_packet(s->opaque, s->buffer, s->buffer_size);
292
    if (len <= 0) {
293
        /* do not modify buffer if EOF reached so that a seek back can
294
           be done without rereading data */
295
        s->eof_reached = 1;
296
        if(len<0)
297
            s->error= len;
298
    } else {
299
        s->pos += len;
300
        s->buf_ptr = s->buffer;
301
        s->buf_end = s->buffer + len;
302
    }
303
}
304

    
305
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len){
306
    return av_crc(av_crc04C11DB7, checksum, buf, len);
307
}
308

    
309
unsigned long get_checksum(ByteIOContext *s){
310
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
311
    s->update_checksum= NULL;
312
    return s->checksum;
313
}
314

    
315
void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum){
316
    s->update_checksum= update_checksum;
317
    if(s->update_checksum){
318
        s->checksum= checksum;
319
        s->checksum_ptr= s->buf_ptr;
320
    }
321
}
322

    
323
/* XXX: put an inline version */
324
int get_byte(ByteIOContext *s)
325
{
326
    if (s->buf_ptr < s->buf_end) {
327
        return *s->buf_ptr++;
328
    } else {
329
        fill_buffer(s);
330
        if (s->buf_ptr < s->buf_end)
331
            return *s->buf_ptr++;
332
        else
333
            return 0;
334
    }
335
}
336

    
337
int url_fgetc(ByteIOContext *s)
338
{
339
    if (s->buf_ptr < s->buf_end) {
340
        return *s->buf_ptr++;
341
    } else {
342
        fill_buffer(s);
343
        if (s->buf_ptr < s->buf_end)
344
            return *s->buf_ptr++;
345
        else
346
            return URL_EOF;
347
    }
348
}
349

    
350
int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
351
{
352
    int len, size1;
353

    
354
    size1 = size;
355
    while (size > 0) {
356
        len = s->buf_end - s->buf_ptr;
357
        if (len > size)
358
            len = size;
359
        if (len == 0) {
360
            if(size > s->buffer_size && !s->update_checksum){
361
                if(s->read_packet)
362
                    len = s->read_packet(s->opaque, buf, size);
363
                if (len <= 0) {
364
                    /* do not modify buffer if EOF reached so that a seek back can
365
                    be done without rereading data */
366
                    s->eof_reached = 1;
367
                    if(len<0)
368
                        s->error= len;
369
                    break;
370
                } else {
371
                    s->pos += len;
372
                    size -= len;
373
                    buf += len;
374
                    s->buf_ptr = s->buffer;
375
                    s->buf_end = s->buffer/* + len*/;
376
                }
377
            }else{
378
                fill_buffer(s);
379
                len = s->buf_end - s->buf_ptr;
380
                if (len == 0)
381
                    break;
382
            }
383
        } else {
384
            memcpy(buf, s->buf_ptr, len);
385
            buf += len;
386
            s->buf_ptr += len;
387
            size -= len;
388
        }
389
    }
390
    return size1 - size;
391
}
392

    
393
int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
394
{
395
    int len;
396

    
397
    if(size<0)
398
        return -1;
399

    
400
    len = s->buf_end - s->buf_ptr;
401
    if (len == 0) {
402
        fill_buffer(s);
403
        len = s->buf_end - s->buf_ptr;
404
    }
405
    if (len > size)
406
        len = size;
407
    memcpy(buf, s->buf_ptr, len);
408
    s->buf_ptr += len;
409
    return len;
410
}
411

    
412
unsigned int get_le16(ByteIOContext *s)
413
{
414
    unsigned int val;
415
    val = get_byte(s);
416
    val |= get_byte(s) << 8;
417
    return val;
418
}
419

    
420
unsigned int get_le24(ByteIOContext *s)
421
{
422
    unsigned int val;
423
    val = get_le16(s);
424
    val |= get_byte(s) << 16;
425
    return val;
426
}
427

    
428
unsigned int get_le32(ByteIOContext *s)
429
{
430
    unsigned int val;
431
    val = get_le16(s);
432
    val |= get_le16(s) << 16;
433
    return val;
434
}
435

    
436
uint64_t get_le64(ByteIOContext *s)
437
{
438
    uint64_t val;
439
    val = (uint64_t)get_le32(s);
440
    val |= (uint64_t)get_le32(s) << 32;
441
    return val;
442
}
443

    
444
unsigned int get_be16(ByteIOContext *s)
445
{
446
    unsigned int val;
447
    val = get_byte(s) << 8;
448
    val |= get_byte(s);
449
    return val;
450
}
451

    
452
unsigned int get_be24(ByteIOContext *s)
453
{
454
    unsigned int val;
455
    val = get_be16(s) << 8;
456
    val |= get_byte(s);
457
    return val;
458
}
459
unsigned int get_be32(ByteIOContext *s)
460
{
461
    unsigned int val;
462
    val = get_be16(s) << 16;
463
    val |= get_be16(s);
464
    return val;
465
}
466

    
467
char *get_strz(ByteIOContext *s, char *buf, int maxlen)
468
{
469
    int i = 0;
470
    char c;
471

    
472
    while ((c = get_byte(s))) {
473
        if (i < maxlen-1)
474
            buf[i++] = c;
475
    }
476

    
477
    buf[i] = 0; /* Ensure null terminated, but may be truncated */
478

    
479
    return buf;
480
}
481

    
482
uint64_t get_be64(ByteIOContext *s)
483
{
484
    uint64_t val;
485
    val = (uint64_t)get_be32(s) << 32;
486
    val |= (uint64_t)get_be32(s);
487
    return val;
488
}
489

    
490
uint64_t ff_get_v(ByteIOContext *bc){
491
    uint64_t val = 0;
492
    int tmp;
493

    
494
    do{
495
        tmp = get_byte(bc);
496
        val= (val<<7) + (tmp&127);
497
    }while(tmp&128);
498
    return val;
499
}
500

    
501
/* link with avio functions */
502

    
503
#ifdef CONFIG_MUXERS
504
static int url_write_packet(void *opaque, uint8_t *buf, int buf_size)
505
{
506
    URLContext *h = opaque;
507
    return url_write(h, buf, buf_size);
508
}
509
#else
510
#define         url_write_packet NULL
511
#endif //CONFIG_MUXERS
512

    
513
static int url_read_packet(void *opaque, uint8_t *buf, int buf_size)
514
{
515
    URLContext *h = opaque;
516
    return url_read(h, buf, buf_size);
517
}
518

    
519
static offset_t url_seek_packet(void *opaque, offset_t offset, int whence)
520
{
521
    URLContext *h = opaque;
522
    return url_seek(h, offset, whence);
523
    //return 0;
524
}
525

    
526
int url_fdopen(ByteIOContext **s, URLContext *h)
527
{
528
    uint8_t *buffer;
529
    int buffer_size, max_packet_size;
530

    
531

    
532
    max_packet_size = url_get_max_packet_size(h);
533
    if (max_packet_size) {
534
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
535
    } else {
536
        buffer_size = IO_BUFFER_SIZE;
537
    }
538
    buffer = av_malloc(buffer_size);
539
    if (!buffer)
540
        return AVERROR(ENOMEM);
541

    
542
    *s = av_mallocz(sizeof(ByteIOContext));
543
    if(!*s) {
544
        av_free(buffer);
545
        return AVERROR(ENOMEM);
546
    }
547

    
548
    if (init_put_byte(*s, buffer, buffer_size,
549
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
550
                      url_read_packet, url_write_packet, url_seek_packet) < 0) {
551
        av_free(buffer);
552
        av_freep(s);
553
        return AVERROR(EIO);
554
    }
555
    (*s)->is_streamed = h->is_streamed;
556
    (*s)->max_packet_size = max_packet_size;
557
    if(h->prot) {
558
        (*s)->read_play  = (int (*)(void *))h->prot->url_read_play;
559
        (*s)->read_pause = (int (*)(void *))h->prot->url_read_pause;
560
        (*s)->read_seek  = (int (*)(void *, int, int64_t, int))h->prot->url_read_seek;
561
    }
562
    return 0;
563
}
564

    
565
int url_setbufsize(ByteIOContext *s, int buf_size)
566
{
567
    uint8_t *buffer;
568
    buffer = av_malloc(buf_size);
569
    if (!buffer)
570
        return AVERROR(ENOMEM);
571

    
572
    av_free(s->buffer);
573
    s->buffer = buffer;
574
    s->buffer_size = buf_size;
575
    s->buf_ptr = buffer;
576
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
577
    return 0;
578
}
579

    
580
int url_resetbuf(ByteIOContext *s, int flags)
581
{
582
    URLContext *h = s->opaque;
583
    if ((flags & URL_RDWR) || (h && h->flags != flags && !h->flags & URL_RDWR))
584
        return AVERROR(EINVAL);
585

    
586
    if (flags & URL_WRONLY) {
587
        s->buf_end = s->buffer + s->buffer_size;
588
        s->write_flag = 1;
589
    } else {
590
        s->buf_end = s->buffer;
591
        s->write_flag = 0;
592
    }
593
    return 0;
594
}
595

    
596
int url_fopen(ByteIOContext **s, const char *filename, int flags)
597
{
598
    URLContext *h;
599
    int err;
600

    
601
    err = url_open(&h, filename, flags);
602
    if (err < 0)
603
        return err;
604
    err = url_fdopen(s, h);
605
    if (err < 0) {
606
        url_close(h);
607
        return err;
608
    }
609
    return 0;
610
}
611

    
612
int url_fclose(ByteIOContext *s)
613
{
614
    URLContext *h = s->opaque;
615

    
616
    av_free(s->buffer);
617
    av_free(s);
618
    return url_close(h);
619
}
620

    
621
URLContext *url_fileno(ByteIOContext *s)
622
{
623
    return s->opaque;
624
}
625

    
626
#ifdef CONFIG_MUXERS
627
int url_fprintf(ByteIOContext *s, const char *fmt, ...)
628
{
629
    va_list ap;
630
    char buf[4096];
631
    int ret;
632

    
633
    va_start(ap, fmt);
634
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
635
    va_end(ap);
636
    put_buffer(s, buf, strlen(buf));
637
    return ret;
638
}
639
#endif //CONFIG_MUXERS
640

    
641
char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
642
{
643
    int c;
644
    char *q;
645

    
646
    c = url_fgetc(s);
647
    if (c == EOF)
648
        return NULL;
649
    q = buf;
650
    for(;;) {
651
        if (c == EOF || c == '\n')
652
            break;
653
        if ((q - buf) < buf_size - 1)
654
            *q++ = c;
655
        c = url_fgetc(s);
656
    }
657
    if (buf_size > 0)
658
        *q = '\0';
659
    return buf;
660
}
661

    
662
int url_fget_max_packet_size(ByteIOContext *s)
663
{
664
    return s->max_packet_size;
665
}
666

    
667
int av_url_read_fplay(ByteIOContext *s)
668
{
669
    if (!s->read_play)
670
        return AVERROR(ENOSYS);
671
    return s->read_play(s->opaque);
672
}
673

    
674
int av_url_read_fpause(ByteIOContext *s)
675
{
676
    if (!s->read_pause)
677
        return AVERROR(ENOSYS);
678
    return s->read_pause(s->opaque);
679
}
680

    
681
int av_url_read_fseek(ByteIOContext *s,
682
        int stream_index, int64_t timestamp, int flags)
683
{
684
    URLContext *h = s->opaque;
685
    int ret;
686
    if (!s->read_seek)
687
        return AVERROR(ENOSYS);
688
    ret = s->read_seek(h, stream_index, timestamp, flags);
689
    if(ret >= 0) {
690
        s->buf_ptr = s->buf_end; // Flush buffer
691
        s->pos = s->seek(h, 0, SEEK_CUR);
692
    }
693
    return ret;
694
}
695

    
696
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
697
 * back to the server even if CONFIG_MUXERS is not set. */
698
#if defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK)
699
/* buffer handling */
700
int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags)
701
{
702
    int ret;
703
    *s = av_mallocz(sizeof(ByteIOContext));
704
    if(!*s)
705
        return AVERROR(ENOMEM);
706
    ret = init_put_byte(*s, buf, buf_size,
707
                        (flags & URL_WRONLY || flags & URL_RDWR),
708
                        NULL, NULL, NULL, NULL);
709
    if(ret != 0)
710
        av_freep(s);
711
    return ret;
712
}
713

    
714
int url_close_buf(ByteIOContext *s)
715
{
716
    put_flush_packet(s);
717
    return s->buf_ptr - s->buffer;
718
}
719

    
720
/* output in a dynamic buffer */
721

    
722
typedef struct DynBuffer {
723
    int pos, size, allocated_size;
724
    uint8_t *buffer;
725
    int io_buffer_size;
726
    uint8_t io_buffer[1];
727
} DynBuffer;
728

    
729
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
730
{
731
    DynBuffer *d = opaque;
732
    int new_size, new_allocated_size;
733

    
734
    /* reallocate buffer if needed */
735
    new_size = d->pos + buf_size;
736
    new_allocated_size = d->allocated_size;
737
    if(new_size < d->pos || new_size > INT_MAX/2)
738
        return -1;
739
    while (new_size > new_allocated_size) {
740
        if (!new_allocated_size)
741
            new_allocated_size = new_size;
742
        else
743
            new_allocated_size += new_allocated_size / 2 + 1;
744
    }
745

    
746
    if (new_allocated_size > d->allocated_size) {
747
        d->buffer = av_realloc(d->buffer, new_allocated_size);
748
        if(d->buffer == NULL)
749
             return -1234;
750
        d->allocated_size = new_allocated_size;
751
    }
752
    memcpy(d->buffer + d->pos, buf, buf_size);
753
    d->pos = new_size;
754
    if (d->pos > d->size)
755
        d->size = d->pos;
756
    return buf_size;
757
}
758

    
759
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
760
{
761
    unsigned char buf1[4];
762
    int ret;
763

    
764
    /* packetized write: output the header */
765
    buf1[0] = (buf_size >> 24);
766
    buf1[1] = (buf_size >> 16);
767
    buf1[2] = (buf_size >> 8);
768
    buf1[3] = (buf_size);
769
    ret= dyn_buf_write(opaque, buf1, 4);
770
    if(ret < 0)
771
        return ret;
772

    
773
    /* then the data */
774
    return dyn_buf_write(opaque, buf, buf_size);
775
}
776

    
777
static offset_t dyn_buf_seek(void *opaque, offset_t offset, int whence)
778
{
779
    DynBuffer *d = opaque;
780

    
781
    if (whence == SEEK_CUR)
782
        offset += d->pos;
783
    else if (whence == SEEK_END)
784
        offset += d->size;
785
    if (offset < 0 || offset > 0x7fffffffLL)
786
        return -1;
787
    d->pos = offset;
788
    return 0;
789
}
790

    
791
static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size)
792
{
793
    DynBuffer *d;
794
    int io_buffer_size, ret;
795

    
796
    if (max_packet_size)
797
        io_buffer_size = max_packet_size;
798
    else
799
        io_buffer_size = 1024;
800

    
801
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
802
        return -1;
803
    d = av_malloc(sizeof(DynBuffer) + io_buffer_size);
804
    if (!d)
805
        return -1;
806
    *s = av_mallocz(sizeof(ByteIOContext));
807
    if(!*s) {
808
        av_free(d);
809
        return AVERROR(ENOMEM);
810
    }
811
    d->io_buffer_size = io_buffer_size;
812
    d->buffer = NULL;
813
    d->pos = 0;
814
    d->size = 0;
815
    d->allocated_size = 0;
816
    ret = init_put_byte(*s, d->io_buffer, io_buffer_size,
817
                        1, d, NULL,
818
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
819
                        max_packet_size ? NULL : dyn_buf_seek);
820
    if (ret == 0) {
821
        (*s)->max_packet_size = max_packet_size;
822
    } else {
823
        av_free(d);
824
        av_freep(s);
825
    }
826
    return ret;
827
}
828

    
829
int url_open_dyn_buf(ByteIOContext **s)
830
{
831
    return url_open_dyn_buf_internal(s, 0);
832
}
833

    
834
int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size)
835
{
836
    if (max_packet_size <= 0)
837
        return -1;
838
    return url_open_dyn_buf_internal(s, max_packet_size);
839
}
840

    
841
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
842
{
843
    DynBuffer *d = s->opaque;
844
    int size;
845

    
846
    put_flush_packet(s);
847

    
848
    *pbuffer = d->buffer;
849
    size = d->size;
850
    av_free(d);
851
    av_free(s);
852
    return size;
853
}
854
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */