1 /*
2 * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #ifndef VPX_VPX_DSP_VPX_DSP_COMMON_H_
12 #define VPX_VPX_DSP_VPX_DSP_COMMON_H_
13
14 #include "./vpx_config.h"
15 #include "vpx/vpx_integer.h"
16 #include "vpx_ports/mem.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 #define VPXMIN(x, y) (((x) < (y)) ? (x) : (y))
23 #define VPXMAX(x, y) (((x) > (y)) ? (x) : (y))
24
25 #define VPX_SWAP(type, a, b) \
26 do { \
27 type c = (b); \
28 (b) = a; \
29 (a) = c; \
30 } while (0)
31
32 #if CONFIG_VP9_HIGHBITDEPTH
33 // Note:
34 // tran_low_t is the datatype used for final transform coefficients.
35 // tran_high_t is the datatype used for intermediate transform stages.
36 typedef int64_t tran_high_t;
37 typedef int32_t tran_low_t;
38 #else
39 // Note:
40 // tran_low_t is the datatype used for final transform coefficients.
41 // tran_high_t is the datatype used for intermediate transform stages.
42 typedef int32_t tran_high_t;
43 typedef int16_t tran_low_t;
44 #endif // CONFIG_VP9_HIGHBITDEPTH
45
46 typedef int16_t tran_coef_t;
47
48 // Visual Studio 2022 (cl.exe) targeting AArch64 with optimizations enabled
49 // produces invalid code for clip_pixel() when the return type is uint8_t.
50 // See:
51 // https://developercommunity.visualstudio.com/t/Misoptimization-for-ARM64-in-VS-2022-17/10363361
52 // TODO(jzern): check the compiler version after a fix for the issue is
53 // released.
54 #if defined(_MSC_VER) && defined(_M_ARM64) && !defined(__clang__)
clip_pixel(int val)55 static INLINE int clip_pixel(int val) {
56 return (val > 255) ? 255 : (val < 0) ? 0 : val;
57 }
58 #else
clip_pixel(int val)59 static INLINE uint8_t clip_pixel(int val) {
60 return (val > 255) ? 255 : (val < 0) ? 0 : val;
61 }
62 #endif
63
clamp(int value,int low,int high)64 static INLINE int clamp(int value, int low, int high) {
65 return value < low ? low : (value > high ? high : value);
66 }
67
fclamp(double value,double low,double high)68 static INLINE double fclamp(double value, double low, double high) {
69 return value < low ? low : (value > high ? high : value);
70 }
71
lclamp(int64_t value,int64_t low,int64_t high)72 static INLINE int64_t lclamp(int64_t value, int64_t low, int64_t high) {
73 return value < low ? low : (value > high ? high : value);
74 }
75
clip_pixel_highbd(int val,int bd)76 static INLINE uint16_t clip_pixel_highbd(int val, int bd) {
77 switch (bd) {
78 case 8:
79 default: return (uint16_t)clamp(val, 0, 255);
80 case 10: return (uint16_t)clamp(val, 0, 1023);
81 case 12: return (uint16_t)clamp(val, 0, 4095);
82 }
83 }
84
85 #ifdef __cplusplus
86 } // extern "C"
87 #endif
88
89 #endif // VPX_VPX_DSP_VPX_DSP_COMMON_H_
90