Commit eb99aebbed for ffmpeg

commit eb99aebbed6edfca116442fdec8da64fdec17241
Author: Wu Jianhua <toqsxw@outlook.com>
Date:   Tue Apr 15 04:47:43 2025 +0800

    avcodec/vvc/inter_template: fix apply_bdof function for high bitdepth

    This commit fixed the Bi-directional optical flow prediction process

    Signed-off-by: Wu Jianhua <toqsxw@outlook.com>

diff --git a/libavcodec/vvc/inter_template.c b/libavcodec/vvc/inter_template.c
index 9a2f814040..7f85a644f8 100644
--- a/libavcodec/vvc/inter_template.c
+++ b/libavcodec/vvc/inter_template.c
@@ -397,9 +397,9 @@ static void FUNC(apply_prof_uni_w)(uint8_t *_dst, const ptrdiff_t _dst_stride,
     }
 }

-static void FUNC(derive_bdof_vx_vy)(const int16_t *_src0, const int16_t *_src1,
+static void FUNC(derive_bdof_vx_vy)(const tpixel *_src0, const tpixel *_src1,
     const int pad_left, const int pad_top, const int pad_right, const int pad_bottom,
-    const int16_t **gradient_h, const int16_t **gradient_v,
+    const tpixel **gradient_h, const tpixel **gradient_v,
     int* vx, int* vy)
 {
     const int shift2 = 4;
@@ -407,17 +407,22 @@ static void FUNC(derive_bdof_vx_vy)(const int16_t *_src0, const int16_t *_src1,
     const int thres = 1 << 4;
     int sgx2 = 0, sgy2 = 0, sgxgy = 0, sgxdi = 0, sgydi = 0;

+    const tpixel *gh0 = gradient_h[0];
+    const tpixel *gv0 = gradient_v[0];
+    const tpixel *gh1 = gradient_h[1];
+    const tpixel *gv1 = gradient_v[1];
+
     for (int y = -1; y < BDOF_MIN_BLOCK_SIZE + 1; y++) {
-        const int dy        = y + (pad_top && y < 0) - (pad_bottom && y == BDOF_MIN_BLOCK_SIZE);         // we pad for the first and last row
-        const int16_t *src0 = _src0 + dy * MAX_PB_SIZE;
-        const int16_t *src1 = _src1 + dy * MAX_PB_SIZE;
+        const int dy       = y + (pad_top && y < 0) - (pad_bottom && y == BDOF_MIN_BLOCK_SIZE);         // we pad for the first and last row
+        const tpixel *src0 = _src0 + dy * MAX_PB_SIZE;
+        const tpixel *src1 = _src1 + dy * MAX_PB_SIZE;

         for (int x = -1; x < BDOF_MIN_BLOCK_SIZE + 1; x++) {
             const int dx    = x + (pad_left && x < 0) - (pad_right && x == BDOF_MIN_BLOCK_SIZE);         // we pad for the first and last col
             const int diff  = (src0[dx] >> shift2) - (src1[dx] >> shift2);
             const int idx   = BDOF_BLOCK_SIZE * dy + dx;
-            const int temph = (gradient_h[0][idx] + gradient_h[1][idx]) >> shift3;
-            const int tempv = (gradient_v[0][idx] + gradient_v[1][idx]) >> shift3;
+            const int temph = (gh0[idx] + gh1[idx]) >> shift3;
+            const int tempv = (gv0[idx] + gv1[idx]) >> shift3;

             sgx2 += FFABS(temph);
             sgy2 += FFABS(tempv);
@@ -430,11 +435,11 @@ static void FUNC(derive_bdof_vx_vy)(const int16_t *_src0, const int16_t *_src1,
     *vy = sgy2 > 0 ? av_clip(((sgydi * (1 << 2)) - ((*vx * sgxgy) >> 1)) >> av_log2(sgy2), -thres + 1, thres - 1) : 0;
 }

-static void FUNC(apply_bdof_min_block)(pixel* dst, const ptrdiff_t dst_stride, const int16_t *src0, const int16_t *src1,
-    const int16_t **gh, const int16_t **gv, const int vx, const int vy)
+static void FUNC(apply_bdof_min_block)(pixel* dst, const ptrdiff_t dst_stride, const tpixel *src0, const tpixel *src1,
+    const tpixel **gh, const tpixel **gv, const int vx, const int vy)
 {
-    const int shift4 = 15 - BIT_DEPTH;
-    const int offset4 = 1 << (shift4 - 1);
+    const int shift4   = FFMAX(3, 15 - BIT_DEPTH);
+    const int offset4  = 1 << (shift4 - 1);

     for (int y = 0; y < BDOF_MIN_BLOCK_SIZE; y++) {
         for (int x = 0; x < BDOF_MIN_BLOCK_SIZE; x++) {
@@ -451,27 +456,31 @@ static void FUNC(apply_bdof_min_block)(pixel* dst, const ptrdiff_t dst_stride, c
 static void FUNC(apply_bdof)(uint8_t *_dst, const ptrdiff_t _dst_stride, const int16_t *_src0, const int16_t *_src1,
     const int block_w, const int block_h)
 {
-    int16_t gradient_h[2][BDOF_BLOCK_SIZE * BDOF_BLOCK_SIZE];
-    int16_t gradient_v[2][BDOF_BLOCK_SIZE * BDOF_BLOCK_SIZE];
+    tpixel gradient_h[2][BDOF_BLOCK_SIZE * BDOF_BLOCK_SIZE];
+    tpixel gradient_v[2][BDOF_BLOCK_SIZE * BDOF_BLOCK_SIZE];
     int vx, vy;
     const ptrdiff_t dst_stride  = _dst_stride / sizeof(pixel);
-    pixel* dst                  = (pixel*)_dst;
+    const tpixel *src0          = (const tpixel *)_src0;
+    const tpixel *src1          = (const tpixel *)_src1;
+    pixel *dst                  = (pixel *)_dst;

     FUNC(prof_grad_filter)(gradient_h[0], gradient_v[0], BDOF_BLOCK_SIZE,
-        _src0, MAX_PB_SIZE, block_w, block_h);
+        src0, MAX_PB_SIZE, block_w, block_h);
     FUNC(prof_grad_filter)(gradient_h[1], gradient_v[1], BDOF_BLOCK_SIZE,
-        _src1, MAX_PB_SIZE, block_w, block_h);
+        src1, MAX_PB_SIZE, block_w, block_h);

     for (int y = 0; y < block_h; y += BDOF_MIN_BLOCK_SIZE) {
         for (int x = 0; x < block_w; x += BDOF_MIN_BLOCK_SIZE) {
-            const int16_t* src0 = _src0 + y * MAX_PB_SIZE + x;
-            const int16_t* src1 = _src1 + y * MAX_PB_SIZE + x;
-            pixel *d            = dst + x;
-            const int idx       = BDOF_BLOCK_SIZE * y  + x;
-            const int16_t* gh[] = { gradient_h[0] + idx, gradient_h[1] + idx };
-            const int16_t* gv[] = { gradient_v[0] + idx, gradient_v[1] + idx };
-            FUNC(derive_bdof_vx_vy)(src0, src1, !x, !y, x + BDOF_MIN_BLOCK_SIZE == block_w, y + BDOF_MIN_BLOCK_SIZE == block_h, gh, gv, &vx, &vy);
-            FUNC(apply_bdof_min_block)(d, dst_stride, src0, src1, gh, gv, vx, vy);
+            const tpixel *s0   = src0 + y * MAX_PB_SIZE + x;
+            const tpixel *s1   = src1 + y * MAX_PB_SIZE + x;
+
+            pixel *d           = dst + x;
+            const int idx      = BDOF_BLOCK_SIZE * y + x;
+            const tpixel *gh[] = { gradient_h[0] + idx, gradient_h[1] + idx };
+            const tpixel *gv[] = { gradient_v[0] + idx, gradient_v[1] + idx };
+
+            FUNC(derive_bdof_vx_vy)(s0, s1, !x, !y, x + BDOF_MIN_BLOCK_SIZE == block_w, y + BDOF_MIN_BLOCK_SIZE == block_h, gh, gv, &vx, &vy);
+            FUNC(apply_bdof_min_block)(d, dst_stride, s0, s1, gh, gv, vx, vy);
         }
         dst += BDOF_MIN_BLOCK_SIZE * dst_stride;
     }