diff mbox series

[FFmpeg-devel,v2,48/69] avcodec/mpegvideo_enc: Avoid allocations for q_int(er|ra)_matrix tables

Message ID AM7PR03MB66602B8DA4BF4E334A4D93508F269@AM7PR03MB6660.eurprd03.prod.outlook.com
State New
Headers show
Series [FFmpeg-devel,v2,01/69] avcodec/avcodec: Avoid MpegEncContext in AVHWAccel.decode_mb | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Feb. 1, 2022, 1:06 p.m. UTC
Do this by making them part of an MPVMainEncContext; the MPVEncContexts
still retain their pointers to access them.
(These tables are quite big, so that putting them in the MpegEncContext
would have been very wasteful for all the decoders using said structure.
But this is no longer a problem: MPVMainEncContext is only used by
encoders and allows to directly share the tables when slice-threading
is in use.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegvideo_enc.c | 24 ++++++++----------------
 libavcodec/mpegvideoenc.h  |  7 +++++++
 2 files changed, 15 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 75f1db2a76..0e14cebb3f 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -871,12 +871,6 @@  av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
     }
 
     if (!(avctx->stats_out = av_mallocz(256))               ||
-        !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix,          32) ||
-        !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix,   32) ||
-        !FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix,          32) ||
-        !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix16,        32) ||
-        !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix16, 32) ||
-        !FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix16,        32) ||
         !FF_ALLOCZ_TYPED_ARRAY(s->reordered_input_picture, MAX_PICTURE_COUNT))
         return AVERROR(ENOMEM);
 
@@ -938,6 +932,11 @@  av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
             s->inter_matrix[j] = avctx->inter_matrix[i];
     }
 
+    s->q_inter_matrix   = m->q_inter_matrix;
+    s->q_intra_matrix   = m->q_intra_matrix;
+    s->q_inter_matrix16 = m->q_inter_matrix16;
+    s->q_intra_matrix16 = m->q_intra_matrix16;
+
     /* precompute matrix */
     /* for mjpeg, we do include qscale in the matrix */
     if (s->out_format != FMT_MJPEG) {
@@ -947,10 +946,11 @@  av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
         ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
                           s->inter_matrix, s->inter_quant_bias, avctx->qmin,
                           31, 0);
-        av_freep(&s->q_chroma_intra_matrix);
-        av_freep(&s->q_chroma_intra_matrix16);
         s->q_chroma_intra_matrix   = s->q_intra_matrix;
         s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
+    } else {
+        s->q_chroma_intra_matrix   = m->q_chroma_intra_matrix;
+        s->q_chroma_intra_matrix16 = m->q_chroma_intra_matrix16;
     }
 
     if ((ret = ff_rate_control_init(m)) < 0)
@@ -1015,14 +1015,6 @@  av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
     av_freep(&m->cplx_tab);
     av_freep(&m->bits_tab);
 
-    if(s->q_chroma_intra_matrix   != s->q_intra_matrix  ) av_freep(&s->q_chroma_intra_matrix);
-    if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
-    s->q_chroma_intra_matrix=   NULL;
-    s->q_chroma_intra_matrix16= NULL;
-    av_freep(&s->q_intra_matrix);
-    av_freep(&s->q_inter_matrix);
-    av_freep(&s->q_intra_matrix16);
-    av_freep(&s->q_inter_matrix16);
     av_freep(&s->reordered_input_picture);
     av_freep(&s->dct_offset);
 
diff --git a/libavcodec/mpegvideoenc.h b/libavcodec/mpegvideoenc.h
index e124555b0a..b83c24debb 100644
--- a/libavcodec/mpegvideoenc.h
+++ b/libavcodec/mpegvideoenc.h
@@ -100,6 +100,13 @@  typedef struct MPVMainEncContext {
     int16_t (*b_bidir_back_mv_table_base)[2];
     int16_t (*b_direct_mv_table_base)[2];
     int16_t (*b_field_mv_table_base)[2];
+
+    int q_inter_matrix[32][64];
+    int q_intra_matrix[32][64];
+    int q_chroma_intra_matrix[32][64];
+    uint16_t q_inter_matrix16[32][2][64];
+    uint16_t q_intra_matrix16[32][2][64];
+    uint16_t q_chroma_intra_matrix16[32][2][64];
 } MPVMainEncContext;
 
 #define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))