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