1 /*****************************************************************************
2  * Copyright (C) 2013-2020 MulticoreWare, Inc
3  *
4  * Authors: Steve Borho <steve@borho.org>
5  *          Min Chen <chenm003@163.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
20  *
21  * This program is also available under a commercial proprietary license.
22  * For more information, contact us at license @ x265.com.
23  *****************************************************************************/
24 
25 #ifndef X265_SLICE_H
26 #define X265_SLICE_H
27 
28 #include "common.h"
29 
30 namespace X265_NS {
31 // private namespace
32 
33 class Frame;
34 class PicList;
35 class PicYuv;
36 class MotionReference;
37 
38 enum SliceType
39 {
40     B_SLICE,
41     P_SLICE,
42     I_SLICE
43 };
44 
45 struct RPS
46 {
47     int  numberOfPictures;
48     int  numberOfNegativePictures;
49     int  numberOfPositivePictures;
50 
51     int  poc[MAX_NUM_REF_PICS];
52     int  deltaPOC[MAX_NUM_REF_PICS];
53     bool bUsed[MAX_NUM_REF_PICS];
54 
RPSRPS55     RPS()
56         : numberOfPictures(0)
57         , numberOfNegativePictures(0)
58         , numberOfPositivePictures(0)
59     {
60         memset(deltaPOC, 0, sizeof(deltaPOC));
61         memset(poc, 0, sizeof(poc));
62         memset(bUsed, 0, sizeof(bUsed));
63     }
64 
65     void sortDeltaPOC();
66 };
67 
68 namespace Profile {
69     enum Name
70     {
71         NONE = 0,
72         MAIN = 1,
73         MAIN10 = 2,
74         MAINSTILLPICTURE = 3,
75         MAINREXT = 4,
76         HIGHTHROUGHPUTREXT = 5
77     };
78 }
79 
80 namespace Level {
81     enum Tier
82     {
83         MAIN = 0,
84         HIGH = 1,
85     };
86 
87     enum Name
88     {
89         NONE = 0,
90         LEVEL1 = 30,
91         LEVEL2 = 60,
92         LEVEL2_1 = 63,
93         LEVEL3 = 90,
94         LEVEL3_1 = 93,
95         LEVEL4 = 120,
96         LEVEL4_1 = 123,
97         LEVEL5 = 150,
98         LEVEL5_1 = 153,
99         LEVEL5_2 = 156,
100         LEVEL6 = 180,
101         LEVEL6_1 = 183,
102         LEVEL6_2 = 186,
103         LEVEL8_5 = 255,
104     };
105 }
106 
107 struct ProfileTierLevel
108 {
109     int      profileIdc;
110     int      levelIdc;
111     uint32_t minCrForLevel;
112     uint32_t maxLumaSrForLevel;
113     uint32_t bitDepthConstraint;
114     int      chromaFormatConstraint;
115     bool     tierFlag;
116     bool     progressiveSourceFlag;
117     bool     interlacedSourceFlag;
118     bool     nonPackedConstraintFlag;
119     bool     frameOnlyConstraintFlag;
120     bool     profileCompatibilityFlag[32];
121     bool     intraConstraintFlag;
122     bool     onePictureOnlyConstraintFlag;
123     bool     lowerBitRateConstraintFlag;
124 };
125 
126 struct HRDInfo
127 {
128     uint32_t bitRateScale;
129     uint32_t cpbSizeScale;
130     uint32_t initialCpbRemovalDelayLength;
131     uint32_t cpbRemovalDelayLength;
132     uint32_t dpbOutputDelayLength;
133     uint32_t bitRateValue;
134     uint32_t cpbSizeValue;
135     bool     cbrFlag;
136 
HRDInfoHRDInfo137     HRDInfo()
138         : bitRateScale(0)
139         , cpbSizeScale(0)
140         , initialCpbRemovalDelayLength(1)
141         , cpbRemovalDelayLength(1)
142         , dpbOutputDelayLength(1)
143         , cbrFlag(false)
144     {
145     }
146 };
147 
148 struct TimingInfo
149 {
150     uint32_t numUnitsInTick;
151     uint32_t timeScale;
152 };
153 
154 struct VPS
155 {
156     HRDInfo          hrdParameters;
157     ProfileTierLevel ptl;
158     uint32_t         maxTempSubLayers;
159     uint32_t         numReorderPics;
160     uint32_t         maxDecPicBuffering;
161     uint32_t         maxLatencyIncrease;
162 };
163 
164 struct Window
165 {
166     int  leftOffset;
167     int  rightOffset;
168     int  topOffset;
169     int  bottomOffset;
170     bool bEnabled;
171 
WindowWindow172     Window()
173     {
174         bEnabled = false;
175     }
176 };
177 
178 struct VUI
179 {
180     int        aspectRatioIdc;
181     int        sarWidth;
182     int        sarHeight;
183     int        videoFormat;
184     int        colourPrimaries;
185     int        transferCharacteristics;
186     int        matrixCoefficients;
187     int        chromaSampleLocTypeTopField;
188     int        chromaSampleLocTypeBottomField;
189 
190     bool       aspectRatioInfoPresentFlag;
191     bool       overscanInfoPresentFlag;
192     bool       overscanAppropriateFlag;
193     bool       videoSignalTypePresentFlag;
194     bool       videoFullRangeFlag;
195     bool       colourDescriptionPresentFlag;
196     bool       chromaLocInfoPresentFlag;
197     bool       frameFieldInfoPresentFlag;
198     bool       fieldSeqFlag;
199     bool       hrdParametersPresentFlag;
200 
201     HRDInfo    hrdParameters;
202     Window     defaultDisplayWindow;
203     TimingInfo timingInfo;
204 };
205 
206 struct SPS
207 {
208     /* cached PicYuv offset arrays, shared by all instances of
209      * PicYuv created by this encoder */
210     intptr_t* cuOffsetY;
211     intptr_t* cuOffsetC;
212     intptr_t* buOffsetY;
213     intptr_t* buOffsetC;
214 
215     int      chromaFormatIdc;        // use param
216     uint32_t picWidthInLumaSamples;  // use param
217     uint32_t picHeightInLumaSamples; // use param
218 
219     uint32_t numCuInWidth;
220     uint32_t numCuInHeight;
221     uint32_t numCUsInFrame;
222     uint32_t numPartitions;
223     uint32_t numPartInCUSize;
224 
225     int      log2MinCodingBlockSize;
226     int      log2DiffMaxMinCodingBlockSize;
227     int      log2MaxPocLsb;
228 
229     uint32_t quadtreeTULog2MaxSize;
230     uint32_t quadtreeTULog2MinSize;
231 
232     uint32_t quadtreeTUMaxDepthInter; // use param
233     uint32_t quadtreeTUMaxDepthIntra; // use param
234 
235     uint32_t maxAMPDepth;
236 
237     uint32_t maxTempSubLayers;   // max number of Temporal Sub layers
238     uint32_t maxDecPicBuffering; // these are dups of VPS values
239     uint32_t maxLatencyIncrease;
240     int      numReorderPics;
241 
242     RPS      spsrps[MAX_NUM_SHORT_TERM_RPS];
243     int      spsrpsNum;
244     int      numGOPBegin;
245 
246     bool     bUseSAO; // use param
247     bool     bUseAMP; // use param
248     bool     bUseStrongIntraSmoothing; // use param
249     bool     bTemporalMVPEnabled;
250     bool     bEmitVUITimingInfo;
251     bool     bEmitVUIHRDInfo;
252 
253     Window   conformanceWindow;
254     VUI      vuiParameters;
255 
SPSSPS256     SPS()
257     {
258         memset(this, 0, sizeof(*this));
259     }
260 
~SPSSPS261     ~SPS()
262     {
263         X265_FREE(cuOffsetY);
264         X265_FREE(cuOffsetC);
265         X265_FREE(buOffsetY);
266         X265_FREE(buOffsetC);
267     }
268 };
269 
270 struct PPS
271 {
272     uint32_t maxCuDQPDepth;
273 
274     int      chromaQpOffset[2];      // use param
275     int      deblockingFilterBetaOffsetDiv2;
276     int      deblockingFilterTcOffsetDiv2;
277 
278     bool     bUseWeightPred;         // use param
279     bool     bUseWeightedBiPred;     // use param
280     bool     bUseDQP;
281     bool     bConstrainedIntraPred;  // use param
282 
283     bool     bTransquantBypassEnabled;  // Indicates presence of cu_transquant_bypass_flag in CUs.
284     bool     bTransformSkipEnabled;     // use param
285     bool     bEntropyCodingSyncEnabled; // use param
286     bool     bSignHideEnabled;          // use param
287 
288     bool     bDeblockingFilterControlPresent;
289     bool     bPicDisableDeblockingFilter;
290 
291     int      numRefIdxDefault[2];
292     bool     pps_slice_chroma_qp_offsets_present_flag;
293 };
294 
295 struct WeightParam
296 {
297     // Explicit weighted prediction parameters parsed in slice header,
298     uint32_t log2WeightDenom;
299     int      inputWeight;
300     int      inputOffset;
301     int      wtPresent;
302 
303     /* makes a non-h265 weight (i.e. fix7), into an h265 weight */
setFromWeightAndOffsetWeightParam304     void setFromWeightAndOffset(int w, int o, int denom, bool bNormalize)
305     {
306         inputOffset = o;
307         log2WeightDenom = denom;
308         inputWeight = w;
309         while (bNormalize && log2WeightDenom > 0 && (inputWeight > 127))
310         {
311             log2WeightDenom--;
312             inputWeight >>= 1;
313         }
314 
315         inputWeight = X265_MIN(inputWeight, 127);
316     }
317 };
318 
319 #define SET_WEIGHT(w, b, s, d, o) \
320     { \
321         (w).inputWeight = (s); \
322         (w).log2WeightDenom = (d); \
323         (w).inputOffset = (o); \
324         (w).wtPresent = (b); \
325     }
326 
327 class Slice
328 {
329 public:
330 
331     const SPS*  m_sps;
332     const PPS*  m_pps;
333     Frame*      m_refFrameList[2][MAX_NUM_REF + 1];
334     PicYuv*     m_refReconPicList[2][MAX_NUM_REF + 1];
335 
336     WeightParam m_weightPredTable[2][MAX_NUM_REF][3]; // [list][refIdx][0:Y, 1:U, 2:V]
337     MotionReference (*m_mref)[MAX_NUM_REF + 1];
338     RPS         m_rps;
339 
340     NalUnitType m_nalUnitType;
341     SliceType   m_sliceType;
342     int         m_sliceQp;
343     int         m_chromaQpOffset[2];
344     int         m_poc;
345     int         m_lastIDR;
346     int         m_rpsIdx;
347 
348     uint32_t    m_colRefIdx;       // never modified
349 
350     int         m_numRefIdx[2];
351     int         m_refPOCList[2][MAX_NUM_REF + 1];
352 
353     uint32_t    m_maxNumMergeCand; // use param
354     uint32_t    m_endCUAddr;
355 
356     bool        m_bCheckLDC;       // TODO: is this necessary?
357     bool        m_sLFaseFlag;      // loop filter boundary flag
358     bool        m_colFromL0Flag;   // collocated picture from List0 or List1 flag
359     int         m_bUseSao;
360 
361     int         m_iPPSQpMinus26;
362     int         numRefIdxDefault[2];
363     int         m_iNumRPSInSPS;
364     const x265_param *m_param;
365     int         m_fieldNum;
366 
Slice()367     Slice()
368     {
369         m_lastIDR = 0;
370         m_sLFaseFlag = true;
371         m_numRefIdx[0] = m_numRefIdx[1] = 0;
372         memset(m_refFrameList, 0, sizeof(m_refFrameList));
373         memset(m_refReconPicList, 0, sizeof(m_refReconPicList));
374         memset(m_refPOCList, 0, sizeof(m_refPOCList));
375         disableWeights();
376         m_iPPSQpMinus26 = 0;
377         numRefIdxDefault[0] = 1;
378         numRefIdxDefault[1] = 1;
379         m_rpsIdx = -1;
380         m_chromaQpOffset[0] = m_chromaQpOffset[1] = 0;
381         m_fieldNum = 0;
382     }
383 
384     void disableWeights();
385 
386     void setRefPicList(PicList& picList);
387 
getRapPicFlag()388     bool getRapPicFlag() const
389     {
390         return m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
391             || m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
392             || m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA;
393     }
getIdrPicFlag()394     bool getIdrPicFlag() const
395     {
396         return m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
397             || m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP;
398     }
isIRAP()399     bool isIRAP() const   { return m_nalUnitType >= 16 && m_nalUnitType <= 23; }
400 
isIntra()401     bool isIntra()  const { return m_sliceType == I_SLICE; }
402 
isInterB()403     bool isInterB() const { return m_sliceType == B_SLICE; }
404 
isInterP()405     bool isInterP() const { return m_sliceType == P_SLICE; }
406 
407     uint32_t realEndAddress(uint32_t endCUAddr) const;
408 };
409 
410 }
411 
412 #endif // ifndef X265_SLICE_H
413