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