1 /*
2 * Copyright(c) 2019 Netflix, Inc.
3 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 *
5 * This source code is subject to the terms of the BSD 2 Clause License and
6 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7 * was not distributed with this source code in the LICENSE file, you can
8 * obtain it at https://www.aomedia.org/license/software-license. If the Alliance for Open
9 * Media Patent License 1.0 was not distributed with this source code in the
10 * PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
11 */
12
13 #include "EbDefinitions.h"
14
15 #include "EbSvtAv1Dec.h"
16 #include "EbDecHandle.h"
17
18 #include "EbDecInverseQuantize.h"
19 #include "EbObuParse.h"
20 #include "EbDecProcessFrame.h"
21 #include "EbCoefficients.h"
22 #include "EbQMatrices.h"
23 #include "EbInvTransforms.h"
24
25 // Same wrapper(av1_ac/dc_quant_qtx) available in .c file of encoder
get_dc_quant(int32_t qindex,int32_t delta,AomBitDepth bit_depth)26 static INLINE int16_t get_dc_quant(int32_t qindex, int32_t delta, AomBitDepth bit_depth) {
27 return svt_av1_dc_quant_q3(qindex, delta, bit_depth);
28 }
29
get_ac_quant(int32_t qindex,int32_t delta,AomBitDepth bit_depth)30 static INLINE int16_t get_ac_quant(int32_t qindex, int32_t delta, AomBitDepth bit_depth) {
31 return svt_av1_ac_quant_q3(qindex, delta, bit_depth);
32 }
33
34 // Called in read_frame_header_obu() -> av1_decode_frame_headers_and_setup() -> read_uncompressed_header()
setup_segmentation_dequant(DecModCtxt * dec_mod_ctxt)35 void setup_segmentation_dequant(DecModCtxt *dec_mod_ctxt) {
36 SeqHeader * seq_header = dec_mod_ctxt->seq_header;
37 FrameHeader *frame_info = dec_mod_ctxt->frame_header;
38 int bit_depth = seq_header->color_config.bit_depth;
39 /*int max_segments = frame_info->segmentation_params.segmentation_enabled ?
40 MAX_SEGMENTS : 1;*/
41 int32_t dc_delta_q, ac_delta_q;
42 for (int i = 0; i < MAX_SEGMENTS; i++) {
43 const int qindex = get_qindex(
44 &frame_info->segmentation_params, i, frame_info->quantization_params.base_q_idx);
45
46 for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
47 dc_delta_q = frame_info->quantization_params.delta_q_dc[plane];
48 ac_delta_q = frame_info->quantization_params.delta_q_ac[plane];
49
50 dec_mod_ctxt->dequants.dequant_qtx[i][plane][0] = get_dc_quant(
51 qindex, dc_delta_q, bit_depth);
52 dec_mod_ctxt->dequants.dequant_qtx[i][plane][1] = get_ac_quant(
53 qindex, ac_delta_q, bit_depth);
54 }
55 }
56 }
57
av1_inverse_qm_init(DecModCtxt * dec_mod_ctxt,SeqHeader * seq_header)58 void av1_inverse_qm_init(DecModCtxt *dec_mod_ctxt, SeqHeader *seq_header) {
59 const int num_planes = av1_num_planes(&seq_header->color_config);
60 int q, c;
61 uint8_t t;
62 int current;
63 for (q = 0; q < NUM_QM_LEVELS; ++q) {
64 for (c = 0; c < num_planes; ++c) {
65 current = 0;
66 for (t = 0; t < TX_SIZES_ALL; ++t) {
67 const int size = tx_size_2d[t];
68 const uint8_t qm_tx_size = av1_get_adjusted_tx_size(t);
69 if (q == NUM_QM_LEVELS - 1)
70 dec_mod_ctxt->giqmatrix[q][c][t] = NULL;
71 else if (t != qm_tx_size) { // Reuse matrices for 'qm_tx_size'
72 dec_mod_ctxt->giqmatrix[q][c][t] = dec_mod_ctxt->giqmatrix[q][c][qm_tx_size];
73 } else {
74 assert(current + size <= QM_TOTAL_SIZE);
75 dec_mod_ctxt->giqmatrix[q][c][t] = &iwt_matrix_ref[q][c >= 1][current];
76 current += size;
77 }
78 }
79 }
80 }
81 }
82
83 // Called in decode_tiles()
84 // Default initilaization of dequant and iquant
85 /*
86 void av1_init_sb(FrameHeader *frame)
87 {
88 frame->dequants_delta_q = &frame->dequants;
89 }
90 */
91
92 // Called in parse_decode_block()
93 // Update de-quantization parameter based on delta qp param
update_dequant(DecModCtxt * dec_mod_ctxt,SBInfo * sb_info)94 void update_dequant(DecModCtxt *dec_mod_ctxt, SBInfo *sb_info) {
95 SeqHeader * seq_header = dec_mod_ctxt->seq_header;
96 FrameHeader *frame = dec_mod_ctxt->frame_header;
97
98 int bit_depth = seq_header->color_config.bit_depth;
99 dec_mod_ctxt->dequants_delta_q = &dec_mod_ctxt->dequants;
100 if (frame->delta_q_params.delta_q_present) {
101 for (int i = 0; i < MAX_SEGMENTS; i++) {
102 const int current_qindex = get_qindex(
103 &frame->segmentation_params, i, sb_info->sb_delta_q[0]);
104
105 for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
106 const int dc_delta_q = frame->quantization_params.delta_q_dc[plane];
107 const int ac_delta_q = frame->quantization_params.delta_q_ac[plane];
108
109 dec_mod_ctxt->dequants_delta_q->dequant_qtx[i][plane][0] = get_dc_quant(
110 current_qindex, dc_delta_q, bit_depth);
111 dec_mod_ctxt->dequants_delta_q->dequant_qtx[i][plane][1] = get_ac_quant(
112 current_qindex, ac_delta_q, bit_depth);
113 }
114 }
115 }
116 }
117
get_dqv(const int16_t dequant,int coeff_idx,const QmVal * iqmatrix)118 static INLINE int get_dqv(const int16_t dequant, int coeff_idx, const QmVal *iqmatrix) {
119 int dqv = dequant;
120 if (iqmatrix != NULL)
121 dqv = ((iqmatrix[coeff_idx] * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
122 return dqv;
123 }
124
inverse_quantize(DecModCtxt * dec_mod_ctxt,PartitionInfo * part,BlockModeInfo * mode,int32_t * level,int32_t * qcoeffs,TxType tx_type,TxSize tx_size,int plane)125 int32_t inverse_quantize(DecModCtxt *dec_mod_ctxt, PartitionInfo *part, BlockModeInfo *mode,
126 int32_t *level, int32_t *qcoeffs, TxType tx_type, TxSize tx_size,
127 int plane) {
128 (void)part;
129 SeqHeader * seq = dec_mod_ctxt->seq_header;
130 FrameHeader * frame = dec_mod_ctxt->frame_header;
131 const ScanOrder *const scan_order =
132 &av1_scan_orders[tx_size][tx_type]; //get_scan(tx_size, tx_type);
133 const int16_t *scan = scan_order->scan;
134 const int32_t max_value = (1 << (7 + seq->color_config.bit_depth)) - 1;
135 const int32_t min_value = -(1 << (7 + seq->color_config.bit_depth));
136 int n_coeffs, i, pos, qmlevel;
137 int16_t * dequant;
138 const QmVal * iqmatrix;
139 const TxSize qm_tx_size = av1_get_adjusted_tx_size(tx_size);
140
141 int using_qm = frame->quantization_params.using_qmatrix;
142 int lossless = frame->lossless_array[mode->segment_id];
143 dequant = dec_mod_ctxt->dequants_delta_q->dequant_qtx[mode->segment_id][plane];
144 qmlevel = (lossless || using_qm == 0) ? NUM_QM_LEVELS - 1
145 : frame->quantization_params.qm[plane];
146 iqmatrix = IS_2D_TRANSFORM(tx_type) ? dec_mod_ctxt->giqmatrix[qmlevel][plane][qm_tx_size]
147 : dec_mod_ctxt->giqmatrix[NUM_QM_LEVELS - 1][0][qm_tx_size];
148 const int shift = av1_get_tx_scale(tx_size);
149
150 // Level is 1D array with eob length as first value then continued by
151 // coeffs value to the length of eob.
152 #if SVT_DEC_COEFF_DEBUG
153 int16_t *cur_coeff = (int16_t *)level;
154 n_coeffs = cur_coeff[1];
155 #else
156 n_coeffs = level[0]; // coeffs length
157 #endif
158 level++;
159
160 TranLow q_coeff;
161 int32_t lev;
162 lev = level[0];
163 if (lev) {
164 pos = scan[0];
165 q_coeff = (TranLow)((int64_t)abs(lev) * get_dqv(dequant[0], pos, iqmatrix) & 0xffffff);
166 q_coeff = q_coeff >> shift;
167
168 if (lev < 0)
169 q_coeff = -q_coeff;
170 qcoeffs[0] = clamp(q_coeff, min_value, max_value);
171 }
172
173 for (i = 1; i < n_coeffs; i++) {
174 lev = level[i];
175 if (lev != 0) {
176 pos = scan[i];
177 q_coeff = (TranLow)((int64_t)abs(lev) * get_dqv(dequant[1], pos, iqmatrix) & 0xffffff);
178 q_coeff = q_coeff >> shift;
179
180 if (lev < 0)
181 q_coeff = -q_coeff;
182
183 qcoeffs[pos] = clamp(q_coeff, min_value, max_value);
184 }
185 }
186 return n_coeffs;
187 }
188