Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 668438a3

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 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 "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
#if !FF_API_URL_RESETBUF
42
static int url_resetbuf(AVIOContext *s, int flags);
43
#endif
44

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

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

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

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

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

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

    
155
        if (s->buf_ptr >= s->buf_end)
156
            flush_buffer(s);
157

    
158
        count -= len;
159
    }
160
}
161

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

    
169
        if (s->buf_ptr >= s->buf_end)
170
            flush_buffer(s);
171

    
172
        buf += len;
173
        size -= len;
174
    }
175
}
176

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

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

    
190
    if(!s)
191
        return AVERROR(EINVAL);
192

    
193
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
194

    
195
    if (whence != SEEK_CUR && whence != SEEK_SET)
196
        return AVERROR(EINVAL);
197

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

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

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

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

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

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

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

    
272
#if FF_API_OLD_AVIO
273
int url_feof(AVIOContext *s)
274
{
275
    if(!s)
276
        return 0;
277
    return s->eof_reached;
278
}
279

    
280
int url_ferror(AVIOContext *s)
281
{
282
    if(!s)
283
        return 0;
284
    return s->error;
285
}
286
#endif
287

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

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

    
304
#if FF_API_OLD_AVIO
305
void put_strz(AVIOContext *s, const char *str)
306
{
307
    avio_put_str(s, str);
308
}
309

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

    
320
GET(16, unsigned int)
321
GET(24, unsigned int)
322
GET(32, unsigned int)
323
GET(64, uint64_t)
324

    
325
#undef GET
326

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

    
337
PUT(16, unsigned int)
338
PUT(24, unsigned int)
339
PUT(32, unsigned int)
340
PUT(64, uint64_t)
341
#undef PUT
342

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

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

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

    
441
int avio_put_str(AVIOContext *s, const char *str)
442
{
443
    int len = 1;
444
    if (str) {
445
        len += strlen(str);
446
        avio_write(s, (const unsigned char *) str, len);
447
    } else
448
        avio_w8(s, 0);
449
    return len;
450
}
451

    
452
int avio_put_str16le(AVIOContext *s, const char *str)
453
{
454
    const uint8_t *q = str;
455
    int ret = 0;
456

    
457
    while (*q) {
458
        uint32_t ch;
459
        uint16_t tmp;
460

    
461
        GET_UTF8(ch, *q++, break;)
462
        PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
463
    }
464
    avio_wl16(s, 0);
465
    ret += 2;
466
    return ret;
467
}
468

    
469
int ff_get_v_length(uint64_t val){
470
    int i=1;
471

    
472
    while(val>>=7)
473
        i++;
474

    
475
    return i;
476
}
477

    
478
void ff_put_v(AVIOContext *bc, uint64_t val){
479
    int i= ff_get_v_length(val);
480

    
481
    while(--i>0)
482
        avio_w8(bc, 128 | (val>>(7*i)));
483

    
484
    avio_w8(bc, val&127);
485
}
486

    
487
void avio_wl64(AVIOContext *s, uint64_t val)
488
{
489
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
490
    avio_wl32(s, (uint32_t)(val >> 32));
491
}
492

    
493
void avio_wb64(AVIOContext *s, uint64_t val)
494
{
495
    avio_wb32(s, (uint32_t)(val >> 32));
496
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
497
}
498

    
499
void avio_wl16(AVIOContext *s, unsigned int val)
500
{
501
    avio_w8(s, val);
502
    avio_w8(s, val >> 8);
503
}
504

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

    
511
void avio_wl24(AVIOContext *s, unsigned int val)
512
{
513
    avio_wl16(s, val & 0xffff);
514
    avio_w8(s, val >> 16);
515
}
516

    
517
void avio_wb24(AVIOContext *s, unsigned int val)
518
{
519
    avio_wb16(s, val >> 8);
520
    avio_w8(s, val);
521
}
522

    
523
#if FF_API_OLD_AVIO
524
void put_tag(AVIOContext *s, const char *tag)
525
{
526
    while (*tag) {
527
        avio_w8(s, *tag++);
528
    }
529
}
530
#endif
531

    
532
/* Input stream */
533

    
534
static void fill_buffer(AVIOContext *s)
535
{
536
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
537
    int len= s->buffer_size - (dst - s->buffer);
538
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
539

    
540
    /* no need to do anything if EOF already reached */
541
    if (s->eof_reached)
542
        return;
543

    
544
    if(s->update_checksum && dst == s->buffer){
545
        if(s->buf_end > s->checksum_ptr)
546
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
547
        s->checksum_ptr= s->buffer;
548
    }
549

    
550
    /* make buffer smaller in case it ended up large after probing */
551
    if (s->buffer_size > max_buffer_size) {
552
        ffio_set_buf_size(s, max_buffer_size);
553

    
554
        s->checksum_ptr = dst = s->buffer;
555
        len = s->buffer_size;
556
    }
557

    
558
    if(s->read_packet)
559
        len = s->read_packet(s->opaque, dst, len);
560
    else
561
        len = 0;
562
    if (len <= 0) {
563
        /* do not modify buffer if EOF reached so that a seek back can
564
           be done without rereading data */
565
        s->eof_reached = 1;
566
        if(len<0)
567
            s->error= len;
568
    } else {
569
        s->pos += len;
570
        s->buf_ptr = dst;
571
        s->buf_end = dst + len;
572
    }
573
}
574

    
575
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
576
                                    unsigned int len)
