Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ ae99313a

History | View | Annotate | Download (24.5 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 "avio_internal.h"
27
#include "internal.h"
28
#include <stdarg.h>
29

    
30
#define IO_BUFFER_SIZE 32768
31

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

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

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

    
77
#if FF_API_OLD_AVIO
78
int init_put_byte(AVIOContext *s,
79
                  unsigned char *buffer,
80
                  int buffer_size,
81
                  int write_flag,
82
                  void *opaque,
83
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
84
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
85
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
86
{
87
    return ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
88
                                read_packet, write_packet, seek);
89
}
90
#endif
91

    
92
AVIOContext *av_alloc_put_byte(
93
                  unsigned char *buffer,
94
                  int buffer_size,
95
                  int write_flag,
96
                  void *opaque,
97
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
98
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
99
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
100
{
101
    AVIOContext *s = av_mallocz(sizeof(AVIOContext));
102
    ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
103
                  read_packet, write_packet, seek);
104
    return s;
105
}
106

    
107
static void flush_buffer(AVIOContext *s)
108
{
109
    if (s->buf_ptr > s->buffer) {
110
        if (s->write_packet && !s->error){
111
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
112
            if(ret < 0){
113
                s->error = ret;
114
            }
115
        }
116
        if(s->update_checksum){
117
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
118
            s->checksum_ptr= s->buffer;
119
        }
120
        s->pos += s->buf_ptr - s->buffer;
121
    }
122
    s->buf_ptr = s->buffer;
123
}
124

    
125
void put_byte(AVIOContext *s, int b)
126
{
127
    *(s->buf_ptr)++ = b;
128
    if (s->buf_ptr >= s->buf_end)
129
        flush_buffer(s);
130
}
131

    
132
void put_nbyte(AVIOContext *s, int b, int count)
133
{
134
    while (count > 0) {
135
        int len = FFMIN(s->buf_end - s->buf_ptr, count);
136
        memset(s->buf_ptr, b, len);
137
        s->buf_ptr += len;
138

    
139
        if (s->buf_ptr >= s->buf_end)
140
            flush_buffer(s);
141

    
142
        count -= len;
143
    }
144
}
145

    
146
void put_buffer(AVIOContext *s, const unsigned char *buf, int size)
147
{
148
    while (size > 0) {
149
        int len = FFMIN(s->buf_end - s->buf_ptr, size);
150
        memcpy(s->buf_ptr, buf, len);
151
        s->buf_ptr += len;
152

    
153
        if (s->buf_ptr >= s->buf_end)
154
            flush_buffer(s);
155

    
156
        buf += len;
157
        size -= len;
158
    }
159
}
160

    
161
void put_flush_packet(AVIOContext *s)
162
{
163
    flush_buffer(s);
164
    s->must_flush = 0;
165
}
166

    
167
int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
168
{
169
    int64_t offset1;
170
    int64_t pos;
171
    int force = whence & AVSEEK_FORCE;
172
    whence &= ~AVSEEK_FORCE;
173

    
174
    if(!s)
175
        return AVERROR(EINVAL);
176

    
177
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
178

    
179
    if (whence != SEEK_CUR && whence != SEEK_SET)
180
        return AVERROR(EINVAL);
181

    
182
    if (whence == SEEK_CUR) {
183
        offset1 = pos + (s->buf_ptr - s->buffer);
184
        if (offset == 0)
185
            return offset1;
186
        offset += offset1;
187
    }
188
    offset1 = offset - pos;
189
    if (!s->must_flush &&
190
        offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
191
        /* can do the seek inside the buffer */
192
        s->buf_ptr = s->buffer + offset1;
193
    } else if ((s->is_streamed ||
194
               offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
195
               !s->write_flag && offset1 >= 0 &&
196
              (whence != SEEK_END || force)) {
197
        while(s->pos < offset && !s->eof_reached)
198
            fill_buffer(s);
199
        if (s->eof_reached)
200
            return AVERROR_EOF;
201
        s->buf_ptr = s->buf_end + offset - s->pos;
202
    } else {
203
        int64_t res;
204

    
205
#if CONFIG_MUXERS || CONFIG_NETWORK
206
        if (s->write_flag) {
207
            flush_buffer(s);
208
            s->must_flush = 1;
209
        }
210
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
211
        if (!s->seek)
212
            return AVERROR(EPIPE);
213
        if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
214
            return res;
215
        if (!s->write_flag)
216
            s->buf_end = s->buffer;
217
        s->buf_ptr = s->buffer;
218
        s->pos = offset;
219
    }
220
    s->eof_reached = 0;
221
    return offset;
222
}
223

    
224
int url_fskip(AVIOContext *s, int64_t offset)
225
{
226
    int64_t ret = url_fseek(s, offset, SEEK_CUR);
227
    return ret < 0 ? ret : 0;
228
}
229

    
230
int64_t url_ftell(AVIOContext *s)
231
{
232
    return url_fseek(s, 0, SEEK_CUR);
233
}
234

    
235
int64_t url_fsize(AVIOContext *s)
236
{
237
    int64_t size;
238

    
239
    if(!s)
240
        return AVERROR(EINVAL);
241

    
242
    if (!s->seek)
243
        return AVERROR(ENOSYS);
244
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
245
    if(size<0){
246
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
247
            return size;
248
        size++;
249
        s->seek(s->opaque, s->pos, SEEK_SET);
250
    }
251
    return size;
252
}
253

    
254
int url_feof(AVIOContext *s)
255
{
256
    if(!s)
257
        return 0;
258
    return s->eof_reached;
259
}
260

    
261
int url_ferror(AVIOContext *s)
262
{
263
    if(!s)
264
        return 0;
265
    return s->error;
266
}
267

    
268
void put_le32(AVIOContext *s, unsigned int val)
269
{
270
    put_byte(s, val);
271
    put_byte(s, val >> 8);
272
    put_byte(s, val >> 16);
273
    put_byte(s, val >> 24);
274
}
275

    
276
void put_be32(AVIOContext *s, unsigned int val)
277
{
278
    put_byte(s, val >> 24);
279
    put_byte(s, val >> 16);
280
    put_byte(s, val >> 8);
281
    put_byte(s, val);
282
}
283

    
284
#if FF_API_OLD_AVIO
285
void put_strz(AVIOContext *s, const char *str)
286
{
287
    avio_put_str(s, str);
288
}
289
#endif
290

    
291
int avio_put_str(AVIOContext *s, const char *str)
292
{
293
    int len = 1;
294
    if (str) {
295
        len += strlen(str);
296
        put_buffer(s, (const unsigned char *) str, len);
297
    } else
298
        put_byte(s, 0);
299
    return len;
300
}
301

    
302
int avio_put_str16le(AVIOContext *s, const char *str)
303
{
304
    const uint8_t *q = str;
305
    int ret = 0;
306

    
307
    while (*q) {
308
        uint32_t ch;
309
        uint16_t tmp;
310

    
311
        GET_UTF8(ch, *q++, break;)
312
        PUT_UTF16(ch, tmp, put_le16(s, tmp);ret += 2;)
313
    }
314
    put_le16(s, 0);
315
    ret += 2;
316
    return ret;
317
}
318

    
319
int ff_get_v_length(uint64_t val){
320
    int i=1;
321

    
322
    while(val>>=7)
323
        i++;
324

    
325
    return i;
326
}
327

    
328
void ff_put_v(AVIOContext *bc, uint64_t val){
329
    int i= ff_get_v_length(val);
330

    
331
    while(--i>0)
332
        put_byte(bc, 128 | (val>>(7*i)));
333

    
334
    put_byte(bc, val&127);
335
}
336

    
337
void put_le64(AVIOContext *s, uint64_t val)
338
{
339
    put_le32(s, (uint32_t)(val & 0xffffffff));
340
    put_le32(s, (uint32_t)(val >> 32));
341
}
342

    
343
void put_be64(AVIOContext *s, uint64_t val)
344
{
345
    put_be32(s, (uint32_t)(val >> 32));
346
    put_be32(s, (uint32_t)(val & 0xffffffff));
347
}
348

    
349
void put_le16(AVIOContext *s, unsigned int val)
350
{
351
    put_byte(s, val);
352
    put_byte(s, val >> 8);
353
}
354

    
355
void put_be16(AVIOContext *s, unsigned int val)
356
{
357
    put_byte(s, val >> 8);
358
    put_byte(s, val);
359
}
360

    
361
void put_le24(AVIOContext *s, unsigned int val)
362
{
363
    put_le16(s, val & 0xffff);
364
    put_byte(s, val >> 16);
365
}
366

    
367
void put_be24(AVIOContext *s, unsigned int val)
368
{
369
    put_be16(s, val >> 8);
370
    put_byte(s, val);
371
}
372

    
373
void put_tag(AVIOContext *s, const char *tag)
374
{
375
    while (*tag) {
376
        put_byte(s, *tag++);
377
    }
378
}
379

    
380
/* Input stream */
381

    
382
static void fill_buffer(AVIOContext *s)
383
{
384
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
385
    int len= s->buffer_size - (dst - s->buffer);
386
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
387

    
388
    /* no need to do anything if EOF already reached */
389
    if (s->eof_reached)
390
        return;
391

    
392
    if(s->update_checksum && dst == s->buffer){
393
        if(s->buf_end > s->checksum_ptr)
394
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
395
        s->checksum_ptr= s->buffer;
396
    }
397

    
398
    /* make buffer smaller in case it ended up large after probing */
399
    if (s->buffer_size > max_buffer_size) {
400
        url_setbufsize(s, max_buffer_size);
401

    
402
        s->checksum_ptr = dst = s->buffer;
403
        len = s->buffer_size;
404
    }
405

    
406
    if(s->read_packet)
407
        len = s->read_packet(s->opaque, dst, len);
408
    else
409
        len = 0;
410
    if (len <= 0) {
411
        /* do not modify buffer if EOF reached so that a seek back can
412
           be done without rereading data */
413
        s->eof_reached = 1;
414
        if(len<0)
415
            s->error= len;
416
    } else {
417
        s->pos += len;
418
        s->buf_ptr = dst;
419
        s->buf_end = dst + len;
420
    }
421
}
422

    
423
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
424
                                    unsigned int len)
425
{
426
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
427
}
428

    
429
unsigned long get_checksum(AVIOContext *s)
430
{
431
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
432
    s->update_checksum= NULL;
433
    return s->checksum;
434
}
435

    
436
void init_checksum(AVIOContext *s,
437
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
438
                   unsigned long checksum)
439
{
440
    s->update_checksum= update_checksum;
441
    if(s->update_checksum){
442
        s->checksum= checksum;
443
        s->checksum_ptr= s->buf_ptr;
444
    }
445
}
446

    
447
/* XXX: put an inline version */
448
int get_byte(AVIOContext *s)
449
{
450
    if (s->buf_ptr >= s->buf_end)
451
        fill_buffer(s);
452
    if (s->buf_ptr < s->buf_end)
453
        return *s->buf_ptr++;
454
    return 0;
455
}
456

    
457
int url_fgetc(AVIOContext *s)
458
{
459
    if (s->buf_ptr >= s->buf_end)
460
        fill_buffer(s);
461
    if (s->buf_ptr < s->buf_end)
462
        return *s->buf_ptr++;
463
    return URL_EOF;
464
}
465

    
466
int get_buffer(AVIOContext *s, unsigned char *buf, int size)
467
{
468
    int len, size1;
469

    
470
    size1 = size;
471
    while (size > 0) {
472
        len = s->buf_end - s->buf_ptr;
473
        if (len > size)
474
            len = size;
475
        if (len == 0) {
476
            if(size > s->buffer_size && !s->update_checksum){
477
                if(s->read_packet)
478
                    len = s->read_packet(s->opaque, buf, size);
479
                if (len <= 0) {
480
                    /* do not modify buffer if EOF reached so that a seek back can
481
                    be done without rereading data */
482
                    s->eof_reached = 1;
483
                    if(len<0)
484
                        s->error= len;
485
                    break;
486
                } else {
487
                    s->pos += len;
488
                    size -= len;
489
                    buf += len;
490
                    s->buf_ptr = s->buffer;
491
                    s->buf_end = s->buffer/* + len*/;
492
                }
493
            }else{
494
                fill_buffer(s);
495
                len = s->buf_end - s->buf_ptr;
496
                if (len == 0)
497
                    break;
498
            }
499
        } else {
500
            memcpy(buf, s->buf_ptr, len);
501
            buf += len;
502
            s->buf_ptr += len;
503
            size -= len;
504
        }
505
    }
506
    if (size1 == size) {
507
        if (url_ferror(s)) return url_ferror(s);
508
        if (url_feof(s))   return AVERROR_EOF;
509
    }
510
    return size1 - size;
511
}
512

    
513
int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size)
514
{
515
    int len;
516

    
517
    if(size<0)
518
        return -1;
519

    
520
    len = s->buf_end - s->buf_ptr;
521
    if (len == 0) {
522
        fill_buffer(s);
523
        len = s->buf_end - s->buf_ptr;
524
    }
525
    if (len > size)
526
        len = size;
527
    memcpy(buf, s->buf_ptr, len);
528
    s->buf_ptr += len;
529
    if (!len) {
530
        if (url_ferror(s)) return url_ferror(s);
531
        if (url_feof(s))   return AVERROR_EOF;
532
    }
533
    return len;
534
}
535

    
536
unsigned int get_le16(AVIOContext *s)
537
{
538
    unsigned int val;
539
    val = get_byte(s);
540
    val |= get_byte(s) << 8;
541
    return val;
542
}
543

    
544
unsigned int get_le24(AVIOContext *s)
545
{
546
    unsigned int val;
547
    val = get_le16(s);
548
    val |= get_byte(s) << 16;
549
    return val;
550
}
551

    
552
unsigned int get_le32(AVIOContext *s)
553
{
554
    unsigned int val;
555
    val = get_le16(s);
556
    val |= get_le16(s) << 16;
557
    return val;
558
}
559

    
560
uint64_t get_le64(AVIOContext *s)
561
{
562
    uint64_t val;
563
    val = (uint64_t)get_le32(s);
564
    val |= (uint64_t)get_le32(s) << 32;
565
    return val;
566
}
567

    
568
unsigned int get_be16(AVIOContext *s)
569
{
570
    unsigned int val;
571
    val = get_byte(s) << 8;
572
    val |= get_byte(s);
573
    return val;
574
}
575

    
576
unsigned int get_be24(AVIOContext *s)
577
{
578
    unsigned int val;
579
    val = get_be16(s) << 8;
580
    val |= get_byte(s);
581
    return val;
582
}
583
unsigned int get_be32(AVIOContext *s)
584
{
585
    unsigned int val;
586
    val = get_be16(s) << 16;
587
    val |= get_be16(s);
588
    return val;
589
}
590

    
591
char *get_strz(AVIOContext *s, char *buf, int maxlen)
592
{
593
    int i = 0;
594
    char c;
595

    
596
    while ((c = get_byte(s))) {
597
        if (i < maxlen-1)
598
            buf[i++] = c;
599
    }
600

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

    
603
    return buf;
604
}
605

    
606
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
607
{
608
    int i = 0;
609
    char c;
610

    
611
    do {
612
        c = get_byte(s);
613
        if (c && i < maxlen-1)
614
            buf[i++] = c;
615
    } while (c != '\n' && c);
616

    
617
    buf[i] = 0;
618
    return i;
619
}
620

    
621
#define GET_STR16(type, read) \
622
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
623
{\
624
    char* q = buf;\
625
    int ret = 0;\
626
    while (ret + 1 < maxlen) {\
627
        uint8_t tmp;\
628
        uint32_t ch;\
629
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
630
        if (!ch)\
631
            break;\
632
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
633
    }\
634
    *q = 0;\
635
    return ret;\
636
}\
637

    
638
GET_STR16(le, get_le16)
639
GET_STR16(be, get_be16)
640

    
641
#undef GET_STR16
642

    
643
uint64_t get_be64(AVIOContext *s)
644
{
645
    uint64_t val;
646
    val = (uint64_t)get_be32(s) << 32;
647
    val |= (uint64_t)get_be32(s);
648
    return val;
649
}
650

    
651
uint64_t ff_get_v(AVIOContext *bc){
652
    uint64_t val = 0;
653
    int tmp;
654

    
655
    do{
656
        tmp = get_byte(bc);
657
        val= (val<<7) + (tmp&127);
658
    }while(tmp&128);
659
    return val;
660
}
661

    
662
int url_fdopen(AVIOContext **s, URLContext *h)
663
{
664
    uint8_t *buffer;
665
    int buffer_size, max_packet_size;
666

    
667
    max_packet_size = url_get_max_packet_size(h);
668
    if (max_packet_size) {
669
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
670
    } else {
671
        buffer_size = IO_BUFFER_SIZE;
672
    }
673
    buffer = av_malloc(buffer_size);
674
    if (!buffer)
675
        return AVERROR(ENOMEM);
676

    
677
    *s = av_mallocz(sizeof(AVIOContext));
678
    if(!*s) {
679
        av_free(buffer);
680
        return AVERROR(ENOMEM);
681
    }
682

    
683
    if (ffio_init_context(*s, buffer, buffer_size,
684
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
685
                      url_read, url_write, url_seek) < 0) {
686
        av_free(buffer);
687
        av_freep(s);
688
        return AVERROR(EIO);
689
    }
690
    (*s)->is_streamed = h->is_streamed;
691
    (*s)->max_packet_size = max_packet_size;
692
    if(h->prot) {
693
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
694
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
695
    }
696
    return 0;
697
}
698

    
699
int url_setbufsize(AVIOContext *s, int buf_size)
700
{
701
    uint8_t *buffer;
702
    buffer = av_malloc(buf_size);
703
    if (!buffer)
704
        return AVERROR(ENOMEM);
705

    
706
    av_free(s->buffer);
707
    s->buffer = buffer;
708
    s->buffer_size = buf_size;
709
    s->buf_ptr = buffer;
710
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
711
    return 0;
712
}
713

    
714
#if FF_API_URL_RESETBUF
715
int url_resetbuf(AVIOContext *s, int flags)
716
#else
717
static int url_resetbuf(AVIOContext *s, int flags)
718
#endif
719
{
720
#if FF_API_URL_RESETBUF
721
    if (flags & URL_RDWR)
722
        return AVERROR(EINVAL);
723
#else
724
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
725
#endif
726

    
727
    if (flags & URL_WRONLY) {
728
        s->buf_end = s->buffer + s->buffer_size;
729
        s->write_flag = 1;
730
    } else {
731
        s->buf_end = s->buffer;
732
        s->write_flag = 0;
733
    }
734
    return 0;
735
}
736

    
737
int ff_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
738
{
739
    int64_t buffer_start;
740
    int buffer_size;
741
    int overlap, new_size, alloc_size;
742

    
743
    if (s->write_flag)
744
        return AVERROR(EINVAL);
745

    
746
    buffer_size = s->buf_end - s->buffer;
747

    
748
    /* the buffers must touch or overlap */
749
    if ((buffer_start = s->pos - buffer_size) > buf_size)
750
        return AVERROR(EINVAL);
751

    
752
    overlap = buf_size - buffer_start;
753
    new_size = buf_size + buffer_size - overlap;
754

    
755
    alloc_size = FFMAX(s->buffer_size, new_size);
756
    if (alloc_size > buf_size)
757
        if (!(buf = av_realloc(buf, alloc_size)))
758
            return AVERROR(ENOMEM);
759

    
760
    if (new_size > buf_size) {
761
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
762
        buf_size = new_size;
763
    }
764

    
765
    av_free(s->buffer);
766
    s->buf_ptr = s->buffer = buf;
767
    s->buffer_size = alloc_size;
768
    s->pos = buf_size;
769
    s->buf_end = s->buf_ptr + buf_size;
770
    s->eof_reached = 0;
771
    s->must_flush = 0;
772

    
773
    return 0;
774
}
775

    
776
int url_fopen(AVIOContext **s, const char *filename, int flags)
777
{
778
    URLContext *h;
779
    int err;
780

    
781
    err = url_open(&h, filename, flags);
782
    if (err < 0)
783
        return err;
784
    err = url_fdopen(s, h);
785
    if (err < 0) {
786
        url_close(h);
787
        return err;
788
    }
789
    return 0;
790
}
791

    
792
int url_fclose(AVIOContext *s)
793
{
794
    URLContext *h = s->opaque;
795

    
796
    av_free(s->buffer);
797
    av_free(s);
798
    return url_close(h);
799
}
800

    
801
URLContext *url_fileno(AVIOContext *s)
802
{
803
    return s->opaque;
804
}
805

    
806
#if CONFIG_MUXERS
807
int url_fprintf(AVIOContext *s, const char *fmt, ...)
808
{
809
    va_list ap;
810
    char buf[4096];
811
    int ret;
812

    
813
    va_start(ap, fmt);
814
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
815
    va_end(ap);
816
    put_buffer(s, buf, strlen(buf));
817
    return ret;
818
}
819
#endif //CONFIG_MUXERS
820

    
821
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
822
{
823
    int c;
824
    char *q;
825

    
826
    c = url_fgetc(s);
827
    if (c == EOF)
828
        return NULL;
829
    q = buf;
830
    for(;;) {
831
        if (c == EOF || c == '\n')
832
            break;
833
        if ((q - buf) < buf_size - 1)
834
            *q++ = c;
835
        c = url_fgetc(s);
836
    }
837
    if (buf_size > 0)
838
        *q = '\0';
839
    return buf;
840
}
841

    
842
int url_fget_max_packet_size(AVIOContext *s)
843
{
844
    return s->max_packet_size;
845
}
846

    
847
int av_url_read_fpause(AVIOContext *s, int pause)
848
{
849
    if (!s->read_pause)
850
        return AVERROR(ENOSYS);
851
    return s->read_pause(s->opaque, pause);
852
}
853

    
854
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
855
                          int64_t timestamp, int flags)
856
{
857
    URLContext *h = s->opaque;
858
    int64_t ret;
859
    if (!s->read_seek)
860
        return AVERROR(ENOSYS);
861
    ret = s->read_seek(h, stream_index, timestamp, flags);
862
    if(ret >= 0) {
863
        int64_t pos;
864
        s->buf_ptr = s->buf_end; // Flush buffer
865
        pos = s->seek(h, 0, SEEK_CUR);
866
        if (pos >= 0)
867
            s->pos = pos;
868
        else if (pos != AVERROR(ENOSYS))
869
            ret = pos;
870
    }
871
    return ret;
872
}
873

    
874
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
875
 * back to the server even if CONFIG_MUXERS is false. */
876
#if CONFIG_MUXERS || CONFIG_NETWORK
877
/* buffer handling */
878
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
879
{
880
    int ret;
881
    *s = av_mallocz(sizeof(AVIOContext));
882
    if(!*s)
883
        return AVERROR(ENOMEM);
884
    ret = ffio_init_context(*s, buf, buf_size,
885
                        (flags & URL_WRONLY || flags & URL_RDWR),
886
                        NULL, NULL, NULL, NULL);
887
    if(ret != 0)
888
        av_freep(s);
889
    return ret;
890
}
891

    
892
int url_close_buf(AVIOContext *s)
893
{
894
    put_flush_packet(s);
895
    return s->buf_ptr - s->buffer;
896
}
897

    
898
/* output in a dynamic buffer */
899

    
900
typedef struct DynBuffer {
901
    int pos, size, allocated_size;
902
    uint8_t *buffer;
903
    int io_buffer_size;
904
    uint8_t io_buffer[1];
905
} DynBuffer;
906

    
907
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
908
{
909
    DynBuffer *d = opaque;
910
    unsigned new_size, new_allocated_size;
911

    
912
    /* reallocate buffer if needed */
913
    new_size = d->pos + buf_size;
914
    new_allocated_size = d->allocated_size;
915
    if(new_size < d->pos || new_size > INT_MAX/2)
916
        return -1;
917
    while (new_size > new_allocated_size) {
918
        if (!new_allocated_size)
919
            new_allocated_size = new_size;
920
        else
921
            new_allocated_size += new_allocated_size / 2 + 1;
922
    }
923

    
924
    if (new_allocated_size > d->allocated_size) {
925
        d->buffer = av_realloc(d->buffer, new_allocated_size);
926
        if(d->buffer == NULL)
927
             return AVERROR(ENOMEM);
928
        d->allocated_size = new_allocated_size;
929
    }
930
    memcpy(d->buffer + d->pos, buf, buf_size);
931
    d->pos = new_size;
932
    if (d->pos > d->size)
933
        d->size = d->pos;
934
    return buf_size;
935
}
936

    
937
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
938
{
939
    unsigned char buf1[4];
940
    int ret;
941

    
942
    /* packetized write: output the header */
943
    AV_WB32(buf1, buf_size);
944
    ret= dyn_buf_write(opaque, buf1, 4);
945
    if(ret < 0)
946
        return ret;
947

    
948
    /* then the data */
949
    return dyn_buf_write(opaque, buf, buf_size);
950
}
951

    
952
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
953
{
954
    DynBuffer *d = opaque;
955

    
956
    if (whence == SEEK_CUR)
957
        offset += d->pos;
958
    else if (whence == SEEK_END)
959
        offset += d->size;
960
    if (offset < 0 || offset > 0x7fffffffLL)
961
        return -1;
962
    d->pos = offset;
963
    return 0;
964
}
965

    
966
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
967
{
968
    DynBuffer *d;
969
    int ret;
970
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
971

    
972
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
973
        return -1;
974
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
975
    if (!d)
976
        return AVERROR(ENOMEM);
977
    *s = av_mallocz(sizeof(AVIOContext));
978
    if(!*s) {
979
        av_free(d);
980
        return AVERROR(ENOMEM);
981
    }
982
    d->io_buffer_size = io_buffer_size;
983
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
984
                        1, d, NULL,
985
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
986
                        max_packet_size ? NULL : dyn_buf_seek);
987
    if (ret == 0) {
988
        (*s)->max_packet_size = max_packet_size;
989
    } else {
990
        av_free(d);
991
        av_freep(s);
992
    }
993
    return ret;
994
}
995

    
996
int url_open_dyn_buf(AVIOContext **s)
997
{
998
    return url_open_dyn_buf_internal(s, 0);
999
}
1000

    
1001
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1002
{
1003
    if (max_packet_size <= 0)
1004
        return -1;
1005
    return url_open_dyn_buf_internal(s, max_packet_size);
1006
}
1007

    
1008
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1009
{
1010
    DynBuffer *d = s->opaque;
1011
    int size;
1012
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1013
    int padding = 0;
1014

    
1015
    /* don't attempt to pad fixed-size packet buffers */
1016
    if (!s->max_packet_size) {
1017
        put_buffer(s, padbuf, sizeof(padbuf));
1018
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1019
    }
1020

    
1021
    put_flush_packet(s);
1022

    
1023
    *pbuffer = d->buffer;
1024
    size = d->size;
1025
    av_free(d);
1026
    av_free(s);
1027
    return size - padding;
1028
}
1029
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */