Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 603e5c0b

History | View | Annotate | Download (22.3 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
static void fill_buffer(ByteIOContext *s);
32
#if LIBAVFORMAT_VERSION_MAJOR >= 53
33
static int url_resetbuf(ByteIOContext *s, int flags);
34
#endif
35

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

    
69
ByteIOContext *av_alloc_put_byte(
70
                  unsigned char *buffer,
71
                  int buffer_size,
72
                  int write_flag,
73
                  void *opaque,
74
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
75
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
76
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
77
{
78
    ByteIOContext *s = av_mallocz(sizeof(ByteIOContext));
79
    init_put_byte(s, buffer, buffer_size, write_flag, opaque,
80
                  read_packet, write_packet, seek);
81
    return s;
82
}
83

    
84
static void flush_buffer(ByteIOContext *s)
85
{
86
    if (s->buf_ptr > s->buffer) {
87
        if (s->write_packet && !s->error){
88
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
89
            if(ret < 0){
90
                s->error = ret;
91
            }
92
        }
93
        if(s->update_checksum){
94
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
95
            s->checksum_ptr= s->buffer;
96
        }
97
        s->pos += s->buf_ptr - s->buffer;
98
    }
99
    s->buf_ptr = s->buffer;
100
}
101

    
102
void put_byte(ByteIOContext *s, int b)
103
{
104
    *(s->buf_ptr)++ = b;
105
    if (s->buf_ptr >= s->buf_end)
106
        flush_buffer(s);
107
}
108

    
109
void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
110
{
111
    while (size > 0) {
112
        int len = FFMIN(s->buf_end - s->buf_ptr, size);
113
        memcpy(s->buf_ptr, buf, len);
114
        s->buf_ptr += len;
115

    
116
        if (s->buf_ptr >= s->buf_end)
117
            flush_buffer(s);
118

    
119
        buf += len;
120
        size -= len;
121
    }
122
}
123

    
124
void put_flush_packet(ByteIOContext *s)
125
{
126
    flush_buffer(s);
127
    s->must_flush = 0;
128
}
129

    
130
int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence)
131
{
132
    int64_t offset1;
133
    int64_t pos;
134
    int force = whence & AVSEEK_FORCE;
135
    whence &= ~AVSEEK_FORCE;
136

    
137
    if(!s)
138
        return AVERROR(EINVAL);
139

    
140
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
141

    
142
    if (whence != SEEK_CUR && whence != SEEK_SET)
143
        return AVERROR(EINVAL);
144

    
145
    if (whence == SEEK_CUR) {
146
        offset1 = pos + (s->buf_ptr - s->buffer);
147
        if (offset == 0)
148
            return offset1;
149
        offset += offset1;
150
    }
151
    offset1 = offset - pos;
152
    if (!s->must_flush &&
153
        offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
154
        /* can do the seek inside the buffer */
155
        s->buf_ptr = s->buffer + offset1;
156
    } else if(s->is_streamed && !s->write_flag && offset1 >= 0 &&
157
              (whence != SEEK_END || force)) {
158
        while(s->pos < offset && !s->eof_reached)
159
            fill_buffer(s);
160
        if (s->eof_reached)
161
            return AVERROR_EOF;
162
        s->buf_ptr = s->buf_end + offset - s->pos;
163
    } else {
164
        int64_t res;
165

    
166
#if CONFIG_MUXERS || CONFIG_NETWORK
167
        if (s->write_flag) {
168
            flush_buffer(s);
169
            s->must_flush = 1;
170
        }
171
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
172
        if (!s->seek)
173
            return AVERROR(EPIPE);
174
        if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
175
            return res;
176
        if (!s->write_flag)
177
            s->buf_end = s->buffer;
178
        s->buf_ptr = s->buffer;
179
        s->pos = offset;
180
    }
181
    s->eof_reached = 0;
182
    return offset;
183
}
184

    
185
void url_fskip(ByteIOContext *s, int64_t offset)
186
{
187
    url_fseek(s, offset, SEEK_CUR);
188
}
189

    
190
int64_t url_ftell(ByteIOContext *s)
191
{
192
    return url_fseek(s, 0, SEEK_CUR);
193
}
194

    
195
int64_t url_fsize(ByteIOContext *s)
196
{
197
    int64_t size;
198

    
199
    if(!s)
200
        return AVERROR(EINVAL);
201

    
202
    if (!s->seek)
203
        return AVERROR(ENOSYS);
204
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
205
    if(size<0){
206
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
207
            return size;
208
        size++;
209
        s->seek(s->opaque, s->pos, SEEK_SET);
210
    }
211
    return size;
212
}
213

    
214
int url_feof(ByteIOContext *s)
215
{
216
    if(!s)
217
        return 0;
218
    return s->eof_reached;
219
}
220

    
221
int url_ferror(ByteIOContext *s)
222
{
223
    if(!s)
224
        return 0;
225
    return s->error;
226
}
227

    
228
void put_le32(ByteIOContext *s, unsigned int val)
229
{
230
    put_byte(s, val);
231
    put_byte(s, val >> 8);
232
    put_byte(s, val >> 16);
233
    put_byte(s, val >> 24);
234
}
235

    
236
void put_be32(ByteIOContext *s, unsigned int val)
237
{
238
    put_byte(s, val >> 24);
239
    put_byte(s, val >> 16);
240
    put_byte(s, val >> 8);
241
    put_byte(s, val);
242
}
243

    
244
void put_strz(ByteIOContext *s, const char *str)
245
{
246
    if (str)
247
        put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
248
    else
249
        put_byte(s, 0);
250
}
251

    
252
int ff_get_v_length(uint64_t val){
253
    int i=1;
254

    
255
    while(val>>=7)
256
        i++;
257

    
258
    return i;
259
}
260

    
261
void ff_put_v(ByteIOContext *bc, uint64_t val){
262
    int i= ff_get_v_length(val);
263

    
264
    while(--i>0)
265
        put_byte(bc, 128 | (val>>(7*i)));
266

    
267
    put_byte(bc, val&127);
268
}
269

    
270
void put_le64(ByteIOContext *s, uint64_t val)
271
{
272
    put_le32(s, (uint32_t)(val & 0xffffffff));
273
    put_le32(s, (uint32_t)(val >> 32));
274
}
275

    
276
void put_be64(ByteIOContext *s, uint64_t val)
277
{
278
    put_be32(s, (uint32_t)(val >> 32));
279
    put_be32(s, (uint32_t)(val & 0xffffffff));
280
}
281

    
282
void put_le16(ByteIOContext *s, unsigned int val)
283
{
284
    put_byte(s, val);
285
    put_byte(s, val >> 8);
286
}
287

    
288
void put_be16(ByteIOContext *s, unsigned int val)
289
{
290
    put_byte(s, val >> 8);
291
    put_byte(s, val);
292
}
293

    
294
void put_le24(ByteIOContext *s, unsigned int val)
295
{
296
    put_le16(s, val & 0xffff);
297
    put_byte(s, val >> 16);
298
}
299

    
300
void put_be24(ByteIOContext *s, unsigned int val)
301
{
302
    put_be16(s, val >> 8);
303
    put_byte(s, val);
304
}
305

    
306
void put_tag(ByteIOContext *s, const char *tag)
307
{
308
    while (*tag) {
309
        put_byte(s, *tag++);
310
    }
311
}
312

    
313
/* Input stream */
314

    
315
static void fill_buffer(ByteIOContext *s)
316
{
317
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
318
    int len= s->buffer_size - (dst - s->buffer);
319
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
320

    
321
    assert(s->buf_ptr == s->buf_end);
322

    
323
    /* no need to do anything if EOF already reached */
324
    if (s->eof_reached)
325
        return;
326

    
327
    if(s->update_checksum && dst == s->buffer){
328
        if(s->buf_end > s->checksum_ptr)
329
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
330
        s->checksum_ptr= s->buffer;
331
    }
332

    
333
    /* make buffer smaller in case it ended up large after probing */
334
    if (s->buffer_size > max_buffer_size) {
335
        url_setbufsize(s, max_buffer_size);
336

    
337
        s->checksum_ptr = dst = s->buffer;
338
        len = s->buffer_size;
339
    }
340

    
341
    if(s->read_packet)
342
        len = s->read_packet(s->opaque, dst, len);
343
    else
344
        len = 0;
345
    if (len <= 0) {
346
        /* do not modify buffer if EOF reached so that a seek back can
347
           be done without rereading data */
348
        s->eof_reached = 1;
349
        if(len<0)
350
            s->error= len;
351
    } else {
352
        s->pos += len;
353
        s->buf_ptr = dst;
354
        s->buf_end = dst + len;
355
    }
356
}
357

    
358
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
359
                                    unsigned int len)
