/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 | 0 | #define HISTOGRAM_NUMBER_OF_BINS 256 |
36 | 0 | #define NUMBER_OF_SEGMENTS_IN_WIDTH 4 |
37 | 0 | #define NUMBER_OF_SEGMENTS_IN_HEIGHT 4 |
38 | | |
39 | | struct ReferencePlanes |
40 | | { |
41 | 0 | ReferencePlanes() { memset(this, 0, sizeof(ReferencePlanes)); } |
42 | | |
43 | | pixel* fpelPlane[3]; |
44 | | pixel* lowresPlane[4]; |
45 | | PicYuv* reconPic; |
46 | | |
47 | | /* 1/16th resolution : Level-0 HME planes */ |
48 | | pixel* fpelLowerResPlane[3]; |
49 | | pixel* lowerResPlane[4]; |
50 | | |
51 | | bool isWeighted; |
52 | | bool isLowres; |
53 | | bool isHMELowres; |
54 | | |
55 | | intptr_t lumaStride; |
56 | | intptr_t chromaStride; |
57 | | |
58 | | struct { |
59 | | int weight; |
60 | | int offset; |
61 | | int shift; |
62 | | int round; |
63 | | } w[3]; |
64 | | |
65 | 0 | pixel* getLumaAddr(uint32_t ctuAddr, uint32_t absPartIdx) { return fpelPlane[0] + reconPic->m_cuOffsetY[ctuAddr] + reconPic->m_buOffsetY[absPartIdx]; } |
66 | 0 | pixel* getCbAddr(uint32_t ctuAddr, uint32_t absPartIdx) { return fpelPlane[1] + reconPic->m_cuOffsetC[ctuAddr] + reconPic->m_buOffsetC[absPartIdx]; } |
67 | 0 | pixel* getCrAddr(uint32_t ctuAddr, uint32_t absPartIdx) { return fpelPlane[2] + reconPic->m_cuOffsetC[ctuAddr] + reconPic->m_buOffsetC[absPartIdx]; } |
68 | | |
69 | | /* lowres motion compensation, you must provide a buffer and stride for QPEL averaged pixels |
70 | | * in case QPEL is required. Else it returns a pointer to the HPEL pixels */ |
71 | | inline pixel *lowresMC(intptr_t blockOffset, const MV& qmv, pixel *buf, intptr_t& outstride, bool hme) |
72 | 0 | { |
73 | 0 | intptr_t YStride = hme ? lumaStride / 2 : lumaStride; |
74 | 0 | pixel **plane = hme ? lowerResPlane : lowresPlane; |
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 = hme ? lowerResPlane : lowresPlane; |
98 | 0 | if ((qmv.x | qmv.y) & 1) |
99 | 0 | { |
100 | 0 | ALIGN_VAR_16(pixel, subpelbuf[8 * 8]); |
101 | 0 | int hpelA = (qmv.y & 2) | ((qmv.x & 2) >> 1); |
102 | 0 | pixel *frefA = plane[hpelA] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * YStride; |
103 | 0 | int qmvx = qmv.x + (qmv.x & 1); |
104 | 0 | int qmvy = qmv.y + (qmv.y & 1); |
105 | 0 | int hpelB = (qmvy & 2) | ((qmvx & 2) >> 1); |
106 | 0 | pixel *frefB = plane[hpelB] + blockOffset + (qmvx >> 2) + (qmvy >> 2) * YStride; |
107 | 0 | primitives.pu[LUMA_8x8].pixelavg_pp[NONALIGNED](subpelbuf, 8, frefA, YStride, frefB, YStride, 32); |
108 | 0 | return comp(fenc, FENC_STRIDE, subpelbuf, 8); |
109 | 0 | } |
110 | 0 | else |
111 | 0 | { |
112 | 0 | int hpel = (qmv.y & 2) | ((qmv.x & 2) >> 1); |
113 | 0 | pixel *fref = plane[hpel] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * YStride; |
114 | 0 | return comp(fenc, FENC_STRIDE, fref, YStride); |
115 | 0 | } |
116 | 0 | } |
117 | | }; |
118 | | |
119 | | static const uint32_t aqLayerDepth[3][4][4] = { |
120 | | { // ctu size 64 |
121 | | { 1, 0, 1, 0 }, |
122 | | { 1, 1, 1, 0 }, |
123 | | { 1, 1, 1, 0 }, |
124 | | { 1, 1, 1, 1 } |
125 | | }, |
126 | | { // ctu size 32 |
127 | | { 1, 1, 0, 0 }, |
128 | | { 1, 1, 0, 0 }, |
129 | | { 1, 1, 1, 0 }, |
130 | | { 0, 0, 0, 0 }, |
131 | | }, |
132 | | { // ctu size 16 |
133 | | { 1, 0, 0, 0 }, |
134 | | { 1, 1, 0, 0 }, |
135 | | { 0, 0, 0, 0 }, |
136 | | { 0, 0, 0, 0 } |
137 | | } |
138 | | }; |
139 | | |
140 | | // min aq size for ctu size 64, 32 and 16 |
141 | | static const uint32_t minAQSize[3] = { 3, 2, 1 }; |
142 | | |
143 | | struct PicQPAdaptationLayer |
144 | | { |
145 | | uint32_t aqPartWidth; |
146 | | uint32_t aqPartHeight; |
147 | | uint32_t numAQPartInWidth; |
148 | | uint32_t numAQPartInHeight; |
149 | | uint32_t minAQDepth; |
150 | | double* dActivity; |
151 | | double* dQpOffset; |
152 | | |
153 | | double* dCuTreeOffset; |
154 | | double* dCuTreeOffset8x8; |
155 | | double dAvgActivity; |
156 | | bool bQpSize; |
157 | | |
158 | | bool create(uint32_t width, uint32_t height, uint32_t aqPartWidth, uint32_t aqPartHeight, uint32_t numAQPartInWidthExt, uint32_t numAQPartInHeightExt); |
159 | | void destroy(); |
160 | | }; |
161 | | |
162 | | /* lowres buffers, sizes and strides */ |
163 | | struct Lowres : public ReferencePlanes |
164 | | { |
165 | | pixel *buffer[4]; |
166 | | pixel *lowerResBuffer[4]; // Level-0 buffer |
167 | | |
168 | | int frameNum; // Presentation frame number |
169 | | int sliceType; // Slice type decided by lookahead |
170 | | int sliceTypeReq; // Slice type required as per the QP file |
171 | | int width; // width of lowres frame in pixels |
172 | | int lines; // height of lowres frame in pixel lines |
173 | | int leadingBframes; // number of leading B frames for P or I |
174 | | |
175 | | bool bScenecut; // Set to false if the frame cannot possibly be part of a real scenecut. |
176 | | bool bKeyframe; |
177 | | bool bLastMiniGopBFrame; |
178 | | bool bIsFadeEnd; |
179 | | |
180 | | double ipCostRatio; |
181 | | |
182 | | /* lookahead output data */ |
183 | | int64_t costEst[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2]; |
184 | | int64_t costEstAq[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2]; |
185 | | int32_t* rowSatds[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2]; |
186 | | int intraMbs[X265_BFRAME_MAX + 2]; |
187 | | int32_t* intraCost; |
188 | | uint8_t* intraMode; |
189 | | int64_t satdCost; |
190 | | uint16_t* lowresCostForRc; |
191 | | uint16_t* lowresCosts[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2]; |
192 | | int32_t* lowresMvCosts[2][X265_BFRAME_MAX + 2]; |
193 | | MV* lowresMvs[2][X265_BFRAME_MAX + 2]; |
194 | | MV* lowresMcstfMvs[2][4]; |
195 | | uint32_t maxBlocksInRow; |
196 | | uint32_t maxBlocksInCol; |
197 | | uint32_t maxBlocksInRowFullRes; |
198 | | uint32_t maxBlocksInColFullRes; |
199 | | |
200 | | /* Hierarchical Motion Estimation */ |
201 | | bool bEnableHME; |
202 | | int32_t* lowerResMvCosts[2][X265_BFRAME_MAX + 2]; |
203 | | MV* lowerResMvs[2][X265_BFRAME_MAX + 2]; |
204 | | |
205 | | /* used for vbvLookahead */ |
206 | | int plannedType[X265_LOOKAHEAD_MAX + 1]; |
207 | | int64_t plannedSatd[X265_LOOKAHEAD_MAX + 1]; |
208 | | int indB; |
209 | | int bframes; |
210 | | |
211 | | /* rate control / adaptive quant data */ |
212 | | double* qpAqOffset; // AQ QP offset values for each 16x16 CU |
213 | | double* qpCuTreeOffset; // cuTree QP offset values for each 16x16 CU |
214 | | double* qpAqMotionOffset; |
215 | | int* invQscaleFactor; // qScale values for qp Aq Offsets |
216 | | int* invQscaleFactor8x8; // temporary buffer for qg-size 8 |
217 | | uint32_t* blockVariance; |
218 | | uint64_t wp_ssd[3]; // This is different than SSDY, this is sum(pixel^2) - sum(pixel)^2 for entire frame |
219 | | uint64_t wp_sum[3]; |
220 | | double frameVariance; |
221 | | int* edgeInclined; |
222 | | |
223 | | |
224 | | /* cutree intermediate data */ |
225 | | PicQPAdaptationLayer* pAQLayer; |
226 | | uint32_t maxAQDepth; |
227 | | uint32_t widthFullRes; |
228 | | uint32_t heightFullRes; |
229 | | uint32_t m_maxCUSize; |
230 | | uint32_t m_qgSize; |
231 | | |
232 | | uint16_t* propagateCost; |
233 | | double weightedCostDelta[X265_BFRAME_MAX + 2]; |
234 | | ReferencePlanes weightedRef[X265_BFRAME_MAX + 2]; |
235 | | |
236 | | /* For hist-based scenecut */ |
237 | | int quarterSampleLowResWidth; // width of 1/4 lowres frame in pixels |
238 | | int quarterSampleLowResHeight; // height of 1/4 lowres frame in pixels |
239 | | int quarterSampleLowResStrideY; |
240 | | int quarterSampleLowResOriginX; |
241 | | int quarterSampleLowResOriginY; |
242 | | pixel *quarterSampleLowResBuffer; |
243 | | bool bHistScenecutAnalyzed; |
244 | | |
245 | | uint16_t picAvgVariance; |
246 | | uint16_t picAvgVarianceCb; |
247 | | uint16_t picAvgVarianceCr; |
248 | | |
249 | | uint32_t ****picHistogram; |
250 | | uint64_t averageIntensityPerSegment[NUMBER_OF_SEGMENTS_IN_WIDTH][NUMBER_OF_SEGMENTS_IN_HEIGHT][3]; |
251 | | uint8_t averageIntensity[3]; |
252 | | |
253 | | bool create(x265_param* param, PicYuv *origPic, uint32_t qgSize); |
254 | | void destroy(x265_param* param); |
255 | | void init(PicYuv *origPic, int poc); |
256 | | }; |
257 | | } |
258 | | |
259 | | #endif // ifndef X265_LOWRES_H |