Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 468644f6

History | View | Annotate | Download (27.8 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 "url.h"
29
#include <stdarg.h>
30

    
31
#define IO_BUFFER_SIZE 32768
32

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

    
40
static void fill_buffer(AVIOContext *s);
41
static int url_resetbuf(AVIOContext *s, int flags);
42

    
43
int ffio_init_context(AVIOContext *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 ? AVIO_WRONLY : AVIO_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
#if FF_API_OLD_AVIO
65
    s->is_streamed = 0;
66
#endif
67
    s->seekable = AVIO_SEEKABLE_NORMAL;
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
#if FF_API_OLD_AVIO
80
int init_put_byte(AVIOContext *s,
81
                  unsigned char *buffer,
82
                  int buffer_size,
83
                  int write_flag,
84
                  void *opaque,
85
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
86
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
87
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
88
{
89
    return ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
90
                                read_packet, write_packet, seek);
91
}
92
AVIOContext *av_alloc_put_byte(
93
                  unsigned char *buffer,
94
                  int buffer_size,
95
                  int write_flag,
96
                  void *opaque,
97
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
98
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
99
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
100
{
101
    return avio_alloc_context(buffer, buffer_size, write_flag, opaque,
102
                              read_packet, write_packet, seek);
103
}
104
#endif
105

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

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

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

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

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

    
156
        count -= len;
157
    }
158
}
159

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
323
#undef GET
324

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

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

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

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

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

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

    
450
int avio_put_str16le(AVIOContext *s, const char *str)
451
{
452
    const uint8_t *q = str;
453
    int ret = 0;
454

    
455
    while (*q) {
456
        uint32_t ch;
457
        uint16_t tmp;
458

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

    
467
int ff_get_v_length(uint64_t val){
468
    int i=1;
469

    
470
    while(val>>=7)
471
        i++;
472

    
473
    return i;
474
}
475

    
476
void ff_put_v(AVIOContext *bc, uint64_t val){
477
    int i= ff_get_v_length(val);
478

    
479
    while(--i>0)
480
        avio_w8(bc, 128 | (val>>(7*i)));
481

    
482
    avio_w8(bc, val&127);
483
}
484

    
485
void avio_wl64(AVIOContext *s, uint64_t val)
486
{
487
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
488
    avio_wl32(s, (uint32_t)(val >> 32));
489
}
490

    
491
void avio_wb64(AVIOContext *s, uint64_t val)
492
{
493
    avio_wb32(s, (uint32_t)(val >> 32));
494
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
495
}
496

    
497
void avio_wl16(AVIOContext *s, unsigned int val)
498
{
499
    avio_w8(s, val);
500
    avio_w8(s, val >> 8);
501
}
502

    
503
void avio_wb16(AVIOContext *s, unsigned int val)
504
{
505
    avio_w8(s, val >> 8);
506
    avio_w8(s, val);
507
}
508

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

    
515
void avio_wb24(AVIOContext *s, unsigned int val)
516
{
517
    avio_wb16(s, val >> 8);
518
    avio_w8(s, val);
519
}
520

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

    
530
/* Input stream */
531

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

    
538
    /* no need to do anything if EOF already reached */
539
    if (s->eof_reached)
540
        return;
541

    
542
    if(s->update_checksum && dst == s->buffer){
543
        if(s->buf_end > s->checksum_ptr)
544
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
545
        s->checksum_ptr= s->buffer;
546
    }
547

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

    
552
        s->checksum_ptr = dst = s->buffer;
553
        len = s->buffer_size;
554
    }
555

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

    
573
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
574
                                    unsigned int len)
575
{
576
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
577
}
578

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

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

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

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

    
618
int avio_read(AVIOContext *s, unsigned char *buf, int size)
619
{
620
    int len, size1;
621

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

    
665
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
666
{
667
    int len;
668

    
669
    if(size<0)
670
        return -1;
671

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

    
688
unsigned int avio_rl16(AVIOContext *s)
689
{
690
    unsigned int val;
691
    val = avio_r8(s);
692
    val |= avio_r8(s) << 8;
693
    return val;
694
}
695

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

    
704
unsigned int avio_rl32(AVIOContext *s)
705
{
706
    unsigned int val;
707
    val = avio_rl16(s);
708
    val |= avio_rl16(s) << 16;
709
    return val;
710
}
711

    
712
uint64_t avio_rl64(AVIOContext *s)
713
{
714
    uint64_t val;
715
    val = (uint64_t)avio_rl32(s);
716
    val |= (uint64_t)avio_rl32(s) << 32;
717
    return val;
718
}
719

    
720
unsigned int avio_rb16(AVIOContext *s)
721
{
722
    unsigned int val;
723
    val = avio_r8(s) << 8;
724
    val |= avio_r8(s);
725
    return val;
726
}
727

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

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

    
751
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
752
{
753
    int i = 0;
754
    char c;
755

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

    
762
    buf[i] = 0;
763
    return i;
764
}
765

    
766
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
767
{
768
    int i;
769

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

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

    
800
GET_STR16(le, avio_rl16)
801
GET_STR16(be, avio_rb16)
802

    
803
#undef GET_STR16
804

    
805
uint64_t avio_rb64(AVIOContext *s)
806
{
807
    uint64_t val;
808
    val = (uint64_t)avio_rb32(s) << 32;
809
    val |= (uint64_t)avio_rb32(s);
810
    return val;
811
}
812

    
813
uint64_t ffio_read_varlen(AVIOContext *bc){
814
    uint64_t val = 0;
815
    int tmp;
816

    
817
    do{
818
        tmp = avio_r8(bc);
819
        val= (val<<7) + (tmp&127);
820
    }while(tmp&128);
821
    return val;
822
}
823

    
824
int ffio_fdopen(AVIOContext **s, URLContext *h)
825
{
826
    uint8_t *buffer;
827
    int buffer_size, max_packet_size;
828

    
829
    max_packet_size = h->max_packet_size;
830
    if (max_packet_size) {
831
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
832
    } else {
833
        buffer_size = IO_BUFFER_SIZE;
834
    }
835
    buffer = av_malloc(buffer_size);
836
    if (!buffer)
837
        return AVERROR(ENOMEM);
838

    
839
    *s = av_mallocz(sizeof(AVIOContext));
840
    if(!*s) {
841
        av_free(buffer);
842
        return AVERROR(ENOMEM);
843
    }
844

    
845
    if (ffio_init_context(*s, buffer, buffer_size,
846
                      (h->flags & AVIO_WRONLY || h->flags & AVIO_RDWR), h,
847
                      ffurl_read, ffurl_write, ffurl_seek) < 0) {
848
        av_free(buffer);
849
        av_freep(s);
850
        return AVERROR(EIO);
851
    }
852
#if FF_API_OLD_AVIO
853
    (*s)->is_streamed = h->is_streamed;
854
#endif
855
    (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
856
    (*s)->max_packet_size = max_packet_size;
857
    if(h->prot) {
858
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
859
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
860
    }
861
    return 0;
862
}
863

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

    
871
    av_free(s->buffer);
872
    s->buffer = buffer;
873
    s->buffer_size = buf_size;
874
    s->buf_ptr = buffer;
875
    url_resetbuf(s, s->write_flag ? AVIO_WRONLY : AVIO_RDONLY);
876
    return 0;
877
}
878

    
879
static int url_resetbuf(AVIOContext *s, int flags)
880
{
881
    assert(flags == AVIO_WRONLY || flags == AVIO_RDONLY);
882

    
883
    if (flags & AVIO_WRONLY) {
884
        s->buf_end = s->buffer + s->buffer_size;
885
        s->write_flag = 1;
886
    } else {
887
        s->buf_end = s->buffer;
888
        s->write_flag = 0;
889
    }
890
    return 0;
891
}
892

    
893
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
894
{
895
    int64_t buffer_start;
896
    int buffer_size;
897
    int overlap, new_size, alloc_size;
898

    
899
    if (s->write_flag)
900
        return AVERROR(EINVAL);
901

    
902
    buffer_size = s->buf_end - s->buffer;
903

    
904
    /* the buffers must touch or overlap */
905
    if ((buffer_start = s->pos - buffer_size) > buf_size)
906
        return AVERROR(EINVAL);
907

    
908
    overlap = buf_size - buffer_start;
909
    new_size = buf_size + buffer_size - overlap;
910

    
911
    alloc_size = FFMAX(s->buffer_size, new_size);
912
    if (alloc_size > buf_size)
913
        if (!(buf = av_realloc(buf, alloc_size)))
914
            return AVERROR(ENOMEM);
915

    
916
    if (new_size > buf_size) {
917
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
918
        buf_size = new_size;
919
    }
920

    
921
    av_free(s->buffer);
922
    s->buf_ptr = s->buffer = buf;
923
    s->buffer_size = alloc_size;
924
    s->pos = buf_size;
925
    s->buf_end = s->buf_ptr + buf_size;
926
    s->eof_reached = 0;
927
    s->must_flush = 0;
928

    
929
    return 0;
930
}
931

    
932
int avio_open(AVIOContext **s, const char *filename, int flags)
933
{
934
    URLContext *h;
935
    int err;
936

    
937
    err = ffurl_open(&h, filename, flags);
938
    if (err < 0)
939
        return err;
940
    err = ffio_fdopen(s, h);
941
    if (err < 0) {
942
        ffurl_close(h);
943
        return err;
944
    }
945
    return 0;
946
}
947

    
948
int avio_close(AVIOContext *s)
949
{
950
    URLContext *h = s->opaque;
951

    
952
    av_free(s->buffer);
953
    av_free(s);
954
    return ffurl_close(h);
955
}
956

    
957
#if FF_API_OLD_AVIO
958
URLContext *url_fileno(AVIOContext *s)
959
{
960
    return s->opaque;
961
}
962
#endif
963

    
964
int avio_printf(AVIOContext *s, const char *fmt, ...)
965
{
966
    va_list ap;
967
    char buf[4096];
968
    int ret;
969

    
970
    va_start(ap, fmt);
971
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
972
    va_end(ap);
973
    avio_write(s, buf, strlen(buf));
974
    return ret;
975
}
976

    
977
#if FF_API_OLD_AVIO
978
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
979
{
980
    int c;
981
    char *q;
982

    
983
    c = avio_r8(s);
984
    if (s->eof_reached)
985
        return NULL;
986
    q = buf;
987
    for(;;) {
988
        if (s->eof_reached || c == '\n')
989
            break;
990
        if ((q - buf) < buf_size - 1)
991
            *q++ = c;
992
        c = avio_r8(s);
993
    }
994
    if (buf_size > 0)
995
        *q = '\0';
996
    return buf;
997
}
998

    
999
int url_fget_max_packet_size(AVIOContext *s)
1000
{
1001
    return s->max_packet_size;
1002
}
1003
#endif
1004

    
1005
int avio_pause(AVIOContext *s, int pause)
1006
{
1007
    if (!s->read_pause)
1008
        return AVERROR(ENOSYS);
1009
    return s->read_pause(s->opaque, pause);
1010
}
1011

    
1012
int64_t avio_seek_time(AVIOContext *s, int stream_index,
1013
                       int64_t timestamp, int flags)
1014
{
1015
    URLContext *h = s->opaque;
1016
    int64_t ret;
1017
    if (!s->read_seek)
1018
        return AVERROR(ENOSYS);
1019
    ret = s->read_seek(h, stream_index, timestamp, flags);
1020
    if(ret >= 0) {
1021
        int64_t pos;
1022
        s->buf_ptr = s->buf_end; // Flush buffer
1023
        pos = s->seek(h, 0, SEEK_CUR);
1024
        if (pos >= 0)
1025
            s->pos = pos;
1026
        else if (pos != AVERROR(ENOSYS))
1027
            ret = pos;
1028
    }
1029
    return ret;
1030
}
1031

    
1032
/* buffer handling */
1033
#if FF_API_OLD_AVIO
1034
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
1035
{
1036
    int ret;
1037
    *s = av_mallocz(sizeof(AVIOContext));
1038
    if(!*s)
1039
        return AVERROR(ENOMEM);
1040
    ret = ffio_init_context(*s, buf, buf_size,
1041
                        (flags & AVIO_WRONLY || flags & AVIO_RDWR),
1042
                        NULL, NULL, NULL, NULL);
1043
    if(ret != 0)
1044
        av_freep(s);
1045
    return ret;
1046
}
1047

    
1048
int url_close_buf(AVIOContext *s)
1049
{
1050
    avio_flush(s);
1051
    return s->buf_ptr - s->buffer;
1052
}
1053
#endif
1054

    
1055
/* output in a dynamic buffer */
1056

    
1057
typedef struct DynBuffer {
1058
    int pos, size, allocated_size;
1059
    uint8_t *buffer;
1060
    int io_buffer_size;
1061
    uint8_t io_buffer[1];
1062
} DynBuffer;
1063

    
1064
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1065
{
1066
    DynBuffer *d = opaque;
1067
    unsigned new_size, new_allocated_size;
1068

    
1069
    /* reallocate buffer if needed */
1070
    new_size = d->pos + buf_size;
1071
    new_allocated_size = d->allocated_size;
1072
    if(new_size < d->pos || new_size > INT_MAX/2)
1073
        return -1;
1074
    while (new_size > new_allocated_size) {
1075
        if (!new_allocated_size)
1076
            new_allocated_size = new_size;
1077
        else
1078
            new_allocated_size += new_allocated_size / 2 + 1;
1079
    }
1080

    
1081
    if (new_allocated_size > d->allocated_size) {
1082
        d->buffer = av_realloc(d->buffer, new_allocated_size);
1083
        if(d->buffer == NULL)
1084
             return AVERROR(ENOMEM);
1085
        d->allocated_size = new_allocated_size;
1086
    }
1087
    memcpy(d->buffer + d->pos, buf, buf_size);
1088
    d->pos = new_size;
1089
    if (d->pos > d->size)
1090
        d->size = d->pos;
1091
    return buf_size;
1092
}
1093

    
1094
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1095
{
1096
    unsigned char buf1[4];
1097
    int ret;
1098

    
1099
    /* packetized write: output the header */
1100
    AV_WB32(buf1, buf_size);
1101
    ret= dyn_buf_write(opaque, buf1, 4);
1102
    if(ret < 0)
1103
        return ret;
1104

    
1105
    /* then the data */
1106
    return dyn_buf_write(opaque, buf, buf_size);
1107
}
1108

    
1109
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1110
{
1111
    DynBuffer *d = opaque;
1112

    
1113
    if (whence == SEEK_CUR)
1114
        offset += d->pos;
1115
    else if (whence == SEEK_END)
1116
        offset += d->size;
1117
    if (offset < 0 || offset > 0x7fffffffLL)
1118
        return -1;
1119
    d->pos = offset;
1120
    return 0;
1121
}
1122

    
1123
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1124
{
1125
    DynBuffer *d;
1126
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1127

    
1128
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1129
        return -1;
1130
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1131
    if (!d)
1132
        return AVERROR(ENOMEM);
1133
    d->io_buffer_size = io_buffer_size;
1134
    *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1135
                            max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1136
                            max_packet_size ? NULL : dyn_buf_seek);
1137
    if(!*s) {
1138
        av_free(d);
1139
        return AVERROR(ENOMEM);
1140
    }
1141
    (*s)->max_packet_size = max_packet_size;
1142
    return 0;
1143
}
1144

    
1145
int avio_open_dyn_buf(AVIOContext **s)
1146
{
1147
    return url_open_dyn_buf_internal(s, 0);
1148
}
1149

    
1150
int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1151
{
1152
    if (max_packet_size <= 0)
1153
        return -1;
1154
    return url_open_dyn_buf_internal(s, max_packet_size);
1155
}
1156

    
1157
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1158
{
1159
    DynBuffer *d = s->opaque;
1160
    int size;
1161
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1162
    int padding = 0;
1163

    
1164
    /* don't attempt to pad fixed-size packet buffers */
1165
    if (!s->max_packet_size) {
1166
        avio_write(s, padbuf, sizeof(padbuf));
1167
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1168
    }
1169

    
1170
    avio_flush(s);
1171

    
1172
    *pbuffer = d->buffer;
1173
    size = d->size;
1174
    av_free(d);
1175
    av_free(s);
1176
    return size - padding;
1177
}