FFmpeg
Loading...
Searching...
No Matches
inter.c
Go to the documentation of this file.
1/*
2 * VVC inter prediction
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#include "libavutil/frame.h"
23
24#include "data.h"
25#include "inter.h"
26#include "mvs.h"
27#include "refs.h"
28
29// +1 is enough, + 32 for asm alignment
30#define PROF_TEMP_OFFSET (MAX_PB_SIZE + 32)
31static const int bcw_w_lut[] = {4, 5, 3, 10, -2};
32
33#define HIGHBD_MULTIPLIER(bd) (((bd) + 2) >> 3)
34
35static void subpic_get_rect(VVCRect *r, const VVCFrame *src_frame, const int subpic_idx, const int is_chroma)
36{
37 const VVCSPS *sps = src_frame->sps;
38 const VVCPPS *pps = src_frame->pps;
39 const int hs = sps->hshift[is_chroma];
40 const int vs = sps->vshift[is_chroma];
41
42 r->l = pps->subpic_x[subpic_idx] >> hs;
43 r->t = pps->subpic_y[subpic_idx] >> vs;
44 r->r = r->l + (pps->subpic_width[subpic_idx] >> hs);
45 r->b = r->t + (pps->subpic_height[subpic_idx] >> vs);
46}
47
48// clip to subblock and subpicture process in 8.5.6.3.2 Luma sample interpolation filtering process
49static void clip_to_subpic(int *x_off, int *y_off, int *pic_width, int *pic_height, const VVCRect *subpic, const VVCRect *sb, const int dmvr_clip)
50{
51 const int l = dmvr_clip ? FFMIN(FFMAX(subpic->l, sb->l), subpic->r - 1) : subpic->l;
52 const int t = dmvr_clip ? FFMIN(FFMAX(subpic->t, sb->t), subpic->b - 1) : subpic->t;
53 const int r = dmvr_clip ? FFMAX(FFMIN(subpic->r, sb->r), subpic->l + 1) : subpic->r;
54 const int b = dmvr_clip ? FFMAX(FFMIN(subpic->b, sb->b), subpic->t + 1) : subpic->b;
55
56 *x_off -= l;
57 *y_off -= t;
58 *pic_width = r - l;
59 *pic_height = b - t;
60}
61
62static void emulated_edge_no_wrap(const VVCLocalContext *lc, uint8_t *dst,
63 const uint8_t **src, ptrdiff_t *src_stride,
64 int x_off, int y_off, const int block_w, const int block_h,
65 const int extra_before, const int extra_after,
66 const VVCRect *subpic, const VVCRect *sb, const int dmvr_clip)
67{
68 const VVCFrameContext *fc = lc->fc;
69 const int extra = extra_before + extra_after;
70 int pic_width, pic_height;
71
72 *src += y_off * *src_stride + (x_off * (1 << fc->ps.sps->pixel_shift));
73
74 clip_to_subpic(&x_off, &y_off, &pic_width, &pic_height, subpic, sb, dmvr_clip);
75
76 if (dmvr_clip || x_off < extra_before || y_off < extra_before ||
77 x_off >= pic_width - block_w - extra_after ||
78 y_off >= pic_height - block_h - extra_after) {
79 const int ps = fc->ps.sps->pixel_shift;
80 const ptrdiff_t edge_emu_stride = EDGE_EMU_BUFFER_STRIDE << ps;
81 const int offset = extra_before * *src_stride + (extra_before << ps);
82 const int buf_offset = extra_before * edge_emu_stride + (extra_before << ps);
83
84 fc->vdsp.emulated_edge_mc(dst, *src - offset, edge_emu_stride, *src_stride,
85 block_w + extra, block_h + extra, x_off - extra_before, y_off - extra_before,
86 pic_width, pic_height);
87
88 *src = dst + buf_offset;
89 *src_stride = edge_emu_stride;
90 }
91}
92
93static void emulated_half(const VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride,
94 const uint8_t *src, const ptrdiff_t src_stride, const int ps,
95 int x_off, int y_off, const int block_w, const int block_h,
96 const VVCRect *subpic,const VVCRect *half_sb, const int dmvr_clip)
97{
98 const VVCFrameContext *fc = lc->fc;
99 int pic_width, pic_height;
100
101 src += y_off * src_stride + x_off * (1 << ps);
102
103 clip_to_subpic(&x_off, &y_off, &pic_width, &pic_height, subpic, half_sb, dmvr_clip);
104
105 fc->vdsp.emulated_edge_mc(dst, src, dst_stride, src_stride,
106 block_w, block_h, x_off, y_off, pic_width, pic_height);
107}
108
109static void sb_set_lr(VVCRect *sb, const int l, const int r)
110{
111 sb->l = l;
112 sb->r = r;
113}
114
115static void sb_wrap(VVCRect *sb, const int wrap)
116{
117 sb_set_lr(sb, sb->l + wrap, sb->r + wrap);
118}
119
120static void emulated_edge(const VVCLocalContext *lc, uint8_t *dst,
121 const uint8_t **src, ptrdiff_t *src_stride, const VVCFrame *src_frame,
122 int x_sb, int y_sb, int x_off, int y_off, int block_w, int block_h, const int wrap_enabled,
123 const int is_chroma, const int extra_before, const int extra_after)
124{
125 const VVCSPS *sps = src_frame->sps;
126 const VVCPPS *pps = src_frame->pps;
127 const int ps = sps->pixel_shift;
128 const int subpic_idx = lc->sc->sh.r->curr_subpic_idx;
129 const int extra = extra_before + extra_after;
130 const int dmvr_clip = x_sb != x_off || y_sb != y_off;
131 const int dmvr_left = FFMAX(x_off, x_sb) - extra_before;
132 const int dmvr_right = FFMIN(x_off, x_sb) + block_w + extra_after;
133 const int left = x_off - extra_before;
134 const int top = y_off - extra_before;
135 const int pic_width = pps->width >> sps->hshift[is_chroma];
136 const int wrap = pps->ref_wraparound_offset << (sps->min_cb_log2_size_y - sps->hshift[is_chroma]);
137 const ptrdiff_t dst_stride = EDGE_EMU_BUFFER_STRIDE << ps;
138 VVCRect sb = { x_sb - extra_before, y_sb - extra_before, x_sb + block_w + extra_after, y_sb + block_h + extra_after };
139 VVCRect subpic;
140
141 subpic_get_rect(&subpic, src_frame, subpic_idx, is_chroma);
142
143 if (!wrap_enabled || (dmvr_left >= 0 && dmvr_right <= pic_width)) {
144 emulated_edge_no_wrap(lc, dst, src, src_stride,
145 x_off, y_off, block_w, block_h, extra_before, extra_after, &subpic, &sb, dmvr_clip);
146 return;
147 }
148 if (dmvr_right <= 0) {
149 sb_wrap(&sb, wrap);
150 emulated_edge_no_wrap(lc, dst, src, src_stride,
151 x_off + wrap, y_off, block_w, block_h, extra_before, extra_after, &subpic, &sb, dmvr_clip);
152 return;
153 }
154 if (dmvr_left >= pic_width) {
155 sb_wrap(&sb, -wrap);
156 emulated_edge_no_wrap(lc, dst, src, src_stride,
157 x_off - wrap, y_off, block_w, block_h, extra_before, extra_after, &subpic, &sb, dmvr_clip);
158 return;
159 }
160
161 block_w += extra;
162 block_h += extra;
163
164 // half block are wrapped
165 if (dmvr_left < 0 ) {
166 const int w = -left;
167 VVCRect half_sb = { sb.l + wrap, sb.t, 0 + wrap, sb.b };
168 emulated_half(lc, dst, dst_stride, *src, *src_stride, ps,
169 left + wrap, top, w, block_h, &subpic, &half_sb, dmvr_clip);
170
171 sb_set_lr(&half_sb, 0, sb.r);
172 emulated_half(lc, dst + (w << ps), dst_stride, *src, *src_stride, ps,
173 0, top, block_w - w, block_h, &subpic, &half_sb, dmvr_clip);
174 } else {
175 const int w = pic_width - left;
176 VVCRect half_sb = { sb.l, sb.t, pic_width, sb.b };
177 emulated_half(lc, dst, dst_stride, *src, *src_stride, ps,
178 left, top, w, block_h, &subpic, &half_sb, dmvr_clip);
179
180 sb_set_lr(&half_sb, pic_width - wrap, sb.r - wrap);
181 emulated_half(lc, dst + (w << ps), dst_stride, *src, *src_stride, ps,
182 pic_width - wrap , top, block_w - w, block_h, &subpic, &half_sb, dmvr_clip);
183 }
184
185 *src = dst + extra_before * dst_stride + (extra_before << ps);
186 *src_stride = dst_stride;
187}
188
189#define MC_EMULATED_EDGE(dst, src, src_stride, x_off, y_off) \
190 emulated_edge(lc, dst, src, src_stride, ref, x_off, y_off, x_off, y_off, block_w, block_h, wrap_enabled, is_chroma, \
191 is_chroma ? CHROMA_EXTRA_BEFORE : LUMA_EXTRA_BEFORE, is_chroma ? CHROMA_EXTRA_AFTER : LUMA_EXTRA_AFTER)
192
193#define MC_EMULATED_EDGE_DMVR(dst, src, src_stride, x_sb, y_sb, x_off, y_off) \
194 emulated_edge(lc, dst, src, src_stride, ref, x_sb, y_sb, x_off, y_off, block_w, block_h, wrap_enabled, is_chroma, \
195 is_chroma ? CHROMA_EXTRA_BEFORE : LUMA_EXTRA_BEFORE, is_chroma ? CHROMA_EXTRA_AFTER : LUMA_EXTRA_AFTER)
196
197#define MC_EMULATED_EDGE_BILINEAR(dst, src, src_stride, x_off, y_off) \
198 emulated_edge(lc, dst, src, src_stride, ref, x_off, y_off, x_off, y_off, pred_w, pred_h, wrap_enabled, 0, \
199 BILINEAR_EXTRA_BEFORE, BILINEAR_EXTRA_AFTER)
200
201// part of 8.5.6.6 Weighted sample prediction process
202static int derive_weight_uni(int *denom, int *wx, int *ox,
203 const VVCLocalContext *lc, const MvField *mvf, const int c_idx)
204{
205 const VVCFrameContext *fc = lc->fc;
206 const VVCPPS *pps = fc->ps.pps;
207 const VVCSH *sh = &lc->sc->sh;
208 const int weight_flag = (IS_P(sh->r) && pps->r->pps_weighted_pred_flag) ||
209 (IS_B(sh->r) && pps->r->pps_weighted_bipred_flag);
210 if (weight_flag) {
211 const int lx = mvf->pred_flag - PF_L0;
212 const PredWeightTable *w = pps->r->pps_wp_info_in_ph_flag ? &fc->ps.ph.pwt : &sh->pwt;
213
214 *denom = w->log2_denom[c_idx > 0];
215 *wx = w->weight[lx][c_idx][mvf->ref_idx[lx]];
216 *ox = w->offset[lx][c_idx][mvf->ref_idx[lx]];
217 }
218 return weight_flag;
219}
220
221// part of 8.5.6.6 Weighted sample prediction process
222static void apply_averaging(uint8_t *dst, const ptrdiff_t dst_stride,
223 const int16_t *src0, const int16_t *src1, int width, int height,
224 const VVCLocalContext *lc, const MvField *mvf, const int c_idx, const int dmvr_flag)
225{
226 const VVCFrameContext *fc = lc->fc;
227 const VVCPPS *pps = fc->ps.pps;
228 const VVCSH *sh = &lc->sc->sh;
229 const int bcw_idx = mvf->bcw_idx;
230 av_assert2(IS_B(sh->r));
231 const int weight_flag = pps->r->pps_weighted_bipred_flag && !dmvr_flag;
232 if ((!weight_flag && !bcw_idx) || (bcw_idx && lc->cu->ciip_flag)) {
233 fc->vvcdsp.inter.avg(dst, dst_stride, src0, src1, width, height);
234 return;
235 }
236
237 int denom, w0, w1, o;
238 if (bcw_idx) {
239 denom = 2;
240 w1 = bcw_w_lut[bcw_idx];
241 w0 = 8 - w1;
242 o = 0;
243 } else {
244 const PredWeightTable *w = pps->r->pps_wp_info_in_ph_flag ? &fc->ps.ph.pwt : &sh->pwt;
245
246 denom = w->log2_denom[c_idx > 0];
247 w0 = w->weight[L0][c_idx][mvf->ref_idx[L0]];
248 w1 = w->weight[L1][c_idx][mvf->ref_idx[L1]];
249 o = w->offset[L0][c_idx][mvf->ref_idx[L0]]
250 + w->offset[L1][c_idx][mvf->ref_idx[L1]];
251 }
252 fc->vvcdsp.inter.w_avg(dst, dst_stride, src0, src1, width, height, denom, w0, w1, o);
253}
254
255#define INTER_FILTER(t, frac) (is_chroma ? ff_vvc_inter_chroma_filters[t][frac] : ff_vvc_inter_luma_filters[t][frac])
256
257static void mc(VVCLocalContext *lc, int16_t *dst, const VVCFrame *ref, const Mv *mv,
258 int x_off, int y_off, const int block_w, const int block_h, const int c_idx)
259{
260 const VVCFrameContext *fc = lc->fc;
261 const PredictionUnit *pu = &lc->cu->pu;
262 const uint8_t *src = ref->frame->data[c_idx];
263 ptrdiff_t src_stride = ref->frame->linesize[c_idx];
264 const int is_chroma = !!c_idx;
265 const int hs = fc->ps.sps->hshift[c_idx];
266 const int vs = fc->ps.sps->vshift[c_idx];
267 const int idx = av_log2(block_w) - 1;
268 const intptr_t mx = av_zero_extend(mv->x, 4 + hs) << (is_chroma - hs);
269 const intptr_t my = av_zero_extend(mv->y, 4 + vs) << (is_chroma - vs);
270 const int hpel_if_idx = (is_chroma || pu->merge_gpm_flag) ? 0 : pu->mi.hpel_if_idx;
271 const int8_t *hf = INTER_FILTER(hpel_if_idx, mx);
272 const int8_t *vf = INTER_FILTER(hpel_if_idx, my);
273 const int wrap_enabled = fc->ps.pps->r->pps_ref_wraparound_enabled_flag;
274
275 x_off += mv->x >> (4 + hs);
276 y_off += mv->y >> (4 + vs);
277
278 MC_EMULATED_EDGE(lc->pred.edge_emu_buffer, &src, &src_stride, x_off, y_off);
279 fc->vvcdsp.inter.put[is_chroma][idx][!!my][!!mx](dst, src, src_stride, block_h, hf, vf, block_w);
280}
281
282static void mc_uni(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride,
283 const VVCFrame *ref, const MvField *mvf, int x_off, int y_off, const int block_w, const int block_h,
284 const int c_idx)
285{
286 const VVCFrameContext *fc = lc->fc;
287 const PredictionUnit *pu = &lc->cu->pu;
288 const uint8_t *src = ref->frame->data[c_idx];
289 ptrdiff_t src_stride = ref->frame->linesize[c_idx];
290 const int lx = mvf->pred_flag - PF_L0;
291 const int hs = fc->ps.sps->hshift[c_idx];
292 const int vs = fc->ps.sps->vshift[c_idx];
293 const int idx = av_log2(block_w) - 1;
294 const Mv *mv = &mvf->mv[lx];
295 const int is_chroma = !!c_idx;
296 const intptr_t mx = av_zero_extend(mv->x, 4 + hs) << (is_chroma - hs);
297 const intptr_t my = av_zero_extend(mv->y, 4 + vs) << (is_chroma - vs);
298 const int hpel_if_idx = is_chroma ? 0 : pu->mi.hpel_if_idx;
299 const int8_t *hf = INTER_FILTER(hpel_if_idx, mx);
300 const int8_t *vf = INTER_FILTER(hpel_if_idx, my);
301 const int wrap_enabled = fc->ps.pps->r->pps_ref_wraparound_enabled_flag;
302 int denom, wx, ox;
303
304 x_off += mv->x >> (4 + hs);
305 y_off += mv->y >> (4 + vs);
306
307 MC_EMULATED_EDGE(lc->pred.edge_emu_buffer, &src, &src_stride, x_off, y_off);
308 if (derive_weight_uni(&denom, &wx, &ox, lc, mvf, c_idx)) {
309 fc->vvcdsp.inter.put_uni_w[is_chroma][idx][!!my][!!mx](dst, dst_stride, src, src_stride,
310 block_h, denom, wx, ox, hf, vf, block_w);
311 } else {
312 fc->vvcdsp.inter.put_uni[is_chroma][idx][!!my][!!mx](dst, dst_stride, src, src_stride,
313 block_h, hf, vf, block_w);
314 }
315}
316
317static void mc_bi(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride,
318 const VVCFrame *ref0, const VVCFrame *ref1, const MvField *mvf, const MvField *orig_mv,
319 const int x_off, const int y_off, const int block_w, const int block_h, const int c_idx,
320 const int sb_bdof_flag)
321{
322 const VVCFrameContext *fc = lc->fc;
323 const PredictionUnit *pu = &lc->cu->pu;
324 const int hs = fc->ps.sps->hshift[c_idx];
325 const int vs = fc->ps.sps->vshift[c_idx];
326 const int idx = av_log2(block_w) - 1;
327 const int mult = HIGHBD_MULTIPLIER(fc->ps.sps->bit_depth);
328 const VVCFrame *refs[] = { ref0, ref1 };
329 int16_t *tmp[] = { lc->pred.tmp + sb_bdof_flag * PROF_TEMP_OFFSET * mult, lc->pred.tmp1 + sb_bdof_flag * PROF_TEMP_OFFSET * mult };
330 const int is_chroma = !!c_idx;
331 const int hpel_if_idx = is_chroma ? 0 : pu->mi.hpel_if_idx;
332
333 for (int i = L0; i <= L1; i++) {
334 const Mv *mv = mvf->mv + i;
335 const int mx = av_zero_extend(mv->x, 4 + hs) << (is_chroma - hs);
336 const int my = av_zero_extend(mv->y, 4 + vs) << (is_chroma - vs);
337 const int ox = x_off + (mv->x >> (4 + hs));
338 const int oy = y_off + (mv->y >> (4 + vs));
339 const VVCFrame *ref = refs[i];
340 ptrdiff_t src_stride = ref->frame->linesize[c_idx];
341 const uint8_t *src = ref->frame->data[c_idx];
342 const int8_t *hf = INTER_FILTER(hpel_if_idx, mx);
343 const int8_t *vf = INTER_FILTER(hpel_if_idx, my);
344 const int wrap_enabled = fc->ps.pps->r->pps_ref_wraparound_enabled_flag;
345
346 if (pu->dmvr_flag) {
347 const int x_sb = x_off + (orig_mv->mv[i].x >> (4 + hs));
348 const int y_sb = y_off + (orig_mv->mv[i].y >> (4 + vs));
349
350 MC_EMULATED_EDGE_DMVR(lc->pred.edge_emu_buffer, &src, &src_stride, x_sb, y_sb, ox, oy);
351 } else {
352 MC_EMULATED_EDGE(lc->pred.edge_emu_buffer, &src, &src_stride, ox, oy);
353 }
354 fc->vvcdsp.inter.put[is_chroma][idx][!!my][!!mx](tmp[i], src, src_stride, block_h, hf, vf, block_w);
355 if (sb_bdof_flag)
356 fc->vvcdsp.inter.bdof_fetch_samples(tmp[i], src, src_stride, mx, my, block_w, block_h);
357 }
358 if (sb_bdof_flag)
359 fc->vvcdsp.inter.apply_bdof(dst, dst_stride, tmp[L0], tmp[L1], block_w, block_h);
360 else
361 apply_averaging(dst, dst_stride, tmp[L0], tmp[L1], block_w, block_h,
362 lc, mvf, c_idx, pu->dmvr_flag);
363}
364
365static const int8_t* inter_filter_scaled(const int scale, const int is_chroma, const int is_affine)
366{
367#define SCALE_THRESHOLD_1 20480
368#define SCALE_THRESHOLD_2 28672
369
370 const int i = (scale > SCALE_THRESHOLD_2) + (scale > SCALE_THRESHOLD_1);
371
372 if (!is_chroma) {
373 if (!is_affine)
374 return &ff_vvc_inter_luma_filters[i + !!i][0][0]; //hpel 1 is not needed for scaled
376 }
377
378 return &ff_vvc_inter_chroma_filters[i][0][0];
379}
380#define INTER_FILTER_SCALED(scale) inter_filter_scaled(scale, is_chroma, is_affine)
381
382#define SCALED_CHROMA_ADDIN(scale, collocated_flag) (is_chroma ? (collocated_flag ? 0 : 8 * (scale - (1 << 14))) : 0)
383// Window offsets are in luma samples; block positions are in component samples.
384#define SCALED_REF_SB(off, scaling_off, ref_mv, scale, add, shift) (((((int64_t)(off) << (4 + shift)) - (scaling_off) * (1 << 4)) + ref_mv) * scale + add)
385#define SCALED_REF(ref_sb, offset, shift) (FFSIGN(ref_sb) * ((FFABS(ref_sb) + (128 << shift)) >> (8 + shift)) + (offset * (1 << (10 - shift))) + (32 >> is_chroma))
386#define SCALED_STEP(scale) ((scale + 8) >> 4)
387
388static void scaled_ref_pos_and_step(const VVCLocalContext *lc, const VVCRefPic *refp, const Mv *mv, const int x_off, const int y_off, const int c_idx,
389 int *x, int *y, int *dx, int *dy)
390{
391 const VVCFrameContext *fc = lc->fc;
392 const VVCSPS *sps = fc->ps.sps;
393 const int is_chroma = !!c_idx;
394 const int hs = sps->hshift[c_idx];
395 const int vs = sps->vshift[c_idx];
396 const int left_offset = fc->ref->scaling_win.left_offset;
397 const int top_offset = fc->ref->scaling_win.top_offset;
398 const int addx = SCALED_CHROMA_ADDIN(refp->scale[0], sps->r->sps_chroma_horizontal_collocated_flag);
399 const int addy = SCALED_CHROMA_ADDIN(refp->scale[1], sps->r->sps_chroma_vertical_collocated_flag);
400 const int64_t refx_sb = SCALED_REF_SB(x_off, left_offset, mv->x, refp->scale[0], addx, hs);
401 const int64_t refy_sb = SCALED_REF_SB(y_off, top_offset, mv->y, refp->scale[1], addy, vs);
402
403 *x = SCALED_REF(refx_sb, refp->ref->scaling_win.left_offset, hs);
404 *y = SCALED_REF(refy_sb, refp->ref->scaling_win.top_offset, vs);
405 *dx = SCALED_STEP(refp->scale[0]);
406 *dy = SCALED_STEP(refp->scale[1]);
407}
408
409static void emulated_edge_scaled(VVCLocalContext *lc, const uint8_t **src, ptrdiff_t *src_stride, int *src_height,
410 const VVCFrame *ref, const int x, const int y, const int dx, const int dy, const int w, const int h, const int is_chroma)
411{
412 const int x_off = SCALED_INT(x);
413 const int y_off = SCALED_INT(y);
414 const int x_end = SCALED_INT(x + w * dx);
415 const int y_end = SCALED_INT(y + h * dy);
416 const int x_last = SCALED_INT(x + (w - 1) * dx);
417 const int y_last = SCALED_INT(y + (h - 1) * dy);
418 const int block_w = x_end - x_off + (x_end == x_last);
419 const int block_h = *src_height = y_end - y_off + (y_end == y_last);
420 const int wrap_enabled = 0;
421
422 MC_EMULATED_EDGE(lc->pred.edge_emu_buffer, src, src_stride, x_off, y_off);
423}
424
425static void mc_scaled(VVCLocalContext *lc, int16_t *dst, const VVCRefPic *refp, const Mv *mv,
426 int x_off, int y_off, const int block_w, const int block_h, const int c_idx)
427{
428 const VVCFrameContext *fc = lc->fc;
429 const PredictionUnit *pu = &lc->cu->pu;
430 const uint8_t *src = refp->ref->frame->data[c_idx];
431 ptrdiff_t src_stride = refp->ref->frame->linesize[c_idx];
432 const int is_affine = pu->inter_affine_flag;
433 const int is_chroma = !!c_idx;
434 const int idx = av_log2(block_w) - 1;
435 const int8_t *hf = INTER_FILTER_SCALED(refp->scale[0]);
436 const int8_t *vf = INTER_FILTER_SCALED(refp->scale[1]);
437 int x, y, dx, dy, src_height;
438
439 scaled_ref_pos_and_step(lc, refp, mv, x_off, y_off, c_idx, &x, &y, &dx, &dy);
440 emulated_edge_scaled(lc, &src, &src_stride, &src_height, refp->ref, x, y, dx, dy, block_w, block_h, is_chroma);
441 fc->vvcdsp.inter.put_scaled[is_chroma][idx](dst, src, src_stride, src_height, x, y, dx, dy, block_h, hf, vf,
442 block_w, lc->pred.scaled_tmp);
443}
444
445static void mc_uni_scaled(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride, const VVCRefPic *refp,
446 const MvField *mvf, const int x_off, const int y_off, const int block_w, const int block_h, const int c_idx)
447{
448 const VVCFrameContext *fc = lc->fc;
449 const PredictionUnit *pu = &lc->cu->pu;
450 const uint8_t *src = refp->ref->frame->data[c_idx];
451 ptrdiff_t src_stride = refp->ref->frame->linesize[c_idx];
452 const int lx = mvf->pred_flag - PF_L0;
453 const Mv *mv = &mvf->mv[lx];
454 const int is_affine = pu->inter_affine_flag;
455 const int is_chroma = !!c_idx;
456 const int idx = av_log2(block_w) - 1;
457 const int8_t *hf = INTER_FILTER_SCALED(refp->scale[0]);
458 const int8_t *vf = INTER_FILTER_SCALED(refp->scale[1]);
459 int denom, wx, ox, x, y, dx, dy, src_height;
460
461 scaled_ref_pos_and_step(lc, refp, mv, x_off, y_off, c_idx, &x, &y, &dx, &dy);
462 emulated_edge_scaled(lc, &src, &src_stride, &src_height, refp->ref, x, y, dx, dy, block_w, block_h, is_chroma);
463
464 if (derive_weight_uni(&denom, &wx, &ox, lc, mvf, c_idx)) {
465 fc->vvcdsp.inter.put_uni_w_scaled[is_chroma][idx](dst, dst_stride, src, src_stride, src_height,
466 x, y, dx, dy, block_h, denom, wx, ox, hf, vf, block_w, lc->pred.scaled_tmp);
467 } else {
468 fc->vvcdsp.inter.put_uni_scaled[is_chroma][idx](dst, dst_stride, src, src_stride, src_height,
469 x, y, dx, dy, block_h, hf, vf, block_w, lc->pred.scaled_tmp);
470 }
471}
472
473static void mc_bi_scaled(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride,
474 const VVCRefPic *refp0, const VVCRefPic *refp1, const MvField *mvf,
475 const int x_off, const int y_off, const int block_w, const int block_h, const int c_idx)
476{
477 const VVCRefPic *refps[] = { refp0, refp1 };
478 int16_t *tmp[] = { lc->pred.tmp, lc->pred.tmp1 };
479
480 for (int i = L0; i <= L1; i++) {
481 const Mv *mv = mvf->mv + i;
482 const VVCRefPic *refp = refps[i];
483
484 if (refp->is_scaled)
485 mc_scaled(lc, tmp[i], refp, mv, x_off, y_off, block_w, block_h, c_idx);
486 else
487 mc(lc, tmp[i], refp->ref, mv, x_off, y_off, block_w, block_h, c_idx);
488 }
489 apply_averaging(dst, dst_stride, tmp[L0], tmp[L1], block_w, block_h,
490 lc, mvf, c_idx, lc->cu->pu.dmvr_flag);
491}
492
493static void luma_prof_uni(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride,
494 const VVCFrame *ref, const MvField *mvf, int x_off, int y_off, const int block_w, const int block_h,
495 const int cb_prof_flag, const int16_t *diff_mv_x, const int16_t *diff_mv_y)
496{
497 const VVCFrameContext *fc = lc->fc;
498 const uint8_t *src = ref->frame->data[LUMA];
499 ptrdiff_t src_stride = ref->frame->linesize[LUMA];
500 int16_t *prof_tmp = lc->pred.tmp + PROF_TEMP_OFFSET * HIGHBD_MULTIPLIER(fc->ps.sps->bit_depth);
501 const int idx = av_log2(block_w) - 1;
502 const int lx = mvf->pred_flag - PF_L0;
503 const Mv *mv = mvf->mv + lx;
504 const int mx = mv->x & 0xf;
505 const int my = mv->y & 0xf;
508 int denom, wx, ox;
509 const int weight_flag = derive_weight_uni(&denom, &wx, &ox, lc, mvf, LUMA);
510 const int wrap_enabled = fc->ps.pps->r->pps_ref_wraparound_enabled_flag;
511 const int is_chroma = 0;
512
513 x_off += mv->x >> 4;
514 y_off += mv->y >> 4;
515
516 MC_EMULATED_EDGE(lc->pred.edge_emu_buffer, &src, &src_stride, x_off, y_off);
517 if (cb_prof_flag) {
518 fc->vvcdsp.inter.put[LUMA][idx][!!my][!!mx](prof_tmp, src, src_stride, AFFINE_MIN_BLOCK_SIZE, hf, vf, AFFINE_MIN_BLOCK_SIZE);
519 fc->vvcdsp.inter.fetch_samples(prof_tmp, src, src_stride, mx, my);
520 if (!weight_flag)
521 fc->vvcdsp.inter.apply_prof_uni(dst, dst_stride, prof_tmp, diff_mv_x, diff_mv_y);
522 else
523 fc->vvcdsp.inter.apply_prof_uni_w(dst, dst_stride, prof_tmp, diff_mv_x, diff_mv_y, denom, wx, ox);
524 } else {
525 if (!weight_flag)
526 fc->vvcdsp.inter.put_uni[LUMA][idx][!!my][!!mx](dst, dst_stride, src, src_stride, block_h, hf, vf, block_w);
527 else
528 fc->vvcdsp.inter.put_uni_w[LUMA][idx][!!my][!!mx](dst, dst_stride, src, src_stride, block_h, denom, wx, ox, hf, vf, block_w);
529 }
530}
531
532static void luma_prof(VVCLocalContext *lc, int16_t *dst, const VVCFrame *ref,
533 const Mv *mv , const int x_off, const int y_off, const int block_w, const int block_h, const int lx)
534{
535 const VVCFrameContext *fc = lc->fc;
536 const PredictionUnit *pu = &lc->cu->pu;
537 const int mx = mv->x & 0xf;
538 const int my = mv->y & 0xf;
539 const int ox = x_off + (mv->x >> 4);
540 const int oy = y_off + (mv->y >> 4);
541 const int idx = av_log2(block_w) - 1;
542 const int is_chroma = 0;
543 int16_t *prof_tmp = lc->pred.prof_tmp + PROF_TEMP_OFFSET * HIGHBD_MULTIPLIER(fc->ps.sps->bit_depth);
544 ptrdiff_t src_stride = ref->frame->linesize[LUMA];
545 const uint8_t *src = ref->frame->data[LUMA];
548 const int wrap_enabled = fc->ps.pps->r->pps_ref_wraparound_enabled_flag;
549
550 MC_EMULATED_EDGE(lc->pred.edge_emu_buffer, &src, &src_stride, ox, oy);
551 if (!pu->cb_prof_flag[lx]) {
552 fc->vvcdsp.inter.put[LUMA][idx][!!my][!!mx](dst, src, src_stride, block_h, hf, vf, block_w);
553 } else {
554 fc->vvcdsp.inter.put[LUMA][idx][!!my][!!mx](prof_tmp, src, src_stride, AFFINE_MIN_BLOCK_SIZE, hf, vf, AFFINE_MIN_BLOCK_SIZE);
555 fc->vvcdsp.inter.fetch_samples(prof_tmp, src, src_stride, mx, my);
556 fc->vvcdsp.inter.apply_prof(dst, prof_tmp, pu->diff_mv_x[lx], pu->diff_mv_y[lx]);
557 }
558}
559
560static void luma_prof_bi(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride,
561 const VVCRefPic *ref0, const VVCRefPic *ref1, const MvField *mvf, const int x_off, const int y_off,
562 const int block_w, const int block_h)
563{
564 const VVCRefPic *refps[] = { ref0, ref1 };
565 int16_t *tmp[] = { lc->pred.tmp, lc->pred.tmp1 };
566
567 for (int i = L0; i <= L1; i++) {
568 const VVCRefPic *refp = refps[i];
569 const Mv *mv = mvf->mv + i;
570
571 if (refp->is_scaled)
572 mc_scaled(lc, tmp[i], refp, mv, x_off, y_off, block_w, block_h, LUMA);
573 else
574 luma_prof(lc, tmp[i], refp->ref, mv, x_off, y_off, block_w, block_h, i);
575 }
576
577 apply_averaging(dst, dst_stride, tmp[L0], tmp[L1], block_w, block_h, lc, mvf, LUMA, 0);
578}
579
580static int pred_get_refs(const VVCLocalContext *lc, VVCRefPic *refp[2], const MvField *mv)
581{
582 RefPicList *rpl = lc->sc->rpl;
583
584 for (int mask = PF_L0; mask <= PF_L1; mask++) {
585 if (mv->pred_flag & mask) {
586 const int lx = mask - PF_L0;
587 refp[lx] = rpl[lx].refs + mv->ref_idx[lx];
588 if (!refp[lx]->ref)
589 return AVERROR_INVALIDDATA;
590 }
591 }
592 return 0;
593}
594
595#define POS(c_idx, x, y) \
596 &fc->frame->data[c_idx][((y) >> fc->ps.sps->vshift[c_idx]) * fc->frame->linesize[c_idx] + \
597 (((x) >> fc->ps.sps->hshift[c_idx]) << fc->ps.sps->pixel_shift)]
598
600{
601 const VVCFrameContext *fc = lc->fc;
602 const CodingUnit *cu = lc->cu;
603 const PredictionUnit *pu = &cu->pu;
604
605 const uint8_t angle_idx = ff_vvc_gpm_angle_idx[pu->gpm_partition_idx];
606 const uint8_t weights_idx = ff_vvc_gpm_angle_to_weights_idx[angle_idx];
607 const int w = av_log2(cu->cb_width) - 3;
608 const int h = av_log2(cu->cb_height) - 3;
609 const uint8_t off_x = ff_vvc_gpm_weights_offset_x[pu->gpm_partition_idx][h][w];
610 const uint8_t off_y = ff_vvc_gpm_weights_offset_y[pu->gpm_partition_idx][h][w];
611 const uint8_t mirror_type = ff_vvc_gpm_angle_to_mirror[angle_idx];
612 const uint8_t *weights;
613
614 const int c_end = fc->ps.sps->r->sps_chroma_format_idc ? 3 : 1;
615
616 int16_t *tmp[2] = {lc->pred.tmp, lc->pred.tmp1};
617
618 for (int c_idx = 0; c_idx < c_end; c_idx++) {
619 const int hs = fc->ps.sps->hshift[c_idx];
620 const int vs = fc->ps.sps->vshift[c_idx];
621 const int x = lc->cu->x0 >> hs;
622 const int y = lc->cu->y0 >> vs;
623 const int width = cu->cb_width >> hs;
624 const int height = cu->cb_height >> vs;
625 uint8_t *dst = POS(c_idx, lc->cu->x0, lc->cu->y0);
626 ptrdiff_t dst_stride = fc->frame->linesize[c_idx];
627
628 int step_x = 1 << hs;
629 int step_y = VVC_GPM_WEIGHT_SIZE << vs;
630 if (!mirror_type) {
631 weights = &ff_vvc_gpm_weights[weights_idx][off_y * VVC_GPM_WEIGHT_SIZE + off_x];
632 } else if (mirror_type == 1) {
633 step_x = -step_x;
634 weights = &ff_vvc_gpm_weights[weights_idx][off_y * VVC_GPM_WEIGHT_SIZE + VVC_GPM_WEIGHT_SIZE - 1- off_x];
635 } else {
636 step_y = -step_y;
637 weights = &ff_vvc_gpm_weights[weights_idx][(VVC_GPM_WEIGHT_SIZE - 1 - off_y) * VVC_GPM_WEIGHT_SIZE + off_x];
638 }
639
640 for (int i = 0; i < 2; i++) {
641 const MvField *mv = pu->gpm_mv + i;
642 const int lx = mv->pred_flag - PF_L0;
643 VVCRefPic *refp = lc->sc->rpl[lx].refs + mv->ref_idx[lx];
644
645 if (!refp->ref)
646 return;
647 if (refp->is_scaled)
648 mc_scaled(lc, tmp[i], refp, mv->mv + lx, x, y, width, height, c_idx);
649 else
650 mc(lc, tmp[i], refp->ref, mv->mv + lx, x, y, width, height, c_idx);
651 }
652 fc->vvcdsp.inter.put_gpm(dst, dst_stride, width, height, tmp[0], tmp[1], weights, step_x, step_y);
653 }
654 return;
655}
656
657static int ciip_derive_intra_weight(const VVCLocalContext *lc, const int x0, const int y0,
658 const int width, const int height)
659{
660 const VVCFrameContext *fc = lc->fc;
661 const VVCSPS *sps = fc->ps.sps;
662 const int x0b = av_zero_extend(x0, sps->ctb_log2_size_y);
663 const int y0b = av_zero_extend(y0, sps->ctb_log2_size_y);
664 const int available_l = lc->ctb_left_flag || x0b;
665 const int available_u = lc->ctb_up_flag || y0b;
666 const int min_pu_width = fc->ps.pps->min_pu_width;
667 int w = 1;
668
669 if (available_u &&fc->tab.mvf[((y0 - 1) >> MIN_PU_LOG2) * min_pu_width + ((x0 - 1 + width)>> MIN_PU_LOG2)].pred_flag == PF_INTRA)
670 w++;
671
672 if (available_l && fc->tab.mvf[((y0 - 1 + height)>> MIN_PU_LOG2) * min_pu_width + ((x0 - 1) >> MIN_PU_LOG2)].pred_flag == PF_INTRA)
673 w++;
674
675 return w;
676}
677
678static void pred_regular(VVCLocalContext *lc, const MvField *mvf, const MvField *orig_mvf,
679 const int x0, const int y0, const int sbw, const int sbh, const int sb_bdof_flag, const int c_start)
680{
681 const VVCFrameContext *fc = lc->fc;
682 const int c_end = fc->ps.sps->r->sps_chroma_format_idc ? CR : LUMA;
683 VVCRefPic *refp[2];
684
685 if (pred_get_refs(lc, refp, mvf) < 0)
686 return;
687
688 for (int c_idx = c_start; c_idx <= c_end; c_idx++) {
689 uint8_t *dst = POS(c_idx, x0, y0);
690 const ptrdiff_t dst_stride = fc->frame->linesize[c_idx];
691 const int hs = fc->ps.sps->hshift[c_idx];
692 const int vs = fc->ps.sps->vshift[c_idx];
693 const int x = x0 >> hs;
694 const int y = y0 >> vs;
695 const int w = sbw >> hs;
696 const int h = sbh >> vs;
697 const int is_luma = !c_idx;
698 const int do_ciip = lc->cu->ciip_flag && (is_luma || (w > 2));
699 uint8_t *inter = do_ciip ? lc->pred.ciip_tmp : dst;
700 const ptrdiff_t inter_stride = do_ciip ? (MAX_PB_SIZE * sizeof(uint16_t)) : dst_stride;
701 const int do_bdof = is_luma && sb_bdof_flag;
702
703 if (mvf->pred_flag != PF_BI) {
704 const int lx = mvf->pred_flag - PF_L0;
705
706 if (refp[lx]->is_scaled) {
707 mc_uni_scaled(lc, inter, inter_stride, refp[lx], mvf,
708 x, y, w, h, c_idx);
709 } else {
710 mc_uni(lc, inter, inter_stride, refp[lx]->ref, mvf,
711 x, y, w, h, c_idx);
712 }
713 } else {
714 if (refp[L0]->is_scaled || refp[L1]->is_scaled) {
715 mc_bi_scaled(lc, inter, inter_stride, refp[L0], refp[L1], mvf,
716 x, y, w, h, c_idx);
717 } else {
718 mc_bi(lc, inter, inter_stride, refp[L0]->ref, refp[L1]->ref, mvf, orig_mvf,
719 x, y, w, h, c_idx, do_bdof);
720 }
721 }
722 if (do_ciip) {
723 const int intra_weight = ciip_derive_intra_weight(lc, x0, y0, sbw, sbh);
724 fc->vvcdsp.intra.intra_pred(lc, x0, y0, sbw, sbh, c_idx);
725 if (!c_idx && lc->sc->sh.r->sh_lmcs_used_flag)
726 fc->vvcdsp.lmcs.filter(inter, inter_stride, w, h, &fc->ps.lmcs.fwd_lut);
727 fc->vvcdsp.inter.put_ciip(dst, dst_stride, w, h, inter, inter_stride, intra_weight);
728 }
729 }
730}
731
732// 8.5.3.5 Parametric motion vector refinement process
733static int parametric_mv_refine(const int *sad, const int stride)
734{
735 const int sad_minus = sad[-stride];
736 const int sad_center = sad[0];
737 const int sad_plus = sad[stride];
738 int dmvc;
739 int denom = (( sad_minus + sad_plus) - (sad_center << 1 ) ) << 3;
740 if (!denom)
741 dmvc = 0;
742 else {
743 if (sad_minus == sad_center)
744 dmvc = -8;
745 else if (sad_plus == sad_center)
746 dmvc = 8;
747 else {
748 int num = ( sad_minus - sad_plus ) * (1 << 4);
749 int sign_num = 0;
750 int quotient = 0;
751 int counter = 3;
752 if (num < 0 ) {
753 num = - num;
754 sign_num = 1;
755 }
756 while (counter > 0) {
757 counter = counter - 1;
758 quotient = quotient << 1;
759 if ( num >= denom ) {
760 num = num - denom;
761 quotient = quotient + 1;
762 }
763 denom = (denom >> 1);
764 }
765 if (sign_num == 1 )
766 dmvc = -quotient;
767 else
768 dmvc = quotient;
769 }
770 }
771 return dmvc;
772}
773
774#define SAD_ARRAY_SIZE 5
775//8.5.3 Decoder-side motion vector refinement process
776static void dmvr_mv_refine(VVCLocalContext *lc, MvField *mvf, MvField *orig_mv, int *sb_bdof_flag,
777 const VVCFrame *ref0, const VVCFrame *ref1, const int x_off, const int y_off, const int block_w, const int block_h)
778{
779 const VVCFrameContext *fc = lc->fc;
780 const int sr_range = 2;
781 const VVCFrame *refs[] = { ref0, ref1 };
782 int16_t *tmp[] = { lc->pred.tmp, lc->pred.tmp1 };
784 int min_dx, min_dy, min_sad, dx, dy;
785
786 *orig_mv = *mvf;
787 min_dx = min_dy = dx = dy = 2;
788
789 for (int i = L0; i <= L1; i++) {
790 const int pred_w = block_w + 2 * sr_range;
791 const int pred_h = block_h + 2 * sr_range;
792 const Mv *mv = mvf->mv + i;
793 const int mx = mv->x & 0xf;
794 const int my = mv->y & 0xf;
795 const int ox = x_off + (mv->x >> 4) - sr_range;
796 const int oy = y_off + (mv->y >> 4) - sr_range;
797 const VVCFrame *ref = refs[i];
798 ptrdiff_t src_stride = ref->frame->linesize[LUMA];
799 const uint8_t *src = ref->frame->data[LUMA];
800 const int wrap_enabled = fc->ps.pps->r->pps_ref_wraparound_enabled_flag;
801
802 MC_EMULATED_EDGE_BILINEAR(lc->pred.edge_emu_buffer, &src, &src_stride, ox, oy);
803 fc->vvcdsp.inter.dmvr[!!my][!!mx](tmp[i], src, src_stride, pred_h, mx, my, pred_w);
804 }
805
806 min_sad = fc->vvcdsp.inter.sad(tmp[L0], tmp[L1], dx, dy, block_w, block_h);
807 min_sad -= min_sad >> 2;
808 sad[dy][dx] = min_sad;
809
810 if (min_sad >= block_w * block_h) {
811 int dmv[2];
812 // 8.5.3.4 Array entry selection process
813 for (dy = 0; dy < SAD_ARRAY_SIZE; dy++) {
814 for (dx = 0; dx < SAD_ARRAY_SIZE; dx++) {
815 if (dx != sr_range || dy != sr_range) {
816 sad[dy][dx] = fc->vvcdsp.inter.sad(lc->pred.tmp, lc->pred.tmp1, dx, dy, block_w, block_h);
817 if (sad[dy][dx] < min_sad) {
818 min_sad = sad[dy][dx];
819 min_dx = dx;
820 min_dy = dy;
821 }
822 }
823 }
824 }
825 dmv[0] = (min_dx - sr_range) * (1 << 4);
826 dmv[1] = (min_dy - sr_range) * (1 << 4);
827 if (min_dx != 0 && min_dx != 4 && min_dy != 0 && min_dy != 4) {
828 dmv[0] += parametric_mv_refine(&sad[min_dy][min_dx], 1);
829 dmv[1] += parametric_mv_refine(&sad[min_dy][min_dx], SAD_ARRAY_SIZE);
830 }
831
832 for (int i = L0; i <= L1; i++) {
833 Mv *mv = mvf->mv + i;
834 mv->x += (1 - 2 * i) * dmv[0];
835 mv->y += (1 - 2 * i) * dmv[1];
837 }
838 }
839 if (min_sad < 2 * block_w * block_h) {
840 *sb_bdof_flag = 0;
841 }
842}
843
844static void set_dmvr_info(VVCFrameContext *fc, const int x0, const int y0,
845 const int width, const int height, const MvField *mvf)
846
847{
848 const VVCPPS *pps = fc->ps.pps;
849
850 for (int y = y0; y < y0 + height; y += MIN_PU_SIZE) {
851 for (int x = x0; x < x0 + width; x += MIN_PU_SIZE) {
852 const int idx = pps->min_pu_width * (y >> MIN_PU_LOG2) + (x >> MIN_PU_LOG2);
853 fc->ref->tab_dmvr_mvf[idx] = *mvf;
854 }
855 }
856}
857
858static void derive_sb_mv(VVCLocalContext *lc, MvField *mv, MvField *orig_mv, int *sb_bdof_flag,
859 const int x0, const int y0, const int sbw, const int sbh)
860{
861 VVCFrameContext *fc = lc->fc;
862 const PredictionUnit *pu = &lc->cu->pu;
863
864 *orig_mv = *mv = *ff_vvc_get_mvf(fc, x0, y0);
865 if (pu->bdof_flag)
866 *sb_bdof_flag = 1;
867 if (pu->dmvr_flag) {
868 VVCRefPic *refp[2];
869 if (pred_get_refs(lc, refp, mv) < 0)
870 return;
871 dmvr_mv_refine(lc, mv, orig_mv, sb_bdof_flag, refp[L0]->ref, refp[L1]->ref, x0, y0, sbw, sbh);
872 set_dmvr_info(fc, x0, y0, sbw, sbh, mv);
873 }
874}
875
876static void pred_regular_blk(VVCLocalContext *lc, const int skip_ciip)
877{
878 const CodingUnit *cu = lc->cu;
879 PredictionUnit *pu = &lc->cu->pu;
880 const MotionInfo *mi = &pu->mi;
881 MvField mv, orig_mv;
882 int sbw, sbh, sb_bdof_flag = 0;
883
884 if (cu->ciip_flag && skip_ciip)
885 return;
886
887 sbw = cu->cb_width / mi->num_sb_x;
888 sbh = cu->cb_height / mi->num_sb_y;
889
890 for (int sby = 0; sby < mi->num_sb_y; sby++) {
891 for (int sbx = 0; sbx < mi->num_sb_x; sbx++) {
892 const int x0 = cu->x0 + sbx * sbw;
893 const int y0 = cu->y0 + sby * sbh;
894
895 if (cu->ciip_flag)
896 ff_vvc_set_neighbour_available(lc, x0, y0, sbw, sbh);
897
898 derive_sb_mv(lc, &mv, &orig_mv, &sb_bdof_flag, x0, y0, sbw, sbh);
899 pred_regular(lc, &mv, &orig_mv, x0, y0, sbw, sbh, sb_bdof_flag, LUMA);
900 }
901 }
902}
903
904static void derive_affine_mvc(MvField *mvc, const VVCFrameContext *fc, const MvField *mv,
905 const int x0, const int y0, const int sbw, const int sbh)
906{
907 const int hs = fc->ps.sps->hshift[1];
908 const int vs = fc->ps.sps->vshift[1];
909 const MvField* mv2 = ff_vvc_get_mvf(fc, x0 + hs * sbw, y0 + vs * sbh);
910 *mvc = *mv;
911
912 // Due to different pred_flag, one of the motion vectors may have an invalid value.
913 // Cast them to an unsigned type to avoid undefined behavior.
914 mvc->mv[0].x += (unsigned int)mv2->mv[0].x;
915 mvc->mv[0].y += (unsigned int)mv2->mv[0].y;
916 mvc->mv[1].x += (unsigned int)mv2->mv[1].x;
917 mvc->mv[1].y += (unsigned int)mv2->mv[1].y;
918 ff_vvc_round_mv(mvc->mv + 0, 0, 1);
919 ff_vvc_round_mv(mvc->mv + 1, 0, 1);
920}
921
923{
924 const VVCFrameContext *fc = lc->fc;
925 const CodingUnit *cu = lc->cu;
926 const PredictionUnit *pu = &cu->pu;
927 const MotionInfo *mi = &pu->mi;
928 const int x0 = cu->x0;
929 const int y0 = cu->y0;
930 const int sbw = cu->cb_width / mi->num_sb_x;
931 const int sbh = cu->cb_height / mi->num_sb_y;
932 const int hs = fc->ps.sps->hshift[1];
933 const int vs = fc->ps.sps->vshift[1];
934 const int dst_stride = fc->frame->linesize[LUMA];
935
936 for (int sby = 0; sby < mi->num_sb_y; sby++) {
937 for (int sbx = 0; sbx < mi->num_sb_x; sbx++) {
938 const int x = x0 + sbx * sbw;
939 const int y = y0 + sby * sbh;
940
941 uint8_t *dst0 = POS(0, x, y);
942 const MvField *mv = ff_vvc_get_mvf(fc, x, y);
943 VVCRefPic *refp[2];
944
945 if (pred_get_refs(lc, refp, mv) < 0)
946 return;
947
948 if (mi->pred_flag != PF_BI) {
949 const int lx = mi->pred_flag - PF_L0;
950 if (refp[lx]->is_scaled) {
951 mc_uni_scaled(lc, dst0, dst_stride, refp[lx], mv, x, y, sbw, sbh, LUMA);
952 } else {
953 luma_prof_uni(lc, dst0, dst_stride, refp[lx]->ref,
954 mv, x, y, sbw, sbh, pu->cb_prof_flag[lx],
955 pu->diff_mv_x[lx], pu->diff_mv_y[lx]);
956 }
957 } else {
958 luma_prof_bi(lc, dst0, dst_stride, refp[L0], refp[L1], mv, x, y, sbw, sbh);
959 }
960 if (fc->ps.sps->r->sps_chroma_format_idc) {
961 if (!av_zero_extend(sby, vs) && !av_zero_extend(sbx, hs)) {
962 MvField mvc;
963
964 derive_affine_mvc(&mvc, fc, mv, x, y, sbw, sbh);
965 pred_regular(lc, &mvc, NULL, x, y, sbw << hs, sbh << vs, 0, CB);
966 }
967 }
968
969 }
970 }
971}
972
974{
975 const VVCFrameContext *fc = lc->fc;
976 const CodingUnit *cu = lc->cu;
977 const PredictionUnit *pu = &cu->pu;
978
979 if (pu->merge_gpm_flag)
980 pred_gpm_blk(lc);
981 else if (pu->inter_affine_flag)
982 pred_affine_blk(lc);
983 else
984 pred_regular_blk(lc, 1); //intra block is not ready yet, skip ciip
985
986 if (lc->sc->sh.r->sh_lmcs_used_flag && !cu->ciip_flag) {
987 uint8_t* dst0 = POS(0, cu->x0, cu->y0);
988 fc->vvcdsp.lmcs.filter(dst0, fc->frame->linesize[LUMA], cu->cb_width, cu->cb_height, &fc->ps.lmcs.fwd_lut);
989 }
990}
991
992static int has_inter_luma(const CodingUnit *cu)
993{
994 return (cu->pred_mode == MODE_INTER || cu->pred_mode == MODE_SKIP) && cu->tree_type != DUAL_TREE_CHROMA;
995}
996
998{
999 const VVCFrameContext *fc = lc->fc;
1000 CodingUnit *cu = fc->tab.cus[rs];
1001
1002 while (cu) {
1003 lc->cu = cu;
1004 if (has_inter_luma(cu))
1005 predict_inter(lc);
1006 cu = cu->next;
1007 }
1008
1009 return 0;
1010}
1011
1013{
1014 av_assert0(lc->cu->ciip_flag);
1015
1016 //todo: refact out ciip from pred_regular_blk
1017 pred_regular_blk(lc, 0);
1018}
1019
1020#undef POS
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
Definition dsp.h:67
uint8_t ptrdiff_t const uint8_t ptrdiff_t int const int8_t const int8_t * vf
Definition dsp.h:272
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
Definition dsp.h:67
uint8_t ptrdiff_t const uint8_t ptrdiff_t int const int8_t * hf
Definition dsp.h:272
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
#define wrap(func)
Definition neontest.h:65
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define av_zero_extend
Definition common.h:151
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
void ff_vvc_set_neighbour_available(VVCLocalContext *lc, const int x0, const int y0, const int w, const int h)
Definition ctu.c:2879
#define AFFINE_MIN_BLOCK_SIZE
Definition ctu.h:69
@ DUAL_TREE_CHROMA
Definition ctu.h:172
#define MIN_PU_SIZE
Definition ctu.h:43
#define SCALED_INT(pos)
Definition ctu.h:65
static const uint16_t fc[]
Definition dcaenc.h:43
#define MIN_PU_LOG2
Definition dec.h:42
uint64_t pps
Definition dovi_rpuenc.c:36
reference-counted frame API
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define MAX_PB_SIZE
Definition dsp.h:32
#define LUMA
Definition filter.c:31
#define CR
Definition filter.c:33
#define CB
Definition filter.c:32
static const int weights[]
Definition hevc_pel.c:32
#define L1
Definition hevcdec.h:57
@ MODE_INTER
Definition hevcdec.h:107
@ MODE_SKIP
Definition hevcdec.h:109
@ PF_L1
Definition hevcdec.h:121
@ PF_BI
Definition hevcdec.h:122
@ PF_L0
Definition hevcdec.h:120
@ PF_INTRA
Definition hevcdec.h:119
#define L0
Definition hevcdec.h:56
#define EDGE_EMU_BUFFER_STRIDE
Definition hevcdec.h:66
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
#define r
Definition input.c:42
#define b
Definition input.c:43
#define HIGHBD_MULTIPLIER(bd)
Definition inter.c:33
static void dmvr_mv_refine(VVCLocalContext *lc, MvField *mvf, MvField *orig_mv, int *sb_bdof_flag, const VVCFrame *ref0, const VVCFrame *ref1, const int x_off, const int y_off, const int block_w, const int block_h)
Definition inter.c:776
#define SCALED_CHROMA_ADDIN(scale, collocated_flag)
Definition inter.c:382
#define MC_EMULATED_EDGE_BILINEAR(dst, src, src_stride, x_off, y_off)
Definition inter.c:197
#define SAD_ARRAY_SIZE
Definition inter.c:774
static void luma_prof_uni(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride, const VVCFrame *ref, const MvField *mvf, int x_off, int y_off, const int block_w, const int block_h, const int cb_prof_flag, const int16_t *diff_mv_x, const int16_t *diff_mv_y)
Definition inter.c:493
static void derive_sb_mv(VVCLocalContext *lc, MvField *mv, MvField *orig_mv, int *sb_bdof_flag, const int x0, const int y0, const int sbw, const int sbh)
Definition inter.c:858
static int has_inter_luma(const CodingUnit *cu)
Definition inter.c:992
static void emulated_half(const VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride, const uint8_t *src, const ptrdiff_t src_stride, const int ps, int x_off, int y_off, const int block_w, const int block_h, const VVCRect *subpic, const VVCRect *half_sb, const int dmvr_clip)
Definition inter.c:93
static void set_dmvr_info(VVCFrameContext *fc, const int x0, const int y0, const int width, const int height, const MvField *mvf)
Definition inter.c:844
static int pred_get_refs(const VVCLocalContext *lc, VVCRefPic *refp[2], const MvField *mv)
Definition inter.c:580
static void luma_prof(VVCLocalContext *lc, int16_t *dst, const VVCFrame *ref, const Mv *mv, const int x_off, const int y_off, const int block_w, const int block_h, const int lx)
Definition inter.c:532
static void mc_bi(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride, const VVCFrame *ref0, const VVCFrame *ref1, const MvField *mvf, const MvField *orig_mv, const int x_off, const int y_off, const int block_w, const int block_h, const int c_idx, const int sb_bdof_flag)
Definition inter.c:317
#define SCALE_THRESHOLD_1
#define PROF_TEMP_OFFSET
Definition inter.c:30
static const int bcw_w_lut[]
Definition inter.c:31
#define POS(c_idx, x, y)
Definition inter.c:595
#define SCALE_THRESHOLD_2
#define SCALED_STEP(scale)
Definition inter.c:386
static int ciip_derive_intra_weight(const VVCLocalContext *lc, const int x0, const int y0, const int width, const int height)
Definition inter.c:657
void ff_vvc_predict_ciip(VVCLocalContext *lc)
CIIP(Combined Inter-Intra Prediction) for a coding block.
Definition inter.c:1012
static void emulated_edge(const VVCLocalContext *lc, uint8_t *dst, const uint8_t **src, ptrdiff_t *src_stride, const VVCFrame *src_frame, int x_sb, int y_sb, int x_off, int y_off, int block_w, int block_h, const int wrap_enabled, const int is_chroma, const int extra_before, const int extra_after)
Definition inter.c:120
static void apply_averaging(uint8_t *dst, const ptrdiff_t dst_stride, const int16_t *src0, const int16_t *src1, int width, int height, const VVCLocalContext *lc, const MvField *mvf, const int c_idx, const int dmvr_flag)
Definition inter.c:222
static void scaled_ref_pos_and_step(const VVCLocalContext *lc, const VVCRefPic *refp, const Mv *mv, const int x_off, const int y_off, const int c_idx, int *x, int *y, int *dx, int *dy)
Definition inter.c:388
static void mc_uni(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride, const VVCFrame *ref, const MvField *mvf, int x_off, int y_off, const int block_w, const int block_h, const int c_idx)
Definition inter.c:282
static void luma_prof_bi(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride, const VVCRefPic *ref0, const VVCRefPic *ref1, const MvField *mvf, const int x_off, const int y_off, const int block_w, const int block_h)
Definition inter.c:560
static void subpic_get_rect(VVCRect *r, const VVCFrame *src_frame, const int subpic_idx, const int is_chroma)
Definition inter.c:35
static void derive_affine_mvc(MvField *mvc, const VVCFrameContext *fc, const MvField *mv, const int x0, const int y0, const int sbw, const int sbh)
Definition inter.c:904
#define MC_EMULATED_EDGE_DMVR(dst, src, src_stride, x_sb, y_sb, x_off, y_off)
Definition inter.c:193
static int parametric_mv_refine(const int *sad, const int stride)
Definition inter.c:733
static void pred_affine_blk(VVCLocalContext *lc)
Definition inter.c:922
#define SCALED_REF(ref_sb, offset, shift)
Definition inter.c:385
static void clip_to_subpic(int *x_off, int *y_off, int *pic_width, int *pic_height, const VVCRect *subpic, const VVCRect *sb, const int dmvr_clip)
Definition inter.c:49
static void sb_wrap(VVCRect *sb, const int wrap)
Definition inter.c:115
static void sb_set_lr(VVCRect *sb, const int l, const int r)
Definition inter.c:109
static void mc_uni_scaled(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride, const VVCRefPic *refp, const MvField *mvf, const int x_off, const int y_off, const int block_w, const int block_h, const int c_idx)
Definition inter.c:445
static const int8_t * inter_filter_scaled(const int scale, const int is_chroma, const int is_affine)
Definition inter.c:365
#define INTER_FILTER(t, frac)
Definition inter.c:255
static void emulated_edge_no_wrap(const VVCLocalContext *lc, uint8_t *dst, const uint8_t **src, ptrdiff_t *src_stride, int x_off, int y_off, const int block_w, const int block_h, const int extra_before, const int extra_after, const VVCRect *subpic, const VVCRect *sb, const int dmvr_clip)
Definition inter.c:62
static void pred_regular(VVCLocalContext *lc, const MvField *mvf, const MvField *orig_mvf, const int x0, const int y0, const int sbw, const int sbh, const int sb_bdof_flag, const int c_start)
Definition inter.c:678
int ff_vvc_predict_inter(VVCLocalContext *lc, const int rs)
Loop entire CTU to predict all inter coding blocks.
Definition inter.c:997
static void pred_regular_blk(VVCLocalContext *lc, const int skip_ciip)
Definition inter.c:876
static int derive_weight_uni(int *denom, int *wx, int *ox, const VVCLocalContext *lc, const MvField *mvf, const int c_idx)
Definition inter.c:202
#define SCALED_REF_SB(off, scaling_off, ref_mv, scale, add, shift)
Definition inter.c:384
static void mc_bi_scaled(VVCLocalContext *lc, uint8_t *dst, const ptrdiff_t dst_stride, const VVCRefPic *refp0, const VVCRefPic *refp1, const MvField *mvf, const int x_off, const int y_off, const int block_w, const int block_h, const int c_idx)
Definition inter.c:473
static void emulated_edge_scaled(VVCLocalContext *lc, const uint8_t **src, ptrdiff_t *src_stride, int *src_height, const VVCFrame *ref, const int x, const int y, const int dx, const int dy, const int w, const int h, const int is_chroma)
Definition inter.c:409
#define INTER_FILTER_SCALED(scale)
Definition inter.c:380
static void predict_inter(VVCLocalContext *lc)
Definition inter.c:973
static void mc_scaled(VVCLocalContext *lc, int16_t *dst, const VVCRefPic *refp, const Mv *mv, int x_off, int y_off, const int block_w, const int block_h, const int c_idx)
Definition inter.c:425
static void pred_gpm_blk(VVCLocalContext *lc)
Definition inter.c:599
#define MC_EMULATED_EDGE(dst, src, src_stride, x_off, y_off)
Definition inter.c:189
#define av_log2
Definition intmath.h:84
static void FUNC pred_h(uint8_t *_src, const uint8_t *_left, const int w, const int h, const ptrdiff_t stride)
unsigned offset
Definition libaomenc.c:812
static const int8_t mv[256][2]
Definition 4xm.c:81
static int16_t mult(Float11 *f1, Float11 *f2)
Definition g726.c:60
uint8_t w
Definition llvidencdsp.c:39
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:524
enum PredMode pred_mode
PredMode.
Definition hevcdec.h:295
int cb_width
Definition ctu.h:293
struct CodingUnit * next
RefStruct reference.
Definition ctu.h:342
int cb_height
Definition ctu.h:294
VVCTreeType tree_type
Definition ctu.h:290
PredictionUnit pu
Definition ctu.h:340
uint8_t ciip_flag
Definition ctu.h:314
int x0
Definition ctu.h:291
int y0
Definition ctu.h:292
uint8_t sh_lmcs_used_flag
Definition cbs_h266.h:795
uint16_t curr_subpic_idx
CurrSubpicIdx.
Definition cbs_h266.h:838
uint8_t hpel_if_idx
hpelIfIdx
Definition ctu.h:249
int8_t ref_idx[2]
refIdxL0, refIdxL1
Definition hevcdec.h:311
int8_t pred_flag
Definition hevcdec.h:312
uint8_t bcw_idx
bcwIdx
Definition ctu.h:208
Mv mv[2]
mvL0, vvL1
Definition hevcdec.h:310
Definition hevcdec.h:304
int16_t x
horizontal component of motion vector
Definition hevcdec.h:305
int16_t y
vertical component of motion vector
Definition hevcdec.h:306
int cb_prof_flag[2]
Definition ctu.h:281
uint8_t merge_gpm_flag
Definition ctu.h:267
MotionInfo mi
Definition ctu.h:273
uint8_t inter_affine_flag
Definition ctu.h:262
uint8_t dmvr_flag
Definition ctu.h:276
MvField gpm_mv[2]
Definition ctu.h:269
int16_t diff_mv_y[2][AFFINE_MIN_BLOCK_SIZE *AFFINE_MIN_BLOCK_SIZE]
diffMvLX
Definition ctu.h:280
uint8_t gpm_partition_idx
Definition ctu.h:268
int16_t diff_mv_x[2][AFFINE_MIN_BLOCK_SIZE *AFFINE_MIN_BLOCK_SIZE]
diffMvLX
Definition ctu.h:279
uint8_t bdof_flag
Definition ctu.h:277
VVCRefPic refs[VVC_MAX_REF_ENTRIES]
Definition dec.h:58
VVCSH sh
Definition dec.h:115
RefPicList * rpl
Definition dec.h:118
Definition dec.h:73
struct AVFrame * frame
Definition dec.h:74
VVCWindow scaling_win
pps_scaling_win_left_offset * SubWithC, pps_scaling_win_right_offset * SubWithC, pps_scaling_win_top_...
Definition dec.h:91
const VVCPPS * pps
RefStruct reference.
Definition dec.h:77
const VVCSPS * sps
RefStruct reference.
Definition dec.h:76
uint8_t ciip_tmp[MAX_PB_SIZE *MAX_PB_SIZE *2]
Definition ctu.h:452
CodingUnit * cu
Definition ctu.h:425
struct VVCLocalContext::@341344066320375062105147172263124117020342060326::@073072245072044000206134265330343062236310026066 pred
only accessed in ff_vvc_predict_inter() and ff_vvc_predict_ciip() during the inter and reconstruction...
uint8_t edge_emu_buffer[EDGE_EMU_BUFFER_STRIDE *EDGE_EMU_BUFFER_STRIDE *2]
Definition ctu.h:445
int16_t tmp[MAX_PB_SIZE *MAX_PB_SIZE *2]
Definition ctu.h:448
uint8_t ctb_up_flag
Definition ctu.h:388
VVCFrameContext * fc
Definition ctu.h:421
int16_t prof_tmp[MAX_PB_SIZE *MAX_PB_SIZE *2]
Definition ctu.h:451
int16_t tmp1[MAX_PB_SIZE *MAX_PB_SIZE *2]
Definition ctu.h:449
SliceContext * sc
Definition ctu.h:420
uint8_t ctb_left_flag
Definition ctu.h:387
uint8_t scaled_tmp[EDGE_EMU_BUFFER_STRIDE *MAX_PB_SIZE *sizeof(int)]
Definition ctu.h:447
Definition ps.h:92
Definition ctu.h:498
int r
Definition ctu.h:501
int b
Definition ctu.h:502
int l
Definition ctu.h:499
int t
Definition ctu.h:500
int scale[2]
RefPicScale[].
Definition dec.h:54
int is_scaled
RprConstraintsActiveFlag.
Definition dec.h:53
struct VVCFrame * ref
Definition dec.h:48
Definition ps.h:238
const H266RawSliceHeader * r
RefStruct reference.
Definition ps.h:239
PredWeightTable pwt
Definition ps.h:247
Definition ps.h:58
int16_t top_offset
Definition dec.h:69
int16_t left_offset
Definition dec.h:67
#define stride
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src1
Definition h264pred.c:141
#define src0
Definition h264pred.c:140
#define src
Definition vp8dsp.c:248
int num
Definition error.c:23
static int ref[MAX_W *MAX_W]
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
#define mc
#define mi
const uint8_t ff_vvc_gpm_angle_to_mirror[VVC_GPM_NUM_ANGLES]
Definition data.c:2095
const uint8_t ff_vvc_gpm_weights_offset_y[VVC_GPM_NUM_PARTITION][4][4]
Definition data.c:2493
const uint8_t ff_vvc_gpm_angle_idx[VVC_GPM_NUM_PARTITION]
Definition data.c:2077
const uint8_t ff_vvc_gpm_angle_to_weights_idx[VVC_GPM_NUM_ANGLES]
Definition data.c:2100
const uint8_t ff_vvc_gpm_weights_offset_x[VVC_GPM_NUM_PARTITION][4][4]
Definition data.c:2106
const int8_t ff_vvc_inter_luma_filters[VVC_INTER_LUMA_FILTER_TYPES][VVC_INTER_LUMA_FACTS][VVC_INTER_LUMA_TAPS]
Definition data.c:1735
const uint8_t ff_vvc_gpm_weights[6][VVC_GPM_WEIGHT_SIZE *VVC_GPM_WEIGHT_SIZE]
Definition data.c:2880
const int8_t ff_vvc_inter_chroma_filters[VVC_INTER_CHROMA_FILTER_TYPES][VVC_INTER_CHROMA_FACTS][VVC_INTER_CHROMA_TAPS]
Definition data.c:1877
#define VVC_INTER_LUMA_FILTER_TYPE_AFFINE
Definition data.h:49
#define VVC_GPM_WEIGHT_SIZE
Definition data.h:71
void ff_vvc_round_mv(Mv *mv, const int lshift, const int rshift)
Definition mvs.c:1886
MvField * ff_vvc_get_mvf(const VVCFrameContext *fc, const int x0, const int y0)
Definition mvs.c:1970
void ff_vvc_clip_mv(Mv *mv)
Definition mvs.c:1902
#define IS_P(rsh)
Definition ps.h:39
#define IS_B(rsh)
Definition ps.h:40