FFmpeg
vp9prob.c
Go to the documentation of this file.
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 #include "vp9dec.h"
28 
29 static av_always_inline void adapt_prob(uint8_t *p, unsigned ct0, unsigned ct1,
30  int max_count, int update_factor)
31 {
32  unsigned ct = ct0 + ct1, p2, p1;
33 
34  if (!ct)
35  return;
36 
37  update_factor = FASTDIV(update_factor * FFMIN(ct, max_count), max_count);
38  p1 = *p;
39  p2 = ((((int64_t) ct0) << 8) + (ct >> 1)) / ct;
40  p2 = av_clip(p2, 1, 255);
41 
42  // (p1 * (256 - update_factor) + p2 * update_factor + 128) >> 8
43  *p = p1 + (((p2 - p1) * update_factor + 128) >> 8);
44 }
45 
47 {
48  int i, j, k, l, m;
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;
51 
52  // coefficients
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];
61 
62  if (l == 0 && m >= 3) // dc only has 3 pt
63  break;
64 
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);
68  }
69 
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));
75  return;
76  }
77 
78  // skip flag
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);
82 
83  // intra/inter flag
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);
87 
88  // comppred flag
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);
93  }
94 
95  // reference frames
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);
100  }
101 
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];
106 
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);
109  }
110  }
111 
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];
117 
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);
121  }
122 
123  // tx size
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];
127 
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);
135  }
136  }
137 
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];
143 
144  adapt_prob(&pp[0], c[0], c[1] + c[2], 20, 128);
145  adapt_prob(&pp[1], c[1], c[2], 20, 128);
146  }
147  }
148 
149  // inter modes
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];
153 
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);
157  }
158 
159  // mv joints
160  {
161  uint8_t *pp = p->mv_joint;
162  unsigned *c = s->td[0].counts.mv_joint;
163 
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);
167  }
168 
169  // mv components
170  for (i = 0; i < 2; i++) {
171  uint8_t *pp;
172  unsigned *c, (*c2)[2], sum;
173 
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);
176 
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);
182  sum -= c[1];
183  adapt_prob(&pp[1], c[1], sum, 20, 128);
184  sum -= c[2] + c[3];
185  adapt_prob(&pp[2], c[2] + c[3], sum, 20, 128);
186  adapt_prob(&pp[3], c[2], c[3], 20, 128);
187  sum -= c[4] + c[5];
188  adapt_prob(&pp[4], c[4] + c[5], sum, 20, 128);
189  adapt_prob(&pp[5], c[4], c[5], 20, 128);
190  sum -= c[6];
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);
195 
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);
202 
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);
209  }
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);
215 
216  if (s->s.h.highprecisionmvs) {
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);
222  }
223  }
224 
225  // y intra modes
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;
229 
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);
234  sum -= c[VERT_PRED];
235  adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
237  sum -= s2;
238  adapt_prob(&pp[3], s2, sum, 20, 128);
239  s2 -= c[HOR_PRED];
240  adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
242  20, 128);
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);
248  }
249 
250  // uv intra modes
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;
254 
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);
259  sum -= c[VERT_PRED];
260  adapt_prob(&pp[2], c[VERT_PRED], sum, 20, 128);
262  sum -= s2;
263  adapt_prob(&pp[3], s2, sum, 20, 128);
264  s2 -= c[HOR_PRED];
265  adapt_prob(&pp[4], c[HOR_PRED], s2, 20, 128);
267  20, 128);
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);
273  }
274 }
ProbContext::class0_fp
uint8_t class0_fp[2][3]
Definition: vp9dec.h:68
av_clip
#define av_clip
Definition: common.h:95
PRED_SWITCHABLE
@ PRED_SWITCHABLE
Definition: vp9shared.h:51
PRED_SINGLEREF
@ PRED_SINGLEREF
Definition: vp9shared.h:49
ProbContext::classes
uint8_t classes[10]
Definition: vp9dec.h:65
PRED_COMPREF
@ PRED_COMPREF
Definition: vp9shared.h:50
TM_VP8_PRED
@ TM_VP8_PRED
Definition: vp9.h:55
DC_PRED
@ DC_PRED
Definition: vp9.h:48
VERT_LEFT_PRED
@ VERT_LEFT_PRED
Definition: vp9.h:53
ProbContext::hp
uint8_t hp
Definition: vp9dec.h:71
ProbContext::tx32p
uint8_t tx32p[2][3]
Definition: vp9dec.h:58
ProbContext::comp
uint8_t comp[5]
Definition: vp9dec.h:55
FILTER_SWITCHABLE
@ FILTER_SWITCHABLE
Definition: vp9.h:70
ProbContext::sign
uint8_t sign
Definition: vp9dec.h:64
TX_SWITCHABLE
@ TX_SWITCHABLE
Definition: vp9.h:33
VERT_PRED
@ VERT_PRED
Definition: vp9.h:46
DIAG_DOWN_RIGHT_PRED
@ DIAG_DOWN_RIGHT_PRED
Definition: vp9.h:50
ProbContext::mv_comp
struct ProbContext::@177 mv_comp[2]
ProbContext::y_mode
uint8_t y_mode[4][9]
Definition: vp9dec.h:50
HOR_PRED
@ HOR_PRED
Definition: vp9.h:47
s
#define s(width, name)
Definition: cbs_vp9.c:256
vp9data.h
vp56.h
VP9Context
Definition: vp9dec.h:94
FASTDIV
#define FASTDIV(a, b)
Definition: mathops.h:204
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ProbContext::bits
uint8_t bits[10]
Definition: vp9dec.h:67
s2
#define s2
Definition: regdef.h:39
vp9.h
ProbContext::skip
uint8_t skip[3]
Definition: vp9dec.h:61
ProbContext::mv_mode
uint8_t mv_mode[7][3]
Definition: vp9dec.h:53
VERT_RIGHT_PRED
@ VERT_RIGHT_PRED
Definition: vp9.h:51
ProbContext::comp_ref
uint8_t comp_ref[5]
Definition: vp9dec.h:57
ProbContext
Definition: vp9dec.h:49
ProbContext::fp
uint8_t fp[3]
Definition: vp9dec.h:69
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ProbContext::class0_hp
uint8_t class0_hp
Definition: vp9dec.h:70
ProbContext::single_ref
uint8_t single_ref[5][2]
Definition: vp9dec.h:56
ProbContext::tx16p
uint8_t tx16p[2][2]
Definition: vp9dec.h:59
c2
static const uint64_t c2
Definition: murmur3.c:52
HOR_UP_PRED
@ HOR_UP_PRED
Definition: vp9.h:54
ProbContext::partition
uint8_t partition[4][4][3]
Definition: vp9dec.h:73
ProbContext::mv_joint
uint8_t mv_joint[3]
Definition: vp9dec.h:62
HOR_DOWN_PRED
@ HOR_DOWN_PRED
Definition: vp9.h:52
ProbContext::filter
uint8_t filter[4][2]
Definition: vp9dec.h:52
vp9dec.h
ProbContext::tx8p
uint8_t tx8p[2]
Definition: vp9dec.h:60
ProbContext::class0
uint8_t class0
Definition: vp9dec.h:66
ProbContext::uv_mode
uint8_t uv_mode[10][9]
Definition: vp9dec.h:51
DIAG_DOWN_LEFT_PRED
@ DIAG_DOWN_LEFT_PRED
Definition: vp9.h:49
ff_vp9_adapt_probs
void ff_vp9_adapt_probs(VP9Context *s)
Definition: vp9prob.c:46
adapt_prob
static av_always_inline void adapt_prob(uint8_t *p, unsigned ct0, unsigned ct1, int max_count, int update_factor)
Definition: vp9prob.c:29
ProbContext::intra
uint8_t intra[4]
Definition: vp9dec.h:54