Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 403ee835

History | View | Annotate | Download (28.3 KB)

1
/*
2
 * Buffered I/O for ffmpeg system
3
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

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

    
30
#define IO_BUFFER_SIZE 32768
31

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

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

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

    
80
#if FF_API_OLD_AVIO
81
int init_put_byte(AVIOContext *s,
82
                  unsigned char *buffer,
83
                  int buffer_size,
84
                  int write_flag,
85
                  void *opaque,
86
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
87
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
88
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
89
{
90
    return ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
91
                                read_packet, write_packet, seek);
92
}
93
AVIOContext *av_alloc_put_byte(
94
                  unsigned char *buffer,
95
                  int buffer_size,
96
                  int write_flag,
97
                  void *opaque,
98
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
99
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
100
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
101
{
102
    return avio_alloc_context(buffer, buffer_size, write_flag, opaque,
103
                              read_packet, write_packet, seek);
104
}
105
#endif
106

    
107
AVIOContext *avio_alloc_context(
108
                  unsigned char *buffer,
109
                  int buffer_size,
110
                  int write_flag,
111
                  void *opaque,
112
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
113
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
114
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
115
{
116
    AVIOContext *s = av_mallocz(sizeof(AVIOContext));
117
    ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
118
                  read_packet, write_packet, seek);
119
    return s;
120
}
121

    
122
static void flush_buffer(AVIOContext *s)
123
{
124
    if (s->buf_ptr > s->buffer) {
125
        if (s->write_packet && !s->error){
126
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
127
            if(ret < 0){
128
                s->error = ret;
129
            }
130
        }
131
        if(s->update_checksum){
132
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
133
            s->checksum_ptr= s->buffer;
134
        }
135
        s->pos += s->buf_ptr - s->buffer;
136
    }
137
    s->buf_ptr = s->buffer;
138
}
139

    
140
void avio_w8(AVIOContext *s, int b)
141
{
142
    *(s->buf_ptr)++ = b;
143
    if (s->buf_ptr >= s->buf_end)
144
        flush_buffer(s);
145
}
146

    
147
void ffio_fill(AVIOContext *s, int b, int count)
148
{
149
    while (count > 0) {
150
        int len = FFMIN(s->buf_end - s->buf_ptr, count);
151
        memset(s->buf_ptr, b, len);
152
        s->buf_ptr += len;
153

    
154
        if (s->buf_ptr >= s->buf_end)
155
            flush_buffer(s);
156

    
157
        count -= len;
158
    }
159
}
160

    
161
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
162
{
163
    while (size > 0) {
164
        int len = FFMIN(s->buf_end - s->buf_ptr, size);
165
        memcpy(s->buf_ptr, buf, len);
166
        s->buf_ptr += len;
167

    
168
        if (s->buf_ptr >= s->buf_end)
169
            flush_buffer(s);
170

    
171
        buf += len;
172
        size -= len;
173
    }
174
}
175

    
176
void avio_flush(AVIOContext *s)
177
{
178
    flush_buffer(s);
179
    s->must_flush = 0;
180
}
181

    
182
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
183
{
184
    int64_t offset1;
185
    int64_t pos;
186
    int force = whence & AVSEEK_FORCE;
187
    whence &= ~AVSEEK_FORCE;
188

    
189
    if(!s)
190
        return AVERROR(EINVAL);
191

    
192
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
193

    
194
    if (whence != SEEK_CUR && whence != SEEK_SET)
195
        return AVERROR(EINVAL);
196

    
197
    if (whence == SEEK_CUR) {
198
        offset1 = pos + (s->buf_ptr - s->buffer);
199
        if (offset == 0)
200
            return offset1;
201
        offset += offset1;
202
    }
203
    offset1 = offset - pos;
204
    if (!s->must_flush &&
205
        offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
206
        /* can do the seek inside the buffer */
207
        s->buf_ptr = s->buffer + offset1;
208
    } else if ((!s->seekable ||
209
               offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
210
               !s->write_flag && offset1 >= 0 &&
211
              (whence != SEEK_END || force)) {
212
        while(s->pos < offset && !s->eof_reached)
213
            fill_buffer(s);
214
        if (s->eof_reached)
215
            return AVERROR_EOF;
216
        s->buf_ptr = s->buf_end + offset - s->pos;
217
    } else {
218
        int64_t res;
219

    
220
#if CONFIG_MUXERS || CONFIG_NETWORK
221
        if (s->write_flag) {
222
            flush_buffer(s);
223
            s->must_flush = 1;
224
        }
225
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
226
        if (!s->seek)
227
            return AVERROR(EPIPE);
228
        if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
229
            return res;
230
        if (!s->write_flag)
231
            s->buf_end = s->buffer;
232
        s->buf_ptr = s->buffer;
233
        s->pos = offset;
234
    }
235
    s->eof_reached = 0;
236
    return offset;
237
}
238

    
239
#if FF_API_OLD_AVIO
240
int url_fskip(AVIOContext *s, int64_t offset)
241
{
242
    int64_t ret = avio_seek(s, offset, SEEK_CUR);
243
    return ret < 0 ? ret : 0;
244
}
245

    
246
int64_t url_ftell(AVIOContext *s)
247
{
248
    return avio_seek(s, 0, SEEK_CUR);
249
}
250
#endif
251

    
252
int64_t avio_size(AVIOContext *s)
253
{
254
    int64_t size;
255

    
256
    if(!s)
257
        return AVERROR(EINVAL);
258

    
259
    if (!s->seek)
260
        return AVERROR(ENOSYS);
261
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
262
    if(size<0){
263
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
264
            return size;
265
        size++;
266
        s->seek(s->opaque, s->pos, SEEK_SET);
267
    }
268
    return size;
269
}
270

    
271
#if FF_API_OLD_AVIO
272
int url_feof(AVIOContext *s)
273
{
274
    if(!s)
275
        return 0;
276
    return s->eof_reached;
277
}
278

    
279
int url_ferror(AVIOContext *s)
280
{
281
    if(!s)
282
        return 0;
283
    return s->error;
284
}
285
#endif
286

    
287
void avio_wl32(AVIOContext *s, unsigned int val)
288
{
289
    avio_w8(s, val);
290
    avio_w8(s, val >> 8);
291
    avio_w8(s, val >> 16);
292
    avio_w8(s, val >> 24);
293
}
294

    
295
void avio_wb32(AVIOContext *s, unsigned int val)
296
{
297
    avio_w8(s, val >> 24);
298
    avio_w8(s, val >> 16);
299
    avio_w8(s, val >> 8);
300
    avio_w8(s, val);
301
}
302

    
303
#if FF_API_OLD_AVIO
304
void put_strz(AVIOContext *s, const char *str)
305
{
306
    avio_put_str(s, str);
307
}
308

    
309
#define GET(name, type) \
310
    type get_be ##name(AVIOContext *s) \
311
{\
312
    return avio_rb ##name(s);\
313
}\
314
    type get_le ##name(AVIOContext *s) \
315
{\
316
    return avio_rl ##name(s);\
317
}
318

    
319
GET(16, unsigned int)
320
GET(24, unsigned int)
321
GET(32, unsigned int)
322
GET(64, uint64_t)
323

    
324
#undef GET
325

    
326
#define PUT(name, type ) \
327
    void put_le ##name(AVIOContext *s, type val)\
328
{\
329
        avio_wl ##name(s, val);\
330
}\
331
    void put_be ##name(AVIOContext *s, type val)\
332
{\
333
        avio_wb ##name(s, val);\
334
}
335

    
336
PUT(16, unsigned int)
337
PUT(24, unsigned int)
338
PUT(32, unsigned int)
339
PUT(64, uint64_t)
340
#undef PUT
341

    
342
int get_byte(AVIOContext *s)
343
{
344
   return avio_r8(s);
345
}
346
int get_buffer(AVIOContext *s, unsigned char *buf, int size)
347
{
348
    return avio_read(s, buf, size);
349
}
350
int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size)
351
{
352
    return ffio_read_partial(s, buf, size);
353
}
354
void put_byte(AVIOContext *s, int val)
355
{
356
    avio_w8(s, val);
357
}
358
void put_buffer(AVIOContext *s, const unsigned char *buf, int size)
359
{
360
    avio_write(s, buf, size);
361
}
362
void put_nbyte(AVIOContext *s, int b, int count)
363
{
364
    ffio_fill(s, b, count);
365
}
366

    
367
int url_fopen(AVIOContext **s, const char *filename, int flags)
368
{
369
    return avio_open(s, filename, flags);
370
}
371
int url_fclose(AVIOContext *s)
372
{
373
    return avio_close(s);
374
}
375
int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
376
{
377
    return avio_seek(s, offset, whence);
378
}
379
int64_t url_fsize(AVIOContext *s)
380
{
381
    return avio_size(s);
382
}
383
int url_setbufsize(AVIOContext *s, int buf_size)
384
{
385
    return ffio_set_buf_size(s, buf_size);
386
}
387
int url_fprintf(AVIOContext *s, const char *fmt, ...)
388
{
389
    va_list ap;
390
    char buf[4096];
391
    int ret;
392

    
393
    va_start(ap, fmt);
394
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
395
    va_end(ap);
396
    avio_write(s, buf, strlen(buf));
397
    return ret;
398
}
399
void put_flush_packet(AVIOContext *s)
400
{
401
    avio_flush(s);
402
}
403
int av_url_read_fpause(AVIOContext *s, int pause)
404
{
405
    return ffio_read_pause(s, pause);
406
}
407
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
408
                         int64_t timestamp, int flags)
409
{
410
    return ffio_read_seek(s, stream_index, timestamp, flags);
411
}
412
void init_checksum(AVIOContext *s,
413
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
414
                   unsigned long checksum)
415
{
416
    ffio_init_checksum(s, update_checksum, checksum);
417
}
418
unsigned long get_checksum(AVIOContext *s)
419
{
420
    return ffio_get_checksum(s);
421
}
422
int url_open_dyn_buf(AVIOContext **s)
423
{
424
    return avio_open_dyn_buf(s);
425
}
426
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
427
{
428
    return ffio_open_dyn_packet_buf(s, max_packet_size);
429
}
430
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
431
{
432
    return avio_close_dyn_buf(s, pbuffer);
433
}
434
#endif
435

    
436
int avio_put_str(AVIOContext *s, const char *str)
437
{
438
    int len = 1;
439
    if (str) {
440
        len += strlen(str);
441
        avio_write(s, (const unsigned char *) str, len);
442
    } else
443
        avio_w8(s, 0);
444
    return len;
445
}
446

    
447
int avio_put_str16le(AVIOContext *s, const char *str)
448
{
449
    const uint8_t *q = str;
450
    int ret = 0;
451

    
452
    while (*q) {
453
        uint32_t ch;
454
        uint16_t tmp;
455

    
456
        GET_UTF8(ch, *q++, break;)
457
        PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
458
    }
459
    avio_wl16(s, 0);
460
    ret += 2;
461
    return ret;
462
}
463

    
464
int ff_get_v_length(uint64_t val){
465
    int i=1;
466

    
467
    while(val>>=7)
468
        i++;
469

    
470
    return i;
471
}
472

    
473
void ff_put_v(AVIOContext *bc, uint64_t val){
474
    int i= ff_get_v_length(val);
475

    
476
    while(--i>0)
477
        avio_w8(bc, 128 | (val>>(7*i)));
478

    
479
    avio_w8(bc, val&127);
480
}
481

    
482
void avio_wl64(AVIOContext *s, uint64_t val)
483
{
484
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
485
    avio_wl32(s, (uint32_t)(val >> 32));
486
}
487

    
488
void avio_wb64(AVIOContext *s, uint64_t val)
489
{
490
    avio_wb32(s, (uint32_t)(val >> 32));
491
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
492
}
493

    
494
void avio_wl16(AVIOContext *s, unsigned int val)
495
{
496
    avio_w8(s, val);
497
    avio_w8(s, val >> 8);
498
}
499

    
500
void avio_wb16(AVIOContext *s, unsigned int val)
501
{
502
    avio_w8(s, val >> 8);
503
    avio_w8(s, val);
504
}
505

    
506
void avio_wl24(AVIOContext *s, unsigned int val)
507
{
508
    avio_wl16(s, val & 0xffff);
509
    avio_w8(s, val >> 16);
510
}
511

    
512
void avio_wb24(AVIOContext *s, unsigned int val)
513
{
514
    avio_wb16(s, val >> 8);
515
    avio_w8(s, val);
516
}
517

    
518
#if FF_API_OLD_AVIO
519
void put_tag(AVIOContext *s, const char *tag)
520
{
521
    while (*tag) {
522
        avio_w8(s, *tag++);
523
    }
524
}
525
#endif
526

    
527
/* Input stream */
528

    
529
static void fill_buffer(AVIOContext *s)
530
{
531
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
532
    int len= s->buffer_size - (dst - s->buffer);
533
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
534

    
535
    /* no need to do anything if EOF already reached */
536
    if (s->eof_reached)
537
        return;
538

    
539
    if(s->update_checksum && dst == s->buffer){
540
        if(s->buf_end > s->checksum_ptr)
541
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
542
        s->checksum_ptr= s->buffer;
543
    }
544

    
545
    /* make buffer smaller in case it ended up large after probing */
546
    if (s->buffer_size > max_buffer_size) {
547
        ffio_set_buf_size(s, max_buffer_size);
548

    
549
        s->checksum_ptr = dst = s->buffer;
550
        len = s->buffer_size;
551
    }
552

    
553
    if(s->read_packet)
554
        len = s->read_packet(s->opaque, dst, len);
555
    else
556
        len = 0;
557
    if (len <= 0) {
558
        /* do not modify buffer if EOF reached so that a seek back can
559
           be done without rereading data */
560
        s->eof_reached = 1;
561
        if(len<0)
562
            s->error= len;
563
    } else {
564
        s->pos += len;
565
        s->buf_ptr = dst;
566
        s->buf_end = dst + len;
567
    }
568
}
569

    
570
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
571
                                    unsigned int len)
