Merge "Fix high bit depth in vp10 codebase"
[platform/upstream/libvpx.git] / vp10 / 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 "vp10/common/vp9_blockd.h"
15 #include "vp10/common/vp9_common.h"
16 #include "vp10/common/vp9_entropy.h"
17 #if CONFIG_COEFFICIENT_RANGE_CHECKING
18 #include "vp10/common/vp9_idct.h"
19 #endif
20
21 #include "vp10/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 (counts)                                            \
38        ++coef_counts[band][ctx][token];                     \
39   } while (0)
40
41 static INLINE int read_coeff(const vpx_prob *probs, int n, vpx_reader *r) {
42   int i, val = 0;
43   for (i = 0; i < n; ++i)
44     val = (val << 1) | vpx_read(r, probs[i]);
45   return val;
46 }
47
48 static int decode_coefs(const MACROBLOCKD *xd,
49                         PLANE_TYPE type,
50                         tran_low_t *dqcoeff, TX_SIZE tx_size, const int16_t *dq,
51                         int ctx, const int16_t *scan, const int16_t *nb,
52                         vpx_reader *r) {
53   FRAME_COUNTS *counts = xd->counts;
54   const int max_eob = 16 << (tx_size << 1);
55   const FRAME_CONTEXT *const fc = xd->fc;
56   const int ref = is_inter_block(&xd->mi[0]->mbmi);
57   int band, c = 0;
58   const vpx_prob (*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
59       fc->coef_probs[tx_size][type][ref];
60   const vpx_prob *prob;
61   unsigned int (*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
62   unsigned int (*eob_branch_count)[COEFF_CONTEXTS];
63   uint8_t token_cache[32 * 32];
64   const uint8_t *band_translate = get_band_translate(tx_size);
65   const int dq_shift = (tx_size == TX_32X32);
66   int v, token;
67   int16_t dqv = dq[0];
68   const uint8_t *cat1_prob;
69   const uint8_t *cat2_prob;
70   const uint8_t *cat3_prob;
71   const uint8_t *cat4_prob;
72   const uint8_t *cat5_prob;
73   const uint8_t *cat6_prob;
74
75   if (counts) {
76     coef_counts = counts->coef[tx_size][type][ref];
77     eob_branch_count = counts->eob_branch[tx_size][type][ref];
78   }
79
80 #if CONFIG_VP9_HIGHBITDEPTH
81   if (xd->bd > VPX_BITS_8) {
82     if (xd->bd == VPX_BITS_10) {
83       cat1_prob = vp10_cat1_prob_high10;
84       cat2_prob = vp10_cat2_prob_high10;
85       cat3_prob = vp10_cat3_prob_high10;
86       cat4_prob = vp10_cat4_prob_high10;
87       cat5_prob = vp10_cat5_prob_high10;
88       cat6_prob = vp10_cat6_prob_high10;
89     } else {
90       cat1_prob = vp10_cat1_prob_high12;
91       cat2_prob = vp10_cat2_prob_high12;
92       cat3_prob = vp10_cat3_prob_high12;
93       cat4_prob = vp10_cat4_prob_high12;
94       cat5_prob = vp10_cat5_prob_high12;
95       cat6_prob = vp10_cat6_prob_high12;
96     }
97   } else {
98     cat1_prob = vp10_cat1_prob;
99     cat2_prob = vp10_cat2_prob;
100     cat3_prob = vp10_cat3_prob;
101     cat4_prob = vp10_cat4_prob;
102     cat5_prob = vp10_cat5_prob;
103     cat6_prob = vp10_cat6_prob;
104   }
105 #else
106   cat1_prob = vp10_cat1_prob;
107   cat2_prob = vp10_cat2_prob;
108   cat3_prob = vp10_cat3_prob;
109   cat4_prob = vp10_cat4_prob;
110   cat5_prob = vp10_cat5_prob;
111   cat6_prob = vp10_cat6_prob;
112 #endif
113
114   while (c < max_eob) {
115     int val = -1;
116     band = *band_translate++;
117     prob = coef_probs[band][ctx];
118     if (counts)
119       ++eob_branch_count[band][ctx];
120     if (!vpx_read(r, prob[EOB_CONTEXT_NODE])) {
121       INCREMENT_COUNT(EOB_MODEL_TOKEN);
122       break;
123     }
124
125     while (!vpx_read(r, prob[ZERO_CONTEXT_NODE])) {
126       INCREMENT_COUNT(ZERO_TOKEN);
127       dqv = dq[1];
128       token_cache[scan[c]] = 0;
129       ++c;
130       if (c >= max_eob)
131         return c;  // zero tokens at the end (no eob token)
132       ctx = get_coef_context(nb, token_cache, c);
133       band = *band_translate++;
134       prob = coef_probs[band][ctx];
135     }
136
137     if (!vpx_read(r, prob[ONE_CONTEXT_NODE])) {
138       INCREMENT_COUNT(ONE_TOKEN);
139       token = ONE_TOKEN;
140       val = 1;
141     } else {
142       INCREMENT_COUNT(TWO_TOKEN);
143       token = vpx_read_tree(r, vp10_coef_con_tree,
144                             vp10_pareto8_full[prob[PIVOT_NODE] - 1]);
145       switch (token) {
146         case TWO_TOKEN:
147         case THREE_TOKEN:
148         case FOUR_TOKEN:
149           val = token;
150           break;
151         case CATEGORY1_TOKEN:
152           val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, r);
153           break;
154         case CATEGORY2_TOKEN:
155           val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, r);
156           break;
157         case CATEGORY3_TOKEN:
158           val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, r);
159           break;
160         case CATEGORY4_TOKEN:
161           val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, r);
162           break;
163         case CATEGORY5_TOKEN:
164           val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, r);
165           break;
166         case CATEGORY6_TOKEN:
167 #if CONFIG_VP9_HIGHBITDEPTH
168           switch (xd->bd) {
169             case VPX_BITS_8:
170               val = CAT6_MIN_VAL + read_coeff(cat6_prob, 14, r);
171               break;
172             case VPX_BITS_10:
173               val = CAT6_MIN_VAL + read_coeff(cat6_prob, 16, r);
174               break;
175             case VPX_BITS_12:
176               val = CAT6_MIN_VAL + read_coeff(cat6_prob, 18, r);
177               break;
178             default:
179               assert(0);
180               return -1;
181           }
182 #else
183           val = CAT6_MIN_VAL + read_coeff(cat6_prob, 14, r);
184 #endif
185           break;
186       }
187     }
188     v = (val * dqv) >> dq_shift;
189 #if CONFIG_COEFFICIENT_RANGE_CHECKING
190 #if CONFIG_VP9_HIGHBITDEPTH
191     dqcoeff[scan[c]] = highbd_check_range((vpx_read_bit(r) ? -v : v),
192                                           xd->bd);
193 #else
194     dqcoeff[scan[c]] = check_range(vpx_read_bit(r) ? -v : v);
195 #endif  // CONFIG_VP9_HIGHBITDEPTH
196 #else
197     dqcoeff[scan[c]] = vpx_read_bit(r) ? -v : v;
198 #endif  // CONFIG_COEFFICIENT_RANGE_CHECKING
199     token_cache[scan[c]] = vp10_pt_energy_class[token];
200     ++c;
201     ctx = get_coef_context(nb, token_cache, c);
202     dqv = dq[1];
203   }
204
205   return c;
206 }
207
208 // TODO(slavarnway): Decode version of vp10_set_context.  Modify vp10_set_context
209 // after testing is complete, then delete this version.
210 static
211 void dec_set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
212                       TX_SIZE tx_size, int has_eob,
213                       int aoff, int loff) {
214   ENTROPY_CONTEXT *const a = pd->above_context + aoff;
215   ENTROPY_CONTEXT *const l = pd->left_context + loff;
216   const int tx_size_in_blocks = 1 << tx_size;
217
218   // above
219   if (has_eob && xd->mb_to_right_edge < 0) {
220     int i;
221     const int blocks_wide = pd->n4_w +
222                             (xd->mb_to_right_edge >> (5 + pd->subsampling_x));
223     int above_contexts = tx_size_in_blocks;
224     if (above_contexts + aoff > blocks_wide)
225       above_contexts = blocks_wide - aoff;
226
227     for (i = 0; i < above_contexts; ++i)
228       a[i] = has_eob;
229     for (i = above_contexts; i < tx_size_in_blocks; ++i)
230       a[i] = 0;
231   } else {
232     memset(a, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
233   }
234
235   // left
236   if (has_eob && xd->mb_to_bottom_edge < 0) {
237     int i;
238     const int blocks_high = pd->n4_h +
239                             (xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
240     int left_contexts = tx_size_in_blocks;
241     if (left_contexts + loff > blocks_high)
242       left_contexts = blocks_high - loff;
243
244     for (i = 0; i < left_contexts; ++i)
245       l[i] = has_eob;
246     for (i = left_contexts; i < tx_size_in_blocks; ++i)
247       l[i] = 0;
248   } else {
249     memset(l, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
250   }
251 }
252
253 int vp10_decode_block_tokens(MACROBLOCKD *xd,
254                             int plane, const scan_order *sc,
255                             int x, int y,
256                             TX_SIZE tx_size, vpx_reader *r,
257                             int seg_id) {
258   struct macroblockd_plane *const pd = &xd->plane[plane];
259   const int16_t *const dequant = pd->seg_dequant[seg_id];
260   const int ctx = get_entropy_context(tx_size, pd->above_context + x,
261                                                pd->left_context + y);
262   const int eob = decode_coefs(xd, pd->plane_type,
263                                pd->dqcoeff, tx_size,
264                                dequant, ctx, sc->scan, sc->neighbors, r);
265   dec_set_contexts(xd, pd, tx_size, eob > 0, x, y);
266   return eob;
267 }
268
269