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 | db29e69 | 2014-02-24 18:36:35 -0800 | [diff] [blame] | 59 | *dst++ = clamp16FromFloat(*src++); |
Glenn Kasten | 5d43605 | 2013-06-21 14:01:54 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Andy Hung | aecb15e | 2014-02-24 19:07:40 -0800 | [diff] [blame] | 63 | void memcpy_to_float_from_q19_12(float *dst, const int32_t *src, size_t c) |
| 64 | { |
| 65 | size_t i; |
Andy Hung | aecb15e | 2014-02-24 19:07:40 -0800 | [diff] [blame] | 66 | for (i = 0; i < c; ++i) { |
Andy Hung | 42605b3 | 2014-02-28 15:29:47 -0800 | [diff] [blame] | 67 | *dst++ = float_from_q19_12(*src++); |
Andy Hung | aecb15e | 2014-02-24 19:07:40 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 71 | void 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 | |
| 78 | void 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 | |
| 86 | void 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 | |
| 98 | void 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 | |
| 113 | void 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 | |
Andy Hung | d582988 | 2014-03-12 11:46:15 -0700 | [diff] [blame^] | 130 | void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count) |
| 131 | { |
| 132 | while (count--) { |
| 133 | *dst++ = (int32_t)*src++ << 8; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count) |
| 138 | { |
| 139 | while (count--) { |
| 140 | *dst++ = clamp24_from_float(*src++); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count) |
| 145 | { |
| 146 | while (count--) { |
| 147 | *dst++ = clamp16(*src++ >> 8); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count) |
| 152 | { |
| 153 | while (count--) { |
| 154 | *dst++ = float_from_q8_23(*src++); |
| 155 | } |
| 156 | } |
| 157 | |
Andy Hung | 2c63fb6 | 2014-03-12 14:44:12 -0700 | [diff] [blame] | 158 | void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count) |
| 159 | { |
| 160 | while (count--) { |
| 161 | *dst++ = (int32_t)*src++ << 16; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count) |
| 166 | { |
| 167 | while (count--) { |
| 168 | *dst++ = clamp32_from_float(*src++); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count) |
| 173 | { |
| 174 | while (count--) { |
| 175 | *dst++ = float_from_i32(*src++); |
| 176 | } |
| 177 | } |
| 178 | |
Glenn Kasten | 7a0baca | 2012-07-19 13:59:50 -0700 | [diff] [blame] | 179 | void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count) |
| 180 | { |
| 181 | while (count--) { |
| 182 | *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1); |
| 183 | src += 2; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count) |
| 188 | { |
| 189 | while (count--) { |
| 190 | int32_t temp = *src++; |
| 191 | dst[0] = temp; |
| 192 | dst[1] = temp; |
| 193 | dst += 2; |
| 194 | } |
| 195 | } |
Glenn Kasten | eb247df | 2014-02-21 10:00:51 -0800 | [diff] [blame] | 196 | |
| 197 | size_t nonZeroMono32(const int32_t *samples, size_t count) |
| 198 | { |
| 199 | size_t nonZero = 0; |
| 200 | while (count-- > 0) { |
| 201 | if (*samples++ != 0) { |
| 202 | nonZero++; |
| 203 | } |
| 204 | } |
| 205 | return nonZero; |
| 206 | } |
| 207 | |
| 208 | size_t nonZeroMono16(const int16_t *samples, size_t count) |
| 209 | { |
| 210 | size_t nonZero = 0; |
| 211 | while (count-- > 0) { |
| 212 | if (*samples++ != 0) { |
| 213 | nonZero++; |
| 214 | } |
| 215 | } |
| 216 | return nonZero; |
| 217 | } |
| 218 | |
| 219 | size_t nonZeroStereo32(const int32_t *frames, size_t count) |
| 220 | { |
| 221 | size_t nonZero = 0; |
| 222 | while (count-- > 0) { |
| 223 | if (frames[0] != 0 || frames[1] != 0) { |
| 224 | nonZero++; |
| 225 | } |
| 226 | frames += 2; |
| 227 | } |
| 228 | return nonZero; |
| 229 | } |
| 230 | |
| 231 | size_t nonZeroStereo16(const int16_t *frames, size_t count) |
| 232 | { |
| 233 | size_t nonZero = 0; |
| 234 | while (count-- > 0) { |
| 235 | if (frames[0] != 0 || frames[1] != 0) { |
| 236 | nonZero++; |
| 237 | } |
| 238 | frames += 2; |
| 239 | } |
| 240 | return nonZero; |
| 241 | } |