Coverage Report

Created: 2024-06-18 06:04

/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
static void Intra16Preds_C(uint8_t* dst,
336
0
                           const uint8_t* left, const uint8_t* top) {
337
0
  DCMode(I16DC16 + dst, left, top, 16, 16, 5);
338
0
  VerticalPred(I16VE16 + dst, top, 16);
339
0
  HorizontalPred(I16HE16 + dst, left, 16);
340
0
  TrueMotion(I16TM16 + dst, left, top, 16);
341
0
}
342
343
//------------------------------------------------------------------------------
344
// luma 4x4 prediction
345
346
0
#define DST(x, y) dst[(x) + (y) * BPS]
347
0
#define AVG3(a, b, c) ((uint8_t)(((a) + 2 * (b) + (c) + 2) >> 2))
348
0
#define AVG2(a, b) (((a) + (b) + 1) >> 1)
349
350
0
static void VE4(uint8_t* dst, const uint8_t* top) {    // vertical
351
0
  const uint8_t vals[4] = {
352
0
    AVG3(top[-1], top[0], top[1]),
353
0
    AVG3(top[ 0], top[1], top[2]),
354
0
    AVG3(top[ 1], top[2], top[3]),
355
0
    AVG3(top[ 2], top[3], top[4])
356
0
  };
357
0
  int i;
358
0
  for (i = 0; i < 4; ++i) {
359
0
    memcpy(dst + i * BPS, vals, 4);
360
0
  }
361
0
}
362
363
0
static void HE4(uint8_t* dst, const uint8_t* top) {    // horizontal
364
0
  const int X = top[-1];
365
0
  const int I = top[-2];
366
0
  const int J = top[-3];
367
0
  const int K = top[-4];
368
0
  const int L = top[-5];
369
0
  WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(X, I, J));
370
0
  WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(I, J, K));
371
0
  WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(J, K, L));
372
0
  WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(K, L, L));
