• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, 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 #ifndef AOM_AV1_ENCODER_AV1_QUANTIZE_H_
13 #define AOM_AV1_ENCODER_AV1_QUANTIZE_H_
14 
15 #include "config/aom_config.h"
16 
17 #include "av1/common/quant_common.h"
18 #include "av1/common/scan.h"
19 #include "av1/encoder/block.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 typedef struct QUANT_PARAM {
26   int log_scale;
27   TX_SIZE tx_size;
28   const qm_val_t *qmatrix;
29   const qm_val_t *iqmatrix;
30   int use_quant_b_adapt;
31   int use_optimize_b;
32   int xform_quant_idx;
33 } QUANT_PARAM;
34 
35 typedef void (*AV1_QUANT_FACADE)(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
36                                  const MACROBLOCK_PLANE *p,
37                                  tran_low_t *qcoeff_ptr,
38                                  tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
39                                  const SCAN_ORDER *sc,
40                                  const QUANT_PARAM *qparam);
41 
42 // The QUANTS structure is used only for internal quantizer setup in
43 // av1_quantize.c.
44 // All of its fields use the same coefficient shift/scaling at TX.
45 typedef struct {
46   // 0: dc 1: ac 2-8: ac repeated to SIMD width
47   DECLARE_ALIGNED(16, int16_t, y_quant[QINDEX_RANGE][8]);
48   DECLARE_ALIGNED(16, int16_t, y_quant_shift[QINDEX_RANGE][8]);
49   DECLARE_ALIGNED(16, int16_t, y_zbin[QINDEX_RANGE][8]);
50   DECLARE_ALIGNED(16, int16_t, y_round[QINDEX_RANGE][8]);
51 
52   // TODO(jingning): in progress of re-working the quantization. will decide
53   // if we want to deprecate the current use of y_quant.
54   DECLARE_ALIGNED(16, int16_t, y_quant_fp[QINDEX_RANGE][8]);
55   DECLARE_ALIGNED(16, int16_t, u_quant_fp[QINDEX_RANGE][8]);
56   DECLARE_ALIGNED(16, int16_t, v_quant_fp[QINDEX_RANGE][8]);
57   DECLARE_ALIGNED(16, int16_t, y_round_fp[QINDEX_RANGE][8]);
58   DECLARE_ALIGNED(16, int16_t, u_round_fp[QINDEX_RANGE][8]);
59   DECLARE_ALIGNED(16, int16_t, v_round_fp[QINDEX_RANGE][8]);
60 
61   DECLARE_ALIGNED(16, int16_t, u_quant[QINDEX_RANGE][8]);
62   DECLARE_ALIGNED(16, int16_t, v_quant[QINDEX_RANGE][8]);
63   DECLARE_ALIGNED(16, int16_t, u_quant_shift[QINDEX_RANGE][8]);
64   DECLARE_ALIGNED(16, int16_t, v_quant_shift[QINDEX_RANGE][8]);
65   DECLARE_ALIGNED(16, int16_t, u_zbin[QINDEX_RANGE][8]);
66   DECLARE_ALIGNED(16, int16_t, v_zbin[QINDEX_RANGE][8]);
67   DECLARE_ALIGNED(16, int16_t, u_round[QINDEX_RANGE][8]);
68   DECLARE_ALIGNED(16, int16_t, v_round[QINDEX_RANGE][8]);
69 } QUANTS;
70 
71 // The Dequants structure is used only for internal quantizer setup in
72 // av1_quantize.c.
73 // Fields are suffixed according to whether or not they're expressed in
74 // the same coefficient shift/precision as TX or a fixed Q3 format.
75 typedef struct {
76   DECLARE_ALIGNED(16, int16_t,
77                   y_dequant_QTX[QINDEX_RANGE][8]);  // 8: SIMD width
78   DECLARE_ALIGNED(16, int16_t,
79                   u_dequant_QTX[QINDEX_RANGE][8]);  // 8: SIMD width
80   DECLARE_ALIGNED(16, int16_t,
81                   v_dequant_QTX[QINDEX_RANGE][8]);  // 8: SIMD width
82 } Dequants;
83 
84 typedef struct {
85   // Quantization parameters for internal quantizer setup.
86   QUANTS quants;
87   // Dequantization parameters for internal quantizer setup.
88   Dequants dequants;
89 } EncQuantDequantParams;
90 
91 struct AV1_COMP;
92 struct AV1Common;
93 
94 void av1_frame_init_quantizer(struct AV1_COMP *cpi);
95 
96 void av1_init_plane_quantizers(const struct AV1_COMP *cpi, MACROBLOCK *x,
97                                int segment_id);
98 
99 void av1_build_quantizer(aom_bit_depth_t bit_depth, int y_dc_delta_q,
100                          int u_dc_delta_q, int u_ac_delta_q, int v_dc_delta_q,
101                          int v_ac_delta_q, QUANTS *const quants,
102                          Dequants *const deq);
103 
104 void av1_init_quantizer(EncQuantDequantParams *const enc_quant_dequant_params,
105                         const CommonQuantParams *quant_params,
106                         aom_bit_depth_t bit_depth);
107 
108 void av1_set_quantizer(struct AV1Common *const cm, int min_qmlevel,
109                        int max_qmlevel, int q, int enable_chroma_deltaq);
110 
111 int av1_quantizer_to_qindex(int quantizer);
112 
113 int av1_qindex_to_quantizer(int qindex);
114 
115 void av1_quantize_skip(intptr_t n_coeffs, tran_low_t *qcoeff_ptr,
116                        tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr);
117 
118 /*!\brief Quantize transform coefficients without using qmatrix
119  *
120  * quant_ptr, dequant_ptr and round_ptr are size 2 arrays,
121  * where index 0 corresponds to dc coeff and index 1 corresponds to ac coeffs.
122  *
123  * \param[in]  quant_ptr    16-bit fixed point representation of inverse
124  *                          quantize step size, i.e. 2^16/dequant
125  * \param[in]  dequant_ptr  quantize step size
126  * \param[in]  round_ptr    rounding
127  * \param[in]  log_scale    the relative log scale of the transform
128  *                          coefficients
129  * \param[in]  scan         scan[i] indicates the position of ith to-be-coded
130  *                          coefficient
131  * \param[in]  coeff_count  number of coefficients
132  * \param[out] qcoeff_ptr   quantized coefficients
133  * \param[out] dqcoeff_ptr  dequantized coefficients
134  *
135  * \return The last non-zero coefficient's scan index plus 1
136  */
137 int av1_quantize_fp_no_qmatrix(const int16_t quant_ptr[2],
138                                const int16_t dequant_ptr[2],
139                                const int16_t round_ptr[2], int log_scale,
140                                const int16_t *scan, int coeff_count,
141                                const tran_low_t *coeff_ptr,
142                                tran_low_t *qcoeff_ptr, tran_low_t *dqcoeff_ptr);
143 
144 void av1_quantize_fp_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
145                             const MACROBLOCK_PLANE *p, tran_low_t *qcoeff_ptr,
146                             tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
147                             const SCAN_ORDER *sc, const QUANT_PARAM *qparam);
148 
149 void av1_quantize_b_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
150                            const MACROBLOCK_PLANE *p, tran_low_t *qcoeff_ptr,
151                            tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
152                            const SCAN_ORDER *sc, const QUANT_PARAM *qparam);
153 
154 void av1_quantize_dc_facade(const tran_low_t *coeff_ptr, intptr_t n_coeffs,
155                             const MACROBLOCK_PLANE *p, tran_low_t *qcoeff_ptr,
156                             tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
157                             const SCAN_ORDER *sc, const QUANT_PARAM *qparam);
158 
159 /*!\brief Update quantize parameters in MACROBLOCK
160  *
161  * \param[in]  enc_quant_dequant_params This parameter cached the quantize and
162  *                                      dequantize parameters for all q
163  *                                      indices.
164  * \param[in]  qindex                   Quantize index used for the current
165  *                                      superblock.
166  * \param[out] x                        A superblock data structure for
167  *                                      encoder.
168  */
169 void av1_set_q_index(const EncQuantDequantParams *enc_quant_dequant_params,
170                      int qindex, MACROBLOCK *x);
171 
172 /*!\brief Update quantize matrix in MACROBLOCKD based on segment id
173  *
174  * \param[in]  quant_params  Quantize parameters used by encoder and decoder
175  * \param[in]  segment_id    Segment id.
176  * \param[out] xd            A superblock data structure used by encoder and
177  * decoder.
178  */
179 void av1_set_qmatrix(const CommonQuantParams *quant_params, int segment_id,
180                      MACROBLOCKD *xd);
181 
182 #if CONFIG_AV1_HIGHBITDEPTH
183 void av1_highbd_quantize_fp_facade(const tran_low_t *coeff_ptr,
184                                    intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
185                                    tran_low_t *qcoeff_ptr,
186                                    tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
187                                    const SCAN_ORDER *sc,
188                                    const QUANT_PARAM *qparam);
189 
190 void av1_highbd_quantize_b_facade(const tran_low_t *coeff_ptr,
191                                   intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
192                                   tran_low_t *qcoeff_ptr,
193                                   tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
194                                   const SCAN_ORDER *sc,
195                                   const QUANT_PARAM *qparam);
196 
197 void av1_highbd_quantize_dc_facade(const tran_low_t *coeff_ptr,
198                                    intptr_t n_coeffs, const MACROBLOCK_PLANE *p,
199                                    tran_low_t *qcoeff_ptr,
200                                    tran_low_t *dqcoeff_ptr, uint16_t *eob_ptr,
201                                    const SCAN_ORDER *sc,
202                                    const QUANT_PARAM *qparam);
203 
204 #endif
205 
206 #ifdef __cplusplus
207 }  // extern "C"
208 #endif
209 
210 #endif  // AOM_AV1_ENCODER_AV1_QUANTIZE_H_
211