• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
21 
22 #include "src/dsp/constants.h"
23 #include "src/utils/constants.h"
24 
25 namespace libgav1 {
26 
27 enum {
28   kPartitionContexts = 4,
29   kSegmentIdContexts = 3,
30   kUsePredictedSegmentIdContexts = 3,
31   kSkipContexts = 3,
32   kSkipModeContexts = 3,
33   kBooleanFieldCdfSize = 3,
34   kDeltaSymbolCount = 4,  // Used for both delta_q and delta_lf.
35   kIntraModeContexts = 5,
36   kYModeContexts = 4,
37   kAngleDeltaSymbolCount = 2 * kMaxAngleDelta + 1,
38   kCflAlphaSignsSymbolCount = 8,
39   kCflAlphaContexts = 6,
40   kCflAlphaSymbolCount = 16,
41   kTxDepthContexts = 3,
42   kMaxTxDepthSymbolCount = 3,
43   kTxSplitContexts = 21,
44   kCoefficientQuantizerContexts = 4,
45   kNumSquareTransformSizes = 5,
46   kAllZeroContexts = 13,
47   kNumExtendedTransformSizes = 4,
48   kEobPtContexts = 2,
49   kEobPt16SymbolCount = 5,
50   kEobPt32SymbolCount = 6,
51   kEobPt64SymbolCount = 7,
52   kEobPt128SymbolCount = 8,
53   kEobPt256SymbolCount = 9,
54   kEobPt512SymbolCount = 10,
55   kEobPt1024SymbolCount = 11,
56   kEobExtraContexts = 9,
57   kCoeffBaseEobContexts = 4,
58   kCoeffBaseEobSymbolCount = 3,
59   kCoeffBaseContexts = 42,
60   kCoeffBaseSymbolCount = 4,
61   kCoeffBaseRangeContexts = 21,
62   kCoeffBaseRangeSymbolCount = 4,
63   kDcSignContexts = 3,
64   kPaletteBlockSizeContexts = 7,
65   kPaletteYModeContexts = 3,
66   kPaletteUVModeContexts = 2,
67   kPaletteSizeSymbolCount = 7,
68   kPaletteColorIndexContexts = 5,
69   kPaletteColorIndexSymbolCount = 8,
70   kIsInterContexts = 4,
71   kUseCompoundReferenceContexts = 5,
72   kCompoundReferenceTypeContexts = 5,
73   kReferenceContexts = 3,
74   kCompoundPredictionModeContexts = 8,
75   kNewMvContexts = 6,
76   kZeroMvContexts = 2,
77   kReferenceMvContexts = 6,
78   kRefMvIndexContexts = 3,
79   kInterIntraContexts = 3,
80   kWedgeIndexSymbolCount = 16,
81   kIsExplicitCompoundTypeContexts = 6,
82   kIsCompoundTypeAverageContexts = 6,
83   kInterpolationFilterContexts = 16,
84   kMvContexts = 2,
85   kMvClassSymbolCount = 11,
86   kMvFractionSymbolCount = 4,
87   kMvBitSymbolCount = 10,
88   kNumMvComponents = 2,
89 };  // anonymous enum
90 
91 struct SymbolDecoderContext {
92   SymbolDecoderContext() = default;
SymbolDecoderContextSymbolDecoderContext93   explicit SymbolDecoderContext(int base_quantizer_index) {
94     Initialize(base_quantizer_index);
95   }
96 
97   void Initialize(int base_quantizer_index);
98 
99   // Partition related variables and functions.
100   static int PartitionCdfSize(int block_size_log2);
101 
102   // Returns the cdf array index for inter_tx_type or intra_tx_type based on
103   // |tx_set|.
104   static int TxTypeIndex(TransformSet tx_set);
105 
106   // Resets the intra_frame_y_mode_cdf array to the default.
107   void ResetIntraFrameYModeCdf();
108 
109   // Resets the symbol counters of all the CDF arrays to zero. Symbol counter is
110   // the last used element in the innermost dimension of each of the CDF array.
111   void ResetCounters();
112 
113   uint16_t partition_cdf[kBlockWidthCount][kPartitionContexts]
114                         [kMaxPartitionTypes + 1];
115   uint16_t segment_id_cdf[kSegmentIdContexts][kMaxSegments + 1];
116   uint16_t use_predicted_segment_id_cdf[kUsePredictedSegmentIdContexts]
117                                        [kBooleanFieldCdfSize];
118   uint16_t skip_cdf[kSkipContexts][kBooleanFieldCdfSize];
119   uint16_t skip_mode_cdf[kSkipModeContexts][kBooleanFieldCdfSize];
120   uint16_t delta_q_cdf[kDeltaSymbolCount + 1];
121   uint16_t delta_lf_cdf[kDeltaSymbolCount + 1];
122   uint16_t delta_lf_multi_cdf[kFrameLfCount][kDeltaSymbolCount + 1];
123   uint16_t intra_block_copy_cdf[kBooleanFieldCdfSize];
124   uint16_t intra_frame_y_mode_cdf[kIntraModeContexts][kIntraModeContexts]
125                                  [kIntraPredictionModesY + 1];
126   uint16_t y_mode_cdf[kYModeContexts][kIntraPredictionModesY + 1];
127   uint16_t angle_delta_cdf[kDirectionalIntraModes][kAngleDeltaSymbolCount + 1];
128   uint16_t uv_mode_cdf[kBooleanSymbolCount][kIntraPredictionModesY]
129                       [kIntraPredictionModesUV + 1];
130   uint16_t cfl_alpha_signs_cdf[kCflAlphaSignsSymbolCount + 1];
131   uint16_t cfl_alpha_cdf[kCflAlphaContexts][kCflAlphaSymbolCount + 1];
132   uint16_t use_filter_intra_cdf[kMaxBlockSizes][kBooleanFieldCdfSize];
133   uint16_t filter_intra_mode_cdf[kNumFilterIntraPredictors + 1];
134   uint16_t tx_depth_cdf[4][kTxDepthContexts][kMaxTxDepthSymbolCount + 1];
135   uint16_t tx_split_cdf[kTxSplitContexts][kBooleanFieldCdfSize];
136   uint16_t all_zero_cdf[kNumSquareTransformSizes][kAllZeroContexts]
137                        [kBooleanFieldCdfSize];
138   uint16_t inter_tx_type_cdf[3][kNumExtendedTransformSizes]
139                             [kNumTransformTypes + 1];
140   uint16_t intra_tx_type_cdf[2][kNumExtendedTransformSizes]
141                             [kIntraPredictionModesY][kNumTransformTypes + 1];
142   uint16_t eob_pt_16_cdf[kNumPlaneTypes][kEobPtContexts]
143                         [kEobPt16SymbolCount + 1];
144   uint16_t eob_pt_32_cdf[kNumPlaneTypes][kEobPtContexts]
145                         [kEobPt32SymbolCount + 1];
146   uint16_t eob_pt_64_cdf[kNumPlaneTypes][kEobPtContexts]
147                         [kEobPt64SymbolCount + 1];
148   uint16_t eob_pt_128_cdf[kNumPlaneTypes][kEobPtContexts]
149                          [kEobPt128SymbolCount + 1];
150   uint16_t eob_pt_256_cdf[kNumPlaneTypes][kEobPtContexts]
151                          [kEobPt256SymbolCount + 1];
152   uint16_t eob_pt_512_cdf[kNumPlaneTypes][kEobPt512SymbolCount + 1];
153   uint16_t eob_pt_1024_cdf[kNumPlaneTypes][kEobPt1024SymbolCount + 1];
154   uint16_t eob_extra_cdf[kNumSquareTransformSizes][kNumPlaneTypes]
155                         [kEobExtraContexts][kBooleanFieldCdfSize];
156   uint16_t coeff_base_eob_cdf[kNumSquareTransformSizes][kNumPlaneTypes]
157                              [kCoeffBaseEobContexts]
158                              [kCoeffBaseEobSymbolCount + 1];
159   uint16_t coeff_base_cdf[kNumSquareTransformSizes][kNumPlaneTypes]
160                          [kCoeffBaseContexts][kCoeffBaseSymbolCount + 1];
161   uint16_t coeff_base_range_cdf[kNumSquareTransformSizes][kNumPlaneTypes]
162                                [kCoeffBaseRangeContexts]
163                                [kCoeffBaseRangeSymbolCount + 1];
164   uint16_t dc_sign_cdf[kNumPlaneTypes][kDcSignContexts][kBooleanFieldCdfSize];
165   uint16_t restoration_type_cdf[kRestorationTypeSymbolCount + 1];
166   uint16_t use_wiener_cdf[kBooleanFieldCdfSize];
167   uint16_t use_sgrproj_cdf[kBooleanFieldCdfSize];
168   uint16_t has_palette_y_cdf[kPaletteBlockSizeContexts][kPaletteYModeContexts]
169                             [kBooleanFieldCdfSize];
170   uint16_t palette_y_size_cdf[kPaletteBlockSizeContexts]
171                              [kPaletteSizeSymbolCount + 1];
172   uint16_t has_palette_uv_cdf[kPaletteUVModeContexts][kBooleanFieldCdfSize];
173   uint16_t palette_uv_size_cdf[kPaletteBlockSizeContexts]
174                               [kPaletteSizeSymbolCount + 1];
175   uint16_t palette_color_index_cdf[kNumPlaneTypes][kPaletteSizeSymbolCount]
176                                   [kPaletteColorIndexContexts]
177                                   [kPaletteColorIndexSymbolCount + 1];
178   uint16_t is_inter_cdf[kIsInterContexts][kBooleanFieldCdfSize];
179   uint16_t use_compound_reference_cdf[kUseCompoundReferenceContexts]
180                                      [kBooleanFieldCdfSize];
181   uint16_t compound_reference_type_cdf[kCompoundReferenceTypeContexts]
182                                       [kBooleanFieldCdfSize];
183   uint16_t compound_reference_cdf[kNumCompoundReferenceTypes]
184                                  [kReferenceContexts][3][kBooleanFieldCdfSize];
185   uint16_t compound_backward_reference_cdf[kReferenceContexts][2]
186                                           [kBooleanFieldCdfSize];
187   uint16_t single_reference_cdf[kReferenceContexts][6][kBooleanFieldCdfSize];
188   uint16_t compound_prediction_mode_cdf[kCompoundPredictionModeContexts]
189                                        [kNumCompoundInterPredictionModes + 1];
190   uint16_t new_mv_cdf[kNewMvContexts][kBooleanFieldCdfSize];
191   uint16_t zero_mv_cdf[kZeroMvContexts][kBooleanFieldCdfSize];
192   uint16_t reference_mv_cdf[kReferenceMvContexts][kBooleanFieldCdfSize];
193   uint16_t ref_mv_index_cdf[kRefMvIndexContexts][kBooleanFieldCdfSize];
194   uint16_t is_inter_intra_cdf[kInterIntraContexts][kBooleanFieldCdfSize];
195   uint16_t inter_intra_mode_cdf[kInterIntraContexts][kNumInterIntraModes + 1];
196   uint16_t is_wedge_inter_intra_cdf[kMaxBlockSizes][kBooleanFieldCdfSize];
197   uint16_t wedge_index_cdf[kMaxBlockSizes][kWedgeIndexSymbolCount + 1];
198   uint16_t use_obmc_cdf[kMaxBlockSizes][kBooleanFieldCdfSize];
199   uint16_t motion_mode_cdf[kMaxBlockSizes][kNumMotionModes + 1];
200   uint16_t is_explicit_compound_type_cdf[kIsExplicitCompoundTypeContexts]
201                                         [kBooleanFieldCdfSize];
202   uint16_t is_compound_type_average_cdf[kIsCompoundTypeAverageContexts]
203                                        [kBooleanFieldCdfSize];
204   uint16_t compound_type_cdf[kMaxBlockSizes]
205                             [kNumExplicitCompoundPredictionTypes + 1];
206   uint16_t interpolation_filter_cdf[kInterpolationFilterContexts]
207                                    [kNumExplicitInterpolationFilters + 1];
208   uint16_t mv_joint_cdf[kMvContexts][kNumMvJointTypes + 1];
209   uint16_t mv_sign_cdf[kMvContexts][kNumMvComponents][kBooleanFieldCdfSize];
210   uint16_t mv_class_cdf[kMvContexts][kNumMvComponents][kMvClassSymbolCount + 1];
211   uint16_t mv_class0_bit_cdf[kMvContexts][kNumMvComponents]
212                             [kBooleanFieldCdfSize];
213   uint16_t mv_class0_fraction_cdf[kMvContexts][kNumMvComponents]
214                                  [kBooleanSymbolCount]
215                                  [kMvFractionSymbolCount + 1];
216   uint16_t mv_class0_high_precision_cdf[kMvContexts][kNumMvComponents]
217                                        [kBooleanFieldCdfSize];
218   uint16_t mv_bit_cdf[kMvContexts][kNumMvComponents][kMvBitSymbolCount]
219                      [kBooleanFieldCdfSize];
220   uint16_t mv_fraction_cdf[kMvContexts][kNumMvComponents]
221                           [kMvFractionSymbolCount + 1];
222   uint16_t mv_high_precision_cdf[kMvContexts][kNumMvComponents]
223                                 [kBooleanFieldCdfSize];
224 };
225 
226 }  // namespace libgav1
227 #endif  // LIBGAV1_SRC_SYMBOL_DECODER_CONTEXT_H_
228