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