blob: 443ed9ce5868992ae6af513da6fb8f6b707ed37b [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
Andy Hungd5829882014-03-12 11:46:15 -0700129void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count)
130{
131 while (count--) {
132 *dst++ = (int32_t)*src++ << 8;
133 }
134}
135
136void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count)
137{
138 while (count--) {
139 *dst++ = clamp24_from_float(*src++);
140 }
141}
142
Andy Hungb878e522014-04-04 13:05:43 -0700143void memcpy_to_q4_27_from_float(int32_t *dst, const float *src, size_t count)
144{
145 while (count--) {
146 *dst++ = clampq4_27_from_float(*src++);
147 }
148}
149
Andy Hungd5829882014-03-12 11:46:15 -0700150void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count)
151{
152 while (count--) {
153 *dst++ = clamp16(*src++ >> 8);
154 }
155}
156
157void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count)
158{
159 while (count--) {
160 *dst++ = float_from_q8_23(*src++);
161 }
162}
163
Andy Hung2c63fb62014-03-12 14:44:12 -0700164void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count)
165{
166 while (count--) {
167 *dst++ = (int32_t)*src++ << 16;
168 }
169}
170
171void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count)
172{
173 while (count--) {
174 *dst++ = clamp32_from_float(*src++);
175 }
176}
177
178void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count)
179{
180 while (count--) {
181 *dst++ = float_from_i32(*src++);
182 }
183}
184
Glenn Kasten7a0baca2012-07-19 13:59:50 -0700185void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count)
186{
187 while (count--) {
188 *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
189 src += 2;
190 }
191}
192
193void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count)
194{
195 while (count--) {
196 int32_t temp = *src++;
197 dst[0] = temp;
198 dst[1] = temp;
199 dst += 2;
200 }
201}
Glenn Kasteneb247df2014-02-21 10:00:51 -0800202
203size_t nonZeroMono32(const int32_t *samples, size_t count)
204{
205 size_t nonZero = 0;
206 while (count-- > 0) {
207 if (*samples++ != 0) {
208 nonZero++;
209 }
210 }
211 return nonZero;
212}
213
214size_t nonZeroMono16(const int16_t *samples, size_t count)
215{
216 size_t nonZero = 0;
217 while (count-- > 0) {
218 if (*samples++ != 0) {
219 nonZero++;
220 }
221 }
222 return nonZero;
223}
224
225size_t nonZeroStereo32(const int32_t *frames, size_t count)
226{
227 size_t nonZero = 0;
228 while (count-- > 0) {
229 if (frames[0] != 0 || frames[1] != 0) {
230 nonZero++;
231 }
232 frames += 2;
233 }
234 return nonZero;
235}
236
237size_t nonZeroStereo16(const int16_t *frames, size_t count)
238{
239 size_t nonZero = 0;
240 while (count-- > 0) {
241 if (frames[0] != 0 || frames[1] != 0) {
242 nonZero++;
243 }
244 frames += 2;
245 }
246 return nonZero;
247}