Revision 01d91b9b

View differences:

libavformat/avio.h
433 433
#endif
434 434

  
435 435
/**
436
 * Rewinds the ByteIOContext using the specified buffer containing the first buf_size bytes of the file.
437
 * Used after probing to avoid seeking.
438
 * Joins buf and s->buffer, taking any overlap into consideration.
439
 * @note s->buffer must overlap with buf or they can't be joined and the function fails
440
 * @note This function is NOT part of the public API
441
 *
442
 * @param s The read-only ByteIOContext to rewind
443
 * @param buf The probe buffer containing the first buf_size bytes of the file
444
 * @param buf_size The size of buf
445
 * @return 0 in case of success, a negative value corresponding to an
446
 * AVERROR code in case of failure
447
 */
448
int ff_rewind_with_probe_data(ByteIOContext *s, unsigned char *buf, int buf_size);
449

  
450
/**
436 451
 * Creates and initializes a ByteIOContext for accessing the
437 452
 * resource indicated by url.
438 453
 * @note When the resource indicated by url has been opened in
libavformat/aviobuf.c
295 295
{
296 296
    uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_ptr : s->buffer;
297 297
    int len= s->buffer_size - (dst - s->buffer);
298
    int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
298 299

  
299 300
    assert(s->buf_ptr == s->buf_end);
300 301

  
......
308 309
        s->checksum_ptr= s->buffer;
309 310
    }
310 311

  
312
    /* make buffer smaller in case it ended up large after probing */
313
    if (s->buffer_size > max_buffer_size) {
314
        url_setbufsize(s, max_buffer_size);
315

  
316
        s->checksum_ptr = dst = s->buffer;
317
        len = s->buffer_size;
318
    }
319

  
311 320
    if(s->read_packet)
312 321
        len = s->read_packet(s->opaque, dst, len);
313 322
    else
......
611 620
    return 0;
612 621
}
613 622

  
623
int ff_rewind_with_probe_data(ByteIOContext *s, unsigned char *buf, int buf_size)
624
{
625
    int64_t buffer_start;
626
    int buffer_size;
627
    int overlap, new_size;
628

  
629
    if (s->write_flag)
630
        return AVERROR(EINVAL);
631

  
632
    buffer_size = s->buf_end - s->buffer;
633

  
634
    /* the buffers must touch or overlap */
635
    if ((buffer_start = s->pos - buffer_size) > buf_size)
636
        return AVERROR(EINVAL);
637

  
638
    overlap = buf_size - buffer_start;
639
    new_size = buf_size + buffer_size - overlap;
640

  
641
    if (new_size > buf_size) {
642
        if (!(buf = av_realloc(buf, new_size)))
643
            return AVERROR(ENOMEM);
644

  
645
        memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
646
        buf_size = new_size;
647
    }
648

  
649
    av_free(s->buffer);
650
    s->buf_ptr = s->buffer = buf;
651
    s->pos = s->buffer_size = buf_size;
652
    s->buf_end = s->buf_ptr + buf_size;
653
    s->eof_reached = 0;
654
    s->must_flush = 0;
655

  
656
    return 0;
657
}
658

  
614 659
int url_fopen(ByteIOContext **s, const char *filename, int flags)
615 660
{
616 661
    URLContext *h;
libavformat/utils.c
516 516
        }
517 517
    }
518 518

  
519
    av_free(buf);
520

  
521 519
    if (!*fmt) {
520
        av_free(buf);
522 521
        return AVERROR_INVALIDDATA;
523 522
    }
524 523

  
525
    if (url_fseek(*pb, 0, SEEK_SET) < 0) {
526
        url_fclose(*pb);
527
        if (url_fopen(pb, filename, URL_RDONLY) < 0)
528
            return AVERROR(EIO);
529
    }
524
    /* rewind. reuse probe buffer to avoid seeking */
525
    if ((ret = ff_rewind_with_probe_data(*pb, buf, pd.buf_size)) < 0)
526
        av_free(buf);
530 527

  
531
    return 0;
528
    return ret;
532 529
}
533 530

  
534 531
int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,

Also available in: Unified diff