360
{
361
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
362
}
363

    
364
unsigned long get_checksum(ByteIOContext *s)
365
{
366
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
367
    s->update_checksum= NULL;
368
    return s->checksum;
369
}
370

    
371
void init_checksum(ByteIOContext *s,
372
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
373
                   unsigned long checksum)
374
{
375
    s->update_checksum= update_checksum;
376
    if(s->update_checksum){
377
        s->checksum= checksum;
378
        s->checksum_ptr= s->buf_ptr;
379
    }
380
}
381

    
382
/* XXX: put an inline version */
383
int get_byte(ByteIOContext *s)
384
{
385
    if (s->buf_ptr < s->buf_end) {
386
        return *s->buf_ptr++;
387
    } else {
388
        fill_buffer(s);
389
        if (s->buf_ptr < s->buf_end)
390
            return *s->buf_ptr++;
391
        else
392
            return 0;
393
    }
394
}
395

    
396
int url_fgetc(ByteIOContext *s)
397
{
398
    if (s->buf_ptr < s->buf_end) {
399
        return *s->buf_ptr++;
400
    } else {
401
        fill_buffer(s);
402
        if (s->buf_ptr < s->buf_end)
403
            return *s->buf_ptr++;
404
        else
405
            return URL_EOF;
406
    }
407
}
408

    
409
int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
410
{
411
    int len, size1;
412

    
413
    size1 = size;
414
    while (size > 0) {
415
        len = s->buf_end - s->buf_ptr;
416
        if (len > size)
417
            len = size;
418
        if (len == 0) {
419
            if(size > s->buffer_size && !s->update_checksum){
420
                if(s->read_packet)
421
                    len = s->read_packet(s->opaque, buf, size);
422
                if (len <= 0) {
423
                    /* do not modify buffer if EOF reached so that a seek back can
424
                    be done without rereading data */
425
                    s->eof_reached = 1;
426
                    if(len<0)
427
                        s->error= len;
428
                    break;
429
                } else {
430
                    s->pos += len;
431
                    size -= len;
432
                    buf += len;
433
                    s->buf_ptr = s->buffer;
434
                    s->buf_end = s->buffer/* + len*/;
435
                }
436
            }else{
437
                fill_buffer(s);
438
                len = s->buf_end - s->buf_ptr;
439
                if (len == 0)
440
                    break;
441
            }
442
        } else {
443
            memcpy(buf, s->buf_ptr, len);
444
            buf += len;
445
            s->buf_ptr += len;
446
            size -= len;
447
        }
448
    }
449
    if (size1 == size) {
450
        if (url_ferror(s)) return url_ferror(s);
451
        if (url_feof(s))   return AVERROR_EOF;
452
    }
453
    return size1 - size;
454
}
455

    
456
int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
457
{
458
    int len;
459

    
460
    if(size<0)
461
        return -1;
462

    
463
    len = s->buf_end - s->buf_ptr;
464
    if (len == 0) {
465
        fill_buffer(s);
466
        len = s->buf_end - s->buf_ptr;
467
    }
468
    if (len > size)
469
        len = size;
470
    memcpy(buf, s->buf_ptr, len);
471
    s->buf_ptr += len;
472
    if (!len) {
473
        if (url_ferror(s)) return url_ferror(s);
474
        if (url_feof(s))   return AVERROR_EOF;
475
    }
476
    return len;
477
}
478

    
479
unsigned int get_le16(ByteIOContext *s)
480
{
481
    unsigned int val;
482
    val = get_byte(s);
483
    val |= get_byte(s) << 8;
484
    return val;
485
}
486

    
487
unsigned int get_le24(ByteIOContext *s)
488
{
489
    unsigned int val;
490
    val = get_le16(s);
491
    val |= get_byte(s) << 16;
492
    return val;
493
}
494

    
495
unsigned int get_le32(ByteIOContext *s)
496
{
497
    unsigned int val;
498
    val = get_le16(s);
499
    val |= get_le16(s) << 16;
500
    return val;
501
}
502

    
503
uint64_t get_le64(ByteIOContext *s)
504
{
505
    uint64_t val;
506
    val = (uint64_t)get_le32(s);
507
    val |= (uint64_t)get_le32(s) << 32;
508
    return val;
509
}
510

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

    
519
unsigned int get_be24(ByteIOContext *s)
520
{
521
    unsigned int val;
522
    val = get_be16(s) << 8;
523
    val |= get_byte(s);
524
    return val;
525
}
526
unsigned int get_be32(ByteIOContext *s)
527
{
528
    unsigned int val;
529
    val = get_be16(s) << 16;
530
    val |= get_be16(s);
531
    return val;
532
}
533

    
534
char *get_strz(ByteIOContext *s, char *buf, int maxlen)
535
{
536
    int i = 0;
537
    char c;
538

    
539
    while ((c = get_byte(s))) {
540
        if (i < maxlen-1)
541
            buf[i++] = c;
542
    }
543

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

    
546
    return buf;
547
}
548

    
549
uint64_t get_be64(ByteIOContext *s)
550
{
551
    uint64_t val;
552
    val = (uint64_t)get_be32(s) << 32;
553
    val |= (uint64_t)get_be32(s);
554
    return val;
555
}
556

    
557
uint64_t ff_get_v(ByteIOContext *bc){
558
    uint64_t val = 0;
559
    int tmp;
560

    
561
    do{
562
        tmp = get_byte(bc);
563
        val= (val<<7) + (tmp&127);
564
    }while(tmp&128);
565
    return val;
566
}
567

    
568
int url_fdopen(ByteIOContext **s, URLContext *h)
569
{
570
    uint8_t *buffer;
571
    int buffer_size, max_packet_size;
572

    
573
    max_packet_size = url_get_max_packet_size(h);
574
    if (max_packet_size) {
575
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
576
    } else {
577
        buffer_size = IO_BUFFER_SIZE;
578
    }
579
    buffer = av_malloc(buffer_size);
580
    if (!buffer)
581
        return AVERROR(ENOMEM);
582

    
583
    *s = av_mallocz(sizeof(ByteIOContext));
584
    if(!*s) {
585
        av_free(buffer);
586
        return AVERROR(ENOMEM);
587
    }
588

    
589
    if (init_put_byte(*s, buffer, buffer_size,
590
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
591
                      url_read, url_write, url_seek) < 0) {
592
        av_free(buffer);
593
        av_freep(s);
594
        return AVERROR(EIO);
595
    }
596
    (*s)->is_streamed = h->is_streamed;
597
    (*s)->max_packet_size = max_packet_size;
598
    if(h->prot) {
599
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
600
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
601
    }
602
    return 0;
603
}
604

    
605
int url_setbufsize(ByteIOContext *s, int buf_size)
606
{
607
    uint8_t *buffer;
608
    buffer = av_malloc(buf_size);
609
    if (!buffer)
610
        return AVERROR(ENOMEM);
611

    
612
    av_free(s->buffer);
613
    s->buffer = buffer;
614
    s->buffer_size = buf_size;
615
    s->buf_ptr = buffer;
616
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
617
    return 0;
618
}
619

    
620
#if LIBAVFORMAT_VERSION_MAJOR < 53
621
int url_resetbuf(ByteIOContext *s, int flags)
622
#else
623
static int url_resetbuf(ByteIOContext *s, int flags)
624
#endif
625
{
626
#if LIBAVFORMAT_VERSION_MAJOR < 53
627
    if (flags & URL_RDWR)
628
        return AVERROR(EINVAL);
629
#else
630
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
631
#endif
632

    
633
    if (flags & URL_WRONLY) {
634
        s->buf_end = s->buffer + s->buffer_size;
635
        s->write_flag = 1;
636
    } else {
637
        s->buf_end = s->buffer;
638
        s->write_flag = 0;
639
    }
640
    return 0;
641
}
642

    
643
int ff_rewind_with_probe_data(ByteIOContext *s, unsigned char *buf, int buf_size)
644
{
645
    int64_t buffer_start;
646
    int buffer_size;
647
    int overlap, new_size;
648

    
649
    if (s->write_flag)
650
        return AVERROR(EINVAL);
651

    
652
    buffer_size = s->buf_end - s->buffer;
653

    
654
    /* the buffers must touch or overlap */
655
    if ((buffer_start = s->pos - buffer_size) > buf_size)
656
        return AVERROR(EINVAL);
657

    
658
    overlap = buf_size - buffer_start;
659
    new_size = buf_size + buffer_size - overlap;
660

    
661
    if (new_size > buf_size) {
662
        if (!(buf = av_realloc(buf, new_size)))
663
            return AVERROR(ENOMEM);
664

    
665
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
666
        buf_size = new_size;
667
    }
668

    
669
    av_free(s->buffer);
670
    s->buf_ptr = s->buffer = buf;
671
    s->pos = s->buffer_size = buf_size;
672
    s->buf_end = s->buf_ptr + buf_size;
673
    s->eof_reached = 0;
674
    s->must_flush = 0;
675

    
676
    return 0;
677
}
678

    
679
int url_fopen(ByteIOContext **s, const char *filename, int flags)
680
{
681
    URLContext *h;
682
    int err;
683

    
684
    err = url_open(&h, filename, flags);
685
    if (err < 0)
686
        return err;
687
    err = url_fdopen(s, h);
688
    if (err < 0) {
689
        url_close(h);
690
        return err;
691
    }
692
    return 0;
693
}
694

    
695
int url_fclose(ByteIOContext *s)
696
{
697
    URLContext *h = s->opaque;
698

    
699
    av_free(s->buffer);
700
    av_free(s);
701
    return url_close(h);
702
}
703

    
704
URLContext *url_fileno(ByteIOContext *s)
705
{
706
    return s->opaque;
707
}
708

    
709
#if CONFIG_MUXERS
710
int url_fprintf(ByteIOContext *s, const char *fmt, ...)
711
{
712
    va_list ap;
713
    char buf[4096];
714
    int ret;
715

    
716
    va_start(ap, fmt);
717
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
718
    va_end(ap);
719
    put_buffer(s, buf, strlen(buf));
720
    return ret;
721
}
722
#endif //CONFIG_MUXERS
723

    
724
char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
725
{
726
    int c;
727
    char *q;
728

    
729
    c = url_fgetc(s);
730
    if (c == EOF)
731
        return NULL;
732
    q = buf;
733
    for(;;) {
734
        if (c == EOF || c == '\n')
735
            break;
736
        if ((q - buf) < buf_size - 1)
737
            *q++ = c;
738
        c = url_fgetc(s);
739
    }
740
    if (buf_size > 0)
741
        *q = '\0';
742
    return buf;
743
}
744

    
745
int url_fget_max_packet_size(ByteIOContext *s)
746
{
747
    return s->max_packet_size;
748
}
749

    
750
int av_url_read_fpause(ByteIOContext *s, int pause)
751
{
752
    if (!s->read_pause)
753
        return AVERROR(ENOSYS);
754
    return s->read_pause(s->opaque, pause);
755
}
756

    
757
int64_t av_url_read_fseek(ByteIOContext *s, int stream_index,
758
                          int64_t timestamp, int flags)
759
{
760
    URLContext *h = s->opaque;
761
    int64_t ret;
762
    if (!s->read_seek)
763
        return AVERROR(ENOSYS);
764
    ret = s->read_seek(h, stream_index, timestamp, flags);
765
    if(ret >= 0) {
766
        int64_t pos;
767
        s->buf_ptr = s->buf_end; // Flush buffer
768
        pos = s->seek(h, 0, SEEK_CUR);
769
        if (pos >= 0)
770
            s->pos = pos;
771
        else if (pos != AVERROR(ENOSYS))
772
            ret = pos;
773
    }
774
    return ret;
775
}
776

    
777
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
778
 * back to the server even if CONFIG_MUXERS is false. */
779
#if CONFIG_MUXERS || CONFIG_NETWORK
780
/* buffer handling */
781
int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags)
782
{
783
    int ret;
784
    *s = av_mallocz(sizeof(ByteIOContext));
785
    if(!*s)
786
        return AVERROR(ENOMEM);
787
    ret = init_put_byte(*s, buf, buf_size,
788
                        (flags & URL_WRONLY || flags & URL_RDWR),
789
                        NULL, NULL, NULL, NULL);
790
    if(ret != 0)
791
        av_freep(s);
792
    return ret;
793
}
794

    
795
int url_close_buf(ByteIOContext *s)
796
{
797
    put_flush_packet(s);
798
    return s->buf_ptr - s->buffer;
799
}
800

    
801
/* output in a dynamic buffer */
802

    
803
typedef struct DynBuffer {
804
    int pos, size, allocated_size;
805
    uint8_t *buffer;
806
    int io_buffer_size;
807
    uint8_t io_buffer[1];
808
} DynBuffer;
809

    
810
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
811
{
812
    DynBuffer *d = opaque;
813
    unsigned new_size, new_allocated_size;
814

    
815
    /* reallocate buffer if needed */
816
    new_size = d->pos + buf_size;
817
    new_allocated_size = d->allocated_size;
818
    if(new_size < d->pos || new_size > INT_MAX/2)
819
        return -1;
820
    while (new_size > new_allocated_size) {
821
        if (!new_allocated_size)
822
            new_allocated_size = new_size;
823
        else
824
            new_allocated_size += new_allocated_size / 2 + 1;
825
    }
826

    
827
    if (new_allocated_size > d->allocated_size) {
828
        d->buffer = av_realloc(d->buffer, new_allocated_size);
829
        if(d->buffer == NULL)
830
             return AVERROR(ENOMEM);
831
        d->allocated_size = new_allocated_size;
832
    }
833
    memcpy(d->buffer + d->pos, buf, buf_size);
834
    d->pos = new_size;
835
    if (d->pos > d->size)
836
        d->size = d->pos;
837
    return buf_size;
838
}
839

    
840
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
841
{
842
    unsigned char buf1[4];
843
    int ret;
844

    
845
    /* packetized write: output the header */
846
    AV_WB32(buf1, buf_size);
847
    ret= dyn_buf_write(opaque, buf1, 4);
848
    if(ret < 0)
849
        return ret;
850

    
851
    /* then the data */
852
    return dyn_buf_write(opaque, buf, buf_size);
853
}
854

    
855
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
856
{
857
    DynBuffer *d = opaque;
858

    
859
    if (whence == SEEK_CUR)
860
        offset += d->pos;
861
    else if (whence == SEEK_END)
862
        offset += d->size;
863
    if (offset < 0 || offset > 0x7fffffffLL)
864
        return -1;
865
    d->pos = offset;
866
    return 0;
867
}
868

    
869
static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size)
870
{
871
    DynBuffer *d;
872
    int ret;
873
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
874

    
875
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
876
        return -1;
877
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
878
    if (!d)
879
        return AVERROR(ENOMEM);
880
    *s = av_mallocz(sizeof(ByteIOContext));
881
    if(!*s) {
882
        av_free(d);
883
        return AVERROR(ENOMEM);
884
    }
885
    d->io_buffer_size = io_buffer_size;
886
    ret = init_put_byte(*s, d->io_buffer, io_buffer_size,
887
                        1, d, NULL,
888
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
889
                        max_packet_size ? NULL : dyn_buf_seek);
890
    if (ret == 0) {
891
        (*s)->max_packet_size = max_packet_size;
892
    } else {
893
        av_free(d);
894
        av_freep(s);
895
    }
896
    return ret;
897
}
898

    
899
int url_open_dyn_buf(ByteIOContext **s)
900
{
901
    return url_open_dyn_buf_internal(s, 0);
902
}
903

    
904
int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size)
905
{
906
    if (max_packet_size <= 0)
907
        return -1;
908
    return url_open_dyn_buf_internal(s, max_packet_size);
909
}
910

    
911
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
912
{
913
    DynBuffer *d = s->opaque;
914
    int size;
915
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
916
    int padding = 0;
917

    
918
    /* don't attempt to pad fixed-size packet buffers */
919
    if (!s->max_packet_size) {
920
        put_buffer(s, padbuf, sizeof(padbuf));
921
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
922
    }
923

    
924
    put_flush_packet(s);
925

    
926
    *pbuffer = d->buffer;
927
    size = d->size;
928
    av_free(d);
929
    av_free(s);
930
    return size - padding;
931
}
932
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */