Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 59f65d95

History | View | Annotate | Download (27 KB)

1
/*
2
 * Buffered I/O for ffmpeg system
3
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "libavutil/crc.h"
23
#include "libavutil/intreadwrite.h"
24
#include "avformat.h"
25
#include "avio.h"
26
#include "avio_internal.h"
27
#include "internal.h"
28
#include <stdarg.h>
29

    
30
#define IO_BUFFER_SIZE 32768
31

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

    
39
static void fill_buffer(AVIOContext *s);
40
#if !FF_API_URL_RESETBUF
41
static int url_resetbuf(AVIOContext *s, int flags);
42
#endif
43

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

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

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

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

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

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

    
151
        if (s->buf_ptr >= s->buf_end)
152
            flush_buffer(s);
153

    
154
        count -= len;
155
    }
156
}
157

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

    
165
        if (s->buf_ptr >= s->buf_end)
166
            flush_buffer(s);
167

    
168
        buf += len;
169
        size -= len;
170
    }
171
}
172

    
173
void put_flush_packet(AVIOContext *s)
174
{
175
    flush_buffer(s);
176
    s->must_flush = 0;
177
}
178

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

    
186
    if(!s)
187
        return AVERROR(EINVAL);
188

    
189
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
190

    
191
    if (whence != SEEK_CUR && whence != SEEK_SET)
192
        return AVERROR(EINVAL);
193

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

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

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

    
243
int64_t url_ftell(AVIOContext *s)
244
{
245
    return avio_seek(s, 0, SEEK_CUR);
246
}
247
#endif
248

    
249
int64_t avio_size(AVIOContext *s)
250
{
251
    int64_t size;
252

    
253
    if(!s)
254
        return AVERROR(EINVAL);
255

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

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

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

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

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

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

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

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

    
321
#undef GET
322

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

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

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

    
364
int url_fopen(AVIOContext **s, const char *filename, int flags)
365
{
366
    return avio_open(s, filename, flags);
367
}
368
int url_fclose(AVIOContext *s)
369
{
370
    return avio_close(s);
371
}
372
int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
373
{
374
    return avio_seek(s, offset, whence);
375
}
376
int64_t url_fsize(AVIOContext *s)
377
{
378
    return avio_size(s);
379
}
380
int url_setbufsize(AVIOContext *s, int buf_size)
381
{
382
    return ffio_set_buf_size(s, buf_size);
383
}
384
#endif
385

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

    
397
int avio_put_str16le(AVIOContext *s, const char *str)
398
{
399
    const uint8_t *q = str;
400
    int ret = 0;
401

    
402
    while (*q) {
403
        uint32_t ch;
404
        uint16_t tmp;
405

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

    
414
int ff_get_v_length(uint64_t val){
415
    int i=1;
416

    
417
    while(val>>=7)
418
        i++;
419

    
420
    return i;
421
}
422

    
423
void ff_put_v(AVIOContext *bc, uint64_t val){
424
    int i= ff_get_v_length(val);
425

    
426
    while(--i>0)
427
        avio_w8(bc, 128 | (val>>(7*i)));
428

    
429
    avio_w8(bc, val&127);
430
}
431

    
432
void avio_wl64(AVIOContext *s, uint64_t val)
433
{
434
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
435
    avio_wl32(s, (uint32_t)(val >> 32));
436
}
437

    
438
void avio_wb64(AVIOContext *s, uint64_t val)
439
{
440
    avio_wb32(s, (uint32_t)(val >> 32));
441
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
442
}
443

    
444
void avio_wl16(AVIOContext *s, unsigned int val)
445
{
446
    avio_w8(s, val);
447
    avio_w8(s, val >> 8);
448
}
449

    
450
void avio_wb16(AVIOContext *s, unsigned int val)
451
{
452
    avio_w8(s, val >> 8);
453
    avio_w8(s, val);
454
}
455

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

    
462
void avio_wb24(AVIOContext *s, unsigned int val)
463
{
464
    avio_wb16(s, val >> 8);
465
    avio_w8(s, val);
466
}
467

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

    
477
/* Input stream */
478

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

    
485
    /* no need to do anything if EOF already reached */
486
    if (s->eof_reached)
487
        return;
488

    
489
    if(s->update_checksum && dst == s->buffer){
490
        if(s->buf_end > s->checksum_ptr)
491
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
492
        s->checksum_ptr= s->buffer;
493
    }
494

    
495
    /* make buffer smaller in case it ended up large after probing */
496
    if (s->buffer_size > max_buffer_size) {
497
        ffio_set_buf_size(s, max_buffer_size);
498

    
499
        s->checksum_ptr = dst = s->buffer;
500
        len = s->buffer_size;
501
    }
502

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

    
520
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
521
                                    unsigned int len)
522
{
523
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
524
}
525

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

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

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

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

    
565
int avio_read(AVIOContext *s, unsigned char *buf, int size)
566
{
567
    int len, size1;
568

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

    
612
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
613
{
614
    int len;
615

    
616
    if(size<0)
617
        return -1;
618

    
619
    len = s->buf_end - s->buf_ptr;
620
    if (len == 0) {
621
        fill_buffer(s);
622
        len = s->buf_end - s->buf_ptr;
623
    }
624
    if (len > size)
625
        len = size;
626
    memcpy(buf, s->buf_ptr, len);
627
    s->buf_ptr += len;
628
    if (!len) {
629
        if (s->error)         return s->error;
630
        if (s->eof_reached)   return AVERROR_EOF;
631
    }
632
    return len;
633
}
634

    
635
unsigned int avio_rl16(AVIOContext *s)
636
{
637
    unsigned int val;
638
    val = avio_r8(s);
639
    val |= avio_r8(s) << 8;
640
    return val;
641
}
642

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

    
651
unsigned int avio_rl32(AVIOContext *s)
652
{
653
    unsigned int val;
654
    val = avio_rl16(s);
655
    val |= avio_rl16(s) << 16;
656
    return val;
657
}
658

    
659
uint64_t avio_rl64(AVIOContext *s)
660
{
661
    uint64_t val;
662
    val = (uint64_t)avio_rl32(s);
663
    val |= (uint64_t)avio_rl32(s) << 32;
664
    return val;
665
}
666

    
667
unsigned int avio_rb16(AVIOContext *s)
668
{
669
    unsigned int val;
670
    val = avio_r8(s) << 8;
671
    val |= avio_r8(s);
672
    return val;
673
}
674

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

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

    
698
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
699
{
700
    int i = 0;
701
    char c;
702

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

    
709
    buf[i] = 0;
710
    return i;
711
}
712

    
713
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
714
{
715
    int i;
716

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

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

    
747
GET_STR16(le, avio_rl16)
748
GET_STR16(be, avio_rb16)
749

    
750
#undef GET_STR16
751

    
752
uint64_t avio_rb64(AVIOContext *s)
753
{
754
    uint64_t val;
755
    val = (uint64_t)avio_rb32(s) << 32;
756
    val |= (uint64_t)avio_rb32(s);
757
    return val;
758
}
759

    
760
uint64_t ffio_read_varlen(AVIOContext *bc){
761
    uint64_t val = 0;
762
    int tmp;
763

    
764
    do{
765
        tmp = avio_r8(bc);
766
        val= (val<<7) + (tmp&127);
767
    }while(tmp&128);
768
    return val;
769
}
770

    
771
int url_fdopen(AVIOContext **s, URLContext *h)
772
{
773
    uint8_t *buffer;
774
    int buffer_size, max_packet_size;
775

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

    
786
    *s = av_mallocz(sizeof(AVIOContext));
787
    if(!*s) {
788
        av_free(buffer);
789
        return AVERROR(ENOMEM);
790
    }
791

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

    
808
int ffio_set_buf_size(AVIOContext *s, int buf_size)
809
{
810
    uint8_t *buffer;
811
    buffer = av_malloc(buf_size);
812
    if (!buffer)
813
        return AVERROR(ENOMEM);
814

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

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

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

    
846
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
847
{
848
    int64_t buffer_start;
849
    int buffer_size;
850
    int overlap, new_size, alloc_size;
851

    
852
    if (s->write_flag)
853
        return AVERROR(EINVAL);
854

    
855
    buffer_size = s->buf_end - s->buffer;
856

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

    
861
    overlap = buf_size - buffer_start;
862
    new_size = buf_size + buffer_size - overlap;
863

    
864
    alloc_size = FFMAX(s->buffer_size, new_size);
865
    if (alloc_size > buf_size)
866
        if (!(buf = av_realloc(buf, alloc_size)))
867
            return AVERROR(ENOMEM);
868

    
869
    if (new_size > buf_size) {
870
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
871
        buf_size = new_size;
872
    }
873

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

    
882
    return 0;
883
}
884

    
885
int avio_open(AVIOContext **s, const char *filename, int flags)
886
{
887
    URLContext *h;
888
    int err;
889

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

    
901
int avio_close(AVIOContext *s)
902
{
903
    URLContext *h = s->opaque;
904

    
905
    av_free(s->buffer);
906
    av_free(s);
907
    return url_close(h);
908
}
909

    
910
URLContext *url_fileno(AVIOContext *s)
911
{
912
    return s->opaque;
913
}
914

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

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

    
930
#if FF_API_OLD_AVIO
931
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
932
{
933
    int c;
934
    char *q;
935

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

    
952
int url_fget_max_packet_size(AVIOContext *s)
953
{
954
    return s->max_packet_size;
955
}
956
#endif
957

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

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

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

    
1003
int url_close_buf(AVIOContext *s)
1004
{
1005
    put_flush_packet(s);
1006
    return s->buf_ptr - s->buffer;
1007
}
1008

    
1009
/* output in a dynamic buffer */
1010

    
1011
typedef struct DynBuffer {
1012
    int pos, size, allocated_size;
1013
    uint8_t *buffer;
1014
    int io_buffer_size;
1015
    uint8_t io_buffer[1];
1016
} DynBuffer;
1017

    
1018
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1019
{
1020
    DynBuffer *d = opaque;
1021
    unsigned new_size, new_allocated_size;
1022

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

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

    
1048
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1049
{
1050
    unsigned char buf1[4];
1051
    int ret;
1052

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

    
1059
    /* then the data */
1060
    return dyn_buf_write(opaque, buf, buf_size);
1061
}
1062

    
1063
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1064
{
1065
    DynBuffer *d = opaque;
1066

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

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

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

    
1107
int url_open_dyn_buf(AVIOContext **s)
1108
{
1109
    return url_open_dyn_buf_internal(s, 0);
1110
}
1111

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

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

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

    
1132
    put_flush_packet(s);
1133

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