Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 8bea72f7

History | View | Annotate | Download (28.2 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
#if FF_API_OLD_AVIO
66
    s->is_streamed = 0;
67
#endif
68
    s->seekable = AVIO_SEEKABLE_NORMAL;
69
    s->max_packet_size = 0;
70
    s->update_checksum= NULL;
71
    if(!read_packet && !write_flag){
72
        s->pos = buffer_size;
73
        s->buf_end = s->buffer + buffer_size;
74
    }
75
    s->read_pause = NULL;
76
    s->read_seek  = NULL;
77
    return 0;
78
}
79

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

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

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

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

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

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

    
157
        count -= len;
158
    }
159
}
160

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
324
#undef GET
325

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

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

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

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

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

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

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

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

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

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

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

    
474
    return i;
475
}
476

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

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

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

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

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

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

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

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

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

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

    
531
/* Input stream */
532

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
804
#undef GET_STR16
805

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
939
    return 0;
940
}
941

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

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

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

    
962
    av_free(s->buffer);
963
    av_free(s);
964
    return url_close(h);
965
}
966

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

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

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

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

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

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

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

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

    
1042
/* avio_open_dyn_buf and avio_close_dyn_buf are used in rtp.c to send a response
1043
 * back to the server even if CONFIG_MUXERS is false. */
1044
#if CONFIG_MUXERS || CONFIG_NETWORK
1045
/* buffer handling */
1046
#if FF_API_OLD_AVIO
1047
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
1048
{
1049
    int ret;
1050
    *s = av_mallocz(sizeof(AVIOContext));
1051
    if(!*s)
1052
        return AVERROR(ENOMEM);
1053
    ret = ffio_init_context(*s, buf, buf_size,
1054
                        (flags & URL_WRONLY || flags & URL_RDWR),
1055
                        NULL, NULL, NULL, NULL);
1056
    if(ret != 0)
1057
        av_freep(s);
1058
    return ret;
1059
}
1060

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

    
1068
/* output in a dynamic buffer */
1069

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1183
    avio_flush(s);
1184

    
1185
    *pbuffer = d->buffer;
1186
    size = d->size;
1187
    av_free(d);
1188
    av_free(s);
1189
    return size - padding;
1190
}
1191
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */