Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ a6963f83

History | View | Annotate | Download (23.8 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
/* needed for usleep() */
23
#define _XOPEN_SOURCE 600
24
#include <unistd.h>
25
#include "libavutil/crc.h"
26
#include "libavutil/intreadwrite.h"
27
#include "avformat.h"
28
#include "avio.h"
29
#include "internal.h"
30
#include <stdarg.h>
31

    
32
#define IO_BUFFER_SIZE 32768
33

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

    
41
static void fill_buffer(ByteIOContext *s);
42
#if !FF_API_URL_RESETBUF
43
static int url_resetbuf(ByteIOContext *s, int flags);
44
#endif
45

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

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

    
94
static inline int retry_transfer_wrapper(void *opaque, unsigned char *buf,
95
                                         int size,
96
                                         int (*transfer_func)(void *, unsigned char *, int)) {
97
    int fast_retries = 5;
98
    while (1) {
99
        int ret = transfer_func(opaque, buf, size);
100
        if (ret == AVERROR(EAGAIN)) {
101
            if (fast_retries)
102
                fast_retries--;
103
            else
104
                usleep(1000);
105
        } else
106
            return ret;
107
    }
108
}
109

    
110
static void flush_buffer(ByteIOContext *s)
111
{
112
    if (s->buf_ptr > s->buffer) {
113
        if (s->write_packet && !s->error){
114
            int ret= retry_transfer_wrapper(s->opaque, s->buffer,
115
                                            s->buf_ptr - s->buffer,
116
                                            s->write_packet);
117
            if(ret < 0){
118
                s->error = ret;
119
            }
120
        }
121
        if(s->update_checksum){
122
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
123
            s->checksum_ptr= s->buffer;
124
        }
125
        s->pos += s->buf_ptr - s->buffer;
126
    }
127
    s->buf_ptr = s->buffer;
128
}
129

    
130
void put_byte(ByteIOContext *s, int b)
131
{
132
    *(s->buf_ptr)++ = b;
133
    if (s->buf_ptr >= s->buf_end)
134
        flush_buffer(s);
135
}
136

    
137
void put_nbyte(ByteIOContext *s, int b, int count)
138
{
139
    while (count > 0) {
140
        int len = FFMIN(s->buf_end - s->buf_ptr, count);
141
        memset(s->buf_ptr, b, len);
142
        s->buf_ptr += len;
143

    
144
        if (s->buf_ptr >= s->buf_end)
145
            flush_buffer(s);
146

    
147
        count -= len;
148
    }
149
}
150

    
151
void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
152
{
153
    while (size > 0) {
154
        int len = FFMIN(s->buf_end - s->buf_ptr, size);
155
        memcpy(s->buf_ptr, buf, len);
156
        s->buf_ptr += len;
157

    
158
        if (s->buf_ptr >= s->buf_end)
159
            flush_buffer(s);
160

    
161
        buf += len;
162
        size -= len;
163
    }
164
}
165

    
166
void put_flush_packet(ByteIOContext *s)
167
{
168
    flush_buffer(s);
169
    s->must_flush = 0;
170
}
171

    
172
int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence)
173
{
174
    int64_t offset1;
175
    int64_t pos;
176
    int force = whence & AVSEEK_FORCE;
177
    whence &= ~AVSEEK_FORCE;
178

    
179
    if(!s)
180
        return AVERROR(EINVAL);
181

    
182
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
183

    
184
    if (whence != SEEK_CUR && whence != SEEK_SET)
185
        return AVERROR(EINVAL);
186

    
187
    if (whence == SEEK_CUR) {
188
        offset1 = pos + (s->buf_ptr - s->buffer);
189
        if (offset == 0)
190
            return offset1;
191
        offset += offset1;
192
    }
193
    offset1 = offset - pos;
194
    if (!s->must_flush &&
195
        offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
196
        /* can do the seek inside the buffer */
197
        s->buf_ptr = s->buffer + offset1;
198
    } else if ((s->is_streamed ||
199
               offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
200
               !s->write_flag && offset1 >= 0 &&
201
              (whence != SEEK_END || force)) {
202
        while(s->pos < offset && !s->eof_reached)
203
            fill_buffer(s);
204
        if (s->eof_reached)
205
            return AVERROR_EOF;
206
        s->buf_ptr = s->buf_end + offset - s->pos;
207
    } else {
208
        int64_t res;
209

    
210
#if CONFIG_MUXERS || CONFIG_NETWORK
211
        if (s->write_flag) {
212
            flush_buffer(s);
213
            s->must_flush = 1;
214
        }
215
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
216
        if (!s->seek)
217
            return AVERROR(EPIPE);
218
        if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
219
            return res;
220
        if (!s->write_flag)
221
            s->buf_end = s->buffer;
222
        s->buf_ptr = s->buffer;
223
        s->pos = offset;
224
    }
225
    s->eof_reached = 0;
226
    return offset;
227
}
228

    
229
int url_fskip(ByteIOContext *s, int64_t offset)
230
{
231
    int64_t ret = url_fseek(s, offset, SEEK_CUR);
232
    return ret < 0 ? ret : 0;
233
}
234

    
235
int64_t url_ftell(ByteIOContext *s)
236
{
237
    return url_fseek(s, 0, SEEK_CUR);
238
}
239

    
240
int64_t url_fsize(ByteIOContext *s)
241
{
242
    int64_t size;
243

    
244
    if(!s)
245
        return AVERROR(EINVAL);
246

    
247
    if (!s->seek)
248
        return AVERROR(ENOSYS);
249
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
250
    if(size<0){
251
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
252
            return size;
253
        size++;
254
        s->seek(s->opaque, s->pos, SEEK_SET);
255
    }
256
    return size;
257
}
258

    
259
int url_feof(ByteIOContext *s)
260
{
261
    if(!s)
262
        return 0;
263
    return s->eof_reached;
264
}
265

    
266
int url_ferror(ByteIOContext *s)
267
{
268
    if(!s)
269
        return 0;
270
    return s->error;
271
}
272

    
273
void put_le32(ByteIOContext *s, unsigned int val)
274
{
275
    put_byte(s, val);
276
    put_byte(s, val >> 8);
277
    put_byte(s, val >> 16);
278
    put_byte(s, val >> 24);
279
}
280

    
281
void put_be32(ByteIOContext *s, unsigned int val)
282
{
283
    put_byte(s, val >> 24);
284
    put_byte(s, val >> 16);
285
    put_byte(s, val >> 8);
286
    put_byte(s, val);
287
}
288

    
289
void put_strz(ByteIOContext *s, const char *str)
290
{
291
    if (str)
292
        put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
293
    else
294
        put_byte(s, 0);
295
}
296

    
297
int ff_get_v_length(uint64_t val){
298
    int i=1;
299

    
300
    while(val>>=7)
301
        i++;
302

    
303
    return i;
304
}
305

    
306
void ff_put_v(ByteIOContext *bc, uint64_t val){
307
    int i= ff_get_v_length(val);
308

    
309
    while(--i>0)
310
        put_byte(bc, 128 | (val>>(7*i)));
311

    
312
    put_byte(bc, val&127);
313
}
314

    
315
void put_le64(ByteIOContext *s, uint64_t val)
316
{
317
    put_le32(s, (uint32_t)(val & 0xffffffff));
318
    put_le32(s, (uint32_t)(val >> 32));
319
}
320

    
321
void put_be64(ByteIOContext *s, uint64_t val)
322
{
323
    put_be32(s, (uint32_t)(val >> 32));
324
    put_be32(s, (uint32_t)(val & 0xffffffff));
325
}
326

    
327
void put_le16(ByteIOContext *s, unsigned int val)
328
{
329
    put_byte(s, val);
330
    put_byte(s, val >> 8);
331
}
332

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

    
339
void put_le24(ByteIOContext *s, unsigned int val)
340
{
341
    put_le16(s, val & 0xffff);
342
    put_byte(s, val >> 16);
343
}
344

    
345
void put_be24(ByteIOContext *s, unsigned int val)
346
{
347
    put_be16(s, val >> 8);
348
    put_byte(s, val);
349
}
350

    
351
void put_tag(ByteIOContext *s, const char *tag)
352
{
353
    while (*tag) {
354
        put_byte(s, *tag++);
355
    }
356
}
357

    
358
/* Input stream */
359

    
360
static void fill_buffer(ByteIOContext *s)
361
{
362
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
363
    int len= s->buffer_size - (dst - s->buffer);
364
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
365

    
366
    /* no need to do anything if EOF already reached */
367
    if (s->eof_reached)
368
        return;
369

    
370
    if(s->update_checksum && dst == s->buffer){
371
        if(s->buf_end > s->checksum_ptr)
372
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
373
        s->checksum_ptr= s->buffer;
374
    }
375

    
376
    /* make buffer smaller in case it ended up large after probing */
377
    if (s->buffer_size > max_buffer_size) {
378
        url_setbufsize(s, max_buffer_size);
379

    
380
        s->checksum_ptr = dst = s->buffer;
381
        len = s->buffer_size;
382
    }
383

    
384
    if(s->read_packet)
385
        len = retry_transfer_wrapper(s->opaque, dst, len, s->read_packet);
386
    else
387
        len = 0;
388
    if (len <= 0) {
389
        /* do not modify buffer if EOF reached so that a seek back can
390
           be done without rereading data */
391
        s->eof_reached = 1;
392
        if(len<0)
393
            s->error= len;
394
    } else {
395
        s->pos += len;
396
        s->buf_ptr = dst;
397
        s->buf_end = dst + len;
398
    }
399
}
400

    
401
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
402
                                    unsigned int len)
