vp9_entropy.c 36.6 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9
10
11
12
13
 */


#include <stdio.h>

14
#include "vp9/common/vp9_entropy.h"
John Koleszar's avatar
John Koleszar committed
15
#include "string.h"
16
17
18
#include "vp9/common/vp9_blockd.h"
#include "vp9/common/vp9_onyxc_int.h"
#include "vp9/common/vp9_entropymode.h"
19
#include "vpx_mem/vpx_mem.h"
20
#include "vpx/vpx_integer.h"
21
#include "vp9/common/vp9_coefupdateprobs.h"
John Koleszar's avatar
John Koleszar committed
22

23
const int vp9_i8x8_block[4] = {0, 2, 8, 10};
24

25
DECLARE_ALIGNED(16, const uint8_t, vp9_norm[256]) = {
John Koleszar's avatar
John Koleszar committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
42
43
};

44
DECLARE_ALIGNED(16, const int, vp9_coef_bands_4x4[16]) = {
Deb Mukherjee's avatar
Deb Mukherjee committed
45
46
  0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7
};
47

48
DECLARE_ALIGNED(16, const uint8_t, vp9_prev_token_class[MAX_ENTROPY_TOKENS]) = {
Deb Mukherjee's avatar
Deb Mukherjee committed
49
50
  0, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 0
};
51

52
DECLARE_ALIGNED(16, const int, vp9_default_zig_zag1d_4x4[16]) = {
John Koleszar's avatar
John Koleszar committed
53
54
55
56
  0,  1,  4,  8,
  5,  2,  3,  6,
  9, 12, 13, 10,
  7, 11, 14, 15,
John Koleszar's avatar
John Koleszar committed
57
};
Jingning Han's avatar
Jingning Han committed
58

59
DECLARE_ALIGNED(16, const int, vp9_col_scan_4x4[16]) = {
Jingning Han's avatar
Jingning Han committed
60
61
62
63
64
  0, 4,  8, 12,
  1, 5,  9, 13,
  2, 6, 10, 14,
  3, 7, 11, 15
};
65

66
DECLARE_ALIGNED(16, const int, vp9_row_scan_4x4[16]) = {
Jingning Han's avatar
Jingning Han committed
67
68
69
70
71
72
  0,   1,  2,  3,
  4,   5,  6,  7,
  8,   9, 10, 11,
  12, 13, 14, 15
};

73
DECLARE_ALIGNED(64, const int, vp9_coef_bands_8x8[64]) = { 0, 1, 2, 3, 5, 4, 4, 5,
74
75
76
77
78
79
80
81
82
  5, 3, 6, 3, 5, 4, 6, 6,
  6, 5, 5, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7
};

83
DECLARE_ALIGNED(64, const int, vp9_default_zig_zag1d_8x8[64]) = {
John Koleszar's avatar
John Koleszar committed
84
85
86
87
  0,  1,  8, 16,  9,  2,  3, 10, 17, 24, 32, 25, 18, 11,  4,  5,
  12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13,  6,  7, 14, 21, 28,
  35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
  58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
88
};
John Koleszar's avatar
John Koleszar committed
89

Daniel Kang's avatar
Daniel Kang committed
90
// Table can be optimized.
91
DECLARE_ALIGNED(16, const int, vp9_coef_bands_16x16[256]) = {
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
  0, 1, 2, 3, 5, 4, 4, 5, 5, 3, 6, 3, 5, 4, 6, 6,
  6, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
Daniel Kang's avatar
Daniel Kang committed
108
};
109

110
DECLARE_ALIGNED(16, const int, vp9_default_zig_zag1d_16x16[256]) = {
111
112
113
114
115
116
117
118
119
120
121
  0,   1,  16,  32,  17,   2,   3,  18,
  33,  48,  64,  49,  34,  19,   4,   5,
  20,  35,  50,  65,  80,  96,  81,  66,
  51,  36,  21,   6,   7,  22,  37,  52,
  67,  82,  97, 112, 128, 113,  98,  83,
  68,  53,  38,  23,   8,   9,  24,  39,
  54,  69,  84,  99, 114, 129, 144, 160,
  145, 130, 115, 100,  85,  70,  55,  40,
  25,  10,  11,  26,  41,  56,  71,  86,
  101, 116, 131, 146, 161, 176, 192, 177,
  162, 147, 132, 117, 102,  87,  72,  57,
122
  42,  27,  12,  13,  28,  43,  58, 73,
123
124
125
  88, 103, 118, 133, 148, 163, 178, 193,
  208, 224, 209, 194, 179, 164, 149, 134,
  119, 104,  89,  74,  59,  44,  29,  14,
126
  15,  30, 45,  60,  75,  90, 105, 120,
127
128
129
  135, 150, 165, 180, 195, 210, 225, 240,
  241, 226, 211, 196, 181, 166, 151, 136,
  121, 106,  91,  76,  61,  46,  31,  47,
130
  62,  77, 92, 107, 122, 137, 152, 167,
131
  182, 197, 212, 227, 242, 243, 228, 213,
132
  198, 183, 168, 153, 138, 123, 108, 93,
133
134
135
136
137
138
139
140
141
142
  78,  63,  79,  94, 109, 124, 139, 154,
  169, 184, 199, 214, 229, 244, 245, 230,
  215, 200, 185, 170, 155, 140, 125, 110,
  95, 111, 126, 141, 156, 171, 186, 201,
  216, 231, 246, 247, 232, 217, 202, 187,
  172, 157, 142, 127, 143, 158, 173, 188,
  203, 218, 233, 248, 249, 234, 219, 204,
  189, 174, 159, 175, 190, 205, 220, 235,
  250, 251, 236, 221, 206, 191, 207, 222,
  237, 252, 253, 238, 223, 239, 254, 255,
Daniel Kang's avatar
Daniel Kang committed
143
};
144

145
#if CONFIG_TX32X32
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#if CONFIG_DWT32X32HYBRID
DECLARE_ALIGNED(16, const int, vp9_coef_bands_32x32[1024]) = {
  0, 1, 2, 3, 5, 4, 4, 5, 5, 3, 6, 3, 5, 4, 6,
  6, 6, 6,
  6,
  6, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,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
  16,   512,  528,
  5,
  36,   67,   98,  129,  160,  192,  161,  130,
  99,   68,   37,    6,    7,   38,   69,  100,
  131,  162,  193,  224, 256,  225,  194,  163,
  132,  101,   70,   39,    8,    9,   40,   71,
  102,  133,  164,  195,  226,  257,  288,  320,
  289,  258,  227,  196,  165,  134,  103,   72,
  41,   10,   11,   42,   73,  104,  135,  166,
  197,  228,  259,  290,  321,  352,  384,  353,
  322,  291,  260,  229,  198,  167,  136,  105,
  74,   43,   12,   13,   44,   75,  106,  137,
  168,  199,  230,  261,  292,  323,  354,  385,
  416,  448,  417,  386,  355,  324,  293,  262,
  231,  200,  169,  138,  107,   76,   45,   14,
  15,   46,   77,  108,  139,  170,  201,  232,
  263,  294,  325,  356,  387,  418,  449,  480,
  481,  450,  419,  388,  357,  326,  295,  264,
  233,  202,  171,  140,  109,   78,   47,   79,
  110,  141,  172,  203,  234,  265,  296,  327,
  358,  389,  420,  451,  482,  483,  452,  421,
  390,  359,  328,  297,  266,  235,  204,  173,
  142,  111,  143,  174,  205,  236,  267,  298,
  329,  360,  391,  422,  453,  484,  485,  454,
  423,  392,  361,  330,  299,  268,  237,  206,
  175,  207,  238,  269,  300,  331,  362,  393,
  424,  455,  486,  487,  456,  425,  394,  363,
  332,  301,  270,  239,  271,  302,  333,  364,
  395,  426,  457,  488,  489,  458,  427,  396,
  365,  334,  303,  335,  366,  397,  428,  459,
  490,  491,  460,  429,  398,  367,  399,  430,
  461,  492,  493,  462,  431,  463,  494,  495,

  17,  513,  529,   48,  544,
  560, 80,  576,  592,   49,  545,  561,   18,
  514,  530,   19,  515,  531,   50,  546,  562,
  81,  577,  593,  112,  608,  624,  144,  640,
  656,  113,  609,  625,   82,  578,  594,   51,
  547,  563,   20,  516,  532,   21,  517,  533,
  52,  548,  564,   83,  579,  595,  114,  610,
  626,  145,  641,  657,  176,  672,  688,  208,
  704,  720,  177,  673,  689,  146,  642,  658,
  115,  611,  627,   84,  580,  596,   53,  549,
  565,   22,  518,  534,   23,  519,  535,   54,
  550,  566,   85,  581,  597,  116,  612,  628,
  147,  643,  659,  178,  674,  690,  209,  705,
  721,  240,  736,  752,  272,  768,  784,  241,
  737,  753,  210,  706,  722,  179,  675,  691,
  148,  644,  660,  117,  613,  629,   86,  582,
  598,   55,  551,  567,   24,  520,  536,   25,
  521,  537,   56,  552,  568,   87,  583,  599,
  118,  614,  630,  149,  645,  661,  180,  676,
  692,  211,  707,  723,  242,  738,  754,  273,
  769,  785,  304,  800,  816,  336,  832,  848,
  305,  801,  817,  274,  770,  786,  243,  739,
  755,  212,  708,  724,  181,  677,  693,  150,
  646,  662,  119,  615,  631,   88,  584,  600,
  57,  553,  569,   26,  522,  538,   27,  523,
  539,   58,  554,  570,   89,  585,  601,  120,
  616,  632,  151,  647,  663,  182,  678,  694,
  213,  709,  725,  244,  740,  756,  275,  771,
  787,  306,  802,  818,  337,  833,  849,  368,
  864,  880,  400,  896,  912,  369,  865,  881,
  338,  834,  850,  307,  803,  819,  276,  772,
  788,  245,  741,  757,  214,  710,  726,  183,

  679,  695,  152,  648,  664,  121,  617,  633,
  90,  586,  602,   59,  555,  571,   28,  524,
  540,   29,  525,  541,   60,  556,  572,   91,
  587,  603,  122,  618,  634,  153,  649,  665,
  184,  680,  696,  215,  711,  727,  246,  742,
  758,  277,  773,  789,  308,  804,  820,  339,
  835,  851,  370,  866,  882,  401,  897,  913,
  432,  928,  944,  464,  960,  976,  433,  929,
  945,  402,  898,  914,  371,  867,  883,  340,
  836,  852,  309,  805,  821,  278,  774,  790,
  247,  743,  759,  216,  712,  728,  185,  681,
  697,  154,  650,  666,  123,  619,  635,   92,
  588,  604,   61,  557,  573,   30,  526,  542,
  31,  527,  543,   62,  558,  574,   93,  589,
  605,  124,  620,  636,  155,  651,  667,  186,
  682,  698,  217,  713,  729,  248,  744,  760,
  279,  775,  791,  310,  806,  822,  341,  837,
  853,  372,  868,  884,  403,  899,  915,  434,
  930,  946,  465,  961,  977,  496,  992, 1008,
  497,  993, 1009,  466,  962,  978,  435,  931,
  947,  404,  900,  916,  373,  869,  885,  342,
  838,  854,  311,  807,  823,  280,  776,  792,
  249,  745,  761,  218,  714,  730,  187,  683,
  699,  156,  652,  668,  125,  621,  637,   94,
  590,  606,   63,  559,  575,   95,  591,  607,
  126,  622,  638,  157,  653,  669,  188,  684,
  700,  219,  715,  731,  250,  746,  762,  281,
  777,  793,  312,  808,  824,  343,  839,  855,
  374,  870,  886,  405,  901,  917,  436,  932,
  948,  467,  963,  979,  498,  994, 1010,  499,
  995, 1011,  468,  964,  980,  437,  933,  949,
  406,  902,  918,  375,  871,  887,  344,  840,

  856,  313,  809,  825,  282,  778,  794,  251,
  747,  763,  220,  716,  732,  189,  685,  701,
  158,  654,  670,  127,  623,  639,  159,  655,
  671,  190,  686,  702,  221,  717,  733,  252,
  748,  764,  283,  779,  795,  314,  810,  826,
  345,  841,  857,  376,  872,  888,  407,  903,
  919,  438,  934,  950,  469,  965,  981,  500,
  996, 1012,  501,  997, 1013,  470,  966,  982,
  439,  935,  951,  408,  904,  920,  377,  873,
  889,  346,  842,  858,  315,  811,  827,  284,
  780,  796,  253,  749,  765,  222,  718,  734,
  191,  687,  703,  223,  719,  735,  254,  750,
  766,  285,  781,  797,  316,  812,  828,  347,
  843,  859,  378,  874,  890,  409,  905,  921,
  440,  936,  952,  471,  967,  983,  502,  998,
  1014,  503,  999, 1015,  472,  968,  984,  441,
  937,  953,  410,  906,  922,  379,  875,  891,
  348,  844,  860,  317,  813,  829,  286,  782,
  798,  255,  751,  767,  287,  783,  799,  318,
  814,  830,  349,  845,  861,  380,  876,  892,
  411,  907,  923,  442,  938,  954,  473,  969,
  985,  504, 1000, 1016,  505, 1001, 1017,  474,
  970,  986,  443,  939,  955,  412,  908,  924,
  381,  877,  893,  350,  846,  862,  319,  815,
  831,  351,  847,  863,  382,  878,  894,  413,
  909,  925,  444,  940,  956,  475,  971,  987,
  506, 1002, 1018,  507, 1003, 1019,  476,  972,
  988,  445,  941,  957,  414,  910,  926,  383,
  879,  895,  415,  911,  927,  446,  942,  958,
  477,  973,  989,  508, 1004, 1020,  509, 1005,
  1021,  478,  974,  990,  447,  943,  959,  479,
  975,  991,  510, 1006, 1022,  511, 1007, 1023,
};

#else

357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
DECLARE_ALIGNED(16, const int, vp9_coef_bands_32x32[1024]) = {
  0, 1, 2, 3, 5, 4, 4, 5, 5, 3, 6, 3, 5, 4, 6, 6,
  6, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,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, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
};
426

427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
DECLARE_ALIGNED(16, const int, vp9_default_zig_zag1d_32x32[1024]) = {
    0,    1,   32,   64,   33,    2,    3,   34,   65,   96,  128,   97,   66,   35,    4,    5,   36,   67,   98,  129,  160,  192,  161,  130,   99,   68,   37,    6,    7,   38,   69,  100,
  131,  162,  193,  224,  256,  225,  194,  163,  132,  101,   70,   39,    8,    9,   40,   71,  102,  133,  164,  195,  226,  257,  288,  320,  289,  258,  227,  196,  165,  134,  103,   72,
   41,   10,   11,   42,   73,  104,  135,  166,  197,  228,  259,  290,  321,  352,  384,  353,  322,  291,  260,  229,  198,  167,  136,  105,   74,   43,   12,   13,   44,   75,  106,  137,
  168,  199,  230,  261,  292,  323,  354,  385,  416,  448,  417,  386,  355,  324,  293,  262,  231,  200,  169,  138,  107,   76,   45,   14,   15,   46,   77,  108,  139,  170,  201,  232,
  263,  294,  325,  356,  387,  418,  449,  480,  512,  481,  450,  419,  388,  357,  326,  295,  264,  233,  202,  171,  140,  109,   78,   47,   16,   17,   48,   79,  110,  141,  172,  203,
  234,  265,  296,  327,  358,  389,  420,  451,  482,  513,  544,  576,  545,  514,  483,  452,  421,  390,  359,  328,  297,  266,  235,  204,  173,  142,  111,   80,   49,   18,   19,   50,
   81,  112,  143,  174,  205,  236,  267,  298,  329,  360,  391,  422,  453,  484,  515,  546,  577,  608,  640,  609,  578,  547,  516,  485,  454,  423,  392,  361,  330,  299,  268,  237,
  206,  175,  144,  113,   82,   51,   20,   21,   52,   83,  114,  145,  176,  207,  238,  269,  300,  331,  362,  393,  424,  455,  486,  517,  548,  579,  610,  641,  672,  704,  673,  642,
  611,  580,  549,  518,  487,  456,  425,  394,  363,  332,  301,  270,  239,  208,  177,  146,  115,   84,   53,   22,   23,   54,   85,  116,  147,  178,  209,  240,  271,  302,  333,  364,
  395,  426,  457,  488,  519,  550,  581,  612,  643,  674,  705,  736,  768,  737,  706,  675,  644,  613,  582,  551,  520,  489,  458,  427,  396,  365,  334,  303,  272,  241,  210,  179,
  148,  117,   86,   55,   24,   25,   56,   87,  118,  149,  180,  211,  242,  273,  304,  335,  366,  397,  428,  459,  490,  521,  552,  583,  614,  645,  676,  707,  738,  769,  800,  832,
  801,  770,  739,  708,  677,  646,  615,  584,  553,  522,  491,  460,  429,  398,  367,  336,  305,  274,  243,  212,  181,  150,  119,   88,   57,   26,   27,   58,   89,  120,  151,  182,
  213,  244,  275,  306,  337,  368,  399,  430,  461,  492,  523,  554,  585,  616,  647,  678,  709,  740,  771,  802,  833,  864,  896,  865,  834,  803,  772,  741,  710,  679,  648,  617,
  586,  555,  524,  493,  462,  431,  400,  369,  338,  307,  276,  245,  214,  183,  152,  121,   90,   59,   28,   29,   60,   91,  122,  153,  184,  215,  246,  277,  308,  339,  370,  401,
  432,  463,  494,  525,  556,  587,  618,  649,  680,  711,  742,  773,  804,  835,  866,  897,  928,  960,  929,  898,  867,  836,  805,  774,  743,  712,  681,  650,  619,  588,  557,  526,
  495,  464,  433,  402,  371,  340,  309,  278,  247,  216,  185,  154,  123,   92,   61,   30,   31,   62,   93,  124,  155,  186,  217,  248,  279,  310,  341,  372,  403,  434,  465,  496,
  527,  558,  589,  620,  651,  682,  713,  744,  775,  806,  837,  868,  899,  930,  961,  992,  993,  962,  931,  900,  869,  838,  807,  776,  745,  714,  683,  652,  621,  590,  559,  528,
  497,  466,  435,  404,  373,  342,  311,  280,  249,  218,  187,  156,  125,   94,   63,   95,  126,  157,  188,  219,  250,  281,  312,  343,  374,  405,  436,  467,  498,  529,  560,  591,
  622,  653,  684,  715,  746,  777,  808,  839,  870,  901,  932,  963,  994,  995,  964,  933,  902,  871,  840,  809,  778,  747,  716,  685,  654,  623,  592,  561,  530,  499,  468,  437,
  406,  375,  344,  313,  282,  251,  220,  189,  158,  127,  159,  190,  221,  252,  283,  314,  345,  376,  407,  438,  469,  500,  531,  562,  593,  624,  655,  686,  717,  748,  779,  810,
  841,  872,  903,  934,  965,  996,  997,  966,  935,  904,  873,  842,  811,  780,  749,  718,  687,  656,  625,  594,  563,  532,  501,  470,  439,  408,  377,  346,  315,  284,  253,  222,
  191,  223,  254,  285,  316,  347,  378,  409,  440,  471,  502,  533,  564,  595,  626,  657,  688,  719,  750,  781,  812,  843,  874,  905,  936,  967,  998,  999,  968,  937,  906,  875,
  844,  813,  782,  751,  720,  689,  658,  627,  596,  565,  534,  503,  472,  441,  410,  379,  348,  317,  286,  255,  287,  318,  349,  380,  411,  442,  473,  504,  535,  566,  597,  628,
  659,  690,  721,  752,  783,  814,  845,  876,  907,  938,  969, 1000, 1001,  970,  939,  908,  877,  846,  815,  784,  753,  722,  691,  660,  629,  598,  567,  536,  505,  474,  443,  412,
  381,  350,  319,  351,  382,  413,  444,  475,  506,  537,  568,  599,  630,  661,  692,  723,  754,  785,  816,  847,  878,  909,  940,  971, 1002, 1003,  972,  941,  910,  879,  848,  817,
  786,  755,  724,  693,  662,  631,  600,  569,  538,  507,  476,  445,  414,  383,  415,  446,  477,  508,  539,  570,  601,  632,  663,  694,  725,  756,  787,  818,  849,  880,  911,  942,
  973, 1004, 1005,  974,  943,  912,  881,  850,  819,  788,  757,  726,  695,  664,  633,  602,  571,  540,  509,  478,  447,  479,  510,  541,  572,  603,  634,  665,  696,  727,  758,  789,
  820,  851,  882,  913,  944,  975, 1006, 1007,  976,  945,  914,  883,  852,  821,  790,  759,  728,  697,  666,  635,  604,  573,  542,  511,  543,  574,  605,  636,  667,  698,  729,  760,
  791,  822,  853,  884,  915,  946,  977, 1008, 1009,  978,  947,  916,  885,  854,  823,  792,  761,  730,  699,  668,  637,  606,  575,  607,  638,  669,  700,  731,  762,  793,  824,  855,
  886,  917,  948,  979, 1010, 1011,  980,  949,  918,  887,  856,  825,  794,  763,  732,  701,  670,  639,  671,  702,  733,  764,  795,  826,  857,  888,  919,  950,  981, 1012, 1013,  982,
  951,  920,  889,  858,  827,  796,  765,  734,  703,  735,  766,  797,  828,  859,  890,  921,  952,  983, 1014, 1015,  984,  953,  922,  891,  860,  829,  798,  767,  799,  830,  861,  892,
  923,  954,  985, 1016, 1017,  986,  955,  924,  893,  862,  831,  863,  894,  925,  956,  987, 1018, 1019,  988,  957,  926,  895,  927,  958,  989, 1020, 1021,  990,  959,  991, 1022, 1023,
};
461
462
#endif  // CONFIG_DWT32X32HYBRID
#endif
John Koleszar's avatar
John Koleszar committed
463
464
465

/* Array indices are identical to previously-existing CONTEXT_NODE indices */

466
const vp9_tree_index vp9_coef_tree[ 22] =     /* corresponding _CONTEXT_NODEs */
John Koleszar's avatar
John Koleszar committed
467
{
John Koleszar's avatar
John Koleszar committed
468
469
470
471
472
473
474
475
476
477
478
  -DCT_EOB_TOKEN, 2,                             /* 0 = EOB */
  -ZERO_TOKEN, 4,                               /* 1 = ZERO */
  -ONE_TOKEN, 6,                               /* 2 = ONE */
  8, 12,                                      /* 3 = LOW_VAL */
  -TWO_TOKEN, 10,                            /* 4 = TWO */
  -THREE_TOKEN, -FOUR_TOKEN,                /* 5 = THREE */
  14, 16,                                    /* 6 = HIGH_LOW */
  -DCT_VAL_CATEGORY1, -DCT_VAL_CATEGORY2,   /* 7 = CAT_ONE */
  18, 20,                                   /* 8 = CAT_THREEFOUR */
  -DCT_VAL_CATEGORY3, -DCT_VAL_CATEGORY4,  /* 9 = CAT_THREE */
  -DCT_VAL_CATEGORY5, -DCT_VAL_CATEGORY6   /* 10 = CAT_FIVE */
John Koleszar's avatar
John Koleszar committed
479
480
};

481
struct vp9_token_struct vp9_coef_encodings[MAX_ENTROPY_TOKENS];
John Koleszar's avatar
John Koleszar committed
482
483
484
485

/* Trees for extra bits.  Probabilities are constant and
   do not depend on previously encoded bits */

486
487
488
489
490
491
static const vp9_prob Pcat1[] = { 159};
static const vp9_prob Pcat2[] = { 165, 145};
static const vp9_prob Pcat3[] = { 173, 148, 140};
static const vp9_prob Pcat4[] = { 176, 155, 140, 135};
static const vp9_prob Pcat5[] = { 180, 157, 141, 134, 130};
static const vp9_prob Pcat6[] = {
492
493
  254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129
};
John Koleszar's avatar
John Koleszar committed
494

495
static vp9_tree_index cat1[2], cat2[4], cat3[6], cat4[8], cat5[10], cat6[28];
John Koleszar's avatar
John Koleszar committed
496

497
static void init_bit_tree(vp9_tree_index *p, int n) {
John Koleszar's avatar
John Koleszar committed
498
  int i = 0;
John Koleszar's avatar
John Koleszar committed
499

John Koleszar's avatar
John Koleszar committed
500
501
502
503
  while (++i < n) {
    p[0] = p[1] = i << 1;
    p += 2;
  }
John Koleszar's avatar
John Koleszar committed
504

John Koleszar's avatar
John Koleszar committed
505
  p[0] = p[1] = 0;
John Koleszar's avatar
John Koleszar committed
506
507
}

John Koleszar's avatar
John Koleszar committed
508
509
510
511
512
513
static void init_bit_trees() {
  init_bit_tree(cat1, 1);
  init_bit_tree(cat2, 2);
  init_bit_tree(cat3, 3);
  init_bit_tree(cat4, 4);
  init_bit_tree(cat5, 5);
514
  init_bit_tree(cat6, 14);
John Koleszar's avatar
John Koleszar committed
515
516
}

517
vp9_extra_bit_struct vp9_extra_bits[12] = {
John Koleszar's avatar
John Koleszar committed
518
519
520
521
522
523
524
525
526
527
  { 0, 0, 0, 0},
  { 0, 0, 0, 1},
  { 0, 0, 0, 2},
  { 0, 0, 0, 3},
  { 0, 0, 0, 4},
  { cat1, Pcat1, 1, 5},
  { cat2, Pcat2, 2, 7},
  { cat3, Pcat3, 3, 11},
  { cat4, Pcat4, 4, 19},
  { cat5, Pcat5, 5, 35},
528
  { cat6, Pcat6, 14, 67},
John Koleszar's avatar
John Koleszar committed
529
  { 0, 0, 0, 0}
John Koleszar's avatar
John Koleszar committed
530
};
531

532
#include "vp9/common/vp9_default_coef_probs.h"
John Koleszar's avatar
John Koleszar committed
533

534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#if CONFIG_NEWCOEFCONTEXT

// Neighborhood 5-tuples for various scans and blocksizes,
// in {top, left, topleft, topright, bottomleft} order
// for each position in raster scan order.
// -1 indicates the neighbor does not exist.
DECLARE_ALIGNED(16, int,
                vp9_default_zig_zag1d_4x4_neighbors[16 * MAX_NEIGHBORS]);
DECLARE_ALIGNED(16, int,
                vp9_col_scan_4x4_neighbors[16 * MAX_NEIGHBORS]);
DECLARE_ALIGNED(16, int,
                vp9_row_scan_4x4_neighbors[16 * MAX_NEIGHBORS]);
DECLARE_ALIGNED(16, int,
                vp9_default_zig_zag1d_8x8_neighbors[64 * MAX_NEIGHBORS]);
DECLARE_ALIGNED(16, int,
                vp9_default_zig_zag1d_16x16_neighbors[256 * MAX_NEIGHBORS]);
550
#if CONFIG_TX32X32
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
DECLARE_ALIGNED(16, int,
                vp9_default_zig_zag1d_32x32_neighbors[1024 * MAX_NEIGHBORS]);
#endif

static int find_in_scan(const int *scan, int l, int m) {
  int i, l2 = l * l;
  for (i = 0; i < l2; ++i) {
    if (scan[i] == m)
      return i;
  }
  return -1;
}

static void init_scan_neighbors(const int *scan, int l, int *neighbors) {
  int l2 = l * l;
  int m, n, i, j, k;
  for (n = 0; n < l2; ++n) {
    int locn = find_in_scan(scan, l, n);
    int z = -1;
    i = n / l;
    j = n % l;
    for (k = 0; k < MAX_NEIGHBORS; ++k)
      neighbors[MAX_NEIGHBORS * n + k] = -1;
    if (i - 1 >= 0) {
      m = (i - 1) * l + j;
      if (find_in_scan(scan, l, m) < locn) {
        neighbors[MAX_NEIGHBORS * n] = m;
        if (m == 0) z = 0;
      }
    }
    if (j - 1 >= 0) {
      m = i * l + j - 1;
      if (find_in_scan(scan, l, m) < locn) {
        neighbors[MAX_NEIGHBORS * n + 1] = m;
        if (m == 0) z = 1;
      }
    }
    if (i - 1 >= 0 && j - 1 >= 0) {
      m = (i - 1) * l + j - 1;
      if (find_in_scan(scan, l, m) < locn) {
        neighbors[MAX_NEIGHBORS * n + 2] = m;
        if (m == 0) z = 2;
      }
    }
    if (i - 1 >= 0 && j + 1 < l) {
      m = (i - 1) * l + j + 1;
      if (find_in_scan(scan, l, m) < locn) {
        neighbors[MAX_NEIGHBORS * n + 3] = m;
        if (m == 0) z = 3;
      }
    }
    if (i + 1 < l && j - 1 >= 0) {
       m = (i + 1) * l + j - 1;
      if (find_in_scan(scan, l, m) < locn) {
        neighbors[MAX_NEIGHBORS * n + 4] = m;
        if (m == 0) z = 4;
      }
    }
    if (z != -1) {  // zero exists
      int v = 0;
      for (k = 0; k < MAX_NEIGHBORS; ++k)
        v += (neighbors[MAX_NEIGHBORS * n + k] > 0);
      if (v) {
        neighbors[MAX_NEIGHBORS * n + z] = -1;
      }
    }
  }
}

void vp9_init_neighbors() {
  init_scan_neighbors(vp9_default_zig_zag1d_4x4, 4,
                      vp9_default_zig_zag1d_4x4_neighbors);
  init_scan_neighbors(vp9_row_scan_4x4, 4,
                      vp9_row_scan_4x4_neighbors);
  init_scan_neighbors(vp9_col_scan_4x4, 4,
                      vp9_col_scan_4x4_neighbors);
  init_scan_neighbors(vp9_default_zig_zag1d_8x8, 8,
                      vp9_default_zig_zag1d_8x8_neighbors);
  init_scan_neighbors(vp9_default_zig_zag1d_16x16, 16,
                      vp9_default_zig_zag1d_16x16_neighbors);
631
#if CONFIG_TX32X32
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
  init_scan_neighbors(vp9_default_zig_zag1d_32x32, 32,
                      vp9_default_zig_zag1d_32x32_neighbors);
#endif
}

const int *vp9_get_coef_neighbors_handle(const int *scan) {
  if (scan == vp9_default_zig_zag1d_4x4) {
    return vp9_default_zig_zag1d_4x4_neighbors;
  } else if (scan == vp9_row_scan_4x4) {
    return vp9_row_scan_4x4_neighbors;
  } else if (scan == vp9_col_scan_4x4) {
    return vp9_col_scan_4x4_neighbors;
  } else if (scan == vp9_default_zig_zag1d_8x8) {
    return vp9_default_zig_zag1d_8x8_neighbors;
  } else if (scan == vp9_default_zig_zag1d_16x16) {
    return vp9_default_zig_zag1d_16x16_neighbors;
648
#if CONFIG_TX32X32
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
  } else if (scan == vp9_default_zig_zag1d_32x32) {
    return vp9_default_zig_zag1d_32x32_neighbors;
#endif
  }
  return vp9_default_zig_zag1d_4x4_neighbors;
}

