History

vvcdec: add motion vector decoder

Nuo Mi committed 2 years ago

Commit 603d0bd1 parent c1a3d175 files · +1865 −0Browse files

Showing the changes to libavcodec (5 of 5 files). Show all

Unified · Split

5 files
  1. libavcodec/vvc/Makefile +1 −0
  2. libavcodec/vvc/vvc_ctu.c +18 −0
  3. libavcodec/vvc/vvc_ctu.h +2 −0
  4. libavcodec/vvc/vvc_mvs.c +1798 −0
  5. libavcodec/vvc/vvc_mvs.h +46 −0
libavcodec/vvc/Makefile+1−0
@@ -4,5 +4,6 @@ clean:: · 3 unmodified lines
4OBJS-$(CONFIG_VVC_DECODER) += vvc/vvc_cabac.o \4OBJS-$(CONFIG_VVC_DECODER) += vvc/vvc_cabac.o \
5 vvc/vvc_ctu.o \5 vvc/vvc_ctu.o \
6 vvc/vvc_data.o \6 vvc/vvc_data.o \
7 vvc/vvc_mvs.o \
7 vvc/vvc_ps.o \8 vvc/vvc_ps.o \
8 vvc/vvc_refs.o \9 vvc/vvc_refs.o \
libavcodec/vvc/vvc_ctu.c+18−0
@@ -20,7 +20,25 @@ · 19 unmodified lines
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */21 */
22 22
23#include "vvc_cabac.h"
23#include "vvc_ctu.h"24#include "vvc_ctu.h"
25#include "vvc_mvs.h"
26
27void ff_vvc_set_neighbour_available(VVCLocalContext *lc,
28 const int x0, const int y0, const int w, const int h)
29{
30 const int log2_ctb_size = lc->fc->ps.sps->ctb_log2_size_y;
31 const int x0b = av_mod_uintp2(x0, log2_ctb_size);
32 const int y0b = av_mod_uintp2(y0, log2_ctb_size);
33
34 lc->na.cand_up = (lc->ctb_up_flag || y0b);
35 lc->na.cand_left = (lc->ctb_left_flag || x0b);
36 lc->na.cand_up_left = (x0b || y0b) ? lc->na.cand_left && lc->na.cand_up : lc->ctb_up_left_flag;
37 lc->na.cand_up_right_sap =
38 (x0b + w == 1 << log2_ctb_size) ? lc->ctb_up_right_flag && !y0b : lc->na.cand_up;
39 lc->na.cand_up_right = lc->na.cand_up_right_sap && (x0 + w) < lc->end_of_tiles_x;
40}
41
24 42
25void ff_vvc_ep_init_stat_coeff(EntryPoint *ep,43void ff_vvc_ep_init_stat_coeff(EntryPoint *ep,
26 const int bit_depth, const int persistent_rice_adaptation_enabled_flag)44 const int bit_depth, const int persistent_rice_adaptation_enabled_flag)
libavcodec/vvc/vvc_ctu.h+2−0
@@ -459,6 +459,8 @@ typedef struct ALFParams { · 458 unmodified lines
459 uint8_t applied[3];459 uint8_t applied[3];
460} ALFParams;460} ALFParams;
461 461
462//utils
463void ff_vvc_set_neighbour_available(VVCLocalContext *lc, int x0, int y0, int w, int h);
462void ff_vvc_ep_init_stat_coeff(EntryPoint *ep, int bit_depth, int persistent_rice_adaptation_enabled_flag);464void ff_vvc_ep_init_stat_coeff(EntryPoint *ep, int bit_depth, int persistent_rice_adaptation_enabled_flag);
463 465
464#endif // AVCODEC_VVC_VVC_CTU_H466#endif // AVCODEC_VVC_VVC_CTU_H
libavcodec/vvc/vvc_mvs.c+1798−0
@@ -0,0 +1,1798 @@
1/*
2 * VVC motion vector decoder
3 *
4 * Copyright (C) 2023 Nuo Mi
5 * Copyright (C) 2022 Xu Mu
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
23#include "vvc_ctu.h"
24#include "vvc_data.h"
25#include "vvc_refs.h"
26#include "vvc_mvs.h"
27
28#define IS_SAME_MV(a, b) (AV_RN64A(a) == AV_RN64A(b))
29
30//check if the two luma locations belong to the same motion estimation region
31static av_always_inline int is_same_mer(const VVCFrameContext *fc, const int xN, const int yN, const int xP, const int yP)
32{
33 const uint8_t plevel = fc->ps.sps->log2_parallel_merge_level;
34
35 return xN >> plevel == xP >> plevel &&
36 yN >> plevel == yP >> plevel;
37}
38
39//return true if we have same mvs and ref_idxs
40static av_always_inline int compare_mv_ref_idx(const MvField *n, const MvField *o)
41{
42 if (!o || n->pred_flag != o->pred_flag)
43 return 0;
44 for (int i = 0; i < 2; i++) {
45 PredFlag mask = i + 1;
46 if (n->pred_flag & mask) {
47 const int same_ref_idx = n->ref_idx[i] == o->ref_idx[i];
48 const int same_mv = IS_SAME_MV(n->mv + i, o->mv + i);
49 if (!same_ref_idx || !same_mv)
50 return 0;
51 }
52 }
53 return 1;
54}
55
56// 8.5.2.15 Temporal motion buffer compression process for collocated motion vectors
57static av_always_inline void mv_compression(Mv *motion)
58{
59 int mv[2] = {motion->x, motion->y};
60 for (int i = 0; i < 2; i++) {
61 const int s = mv[i] >> 17;
62 const int f = av_log2((mv[i] ^ s) | 31) - 4;
63 const int mask = (-1 << f) >> 1;
64 const int round = (1 << f) >> 2;
65 mv[i] = (mv[i] + round) & mask;
66 }
67 motion->x = mv[0];
68 motion->y = mv[1];
69}
70
71void ff_vvc_mv_scale(Mv *dst, const Mv *src, int td, int tb)
72{
73 int tx, scale_factor;
74
75 td = av_clip_int8(td);
76 tb = av_clip_int8(tb);
77 tx = (0x4000 + (abs(td) >> 1)) / td;
78 scale_factor = av_clip_intp2((tb * tx + 32) >> 6, 12);
79 dst->x = av_clip_intp2((scale_factor * src->x + 127 +
80 (scale_factor * src->x < 0)) >> 8, 17);
81 dst->y = av_clip_intp2((scale_factor * src->y + 127 +
82 (scale_factor * src->y < 0)) >> 8, 17);
83}
84
85//part of 8.5.2.12 Derivation process for collocated motion vectors
86static int check_mvset(Mv *mvLXCol, Mv *mvCol,
87 int colPic, int poc,
88 const RefPicList *refPicList, int X, int refIdxLx,
89 const RefPicList *refPicList_col, int listCol, int refidxCol)
90{
91 int cur_lt = refPicList[X].isLongTerm[refIdxLx];
92 int col_lt = refPicList_col[listCol].isLongTerm[refidxCol];
93 int col_poc_diff, cur_poc_diff;
94
95 if (cur_lt != col_lt) {
96 mvLXCol->x = 0;
97 mvLXCol->y = 0;
98 return 0;
99 }
100
101 col_poc_diff = colPic - refPicList_col[listCol].list[refidxCol];
102 cur_poc_diff = poc - refPicList[X].list[refIdxLx];
103
104 mv_compression(mvCol);
105 if (cur_lt || col_poc_diff == cur_poc_diff) {
106 mvLXCol->x = av_clip_intp2(mvCol->x, 17);
107 mvLXCol->y = av_clip_intp2(mvCol->y, 17);
108 } else {
109 ff_vvc_mv_scale(mvLXCol, mvCol, col_poc_diff, cur_poc_diff);
110 }
111 return 1;
112}
113
114#define CHECK_MVSET(l) \
115 check_mvset(mvLXCol, temp_col.mv + l, \
116 colPic, fc->ps.ph.poc, \
117 refPicList, X, refIdxLx, \
118 refPicList_col, L ## l, temp_col.ref_idx[l])
119
120//derive NoBackwardPredFlag
121int ff_vvc_no_backward_pred_flag(const VVCLocalContext *lc)
122{
123 int check_diffpicount = 0;
124 int i, j;
125 const RefPicList *rpl = lc->sc->rpl;
126
127 for (j = 0; j < 2; j++) {
128 for (i = 0; i < rpl[j].nb_refs; i++) {
129 if (rpl[j].list[i] > lc->fc->ps.ph.poc) {
130 check_diffpicount++;
131 break;
132 }
133 }
134 }
135 return !check_diffpicount;
136}
137
138//8.5.2.12 Derivation process for collocated motion vectors
139static int derive_temporal_colocated_mvs(const VVCLocalContext *lc, MvField temp_col,
140 int refIdxLx, Mv *mvLXCol, int X,
141 int colPic, const RefPicList *refPicList_col, int sb_flag)
142{
143 const VVCFrameContext *fc = lc->fc;
144 const SliceContext *sc = lc->sc;
145 RefPicList* refPicList = sc->rpl;
146
147 if (temp_col.pred_flag == PF_INTRA)
148 return 0;
149
150 if (sb_flag){
151 if (X == 0) {
152 if (temp_col.pred_flag & PF_L0)
153 return CHECK_MVSET(0);
154 else if (ff_vvc_no_backward_pred_flag(lc) && (temp_col.pred_flag & PF_L1))
155 return CHECK_MVSET(1);
156 } else {
157 if (temp_col.pred_flag & PF_L1)
158 return CHECK_MVSET(1);
159 else if (ff_vvc_no_backward_pred_flag(lc) && (temp_col.pred_flag & PF_L0))
160 return CHECK_MVSET(0);
161 }
162 } else {
163 if (!(temp_col.pred_flag & PF_L0))
164 return CHECK_MVSET(1);
165 else if (temp_col.pred_flag == PF_L0)
166 return CHECK_MVSET(0);
167 else if (temp_col.pred_flag == PF_BI) {
168 if (ff_vvc_no_backward_pred_flag(lc)) {
169 if (X == 0)
170 return CHECK_MVSET(0);
171 else
172 return CHECK_MVSET(1);
173 } else {
174 if (!lc->sc->sh.r->sh_collocated_from_l0_flag)
175 return CHECK_MVSET(0);
176 else
177 return CHECK_MVSET(1);
178 }
179 }
180 }
181 return 0;
182}
183
184#define TAB_MVF(x, y) \
185 tab_mvf[((y) >> MIN_PU_LOG2) * min_pu_width + ((x) >> MIN_PU_LOG2)]
186
187#define TAB_MVF_PU(v) \
188 TAB_MVF(x ## v, y ## v)
189
190#define TAB_CP_MV(lx, x, y) \
191 fc->tab.cp_mv[lx][((((y) >> min_cb_log2_size) * min_cb_width + ((x) >> min_cb_log2_size)) ) * MAX_CONTROL_POINTS]
192
193
194#define DERIVE_TEMPORAL_COLOCATED_MVS(sb_flag) \
195 derive_temporal_colocated_mvs(lc, temp_col, \
196 refIdxLx, mvLXCol, X, colPic, \
197 ff_vvc_get_ref_list(fc, ref, x, y), sb_flag)
198
199//8.5.2.11 Derivation process for temporal luma motion vector prediction
200static int temporal_luma_motion_vector(const VVCLocalContext *lc,
201 const int refIdxLx, Mv *mvLXCol, const int X, int check_center, int sb_flag)
202{
203 const VVCFrameContext *fc = lc->fc;
204 const VVCSPS *sps = fc->ps.sps;
205 const CodingUnit *cu = lc->cu;
206 int x, y, colPic, availableFlagLXCol = 0;
207 int min_pu_width = fc->ps.pps->min_pu_width;
208 VVCFrame *ref = fc->ref->collocated_ref;
209 MvField *tab_mvf;
210 MvField temp_col;
211
212 if (!ref) {
213 memset(mvLXCol, 0, sizeof(*mvLXCol));
214 return 0;
215 }
216
217 if (!fc->ps.ph.r->ph_temporal_mvp_enabled_flag || (cu->cb_width * cu->cb_height <= 32))
218 return 0;
219
220 tab_mvf = ref->tab_dmvr_mvf;
221 colPic = ref->poc;
222
223 //bottom right collocated motion vector
224 x = cu->x0 + cu->cb_width;
225 y = cu->y0 + cu->cb_height;
226
227 if (tab_mvf &&
228 (cu->y0 >> sps->ctb_log2_size_y) == (y >> sps->ctb_log2_size_y) &&
229 y < fc->ps.sps->height &&
230 x < fc->ps.sps->width) {
231 x &= ~7;
232 y &= ~7;
233 temp_col = TAB_MVF(x, y);
234 availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS(sb_flag);
235 }
236 if (check_center) {
237 // derive center collocated motion vector
238 if (tab_mvf && !availableFlagLXCol) {
239 x = cu->x0 + (cu->cb_width >> 1);
240 y = cu->y0 + (cu->cb_height >> 1);
241 x &= ~7;
242 y &= ~7;
243 temp_col = TAB_MVF(x, y);
244 availableFlagLXCol = DERIVE_TEMPORAL_COLOCATED_MVS(sb_flag);
245 }
246 }
247 return availableFlagLXCol;
248}
249
250void ff_vvc_set_mvf(const VVCLocalContext *lc, const int x0, const int y0, const int w, const int h, const MvField *mvf)
251{
252 const VVCFrameContext *fc = lc->fc;
253 MvField *tab_mvf = fc->tab.mvf;
254 const int min_pu_width = fc->ps.pps->min_pu_width;
255 const int min_pu_size = 1 << MIN_PU_LOG2;
256 for (int dy = 0; dy < h; dy += min_pu_size) {
257 for (int dx = 0; dx < w; dx += min_pu_size) {
258 const int x = x0 + dx;
259 const int y = y0 + dy;
260 TAB_MVF(x, y) = *mvf;
261 }
262 }
263}
264
265void ff_vvc_set_intra_mvf(const VVCLocalContext *lc)
266{
267 const VVCFrameContext *fc = lc->fc;
268 const CodingUnit *cu = lc->cu;
269 MvField *tab_mvf = fc->tab.mvf;
270 const int min_pu_width = fc->ps.pps->min_pu_width;
271 const int min_pu_size = 1 << MIN_PU_LOG2;
272 for (int dy = 0; dy < cu->cb_height; dy += min_pu_size) {
273 for (int dx = 0; dx < cu->cb_width; dx += min_pu_size) {
274 const int x = cu->x0 + dx;
275 const int y = cu->y0 + dy;
276 TAB_MVF(x, y).pred_flag = PF_INTRA;
277 }
278 }
279}
280
281//cbProfFlagLX from 8.5.5.9 Derivation process for motion vector arrays from affine control point motion vectors
282static int derive_cb_prof_flag_lx(const VVCLocalContext *lc, const PredictionUnit* pu, int lx, int is_fallback)
283{
284 const MotionInfo* mi = &pu->mi;
285 const Mv* cp_mv = &mi->mv[lx][0];
286 if (lc->fc->ps.ph.r->ph_prof_disabled_flag || is_fallback)
287 return 0;
288 if (mi->motion_model_idc == MOTION_4_PARAMS_AFFINE) {
289 if (IS_SAME_MV(cp_mv, cp_mv + 1))
290 return 0;
291 }
292 if (mi->motion_model_idc == MOTION_6_PARAMS_AFFINE) {
293 if (IS_SAME_MV(cp_mv, cp_mv + 1) && IS_SAME_MV(cp_mv, cp_mv + 2))
294 return 0;
295 }
296 //fixme: RprConstraintsActiveFlag
297 return 1;
298}
299
300typedef struct SubblockParams {
301 int d_hor_x;
302 int d_ver_x;
303 int d_hor_y;
304 int d_ver_y;
305 int mv_scale_hor;
306 int mv_scale_ver;
307 int is_fallback;
308
309 int cb_width;
310 int cb_height;
311} SubblockParams;
312
313static int is_fallback_mode(const SubblockParams *sp, const PredFlag pred_flag)
314{
315 const int a = 4 * (2048 + sp->d_hor_x);
316 const int b = 4 * sp->d_hor_y;
317 const int c = 4 * (2048 + sp->d_ver_y);
318 const int d = 4 * sp->d_ver_x;
319 if (pred_flag == PF_BI) {
320 const int max_w4 = FFMAX(0, FFMAX(a, FFMAX(b, a + b)));
321 const int min_w4 = FFMIN(0, FFMIN(a, FFMIN(b, a + b)));
322 const int max_h4 = FFMAX(0, FFMAX(c, FFMAX(d, c + d)));
323 const int min_h4 = FFMIN(0, FFMIN(c, FFMIN(d, c + d)));
324 const int bx_wx4 = ((max_w4 - min_w4) >> 11) + 9;
325 const int bx_hx4 = ((max_h4 - min_h4) >> 11) + 9;
326 return bx_wx4 * bx_hx4 > 225;
327 } else {
328 const int bx_wxh = (FFABS(a) >> 11) + 9;
329 const int bx_hxh = (FFABS(d) >> 11) + 9;
330 const int bx_wxv = (FFABS(b) >> 11) + 9;
331 const int bx_hxv = (FFABS(c) >> 11) + 9;
332 if (bx_wxh * bx_hxh <= 165 && bx_wxv * bx_hxv <= 165)
333 return 0;
334 }
335 return 1;
336}
337
338static void init_subblock_params(SubblockParams *sp, const MotionInfo* mi,
339 const int cb_width, const int cb_height, const int lx)
340{
341 const int log2_cbw = av_log2(cb_width);
342 const int log2_cbh = av_log2(cb_height);
343 const Mv* cp_mv = mi->mv[lx];
344 const int num_cp_mv = mi->motion_model_idc + 1;
345 sp->d_hor_x = (cp_mv[1].x - cp_mv[0].x) << (MAX_CU_DEPTH - log2_cbw);
346 sp->d_ver_x = (cp_mv[1].y - cp_mv[0].y) << (MAX_CU_DEPTH - log2_cbw);
347 if (num_cp_mv == 3) {
348 sp->d_hor_y = (cp_mv[2].x - cp_mv[0].x) << (MAX_CU_DEPTH - log2_cbh);
349 sp->d_ver_y = (cp_mv[2].y - cp_mv[0].y) << (MAX_CU_DEPTH - log2_cbh);
350 } else {
351 sp->d_hor_y = -sp->d_ver_x;
352 sp->d_ver_y = sp->d_hor_x;
353 }
354 sp->mv_scale_hor = (cp_mv[0].x) << MAX_CU_DEPTH;
355 sp->mv_scale_ver = (cp_mv[0].y) << MAX_CU_DEPTH;
356 sp->cb_width = cb_width;
357 sp->cb_height = cb_height;
358 sp->is_fallback = is_fallback_mode(sp, mi->pred_flag);
359}
360
361static void derive_subblock_diff_mvs(const VVCLocalContext *lc, PredictionUnit* pu, const SubblockParams* sp, const int lx)
362{
363 pu->cb_prof_flag[lx] = derive_cb_prof_flag_lx(lc, pu, lx, sp->is_fallback);
364 if (pu->cb_prof_flag[lx]) {
365 const int dmv_limit = 1 << 5;
366 const int pos_offset_x = 6 * (sp->d_hor_x + sp->d_hor_y);
367 const int pos_offset_y = 6 * (sp->d_ver_x + sp->d_ver_y);
368 for (int x = 0; x < AFFINE_MIN_BLOCK_SIZE; x++) {
369 for (int y = 0; y < AFFINE_MIN_BLOCK_SIZE; y++) {
370 Mv diff;
371 diff.x = x * (sp->d_hor_x << 2) + y * (sp->d_hor_y << 2) - pos_offset_x;
372 diff.y = x * (sp->d_ver_x << 2) + y * (sp->d_ver_y << 2) - pos_offset_y;
373 ff_vvc_round_mv(&diff, 0, 8);
374 pu->diff_mv_x[lx][AFFINE_MIN_BLOCK_SIZE * y + x] = av_clip(diff.x, -dmv_limit + 1, dmv_limit - 1);
375 pu->diff_mv_y[lx][AFFINE_MIN_BLOCK_SIZE * y + x] = av_clip(diff.y, -dmv_limit + 1, dmv_limit - 1);
376 }
377 }
378 }
379}
380
381static void store_cp_mv(const VVCLocalContext *lc, const MotionInfo *mi, const int lx)
382{
383 VVCFrameContext *fc = lc->fc;
384 const CodingUnit *cu = lc->cu;
385 const int log2_min_cb_size = fc->ps.sps->min_cb_log2_size_y;
386 const int min_cb_size = fc->ps.sps->min_cb_size_y;
387 const int min_cb_width = fc->ps.pps->min_cb_width;
388 const int num_cp_mv = mi->motion_model_idc + 1;
389
390 for (int dy = 0; dy < cu->cb_height; dy += min_cb_size) {
391 for (int dx = 0; dx < cu->cb_width; dx += min_cb_size) {
392 const int x_cb = (cu->x0 + dx) >> log2_min_cb_size;
393 const int y_cb = (cu->y0 + dy) >> log2_min_cb_size;
394 const int offset = (y_cb * min_cb_width + x_cb) * MAX_CONTROL_POINTS;
395
396 memcpy(&fc->tab.cp_mv[lx][offset], mi->mv[lx], sizeof(Mv) * num_cp_mv);
397 SAMPLE_CTB(fc->tab.mmi, x_cb, y_cb) = mi->motion_model_idc;
398 }
399 }
400}
401
402//8.5.5.9 Derivation process for motion vector arrays from affine control point motion vectors
403void ff_vvc_store_sb_mvs(const VVCLocalContext *lc, PredictionUnit *pu)
404{
405 const CodingUnit *cu = lc->cu;
406 const MotionInfo *mi = &pu->mi;
407 const int sbw = cu->cb_width / mi->num_sb_x;
408 const int sbh = cu->cb_height / mi->num_sb_y;
409 SubblockParams params[2];
410 MvField mvf;
411
412 mvf.pred_flag = mi->pred_flag;
413 mvf.bcw_idx = mi->bcw_idx;
414 mvf.hpel_if_idx = mi->hpel_if_idx;
415 mvf.ciip_flag = 0;
416 for (int i = 0; i < 2; i++) {
417 const PredFlag mask = i + 1;
418 if (mi->pred_flag & mask) {
419 store_cp_mv(lc, mi, i);
420 init_subblock_params(params + i, mi, cu->cb_width, cu->cb_height, i);
421 derive_subblock_diff_mvs(lc, pu, params + i, i);
422 mvf.ref_idx[i] = mi->ref_idx[i];
423 }
424 }
425
426 for (int sby = 0; sby < mi->num_sb_y; sby++) {
427 for (int sbx = 0; sbx < mi->num_sb_x; sbx++) {
428 const int x0 = cu->x0 + sbx * sbw;
429 const int y0 = cu->y0 + sby * sbh;
430 for (int i = 0; i < 2; i++) {
431 const PredFlag mask = i + 1;
432 if (mi->pred_flag & mask) {
433 const SubblockParams* sp = params + i;
434 const int x_pos_cb = sp->is_fallback ? (cu->cb_width >> 1) : (2 + (sbx << MIN_CU_LOG2));
435 const int y_pos_cb = sp->is_fallback ? (cu->cb_height >> 1) : (2 + (sby << MIN_CU_LOG2));
436 Mv *mv = mvf.mv + i;
437
438 mv->x = sp->mv_scale_hor + sp->d_hor_x * x_pos_cb + sp->d_hor_y * y_pos_cb;
439 mv->y = sp->mv_scale_ver + sp->d_ver_x * x_pos_cb + sp->d_ver_y * y_pos_cb;
440 ff_vvc_round_mv(mv, 0, MAX_CU_DEPTH);
441 ff_vvc_clip_mv(mv);
442 }
443 }
444 ff_vvc_set_mvf(lc, x0, y0, sbw, sbh, &mvf);
445 }
446 }
447}
448
449void ff_vvc_store_gpm_mvf(const VVCLocalContext *lc, const PredictionUnit *pu)
450{
451 const CodingUnit *cu = lc->cu;
452 const int angle_idx = ff_vvc_gpm_angle_idx[pu->gpm_partition_idx];
453 const int distance_idx = ff_vvc_gpm_distance_idx[pu->gpm_partition_idx];
454 const int displacement_x = ff_vvc_gpm_distance_lut[angle_idx];
455 const int displacement_y = ff_vvc_gpm_distance_lut[(angle_idx + 8) % 32];
456 const int is_flip = angle_idx >= 13 &&angle_idx <= 27;
457 const int shift_hor = (angle_idx % 16 == 8 || (angle_idx % 16 && cu->cb_height >= cu->cb_width)) ? 0 : 1;
458 const int sign = angle_idx < 16 ? 1 : -1;
459 const int block_size = 4;
460 int offset_x = (-cu->cb_width) >> 1;
461 int offset_y = (-cu->cb_height) >> 1;
462
463 if (!shift_hor)
464 offset_y += sign * ((distance_idx * cu->cb_height) >> 3);
465 else
466 offset_x += sign * ((distance_idx * cu->cb_width) >> 3);
467
468 for (int y = 0; y < cu->cb_height; y += block_size) {
469 for (int x = 0; x < cu->cb_width; x += block_size) {
470 const int motion_idx = (((x + offset_x) << 1) + 5) * displacement_x +
471 (((y + offset_y) << 1) + 5) * displacement_y;
472 const int s_type = FFABS(motion_idx) < 32 ? 2 : (motion_idx <= 0 ? (1 - is_flip) : is_flip);
473 const int pred_flag = pu->gpm_mv[0].pred_flag | pu->gpm_mv[1].pred_flag;
474 const int x0 = cu->x0 + x;
475 const int y0 = cu->y0 + y;
476
477 if (!s_type)
478 ff_vvc_set_mvf(lc, x0, y0, block_size, block_size, pu->gpm_mv + 0);
479 else if (s_type == 1 || (s_type == 2 && pred_flag != PF_BI))
480 ff_vvc_set_mvf(lc, x0, y0, block_size, block_size, pu->gpm_mv + 1);
481 else {
482 MvField mvf = pu->gpm_mv[0];
483 const MvField *mv1 = &pu->gpm_mv[1];
484 const int lx = mv1->pred_flag - PF_L0;
485 mvf.pred_flag = PF_BI;
486 mvf.ref_idx[lx] = mv1->ref_idx[lx];
487 mvf.mv[lx] = mv1->mv[lx];
488 ff_vvc_set_mvf(lc, x0, y0, block_size, block_size, &mvf);
489 }
490 }
491 }
492}
493
494void ff_vvc_store_mvf(const VVCLocalContext *lc, const MvField *mvf)
495{
496 const CodingUnit *cu = lc->cu;
497 ff_vvc_set_mvf(lc, cu->x0, cu->y0, cu->cb_width, cu->cb_height, mvf);
498}
499
500void ff_vvc_store_mv(const VVCLocalContext *lc, const MotionInfo *mi)
501{
502 const CodingUnit *cu = lc->cu;
503 MvField mvf;
504
505 mvf.hpel_if_idx = mi->hpel_if_idx;
506 mvf.bcw_idx = mi->bcw_idx;
507 mvf.pred_flag = mi->pred_flag;
508 mvf.ciip_flag = 0;
509
510 for (int i = 0; i < 2; i++) {
511 const PredFlag mask = i + 1;
512 if (mvf.pred_flag & mask) {
513 mvf.mv[i] = mi->mv[i][0];
514 mvf.ref_idx[i] = mi->ref_idx[i];
515 }
516 }
517 ff_vvc_set_mvf(lc, cu->x0, cu->y0, cu->cb_width, cu->cb_height, &mvf);
518}
519
520typedef enum NeighbourIdx {
521 A0,
522 A1,
523 A2,
524 B0,
525 B1,
526 B2,
527 B3,
528 NUM_NBS,
529 NB_IDX_NONE = NUM_NBS,
530} NeighbourIdx;
531
532typedef struct Neighbour {
533 int x;
534 int y;
535
536 int checked;
537 int available;
538} Neighbour;
539
540typedef struct NeighbourContext {
541 Neighbour neighbours[NUM_NBS];
542 const VVCLocalContext *lc;
543} NeighbourContext;
544
545static int is_a0_available(const VVCLocalContext *lc, const CodingUnit *cu)
546{
547 const VVCFrameContext *fc = lc->fc;
548 const VVCSPS *sps = fc->ps.sps;
549 const int x0b = av_mod_uintp2(cu->x0, sps->ctb_log2_size_y);
550 int cand_bottom_left;
551
552 if (!x0b && !lc->ctb_left_flag) {
553 cand_bottom_left = 0;
554 } else {
555 const int log2_min_cb_size = sps->min_cb_log2_size_y;
556 const int min_cb_width = fc->ps.pps->min_cb_width;
557 const int x = (cu->x0 - 1) >> log2_min_cb_size;
558 const int y = (cu->y0 + cu->cb_height) >> log2_min_cb_size;
559 const int max_y = FFMIN(fc->ps.pps->height, ((cu->y0 >> sps->ctb_log2_size_y) + 1) << sps->ctb_log2_size_y);
560 if (cu->y0 + cu->cb_height >= max_y)
561 cand_bottom_left = 0;
562 else
563 cand_bottom_left = SAMPLE_CTB(fc->tab.cb_width[0], x, y) != 0;
564 }
565 return cand_bottom_left;
566}
567
568static void init_neighbour_context(NeighbourContext *ctx, const VVCLocalContext *lc)
569{
570 const CodingUnit *cu = lc->cu;
571 const NeighbourAvailable *na = &lc->na;
572 const int x0 = cu->x0;
573 const int y0 = cu->y0;
574 const int cb_width = cu->cb_width;
575 const int cb_height = cu->cb_height;
576 const int a0_available = is_a0_available(lc, cu);
577
578 Neighbour neighbours[NUM_NBS] = {
579 { x0 - 1, y0 + cb_height, !a0_available }, //A0
580 { x0 - 1, y0 + cb_height - 1, !na->cand_left }, //A1
581 { x0 - 1, y0, !na->cand_left }, //A2
582 { x0 + cb_width, y0 - 1, !na->cand_up_right }, //B0
583 { x0 + cb_width - 1, y0 - 1, !na->cand_up }, //B1
584 { x0 - 1, y0 - 1, !na->cand_up_left }, //B2
585 { x0, y0 - 1, !na->cand_up }, //B3
586 };
587
588 memcpy(ctx->neighbours, neighbours, sizeof(neighbours));
589 ctx->lc = lc;
590}
591
592static int check_available(Neighbour *n, const VVCLocalContext *lc, const int is_mvp)
593{
594 const VVCFrameContext *fc = lc->fc;
595 const VVCSPS *sps = fc->ps.sps;
596 const CodingUnit *cu = lc->cu;
597 const MvField *tab_mvf = fc->tab.mvf;
598 const int min_pu_width = fc->ps.pps->min_pu_width;
599
600 if (!n->checked) {
601 n->checked = 1;
602 n->available = !sps->r->sps_entropy_coding_sync_enabled_flag || ((n->x >> sps->ctb_log2_size_y) <= (cu->x0 >> sps->ctb_log2_size_y));
603 n->available &= TAB_MVF(n->x, n->y).pred_flag != PF_INTRA;
604 if (!is_mvp)
605 n->available &= !is_same_mer(fc, n->x, n->y, cu->x0, cu->y0);
606 }
607 return n->available;
608}
609
610static const MvField *mv_merge_candidate(const VVCLocalContext *lc, const int x_cand, const int y_cand)
611{
612 const VVCFrameContext *fc = lc->fc;
613 const int min_pu_width = fc->ps.pps->min_pu_width;
614 const MvField* tab_mvf = fc->tab.mvf;
615 const MvField *mvf = &TAB_MVF(x_cand, y_cand);
616
617 return mvf;
618}
619
620static const MvField* mv_merge_from_nb(NeighbourContext *ctx, const NeighbourIdx nb)
621{
622 const VVCLocalContext *lc = ctx->lc;
623 const int is_mvp = 0;
624 Neighbour *n = &ctx->neighbours[nb];
625
626 if (check_available(n, lc, is_mvp))
627 return mv_merge_candidate(lc, n->x, n->y);
628 return 0;
629}
630#define MV_MERGE_FROM_NB(nb) mv_merge_from_nb(&nctx, nb)
631
632//8.5.2.3 Derivation process for spatial merging candidates
633static int mv_merge_spatial_candidates(const VVCLocalContext *lc, const int merge_idx,
634 const MvField **nb_list, MvField *cand_list, int *nb_merge_cand)
635{
636 const MvField *cand;
637 int num_cands = 0;
638 NeighbourContext nctx;
639
640 static NeighbourIdx nbs[][2] = {
641 {B1, NB_IDX_NONE },
642 {A1, B1 },
643 {B0, B1 },
644 {A0, A1 },
645 };
646
647 init_neighbour_context(&nctx, lc);
648 for (int i = 0; i < FF_ARRAY_ELEMS(nbs); i++) {
649 NeighbourIdx nb = nbs[i][0];
650 NeighbourIdx old = nbs[i][1];
651 cand = nb_list[nb] = MV_MERGE_FROM_NB(nb);
652 if (cand && !compare_mv_ref_idx(cand, nb_list[old])) {
653 cand_list[num_cands] = *cand;
654 if (merge_idx == num_cands)
655 return 1;
656 num_cands++;
657 }
658 }
659 if (num_cands != 4) {
660 cand = MV_MERGE_FROM_NB(B2);
661 if (cand && !compare_mv_ref_idx(cand, nb_list[A1])
662 && !compare_mv_ref_idx(cand, nb_list[B1])) {
663 cand_list[num_cands] = *cand;
664 if (merge_idx == num_cands)
665 return 1;
666 num_cands++;
667 }
668 }
669 *nb_merge_cand = num_cands;
670 return 0;
671}
672
673static int mv_merge_temporal_candidate(const VVCLocalContext *lc, MvField *cand)
674{
675 const VVCFrameContext *fc = lc->fc;
676 const CodingUnit *cu = lc->cu;
677
678 memset(cand, 0, sizeof(*cand));
679 if (fc->ps.ph.r->ph_temporal_mvp_enabled_flag && (cu->cb_width * cu->cb_height > 32)) {
680 int available_l0 = temporal_luma_motion_vector(lc, 0, cand->mv + 0, 0, 1, 0);
681 int available_l1 = IS_B(lc->sc->sh.r) ?
682 temporal_luma_motion_vector(lc, 0, cand->mv + 1, 1, 1, 0) : 0;
683 cand->pred_flag = available_l0 + (available_l1 << 1);
684 }
685 return cand->pred_flag;
686}
687
688//8.5.2.6 Derivation process for history-based merging candidates
689static int mv_merge_history_candidates(const VVCLocalContext *lc, const int merge_idx,
690 const MvField **nb_list, MvField *cand_list, int *num_cands)
691{
692 const VVCSPS *sps = lc->fc->ps.sps;
693 const EntryPoint* ep = lc->ep;
694 for (int i = 1; i <= ep->num_hmvp && (*num_cands < sps->max_num_merge_cand - 1); i++) {
695 const MvField *h = &ep->hmvp[ep->num_hmvp - i];
696 const int same_motion = i <= 2 && (compare_mv_ref_idx(h, nb_list[A1]) || compare_mv_ref_idx(h, nb_list[B1]));
697 if (!same_motion) {
698 cand_list[*num_cands] = *h;
699 if (merge_idx == *num_cands)
700 return 1;
701 (*num_cands)++;
702 }
703 }
704 return 0;
705}
706
707//8.5.2.4 Derivation process for pairwise average merging candidate
708static int mv_merge_pairwise_candidate(MvField *cand_list, const int num_cands, const int is_b)
709{
710 if (num_cands > 1) {
711 const int num_ref_rists = is_b ? 2 : 1;
712 const MvField* p0 = cand_list + 0;
713 const MvField* p1 = cand_list + 1;
714 MvField* cand = cand_list + num_cands;
715
716 cand->pred_flag = 0;
717 for (int i = 0; i < num_ref_rists; i++) {
718 PredFlag mask = i + 1;
719 if (p0->pred_flag & mask) {
720 cand->pred_flag |= mask;
721 cand->ref_idx[i] = p0->ref_idx[i];
722 if (p1->pred_flag & mask) {
723 Mv *mv = cand->mv + i;
724 mv->x = p0->mv[i].x + p1->mv[i].x;
725 mv->y = p0->mv[i].y + p1->mv[i].y;
726 ff_vvc_round_mv(mv, 0, 1);
727 } else {
728 cand->mv[i] = p0->mv[i];
729 }
730 } else if (p1->pred_flag & mask) {
731 cand->pred_flag |= mask;
732 cand->mv[i] = p1->mv[i];
733 cand->ref_idx[i] = p1->ref_idx[i];
734 }
735 }
736 if (cand->pred_flag) {
737 cand->hpel_if_idx = p0->hpel_if_idx == p1->hpel_if_idx ? p0->hpel_if_idx : 0;
738 cand->bcw_idx = 0;
739 cand->ciip_flag = 0;
740 return 1;
741 }
742 }
743 return 0;
744}
745
746//8.5.2.5 Derivation process for zero motion vector merging candidates
747static void mv_merge_zero_motion_candidate(const VVCLocalContext *lc, const int merge_idx,
748 MvField *cand_list, int num_cands)
749{
750 const VVCSPS *sps = lc->fc->ps.sps;
751 const H266RawSliceHeader *rsh = lc->sc->sh.r;
752 const int num_ref_idx = IS_P(rsh) ?
753 rsh->num_ref_idx_active[L0] : FFMIN(rsh->num_ref_idx_active[L0], rsh->num_ref_idx_active[L1]);
754 int zero_idx = 0;
755
756 while (num_cands < sps->max_num_merge_cand) {
757 MvField *cand = cand_list + num_cands;
758
759 cand->pred_flag = PF_L0 + (IS_B(rsh) << 1);
760 AV_ZERO64(cand->mv + 0);
761 AV_ZERO64(cand->mv + 1);
762 cand->ref_idx[0] = zero_idx < num_ref_idx ? zero_idx : 0;
763 cand->ref_idx[1] = zero_idx < num_ref_idx ? zero_idx : 0;
764 cand->bcw_idx = 0;
765 cand->hpel_if_idx = 0;
766 if (merge_idx == num_cands)
767 return;
768 num_cands++;
769 zero_idx++;
770 }
771}
772
773static void mv_merge_mode(const VVCLocalContext *lc, const int merge_idx, MvField *cand_list)
774{
775 int num_cands = 0;
776 const MvField *nb_list[NUM_NBS + 1] = { NULL };
777
778 if (mv_merge_spatial_candidates(lc, merge_idx, nb_list, cand_list, &num_cands))
779 return;
780
781 if (mv_merge_temporal_candidate(lc, &cand_list[num_cands])) {
782 if (merge_idx == num_cands)
783 return;
784 num_cands++;
785 }
786
787 if (mv_merge_history_candidates(lc, merge_idx, nb_list, cand_list, &num_cands))
788 return;
789
790 if (mv_merge_pairwise_candidate(cand_list, num_cands, IS_B(lc->sc->sh.r))) {
791 if (merge_idx == num_cands)
792 return;
793 num_cands++;
794 }
795
796 mv_merge_zero_motion_candidate(lc, merge_idx, cand_list, num_cands);
797}
798
799//8.5.2.2 Derivation process for luma motion vectors for merge mode
800void ff_vvc_luma_mv_merge_mode(VVCLocalContext *lc, const int merge_idx, const int ciip_flag, MvField *mv)
801{
802 const CodingUnit *cu = lc->cu;
803 MvField cand_list[MRG_MAX_NUM_CANDS];
804
805 ff_vvc_set_neighbour_available(lc, cu->x0, cu->y0, cu->cb_width, cu->cb_height);
806 mv_merge_mode(lc, merge_idx, cand_list);
807 *mv = cand_list[merge_idx];
808 //ciip flag in not inhritable
809 mv->ciip_flag = ciip_flag;
810}
811
812//8.5.4.2 Derivation process for luma motion vectors for geometric partitioning merge mode
813void ff_vvc_luma_mv_merge_gpm(VVCLocalContext *lc, const int merge_gpm_idx[2], MvField *mv)
814{
815 const CodingUnit *cu = lc->cu;
816 MvField cand_list[MRG_MAX_NUM_CANDS];
817
818 const int idx[] = { merge_gpm_idx[0], merge_gpm_idx[1] + (merge_gpm_idx[1] >= merge_gpm_idx[0]) };
819
820 ff_vvc_set_neighbour_available(lc, cu->x0, cu->y0, cu->cb_width, cu->cb_height);
821 mv_merge_mode(lc, FFMAX(idx[0], idx[1]), cand_list);
822 memset(mv, 0, 2 * sizeof(*mv));
823 for (int i = 0; i < 2; i++) {
824 int lx = idx[i] & 1;
825 int mask = lx + PF_L0;
826 MvField *cand = cand_list + idx[i];
827 if (!(cand->pred_flag & mask)) {
828 lx = !lx;
829 mask = lx + PF_L0;
830 }
831 mv[i].pred_flag = mask;
832 mv[i].ref_idx[lx] = cand->ref_idx[lx];
833 mv[i].mv[lx] = cand->mv[lx];
834 }
835
836}
837
838//8.5.5.5 Derivation process for luma affine control point motion vectors from a neighbouring block
839static void affine_cps_from_nb(const VVCLocalContext *lc,
840 const int x_nb, int y_nb, const int nbw, const int nbh, const int lx,
841 Mv *cps, int num_cps)
842{
843 const VVCFrameContext *fc = lc->fc;
844 const CodingUnit *cu = lc->cu;
845 const int x0 = cu->x0;
846 const int y0 = cu->y0;
847 const int cb_width = cu->cb_width;
848 const int cb_height = cu->cb_height;
849 const MvField* tab_mvf = fc->tab.mvf;
850 const int min_cb_log2_size = fc->ps.sps->min_cb_log2_size_y;
851 const int min_cb_width = fc->ps.pps->min_cb_width;
852
853 const int log2_nbw = ff_log2(nbw);
854 const int log2_nbh = ff_log2(nbh);
855 const int is_ctb_boundary = !((y_nb + nbh) % fc->ps.sps->ctb_size_y) && (y_nb + nbh == y0);
856 const Mv *l, *r;
857 int mv_scale_hor, mv_scale_ver, d_hor_x, d_ver_x, d_hor_y, d_ver_y, motion_model_idc_nb;
858 if (is_ctb_boundary) {
859 const int min_pu_width = fc->ps.pps->min_pu_width;
860 l = &TAB_MVF(x_nb, y_nb + nbh - 1).mv[lx];
861 r = &TAB_MVF(x_nb + nbw - 1, y_nb + nbh - 1).mv[lx];
862 } else {
863 const int x = x_nb >> min_cb_log2_size;
864 const int y = y_nb >> min_cb_log2_size;
865 motion_model_idc_nb = SAMPLE_CTB(fc->tab.mmi, x, y);
866
867 l = &TAB_CP_MV(lx, x_nb, y_nb);
868 r = &TAB_CP_MV(lx, x_nb + nbw - 1, y_nb) + 1;
869 }
870 mv_scale_hor = l->x << 7;
871 mv_scale_ver = l->y << 7;
872 d_hor_x = (r->x - l->x) << (7 - log2_nbw);
873 d_ver_x = (r->y - l->y) << (7 - log2_nbw);
874 if (!is_ctb_boundary && motion_model_idc_nb == MOTION_6_PARAMS_AFFINE) {
875 const Mv* lb = &TAB_CP_MV(lx, x_nb, y_nb + nbh - 1) + 2;
876 d_hor_y = (lb->x - l->x) << (7 - log2_nbh);
877 d_ver_y = (lb->y - l->y) << (7 - log2_nbh);
878 } else {
879 d_hor_y = -d_ver_x;
880 d_ver_y = d_hor_x;
881 }
882
883 if (is_ctb_boundary) {
884 y_nb = y0;
885 }
886 cps[0].x = mv_scale_hor + d_hor_x * (x0 - x_nb) + d_hor_y * (y0 - y_nb);
887 cps[0].y = mv_scale_ver + d_ver_x * (x0 - x_nb) + d_ver_y * (y0 - y_nb);
888 cps[1].x = mv_scale_hor + d_hor_x * (x0 + cb_width - x_nb) + d_hor_y * (y0 - y_nb);
889 cps[1].y = mv_scale_ver + d_ver_x * (x0 + cb_width - x_nb) + d_ver_y * (y0 - y_nb);
890 if (num_cps == 3) {
891 cps[2].x = mv_scale_hor + d_hor_x * (x0 - x_nb) + d_hor_y * (y0 + cb_height - y_nb);
892 cps[2].y = mv_scale_ver + d_ver_x * (x0 - x_nb) + d_ver_y * (y0 + cb_height - y_nb);
893 }
894 for (int i = 0; i < num_cps; i++) {
895 ff_vvc_round_mv(cps + i, 0, 7);
896 ff_vvc_clip_mv(cps + i);
897 }
898}
899
900//derive affine neighbour's postion, width and height,
901static int affine_neighbour_cb(const VVCFrameContext *fc, const int x_nb, const int y_nb, int *x_cb, int *y_cb, int *cbw, int *cbh)
902{
903 const int log2_min_cb_size = fc->ps.sps->min_cb_log2_size_y;
904 const int min_cb_width = fc->ps.pps->min_cb_width;
905 const int x = x_nb >> log2_min_cb_size;
906 const int y = y_nb >> log2_min_cb_size;
907 const int motion_model_idc = SAMPLE_CTB(fc->tab.mmi, x, y);
908 if (motion_model_idc) {
909 *x_cb = SAMPLE_CTB(fc->tab.cb_pos_x[0], x, y);
910 *y_cb = SAMPLE_CTB(fc->tab.cb_pos_y[0], x, y);
911 *cbw = SAMPLE_CTB(fc->tab.cb_width[0], x, y);
912 *cbh = SAMPLE_CTB(fc->tab.cb_height[0], x, y);
913 }
914 return motion_model_idc;
915}
916
917//part of 8.5.5.2 Derivation process for motion vectors and reference indices in subblock merge mode
918static int affine_merge_candidate(const VVCLocalContext *lc, const int x_cand, const int y_cand, MotionInfo* mi)
919{
920 const VVCFrameContext *fc = lc->fc;
921 int x, y, w, h, motion_model_idc;
922
923 motion_model_idc = affine_neighbour_cb(fc, x_cand, y_cand, &x, &y, &w, &h);
924 if (motion_model_idc) {
925 const int min_pu_width = fc->ps.pps->min_pu_width;
926 const MvField* tab_mvf = fc->tab.mvf;
927 const MvField *mvf = &TAB_MVF(x, y);
928
929 mi->bcw_idx = mvf->bcw_idx;
930 mi->pred_flag = mvf->pred_flag;
931 for (int i = 0; i < 2; i++) {
932 PredFlag mask = i + 1;
933 if (mi->pred_flag & mask) {
934 affine_cps_from_nb(lc, x, y, w, h, i, &mi->mv[i][0], motion_model_idc + 1);
935 }
936 mi->ref_idx[i] = mvf->ref_idx[i];
937 }
938 mi->motion_model_idc = motion_model_idc;
939 }
940 return motion_model_idc;
941}
942
943static int affine_merge_from_nbs(NeighbourContext *ctx, const NeighbourIdx *nbs, const int num_nbs, MotionInfo* cand)
944{
945 const VVCLocalContext *lc = ctx->lc;
946 const int is_mvp = 0;
947 for (int i = 0; i < num_nbs; i++) {
948 Neighbour *n = &ctx->neighbours[nbs[i]];
949 if (check_available(n, lc, is_mvp) && affine_merge_candidate(lc, n->x, n->y, cand))
950 return 1;
951 }
952 return 0;
953}
954#define AFFINE_MERGE_FROM_NBS(nbs) affine_merge_from_nbs(&nctx, nbs, FF_ARRAY_ELEMS(nbs), mi)
955
956
957static const MvField* derive_corner_mvf(NeighbourContext *ctx, const NeighbourIdx *neighbour, const int num_neighbour)
958{
959 const VVCFrameContext *fc = ctx->lc->fc;
960 const MvField *tab_mvf = fc->tab.mvf;
961 const int min_pu_width = fc->ps.pps->min_pu_width;
962 for (int i = 0; i < num_neighbour; i++) {
963 Neighbour *n = &ctx->neighbours[neighbour[i]];
964 if (check_available(n, ctx->lc, 0)) {
965 return &TAB_MVF(n->x, n->y);
966 }
967 }
968 return NULL;
969}
970
971#define DERIVE_CORNER_MV(nbs) derive_corner_mvf(nctx, nbs, FF_ARRAY_ELEMS(nbs))
972
973// check if the mv's and refidx are the same between A and B
974static av_always_inline int compare_pf_ref_idx(const MvField *A, const struct MvField *B, const struct MvField *C, const int lx)
975{
976
977 const PredFlag mask = (lx + 1) & A->pred_flag;
978 if (!(B->pred_flag & mask))
979 return 0;
980 if (A->ref_idx[lx] != B->ref_idx[lx])
981 return 0;
982 if (C) {
983 if (!(C->pred_flag & mask))
984 return 0;
985 if (A->ref_idx[lx] != C->ref_idx[lx])
986 return 0;
987 }
988 return 1;
989}
990
991static av_always_inline void sb_clip_location(const VVCFrameContext *fc,
992 const int x_ctb, const int y_ctb, const Mv* temp_mv, int *x, int *y)
993{
994 const VVCPPS *pps = fc->ps.pps;
995 const int ctb_log2_size = fc->ps.sps->ctb_log2_size_y;
996 *y = av_clip(*y + temp_mv->y, y_ctb, FFMIN(pps->height - 1, y_ctb + (1 << ctb_log2_size) - 1)) & ~7;
997 *x = av_clip(*x + temp_mv->x, x_ctb, FFMIN(pps->width - 1, x_ctb + (1 << ctb_log2_size) + 3)) & ~7;
998}
999
1000static void sb_temproal_luma_motion(const VVCLocalContext *lc,
1001 const int x_ctb, const int y_ctb, const Mv *temp_mv,
1002 int x, int y, uint8_t *pred_flag, Mv *mv)
1003{
1004 MvField temp_col;
1005 Mv* mvLXCol;
1006 const int refIdxLx = 0;
1007 const VVCFrameContext *fc = lc->fc;
1008 const VVCSH *sh = &lc->sc->sh;
1009 const int min_pu_width = fc->ps.pps->min_pu_width;
1010 VVCFrame *ref = fc->ref->collocated_ref;
1011 MvField *tab_mvf = ref->tab_dmvr_mvf;
1012 int colPic = ref->poc;
1013 int X = 0;
1014
1015 sb_clip_location(fc, x_ctb, y_ctb, temp_mv, &x, &y);
1016
1017 temp_col = TAB_MVF(x, y);
1018 mvLXCol = mv + 0;
1019 *pred_flag = DERIVE_TEMPORAL_COLOCATED_MVS(1);
1020 if (IS_B(sh->r)) {
1021 X = 1;
1022 mvLXCol = mv + 1;
1023 *pred_flag |= (DERIVE_TEMPORAL_COLOCATED_MVS(1)) << 1;
1024 }
1025}
1026
1027//8.5.5.4 Derivation process for subblock-based temporal merging base motion data
1028static int sb_temporal_luma_motion_data(const VVCLocalContext *lc, const MvField *a1,
1029 const int x_ctb, const int y_ctb, MvField *ctr_mvf, Mv *temp_mv)
1030{
1031 const VVCFrameContext *fc = lc->fc;
1032 const RefPicList *rpl = lc->sc->rpl;
1033 const CodingUnit *cu = lc->cu;
1034 const int x = cu->x0 + cu->cb_width / 2;
1035 const int y = cu->y0 + cu->cb_height / 2;
1036 const VVCFrame *ref = fc->ref->collocated_ref;
1037
1038 int colPic;
1039
1040 memset(temp_mv, 0, sizeof(*temp_mv));
1041
1042 if (!ref) {
1043 memset(ctr_mvf, 0, sizeof(*ctr_mvf));
1044 return 0;
1045 }
1046
1047 colPic = ref->poc;
1048
1049 AV_ZERO64(temp_mv);
1050 if (a1) {
1051 if ((a1->pred_flag & PF_L0) && colPic == rpl[0].list[a1->ref_idx[0]])
1052 *temp_mv = a1->mv[0];
1053 else if ((a1->pred_flag & PF_L1) && colPic == rpl[1].list[a1->ref_idx[1]])
1054 *temp_mv = a1->mv[1];
1055 ff_vvc_round_mv(temp_mv, 0, 4);
1056 }
1057 sb_temproal_luma_motion(lc, x_ctb, y_ctb, temp_mv, x, y, &ctr_mvf->pred_flag , ctr_mvf->mv);
1058
1059 return ctr_mvf->pred_flag;
1060}
1061
1062
1063//8.5.5.3 Derivation process for subblock-based temporal merging candidates
1064static int sb_temporal_merge_candidate(const VVCLocalContext* lc, NeighbourContext *nctx, PredictionUnit *pu)
1065{
1066 const VVCFrameContext *fc = lc->fc;
1067 const CodingUnit *cu = lc->cu;
1068 const VVCSPS *sps = fc->ps.sps;
1069 const VVCPH *ph = &fc->ps.ph;
1070 MotionInfo *mi = &pu->mi;
1071 const int ctb_log2_size = sps->ctb_log2_size_y;
1072 const int x0 = cu->x0;
1073 const int y0 = cu->y0;
1074 const NeighbourIdx n = A1;
1075 const MvField *a1;
1076 MvField ctr_mvf;
1077 Mv temp_mv;
1078 const int x_ctb = (x0 >> ctb_log2_size) << ctb_log2_size;
1079 const int y_ctb = (y0 >> ctb_log2_size) << ctb_log2_size;
1080
1081
1082 if (!ph->r->ph_temporal_mvp_enabled_flag ||
1083 !sps->r->sps_sbtmvp_enabled_flag ||
1084 (cu->cb_width < 8 && cu->cb_height < 8))
1085 return 0;
1086
1087 mi->num_sb_x = cu->cb_width >> 3;
1088 mi->num_sb_y = cu->cb_height >> 3;
1089
1090 a1 = derive_corner_mvf(nctx, &n, 1);
1091 if (sb_temporal_luma_motion_data(lc, a1, x_ctb, y_ctb, &ctr_mvf, &temp_mv)) {
1092 const int sbw = cu->cb_width / mi->num_sb_x;
1093 const int sbh = cu->cb_height / mi->num_sb_y;
1094 MvField mvf = {0};
1095 for (int sby = 0; sby < mi->num_sb_y; sby++) {
1096 for (int sbx = 0; sbx < mi->num_sb_x; sbx++) {
1097 int x = x0 + sbx * sbw;
1098 int y = y0 + sby * sbh;
1099 sb_temproal_luma_motion(lc, x_ctb, y_ctb, &temp_mv, x + sbw / 2, y + sbh / 2, &mvf.pred_flag, mvf.mv);
1100 if (!mvf.pred_flag) {
1101 mvf.pred_flag = ctr_mvf.pred_flag;
1102 memcpy(mvf.mv, ctr_mvf.mv, sizeof(mvf.mv));
1103 }
1104 ff_vvc_set_mvf(lc, x, y, sbw, sbh, &mvf);
1105 }
1106 }
1107 return 1;
1108 }
1109 return 0;
1110}
1111
1112static int affine_merge_const1(const MvField *c0, const MvField *c1, const MvField *c2, MotionInfo *mi)
1113{
1114 if (c0 && c1 && c2) {
1115 mi->pred_flag = 0;
1116 for (int i = 0; i < 2; i++) {
1117 PredFlag mask = i + 1;
1118 if (compare_pf_ref_idx(c0, c1, c2, i)) {
1119 mi->pred_flag |= mask;
1120 mi->ref_idx[i] = c0->ref_idx[i];
1121 mi->mv[i][0] = c0->mv[i];
1122 mi->mv[i][1] = c1->mv[i];
1123 mi->mv[i][2] = c2->mv[i];
1124 }
1125 }
1126 if (mi->pred_flag) {
1127 if (mi->pred_flag == PF_BI)
1128 mi->bcw_idx = c0->bcw_idx;
1129 mi->motion_model_idc = MOTION_6_PARAMS_AFFINE;
1130 return 1;
1131 }
1132 }
1133 return 0;
1134}
1135
1136static int affine_merge_const2(const MvField *c0, const MvField *c1, const MvField *c3, MotionInfo *mi)
1137{
1138 if (c0 && c1 && c3) {
1139 mi->pred_flag = 0;
1140 for (int i = 0; i < 2; i++) {
1141 PredFlag mask = i + 1;
1142 if (compare_pf_ref_idx(c0, c1, c3, i)) {
1143 mi->pred_flag |= mask;
1144 mi->ref_idx[i] = c0->ref_idx[i];
1145 mi->mv[i][0] = c0->mv[i];
1146 mi->mv[i][1] = c1->mv[i];
1147 mi->mv[i][2].x = c3->mv[i].x + c0->mv[i].x - c1->mv[i].x;
1148 mi->mv[i][2].y = c3->mv[i].y + c0->mv[i].y - c1->mv[i].y;
1149 ff_vvc_clip_mv(&mi->mv[i][2]);
1150 }
1151 }
1152 if (mi->pred_flag) {
1153 mi->bcw_idx = mi->pred_flag == PF_BI ? c0->bcw_idx : 0;
1154 mi->motion_model_idc = MOTION_6_PARAMS_AFFINE;
1155 return 1;
1156 }
1157 }
1158 return 0;
1159}
1160
1161static int affine_merge_const3(const MvField *c0, const MvField *c2, const MvField *c3, MotionInfo *mi)
1162{
1163 if (c0 && c2 && c3) {
1164 mi->pred_flag = 0;
1165 for (int i = 0; i < 2; i++) {
1166 PredFlag mask = i + 1;
1167 if (compare_pf_ref_idx(c0, c2, c3, i)) {
1168 mi->pred_flag |= mask;
1169 mi->ref_idx[i] = c0->ref_idx[i];
1170 mi->mv[i][0] = c0->mv[i];
1171 mi->mv[i][1].x = c3->mv[i].x + c0->mv[i].x - c2->mv[i].x;
1172 mi->mv[i][1].y = c3->mv[i].y + c0->mv[i].y - c2->mv[i].y;
1173 ff_vvc_clip_mv(&mi->mv[i][1]);
1174 mi->mv[i][2] = c2->mv[i];
1175 }
1176 }
1177 if (mi->pred_flag) {
1178 mi->bcw_idx = mi->pred_flag == PF_BI ? c0->bcw_idx : 0;
1179 mi->motion_model_idc = MOTION_6_PARAMS_AFFINE;
1180 return 1;
1181 }
1182 }
1183 return 0;
1184}
1185
1186static int affine_merge_const4(const MvField *c1, const MvField *c2, const MvField *c3, MotionInfo *mi)
1187{
1188 if (c1 && c2 && c3) {
1189 mi->pred_flag = 0;
1190 for (int i = 0; i < 2; i++) {
1191 PredFlag mask = i + 1;
1192 if (compare_pf_ref_idx(c1, c2, c3, i)) {
1193 mi->pred_flag |= mask;
1194 mi->ref_idx[i] = c1->ref_idx[i];
1195 mi->mv[i][0].x = c1->mv[i].x + c2->mv[i].x - c3->mv[i].x;
1196 mi->mv[i][0].y = c1->mv[i].y + c2->mv[i].y - c3->mv[i].y;
1197 ff_vvc_clip_mv(&mi->mv[i][0]);
1198 mi->mv[i][1] = c1->mv[i];
1199 mi->mv[i][2] = c2->mv[i];
1200 }
1201 }
1202 if (mi->pred_flag) {
1203 mi->bcw_idx = mi->pred_flag == PF_BI ? c1->bcw_idx : 0;
1204 mi->motion_model_idc = MOTION_6_PARAMS_AFFINE;
1205 return 1;
1206 }
1207 }
1208 return 0;
1209}
1210
1211static int affine_merge_const5(const MvField *c0, const MvField *c1, MotionInfo *mi)
1212{
1213 if (c0 && c1) {
1214 mi->pred_flag = 0;
1215 for (int i = 0; i < 2; i++) {
1216 PredFlag mask = i + 1;
1217 if (compare_pf_ref_idx(c0, c1, NULL, i)) {
1218 mi->pred_flag |= mask;
1219 mi->ref_idx[i] = c0->ref_idx[i];
1220 mi->mv[i][0] = c0->mv[i];
1221 mi->mv[i][1] = c1->mv[i];
1222 }
1223 }
1224 if (mi->pred_flag) {
1225 if (mi->pred_flag == PF_BI)
1226 mi->bcw_idx = c0->bcw_idx;
1227 mi->motion_model_idc = MOTION_4_PARAMS_AFFINE;
1228 return 1;
1229 }
1230 }
1231 return 0;
1232}
1233
1234static int affine_merge_const6(const MvField* c0, const MvField* c2, const int cb_width, const int cb_height, MotionInfo *mi)
1235{
1236 if (c0 && c2) {
1237 const int shift = 7 + av_log2(cb_width) - av_log2(cb_height);
1238 mi->pred_flag = 0;
1239 for (int i = 0; i < 2; i++) {
1240 PredFlag mask = i + 1;
1241 if (compare_pf_ref_idx(c0, c2, NULL, i)) {
1242 mi->pred_flag |= mask;
1243 mi->ref_idx[i] = c0->ref_idx[i];
1244 mi->mv[i][0] = c0->mv[i];
1245 mi->mv[i][1].x = (c0->mv[i].x << 7) + ((c2->mv[i].y - c0->mv[i].y) << shift);
1246 mi->mv[i][1].y = (c0->mv[i].y << 7) - ((c2->mv[i].x - c0->mv[i].x) << shift);
1247 ff_vvc_round_mv(&mi->mv[i][1], 0, 7);
1248 ff_vvc_clip_mv(&mi->mv[i][1]);
1249 }
1250 }
1251 if (mi->pred_flag) {
1252 if (mi->pred_flag == PF_BI)
1253 mi->bcw_idx = c0->bcw_idx;
1254 mi->motion_model_idc = MOTION_4_PARAMS_AFFINE;
1255 return 1;
1256 }
1257 }
1258 return 0;
1259}
1260
1261static void affine_merge_zero_motion(const VVCLocalContext *lc, MotionInfo *mi)
1262{
1263 const CodingUnit *cu = lc->cu;
1264
1265 memset(mi, 0, sizeof(*mi));
1266 mi->pred_flag = PF_L0 + (IS_B(lc->sc->sh.r) << 1);
1267 mi->motion_model_idc = MOTION_4_PARAMS_AFFINE;
1268 mi->num_sb_x = cu->cb_width >> MIN_PU_LOG2;
1269 mi->num_sb_y = cu->cb_height >> MIN_PU_LOG2;
1270}
1271
1272//8.5.5.6 Derivation process for constructed affine control point motion vector merging candidates
1273static int affine_merge_const_candidates(const VVCLocalContext *lc, MotionInfo *mi,
1274 NeighbourContext *nctx, const int merge_subblock_idx, int num_cands)
1275{
1276 const VVCFrameContext *fc = lc->fc;
1277 const CodingUnit *cu = lc->cu;
1278 const NeighbourIdx tl[] = { B2, B3, A2 };
1279 const NeighbourIdx tr[] = { B1, B0};
1280 const NeighbourIdx bl[] = { A1, A0};
1281 const MvField *c0, *c1, *c2;
1282
1283 c0 = DERIVE_CORNER_MV(tl);
1284 c1 = DERIVE_CORNER_MV(tr);
1285 c2 = DERIVE_CORNER_MV(bl);
1286
1287 if (fc->ps.sps->r->sps_6param_affine_enabled_flag) {
1288 MvField corner3, *c3 = NULL;
1289 //Const1
1290 if (affine_merge_const1(c0, c1, c2, mi)) {
1291 if (merge_subblock_idx == num_cands)
1292 return 1;
1293 num_cands++;
1294 }
1295
1296 memset(&corner3, 0, sizeof(corner3));
1297 if (fc->ps.ph.r->ph_temporal_mvp_enabled_flag){
1298 const int available_l0 = temporal_luma_motion_vector(lc, 0, corner3.mv + 0, 0, 0, 0);
1299 const int available_l1 = (lc->sc->sh.r->sh_slice_type == VVC_SLICE_TYPE_B) ?
1300 temporal_luma_motion_vector(lc, 0, corner3.mv + 1, 1, 0, 0) : 0;
1301
1302 corner3.pred_flag = available_l0 + (available_l1 << 1);
1303 if (corner3.pred_flag)
1304 c3 = &corner3;
1305 }
1306
1307 //Const2
1308 if (affine_merge_const2(c0, c1, c3, mi)) {
1309 if (merge_subblock_idx == num_cands)
1310 return 1;
1311 num_cands++;
1312 }
1313
1314 //Const3
1315 if (affine_merge_const3(c0, c2, c3, mi)) {
1316 if (merge_subblock_idx == num_cands)
1317 return 1;
1318 num_cands++;
1319 }
1320
1321 //Const4
1322 if (affine_merge_const4(c1, c2, c3, mi)) {
1323 if (merge_subblock_idx == num_cands)
1324 return 1;
1325 num_cands++;
1326 }
1327 }
1328
1329 //Const5
1330 if (affine_merge_const5(c0, c1, mi)) {
1331 if (merge_subblock_idx == num_cands)
1332 return 1;
1333 num_cands++;
1334 }
1335
1336 if (affine_merge_const6(c0, c2, cu->cb_width, cu->cb_height, mi)) {
1337 if (merge_subblock_idx == num_cands)
1338 return 1;
1339 }
1340 return 0;
1341}
1342
1343//8.5.5.2 Derivation process for motion vectors and reference indices in subblock merge mode
1344//return 1 if candidate is SbCol
1345static int sb_mv_merge_mode(const VVCLocalContext *lc, const int merge_subblock_idx, PredictionUnit *pu)
1346{
1347 const VVCSPS *sps = lc->fc->ps.sps;
1348 const CodingUnit *cu = lc->cu;
1349 MotionInfo *mi = &pu->mi;
1350 int num_cands = 0;
1351 NeighbourContext nctx;
1352
1353 init_neighbour_context(&nctx, lc);
1354
1355 //SbCol
1356 if (sb_temporal_merge_candidate(lc, &nctx, pu)) {
1357 if (merge_subblock_idx == num_cands)
1358 return 1;
1359 num_cands++;
1360 }
1361
1362 pu->inter_affine_flag = 1;
1363 mi->num_sb_x = cu->cb_width >> MIN_PU_LOG2;
1364 mi->num_sb_y = cu->cb_height >> MIN_PU_LOG2;
1365
1366 if (sps->r->sps_affine_enabled_flag) {
1367 const NeighbourIdx ak[] = { A0, A1 };
1368 const NeighbourIdx bk[] = { B0, B1, B2 };
1369 //A
1370 if (AFFINE_MERGE_FROM_NBS(ak)) {
1371 if (merge_subblock_idx == num_cands)
1372 return 0;
1373 num_cands++;
1374 }
1375
1376 //B
1377 if (AFFINE_MERGE_FROM_NBS(bk)) {
1378 if (merge_subblock_idx == num_cands)
1379 return 0;
1380 num_cands++;
1381 }
1382
1383 //Const1 to Const6
1384 if (affine_merge_const_candidates(lc, mi, &nctx, merge_subblock_idx, num_cands))
1385 return 0;
1386 }
1387 //Zero
1388 affine_merge_zero_motion(lc, mi);
1389 return 0;
1390}
1391
1392void ff_vvc_sb_mv_merge_mode(VVCLocalContext *lc, const int merge_subblock_idx, PredictionUnit *pu)
1393{
1394 const CodingUnit *cu = lc->cu;
1395 ff_vvc_set_neighbour_available(lc, cu->x0, cu->y0, cu->cb_width, cu->cb_height);
1396 if (!sb_mv_merge_mode(lc, merge_subblock_idx, pu)) {
1397 ff_vvc_store_sb_mvs(lc, pu);
1398 }
1399}
1400
1401static int mvp_candidate(const VVCLocalContext *lc, const int x_cand, const int y_cand,
1402 const int lx, const int8_t *ref_idx, Mv *mv)
1403{
1404 const VVCFrameContext *fc = lc->fc;
1405 const RefPicList *rpl = lc->sc->rpl;
1406 const int min_pu_width = fc->ps.pps->min_pu_width;
1407 const MvField* tab_mvf = fc->tab.mvf;
1408 const MvField *mvf = &TAB_MVF(x_cand, y_cand);
1409 const PredFlag maskx = lx + 1;
1410 const int poc = rpl[lx].list[ref_idx[lx]];
1411 int available = 0;
1412
1413 if ((mvf->pred_flag & maskx) && rpl[lx].list[mvf->ref_idx[lx]] == poc) {
1414 available = 1;
1415 *mv = mvf->mv[lx];
1416 } else {
1417 const int ly = !lx;
1418 const PredFlag masky = ly + 1;
1419 if ((mvf->pred_flag & masky) && rpl[ly].list[mvf->ref_idx[ly]] == poc) {
1420 available = 1;
1421 *mv = mvf->mv[ly];
1422 }
1423 }
1424
1425 return available;
1426}
1427
1428static int affine_mvp_candidate(const VVCLocalContext *lc,
1429 const int x_cand, const int y_cand, const int lx, const int8_t *ref_idx,
1430 Mv *cps, const int num_cp)
1431{
1432 const VVCFrameContext *fc = lc->fc;
1433 int x_nb, y_nb, nbw, nbh, motion_model_idc, available = 0;
1434
1435 motion_model_idc = affine_neighbour_cb(fc, x_cand, y_cand, &x_nb, &y_nb, &nbw, &nbh);
1436 if (motion_model_idc) {
1437 const int min_pu_width = fc->ps.pps->min_pu_width;
1438 const MvField* tab_mvf = fc->tab.mvf;
1439 const MvField *mvf = &TAB_MVF(x_nb, y_nb);
1440 RefPicList* rpl = lc->sc->rpl;
1441 const PredFlag maskx = lx + 1;
1442 const int poc = rpl[lx].list[ref_idx[lx]];
1443
1444 if ((mvf->pred_flag & maskx) && rpl[lx].list[mvf->ref_idx[lx]] == poc) {
1445 available = 1;
1446 affine_cps_from_nb(lc, x_nb, y_nb, nbw, nbh, lx, cps, num_cp);
1447 } else {
1448 const int ly = !lx;
1449 const PredFlag masky = ly + 1;
1450 if ((mvf->pred_flag & masky) && rpl[ly].list[mvf->ref_idx[ly]] == poc) {
1451 available = 1;
1452 affine_cps_from_nb(lc, x_nb, y_nb, nbw, nbh, ly, cps, num_cp);
1453 }
1454 }
1455
1456 }
1457 return available;
1458}
1459
1460static int mvp_from_nbs(NeighbourContext *ctx,
1461 const NeighbourIdx *nbs, const int num_nbs, const int lx, const int8_t *ref_idx, const int amvr_shift,
1462 Mv *cps, const int num_cps)
1463{
1464 const VVCLocalContext *lc = ctx->lc;
1465 const int is_mvp = 1;
1466 int available = 0;
1467
1468 for (int i = 0; i < num_nbs; i++) {
1469 Neighbour *n = &ctx->neighbours[nbs[i]];
1470 if (check_available(n, lc, is_mvp)) {
1471 if (num_cps > 1)
1472 available = affine_mvp_candidate(lc, n->x, n->y, lx, ref_idx, cps, num_cps);
1473 else
1474 available = mvp_candidate(lc, n->x, n->y, lx, ref_idx, cps);
1475 if (available) {
1476 for (int c = 0; c < num_cps; c++)
1477 ff_vvc_round_mv(cps + c, amvr_shift, amvr_shift);
1478 return 1;
1479 }
1480 }
1481 }
1482 return 0;
1483}
1484
1485//get mvp from neighbours
1486#define AFFINE_MVP_FROM_NBS(nbs) \
1487 mvp_from_nbs(&nctx, nbs, FF_ARRAY_ELEMS(nbs), lx, ref_idx, amvr_shift, cps, num_cp) \
1488
1489#define MVP_FROM_NBS(nbs) \
1490 mvp_from_nbs(&nctx, nbs, FF_ARRAY_ELEMS(nbs), lx, ref_idx, amvr_shift, mv, 1) \
1491
1492static int mvp_spatial_candidates(const VVCLocalContext *lc,
1493 const int mvp_lx_flag, const int lx, const int8_t* ref_idx, const int amvr_shift,
1494 Mv* mv, int *nb_merge_cand)
1495{
1496 const NeighbourIdx ak[] = { A0, A1 };
1497 const NeighbourIdx bk[] = { B0, B1, B2 };
1498 NeighbourContext nctx;
1499 int available_a, num_cands = 0;
1500 Mv mv_a;
1501
1502 init_neighbour_context(&nctx, lc);
1503
1504 available_a = MVP_FROM_NBS(ak);
1505 if (available_a) {
1506 if (mvp_lx_flag == num_cands)
1507 return 1;
1508 num_cands++;
1509 mv_a = *mv;
1510 }
1511 if (MVP_FROM_NBS(bk)) {
1512 if (!available_a || !IS_SAME_MV(&mv_a, mv)) {
1513 if (mvp_lx_flag == num_cands)
1514 return 1;
1515 num_cands++;
1516 }
1517 }
1518 *nb_merge_cand = num_cands;
1519 return 0;
1520}
1521
1522static int mvp_temporal_candidates(const VVCLocalContext* lc,
1523 const int mvp_lx_flag, const int lx, const int8_t *ref_idx, const int amvr_shift,
1524 Mv* mv, int *num_cands)
1525{
1526 if (temporal_luma_motion_vector(lc, ref_idx[lx], mv, lx, 1, 0)) {
1527 if (mvp_lx_flag == *num_cands) {
1528 ff_vvc_round_mv(mv, amvr_shift, amvr_shift);
1529 return 1;
1530 }
1531 (*num_cands)++;
1532 }
1533 return 0;
1534
1535}
1536
1537static int mvp_history_candidates(const VVCLocalContext *lc,
1538 const int mvp_lx_flag, const int lx, const int8_t ref_idx, const int amvr_shift,
1539 Mv *mv, int num_cands)
1540{
1541 const EntryPoint* ep = lc->ep;
1542 const RefPicList* rpl = lc->sc->rpl;
1543 const int poc = rpl[lx].list[ref_idx];
1544
1545 if (ep->num_hmvp == 0)
1546 return 0;
1547 for (int i = 1; i <= FFMIN(4, ep->num_hmvp); i++) {
1548 const MvField* h = &ep->hmvp[i - 1];
1549 for (int j = 0; j < 2; j++) {
1550 const int ly = (j ? !lx : lx);
1551 PredFlag mask = PF_L0 + ly;
1552 if ((h->pred_flag & mask) && poc == rpl[ly].list[h->ref_idx[ly]]) {
1553 if (mvp_lx_flag == num_cands) {
1554 *mv = h->mv[ly];
1555 ff_vvc_round_mv(mv, amvr_shift, amvr_shift);
1556 return 1;
1557 }
1558 num_cands++;
1559 }
1560 }
1561 }
1562 return 0;
1563}
1564
1565//8.5.2.8 Derivation process for luma motion vector prediction
1566static void mvp(const VVCLocalContext *lc, const int mvp_lx_flag, const int lx,
1567 const int8_t *ref_idx, const int amvr_shift, Mv *mv)
1568{
1569 int num_cands;
1570
1571 if (mvp_spatial_candidates(lc, mvp_lx_flag, lx, ref_idx, amvr_shift, mv, &num_cands))
1572 return;
1573
1574 if (mvp_temporal_candidates(lc, mvp_lx_flag, lx, ref_idx, amvr_shift, mv, &num_cands))
1575 return;
1576
1577 if (mvp_history_candidates(lc, mvp_lx_flag, lx, ref_idx[lx], amvr_shift, mv, num_cands))
1578 return;
1579
1580 memset(mv, 0, sizeof(*mv));
1581}
1582
1583void ff_vvc_mvp(VVCLocalContext *lc, const int *mvp_lx_flag, const int amvr_shift, MotionInfo *mi)
1584{
1585 const CodingUnit *cu = lc->cu;
1586 mi->num_sb_x = 1;
1587 mi->num_sb_y = 1;
1588
1589 ff_vvc_set_neighbour_available(lc, cu->x0, cu->y0, cu->cb_width, cu->cb_height);
1590 if (mi->pred_flag != PF_L1)
1591 mvp(lc, mvp_lx_flag[L0], L0, mi->ref_idx, amvr_shift, &mi->mv[L0][0]);
1592 if (mi->pred_flag != PF_L0)
1593 mvp(lc, mvp_lx_flag[L1], L1, mi->ref_idx, amvr_shift, &mi->mv[L1][0]);
1594}
1595
1596static int affine_mvp_constructed_cp(NeighbourContext *ctx,
1597 const NeighbourIdx *neighbour, const int num_neighbour,
1598 const int lx, const int8_t ref_idx, const int amvr_shift, Mv *cp)
1599{
1600 const VVCLocalContext *lc = ctx->lc;
1601 const VVCFrameContext *fc = lc->fc;
1602 const MvField *tab_mvf = fc->tab.mvf;
1603 const int min_pu_width = fc->ps.pps->min_pu_width;
1604 const RefPicList* rpl = lc->sc->rpl;
1605 const int is_mvp = 1;
1606 int available = 0;
1607
1608 for (int i = 0; i < num_neighbour; i++) {
1609 Neighbour *n = &ctx->neighbours[neighbour[i]];
1610 if (check_available(n, ctx->lc, is_mvp)) {
1611 const PredFlag maskx = lx + 1;
1612 const MvField* mvf = &TAB_MVF(n->x, n->y);
1613 const int poc = rpl[lx].list[ref_idx];
1614 if ((mvf->pred_flag & maskx) && rpl[lx].list[mvf->ref_idx[lx]] == poc) {
1615 available = 1;
1616 *cp = mvf->mv[lx];
1617 } else {
1618 const int ly = !lx;
1619 const PredFlag masky = ly + 1;
1620 if ((mvf->pred_flag & masky) && rpl[ly].list[mvf->ref_idx[ly]] == poc) {
1621 available = 1;
1622 *cp = mvf->mv[ly];
1623 }
1624 }
1625 if (available) {
1626 ff_vvc_round_mv(cp, amvr_shift, amvr_shift);
1627 return 1;
1628 }
1629 }
1630 }
1631 return 0;
1632}
1633
1634#define AFFINE_MVP_CONSTRUCTED_CP(cands, cp) \
1635 affine_mvp_constructed_cp(nctx, cands, FF_ARRAY_ELEMS(cands), lx, ref_idx, \
1636 amvr_shift, cp)
1637
1638//8.5.5.8 Derivation process for constructed affine control point motion vector prediction candidates
1639static int affine_mvp_const1(NeighbourContext* nctx,
1640 const int lx, const int8_t ref_idx, const int amvr_shift,
1641 Mv *cps, int *available)
1642{
1643 const NeighbourIdx tl[] = { B2, B3, A2 };
1644 const NeighbourIdx tr[] = { B1, B0 };
1645 const NeighbourIdx bl[] = { A1, A0 };
1646
1647 available[0] = AFFINE_MVP_CONSTRUCTED_CP(tl, cps + 0);
1648 available[1] = AFFINE_MVP_CONSTRUCTED_CP(tr, cps + 1);
1649 available[2] = AFFINE_MVP_CONSTRUCTED_CP(bl, cps + 2);
1650 return available[0] && available[1];
1651}
1652
1653//8.5.5.7 item 7
1654static void affine_mvp_const2(const int idx, Mv *cps, const int num_cp)
1655{
1656 const Mv mv = cps[idx];
1657 for (int j = 0; j < num_cp; j++)
1658 cps[j] = mv;
1659}
1660
1661//8.5.5.7 Derivation process for luma affine control point motion vector predictors
1662static void affine_mvp(const VVCLocalContext *lc,
1663 const int mvp_lx_flag, const int lx, const int8_t *ref_idx, const int amvr_shift,
1664 MotionModelIdc motion_model_idc, Mv *cps)
1665{
1666 const NeighbourIdx ak[] = { A0, A1 };
1667 const NeighbourIdx bk[] = { B0, B1, B2 };
1668 const int num_cp = motion_model_idc + 1;
1669 NeighbourContext nctx;
1670 int available[MAX_CONTROL_POINTS];
1671 int num_cands = 0;
1672
1673 init_neighbour_context(&nctx, lc);
1674 //Ak
1675 if (AFFINE_MVP_FROM_NBS(ak)) {
1676 if (mvp_lx_flag == num_cands)
1677 return;
1678 num_cands++;
1679 }
1680 //Bk
1681 if (AFFINE_MVP_FROM_NBS(bk)) {
1682 if (mvp_lx_flag == num_cands)
1683 return;
1684 num_cands++;
1685 }
1686
1687 //Const1
1688 if (affine_mvp_const1(&nctx, lx, ref_idx[lx], amvr_shift, cps, available)) {
1689 if (available[2] || motion_model_idc == MOTION_4_PARAMS_AFFINE) {
1690 if (mvp_lx_flag == num_cands)
1691 return;
1692 num_cands++;
1693 }
1694 }
1695
1696 //Const2
1697 for (int i = 2; i >= 0; i--) {
1698 if (available[i]) {
1699 if (mvp_lx_flag == num_cands) {
1700 affine_mvp_const2(i, cps, num_cp);
1701 return;
1702 }
1703 num_cands++;
1704 }
1705 }
1706 if (temporal_luma_motion_vector(lc, ref_idx[lx], cps, lx, 1, 0)) {
1707 if (mvp_lx_flag == num_cands) {
1708 ff_vvc_round_mv(cps, amvr_shift, amvr_shift);
1709 for (int i = 1; i < num_cp; i++)
1710 cps[i] = cps[0];
1711 return;
1712 }
1713 num_cands++;
1714 }
1715
1716 //Zero Mv
1717 memset(cps, 0, num_cp * sizeof(Mv));
1718}
1719
1720void ff_vvc_affine_mvp(VVCLocalContext *lc, const int *mvp_lx_flag, const int amvr_shift, MotionInfo *mi)
1721{
1722 const CodingUnit *cu = lc->cu;
1723
1724 mi->num_sb_x = cu->cb_width >> MIN_PU_LOG2;
1725 mi->num_sb_y = cu->cb_height >> MIN_PU_LOG2;
1726
1727 ff_vvc_set_neighbour_available(lc, cu->x0, cu->y0, cu->cb_width, cu->cb_height);
1728 if (mi->pred_flag != PF_L1)
1729 affine_mvp(lc, mvp_lx_flag[L0], L0, mi->ref_idx, amvr_shift, mi->motion_model_idc, &mi->mv[L0][0]);
1730 if (mi->pred_flag != PF_L0)
1731 affine_mvp(lc, mvp_lx_flag[L1], L1, mi->ref_idx, amvr_shift, mi->motion_model_idc, &mi->mv[L1][0]);
1732}
1733
1734//8.5.2.14 Rounding process for motion vectors
1735void ff_vvc_round_mv(Mv *mv, const int lshift, const int rshift)
1736{
1737 if (rshift) {
1738 const int offset = 1 << (rshift - 1);
1739 mv->x = ((mv->x + offset - (mv->x >= 0)) >> rshift) << lshift;
1740 mv->y = ((mv->y + offset - (mv->y >= 0)) >> rshift) << lshift;
1741 } else {
1742 mv->x = mv->x << lshift;
1743 mv->y = mv->y << lshift;
1744 }
1745}
1746
1747void ff_vvc_clip_mv(Mv *mv)
1748{
1749 mv->x = av_clip(mv->x, -(1 << 17), (1 << 17) - 1);
1750 mv->y = av_clip(mv->y, -(1 << 17), (1 << 17) - 1);
1751}
1752
1753//8.5.2.1 Derivation process for motion vector components and reference indices
1754static av_always_inline int is_greater_mer(const VVCFrameContext *fc, const int x0, const int y0, const int x0_br, const int y0_br)
1755{
1756 const uint8_t plevel = fc->ps.sps->log2_parallel_merge_level;
1757
1758 return x0_br >> plevel > x0 >> plevel &&
1759 y0_br >> plevel > y0 >> plevel;
1760}
1761
1762//8.5.2.16 Updating process for the history-based motion vector predictor candidate list
1763void ff_vvc_update_hmvp(VVCLocalContext *lc, const MotionInfo *mi)
1764{
1765 const VVCFrameContext *fc = lc->fc;
1766 const CodingUnit *cu = lc->cu;
1767 const int min_pu_width = fc->ps.pps->min_pu_width;
1768 const MvField* tab_mvf = fc->tab.mvf;
1769 EntryPoint* ep = lc->ep;
1770 const MvField *mvf;
1771 int i;
1772
1773 if (!is_greater_mer(fc, cu->x0, cu->y0, cu->x0 + cu->cb_width, cu->y0 + cu->cb_height))
1774 return;
1775 mvf = &TAB_MVF(cu->x0, cu->y0);
1776
1777 for (i = 0; i < ep->num_hmvp; i++) {
1778 if (compare_mv_ref_idx(mvf, ep->hmvp + i)) {
1779 ep->num_hmvp--;
1780 break;
1781 }
1782 }
1783 if (i == MAX_NUM_HMVP_CANDS) {
1784 ep->num_hmvp--;
1785 i = 0;
1786 }
1787
1788 memmove(ep->hmvp + i, ep->hmvp + i + 1, (ep->num_hmvp - i) * sizeof(MvField));
1789 ep->hmvp[ep->num_hmvp++] = *mvf;
1790}
1791
1792MvField* ff_vvc_get_mvf(const VVCFrameContext *fc, const int x0, const int y0)
1793{
1794 const int min_pu_width = fc->ps.pps->min_pu_width;
1795 MvField* tab_mvf = fc->tab.mvf;
1796
1797 return &TAB_MVF(x0, y0);
1798}
libavcodec/vvc/vvc_mvs.h+46−0
@@ -0,0 +1,46 @@
1/*
2 * VVC motion vector decoder
3 *
4 * Copyright (C) 2023 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
23#ifndef AVCODEC_VVC_VVC_MVS_H
24#define AVCODEC_VVC_VVC_MVS_H
25
26#include "vvcdec.h"
27
28void ff_vvc_round_mv(Mv *mv, int lshift, int rshift);
29void ff_vvc_clip_mv(Mv *mv);
30void ff_vvc_mv_scale(Mv *dst, const Mv *src, int td, int tb);
31void ff_vvc_luma_mv_merge_mode(VVCLocalContext *lc, int merge_idx, int ciip_flag, MvField *mv);
32void ff_vvc_luma_mv_merge_gpm(VVCLocalContext *lc, const int merge_gpm_idx[2], MvField *mv);
33void ff_vvc_mvp(VVCLocalContext *lc, const int *mvp_lx_flag, const int amvr_shift, MotionInfo *mi);
34void ff_vvc_sb_mv_merge_mode(VVCLocalContext *lc, int merge_subblock_idx, PredictionUnit *pu);
35void ff_vvc_affine_mvp(VVCLocalContext *lc, const int *mvp_lx_flag, const int amvr_shift, MotionInfo* mi);
36void ff_vvc_store_sb_mvs(const VVCLocalContext *lc, PredictionUnit *pu);
37void ff_vvc_store_mv(const VVCLocalContext *lc, const MotionInfo *mi);
38void ff_vvc_store_mvf(const VVCLocalContext *lc, const MvField *mvf);
39void ff_vvc_store_gpm_mvf(const VVCLocalContext *lc, const PredictionUnit* pu);
40void ff_vvc_update_hmvp(VVCLocalContext *lc, const MotionInfo *mi);
41int ff_vvc_no_backward_pred_flag(const VVCLocalContext *lc);
42MvField* ff_vvc_get_mvf(const VVCFrameContext *fc, const int x0, const int y0);
43void ff_vvc_set_mvf(const VVCLocalContext *lc, const int x0, const int y0, const int w, const int h, const MvField *mvf);
44void ff_vvc_set_intra_mvf(const VVCLocalContext *lc);
45
46#endif //AVCODEC_VVC_VVC_MVS_H

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t