403
{
404
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
405
}
406

    
407
unsigned long get_checksum(ByteIOContext *s)
408
{
409
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
410
    s->update_checksum= NULL;
411
    return s->checksum;
412
}
413

    
414
void init_checksum(ByteIOContext *s,
415
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
416
                   unsigned long checksum)
417
{
418
    s->update_checksum= update_checksum;
419
    if(s->update_checksum){
420
        s->checksum= checksum;
421
        s->checksum_ptr= s->buf_ptr;
422
    }
423
}
424

    
425
/* XXX: put an inline version */
426
int get_byte(ByteIOContext *s)
427
{
428
    if (s->buf_ptr >= s->buf_end)
429
        fill_buffer(s);
430
    if (s->buf_ptr < s->buf_end)
431
        return *s->buf_ptr++;
432
    return 0;
433
}
434

    
435
int url_fgetc(ByteIOContext *s)
436
{
437
    if (s->buf_ptr >= s->buf_end)
438
        fill_buffer(s);
439
    if (s->buf_ptr < s->buf_end)
440
        return *s->buf_ptr++;
441
    return URL_EOF;
442
}
443

    
444
int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
445
{
446
    int len, size1;
447

    
448
    size1 = size;
449
    while (size > 0) {
450
        len = s->buf_end - s->buf_ptr;
451
        if (len > size)
452
            len = size;
453
        if (len == 0) {
454
            if(size > s->buffer_size && !s->update_checksum){
455
                if(s->read_packet)
456
                    len = retry_transfer_wrapper(s->opaque, buf, size,
457
                                                 s->read_packet);
458
                if (len <= 0) {
459
                    /* do not modify buffer if EOF reached so that a seek back can
460
                    be done without rereading data */
461
                    s->eof_reached = 1;
462
                    if(len<0)
463
                        s->error= len;
464
                    break;
465
                } else {
466
                    s->pos += len;
467
                    size -= len;
468
                    buf += len;
469
                    s->buf_ptr = s->buffer;
470
                    s->buf_end = s->buffer/* + len*/;
471
                }
472
            }else{
473
                fill_buffer(s);
474
                len = s->buf_end - s->buf_ptr;
475
                if (len == 0)
476
                    break;
477
            }
478
        } else {
479
            memcpy(buf, s->buf_ptr, len);
480
            buf += len;
481
            s->buf_ptr += len;
482
            size -= len;
483
        }
484
    }
485
    if (size1 == size) {
486
        if (url_ferror(s)) return url_ferror(s);
487
        if (url_feof(s))   return AVERROR_EOF;
488
    }
489
    return size1 - size;
490
}
491

    
492
int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
493
{
494
    int len;
495

    
496
    if(size<0)
497
        return -1;
498

    
499
    len = s->buf_end - s->buf_ptr;
500
    if (len == 0) {
501
        fill_buffer(s);
502
        len = s->buf_end - s->buf_ptr;
503
    }
504
    if (len > size)
505
        len = size;
506
    memcpy(buf, s->buf_ptr, len);
507
    s->buf_ptr += len;
508
    if (!len) {
509
        if (url_ferror(s)) return url_ferror(s);
510
        if (url_feof(s))   return AVERROR_EOF;
511
    }
512
    return len;
513
}
514

    
515
unsigned int get_le16(ByteIOContext *s)
516
{
517
    unsigned int val;
518
    val = get_byte(s);
519
    val |= get_byte(s) << 8;
520
    return val;
521
}
522

    
523
unsigned int get_le24(ByteIOContext *s)
524
{
525
    unsigned int val;
526
    val = get_le16(s);
527
    val |= get_byte(s) << 16;
528
    return val;
529
}
530

    
531
unsigned int get_le32(ByteIOContext *s)
532
{
533
    unsigned int val;
534
    val = get_le16(s);
535
    val |= get_le16(s) << 16;
536
    return val;
537
}
538

    
539
uint64_t get_le64(ByteIOContext *s)
540
{
541
    uint64_t val;
542
    val = (uint64_t)get_le32(s);
543
    val |= (uint64_t)get_le32(s) << 32;
544
    return val;
545
}
546

    
547
unsigned int get_be16(ByteIOContext *s)
548
{
549
    unsigned int val;
550
    val = get_byte(s) << 8;
551
    val |= get_byte(s);
552
    return val;
553
}
554

    
555
unsigned int get_be24(ByteIOContext *s)
556
{
557
    unsigned int val;
558
    val = get_be16(s) << 8;
559
    val |= get_byte(s);
560
    return val;
561
}
562
unsigned int get_be32(ByteIOContext *s)
563
{
564
    unsigned int val;
565
    val = get_be16(s) << 16;
566
    val |= get_be16(s);
567
    return val;
568
}
569

    
570
char *get_strz(ByteIOContext *s, char *buf, int maxlen)
571
{
572
    int i = 0;
573
    char c;
574

    
575
    while ((c = get_byte(s))) {
576
        if (i < maxlen-1)
577
            buf[i++] = c;
578
    }
579

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

    
582
    return buf;
583
}
584

    
585
int ff_get_line(ByteIOContext *s, char *buf, int maxlen)
586
{
587
    int i = 0;
588
    char c;
589

    
590
    do {
591
        c = get_byte(s);
592
        if (c && i < maxlen-1)
593
            buf[i++] = c;
594
    } while (c != '\n' && c);
595

    
596
    buf[i] = 0;
597
    return i;
598
}
599

    
600
uint64_t get_be64(ByteIOContext *s)
601
{
602
    uint64_t val;
603
    val = (uint64_t)get_be32(s) << 32;
604
    val |= (uint64_t)get_be32(s);
605
    return val;
606
}
607

    
608
uint64_t ff_get_v(ByteIOContext *bc){
609
    uint64_t val = 0;
610
    int tmp;
611

    
612
    do{
613
        tmp = get_byte(bc);
614
        val= (val<<7) + (tmp&127);
615
    }while(tmp&128);
616
    return val;
617
}
618

    
619
int url_fdopen(ByteIOContext **s, URLContext *h)
620
{
621
    uint8_t *buffer;
622
    int buffer_size, max_packet_size;
623

    
624
    max_packet_size = url_get_max_packet_size(h);
625
    if (max_packet_size) {
626
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
627
    } else {
628
        buffer_size = IO_BUFFER_SIZE;
629
    }
630
    buffer = av_malloc(buffer_size);
631
    if (!buffer)
632
        return AVERROR(ENOMEM);
633

    
634
    *s = av_mallocz(sizeof(ByteIOContext));
635
    if(!*s) {
636
        av_free(buffer);
637
        return AVERROR(ENOMEM);
638
    }
639

    
640
    if (init_put_byte(*s, buffer, buffer_size,
641
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
642
                      url_read, url_write, url_seek) < 0) {
643
        av_free(buffer);
644
        av_freep(s);
645
        return AVERROR(EIO);
646
    }
647
    (*s)->is_streamed = h->is_streamed;
648
    (*s)->max_packet_size = max_packet_size;
649
    if(h->prot) {
650
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
651
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
652
    }
653
    return 0;
654
}
655

    
656
int url_setbufsize(ByteIOContext *s, int buf_size)
657
{
658
    uint8_t *buffer;
659
    buffer = av_malloc(buf_size);
660
    if (!buffer)
661
        return AVERROR(ENOMEM);
662

    
663
    av_free(s->buffer);
664
    s->buffer = buffer;
665
    s->buffer_size = buf_size;
666
    s->buf_ptr = buffer;
667
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
668
    return 0;
669
}
670

    
671
#if FF_API_URL_RESETBUF
672
int url_resetbuf(ByteIOContext *s, int flags)
673
#else
674
static int url_resetbuf(ByteIOContext *s, int flags)
675
#endif
676
{
677
#if FF_API_URL_RESETBUF
678
    if (flags & URL_RDWR)
679
        return AVERROR(EINVAL);
680
#else
681
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
682
#endif
683

    
684
    if (flags & URL_WRONLY) {
685
        s->buf_end = s->buffer + s->buffer_size;
686
        s->write_flag = 1;
687
    } else {
688
        s->buf_end = s->buffer;
689
        s->write_flag = 0;
690
    }
691
    return 0;
692
}
693

    
694
int ff_rewind_with_probe_data(ByteIOContext *s, unsigned char *buf, int buf_size)
695
{
696
    int64_t buffer_start;
697
    int buffer_size;
698
    int overlap, new_size, alloc_size;
699

    
700
    if (s->write_flag)
701
        return AVERROR(EINVAL);
702

    
703
    buffer_size = s->buf_end - s->buffer;
704

    
705
    /* the buffers must touch or overlap */
706
    if ((buffer_start = s->pos - buffer_size) > buf_size)
707
        return AVERROR(EINVAL);
708

    
709
    overlap = buf_size - buffer_start;
710
    new_size = buf_size + buffer_size - overlap;
711

    
712
    alloc_size = FFMAX(s->buffer_size, new_size);
713
    if (alloc_size > buf_size)
714
        if (!(buf = av_realloc(buf, alloc_size)))
715
            return AVERROR(ENOMEM);
716

    
717
    if (new_size > buf_size) {
718
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
719
        buf_size = new_size;
720
    }
721

    
722
    av_free(s->buffer);
723
    s->buf_ptr = s->buffer = buf;
724
    s->buffer_size = alloc_size;
725
    s->pos = buf_size;
726
    s->buf_end = s->buf_ptr + buf_size;
727
    s->eof_reached = 0;
728
    s->must_flush = 0;
729

    
730
    return 0;
731
}
732

    
733
int url_fopen(ByteIOContext **s, const char *filename, int flags)
734
{
735
    URLContext *h;
736
    int err;
737

    
738
    err = url_open(&h, filename, flags);
739
    if (err < 0)
740
        return err;
741
    err = url_fdopen(s, h);
742
    if (err < 0) {
743
        url_close(h);
744
        return err;
745
    }
746
    return 0;
747
}
748

    
749
int url_fclose(ByteIOContext *s)
750
{
751
    URLContext *h = s->opaque;
752

    
753
    av_free(s->buffer);
754
    av_free(s);
755
    return url_close(h);
756
}
757

    
758
URLContext *url_fileno(ByteIOContext *s)
759
{
760
    return s->opaque;
761
}
762

    
763
#if CONFIG_MUXERS
764
int url_fprintf(ByteIOContext *s, const char *fmt, ...)
765
{
766
    va_list ap;
767
    char buf[4096];
768
    int ret;
769

    
770
    va_start(ap, fmt);
771
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
772
    va_end(ap);
773
    put_buffer(s, buf, strlen(buf));
774
    return ret;
775
}
776
#endif //CONFIG_MUXERS
777

    
778
char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
779
{
780
    int c;
781
    char *q;
782

    
783
    c = url_fgetc(s);
784
    if (c == EOF)
785
        return NULL;
786
    q = buf;
787
    for(;;) {
788
        if (c == EOF || c == '\n')
789
            break;
790
        if ((q - buf) < buf_size - 1)
791
            *q++ = c;
792
        c = url_fgetc(s);
793
    }
794
    if (buf_size > 0)
795
        *q = '\0';
796
    return buf;
797
}
798

    
799
int url_fget_max_packet_size(ByteIOContext *s)
800
{
801
    return s->max_packet_size;
802
}
803

    
804
int av_url_read_fpause(ByteIOContext *s, int pause)
805
{
806
    if (!s->read_pause)
807
        return AVERROR(ENOSYS);
808
    return s->read_pause(s->opaque, pause);
809
}
810

    
811
int64_t av_url_read_fseek(ByteIOContext *s, int stream_index,
812
                          int64_t timestamp, int flags)
813
{
814
    URLContext *h = s->opaque;
815
    int64_t ret;
816
    if (!s->read_seek)
817
        return AVERROR(ENOSYS);
818
    ret = s->read_seek(h, stream_index, timestamp, flags);
819
    if(ret >= 0) {
820
        int64_t pos;
821
        s->buf_ptr = s->buf_end; // Flush buffer
822
        pos = s->seek(h, 0, SEEK_CUR);
823
        if (pos >= 0)
824
            s->pos = pos;
825
        else if (pos != AVERROR(ENOSYS))
826
            ret = pos;
827
    }
828
    return ret;
829
}
830

    
831
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
832
 * back to the server even if CONFIG_MUXERS is false. */
833
#if CONFIG_MUXERS || CONFIG_NETWORK
834
/* buffer handling */
835
int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags)
836
{
837
    int ret;
838
    *s = av_mallocz(sizeof(ByteIOContext));
839
    if(!*s)
840
        return AVERROR(ENOMEM);
841
    ret = init_put_byte(*s, buf, buf_size,
842
                        (flags & URL_WRONLY || flags & URL_RDWR),
843
                        NULL, NULL, NULL, NULL);
844
    if(ret != 0)
845
        av_freep(s);
846
    return ret;
847
}
848

    
849
int url_close_buf(ByteIOContext *s)
850
{
851
    put_flush_packet(s);
852
    return s->buf_ptr - s->buffer;
853
}
854

    
855
/* output in a dynamic buffer */
856

    
857
typedef struct DynBuffer {
858
    int pos, size, allocated_size;
859
    uint8_t *buffer;
860
    int io_buffer_size;
861
    uint8_t io_buffer[1];
862
} DynBuffer;
863

    
864
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
865
{
866
    DynBuffer *d = opaque;
867
    unsigned new_size, new_allocated_size;
868

    
869
    /* reallocate buffer if needed */
870
    new_size = d->pos + buf_size;
871
    new_allocated_size = d->allocated_size;
872
    if(new_size < d->pos || new_size > INT_MAX/2)
873
        return -1;
874
    while (new_size > new_allocated_size) {
875
        if (!new_allocated_size)
876
            new_allocated_size = new_size;
877
        else
878
            new_allocated_size += new_allocated_size / 2 + 1;
879
    }
880

    
881
    if (new_allocated_size > d->allocated_size) {
882
        d->buffer = av_realloc(d->buffer, new_allocated_size);
883
        if(d->buffer == NULL)
884
             return AVERROR(ENOMEM);
885
        d->allocated_size = new_allocated_size;
886
    }
887
    memcpy(d->buffer + d->pos, buf, buf_size);
888
    d->pos = new_size;
889
    if (d->pos > d->size)
890
        d->size = d->pos;
891
    return buf_size;
892
}
893

    
894
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
895
{
896
    unsigned char buf1[4];
897
    int ret;
898

    
899
    /* packetized write: output the header */
900
    AV_WB32(buf1, buf_size);
901
    ret= dyn_buf_write(opaque, buf1, 4);
902
    if(ret < 0)
903
        return ret;
904

    
905
    /* then the data */
906
    return dyn_buf_write(opaque, buf, buf_size);
907
}
908

    
909
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
910
{
911
    DynBuffer *d = opaque;
912

    
913
    if (whence == SEEK_CUR)
914
        offset += d->pos;
915
    else if (whence == SEEK_END)
916
        offset += d->size;
917
    if (offset < 0 || offset > 0x7fffffffLL)
918
        return -1;
919
    d->pos = offset;
920
    return 0;
921
}
922

    
923
static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size)
924
{
925
    DynBuffer *d;
926
    int ret;
927
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
928

    
929
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
930
        return -1;
931
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
932
    if (!d)
933
        return AVERROR(ENOMEM);
934
    *s = av_mallocz(sizeof(ByteIOContext));
935
    if(!*s) {
936
        av_free(d);
937
        return AVERROR(ENOMEM);
938
    }
939
    d->io_buffer_size = io_buffer_size;
940
    ret = init_put_byte(*s, d->io_buffer, io_buffer_size,
941
                        1, d, NULL,
942
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
943
                        max_packet_size ? NULL : dyn_buf_seek);
944
    if (ret == 0) {
945
        (*s)->max_packet_size = max_packet_size;
946
    } else {
947
        av_free(d);
948
        av_freep(s);
949
    }
950
    return ret;
951
}
952

    
953
int url_open_dyn_buf(ByteIOContext **s)
954
{
955
    return url_open_dyn_buf_internal(s, 0);
956
}
957

    
958
int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size)
959
{
960
    if (max_packet_size <= 0)
961
        return -1;
962
    return url_open_dyn_buf_internal(s, max_packet_size);
963
}
964

    
965
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
966
{
967
    DynBuffer *d = s->opaque;
968
    int size;
969
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
970
    int padding = 0;
971

    
972
    /* don't attempt to pad fixed-size packet buffers */
973
    if (!s->max_packet_size) {
974
        put_buffer(s, padbuf, sizeof(padbuf));
975
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
976
    }
977

    
978
    put_flush_packet(s);
979

    
980
    *pbuffer = d->buffer;
981
    size = d->size;
982
    av_free(d);
983
    av_free(s);
984
    return size - padding;
985
}
986
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */