Coverage Report

Created: 2022-08-24 06:17

/src/x265/source/common/lowres.h
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************
2
 * Copyright (C) 2013-2020 MulticoreWare, Inc
3
 *
4
 * Authors: Gopu Govindaswamy <gopu@multicorewareinc.com>
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
19
 *
20
 * This program is also available under a commercial proprietary license.
21
 * For more information, contact us at license @ x265.com.
22
 *****************************************************************************/
23
24
#ifndef X265_LOWRES_H
25
#define X265_LOWRES_H
26
27
#include "primitives.h"
28
#include "common.h"
29
#include "picyuv.h"
30
#include "mv.h"
31
32
namespace X265_NS {
33
// private namespace
34
35
struct ReferencePlanes
36
{
37
119k
    ReferencePlanes() { memset(this, 0, sizeof(ReferencePlanes)); }
38
39
    pixel*   fpelPlane[3];
40
    pixel*   lowresPlane[4];
41
    PicYuv*  reconPic;
42
43
    /* 1/16th resolution : Level-0 HME planes */
44
    pixel*   fpelLowerResPlane[3];
45
    pixel*   lowerResPlane[4];
46
47
    bool     isWeighted;
48
    bool     isLowres;
49
    bool     isHMELowres;
50
51
    intptr_t lumaStride;
52
    intptr_t chromaStride;
53
54
    struct {
55
        int      weight;
56
        int      offset;
57
        int      shift;
58
        int      round;
59
    } w[3];
60
61
0
    pixel* getLumaAddr(uint32_t ctuAddr, uint32_t absPartIdx) { return fpelPlane[0] + reconPic->m_cuOffsetY[ctuAddr] + reconPic->m_buOffsetY[absPartIdx]; }
62
0
    pixel* getCbAddr(uint32_t ctuAddr, uint32_t absPartIdx)   { return fpelPlane[1] + reconPic->m_cuOffsetC[ctuAddr] + reconPic->m_buOffsetC[absPartIdx]; }
63
0
    pixel* getCrAddr(uint32_t ctuAddr, uint32_t absPartIdx)   { return fpelPlane[2] + reconPic->m_cuOffsetC[ctuAddr] + reconPic->m_buOffsetC[absPartIdx]; }
64
65
    /* lowres motion compensation, you must provide a buffer and stride for QPEL averaged pixels
66
     * in case QPEL is required.  Else it returns a pointer to the HPEL pixels */
67
    inline pixel *lowresMC(intptr_t blockOffset, const MV& qmv, pixel *buf, intptr_t& outstride, bool hme)
68
0
    {
69
0
        intptr_t YStride = hme ? lumaStride / 2 : lumaStride;
70
0
        pixel *plane[4];
71
0
        for (int i = 0; i < 4; i++)
72
0
        {
73
0
            plane[i] = hme ? lowerResPlane[i] : lowresPlane[i];
74
0
        }
75
0
        if ((qmv.x | qmv.y) & 1)
76
0
        {
77
0
            int hpelA = (qmv.y & 2) | ((qmv.x & 2) >> 1);
78
0
            pixel *frefA = plane[hpelA] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * YStride;
79
0
            int qmvx = qmv.x + (qmv.x & 1);
80
0
            int qmvy = qmv.y + (qmv.y & 1);
81
0
            int hpelB = (qmvy & 2) | ((qmvx & 2) >> 1);
82
0
            pixel *frefB = plane[hpelB] + blockOffset + (qmvx >> 2) + (qmvy >> 2) * YStride;
83
0
            primitives.pu[LUMA_8x8].pixelavg_pp[(outstride % 64 == 0) && (YStride % 64 == 0)](buf, outstride, frefA, YStride, frefB, YStride, 32);
84
0
            return buf;
85
0
        }
86
0
        else
87
0
        {
88
0
            outstride = YStride;
89
0
            int hpel = (qmv.y & 2) | ((qmv.x & 2) >> 1);
90
0
            return plane[hpel] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * YStride;
91
0
        }
92
0
    }
93
94
    inline int lowresQPelCost(pixel *fenc, intptr_t blockOffset, const MV& qmv, pixelcmp_t comp, bool hme)
95
0
    {
96
0
        intptr_t YStride = hme ? lumaStride / 2 : lumaStride;
97
0
        pixel *plane[4];
98
0
        for (int i = 0; i < 4; i++)
99
0
        {
100
0
            plane[i] = hme ? lowerResPlane[i] : lowresPlane[i];
101
0
        }
102
0
        if ((qmv.x | qmv.y) & 1)
103
0
        {
104
0
            ALIGN_VAR_16(pixel, subpelbuf[8 * 8]);
105
0
            int hpelA = (qmv.y & 2) | ((qmv.x & 2) >> 1);
106
0
            pixel *frefA = plane[hpelA] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * YStride;
107
0
            int qmvx = qmv.x + (qmv.x & 1);
108
0
            int qmvy = qmv.y + (qmv.y & 1);
109
0
            int hpelB = (qmvy & 2) | ((qmvx & 2) >> 1);
110
0
            pixel *frefB = plane[hpelB] + blockOffset + (qmvx >> 2) + (qmvy >> 2) * YStride;
111
0
            primitives.pu[LUMA_8x8].pixelavg_pp[NONALIGNED](subpelbuf, 8, frefA, YStride, frefB, YStride, 32);
112
0
            return comp(fenc, FENC_STRIDE, subpelbuf, 8);
113
0
        }
114
0
        else
115
0
        {
116
0
            int hpel = (qmv.y & 2) | ((qmv.x & 2) >> 1);
117
0
            pixel *fref = plane[hpel] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * YStride;
118
0
            return comp(fenc, FENC_STRIDE, fref, YStride);
119
0
        }
120
0
    }
121
};
122
123
static const uint32_t aqLayerDepth[3][4][4] = {
124
    {  // ctu size 64
125
        { 1, 0, 1, 0 },
126
        { 1, 1, 1, 0 },
127
        { 1, 1, 1, 0 },
128
        { 1, 1, 1, 1 }
129
    },
130
    {  // ctu size 32
131
        { 1, 1, 0, 0 },
132
        { 1, 1, 0, 0 },
133
        { 1, 1, 1, 0 },
134
        { 0, 0, 0, 0 },
135
    },
136
    {  // ctu size 16
137
        { 1, 0, 0, 0 },
138
        { 1, 1, 0, 0 },
139
        { 0, 0, 0, 0 },
140
        { 0, 0, 0, 0 }
141
    }
142
};
143
144
// min aq size for ctu size 64, 32 and 16
145
static const uint32_t minAQSize[3] = { 3, 2, 1 };
146
147
struct PicQPAdaptationLayer
148
{
149
    uint32_t aqPartWidth;
150
    uint32_t aqPartHeight;
151
    uint32_t numAQPartInWidth;
152
    uint32_t numAQPartInHeight;
153
    uint32_t minAQDepth;
154
    double*  dActivity;
155
    double*  dQpOffset;
156
157
    double*  dCuTreeOffset;
158
    double*  dCuTreeOffset8x8;
159
    double   dAvgActivity;
160
    bool     bQpSize;
161
162
    bool  create(uint32_t width, uint32_t height, uint32_t aqPartWidth, uint32_t aqPartHeight, uint32_t numAQPartInWidthExt, uint32_t numAQPartInHeightExt);
163
    void  destroy();
164
};
165
166
/* lowres buffers, sizes and strides */
167
struct Lowres : public ReferencePlanes
168
{
169
    pixel *buffer[4];
170
    pixel *lowerResBuffer[4]; // Level-0 buffer
171
172
    int    frameNum;         // Presentation frame number
173
    int    sliceType;        // Slice type decided by lookahead
174
    int    width;            // width of lowres frame in pixels
175
    int    lines;            // height of lowres frame in pixel lines
176
    int    leadingBframes;   // number of leading B frames for P or I
177
178
    bool   bScenecut;        // Set to false if the frame cannot possibly be part of a real scenecut.
179
    bool   bKeyframe;
180
    bool   bLastMiniGopBFrame;
181
    bool   bIsFadeEnd;
182
183
    double ipCostRatio;
184
185
    /* lookahead output data */
186
    int64_t   costEst[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2];
187
    int64_t   costEstAq[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2];
188
    int32_t*  rowSatds[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2];
189
    int       intraMbs[X265_BFRAME_MAX + 2];
190
    int32_t*  intraCost;
191
    uint8_t*  intraMode;
192
    int64_t   satdCost;
193
    uint16_t* lowresCostForRc;
194
    uint16_t* lowresCosts[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2];
195
    int32_t*  lowresMvCosts[2][X265_BFRAME_MAX + 2];
196
    MV*       lowresMvs[2][X265_BFRAME_MAX + 2];
197
    uint32_t  maxBlocksInRow;
198
    uint32_t  maxBlocksInCol;
199
    uint32_t  maxBlocksInRowFullRes;
200
    uint32_t  maxBlocksInColFullRes;
201
202
    /* Hierarchical Motion Estimation */
203
    bool      bEnableHME;
204
    int32_t*  lowerResMvCosts[2][X265_BFRAME_MAX + 2];
205
    MV*       lowerResMvs[2][X265_BFRAME_MAX + 2];
206
207
    /* used for vbvLookahead */
208
    int       plannedType[X265_LOOKAHEAD_MAX + 1];
209
    int64_t   plannedSatd[X265_LOOKAHEAD_MAX + 1];
210
    int       indB;
211
    int       bframes;
212
213
    /* rate control / adaptive quant data */
214
    double*   qpAqOffset;      // AQ QP offset values for each 16x16 CU
215
    double*   qpCuTreeOffset;  // cuTree QP offset values for each 16x16 CU
216
    double*   qpAqMotionOffset;
217
    int*      invQscaleFactor; // qScale values for qp Aq Offsets
218
    int*      invQscaleFactor8x8; // temporary buffer for qg-size 8
219
    uint32_t* blockVariance;
220
    uint64_t  wp_ssd[3];       // This is different than SSDY, this is sum(pixel^2) - sum(pixel)^2 for entire frame
221
    uint64_t  wp_sum[3];
222
    double    frameVariance;
223
    int* edgeInclined;
224
225
226
    /* cutree intermediate data */
227
    PicQPAdaptationLayer* pAQLayer;
228
    uint32_t maxAQDepth;
229
    uint32_t widthFullRes;
230
    uint32_t heightFullRes;
231
    uint32_t m_maxCUSize;
232
    uint32_t m_qgSize;
233
    
234
    uint16_t* propagateCost;
235
    double    weightedCostDelta[X265_BFRAME_MAX + 2];
236
    ReferencePlanes weightedRef[X265_BFRAME_MAX + 2];
237
    bool create(x265_param* param, PicYuv *origPic, uint32_t qgSize);
238
    void destroy();
239
    void init(PicYuv *origPic, int poc);
240
};
241
}
242
243
#endif // ifndef X265_LOWRES_H