Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 183401b9

History | View | Annotate | Download (28 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 "url.h"
29
#include <stdarg.h>
30

    
31
#define IO_BUFFER_SIZE 32768
32

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

    
40
static void fill_buffer(AVIOContext *s);
41
static int url_resetbuf(AVIOContext *s, int flags);
42

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

    
79
#if FF_API_OLD_AVIO
80
int init_put_byte(AVIOContext *s,
81
                  unsigned char *buffer,
82
                  int buffer_size,
83
                  int write_flag,
84
                  void *opaque,
85
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
86
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
87
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
88
{
89
    return ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
90
                                read_packet, write_packet, seek);
91
}
92
AVIOContext *av_alloc_put_byte(
93
                  unsigned char *buffer,
94
                  int buffer_size,
95
                  int write_flag,
96
                  void *opaque,
97
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
98
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
99
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
100
{
101
    return avio_alloc_context(buffer, buffer_size, write_flag, opaque,
102
                              read_packet, write_packet, seek);
103
}
104
#endif
105

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

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

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

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

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

    
156
        count -= len;
157
    }
158
}
159

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

    
167
        if (s->buf_ptr >= s->buf_end)
168
            flush_buffer(s);
169

    
170
        buf += len;
171
        size -= len;
172
    }
173
}
174

    
175
void avio_flush(AVIOContext *s)
176
{
177
    flush_buffer(s);
178
    s->must_flush = 0;
179
}
180

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

    
188
    if(!s)
189
        return AVERROR(EINVAL);
190

    
191
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
192

    
193
    if (whence != SEEK_CUR && whence != SEEK_SET)
194
        return AVERROR(EINVAL);
195

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

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

    
238
int64_t avio_skip(AVIOContext *s, int64_t offset)
239
{
240
    return avio_seek(s, offset, SEEK_CUR);
241
}
242

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

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

    
256
int64_t avio_size(AVIOContext *s)
257
{
258
    int64_t size;
259

    
260
    if(!s)
261
        return AVERROR(EINVAL);
262

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

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

    
286
#if FF_API_OLD_AVIO
287
int url_ferror(AVIOContext *s)
288
{
289
    if(!s)
290
        return 0;
291
    return s->error;
292
}
293
#endif
294

    
295
void avio_wl32(AVIOContext *s, unsigned int val)
296
{
297
    avio_w8(s, val);
298
    avio_w8(s, val >> 8);
299
    avio_w8(s, val >> 16);
300
    avio_w8(s, val >> 24);
301
}
302

    
303
void avio_wb32(AVIOContext *s, unsigned int val)
304
{
305
    avio_w8(s, val >> 24);
306
    avio_w8(s, val >> 16);
307
    avio_w8(s, val >> 8);
308
    avio_w8(s, val);
309
}
310

    
311
#if FF_API_OLD_AVIO
312
void put_strz(AVIOContext *s, const char *str)
313
{
314
    avio_put_str(s, str);
315
}
316

    
317
#define GET(name, type) \
318
    type get_be ##name(AVIOContext *s) \
319
{\
320
    return avio_rb ##name(s);\
321
}\
322
    type get_le ##name(AVIOContext *s) \
323
{\
324
    return avio_rl ##name(s);\
325
}
326

    
327
GET(16, unsigned int)
328
GET(24, unsigned int)
329
GET(32, unsigned int)
330
GET(64, uint64_t)
331

    
332
#undef GET
333

    
334
#define PUT(name, type ) \
335
    void put_le ##name(AVIOContext *s, type val)\
336
{\
337
        avio_wl ##name(s, val);\
338
}\
339
    void put_be ##name(AVIOContext *s, type val)\
340
{\
341
        avio_wb ##name(s, val);\
342
}
343

    
344
PUT(16, unsigned int)
345
PUT(24, unsigned int)
346
PUT(32, unsigned int)
347
PUT(64, uint64_t)
348
#undef PUT
349

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

    
375
int url_fopen(AVIOContext **s, const char *filename, int flags)
376
{
377
    return avio_open(s, filename, flags+1);
378
}
379
int url_fclose(AVIOContext *s)
380
{
381
    return avio_close(s);
382
}
383
int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
384
{
385
    return avio_seek(s, offset, whence);
386
}
387
int64_t url_fsize(AVIOContext *s)
388
{
389
    return avio_size(s);
390
}
391
int url_setbufsize(AVIOContext *s, int buf_size)
392
{
393
    return ffio_set_buf_size(s, buf_size);
394
}
395
int url_fprintf(AVIOContext *s, const char *fmt, ...)
396
{
397
    va_list ap;
398
    char buf[4096];
399
    int ret;
400

    
401
    va_start(ap, fmt);
402
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
403
    va_end(ap);
404
    avio_write(s, buf, strlen(buf));
405
    return ret;
406
}
407
void put_flush_packet(AVIOContext *s)
408
{
409
    avio_flush(s);
410
}
411
int av_url_read_fpause(AVIOContext *s, int pause)
412
{
413
    return avio_pause(s, pause);
414
}
415
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
416
                         int64_t timestamp, int flags)
417
{
418
    return avio_seek_time(s, stream_index, timestamp, flags);
419
}
420
void init_checksum(AVIOContext *s,
421
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
422
                   unsigned long checksum)
423
{
424
    ffio_init_checksum(s, update_checksum, checksum);
425
}
426
unsigned long get_checksum(AVIOContext *s)
427
{
428
    return ffio_get_checksum(s);
429
}
430
int url_open_dyn_buf(AVIOContext **s)
431
{
432
    return avio_open_dyn_buf(s);
433
}
434
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
435
{
436
    return ffio_open_dyn_packet_buf(s, max_packet_size);
437
}
438
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
439
{
440
    return avio_close_dyn_buf(s, pbuffer);
441
}
442
int url_fdopen(AVIOContext **s, URLContext *h)
443
{
444
    return ffio_fdopen(s, h);
445
}
446
#endif
447

    
448
int avio_put_str(AVIOContext *s, const char *str)
449
{
450
    int len = 1;
451
    if (str) {
452
        len += strlen(str);
453
        avio_write(s, (const unsigned char *) str, len);
454
    } else
455
        avio_w8(s, 0);
456
    return len;
457
}
458

    
459
int avio_put_str16le(AVIOContext *s, const char *str)
460
{
461
    const uint8_t *q = str;
462
    int ret = 0;
463

    
464
    while (*q) {
465
        uint32_t ch;
466
        uint16_t tmp;
467

    
468
        GET_UTF8(ch, *q++, break;)
469
        PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
470
    }
471
    avio_wl16(s, 0);
472
    ret += 2;
473
    return ret;
474
}
475

    
476
int ff_get_v_length(uint64_t val){
477
    int i=1;
478

    
479
    while(val>>=7)
480
        i++;
481

    
482
    return i;
483
}
484

    
485
void ff_put_v(AVIOContext *bc, uint64_t val){
486
    int i= ff_get_v_length(val);
487

    
488
    while(--i>0)
489
        avio_w8(bc, 128 | (val>>(7*i)));
490

    
491
    avio_w8(bc, val&127);
492
}
493

    
494
void avio_wl64(AVIOContext *s, uint64_t val)
495
{
496
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
497
    avio_wl32(s, (uint32_t)(val >> 32));
498
}
499

    
500
void avio_wb64(AVIOContext *s, uint64_t val)
501
{
502
    avio_wb32(s, (uint32_t)(val >> 32));
503
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
504
}
505

    
506
void avio_wl16(AVIOContext *s, unsigned int val)
507
{
508
    avio_w8(s, val);
509
    avio_w8(s, val >> 8);
510
}
511

    
512
void avio_wb16(AVIOContext *s, unsigned int val)
513
{
514
    avio_w8(s, val >> 8);
515
    avio_w8(s, val);
516
}
517

    
518
void avio_wl24(AVIOContext *s, unsigned int val)
519
{
520
    avio_wl16(s, val & 0xffff);
521
    avio_w8(s, val >> 16);
522
}
523

    
524
void avio_wb24(AVIOContext *s, unsigned int val)
525
{
526
    avio_wb16(s, val >> 8);
527
    avio_w8(s, val);
528
}
529

    
530
#if FF_API_OLD_AVIO
531
void put_tag(AVIOContext *s, const char *tag)
532
{
533
    while (*tag) {
534
        avio_w8(s, *tag++);
535
    }
536
}
537
#endif
538

    
539
/* Input stream */
540

    
541
static void fill_buffer(AVIOContext *s)
542
{
543
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
544
    int len= s->buffer_size - (dst - s->buffer);
545
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
546

    
547
    /* no need to do anything if EOF already reached */
548
    if (s->eof_reached)
549
        return;
550

    
551
    if(s->update_checksum && dst == s->buffer){
552
        if(s->buf_end > s->checksum_ptr)
553
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
554
        s->checksum_ptr= s->buffer;
555
    }
556

    
557
    /* make buffer smaller in case it ended up large after probing */
558
    if (s->read_packet && s->buffer_size > max_buffer_size) {
559
        ffio_set_buf_size(s, max_buffer_size);
560

    
561
        s->checksum_ptr = dst = s->buffer;
562
        len = s->buffer_size;
563
    }
564

    
565
    if(s->read_packet)
566
        len = s->read_packet(s->opaque, dst, len);
567
    else
568
        len = 0;
569
    if (len <= 0) {
570
        /* do not modify buffer if EOF reached so that a seek back can
571
           be done without rereading data */
572
        s->eof_reached = 1;
573
        if(len<0)
574
            s->error= len;
575
    } else {
576
        s->pos += len;
577
        s->buf_ptr = dst;
578
        s->buf_end = dst + len;
579
    }
580
}
581

    
582
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
583
                                    unsigned int len)
584
{
585
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
586
}
587

    
588
unsigned long ffio_get_checksum(AVIOContext *s)
589
{
590
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
591
    s->update_checksum= NULL;
592
    return s->checksum;
593
}
594

    
595
void ffio_init_checksum(AVIOContext *s,
596
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
597
                   unsigned long checksum)
598
{
599
    s->update_checksum= update_checksum;
600
    if(s->update_checksum){
601
        s->checksum= checksum;
602
        s->checksum_ptr= s->buf_ptr;
603
    }
604
}
605

    
606
/* XXX: put an inline version */
607
int avio_r8(AVIOContext *s)
608
{
609
    if (s->buf_ptr >= s->buf_end)
610
        fill_buffer(s);
611
    if (s->buf_ptr < s->buf_end)
612
        return *s->buf_ptr++;
613
    return 0;
614
}
615

    
616
#if FF_API_OLD_AVIO
617
int url_fgetc(AVIOContext *s)
618
{
619
    if (s->buf_ptr >= s->buf_end)
620
        fill_buffer(s);
621
    if (s->buf_ptr < s->buf_end)
622
        return *s->buf_ptr++;
623
    return URL_EOF;
624
}
625
#endif
626

    
627
int avio_read(AVIOContext *s, unsigned char *buf, int size)
628
{
629
    int len, size1;
630

    
631
    size1 = size;
632
    while (size > 0) {
633
        len = s->buf_end - s->buf_ptr;
634
        if (len > size)
635
            len = size;
636
        if (len == 0) {
637
            if(size > s->buffer_size && !s->update_checksum){
638
                if(s->read_packet)
639
                    len = s->read_packet(s->opaque, buf, size);
640
                if (len <= 0) {
641
                    /* do not modify buffer if EOF reached so that a seek back can
642
                    be done without rereading data */
643
                    s->eof_reached = 1;
644
                    if(len<0)
645
                        s->error= len;
646
                    break;
647
                } else {
648
                    s->pos += len;
649
                    size -= len;
650
                    buf += len;
651
                    s->buf_ptr = s->buffer;
652
                    s->buf_end = s->buffer/* + len*/;
653
                }
654
            }else{
655
                fill_buffer(s);
656
                len = s->buf_end - s->buf_ptr;
657
                if (len == 0)
658
                    break;
659
            }
660
        } else {
661
            memcpy(buf, s->buf_ptr, len);
662
            buf += len;
663
            s->buf_ptr += len;
664
            size -= len;
665
        }
666
    }
667
    if (size1 == size) {
668
        if (s->error)      return s->error;
669
        if (url_feof(s))   return AVERROR_EOF;
670
    }
671
    return size1 - size;
672
}
673

    
674
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
675
{
676
    int len;
677

    
678
    if(size<0)
679
        return -1;
680

    
681
    len = s->buf_end - s->buf_ptr;
682
    if (len == 0) {
683
        fill_buffer(s);
684
        len = s->buf_end - s->buf_ptr;
685
    }
686
    if (len > size)
687
        len = size;
688
    memcpy(buf, s->buf_ptr, len);
689
    s->buf_ptr += len;
690
    if (!len) {
691
        if (s->error)      return s->error;
692
        if (url_feof(s))   return AVERROR_EOF;
693
    }
694
    return len;
695
}
696

    
697
unsigned int avio_rl16(AVIOContext *s)
698
{
699
    unsigned int val;
700
    val = avio_r8(s);
701
    val |= avio_r8(s) << 8;
702
    return val;
703
}
704

    
705
unsigned int avio_rl24(AVIOContext *s)
706
{
707
    unsigned int val;
708
    val = avio_rl16(s);
709
    val |= avio_r8(s) << 16;
710
    return val;
711
}
712

    
713
unsigned int avio_rl32(AVIOContext *s)
714
{
715
    unsigned int val;
716
    val = avio_rl16(s);
717
    val |= avio_rl16(s) << 16;
718
    return val;
719
}
720

    
721
uint64_t avio_rl64(AVIOContext *s)
722
{
723
    uint64_t val;
724
    val = (uint64_t)avio_rl32(s);
725
    val |= (uint64_t)avio_rl32(s) << 32;
726
    return val;
727
}
728

    
729
unsigned int avio_rb16(AVIOContext *s)
730
{
731
    unsigned int val;
732
    val = avio_r8(s) << 8;
733
    val |= avio_r8(s);
734
    return val;
735
}
736

    
737
unsigned int avio_rb24(AVIOContext *s)
738
{
739
    unsigned int val;
740
    val = avio_rb16(s) << 8;
741
    val |= avio_r8(s);
742
    return val;
743
}
744
unsigned int avio_rb32(AVIOContext *s)
745
{
746
    unsigned int val;
747
    val = avio_rb16(s) << 16;
748
    val |= avio_rb16(s);
749
    return val;
750
}
751

    
752
#if FF_API_OLD_AVIO
753
char *get_strz(AVIOContext *s, char *buf, int maxlen)
754
{
755
    avio_get_str(s, INT_MAX, buf, maxlen);
756
    return buf;
757
}
758
#endif
759

    
760
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
761
{
762
    int i = 0;
763
    char c;
764

    
765
    do {
766
        c = avio_r8(s);
767
        if (c && i < maxlen-1)
768
            buf[i++] = c;
769
    } while (c != '\n' && c);
770

    
771
    buf[i] = 0;
772
    return i;
773
}
774

    
775
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
776
{
777
    int i;
778

    
779
    // reserve 1 byte for terminating 0
780
    buflen = FFMIN(buflen - 1, maxlen);
781
    for (i = 0; i < buflen; i++)
782
        if (!(buf[i] = avio_r8(s)))
783
            return i + 1;
784
    if (buflen)
785
        buf[i] = 0;
786
    for (; i < maxlen; i++)
787
        if (!avio_r8(s))
788
            return i + 1;
789
    return maxlen;
790
}
791

    
792
#define GET_STR16(type, read) \
793
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
794
{\
795
    char* q = buf;\
796
    int ret = 0;\
797
    while (ret + 1 < maxlen) {\
798
        uint8_t tmp;\
799
        uint32_t ch;\
800
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
801
        if (!ch)\
802
            break;\
803
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
804
    }\
805
    *q = 0;\
806
    return ret;\
807
}\
808

    
809
GET_STR16(le, avio_rl16)
810
GET_STR16(be, avio_rb16)
811

    
812
#undef GET_STR16
813

    
814
uint64_t avio_rb64(AVIOContext *s)
815
{
816
    uint64_t val;
817
    val = (uint64_t)avio_rb32(s) << 32;
818
    val |= (uint64_t)avio_rb32(s);
819
    return val;
820
}
821

    
822
uint64_t ffio_read_varlen(AVIOContext *bc){
823
    uint64_t val = 0;
824
    int tmp;
825

    
826
    do{
827
        tmp = avio_r8(bc);
828
        val= (val<<7) + (tmp&127);
829
    }while(tmp&128);
830
    return val;
831
}
832

    
833
int ffio_fdopen(AVIOContext **s, URLContext *h)
834
{
835
    uint8_t *buffer;
836
    int buffer_size, max_packet_size;
837

    
838
    max_packet_size = h->max_packet_size;
839
    if (max_packet_size) {
840
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
841
    } else {
842
        buffer_size = IO_BUFFER_SIZE;
843
    }
844
    buffer = av_malloc(buffer_size);
845
    if (!buffer)
846
        return AVERROR(ENOMEM);
847

    
848
    *s = av_mallocz(sizeof(AVIOContext));
849
    if(!*s) {
850
        av_free(buffer);
851
        return AVERROR(ENOMEM);
852
    }
853

    
854
    if (ffio_init_context(*s, buffer, buffer_size,
855
                      h->flags & AVIO_FLAG_WRITE, h,
856
                      (void*)ffurl_read, (void*)ffurl_write, (void*)ffurl_seek) < 0) {
857
        av_free(buffer);
858
        av_freep(s);
859
        return AVERROR(EIO);
860
    }
861
#if FF_API_OLD_AVIO
862
    (*s)->is_streamed = h->is_streamed;
863
#endif
864
    (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
865
    (*s)->max_packet_size = max_packet_size;
866
    if(h->prot) {
867
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
868
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
869
    }
870
    return 0;
871
}
872

    
873
int ffio_set_buf_size(AVIOContext *s, int buf_size)
874
{
875
    uint8_t *buffer;
876
    buffer = av_malloc(buf_size);
877
    if (!buffer)
878
        return AVERROR(ENOMEM);
879

    
880
    av_free(s->buffer);
881
    s->buffer = buffer;
882
    s->buffer_size = buf_size;
883
    s->buf_ptr = buffer;
884
    url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
885
    return 0;
886
}
887

    
888
static int url_resetbuf(AVIOContext *s, int flags)
889
{
890
    assert(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
891

    
892
    if (flags & AVIO_FLAG_WRITE) {
893
        s->buf_end = s->buffer + s->buffer_size;
894
        s->write_flag = 1;
895
    } else {
896
        s->buf_end = s->buffer;
897
        s->write_flag = 0;
898
    }
899
    return 0;
900
}
901

    
902
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
903
{
904
    int64_t buffer_start;
905
    int buffer_size;
906
    int overlap, new_size, alloc_size;
907

    
908
    if (s->write_flag)
909
        return AVERROR(EINVAL);
910

    
911
    buffer_size = s->buf_end - s->buffer;
912

    
913
    /* the buffers must touch or overlap */
914
    if ((buffer_start = s->pos - buffer_size) > buf_size)
915
        return AVERROR(EINVAL);
916

    
917
    overlap = buf_size - buffer_start;
918
    new_size = buf_size + buffer_size - overlap;
919

    
920
    alloc_size = FFMAX(s->buffer_size, new_size);
921
    if (alloc_size > buf_size)
922
        if (!(buf = av_realloc(buf, alloc_size)))
923
            return AVERROR(ENOMEM);
924

    
925
    if (new_size > buf_size) {
926
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
927
        buf_size = new_size;
928
    }
929

    
930
    av_free(s->buffer);
931
    s->buf_ptr = s->buffer = buf;
932
    s->buffer_size = alloc_size;
933
    s->pos = buf_size;
934
    s->buf_end = s->buf_ptr + buf_size;
935
    s->eof_reached = 0;
936
    s->must_flush = 0;
937

    
938
    return 0;
939
}
940

    
941
int avio_open(AVIOContext **s, const char *filename, int flags)
942
{
943
    URLContext *h;
944
    int err;
945

    
946
    err = ffurl_open(&h, filename, flags);
947
    if (err < 0)
948
        return err;
949
    err = ffio_fdopen(s, h);
950
    if (err < 0) {
951
        ffurl_close(h);
952
        return err;
953
    }
954
    return 0;
955
}
956

    
957
int avio_close(AVIOContext *s)
958
{
959
    URLContext *h = s->opaque;
960

    
961
    av_free(s->buffer);
962
    av_free(s);
963
    return ffurl_close(h);
964
}
965

    
966
#if FF_API_OLD_AVIO
967
URLContext *url_fileno(AVIOContext *s)
968
{
969
    return s->opaque;
970
}
971
#endif
972

    
973
int avio_printf(AVIOContext *s, const char *fmt, ...)
974
{
975
    va_list ap;
976
    char buf[4096];
977
    int ret;
978

    
979
    va_start(ap, fmt);
980
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
981
    va_end(ap);
982
    avio_write(s, buf, strlen(buf));
983
    return ret;
984
}
985

    
986
#if FF_API_OLD_AVIO
987
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
988
{
989
    int c;
990
    char *q;
991

    
992
    c = avio_r8(s);
993
    if (url_feof(s))
994
        return NULL;
995
    q = buf;
996
    for(;;) {
997
        if (url_feof(s) || c == '\n')
998
            break;
999
        if ((q - buf) < buf_size - 1)
1000
            *q++ = c;
1001
        c = avio_r8(s);
1002
    }
1003
    if (buf_size > 0)
1004
        *q = '\0';
1005
    return buf;
1006
}
1007

    
1008
int url_fget_max_packet_size(AVIOContext *s)
1009
{
1010
    return s->max_packet_size;
1011
}
1012
#endif
1013

    
1014
int avio_pause(AVIOContext *s, int pause)
1015
{
1016
    if (!s->read_pause)
1017
        return AVERROR(ENOSYS);
1018
    return s->read_pause(s->opaque, pause);
1019
}
1020

    
1021
int64_t avio_seek_time(AVIOContext *s, int stream_index,
1022
                       int64_t timestamp, int flags)
1023
{
1024
    URLContext *h = s->opaque;
1025
    int64_t ret;
1026
    if (!s->read_seek)
1027
        return AVERROR(ENOSYS);
1028
    ret = s->read_seek(h, stream_index, timestamp, flags);
1029
    if(ret >= 0) {
1030
        int64_t pos;
1031
        s->buf_ptr = s->buf_end; // Flush buffer
1032
        pos = s->seek(h, 0, SEEK_CUR);
1033
        if (pos >= 0)
1034
            s->pos = pos;
1035
        else if (pos != AVERROR(ENOSYS))
1036
            ret = pos;
1037
    }
1038
    return ret;
1039
}
1040

    
1041
/* buffer handling */
1042
#if FF_API_OLD_AVIO
1043
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
1044
{
1045
    int ret;
1046
    *s = av_mallocz(sizeof(AVIOContext));
1047
    if(!*s)
1048
        return AVERROR(ENOMEM);
1049
    ret = ffio_init_context(*s, buf, buf_size,
1050
                            flags & AVIO_FLAG_WRITE,
1051
                        NULL, NULL, NULL, NULL);
1052
    if(ret != 0)
1053
        av_freep(s);
1054
    return ret;
1055
}
1056

    
1057
int url_close_buf(AVIOContext *s)
1058
{
1059
    avio_flush(s);
1060
    return s->buf_ptr - s->buffer;
1061
}
1062
#endif
1063

    
1064
/* output in a dynamic buffer */
1065

    
1066
typedef struct DynBuffer {
1067
    int pos, size, allocated_size;
1068
    uint8_t *buffer;
1069
    int io_buffer_size;
1070
    uint8_t io_buffer[1];
1071
} DynBuffer;
1072

    
1073
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1074
{
1075
    DynBuffer *d = opaque;
1076
    unsigned new_size, new_allocated_size;
1077

    
1078
    /* reallocate buffer if needed */
1079
    new_size = d->pos + buf_size;
1080
    new_allocated_size = d->allocated_size;
1081
    if(new_size < d->pos || new_size > INT_MAX/2)
1082
        return -1;
1083
    while (new_size > new_allocated_size) {
1084
        if (!new_allocated_size)
1085
            new_allocated_size = new_size;
1086
        else
1087
            new_allocated_size += new_allocated_size / 2 + 1;
1088
    }
1089

    
1090
    if (new_allocated_size > d->allocated_size) {
1091
        d->buffer = av_realloc(d->buffer, new_allocated_size);
1092
        if(d->buffer == NULL)
1093
             return AVERROR(ENOMEM);
1094
        d->allocated_size = new_allocated_size;
1095
    }
1096
    memcpy(d->buffer + d->pos, buf, buf_size);
1097
    d->pos = new_size;
1098
    if (d->pos > d->size)
1099
        d->size = d->pos;
1100
    return buf_size;
1101
}
1102

    
1103
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1104
{
1105
    unsigned char buf1[4];
1106
    int ret;
1107

    
1108
    /* packetized write: output the header */
1109
    AV_WB32(buf1, buf_size);
1110
    ret= dyn_buf_write(opaque, buf1, 4);
1111
    if(ret < 0)
1112
        return ret;
1113

    
1114
    /* then the data */
1115
    return dyn_buf_write(opaque, buf, buf_size);
1116
}
1117

    
1118
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1119
{
1120
    DynBuffer *d = opaque;
1121

    
1122
    if (whence == SEEK_CUR)
1123
        offset += d->pos;
1124
    else if (whence == SEEK_END)
1125
        offset += d->size;
1126
    if (offset < 0 || offset > 0x7fffffffLL)
1127
        return -1;
1128
    d->pos = offset;
1129
    return 0;
1130
}
1131

    
1132
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1133
{
1134
    DynBuffer *d;
1135
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1136

    
1137
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1138
        return -1;
1139
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1140
    if (!d)
1141
        return AVERROR(ENOMEM);
1142
    d->io_buffer_size = io_buffer_size;
1143
    *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1144
                            max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1145
                            max_packet_size ? NULL : dyn_buf_seek);
1146
    if(!*s) {
1147
        av_free(d);
1148
        return AVERROR(ENOMEM);
1149
    }
1150
    (*s)->max_packet_size = max_packet_size;
1151
    return 0;
1152
}
1153

    
1154
int avio_open_dyn_buf(AVIOContext **s)
1155
{
1156
    return url_open_dyn_buf_internal(s, 0);
1157
}
1158

    
1159
int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1160
{
1161
    if (max_packet_size <= 0)
1162
        return -1;
1163
    return url_open_dyn_buf_internal(s, max_packet_size);
1164
}
1165

    
1166
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1167
{
1168
    DynBuffer *d = s->opaque;
1169
    int size;
1170
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1171
    int padding = 0;
1172

    
1173
    /* don't attempt to pad fixed-size packet buffers */
1174
    if (!s->max_packet_size) {
1175
        avio_write(s, padbuf, sizeof(padbuf));
1176
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1177
    }
1178

    
1179
    avio_flush(s);
1180

    
1181
    *pbuffer = d->buffer;
1182
    size = d->size;
1183
    av_free(d);
1184
    av_free(s);
1185
    return size - padding;
1186
}