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
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
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
41 {
43 return 0;
44 for (
int i = 0;
i < 2;
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
58 {
59 int mv[2] = {motion->
x, motion->
y};
60 for (
int i = 0;
i < 2;
i++) {
61 const int s =
mv[
i] >> 17;
63 const int mask = (-1 * (1 <<
f)) >> 1;
64 const int round = (1 <<
f) >> 2;
66 }
69 }
70
72 {
73 int tx, scale_factor;
74
77 tx = (0x4000 + (
abs(td) >> 1)) / td;
80 (scale_factor *
src->x < 0)) >> 8, 17);
82 (scale_factor *
src->y < 0)) >> 8, 17);
83 }
84
85 //part of 8.5.2.12 Derivation process for collocated motion vectors
87 int colPic, int poc,
89 const RefPicList *refPicList_col,
int listCol,
int refidxCol)
90 {
91 int cur_lt = refPicList[
X].
refs[refIdxLx].
is_lt;
92 int col_lt = refPicList_col[listCol].
refs[refidxCol].
is_lt;
93 int col_poc_diff, cur_poc_diff;
94
95 if (cur_lt != col_lt) {
98 return 0;
99 }
100
101 col_poc_diff = colPic - refPicList_col[listCol].
refs[refidxCol].
poc;
102 cur_poc_diff = poc - refPicList[
X].
refs[refIdxLx].
poc;
103
105 if (cur_lt || col_poc_diff == cur_poc_diff) {
108 } else {
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
122 {
123 int check_diffpicount = 0;
126
127 for (j = 0; j < 2; j++) {
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
140 int refIdxLx,
Mv *mvLXCol,
int X,
141 int colPic,
const RefPicList *refPicList_col,
int sb_flag)
142 {
146
150 return 0;
151
152 if (sb_flag){
158 } else {
163 }
164 } else {
173 else
175 } else {
178 else
180 }
181 }
182 }
183 return 0;
184 }
185
186 #define TAB_MVF(x, y) \
187 tab_mvf[((y) >> MIN_PU_LOG2) * min_pu_width + ((x) >> MIN_PU_LOG2)]
188
189 #define TAB_MVF_PU(v) \
190 TAB_MVF(x ## v, y ## v)
191
192 #define TAB_CP_MV(lx, x, y) \
193 fc->tab.cp_mv[lx][((((y) >> min_cb_log2_size) * min_cb_width + ((x) >> min_cb_log2_size)) ) * MAX_CONTROL_POINTS]
194
195
196 #define DERIVE_TEMPORAL_COLOCATED_MVS(sb_flag) \
197 derive_temporal_colocated_mvs(lc, temp_col, \
198 refIdxLx, mvLXCol, X, colPic, \
199 ff_vvc_get_ref_list(fc, ref, x, y), sb_flag)
200
201 //8.5.2.11 Derivation process for temporal luma motion vector prediction
203 const int refIdxLx,
Mv *mvLXCol,
const int X,
int check_center,
int sb_flag)
204 {
210 int x, y, x_end, y_end, colPic, availableFlagLXCol = 0;
211 int min_pu_width =
fc->ps.pps->min_pu_width;
215
217 memset(mvLXCol, 0, sizeof(*mvLXCol));
218 return 0;
219 }
220
222 return 0;
223
224 tab_mvf =
ref->tab_dmvr_mvf;
226
227 //bottom right collocated motion vector
230
231 x_end =
pps->subpic_x[subpic_idx] +
pps->subpic_width[subpic_idx];
232 y_end =
pps->subpic_y[subpic_idx] +
pps->subpic_height[subpic_idx];
233
234 if (tab_mvf &&
235 (cu->
y0 >>
sps->ctb_log2_size_y) == (y >>
sps->ctb_log2_size_y) &&
236 x < x_end && y < y_end) {
237 x &= ~7;
238 y &= ~7;
241 }
242 if (check_center) {
243 // derive center collocated motion vector
244 if (tab_mvf && !availableFlagLXCol) {
247 x &= ~7;
248 y &= ~7;
251 }
252 }
253 return availableFlagLXCol;
254 }
255
257 {
260 const int min_pu_width =
fc->ps.pps->min_pu_width;
262 for (
int dy = 0; dy <
h; dy += min_pu_size) {
263 for (
int dx = 0; dx <
w; dx += min_pu_size) {
264 const int x = x0 + dx;
265 const int y = y0 + dy;
267 }
268 }
269 }
270
272 {
276 const int min_pu_width =
fc->ps.pps->min_pu_width;
278 for (
int dy = 0; dy < cu->
cb_height; dy += min_pu_size) {
279 for (
int dx = 0; dx < cu->
cb_width; dx += min_pu_size) {
280 const int x = cu->
x0 + dx;
281 const int y = cu->
y0 + dy;
283
285 mv->ciip_flag = ciip_flag;
286 }
287 }
288 }
289
290 //cbProfFlagLX from 8.5.5.9 Derivation process for motion vector arrays from affine control point motion vectors
292 {
294 const Mv* cp_mv = &
mi->mv[lx][0];
296 return 0;
299 return 0;
300 }
303 return 0;
304 }
306 return 0;
307 return 1;
308 }
309
318
322
324 {
325 const int a = 4 * (2048 + sp->
d_hor_x);
327 const int c = 4 * (2048 + sp->
d_ver_y);
329 if (pred_flag ==
PF_BI) {
334 const int bx_wx4 = ((max_w4 - min_w4) >> 11) + 9;
335 const int bx_hx4 = ((max_h4 - min_h4) >> 11) + 9;
336 return bx_wx4 * bx_hx4 > 225;
337 } else {
338 const int bx_wxh = (
FFABS(
a) >> 11) + 9;
339 const int bx_hxh = (
FFABS(d) >> 11) + 9;
340 const int bx_wxv = (
FFABS(
b) >> 11) + 9;
341 const int bx_hxv = (
FFABS(
c) >> 11) + 9;
342 if (bx_wxh * bx_hxh <= 165 && bx_wxv * bx_hxv <= 165)
343 return 0;
344 }
345 return 1;
346 }
347
349 const int cb_width, const int cb_height, const int lx)
350 {
351 const int log2_cbw =
av_log2(cb_width);
352 const int log2_cbh =
av_log2(cb_height);
353 const Mv* cp_mv =
mi->mv[lx];
354 const int num_cp_mv =
mi->motion_model_idc + 1;
357 if (num_cp_mv == 3) {
360 } else {
363 }
369 }
370
372 {
375 const int dmv_limit = 1 << 5;
386 }
387 }
388 }
389 }
390
392 {
395 const int log2_min_cb_size =
fc->ps.sps->min_cb_log2_size_y;
396 const int min_cb_size =
fc->ps.sps->min_cb_size_y;
397 const int min_cb_width =
fc->ps.pps->min_cb_width;
398 const int num_cp_mv =
mi->motion_model_idc + 1;
399
400 for (
int dy = 0; dy < cu->
cb_height; dy += min_cb_size) {
401 for (
int dx = 0; dx < cu->
cb_width; dx += min_cb_size) {
402 const int x_cb = (cu->
x0 + dx) >> log2_min_cb_size;
403 const int y_cb = (cu->
y0 + dy) >> log2_min_cb_size;
405
406 memcpy(&
fc->tab.cp_mv[lx][
offset],
mi->mv[lx],
sizeof(
Mv) * num_cp_mv);
407 }
408 }
409 }
410
411 //8.5.5.9 Derivation process for motion vector arrays from affine control point motion vectors
413 {
420
424 for (
int i = 0;
i < 2;
i++) {
426 if (
mi->pred_flag &
mask) {
431 }
432 }
433
434 for (
int sby = 0; sby <
mi->num_sb_y; sby++) {
435 for (
int sbx = 0; sbx <
mi->num_sb_x; sbx++) {
436 const int x0 = cu->
x0 + sbx * sbw;
437 const int y0 = cu->
y0 + sby * sbh;
438 for (
int i = 0;
i < 2;
i++) {
440 if (
mi->pred_flag &
mask) {
445
450 }
451 }
453 }
454 }
455 }
456
458 {
464 const int is_flip = angle_idx >= 13 &&angle_idx <= 27;
465 const int shift_hor = (angle_idx % 16 == 8 || (angle_idx % 16 && cu->
cb_height >= cu->
cb_width)) ? 0 : 1;
466 const int sign = angle_idx < 16 ? 1 : -1;
467 const int block_size = 4;
468 int offset_x = (-cu->
cb_width) >> 1;
470
471 if (!shift_hor)
472 offset_y += sign * ((distance_idx * cu->
cb_height) >> 3);
473 else
474 offset_x += sign * ((distance_idx * cu->
cb_width) >> 3);
475
476 for (
int y = 0; y < cu->
cb_height; y += block_size) {
477 for (
int x = 0; x < cu->
cb_width; x += block_size) {
478 const int motion_idx = (((x + offset_x) * (1 << 1)) + 5) * displacement_x +
479 (((y + offset_y) * (1 << 1)) + 5) * displacement_y;
480 const int s_type =
FFABS(motion_idx) < 32 ? 2 : (motion_idx <= 0 ? (1 - is_flip) : is_flip);
482 const int x0 = cu->
x0 + x;
483 const int y0 = cu->
y0 + y;
484
485 if (!s_type)
487 else if (s_type == 1 || (s_type == 2 && pred_flag !=
PF_BI))
489 else {
495 mvf.
mv[lx] = mv1->
mv[lx];
497 }
498 }
499 }
500 }
501
503 {
506 }
507
509 {
512
516
517 for (
int i = 0;
i < 2;
i++) {
522 }
523 }
525 }
526
538
542
546
551
553 {
555 const int x = x0 >>
sps->min_cb_log2_size_y;
556 const int y = y0 >>
sps->min_cb_log2_size_y;
557 const int min_cb_width =
fc->ps.pps->min_cb_width;
558
560 }
561
563 {
567 int cand_bottom_left;
568
570 cand_bottom_left = 0;
571 } else {
572 const int max_y =
FFMIN(
fc->ps.pps->height, ((cu->
y0 >>
sps->ctb_log2_size_y) + 1) <<
sps->ctb_log2_size_y);
574 cand_bottom_left = 0;
575 else
577 }
578 return cand_bottom_left;
579 }
580
582 {
585 const int x0 = cu->
x0;
586 const int y0 = cu->
y0;
590
592 { x0 - 1, y0 + cb_height, !a0_available }, //A0
593 { x0 - 1, y0 + cb_height - 1, !na->
cand_left },
//A1
596 { x0 + cb_width - 1, y0 - 1, !na->
cand_up },
//B1
598 { x0, y0 - 1, !na->
cand_up },
//B3
599 };
600
601 memcpy(
ctx->neighbours, neighbours,
sizeof(neighbours));
603 }
604
606 {
612 0, // invalid
614 0, // invalid
615 0, // invalid
617 };
618
620 }
621
623 {
628 const int min_pu_width =
fc->ps.pps->min_pu_width;
629
632 n->
available = !
sps->r->sps_entropy_coding_sync_enabled_flag || ((n->
x >>
sps->ctb_log2_size_y) <= (cu->
x0 >>
sps->ctb_log2_size_y));
634 if (check_mer)
636 }
638 }
639
641 {
643 const int min_pu_width =
fc->ps.pps->min_pu_width;
646
647 return mvf;
648 }
649
651 {
654
657 return 0;
658 }
659 #define MV_MERGE_FROM_NB(nb) mv_merge_from_nb(&nctx, nb)
660
661 //8.5.2.3 Derivation process for spatial merging candidates
664 {
666 int num_cands = 0;
668
674 };
675
682 cand_list[num_cands] = *cand;
683 if (merge_idx == num_cands)
684 return 1;
685 num_cands++;
686 }
687 }
688 if (num_cands != 4) {
692 cand_list[num_cands] = *cand;
693 if (merge_idx == num_cands)
694 return 1;
695 num_cands++;
696 }
697 }
698 *nb_merge_cand = num_cands;
699 return 0;
700 }
701
703 {
706
707 memset(cand, 0, sizeof(*cand));
712 cand->
pred_flag = available_l0 + (available_l1 << 1);
713 }
715 }
716
717 //8.5.2.6 Derivation process for history-based merging candidates
720 {
723 for (
int i = 1;
i <= ep->
num_hmvp && (*num_cands <
sps->max_num_merge_cand - 1);
i++) {
726 if (!same_motion) {
727 cand_list[*num_cands] = *
h;
728 if (merge_idx == *num_cands)
729 return 1;
730 (*num_cands)++;
731 }
732 }
733 return 0;
734 }
735
736 //8.5.2.4 Derivation process for pairwise average merging candidate
738 {
739 if (num_cands > 1) {
740 const int num_ref_rists = is_b ? 2 : 1;
741 const MvField* p0 = cand_list + 0;
742 const MvField* p1 = cand_list + 1;
743 MvField* cand = cand_list + num_cands;
744
746 for (
int i = 0;
i < num_ref_rists;
i++) {
756 } else {
758 }
763 }
764 }
769 return 1;
770 }
771 }
772 return 0;
773 }
774
775 //8.5.2.5 Derivation process for zero motion vector merging candidates
777 MvField *cand_list,
int num_cands)
778 {
781 const int num_ref_idx =
IS_P(rsh) ?
783 int zero_idx = 0;
784
785 while (num_cands < sps->max_num_merge_cand) {
786 MvField *cand = cand_list + num_cands;
787
791 cand->
ref_idx[0] = zero_idx < num_ref_idx ? zero_idx : 0;
792 cand->
ref_idx[1] = zero_idx < num_ref_idx ? zero_idx : 0;
795 if (merge_idx == num_cands)
796 return;
797 num_cands++;
798 zero_idx++;
799 }
800 }
801
803 {
804 int num_cands = 0;
806
808 return;
809
811 if (merge_idx == num_cands)
812 return;
813 num_cands++;
814 }
815
817 return;
818
820 if (merge_idx == num_cands)
821 return;
822 num_cands++;
823 }
824
826 }
827
828 //8.5.2.2 Derivation process for luma motion vectors for merge mode
830 {
833
836 *
mv = cand_list[merge_idx];
837 //ciip flag in not inhritable
838 mv->ciip_flag = ciip_flag;
839 }
840
841 //8.5.4.2 Derivation process for luma motion vectors for geometric partitioning merge mode
843 {
846
847 const int idx[] = { merge_gpm_idx[0], merge_gpm_idx[1] + (merge_gpm_idx[1] >= merge_gpm_idx[0]) };
848
851 memset(
mv, 0, 2 *
sizeof(*
mv));
852 for (
int i = 0;
i < 2;
i++) {
857 lx = !lx;
859 }
862 mv[
i].mv[lx] = cand->
mv[lx];
863 }
864
865 }
866
867 //8.5.5.5 Derivation process for luma affine control point motion vectors from a neighbouring block
869 const int x_nb, int y_nb, const int nbw, const int nbh, const int lx,
870 Mv *cps,
int num_cps)
871 {
874 const int x0 = cu->
x0;
875 const int y0 = cu->
y0;
879 const int min_cb_log2_size =
fc->ps.sps->min_cb_log2_size_y;
880 const int min_cb_width =
fc->ps.pps->min_cb_width;
881
882 const int log2_nbw =
ff_log2(nbw);
883 const int log2_nbh =
ff_log2(nbh);
884 const int is_ctb_boundary = !((y_nb + nbh) %
fc->ps.sps->ctb_size_y) && (y_nb + nbh == y0);
886 int mv_scale_hor, mv_scale_ver, d_hor_x, d_ver_x, d_hor_y, d_ver_y, motion_model_idc_nb;
887 if (is_ctb_boundary) {
888 const int min_pu_width =
fc->ps.pps->min_pu_width;
889 l = &
TAB_MVF(x_nb, y_nb + nbh - 1).mv[lx];
890 r = &
TAB_MVF(x_nb + nbw - 1, y_nb + nbh - 1).mv[lx];
891 } else {
892 const int x = x_nb >> min_cb_log2_size;
893 const int y = y_nb >> min_cb_log2_size;
895
898 }
899 mv_scale_hor = l->
x * (1 << 7);
900 mv_scale_ver = l->
y * (1 << 7);
901 d_hor_x = (
r->x - l->
x) * (1 << (7 - log2_nbw));
902 d_ver_x = (
r->y - l->
y) * (1 << (7 - log2_nbw));
904 const Mv* lb = &
TAB_CP_MV(lx, x_nb, y_nb + nbh - 1) + 2;
905 d_hor_y = (lb->
x - l->
x) * (1 << (7 - log2_nbh));
906 d_ver_y = (lb->
y - l->
y) * (1 << (7 - log2_nbh));
907 } else {
908 d_hor_y = -d_ver_x;
909 d_ver_y = d_hor_x;
910 }
911
912 if (is_ctb_boundary) {
913 y_nb = y0;
914 }
915 cps[0].
x = mv_scale_hor + d_hor_x * (x0 - x_nb) + d_hor_y * (y0 - y_nb);
916 cps[0].
y = mv_scale_ver + d_ver_x * (x0 - x_nb) + d_ver_y * (y0 - y_nb);
917 cps[1].
x = mv_scale_hor + d_hor_x * (x0 + cb_width - x_nb) + d_hor_y * (y0 - y_nb);
918 cps[1].
y = mv_scale_ver + d_ver_x * (x0 + cb_width - x_nb) + d_ver_y * (y0 - y_nb);
919 if (num_cps == 3) {
920 cps[2].
x = mv_scale_hor + d_hor_x * (x0 - x_nb) + d_hor_y * (y0 + cb_height - y_nb);
921 cps[2].
y = mv_scale_ver + d_ver_x * (x0 - x_nb) + d_ver_y * (y0 + cb_height - y_nb);
922 }
923 for (
int i = 0;
i < num_cps;
i++) {
926 }
927 }
928
929 //derive affine neighbour's position, width and height,
931 {
932 const int log2_min_cb_size =
fc->ps.sps->min_cb_log2_size_y;
933 const int min_cb_width =
fc->ps.pps->min_cb_width;
934 const int x = x_nb >> log2_min_cb_size;
935 const int y = y_nb >> log2_min_cb_size;
936 const int motion_model_idc =
SAMPLE_CTB(
fc->tab.mmi, x, y);
937 if (motion_model_idc) {
942 }
943 return motion_model_idc;
944 }
945
946 //part of 8.5.5.2 Derivation process for motion vectors and reference indices in subblock merge mode
948 {
950 int x, y,
w,
h, motion_model_idc;
951
953 if (motion_model_idc) {
954 const int min_pu_width =
fc->ps.pps->min_pu_width;
957
960 for (
int i = 0;
i < 2;
i++) {
962 if (
mi->pred_flag &
mask) {
964 }
966 }
967 mi->motion_model_idc = motion_model_idc;
968 }
969 return motion_model_idc;
970 }
971
973 {
975 for (
int i = 0;
i < num_nbs;
i++) {
978 return 1;
979 }
980 return 0;
981 }
982 #define AFFINE_MERGE_FROM_NBS(nbs) affine_merge_from_nbs(&nctx, nbs, FF_ARRAY_ELEMS(nbs), mi)
983
984
986 {
989 const int min_pu_width =
fc->ps.pps->min_pu_width;
990 for (
int i = 0;
i < num_neighbour;
i++) {
994 }
995 }
997 }
998
999 #define DERIVE_CORNER_MV(nbs) derive_corner_mvf(nctx, nbs, FF_ARRAY_ELEMS(nbs))
1000
1001 // check if the mv's and refidx are the same between A and B
1003 {
1004
1007 return 0;
1008 if (
A->ref_idx[lx] !=
B->ref_idx[lx])
1009 return 0;
1011 if (!(
C->pred_flag &
mask))
1012 return 0;
1013 if (
A->ref_idx[lx] !=
C->ref_idx[lx])
1014 return 0;
1015 }
1016 return 1;
1017 }
1018
1020 const int x_ctb,
const int y_ctb,
const Mv* temp_mv,
int *x,
int *y)
1021 {
1024 const int ctb_log2_size =
fc->ps.sps->ctb_log2_size_y;
1026 const int x_end =
pps->subpic_x[subpic_idx] +
pps->subpic_width[subpic_idx];
1027 const int y_end =
pps->subpic_y[subpic_idx] +
pps->subpic_height[subpic_idx];
1028
1029 *x =
av_clip(*x + temp_mv->
x, x_ctb,
FFMIN(x_end - 1, x_ctb + (1 << ctb_log2_size) + 3)) & ~7;
1030 *y =
av_clip(*y + temp_mv->
y, y_ctb,
FFMIN(y_end - 1, y_ctb + (1 << ctb_log2_size) - 1)) & ~7;
1031 }
1032
1034 const int x_ctb,
const int y_ctb,
const Mv *temp_mv,
1035 int x,
int y, uint8_t *pred_flag,
Mv *
mv)
1036 {
1039 const int refIdxLx = 0;
1042 const int min_pu_width =
fc->ps.pps->min_pu_width;
1045 int colPic =
ref->poc;
1047
1049
1057 }
1058 }
1059
1060 //8.5.5.4 Derivation process for subblock-based temporal merging base motion data
1062 const int x_ctb,
const int y_ctb,
MvField *ctr_mvf,
Mv *temp_mv)
1063 {
1070
1071 int colPic;
1072
1073 memset(temp_mv, 0, sizeof(*temp_mv));
1074
1076 memset(ctr_mvf, 0, sizeof(*ctr_mvf));
1077 return 0;
1078 }
1079
1081
1084 *temp_mv =
a1->mv[0];
1086 *temp_mv =
a1->mv[1];
1088 }
1090
1092 }
1093
1094
1095 //8.5.5.3 Derivation process for subblock-based temporal merging candidates
1097 {
1103 const int ctb_log2_size =
sps->ctb_log2_size_y;
1104 const int x0 = cu->
x0;
1105 const int y0 = cu->
y0;
1110 const int x_ctb = (x0 >> ctb_log2_size) << ctb_log2_size;
1111 const int y_ctb = (y0 >> ctb_log2_size) << ctb_log2_size;
1112
1113
1114 if (!
ph->r->ph_temporal_mvp_enabled_flag ||
1115 !
sps->r->sps_sbtmvp_enabled_flag ||
1117 return 0;
1118
1121
1127 for (
int sby = 0; sby <
mi->num_sb_y; sby++) {
1128 for (
int sbx = 0; sbx <
mi->num_sb_x; sbx++) {
1129 int x = x0 + sbx * sbw;
1130 int y = y0 + sby * sbh;
1134 memcpy(mvf.
mv, ctr_mvf.
mv,
sizeof(mvf.
mv));
1135 }
1137 }
1138 }
1139 return 1;
1140 }
1141 return 0;
1142 }
1143
1145 {
1146 if (c0 &&
c1 &&
c2) {
1148 for (
int i = 0;
i < 2;
i++) {
1153 mi->mv[
i][0] = c0->
mv[
i];
1154 mi->mv[
i][1] =
c1->mv[
i];
1155 mi->mv[
i][2] =
c2->mv[
i];
1156 }
1157 }
1158 if (
mi->pred_flag) {
1162 return 1;
1163 }
1164 }
1165 return 0;
1166 }
1167
1169 {
1170 if (c0 &&
c1 && c3) {
1172 for (
int i = 0;
i < 2;
i++) {
1177 mi->mv[
i][0] = c0->
mv[
i];
1178 mi->mv[
i][1] =
c1->mv[
i];
1182 }
1183 }
1184 if (
mi->pred_flag) {
1187 return 1;
1188 }
1189 }
1190 return 0;
1191 }
1192
1194 {
1195 if (c0 &&
c2 && c3) {
1197 for (
int i = 0;
i < 2;
i++) {
1202 mi->mv[
i][0] = c0->
mv[
i];
1206 mi->mv[
i][2] =
c2->mv[
i];
1207 }
1208 }
1209 if (
mi->pred_flag) {
1212 return 1;
1213 }
1214 }
1215 return 0;
1216 }
1217
1219 {
1220 if (
c1 &&
c2 && c3) {
1222 for (
int i = 0;
i < 2;
i++) {
1226 mi->ref_idx[
i] =
c1->ref_idx[
i];
1230 mi->mv[
i][1] =
c1->mv[
i];
1231 mi->mv[
i][2] =
c2->mv[
i];
1232 }
1233 }
1234 if (
mi->pred_flag) {
1235 mi->bcw_idx =
mi->pred_flag ==
PF_BI ?
c1->bcw_idx : 0;
1237 return 1;
1238 }
1239 }
1240 return 0;
1241 }
1242
1244 {
1247 for (
int i = 0;
i < 2;
i++) {
1252 mi->mv[
i][0] = c0->
mv[
i];
1253 mi->mv[
i][1] =
c1->mv[
i];
1254 }
1255 }
1256 if (
mi->pred_flag) {
1260 return 1;
1261 }
1262 }
1263 return 0;
1264 }
1265
1267 {
1271 for (
int i = 0;
i < 2;
i++) {
1276 mi->mv[
i][0] = c0->
mv[
i];
1281 }
1282 }
1283 if (
mi->pred_flag) {
1287 return 1;
1288 }
1289 }
1290 return 0;
1291 }
1292
1294 {
1296
1297 memset(
mi, 0,
sizeof(*
mi));
1302 }
1303
1304 //8.5.5.6 Derivation process for constructed affine control point motion vector merging candidates
1307 {
1314
1318
1319 if (
fc->ps.sps->r->sps_6param_affine_enabled_flag) {
1321 //Const1
1323 if (merge_subblock_idx == num_cands)
1324 return 1;
1325 num_cands++;
1326 }
1327
1328 memset(&corner3, 0, sizeof(corner3));
1329 if (
fc->ps.ph.r->ph_temporal_mvp_enabled_flag){
1333
1334 corner3.
pred_flag = available_l0 + (available_l1 << 1);
1336 c3 = &corner3;
1337 }
1338
1339 //Const2
1341 if (merge_subblock_idx == num_cands)
1342 return 1;
1343 num_cands++;
1344 }
1345
1346 //Const3
1348 if (merge_subblock_idx == num_cands)
1349 return 1;
1350 num_cands++;
1351 }
1352
1353 //Const4
1355 if (merge_subblock_idx == num_cands)
1356 return 1;
1357 num_cands++;
1358 }
1359 }
1360
1361 //Const5
1363 if (merge_subblock_idx == num_cands)
1364 return 1;
1365 num_cands++;
1366 }
1367
1369 if (merge_subblock_idx == num_cands)
1370 return 1;
1371 }
1372 return 0;
1373 }
1374
1375 //8.5.5.2 Derivation process for motion vectors and reference indices in subblock merge mode
1376 //return 1 if candidate is SbCol
1378 {
1382 int num_cands = 0;
1384
1386
1387 //SbCol
1389 if (merge_subblock_idx == num_cands)
1390 return 1;
1391 num_cands++;
1392 }
1393
1397
1398 if (
sps->r->sps_affine_enabled_flag) {
1401 //A
1403 if (merge_subblock_idx == num_cands)
1404 return 0;
1405 num_cands++;
1406 }
1407
1408 //B
1410 if (merge_subblock_idx == num_cands)
1411 return 0;
1412 num_cands++;
1413 }
1414
1415 //Const1 to Const6
1417 return 0;
1418 }
1419 //Zero
1421 return 0;
1422 }
1423
1425 {
1430 }
1431 }
1432
1434 const int lx,
const int8_t *ref_idx,
Mv *
mv)
1435 {
1438 const int min_pu_width =
fc->ps.pps->min_pu_width;
1442 const int poc = rpl[lx].
refs[ref_idx[lx]].
poc;
1444
1448 } else {
1449 const int ly = !lx;
1454 }
1455 }
1456
1458 }
1459
1461 const int x_cand, const int y_cand, const int lx, const int8_t *ref_idx,
1462 Mv *cps,
const int num_cp)
1463 {
1465 int x_nb, y_nb, nbw, nbh, motion_model_idc,
available = 0;
1466
1468 if (motion_model_idc) {
1469 const int min_pu_width =
fc->ps.pps->min_pu_width;
1474 const int poc = rpl[lx].
refs[ref_idx[lx]].
poc;
1475
1479 } else {
1480 const int ly = !lx;
1485 }
1486 }
1487
1488 }
1490 }
1491
1493 const NeighbourIdx *nbs,
const int num_nbs,
const int lx,
const int8_t *ref_idx,
const int amvr_shift,
1494 Mv *cps,
const int num_cps)
1495 {
1498
1499 for (
int i = 0;
i < num_nbs;
i++) {
1502 if (num_cps > 1)
1504 else
1507 for (
int c = 0;
c < num_cps;
c++)
1509 return 1;
1510 }
1511 }
1512 }
1513 return 0;
1514 }
1515
1516 //get mvp from neighbours
1517 #define AFFINE_MVP_FROM_NBS(nbs) \
1518 mvp_from_nbs(&nctx, nbs, FF_ARRAY_ELEMS(nbs), lx, ref_idx, amvr_shift, cps, num_cp) \
1519
1520 #define MVP_FROM_NBS(nbs) \
1521 mvp_from_nbs(&nctx, nbs, FF_ARRAY_ELEMS(nbs), lx, ref_idx, amvr_shift, mv, 1) \
1522
1524 const int mvp_lx_flag, const int lx, const int8_t* ref_idx, const int amvr_shift,
1525 Mv*
mv,
int *nb_merge_cand)
1526 {
1530 int available_a, num_cands = 0;
1532
1534
1536 if (available_a) {
1537 if (mvp_lx_flag == num_cands)
1538 return 1;
1539 num_cands++;
1541 }
1544 if (mvp_lx_flag == num_cands)
1545 return 1;
1546 num_cands++;
1547 }
1548 }
1549 *nb_merge_cand = num_cands;
1550 return 0;
1551 }
1552
1554 const int mvp_lx_flag, const int lx, const int8_t *ref_idx, const int amvr_shift,
1555 Mv*
mv,
int *num_cands)
1556 {
1558 if (mvp_lx_flag == *num_cands) {
1560 return 1;
1561 }
1562 (*num_cands)++;
1563 }
1564 return 0;
1565
1566 }
1567
1569 const int mvp_lx_flag, const int lx, const int8_t ref_idx, const int amvr_shift,
1570 Mv *
mv,
int num_cands)
1571 {
1574 const int poc = rpl[lx].
refs[ref_idx].
poc;
1575
1577 return 0;
1580 for (int j = 0; j < 2; j++) {
1581 const int ly = (j ? !lx : lx);
1583 if ((
h->pred_flag &
mask) && poc == rpl[ly].
refs[
h->ref_idx[ly]].
poc) {
1584 if (mvp_lx_flag == num_cands) {
1587 return 1;
1588 }
1589 num_cands++;
1590 }
1591 }
1592 }
1593 return 0;
1594 }
1595
1596 //8.5.2.8 Derivation process for luma motion vector prediction
1598 const int8_t *ref_idx,
const int amvr_shift,
Mv *
mv)
1599 {
1600 int num_cands;
1601
1603 return;
1604
1606 return;
1607
1609 return;
1610
1611 memset(
mv, 0,
sizeof(*
mv));
1612 }
1613
1615 {
1619
1622 mvp(lc, mvp_lx_flag[
L0],
L0,
mi->ref_idx, amvr_shift, &
mi->mv[
L0][0]);
1624 mvp(lc, mvp_lx_flag[
L1],
L1,
mi->ref_idx, amvr_shift, &
mi->mv[
L1][0]);
1625 }
1626
1628 {
1631 const int min_pu_width =
fc->ps.pps->min_pu_width;
1634 int num_cands = 0;
1635
1639
1640 if (!is_gt4by4) {
1641 *nb_merge_cand = 0;
1642 return 0;
1643 }
1644
1646
1649 if (num_cands > merge_idx)
1650 return 1;
1651 }
1654 if (!num_cands || !
IS_SAME_MV(&cand_list[0], mvf->
mv)) {
1655 cand_list[num_cands++] = mvf->
mv[
L0];
1656 if (num_cands > merge_idx)
1657 return 1;
1658 }
1659 }
1660
1661 *nb_merge_cand = num_cands;
1662 return 0;
1663 }
1664
1666 const int merge_idx,
Mv *cand_list,
int *nb_merge_cand)
1667 {
1671 int num_cands = *nb_merge_cand;
1672
1674 int same_motion = 0;
1676 for (int j = 0; j < *nb_merge_cand; j++) {
1677 same_motion = is_gt4by4 &&
i == 1 &&
IS_SAME_MV(&mvf->
mv[
L0], &cand_list[j]);
1678 if (same_motion)
1679 break;
1680 }
1681 if (!same_motion) {
1682 cand_list[num_cands++] = mvf->
mv[
L0];
1683 if (num_cands > merge_idx)
1684 return 1;
1685 }
1686 }
1687
1688 *nb_merge_cand = num_cands;
1689 return 0;
1690 }
1691
1693 #define IBC_SHIFT(v) ((v) >= (1 << (MV_BITS - 1)) ? ((v) - (1 << MV_BITS)) : (v))
1694
1696 {
1701 }
1702
1704 {
1707 int nb_cands;
1708
1712 *
mv = cand_list[merge_idx];
1713 return;
1714 }
1715
1716 //zero mv
1717 memset(
mv, 0,
sizeof(*
mv));
1718 }
1719
1721 {
1726
1727 if (
sps->ctb_size_y < ((cu->
y0 + (bv->
y >> 4)) & (
sps->ctb_size_y - 1)) + cu->
cb_height) {
1730 }
1731
1732 return 0;
1733 }
1734
1736 {
1738
1742 }
1743
1745 {
1748 }
1749
1751 const NeighbourIdx *neighbour,
const int num_neighbour,
1752 const int lx,
const int8_t ref_idx,
const int amvr_shift,
Mv *cp)
1753 {
1757 const int min_pu_width =
fc->ps.pps->min_pu_width;
1760
1761 for (
int i = 0;
i < num_neighbour;
i++) {
1766 const int poc = rpl[lx].
refs[ref_idx].
poc;
1770 } else {
1771 const int ly = !lx;
1776 }
1777 }
1780 return 1;
1781 }
1782 }
1783 }
1784 return 0;
1785 }
1786
1787 #define AFFINE_MVP_CONSTRUCTED_CP(cands, cp) \
1788 affine_mvp_constructed_cp(nctx, cands, FF_ARRAY_ELEMS(cands), lx, ref_idx, \
1789 amvr_shift, cp)
1790
1791 //8.5.5.8 Derivation process for constructed affine control point motion vector prediction candidates
1793 const int lx, const int8_t ref_idx, const int amvr_shift,
1795 {
1799
1804 }
1805
1806 //8.5.5.7 item 7
1808 {
1809 const Mv mv = cps[idx];
1810 for (int j = 0; j < num_cp; j++)
1812 }
1813
1814 //8.5.5.7 Derivation process for luma affine control point motion vector predictors
1816 const int mvp_lx_flag, const int lx, const int8_t *ref_idx, const int amvr_shift,
1818 {
1821 const int num_cp = motion_model_idc + 1;
1824 int num_cands = 0;
1825
1827 //Ak
1829 if (mvp_lx_flag == num_cands)
1830 return;
1831 num_cands++;
1832 }
1833 //Bk
1835 if (mvp_lx_flag == num_cands)
1836 return;
1837 num_cands++;
1838 }
1839
1840 //Const1
1843 if (mvp_lx_flag == num_cands)
1844 return;
1845 num_cands++;
1846 }
1847 }
1848
1849 //Const2
1850 for (
int i = 2;
i >= 0;
i--) {
1852 if (mvp_lx_flag == num_cands) {
1854 return;
1855 }
1856 num_cands++;
1857 }
1858 }
1860 if (mvp_lx_flag == num_cands) {
1862 for (
int i = 1;
i < num_cp;
i++)
1864 return;
1865 }
1866 num_cands++;
1867 }
1868
1869 //Zero Mv
1870 memset(cps, 0, num_cp *
sizeof(
Mv));
1871 }
1872
1874 {
1876
1879
1885 }
1886
1887 //8.5.2.14 Rounding process for motion vectors
1889 {
1890 if (rshift) {
1891 const int offset = 1 << (rshift - 1);
1892 mv->x = ((
mv->x +
offset - (
mv->x >= 0)) >> rshift) * (1 << lshift);
1893 mv->y = ((
mv->y +
offset - (
mv->y >= 0)) >> rshift) * (1 << lshift);
1894 } else {
1895 mv->x =
mv->x * (1 << lshift);
1896 mv->y =
mv->y * (1 << lshift);
1897 }
1898 }
1899
1901 {
1902 mv->x =
av_clip(
mv->x, -(1 << 17), (1 << 17) - 1);
1903 mv->y =
av_clip(
mv->y, -(1 << 17), (1 << 17) - 1);
1904 }
1905
1906 //8.5.2.1 Derivation process for motion vector components and reference indices
1908 {
1909 const uint8_t plevel =
fc->ps.sps->log2_parallel_merge_level;
1910
1911 return x0_br >> plevel > x0 >> plevel &&
1912 y0_br >> plevel > y0 >> plevel;
1913 }
1914
1917 {
1919 for (
i = 0;
i < *num_hmvp;
i++) {
1921 (*num_hmvp)--;
1922 break;
1923 }
1924 }
1926 (*num_hmvp)--;
1928 }
1929
1930 memmove(hmvp +
i, hmvp +
i + 1, (*num_hmvp -
i) *
sizeof(
MvField));
1931 hmvp[(*num_hmvp)++] = *mvf;
1932 }
1933
1935 {
1937 }
1938
1939 //8.6.2.4 Derivation process for IBC history-based block vector candidates
1940 //8.5.2.16 Updating process for the history-based motion vector predictor candidate list
1942 {
1945 const int min_pu_width =
fc->ps.pps->min_pu_width;
1948
1951 return;
1953 } else {
1955 return;
1957 }
1958 }
1959
1961 {
1962 const int min_pu_width =
fc->ps.pps->min_pu_width;
1964
1966 }