Home | History | Annotate | Download | only in common
      1 /*
      2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  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.
      9  */
     10 
     11 #include "vp9/common/vp9_entropy.h"
     12 #include "vp9/common/vp9_blockd.h"
     13 #include "vp9/common/vp9_onyxc_int.h"
     14 #include "vp9/common/vp9_entropymode.h"
     15 #include "vpx_mem/vpx_mem.h"
     16 #include "vpx/vpx_integer.h"
     17 
     18 #define MODEL_NODES (ENTROPY_NODES - UNCONSTRAINED_NODES)
     19 
     20 DECLARE_ALIGNED(16, const uint8_t, vp9_norm[256]) = {
     21   0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
     22   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
     23   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
     24   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
     25   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     26   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     27   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     28   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     29   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     30   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     31   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     32   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     33   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     34   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     35   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     36   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
     37 };
     38 
     39 DECLARE_ALIGNED(16, const uint8_t,
     40                 vp9_coefband_trans_8x8plus[1024]) = {
     41   0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
     42   4, 4, 4, 4, 4, 5,
     43   // beyond MAXBAND_INDEX+1 all values are filled as 5
     44                     5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     45   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     46   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     47   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     48   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     49   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     50   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     51   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     52   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     53   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     54   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     55   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     56   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     57   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     58   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     59   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     60   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     61   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     62   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     63   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     64   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     65   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     66   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     67   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     68   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     69   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     70   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     71   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     72   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     73   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     74   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     75   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     76   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     77   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     78   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     79   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     80   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     81   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     82   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     83   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     84   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     85   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     86   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     87   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     88   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     89   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     90   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     91   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     92   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     93   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     94   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     95   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     96   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     97   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     98   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
     99   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
    100   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
    101   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
    102   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
    103   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
    104   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
    105   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
    106   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
    107 };
    108 
    109 DECLARE_ALIGNED(16, const uint8_t,
    110                 vp9_coefband_trans_4x4[16]) = {
    111   0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5,
    112 };
    113 
    114 DECLARE_ALIGNED(16, const uint8_t, vp9_pt_energy_class[MAX_ENTROPY_TOKENS]) = {
    115   0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5
    116 };
    117 
    118 
    119 
    120 /* Array indices are identical to previously-existing CONTEXT_NODE indices */
    121 
    122 const vp9_tree_index vp9_coef_tree[TREE_SIZE(MAX_ENTROPY_TOKENS)] = {
    123   -DCT_EOB_TOKEN, 2,                          /* 0 = EOB */
    124   -ZERO_TOKEN, 4,                             /* 1 = ZERO */
    125   -ONE_TOKEN, 6,                              /* 2 = ONE */
    126   8, 12,                                      /* 3 = LOW_VAL */
    127   -TWO_TOKEN, 10,                            /* 4 = TWO */
    128   -THREE_TOKEN, -FOUR_TOKEN,                /* 5 = THREE */
    129   14, 16,                                   /* 6 = HIGH_LOW */
    130   -DCT_VAL_CATEGORY1, -DCT_VAL_CATEGORY2,   /* 7 = CAT_ONE */
    131   18, 20,                                   /* 8 = CAT_THREEFOUR */
    132   -DCT_VAL_CATEGORY3, -DCT_VAL_CATEGORY4,   /* 9 = CAT_THREE */
    133   -DCT_VAL_CATEGORY5, -DCT_VAL_CATEGORY6    /* 10 = CAT_FIVE */
    134 };
    135 
    136 struct vp9_token vp9_coef_encodings[MAX_ENTROPY_TOKENS];
    137 
    138 /* Trees for extra bits.  Probabilities are constant and
    139    do not depend on previously encoded bits */
    140 
    141 static const vp9_prob Pcat1[] = { 159};
    142 static const vp9_prob Pcat2[] = { 165, 145};
    143 static const vp9_prob Pcat3[] = { 173, 148, 140};
    144 static const vp9_prob Pcat4[] = { 176, 155, 140, 135};
    145 static const vp9_prob Pcat5[] = { 180, 157, 141, 134, 130};
    146 static const vp9_prob Pcat6[] = {
    147   254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129
    148 };
    149 
    150 const vp9_tree_index vp9_coefmodel_tree[6] = {
    151   -DCT_EOB_MODEL_TOKEN, 2,                      /* 0 = EOB */
    152   -ZERO_TOKEN, 4,                               /* 1 = ZERO */
    153   -ONE_TOKEN, -TWO_TOKEN,
    154 };
    155 
    156 // Model obtained from a 2-sided zero-centerd distribuition derived
    157 // from a Pareto distribution. The cdf of the distribution is:
    158 // cdf(x) = 0.5 + 0.5 * sgn(x) * [1 - {alpha/(alpha + |x|)} ^ beta]
    159 //
    160 // For a given beta and a given probablity of the 1-node, the alpha
    161 // is first solved, and then the {alpha, beta} pair is used to generate
    162 // the probabilities for the rest of the nodes.
    163 
    164 // beta = 8
    165 static const vp9_prob modelcoefprobs_pareto8[COEFPROB_MODELS][MODEL_NODES] = {
    166   {  3,  86, 128,   6,  86,  23,  88,  29},
    167   {  9,  86, 129,  17,  88,  61,  94,  76},
    168   { 15,  87, 129,  28,  89,  93, 100, 110},
    169   { 20,  88, 130,  38,  91, 118, 106, 136},
    170   { 26,  89, 131,  48,  92, 139, 111, 156},
    171   { 31,  90, 131,  58,  94, 156, 117, 171},
    172   { 37,  90, 132,  66,  95, 171, 122, 184},
    173   { 42,  91, 132,  75,  97, 183, 127, 194},
    174   { 47,  92, 133,  83,  98, 193, 132, 202},
    175   { 52,  93, 133,  90, 100, 201, 137, 208},
    176   { 57,  94, 134,  98, 101, 208, 142, 214},
    177   { 62,  94, 135, 105, 103, 214, 146, 218},
    178   { 66,  95, 135, 111, 104, 219, 151, 222},
    179   { 71,  96, 136, 117, 106, 224, 155, 225},
    180   { 76,  97, 136, 123, 107, 227, 159, 228},
    181   { 80,  98, 137, 129, 109, 231, 162, 231},
    182   { 84,  98, 138, 134, 110, 234, 166, 233},
    183   { 89,  99, 138, 140, 112, 236, 170, 235},
    184   { 93, 100, 139, 145, 113, 238, 173, 236},
    185   { 97, 101, 140, 149, 115, 240, 176, 238},
    186   {101, 102, 140, 154, 116, 242, 179, 239},
    187   {105, 103, 141, 158, 118, 243, 182, 240},
    188   {109, 104, 141, 162, 119, 244, 185, 241},
    189   {113, 104, 142, 166, 120, 245, 187, 242},
    190   {116, 105, 143, 170, 122, 246, 190, 243},
    191   {120, 106, 143, 173, 123, 247, 192, 244},
    192   {123, 107, 144, 177, 125, 248, 195, 244},
    193   {127, 108, 145, 180, 126, 249, 197, 245},
    194   {130, 109, 145, 183, 128, 249, 199, 245},
    195   {134, 110, 146, 186, 129, 250, 201, 246},
    196   {137, 111, 147, 189, 131, 251, 203, 246},
    197   {140, 112, 147, 192, 132, 251, 205, 247},
    198   {143, 113, 148, 194, 133, 251, 207, 247},
    199   {146, 114, 149, 197, 135, 252, 208, 248},
    200   {149, 115, 149, 199, 136, 252, 210, 248},
    201   {152, 115, 150, 201, 138, 252, 211, 248},
    202   {155, 116, 151, 204, 139, 253, 213, 249},
    203   {158, 117, 151, 206, 140, 253, 214, 249},
    204   {161, 118, 152, 208, 142, 253, 216, 249},
    205   {163, 119, 153, 210, 143, 253, 217, 249},
    206   {166, 120, 153, 212, 144, 254, 218, 250},
    207   {168, 121, 154, 213, 146, 254, 220, 250},
    208   {171, 122, 155, 215, 147, 254, 221, 250},
    209   {173, 123, 155, 217, 148, 254, 222, 250},
    210   {176, 124, 156, 218, 150, 254, 223, 250},
    211   {178, 125, 157, 220, 151, 254, 224, 251},
    212   {180, 126, 157, 221, 152, 254, 225, 251},
    213   {183, 127, 158, 222, 153, 254, 226, 251},
    214   {185, 128, 159, 224, 155, 255, 227, 251},
    215   {187, 129, 160, 225, 156, 255, 228, 251},
    216   {189, 131, 160, 226, 157, 255, 228, 251},
    217   {191, 132, 161, 227, 159, 255, 229, 251},
    218   {193, 133, 162, 228, 160, 255, 230, 252},
    219   {195, 134, 163, 230, 161, 255, 231, 252},
    220   {197, 135, 163, 231, 162, 255, 231, 252},
    221   {199, 136, 164, 232, 163, 255, 232, 252},
    222   {201, 137, 165, 233, 165, 255, 233, 252},
    223   {202, 138, 166, 233, 166, 255, 233, 252},
    224   {204, 139, 166, 234, 167, 255, 234, 252},
    225   {206, 140, 167, 235, 168, 255, 235, 252},
    226   {207, 141, 168, 236, 169, 255, 235, 252},
    227   {209, 142, 169, 237, 171, 255, 236, 252},
    228   {210, 144, 169, 237, 172, 255, 236, 252},
    229   {212, 145, 170, 238, 173, 255, 237, 252},
    230   {214, 146, 171, 239, 174, 255, 237, 253},
    231   {215, 147, 172, 240, 175, 255, 238, 253},
    232   {216, 148, 173, 240, 176, 255, 238, 253},
    233   {218, 149, 173, 241, 177, 255, 239, 253},
    234   {219, 150, 174, 241, 179, 255, 239, 253},
    235   {220, 152, 175, 242, 180, 255, 240, 253},
    236   {222, 153, 176, 242, 181, 255, 240, 253},
    237   {223, 154, 177, 243, 182, 255, 240, 253},
    238   {224, 155, 178, 244, 183, 255, 241, 253},
    239   {225, 156, 178, 244, 184, 255, 241, 253},
    240   {226, 158, 179, 244, 185, 255, 242, 253},
    241   {228, 159, 180, 245, 186, 255, 242, 253},
    242   {229, 160, 181, 245, 187, 255, 242, 253},
    243   {230, 161, 182, 246, 188, 255, 243, 253},
    244   {231, 163, 183, 246, 189, 255, 243, 253},
    245   {232, 164, 184, 247, 190, 255, 243, 253},
    246   {233, 165, 185, 247, 191, 255, 244, 253},
    247   {234, 166, 185, 247, 192, 255, 244, 253},
    248   {235, 168, 186, 248, 193, 255, 244, 253},
    249   {236, 169, 187, 248, 194, 255, 244, 253},
    250   {236, 170, 188, 248, 195, 255, 245, 253},
    251   {237, 171, 189, 249, 196, 255, 245, 254},
    252   {238, 173, 190, 249, 197, 255, 245, 254},
    253   {239, 174, 191, 249, 198, 255, 245, 254},
    254   {240, 175, 192, 249, 199, 255, 246, 254},
    255   {240, 177, 193, 250, 200, 255, 246, 254},
    256   {241, 178, 194, 250, 201, 255, 246, 254},
    257   {242, 179, 195, 250, 202, 255, 246, 254},
    258   {242, 181, 196, 250, 203, 255, 247, 254},
    259   {243, 182, 197, 251, 204, 255, 247, 254},
    260   {244, 184, 198, 251, 205, 255, 247, 254},
    261   {244, 185, 199, 251, 206, 255, 247, 254},
    262   {245, 186, 200, 251, 207, 255, 247, 254},
    263   {246, 188, 201, 252, 207, 255, 248, 254},
    264   {246, 189, 202, 252, 208, 255, 248, 254},
    265   {247, 191, 203, 252, 209, 255, 248, 254},
    266   {247, 192, 204, 252, 210, 255, 248, 254},
    267   {248, 194, 205, 252, 211, 255, 248, 254},
    268   {248, 195, 206, 252, 212, 255, 249, 254},
    269   {249, 197, 207, 253, 213, 255, 249, 254},
    270   {249, 198, 208, 253, 214, 255, 249, 254},
    271   {250, 200, 210, 253, 215, 255, 249, 254},
    272   {250, 201, 211, 253, 215, 255, 249, 254},
    273   {250, 203, 212, 253, 216, 255, 249, 254},
    274   {251, 204, 213, 253, 217, 255, 250, 254},
    275   {251, 206, 214, 254, 218, 255, 250, 254},
    276   {252, 207, 216, 254, 219, 255, 250, 254},
    277   {252, 209, 217, 254, 220, 255, 250, 254},
    278   {252, 211, 218, 254, 221, 255, 250, 254},
    279   {253, 213, 219, 254, 222, 255, 250, 254},
    280   {253, 214, 221, 254, 223, 255, 250, 254},
    281   {253, 216, 222, 254, 224, 255, 251, 254},
    282   {253, 218, 224, 254, 225, 255, 251, 254},
    283   {254, 220, 225, 254, 225, 255, 251, 254},
    284   {254, 222, 227, 255, 226, 255, 251, 254},
    285   {254, 224, 228, 255, 227, 255, 251, 254},
    286   {254, 226, 230, 255, 228, 255, 251, 254},
    287   {255, 228, 231, 255, 230, 255, 251, 254},
    288   {255, 230, 233, 255, 231, 255, 252, 254},
    289   {255, 232, 235, 255, 232, 255, 252, 254},
    290   {255, 235, 237, 255, 233, 255, 252, 254},
    291   {255, 238, 240, 255, 235, 255, 252, 255},
    292   {255, 241, 243, 255, 236, 255, 252, 254},
    293   {255, 246, 247, 255, 239, 255, 253, 255}
    294 };
    295 
    296 static void extend_model_to_full_distribution(vp9_prob p,
    297                                               vp9_prob *tree_probs) {
    298   const int l = (p - 1) / 2;
    299   const vp9_prob (*model)[MODEL_NODES] = modelcoefprobs_pareto8;
    300   if (p & 1) {
    301     vpx_memcpy(tree_probs + UNCONSTRAINED_NODES,
    302                model[l], MODEL_NODES * sizeof(vp9_prob));
    303   } else {
    304     // interpolate
    305     int i;
    306     for (i = UNCONSTRAINED_NODES; i < ENTROPY_NODES; ++i)
    307       tree_probs[i] = (model[l][i - UNCONSTRAINED_NODES] +
    308                        model[l + 1][i - UNCONSTRAINED_NODES]) >> 1;
    309   }
    310 }
    311 
    312 void vp9_model_to_full_probs(const vp9_prob *model, vp9_prob *full) {
    313   if (full != model)
    314     vpx_memcpy(full, model, sizeof(vp9_prob) * UNCONSTRAINED_NODES);
    315   extend_model_to_full_distribution(model[PIVOT_NODE], full);
    316 }
    317 
    318 static vp9_tree_index cat1[2], cat2[4], cat3[6], cat4[8], cat5[10], cat6[28];
    319 
    320 static void init_bit_tree(vp9_tree_index *p, int n) {
    321   int i = 0;
    322 
    323   while (++i < n) {
    324     p[0] = p[1] = i << 1;
    325     p += 2;
    326   }
    327 
    328   p[0] = p[1] = 0;
    329 }
    330 
    331 static void init_bit_trees() {
    332   init_bit_tree(cat1, 1);
    333   init_bit_tree(cat2, 2);
    334   init_bit_tree(cat3, 3);
    335   init_bit_tree(cat4, 4);
    336   init_bit_tree(cat5, 5);
    337   init_bit_tree(cat6, 14);
    338 }
    339 
    340 const vp9_extra_bit vp9_extra_bits[MAX_ENTROPY_TOKENS] = {
    341   { 0, 0, 0, 0},
    342   { 0, 0, 0, 1},
    343   { 0, 0, 0, 2},
    344   { 0, 0, 0, 3},
    345   { 0, 0, 0, 4},
    346   { cat1, Pcat1, 1, 5},
    347   { cat2, Pcat2, 2, 7},
    348   { cat3, Pcat3, 3, 11},
    349   { cat4, Pcat4, 4, 19},
    350   { cat5, Pcat5, 5, 35},
    351   { cat6, Pcat6, 14, 67},
    352   { 0, 0, 0, 0}
    353 };
    354 
    355 #include "vp9/common/vp9_default_coef_probs.h"
    356 
    357 void vp9_default_coef_probs(VP9_COMMON *cm) {
    358   vp9_copy(cm->fc.coef_probs[TX_4X4], default_coef_probs_4x4);
    359   vp9_copy(cm->fc.coef_probs[TX_8X8], default_coef_probs_8x8);
    360   vp9_copy(cm->fc.coef_probs[TX_16X16], default_coef_probs_16x16);
    361   vp9_copy(cm->fc.coef_probs[TX_32X32], default_coef_probs_32x32);
    362 }
    363 
    364 void vp9_coef_tree_initialize() {
    365   init_bit_trees();
    366   vp9_tokens_from_tree(vp9_coef_encodings, vp9_coef_tree);
    367 }
    368 
    369 // #define COEF_COUNT_TESTING
    370 
    371 #define COEF_COUNT_SAT 24
    372 #define COEF_MAX_UPDATE_FACTOR 112
    373 #define COEF_COUNT_SAT_KEY 24
    374 #define COEF_MAX_UPDATE_FACTOR_KEY 112
    375 #define COEF_COUNT_SAT_AFTER_KEY 24
    376 #define COEF_MAX_UPDATE_FACTOR_AFTER_KEY 128
    377 
    378 static void adapt_coef_probs(VP9_COMMON *cm, TX_SIZE tx_size,
    379                              unsigned int count_sat,
    380                              unsigned int update_factor) {
    381   const FRAME_CONTEXT *pre_fc = &cm->frame_contexts[cm->frame_context_idx];
    382 
    383   vp9_coeff_probs_model *dst_coef_probs = cm->fc.coef_probs[tx_size];
    384   const vp9_coeff_probs_model *pre_coef_probs = pre_fc->coef_probs[tx_size];
    385   vp9_coeff_count_model *coef_counts = cm->counts.coef[tx_size];
    386   unsigned int (*eob_branch_count)[REF_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS] =
    387       cm->counts.eob_branch[tx_size];
    388   int i, j, k, l, m;
    389   unsigned int branch_ct[UNCONSTRAINED_NODES][2];
    390 
    391   for (i = 0; i < BLOCK_TYPES; ++i)
    392     for (j = 0; j < REF_TYPES; ++j)
    393       for (k = 0; k < COEF_BANDS; ++k)
    394         for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
    395           if (l >= 3 && k == 0)
    396             continue;
    397           vp9_tree_probs_from_distribution(vp9_coefmodel_tree, branch_ct,
    398                                            coef_counts[i][j][k][l]);
    399           branch_ct[0][1] = eob_branch_count[i][j][k][l] - branch_ct[0][0];
    400           for (m = 0; m < UNCONSTRAINED_NODES; ++m)
    401             dst_coef_probs[i][j][k][l][m] = merge_probs(
    402                                                 pre_coef_probs[i][j][k][l][m],
    403                                                 branch_ct[m],
    404                                                 count_sat, update_factor);
    405         }
    406 }
    407 
    408 void vp9_adapt_coef_probs(VP9_COMMON *cm) {
    409   TX_SIZE t;
    410   unsigned int count_sat, update_factor;
    411 
    412   if (frame_is_intra_only(cm)) {
    413     update_factor = COEF_MAX_UPDATE_FACTOR_KEY;
    414     count_sat = COEF_COUNT_SAT_KEY;
    415   } else if (cm->last_frame_type == KEY_FRAME) {
    416     update_factor = COEF_MAX_UPDATE_FACTOR_AFTER_KEY;  /* adapt quickly */
    417     count_sat = COEF_COUNT_SAT_AFTER_KEY;
    418   } else {
    419     update_factor = COEF_MAX_UPDATE_FACTOR;
    420     count_sat = COEF_COUNT_SAT;
    421   }
    422   for (t = TX_4X4; t <= TX_32X32; t++)
    423     adapt_coef_probs(cm, t, count_sat, update_factor);
    424 }
    425