Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 1447dc59

History | View | Annotate | Download (26.8 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
AVIOContext *av_alloc_put_byte(
91
                  unsigned char *buffer,
92
                  int buffer_size,
93
                  int write_flag,
94
                  void *opaque,
95
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
96
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
97
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
98
{
99
    return avio_alloc_context(buffer, buffer_size, write_flag, opaque,
100
                              read_packet, write_packet, seek);
101
}
102
#endif
103

    
104
AVIOContext *avio_alloc_context(
105
                  unsigned char *buffer,
106
                  int buffer_size,
107
                  int write_flag,
108
                  void *opaque,
109
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
110
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
111
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
112
{
113
    AVIOContext *s = av_mallocz(sizeof(AVIOContext));
114
    ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
115
                  read_packet, write_packet, seek);
116
    return s;
117
}
118

    
119
static void flush_buffer(AVIOContext *s)
120
{
121
    if (s->buf_ptr > s->buffer) {
122
        if (s->write_packet && !s->error){
123
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
124
            if(ret < 0){
125
                s->error = ret;
126
            }
127
        }
128
        if(s->update_checksum){
129
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
130
            s->checksum_ptr= s->buffer;
131
        }
132
        s->pos += s->buf_ptr - s->buffer;
133
    }
134
    s->buf_ptr = s->buffer;
135
}
136

    
137
void avio_w8(AVIOContext *s, int b)
138
{
139
    *(s->buf_ptr)++ = b;
140
    if (s->buf_ptr >= s->buf_end)
141
        flush_buffer(s);
142
}
143

    
144
void ffio_fill(AVIOContext *s, int b, int count)
145
{
146
    while (count > 0) {
147
        int len = FFMIN(s->buf_end - s->buf_ptr, count);
148
        memset(s->buf_ptr, b, len);
149
        s->buf_ptr += len;
150

    
151
        if (s->buf_ptr >= s->buf_end)
152
            flush_buffer(s);
153

    
154
        count -= len;
155
    }
156
}
157

    
158
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
159
{
160
    while (size > 0) {
161
        int len = FFMIN(s->buf_end - s->buf_ptr, size);
162
        memcpy(s->buf_ptr, buf, len);
163
        s->buf_ptr += len;
164

    
165
        if (s->buf_ptr >= s->buf_end)
166
            flush_buffer(s);
167

    
168
        buf += len;
169
        size -= len;
170
    }
171
}
172

    
173
void put_flush_packet(AVIOContext *s)
174
{
175
    flush_buffer(s);
176
    s->must_flush = 0;
177
}
178

    
179
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
180
{
181
    int64_t offset1;
182
    int64_t pos;
183
    int force = whence & AVSEEK_FORCE;
184
    whence &= ~AVSEEK_FORCE;
185

    
186
    if(!s)
187
        return AVERROR(EINVAL);
188

    
189
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
190

    
191
    if (whence != SEEK_CUR && whence != SEEK_SET)
192
        return AVERROR(EINVAL);
193

    
194
    if (whence == SEEK_CUR) {
195
        offset1 = pos + (s->buf_ptr - s->buffer);
196
        if (offset == 0)
197
            return offset1;
198
        offset += offset1;
199
    }
200
    offset1 = offset - pos;
201
    if (!s->must_flush &&
202
        offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
203
        /* can do the seek inside the buffer */
204
        s->buf_ptr = s->buffer + offset1;
205
    } else if ((s->is_streamed ||
206
               offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
207
               !s->write_flag && offset1 >= 0 &&
208
              (whence != SEEK_END || force)) {
209
        while(s->pos < offset && !s->eof_reached)
210
            fill_buffer(s);
211
        if (s->eof_reached)
212
            return AVERROR_EOF;
213
        s->buf_ptr = s->buf_end + offset - s->pos;
214
    } else {
215
        int64_t res;
216

    
217
#if CONFIG_MUXERS || CONFIG_NETWORK
218
        if (s->write_flag) {
219
            flush_buffer(s);
220
            s->must_flush = 1;
221
        }
222
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
223
        if (!s->seek)
224
            return AVERROR(EPIPE);
225
        if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
226
            return res;
227
        if (!s->write_flag)
228
            s->buf_end = s->buffer;
229
        s->buf_ptr = s->buffer;
230
        s->pos = offset;
231
    }
232
    s->eof_reached = 0;
233
    return offset;
234
}
235

    
236
#if FF_API_OLD_AVIO
237
int url_fskip(AVIOContext *s, int64_t offset)
238
{
239
    int64_t ret = avio_seek(s, offset, SEEK_CUR);
240
    return ret < 0 ? ret : 0;
241
}
242

    
243
int64_t url_ftell(AVIOContext *s)
244
{
245
    return avio_seek(s, 0, SEEK_CUR);
246
}
247
#endif
248

    
249
int64_t url_fsize(AVIOContext *s)
250
{
251
    int64_t size;
252

    
253
    if(!s)
254
        return AVERROR(EINVAL);
255

    
256
    if (!s->seek)
257
        return AVERROR(ENOSYS);
258
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
259
    if(size<0){
260
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
261
            return size;
262
        size++;
263
        s->seek(s->opaque, s->pos, SEEK_SET);
264
    }
265
    return size;
266
}
267

    
268
int url_feof(AVIOContext *s)
269
{
270
    if(!s)
271
        return 0;
272
    return s->eof_reached;
273
}
274

    
275
int url_ferror(AVIOContext *s)
276
{
277
    if(!s)
278
        return 0;
279
    return s->error;
280
}
281

    
282
void avio_wl32(AVIOContext *s, unsigned int val)
283
{
284
    avio_w8(s, val);
285
    avio_w8(s, val >> 8);
286
    avio_w8(s, val >> 16);
287
    avio_w8(s, val >> 24);
288
}
289

    
290
void avio_wb32(AVIOContext *s, unsigned int val)
291
{
292
    avio_w8(s, val >> 24);
293
    avio_w8(s, val >> 16);
294
    avio_w8(s, val >> 8);
295
    avio_w8(s, val);
296
}
297

    
298
#if FF_API_OLD_AVIO
299
void put_strz(AVIOContext *s, const char *str)
300
{
301
    avio_put_str(s, str);
302
}
303

    
304
#define GET(name, type) \
305
    type get_be ##name(AVIOContext *s) \
306
{\
307
    return avio_rb ##name(s);\
308
}\
309
    type get_le ##name(AVIOContext *s) \
310
{\
311
    return avio_rl ##name(s);\
312
}
313

    
314
GET(16, unsigned int)
315
GET(24, unsigned int)
316
GET(32, unsigned int)
317
GET(64, uint64_t)
318

    
319
#undef GET
320

    
321
#define PUT(name, type ) \
322
    void put_le ##name(AVIOContext *s, type val)\
323
{\
324
        avio_wl ##name(s, val);\
325
}\
326
    void put_be ##name(AVIOContext *s, type val)\
327
{\
328
        avio_wb ##name(s, val);\
329
}
330

    
331
PUT(16, unsigned int)
332
PUT(24, unsigned int)
333
PUT(32, unsigned int)
334
PUT(64, uint64_t)
335
#undef PUT
336

    
337
int get_byte(AVIOContext *s)
338
{
339
   return avio_r8(s);
340
}
341
int get_buffer(AVIOContext *s, unsigned char *buf, int size)
342
{
343
    return avio_read(s, buf, size);
344
}
345
int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size)
346
{
347
    return ffio_read_partial(s, buf, size);
348
}
349
void put_byte(AVIOContext *s, int val)
350
{
351
    avio_w8(s, val);
352
}
353
void put_buffer(AVIOContext *s, const unsigned char *buf, int size)
354
{
355
    avio_write(s, buf, size);
356
}
357
void put_nbyte(AVIOContext *s, int b, int count)
358
{
359
    ffio_fill(s, b, count);
360
}
361

    
362
int url_fopen(AVIOContext **s, const char *filename, int flags)
363
{
364
    return avio_open(s, filename, flags);
365
}
366
int url_fclose(AVIOContext *s)
367
{
368
    return avio_close(s);
369
}
370
int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
371
{
372
    return avio_seek(s, offset, whence);
373
}
374
#endif
375

    
376
int avio_put_str(AVIOContext *s, const char *str)
377
{
378
    int len = 1;
379
    if (str) {
380
        len += strlen(str);
381
        avio_write(s, (const unsigned char *) str, len);
382
    } else
383
        avio_w8(s, 0);
384
    return len;
385
}
386

    
387
int avio_put_str16le(AVIOContext *s, const char *str)
388
{
389
    const uint8_t *q = str;
390
    int ret = 0;
391

    
392
    while (*q) {
393
        uint32_t ch;
394
        uint16_t tmp;
395

    
396
        GET_UTF8(ch, *q++, break;)
397
        PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
398
    }
399
    avio_wl16(s, 0);
400
    ret += 2;
401
    return ret;
402
}
403

    
404
int ff_get_v_length(uint64_t val){
405
    int i=1;
406

    
407
    while(val>>=7)
408
        i++;
409

    
410
    return i;
411
}
412

    
413
void ff_put_v(AVIOContext *bc, uint64_t val){
414
    int i= ff_get_v_length(val);
415

    
416
    while(--i>0)
417
        avio_w8(bc, 128 | (val>>(7*i)));
418

    
419
    avio_w8(bc, val&127);
420
}
421

    
422
void avio_wl64(AVIOContext *s, uint64_t val)
423
{
424
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
425
    avio_wl32(s, (uint32_t)(val >> 32));
426
}
427

    
428
void avio_wb64(AVIOContext *s, uint64_t val)
429
{
430
    avio_wb32(s, (uint32_t)(val >> 32));
431
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
432
}
433

    
434
void avio_wl16(AVIOContext *s, unsigned int val)
435
{
436
    avio_w8(s, val);
437
    avio_w8(s, val >> 8);
438
}
439

    
440
void avio_wb16(AVIOContext *s, unsigned int val)
441
{
442
    avio_w8(s, val >> 8);
443
    avio_w8(s, val);
444
}
445

    
446
void avio_wl24(AVIOContext *s, unsigned int val)
447
{
448
    avio_wl16(s, val & 0xffff);
449
    avio_w8(s, val >> 16);
450
}
451

    
452
void avio_wb24(AVIOContext *s, unsigned int val)
453
{
454
    avio_wb16(s, val >> 8);
455
    avio_w8(s, val);
456
}
457

    
458
#if FF_API_OLD_AVIO
459
void put_tag(AVIOContext *s, const char *tag)
460
{
461
    while (*tag) {
462
        avio_w8(s, *tag++);
463
    }
464
}
465
#endif
466

    
467
/* Input stream */
468

    
469
static void fill_buffer(AVIOContext *s)
470
{
471
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
472
    int len= s->buffer_size - (dst - s->buffer);
473
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
474

    
475
    /* no need to do anything if EOF already reached */
476
    if (s->eof_reached)
477
        return;
478

    
479
    if(s->update_checksum && dst == s->buffer){
480
        if(s->buf_end > s->checksum_ptr)
481
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
482
        s->checksum_ptr= s->buffer;
483
    }
484

    
485
    /* make buffer smaller in case it ended up large after probing */
486
    if (s->buffer_size > max_buffer_size) {
487
        url_setbufsize(s, max_buffer_size);
488

    
489
        s->checksum_ptr = dst = s->buffer;
490
        len = s->buffer_size;
491
    }
492

    
493
    if(s->read_packet)
494
        len = s->read_packet(s->opaque, dst, len);
495
    else
496
        len = 0;
497
    if (len <= 0) {
498
        /* do not modify buffer if EOF reached so that a seek back can
499
           be done without rereading data */
500
        s->eof_reached = 1;
501
        if(len<0)
502
            s->error= len;
503
    } else {
504
        s->pos += len;
505
        s->buf_ptr = dst;
506
        s->buf_end = dst + len;
507
    }
508
}
509

    
510
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
511
                                    unsigned int len)
512
{
513
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
514
}
515

    
516
unsigned long get_checksum(AVIOContext *s)
517
{
518
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
519
    s->update_checksum= NULL;
520
    return s->checksum;
521
}
522

    
523
void init_checksum(AVIOContext *s,
524
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
525
                   unsigned long checksum)
526
{
527
    s->update_checksum= update_checksum;
528
    if(s->update_checksum){
529
        s->checksum= checksum;
530
        s->checksum_ptr= s->buf_ptr;
531
    }
532
}
533

    
534
/* XXX: put an inline version */
535
int avio_r8(AVIOContext *s)
536
{
537
    if (s->buf_ptr >= s->buf_end)
538
        fill_buffer(s);
539
    if (s->buf_ptr < s->buf_end)
540
        return *s->buf_ptr++;
541
    return 0;
542
}
543

    
544
#if FF_API_OLD_AVIO
545
int url_fgetc(AVIOContext *s)
546
{
547
    if (s->buf_ptr >= s->buf_end)
548
        fill_buffer(s);
549
    if (s->buf_ptr < s->buf_end)
550
        return *s->buf_ptr++;
551
    return URL_EOF;
552
}
553
#endif
554

    
555
int avio_read(AVIOContext *s, unsigned char *buf, int size)
556
{
557
    int len, size1;
558

    
559
    size1 = size;
560
    while (size > 0) {
561
        len = s->buf_end - s->buf_ptr;
562
        if (len > size)
563
            len = size;
564
        if (len == 0) {
565
            if(size > s->buffer_size && !s->update_checksum){
566
                if(s->read_packet)
567
                    len = s->read_packet(s->opaque, buf, size);
568
                if (len <= 0) {
569
                    /* do not modify buffer if EOF reached so that a seek back can
570
                    be done without rereading data */
571
                    s->eof_reached = 1;
572
                    if(len<0)
573
                        s->error= len;
574
                    break;
575
                } else {
576
                    s->pos += len;
577
                    size -= len;
578
                    buf += len;
579
                    s->buf_ptr = s->buffer;
580
                    s->buf_end = s->buffer/* + len*/;
581
                }
582
            }else{
583
                fill_buffer(s);
584
                len = s->buf_end - s->buf_ptr;
585
                if (len == 0)
586
                    break;
587
            }
588
        } else {
589
            memcpy(buf, s->buf_ptr, len);
590
            buf += len;
591
            s->buf_ptr += len;
592
            size -= len;
593
        }
594
    }
595
    if (size1 == size) {
596
        if (url_ferror(s)) return url_ferror(s);
597
        if (url_feof(s))   return AVERROR_EOF;
598
    }
599
    return size1 - size;
600
}
601

    
602
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
603
{
604
    int len;
605

    
606
    if(size<0)
607
        return -1;
608

    
609
    len = s->buf_end - s->buf_ptr;
610
    if (len == 0) {
611
        fill_buffer(s);
612
        len = s->buf_end - s->buf_ptr;
613
    }
614
    if (len > size)
615
        len = size;
616
    memcpy(buf, s->buf_ptr, len);
617
    s->buf_ptr += len;
618
    if (!len) {
619
        if (url_ferror(s)) return url_ferror(s);
620
        if (url_feof(s))   return AVERROR_EOF;
621
    }
622
    return len;
623
}
624

    
625
unsigned int avio_rl16(AVIOContext *s)
626
{
627
    unsigned int val;
628
    val = avio_r8(s);
629
    val |= avio_r8(s) << 8;
630
    return val;
631
}
632

    
633
unsigned int avio_rl24(AVIOContext *s)
634
{
635
    unsigned int val;
636
    val = avio_rl16(s);
637
    val |= avio_r8(s) << 16;
638
    return val;
639
}
640

    
641
unsigned int avio_rl32(AVIOContext *s)
642
{
643
    unsigned int val;
644
    val = avio_rl16(s);
645
    val |= avio_rl16(s) << 16;
646
    return val;
647
}
648

    
649
uint64_t avio_rl64(AVIOContext *s)
650
{
651
    uint64_t val;
652
    val = (uint64_t)avio_rl32(s);
653
    val |= (uint64_t)avio_rl32(s) << 32;
654
    return val;
655
}
656

    
657
unsigned int avio_rb16(AVIOContext *s)
658
{
659
    unsigned int val;
660
    val = avio_r8(s) << 8;
661
    val |= avio_r8(s);
662
    return val;
663
}
664

    
665
unsigned int avio_rb24(AVIOContext *s)
666
{
667
    unsigned int val;
668
    val = avio_rb16(s) << 8;
669
    val |= avio_r8(s);
670
    return val;
671
}
672
unsigned int avio_rb32(AVIOContext *s)
673
{
674
    unsigned int val;
675
    val = avio_rb16(s) << 16;
676
    val |= avio_rb16(s);
677
    return val;
678
}
679

    
680
#if FF_API_OLD_AVIO
681
char *get_strz(AVIOContext *s, char *buf, int maxlen)
682
{
683
    avio_get_str(s, INT_MAX, buf, maxlen);
684
    return buf;
685
}
686
#endif
687

    
688
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
689
{
690
    int i = 0;
691
    char c;
692

    
693
    do {
694
        c = avio_r8(s);
695
        if (c && i < maxlen-1)
696
            buf[i++] = c;
697
    } while (c != '\n' && c);
698

    
699
    buf[i] = 0;
700
    return i;
701
}
702

    
703
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
704
{
705
    int i;
706

    
707
    // reserve 1 byte for terminating 0
708
    buflen = FFMIN(buflen - 1, maxlen);
709
    for (i = 0; i < buflen; i++)
710
        if (!(buf[i] = avio_r8(s)))
711
            return i + 1;
712
    if (buflen)
713
        buf[i] = 0;
714
    for (; i < maxlen; i++)
715
        if (!avio_r8(s))
716
            return i + 1;
717
    return maxlen;
718
}
719

    
720
#define GET_STR16(type, read) \
721
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
722
{\
723
    char* q = buf;\
724
    int ret = 0;\
725
    while (ret + 1 < maxlen) {\
726
        uint8_t tmp;\
727
        uint32_t ch;\
728
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
729
        if (!ch)\
730
            break;\
731
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
732
    }\
733
    *q = 0;\
734
    return ret;\
735
}\
736

    
737
GET_STR16(le, avio_rl16)
738
GET_STR16(be, avio_rb16)
739

    
740
#undef GET_STR16
741

    
742
uint64_t avio_rb64(AVIOContext *s)
743
{
744
    uint64_t val;
745
    val = (uint64_t)avio_rb32(s) << 32;
746
    val |= (uint64_t)avio_rb32(s);
747
    return val;
748
}
749

    
750
uint64_t ff_get_v(AVIOContext *bc){
751
    uint64_t val = 0;
752
    int tmp;
753

    
754
    do{
755
        tmp = avio_r8(bc);
756
        val= (val<<7) + (tmp&127);
757
    }while(tmp&128);
758
    return val;
759
}
760

    
761
int url_fdopen(AVIOContext **s, URLContext *h)
762
{
763
    uint8_t *buffer;
764
    int buffer_size, max_packet_size;
765

    
766
    max_packet_size = url_get_max_packet_size(h);
767
    if (max_packet_size) {
768
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
769
    } else {
770
        buffer_size = IO_BUFFER_SIZE;
771
    }
772
    buffer = av_malloc(buffer_size);
773
    if (!buffer)
774
        return AVERROR(ENOMEM);
775

    
776
    *s = av_mallocz(sizeof(AVIOContext));
777
    if(!*s) {
778
        av_free(buffer);
779
        return AVERROR(ENOMEM);
780
    }
781

    
782
    if (ffio_init_context(*s, buffer, buffer_size,
783
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
784
                      url_read, url_write, url_seek) < 0) {
785
        av_free(buffer);
786
        av_freep(s);
787
        return AVERROR(EIO);
788
    }
789
    (*s)->is_streamed = h->is_streamed;
790
    (*s)->max_packet_size = max_packet_size;
791
    if(h->prot) {
792
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
793
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
794
    }
795
    return 0;
796
}
797

    
798
int url_setbufsize(AVIOContext *s, int buf_size)
799
{
800
    uint8_t *buffer;
801
    buffer = av_malloc(buf_size);
802
    if (!buffer)
803
        return AVERROR(ENOMEM);
804

    
805
    av_free(s->buffer);
806
    s->buffer = buffer;
807
    s->buffer_size = buf_size;
808
    s->buf_ptr = buffer;
809
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
810
    return 0;
811
}
812

    
813
#if FF_API_URL_RESETBUF
814
int url_resetbuf(AVIOContext *s, int flags)
815
#else
816
static int url_resetbuf(AVIOContext *s, int flags)
817
#endif
818
{
819
#if FF_API_URL_RESETBUF
820
    if (flags & URL_RDWR)
821
        return AVERROR(EINVAL);
822
#else
823
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
824
#endif
825

    
826
    if (flags & URL_WRONLY) {
827
        s->buf_end = s->buffer + s->buffer_size;
828
        s->write_flag = 1;
829
    } else {
830
        s->buf_end = s->buffer;
831
        s->write_flag = 0;
832
    }
833
    return 0;
834
}
835

    
836
int ff_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
837
{
838
    int64_t buffer_start;
839
    int buffer_size;
840
    int overlap, new_size, alloc_size;
841

    
842
    if (s->write_flag)
843
        return AVERROR(EINVAL);
844

    
845
    buffer_size = s->buf_end - s->buffer;
846

    
847
    /* the buffers must touch or overlap */
848
    if ((buffer_start = s->pos - buffer_size) > buf_size)
849
        return AVERROR(EINVAL);
850

    
851
    overlap = buf_size - buffer_start;
852
    new_size = buf_size + buffer_size - overlap;
853

    
854
    alloc_size = FFMAX(s->buffer_size, new_size);
855
    if (alloc_size > buf_size)
856
        if (!(buf = av_realloc(buf, alloc_size)))
857
            return AVERROR(ENOMEM);
858

    
859
    if (new_size > buf_size) {
860
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
861
        buf_size = new_size;
862
    }
863

    
864
    av_free(s->buffer);
865
    s->buf_ptr = s->buffer = buf;
866
    s->buffer_size = alloc_size;
867
    s->pos = buf_size;
868
    s->buf_end = s->buf_ptr + buf_size;
869
    s->eof_reached = 0;
870
    s->must_flush = 0;
871

    
872
    return 0;
873
}
874

    
875
int avio_open(AVIOContext **s, const char *filename, int flags)
876
{
877
    URLContext *h;
878
    int err;
879

    
880
    err = url_open(&h, filename, flags);
881
    if (err < 0)
882
        return err;
883
    err = url_fdopen(s, h);
884
    if (err < 0) {
885
        url_close(h);
886
        return err;
887
    }
888
    return 0;
889
}
890

    
891
int avio_close(AVIOContext *s)
892
{
893
    URLContext *h = s->opaque;
894

    
895
    av_free(s->buffer);
896
    av_free(s);
897
    return url_close(h);
898
}
899

    
900
URLContext *url_fileno(AVIOContext *s)
901
{
902
    return s->opaque;
903
}
904

    
905
#if CONFIG_MUXERS
906
int url_fprintf(AVIOContext *s, const char *fmt, ...)
907
{
908
    va_list ap;
909
    char buf[4096];
910
    int ret;
911

    
912
    va_start(ap, fmt);
913
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
914
    va_end(ap);
915
    avio_write(s, buf, strlen(buf));
916
    return ret;
917
}
918
#endif //CONFIG_MUXERS
919

    
920
#if FF_API_OLD_AVIO
921
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
922
{
923
    int c;
924
    char *q;
925

    
926
    c = avio_r8(s);
927
    if (url_feof(s))
928
        return NULL;
929
    q = buf;
930
    for(;;) {
931
        if (url_feof(s) || c == '\n')
932
            break;
933
        if ((q - buf) < buf_size - 1)
934
            *q++ = c;
935
        c = avio_r8(s);
936
    }
937
    if (buf_size > 0)
938
        *q = '\0';
939
    return buf;
940
}
941
#endif
942

    
943
int url_fget_max_packet_size(AVIOContext *s)
944
{
945
    return s->max_packet_size;
946
}
947

    
948
int av_url_read_fpause(AVIOContext *s, int pause)
949
{
950
    if (!s->read_pause)
951
        return AVERROR(ENOSYS);
952
    return s->read_pause(s->opaque, pause);
953
}
954

    
955
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
956
                          int64_t timestamp, int flags)
957
{
958
    URLContext *h = s->opaque;
959
    int64_t ret;
960
    if (!s->read_seek)
961
        return AVERROR(ENOSYS);
962
    ret = s->read_seek(h, stream_index, timestamp, flags);
963
    if(ret >= 0) {
964
        int64_t pos;
965
        s->buf_ptr = s->buf_end; // Flush buffer
966
        pos = s->seek(h, 0, SEEK_CUR);
967
        if (pos >= 0)
968
            s->pos = pos;
969
        else if (pos != AVERROR(ENOSYS))
970
            ret = pos;
971
    }
972
    return ret;
973
}
974

    
975
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
976
 * back to the server even if CONFIG_MUXERS is false. */
977
#if CONFIG_MUXERS || CONFIG_NETWORK
978
/* buffer handling */
979
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
980
{
981
    int ret;
982
    *s = av_mallocz(sizeof(AVIOContext));
983
    if(!*s)
984
        return AVERROR(ENOMEM);
985
    ret = ffio_init_context(*s, buf, buf_size,
986
                        (flags & URL_WRONLY || flags & URL_RDWR),
987
                        NULL, NULL, NULL, NULL);
988
    if(ret != 0)
989
        av_freep(s);
990
    return ret;
991
}
992

    
993
int url_close_buf(AVIOContext *s)
994
{
995
    put_flush_packet(s);
996
    return s->buf_ptr - s->buffer;
997
}
998

    
999
/* output in a dynamic buffer */
1000

    
1001
typedef struct DynBuffer {
1002
    int pos, size, allocated_size;
1003
    uint8_t *buffer;
1004
    int io_buffer_size;
1005
    uint8_t io_buffer[1];
1006
} DynBuffer;
1007

    
1008
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1009
{
1010
    DynBuffer *d = opaque;
1011
    unsigned new_size, new_allocated_size;
1012

    
1013
    /* reallocate buffer if needed */
1014
    new_size = d->pos + buf_size;
1015
    new_allocated_size = d->allocated_size;
1016
    if(new_size < d->pos || new_size > INT_MAX/2)
1017
        return -1;
1018
    while (new_size > new_allocated_size) {
1019
        if (!new_allocated_size)
1020
            new_allocated_size = new_size;
1021
        else
1022
            new_allocated_size += new_allocated_size / 2 + 1;
1023
    }
1024

    
1025
    if (new_allocated_size > d->allocated_size) {
1026
        d->buffer = av_realloc(d->buffer, new_allocated_size);
1027
        if(d->buffer == NULL)
1028
             return AVERROR(ENOMEM);
1029
        d->allocated_size = new_allocated_size;
1030
    }
1031
    memcpy(d->buffer + d->pos, buf, buf_size);
1032
    d->pos = new_size;
1033
    if (d->pos > d->size)
1034
        d->size = d->pos;
1035
    return buf_size;
1036
}
1037

    
1038
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1039
{
1040
    unsigned char buf1[4];
1041
    int ret;
1042

    
1043
    /* packetized write: output the header */
1044
    AV_WB32(buf1, buf_size);
1045
    ret= dyn_buf_write(opaque, buf1, 4);
1046
    if(ret < 0)
1047
        return ret;
1048

    
1049
    /* then the data */
1050
    return dyn_buf_write(opaque, buf, buf_size);
1051
}
1052

    
1053
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1054
{
1055
    DynBuffer *d = opaque;
1056

    
1057
    if (whence == SEEK_CUR)
1058
        offset += d->pos;
1059
    else if (whence == SEEK_END)
1060
        offset += d->size;
1061
    if (offset < 0 || offset > 0x7fffffffLL)
1062
        return -1;
1063
    d->pos = offset;
1064
    return 0;
1065
}
1066

    
1067
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1068
{
1069
    DynBuffer *d;
1070
    int ret;
1071
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1072

    
1073
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1074
        return -1;
1075
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1076
    if (!d)
1077
        return AVERROR(ENOMEM);
1078
    *s = av_mallocz(sizeof(AVIOContext));
1079
    if(!*s) {
1080
        av_free(d);
1081
        return AVERROR(ENOMEM);
1082
    }
1083
    d->io_buffer_size = io_buffer_size;
1084
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
1085
                        1, d, NULL,
1086
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1087
                        max_packet_size ? NULL : dyn_buf_seek);
1088
    if (ret == 0) {
1089
        (*s)->max_packet_size = max_packet_size;
1090
    } else {
1091
        av_free(d);
1092
        av_freep(s);
1093
    }
1094
    return ret;
1095
}
1096

    
1097
int url_open_dyn_buf(AVIOContext **s)
1098
{
1099
    return url_open_dyn_buf_internal(s, 0);
1100
}
1101

    
1102
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1103
{
1104
    if (max_packet_size <= 0)
1105
        return -1;
1106
    return url_open_dyn_buf_internal(s, max_packet_size);
1107
}
1108

    
1109
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1110
{
1111
    DynBuffer *d = s->opaque;
1112
    int size;
1113
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1114
    int padding = 0;
1115

    
1116
    /* don't attempt to pad fixed-size packet buffers */
1117
    if (!s->max_packet_size) {
1118
        avio_write(s, padbuf, sizeof(padbuf));
1119
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1120
    }
1121

    
1122
    put_flush_packet(s);
1123

    
1124
    *pbuffer = d->buffer;
1125
    size = d->size;
1126
    av_free(d);
1127
    av_free(s);
1128
    return size - padding;
1129
}
1130
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */