• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include "av1/decoder/decodetxb.h"
13 
14 #include "aom_ports/mem.h"
15 #include "av1/common/idct.h"
16 #include "av1/common/scan.h"
17 #include "av1/common/txb_common.h"
18 #include "av1/decoder/decodemv.h"
19 
20 #define ACCT_STR __func__
21 
read_golomb(MACROBLOCKD * xd,aom_reader * r)22 static int read_golomb(MACROBLOCKD *xd, aom_reader *r) {
23   int x = 1;
24   int length = 0;
25   int i = 0;
26 
27   while (!i) {
28     i = aom_read_bit(r, ACCT_STR);
29     ++length;
30     if (length > 20) {
31       aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
32                          "Invalid length in read_golomb");
33       break;
34     }
35   }
36 
37   for (i = 0; i < length - 1; ++i) {
38     x <<= 1;
39     x += aom_read_bit(r, ACCT_STR);
40   }
41 
42   return x - 1;
43 }
44 
rec_eob_pos(const int eob_token,const int extra)45 static INLINE int rec_eob_pos(const int eob_token, const int extra) {
46   int eob = av1_eob_group_start[eob_token];
47   if (eob > 2) {
48     eob += extra;
49   }
50   return eob;
51 }
52 
get_dqv(const int16_t * dequant,int coeff_idx,const qm_val_t * iqmatrix)53 static INLINE int get_dqv(const int16_t *dequant, int coeff_idx,
54                           const qm_val_t *iqmatrix) {
55   int dqv = dequant[!!coeff_idx];
56   if (iqmatrix != NULL)
57     dqv =
58         ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
59   return dqv;
60 }
61 
read_coeffs_reverse_2d(aom_reader * r,TX_SIZE tx_size,int start_si,int end_si,const int16_t * scan,int bwl,uint8_t * levels,base_cdf_arr base_cdf,br_cdf_arr br_cdf)62 static INLINE void read_coeffs_reverse_2d(aom_reader *r, TX_SIZE tx_size,
63                                           int start_si, int end_si,
64                                           const int16_t *scan, int bwl,
65                                           uint8_t *levels,
66                                           base_cdf_arr base_cdf,
67                                           br_cdf_arr br_cdf) {
68   for (int c = end_si; c >= start_si; --c) {
69     const int pos = scan[c];
70     const int coeff_ctx = get_lower_levels_ctx_2d(levels, pos, bwl, tx_size);
71     const int nsymbs = 4;
72     int level = aom_read_symbol(r, base_cdf[coeff_ctx], nsymbs, ACCT_STR);
73     if (level > NUM_BASE_LEVELS) {
74       const int br_ctx = get_br_ctx_2d(levels, pos, bwl);
75       aom_cdf_prob *cdf = br_cdf[br_ctx];
76       for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
77         const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR);
78         level += k;
79         if (k < BR_CDF_SIZE - 1) break;
80       }
81     }
82     levels[get_padded_idx(pos, bwl)] = level;
83   }
84 }
85 
read_coeffs_reverse(aom_reader * r,TX_SIZE tx_size,TX_CLASS tx_class,int start_si,int end_si,const int16_t * scan,int bwl,uint8_t * levels,base_cdf_arr base_cdf,br_cdf_arr br_cdf)86 static INLINE void read_coeffs_reverse(aom_reader *r, TX_SIZE tx_size,
87                                        TX_CLASS tx_class, int start_si,
88                                        int end_si, const int16_t *scan, int bwl,
89                                        uint8_t *levels, base_cdf_arr base_cdf,
90                                        br_cdf_arr br_cdf) {
91   for (int c = end_si; c >= start_si; --c) {
92     const int pos = scan[c];
93     const int coeff_ctx =
94         get_lower_levels_ctx(levels, pos, bwl, tx_size, tx_class);
95     const int nsymbs = 4;
96     int level = aom_read_symbol(r, base_cdf[coeff_ctx], nsymbs, ACCT_STR);
97     if (level > NUM_BASE_LEVELS) {
98       const int br_ctx = get_br_ctx(levels, pos, bwl, tx_class);
99       aom_cdf_prob *cdf = br_cdf[br_ctx];
100       for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
101         const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR);
102         level += k;
103         if (k < BR_CDF_SIZE - 1) break;
104       }
105     }
106     levels[get_padded_idx(pos, bwl)] = level;
107   }
108 }
109 
av1_read_coeffs_txb(const AV1_COMMON * const cm,MACROBLOCKD * const xd,aom_reader * const r,const int blk_row,const int blk_col,const int plane,const TXB_CTX * const txb_ctx,const TX_SIZE tx_size)110 uint8_t av1_read_coeffs_txb(const AV1_COMMON *const cm, MACROBLOCKD *const xd,
111                             aom_reader *const r, const int blk_row,
112                             const int blk_col, const int plane,
113                             const TXB_CTX *const txb_ctx,
114                             const TX_SIZE tx_size) {
115   FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
116   const int32_t max_value = (1 << (7 + xd->bd)) - 1;
117   const int32_t min_value = -(1 << (7 + xd->bd));
118   const TX_SIZE txs_ctx = get_txsize_entropy_ctx(tx_size);
119   const PLANE_TYPE plane_type = get_plane_type(plane);
120   MB_MODE_INFO *const mbmi = xd->mi[0];
121   struct macroblockd_plane *const pd = &xd->plane[plane];
122   const int16_t *const dequant = pd->seg_dequant_QTX[mbmi->segment_id];
123   tran_low_t *const tcoeffs = pd->dqcoeff_block + xd->cb_offset[plane];
124   const int shift = av1_get_tx_scale(tx_size);
125   const int bwl = get_txb_bwl(tx_size);
126   const int width = get_txb_wide(tx_size);
127   const int height = get_txb_high(tx_size);
128   int cul_level = 0;
129   int dc_val = 0;
130   uint8_t levels_buf[TX_PAD_2D];
131   uint8_t *const levels = set_levels(levels_buf, width);
132   const int all_zero = aom_read_symbol(
133       r, ec_ctx->txb_skip_cdf[txs_ctx][txb_ctx->txb_skip_ctx], 2, ACCT_STR);
134   eob_info *eob_data = pd->eob_data + xd->txb_offset[plane];
135   uint16_t *const eob = &(eob_data->eob);
136   uint16_t *const max_scan_line = &(eob_data->max_scan_line);
137   *max_scan_line = 0;
138   *eob = 0;
139 
140 #if CONFIG_INSPECTION
141   if (plane == 0) {
142     const int txk_type_idx =
143         av1_get_txk_type_index(mbmi->sb_type, blk_row, blk_col);
144     mbmi->tx_skip[txk_type_idx] = all_zero;
145   }
146 #endif
147 
148   if (all_zero) {
149     *max_scan_line = 0;
150     if (plane == 0) {
151       xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col] = DCT_DCT;
152     }
153     return 0;
154   }
155 
156   if (plane == AOM_PLANE_Y) {
157     // only y plane's tx_type is transmitted
158     av1_read_tx_type(cm, xd, blk_row, blk_col, tx_size, r);
159   }
160   const TX_TYPE tx_type =
161       av1_get_tx_type(xd, plane_type, blk_row, blk_col, tx_size,
162                       cm->features.reduced_tx_set_used);
163   const TX_CLASS tx_class = tx_type_to_class[tx_type];
164   const qm_val_t *iqmatrix =
165       av1_get_iqmatrix(&cm->quant_params, xd, plane, tx_size, tx_type);
166   const SCAN_ORDER *const scan_order = get_scan(tx_size, tx_type);
167   const int16_t *const scan = scan_order->scan;
168   int eob_extra = 0;
169   int eob_pt = 1;
170 
171   const int eob_multi_size = txsize_log2_minus4[tx_size];
172   const int eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1;
173   switch (eob_multi_size) {
174     case 0:
175       eob_pt =
176           aom_read_symbol(r, ec_ctx->eob_flag_cdf16[plane_type][eob_multi_ctx],
177                           5, ACCT_STR) +
178           1;
179       break;
180     case 1:
181       eob_pt =
182           aom_read_symbol(r, ec_ctx->eob_flag_cdf32[plane_type][eob_multi_ctx],
183                           6, ACCT_STR) +
184           1;
185       break;
186     case 2:
187       eob_pt =
188           aom_read_symbol(r, ec_ctx->eob_flag_cdf64[plane_type][eob_multi_ctx],
189                           7, ACCT_STR) +
190           1;
191       break;
192     case 3:
193       eob_pt =
194           aom_read_symbol(r, ec_ctx->eob_flag_cdf128[plane_type][eob_multi_ctx],
195                           8, ACCT_STR) +
196           1;
197       break;
198     case 4:
199       eob_pt =
200           aom_read_symbol(r, ec_ctx->eob_flag_cdf256[plane_type][eob_multi_ctx],
201                           9, ACCT_STR) +
202           1;
203       break;
204     case 5:
205       eob_pt =
206           aom_read_symbol(r, ec_ctx->eob_flag_cdf512[plane_type][eob_multi_ctx],
207                           10, ACCT_STR) +
208           1;
209       break;
210     case 6:
211     default:
212       eob_pt = aom_read_symbol(
213                    r, ec_ctx->eob_flag_cdf1024[plane_type][eob_multi_ctx], 11,
214                    ACCT_STR) +
215                1;
216       break;
217   }
218 
219   const int eob_offset_bits = av1_eob_offset_bits[eob_pt];
220   if (eob_offset_bits > 0) {
221     const int eob_ctx = eob_pt - 3;
222     int bit = aom_read_symbol(
223         r, ec_ctx->eob_extra_cdf[txs_ctx][plane_type][eob_ctx], 2, ACCT_STR);
224     if (bit) {
225       eob_extra += (1 << (eob_offset_bits - 1));
226     }
227 
228     for (int i = 1; i < eob_offset_bits; i++) {
229       bit = aom_read_bit(r, ACCT_STR);
230       if (bit) {
231         eob_extra += (1 << (eob_offset_bits - 1 - i));
232       }
233     }
234   }
235   *eob = rec_eob_pos(eob_pt, eob_extra);
236 
237   if (*eob > 1) {
238     memset(levels_buf, 0,
239            sizeof(*levels_buf) *
240                ((width + TX_PAD_HOR) * (height + TX_PAD_VER) + TX_PAD_END));
241   }
242 
243   {
244     // Read the non-zero coefficient with scan index eob-1
245     // TODO(angiebird): Put this into a function
246     const int c = *eob - 1;
247     const int pos = scan[c];
248     const int coeff_ctx = get_lower_levels_ctx_eob(bwl, height, c);
249     const int nsymbs = 3;
250     aom_cdf_prob *cdf =
251         ec_ctx->coeff_base_eob_cdf[txs_ctx][plane_type][coeff_ctx];
252     int level = aom_read_symbol(r, cdf, nsymbs, ACCT_STR) + 1;
253     if (level > NUM_BASE_LEVELS) {
254       const int br_ctx = get_br_ctx_eob(pos, bwl, tx_class);
255       cdf = ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type][br_ctx];
256       for (int idx = 0; idx < COEFF_BASE_RANGE; idx += BR_CDF_SIZE - 1) {
257         const int k = aom_read_symbol(r, cdf, BR_CDF_SIZE, ACCT_STR);
258         level += k;
259         if (k < BR_CDF_SIZE - 1) break;
260       }
261     }
262     levels[get_padded_idx(pos, bwl)] = level;
263   }
264   if (*eob > 1) {
265     base_cdf_arr base_cdf = ec_ctx->coeff_base_cdf[txs_ctx][plane_type];
266     br_cdf_arr br_cdf =
267         ec_ctx->coeff_br_cdf[AOMMIN(txs_ctx, TX_32X32)][plane_type];
268     if (tx_class == TX_CLASS_2D) {
269       read_coeffs_reverse_2d(r, tx_size, 1, *eob - 1 - 1, scan, bwl, levels,
270                              base_cdf, br_cdf);
271       read_coeffs_reverse(r, tx_size, tx_class, 0, 0, scan, bwl, levels,
272                           base_cdf, br_cdf);
273     } else {
274       read_coeffs_reverse(r, tx_size, tx_class, 0, *eob - 1 - 1, scan, bwl,
275                           levels, base_cdf, br_cdf);
276     }
277   }
278 
279   for (int c = 0; c < *eob; ++c) {
280     const int pos = scan[c];
281     uint8_t sign;
282     tran_low_t level = levels[get_padded_idx(pos, bwl)];
283     if (level) {
284       *max_scan_line = AOMMAX(*max_scan_line, pos);
285       if (c == 0) {
286         const int dc_sign_ctx = txb_ctx->dc_sign_ctx;
287         sign = aom_read_symbol(r, ec_ctx->dc_sign_cdf[plane_type][dc_sign_ctx],
288                                2, ACCT_STR);
289       } else {
290         sign = aom_read_bit(r, ACCT_STR);
291       }
292       if (level >= MAX_BASE_BR_RANGE) {
293         level += read_golomb(xd, r);
294       }
295 
296       if (c == 0) dc_val = sign ? -level : level;
297 
298       // Bitmasking to clamp level to valid range:
299       //   The valid range for 8/10/12 bit vdieo is at most 14/16/18 bit
300       level &= 0xfffff;
301       cul_level += level;
302       tran_low_t dq_coeff;
303       // Bitmasking to clamp dq_coeff to valid range:
304       //   The valid range for 8/10/12 bit video is at most 17/19/21 bit
305       dq_coeff = (tran_low_t)(
306           (int64_t)level * get_dqv(dequant, scan[c], iqmatrix) & 0xffffff);
307       dq_coeff = dq_coeff >> shift;
308       if (sign) {
309         dq_coeff = -dq_coeff;
310       }
311       tcoeffs[pos] = clamp(dq_coeff, min_value, max_value);
312     }
313   }
314 
315   cul_level = AOMMIN(COEFF_CONTEXT_MASK, cul_level);
316 
317   // DC value
318   set_dc_sign(&cul_level, dc_val);
319 
320   return cul_level;
321 }
322 
av1_read_coeffs_txb_facade(const AV1_COMMON * const cm,MACROBLOCKD * const xd,aom_reader * const r,const int plane,const int row,const int col,const TX_SIZE tx_size)323 void av1_read_coeffs_txb_facade(const AV1_COMMON *const cm,
324                                 MACROBLOCKD *const xd, aom_reader *const r,
325                                 const int plane, const int row, const int col,
326                                 const TX_SIZE tx_size) {
327 #if TXCOEFF_TIMER
328   struct aom_usec_timer timer;
329   aom_usec_timer_start(&timer);
330 #endif
331   MB_MODE_INFO *const mbmi = xd->mi[0];
332   struct macroblockd_plane *const pd = &xd->plane[plane];
333 
334   const BLOCK_SIZE bsize = mbmi->sb_type;
335   assert(bsize < BLOCK_SIZES_ALL);
336   const BLOCK_SIZE plane_bsize =
337       get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
338 
339   TXB_CTX txb_ctx;
340   get_txb_ctx(plane_bsize, tx_size, plane, pd->above_entropy_context + col,
341               pd->left_entropy_context + row, &txb_ctx);
342   const uint8_t cul_level =
343       av1_read_coeffs_txb(cm, xd, r, row, col, plane, &txb_ctx, tx_size);
344   av1_set_entropy_contexts(xd, pd, plane, plane_bsize, tx_size, cul_level, col,
345                            row);
346 
347   if (is_inter_block(mbmi)) {
348     const PLANE_TYPE plane_type = get_plane_type(plane);
349     // tx_type will be read out in av1_read_coeffs_txb_facade
350     const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, row, col, tx_size,
351                                             cm->features.reduced_tx_set_used);
352 
353     if (plane == 0) {
354       const int txw = tx_size_wide_unit[tx_size];
355       const int txh = tx_size_high_unit[tx_size];
356       // The 16x16 unit is due to the constraint from tx_64x64 which sets the
357       // maximum tx size for chroma as 32x32. Coupled with 4x1 transform block
358       // size, the constraint takes effect in 32x16 / 16x32 size too. To solve
359       // the intricacy, cover all the 16x16 units inside a 64 level transform.
360       if (txw == tx_size_wide_unit[TX_64X64] ||
361           txh == tx_size_high_unit[TX_64X64]) {
362         const int tx_unit = tx_size_wide_unit[TX_16X16];
363         const int stride = xd->tx_type_map_stride;
364         for (int idy = 0; idy < txh; idy += tx_unit) {
365           for (int idx = 0; idx < txw; idx += tx_unit) {
366             xd->tx_type_map[(row + idy) * stride + col + idx] = tx_type;
367           }
368         }
369       }
370     }
371   }
372 
373 #if TXCOEFF_TIMER
374   aom_usec_timer_mark(&timer);
375   const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);
376   cm->txcoeff_timer += elapsed_time;
377   ++cm->txb_count;
378 #endif
379 }
380