int vp9_get_coef_neighbor_context(const short int *qcoeff_ptr, int nodc,
                                  const int *neigbor_handle, int rc) {
  static int neighbors_used = MAX_NEIGHBORS;   // maximum is MAX_NEIGHBORS
  const int *nb = neigbor_handle + rc * MAX_NEIGHBORS;
  int i, v, val = 0, n = 0;
  for (i = 0; i < neighbors_used; ++i) {
    if (nb[i] == -1 || (nb[i] == 0 && nodc)) {
      continue;
    }
    v = abs(qcoeff_ptr[nb[i]]);
    val = (v > val ? v : val);
    n++;
  }
  if (n == 0)
    return 0;
  else if (val <= 1)
    return val;
  else if (val < 4)
    return 2;
  else
    return 3;
}
#endif  /* CONFIG_NEWCOEFCONTEXT */

680
void vp9_default_coef_probs(VP9_COMMON *pc) {
681
682
683
684
  vpx_memcpy(pc->fc.coef_probs_4x4, default_coef_probs_4x4,
             sizeof(pc->fc.coef_probs_4x4));
  vpx_memcpy(pc->fc.hybrid_coef_probs_4x4, default_hybrid_coef_probs_4x4,
             sizeof(pc->fc.hybrid_coef_probs_4x4));
685

686
  vpx_memcpy(pc->fc.coef_probs_8x8, default_coef_probs_8x8,
Daniel Kang's avatar
Daniel Kang committed
687
             sizeof(pc->fc.coef_probs_8x8));
688
689
  vpx_memcpy(pc->fc.hybrid_coef_probs_8x8, default_hybrid_coef_probs_8x8,
             sizeof(pc->fc.hybrid_coef_probs_8x8));
Yaowu Xu's avatar
Yaowu Xu committed
690

691
  vpx_memcpy(pc->fc.coef_probs_16x16, default_coef_probs_16x16,
Daniel Kang's avatar
Daniel Kang committed
692
             sizeof(pc->fc.coef_probs_16x16));
693
694
695
  vpx_memcpy(pc->fc.hybrid_coef_probs_16x16,
             default_hybrid_coef_probs_16x16,
             sizeof(pc->fc.hybrid_coef_probs_16x16));
696
#if CONFIG_TX32X32
697
698
699
  vpx_memcpy(pc->fc.coef_probs_32x32, default_coef_probs_32x32,
             sizeof(pc->fc.coef_probs_32x32));
#endif
700
}
John Koleszar's avatar
John Koleszar committed
701

702
void vp9_coef_tree_initialize() {
John Koleszar's avatar
John Koleszar committed
703
  init_bit_trees();
704
  vp9_tokens_from_tree(vp9_coef_encodings, vp9_coef_tree);
John Koleszar's avatar
John Koleszar committed
705
}
706

John Koleszar's avatar
John Koleszar committed
707
// #define COEF_COUNT_TESTING
708
709
710
711
712
713
714
715

#define COEF_COUNT_SAT 24
#define COEF_MAX_UPDATE_FACTOR 112
#define COEF_COUNT_SAT_KEY 24
#define COEF_MAX_UPDATE_FACTOR_KEY 112
#define COEF_COUNT_SAT_AFTER_KEY 24
#define COEF_MAX_UPDATE_FACTOR_AFTER_KEY 128

716
717
718
719
static void update_coef_probs(vp9_coeff_probs *dst_coef_probs,
                              vp9_coeff_probs *pre_coef_probs,
                              int block_types, vp9_coeff_count *coef_counts,
                              int count_sat, int update_factor) {
John Koleszar's avatar
John Koleszar committed
720
721
  int t, i, j, k, count;
  unsigned int branch_ct[ENTROPY_NODES][2];
722
  vp9_prob coef_probs[ENTROPY_NODES];
John Koleszar's avatar
John Koleszar committed
723
  int factor;
724
725
726
727
728
729
730
731
732

  for (i = 0; i < block_types; ++i)
    for (j = 0; j < COEF_BANDS; ++j)
      for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
        if (k >= 3 && ((i == 0 && j == 1) || (i > 0 && j == 0)))
          continue;
        vp9_tree_probs_from_distribution(MAX_ENTROPY_TOKENS,
                                         vp9_coef_encodings, vp9_coef_tree,
                                         coef_probs, branch_ct,
733
                                         coef_counts[i][j][k]);
734
735
736
737
        for (t = 0; t < ENTROPY_NODES; ++t) {
          count = branch_ct[t][0] + branch_ct[t][1];
          count = count > count_sat ? count_sat : count;
          factor = (update_factor * count / count_sat);
738
739
          dst_coef_probs[i][j][k][t] = weighted_prob(pre_coef_probs[i][j][k][t],
                                                     coef_probs[t], factor);
740
741
742
743
744
745
746
747
        }
      }
}

void vp9_adapt_coef_probs(VP9_COMMON *cm) {
#ifdef COEF_COUNT_TESTING
  int t, i, j, k;
#endif
John Koleszar's avatar
John Koleszar committed
748
  int count_sat;
749
  int update_factor; /* denominator 256 */
John Koleszar's avatar
John Koleszar committed
750
751
752
753
754
755
756
757
758
759
760
761

  // printf("Frame type: %d\n", cm->frame_type);
  if (cm->frame_type == KEY_FRAME) {
    update_factor = COEF_MAX_UPDATE_FACTOR_KEY;
    count_sat = COEF_COUNT_SAT_KEY;
  } else if (cm->last_frame_type == KEY_FRAME) {
    update_factor = COEF_MAX_UPDATE_FACTOR_AFTER_KEY;  /* adapt quickly */
    count_sat = COEF_COUNT_SAT_AFTER_KEY;
  } else {
    update_factor = COEF_MAX_UPDATE_FACTOR;
    count_sat = COEF_COUNT_SAT;
  }
762
763

#ifdef COEF_COUNT_TESTING
John Koleszar's avatar
John Koleszar committed
764
765
766
767
768
769
770
771
772
773
  {
    printf("static const unsigned int\ncoef_counts"
           "[BLOCK_TYPES] [COEF_BANDS]"
           "[PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {\n");
    for (i = 0; i < BLOCK_TYPES; ++i) {
      printf("  {\n");
      for (j = 0; j < COEF_BANDS; ++j) {
        printf("    {\n");
        for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
          printf("      {");
774
775
          for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
            printf("%d, ", cm->fc.coef_counts[i][j][k][t]);
John Koleszar's avatar
John Koleszar committed
776
          printf("},\n");
777
        }
John Koleszar's avatar
John Koleszar committed
778
        printf("    },\n");
779
      }
John Koleszar's avatar
John Koleszar committed
780
781
782
783
784
785
786
787
788
789
790
791
      printf("  },\n");
    }
    printf("};\n");
    printf("static const unsigned int\ncoef_counts_8x8"
           "[BLOCK_TYPES_8X8] [COEF_BANDS]"
           "[PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {\n");
    for (i = 0; i < BLOCK_TYPES_8X8; ++i) {
      printf("  {\n");
      for (j = 0; j < COEF_BANDS; ++j) {
        printf("    {\n");
        for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
          printf("      {");
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
          for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
            printf("%d, ", cm->fc.coef_counts_8x8[i][j][k][t]);
          printf("},\n");
        }
        printf("    },\n");
      }
      printf("  },\n");
    }
    printf("};\n");
    printf("static const unsigned int\nhybrid_coef_counts"
           "[BLOCK_TYPES] [COEF_BANDS]"
           "[PREV_COEF_CONTEXTS] [MAX_ENTROPY_TOKENS] = {\n");
    for (i = 0; i < BLOCK_TYPES; ++i) {
      printf("  {\n");
      for (j = 0; j < COEF_BANDS; ++j) {
        printf("    {\n");
        for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
          printf("      {");
          for (t = 0; t < MAX_ENTROPY_TOKENS; ++t)
            printf("%d, ", cm->fc.hybrid_coef_counts[i][j][k][t]);
John Koleszar's avatar
John Koleszar committed
812
          printf("},\n");
813
        }
John Koleszar's avatar
John Koleszar committed
814
        printf("    },\n");
815
      }
John Koleszar's avatar
John Koleszar committed
816
      printf("  },\n");
817
    }
John Koleszar's avatar
John Koleszar committed
818
819
    printf("};\n");
  }
820
821
#endif

822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
  update_coef_probs(cm->fc.coef_probs_4x4, cm->fc.pre_coef_probs_4x4,
                    BLOCK_TYPES_4X4, cm->fc.coef_counts_4x4,
                    count_sat, update_factor);
  update_coef_probs(cm->fc.hybrid_coef_probs_4x4,
                    cm->fc.pre_hybrid_coef_probs_4x4,
                    BLOCK_TYPES_4X4, cm->fc.hybrid_coef_counts_4x4,
                    count_sat, update_factor);
  update_coef_probs(cm->fc.coef_probs_8x8, cm->fc.pre_coef_probs_8x8,
                    BLOCK_TYPES_8X8, cm->fc.coef_counts_8x8,
                    count_sat, update_factor);
  update_coef_probs(cm->fc.hybrid_coef_probs_8x8,
                    cm->fc.pre_hybrid_coef_probs_8x8,
                    BLOCK_TYPES_8X8, cm->fc.hybrid_coef_counts_8x8,
                    count_sat, update_factor);
  update_coef_probs(cm->fc.coef_probs_16x16, cm->fc.pre_coef_probs_16x16,
                    BLOCK_TYPES_16X16, cm->fc.coef_counts_16x16,
                    count_sat, update_factor);
  update_coef_probs(cm->fc.hybrid_coef_probs_16x16,
                    cm->fc.pre_hybrid_coef_probs_16x16,
                    BLOCK_TYPES_16X16, cm->fc.hybrid_coef_counts_16x16,
                    count_sat, update_factor);
843
#if CONFIG_TX32X32
844
845
846
  update_coef_probs(cm->fc.coef_probs_32x32, cm->fc.pre_coef_probs_32x32,
                    BLOCK_TYPES_32X32, cm->fc.coef_counts_32x32,
                    count_sat, update_factor);
847
#endif
848
}