Statistics
| Branch: | Revision:

ffmpeg / libavformat / aviobuf.c @ 7e1720de

History | View | Annotate | Download (19.9 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
#include "avformat.h"
22 5c91a675 Zdenek Kabelac
#include "avio.h"
23 992e78f5 Baptiste Coudurier
#include "crc.h"
24 abba3dd1 Fabrice Bellard
#include <stdarg.h>
25 de6d9b64 Fabrice Bellard
26
#define IO_BUFFER_SIZE 32768
27
28 398f5d3f Michael Niedermayer
static void fill_buffer(ByteIOContext *s);
29
30 de6d9b64 Fabrice Bellard
int init_put_byte(ByteIOContext *s,
31
                  unsigned char *buffer,
32
                  int buffer_size,
33
                  int write_flag,
34
                  void *opaque,
35 0c1a9eda Zdenek Kabelac
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
36 576ae256 Michael Niedermayer
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
37 536ffa59 Wolfram Gloger
                  offset_t (*seek)(void *opaque, offset_t offset, int whence))
38 de6d9b64 Fabrice Bellard
{
39
    s->buffer = buffer;
40
    s->buffer_size = buffer_size;
41
    s->buf_ptr = buffer;
42 770d9daf Björn Axelsson
    url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);
43 de6d9b64 Fabrice Bellard
    s->opaque = opaque;
44
    s->write_packet = write_packet;
45
    s->read_packet = read_packet;
46
    s->seek = seek;
47
    s->pos = 0;
48
    s->must_flush = 0;
49
    s->eof_reached = 0;
50 576ae256 Michael Niedermayer
    s->error = 0;
51 de6d9b64 Fabrice Bellard
    s->is_streamed = 0;
52 abba3dd1 Fabrice Bellard
    s->max_packet_size = 0;
53 ee9f36a8 Michael Niedermayer
    s->update_checksum= NULL;
54 942f3bb5 Michael Niedermayer
    if(!read_packet && !write_flag){
55
        s->pos = buffer_size;
56
        s->buf_end = s->buffer + buffer_size;
57
    }
58 e7e4810a Björn Axelsson
    s->read_play  = NULL;
59
    s->read_pause = NULL;
60
    s->read_seek  = NULL;
61 de6d9b64 Fabrice Bellard
    return 0;
62
}
63 115329f1 Diego Biurrun
64 de6d9b64 Fabrice Bellard
static void flush_buffer(ByteIOContext *s)
65
{
66
    if (s->buf_ptr > s->buffer) {
67 576ae256 Michael Niedermayer
        if (s->write_packet && !s->error){
68
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
69
            if(ret < 0){
70
                s->error = ret;
71
            }
72
        }
73 465e1dad Michael Niedermayer
        if(s->update_checksum){
74 ee9f36a8 Michael Niedermayer
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
75
            s->checksum_ptr= s->buffer;
76
        }
77 de6d9b64 Fabrice Bellard
        s->pos += s->buf_ptr - s->buffer;
78
    }
79
    s->buf_ptr = s->buffer;
80
}
81
82
void put_byte(ByteIOContext *s, int b)
83
{
84
    *(s->buf_ptr)++ = b;
85 115329f1 Diego Biurrun
    if (s->buf_ptr >= s->buf_end)
86 de6d9b64 Fabrice Bellard
        flush_buffer(s);
87
}
88
89 75bdb984 Philip Gladstone
void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
90 de6d9b64 Fabrice Bellard
{
91
    int len;
92
93
    while (size > 0) {
94
        len = (s->buf_end - s->buf_ptr);
95
        if (len > size)
96
            len = size;
97
        memcpy(s->buf_ptr, buf, len);
98
        s->buf_ptr += len;
99
100 115329f1 Diego Biurrun
        if (s->buf_ptr >= s->buf_end)
101 de6d9b64 Fabrice Bellard
            flush_buffer(s);
102
103
        buf += len;
104
        size -= len;
105
    }
106
}
107
108
void put_flush_packet(ByteIOContext *s)
109
{
110
    flush_buffer(s);
111
    s->must_flush = 0;
112
}
113
114
offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence)
115
{
116
    offset_t offset1;
117 899681cd Björn Axelsson
    offset_t pos;
118
119
    if(!s)
120
        return AVERROR(EINVAL);
121
122
    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
123 de6d9b64 Fabrice Bellard
124
    if (whence != SEEK_CUR && whence != SEEK_SET)
125 8fa36ae0 François Revol
        return AVERROR(EINVAL);
126 115329f1 Diego Biurrun
127 f40d1739 Michael Niedermayer
    if (whence == SEEK_CUR) {
128
        offset1 = pos + (s->buf_ptr - s->buffer);
129
        if (offset == 0)
130
            return offset1;
131
        offset += offset1;
132
    }
133
    offset1 = offset - pos;
134
    if (!s->must_flush &&
135
        offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) {
136
        /* can do the seek inside the buffer */
137
        s->buf_ptr = s->buffer + offset1;
138 398f5d3f Michael Niedermayer
    } else if(s->is_streamed && !s->write_flag &&
139
              offset1 >= 0 && offset1 < (s->buf_end - s->buffer) + (1<<16)){
140
        while(s->pos < offset && !s->eof_reached)
141
            fill_buffer(s);
142
        s->buf_ptr = s->buf_end + offset - s->pos;
143 f40d1739 Michael Niedermayer
    } else {
144 8fa36ae0 François Revol
        offset_t res = AVERROR(EPIPE);
145 68fcdbf1 Ronald S. Bultje
146 8fa641f8 Loïc Le Loarer
#if defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK)
147 f40d1739 Michael Niedermayer
        if (s->write_flag) {
148 de6d9b64 Fabrice Bellard
            flush_buffer(s);
149
            s->must_flush = 1;
150 f40d1739 Michael Niedermayer
        } else
151 8fa641f8 Loïc Le Loarer
#endif /* defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK) */
152 f40d1739 Michael Niedermayer
        {
153 de6d9b64 Fabrice Bellard
            s->buf_end = s->buffer;
154
        }
155 f40d1739 Michael Niedermayer
        s->buf_ptr = s->buffer;
156 68fcdbf1 Ronald S. Bultje
        if (!s->seek || (res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
157
            return res;
158 f40d1739 Michael Niedermayer
        s->pos = offset;
159 de6d9b64 Fabrice Bellard
    }
160 f40d1739 Michael Niedermayer
    s->eof_reached = 0;
161 de6d9b64 Fabrice Bellard
    return offset;
162
}
163
164
void url_fskip(ByteIOContext *s, offset_t offset)
165
{
166
    url_fseek(s, offset, SEEK_CUR);
167
}
168
169
offset_t url_ftell(ByteIOContext *s)
170
{
171
    return url_fseek(s, 0, SEEK_CUR);
172
}
173
174 a965c478 Aurelien Jacobs
offset_t url_fsize(ByteIOContext *s)
175
{
176
    offset_t size;
177 115329f1 Diego Biurrun
178 899681cd Björn Axelsson
    if(!s)
179
        return AVERROR(EINVAL);
180
181 a965c478 Aurelien Jacobs
    if (!s->seek)
182 8fa36ae0 François Revol
        return AVERROR(EPIPE);
183 8e287af0 Michael Niedermayer
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
184
    if(size<0){
185 8f42f523 Ronald S. Bultje
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
186
            return size;
187
        size++;
188 1ae2c5f2 Michael Niedermayer
        s->seek(s->opaque, s->pos, SEEK_SET);
189 8e287af0 Michael Niedermayer
    }
190 a965c478 Aurelien Jacobs
    return size;
191
}
192
193 de6d9b64 Fabrice Bellard
int url_feof(ByteIOContext *s)
194
{
195 899681cd Björn Axelsson
    if(!s)
196
        return 0;
197 de6d9b64 Fabrice Bellard
    return s->eof_reached;
198
}
199
200 576ae256 Michael Niedermayer
int url_ferror(ByteIOContext *s)
201
{
202 899681cd Björn Axelsson
    if(!s)
203
        return 0;
204 576ae256 Michael Niedermayer
    return s->error;
205
}
206
207 de6d9b64 Fabrice Bellard
void put_le32(ByteIOContext *s, unsigned int val)
208
{
209
    put_byte(s, val);
210
    put_byte(s, val >> 8);
211
    put_byte(s, val >> 16);
212
    put_byte(s, val >> 24);
213
}
214
215
void put_be32(ByteIOContext *s, unsigned int val)
216
{
217
    put_byte(s, val >> 24);
218
    put_byte(s, val >> 16);
219
    put_byte(s, val >> 8);
220
    put_byte(s, val);
221
}
222
223 3b4b29dc Fabrice Bellard
void put_strz(ByteIOContext *s, const char *str)
224 75bdb984 Philip Gladstone
{
225
    if (str)
226
        put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
227
    else
228
        put_byte(s, 0);
229
}
230
231 0c1a9eda Zdenek Kabelac
void put_le64(ByteIOContext *s, uint64_t val)
232 de6d9b64 Fabrice Bellard
{
233 0c1a9eda Zdenek Kabelac
    put_le32(s, (uint32_t)(val & 0xffffffff));
234
    put_le32(s, (uint32_t)(val >> 32));
235 de6d9b64 Fabrice Bellard
}
236
237 0c1a9eda Zdenek Kabelac
void put_be64(ByteIOContext *s, uint64_t val)
238 de6d9b64 Fabrice Bellard
{
239 0c1a9eda Zdenek Kabelac
    put_be32(s, (uint32_t)(val >> 32));
240
    put_be32(s, (uint32_t)(val & 0xffffffff));
241 de6d9b64 Fabrice Bellard
}
242
243
void put_le16(ByteIOContext *s, unsigned int val)
244
{
245
    put_byte(s, val);
246
    put_byte(s, val >> 8);
247
}
248
249
void put_be16(ByteIOContext *s, unsigned int val)
250
{
251
    put_byte(s, val >> 8);
252
    put_byte(s, val);
253
}
254
255 ea395e8c Aurelien Jacobs
void put_le24(ByteIOContext *s, unsigned int val)
256
{
257
    put_le16(s, val & 0xffff);
258
    put_byte(s, val >> 16);
259
}
260
261 a254c574 Michael Niedermayer
void put_be24(ByteIOContext *s, unsigned int val)
262
{
263
    put_be16(s, val >> 8);
264
    put_byte(s, val);
265
}
266
267 0570bf06 Zdenek Kabelac
void put_tag(ByteIOContext *s, const char *tag)
268 de6d9b64 Fabrice Bellard
{
269
    while (*tag) {
270
        put_byte(s, *tag++);
271
    }
272
}
273
274
/* Input stream */
275
276
static void fill_buffer(ByteIOContext *s)
277
{
278 602dd8f6 Benoit Fouet
    int len=0;
279 de6d9b64 Fabrice Bellard
280 abba3dd1 Fabrice Bellard
    /* no need to do anything if EOF already reached */
281
    if (s->eof_reached)
282
        return;
283 ee9f36a8 Michael Niedermayer
284 465e1dad Michael Niedermayer
    if(s->update_checksum){
285 0f0680c5 Michael Niedermayer
        if(s->buf_end > s->checksum_ptr)
286
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
287 ee9f36a8 Michael Niedermayer
        s->checksum_ptr= s->buffer;
288
    }
289
290 602dd8f6 Benoit Fouet
    if(s->read_packet)
291 2d89f334 Benoit Fouet
        len = s->read_packet(s->opaque, s->buffer, s->buffer_size);
292 abba3dd1 Fabrice Bellard
    if (len <= 0) {
293
        /* do not modify buffer if EOF reached so that a seek back can
294
           be done without rereading data */
295 de6d9b64 Fabrice Bellard
        s->eof_reached = 1;
296 5f097703 Alex Beregszaszi
        if(len<0)
297
            s->error= len;
298 abba3dd1 Fabrice Bellard
    } else {
299
        s->pos += len;
300
        s->buf_ptr = s->buffer;
301
        s->buf_end = s->buffer + len;
302 de6d9b64 Fabrice Bellard
    }
303
}
304
305 992e78f5 Baptiste Coudurier
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len){
306
    return av_crc(av_crc04C11DB7, checksum, buf, len);
307
}
308
309 ee9f36a8 Michael Niedermayer
unsigned long get_checksum(ByteIOContext *s){
310
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
311 465e1dad Michael Niedermayer
    s->update_checksum= NULL;
312 ee9f36a8 Michael Niedermayer
    return s->checksum;
313
}
314
315
void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum){
316
    s->update_checksum= update_checksum;
317 465e1dad Michael Niedermayer
    if(s->update_checksum){
318 ccb15994 Michael Niedermayer
        s->checksum= checksum;
319 465e1dad Michael Niedermayer
        s->checksum_ptr= s->buf_ptr;
320
    }
321 ee9f36a8 Michael Niedermayer
}
322
323 abba3dd1 Fabrice Bellard
/* XXX: put an inline version */
324 de6d9b64 Fabrice Bellard
int get_byte(ByteIOContext *s)
325
{
326
    if (s->buf_ptr < s->buf_end) {
327
        return *s->buf_ptr++;
328
    } else {
329
        fill_buffer(s);
330
        if (s->buf_ptr < s->buf_end)
331
            return *s->buf_ptr++;
332
        else
333
            return 0;
334
    }
335
}
336
337 abba3dd1 Fabrice Bellard
int url_fgetc(ByteIOContext *s)
338
{
339
    if (s->buf_ptr < s->buf_end) {
340
        return *s->buf_ptr++;
341
    } else {
342
        fill_buffer(s);
343
        if (s->buf_ptr < s->buf_end)
344
            return *s->buf_ptr++;
345
        else
346
            return URL_EOF;
347
    }
348
}
349
350 de6d9b64 Fabrice Bellard
int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
351
{
352
    int len, size1;
353
354
    size1 = size;
355
    while (size > 0) {
356
        len = s->buf_end - s->buf_ptr;
357
        if (len > size)
358
            len = size;
359
        if (len == 0) {
360 3e2b6358 Michael Niedermayer
            if(size > s->buffer_size && !s->update_checksum){
361 602dd8f6 Benoit Fouet
                if(s->read_packet)
362 2d89f334 Benoit Fouet
                    len = s->read_packet(s->opaque, buf, size);
363 3e2b6358 Michael Niedermayer
                if (len <= 0) {
364
                    /* do not modify buffer if EOF reached so that a seek back can
365
                    be done without rereading data */
366
                    s->eof_reached = 1;
367
                    if(len<0)
368
                        s->error= len;
369
                    break;
370
                } else {
371
                    s->pos += len;
372
                    size -= len;
373
                    buf += len;
374
                    s->buf_ptr = s->buffer;
375
                    s->buf_end = s->buffer/* + len*/;
376
                }
377
            }else{
378
                fill_buffer(s);
379
                len = s->buf_end - s->buf_ptr;
380
                if (len == 0)
381
                    break;
382
            }
383 de6d9b64 Fabrice Bellard
        } else {
384
            memcpy(buf, s->buf_ptr, len);
385
            buf += len;
386
            s->buf_ptr += len;
387
            size -= len;
388
        }
389
    }
390
    return size1 - size;
391
}
392
393 e15dec10 Leon van Stuivenberg
int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
394
{
395
    int len;
396 115329f1 Diego Biurrun
397 0ecca7a4 Michael Niedermayer
    if(size<0)
398
        return -1;
399 e15dec10 Leon van Stuivenberg
400
    len = s->buf_end - s->buf_ptr;
401
    if (len == 0) {
402
        fill_buffer(s);
403
        len = s->buf_end - s->buf_ptr;
404
    }
405
    if (len > size)
406
        len = size;
407
    memcpy(buf, s->buf_ptr, len);
408
    s->buf_ptr += len;
409
    return len;
410
}
411
412 de6d9b64 Fabrice Bellard
unsigned int get_le16(ByteIOContext *s)
413
{
414
    unsigned int val;
415
    val = get_byte(s);
416
    val |= get_byte(s) << 8;
417
    return val;
418
}
419
420 ea395e8c Aurelien Jacobs
unsigned int get_le24(ByteIOContext *s)
421
{
422
    unsigned int val;
423
    val = get_le16(s);
424
    val |= get_byte(s) << 16;
425
    return val;
426
}
427
428 de6d9b64 Fabrice Bellard
unsigned int get_le32(ByteIOContext *s)
429
{
430
    unsigned int val;
431 a254c574 Michael Niedermayer
    val = get_le16(s);
432
    val |= get_le16(s) << 16;
433 de6d9b64 Fabrice Bellard
    return val;
434
}
435
436 0c1a9eda Zdenek Kabelac
uint64_t get_le64(ByteIOContext *s)
437 de6d9b64 Fabrice Bellard
{
438 0c1a9eda Zdenek Kabelac
    uint64_t val;
439
    val = (uint64_t)get_le32(s);
440
    val |= (uint64_t)get_le32(s) << 32;
441 de6d9b64 Fabrice Bellard
    return val;
442
}
443
444
unsigned int get_be16(ByteIOContext *s)
445
{
446
    unsigned int val;
447
    val = get_byte(s) << 8;
448
    val |= get_byte(s);
449
    return val;
450
}
451
452 a254c574 Michael Niedermayer
unsigned int get_be24(ByteIOContext *s)
453 de6d9b64 Fabrice Bellard
{
454
    unsigned int val;
455 a254c574 Michael Niedermayer
    val = get_be16(s) << 8;
456 de6d9b64 Fabrice Bellard
    val |= get_byte(s);
457
    return val;
458
}
459 a254c574 Michael Niedermayer
unsigned int get_be32(ByteIOContext *s)
460
{
461
    unsigned int val;
462
    val = get_be16(s) << 16;
463
    val |= get_be16(s);
464
    return val;
465
}
466 de6d9b64 Fabrice Bellard
467 3b4b29dc Fabrice Bellard
char *get_strz(ByteIOContext *s, char *buf, int maxlen)
468 75bdb984 Philip Gladstone
{
469
    int i = 0;
470
    char c;
471
472
    while ((c = get_byte(s))) {
473
        if (i < maxlen-1)
474
            buf[i++] = c;
475
    }
476 115329f1 Diego Biurrun
477 75bdb984 Philip Gladstone
    buf[i] = 0; /* Ensure null terminated, but may be truncated */
478
479
    return buf;
480
}
481
482 0c1a9eda Zdenek Kabelac
uint64_t get_be64(ByteIOContext *s)
483 de6d9b64 Fabrice Bellard
{
484 0c1a9eda Zdenek Kabelac
    uint64_t val;
485
    val = (uint64_t)get_be32(s) << 32;
486
    val |= (uint64_t)get_be32(s);
487 de6d9b64 Fabrice Bellard
    return val;
488
}
489
490 7798b42d Kostya Shishkov
uint64_t ff_get_v(ByteIOContext *bc){
491 897d3eef Kostya Shishkov
    uint64_t val = 0;
492
    int tmp;
493
494
    do{
495
        tmp = get_byte(bc);
496
        val= (val<<7) + (tmp&127);
497
    }while(tmp&128);
498
    return val;
499
}
500
501 de6d9b64 Fabrice Bellard
/* link with avio functions */
502
503 a9e35095 Diego Biurrun
#ifdef CONFIG_MUXERS
504 576ae256 Michael Niedermayer
static int url_write_packet(void *opaque, uint8_t *buf, int buf_size)
505 de6d9b64 Fabrice Bellard
{
506
    URLContext *h = opaque;
507 576ae256 Michael Niedermayer
    return url_write(h, buf, buf_size);
508 de6d9b64 Fabrice Bellard
}
509 764ef400 Mike Melanson
#else
510 bb270c08 Diego Biurrun
#define         url_write_packet NULL
511 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
512 de6d9b64 Fabrice Bellard
513 0c1a9eda Zdenek Kabelac
static int url_read_packet(void *opaque, uint8_t *buf, int buf_size)
514 de6d9b64 Fabrice Bellard
{
515
    URLContext *h = opaque;
516
    return url_read(h, buf, buf_size);
517
}
518
519 536ffa59 Wolfram Gloger
static offset_t url_seek_packet(void *opaque, offset_t offset, int whence)
520 de6d9b64 Fabrice Bellard
{
521
    URLContext *h = opaque;
522 53e2f9ca Michael Niedermayer
    return url_seek(h, offset, whence);
523
    //return 0;
524 de6d9b64 Fabrice Bellard
}
525
526 899681cd Björn Axelsson
int url_fdopen(ByteIOContext **s, URLContext *h)
527 de6d9b64 Fabrice Bellard
{
528 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
529 abba3dd1 Fabrice Bellard
    int buffer_size, max_packet_size;
530 de6d9b64 Fabrice Bellard
531 115329f1 Diego Biurrun
532 abba3dd1 Fabrice Bellard
    max_packet_size = url_get_max_packet_size(h);
533
    if (max_packet_size) {
534
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
535
    } else {
536
        buffer_size = IO_BUFFER_SIZE;
537
    }
538 1ea4f593 Fabrice Bellard
    buffer = av_malloc(buffer_size);
539 de6d9b64 Fabrice Bellard
    if (!buffer)
540 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
541 de6d9b64 Fabrice Bellard
542 899681cd Björn Axelsson
    *s = av_mallocz(sizeof(ByteIOContext));
543
    if(!*s) {
544
        av_free(buffer);
545
        return AVERROR(ENOMEM);
546
    }
547
548
    if (init_put_byte(*s, buffer, buffer_size,
549 ac9fe33d Gildas Bazin
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
550 de6d9b64 Fabrice Bellard
                      url_read_packet, url_write_packet, url_seek_packet) < 0) {
551 1ea4f593 Fabrice Bellard
        av_free(buffer);
552 899681cd Björn Axelsson
        av_freep(s);
553 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
554 de6d9b64 Fabrice Bellard
    }
555 899681cd Björn Axelsson
    (*s)->is_streamed = h->is_streamed;
556
    (*s)->max_packet_size = max_packet_size;
557 e7e4810a Björn Axelsson
    if(h->prot) {
558
        (*s)->read_play  = (int (*)(void *))h->prot->url_read_play;
559
        (*s)->read_pause = (int (*)(void *))h->prot->url_read_pause;
560
        (*s)->read_seek  = (int (*)(void *, int, int64_t, int))h->prot->url_read_seek;
561
    }
562 de6d9b64 Fabrice Bellard
    return 0;
563
}
564
565
int url_setbufsize(ByteIOContext *s, int buf_size)
566
{
567 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
568 1ea4f593 Fabrice Bellard
    buffer = av_malloc(buf_size);
569 de6d9b64 Fabrice Bellard
    if (!buffer)
570 8fa36ae0 François Revol
        return AVERROR(ENOMEM);
571 de6d9b64 Fabrice Bellard
572 1ea4f593 Fabrice Bellard
    av_free(s->buffer);
573 de6d9b64 Fabrice Bellard
    s->buffer = buffer;
574
    s->buffer_size = buf_size;
575
    s->buf_ptr = buffer;
576 770d9daf Björn Axelsson
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
577
    return 0;
578
}
579
580
int url_resetbuf(ByteIOContext *s, int flags)
581
{
582
    URLContext *h = s->opaque;
583
    if ((flags & URL_RDWR) || (h && h->flags != flags && !h->flags & URL_RDWR))
584
        return AVERROR(EINVAL);
585
586
    if (flags & URL_WRONLY) {
587
        s->buf_end = s->buffer + s->buffer_size;
588
        s->write_flag = 1;
589
    } else {
590
        s->buf_end = s->buffer;
591
        s->write_flag = 0;
592
    }
593 de6d9b64 Fabrice Bellard
    return 0;
594
}
595
596 899681cd Björn Axelsson
int url_fopen(ByteIOContext **s, const char *filename, int flags)
597 de6d9b64 Fabrice Bellard
{
598
    URLContext *h;
599
    int err;
600
601
    err = url_open(&h, filename, flags);
602
    if (err < 0)
603
        return err;
604
    err = url_fdopen(s, h);
605
    if (err < 0) {
606
        url_close(h);
607
        return err;
608
    }
609
    return 0;
610
}
611
612
int url_fclose(ByteIOContext *s)
613
{
614
    URLContext *h = s->opaque;
615 115329f1 Diego Biurrun
616 1ea4f593 Fabrice Bellard
    av_free(s->buffer);
617 899681cd Björn Axelsson
    av_free(s);
618 de6d9b64 Fabrice Bellard
    return url_close(h);
619
}
620
621
URLContext *url_fileno(ByteIOContext *s)
622
{
623
    return s->opaque;
624
}
625
626 a9e35095 Diego Biurrun
#ifdef CONFIG_MUXERS
627 abba3dd1 Fabrice Bellard
int url_fprintf(ByteIOContext *s, const char *fmt, ...)
628
{
629
    va_list ap;
630
    char buf[4096];
631
    int ret;
632
633
    va_start(ap, fmt);
634
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
635
    va_end(ap);
636
    put_buffer(s, buf, strlen(buf));
637
    return ret;
638
}
639 a9e35095 Diego Biurrun
#endif //CONFIG_MUXERS
640 abba3dd1 Fabrice Bellard
641
char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
642
{
643
    int c;
644
    char *q;
645
646
    c = url_fgetc(s);
647
    if (c == EOF)
648
        return NULL;
649
    q = buf;
650
    for(;;) {
651
        if (c == EOF || c == '\n')
652
            break;
653
        if ((q - buf) < buf_size - 1)
654
            *q++ = c;
655
        c = url_fgetc(s);
656
    }
657
    if (buf_size > 0)
658
        *q = '\0';
659
    return buf;
660
}
661
662
int url_fget_max_packet_size(ByteIOContext *s)
663
{
664
    return s->max_packet_size;
665
}
666
667 e7e4810a Björn Axelsson
int av_url_read_fplay(ByteIOContext *s)
668
{
669
    if (!s->read_play)
670
        return AVERROR(ENOSYS);
671
    return s->read_play(s->opaque);
672
}
673
674
int av_url_read_fpause(ByteIOContext *s)
675
{
676
    if (!s->read_pause)
677
        return AVERROR(ENOSYS);
678
    return s->read_pause(s->opaque);
679
}
680
681
int av_url_read_fseek(ByteIOContext *s,
682
        int stream_index, int64_t timestamp, int flags)
683
{
684
    URLContext *h = s->opaque;
685
    int ret;
686
    if (!s->read_seek)
687
        return AVERROR(ENOSYS);
688
    ret = s->read_seek(h, stream_index, timestamp, flags);
689
    if(ret >= 0) {
690
        s->buf_ptr = s->buf_end; // Flush buffer
691
        s->pos = s->seek(h, 0, SEEK_CUR);
692
    }
693
    return ret;
694
}
695
696 8fa641f8 Loïc Le Loarer
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
697
 * back to the server even if CONFIG_MUXERS is not set. */
698
#if defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK)
699 de6d9b64 Fabrice Bellard
/* buffer handling */
700 899681cd Björn Axelsson
int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags)
701 de6d9b64 Fabrice Bellard
{
702 899681cd Björn Axelsson
    int ret;
703
    *s = av_mallocz(sizeof(ByteIOContext));
704
    if(!*s)
705
        return AVERROR(ENOMEM);
706
    ret = init_put_byte(*s, buf, buf_size,
707
                        (flags & URL_WRONLY || flags & URL_RDWR),
708
                        NULL, NULL, NULL, NULL);
709
    if(ret != 0)
710
        av_freep(s);
711
    return ret;
712 de6d9b64 Fabrice Bellard
}
713
714
int url_close_buf(ByteIOContext *s)
715
{
716 abba3dd1 Fabrice Bellard
    put_flush_packet(s);
717 de6d9b64 Fabrice Bellard
    return s->buf_ptr - s->buffer;
718
}
719 abba3dd1 Fabrice Bellard
720
/* output in a dynamic buffer */
721
722
typedef struct DynBuffer {
723
    int pos, size, allocated_size;
724 0c1a9eda Zdenek Kabelac
    uint8_t *buffer;
725 abba3dd1 Fabrice Bellard
    int io_buffer_size;
726 0c1a9eda Zdenek Kabelac
    uint8_t io_buffer[1];
727 abba3dd1 Fabrice Bellard
} DynBuffer;
728
729 576ae256 Michael Niedermayer
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
730 abba3dd1 Fabrice Bellard
{
731
    DynBuffer *d = opaque;
732
    int new_size, new_allocated_size;
733 115329f1 Diego Biurrun
734 abba3dd1 Fabrice Bellard
    /* reallocate buffer if needed */
735
    new_size = d->pos + buf_size;
736
    new_allocated_size = d->allocated_size;
737 568e18b1 Michael Niedermayer
    if(new_size < d->pos || new_size > INT_MAX/2)
738
        return -1;
739 abba3dd1 Fabrice Bellard
    while (new_size > new_allocated_size) {
740
        if (!new_allocated_size)
741
            new_allocated_size = new_size;
742
        else
743 115329f1 Diego Biurrun
            new_allocated_size += new_allocated_size / 2 + 1;
744 abba3dd1 Fabrice Bellard
    }
745 115329f1 Diego Biurrun
746 abba3dd1 Fabrice Bellard
    if (new_allocated_size > d->allocated_size) {
747 b88ba823 Mark Hills
        d->buffer = av_realloc(d->buffer, new_allocated_size);
748
        if(d->buffer == NULL)
749 576ae256 Michael Niedermayer
             return -1234;
750 abba3dd1 Fabrice Bellard
        d->allocated_size = new_allocated_size;
751
    }
752
    memcpy(d->buffer + d->pos, buf, buf_size);
753
    d->pos = new_size;
754
    if (d->pos > d->size)
755
        d->size = d->pos;
756 576ae256 Michael Niedermayer
    return buf_size;
757 abba3dd1 Fabrice Bellard
}
758
759 576ae256 Michael Niedermayer
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
760 abba3dd1 Fabrice Bellard
{
761
    unsigned char buf1[4];
762 576ae256 Michael Niedermayer
    int ret;
763 abba3dd1 Fabrice Bellard
764
    /* packetized write: output the header */
765
    buf1[0] = (buf_size >> 24);
766
    buf1[1] = (buf_size >> 16);
767
    buf1[2] = (buf_size >> 8);
768
    buf1[3] = (buf_size);
769 576ae256 Michael Niedermayer
    ret= dyn_buf_write(opaque, buf1, 4);
770
    if(ret < 0)
771
        return ret;
772 abba3dd1 Fabrice Bellard
773
    /* then the data */
774 576ae256 Michael Niedermayer
    return dyn_buf_write(opaque, buf, buf_size);
775 abba3dd1 Fabrice Bellard
}
776
777 536ffa59 Wolfram Gloger
static offset_t dyn_buf_seek(void *opaque, offset_t offset, int whence)
778 abba3dd1 Fabrice Bellard
{
779
    DynBuffer *d = opaque;
780
781
    if (whence == SEEK_CUR)
782
        offset += d->pos;
783
    else if (whence == SEEK_END)
784
        offset += d->size;
785
    if (offset < 0 || offset > 0x7fffffffLL)
786
        return -1;
787
    d->pos = offset;
788
    return 0;
789
}
790
791 899681cd Björn Axelsson
static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size)
792 abba3dd1 Fabrice Bellard
{
793
    DynBuffer *d;
794
    int io_buffer_size, ret;
795 115329f1 Diego Biurrun
796
    if (max_packet_size)
797 abba3dd1 Fabrice Bellard
        io_buffer_size = max_packet_size;
798
    else
799
        io_buffer_size = 1024;
800 115329f1 Diego Biurrun
801 568e18b1 Michael Niedermayer
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
802
        return -1;
803 abba3dd1 Fabrice Bellard
    d = av_malloc(sizeof(DynBuffer) + io_buffer_size);
804
    if (!d)
805
        return -1;
806 899681cd Björn Axelsson
    *s = av_mallocz(sizeof(ByteIOContext));
807
    if(!*s) {
808
        av_free(d);
809
        return AVERROR(ENOMEM);
810
    }
811 abba3dd1 Fabrice Bellard
    d->io_buffer_size = io_buffer_size;
812
    d->buffer = NULL;
813
    d->pos = 0;
814
    d->size = 0;
815
    d->allocated_size = 0;
816 899681cd Björn Axelsson
    ret = init_put_byte(*s, d->io_buffer, io_buffer_size,
817 115329f1 Diego Biurrun
                        1, d, NULL,
818
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
819 abba3dd1 Fabrice Bellard
                        max_packet_size ? NULL : dyn_buf_seek);
820
    if (ret == 0) {
821 899681cd Björn Axelsson
        (*s)->max_packet_size = max_packet_size;
822
    } else {
823
        av_free(d);
824
        av_freep(s);
825 abba3dd1 Fabrice Bellard
    }
826
    return ret;
827
}
828
829 899681cd Björn Axelsson
int url_open_dyn_buf(ByteIOContext **s)
830 abba3dd1 Fabrice Bellard
{
831
    return url_open_dyn_buf_internal(s, 0);
832
}
833
834 899681cd Björn Axelsson
int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size)
835 abba3dd1 Fabrice Bellard
{
836
    if (max_packet_size <= 0)
837
        return -1;
838
    return url_open_dyn_buf_internal(s, max_packet_size);
839
}
840
841 0c1a9eda Zdenek Kabelac
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
842 abba3dd1 Fabrice Bellard
{
843
    DynBuffer *d = s->opaque;
844
    int size;
845
846
    put_flush_packet(s);
847
848
    *pbuffer = d->buffer;
849
    size = d->size;
850
    av_free(d);
851 899681cd Björn Axelsson
    av_free(s);
852 abba3dd1 Fabrice Bellard
    return size;
853
}
854 8fa641f8 Loïc Le Loarer
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */