FFmpeg
inter_template.c
Go to the documentation of this file.
1 /*
2  * VVC inter prediction DSP
3  *
4  * Copyright (C) 2022 Nuo Mi
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
24 
25 #define TMP_STRIDE EDGE_EMU_BUFFER_STRIDE
26 static void av_always_inline FUNC(put_scaled)(uint8_t *_dst, const ptrdiff_t _dst_stride,
27  const uint8_t *const _src, ptrdiff_t _src_stride, const int src_height,
28  const int _x, const int _y, const int dx, const int dy,
29  const int height, const int8_t *hf, const int8_t *vf, const int width, const int is_uni, const int is_chroma)
30 {
31  int16_t tmp_array[TMP_STRIDE * MAX_PB_SIZE];
32  int16_t *tmp = tmp_array;
33  pixel *dst = (pixel*)_dst;
34  int16_t *dst16 = (int16_t*)_dst;
35  const ptrdiff_t dst_stride = _dst_stride / sizeof(pixel);
36  const ptrdiff_t src_stride = _src_stride / sizeof(pixel);
37  const int shift = FFMAX(2, 14 - BIT_DEPTH);
38  const int offset = 1 << (shift - 1);
39  const int taps = is_chroma ? VVC_INTER_CHROMA_TAPS : VVC_INTER_LUMA_TAPS;
40  const int extra = is_chroma ? CHROMA_EXTRA : LUMA_EXTRA;
41  const int extra_before = is_chroma ? CHROMA_EXTRA_BEFORE : LUMA_EXTRA_BEFORE;
42  const int shift1 = 6 - is_chroma;
43  const int shift2 = 4 + is_chroma;
44  const int x0 = SCALED_INT(_x);
45  const int y0 = SCALED_INT(_y);
46 
47  for (int i = 0; i < width; i++) {
48  const int tx = _x + dx * i;
49  const int x = SCALED_INT(tx) - x0;
50  const int mx = av_mod_uintp2(tx >> shift1, shift2);
51  const int8_t *filter = hf + mx * taps;
52  const pixel *src = (pixel*)_src - extra_before * src_stride;
53 
54  for (int j = 0; j < src_height + extra; j++) {
55  tmp[j] = (is_chroma ? CHROMA_FILTER(src, 1) : LUMA_FILTER(src, 1)) >> (BIT_DEPTH - 8);
56  src += src_stride;
57  }
58  tmp += TMP_STRIDE;
59  }
60 
61  for (int i = 0; i < height; i++) {
62  const int ty = _y + dy * i;
63  const int x = SCALED_INT(ty) - y0;
64  const int mx = av_mod_uintp2(ty >> shift1, shift2);
65  const int8_t *filter = vf + mx * taps;
66 
67  tmp = tmp_array + extra_before;
68  for (int j = 0; j < width; j++) {
69  const int val = (is_chroma ? CHROMA_FILTER(tmp, 1) : LUMA_FILTER(tmp, 1)) >> 6;
70  if (is_uni)
71  dst[j] = av_clip_pixel((val + offset) >> shift);
72  else
73  dst16[j] = val;
74  tmp += TMP_STRIDE;
75  }
76  if (is_uni)
77  dst += dst_stride;
78  else
79  dst16 += dst_stride;
80  }
81 }
82 
83 static void FUNC(put_luma_scaled)(int16_t *_dst,
84  const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
85  const int x, const int y, const int dx, const int dy,
86  const int height, const int8_t *hf, const int8_t *vf, const int width)
87 {
88  FUNC(put_scaled)((uint8_t *)_dst, MAX_PB_SIZE * sizeof(pixel), _src, _src_stride, src_height, x, y, dx, dy, height, hf, vf, width, 0, 0);
89 }
90 
91 static void FUNC(put_chroma_scaled)(int16_t *_dst,
92  const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
93  const int x, const int y, const int dx, const int dy,
94  const int height, const int8_t *hf, const int8_t *vf, const int width)
95 {
96  FUNC(put_scaled)((uint8_t *)_dst, MAX_PB_SIZE * sizeof(pixel), _src, _src_stride, src_height, x, y, dx, dy, height, hf, vf, width, 0, 1);
97 }
98 
99 static void FUNC(put_uni_luma_scaled)(uint8_t *_dst, const ptrdiff_t _dst_stride,
100  const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
101  const int x, const int y, const int dx, const int dy,
102  const int height, const int8_t *hf, const int8_t *vf, const int width)
103 {
104  FUNC(put_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, x, y, dx, dy, height, hf, vf, width, 1, 0);
105 }
106 
107 static void FUNC(put_uni_chroma_scaled)(uint8_t *_dst, const ptrdiff_t _dst_stride,
108  const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
109  const int x, const int y, const int dx, const int dy,
110  const int height, const int8_t *hf, const int8_t *vf, const int width)
111 {
112  FUNC(put_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, x, y, dx, dy, height, hf, vf, width, 1, 1);
113 }
114 
115 static void av_always_inline FUNC(put_uni_w_scaled)(uint8_t *_dst, const ptrdiff_t _dst_stride,
116  const uint8_t *const _src, ptrdiff_t _src_stride, const int src_height,
117  const int _x, const int _y, const int dx, const int dy, const int denom, const int wx, const int _ox,
118  const int height, const int8_t *hf, const int8_t *vf, const int width, const int is_chroma)
119 {
120  int16_t tmp_array[TMP_STRIDE * MAX_PB_SIZE];
121  int16_t *tmp = tmp_array;
122  pixel *dst = (pixel*)_dst;
123  const ptrdiff_t dst_stride = _dst_stride / sizeof(pixel);
124  const ptrdiff_t src_stride = _src_stride / sizeof(pixel);
125  const int shift = FFMAX(2, 14 - BIT_DEPTH);
126  const int offset = 1 << (shift - 1);
127  const int ox = _ox * (1 << (BIT_DEPTH - 8));
128  const int taps = is_chroma ? VVC_INTER_CHROMA_TAPS : VVC_INTER_LUMA_TAPS;
129  const int extra = is_chroma ? CHROMA_EXTRA : LUMA_EXTRA;
130  const int extra_before = is_chroma ? CHROMA_EXTRA_BEFORE : LUMA_EXTRA_BEFORE;
131  const int shift1 = 6 - is_chroma;
132  const int shift2 = 4 + is_chroma;
133  const int x0 = SCALED_INT(_x);
134  const int y0 = SCALED_INT(_y);
135 
136  for (int i = 0; i < width; i++) {
137  const int tx = _x + dx * i;
138  const int x = SCALED_INT(tx) - x0;
139  const int mx = av_mod_uintp2(tx >> shift1, shift2);
140  const int8_t *filter = hf + mx * taps;
141  const pixel *src = (pixel*)_src - extra_before * src_stride;
142 
143  for (int j = 0; j < src_height + extra; j++) {
144  tmp[j] = (is_chroma ? CHROMA_FILTER(src, 1) : LUMA_FILTER(src, 1)) >> (BIT_DEPTH - 8);
145  src += src_stride;
146  }
147  tmp += TMP_STRIDE;
148  }
149 
150  for (int i = 0; i < height; i++) {
151  const int ty = _y + dy * i;
152  const int x = SCALED_INT(ty) - y0;
153  const int mx = av_mod_uintp2(ty >> shift1, shift2);
154  const int8_t *filter = vf + mx * taps;
155 
156  tmp = tmp_array + extra_before;
157  for (int j = 0; j < width; j++) {
158  const int val = (is_chroma ? CHROMA_FILTER(tmp, 1) : LUMA_FILTER(tmp, 1)) >> 6;
159  dst[j] = av_clip_pixel(((wx * val + offset) >> shift) + ox);
160  tmp += TMP_STRIDE;
161  }
162  dst += dst_stride;
163  }
164 }
165 
166 static void FUNC(put_uni_luma_w_scaled)(uint8_t *_dst, const ptrdiff_t _dst_stride,
167  const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
168  const int x, const int y, const int dx, const int dy, const int denom, const int wx, const int ox,
169  const int height, const int8_t *hf, const int8_t *vf, const int width)
170 {
171  FUNC(put_uni_w_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, x, y, dx, dy, denom, wx, ox, height, hf, vf, width, 0);
172 }
173 
174 static void FUNC(put_uni_chroma_w_scaled)(uint8_t *_dst, const ptrdiff_t _dst_stride,
175  const uint8_t *_src, ptrdiff_t _src_stride, const int src_height,
176  const int x, const int y, const int dx, const int dy, const int denom, const int wx, const int ox,
177  const int height, const int8_t *hf, const int8_t *vf, const int width)
178 {
179  FUNC(put_uni_w_scaled)(_dst, _dst_stride, _src, _src_stride, src_height, x, y, dx, dy, denom, wx, ox, height, hf, vf, width, 1);
180 }
181 
182 #undef TMP_STRIDE
183 
184 static void FUNC(avg)(uint8_t *_dst, const ptrdiff_t _dst_stride,
185  const int16_t *src0, const int16_t *src1, const int width, const int height)
186 {
187  pixel *dst = (pixel*)_dst;
188  const ptrdiff_t dst_stride = _dst_stride / sizeof(pixel);
189  const int shift = FFMAX(3, 15 - BIT_DEPTH);
190  const int offset = 1 << (shift - 1);
191 
192  for (int y = 0; y < height; y++) {
193  for (int x = 0; x < width; x++)
194  dst[x] = av_clip_pixel((src0[x] + src1[x] + offset) >> shift);
195  src0 += MAX_PB_SIZE;
196  src1 += MAX_PB_SIZE;
197  dst += dst_stride;
198  }
199 }
200 
201 static void FUNC(w_avg)(uint8_t *_dst, const ptrdiff_t _dst_stride,
202  const int16_t *src0, const int16_t *src1, const int width, const int height,
203  const int denom, const int w0, const int w1, const int o0, const int o1)
204 {
205  pixel *dst = (pixel*)_dst;
206  const ptrdiff_t dst_stride = _dst_stride / sizeof(pixel);
207  const int shift = denom + FFMAX(3, 15 - BIT_DEPTH);
208  const int offset = ((o0 + o1) * (1 << (BIT_DEPTH - 8)) + 1) * (1 << (shift - 1));
209 
210  for (int y = 0; y < height; y++) {
211  for (int x = 0; x < width; x++)
212  dst[x] = av_clip_pixel((src0[x] * w0 + src1[x] * w1 + offset) >> shift);
213  src0 += MAX_PB_SIZE;
214  src1 += MAX_PB_SIZE;
215  dst += dst_stride;
216  }
217 }
218 
219 static void FUNC(put_ciip)(uint8_t *_dst, const ptrdiff_t _dst_stride,
220  const int width, const int height,
221  const uint8_t *_inter, const ptrdiff_t _inter_stride, const int intra_weight)
222 {
223  pixel *dst = (pixel *)_dst;
224  pixel *inter = (pixel *)_inter;
225  const size_t dst_stride = _dst_stride / sizeof(pixel);
226  const size_t inter_stride = _inter_stride / sizeof(pixel);
227  const int inter_weight = 4 - intra_weight;
228 
229  for (int y = 0; y < height; y++) {
230  for (int x = 0; x < width; x++)
231  dst[x] = (dst[x] * intra_weight + inter[x] * inter_weight + 2) >> 2;
232  dst += dst_stride;
233  inter += inter_stride;
234  }
235 }
236 
237 static void FUNC(put_gpm)(uint8_t *_dst, ptrdiff_t dst_stride,
238  const int width, const int height,
239  const int16_t *src0, const int16_t *src1,
240  const uint8_t *weights, const int step_x, const int step_y)
241 {
242  const int shift = FFMAX(5, 17 - BIT_DEPTH);
243  const int offset = 1 << (shift - 1);
244  pixel *dst = (pixel *)_dst;
245 
246  dst_stride /= sizeof(pixel);
247  for (int y = 0; y < height; y++) {
248  for (int x = 0; x < width; x++) {
249  const uint8_t w = weights[x * step_x];
250  dst[x] = av_clip_pixel((src0[x] * w + src1[x] * (8 - w) + offset) >> shift);
251  }
252  dst += dst_stride;
253  src0 += MAX_PB_SIZE;
254  src1 += MAX_PB_SIZE;
255  weights += step_y;
256  }
257 }
258 
259 //8.5.6.3.3 Luma integer sample fetching process, add one extra pad line
260 static void FUNC(bdof_fetch_samples)(int16_t *_dst, const uint8_t *_src, const ptrdiff_t _src_stride,
261  const int x_frac, const int y_frac, const int width, const int height)
262 {
263  const int x_off = (x_frac >> 3) - 1;
264  const int y_off = (y_frac >> 3) - 1;
265  const ptrdiff_t src_stride = _src_stride / sizeof(pixel);
266  const pixel *src = (pixel*)_src + (x_off) + y_off * src_stride;
267  int16_t *dst = _dst - 1 - MAX_PB_SIZE;
268  const int shift = 14 - BIT_DEPTH;
269  const int bdof_width = width + 2 * BDOF_BORDER_EXT;
270 
271  // top
272  for (int i = 0; i < bdof_width; i++)
273  dst[i] = src[i] << shift;
274 
275  dst += MAX_PB_SIZE;
276  src += src_stride;
277 
278  for (int i = 0; i < height; i++) {
279  dst[0] = src[0] << shift;
280  dst[1 + width] = src[1 + width] << shift;
281  dst += MAX_PB_SIZE;
282  src += src_stride;
283  }
284  for (int i = 0; i < bdof_width; i++)
285  dst[i] = src[i] << shift;
286 }
287 
288 //8.5.6.3.3 Luma integer sample fetching process
289 static void FUNC(fetch_samples)(int16_t *_dst, const uint8_t *_src, const ptrdiff_t _src_stride, const int x_frac, const int y_frac)
290 {
291  FUNC(bdof_fetch_samples)(_dst, _src, _src_stride, x_frac, y_frac, AFFINE_MIN_BLOCK_SIZE, AFFINE_MIN_BLOCK_SIZE);
292 }
293 
294 static void FUNC(prof_grad_filter)(int16_t *_gradient_h, int16_t *_gradient_v, const ptrdiff_t gradient_stride,
295  const int16_t *_src, const ptrdiff_t src_stride, const int width, const int height, const int pad)
296 {
297  const int shift = 6;
298  const int16_t *src = _src;
299  int16_t *gradient_h = _gradient_h + pad * (1 + gradient_stride);
300  int16_t *gradient_v = _gradient_v + pad * (1 + gradient_stride);
301 
302  for (int y = 0; y < height; y++) {
303  const int16_t *p = src;
304  for (int x = 0; x < width; x++) {
305  gradient_h[x] = (p[1] >> shift) - (p[-1] >> shift);
306  gradient_v[x] = (p[src_stride] >> shift) - (p[-src_stride] >> shift);
307  p++;
308  }
309  gradient_h += gradient_stride;
310  gradient_v += gradient_stride;
311  src += src_stride;
312  }
313  if (pad) {
314  pad_int16(_gradient_h + 1 + gradient_stride, gradient_stride, width, height);
315  pad_int16(_gradient_v + 1 + gradient_stride, gradient_stride, width, height);
316  }
317 }
318 
319 static void FUNC(apply_prof)(int16_t *dst, const int16_t *src, const int16_t *diff_mv_x, const int16_t *diff_mv_y)
320 {
321  const int limit = (1 << FFMAX(13, BIT_DEPTH + 1)); ///< dILimit
322 
323  int16_t gradient_h[AFFINE_MIN_BLOCK_SIZE * AFFINE_MIN_BLOCK_SIZE];
324  int16_t gradient_v[AFFINE_MIN_BLOCK_SIZE * AFFINE_MIN_BLOCK_SIZE];
326 
327  for (int y = 0; y < AFFINE_MIN_BLOCK_SIZE; y++) {
328  for (int x = 0; x < AFFINE_MIN_BLOCK_SIZE; x++) {
329  const int o = y * AFFINE_MIN_BLOCK_SIZE + x;
330  const int di = gradient_h[o] * diff_mv_x[o] + gradient_v[o] * diff_mv_y[o];
331  const int val = src[x] + av_clip(di, -limit, limit - 1);
332  dst[x] = val;
333 
334  }
335  src += MAX_PB_SIZE;
336  dst += MAX_PB_SIZE;
337  }
338 }
339 
340 static void FUNC(apply_prof_uni)(uint8_t *_dst, const ptrdiff_t _dst_stride, const int16_t *src, const int16_t *diff_mv_x, const int16_t *diff_mv_y)
341 {
342  const int limit = (1 << FFMAX(13, BIT_DEPTH + 1)); ///< dILimit
343  pixel *dst = (pixel*)_dst;
344  const ptrdiff_t dst_stride = _dst_stride / sizeof(pixel);
345  const int shift = 14 - BIT_DEPTH;
346 #if BIT_DEPTH < 14
347  const int offset = 1 << (shift - 1);
348 #else
349  const int offset = 0;
350 #endif
351  int16_t gradient_h[AFFINE_MIN_BLOCK_SIZE * AFFINE_MIN_BLOCK_SIZE];
352  int16_t gradient_v[AFFINE_MIN_BLOCK_SIZE * AFFINE_MIN_BLOCK_SIZE];
353 
355 
356  for (int y = 0; y < AFFINE_MIN_BLOCK_SIZE; y++) {
357  for (int x = 0; x < AFFINE_MIN_BLOCK_SIZE; x++) {
358  const int o = y * AFFINE_MIN_BLOCK_SIZE + x;
359  const int di = gradient_h[o] * diff_mv_x[o] + gradient_v[o] * diff_mv_y[o];
360  const int val = src[x] + av_clip(di, -limit, limit - 1);
361  dst[x] = av_clip_pixel((val + offset) >> shift);
362 
363  }
364  src += MAX_PB_SIZE;
365  dst += dst_stride;
366  }
367 }
368 
369 static void FUNC(apply_prof_uni_w)(uint8_t *_dst, const ptrdiff_t _dst_stride,
370  const int16_t *src, const int16_t *diff_mv_x, const int16_t *diff_mv_y,
371  const int denom, const int wx, const int _ox)
372 {
373  const int limit = (1 << FFMAX(13, BIT_DEPTH + 1)); ///< dILimit
374  pixel *dst = (pixel*)_dst;
375  const ptrdiff_t dst_stride = _dst_stride / sizeof(pixel);
376  const int shift = denom + FFMAX(2, 14 - BIT_DEPTH);
377  const int offset = 1 << (shift - 1);
378  const int ox = _ox * (1 << (BIT_DEPTH - 8));
379  int16_t gradient_h[AFFINE_MIN_BLOCK_SIZE * AFFINE_MIN_BLOCK_SIZE];
380  int16_t gradient_v[AFFINE_MIN_BLOCK_SIZE * AFFINE_MIN_BLOCK_SIZE];
381 
383 
384  for (int y = 0; y < AFFINE_MIN_BLOCK_SIZE; y++) {
385  for (int x = 0; x < AFFINE_MIN_BLOCK_SIZE; x++) {
386  const int o = y * AFFINE_MIN_BLOCK_SIZE + x;
387  const int di = gradient_h[o] * diff_mv_x[o] + gradient_v[o] * diff_mv_y[o];
388  const int val = src[x] + av_clip(di, -limit, limit - 1);
389  dst[x] = av_clip_pixel(((val * wx + offset) >> shift) + ox);
390  }
391  src += MAX_PB_SIZE;
392  dst += dst_stride;
393  }
394 }
395 
396 static void FUNC(derive_bdof_vx_vy)(const int16_t *_src0, const int16_t *_src1,
397  const int16_t **gradient_h, const int16_t **gradient_v, ptrdiff_t gradient_stride,
398  int* vx, int* vy)
399 {
400  const int shift2 = 4;
401  const int shift3 = 1;
402  const int thres = 1 << 4;
403  int sgx2 = 0, sgy2 = 0, sgxgy = 0, sgxdi = 0, sgydi = 0;
404  const int16_t *src0 = _src0 - 1 - MAX_PB_SIZE;
405  const int16_t *src1 = _src1 - 1 - MAX_PB_SIZE;
406 
407  for (int y = 0; y < BDOF_GRADIENT_SIZE; y++) {
408  for (int x = 0; x < BDOF_GRADIENT_SIZE; x++) {
409  const int diff = (src0[x] >> shift2) - (src1[x] >> shift2);
410  const int idx = gradient_stride * y + x;
411  const int temph = (gradient_h[0][idx] + gradient_h[1][idx]) >> shift3;
412  const int tempv = (gradient_v[0][idx] + gradient_v[1][idx]) >> shift3;
413  sgx2 += FFABS(temph);
414  sgy2 += FFABS(tempv);
415  sgxgy += VVC_SIGN(tempv) * temph;
416  sgxdi += -VVC_SIGN(temph) * diff;
417  sgydi += -VVC_SIGN(tempv) * diff;
418  }
419  src0 += MAX_PB_SIZE;
420  src1 += MAX_PB_SIZE;
421  }
422  *vx = sgx2 > 0 ? av_clip((sgxdi * (1 << 2)) >> av_log2(sgx2) , -thres + 1, thres - 1) : 0;
423  *vy = sgy2 > 0 ? av_clip(((sgydi * (1 << 2)) - ((*vx * sgxgy) >> 1)) >> av_log2(sgy2), -thres + 1, thres - 1) : 0;
424 }
425 
426 static void FUNC(apply_bdof_min_block)(pixel* dst, const ptrdiff_t dst_stride, const int16_t *src0, const int16_t *src1,
427  const int16_t **gradient_h, const int16_t **gradient_v, const int vx, const int vy)
428 {
429  const int shift4 = 15 - BIT_DEPTH;
430  const int offset4 = 1 << (shift4 - 1);
431 
432  const int16_t* gh[] = { gradient_h[0] + 1 + BDOF_PADDED_SIZE, gradient_h[1] + 1 + BDOF_PADDED_SIZE };
433  const int16_t* gv[] = { gradient_v[0] + 1 + BDOF_PADDED_SIZE, gradient_v[1] + 1 + BDOF_PADDED_SIZE };
434 
435  for (int y = 0; y < BDOF_BLOCK_SIZE; y++) {
436  for (int x = 0; x < BDOF_BLOCK_SIZE; x++) {
437  const int idx = y * BDOF_PADDED_SIZE + x;
438  const int bdof_offset = vx * (gh[0][idx] - gh[1][idx]) + vy * (gv[0][idx] - gv[1][idx]);
439  dst[x] = av_clip_pixel((src0[x] + offset4 + src1[x] + bdof_offset) >> shift4);
440  }
441  dst += dst_stride;
442  src0 += MAX_PB_SIZE;
443  src1 += MAX_PB_SIZE;
444  }
445 }
446 
447 static void FUNC(apply_bdof)(uint8_t *_dst, const ptrdiff_t _dst_stride, int16_t *_src0, int16_t *_src1,
448  const int block_w, const int block_h)
449 {
450  int16_t gradient_h[2][BDOF_PADDED_SIZE * BDOF_PADDED_SIZE];
451  int16_t gradient_v[2][BDOF_PADDED_SIZE * BDOF_PADDED_SIZE];
452  int vx, vy;
453  const ptrdiff_t dst_stride = _dst_stride / sizeof(pixel);
454  pixel* dst = (pixel*)_dst;
455 
456  FUNC(prof_grad_filter)(gradient_h[0], gradient_v[0], BDOF_PADDED_SIZE,
457  _src0, MAX_PB_SIZE, block_w, block_h, 1);
458  pad_int16(_src0, MAX_PB_SIZE, block_w, block_h);
459  FUNC(prof_grad_filter)(gradient_h[1], gradient_v[1], BDOF_PADDED_SIZE,
460  _src1, MAX_PB_SIZE, block_w, block_h, 1);
461  pad_int16(_src1, MAX_PB_SIZE, block_w, block_h);
462 
463  for (int y = 0; y < block_h; y += BDOF_BLOCK_SIZE) {
464  for (int x = 0; x < block_w; x += BDOF_BLOCK_SIZE) {
465  const int16_t* src0 = _src0 + y * MAX_PB_SIZE + x;
466  const int16_t* src1 = _src1 + y * MAX_PB_SIZE + x;
467  pixel *d = dst + x;
468  const int idx = BDOF_PADDED_SIZE * y + x;
469  const int16_t* gh[] = { gradient_h[0] + idx, gradient_h[1] + idx };
470  const int16_t* gv[] = { gradient_v[0] + idx, gradient_v[1] + idx };
471  FUNC(derive_bdof_vx_vy)(src0, src1, gh, gv, BDOF_PADDED_SIZE, &vx, &vy);
472  FUNC(apply_bdof_min_block)(d, dst_stride, src0, src1, gh, gv, vx, vy);
473  }
474  dst += BDOF_BLOCK_SIZE * dst_stride;
475  }
476 }
477 
478 #define DMVR_FILTER(src, stride) \
479  (filter[0] * src[x] + \
480  filter[1] * src[x + stride])
481 
482 //8.5.3.2.2 Luma sample bilinear interpolation process
483 static void FUNC(dmvr)(int16_t *dst, const uint8_t *_src, const ptrdiff_t _src_stride,
484  const int height, const intptr_t mx, const intptr_t my, const int width)
485 {
486  const pixel *src = (const pixel *)_src;
487  const ptrdiff_t src_stride = _src_stride / sizeof(pixel);
488 #if BIT_DEPTH > 10
489  const int shift4 = BIT_DEPTH - 10;
490  const int offset4 = 1 << (shift4 - 1);
491  #define DMVR_SHIFT(s) (((s) + offset4) >> shift4)
492 #else
493  #define DMVR_SHIFT(s) ((s) << (10 - BIT_DEPTH))
494 #endif
495 
496  for (int y = 0; y < height; y++) {
497  for (int x = 0; x < width; x++)
498  dst[x] = DMVR_SHIFT(src[x]);
499  src += src_stride;
500  dst += MAX_PB_SIZE;
501  }
502 #undef DMVR_SHIFT
503 }
504 
505 //8.5.3.2.2 Luma sample bilinear interpolation process
506 static void FUNC(dmvr_h)(int16_t *dst, const uint8_t *_src, const ptrdiff_t _src_stride,
507  const int height, const intptr_t mx, const intptr_t my, const int width)
508 {
509  const pixel *src = (const pixel*)_src;
510  const ptrdiff_t src_stride = _src_stride / sizeof(pixel);
511  const int8_t *filter = ff_vvc_inter_luma_dmvr_filters[mx];
512  const int shift1 = BIT_DEPTH - 6;
513  const int offset1 = 1 << (shift1 - 1);
514 
515  for (int y = 0; y < height; y++) {
516  for (int x = 0; x < width; x++)
517  dst[x] = (DMVR_FILTER(src, 1) + offset1) >> shift1;
518  src += src_stride;
519  dst += MAX_PB_SIZE;
520  }
521 }
522 
523 //8.5.3.2.2 Luma sample bilinear interpolation process
524 static void FUNC(dmvr_v)(int16_t *dst, const uint8_t *_src, const ptrdiff_t _src_stride,
525  const int height, const intptr_t mx, const intptr_t my, const int width)
526 {
527  const pixel *src = (pixel*)_src;
528  const ptrdiff_t src_stride = _src_stride / sizeof(pixel);
529  const int8_t *filter = ff_vvc_inter_luma_dmvr_filters[my];
530  const int shift1 = BIT_DEPTH - 6;
531  const int offset1 = 1 << (shift1 - 1);
532 
533  for (int y = 0; y < height; y++) {
534  for (int x = 0; x < width; x++)
535  dst[x] = (DMVR_FILTER(src, src_stride) + offset1) >> shift1;
536  src += src_stride;
537  dst += MAX_PB_SIZE;
538  }
539 
540 }
541 
542 //8.5.3.2.2 Luma sample bilinear interpolation process
543 static void FUNC(dmvr_hv)(int16_t *dst, const uint8_t *_src, const ptrdiff_t _src_stride,
544  const int height, const intptr_t mx, const intptr_t my, const int width)
545 {
546  int16_t tmp_array[(MAX_PB_SIZE + BILINEAR_EXTRA) * MAX_PB_SIZE];
547  int16_t *tmp = tmp_array;
548  const pixel *src = (const pixel*)_src;
549  const ptrdiff_t src_stride = _src_stride / sizeof(pixel);
550  const int8_t *filter = ff_vvc_inter_luma_dmvr_filters[mx];
551  const int shift1 = BIT_DEPTH - 6;
552  const int offset1 = 1 << (shift1 - 1);
553  const int shift2 = 4;
554  const int offset2 = 1 << (shift2 - 1);
555 
556  src -= BILINEAR_EXTRA_BEFORE * src_stride;
557  for (int y = 0; y < height + BILINEAR_EXTRA; y++) {
558  for (int x = 0; x < width; x++)
559  tmp[x] = (DMVR_FILTER(src, 1) + offset1) >> shift1;
560  src += src_stride;
561  tmp += MAX_PB_SIZE;
562  }
563 
564  tmp = tmp_array + BILINEAR_EXTRA_BEFORE * MAX_PB_SIZE;
566  for (int y = 0; y < height; y++) {
567  for (int x = 0; x < width; x++)
568  dst[x] = (DMVR_FILTER(tmp, MAX_PB_SIZE) + offset2) >> shift2;
569  tmp += MAX_PB_SIZE;
570  dst += MAX_PB_SIZE;
571  }
572 }
573 
574 #define PEL_FUNC(dst, C, idx1, idx2, a) \
575  do { \
576  for (int w = 0; w < 7; w++) \
577  inter->dst[C][w][idx1][idx2] = FUNC(a); \
578  } while (0) \
579 
580 #define DIR_FUNCS(d, C, c) \
581  PEL_FUNC(put_##d, C, 0, 0, put_##d##_pixels); \
582  PEL_FUNC(put_##d, C, 0, 1, put_##d##_##c##_h); \
583  PEL_FUNC(put_##d, C, 1, 0, put_##d##_##c##_v); \
584  PEL_FUNC(put_##d, C, 1, 1, put_##d##_##c##_hv); \
585  PEL_FUNC(put_##d##_w, C, 0, 0, put_##d##_w_pixels); \
586  PEL_FUNC(put_##d##_w, C, 0, 1, put_##d##_##c##_w_h); \
587  PEL_FUNC(put_##d##_w, C, 1, 0, put_##d##_##c##_w_v); \
588  PEL_FUNC(put_##d##_w, C, 1, 1, put_##d##_##c##_w_hv);
589 
590 #define FUNCS(C, c) \
591  PEL_FUNC(put, C, 0, 0, put_pixels); \
592  PEL_FUNC(put, C, 0, 1, put_##c##_h); \
593  PEL_FUNC(put, C, 1, 0, put_##c##_v); \
594  PEL_FUNC(put, C, 1, 1, put_##c##_hv); \
595  DIR_FUNCS(uni, C, c); \
596 
598 {
599  FUNCS(LUMA, luma);
600  FUNCS(CHROMA, chroma);
601 
602  for (int i = 0; i < FF_ARRAY_ELEMS(inter->put_scaled[LUMA]); i++) {
603  inter->put_scaled[LUMA][i] = FUNC(put_luma_scaled);
604  inter->put_scaled[CHROMA][i] = FUNC(put_chroma_scaled);
605  inter->put_uni_scaled[LUMA][i] = FUNC(put_uni_luma_scaled);
606  inter->put_uni_scaled[CHROMA][i] = FUNC(put_uni_chroma_scaled);
607  inter->put_uni_w_scaled[LUMA][i] = FUNC(put_uni_luma_w_scaled);
608  inter->put_uni_w_scaled[CHROMA][i] = FUNC(put_uni_chroma_w_scaled);
609  }
610 
611  inter->avg = FUNC(avg);
612  inter->w_avg = FUNC(w_avg);
613 
614  inter->dmvr[0][0] = FUNC(dmvr);
615  inter->dmvr[0][1] = FUNC(dmvr_h);
616  inter->dmvr[1][0] = FUNC(dmvr_v);
617  inter->dmvr[1][1] = FUNC(dmvr_hv);
618 
619  inter->put_ciip = FUNC(put_ciip);
620  inter->put_gpm = FUNC(put_gpm);
621 
622  inter->fetch_samples = FUNC(fetch_samples);
623  inter->bdof_fetch_samples = FUNC(bdof_fetch_samples);
624  inter->apply_prof = FUNC(apply_prof);
625  inter->apply_prof_uni = FUNC(apply_prof_uni);
626  inter->apply_prof_uni_w = FUNC(apply_prof_uni_w);
627  inter->apply_bdof = FUNC(apply_bdof);
628  inter->prof_grad_filter = FUNC(prof_grad_filter);
629  inter->sad = vvc_sad;
630 }
631 
632 #undef FUNCS
633 #undef PEL_FUNC
634 #undef DMVR_FUNCS
dmvr_hv
static void FUNC() dmvr_hv(int16_t *dst, const uint8_t *_src, const ptrdiff_t _src_stride, const int height, const intptr_t mx, const intptr_t my, const int width)
Definition: inter_template.c:543
av_clip
#define av_clip
Definition: common.h:99
BDOF_BORDER_EXT
#define BDOF_BORDER_EXT
Definition: dsp.c:80
TMP_STRIDE
#define TMP_STRIDE
Definition: inter_template.c:25
BIT_DEPTH
#define BIT_DEPTH
Definition: aom_film_grain.c:61
VVC_SIGN
#define VVC_SIGN(v)
Definition: dsp.c:27
src1
const pixel * src1
Definition: h264pred_template.c:421
BILINEAR_EXTRA
#define BILINEAR_EXTRA
Definition: ctu.h:60
CHROMA_FILTER
#define CHROMA_FILTER(src, stride)
Definition: h2656_inter_template.c:336
av_mod_uintp2
#define av_mod_uintp2
Definition: common.h:126
apply_bdof
static void FUNC() apply_bdof(uint8_t *_dst, const ptrdiff_t _dst_stride, int16_t *_src0, int16_t *_src1, const int block_w, const int block_h)
Definition: inter_template.c:447
ff_vvc_inter_dsp_init
static void FUNC() ff_vvc_inter_dsp_init(VVCInterDSPContext *const inter)
Definition: inter_template.c:597
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
w
uint8_t w
Definition: llviddspenc.c:38
CHROMA_EXTRA_BEFORE
#define CHROMA_EXTRA_BEFORE
Definition: h2656_inter_template.c:24
chroma
static av_always_inline void chroma(WaveformContext *s, AVFrame *in, AVFrame *out, int component, int intensity, int offset_y, int offset_x, int column, int mirror, int jobnr, int nb_jobs)
Definition: vf_waveform.c:1639
bdof_fetch_samples
static void FUNC() bdof_fetch_samples(int16_t *_dst, const uint8_t *_src, const ptrdiff_t _src_stride, const int x_frac, const int y_frac, const int width, const int height)
Definition: inter_template.c:260
filter
void(* filter)(uint8_t *src, int stride, int qscale)
Definition: h263dsp.c:29
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
dmvr
static void FUNC() dmvr(int16_t *dst, const uint8_t *_src, const ptrdiff_t _src_stride, const int height, const intptr_t mx, const intptr_t my, const int width)
Definition: inter_template.c:483
put_scaled
static void av_always_inline FUNC() put_scaled(uint8_t *_dst, const ptrdiff_t _dst_stride, const uint8_t *const _src, ptrdiff_t _src_stride, const int src_height, const int _x, const int _y, const int dx, const int dy, const int height, const int8_t *hf, const int8_t *vf, const int width, const int is_uni, const int is_chroma)
Definition: inter_template.c:26
prof_grad_filter
static void FUNC() prof_grad_filter(int16_t *_gradient_h, int16_t *_gradient_v, const ptrdiff_t gradient_stride, const int16_t *_src, const ptrdiff_t src_stride, const int width, const int height, const int pad)
Definition: inter_template.c:294
put_uni_chroma_scaled
static void FUNC() put_uni_chroma_scaled(uint8_t *_dst, const ptrdiff_t _dst_stride, const uint8_t *_src, ptrdiff_t _src_stride, const int src_height, const int x, const int y, const int dx, const int dy, const int height, const int8_t *hf, const int8_t *vf, const int width)
Definition: inter_template.c:107
h2656_inter_template.c
val
static double val(void *priv, double ch)
Definition: aeval.c:78
SCALED_INT
#define SCALED_INT(pos)
Definition: ctu.h:62
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
VVC_INTER_CHROMA_TAPS
#define VVC_INTER_CHROMA_TAPS
Definition: data.h:54
BDOF_PADDED_SIZE
#define BDOF_PADDED_SIZE
Definition: dsp.c:82
width
#define width
derive_bdof_vx_vy
static void FUNC() derive_bdof_vx_vy(const int16_t *_src0, const int16_t *_src1, const int16_t **gradient_h, const int16_t **gradient_v, ptrdiff_t gradient_stride, int *vx, int *vy)
Definition: inter_template.c:396
apply_prof
static void FUNC() apply_prof(int16_t *dst, const int16_t *src, const int16_t *diff_mv_x, const int16_t *diff_mv_y)
Definition: inter_template.c:319
apply_bdof_min_block
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 **gradient_h, const int16_t **gradient_v, const int vx, const int vy)
Definition: inter_template.c:426
LUMA_EXTRA
#define LUMA_EXTRA
Definition: h2656_inter_template.c:27
put_ciip
static void FUNC() put_ciip(uint8_t *_dst, const ptrdiff_t _dst_stride, const int width, const int height, const uint8_t *_inter, const ptrdiff_t _inter_stride, const int intra_weight)
Definition: inter_template.c:219
put_uni_w_scaled
static void av_always_inline FUNC() put_uni_w_scaled(uint8_t *_dst, const ptrdiff_t _dst_stride, const uint8_t *const _src, ptrdiff_t _src_stride, const int src_height, const int _x, const int _y, const int dx, const int dy, const int denom, const int wx, const int _ox, const int height, const int8_t *hf, const int8_t *vf, const int width, const int is_chroma)
Definition: inter_template.c:115
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:73
FUNCS
#define FUNCS(C, c)
Definition: inter_template.c:590
pixel
uint8_t pixel
Definition: tiny_ssim.c:41
put_uni_luma_w_scaled
static void FUNC() put_uni_luma_w_scaled(uint8_t *_dst, const ptrdiff_t _dst_stride, const uint8_t *_src, ptrdiff_t _src_stride, const int src_height, const int x, const int y, const int dx, const int dy, const int denom, const int wx, const int ox, const int height, const int8_t *hf, const int8_t *vf, const int width)
Definition: inter_template.c:166
LUMA_EXTRA_BEFORE
#define LUMA_EXTRA_BEFORE
Definition: h2656_inter_template.c:26
dmvr_v
static void FUNC() dmvr_v(int16_t *dst, const uint8_t *_src, const ptrdiff_t _src_stride, const int height, const intptr_t mx, const intptr_t my, const int width)
Definition: inter_template.c:524
pad_int16
static void av_always_inline pad_int16(int16_t *_dst, const ptrdiff_t dst_stride, const int width, const int height)
Definition: dsp.c:29
apply_prof_uni
static void FUNC() apply_prof_uni(uint8_t *_dst, const ptrdiff_t _dst_stride, const int16_t *src, const int16_t *diff_mv_x, const int16_t *diff_mv_y)
Definition: inter_template.c:340
vvc_sad
static int vvc_sad(const int16_t *src0, const int16_t *src1, int dx, int dy, const int block_w, const int block_h)
Definition: dsp.c:49
shift
static int shift(int a, int b)
Definition: bonk.c:261
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:165
AFFINE_MIN_BLOCK_SIZE
#define AFFINE_MIN_BLOCK_SIZE
Definition: ctu.h:66
BDOF_BLOCK_SIZE
#define BDOF_BLOCK_SIZE
Definition: dsp.c:83
height
#define height
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
ff_vvc_inter_luma_dmvr_filters
const int8_t ff_vvc_inter_luma_dmvr_filters[VVC_INTER_LUMA_DMVR_FACTS][VVC_INTER_LUMA_DMVR_TAPS]
Definition: data.c:1985
put_uni_luma_scaled
static void FUNC() put_uni_luma_scaled(uint8_t *_dst, const ptrdiff_t _dst_stride, const uint8_t *_src, ptrdiff_t _src_stride, const int src_height, const int x, const int y, const int dx, const int dy, const int height, const int8_t *hf, const int8_t *vf, const int width)
Definition: inter_template.c:99
BDOF_GRADIENT_SIZE
#define BDOF_GRADIENT_SIZE
Definition: dsp.c:84
DMVR_FILTER
#define DMVR_FILTER(src, stride)
Definition: inter_template.c:478
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
DMVR_SHIFT
#define DMVR_SHIFT(s)
MAX_PB_SIZE
#define MAX_PB_SIZE
Definition: hevcdsp.h:32
weights
static const int weights[]
Definition: hevc_pel.c:32
av_always_inline
#define av_always_inline
Definition: attributes.h:49
put_gpm
static void FUNC() put_gpm(uint8_t *_dst, ptrdiff_t dst_stride, const int width, const int height, const int16_t *src0, const int16_t *src1, const uint8_t *weights, const int step_x, const int step_y)
Definition: inter_template.c:237
avg
static void FUNC() avg(uint8_t *_dst, const ptrdiff_t _dst_stride, const int16_t *src0, const int16_t *src1, const int width, const int height)
Definition: inter_template.c:184
shift2
static const uint8_t shift2[6]
Definition: dxa.c:49
limit
static double limit(double x)
Definition: vf_pseudocolor.c:142
dmvr_h
static void FUNC() dmvr_h(int16_t *dst, const uint8_t *_src, const ptrdiff_t _src_stride, const int height, const intptr_t mx, const intptr_t my, const int width)
Definition: inter_template.c:506
CHROMA_EXTRA
#define CHROMA_EXTRA
Definition: h2656_inter_template.c:25
w_avg
static void FUNC() w_avg(uint8_t *_dst, const ptrdiff_t _dst_stride, const int16_t *src0, const int16_t *src1, const int width, const int height, const int denom, const int w0, const int w1, const int o0, const int o1)
Definition: inter_template.c:201
CHROMA
@ CHROMA
Definition: vf_waveform.c:49
av_clip_pixel
#define av_clip_pixel(a)
Definition: bit_depth_template.c:98
FUNC
#define FUNC(a)
Definition: bit_depth_template.c:104
BILINEAR_EXTRA_BEFORE
#define BILINEAR_EXTRA_BEFORE
Definition: ctu.h:58
VVCInterDSPContext
Definition: dsp.h:47
src0
const pixel *const src0
Definition: h264pred_template.c:420
put_luma_scaled
static void FUNC() put_luma_scaled(int16_t *_dst, const uint8_t *_src, ptrdiff_t _src_stride, const int src_height, const int x, const int y, const int dx, const int dy, const int height, const int8_t *hf, const int8_t *vf, const int width)
Definition: inter_template.c:83
put_chroma_scaled
static void FUNC() put_chroma_scaled(int16_t *_dst, const uint8_t *_src, ptrdiff_t _src_stride, const int src_height, const int x, const int y, const int dx, const int dy, const int height, const int8_t *hf, const int8_t *vf, const int width)
Definition: inter_template.c:91
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
d
d
Definition: ffmpeg_filter.c:424
VVC_INTER_LUMA_TAPS
#define VVC_INTER_LUMA_TAPS
Definition: data.h:52
put_uni_chroma_w_scaled
static void FUNC() put_uni_chroma_w_scaled(uint8_t *_dst, const ptrdiff_t _dst_stride, const uint8_t *_src, ptrdiff_t _src_stride, const int src_height, const int x, const int y, const int dx, const int dy, const int denom, const int wx, const int ox, const int height, const int8_t *hf, const int8_t *vf, const int width)
Definition: inter_template.c:174
shift1
static const uint8_t shift1[6]
Definition: dxa.c:48
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
LUMA
#define LUMA
Definition: hevc_filter.c:31
apply_prof_uni_w
static void FUNC() apply_prof_uni_w(uint8_t *_dst, const ptrdiff_t _dst_stride, const int16_t *src, const int16_t *diff_mv_x, const int16_t *diff_mv_y, const int denom, const int wx, const int _ox)
Definition: inter_template.c:369
LUMA_FILTER
#define LUMA_FILTER(src, stride)
Definition: h2656_inter_template.c:87
fetch_samples
static void FUNC() fetch_samples(int16_t *_dst, const uint8_t *_src, const ptrdiff_t _src_stride, const int x_frac, const int y_frac)
Definition: inter_template.c:289