Coverage Report

Created: 2025-07-23 08:18

/src/x265/source/common/slice.h
Line
Count
Source (jump to first uncovered line)
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
55
    RPS()
56
0
        : numberOfPictures(0)
57
0
        , numberOfNegativePictures(0)
58
0
        , numberOfPositivePictures(0)
59
0
    {
60
0
        memset(deltaPOC, 0, sizeof(deltaPOC));
61
0
        memset(poc, 0, sizeof(poc));
62
0
        memset(bUsed, 0, sizeof(bUsed));
63
0
    }
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
        MULTIVIEWMAIN = 6,
78
        SCALABLEMAIN = 7,
79
        SCALABLEMAIN10 = 8,
80
        MAINSCC = 9
81
    };
82
}
83
84
namespace Level {
85
    enum Tier
86
    {
87
        MAIN = 0,
88
        HIGH = 1,
89
    };
90
91
    enum Name
92
    {
93
        NONE = 0,
94
        LEVEL1 = 30,
95
        LEVEL2 = 60,
96
        LEVEL2_1 = 63,
97
        LEVEL3 = 90,
98
        LEVEL3_1 = 93,
99
        LEVEL4 = 120,
100
        LEVEL4_1 = 123,
101
        LEVEL5 = 150,
102
        LEVEL5_1 = 153,
103
        LEVEL5_2 = 156,
104
        LEVEL6 = 180,
105
        LEVEL6_1 = 183,
106
        LEVEL6_2 = 186,
107
        LEVEL6_3 = 189,
108
        LEVEL7 = 210,
109
        LEVEL7_1 = 213,
110
        LEVEL7_2 = 216,
111
        LEVEL8_5 = 255,
112
    };
113
}
114
115
struct ProfileTierLevel
116
{
117
    int      profileIdc[MAX_LAYERS];
118
    int      levelIdc;
119
    uint32_t minCrForLevel;
120
    uint64_t maxLumaSrForLevel;
121
    uint32_t bitDepthConstraint;
122
    int      chromaFormatConstraint;
123
    bool     tierFlag;
124
    bool     progressiveSourceFlag;
125
    bool     interlacedSourceFlag;
126
    bool     nonPackedConstraintFlag;
127
    bool     frameOnlyConstraintFlag;
128
    bool     profileCompatibilityFlag[32];
129
    bool     intraConstraintFlag;
130
    bool     onePictureOnlyConstraintFlag;
131
    bool     lowerBitRateConstraintFlag;
132
};
133
134
struct HRDInfo
135
{
136
    uint32_t bitRateScale;
137
    uint32_t cpbSizeScale;
138
    uint32_t initialCpbRemovalDelayLength;
139
    uint32_t cpbRemovalDelayLength;
140
    uint32_t dpbOutputDelayLength;
141
    uint32_t bitRateValue;
142
    uint32_t cpbSizeValue;
143
    bool     cbrFlag;
144
145
    HRDInfo()
146
0
        : bitRateScale(0)
147
0
        , cpbSizeScale(0)
148
0
        , initialCpbRemovalDelayLength(1)
149
0
        , cpbRemovalDelayLength(1)
150
0
        , dpbOutputDelayLength(1)
151
0
        , cbrFlag(false)
152
0
    {
153
0
    }
154
};
155
156
struct TimingInfo
157
{
158
    uint32_t numUnitsInTick;
159
    uint32_t timeScale;
160
};
161
162
struct VPS
163
{
164
    HRDInfo          hrdParameters;
165
    ProfileTierLevel ptl;
166
    uint32_t         maxTempSubLayers;
167
    uint32_t         numReorderPics[MAX_T_LAYERS];
168
    uint32_t         maxDecPicBuffering[MAX_T_LAYERS];
169
    uint32_t         maxLatencyIncrease[MAX_T_LAYERS];
170
    int              m_numLayers;
171
    int              m_numViews;
172
    bool             vps_extension_flag;
173
174
#if (ENABLE_ALPHA || ENABLE_MULTIVIEW)
175
    bool             splitting_flag;
176
    int              m_scalabilityMask[MAX_VPS_NUM_SCALABILITY_TYPES];
177
    int              scalabilityTypes;
178
    uint8_t          m_dimensionIdLen[MAX_VPS_NUM_SCALABILITY_TYPES];
179
    uint8_t          m_dimensionId[MAX_VPS_LAYER_ID_PLUS1][MAX_VPS_NUM_SCALABILITY_TYPES];
180
    bool             m_nuhLayerIdPresentFlag;
181
    uint8_t          m_layerIdInNuh[MAX_VPS_LAYER_ID_PLUS1];
182
    uint8_t          m_layerIdInVps[MAX_VPS_LAYER_ID_PLUS1];
183
    int              m_viewIdLen;
184
    int              m_vpsNumLayerSetsMinus1;
185
    int              m_numLayersInIdList[1023];
186
#endif
187
188
#if ENABLE_MULTIVIEW
189
    int              m_layerIdIncludedFlag;
190
#endif
191
};
192
193
struct Window
194
{
195
    int  leftOffset;
196
    int  rightOffset;
197
    int  topOffset;
198
    int  bottomOffset;
199
    bool bEnabled;
200
201
    Window()
202
0
    {
203
0
        bEnabled = false;
204
0
    }
205
};
206
207
struct VUI
208
{
209
    int        aspectRatioIdc;
210
    int        sarWidth;
211
    int        sarHeight;
212
    int        videoFormat;
213
    int        colourPrimaries;
214
    int        transferCharacteristics;
215
    int        matrixCoefficients;
216
    int        chromaSampleLocTypeTopField;
217
    int        chromaSampleLocTypeBottomField;
218
219
    bool       aspectRatioInfoPresentFlag;
220
    bool       overscanInfoPresentFlag;
221
    bool       overscanAppropriateFlag;
222
    bool       videoSignalTypePresentFlag;
223
    bool       videoFullRangeFlag;
224
    bool       colourDescriptionPresentFlag;
225
    bool       chromaLocInfoPresentFlag;
226
    bool       frameFieldInfoPresentFlag;
227
    bool       fieldSeqFlag;
228
    bool       hrdParametersPresentFlag;
229
230
    HRDInfo    hrdParameters;
231
    Window     defaultDisplayWindow;
232
    TimingInfo timingInfo;
233
};
234
235
struct SPS
236
{
237
    /* cached PicYuv offset arrays, shared by all instances of
238
     * PicYuv created by this encoder */
239
    intptr_t* cuOffsetY;
240
    intptr_t* cuOffsetC;
241
    intptr_t* buOffsetY;
242
    intptr_t* buOffsetC;
243
244
    int      chromaFormatIdc;        // use param
245
    uint32_t picWidthInLumaSamples;  // use param
246
    uint32_t picHeightInLumaSamples; // use param
247
248
    uint32_t numCuInWidth;
249
    uint32_t numCuInHeight;
250
    uint32_t numCUsInFrame;
251
    uint32_t numPartitions;
252
    uint32_t numPartInCUSize;
253
254
    int      log2MinCodingBlockSize;
255
    int      log2DiffMaxMinCodingBlockSize;
256
    int      log2MaxPocLsb;
257
258
    uint32_t quadtreeTULog2MaxSize;
259
    uint32_t quadtreeTULog2MinSize;
260
261
    uint32_t quadtreeTUMaxDepthInter; // use param
262
    uint32_t quadtreeTUMaxDepthIntra; // use param
263
264
    uint32_t maxAMPDepth;
265
266
    uint32_t maxTempSubLayers;   // max number of Temporal Sub layers
267
    uint32_t maxDecPicBuffering[MAX_T_LAYERS]; // these are dups of VPS values
268
    uint32_t maxLatencyIncrease[MAX_T_LAYERS];
269
    int      numReorderPics[MAX_T_LAYERS];
270
271
    RPS      spsrps[MAX_NUM_SHORT_TERM_RPS];
272
    int      spsrpsNum;
273
    int      numGOPBegin;
274
275
    bool     bUseSAO; // use param
276
    bool     bUseAMP; // use param
277
    bool     bUseStrongIntraSmoothing; // use param
278
    bool     bTemporalMVPEnabled;
279
    bool     bEmitVUITimingInfo;
280
    bool     bEmitVUIHRDInfo;
281
282
    Window   conformanceWindow;
283
    VUI      vuiParameters;
284
    bool     sps_extension_flag;
285
286
#if ENABLE_MULTIVIEW
287
    int      setSpsExtOrMaxSubLayersMinus1;
288
    int      spsInferScalingListFlag;
289
    int      maxViews;
290
    bool     vui_parameters_present_flag;
291
#endif
292
293
    SPS()
294
0
    {
295
0
        memset(this, 0, sizeof(*this));
296
0
    }
297
298
    ~SPS()
299
0
    {
300
0
        X265_FREE(cuOffsetY);
301
0
        X265_FREE(cuOffsetC);
302
0
        X265_FREE(buOffsetY);
303
0
        X265_FREE(buOffsetC);
304
0
    }
305
};
306
307
struct PPS
308
{
309
    uint32_t maxCuDQPDepth;
310
311
    int      chromaQpOffset[2];      // use param
312
    int      deblockingFilterBetaOffsetDiv2;
313
    int      deblockingFilterTcOffsetDiv2;
314
315
    bool     bUseWeightPred;         // use param
316
    bool     bUseWeightedBiPred;     // use param
317
    bool     bUseDQP;
318
    bool     bConstrainedIntraPred;  // use param
319
320
    bool     bTransquantBypassEnabled;  // Indicates presence of cu_transquant_bypass_flag in CUs.
321
    bool     bTransformSkipEnabled;     // use param
322
    bool     bEntropyCodingSyncEnabled; // use param
323
    bool     bSignHideEnabled;          // use param
324
325
    bool     bDeblockingFilterControlPresent;
326
    bool     bPicDisableDeblockingFilter;
327
328
    int      numRefIdxDefault[2];
329
    bool     pps_slice_chroma_qp_offsets_present_flag;
330
331
    bool     pps_extension_flag;
332
    int      maxViews;
333
334
    int      profileIdc;
335
};
336
337
struct WeightParam
338
{
339
    // Explicit weighted prediction parameters parsed in slice header,
340
    uint32_t log2WeightDenom;
341
    int      inputWeight;
342
    int      inputOffset;
343
    int      wtPresent;
344
345
    /* makes a non-h265 weight (i.e. fix7), into an h265 weight */
346
    void setFromWeightAndOffset(int w, int o, int denom, bool bNormalize)
347
0
    {
348
0
        inputOffset = o;
349
0
        log2WeightDenom = denom;
350
0
        inputWeight = w;
351
0
        while (bNormalize && log2WeightDenom > 0 && (inputWeight > 127))
352
0
        {
353
0
            log2WeightDenom--;
354
0
            inputWeight >>= 1;
355
0
        }
356
357
0
        inputWeight = X265_MIN(inputWeight, 127);
358
0
    }
359
};
360
361
#define SET_WEIGHT(w, b, s, d, o) \
362
0
    { \
363
0
        (w).inputWeight = (s); \
364
0
        (w).log2WeightDenom = (d); \
365
0
        (w).inputOffset = (o); \
366
0
        (w).wtPresent = (b); \
367
0
    }
