lavf/avio: add a ffio_realloc_buf API for AVIO buffer realloc

Add new API ffio_realloc_buf for AVIO buffer realloc.

Signed-off-by: Zhongxing Jiang <tomajsjiang@tencent.com>
This commit is contained in:
tomajsjiang 2019-07-04 11:58:41 +08:00 committed by Jun Zhao
parent 03ba386833
commit 3d1506c630
2 changed files with 40 additions and 0 deletions

View File

@ -86,6 +86,15 @@ int ffio_read_size(AVIOContext *s, unsigned char *buf, int size);
/** @warning must be called before any I/O */ /** @warning must be called before any I/O */
int ffio_set_buf_size(AVIOContext *s, int buf_size); int ffio_set_buf_size(AVIOContext *s, int buf_size);
/**
* Reallocate a given buffer for AVIOContext.
*
* @param s the AVIOContext to realloc.
* @param buf_size required new buffer size.
* @return 0 on success, a negative AVERROR on failure.
*/
int ffio_realloc_buf(AVIOContext *s, int buf_size);
/** /**
* Ensures that the requested seekback buffer size will be available * Ensures that the requested seekback buffer size will be available
* *

View File

@ -1096,6 +1096,37 @@ int ffio_set_buf_size(AVIOContext *s, int buf_size)
return 0; return 0;
} }
int ffio_realloc_buf(AVIOContext *s, int buf_size)
{
uint8_t *buffer;
int data_size;
if (!s->buffer_size)
return ffio_set_buf_size(s, buf_size);
if (buf_size <= s->buffer_size)
return 0;
buffer = av_malloc(buf_size);
if (!buffer)
return AVERROR(ENOMEM);
data_size = s->write_flag ? (s->buf_ptr - s->buffer) : (s->buf_end - s->buf_ptr);
if (data_size > 0)
memcpy(buffer, s->write_flag ? s->buffer : s->buf_ptr, data_size);
av_free(s->buffer);
s->buffer = buffer;
s->orig_buffer_size = buf_size;
s->buffer_size = buf_size;
s->buf_ptr = s->write_flag ? (s->buffer + data_size) : s->buffer;
if (s->write_flag)
s->buf_ptr_max = s->buffer + data_size;
s->buf_end = s->write_flag ? (s->buffer + s->buffer_size) : (s->buf_ptr + data_size);
return 0;
}
static int url_resetbuf(AVIOContext *s, int flags) static int url_resetbuf(AVIOContext *s, int flags)
{ {
av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ); av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);