primitives.c revision 2c63fb60f8086000645e82cfb384cc8dfc5d1634
1/* 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 17#include <audio_utils/primitives.h> 18 19void ditherAndClamp(int32_t* out, const int32_t *sums, size_t c) 20{ 21 size_t i; 22 for (i=0 ; i<c ; i++) { 23 int32_t l = *sums++; 24 int32_t r = *sums++; 25 int32_t nl = l >> 12; 26 int32_t nr = r >> 12; 27 l = clamp16(nl); 28 r = clamp16(nr); 29 *out++ = (r<<16) | (l & 0xFFFF); 30 } 31} 32 33void memcpy_to_i16_from_u8(int16_t *dst, const uint8_t *src, size_t count) 34{ 35 dst += count; 36 src += count; 37 while (count--) { 38 *--dst = (int16_t)(*--src - 0x80) << 8; 39 } 40} 41 42void memcpy_to_u8_from_i16(uint8_t *dst, const int16_t *src, size_t count) 43{ 44 while (count--) { 45 *dst++ = (*src++ >> 8) + 0x80; 46 } 47} 48 49void memcpy_to_i16_from_i32(int16_t *dst, const int32_t *src, size_t count) 50{ 51 while (count--) { 52 *dst++ = *src++ >> 16; 53 } 54} 55 56void memcpy_to_i16_from_float(int16_t *dst, const float *src, size_t count) 57{ 58 while (count--) { 59 *dst++ = clamp16FromFloat(*src++); 60 } 61} 62 63void memcpy_to_float_from_q19_12(float *dst, const int32_t *src, size_t c) 64{ 65 size_t i; 66 for (i = 0; i < c; ++i) { 67 *dst++ = float_from_q19_12(*src++); 68 } 69} 70 71void memcpy_to_float_from_i16(float *dst, const int16_t *src, size_t count) 72{ 73 while (count--) { 74 *dst++ = float_from_i16(*src++); 75 } 76} 77 78void memcpy_to_float_from_p24(float *dst, const uint8_t *src, size_t count) 79{ 80 while (count--) { 81 *dst++ = float_from_p24(src); 82 src += 3; 83 } 84} 85 86void memcpy_to_i16_from_p24(int16_t *dst, const uint8_t *src, size_t count) 87{ 88 while (count--) { 89#ifdef HAVE_BIG_ENDIAN 90 *dst++ = src[1] | (src[0] << 8); 91#else 92 *dst++ = src[1] | (src[2] << 8); 93#endif 94 src += 3; 95 } 96} 97 98void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count) 99{ 100 while (count--) { 101#ifdef HAVE_BIG_ENDIAN 102 *dst++ = *src >> 8; 103 *dst++ = *src++; 104 *dst++ = 0; 105#else 106 *dst++ = 0; 107 *dst++ = *src; 108 *dst++ = *src++ >> 8; 109#endif 110 } 111} 112 113void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count) 114{ 115 while (count--) { 116 int32_t ival = clamp24_from_float(*src++); 117 118#ifdef HAVE_BIG_ENDIAN 119 *dst++ = ival >> 16; 120 *dst++ = ival >> 8; 121 *dst++ = ival; 122#else 123 *dst++ = ival; 124 *dst++ = ival >> 8; 125 *dst++ = ival >> 16; 126#endif 127 } 128} 129 130void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count) 131{ 132 while (count--) { 133 *dst++ = (int32_t)*src++ << 16; 134 } 135} 136 137void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count) 138{ 139 while (count--) { 140 *dst++ = clamp32_from_float(*src++); 141 } 142} 143 144void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count) 145{ 146 while (count--) { 147 *dst++ = float_from_i32(*src++); 148 } 149} 150 151void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count) 152{ 153 while (count--) { 154 *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1); 155 src += 2; 156 } 157} 158 159void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count) 160{ 161 while (count--) { 162 int32_t temp = *src++; 163 dst[0] = temp; 164 dst[1] = temp; 165 dst += 2; 166 } 167} 168 169size_t nonZeroMono32(const int32_t *samples, size_t count) 170{ 171 size_t nonZero = 0; 172 while (count-- > 0) { 173 if (*samples++ != 0) { 174 nonZero++; 175 } 176 } 177 return nonZero; 178} 179 180size_t nonZeroMono16(const int16_t *samples, size_t count) 181{ 182 size_t nonZero = 0; 183 while (count-- > 0) { 184 if (*samples++ != 0) { 185 nonZero++; 186 } 187 } 188 return nonZero; 189} 190 191size_t nonZeroStereo32(const int32_t *frames, size_t count) 192{ 193 size_t nonZero = 0; 194 while (count-- > 0) { 195 if (frames[0] != 0 || frames[1] != 0) { 196 nonZero++; 197 } 198 frames += 2; 199 } 200 return nonZero; 201} 202 203size_t nonZeroStereo16(const int16_t *frames, size_t count) 204{ 205 size_t nonZero = 0; 206 while (count-- > 0) { 207 if (frames[0] != 0 || frames[1] != 0) { 208 nonZero++; 209 } 210 frames += 2; 211 } 212 return nonZero; 213} 214