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