572
{
573
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
574
}
575

    
576
unsigned long ffio_get_checksum(AVIOContext *s)
577
{
578
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
579
    s->update_checksum= NULL;
580
    return s->checksum;
581
}
582

    
583
void ffio_init_checksum(AVIOContext *s,
584
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
585
                   unsigned long checksum)
586
{
587
    s->update_checksum= update_checksum;
588
    if(s->update_checksum){
589
        s->checksum= checksum;
590
        s->checksum_ptr= s->buf_ptr;
591
    }
592
}
593

    
594
/* XXX: put an inline version */
595
int avio_r8(AVIOContext *s)
596
{
597
    if (s->buf_ptr >= s->buf_end)
598
        fill_buffer(s);
599
    if (s->buf_ptr < s->buf_end)
600
        return *s->buf_ptr++;
601
    return 0;
602
}
603

    
604
#if FF_API_OLD_AVIO
605
int url_fgetc(AVIOContext *s)
606
{
607
    if (s->buf_ptr >= s->buf_end)
608
        fill_buffer(s);
609
    if (s->buf_ptr < s->buf_end)
610
        return *s->buf_ptr++;
611
    return URL_EOF;
612
}
613
#endif
614

    
615
int avio_read(AVIOContext *s, unsigned char *buf, int size)
616
{
617
    int len, size1;
618

    
619
    size1 = size;
620
    while (size > 0) {
621
        len = s->buf_end - s->buf_ptr;
622
        if (len > size)
623
            len = size;
624
        if (len == 0) {
625
            if(size > s->buffer_size && !s->update_checksum){
626
                if(s->read_packet)
627
                    len = s->read_packet(s->opaque, buf, size);
628
                if (len <= 0) {
629
                    /* do not modify buffer if EOF reached so that a seek back can
630
                    be done without rereading data */
631
                    s->eof_reached = 1;
632
                    if(len<0)
633
                        s->error= len;
634
                    break;
635
                } else {
636
                    s->pos += len;
637
                    size -= len;
638
                    buf += len;
639
                    s->buf_ptr = s->buffer;
640
                    s->buf_end = s->buffer/* + len*/;
641
                }
642
            }else{
643
                fill_buffer(s);
644
                len = s->buf_end - s->buf_ptr;
645
                if (len == 0)
646
                    break;
647
            }
648
        } else {
649
            memcpy(buf, s->buf_ptr, len);
650
            buf += len;
651
            s->buf_ptr += len;
652
            size -= len;
653
        }
654
    }
655
    if (size1 == size) {
656
        if (s->error)         return s->error;
657
        if (s->eof_reached)   return AVERROR_EOF;
658
    }
659
    return size1 - size;
660
}
661

    
662
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
663
{
664
    int len;
665

    
666
    if(size<0)
667
        return -1;
668

    
669
    len = s->buf_end - s->buf_ptr;
670
    if (len == 0) {
671
        fill_buffer(s);
672
        len = s->buf_end - s->buf_ptr;
673
    }
674
    if (len > size)
675
        len = size;
676
    memcpy(buf, s->buf_ptr, len);
677
    s->buf_ptr += len;
678
    if (!len) {
679
        if (s->error)         return s->error;
680
        if (s->eof_reached)   return AVERROR_EOF;
681
    }
682
    return len;
683
}
684

    
685
unsigned int avio_rl16(AVIOContext *s)
686
{
687
    unsigned int val;
688
    val = avio_r8(s);
689
    val |= avio_r8(s) << 8;
690
    return val;
691
}
692

    
693
unsigned int avio_rl24(AVIOContext *s)
694
{
695
    unsigned int val;
696
    val = avio_rl16(s);
697
    val |= avio_r8(s) << 16;
698
    return val;
699
}
700

    
701
unsigned int avio_rl32(AVIOContext *s)
702
{
703
    unsigned int val;
704
    val = avio_rl16(s);
705
    val |= avio_rl16(s) << 16;
706
    return val;
707
}
708

    
709
uint64_t avio_rl64(AVIOContext *s)
710
{
711
    uint64_t val;
712
    val = (uint64_t)avio_rl32(s);
713
    val |= (uint64_t)avio_rl32(s) << 32;
714
    return val;
715
}
716

    
717
unsigned int avio_rb16(AVIOContext *s)
718
{
719
    unsigned int val;
720
    val = avio_r8(s) << 8;
721
    val |= avio_r8(s);
722
    return val;
723
}
724

    
725
unsigned int avio_rb24(AVIOContext *s)
726
{
727
    unsigned int val;
728
    val = avio_rb16(s) << 8;
729
    val |= avio_r8(s);
730
    return val;
731
}
732
unsigned int avio_rb32(AVIOContext *s)
733
{
734
    unsigned int val;
735
    val = avio_rb16(s) << 16;
736
    val |= avio_rb16(s);
737
    return val;
738
}
739

    
740
#if FF_API_OLD_AVIO
741
char *get_strz(AVIOContext *s, char *buf, int maxlen)
742
{
743
    avio_get_str(s, INT_MAX, buf, maxlen);
744
    return buf;
745
}
746
#endif
747

    
748
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
749
{
750
    int i = 0;
751
    char c;
752

    
753
    do {
754
        c = avio_r8(s);
755
        if (c && i < maxlen-1)
756
            buf[i++] = c;
757
    } while (c != '\n' && c);
758

    
759
    buf[i] = 0;
760
    return i;
761
}
762

    
763
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
764
{
765
    int i;
766

    
767
    // reserve 1 byte for terminating 0
768
    buflen = FFMIN(buflen - 1, maxlen);
769
    for (i = 0; i < buflen; i++)
770
        if (!(buf[i] = avio_r8(s)))
771
            return i + 1;
772
    if (buflen)
773
        buf[i] = 0;
774
    for (; i < maxlen; i++)
775
        if (!avio_r8(s))
776
            return i + 1;
777
    return maxlen;
778
}
779

    
780
#define GET_STR16(type, read) \
781
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
782
{\
783
    char* q = buf;\
784
    int ret = 0;\
785
    while (ret + 1 < maxlen) {\
786
        uint8_t tmp;\
787
        uint32_t ch;\
788
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
789
        if (!ch)\
790
            break;\
791
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
792
    }\
793
    *q = 0;\
794
    return ret;\
795
}\
796

    
797
GET_STR16(le, avio_rl16)
798
GET_STR16(be, avio_rb16)
799

    
800
#undef GET_STR16
801

    
802
uint64_t avio_rb64(AVIOContext *s)
803
{
804
    uint64_t val;
805
    val = (uint64_t)avio_rb32(s) << 32;
806
    val |= (uint64_t)avio_rb32(s);
807
    return val;
808
}
809

    
810
uint64_t ffio_read_varlen(AVIOContext *bc){
811
    uint64_t val = 0;
812
    int tmp;
813

    
814
    do{
815
        tmp = avio_r8(bc);
816
        val= (val<<7) + (tmp&127);
817
    }while(tmp&128);
818
    return val;
819
}
820

    
821
int url_fdopen(AVIOContext **s, URLContext *h)
822
{
823
    uint8_t *buffer;
824
    int buffer_size, max_packet_size;
825

    
826
    max_packet_size = url_get_max_packet_size(h);
827
    if (max_packet_size) {
828
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
829
    } else {
830
        buffer_size = IO_BUFFER_SIZE;
831
    }
832
    buffer = av_malloc(buffer_size);
833
    if (!buffer)
834
        return AVERROR(ENOMEM);
835

    
836
    *s = av_mallocz(sizeof(AVIOContext));
837
    if(!*s) {
838
        av_free(buffer);
839
        return AVERROR(ENOMEM);
840
    }
841

    
842
    if (ffio_init_context(*s, buffer, buffer_size,
843
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
844
                      url_read, url_write, url_seek) < 0) {
845
        av_free(buffer);
846
        av_freep(s);
847
        return AVERROR(EIO);
848
    }
849
#if FF_API_OLD_AVIO
850
    (*s)->is_streamed = h->is_streamed;
851
#endif
852
    (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
853
    (*s)->max_packet_size = max_packet_size;
854
    if(h->prot) {
855
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
856
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
857
    }
858
    return 0;
859
}
860

    
861
int ffio_set_buf_size(AVIOContext *s, int buf_size)
862
{
863
    uint8_t *buffer;
864
    buffer = av_malloc(buf_size);
865
    if (!buffer)
866
        return AVERROR(ENOMEM);
867

    
868
    av_free(s->buffer);
869
    s->buffer = buffer;
870
    s->buffer_size = buf_size;
871
    s->buf_ptr = buffer;
872
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
873
    return 0;
874
}
875

    
876
#if FF_API_URL_RESETBUF
877
int url_resetbuf(AVIOContext *s, int flags)
878
#else
879
static int url_resetbuf(AVIOContext *s, int flags)
880
#endif
881
{
882
#if FF_API_URL_RESETBUF
883
    if (flags & URL_RDWR)
884
        return AVERROR(EINVAL);
885
#else
886
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
887
#endif
888

    
889
    if (flags & URL_WRONLY) {
890
        s->buf_end = s->buffer + s->buffer_size;
891
        s->write_flag = 1;
892
    } else {
893
        s->buf_end = s->buffer;
894
        s->write_flag = 0;
895
    }
896
    return 0;
897
}
898

    
899
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
900
{
901
    int64_t buffer_start;
902
    int buffer_size;
903
    int overlap, new_size, alloc_size;
904

    
905
    if (s->write_flag)
906
        return AVERROR(EINVAL);
907

    
908
    buffer_size = s->buf_end - s->buffer;
909

    
910
    /* the buffers must touch or overlap */
911
    if ((buffer_start = s->pos - buffer_size) > buf_size)
912
        return AVERROR(EINVAL);
913

    
914
    overlap = buf_size - buffer_start;
915
    new_size = buf_size + buffer_size - overlap;
916

    
917
    alloc_size = FFMAX(s->buffer_size, new_size);
918
    if (alloc_size > buf_size)
919
        if (!(buf = av_realloc(buf, alloc_size)))
920
            return AVERROR(ENOMEM);
921

    
922
    if (new_size > buf_size) {
923
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
924
        buf_size = new_size;
925
    }
926

    
927
    av_free(s->buffer);
928
    s->buf_ptr = s->buffer = buf;
929
    s->buffer_size = alloc_size;
930
    s->pos = buf_size;
931
    s->buf_end = s->buf_ptr + buf_size;
932
    s->eof_reached = 0;
933
    s->must_flush = 0;
934

    
935
    return 0;
936
}
937

    
938
int avio_open(AVIOContext **s, const char *filename, int flags)
939
{
940
    URLContext *h;
941
    int err;
942

    
943
    err = url_open(&h, filename, flags);
944
    if (err < 0)
945
        return err;
946
    err = url_fdopen(s, h);
947
    if (err < 0) {
948
        url_close(h);
949
        return err;
950
    }
951
    return 0;
952
}
953

    
954
int avio_close(AVIOContext *s)
955
{
956
    URLContext *h = s->opaque;
957

    
958
    av_free(s->buffer);
959
    av_free(s);
960
    return url_close(h);
961
}
962

    
963
#if FF_API_OLD_AVIO
964
URLContext *url_fileno(AVIOContext *s)
965
{
966
    return s->opaque;
967
}
968
#endif
969

    
970
int avio_printf(AVIOContext *s, const char *fmt, ...)
971
{
972
    va_list ap;
973
    char buf[4096];
974
    int ret;
975

    
976
    va_start(ap, fmt);
977
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
978
    va_end(ap);
979
    avio_write(s, buf, strlen(buf));
980
    return ret;
981
}
982

    
983
#if FF_API_OLD_AVIO
984
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
985
{
986
    int c;
987
    char *q;
988

    
989
    c = avio_r8(s);
990
    if (s->eof_reached)
991
        return NULL;
992
    q = buf;
993
    for(;;) {
994
        if (s->eof_reached || c == '\n')
995
            break;
996
        if ((q - buf) < buf_size - 1)
997
            *q++ = c;
998
        c = avio_r8(s);
999
    }
1000
    if (buf_size > 0)
1001
        *q = '\0';
1002
    return buf;
1003
}
1004

    
1005
int url_fget_max_packet_size(AVIOContext *s)
1006
{
1007
    return s->max_packet_size;
1008
}
1009
#endif
1010

    
1011
int ffio_read_pause(AVIOContext *s, int pause)
1012
{
1013
    if (!s->read_pause)
1014
        return AVERROR(ENOSYS);
1015
    return s->read_pause(s->opaque, pause);
1016
}
1017

    
1018
int64_t ffio_read_seek(AVIOContext *s, int stream_index,
1019
                       int64_t timestamp, int flags)
1020
{
1021
    URLContext *h = s->opaque;
1022
    int64_t ret;
1023
    if (!s->read_seek)
1024
        return AVERROR(ENOSYS);
1025
    ret = s->read_seek(h, stream_index, timestamp, flags);
1026
    if(ret >= 0) {
1027
        int64_t pos;
1028
        s->buf_ptr = s->buf_end; // Flush buffer
1029
        pos = s->seek(h, 0, SEEK_CUR);
1030
        if (pos >= 0)
1031
            s->pos = pos;
1032
        else if (pos != AVERROR(ENOSYS))
1033
            ret = pos;
1034
    }
1035
    return ret;
1036
}
1037

    
1038
/* avio_open_dyn_buf and avio_close_dyn_buf are used in rtp.c to send a response
1039
 * back to the server even if CONFIG_MUXERS is false. */
1040
#if CONFIG_MUXERS || CONFIG_NETWORK
1041
/* buffer handling */
1042
#if FF_API_OLD_AVIO
1043
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
1044
{
1045
    int ret;
1046
    *s = av_mallocz(sizeof(AVIOContext));
1047
    if(!*s)
1048
        return AVERROR(ENOMEM);
1049
    ret = ffio_init_context(*s, buf, buf_size,
1050
                        (flags & URL_WRONLY || flags & URL_RDWR),
1051
                        NULL, NULL, NULL, NULL);
1052
    if(ret != 0)
1053
        av_freep(s);
1054
    return ret;
1055
}
1056

    
1057
int url_close_buf(AVIOContext *s)
1058
{
1059
    avio_flush(s);
1060
    return s->buf_ptr - s->buffer;
1061
}
1062
#endif
1063

    
1064
/* output in a dynamic buffer */
1065

    
1066
typedef struct DynBuffer {
1067
    int pos, size, allocated_size;
1068
    uint8_t *buffer;
1069
    int io_buffer_size;
1070
    uint8_t io_buffer[1];
1071
} DynBuffer;
1072

    
1073
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1074
{
1075
    DynBuffer *d = opaque;
1076
    unsigned new_size, new_allocated_size;
1077

    
1078
    /* reallocate buffer if needed */
1079
    new_size = d->pos + buf_size;
1080
    new_allocated_size = d->allocated_size;
1081
    if(new_size < d->pos || new_size > INT_MAX/2)
1082
        return -1;
1083
    while (new_size > new_allocated_size) {
1084
        if (!new_allocated_size)
1085
            new_allocated_size = new_size;
1086
        else
1087
            new_allocated_size += new_allocated_size / 2 + 1;
1088
    }
1089

    
1090
    if (new_allocated_size > d->allocated_size) {
1091
        d->buffer = av_realloc(d->buffer, new_allocated_size);
1092
        if(d->buffer == NULL)
1093
             return AVERROR(ENOMEM);
1094
        d->allocated_size = new_allocated_size;
1095
    }
1096
    memcpy(d->buffer + d->pos, buf, buf_size);
1097
    d->pos = new_size;
1098
    if (d->pos > d->size)
1099
        d->size = d->pos;
1100
    return buf_size;
1101
}
1102

    
1103
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1104
{
1105
    unsigned char buf1[4];
1106
    int ret;
1107

    
1108
    /* packetized write: output the header */
1109
    AV_WB32(buf1, buf_size);
1110
    ret= dyn_buf_write(opaque, buf1, 4);
1111
    if(ret < 0)
1112
        return ret;
1113

    
1114
    /* then the data */
1115
    return dyn_buf_write(opaque, buf, buf_size);
1116
}
1117

    
1118
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1119
{
1120
    DynBuffer *d = opaque;
1121

    
1122
    if (whence == SEEK_CUR)
1123
        offset += d->pos;
1124
    else if (whence == SEEK_END)
1125
        offset += d->size;
1126
    if (offset < 0 || offset > 0x7fffffffLL)
1127
        return -1;
1128
    d->pos = offset;
1129
    return 0;
1130
}
1131

    
1132
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1133
{
1134
    DynBuffer *d;
1135
    int ret;
1136
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1137

    
1138
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1139
        return -1;
1140
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1141
    if (!d)
1142
        return AVERROR(ENOMEM);
1143
    *s = av_mallocz(sizeof(AVIOContext));
1144
    if(!*s) {
1145
        av_free(d);
1146
        return AVERROR(ENOMEM);
1147
    }
1148
    d->io_buffer_size = io_buffer_size;
1149
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
1150
                        1, d, NULL,
1151
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1152
                        max_packet_size ? NULL : dyn_buf_seek);
1153
    if (ret == 0) {
1154
        (*s)->max_packet_size = max_packet_size;
1155
    } else {
1156
        av_free(d);
1157
        av_freep(s);
1158
    }
1159
    return ret;
1160
}
1161

    
1162
int avio_open_dyn_buf(AVIOContext **s)
1163
{
1164
    return url_open_dyn_buf_internal(s, 0);
1165
}
1166

    
1167
int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1168
{
1169
    if (max_packet_size <= 0)
1170
        return -1;
1171
    return url_open_dyn_buf_internal(s, max_packet_size);
1172
}
1173

    
1174
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1175
{
1176
    DynBuffer *d = s->opaque;
1177
    int size;
1178
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1179
    int padding = 0;
1180

    
1181
    /* don't attempt to pad fixed-size packet buffers */
1182
    if (!s->max_packet_size) {
1183
        avio_write(s, padbuf, sizeof(padbuf));
1184
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1185
    }
1186

    
1187
    avio_flush(s);
1188

    
1189
    *pbuffer = d->buffer;
1190
    size = d->size;
1191
    av_free(d);
1192
    av_free(s);
1193
    return size - padding;
1194
}
1195
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */