/src/x265/source/common/intrapred.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * Copyright (C) 2013-2020 MulticoreWare, Inc |
3 | | * |
4 | | * Authors: Min Chen <chenm003@163.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 | | #include "common.h" |
25 | | #include "primitives.h" |
26 | | |
27 | | using namespace X265_NS; |
28 | | |
29 | | namespace { |
30 | | |
31 | | template<int tuSize> |
32 | | void intraFilter(const pixel* samples, pixel* filtered) /* 1:2:1 filtering of left and top reference samples */ |
33 | 0 | { |
34 | 0 | const int tuSize2 = tuSize << 1; |
35 | |
|
36 | 0 | pixel topLeft = samples[0], topLast = samples[tuSize2], leftLast = samples[tuSize2 + tuSize2]; |
37 | | |
38 | | // filtering top |
39 | 0 | for (int i = 1; i < tuSize2; i++) |
40 | 0 | filtered[i] = ((samples[i] << 1) + samples[i - 1] + samples[i + 1] + 2) >> 2; |
41 | 0 | filtered[tuSize2] = topLast; |
42 | | |
43 | | // filtering top-left |
44 | 0 | filtered[0] = ((topLeft << 1) + samples[1] + samples[tuSize2 + 1] + 2) >> 2; |
45 | | |
46 | | // filtering left |
47 | 0 | filtered[tuSize2 + 1] = ((samples[tuSize2 + 1] << 1) + topLeft + samples[tuSize2 + 2] + 2) >> 2; |
48 | 0 | for (int i = tuSize2 + 2; i < tuSize2 + tuSize2; i++) |
49 | 0 | filtered[i] = ((samples[i] << 1) + samples[i - 1] + samples[i + 1] + 2) >> 2; |
50 | 0 | filtered[tuSize2 + tuSize2] = leftLast; |
51 | 0 | } Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intraFilter<4>(unsigned char const*, unsigned char*) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intraFilter<8>(unsigned char const*, unsigned char*) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intraFilter<16>(unsigned char const*, unsigned char*) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intraFilter<32>(unsigned char const*, unsigned char*) |
52 | | |
53 | | static void dcPredFilter(const pixel* above, const pixel* left, pixel* dst, intptr_t dststride, int size) |
54 | 0 | { |
55 | | // boundary pixels processing |
56 | 0 | dst[0] = (pixel)((above[0] + left[0] + 2 * dst[0] + 2) >> 2); |
57 | |
|
58 | 0 | for (int x = 1; x < size; x++) |
59 | 0 | dst[x] = (pixel)((above[x] + 3 * dst[x] + 2) >> 2); |
60 | |
|
61 | 0 | dst += dststride; |
62 | 0 | for (int y = 1; y < size; y++) |
63 | 0 | { |
64 | 0 | *dst = (pixel)((left[y] + 3 * *dst + 2) >> 2); |
65 | 0 | dst += dststride; |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | | template<int width> |
70 | | void intra_pred_dc_c(pixel* dst, intptr_t dstStride, const pixel* srcPix, int /*dirMode*/, int bFilter) |
71 | 0 | { |
72 | 0 | int k, l; |
73 | |
|
74 | 0 | int dcVal = width; |
75 | 0 | for (int i = 0; i < width; i++) |
76 | 0 | dcVal += srcPix[1 + i] + srcPix[2 * width + 1 + i]; |
77 | |
|
78 | 0 | dcVal = dcVal / (width + width); |
79 | 0 | for (k = 0; k < width; k++) |
80 | 0 | for (l = 0; l < width; l++) |
81 | 0 | dst[k * dstStride + l] = (pixel)dcVal; |
82 | |
|
83 | 0 | if (bFilter) |
84 | 0 | dcPredFilter(srcPix + 1, srcPix + (2 * width + 1), dst, dstStride, width); |
85 | 0 | } Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intra_pred_dc_c<4>(unsigned char*, long, unsigned char const*, int, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intra_pred_dc_c<8>(unsigned char*, long, unsigned char const*, int, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intra_pred_dc_c<16>(unsigned char*, long, unsigned char const*, int, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intra_pred_dc_c<32>(unsigned char*, long, unsigned char const*, int, int) |
86 | | |
87 | | template<int log2Size> |
88 | | void planar_pred_c(pixel* dst, intptr_t dstStride, const pixel* srcPix, int /*dirMode*/, int /*bFilter*/) |
89 | 0 | { |
90 | 0 | const int blkSize = 1 << log2Size; |
91 | |
|
92 | 0 | const pixel* above = srcPix + 1; |
93 | 0 | const pixel* left = srcPix + (2 * blkSize + 1); |
94 | |
|
95 | 0 | pixel topRight = above[blkSize]; |
96 | 0 | pixel bottomLeft = left[blkSize]; |
97 | 0 | for (int y = 0; y < blkSize; y++) |
98 | 0 | for (int x = 0; x < blkSize; x++) |
99 | 0 | dst[y * dstStride + x] = (pixel) (((blkSize - 1 - x) * left[y] + (blkSize - 1 -y) * above[x] + (x + 1) * topRight + (y + 1) * bottomLeft + blkSize) >> (log2Size + 1)); |
100 | 0 | } Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::planar_pred_c<2>(unsigned char*, long, unsigned char const*, int, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::planar_pred_c<3>(unsigned char*, long, unsigned char const*, int, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::planar_pred_c<4>(unsigned char*, long, unsigned char const*, int, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::planar_pred_c<5>(unsigned char*, long, unsigned char const*, int, int) |
101 | | |
102 | | template<int width> |
103 | | void intra_pred_ang_c(pixel* dst, intptr_t dstStride, const pixel *srcPix0, int dirMode, int bFilter) |
104 | 0 | { |
105 | 0 | int width2 = width << 1; |
106 | | // Flip the neighbours in the horizontal case. |
107 | 0 | int horMode = dirMode < 18; |
108 | 0 | pixel neighbourBuf[129]; |
109 | 0 | const pixel *srcPix = srcPix0; |
110 | |
|
111 | 0 | if (horMode) |
112 | 0 | { |
113 | 0 | neighbourBuf[0] = srcPix[0]; |
114 | 0 | for (int i = 0; i < width << 1; i++) |
115 | 0 | { |
116 | 0 | neighbourBuf[1 + i] = srcPix[width2 + 1 + i]; |
117 | 0 | neighbourBuf[width2 + 1 + i] = srcPix[1 + i]; |
118 | 0 | } |
119 | 0 | srcPix = neighbourBuf; |
120 | 0 | } |
121 | | |
122 | | // Intra prediction angle and inverse angle tables. |
123 | 0 | const int8_t angleTable[17] = { -32, -26, -21, -17, -13, -9, -5, -2, 0, 2, 5, 9, 13, 17, 21, 26, 32 }; |
124 | 0 | const int16_t invAngleTable[8] = { 4096, 1638, 910, 630, 482, 390, 315, 256 }; |
125 | | |
126 | | // Get the prediction angle. |
127 | 0 | int angleOffset = horMode ? 10 - dirMode : dirMode - 26; |
128 | 0 | int angle = angleTable[8 + angleOffset]; |
129 | | |
130 | | // Vertical Prediction. |
131 | 0 | if (!angle) |
132 | 0 | { |
133 | 0 | for (int y = 0; y < width; y++) |
134 | 0 | for (int x = 0; x < width; x++) |
135 | 0 | dst[y * dstStride + x] = srcPix[1 + x]; |
136 | |
|
137 | 0 | if (bFilter) |
138 | 0 | { |
139 | 0 | int topLeft = srcPix[0], top = srcPix[1]; |
140 | 0 | for (int y = 0; y < width; y++) |
141 | 0 | dst[y * dstStride] = x265_clip((int16_t)(top + ((srcPix[width2 + 1 + y] - topLeft) >> 1))); |
142 | 0 | } |
143 | 0 | } |
144 | 0 | else // Angular prediction. |
145 | 0 | { |
146 | | // Get the reference pixels. The reference base is the first pixel to the top (neighbourBuf[1]). |
147 | 0 | pixel refBuf[64]; |
148 | 0 | const pixel *ref; |
149 | | |
150 | | // Use the projected left neighbours and the top neighbours. |
151 | 0 | if (angle < 0) |
152 | 0 | { |
153 | | // Number of neighbours projected. |
154 | 0 | int nbProjected = -((width * angle) >> 5) - 1; |
155 | 0 | pixel *ref_pix = refBuf + nbProjected + 1; |
156 | | |
157 | | // Project the neighbours. |
158 | 0 | int invAngle = invAngleTable[- angleOffset - 1]; |
159 | 0 | int invAngleSum = 128; |
160 | 0 | for (int i = 0; i < nbProjected; i++) |
161 | 0 | { |
162 | 0 | invAngleSum += invAngle; |
163 | 0 | ref_pix[- 2 - i] = srcPix[width2 + (invAngleSum >> 8)]; |
164 | 0 | } |
165 | | |
166 | | // Copy the top-left and top pixels. |
167 | 0 | for (int i = 0; i < width + 1; i++) |
168 | 0 | ref_pix[-1 + i] = srcPix[i]; |
169 | 0 | ref = ref_pix; |
170 | 0 | } |
171 | 0 | else // Use the top and top-right neighbours. |
172 | 0 | ref = srcPix + 1; |
173 | | |
174 | | // Pass every row. |
175 | 0 | int angleSum = 0; |
176 | 0 | for (int y = 0; y < width; y++) |
177 | 0 | { |
178 | 0 | angleSum += angle; |
179 | 0 | int offset = angleSum >> 5; |
180 | 0 | int fraction = angleSum & 31; |
181 | |
|
182 | 0 | if (fraction) // Interpolate |
183 | 0 | for (int x = 0; x < width; x++) |
184 | 0 | dst[y * dstStride + x] = (pixel)(((32 - fraction) * ref[offset + x] + fraction * ref[offset + x + 1] + 16) >> 5); |
185 | 0 | else // Copy. |
186 | 0 | for (int x = 0; x < width; x++) |
187 | 0 | dst[y * dstStride + x] = ref[offset + x]; |
188 | 0 | } |
189 | 0 | } |
190 | | |
191 | | // Flip for horizontal. |
192 | 0 | if (horMode) |
193 | 0 | { |
194 | 0 | for (int y = 0; y < width - 1; y++) |
195 | 0 | { |
196 | 0 | for (int x = y + 1; x < width; x++) |
197 | 0 | { |
198 | 0 | pixel tmp = dst[y * dstStride + x]; |
199 | 0 | dst[y * dstStride + x] = dst[x * dstStride + y]; |
200 | 0 | dst[x * dstStride + y] = tmp; |
201 | 0 | } |
202 | 0 | } |
203 | 0 | } |
204 | 0 | } Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intra_pred_ang_c<4>(unsigned char*, long, unsigned char const*, int, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intra_pred_ang_c<8>(unsigned char*, long, unsigned char const*, int, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intra_pred_ang_c<16>(unsigned char*, long, unsigned char const*, int, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::intra_pred_ang_c<32>(unsigned char*, long, unsigned char const*, int, int) |
205 | | |
206 | | template<int log2Size> |
207 | | void all_angs_pred_c(pixel *dest, pixel *refPix, pixel *filtPix, int bLuma) |
208 | 0 | { |
209 | 0 | const int size = 1 << log2Size; |
210 | 0 | for (int mode = 2; mode <= 34; mode++) |
211 | 0 | { |
212 | 0 | pixel *srcPix = (g_intraFilterFlags[mode] & size ? filtPix : refPix); |
213 | 0 | pixel *out = dest + ((mode - 2) << (log2Size * 2)); |
214 | |
|
215 | 0 | intra_pred_ang_c<size>(out, size, srcPix, mode, bLuma); |
216 | | |
217 | | // Optimize code don't flip buffer |
218 | 0 | bool modeHor = (mode < 18); |
219 | | |
220 | | // transpose the block if this is a horizontal mode |
221 | 0 | if (modeHor) |
222 | 0 | { |
223 | 0 | for (int k = 0; k < size - 1; k++) |
224 | 0 | { |
225 | 0 | for (int l = k + 1; l < size; l++) |
226 | 0 | { |
227 | 0 | pixel tmp = out[k * size + l]; |
228 | 0 | out[k * size + l] = out[l * size + k]; |
229 | 0 | out[l * size + k] = tmp; |
230 | 0 | } |
231 | 0 | } |
232 | 0 | } |
233 | 0 | } |
234 | 0 | } Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::all_angs_pred_c<2>(unsigned char*, unsigned char*, unsigned char*, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::all_angs_pred_c<3>(unsigned char*, unsigned char*, unsigned char*, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::all_angs_pred_c<4>(unsigned char*, unsigned char*, unsigned char*, int) Unexecuted instantiation: intrapred.cpp:void (anonymous namespace)::all_angs_pred_c<5>(unsigned char*, unsigned char*, unsigned char*, int) |
235 | | } |
236 | | |
237 | | namespace X265_NS { |
238 | | // x265 private namespace |
239 | | |
240 | | void setupIntraPrimitives_c(EncoderPrimitives& p) |
241 | 0 | { |
242 | 0 | p.cu[BLOCK_4x4].intra_filter = intraFilter<4>; |
243 | 0 | p.cu[BLOCK_8x8].intra_filter = intraFilter<8>; |
244 | 0 | p.cu[BLOCK_16x16].intra_filter = intraFilter<16>; |
245 | 0 | p.cu[BLOCK_32x32].intra_filter = intraFilter<32>; |
246 | |
|
247 | 0 | p.cu[BLOCK_4x4].intra_pred[PLANAR_IDX] = planar_pred_c<2>; |
248 | 0 | p.cu[BLOCK_8x8].intra_pred[PLANAR_IDX] = planar_pred_c<3>; |
249 | 0 | p.cu[BLOCK_16x16].intra_pred[PLANAR_IDX] = planar_pred_c<4>; |
250 | 0 | p.cu[BLOCK_32x32].intra_pred[PLANAR_IDX] = planar_pred_c<5>; |
251 | |
|
252 | 0 | p.cu[BLOCK_4x4].intra_pred[DC_IDX] = intra_pred_dc_c<4>; |
253 | 0 | p.cu[BLOCK_8x8].intra_pred[DC_IDX] = intra_pred_dc_c<8>; |
254 | 0 | p.cu[BLOCK_16x16].intra_pred[DC_IDX] = intra_pred_dc_c<16>; |
255 | 0 | p.cu[BLOCK_32x32].intra_pred[DC_IDX] = intra_pred_dc_c<32>; |
256 | |
|
257 | 0 | for (int i = 2; i < NUM_INTRA_MODE; i++) |
258 | 0 | { |
259 | 0 | p.cu[BLOCK_4x4].intra_pred[i] = intra_pred_ang_c<4>; |
260 | 0 | p.cu[BLOCK_8x8].intra_pred[i] = intra_pred_ang_c<8>; |
261 | 0 | p.cu[BLOCK_16x16].intra_pred[i] = intra_pred_ang_c<16>; |
262 | 0 | p.cu[BLOCK_32x32].intra_pred[i] = intra_pred_ang_c<32>; |
263 | 0 | } |
264 | |
|
265 | 0 | p.cu[BLOCK_4x4].intra_pred_allangs = all_angs_pred_c<2>; |
266 | 0 | p.cu[BLOCK_8x8].intra_pred_allangs = all_angs_pred_c<3>; |
267 | 0 | p.cu[BLOCK_16x16].intra_pred_allangs = all_angs_pred_c<4>; |
268 | 0 | p.cu[BLOCK_32x32].intra_pred_allangs = all_angs_pred_c<5>; |
269 | 0 | } |
270 | | } |