blob: 1fc399b9cb1bca633183ec420bdb9e8071c7be62 [file] [log] [blame]
Glenn Kasten632e0c02011-12-16 10:42:58 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <audio_utils/primitives.h>
18
Glenn Kasten7ef795a2013-07-17 07:25:05 -070019void ditherAndClamp(int32_t* out, const int32_t *sums, size_t c)
Glenn Kasten632e0c02011-12-16 10:42:58 -080020{
21 size_t i;
22 for (i=0 ; i<c ; i++) {
23 int32_t l = *sums++;
24 int32_t r = *sums++;
25 int32_t nl = l >> 12;
26 int32_t nr = r >> 12;
27 l = clamp16(nl);
28 r = clamp16(nr);
29 *out++ = (r<<16) | (l & 0xFFFF);
30 }
31}
Glenn Kastenddb2e932012-01-16 13:21:31 -080032
33void memcpy_to_i16_from_u8(int16_t *dst, const uint8_t *src, size_t count)
34{
35 dst += count;
36 src += count;
37 while (count--) {
38 *--dst = (int16_t)(*--src - 0x80) << 8;
39 }
40}
Glenn Kasten7a0baca2012-07-19 13:59:50 -070041
Glenn Kasten78da2ac2012-11-12 14:39:36 -080042void memcpy_to_u8_from_i16(uint8_t *dst, const int16_t *src, size_t count)
43{
44 while (count--) {
45 *dst++ = (*src++ >> 8) + 0x80;
46 }
47}
48
Glenn Kasten5d436052013-06-21 14:01:54 -070049void memcpy_to_i16_from_i32(int16_t *dst, const int32_t *src, size_t count)
50{
51 while (count--) {
52 *dst++ = *src++ >> 16;
53 }
54}
55
56void memcpy_to_i16_from_float(int16_t *dst, const float *src, size_t count)
57{
58 while (count--) {
Andy Hung65b5ccd2014-03-18 12:00:55 -070059 *dst++ = clamp16_from_float(*src++);
Glenn Kasten5d436052013-06-21 14:01:54 -070060 }
61}
62
Andy Hungb878e522014-04-04 13:05:43 -070063void memcpy_to_float_from_q4_27(float *dst, const int32_t *src, size_t count)
Andy Hungaecb15e2014-02-24 19:07:40 -080064{
Andy Hungb878e522014-04-04 13:05:43 -070065 while (count--) {
Andy Hungd2a25cd2014-04-02 11:23:56 -070066 *dst++ = float_from_q4_27(*src++);
Andy Hungaecb15e2014-02-24 19:07:40 -080067 }
68}
69
Andy Hunge0454e22014-03-06 13:04:56 -080070void memcpy_to_float_from_i16(float *dst, const int16_t *src, size_t count)
71{
72 while (count--) {
73 *dst++ = float_from_i16(*src++);
74 }
75}
76
77void memcpy_to_float_from_p24(float *dst, const uint8_t *src, size_t count)
78{
79 while (count--) {
80 *dst++ = float_from_p24(src);
81 src += 3;
82 }
83}
84
85void memcpy_to_i16_from_p24(int16_t *dst, const uint8_t *src, size_t count)
86{
87 while (count--) {
88#ifdef HAVE_BIG_ENDIAN
89 *dst++ = src[1] | (src[0] << 8);
90#else
91 *dst++ = src[1] | (src[2] << 8);
92#endif
93 src += 3;
94 }
95}
96
97void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count)
98{
99 while (count--) {
100#ifdef HAVE_BIG_ENDIAN
101 *dst++ = *src >> 8;
102 *dst++ = *src++;
103 *dst++ = 0;
104#else
105 *dst++ = 0;
106 *dst++ = *src;
107 *dst++ = *src++ >> 8;
108#endif
109 }
110}
111
112void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count)
113{
114 while (count--) {
115 int32_t ival = clamp24_from_float(*src++);
116
117#ifdef HAVE_BIG_ENDIAN
118 *dst++ = ival >> 16;
119 *dst++ = ival >> 8;
120 *dst++ = ival;
121#else
122 *dst++ = ival;
123 *dst++ = ival >> 8;
124 *dst++ = ival >> 16;
125#endif
126 }
127}
128
Glenn Kasteneee45152014-05-02 12:41:04 -0700129void memcpy_to_p24_from_q8_23(uint8_t *dst, const int32_t *src, size_t count)
130{
131 while (count--) {
132 int32_t ival = clamp24_from_q8_23(*src++);
133
134#ifdef HAVE_BIG_ENDIAN
135 *dst++ = ival >> 16;
136 *dst++ = ival >> 8;
137 *dst++ = ival;
138#else
139 *dst++ = ival;
140 *dst++ = ival >> 8;
141 *dst++ = ival >> 16;
142#endif
143 }
144}
145
Andy Hungd5829882014-03-12 11:46:15 -0700146void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count)
147{
148 while (count--) {
149 *dst++ = (int32_t)*src++ << 8;
150 }
151}
152
153void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count)
154{
155 while (count--) {
156 *dst++ = clamp24_from_float(*src++);
157 }
158}
159
Andy Hungb878e522014-04-04 13:05:43 -0700160void memcpy_to_q4_27_from_float(int32_t *dst, const float *src, size_t count)
161{
162 while (count--) {
163 *dst++ = clampq4_27_from_float(*src++);
164 }
165}
166
Andy Hungd5829882014-03-12 11:46:15 -0700167void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count)
168{
169 while (count--) {
170 *dst++ = clamp16(*src++ >> 8);
171 }
172}
173
174void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count)
175{
176 while (count--) {
177 *dst++ = float_from_q8_23(*src++);
178 }
179}
180
Andy Hung2c63fb62014-03-12 14:44:12 -0700181void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count)
182{
183 while (count--) {
184 *dst++ = (int32_t)*src++ << 16;
185 }
186}
187
188void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count)
189{
190 while (count--) {
191 *dst++ = clamp32_from_float(*src++);
192 }
193}
194
195void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count)
196{
197 while (count--) {
198 *dst++ = float_from_i32(*src++);
199 }
200}
201
Glenn Kasten7a0baca2012-07-19 13:59:50 -0700202void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count)
203{
204 while (count--) {
205 *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
206 src += 2;
207 }
208}
209
210void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count)
211{
212 while (count--) {
213 int32_t temp = *src++;
214 dst[0] = temp;
215 dst[1] = temp;
216 dst += 2;
217 }
218}
Glenn Kasteneb247df2014-02-21 10:00:51 -0800219
220size_t nonZeroMono32(const int32_t *samples, size_t count)
221{
222 size_t nonZero = 0;
223 while (count-- > 0) {
224 if (*samples++ != 0) {
225 nonZero++;
226 }
227 }
228 return nonZero;
229}
230
231size_t nonZeroMono16(const int16_t *samples, size_t count)
232{
233 size_t nonZero = 0;
234 while (count-- > 0) {
235 if (*samples++ != 0) {
236 nonZero++;
237 }
238 }
239 return nonZero;
240}
241
242size_t nonZeroStereo32(const int32_t *frames, size_t count)
243{
244 size_t nonZero = 0;
245 while (count-- > 0) {
246 if (frames[0] != 0 || frames[1] != 0) {
247 nonZero++;
248 }
249 frames += 2;
250 }
251 return nonZero;
252}
253
254size_t nonZeroStereo16(const int16_t *frames, size_t count)
255{
256 size_t nonZero = 0;
257 while (count-- > 0) {
258 if (frames[0] != 0 || frames[1] != 0) {
259 nonZero++;
260 }
261 frames += 2;
262 }
263 return nonZero;
264}