373
0
}
374
375
0
static void DC4(uint8_t* dst, const uint8_t* top) {
376
0
  uint32_t dc = 4;
377
0
  int i;
378
0
  for (i = 0; i < 4; ++i) dc += top[i] + top[-5 + i];
379
0
  Fill(dst, dc >> 3, 4);
380
0
}
381
382
0
static void RD4(uint8_t* dst, const uint8_t* top) {
383
0
  const int X = top[-1];
384
0
  const int I = top[-2];
385
0
  const int J = top[-3];
386
0
  const int K = top[-4];
387
0
  const int L = top[-5];
388
0
  const int A = top[0];
389
0
  const int B = top[1];
390
0
  const int C = top[2];
391
0
  const int D = top[3];
392
0
  DST(0, 3)                                     = AVG3(J, K, L);
393
0
  DST(0, 2) = DST(1, 3)                         = AVG3(I, J, K);
394
0
  DST(0, 1) = DST(1, 2) = DST(2, 3)             = AVG3(X, I, J);
395
0
  DST(0, 0) = DST(1, 1) = DST(2, 2) = DST(3, 3) = AVG3(A, X, I);
396
0
  DST(1, 0) = DST(2, 1) = DST(3, 2)             = AVG3(B, A, X);
397
0
  DST(2, 0) = DST(3, 1)                         = AVG3(C, B, A);
398
0
  DST(3, 0)                                     = AVG3(D, C, B);
399
0
}
400
401
0
static void LD4(uint8_t* dst, const uint8_t* top) {
402
0
  const int A = top[0];
403
0
  const int B = top[1];
404
0
  const int C = top[2];
405
0
  const int D = top[3];
406
0
  const int E = top[4];
407
0
  const int F = top[5];
408
0
  const int G = top[6];
409
0
  const int H = top[7];
410
0
  DST(0, 0)                                     = AVG3(A, B, C);
411
0
  DST(1, 0) = DST(0, 1)                         = AVG3(B, C, D);
412
0
  DST(2, 0) = DST(1, 1) = DST(0, 2)             = AVG3(C, D, E);
413
0
  DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
414
0
  DST(3, 1) = DST(2, 2) = DST(1, 3)             = AVG3(E, F, G);
415
0
  DST(3, 2) = DST(2, 3)                         = AVG3(F, G, H);
416
0
  DST(3, 3)                                     = AVG3(G, H, H);
417
0
}
418
419
0
static void VR4(uint8_t* dst, const uint8_t* top) {
420
0
  const int X = top[-1];
421
0
  const int I = top[-2];
422
0
  const int J = top[-3];
423
0
  const int K = top[-4];
424
0
  const int A = top[0];
425
0
  const int B = top[1];
426
0
  const int C = top[2];
427
0
  const int D = top[3];
428
0
  DST(0, 0) = DST(1, 2) = AVG2(X, A);
429
0
  DST(1, 0) = DST(2, 2) = AVG2(A, B);
430
0
  DST(2, 0) = DST(3, 2) = AVG2(B, C);
431
0
  DST(3, 0)             = AVG2(C, D);
432
433
0
  DST(0, 3) =             AVG3(K, J, I);
434
0
  DST(0, 2) =             AVG3(J, I, X);
435
0
  DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
436
0
  DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
437
0
  DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
438
0
  DST(3, 1) =             AVG3(B, C, D);
439
0
}
440
441
0
static void VL4(uint8_t* dst, const uint8_t* top) {
442
0
  const int A = top[0];
443
0
  const int B = top[1];
444
0
  const int C = top[2];
445
0
  const int D = top[3];
446
0
  const int E = top[4];
447
0
  const int F = top[5];
448
0
  const int G = top[6];
449
0
  const int H = top[7];
450
0
  DST(0, 0) =             AVG2(A, B);
451
0
  DST(1, 0) = DST(0, 2) = AVG2(B, C);
452
0
  DST(2, 0) = DST(1, 2) = AVG2(C, D);
453
0
  DST(3, 0) = DST(2, 2) = AVG2(D, E);
454
455
0
  DST(0, 1) =             AVG3(A, B, C);
456
0
  DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
457
0
  DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
458
0
  DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
459
0
              DST(3, 2) = AVG3(E, F, G);
460
0
              DST(3, 3) = AVG3(F, G, H);
461
0
}
462
463
0
static void HU4(uint8_t* dst, const uint8_t* top) {
464
0
  const int I = top[-2];
465
0
  const int J = top[-3];
466
0
  const int K = top[-4];
467
0
  const int L = top[-5];
468
0
  DST(0, 0) =             AVG2(I, J);
469
0
  DST(2, 0) = DST(0, 1) = AVG2(J, K);
470
0
  DST(2, 1) = DST(0, 2) = AVG2(K, L);
471
0
  DST(1, 0) =             AVG3(I, J, K);
472
0
  DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
473
0
  DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
474
0
  DST(3, 2) = DST(2, 2) =
475
0
  DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
476
0
}
477
478
0
static void HD4(uint8_t* dst, const uint8_t* top) {
479
0
  const int X = top[-1];
480
0
  const int I = top[-2];
481
0
  const int J = top[-3];
482
0
  const int K = top[-4];
483
0
  const int L = top[-5];
484
0
  const int A = top[0];
485
0
  const int B = top[1];
486
0
  const int C = top[2];
487
488
0
  DST(0, 0) = DST(2, 1) = AVG2(I, X);
489
0
  DST(0, 1) = DST(2, 2) = AVG2(J, I);
490
0
  DST(0, 2) = DST(2, 3) = AVG2(K, J);
491
0
  DST(0, 3)             = AVG2(L, K);
492
493
0
  DST(3, 0)             = AVG3(A, B, C);
494
0
  DST(2, 0)             = AVG3(X, A, B);
495
0
  DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
496
0
  DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
497
0
  DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
498
0
  DST(1, 3)             = AVG3(L, K, J);
499
0
}
500
501
0
static void TM4(uint8_t* dst, const uint8_t* top) {
502
0
  int x, y;
503
0
  const uint8_t* const clip = clip1 + 255 - top[-1];
504
0
  for (y = 0; y < 4; ++y) {
505
0
    const uint8_t* const clip_table = clip + top[-2 - y];
506
0
    for (x = 0; x < 4; ++x) {
507
0
      dst[x] = clip_table[top[x]];
508
0
    }
509
0
    dst += BPS;
510
0
  }
511
0
}
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]
519
0
static void Intra4Preds_C(uint8_t* dst, const uint8_t* top) {
520
0
  DC4(I4DC4 + dst, top);
521
0
  TM4(I4TM4 + dst, top);
522
0
  VE4(I4VE4 + dst, top);
523
0
  HE4(I4HE4 + dst, top);
524
0
  RD4(I4RD4 + dst, top);
525
0
  VR4(I4VR4 + dst, top);
526
0
  LD4(I4LD4 + dst, top);
527
0
  VL4(I4VL4 + dst, top);
528
0
  HD4(I4HD4 + dst, top);
529
0
  HU4(I4HU4 + dst, top);
530
0
}
531
532
//------------------------------------------------------------------------------
533
// Metric
534
535
#if !WEBP_NEON_OMIT_C_CODE
536
static WEBP_INLINE int GetSSE(const uint8_t* a, const uint8_t* b,
537
0
                              int w, int h) {
538
0
  int count = 0;
539
0
  int y, x;
540
0
  for (y = 0; y < h; ++y) {
541
0
    for (x = 0; x < w; ++x) {
542
0
      const int diff = (int)a[x] - b[x];
543
0
      count += diff * diff;
544
0
    }
545
0
    a += BPS;
546
0
    b += BPS;
547
0
  }
548
0
  return count;
549
0
}
550
551
0
static int SSE16x16_C(const uint8_t* a, const uint8_t* b) {
552
0
  return GetSSE(a, b, 16, 16);
553
0
}
554
0
static int SSE16x8_C(const uint8_t* a, const uint8_t* b) {
555
0
  return GetSSE(a, b, 16, 8);
556
0
}
557
0
static int SSE8x8_C(const uint8_t* a, const uint8_t* b) {
558
0
  return GetSSE(a, b, 8, 8);
559
0
}
560
0
static int SSE4x4_C(const uint8_t* a, const uint8_t* b) {
561
0
  return GetSSE(a, b, 4, 4);
562
0
}
563
#endif  // !WEBP_NEON_OMIT_C_CODE
564
565
0
static void Mean16x4_C(const uint8_t* ref, uint32_t dc[4]) {
566
0
  int k, x, y;
567
0
  for (k = 0; k < 4; ++k) {
568
0
    uint32_t avg = 0;
569
0
    for (y = 0; y < 4; ++y) {
570
0
      for (x = 0; x < 4; ++x) {
571
0
        avg += ref[x + y * BPS];
572
0
      }
573
0
    }
574
0
    dc[k] = avg;
575
0
    ref += 4;   // go to next 4x4 block.
576
0
  }
577
0
}
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.
589
0
static int TTransform(const uint8_t* in, const uint16_t* w) {
590
0
  int sum = 0;
591
0
  int tmp[16];
592
0
  int i;
593
  // horizontal pass
594
0
  for (i = 0; i < 4; ++i, in += BPS) {
595
0
    const int a0 = in[0] + in[2];
596
0
    const int a1 = in[1] + in[3];
597
0
    const int a2 = in[1] - in[3];
598
0
    const int a3 = in[0] - in[2];
599
0
    tmp[0 + i * 4] = a0 + a1;
600
0
    tmp[1 + i * 4] = a3 + a2;
601
0
    tmp[2 + i * 4] = a3 - a2;
602
0
    tmp[3 + i * 4] = a0 - a1;
603
0
  }
604
  // vertical pass
605
0
  for (i = 0; i < 4; ++i, ++w) {
606
0
    const int a0 = tmp[0 + i] + tmp[8 + i];
607
0
    const int a1 = tmp[4 + i] + tmp[12+ i];
608
0
    const int a2 = tmp[4 + i] - tmp[12+ i];
609
0
    const int a3 = tmp[0 + i] - tmp[8 + i];
610
0
    const int b0 = a0 + a1;
611
0
    const int b1 = a3 + a2;
612
0
    const int b2 = a3 - a2;
613
0
    const int b3 = a0 - a1;
614
615
0
    sum += w[ 0] * abs(b0);
616
0
    sum += w[ 4] * abs(b1);
617
0
    sum += w[ 8] * abs(b2);
618
0
    sum += w[12] * abs(b3);
619
0
  }
620
0
  return sum;
621
0
}
622
623
static int Disto4x4_C(const uint8_t* const a, const uint8_t* const b,
624
0
                      const uint16_t* const w) {
625
0
  const int sum1 = TTransform(a, w);
626
0
  const int sum2 = TTransform(b, w);
627
0
  return abs(sum2 - sum1) >> 5;
628
0
}
629
630
static int Disto16x16_C(const uint8_t* const a, const uint8_t* const b,
631
0
                        const uint16_t* const w) {
632
0
  int D = 0;
633
0
  int x, y;
634
0
  for (y = 0; y < 16 * BPS; y += 4 * BPS) {
635
0
    for (x = 0; x < 16; x += 4) {
636
0
      D += Disto4x4_C(a + x + y, b + x + y, w);
637
0
    }
638
0
  }
639
0
  return D;
640
0
}
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
652
static int QuantizeBlock_C(int16_t in[16], int16_t out[16],
653
0
                           const VP8Matrix* const mtx) {
654
0
  int last = -1;
655
0
  int n;
656
0
  for (n = 0; n < 16; ++n) {
657
0
    const int j = kZigzag[n];
658
0
    const int sign = (in[j] < 0);
659
0
    const uint32_t coeff = (sign ? -in[j] : in[j]) + mtx->sharpen_[j];
660
0
    if (coeff > mtx->zthresh_[j]) {
661
0
      const uint32_t Q = mtx->q_[j];
662
0
      const uint32_t iQ = mtx->iq_[j];
663
0
      const uint32_t B = mtx->bias_[j];
664
0
      int level = QUANTDIV(coeff, iQ, B);
665
0
      if (level > MAX_LEVEL) level = MAX_LEVEL;
666
0
      if (sign) level = -level;
667
0
      in[j] = level * (int)Q;
668
0
      out[n] = level;
669
0
      if (level) last = n;
670
0
    } else {
671
0
      out[n] = 0;
672
0
      in[j] = 0;
673
0
    }
674
0
  }
675
0
  return (last >= 0);
676
0
}
677
678
#if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
679
static int Quantize2Blocks_C(int16_t in[32], int16_t out[32],
680
0
                             const VP8Matrix* const mtx) {
681
0
  int nz;
682
0
  nz  = VP8EncQuantizeBlock(in + 0 * 16, out + 0 * 16, mtx) << 0;
683
0
  nz |= VP8EncQuantizeBlock(in + 1 * 16, out + 1 * 16, mtx) << 1;
684
0
  return nz;
685
0
}
686
#endif  // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
687
688
//------------------------------------------------------------------------------
689
// Block copy
690
691
0
static WEBP_INLINE void Copy(const uint8_t* src, uint8_t* dst, int w, int h) {
692
0
  int y;
693
0
  for (y = 0; y < h; ++y) {
694
0
    memcpy(dst, src, w);
695
0
    src += BPS;
696
0
    dst += BPS;
697
0
  }
698
0
}
699
700
0
static void Copy4x4_C(const uint8_t* src, uint8_t* dst) {
701
0
  Copy(src, dst, 4, 4);
702
0
}
703
704
0
static void Copy16x8_C(const uint8_t* src, uint8_t* dst) {
705
0
  Copy(src, dst, 16, 8);
706
0
}
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
742
0
WEBP_DSP_INIT_FUNC(VP8EncDspInit) {
743
0
  VP8DspInit();  // common inverse transforms
744
0
  InitTables();
745
746
  // default C implementations
747
0
#if !WEBP_NEON_OMIT_C_CODE
748
0
  VP8ITransform = ITransform_C;
749
0
  VP8FTransform = FTransform_C;
750
0
  VP8FTransformWHT = FTransformWHT_C;
751
0
  VP8TDisto4x4 = Disto4x4_C;
752
0
  VP8TDisto16x16 = Disto16x16_C;
753
0
  VP8CollectHistogram = CollectHistogram_C;
754
0
  VP8SSE16x16 = SSE16x16_C;
755
0
  VP8SSE16x8 = SSE16x8_C;
756
0
  VP8SSE8x8 = SSE8x8_C;
757
0
  VP8SSE4x4 = SSE4x4_C;
758
0
#endif
759
760
0
#if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
761
0
  VP8EncQuantizeBlock = QuantizeBlock_C;
762
0
  VP8EncQuantize2Blocks = Quantize2Blocks_C;
763
0
#endif
764
765
0
  VP8FTransform2 = FTransform2_C;
766
0
  VP8EncPredLuma4 = Intra4Preds_C;
767
0
  VP8EncPredLuma16 = Intra16Preds_C;
768
0
  VP8EncPredChroma8 = IntraChromaPreds_C;
769
0
  VP8Mean16x4 = Mean16x4_C;
770
0
  VP8EncQuantizeBlockWHT = QuantizeBlock_C;
771
0
  VP8Copy4x4 = Copy4x4_C;
772
0
  VP8Copy16x8 = Copy16x8_C;
773
774
  // If defined, use CPUInfo() to overwrite some pointers with faster versions.
775
0
  if (VP8GetCPUInfo != NULL) {
776
0
#if defined(WEBP_HAVE_SSE2)
777
0
    if (VP8GetCPUInfo(kSSE2)) {
778
0
      VP8EncDspInitSSE2();
779
0
#if defined(WEBP_HAVE_SSE41)
780
0
      if (VP8GetCPUInfo(kSSE4_1)) {
781
0
        VP8EncDspInitSSE41();
782
0
      }
783
0
#endif
784
0
    }
785
0
#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
0
  }
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
0
  assert(VP8ITransform != NULL);
811
0
  assert(VP8FTransform != NULL);
812
0
  assert(VP8FTransformWHT != NULL);
813
0
  assert(VP8TDisto4x4 != NULL);
814
0
  assert(VP8TDisto16x16 != NULL);
815
0
  assert(VP8CollectHistogram != NULL);
816
0
  assert(VP8SSE16x16 != NULL);
817
0
  assert(VP8SSE16x8 != NULL);
818
0
  assert(VP8SSE8x8 != NULL);
819
0
  assert(VP8SSE4x4 != NULL);
820
0
  assert(VP8EncQuantizeBlock != NULL);
821
0
  assert(VP8EncQuantize2Blocks != NULL);
822
0
  assert(VP8FTransform2 != NULL);
823
0
  assert(VP8EncPredLuma4 != NULL);
824
0
  assert(VP8EncPredLuma16 != NULL);
825
0
  assert(VP8EncPredChroma8 != NULL);
826
0
  assert(VP8Mean16x4 != NULL);
827
0
  assert(VP8EncQuantizeBlockWHT != NULL);
828
0
  assert(VP8Copy4x4 != NULL);
829
0
  assert(VP8Copy16x8 != NULL);
830
0
}