[FFmpeg-devel] [PATCH 2/6] avcodec/libvpxenc: only allocate U/V planes once for the alpha encoder instance

Marton Balint cus at passwd.hu
Sun Oct 27 19:10:32 EET 2019


Signed-off-by: Marton Balint <cus at passwd.hu>
---
 libavcodec/libvpxenc.c | 44 +++++++++++++++++++++++---------------------
 1 file changed, 23 insertions(+), 21 deletions(-)

diff --git a/libavcodec/libvpxenc.c b/libavcodec/libvpxenc.c
index 4c02315fd2..9da4529205 100644
--- a/libavcodec/libvpxenc.c
+++ b/libavcodec/libvpxenc.c
@@ -36,6 +36,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/base64.h"
 #include "libavutil/common.h"
+#include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
@@ -347,8 +348,11 @@ static av_cold int vpx_free(AVCodecContext *avctx)
 #endif
 
     vpx_codec_destroy(&ctx->encoder);
-    if (ctx->is_alpha)
+    if (ctx->is_alpha) {
         vpx_codec_destroy(&ctx->encoder_alpha);
+        av_freep(&ctx->rawimg_alpha.planes[VPX_PLANE_U]);
+        av_freep(&ctx->rawimg_alpha.planes[VPX_PLANE_V]);
+    }
     av_freep(&ctx->twopass_stats.buf);
     av_freep(&avctx->stats_out);
     free_frame_list(ctx->coded_frame_list);
@@ -872,9 +876,26 @@ FF_ENABLE_DEPRECATION_WARNINGS
         ctx->rawimg.bit_depth = enccfg.g_bit_depth;
 #endif
 
-    if (ctx->is_alpha)
+    if (ctx->is_alpha) {
+        uint8_t *u_plane, *v_plane;
+        int u_stride = av_image_get_linesize(avctx->pix_fmt, avctx->width, 1);
+        int v_stride = av_image_get_linesize(avctx->pix_fmt, avctx->width, 2);
         vpx_img_wrap(&ctx->rawimg_alpha, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1,
                      (unsigned char*)1);
+        u_plane = av_malloc(u_stride * avctx->height);
+        v_plane = av_malloc(v_stride * avctx->height);
+        if (!u_plane || !v_plane) {
+            av_free(u_plane);
+            av_free(v_plane);
+            return AVERROR(ENOMEM);
+        }
+        memset(u_plane, 0x80, u_stride * avctx->height);
+        memset(v_plane, 0x80, v_stride * avctx->height);
+        ctx->rawimg_alpha.planes[VPX_PLANE_U] = u_plane;
+        ctx->rawimg_alpha.planes[VPX_PLANE_V] = v_plane;
+        ctx->rawimg_alpha.stride[VPX_PLANE_U] = u_stride;
+        ctx->rawimg_alpha.stride[VPX_PLANE_V] = v_stride;
+    }
 
     cpb_props = ff_add_cpb_side_data(avctx);
     if (!cpb_props)
@@ -1312,23 +1333,9 @@ static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt,
         rawimg->stride[VPX_PLANE_U] = frame->linesize[1];
         rawimg->stride[VPX_PLANE_V] = frame->linesize[2];
         if (ctx->is_alpha) {
-            uint8_t *u_plane, *v_plane;
             rawimg_alpha = &ctx->rawimg_alpha;
             rawimg_alpha->planes[VPX_PLANE_Y] = frame->data[3];
-            u_plane = av_malloc(frame->linesize[1] * frame->height);
-            v_plane = av_malloc(frame->linesize[2] * frame->height);
-            if (!u_plane || !v_plane) {
-                av_free(u_plane);
-                av_free(v_plane);
-                return AVERROR(ENOMEM);
-            }
-            memset(u_plane, 0x80, frame->linesize[1] * frame->height);
-            rawimg_alpha->planes[VPX_PLANE_U] = u_plane;
-            memset(v_plane, 0x80, frame->linesize[2] * frame->height);
-            rawimg_alpha->planes[VPX_PLANE_V] = v_plane;
             rawimg_alpha->stride[VPX_PLANE_Y] = frame->linesize[3];
-            rawimg_alpha->stride[VPX_PLANE_U] = frame->linesize[1];
-            rawimg_alpha->stride[VPX_PLANE_V] = frame->linesize[2];
         }
         timestamp                   = frame->pts;
 #if VPX_IMAGE_ABI_VERSION >= 4
@@ -1390,11 +1397,6 @@ static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt,
                          ctx->twopass_stats.sz);
     }
 
-    if (rawimg_alpha) {
-        av_freep(&rawimg_alpha->planes[VPX_PLANE_U]);
-        av_freep(&rawimg_alpha->planes[VPX_PLANE_V]);
-    }
-
     *got_packet = !!coded_size;
     return 0;
 }
-- 
2.16.4



More information about the ffmpeg-devel mailing list