Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 17ee8f66

History | View | Annotate | Download (23 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
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
96
            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 3b4b29dc Fabrice Bellard
void put_strz(ByteIOContext *s, const char *str)
269 75bdb984 Philip Gladstone
{
270
    if (str)
271
        put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
272
    else
273
        put_byte(s, 0);
274
}
275
276 603e5c0b Michael Chinen
int ff_get_v_length(uint64_t val){
277
    int i=1;
278
279
    while(val>>=7)
280
        i++;
281
282
    return i;
283
}
284
285
void ff_put_v(ByteIOContext *bc, uint64_t val){
286
    int i= ff_get_v_length(val);
287
288
    while(--i>0)
289
        put_byte(bc, 128 | (val>>(7*i)));
290
291
    put_byte(bc, val&127);
292
}
293
294 0c1a9eda Zdenek Kabelac
void put_le64(ByteIOContext *s, uint64_t val)
295 de6d9b64 Fabrice Bellard
{
296 0c1a9eda Zdenek Kabelac
    put_le32(s, (uint32_t)(val & 0xffffffff));
297
    put_le32(s, (uint32_t)(val >> 32));
298 de6d9b64 Fabrice Bellard
}
299
300 0c1a9eda Zdenek Kabelac
void put_be64(ByteIOContext *s, uint64_t val)
301 de6d9b64 Fabrice Bellard
{
302 0c1a9eda Zdenek Kabelac
    put_be32(s, (uint32_t)(val >> 32));
303
    put_be32(s, (uint32_t)(val & 0xffffffff));
304 de6d9b64 Fabrice Bellard
}
305
306
void put_le16(ByteIOContext *s, unsigned int val)
307
{
308
    put_byte(s, val);
309
    put_byte(s, val >> 8);
310
}
311
312
void put_be16(ByteIOContext *s, unsigned int val)
313
{
314
    put_byte(s, val >> 8);
315
    put_byte(s, val);
316
}
317
318 ea395e8c Aurelien Jacobs
void put_le24(ByteIOContext *s, unsigned int val)
319
{
320
    put_le16(s, val & 0xffff);
321
    put_byte(s, val >> 16);
322
}
323
324 a254c574 Michael Niedermayer
void put_be24(ByteIOContext *s, unsigned int val)
325
{
326
    put_be16(s, val >> 8);
327
    put_byte(s, val);
328
}
329
330 0570bf06 Zdenek Kabelac
void put_tag(ByteIOContext *s, const char *tag)
331 de6d9b64 Fabrice Bellard
{
332
    while (*tag) {
333
        put_byte(s, *tag++);
334
    }
335
}
336
337
/* Input stream */
338
339
static void fill_buffer(ByteIOContext *s)
340
{
341 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;
342 4589dc5c Michael Niedermayer
    int len= s->buffer_size - (dst - s->buffer);
343 01d91b9b Tomas Härdin
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
344 4589dc5c Michael Niedermayer
345 abba3dd1 Fabrice Bellard
    /* no need to do anything if EOF already reached */
346
    if (s->eof_reached)
347
        return;
348 ee9f36a8 Michael Niedermayer
349 4589dc5c Michael Niedermayer
    if(s->update_checksum && dst == s->buffer){
350 0f0680c5 Michael Niedermayer
        if(s->buf_end > s->checksum_ptr)
351
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
352 ee9f36a8 Michael Niedermayer
        s->checksum_ptr= s->buffer;
353
    }
354
355 01d91b9b Tomas Härdin
    /* make buffer smaller in case it ended up large after probing */
356
    if (s->buffer_size > max_buffer_size) {
357
        url_setbufsize(s, max_buffer_size);
358
359
        s->checksum_ptr = dst = s->buffer;
360
        len = s->buffer_size;
361
    }
362
363 602dd8f6 Benoit Fouet
    if(s->read_packet)
364 4589dc5c Michael Niedermayer
        len = s->read_packet(s->opaque, dst, len);
365
    else
366
        len = 0;
367 abba3dd1 Fabrice Bellard
    if (len <= 0) {
368
        /* do not modify buffer if EOF reached so that a seek back can
369
           be done without rereading data */
370 de6d9b64 Fabrice Bellard
        s->eof_reached = 1;
371 5f097703 Alex Beregszaszi
        if(len<0)
372
            s->error= len;
373 abba3dd1 Fabrice Bellard
    } else {
374
        s->pos += len;
375 4589dc5c Michael Niedermayer
        s->buf_ptr = dst;
376
        s->buf_end = dst + len;
377 de6d9b64 Fabrice Bellard
    }
378
}
379
380 7f37f568 Diego Biurrun
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
381
                                    unsigned int len)
382
{
383 3abe5fbd Aurelien Jacobs
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
384 992e78f5 Baptiste Coudurier
}
385
386 7f37f568 Diego Biurrun
unsigned long get_checksum(ByteIOContext *s)
387
{
388 ee9f36a8 Michael Niedermayer
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
389 465e1dad Michael Niedermayer
    s->update_checksum= NULL;
390 ee9f36a8 Michael Niedermayer
    return s->checksum;
391
}
392
393 7f37f568 Diego Biurrun
void init_checksum(ByteIOContext *s,
394
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
395
                   unsigned long checksum)
396
{
397 ee9f36a8 Michael Niedermayer
    s->update_checksum= update_checksum;
398 465e1dad Michael Niedermayer
    if(s->update_checksum){
399 ccb15994 Michael Niedermayer
        s->checksum= checksum;
400 465e1dad Michael Niedermayer
        s->checksum_ptr= s->buf_ptr;
401
    }
402 ee9f36a8 Michael Niedermayer
}
403
404 abba3dd1 Fabrice Bellard
/* XXX: put an inline version */
405 de6d9b64 Fabrice Bellard
int get_byte(ByteIOContext *s)
406
{
407 3aa13da9 Reimar Döffinger
    if (s->buf_ptr >= s->buf_end)
408 de6d9b64 Fabrice Bellard
        fill_buffer(s);
409 853a0fcc Reimar Döffinger
    if (s->buf_ptr < s->buf_end)
410
        return *s->buf_ptr++;
411
    return 0;
412 de6d9b64 Fabrice Bellard
}
413
414 abba3dd1 Fabrice Bellard
int url_fgetc(ByteIOContext *s)
415
{
416 3aa13da9 Reimar Döffinger
    if (s->buf_ptr >= s->buf_end)
417 abba3dd1 Fabrice Bellard
        fill_buffer(s);
418 853a0fcc Reimar Döffinger
    if (s->buf_ptr < s->buf_end)
419
        return *s->buf_ptr++;
420
    return URL_EOF;
421 abba3dd1 Fabrice Bellard
}
422
423 de6d9b64 Fabrice Bellard
int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
424
{
425
    int len, size1;
426
427
    size1 = size;
428
    while (size > 0) {
429
        len = s->buf_end - s->buf_ptr;
430
        if (len > size)
431
            len = size;
432
        if (len == 0) {
433 3e2b6358 Michael Niedermayer
            if(size > s->buffer_size && !s->update_checksum){
434 602dd8f6 Benoit Fouet
                if(s->read_packet)
435 2d89f334 Benoit Fouet
                    len = s->read_packet(s->opaque, buf, size);
436 3e2b6358 Michael Niedermayer
                if (len <= 0) {
437
                    /* do not modify buffer if EOF reached so that a seek back can
438
                    be done without rereading data */
439
                    s->eof_reached = 1;
440
                    if(len<0)
441
                        s->error= len;
442
                    break;
443
                } else {
444
                    s->pos += len;
445
                    size -= len;
446
                    buf += len;
447
                    s->buf_ptr = s->buffer;
448
                    s->buf_end = s->buffer/* + len*/;
449
                }
450
            }else{
451
                fill_buffer(s);
452
                len = s->buf_end - s->buf_ptr;
453
                if (len == 0)
454
                    break;
455
            }
456 de6d9b64 Fabrice Bellard
        } else {
457
            memcpy(buf, s->buf_ptr, len);
458
            buf += len;
459
            s->buf_ptr += len;
460
            size -= len;
461
        }
462
    }
463 7f7686df Reimar Döffinger
    if (size1 == size) {
464
        if (url_ferror(s)) return url_ferror(s);
465
        if (url_feof(s))   return AVERROR_EOF;
466
    }
467 de6d9b64 Fabrice Bellard
    return size1 - size;
468
}
469
470 e15dec10 Leon van Stuivenberg
int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
471
{
472
    int len;
473 115329f1 Diego Biurrun
474 0ecca7a4 Michael Niedermayer
    if(size<0)
475
        return -1;
476 e15dec10 Leon van Stuivenberg
477
    len = s->buf_end - s->buf_ptr;
478
    if (len == 0) {
479
        fill_buffer(s);
480
        len = s->buf_end - s->buf_ptr;
481
    }
482
    if (len > size)
483
        len = size;
484
    memcpy(buf, s->buf_ptr, len);
485
    s->buf_ptr += len;
486 7f7686df Reimar Döffinger
    if (!len) {
487
        if (url_ferror(s)) return url_ferror(s);
488
        if (url_feof(s))   return AVERROR_EOF;
489
    }
490 e15dec10 Leon van Stuivenberg
    return len;
491
}
492
493 de6d9b64 Fabrice Bellard
unsigned int get_le16(ByteIOContext *s)
494
{
495
    unsigned int val;
496
    val = get_byte(s);
497
    val |= get_byte(s) << 8;
498
    return val;
499
}
500
501 ea395e8c Aurelien Jacobs
unsigned int get_le24(ByteIOContext *s)
502
{
503
    unsigned int val;
504
    val = get_le16(s);
505
    val |= get_byte(s) << 16;
506
    return val;
507
}
508
509 de6d9b64 Fabrice Bellard
unsigned int get_le32(ByteIOContext *s)
510
{
511
    unsigned int val;
512 a254c574 Michael Niedermayer
    val = get_le16(s);
513
    val |= get_le16(s) << 16;
514 de6d9b64 Fabrice Bellard
    return val;
515
}
516
517 0c1a9eda Zdenek Kabelac
uint64_t get_le64(ByteIOContext *s)
518 de6d9b64 Fabrice Bellard
{
519 0c1a9eda Zdenek Kabelac
    uint64_t val;
520
    val = (uint64_t)get_le32(s);
521
    val |= (uint64_t)get_le32(s) << 32;
522 de6d9b64 Fabrice Bellard
    return val;
523
}
524
525
unsigned int get_be16(ByteIOContext *s)
526
{
527
    unsigned int val;
528
    val = get_byte(s) << 8;
529
    val |= get_byte(s);
530
    return val;
531
}
532
533 a254c574 Michael Niedermayer
unsigned int get_be24(ByteIOContext *s)
534 de6d9b64 Fabrice Bellard
{
535
    unsigned int val;
536 a254c574 Michael Niedermayer
    val = get_be16(s) << 8;
537 de6d9b64 Fabrice Bellard
    val |= get_byte(s);
538
    return val;
539
}
540 a254c574 Michael Niedermayer
unsigned int get_be32(ByteIOContext *s)
541
{
542
    unsigned int val;
543
    val = get_be16(s) << 16;
544
    val |= get_be16(s);
545
    return val;
546
}
547 de6d9b64 Fabrice Bellard
548 3b4b29dc Fabrice Bellard
char *get_strz(ByteIOContext *s, char *buf, int maxlen)
549 75bdb984 Philip Gladstone
{
550
    int i = 0;
551
    char c;
552
553
    while ((c = get_byte(s))) {
554
        if (i < maxlen-1)
555
            buf[i++] = c;
556
    }
557 115329f1 Diego Biurrun
558 75bdb984 Philip Gladstone
    buf[i] = 0; /* Ensure null terminated, but may be truncated */
559
560
    return buf;
561
}
562
563 a43416a5 Aurelien Jacobs
int ff_get_line(ByteIOContext *s, char *buf, int maxlen)
564 7c892951 Aurelien Jacobs
{
565
    int i = 0;
566
    char c;
567
568
    do {
569
        c = get_byte(s);
570 a43416a5 Aurelien Jacobs
        if (c && i < maxlen-1)
571 7c892951 Aurelien Jacobs
            buf[i++] = c;
572
    } while (c != '\n' && c);
573
574
    buf[i] = 0;
575 a43416a5 Aurelien Jacobs
    return i;
576 7c892951 Aurelien Jacobs
}
577
578 0c1a9eda Zdenek Kabelac
uint64_t get_be64(ByteIOContext *s)
579 de6d9b64 Fabrice Bellard
{
580 0c1a9eda Zdenek Kabelac
    uint64_t val;
581
    val = (uint64_t)get_be32(s) << 32;
582
    val |= (uint64_t)get_be32(s);
583 de6d9b64 Fabrice Bellard
    return val;
584
}
585
586 7798b42d Kostya Shishkov
uint64_t ff_get_v(ByteIOContext *bc){
587 897d3eef Kostya Shishkov
    uint64_t val = 0;
588
    int tmp;
589
590
    do{
591
        tmp = get_byte(bc);
592
        val= (val<<7) + (tmp&127);
593
    }while(tmp&128);
594
    return val;
595
}
596
597 899681cd Björn Axelsson
int url_fdopen(ByteIOContext **s, URLContext *h)
598 de6d9b64 Fabrice Bellard
{
599 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
600 abba3dd1 Fabrice Bellard
    int buffer_size, max_packet_size;
601 de6d9b64 Fabrice Bellard
602 abba3dd1 Fabrice Bellard
    max_packet_size = url_get_max_packet_size(h);
603
    if (max_packet_size) {
604
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
605
    } else {
606
        buffer_size = IO_BUFFER_SIZE;
607
    }
608 1ea4f593 Fabrice Bellard
    buffer = av_malloc(buffer_size);
609 de6d9b64 Fabrice Bellard
    if (!buffer)
610 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
611 de6d9b64 Fabrice Bellard
612 899681cd Björn Axelsson
    *s = av_mallocz(sizeof(ByteIOContext));
613
    if(!*s) {
614
        av_free(buffer);
615
        return AVERROR(ENOMEM);
616
    }
617
618
    if (init_put_byte(*s, buffer, buffer_size,
619 ac9fe33d Gildas Bazin
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
620 004050ee Michael Niedermayer
                      url_read, url_write, url_seek) < 0) {
621 1ea4f593 Fabrice Bellard
        av_free(buffer);
622 899681cd Björn Axelsson
        av_freep(s);
623 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
624 de6d9b64 Fabrice Bellard
    }
625 899681cd Björn Axelsson
    (*s)->is_streamed = h->is_streamed;
626
    (*s)->max_packet_size = max_packet_size;
627 e7e4810a Björn Axelsson
    if(h->prot) {
628 502bdf68 Michael Niedermayer
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
629 bc5c918e Diego Biurrun
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
630 e7e4810a Björn Axelsson
    }
631 de6d9b64 Fabrice Bellard
    return 0;
632
}
633
634
int url_setbufsize(ByteIOContext *s, int buf_size)
635
{
636 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
637 1ea4f593 Fabrice Bellard
    buffer = av_malloc(buf_size);
638 de6d9b64 Fabrice Bellard
    if (!buffer)
639 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
640 de6d9b64 Fabrice Bellard
641 1ea4f593 Fabrice Bellard
    av_free(s->buffer);
642 de6d9b64 Fabrice Bellard
    s->buffer = buffer;
643
    s->buffer_size = buf_size;
644
    s->buf_ptr = buffer;
645 770d9daf Björn Axelsson
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
646
    return 0;
647
}
648
649 364cacc7 Aurelien Jacobs
#if FF_API_URL_RESETBUF
650 770d9daf Björn Axelsson
int url_resetbuf(ByteIOContext *s, int flags)
651 08580cb0 Benoit Fouet
#else
652
static int url_resetbuf(ByteIOContext *s, int flags)
653
#endif
654 770d9daf Björn Axelsson
{
655 364cacc7 Aurelien Jacobs
#if FF_API_URL_RESETBUF
656 6fa197e2 Eli Friedman
    if (flags & URL_RDWR)
657 770d9daf Björn Axelsson
        return AVERROR(EINVAL);
658 08580cb0 Benoit Fouet
#else
659
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
660
#endif
661 770d9daf Björn Axelsson
662
    if (flags & URL_WRONLY) {
663
        s->buf_end = s->buffer + s->buffer_size;
664
        s->write_flag = 1;
665
    } else {
666
        s->buf_end = s->buffer;
667
        s->write_flag = 0;
668
    }
669 de6d9b64 Fabrice Bellard
    return 0;
670
}
671
672 01d91b9b Tomas Härdin
int ff_rewind_with_probe_data(ByteIOContext *s, unsigned char *buf, int buf_size)
673
{
674
    int64_t buffer_start;
675
    int buffer_size;
676 496c645d Martin Storsjö
    int overlap, new_size, alloc_size;
677 01d91b9b Tomas Härdin
678
    if (s->write_flag)
679
        return AVERROR(EINVAL);
680
681
    buffer_size = s->buf_end - s->buffer;
682
683
    /* the buffers must touch or overlap */
684
    if ((buffer_start = s->pos - buffer_size) > buf_size)
685
        return AVERROR(EINVAL);
686
687
    overlap = buf_size - buffer_start;
688
    new_size = buf_size + buffer_size - overlap;
689
690 496c645d Martin Storsjö
    alloc_size = FFMAX(s->buffer_size, new_size);
691
    if (alloc_size > buf_size)
692
        if (!(buf = av_realloc(buf, alloc_size)))
693 01d91b9b Tomas Härdin
            return AVERROR(ENOMEM);
694
695 496c645d Martin Storsjö
    if (new_size > buf_size) {
696 01d91b9b Tomas Härdin
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
697
        buf_size = new_size;
698
    }
699
700
    av_free(s->buffer);
701
    s->buf_ptr = s->buffer = buf;
702 496c645d Martin Storsjö
    s->buffer_size = alloc_size;
703
    s->pos = buf_size;
704 01d91b9b Tomas Härdin
    s->buf_end = s->buf_ptr + buf_size;
705
    s->eof_reached = 0;
706
    s->must_flush = 0;
707
708
    return 0;
709
}
710
711 899681cd Björn Axelsson
int url_fopen(ByteIOContext **s, const char *filename, int flags)
712 de6d9b64 Fabrice Bellard
{
713
    URLContext *h;
714
    int err;
715
716
    err = url_open(&h, filename, flags);
717
    if (err < 0)
718
        return err;
719
    err = url_fdopen(s, h);
720
    if (err < 0) {
721
        url_close(h);
722
        return err;
723
    }
724
    return 0;
725
}
726
727
int url_fclose(ByteIOContext *s)
728
{
729
    URLContext *h = s->opaque;
730 115329f1 Diego Biurrun
731 1ea4f593 Fabrice Bellard
    av_free(s->buffer);
732 899681cd Björn Axelsson
    av_free(s);
733 de6d9b64 Fabrice Bellard
    return url_close(h);
734
}
735
736
URLContext *url_fileno(ByteIOContext *s)
737
{
738
    return s->opaque;
739
}
740
741 b250f9c6 Aurelien Jacobs
#if CONFIG_MUXERS
742 abba3dd1 Fabrice Bellard
int url_fprintf(ByteIOContext *s, const char *fmt, ...)
743
{
744
    va_list ap;
745
    char buf[4096];
746
    int ret;
747
748
    va_start(ap, fmt);
749
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
750
    va_end(ap);
751
    put_buffer(s, buf, strlen(buf));
752
    return ret;
753
}
754 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
755 abba3dd1 Fabrice Bellard
756
char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
757
{
758
    int c;
759
    char *q;
760
761
    c = url_fgetc(s);
762
    if (c == EOF)
763
        return NULL;
764
    q = buf;
765
    for(;;) {
766
        if (c == EOF || c == '\n')
767
            break;
768
        if ((q - buf) < buf_size - 1)
769
            *q++ = c;
770
        c = url_fgetc(s);
771
    }
772
    if (buf_size > 0)
773
        *q = '\0';
774
    return buf;
775
}
776
777
int url_fget_max_packet_size(ByteIOContext *s)
778
{
779
    return s->max_packet_size;
780
}
781
782 502bdf68 Michael Niedermayer
int av_url_read_fpause(ByteIOContext *s, int pause)
783 e7e4810a Björn Axelsson
{
784
    if (!s->read_pause)
785
        return AVERROR(ENOSYS);
786 502bdf68 Michael Niedermayer
    return s->read_pause(s->opaque, pause);
787 e7e4810a Björn Axelsson
}
788
789 7f37f568 Diego Biurrun
int64_t av_url_read_fseek(ByteIOContext *s, int stream_index,
790
                          int64_t timestamp, int flags)
791 e7e4810a Björn Axelsson
{
792
    URLContext *h = s->opaque;
793 bc5c918e Diego Biurrun
    int64_t ret;
794 e7e4810a Björn Axelsson
    if (!s->read_seek)
795
        return AVERROR(ENOSYS);
796
    ret = s->read_seek(h, stream_index, timestamp, flags);
797
    if(ret >= 0) {
798 c007c687 Howard Chu
        int64_t pos;
799 e7e4810a Björn Axelsson
        s->buf_ptr = s->buf_end; // Flush buffer
800 c007c687 Howard Chu
        pos = s->seek(h, 0, SEEK_CUR);
801
        if (pos >= 0)
802
            s->pos = pos;
803
        else if (pos != AVERROR(ENOSYS))
804
            ret = pos;
805 e7e4810a Björn Axelsson
    }
806
    return ret;
807
}
808
809 8fa641f8 Loïc Le Loarer
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
810 b250f9c6 Aurelien Jacobs
 * back to the server even if CONFIG_MUXERS is false. */
811
#if CONFIG_MUXERS || CONFIG_NETWORK
812 de6d9b64 Fabrice Bellard
/* buffer handling */
813 899681cd Björn Axelsson
int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags)
814 de6d9b64 Fabrice Bellard
{
815 899681cd Björn Axelsson
    int ret;
816
    *s = av_mallocz(sizeof(ByteIOContext));
817
    if(!*s)
818
        return AVERROR(ENOMEM);
819
    ret = init_put_byte(*s, buf, buf_size,
820
                        (flags & URL_WRONLY || flags & URL_RDWR),
821
                        NULL, NULL, NULL, NULL);
822
    if(ret != 0)
823
        av_freep(s);
824
    return ret;
825 de6d9b64 Fabrice Bellard
}
826
827
int url_close_buf(ByteIOContext *s)
828
{
829 abba3dd1 Fabrice Bellard
    put_flush_packet(s);
830 de6d9b64 Fabrice Bellard
    return s->buf_ptr - s->buffer;
831
}
832 abba3dd1 Fabrice Bellard
833
/* output in a dynamic buffer */
834
835
typedef struct DynBuffer {
836
    int pos, size, allocated_size;
837 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
838 abba3dd1 Fabrice Bellard
    int io_buffer_size;
839 0c1a9eda Zdenek Kabelac
    uint8_t io_buffer[1];
840 abba3dd1 Fabrice Bellard
} DynBuffer;
841
842 576ae256 Michael Niedermayer
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
843 abba3dd1 Fabrice Bellard
{
844
    DynBuffer *d = opaque;
845 6bc03a69 Reimar Döffinger
    unsigned new_size, new_allocated_size;
846 115329f1 Diego Biurrun
847 abba3dd1 Fabrice Bellard
    /* reallocate buffer if needed */
848
    new_size = d->pos + buf_size;
849
    new_allocated_size = d->allocated_size;
850 568e18b1 Michael Niedermayer
    if(new_size < d->pos || new_size > INT_MAX/2)
851
        return -1;
852 abba3dd1 Fabrice Bellard
    while (new_size > new_allocated_size) {
853
        if (!new_allocated_size)
854
            new_allocated_size = new_size;
855
        else
856 115329f1 Diego Biurrun
            new_allocated_size += new_allocated_size / 2 + 1;
857 abba3dd1 Fabrice Bellard
    }
858 115329f1 Diego Biurrun
859 abba3dd1 Fabrice Bellard
    if (new_allocated_size > d->allocated_size) {
860 b88ba823 Mark Hills
        d->buffer = av_realloc(d->buffer, new_allocated_size);
861
        if(d->buffer == NULL)
862 7ce820ef Reimar Döffinger
             return AVERROR(ENOMEM);
863 abba3dd1 Fabrice Bellard
        d->allocated_size = new_allocated_size;
864
    }
865
    memcpy(d->buffer + d->pos, buf, buf_size);
866
    d->pos = new_size;
867
    if (d->pos > d->size)
868
        d->size = d->pos;
869 576ae256 Michael Niedermayer
    return buf_size;
870 abba3dd1 Fabrice Bellard
}
871
872 576ae256 Michael Niedermayer
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
873 abba3dd1 Fabrice Bellard
{
874
    unsigned char buf1[4];
875 576ae256 Michael Niedermayer
    int ret;
876 abba3dd1 Fabrice Bellard
877
    /* packetized write: output the header */
878 aa805f94 Reimar Döffinger
    AV_WB32(buf1, buf_size);
879 576ae256 Michael Niedermayer
    ret= dyn_buf_write(opaque, buf1, 4);
880
    if(ret < 0)
881
        return ret;
882 abba3dd1 Fabrice Bellard
883
    /* then the data */
884 576ae256 Michael Niedermayer
    return dyn_buf_write(opaque, buf, buf_size);
885 abba3dd1 Fabrice Bellard
}
886
887 bc5c918e Diego Biurrun
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
888 abba3dd1 Fabrice Bellard
{
889
    DynBuffer *d = opaque;
890
891
    if (whence == SEEK_CUR)
892
        offset += d->pos;
893
    else if (whence == SEEK_END)
894
        offset += d->size;
895
    if (offset < 0 || offset > 0x7fffffffLL)
896
        return -1;
897
    d->pos = offset;
898
    return 0;
899
}
900
901 899681cd Björn Axelsson
static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size)
902 abba3dd1 Fabrice Bellard
{
903
    DynBuffer *d;
904 2bdaf78c Reimar Döffinger
    int ret;
905 0c5a43d6 Reimar Döffinger
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
906 115329f1 Diego Biurrun
907 568e18b1 Michael Niedermayer
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
908
        return -1;
909 dc2c2eea Reimar Döffinger
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
910 abba3dd1 Fabrice Bellard
    if (!d)
911 0e7ceb24 Reimar Döffinger
        return AVERROR(ENOMEM);
912 899681cd Björn Axelsson
    *s = av_mallocz(sizeof(ByteIOContext));
913
    if(!*s) {
914
        av_free(d);
915
        return AVERROR(ENOMEM);
916
    }
917 abba3dd1 Fabrice Bellard
    d->io_buffer_size = io_buffer_size;
918 899681cd Björn Axelsson
    ret = init_put_byte(*s, d->io_buffer, io_buffer_size,
919 115329f1 Diego Biurrun
                        1, d, NULL,
920
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
921 abba3dd1 Fabrice Bellard
                        max_packet_size ? NULL : dyn_buf_seek);
922
    if (ret == 0) {
923 899681cd Björn Axelsson
        (*s)->max_packet_size = max_packet_size;
924
    } else {
925
        av_free(d);
926
        av_freep(s);
927 abba3dd1 Fabrice Bellard
    }
928
    return ret;
929
}
930
931 899681cd Björn Axelsson
int url_open_dyn_buf(ByteIOContext **s)
932 abba3dd1 Fabrice Bellard
{
933
    return url_open_dyn_buf_internal(s, 0);
934
}
935
936 899681cd Björn Axelsson
int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size)
937 abba3dd1 Fabrice Bellard
{
938
    if (max_packet_size <= 0)
939
        return -1;
940
    return url_open_dyn_buf_internal(s, max_packet_size);
941
}
942
943 0c1a9eda Zdenek Kabelac
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
944 abba3dd1 Fabrice Bellard
{
945
    DynBuffer *d = s->opaque;
946
    int size;
947 de1807bb Josh Allmann
    static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
948
    int padding = 0;
949
950
    /* don't attempt to pad fixed-size packet buffers */
951
    if (!s->max_packet_size) {
952
        put_buffer(s, padbuf, sizeof(padbuf));
953
        padding = FF_INPUT_BUFFER_PADDING_SIZE;
954
    }
955 abba3dd1 Fabrice Bellard
956
    put_flush_packet(s);
957
958
    *pbuffer = d->buffer;
959
    size = d->size;
960
    av_free(d);
961 899681cd Björn Axelsson
    av_free(s);
962 de1807bb Josh Allmann
    return size - padding;
963 abba3dd1 Fabrice Bellard
}
964 8fa641f8 Loïc Le Loarer
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */