File: slice.h

package info (click to toggle)
x265 4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,408 kB
  • sloc: asm: 187,063; cpp: 118,996; ansic: 741; makefile: 146; sh: 91; python: 11
file content (472 lines) | stat: -rw-r--r-- 12,615 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*****************************************************************************
 * Copyright (C) 2013-2020 MulticoreWare, Inc
 *
 * Authors: Steve Borho <steve@borho.org>
 *          Min Chen <chenm003@163.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at license @ x265.com.
 *****************************************************************************/

#ifndef X265_SLICE_H
#define X265_SLICE_H

#include "common.h"

namespace X265_NS {
// private namespace

class Frame;
class PicList;
class PicYuv;
class MotionReference;

enum SliceType
{
    B_SLICE,
    P_SLICE,
    I_SLICE
};

struct RPS
{
    int  numberOfPictures;
    int  numberOfNegativePictures;
    int  numberOfPositivePictures;

    int  poc[MAX_NUM_REF_PICS];
    int  deltaPOC[MAX_NUM_REF_PICS];
    bool bUsed[MAX_NUM_REF_PICS];

    RPS()
        : numberOfPictures(0)
        , numberOfNegativePictures(0)
        , numberOfPositivePictures(0)
    {
        memset(deltaPOC, 0, sizeof(deltaPOC));
        memset(poc, 0, sizeof(poc));
        memset(bUsed, 0, sizeof(bUsed));
    }

    void sortDeltaPOC();
};

namespace Profile {
    enum Name
    {
        NONE = 0,
        MAIN = 1,
        MAIN10 = 2,
        MAINSTILLPICTURE = 3,
        MAINREXT = 4,
        HIGHTHROUGHPUTREXT = 5,
        MULTIVIEWMAIN = 6,
        SCALABLEMAIN = 7,
        SCALABLEMAIN10 = 8,
        MAINSCC = 9
    };
}

namespace Level {
    enum Tier
    {
        MAIN = 0,
        HIGH = 1,
    };

    enum Name
    {
        NONE = 0,
        LEVEL1 = 30,
        LEVEL2 = 60,
        LEVEL2_1 = 63,
        LEVEL3 = 90,
        LEVEL3_1 = 93,
        LEVEL4 = 120,
        LEVEL4_1 = 123,
        LEVEL5 = 150,
        LEVEL5_1 = 153,
        LEVEL5_2 = 156,
        LEVEL6 = 180,
        LEVEL6_1 = 183,
        LEVEL6_2 = 186,
        LEVEL8_5 = 255,
    };
}

struct ProfileTierLevel
{
    int      profileIdc[MAX_LAYERS];
    int      levelIdc;
    uint32_t minCrForLevel;
    uint32_t maxLumaSrForLevel;
    uint32_t bitDepthConstraint;
    int      chromaFormatConstraint;
    bool     tierFlag;
    bool     progressiveSourceFlag;
    bool     interlacedSourceFlag;
    bool     nonPackedConstraintFlag;
    bool     frameOnlyConstraintFlag;
    bool     profileCompatibilityFlag[32];
    bool     intraConstraintFlag;
    bool     onePictureOnlyConstraintFlag;
    bool     lowerBitRateConstraintFlag;
};

struct HRDInfo
{
    uint32_t bitRateScale;
    uint32_t cpbSizeScale;
    uint32_t initialCpbRemovalDelayLength;
    uint32_t cpbRemovalDelayLength;
    uint32_t dpbOutputDelayLength;
    uint32_t bitRateValue;
    uint32_t cpbSizeValue;
    bool     cbrFlag;

    HRDInfo()
        : bitRateScale(0)
        , cpbSizeScale(0)
        , initialCpbRemovalDelayLength(1)
        , cpbRemovalDelayLength(1)
        , dpbOutputDelayLength(1)
        , cbrFlag(false)
    {
    }
};

struct TimingInfo
{
    uint32_t numUnitsInTick;
    uint32_t timeScale;
};

struct VPS
{
    HRDInfo          hrdParameters;
    ProfileTierLevel ptl;
    uint32_t         maxTempSubLayers;
    uint32_t         numReorderPics[MAX_T_LAYERS];
    uint32_t         maxDecPicBuffering[MAX_T_LAYERS];
    uint32_t         maxLatencyIncrease[MAX_T_LAYERS];
    int              m_numLayers;
    int              m_numViews;
    bool             vps_extension_flag;

#if (ENABLE_ALPHA || ENABLE_MULTIVIEW)
    bool             splitting_flag;
    int              m_scalabilityMask[MAX_VPS_NUM_SCALABILITY_TYPES];
    int              scalabilityTypes;
    uint8_t          m_dimensionIdLen[MAX_VPS_NUM_SCALABILITY_TYPES];
    uint8_t          m_dimensionId[MAX_VPS_LAYER_ID_PLUS1][MAX_VPS_NUM_SCALABILITY_TYPES];
    bool             m_nuhLayerIdPresentFlag;
    uint8_t          m_layerIdInNuh[MAX_VPS_LAYER_ID_PLUS1];
    uint8_t          m_layerIdInVps[MAX_VPS_LAYER_ID_PLUS1];
    int              m_viewIdLen;
    int              m_vpsNumLayerSetsMinus1;
    int              m_numLayersInIdList[1023];
#endif

#if ENABLE_MULTIVIEW
    int              m_layerIdIncludedFlag;
#endif
};

struct Window
{
    int  leftOffset;
    int  rightOffset;
    int  topOffset;
    int  bottomOffset;
    bool bEnabled;

