../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
117 / 118
99.2%
Branches:
105 / 114
92.1%
Functions:
11 / 11
100.0%

libavcodec/vvc/intra_utils.c
Line Branch Exec Source
1 /*
2 * VVC intra prediction utils
3 *
4 * Copyright (C) 2021 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 #include <stdint.h>
24 #include <stdlib.h>
25 #include "libavutil/avassert.h"
26 #include "libavutil/macros.h"
27 #include "libavutil/common.h"
28 #include "ctu.h"
29 #include "intra.h"
30 #include "ps.h"
31 #include "dec.h"
32
33 116963 int ff_vvc_get_mip_size_id(const int w, const int h)
34 {
35
4/4
✓ Branch 2 → 3 taken 33923 times.
✓ Branch 2 → 5 taken 83040 times.
✓ Branch 3 → 4 taken 16804 times.
✓ Branch 3 → 5 taken 17119 times.
116963 if (w == 4 && h == 4)
36 16804 return 0;
37
8/8
✓ Branch 5 → 6 taken 83040 times.
✓ Branch 5 → 9 taken 17119 times.
✓ Branch 6 → 7 taken 57961 times.
✓ Branch 6 → 9 taken 25079 times.
✓ Branch 7 → 8 taken 19068 times.
✓ Branch 7 → 10 taken 38893 times.
✓ Branch 8 → 9 taken 11121 times.
✓ Branch 8 → 10 taken 7947 times.
100159 if ((w == 4 || h == 4) || (w == 8 && h == 8))
38 53319 return 1;
39 46840 return 2;
40 }
41
42 562532 int ff_vvc_nscale_derive(const int w, const int h, const int mode)
43 {
44 int side_size, nscale;
45
4/6
✓ Branch 2 → 3 taken 562532 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 257702 times.
✓ Branch 3 → 7 taken 304830 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 7 taken 257702 times.
562532 av_assert0(mode < INTRA_LT_CCLM && !(mode > INTRA_HORZ && mode < INTRA_VERT));
46
3/6
✓ Branch 7 → 8 taken 562532 times.
✗ Branch 7 → 11 not taken.
✓ Branch 8 → 9 taken 562532 times.
✗ Branch 8 → 11 not taken.
✓ Branch 9 → 10 taken 562532 times.
✗ Branch 9 → 11 not taken.
562532 if (mode == INTRA_PLANAR || mode == INTRA_DC ||
47
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 562532 times.
562532 mode == INTRA_HORZ || mode == INTRA_VERT) {
48 ✗ nscale = (av_log2(w) + av_log2(h) - 2) >> 2;
49 } else {
50 562532 const int intra_pred_angle = ff_vvc_intra_pred_angle_derive(mode);
51 562532 const int inv_angle = ff_vvc_intra_inv_angle_derive(intra_pred_angle);
52
2/2
✓ Branch 14 → 15 taken 257702 times.
✓ Branch 14 → 16 taken 304830 times.
562532 if (mode >= INTRA_VERT)
53 257702 side_size = h;
54
2/2
✓ Branch 16 → 17 taken 304830 times.
✓ Branch 16 → 18 taken 257702 times.
562532 if (mode <= INTRA_HORZ)
55 304830 side_size = w;
56 562532 nscale = FFMIN(2, av_log2(side_size) - av_log2(3 * inv_angle - 2) + 8);
57 }
58 562532 return nscale;
59 }
60
61 1708343 int ff_vvc_need_pdpc(const int w, const int h, const uint8_t bdpcm_flag, const int mode, const int ref_idx)
62 {
63
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 1708343 times.
1708343 av_assert0(mode < INTRA_LT_CCLM);
64
7/8
✓ Branch 5 → 6 taken 1708343 times.
✗ Branch 5 → 19 not taken.
✓ Branch 6 → 7 taken 1569823 times.
✓ Branch 6 → 19 taken 138520 times.
✓ Branch 7 → 8 taken 1515093 times.
✓ Branch 7 → 19 taken 54730 times.
✓ Branch 8 → 9 taken 1495628 times.
✓ Branch 8 → 19 taken 19465 times.
1708343 if ((w >= 4 && h >= 4) && !ref_idx && !bdpcm_flag) {
65 int nscale;
66
6/6
✓ Branch 9 → 10 taken 1015876 times.
✓ Branch 9 → 13 taken 479752 times.
✓ Branch 10 → 11 taken 908523 times.
✓ Branch 10 → 13 taken 107353 times.
✓ Branch 11 → 12 taken 836468 times.
✓ Branch 11 → 13 taken 72055 times.
1495628 if (mode == INTRA_PLANAR || mode == INTRA_DC ||
67
2/2
✓ Branch 12 → 13 taken 77345 times.
✓ Branch 12 → 14 taken 759123 times.
836468 mode == INTRA_HORZ || mode == INTRA_VERT)
68 736505 return 1;
69
4/4
✓ Branch 14 → 15 taken 541016 times.
✓ Branch 14 → 17 taken 218107 times.
✓ Branch 15 → 16 taken 361135 times.
✓ Branch 15 → 17 taken 179881 times.
759123 if (mode > INTRA_HORZ && mode < INTRA_VERT)
70 361135 return 0;
71 397988 nscale = ff_vvc_nscale_derive(w, h, mode);
72 397988 return nscale >= 0;
73
74 }
75 212715 return 0;
76 }
77
78 5864068 static const ReconstructedArea* get_reconstructed_area(const VVCLocalContext *lc, const int x, const int y, const int c_idx)
79 {
80 5864068 const int ch_type = c_idx > 0;
81
2/2
✓ Branch 12 → 3 taken 54078004 times.
✓ Branch 12 → 13 taken 100849 times.
54178853 for (int i = lc->num_ras[ch_type] - 1; i >= 0; i--) {
82 54078004 const ReconstructedArea* a = &lc->ras[ch_type][i];
83 54078004 const int r = (a->x + a->w);
84 54078004 const int b = (a->y + a->h);
85
8/8
✓ Branch 3 → 4 taken 29923203 times.
✓ Branch 3 → 8 taken 24154801 times.
✓ Branch 4 → 5 taken 9408672 times.
✓ Branch 4 → 8 taken 20514531 times.
✓ Branch 5 → 6 taken 6556745 times.
✓ Branch 5 → 8 taken 2851927 times.
✓ Branch 6 → 7 taken 4645099 times.
✓ Branch 6 → 8 taken 1911646 times.
54078004 if (a->x <= x && x < r && a->y <= y && y < b)
86 4645099 return a;
87
88 //it's too far away, no need check it;
89
4/4
✓ Branch 8 → 9 taken 20514531 times.
✓ Branch 8 → 11 taken 28918374 times.
✓ Branch 9 → 10 taken 1118120 times.
✓ Branch 9 → 11 taken 19396411 times.
49432905 if (x >= r && y >= b)
90 1118120 break;
91 }
92 1218969 return NULL;
93 }
94
95 1832056 int ff_vvc_get_top_available(const VVCLocalContext *lc, const int x, const int y, int target_size, const int c_idx)
96 {
97 1832056 const VVCFrameContext *fc = lc->fc;
98 1832056 const VVCSPS *sps = fc->ps.sps;
99 1832056 const int hs = sps->hshift[c_idx];
100 1832056 const int vs = sps->vshift[c_idx];
101 1832056 const int log2_ctb_size_v = sps->ctb_log2_size_y - vs;
102 1832056 const int end_of_ctb_x = ((lc->cu->x0 >> sps->ctb_log2_size_y) + 1) << sps->ctb_log2_size_y;
103 1832056 const int y0b = av_zero_extend(y, log2_ctb_size_v);
104 1832056 const int max_x = FFMIN(fc->ps.pps->width, end_of_ctb_x) >> hs;
105 const ReconstructedArea *a;
106 1832056 int px = x;
107
108
2/2
✓ Branch 2 → 3 taken 190011 times.
✓ Branch 2 → 8 taken 1642045 times.
1832056 if (!y0b) {
109
2/2
✓ Branch 3 → 4 taken 53238 times.
✓ Branch 3 → 5 taken 136773 times.
190011 if (!lc->ctb_up_flag)
110 53238 return 0;
111 136773 target_size = FFMIN(target_size, (lc->end_of_tiles_x >> hs) - x);
112
2/2
✓ Branch 5 → 6 taken 4760 times.
✓ Branch 5 → 7 taken 132013 times.
136773 if (sps->r->sps_entropy_coding_sync_enabled_flag)
113 4760 target_size = FFMIN(target_size, (end_of_ctb_x >> hs) - x);
114 136773 return target_size;
115 }
116
117 1642045 target_size = FFMAX(0, FFMIN(target_size, max_x - x));
118
4/4
✓ Branch 10 → 11 taken 2938545 times.
✓ Branch 10 → 13 taken 1057497 times.
✓ Branch 12 → 9 taken 2353997 times.
✓ Branch 12 → 13 taken 584548 times.
3996042 while (target_size > 0 && (a = get_reconstructed_area(lc, px, y - 1, c_idx))) {
119 2353997 const int sz = FFMIN(target_size, a->x + a->w - px);
120 2353997 px += sz;
121 2353997 target_size -= sz;
122 }
123 1642045 return px - x;
124 }
125
126 1829615 int ff_vvc_get_left_available(const VVCLocalContext *lc, const int x, const int y, int target_size, const int c_idx)
127 {
128 1829615 const VVCFrameContext *fc = lc->fc;
129 1829615 const VVCSPS *sps = fc->ps.sps;
130 1829615 const int hs = sps->hshift[c_idx];
131 1829615 const int vs = sps->vshift[c_idx];
132 1829615 const int log2_ctb_size_h = sps->ctb_log2_size_y - hs;
133 1829615 const int x0b = av_zero_extend(x, log2_ctb_size_h);
134 1829615 const int end_of_ctb_y = ((lc->cu->y0 >> sps->ctb_log2_size_y) + 1) << sps->ctb_log2_size_y;
135 1829615 const int max_y = FFMIN(fc->ps.pps->height, end_of_ctb_y) >> vs;
136 const ReconstructedArea *a;
137 1829615 int py = y;
138
139
4/4
✓ Branch 2 → 3 taken 226603 times.
✓ Branch 2 → 5 taken 1603012 times.
✓ Branch 3 → 4 taken 45894 times.
✓ Branch 3 → 5 taken 180709 times.
1829615 if (!x0b && !lc->ctb_left_flag)
140 45894 return 0;
141
142 1783721 target_size = FFMAX(0, FFMIN(target_size, max_y - y));
143
2/2
✓ Branch 5 → 6 taken 180709 times.
✓ Branch 5 → 7 taken 1603012 times.
1783721 if (!x0b)
144 180709 return target_size;
145
146
4/4
✓ Branch 9 → 10 taken 2925523 times.
✓ Branch 9 → 12 taken 968591 times.
✓ Branch 11 → 8 taken 2291102 times.
✓ Branch 11 → 12 taken 634421 times.
3894114 while (target_size > 0 && (a = get_reconstructed_area(lc, x - 1, py, c_idx))) {
147 2291102 const int sz = FFMIN(target_size, a->y + a->h - py);
148 2291102 py += sz;
149 2291102 target_size -= sz;
150 }
151 1603012 return py - y;
152 }
153
154 5887031 static int less(const void *a, const void *b)
155 {
156 5887031 return *(const int*)a - *(const int*)b;
157 }
158
159 1594692 int ff_vvc_ref_filter_flag_derive(const int mode)
160 {
161 static const int modes[] = { -14, -12, -10, -6, INTRA_PLANAR, 2, 34, 66, 72, 76, 78, 80};
162 1594692 return bsearch(&mode, modes, FF_ARRAY_ELEMS(modes), sizeof(int), less) != NULL;
163 }
164
165 2143547 int ff_vvc_intra_pred_angle_derive(const int pred_mode)
166 {
167 static const int angles[] = {
168 0, 1, 2, 3, 4, 6, 8, 10, 12, 14, 16, 18, 20, 23, 26, 29,
169 32, 35, 39, 45, 51, 57, 64, 73, 86, 102, 128, 171, 256, 341, 512
170 };
171 2143547 int sign = 1, idx, intra_pred_angle;
172
2/2
✓ Branch 2 → 3 taken 1090105 times.
✓ Branch 2 → 4 taken 1053442 times.
2143547 if (pred_mode > INTRA_DIAG) {
173 1090105 idx = pred_mode - INTRA_VERT;
174
2/2
✓ Branch 4 → 5 taken 972536 times.
✓ Branch 4 → 6 taken 80906 times.
1053442 } else if (pred_mode > 0) {
175 972536 idx = INTRA_HORZ - pred_mode;
176 } else {
177 80906 idx = INTRA_HORZ - 2 - pred_mode;
178 }
179
2/2
✓ Branch 7 → 8 taken 836011 times.
✓ Branch 7 → 9 taken 1307536 times.
2143547 if (idx < 0) {
180 836011 idx = -idx;
181 836011 sign = -1;
182 }
183 2143547 intra_pred_angle = sign * angles[idx];
184 2143547 return intra_pred_angle;
185 }
186
187 1503356 int ff_vvc_intra_inv_angle_derive(const int intra_pred_angle)
188 {
189 av_assert2(intra_pred_angle != 0);
190
2/2
✓ Branch 2 → 3 taken 1092348 times.
✓ Branch 2 → 4 taken 411008 times.
1503356 if (intra_pred_angle > 0)
191 1092348 return ROUNDED_DIV(32*512, intra_pred_angle);
192 else
193 411008 return -ROUNDED_DIV(32*512, -intra_pred_angle);
194 }
195
196 //8.4.5.2.7 Wide angle intra prediction mode mapping process
197 1869712 int ff_vvc_wide_angle_mode_mapping(const CodingUnit *cu,
198 const int tb_width, const int tb_height, const int c_idx, int pred_mode_intra)
199 {
200 int nw, nh, wh_ratio, min, max;
201
202
4/4
✓ Branch 2 → 3 taken 238808 times.
✓ Branch 2 → 4 taken 1630904 times.
✓ Branch 3 → 4 taken 8710 times.
✓ Branch 3 → 5 taken 230098 times.
1869712 if (cu->isp_split_type == ISP_NO_SPLIT || c_idx) {
203 1639614 nw = tb_width;
204 1639614 nh = tb_height;
205 } else {
206 230098 nw = cu->cb_width;
207 230098 nh = cu->cb_height;
208 }
209 1869712 wh_ratio = FFABS(ff_log2(nw) - ff_log2(nh));
210
2/2
✓ Branch 6 → 7 taken 402409 times.
✓ Branch 6 → 8 taken 1467303 times.
1869712 max = (wh_ratio > 1) ? (8 + 2 * wh_ratio) : 8;
211
2/2
✓ Branch 9 → 10 taken 402409 times.
✓ Branch 9 → 11 taken 1467303 times.
1869712 min = (wh_ratio > 1) ? (60 - 2 * wh_ratio) : 60;
212
213
6/6
✓ Branch 12 → 13 taken 694147 times.
✓ Branch 12 → 16 taken 1175565 times.
✓ Branch 13 → 14 taken 414410 times.
✓ Branch 13 → 16 taken 279737 times.
✓ Branch 14 → 15 taken 69904 times.
✓ Branch 14 → 16 taken 344506 times.
1869712 if (nw > nh && pred_mode_intra >=2 && pred_mode_intra < max)
214 69904 pred_mode_intra += 65;
215
5/6
✓ Branch 16 → 17 taken 497573 times.
✓ Branch 16 → 20 taken 1302235 times.
✓ Branch 17 → 18 taken 497573 times.
✗ Branch 17 → 20 not taken.
✓ Branch 18 → 19 taken 23426 times.
✓ Branch 18 → 20 taken 474147 times.
1799808 else if (nh > nw && pred_mode_intra <= 66 && pred_mode_intra > min)
216 23426 pred_mode_intra -= 67;
217 1869712 return pred_mode_intra;
218 }
219