• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "vpx_mem/vpx_mem.h"
12 #include "vpx_ports/mem.h"
13 
14 #include "vp9/common/vp9_blockd.h"
15 #include "vp9/common/vp9_common.h"
16 #include "vp9/common/vp9_entropy.h"
17 
18 #include "vp9/decoder/vp9_detokenize.h"
19 
20 #define EOB_CONTEXT_NODE            0
21 #define ZERO_CONTEXT_NODE           1
22 #define ONE_CONTEXT_NODE            2
23 #define LOW_VAL_CONTEXT_NODE        0
24 #define TWO_CONTEXT_NODE            1
25 #define THREE_CONTEXT_NODE          2
26 #define HIGH_LOW_CONTEXT_NODE       3
27 #define CAT_ONE_CONTEXT_NODE        4
28 #define CAT_THREEFOUR_CONTEXT_NODE  5
29 #define CAT_THREE_CONTEXT_NODE      6
30 #define CAT_FIVE_CONTEXT_NODE       7
31 
32 #define INCREMENT_COUNT(token)                              \
33   do {                                                      \
34      if (!cm->frame_parallel_decoding_mode)                 \
35        ++coef_counts[band][ctx][token];                      \
36   } while (0)
37 
read_coeff(const vp9_prob * probs,int n,vp9_reader * r)38 static INLINE int read_coeff(const vp9_prob *probs, int n, vp9_reader *r) {
39   int i, val = 0;
40   for (i = 0; i < n; ++i)
41     val = (val << 1) | vp9_read(r, probs[i]);
42   return val;
43 }
44 
45 static const vp9_tree_index coeff_subtree_high[TREE_SIZE(ENTROPY_TOKENS)] = {
46   2, 6,                                         /* 0 = LOW_VAL */
47   -TWO_TOKEN, 4,                                /* 1 = TWO */
48   -THREE_TOKEN, -FOUR_TOKEN,                    /* 2 = THREE */
49   8, 10,                                        /* 3 = HIGH_LOW */
50   -CATEGORY1_TOKEN, -CATEGORY2_TOKEN,           /* 4 = CAT_ONE */
51   12, 14,                                       /* 5 = CAT_THREEFOUR */
52   -CATEGORY3_TOKEN, -CATEGORY4_TOKEN,           /* 6 = CAT_THREE */
53   -CATEGORY5_TOKEN, -CATEGORY6_TOKEN            /* 7 = CAT_FIVE */
54 };
55 
decode_coefs(VP9_COMMON * cm,const MACROBLOCKD * xd,PLANE_TYPE type,tran_low_t * dqcoeff,TX_SIZE tx_size,const int16_t * dq,int ctx,const int16_t * scan,const int16_t * nb,vp9_reader * r)56 static int decode_coefs(VP9_COMMON *cm, const MACROBLOCKD *xd, PLANE_TYPE type,
57                         tran_low_t *dqcoeff, TX_SIZE tx_size, const int16_t *dq,
58                         int ctx, const int16_t *scan, const int16_t *nb,
59                         vp9_reader *r) {
60   const int max_eob = 16 << (tx_size << 1);
61   const FRAME_CONTEXT *const fc = &cm->fc;
62   FRAME_COUNTS *const counts = &cm->counts;
63   const int ref = is_inter_block(&xd->mi[0].src_mi->mbmi);
64   int band, c = 0;
65   const vp9_prob (*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
66       fc->coef_probs[tx_size][type][ref];
67   const vp9_prob *prob;
68   unsigned int (*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1] =
69       counts->coef[tx_size][type][ref];
70   unsigned int (*eob_branch_count)[COEFF_CONTEXTS] =
71       counts->eob_branch[tx_size][type][ref];
72   uint8_t token_cache[32 * 32];
73   const uint8_t *band_translate = get_band_translate(tx_size);
74   const int dq_shift = (tx_size == TX_32X32);
75   int v, token;
76   int16_t dqv = dq[0];
77   const uint8_t *cat1_prob;
78   const uint8_t *cat2_prob;
79   const uint8_t *cat3_prob;
80   const uint8_t *cat4_prob;
81   const uint8_t *cat5_prob;
82   const uint8_t *cat6_prob;
83 
84 #if CONFIG_VP9_HIGHBITDEPTH
85   if (cm->use_highbitdepth) {
86     if (cm->bit_depth == VPX_BITS_10) {
87       cat1_prob = vp9_cat1_prob_high10;
88       cat2_prob = vp9_cat2_prob_high10;
89       cat3_prob = vp9_cat3_prob_high10;
90       cat4_prob = vp9_cat4_prob_high10;
91       cat5_prob = vp9_cat5_prob_high10;
92       cat6_prob = vp9_cat6_prob_high10;
93     } else {
94       cat1_prob = vp9_cat1_prob_high12;
95       cat2_prob = vp9_cat2_prob_high12;
96       cat3_prob = vp9_cat3_prob_high12;
97       cat4_prob = vp9_cat4_prob_high12;
98       cat5_prob = vp9_cat5_prob_high12;
99       cat6_prob = vp9_cat6_prob_high12;
100     }
101   } else {
102     cat1_prob = vp9_cat1_prob;
103     cat2_prob = vp9_cat2_prob;
104     cat3_prob = vp9_cat3_prob;
105     cat4_prob = vp9_cat4_prob;
106     cat5_prob = vp9_cat5_prob;
107     cat6_prob = vp9_cat6_prob;
108   }
109 #else
110   cat1_prob = vp9_cat1_prob;
111   cat2_prob = vp9_cat2_prob;
112   cat3_prob = vp9_cat3_prob;
113   cat4_prob = vp9_cat4_prob;
114   cat5_prob = vp9_cat5_prob;
115   cat6_prob = vp9_cat6_prob;
116 #endif
117 
118   while (c < max_eob) {
119     int val = -1;
120     band = *band_translate++;
121     prob = coef_probs[band][ctx];
122     if (!cm->frame_parallel_decoding_mode)
123       ++eob_branch_count[band][ctx];
124     if (!vp9_read(r, prob[EOB_CONTEXT_NODE])) {
125       INCREMENT_COUNT(EOB_MODEL_TOKEN);
126       break;
127     }
128 
129     while (!vp9_read(r, prob[ZERO_CONTEXT_NODE])) {
130       INCREMENT_COUNT(ZERO_TOKEN);
131       dqv = dq[1];
132       token_cache[scan[c]] = 0;
133       ++c;
134       if (c >= max_eob)
135         return c;  // zero tokens at the end (no eob token)
136       ctx = get_coef_context(nb, token_cache, c);
137       band = *band_translate++;
138       prob = coef_probs[band][ctx];
139     }
140 
141     if (!vp9_read(r, prob[ONE_CONTEXT_NODE])) {
142       INCREMENT_COUNT(ONE_TOKEN);
143       token = ONE_TOKEN;
144       val = 1;
145     } else {
146       INCREMENT_COUNT(TWO_TOKEN);
147       token = vp9_read_tree(r, coeff_subtree_high,
148                             vp9_pareto8_full[prob[PIVOT_NODE] - 1]);
149       switch (token) {
150         case TWO_TOKEN:
151         case THREE_TOKEN:
152         case FOUR_TOKEN:
153           val = token;
154           break;
155         case CATEGORY1_TOKEN:
156           val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, r);
157           break;
158         case CATEGORY2_TOKEN:
159           val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, r);
160           break;
161         case CATEGORY3_TOKEN:
162           val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, r);
163           break;
164         case CATEGORY4_TOKEN:
165           val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, r);
166           break;
167         case CATEGORY5_TOKEN:
168           val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, r);
169           break;
170         case CATEGORY6_TOKEN:
171 #if CONFIG_VP9_HIGHBITDEPTH
172           switch (cm->bit_depth) {
173             case VPX_BITS_8:
174               val = CAT6_MIN_VAL + read_coeff(cat6_prob, 14, r);
175               break;
176             case VPX_BITS_10:
177               val = CAT6_MIN_VAL + read_coeff(cat6_prob, 16, r);
178               break;
179             case VPX_BITS_12:
180               val = CAT6_MIN_VAL + read_coeff(cat6_prob, 18, r);
181               break;
182             default:
183               assert(0);
184               return -1;
185           }
186 #else
187           val = CAT6_MIN_VAL + read_coeff(cat6_prob, 14, r);
188 #endif
189           break;
190       }
191     }
192     v = (val * dqv) >> dq_shift;
193     dqcoeff[scan[c]] = vp9_read_bit(r) ? -v : v;
194     token_cache[scan[c]] = vp9_pt_energy_class[token];
195     ++c;
196     ctx = get_coef_context(nb, token_cache, c);
197     dqv = dq[1];
198   }
199 
200   return c;
201 }
202 
vp9_decode_block_tokens(VP9_COMMON * cm,MACROBLOCKD * xd,int plane,int block,BLOCK_SIZE plane_bsize,int x,int y,TX_SIZE tx_size,vp9_reader * r)203 int vp9_decode_block_tokens(VP9_COMMON *cm, MACROBLOCKD *xd,
204                             int plane, int block, BLOCK_SIZE plane_bsize,
205                             int x, int y, TX_SIZE tx_size, vp9_reader *r) {
206   struct macroblockd_plane *const pd = &xd->plane[plane];
207   const int ctx = get_entropy_context(tx_size, pd->above_context + x,
208                                                pd->left_context + y);
209   const scan_order *so = get_scan(xd, tx_size, pd->plane_type, block);
210   const int eob = decode_coefs(cm, xd, pd->plane_type,
211                                BLOCK_OFFSET(pd->dqcoeff, block), tx_size,
212                                pd->dequant, ctx, so->scan, so->neighbors, r);
213   vp9_set_contexts(xd, pd, plane_bsize, tx_size, eob > 0, x, y);
214   return eob;
215 }
216 
217 
218