[FFmpeg-devel] [PATCH v2 087/162] avcodec/mpeg4videodec: Make studio VLCs static

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Fri Nov 20 09:20:00 EET 2020


Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavcodec/mpeg4video.h    |  4 --
 libavcodec/mpeg4videodec.c | 88 ++++++++++++++++----------------------
 2 files changed, 36 insertions(+), 56 deletions(-)

diff --git a/libavcodec/mpeg4video.h b/libavcodec/mpeg4video.h
index 3de598465f..e919db87a5 100644
--- a/libavcodec/mpeg4video.h
+++ b/libavcodec/mpeg4video.h
@@ -115,10 +115,6 @@ typedef struct Mpeg4DecContext {
     int cplx_estimation_trash_p;
     int cplx_estimation_trash_b;
 
-    VLC studio_intra_tab[12];
-    VLC studio_luma_dc;
-    VLC studio_chroma_dc;
-
     int rgb;
 } Mpeg4DecContext;
 
diff --git a/libavcodec/mpeg4videodec.c b/libavcodec/mpeg4videodec.c
index cc3c36d722..19dcaff764 100644
--- a/libavcodec/mpeg4videodec.c
+++ b/libavcodec/mpeg4videodec.c
@@ -25,6 +25,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/thread.h"
 #include "error_resilience.h"
 #include "hwconfig.h"
 #include "idctdsp.h"
@@ -53,6 +54,9 @@ static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb);
 static VLC dc_lum, dc_chrom;
 static VLC sprite_trajectory;
 static VLC mb_type_b_vlc;
+static VLC studio_intra_tab[12];
+static VLC studio_luma_dc;
+static VLC studio_chroma_dc;
 
 static const int mb_type_b_map[4] = {
     MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
@@ -1820,7 +1824,7 @@ static int mpeg4_decode_studio_block(MpegEncContext *s, int32_t block[64], int n
 
     int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
         additional_code_len, sign, mismatch;
-    VLC *cur_vlc = &ctx->studio_intra_tab[0];
+    const VLC *cur_vlc = &studio_intra_tab[0];
     uint8_t *const scantable = s->intra_scantable.permutated;
     const uint16_t *quant_matrix;
     uint32_t flc;
@@ -1834,14 +1838,14 @@ static int mpeg4_decode_studio_block(MpegEncContext *s, int32_t block[64], int n
 
     if (n < 4) {
         cc = 0;
-        dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
+        dct_dc_size = get_vlc2(&s->gb, studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
         quant_matrix = s->intra_matrix;
     } else {
         cc = (n & 1) + 1;
         if (ctx->rgb)
-            dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
+            dct_dc_size = get_vlc2(&s->gb, studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
         else
-            dct_dc_size = get_vlc2(&s->gb, ctx->studio_chroma_dc.table, STUDIO_INTRA_BITS, 2);
+            dct_dc_size = get_vlc2(&s->gb, studio_chroma_dc.table, STUDIO_INTRA_BITS, 2);
         quant_matrix = s->chroma_intra_matrix;
     }
 
@@ -1878,7 +1882,7 @@ static int mpeg4_decode_studio_block(MpegEncContext *s, int32_t block[64], int n
         }
 
         additional_code_len = ac_state_tab[group][0];
-        cur_vlc = &ctx->studio_intra_tab[ac_state_tab[group][1]];
+        cur_vlc = &studio_intra_tab[ac_state_tab[group][1]];
 
         if (group == 0) {
             /* End of Block */
@@ -3501,40 +3505,36 @@ static int mpeg4_update_thread_context(AVCodecContext *dst,
 }
 #endif
 
-static av_cold int init_studio_vlcs(Mpeg4DecContext *ctx)
+static av_cold void mpeg4_init_static(void)
 {
-    int i, ret;
-
-    for (i = 0; i < 12; i++) {
-        ret = ff_init_vlc_from_lengths(&ctx->studio_intra_tab[i],
-                                       STUDIO_INTRA_BITS, 24,
-                                       &ff_mpeg4_studio_intra[i][0][1], 2,
-                                       &ff_mpeg4_studio_intra[i][0][0], 2, 1,
-                                       0, 0, NULL);
-
-        if (ret < 0)
-            return ret;
+    INIT_VLC_STATIC_FROM_LENGTHS(&studio_luma_dc, STUDIO_INTRA_BITS, 19,
+                                 &ff_mpeg4_studio_dc_luma[0][1], 2,
+                                 &ff_mpeg4_studio_dc_luma[0][0], 2, 1,
+                                 0, 0, 528);
+
+    INIT_VLC_STATIC_FROM_LENGTHS(&studio_chroma_dc, STUDIO_INTRA_BITS, 19,
+                                 &ff_mpeg4_studio_dc_chroma[0][1], 2,
+                                 &ff_mpeg4_studio_dc_chroma[0][0], 2, 1,
+                                 0, 0, 528);
+
+    for (unsigned i = 0, offset = 0; i < 12; i++) {
+        static VLC_TYPE vlc_buf[6498][2];
+
+        studio_intra_tab[i].table           = &vlc_buf[offset];
+        studio_intra_tab[i].table_allocated = FF_ARRAY_ELEMS(vlc_buf) - offset;
+        ff_init_vlc_from_lengths(&studio_intra_tab[i],
+                                 STUDIO_INTRA_BITS, 24,
+                                 &ff_mpeg4_studio_intra[i][0][1], 2,
+                                 &ff_mpeg4_studio_intra[i][0][0], 2, 1,
+                                 0, INIT_VLC_STATIC_OVERLONG, NULL);
+        offset += studio_intra_tab[i].table_size;
     }
-
-    ret = ff_init_vlc_from_lengths(&ctx->studio_luma_dc, STUDIO_INTRA_BITS, 19,
-                                   &ff_mpeg4_studio_dc_luma[0][1], 2,
-                                   &ff_mpeg4_studio_dc_luma[0][0], 2, 1,
-                                   0, 0, NULL);
-    if (ret < 0)
-        return ret;
-
-    ret = ff_init_vlc_from_lengths(&ctx->studio_chroma_dc, STUDIO_INTRA_BITS, 19,
-                                   &ff_mpeg4_studio_dc_chroma[0][1], 2,
-                                   &ff_mpeg4_studio_dc_chroma[0][0], 2, 1,
-                                   0, 0, NULL);
-    if (ret < 0)
-        return ret;
-
-    return 0;
+    ff_mpeg4videodec_static_init();
 }
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
+    static AVOnce static_init_once = AV_ONCE_INIT;
     Mpeg4DecContext *ctx = avctx->priv_data;
     MpegEncContext *s = &ctx->m;
     int ret;
@@ -3547,10 +3547,6 @@ static av_cold int decode_init(AVCodecContext *avctx)
     if ((ret = ff_h263_decode_init(avctx)) < 0)
         return ret;
 
-    ff_mpeg4videodec_static_init();
-    if ((ret = init_studio_vlcs(ctx)) < 0)
-        return ret;
-
     s->h263_pred = 1;
     s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
     s->decode_mb = mpeg4_decode_mb;
@@ -3558,21 +3554,9 @@ static av_cold int decode_init(AVCodecContext *avctx)
 
     avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
 
-    return 0;
-}
+    ff_thread_once(&static_init_once, mpeg4_init_static);
 
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    Mpeg4DecContext *ctx = avctx->priv_data;
-    int i;
-
-    for (i = 0; i < 12; i++)
-        ff_free_vlc(&ctx->studio_intra_tab[i]);
-
-    ff_free_vlc(&ctx->studio_luma_dc);
-    ff_free_vlc(&ctx->studio_chroma_dc);
-
-    return ff_h263_decode_end(avctx);
+    return 0;
 }
 
 static const AVOption mpeg4_options[] = {
@@ -3595,7 +3579,7 @@ AVCodec ff_mpeg4_decoder = {
     .id                    = AV_CODEC_ID_MPEG4,
     .priv_data_size        = sizeof(Mpeg4DecContext),
     .init                  = decode_init,
-    .close                 = decode_end,
+    .close                 = ff_h263_decode_end,
     .decode                = ff_h263_decode_frame,
     .capabilities          = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
                              AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
-- 
2.25.1



More information about the ffmpeg-devel mailing list