• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/symbol_decoder_context.h"
16 
17 #include <cassert>
18 #include <cstring>
19 #include <type_traits>
20 
21 namespace libgav1 {
22 namespace {
23 
24 // Import all the constants in the anonymous namespace.
25 #include "src/symbol_decoder_context_cdfs.inc"
26 
GetQuantizerContext(int base_quantizer_index)27 uint8_t GetQuantizerContext(int base_quantizer_index) {
28   if (base_quantizer_index <= 20) return 0;
29   if (base_quantizer_index <= 60) return 1;
30   if (base_quantizer_index <= 120) return 2;
31   return 3;
32 }
33 
34 // Reset*Counters() are helper functions to reset the CDF arrays where the
35 // counters are not in the last element of the innermost dimension.
36 
ResetPartitionCounters(SymbolDecoderContext * const context)37 void ResetPartitionCounters(SymbolDecoderContext* const context) {
38   int block_size_log2 = k4x4WidthLog2[kBlock8x8];
39   for (auto& d1 : context->partition_cdf) {
40     const int cdf_size =
41         SymbolDecoderContext::PartitionCdfSize(block_size_log2++);
42     for (auto& d2 : d1) {
43       d2[cdf_size] = 0;
44     }
45   }
46 }
47 
ResetPaletteColorIndexCounters(SymbolDecoderContext * const context)48 void ResetPaletteColorIndexCounters(SymbolDecoderContext* const context) {
49   for (auto& d1 : context->palette_color_index_cdf) {
50     int cdf_size = kMinPaletteSize;
51     for (auto& d2 : d1) {
52       for (auto& d3 : d2) {
53         d3[cdf_size] = 0;
54       }
55       ++cdf_size;
56     }
57   }
58 }
59 
ResetTxTypeCounters(SymbolDecoderContext * const context)60 void ResetTxTypeCounters(SymbolDecoderContext* const context) {
61   int set_index = kTransformSetIntra1;
62   for (auto& d1 : context->intra_tx_type_cdf) {
63     const int cdf_size = kNumTransformTypesInSet[set_index++];
64     for (auto& d2 : d1) {
65       for (auto& d3 : d2) {
66         d3[cdf_size] = 0;
67       }
68     }
69   }
70   for (auto& d1 : context->inter_tx_type_cdf) {
71     const int cdf_size = kNumTransformTypesInSet[set_index++];
72     for (auto& d2 : d1) {
73       d2[cdf_size] = 0;
74     }
75   }
76 }
77 
ResetTxDepthCounters(SymbolDecoderContext * const context)78 void ResetTxDepthCounters(SymbolDecoderContext* const context) {
79   int delta = 1;
80   for (auto& d1 : context->tx_depth_cdf) {
81     const int cdf_size = kMaxTxDepthSymbolCount - delta;
82     delta = 0;
83     for (auto& d2 : d1) {
84       d2[cdf_size] = 0;
85     }
86   }
87 }
88 
ResetUVModeCounters(SymbolDecoderContext * const context)89 void ResetUVModeCounters(SymbolDecoderContext* const context) {
90   int cdf_size = kIntraPredictionModesUV - 1;
91   for (auto& d1 : context->uv_mode_cdf) {
92     for (auto& d2 : d1) {
93       d2[cdf_size] = 0;
94     }
95     ++cdf_size;
96   }
97 }
98 
99 }  // namespace
100 
101 #define CDF_COPY(source, destination)                       \
102   static_assert(sizeof(source) == sizeof(destination), ""); \
103   memcpy(destination, source, sizeof(source))
104 
Initialize(int base_quantizer_index)105 void SymbolDecoderContext::Initialize(int base_quantizer_index) {
106   CDF_COPY(kDefaultPartitionCdf, partition_cdf);
107   CDF_COPY(kDefaultSkipCdf, skip_cdf);
108   CDF_COPY(kDefaultSkipModeCdf, skip_mode_cdf);
109   CDF_COPY(kDefaultSegmentIdCdf, segment_id_cdf);
110   CDF_COPY(kDefaultUsePredictedSegmentIdCdf, use_predicted_segment_id_cdf);
111   CDF_COPY(kDefaultDeltaQCdf, delta_q_cdf);
112   CDF_COPY(kDefaultDeltaQCdf, delta_lf_cdf);
113   for (auto& delta_lf_multi_cdf_entry : delta_lf_multi_cdf) {
114     CDF_COPY(kDefaultDeltaQCdf, delta_lf_multi_cdf_entry);
115   }
116   CDF_COPY(kDefaultIntraBlockCopyCdf, intra_block_copy_cdf);
117   CDF_COPY(kDefaultIntraFrameYModeCdf, intra_frame_y_mode_cdf);
118   CDF_COPY(kDefaultYModeCdf, y_mode_cdf);
119   CDF_COPY(kDefaultAngleDeltaCdf, angle_delta_cdf);
120   CDF_COPY(kDefaultUVModeCdf, uv_mode_cdf);
121   CDF_COPY(kDefaultCflAlphaSignsCdf, cfl_alpha_signs_cdf);
122   CDF_COPY(kDefaultCflAlphaCdf, cfl_alpha_cdf);
123   CDF_COPY(kDefaultUseFilterIntraCdf, use_filter_intra_cdf);
124   CDF_COPY(kDefaultFilterIntraModeCdf, filter_intra_mode_cdf);
125   CDF_COPY(kDefaultTxDepthCdf, tx_depth_cdf);
126   CDF_COPY(kDefaultTxSplitCdf, tx_split_cdf);
127   CDF_COPY(kDefaultInterTxTypeCdf, inter_tx_type_cdf);
128   CDF_COPY(kDefaultIntraTxTypeCdf, intra_tx_type_cdf);
129   CDF_COPY(kDefaultRestorationTypeCdf, restoration_type_cdf);
130   CDF_COPY(kDefaultUseWienerCdf, use_wiener_cdf);
131   CDF_COPY(kDefaultUseSgrProjCdf, use_sgrproj_cdf);
132   CDF_COPY(kDefaultHasPaletteYCdf, has_palette_y_cdf);
133   CDF_COPY(kDefaultPaletteYSizeCdf, palette_y_size_cdf);
134   CDF_COPY(kDefaultHasPaletteUVCdf, has_palette_uv_cdf);
135   CDF_COPY(kDefaultPaletteUVSizeCdf, palette_uv_size_cdf);
136   CDF_COPY(kDefaultPaletteColorIndexCdf, palette_color_index_cdf);
137   CDF_COPY(kDefaultIsInterCdf, is_inter_cdf);
138   CDF_COPY(kDefaultUseCompoundReferenceCdf, use_compound_reference_cdf);
139   CDF_COPY(kDefaultCompoundReferenceTypeCdf, compound_reference_type_cdf);
140   CDF_COPY(kDefaultCompoundReferenceCdf, compound_reference_cdf);
141   CDF_COPY(kDefaultCompoundBackwardReferenceCdf,
142            compound_backward_reference_cdf);
143   CDF_COPY(kDefaultSingleReferenceCdf, single_reference_cdf);
144   CDF_COPY(kDefaultCompoundPredictionModeCdf, compound_prediction_mode_cdf);
145   CDF_COPY(kDefaultNewMvCdf, new_mv_cdf);
146   CDF_COPY(kDefaultZeroMvCdf, zero_mv_cdf);
147   CDF_COPY(kDefaultReferenceMvCdf, reference_mv_cdf);
148   CDF_COPY(kDefaultRefMvIndexCdf, ref_mv_index_cdf);
149   CDF_COPY(kDefaultIsInterIntraCdf, is_inter_intra_cdf);
150   CDF_COPY(kDefaultInterIntraModeCdf, inter_intra_mode_cdf);
151   CDF_COPY(kDefaultIsWedgeInterIntraCdf, is_wedge_inter_intra_cdf);
152   CDF_COPY(kDefaultWedgeIndexCdf, wedge_index_cdf);
153   CDF_COPY(kDefaultUseObmcCdf, use_obmc_cdf);
154   CDF_COPY(kDefaultMotionModeCdf, motion_mode_cdf);
155   CDF_COPY(kDefaultIsExplicitCompoundTypeCdf, is_explicit_compound_type_cdf);
156   CDF_COPY(kDefaultIsCompoundTypeAverageCdf, is_compound_type_average_cdf);
157   CDF_COPY(kDefaultCompoundTypeCdf, compound_type_cdf);
158   CDF_COPY(kDefaultInterpolationFilterCdf, interpolation_filter_cdf);
159   for (int i = 0; i < kMvContexts; ++i) {
160     CDF_COPY(kDefaultMvJointCdf, mv_joint_cdf[i]);
161     for (int j = 0; j < kNumMvComponents; ++j) {
162       CDF_COPY(kDefaultMvSignCdf, mv_sign_cdf[i][j]);
163       CDF_COPY(kDefaultMvClassCdf, mv_class_cdf[i][j]);
164       CDF_COPY(kDefaultMvClass0BitCdf, mv_class0_bit_cdf[i][j]);
165       CDF_COPY(kDefaultMvClass0FractionCdf, mv_class0_fraction_cdf[i][j]);
166       CDF_COPY(kDefaultMvClass0HighPrecisionCdf,
167                mv_class0_high_precision_cdf[i][j]);
168       CDF_COPY(kDefaultMvBitCdf, mv_bit_cdf[i][j]);
169       CDF_COPY(kDefaultMvFractionCdf, mv_fraction_cdf[i][j]);
170       CDF_COPY(kDefaultMvHighPrecisionCdf, mv_high_precision_cdf[i][j]);
171     }
172   }
173   const int quantizer_context = GetQuantizerContext(base_quantizer_index);
174   CDF_COPY(kDefaultAllZeroCdf[quantizer_context], all_zero_cdf);
175   CDF_COPY(kDefaultEobPt16Cdf[quantizer_context], eob_pt_16_cdf);
176   CDF_COPY(kDefaultEobPt32Cdf[quantizer_context], eob_pt_32_cdf);
177   CDF_COPY(kDefaultEobPt64Cdf[quantizer_context], eob_pt_64_cdf);
178   CDF_COPY(kDefaultEobPt128Cdf[quantizer_context], eob_pt_128_cdf);
179   CDF_COPY(kDefaultEobPt256Cdf[quantizer_context], eob_pt_256_cdf);
180   CDF_COPY(kDefaultEobPt512Cdf[quantizer_context], eob_pt_512_cdf);
181   CDF_COPY(kDefaultEobPt1024Cdf[quantizer_context], eob_pt_1024_cdf);
182   CDF_COPY(kDefaultEobExtraCdf[quantizer_context], eob_extra_cdf);
183   CDF_COPY(kDefaultCoeffBaseEobCdf[quantizer_context], coeff_base_eob_cdf);
184   CDF_COPY(kDefaultCoeffBaseCdf[quantizer_context], coeff_base_cdf);
185   CDF_COPY(kDefaultCoeffBaseRangeCdf[quantizer_context], coeff_base_range_cdf);
186   CDF_COPY(kDefaultDcSignCdf[quantizer_context], dc_sign_cdf);
187 }
188 
ResetIntraFrameYModeCdf()189 void SymbolDecoderContext::ResetIntraFrameYModeCdf() {
190   CDF_COPY(kDefaultIntraFrameYModeCdf, intra_frame_y_mode_cdf);
191 }
192 
193 #undef CDF_COPY
194 
195 // These macros set the last element in the inner-most dimension of the array to
196 // zero.
197 #define RESET_COUNTER_1D(array)                              \
198   do {                                                       \
199     (array)[std::extent<decltype(array), 0>::value - 1] = 0; \
200   } while (false)
201 
202 #define RESET_COUNTER_2D(array)                           \
203   do {                                                    \
204     for (auto& d1 : (array)) {                            \
205       d1[std::extent<decltype(array), 1>::value - 1] = 0; \
206     }                                                     \
207   } while (false)
208 
209 #define RESET_COUNTER_3D(array)                             \
210   do {                                                      \
211     for (auto& d1 : (array)) {                              \
212       for (auto& d2 : d1) {                                 \
213         d2[std::extent<decltype(array), 2>::value - 1] = 0; \
214       }                                                     \
215     }                                                       \
216   } while (false)
217 
218 #define RESET_COUNTER_4D(array)                               \
219   do {                                                        \
220     for (auto& d1 : (array)) {                                \
221       for (auto& d2 : d1) {                                   \
222         for (auto& d3 : d2) {                                 \
223           d3[std::extent<decltype(array), 3>::value - 1] = 0; \
224         }                                                     \
225       }                                                       \
226     }                                                         \
227   } while (false)
228 
ResetCounters()229 void SymbolDecoderContext::ResetCounters() {
230   ResetPartitionCounters(this);
231   RESET_COUNTER_2D(segment_id_cdf);
232   RESET_COUNTER_2D(use_predicted_segment_id_cdf);
233   RESET_COUNTER_2D(skip_cdf);
234   RESET_COUNTER_2D(skip_mode_cdf);
235   RESET_COUNTER_1D(delta_q_cdf);
236   RESET_COUNTER_1D(delta_lf_cdf);
237   RESET_COUNTER_2D(delta_lf_multi_cdf);
238   RESET_COUNTER_1D(intra_block_copy_cdf);
239   RESET_COUNTER_3D(intra_frame_y_mode_cdf);
240   RESET_COUNTER_2D(y_mode_cdf);
241   RESET_COUNTER_2D(angle_delta_cdf);
242   ResetUVModeCounters(this);
243   RESET_COUNTER_1D(cfl_alpha_signs_cdf);
244   RESET_COUNTER_2D(cfl_alpha_cdf);
245   RESET_COUNTER_2D(use_filter_intra_cdf);
246   RESET_COUNTER_1D(filter_intra_mode_cdf);
247   ResetTxDepthCounters(this);
248   RESET_COUNTER_2D(tx_split_cdf);
249   RESET_COUNTER_3D(all_zero_cdf);
250   ResetTxTypeCounters(this);
251   RESET_COUNTER_3D(eob_pt_16_cdf);
252   RESET_COUNTER_3D(eob_pt_32_cdf);
253   RESET_COUNTER_3D(eob_pt_64_cdf);
254   RESET_COUNTER_3D(eob_pt_128_cdf);
255   RESET_COUNTER_3D(eob_pt_256_cdf);
256   RESET_COUNTER_2D(eob_pt_512_cdf);
257   RESET_COUNTER_2D(eob_pt_1024_cdf);
258   RESET_COUNTER_4D(eob_extra_cdf);
259   RESET_COUNTER_4D(coeff_base_eob_cdf);
260   RESET_COUNTER_4D(coeff_base_cdf);
261   RESET_COUNTER_4D(coeff_base_range_cdf);
262   RESET_COUNTER_3D(dc_sign_cdf);
263   RESET_COUNTER_1D(restoration_type_cdf);
264   RESET_COUNTER_1D(use_wiener_cdf);
265   RESET_COUNTER_1D(use_sgrproj_cdf);
266   RESET_COUNTER_3D(has_palette_y_cdf);
267   RESET_COUNTER_2D(palette_y_size_cdf);
268   RESET_COUNTER_2D(has_palette_uv_cdf);
269   RESET_COUNTER_2D(palette_uv_size_cdf);
270   ResetPaletteColorIndexCounters(this);
271   RESET_COUNTER_2D(is_inter_cdf);
272   RESET_COUNTER_2D(use_compound_reference_cdf);
273   RESET_COUNTER_2D(compound_reference_type_cdf);
274   RESET_COUNTER_4D(compound_reference_cdf);
275   RESET_COUNTER_3D(compound_backward_reference_cdf);
276   RESET_COUNTER_3D(single_reference_cdf);
277   RESET_COUNTER_2D(compound_prediction_mode_cdf);
278   RESET_COUNTER_2D(new_mv_cdf);
279   RESET_COUNTER_2D(zero_mv_cdf);
280   RESET_COUNTER_2D(reference_mv_cdf);
281   RESET_COUNTER_2D(ref_mv_index_cdf);
282   RESET_COUNTER_2D(is_inter_intra_cdf);
283   RESET_COUNTER_2D(inter_intra_mode_cdf);
284   RESET_COUNTER_2D(is_wedge_inter_intra_cdf);
285   RESET_COUNTER_2D(wedge_index_cdf);
286   RESET_COUNTER_2D(use_obmc_cdf);
287   RESET_COUNTER_2D(motion_mode_cdf);
288   RESET_COUNTER_2D(is_explicit_compound_type_cdf);
289   RESET_COUNTER_2D(is_compound_type_average_cdf);
290   RESET_COUNTER_2D(compound_type_cdf);
291   RESET_COUNTER_2D(interpolation_filter_cdf);
292   RESET_COUNTER_2D(mv_joint_cdf);
293   RESET_COUNTER_3D(mv_sign_cdf);
294   RESET_COUNTER_3D(mv_class_cdf);
295   RESET_COUNTER_3D(mv_class0_bit_cdf);
296   RESET_COUNTER_4D(mv_class0_fraction_cdf);
297   RESET_COUNTER_3D(mv_class0_high_precision_cdf);
298   RESET_COUNTER_4D(mv_bit_cdf);
299   RESET_COUNTER_3D(mv_fraction_cdf);
300   RESET_COUNTER_3D(mv_high_precision_cdf);
301 }
302 
303 #undef RESET_COUNTER_1D
304 #undef RESET_COUNTER_2D
305 #undef RESET_COUNTER_3D
306 #undef RESET_COUNTER_4D
307 
PartitionCdfSize(int block_size_log2)308 int SymbolDecoderContext::PartitionCdfSize(int block_size_log2) {
309   assert(block_size_log2 > 0);
310   assert(block_size_log2 < 6);
311 
312   switch (block_size_log2) {
313     case 1:
314       return kPartitionSplit + 1;
315     case 5:
316       return kPartitionVerticalWithRightSplit + 1;
317     default:
318       return kMaxPartitionTypes;
319   }
320 }
321 
322 }  // namespace libgav1
323