368
369
class Slice
370
{
371
public:
372
373
    const SPS*  m_sps;
374
    const PPS*  m_pps;
375
    Frame*      m_refFrameList[2][MAX_NUM_REF + 1];
376
    PicYuv*     m_refReconPicList[2][MAX_NUM_REF + 1];
377
378
    WeightParam m_weightPredTable[2][MAX_NUM_REF][3]; // [list][refIdx][0:Y, 1:U, 2:V]
379
    MotionReference (*m_mref)[MAX_NUM_REF + 1];
380
    RPS         m_rps;
381
382
    NalUnitType m_nalUnitType;
383
    SliceType   m_sliceType;
384
    SliceType   m_origSliceType;
385
    int         m_sliceQp;
386
    int         m_chromaQpOffset[2];
387
    int         m_poc;
388
    int         m_lastIDR;
389
    int         m_rpsIdx;
390
391
    uint32_t    m_colRefIdx;       // never modified
392
393
    int         m_numRefIdx[2];
394
    int         m_refPOCList[2][MAX_NUM_REF + 1];
395
396
    uint32_t    m_maxNumMergeCand; // use param
397
    uint32_t    m_endCUAddr;
398
399
    bool        m_bCheckLDC;       // TODO: is this necessary?
400
    bool        m_sLFaseFlag;      // loop filter boundary flag
401
    bool        m_colFromL0Flag;   // collocated picture from List0 or List1 flag
402
    int         m_bUseSao;
403
404
    int         m_iPPSQpMinus26;
405
    int         numRefIdxDefault[2];
406
    int         m_iNumRPSInSPS;
407
    const x265_param *m_param;
408
    int         m_fieldNum;
409
    Frame*      m_mcstfRefFrameList[2][MAX_MCSTF_TEMPORAL_WINDOW_LENGTH];
410
411
#if  ENABLE_SCC_EXT
412
    Frame*      m_lastEncPic;
413
    bool        m_useIntegerMv;
414
#endif
415
    bool        m_bTemporalMvp;
416
417
    Slice()
418
0
    {
419
0
        m_lastIDR = 0;
420
0
        m_sLFaseFlag = true;
421
0
        m_numRefIdx[0] = m_numRefIdx[1] = 0;
422
0
        memset(m_refFrameList, 0, sizeof(m_refFrameList));
423
0
        memset(m_refReconPicList, 0, sizeof(m_refReconPicList));
424
0
        memset(m_refPOCList, 0, sizeof(m_refPOCList));
425
0
        disableWeights();
426
0
        m_iPPSQpMinus26 = 0;
427
0
        numRefIdxDefault[0] = 1;
428
0
        numRefIdxDefault[1] = 1;
429
0
        m_rpsIdx = -1;
430
0
        m_chromaQpOffset[0] = m_chromaQpOffset[1] = 0;
431
0
        m_fieldNum = 0;
432
#if  ENABLE_SCC_EXT
433
        m_lastEncPic = NULL;
434
        m_useIntegerMv = false;
435
#endif
436
0
        m_bTemporalMvp = false;
437
0
    }
438
439
    void disableWeights();
440
441
#if ENABLE_MULTIVIEW
442
    void setRefPicList(PicList& picList, int viewId, PicList& refPicSetInterLayer0, PicList& refPicSetInterLayer1);
443
    void createInterLayerReferencePictureSet(PicList& picList, PicList& refPicSetInterLayer0, PicList& refPicSetInterLayer1);
444
#else
445
    void setRefPicList(PicList& picList, int viewId);
446
#endif
447
448
#if  ENABLE_SCC_EXT
449
    bool isOnlyCurrentPictureAsReference() const;
450
#endif
451
452
    bool getRapPicFlag() const
453
0
    {
454
0
        return m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
455
0
            || m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
456
0
            || m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA;
457
0
    }
458
    bool getIdrPicFlag() const
459
0
    {
460
0
        return m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
461
0
            || m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP;
462
0
    }
463
0
    bool isIRAP() const   { return m_nalUnitType >= 16 && m_nalUnitType <= 23; }
464
465
0
    bool isIntra()  const { return m_sliceType == I_SLICE; }
466
467
0
    bool isInterB() const { return m_sliceType == B_SLICE; }
468
469
0
    bool isInterP() const { return m_sliceType == P_SLICE; }
470
471
    uint32_t realEndAddress(uint32_t endCUAddr) const;
472
};
473
474
}
475
476
#endif // ifndef X265_SLICE_H