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)
104 #ifdef HAVE_BIG_ENDIAN
105 *dst++ = src[1] | (src[0] << 8);
107 *dst++ = src[1] | (src[2] << 8);
113 void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count)
116 #ifdef HAVE_BIG_ENDIAN
123 *dst++ = *src++ >> 8;
128 void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count)
131 int32_t ival = clamp24_from_float(*src++);
133 #ifdef HAVE_BIG_ENDIAN
145 void memcpy_to_p24_from_q8_23(uint8_t *dst, const int32_t *src, size_t count)
148 int32_t ival = clamp24_from_q8_23(*src++);
150 #ifdef HAVE_BIG_ENDIAN
162 void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count)
165 *dst++ = (int32_t)*src++ << 8;
169 void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count)
172 *dst++ = clamp24_from_float(*src++);
176 void memcpy_to_q4_27_from_float(int32_t *dst, const float *src, size_t count)
179 *dst++ = clampq4_27_from_float(*src++);
183 void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count)
186 *dst++ = clamp16(*src++ >> 8);
190 void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count)
193 *dst++ = float_from_q8_23(*src++);
197 void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count)
200 *dst++ = (int32_t)*src++ << 16;
204 void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count)
207 *dst++ = clamp32_from_float(*src++);
211 void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count)
214 *dst++ = float_from_i32(*src++);
218 void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count)
221 *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
226 void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count)
229 int32_t temp = *src++;
236 size_t nonZeroMono32(const int32_t *samples, size_t count)
239 while (count-- > 0) {
240 if (*samples++ != 0) {
247 size_t nonZeroMono16(const int16_t *samples, size_t count)
250 while (count-- > 0) {
251 if (*samples++ != 0) {
258 size_t nonZeroStereo32(const int32_t *frames, size_t count)
261 while (count-- > 0) {
262 if (frames[0] != 0 || frames[1] != 0) {
270 size_t nonZeroStereo16(const int16_t *frames, size_t count)
273 while (count-- > 0) {
274 if (frames[0] != 0 || frames[1] != 0) {
283 * C macro to do channel mask copying independent of dst/src sample type.
284 * Don't pass in any expressions for the macro arguments here.
286 #define copy_frame_by_mask(dst, dmask, src, smask, count, zero) \
288 uint32_t bit, ormask; \
290 ormask = dmask | smask; \
292 bit = ormask & -ormask; /* get lowest bit */ \
293 ormask ^= bit; /* remove lowest bit */ \
295 *dst++ = smask & bit ? *src++ : zero; \
296 } else { /* source channel only */ \
303 void memcpy_by_channel_mask(void *dst, uint32_t dst_mask,
304 const void *src, uint32_t src_mask, size_t sample_size, size_t count)
307 /* alternate way of handling memcpy_by_channel_mask by using the idxary */
309 uint32_t src_channels = popcount(src_mask);
310 uint32_t dst_channels =
311 memcpy_by_index_array_initialization(idxary, 32, dst_mask, src_mask);
313 memcpy_by_idxary(dst, dst_channels, src, src_channels, idxary, sample_size, count);
315 if (dst_mask == src_mask) {
316 memcpy(dst, src, sample_size * popcount(dst_mask) * count);
319 switch (sample_size) {
321 uint8_t *udst = (uint8_t*)dst;
322 const uint8_t *usrc = (const uint8_t*)src;
324 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
327 uint16_t *udst = (uint16_t*)dst;
328 const uint16_t *usrc = (const uint16_t*)src;
330 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
332 case 3: { /* could be slow. use a struct to represent 3 bytes of data. */
333 uint8x3_t *udst = (uint8x3_t*)dst;
334 const uint8x3_t *usrc = (const uint8x3_t*)src;
335 static const uint8x3_t zero; /* tricky - we use this to zero out a sample */
337 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, zero);
340 uint32_t *udst = (uint32_t*)dst;
341 const uint32_t *usrc = (const uint32_t*)src;
343 copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0);
346 abort(); /* illegal value */
353 * C macro to do copying by index array, to rearrange samples
354 * within a frame. This is independent of src/dst sample type.
355 * Don't pass in any expressions for the macro arguments here.
357 #define copy_frame_by_idx(dst, dst_channels, src, src_channels, idxary, count, zero) \
362 for (i = 0; i < dst_channels; ++i) { \
364 *dst++ = index < 0 ? zero : src[index]; \
366 src += src_channels; \
370 void memcpy_by_index_array(void *dst, uint32_t dst_channels,
371 const void *src, uint32_t src_channels,
372 const int8_t *idxary, size_t sample_size, size_t count)
374 switch (sample_size) {
376 uint8_t *udst = (uint8_t*)dst;
377 const uint8_t *usrc = (const uint8_t*)src;
379 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
382 uint16_t *udst = (uint16_t*)dst;
383 const uint16_t *usrc = (const uint16_t*)src;
385 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
387 case 3: { /* could be slow. use a struct to represent 3 bytes of data. */
388 uint8x3_t *udst = (uint8x3_t*)dst;
389 const uint8x3_t *usrc = (const uint8x3_t*)src;
390 static const uint8x3_t zero;
392 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, zero);
395 uint32_t *udst = (uint32_t*)dst;
396 const uint32_t *usrc = (const uint32_t*)src;
398 copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0);
401 abort(); /* illegal value */
406 size_t memcpy_by_index_array_initialization(int8_t *idxary, size_t idxcount,
407 uint32_t dst_mask, uint32_t src_mask)
411 uint32_t bit, ormask = src_mask | dst_mask;
413 while (ormask && n < idxcount) {
414 bit = ormask & -ormask; /* get lowest bit */
415 ormask ^= bit; /* remove lowest bit */
416 if (src_mask & dst_mask & bit) { /* matching channel */
417 idxary[n++] = srcidx++;
418 } else if (src_mask & bit) { /* source channel only */
420 } else { /* destination channel only */
424 return n + popcount(ormask & dst_mask);