Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ b3db9cee

History | View | Annotate | Download (25.6 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

    
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
int get_byte(AVIOContext *s)
320
{
321
   return avio_r8(s);
322
}
323
int get_buffer(AVIOContext *s, unsigned char *buf, int size)
324
{
325
    return avio_read(s, buf, size);
326
}
327
int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size)
328
{
329
    return ffio_read_partial(s, buf, size);
330
}
331
#endif
332

    
333
int avio_put_str(AVIOContext *s, const char *str)
334
{
335
    int len = 1;
336
    if (str) {
337
        len += strlen(str);
338
        put_buffer(s, (const unsigned char *) str, len);
339
    } else
340
        put_byte(s, 0);
341
    return len;
342
}
343

    
344
int avio_put_str16le(AVIOContext *s, const char *str)
345
{
346
    const uint8_t *q = str;
347
    int ret = 0;
348

    
349
    while (*q) {
350
        uint32_t ch;
351
        uint16_t tmp;
352

    
353
        GET_UTF8(ch, *q++, break;)
354
        PUT_UTF16(ch, tmp, put_le16(s, tmp);ret += 2;)
355
    }
356
    put_le16(s, 0);
357
    ret += 2;
358
    return ret;
359
}
360

    
361
int ff_get_v_length(uint64_t val){
362
    int i=1;
363

    
364
    while(val>>=7)
365
        i++;
366

    
367
    return i;
368
}
369

    
370
void ff_put_v(AVIOContext *bc, uint64_t val){
371
    int i= ff_get_v_length(val);
372

    
373
    while(--i>0)
374
        put_byte(bc, 128 | (val>>(7*i)));
375

    
376
    put_byte(bc, val&127);
377
}
378

    
379
void put_le64(AVIOContext *s, uint64_t val)
380
{
381
    put_le32(s, (uint32_t)(val & 0xffffffff));
382
    put_le32(s, (uint32_t)(val >> 32));
383
}
384

    
385
void put_be64(AVIOContext *s, uint64_t val)
386
{
387
    put_be32(s, (uint32_t)(val >> 32));
388
    put_be32(s, (uint32_t)(val & 0xffffffff));
389
}
390

    
391
void put_le16(AVIOContext *s, unsigned int val)
392
{
393
    put_byte(s, val);
394
    put_byte(s, val >> 8);
395
}
396

    
397
void put_be16(AVIOContext *s, unsigned int val)
398
{
399
    put_byte(s, val >> 8);
400
    put_byte(s, val);
401
}
402

    
403
void put_le24(AVIOContext *s, unsigned int val)
404
{
405
    put_le16(s, val & 0xffff);
406
    put_byte(s, val >> 16);
407
}
408

    
409
void put_be24(AVIOContext *s, unsigned int val)
410
{
411
    put_be16(s, val >> 8);
412
    put_byte(s, val);
413
}
414

    
415
void put_tag(AVIOContext *s, const char *tag)
416
{
417
    while (*tag) {
418
        put_byte(s, *tag++);
419
    }
420
}
421

    
422
/* Input stream */
423

    
424
static void fill_buffer(AVIOContext *s)
425
{
426
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
427
    int len= s->buffer_size - (dst - s->buffer);
428
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
429

    
430
    /* no need to do anything if EOF already reached */
431
    if (s->eof_reached)
432
        return;
433

    
434
    if(s->update_checksum && dst == s->buffer){
435
        if(s->buf_end > s->checksum_ptr)
436
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
437
        s->checksum_ptr= s->buffer;
438
    }
439

    
440
    /* make buffer smaller in case it ended up large after probing */
441
    if (s->buffer_size > max_buffer_size) {
442
        url_setbufsize(s, max_buffer_size);
443

    
444
        s->checksum_ptr = dst = s->buffer;
445
        len = s->buffer_size;
446
    }
447

    
448
    if(s->read_packet)
449
        len = s->read_packet(s->opaque, dst, len);
450
    else
451
        len = 0;
452
    if (len <= 0) {
453
        /* do not modify buffer if EOF reached so that a seek back can
454
           be done without rereading data */
455
        s->eof_reached = 1;
456
        if(len<0)
457
            s->error= len;
458
    } else {
459
        s->pos += len;
460
        s->buf_ptr = dst;
461
        s->buf_end = dst + len;
462
    }
463
}
464

    
465
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
466
                                    unsigned int len)
467
{
468
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
469
}
470

    
471
unsigned long get_checksum(AVIOContext *s)
472
{
473
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
474
    s->update_checksum= NULL;
475
    return s->checksum;
476
}
477

    
478
void init_checksum(AVIOContext *s,
479
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
480
                   unsigned long checksum)
481
{
482
    s->update_checksum= update_checksum;
483
    if(s->update_checksum){
484
        s->checksum= checksum;
485
        s->checksum_ptr= s->buf_ptr;
486
    }
487
}
488

    
489
/* XXX: put an inline version */
490
int avio_r8(AVIOContext *s)
491
{
492
    if (s->buf_ptr >= s->buf_end)
493
        fill_buffer(s);
494
    if (s->buf_ptr < s->buf_end)
495
        return *s->buf_ptr++;
496
    return 0;
497
}
498

    
499
int url_fgetc(AVIOContext *s)
500
{
501
    if (s->buf_ptr >= s->buf_end)
502
        fill_buffer(s);
503
    if (s->buf_ptr < s->buf_end)
504
        return *s->buf_ptr++;
505
    return URL_EOF;
506
}
507

    
508
int avio_read(AVIOContext *s, unsigned char *buf, int size)
509
{
510
    int len, size1;
511

    
512
    size1 = size;
513
    while (size > 0) {
514
        len = s->buf_end - s->buf_ptr;
515
        if (len > size)
516
            len = size;
517
        if (len == 0) {
518
            if(size > s->buffer_size && !s->update_checksum){
519
                if(s->read_packet)
520
                    len = s->read_packet(s->opaque, buf, size);
521
                if (len <= 0) {
522
                    /* do not modify buffer if EOF reached so that a seek back can
523
                    be done without rereading data */
524
                    s->eof_reached = 1;
525
                    if(len<0)
526
                        s->error= len;
527
                    break;
528
                } else {
529
                    s->pos += len;
530
                    size -= len;
531
                    buf += len;
532
                    s->buf_ptr = s->buffer;
533
                    s->buf_end = s->buffer/* + len*/;
534
                }
535
            }else{
536
                fill_buffer(s);
537
                len = s->buf_end - s->buf_ptr;
538
                if (len == 0)
539
                    break;
540
            }
541
        } else {
542
            memcpy(buf, s->buf_ptr, len);
543
            buf += len;
544
            s->buf_ptr += len;
545
            size -= len;
546
        }
547
    }
548
    if (size1 == size) {
549
        if (url_ferror(s)) return url_ferror(s);
550
        if (url_feof(s))   return AVERROR_EOF;
551
    }
552
    return size1 - size;
553
}
554

    
555
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
556
{
557
    int len;
558

    
559
    if(size<0)
560
        return -1;
561

    
562
    len = s->buf_end - s->buf_ptr;
563
    if (len == 0) {
564
        fill_buffer(s);
565
        len = s->buf_end - s->buf_ptr;
566
    }
567
    if (len > size)
568
        len = size;
569
    memcpy(buf, s->buf_ptr, len);
570
    s->buf_ptr += len;
571
    if (!len) {
572
        if (url_ferror(s)) return url_ferror(s);
573
        if (url_feof(s))   return AVERROR_EOF;
574
    }
575
    return len;
576
}
577

    
578
unsigned int avio_rl16(AVIOContext *s)
579
{
580
    unsigned int val;
581
    val = avio_r8(s);
582
    val |= avio_r8(s) << 8;
583
    return val;
584
}
585

    
586
unsigned int avio_rl24(AVIOContext *s)
587
{
588
    unsigned int val;
589
    val = avio_rl16(s);
590
    val |= avio_r8(s) << 16;
591
    return val;
592
}
593

    
594
unsigned int avio_rl32(AVIOContext *s)
595
{
596
    unsigned int val;
597
    val = avio_rl16(s);
598
    val |= avio_rl16(s) << 16;
599
    return val;
600
}
601

    
602
uint64_t avio_rl64(AVIOContext *s)
603
{
604
    uint64_t val;
605
    val = (uint64_t)avio_rl32(s);
606
    val |= (uint64_t)avio_rl32(s) << 32;
607
    return val;
608
}
609

    
610
unsigned int avio_rb16(AVIOContext *s)
611
{
612
    unsigned int val;
613
    val = avio_r8(s) << 8;
614
    val |= avio_r8(s);
615
    return val;
616
}
617

    
618
unsigned int avio_rb24(AVIOContext *s)
619
{
620
    unsigned int val;
621
    val = avio_rb16(s) << 8;
622
    val |= avio_r8(s);
623
    return val;
624
}
625
unsigned int avio_rb32(AVIOContext *s)
626
{
627
    unsigned int val;
628
    val = avio_rb16(s) << 16;
629
    val |= avio_rb16(s);
630
    return val;
631
}
632

    
633
char *get_strz(AVIOContext *s, char *buf, int maxlen)
634
{
635
    int i = 0;
636
    char c;
637

    
638
    while ((c = avio_r8(s))) {
639
        if (i < maxlen-1)
640
            buf[i++] = c;
641
    }
642

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

    
645
    return buf;
646
}
647

    
648
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
649
{
650
    int i = 0;
651
    char c;
652

    
653
    do {
654
        c = avio_r8(s);
655
        if (c && i < maxlen-1)
656
            buf[i++] = c;
657
    } while (c != '\n' && c);
658

    
659
    buf[i] = 0;
660
    return i;
661
}
662

    
663
#define GET_STR16(type, read) \
664
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
665
{\
666
    char* q = buf;\
667
    int ret = 0;\
668
    while (ret + 1 < maxlen) {\
669
        uint8_t tmp;\
670
        uint32_t ch;\
671
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
672
        if (!ch)\
673
            break;\
674
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
675
    }\
676
    *q = 0;\
677
    return ret;\
678
}\
679

    
680
GET_STR16(le, avio_rl16)
681
GET_STR16(be, avio_rb16)
682

    
683
#undef GET_STR16
684

    
685
uint64_t avio_rb64(AVIOContext *s)
686
{
687
    uint64_t val;
688
    val = (uint64_t)avio_rb32(s) << 32;
689
    val |= (uint64_t)avio_rb32(s);
690
    return val;
691
}
692

    
693
uint64_t ff_get_v(AVIOContext *bc){
694
    uint64_t val = 0;
695
    int tmp;
696

    
697
    do{
698
        tmp = avio_r8(bc);
699
        val= (val<<7) + (tmp&127);
700
    }while(tmp&128);
701
    return val;
702
}
703

    
704
int url_fdopen(AVIOContext **s, URLContext *h)
705
{
706
    uint8_t *buffer;
707
    int buffer_size, max_packet_size;
708

    
709
    max_packet_size = url_get_max_packet_size(h);
710
    if (max_packet_size) {
711
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
712
    } else {
713
        buffer_size = IO_BUFFER_SIZE;
714
    }
715
    buffer = av_malloc(buffer_size);
716
    if (!buffer)
717
        return AVERROR(ENOMEM);
718

    
719
    *s = av_mallocz(sizeof(AVIOContext));
720
    if(!*s) {
721
        av_free(buffer);
722
        return AVERROR(ENOMEM);
723
    }
724

    
725
    if (ffio_init_context(*s, buffer, buffer_size,
726
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
727
                      url_read, url_write, url_seek) < 0) {
728
        av_free(buffer);
729
        av_freep(s);
730
        return AVERROR(EIO);
731
    }
732
    (*s)->is_streamed = h->is_streamed;
733
    (*s)->max_packet_size = max_packet_size;
734
    if(h->prot) {
735
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
736
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
737
    }
738
    return 0;
739
}
740

    
741
int url_setbufsize(AVIOContext *s, int buf_size)
742
{
743
    uint8_t *buffer;
744
    buffer = av_malloc(buf_size);
745
    if (!buffer)
746
        return AVERROR(ENOMEM);
747

    
748
    av_free(s->buffer);
749
    s->buffer = buffer;
750
    s->buffer_size = buf_size;
751
    s->buf_ptr = buffer;
752
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
753
    return 0;
754
}
755

    
756
#if FF_API_URL_RESETBUF
757
int url_resetbuf(AVIOContext *s, int flags)
758
#else
759
static int url_resetbuf(AVIOContext *s, int flags)
760
#endif
761
{
762
#if FF_API_URL_RESETBUF
763
    if (flags & URL_RDWR)
764
        return AVERROR(EINVAL);
765
#else
766
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
767
#endif
768

    
769
    if (flags & URL_WRONLY) {
770
        s->buf_end = s->buffer + s->buffer_size;
771
        s->write_flag = 1;
772
    } else {
773
        s->buf_end = s->buffer;
774
        s->write_flag = 0;
775
    }
776
    return 0;
777
}
778

    
779
int ff_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
780
{
781
    int64_t buffer_start;
782
    int buffer_size;
783
    int overlap, new_size, alloc_size;
784

    
785
    if (s->write_flag)
786
        return AVERROR(EINVAL);
787

    
788
    buffer_size = s->buf_end - s->buffer;
789

    
790
    /* the buffers must touch or overlap */
791
    if ((buffer_start = s->pos - buffer_size) > buf_size)
792
        return AVERROR(EINVAL);
793

    
794
    overlap = buf_size - buffer_start;
795
    new_size = buf_size + buffer_size - overlap;
796

    
797
    alloc_size = FFMAX(s->buffer_size, new_size);
798
    if (alloc_size > buf_size)
799
        if (!(buf = av_realloc(buf, alloc_size)))
800
            return AVERROR(ENOMEM);
801

    
802
    if (new_size > buf_size) {
803
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
804
        buf_size = new_size;
805
    }
806

    
807
    av_free(s->buffer);
808
    s->buf_ptr = s->buffer = buf;
809
    s->buffer_size = alloc_size;
810
    s->pos = buf_size;
811
    s->buf_end = s->buf_ptr + buf_size;
812
    s->eof_reached = 0;
813
    s->must_flush = 0;
814

    
815
    return 0;
816
}
817

    
818
int url_fopen(AVIOContext **s, const char *filename, int flags)
819
{
820
    URLContext *h;
821
    int err;
822

    
823
    err = url_open(&h, filename, flags);
824
    if (err < 0)
825
        return err;
826
    err = url_fdopen(s, h);
827
    if (err < 0) {
828
        url_close(h);
829
        return err;
830
    }
831
    return 0;
832
}
833

    
834
int url_fclose(AVIOContext *s)
835
{
836
    URLContext *h = s->opaque;
837

    
838
    av_free(s->buffer);
839
    av_free(s);
840
    return url_close(h);
841
}
842

    
843
URLContext *url_fileno(AVIOContext *s)
844
{
845
    return s->opaque;
846
}
847

    
848
#if CONFIG_MUXERS
849
int url_fprintf(AVIOContext *s, const char *fmt, ...)
850
{
851
    va_list ap;
852
    char buf[4096];
853
    int ret;
854

    
855
    va_start(ap, fmt);
856
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
857
    va_end(ap);
858
    put_buffer(s, buf, strlen(buf));
859
    return ret;
860
}
861
#endif //CONFIG_MUXERS
862

    
863
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
864
{
865
    int c;
866
    char *q;
867

    
868
    c = url_fgetc(s);
869
    if (c == EOF)
870
        return NULL;
871
    q = buf;
872
    for(;;) {
873
        if (c == EOF || c == '\n')
874
            break;
875
        if ((q - buf) < buf_size - 1)
876
            *q++ = c;
877
        c = url_fgetc(s);
878
    }
879
    if (buf_size > 0)
880
        *q = '\0';
881
    return buf;
882
}
883

    
884
int url_fget_max_packet_size(AVIOContext *s)
885
{
886
    return s->max_packet_size;
887
}
888

    
889
int av_url_read_fpause(AVIOContext *s, int pause)
890
{
891
    if (!s->read_pause)
892
        return AVERROR(ENOSYS);
893
    return s->read_pause(s->opaque, pause);
894
}
895

    
896
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
897
                          int64_t timestamp, int flags)
898
{
899
    URLContext *h = s->opaque;
900
    int64_t ret;
901
    if (!s->read_seek)
902
        return AVERROR(ENOSYS);
903
    ret = s->read_seek(h, stream_index, timestamp, flags);
904
    if(ret >= 0) {
905
        int64_t pos;
906
        s->buf_ptr = s->buf_end; // Flush buffer
907
        pos = s->seek(h, 0, SEEK_CUR);
908
        if (pos >= 0)
909
            s->pos = pos;
910
        else if (pos != AVERROR(ENOSYS))
911
            ret = pos;
912
    }
913
    return ret;
914
}
915

    
916
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
917
 * back to the server even if CONFIG_MUXERS is false. */
918
#if CONFIG_MUXERS || CONFIG_NETWORK
919
/* buffer handling */
920
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
921
{
922
    int ret;
923
    *s = av_mallocz(sizeof(AVIOContext));
924
    if(!*s)
925
        return AVERROR(ENOMEM);
926
    ret = ffio_init_context(*s, buf, buf_size,
927
                        (flags & URL_WRONLY || flags & URL_RDWR),
928
                        NULL, NULL, NULL, NULL);
929
    if(ret != 0)
930
        av_freep(s);
931
    return ret;
932
}
933

    
934
int url_close_buf(AVIOContext *s)
935
{
936
    put_flush_packet(s);
937
    return s->buf_ptr - s->buffer;
938
}
939

    
940
/* output in a dynamic buffer */
941

    
942
typedef struct DynBuffer {
943
    int pos, size, allocated_size;
944
    uint8_t *buffer;
945
    int io_buffer_size;
946
    uint8_t io_buffer[1];
947
} DynBuffer;
948

    
949
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
950
{
951
    DynBuffer *d = opaque;
952
    unsigned new_size, new_allocated_size;
953

    
954
    /* reallocate buffer if needed */
955
    new_size = d->pos + buf_size;
956
    new_allocated_size = d->allocated_size;
957
    if(new_size < d->pos || new_size > INT_MAX/2)
958
        return -1;
959
    while (new_size > new_allocated_size) {
960
        if (!new_allocated_size)
961
            new_allocated_size = new_size;
962
        else
963
            new_allocated_size += new_allocated_size / 2 + 1;
964
    }
965

    
966
    if (new_allocated_size > d->allocated_size) {
967
        d->buffer = av_realloc(d->buffer, new_allocated_size);
968
        if(d->buffer == NULL)
969
             return AVERROR(ENOMEM);
970
        d->allocated_size = new_allocated_size;
971
    }
972
    memcpy(d->buffer + d->pos, buf, buf_size);
973
    d->pos = new_size;
974
    if (d->pos > d->size)
975
        d->size = d->pos;
976
    return buf_size;
977
}
978

    
979
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
980
{
981
    unsigned char buf1[4];
982
    int ret;
983

    
984
    /* packetized write: output the header */
985
    AV_WB32(buf1, buf_size);
986
    ret= dyn_buf_write(opaque, buf1, 4);
987
    if(ret < 0)
988
        return ret;
989

    
990
    /* then the data */
991
    return dyn_buf_write(opaque, buf, buf_size);
992
}
993

    
994
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
995
{
996
    DynBuffer *d = opaque;
997

    
998
    if (whence == SEEK_CUR)
999
        offset += d->pos;
1000
    else if (whence == SEEK_END)
1001
        offset += d->size;
1002
    if (offset < 0 || offset > 0x7fffffffLL)
1003
        return -1;
1004
    d->pos = offset;
1005
    return 0;
1006
}
1007

    
1008
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1009
{
1010
    DynBuffer *d;
1011
    int ret;
1012
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1013

    
1014
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1015
        return -1;
1016
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1017
    if (!d)
1018
        return AVERROR(ENOMEM);
1019
    *s = av_mallocz(sizeof(AVIOContext));
1020
    if(!*s) {
1021
        av_free(d);
1022
        return AVERROR(ENOMEM);
1023
    }
1024
    d->io_buffer_size = io_buffer_size;
1025
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
1026
                        1, d, NULL,
1027
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1028
                        max_packet_size ? NULL : dyn_buf_seek);
1029
    if (ret == 0) {
1030
        (*s)->max_packet_size = max_packet_size;
1031
    } else {
1032
        av_free(d);
1033
        av_freep(s);
1034
    }
1035
    return ret;
1036
}
1037

    
1038
int url_open_dyn_buf(AVIOContext **s)
1039
{
1040
    return url_open_dyn_buf_internal(s, 0);
1041
}
1042

    
1043
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1044
{
1045
    if (max_packet_size <= 0)
1046
        return -1;
1047
    return url_open_dyn_buf_internal(s, max_packet_size);
1048
}
1049

    
1050
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1051
{
1052
    DynBuffer *d = s->opaque;
1053
    int size;
1054
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1055
    int padding = 0;
1056

    
1057
    /* don't attempt to pad fixed-size packet buffers */
1058
    if (!s->max_packet_size) {
1059
        put_buffer(s, padbuf, sizeof(padbuf));
1060
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1061
    }
1062

    
1063
    put_flush_packet(s);
1064

    
1065
    *pbuffer = d->buffer;
1066
    size = d->size;
1067
    av_free(d);
1068
    av_free(s);
1069
    return size - padding;
1070
}
1071
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */