Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 75a8e0f0

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