Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 933e90a6

History | View | Annotate | Download (27.5 KB)

1
/*
2
 * Buffered I/O for ffmpeg system
3
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; 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 avio_flush(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 avio_size(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
#if FF_API_OLD_AVIO
269
int url_feof(AVIOContext *s)
270
{
271
    if(!s)
272
        return 0;
273
    return s->eof_reached;
274
}
275

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

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

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

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

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

    
316
GET(16, unsigned int)
317
GET(24, unsigned int)
318
GET(32, unsigned int)
319
GET(64, uint64_t)
320

    
321
#undef GET
322

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

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

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

    
364
int url_fopen(AVIOContext **s, const char *filename, int flags)
365
{
366
    return avio_open(s, filename, flags);
367
}
368
int url_fclose(AVIOContext *s)
369
{
370
    return avio_close(s);
371
}
372
int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
373
{
374
    return avio_seek(s, offset, whence);
375
}
376
int64_t url_fsize(AVIOContext *s)
377
{
378
    return avio_size(s);
379
}
380
int url_setbufsize(AVIOContext *s, int buf_size)
381
{
382
    return ffio_set_buf_size(s, buf_size);
383
}
384
int url_fprintf(AVIOContext *s, const char *fmt, ...)
385
{
386
    va_list ap;
387
    char buf[4096];
388
    int ret;
389

    
390
    va_start(ap, fmt);
391
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
392
    va_end(ap);
393
    avio_write(s, buf, strlen(buf));
394
    return ret;
395
}
396
void put_flush_packet(AVIOContext *s)
397
{
398
    avio_flush(s);
399
}
400
int av_url_read_fpause(AVIOContext *s, int pause)
401
{
402
    return ffio_read_pause(s, pause);
403
}
404
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
405
                         int64_t timestamp, int flags)
406
{
407
    return ffio_read_seek(s, stream_index, timestamp, flags);
408
}
409
#endif
410

    
411
int avio_put_str(AVIOContext *s, const char *str)
412
{
413
    int len = 1;
414
    if (str) {
415
        len += strlen(str);
416
        avio_write(s, (const unsigned char *) str, len);
417
    } else
418
        avio_w8(s, 0);
419
    return len;
420
}
421

    
422
int avio_put_str16le(AVIOContext *s, const char *str)
423
{
424
    const uint8_t *q = str;
425
    int ret = 0;
426

    
427
    while (*q) {
428
        uint32_t ch;
429
        uint16_t tmp;
430

    
431
        GET_UTF8(ch, *q++, break;)
432
        PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
433
    }
434
    avio_wl16(s, 0);
435
    ret += 2;
436
    return ret;
437
}
438

    
439
int ff_get_v_length(uint64_t val){
440
    int i=1;
441

    
442
    while(val>>=7)
443
        i++;
444

    
445
    return i;
446
}
447

    
448
void ff_put_v(AVIOContext *bc, uint64_t val){
449
    int i= ff_get_v_length(val);
450

    
451
    while(--i>0)
452
        avio_w8(bc, 128 | (val>>(7*i)));
453

    
454
    avio_w8(bc, val&127);
455
}
456

    
457
void avio_wl64(AVIOContext *s, uint64_t val)
458
{
459
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
460
    avio_wl32(s, (uint32_t)(val >> 32));
461
}
462

    
463
void avio_wb64(AVIOContext *s, uint64_t val)
464
{
465
    avio_wb32(s, (uint32_t)(val >> 32));
466
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
467
}
468

    
469
void avio_wl16(AVIOContext *s, unsigned int val)
470
{
471
    avio_w8(s, val);
472
    avio_w8(s, val >> 8);
473
}
474

    
475
void avio_wb16(AVIOContext *s, unsigned int val)
476
{
477
    avio_w8(s, val >> 8);
478
    avio_w8(s, val);
479
}
480

    
481
void avio_wl24(AVIOContext *s, unsigned int val)
482
{
483
    avio_wl16(s, val & 0xffff);
484
    avio_w8(s, val >> 16);
485
}
486

    
487
void avio_wb24(AVIOContext *s, unsigned int val)
488
{
489
    avio_wb16(s, val >> 8);
490
    avio_w8(s, val);
491
}
492

    
493
#if FF_API_OLD_AVIO
494
void put_tag(AVIOContext *s, const char *tag)
495
{
496
    while (*tag) {
497
        avio_w8(s, *tag++);
498
    }
499
}
500
#endif
501

    
502
/* Input stream */
503

    
504
static void fill_buffer(AVIOContext *s)
505
{
506
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
507
    int len= s->buffer_size - (dst - s->buffer);
508
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
509

    
510
    /* no need to do anything if EOF already reached */
511
    if (s->eof_reached)
512
        return;
513

    
514
    if(s->update_checksum && dst == s->buffer){
515
        if(s->buf_end > s->checksum_ptr)
516
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
517
        s->checksum_ptr= s->buffer;
518
    }
519

    
520
    /* make buffer smaller in case it ended up large after probing */
521
    if (s->buffer_size > max_buffer_size) {
522
        ffio_set_buf_size(s, max_buffer_size);
523

    
524
        s->checksum_ptr = dst = s->buffer;
525
        len = s->buffer_size;
526
    }
527

    
528
    if(s->read_packet)
529
        len = s->read_packet(s->opaque, dst, len);
530
    else
531
        len = 0;
532
    if (len <= 0) {
533
        /* do not modify buffer if EOF reached so that a seek back can
534
           be done without rereading data */
535
        s->eof_reached = 1;
536
        if(len<0)
537
            s->error= len;
538
    } else {
539
        s->pos += len;
540
        s->buf_ptr = dst;
541
        s->buf_end = dst + len;
542
    }
543
}
544

    
545
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
546
                                    unsigned int len)
547
{
548
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
549
}
550

    
551
unsigned long get_checksum(AVIOContext *s)
552
{
553
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
554
    s->update_checksum= NULL;
555
    return s->checksum;
556
}
557

    
558
void init_checksum(AVIOContext *s,
559
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
560
                   unsigned long checksum)
561
{
562
    s->update_checksum= update_checksum;
563
    if(s->update_checksum){
564
        s->checksum= checksum;
565
        s->checksum_ptr= s->buf_ptr;
566
    }
567
}
568

    
569
/* XXX: put an inline version */
570
int avio_r8(AVIOContext *s)
571
{
572
    if (s->buf_ptr >= s->buf_end)
573
        fill_buffer(s);
574
    if (s->buf_ptr < s->buf_end)
575
        return *s->buf_ptr++;
576
    return 0;
577
}
578

    
579
#if FF_API_OLD_AVIO
580
int url_fgetc(AVIOContext *s)
581
{
582
    if (s->buf_ptr >= s->buf_end)
583
        fill_buffer(s);
584
    if (s->buf_ptr < s->buf_end)
585
        return *s->buf_ptr++;
586
    return URL_EOF;
587
}
588
#endif
589

    
590
int avio_read(AVIOContext *s, unsigned char *buf, int size)
591
{
592
    int len, size1;
593

    
594
    size1 = size;
595
    while (size > 0) {
596
        len = s->buf_end - s->buf_ptr;
597
        if (len > size)
598
            len = size;
599
        if (len == 0) {
600
            if(size > s->buffer_size && !s->update_checksum){
601
                if(s->read_packet)
602
                    len = s->read_packet(s->opaque, buf, size);
603
                if (len <= 0) {
604
                    /* do not modify buffer if EOF reached so that a seek back can
605
                    be done without rereading data */
606
                    s->eof_reached = 1;
607
                    if(len<0)
608
                        s->error= len;
609
                    break;
610
                } else {
611
                    s->pos += len;
612
                    size -= len;
613
                    buf += len;
614
                    s->buf_ptr = s->buffer;
615
                    s->buf_end = s->buffer/* + len*/;
616
                }
617
            }else{
618
                fill_buffer(s);
619
                len = s->buf_end - s->buf_ptr;
620
                if (len == 0)
621
                    break;
622
            }
623
        } else {
624
            memcpy(buf, s->buf_ptr, len);
625
            buf += len;
626
            s->buf_ptr += len;
627
            size -= len;
628
        }
629
    }
630
    if (size1 == size) {
631
        if (s->error)         return s->error;
632
        if (s->eof_reached)   return AVERROR_EOF;
633
    }
634
    return size1 - size;
635
}
636

    
637
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
638
{
639
    int len;
640

    
641
    if(size<0)
642
        return -1;
643

    
644
    len = s->buf_end - s->buf_ptr;
645
    if (len == 0) {
646
        fill_buffer(s);
647
        len = s->buf_end - s->buf_ptr;
648
    }
649
    if (len > size)
650
        len = size;
651
    memcpy(buf, s->buf_ptr, len);
652
    s->buf_ptr += len;
653
    if (!len) {
654
        if (s->error)         return s->error;
655
        if (s->eof_reached)   return AVERROR_EOF;
656
    }
657
    return len;
658
}
659

    
660
unsigned int avio_rl16(AVIOContext *s)
661
{
662
    unsigned int val;
663
    val = avio_r8(s);
664
    val |= avio_r8(s) << 8;
665
    return val;
666
}
667

    
668
unsigned int avio_rl24(AVIOContext *s)
669
{
670
    unsigned int val;
671
    val = avio_rl16(s);
672
    val |= avio_r8(s) << 16;
673
    return val;
674
}
675

    
676
unsigned int avio_rl32(AVIOContext *s)
677
{
678
    unsigned int val;
679
    val = avio_rl16(s);
680
    val |= avio_rl16(s) << 16;
681
    return val;
682
}
683

    
684
uint64_t avio_rl64(AVIOContext *s)
685
{
686
    uint64_t val;
687
    val = (uint64_t)avio_rl32(s);
688
    val |= (uint64_t)avio_rl32(s) << 32;
689
    return val;
690
}
691

    
692
unsigned int avio_rb16(AVIOContext *s)
693
{
694
    unsigned int val;
695
    val = avio_r8(s) << 8;
696
    val |= avio_r8(s);
697
    return val;
698
}
699

    
700
unsigned int avio_rb24(AVIOContext *s)
701
{
702
    unsigned int val;
703
    val = avio_rb16(s) << 8;
704
    val |= avio_r8(s);
705
    return val;
706
}
707
unsigned int avio_rb32(AVIOContext *s)
708
{
709
    unsigned int val;
710
    val = avio_rb16(s) << 16;
711
    val |= avio_rb16(s);
712
    return val;
713
}
714

    
715
#if FF_API_OLD_AVIO
716
char *get_strz(AVIOContext *s, char *buf, int maxlen)
717
{
718
    avio_get_str(s, INT_MAX, buf, maxlen);
719
    return buf;
720
}
721
#endif
722

    
723
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
724
{
725
    int i = 0;
726
    char c;
727

    
728
    do {
729
        c = avio_r8(s);
730
        if (c && i < maxlen-1)
731
            buf[i++] = c;
732
    } while (c != '\n' && c);
733

    
734
    buf[i] = 0;
735
    return i;
736
}
737

    
738
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
739
{
740
    int i;
741

    
742
    // reserve 1 byte for terminating 0
743
    buflen = FFMIN(buflen - 1, maxlen);
744
    for (i = 0; i < buflen; i++)
745
        if (!(buf[i] = avio_r8(s)))
746
            return i + 1;
747
    if (buflen)
748
        buf[i] = 0;
749
    for (; i < maxlen; i++)
750
        if (!avio_r8(s))
751
            return i + 1;
752
    return maxlen;
753
}
754

    
755
#define GET_STR16(type, read) \
756
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
757
{\
758
    char* q = buf;\
759
    int ret = 0;\
760
    while (ret + 1 < maxlen) {\
761
        uint8_t tmp;\
762
        uint32_t ch;\
763
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
764
        if (!ch)\
765
            break;\
766
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
767
    }\
768
    *q = 0;\
769
    return ret;\
770
}\
771

    
772
GET_STR16(le, avio_rl16)
773
GET_STR16(be, avio_rb16)
774

    
775
#undef GET_STR16
776

    
777
uint64_t avio_rb64(AVIOContext *s)
778
{
779
    uint64_t val;
780
    val = (uint64_t)avio_rb32(s) << 32;
781
    val |= (uint64_t)avio_rb32(s);
782
    return val;
783
}
784

    
785
uint64_t ffio_read_varlen(AVIOContext *bc){
786
    uint64_t val = 0;
787
    int tmp;
788

    
789
    do{
790
        tmp = avio_r8(bc);
791
        val= (val<<7) + (tmp&127);
792
    }while(tmp&128);
793
    return val;
794
}
795

    
796
int url_fdopen(AVIOContext **s, URLContext *h)
797
{
798
    uint8_t *buffer;
799
    int buffer_size, max_packet_size;
800

    
801
    max_packet_size = url_get_max_packet_size(h);
802
    if (max_packet_size) {
803
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
804
    } else {
805
        buffer_size = IO_BUFFER_SIZE;
806
    }
807
    buffer = av_malloc(buffer_size);
808
    if (!buffer)
809
        return AVERROR(ENOMEM);
810

    
811
    *s = av_mallocz(sizeof(AVIOContext));
812
    if(!*s) {
813
        av_free(buffer);
814
        return AVERROR(ENOMEM);
815
    }
816

    
817
    if (ffio_init_context(*s, buffer, buffer_size,
818
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
819
                      url_read, url_write, url_seek) < 0) {
820
        av_free(buffer);
821
        av_freep(s);
822
        return AVERROR(EIO);
823
    }
824
    (*s)->is_streamed = h->is_streamed;
825
    (*s)->max_packet_size = max_packet_size;
826
    if(h->prot) {
827
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
828
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
829
    }
830
    return 0;
831
}
832

    
833
int ffio_set_buf_size(AVIOContext *s, int buf_size)
834
{
835
    uint8_t *buffer;
836
    buffer = av_malloc(buf_size);
837
    if (!buffer)
838
        return AVERROR(ENOMEM);
839

    
840
    av_free(s->buffer);
841
    s->buffer = buffer;
842
    s->buffer_size = buf_size;
843
    s->buf_ptr = buffer;
844
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
845
    return 0;
846
}
847

    
848
#if FF_API_URL_RESETBUF
849
int url_resetbuf(AVIOContext *s, int flags)
850
#else
851
static int url_resetbuf(AVIOContext *s, int flags)
852
#endif
853
{
854
#if FF_API_URL_RESETBUF
855
    if (flags & URL_RDWR)
856
        return AVERROR(EINVAL);
857
#else
858
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
859
#endif
860

    
861
    if (flags & URL_WRONLY) {
862
        s->buf_end = s->buffer + s->buffer_size;
863
        s->write_flag = 1;
864
    } else {
865
        s->buf_end = s->buffer;
866
        s->write_flag = 0;
867
    }
868
    return 0;
869
}
870

    
871
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
872
{
873
    int64_t buffer_start;
874
    int buffer_size;
875
    int overlap, new_size, alloc_size;
876

    
877
    if (s->write_flag)
878
        return AVERROR(EINVAL);
879

    
880
    buffer_size = s->buf_end - s->buffer;
881

    
882
    /* the buffers must touch or overlap */
883
    if ((buffer_start = s->pos - buffer_size) > buf_size)
884
        return AVERROR(EINVAL);
885

    
886
    overlap = buf_size - buffer_start;
887
    new_size = buf_size + buffer_size - overlap;
888

    
889
    alloc_size = FFMAX(s->buffer_size, new_size);
890
    if (alloc_size > buf_size)
891
        if (!(buf = av_realloc(buf, alloc_size)))
892
            return AVERROR(ENOMEM);
893

    
894
    if (new_size > buf_size) {
895
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
896
        buf_size = new_size;
897
    }
898

    
899
    av_free(s->buffer);
900
    s->buf_ptr = s->buffer = buf;
901
    s->buffer_size = alloc_size;
902
    s->pos = buf_size;
903
    s->buf_end = s->buf_ptr + buf_size;
904
    s->eof_reached = 0;
905
    s->must_flush = 0;
906

    
907
    return 0;
908
}
909

    
910
int avio_open(AVIOContext **s, const char *filename, int flags)
911
{
912
    URLContext *h;
913
    int err;
914

    
915
    err = url_open(&h, filename, flags);
916
    if (err < 0)
917
        return err;
918
    err = url_fdopen(s, h);
919
    if (err < 0) {
920
        url_close(h);
921
        return err;
922
    }
923
    return 0;
924
}
925

    
926
int avio_close(AVIOContext *s)
927
{
928
    URLContext *h = s->opaque;
929

    
930
    av_free(s->buffer);
931
    av_free(s);
932
    return url_close(h);
933
}
934

    
935
#if FF_API_OLD_AVIO
936
URLContext *url_fileno(AVIOContext *s)
937
{
938
    return s->opaque;
939
}
940
#endif
941

    
942
int avio_printf(AVIOContext *s, const char *fmt, ...)
943
{
944
    va_list ap;
945
    char buf[4096];
946
    int ret;
947

    
948
    va_start(ap, fmt);
949
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
950
    va_end(ap);
951
    avio_write(s, buf, strlen(buf));
952
    return ret;
953
}
954

    
955
#if FF_API_OLD_AVIO
956
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
957
{
958
    int c;
959
    char *q;
960

    
961
    c = avio_r8(s);
962
    if (s->eof_reached)
963
        return NULL;
964
    q = buf;
965
    for(;;) {
966
        if (s->eof_reached || c == '\n')
967
            break;
968
        if ((q - buf) < buf_size - 1)
969
            *q++ = c;
970
        c = avio_r8(s);
971
    }
972
    if (buf_size > 0)
973
        *q = '\0';
974
    return buf;
975
}
976

    
977
int url_fget_max_packet_size(AVIOContext *s)
978
{
979
    return s->max_packet_size;
980
}
981
#endif
982

    
983
int ffio_read_pause(AVIOContext *s, int pause)
984
{
985
    if (!s->read_pause)
986
        return AVERROR(ENOSYS);
987
    return s->read_pause(s->opaque, pause);
988
}
989

    
990
int64_t ffio_read_seek(AVIOContext *s, int stream_index,
991
                       int64_t timestamp, int flags)
