Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ c66f53cf

History | View | Annotate | Download (23 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
void put_strz(ByteIOContext *s, const char *str)
269
{
270
    if (str)
271
        put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
272
    else
273
        put_byte(s, 0);
274
}
275

    
276
int ff_get_v_length(uint64_t val){
277
    int i=1;
278

    
279
    while(val>>=7)
280
        i++;
281

    
282
    return i;
283
}
284

    
285
void ff_put_v(ByteIOContext *bc, uint64_t val){
286
    int i= ff_get_v_length(val);
287

    
288
    while(--i>0)
289
        put_byte(bc, 128 | (val>>(7*i)));
290

    
291
    put_byte(bc, val&127);
292
}
293

    
294
void put_le64(ByteIOContext *s, uint64_t val)
295
{
296
    put_le32(s, (uint32_t)(val & 0xffffffff));
297
    put_le32(s, (uint32_t)(val >> 32));
298
}
299

    
300
void put_be64(ByteIOContext *s, uint64_t val)
301
{
302
    put_be32(s, (uint32_t)(val >> 32));
303
    put_be32(s, (uint32_t)(val & 0xffffffff));
304
}
305

    
306
void put_le16(ByteIOContext *s, unsigned int val)
307
{
308
    put_byte(s, val);
309
    put_byte(s, val >> 8);
310
}
311

    
312
void put_be16(ByteIOContext *s, unsigned int val)
313
{
314
    put_byte(s, val >> 8);
315
    put_byte(s, val);
316
}
317

    
318
void put_le24(ByteIOContext *s, unsigned int val)
319
{
320
    put_le16(s, val & 0xffff);
321
    put_byte(s, val >> 16);
322
}
323

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

    
330
void put_tag(ByteIOContext *s, const char *tag)
331
{
332
    while (*tag) {
333
        put_byte(s, *tag++);
334
    }
335
}
336

    
337
/* Input stream */
338

    
339
static void fill_buffer(ByteIOContext *s)
340
{
341
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
342
    int len= s->buffer_size - (dst - s->buffer);
343
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
344

    
345
    /* no need to do anything if EOF already reached */
346
    if (s->eof_reached)
347
        return;
348

    
349
    if(s->update_checksum && dst == s->buffer){
350
        if(s->buf_end > s->checksum_ptr)
351
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
352
        s->checksum_ptr= s->buffer;
353
    }
354

    
355
    /* make buffer smaller in case it ended up large after probing */
356
    if (s->buffer_size > max_buffer_size) {
357
        url_setbufsize(s, max_buffer_size);
358

    
359
        s->checksum_ptr = dst = s->buffer;
360
        len = s->buffer_size;
361
    }
362

    
363
    if(s->read_packet)
364
        len = s->read_packet(s->opaque, dst, len);
365
    else
366
        len = 0;
367
    if (len <= 0) {
368
        /* do not modify buffer if EOF reached so that a seek back can
369
           be done without rereading data */
370
        s->eof_reached = 1;
371
        if(len<0)
372
            s->error= len;
373
    } else {
374
        s->pos += len;
375
        s->buf_ptr = dst;
376
        s->buf_end = dst + len;
377
    }
378
}
379

    
380
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
381
                                    unsigned int len)
382
{
383
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
384
}
385

    
386
unsigned long get_checksum(ByteIOContext *s)
387
{
388
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
389
    s->update_checksum= NULL;
390
    return s->checksum;
391
}
392

    
393
void init_checksum(ByteIOContext *s,
394
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
395
                   unsigned long checksum)
396
{
397
    s->update_checksum= update_checksum;
398
    if(s->update_checksum){
399
        s->checksum= checksum;
400
        s->checksum_ptr= s->buf_ptr;
401
    }
402
}
403

    
404
/* XXX: put an inline version */
405
int get_byte(ByteIOContext *s)
406
{
407
    if (s->buf_ptr >= s->buf_end)
408
        fill_buffer(s);
409
    if (s->buf_ptr < s->buf_end)
410
        return *s->buf_ptr++;
411
    return 0;
412
}
413

    
414
int url_fgetc(ByteIOContext *s)
415
{
416
    if (s->buf_ptr >= s->buf_end)
417
        fill_buffer(s);
418
    if (s->buf_ptr < s->buf_end)
419
        return *s->buf_ptr++;
420
    return URL_EOF;
421
}
422

    
423
int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
424
{
425
    int len, size1;
426

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

    
470
int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
471
{
472
    int len;
473

    
474
    if(size<0)
475
        return -1;
476

    
477
    len = s->buf_end - s->buf_ptr;
478
    if (len == 0) {
479
        fill_buffer(s);
480
        len = s->buf_end - s->buf_ptr;
481
    }
482
    if (len > size)
483
        len = size;
484
    memcpy(buf, s->buf_ptr, len);
485
    s->buf_ptr += len;
486
    if (!len) {
487
        if (url_ferror(s)) return url_ferror(s);
488
        if (url_feof(s))   return AVERROR_EOF;
489
    }
490
    return len;
491
}
492

    
493
unsigned int get_le16(ByteIOContext *s)
494
{
495
    unsigned int val;
496
    val = get_byte(s);
497
    val |= get_byte(s) << 8;
498
    return val;
499
}
500

    
501
unsigned int get_le24(ByteIOContext *s)
502
{
503
    unsigned int val;
504
    val = get_le16(s);
505
    val |= get_byte(s) << 16;
506
    return val;
507
}
508

    
509
unsigned int get_le32(ByteIOContext *s)
510
{
511
    unsigned int val;
512
    val = get_le16(s);
513
    val |= get_le16(s) << 16;
514
    return val;
515
}
516

    
517
uint64_t get_le64(ByteIOContext *s)
518
{
519
    uint64_t val;
520
    val = (uint64_t)get_le32(s);
521
    val |= (uint64_t)get_le32(s) << 32;
522
    return val;
523
}
524

    
525
unsigned int get_be16(ByteIOContext *s)
526
{
527
    unsigned int val;
528
    val = get_byte(s) << 8;
529
    val |= get_byte(s);
530
    return val;
531
}
532

    
533
unsigned int get_be24(ByteIOContext *s)
534
{
535
    unsigned int val;
536
    val = get_be16(s) << 8;
537
    val |= get_byte(s);
538
    return val;
539
}
540
unsigned int get_be32(ByteIOContext *s)
541
{
542
    unsigned int val;
543
    val = get_be16(s) << 16;
544
    val |= get_be16(s);
545
    return val;
546
}
547

    
548
char *get_strz(ByteIOContext *s, char *buf, int maxlen)
549
{
550
    int i = 0;
551
    char c;
552

    
553
    while ((c = get_byte(s))) {
554
        if (i < maxlen-1)
555
            buf[i++] = c;
556
    }
557

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

    
560
    return buf;
561
}
562

    
563
int ff_get_line(ByteIOContext *s, char *buf, int maxlen)
564
{
565
    int i = 0;
566
    char c;
567

    
568
    do {
569
        c = get_byte(s);
570
        if (c && i < maxlen-1)
571
            buf[i++] = c;
572
    } while (c != '\n' && c);
573

    
574
    buf[i] = 0;
575
    return i;
576
}
577

    
578
uint64_t get_be64(ByteIOContext *s)
579
{
580
    uint64_t val;
581
    val = (uint64_t)get_be32(s) << 32;
582
    val |= (uint64_t)get_be32(s);
583
    return val;
584
}
585

    
586
uint64_t ff_get_v(ByteIOContext *bc){
587
    uint64_t val = 0;
588
    int tmp;
589

    
590
    do{
591
        tmp = get_byte(bc);
592
        val= (val<<7) + (tmp&127);
593
    }while(tmp&128);
594
    return val;
595
}
596

    
597
int url_fdopen(ByteIOContext **s, URLContext *h)
598
{
599
    uint8_t *buffer;
600
    int buffer_size, max_packet_size;
601

    
602
    max_packet_size = url_get_max_packet_size(h);
603
    if (max_packet_size) {
604
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
605
    } else {
606
        buffer_size = IO_BUFFER_SIZE;
607
    }
608
    buffer = av_malloc(buffer_size);
609
    if (!buffer)
610
        return AVERROR(ENOMEM);
611

    
612
    *s = av_mallocz(sizeof(ByteIOContext));
613
    if(!*s) {
614
        av_free(buffer);
615
        return AVERROR(ENOMEM);
616
    }
617

    
618
    if (init_put_byte(*s, buffer, buffer_size,
619
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
620
                      url_read, url_write, url_seek) < 0) {
621
        av_free(buffer);
622
        av_freep(s);
623
        return AVERROR(EIO);
624
    }
625
    (*s)->is_streamed = h->is_streamed;
626
    (*s)->max_packet_size = max_packet_size;
627
    if(h->prot) {
628
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
629
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
630
    }
631
    return 0;
632
}
633

    
634
int url_setbufsize(ByteIOContext *s, int buf_size)
635
{
636
    uint8_t *buffer;
637
    buffer = av_malloc(buf_size);
638
    if (!buffer)
639
        return AVERROR(ENOMEM);
640

    
641
    av_free(s->buffer);
642
    s->buffer = buffer;
643
    s->buffer_size = buf_size;
644
    s->buf_ptr = buffer;
645
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
646
    return 0;
647
}
648

    
649
#if FF_API_URL_RESETBUF
650
int url_resetbuf(ByteIOContext *s, int flags)
651
#else
652
static int url_resetbuf(ByteIOContext *s, int flags)
653
#endif
654
{
655
#if FF_API_URL_RESETBUF
656
    if (flags & URL_RDWR)
657
        return AVERROR(EINVAL);
658
#else
659
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
660
#endif
661

    
662
    if (flags & URL_WRONLY) {
663
        s->buf_end = s->buffer + s->buffer_size;
664
        s->write_flag = 1;
665
    } else {
666
        s->buf_end = s->buffer;
667
        s->write_flag = 0;
668
    }
669
    return 0;
670
}
671

    
672
int ff_rewind_with_probe_data(ByteIOContext *s, unsigned char *buf, int buf_size)
673
{
674
    int64_t buffer_start;
675
    int buffer_size;
676
    int overlap, new_size, alloc_size;
677

    
678
    if (s->write_flag)
679
        return AVERROR(EINVAL);
680

    
681
    buffer_size = s->buf_end - s->buffer;
682

    
683
    /* the buffers must touch or overlap */
684
    if ((buffer_start = s->pos - buffer_size) > buf_size)
685
        return AVERROR(EINVAL);
686

    
687
    overlap = buf_size - buffer_start;
688
    new_size = buf_size + buffer_size - overlap;
689

    
690
    alloc_size = FFMAX(s->buffer_size, new_size);
691
    if (alloc_size > buf_size)
692
        if (!(buf = av_realloc(buf, alloc_size)))
693
            return AVERROR(ENOMEM);
694

    
695
    if (new_size > buf_size) {
696
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
697
        buf_size = new_size;
698
    }
699

    
700
    av_free(s->buffer);
701
    s->buf_ptr = s->buffer = buf;
702
    s->buffer_size = alloc_size;
703
    s->pos = buf_size;
704
    s->buf_end = s->buf_ptr + buf_size;
705
    s->eof_reached = 0;
706
    s->must_flush = 0;
707

    
708
    return 0;
709
}
710

    
711
int url_fopen(ByteIOContext **s, const char *filename, int flags)
712
{
713
    URLContext *h;
714
    int err;
715

    
716
    err = url_open(&h, filename, flags);
717
    if (err < 0)
718
        return err;
719
    err = url_fdopen(s, h);
720
    if (err < 0) {
721
        url_close(h);
722
        return err;
723
    }
724
    return 0;
725
}
726

    
727
int url_fclose(ByteIOContext *s)
728
{
729
    URLContext *h = s->opaque;
730

    
731
    av_free(s->buffer);
732
    av_free(s);
733
    return url_close(h);
734
}
735

    
736
URLContext *url_fileno(ByteIOContext *s)
737
{
738
    return s->opaque;
739
}
740

    
741
#if CONFIG_MUXERS
742
int url_fprintf(ByteIOContext *s, const char *fmt, ...)
743
{
744
    va_list ap;
745
    char buf[4096];
746
    int ret;
747

    
748
    va_start(ap, fmt);
749
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
750
    va_end(ap);
751
    put_buffer(s, buf, strlen(buf));
752
    return ret;
753
}
754
#endif //CONFIG_MUXERS
755

    
756
char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
757
{
758
    int c;
759
    char *q;
760

    
761
    c = url_fgetc(s);
762
    if (c == EOF)
763
        return NULL;
764
    q = buf;
765
    for(;;) {
766
        if (c == EOF || c == '\n')
767
            break;
768
        if ((q - buf) < buf_size - 1)
769
            *q++ = c;
770
        c = url_fgetc(s);
771
    }
772
    if (buf_size > 0)
773
        *q = '\0';
774
    return buf;
775
}
776

    
777
int url_fget_max_packet_size(ByteIOContext *s)
778
{
779
    return s->max_packet_size;
780
}
781

    
782
int av_url_read_fpause(ByteIOContext *s, int pause)
783
{
784
    if (!s->read_pause)
785
        return AVERROR(ENOSYS);
786
    return s->read_pause(s->opaque, pause);
787
}
788

    
789
int64_t av_url_read_fseek(ByteIOContext *s, int stream_index,
790
                          int64_t timestamp, int flags)
791
{
792
    URLContext *h = s->opaque;
793
    int64_t ret;
794
    if (!s->read_seek)
795
        return AVERROR(ENOSYS);
796
    ret = s->read_seek(h, stream_index, timestamp, flags);
797
    if(ret >= 0) {
798
        int64_t pos;
799
        s->buf_ptr = s->buf_end; // Flush buffer
800
        pos = s->seek(h, 0, SEEK_CUR);
801
        if (pos >= 0)
802
            s->pos = pos;
803
        else if (pos != AVERROR(ENOSYS))
804
            ret = pos;
805
    }
806
    return ret;
807
}
808

    
809
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
810
 * back to the server even if CONFIG_MUXERS is false. */
811
#if CONFIG_MUXERS || CONFIG_NETWORK
812
/* buffer handling */
813
int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags)
814
{
815
    int ret;
816
    *s = av_mallocz(sizeof(ByteIOContext));
817
    if(!*s)
818
        return AVERROR(ENOMEM);
819
    ret = init_put_byte(*s, buf, buf_size,
820
                        (flags & URL_WRONLY || flags & URL_RDWR),
821
                        NULL, NULL, NULL, NULL);
822
    if(ret != 0)
823
        av_freep(s);
824
    return ret;
825
}
826

    
827
int url_close_buf(ByteIOContext *s)
828
{
829
    put_flush_packet(s);
830
    return s->buf_ptr - s->buffer;
831
}
832

    
833
/* output in a dynamic buffer */
834

    
835
typedef struct DynBuffer {
836
    int pos, size, allocated_size;
837
    uint8_t *buffer;
838
    int io_buffer_size;
839
    uint8_t io_buffer[1];
840
} DynBuffer;
841

    
842
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
843
{
844
    DynBuffer *d = opaque;
845
    unsigned new_size, new_allocated_size;
846

    
847
    /* reallocate buffer if needed */
848
    new_size = d->pos + buf_size;
849
    new_allocated_size = d->allocated_size;
850
    if(new_size < d->pos || new_size > INT_MAX/2)
851
        return -1;
852
    while (new_size > new_allocated_size) {
853
        if (!new_allocated_size)
854
            new_allocated_size = new_size;
855
        else
856
            new_allocated_size += new_allocated_size / 2 + 1;
857
    }
858

    
859
    if (new_allocated_size > d->allocated_size) {
860
        d->buffer = av_realloc(d->buffer, new_allocated_size);
861
        if(d->buffer == NULL)
862
             return AVERROR(ENOMEM);
863
        d->allocated_size = new_allocated_size;
864
    }
865
    memcpy(d->buffer + d->pos, buf, buf_size);
866
    d->pos = new_size;
867
    if (d->pos > d->size)
868
        d->size = d->pos;
869
    return buf_size;
870
}
871

    
872
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
873
{
874
    unsigned char buf1[4];
875
    int ret;
876

    
877
    /* packetized write: output the header */
878
    AV_WB32(buf1, buf_size);
879
    ret= dyn_buf_write(opaque, buf1, 4);
880
    if(ret < 0)
881
        return ret;
882

    
883
    /* then the data */
884
    return dyn_buf_write(opaque, buf, buf_size);
885
}
886

    
887
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
888
{
889
    DynBuffer *d = opaque;
890

    
891
    if (whence == SEEK_CUR)
892
        offset += d->pos;
893
    else if (whence == SEEK_END)
894
        offset += d->size;
895
    if (offset < 0 || offset > 0x7fffffffLL)
896
        return -1;
897
    d->pos = offset;
898
    return 0;
899
}
900

    
901
static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size)
902
{
903
    DynBuffer *d;
904
    int ret;
905
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
906

    
907
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
908
        return -1;
909
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
910
    if (!d)
911
        return AVERROR(ENOMEM);
912
    *s = av_mallocz(sizeof(ByteIOContext));
913
    if(!*s) {
914
        av_free(d);
915
        return AVERROR(ENOMEM);
916
    }
917
    d->io_buffer_size = io_buffer_size;
918
    ret = init_put_byte(*s, d->io_buffer, io_buffer_size,
919
                        1, d, NULL,
920
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
921
                        max_packet_size ? NULL : dyn_buf_seek);
922
    if (ret == 0) {
923
        (*s)->max_packet_size = max_packet_size;
924
    } else {
925
        av_free(d);
926
        av_freep(s);
927
    }
928
    return ret;
929
}
930

    
931
int url_open_dyn_buf(ByteIOContext **s)
932
{
933
    return url_open_dyn_buf_internal(s, 0);
934
}
935

    
936
int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size)
937
{
938
    if (max_packet_size <= 0)
939
        return -1;
940
    return url_open_dyn_buf_internal(s, max_packet_size);
941
}
942

    
943
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
944
{
945
    DynBuffer *d = s->opaque;
946
    int size;
947
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
948
    int padding = 0;
949

    
950
    /* don't attempt to pad fixed-size packet buffers */
951
    if (!s->max_packet_size) {
952
        put_buffer(s, padbuf, sizeof(padbuf));
953
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
954
    }
955

    
956
    put_flush_packet(s);
957

    
958
    *pbuffer = d->buffer;
959
    size = d->size;
960
    av_free(d);
961
    av_free(s);
962
    return size - padding;
963
}
964
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */