2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <cutils/bitops.h> /* for popcount() */
18 #include <audio_utils/primitives.h>
19 #include "private/private.h"
21 void ditherAndClamp(int32_t* out, const int32_t *sums, size_t c)
24 for (i=0 ; i<c ; i++) {
31 *out++ = (r<<16) | (l & 0xFFFF);
35 void memcpy_to_i16_from_u8(int16_t *dst, const uint8_t *src, size_t count)
40 *--dst = (int16_t)(*--src - 0x80) << 8;
44 void memcpy_to_u8_from_i16(uint8_t *dst, const int16_t *src, size_t count)
47 *dst++ = (*src++ >> 8) + 0x80;
51 void memcpy_to_u8_from_float(uint8_t *dst, const float *src, size_t count)
54 *dst++ = clamp8_from_float(*src++);
58 void memcpy_to_i16_from_i32(int16_t *dst, const int32_t *src, size_t count)
61 *dst++ = *src++ >> 16;
65 void memcpy_to_i16_from_float(int16_t *dst, const float *src, size_t count)
68 *dst++ = clamp16_from_float(*src++);
72 void memcpy_to_float_from_q4_27(float *dst, const int32_t *src, size_t count)
75 *dst++ = float_from_q4_27(*src++);
79 void memcpy_to_float_from_i16(float *dst, const int16_t *src, size_t count)
82 *dst++ = float_from_i16(*src++);
86 void memcpy_to_float_from_u8(float *dst, const uint8_t *src, size_t count)
89 *dst++ = float_from_u8(*src++);
93 void memcpy_to_float_from_p24(float *dst, const uint8_t *src, size_t count)
96 *dst++ = float_from_p24(src);
101 void memcpy_to_i16_from_p24(int16_t *dst, const uint8_t *src, size_t count)
105 *dst++ = src[1] | (src[0] << 8);
107 *dst++ = src[1] | (src[2] << 8);
113 void memcpy_to_i32_from_p24(int32_t *dst, const uint8_t *src, size_t count)
117 *dst++ = (src[2] << 8) | (src[1] << 16) | (src[0] << 24);
119 *dst++ = (src[0] << 8) | (src[1] << 16) | (src[2] << 24);
125 void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count)
135 *dst++ = *src++ >> 8;
140 void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count)
143 int32_t ival = clamp24_from_float(*src++);
157 void memcpy_to_p24_from_q8_23(uint8_t *dst, const int32_t *src, size_t count)
160 int32_t ival = clamp24_from_q8_23(*src++);
174 void memcpy_to_p24_from_i32(uint8_t *dst, const int32_t *src, size_t count)
177 int32_t ival = *src++ >> 8;
191 void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count)
194 *dst++ = (int32_t)*src++ << 8;
198 void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count)
201 *dst++ = clamp24_from_float(*src++);
205 void memcpy_to_q8_23_from_p24(int32_t *dst, const uint8_t *src, size_t count)
209 *dst++ = (int8_t)src[0] << 16 | src[1] << 8 | src[2];
211 *dst++ = (int8_t)src[2] << 16 | src[1] << 8 | src[0];
217 void memcpy_to_q4_27_from_float(int32_t *dst, const float *src, size_t count)
220 *dst++ = clampq4_27_from_float(*src++);
224 void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count)
227 *dst++ = clamp16(*src++ >> 8);
231 void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count)
234 *dst++ = float_from_q8_23(*src++);
238 void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count)
241 *dst++ = (int32_t)*src++ << 16;
245 void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count)
248 *dst++ = clamp32_from_float(*src++);
252 void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count)
255 *dst++ = float_from_i32(*src++);
259 void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count)
262 *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
267 void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count)
270 int32_t temp = *src++;
277 void downmix_to_mono_float_from_stereo_float(float *dst, const float *src, size_t frames)
280 *dst++ = (src[0] + src[1]) * 0.5;
285 void upmix_to_stereo_float_from_mono_float(float *dst, const float *src, size_t frames)
295 size_t nonZeroMono32(const int32_t *samples, size_t count)
298 while (count-- > 0) {
299 if (*samples++ != 0) {
306 size_t nonZeroMono16(const int16_t *samples, size_t count)
309 while (count-- > 0) {
310 if (*samples++ != 0) {
317 size_t nonZeroStereo32(const int32_t *frames, size_t count)
320 while (count-- > 0) {
321 if (frames[0] != 0 || frames[1] != 0) {
329 size_t nonZeroStereo16(const int16_t *frames, size_t count)
332 while (count-- > 0) {
333 if (frames[0] != 0 || frames[1] != 0) {
342 * C macro to do channel mask copying independent of dst/src sample type.
343 * Don't pass in any expressions for the macro arguments here.
345 #define copy_frame_by_mask(dst, dmask, src, smask, count, zero) \
347 uint32_t bit, ormask; \
348 while ((count)--) { \
349 ormask = (dmask) | (smask); \
351 bit = ormask & -ormask; /* get lowest bit */ \
352 ormask ^= bit; /* remove lowest bit */ \
353 if ((dmask) & bit) { \
354 *(dst)++ = (smask) & bit ? *(src)++ : (zero); \
355 } else { /* source channel only */ \
362 void memcpy_by_channel_mask(void *dst, uint32_t dst_mask,
363 const void *src, uint32_t src_mask, size_t sample_size, size_t count)
366 /* alternate way of handling memcpy_by_channel_mask by using the idxary */
368 uint32_t src_channels = popcount(src_mask);
369 uint32_t dst_channels =
370 memcpy_by_index_array_initialization(idxary, 32, dst_mask, src_mask);
372 memcpy_by_idxary(dst, dst_channels, src, src_channels, idxary, sample_size, count);
374 if (dst_mask == src_mask) {
375 memcpy(dst, src, sample_size * popcount(dst_mask) * count);
378 switch (sample_size) {
380 uint8_t *udst = (uint8_t*)dst;
381 const uint8_t *usrc = (const uint8_t*)src;
383 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
386 uint16_t *udst = (uint16_t*)dst;
387 const uint16_t *usrc = (const uint16_t*)src;
389 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
391 case 3: { /* could be slow. use a struct to represent 3 bytes of data. */
392 uint8x3_t *udst = (uint8x3_t*)dst;
393 const uint8x3_t *usrc = (const uint8x3_t*)src;
394 static const uint8x3_t zero; /* tricky - we use this to zero out a sample */
396 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, zero);
399 uint32_t *udst = (uint32_t*)dst;
400 const uint32_t *usrc = (const uint32_t*)src;
402 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
405 abort(); /* illegal value */
412 * C macro to do copying by index array, to rearrange samples
413 * within a frame. This is independent of src/dst sample type.
414 * Don't pass in any expressions for the macro arguments here.
416 #define copy_frame_by_idx(dst, dst_channels, src, src_channels, idxary, count, zero) \
420 while ((count)--) { \
421 for (i = 0; i < (dst_channels); ++i) { \
422 index = (idxary)[i]; \
423 *(dst)++ = index < 0 ? (zero) : (src)[index]; \
425 (src) += (src_channels); \
429 void memcpy_by_index_array(void *dst, uint32_t dst_channels,
430 const void *src, uint32_t src_channels,
431 const int8_t *idxary, size_t sample_size, size_t count)
433 switch (sample_size) {
435 uint8_t *udst = (uint8_t*)dst;
436 const uint8_t *usrc = (const uint8_t*)src;
438 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
441 uint16_t *udst = (uint16_t*)dst;
442 const uint16_t *usrc = (const uint16_t*)src;
444 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
446 case 3: { /* could be slow. use a struct to represent 3 bytes of data. */
447 uint8x3_t *udst = (uint8x3_t*)dst;
448 const uint8x3_t *usrc = (const uint8x3_t*)src;
449 static const uint8x3_t zero;
451 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, zero);
454 uint32_t *udst = (uint32_t*)dst;
455 const uint32_t *usrc = (const uint32_t*)src;
457 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
460 abort(); /* illegal value */
465 size_t memcpy_by_index_array_initialization(int8_t *idxary, size_t idxcount,
466 uint32_t dst_mask, uint32_t src_mask)
470 uint32_t bit, ormask = src_mask | dst_mask;
472 while (ormask && n < idxcount) {
473 bit = ormask & -ormask; /* get lowest bit */
474 ormask ^= bit; /* remove lowest bit */
475 if (src_mask & dst_mask & bit) { /* matching channel */
476 idxary[n++] = srcidx++;
477 } else if (src_mask & bit) { /* source channel only */
479 } else { /* destination channel only */
483 return n + popcount(ormask & dst_mask);
486 size_t memcpy_by_index_array_initialization_src_index(int8_t *idxary, size_t idxcount,
487 uint32_t dst_mask, uint32_t src_mask) {
488 size_t dst_count = popcount(dst_mask);
492 if (dst_count > idxcount) {
493 dst_count = idxcount;
496 size_t src_idx, dst_idx;
497 for (src_idx = 0, dst_idx = 0; dst_idx < dst_count; ++dst_idx) {
499 idxary[dst_idx] = src_idx++;
501 idxary[dst_idx] = -1;
508 size_t memcpy_by_index_array_initialization_dst_index(int8_t *idxary, size_t idxcount,
509 uint32_t dst_mask, uint32_t src_mask) {
510 size_t src_idx, dst_idx;
511 size_t dst_count = __builtin_popcount(dst_mask);
512 size_t src_count = __builtin_popcount(src_mask);
516 if (dst_count > idxcount) {
517 dst_count = idxcount;
519 for (src_idx = 0, dst_idx = 0; dst_idx < dst_count; ++src_idx) {
521 idxary[dst_idx++] = src_idx < src_count ? (signed)src_idx : -1;