1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avcodec/mqc: Hardcode tables to save space

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>
This commit is contained in:
Andreas Rheinhardt 2021-05-07 04:12:13 +02:00
parent 6546218572
commit b43e946b71
4 changed files with 32 additions and 86 deletions

View File

@ -1780,7 +1780,6 @@ static av_cold int j2kenc_init(AVCodecContext *avctx)
} }
ff_jpeg2000_init_tier1_luts(); ff_jpeg2000_init_tier1_luts();
ff_mqc_init_context_tables();
init_luts(); init_luts();
init_quantization(s); init_quantization(s);

View File

@ -2476,7 +2476,6 @@ static int jp2_find_codestream(Jpeg2000DecoderContext *s)
static av_cold void jpeg2000_init_static_data(void) static av_cold void jpeg2000_init_static_data(void)
{ {
ff_jpeg2000_init_tier1_luts(); ff_jpeg2000_init_tier1_luts();
ff_mqc_init_context_tables();
} }
static av_cold int jpeg2000_decode_init(AVCodecContext *avctx) static av_cold int jpeg2000_decode_init(AVCodecContext *avctx)

View File

@ -28,85 +28,38 @@
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include "libavutil/attributes.h"
#include "mqc.h" #include "mqc.h"
/* MQ coder context state structure */ const uint16_t ff_mqc_qe[2 * 47] = {
typedef struct MqcCxState { 0x5601, 0x5601, 0x3401, 0x3401, 0x1801, 0x1801, 0x0ac1, 0x0ac1,
uint16_t qe; 0x0521, 0x0521, 0x0221, 0x0221, 0x5601, 0x5601, 0x5401, 0x5401,
uint8_t nmps; 0x4801, 0x4801, 0x3801, 0x3801, 0x3001, 0x3001, 0x2401, 0x2401,
uint8_t nlps; 0x1c01, 0x1c01, 0x1601, 0x1601, 0x5601, 0x5601, 0x5401, 0x5401,
uint8_t sw; 0x5101, 0x5101, 0x4801, 0x4801, 0x3801, 0x3801, 0x3401, 0x3401,
} MqcCxState; 0x3001, 0x3001, 0x2801, 0x2801, 0x2401, 0x2401, 0x2201, 0x2201,
0x1c01, 0x1c01, 0x1801, 0x1801, 0x1601, 0x1601, 0x1401, 0x1401,
static const MqcCxState cx_states[47] = { 0x1201, 0x1201, 0x1101, 0x1101, 0x0ac1, 0x0ac1, 0x09c1, 0x09c1,
{ 0x5601, 1, 1, 1 }, 0x08a1, 0x08a1, 0x0521, 0x0521, 0x0441, 0x0441, 0x02a1, 0x02a1,
{ 0x3401, 2, 6, 0 }, 0x0221, 0x0221, 0x0141, 0x0141, 0x0111, 0x0111, 0x0085, 0x0085,
{ 0x1801, 3, 9, 0 }, 0x0049, 0x0049, 0x0025, 0x0025, 0x0015, 0x0015, 0x0009, 0x0009,
{ 0x0AC1, 4, 12, 0 }, 0x0005, 0x0005, 0x0001, 0x0001, 0x5601, 0x5601
{ 0x0521, 5, 29, 0 }, };
{ 0x0221, 38, 33, 0 }, const uint8_t ff_mqc_nlps[2 * 47] = {
{ 0x5601, 7, 6, 1 }, 3, 2, 12, 13, 18, 19, 24, 25, 58, 59, 66, 67, 13, 12, 28, 29,
{ 0x5401, 8, 14, 0 }, 28, 29, 28, 29, 34, 35, 36, 37, 40, 41, 42, 43, 29, 28, 28, 29,
{ 0x4801, 9, 14, 0 }, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 38, 39, 40, 41, 42, 43,
{ 0x3801, 10, 14, 0 }, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
{ 0x3001, 11, 17, 0 }, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
{ 0x2401, 12, 18, 0 }, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 92, 93
{ 0x1C01, 13, 20, 0 }, };
{ 0x1601, 29, 21, 0 }, const uint8_t ff_mqc_nmps[2 * 47] = {
{ 0x5601, 15, 14, 1 }, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 76, 77, 14, 15, 16, 17,
{ 0x5401, 16, 14, 0 }, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 58, 59, 30, 31, 32, 33,
{ 0x5101, 17, 15, 0 }, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
{ 0x4801, 18, 16, 0 }, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
{ 0x3801, 19, 17, 0 }, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
{ 0x3401, 20, 18, 0 }, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 90, 91, 92, 93
{ 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 }
}; };
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) void ff_mqc_init_contexts(MqcState *mqc)
{ {

View File

@ -33,9 +33,9 @@
#define MQC_CX_UNI 17 #define MQC_CX_UNI 17
#define MQC_CX_RL 18 #define MQC_CX_RL 18
extern uint16_t ff_mqc_qe[2 * 47]; extern const uint16_t ff_mqc_qe[2 * 47];
extern uint8_t ff_mqc_nlps[2 * 47]; extern const uint8_t ff_mqc_nlps[2 * 47];
extern uint8_t ff_mqc_nmps[2 * 47]; extern const uint8_t ff_mqc_nmps[2 * 47];
typedef struct MqcState { typedef struct MqcState {
uint8_t *bp, *bpstart; uint8_t *bp, *bpstart;
@ -79,11 +79,6 @@ int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate);
/* common */ /* common */
/**
* MQ-coder Initialize context tables (QE, NLPS, NMPS)
*/
void ff_mqc_init_context_tables(void);
/** /**
* MQ-coder context initialisations. * MQ-coder context initialisations.
* @param mqc MQ-coder context * @param mqc MQ-coder context