2 * VP9 compatible video decoder
4 * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5 * Copyright (C) 2013 Clément Bœsch <u pkh me>
7 * This file is part of FFmpeg.
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.
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.
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
29 static av_always_inline void adapt_prob(uint8_t *p, unsigned ct0, unsigned ct1,
30 int max_count, int update_factor)
32 unsigned ct = ct0 + ct1, p2, p1;
37 update_factor = FASTDIV(update_factor * FFMIN(ct, max_count), max_count);
39 p2 = ((((int64_t) ct0) << 8) + (ct >> 1)) / ct;
40 p2 = av_clip(p2, 1, 255);
42 // (p1 * (256 - update_factor) + p2 * update_factor + 128) >> 8
43 *p = p1 + (((p2 - p1) * update_factor + 128) >> 8);
46 void ff_vp9_adapt_probs(VP9Context *s)
49 ProbContext *p = &s->prob_ctx[s->s.h.framectxid].p;
50 int uf = (s->s.h.keyframe || s->s.h.intraonly || !s->last_keyframe) ? 112 : 128;
53 for (i = 0; i < 4; i++)
54 for (j = 0; j < 2; j++)
55 for (k = 0; k < 2; k++)
56 for (l = 0; l < 6; l++)
57 for (m = 0; m < 6; m++) {
58 uint8_t *pp = s->prob_ctx[s->s.h.framectxid].coef[i][j][k][l][m];
59 unsigned *e = s->td[0].counts.eob[i][j][k][l][m];
60 unsigned *c = s->td[0].counts.coef[i][j][k][l][m];
62 if (l == 0 && m >= 3) // dc only has 3 pt
65 adapt_prob(&pp[0], e[0], e[1], 24, uf);
66 adapt_prob(&pp[1], c[0], c[1] + c[2], 24, uf);
67 adapt_prob(&pp[2], c[1], c[2], 24, uf);
70 if (s->s.h.keyframe || s->s.h.intraonly) {
71 memcpy(p->skip, s->prob.p.skip, sizeof(p->skip));
72 memcpy(p->tx32p, s->prob.p.tx32p, sizeof(p->tx32p));
73 memcpy(p->tx16p, s->prob.p.tx16p, sizeof(p->tx16p));
74 memcpy(p->tx8p, s->prob.p.tx8p, sizeof(p->tx8p));
79 for (i = 0; i < 3; i++)
80 adapt_prob(&p->skip[i], s->td[0].counts.skip[i][0],
81 s->td[0].counts.skip[i][1], 20, 128);
84 for (i = 0; i < 4; i++)
85 adapt_prob(&p->intra[i], s->td[0].counts.intra[i][0],
86 s->td[0].counts.intra[i][1], 20, 128);
89 if (s->s.h.comppredmode == PRED_SWITCHABLE) {
90 for (i = 0; i < 5; i++)
91 adapt_prob(&p->comp[i], s->td[0].counts.comp[i][0],
92 s->td[0].counts.comp[i][1], 20, 128);
96 if (s->s.h.comppredmode != PRED_SINGLEREF) {
97 for (i = 0; i < 5; i++)
98 adapt_prob(&p->comp_ref[i], s->td[0].counts.comp_ref[i][0],
99 s->td[0].counts.comp_ref[i][1], 20, 128);
102 if (s->s.h.comppredmode != PRED_COMPREF) {
103 for (i = 0; i < 5; i++) {
104 uint8_t *pp = p->single_ref[i];
105 unsigned (*c)[2] = s->td[0].counts.single_ref[i];
107 adapt_prob(&pp[0], c[0][0], c[0][1], 20, 128);
108 adapt_prob(&pp[1], c[1][0], c[1][1], 20, 128);
112 // block partitioning
113 for (i = 0; i < 4; i++)
114 for (j = 0; j < 4; j++) {
115 uint8_t *pp = p->partition[i][j];
116 unsigned *c = s->td[0].counts.partition[i][j];
118 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
119 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
120 adapt_prob(&pp[2], c[2], c[3], 20, 128);
124 if (s->s.h.txfmmode == TX_SWITCHABLE) {
125 for (i = 0; i < 2; i++) {
126 unsigned *c16 = s->td[0].counts.tx16p[i], *c32 = s->td[0].counts.tx32p[i];
128 adapt_prob(&p->tx8p[i], s->td[0].counts.tx8p[i][0],
129 s->td[0].counts.tx8p[i][1], 20, 128);
130 adapt_prob(&p->tx16p[i][0], c16[0], c16[1] + c16[2], 20, 128);
131 adapt_prob(&p->tx16p[i][1], c16[1], c16[2], 20, 128);
132 adapt_prob(&p->tx32p[i][0], c32[0], c32[1] + c32[2] + c32[3], 20, 128);
133 adapt_prob(&p->tx32p[i][1], c32[1], c32[2] + c32[3], 20, 128);
134 adapt_prob(&p->tx32p[i][2], c32[2], c32[3], 20, 128);
138 // interpolation filter
139 if (s->s.h.filtermode == FILTER_SWITCHABLE) {
140 for (i = 0; i < 4; i++) {
141 uint8_t *pp = p->filter[i];
142 unsigned *c = s->td[0].counts.filter[i];
144 adapt_prob(&pp[0], c[0], c[1] + c[2], 20, 128);
145 adapt_prob(&pp[1], c[1], c[2], 20, 128);
150 for (i = 0; i < 7; i++) {
151 uint8_t *pp = p->mv_mode[i];
152 unsigned *c = s->td[0].counts.mv_mode[i];
154 adapt_prob(&pp[0], c[2], c[1] + c[0] + c[3], 20, 128);
155 adapt_prob(&pp[1], c[0], c[1] + c[3], 20, 128);
156 adapt_prob(&pp[2], c[1], c[3], 20, 128);
161 uint8_t *pp = p->mv_joint;
162 unsigned *c = s->td[0].counts.mv_joint;
164 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
165 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
166 adapt_prob(&pp[2], c[2], c[3], 20, 128);
170 for (i = 0; i < 2; i++) {
172 unsigned *c, (*c2)[2], sum;
174 adapt_prob(&p->mv_comp[i].sign, s->td[0].counts.mv_comp[i].sign[0],
175 s->td[0].counts.mv_comp[i].sign[1], 20, 128);
177 pp = p->mv_comp[i].classes;
178 c = s->td[0].counts.mv_comp[i].classes;
179 sum = c[1] + c[2] + c[3] + c[4] + c[5] +
180 c[6] + c[7] + c[8] + c[9] + c[10];
181 adapt_prob(&pp[0], c[0], sum, 20, 128);
183 adapt_prob(&pp[1], c[1], sum, 20, 128);
185 adapt_prob(&pp[2], c[2] + c[3], sum, 20, 128);
186 adapt_prob(&pp[3], c[2], c[3], 20, 128);
188 adapt_prob(&pp[4], c[4] + c[5], sum, 20, 128);
189 adapt_prob(&pp[5], c[4], c[5], 20, 128);
191 adapt_prob(&pp[6], c[6], sum, 20, 128);
192 adapt_prob(&pp[7], c[7] + c[8], c[9] + c[10], 20, 128);
193 adapt_prob(&pp[8], c[7], c[8], 20, 128);
194 adapt_prob(&pp[9], c[9], c[10], 20, 128);
196 adapt_prob(&p->mv_comp[i].class0, s->td[0].counts.mv_comp[i].class0[0],
197 s->td[0].counts.mv_comp[i].class0[1], 20, 128);
198 pp = p->mv_comp[i].bits;
199 c2 = s->td[0].counts.mv_comp[i].bits;
200 for (j = 0; j < 10; j++)
201 adapt_prob(&pp[j], c2[j][0], c2[j][1], 20, 128);
203 for (j = 0; j < 2; j++) {
204 pp = p->mv_comp[i].class0_fp[j];
205 c = s->td[0].counts.mv_comp[i].class0_fp[j];
206 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
207 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
208 adapt_prob(&pp[2], c[2], c[3], 20, 128);
210 pp = p->mv_comp[i].fp;
211 c = s->td[0].counts.mv_comp[i].fp;
212 adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
213 adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
214 adapt_prob(&pp[2], c[2], c[3], 20, 128);
216 if (s->s.h.highprecisionmvs) {
217 adapt_prob(&p->mv_comp[i].class0_hp,
218 s->td[0].counts.mv_comp[i].class0_hp[0],
219 s->td[0].counts.mv_comp[i].class0_hp[1], 20, 128);
220 adapt_prob(&p->mv_comp[i].hp, s->td[0].counts.mv_comp[i].hp[0],
221 s->td[0].counts.mv_comp[i].hp[1], 20, 128);
226 for (i = 0; i < 4; i++) {
227 uint8_t *pp = p->y_mode[i];
228 unsigned *c = s->td[0].counts.y_mode[i], sum, s2;
230 sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
231 adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
232 sum -= c[TM_VP8_PRED];
233 adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
235 adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
236 s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
238 adapt_prob(&pp[3], s2, sum, 20, 128);
240 adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
241 adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED],
243 sum -= c[DIAG_DOWN_LEFT_PRED];
244 adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
245 sum -= c[VERT_LEFT_PRED];
246 adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
247 adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
251 for (i = 0; i < 10; i++) {
252 uint8_t *pp = p->uv_mode[i];
253 unsigned *c = s->td[0].counts.uv_mode[i], sum, s2;
255 sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
256 adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
257 sum -= c[TM_VP8_PRED];
258 adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
260 adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
261 s2 = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
263 adapt_prob(&pp[3], s2, sum, 20, 128);
265 adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
266 adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED],
268 sum -= c[DIAG_DOWN_LEFT_PRED];
269 adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
270 sum -= c[VERT_LEFT_PRED];
271 adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
272 adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);