1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-26 19:01:44 +02:00

avcodec/dvdec: Avoid stack buffers

Instead reuse the destination RL VLC as scratch space.
This is possible, because the (implicit) codes here are already
ordered from left-to-right in the tree and because the codelengths
are increasing, which implies that mapping from VLC entries to the
corresponding entries used to initialize the VLC is monotonically
increasing. This means that one can reuse the right end of the
destination RL VLC to store the tables used to initialize the VLC
with.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2022-09-04 04:16:17 +02:00
parent 1fa535e8f3
commit 51ca74b525
2 changed files with 15 additions and 13 deletions

View File

@ -27,6 +27,8 @@ extern const uint8_t ff_dv_quant_shifts[22][4];
extern const uint8_t ff_dv_quant_offset[4]; extern const uint8_t ff_dv_quant_offset[4];
#define NB_DV_VLC 409 #define NB_DV_VLC 409
/* The number of entries with value zero in ff_dv_vlc_level. */
#define NB_DV_ZERO_LEVEL_ENTRIES 72
extern const uint8_t ff_dv_vlc_len[NB_DV_VLC]; extern const uint8_t ff_dv_vlc_len[NB_DV_VLC];
extern const uint8_t ff_dv_vlc_run[NB_DV_VLC]; extern const uint8_t ff_dv_vlc_run[NB_DV_VLC];

View File

@ -152,31 +152,30 @@ static av_cold void dv_init_static(void)
{ {
VLCElem vlc_buf[FF_ARRAY_ELEMS(dv_rl_vlc)] = { 0 }; VLCElem vlc_buf[FF_ARRAY_ELEMS(dv_rl_vlc)] = { 0 };
VLC dv_vlc = { .table = vlc_buf, .table_allocated = FF_ARRAY_ELEMS(vlc_buf) }; VLC dv_vlc = { .table = vlc_buf, .table_allocated = FF_ARRAY_ELEMS(vlc_buf) };
uint8_t new_dv_vlc_len[NB_DV_VLC * 2]; const unsigned offset = FF_ARRAY_ELEMS(dv_rl_vlc) - (2 * NB_DV_VLC - NB_DV_ZERO_LEVEL_ENTRIES);
uint8_t new_dv_vlc_run[NB_DV_VLC * 2]; RL_VLC_ELEM *tmp = dv_rl_vlc + offset;
int16_t new_dv_vlc_level[NB_DV_VLC * 2];
int i, j; int i, j;
/* it's faster to include sign bit in a generic VLC parsing scheme */ /* it's faster to include sign bit in a generic VLC parsing scheme */
for (i = 0, j = 0; i < NB_DV_VLC; i++, j++) { for (i = 0, j = 0; i < NB_DV_VLC; i++, j++) {
new_dv_vlc_len[j] = ff_dv_vlc_len[i]; tmp[j].len = ff_dv_vlc_len[i];
new_dv_vlc_run[j] = ff_dv_vlc_run[i]; tmp[j].run = ff_dv_vlc_run[i];
new_dv_vlc_level[j] = ff_dv_vlc_level[i]; tmp[j].level = ff_dv_vlc_level[i];
if (ff_dv_vlc_level[i]) { if (ff_dv_vlc_level[i]) {
new_dv_vlc_len[j]++; tmp[j].len++;
j++; j++;
new_dv_vlc_len[j] = ff_dv_vlc_len[i] + 1; tmp[j].len = ff_dv_vlc_len[i] + 1;
new_dv_vlc_run[j] = ff_dv_vlc_run[i]; tmp[j].run = ff_dv_vlc_run[i];
new_dv_vlc_level[j] = -ff_dv_vlc_level[i]; tmp[j].level = -ff_dv_vlc_level[i];
} }
} }
/* NOTE: as a trick, we use the fact the no codes are unused /* NOTE: as a trick, we use the fact the no codes are unused
* to accelerate the parsing of partial codes */ * to accelerate the parsing of partial codes */
ff_init_vlc_from_lengths(&dv_vlc, TEX_VLC_BITS, j, ff_init_vlc_from_lengths(&dv_vlc, TEX_VLC_BITS, j,
new_dv_vlc_len, 1, &tmp[0].len, sizeof(tmp[0]),
NULL, 0, 0, 0, INIT_VLC_USE_NEW_STATIC, NULL); NULL, 0, 0, 0, INIT_VLC_USE_NEW_STATIC, NULL);
av_assert1(dv_vlc.table_size == 1664); av_assert1(dv_vlc.table_size == 1664);
@ -189,8 +188,9 @@ static av_cold void dv_init_static(void)
run = 0; run = 0;
level = code; level = code;
} else { } else {
run = new_dv_vlc_run[code] + 1; av_assert1(i <= code + offset);
level = new_dv_vlc_level[code]; run = tmp[code].run + 1;
level = tmp[code].level;
} }
dv_rl_vlc[i].len = len; dv_rl_vlc[i].len = len;
dv_rl_vlc[i].level = level; dv_rl_vlc[i].level = level;