Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 90ed2776

History | View | Annotate | Download (25 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 put_byte(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 put_nbyte(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 put_buffer(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 put_le32(AVIOContext *s, unsigned int val)
281
{
282
    put_byte(s, val);
283
    put_byte(s, val >> 8);
284
    put_byte(s, val >> 16);
285
    put_byte(s, val >> 24);
286
}
287

    
288
void put_be32(AVIOContext *s, unsigned int val)
289
{
290
    put_byte(s, val >> 24);
291
    put_byte(s, val >> 16);
292
    put_byte(s, val >> 8);
293
    put_byte(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
#endif
302

    
303
int avio_put_str(AVIOContext *s, const char *str)
304
{
305
    int len = 1;
306
    if (str) {
307
        len += strlen(str);
308
        put_buffer(s, (const unsigned char *) str, len);
309
    } else
310
        put_byte(s, 0);
311
    return len;
312
}
313

    
314
int avio_put_str16le(AVIOContext *s, const char *str)
315
{
316
    const uint8_t *q = str;
317
    int ret = 0;
318

    
319
    while (*q) {
320
        uint32_t ch;
321
        uint16_t tmp;
322

    
323
        GET_UTF8(ch, *q++, break;)
324
        PUT_UTF16(ch, tmp, put_le16(s, tmp);ret += 2;)
325
    }
326
    put_le16(s, 0);
327
    ret += 2;
328
    return ret;
329
}
330

    
331
int ff_get_v_length(uint64_t val){
332
    int i=1;
333

    
334
    while(val>>=7)
335
        i++;
336

    
337
    return i;
338
}
339

    
340
void ff_put_v(AVIOContext *bc, uint64_t val){
341
    int i= ff_get_v_length(val);
342

    
343
    while(--i>0)
344
        put_byte(bc, 128 | (val>>(7*i)));
345

    
346
    put_byte(bc, val&127);
347
}
348

    
349
void put_le64(AVIOContext *s, uint64_t val)
350
{
351
    put_le32(s, (uint32_t)(val & 0xffffffff));
352
    put_le32(s, (uint32_t)(val >> 32));
353
}
354

    
355
void put_be64(AVIOContext *s, uint64_t val)
356
{
357
    put_be32(s, (uint32_t)(val >> 32));
358
    put_be32(s, (uint32_t)(val & 0xffffffff));
359
}
360

    
361
void put_le16(AVIOContext *s, unsigned int val)
362
{
363
    put_byte(s, val);
364
    put_byte(s, val >> 8);
365
}
366

    
367
void put_be16(AVIOContext *s, unsigned int val)
368
{
369
    put_byte(s, val >> 8);
370
    put_byte(s, val);
371
}
372

    
373
void put_le24(AVIOContext *s, unsigned int val)
374
{
375
    put_le16(s, val & 0xffff);
376
    put_byte(s, val >> 16);
377
}
378

    
379
void put_be24(AVIOContext *s, unsigned int val)
380
{
381
    put_be16(s, val >> 8);
382
    put_byte(s, val);
383
}
384

    
385
void put_tag(AVIOContext *s, const char *tag)
386
{
387
    while (*tag) {
388
        put_byte(s, *tag++);
389
    }
390
}
391

    
392
/* Input stream */
393

    
394
static void fill_buffer(AVIOContext *s)
395
{
396
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
397
    int len= s->buffer_size - (dst - s->buffer);
398
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
399

    
400
    /* no need to do anything if EOF already reached */
401
    if (s->eof_reached)
402
        return;
403

    
404
    if(s->update_checksum && dst == s->buffer){
405
        if(s->buf_end > s->checksum_ptr)
406
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
407
        s->checksum_ptr= s->buffer;
408
    }
409

    
410
    /* make buffer smaller in case it ended up large after probing */
411
    if (s->buffer_size > max_buffer_size) {
412
        url_setbufsize(s, max_buffer_size);
413

    
414
        s->checksum_ptr = dst = s->buffer;
415
        len = s->buffer_size;
416
    }
417

    
418
    if(s->read_packet)
419
        len = s->read_packet(s->opaque, dst, len);
420
    else
421
        len = 0;
422
    if (len <= 0) {
423
        /* do not modify buffer if EOF reached so that a seek back can
424
           be done without rereading data */
425
        s->eof_reached = 1;
426
        if(len<0)
427
            s->error= len;
428
    } else {
429
        s->pos += len;
430
        s->buf_ptr = dst;
431
        s->buf_end = dst + len;
432
    }
433
}
434

    
435
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
436
                                    unsigned int len)
437
{
438
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
439
}
440

    
441
unsigned long get_checksum(AVIOContext *s)
442
{
443
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
444
    s->update_checksum= NULL;
445
    return s->checksum;
446
}
447

    
448
void init_checksum(AVIOContext *s,
449
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
450
                   unsigned long checksum)
451
{
452
    s->update_checksum= update_checksum;
453
    if(s->update_checksum){
454
        s->checksum= checksum;
455
        s->checksum_ptr= s->buf_ptr;
456
    }
457
}
458

    
459
/* XXX: put an inline version */
460
int get_byte(AVIOContext *s)
461
{
462
    if (s->buf_ptr >= s->buf_end)
463
        fill_buffer(s);
464
    if (s->buf_ptr < s->buf_end)
465
        return *s->buf_ptr++;
466
    return 0;
467
}
468

    
469
int url_fgetc(AVIOContext *s)
470
{
471
    if (s->buf_ptr >= s->buf_end)
472
        fill_buffer(s);
473
    if (s->buf_ptr < s->buf_end)
474
        return *s->buf_ptr++;
475
    return URL_EOF;
476
}
477

    
478
int get_buffer(AVIOContext *s, unsigned char *buf, int size)
479
{
480
    int len, size1;
481

    
482
    size1 = size;
483
    while (size > 0) {
484
        len = s->buf_end - s->buf_ptr;
485
        if (len > size)
486
            len = size;
487
        if (len == 0) {
488
            if(size > s->buffer_size && !s->update_checksum){
489
                if(s->read_packet)
490
                    len = s->read_packet(s->opaque, buf, size);
491
                if (len <= 0) {
492
                    /* do not modify buffer if EOF reached so that a seek back can
493
                    be done without rereading data */
494
                    s->eof_reached = 1;
495
                    if(len<0)
496
                        s->error= len;
497
                    break;
498
                } else {
499
                    s->pos += len;
500
                    size -= len;
501
                    buf += len;
502
                    s->buf_ptr = s->buffer;
503
                    s->buf_end = s->buffer/* + len*/;
504
                }
505
            }else{
506
                fill_buffer(s);
507
                len = s->buf_end - s->buf_ptr;
508
                if (len == 0)
509
                    break;
510
            }
511
        } else {
512
            memcpy(buf, s->buf_ptr, len);
513
            buf += len;
514
            s->buf_ptr += len;
515
            size -= len;
516
        }
517
    }
518
    if (size1 == size) {
519
        if (url_ferror(s)) return url_ferror(s);
520
        if (url_feof(s))   return AVERROR_EOF;
521
    }
522
    return size1 - size;
523
}
524

    
525
int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size)
526
{
527
    int len;
528

    
529
    if(size<0)
530
        return -1;
531

    
532
    len = s->buf_end - s->buf_ptr;
533
    if (len == 0) {
534
        fill_buffer(s);
535
        len = s->buf_end - s->buf_ptr;
536
    }
537
    if (len > size)
538
        len = size;
539
    memcpy(buf, s->buf_ptr, len);
540
    s->buf_ptr += len;
541
    if (!len) {
542
        if (url_ferror(s)) return url_ferror(s);
543
        if (url_feof(s))   return AVERROR_EOF;
544
    }
545
    return len;
546
}
547

    
548
unsigned int get_le16(AVIOContext *s)
549
{
550
    unsigned int val;
551
    val = get_byte(s);
552
    val |= get_byte(s) << 8;
553
    return val;
554
}
555

    
556
unsigned int get_le24(AVIOContext *s)
557
{
558
    unsigned int val;
559
    val = get_le16(s);
560
    val |= get_byte(s) << 16;
561
    return val;
562
}
563

    
564
unsigned int get_le32(AVIOContext *s)
565
{
566
    unsigned int val;
567
    val = get_le16(s);
568
    val |= get_le16(s) << 16;
569
    return val;
570
}
571

    
572
uint64_t get_le64(AVIOContext *s)
573
{
574
    uint64_t val;
575
    val = (uint64_t)get_le32(s);
576
    val |= (uint64_t)get_le32(s) << 32;
577
    return val;
578
}
579

    
580
unsigned int get_be16(AVIOContext *s)
581
{
582
    unsigned int val;
583
    val = get_byte(s) << 8;
584
    val |= get_byte(s);
585
    return val;
586
}
587

    
588
unsigned int get_be24(AVIOContext *s)
589
{
590
    unsigned int val;
591
    val = get_be16(s) << 8;
592
    val |= get_byte(s);
593
    return val;
594
}
595
unsigned int get_be32(AVIOContext *s)
596
{
597
    unsigned int val;
598
    val = get_be16(s) << 16;
599
    val |= get_be16(s);
600
    return val;
601
}
602

    
603
char *get_strz(AVIOContext *s, char *buf, int maxlen)
604
{
605
    int i = 0;
606
    char c;
607

    
608
    while ((c = get_byte(s))) {
609
        if (i < maxlen-1)
610
            buf[i++] = c;
611
    }
612

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

    
615
    return buf;
616
}
617

    
618
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
619
{
620
    int i = 0;
621
    char c;
622

    
623
    do {
624
        c = get_byte(s);
625
        if (c && i < maxlen-1)
626
            buf[i++] = c;
627
    } while (c != '\n' && c);
628

    
629
    buf[i] = 0;
630
    return i;
631
}
632

    
633
#define GET_STR16(type, read) \
634
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
635
{\
636
    char* q = buf;\
637
    int ret = 0;\
638
    while (ret + 1 < maxlen) {\
639
        uint8_t tmp;\
640
        uint32_t ch;\
641
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
642
        if (!ch)\
643
            break;\
644
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
645
    }\
646
    *q = 0;\
647
    return ret;\
648
}\
649

    
650
GET_STR16(le, get_le16)
651
GET_STR16(be, get_be16)
652

    
653
#undef GET_STR16
654

    
655
uint64_t get_be64(AVIOContext *s)
656
{
657
    uint64_t val;
658
    val = (uint64_t)get_be32(s) << 32;
659
    val |= (uint64_t)get_be32(s);
660
    return val;
661
}
662

    
663
uint64_t ff_get_v(AVIOContext *bc){
664
    uint64_t val = 0;
665
    int tmp;
666

    
667
    do{
668
        tmp = get_byte(bc);
669
        val= (val<<7) + (tmp&127);
670
    }while(tmp&128);
671
    return val;
672
}
673

    
674
int url_fdopen(AVIOContext **s, URLContext *h)
675
{
676
    uint8_t *buffer;
677
    int buffer_size, max_packet_size;
678

    
679
    max_packet_size = url_get_max_packet_size(h);
680
    if (max_packet_size) {
681
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
682
    } else {
683
        buffer_size = IO_BUFFER_SIZE;
684
    }
685
    buffer = av_malloc(buffer_size);
686
    if (!buffer)
687
        return AVERROR(ENOMEM);
688

    
689
    *s = av_mallocz(sizeof(AVIOContext));
690
    if(!*s) {
691
        av_free(buffer);
692
        return AVERROR(ENOMEM);
693
    }
694

    
695
    if (ffio_init_context(*s, buffer, buffer_size,
696
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
697
                      url_read, url_write, url_seek) < 0) {
698
        av_free(buffer);
699
        av_freep(s);
700
        return AVERROR(EIO);
701
    }
702
    (*s)->is_streamed = h->is_streamed;
703
    (*s)->max_packet_size = max_packet_size;
704
    if(h->prot) {
705
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
706
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
707
    }
708
    return 0;
709
}
710

    
711
int url_setbufsize(AVIOContext *s, int buf_size)
712
{
713
    uint8_t *buffer;
714
    buffer = av_malloc(buf_size);
715
    if (!buffer)
716
        return AVERROR(ENOMEM);
717

    
718
    av_free(s->buffer);
719
    s->buffer = buffer;
720
    s->buffer_size = buf_size;
721
    s->buf_ptr = buffer;
722
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
723
    return 0;
724
}
725

    
726
#if FF_API_URL_RESETBUF
727
int url_resetbuf(AVIOContext *s, int flags)
728
#else
729
static int url_resetbuf(AVIOContext *s, int flags)
730
#endif
731
{
732
#if FF_API_URL_RESETBUF
733
    if (flags & URL_RDWR)
734
        return AVERROR(EINVAL);
735
#else
736
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
737
#endif
738

    
739
    if (flags & URL_WRONLY) {
740
        s->buf_end = s->buffer + s->buffer_size;
741
        s->write_flag = 1;
742
    } else {
743
        s->buf_end = s->buffer;
744
        s->write_flag = 0;
745
    }
746
    return 0;
747
}
748

    
749
int ff_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
750
{
751
    int64_t buffer_start;
752
    int buffer_size;
753
    int overlap, new_size, alloc_size;
754

    
755
    if (s->write_flag)
756
        return AVERROR(EINVAL);
757

    
758
    buffer_size = s->buf_end - s->buffer;
759

    
760
    /* the buffers must touch or overlap */
761
    if ((buffer_start = s->pos - buffer_size) > buf_size)
762
        return AVERROR(EINVAL);
763

    
764
    overlap = buf_size - buffer_start;
765
    new_size = buf_size + buffer_size - overlap;
766

    
767
    alloc_size = FFMAX(s->buffer_size, new_size);
768
    if (alloc_size > buf_size)
769
        if (!(buf = av_realloc(buf, alloc_size)))
770
            return AVERROR(ENOMEM);
771

    
772
    if (new_size > buf_size) {
773
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
774
        buf_size = new_size;
775
    }
776

    
777
    av_free(s->buffer);
778
    s->buf_ptr = s->buffer = buf;
779
    s->buffer_size = alloc_size;
780
    s->pos = buf_size;
781
    s->buf_end = s->buf_ptr + buf_size;
782
    s->eof_reached = 0;
783
    s->must_flush = 0;
784

    
785
    return 0;
786
}
787

    
788
int url_fopen(AVIOContext **s, const char *filename, int flags)
789
{
790
    URLContext *h;
791
    int err;
792

    
793
    err = url_open(&h, filename, flags);
794
    if (err < 0)
795
        return err;
796
    err = url_fdopen(s, h);
797
    if (err < 0) {
798
        url_close(h);
799
        return err;
800
    }
801
    return 0;
802
}
803

    
804
int url_fclose(AVIOContext *s)
805
{
806
    URLContext *h = s->opaque;
807

    
808
    av_free(s->buffer);
809
    av_free(s);
810
    return url_close(h);
811
}
812

    
813
URLContext *url_fileno(AVIOContext *s)
814
{
815
    return s->opaque;
816
}
817

    
818
#if CONFIG_MUXERS
819
int url_fprintf(AVIOContext *s, const char *fmt, ...)
820
{
821
    va_list ap;
822
    char buf[4096];
823
    int ret;
824

    
825
    va_start(ap, fmt);
826
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
827
    va_end(ap);
828
    put_buffer(s, buf, strlen(buf));
829
    return ret;
830
}
831
#endif //CONFIG_MUXERS
832

    
833
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
834
{
835
    int c;
836
    char *q;
837

    
838
    c = url_fgetc(s);
839
    if (c == EOF)
840
        return NULL;
841
    q = buf;
842
    for(;;) {
843
        if (c == EOF || c == '\n')
844
            break;
845
        if ((q - buf) < buf_size - 1)
846
            *q++ = c;
847
        c = url_fgetc(s);
848
    }
849
    if (buf_size > 0)
850
        *q = '\0';
851
    return buf;
852
}
853

    
854
int url_fget_max_packet_size(AVIOContext *s)
855
{
856
    return s->max_packet_size;
857
}
858

    
859
int av_url_read_fpause(AVIOContext *s, int pause)
860
{
861
    if (!s->read_pause)
862
        return AVERROR(ENOSYS);
863
    return s->read_pause(s->opaque, pause);
864
}
865

    
866
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
867
                          int64_t timestamp, int flags)
868
{
869
    URLContext *h = s->opaque;
870
    int64_t ret;
871
    if (!s->read_seek)
872
        return AVERROR(ENOSYS);
873
    ret = s->read_seek(h, stream_index, timestamp, flags);
874
    if(ret >= 0) {
875
        int64_t pos;
876
        s->buf_ptr = s->buf_end; // Flush buffer
877
        pos = s->seek(h, 0, SEEK_CUR);
878
        if (pos >= 0)
879
            s->pos = pos;
880
        else if (pos != AVERROR(ENOSYS))
881
            ret = pos;
882
    }
883
    return ret;
884
}
885

    
886
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
887
 * back to the server even if CONFIG_MUXERS is false. */
888
#if CONFIG_MUXERS || CONFIG_NETWORK
889
/* buffer handling */
890
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
891
{
892
    int ret;
893
    *s = av_mallocz(sizeof(AVIOContext));
894
    if(!*s)
895
        return AVERROR(ENOMEM);
896
    ret = ffio_init_context(*s, buf, buf_size,
897
                        (flags & URL_WRONLY || flags & URL_RDWR),
898
                        NULL, NULL, NULL, NULL);
899
    if(ret != 0)
900
        av_freep(s);
901
    return ret;
902
}
903

    
904
int url_close_buf(AVIOContext *s)
905
{
906
    put_flush_packet(s);
907
    return s->buf_ptr - s->buffer;
908
}
909

    
910
/* output in a dynamic buffer */
911

    
912
typedef struct DynBuffer {
913
    int pos, size, allocated_size;
914
    uint8_t *buffer;
915
    int io_buffer_size;
916
    uint8_t io_buffer[1];
917
} DynBuffer;
918

    
919
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
920
{
921
    DynBuffer *d = opaque;
922
    unsigned new_size, new_allocated_size;
923

    
924
    /* reallocate buffer if needed */
925
    new_size = d->pos + buf_size;
926
    new_allocated_size = d->allocated_size;
927
    if(new_size < d->pos || new_size > INT_MAX/2)
928
        return -1;
929
    while (new_size > new_allocated_size) {
930
        if (!new_allocated_size)
931
            new_allocated_size = new_size;
932
        else
933
            new_allocated_size += new_allocated_size / 2 + 1;
934
    }
935

    
936
    if (new_allocated_size > d->allocated_size) {
937
        d->buffer = av_realloc(d->buffer, new_allocated_size);
938
        if(d->buffer == NULL)
939
             return AVERROR(ENOMEM);
940
        d->allocated_size = new_allocated_size;
941
    }
942
    memcpy(d->buffer + d->pos, buf, buf_size);
943
    d->pos = new_size;
944
    if (d->pos > d->size)
945
        d->size = d->pos;
946
    return buf_size;
947
}
948

    
949
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
950
{
951
    unsigned char buf1[4];
952
    int ret;
953

    
954
    /* packetized write: output the header */
955
    AV_WB32(buf1, buf_size);
956
    ret= dyn_buf_write(opaque, buf1, 4);
957
    if(ret < 0)
958
        return ret;
959

    
960
    /* then the data */
961
    return dyn_buf_write(opaque, buf, buf_size);
962
}
963

    
964
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
965
{
966
    DynBuffer *d = opaque;
967

    
968
    if (whence == SEEK_CUR)
969
        offset += d->pos;
970
    else if (whence == SEEK_END)
971
        offset += d->size;
972
    if (offset < 0 || offset > 0x7fffffffLL)
973
        return -1;
974
    d->pos = offset;
975
    return 0;
976
}
977

    
978
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
979
{
980
    DynBuffer *d;
981
    int ret;
982
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
983

    
984
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
985
        return -1;
986
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
987
    if (!d)
988
        return AVERROR(ENOMEM);
989
    *s = av_mallocz(sizeof(AVIOContext));
990
    if(!*s) {
991
        av_free(d);
992
        return AVERROR(ENOMEM);
993
    }
994
    d->io_buffer_size = io_buffer_size;
995
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
996
                        1, d, NULL,
997
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
998
                        max_packet_size ? NULL : dyn_buf_seek);
999
    if (ret == 0) {
1000
        (*s)->max_packet_size = max_packet_size;
1001
    } else {
1002
        av_free(d);
1003
        av_freep(s);
1004
    }
1005
    return ret;
1006
}
1007

    
1008
int url_open_dyn_buf(AVIOContext **s)
1009
{
1010
    return url_open_dyn_buf_internal(s, 0);
1011
}
1012

    
1013
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1014
{
1015
    if (max_packet_size <= 0)
1016
        return -1;
1017
    return url_open_dyn_buf_internal(s, max_packet_size);
1018
}
1019

    
1020
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1021
{
1022
    DynBuffer *d = s->opaque;
1023
    int size;
1024
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1025
    int padding = 0;
1026

    
1027
    /* don't attempt to pad fixed-size packet buffers */
1028
    if (!s->max_packet_size) {
1029
        put_buffer(s, padbuf, sizeof(padbuf));
1030
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1031
    }
1032

    
1033
    put_flush_packet(s);
1034

    
1035
    *pbuffer = d->buffer;
1036
    size = d->size;
1037
    av_free(d);
1038
    av_free(s);
1039
    return size - padding;
1040
}
1041
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */