Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ eda4cf92

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