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