Coverage Report

Created: 2022-08-24 06:15

/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
        : numberOfPictures(0)
57
        , numberOfNegativePictures(0)
58
        , numberOfPositivePictures(0)
59
48.4k
    {
60
48.4k
        memset(deltaPOC, 0, sizeof(deltaPOC));
61
48.4k
        memset(poc, 0, sizeof(poc));
62
48.4k
        memset(bUsed, 0, sizeof(bUsed));
63
48.4k
    }
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
137
    HRDInfo()
138
        : bitRateScale(0)
139
        , cpbSizeScale(0)
140
        , initialCpbRemovalDelayLength(1)
141
        , cpbRemovalDelayLength(1)
142
        , dpbOutputDelayLength(1)
143
        , cbrFlag(false)
144
1.39k
    {
145
1.39k
    }
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
172
    Window()
173
2.09k
    {
174
2.09k
        bEnabled = false;
175
2.09k
    }
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
256
    SPS()
257
698
    {
258
698
        memset(this, 0, sizeof(*this));
259
698
    }
260
261
    ~SPS()
262
698
    {
263
698
        X265_FREE(cuOffsetY);
264
698
        X265_FREE(cuOffsetC);
265
698
        X265_FREE(buOffsetY);
266
698
        X265_FREE(buOffsetC);
267
698
    }
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 */
304
    void setFromWeightAndOffset(int w, int o, int denom, bool bNormalize)
305
0
    {
306
0
        inputOffset = o;
307
0
        log2WeightDenom = denom;
308
0
        inputWeight = w;
309
0
        while (bNormalize && log2WeightDenom > 0 && (inputWeight > 127))
310
0
        {
311
0
            log2WeightDenom--;
312
0
            inputWeight >>= 1;
313
0
        }
314
315
0
        inputWeight = X265_MIN(inputWeight, 127);
316
0
    }
317
};
318
319
#define SET_WEIGHT(w, b, s, d, o) \
320
0
    { \
321
0
        (w).inputWeight = (s); \
322
0
        (w).log2WeightDenom = (d); \
323
0
        (w).inputOffset = (o); \
324
0
        (w).wtPresent = (b); \
325
0
    }
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
367
    Slice()
368
698
    {
369
698
        m_lastIDR = 0;
370
698
        m_sLFaseFlag = true;
371
698
        m_numRefIdx[0] = m_numRefIdx[1] = 0;
372
698
        memset(m_refFrameList, 0, sizeof(m_refFrameList));
373
698
        memset(m_refReconPicList, 0, sizeof(m_refReconPicList));
374
698
        memset(m_refPOCList, 0, sizeof(m_refPOCList));
375
698
        disableWeights();
376
698
        m_iPPSQpMinus26 = 0;
377
698
        numRefIdxDefault[0] = 1;
378
698
        numRefIdxDefault[1] = 1;
379
698
        m_rpsIdx = -1;
380
698
        m_chromaQpOffset[0] = m_chromaQpOffset[1] = 0;
381
698
        m_fieldNum = 0;
382
698
    }
383
384
    void disableWeights();
385
386
    void setRefPicList(PicList& picList);
387
388
    bool getRapPicFlag() const
389
698
    {
390
698
        return m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
391
698
            || m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
392
698
            || m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA;
393
698
    }
394
    bool getIdrPicFlag() const
395
698
    {
396
698
        return m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
397
698
            || m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP;
398
698
    }
399
698
    bool isIRAP() const   { return m_nalUnitType >= 16 && m_nalUnitType <= 23; }
400
401
13.6M
    bool isIntra()  const { return m_sliceType == I_SLICE; }
402
403
2.09k
    bool isInterB() const { return m_sliceType == B_SLICE; }
404
405
1.39k
    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