Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 668438a3

History | View | Annotate | Download (28 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * Buffered I/O for ffmpeg system
3 19720f15 Fabrice Bellard
 * Copyright (c) 2000,2001 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 2912e87a Mans Rullgard
 * This file is part of Libav.
6 b78e7197 Diego Biurrun
 *
7 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 245976da Diego Biurrun
22
#include "libavutil/crc.h"
23 aa805f94 Reimar Döffinger
#include "libavutil/intreadwrite.h"
24 de6d9b64 Fabrice Bellard
#include "avformat.h"
25 5c91a675 Zdenek Kabelac
#include "avio.h"
26 e731b8d8 Anton Khirnov
#include "avio_internal.h"
27 603e5c0b Michael Chinen
#include "internal.h"
28 0589da0a Anton Khirnov
#include "url.h"
29 abba3dd1 Fabrice Bellard
#include <stdarg.h>
30 de6d9b64 Fabrice Bellard
31
#define IO_BUFFER_SIZE 32768
32
33 7939d0ed Martin Storsjö
/**
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 ae628ec1 Anton Khirnov
static void fill_buffer(AVIOContext *s);
41 364cacc7 Aurelien Jacobs
#if !FF_API_URL_RESETBUF
42 ae628ec1 Anton Khirnov
static int url_resetbuf(AVIOContext *s, int flags);
43 08580cb0 Benoit Fouet
#endif
44 398f5d3f Michael Niedermayer
45 e731b8d8 Anton Khirnov
int ffio_init_context(AVIOContext *s,
46 de6d9b64 Fabrice Bellard
                  unsigned char *buffer,
47
                  int buffer_size,
48
                  int write_flag,
49
                  void *opaque,
50 0c1a9eda Zdenek Kabelac
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
51 576ae256 Michael Niedermayer
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
52 bc5c918e Diego Biurrun
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
53 de6d9b64 Fabrice Bellard
{
54
    s->buffer = buffer;
55
    s->buffer_size = buffer_size;
56
    s->buf_ptr = buffer;
57
    s->opaque = opaque;
58 9daa4cea Aurelien Jacobs
    url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);
59 de6d9b64 Fabrice Bellard
    s->write_packet = write_packet;
60
    s->read_packet = read_packet;
61
    s->seek = seek;
62
    s->pos = 0;
63
    s->must_flush = 0;
64
    s->eof_reached = 0;
65 576ae256 Michael Niedermayer
    s->error = 0;
66 8978feda Anton Khirnov
#if FF_API_OLD_AVIO
67 de6d9b64 Fabrice Bellard
    s->is_streamed = 0;
68 8978feda Anton Khirnov
#endif
69
    s->seekable = AVIO_SEEKABLE_NORMAL;
70 abba3dd1 Fabrice Bellard
    s->max_packet_size = 0;
71 ee9f36a8 Michael Niedermayer
    s->update_checksum= NULL;
72 942f3bb5 Michael Niedermayer
    if(!read_packet && !write_flag){
73
        s->pos = buffer_size;
74
        s->buf_end = s->buffer + buffer_size;
75
    }
76 e7e4810a Björn Axelsson
    s->read_pause = NULL;
77
    s->read_seek  = NULL;
78 de6d9b64 Fabrice Bellard
    return 0;
79
}
80 115329f1 Diego Biurrun
81 e731b8d8 Anton Khirnov
#if FF_API_OLD_AVIO
82
int init_put_byte(AVIOContext *s,
83
                  unsigned char *buffer,
84
                  int buffer_size,
85
                  int write_flag,
86
                  void *opaque,
87
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
88
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
89
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
90
{
91
    return ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
92
                                read_packet, write_packet, seek);
93
}
94 8d9ac969 Anton Khirnov
AVIOContext *av_alloc_put_byte(
95
                  unsigned char *buffer,
96
                  int buffer_size,
97
                  int write_flag,
98
                  void *opaque,
99
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
100
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
101
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
102
{
103
    return avio_alloc_context(buffer, buffer_size, write_flag, opaque,
104
                              read_packet, write_packet, seek);
105
}
106 e731b8d8 Anton Khirnov
#endif
107
108 8d9ac969 Anton Khirnov
AVIOContext *avio_alloc_context(
109 1e0f3468 Reimar Döffinger
                  unsigned char *buffer,
110
                  int buffer_size,
111
                  int write_flag,
112
                  void *opaque,
113
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
114
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
115 7f37f568 Diego Biurrun
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
116
{
117 ae628ec1 Anton Khirnov
    AVIOContext *s = av_mallocz(sizeof(AVIOContext));
118 e731b8d8 Anton Khirnov
    ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
119 1e0f3468 Reimar Döffinger
                  read_packet, write_packet, seek);
120
    return s;
121
}
122
123 ae628ec1 Anton Khirnov
static void flush_buffer(AVIOContext *s)
124 de6d9b64 Fabrice Bellard
{
125
    if (s->buf_ptr > s->buffer) {
126 576ae256 Michael Niedermayer
        if (s->write_packet && !s->error){
127 c66f53cf Martin Storsjö
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
128 576ae256 Michael Niedermayer
            if(ret < 0){
129
                s->error = ret;
130
            }
131
        }
132 465e1dad Michael Niedermayer
        if(s->update_checksum){
133 ee9f36a8 Michael Niedermayer
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
134
            s->checksum_ptr= s->buffer;
135
        }
136 de6d9b64 Fabrice Bellard
        s->pos += s->buf_ptr - s->buffer;
137
    }
138
    s->buf_ptr = s->buffer;
139
}
140
141 77eb5504 Anton Khirnov
void avio_w8(AVIOContext *s, int b)
142 de6d9b64 Fabrice Bellard
{
143
    *(s->buf_ptr)++ = b;
144 115329f1 Diego Biurrun
    if (s->buf_ptr >= s->buf_end)
145 de6d9b64 Fabrice Bellard
        flush_buffer(s);
146
}
147
148 0ac8e2bf Anton Khirnov
void ffio_fill(AVIOContext *s, int b, int count)
149 17ee8f66 Anssi Hannula
{
150
    while (count > 0) {
151
        int len = FFMIN(s->buf_end - s->buf_ptr, count);
152
        memset(s->buf_ptr, b, len);
153
        s->buf_ptr += len;
154
155
        if (s->buf_ptr >= s->buf_end)
156
            flush_buffer(s);
157
158
        count -= len;
159
    }
160
}
161
162 77eb5504 Anton Khirnov
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
163 de6d9b64 Fabrice Bellard
{
164
    while (size > 0) {
165 90aa6ace Reimar Döffinger
        int len = FFMIN(s->buf_end - s->buf_ptr, size);
166 de6d9b64 Fabrice Bellard
        memcpy(s->buf_ptr, buf, len);
167
        s->buf_ptr += len;
168
169 115329f1 Diego Biurrun
        if (s->buf_ptr >= s->buf_end)
170 de6d9b64 Fabrice Bellard
            flush_buffer(s);
171
172
        buf += len;
173
        size -= len;
174
    }
175
}
176
177 b7f2fdde Anton Khirnov
void avio_flush(AVIOContext *s)
178 de6d9b64 Fabrice Bellard
{
179
    flush_buffer(s);
180
    s->must_flush = 0;
181
}
182
183 6b4aa5da Anton Khirnov
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
184 de6d9b64 Fabrice Bellard
{
185 bc5c918e Diego Biurrun
    int64_t offset1;
186
    int64_t pos;
187 c6a5e087 Tomas Härdin
    int force = whence & AVSEEK_FORCE;
188
    whence &= ~AVSEEK_FORCE;
189 899681cd Björn Axelsson
190
    if(!s)
191
        return AVERROR(EINVAL);
192
193
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
194 de6d9b64 Fabrice Bellard
195
    if (whence != SEEK_CUR && whence != SEEK_SET)
196 8fa36ae0 François Revol
        return AVERROR(EINVAL);
197 115329f1 Diego Biurrun
198 f40d1739 Michael Niedermayer
    if (whence == SEEK_CUR) {
199
        offset1 = pos + (s->buf_ptr - s->buffer);
200
        if (offset == 0)
201
            return offset1;
202
        offset += offset1;
203
    }
204
    offset1 = offset - pos;
205
    if (!s->must_flush &&
206 b3eab9fb Baptiste Coudurier
        offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
207 f40d1739 Michael Niedermayer
        /* can do the seek inside the buffer */
208
        s->buf_ptr = s->buffer + offset1;
209 8978feda Anton Khirnov
    } else if ((!s->seekable ||
210 7939d0ed Martin Storsjö
               offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
211
               !s->write_flag && offset1 >= 0 &&
212 7a6fe01f Tomas Härdin
              (whence != SEEK_END || force)) {
213 398f5d3f Michael Niedermayer
        while(s->pos < offset && !s->eof_reached)
214
            fill_buffer(s);
215 5cb4b82a Baptiste Coudurier
        if (s->eof_reached)
216 8d5e638f Stefano Sabatini
            return AVERROR_EOF;
217 398f5d3f Michael Niedermayer
        s->buf_ptr = s->buf_end + offset - s->pos;
218 f40d1739 Michael Niedermayer
    } else {
219 9d30e068 Stefano Sabatini
        int64_t res;
220 68fcdbf1 Ronald S. Bultje
221 b250f9c6 Aurelien Jacobs
#if CONFIG_MUXERS || CONFIG_NETWORK
222 f40d1739 Michael Niedermayer
        if (s->write_flag) {
223 de6d9b64 Fabrice Bellard
            flush_buffer(s);
224
            s->must_flush = 1;
225
        }
226 b250f9c6 Aurelien Jacobs
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
227 9d30e068 Stefano Sabatini
        if (!s->seek)
228
            return AVERROR(EPIPE);
229
        if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
230 68fcdbf1 Ronald S. Bultje
            return res;
231 39a127bf Baptiste Coudurier
        if (!s->write_flag)
232
            s->buf_end = s->buffer;
233
        s->buf_ptr = s->buffer;
234 f40d1739 Michael Niedermayer
        s->pos = offset;
235 de6d9b64 Fabrice Bellard
    }
236 f40d1739 Michael Niedermayer
    s->eof_reached = 0;
237 de6d9b64 Fabrice Bellard
    return offset;
238
}
239
240 0300db8a Anton Khirnov
#if FF_API_OLD_AVIO
241 ae628ec1 Anton Khirnov
int url_fskip(AVIOContext *s, int64_t offset)
242 de6d9b64 Fabrice Bellard
{
243 6b4aa5da Anton Khirnov
    int64_t ret = avio_seek(s, offset, SEEK_CUR);
244 d5e9ddeb Martin Storsjö
    return ret < 0 ? ret : 0;
245 de6d9b64 Fabrice Bellard
}
246
247 ae628ec1 Anton Khirnov
int64_t url_ftell(AVIOContext *s)
248 de6d9b64 Fabrice Bellard
{
249 6b4aa5da Anton Khirnov
    return avio_seek(s, 0, SEEK_CUR);
250 de6d9b64 Fabrice Bellard
}
251 a2704c97 Anton Khirnov
#endif
252 de6d9b64 Fabrice Bellard
253 76aa876e Anton Khirnov
int64_t avio_size(AVIOContext *s)
254 a965c478 Aurelien Jacobs
{
255 bc5c918e Diego Biurrun
    int64_t size;
256 115329f1 Diego Biurrun
257 899681cd Björn Axelsson
    if(!s)
258
        return AVERROR(EINVAL);
259
260 a965c478 Aurelien Jacobs
    if (!s->seek)
261 4c4ef3db Stefano Sabatini
        return AVERROR(ENOSYS);
262 8e287af0 Michael Niedermayer
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
263
    if(size<0){
264 8f42f523 Ronald S. Bultje
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
265
            return size;
266
        size++;
267 1ae2c5f2 Michael Niedermayer
        s->seek(s->opaque, s->pos, SEEK_SET);
268 8e287af0 Michael Niedermayer
    }
269 a965c478 Aurelien Jacobs
    return size;
270
}
271
272 66e5b1df Anton Khirnov
#if FF_API_OLD_AVIO
273 ae628ec1 Anton Khirnov
int url_feof(AVIOContext *s)
274 de6d9b64 Fabrice Bellard
{
275 899681cd Björn Axelsson
    if(!s)
276
        return 0;
277 de6d9b64 Fabrice Bellard
    return s->eof_reached;
278
}
279
280 ae628ec1 Anton Khirnov
int url_ferror(AVIOContext *s)
281 576ae256 Michael Niedermayer
{
282 899681cd Björn Axelsson
    if(!s)
283
        return 0;
284 576ae256 Michael Niedermayer
    return s->error;
285
}
286 3e68b3ba Anton Khirnov
#endif
287 576ae256 Michael Niedermayer
288 77eb5504 Anton Khirnov
void avio_wl32(AVIOContext *s, unsigned int val)
289 de6d9b64 Fabrice Bellard
{
290 77eb5504 Anton Khirnov
    avio_w8(s, val);
291
    avio_w8(s, val >> 8);
292
    avio_w8(s, val >> 16);
293
    avio_w8(s, val >> 24);
294 de6d9b64 Fabrice Bellard
}
295
296 77eb5504 Anton Khirnov
void avio_wb32(AVIOContext *s, unsigned int val)
297 de6d9b64 Fabrice Bellard
{
298 77eb5504 Anton Khirnov
    avio_w8(s, val >> 24);
299
    avio_w8(s, val >> 16);
300
    avio_w8(s, val >> 8);
301
    avio_w8(s, val);
302 de6d9b64 Fabrice Bellard
}
303
304 4efd5cf3 Anton Khirnov
#if FF_API_OLD_AVIO
305 ae628ec1 Anton Khirnov
void put_strz(AVIOContext *s, const char *str)
306 75bdb984 Philip Gladstone
{
307 4efd5cf3 Anton Khirnov
    avio_put_str(s, str);
308
}
309 b7effd4e Anton Khirnov
310
#define GET(name, type) \
311
    type get_be ##name(AVIOContext *s) \
312
{\
313
    return avio_rb ##name(s);\
314
}\
315
    type get_le ##name(AVIOContext *s) \
316
{\
317
    return avio_rl ##name(s);\
318
}
319
320
GET(16, unsigned int)
321
GET(24, unsigned int)
322
GET(32, unsigned int)
323
GET(64, uint64_t)
324
325
#undef GET
326
327 77eb5504 Anton Khirnov
#define PUT(name, type ) \
328
    void put_le ##name(AVIOContext *s, type val)\
329
{\
330
        avio_wl ##name(s, val);\
331
}\
332
    void put_be ##name(AVIOContext *s, type val)\
333
{\
334
        avio_wb ##name(s, val);\
335
}
336
337
PUT(16, unsigned int)
338
PUT(24, unsigned int)
339
PUT(32, unsigned int)
340
PUT(64, uint64_t)
341
#undef PUT
342
343 b7effd4e Anton Khirnov
int get_byte(AVIOContext *s)
344
{
345
   return avio_r8(s);
346
}
347
int get_buffer(AVIOContext *s, unsigned char *buf, int size)
348
{
349
    return avio_read(s, buf, size);
350
}
351 b3db9cee Anton Khirnov
int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size)
352
{
353
    return ffio_read_partial(s, buf, size);
354
}
355 77eb5504 Anton Khirnov
void put_byte(AVIOContext *s, int val)
356
{
357
    avio_w8(s, val);
358
}
359
void put_buffer(AVIOContext *s, const unsigned char *buf, int size)
360
{
361
    avio_write(s, buf, size);
362
}
363 0ac8e2bf Anton Khirnov
void put_nbyte(AVIOContext *s, int b, int count)
364
{
365
    ffio_fill(s, b, count);
366
}
367 22a3212e Anton Khirnov
368
int url_fopen(AVIOContext **s, const char *filename, int flags)
369
{
370
    return avio_open(s, filename, flags);
371
}
372
int url_fclose(AVIOContext *s)
373
{
374
    return avio_close(s);
375
}
376 6b4aa5da Anton Khirnov
int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
377
{
378
    return avio_seek(s, offset, whence);
379
}
380 76aa876e Anton Khirnov
int64_t url_fsize(AVIOContext *s)
381
{
382
    return avio_size(s);
383
}
384 59f65d95 Anton Khirnov
int url_setbufsize(AVIOContext *s, int buf_size)
385
{
386
    return ffio_set_buf_size(s, buf_size);
387
}
388 d9d86e00 Anton Khirnov
int url_fprintf(AVIOContext *s, const char *fmt, ...)
389
{
390
    va_list ap;
391
    char buf[4096];
392
    int ret;
393
394
    va_start(ap, fmt);
395
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
396
    va_end(ap);
397
    avio_write(s, buf, strlen(buf));
398
    return ret;
399
}
400 b7f2fdde Anton Khirnov
void put_flush_packet(AVIOContext *s)
401
{
402
    avio_flush(s);
403
}
404 933e90a6 Anton Khirnov
int av_url_read_fpause(AVIOContext *s, int pause)
405
{
406
    return ffio_read_pause(s, pause);
407
}
408
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
409
                         int64_t timestamp, int flags)
410
{
411
    return ffio_read_seek(s, stream_index, timestamp, flags);
412
}
413 4c4427a7 Anton Khirnov
void init_checksum(AVIOContext *s,
414
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
415
                   unsigned long checksum)
416
{
417
    ffio_init_checksum(s, update_checksum, checksum);
418
}
419 b64030f2 Anton Khirnov
unsigned long get_checksum(AVIOContext *s)
420
{
421
    return ffio_get_checksum(s);
422
}
423 b92c5452 Anton Khirnov
int url_open_dyn_buf(AVIOContext **s)
424
{
425
    return avio_open_dyn_buf(s);
426
}
427 403ee835 Anton Khirnov
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
428
{
429
    return ffio_open_dyn_packet_buf(s, max_packet_size);
430
}
431 6dc7d80d Anton Khirnov
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
432
{
433
    return avio_close_dyn_buf(s, pbuffer);
434
}
435 724f6a0f Anton Khirnov
int url_fdopen(AVIOContext **s, URLContext *h)
436
{
437
    return ffio_fdopen(s, h);
438
}
439 4efd5cf3 Anton Khirnov
#endif
440
441 ae628ec1 Anton Khirnov
int avio_put_str(AVIOContext *s, const char *str)
442 4efd5cf3 Anton Khirnov
{
443
    int len = 1;
444
    if (str) {
445
        len += strlen(str);
446 77eb5504 Anton Khirnov
        avio_write(s, (const unsigned char *) str, len);
447 4efd5cf3 Anton Khirnov
    } else
448 77eb5504 Anton Khirnov
        avio_w8(s, 0);
449 4efd5cf3 Anton Khirnov
    return len;
450 75bdb984 Philip Gladstone
}
451
452 ae628ec1 Anton Khirnov
int avio_put_str16le(AVIOContext *s, const char *str)
453 dccbd97d Anton Khirnov
{
454
    const uint8_t *q = str;
455
    int ret = 0;
456
457
    while (*q) {
458
        uint32_t ch;
459
        uint16_t tmp;
460
461
        GET_UTF8(ch, *q++, break;)
462 77eb5504 Anton Khirnov
        PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
463 dccbd97d Anton Khirnov
    }
464 77eb5504 Anton Khirnov
    avio_wl16(s, 0);
465 dccbd97d Anton Khirnov
    ret += 2;
466
    return ret;
467
}
468
469 603e5c0b Michael Chinen
int ff_get_v_length(uint64_t val){
470
    int i=1;
471
472
    while(val>>=7)
473
        i++;
474
475
    return i;
476
}
477
478 ae628ec1 Anton Khirnov
void ff_put_v(AVIOContext *bc, uint64_t val){
479 603e5c0b Michael Chinen
    int i= ff_get_v_length(val);
480
481
    while(--i>0)
482 77eb5504 Anton Khirnov
        avio_w8(bc, 128 | (val>>(7*i)));
483 603e5c0b Michael Chinen
484 77eb5504 Anton Khirnov
    avio_w8(bc, val&127);
485 603e5c0b Michael Chinen
}
486
487 77eb5504 Anton Khirnov
void avio_wl64(AVIOContext *s, uint64_t val)
488 de6d9b64 Fabrice Bellard
{
489 77eb5504 Anton Khirnov
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
490
    avio_wl32(s, (uint32_t)(val >> 32));
491 de6d9b64 Fabrice Bellard
}
492
493 77eb5504 Anton Khirnov
void avio_wb64(AVIOContext *s, uint64_t val)
494 de6d9b64 Fabrice Bellard
{
495 77eb5504 Anton Khirnov
    avio_wb32(s, (uint32_t)(val >> 32));
496
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
497 de6d9b64 Fabrice Bellard
}
498
499 77eb5504 Anton Khirnov
void avio_wl16(AVIOContext *s, unsigned int val)
500 de6d9b64 Fabrice Bellard
{
501 77eb5504 Anton Khirnov
    avio_w8(s, val);
502
    avio_w8(s, val >> 8);
503 de6d9b64 Fabrice Bellard
}
504
505 77eb5504 Anton Khirnov
void avio_wb16(AVIOContext *s, unsigned int val)
506 de6d9b64 Fabrice Bellard
{
507 77eb5504 Anton Khirnov
    avio_w8(s, val >> 8);
508
    avio_w8(s, val);
509 de6d9b64 Fabrice Bellard
}
510
511 77eb5504 Anton Khirnov
void avio_wl24(AVIOContext *s, unsigned int val)
512 ea395e8c Aurelien Jacobs
{
513 77eb5504 Anton Khirnov
    avio_wl16(s, val & 0xffff);
514
    avio_w8(s, val >> 16);
515 ea395e8c Aurelien Jacobs
}
516
517 77eb5504 Anton Khirnov
void avio_wb24(AVIOContext *s, unsigned int val)
518 a254c574 Michael Niedermayer
{
519 77eb5504 Anton Khirnov
    avio_wb16(s, val >> 8);
520
    avio_w8(s, val);
521 a254c574 Michael Niedermayer
}
522
523 61840b43 Anton Khirnov
#if FF_API_OLD_AVIO
524 ae628ec1 Anton Khirnov
void put_tag(AVIOContext *s, const char *tag)
525 de6d9b64 Fabrice Bellard
{
526
    while (*tag) {
527 77eb5504 Anton Khirnov
        avio_w8(s, *tag++);
528 de6d9b64 Fabrice Bellard
    }
529
}
530 61840b43 Anton Khirnov
#endif
531 de6d9b64 Fabrice Bellard
532
/* Input stream */
533
534 ae628ec1 Anton Khirnov
static void fill_buffer(AVIOContext *s)
535 de6d9b64 Fabrice Bellard
{
536 e360ada2 Martin Storsjö
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
537 4589dc5c Michael Niedermayer
    int len= s->buffer_size - (dst - s->buffer);
538 01d91b9b Tomas Härdin
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
539 4589dc5c Michael Niedermayer
540 abba3dd1 Fabrice Bellard
    /* no need to do anything if EOF already reached */
541
    if (s->eof_reached)
542
        return;
543 ee9f36a8 Michael Niedermayer
544 4589dc5c Michael Niedermayer
    if(s->update_checksum && dst == s->buffer){
545 0f0680c5 Michael Niedermayer
        if(s->buf_end > s->checksum_ptr)
546
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
547 ee9f36a8 Michael Niedermayer
        s->checksum_ptr= s->buffer;
548
    }
549
550 01d91b9b Tomas Härdin
    /* make buffer smaller in case it ended up large after probing */
551
    if (s->buffer_size > max_buffer_size) {
552 59f65d95 Anton Khirnov
        ffio_set_buf_size(s, max_buffer_size);
553 01d91b9b Tomas Härdin
554
        s->checksum_ptr = dst = s->buffer;
555
        len = s->buffer_size;
556
    }
557
558 602dd8f6 Benoit Fouet
    if(s->read_packet)
559 c66f53cf Martin Storsjö
        len = s->read_packet(s->opaque, dst, len);
560 4589dc5c Michael Niedermayer
    else
561
        len = 0;
562 abba3dd1 Fabrice Bellard
    if (len <= 0) {
563
        /* do not modify buffer if EOF reached so that a seek back can
564
           be done without rereading data */
565 de6d9b64 Fabrice Bellard
        s->eof_reached = 1;
566 5f097703 Alex Beregszaszi
        if(len<0)
567
            s->error= len;
568 abba3dd1 Fabrice Bellard
    } else {
569
        s->pos += len;
570 4589dc5c Michael Niedermayer
        s->buf_ptr = dst;
571
        s->buf_end = dst + len;
572 de6d9b64 Fabrice Bellard
    }
573
}
574
575 7f37f568 Diego Biurrun
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
576
                                    unsigned int len)
577
{
578 3abe5fbd Aurelien Jacobs
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
579 992e78f5 Baptiste Coudurier
}
580
581 b64030f2 Anton Khirnov
unsigned long ffio_get_checksum(AVIOContext *s)
582 7f37f568 Diego Biurrun
{
583 ee9f36a8 Michael Niedermayer
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
584 465e1dad Michael Niedermayer
    s->update_checksum= NULL;
585 ee9f36a8 Michael Niedermayer
    return s->checksum;
586
}
587
588 4c4427a7 Anton Khirnov
void ffio_init_checksum(AVIOContext *s,
589 7f37f568 Diego Biurrun
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
590
                   unsigned long checksum)
591
{
592 ee9f36a8 Michael Niedermayer
    s->update_checksum= update_checksum;
593 465e1dad Michael Niedermayer
    if(s->update_checksum){
594 ccb15994 Michael Niedermayer
        s->checksum= checksum;
595 465e1dad Michael Niedermayer
        s->checksum_ptr= s->buf_ptr;
596
    }
597 ee9f36a8 Michael Niedermayer
}
598
599 abba3dd1 Fabrice Bellard
/* XXX: put an inline version */
600 b7effd4e Anton Khirnov
int avio_r8(AVIOContext *s)
601 de6d9b64 Fabrice Bellard
{
602 3aa13da9 Reimar Döffinger
    if (s->buf_ptr >= s->buf_end)
603 de6d9b64 Fabrice Bellard
        fill_buffer(s);
604 853a0fcc Reimar Döffinger
    if (s->buf_ptr < s->buf_end)
605
        return *s->buf_ptr++;
606
    return 0;
607 de6d9b64 Fabrice Bellard
}
608
609 e5197539 Anton Khirnov
#if FF_API_OLD_AVIO
610 ae628ec1 Anton Khirnov
int url_fgetc(AVIOContext *s)
611 abba3dd1 Fabrice Bellard
{
612 3aa13da9 Reimar Döffinger
    if (s->buf_ptr >= s->buf_end)
613 abba3dd1 Fabrice Bellard
        fill_buffer(s);
614 853a0fcc Reimar Döffinger
    if (s->buf_ptr < s->buf_end)
615
        return *s->buf_ptr++;
616
    return URL_EOF;
617 abba3dd1 Fabrice Bellard
}
618 e5197539 Anton Khirnov
#endif
619 abba3dd1 Fabrice Bellard
620 b7effd4e Anton Khirnov
int avio_read(AVIOContext *s, unsigned char *buf, int size)
621 de6d9b64 Fabrice Bellard
{
622
    int len, size1;
623
624
    size1 = size;
625
    while (size > 0) {
626
        len = s->buf_end - s->buf_ptr;
627
        if (len > size)
628
            len = size;
629
        if (len == 0) {
630 3e2b6358 Michael Niedermayer
            if(size > s->buffer_size && !s->update_checksum){
631 602dd8f6 Benoit Fouet
                if(s->read_packet)
632 c66f53cf Martin Storsjö
                    len = s->read_packet(s->opaque, buf, size);
633 3e2b6358 Michael Niedermayer
                if (len <= 0) {
634
                    /* do not modify buffer if EOF reached so that a seek back can
635
                    be done without rereading data */
636
                    s->eof_reached = 1;
637
                    if(len<0)
638
                        s->error= len;
639
                    break;
640
                } else {
641
                    s->pos += len;
642
                    size -= len;
643
                    buf += len;
644
                    s->buf_ptr = s->buffer;
645
                    s->buf_end = s->buffer/* + len*/;
646
                }
647
            }else{
648
                fill_buffer(s);
649
                len = s->buf_end - s->buf_ptr;
650
                if (len == 0)
651
                    break;
652
            }
653 de6d9b64 Fabrice Bellard
        } else {
654
            memcpy(buf, s->buf_ptr, len);
655
            buf += len;
656
            s->buf_ptr += len;
657
            size -= len;
658
        }
659
    }
660 7f7686df Reimar Döffinger
    if (size1 == size) {
661 3e68b3ba Anton Khirnov
        if (s->error)         return s->error;
662 66e5b1df Anton Khirnov
        if (s->eof_reached)   return AVERROR_EOF;
663 7f7686df Reimar Döffinger
    }
664 de6d9b64 Fabrice Bellard
    return size1 - size;
665
}
666
667 b3db9cee Anton Khirnov
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
668 e15dec10 Leon van Stuivenberg
{
669
    int len;
670 115329f1 Diego Biurrun
671 0ecca7a4 Michael Niedermayer
    if(size<0)
672
        return -1;
673 e15dec10 Leon van Stuivenberg
674
    len = s->buf_end - s->buf_ptr;
675
    if (len == 0) {
676
        fill_buffer(s);
677
        len = s->buf_end - s->buf_ptr;
678
    }
679
    if (len > size)
680
        len = size;
681
    memcpy(buf, s->buf_ptr, len);
682
    s->buf_ptr += len;
683 7f7686df Reimar Döffinger
    if (!len) {
684 3e68b3ba Anton Khirnov
        if (s->error)         return s->error;
685 66e5b1df Anton Khirnov
        if (s->eof_reached)   return AVERROR_EOF;
686 7f7686df Reimar Döffinger
    }
687 e15dec10 Leon van Stuivenberg
    return len;
688
}
689
690 b7effd4e Anton Khirnov
unsigned int avio_rl16(AVIOContext *s)
691 de6d9b64 Fabrice Bellard
{
692
    unsigned int val;
693 b7effd4e Anton Khirnov
    val = avio_r8(s);
694
    val |= avio_r8(s) << 8;
695 de6d9b64 Fabrice Bellard
    return val;
696
}
697
698 b7effd4e Anton Khirnov
unsigned int avio_rl24(AVIOContext *s)
699 ea395e8c Aurelien Jacobs
{
700
    unsigned int val;
701 b7effd4e Anton Khirnov
    val = avio_rl16(s);
702
    val |= avio_r8(s) << 16;
703 ea395e8c Aurelien Jacobs
    return val;
704
}
705
706 b7effd4e Anton Khirnov
unsigned int avio_rl32(AVIOContext *s)
707 de6d9b64 Fabrice Bellard
{
708
    unsigned int val;
709 b7effd4e Anton Khirnov
    val = avio_rl16(s);
710
    val |= avio_rl16(s) << 16;
711 de6d9b64 Fabrice Bellard
    return val;
712
}
713
714 b7effd4e Anton Khirnov
uint64_t avio_rl64(AVIOContext *s)
715 de6d9b64 Fabrice Bellard
{
716 0c1a9eda Zdenek Kabelac
    uint64_t val;
717 b7effd4e Anton Khirnov
    val = (uint64_t)avio_rl32(s);
718
    val |= (uint64_t)avio_rl32(s) << 32;
719 de6d9b64 Fabrice Bellard
    return val;
720
}
721
722 b7effd4e Anton Khirnov
unsigned int avio_rb16(AVIOContext *s)
723 de6d9b64 Fabrice Bellard
{
724
    unsigned int val;
725 b7effd4e Anton Khirnov
    val = avio_r8(s) << 8;
726
    val |= avio_r8(s);
727 de6d9b64 Fabrice Bellard
    return val;
728
}
729
730 b7effd4e Anton Khirnov
unsigned int avio_rb24(AVIOContext *s)
731 de6d9b64 Fabrice Bellard
{
732
    unsigned int val;
733 b7effd4e Anton Khirnov
    val = avio_rb16(s) << 8;
734
    val |= avio_r8(s);
735 de6d9b64 Fabrice Bellard
    return val;
736
}
737 b7effd4e Anton Khirnov
unsigned int avio_rb32(AVIOContext *s)
738 a254c574 Michael Niedermayer
{
739
    unsigned int val;
740 b7effd4e Anton Khirnov
    val = avio_rb16(s) << 16;
741
    val |= avio_rb16(s);
742 a254c574 Michael Niedermayer
    return val;
743
}
744 de6d9b64 Fabrice Bellard
745 e16ead07 Anton Khirnov
#if FF_API_OLD_AVIO
746 ae628ec1 Anton Khirnov
char *get_strz(AVIOContext *s, char *buf, int maxlen)
747 75bdb984 Philip Gladstone
{
748 e16ead07 Anton Khirnov
    avio_get_str(s, INT_MAX, buf, maxlen);
749 75bdb984 Philip Gladstone
    return buf;
750
}
751 e16ead07 Anton Khirnov
#endif
752 75bdb984 Philip Gladstone
753 ae628ec1 Anton Khirnov
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
754 7c892951 Aurelien Jacobs
{
755
    int i = 0;
756
    char c;
757
758
    do {
759 b7effd4e Anton Khirnov
        c = avio_r8(s);
760 a43416a5 Aurelien Jacobs
        if (c && i < maxlen-1)
761 7c892951 Aurelien Jacobs
            buf[i++] = c;
762
    } while (c != '\n' && c);
763
764
    buf[i] = 0;
765 a43416a5 Aurelien Jacobs
    return i;
766 7c892951 Aurelien Jacobs
}
767
768 41d8555f Reimar Döffinger
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
769
{
770
    int i;
771
772
    // reserve 1 byte for terminating 0
773
    buflen = FFMIN(buflen - 1, maxlen);
774
    for (i = 0; i < buflen; i++)
775
        if (!(buf[i] = avio_r8(s)))
776
            return i + 1;
777
    if (buflen)
778
        buf[i] = 0;
779
    for (; i < maxlen; i++)
780
        if (!avio_r8(s))
781
            return i + 1;
782
    return maxlen;
783
}
784
785 93b78d12 Anton Khirnov
#define GET_STR16(type, read) \
786 ae628ec1 Anton Khirnov
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
787 93b78d12 Anton Khirnov
{\
788
    char* q = buf;\
789
    int ret = 0;\
790
    while (ret + 1 < maxlen) {\
791
        uint8_t tmp;\
792
        uint32_t ch;\
793
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
794
        if (!ch)\
795
            break;\
796
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
797
    }\
798
    *q = 0;\
799
    return ret;\
800
}\
801
802 b7effd4e Anton Khirnov
GET_STR16(le, avio_rl16)
803
GET_STR16(be, avio_rb16)
804 93b78d12 Anton Khirnov
805
#undef GET_STR16
806
807 b7effd4e Anton Khirnov
uint64_t avio_rb64(AVIOContext *s)
808 de6d9b64 Fabrice Bellard
{
809 0c1a9eda Zdenek Kabelac
    uint64_t val;
810 b7effd4e Anton Khirnov
    val = (uint64_t)avio_rb32(s) << 32;
811
    val |= (uint64_t)avio_rb32(s);
812 de6d9b64 Fabrice Bellard
    return val;
813
}
814
815 4839c192 Anton Khirnov
uint64_t ffio_read_varlen(AVIOContext *bc){
816 897d3eef Kostya Shishkov
    uint64_t val = 0;
817
    int tmp;
818
819
    do{
820 b7effd4e Anton Khirnov
        tmp = avio_r8(bc);
821 897d3eef Kostya Shishkov
        val= (val<<7) + (tmp&127);
822
    }while(tmp&128);
823
    return val;
824
}
825
826 724f6a0f Anton Khirnov
int ffio_fdopen(AVIOContext **s, URLContext *h)
827 de6d9b64 Fabrice Bellard
{
828 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
829 abba3dd1 Fabrice Bellard
    int buffer_size, max_packet_size;
830 de6d9b64 Fabrice Bellard
831 5958df34 Anton Khirnov
    max_packet_size = h->max_packet_size;
832 abba3dd1 Fabrice Bellard
    if (max_packet_size) {
833
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
834
    } else {
835
        buffer_size = IO_BUFFER_SIZE;
836
    }
837 1ea4f593 Fabrice Bellard
    buffer = av_malloc(buffer_size);
838 de6d9b64 Fabrice Bellard
    if (!buffer)
839 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
840 de6d9b64 Fabrice Bellard
841 ae628ec1 Anton Khirnov
    *s = av_mallocz(sizeof(AVIOContext));
842 899681cd Björn Axelsson
    if(!*s) {
843
        av_free(buffer);
844
        return AVERROR(ENOMEM);
845
    }
846
847 e731b8d8 Anton Khirnov
    if (ffio_init_context(*s, buffer, buffer_size,
848 ac9fe33d Gildas Bazin
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
849 58a48c65 Anton Khirnov
                      ffurl_read, ffurl_write, ffurl_seek) < 0) {
850 1ea4f593 Fabrice Bellard
        av_free(buffer);
851 899681cd Björn Axelsson
        av_freep(s);
852 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
853 de6d9b64 Fabrice Bellard
    }
854 8978feda Anton Khirnov
#if FF_API_OLD_AVIO
855 899681cd Björn Axelsson
    (*s)->is_streamed = h->is_streamed;
856 8978feda Anton Khirnov
#endif
857
    (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
858 899681cd Björn Axelsson
    (*s)->max_packet_size = max_packet_size;
859 e7e4810a Björn Axelsson
    if(h->prot) {
860 502bdf68 Michael Niedermayer
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
861 bc5c918e Diego Biurrun
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
862 e7e4810a Björn Axelsson
    }
863 de6d9b64 Fabrice Bellard
    return 0;
864
}
865
866 59f65d95 Anton Khirnov
int ffio_set_buf_size(AVIOContext *s, int buf_size)
867 de6d9b64 Fabrice Bellard
{
868 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
869 1ea4f593 Fabrice Bellard
    buffer = av_malloc(buf_size);
870 de6d9b64 Fabrice Bellard
    if (!buffer)
871 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
872 de6d9b64 Fabrice Bellard
873 1ea4f593 Fabrice Bellard
    av_free(s->buffer);
874 de6d9b64 Fabrice Bellard
    s->buffer = buffer;
875
    s->buffer_size = buf_size;
876
    s->buf_ptr = buffer;
877 770d9daf Björn Axelsson
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
878
    return 0;
879
}
880
881 364cacc7 Aurelien Jacobs
#if FF_API_URL_RESETBUF
882 ae628ec1 Anton Khirnov
int url_resetbuf(AVIOContext *s, int flags)
883 08580cb0 Benoit Fouet
#else
884 ae628ec1 Anton Khirnov
static int url_resetbuf(AVIOContext *s, int flags)
885 08580cb0 Benoit Fouet
#endif
886 770d9daf Björn Axelsson
{
887 364cacc7 Aurelien Jacobs
#if FF_API_URL_RESETBUF
888 6fa197e2 Eli Friedman
    if (flags & URL_RDWR)
889 770d9daf Björn Axelsson
        return AVERROR(EINVAL);
890 08580cb0 Benoit Fouet
#else
891
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
892
#endif
893 770d9daf Björn Axelsson
894
    if (flags & URL_WRONLY) {
895
        s->buf_end = s->buffer + s->buffer_size;
896
        s->write_flag = 1;
897
    } else {
898
        s->buf_end = s->buffer;
899
        s->write_flag = 0;
900
    }
901 de6d9b64 Fabrice Bellard
    return 0;
902
}
903
904 f1ef2cd9 Anton Khirnov
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
905 01d91b9b Tomas Härdin
{
906
    int64_t buffer_start;
907
    int buffer_size;
908 496c645d Martin Storsjö
    int overlap, new_size, alloc_size;
909 01d91b9b Tomas Härdin
910
    if (s->write_flag)
911
        return AVERROR(EINVAL);
912
913
    buffer_size = s->buf_end - s->buffer;
914
915
    /* the buffers must touch or overlap */
916
    if ((buffer_start = s->pos - buffer_size) > buf_size)
917
        return AVERROR(EINVAL);
918
919
    overlap = buf_size - buffer_start;
920
    new_size = buf_size + buffer_size - overlap;
921
922 496c645d Martin Storsjö
    alloc_size = FFMAX(s->buffer_size, new_size);
923
    if (alloc_size > buf_size)
924
        if (!(buf = av_realloc(buf, alloc_size)))
925 01d91b9b Tomas Härdin
            return AVERROR(ENOMEM);
926
927 496c645d Martin Storsjö
    if (new_size > buf_size) {
928 01d91b9b Tomas Härdin
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
929
        buf_size = new_size;
930
    }
931
932
    av_free(s->buffer);
933
    s->buf_ptr = s->buffer = buf;
934 496c645d Martin Storsjö
    s->buffer_size = alloc_size;
935
    s->pos = buf_size;
936 01d91b9b Tomas Härdin
    s->buf_end = s->buf_ptr + buf_size;
937
    s->eof_reached = 0;
938
    s->must_flush = 0;
939
940
    return 0;
941
}
942
943 22a3212e Anton Khirnov
int avio_open(AVIOContext **s, const char *filename, int flags)
944 de6d9b64 Fabrice Bellard
{
945
    URLContext *h;
946
    int err;
947
948 0589da0a Anton Khirnov
    err = ffurl_open(&h, filename, flags);
949 de6d9b64 Fabrice Bellard
    if (err < 0)
950
        return err;
951 724f6a0f Anton Khirnov
    err = ffio_fdopen(s, h);
952 de6d9b64 Fabrice Bellard
    if (err < 0) {
953 e52a9145 Anton Khirnov
        ffurl_close(h);
954 de6d9b64 Fabrice Bellard
        return err;
955
    }
956
    return 0;
957
}
958
959 22a3212e Anton Khirnov
int avio_close(AVIOContext *s)
960 de6d9b64 Fabrice Bellard
{
961
    URLContext *h = s->opaque;
962 115329f1 Diego Biurrun
963 1ea4f593 Fabrice Bellard
    av_free(s->buffer);
964 899681cd Björn Axelsson
    av_free(s);
965 e52a9145 Anton Khirnov
    return ffurl_close(h);
966 de6d9b64 Fabrice Bellard
}
967
968 8d9769a7 Anton Khirnov
#if FF_API_OLD_AVIO
969 ae628ec1 Anton Khirnov
URLContext *url_fileno(AVIOContext *s)
970 de6d9b64 Fabrice Bellard
{
971
    return s->opaque;
972
}
973 8d9769a7 Anton Khirnov
#endif
974 de6d9b64 Fabrice Bellard
975 d9d86e00 Anton Khirnov
int avio_printf(AVIOContext *s, const char *fmt, ...)
976 abba3dd1 Fabrice Bellard
{
977
    va_list ap;
978
    char buf[4096];
979
    int ret;
980
981
    va_start(ap, fmt);
982
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
983
    va_end(ap);
984 77eb5504 Anton Khirnov
    avio_write(s, buf, strlen(buf));
985 abba3dd1 Fabrice Bellard
    return ret;
986
}
987
988 655e45e7 Anton Khirnov
#if FF_API_OLD_AVIO
989 ae628ec1 Anton Khirnov
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
990 abba3dd1 Fabrice Bellard
{
991
    int c;
992
    char *q;
993
994 e5197539 Anton Khirnov
    c = avio_r8(s);
995 66e5b1df Anton Khirnov
    if (s->eof_reached)
996 abba3dd1 Fabrice Bellard
        return NULL;
997
    q = buf;
998
    for(;;) {
999 66e5b1df Anton Khirnov
        if (s->eof_reached || c == '\n')
1000 abba3dd1 Fabrice Bellard
            break;
1001
        if ((q - buf) < buf_size - 1)
1002
            *q++ = c;
1003 e5197539 Anton Khirnov
        c = avio_r8(s);
1004 abba3dd1 Fabrice Bellard
    }
1005
    if (buf_size > 0)
1006
        *q = '\0';
1007
    return buf;
1008
}
1009
1010 ae628ec1 Anton Khirnov
int url_fget_max_packet_size(AVIOContext *s)
1011 abba3dd1 Fabrice Bellard
{
1012
    return s->max_packet_size;
1013
}
1014 e8bb2e24 Anton Khirnov
#endif
1015 abba3dd1 Fabrice Bellard
1016 933e90a6 Anton Khirnov
int ffio_read_pause(AVIOContext *s, int pause)
1017 e7e4810a Björn Axelsson
{
1018
    if (!s->read_pause)
1019
        return AVERROR(ENOSYS);
1020 502bdf68 Michael Niedermayer
    return s->read_pause(s->opaque, pause);
1021 e7e4810a Björn Axelsson
}
1022
1023 933e90a6 Anton Khirnov
int64_t ffio_read_seek(AVIOContext *s, int stream_index,
1024
                       int64_t timestamp, int flags)
1025 e7e4810a Björn Axelsson
{
1026
    URLContext *h = s->opaque;
1027 bc5c918e Diego Biurrun
    int64_t ret;
1028 e7e4810a Björn Axelsson
    if (!s->read_seek)
1029
        return AVERROR(ENOSYS);
1030
    ret = s->read_seek(h, stream_index, timestamp, flags);
1031
    if(ret >= 0) {
1032 c007c687 Howard Chu
        int64_t pos;
1033 e7e4810a Björn Axelsson
        s->buf_ptr = s->buf_end; // Flush buffer
1034 c007c687 Howard Chu
        pos = s->seek(h, 0, SEEK_CUR);
1035
        if (pos >= 0)
1036
            s->pos = pos;
1037
        else if (pos != AVERROR(ENOSYS))
1038
            ret = pos;
1039 e7e4810a Björn Axelsson
    }
1040
    return ret;
1041
}
1042
1043 de6d9b64 Fabrice Bellard
/* buffer handling */
1044 83fddaeb Anton Khirnov
#if FF_API_OLD_AVIO
1045 ae628ec1 Anton Khirnov
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
1046 de6d9b64 Fabrice Bellard
{
1047 899681cd Björn Axelsson
    int ret;
1048 ae628ec1 Anton Khirnov
    *s = av_mallocz(sizeof(AVIOContext));
1049 899681cd Björn Axelsson
    if(!*s)
1050
        return AVERROR(ENOMEM);
1051 e731b8d8 Anton Khirnov
    ret = ffio_init_context(*s, buf, buf_size,
1052 899681cd Björn Axelsson
                        (flags & URL_WRONLY || flags & URL_RDWR),
1053
                        NULL, NULL, NULL, NULL);
1054
    if(ret != 0)
1055
        av_freep(s);
1056
    return ret;
1057 de6d9b64 Fabrice Bellard
}
1058
1059 ae628ec1 Anton Khirnov
int url_close_buf(AVIOContext *s)
1060 de6d9b64 Fabrice Bellard
{
1061 b7f2fdde Anton Khirnov
    avio_flush(s);
1062 de6d9b64 Fabrice Bellard
    return s->buf_ptr - s->buffer;
1063
}
1064 35f10235 Anton Khirnov
#endif
1065 abba3dd1 Fabrice Bellard
1066
/* output in a dynamic buffer */
1067
1068
typedef struct DynBuffer {
1069
    int pos, size, allocated_size;
1070 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
1071 abba3dd1 Fabrice Bellard
    int io_buffer_size;
1072 0c1a9eda Zdenek Kabelac
    uint8_t io_buffer[1];
1073 abba3dd1 Fabrice Bellard
} DynBuffer;
1074
1075 576ae256 Michael Niedermayer
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1076 abba3dd1 Fabrice Bellard
{
1077
    DynBuffer *d = opaque;
1078 6bc03a69 Reimar Döffinger
    unsigned new_size, new_allocated_size;
1079 115329f1 Diego Biurrun
1080 abba3dd1 Fabrice Bellard
    /* reallocate buffer if needed */
1081
    new_size = d->pos + buf_size;
1082
    new_allocated_size = d->allocated_size;
1083 568e18b1 Michael Niedermayer
    if(new_size < d->pos || new_size > INT_MAX/2)
1084
        return -1;
1085 abba3dd1 Fabrice Bellard
    while (new_size > new_allocated_size) {
1086
        if (!new_allocated_size)
1087
            new_allocated_size = new_size;
1088
        else
1089 115329f1 Diego Biurrun
            new_allocated_size += new_allocated_size / 2 + 1;
1090 abba3dd1 Fabrice Bellard
    }
1091 115329f1 Diego Biurrun
1092 abba3dd1 Fabrice Bellard
    if (new_allocated_size > d->allocated_size) {
1093 b88ba823 Mark Hills
        d->buffer = av_realloc(d->buffer, new_allocated_size);
1094
        if(d->buffer == NULL)
1095 7ce820ef Reimar Döffinger
             return AVERROR(ENOMEM);
1096 abba3dd1 Fabrice Bellard
        d->allocated_size = new_allocated_size;
1097
    }
1098
    memcpy(d->buffer + d->pos, buf, buf_size);
1099
    d->pos = new_size;
1100
    if (d->pos > d->size)
1101
        d->size = d->pos;
1102 576ae256 Michael Niedermayer
    return buf_size;
1103 abba3dd1 Fabrice Bellard
}
1104
1105 576ae256 Michael Niedermayer
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1106 abba3dd1 Fabrice Bellard
{
1107
    unsigned char buf1[4];
1108 576ae256 Michael Niedermayer
    int ret;
1109 abba3dd1 Fabrice Bellard
1110
    /* packetized write: output the header */
1111 aa805f94 Reimar Döffinger
    AV_WB32(buf1, buf_size);
1112 576ae256 Michael Niedermayer
    ret= dyn_buf_write(opaque, buf1, 4);
1113
    if(ret < 0)
1114
        return ret;
1115 abba3dd1 Fabrice Bellard
1116
    /* then the data */
1117 576ae256 Michael Niedermayer
    return dyn_buf_write(opaque, buf, buf_size);
1118 abba3dd1 Fabrice Bellard
}
1119
1120 bc5c918e Diego Biurrun
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1121 abba3dd1 Fabrice Bellard
{
1122
    DynBuffer *d = opaque;
1123
1124
    if (whence == SEEK_CUR)
1125
        offset += d->pos;
1126
    else if (whence == SEEK_END)
1127
        offset += d->size;
1128
    if (offset < 0 || offset > 0x7fffffffLL)
1129
        return -1;
1130
    d->pos = offset;
1131
    return 0;
1132
}
1133
1134 ae628ec1 Anton Khirnov
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1135 abba3dd1 Fabrice Bellard
{
1136
    DynBuffer *d;
1137 0c5a43d6 Reimar Döffinger
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1138 115329f1 Diego Biurrun
1139 568e18b1 Michael Niedermayer
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1140
        return -1;
1141 dc2c2eea Reimar Döffinger
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1142 abba3dd1 Fabrice Bellard
    if (!d)
1143 0e7ceb24 Reimar Döffinger
        return AVERROR(ENOMEM);
1144 8bea72f7 Anton Khirnov
    d->io_buffer_size = io_buffer_size;
1145
    *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
1146
                            max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1147
                            max_packet_size ? NULL : dyn_buf_seek);
1148 899681cd Björn Axelsson
    if(!*s) {
1149
        av_free(d);
1150
        return AVERROR(ENOMEM);
1151
    }
1152 8bea72f7 Anton Khirnov
    (*s)->max_packet_size = max_packet_size;
1153
    return 0;
1154 abba3dd1 Fabrice Bellard
}
1155
1156 b92c5452 Anton Khirnov
int avio_open_dyn_buf(AVIOContext **s)
1157 abba3dd1 Fabrice Bellard
{
1158
    return url_open_dyn_buf_internal(s, 0);
1159
}
1160
1161 403ee835 Anton Khirnov
int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1162 abba3dd1 Fabrice Bellard
{
1163
    if (max_packet_size <= 0)
1164
        return -1;
1165
    return url_open_dyn_buf_internal(s, max_packet_size);
1166
}
1167
1168 6dc7d80d Anton Khirnov
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1169 abba3dd1 Fabrice Bellard
{
1170
    DynBuffer *d = s->opaque;
1171
    int size;
1172 de1807bb Josh Allmann
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1173
    int padding = 0;
1174
1175
    /* don't attempt to pad fixed-size packet buffers */
1176
    if (!s->max_packet_size) {
1177 77eb5504 Anton Khirnov
        avio_write(s, padbuf, sizeof(padbuf));
1178 de1807bb Josh Allmann
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1179
    }
1180 abba3dd1 Fabrice Bellard
1181 b7f2fdde Anton Khirnov
    avio_flush(s);
1182 abba3dd1 Fabrice Bellard
1183
    *pbuffer = d->buffer;
1184
    size = d->size;
1185
    av_free(d);
1186 899681cd Björn Axelsson
    av_free(s);
1187 de1807bb Josh Allmann
    return size - padding;
1188 abba3dd1 Fabrice Bellard
}