Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 2912e87a

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 2912e87a Mans Rullgard
 * This file is part of Libav.
6 b78e7197 Diego Biurrun
 *
7 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 245976da Diego Biurrun
22
#include "libavutil/crc.h"
23 aa805f94 Reimar Döffinger
#include "libavutil/intreadwrite.h"
24 de6d9b64 Fabrice Bellard
#include "avformat.h"
25 5c91a675 Zdenek Kabelac
#include "avio.h"
26 e731b8d8 Anton Khirnov
#include "avio_internal.h"
27 603e5c0b Michael Chinen
#include "internal.h"
28 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 b7f2fdde Anton Khirnov
void avio_flush(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 b7f2fdde Anton Khirnov
void put_flush_packet(AVIOContext *s)
397
{
398
    avio_flush(s);
399
}
400 4efd5cf3 Anton Khirnov
#endif
401
402 ae628ec1 Anton Khirnov
int avio_put_str(AVIOContext *s, const char *str)
403 4efd5cf3 Anton Khirnov
{
404
    int len = 1;
405
    if (str) {
406
        len += strlen(str);
407 77eb5504 Anton Khirnov
        avio_write(s, (const unsigned char *) str, len);
408 4efd5cf3 Anton Khirnov
    } else
409 77eb5504 Anton Khirnov
        avio_w8(s, 0);
410 4efd5cf3 Anton Khirnov
    return len;
411 75bdb984 Philip Gladstone
}
412
413 ae628ec1 Anton Khirnov
int avio_put_str16le(AVIOContext *s, const char *str)
414 dccbd97d Anton Khirnov
{
415
    const uint8_t *q = str;
416
    int ret = 0;
417
418
    while (*q) {
419
        uint32_t ch;
420
        uint16_t tmp;
421
422
        GET_UTF8(ch, *q++, break;)
423 77eb5504 Anton Khirnov
        PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
424 dccbd97d Anton Khirnov
    }
425 77eb5504 Anton Khirnov
    avio_wl16(s, 0);
426 dccbd97d Anton Khirnov
    ret += 2;
427
    return ret;
428
}
429
430 603e5c0b Michael Chinen
int ff_get_v_length(uint64_t val){
431
    int i=1;
432
433
    while(val>>=7)
434
        i++;
435
436
    return i;
437
}
438
439 ae628ec1 Anton Khirnov
void ff_put_v(AVIOContext *bc, uint64_t val){
440 603e5c0b Michael Chinen
    int i= ff_get_v_length(val);
441
442
    while(--i>0)
443 77eb5504 Anton Khirnov
        avio_w8(bc, 128 | (val>>(7*i)));
444 603e5c0b Michael Chinen
445 77eb5504 Anton Khirnov
    avio_w8(bc, val&127);
446 603e5c0b Michael Chinen
}
447
448 77eb5504 Anton Khirnov
void avio_wl64(AVIOContext *s, uint64_t val)
449 de6d9b64 Fabrice Bellard
{
450 77eb5504 Anton Khirnov
    avio_wl32(s, (uint32_t)(val & 0xffffffff));
451
    avio_wl32(s, (uint32_t)(val >> 32));
452 de6d9b64 Fabrice Bellard
}
453
454 77eb5504 Anton Khirnov
void avio_wb64(AVIOContext *s, uint64_t val)
455 de6d9b64 Fabrice Bellard
{
456 77eb5504 Anton Khirnov
    avio_wb32(s, (uint32_t)(val >> 32));
457
    avio_wb32(s, (uint32_t)(val & 0xffffffff));
458 de6d9b64 Fabrice Bellard
}
459
460 77eb5504 Anton Khirnov
void avio_wl16(AVIOContext *s, unsigned int val)
461 de6d9b64 Fabrice Bellard
{
462 77eb5504 Anton Khirnov
    avio_w8(s, val);
463
    avio_w8(s, val >> 8);
464 de6d9b64 Fabrice Bellard
}
465
466 77eb5504 Anton Khirnov
void avio_wb16(AVIOContext *s, unsigned int val)
467 de6d9b64 Fabrice Bellard
{
468 77eb5504 Anton Khirnov
    avio_w8(s, val >> 8);
469
    avio_w8(s, val);
470 de6d9b64 Fabrice Bellard
}
471
472 77eb5504 Anton Khirnov
void avio_wl24(AVIOContext *s, unsigned int val)
473 ea395e8c Aurelien Jacobs
{
474 77eb5504 Anton Khirnov
    avio_wl16(s, val & 0xffff);
475
    avio_w8(s, val >> 16);
476 ea395e8c Aurelien Jacobs
}
477
478 77eb5504 Anton Khirnov
void avio_wb24(AVIOContext *s, unsigned int val)
479 a254c574 Michael Niedermayer
{
480 77eb5504 Anton Khirnov
    avio_wb16(s, val >> 8);
481
    avio_w8(s, val);
482 a254c574 Michael Niedermayer
}
483
484 61840b43 Anton Khirnov
#if FF_API_OLD_AVIO
485 ae628ec1 Anton Khirnov
void put_tag(AVIOContext *s, const char *tag)
486 de6d9b64 Fabrice Bellard
{
487
    while (*tag) {
488 77eb5504 Anton Khirnov
        avio_w8(s, *tag++);
489 de6d9b64 Fabrice Bellard
    }
490
}
491 61840b43 Anton Khirnov
#endif
492 de6d9b64 Fabrice Bellard
493
/* Input stream */
494
495 ae628ec1 Anton Khirnov
static void fill_buffer(AVIOContext *s)
496 de6d9b64 Fabrice Bellard
{
497 e360ada2 Martin Storsjö
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
498 4589dc5c Michael Niedermayer
    int len= s->buffer_size - (dst - s->buffer);
499 01d91b9b Tomas Härdin
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
500 4589dc5c Michael Niedermayer
501 abba3dd1 Fabrice Bellard
    /* no need to do anything if EOF already reached */
502
    if (s->eof_reached)
503
        return;
504 ee9f36a8 Michael Niedermayer
505 4589dc5c Michael Niedermayer
    if(s->update_checksum && dst == s->buffer){
506 0f0680c5 Michael Niedermayer
        if(s->buf_end > s->checksum_ptr)
507
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
508 ee9f36a8 Michael Niedermayer
        s->checksum_ptr= s->buffer;
509
    }
510
511 01d91b9b Tomas Härdin
    /* make buffer smaller in case it ended up large after probing */
512
    if (s->buffer_size > max_buffer_size) {
513 59f65d95 Anton Khirnov
        ffio_set_buf_size(s, max_buffer_size);
514 01d91b9b Tomas Härdin
515
        s->checksum_ptr = dst = s->buffer;
516
        len = s->buffer_size;
517
    }
518
519 602dd8f6 Benoit Fouet
    if(s->read_packet)
520 c66f53cf Martin Storsjö
        len = s->read_packet(s->opaque, dst, len);
521 4589dc5c Michael Niedermayer
    else
522
        len = 0;
523 abba3dd1 Fabrice Bellard
    if (len <= 0) {
524
        /* do not modify buffer if EOF reached so that a seek back can
525
           be done without rereading data */
526 de6d9b64 Fabrice Bellard
        s->eof_reached = 1;
527 5f097703 Alex Beregszaszi
        if(len<0)
528
            s->error= len;
529 abba3dd1 Fabrice Bellard
    } else {
530
        s->pos += len;
531 4589dc5c Michael Niedermayer
        s->buf_ptr = dst;
532
        s->buf_end = dst + len;
533 de6d9b64 Fabrice Bellard
    }
534
}
535
536 7f37f568 Diego Biurrun
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
537
                                    unsigned int len)
538
{
539 3abe5fbd Aurelien Jacobs
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
540 992e78f5 Baptiste Coudurier
}
541
542 ae628ec1 Anton Khirnov
unsigned long get_checksum(AVIOContext *s)
543 7f37f568 Diego Biurrun
{
544 ee9f36a8 Michael Niedermayer
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
545 465e1dad Michael Niedermayer
    s->update_checksum= NULL;
546 ee9f36a8 Michael Niedermayer
    return s->checksum;
547
}
548
549 ae628ec1 Anton Khirnov
void init_checksum(AVIOContext *s,
550 7f37f568 Diego Biurrun
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
551
                   unsigned long checksum)
552
{
553 ee9f36a8 Michael Niedermayer
    s->update_checksum= update_checksum;
554 465e1dad Michael Niedermayer
    if(s->update_checksum){
555 ccb15994 Michael Niedermayer
        s->checksum= checksum;
556 465e1dad Michael Niedermayer
        s->checksum_ptr= s->buf_ptr;
557
    }
558 ee9f36a8 Michael Niedermayer
}
559
560 abba3dd1 Fabrice Bellard
/* XXX: put an inline version */
561 b7effd4e Anton Khirnov
int avio_r8(AVIOContext *s)
562 de6d9b64 Fabrice Bellard
{
563 3aa13da9 Reimar Döffinger
    if (s->buf_ptr >= s->buf_end)
564 de6d9b64 Fabrice Bellard
        fill_buffer(s);
565 853a0fcc Reimar Döffinger
    if (s->buf_ptr < s->buf_end)
566
        return *s->buf_ptr++;
567
    return 0;
568 de6d9b64 Fabrice Bellard
}
569
570 e5197539 Anton Khirnov
#if FF_API_OLD_AVIO
571 ae628ec1 Anton Khirnov
int url_fgetc(AVIOContext *s)
572 abba3dd1 Fabrice Bellard
{
573 3aa13da9 Reimar Döffinger
    if (s->buf_ptr >= s->buf_end)
574 abba3dd1 Fabrice Bellard
        fill_buffer(s);
575 853a0fcc Reimar Döffinger
    if (s->buf_ptr < s->buf_end)
576
        return *s->buf_ptr++;
577
    return URL_EOF;
578 abba3dd1 Fabrice Bellard
}
579 e5197539 Anton Khirnov
#endif
580 abba3dd1 Fabrice Bellard
581 b7effd4e Anton Khirnov
int avio_read(AVIOContext *s, unsigned char *buf, int size)
582 de6d9b64 Fabrice Bellard
{
583
    int len, size1;
584
585
    size1 = size;
586
    while (size > 0) {
587
        len = s->buf_end - s->buf_ptr;
588
        if (len > size)
589
            len = size;
590
        if (len == 0) {
591 3e2b6358 Michael Niedermayer
            if(size > s->buffer_size && !s->update_checksum){
592 602dd8f6 Benoit Fouet
                if(s->read_packet)
593 c66f53cf Martin Storsjö
                    len = s->read_packet(s->opaque, buf, size);
594 3e2b6358 Michael Niedermayer
                if (len <= 0) {
595
                    /* do not modify buffer if EOF reached so that a seek back can
596
                    be done without rereading data */
597
                    s->eof_reached = 1;
598
                    if(len<0)
599
                        s->error= len;
600
                    break;
601
                } else {
602
                    s->pos += len;
603
                    size -= len;
604
                    buf += len;
605
                    s->buf_ptr = s->buffer;
606
                    s->buf_end = s->buffer/* + len*/;
607
                }
608
            }else{
609
                fill_buffer(s);
610
                len = s->buf_end - s->buf_ptr;
611
                if (len == 0)
612
                    break;
613
            }
614 de6d9b64 Fabrice Bellard
        } else {
615
            memcpy(buf, s->buf_ptr, len);
616
            buf += len;
617
            s->buf_ptr += len;
618
            size -= len;
619
        }
620
    }
621 7f7686df Reimar Döffinger
    if (size1 == size) {
622 3e68b3ba Anton Khirnov
        if (s->error)         return s->error;
623 66e5b1df Anton Khirnov
        if (s->eof_reached)   return AVERROR_EOF;
624 7f7686df Reimar Döffinger
    }
625 de6d9b64 Fabrice Bellard
    return size1 - size;
626
}
627
628 b3db9cee Anton Khirnov
int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
629 e15dec10 Leon van Stuivenberg
{
630
    int len;
631 115329f1 Diego Biurrun
632 0ecca7a4 Michael Niedermayer
    if(size<0)
633
        return -1;
634 e15dec10 Leon van Stuivenberg
635
    len = s->buf_end - s->buf_ptr;
636
    if (len == 0) {
637
        fill_buffer(s);
638
        len = s->buf_end - s->buf_ptr;
639
    }
640
    if (len > size)
641
        len = size;
642
    memcpy(buf, s->buf_ptr, len);
643
    s->buf_ptr += len;
644 7f7686df Reimar Döffinger
    if (!len) {
645 3e68b3ba Anton Khirnov
        if (s->error)         return s->error;
646 66e5b1df Anton Khirnov
        if (s->eof_reached)   return AVERROR_EOF;
647 7f7686df Reimar Döffinger
    }
648 e15dec10 Leon van Stuivenberg
    return len;
649
}
650
651 b7effd4e Anton Khirnov
unsigned int avio_rl16(AVIOContext *s)
652 de6d9b64 Fabrice Bellard
{
653
    unsigned int val;
654 b7effd4e Anton Khirnov
    val = avio_r8(s);
655
    val |= avio_r8(s) << 8;
656 de6d9b64 Fabrice Bellard
    return val;
657
}
658
659 b7effd4e Anton Khirnov
unsigned int avio_rl24(AVIOContext *s)
660 ea395e8c Aurelien Jacobs
{
661
    unsigned int val;
662 b7effd4e Anton Khirnov
    val = avio_rl16(s);
663
    val |= avio_r8(s) << 16;
664 ea395e8c Aurelien Jacobs
    return val;
665
}
666
667 b7effd4e Anton Khirnov
unsigned int avio_rl32(AVIOContext *s)
668 de6d9b64 Fabrice Bellard
{
669
    unsigned int val;
670 b7effd4e Anton Khirnov
    val = avio_rl16(s);
671
    val |= avio_rl16(s) << 16;
672 de6d9b64 Fabrice Bellard
    return val;
673
}
674
675 b7effd4e Anton Khirnov
uint64_t avio_rl64(AVIOContext *s)
676 de6d9b64 Fabrice Bellard
{
677 0c1a9eda Zdenek Kabelac
    uint64_t val;
678 b7effd4e Anton Khirnov
    val = (uint64_t)avio_rl32(s);
679
    val |= (uint64_t)avio_rl32(s) << 32;
680 de6d9b64 Fabrice Bellard
    return val;
681
}
682
683 b7effd4e Anton Khirnov
unsigned int avio_rb16(AVIOContext *s)
684 de6d9b64 Fabrice Bellard
{
685
    unsigned int val;
686 b7effd4e Anton Khirnov
    val = avio_r8(s) << 8;
687
    val |= avio_r8(s);
688 de6d9b64 Fabrice Bellard
    return val;
689
}
690
691 b7effd4e Anton Khirnov
unsigned int avio_rb24(AVIOContext *s)
692 de6d9b64 Fabrice Bellard
{
693
    unsigned int val;
694 b7effd4e Anton Khirnov
    val = avio_rb16(s) << 8;
695
    val |= avio_r8(s);
696 de6d9b64 Fabrice Bellard
    return val;
697
}
698 b7effd4e Anton Khirnov
unsigned int avio_rb32(AVIOContext *s)
699 a254c574 Michael Niedermayer
{
700
    unsigned int val;
701 b7effd4e Anton Khirnov
    val = avio_rb16(s) << 16;
702
    val |= avio_rb16(s);
703 a254c574 Michael Niedermayer
    return val;
704
}
705 de6d9b64 Fabrice Bellard
706 e16ead07 Anton Khirnov
#if FF_API_OLD_AVIO
707 ae628ec1 Anton Khirnov
char *get_strz(AVIOContext *s, char *buf, int maxlen)
708 75bdb984 Philip Gladstone
{
709 e16ead07 Anton Khirnov
    avio_get_str(s, INT_MAX, buf, maxlen);
710 75bdb984 Philip Gladstone
    return buf;
711
}
712 e16ead07 Anton Khirnov
#endif
713 75bdb984 Philip Gladstone
714 ae628ec1 Anton Khirnov
int ff_get_line(AVIOContext *s, char *buf, int maxlen)
715 7c892951 Aurelien Jacobs
{
716
    int i = 0;
717
    char c;
718
719
    do {
720 b7effd4e Anton Khirnov
        c = avio_r8(s);
721 a43416a5 Aurelien Jacobs
        if (c && i < maxlen-1)
722 7c892951 Aurelien Jacobs
            buf[i++] = c;
723
    } while (c != '\n' && c);
724
725
    buf[i] = 0;
726 a43416a5 Aurelien Jacobs
    return i;
727 7c892951 Aurelien Jacobs
}
728
729 41d8555f Reimar Döffinger
int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
730
{
731
    int i;
732
733
    // reserve 1 byte for terminating 0
734
    buflen = FFMIN(buflen - 1, maxlen);
735
    for (i = 0; i < buflen; i++)
736
        if (!(buf[i] = avio_r8(s)))
737
            return i + 1;
738
    if (buflen)
739
        buf[i] = 0;
740
    for (; i < maxlen; i++)
741
        if (!avio_r8(s))
742
            return i + 1;
743
    return maxlen;
744
}
745
746 93b78d12 Anton Khirnov
#define GET_STR16(type, read) \
747 ae628ec1 Anton Khirnov
    int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
748 93b78d12 Anton Khirnov
{\
749
    char* q = buf;\
750
    int ret = 0;\
751
    while (ret + 1 < maxlen) {\
752
        uint8_t tmp;\
753
        uint32_t ch;\
754
        GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
755
        if (!ch)\
756
            break;\
757
        PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
758
    }\
759
    *q = 0;\
760
    return ret;\
761
}\
762
763 b7effd4e Anton Khirnov
GET_STR16(le, avio_rl16)
764
GET_STR16(be, avio_rb16)
765 93b78d12 Anton Khirnov
766
#undef GET_STR16
767
768 b7effd4e Anton Khirnov
uint64_t avio_rb64(AVIOContext *s)
769 de6d9b64 Fabrice Bellard
{
770 0c1a9eda Zdenek Kabelac
    uint64_t val;
771 b7effd4e Anton Khirnov
    val = (uint64_t)avio_rb32(s) << 32;
772
    val |= (uint64_t)avio_rb32(s);
773 de6d9b64 Fabrice Bellard
    return val;
774
}
775
776 4839c192 Anton Khirnov
uint64_t ffio_read_varlen(AVIOContext *bc){
777 897d3eef Kostya Shishkov
    uint64_t val = 0;
778
    int tmp;
779
780
    do{
781 b7effd4e Anton Khirnov
        tmp = avio_r8(bc);
782 897d3eef Kostya Shishkov
        val= (val<<7) + (tmp&127);
783
    }while(tmp&128);
784
    return val;
785
}
786
787 ae628ec1 Anton Khirnov
int url_fdopen(AVIOContext **s, URLContext *h)
788 de6d9b64 Fabrice Bellard
{
789 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
790 abba3dd1 Fabrice Bellard
    int buffer_size, max_packet_size;
791 de6d9b64 Fabrice Bellard
792 abba3dd1 Fabrice Bellard
    max_packet_size = url_get_max_packet_size(h);
793
    if (max_packet_size) {
794
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
795
    } else {
796
        buffer_size = IO_BUFFER_SIZE;
797
    }
798 1ea4f593 Fabrice Bellard
    buffer = av_malloc(buffer_size);
799 de6d9b64 Fabrice Bellard
    if (!buffer)
800 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
801 de6d9b64 Fabrice Bellard
802 ae628ec1 Anton Khirnov
    *s = av_mallocz(sizeof(AVIOContext));
803 899681cd Björn Axelsson
    if(!*s) {
804
        av_free(buffer);
805
        return AVERROR(ENOMEM);
806
    }
807
808 e731b8d8 Anton Khirnov
    if (ffio_init_context(*s, buffer, buffer_size,
809 ac9fe33d Gildas Bazin
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
810 004050ee Michael Niedermayer
                      url_read, url_write, url_seek) < 0) {
811 1ea4f593 Fabrice Bellard
        av_free(buffer);
812 899681cd Björn Axelsson
        av_freep(s);
813 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
814 de6d9b64 Fabrice Bellard
    }
815 899681cd Björn Axelsson
    (*s)->is_streamed = h->is_streamed;
816
    (*s)->max_packet_size = max_packet_size;
817 e7e4810a Björn Axelsson
    if(h->prot) {
818 502bdf68 Michael Niedermayer
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
819 bc5c918e Diego Biurrun
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
820 e7e4810a Björn Axelsson
    }
821 de6d9b64 Fabrice Bellard
    return 0;
822
}
823
824 59f65d95 Anton Khirnov
int ffio_set_buf_size(AVIOContext *s, int buf_size)
825 de6d9b64 Fabrice Bellard
{
826 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
827 1ea4f593 Fabrice Bellard
    buffer = av_malloc(buf_size);
828 de6d9b64 Fabrice Bellard
    if (!buffer)
829 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
830 de6d9b64 Fabrice Bellard
831 1ea4f593 Fabrice Bellard
    av_free(s->buffer);
832 de6d9b64 Fabrice Bellard
    s->buffer = buffer;
833
    s->buffer_size = buf_size;
834
    s->buf_ptr = buffer;
835 770d9daf Björn Axelsson
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
836
    return 0;
837
}
838
839 364cacc7 Aurelien Jacobs
#if FF_API_URL_RESETBUF
840 ae628ec1 Anton Khirnov
int url_resetbuf(AVIOContext *s, int flags)
841 08580cb0 Benoit Fouet
#else
842 ae628ec1 Anton Khirnov
static int url_resetbuf(AVIOContext *s, int flags)
843 08580cb0 Benoit Fouet
#endif
844 770d9daf Björn Axelsson
{
845 364cacc7 Aurelien Jacobs
#if FF_API_URL_RESETBUF
846 6fa197e2 Eli Friedman
    if (flags & URL_RDWR)
847 770d9daf Björn Axelsson
        return AVERROR(EINVAL);
848 08580cb0 Benoit Fouet
#else
849
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
850
#endif
851 770d9daf Björn Axelsson
852
    if (flags & URL_WRONLY) {
853
        s->buf_end = s->buffer + s->buffer_size;
854
        s->write_flag = 1;
855
    } else {
856
        s->buf_end = s->buffer;
857
        s->write_flag = 0;
858
    }
859 de6d9b64 Fabrice Bellard
    return 0;
860
}
861
862 f1ef2cd9 Anton Khirnov
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
863 01d91b9b Tomas Härdin
{
864
    int64_t buffer_start;
865
    int buffer_size;
866 496c645d Martin Storsjö
    int overlap, new_size, alloc_size;
867 01d91b9b Tomas Härdin
868
    if (s->write_flag)
869
        return AVERROR(EINVAL);
870
871
    buffer_size = s->buf_end - s->buffer;
872
873
    /* the buffers must touch or overlap */
874
    if ((buffer_start = s->pos - buffer_size) > buf_size)
875
        return AVERROR(EINVAL);
876
877
    overlap = buf_size - buffer_start;
878
    new_size = buf_size + buffer_size - overlap;
879
880 496c645d Martin Storsjö
    alloc_size = FFMAX(s->buffer_size, new_size);
881
    if (alloc_size > buf_size)
882
        if (!(buf = av_realloc(buf, alloc_size)))
883 01d91b9b Tomas Härdin
            return AVERROR(ENOMEM);
884
885 496c645d Martin Storsjö
    if (new_size > buf_size) {
886 01d91b9b Tomas Härdin
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
887
        buf_size = new_size;
888
    }
889
890
    av_free(s->buffer);
891
    s->buf_ptr = s->buffer = buf;
892 496c645d Martin Storsjö
    s->buffer_size = alloc_size;
893
    s->pos = buf_size;
894 01d91b9b Tomas Härdin
    s->buf_end = s->buf_ptr + buf_size;
895
    s->eof_reached = 0;
896
    s->must_flush = 0;
897
898
    return 0;
899
}
900
901 22a3212e Anton Khirnov
int avio_open(AVIOContext **s, const char *filename, int flags)
902 de6d9b64 Fabrice Bellard
{
903
    URLContext *h;
904
    int err;
905
906
    err = url_open(&h, filename, flags);
907
    if (err < 0)
908
        return err;
909
    err = url_fdopen(s, h);
910
    if (err < 0) {
911
        url_close(h);
912
        return err;
913
    }
914
    return 0;
915
}
916
917 22a3212e Anton Khirnov
int avio_close(AVIOContext *s)
918 de6d9b64 Fabrice Bellard
{
919
    URLContext *h = s->opaque;
920 115329f1 Diego Biurrun
921 1ea4f593 Fabrice Bellard
    av_free(s->buffer);
922 899681cd Björn Axelsson
    av_free(s);
923 de6d9b64 Fabrice Bellard
    return url_close(h);
924
}
925
926 ae628ec1 Anton Khirnov
URLContext *url_fileno(AVIOContext *s)
927 de6d9b64 Fabrice Bellard
{
928
    return s->opaque;
929
}
930
931 d9d86e00 Anton Khirnov
int avio_printf(AVIOContext *s, const char *fmt, ...)
932 abba3dd1 Fabrice Bellard
{
933
    va_list ap;
934
    char buf[4096];
935
    int ret;
936
937
    va_start(ap, fmt);
938
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
939
    va_end(ap);
940 77eb5504 Anton Khirnov
    avio_write(s, buf, strlen(buf));
941 abba3dd1 Fabrice Bellard
    return ret;
942
}
943
944 655e45e7 Anton Khirnov
#if FF_API_OLD_AVIO
945 ae628ec1 Anton Khirnov
char *url_fgets(AVIOContext *s, char *buf, int buf_size)
946 abba3dd1 Fabrice Bellard
{
947
    int c;
948
    char *q;
949
950 e5197539 Anton Khirnov
    c = avio_r8(s);
951 66e5b1df Anton Khirnov
    if (s->eof_reached)
952 abba3dd1 Fabrice Bellard
        return NULL;
953
    q = buf;
954
    for(;;) {
955 66e5b1df Anton Khirnov
        if (s->eof_reached || c == '\n')
956 abba3dd1 Fabrice Bellard
            break;
957
        if ((q - buf) < buf_size - 1)
958
            *q++ = c;
959 e5197539 Anton Khirnov
        c = avio_r8(s);
960 abba3dd1 Fabrice Bellard
    }
961
    if (buf_size > 0)
962
        *q = '\0';
963
    return buf;
964
}
965
966 ae628ec1 Anton Khirnov
int url_fget_max_packet_size(AVIOContext *s)
967 abba3dd1 Fabrice Bellard
{
968
    return s->max_packet_size;
969
}
970 e8bb2e24 Anton Khirnov
#endif
971 abba3dd1 Fabrice Bellard
972 ae628ec1 Anton Khirnov
int av_url_read_fpause(AVIOContext *s, int pause)
973 e7e4810a Björn Axelsson
{
974
    if (!s->read_pause)
975
        return AVERROR(ENOSYS);
976 502bdf68 Michael Niedermayer
    return s->read_pause(s->opaque, pause);
977 e7e4810a Björn Axelsson
}
978
979 ae628ec1 Anton Khirnov
int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
980 7f37f568 Diego Biurrun
                          int64_t timestamp, int flags)
981 e7e4810a Björn Axelsson
{
982
    URLContext *h = s->opaque;
983 bc5c918e Diego Biurrun
    int64_t ret;
984 e7e4810a Björn Axelsson
    if (!s->read_seek)
985
        return AVERROR(ENOSYS);
986
    ret = s->read_seek(h, stream_index, timestamp, flags);
987
    if(ret >= 0) {
988 c007c687 Howard Chu
        int64_t pos;
989 e7e4810a Björn Axelsson
        s->buf_ptr = s->buf_end; // Flush buffer
990 c007c687 Howard Chu
        pos = s->seek(h, 0, SEEK_CUR);
991
        if (pos >= 0)
992
            s->pos = pos;
993
        else if (pos != AVERROR(ENOSYS))
994
            ret = pos;
995 e7e4810a Björn Axelsson
    }
996
    return ret;
997
}
998
999 8fa641f8 Loïc Le Loarer
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
1000 b250f9c6 Aurelien Jacobs
 * back to the server even if CONFIG_MUXERS is false. */
1001
#if CONFIG_MUXERS || CONFIG_NETWORK
1002 de6d9b64 Fabrice Bellard
/* buffer handling */
1003 83fddaeb Anton Khirnov
#if FF_API_OLD_AVIO
1004 ae628ec1 Anton Khirnov
int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
1005 de6d9b64 Fabrice Bellard
{
1006 899681cd Björn Axelsson
    int ret;
1007 ae628ec1 Anton Khirnov
    *s = av_mallocz(sizeof(AVIOContext));
1008 899681cd Björn Axelsson
    if(!*s)
1009
        return AVERROR(ENOMEM);
1010 e731b8d8 Anton Khirnov
    ret = ffio_init_context(*s, buf, buf_size,
1011 899681cd Björn Axelsson
                        (flags & URL_WRONLY || flags & URL_RDWR),
1012
                        NULL, NULL, NULL, NULL);
1013
    if(ret != 0)
1014
        av_freep(s);
1015
    return ret;
1016 de6d9b64 Fabrice Bellard
}
1017
1018 ae628ec1 Anton Khirnov
int url_close_buf(AVIOContext *s)
1019 de6d9b64 Fabrice Bellard
{
1020 b7f2fdde Anton Khirnov
    avio_flush(s);
1021 de6d9b64 Fabrice Bellard
    return s->buf_ptr - s->buffer;
1022
}
1023 35f10235 Anton Khirnov
#endif
1024 abba3dd1 Fabrice Bellard
1025
/* output in a dynamic buffer */
1026
1027
typedef struct DynBuffer {
1028
    int pos, size, allocated_size;
1029 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
1030 abba3dd1 Fabrice Bellard
    int io_buffer_size;
1031 0c1a9eda Zdenek Kabelac
    uint8_t io_buffer[1];
1032 abba3dd1 Fabrice Bellard
} DynBuffer;
1033
1034 576ae256 Michael Niedermayer
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
1035 abba3dd1 Fabrice Bellard
{
1036
    DynBuffer *d = opaque;
1037 6bc03a69 Reimar Döffinger
    unsigned new_size, new_allocated_size;
1038 115329f1 Diego Biurrun
1039 abba3dd1 Fabrice Bellard
    /* reallocate buffer if needed */
1040
    new_size = d->pos + buf_size;
1041
    new_allocated_size = d->allocated_size;
1042 568e18b1 Michael Niedermayer
    if(new_size < d->pos || new_size > INT_MAX/2)
1043
        return -1;
1044 abba3dd1 Fabrice Bellard
    while (new_size > new_allocated_size) {
1045
        if (!new_allocated_size)
1046
            new_allocated_size = new_size;
1047
        else
1048 115329f1 Diego Biurrun
            new_allocated_size += new_allocated_size / 2 + 1;
1049 abba3dd1 Fabrice Bellard
    }
1050 115329f1 Diego Biurrun
1051 abba3dd1 Fabrice Bellard
    if (new_allocated_size > d->allocated_size) {
1052 b88ba823 Mark Hills
        d->buffer = av_realloc(d->buffer, new_allocated_size);
1053
        if(d->buffer == NULL)
1054 7ce820ef Reimar Döffinger
             return AVERROR(ENOMEM);
1055 abba3dd1 Fabrice Bellard
        d->allocated_size = new_allocated_size;
1056
    }
1057
    memcpy(d->buffer + d->pos, buf, buf_size);
1058
    d->pos = new_size;
1059
    if (d->pos > d->size)
1060
        d->size = d->pos;
1061 576ae256 Michael Niedermayer
    return buf_size;
1062 abba3dd1 Fabrice Bellard
}
1063
1064 576ae256 Michael Niedermayer
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
1065 abba3dd1 Fabrice Bellard
{
1066
    unsigned char buf1[4];
1067 576ae256 Michael Niedermayer
    int ret;
1068 abba3dd1 Fabrice Bellard
1069
    /* packetized write: output the header */
1070 aa805f94 Reimar Döffinger
    AV_WB32(buf1, buf_size);
1071 576ae256 Michael Niedermayer
    ret= dyn_buf_write(opaque, buf1, 4);
1072
    if(ret < 0)
1073
        return ret;
1074 abba3dd1 Fabrice Bellard
1075
    /* then the data */
1076 576ae256 Michael Niedermayer
    return dyn_buf_write(opaque, buf, buf_size);
1077 abba3dd1 Fabrice Bellard
}
1078
1079 bc5c918e Diego Biurrun
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
1080 abba3dd1 Fabrice Bellard
{
1081
    DynBuffer *d = opaque;
1082
1083
    if (whence == SEEK_CUR)
1084
        offset += d->pos;
1085
    else if (whence == SEEK_END)
1086
        offset += d->size;
1087
    if (offset < 0 || offset > 0x7fffffffLL)
1088
        return -1;
1089
    d->pos = offset;
1090
    return 0;
1091
}
1092
1093 ae628ec1 Anton Khirnov
static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
1094 abba3dd1 Fabrice Bellard
{
1095
    DynBuffer *d;
1096 2bdaf78c Reimar Döffinger
    int ret;
1097 0c5a43d6 Reimar Döffinger
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
1098 115329f1 Diego Biurrun
1099 568e18b1 Michael Niedermayer
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
1100
        return -1;
1101 dc2c2eea Reimar Döffinger
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
1102 abba3dd1 Fabrice Bellard
    if (!d)
1103 0e7ceb24 Reimar Döffinger
        return AVERROR(ENOMEM);
1104 ae628ec1 Anton Khirnov
    *s = av_mallocz(sizeof(AVIOContext));
1105 899681cd Björn Axelsson
    if(!*s) {
1106
        av_free(d);
1107
        return AVERROR(ENOMEM);
1108
    }
1109 abba3dd1 Fabrice Bellard
    d->io_buffer_size = io_buffer_size;
1110 e731b8d8 Anton Khirnov
    ret = ffio_init_context(*s, d->io_buffer, io_buffer_size,
1111 115329f1 Diego Biurrun
                        1, d, NULL,
1112
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
1113 abba3dd1 Fabrice Bellard
                        max_packet_size ? NULL : dyn_buf_seek);
1114
    if (ret == 0) {
1115 899681cd Björn Axelsson
        (*s)->max_packet_size = max_packet_size;
1116
    } else {
1117
        av_free(d);
1118
        av_freep(s);
1119 abba3dd1 Fabrice Bellard
    }
1120
    return ret;
1121
}
1122
1123 ae628ec1 Anton Khirnov
int url_open_dyn_buf(AVIOContext **s)
1124 abba3dd1 Fabrice Bellard
{
1125
    return url_open_dyn_buf_internal(s, 0);
1126
}
1127
1128 ae628ec1 Anton Khirnov
int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
1129 abba3dd1 Fabrice Bellard
{
1130
    if (max_packet_size <= 0)
1131
        return -1;
1132
    return url_open_dyn_buf_internal(s, max_packet_size);
1133
}
1134
1135 ae628ec1 Anton Khirnov
int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
1136 abba3dd1 Fabrice Bellard
{
1137
    DynBuffer *d = s->opaque;
1138
    int size;
1139 de1807bb Josh Allmann
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
1140
    int padding = 0;
1141
1142
    /* don't attempt to pad fixed-size packet buffers */
1143
    if (!s->max_packet_size) {
1144 77eb5504 Anton Khirnov
        avio_write(s, padbuf, sizeof(padbuf));
1145 de1807bb Josh Allmann
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
1146
    }
1147 abba3dd1 Fabrice Bellard
1148 b7f2fdde Anton Khirnov
    avio_flush(s);
1149 abba3dd1 Fabrice Bellard
1150
    *pbuffer = d->buffer;
1151
    size = d->size;
1152
    av_free(d);
1153 899681cd Björn Axelsson
    av_free(s);
1154 de1807bb Josh Allmann
    return size - padding;
1155 abba3dd1 Fabrice Bellard
}
1156 8fa641f8 Loïc Le Loarer
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */