1 // Copyright 2018 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 #ifndef WEBP_DSP_QUANT_H_
11 #define WEBP_DSP_QUANT_H_
12
13 #include <string.h>
14
15 #include "src/dsp/dsp.h"
16 #include "src/webp/types.h"
17
18 #if defined(WEBP_USE_NEON) && !defined(WEBP_ANDROID_NEON) && \
19 !defined(WEBP_HAVE_NEON_RTCD)
20 #include <arm_neon.h>
21
22 #define IsFlat IsFlat_NEON
23
horizontal_add_uint32x4(const uint32x4_t a)24 static uint32_t horizontal_add_uint32x4(const uint32x4_t a) {
25 #if defined(__aarch64__)
26 return vaddvq_u32(a);
27 #else
28 const uint64x2_t b = vpaddlq_u32(a);
29 const uint32x2_t c = vadd_u32(vreinterpret_u32_u64(vget_low_u64(b)),
30 vreinterpret_u32_u64(vget_high_u64(b)));
31 return vget_lane_u32(c, 0);
32 #endif
33 }
34
IsFlat(const int16_t * levels,int num_blocks,int thresh)35 static WEBP_INLINE int IsFlat(const int16_t* levels, int num_blocks,
36 int thresh) {
37 const int16x8_t tst_ones = vdupq_n_s16(-1);
38 uint32x4_t sum = vdupq_n_u32(0);
39
40 for (int i = 0; i < num_blocks; ++i) {
41 // Set DC to zero.
42 const int16x8_t a_0 = vsetq_lane_s16(0, vld1q_s16(levels), 0);
43 const int16x8_t a_1 = vld1q_s16(levels + 8);
44
45 const uint16x8_t b_0 = vshrq_n_u16(vtstq_s16(a_0, tst_ones), 15);
46 const uint16x8_t b_1 = vshrq_n_u16(vtstq_s16(a_1, tst_ones), 15);
47
48 sum = vpadalq_u16(sum, b_0);
49 sum = vpadalq_u16(sum, b_1);
50
51 levels += 16;
52 }
53 return thresh >= (int)horizontal_add_uint32x4(sum);
54 }
55
56 #else
57
58 #define IsFlat IsFlat_C
59
IsFlat(const int16_t * levels,int num_blocks,int thresh)60 static WEBP_INLINE int IsFlat(const int16_t* levels, int num_blocks,
61 int thresh) {
62 int score = 0;
63 while (num_blocks-- > 0) { // TODO(skal): refine positional scoring?
64 int i;
65 for (i = 1; i < 16; ++i) { // omit DC, we're only interested in AC
66 score += (levels[i] != 0);
67 if (score > thresh) return 0;
68 }
69 levels += 16;
70 }
71 return 1;
72 }
73
74 #endif // defined(WEBP_USE_NEON) && !defined(WEBP_ANDROID_NEON) &&
75 // !defined(WEBP_HAVE_NEON_RTCD)
76
IsFlatSource16(const uint8_t * src)77 static WEBP_INLINE int IsFlatSource16(const uint8_t* src) {
78 const uint32_t v = src[0] * 0x01010101u;
79 int i;
80 for (i = 0; i < 16; ++i) {
81 if (memcmp(src + 0, &v, 4) || memcmp(src + 4, &v, 4) ||
82 memcmp(src + 8, &v, 4) || memcmp(src + 12, &v, 4)) {
83 return 0;
84 }
85 src += BPS;
86 }
87 return 1;
88 }
89
90 #endif // WEBP_DSP_QUANT_H_
91