Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ f9d6b13a

History | View | Annotate | Download (24 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

    
22
#include "libavutil/crc.h"
23
#include "libavutil/intreadwrite.h"
24
#include "avformat.h"
25
#include "avio.h"
26
#include "internal.h"
27
#include <stdarg.h>
28

    
29
#define IO_BUFFER_SIZE 32768
30

    
31
/**
32
 * Do seeks within this distance ahead of the current buffer by skipping
33
 * data instead of calling the protocol seek function, for seekable
34
 * protocols.
35
 */
36
#define SHORT_SEEK_THRESHOLD 4096
37

    
38
static void fill_buffer(ByteIOContext *s);
39
#if !FF_API_URL_RESETBUF
40
static int url_resetbuf(ByteIOContext *s, int flags);
41
#endif
42

    
43
int init_put_byte(ByteIOContext *s,
44
                  unsigned char *buffer,
45
                  int buffer_size,
46
                  int write_flag,
47
                  void *opaque,
48
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
49
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
50
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
51
{
52
    s->buffer = buffer;
53
    s->buffer_size = buffer_size;
54
    s->buf_ptr = buffer;
55
    s->opaque = opaque;
56
    url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);
57
    s->write_packet = write_packet;
58
    s->read_packet = read_packet;
59
    s->seek = seek;
60
    s->pos = 0;
61
    s->must_flush = 0;
62
    s->eof_reached = 0;
63
    s->error = 0;
64
    s->is_streamed = 0;
65
    s->max_packet_size = 0;
66
    s->update_checksum= NULL;
67
    if(!read_packet && !write_flag){
68
        s->pos = buffer_size;
69
        s->buf_end = s->buffer + buffer_size;
70
    }
71
    s->read_pause = NULL;
72
    s->read_seek  = NULL;
73
    return 0;
74
}
75

    
76
ByteIOContext *av_alloc_put_byte(
77
                  unsigned char *buffer,
78
                  int buffer_size,
79
                  int write_flag,
80
                  void *opaque,
81
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
82
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
83
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
84
{
85
    ByteIOContext *s = av_mallocz(sizeof(ByteIOContext));
86
    init_put_byte(s, buffer, buffer_size, write_flag, opaque,
87
                  read_packet, write_packet, seek);
88
    return s;
89
}
90

    
91
static void flush_buffer(ByteIOContext *s)
92
{
93
    if (s->buf_ptr > s->buffer) {
94
        if (s->write_packet && !s->error){
95
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
96
            if(ret < 0){
97
                s->error = ret;
98
            }
99
        }
100
        if(s->update_checksum){
101
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
102
            s->checksum_ptr= s->buffer;
103
        }
104
        s->pos += s->buf_ptr - s->buffer;
105
    }
106
    s->buf_ptr = s->buffer;
107
}
108

    
109
void put_byte(ByteIOContext *s, int b)
110
{
111
    *(s->buf_ptr)++ = b;
112
    if (s->buf_ptr >= s->buf_end)
113
        flush_buffer(s);
114
}
115

    
116
void put_nbyte(ByteIOContext *s, int b, int count)
117
{
118
    while (count > 0) {
119
        int len = FFMIN(s->buf_end - s->buf_ptr, count);
120
        memset(s->buf_ptr, b, len);
121
        s->buf_ptr += len;
122

    
123
        if (s->buf_ptr >= s->buf_end)
124
            flush_buffer(s);
125

    
126
        count -= len;
127
    }
128
}
129

    
130
void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
131
{
132
    while (size > 0) {
133
        int len = FFMIN(s->buf_end - s->buf_ptr, size);
134
        memcpy(s->buf_ptr, buf, len);
135
        s->buf_ptr += len;
136

    
137
        if (s->buf_ptr >= s->buf_end)
138
            flush_buffer(s);
139

    
140
        buf += len;
141
        size -= len;
142
    }
143
}
144

    
145
void put_flush_packet(ByteIOContext *s)
146
{
147
    flush_buffer(s);
148
    s->must_flush = 0;
149
}
150

    
151
int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence)
152
{
153
    int64_t offset1;
154
    int64_t pos;
155
    int force = whence & AVSEEK_FORCE;
156
    whence &= ~AVSEEK_FORCE;
157

    
158
    if(!s)
159
        return AVERROR(EINVAL);
160

    
161
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
162

    
163
    if (whence != SEEK_CUR && whence != SEEK_SET)
164
        return AVERROR(EINVAL);
165

    
166
    if (whence == SEEK_CUR) {
167
        offset1 = pos + (s->buf_ptr - s->buffer);
168
        if (offset == 0)
169
            return offset1;
170
        offset += offset1;
171
    }
172
    offset1 = offset - pos;
173
    if (!s->must_flush &&
174
        offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
175
        /* can do the seek inside the buffer */
176
        s->buf_ptr = s->buffer + offset1;
177
    } else if ((s->is_streamed ||
178
               offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
179
               !s->write_flag && offset1 >= 0 &&
180
              (whence != SEEK_END || force)) {
181
        while(s->pos < offset && !s->eof_reached)
182
            fill_buffer(s);
183
        if (s->eof_reached)
184
            return AVERROR_EOF;
185
        s->buf_ptr = s->buf_end + offset - s->pos;
186
    } else {
187
        int64_t res;
188

    
189
#if CONFIG_MUXERS || CONFIG_NETWORK
190
        if (s->write_flag) {
191
            flush_buffer(s);
192
            s->must_flush = 1;
193
        }
194
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
195
        if (!s->seek)
196
            return AVERROR(EPIPE);
197
        if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
198
            return res;
199
        if (!s->write_flag)
200
            s->buf_end = s->buffer;
201
        s->buf_ptr = s->buffer;
202
        s->pos = offset;
203
    }
204
    s->eof_reached = 0;
205
    return offset;
206
}
207

    
208
int url_fskip(ByteIOContext *s, int64_t offset)
209
{
210
    int64_t ret = url_fseek(s, offset, SEEK_CUR);
211
    return ret < 0 ? ret : 0;
212
}
213

    
214
int64_t url_ftell(ByteIOContext *s)
215
{
216
    return url_fseek(s, 0, SEEK_CUR);
217
}
218

    
219
int64_t url_fsize(ByteIOContext *s)
220
{
221
    int64_t size;
222

    
223
    if(!s)
224
        return AVERROR(EINVAL);
225

    
226
    if (!s->seek)
227
        return AVERROR(ENOSYS);
228
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
229
    if(size<0){
230
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
231
            return size;
232
        size++;
233
        s->seek(s->opaque, s->pos, SEEK_SET);
234
    }
235
    return size;
236
}
237

    
238
int url_feof(ByteIOContext *s)
239
{
240
    if(!s)
241
        return 0;
242
    return s->eof_reached;
243
}
244

    
245
int url_ferror(ByteIOContext *s)
246
{
247
    if(!s)
248
        return 0;
249
    return s->error;
250
}
251

    
252
void put_le32(ByteIOContext *s, unsigned int val)
253
{
254
    put_byte(s, val);
255
    put_byte(s, val >> 8);
256
    put_byte(s, val >> 16);
257
    put_byte(s, val >> 24);
258
}
259

    
260
void put_be32(ByteIOContext *s, unsigned int val)
261
{
262
    put_byte(s, val >> 24);
263
    put_byte(s, val >> 16);
264
    put_byte(s, val >> 8);
265
    put_byte(s, val);
266
}
267

    
268
#if FF_API_OLD_AVIO
269
void put_strz(ByteIOContext *s, const char *str)
270
{
271
    avio_put_str(s, str);
272
}
273
#endif
274

    
275
int avio_put_str(ByteIOContext *s, const char *str)
276
{
277
    int len = 1;
278
    if (str) {
279
        len += strlen(str);
280
        put_buffer(s, (const unsigned char *) str, len);
281
    } else
282
        put_byte(s, 0);
283
    return len;
284
}
285

    
286
int avio_put_str16le(ByteIOContext *s, const char *str)
287
{
288
    const uint8_t *q = str;
289
    int ret = 0;
290

    
291
    while (*q) {
292
        uint32_t ch;
293
        uint16_t tmp;
294

    
295
        GET_UTF8(ch, *q++, break;)
296
        PUT_UTF16(ch, tmp, put_le16(s, tmp);ret += 2;)
297
    }
298
    put_le16(s, 0);
299
    ret += 2;
300
    return ret;
301
}
302

    
303
int ff_get_v_length(uint64_t val){
304
    int i=1;
305

    
306
    while(val>>=7)
307
        i++;
308

    
309
    return i;
310
}
311

    
312
void ff_put_v(ByteIOContext *bc, uint64_t val){
313
    int i= ff_get_v_length(val);
314

    
315
    while(--i>0)
316
        put_byte(bc, 128 | (val>>(7*i)));
317

    
318
    put_byte(bc, val&127);
319
}
320

    
321
void put_le64(ByteIOContext *s, uint64_t val)
322
{
323
    put_le32(s, (uint32_t)(val & 0xffffffff));
324
    put_le32(s, (uint32_t)(val >> 32));
325
}
326

    
327
void put_be64(ByteIOContext *s, uint64_t val)
328
{
329
    put_be32(s, (uint32_t)(val >> 32));
330
    put_be32(s, (uint32_t)(val & 0xffffffff));
331
}
332

    
333
void put_le16(ByteIOContext *s, unsigned int val)
334
{
335
    put_byte(s, val);
336
    put_byte(s, val >> 8);
337
}
338

    
339
void put_be16(ByteIOContext *s, unsigned int val)
340
{
341
    put_byte(s, val >> 8);
342
    put_byte(s, val);
343
}
344

    
345
void put_le24(ByteIOContext *s, unsigned int val)
346
{
347
    put_le16(s, val & 0xffff);
348
    put_byte(s, val >> 16);
349
}
350

    
351
void put_be24(ByteIOContext *s, unsigned int val)
352
{
353
    put_be16(s, val >> 8);
354
    put_byte(s, val);
355
}
356

    
357
void put_tag(ByteIOContext *s, const char *tag)
358
{
359
    while (*tag) {
360
        put_byte(s, *tag++);
361
    }
362
}
363

    
364
/* Input stream */
365

    
366
static void fill_buffer(ByteIOContext *s)
367
{
368
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
369
    int len= s->buffer_size - (dst - s->buffer);
370
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
371

    
372
    /* no need to do anything if EOF already reached */
373
    if (s->eof_reached)
374
        return;
375

    
376
    if(s->update_checksum && dst == s->buffer){
377
        if(s->buf_end > s->checksum_ptr)
378
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
379
        s->checksum_ptr= s->buffer;
380
    }
381

    
382
    /* make buffer smaller in case it ended up large after probing */
383
    if (s->buffer_size > max_buffer_size) {
384
        url_setbufsize(s, max_buffer_size);
385

    
386
        s->checksum_ptr = dst = s->buffer;
387
        len = s->buffer_size;
388
    }
389

    
390
    if(s->read_packet)
391
        len = s->read_packet(s->opaque, dst, len);
392
    else
393
        len = 0;
394
    if (len <= 0) {
395
        /* do not modify buffer if EOF reached so that a seek back can
396
           be done without rereading data */
397
        s->eof_reached = 1;
398
        if(len<0)
399
            s->error= len;
400
    } else {
401
        s->pos += len;
402
        s->buf_ptr = dst;
403
        s->buf_end = dst + len;
404
    }
405
}
406

    
407
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
408
                                    unsigned int len)
409
{
410
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
411
}
412

    
413
unsigned long get_checksum(ByteIOContext *s)
414
{
415
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
416
    s->update_checksum= NULL;
417
    return s->checksum;
418
}
419

    
420
void init_checksum(ByteIOContext *s,
421
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
422
                   unsigned long checksum)
423
{
424
    s->update_checksum= update_checksum;
425
    if(s->update_checksum){
426
        s->checksum= checksum;
427
        s->checksum_ptr= s->buf_ptr;
428
    }
429
}
430

    
431
/* XXX: put an inline version */
432
int get_byte(ByteIOContext *s)
433
{
434
    if (s->buf_ptr >= s->buf_end)
435
        fill_buffer(s);
436
    if (s->buf_ptr < s->buf_end)
437
        return *s->buf_ptr++;
438
    return 0;
439
}
440

    
441
int url_fgetc(ByteIOContext *s)
442
{
443
    if (s->buf_ptr >= s->buf_end)
444
        fill_buffer(s);
445
    if (s->buf_ptr < s->buf_end)
446
        return *s->buf_ptr++;
447
    return URL_EOF;
448
}
449

    
450
int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
451
{
452
    int len, size1;
453

    
454
    size1 = size;
455
    while (size > 0) {
456
        len = s->buf_end - s->buf_ptr;
457
        if (len > size)
458
            len = size;
459
        if (len == 0) {
460
            if(size > s->buffer_size && !s->update_checksum){
461
                if(s->read_packet)
462
                    len = s->read_packet(s->opaque, buf, size);
463
                if (len <= 0) {
464
                    /* do not modify buffer if EOF reached so that a seek back can
465
                    be done without rereading data */
466
                    s->eof_reached = 1;
467
                    if(len<0)
468
                        s->error= len;
469
                    break;
470
                } else {
471
                    s->pos += len;
472
                    size -= len;
473
                    buf += len;
474
                    s->buf_ptr = s->buffer;
475
                    s->buf_end = s->buffer/* + len*/;
476
                }
477
            }else{
478
                fill_buffer(s);
479
                len = s->buf_end - s->buf_ptr;
480
                if (len == 0)
481
                    break;
482
            }
483
        } else {
484
            memcpy(buf, s->buf_ptr, len);
485
            buf += len;
486
            s->buf_ptr += len;
487
            size -= len;
488
        }
489
    }
490
    if (size1 == size) {
491
        if (url_ferror(s)) return url_ferror(s);
492
        if (url_feof(s))   return AVERROR_EOF;
493
    }
494
    return size1 - size;
495
}
496

    
497
int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
498
{
499
    int len;
500

    
501
    if(size<0)
502
        return -1;
503

    
504
    len = s->buf_end - s->buf_ptr;
505
    if (len == 0) {
506
        fill_buffer(s);
507
        len = s->buf_end - s->buf_ptr;
508
    }
509
    if (len > size)
510
        len = size;
511
    memcpy(buf, s->buf_ptr, len);
512
    s->buf_ptr += len;
513
    if (!len) {
514
        if (url_ferror(s)) return url_ferror(s);
515
        if (url_feof(s))   return AVERROR_EOF;
516
    }
517
    return len;
518
}
519

    
520
unsigned int get_le16(ByteIOContext *s)
521
{
522
    unsigned int val;
523
    val = get_byte(s);
524
    val |= get_byte(s) << 8;
525
    return val;
526
}
527

    
528
unsigned int get_le24(ByteIOContext *s)
529
{
530
    unsigned int val;
531
    val = get_le16(s);
532
    val |= get_byte(s) << 16;
533
    return val;
534
}
535

    
536
unsigned int get_le32(ByteIOContext *s)
537
{
538
    unsigned int val;
539
    val = get_le16(s);
540
    val |= get_le16(s) << 16;
541
    return val;
542
}
543

    
544
uint64_t get_le64(ByteIOContext *s)
545
{
546
    uint64_t val;
547
    val = (uint64_t)get_le32(s);
548
    val |= (uint64_t)get_le32(s) << 32;
549
    return val;
550
}
551

    
552
unsigned int get_be16(ByteIOContext *s)
553
{
554
    unsigned int val;
555
    val = get_byte(s) << 8;
556
    val |= get_byte(s);
557
    return val;
558
}
559

    
560
unsigned int get_be24(ByteIOContext *s)
561
{
562
    unsigned int val;
563
    val = get_be16(s) << 8;
564
    val |= get_byte(s);
565
    return val;
566
}
567
unsigned int get_be32(ByteIOContext *s)
568
{
569
    unsigned int val;
570
    val = get_be16(s) << 16;
571
    val |= get_be16(s);
572
    return val;
573
}
574

    
575
char *get_strz(ByteIOContext *s, char *buf, int maxlen)
576
{
577
    int i = 0;
578
    char c;
579

    
580
    while ((c = get_byte(s))) {
581
        if (i < maxlen-1)
582
            buf[i++] = c;
583
    }
584

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

    
587
    return buf;
588
}
589

    
590
int ff_get_line(ByteIOContext *s, char *buf, int maxlen)
591
{
592
    int i = 0;
593
    char c;
594

    
595
    do {
596
        c = get_byte(s);
597
        if (c && i < maxlen-1)
598
            buf[i++] = c;
599
    } while (c != '\n' && c);
600

    
601
    buf[i] = 0;
602
    return i;
603
}
604

    
605
#define GET_STR16(type, read) \
606
    int avio_get_str16 ##type(ByteIOContext *pb, int maxlen, char *buf, int buflen)\
607
{\
608
    char* q = buf;\
609
    int ret = 0;\
610
    while (ret + 1 < maxlen) {\
611
        uint8_t tmp;\
612
        uint32_t ch;\
613
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
614
        if (!ch)\
615
            break;\
616
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
617
    }\
618
    *q = 0;\
619
    return ret;\
620
}\
621

    
622
GET_STR16(le, get_le16)
623
GET_STR16(be, get_be16)
624

    
625
#undef GET_STR16
626

    
627
uint64_t get_be64(ByteIOContext *s)
628
{
629
    uint64_t val;
630
    val = (uint64_t)get_be32(s) << 32;
631
    val |= (uint64_t)get_be32(s);
632
    return val;
633
}
634

    
635
uint64_t ff_get_v(ByteIOContext *bc){
636
    uint64_t val = 0;
637
    int tmp;
638

    
639
    do{
640
        tmp = get_byte(bc);
641
        val= (val<<7) + (tmp&127);
642
    }while(tmp&128);
643
    return val;
644
}
645

    
646
int url_fdopen(ByteIOContext **s, URLContext *h)
647
{
648
    uint8_t *buffer;
649
    int buffer_size, max_packet_size;
650

    
651
    max_packet_size = url_get_max_packet_size(h);
652
    if (max_packet_size) {
653
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
654
    } else {
655
        buffer_size = IO_BUFFER_SIZE;
656
    }
657
    buffer = av_malloc(buffer_size);
658
    if (!buffer)
659
        return AVERROR(ENOMEM);
660

    
661
    *s = av_mallocz(sizeof(ByteIOContext));
662
    if(!*s) {
663
        av_free(buffer);
664
        return AVERROR(ENOMEM);
665
    }
666

    
667
    if (init_put_byte(*s, buffer, buffer_size,
668
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
669
                      url_read, url_write, url_seek) < 0) {
670
        av_free(buffer);
671
        av_freep(s);
672
        return AVERROR(EIO);
673
    }
674
    (*s)->is_streamed = h->is_streamed;
675
    (*s)->max_packet_size = max_packet_size;
676
    if(h->prot) {
677
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
678
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
679
    }
680
    return 0;
681
}
682

    
683
int url_setbufsize(ByteIOContext *s, int buf_size)
684
{
685
    uint8_t *buffer;
686
    buffer = av_malloc(buf_size);
687
    if (!buffer)
688
        return AVERROR(ENOMEM);
689

    
690
    av_free(s->buffer);
691
    s->buffer = buffer;
692
    s->buffer_size = buf_size;
693
    s->buf_ptr = buffer;
694
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
695
    return 0;
696
}
697

    
698
#if FF_API_URL_RESETBUF
699
int url_resetbuf(ByteIOContext *s, int flags)
700
#else
701
static int url_resetbuf(ByteIOContext *s, int flags)
702
#endif
703
{
704
#if FF_API_URL_RESETBUF
705
    if (flags & URL_RDWR)
706
        return AVERROR(EINVAL);
707
#else
708
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
709
#endif
710

    
711
    if (flags & URL_WRONLY) {
712
        s->buf_end = s->buffer + s->buffer_size;
713
        s->write_flag = 1;
714
    } else {
715
        s->buf_end = s->buffer;
716
        s->write_flag = 0;
717
    }
718
    return 0;
719
}
720

    
721
int ff_rewind_with_probe_data(ByteIOContext *s, unsigned char *buf, int buf_size)
722
{
723
    int64_t buffer_start;
724
    int buffer_size;
725
    int overlap, new_size, alloc_size;
726

    
727
    if (s->write_flag)
728
        return AVERROR(EINVAL);
729

    
730
    buffer_size = s->buf_end - s->buffer;
731

    
732
    /* the buffers must touch or overlap */
733
    if ((buffer_start = s->pos - buffer_size) > buf_size)
734
        return AVERROR(EINVAL);
735

    
736
    overlap = buf_size - buffer_start;
737
    new_size = buf_size + buffer_size - overlap;
738

    
739
    alloc_size = FFMAX(s->buffer_size, new_size);
740
    if (alloc_size > buf_size)
741
        if (!(buf = av_realloc(buf, alloc_size)))
742
            return AVERROR(ENOMEM);
743

    
744
    if (new_size > buf_size) {
745
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
746
        buf_size = new_size;
747
    }
748

    
749
    av_free(s->buffer);
750
    s->buf_ptr = s->buffer = buf;
751
    s->buffer_size = alloc_size;
752
    s->pos = buf_size;
753
    s->buf_end = s->buf_ptr + buf_size;
754
    s->eof_reached = 0;
755
    s->must_flush = 0;
756

    
757
    return 0;
758
}
759

    
760
int url_fopen(ByteIOContext **s, const char *filename, int flags)
761
{
762
    URLContext *h;
763
    int err;
764

    
765
    err = url_open(&h, filename, flags);
766
    if (err < 0)
767
        return err;
768
    err = url_fdopen(s, h);
769
    if (err < 0) {
770
        url_close(h);
771
        return err;
772
    }
773
    return 0;
774
}
775

    
776
int url_fclose(ByteIOContext *s)
777
{
778
    URLContext *h = s->opaque;
779

    
780
    av_free(s->buffer);
781
    av_free(s);
782
    return url_close(h);
783
}
784

    
785
URLContext *url_fileno(ByteIOContext *s)
786
{
787
    return s->opaque;
788
}
789

    
790
#if CONFIG_MUXERS
791
int url_fprintf(ByteIOContext *s, const char *fmt, ...)
792
{
793
    va_list ap;
794
    char buf[4096];
795
    int ret;
796

    
797
    va_start(ap, fmt);
798
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
799
    va_end(ap);
800
    put_buffer(s, buf, strlen(buf));
801
    return ret;
802
}
803
#endif //CONFIG_MUXERS
804

    
805
char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
806
{
807
    int c;
808
    char *q;
809

    
810
    c = url_fgetc(s);
811
    if (c == EOF)
812
        return NULL;
813
    q = buf;
814
    for(;;) {
815
        if (c == EOF || c == '\n')
816
            break;
817
        if ((q - buf) < buf_size - 1)
818
            *q++ = c;
819
        c = url_fgetc(s);
820
    }
821
    if (buf_size > 0)
822
        *q = '\0';
823
    return buf;
824
}
825

    
826
int url_fget_max_packet_size(ByteIOContext *s)
827
{
828
    return s->max_packet_size;
829
}
830

    
831
int av_url_read_fpause(ByteIOContext *s, int pause)
832
{
833
    if (!s->read_pause)
834
        return AVERROR(ENOSYS);
835
    return s->read_pause(s->opaque, pause);
836
}
837

    
838
int64_t av_url_read_fseek(ByteIOContext *s, int stream_index,
839
                          int64_t timestamp, int flags)
840
{
841
    URLContext *h = s->opaque;
842
    int64_t ret;
843
    if (!s->read_seek)
844
        return AVERROR(ENOSYS);
845
    ret = s->read_seek(h, stream_index, timestamp, flags);
846
    if(ret >= 0) {
847
        int64_t pos;
848
        s->buf_ptr = s->buf_end; // Flush buffer
849
        pos = s->seek(h, 0, SEEK_CUR);
850
        if (pos >= 0)
851
            s->pos = pos;
852
        else if (pos != AVERROR(ENOSYS))
853
            ret = pos;
854
    }
855
    return ret;
856
}
857

    
858
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
859
 * back to the server even if CONFIG_MUXERS is false. */
860
#if CONFIG_MUXERS || CONFIG_NETWORK
861
/* buffer handling */
862
int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags)
863
{
864
    int ret;
865
    *s = av_mallocz(sizeof(ByteIOContext));
866
    if(!*s)
867
        return AVERROR(ENOMEM);
868
    ret = init_put_byte(*s, buf, buf_size,
869
                        (flags & URL_WRONLY || flags & URL_RDWR),
870
                        NULL, NULL, NULL, NULL);
871
    if(ret != 0)
872
        av_freep(s);
873
    return ret;
874
}
875

    
876
int url_close_buf(ByteIOContext *s)
877
{
878
    put_flush_packet(s);
879
    return s->buf_ptr - s->buffer;
880
}
881

    
882
/* output in a dynamic buffer */
883

    
884
typedef struct DynBuffer {
885
    int pos, size, allocated_size;
886
    uint8_t *buffer;
887
    int io_buffer_size;
888
    uint8_t io_buffer[1];
889
} DynBuffer;
890

    
891
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
892
{
893
    DynBuffer *d = opaque;
894
    unsigned new_size, new_allocated_size;
895

    
896
    /* reallocate buffer if needed */
897
    new_size = d->pos + buf_size;
898
    new_allocated_size = d->allocated_size;
899
    if(new_size < d->pos || new_size > INT_MAX/2)
900
        return -1;
901
    while (new_size > new_allocated_size) {
902
        if (!new_allocated_size)
903
            new_allocated_size = new_size;
904
        else
905
            new_allocated_size += new_allocated_size / 2 + 1;
906
    }
907

    
908
    if (new_allocated_size > d->allocated_size) {
909
        d->buffer = av_realloc(d->buffer, new_allocated_size);
910
        if(d->buffer == NULL)
911
             return AVERROR(ENOMEM);
912
        d->allocated_size = new_allocated_size;
913
    }
914
    memcpy(d->buffer + d->pos, buf, buf_size);
915
    d->pos = new_size;
916
    if (d->pos > d->size)
917
        d->size = d->pos;
918
    return buf_size;
919
}
920

    
921
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
922
{
923
    unsigned char buf1[4];
924
    int ret;
925

    
926
    /* packetized write: output the header */
927
    AV_WB32(buf1, buf_size);
928
    ret= dyn_buf_write(opaque, buf1, 4);
929
    if(ret < 0)
930
        return ret;
931

    
932
    /* then the data */
933
    return dyn_buf_write(opaque, buf, buf_size);
934
}
935

    
936
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
937
{
938
    DynBuffer *d = opaque;
939

    
940
    if (whence == SEEK_CUR)
941
        offset += d->pos;
942
    else if (whence == SEEK_END)
943
        offset += d->size;
944
    if (offset < 0 || offset > 0x7fffffffLL)
945
        return -1;
946
    d->pos = offset;
947
    return 0;
948
}
949

    
950
static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size)
951
{
952
    DynBuffer *d;
953
    int ret;
954
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
955

    
956
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
957
        return -1;
958
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
959
    if (!d)
960
        return AVERROR(ENOMEM);
961
    *s = av_mallocz(sizeof(ByteIOContext));
962
    if(!*s) {
963
        av_free(d);
964
        return AVERROR(ENOMEM);
965
    }
966
    d->io_buffer_size = io_buffer_size;
967
    ret = init_put_byte(*s, d->io_buffer, io_buffer_size,
968
                        1, d, NULL,
969
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
970
                        max_packet_size ? NULL : dyn_buf_seek);
971
    if (ret == 0) {
972
        (*s)->max_packet_size = max_packet_size;
973
    } else {
974
        av_free(d);
975
        av_freep(s);
976
    }
977
    return ret;
978
}
979

    
980
int url_open_dyn_buf(ByteIOContext **s)
981
{
982
    return url_open_dyn_buf_internal(s, 0);
983
}
984

    
985
int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size)
986
{
987
    if (max_packet_size <= 0)
988
        return -1;
989
    return url_open_dyn_buf_internal(s, max_packet_size);
990
}
991

    
992
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
993
{
994
    DynBuffer *d = s->opaque;
995
    int size;
996
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
997
    int padding = 0;
998

    
999
    /* don't attempt to pad fixed-size packet buffers */
1000
    if (!s->max_packet_size) {
1001
        put_buffer(s, padbuf, sizeof(padbuf));
1002
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1003
    }
1004

    
1005
    put_flush_packet(s);
1006

    
1007
    *pbuffer = d->buffer;
1008
    size = d->size;
1009
    av_free(d);
1010
    av_free(s);
1011
    return size - padding;
1012
}
1013
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */