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 | 4086299 | static av_always_inline void adapt_prob(uint8_t *p, unsigned ct0, unsigned ct1, | |
28 | int max_count, int update_factor) | ||
29 | { | ||
30 | 4086299 | unsigned ct = ct0 + ct1, p2, p1; | |
31 | |||
32 |
2/2✓ Branch 0 taken 3364629 times.
✓ Branch 1 taken 721670 times.
|
4086299 | if (!ct) |
33 | 3364629 | return; | |
34 | |||
35 | 721670 | update_factor = FASTDIV(update_factor * FFMIN(ct, max_count), max_count); | |
36 | 721670 | p1 = *p; | |
37 | 721670 | p2 = ((((int64_t) ct0) << 8) + (ct >> 1)) / ct; | |
38 | 721670 | p2 = av_clip(p2, 1, 255); | |
39 | |||
40 | // (p1 * (256 - update_factor) + p2 * update_factor + 128) >> 8 | ||
41 | 721670 | *p = p1 + (((p2 - p1) * update_factor + 128) >> 8); | |
42 | } | ||
43 | |||
44 | 2247 | void ff_vp9_adapt_probs(VP9Context *s) | |
45 | { | ||
46 | int i, j, k, l, m; | ||
47 | 2247 | ProbContext *p = &s->prob_ctx[s->s.h.framectxid].p; | |
48 |
6/6✓ Branch 0 taken 1783 times.
✓ Branch 1 taken 464 times.
✓ Branch 2 taken 1777 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1554 times.
✓ Branch 5 taken 223 times.
|
2247 | int uf = (s->s.h.keyframe || s->s.h.intraonly || !s->last_keyframe) ? 112 : 128; |
49 | |||
50 | // coefficients | ||
51 |
2/2✓ Branch 0 taken 8988 times.
✓ Branch 1 taken 2247 times.
|
11235 | for (i = 0; i < 4; i++) |
52 |
2/2✓ Branch 0 taken 17976 times.
✓ Branch 1 taken 8988 times.
|
26964 | for (j = 0; j < 2; j++) |
53 |
2/2✓ Branch 0 taken 35952 times.
✓ Branch 1 taken 17976 times.
|
53928 | for (k = 0; k < 2; k++) |
54 |
2/2✓ Branch 0 taken 215712 times.
✓ Branch 1 taken 35952 times.
|
251664 | for (l = 0; l < 6; l++) |
55 |
2/2✓ Branch 0 taken 1222368 times.
✓ Branch 1 taken 179760 times.
|
1402128 | for (m = 0; m < 6; m++) { |
56 | 1222368 | uint8_t *pp = s->prob_ctx[s->s.h.framectxid].coef[i][j][k][l][m]; | |
57 | 1222368 | unsigned *e = s->td[0].counts.eob[i][j][k][l][m]; | |
58 | 1222368 | unsigned *c = s->td[0].counts.coef[i][j][k][l][m]; | |
59 | |||
60 |
4/4✓ Branch 0 taken 143808 times.
✓ Branch 1 taken 1078560 times.
✓ Branch 2 taken 35952 times.
✓ Branch 3 taken 107856 times.
|
1222368 | if (l == 0 && m >= 3) // dc only has 3 pt |
61 | 35952 | break; | |
62 | |||
63 | 1186416 | adapt_prob(&pp[0], e[0], e[1], 24, uf); | |
64 | 1186416 | adapt_prob(&pp[1], c[0], c[1] + c[2], 24, uf); | |
65 | 1186416 | adapt_prob(&pp[2], c[1], c[2], 24, uf); | |
66 | } | ||
67 | |||
68 |
4/4✓ Branch 0 taken 1783 times.
✓ Branch 1 taken 464 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1777 times.
|
2247 | if (s->s.h.keyframe || s->s.h.intraonly) { |
69 | 470 | memcpy(p->skip, s->prob.p.skip, sizeof(p->skip)); | |
70 | 470 | memcpy(p->tx32p, s->prob.p.tx32p, sizeof(p->tx32p)); | |
71 | 470 | memcpy(p->tx16p, s->prob.p.tx16p, sizeof(p->tx16p)); | |
72 | 470 | memcpy(p->tx8p, s->prob.p.tx8p, sizeof(p->tx8p)); | |
73 | 470 | return; | |
74 | } | ||
75 | |||
76 | // skip flag | ||
77 |
2/2✓ Branch 0 taken 5331 times.
✓ Branch 1 taken 1777 times.
|
7108 | for (i = 0; i < 3; i++) |
78 | 5331 | adapt_prob(&p->skip[i], s->td[0].counts.skip[i][0], | |
79 | 5331 | s->td[0].counts.skip[i][1], 20, 128); | |
80 | |||
81 | // intra/inter flag | ||
82 |
2/2✓ Branch 0 taken 7108 times.
✓ Branch 1 taken 1777 times.
|
8885 | for (i = 0; i < 4; i++) |
83 | 7108 | adapt_prob(&p->intra[i], s->td[0].counts.intra[i][0], | |
84 | 7108 | s->td[0].counts.intra[i][1], 20, 128); | |
85 | |||
86 | // comppred flag | ||
87 |
2/2✓ Branch 0 taken 308 times.
✓ Branch 1 taken 1469 times.
|
1777 | 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 1467 times.
|
1777 | 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 1775 times.
✓ Branch 1 taken 2 times.
|
1777 | if (s->s.h.comppredmode != PRED_COMPREF) { |
101 |
2/2✓ Branch 0 taken 8875 times.
✓ Branch 1 taken 1775 times.
|
10650 | for (i = 0; i < 5; i++) { |
102 | 8875 | uint8_t *pp = p->single_ref[i]; | |
103 | 8875 | unsigned (*c)[2] = s->td[0].counts.single_ref[i]; | |
104 | |||
105 | 8875 | adapt_prob(&pp[0], c[0][0], c[0][1], 20, 128); | |
106 | 8875 | adapt_prob(&pp[1], c[1][0], c[1][1], 20, 128); | |
107 | } | ||
108 | } | ||
109 | |||
110 | // block partitioning | ||
111 |
2/2✓ Branch 0 taken 7108 times.
✓ Branch 1 taken 1777 times.
|
8885 | for (i = 0; i < 4; i++) |
112 |
2/2✓ Branch 0 taken 28432 times.
✓ Branch 1 taken 7108 times.
|
35540 | for (j = 0; j < 4; j++) { |
113 | 28432 | uint8_t *pp = p->partition[i][j]; | |
114 | 28432 | unsigned *c = s->td[0].counts.partition[i][j]; | |
115 | |||
116 | 28432 | adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128); | |
117 | 28432 | adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128); | |
118 | 28432 | 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 471 times.
|
1777 | 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 1308 times.
✓ Branch 1 taken 469 times.
|
1777 | if (s->s.h.filtermode == FILTER_SWITCHABLE) { |
138 |
2/2✓ Branch 0 taken 5232 times.
✓ Branch 1 taken 1308 times.
|
6540 | for (i = 0; i < 4; i++) { |
139 | 5232 | uint8_t *pp = p->filter[i]; | |
140 | 5232 | unsigned *c = s->td[0].counts.filter[i]; | |
141 | |||
142 | 5232 | adapt_prob(&pp[0], c[0], c[1] + c[2], 20, 128); | |
143 | 5232 | adapt_prob(&pp[1], c[1], c[2], 20, 128); | |
144 | } | ||
145 | } | ||
146 | |||
147 | // inter modes | ||
148 |
2/2✓ Branch 0 taken 12439 times.
✓ Branch 1 taken 1777 times.
|
14216 | for (i = 0; i < 7; i++) { |
149 | 12439 | uint8_t *pp = p->mv_mode[i]; | |
150 | 12439 | unsigned *c = s->td[0].counts.mv_mode[i]; | |
151 | |||
152 | 12439 | adapt_prob(&pp[0], c[2], c[1] + c[0] + c[3], 20, 128); | |
153 | 12439 | adapt_prob(&pp[1], c[0], c[1] + c[3], 20, 128); | |
154 | 12439 | adapt_prob(&pp[2], c[1], c[3], 20, 128); | |
155 | } | ||
156 | |||
157 | // mv joints | ||
158 | { | ||
159 | 1777 | uint8_t *pp = p->mv_joint; | |
160 | 1777 | unsigned *c = s->td[0].counts.mv_joint; | |
161 | |||
162 | 1777 | adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128); | |
163 | 1777 | adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128); | |
164 | 1777 | adapt_prob(&pp[2], c[2], c[3], 20, 128); | |
165 | } | ||
166 | |||
167 | // mv components | ||
168 |
2/2✓ Branch 0 taken 3554 times.
✓ Branch 1 taken 1777 times.
|
5331 | for (i = 0; i < 2; i++) { |
169 | uint8_t *pp; | ||
170 | unsigned *c, (*c2)[2], sum; | ||
171 | |||
172 | 3554 | adapt_prob(&p->mv_comp[i].sign, s->td[0].counts.mv_comp[i].sign[0], | |
173 | 3554 | s->td[0].counts.mv_comp[i].sign[1], 20, 128); | |
174 | |||
175 | 3554 | pp = p->mv_comp[i].classes; | |
176 | 3554 | c = s->td[0].counts.mv_comp[i].classes; | |
177 | 3554 | sum = c[1] + c[2] + c[3] + c[4] + c[5] + | |
178 | 3554 | c[6] + c[7] + c[8] + c[9] + c[10]; | |
179 | 3554 | adapt_prob(&pp[0], c[0], sum, 20, 128); | |
180 | 3554 | sum -= c[1]; | |
181 | 3554 | adapt_prob(&pp[1], c[1], sum, 20, 128); | |
182 | 3554 | sum -= c[2] + c[3]; | |
183 | 3554 | adapt_prob(&pp[2], c[2] + c[3], sum, 20, 128); | |
184 | 3554 | adapt_prob(&pp[3], c[2], c[3], 20, 128); | |
185 | 3554 | sum -= c[4] + c[5]; | |
186 | 3554 | adapt_prob(&pp[4], c[4] + c[5], sum, 20, 128); | |
187 | 3554 | adapt_prob(&pp[5], c[4], c[5], 20, 128); | |
188 | 3554 | sum -= c[6]; | |
189 | 3554 | adapt_prob(&pp[6], c[6], sum, 20, 128); | |
190 | 3554 | adapt_prob(&pp[7], c[7] + c[8], c[9] + c[10], 20, 128); | |
191 | 3554 | adapt_prob(&pp[8], c[7], c[8], 20, 128); | |
192 | 3554 | adapt_prob(&pp[9], c[9], c[10], 20, 128); | |
193 | |||
194 | 3554 | adapt_prob(&p->mv_comp[i].class0, s->td[0].counts.mv_comp[i].class0[0], | |
195 | 3554 | s->td[0].counts.mv_comp[i].class0[1], 20, 128); | |
196 | 3554 | pp = p->mv_comp[i].bits; | |
197 | 3554 | c2 = s->td[0].counts.mv_comp[i].bits; | |
198 |
2/2✓ Branch 0 taken 35540 times.
✓ Branch 1 taken 3554 times.
|
39094 | for (j = 0; j < 10; j++) |
199 | 35540 | adapt_prob(&pp[j], c2[j][0], c2[j][1], 20, 128); | |
200 | |||
201 |
2/2✓ Branch 0 taken 7108 times.
✓ Branch 1 taken 3554 times.
|
10662 | for (j = 0; j < 2; j++) { |
202 | 7108 | pp = p->mv_comp[i].class0_fp[j]; | |
203 | 7108 | c = s->td[0].counts.mv_comp[i].class0_fp[j]; | |
204 | 7108 | adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128); | |
205 | 7108 | adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128); | |
206 | 7108 | adapt_prob(&pp[2], c[2], c[3], 20, 128); | |
207 | } | ||
208 | 3554 | pp = p->mv_comp[i].fp; | |
209 | 3554 | c = s->td[0].counts.mv_comp[i].fp; | |
210 | 3554 | adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128); | |
211 | 3554 | adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128); | |
212 | 3554 | adapt_prob(&pp[2], c[2], c[3], 20, 128); | |
213 | |||
214 |
2/2✓ Branch 0 taken 2808 times.
✓ Branch 1 taken 746 times.
|
3554 | if (s->s.h.highprecisionmvs) { |
215 | 2808 | adapt_prob(&p->mv_comp[i].class0_hp, | |
216 | 2808 | s->td[0].counts.mv_comp[i].class0_hp[0], | |
217 | 2808 | s->td[0].counts.mv_comp[i].class0_hp[1], 20, 128); | |
218 | 2808 | adapt_prob(&p->mv_comp[i].hp, s->td[0].counts.mv_comp[i].hp[0], | |
219 | 2808 | s->td[0].counts.mv_comp[i].hp[1], 20, 128); | |
220 | } | ||
221 | } | ||
222 | |||
223 | // y intra modes | ||
224 |
2/2✓ Branch 0 taken 7108 times.
✓ Branch 1 taken 1777 times.
|
8885 | for (i = 0; i < 4; i++) { |
225 | 7108 | uint8_t *pp = p->y_mode[i]; | |
226 | 7108 | unsigned *c = s->td[0].counts.y_mode[i], sum, s2; | |
227 | |||
228 | 7108 | sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9]; | |
229 | 7108 | adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128); | |
230 | 7108 | sum -= c[TM_VP8_PRED]; | |
231 | 7108 | adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128); | |
232 | 7108 | sum -= c[VERT_PRED]; | |
233 | 7108 | adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128); | |
234 | 7108 | s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED]; | |
235 | 7108 | sum -= s2; | |
236 | 7108 | adapt_prob(&pp[3], s2, sum, 20, 128); | |
237 | 7108 | s2 -= c[HOR_PRED]; | |
238 | 7108 | adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128); | |
239 | 7108 | adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED], | |
240 | 20, 128); | ||
241 | 7108 | sum -= c[DIAG_DOWN_LEFT_PRED]; | |
242 | 7108 | adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128); | |
243 | 7108 | sum -= c[VERT_LEFT_PRED]; | |
244 | 7108 | adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128); | |
245 | 7108 | 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 17770 times.
✓ Branch 1 taken 1777 times.
|
19547 | for (i = 0; i < 10; i++) { |
250 | 17770 | uint8_t *pp = p->uv_mode[i]; | |
251 | 17770 | unsigned *c = s->td[0].counts.uv_mode[i], sum, s2; | |
252 | |||
253 | 17770 | sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9]; | |
254 | 17770 | adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128); | |
255 | 17770 | sum -= c[TM_VP8_PRED]; | |
256 | 17770 | adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128); | |
257 | 17770 | sum -= c[VERT_PRED]; | |
258 | 17770 | adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128); | |
259 | 17770 | s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED]; | |
260 | 17770 | sum -= s2; | |
261 | 17770 | adapt_prob(&pp[3], s2, sum, 20, 128); | |
262 | 17770 | s2 -= c[HOR_PRED]; | |
263 | 17770 | adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128); | |
264 | 17770 | adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED], | |
265 | 20, 128); | ||
266 | 17770 | sum -= c[DIAG_DOWN_LEFT_PRED]; | |
267 | 17770 | adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128); | |
268 | 17770 | sum -= c[VERT_LEFT_PRED]; | |
269 | 17770 | adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128); | |
270 | 17770 | adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128); | |
271 | } | ||
272 | } | ||
273 |