    Window()
    {
        bEnabled = false;
    }
};

struct VUI
{
    int        aspectRatioIdc;
    int        sarWidth;
    int        sarHeight;
    int        videoFormat;
    int        colourPrimaries;
    int        transferCharacteristics;
    int        matrixCoefficients;
    int        chromaSampleLocTypeTopField;
    int        chromaSampleLocTypeBottomField;

    bool       aspectRatioInfoPresentFlag;
    bool       overscanInfoPresentFlag;
    bool       overscanAppropriateFlag;
    bool       videoSignalTypePresentFlag;
    bool       videoFullRangeFlag;
    bool       colourDescriptionPresentFlag;
    bool       chromaLocInfoPresentFlag;
    bool       frameFieldInfoPresentFlag;
    bool       fieldSeqFlag;
    bool       hrdParametersPresentFlag;

    HRDInfo    hrdParameters;
    Window     defaultDisplayWindow;
    TimingInfo timingInfo;
};

struct SPS
{
    /* cached PicYuv offset arrays, shared by all instances of
     * PicYuv created by this encoder */
    intptr_t* cuOffsetY;
    intptr_t* cuOffsetC;
    intptr_t* buOffsetY;
    intptr_t* buOffsetC;

    int      chromaFormatIdc;        // use param
    uint32_t picWidthInLumaSamples;  // use param
    uint32_t picHeightInLumaSamples; // use param

    uint32_t numCuInWidth;
    uint32_t numCuInHeight;
    uint32_t numCUsInFrame;
    uint32_t numPartitions;
    uint32_t numPartInCUSize;

    int      log2MinCodingBlockSize;
    int      log2DiffMaxMinCodingBlockSize;
    int      log2MaxPocLsb;

    uint32_t quadtreeTULog2MaxSize;
    uint32_t quadtreeTULog2MinSize;

    uint32_t quadtreeTUMaxDepthInter; // use param
    uint32_t quadtreeTUMaxDepthIntra; // use param

    uint32_t maxAMPDepth;

    uint32_t maxTempSubLayers;   // max number of Temporal Sub layers
    uint32_t maxDecPicBuffering[MAX_T_LAYERS]; // these are dups of VPS values
    uint32_t maxLatencyIncrease[MAX_T_LAYERS];
    int      numReorderPics[MAX_T_LAYERS];

    RPS      spsrps[MAX_NUM_SHORT_TERM_RPS];
    int      spsrpsNum;
    int      numGOPBegin;

    bool     bUseSAO; // use param
    bool     bUseAMP; // use param
    bool     bUseStrongIntraSmoothing; // use param
    bool     bTemporalMVPEnabled;
    bool     bEmitVUITimingInfo;
    bool     bEmitVUIHRDInfo;

    Window   conformanceWindow;
    VUI      vuiParameters;
    bool     sps_extension_flag;

#if ENABLE_MULTIVIEW
    int      setSpsExtOrMaxSubLayersMinus1;
    int      spsInferScalingListFlag;
    int      maxViews;
    bool     vui_parameters_present_flag;
#endif

    SPS()
    {
        memset(this, 0, sizeof(*this));
    }

    ~SPS()
    {
        X265_FREE(cuOffsetY);
        X265_FREE(cuOffsetC);
        X265_FREE(buOffsetY);
        X265_FREE(buOffsetC);
    }
};

struct PPS
{
    uint32_t maxCuDQPDepth;

    int      chromaQpOffset[2];      // use param
    int      deblockingFilterBetaOffsetDiv2;
    int      deblockingFilterTcOffsetDiv2;

    bool     bUseWeightPred;         // use param
    bool     bUseWeightedBiPred;     // use param
    bool     bUseDQP;
    bool     bConstrainedIntraPred;  // use param

    bool     bTransquantBypassEnabled;  // Indicates presence of cu_transquant_bypass_flag in CUs.
    bool     bTransformSkipEnabled;     // use param
    bool     bEntropyCodingSyncEnabled; // use param
    bool     bSignHideEnabled;          // use param

    bool     bDeblockingFilterControlPresent;
    bool     bPicDisableDeblockingFilter;

    int      numRefIdxDefault[2];
    bool     pps_slice_chroma_qp_offsets_present_flag;

    bool     pps_extension_flag;
    int      maxViews;

    int      profileIdc;
};

struct WeightParam
{
    // Explicit weighted prediction parameters parsed in slice header,
    uint32_t log2WeightDenom;
    int      inputWeight;
    int      inputOffset;
    int      wtPresent;

    /* makes a non-h265 weight (i.e. fix7), into an h265 weight */
    void setFromWeightAndOffset(int w, int o, int denom, bool bNormalize)
    {
        inputOffset = o;
        log2WeightDenom = denom;
        inputWeight = w;
        while (bNormalize && log2WeightDenom > 0 && (inputWeight > 127))
        {
            log2WeightDenom--;
            inputWeight >>= 1;
        }

        inputWeight = X265_MIN(inputWeight, 127);
    }
};

#define SET_WEIGHT(w, b, s, d, o) \
    { \
        (w).inputWeight = (s); \
        (w).log2WeightDenom = (d); \
        (w).inputOffset = (o); \
        (w).wtPresent = (b); \
    }

class Slice
{
public:

    const SPS*  m_sps;
    const PPS*  m_pps;
    Frame*      m_refFrameList[2][MAX_NUM_REF + 1];
    PicYuv*     m_refReconPicList[2][MAX_NUM_REF + 1];

    WeightParam m_weightPredTable[2][MAX_NUM_REF][3]; // [list][refIdx][0:Y, 1:U, 2:V]
    MotionReference (*m_mref)[MAX_NUM_REF + 1];
    RPS         m_rps;

    NalUnitType m_nalUnitType;
    SliceType   m_sliceType;
    SliceType   m_origSliceType;
    int         m_sliceQp;
    int         m_chromaQpOffset[2];
    int         m_poc;
    int         m_lastIDR;
    int         m_rpsIdx;

    uint32_t    m_colRefIdx;       // never modified

    int         m_numRefIdx[2];
    int         m_refPOCList[2][MAX_NUM_REF + 1];

    uint32_t    m_maxNumMergeCand; // use param
    uint32_t    m_endCUAddr;

    bool        m_bCheckLDC;       // TODO: is this necessary?
    bool        m_sLFaseFlag;      // loop filter boundary flag
    bool        m_colFromL0Flag;   // collocated picture from List0 or List1 flag
    int         m_bUseSao;

    int         m_iPPSQpMinus26;
    int         numRefIdxDefault[2];
    int         m_iNumRPSInSPS;
    const x265_param *m_param;
    int         m_fieldNum;
    Frame*      m_mcstfRefFrameList[2][MAX_MCSTF_TEMPORAL_WINDOW_LENGTH];

#if  ENABLE_SCC_EXT
    Frame*      m_lastEncPic;
    bool        m_useIntegerMv;
#endif
    bool        m_bTemporalMvp;

    Slice()
    {
        m_lastIDR = 0;
        m_sLFaseFlag = true;
        m_numRefIdx[0] = m_numRefIdx[1] = 0;
        memset(m_refFrameList, 0, sizeof(m_refFrameList));
        memset(m_refReconPicList, 0, sizeof(m_refReconPicList));
        memset(m_refPOCList, 0, sizeof(m_refPOCList));
        disableWeights();
        m_iPPSQpMinus26 = 0;
        numRefIdxDefault[0] = 1;
        numRefIdxDefault[1] = 1;
        m_rpsIdx = -1;
        m_chromaQpOffset[0] = m_chromaQpOffset[1] = 0;
        m_fieldNum = 0;
#if  ENABLE_SCC_EXT
        m_lastEncPic = NULL;
        m_useIntegerMv = false;
#endif
        m_bTemporalMvp = false;
    }

    void disableWeights();

#if ENABLE_MULTIVIEW
    void setRefPicList(PicList& picList, int viewId, PicList& refPicSetInterLayer0, PicList& refPicSetInterLayer1);
    void createInterLayerReferencePictureSet(PicList& picList, PicList& refPicSetInterLayer0, PicList& refPicSetInterLayer1);
#else
    void setRefPicList(PicList& picList, int viewId);
#endif

#if  ENABLE_SCC_EXT
    bool isOnlyCurrentPictureAsReference() const;
#endif

    bool getRapPicFlag() const
    {
        return m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
            || m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP
            || m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA;
    }
    bool getIdrPicFlag() const
    {
        return m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL
            || m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP;
    }
    bool isIRAP() const   { return m_nalUnitType >= 16 && m_nalUnitType <= 23; }

    bool isIntra()  const { return m_sliceType == I_SLICE; }

    bool isInterB() const { return m_sliceType == B_SLICE; }

    bool isInterP() const { return m_sliceType == P_SLICE; }

    uint32_t realEndAddress(uint32_t endCUAddr) const;
};

}

#endif // ifndef X265_SLICE_H