Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 3308ddfb

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