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_COMMON_QUANT_COMMON_H_
13 #define AOM_AV1_COMMON_QUANT_COMMON_H_
14
15 #include <stdbool.h>
16 #include "aom/aom_codec.h"
17 #include "av1/common/seg_common.h"
18 #include "av1/common/enums.h"
19 #include "av1/common/entropy.h"
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 #define MINQ 0
26 #define MAXQ 255
27 #define QINDEX_RANGE (MAXQ - MINQ + 1)
28 #define QINDEX_BITS 8
29 // Total number of QM sets stored
30 #define QM_LEVEL_BITS 4
31 #define NUM_QM_LEVELS (1 << QM_LEVEL_BITS)
32 /* Range of QMS is between first and last value, with offset applied to inter
33 * blocks*/
34 #define DEFAULT_QM_Y 10
35 #define DEFAULT_QM_U 11
36 #define DEFAULT_QM_V 12
37 #define DEFAULT_QM_FIRST 5
38 #define DEFAULT_QM_LAST 9
39 #define DEFAULT_QM_FIRST_ALLINTRA 4
40 #define DEFAULT_QM_LAST_ALLINTRA 10
41 #define QM_FIRST_IQ 2
42 #define QM_LAST_IQ 10
43 #define LOSSLESS_Q_STEP 4 // this should equal to dc/ac_qlookup_QTX[0]
44
45 struct AV1Common;
46 struct CommonQuantParams;
47 struct macroblockd;
48
49 int16_t av1_dc_quant_QTX(int qindex, int delta, aom_bit_depth_t bit_depth);
50 int16_t av1_ac_quant_QTX(int qindex, int delta, aom_bit_depth_t bit_depth);
51
52 int av1_get_qindex(const struct segmentation *seg, int segment_id,
53 int base_qindex);
54
55 // Returns true if we are using quantization matrix.
56 bool av1_use_qmatrix(const struct CommonQuantParams *quant_params,
57 const struct macroblockd *xd, int segment_id);
58
59 // Reduce the large number of quantizers to a smaller number of levels for which
60 // different matrices may be defined. This is an increasing function in qindex.
aom_get_qmlevel(int qindex,int first,int last)61 static inline int aom_get_qmlevel(int qindex, int first, int last) {
62 return first + (qindex * (last + 1 - first)) / QINDEX_RANGE;
63 }
64
65 // QM levels tuned for all intra mode (including still images)
66 // This formula was empirically derived by encoding the CID22 validation
67 // testset for each QP/QM tuple, and building a convex hull that
68 // maximizes SSIMULACRA 2 scores, and a final subjective visual quality pass
69 // as a quick validation. This is a decreasing function in qindex.
70 // There are a total of 16 luma QM levels, and the higher the level, the
71 // flatter these QMs are.
72 // QM level 15 is a completely-flat matrix and level 0 is the steepest.
73 // This formula only uses levels 4 through 10, unless qm-min and qm-max are
74 // both set below or above this range.
75 // For more information on quantization matrices, please refer to
76 // https://arxiv.org/pdf/2008.06091, section F.
aom_get_qmlevel_allintra(int qindex,int first,int last)77 static inline int aom_get_qmlevel_allintra(int qindex, int first, int last) {
78 int qm_level = 0;
79
80 if (qindex <= 40) {
81 qm_level = 10;
82 } else if (qindex <= 100) {
83 qm_level = 9;
84 } else if (qindex <= 160) {
85 qm_level = 8;
86 } else if (qindex <= 200) {
87 qm_level = 7;
88 } else if (qindex <= 220) {
89 qm_level = 6;
90 } else if (qindex <= 240) {
91 qm_level = 5;
92 } else {
93 qm_level = 4;
94 }
95
96 return clamp(qm_level, first, last);
97 }
98
99 // Luma QM levels tuned for image quality (IQ)
100 // This formula was empirically derived by encoding Daala's subset1 validation
101 // testset for each QP/QM tuple, and building a convex hull that maximizes
102 // SSIMULACRA 2 scores, and a final subjective visual quality pass as a quick
103 // validation. This is a decreasing function in qindex.
104 // There are a total of 16 luma QM levels, and the higher the level, the
105 // flatter these QMs are.
106 // QM level 15 is a completely-flat matrix and level 0 is the steepest.
107 // This formula only uses levels 2 through 10, unless qm-min and qm-max are
108 // both set below or above this range.
109 // For more information on quantization matrices, please refer to
110 // https://arxiv.org/pdf/2008.06091, section F.
aom_get_qmlevel_luma_iq(int qindex,int first,int last)111 static inline int aom_get_qmlevel_luma_iq(int qindex, int first, int last) {
112 int qm_level = 0;
113
114 if (qindex <= 40) {
115 qm_level = 10;
116 } else if (qindex <= 60) {
117 qm_level = 9;
118 } else if (qindex <= 100) {
119 qm_level = 8;
120 } else if (qindex <= 120) {
121 qm_level = 7;
122 } else if (qindex <= 140) {
123 qm_level = 6;
124 } else if (qindex <= 160) {
125 qm_level = 5;
126 } else if (qindex <= 200) {
127 qm_level = 4;
128 } else if (qindex <= 220) {
129 qm_level = 3;
130 } else {
131 qm_level = 2;
132 }
133
134 return clamp(qm_level, first, last);
135 }
136
137 // Chroma QM levels for 4:4:4 subsampling tuned for image quality (IQ)
138 // This formula was empirically derived by encoding Daala's subset1 validation
139 // testset for each QP/QM tuple, and building a convex hull that maximizes
140 // SSIMULACRA 2 scores, and a final subjective visual quality pass as a quick
141 // validation. This is a decreasing function in qindex.
142 // Like with luma QMs, there are a total of 16 chroma QM levels, and the higher
143 // the level, the flatter these QMs are.
144 // QM level 15 is a completely-flat matrix and level 0 is the steepest.
145 // This formula only uses levels 2 through 10, unless qm-min and qm-max are
146 // both set below or above this range.
147 // For more information on quantization matrices, please refer to
148 // https://arxiv.org/pdf/2008.06091, section F.
aom_get_qmlevel_444_chroma_iq(int qindex,int first,int last)149 static inline int aom_get_qmlevel_444_chroma_iq(int qindex, int first,
150 int last) {
151 int chroma_qm_level = 0;
152
153 if (qindex <= 12) {
154 chroma_qm_level = 10;
155 } else if (qindex <= 24) {
156 chroma_qm_level = 9;
157 } else if (qindex <= 32) {
158 chroma_qm_level = 8;
159 } else if (qindex <= 36) {
160 chroma_qm_level = 7;
161 } else if (qindex <= 44) {
162 chroma_qm_level = 6;
163 } else if (qindex <= 48) {
164 chroma_qm_level = 5;
165 } else if (qindex <= 56) {
166 chroma_qm_level = 4;
167 } else if (qindex <= 88) {
168 chroma_qm_level = 3;
169 } else {
170 chroma_qm_level = 2;
171 }
172
173 return clamp(chroma_qm_level, first, last);
174 }
175
176 // Initialize all global quant/dequant matrices.
177 void av1_qm_init(struct CommonQuantParams *quant_params, int num_planes);
178
179 // Get either local / global dequant matrix as appropriate.
180 const qm_val_t *av1_get_iqmatrix(const struct CommonQuantParams *quant_params,
181 const struct macroblockd *xd, int plane,
182 TX_SIZE tx_size, TX_TYPE tx_type);
183 // Get either local / global quant matrix as appropriate.
184 const qm_val_t *av1_get_qmatrix(const struct CommonQuantParams *quant_params,
185 const struct macroblockd *xd, int plane,
186 TX_SIZE tx_size, TX_TYPE tx_type);
187
188 #ifdef __cplusplus
189 } // extern "C"
190 #endif
191
192 #endif // AOM_AV1_COMMON_QUANT_COMMON_H_
193