992
{
993
    URLContext *h = s->opaque;
994
    int64_t ret;
995
    if (!s->read_seek)
996
        return AVERROR(ENOSYS);
997
    ret = s->read_seek(h, stream_index, timestamp, flags);
998
    if(ret >= 0) {
999
        int64_t pos;
1000
        s->buf_ptr = s->buf_end; // Flush buffer
1001
        pos = s->seek(h, 0, SEEK_CUR);
1002
        if (pos >= 0)
1003
            s->pos = pos;
1004
        else if (pos != AVERROR(ENOSYS))
1005
            ret = pos;
1006
    }
1007
    return ret;
1008
}
1009

    
1010
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
1011
 * back to the server even if CONFIG_MUXERS is false. */
1012
#if CONFIG_MUXERS || CONFIG_NETWORK
1013
/* buffer handling */
1014
#if FF_API_OLD_AVIO
1015
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
1016
{
1017
    int ret;
1018
    *s = av_mallocz(sizeof(AVIOContext));
1019
    if(!*s)
1020
        return AVERROR(ENOMEM);
1021
    ret = ffio_init_context(*s, buf, buf_size,
1022
                        (flags & URL_WRONLY || flags & URL_RDWR),
1023
                        NULL, NULL, NULL, NULL);
1024
    if(ret != 0)
1025
        av_freep(s);
1026
    return ret;
1027
}
1028

    
1029
int url_close_buf(AVIOContext *s)
1030
{
1031
    avio_flush(s);
1032
    return s->buf_ptr - s->buffer;
1033
}
1034
#endif
1035

    
1036
/* output in a dynamic buffer */
1037

    
1038
typedef struct DynBuffer {
1039
    int pos, size, allocated_size;
1040
    uint8_t *buffer;
1041
    int io_buffer_size;
1042
    uint8_t io_buffer[1];
1043
} DynBuffer;
1044

    
1045
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1046
{
1047
    DynBuffer *d = opaque;
1048
    unsigned new_size, new_allocated_size;
1049

    
1050
    /* reallocate buffer if needed */
1051
    new_size = d->pos + buf_size;
1052
    new_allocated_size = d->allocated_size;
1053
    if(new_size < d->pos || new_size > INT_MAX/2)
1054
        return -1;
1055
    while (new_size > new_allocated_size) {
1056
        if (!new_allocated_size)
1057
            new_allocated_size = new_size;
1058
        else
1059
            new_allocated_size += new_allocated_size / 2 + 1;
1060
    }
1061

    
1062
    if (new_allocated_size > d->allocated_size) {
1063
        d->buffer = av_realloc(d->buffer, new_allocated_size);
1064
        if(d->buffer == NULL)
1065
             return AVERROR(ENOMEM);
1066
        d->allocated_size = new_allocated_size;
1067
    }
1068
    memcpy(d->buffer + d->pos, buf, buf_size);
1069
    d->pos = new_size;
1070
    if (d->pos > d->size)
1071
        d->size = d->pos;
1072
    return buf_size;
1073
}
1074

    
1075
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1076
{
1077
    unsigned char buf1[4];
1078
    int ret;
1079

    
1080
    /* packetized write: output the header */
1081
    AV_WB32(buf1, buf_size);
1082
    ret= dyn_buf_write(opaque, buf1, 4);
1083
    if(ret < 0)
1084
        return ret;
1085

    
1086
    /* then the data */
1087
    return dyn_buf_write(opaque, buf, buf_size);
1088
}
1089

    
1090
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1091
{
1092
    DynBuffer *d = opaque;
1093

    
1094
    if (whence == SEEK_CUR)
1095
        offset += d->pos;
1096
    else if (whence == SEEK_END)
1097
        offset += d->size;
1098
    if (offset < 0 || offset > 0x7fffffffLL)
1099
        return -1;
1100
    d->pos = offset;
1101
    return 0;
1102
}
1103

    
1104
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1105
{
1106
    DynBuffer *d;
1107
    int ret;
1108
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1109

    
1110
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1111
        return -1;
1112
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1113
    if (!d)
1114
        return AVERROR(ENOMEM);
1115
    *s = av_mallocz(sizeof(AVIOContext));
1116
    if(!*s) {
1117
        av_free(d);
1118
        return AVERROR(ENOMEM);
1119
    }
1120
    d->io_buffer_size = io_buffer_size;
1121
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
1122
                        1, d, NULL,
1123
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1124
                        max_packet_size ? NULL : dyn_buf_seek);
1125
    if (ret == 0) {
1126
        (*s)->max_packet_size = max_packet_size;
1127
    } else {
1128
        av_free(d);
1129
        av_freep(s);
1130
    }
1131
    return ret;
1132
}
1133

    
1134
int url_open_dyn_buf(AVIOContext **s)
1135
{
1136
    return url_open_dyn_buf_internal(s, 0);
1137
}
1138

    
1139
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1140
{
1141
    if (max_packet_size <= 0)
1142
        return -1;
1143
    return url_open_dyn_buf_internal(s, max_packet_size);
1144
}
1145

    
1146
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1147
{
1148
    DynBuffer *d = s->opaque;
1149
    int size;
1150
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1151
    int padding = 0;
1152

    
1153
    /* don't attempt to pad fixed-size packet buffers */
1154
    if (!s->max_packet_size) {
1155
        avio_write(s, padbuf, sizeof(padbuf));
1156
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1157
    }
1158

    
1159
    avio_flush(s);
1160

    
1161
    *pbuffer = d->buffer;
1162
    size = d->size;
1163
    av_free(d);
1164
    av_free(s);
1165
    return size - padding;
1166
}
1167
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */