Glenn Kasten | 632e0c0 | 2011-12-16 10:42:58 -0800 | [diff] [blame] | 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 | |
Glenn Kasten | 7ef795a | 2013-07-17 07:25:05 -0700 | [diff] [blame] | 19 | void ditherAndClamp(int32_t* out, const int32_t *sums, size_t c) |
Glenn Kasten | 632e0c0 | 2011-12-16 10:42:58 -0800 | [diff] [blame] | 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 | } |
Glenn Kasten | ddb2e93 | 2012-01-16 13:21:31 -0800 | [diff] [blame] | 32 | |
| 33 | void 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 | } |
Glenn Kasten | 7a0baca | 2012-07-19 13:59:50 -0700 | [diff] [blame] | 41 | |
Glenn Kasten | 78da2ac | 2012-11-12 14:39:36 -0800 | [diff] [blame] | 42 | void 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 | |
Glenn Kasten | 5d43605 | 2013-06-21 14:01:54 -0700 | [diff] [blame] | 49 | void 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 | |
| 56 | void memcpy_to_i16_from_float(int16_t *dst, const float *src, size_t count) |
| 57 | { |
| 58 | while (count--) { |
Andy Hung | 65b5ccd | 2014-03-18 12:00:55 -0700 | [diff] [blame] | 59 | *dst++ = clamp16_from_float(*src++); |
Glenn Kasten | 5d43605 | 2013-06-21 14:01:54 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Andy Hung | b878e52 | 2014-04-04 13:05:43 -0700 | [diff] [blame] | 63 | void memcpy_to_float_from_q4_27(float *dst, const int32_t *src, size_t count) |
Andy Hung | aecb15e | 2014-02-24 19:07:40 -0800 | [diff] [blame] | 64 | { |
Andy Hung | b878e52 | 2014-04-04 13:05:43 -0700 | [diff] [blame] | 65 | while (count--) { |
Andy Hung | d2a25cd | 2014-04-02 11:23:56 -0700 | [diff] [blame] | 66 | *dst++ = float_from_q4_27(*src++); |
Andy Hung | aecb15e | 2014-02-24 19:07:40 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 70 | void memcpy_to_float_from_i16(float *dst, const int16_t *src, size_t count) |
| 71 | { |
| 72 | while (count--) { |
| 73 | *dst++ = float_from_i16(*src++); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void memcpy_to_float_from_p24(float *dst, const uint8_t *src, size_t count) |
| 78 | { |
| 79 | while (count--) { |
| 80 | *dst++ = float_from_p24(src); |
| 81 | src += 3; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void memcpy_to_i16_from_p24(int16_t *dst, const uint8_t *src, size_t count) |
| 86 | { |
| 87 | while (count--) { |
| 88 | #ifdef HAVE_BIG_ENDIAN |
| 89 | *dst++ = src[1] | (src[0] << 8); |
| 90 | #else |
| 91 | *dst++ = src[1] | (src[2] << 8); |
| 92 | #endif |
| 93 | src += 3; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count) |
| 98 | { |
| 99 | while (count--) { |
| 100 | #ifdef HAVE_BIG_ENDIAN |
| 101 | *dst++ = *src >> 8; |
| 102 | *dst++ = *src++; |
| 103 | *dst++ = 0; |
| 104 | #else |
| 105 | *dst++ = 0; |
| 106 | *dst++ = *src; |
| 107 | *dst++ = *src++ >> 8; |
| 108 | #endif |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count) |
| 113 | { |
| 114 | while (count--) { |
| 115 | int32_t ival = clamp24_from_float(*src++); |
| 116 | |
| 117 | #ifdef HAVE_BIG_ENDIAN |
| 118 | *dst++ = ival >> 16; |
| 119 | *dst++ = ival >> 8; |
| 120 | *dst++ = ival; |
| 121 | #else |
| 122 | *dst++ = ival; |
| 123 | *dst++ = ival >> 8; |
| 124 | *dst++ = ival >> 16; |
| 125 | #endif |
| 126 | } |
| 127 | } |
| 128 | |
Glenn Kasten | eee4515 | 2014-05-02 12:41:04 -0700 | [diff] [blame^] | 129 | void memcpy_to_p24_from_q8_23(uint8_t *dst, const int32_t *src, size_t count) |
| 130 | { |
| 131 | while (count--) { |
| 132 | int32_t ival = clamp24_from_q8_23(*src++); |
| 133 | |
| 134 | #ifdef HAVE_BIG_ENDIAN |
| 135 | *dst++ = ival >> 16; |
| 136 | *dst++ = ival >> 8; |
| 137 | *dst++ = ival; |
| 138 | #else |
| 139 | *dst++ = ival; |
| 140 | *dst++ = ival >> 8; |
| 141 | *dst++ = ival >> 16; |
| 142 | #endif |
| 143 | } |
| 144 | } |
| 145 | |
Andy Hung | d582988 | 2014-03-12 11:46:15 -0700 | [diff] [blame] | 146 | void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count) |
| 147 | { |
| 148 | while (count--) { |
| 149 | *dst++ = (int32_t)*src++ << 8; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count) |
| 154 | { |
| 155 | while (count--) { |
| 156 | *dst++ = clamp24_from_float(*src++); |
| 157 | } |
| 158 | } |
| 159 | |
Andy Hung | b878e52 | 2014-04-04 13:05:43 -0700 | [diff] [blame] | 160 | void memcpy_to_q4_27_from_float(int32_t *dst, const float *src, size_t count) |
| 161 | { |
| 162 | while (count--) { |
| 163 | *dst++ = clampq4_27_from_float(*src++); |
| 164 | } |
| 165 | } |
| 166 | |
Andy Hung | d582988 | 2014-03-12 11:46:15 -0700 | [diff] [blame] | 167 | void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count) |
| 168 | { |
| 169 | while (count--) { |
| 170 | *dst++ = clamp16(*src++ >> 8); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count) |
| 175 | { |
| 176 | while (count--) { |
| 177 | *dst++ = float_from_q8_23(*src++); |
| 178 | } |
| 179 | } |
| 180 | |
Andy Hung | 2c63fb6 | 2014-03-12 14:44:12 -0700 | [diff] [blame] | 181 | void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count) |
| 182 | { |
| 183 | while (count--) { |
| 184 | *dst++ = (int32_t)*src++ << 16; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count) |
| 189 | { |
| 190 | while (count--) { |
| 191 | *dst++ = clamp32_from_float(*src++); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count) |
| 196 | { |
| 197 | while (count--) { |
| 198 | *dst++ = float_from_i32(*src++); |
| 199 | } |
| 200 | } |
| 201 | |
Glenn Kasten | 7a0baca | 2012-07-19 13:59:50 -0700 | [diff] [blame] | 202 | void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count) |
| 203 | { |
| 204 | while (count--) { |
| 205 | *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1); |
| 206 | src += 2; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count) |
| 211 | { |
| 212 | while (count--) { |
| 213 | int32_t temp = *src++; |
| 214 | dst[0] = temp; |
| 215 | dst[1] = temp; |
| 216 | dst += 2; |
| 217 | } |
| 218 | } |
Glenn Kasten | eb247df | 2014-02-21 10:00:51 -0800 | [diff] [blame] | 219 | |
| 220 | size_t nonZeroMono32(const int32_t *samples, size_t count) |
| 221 | { |
| 222 | size_t nonZero = 0; |
| 223 | while (count-- > 0) { |
| 224 | if (*samples++ != 0) { |
| 225 | nonZero++; |
| 226 | } |
| 227 | } |
| 228 | return nonZero; |
| 229 | } |
| 230 | |
| 231 | size_t nonZeroMono16(const int16_t *samples, size_t count) |
| 232 | { |
| 233 | size_t nonZero = 0; |
| 234 | while (count-- > 0) { |
| 235 | if (*samples++ != 0) { |
| 236 | nonZero++; |
| 237 | } |
| 238 | } |
| 239 | return nonZero; |
| 240 | } |
| 241 | |
| 242 | size_t nonZeroStereo32(const int32_t *frames, size_t count) |
| 243 | { |
| 244 | size_t nonZero = 0; |
| 245 | while (count-- > 0) { |
| 246 | if (frames[0] != 0 || frames[1] != 0) { |
| 247 | nonZero++; |
| 248 | } |
| 249 | frames += 2; |
| 250 | } |
| 251 | return nonZero; |
| 252 | } |
| 253 | |
| 254 | size_t nonZeroStereo16(const int16_t *frames, size_t count) |
| 255 | { |
| 256 | size_t nonZero = 0; |
| 257 | while (count-- > 0) { |
| 258 | if (frames[0] != 0 || frames[1] != 0) { |
| 259 | nonZero++; |
| 260 | } |
| 261 | frames += 2; |
| 262 | } |
| 263 | return nonZero; |
| 264 | } |