Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 75a8e0f0

History | View | Annotate | Download (27 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
int64_t avio_skip(AVIOContext *s, int64_t offset)
237
{
238
    return avio_seek(s, offset, SEEK_CUR);
239
}
240

    
241
#if FF_API_OLD_AVIO
242
int url_fskip(AVIOContext *s, int64_t offset)
243
{
244
    int64_t ret = avio_seek(s, offset, SEEK_CUR);
245
    return ret < 0 ? ret : 0;
246
}
247

    
248
int64_t url_ftell(AVIOContext *s)
249
{
250
    return avio_seek(s, 0, SEEK_CUR);
251
}
252
#endif
253

    
254
int64_t avio_size(AVIOContext *s)
255
{
256
    int64_t size;
257

    
258
    if(!s)
259
        return AVERROR(EINVAL);
260

    
261
    if (!s->seek)
262
        return AVERROR(ENOSYS);
263
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
264
    if(size<0){
265
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
266
            return size;
267
        size++;
268
        s->seek(s->opaque, s->pos, SEEK_SET);
269
    }
270
    return size;
271
}
272

    
273
int url_feof(AVIOContext *s)
274
{
275
    if(!s)
276
        return 0;
277
    if(s->eof_reached){
278
        s->eof_reached=0;
279
        fill_buffer(s);
280
    }
281
    return s->eof_reached;
282
}
283

    
284
int url_ferror(AVIOContext *s)
285
{
286
    if(!s)
287
        return 0;
288
    return s->error;
289
}
290

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

    
299
void avio_wb32(AVIOContext *s, unsigned int val)
300
{
301
    avio_w8(s, val >> 24);
302
    avio_w8(s, val >> 16);
303
    avio_w8(s, val >> 8);
304
    avio_w8(s, val);
305
}
306

    
307
#if FF_API_OLD_AVIO
308
void put_strz(AVIOContext *s, const char *str)
309
{
310
    avio_put_str(s, str);
311
}
312

    
313
#define GET(name, type) \
314
    type get_be ##name(AVIOContext *s) \
315
{\
316
    return avio_rb ##name(s);\
317
}\
318
    type get_le ##name(AVIOContext *s) \
319
{\
320
    return avio_rl ##name(s);\
321
}
322

    
323
GET(16, unsigned int)
324
GET(24, unsigned int)
325
GET(32, unsigned int)
326
GET(64, uint64_t)
327

    
328
#undef GET
329

    
330
#define PUT(name, type ) \
331
    void put_le ##name(AVIOContext *s, type val)\
332
{\
333
        avio_wl ##name(s, val);\
334
}\
335
    void put_be ##name(AVIOContext *s, type val)\
336
{\
337
        avio_wb ##name(s, val);\
338
}
339

    
340
PUT(16, unsigned int)
341
PUT(24, unsigned int)
342
PUT(32, unsigned int)
343
PUT(64, uint64_t)
344
#undef PUT
345

    
346
int get_byte(AVIOContext *s)
347
{
348
   return avio_r8(s);
349
}
350
int get_buffer(AVIOContext *s, unsigned char *buf, int size)
351
{
352
    return avio_read(s, buf, size);
353
}
354
int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size)
355
{
356
    return ffio_read_partial(s, buf, size);
357
}
358
void put_byte(AVIOContext *s, int val)
359
{
360
    avio_w8(s, val);
361
}
362
void put_buffer(AVIOContext *s, const unsigned char *buf, int size)
363
{
364
    avio_write(s, buf, size);
365
}
366
void put_nbyte(AVIOContext *s, int b, int count)
367
{
368
    ffio_fill(s, b, count);
369
}
370

    
371
int url_fopen(AVIOContext **s, const char *filename, int flags)
372
{
373
    return avio_open(s, filename, flags);
374
}
375
int url_fclose(AVIOContext *s)
376
{
377
    return avio_close(s);
378
}
379
int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
380
{
381
    return avio_seek(s, offset, whence);
382
}
383
int64_t url_fsize(AVIOContext *s)
384
{
385
    return avio_size(s);
386
}
387
#endif
388

    
389
int avio_put_str(AVIOContext *s, const char *str)
390
{
391
    int len = 1;
392
    if (str) {
393
        len += strlen(str);
394
        avio_write(s, (const unsigned char *) str, len);
395
    } else
396
        avio_w8(s, 0);
397
    return len;
398
}
399

    
400
int avio_put_str16le(AVIOContext *s, const char *str)
401
{
402
    const uint8_t *q = str;
403
    int ret = 0;
404

    
405
    while (*q) {
406
        uint32_t ch;
407
        uint16_t tmp;
408

    
409
        GET_UTF8(ch, *q++, break;)
410
        PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
411
    }
412
    avio_wl16(s, 0);
413
    ret += 2;
414
    return ret;
415
}
416

    
417
int ff_get_v_length(uint64_t val){
418
    int i=1;
419

    
420
    while(val>>=7)
421
        i++;
422

    
423
    return i;
424
}
425

    
426
void ff_put_v(AVIOContext *bc, uint64_t val){
427
    int i= ff_get_v_length(val);
428

    
429
    while(--i>0)
430
        avio_w8(bc, 128 | (val>>(7*i)));
431

    
432
    avio_w8(bc, val&127);
433
}
434

    
435
void avio_wl64(AVIOContext *s, uint64_t val)
436
{
437
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
438
    avio_wl32(s, (uint32_t)(val >> 32));
439
}
440

    
441
void avio_wb64(AVIOContext *s, uint64_t val)
442
{
443
    avio_wb32(s, (uint32_t)(val >> 32));
444
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
445
}
446

    
447
void avio_wl16(AVIOContext *s, unsigned int val)
448
{
449
    avio_w8(s, val);
450
    avio_w8(s, val >> 8);
451
}
452

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

    
459
void avio_wl24(AVIOContext *s, unsigned int val)
460
{
461
    avio_wl16(s, val & 0xffff);
462
    avio_w8(s, val >> 16);
463
}
464

    
465
void avio_wb24(AVIOContext *s, unsigned int val)
466
{
467
    avio_wb16(s, val >> 8);
468
    avio_w8(s, val);
469
}
470

    
471
#if FF_API_OLD_AVIO
472
void put_tag(AVIOContext *s, const char *tag)
473
{
474
    while (*tag) {
475
        avio_w8(s, *tag++);
476
    }
477
}
478
#endif
479

    
480
/* Input stream */
481

    
482
static void fill_buffer(AVIOContext *s)
483
{
484
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
485
    int len= s->buffer_size - (dst - s->buffer);
486
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
487

    
488
    /* no need to do anything if EOF already reached */
489
    if (s->eof_reached)
490
        return;
491

    
492
    if(s->update_checksum && dst == s->buffer){
493
        if(s->buf_end > s->checksum_ptr)
494
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
495
        s->checksum_ptr= s->buffer;
496
    }
497

    
498
    /* make buffer smaller in case it ended up large after probing */
499
    if (s->buffer_size > max_buffer_size) {
500
        url_setbufsize(s, max_buffer_size);
501

    
502
        s->checksum_ptr = dst = s->buffer;
503
        len = s->buffer_size;
504
    }
505

    
506
    if(s->read_packet)
507
        len = s->read_packet(s->opaque, dst, len);
508
    else
509
        len = 0;
510
    if (len <= 0) {
511
        /* do not modify buffer if EOF reached so that a seek back can
512
           be done without rereading data */
513
        s->eof_reached = 1;
514
        if(len<0)
515
            s->error= len;
516
    } else {
517
        s->pos += len;
518
        s->buf_ptr = dst;
519
        s->buf_end = dst + len;
520
    }
521
}
522

    
523
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
524
                                    unsigned int len)
525
{
526
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
527
}
528

    
529
unsigned long get_checksum(AVIOContext *s)
530
{
531
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
532
    s->update_checksum= NULL;
533
    return s->checksum;
534
}
535

    
536
void init_checksum(AVIOContext *s,
537
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
538
                   unsigned long checksum)
539
{
540
    s->update_checksum= update_checksum;
541
    if(s->update_checksum){
542
        s->checksum= checksum;
543
        s->checksum_ptr= s->buf_ptr;
544
    }
545
}
546

    
547
/* XXX: put an inline version */
548
int avio_r8(AVIOContext *s)
549
{
550
    if (s->buf_ptr >= s->buf_end)
551
        fill_buffer(s);
552
    if (s->buf_ptr < s->buf_end)
553
        return *s->buf_ptr++;
554
    return 0;
555
}
556

    
557
#if FF_API_OLD_AVIO
558
int url_fgetc(AVIOContext *s)
559
{
560
    if (s->buf_ptr >= s->buf_end)
561
        fill_buffer(s);
562
    if (s->buf_ptr < s->buf_end)
563
        return *s->buf_ptr++;
564
    return URL_EOF;
565
}
566
#endif
567

    
568
int avio_read(AVIOContext *s, unsigned char *buf, int size)
569
{
570
    int len, size1;
571

    
572
    size1 = size;
573
    while (size > 0) {
574
        len = s->buf_end - s->buf_ptr;
575
        if (len > size)
576
            len = size;
577
        if (len == 0) {
578
            if(size > s->buffer_size && !s->update_checksum){
579
                if(s->read_packet)
580
                    len = s->read_packet(s->opaque, buf, size);
581
                if (len <= 0) {
582
                    /* do not modify buffer if EOF reached so that a seek back can
583
                    be done without rereading data */
584
                    s->eof_reached = 1;
585
                    if(len<0)
586
                        s->error= len;
587
                    break;
588
                } else {
589
                    s->pos += len;
590
                    size -= len;
591
                    buf += len;
592
                    s->buf_ptr = s->buffer;
593
                    s->buf_end = s->buffer/* + len*/;
594
                }
595
            }else{
596
                fill_buffer(s);
597
                len = s->buf_end - s->buf_ptr;
598
                if (len == 0)
599
                    break;
600
            }
601
        } else {
602
            memcpy(buf, s->buf_ptr, len);
603
            buf += len;
604
            s->buf_ptr += len;
605
            size -= len;
606
        }
607
    }
608
    if (size1 == size) {
609
        if (url_ferror(s)) return url_ferror(s);
610
        if (url_feof(s))   return AVERROR_EOF;
611
    }
612
    return size1 - size;
613
}
614

    
615
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
616
{
617
    int len;
618

    
619
    if(size<0)
620
        return -1;
621

    
622
    len = s->buf_end - s->buf_ptr;
623
    if (len == 0) {
624
        fill_buffer(s);
625
        len = s->buf_end - s->buf_ptr;
626
    }
627
    if (len > size)
628
        len = size;
629
    memcpy(buf, s->buf_ptr, len);
630
    s->buf_ptr += len;
631
    if (!len) {
632
        if (url_ferror(s)) return url_ferror(s);
633
        if (url_feof(s))   return AVERROR_EOF;
634
    }
635
    return len;
636
}
637

    
638
unsigned int avio_rl16(AVIOContext *s)
639
{
640
    unsigned int val;
641
    val = avio_r8(s);
642
    val |= avio_r8(s) << 8;
643
    return val;
644
}
645

    
646
unsigned int avio_rl24(AVIOContext *s)
647
{
648
    unsigned int val;
649
    val = avio_rl16(s);
650
    val |= avio_r8(s) << 16;
651
    return val;
652
}
653

    
654
unsigned int avio_rl32(AVIOContext *s)
655
{
656
    unsigned int val;
657
    val = avio_rl16(s);
658
    val |= avio_rl16(s) << 16;
659
    return val;
660
}
661

    
662
uint64_t avio_rl64(AVIOContext *s)
663
{
664
    uint64_t val;
665
    val = (uint64_t)avio_rl32(s);
666
    val |= (uint64_t)avio_rl32(s) << 32;
667
    return val;
668
}
669

    
670
unsigned int avio_rb16(AVIOContext *s)
671
{
672
    unsigned int val;
673
    val = avio_r8(s) << 8;
674
    val |= avio_r8(s);
675
    return val;
676
}
677

    
678
unsigned int avio_rb24(AVIOContext *s)
679
{
680
    unsigned int val;
681
    val = avio_rb16(s) << 8;
682
    val |= avio_r8(s);
683
    return val;
684
}
685
unsigned int avio_rb32(AVIOContext *s)
686
{
687
    unsigned int val;
688
    val = avio_rb16(s) << 16;
689
    val |= avio_rb16(s);
690
    return val;
691
}
692

    
693
#if FF_API_OLD_AVIO
694
char *get_strz(AVIOContext *s, char *buf, int maxlen)
695
{
696
    avio_get_str(s, INT_MAX, buf, maxlen);
697
    return buf;
698
}
699
#endif
700

    
701
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
702
{
703
    int i = 0;
704
    char c;
705

    
706
    do {
707
        c = avio_r8(s);
708
        if (c && i < maxlen-1)
709
            buf[i++] = c;
710
    } while (c != '\n' && c);
711

    
712
    buf[i] = 0;
713
    return i;
714
}
715

    
716
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
717
{
718
    int i;
719

    
720
    // reserve 1 byte for terminating 0
721
    buflen = FFMIN(buflen - 1, maxlen);
722
    for (i = 0; i < buflen; i++)
723
        if (!(buf[i] = avio_r8(s)))
724
            return i + 1;
725
    if (buflen)
726
        buf[i] = 0;
727
    for (; i < maxlen; i++)
728
        if (!avio_r8(s))
729
            return i + 1;
730
    return maxlen;
731
}
732

    
733
#define GET_STR16(type, read) \
734
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
735
{\
736
    char* q = buf;\
737
    int ret = 0;\
738
    while (ret + 1 < maxlen) {\
739
        uint8_t tmp;\
740
        uint32_t ch;\
741
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
742
        if (!ch)\
743
            break;\
744
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
745
    }\
746
    *q = 0;\
747
    return ret;\
748
}\
749

    
750
GET_STR16(le, avio_rl16)
751
GET_STR16(be, avio_rb16)
752

    
753
#undef GET_STR16
754

    
755
uint64_t avio_rb64(AVIOContext *s)
756
{
757
    uint64_t val;
758
    val = (uint64_t)avio_rb32(s) << 32;
759
    val |= (uint64_t)avio_rb32(s);
760
    return val;
761
}
762

    
763
uint64_t ff_get_v(AVIOContext *bc){
764
    uint64_t val = 0;
765
    int tmp;
766

    
767
    do{
768
        tmp = avio_r8(bc);
769
        val= (val<<7) + (tmp&127);
770
    }while(tmp&128);
771
    return val;
772
}
773

    
774
int url_fdopen(AVIOContext **s, URLContext *h)
775
{
776
    uint8_t *buffer;
777
    int buffer_size, max_packet_size;
778

    
779
    max_packet_size = url_get_max_packet_size(h);
780
    if (max_packet_size) {
781
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
782
    } else {
783
        buffer_size = IO_BUFFER_SIZE;
784
    }
785
    buffer = av_malloc(buffer_size);
786
    if (!buffer)
787
        return AVERROR(ENOMEM);
788

    
789
    *s = av_mallocz(sizeof(AVIOContext));
790
    if(!*s) {
791
        av_free(buffer);
792
        return AVERROR(ENOMEM);
793
    }
794

    
795
    if (ffio_init_context(*s, buffer, buffer_size,
796
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
797
                      url_read, url_write, url_seek) < 0) {
798
        av_free(buffer);
799
        av_freep(s);
800
        return AVERROR(EIO);
801
    }
802
    (*s)->is_streamed = h->is_streamed;
803
    (*s)->max_packet_size = max_packet_size;
804
    if(h->prot) {
805
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
806
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
807
    }
808
    return 0;
809
}
810

    
811
int url_setbufsize(AVIOContext *s, int buf_size)
812
{
813
    uint8_t *buffer;
814
    buffer = av_malloc(buf_size);
815
    if (!buffer)
816
        return AVERROR(ENOMEM);
817

    
818
    av_free(s->buffer);
819
    s->buffer = buffer;
820
    s->buffer_size = buf_size;
821
    s->buf_ptr = buffer;
822
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
823
    return 0;
824
}
825

    
826
#if FF_API_URL_RESETBUF
827
int url_resetbuf(AVIOContext *s, int flags)
828
#else
829
static int url_resetbuf(AVIOContext *s, int flags)
830
#endif
831
{
832
#if FF_API_URL_RESETBUF
833
    if (flags & URL_RDWR)
834
        return AVERROR(EINVAL);
835
#else
836
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
837
#endif
838

    
839
    if (flags & URL_WRONLY) {
840
        s->buf_end = s->buffer + s->buffer_size;
841
        s->write_flag = 1;
842
    } else {
843
        s->buf_end = s->buffer;
844
        s->write_flag = 0;
845
    }
846
    return 0;
847
}
848

    
849
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
850
{
851
    int64_t buffer_start;
852
    int buffer_size;
853
    int overlap, new_size, alloc_size;
854

    
855
    if (s->write_flag)
856
        return AVERROR(EINVAL);
857

    
858
    buffer_size = s->buf_end - s->buffer;
859

    
860
    /* the buffers must touch or overlap */
861
    if ((buffer_start = s->pos - buffer_size) > buf_size)
862
        return AVERROR(EINVAL);
863

    
864
    overlap = buf_size - buffer_start;
865
    new_size = buf_size + buffer_size - overlap;
866

    
867
    alloc_size = FFMAX(s->buffer_size, new_size);
868
    if (alloc_size > buf_size)
869
        if (!(buf = av_realloc(buf, alloc_size)))
870
            return AVERROR(ENOMEM);
871

    
872
    if (new_size > buf_size) {
873
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
874
        buf_size = new_size;
875
    }
876

    
877
    av_free(s->buffer);
878
    s->buf_ptr = s->buffer = buf;
879
    s->buffer_size = alloc_size;
880
    s->pos = buf_size;
881
    s->buf_end = s->buf_ptr + buf_size;
882
    s->eof_reached = 0;
883
    s->must_flush = 0;
884

    
885
    return 0;
886
}
887

    
888
int avio_open(AVIOContext **s, const char *filename, int flags)
889
{
890
    URLContext *h;
891
    int err;
892

    
893
    err = url_open(&h, filename, flags);
894
    if (err < 0)
895
        return err;
896
    err = url_fdopen(s, h);
897
    if (err < 0) {
898
        url_close(h);
899
        return err;
900
    }
901
    return 0;
902
}
903

    
904
int avio_close(AVIOContext *s)
905
{
906
    URLContext *h = s->opaque;
907

    
908
    av_free(s->buffer);
909
    av_free(s);
910
    return url_close(h);
911
}
912

    
913
URLContext *url_fileno(AVIOContext *s)
914
{
915
    return s->opaque;
916
}
917

    
918
#if CONFIG_MUXERS
919
int url_fprintf(AVIOContext *s, const char *fmt, ...)
920
{
921
    va_list ap;
922
    char buf[4096];
923
    int ret;
924

    
925
    va_start(ap, fmt);
926
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
927
    va_end(ap);
928
    avio_write(s, buf, strlen(buf));
929
    return ret;
930
}
931
#endif //CONFIG_MUXERS
932

    
933
#if FF_API_OLD_AVIO
934
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
935
{
936
    int c;
937
    char *q;
938

    
939
    c = avio_r8(s);
940
    if (url_feof(s))
941
        return NULL;
942
    q = buf;
943
    for(;;) {
944
        if (url_feof(s) || c == '\n')
945
            break;
946
        if ((q - buf) < buf_size - 1)
947
            *q++ = c;
948
        c = avio_r8(s);
949
    }
950
    if (buf_size > 0)
951
        *q = '\0';
952
    return buf;
953
}
954

    
955
int url_fget_max_packet_size(AVIOContext *s)
956
{
957
    return s->max_packet_size;
958
}
959
#endif
960

    
961
int av_url_read_fpause(AVIOContext *s, int pause)
962
{
963
    if (!s->read_pause)
964
        return AVERROR(ENOSYS);
965
    return s->read_pause(s->opaque, pause);
966
}
967

    
968
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
969
                          int64_t timestamp, int flags)
970
{
971
    URLContext *h = s->opaque;
972
    int64_t ret;
973
    if (!s->read_seek)
974
        return AVERROR(ENOSYS);
975
    ret = s->read_seek(h, stream_index, timestamp, flags);
976
    if(ret >= 0) {
977
        int64_t pos;
978
        s->buf_ptr = s->buf_end; // Flush buffer
979
        pos = s->seek(h, 0, SEEK_CUR);
980
        if (pos >= 0)
981
            s->pos = pos;
982
        else if (pos != AVERROR(ENOSYS))
983
            ret = pos;
984
    }
985
    return ret;
986
}
987

    
988
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
989
 * back to the server even if CONFIG_MUXERS is false. */
990
#if CONFIG_MUXERS || CONFIG_NETWORK
991
/* buffer handling */
992
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
993
{
994
    int ret;
995
    *s = av_mallocz(sizeof(AVIOContext));
996
    if(!*s)
997
        return AVERROR(ENOMEM);
998
    ret = ffio_init_context(*s, buf, buf_size,
999
                        (flags & URL_WRONLY || flags & URL_RDWR),
1000
                        NULL, NULL, NULL, NULL);
1001
    if(ret != 0)
1002
        av_freep(s);
1003
    return ret;
1004
}
1005

    
1006
int url_close_buf(AVIOContext *s)
1007
{
1008
    put_flush_packet(s);
1009
    return s->buf_ptr - s->buffer;
1010
}
1011

    
1012
/* output in a dynamic buffer */
1013

    
1014
typedef struct DynBuffer {
1015
    int pos, size, allocated_size;
1016
    uint8_t *buffer;
1017
    int io_buffer_size;
1018
    uint8_t io_buffer[1];
1019
} DynBuffer;
1020

    
1021
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1022
{
1023
    DynBuffer *d = opaque;
1024
    unsigned new_size, new_allocated_size;
1025

    
1026
    /* reallocate buffer if needed */
1027
    new_size = d->pos + buf_size;
1028
    new_allocated_size = d->allocated_size;
1029
    if(new_size < d->pos || new_size > INT_MAX/2)
1030
        return -1;
1031
    while (new_size > new_allocated_size) {
1032
        if (!new_allocated_size)
1033
            new_allocated_size = new_size;
1034
        else
1035
            new_allocated_size += new_allocated_size / 2 + 1;
1036
    }
1037

    
1038
    if (new_allocated_size > d->allocated_size) {
1039
        d->buffer = av_realloc(d->buffer, new_allocated_size);
1040
        if(d->buffer == NULL)
1041
             return AVERROR(ENOMEM);
1042
        d->allocated_size = new_allocated_size;
1043
    }
1044
    memcpy(d->buffer + d->pos, buf, buf_size);
1045
    d->pos = new_size;
1046
    if (d->pos > d->size)
1047
        d->size = d->pos;
1048
    return buf_size;
1049
}
1050

    
1051
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1052
{
1053
    unsigned char buf1[4];
1054
    int ret;
1055

    
1056
    /* packetized write: output the header */
1057
    AV_WB32(buf1, buf_size);
1058
    ret= dyn_buf_write(opaque, buf1, 4);
1059
    if(ret < 0)
1060
        return ret;
1061

    
1062
    /* then the data */
1063
    return dyn_buf_write(opaque, buf, buf_size);
1064
}
1065

    
1066
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1067
{
1068
    DynBuffer *d = opaque;
1069

    
1070
    if (whence == SEEK_CUR)
1071
        offset += d->pos;
1072
    else if (whence == SEEK_END)
1073
        offset += d->size;
1074
    if (offset < 0 || offset > 0x7fffffffLL)
1075
        return -1;
1076
    d->pos = offset;
1077
    return 0;
1078
}
1079

    
1080
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1081
{
1082
    DynBuffer *d;
1083
    int ret;
1084
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1085

    
1086
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1087
        return -1;
1088
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1089
    if (!d)
1090
        return AVERROR(ENOMEM);
1091
    *s = av_mallocz(sizeof(AVIOContext));
1092
    if(!*s) {
1093
        av_free(d);
1094
        return AVERROR(ENOMEM);
1095
    }
1096
    d->io_buffer_size = io_buffer_size;
1097
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
1098
                        1, d, NULL,
1099
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1100
                        max_packet_size ? NULL : dyn_buf_seek);
1101
    if (ret == 0) {
1102
        (*s)->max_packet_size = max_packet_size;
1103
    } else {
1104
        av_free(d);
1105
        av_freep(s);
1106
    }
1107
    return ret;
1108
}
1109

    
1110
int url_open_dyn_buf(AVIOContext **s)
1111
{
1112
    return url_open_dyn_buf_internal(s, 0);
1113
}
1114

    
1115
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1116
{
1117
    if (max_packet_size <= 0)
1118
        return -1;
1119
    return url_open_dyn_buf_internal(s, max_packet_size);
1120
}
1121

    
1122
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1123
{
1124
    DynBuffer *d = s->opaque;
1125
    int size;
1126
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1127
    int padding = 0;
1128

    
1129
    /* don't attempt to pad fixed-size packet buffers */
1130
    if (!s->max_packet_size) {
1131
        avio_write(s, padbuf, sizeof(padbuf));
1132
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1133
    }
1134

    
1135
    put_flush_packet(s);
1136

    
1137
    *pbuffer = d->buffer;
1138
    size = d->size;
1139
    av_free(d);
1140
    av_free(s);
1141
    return size - padding;
1142
}
1143
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */