Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 93b78d12

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