File: slice.cpp

package info (click to toggle)
x265 4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 15,408 kB
  • sloc: asm: 187,063; cpp: 118,996; ansic: 741; makefile: 146; sh: 91; python: 11
file content (360 lines) | stat: -rw-r--r-- 11,086 bytes parent folder | download | duplicates (3)
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
/*****************************************************************************
 * Copyright (C) 2013-2020 MulticoreWare, Inc
 *
 * Authors: Steve Borho <steve@borho.org>
 *
 * 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.
 *****************************************************************************/

#include "common.h"
#include "frame.h"
#include "piclist.h"
#include "picyuv.h"
#include "slice.h"

using namespace X265_NS;

#if ENABLE_MULTIVIEW
void Slice::createInterLayerReferencePictureSet(PicList& picList, PicList& refPicSetInterLayer0, PicList& refPicSetInterLayer1)
{

    for (int i = 0; i < 1; i++)
    {
        Frame* refPic = picList.getPOC(m_poc, 0);
        int viewIdCur = 0;
        int viewIdZero = 1;
        int viewIdRef = 1;

        if ((viewIdCur <= viewIdZero && viewIdCur <= viewIdRef) || (viewIdCur >= viewIdZero && viewIdCur >= viewIdRef))
        {
            refPicSetInterLayer0.pushBackSubDPB(*refPic);
        }
        else
        {
            refPicSetInterLayer1.pushBackSubDPB(*refPic);
        }
    }
}
#endif

#if ENABLE_MULTIVIEW
void Slice::setRefPicList(PicList& picList, int sLayerId, PicList& refPicSetInterLayer0, PicList& refPicSetInterLayer1)
#else
void Slice::setRefPicList(PicList& picList, int sLayerId)
#endif
{
    if (m_sliceType == I_SLICE)
    {
        memset(m_refFrameList, 0, sizeof(m_refFrameList));
        memset(m_refReconPicList, 0, sizeof(m_refReconPicList));
        memset(m_refPOCList, 0, sizeof(m_refPOCList));
        m_numRefIdx[1] = m_numRefIdx[0] = 0;

#if ENABLE_SCC_EXT
        bool checkNumPocTotalCurr = m_param->bEnableSCC ? false : true;
        if (!checkNumPocTotalCurr)
        {
            if (m_rps.numberOfPictures == 0)
            {
                Frame* prevPic = picList.getPOC(X265_MAX(0, m_poc - 1));
                if (prevPic->m_poc != X265_MAX(0, m_poc - 1))
                {
                    prevPic = picList.getPOC(m_poc);
                }
                m_lastEncPic = prevPic;
            }
            return;
        }
#endif

        return;
    }

#if ENABLE_SCC_EXT || ENABLE_MULTIVIEW || ENABLE_ALPHA
    /*Reset the number of references for I-slice marked as P-slice*/
    if ((m_param->bEnableSCC || sLayerId) && m_sliceType != m_origSliceType)
    {
        memset(m_refFrameList, 0, sizeof(m_refFrameList));
        memset(m_refReconPicList, 0, sizeof(m_refReconPicList));
        memset(m_refPOCList, 0, sizeof(m_refPOCList));
        m_numRefIdx[0] = 1;
    }
#endif

#if ENABLE_SCC_EXT
    bool checkNumPocTotalCurr = m_param->bEnableSCC ? false : true;
    if (!checkNumPocTotalCurr && m_rps.numberOfPictures == 0)
    {
        Frame* prevPic = picList.getPOC(X265_MAX(0, m_poc - 1));
        if (prevPic && prevPic->m_poc != X265_MAX(0, m_poc - 1))
        {
            prevPic = picList.getPOC(m_poc);

        }
        m_lastEncPic = prevPic;
    }
#endif

    Frame* refPic = NULL;
    Frame* refPicSetStCurr0[MAX_NUM_REF];
    Frame* refPicSetStCurr1[MAX_NUM_REF];
    Frame* refPicSetLtCurr[MAX_NUM_REF];
    int numPocStCurr0 = 0;
    int numPocStCurr1 = 0;
    int numPocLtCurr = 0;
    int i;

    for (i = 0; i < m_rps.numberOfNegativePictures; i++)
    {
        if (m_rps.bUsed[i] && m_origSliceType != I_SLICE)
        {
            refPic = picList.getPOC(m_poc + m_rps.deltaPOC[i], m_rps.deltaPOC[i] ? sLayerId : 0);
            refPicSetStCurr0[numPocStCurr0] = refPic;
            numPocStCurr0++;
        }
    }

    for (; i < m_rps.numberOfNegativePictures + m_rps.numberOfPositivePictures; i++)
    {
        if (m_rps.bUsed[i] && m_origSliceType != I_SLICE)
        {
            refPic = picList.getPOC(m_poc + m_rps.deltaPOC[i], m_rps.deltaPOC[i] ? sLayerId : 0);
            refPicSetStCurr1[numPocStCurr1] = refPic;
            numPocStCurr1++;
        }
    }

    X265_CHECK(m_rps.numberOfPictures == m_rps.numberOfNegativePictures + m_rps.numberOfPositivePictures,
               "unexpected picture in RPS\n");

    // ref_pic_list_init
    Frame* rpsCurrList0[MAX_NUM_REF + 1];
    Frame* rpsCurrList1[MAX_NUM_REF + 1];
#if ENABLE_MULTIVIEW
    int numPocTotalCurr = numPocStCurr0 + numPocStCurr1 + numPocLtCurr + refPicSetInterLayer0.size() + refPicSetInterLayer1.size();
#else
    int numPocTotalCurr = numPocStCurr0 + numPocStCurr1 + numPocLtCurr;
#endif

#if ENABLE_SCC_EXT
    if (m_param->bEnableSCC)
        numPocTotalCurr++;
#endif

    int cIdx = 0;
    for (i = 0; i < numPocStCurr0; i++, cIdx++)
        rpsCurrList0[cIdx] = refPicSetStCurr0[i];

#if ENABLE_MULTIVIEW
    if (m_param->numViews > 1)
        for (i = 0; i < refPicSetInterLayer0.size(); i++, cIdx++)
            rpsCurrList0[cIdx] = refPicSetInterLayer0.getPOC(m_poc, 0);
#endif

    for (i = 0; i < numPocStCurr1; i++, cIdx++)
        rpsCurrList0[cIdx] = refPicSetStCurr1[i];

    for (i = 0; i < numPocLtCurr; i++, cIdx++)
        rpsCurrList0[cIdx] = refPicSetLtCurr[i];

#if ENABLE_MULTIVIEW
    if (m_param->numViews > 1)
        for (i = 0; i < refPicSetInterLayer1.size(); i++, cIdx++)
            rpsCurrList0[cIdx] = refPicSetInterLayer1.getPOC(m_poc, 0);
#endif

#if ENABLE_SCC_EXT
    if (m_param->bEnableSCC)
        rpsCurrList0[cIdx++] = picList.getPOC(m_poc);
#endif

    X265_CHECK(cIdx == numPocTotalCurr, "RPS index check fail\n");

    if (m_sliceType == B_SLICE)
    {
        cIdx = 0;
        for (i = 0; i < numPocStCurr1; i++, cIdx++)
            rpsCurrList1[cIdx] = refPicSetStCurr1[i];

#if ENABLE_MULTIVIEW
        if (m_param->numViews > 1)
            for (i = 0; i < refPicSetInterLayer1.size(); i++, cIdx++)
                rpsCurrList1[cIdx] = refPicSetInterLayer1.getPOC(m_poc, 0);
#endif

        for (i = 0; i < numPocStCurr0; i++, cIdx++)
            rpsCurrList1[cIdx] = refPicSetStCurr0[i];

        for (i = 0; i < numPocLtCurr; i++, cIdx++)
            rpsCurrList1[cIdx] = refPicSetLtCurr[i];

#if ENABLE_MULTIVIEW
        if (m_param->numViews > 1)
            for (i = 0; i < refPicSetInterLayer0.size(); i++, cIdx++)
                rpsCurrList1[cIdx] = refPicSetInterLayer0.getPOC(m_poc, 0);
#endif

#if  ENABLE_SCC_EXT
        if (m_param->bEnableSCC)
            rpsCurrList1[cIdx++] = picList.getPOC(m_poc);
#endif

        X265_CHECK(cIdx == numPocTotalCurr, "RPS index check fail\n");
    }

    for (int rIdx = 0; rIdx < m_numRefIdx[0]; rIdx++)
    {
        cIdx = rIdx % numPocTotalCurr;
        X265_CHECK(cIdx >= 0 && cIdx < numPocTotalCurr, "RPS index check fail\n");
        m_refFrameList[0][rIdx] = rpsCurrList0[cIdx];
#if ENABLE_MULTIVIEW
        m_refFrameList[0][rIdx] = rpsCurrList0[cIdx];
#endif
    }

#if  ENABLE_SCC_EXT
    if (m_param->bEnableSCC && numPocTotalCurr > m_numRefIdx[0])
    {
        m_refFrameList[0][m_numRefIdx[0] - 1] = picList.getPOC(m_poc);
    }
#endif

    if (m_sliceType != B_SLICE)
    {
        m_numRefIdx[1] = 0;
        memset(m_refFrameList[1], 0, sizeof(m_refFrameList[1]));
    }
    else
    {
        for (int rIdx = 0; rIdx < m_numRefIdx[1]; rIdx++)
        {
            cIdx = rIdx % numPocTotalCurr;
            X265_CHECK(cIdx >= 0 && cIdx < numPocTotalCurr, "RPS index check fail\n");
            m_refFrameList[1][rIdx] = rpsCurrList1[cIdx];
#if ENABLE_MULTIVIEW
            m_refFrameList[1][rIdx] = rpsCurrList1[cIdx];
#endif
        }
    }

    for (int dir = 0; dir < 2; dir++)
        for (int numRefIdx = 0; numRefIdx < m_numRefIdx[dir]; numRefIdx++)
            m_refPOCList[dir][numRefIdx] = m_refFrameList[dir][numRefIdx]->m_poc;
}

void Slice::disableWeights()
{
    for (int l = 0; l < 2; l++)
        for (int i = 0; i < MAX_NUM_REF; i++)
            for (int yuv = 0; yuv < 3; yuv++)
            {
                WeightParam& wp = m_weightPredTable[l][i][yuv];
                wp.wtPresent = 0;
                wp.log2WeightDenom = 0;
                wp.inputWeight = 1;
                wp.inputOffset = 0;
            }
}

#if  ENABLE_SCC_EXT
bool Slice::isOnlyCurrentPictureAsReference() const
{
    if (m_sliceType == I_SLICE)
    {
        return true;
    }

    for (int i = 0; i < m_numRefIdx[0]; i++)
    {
        if (m_refFrameList[0][i]->m_poc != m_poc)
        {
            return false;
        }
    }

    for (int i = 0; i < m_numRefIdx[1]; i++)
    {
        if (m_refFrameList[1][i]->m_poc != m_poc)
        {
            return false;
        }
    }

    return true;
}
#endif

/* Sorts the deltaPOC and Used by current values in the RPS based on the
 * deltaPOC values.  deltaPOC values are sorted with -ve values before the +ve
 * values.  -ve values are in decreasing order.  +ve values are in increasing
 * order */
void RPS::sortDeltaPOC()
{
    // sort in increasing order (smallest first)
    for (int j = 1; j < numberOfPictures; j++)
    {
        int dPOC = deltaPOC[j];
        bool used = bUsed[j];
        for (int k = j - 1; k >= 0; k--)
        {
            int temp = deltaPOC[k];
            if (dPOC < temp)
            {
                deltaPOC[k + 1] = temp;
                bUsed[k + 1] = bUsed[k];
                deltaPOC[k] = dPOC;
                bUsed[k] = used;
            }
        }
    }

    // flip the negative values to largest first
    int numNegPics = numberOfNegativePictures;
    for (int j = 0, k = numNegPics - 1; j < numNegPics >> 1; j++, k--)
    {
        int dPOC = deltaPOC[j];
        bool used = bUsed[j];
        deltaPOC[j] = deltaPOC[k];
        bUsed[j] = bUsed[k];
        deltaPOC[k] = dPOC;
        bUsed[k] = used;
    }
}

uint32_t Slice::realEndAddress(uint32_t endCUAddr) const
{
    // Calculate end address
    uint32_t internalAddress = (endCUAddr - 1) % m_param->num4x4Partitions;
    uint32_t externalAddress = (endCUAddr - 1) / m_param->num4x4Partitions;
    uint32_t xmax = m_sps->picWidthInLumaSamples - (externalAddress % m_sps->numCuInWidth) * m_param->maxCUSize;
    uint32_t ymax = m_sps->picHeightInLumaSamples - (externalAddress / m_sps->numCuInWidth) * m_param->maxCUSize;

    while (g_zscanToPelX[internalAddress] >= xmax || g_zscanToPelY[internalAddress] >= ymax)
        internalAddress--;

    internalAddress++;
    if (internalAddress == m_param->num4x4Partitions)
    {
        internalAddress = 0;
        externalAddress++;
    }

    return externalAddress * m_param->num4x4Partitions + internalAddress;
}