quant_common.h (6947B)
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_SSIMULACRA2 2 42 #define QM_LAST_IQ_SSIMULACRA2 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. 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. 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 SSIMULACRA 2 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. 111 static inline int aom_get_qmlevel_luma_ssimulacra2(int qindex, int first, 112 int last) { 113 int qm_level = 0; 114 115 if (qindex <= 40) { 116 qm_level = 10; 117 } else if (qindex <= 60) { 118 qm_level = 9; 119 } else if (qindex <= 100) { 120 qm_level = 8; 121 } else if (qindex <= 120) { 122 qm_level = 7; 123 } else if (qindex <= 140) { 124 qm_level = 6; 125 } else if (qindex <= 160) { 126 qm_level = 5; 127 } else if (qindex <= 200) { 128 qm_level = 4; 129 } else if (qindex <= 220) { 130 qm_level = 3; 131 } else { 132 qm_level = 2; 133 } 134 135 return clamp(qm_level, first, last); 136 } 137 138 // Chroma QM levels for 4:4:4 subsampling used for SSIMULACRA 2 and IQ tunings 139 // This formula was empirically derived by encoding Daala's subset1 validation 140 // testset for each QP/QM tuple, and building a convex hull that maximizes 141 // SSIMULACRA 2 scores, and a final subjective visual quality pass as a quick 142 // validation. This is a decreasing function in qindex. 143 // Like with luma QMs, there are a total of 16 chroma QM levels, and the higher 144 // the level, the flatter these QMs are. 145 // QM level 15 is a completely-flat matrix and level 0 is the steepest. 146 // This formula only uses levels 2 through 10, unless qm-min and qm-max are 147 // both set below or above this range. 148 // For more information on quantization matrices, please refer to 149 // https://arxiv.org/pdf/2008.06091, section F. 150 static inline int aom_get_qmlevel_444_chroma(int qindex, int first, 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_