Commit 587a13169d for ffmpeg
commit 587a13169db98cac5485c91a6628cd6a879c808d
Author: Wu Jianhua <toqsxw@outlook.com>
Date: Fri Nov 27 23:37:55 2026 +0800
avcodec/vvc: widen the final 64-point inverse transform sums
The 64-point DCT-II has at most 32 nonzero inputs. Each even or
odd half sums at most 16 products and fits in int with 20-bit
transform coefficients. The final sum or difference can exceed
INT_MAX. Widen it before rounding and shifting.
Signed-off-by: Wu Jianhua <toqsxw@outlook.com>
diff --git a/libavcodec/vvc/intra.c b/libavcodec/vvc/intra.c
index 1f32f89239..aeb45e70dc 100644
--- a/libavcodec/vvc/intra.c
+++ b/libavcodec/vvc/intra.c
@@ -449,13 +449,24 @@ static void itx_2d(const VVCFrameContext *fc, TransformBlock *tb, const enum VVC
return;
}
- for (int x = 0; x < nzw; x++)
- fc->vvcdsp.itx.itx[trv][tb->log2_tb_height - 1](tb->coeffs + x, w, nzh);
- scale_clip(tb->coeffs, nzw, w, h, shift[0], sps->log2_transform_range);
+ if (h == 64 && sps->log2_transform_range > 18) {
+ for (int x = 0; x < nzw; x++)
+ ff_vvc_inv_dct2_64_scaled(tb->coeffs + x, w, nzh,
+ shift[0], sps->log2_transform_range);
+ } else {
+ for (int x = 0; x < nzw; x++)
+ fc->vvcdsp.itx.itx[trv][tb->log2_tb_height - 1](tb->coeffs + x, w, nzh);
+ scale_clip(tb->coeffs, nzw, w, h, shift[0], sps->log2_transform_range);
+ }
- for (int y = 0; y < h; y++)
- fc->vvcdsp.itx.itx[trh][tb->log2_tb_width - 1](tb->coeffs + y * w, 1, nzw);
- scale(tb->coeffs, tb->coeffs, w, h, shift[1]);
+ if (w == 64 && sps->log2_transform_range > 18) {
+ for (int y = 0; y < h; y++)
+ ff_vvc_inv_dct2_64_scaled(tb->coeffs + y * w, 1, nzw, shift[1], 0);
+ } else {
+ for (int y = 0; y < h; y++)
+ fc->vvcdsp.itx.itx[trh][tb->log2_tb_width - 1](tb->coeffs + y * w, 1, nzw);
+ scale(tb->coeffs, tb->coeffs, w, h, shift[1]);
+ }
}
static void itx_1d(const VVCFrameContext *fc, TransformBlock *tb, const enum VVCTxType trh, const enum VVCTxType trv)
@@ -477,11 +488,16 @@ static void itx_1d(const VVCFrameContext *fc, TransformBlock *tb, const enum VVC
return;
}
- if (w > 1)
- fc->vvcdsp.itx.itx[trh][tb->log2_tb_width - 1](tb->coeffs, 1, nzw);
- else
- fc->vvcdsp.itx.itx[trv][tb->log2_tb_height - 1](tb->coeffs, 1, nzh);
- scale(tb->coeffs, tb->coeffs, w, h, 6 + sps->log2_transform_range - sps->bit_depth);
+ if ((w == 64 || h == 64) && sps->log2_transform_range > 18) {
+ ff_vvc_inv_dct2_64_scaled(tb->coeffs, 1, w > 1 ? nzw : nzh,
+ 6 + sps->log2_transform_range - sps->bit_depth, 0);
+ } else {
+ if (w > 1)
+ fc->vvcdsp.itx.itx[trh][tb->log2_tb_width - 1](tb->coeffs, 1, nzw);
+ else
+ fc->vvcdsp.itx.itx[trv][tb->log2_tb_height - 1](tb->coeffs, 1, nzh);
+ scale(tb->coeffs, tb->coeffs, w, h, 6 + sps->log2_transform_range - sps->bit_depth);
+ }
}
static void transform_bdpcm(TransformBlock *tb, const VVCLocalContext *lc, const CodingUnit *cu)
diff --git a/libavcodec/vvc/itx_1d.c b/libavcodec/vvc/itx_1d.c
index c84e39440a..175138c3f2 100644
--- a/libavcodec/vvc/itx_1d.c
+++ b/libavcodec/vvc/itx_1d.c
@@ -456,7 +456,8 @@ transMatrix[64][64] = {
}
*/
-void ff_vvc_inv_dct2_64(int *coeffs, const ptrdiff_t stride, const size_t nz)
+static av_always_inline void inv_dct2_64(int *coeffs, const ptrdiff_t stride, const size_t nz,
+ const int shift, const int clip_bits)
{
const int aa = 64, ab = 83, ac = 36, ad = 89, ae = 75, af = 50, ag = 18, ah = 90;
const int ai = 87, aj = 80, ak = 70, al = 57, am = 43, an = 25, ao = 9, ap = 90;
@@ -575,6 +576,20 @@ void ff_vvc_inv_dct2_64(int *coeffs, const ptrdiff_t stride, const size_t nz)
cj * x1 + G2(-cg * x3) + G4(+cd * x5 - ca * x7) + G8(+bx * x9 - bu * x11 + br * x13 - bo * x15) + G16(+bl * x17 - bi * x19 + bf * x21 - bh * x23 + bk * x25 - bn * x27 + bq * x29 - bt * x31),
ck * x1 + G2(-cj * x3) + G4(+ci * x5 - ch * x7) + G8(+cg * x9 - cf * x11 + ce * x13 - cd * x15) + G16(+cc * x17 - cb * x19 + ca * x21 - bz * x23 + by * x25 - bx * x27 + bw * x29 - bv * x31),
};
+
+ if (shift) {
+ const int add = 1 << (shift - 1);
+
+ for (int i = 0; i < 32; i++) {
+ const int a = ((int64_t)E[i] + O[i] + add) >> shift;
+ const int b = ((int64_t)E[i] - O[i] + add) >> shift;
+
+ coeffs[i * stride] = clip_bits ? av_clip_intp2(a, clip_bits) : a;
+ coeffs[(63 - i) * stride] = clip_bits ? av_clip_intp2(b, clip_bits) : b;
+ }
+ return;
+ }
+
coeffs[0 * stride] = E[0 ] + O[0 ];
coeffs[1 * stride] = E[1 ] + O[1 ];
coeffs[2 * stride] = E[2 ] + O[2 ];
@@ -641,6 +656,17 @@ void ff_vvc_inv_dct2_64(int *coeffs, const ptrdiff_t stride, const size_t nz)
coeffs[63 * stride] = E[0] - O[0];
}
+void ff_vvc_inv_dct2_64(int *coeffs, const ptrdiff_t stride, const size_t nz)
+{
+ inv_dct2_64(coeffs, stride, nz, 0, 0);
+}
+
+void ff_vvc_inv_dct2_64_scaled(int *coeffs, const ptrdiff_t stride, const size_t nz,
+ const int shift, const int clip_bits)
+{
+ inv_dct2_64(coeffs, stride, nz, shift, clip_bits);
+}
+
static void matrix_mul(int *coeffs, const ptrdiff_t stride, const int8_t* matrix, const int size, const size_t nz)
{
//for dst7 and dct8, coeffs > 16 are zero out
diff --git a/libavcodec/vvc/itx_1d.h b/libavcodec/vvc/itx_1d.h
index 59495827b8..ea18f08c8d 100644
--- a/libavcodec/vvc/itx_1d.h
+++ b/libavcodec/vvc/itx_1d.h
@@ -46,6 +46,9 @@ vvc_itx_1d_fn(ff_vvc_inv_dct8_16);
vvc_itx_1d_fn(ff_vvc_inv_dct8_32);
+void ff_vvc_inv_dct2_64_scaled(int *coeffs, ptrdiff_t stride, size_t nz,
+ int shift, int clip_bits);
+
void ff_vvc_inv_lfnst_1d(int *v, const int *u, int no_zero_size, int n_tr_s,
int pred_mode_intra, int lfnst_idx, int log2_transform_range);