blob: d88d701d18cbdcfa77d95dc58a3369cf15220a75 [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
Andy Hung1307aa72017-11-17 15:46:55 -080021void ditherAndClamp(int32_t *out, const int32_t *sums, size_t pairs)
Glenn Kasten632e0c02011-12-16 10:42:58 -080022{
Andy Hung1307aa72017-11-17 15:46:55 -080023 for (; pairs > 0; --pairs) {
24 const int32_t l = clamp16(*sums++ >> 12);
25 const int32_t r = clamp16(*sums++ >> 12);
26 *out++ = (r << 16) | (l & 0xFFFF);
Glenn Kasten632e0c02011-12-16 10:42:58 -080027 }
28}
Glenn Kastenddb2e932012-01-16 13:21:31 -080029
Andy Hung62c3d9f2017-11-21 16:36:41 -080030void memcpy_to_i16_from_q4_27(int16_t *dst, const int32_t *src, size_t count)
31{
32 for (; count > 0; --count) {
33 *dst++ = clamp16(*src++ >> 12);
34 }
35}
36
Glenn Kastenddb2e932012-01-16 13:21:31 -080037void memcpy_to_i16_from_u8(int16_t *dst, const uint8_t *src, size_t count)
38{
39 dst += count;
40 src += count;
Andy Hung1307aa72017-11-17 15:46:55 -080041 for (; count > 0; --count) {
Glenn Kastenddb2e932012-01-16 13:21:31 -080042 *--dst = (int16_t)(*--src - 0x80) << 8;
43 }
44}
Glenn Kasten7a0baca2012-07-19 13:59:50 -070045
Glenn Kasten78da2ac2012-11-12 14:39:36 -080046void memcpy_to_u8_from_i16(uint8_t *dst, const int16_t *src, size_t count)
47{
Andy Hung1307aa72017-11-17 15:46:55 -080048 for (; count > 0; --count) {
Glenn Kasten78da2ac2012-11-12 14:39:36 -080049 *dst++ = (*src++ >> 8) + 0x80;
50 }
51}
52
Andy Hung23ef1b32015-01-13 13:56:37 -080053void memcpy_to_u8_from_float(uint8_t *dst, const float *src, size_t count)
54{
Andy Hung1307aa72017-11-17 15:46:55 -080055 for (; count > 0; --count) {
Andy Hung23ef1b32015-01-13 13:56:37 -080056 *dst++ = clamp8_from_float(*src++);
57 }
58}
59
Glenn Kasten5d436052013-06-21 14:01:54 -070060void memcpy_to_i16_from_i32(int16_t *dst, const int32_t *src, size_t count)
61{
Andy Hung1307aa72017-11-17 15:46:55 -080062 for (; count > 0; --count) {
Glenn Kasten5d436052013-06-21 14:01:54 -070063 *dst++ = *src++ >> 16;
64 }
65}
66
67void memcpy_to_i16_from_float(int16_t *dst, const float *src, size_t count)
68{
Andy Hung1307aa72017-11-17 15:46:55 -080069 for (; count > 0; --count) {
Andy Hung65b5ccd2014-03-18 12:00:55 -070070 *dst++ = clamp16_from_float(*src++);
Glenn Kasten5d436052013-06-21 14:01:54 -070071 }
72}
73
Andy Hungb878e522014-04-04 13:05:43 -070074void memcpy_to_float_from_q4_27(float *dst, const int32_t *src, size_t count)
Andy Hungaecb15e2014-02-24 19:07:40 -080075{
Andy Hung1307aa72017-11-17 15:46:55 -080076 for (; count > 0; --count) {
Andy Hungd2a25cd2014-04-02 11:23:56 -070077 *dst++ = float_from_q4_27(*src++);
Andy Hungaecb15e2014-02-24 19:07:40 -080078 }
79}
80
Andy Hunge0454e22014-03-06 13:04:56 -080081void memcpy_to_float_from_i16(float *dst, const int16_t *src, size_t count)
82{
Andy Hung22667a62017-11-17 15:54:27 -080083 dst += count;
84 src += count;
Andy Hung1307aa72017-11-17 15:46:55 -080085 for (; count > 0; --count) {
Andy Hung22667a62017-11-17 15:54:27 -080086 *--dst = float_from_i16(*--src);
Andy Hunge0454e22014-03-06 13:04:56 -080087 }
88}
89
Andy Hung23ef1b32015-01-13 13:56:37 -080090void memcpy_to_float_from_u8(float *dst, const uint8_t *src, size_t count)
91{
Andy Hung22667a62017-11-17 15:54:27 -080092 dst += count;
93 src += count;
Andy Hung1307aa72017-11-17 15:46:55 -080094 for (; count > 0; --count) {
Andy Hung22667a62017-11-17 15:54:27 -080095 *--dst = float_from_u8(*--src);
Andy Hung23ef1b32015-01-13 13:56:37 -080096 }
97}
98
Andy Hunge0454e22014-03-06 13:04:56 -080099void memcpy_to_float_from_p24(float *dst, const uint8_t *src, size_t count)
100{
Andy Hung22667a62017-11-17 15:54:27 -0800101 dst += count;
102 src += count * 3;
Andy Hung1307aa72017-11-17 15:46:55 -0800103 for (; count > 0; --count) {
Andy Hung22667a62017-11-17 15:54:27 -0800104 src -= 3;
105 *--dst = float_from_p24(src);
Andy Hunge0454e22014-03-06 13:04:56 -0800106 }
107}
108
109void memcpy_to_i16_from_p24(int16_t *dst, const uint8_t *src, size_t count)
110{
Andy Hung1307aa72017-11-17 15:46:55 -0800111 for (; count > 0; --count) {
Andy Hung2af0e172017-03-31 14:08:14 -0700112#if HAVE_BIG_ENDIAN
Andy Hunge0454e22014-03-06 13:04:56 -0800113 *dst++ = src[1] | (src[0] << 8);
114#else
115 *dst++ = src[1] | (src[2] << 8);
116#endif
117 src += 3;
118 }
119}
120
Glenn Kasten95880cb2015-05-28 15:19:16 -0700121void memcpy_to_i32_from_p24(int32_t *dst, const uint8_t *src, size_t count)
122{
Andy Hung22667a62017-11-17 15:54:27 -0800123 dst += count;
124 src += count * 3;
Andy Hung1307aa72017-11-17 15:46:55 -0800125 for (; count > 0; --count) {
Andy Hung22667a62017-11-17 15:54:27 -0800126 src -= 3;
Andy Hung2af0e172017-03-31 14:08:14 -0700127#if HAVE_BIG_ENDIAN
Andy Hung22667a62017-11-17 15:54:27 -0800128 *--dst = (src[2] << 8) | (src[1] << 16) | (src[0] << 24);
Glenn Kasten95880cb2015-05-28 15:19:16 -0700129#else
Andy Hung22667a62017-11-17 15:54:27 -0800130 *--dst = (src[0] << 8) | (src[1] << 16) | (src[2] << 24);
Glenn Kasten95880cb2015-05-28 15:19:16 -0700131#endif
Glenn Kasten95880cb2015-05-28 15:19:16 -0700132 }
133}
134
Andy Hunge0454e22014-03-06 13:04:56 -0800135void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count)
136{
Andy Hung22667a62017-11-17 15:54:27 -0800137 dst += count * 3;
138 src += count;
Andy Hung1307aa72017-11-17 15:46:55 -0800139 for (; count > 0; --count) {
Andy Hung22667a62017-11-17 15:54:27 -0800140 dst -= 3;
Andy Hung2af0e172017-03-31 14:08:14 -0700141#if HAVE_BIG_ENDIAN
Andy Hung22667a62017-11-17 15:54:27 -0800142 dst[0] = *--src >> 8;
143 dst[1] = *src;
144 dst[2] = 0;
Andy Hunge0454e22014-03-06 13:04:56 -0800145#else
Andy Hung22667a62017-11-17 15:54:27 -0800146 dst[0] = 0;
147 dst[1] = *--src;
148 dst[2] = *src >> 8;
Andy Hunge0454e22014-03-06 13:04:56 -0800149#endif
150 }
151}
152
153void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count)
154{
Andy Hung1307aa72017-11-17 15:46:55 -0800155 for (; count > 0; --count) {
Andy Hunge0454e22014-03-06 13:04:56 -0800156 int32_t ival = clamp24_from_float(*src++);
157
Andy Hung2af0e172017-03-31 14:08:14 -0700158#if HAVE_BIG_ENDIAN
Andy Hunge0454e22014-03-06 13:04:56 -0800159 *dst++ = ival >> 16;
160 *dst++ = ival >> 8;
161 *dst++ = ival;
162#else
163 *dst++ = ival;
164 *dst++ = ival >> 8;
165 *dst++ = ival >> 16;
166#endif
167 }
168}
169
Glenn Kasteneee45152014-05-02 12:41:04 -0700170void memcpy_to_p24_from_q8_23(uint8_t *dst, const int32_t *src, size_t count)
171{
Andy Hung1307aa72017-11-17 15:46:55 -0800172 for (; count > 0; --count) {
Glenn Kasteneee45152014-05-02 12:41:04 -0700173 int32_t ival = clamp24_from_q8_23(*src++);
174
Andy Hung2af0e172017-03-31 14:08:14 -0700175#if HAVE_BIG_ENDIAN
Glenn Kasteneee45152014-05-02 12:41:04 -0700176 *dst++ = ival >> 16;
177 *dst++ = ival >> 8;
178 *dst++ = ival;
179#else
180 *dst++ = ival;
181 *dst++ = ival >> 8;
182 *dst++ = ival >> 16;
183#endif
184 }
185}
186
Glenn Kastendaa1a002015-05-29 16:50:24 -0700187void memcpy_to_p24_from_i32(uint8_t *dst, const int32_t *src, size_t count)
188{
Andy Hung1307aa72017-11-17 15:46:55 -0800189 for (; count > 0; --count) {
Glenn Kastendaa1a002015-05-29 16:50:24 -0700190 int32_t ival = *src++ >> 8;
191
Andy Hung2af0e172017-03-31 14:08:14 -0700192#if HAVE_BIG_ENDIAN
Glenn Kastendaa1a002015-05-29 16:50:24 -0700193 *dst++ = ival >> 16;
194 *dst++ = ival >> 8;
195 *dst++ = ival;
196#else
197 *dst++ = ival;
198 *dst++ = ival >> 8;
199 *dst++ = ival >> 16;
200#endif
201 }
202}
203
Andy Hungd5829882014-03-12 11:46:15 -0700204void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count)
205{
Andy Hung22667a62017-11-17 15:54:27 -0800206 dst += count;
207 src += count;
Andy Hung1307aa72017-11-17 15:46:55 -0800208 for (; count > 0; --count) {
Andy Hung22667a62017-11-17 15:54:27 -0800209 *--dst = (int32_t)*--src << 8;
Andy Hungd5829882014-03-12 11:46:15 -0700210 }
211}
212
213void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count)
214{
Andy Hung1307aa72017-11-17 15:46:55 -0800215 for (; count > 0; --count) {
Andy Hungd5829882014-03-12 11:46:15 -0700216 *dst++ = clamp24_from_float(*src++);
217 }
218}
219
Haynes Mathew George78ac9f82015-04-09 13:50:13 -0700220void memcpy_to_q8_23_from_p24(int32_t *dst, const uint8_t *src, size_t count)
221{
Andy Hung22667a62017-11-17 15:54:27 -0800222 dst += count;
223 src += count * 3;
Andy Hung1307aa72017-11-17 15:46:55 -0800224 for (; count > 0; --count) {
Andy Hung22667a62017-11-17 15:54:27 -0800225 src -= 3;
Andy Hung2af0e172017-03-31 14:08:14 -0700226#if HAVE_BIG_ENDIAN
Andy Hung22667a62017-11-17 15:54:27 -0800227 *--dst = (int8_t)src[0] << 16 | src[1] << 8 | src[2];
Haynes Mathew George78ac9f82015-04-09 13:50:13 -0700228#else
Andy Hung22667a62017-11-17 15:54:27 -0800229 *--dst = (int8_t)src[2] << 16 | src[1] << 8 | src[0];
Haynes Mathew George78ac9f82015-04-09 13:50:13 -0700230#endif
Haynes Mathew George78ac9f82015-04-09 13:50:13 -0700231 }
232}
233
Andy Hungb878e522014-04-04 13:05:43 -0700234void memcpy_to_q4_27_from_float(int32_t *dst, const float *src, size_t count)
235{
Andy Hung1307aa72017-11-17 15:46:55 -0800236 for (; count > 0; --count) {
Andy Hungb878e522014-04-04 13:05:43 -0700237 *dst++ = clampq4_27_from_float(*src++);
238 }
239}
240
Andy Hungd5829882014-03-12 11:46:15 -0700241void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count)
242{
Andy Hung1307aa72017-11-17 15:46:55 -0800243 for (; count > 0; --count) {
Andy Hungd5829882014-03-12 11:46:15 -0700244 *dst++ = clamp16(*src++ >> 8);
245 }
246}
247
248void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count)
249{
Andy Hung1307aa72017-11-17 15:46:55 -0800250 for (; count > 0; --count) {
Andy Hungd5829882014-03-12 11:46:15 -0700251 *dst++ = float_from_q8_23(*src++);
252 }
253}
254
Andy Hung2c63fb62014-03-12 14:44:12 -0700255void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count)
256{
Andy Hung22667a62017-11-17 15:54:27 -0800257 dst += count;
258 src += count;
Andy Hung1307aa72017-11-17 15:46:55 -0800259 for (; count > 0; --count) {
Andy Hung22667a62017-11-17 15:54:27 -0800260 *--dst = (int32_t)*--src << 16;
Andy Hung2c63fb62014-03-12 14:44:12 -0700261 }
262}
263
264void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count)
265{
Andy Hung1307aa72017-11-17 15:46:55 -0800266 for (; count > 0; --count) {
Andy Hung2c63fb62014-03-12 14:44:12 -0700267 *dst++ = clamp32_from_float(*src++);
268 }
269}
270
271void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count)
272{
Andy Hung1307aa72017-11-17 15:46:55 -0800273 for (; count > 0; --count) {
Andy Hung2c63fb62014-03-12 14:44:12 -0700274 *dst++ = float_from_i32(*src++);
275 }
276}
277
Kevin Rocard2d98fd32017-11-09 22:12:51 -0800278void memcpy_to_float_from_float_with_clamping(float *dst, const float *src, size_t count,
279 float absMax) {
280 // Note: using NEON intrinsics (vminq_f32, vld1q_f32...) did NOT accelerate
281 // the function when benchmarked. The compiler already vectorize using FMINNM f32x4 & similar.
282 // Note: clamping induce a ~20% overhead compared to memcpy for count in [64, 512]
283 // See primitives_benchmark
Andy Hung1307aa72017-11-17 15:46:55 -0800284 for (; count > 0; --count) {
Kevin Rocard2d98fd32017-11-09 22:12:51 -0800285 const float sample = *src++;
286 *dst++ = fmax(-absMax, fmin(absMax, sample));
287 }
288}
289
Glenn Kasten7a0baca2012-07-19 13:59:50 -0700290void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count)
291{
Andy Hung1307aa72017-11-17 15:46:55 -0800292 for (; count > 0; --count) {
Glenn Kasten7a0baca2012-07-19 13:59:50 -0700293 *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
294 src += 2;
295 }
296}
297
298void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count)
299{
Andy Hung22667a62017-11-17 15:54:27 -0800300 dst += count * 2;
301 src += count;
Andy Hung1307aa72017-11-17 15:46:55 -0800302 for (; count > 0; --count) {
Andy Hung22667a62017-11-17 15:54:27 -0800303 const int32_t temp = *--src;
304 dst -= 2;
Glenn Kasten7a0baca2012-07-19 13:59:50 -0700305 dst[0] = temp;
306 dst[1] = temp;
Glenn Kasten7a0baca2012-07-19 13:59:50 -0700307 }
308}
Glenn Kasteneb247df2014-02-21 10:00:51 -0800309
Andy Hung9c8dd452015-04-21 13:21:36 -0700310void downmix_to_mono_float_from_stereo_float(float *dst, const float *src, size_t frames)
311{
Andy Hung1307aa72017-11-17 15:46:55 -0800312 for (; frames > 0; --frames) {
Andy Hung9c8dd452015-04-21 13:21:36 -0700313 *dst++ = (src[0] + src[1]) * 0.5;
314 src += 2;
315 }
316}
317
318void upmix_to_stereo_float_from_mono_float(float *dst, const float *src, size_t frames)
319{
Andy Hung22667a62017-11-17 15:54:27 -0800320 dst += frames * 2;
321 src += frames;
Andy Hung1307aa72017-11-17 15:46:55 -0800322 for (; frames > 0; --frames) {
Andy Hung22667a62017-11-17 15:54:27 -0800323 const float temp = *--src;
324 dst -= 2;
Andy Hung9c8dd452015-04-21 13:21:36 -0700325 dst[0] = temp;
326 dst[1] = temp;
Andy Hung9c8dd452015-04-21 13:21:36 -0700327 }
328}
329
Glenn Kasteneb247df2014-02-21 10:00:51 -0800330size_t nonZeroMono32(const int32_t *samples, size_t count)
331{
332 size_t nonZero = 0;
Andy Hung1307aa72017-11-17 15:46:55 -0800333 for (; count > 0; --count) {
Andy Hung0ebba4b2017-11-17 16:02:19 -0800334 nonZero += *samples++ != 0;
Glenn Kasteneb247df2014-02-21 10:00:51 -0800335 }
336 return nonZero;
337}
338
339size_t nonZeroMono16(const int16_t *samples, size_t count)
340{
341 size_t nonZero = 0;
Andy Hung1307aa72017-11-17 15:46:55 -0800342 for (; count > 0; --count) {
Andy Hung0ebba4b2017-11-17 16:02:19 -0800343 nonZero += *samples++ != 0;
Glenn Kasteneb247df2014-02-21 10:00:51 -0800344 }
345 return nonZero;
346}
347
348size_t nonZeroStereo32(const int32_t *frames, size_t count)
349{
350 size_t nonZero = 0;
Andy Hung1307aa72017-11-17 15:46:55 -0800351 for (; count > 0; --count) {
Andy Hung0ebba4b2017-11-17 16:02:19 -0800352 nonZero += frames[0] != 0 || frames[1] != 0;
Glenn Kasteneb247df2014-02-21 10:00:51 -0800353 frames += 2;
354 }
355 return nonZero;
356}
357
358size_t nonZeroStereo16(const int16_t *frames, size_t count)
359{
360 size_t nonZero = 0;
Andy Hung1307aa72017-11-17 15:46:55 -0800361 for (; count > 0; --count) {
Andy Hung0ebba4b2017-11-17 16:02:19 -0800362 nonZero += frames[0] != 0 || frames[1] != 0;
Glenn Kasteneb247df2014-02-21 10:00:51 -0800363 frames += 2;
364 }
365 return nonZero;
366}
Andy Hung3af2af22014-05-22 18:40:30 -0700367
Andy Hung3af2af22014-05-22 18:40:30 -0700368/*
369 * C macro to do channel mask copying independent of dst/src sample type.
370 * Don't pass in any expressions for the macro arguments here.
371 */
372#define copy_frame_by_mask(dst, dmask, src, smask, count, zero) \
373{ \
374 uint32_t bit, ormask; \
Andy Hung1307aa72017-11-17 15:46:55 -0800375 for (; (count) > 0; --(count)) { \
Chih-Hung Hsieh2c853ac2016-05-11 15:13:31 -0700376 ormask = (dmask) | (smask); \
Andy Hung3af2af22014-05-22 18:40:30 -0700377 while (ormask) { \
378 bit = ormask & -ormask; /* get lowest bit */ \
379 ormask ^= bit; /* remove lowest bit */ \
Chih-Hung Hsieh2c853ac2016-05-11 15:13:31 -0700380 if ((dmask) & bit) { \
381 *(dst)++ = (smask) & bit ? *(src)++ : (zero); \
Andy Hung3af2af22014-05-22 18:40:30 -0700382 } else { /* source channel only */ \
Chih-Hung Hsieh2c853ac2016-05-11 15:13:31 -0700383 ++(src); \
Andy Hung3af2af22014-05-22 18:40:30 -0700384 } \
385 } \
386 } \
387}
388
389void memcpy_by_channel_mask(void *dst, uint32_t dst_mask,
390 const void *src, uint32_t src_mask, size_t sample_size, size_t count)
391{
392#if 0
393 /* alternate way of handling memcpy_by_channel_mask by using the idxary */
394 int8_t idxary[32];
395 uint32_t src_channels = popcount(src_mask);
396 uint32_t dst_channels =
397 memcpy_by_index_array_initialization(idxary, 32, dst_mask, src_mask);
398
399 memcpy_by_idxary(dst, dst_channels, src, src_channels, idxary, sample_size, count);
400#else
401 if (dst_mask == src_mask) {
402 memcpy(dst, src, sample_size * popcount(dst_mask) * count);
403 return;
404 }
405 switch (sample_size) {
406 case 1: {
407 uint8_t *udst = (uint8_t*)dst;
408 const uint8_t *usrc = (const uint8_t*)src;
409
410 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
411 } break;
412 case 2: {
413 uint16_t *udst = (uint16_t*)dst;
414 const uint16_t *usrc = (const uint16_t*)src;
415
416 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
417 } break;
418 case 3: { /* could be slow. use a struct to represent 3 bytes of data. */
419 uint8x3_t *udst = (uint8x3_t*)dst;
420 const uint8x3_t *usrc = (const uint8x3_t*)src;
421 static const uint8x3_t zero; /* tricky - we use this to zero out a sample */
422
423 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, zero);
424 } break;
425 case 4: {
426 uint32_t *udst = (uint32_t*)dst;
427 const uint32_t *usrc = (const uint32_t*)src;
428
429 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
430 } break;
431 default:
432 abort(); /* illegal value */
433 break;
434 }
435#endif
436}
437
438/*
439 * C macro to do copying by index array, to rearrange samples
440 * within a frame. This is independent of src/dst sample type.
441 * Don't pass in any expressions for the macro arguments here.
442 */
443#define copy_frame_by_idx(dst, dst_channels, src, src_channels, idxary, count, zero) \
444{ \
445 unsigned i; \
446 int index; \
Andy Hung1307aa72017-11-17 15:46:55 -0800447 for (; (count) > 0; --(count)) { \
Chih-Hung Hsieh2c853ac2016-05-11 15:13:31 -0700448 for (i = 0; i < (dst_channels); ++i) { \
449 index = (idxary)[i]; \
450 *(dst)++ = index < 0 ? (zero) : (src)[index]; \
Andy Hung3af2af22014-05-22 18:40:30 -0700451 } \
Chih-Hung Hsieh2c853ac2016-05-11 15:13:31 -0700452 (src) += (src_channels); \
Andy Hung3af2af22014-05-22 18:40:30 -0700453 } \
454}
455
456void memcpy_by_index_array(void *dst, uint32_t dst_channels,
457 const void *src, uint32_t src_channels,
458 const int8_t *idxary, size_t sample_size, size_t count)
459{
460 switch (sample_size) {
461 case 1: {
462 uint8_t *udst = (uint8_t*)dst;
463 const uint8_t *usrc = (const uint8_t*)src;
464
465 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
466 } break;
467 case 2: {
468 uint16_t *udst = (uint16_t*)dst;
469 const uint16_t *usrc = (const uint16_t*)src;
470
471 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
472 } break;
473 case 3: { /* could be slow. use a struct to represent 3 bytes of data. */
474 uint8x3_t *udst = (uint8x3_t*)dst;
475 const uint8x3_t *usrc = (const uint8x3_t*)src;
476 static const uint8x3_t zero;
477
478 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, zero);
479 } break;
480 case 4: {
481 uint32_t *udst = (uint32_t*)dst;
482 const uint32_t *usrc = (const uint32_t*)src;
483
484 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
485 } break;
486 default:
487 abort(); /* illegal value */
488 break;
489 }
490}
491
492size_t memcpy_by_index_array_initialization(int8_t *idxary, size_t idxcount,
493 uint32_t dst_mask, uint32_t src_mask)
494{
495 size_t n = 0;
496 int srcidx = 0;
497 uint32_t bit, ormask = src_mask | dst_mask;
498
499 while (ormask && n < idxcount) {
500 bit = ormask & -ormask; /* get lowest bit */
501 ormask ^= bit; /* remove lowest bit */
502 if (src_mask & dst_mask & bit) { /* matching channel */
503 idxary[n++] = srcidx++;
504 } else if (src_mask & bit) { /* source channel only */
505 ++srcidx;
506 } else { /* destination channel only */
507 idxary[n++] = -1;
508 }
509 }
510 return n + popcount(ormask & dst_mask);
511}
Andy Hung5a0d0282015-02-02 15:34:13 -0800512
513size_t memcpy_by_index_array_initialization_src_index(int8_t *idxary, size_t idxcount,
514 uint32_t dst_mask, uint32_t src_mask) {
515 size_t dst_count = popcount(dst_mask);
516 if (idxcount == 0) {
517 return dst_count;
518 }
519 if (dst_count > idxcount) {
520 dst_count = idxcount;
521 }
522
523 size_t src_idx, dst_idx;
524 for (src_idx = 0, dst_idx = 0; dst_idx < dst_count; ++dst_idx) {
525 if (src_mask & 1) {
526 idxary[dst_idx] = src_idx++;
527 } else {
528 idxary[dst_idx] = -1;
529 }
530 src_mask >>= 1;
531 }
532 return dst_idx;
533}
Andy Hung291a1942015-02-27 14:20:33 -0800534
535size_t memcpy_by_index_array_initialization_dst_index(int8_t *idxary, size_t idxcount,
536 uint32_t dst_mask, uint32_t src_mask) {
537 size_t src_idx, dst_idx;
538 size_t dst_count = __builtin_popcount(dst_mask);
539 size_t src_count = __builtin_popcount(src_mask);
540 if (idxcount == 0) {
541 return dst_count;
542 }
543 if (dst_count > idxcount) {
544 dst_count = idxcount;
545 }
546 for (src_idx = 0, dst_idx = 0; dst_idx < dst_count; ++src_idx) {
547 if (dst_mask & 1) {
548 idxary[dst_idx++] = src_idx < src_count ? (signed)src_idx : -1;
549 }
550 dst_mask >>= 1;
551 }
552 return dst_idx;
553}
Ari Hausman-Cohen90ac5f62017-08-16 18:32:22 -0700554
555void accumulate_i16(int16_t *dst, const int16_t *src, size_t count) {
556 while (count--) {
557 *dst = clamp16((int32_t)*dst + *src++);
558 ++dst;
559 }
560}
561
562void accumulate_u8(uint8_t *dst, const uint8_t *src, size_t count) {
563 int32_t sum;
Andy Hung1307aa72017-11-17 15:46:55 -0800564 for (; count > 0; --count) {
Ari Hausman-Cohen90ac5f62017-08-16 18:32:22 -0700565 // 8-bit samples are centered around 0x80.
566 sum = *dst + *src++ - 0x80;
567 // Clamp to [0, 0xff].
568 *dst++ = (sum & 0x100) ? (~sum >> 9) : sum;
569 }
570}
571
572void accumulate_p24(uint8_t *dst, const uint8_t *src, size_t count) {
Andy Hung1307aa72017-11-17 15:46:55 -0800573 for (; count > 0; --count) {
Ari Hausman-Cohen90ac5f62017-08-16 18:32:22 -0700574 // Unpack.
575 int32_t dst_q8_23 = 0;
576 int32_t src_q8_23 = 0;
577 memcpy_to_q8_23_from_p24(&dst_q8_23, dst, 1);
578 memcpy_to_q8_23_from_p24(&src_q8_23, src, 1);
579
580 // Accumulate and overwrite.
581 dst_q8_23 += src_q8_23;
582 memcpy_to_p24_from_q8_23(dst, &dst_q8_23, 1);
583
584 // Move on to next sample.
585 dst += 3;
586 src += 3;
587 }
588}
589
590void accumulate_q8_23(int32_t *dst, const int32_t *src, size_t count) {
Andy Hung1307aa72017-11-17 15:46:55 -0800591 for (; count > 0; --count) {
Ari Hausman-Cohen90ac5f62017-08-16 18:32:22 -0700592 *dst = clamp24_from_q8_23(*dst + *src++);
593 ++dst;
594 }
595}
596
597void accumulate_i32(int32_t *dst, const int32_t *src, size_t count) {
Andy Hung1307aa72017-11-17 15:46:55 -0800598 for (; count > 0; --count) {
Ari Hausman-Cohen90ac5f62017-08-16 18:32:22 -0700599 *dst = clamp32((int64_t)*dst + *src++);
600 ++dst;
601 }
602}
603
604void accumulate_float(float *dst, const float *src, size_t count) {
Andy Hung1307aa72017-11-17 15:46:55 -0800605 for (; count > 0; --count) {
Ari Hausman-Cohen90ac5f62017-08-16 18:32:22 -0700606 *dst++ += *src++;
607 }
608}