1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2016 Intel Corporation. All rights reserved.
4  *
5  * Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
6  *         Liam Girdwood <liam.r.girdwood@linux.intel.com>
7  *         Keyon Jie <yang.jie@linux.intel.com>
8  */
9 
10 #ifndef __SOF_AUDIO_FORMAT_H__
11 #define __SOF_AUDIO_FORMAT_H__
12 
13 #include <ipc/stream.h>
14 #include <stdint.h>
15 
16 /* Maximum and minimum values for 24 bit */
17 #define INT24_MAXVALUE  8388607
18 #define INT24_MINVALUE -8388608
19 
20 /* Collection of common fractional numbers */
21 #define ONE_Q2_30 1073741824 /* Q2.30 1.0 */
22 #define ONE_Q1_31 2147483647 /* Q1.31 ~1.0 */
23 #define MINUS_3DB_Q1_31  1520301996 /* 10^(-3/20) */
24 #define MINUS_6DB_Q1_31  1076291389 /* 10^(-6/20) */
25 #define MINUS_10DB_Q1_31  679093957  /* 10^(-10/20) */
26 #define MINUS_20DB_Q1_31  214748365  /* 10^(-20/20) */
27 #define MINUS_30DB_Q1_31   67909396  /* 10^(-30/20) */
28 #define MINUS_40DB_Q1_31   21474836  /* 10^(-40/20) */
29 #define MINUS_50DB_Q1_31    6790940  /* 10^(-50/20) */
30 #define MINUS_60DB_Q1_31    2147484  /* 10^(-60/20) */
31 #define MINUS_70DB_Q1_31     679094  /* 10^(-70/20) */
32 #define MINUS_80DB_Q1_31     214748  /* 10^(-80/20) */
33 #define MINUS_90DB_Q1_31      67909  /* 10^(-90/20) */
34 
35 /* Compute the number of shifts
36  * This will result in a compiler overflow error if shift bits are out of
37  * range as INT64_MAX/MIN is greater than 32 bit Q shift parameter
38  */
39 #define Q_SHIFT_BITS_64(qx, qy, qz) \
40 	((qx + qy - qz) <= 63 ? (((qx + qy - qz) >= 0) ? \
41 	 (qx + qy - qz) : INT64_MIN) : INT64_MAX)
42 
43 #define Q_SHIFT_BITS_32(qx, qy, qz) \
44 	((qx + qy - qz) <= 31 ? (((qx + qy - qz) >= 0) ? \
45 	 (qx + qy - qz) : INT32_MIN) : INT32_MAX)
46 
47 /* Convert a float number to fractional Qnx.ny format. Note that there is no
48  * check for nx+ny number of bits to fit the word length of int. The parameter
49  * qy must be 31 or less.
50  */
51 #define Q_CONVERT_FLOAT(f, qy) \
52 	((int32_t)(((const double)f) * ((int64_t)1 << (const int)qy) + 0.5))
53 
54 /* Convert fractional Qnx.ny number x to float */
55 #define Q_CONVERT_QTOF(x, ny) ((float)(x) / ((int64_t)1 << (ny)))
56 
57 /* A more clever macro for Q-shifts */
58 #define Q_SHIFT(x, src_q, dst_q) ((x) >> ((src_q) - (dst_q)))
59 #define Q_SHIFT_RND(x, src_q, dst_q) \
60 	((((x) >> ((src_q) - (dst_q) - 1)) + 1) >> 1)
61 
62 /* Alternative version since compiler does not allow (x >> -1) */
63 #define Q_SHIFT_LEFT(x, src_q, dst_q) ((x) << ((dst_q) - (src_q)))
64 
65 /* Fractional multiplication with shift
66  * Note that the parameters px and py must be cast to (int64_t) if other type.
67  */
68 #define Q_MULTS_32X32(px, py, qx, qy, qp) \
69 	((px) * (py) >> (((qx) + (qy) - (qp))))
70 
71 /* Fractional multiplication with shift and round
72  * Note that the parameters px and py must be cast to (int64_t) if other type.
73  */
74 #define Q_MULTSR_32X32(px, py, qx, qy, qp) \
75 	((((px) * (py) >> ((qx) + (qy) - (qp) - 1)) + 1) >> 1)
76 
77 /* Saturation */
78 #define SATP_INT32(x) (((x) > INT32_MAX) ? INT32_MAX : (x))
79 #define SATM_INT32(x) (((x) < INT32_MIN) ? INT32_MIN : (x))
80 
q_mults_32x32(int32_t x,int32_t y,const int shift_bits)81 static inline int64_t q_mults_32x32(int32_t x, int32_t y, const int shift_bits)
82 {
83 	return ((int64_t)x * y) >> shift_bits;
84 }
85 
q_multsr_32x32(int32_t x,int32_t y,const int shift_bits)86 static inline int64_t q_multsr_32x32(int32_t x, int32_t y, const int shift_bits)
87 {
88 	return ((((int64_t)x * y) >> (shift_bits - 1)) + 1) >> 1;
89 }
90 
q_mults_16x16(int16_t x,int32_t y,const int shift_bits)91 static inline int32_t q_mults_16x16(int16_t x, int32_t y, const int shift_bits)
92 {
93 	return ((int32_t)x * y) >> shift_bits;
94 }
95 
q_multsr_16x16(int16_t x,int32_t y,const int shift_bits)96 static inline int16_t q_multsr_16x16(int16_t x, int32_t y, const int shift_bits)
97 {
98 	return ((((int32_t)x * y) >> (shift_bits - 1)) + 1) >> 1;
99 }
100 
101 /* Saturation inline functions */
102 
sat_int32(int64_t x)103 static inline int32_t sat_int32(int64_t x)
104 {
105 	if (x > INT32_MAX)
106 		return INT32_MAX;
107 	else if (x < INT32_MIN)
108 		return INT32_MIN;
109 	else
110 		return (int32_t)x;
111 }
112 
sat_int24(int32_t x)113 static inline int32_t sat_int24(int32_t x)
114 {
115 	if (x > INT24_MAXVALUE)
116 		return INT24_MAXVALUE;
117 	else if (x < INT24_MINVALUE)
118 		return INT24_MINVALUE;
119 	else
120 		return x;
121 }
122 
sat_int16(int32_t x)123 static inline int16_t sat_int16(int32_t x)
124 {
125 	if (x > INT16_MAX)
126 		return INT16_MAX;
127 	else if (x < INT16_MIN)
128 		return INT16_MIN;
129 	else
130 		return (int16_t)x;
131 }
132 
133 /* Fractional multiplication with shift and saturation */
q_multsr_sat_32x32(int32_t x,int32_t y,const int shift_bits)134 static inline int32_t q_multsr_sat_32x32(int32_t x, int32_t y,
135 					 const int shift_bits)
136 {
137 	return sat_int32(((((int64_t)x * y) >> (shift_bits - 1)) + 1) >> 1);
138 }
139 
q_multsr_sat_32x32_24(int32_t x,int32_t y,const int shift_bits)140 static inline int32_t q_multsr_sat_32x32_24(int32_t x, int32_t y,
141 					    const int shift_bits)
142 {
143 	return sat_int24(((((int64_t)x * y) >> (shift_bits - 1)) + 1) >> 1);
144 }
145 
q_multsr_sat_32x32_16(int32_t x,int32_t y,const int shift_bits)146 static inline int32_t q_multsr_sat_32x32_16(int32_t x, int32_t y,
147 					    const int shift_bits)
148 {
149 	return sat_int16(((((int64_t)x * y) >> (shift_bits - 1)) + 1) >> 1);
150 }
151 
q_multsr_sat_16x16(int16_t x,int32_t y,const int shift_bits)152 static inline int16_t q_multsr_sat_16x16(int16_t x, int32_t y,
153 					 const int shift_bits)
154 {
155 	return sat_int16(((((int32_t)x * y) >> (shift_bits - 1)) + 1) >> 1);
156 }
157 
sign_extend_s24(int32_t x)158 static inline int32_t sign_extend_s24(int32_t x)
159 {
160 	return (x << 8) >> 8;
161 }
162 
get_sample_bytes(enum sof_ipc_frame fmt)163 static inline uint32_t get_sample_bytes(enum sof_ipc_frame fmt)
164 {
165 	return fmt == SOF_IPC_FRAME_S16_LE ? 2 : 4;
166 }
167 
get_frame_bytes(enum sof_ipc_frame fmt,uint32_t channels)168 static inline uint32_t get_frame_bytes(enum sof_ipc_frame fmt,
169 				       uint32_t channels)
170 {
171 	return get_sample_bytes(fmt) * channels;
172 }
173 
174 #endif /* __SOF_AUDIO_FORMAT_H__ */
175