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_i16_from_i32(int16_t *dst, const int32_t *src, size_t count)
54 *dst++ = *src++ >> 16;
58 void memcpy_to_i16_from_float(int16_t *dst, const float *src, size_t count)
61 *dst++ = clamp16_from_float(*src++);
65 void memcpy_to_float_from_q4_27(float *dst, const int32_t *src, size_t count)
68 *dst++ = float_from_q4_27(*src++);
72 void memcpy_to_float_from_i16(float *dst, const int16_t *src, size_t count)
75 *dst++ = float_from_i16(*src++);
79 void memcpy_to_float_from_p24(float *dst, const uint8_t *src, size_t count)
82 *dst++ = float_from_p24(src);
87 void memcpy_to_i16_from_p24(int16_t *dst, const uint8_t *src, size_t count)
90 #ifdef HAVE_BIG_ENDIAN
91 *dst++ = src[1] | (src[0] << 8);
93 *dst++ = src[1] | (src[2] << 8);
99 void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count)
102 #ifdef HAVE_BIG_ENDIAN
109 *dst++ = *src++ >> 8;
114 void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count)
117 int32_t ival = clamp24_from_float(*src++);
119 #ifdef HAVE_BIG_ENDIAN
131 void memcpy_to_p24_from_q8_23(uint8_t *dst, const int32_t *src, size_t count)
134 int32_t ival = clamp24_from_q8_23(*src++);
136 #ifdef HAVE_BIG_ENDIAN
148 void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count)
151 *dst++ = (int32_t)*src++ << 8;
155 void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count)
158 *dst++ = clamp24_from_float(*src++);
162 void memcpy_to_q4_27_from_float(int32_t *dst, const float *src, size_t count)
165 *dst++ = clampq4_27_from_float(*src++);
169 void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count)
172 *dst++ = clamp16(*src++ >> 8);
176 void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count)
179 *dst++ = float_from_q8_23(*src++);
183 void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count)
186 *dst++ = (int32_t)*src++ << 16;
190 void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count)
193 *dst++ = clamp32_from_float(*src++);
197 void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count)
200 *dst++ = float_from_i32(*src++);
204 void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count)
207 *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
212 void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count)
215 int32_t temp = *src++;
222 size_t nonZeroMono32(const int32_t *samples, size_t count)
225 while (count-- > 0) {
226 if (*samples++ != 0) {
233 size_t nonZeroMono16(const int16_t *samples, size_t count)
236 while (count-- > 0) {
237 if (*samples++ != 0) {
244 size_t nonZeroStereo32(const int32_t *frames, size_t count)
247 while (count-- > 0) {
248 if (frames[0] != 0 || frames[1] != 0) {
256 size_t nonZeroStereo16(const int16_t *frames, size_t count)
259 while (count-- > 0) {
260 if (frames[0] != 0 || frames[1] != 0) {
269 * C macro to do channel mask copying independent of dst/src sample type.
270 * Don't pass in any expressions for the macro arguments here.
272 #define copy_frame_by_mask(dst, dmask, src, smask, count, zero) \
274 uint32_t bit, ormask; \
276 ormask = dmask | smask; \
278 bit = ormask & -ormask; /* get lowest bit */ \
279 ormask ^= bit; /* remove lowest bit */ \
281 *dst++ = smask & bit ? *src++ : zero; \
282 } else { /* source channel only */ \
289 void memcpy_by_channel_mask(void *dst, uint32_t dst_mask,
290 const void *src, uint32_t src_mask, size_t sample_size, size_t count)
293 /* alternate way of handling memcpy_by_channel_mask by using the idxary */
295 uint32_t src_channels = popcount(src_mask);
296 uint32_t dst_channels =
297 memcpy_by_index_array_initialization(idxary, 32, dst_mask, src_mask);
299 memcpy_by_idxary(dst, dst_channels, src, src_channels, idxary, sample_size, count);
301 if (dst_mask == src_mask) {
302 memcpy(dst, src, sample_size * popcount(dst_mask) * count);
305 switch (sample_size) {
307 uint8_t *udst = (uint8_t*)dst;
308 const uint8_t *usrc = (const uint8_t*)src;
310 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
313 uint16_t *udst = (uint16_t*)dst;
314 const uint16_t *usrc = (const uint16_t*)src;
316 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
318 case 3: { /* could be slow. use a struct to represent 3 bytes of data. */
319 uint8x3_t *udst = (uint8x3_t*)dst;
320 const uint8x3_t *usrc = (const uint8x3_t*)src;
321 static const uint8x3_t zero; /* tricky - we use this to zero out a sample */
323 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, zero);
326 uint32_t *udst = (uint32_t*)dst;
327 const uint32_t *usrc = (const uint32_t*)src;
329 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
332 abort(); /* illegal value */
339 * C macro to do copying by index array, to rearrange samples
340 * within a frame. This is independent of src/dst sample type.
341 * Don't pass in any expressions for the macro arguments here.
343 #define copy_frame_by_idx(dst, dst_channels, src, src_channels, idxary, count, zero) \
348 for (i = 0; i < dst_channels; ++i) { \
350 *dst++ = index < 0 ? zero : src[index]; \
352 src += src_channels; \
356 void memcpy_by_index_array(void *dst, uint32_t dst_channels,
357 const void *src, uint32_t src_channels,
358 const int8_t *idxary, size_t sample_size, size_t count)
360 switch (sample_size) {
362 uint8_t *udst = (uint8_t*)dst;
363 const uint8_t *usrc = (const uint8_t*)src;
365 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
368 uint16_t *udst = (uint16_t*)dst;
369 const uint16_t *usrc = (const uint16_t*)src;
371 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
373 case 3: { /* could be slow. use a struct to represent 3 bytes of data. */
374 uint8x3_t *udst = (uint8x3_t*)dst;
375 const uint8x3_t *usrc = (const uint8x3_t*)src;
376 static const uint8x3_t zero;
378 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, zero);
381 uint32_t *udst = (uint32_t*)dst;
382 const uint32_t *usrc = (const uint32_t*)src;
384 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
387 abort(); /* illegal value */
392 size_t memcpy_by_index_array_initialization(int8_t *idxary, size_t idxcount,
393 uint32_t dst_mask, uint32_t src_mask)
397 uint32_t bit, ormask = src_mask | dst_mask;
399 while (ormask && n < idxcount) {
400 bit = ormask & -ormask; /* get lowest bit */
401 ormask ^= bit; /* remove lowest bit */
402 if (src_mask & dst_mask & bit) { /* matching channel */
403 idxary[n++] = srcidx++;
404 } else if (src_mask & bit) { /* source channel only */
406 } else { /* destination channel only */
410 return n + popcount(ormask & dst_mask);