aviobuf.c 20.4 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * Buffered I/O for ffmpeg system
3
 * Copyright (c) 2000,2001 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellard's avatar
Fabrice Bellard committed
20
 */
21 22

#include "libavutil/crc.h"
23
#include "libavutil/intreadwrite.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
24
#include "avformat.h"
25
#include "avio.h"
26
#include <stdarg.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
27 28 29

#define IO_BUFFER_SIZE 32768

30
static void fill_buffer(ByteIOContext *s);
31 32 33
#if LIBAVFORMAT_VERSION_MAJOR >= 53
static int url_resetbuf(ByteIOContext *s, int flags);
#endif
34

Fabrice Bellard's avatar
Fabrice Bellard committed
35 36 37 38 39
int init_put_byte(ByteIOContext *s,
                  unsigned char *buffer,
                  int buffer_size,
                  int write_flag,
                  void *opaque,
40
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
41
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
42
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
Fabrice Bellard's avatar
Fabrice Bellard committed
43 44 45 46 47
{
    s->buffer = buffer;
    s->buffer_size = buffer_size;
    s->buf_ptr = buffer;
    s->opaque = opaque;
48
    url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);
Fabrice Bellard's avatar
Fabrice Bellard committed
49 50 51 52 53 54
    s->write_packet = write_packet;
    s->read_packet = read_packet;
    s->seek = seek;
    s->pos = 0;
    s->must_flush = 0;
    s->eof_reached = 0;
55
    s->error = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
56
    s->is_streamed = 0;
57
    s->max_packet_size = 0;
58
    s->update_checksum= NULL;
59 60 61 62
    if(!read_packet && !write_flag){
        s->pos = buffer_size;
        s->buf_end = s->buffer + buffer_size;
    }
63 64
    s->read_pause = NULL;
    s->read_seek  = NULL;
Fabrice Bellard's avatar
Fabrice Bellard committed
65 66
    return 0;
}
67

68 69 70 71 72 73 74
ByteIOContext *av_alloc_put_byte(
                  unsigned char *buffer,
                  int buffer_size,
                  int write_flag,
                  void *opaque,
                  int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
                  int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
Diego Biurrun's avatar
Diego Biurrun committed
75 76
                  int64_t (*seek)(void *opaque, int64_t offset, int whence))
{
77 78 79 80 81 82
    ByteIOContext *s = av_mallocz(sizeof(ByteIOContext));
    init_put_byte(s, buffer, buffer_size, write_flag, opaque,
                  read_packet, write_packet, seek);
    return s;
}

Fabrice Bellard's avatar
Fabrice Bellard committed
83 84 85
static void flush_buffer(ByteIOContext *s)
{
    if (s->buf_ptr > s->buffer) {
86 87 88 89 90 91
        if (s->write_packet && !s->error){
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
            if(ret < 0){
                s->error = ret;
            }
        }
92
        if(s->update_checksum){
93 94 95
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
            s->checksum_ptr= s->buffer;
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
96 97 98 99 100 101 102 103
        s->pos += s->buf_ptr - s->buffer;
    }
    s->buf_ptr = s->buffer;
}

void put_byte(ByteIOContext *s, int b)
{
    *(s->buf_ptr)++ = b;
104
    if (s->buf_ptr >= s->buf_end)
Fabrice Bellard's avatar
Fabrice Bellard committed
105 106 107
        flush_buffer(s);
}

108
void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
109 110
{
    while (size > 0) {
111
        int len = FFMIN(s->buf_end - s->buf_ptr, size);
Fabrice Bellard's avatar
Fabrice Bellard committed
112 113 114
        memcpy(s->buf_ptr, buf, len);
        s->buf_ptr += len;

115
        if (s->buf_ptr >= s->buf_end)
Fabrice Bellard's avatar
Fabrice Bellard committed
116 117 118 119 120 121 122 123 124 125 126 127 128
            flush_buffer(s);

        buf += len;
        size -= len;
    }
}

void put_flush_packet(ByteIOContext *s)
{
    flush_buffer(s);
    s->must_flush = 0;
}

129
int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence)
Fabrice Bellard's avatar
Fabrice Bellard committed
130
{
131 132
    int64_t offset1;
    int64_t pos;
133 134 135 136 137

    if(!s)
        return AVERROR(EINVAL);

    pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
Fabrice Bellard's avatar
Fabrice Bellard committed
138 139

    if (whence != SEEK_CUR && whence != SEEK_SET)
140
        return AVERROR(EINVAL);
141

142 143 144 145 146 147 148 149
    if (whence == SEEK_CUR) {
        offset1 = pos + (s->buf_ptr - s->buffer);
        if (offset == 0)
            return offset1;
        offset += offset1;
    }
    offset1 = offset - pos;
    if (!s->must_flush &&
150
        offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
151 152
        /* can do the seek inside the buffer */
        s->buf_ptr = s->buffer + offset1;
153 154 155 156
    } else if(s->is_streamed && !s->write_flag &&
              offset1 >= 0 && offset1 < (s->buf_end - s->buffer) + (1<<16)){
        while(s->pos < offset && !s->eof_reached)
            fill_buffer(s);
157 158
        if (s->eof_reached)
            return AVERROR(EPIPE);
159
        s->buf_ptr = s->buf_end + offset - s->pos;
160
    } else {
161
        int64_t res = AVERROR(EPIPE);
162

163
#if CONFIG_MUXERS || CONFIG_NETWORK
164
        if (s->write_flag) {
Fabrice Bellard's avatar
Fabrice Bellard committed
165 166 167
            flush_buffer(s);
            s->must_flush = 1;
        }
168
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */
169 170
        if (!s->seek || (res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
            return res;
171 172 173
        if (!s->write_flag)
            s->buf_end = s->buffer;
        s->buf_ptr = s->buffer;
174
        s->pos = offset;
Fabrice Bellard's avatar
Fabrice Bellard committed
175
    }
176
    s->eof_reached = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
177 178 179
    return offset;
}

180
void url_fskip(ByteIOContext *s, int64_t offset)
Fabrice Bellard's avatar
Fabrice Bellard committed
181 182 183 184
{
    url_fseek(s, offset, SEEK_CUR);
}

185
int64_t url_ftell(ByteIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
186 187 188 189
{
    return url_fseek(s, 0, SEEK_CUR);
}

190
int64_t url_fsize(ByteIOContext *s)
191
{
192
    int64_t size;
193

194 195 196
    if(!s)
        return AVERROR(EINVAL);

197
    if (!s->seek)
198
        return AVERROR(EPIPE);
199 200
    size = s->seek(s->opaque, 0, AVSEEK_SIZE);
    if(size<0){
201 202 203
        if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
            return size;
        size++;
204
        s->seek(s->opaque, s->pos, SEEK_SET);
205
    }
206 207 208
    return size;
}

Fabrice Bellard's avatar
Fabrice Bellard committed
209 210
int url_feof(ByteIOContext *s)
{
211 212
    if(!s)
        return 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
213 214 215
    return s->eof_reached;
}

216 217
int url_ferror(ByteIOContext *s)
{
218 219
    if(!s)
        return 0;
220 221 222
    return s->error;
}

Fabrice Bellard's avatar
Fabrice Bellard committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
void put_le32(ByteIOContext *s, unsigned int val)
{
    put_byte(s, val);
    put_byte(s, val >> 8);
    put_byte(s, val >> 16);
    put_byte(s, val >> 24);
}

void put_be32(ByteIOContext *s, unsigned int val)
{
    put_byte(s, val >> 24);
    put_byte(s, val >> 16);
    put_byte(s, val >> 8);
    put_byte(s, val);
}

239
void put_strz(ByteIOContext *s, const char *str)
240 241 242 243 244 245 246
{
    if (str)
        put_buffer(s, (const unsigned char *) str, strlen(str) + 1);
    else
        put_byte(s, 0);
}

247
void put_le64(ByteIOContext *s, uint64_t val)
Fabrice Bellard's avatar
Fabrice Bellard committed
248
{
249 250
    put_le32(s, (uint32_t)(val & 0xffffffff));
    put_le32(s, (uint32_t)(val >> 32));
Fabrice Bellard's avatar
Fabrice Bellard committed
251 252
}

253
void put_be64(ByteIOContext *s, uint64_t val)
Fabrice Bellard's avatar
Fabrice Bellard committed
254
{
255 256
    put_be32(s, (uint32_t)(val >> 32));
    put_be32(s, (uint32_t)(val & 0xffffffff));
Fabrice Bellard's avatar
Fabrice Bellard committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270
}

void put_le16(ByteIOContext *s, unsigned int val)
{
    put_byte(s, val);
    put_byte(s, val >> 8);
}

void put_be16(ByteIOContext *s, unsigned int val)
{
    put_byte(s, val >> 8);
    put_byte(s, val);
}

271 272 273 274 275 276
void put_le24(ByteIOContext *s, unsigned int val)
{
    put_le16(s, val & 0xffff);
    put_byte(s, val >> 16);
}

277 278 279 280 281 282
void put_be24(ByteIOContext *s, unsigned int val)
{
    put_be16(s, val >> 8);
    put_byte(s, val);
}

283
void put_tag(ByteIOContext *s, const char *tag)
Fabrice Bellard's avatar
Fabrice Bellard committed
284 285 286 287 288 289 290 291 292 293
{
    while (*tag) {
        put_byte(s, *tag++);
    }
}

/* Input stream */

static void fill_buffer(ByteIOContext *s)
{
294
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
295 296 297
    int len= s->buffer_size - (dst - s->buffer);

    assert(s->buf_ptr == s->buf_end);
Fabrice Bellard's avatar
Fabrice Bellard committed
298

299 300 301
    /* no need to do anything if EOF already reached */
    if (s->eof_reached)
        return;
302

303
    if(s->update_checksum && dst == s->buffer){
Michael Niedermayer's avatar
Michael Niedermayer committed
304 305
        if(s->buf_end > s->checksum_ptr)
            s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
306 307 308
        s->checksum_ptr= s->buffer;
    }

309
    if(s->read_packet)
310 311 312
        len = s->read_packet(s->opaque, dst, len);
    else
        len = 0;
313 314 315
    if (len <= 0) {
        /* do not modify buffer if EOF reached so that a seek back can
           be done without rereading data */
Fabrice Bellard's avatar
Fabrice Bellard committed
316
        s->eof_reached = 1;
317 318
        if(len<0)
            s->error= len;
319 320
    } else {
        s->pos += len;
321 322
        s->buf_ptr = dst;
        s->buf_end = dst + len;
Fabrice Bellard's avatar
Fabrice Bellard committed
323 324 325
    }
}

Diego Biurrun's avatar
Diego Biurrun committed
326 327 328
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
                                    unsigned int len)
{
Aurelien Jacobs's avatar
Aurelien Jacobs committed
329
    return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
330 331
}

Diego Biurrun's avatar
Diego Biurrun committed
332 333
unsigned long get_checksum(ByteIOContext *s)
{
334
    s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
335
    s->update_checksum= NULL;
336 337 338
    return s->checksum;
}

Diego Biurrun's avatar
Diego Biurrun committed
339 340 341 342
void init_checksum(ByteIOContext *s,
                   unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
                   unsigned long checksum)
{
343
    s->update_checksum= update_checksum;
344
    if(s->update_checksum){
345
        s->checksum= checksum;
346 347
        s->checksum_ptr= s->buf_ptr;
    }
348 349
}

350
/* XXX: put an inline version */
Fabrice Bellard's avatar
Fabrice Bellard committed
351 352 353 354 355 356 357 358 359 360 361 362 363
int get_byte(ByteIOContext *s)
{
    if (s->buf_ptr < s->buf_end) {
        return *s->buf_ptr++;
    } else {
        fill_buffer(s);
        if (s->buf_ptr < s->buf_end)
            return *s->buf_ptr++;
        else
            return 0;
    }
}

364 365 366 367 368 369 370 371 372 373 374 375 376
int url_fgetc(ByteIOContext *s)
{
    if (s->buf_ptr < s->buf_end) {
        return *s->buf_ptr++;
    } else {
        fill_buffer(s);
        if (s->buf_ptr < s->buf_end)
            return *s->buf_ptr++;
        else
            return URL_EOF;
    }
}

Fabrice Bellard's avatar
Fabrice Bellard committed
377 378 379 380 381 382 383 384 385 386
int get_buffer(ByteIOContext *s, unsigned char *buf, int size)
{
    int len, size1;

    size1 = size;
    while (size > 0) {
        len = s->buf_end - s->buf_ptr;
        if (len > size)
            len = size;
        if (len == 0) {
387
            if(size > s->buffer_size && !s->update_checksum){
388
                if(s->read_packet)
Benoit Fouet's avatar
Benoit Fouet committed
389
                    len = s->read_packet(s->opaque, buf, size);
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
                if (len <= 0) {
                    /* do not modify buffer if EOF reached so that a seek back can
                    be done without rereading data */
                    s->eof_reached = 1;
                    if(len<0)
                        s->error= len;
                    break;
                } else {
                    s->pos += len;
                    size -= len;
                    buf += len;
                    s->buf_ptr = s->buffer;
                    s->buf_end = s->buffer/* + len*/;
                }
            }else{
                fill_buffer(s);
                len = s->buf_end - s->buf_ptr;
                if (len == 0)
                    break;
            }
Fabrice Bellard's avatar
Fabrice Bellard committed
410 411 412 413 414 415 416
        } else {
            memcpy(buf, s->buf_ptr, len);
            buf += len;
            s->buf_ptr += len;
            size -= len;
        }
    }
417 418 419 420
    if (size1 == size) {
        if (url_ferror(s)) return url_ferror(s);
        if (url_feof(s))   return AVERROR_EOF;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
421 422 423
    return size1 - size;
}

424 425 426
int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size)
{
    int len;
427

428 429
    if(size<0)
        return -1;
430 431 432 433 434 435 436 437 438 439

    len = s->buf_end - s->buf_ptr;
    if (len == 0) {
        fill_buffer(s);
        len = s->buf_end - s->buf_ptr;
    }
    if (len > size)
        len = size;
    memcpy(buf, s->buf_ptr, len);
    s->buf_ptr += len;
440 441 442 443
    if (!len) {
        if (url_ferror(s)) return url_ferror(s);
        if (url_feof(s))   return AVERROR_EOF;
    }
444 445 446
    return len;
}

Fabrice Bellard's avatar
Fabrice Bellard committed
447 448 449 450 451 452 453 454
unsigned int get_le16(ByteIOContext *s)
{
    unsigned int val;
    val = get_byte(s);
    val |= get_byte(s) << 8;
    return val;
}

455 456 457 458 459 460 461 462
unsigned int get_le24(ByteIOContext *s)
{
    unsigned int val;
    val = get_le16(s);
    val |= get_byte(s) << 16;
    return val;
}

Fabrice Bellard's avatar
Fabrice Bellard committed
463 464 465
unsigned int get_le32(ByteIOContext *s)
{
    unsigned int val;
466 467
    val = get_le16(s);
    val |= get_le16(s) << 16;
Fabrice Bellard's avatar
Fabrice Bellard committed
468 469 470
    return val;
}

471
uint64_t get_le64(ByteIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
472
{
473 474 475
    uint64_t val;
    val = (uint64_t)get_le32(s);
    val |= (uint64_t)get_le32(s) << 32;
Fabrice Bellard's avatar
Fabrice Bellard committed
476 477 478 479 480 481 482 483 484 485 486
    return val;
}

unsigned int get_be16(ByteIOContext *s)
{
    unsigned int val;
    val = get_byte(s) << 8;
    val |= get_byte(s);
    return val;
}

487
unsigned int get_be24(ByteIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
488 489
{
    unsigned int val;
490
    val = get_be16(s) << 8;
Fabrice Bellard's avatar
Fabrice Bellard committed
491 492 493
    val |= get_byte(s);
    return val;
}
494 495 496 497 498 499 500
unsigned int get_be32(ByteIOContext *s)
{
    unsigned int val;
    val = get_be16(s) << 16;
    val |= get_be16(s);
    return val;
}
Fabrice Bellard's avatar
Fabrice Bellard committed
501

502
char *get_strz(ByteIOContext *s, char *buf, int maxlen)
503 504 505 506 507 508 509 510
{
    int i = 0;
    char c;

    while ((c = get_byte(s))) {
        if (i < maxlen-1)
            buf[i++] = c;
    }
511

512 513 514 515 516
    buf[i] = 0; /* Ensure null terminated, but may be truncated */

    return buf;
}

517
uint64_t get_be64(ByteIOContext *s)
Fabrice Bellard's avatar
Fabrice Bellard committed
518
{
519 520 521
    uint64_t val;
    val = (uint64_t)get_be32(s) << 32;
    val |= (uint64_t)get_be32(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
522 523 524
    return val;
}

525
uint64_t ff_get_v(ByteIOContext *bc){
526 527 528 529 530 531 532 533 534 535
    uint64_t val = 0;
    int tmp;

    do{
        tmp = get_byte(bc);
        val= (val<<7) + (tmp&127);
    }while(tmp&128);
    return val;
}

536
int url_fdopen(ByteIOContext **s, URLContext *h)
Fabrice Bellard's avatar
Fabrice Bellard committed
537
{
538
    uint8_t *buffer;
539
    int buffer_size, max_packet_size;
Fabrice Bellard's avatar
Fabrice Bellard committed
540

541 542 543 544 545 546
    max_packet_size = url_get_max_packet_size(h);
    if (max_packet_size) {
        buffer_size = max_packet_size; /* no need to bufferize more than one packet */
    } else {
        buffer_size = IO_BUFFER_SIZE;
    }
547
    buffer = av_malloc(buffer_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
548
    if (!buffer)
549
        return AVERROR(ENOMEM);
Fabrice Bellard's avatar
Fabrice Bellard committed
550

551 552 553 554 555 556 557
    *s = av_mallocz(sizeof(ByteIOContext));
    if(!*s) {
        av_free(buffer);
        return AVERROR(ENOMEM);
    }

    if (init_put_byte(*s, buffer, buffer_size,
558
                      (h->flags & URL_WRONLY || h->flags & URL_RDWR), h,
559
                      url_read, url_write, url_seek) < 0) {
560
        av_free(buffer);
561
        av_freep(s);
562
        return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
563
    }
564 565
    (*s)->is_streamed = h->is_streamed;
    (*s)->max_packet_size = max_packet_size;
566
    if(h->prot) {
567
        (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
568
        (*s)->read_seek  = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
569
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
570 571 572 573 574
    return 0;
}

int url_setbufsize(ByteIOContext *s, int buf_size)
{
575
    uint8_t *buffer;
576
    buffer = av_malloc(buf_size);
Fabrice Bellard's avatar
Fabrice Bellard committed
577
    if (!buffer)
578
        return AVERROR(ENOMEM);
Fabrice Bellard's avatar
Fabrice Bellard committed
579

580
    av_free(s->buffer);
Fabrice Bellard's avatar
Fabrice Bellard committed
581 582 583
    s->buffer = buffer;
    s->buffer_size = buf_size;
    s->buf_ptr = buffer;
584 585 586 587
    url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY);
    return 0;
}

588
#if LIBAVFORMAT_VERSION_MAJOR < 53
589
int url_resetbuf(ByteIOContext *s, int flags)
590 591 592
#else
static int url_resetbuf(ByteIOContext *s, int flags)
#endif
593
{
594
#if LIBAVFORMAT_VERSION_MAJOR < 53
595 596 597
    URLContext *h = s->opaque;
    if ((flags & URL_RDWR) || (h && h->flags != flags && !h->flags & URL_RDWR))
        return AVERROR(EINVAL);
598 599 600
#else
    assert(flags == URL_WRONLY || flags == URL_RDONLY);
#endif
601 602 603 604 605 606 607 608

    if (flags & URL_WRONLY) {
        s->buf_end = s->buffer + s->buffer_size;
        s->write_flag = 1;
    } else {
        s->buf_end = s->buffer;
        s->write_flag = 0;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
609 610 611
    return 0;
}

612
int url_fopen(ByteIOContext **s, const char *filename, int flags)
Fabrice Bellard's avatar
Fabrice Bellard committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
{
    URLContext *h;
    int err;

    err = url_open(&h, filename, flags);
    if (err < 0)
        return err;
    err = url_fdopen(s, h);
    if (err < 0) {
        url_close(h);
        return err;
    }
    return 0;
}

int url_fclose(ByteIOContext *s)
{
    URLContext *h = s->opaque;
631

632
    av_free(s->buffer);
633
    av_free(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
634 635 636 637 638 639 640 641
    return url_close(h);
}

URLContext *url_fileno(ByteIOContext *s)
{
    return s->opaque;
}

642
#if CONFIG_MUXERS
643 644 645 646 647 648 649 650 651 652 653 654
int url_fprintf(ByteIOContext *s, const char *fmt, ...)
{
    va_list ap;
    char buf[4096];
    int ret;

    va_start(ap, fmt);
    ret = vsnprintf(buf, sizeof(buf), fmt, ap);
    va_end(ap);
    put_buffer(s, buf, strlen(buf));
    return ret;
}
655
#endif //CONFIG_MUXERS
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

char *url_fgets(ByteIOContext *s, char *buf, int buf_size)
{
    int c;
    char *q;

    c = url_fgetc(s);
    if (c == EOF)
        return NULL;
    q = buf;
    for(;;) {
        if (c == EOF || c == '\n')
            break;
        if ((q - buf) < buf_size - 1)
            *q++ = c;
        c = url_fgetc(s);
    }
    if (buf_size > 0)
        *q = '\0';
    return buf;
}

int url_fget_max_packet_size(ByteIOContext *s)
{
    return s->max_packet_size;
}

683
int av_url_read_fpause(ByteIOContext *s, int pause)
684 685 686
{
    if (!s->read_pause)
        return AVERROR(ENOSYS);
687
    return s->read_pause(s->opaque, pause);
688 689
}

Diego Biurrun's avatar
Diego Biurrun committed
690 691
int64_t av_url_read_fseek(ByteIOContext *s, int stream_index,
                          int64_t timestamp, int flags)
692 693
{
    URLContext *h = s->opaque;
694
    int64_t ret;
695 696 697 698 699 700 701 702 703 704
    if (!s->read_seek)
        return AVERROR(ENOSYS);
    ret = s->read_seek(h, stream_index, timestamp, flags);
    if(ret >= 0) {
        s->buf_ptr = s->buf_end; // Flush buffer
        s->pos = s->seek(h, 0, SEEK_CUR);
    }
    return ret;
}

705
/* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response
706 707
 * back to the server even if CONFIG_MUXERS is false. */
#if CONFIG_MUXERS || CONFIG_NETWORK
Fabrice Bellard's avatar
Fabrice Bellard committed
708
/* buffer handling */
709
int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags)
Fabrice Bellard's avatar
Fabrice Bellard committed
710
{
711 712 713 714 715 716 717 718 719 720
    int ret;
    *s = av_mallocz(sizeof(ByteIOContext));
    if(!*s)
        return AVERROR(ENOMEM);
    ret = init_put_byte(*s, buf, buf_size,
                        (flags & URL_WRONLY || flags & URL_RDWR),
                        NULL, NULL, NULL, NULL);
    if(ret != 0)
        av_freep(s);
    return ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
721 722 723 724
}

int url_close_buf(ByteIOContext *s)
{
725
    put_flush_packet(s);
Fabrice Bellard's avatar
Fabrice Bellard committed
726 727
    return s->buf_ptr - s->buffer;
}
728 729 730 731 732

/* output in a dynamic buffer */

typedef struct DynBuffer {
    int pos, size, allocated_size;
733
    uint8_t *buffer;
734
    int io_buffer_size;
735
    uint8_t io_buffer[1];
736 737
} DynBuffer;

738
static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
739 740
{
    DynBuffer *d = opaque;
741
    unsigned new_size, new_allocated_size;
742

743 744 745
    /* reallocate buffer if needed */
    new_size = d->pos + buf_size;
    new_allocated_size = d->allocated_size;
746 747
    if(new_size < d->pos || new_size > INT_MAX/2)
        return -1;
748 749 750 751
    while (new_size > new_allocated_size) {
        if (!new_allocated_size)
            new_allocated_size = new_size;
        else
752
            new_allocated_size += new_allocated_size / 2 + 1;
753
    }
754

755
    if (new_allocated_size > d->allocated_size) {
756 757
        d->buffer = av_realloc(d->buffer, new_allocated_size);
        if(d->buffer == NULL)
758
             return AVERROR(ENOMEM);
759 760 761 762 763 764
        d->allocated_size = new_allocated_size;
    }
    memcpy(d->buffer + d->pos, buf, buf_size);
    d->pos = new_size;
    if (d->pos > d->size)
        d->size = d->pos;
765
    return buf_size;
766 767
}

768
static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
769 770
{
    unsigned char buf1[4];
771
    int ret;
772 773

    /* packetized write: output the header */
774
    AV_WB32(buf1, buf_size);
775 776 777
    ret= dyn_buf_write(opaque, buf1, 4);
    if(ret < 0)
        return ret;
778 779

    /* then the data */
780
    return dyn_buf_write(opaque, buf, buf_size);
781 782
}

783
static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
784 785 786 787 788 789 790 791 792 793 794 795 796
{
    DynBuffer *d = opaque;

    if (whence == SEEK_CUR)
        offset += d->pos;
    else if (whence == SEEK_END)
        offset += d->size;
    if (offset < 0 || offset > 0x7fffffffLL)
        return -1;
    d->pos = offset;
    return 0;
}

797
static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size)
798 799
{
    DynBuffer *d;
800
    int ret;
801
    unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
802

803 804
    if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
        return -1;
805
    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
806
    if (!d)
807
        return AVERROR(ENOMEM);
808 809 810 811 812
    *s = av_mallocz(sizeof(ByteIOContext));
    if(!*s) {
        av_free(d);
        return AVERROR(ENOMEM);
    }
813
    d->io_buffer_size = io_buffer_size;
814
    ret = init_put_byte(*s, d->io_buffer, io_buffer_size,
815 816
                        1, d, NULL,
                        max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
817 818
                        max_packet_size ? NULL : dyn_buf_seek);
    if (ret == 0) {
819 820 821 822
        (*s)->max_packet_size = max_packet_size;
    } else {
        av_free(d);
        av_freep(s);
823 824 825 826
    }
    return ret;
}

827
int url_open_dyn_buf(ByteIOContext **s)
828 829 830 831
{
    return url_open_dyn_buf_internal(s, 0);
}

832
int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size)
833 834 835 836 837 838
{
    if (max_packet_size <= 0)
        return -1;
    return url_open_dyn_buf_internal(s, max_packet_size);
}

839
int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer)
840 841 842 843 844 845 846 847 848
{
    DynBuffer *d = s->opaque;
    int size;

    put_flush_packet(s);

    *pbuffer = d->buffer;
    size = d->size;
    av_free(d);
849
    av_free(s);
850 851
    return size;
}
852
#endif /* CONFIG_MUXERS || CONFIG_NETWORK */