avcodec/ituh263enc: Simplify creating LUT

Only very few combinations (2x102) of 16384 correspond to
valid codes; so just initialize all codes via memset
and then set the few valid codes explicitly instead of initializing
everything in the same way.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2025-06-15 01:26:54 +02:00
parent ba79fff6d5
commit 8a3566498e
2 changed files with 24 additions and 30 deletions

View file

@ -59,6 +59,10 @@ extern const uint16_t ff_inter_vlc[103][2];
extern const int8_t ff_inter_level[102];
extern const int8_t ff_inter_run[102];
/* the following defines are valid for both ff_h263_rl_inter and ff_rl_intra_aic */
#define H263_RL_NB_ELEMS 102 // does not include escape
#define H263_RL_NON_LAST_CODES 58
#define H263_ESCAPE_CODE_LENGTH 7
extern RLTable ff_h263_rl_inter;
extern RLTable ff_rl_intra_aic;

View file

@ -99,41 +99,31 @@ static uint8_t uni_h263_inter_rl_len [64*64*2*2];
static av_cold void init_uni_h263_rl_tab(const RLTable *rl, uint8_t *len_tab)
{
const uint16_t (*table_vlc)[2] = rl->table_vlc;
const uint8_t *table_run = rl->table_run;
const uint8_t *table_level = rl->table_level;
av_assert0(MAX_LEVEL >= 64);
av_assert0(MAX_RUN >= 63);
for (int slevel = -64; slevel < 64; slevel++) {
if (slevel == 0) continue;
for (int run = 0; run < 64; run++) {
for (int last = 0; last <= 1; last++) {
const int index = UNI_MPEG4_ENC_INDEX(last, run, slevel + 64);
int level = slevel < 0 ? -slevel : slevel;
int sign = slevel < 0 ? 1 : 0;
int bits, len, code;
// Note: The LUT only covers level values for which the escape value
// is eight bits (not 8 + 5 + 6)
memset(len_tab, H263_ESCAPE_CODE_LENGTH + 1 + 6 + 8,
sizeof(uni_h263_intra_aic_rl_len));
len_tab[index] = 100;
len_tab += 64; // simplifies addressing
for (int i = 0; i < H263_RL_NB_ELEMS; ++i) {
int run = table_run[i];
int level = table_level[i];
int last = i >= H263_RL_NON_LAST_CODES;
int len = table_vlc[i][1];
/* ESC0 */
code = get_rl_index(rl, last, run, level);
bits = rl->table_vlc[code][0];
len = rl->table_vlc[code][1];
bits = bits * 2 + sign;
len++;
if (code != rl->n && len < len_tab[index])
len_tab[index] = len;
/* ESC */
bits = rl->table_vlc[rl->n][0];
len = rl->table_vlc[rl->n][1];
bits = bits * 2 + last; len++;
bits = bits * 64 + run; len += 6;
bits = bits * 256 + (level & 0xff); len += 8;
if (len < len_tab[index])
len_tab[index] = len;
}
}
len_tab[UNI_MPEG4_ENC_INDEX(last, run, level)] =
len_tab[UNI_MPEG4_ENC_INDEX(last, run, -level)] = len + 1 /* sign */;
}
for (int run = 0; run < MAX_RUN; ++run) {
len_tab[UNI_MPEG4_ENC_INDEX(0, run, 0)] =
len_tab[UNI_MPEG4_ENC_INDEX(1, run, 0)] = 0; // is this necessary?
}
}
#endif