]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp9prob.c
dirac: make initialization of arithmetic coder tables threadsafe.
[ffmpeg] / libavcodec / vp9prob.c
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 "vp56.h"
25 #include "vp9.h"
26 #include "vp9data.h"
27
28 static av_always_inline void adapt_prob(uint8_t *p, unsigned ct0, unsigned ct1,
29                                         int max_count, int update_factor)
30 {
31     unsigned ct = ct0 + ct1, p2, p1;
32
33     if (!ct)
34         return;
35
36     update_factor = FASTDIV(update_factor * FFMIN(ct, max_count), max_count);
37     p1 = *p;
38     p2 = ((((int64_t) ct0) << 8) + (ct >> 1)) / ct;
39     p2 = av_clip(p2, 1, 255);
40
41     // (p1 * (256 - update_factor) + p2 * update_factor + 128) >> 8
42     *p = p1 + (((p2 - p1) * update_factor + 128) >> 8);
43 }
44
45 void ff_vp9_adapt_probs(VP9Context *s)
46 {
47     int i, j, k, l, m;
48     ProbContext *p = &s->prob_ctx[s->s.h.framectxid].p;
49     int uf = (s->s.h.keyframe || s->s.h.intraonly || !s->last_keyframe) ? 112 : 128;
50
51     // coefficients
52     for (i = 0; i < 4; i++)
53         for (j = 0; j < 2; j++)
54             for (k = 0; k < 2; k++)
55                 for (l = 0; l < 6; l++)
56                     for (m = 0; m < 6; m++) {
57                         uint8_t *pp = s->prob_ctx[s->s.h.framectxid].coef[i][j][k][l][m];
58                         unsigned *e = s->counts.eob[i][j][k][l][m];
59                         unsigned *c = s->counts.coef[i][j][k][l][m];
60
61                         if (l == 0 && m >= 3) // dc only has 3 pt
62                             break;
63
64                         adapt_prob(&pp[0], e[0], e[1], 24, uf);
65                         adapt_prob(&pp[1], c[0], c[1] + c[2], 24, uf);
66                         adapt_prob(&pp[2], c[1], c[2], 24, uf);
67                     }
68
69     if (s->s.h.keyframe || s->s.h.intraonly) {
70         memcpy(p->skip,  s->prob.p.skip,  sizeof(p->skip));
71         memcpy(p->tx32p, s->prob.p.tx32p, sizeof(p->tx32p));
72         memcpy(p->tx16p, s->prob.p.tx16p, sizeof(p->tx16p));
73         memcpy(p->tx8p,  s->prob.p.tx8p,  sizeof(p->tx8p));
74         return;
75     }
76
77     // skip flag
78     for (i = 0; i < 3; i++)
79         adapt_prob(&p->skip[i], s->counts.skip[i][0],
80                    s->counts.skip[i][1], 20, 128);
81
82     // intra/inter flag
83     for (i = 0; i < 4; i++)
84         adapt_prob(&p->intra[i], s->counts.intra[i][0],
85                    s->counts.intra[i][1], 20, 128);
86
87     // comppred flag
88     if (s->s.h.comppredmode == PRED_SWITCHABLE) {
89         for (i = 0; i < 5; i++)
90             adapt_prob(&p->comp[i], s->counts.comp[i][0],
91                        s->counts.comp[i][1], 20, 128);
92     }
93
94     // reference frames
95     if (s->s.h.comppredmode != PRED_SINGLEREF) {
96         for (i = 0; i < 5; i++)
97             adapt_prob(&p->comp_ref[i], s->counts.comp_ref[i][0],
98                        s->counts.comp_ref[i][1], 20, 128);
99     }
100
101     if (s->s.h.comppredmode != PRED_COMPREF) {
102         for (i = 0; i < 5; i++) {
103             uint8_t *pp = p->single_ref[i];
104             unsigned (*c)[2] = s->counts.single_ref[i];
105
106             adapt_prob(&pp[0], c[0][0], c[0][1], 20, 128);
107             adapt_prob(&pp[1], c[1][0], c[1][1], 20, 128);
108         }
109     }
110
111     // block partitioning
112     for (i = 0; i < 4; i++)
113         for (j = 0; j < 4; j++) {
114             uint8_t *pp = p->partition[i][j];
115             unsigned *c = s->counts.partition[i][j];
116
117             adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
118             adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
119             adapt_prob(&pp[2], c[2], c[3], 20, 128);
120         }
121
122     // tx size
123     if (s->s.h.txfmmode == TX_SWITCHABLE) {
124         for (i = 0; i < 2; i++) {
125             unsigned *c16 = s->counts.tx16p[i], *c32 = s->counts.tx32p[i];
126
127             adapt_prob(&p->tx8p[i], s->counts.tx8p[i][0],
128                        s->counts.tx8p[i][1], 20, 128);
129             adapt_prob(&p->tx16p[i][0], c16[0], c16[1] + c16[2], 20, 128);
130             adapt_prob(&p->tx16p[i][1], c16[1], c16[2], 20, 128);
131             adapt_prob(&p->tx32p[i][0], c32[0], c32[1] + c32[2] + c32[3], 20, 128);
132             adapt_prob(&p->tx32p[i][1], c32[1], c32[2] + c32[3], 20, 128);
133             adapt_prob(&p->tx32p[i][2], c32[2], c32[3], 20, 128);
134         }
135     }
136
137     // interpolation filter
138     if (s->s.h.filtermode == FILTER_SWITCHABLE) {
139         for (i = 0; i < 4; i++) {
140             uint8_t *pp = p->filter[i];
141             unsigned *c = s->counts.filter[i];
142
143             adapt_prob(&pp[0], c[0], c[1] + c[2], 20, 128);
144             adapt_prob(&pp[1], c[1], c[2], 20, 128);
145         }
146     }
147
148     // inter modes
149     for (i = 0; i < 7; i++) {
150         uint8_t *pp = p->mv_mode[i];
151         unsigned *c = s->counts.mv_mode[i];
152
153         adapt_prob(&pp[0], c[2], c[1] + c[0] + c[3], 20, 128);
154         adapt_prob(&pp[1], c[0], c[1] + c[3], 20, 128);
155         adapt_prob(&pp[2], c[1], c[3], 20, 128);
156     }
157
158     // mv joints
159     {
160         uint8_t *pp = p->mv_joint;
161         unsigned *c = s->counts.mv_joint;
162
163         adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
164         adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
165         adapt_prob(&pp[2], c[2], c[3], 20, 128);
166     }
167
168     // mv components
169     for (i = 0; i < 2; i++) {
170         uint8_t *pp;
171         unsigned *c, (*c2)[2], sum;
172
173         adapt_prob(&p->mv_comp[i].sign, s->counts.mv_comp[i].sign[0],
174                    s->counts.mv_comp[i].sign[1], 20, 128);
175
176         pp  = p->mv_comp[i].classes;
177         c   = s->counts.mv_comp[i].classes;
178         sum = c[1] + c[2] + c[3] + c[4] + c[5] +
179               c[6] + c[7] + c[8] + c[9] + c[10];
180         adapt_prob(&pp[0], c[0], sum, 20, 128);
181         sum -= c[1];
182         adapt_prob(&pp[1], c[1], sum, 20, 128);
183         sum -= c[2] + c[3];
184         adapt_prob(&pp[2], c[2] + c[3], sum, 20, 128);
185         adapt_prob(&pp[3], c[2], c[3], 20, 128);
186         sum -= c[4] + c[5];
187         adapt_prob(&pp[4], c[4] + c[5], sum, 20, 128);
188         adapt_prob(&pp[5], c[4], c[5], 20, 128);
189         sum -= c[6];
190         adapt_prob(&pp[6], c[6], sum, 20, 128);
191         adapt_prob(&pp[7], c[7] + c[8], c[9] + c[10], 20, 128);
192         adapt_prob(&pp[8], c[7], c[8], 20, 128);
193         adapt_prob(&pp[9], c[9], c[10], 20, 128);
194
195         adapt_prob(&p->mv_comp[i].class0, s->counts.mv_comp[i].class0[0],
196                    s->counts.mv_comp[i].class0[1], 20, 128);
197         pp = p->mv_comp[i].bits;
198         c2 = s->counts.mv_comp[i].bits;
199         for (j = 0; j < 10; j++)
200             adapt_prob(&pp[j], c2[j][0], c2[j][1], 20, 128);
201
202         for (j = 0; j < 2; j++) {
203             pp = p->mv_comp[i].class0_fp[j];
204             c  = s->counts.mv_comp[i].class0_fp[j];
205             adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
206             adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
207             adapt_prob(&pp[2], c[2], c[3], 20, 128);
208         }
209         pp = p->mv_comp[i].fp;
210         c  = s->counts.mv_comp[i].fp;
211         adapt_prob(&pp[0], c[0], c[1] + c[2] + c[3], 20, 128);
212         adapt_prob(&pp[1], c[1], c[2] + c[3], 20, 128);
213         adapt_prob(&pp[2], c[2], c[3], 20, 128);
214
215         if (s->s.h.highprecisionmvs) {
216             adapt_prob(&p->mv_comp[i].class0_hp,
217                        s->counts.mv_comp[i].class0_hp[0],
218                        s->counts.mv_comp[i].class0_hp[1], 20, 128);
219             adapt_prob(&p->mv_comp[i].hp, s->counts.mv_comp[i].hp[0],
220                        s->counts.mv_comp[i].hp[1], 20, 128);
221         }
222     }
223
224     // y intra modes
225     for (i = 0; i < 4; i++) {
226         uint8_t *pp = p->y_mode[i];
227         unsigned *c = s->counts.y_mode[i], sum, s2;
228
229         sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
230         adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
231         sum -= c[TM_VP8_PRED];
232         adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
233         sum -= c[VERT_PRED];
234         adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
235         s2   = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
236         sum -= s2;
237         adapt_prob(&pp[3], s2, sum, 20, 128);
238         s2 -= c[HOR_PRED];
239         adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
240         adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED],
241                    20, 128);
242         sum -= c[DIAG_DOWN_LEFT_PRED];
243         adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
244         sum -= c[VERT_LEFT_PRED];
245         adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
246         adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
247     }
248
249     // uv intra modes
250     for (i = 0; i < 10; i++) {
251         uint8_t *pp = p->uv_mode[i];
252         unsigned *c = s->counts.uv_mode[i], sum, s2;
253
254         sum = c[0] + c[1] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
255         adapt_prob(&pp[0], c[DC_PRED], sum, 20, 128);
256         sum -= c[TM_VP8_PRED];
257         adapt_prob(&pp[1], c[TM_VP8_PRED], sum, 20, 128);
258         sum -= c[VERT_PRED];
259         adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
260         s2   = c[HOR_PRED] + c[DIAG_DOWN_RIGHT_PRED] + c[VERT_RIGHT_PRED];
261         sum -= s2;
262         adapt_prob(&pp[3], s2, sum, 20, 128);
263         s2 -= c[HOR_PRED];
264         adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
265         adapt_prob(&pp[5], c[DIAG_DOWN_RIGHT_PRED], c[VERT_RIGHT_PRED],
266                    20, 128);
267         sum -= c[DIAG_DOWN_LEFT_PRED];
268         adapt_prob(&pp[6], c[DIAG_DOWN_LEFT_PRED], sum, 20, 128);
269         sum -= c[VERT_LEFT_PRED];
270         adapt_prob(&pp[7], c[VERT_LEFT_PRED], sum, 20, 128);
271         adapt_prob(&pp[8], c[HOR_DOWN_PRED], c[HOR_UP_PRED], 20, 128);
272     }
273 }