577
{
578
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
579
}
580

    
581
unsigned long ffio_get_checksum(AVIOContext *s)
582
{
583
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
584
    s->update_checksum= NULL;
585
    return s->checksum;
586
}
587

    
588
void ffio_init_checksum(AVIOContext *s,
589
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
590
                   unsigned long checksum)
591
{
592
    s->update_checksum= update_checksum;
593
    if(s->update_checksum){
594
        s->checksum= checksum;
595
        s->checksum_ptr= s->buf_ptr;
596
    }
597
}
598

    
599
/* XXX: put an inline version */
600
int avio_r8(AVIOContext *s)
601
{
602
    if (s->buf_ptr >= s->buf_end)
603
        fill_buffer(s);
604
    if (s->buf_ptr < s->buf_end)
605
        return *s->buf_ptr++;
606
    return 0;
607
}
608

    
609
#if FF_API_OLD_AVIO
610
int url_fgetc(AVIOContext *s)
611
{
612
    if (s->buf_ptr >= s->buf_end)
613
        fill_buffer(s);
614
    if (s->buf_ptr < s->buf_end)
615
        return *s->buf_ptr++;
616
    return URL_EOF;
617
}
618
#endif
619

    
620
int avio_read(AVIOContext *s, unsigned char *buf, int size)
621
{
622
    int len, size1;
623

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

    
667
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
668
{
669
    int len;
670

    
671
    if(size<0)
672
        return -1;
673

    
674
    len = s->buf_end - s->buf_ptr;
675
    if (len == 0) {
676
        fill_buffer(s);
677
        len = s->buf_end - s->buf_ptr;
678
    }
679
    if (len > size)
680
        len = size;
681
    memcpy(buf, s->buf_ptr, len);
682
    s->buf_ptr += len;
683
    if (!len) {
684
        if (s->error)         return s->error;
685
        if (s->eof_reached)   return AVERROR_EOF;
686
    }
687
    return len;
688
}
689

    
690
unsigned int avio_rl16(AVIOContext *s)
691
{
692
    unsigned int val;
693
    val = avio_r8(s);
694
    val |= avio_r8(s) << 8;
695
    return val;
696
}
697

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

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

    
714
uint64_t avio_rl64(AVIOContext *s)
715
{
716
    uint64_t val;
717
    val = (uint64_t)avio_rl32(s);
718
    val |= (uint64_t)avio_rl32(s) << 32;
719
    return val;
720
}
721

    
722
unsigned int avio_rb16(AVIOContext *s)
723
{
724
    unsigned int val;
725
    val = avio_r8(s) << 8;
726
    val |= avio_r8(s);
727
    return val;
728
}
729

    
730
unsigned int avio_rb24(AVIOContext *s)
731
{
732
    unsigned int val;
733
    val = avio_rb16(s) << 8;
734
    val |= avio_r8(s);
735
    return val;
736
}
737
unsigned int avio_rb32(AVIOContext *s)
738
{
739
    unsigned int val;
740
    val = avio_rb16(s) << 16;
741
    val |= avio_rb16(s);
742
    return val;
743
}
744

    
745
#if FF_API_OLD_AVIO
746
char *get_strz(AVIOContext *s, char *buf, int maxlen)
747
{
748
    avio_get_str(s, INT_MAX, buf, maxlen);
749
    return buf;
750
}
751
#endif
752

    
753
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
754
{
755
    int i = 0;
756
    char c;
757

    
758
    do {
759
        c = avio_r8(s);
760
        if (c && i < maxlen-1)
761
            buf[i++] = c;
762
    } while (c != '\n' && c);
763

    
764
    buf[i] = 0;
765
    return i;
766
}
767

    
768
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
769
{
770
    int i;
771

    
772
    // reserve 1 byte for terminating 0
773
    buflen = FFMIN(buflen - 1, maxlen);
774
    for (i = 0; i < buflen; i++)
775
        if (!(buf[i] = avio_r8(s)))
776
            return i + 1;
777
    if (buflen)
778
        buf[i] = 0;
779
    for (; i < maxlen; i++)
780
        if (!avio_r8(s))
781
            return i + 1;
782
    return maxlen;
783
}
784

    
785
#define GET_STR16(type, read) \
786
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
787
{\
788
    char* q = buf;\
789
    int ret = 0;\
790
    while (ret + 1 < maxlen) {\
791
        uint8_t tmp;\
792
        uint32_t ch;\
793
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
794
        if (!ch)\
795
            break;\
796
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
797
    }\
798
    *q = 0;\
799
    return ret;\
800
}\
801

    
802
GET_STR16(le, avio_rl16)
803
GET_STR16(be, avio_rb16)
804

    
805
#undef GET_STR16
806

    
807
uint64_t avio_rb64(AVIOContext *s)
808
{
809
    uint64_t val;
810
    val = (uint64_t)avio_rb32(s) << 32;
811
    val |= (uint64_t)avio_rb32(s);
812
    return val;
813
}
814

    
815
uint64_t ffio_read_varlen(AVIOContext *bc){
816
    uint64_t val = 0;
817
    int tmp;
818

    
819
    do{
820
        tmp = avio_r8(bc);
821
        val= (val<<7) + (tmp&127);
822
    }while(tmp&128);
823
    return val;
824
}
825

    
826
int ffio_fdopen(AVIOContext **s, URLContext *h)
827
{
828
    uint8_t *buffer;
829
    int buffer_size, max_packet_size;
830

    
831
    max_packet_size = h->max_packet_size;
832
    if (max_packet_size) {
833
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
834
    } else {
835
        buffer_size = IO_BUFFER_SIZE;
836
    }
837
    buffer = av_malloc(buffer_size);
838
    if (!buffer)
839
        return AVERROR(ENOMEM);
840

    
841
    *s = av_mallocz(sizeof(AVIOContext));
842
    if(!*s) {
843
        av_free(buffer);
844
        return AVERROR(ENOMEM);
845
    }
846

    
847
    if (ffio_init_context(*s, buffer, buffer_size,
848
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
849
                      ffurl_read, ffurl_write, ffurl_seek) < 0) {
850
        av_free(buffer);
851
        av_freep(s);
852
        return AVERROR(EIO);
853
    }
854
#if FF_API_OLD_AVIO
855
    (*s)->is_streamed = h->is_streamed;
856
#endif
857
    (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
858
    (*s)->max_packet_size = max_packet_size;
859
    if(h->prot) {
860
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
861
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
862
    }
863
    return 0;
864
}
865

    
866
int ffio_set_buf_size(AVIOContext *s, int buf_size)
867
{
868
    uint8_t *buffer;
869
    buffer = av_malloc(buf_size);
870
    if (!buffer)
871
        return AVERROR(ENOMEM);
872

    
873
    av_free(s->buffer);
874
    s->buffer = buffer;
875
    s->buffer_size = buf_size;
876
    s->buf_ptr = buffer;
877
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
878
    return 0;
879
}
880

    
881
#if FF_API_URL_RESETBUF
882
int url_resetbuf(AVIOContext *s, int flags)
883
#else
884
static int url_resetbuf(AVIOContext *s, int flags)
885
#endif
886
{
887
#if FF_API_URL_RESETBUF
888
    if (flags & URL_RDWR)
889
        return AVERROR(EINVAL);
890
#else
891
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
892
#endif
893

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

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

    
910
    if (s->write_flag)
911
        return AVERROR(EINVAL);
912

    
913
    buffer_size = s->buf_end - s->buffer;
914

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

    
919
    overlap = buf_size - buffer_start;
920
    new_size = buf_size + buffer_size - overlap;
921

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

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

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

    
940
    return 0;
941
}
942

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

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

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

    
963
    av_free(s->buffer);
964
    av_free(s);
965
    return ffurl_close(h);
966
}
967

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

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

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

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

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

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

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

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

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

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

    
1066
/* output in a dynamic buffer */
1067

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1181
    avio_flush(s);
1182

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