diff mbox series

[FFmpeg-devel,12/20] avcodec/mqc: Hardcode tables to save space

Message ID HE1PR0301MB215452650BF0F7C4B432B1A88F579@HE1PR0301MB2154.eurprd03.prod.outlook.com
State Accepted
Commit b43e946b71e144a4ce5e83cd18e5a07155edb26f
Headers show
Series [FFmpeg-devel,01/20] avcodec/wma: Remove nonsense volatile
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt May 7, 2021, 6:46 a.m. UTC
mqc currently initializes three arrays at runtime; each of them
has 2 * 47 elements, one is uint16_t, two are uint8_t, so that their
combined size is 8 * 47. The source data for these initializations
is contained in an array of 47 elements of size six. Said array is
only used in order to initialize the other arrays, so the savings
are just 2 * 47B. Yet this is dwarfed by the size of the code for
performing the initializations: It is 109B (GCC 10.2, x64, -O3 albeit
in an av_cold function); this does not even include the size of the
code in the callers. So just hardcode these tables.

This also fixes a data race, because the encoder always initialized
these tables during init, although they might already be used at the
same time by already running encoder/decoder instances.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/j2kenc.c      |   1 -
 libavcodec/jpeg2000dec.c |   1 -
 libavcodec/mqc.c         | 105 +++++++++++----------------------------
 libavcodec/mqc.h         |  11 ++--
 4 files changed, 32 insertions(+), 86 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index c51adadd13..17eb959d73 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -1780,7 +1780,6 @@  static av_cold int j2kenc_init(AVCodecContext *avctx)
     }
 
     ff_jpeg2000_init_tier1_luts();
-    ff_mqc_init_context_tables();
     init_luts();
 
     init_quantization(s);
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5364631432..1295c96305 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -2476,7 +2476,6 @@  static int jp2_find_codestream(Jpeg2000DecoderContext *s)
 static av_cold void jpeg2000_init_static_data(void)
 {
     ff_jpeg2000_init_tier1_luts();
-    ff_mqc_init_context_tables();
 }
 
 static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)
diff --git a/libavcodec/mqc.c b/libavcodec/mqc.c
index f2d1e3b838..6f7088ec48 100644
--- a/libavcodec/mqc.c
+++ b/libavcodec/mqc.c
@@ -28,85 +28,38 @@ 
 #include <string.h>
 #include <stdint.h>
 
-#include "libavutil/attributes.h"
-
 #include "mqc.h"
 
-/* MQ coder context state structure */
-typedef struct MqcCxState {
-    uint16_t qe;
-    uint8_t  nmps;
-    uint8_t  nlps;
-    uint8_t  sw;
-} MqcCxState;
-
-static const MqcCxState cx_states[47] = {
-    { 0x5601,  1,  1, 1 },
-    { 0x3401,  2,  6, 0 },
-    { 0x1801,  3,  9, 0 },
-    { 0x0AC1,  4, 12, 0 },
-    { 0x0521,  5, 29, 0 },
-    { 0x0221, 38, 33, 0 },
-    { 0x5601,  7,  6, 1 },
-    { 0x5401,  8, 14, 0 },
-    { 0x4801,  9, 14, 0 },
-    { 0x3801, 10, 14, 0 },
-    { 0x3001, 11, 17, 0 },
-    { 0x2401, 12, 18, 0 },
-    { 0x1C01, 13, 20, 0 },
-    { 0x1601, 29, 21, 0 },
-    { 0x5601, 15, 14, 1 },
-    { 0x5401, 16, 14, 0 },
-    { 0x5101, 17, 15, 0 },
-    { 0x4801, 18, 16, 0 },
-    { 0x3801, 19, 17, 0 },
-    { 0x3401, 20, 18, 0 },
-    { 0x3001, 21, 19, 0 },
-    { 0x2801, 22, 19, 0 },
-    { 0x2401, 23, 20, 0 },
-    { 0x2201, 24, 21, 0 },
-    { 0x1C01, 25, 22, 0 },
-    { 0x1801, 26, 23, 0 },
-    { 0x1601, 27, 24, 0 },
-    { 0x1401, 28, 25, 0 },
-    { 0x1201, 29, 26, 0 },
-    { 0x1101, 30, 27, 0 },
-    { 0x0AC1, 31, 28, 0 },
-    { 0x09C1, 32, 29, 0 },
-    { 0x08A1, 33, 30, 0 },
-    { 0x0521, 34, 31, 0 },
-    { 0x0441, 35, 32, 0 },
-    { 0x02A1, 36, 33, 0 },
-    { 0x0221, 37, 34, 0 },
-    { 0x0141, 38, 35, 0 },
-    { 0x0111, 39, 36, 0 },
-    { 0x0085, 40, 37, 0 },
-    { 0x0049, 41, 38, 0 },
-    { 0x0025, 42, 39, 0 },
-    { 0x0015, 43, 40, 0 },
-    { 0x0009, 44, 41, 0 },
-    { 0x0005, 45, 42, 0 },
-    { 0x0001, 45, 43, 0 },
-    { 0x5601, 46, 46, 0 }
+const uint16_t ff_mqc_qe[2 * 47] = {
+    0x5601, 0x5601, 0x3401, 0x3401, 0x1801, 0x1801, 0x0ac1, 0x0ac1,
+    0x0521, 0x0521, 0x0221, 0x0221, 0x5601, 0x5601, 0x5401, 0x5401,
+    0x4801, 0x4801, 0x3801, 0x3801, 0x3001, 0x3001, 0x2401, 0x2401,
+    0x1c01, 0x1c01, 0x1601, 0x1601, 0x5601, 0x5601, 0x5401, 0x5401,
+    0x5101, 0x5101, 0x4801, 0x4801, 0x3801, 0x3801, 0x3401, 0x3401,
+    0x3001, 0x3001, 0x2801, 0x2801, 0x2401, 0x2401, 0x2201, 0x2201,
+    0x1c01, 0x1c01, 0x1801, 0x1801, 0x1601, 0x1601, 0x1401, 0x1401,
+    0x1201, 0x1201, 0x1101, 0x1101, 0x0ac1, 0x0ac1, 0x09c1, 0x09c1,
+    0x08a1, 0x08a1, 0x0521, 0x0521, 0x0441, 0x0441, 0x02a1, 0x02a1,
+    0x0221, 0x0221, 0x0141, 0x0141, 0x0111, 0x0111, 0x0085, 0x0085,
+    0x0049, 0x0049, 0x0025, 0x0025, 0x0015, 0x0015, 0x0009, 0x0009,
+    0x0005, 0x0005, 0x0001, 0x0001, 0x5601, 0x5601
+};
+const uint8_t ff_mqc_nlps[2 * 47] = {
+     3,  2, 12, 13, 18, 19, 24, 25, 58, 59, 66, 67, 13, 12, 28, 29,
+    28, 29, 28, 29, 34, 35, 36, 37, 40, 41, 42, 43, 29, 28, 28, 29,
+    30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 38, 39, 40, 41, 42, 43,
+    44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
+    60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
+    76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 92, 93
+};
+const uint8_t ff_mqc_nmps[2 * 47] = {
+     2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 76, 77, 14, 15, 16, 17,
+    18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 58, 59, 30, 31, 32, 33,
+    34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
+    50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
+    66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
+    82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 90, 91, 92, 93
 };
-
-uint16_t ff_mqc_qe [2 * 47];
-uint8_t ff_mqc_nlps[2 * 47];
-uint8_t ff_mqc_nmps[2 * 47];
-
-void av_cold ff_mqc_init_context_tables(void)
-{
-    int i;
-    for (i = 0; i < 47; i++) {
-        ff_mqc_qe[2 * i]     =
-        ff_mqc_qe[2 * i + 1] = cx_states[i].qe;
-
-        ff_mqc_nlps[2 * i]     = 2 * cx_states[i].nlps + cx_states[i].sw;
-        ff_mqc_nlps[2 * i + 1] = 2 * cx_states[i].nlps + 1 - cx_states[i].sw;
-        ff_mqc_nmps[2 * i]     = 2 * cx_states[i].nmps;
-        ff_mqc_nmps[2 * i + 1] = 2 * cx_states[i].nmps + 1;
-    }
-}
 
 void ff_mqc_init_contexts(MqcState *mqc)
 {
diff --git a/libavcodec/mqc.h b/libavcodec/mqc.h
index 73604d5efe..ad80fe9228 100644
--- a/libavcodec/mqc.h
+++ b/libavcodec/mqc.h
@@ -33,9 +33,9 @@ 
 #define MQC_CX_UNI 17
 #define MQC_CX_RL  18
 
-extern uint16_t ff_mqc_qe[2 * 47];
-extern uint8_t  ff_mqc_nlps[2 * 47];
-extern uint8_t  ff_mqc_nmps[2 * 47];
+extern const uint16_t ff_mqc_qe[2 * 47];
+extern const uint8_t  ff_mqc_nlps[2 * 47];
+extern const uint8_t  ff_mqc_nmps[2 * 47];
 
 typedef struct MqcState {
     uint8_t *bp, *bpstart;
@@ -79,11 +79,6 @@  int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate);
 
 /* common */
 
-/**
- * MQ-coder Initialize context tables (QE, NLPS, NMPS)
- */
-void ff_mqc_init_context_tables(void);
-
 /**
  * MQ-coder context initialisations.
  * @param mqc       MQ-coder context