/src/libwebp/src/dsp/enc.c
Line | Count | Source |
1 | | // Copyright 2011 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style license |
4 | | // that can be found in the COPYING file in the root of the source |
5 | | // tree. An additional intellectual property rights grant can be found |
6 | | // in the file PATENTS. All contributing project authors may |
7 | | // be found in the AUTHORS file in the root of the source tree. |
8 | | // ----------------------------------------------------------------------------- |
9 | | // |
10 | | // Speed-critical encoding functions. |
11 | | // |
12 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #include <assert.h> |
15 | | #include <stdlib.h> // for abs() |
16 | | #include <string.h> |
17 | | |
18 | | #include "src/dsp/cpu.h" |
19 | | #include "src/dsp/dsp.h" |
20 | | #include "src/enc/vp8i_enc.h" |
21 | | #include "src/utils/utils.h" |
22 | | #include "src/webp/types.h" |
23 | | |
24 | 0 | static WEBP_INLINE uint8_t clip_8b(int v) { |
25 | 0 | return (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255; |
26 | 0 | } |
27 | | |
28 | | #if !WEBP_NEON_OMIT_C_CODE |
29 | 0 | static WEBP_INLINE int clip_max(int v, int max) { return (v > max) ? max : v; } |
30 | | #endif // !WEBP_NEON_OMIT_C_CODE |
31 | | |
32 | | //------------------------------------------------------------------------------ |
33 | | // Compute susceptibility based on DCT-coeff histograms: |
34 | | // the higher, the "easier" the macroblock is to compress. |
35 | | |
36 | | const int VP8DspScan[16 + 4 + 4] = { |
37 | | // Luma |
38 | | 0 + 0 * BPS, 4 + 0 * BPS, 8 + 0 * BPS, 12 + 0 * BPS, |
39 | | 0 + 4 * BPS, 4 + 4 * BPS, 8 + 4 * BPS, 12 + 4 * BPS, |
40 | | 0 + 8 * BPS, 4 + 8 * BPS, 8 + 8 * BPS, 12 + 8 * BPS, |
41 | | 0 + 12 * BPS, 4 + 12 * BPS, 8 + 12 * BPS, 12 + 12 * BPS, |
42 | | |
43 | | 0 + 0 * BPS, 4 + 0 * BPS, 0 + 4 * BPS, 4 + 4 * BPS, // U |
44 | | 8 + 0 * BPS, 12 + 0 * BPS, 8 + 4 * BPS, 12 + 4 * BPS // V |
45 | | }; |
46 | | |
47 | | // general-purpose util function |
48 | | void VP8SetHistogramData(const int distribution[MAX_COEFF_THRESH + 1], |
49 | 0 | VP8Histogram* const histo) { |
50 | 0 | int max_value = 0, last_non_zero = 1; |
51 | 0 | int k; |
52 | 0 | for (k = 0; k <= MAX_COEFF_THRESH; ++k) { |
53 | 0 | const int value = distribution[k]; |
54 | 0 | if (value > 0) { |
55 | 0 | if (value > max_value) max_value = value; |
56 | 0 | last_non_zero = k; |
57 | 0 | } |
58 | 0 | } |
59 | 0 | histo->max_value = max_value; |
60 | 0 | histo->last_non_zero = last_non_zero; |
61 | 0 | } |
62 | | |
63 | | #if !WEBP_NEON_OMIT_C_CODE |
64 | | static void CollectHistogram_C(const uint8_t* WEBP_RESTRICT ref, |
65 | | const uint8_t* WEBP_RESTRICT pred, |
66 | | int start_block, int end_block, |
67 | 0 | VP8Histogram* WEBP_RESTRICT const histo) { |
68 | 0 | int j; |
69 | 0 | int distribution[MAX_COEFF_THRESH + 1] = {0}; |
70 | 0 | for (j = start_block; j < end_block; ++j) { |
71 | 0 | int k; |
72 | 0 | int16_t out[16]; |
73 | |
|
74 | 0 | VP8FTransform(ref + VP8DspScan[j], pred + VP8DspScan[j], out); |
75 | | |
76 | | // Convert coefficients to bin. |
77 | 0 | for (k = 0; k < 16; ++k) { |
78 | 0 | const int v = abs(out[k]) >> 3; |
79 | 0 | const int clipped_value = clip_max(v, MAX_COEFF_THRESH); |
80 | 0 | ++distribution[clipped_value]; |
81 | 0 | } |
82 | 0 | } |
83 | 0 | VP8SetHistogramData(distribution, histo); |
84 | 0 | } |
85 | | #endif // !WEBP_NEON_OMIT_C_CODE |
86 | | |
87 | | //------------------------------------------------------------------------------ |
88 | | // run-time tables (~4k) |
89 | | |
90 | | static uint8_t clip1[255 + 510 + 1]; // clips [-255,510] to [0,255] |
91 | | |
92 | | // We declare this variable 'volatile' to prevent instruction reordering |
93 | | // and make sure it's set to true _last_ (so as to be thread-safe) |
94 | | static volatile int tables_ok = 0; |
95 | | |
96 | 0 | static WEBP_TSAN_IGNORE_FUNCTION void InitTables(void) { |
97 | 0 | if (!tables_ok) { |
98 | 0 | int i; |
99 | 0 | for (i = -255; i <= 255 + 255; ++i) { |
100 | 0 | clip1[255 + i] = clip_8b(i); |
101 | 0 | } |
102 | 0 | tables_ok = 1; |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | | //------------------------------------------------------------------------------ |
107 | | // Transforms (Paragraph 14.4) |
108 | | |
109 | | #if !WEBP_NEON_OMIT_C_CODE |
110 | | |
111 | | #define STORE(x, y, v) \ |
112 | 0 | dst[(x) + (y) * BPS] = clip_8b(ref[(x) + (y) * BPS] + ((v) >> 3)) |
113 | | |
114 | | static WEBP_INLINE void ITransformOne(const uint8_t* WEBP_RESTRICT ref, |
115 | | const int16_t* WEBP_RESTRICT in, |
116 | 0 | uint8_t* WEBP_RESTRICT dst) { |
117 | 0 | int C[4 * 4], *tmp; |
118 | 0 | int i; |
119 | 0 | tmp = C; |
120 | 0 | for (i = 0; i < 4; ++i) { // vertical pass |
121 | 0 | const int a = in[0] + in[8]; |
122 | 0 | const int b = in[0] - in[8]; |
123 | 0 | const int c = |
124 | 0 | WEBP_TRANSFORM_AC3_MUL2(in[4]) - WEBP_TRANSFORM_AC3_MUL1(in[12]); |
125 | 0 | const int d = |
126 | 0 | WEBP_TRANSFORM_AC3_MUL1(in[4]) + WEBP_TRANSFORM_AC3_MUL2(in[12]); |
127 | 0 | tmp[0] = a + d; |
128 | 0 | tmp[1] = b + c; |
129 | 0 | tmp[2] = b - c; |
130 | 0 | tmp[3] = a - d; |
131 | 0 | tmp += 4; |
132 | 0 | in++; |
133 | 0 | } |
134 | |
|
135 | 0 | tmp = C; |
136 | 0 | for (i = 0; i < 4; ++i) { // horizontal pass |
137 | 0 | const int dc = tmp[0] + 4; |
138 | 0 | const int a = dc + tmp[8]; |
139 | 0 | const int b = dc - tmp[8]; |
140 | 0 | const int c = |
141 | 0 | WEBP_TRANSFORM_AC3_MUL2(tmp[4]) - WEBP_TRANSFORM_AC3_MUL1(tmp[12]); |
142 | 0 | const int d = |
143 | 0 | WEBP_TRANSFORM_AC3_MUL1(tmp[4]) + WEBP_TRANSFORM_AC3_MUL2(tmp[12]); |
144 | 0 | STORE(0, i, a + d); |
145 | 0 | STORE(1, i, b + c); |
146 | 0 | STORE(2, i, b - c); |
147 | 0 | STORE(3, i, a - d); |
148 | 0 | tmp++; |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | | static void ITransform_C(const uint8_t* WEBP_RESTRICT ref, |
153 | | const int16_t* WEBP_RESTRICT in, |
154 | 0 | uint8_t* WEBP_RESTRICT dst, int do_two) { |
155 | 0 | ITransformOne(ref, in, dst); |
156 | 0 | if (do_two) { |
157 | 0 | ITransformOne(ref + 4, in + 16, dst + 4); |
158 | 0 | } |
159 | 0 | } |
160 | | |
161 | | static void FTransform_C(const uint8_t* WEBP_RESTRICT src, |
162 | | const uint8_t* WEBP_RESTRICT ref, |
163 | 0 | int16_t* WEBP_RESTRICT out) { |
164 | 0 | int i; |
165 | 0 | int tmp[16]; |
166 | 0 | for (i = 0; i < 4; ++i, src += BPS, ref += BPS) { |
167 | 0 | const int d0 = src[0] - ref[0]; // 9bit dynamic range ([-255,255]) |
168 | 0 | const int d1 = src[1] - ref[1]; |
169 | 0 | const int d2 = src[2] - ref[2]; |
170 | 0 | const int d3 = src[3] - ref[3]; |
171 | 0 | const int a0 = (d0 + d3); // 10b [-510,510] |
172 | 0 | const int a1 = (d1 + d2); |
173 | 0 | const int a2 = (d1 - d2); |
174 | 0 | const int a3 = (d0 - d3); |
175 | 0 | tmp[0 + i * 4] = (a0 + a1) * 8; // 14b [-8160,8160] |
176 | 0 | tmp[1 + i * 4] = (a2 * 2217 + a3 * 5352 + 1812) >> 9; // [-7536,7542] |
177 | 0 | tmp[2 + i * 4] = (a0 - a1) * 8; |
178 | 0 | tmp[3 + i * 4] = (a3 * 2217 - a2 * 5352 + 937) >> 9; |
179 | 0 | } |
180 | 0 | for (i = 0; i < 4; ++i) { |
181 | 0 | const int a0 = (tmp[0 + i] + tmp[12 + i]); // 15b |
182 | 0 | const int a1 = (tmp[4 + i] + tmp[8 + i]); |
183 | 0 | const int a2 = (tmp[4 + i] - tmp[8 + i]); |
184 | 0 | const int a3 = (tmp[0 + i] - tmp[12 + i]); |
185 | 0 | out[0 + i] = (a0 + a1 + 7) >> 4; // 12b |
186 | 0 | out[4 + i] = ((a2 * 2217 + a3 * 5352 + 12000) >> 16) + (a3 != 0); |
187 | 0 | out[8 + i] = (a0 - a1 + 7) >> 4; |
188 | 0 | out[12 + i] = ((a3 * 2217 - a2 * 5352 + 51000) >> 16); |
189 | 0 | } |
190 | 0 | } |
191 | | #endif // !WEBP_NEON_OMIT_C_CODE |
192 | | |
193 | | static void FTransform2_C(const uint8_t* WEBP_RESTRICT src, |
194 | | const uint8_t* WEBP_RESTRICT ref, |
195 | 0 | int16_t* WEBP_RESTRICT out) { |
196 | 0 | VP8FTransform(src, ref, out); |
197 | 0 | VP8FTransform(src + 4, ref + 4, out + 16); |
198 | 0 | } |
199 | | |
200 | | #if !WEBP_NEON_OMIT_C_CODE |
201 | | static void FTransformWHT_C(const int16_t* WEBP_RESTRICT in, |
202 | 0 | int16_t* WEBP_RESTRICT out) { |
203 | | // input is 12b signed |
204 | 0 | int32_t tmp[16]; |
205 | 0 | int i; |
206 | 0 | for (i = 0; i < 4; ++i, in += 64) { |
207 | 0 | const int a0 = (in[0 * 16] + in[2 * 16]); // 13b |
208 | 0 | const int a1 = (in[1 * 16] + in[3 * 16]); |
209 | 0 | const int a2 = (in[1 * 16] - in[3 * 16]); |
210 | 0 | const int a3 = (in[0 * 16] - in[2 * 16]); |
211 | 0 | tmp[0 + i * 4] = a0 + a1; // 14b |
212 | 0 | tmp[1 + i * 4] = a3 + a2; |
213 | 0 | tmp[2 + i * 4] = a3 - a2; |
214 | 0 | tmp[3 + i * 4] = a0 - a1; |
215 | 0 | } |
216 | 0 | for (i = 0; i < 4; ++i) { |
217 | 0 | const int a0 = (tmp[0 + i] + tmp[8 + i]); // 15b |
218 | 0 | const int a1 = (tmp[4 + i] + tmp[12 + i]); |
219 | 0 | const int a2 = (tmp[4 + i] - tmp[12 + i]); |
220 | 0 | const int a3 = (tmp[0 + i] - tmp[8 + i]); |
221 | 0 | const int b0 = a0 + a1; // 16b |
222 | 0 | const int b1 = a3 + a2; |
223 | 0 | const int b2 = a3 - a2; |
224 | 0 | const int b3 = a0 - a1; |
225 | 0 | out[0 + i] = b0 >> 1; // 15b |
226 | 0 | out[4 + i] = b1 >> 1; |
227 | 0 | out[8 + i] = b2 >> 1; |
228 | 0 | out[12 + i] = b3 >> 1; |
229 | 0 | } |
230 | 0 | } |
231 | | #endif // !WEBP_NEON_OMIT_C_CODE |
232 | | |
233 | | #undef STORE |
234 | | |
235 | | //------------------------------------------------------------------------------ |
236 | | // Intra predictions |
237 | | |
238 | 0 | static WEBP_INLINE void Fill(uint8_t* dst, int value, int size) { |
239 | 0 | int j; |
240 | 0 | for (j = 0; j < size; ++j) { |
241 | 0 | memset(dst + j * BPS, value, size); |
242 | 0 | } |
243 | 0 | } |
244 | | |
245 | | static WEBP_INLINE void VerticalPred(uint8_t* WEBP_RESTRICT dst, |
246 | | const uint8_t* WEBP_RESTRICT top, |
247 | 0 | int size) { |
248 | 0 | int j; |
249 | 0 | if (top != NULL) { |
250 | 0 | for (j = 0; j < size; ++j) memcpy(dst + j * BPS, top, size); |
251 | 0 | } else { |
252 | 0 | Fill(dst, 127, size); |
253 | 0 | } |
254 | 0 | } |
255 | | |
256 | | static WEBP_INLINE void HorizontalPred(uint8_t* WEBP_RESTRICT dst, |
257 | | const uint8_t* WEBP_RESTRICT left, |
258 | 0 | int size) { |
259 | 0 | if (left != NULL) { |
260 | 0 | int j; |
261 | 0 | for (j = 0; j < size; ++j) { |
262 | 0 | memset(dst + j * BPS, left[j], size); |
263 | 0 | } |
264 | 0 | } else { |
265 | 0 | Fill(dst, 129, size); |
266 | 0 | } |
267 | 0 | } |
268 | | |
269 | | static WEBP_INLINE void TrueMotion(uint8_t* WEBP_RESTRICT dst, |
270 | | const uint8_t* WEBP_RESTRICT left, |
271 | 0 | const uint8_t* WEBP_RESTRICT top, int size) { |
272 | 0 | int y; |
273 | 0 | if (left != NULL) { |
274 | 0 | if (top != NULL) { |
275 | 0 | const uint8_t* const clip = clip1 + 255 - left[-1]; |
276 | 0 | for (y = 0; y < size; ++y) { |
277 | 0 | const uint8_t* const clip_table = clip + left[y]; |
278 | 0 | int x; |
279 | 0 | for (x = 0; x < size; ++x) { |
280 | 0 | dst[x] = clip_table[top[x]]; |
281 | 0 | } |
282 | 0 | dst += BPS; |
283 | 0 | } |
284 | 0 | } else { |
285 | 0 | HorizontalPred(dst, left, size); |
286 | 0 | } |
287 | 0 | } else { |
288 | | // true motion without left samples (hence: with default 129 value) |
289 | | // is equivalent to VE prediction where you just copy the top samples. |
290 | | // Note that if top samples are not available, the default value is |
291 | | // then 129, and not 127 as in the VerticalPred case. |
292 | 0 | if (top != NULL) { |
293 | 0 | VerticalPred(dst, top, size); |
294 | 0 | } else { |
295 | 0 | Fill(dst, 129, size); |
296 | 0 | } |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | | static WEBP_INLINE void DCMode(uint8_t* WEBP_RESTRICT dst, |
301 | | const uint8_t* WEBP_RESTRICT left, |
302 | | const uint8_t* WEBP_RESTRICT top, int size, |
303 | 0 | int round, int shift) { |
304 | 0 | int DC = 0; |
305 | 0 | int j; |
306 | 0 | if (top != NULL) { |
307 | 0 | for (j = 0; j < size; ++j) DC += top[j]; |
308 | 0 | if (left != NULL) { // top and left present |
309 | 0 | for (j = 0; j < size; ++j) DC += left[j]; |
310 | 0 | } else { // top, but no left |
311 | 0 | DC += DC; |
312 | 0 | } |
313 | 0 | DC = (DC + round) >> shift; |
314 | 0 | } else if (left != NULL) { // left but no top |
315 | 0 | for (j = 0; j < size; ++j) DC += left[j]; |
316 | 0 | DC += DC; |
317 | 0 | DC = (DC + round) >> shift; |
318 | 0 | } else { // no top, no left, nothing. |
319 | 0 | DC = 0x80; |
320 | 0 | } |
321 | 0 | Fill(dst, DC, size); |
322 | 0 | } |
323 | | |
324 | | //------------------------------------------------------------------------------ |
325 | | // Chroma 8x8 prediction (paragraph 12.2) |
326 | | |
327 | | static void IntraChromaPreds_C(uint8_t* WEBP_RESTRICT dst, |
328 | | const uint8_t* WEBP_RESTRICT left, |
329 | 0 | const uint8_t* WEBP_RESTRICT top) { |
330 | | // U block |
331 | 0 | DCMode(C8DC8 + dst, left, top, 8, 8, 4); |
332 | 0 | VerticalPred(C8VE8 + dst, top, 8); |
333 | 0 | HorizontalPred(C8HE8 + dst, left, 8); |
334 | 0 | TrueMotion(C8TM8 + dst, left, top, 8); |
335 | | // V block |
336 | 0 | dst += 8; |
337 | 0 | if (top != NULL) top += 8; |
338 | 0 | if (left != NULL) left += 16; |
339 | 0 | DCMode(C8DC8 + dst, left, top, 8, 8, 4); |
340 | 0 | VerticalPred(C8VE8 + dst, top, 8); |
341 | 0 | HorizontalPred(C8HE8 + dst, left, 8); |
342 | 0 | TrueMotion(C8TM8 + dst, left, top, 8); |
343 | 0 | } |
344 | | |
345 | | //------------------------------------------------------------------------------ |
346 | | // luma 16x16 prediction (paragraph 12.3) |
347 | | |
348 | | #if !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64 |
349 | | static void Intra16Preds_C(uint8_t* WEBP_RESTRICT dst, |
350 | | const uint8_t* WEBP_RESTRICT left, |
351 | 0 | const uint8_t* WEBP_RESTRICT top) { |
352 | 0 | DCMode(I16DC16 + dst, left, top, 16, 16, 5); |
353 | 0 | VerticalPred(I16VE16 + dst, top, 16); |
354 | 0 | HorizontalPred(I16HE16 + dst, left, 16); |
355 | 0 | TrueMotion(I16TM16 + dst, left, top, 16); |
356 | 0 | } |
357 | | #endif // !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64 |
358 | | |
359 | | //------------------------------------------------------------------------------ |
360 | | // luma 4x4 prediction |
361 | | |
362 | | #if !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64 || BPS != 32 |
363 | | |
364 | 0 | #define DST(x, y) dst[(x) + (y) * BPS] |
365 | 0 | #define AVG3(a, b, c) ((uint8_t)(((a) + 2 * (b) + (c) + 2) >> 2)) |
366 | 0 | #define AVG2(a, b) (((a) + (b) + 1) >> 1) |
367 | | |
368 | | // vertical |
369 | 0 | static void VE4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) { |
370 | 0 | const uint8_t vals[4] = { |
371 | 0 | AVG3(top[-1], top[0], top[1]), |
372 | 0 | AVG3(top[0], top[1], top[2]), |
373 | 0 | AVG3(top[1], top[2], top[3]), |
374 | 0 | AVG3(top[2], top[3], top[4]), |
375 | 0 | }; |
376 | 0 | int i; |
377 | 0 | for (i = 0; i < 4; ++i) { |
378 | 0 | memcpy(dst + i * BPS, vals, 4); |
379 | 0 | } |
380 | 0 | } |
381 | | |
382 | | // horizontal |
383 | 0 | static void HE4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) { |
384 | 0 | const int X = top[-1]; |
385 | 0 | const int I = top[-2]; |
386 | 0 | const int J = top[-3]; |
387 | 0 | const int K = top[-4]; |
388 | 0 | const int L = top[-5]; |
389 | 0 | WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(X, I, J)); |
390 | 0 | WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(I, J, K)); |
391 | 0 | WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(J, K, L)); |
392 | 0 | WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(K, L, L)); |
393 | 0 | } |
394 | | |
395 | 0 | static void DC4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) { |
396 | 0 | uint32_t dc = 4; |
397 | 0 | int i; |
398 | 0 | for (i = 0; i < 4; ++i) dc += top[i] + top[-5 + i]; |
399 | 0 | Fill(dst, dc >> 3, 4); |
400 | 0 | } |
401 | | |
402 | 0 | static void RD4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) { |
403 | 0 | const int X = top[-1]; |
404 | 0 | const int I = top[-2]; |
405 | 0 | const int J = top[-3]; |
406 | 0 | const int K = top[-4]; |
407 | 0 | const int L = top[-5]; |
408 | 0 | const int A = top[0]; |
409 | 0 | const int B = top[1]; |
410 | 0 | const int C = top[2]; |
411 | 0 | const int D = top[3]; |
412 | 0 | DST(0, 3) = AVG3(J, K, L); |
413 | 0 | DST(0, 2) = DST(1, 3) = AVG3(I, J, K); |
414 | 0 | DST(0, 1) = DST(1, 2) = DST(2, 3) = AVG3(X, I, J); |
415 | 0 | DST(0, 0) = DST(1, 1) = DST(2, 2) = DST(3, 3) = AVG3(A, X, I); |
416 | 0 | DST(1, 0) = DST(2, 1) = DST(3, 2) = AVG3(B, A, X); |
417 | 0 | DST(2, 0) = DST(3, 1) = AVG3(C, B, A); |
418 | 0 | DST(3, 0) = AVG3(D, C, B); |
419 | 0 | } |
420 | | |
421 | 0 | static void LD4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) { |
422 | 0 | const int A = top[0]; |
423 | 0 | const int B = top[1]; |
424 | 0 | const int C = top[2]; |
425 | 0 | const int D = top[3]; |
426 | 0 | const int E = top[4]; |
427 | 0 | const int F = top[5]; |
428 | 0 | const int G = top[6]; |
429 | 0 | const int H = top[7]; |
430 | 0 | DST(0, 0) = AVG3(A, B, C); |
431 | 0 | DST(1, 0) = DST(0, 1) = AVG3(B, C, D); |
432 | 0 | DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E); |
433 | 0 | DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F); |
434 | 0 | DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G); |
435 | 0 | DST(3, 2) = DST(2, 3) = AVG3(F, G, H); |
436 | 0 | DST(3, 3) = AVG3(G, H, H); |
437 | 0 | } |
438 | | |
439 | 0 | static void VR4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) { |
440 | 0 | const int X = top[-1]; |
441 | 0 | const int I = top[-2]; |
442 | 0 | const int J = top[-3]; |
443 | 0 | const int K = top[-4]; |
444 | 0 | const int A = top[0]; |
445 | 0 | const int B = top[1]; |
446 | 0 | const int C = top[2]; |
447 | 0 | const int D = top[3]; |
448 | 0 | DST(0, 0) = DST(1, 2) = AVG2(X, A); |
449 | 0 | DST(1, 0) = DST(2, 2) = AVG2(A, B); |
450 | 0 | DST(2, 0) = DST(3, 2) = AVG2(B, C); |
451 | 0 | DST(3, 0) = AVG2(C, D); |
452 | |
|
453 | 0 | DST(0, 3) = AVG3(K, J, I); |
454 | 0 | DST(0, 2) = AVG3(J, I, X); |
455 | 0 | DST(0, 1) = DST(1, 3) = AVG3(I, X, A); |
456 | 0 | DST(1, 1) = DST(2, 3) = AVG3(X, A, B); |
457 | 0 | DST(2, 1) = DST(3, 3) = AVG3(A, B, C); |
458 | 0 | DST(3, 1) = AVG3(B, C, D); |
459 | 0 | } |
460 | | |
461 | 0 | static void VL4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) { |
462 | 0 | const int A = top[0]; |
463 | 0 | const int B = top[1]; |
464 | 0 | const int C = top[2]; |
465 | 0 | const int D = top[3]; |
466 | 0 | const int E = top[4]; |
467 | 0 | const int F = top[5]; |
468 | 0 | const int G = top[6]; |
469 | 0 | const int H = top[7]; |
470 | 0 | DST(0, 0) = AVG2(A, B); |
471 | 0 | DST(1, 0) = DST(0, 2) = AVG2(B, C); |
472 | 0 | DST(2, 0) = DST(1, 2) = AVG2(C, D); |
473 | 0 | DST(3, 0) = DST(2, 2) = AVG2(D, E); |
474 | |
|
475 | 0 | DST(0, 1) = AVG3(A, B, C); |
476 | 0 | DST(1, 1) = DST(0, 3) = AVG3(B, C, D); |
477 | 0 | DST(2, 1) = DST(1, 3) = AVG3(C, D, E); |
478 | 0 | DST(3, 1) = DST(2, 3) = AVG3(D, E, F); |
479 | 0 | DST(3, 2) = AVG3(E, F, G); |
480 | 0 | DST(3, 3) = AVG3(F, G, H); |
481 | 0 | } |
482 | | |
483 | 0 | static void HU4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) { |
484 | 0 | const int I = top[-2]; |
485 | 0 | const int J = top[-3]; |
486 | 0 | const int K = top[-4]; |
487 | 0 | const int L = top[-5]; |
488 | 0 | DST(0, 0) = AVG2(I, J); |
489 | 0 | DST(2, 0) = DST(0, 1) = AVG2(J, K); |
490 | 0 | DST(2, 1) = DST(0, 2) = AVG2(K, L); |
491 | 0 | DST(1, 0) = AVG3(I, J, K); |
492 | 0 | DST(3, 0) = DST(1, 1) = AVG3(J, K, L); |
493 | 0 | DST(3, 1) = DST(1, 2) = AVG3(K, L, L); |
494 | 0 | DST(3, 2) = DST(2, 2) = DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L; |
495 | 0 | } |
496 | | |
497 | 0 | static void HD4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) { |
498 | 0 | const int X = top[-1]; |
499 | 0 | const int I = top[-2]; |
500 | 0 | const int J = top[-3]; |
501 | 0 | const int K = top[-4]; |
502 | 0 | const int L = top[-5]; |
503 | 0 | const int A = top[0]; |
504 | 0 | const int B = top[1]; |
505 | 0 | const int C = top[2]; |
506 | |
|
507 | 0 | DST(0, 0) = DST(2, 1) = AVG2(I, X); |
508 | 0 | DST(0, 1) = DST(2, 2) = AVG2(J, I); |
509 | 0 | DST(0, 2) = DST(2, 3) = AVG2(K, J); |
510 | 0 | DST(0, 3) = AVG2(L, K); |
511 | |
|
512 | 0 | DST(3, 0) = AVG3(A, B, C); |
513 | 0 | DST(2, 0) = AVG3(X, A, B); |
514 | 0 | DST(1, 0) = DST(3, 1) = AVG3(I, X, A); |
515 | 0 | DST(1, 1) = DST(3, 2) = AVG3(J, I, X); |
516 | 0 | DST(1, 2) = DST(3, 3) = AVG3(K, J, I); |
517 | 0 | DST(1, 3) = AVG3(L, K, J); |
518 | 0 | } |
519 | | |
520 | 0 | static void TM4(uint8_t* WEBP_RESTRICT dst, const uint8_t* WEBP_RESTRICT top) { |
521 | 0 | int x, y; |
522 | 0 | const uint8_t* const clip = clip1 + 255 - top[-1]; |
523 | 0 | for (y = 0; y < 4; ++y) { |
524 | 0 | const uint8_t* const clip_table = clip + top[-2 - y]; |
525 | 0 | for (x = 0; x < 4; ++x) { |
526 | 0 | dst[x] = clip_table[top[x]]; |
527 | 0 | } |
528 | 0 | dst += BPS; |
529 | 0 | } |
530 | 0 | } |
531 | | |
532 | | #undef DST |
533 | | #undef AVG3 |
534 | | #undef AVG2 |
535 | | |
536 | | // Left samples are top[-5 .. -2], top_left is top[-1], top are |
537 | | // located at top[0..3], and top right is top[4..7] |
538 | | static void Intra4Preds_C(uint8_t* WEBP_RESTRICT dst, |
539 | 0 | const uint8_t* WEBP_RESTRICT top) { |
540 | 0 | DC4(I4DC4 + dst, top); |
541 | 0 | TM4(I4TM4 + dst, top); |
542 | 0 | VE4(I4VE4 + dst, top); |
543 | 0 | HE4(I4HE4 + dst, top); |
544 | 0 | RD4(I4RD4 + dst, top); |
545 | 0 | VR4(I4VR4 + dst, top); |
546 | 0 | LD4(I4LD4 + dst, top); |
547 | 0 | VL4(I4VL4 + dst, top); |
548 | 0 | HD4(I4HD4 + dst, top); |
549 | 0 | HU4(I4HU4 + dst, top); |
550 | 0 | } |
551 | | |
552 | | #endif // !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64 || BPS != 32 |
553 | | |
554 | | //------------------------------------------------------------------------------ |
555 | | // Metric |
556 | | |
557 | | #if !WEBP_NEON_OMIT_C_CODE |
558 | | static WEBP_INLINE int GetSSE(const uint8_t* WEBP_RESTRICT a, |
559 | 0 | const uint8_t* WEBP_RESTRICT b, int w, int h) { |
560 | 0 | int count = 0; |
561 | 0 | int y, x; |
562 | 0 | for (y = 0; y < h; ++y) { |
563 | 0 | for (x = 0; x < w; ++x) { |
564 | 0 | const int diff = (int)a[x] - b[x]; |
565 | 0 | count += diff * diff; |
566 | 0 | } |
567 | 0 | a += BPS; |
568 | 0 | b += BPS; |
569 | 0 | } |
570 | 0 | return count; |
571 | 0 | } |
572 | | |
573 | | static int SSE16x16_C(const uint8_t* WEBP_RESTRICT a, |
574 | 0 | const uint8_t* WEBP_RESTRICT b) { |
575 | 0 | return GetSSE(a, b, 16, 16); |
576 | 0 | } |
577 | | static int SSE16x8_C(const uint8_t* WEBP_RESTRICT a, |
578 | 0 | const uint8_t* WEBP_RESTRICT b) { |
579 | 0 | return GetSSE(a, b, 16, 8); |
580 | 0 | } |
581 | | static int SSE8x8_C(const uint8_t* WEBP_RESTRICT a, |
582 | 0 | const uint8_t* WEBP_RESTRICT b) { |
583 | 0 | return GetSSE(a, b, 8, 8); |
584 | 0 | } |
585 | | static int SSE4x4_C(const uint8_t* WEBP_RESTRICT a, |
586 | 0 | const uint8_t* WEBP_RESTRICT b) { |
587 | 0 | return GetSSE(a, b, 4, 4); |
588 | 0 | } |
589 | | #endif // !WEBP_NEON_OMIT_C_CODE |
590 | | |
591 | 0 | static void Mean16x4_C(const uint8_t* WEBP_RESTRICT ref, uint32_t dc[4]) { |
592 | 0 | int k, x, y; |
593 | 0 | for (k = 0; k < 4; ++k) { |
594 | 0 | uint32_t avg = 0; |
595 | 0 | for (y = 0; y < 4; ++y) { |
596 | 0 | for (x = 0; x < 4; ++x) { |
597 | 0 | avg += ref[x + y * BPS]; |
598 | 0 | } |
599 | 0 | } |
600 | 0 | dc[k] = avg; |
601 | 0 | ref += 4; // go to next 4x4 block. |
602 | 0 | } |
603 | 0 | } |
604 | | |
605 | | //------------------------------------------------------------------------------ |
606 | | // Texture distortion |
607 | | // |
608 | | // We try to match the spectral content (weighted) between source and |
609 | | // reconstructed samples. |
610 | | |
611 | | #if !WEBP_NEON_OMIT_C_CODE |
612 | | // Hadamard transform |
613 | | // Returns the weighted sum of the absolute value of transformed coefficients. |
614 | | // w[] contains a row-major 4 by 4 symmetric matrix. |
615 | | static int TTransform(const uint8_t* WEBP_RESTRICT in, |
616 | 0 | const uint16_t* WEBP_RESTRICT w) { |
617 | 0 | int sum = 0; |
618 | 0 | int tmp[16]; |
619 | 0 | int i; |
620 | | // horizontal pass |
621 | 0 | for (i = 0; i < 4; ++i, in += BPS) { |
622 | 0 | const int a0 = in[0] + in[2]; |
623 | 0 | const int a1 = in[1] + in[3]; |
624 | 0 | const int a2 = in[1] - in[3]; |
625 | 0 | const int a3 = in[0] - in[2]; |
626 | 0 | tmp[0 + i * 4] = a0 + a1; |
627 | 0 | tmp[1 + i * 4] = a3 + a2; |
628 | 0 | tmp[2 + i * 4] = a3 - a2; |
629 | 0 | tmp[3 + i * 4] = a0 - a1; |
630 | 0 | } |
631 | | // vertical pass |
632 | 0 | for (i = 0; i < 4; ++i, ++w) { |
633 | 0 | const int a0 = tmp[0 + i] + tmp[8 + i]; |
634 | 0 | const int a1 = tmp[4 + i] + tmp[12 + i]; |
635 | 0 | const int a2 = tmp[4 + i] - tmp[12 + i]; |
636 | 0 | const int a3 = tmp[0 + i] - tmp[8 + i]; |
637 | 0 | const int b0 = a0 + a1; |
638 | 0 | const int b1 = a3 + a2; |
639 | 0 | const int b2 = a3 - a2; |
640 | 0 | const int b3 = a0 - a1; |
641 | |
|
642 | 0 | sum += w[0] * abs(b0); |
643 | 0 | sum += w[4] * abs(b1); |
644 | 0 | sum += w[8] * abs(b2); |
645 | 0 | sum += w[12] * abs(b3); |
646 | 0 | } |
647 | 0 | return sum; |
648 | 0 | } |
649 | | |
650 | | static int Disto4x4_C(const uint8_t* WEBP_RESTRICT const a, |
651 | | const uint8_t* WEBP_RESTRICT const b, |
652 | 0 | const uint16_t* WEBP_RESTRICT const w) { |
653 | 0 | const int sum1 = TTransform(a, w); |
654 | 0 | const int sum2 = TTransform(b, w); |
655 | 0 | return abs(sum2 - sum1) >> 5; |
656 | 0 | } |
657 | | |
658 | | static int Disto16x16_C(const uint8_t* WEBP_RESTRICT const a, |
659 | | const uint8_t* WEBP_RESTRICT const b, |
660 | 0 | const uint16_t* WEBP_RESTRICT const w) { |
661 | 0 | int D = 0; |
662 | 0 | int x, y; |
663 | 0 | for (y = 0; y < 16 * BPS; y += 4 * BPS) { |
664 | 0 | for (x = 0; x < 16; x += 4) { |
665 | 0 | D += Disto4x4_C(a + x + y, b + x + y, w); |
666 | 0 | } |
667 | 0 | } |
668 | 0 | return D; |
669 | 0 | } |
670 | | #endif // !WEBP_NEON_OMIT_C_CODE |
671 | | |
672 | | //------------------------------------------------------------------------------ |
673 | | // Quantization |
674 | | // |
675 | | |
676 | | #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC |
677 | | static const uint8_t kZigzag[16] = {0, 1, 4, 8, 5, 2, 3, 6, |
678 | | 9, 12, 13, 10, 7, 11, 14, 15}; |
679 | | |
680 | | // Simple quantization |
681 | | static int QuantizeBlock_C(int16_t in[16], int16_t out[16], |
682 | 0 | const VP8Matrix* WEBP_RESTRICT const mtx) { |
683 | 0 | int last = -1; |
684 | 0 | int n; |
685 | 0 | for (n = 0; n < 16; ++n) { |
686 | 0 | const int j = kZigzag[n]; |
687 | 0 | const int sign = (in[j] < 0); |
688 | 0 | const uint32_t coeff = (sign ? -in[j] : in[j]) + mtx->sharpen[j]; |
689 | 0 | if (coeff > mtx->zthresh[j]) { |
690 | 0 | const uint32_t Q = mtx->q[j]; |
691 | 0 | const uint32_t iQ = mtx->iq[j]; |
692 | 0 | const uint32_t B = mtx->bias[j]; |
693 | 0 | int level = QUANTDIV(coeff, iQ, B); |
694 | 0 | if (level > MAX_LEVEL) level = MAX_LEVEL; |
695 | 0 | if (sign) level = -level; |
696 | 0 | in[j] = level * (int)Q; |
697 | 0 | out[n] = level; |
698 | 0 | if (level) last = n; |
699 | 0 | } else { |
700 | 0 | out[n] = 0; |
701 | 0 | in[j] = 0; |
702 | 0 | } |
703 | 0 | } |
704 | 0 | return (last >= 0); |
705 | 0 | } |
706 | | |
707 | | static int Quantize2Blocks_C(int16_t in[32], int16_t out[32], |
708 | 0 | const VP8Matrix* WEBP_RESTRICT const mtx) { |
709 | 0 | int nz; |
710 | 0 | nz = VP8EncQuantizeBlock(in + 0 * 16, out + 0 * 16, mtx) << 0; |
711 | 0 | nz |= VP8EncQuantizeBlock(in + 1 * 16, out + 1 * 16, mtx) << 1; |
712 | 0 | return nz; |
713 | 0 | } |
714 | | #endif // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC |
715 | | |
716 | | //------------------------------------------------------------------------------ |
717 | | // Block copy |
718 | | |
719 | | static WEBP_INLINE void Copy(const uint8_t* WEBP_RESTRICT src, |
720 | 0 | uint8_t* WEBP_RESTRICT dst, int w, int h) { |
721 | 0 | int y; |
722 | 0 | for (y = 0; y < h; ++y) { |
723 | 0 | memcpy(dst, src, w); |
724 | 0 | src += BPS; |
725 | 0 | dst += BPS; |
726 | 0 | } |
727 | 0 | } |
728 | | |
729 | | static void Copy4x4_C(const uint8_t* WEBP_RESTRICT src, |
730 | 0 | uint8_t* WEBP_RESTRICT dst) { |
731 | 0 | Copy(src, dst, 4, 4); |
732 | 0 | } |
733 | | |
734 | | static void Copy16x8_C(const uint8_t* WEBP_RESTRICT src, |
735 | 0 | uint8_t* WEBP_RESTRICT dst) { |
736 | 0 | Copy(src, dst, 16, 8); |
737 | 0 | } |
738 | | |
739 | | //------------------------------------------------------------------------------ |
740 | | // Initialization |
741 | | |
742 | | // Speed-critical function pointers. We have to initialize them to the default |
743 | | // implementations within VP8EncDspInit(). |
744 | | VP8CHisto VP8CollectHistogram; |
745 | | VP8Idct VP8ITransform; |
746 | | VP8Fdct VP8FTransform; |
747 | | VP8Fdct VP8FTransform2; |
748 | | VP8WHT VP8FTransformWHT; |
749 | | VP8Intra4Preds VP8EncPredLuma4; |
750 | | VP8IntraPreds VP8EncPredLuma16; |
751 | | VP8IntraPreds VP8EncPredChroma8; |
752 | | VP8Metric VP8SSE16x16; |
753 | | VP8Metric VP8SSE8x8; |
754 | | VP8Metric VP8SSE16x8; |
755 | | VP8Metric VP8SSE4x4; |
756 | | VP8WMetric VP8TDisto4x4; |
757 | | VP8WMetric VP8TDisto16x16; |
758 | | VP8MeanMetric VP8Mean16x4; |
759 | | VP8QuantizeBlock VP8EncQuantizeBlock; |
760 | | VP8Quantize2Blocks VP8EncQuantize2Blocks; |
761 | | VP8QuantizeBlockWHT VP8EncQuantizeBlockWHT; |
762 | | VP8BlockCopy VP8Copy4x4; |
763 | | VP8BlockCopy VP8Copy16x8; |
764 | | |
765 | | extern VP8CPUInfo VP8GetCPUInfo; |
766 | | extern void VP8EncDspInitSSE2(void); |
767 | | extern void VP8EncDspInitSSE41(void); |
768 | | extern void VP8EncDspInitNEON(void); |
769 | | extern void VP8EncDspInitMIPS32(void); |
770 | | extern void VP8EncDspInitMIPSdspR2(void); |
771 | | extern void VP8EncDspInitMSA(void); |
772 | | |
773 | 0 | WEBP_DSP_INIT_FUNC(VP8EncDspInit) { |
774 | 0 | VP8DspInit(); // common inverse transforms |
775 | 0 | InitTables(); |
776 | | |
777 | | // default C implementations |
778 | 0 | #if !WEBP_NEON_OMIT_C_CODE |
779 | 0 | VP8ITransform = ITransform_C; |
780 | 0 | VP8FTransform = FTransform_C; |
781 | 0 | VP8FTransformWHT = FTransformWHT_C; |
782 | 0 | VP8TDisto4x4 = Disto4x4_C; |
783 | 0 | VP8TDisto16x16 = Disto16x16_C; |
784 | 0 | VP8CollectHistogram = CollectHistogram_C; |
785 | 0 | VP8SSE16x16 = SSE16x16_C; |
786 | 0 | VP8SSE16x8 = SSE16x8_C; |
787 | 0 | VP8SSE8x8 = SSE8x8_C; |
788 | 0 | VP8SSE4x4 = SSE4x4_C; |
789 | 0 | #endif |
790 | |
|
791 | 0 | #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC |
792 | 0 | VP8EncQuantizeBlock = QuantizeBlock_C; |
793 | 0 | VP8EncQuantize2Blocks = Quantize2Blocks_C; |
794 | 0 | VP8EncQuantizeBlockWHT = QuantizeBlock_C; |
795 | 0 | #endif |
796 | |
|
797 | 0 | #if !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64 || BPS != 32 |
798 | 0 | VP8EncPredLuma4 = Intra4Preds_C; |
799 | 0 | #endif |
800 | 0 | #if !WEBP_NEON_OMIT_C_CODE || !WEBP_AARCH64 |
801 | 0 | VP8EncPredLuma16 = Intra16Preds_C; |
802 | 0 | #endif |
803 | |
|
804 | 0 | VP8FTransform2 = FTransform2_C; |
805 | 0 | VP8EncPredChroma8 = IntraChromaPreds_C; |
806 | 0 | VP8Mean16x4 = Mean16x4_C; |
807 | 0 | VP8Copy4x4 = Copy4x4_C; |
808 | 0 | VP8Copy16x8 = Copy16x8_C; |
809 | | |
810 | | // If defined, use CPUInfo() to overwrite some pointers with faster versions. |
811 | 0 | if (VP8GetCPUInfo != NULL) { |
812 | 0 | #if defined(WEBP_HAVE_SSE2) |
813 | 0 | if (VP8GetCPUInfo(kSSE2)) { |
814 | 0 | VP8EncDspInitSSE2(); |
815 | 0 | #if defined(WEBP_HAVE_SSE41) |
816 | 0 | if (VP8GetCPUInfo(kSSE4_1)) { |
817 | 0 | VP8EncDspInitSSE41(); |
818 | 0 | } |
819 | 0 | #endif |
820 | 0 | } |
821 | 0 | #endif |
822 | | #if defined(WEBP_USE_MIPS32) |
823 | | if (VP8GetCPUInfo(kMIPS32)) { |
824 | | VP8EncDspInitMIPS32(); |
825 | | } |
826 | | #endif |
827 | | #if defined(WEBP_USE_MIPS_DSP_R2) |
828 | | if (VP8GetCPUInfo(kMIPSdspR2)) { |
829 | | VP8EncDspInitMIPSdspR2(); |
830 | | } |
831 | | #endif |
832 | | #if defined(WEBP_USE_MSA) |
833 | | if (VP8GetCPUInfo(kMSA)) { |
834 | | VP8EncDspInitMSA(); |
835 | | } |
836 | | #endif |
837 | 0 | } |
838 | |
|
839 | | #if defined(WEBP_HAVE_NEON) |
840 | | if (WEBP_NEON_OMIT_C_CODE || |
841 | | (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) { |
842 | | VP8EncDspInitNEON(); |
843 | | } |
844 | | #endif |
845 | |
|
846 | 0 | assert(VP8ITransform != NULL); |
847 | 0 | assert(VP8FTransform != NULL); |
848 | 0 | assert(VP8FTransformWHT != NULL); |
849 | 0 | assert(VP8TDisto4x4 != NULL); |
850 | 0 | assert(VP8TDisto16x16 != NULL); |
851 | 0 | assert(VP8CollectHistogram != NULL); |
852 | 0 | assert(VP8SSE16x16 != NULL); |
853 | 0 | assert(VP8SSE16x8 != NULL); |
854 | 0 | assert(VP8SSE8x8 != NULL); |
855 | 0 | assert(VP8SSE4x4 != NULL); |
856 | 0 | assert(VP8EncQuantizeBlock != NULL); |
857 | 0 | assert(VP8EncQuantize2Blocks != NULL); |
858 | 0 | assert(VP8FTransform2 != NULL); |
859 | 0 | assert(VP8EncPredLuma4 != NULL); |
860 | 0 | assert(VP8EncPredLuma16 != NULL); |
861 | 0 | assert(VP8EncPredChroma8 != NULL); |
862 | 0 | assert(VP8Mean16x4 != NULL); |
863 | 0 | assert(VP8EncQuantizeBlockWHT != NULL); |
864 | 0 | assert(VP8Copy4x4 != NULL); |
865 | 0 | assert(VP8Copy16x8 != NULL); |
866 | 0 | } |