xref: /aosp_15_r20/external/libaom/aom_dsp/aom_dsp_common.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AOM_DSP_AOM_DSP_COMMON_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AOM_DSP_AOM_DSP_COMMON_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include <limits.h>
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
18*77c1e3ccSAndroid Build Coastguard Worker 
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/mem.h"
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
23*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
24*77c1e3ccSAndroid Build Coastguard Worker #endif
25*77c1e3ccSAndroid Build Coastguard Worker 
26*77c1e3ccSAndroid Build Coastguard Worker #if defined(_MSC_VER)
27*77c1e3ccSAndroid Build Coastguard Worker #define AOM_FORCE_INLINE __forceinline
28*77c1e3ccSAndroid Build Coastguard Worker #else
29*77c1e3ccSAndroid Build Coastguard Worker #define AOM_FORCE_INLINE __inline__ __attribute__((always_inline))
30*77c1e3ccSAndroid Build Coastguard Worker #endif
31*77c1e3ccSAndroid Build Coastguard Worker 
32*77c1e3ccSAndroid Build Coastguard Worker #define PI 3.141592653589793238462643383279502884
33*77c1e3ccSAndroid Build Coastguard Worker 
34*77c1e3ccSAndroid Build Coastguard Worker #define AOMMIN(x, y) (((x) < (y)) ? (x) : (y))
35*77c1e3ccSAndroid Build Coastguard Worker #define AOMMAX(x, y) (((x) > (y)) ? (x) : (y))
36*77c1e3ccSAndroid Build Coastguard Worker #define AOMSIGN(x) ((x) < 0 ? -1 : 0)
37*77c1e3ccSAndroid Build Coastguard Worker 
38*77c1e3ccSAndroid Build Coastguard Worker #define NELEMENTS(x) (int)(sizeof(x) / sizeof(x[0]))
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker #define IMPLIES(a, b) (!(a) || (b))  //  Logical 'a implies b' (or 'a -> b')
41*77c1e3ccSAndroid Build Coastguard Worker 
42*77c1e3ccSAndroid Build Coastguard Worker #define IS_POWER_OF_TWO(x) (((x) & ((x)-1)) == 0)
43*77c1e3ccSAndroid Build Coastguard Worker 
44*77c1e3ccSAndroid Build Coastguard Worker /* Left shifting a negative value became undefined behavior in C99 (downgraded
45*77c1e3ccSAndroid Build Coastguard Worker    from merely implementation-defined in C89). This should still compile to the
46*77c1e3ccSAndroid Build Coastguard Worker    correct thing on any two's-complement machine, but avoid ubsan warnings.*/
47*77c1e3ccSAndroid Build Coastguard Worker #define AOM_SIGNED_SHL(x, shift) ((x) * (((x)*0 + 1) << (shift)))
48*77c1e3ccSAndroid Build Coastguard Worker 
49*77c1e3ccSAndroid Build Coastguard Worker // These can be used to give a hint about branch outcomes.
50*77c1e3ccSAndroid Build Coastguard Worker // This can have an effect, even if your target processor has a
51*77c1e3ccSAndroid Build Coastguard Worker // good branch predictor, as these hints can affect basic block
52*77c1e3ccSAndroid Build Coastguard Worker // ordering by the compiler.
53*77c1e3ccSAndroid Build Coastguard Worker #ifdef __GNUC__
54*77c1e3ccSAndroid Build Coastguard Worker #define LIKELY(v) __builtin_expect(v, 1)
55*77c1e3ccSAndroid Build Coastguard Worker #define UNLIKELY(v) __builtin_expect(v, 0)
56*77c1e3ccSAndroid Build Coastguard Worker #else
57*77c1e3ccSAndroid Build Coastguard Worker #define LIKELY(v) (v)
58*77c1e3ccSAndroid Build Coastguard Worker #define UNLIKELY(v) (v)
59*77c1e3ccSAndroid Build Coastguard Worker #endif
60*77c1e3ccSAndroid Build Coastguard Worker 
61*77c1e3ccSAndroid Build Coastguard Worker typedef uint8_t qm_val_t;
62*77c1e3ccSAndroid Build Coastguard Worker #define AOM_QM_BITS 5
63*77c1e3ccSAndroid Build Coastguard Worker 
64*77c1e3ccSAndroid Build Coastguard Worker // Note:
65*77c1e3ccSAndroid Build Coastguard Worker // tran_low_t  is the datatype used for final transform coefficients.
66*77c1e3ccSAndroid Build Coastguard Worker // tran_high_t is the datatype used for intermediate transform stages.
67*77c1e3ccSAndroid Build Coastguard Worker typedef int64_t tran_high_t;
68*77c1e3ccSAndroid Build Coastguard Worker typedef int32_t tran_low_t;
69*77c1e3ccSAndroid Build Coastguard Worker 
clip_pixel(int val)70*77c1e3ccSAndroid Build Coastguard Worker static inline uint8_t clip_pixel(int val) {
71*77c1e3ccSAndroid Build Coastguard Worker   return (val > 255) ? 255 : (val < 0) ? 0 : val;
72*77c1e3ccSAndroid Build Coastguard Worker }
73*77c1e3ccSAndroid Build Coastguard Worker 
clamp(int value,int low,int high)74*77c1e3ccSAndroid Build Coastguard Worker static inline int clamp(int value, int low, int high) {
75*77c1e3ccSAndroid Build Coastguard Worker   return value < low ? low : (value > high ? high : value);
76*77c1e3ccSAndroid Build Coastguard Worker }
77*77c1e3ccSAndroid Build Coastguard Worker 
clamp64(int64_t value,int64_t low,int64_t high)78*77c1e3ccSAndroid Build Coastguard Worker static inline int64_t clamp64(int64_t value, int64_t low, int64_t high) {
79*77c1e3ccSAndroid Build Coastguard Worker   return value < low ? low : (value > high ? high : value);
80*77c1e3ccSAndroid Build Coastguard Worker }
81*77c1e3ccSAndroid Build Coastguard Worker 
fclamp(double value,double low,double high)82*77c1e3ccSAndroid Build Coastguard Worker static inline double fclamp(double value, double low, double high) {
83*77c1e3ccSAndroid Build Coastguard Worker   return value < low ? low : (value > high ? high : value);
84*77c1e3ccSAndroid Build Coastguard Worker }
85*77c1e3ccSAndroid Build Coastguard Worker 
clip_pixel_highbd(int val,int bd)86*77c1e3ccSAndroid Build Coastguard Worker static inline uint16_t clip_pixel_highbd(int val, int bd) {
87*77c1e3ccSAndroid Build Coastguard Worker   switch (bd) {
88*77c1e3ccSAndroid Build Coastguard Worker     case 8:
89*77c1e3ccSAndroid Build Coastguard Worker     default: return (uint16_t)clamp(val, 0, 255);
90*77c1e3ccSAndroid Build Coastguard Worker     case 10: return (uint16_t)clamp(val, 0, 1023);
91*77c1e3ccSAndroid Build Coastguard Worker     case 12: return (uint16_t)clamp(val, 0, 4095);
92*77c1e3ccSAndroid Build Coastguard Worker   }
93*77c1e3ccSAndroid Build Coastguard Worker }
94*77c1e3ccSAndroid Build Coastguard Worker 
95*77c1e3ccSAndroid Build Coastguard Worker // The result of this branchless code is equivalent to (value < 0 ? 0 : value)
96*77c1e3ccSAndroid Build Coastguard Worker // or max(0, value) and might be faster in some cases.
97*77c1e3ccSAndroid Build Coastguard Worker // Care should be taken since the behavior of right shifting signed type
98*77c1e3ccSAndroid Build Coastguard Worker // negative value is undefined by C standards and implementation defined,
negative_to_zero(int value)99*77c1e3ccSAndroid Build Coastguard Worker static inline unsigned int negative_to_zero(int value) {
100*77c1e3ccSAndroid Build Coastguard Worker   return value & ~(value >> (sizeof(value) * 8 - 1));
101*77c1e3ccSAndroid Build Coastguard Worker }
102*77c1e3ccSAndroid Build Coastguard Worker 
103*77c1e3ccSAndroid Build Coastguard Worker // Returns the saturating cast of a double value to int.
saturate_cast_double_to_int(double d)104*77c1e3ccSAndroid Build Coastguard Worker static inline int saturate_cast_double_to_int(double d) {
105*77c1e3ccSAndroid Build Coastguard Worker   if (d > INT_MAX) return INT_MAX;
106*77c1e3ccSAndroid Build Coastguard Worker   return (int)d;
107*77c1e3ccSAndroid Build Coastguard Worker }
108*77c1e3ccSAndroid Build Coastguard Worker 
109*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
110*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
111*77c1e3ccSAndroid Build Coastguard Worker #endif
112*77c1e3ccSAndroid Build Coastguard Worker 
113*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AOM_DSP_AOM_DSP_COMMON_H_
114