frame: Simplify the video allocation

Message ID 20180728051859.46205-1-lu_zero@gentoo.org
State New
Headers show
Series
  • frame: Simplify the video allocation
Related show

Commit Message

Luca Barbato July 28, 2018, 5:18 a.m.
And reduce the memory fragmentation.

---

 libavutil/frame.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)

--
2.12.2

Comments

Maxym Dmytrychenko Aug. 1, 2018, 7:40 a.m. | #1
On Sat, Jul 28, 2018 at 7:19 AM Luca Barbato <lu_zero@gentoo.org> wrote:

> And reduce the memory fragmentation.
>
> ---
>
>  libavutil/frame.c | 29 +++++++++--------------------
>  1 file changed, 9 insertions(+), 20 deletions(-)
>
> diff --git a/libavutil/frame.c b/libavutil/frame.c
> index db3897c299..f16b1d4d52 100644
> --- a/libavutil/frame.c
> +++ b/libavutil/frame.c
> @@ -89,12 +89,8 @@ void av_frame_free(AVFrame **frame)
>
>  static int get_video_buffer(AVFrame *frame, int align)
>  {
> -    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
>      int ret, i;
>
> -    if (!desc)
> -        return AVERROR(EINVAL);
> -
>      if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL))
> < 0)
>          return ret;
>
> @@ -111,24 +107,17 @@ static int get_video_buffer(AVFrame *frame, int
> align)
>              frame->linesize[i] = FFALIGN(frame->linesize[i], align);
>      }
>
> -    for (i = 0; i < 4 && frame->linesize[i]; i++) {
> -        int h = frame->height;
> -        if (i == 1 || i == 2)
> -            h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
> +    if ((ret = av_image_fill_pointers(frame->data, frame->format,
> frame->height,
> +                                       NULL, frame->linesize)) < 0)
> +        return ret;
>
> -        frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
> -        if (!frame->buf[i])
> -            goto fail;
> +    frame->buf[0] = av_buffer_alloc(ret);
> +    if (!frame->buf[0])
> +        goto fail;
>
> -        frame->data[i] = frame->buf[i]->data;
> -    }
> -    if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags &
> AV_PIX_FMT_FLAG_PSEUDOPAL) {
> -        av_buffer_unref(&frame->buf[1]);
> -        frame->buf[1] = av_buffer_alloc(1024);
> -        if (!frame->buf[1])
> -            goto fail;
> -        frame->data[1] = frame->buf[1]->data;
> -    }
> +    if (av_image_fill_pointers(frame->data, frame->format, frame->height,
> +                               frame->buf[0]->data, frame->linesize) < 0)
> +        goto fail;
>
>      frame->extended_data = frame->data;
>
> --
> 2.12.2
>
> _______________________________________________
> libav-devel mailing list
> libav-devel@libav.org
> https://lists.libav.org/mailman/listinfo/libav-devel


seems to be very reasonable

regards
Max

Patch

diff --git a/libavutil/frame.c b/libavutil/frame.c
index db3897c299..f16b1d4d52 100644
--- a/libavutil/frame.c
+++ b/libavutil/frame.c
@@ -89,12 +89,8 @@  void av_frame_free(AVFrame **frame)

 static int get_video_buffer(AVFrame *frame, int align)
 {
-    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
     int ret, i;

-    if (!desc)
-        return AVERROR(EINVAL);
-
     if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
         return ret;

@@ -111,24 +107,17 @@  static int get_video_buffer(AVFrame *frame, int align)
             frame->linesize[i] = FFALIGN(frame->linesize[i], align);
     }

-    for (i = 0; i < 4 && frame->linesize[i]; i++) {
-        int h = frame->height;
-        if (i == 1 || i == 2)
-            h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
+    if ((ret = av_image_fill_pointers(frame->data, frame->format, frame->height,
+                                       NULL, frame->linesize)) < 0)
+        return ret;

-        frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h);
-        if (!frame->buf[i])
-            goto fail;
+    frame->buf[0] = av_buffer_alloc(ret);
+    if (!frame->buf[0])
+        goto fail;

-        frame->data[i] = frame->buf[i]->data;
-    }
-    if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
-        av_buffer_unref(&frame->buf[1]);
-        frame->buf[1] = av_buffer_alloc(1024);
-        if (!frame->buf[1])
-            goto fail;
-        frame->data[1] = frame->buf[1]->data;
-    }
+    if (av_image_fill_pointers(frame->data, frame->format, frame->height,
+                               frame->buf[0]->data, frame->linesize) < 0)
+        goto fail;

     frame->extended_data = frame->data;