FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vp9prob.c
Date: 2026-10-29 23:19:09
Exec Total Coverage
Lines: 169 169 100.0%
Functions: 2 2 100.0%
Branches: 68 68 100.0%

Line Branch Exec Source
1 /*
2 * VP9 compatible video decoder
3 *
4 * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5 * Copyright (C) 2013 Clément Bœsch <u pkh me>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "vp9.h"
25 #include "vp9dec.h"
26
27 4104451 static av_always_inline void adapt_prob(uint8_t *p, unsigned ct0, unsigned ct1,
28 int max_count, int update_factor)
29 {
30 4104451 unsigned ct = ct0 + ct1, p2, p1;
31
32
2/2
✓ Branch 0 taken 3378856 times.
✓ Branch 1 taken 725595 times.
4104451 if (!ct)
33 3378856 return;
34
35 725595 update_factor = FASTDIV(update_factor * FFMIN(ct, max_count), max_count);
36 725595 p1 = *p;
37 725595 p2 = ((((int64_t) ct0) << 8) + (ct >> 1)) / ct;
38 725595 p2 = av_clip(p2, 1, 255);
39
40 // (p1 * (256 - update_factor) + p2 * update_factor + 128) >> 8
41 725595 *p = p1 + (((p2 - p1) * update_factor + 128) >> 8);
42 }
43
44 2257 void ff_vp9_adapt_probs(VP9Context *s)
45 {
46 int i, j, k, l, m;
47 2257 ProbContext *p = &s->prob_ctx[s->s.h.framectxid].p;
48
6/6
✓ Branch 0 taken 1791 times.
✓ Branch 1 taken 466 times.
✓ Branch 2 taken 1785 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1561 times.
✓ Branch 5 taken 224 times.
2257 int uf = (s->s.h.keyframe || s->s.h.intraonly || !s->last_keyframe) ? 112 : 128;
49
50 // coefficients
51
2/2
✓ Branch 0 taken 9028 times.
✓ Branch 1 taken 2257 times.
11285 for (i = 0; i < 4; i++)
52
2/2
✓ Branch 0 taken 18056 times.
✓ Branch 1 taken 9028 times.
27084 for (j = 0; j < 2; j++)
53
2/2
✓ Branch 0 taken 36112 times.
✓ Branch 1 taken 18056 times.
54168 for (k = 0; k < 2; k++)
54
2/2
✓ Branch 0 taken 216672 times.
✓ Branch 1 taken 36112 times.
252784 for (l = 0; l < 6; l++)
55
2/2
✓ Branch 0 taken 1227808 times.
✓ Branch 1 taken 180560 times.
1408368 for (m = 0; m < 6; m++) {
56 1227808 uint8_t *pp = s->prob_ctx[s->s.h.framectxid].coef[i][j][k][l][m];
57 1227808 unsigned *e = s->td[0].counts.eob[i][j][k][l][m];
58 1227808 unsigned *c = s->td[0].counts.coef[i][j][k][l][m];
59
60
4/4
✓ Branch 0 taken 144448 times.
✓ Branch 1 taken 1083360 times.
✓ Branch 2 taken 36112 times.
✓ Branch 3 taken 108336 times.
1227808 if (l == 0 && m >= 3) // dc only has 3 pt
61 36112 break;
62
63 1191696 adapt_prob(&pp[0], e[0], e[1], 24, uf);
64 1191696 adapt_prob(&pp[1], c[0], c[1] + c[2], 24, uf);
65 1191696 adapt_prob(&pp[2], c[1], c[2], 24, uf);
66 }
67
68
4/4
✓ Branch 0 taken 1791 times.
✓ Branch 1 taken 466 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1785 times.
2257 if (s->s.h.keyframe || s->s.h.intraonly) {
69 472 memcpy(p->skip, s->prob.p.skip, sizeof(p->skip));
70 472 memcpy(p->tx32p, s->prob.p.tx32p, sizeof(p->tx32p));
71 472 memcpy(p->tx16p, s->prob.p.tx16p, sizeof(p->tx16p));
72 472 memcpy(p->tx8p, s->prob.p.tx8p, sizeof(p->tx8p));
73 472 return;
74 }
75
76 // skip flag
77
2/2
✓ Branch 0 taken 5355 times.
✓ Branch 1 taken 1785 times.
7140 for (i = 0; i < 3; i++)
78 5355 adapt_prob(&p->skip[i], s->td[0].counts.skip[i][0],
79 5355 s->td[0].counts.skip[i][1], 20, 128);
80
81 // intra/inter flag
82
2/2
✓ Branch 0 taken 7140 times.
✓ Branch 1 taken 1785 times.
8925 for (i = 0; i < 4; i++)
83 7140 adapt_prob(&p->intra[i], s->td[0].counts.intra[i][0],
84 7140 s->td[0].counts.intra[i][1], 20, 128);
85
86 // comppred flag
87
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 1477 times.
1785 if (s->s.h.comppredmode == PRED_SWITCHABLE) {
88
2/2
✓ Branch 0 taken 1540 times.
✓ Branch 1 taken 308 times.
1848 for (i = 0; i < 5; i++)
89 1540 adapt_prob(&p->comp[i], s->td[0].counts.comp[i][0],
90 1540 s->td[0].counts.comp[i][1], 20, 128);
91 }
92
93 // reference frames
94
2/2
✓ Branch 0 taken 310 times.
✓ Branch 1 taken 1475 times.
1785 if (s->s.h.comppredmode != PRED_SINGLEREF) {
95
2/2
✓ Branch 0 taken 1550 times.
✓ Branch 1 taken 310 times.
1860 for (i = 0; i < 5; i++)
96 1550 adapt_prob(&p->comp_ref[i], s->td[0].counts.comp_ref[i][0],
97 1550 s->td[0].counts.comp_ref[i][1], 20, 128);
98 }
99
100
2/2
✓ Branch 0 taken 1783 times.
✓ Branch 1 taken 2 times.
1785 if (s->s.h.comppredmode != PRED_COMPREF) {
101
2/2
✓ Branch 0 taken 8915 times.
✓ Branch 1 taken 1783 times.
10698 for (i = 0; i < 5; i++) {
102 8915 uint8_t *pp = p->single_ref[i];
103 8915 unsigned (*c)[2] = s->td[0].counts.single_ref[i];
104
105 8915 adapt_prob(&pp[0], c[0][0], c[0][1], 20, 128);
106 8915 adapt_prob(&pp[1], c[1][0], c[1][1], 20, 128);
107 }
108 }
109
110 // block partitioning
111
2/2
✓ Branch 0 taken 7140 times.
✓ Branch 1 taken 1785 times.
8925 for (i = 0; i < 4; i++)
112
2/2
✓ Branch 0 taken 28560 times.
✓ Branch 1 taken 7140 times.
35700 for (j = 0; j < 4; j++) {
113 28560 uint8_t *pp = p->partition[i][j];
114 28560 unsigned *c = s->td[0].counts.partition[i][j];
115
116 28560 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
117 28560 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
118 28560 adapt_prob(&pp[2], c[2], c[3], 20, 128);
119 }
120
121 // tx size
122
2/2
✓ Branch 0 taken 1306 times.
✓ Branch 1 taken 479 times.
1785 if (s->s.h.txfmmode == TX_SWITCHABLE) {
123
2/2
✓ Branch 0 taken 2612 times.
✓ Branch 1 taken 1306 times.
3918 for (i = 0; i < 2; i++) {
124 2612 unsigned *c16 = s->td[0].counts.tx16p[i], *c32 = s->td[0].counts.tx32p[i];
125
126 2612 adapt_prob(&p->tx8p[i], s->td[0].counts.tx8p[i][0],
127 2612 s->td[0].counts.tx8p[i][1], 20, 128);
128 2612 adapt_prob(&p->tx16p[i][0], c16[0], c16[1] + c16[2], 20, 128);
129 2612 adapt_prob(&p->tx16p[i][1], c16[1], c16[2], 20, 128);
130 2612 adapt_prob(&p->tx32p[i][0], c32[0], c32[1] + c32[2] + c32[3], 20, 128);
131 2612 adapt_prob(&p->tx32p[i][1], c32[1], c32[2] + c32[3], 20, 128);
132 2612 adapt_prob(&p->tx32p[i][2], c32[2], c32[3], 20, 128);
133 }
134 }
135
136 // interpolation filter
137
2/2
✓ Branch 0 taken 1316 times.
✓ Branch 1 taken 469 times.
1785 if (s->s.h.filtermode == FILTER_SWITCHABLE) {
138
2/2
✓ Branch 0 taken 5264 times.
✓ Branch 1 taken 1316 times.
6580 for (i = 0; i < 4; i++) {
139 5264 uint8_t *pp = p->filter[i];
140 5264 unsigned *c = s->td[0].counts.filter[i];
141
142 5264 adapt_prob(&pp[0], c[0], c[1] + c[2], 20, 128);
143 5264 adapt_prob(&pp[1], c[1], c[2], 20, 128);
144 }
145 }
146
147 // inter modes
148
2/2
✓ Branch 0 taken 12495 times.
✓ Branch 1 taken 1785 times.
14280 for (i = 0; i < 7; i++) {
149 12495 uint8_t *pp = p->mv_mode[i];
150 12495 unsigned *c = s->td[0].counts.mv_mode[i];
151
152 12495 adapt_prob(&pp[0], c[2], c[1] + c[0] + c[3], 20, 128);
153 12495 adapt_prob(&pp[1], c[0], c[1] + c[3], 20, 128);
154 12495 adapt_prob(&pp[2], c[1], c[3], 20, 128);
155 }
156
157 // mv joints
158 {
159 1785 uint8_t *pp = p->mv_joint;
160 1785 unsigned *c = s->td[0].counts.mv_joint;
161
162 1785 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
163 1785 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
164 1785 adapt_prob(&pp[2], c[2], c[3], 20, 128);
165 }
166
167 // mv components
168
2/2
✓ Branch 0 taken 3570 times.
✓ Branch 1 taken 1785 times.
5355 for (i = 0; i < 2; i++) {
169 uint8_t *pp;
170 unsigned *c, (*c2)[2], sum;
171
172 3570 adapt_prob(&p->mv_comp[i].sign, s->td[0].counts.mv_comp[i].sign[0],
173 3570 s->td[0].counts.mv_comp[i].sign[1], 20, 128);
174
175 3570 pp = p->mv_comp[i].classes;
176 3570 c = s->td[0].counts.mv_comp[i].classes;
177 3570 sum = c[1] + c[2] + c[3] + c[4] + c[5] +
178 3570 c[6] + c[7] + c[8] + c[9] + c[10];
179 3570 adapt_prob(&pp[0], c[0], sum, 20, 128);
180 3570 sum -= c[1];
181 3570 adapt_prob(&pp[1], c[1], sum, 20, 128);
182 3570 sum -= c[2] + c[3];
183 3570 adapt_prob(&pp[2], c[2] + c[3], sum, 20, 128);
184 3570 adapt_prob(&pp[3], c[2], c[3], 20, 128);
185 3570 sum -= c[4] + c[5];
186 3570 adapt_prob(&pp[4], c[4] + c[5], sum, 20, 128);
187 3570 adapt_prob(&pp[5], c[4], c[5], 20, 128);
188 3570 sum -= c[6];
189 3570 adapt_prob(&pp[6], c[6], sum, 20, 128);
190 3570 adapt_prob(&pp[7], c[7] + c[8], c[9] + c[10], 20, 128);
191 3570 adapt_prob(&pp[8], c[7], c[8], 20, 128);
192 3570 adapt_prob(&pp[9], c[9], c[10], 20, 128);
193
194 3570 adapt_prob(&p->mv_comp[i].class0, s->td[0].counts.mv_comp[i].class0[0],
195 3570 s->td[0].counts.mv_comp[i].class0[1], 20, 128);
196 3570 pp = p->mv_comp[i].bits;
197 3570 c2 = s->td[0].counts.mv_comp[i].bits;
198
2/2
✓ Branch 0 taken 35700 times.
✓ Branch 1 taken 3570 times.
39270 for (j = 0; j < 10; j++)
199 35700 adapt_prob(&pp[j], c2[j][0], c2[j][1], 20, 128);
200
201
2/2
✓ Branch 0 taken 7140 times.
✓ Branch 1 taken 3570 times.
10710 for (j = 0; j < 2; j++) {
202 7140 pp = p->mv_comp[i].class0_fp[j];
203 7140 c = s->td[0].counts.mv_comp[i].class0_fp[j];
204 7140 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
205 7140 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
206 7140 adapt_prob(&pp[2], c[2], c[3], 20, 128);
207 }
208 3570 pp = p->mv_comp[i].fp;
209 3570 c = s->td[0].counts.mv_comp[i].fp;
210 3570 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
211 3570 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
212 3570 adapt_prob(&pp[2], c[2], c[3], 20, 128);
213
214
2/2
✓ Branch 0 taken 2824 times.
✓ Branch 1 taken 746 times.
3570 if (s->s.h.highprecisionmvs) {
215 2824 adapt_prob(&p->mv_comp[i].class0_hp,
216 2824 s->td[0].counts.mv_comp[i].class0_hp[0],
217 2824 s->td[0].counts.mv_comp[i].class0_hp[1], 20, 128);
218 2824 adapt_prob(&p->mv_comp[i].hp, s->td[0].counts.mv_comp[i].hp[0],
219 2824 s->td[0].counts.mv_comp[i].hp[1], 20, 128);
220 }
221 }
222
223 // y intra modes
224
2/2
✓ Branch 0 taken 7140 times.
✓ Branch 1 taken 1785 times.
8925 for (i = 0; i < 4; i++) {
225 7140 uint8_t *pp = p->y_mode[i];
226 7140 unsigned *c = s->td[0].counts.y_mode[i], sum, s2;
227
228 7140 sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
229 7140 adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
230 7140 sum -= c[TM_VP8_PRED];
231 7140 adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
232 7140 sum -= c[VERT_PRED];
233 7140 adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
234 7140 s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
235 7140 sum -= s2;
236 7140 adapt_prob(&pp[3], s2, sum, 20, 128);
237 7140 s2 -= c[HOR_PRED];
238 7140 adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
239 7140 adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED],
240 20, 128);
241 7140 sum -= c[DIAG_DOWN_LEFT_PRED];
242 7140 adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
243 7140 sum -= c[VERT_LEFT_PRED];
244 7140 adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
245 7140 adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
246 }
247
248 // uv intra modes
249
2/2
✓ Branch 0 taken 17850 times.
✓ Branch 1 taken 1785 times.
19635 for (i = 0; i < 10; i++) {
250 17850 uint8_t *pp = p->uv_mode[i];
251 17850 unsigned *c = s->td[0].counts.uv_mode[i], sum, s2;
252
253 17850 sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
254 17850 adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
255 17850 sum -= c[TM_VP8_PRED];
256 17850 adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
257 17850 sum -= c[VERT_PRED];
258 17850 adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
259 17850 s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
260 17850 sum -= s2;
261 17850 adapt_prob(&pp[3], s2, sum, 20, 128);
262 17850 s2 -= c[HOR_PRED];
263 17850 adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
264 17850 adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED],
265 20, 128);
266 17850 sum -= c[DIAG_DOWN_LEFT_PRED];
267 17850 adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
268 17850 sum -= c[VERT_LEFT_PRED];
269 17850 adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
270 17850 adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
271 }
272 }
273