blob: 87c72d2046054f31a9449bd145a44571872493e3 [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 Hungaecb15e2014-02-24 19:07:40 -080063void memcpy_to_float_from_q19_12(float *dst, const int32_t *src, size_t c)
64{
65 size_t i;
Andy Hungaecb15e2014-02-24 19:07:40 -080066 for (i = 0; i < c; ++i) {
Andy Hung42605b32014-02-28 15:29:47 -080067 *dst++ = float_from_q19_12(*src++);
Andy Hungaecb15e2014-02-24 19:07:40 -080068 }
69}
70
Andy Hunge0454e22014-03-06 13:04:56 -080071void memcpy_to_float_from_i16(float *dst, const int16_t *src, size_t count)
72{
73 while (count--) {
74 *dst++ = float_from_i16(*src++);
75 }
76}
77
78void memcpy_to_float_from_p24(float *dst, const uint8_t *src, size_t count)
79{
80 while (count--) {
81 *dst++ = float_from_p24(src);
82 src += 3;
83 }
84}
85
86void memcpy_to_i16_from_p24(int16_t *dst, const uint8_t *src, size_t count)
87{
88 while (count--) {
89#ifdef HAVE_BIG_ENDIAN
90 *dst++ = src[1] | (src[0] << 8);
91#else
92 *dst++ = src[1] | (src[2] << 8);
93#endif
94 src += 3;
95 }
96}
97
98void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count)
99{
100 while (count--) {
101#ifdef HAVE_BIG_ENDIAN
102 *dst++ = *src >> 8;
103 *dst++ = *src++;
104 *dst++ = 0;
105#else
106 *dst++ = 0;
107 *dst++ = *src;
108 *dst++ = *src++ >> 8;
109#endif
110 }
111}
112
113void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count)
114{
115 while (count--) {
116 int32_t ival = clamp24_from_float(*src++);
117
118#ifdef HAVE_BIG_ENDIAN
119 *dst++ = ival >> 16;
120 *dst++ = ival >> 8;
121 *dst++ = ival;
122#else
123 *dst++ = ival;
124 *dst++ = ival >> 8;
125 *dst++ = ival >> 16;
126#endif
127 }
128}
129
Andy Hungd5829882014-03-12 11:46:15 -0700130void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count)
131{
132 while (count--) {
133 *dst++ = (int32_t)*src++ << 8;
134 }
135}
136
137void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count)
138{
139 while (count--) {
140 *dst++ = clamp24_from_float(*src++);
141 }
142}
143
144void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count)
145{
146 while (count--) {
147 *dst++ = clamp16(*src++ >> 8);
148 }
149}
150
151void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count)
152{
153 while (count--) {
154 *dst++ = float_from_q8_23(*src++);
155 }
156}
157
Andy Hung2c63fb62014-03-12 14:44:12 -0700158void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count)
159{
160 while (count--) {
161 *dst++ = (int32_t)*src++ << 16;
162 }
163}
164
165void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count)
166{
167 while (count--) {
168 *dst++ = clamp32_from_float(*src++);
169 }
170}
171
172void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count)
173{
174 while (count--) {
175 *dst++ = float_from_i32(*src++);
176 }
177}
178
Glenn Kasten7a0baca2012-07-19 13:59:50 -0700179void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count)
180{
181 while (count--) {
182 *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
183 src += 2;
184 }
185}
186
187void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count)
188{
189 while (count--) {
190 int32_t temp = *src++;
191 dst[0] = temp;
192 dst[1] = temp;
193 dst += 2;
194 }
195}
Glenn Kasteneb247df2014-02-21 10:00:51 -0800196
197size_t nonZeroMono32(const int32_t *samples, size_t count)
198{
199 size_t nonZero = 0;
200 while (count-- > 0) {
201 if (*samples++ != 0) {
202 nonZero++;
203 }
204 }
205 return nonZero;
206}
207
208size_t nonZeroMono16(const int16_t *samples, size_t count)
209{
210 size_t nonZero = 0;
211 while (count-- > 0) {
212 if (*samples++ != 0) {
213 nonZero++;
214 }
215 }
216 return nonZero;
217}
218
219size_t nonZeroStereo32(const int32_t *frames, size_t count)
220{
221 size_t nonZero = 0;
222 while (count-- > 0) {
223 if (frames[0] != 0 || frames[1] != 0) {
224 nonZero++;
225 }
226 frames += 2;
227 }
228 return nonZero;
229}
230
231size_t nonZeroStereo16(const int16_t *frames, size_t count)
232{
233 size_t nonZero = 0;
234 while (count-- > 0) {
235 if (frames[0] != 0 || frames[1] != 0) {
236 nonZero++;
237 }
238 frames += 2;
239 }
240 return nonZero;
241}