Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 0ac8e2bf

History | View | Annotate | Download (26.1 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
#endif
360

    
361
int avio_put_str(AVIOContext *s, const char *str)
362
{
363
    int len = 1;
364
    if (str) {
365
        len += strlen(str);
366
        avio_write(s, (const unsigned char *) str, len);
367
    } else
368
        avio_w8(s, 0);
369
    return len;
370
}
371

    
372
int avio_put_str16le(AVIOContext *s, const char *str)
373
{
374
    const uint8_t *q = str;
375
    int ret = 0;
376

    
377
    while (*q) {
378
        uint32_t ch;
379
        uint16_t tmp;
380

    
381
        GET_UTF8(ch, *q++, break;)
382
        PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
383
    }
384
    avio_wl16(s, 0);
385
    ret += 2;
386
    return ret;
387
}
388

    
389
int ff_get_v_length(uint64_t val){
390
    int i=1;
391

    
392
    while(val>>=7)
393
        i++;
394

    
395
    return i;
396
}
397

    
398
void ff_put_v(AVIOContext *bc, uint64_t val){
399
    int i= ff_get_v_length(val);
400

    
401
    while(--i>0)
402
        avio_w8(bc, 128 | (val>>(7*i)));
403

    
404
    avio_w8(bc, val&127);
405
}
406

    
407
void avio_wl64(AVIOContext *s, uint64_t val)
408
{
409
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
410
    avio_wl32(s, (uint32_t)(val >> 32));
411
}
412

    
413
void avio_wb64(AVIOContext *s, uint64_t val)
414
{
415
    avio_wb32(s, (uint32_t)(val >> 32));
416
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
417
}
418

    
419
void avio_wl16(AVIOContext *s, unsigned int val)
420
{
421
    avio_w8(s, val);
422
    avio_w8(s, val >> 8);
423
}
424

    
425
void avio_wb16(AVIOContext *s, unsigned int val)
426
{
427
    avio_w8(s, val >> 8);
428
    avio_w8(s, val);
429
}
430

    
431
void avio_wl24(AVIOContext *s, unsigned int val)
432
{
433
    avio_wl16(s, val & 0xffff);
434
    avio_w8(s, val >> 16);
435
}
436

    
437
void avio_wb24(AVIOContext *s, unsigned int val)
438
{
439
    avio_wb16(s, val >> 8);
440
    avio_w8(s, val);
441
}
442

    
443
void put_tag(AVIOContext *s, const char *tag)
444
{
445
    while (*tag) {
446
        avio_w8(s, *tag++);
447
    }
448
}
449

    
450
/* Input stream */
451

    
452
static void fill_buffer(AVIOContext *s)
453
{
454
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
455
    int len= s->buffer_size - (dst - s->buffer);
456
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
457

    
458
    /* no need to do anything if EOF already reached */
459
    if (s->eof_reached)
460
        return;
461

    
462
    if(s->update_checksum && dst == s->buffer){
463
        if(s->buf_end > s->checksum_ptr)
464
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
465
        s->checksum_ptr= s->buffer;
466
    }
467

    
468
    /* make buffer smaller in case it ended up large after probing */
469
    if (s->buffer_size > max_buffer_size) {
470
        url_setbufsize(s, max_buffer_size);
471

    
472
        s->checksum_ptr = dst = s->buffer;
473
        len = s->buffer_size;
474
    }
475

    
476
    if(s->read_packet)
477
        len = s->read_packet(s->opaque, dst, len);
478
    else
479
        len = 0;
480
    if (len <= 0) {
481
        /* do not modify buffer if EOF reached so that a seek back can
482
           be done without rereading data */
483
        s->eof_reached = 1;
484
        if(len<0)
485
            s->error= len;
486
    } else {
487
        s->pos += len;
488
        s->buf_ptr = dst;
489
        s->buf_end = dst + len;
490
    }
491
}
492

    
493
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
494
                                    unsigned int len)
495
{
496
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
497
}
498

    
499
unsigned long get_checksum(AVIOContext *s)
500
{
501
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
502
    s->update_checksum= NULL;
503
    return s->checksum;
504
}
505

    
506
void init_checksum(AVIOContext *s,
507
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
508
                   unsigned long checksum)
509
{
510
    s->update_checksum= update_checksum;
511
    if(s->update_checksum){
512
        s->checksum= checksum;
513
        s->checksum_ptr= s->buf_ptr;
514
    }
515
}
516

    
517
/* XXX: put an inline version */
518
int avio_r8(AVIOContext *s)
519
{
520
    if (s->buf_ptr >= s->buf_end)
521
        fill_buffer(s);
522
    if (s->buf_ptr < s->buf_end)
523
        return *s->buf_ptr++;
524
    return 0;
525
}
526

    
527
int url_fgetc(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 URL_EOF;
534
}
535

    
536
int avio_read(AVIOContext *s, unsigned char *buf, int size)
537
{
538
    int len, size1;
539

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

    
583
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
584
{
585
    int len;
586

    
587
    if(size<0)
588
        return -1;
589

    
590
    len = s->buf_end - s->buf_ptr;
591
    if (len == 0) {
592
        fill_buffer(s);
593
        len = s->buf_end - s->buf_ptr;
594
    }
595
    if (len > size)
596
        len = size;
597
    memcpy(buf, s->buf_ptr, len);
598
    s->buf_ptr += len;
599
    if (!len) {
600
        if (url_ferror(s)) return url_ferror(s);
601
        if (url_feof(s))   return AVERROR_EOF;
602
    }
603
    return len;
604
}
605

    
606
unsigned int avio_rl16(AVIOContext *s)
607
{
608
    unsigned int val;
609
    val = avio_r8(s);
610
    val |= avio_r8(s) << 8;
611
    return val;
612
}
613

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

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

    
630
uint64_t avio_rl64(AVIOContext *s)
631
{
632
    uint64_t val;
633
    val = (uint64_t)avio_rl32(s);
634
    val |= (uint64_t)avio_rl32(s) << 32;
635
    return val;
636
}
637

    
638
unsigned int avio_rb16(AVIOContext *s)
639
{
640
    unsigned int val;
641
    val = avio_r8(s) << 8;
642
    val |= avio_r8(s);
643
    return val;
644
}
645

    
646
unsigned int avio_rb24(AVIOContext *s)
647
{
648
    unsigned int val;
649
    val = avio_rb16(s) << 8;
650
    val |= avio_r8(s);
651
    return val;
652
}
653
unsigned int avio_rb32(AVIOContext *s)
654
{
655
    unsigned int val;
656
    val = avio_rb16(s) << 16;
657
    val |= avio_rb16(s);
658
    return val;
659
}
660

    
661
char *get_strz(AVIOContext *s, char *buf, int maxlen)
662
{
663
    int i = 0;
664
    char c;
665

    
666
    while ((c = avio_r8(s))) {
667
        if (i < maxlen-1)
668
            buf[i++] = c;
669
    }
670

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

    
673
    return buf;
674
}
675

    
676
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
677
{
678
    int i = 0;
679
    char c;
680

    
681
    do {
682
        c = avio_r8(s);
683
        if (c && i < maxlen-1)
684
            buf[i++] = c;
685
    } while (c != '\n' && c);
686

    
687
    buf[i] = 0;
688
    return i;
689
}
690

    
691
#define GET_STR16(type, read) \
692
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
693
{\
694
    char* q = buf;\
695
    int ret = 0;\
696
    while (ret + 1 < maxlen) {\
697
        uint8_t tmp;\
698
        uint32_t ch;\
699
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
700
        if (!ch)\
701
            break;\
702
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
703
    }\
704
    *q = 0;\
705
    return ret;\
706
}\
707

    
708
GET_STR16(le, avio_rl16)
709
GET_STR16(be, avio_rb16)
710

    
711
#undef GET_STR16
712

    
713
uint64_t avio_rb64(AVIOContext *s)
714
{
715
    uint64_t val;
716
    val = (uint64_t)avio_rb32(s) << 32;
717
    val |= (uint64_t)avio_rb32(s);
718
    return val;
719
}
720

    
721
uint64_t ff_get_v(AVIOContext *bc){
722
    uint64_t val = 0;
723
    int tmp;
724

    
725
    do{
726
        tmp = avio_r8(bc);
727
        val= (val<<7) + (tmp&127);
728
    }while(tmp&128);
729
    return val;
730
}
731

    
732
int url_fdopen(AVIOContext **s, URLContext *h)
733
{
734
    uint8_t *buffer;
735
    int buffer_size, max_packet_size;
736

    
737
    max_packet_size = url_get_max_packet_size(h);
738
    if (max_packet_size) {
739
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
740
    } else {
741
        buffer_size = IO_BUFFER_SIZE;
742
    }
743
    buffer = av_malloc(buffer_size);
744
    if (!buffer)
745
        return AVERROR(ENOMEM);
746

    
747
    *s = av_mallocz(sizeof(AVIOContext));
748
    if(!*s) {
749
        av_free(buffer);
750
        return AVERROR(ENOMEM);
751
    }
752

    
753
    if (ffio_init_context(*s, buffer, buffer_size,
754
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
755
                      url_read, url_write, url_seek) < 0) {
756
        av_free(buffer);
757
        av_freep(s);
758
        return AVERROR(EIO);
759
    }
760
    (*s)->is_streamed = h->is_streamed;
761
    (*s)->max_packet_size = max_packet_size;
762
    if(h->prot) {
763
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
764
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
765
    }
766
    return 0;
767
}
768

    
769
int url_setbufsize(AVIOContext *s, int buf_size)
770
{
771
    uint8_t *buffer;
772
    buffer = av_malloc(buf_size);
773
    if (!buffer)
774
        return AVERROR(ENOMEM);
775

    
776
    av_free(s->buffer);
777
    s->buffer = buffer;
778
    s->buffer_size = buf_size;
779
    s->buf_ptr = buffer;
780
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
781
    return 0;
782
}
783

    
784
#if FF_API_URL_RESETBUF
785
int url_resetbuf(AVIOContext *s, int flags)
786
#else
787
static int url_resetbuf(AVIOContext *s, int flags)
788
#endif
789
{
790
#if FF_API_URL_RESETBUF
791
    if (flags & URL_RDWR)
792
        return AVERROR(EINVAL);
793
#else
794
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
795
#endif
796

    
797
    if (flags & URL_WRONLY) {
798
        s->buf_end = s->buffer + s->buffer_size;
799
        s->write_flag = 1;
800
    } else {
801
        s->buf_end = s->buffer;
802
        s->write_flag = 0;
803
    }
804
    return 0;
805
}
806

    
807
int ff_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
808
{
809
    int64_t buffer_start;
810
    int buffer_size;
811
    int overlap, new_size, alloc_size;
812

    
813
    if (s->write_flag)
814
        return AVERROR(EINVAL);
815

    
816
    buffer_size = s->buf_end - s->buffer;
817

    
818
    /* the buffers must touch or overlap */
819
    if ((buffer_start = s->pos - buffer_size) > buf_size)
820
        return AVERROR(EINVAL);
821

    
822
    overlap = buf_size - buffer_start;
823
    new_size = buf_size + buffer_size - overlap;
824

    
825
    alloc_size = FFMAX(s->buffer_size, new_size);
826
    if (alloc_size > buf_size)
827
        if (!(buf = av_realloc(buf, alloc_size)))
828
            return AVERROR(ENOMEM);
829

    
830
    if (new_size > buf_size) {
831
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
832
        buf_size = new_size;
833
    }
834

    
835
    av_free(s->buffer);
836
    s->buf_ptr = s->buffer = buf;
837
    s->buffer_size = alloc_size;
838
    s->pos = buf_size;
839
    s->buf_end = s->buf_ptr + buf_size;
840
    s->eof_reached = 0;
841
    s->must_flush = 0;
842

    
843
    return 0;
844
}
845

    
846
int url_fopen(AVIOContext **s, const char *filename, int flags)
847
{
848
    URLContext *h;
849
    int err;
850

    
851
    err = url_open(&h, filename, flags);
852
    if (err < 0)
853
        return err;
854
    err = url_fdopen(s, h);
855
    if (err < 0) {
856
        url_close(h);
857
        return err;
858
    }
859
    return 0;
860
}
861

    
862
int url_fclose(AVIOContext *s)
863
{
864
    URLContext *h = s->opaque;
865

    
866
    av_free(s->buffer);
867
    av_free(s);
868
    return url_close(h);
869
}
870

    
871
URLContext *url_fileno(AVIOContext *s)
872
{
873
    return s->opaque;
874
}
875

    
876
#if CONFIG_MUXERS
877
int url_fprintf(AVIOContext *s, const char *fmt, ...)
878
{
879
    va_list ap;
880
    char buf[4096];
881
    int ret;
882

    
883
    va_start(ap, fmt);
884
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
885
    va_end(ap);
886
    avio_write(s, buf, strlen(buf));
887
    return ret;
888
}
889
#endif //CONFIG_MUXERS
890

    
891
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
892
{
893
    int c;
894
    char *q;
895

    
896
    c = url_fgetc(s);
897
    if (c == EOF)
898
        return NULL;
899
    q = buf;
900
    for(;;) {
901
        if (c == EOF || c == '\n')
902
            break;
903
        if ((q - buf) < buf_size - 1)
904
            *q++ = c;
905
        c = url_fgetc(s);
906
    }
907
    if (buf_size > 0)
908
        *q = '\0';
909
    return buf;
910
}
911

    
912
int url_fget_max_packet_size(AVIOContext *s)
913
{
914
    return s->max_packet_size;
915
}
916

    
917
int av_url_read_fpause(AVIOContext *s, int pause)
918
{
919
    if (!s->read_pause)
920
        return AVERROR(ENOSYS);
921
    return s->read_pause(s->opaque, pause);
922
}
923

    
924
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
925
                          int64_t timestamp, int flags)
926
{
927
    URLContext *h = s->opaque;
928
    int64_t ret;
929
    if (!s->read_seek)
930
        return AVERROR(ENOSYS);
931
    ret = s->read_seek(h, stream_index, timestamp, flags);
932
    if(ret >= 0) {
933
        int64_t pos;
934
        s->buf_ptr = s->buf_end; // Flush buffer
935
        pos = s->seek(h, 0, SEEK_CUR);
936
        if (pos >= 0)
937
            s->pos = pos;
938
        else if (pos != AVERROR(ENOSYS))
939
            ret = pos;
940
    }
941
    return ret;
942
}
943

    
944
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
945
 * back to the server even if CONFIG_MUXERS is false. */
946
#if CONFIG_MUXERS || CONFIG_NETWORK
947
/* buffer handling */
948
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
949
{
950
    int ret;
951
    *s = av_mallocz(sizeof(AVIOContext));
952
    if(!*s)
953
        return AVERROR(ENOMEM);
954
    ret = ffio_init_context(*s, buf, buf_size,
955
                        (flags & URL_WRONLY || flags & URL_RDWR),
956
                        NULL, NULL, NULL, NULL);
957
    if(ret != 0)
958
        av_freep(s);
959
    return ret;
960
}
961

    
962
int url_close_buf(AVIOContext *s)
963
{
964
    put_flush_packet(s);
965
    return s->buf_ptr - s->buffer;
966
}
967

    
968
/* output in a dynamic buffer */
969

    
970
typedef struct DynBuffer {
971
    int pos, size, allocated_size;
972
    uint8_t *buffer;
973
    int io_buffer_size;
974
    uint8_t io_buffer[1];
975
} DynBuffer;
976

    
977
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
978
{
979
    DynBuffer *d = opaque;
980
    unsigned new_size, new_allocated_size;
981

    
982
    /* reallocate buffer if needed */
983
    new_size = d->pos + buf_size;
984
    new_allocated_size = d->allocated_size;
985
    if(new_size < d->pos || new_size > INT_MAX/2)
986
        return -1;
987
    while (new_size > new_allocated_size) {
988
        if (!new_allocated_size)
989
            new_allocated_size = new_size;
990
        else
991
            new_allocated_size += new_allocated_size / 2 + 1;
992
    }
993

    
994
    if (new_allocated_size > d->allocated_size) {
995
        d->buffer = av_realloc(d->buffer, new_allocated_size);
996
        if(d->buffer == NULL)
997
             return AVERROR(ENOMEM);
998
        d->allocated_size = new_allocated_size;
999
    }
1000
    memcpy(d->buffer + d->pos, buf, buf_size);
1001
    d->pos = new_size;
1002
    if (d->pos > d->size)
1003
        d->size = d->pos;
1004
    return buf_size;
1005
}
1006

    
1007
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1008
{
1009
    unsigned char buf1[4];
1010
    int ret;
1011

    
1012
    /* packetized write: output the header */
1013
    AV_WB32(buf1, buf_size);
1014
    ret= dyn_buf_write(opaque, buf1, 4);
1015
    if(ret < 0)
1016
        return ret;
1017

    
1018
    /* then the data */
1019
    return dyn_buf_write(opaque, buf, buf_size);
1020
}
1021

    
1022
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1023
{
1024
    DynBuffer *d = opaque;
1025

    
1026
    if (whence == SEEK_CUR)
1027
        offset += d->pos;
1028
    else if (whence == SEEK_END)
1029
        offset += d->size;
1030
    if (offset < 0 || offset > 0x7fffffffLL)
1031
        return -1;
1032
    d->pos = offset;
1033
    return 0;
1034
}
1035

    
1036
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1037
{
1038
    DynBuffer *d;
1039
    int ret;
1040
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1041

    
1042
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1043
        return -1;
1044
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1045
    if (!d)
1046
        return AVERROR(ENOMEM);
1047
    *s = av_mallocz(sizeof(AVIOContext));
1048
    if(!*s) {
1049
        av_free(d);
1050
        return AVERROR(ENOMEM);
1051
    }
1052
    d->io_buffer_size = io_buffer_size;
1053
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
1054
                        1, d, NULL,
1055
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1056
                        max_packet_size ? NULL : dyn_buf_seek);
1057
    if (ret == 0) {
1058
        (*s)->max_packet_size = max_packet_size;
1059
    } else {
1060
        av_free(d);
1061
        av_freep(s);
1062
    }
1063
    return ret;
1064
}
1065

    
1066
int url_open_dyn_buf(AVIOContext **s)
1067
{
1068
    return url_open_dyn_buf_internal(s, 0);
1069
}
1070

    
1071
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1072
{
1073
    if (max_packet_size <= 0)
1074
        return -1;
1075
    return url_open_dyn_buf_internal(s, max_packet_size);
1076
}
1077

    
1078
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1079
{
1080
    DynBuffer *d = s->opaque;
1081
    int size;
1082
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1083
    int padding = 0;
1084

    
1085
    /* don't attempt to pad fixed-size packet buffers */
1086
    if (!s->max_packet_size) {
1087
        avio_write(s, padbuf, sizeof(padbuf));
1088
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1089
    }
1090

    
1091
    put_flush_packet(s);
1092

    
1093
    *pbuffer = d->buffer;
1094
    size = d->size;
1095
    av_free(d);
1096
    av_free(s);
1097
    return size - padding;
1098
}
1099
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */