[FFmpeg-devel,V1,1/2] lavf/avio: add a ffio_realloc_buf API for AVIO buffer realloc

Submitted by Jun Zhao on Aug. 8, 2019, 11:32 a.m.

Details

Message ID 1565263941-20590-1-git-send-email-mypopydev@gmail.com
State Accepted
Commit 3d1506c630eb59b428eb3585ccaa446fec7f3b0a
Headers show

Commit Message

Jun Zhao Aug. 8, 2019, 11:32 a.m.
From: tomajsjiang <tomajsjiang@tencent.com>

Add new API ffio_realloc_buf for AVIO buffer realloc.

Signed-off-by: Zhongxing Jiang <tomajsjiang@tencent.com>
---
 libavformat/avio_internal.h |    9 +++++++++
 libavformat/aviobuf.c       |   31 +++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 0 deletions(-)

Comments

Steven Liu Aug. 11, 2019, 8:43 a.m.
> 在 2019年8月8日,19:32,Jun Zhao <mypopydev@gmail.com> 写道:
> 
> From: tomajsjiang <tomajsjiang@tencent.com>
> 
> Add new API ffio_realloc_buf for AVIO buffer realloc.
> 
> Signed-off-by: Zhongxing Jiang <tomajsjiang@tencent.com>
> ---
> libavformat/avio_internal.h |    9 +++++++++
> libavformat/aviobuf.c       |   31 +++++++++++++++++++++++++++++++
> 2 files changed, 40 insertions(+), 0 deletions(-)
> 
> diff --git a/libavformat/avio_internal.h b/libavformat/avio_internal.h
> index 04c1ad5..eb628ac 100644
> --- a/libavformat/avio_internal.h
> +++ b/libavformat/avio_internal.h
> @@ -87,6 +87,15 @@ int ffio_read_size(AVIOContext *s, unsigned char *buf, int 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
>  *
>  * Will ensure that when reading sequentially up to buf_size, seeking
> diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
> index 2d01102..3b59180 100644
> --- a/libavformat/aviobuf.c
> +++ b/libavformat/aviobuf.c
> @@ -1093,6 +1093,37 @@ int ffio_set_buf_size(AVIOContext *s, int buf_size)
>     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)
> {
>     av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
> -- 
> 1.7.1
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe”.

patchset LGTM

btw, maybe this should bump version.h ?

Thanks
Steven
mypopy@gmail.com Aug. 12, 2019, 12:29 p.m.
On Sun, Aug 11, 2019 at 4:43 PM Steven Liu <lq@chinaffmpeg.org> wrote:
>
>
>
> > 在 2019年8月8日,19:32,Jun Zhao <mypopydev@gmail.com> 写道:
> >
> > From: tomajsjiang <tomajsjiang@tencent.com>
> >
> > Add new API ffio_realloc_buf for AVIO buffer realloc.
> >
> > Signed-off-by: Zhongxing Jiang <tomajsjiang@tencent.com>
> > ---
> > libavformat/avio_internal.h |    9 +++++++++
> > libavformat/aviobuf.c       |   31 +++++++++++++++++++++++++++++++
> > 2 files changed, 40 insertions(+), 0 deletions(-)
> >
> > diff --git a/libavformat/avio_internal.h b/libavformat/avio_internal.h
> > index 04c1ad5..eb628ac 100644
> > --- a/libavformat/avio_internal.h
> > +++ b/libavformat/avio_internal.h
> > @@ -87,6 +87,15 @@ int ffio_read_size(AVIOContext *s, unsigned char *buf, int 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
> >  *
> >  * Will ensure that when reading sequentially up to buf_size, seeking
> > diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
> > index 2d01102..3b59180 100644
> > --- a/libavformat/aviobuf.c
> > +++ b/libavformat/aviobuf.c
> > @@ -1093,6 +1093,37 @@ int ffio_set_buf_size(AVIOContext *s, int buf_size)
> >     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)
> > {
> >     av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
> > --
> > 1.7.1
> >
> > _______________________________________________
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe”.
>
> patchset LGTM
>
> btw, maybe this should bump version.h ?
>
ffio_realloc_buf is an internal function, I don't know is it needed to
bump the version.
Steven Liu Aug. 12, 2019, 10:03 p.m.
> 在 2019年8月12日,20:29,mypopy@gmail.com 写道:
> 
> On Sun, Aug 11, 2019 at 4:43 PM Steven Liu <lq@chinaffmpeg.org> wrote:
>> 
>> 
>> 
>>> 在 2019年8月8日,19:32,Jun Zhao <mypopydev@gmail.com> 写道:
>>> 
>>> From: tomajsjiang <tomajsjiang@tencent.com>
>>> 
>>> Add new API ffio_realloc_buf for AVIO buffer realloc.
>>> 
>>> Signed-off-by: Zhongxing Jiang <tomajsjiang@tencent.com>
>>> ---
>>> libavformat/avio_internal.h |    9 +++++++++
>>> libavformat/aviobuf.c       |   31 +++++++++++++++++++++++++++++++
>>> 2 files changed, 40 insertions(+), 0 deletions(-)
>>> 
>>> diff --git a/libavformat/avio_internal.h b/libavformat/avio_internal.h
>>> index 04c1ad5..eb628ac 100644
>>> --- a/libavformat/avio_internal.h
>>> +++ b/libavformat/avio_internal.h
>>> @@ -87,6 +87,15 @@ int ffio_read_size(AVIOContext *s, unsigned char *buf, int 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
>>> *
>>> * Will ensure that when reading sequentially up to buf_size, seeking
>>> diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
>>> index 2d01102..3b59180 100644
>>> --- a/libavformat/aviobuf.c
>>> +++ b/libavformat/aviobuf.c
>>> @@ -1093,6 +1093,37 @@ int ffio_set_buf_size(AVIOContext *s, int buf_size)
>>>    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)
>>> {
>>>    av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
>>> --
>>> 1.7.1
>>> 
>>> _______________________________________________
>>> ffmpeg-devel mailing list
>>> ffmpeg-devel@ffmpeg.org
>>> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>> 
>>> To unsubscribe, visit link above, or email
>>> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe”.
>> 
>> patchset LGTM
>> 
>> btw, maybe this should bump version.h ?
>> 
> ffio_realloc_buf is an internal function, I don't know is it needed to
> bump the version.
yes you are right :)
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

Thanks
Steven

Patch hide | download patch | download mbox

diff --git a/libavformat/avio_internal.h b/libavformat/avio_internal.h
index 04c1ad5..eb628ac 100644
--- a/libavformat/avio_internal.h
+++ b/libavformat/avio_internal.h
@@ -87,6 +87,15 @@  int ffio_read_size(AVIOContext *s, unsigned char *buf, int 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
  *
  * Will ensure that when reading sequentially up to buf_size, seeking
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 2d01102..3b59180 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -1093,6 +1093,37 @@  int ffio_set_buf_size(AVIOContext *s, int buf_size)
     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)
 {
     av_assert1(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);