1 /* -----------------------------------------------------------------------------
2 The copyright in this software is being made available under the BSD
3 License, included below. No patent rights, trademark rights and/or
4 other Intellectual Property Rights other than the copyrights concerning
5 the Software are granted under this license.
6 
7 For any license concerning other Intellectual Property rights than the software,
8 especially patent licenses, a separate Agreement needs to be closed.
9 For more information please contact:
10 
11 Fraunhofer Heinrich Hertz Institute
12 Einsteinufer 37
13 10587 Berlin, Germany
14 www.hhi.fraunhofer.de/vvc
15 vvc@hhi.fraunhofer.de
16 
17 Copyright (c) 2018-2021, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
18 All rights reserved.
19 
20 Redistribution and use in source and binary forms, with or without
21 modification, are permitted provided that the following conditions are met:
22 
23  * Redistributions of source code must retain the above copyright notice,
24    this list of conditions and the following disclaimer.
25  * Redistributions in binary form must reproduce the above copyright notice,
26    this list of conditions and the following disclaimer in the documentation
27    and/or other materials provided with the distribution.
28  * Neither the name of Fraunhofer nor the names of its contributors may
29    be used to endorse or promote products derived from this software without
30    specific prior written permission.
31 
32 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
36 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
42 THE POSSIBILITY OF SUCH DAMAGE.
43 
44 
45 ------------------------------------------------------------------------------------------- */
46 
47 /** \file     TypeDef.h
48     \brief    Define macros, basic types, new types and enumerations
49 */
50 
51 #pragma once
52 
53 #ifndef COMMONDEF_H
54 #error Include CommonDef.h not TypeDef.h
55 #endif
56 
57 #include <vector>
58 #include <utility>
59 #include <sstream>
60 #include <cstddef>
61 #include <cstring>
62 #include <assert.h>
63 #include <cassert>
64 
65 namespace vvdec
66 {
67 
68 #if __SANITIZE_ADDRESS__
69 // macro to enable workarounds for address-sanitizer false-positives
70 # define ASAN_WORKAROUND                                  1
71 #endif
72 
73 #define RECO_WHILE_PARSE                                  1
74 #define ALLOW_MIDER_LF_DURING_PICEXT                      1
75 
76 #define JVET_O1170_CHECK_BV_AT_DECODER                    0 // For decoder to check if a BV is valid or not
77 
78 #define DISABLE_CONFROMANCE_CHECK                         1
79 #define DISABLE_CHECK_NO_OUTPUT_PRIOR_PICS_FLAG           0
80 
81 #define RPR_FIX                                           1
82 #define RPR_YUV_OUTPUT                                    1
83 #define RPR_OUTPUT_MSG                                    1
84 
85 #define GDR_ADJ                                           1
86 #define ALF_FIX                                           1
87 
88 #define TBC                                               0
89 
90 #define JVET_R0270                                        TBC // JVET-S0270: Treating picture with mixed RASL and RADL slices as RASL picture
91 #define JVET_S0155_EOS_NALU_CHECK                         TBC // JVET-S0155: Constraints on EOS NAL units
92 
93 // ====================================================================================================================
94 // NEXT software switches
95 // ====================================================================================================================
96 
97 #if 0                                                       // set to 1 to avoid valgrind errors concerning uninitilized memory
98 #define VALGRIND_MEMCLEAR( ref )                           memset(ref,0,sizeof(ref))
99 #define VALGRIND_MEMZERO( ref,size )                       memset(ref,0,size)
100 #else
101 #define VALGRIND_MEMCLEAR( ref )
102 #define VALGRIND_MEMZERO( ref,size )
103 #endif
104 
105 #ifndef ENABLE_TRACING
106 #define ENABLE_TRACING                                    0 // DISABLE by default (enable only when debugging)
107 #endif // ! ENABLE_TRACING
108 
109 #ifndef ENABLE_TIME_PROFILING
110 #define ENABLE_TIME_PROFILING                             0 // DISABLED by default (can be enabled by project configuration or make command)
111 #endif
112 #ifndef ENABLE_TIME_PROFILING_PIC_TYPES
113 #define ENABLE_TIME_PROFILING_PIC_TYPES                   0 // DISABLED by default (can be enabled by project configuration or make command)
114 #endif
115 #ifndef ENABLE_TIME_PROFILING_CU_SHAPES
116 #define ENABLE_TIME_PROFILING_CU_SHAPES                   0 // DISABLED by default (can be enabled by project configuration or make command)
117 #endif
118 #define ENABLE_TIME_PROFILING_EXTENDED                    ( ENABLE_TIME_PROFILING_PIC_TYPES || ENABLE_TIME_PROFILING_CU_SHAPES )
119 
120 // ====================================================================================================================
121 // Debugging
122 // ====================================================================================================================
123 
124 #define PRINT_MACRO_VALUES                                1 ///< When enabled, the encoder prints out a list of the non-environment-variable controlled macros and their values on startup
125 
126 // ====================================================================================================================
127 // Tool Switches - transitory (these macros are likely to be removed in future revisions)
128 // ====================================================================================================================
129 
130 #define DECODER_CHECK_SUBSTREAM_AND_SLICE_TRAILING_BYTES  1 ///< TODO: integrate this macro into a broader conformance checking system.
131 #define U0033_ALTERNATIVE_TRANSFER_CHARACTERISTICS_SEI    1 ///< Alternative transfer characteristics SEI message (JCTVC-U0033, with syntax naming from V1005)
132 
133 // ====================================================================================================================
134 // Tool Switches
135 // ====================================================================================================================
136 
137 
138 // This can be enabled by the makefile
139 #ifndef RExt__HIGH_BIT_DEPTH_SUPPORT
140 #define RExt__HIGH_BIT_DEPTH_SUPPORT                      0 ///< 0 (default) use data type definitions for 8-10 bit video, 1 = use larger data types to allow for up to 16-bit video (originally developed as part of N0188)
141 #endif
142 
143 // SIMD optimizations
144 #define SIMD_ENABLE                                       1
145 #define ENABLE_SIMD_OPT                                 ( SIMD_ENABLE && !RExt__HIGH_BIT_DEPTH_SUPPORT )    ///< SIMD optimizations, no impact on RD performance
146 #define ENABLE_SIMD_OPT_MCIF                            ( 1 && ENABLE_SIMD_OPT )                            ///< SIMD optimization for the interpolation filter, no impact on RD performance
147 #define ENABLE_SIMD_OPT_BUFFER                          ( 1 && ENABLE_SIMD_OPT )                            ///< SIMD optimization for the buffer operations, no impact on RD performance
148 #define ENABLE_SIMD_OPT_DIST                            ( 1 && ENABLE_SIMD_OPT )                            ///< SIMD optimization for the distortion calculations(SAD,SSE,HADAMARD), no impact on RD performance
149 #define ENABLE_SIMD_OPT_ALF                             ( 1 && ENABLE_SIMD_OPT /*&& !ALF_FIX*/ )                            ///< SIMD optimization for ALF
150 #define ENABLE_SIMD_OPT_INTRAPRED                       ( 1 && ENABLE_SIMD_OPT )                            ///< SIMD optimization for Intra Prediction
151 #define ENABLE_SIMD_OPT_QUANT                           ( 1 && ENABLE_SIMD_OPT )                            ///< SIMD optimization for Quant/Dequant
152 #if ENABLE_SIMD_OPT_BUFFER
153 #define ENABLE_SIMD_OPT_GBI                               1                                                 ///< SIMD optimization for GBi
154 #endif
155 #define ENABLE_SIMD_OPT_BIO                             ( 1 && ENABLE_SIMD_OPT )                            ///< SIMD optimization for BIO
156 #define ENABLE_SIMD_OPT_PICTURE                         ( 1 && ENABLE_SIMD_OPT )                            ///< SIMD optimization for Picture Padding
157 #define ENABLE_SIMD_OPT_SAO                             ( 1 && ENABLE_SIMD_OPT )                            ///< SIMD optimization for BIO
158 #define ENABLE_SIMD_TCOEFF_OPS                          ( 1 && ENABLE_SIMD_OPT )
159 #define ENABLE_SIMD_LOG2                                ( 1 && ENABLE_SIMD_OPT )
160 #define ENABLE_SIMD_DBLF                                ( 1 && ENABLE_SIMD_OPT )
161 
162 // End of SIMD optimizations
163 
164 #define LUMA_ADAPTIVE_DEBLOCKING_FILTER_QP_OFFSET         1 /// JVET-L0414 (CE11.2.2) with explicit signalling of num interval, threshold and qpOffset
165 
166 // ====================================================================================================================
167 // Derived macros
168 // ====================================================================================================================
169 
170 #if RExt__HIGH_BIT_DEPTH_SUPPORT
171 #define FULL_NBIT                                         1 ///< When enabled, use distortion measure derived from all bits of source data, otherwise discard (bitDepth - 8) least-significant bits of distortion
172 #else
173 #define FULL_NBIT                                         1 ///< When enabled, use distortion measure derived from all bits of source data, otherwise discard (bitDepth - 8) least-significant bits of distortion
174 #endif
175 
176 #if FULL_NBIT
177 #define DISTORTION_PRECISION_ADJUSTMENT(x)                0
178 #else
179 #define DISTORTION_ESTIMATION_BITS                        8
180 #define DISTORTION_PRECISION_ADJUSTMENT(x)                ((x>DISTORTION_ESTIMATION_BITS)? ((x)-DISTORTION_ESTIMATION_BITS) : 0)
181 #endif
182 
183 // ====================================================================================================================
184 // Error checks
185 // ====================================================================================================================
186 
187 
188 // ====================================================================================================================
189 // Named numerical types
190 // ====================================================================================================================
191 
192 #if RExt__HIGH_BIT_DEPTH_SUPPORT
193 typedef       int             Pel;               ///< pixel type
194 typedef       int64_t         TCoeff;            ///< transform coefficient
195 typedef       int             TMatrixCoeff;      ///< transform matrix coefficient
196 typedef       int16_t         TFilterCoeff;      ///< filter coefficient
197 typedef       int64_t         Intermediate_Int;  ///< used as intermediate value in calculations
198 #else
199 typedef       int16_t         Pel;               ///< pixel type
200 typedef       int             TCoeff;            ///< transform coefficient
201 typedef       int16_t         TCoeffSig;
202 typedef       int16_t         TMatrixCoeff;      ///< transform matrix coefficient
203 typedef       int16_t         TFilterCoeff;      ///< filter coefficient
204 typedef       int             Intermediate_Int;  ///< used as intermediate value in calculations
205 #endif
206 
207 typedef       uint64_t        Distortion;        ///< distortion measurement
208 
209 typedef       uint16_t        SplitSeries;       ///< used to encoded the splits that caused a particular CU size
210 
211 // ====================================================================================================================
212 // Enumeration
213 // ====================================================================================================================
214 
215 enum ApsTypeValues
216 {
217   ALF_APS  = 0,
218   LMCS_APS = 1,
219   SCALING_LIST_APS = 2,
220 };
221 
222 // EMT transform tags
223 enum TransType : uint8_t
224 {
225   DCT2           = 0,
226   DCT8           = 1,
227   DST7           = 2,
228   NUM_TRANS_TYPE
229 };
230 
231 enum MTSIdx : uint8_t
232 {
233   MTS_DCT2_DCT2 = 0,
234   MTS_SKIP = 1,
235   MTS_DST7_DST7 = 2,
236   MTS_DCT8_DST7 = 3,
237   MTS_DST7_DCT8 = 4,
238   MTS_DCT8_DCT8 = 5
239 };
240 
241 enum ISPType : uint8_t
242 {
243   NOT_INTRA_SUBPARTITIONS = 0,
244   HOR_INTRA_SUBPARTITIONS = 1,
245   VER_INTRA_SUBPARTITIONS = 2,
246   NUM_INTRA_SUBPARTITIONS_MODES
247 };
248 
249 enum SbtIdx : uint8_t
250 {
251   SBT_OFF_DCT  = 0,
252   SBT_VER_HALF = 1,
253   SBT_HOR_HALF = 2,
254   SBT_VER_QUAD = 3,
255   SBT_HOR_QUAD = 4,
256   NUMBER_SBT_IDX
257 };
258 
259 enum SbtPos : uint8_t
260 {
261   SBT_POS0 = 0,
262   SBT_POS1 = 1,
263   NUMBER_SBT_POS
264 };
265 
266 /// supported slice type
267 enum SliceType
268 {
269   B_SLICE               = 0,
270   P_SLICE               = 1,
271   I_SLICE               = 2,
272   NUMBER_OF_SLICE_TYPES
273 };
274 
275 /// chroma formats (according to semantics of chroma_format_idc)
276 enum ChromaFormat : uint8_t
277 {
278   CHROMA_400        = 0,
279   CHROMA_420        = 1,
280   CHROMA_422        = 2,
281   CHROMA_444        = 3,
282   NUM_CHROMA_FORMAT
283 };
284 
285 enum ChannelType : uint8_t
286 {
287   CHANNEL_TYPE_LUMA    = 0,
288   CHANNEL_TYPE_CHROMA  = 1,
289   MAX_NUM_CHANNEL_TYPE
290 };
291 
292 enum TreeType : uint8_t
293 {
294   TREE_D = 0, //default tree status (for single-tree slice, TREE_D means joint tree; for dual-tree I slice, TREE_D means TREE_L for luma and TREE_C for chroma)
295   TREE_L = 1, //separate tree only contains luma (may split)
296   TREE_C = 2, //separate tree only contains chroma (not split), to avoid small chroma block
297 };
298 
299 enum ModeType : uint8_t
300 {
301   MODE_TYPE_ALL = 0, //all modes can try
302   MODE_TYPE_INTER = 1, //can try inter
303   MODE_TYPE_INTRA = 2, //can try intra, ibc, palette
304 };
305 
306 #define CH_L CHANNEL_TYPE_LUMA
307 #define CH_C CHANNEL_TYPE_CHROMA
308 
309 enum ComponentID : uint8_t
310 {
311   COMPONENT_Y         = 0,
312   COMPONENT_Cb        = 1,
313   COMPONENT_Cr        = 2,
314   MAX_NUM_COMPONENT,
315   JOINT_CbCr          = MAX_NUM_COMPONENT,
316   MAX_NUM_TBLOCKS     = MAX_NUM_COMPONENT
317 };
318 
319 #define MAP_CHROMA(c) (ComponentID(c))
320 
321 enum DeblockEdgeDir : uint8_t
322 {
323   EDGE_VER     = 0,
324   EDGE_HOR     = 1,
325   NUM_EDGE_DIR
326 };
327 
328 /// supported prediction type
329 enum PredMode : uint8_t
330 {
331   MODE_INTER                 = 0,     ///< inter-prediction mode
332   MODE_INTRA                 = 1,     ///< intra-prediction mode
333   MODE_IBC                   = 2,     ///< ibc-prediction mode
334   NUMBER_OF_PREDICTION_MODES
335 };
336 
337 /// reference list index
338 enum RefPicList : uint8_t
339 {
340   REF_PIC_LIST_0               = 0,   ///< reference list 0
341   REF_PIC_LIST_1               = 1,   ///< reference list 1
342   NUM_REF_PIC_LIST_01          = 2,
343   REF_PIC_LIST_X               = 100  ///< special mark
344 };
345 
346 #define L0 REF_PIC_LIST_0
347 #define L1 REF_PIC_LIST_1
348 
349 /// distortion function index
350 enum DFunc
351 {
352   DF_SAD             = 8,             ///< general size SAD
353   DF_SAD2            = DF_SAD+1,      ///<   2xM SAD
354   DF_SAD4            = DF_SAD+2,      ///<   4xM SAD
355   DF_SAD8            = DF_SAD+3,      ///<   8xM SAD
356   DF_SAD16           = DF_SAD+4,      ///<  16xM SAD
357   DF_SAD32           = DF_SAD+5,      ///<  32xM SAD
358   DF_SAD64           = DF_SAD+6,      ///<  64xM SAD
359   DF_SAD16N          = DF_SAD+7,      ///< 16NxM SAD
360 
361   DF_TOTAL_FUNCTIONS = DF_SAD16N + 1
362 };
363 
364 /// motion vector predictor direction used in AMVP
365 enum MvpDir : uint8_t
366 {
367   MD_LEFT = 0,          ///< MVP of left block
368   MD_ABOVE,             ///< MVP of above block
369   MD_ABOVE_RIGHT,       ///< MVP of above right block
370   MD_BELOW_LEFT,        ///< MVP of below left block
371   MD_ABOVE_LEFT         ///< MVP of above left block
372 };
373 
374 enum CoeffScanGroupType
375 {
376   SCAN_UNGROUPED   = 0,
377   SCAN_GROUPED_4x4 = 1,
378   SCAN_NUMBER_OF_GROUP_TYPES = 2
379 };
380 
381 enum ScalingListSize
382 {
383   SCALING_LIST_1x1 = 0,
384   SCALING_LIST_2x2,
385   SCALING_LIST_4x4,
386   SCALING_LIST_8x8,
387   SCALING_LIST_16x16,
388   SCALING_LIST_32x32,
389   SCALING_LIST_64x64,
390   SCALING_LIST_SIZE_NUM,
391   //for user define matrix
392   SCALING_LIST_FIRST_CODED = SCALING_LIST_2x2,
393   SCALING_LIST_LAST_CODED = SCALING_LIST_64x64
394 };
395 
396 enum ScalingList1dStartIdx
397 {
398   SCALING_LIST_1D_START_2x2    = 0,
399   SCALING_LIST_1D_START_4x4    = 2,
400   SCALING_LIST_1D_START_8x8    = 8,
401   SCALING_LIST_1D_START_16x16  = 14,
402   SCALING_LIST_1D_START_32x32  = 20,
403   SCALING_LIST_1D_START_64x64  = 26,
404 };
405 
406 enum SAOMode //mode
407 {
408   SAO_MODE_OFF = 0,
409   SAO_MODE_NEW,
410   SAO_MODE_MERGE,
411   NUM_SAO_MODES
412 };
413 
414 enum SAOModeMergeTypes
415 {
416   SAO_MERGE_LEFT =0,
417   SAO_MERGE_ABOVE,
418   NUM_SAO_MERGE_TYPES
419 };
420 
421 
422 enum SAOModeNewTypes
423 {
424   SAO_TYPE_START_EO =0,
425   SAO_TYPE_EO_0 = SAO_TYPE_START_EO,
426   SAO_TYPE_EO_90,
427   SAO_TYPE_EO_135,
428   SAO_TYPE_EO_45,
429 
430   SAO_TYPE_START_BO,
431   SAO_TYPE_BO = SAO_TYPE_START_BO,
432 
433   NUM_SAO_NEW_TYPES
434 };
435 #define NUM_SAO_EO_TYPES_LOG2 2
436 
437 enum SAOEOClasses
438 {
439   SAO_CLASS_EO_FULL_VALLEY = 0,
440   SAO_CLASS_EO_HALF_VALLEY = 1,
441   SAO_CLASS_EO_PLAIN       = 2,
442   SAO_CLASS_EO_HALF_PEAK   = 3,
443   SAO_CLASS_EO_FULL_PEAK   = 4,
444   NUM_SAO_EO_CLASSES,
445 };
446 
447 #define NUM_SAO_BO_CLASSES_LOG2  5
448 #define NUM_SAO_BO_CLASSES       (1<<NUM_SAO_BO_CLASSES_LOG2)
449 
450 namespace Profile
451 {
452   enum Name
453   {
454     NONE                                 = 0,
455     STILL_PICTURE                        = 64,
456     MAIN_10                              = 1,
457     MAIN_10_STILL_PICTURE                = MAIN_10 | STILL_PICTURE,
458     MULTILAYER_MAIN_10                   = 17,
459     MULTILAYER_MAIN_10_STILL_PICTURE     = MULTILAYER_MAIN_10 | STILL_PICTURE,
460     MAIN_10_444                          = 33,
461     MAIN_10_444_STILL_PICTURE            = MAIN_10_444 | STILL_PICTURE,
462     MULTILAYER_MAIN_10_444               = 49,
463     MULTILAYER_MAIN_10_444_STILL_PICTURE = MULTILAYER_MAIN_10_444 | STILL_PICTURE,
464   };
465 }
466 
467 enum Tier
468 {
469   MAIN = 0,
470   HIGH = 1,
471   NUMBER_OF_TIERS=2
472 };
473 
474 enum SPSExtensionFlagIndex
475 {
476   SPS_EXT__REXT           = 0,
477 //SPS_EXT__MVHEVC         = 1, //for use in future versions
478 //SPS_EXT__SHVC           = 2, //for use in future versions
479   SPS_EXT__NEXT           = 3,
480   NUM_SPS_EXTENSION_FLAGS = 8
481 };
482 
483 enum PPSExtensionFlagIndex
484 {
485   PPS_EXT__REXT           = 0,
486 //PPS_EXT__MVHEVC         = 1, //for use in future versions
487 //PPS_EXT__SHVC           = 2, //for use in future versions
488   NUM_PPS_EXTENSION_FLAGS = 8
489 };
490 
491 // TODO: Existing names used for the different NAL unit types can be altered to better reflect the names in the spec.
492 //       However, the names in the spec are not yet stable at this point. Once the names are stable, a cleanup
493 //       effort can be done without use of macros to alter the names used to indicate the different NAL unit types.
494 enum NalUnitType
495 {
496   NAL_UNIT_CODED_SLICE_TRAIL = 0,   // 0
497   NAL_UNIT_CODED_SLICE_STSA,        // 1
498   NAL_UNIT_CODED_SLICE_RADL,        // 2
499   NAL_UNIT_CODED_SLICE_RASL,        // 3
500 
501   NAL_UNIT_RESERVED_VCL_4,
502   NAL_UNIT_RESERVED_VCL_5,
503   NAL_UNIT_RESERVED_VCL_6,
504 
505   NAL_UNIT_CODED_SLICE_IDR_W_RADL,  // 7
506   NAL_UNIT_CODED_SLICE_IDR_N_LP,    // 8
507   NAL_UNIT_CODED_SLICE_CRA,         // 9
508   NAL_UNIT_CODED_SLICE_GDR,         // 10
509 
510   NAL_UNIT_RESERVED_IRAP_VCL_11,
511   NAL_UNIT_RESERVED_IRAP_VCL_12,
512   NAL_UNIT_DCI,                     // 13
513   NAL_UNIT_VPS,                     // 14
514   NAL_UNIT_SPS,                     // 15
515   NAL_UNIT_PPS,                     // 16
516   NAL_UNIT_PREFIX_APS,              // 17
517   NAL_UNIT_SUFFIX_APS,              // 18
518   NAL_UNIT_PH,                      // 19
519   NAL_UNIT_ACCESS_UNIT_DELIMITER,   // 20
520   NAL_UNIT_EOS,                     // 21
521   NAL_UNIT_EOB,                     // 22
522   NAL_UNIT_PREFIX_SEI,              // 23
523   NAL_UNIT_SUFFIX_SEI,              // 24
524   NAL_UNIT_FD,                      // 25
525 
526   NAL_UNIT_RESERVED_NVCL_26,
527   NAL_UNIT_RESERVED_NVCL_27,
528 
529   NAL_UNIT_UNSPECIFIED_28,
530   NAL_UNIT_UNSPECIFIED_29,
531   NAL_UNIT_UNSPECIFIED_30,
532   NAL_UNIT_UNSPECIFIED_31,
533   NAL_UNIT_INVALID
534 };
535 
536 enum MergeType : uint8_t
537 {
538   MRG_TYPE_DEFAULT_N        = 0, // 0
539   MRG_TYPE_SUBPU_ATMVP,
540   MRG_TYPE_IBC,
541   NUM_MRG_TYPE                   // 5
542 };
543 
544 enum TriangleSplit : uint8_t
545 {
546   TRIANGLE_DIR_135 = 0,
547   TRIANGLE_DIR_45,
548   TRIANGLE_DIR_NUM
549 };
550 
551 enum AffineModel : uint8_t
552 {
553   AFFINEMODEL_4PARAM,
554   AFFINEMODEL_6PARAM,
555   AFFINE_MODEL_NUM
556 };
557 
558 enum ImvMode : uint8_t
559 {
560   IMV_OFF = 0,
561   IMV_FPEL,
562   IMV_4PEL,
563   IMV_HPEL,
564   NUM_IMV_MODES
565 };
566 
567 
568 // ====================================================================================================================
569 // Type definition
570 // ====================================================================================================================
571 
572 /// parameters for adaptive loop filter
573 class PicSym;
574 
575 #define MAX_NUM_SAO_CLASSES  32  //(NUM_SAO_EO_GROUPS > NUM_SAO_BO_GROUPS)?NUM_SAO_EO_GROUPS:NUM_SAO_BO_GROUPS
576 
577 struct SAOOffset
578 {
579   SAOMode modeIdc;       // NEW, MERGE, OFF
580   int     typeIdc;       // union of SAOModeMergeTypes and SAOModeNewTypes, depending on modeIdc.
581   int     typeAuxInfo;   // BO: starting band index
582   int     offset[MAX_NUM_SAO_CLASSES];
583 
resetSAOOffset584   void reset()
585   {
586     modeIdc     = SAO_MODE_OFF;
587     typeIdc     = -1;
588     typeAuxInfo = -1;
589     ::memset( offset, 0, sizeof( int ) * MAX_NUM_SAO_CLASSES );
590   }
591 
592   const SAOOffset& operator=( const SAOOffset& src )
593   {
594     modeIdc     = src.modeIdc;
595     typeIdc     = src.typeIdc;
596     typeAuxInfo = src.typeAuxInfo;
597     ::memcpy( offset, src.offset, sizeof( int ) * MAX_NUM_SAO_CLASSES );
598 
599     return *this;
600   }
601 };
602 
603 struct SAOBlkParam
604 {
resetSAOBlkParam605   void reset()
606   {
607     for( int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++ )
608     {
609       offsetParam[compIdx].reset();
610     }
611   }
612 
613   const SAOBlkParam& operator=( const SAOBlkParam& src )
614   {
615     for( int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++ )
616     {
617       offsetParam[compIdx] = src.offsetParam[compIdx];
618     }
619     return *this;
620   }
621 
622   SAOOffset&       operator[]( int compIdx ) { return offsetParam[compIdx]; }
623   const SAOOffset& operator[]( int compIdx ) const { return offsetParam[compIdx]; }
624 
625 private:
626   SAOOffset offsetParam[MAX_NUM_COMPONENT];
627 };
628 
629 struct BitDepths
630 {
631   int recon[MAX_NUM_CHANNEL_TYPE] = {8, 8}; ///< the bit depth as indicated in the SPS
632 };
633 
634 /// parameters for deblocking filter
635 struct LFCUParam
636 {
637 //  bool internalEdge;                     ///< indicates internal edge
638   bool leftEdge;                         ///< indicates left edge
639   bool topEdge;                          ///< indicates top edge
640 };
641 
642 
643 struct SEITimeSet
644 {
SEITimeSetSEITimeSet645   SEITimeSet() : clockTimeStampFlag(false),
646                      numUnitFieldBasedFlag(false),
647                      countingType(0),
648                      fullTimeStampFlag(false),
649                      discontinuityFlag(false),
650                      cntDroppedFlag(false),
651                      numberOfFrames(0),
652                      secondsValue(0),
653                      minutesValue(0),
654                      hoursValue(0),
655                      secondsFlag(false),
656                      minutesFlag(false),
657                      hoursFlag(false),
658                      timeOffsetLength(0),
659                      timeOffsetValue(0)
660   { }
661   bool clockTimeStampFlag;
662   bool numUnitFieldBasedFlag;
663   int  countingType;
664   bool fullTimeStampFlag;
665   bool discontinuityFlag;
666   bool cntDroppedFlag;
667   int  numberOfFrames;
668   int  secondsValue;
669   int  minutesValue;
670   int  hoursValue;
671   bool secondsFlag;
672   bool minutesFlag;
673   bool hoursFlag;
674   int  timeOffsetLength;
675   int  timeOffsetValue;
676 };
677 
678 struct SEIMasteringDisplay
679 {
680   uint32_t maxLuminance;
681   uint32_t minLuminance;
682   uint16_t primaries[3][2];
683   uint16_t whitePoint[2];
684 };
685 
686 class ChromaCbfs
687 {
688 public:
ChromaCbfs()689   ChromaCbfs()
690     : Cb( false ), Cr( false )
691   {}
692 public:
sigChroma(ChromaFormat chromaFormat)693   bool sigChroma( ChromaFormat chromaFormat ) const
694   {
695     if( chromaFormat == CHROMA_400 )
696     {
697       return false;
698     }
699     return   ( Cb || Cr );
700   }
cbf(ComponentID compID)701   bool& cbf( ComponentID compID )
702   {
703     bool *cbfs[MAX_NUM_TBLOCKS] = { nullptr, &Cb, &Cr };
704 
705     return *cbfs[compID];
706   }
707 public:
708   bool Cb;
709   bool Cr;
710 };
711 
712 struct LoopFilterParam
713 {
714   int8_t   qp[3];
715   uint8_t  bs;
716   uint8_t  sideMaxFiltLength;
717   uint8_t  flags;
718 
filterEdgeLoopFilterParam719   bool filterEdge( ChannelType chType ) const { return ( flags >> chType ) & 1; }
720   // chroma max filter lenght
filterCMFLLoopFilterParam721   bool filterCMFL()                     const { return ( flags >>      5 ) & 1; }
722 
setFilterEdgeLoopFilterParam723   void setFilterEdge( ChannelType chType, int f ) { flags = ( flags & ~( 1 << chType ) ) | ( f << chType ); }
setFilterCMFLLoopFilterParam724   void setFilterCMFL(                     int f ) { flags = ( flags & ~( 1 <<      5 ) ) | ( f <<      5 ); }
725 };
726 
727 enum MsgLevel
728 {
729   SILENT  = 0,
730   ERROR   = 1,
731   WARNING = 2,
732   INFO    = 3,
733   NOTICE  = 4,
734   VERBOSE = 5,
735   DETAILS = 6
736 };
737 
738 // ---------------------------------------------------------------------------
739 // exception class
740 // ---------------------------------------------------------------------------
741 
742 #ifdef assert
743 #undef assert
744 #endif
745 
746 #define assert dont_use_assert_use_CHECK_instead
747 
748 class Exception : public std::exception
749 {
750 public:
Exception(const std::string & _s)751   Exception( const std::string& _s ) : m_str( _s ) { }
Exception(const Exception & _e)752   Exception( const Exception& _e ) : std::exception( _e ), m_str( _e.m_str ) { }
~Exception()753   virtual ~Exception() noexcept { };
what()754   virtual const char* what() const noexcept { return m_str.c_str(); }
755   Exception& operator=( const Exception& _e ) { std::exception::operator=( _e ); m_str = _e.m_str; return *this; }
756   template<typename T> Exception& operator<<( T t ) { std::ostringstream oss; oss << t; m_str += oss.str(); return *this; }
757 private:
758   std::string m_str;
759 };
760 
761 // if a check fails with THROW or CHECK, please check if ported correctly from assert in revision 1196)
762 #define THROW(x)            throw( Exception( "\nERROR: In function \"" ) << __FUNCTION__ << "\" in " << __FILE__ << ":" << __LINE__ << ": " << x )
763 //#define THROW(x)            { std::cerr << "\nERROR: In function \"" << __FUNCTION__ << "\" in " << __FILE__ << ":" << __LINE__ << ": " << x << std::endl; abort(); }
764 #define CHECK(c,x)          if(c){ THROW( x << "\nERROR CONDITION: " << #c ); }
765 #define CHECK_WARN(c,x)     if(c){ std::cerr << "\nWARNING: In function \"" << __FUNCTION__ << "\" in " << __FILE__ << ":" << __LINE__ << ": " << x << "\nERROR CONDITION: " << #c << std::endl; }
766 #define EXIT(x)             throw( Exception( "\n" ) << x << "\n" )
767 #define CHECK_NULLPTR(_ptr) CHECK( !( _ptr ), "Accessing an empty pointer!" )
768 
769 #if !NDEBUG  // for non MSVC compiler, define _DEBUG if in debug mode to have same behavior between MSVC and others in debug
770 #ifndef _DEBUG
771 #define _DEBUG 1
772 #endif
773 #endif
774 
775 #if defined( _DEBUG )
776 #define CHECKD(c,x)         if(c){ THROW(x); }
777 #else
778 #define CHECKD(c,x)
779 #endif   // _DEBUG
780 
781 #define CHECKD_NULLPTR( _ptr ) CHECKD( !( _ptr ), "Accessing an empty pointer!" )
782 
783 // ---------------------------------------------------------------------------
784 // static vector
785 // ---------------------------------------------------------------------------
786 
787 template<typename T, size_t N>
788 class static_vector
789 {
790   T _arr[ N ];
791   size_t _size;
792 
793 public:
794 
795   typedef T         value_type;
796   typedef size_t    size_type;
797   typedef ptrdiff_t difference_type;
798   typedef T&        reference;
799   typedef T const&  const_reference;
800   typedef T*        pointer;
801   typedef T const*  const_pointer;
802   typedef T*        iterator;
803   typedef T const*  const_iterator;
804 
805   static const size_type max_num_elements = N;
806 
static_vector()807   static_vector() : _size( 0 )                                 { }
static_vector(size_t N_)808   static_vector( size_t N_ ) : _size( N_ )                     { CHECKD( _size > N, "capacity exceeded" ); }
static_vector(size_t N_,const T & _val)809   static_vector( size_t N_, const T& _val ) : _size( 0 )       { resize( N_, _val ); }
810   template<typename It>
static_vector(It _it1,It _it2)811   static_vector( It _it1, It _it2 ) : _size( 0 )               { while( _it1 < _it2 ) _arr[ _size++ ] = *_it1++; }
static_vector(std::initializer_list<T> _il)812   static_vector( std::initializer_list<T> _il ) : _size( 0 )
813   {
814     typename std::initializer_list<T>::iterator _src1 = _il.begin();
815     typename std::initializer_list<T>::iterator _src2 = _il.end();
816 
817     while( _src1 < _src2 ) _arr[ _size++ ] = *_src1++;
818 
819     CHECKD( _size > N, "capacity exceeded" );
820   }
821   static_vector& operator=( std::initializer_list<T> _il )
822   {
823     _size = 0;
824 
825     typename std::initializer_list<T>::iterator _src1 = _il.begin();
826     typename std::initializer_list<T>::iterator _src2 = _il.end();
827 
828     while( _src1 < _src2 ) _arr[ _size++ ] = *_src1++;
829 
830     CHECKD( _size > N, "capacity exceeded" );
831     return *this;
832   }
833 
resize_noinit(size_t N_)834   void resize_noinit( size_t N_ )               { CHECKD( N_ > N, "capacity exceeded" ); _size = N_; }
resize(size_t N_)835   void resize( size_t N_ )                      { CHECKD( N_ > N, "capacity exceeded" ); while(_size < N_) _arr[ _size++ ] = T() ; _size = N_; }
resize(size_t N_,const T & _val)836   void resize( size_t N_, const T& _val )       { CHECKD( N_ > N, "capacity exceeded" ); while(_size < N_) _arr[ _size++ ] = _val; _size = N_; }
reserve(size_t N_)837   void reserve( size_t N_ )                     { CHECKD( N_ > N, "capacity exceeded" ); }
push_back(const T & _val)838   void push_back( const T& _val )               { CHECKD( _size >= N, "capacity exceeded" ); _arr[ _size++ ] = _val; }
push_back(T && val)839   void push_back( T&& val )                     { CHECKD( _size >= N, "capacity exceeded" ); _arr[ _size++ ] = std::forward<T>( val ); }
pop_back()840   void pop_back()                               { CHECKD( _size == 0, "calling pop_back on an empty vector" ); _size--; }
pop_front()841   void pop_front()                              { CHECKD( _size == 0, "calling pop_front on an empty vector" ); _size--; for( int i = 0; i < _size; i++ ) _arr[i] = _arr[i + 1]; }
clear()842   void clear()                                  { _size = 0; }
at(size_t _i)843   reference       at( size_t _i )               { CHECKD( _i >= _size, "Trying to access an out-of-bound-element" ); return _arr[ _i ]; }
at(size_t _i)844   const_reference at( size_t _i ) const         { CHECKD( _i >= _size, "Trying to access an out-of-bound-element" ); return _arr[ _i ]; }
845   reference       operator[]( size_t _i )       { CHECKD( _i >= _size, "Trying to access an out-of-bound-element" ); return _arr[ _i ]; }
846   const_reference operator[]( size_t _i ) const { CHECKD( _i >= _size, "Trying to access an out-of-bound-element" ); return _arr[ _i ]; }
front()847   reference       front()                       { CHECKD( _size == 0, "Trying to access the first element of an empty vector" ); return _arr[ 0 ]; }
front()848   const_reference front() const                 { CHECKD( _size == 0, "Trying to access the first element of an empty vector" ); return _arr[ 0 ]; }
back()849   reference       back()                        { CHECKD( _size == 0, "Trying to access the last element of an empty vector" );  return _arr[ _size - 1 ]; }
back()850   const_reference back() const                  { CHECKD( _size == 0, "Trying to access the last element of an empty vector" );  return _arr[ _size - 1 ]; }
data()851   pointer         data()                        { return _arr; }
data()852   const_pointer   data() const                  { return _arr; }
begin()853   iterator        begin()                       { return _arr; }
begin()854   const_iterator  begin() const                 { return _arr; }
cbegin()855   const_iterator  cbegin() const                { return _arr; }
end()856   iterator        end()                         { return _arr + _size; }
end()857   const_iterator  end() const                   { return _arr + _size; };
cend()858   const_iterator  cend() const                  { return _arr + _size; };
size()859   size_type       size() const                  { return _size; };
byte_size()860   size_type       byte_size() const             { return _size * sizeof( T ); }
empty()861   bool            empty() const                 { return _size == 0; }
862 
capacity()863   size_type       capacity() const              { return N; }
max_size()864   size_type       max_size() const              { return N; }
byte_capacity()865   size_type       byte_capacity() const         { return sizeof(_arr); }
866 
erase(const_iterator _pos)867   void            erase( const_iterator _pos )  { iterator it   = const_cast<iterator>( _pos ) - 1;
868                                                   iterator last = end() - 1;
869                                                   while( ++it != last ) *it = *( it + 1 );
870                                                   _size--; }
871 };
872 
873 #define SIGN(x) ( (x) >= 0 ? 1 : -1 )
874 
875 #define MAX_NUM_ALF_CLASSES             25
876 #define MAX_NUM_ALF_LUMA_COEFF          13
877 #define MAX_NUM_ALF_CHROMA_COEFF        7
878 #define MAX_ALF_FILTER_LENGTH           7
879 #define MAX_NUM_ALF_COEFF               (MAX_ALF_FILTER_LENGTH * MAX_ALF_FILTER_LENGTH / 2 + 1)
880 #define MAX_NUM_ALF_TRANSPOSE_ID        4
881 #define MAX_ALF_PADDING_SIZE            4
882 #define MAX_NUM_ALF_ALTERNATIVES_CHROMA 8
883 #define MAX_NUM_CC_ALF_FILTERS          4
884 #define MAX_NUM_CC_ALF_CHROMA_COEFF     8
885 #define CCALF_DYNAMIC_RANGE             6
886 #define CCALF_BITS_PER_COEFF_LEVEL      3
887 
888 enum AlfFilterType
889 {
890   ALF_FILTER_5 = 0,
891   ALF_FILTER_7,
892   CC_ALF,
893   ALF_NUM_OF_FILTER_TYPES
894 };
895 
896 struct AlfSliceParam
897 {
898   bool             enabledFlag        [MAX_NUM_COMPONENT];                          // alf_slice_enable_flag, alf_chroma_idc
899   bool             nonLinearFlagLuma;
900   bool             nonLinearFlagChroma;
901   short            lumaCoeff          [MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; // alf_coeff_luma_delta[i][j]
902   short            lumaClipp          [MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF]; // alf_clipp_luma_[i][j]
903   int              numAlternativesChroma;                                                  // alf_chroma_num_alts_minus_one + 1
904   short            chromaCoeff        [MAX_NUM_ALF_ALTERNATIVES_CHROMA * MAX_NUM_ALF_CHROMA_COEFF]; // alf_coeff_chroma[i]
905   short            chromaClipp        [MAX_NUM_ALF_ALTERNATIVES_CHROMA * MAX_NUM_ALF_CHROMA_COEFF]; // alf_clipp_chroma[i]
906   short            filterCoeffDeltaIdx[MAX_NUM_ALF_CLASSES];                        // filter_coeff_delta[i]
907   bool             filterCoeffFlag    [MAX_NUM_ALF_CLASSES];                        // filter_coefficient_flag[i]
908   int              numLumaFilters;                                                  // number_of_filters_minus1 + 1
909   bool             coeffDeltaFlag;                                                  // alf_coefficients_delta_flag
910 
911   bool             lumaCoeffSummed = false;
912   bool             lumaFinalDone   = false;
913   bool             chrmFinalDone   = false;
914   short            lumaCoeffFinal     [MAX_NUM_ALF_TRANSPOSE_ID * MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF];
915   short            lumaClippFinal     [MAX_NUM_ALF_TRANSPOSE_ID * MAX_NUM_ALF_CLASSES * MAX_NUM_ALF_LUMA_COEFF];
916   short            chrmClippFinal     [MAX_NUM_ALF_ALTERNATIVES_CHROMA * MAX_NUM_ALF_CHROMA_COEFF];
917   int              tLayer;
918   bool             newFilterFlag      [MAX_NUM_CHANNEL_TYPE];
919 
AlfSliceParamAlfSliceParam920   AlfSliceParam()
921   {
922     memset( this, 0, sizeof( *this ) );
923   }
924 
resetAlfSliceParam925   void reset()
926   {
927     std::memset( this, 0, sizeof( AlfSliceParam ) );
928     numLumaFilters = 1;
929     numAlternativesChroma = 1;
930   }
931 
932   const AlfSliceParam& operator=( const AlfSliceParam& src )
933   {
934     std::memcpy( this, &src, sizeof( AlfSliceParam ) );
935     return *this;
936   }
937 };
938 
939 struct CcAlfFilterParam
940 {
941   bool    ccAlfFilterEnabled[2];
942   bool    ccAlfFilterIdxEnabled[2][MAX_NUM_CC_ALF_FILTERS];
943   uint8_t ccAlfFilterCount[2];
944   short   ccAlfCoeff[2][MAX_NUM_CC_ALF_FILTERS][MAX_NUM_CC_ALF_CHROMA_COEFF];
945   int     newCcAlfFilter[2];
946   int     numberValidComponents;
947 
CcAlfFilterParamCcAlfFilterParam948   CcAlfFilterParam()
949   {
950     memset( this, 0, sizeof( *this ) );
951   }
952 
resetCcAlfFilterParam953   void reset()
954   {
955     std::memset( this, 0, sizeof( CcAlfFilterParam ) );
956     ccAlfFilterCount[0] = ccAlfFilterCount[1] = MAX_NUM_CC_ALF_FILTERS;
957     numberValidComponents = 3;
958     newCcAlfFilter[0] = newCcAlfFilter[1] = 0;
959   }
960 
961   const CcAlfFilterParam& operator = ( const CcAlfFilterParam& src )
962   {
963     std::memcpy( this, &src, sizeof( CcAlfFilterParam ) );
964     return *this;
965   }
966 };
967 
968 // ---------------------------------------------------------------------------
969 // general helpers
970 // ---------------------------------------------------------------------------
971 // move an element within a list to the list's end without moving or copying the contained object
972 template<typename TList>
move_to_end(typename TList::const_iterator it,TList & list)973 static void move_to_end( typename TList::const_iterator it, TList& list )
974 {
975 #ifdef _DEBUG
976   const auto* oldAddr = &( *it );
977 #endif
978 
979   list.splice( list.cend(), list, it );
980 
981   CHECKD( &list.back() != oldAddr, "moving failed" );
982 }
983 
984 }
985