blob: df1dc0bf2ae1290420231851e80a686c6949d65d [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
Andy Hunge0ccb202014-07-27 20:11:31 -070017#include <cutils/bitops.h> /* for popcount() */
Glenn Kasten632e0c02011-12-16 10:42:58 -080018#include <audio_utils/primitives.h>
Andy Hunge0ccb202014-07-27 20:11:31 -070019#include "private/private.h"
Glenn Kasten632e0c02011-12-16 10:42:58 -080020
Glenn Kasten7ef795a2013-07-17 07:25:05 -070021void ditherAndClamp(int32_t* out, const int32_t *sums, size_t c)
Glenn Kasten632e0c02011-12-16 10:42:58 -080022{
23 size_t i;
24 for (i=0 ; i<c ; i++) {
25 int32_t l = *sums++;
26 int32_t r = *sums++;
27 int32_t nl = l >> 12;
28 int32_t nr = r >> 12;
29 l = clamp16(nl);
30 r = clamp16(nr);
31 *out++ = (r<<16) | (l & 0xFFFF);
32 }
33}
Glenn Kastenddb2e932012-01-16 13:21:31 -080034
35void memcpy_to_i16_from_u8(int16_t *dst, const uint8_t *src, size_t count)
36{
37 dst += count;
38 src += count;
39 while (count--) {
40 *--dst = (int16_t)(*--src - 0x80) << 8;
41 }
42}
Glenn Kasten7a0baca2012-07-19 13:59:50 -070043
Glenn Kasten78da2ac2012-11-12 14:39:36 -080044void memcpy_to_u8_from_i16(uint8_t *dst, const int16_t *src, size_t count)
45{
46 while (count--) {
47 *dst++ = (*src++ >> 8) + 0x80;
48 }
49}
50
Andy Hung23ef1b32015-01-13 13:56:37 -080051void memcpy_to_u8_from_float(uint8_t *dst, const float *src, size_t count)
52{
53 while (count--) {
54 *dst++ = clamp8_from_float(*src++);
55 }
56}
57
Glenn Kasten5d436052013-06-21 14:01:54 -070058void memcpy_to_i16_from_i32(int16_t *dst, const int32_t *src, size_t count)
59{
60 while (count--) {
61 *dst++ = *src++ >> 16;
62 }
63}
64
65void memcpy_to_i16_from_float(int16_t *dst, const float *src, size_t count)
66{
67 while (count--) {
Andy Hung65b5ccd2014-03-18 12:00:55 -070068 *dst++ = clamp16_from_float(*src++);
Glenn Kasten5d436052013-06-21 14:01:54 -070069 }
70}
71
Andy Hungb878e522014-04-04 13:05:43 -070072void memcpy_to_float_from_q4_27(float *dst, const int32_t *src, size_t count)
Andy Hungaecb15e2014-02-24 19:07:40 -080073{
Andy Hungb878e522014-04-04 13:05:43 -070074 while (count--) {
Andy Hungd2a25cd2014-04-02 11:23:56 -070075 *dst++ = float_from_q4_27(*src++);
Andy Hungaecb15e2014-02-24 19:07:40 -080076 }
77}
78
Andy Hunge0454e22014-03-06 13:04:56 -080079void memcpy_to_float_from_i16(float *dst, const int16_t *src, size_t count)
80{
81 while (count--) {
82 *dst++ = float_from_i16(*src++);
83 }
84}
85
Andy Hung23ef1b32015-01-13 13:56:37 -080086void memcpy_to_float_from_u8(float *dst, const uint8_t *src, size_t count)
87{
88 while (count--) {
89 *dst++ = float_from_u8(*src++);
90 }
91}
92
Andy Hunge0454e22014-03-06 13:04:56 -080093void memcpy_to_float_from_p24(float *dst, const uint8_t *src, size_t count)
94{
95 while (count--) {
96 *dst++ = float_from_p24(src);
97 src += 3;
98 }
99}
100
101void memcpy_to_i16_from_p24(int16_t *dst, const uint8_t *src, size_t count)
102{
103 while (count--) {
104#ifdef HAVE_BIG_ENDIAN
105 *dst++ = src[1] | (src[0] << 8);
106#else
107 *dst++ = src[1] | (src[2] << 8);
108#endif
109 src += 3;
110 }
111}
112
Glenn Kasten95880cb2015-05-28 15:19:16 -0700113void memcpy_to_i32_from_p24(int32_t *dst, const uint8_t *src, size_t count)
114{
115 while (count--) {
116#ifdef HAVE_BIG_ENDIAN
117 *dst++ = (src[2] << 8) | (src[1] << 16) | (src[0] << 24);
118#else
119 *dst++ = (src[0] << 8) | (src[1] << 16) | (src[2] << 24);
120#endif
121 src += 3;
122 }
123}
124
Andy Hunge0454e22014-03-06 13:04:56 -0800125void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count)
126{
127 while (count--) {
128#ifdef HAVE_BIG_ENDIAN
129 *dst++ = *src >> 8;
130 *dst++ = *src++;
131 *dst++ = 0;
132#else
133 *dst++ = 0;
134 *dst++ = *src;
135 *dst++ = *src++ >> 8;
136#endif
137 }
138}
139
140void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count)
141{
142 while (count--) {
143 int32_t ival = clamp24_from_float(*src++);
144
145#ifdef HAVE_BIG_ENDIAN
146 *dst++ = ival >> 16;
147 *dst++ = ival >> 8;
148 *dst++ = ival;
149#else
150 *dst++ = ival;
151 *dst++ = ival >> 8;
152 *dst++ = ival >> 16;
153#endif
154 }
155}
156
Glenn Kasteneee45152014-05-02 12:41:04 -0700157void memcpy_to_p24_from_q8_23(uint8_t *dst, const int32_t *src, size_t count)
158{
159 while (count--) {
160 int32_t ival = clamp24_from_q8_23(*src++);
161
162#ifdef HAVE_BIG_ENDIAN
163 *dst++ = ival >> 16;
164 *dst++ = ival >> 8;
165 *dst++ = ival;
166#else
167 *dst++ = ival;
168 *dst++ = ival >> 8;
169 *dst++ = ival >> 16;
170#endif
171 }
172}
173
Andy Hungd5829882014-03-12 11:46:15 -0700174void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count)
175{
176 while (count--) {
177 *dst++ = (int32_t)*src++ << 8;
178 }
179}
180
181void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count)
182{
183 while (count--) {
184 *dst++ = clamp24_from_float(*src++);
185 }
186}
187
Haynes Mathew George78ac9f82015-04-09 13:50:13 -0700188void memcpy_to_q8_23_from_p24(int32_t *dst, const uint8_t *src, size_t count)
189{
190 while (count--) {
191#ifdef HAVE_BIG_ENDIAN
192 *dst++ = (int8_t)src[0] << 16 | src[1] << 8 | src[2];
193#else
194 *dst++ = (int8_t)src[2] << 16 | src[1] << 8 | src[0];
195#endif
196 src += 3;
197 }
198}
199
Andy Hungb878e522014-04-04 13:05:43 -0700200void memcpy_to_q4_27_from_float(int32_t *dst, const float *src, size_t count)
201{
202 while (count--) {
203 *dst++ = clampq4_27_from_float(*src++);
204 }
205}
206
Andy Hungd5829882014-03-12 11:46:15 -0700207void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count)
208{
209 while (count--) {
210 *dst++ = clamp16(*src++ >> 8);
211 }
212}
213
214void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count)
215{
216 while (count--) {
217 *dst++ = float_from_q8_23(*src++);
218 }
219}
220
Andy Hung2c63fb62014-03-12 14:44:12 -0700221void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count)
222{
223 while (count--) {
224 *dst++ = (int32_t)*src++ << 16;
225 }
226}
227
228void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count)
229{
230 while (count--) {
231 *dst++ = clamp32_from_float(*src++);
232 }
233}
234
235void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count)
236{
237 while (count--) {
238 *dst++ = float_from_i32(*src++);
239 }
240}
241
Glenn Kasten7a0baca2012-07-19 13:59:50 -0700242void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count)
243{
244 while (count--) {
245 *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
246 src += 2;
247 }
248}
249
250void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count)
251{
252 while (count--) {
253 int32_t temp = *src++;
254 dst[0] = temp;
255 dst[1] = temp;
256 dst += 2;
257 }
258}
Glenn Kasteneb247df2014-02-21 10:00:51 -0800259
Andy Hung9c8dd452015-04-21 13:21:36 -0700260void downmix_to_mono_float_from_stereo_float(float *dst, const float *src, size_t frames)
261{
262 while (frames--) {
263 *dst++ = (src[0] + src[1]) * 0.5;
264 src += 2;
265 }
266}
267
268void upmix_to_stereo_float_from_mono_float(float *dst, const float *src, size_t frames)
269{
270 while (frames--) {
271 float temp = *src++;
272 dst[0] = temp;
273 dst[1] = temp;
274 dst += 2;
275 }
276}
277
Glenn Kasteneb247df2014-02-21 10:00:51 -0800278size_t nonZeroMono32(const int32_t *samples, size_t count)
279{
280 size_t nonZero = 0;
281 while (count-- > 0) {
282 if (*samples++ != 0) {
283 nonZero++;
284 }
285 }
286 return nonZero;
287}
288
289size_t nonZeroMono16(const int16_t *samples, size_t count)
290{
291 size_t nonZero = 0;
292 while (count-- > 0) {
293 if (*samples++ != 0) {
294 nonZero++;
295 }
296 }
297 return nonZero;
298}
299
300size_t nonZeroStereo32(const int32_t *frames, size_t count)
301{
302 size_t nonZero = 0;
303 while (count-- > 0) {
304 if (frames[0] != 0 || frames[1] != 0) {
305 nonZero++;
306 }
307 frames += 2;
308 }
309 return nonZero;
310}
311
312size_t nonZeroStereo16(const int16_t *frames, size_t count)
313{
314 size_t nonZero = 0;
315 while (count-- > 0) {
316 if (frames[0] != 0 || frames[1] != 0) {
317 nonZero++;
318 }
319 frames += 2;
320 }
321 return nonZero;
322}
Andy Hung3af2af22014-05-22 18:40:30 -0700323
Andy Hung3af2af22014-05-22 18:40:30 -0700324/*
325 * C macro to do channel mask copying independent of dst/src sample type.
326 * Don't pass in any expressions for the macro arguments here.
327 */
328#define copy_frame_by_mask(dst, dmask, src, smask, count, zero) \
329{ \
330 uint32_t bit, ormask; \
331 while (count--) { \
332 ormask = dmask | smask; \
333 while (ormask) { \
334 bit = ormask & -ormask; /* get lowest bit */ \
335 ormask ^= bit; /* remove lowest bit */ \
336 if (dmask & bit) { \
337 *dst++ = smask & bit ? *src++ : zero; \
338 } else { /* source channel only */ \
339 ++src; \
340 } \
341 } \
342 } \
343}
344
345void memcpy_by_channel_mask(void *dst, uint32_t dst_mask,
346 const void *src, uint32_t src_mask, size_t sample_size, size_t count)
347{
348#if 0
349 /* alternate way of handling memcpy_by_channel_mask by using the idxary */
350 int8_t idxary[32];
351 uint32_t src_channels = popcount(src_mask);
352 uint32_t dst_channels =
353 memcpy_by_index_array_initialization(idxary, 32, dst_mask, src_mask);
354
355 memcpy_by_idxary(dst, dst_channels, src, src_channels, idxary, sample_size, count);
356#else
357 if (dst_mask == src_mask) {
358 memcpy(dst, src, sample_size * popcount(dst_mask) * count);
359 return;
360 }
361 switch (sample_size) {
362 case 1: {
363 uint8_t *udst = (uint8_t*)dst;
364 const uint8_t *usrc = (const uint8_t*)src;
365
366 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
367 } break;
368 case 2: {
369 uint16_t *udst = (uint16_t*)dst;
370 const uint16_t *usrc = (const uint16_t*)src;
371
372 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
373 } break;
374 case 3: { /* could be slow. use a struct to represent 3 bytes of data. */
375 uint8x3_t *udst = (uint8x3_t*)dst;
376 const uint8x3_t *usrc = (const uint8x3_t*)src;
377 static const uint8x3_t zero; /* tricky - we use this to zero out a sample */
378
379 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, zero);
380 } break;
381 case 4: {
382 uint32_t *udst = (uint32_t*)dst;
383 const uint32_t *usrc = (const uint32_t*)src;
384
385 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
386 } break;
387 default:
388 abort(); /* illegal value */
389 break;
390 }
391#endif
392}
393
394/*
395 * C macro to do copying by index array, to rearrange samples
396 * within a frame. This is independent of src/dst sample type.
397 * Don't pass in any expressions for the macro arguments here.
398 */
399#define copy_frame_by_idx(dst, dst_channels, src, src_channels, idxary, count, zero) \
400{ \
401 unsigned i; \
402 int index; \
403 while (count--) { \
404 for (i = 0; i < dst_channels; ++i) { \
405 index = idxary[i]; \
406 *dst++ = index < 0 ? zero : src[index]; \
407 } \
408 src += src_channels; \
409 } \
410}
411
412void memcpy_by_index_array(void *dst, uint32_t dst_channels,
413 const void *src, uint32_t src_channels,
414 const int8_t *idxary, size_t sample_size, size_t count)
415{
416 switch (sample_size) {
417 case 1: {
418 uint8_t *udst = (uint8_t*)dst;
419 const uint8_t *usrc = (const uint8_t*)src;
420
421 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
422 } break;
423 case 2: {
424 uint16_t *udst = (uint16_t*)dst;
425 const uint16_t *usrc = (const uint16_t*)src;
426
427 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
428 } break;
429 case 3: { /* could be slow. use a struct to represent 3 bytes of data. */
430 uint8x3_t *udst = (uint8x3_t*)dst;
431 const uint8x3_t *usrc = (const uint8x3_t*)src;
432 static const uint8x3_t zero;
433
434 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, zero);
435 } break;
436 case 4: {
437 uint32_t *udst = (uint32_t*)dst;
438 const uint32_t *usrc = (const uint32_t*)src;
439
440 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
441 } break;
442 default:
443 abort(); /* illegal value */
444 break;
445 }
446}
447
448size_t memcpy_by_index_array_initialization(int8_t *idxary, size_t idxcount,
449 uint32_t dst_mask, uint32_t src_mask)
450{
451 size_t n = 0;
452 int srcidx = 0;
453 uint32_t bit, ormask = src_mask | dst_mask;
454
455 while (ormask && n < idxcount) {
456 bit = ormask & -ormask; /* get lowest bit */
457 ormask ^= bit; /* remove lowest bit */
458 if (src_mask & dst_mask & bit) { /* matching channel */
459 idxary[n++] = srcidx++;
460 } else if (src_mask & bit) { /* source channel only */
461 ++srcidx;
462 } else { /* destination channel only */
463 idxary[n++] = -1;
464 }
465 }
466 return n + popcount(ormask & dst_mask);
467}
Andy Hung5a0d0282015-02-02 15:34:13 -0800468
469size_t memcpy_by_index_array_initialization_src_index(int8_t *idxary, size_t idxcount,
470 uint32_t dst_mask, uint32_t src_mask) {
471 size_t dst_count = popcount(dst_mask);
472 if (idxcount == 0) {
473 return dst_count;
474 }
475 if (dst_count > idxcount) {
476 dst_count = idxcount;
477 }
478
479 size_t src_idx, dst_idx;
480 for (src_idx = 0, dst_idx = 0; dst_idx < dst_count; ++dst_idx) {
481 if (src_mask & 1) {
482 idxary[dst_idx] = src_idx++;
483 } else {
484 idxary[dst_idx] = -1;
485 }
486 src_mask >>= 1;
487 }
488 return dst_idx;
489}
Andy Hung291a1942015-02-27 14:20:33 -0800490
491size_t memcpy_by_index_array_initialization_dst_index(int8_t *idxary, size_t idxcount,
492 uint32_t dst_mask, uint32_t src_mask) {
493 size_t src_idx, dst_idx;
494 size_t dst_count = __builtin_popcount(dst_mask);
495 size_t src_count = __builtin_popcount(src_mask);
496 if (idxcount == 0) {
497 return dst_count;
498 }
499 if (dst_count > idxcount) {
500 dst_count = idxcount;
501 }
502 for (src_idx = 0, dst_idx = 0; dst_idx < dst_count; ++src_idx) {
503 if (dst_mask & 1) {
504 idxary[dst_idx++] = src_idx < src_count ? (signed)src_idx : -1;
505 }
506 dst_mask >>= 1;
507 }
508 return dst_idx;
509}