[FFmpeg-devel] [PATCH 4/7] avcodec/mpegvideo: Allocate map and score_map buffers jointly

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Wed Oct 26 05:01:45 EEST 2022


Reduces the amounts of allocs, frees and allocation checks.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/mpegvideo.c | 7 ++++---
 libavcodec/snow.c      | 1 -
 libavcodec/snowenc.c   | 6 +++---
 libavcodec/svq1enc.c   | 8 +++-----
 4 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 0cd7c86ff6..a04d519ccc 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -373,9 +373,10 @@ static int init_duplicate_context(MpegEncContext *s)
         yc_size += 2*s->b8_stride + 2*s->mb_stride;
 
     if (s->encoding) {
-        if (!FF_ALLOCZ_TYPED_ARRAY(s->me.map,       ME_MAP_SIZE) ||
-            !FF_ALLOCZ_TYPED_ARRAY(s->me.score_map, ME_MAP_SIZE))
+        s->me.map = av_mallocz(2 * ME_MAP_SIZE * sizeof(*s->me.map));
+        if (!s->me.map)
             return AVERROR(ENOMEM);
+        s->me.score_map = s->me.map + ME_MAP_SIZE;
 
         if (s->noise_reduction) {
             if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_error_sum,  2))
@@ -445,7 +446,7 @@ static void free_duplicate_context(MpegEncContext *s)
 
     av_freep(&s->dct_error_sum);
     av_freep(&s->me.map);
-    av_freep(&s->me.score_map);
+    s->me.score_map = NULL;
     av_freep(&s->blocks);
     av_freep(&s->ac_val_base);
     s->block = NULL;
diff --git a/libavcodec/snow.c b/libavcodec/snow.c
index cde09902c3..b6c8d5e256 100644
--- a/libavcodec/snow.c
+++ b/libavcodec/snow.c
@@ -635,7 +635,6 @@ av_cold void ff_snow_common_end(SnowContext *s)
     s->m.me.temp= NULL;
     av_freep(&s->m.me.scratchpad);
     av_freep(&s->m.me.map);
-    av_freep(&s->m.me.score_map);
     av_freep(&s->m.sc.obmc_scratchpad);
 
     av_freep(&s->block);
diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index ada24f7895..7fad95b69a 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -82,11 +82,11 @@ static av_cold int encode_init(AVCodecContext *avctx)
 
     s->m.me.temp      =
     s->m.me.scratchpad = av_calloc(avctx->width + 64, 2*16*2*sizeof(uint8_t));
-    s->m.me.map       = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
-    s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
     s->m.sc.obmc_scratchpad= av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
-    if (!s->m.me.scratchpad || !s->m.me.map || !s->m.me.score_map || !s->m.sc.obmc_scratchpad)
+    s->m.me.map       = av_mallocz(2 * ME_MAP_SIZE * sizeof(*s->m.me.map));
+    if (!s->m.me.scratchpad || !s->m.me.map || !s->m.sc.obmc_scratchpad)
         return AVERROR(ENOMEM);
+    s->m.me.score_map = s->m.me.map + ME_MAP_SIZE;
 
     ff_h263_encode_init(&s->m); //mv_penalty
 
diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c
index 7c9430a137..92f91aeebd 100644
--- a/libavcodec/svq1enc.c
+++ b/libavcodec/svq1enc.c
@@ -552,7 +552,6 @@ static av_cold int svq1_encode_end(AVCodecContext *avctx)
 
     av_freep(&s->m.me.scratchpad);
     av_freep(&s->m.me.map);
-    av_freep(&s->m.me.score_map);
     av_freep(&s->mb_type);
     av_freep(&s->dummy);
     av_freep(&s->scratchbuf);
@@ -608,18 +607,17 @@ static av_cold int svq1_encode_init(AVCodecContext *avctx)
     s->m.me.temp           =
     s->m.me.scratchpad     = av_mallocz((avctx->width + 64) *
                                         2 * 16 * 2 * sizeof(uint8_t));
-    s->m.me.map            = av_mallocz(ME_MAP_SIZE * sizeof(uint32_t));
-    s->m.me.score_map      = av_mallocz(ME_MAP_SIZE * sizeof(uint32_t));
     s->mb_type             = av_mallocz((s->y_block_width + 1) *
                                         s->y_block_height * sizeof(int16_t));
     s->dummy               = av_mallocz((s->y_block_width + 1) *
                                         s->y_block_height * sizeof(int32_t));
+    s->m.me.map            = av_mallocz(2 * ME_MAP_SIZE * sizeof(*s->m.me.map));
     s->svq1encdsp.ssd_int8_vs_int16 = ssd_int8_vs_int16_c;
 
     if (!s->m.me.temp || !s->m.me.scratchpad || !s->m.me.map ||
-        !s->m.me.score_map || !s->mb_type || !s->dummy) {
+        !s->mb_type || !s->dummy)
         return AVERROR(ENOMEM);
-    }
+    s->m.me.score_map = s->m.me.map + ME_MAP_SIZE;
 
 #if ARCH_PPC
     ff_svq1enc_init_ppc(&s->svq1encdsp);
-- 
2.34.1



More information about the ffmpeg-devel mailing list