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