xref: /aosp_15_r20/external/webp/src/dsp/quant.h (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
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 WEBP_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   int i;
40 
41   for (i = 0; i < num_blocks; ++i) {
42     // Set DC to zero.
43     const int16x8_t a_0 = vsetq_lane_s16(0, vld1q_s16(levels), 0);
44     const int16x8_t a_1 = vld1q_s16(levels + 8);
45 
46     const uint16x8_t b_0 = vshrq_n_u16(vtstq_s16(a_0, tst_ones), 15);
47     const uint16x8_t b_1 = vshrq_n_u16(vtstq_s16(a_1, tst_ones), 15);
48 
49     sum = vpadalq_u16(sum, b_0);
50     sum = vpadalq_u16(sum, b_1);
51 
52     levels += 16;
53   }
54   return thresh >= (int)horizontal_add_uint32x4(sum);
55 }
56 
57 #else
58 
59 #define IsFlat IsFlat_C
60 
IsFlat(const int16_t * levels,int num_blocks,int thresh)61 static WEBP_INLINE int IsFlat(const int16_t* levels, int num_blocks,
62                               int thresh) {
63   int score = 0;
64   while (num_blocks-- > 0) {      // TODO(skal): refine positional scoring?
65     int i;
66     for (i = 1; i < 16; ++i) {    // omit DC, we're only interested in AC
67       score += (levels[i] != 0);
68       if (score > thresh) return 0;
69     }
70     levels += 16;
71   }
72   return 1;
73 }
74 
75 #endif  // defined(WEBP_USE_NEON) && !defined(WEBP_ANDROID_NEON) &&
76         // !defined(WEBP_HAVE_NEON_RTCD)
77 
IsFlatSource16(const uint8_t * src)78 static WEBP_INLINE int IsFlatSource16(const uint8_t* src) {
79   const uint32_t v = src[0] * 0x01010101u;
80   int i;
81   for (i = 0; i < 16; ++i) {
82     if (memcmp(src + 0, &v, 4) || memcmp(src +  4, &v, 4) ||
83         memcmp(src + 8, &v, 4) || memcmp(src + 12, &v, 4)) {
84       return 0;
85     }
86     src += BPS;
87   }
88   return 1;
89 }
90 
91 #endif  // WEBP_DSP_QUANT_H_
92