1 /* 2 * Copyright 2019 The libgav1 Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LIBGAV1_SRC_SYMBOL_DECODER_CONTEXT_H_ 18 #define LIBGAV1_SRC_SYMBOL_DECODER_CONTEXT_H_ 19 20 #include <cassert> 21 #include <cstdint> 22 23 #include "src/dsp/constants.h" 24 #include "src/utils/constants.h" 25 #include "src/utils/memory.h" 26 27 namespace libgav1 { 28 29 enum { 30 kPartitionContexts = 4, 31 kSegmentIdContexts = 3, 32 kUsePredictedSegmentIdContexts = 3, 33 kSkipContexts = 3, 34 kSkipModeContexts = 3, 35 kBooleanFieldCdfSize = 3, 36 kDeltaSymbolCount = 4, // Used for both delta_q and delta_lf. 37 kIntraModeContexts = 5, 38 kYModeContexts = 4, 39 kAngleDeltaSymbolCount = 2 * kMaxAngleDelta + 1, 40 kCflAlphaSignsSymbolCount = 8, 41 kCflAlphaContexts = 6, 42 kCflAlphaSymbolCount = 16, 43 kTxDepthContexts = 3, 44 kMaxTxDepthSymbolCount = 3, 45 kTxSplitContexts = 21, 46 kCoefficientQuantizerContexts = 4, 47 kNumSquareTransformSizes = 5, 48 kAllZeroContexts = 13, 49 kNumExtendedTransformSizes = 4, 50 kEobPtContexts = 2, 51 kEobPt16SymbolCount = 5, 52 kEobPt32SymbolCount = 6, 53 kEobPt64SymbolCount = 7, 54 kEobPt128SymbolCount = 8, 55 kEobPt256SymbolCount = 9, 56 kEobPt512SymbolCount = 10, 57 kEobPt1024SymbolCount = 11, 58 kEobExtraContexts = 9, 59 kCoeffBaseEobContexts = 4, 60 kCoeffBaseEobSymbolCount = 3, 61 kCoeffBaseContexts = 42, 62 kCoeffBaseSymbolCount = 4, 63 kCoeffBaseRangeContexts = 21, 64 kCoeffBaseRangeSymbolCount = 4, 65 kDcSignContexts = 3, 66 kPaletteBlockSizeContexts = 7, 67 kPaletteYModeContexts = 3, 68 kPaletteUVModeContexts = 2, 69 kPaletteSizeSymbolCount = 7, 70 kPaletteColorIndexContexts = 5, 71 kPaletteColorIndexSymbolCount = 8, 72 kIsInterContexts = 4, 73 kUseCompoundReferenceContexts = 5, 74 kCompoundReferenceTypeContexts = 5, 75 kReferenceContexts = 3, 76 kCompoundPredictionModeContexts = 8, 77 kNewMvContexts = 6, 78 kZeroMvContexts = 2, 79 kReferenceMvContexts = 6, 80 kRefMvIndexContexts = 3, 81 kInterIntraContexts = 3, 82 kWedgeIndexSymbolCount = 16, 83 kIsExplicitCompoundTypeContexts = 6, 84 kIsCompoundTypeAverageContexts = 6, 85 kInterpolationFilterContexts = 16, 86 kMvContexts = 2, 87 kMvClassSymbolCount = 11, 88 kMvFractionSymbolCount = 4, 89 kMvBitSymbolCount = 10, 90 kNumMvComponents = 2, 91 }; // anonymous enum 92 93 struct SymbolDecoderContext { 94 SymbolDecoderContext() = default; SymbolDecoderContextSymbolDecoderContext95 explicit SymbolDecoderContext(int base_quantizer_index) { 96 Initialize(base_quantizer_index); 97 } 98 99 void Initialize(int base_quantizer_index); 100 101 // Partition related variables and functions. 102 static int PartitionCdfSize(int block_size_log2); 103 104 // Returns the cdf array index for inter_tx_type or intra_tx_type based on 105 // |tx_set|. TxTypeIndexSymbolDecoderContext106 static int TxTypeIndex(TransformSet tx_set) { 107 assert(tx_set != kTransformSetDctOnly); 108 switch (tx_set) { 109 case kTransformSetInter1: 110 case kTransformSetIntra1: 111 return 0; 112 case kTransformSetInter2: 113 case kTransformSetIntra2: 114 return 1; 115 case kTransformSetInter3: 116 return 2; 117 default: 118 return -1; 119 } 120 } 121 122 // Resets the intra_frame_y_mode_cdf array to the default. 123 void ResetIntraFrameYModeCdf(); 124 125 // Resets the symbol counters of all the CDF arrays to zero. Symbol counter is 126 // the last used element in the innermost dimension of each of the CDF array. 127 void ResetCounters(); 128 129 // Note kMaxAlignment allows for aligned instructions to be used in the 130 // copies done in Initialize(). 131 alignas(kMaxAlignment) uint16_t 132 partition_cdf[kBlockWidthCount][kPartitionContexts] 133 [kMaxPartitionTypes + 1]; 134 alignas(kMaxAlignment) uint16_t 135 segment_id_cdf[kSegmentIdContexts][kMaxSegments + 1]; 136 alignas(kMaxAlignment) uint16_t 137 use_predicted_segment_id_cdf[kUsePredictedSegmentIdContexts] 138 [kBooleanFieldCdfSize]; 139 alignas(kMaxAlignment) uint16_t skip_cdf[kSkipContexts][kBooleanFieldCdfSize]; 140 alignas(kMaxAlignment) uint16_t 141 skip_mode_cdf[kSkipModeContexts][kBooleanFieldCdfSize]; 142 alignas(kMaxAlignment) uint16_t delta_q_cdf[kDeltaSymbolCount + 1]; 143 alignas(kMaxAlignment) uint16_t delta_lf_cdf[kDeltaSymbolCount + 1]; 144 alignas(kMaxAlignment) uint16_t 145 delta_lf_multi_cdf[kFrameLfCount][kDeltaSymbolCount + 1]; 146 alignas(kMaxAlignment) uint16_t intra_block_copy_cdf[kBooleanFieldCdfSize]; 147 alignas(kMaxAlignment) uint16_t 148 intra_frame_y_mode_cdf[kIntraModeContexts][kIntraModeContexts] 149 [kIntraPredictionModesY + 1]; 150 alignas(kMaxAlignment) uint16_t 151 y_mode_cdf[kYModeContexts][kIntraPredictionModesY + 1]; 152 alignas(kMaxAlignment) uint16_t 153 angle_delta_cdf[kDirectionalIntraModes][kAngleDeltaSymbolCount + 1]; 154 alignas(kMaxAlignment) uint16_t 155 uv_mode_cdf[kBooleanSymbolCount][kIntraPredictionModesY] 156 [kIntraPredictionModesUV + 1]; 157 alignas(kMaxAlignment) uint16_t 158 cfl_alpha_signs_cdf[kCflAlphaSignsSymbolCount + 1]; 159 alignas(kMaxAlignment) uint16_t 160 cfl_alpha_cdf[kCflAlphaContexts][kCflAlphaSymbolCount + 1]; 161 alignas(kMaxAlignment) uint16_t 162 use_filter_intra_cdf[kMaxBlockSizes][kBooleanFieldCdfSize]; 163 alignas(kMaxAlignment) uint16_t 164 filter_intra_mode_cdf[kNumFilterIntraPredictors + 1]; 165 alignas(kMaxAlignment) uint16_t 166 tx_depth_cdf[4][kTxDepthContexts][kMaxTxDepthSymbolCount + 1]; 167 alignas(kMaxAlignment) uint16_t 168 tx_split_cdf[kTxSplitContexts][kBooleanFieldCdfSize]; 169 alignas(kMaxAlignment) uint16_t 170 all_zero_cdf[kNumSquareTransformSizes][kAllZeroContexts] 171 [kBooleanFieldCdfSize]; 172 alignas(kMaxAlignment) uint16_t 173 inter_tx_type_cdf[3][kNumExtendedTransformSizes][kNumTransformTypes + 1]; 174 alignas(kMaxAlignment) uint16_t 175 intra_tx_type_cdf[2][kNumExtendedTransformSizes][kIntraPredictionModesY] 176 [kNumTransformTypes + 1]; 177 alignas(kMaxAlignment) uint16_t 178 eob_pt_16_cdf[kNumPlaneTypes][kEobPtContexts][kEobPt16SymbolCount + 1]; 179 alignas(kMaxAlignment) uint16_t 180 eob_pt_32_cdf[kNumPlaneTypes][kEobPtContexts][kEobPt32SymbolCount + 1]; 181 alignas(kMaxAlignment) uint16_t 182 eob_pt_64_cdf[kNumPlaneTypes][kEobPtContexts][kEobPt64SymbolCount + 1]; 183 alignas(kMaxAlignment) uint16_t 184 eob_pt_128_cdf[kNumPlaneTypes][kEobPtContexts][kEobPt128SymbolCount + 1]; 185 alignas(kMaxAlignment) uint16_t 186 eob_pt_256_cdf[kNumPlaneTypes][kEobPtContexts][kEobPt256SymbolCount + 1]; 187 alignas(kMaxAlignment) uint16_t 188 eob_pt_512_cdf[kNumPlaneTypes][kEobPt512SymbolCount + 1]; 189 alignas(kMaxAlignment) uint16_t 190 eob_pt_1024_cdf[kNumPlaneTypes][kEobPt1024SymbolCount + 1]; 191 alignas(kMaxAlignment) uint16_t 192 eob_extra_cdf[kNumSquareTransformSizes][kNumPlaneTypes][kEobExtraContexts] 193 [kBooleanFieldCdfSize]; 194 alignas(kMaxAlignment) uint16_t 195 coeff_base_eob_cdf[kNumSquareTransformSizes][kNumPlaneTypes] 196 [kCoeffBaseEobContexts][kCoeffBaseEobSymbolCount + 1]; 197 alignas(kMaxAlignment) uint16_t 198 coeff_base_cdf[kNumSquareTransformSizes][kNumPlaneTypes] 199 [kCoeffBaseContexts][kCoeffBaseSymbolCount + 1]; 200 alignas(kMaxAlignment) uint16_t 201 coeff_base_range_cdf[kNumSquareTransformSizes][kNumPlaneTypes] 202 [kCoeffBaseRangeContexts] 203 [kCoeffBaseRangeSymbolCount + 1]; 204 alignas(kMaxAlignment) uint16_t 205 dc_sign_cdf[kNumPlaneTypes][kDcSignContexts][kBooleanFieldCdfSize]; 206 alignas(kMaxAlignment) uint16_t 207 restoration_type_cdf[kRestorationTypeSymbolCount + 1]; 208 alignas(kMaxAlignment) uint16_t use_wiener_cdf[kBooleanFieldCdfSize]; 209 alignas(kMaxAlignment) uint16_t use_sgrproj_cdf[kBooleanFieldCdfSize]; 210 alignas(kMaxAlignment) uint16_t 211 has_palette_y_cdf[kPaletteBlockSizeContexts][kPaletteYModeContexts] 212 [kBooleanFieldCdfSize]; 213 alignas(kMaxAlignment) uint16_t 214 palette_y_size_cdf[kPaletteBlockSizeContexts] 215 [kPaletteSizeSymbolCount + 1]; 216 alignas(kMaxAlignment) uint16_t 217 has_palette_uv_cdf[kPaletteUVModeContexts][kBooleanFieldCdfSize]; 218 alignas(kMaxAlignment) uint16_t 219 palette_uv_size_cdf[kPaletteBlockSizeContexts] 220 [kPaletteSizeSymbolCount + 1]; 221 alignas(kMaxAlignment) uint16_t 222 palette_color_index_cdf[kNumPlaneTypes][kPaletteSizeSymbolCount] 223 [kPaletteColorIndexContexts] 224 [kPaletteColorIndexSymbolCount + 1]; 225 alignas(kMaxAlignment) uint16_t 226 is_inter_cdf[kIsInterContexts][kBooleanFieldCdfSize]; 227 alignas(kMaxAlignment) uint16_t 228 use_compound_reference_cdf[kUseCompoundReferenceContexts] 229 [kBooleanFieldCdfSize]; 230 alignas(kMaxAlignment) uint16_t 231 compound_reference_type_cdf[kCompoundReferenceTypeContexts] 232 [kBooleanFieldCdfSize]; 233 alignas(kMaxAlignment) uint16_t 234 compound_reference_cdf[kNumCompoundReferenceTypes][kReferenceContexts][3] 235 [kBooleanFieldCdfSize]; 236 alignas(kMaxAlignment) uint16_t 237 compound_backward_reference_cdf[kReferenceContexts][2] 238 [kBooleanFieldCdfSize]; 239 alignas(kMaxAlignment) uint16_t 240 single_reference_cdf[kReferenceContexts][6][kBooleanFieldCdfSize]; 241 alignas(kMaxAlignment) uint16_t 242 compound_prediction_mode_cdf[kCompoundPredictionModeContexts] 243 [kNumCompoundInterPredictionModes + 1]; 244 alignas(kMaxAlignment) uint16_t 245 new_mv_cdf[kNewMvContexts][kBooleanFieldCdfSize]; 246 alignas(kMaxAlignment) uint16_t 247 zero_mv_cdf[kZeroMvContexts][kBooleanFieldCdfSize]; 248 alignas(kMaxAlignment) uint16_t 249 reference_mv_cdf[kReferenceMvContexts][kBooleanFieldCdfSize]; 250 alignas(kMaxAlignment) uint16_t 251 ref_mv_index_cdf[kRefMvIndexContexts][kBooleanFieldCdfSize]; 252 alignas(kMaxAlignment) uint16_t 253 is_inter_intra_cdf[kInterIntraContexts][kBooleanFieldCdfSize]; 254 alignas(kMaxAlignment) uint16_t 255 inter_intra_mode_cdf[kInterIntraContexts][kNumInterIntraModes + 1]; 256 alignas(kMaxAlignment) uint16_t 257 is_wedge_inter_intra_cdf[kMaxBlockSizes][kBooleanFieldCdfSize]; 258 alignas(kMaxAlignment) uint16_t 259 wedge_index_cdf[kMaxBlockSizes][kWedgeIndexSymbolCount + 1]; 260 alignas(kMaxAlignment) uint16_t 261 use_obmc_cdf[kMaxBlockSizes][kBooleanFieldCdfSize]; 262 alignas(kMaxAlignment) uint16_t 263 motion_mode_cdf[kMaxBlockSizes][kNumMotionModes + 1]; 264 alignas(kMaxAlignment) uint16_t 265 is_explicit_compound_type_cdf[kIsExplicitCompoundTypeContexts] 266 [kBooleanFieldCdfSize]; 267 alignas(kMaxAlignment) uint16_t 268 is_compound_type_average_cdf[kIsCompoundTypeAverageContexts] 269 [kBooleanFieldCdfSize]; 270 alignas(kMaxAlignment) uint16_t 271 compound_type_cdf[kMaxBlockSizes] 272 [kNumExplicitCompoundPredictionTypes + 1]; 273 alignas(kMaxAlignment) uint16_t 274 interpolation_filter_cdf[kInterpolationFilterContexts] 275 [kNumExplicitInterpolationFilters + 1]; 276 alignas(kMaxAlignment) uint16_t 277 mv_joint_cdf[kMvContexts][kNumMvJointTypes + 1]; 278 alignas(kMaxAlignment) uint16_t 279 mv_sign_cdf[kMvContexts][kNumMvComponents][kBooleanFieldCdfSize]; 280 alignas(kMaxAlignment) uint16_t 281 mv_class_cdf[kMvContexts][kNumMvComponents][kMvClassSymbolCount + 1]; 282 alignas(kMaxAlignment) uint16_t 283 mv_class0_bit_cdf[kMvContexts][kNumMvComponents][kBooleanFieldCdfSize]; 284 alignas(kMaxAlignment) uint16_t 285 mv_class0_fraction_cdf[kMvContexts][kNumMvComponents][kBooleanSymbolCount] 286 [kMvFractionSymbolCount + 1]; 287 alignas(kMaxAlignment) uint16_t 288 mv_class0_high_precision_cdf[kMvContexts][kNumMvComponents] 289 [kBooleanFieldCdfSize]; 290 alignas(kMaxAlignment) uint16_t 291 mv_bit_cdf[kMvContexts][kNumMvComponents][kMvBitSymbolCount] 292 [kBooleanFieldCdfSize]; 293 alignas(kMaxAlignment) uint16_t mv_fraction_cdf[kMvContexts][kNumMvComponents] 294 [kMvFractionSymbolCount + 1]; 295 alignas(kMaxAlignment) uint16_t 296 mv_high_precision_cdf[kMvContexts][kNumMvComponents] 297 [kBooleanFieldCdfSize]; 298 }; 299 300 } // namespace libgav1 301 #endif // LIBGAV1_SRC_SYMBOL_DECODER_CONTEXT_H_ 302