Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ d9d86e00

History | View | Annotate | Download (27.2 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
int url_fprintf(AVIOContext *s, const char *fmt, ...)
385
{
386
    va_list ap;
387
    char buf[4096];
388
    int ret;
389

    
390
    va_start(ap, fmt);
391
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
392
    va_end(ap);
393
    avio_write(s, buf, strlen(buf));
394
    return ret;
395
}
396
#endif
397

    
398
int avio_put_str(AVIOContext *s, const char *str)
399
{
400
    int len = 1;
401
    if (str) {
402
        len += strlen(str);
403
        avio_write(s, (const unsigned char *) str, len);
404
    } else
405
        avio_w8(s, 0);
406
    return len;
407
}
408

    
409
int avio_put_str16le(AVIOContext *s, const char *str)
410
{
411
    const uint8_t *q = str;
412
    int ret = 0;
413

    
414
    while (*q) {
415
        uint32_t ch;
416
        uint16_t tmp;
417

    
418
        GET_UTF8(ch, *q++, break;)
419
        PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
420
    }
421
    avio_wl16(s, 0);
422
    ret += 2;
423
    return ret;
424
}
425

    
426
int ff_get_v_length(uint64_t val){
427
    int i=1;
428

    
429
    while(val>>=7)
430
        i++;
431

    
432
    return i;
433
}
434

    
435
void ff_put_v(AVIOContext *bc, uint64_t val){
436
    int i= ff_get_v_length(val);
437

    
438
    while(--i>0)
439
        avio_w8(bc, 128 | (val>>(7*i)));
440

    
441
    avio_w8(bc, val&127);
442
}
443

    
444
void avio_wl64(AVIOContext *s, uint64_t val)
445
{
446
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
447
    avio_wl32(s, (uint32_t)(val >> 32));
448
}
449

    
450
void avio_wb64(AVIOContext *s, uint64_t val)
451
{
452
    avio_wb32(s, (uint32_t)(val >> 32));
453
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
454
}
455

    
456
void avio_wl16(AVIOContext *s, unsigned int val)
457
{
458
    avio_w8(s, val);
459
    avio_w8(s, val >> 8);
460
}
461

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

    
468
void avio_wl24(AVIOContext *s, unsigned int val)
469
{
470
    avio_wl16(s, val & 0xffff);
471
    avio_w8(s, val >> 16);
472
}
473

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

    
480
#if FF_API_OLD_AVIO
481
void put_tag(AVIOContext *s, const char *tag)
482
{
483
    while (*tag) {
484
        avio_w8(s, *tag++);
485
    }
486
}
487
#endif
488

    
489
/* Input stream */
490

    
491
static void fill_buffer(AVIOContext *s)
492
{
493
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
494
    int len= s->buffer_size - (dst - s->buffer);
495
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
496

    
497
    /* no need to do anything if EOF already reached */
498
    if (s->eof_reached)
499
        return;
500

    
501
    if(s->update_checksum && dst == s->buffer){
502
        if(s->buf_end > s->checksum_ptr)
503
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
504
        s->checksum_ptr= s->buffer;
505
    }
506

    
507
    /* make buffer smaller in case it ended up large after probing */
508
    if (s->buffer_size > max_buffer_size) {
509
        ffio_set_buf_size(s, max_buffer_size);
510

    
511
        s->checksum_ptr = dst = s->buffer;
512
        len = s->buffer_size;
513
    }
514

    
515
    if(s->read_packet)
516
        len = s->read_packet(s->opaque, dst, len);
517
    else
518
        len = 0;
519
    if (len <= 0) {
520
        /* do not modify buffer if EOF reached so that a seek back can
521
           be done without rereading data */
522
        s->eof_reached = 1;
523
        if(len<0)
524
            s->error= len;
525
    } else {
526
        s->pos += len;
527
        s->buf_ptr = dst;
528
        s->buf_end = dst + len;
529
    }
530
}
531

    
532
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
533
                                    unsigned int len)
534
{
535
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
536
}
537

    
538
unsigned long get_checksum(AVIOContext *s)
539
{
540
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
541
    s->update_checksum= NULL;
542
    return s->checksum;
543
}
544

    
545
void init_checksum(AVIOContext *s,
546
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
547
                   unsigned long checksum)
548
{
549
    s->update_checksum= update_checksum;
550
    if(s->update_checksum){
551
        s->checksum= checksum;
552
        s->checksum_ptr= s->buf_ptr;
553
    }
554
}
555

    
556
/* XXX: put an inline version */
557
int avio_r8(AVIOContext *s)
558
{
559
    if (s->buf_ptr >= s->buf_end)
560
        fill_buffer(s);
561
    if (s->buf_ptr < s->buf_end)
562
        return *s->buf_ptr++;
563
    return 0;
564
}
565

    
566
#if FF_API_OLD_AVIO
567
int url_fgetc(AVIOContext *s)
568
{
569
    if (s->buf_ptr >= s->buf_end)
570
        fill_buffer(s);
571
    if (s->buf_ptr < s->buf_end)
572
        return *s->buf_ptr++;
573
    return URL_EOF;
574
}
575
#endif
576

    
577
int avio_read(AVIOContext *s, unsigned char *buf, int size)
578
{
579
    int len, size1;
580

    
581
    size1 = size;
582
    while (size > 0) {
583
        len = s->buf_end - s->buf_ptr;
584
        if (len > size)
585
            len = size;
586
        if (len == 0) {
587
            if(size > s->buffer_size && !s->update_checksum){
588
                if(s->read_packet)
589
                    len = s->read_packet(s->opaque, buf, size);
590
                if (len <= 0) {
591
                    /* do not modify buffer if EOF reached so that a seek back can
592
                    be done without rereading data */
593
                    s->eof_reached = 1;
594
                    if(len<0)
595
                        s->error= len;
596
                    break;
597
                } else {
598
                    s->pos += len;
599
                    size -= len;
600
                    buf += len;
601
                    s->buf_ptr = s->buffer;
602
                    s->buf_end = s->buffer/* + len*/;
603
                }
604
            }else{
605
                fill_buffer(s);
606
                len = s->buf_end - s->buf_ptr;
607
                if (len == 0)
608
                    break;
609
            }
610
        } else {
611
            memcpy(buf, s->buf_ptr, len);
612
            buf += len;
613
            s->buf_ptr += len;
614
            size -= len;
615
        }
616
    }
617
    if (size1 == size) {
618
        if (s->error)         return s->error;
619
        if (s->eof_reached)   return AVERROR_EOF;
620
    }
621
    return size1 - size;
622
}
623

    
624
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
625
{
626
    int len;
627

    
628
    if(size<0)
629
        return -1;
630

    
631
    len = s->buf_end - s->buf_ptr;
632
    if (len == 0) {
633
        fill_buffer(s);
634
        len = s->buf_end - s->buf_ptr;
635
    }
636
    if (len > size)
637
        len = size;
638
    memcpy(buf, s->buf_ptr, len);
639
    s->buf_ptr += len;
640
    if (!len) {
641
        if (s->error)         return s->error;
642
        if (s->eof_reached)   return AVERROR_EOF;
643
    }
644
    return len;
645
}
646

    
647
unsigned int avio_rl16(AVIOContext *s)
648
{
649
    unsigned int val;
650
    val = avio_r8(s);
651
    val |= avio_r8(s) << 8;
652
    return val;
653
}
654

    
655
unsigned int avio_rl24(AVIOContext *s)
656
{
657
    unsigned int val;
658
    val = avio_rl16(s);
659
    val |= avio_r8(s) << 16;
660
    return val;
661
}
662

    
663
unsigned int avio_rl32(AVIOContext *s)
664
{
665
    unsigned int val;
666
    val = avio_rl16(s);
667
    val |= avio_rl16(s) << 16;
668
    return val;
669
}
670

    
671
uint64_t avio_rl64(AVIOContext *s)
672
{
673
    uint64_t val;
674
    val = (uint64_t)avio_rl32(s);
675
    val |= (uint64_t)avio_rl32(s) << 32;
676
    return val;
677
}
678

    
679
unsigned int avio_rb16(AVIOContext *s)
680
{
681
    unsigned int val;
682
    val = avio_r8(s) << 8;
683
    val |= avio_r8(s);
684
    return val;
685
}
686

    
687
unsigned int avio_rb24(AVIOContext *s)
688
{
689
    unsigned int val;
690
    val = avio_rb16(s) << 8;
691
    val |= avio_r8(s);
692
    return val;
693
}
694
unsigned int avio_rb32(AVIOContext *s)
695
{
696
    unsigned int val;
697
    val = avio_rb16(s) << 16;
698
    val |= avio_rb16(s);
699
    return val;
700
}
701

    
702
#if FF_API_OLD_AVIO
703
char *get_strz(AVIOContext *s, char *buf, int maxlen)
704
{
705
    avio_get_str(s, INT_MAX, buf, maxlen);
706
    return buf;
707
}
708
#endif
709

    
710
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
711
{
712
    int i = 0;
713
    char c;
714

    
715
    do {
716
        c = avio_r8(s);
717
        if (c && i < maxlen-1)
718
            buf[i++] = c;
719
    } while (c != '\n' && c);
720

    
721
    buf[i] = 0;
722
    return i;
723
}
724

    
725
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
726
{
727
    int i;
728

    
729
    // reserve 1 byte for terminating 0
730
    buflen = FFMIN(buflen - 1, maxlen);
731
    for (i = 0; i < buflen; i++)
732
        if (!(buf[i] = avio_r8(s)))
733
            return i + 1;
734
    if (buflen)
735
        buf[i] = 0;
736
    for (; i < maxlen; i++)
737
        if (!avio_r8(s))
738
            return i + 1;
739
    return maxlen;
740
}
741

    
742
#define GET_STR16(type, read) \
743
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
744
{\
745
    char* q = buf;\
746
    int ret = 0;\
747
    while (ret + 1 < maxlen) {\
748
        uint8_t tmp;\
749
        uint32_t ch;\
750
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
751
        if (!ch)\
752
            break;\
753
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
754
    }\
755
    *q = 0;\
756
    return ret;\
757
}\
758

    
759
GET_STR16(le, avio_rl16)
760
GET_STR16(be, avio_rb16)
761

    
762
#undef GET_STR16
763

    
764
uint64_t avio_rb64(AVIOContext *s)
765
{
766
    uint64_t val;
767
    val = (uint64_t)avio_rb32(s) << 32;
768
    val |= (uint64_t)avio_rb32(s);
769
    return val;
770
}
771

    
772
uint64_t ffio_read_varlen(AVIOContext *bc){
773
    uint64_t val = 0;
774
    int tmp;
775

    
776
    do{
777
        tmp = avio_r8(bc);
778
        val= (val<<7) + (tmp&127);
779
    }while(tmp&128);
780
    return val;
781
}
782

    
783
int url_fdopen(AVIOContext **s, URLContext *h)
784
{
785
    uint8_t *buffer;
786
    int buffer_size, max_packet_size;
787

    
788
    max_packet_size = url_get_max_packet_size(h);
789
    if (max_packet_size) {
790
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
791
    } else {
792
        buffer_size = IO_BUFFER_SIZE;
793
    }
794
    buffer = av_malloc(buffer_size);
795
    if (!buffer)
796
        return AVERROR(ENOMEM);
797

    
798
    *s = av_mallocz(sizeof(AVIOContext));
799
    if(!*s) {
800
        av_free(buffer);
801
        return AVERROR(ENOMEM);
802
    }
803

    
804
    if (ffio_init_context(*s, buffer, buffer_size,
805
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
806
                      url_read, url_write, url_seek) < 0) {
807
        av_free(buffer);
808
        av_freep(s);
809
        return AVERROR(EIO);
810
    }
811
    (*s)->is_streamed = h->is_streamed;
812
    (*s)->max_packet_size = max_packet_size;
813
    if(h->prot) {
814
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
815
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
816
    }
817
    return 0;
818
}
819

    
820
int ffio_set_buf_size(AVIOContext *s, int buf_size)
821
{
822
    uint8_t *buffer;
823
    buffer = av_malloc(buf_size);
824
    if (!buffer)
825
        return AVERROR(ENOMEM);
826

    
827
    av_free(s->buffer);
828
    s->buffer = buffer;
829
    s->buffer_size = buf_size;
830
    s->buf_ptr = buffer;
831
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
832
    return 0;
833
}
834

    
835
#if FF_API_URL_RESETBUF
836
int url_resetbuf(AVIOContext *s, int flags)
837
#else
838
static int url_resetbuf(AVIOContext *s, int flags)
839
#endif
840
{
841
#if FF_API_URL_RESETBUF
842
    if (flags & URL_RDWR)
843
        return AVERROR(EINVAL);
844
#else
845
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
846
#endif
847

    
848
    if (flags & URL_WRONLY) {
849
        s->buf_end = s->buffer + s->buffer_size;
850
        s->write_flag = 1;
851
    } else {
852
        s->buf_end = s->buffer;
853
        s->write_flag = 0;
854
    }
855
    return 0;
856
}
857

    
858
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
859
{
860
    int64_t buffer_start;
861
    int buffer_size;
862
    int overlap, new_size, alloc_size;
863

    
864
    if (s->write_flag)
865
        return AVERROR(EINVAL);
866

    
867
    buffer_size = s->buf_end - s->buffer;
868

    
869
    /* the buffers must touch or overlap */
870
    if ((buffer_start = s->pos - buffer_size) > buf_size)
871
        return AVERROR(EINVAL);
872

    
873
    overlap = buf_size - buffer_start;
874
    new_size = buf_size + buffer_size - overlap;
875

    
876
    alloc_size = FFMAX(s->buffer_size, new_size);
877
    if (alloc_size > buf_size)
878
        if (!(buf = av_realloc(buf, alloc_size)))
879
            return AVERROR(ENOMEM);
880

    
881
    if (new_size > buf_size) {
882
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
883
        buf_size = new_size;
884
    }
885

    
886
    av_free(s->buffer);
887
    s->buf_ptr = s->buffer = buf;
888
    s->buffer_size = alloc_size;
889
    s->pos = buf_size;
890
    s->buf_end = s->buf_ptr + buf_size;
891
    s->eof_reached = 0;
892
    s->must_flush = 0;
893

    
894
    return 0;
895
}
896

    
897
int avio_open(AVIOContext **s, const char *filename, int flags)
898
{
899
    URLContext *h;
900
    int err;
901

    
902
    err = url_open(&h, filename, flags);
903
    if (err < 0)
904
        return err;
905
    err = url_fdopen(s, h);
906
    if (err < 0) {
907
        url_close(h);
908
        return err;
909
    }
910
    return 0;
911
}
912

    
913
int avio_close(AVIOContext *s)
914
{
915
    URLContext *h = s->opaque;
916

    
917
    av_free(s->buffer);
918
    av_free(s);
919
    return url_close(h);
920
}
921

    
922
URLContext *url_fileno(AVIOContext *s)
923
{
924
    return s->opaque;
925
}
926

    
927
#if CONFIG_MUXERS
928
int avio_printf(AVIOContext *s, const char *fmt, ...)
929
{
930
    va_list ap;
931
    char buf[4096];
932
    int ret;
933

    
934
    va_start(ap, fmt);
935
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
936
    va_end(ap);
937
    avio_write(s, buf, strlen(buf));
938
    return ret;
939
}
940
#endif //CONFIG_MUXERS
941

    
942
#if FF_API_OLD_AVIO
943
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
944
{
945
    int c;
946
    char *q;
947

    
948
    c = avio_r8(s);
949
    if (s->eof_reached)
950
        return NULL;
951
    q = buf;
952
    for(;;) {
953
        if (s->eof_reached || c == '\n')
954
            break;
955
        if ((q - buf) < buf_size - 1)
956
            *q++ = c;
957
        c = avio_r8(s);
958
    }
959
    if (buf_size > 0)
960
        *q = '\0';
961
    return buf;
962
}
963

    
964
int url_fget_max_packet_size(AVIOContext *s)
965
{
966
    return s->max_packet_size;
967
}
968
#endif
969

    
970
int av_url_read_fpause(AVIOContext *s, int pause)
971
{
972
    if (!s->read_pause)
973
        return AVERROR(ENOSYS);
974
    return s->read_pause(s->opaque, pause);
975
}
976

    
977
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
978
                          int64_t timestamp, int flags)
979
{
980
    URLContext *h = s->opaque;
981
    int64_t ret;
982
    if (!s->read_seek)
983
        return AVERROR(ENOSYS);
984
    ret = s->read_seek(h, stream_index, timestamp, flags);
985
    if(ret >= 0) {
986
        int64_t pos;
987
        s->buf_ptr = s->buf_end; // Flush buffer
988
        pos = s->seek(h, 0, SEEK_CUR);
989
        if (pos >= 0)
990
            s->pos = pos;
991
        else if (pos != AVERROR(ENOSYS))
992
            ret = pos;
993
    }
994
    return ret;
995
}
996

    
997
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
998
 * back to the server even if CONFIG_MUXERS is false. */
999
#if CONFIG_MUXERS || CONFIG_NETWORK
1000
/* buffer handling */
1001
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
1002
{
1003
    int ret;
1004
    *s = av_mallocz(sizeof(AVIOContext));
1005
    if(!*s)
1006
        return AVERROR(ENOMEM);
1007
    ret = ffio_init_context(*s, buf, buf_size,
1008
                        (flags & URL_WRONLY || flags & URL_RDWR),
1009
                        NULL, NULL, NULL, NULL);
1010
    if(ret != 0)
1011
        av_freep(s);
1012
    return ret;
1013
}
1014

    
1015
int url_close_buf(AVIOContext *s)
1016
{
1017
    put_flush_packet(s);
1018
    return s->buf_ptr - s->buffer;
1019
}
1020

    
1021
/* output in a dynamic buffer */
1022

    
1023
typedef struct DynBuffer {
1024
    int pos, size, allocated_size;
1025
    uint8_t *buffer;
1026
    int io_buffer_size;
1027
    uint8_t io_buffer[1];
1028
} DynBuffer;
1029

    
1030
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1031
{
1032
    DynBuffer *d = opaque;
1033
    unsigned new_size, new_allocated_size;
1034

    
1035
    /* reallocate buffer if needed */
1036
    new_size = d->pos + buf_size;
1037
    new_allocated_size = d->allocated_size;
1038
    if(new_size < d->pos || new_size > INT_MAX/2)
1039
        return -1;
1040
    while (new_size > new_allocated_size) {
1041
        if (!new_allocated_size)
1042
            new_allocated_size = new_size;
1043
        else
1044
            new_allocated_size += new_allocated_size / 2 + 1;
1045
    }
1046

    
1047
    if (new_allocated_size > d->allocated_size) {
1048
        d->buffer = av_realloc(d->buffer, new_allocated_size);
1049
        if(d->buffer == NULL)
1050
             return AVERROR(ENOMEM);
1051
        d->allocated_size = new_allocated_size;
1052
    }
1053
    memcpy(d->buffer + d->pos, buf, buf_size);
1054
    d->pos = new_size;
1055
    if (d->pos > d->size)
1056
        d->size = d->pos;
1057
    return buf_size;
1058
}
1059

    
1060
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1061
{
1062
    unsigned char buf1[4];
1063
    int ret;
1064

    
1065
    /* packetized write: output the header */
1066
    AV_WB32(buf1, buf_size);
1067
    ret= dyn_buf_write(opaque, buf1, 4);
1068
    if(ret < 0)
1069
        return ret;
1070

    
1071
    /* then the data */
1072
    return dyn_buf_write(opaque, buf, buf_size);
1073
}
1074

    
1075
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1076
{
1077
    DynBuffer *d = opaque;
1078

    
1079
    if (whence == SEEK_CUR)
1080
        offset += d->pos;
1081
    else if (whence == SEEK_END)
1082
        offset += d->size;
1083
    if (offset < 0 || offset > 0x7fffffffLL)
1084
        return -1;
1085
    d->pos = offset;
1086
    return 0;
1087
}
1088

    
1089
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1090
{
1091
    DynBuffer *d;
1092
    int ret;
1093
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1094

    
1095
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1096
        return -1;
1097
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1098
    if (!d)
1099
        return AVERROR(ENOMEM);
1100
    *s = av_mallocz(sizeof(AVIOContext));
1101
    if(!*s) {
1102
        av_free(d);
1103
        return AVERROR(ENOMEM);
1104
    }
1105
    d->io_buffer_size = io_buffer_size;
1106
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
1107
                        1, d, NULL,
1108
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1109
                        max_packet_size ? NULL : dyn_buf_seek);
1110
    if (ret == 0) {
1111
        (*s)->max_packet_size = max_packet_size;
1112
    } else {
1113
        av_free(d);
1114
        av_freep(s);
1115
    }
1116
    return ret;
1117
}
1118

    
1119
int url_open_dyn_buf(AVIOContext **s)
1120
{
1121
    return url_open_dyn_buf_internal(s, 0);
1122
}
1123

    
1124
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1125
{
1126
    if (max_packet_size <= 0)
1127
        return -1;
1128
    return url_open_dyn_buf_internal(s, max_packet_size);
1129
}
1130

    
1131
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1132
{
1133
    DynBuffer *d = s->opaque;
1134
    int size;
1135
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1136
    int padding = 0;
1137

    
1138
    /* don't attempt to pad fixed-size packet buffers */
1139
    if (!s->max_packet_size) {
1140
        avio_write(s, padbuf, sizeof(padbuf));
1141
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1142
    }
1143

    
1144
    put_flush_packet(s);
1145

    
1146
    *pbuffer = d->buffer;
1147
    size = d->size;
1148
    av_free(d);
1149
    av_free(s);
1150
    return size - padding;
1151
}
1152
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */