Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ e910a77b

History | View | Annotate | Download (23.2 KB)

1
/*
2
 * Buffered I/O for ffmpeg system
3
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "libavutil/crc.h"
23
#include "libavutil/intreadwrite.h"
24
#include "avformat.h"
25
#include "avio.h"
26
#include "internal.h"
27
#include <stdarg.h>
28

    
29
#define IO_BUFFER_SIZE 32768
30

    
31
/**
32
 * Do seeks within this distance ahead of the current buffer by skipping
33
 * data instead of calling the protocol seek function, for seekable
34
 * protocols.
35
 */
36
#define SHORT_SEEK_THRESHOLD 4096
37

    
38
static void fill_buffer(ByteIOContext *s);
39
#if !FF_API_URL_RESETBUF
40
static int url_resetbuf(ByteIOContext *s, int flags);
41
#endif
42

    
43
int init_put_byte(ByteIOContext *s,
44
                  unsigned char *buffer,
45
                  int buffer_size,
46
                  int write_flag,
47
                  void *opaque,
48
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
49
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
50
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
51
{
52
    s->buffer = buffer;
53
    s->buffer_size = buffer_size;
54
    s->buf_ptr = buffer;
55
    s->opaque = opaque;
56
    url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);
57
    s->write_packet = write_packet;
58
    s->read_packet = read_packet;
59
    s->seek = seek;
60
    s->pos = 0;
61
    s->must_flush = 0;
62
    s->eof_reached = 0;
63
    s->error = 0;
64
    s->is_streamed = 0;
65
    s->max_packet_size = 0;
66
    s->update_checksum= NULL;
67
    if(!read_packet && !write_flag){
68
        s->pos = buffer_size;
69
        s->buf_end = s->buffer + buffer_size;
70
    }
71
    s->read_pause = NULL;
72
    s->read_seek  = NULL;
73
    return 0;
74
}
75

    
76
ByteIOContext *av_alloc_put_byte(
77
                  unsigned char *buffer,
78
                  int buffer_size,
79
                  int write_flag,
80
                  void *opaque,
81
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
82
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
83
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
84
{
85
    ByteIOContext *s = av_mallocz(sizeof(ByteIOContext));
86
    init_put_byte(s, buffer, buffer_size, write_flag, opaque,
87
                  read_packet, write_packet, seek);
88
    return s;
89
}
90

    
91
static void flush_buffer(ByteIOContext *s)
92
{
93
    if (s->buf_ptr > s->buffer) {
94
        if (s->write_packet && !s->error){
95
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
96
            if(ret < 0){
97
                s->error = ret;
98
            }
99
        }
100
        if(s->update_checksum){
101
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
102
            s->checksum_ptr= s->buffer;
103
        }
104
        s->pos += s->buf_ptr - s->buffer;
105
    }
106
    s->buf_ptr = s->buffer;
107
}
108

    
109
void put_byte(ByteIOContext *s, int b)
110
{
111
    *(s->buf_ptr)++ = b;
112
    if (s->buf_ptr >= s->buf_end)
113
        flush_buffer(s);
114
}
115

    
116
void put_nbyte(ByteIOContext *s, int b, int count)
117
{
118
    while (count > 0) {
119
        int len = FFMIN(s->buf_end - s->buf_ptr, count);
120
        memset(s->buf_ptr, b, len);
121
        s->buf_ptr += len;
122

    
123
        if (s->buf_ptr >= s->buf_end)
124
            flush_buffer(s);
125

    
126
        count -= len;
127
    }
128
}
129

    
130
void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
131
{
132
    while (size > 0) {
133
        int len = FFMIN(s->buf_end - s->buf_ptr, size);
134
        memcpy(s->buf_ptr, buf, len);
135
        s->buf_ptr += len;
136

    
137
        if (s->buf_ptr >= s->buf_end)
138
            flush_buffer(s);
139

    
140
        buf += len;
141
        size -= len;
142
    }
143
}
144

    
145
void put_flush_packet(ByteIOContext *s)
146
{
147
    flush_buffer(s);
148
    s->must_flush = 0;
149
}
150

    
151
int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence)
152
{
153
    int64_t offset1;
154
    int64_t pos;
155
    int force = whence & AVSEEK_FORCE;
156
    whence &= ~AVSEEK_FORCE;
157

    
158
    if(!s)
159
        return AVERROR(EINVAL);
160

    
161
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
162

    
163
    if (whence != SEEK_CUR && whence != SEEK_SET)
164
        return AVERROR(EINVAL);
165

    
166
    if (whence == SEEK_CUR) {
167
        offset1 = pos + (s->buf_ptr - s->buffer);
168
        if (offset == 0)
169
            return offset1;
170
        offset += offset1;
171
    }
172
    offset1 = offset - pos;
173
    if (!s->must_flush &&
174
        offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
175
        /* can do the seek inside the buffer */
176
        s->buf_ptr = s->buffer + offset1;
177
    } else if ((s->is_streamed ||
178
               offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
179
               !s->write_flag && offset1 >= 0 &&
180
              (whence != SEEK_END || force)) {
181
        while(s->pos < offset && !s->eof_reached)
182
            fill_buffer(s);
183
        if (s->eof_reached)
184
            return AVERROR_EOF;
185
        s->buf_ptr = s->buf_end + offset - s->pos;
186
    } else {
187
        int64_t res;
188

    
189
#if CONFIG_MUXERS || CONFIG_NETWORK
190
        if (s->write_flag) {
191
            flush_buffer(s);
192
            s->must_flush = 1;
193
        }
194
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
195
        if (!s->seek)
196
            return AVERROR(EPIPE);
197
        if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
198
            return res;
199
        if (!s->write_flag)
200
            s->buf_end = s->buffer;
201
        s->buf_ptr = s->buffer;
202
        s->pos = offset;
203
    }
204
    s->eof_reached = 0;
205
    return offset;
206
}
207

    
208
int url_fskip(ByteIOContext *s, int64_t offset)
209
{
210
    int64_t ret = url_fseek(s, offset, SEEK_CUR);
211
    return ret < 0 ? ret : 0;
212
}
213

    
214
int64_t url_ftell(ByteIOContext *s)
215
{
216
    return url_fseek(s, 0, SEEK_CUR);
217
}
218

    
219
int64_t url_fsize(ByteIOContext *s)
220
{
221
    int64_t size;
222

    
223
    if(!s)
224
        return AVERROR(EINVAL);
225

    
226
    if (!s->seek)
227
        return AVERROR(ENOSYS);
228
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
229
    if(size<0){
230
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
231
            return size;
232
        size++;
233
        s->seek(s->opaque, s->pos, SEEK_SET);
234
    }
235
    return size;
236
}
237

    
238
int url_feof(ByteIOContext *s)
239
{
240
    if(!s)
241
        return 0;
242
    return s->eof_reached;
243
}
244

    
245
int url_ferror(ByteIOContext *s)
246
{
247
    if(!s)
248
        return 0;
249
    return s->error;
250
}
251

    
252
void put_le32(ByteIOContext *s, unsigned int val)
253
{
254
    put_byte(s, val);
255
    put_byte(s, val >> 8);
256
    put_byte(s, val >> 16);
257
    put_byte(s, val >> 24);
258
}
259

    
260
void put_be32(ByteIOContext *s, unsigned int val)
261
{
262
    put_byte(s, val >> 24);
263
    put_byte(s, val >> 16);
264
    put_byte(s, val >> 8);
265
    put_byte(s, val);
266
}
267

    
268
#if FF_API_OLD_AVIO
269
void put_strz(ByteIOContext *s, const char *str)
270
{
271
    avio_put_str(s, str);
272
}
273
#endif
274

    
275
int avio_put_str(ByteIOContext *s, const char *str)
276
{
277
    int len = 1;
278
    if (str) {
279
        len += strlen(str);
280
        put_buffer(s, (const unsigned char *) str, len);
281
    } else
282
        put_byte(s, 0);
283
    return len;
284
}
285

    
286
int ff_get_v_length(uint64_t val){
287
    int i=1;
288

    
289
    while(val>>=7)
290
        i++;
291

    
292
    return i;
293
}
294

    
295
void ff_put_v(ByteIOContext *bc, uint64_t val){
296
    int i= ff_get_v_length(val);
297

    
298
    while(--i>0)
299
        put_byte(bc, 128 | (val>>(7*i)));
300

    
301
    put_byte(bc, val&127);
302
}
303

    
304
void put_le64(ByteIOContext *s, uint64_t val)
305
{
306
    put_le32(s, (uint32_t)(val & 0xffffffff));
307
    put_le32(s, (uint32_t)(val >> 32));
308
}
309

    
310
void put_be64(ByteIOContext *s, uint64_t val)
311
{
312
    put_be32(s, (uint32_t)(val >> 32));
313
    put_be32(s, (uint32_t)(val & 0xffffffff));
314
}
315

    
316
void put_le16(ByteIOContext *s, unsigned int val)
317
{
318
    put_byte(s, val);
319
    put_byte(s, val >> 8);
320
}
321

    
322
void put_be16(ByteIOContext *s, unsigned int val)
323
{
324
    put_byte(s, val >> 8);
325
    put_byte(s, val);
326
}
327

    
328
void put_le24(ByteIOContext *s, unsigned int val)
329
{
330
    put_le16(s, val & 0xffff);
331
    put_byte(s, val >> 16);
332
}
333

    
334
void put_be24(ByteIOContext *s, unsigned int val)
335
{
336
    put_be16(s, val >> 8);
337
    put_byte(s, val);
338
}
339

    
340
void put_tag(ByteIOContext *s, const char *tag)
341
{
342
    while (*tag) {
343
        put_byte(s, *tag++);
344
    }
345
}
346

    
347
/* Input stream */
348

    
349
static void fill_buffer(ByteIOContext *s)
350
{
351
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
352
    int len= s->buffer_size - (dst - s->buffer);
353
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
354

    
355
    /* no need to do anything if EOF already reached */
356
    if (s->eof_reached)
357
        return;
358

    
359
    if(s->update_checksum && dst == s->buffer){
360
        if(s->buf_end > s->checksum_ptr)
361
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
362
        s->checksum_ptr= s->buffer;
363
    }
364

    
365
    /* make buffer smaller in case it ended up large after probing */
366
    if (s->buffer_size > max_buffer_size) {
367
        url_setbufsize(s, max_buffer_size);
368

    
369
        s->checksum_ptr = dst = s->buffer;
370
        len = s->buffer_size;
371
    }
372

    
373
    if(s->read_packet)
374
        len = s->read_packet(s->opaque, dst, len);
375
    else
376
        len = 0;
377
    if (len <= 0) {
378
        /* do not modify buffer if EOF reached so that a seek back can
379
           be done without rereading data */
380
        s->eof_reached = 1;
381
        if(len<0)
382
            s->error= len;
383
    } else {
384
        s->pos += len;
385
        s->buf_ptr = dst;
386
        s->buf_end = dst + len;
387
    }
388
}
389

    
390
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
391
                                    unsigned int len)
392
{
393
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
394
}
395

    
396
unsigned long get_checksum(ByteIOContext *s)
397
{
398
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
399
    s->update_checksum= NULL;
400
    return s->checksum;
401
}
402

    
403
void init_checksum(ByteIOContext *s,
404
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
405
                   unsigned long checksum)
406
{
407
    s->update_checksum= update_checksum;
408
    if(s->update_checksum){
409
        s->checksum= checksum;
410
        s->checksum_ptr= s->buf_ptr;
411
    }
412
}
413

    
414
/* XXX: put an inline version */
415
int get_byte(ByteIOContext *s)
416
{
417
    if (s->buf_ptr >= s->buf_end)
418
        fill_buffer(s);
419
    if (s->buf_ptr < s->buf_end)
420
        return *s->buf_ptr++;
421
    return 0;
422
}
423

    
424
int url_fgetc(ByteIOContext *s)
425
{
426
    if (s->buf_ptr >= s->buf_end)
427
        fill_buffer(s);
428
    if (s->buf_ptr < s->buf_end)
429
        return *s->buf_ptr++;
430
    return URL_EOF;
431
}
432

    
433
int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
434
{
435
    int len, size1;
436

    
437
    size1 = size;
438
    while (size > 0) {
439
        len = s->buf_end - s->buf_ptr;
440
        if (len > size)
441
            len = size;
442
        if (len == 0) {
443
            if(size > s->buffer_size && !s->update_checksum){
444
                if(s->read_packet)
445
                    len = s->read_packet(s->opaque, buf, size);
446
                if (len <= 0) {
447
                    /* do not modify buffer if EOF reached so that a seek back can
448
                    be done without rereading data */
449
                    s->eof_reached = 1;
450
                    if(len<0)
451
                        s->error= len;
452
                    break;
453
                } else {
454
                    s->pos += len;
455
                    size -= len;
456
                    buf += len;
457
                    s->buf_ptr = s->buffer;
458
                    s->buf_end = s->buffer/* + len*/;
459
                }
460
            }else{
461
                fill_buffer(s);
462
                len = s->buf_end - s->buf_ptr;
463
                if (len == 0)
464
                    break;
465
            }
466
        } else {
467
            memcpy(buf, s->buf_ptr, len);
468
            buf += len;
469
            s->buf_ptr += len;
470
            size -= len;
471
        }
472
    }
473
    if (size1 == size) {
474
        if (url_ferror(s)) return url_ferror(s);
475
        if (url_feof(s))   return AVERROR_EOF;
476
    }
477
    return size1 - size;
478
}
479

    
480
int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
481
{
482
    int len;
483

    
484
    if(size<0)
485
        return -1;
486

    
487
    len = s->buf_end - s->buf_ptr;
488
    if (len == 0) {
489
        fill_buffer(s);
490
        len = s->buf_end - s->buf_ptr;
491
    }
492
    if (len > size)
493
        len = size;
494
    memcpy(buf, s->buf_ptr, len);
495
    s->buf_ptr += len;
496
    if (!len) {
497
        if (url_ferror(s)) return url_ferror(s);
498
        if (url_feof(s))   return AVERROR_EOF;
499
    }
500
    return len;
501
}
502

    
503
unsigned int get_le16(ByteIOContext *s)
504
{
505
    unsigned int val;
506
    val = get_byte(s);
507
    val |= get_byte(s) << 8;
508
    return val;
509
}
510

    
511
unsigned int get_le24(ByteIOContext *s)
512
{
513
    unsigned int val;
514
    val = get_le16(s);
515
    val |= get_byte(s) << 16;
516
    return val;
517
}
518

    
519
unsigned int get_le32(ByteIOContext *s)
520
{
521
    unsigned int val;
522
    val = get_le16(s);
523
    val |= get_le16(s) << 16;
524
    return val;
525
}
526

    
527
uint64_t get_le64(ByteIOContext *s)
528
{
529
    uint64_t val;
530
    val = (uint64_t)get_le32(s);
531
    val |= (uint64_t)get_le32(s) << 32;
532
    return val;
533
}
534

    
535
unsigned int get_be16(ByteIOContext *s)
536
{
537
    unsigned int val;
538
    val = get_byte(s) << 8;
539
    val |= get_byte(s);
540
    return val;
541
}
542

    
543
unsigned int get_be24(ByteIOContext *s)
544
{
545
    unsigned int val;
546
    val = get_be16(s) << 8;
547
    val |= get_byte(s);
548
    return val;
549
}
550
unsigned int get_be32(ByteIOContext *s)
551
{
552
    unsigned int val;
553
    val = get_be16(s) << 16;
554
    val |= get_be16(s);
555
    return val;
556
}
557

    
558
char *get_strz(ByteIOContext *s, char *buf, int maxlen)
559
{
560
    int i = 0;
561
    char c;
562

    
563
    while ((c = get_byte(s))) {
564
        if (i < maxlen-1)
565
            buf[i++] = c;
566
    }
567

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

    
570
    return buf;
571
}
572

    
573
int ff_get_line(ByteIOContext *s, char *buf, int maxlen)
574
{
575
    int i = 0;
576
    char c;
577

    
578
    do {
579
        c = get_byte(s);
580
        if (c && i < maxlen-1)
581
            buf[i++] = c;
582
    } while (c != '\n' && c);
583

    
584
    buf[i] = 0;
585
    return i;
586
}
587

    
588
uint64_t get_be64(ByteIOContext *s)
589
{
590
    uint64_t val;
591
    val = (uint64_t)get_be32(s) << 32;
592
    val |= (uint64_t)get_be32(s);
593
    return val;
594
}
595

    
596
uint64_t ff_get_v(ByteIOContext *bc){
597
    uint64_t val = 0;
598
    int tmp;
599

    
600
    do{
601
        tmp = get_byte(bc);
602
        val= (val<<7) + (tmp&127);
603
    }while(tmp&128);
604
    return val;
605
}
606

    
607
int url_fdopen(ByteIOContext **s, URLContext *h)
608
{
609
    uint8_t *buffer;
610
    int buffer_size, max_packet_size;
611

    
612
    max_packet_size = url_get_max_packet_size(h);
613
    if (max_packet_size) {
614
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
615
    } else {
616
        buffer_size = IO_BUFFER_SIZE;
617
    }
618
    buffer = av_malloc(buffer_size);
619
    if (!buffer)
620
        return AVERROR(ENOMEM);
621

    
622
    *s = av_mallocz(sizeof(ByteIOContext));
623
    if(!*s) {
624
        av_free(buffer);
625
        return AVERROR(ENOMEM);
626
    }
627

    
628
    if (init_put_byte(*s, buffer, buffer_size,
629
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
630
                      url_read, url_write, url_seek) < 0) {
631
        av_free(buffer);
632
        av_freep(s);
633
        return AVERROR(EIO);
634
    }
635
    (*s)->is_streamed = h->is_streamed;
636
    (*s)->max_packet_size = max_packet_size;
637
    if(h->prot) {
638
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
639
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
640
    }
641
    return 0;
642
}
643

    
644
int url_setbufsize(ByteIOContext *s, int buf_size)
645
{
646
    uint8_t *buffer;
647
    buffer = av_malloc(buf_size);
648
    if (!buffer)
649
        return AVERROR(ENOMEM);
650

    
651
    av_free(s->buffer);
652
    s->buffer = buffer;
653
    s->buffer_size = buf_size;
654
    s->buf_ptr = buffer;
655
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
656
    return 0;
657
}
658

    
659
#if FF_API_URL_RESETBUF
660
int url_resetbuf(ByteIOContext *s, int flags)
661
#else
662
static int url_resetbuf(ByteIOContext *s, int flags)
663
#endif
664
{
665
#if FF_API_URL_RESETBUF
666
    if (flags & URL_RDWR)
667
        return AVERROR(EINVAL);
668
#else
669
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
670
#endif
671

    
672
    if (flags & URL_WRONLY) {
673
        s->buf_end = s->buffer + s->buffer_size;
674
        s->write_flag = 1;
675
    } else {
676
        s->buf_end = s->buffer;
677
        s->write_flag = 0;
678
    }
679
    return 0;
680
}
681

    
682
int ff_rewind_with_probe_data(ByteIOContext *s, unsigned char *buf, int buf_size)
683
{
684
    int64_t buffer_start;
685
    int buffer_size;
686
    int overlap, new_size, alloc_size;
687

    
688
    if (s->write_flag)
689
        return AVERROR(EINVAL);
690

    
691
    buffer_size = s->buf_end - s->buffer;
692

    
693
    /* the buffers must touch or overlap */
694
    if ((buffer_start = s->pos - buffer_size) > buf_size)
695
        return AVERROR(EINVAL);
696

    
697
    overlap = buf_size - buffer_start;
698
    new_size = buf_size + buffer_size - overlap;
699

    
700
    alloc_size = FFMAX(s->buffer_size, new_size);
701
    if (alloc_size > buf_size)
702
        if (!(buf = av_realloc(buf, alloc_size)))
703
            return AVERROR(ENOMEM);
704

    
705
    if (new_size > buf_size) {
706
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
707
        buf_size = new_size;
708
    }
709

    
710
    av_free(s->buffer);
711
    s->buf_ptr = s->buffer = buf;
712
    s->buffer_size = alloc_size;
713
    s->pos = buf_size;
714
    s->buf_end = s->buf_ptr + buf_size;
715
    s->eof_reached = 0;
716
    s->must_flush = 0;
717

    
718
    return 0;
719
}
720

    
721
int url_fopen(ByteIOContext **s, const char *filename, int flags)
722
{
723
    URLContext *h;
724
    int err;
725

    
726
    err = url_open(&h, filename, flags);
727
    if (err < 0)
728
        return err;
729
    err = url_fdopen(s, h);
730
    if (err < 0) {
731
        url_close(h);
732
        return err;
733
    }
734
    return 0;
735
}
736

    
737
int url_fclose(ByteIOContext *s)
738
{
739
    URLContext *h = s->opaque;
740

    
741
    av_free(s->buffer);
742
    av_free(s);
743
    return url_close(h);
744
}
745

    
746
URLContext *url_fileno(ByteIOContext *s)
747
{
748
    return s->opaque;
749
}
750

    
751
#if CONFIG_MUXERS
752
int url_fprintf(ByteIOContext *s, const char *fmt, ...)
753
{
754
    va_list ap;
755
    char buf[4096];
756
    int ret;
757

    
758
    va_start(ap, fmt);
759
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
760
    va_end(ap);
761
    put_buffer(s, buf, strlen(buf));
762
    return ret;
763
}
764
#endif //CONFIG_MUXERS
765

    
766
char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
767
{
768
    int c;
769
    char *q;
770

    
771
    c = url_fgetc(s);
772
    if (c == EOF)
773
        return NULL;
774
    q = buf;
775
    for(;;) {
776
        if (c == EOF || c == '\n')
777
            break;
778
        if ((q - buf) < buf_size - 1)
779
            *q++ = c;
780
        c = url_fgetc(s);
781
    }
782
    if (buf_size > 0)
783
        *q = '\0';
784
    return buf;
785
}
786

    
787
int url_fget_max_packet_size(ByteIOContext *s)
788
{
789
    return s->max_packet_size;
790
}
791

    
792
int av_url_read_fpause(ByteIOContext *s, int pause)
793
{
794
    if (!s->read_pause)
795
        return AVERROR(ENOSYS);
796
    return s->read_pause(s->opaque, pause);
797
}
798

    
799
int64_t av_url_read_fseek(ByteIOContext *s, int stream_index,
800
                          int64_t timestamp, int flags)
801
{
802
    URLContext *h = s->opaque;
803
    int64_t ret;
804
    if (!s->read_seek)
805
        return AVERROR(ENOSYS);
806
    ret = s->read_seek(h, stream_index, timestamp, flags);
807
    if(ret >= 0) {
808
        int64_t pos;
809
        s->buf_ptr = s->buf_end; // Flush buffer
810
        pos = s->seek(h, 0, SEEK_CUR);
811
        if (pos >= 0)
812
            s->pos = pos;
813
        else if (pos != AVERROR(ENOSYS))
814
            ret = pos;
815
    }
816
    return ret;
817
}
818

    
819
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
820
 * back to the server even if CONFIG_MUXERS is false. */
821
#if CONFIG_MUXERS || CONFIG_NETWORK
822
/* buffer handling */
823
int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags)
824
{
825
    int ret;
826
    *s = av_mallocz(sizeof(ByteIOContext));
827
    if(!*s)
828
        return AVERROR(ENOMEM);
829
    ret = init_put_byte(*s, buf, buf_size,
830
                        (flags & URL_WRONLY || flags & URL_RDWR),
831
                        NULL, NULL, NULL, NULL);
832
    if(ret != 0)
833
        av_freep(s);
834
    return ret;
835
}
836

    
837
int url_close_buf(ByteIOContext *s)
838
{
839
    put_flush_packet(s);
840
    return s->buf_ptr - s->buffer;
841
}
842

    
843
/* output in a dynamic buffer */
844

    
845
typedef struct DynBuffer {
846
    int pos, size, allocated_size;
847
    uint8_t *buffer;
848
    int io_buffer_size;
849
    uint8_t io_buffer[1];
850
} DynBuffer;
851

    
852
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
853
{
854
    DynBuffer *d = opaque;
855
    unsigned new_size, new_allocated_size;
856

    
857
    /* reallocate buffer if needed */
858
    new_size = d->pos + buf_size;
859
    new_allocated_size = d->allocated_size;
860
    if(new_size < d->pos || new_size > INT_MAX/2)
861
        return -1;
862
    while (new_size > new_allocated_size) {
863
        if (!new_allocated_size)
864
            new_allocated_size = new_size;
865
        else
866
            new_allocated_size += new_allocated_size / 2 + 1;
867
    }
868

    
869
    if (new_allocated_size > d->allocated_size) {
870
        d->buffer = av_realloc(d->buffer, new_allocated_size);
871
        if(d->buffer == NULL)
872
             return AVERROR(ENOMEM);
873
        d->allocated_size = new_allocated_size;
874
    }
875
    memcpy(d->buffer + d->pos, buf, buf_size);
876
    d->pos = new_size;
877
    if (d->pos > d->size)
878
        d->size = d->pos;
879
    return buf_size;
880
}
881

    
882
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
883
{
884
    unsigned char buf1[4];
885
    int ret;
886

    
887
    /* packetized write: output the header */
888
    AV_WB32(buf1, buf_size);
889
    ret= dyn_buf_write(opaque, buf1, 4);
890
    if(ret < 0)
891
        return ret;
892

    
893
    /* then the data */
894
    return dyn_buf_write(opaque, buf, buf_size);
895
}
896

    
897
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
898
{
899
    DynBuffer *d = opaque;
900

    
901
    if (whence == SEEK_CUR)
902
        offset += d->pos;
903
    else if (whence == SEEK_END)
904
        offset += d->size;
905
    if (offset < 0 || offset > 0x7fffffffLL)
906
        return -1;
907
    d->pos = offset;
908
    return 0;
909
}
910

    
911
static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size)
912
{
913
    DynBuffer *d;
914
    int ret;
915
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
916

    
917
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
918
        return -1;
919
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
920
    if (!d)
921
        return AVERROR(ENOMEM);
922
    *s = av_mallocz(sizeof(ByteIOContext));
923
    if(!*s) {
924
        av_free(d);
925
        return AVERROR(ENOMEM);
926
    }
927
    d->io_buffer_size = io_buffer_size;
928
    ret = init_put_byte(*s, d->io_buffer, io_buffer_size,
929
                        1, d, NULL,
930
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
931
                        max_packet_size ? NULL : dyn_buf_seek);
932
    if (ret == 0) {
933
        (*s)->max_packet_size = max_packet_size;
934
    } else {
935
        av_free(d);
936
        av_freep(s);
937
    }
938
    return ret;
939
}
940

    
941
int url_open_dyn_buf(ByteIOContext **s)
942
{
943
    return url_open_dyn_buf_internal(s, 0);
944
}
945

    
946
int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size)
947
{
948
    if (max_packet_size <= 0)
949
        return -1;
950
    return url_open_dyn_buf_internal(s, max_packet_size);
951
}
952

    
953
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
954
{
955
    DynBuffer *d = s->opaque;
956
    int size;
957
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
958
    int padding = 0;
959

    
960
    /* don't attempt to pad fixed-size packet buffers */
961
    if (!s->max_packet_size) {
962
        put_buffer(s, padbuf, sizeof(padbuf));
963
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
964
    }
965

    
966
    put_flush_packet(s);
967

    
968
    *pbuffer = d->buffer;
969
    size = d->size;
970
    av_free(d);
971
    av_free(s);
972
    return size - padding;
973
}
974
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */