Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ ebb92e07

History | View | Annotate | Download (26.3 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 url_fseek(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
int url_fskip(AVIOContext *s, int64_t offset)
237
{
238
    int64_t ret = url_fseek(s, offset, SEEK_CUR);
239
    return ret < 0 ? ret : 0;
240
}
241

    
242
int64_t url_ftell(AVIOContext *s)
243
{
244
    return url_fseek(s, 0, SEEK_CUR);
245
}
246

    
247
int64_t url_fsize(AVIOContext *s)
248
{
249
    int64_t size;
250

    
251
    if(!s)
252
        return AVERROR(EINVAL);
253

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

    
266
int url_feof(AVIOContext *s)
267
{
268
    if(!s)
269
        return 0;
270
    return s->eof_reached;
271
}
272

    
273
int url_ferror(AVIOContext *s)
274
{
275
    if(!s)
276
        return 0;
277
    return s->error;
278
}
279

    
280
void avio_wl32(AVIOContext *s, unsigned int val)
281
{
282
    avio_w8(s, val);
283
    avio_w8(s, val >> 8);
284
    avio_w8(s, val >> 16);
285
    avio_w8(s, val >> 24);
286
}
287

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

    
296
#if FF_API_OLD_AVIO
297
void put_strz(AVIOContext *s, const char *str)
298
{
299
    avio_put_str(s, str);
300
}
301

    
302
#define GET(name, type) \
303
    type get_be ##name(AVIOContext *s) \
304
{\
305
    return avio_rb ##name(s);\
306
}\
307
    type get_le ##name(AVIOContext *s) \
308
{\
309
    return avio_rl ##name(s);\
310
}
311

    
312
GET(16, unsigned int)
313
GET(24, unsigned int)
314
GET(32, unsigned int)
315
GET(64, uint64_t)
316

    
317
#undef GET
318

    
319
#define PUT(name, type ) \
320
    void put_le ##name(AVIOContext *s, type val)\
321
{\
322
        avio_wl ##name(s, val);\
323
}\
324
    void put_be ##name(AVIOContext *s, type val)\
325
{\
326
        avio_wb ##name(s, val);\
327
}
328

    
329
PUT(16, unsigned int)
330
PUT(24, unsigned int)
331
PUT(32, unsigned int)
332
PUT(64, uint64_t)
333
#undef PUT
334

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

    
360
int url_fopen(AVIOContext **s, const char *filename, int flags)
361
{
362
    return avio_open(s, filename, flags);
363
}
364
int url_fclose(AVIOContext *s)
365
{
366
    return avio_close(s);
367
}
368
#endif
369

    
370
int avio_put_str(AVIOContext *s, const char *str)
371
{
372
    int len = 1;
373
    if (str) {
374
        len += strlen(str);
375
        avio_write(s, (const unsigned char *) str, len);
376
    } else
377
        avio_w8(s, 0);
378
    return len;
379
}
380

    
381
int avio_put_str16le(AVIOContext *s, const char *str)
382
{
383
    const uint8_t *q = str;
384
    int ret = 0;
385

    
386
    while (*q) {
387
        uint32_t ch;
388
        uint16_t tmp;
389

    
390
        GET_UTF8(ch, *q++, break;)
391
        PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
392
    }
393
    avio_wl16(s, 0);
394
    ret += 2;
395
    return ret;
396
}
397

    
398
int ff_get_v_length(uint64_t val){
399
    int i=1;
400

    
401
    while(val>>=7)
402
        i++;
403

    
404
    return i;
405
}
406

    
407
void ff_put_v(AVIOContext *bc, uint64_t val){
408
    int i= ff_get_v_length(val);
409

    
410
    while(--i>0)
411
        avio_w8(bc, 128 | (val>>(7*i)));
412

    
413
    avio_w8(bc, val&127);
414
}
415

    
416
void avio_wl64(AVIOContext *s, uint64_t val)
417
{
418
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
419
    avio_wl32(s, (uint32_t)(val >> 32));
420
}
421

    
422
void avio_wb64(AVIOContext *s, uint64_t val)
423
{
424
    avio_wb32(s, (uint32_t)(val >> 32));
425
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
426
}
427

    
428
void avio_wl16(AVIOContext *s, unsigned int val)
429
{
430
    avio_w8(s, val);
431
    avio_w8(s, val >> 8);
432
}
433

    
434
void avio_wb16(AVIOContext *s, unsigned int val)
435
{
436
    avio_w8(s, val >> 8);
437
    avio_w8(s, val);
438
}
439

    
440
void avio_wl24(AVIOContext *s, unsigned int val)
441
{
442
    avio_wl16(s, val & 0xffff);
443
    avio_w8(s, val >> 16);
444
}
445

    
446
void avio_wb24(AVIOContext *s, unsigned int val)
447
{
448
    avio_wb16(s, val >> 8);
449
    avio_w8(s, val);
450
}
451

    
452
void put_tag(AVIOContext *s, const char *tag)
453
{
454
    while (*tag) {
455
        avio_w8(s, *tag++);
456
    }
457
}
458

    
459
/* Input stream */
460

    
461
static void fill_buffer(AVIOContext *s)
462
{
463
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
464
    int len= s->buffer_size - (dst - s->buffer);
465
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
466

    
467
    /* no need to do anything if EOF already reached */
468
    if (s->eof_reached)
469
        return;
470

    
471
    if(s->update_checksum && dst == s->buffer){
472
        if(s->buf_end > s->checksum_ptr)
473
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
474
        s->checksum_ptr= s->buffer;
475
    }
476

    
477
    /* make buffer smaller in case it ended up large after probing */
478
    if (s->buffer_size > max_buffer_size) {
479
        url_setbufsize(s, max_buffer_size);
480

    
481
        s->checksum_ptr = dst = s->buffer;
482
        len = s->buffer_size;
483
    }
484

    
485
    if(s->read_packet)
486
        len = s->read_packet(s->opaque, dst, len);
487
    else
488
        len = 0;
489
    if (len <= 0) {
490
        /* do not modify buffer if EOF reached so that a seek back can
491
           be done without rereading data */
492
        s->eof_reached = 1;
493
        if(len<0)
494
            s->error= len;
495
    } else {
496
        s->pos += len;
497
        s->buf_ptr = dst;
498
        s->buf_end = dst + len;
499
    }
500
}
501

    
502
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
503
                                    unsigned int len)
504
{
505
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
506
}
507

    
508
unsigned long get_checksum(AVIOContext *s)
509
{
510
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
511
    s->update_checksum= NULL;
512
    return s->checksum;
513
}
514

    
515
void init_checksum(AVIOContext *s,
516
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
517
                   unsigned long checksum)
518
{
519
    s->update_checksum= update_checksum;
520
    if(s->update_checksum){
521
        s->checksum= checksum;
522
        s->checksum_ptr= s->buf_ptr;
523
    }
524
}
525

    
526
/* XXX: put an inline version */
527
int avio_r8(AVIOContext *s)
528
{
529
    if (s->buf_ptr >= s->buf_end)
530
        fill_buffer(s);
531
    if (s->buf_ptr < s->buf_end)
532
        return *s->buf_ptr++;
533
    return 0;
534
}
535

    
536
int url_fgetc(AVIOContext *s)
537
{
538
    if (s->buf_ptr >= s->buf_end)
539
        fill_buffer(s);
540
    if (s->buf_ptr < s->buf_end)
541
        return *s->buf_ptr++;
542
    return URL_EOF;
543
}
544

    
545
int avio_read(AVIOContext *s, unsigned char *buf, int size)
546
{
547
    int len, size1;
548

    
549
    size1 = size;
550
    while (size > 0) {
551
        len = s->buf_end - s->buf_ptr;
552
        if (len > size)
553
            len = size;
554
        if (len == 0) {
555
            if(size > s->buffer_size && !s->update_checksum){
556
                if(s->read_packet)
557
                    len = s->read_packet(s->opaque, buf, size);
558
                if (len <= 0) {
559
                    /* do not modify buffer if EOF reached so that a seek back can
560
                    be done without rereading data */
561
                    s->eof_reached = 1;
562
                    if(len<0)
563
                        s->error= len;
564
                    break;
565
                } else {
566
                    s->pos += len;
567
                    size -= len;
568
                    buf += len;
569
                    s->buf_ptr = s->buffer;
570
                    s->buf_end = s->buffer/* + len*/;
571
                }
572
            }else{
573
                fill_buffer(s);
574
                len = s->buf_end - s->buf_ptr;
575
                if (len == 0)
576
                    break;
577
            }
578
        } else {
579
            memcpy(buf, s->buf_ptr, len);
580
            buf += len;
581
            s->buf_ptr += len;
582
            size -= len;
583
        }
584
    }
585
    if (size1 == size) {
586
        if (url_ferror(s)) return url_ferror(s);
587
        if (url_feof(s))   return AVERROR_EOF;
588
    }
589
    return size1 - size;
590
}
591

    
592
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
593
{
594
    int len;
595

    
596
    if(size<0)
597
        return -1;
598

    
599
    len = s->buf_end - s->buf_ptr;
600
    if (len == 0) {
601
        fill_buffer(s);
602
        len = s->buf_end - s->buf_ptr;
603
    }
604
    if (len > size)
605
        len = size;
606
    memcpy(buf, s->buf_ptr, len);
607
    s->buf_ptr += len;
608
    if (!len) {
609
        if (url_ferror(s)) return url_ferror(s);
610
        if (url_feof(s))   return AVERROR_EOF;
611
    }
612
    return len;
613
}
614

    
615
unsigned int avio_rl16(AVIOContext *s)
616
{
617
    unsigned int val;
618
    val = avio_r8(s);
619
    val |= avio_r8(s) << 8;
620
    return val;
621
}
622

    
623
unsigned int avio_rl24(AVIOContext *s)
624
{
625
    unsigned int val;
626
    val = avio_rl16(s);
627
    val |= avio_r8(s) << 16;
628
    return val;
629
}
630

    
631
unsigned int avio_rl32(AVIOContext *s)
632
{
633
    unsigned int val;
634
    val = avio_rl16(s);
635
    val |= avio_rl16(s) << 16;
636
    return val;
637
}
638

    
639
uint64_t avio_rl64(AVIOContext *s)
640
{
641
    uint64_t val;
642
    val = (uint64_t)avio_rl32(s);
643
    val |= (uint64_t)avio_rl32(s) << 32;
644
    return val;
645
}
646

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

    
655
unsigned int avio_rb24(AVIOContext *s)
656
{
657
    unsigned int val;
658
    val = avio_rb16(s) << 8;
659
    val |= avio_r8(s);
660
    return val;
661
}
662
unsigned int avio_rb32(AVIOContext *s)
663
{
664
    unsigned int val;
665
    val = avio_rb16(s) << 16;
666
    val |= avio_rb16(s);
667
    return val;
668
}
669

    
670
char *get_strz(AVIOContext *s, char *buf, int maxlen)
671
{
672
    int i = 0;
673
    char c;
674

    
675
    while ((c = avio_r8(s))) {
676
        if (i < maxlen-1)
677
            buf[i++] = c;
678
    }
679

    
680
    buf[i] = 0; /* Ensure null terminated, but may be truncated */
681

    
682
    return buf;
683
}
684

    
685
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
686
{
687
    int i = 0;
688
    char c;
689

    
690
    do {
691
        c = avio_r8(s);
692
        if (c && i < maxlen-1)
693
            buf[i++] = c;
694
    } while (c != '\n' && c);
695

    
696
    buf[i] = 0;
697
    return i;
698
}
699

    
700
#define GET_STR16(type, read) \
701
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
702
{\
703
    char* q = buf;\
704
    int ret = 0;\
705
    while (ret + 1 < maxlen) {\
706
        uint8_t tmp;\
707
        uint32_t ch;\
708
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
709
        if (!ch)\
710
            break;\
711
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
712
    }\
713
    *q = 0;\
714
    return ret;\
715
}\
716

    
717
GET_STR16(le, avio_rl16)
718
GET_STR16(be, avio_rb16)
719

    
720
#undef GET_STR16
721

    
722
uint64_t avio_rb64(AVIOContext *s)
723
{
724
    uint64_t val;
725
    val = (uint64_t)avio_rb32(s) << 32;
726
    val |= (uint64_t)avio_rb32(s);
727
    return val;
728
}
729

    
730
uint64_t ff_get_v(AVIOContext *bc){
731
    uint64_t val = 0;
732
    int tmp;
733

    
734
    do{
735
        tmp = avio_r8(bc);
736
        val= (val<<7) + (tmp&127);
737
    }while(tmp&128);
738
    return val;
739
}
740

    
741
int url_fdopen(AVIOContext **s, URLContext *h)
742
{
743
    uint8_t *buffer;
744
    int buffer_size, max_packet_size;
745

    
746
    max_packet_size = url_get_max_packet_size(h);
747
    if (max_packet_size) {
748
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
749
    } else {
750
        buffer_size = IO_BUFFER_SIZE;
751
    }
752
    buffer = av_malloc(buffer_size);
753
    if (!buffer)
754
        return AVERROR(ENOMEM);
755

    
756
    *s = av_mallocz(sizeof(AVIOContext));
757
    if(!*s) {
758
        av_free(buffer);
759
        return AVERROR(ENOMEM);
760
    }
761

    
762
    if (ffio_init_context(*s, buffer, buffer_size,
763
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
764
                      url_read, url_write, url_seek) < 0) {
765
        av_free(buffer);
766
        av_freep(s);
767
        return AVERROR(EIO);
768
    }
769
    (*s)->is_streamed = h->is_streamed;
770
    (*s)->max_packet_size = max_packet_size;
771
    if(h->prot) {
772
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
773
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
774
    }
775
    return 0;
776
}
777

    
778
int url_setbufsize(AVIOContext *s, int buf_size)
779
{
780
    uint8_t *buffer;
781
    buffer = av_malloc(buf_size);
782
    if (!buffer)
783
        return AVERROR(ENOMEM);
784

    
785
    av_free(s->buffer);
786
    s->buffer = buffer;
787
    s->buffer_size = buf_size;
788
    s->buf_ptr = buffer;
789
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
790
    return 0;
791
}
792

    
793
#if FF_API_URL_RESETBUF
794
int url_resetbuf(AVIOContext *s, int flags)
795
#else
796
static int url_resetbuf(AVIOContext *s, int flags)
797
#endif
798
{
799
#if FF_API_URL_RESETBUF
800
    if (flags & URL_RDWR)
801
        return AVERROR(EINVAL);
802
#else
803
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
804
#endif
805

    
806
    if (flags & URL_WRONLY) {
807
        s->buf_end = s->buffer + s->buffer_size;
808
        s->write_flag = 1;
809
    } else {
810
        s->buf_end = s->buffer;
811
        s->write_flag = 0;
812
    }
813
    return 0;
814
}
815

    
816
int ff_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
817
{
818
    int64_t buffer_start;
819
    int buffer_size;
820
    int overlap, new_size, alloc_size;
821

    
822
    if (s->write_flag)
823
        return AVERROR(EINVAL);
824

    
825
    buffer_size = s->buf_end - s->buffer;
826

    
827
    /* the buffers must touch or overlap */
828
    if ((buffer_start = s->pos - buffer_size) > buf_size)
829
        return AVERROR(EINVAL);
830

    
831
    overlap = buf_size - buffer_start;
832
    new_size = buf_size + buffer_size - overlap;
833

    
834
    alloc_size = FFMAX(s->buffer_size, new_size);
835
    if (alloc_size > buf_size)
836
        if (!(buf = av_realloc(buf, alloc_size)))
837
            return AVERROR(ENOMEM);
838

    
839
    if (new_size > buf_size) {
840
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
841
        buf_size = new_size;
842
    }
843

    
844
    av_free(s->buffer);
845
    s->buf_ptr = s->buffer = buf;
846
    s->buffer_size = alloc_size;
847
    s->pos = buf_size;
848
    s->buf_end = s->buf_ptr + buf_size;
849
    s->eof_reached = 0;
850
    s->must_flush = 0;
851

    
852
    return 0;
853
}
854

    
855
int avio_open(AVIOContext **s, const char *filename, int flags)
856
{
857
    URLContext *h;
858
    int err;
859

    
860
    err = url_open(&h, filename, flags);
861
    if (err < 0)
862
        return err;
863
    err = url_fdopen(s, h);
864
    if (err < 0) {
865
        url_close(h);
866
        return err;
867
    }
868
    return 0;
869
}
870

    
871
int avio_close(AVIOContext *s)
872
{
873
    URLContext *h = s->opaque;
874

    
875
    av_free(s->buffer);
876
    av_free(s);
877
    return url_close(h);
878
}
879

    
880
URLContext *url_fileno(AVIOContext *s)
881
{
882
    return s->opaque;
883
}
884

    
885
#if CONFIG_MUXERS
886
int url_fprintf(AVIOContext *s, const char *fmt, ...)
887
{
888
    va_list ap;
889
    char buf[4096];
890
    int ret;
891

    
892
    va_start(ap, fmt);
893
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
894
    va_end(ap);
895
    avio_write(s, buf, strlen(buf));
896
    return ret;
897
}
898
#endif //CONFIG_MUXERS
899

    
900
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
901
{
902
    int c;
903
    char *q;
904

    
905
    c = url_fgetc(s);
906
    if (c == EOF)
907
        return NULL;
908
    q = buf;
909
    for(;;) {
910
        if (c == EOF || c == '\n')
911
            break;
912
        if ((q - buf) < buf_size - 1)
913
            *q++ = c;
914
        c = url_fgetc(s);
915
    }
916
    if (buf_size > 0)
917
        *q = '\0';
918
    return buf;
919
}
920

    
921
int url_fget_max_packet_size(AVIOContext *s)
922
{
923
    return s->max_packet_size;
924
}
925

    
926
int av_url_read_fpause(AVIOContext *s, int pause)
927
{
928
    if (!s->read_pause)
929
        return AVERROR(ENOSYS);
930
    return s->read_pause(s->opaque, pause);
931
}
932

    
933
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
934
                          int64_t timestamp, int flags)
935
{
936
    URLContext *h = s->opaque;
937
    int64_t ret;
938
    if (!s->read_seek)
939
        return AVERROR(ENOSYS);
940
    ret = s->read_seek(h, stream_index, timestamp, flags);
941
    if(ret >= 0) {
942
        int64_t pos;
943
        s->buf_ptr = s->buf_end; // Flush buffer
944
        pos = s->seek(h, 0, SEEK_CUR);
945
        if (pos >= 0)
946
            s->pos = pos;
947
        else if (pos != AVERROR(ENOSYS))
948
            ret = pos;
949
    }
950
    return ret;
951
}
952

    
953
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
954
 * back to the server even if CONFIG_MUXERS is false. */
955
#if CONFIG_MUXERS || CONFIG_NETWORK
956
/* buffer handling */
957
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
958
{
959
    int ret;
960
    *s = av_mallocz(sizeof(AVIOContext));
961
    if(!*s)
962
        return AVERROR(ENOMEM);
963
    ret = ffio_init_context(*s, buf, buf_size,
964
                        (flags & URL_WRONLY || flags & URL_RDWR),
965
                        NULL, NULL, NULL, NULL);
966
    if(ret != 0)
967
        av_freep(s);
968
    return ret;
969
}
970

    
971
int url_close_buf(AVIOContext *s)
972
{
973
    put_flush_packet(s);
974
    return s->buf_ptr - s->buffer;
975
}
976

    
977
/* output in a dynamic buffer */
978

    
979
typedef struct DynBuffer {
980
    int pos, size, allocated_size;
981
    uint8_t *buffer;
982
    int io_buffer_size;
983
    uint8_t io_buffer[1];
984
} DynBuffer;
985

    
986
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
987
{
988
    DynBuffer *d = opaque;
989
    unsigned new_size, new_allocated_size;
990

    
991
    /* reallocate buffer if needed */
992
    new_size = d->pos + buf_size;
993
    new_allocated_size = d->allocated_size;
994
    if(new_size < d->pos || new_size > INT_MAX/2)
995
        return -1;
996
    while (new_size > new_allocated_size) {
997
        if (!new_allocated_size)
998
            new_allocated_size = new_size;
999
        else
1000
            new_allocated_size += new_allocated_size / 2 + 1;
1001
    }
1002

    
1003
    if (new_allocated_size > d->allocated_size) {
1004
        d->buffer = av_realloc(d->buffer, new_allocated_size);
1005
        if(d->buffer == NULL)
1006
             return AVERROR(ENOMEM);
1007
        d->allocated_size = new_allocated_size;
1008
    }
1009
    memcpy(d->buffer + d->pos, buf, buf_size);
1010
    d->pos = new_size;
1011
    if (d->pos > d->size)
1012
        d->size = d->pos;
1013
    return buf_size;
1014
}
1015

    
1016
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1017
{
1018
    unsigned char buf1[4];
1019
    int ret;
1020

    
1021
    /* packetized write: output the header */
1022
    AV_WB32(buf1, buf_size);
1023
    ret= dyn_buf_write(opaque, buf1, 4);
1024
    if(ret < 0)
1025
        return ret;
1026

    
1027
    /* then the data */
1028
    return dyn_buf_write(opaque, buf, buf_size);
1029
}
1030

    
1031
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1032
{
1033
    DynBuffer *d = opaque;
1034

    
1035
    if (whence == SEEK_CUR)
1036
        offset += d->pos;
1037
    else if (whence == SEEK_END)
1038
        offset += d->size;
1039
    if (offset < 0 || offset > 0x7fffffffLL)
1040
        return -1;
1041
    d->pos = offset;
1042
    return 0;
1043
}
1044

    
1045
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1046
{
1047
    DynBuffer *d;
1048
    int ret;
1049
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1050

    
1051
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1052
        return -1;
1053
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1054
    if (!d)
1055
        return AVERROR(ENOMEM);
1056
    *s = av_mallocz(sizeof(AVIOContext));
1057
    if(!*s) {
1058
        av_free(d);
1059
        return AVERROR(ENOMEM);
1060
    }
1061
    d->io_buffer_size = io_buffer_size;
1062
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
1063
                        1, d, NULL,
1064
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1065
                        max_packet_size ? NULL : dyn_buf_seek);
1066
    if (ret == 0) {
1067
        (*s)->max_packet_size = max_packet_size;
1068
    } else {
1069
        av_free(d);
1070
        av_freep(s);
1071
    }
1072
    return ret;
1073
}
1074

    
1075
int url_open_dyn_buf(AVIOContext **s)
1076
{
1077
    return url_open_dyn_buf_internal(s, 0);
1078
}
1079

    
1080
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1081
{
1082
    if (max_packet_size <= 0)
1083
        return -1;
1084
    return url_open_dyn_buf_internal(s, max_packet_size);
1085
}
1086

    
1087
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1088
{
1089
    DynBuffer *d = s->opaque;
1090
    int size;
1091
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1092
    int padding = 0;
1093

    
1094
    /* don't attempt to pad fixed-size packet buffers */
1095
    if (!s->max_packet_size) {
1096
        avio_write(s, padbuf, sizeof(padbuf));
1097
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1098
    }
1099

    
1100
    put_flush_packet(s);
1101

    
1102
    *pbuffer = d->buffer;
1103
    size = d->size;
1104
    av_free(d);
1105
    av_free(s);
1106
    return size - padding;
1107
}
1108
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */