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 | |
Andy Hung | e0ccb20 | 2014-07-27 20:11:31 -0700 | [diff] [blame] | 17 | #include <cutils/bitops.h> /* for popcount() */ |
Glenn Kasten | 632e0c0 | 2011-12-16 10:42:58 -0800 | [diff] [blame] | 18 | #include <audio_utils/primitives.h> |
Andy Hung | e0ccb20 | 2014-07-27 20:11:31 -0700 | [diff] [blame] | 19 | #include "private/private.h" |
Glenn Kasten | 632e0c0 | 2011-12-16 10:42:58 -0800 | [diff] [blame] | 20 | |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 21 | void ditherAndClamp(int32_t *out, const int32_t *sums, size_t pairs) |
Glenn Kasten | 632e0c0 | 2011-12-16 10:42:58 -0800 | [diff] [blame] | 22 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 23 | for (; pairs > 0; --pairs) { |
| 24 | const int32_t l = clamp16(*sums++ >> 12); |
| 25 | const int32_t r = clamp16(*sums++ >> 12); |
| 26 | *out++ = (r << 16) | (l & 0xFFFF); |
Glenn Kasten | 632e0c0 | 2011-12-16 10:42:58 -0800 | [diff] [blame] | 27 | } |
| 28 | } |
Glenn Kasten | ddb2e93 | 2012-01-16 13:21:31 -0800 | [diff] [blame] | 29 | |
Andy Hung | 62c3d9f | 2017-11-21 16:36:41 -0800 | [diff] [blame] | 30 | void memcpy_to_i16_from_q4_27(int16_t *dst, const int32_t *src, size_t count) |
| 31 | { |
| 32 | for (; count > 0; --count) { |
| 33 | *dst++ = clamp16(*src++ >> 12); |
| 34 | } |
| 35 | } |
| 36 | |
Glenn Kasten | ddb2e93 | 2012-01-16 13:21:31 -0800 | [diff] [blame] | 37 | void memcpy_to_i16_from_u8(int16_t *dst, const uint8_t *src, size_t count) |
| 38 | { |
| 39 | dst += count; |
| 40 | src += count; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 41 | for (; count > 0; --count) { |
Glenn Kasten | ddb2e93 | 2012-01-16 13:21:31 -0800 | [diff] [blame] | 42 | *--dst = (int16_t)(*--src - 0x80) << 8; |
| 43 | } |
| 44 | } |
Glenn Kasten | 7a0baca | 2012-07-19 13:59:50 -0700 | [diff] [blame] | 45 | |
Glenn Kasten | 78da2ac | 2012-11-12 14:39:36 -0800 | [diff] [blame] | 46 | void memcpy_to_u8_from_i16(uint8_t *dst, const int16_t *src, size_t count) |
| 47 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 48 | for (; count > 0; --count) { |
Glenn Kasten | 78da2ac | 2012-11-12 14:39:36 -0800 | [diff] [blame] | 49 | *dst++ = (*src++ >> 8) + 0x80; |
| 50 | } |
| 51 | } |
| 52 | |
Andy Hung | 23ef1b3 | 2015-01-13 13:56:37 -0800 | [diff] [blame] | 53 | void memcpy_to_u8_from_float(uint8_t *dst, const float *src, size_t count) |
| 54 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 55 | for (; count > 0; --count) { |
Andy Hung | 23ef1b3 | 2015-01-13 13:56:37 -0800 | [diff] [blame] | 56 | *dst++ = clamp8_from_float(*src++); |
| 57 | } |
| 58 | } |
| 59 | |
Glenn Kasten | 5d43605 | 2013-06-21 14:01:54 -0700 | [diff] [blame] | 60 | void memcpy_to_i16_from_i32(int16_t *dst, const int32_t *src, size_t count) |
| 61 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 62 | for (; count > 0; --count) { |
Glenn Kasten | 5d43605 | 2013-06-21 14:01:54 -0700 | [diff] [blame] | 63 | *dst++ = *src++ >> 16; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void memcpy_to_i16_from_float(int16_t *dst, const float *src, size_t count) |
| 68 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 69 | for (; count > 0; --count) { |
Andy Hung | 65b5ccd | 2014-03-18 12:00:55 -0700 | [diff] [blame] | 70 | *dst++ = clamp16_from_float(*src++); |
Glenn Kasten | 5d43605 | 2013-06-21 14:01:54 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Andy Hung | b878e52 | 2014-04-04 13:05:43 -0700 | [diff] [blame] | 74 | 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] | 75 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 76 | for (; count > 0; --count) { |
Andy Hung | d2a25cd | 2014-04-02 11:23:56 -0700 | [diff] [blame] | 77 | *dst++ = float_from_q4_27(*src++); |
Andy Hung | aecb15e | 2014-02-24 19:07:40 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 81 | void memcpy_to_float_from_i16(float *dst, const int16_t *src, size_t count) |
| 82 | { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 83 | dst += count; |
| 84 | src += count; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 85 | for (; count > 0; --count) { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 86 | *--dst = float_from_i16(*--src); |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Andy Hung | 23ef1b3 | 2015-01-13 13:56:37 -0800 | [diff] [blame] | 90 | void memcpy_to_float_from_u8(float *dst, const uint8_t *src, size_t count) |
| 91 | { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 92 | dst += count; |
| 93 | src += count; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 94 | for (; count > 0; --count) { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 95 | *--dst = float_from_u8(*--src); |
Andy Hung | 23ef1b3 | 2015-01-13 13:56:37 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 99 | void memcpy_to_float_from_p24(float *dst, const uint8_t *src, size_t count) |
| 100 | { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 101 | dst += count; |
| 102 | src += count * 3; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 103 | for (; count > 0; --count) { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 104 | src -= 3; |
| 105 | *--dst = float_from_p24(src); |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | void memcpy_to_i16_from_p24(int16_t *dst, const uint8_t *src, size_t count) |
| 110 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 111 | for (; count > 0; --count) { |
Andy Hung | 2af0e17 | 2017-03-31 14:08:14 -0700 | [diff] [blame] | 112 | #if HAVE_BIG_ENDIAN |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 113 | *dst++ = src[1] | (src[0] << 8); |
| 114 | #else |
| 115 | *dst++ = src[1] | (src[2] << 8); |
| 116 | #endif |
| 117 | src += 3; |
| 118 | } |
| 119 | } |
| 120 | |
Glenn Kasten | 95880cb | 2015-05-28 15:19:16 -0700 | [diff] [blame] | 121 | void memcpy_to_i32_from_p24(int32_t *dst, const uint8_t *src, size_t count) |
| 122 | { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 123 | dst += count; |
| 124 | src += count * 3; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 125 | for (; count > 0; --count) { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 126 | src -= 3; |
Andy Hung | 2af0e17 | 2017-03-31 14:08:14 -0700 | [diff] [blame] | 127 | #if HAVE_BIG_ENDIAN |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 128 | *--dst = (src[2] << 8) | (src[1] << 16) | (src[0] << 24); |
Glenn Kasten | 95880cb | 2015-05-28 15:19:16 -0700 | [diff] [blame] | 129 | #else |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 130 | *--dst = (src[0] << 8) | (src[1] << 16) | (src[2] << 24); |
Glenn Kasten | 95880cb | 2015-05-28 15:19:16 -0700 | [diff] [blame] | 131 | #endif |
Glenn Kasten | 95880cb | 2015-05-28 15:19:16 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 135 | void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count) |
| 136 | { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 137 | dst += count * 3; |
| 138 | src += count; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 139 | for (; count > 0; --count) { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 140 | dst -= 3; |
Andy Hung | c6ddd03 | 2018-04-25 17:59:24 -0700 | [diff] [blame^] | 141 | const int16_t sample = *--src; |
Andy Hung | 2af0e17 | 2017-03-31 14:08:14 -0700 | [diff] [blame] | 142 | #if HAVE_BIG_ENDIAN |
Andy Hung | c6ddd03 | 2018-04-25 17:59:24 -0700 | [diff] [blame^] | 143 | dst[0] = sample >> 8; |
| 144 | dst[1] = sample; |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 145 | dst[2] = 0; |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 146 | #else |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 147 | dst[0] = 0; |
Andy Hung | c6ddd03 | 2018-04-25 17:59:24 -0700 | [diff] [blame^] | 148 | dst[1] = sample; |
| 149 | dst[2] = sample >> 8; |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 150 | #endif |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count) |
| 155 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 156 | for (; count > 0; --count) { |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 157 | int32_t ival = clamp24_from_float(*src++); |
| 158 | |
Andy Hung | 2af0e17 | 2017-03-31 14:08:14 -0700 | [diff] [blame] | 159 | #if HAVE_BIG_ENDIAN |
Andy Hung | e0454e2 | 2014-03-06 13:04:56 -0800 | [diff] [blame] | 160 | *dst++ = ival >> 16; |
| 161 | *dst++ = ival >> 8; |
| 162 | *dst++ = ival; |
| 163 | #else |
| 164 | *dst++ = ival; |
| 165 | *dst++ = ival >> 8; |
| 166 | *dst++ = ival >> 16; |
| 167 | #endif |
| 168 | } |
| 169 | } |
| 170 | |
Glenn Kasten | eee4515 | 2014-05-02 12:41:04 -0700 | [diff] [blame] | 171 | void memcpy_to_p24_from_q8_23(uint8_t *dst, const int32_t *src, size_t count) |
| 172 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 173 | for (; count > 0; --count) { |
Glenn Kasten | eee4515 | 2014-05-02 12:41:04 -0700 | [diff] [blame] | 174 | int32_t ival = clamp24_from_q8_23(*src++); |
| 175 | |
Andy Hung | 2af0e17 | 2017-03-31 14:08:14 -0700 | [diff] [blame] | 176 | #if HAVE_BIG_ENDIAN |
Glenn Kasten | eee4515 | 2014-05-02 12:41:04 -0700 | [diff] [blame] | 177 | *dst++ = ival >> 16; |
| 178 | *dst++ = ival >> 8; |
| 179 | *dst++ = ival; |
| 180 | #else |
| 181 | *dst++ = ival; |
| 182 | *dst++ = ival >> 8; |
| 183 | *dst++ = ival >> 16; |
| 184 | #endif |
| 185 | } |
| 186 | } |
| 187 | |
Glenn Kasten | daa1a00 | 2015-05-29 16:50:24 -0700 | [diff] [blame] | 188 | void memcpy_to_p24_from_i32(uint8_t *dst, const int32_t *src, size_t count) |
| 189 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 190 | for (; count > 0; --count) { |
Glenn Kasten | daa1a00 | 2015-05-29 16:50:24 -0700 | [diff] [blame] | 191 | int32_t ival = *src++ >> 8; |
| 192 | |
Andy Hung | 2af0e17 | 2017-03-31 14:08:14 -0700 | [diff] [blame] | 193 | #if HAVE_BIG_ENDIAN |
Glenn Kasten | daa1a00 | 2015-05-29 16:50:24 -0700 | [diff] [blame] | 194 | *dst++ = ival >> 16; |
| 195 | *dst++ = ival >> 8; |
| 196 | *dst++ = ival; |
| 197 | #else |
| 198 | *dst++ = ival; |
| 199 | *dst++ = ival >> 8; |
| 200 | *dst++ = ival >> 16; |
| 201 | #endif |
| 202 | } |
| 203 | } |
| 204 | |
Andy Hung | d582988 | 2014-03-12 11:46:15 -0700 | [diff] [blame] | 205 | void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count) |
| 206 | { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 207 | dst += count; |
| 208 | src += count; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 209 | for (; count > 0; --count) { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 210 | *--dst = (int32_t)*--src << 8; |
Andy Hung | d582988 | 2014-03-12 11:46:15 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count) |
| 215 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 216 | for (; count > 0; --count) { |
Andy Hung | d582988 | 2014-03-12 11:46:15 -0700 | [diff] [blame] | 217 | *dst++ = clamp24_from_float(*src++); |
| 218 | } |
| 219 | } |
| 220 | |
Haynes Mathew George | 78ac9f8 | 2015-04-09 13:50:13 -0700 | [diff] [blame] | 221 | void memcpy_to_q8_23_from_p24(int32_t *dst, const uint8_t *src, size_t count) |
| 222 | { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 223 | dst += count; |
| 224 | src += count * 3; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 225 | for (; count > 0; --count) { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 226 | src -= 3; |
Andy Hung | 2af0e17 | 2017-03-31 14:08:14 -0700 | [diff] [blame] | 227 | #if HAVE_BIG_ENDIAN |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 228 | *--dst = (int8_t)src[0] << 16 | src[1] << 8 | src[2]; |
Haynes Mathew George | 78ac9f8 | 2015-04-09 13:50:13 -0700 | [diff] [blame] | 229 | #else |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 230 | *--dst = (int8_t)src[2] << 16 | src[1] << 8 | src[0]; |
Haynes Mathew George | 78ac9f8 | 2015-04-09 13:50:13 -0700 | [diff] [blame] | 231 | #endif |
Haynes Mathew George | 78ac9f8 | 2015-04-09 13:50:13 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Andy Hung | b878e52 | 2014-04-04 13:05:43 -0700 | [diff] [blame] | 235 | void memcpy_to_q4_27_from_float(int32_t *dst, const float *src, size_t count) |
| 236 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 237 | for (; count > 0; --count) { |
Andy Hung | b878e52 | 2014-04-04 13:05:43 -0700 | [diff] [blame] | 238 | *dst++ = clampq4_27_from_float(*src++); |
| 239 | } |
| 240 | } |
| 241 | |
Andy Hung | d582988 | 2014-03-12 11:46:15 -0700 | [diff] [blame] | 242 | void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count) |
| 243 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 244 | for (; count > 0; --count) { |
Andy Hung | d582988 | 2014-03-12 11:46:15 -0700 | [diff] [blame] | 245 | *dst++ = clamp16(*src++ >> 8); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count) |
| 250 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 251 | for (; count > 0; --count) { |
Andy Hung | d582988 | 2014-03-12 11:46:15 -0700 | [diff] [blame] | 252 | *dst++ = float_from_q8_23(*src++); |
| 253 | } |
| 254 | } |
| 255 | |
Andy Hung | 2c63fb6 | 2014-03-12 14:44:12 -0700 | [diff] [blame] | 256 | void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count) |
| 257 | { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 258 | dst += count; |
| 259 | src += count; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 260 | for (; count > 0; --count) { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 261 | *--dst = (int32_t)*--src << 16; |
Andy Hung | 2c63fb6 | 2014-03-12 14:44:12 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count) |
| 266 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 267 | for (; count > 0; --count) { |
Andy Hung | 2c63fb6 | 2014-03-12 14:44:12 -0700 | [diff] [blame] | 268 | *dst++ = clamp32_from_float(*src++); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count) |
| 273 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 274 | for (; count > 0; --count) { |
Andy Hung | 2c63fb6 | 2014-03-12 14:44:12 -0700 | [diff] [blame] | 275 | *dst++ = float_from_i32(*src++); |
| 276 | } |
| 277 | } |
| 278 | |
Kevin Rocard | 2d98fd3 | 2017-11-09 22:12:51 -0800 | [diff] [blame] | 279 | void memcpy_to_float_from_float_with_clamping(float *dst, const float *src, size_t count, |
| 280 | float absMax) { |
| 281 | // Note: using NEON intrinsics (vminq_f32, vld1q_f32...) did NOT accelerate |
| 282 | // the function when benchmarked. The compiler already vectorize using FMINNM f32x4 & similar. |
| 283 | // Note: clamping induce a ~20% overhead compared to memcpy for count in [64, 512] |
| 284 | // See primitives_benchmark |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 285 | for (; count > 0; --count) { |
Kevin Rocard | 2d98fd3 | 2017-11-09 22:12:51 -0800 | [diff] [blame] | 286 | const float sample = *src++; |
| 287 | *dst++ = fmax(-absMax, fmin(absMax, sample)); |
| 288 | } |
| 289 | } |
| 290 | |
Glenn Kasten | 7a0baca | 2012-07-19 13:59:50 -0700 | [diff] [blame] | 291 | void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count) |
| 292 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 293 | for (; count > 0; --count) { |
Glenn Kasten | 7a0baca | 2012-07-19 13:59:50 -0700 | [diff] [blame] | 294 | *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1); |
| 295 | src += 2; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count) |
| 300 | { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 301 | dst += count * 2; |
| 302 | src += count; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 303 | for (; count > 0; --count) { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 304 | const int32_t temp = *--src; |
| 305 | dst -= 2; |
Glenn Kasten | 7a0baca | 2012-07-19 13:59:50 -0700 | [diff] [blame] | 306 | dst[0] = temp; |
| 307 | dst[1] = temp; |
Glenn Kasten | 7a0baca | 2012-07-19 13:59:50 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
Glenn Kasten | eb247df | 2014-02-21 10:00:51 -0800 | [diff] [blame] | 310 | |
Andy Hung | 9c8dd45 | 2015-04-21 13:21:36 -0700 | [diff] [blame] | 311 | void downmix_to_mono_float_from_stereo_float(float *dst, const float *src, size_t frames) |
| 312 | { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 313 | for (; frames > 0; --frames) { |
Andy Hung | 9c8dd45 | 2015-04-21 13:21:36 -0700 | [diff] [blame] | 314 | *dst++ = (src[0] + src[1]) * 0.5; |
| 315 | src += 2; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void upmix_to_stereo_float_from_mono_float(float *dst, const float *src, size_t frames) |
| 320 | { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 321 | dst += frames * 2; |
| 322 | src += frames; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 323 | for (; frames > 0; --frames) { |
Andy Hung | 22667a6 | 2017-11-17 15:54:27 -0800 | [diff] [blame] | 324 | const float temp = *--src; |
| 325 | dst -= 2; |
Andy Hung | 9c8dd45 | 2015-04-21 13:21:36 -0700 | [diff] [blame] | 326 | dst[0] = temp; |
| 327 | dst[1] = temp; |
Andy Hung | 9c8dd45 | 2015-04-21 13:21:36 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Glenn Kasten | eb247df | 2014-02-21 10:00:51 -0800 | [diff] [blame] | 331 | size_t nonZeroMono32(const int32_t *samples, size_t count) |
| 332 | { |
| 333 | size_t nonZero = 0; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 334 | for (; count > 0; --count) { |
Andy Hung | 0ebba4b | 2017-11-17 16:02:19 -0800 | [diff] [blame] | 335 | nonZero += *samples++ != 0; |
Glenn Kasten | eb247df | 2014-02-21 10:00:51 -0800 | [diff] [blame] | 336 | } |
| 337 | return nonZero; |
| 338 | } |
| 339 | |
| 340 | size_t nonZeroMono16(const int16_t *samples, size_t count) |
| 341 | { |
| 342 | size_t nonZero = 0; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 343 | for (; count > 0; --count) { |
Andy Hung | 0ebba4b | 2017-11-17 16:02:19 -0800 | [diff] [blame] | 344 | nonZero += *samples++ != 0; |
Glenn Kasten | eb247df | 2014-02-21 10:00:51 -0800 | [diff] [blame] | 345 | } |
| 346 | return nonZero; |
| 347 | } |
| 348 | |
| 349 | size_t nonZeroStereo32(const int32_t *frames, size_t count) |
| 350 | { |
| 351 | size_t nonZero = 0; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 352 | for (; count > 0; --count) { |
Andy Hung | 0ebba4b | 2017-11-17 16:02:19 -0800 | [diff] [blame] | 353 | nonZero += frames[0] != 0 || frames[1] != 0; |
Glenn Kasten | eb247df | 2014-02-21 10:00:51 -0800 | [diff] [blame] | 354 | frames += 2; |
| 355 | } |
| 356 | return nonZero; |
| 357 | } |
| 358 | |
| 359 | size_t nonZeroStereo16(const int16_t *frames, size_t count) |
| 360 | { |
| 361 | size_t nonZero = 0; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 362 | for (; count > 0; --count) { |
Andy Hung | 0ebba4b | 2017-11-17 16:02:19 -0800 | [diff] [blame] | 363 | nonZero += frames[0] != 0 || frames[1] != 0; |
Glenn Kasten | eb247df | 2014-02-21 10:00:51 -0800 | [diff] [blame] | 364 | frames += 2; |
| 365 | } |
| 366 | return nonZero; |
| 367 | } |
Andy Hung | 3af2af2 | 2014-05-22 18:40:30 -0700 | [diff] [blame] | 368 | |
Andy Hung | 3af2af2 | 2014-05-22 18:40:30 -0700 | [diff] [blame] | 369 | /* |
| 370 | * C macro to do channel mask copying independent of dst/src sample type. |
| 371 | * Don't pass in any expressions for the macro arguments here. |
| 372 | */ |
| 373 | #define copy_frame_by_mask(dst, dmask, src, smask, count, zero) \ |
| 374 | { \ |
| 375 | uint32_t bit, ormask; \ |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 376 | for (; (count) > 0; --(count)) { \ |
Chih-Hung Hsieh | 2c853ac | 2016-05-11 15:13:31 -0700 | [diff] [blame] | 377 | ormask = (dmask) | (smask); \ |
Andy Hung | 3af2af2 | 2014-05-22 18:40:30 -0700 | [diff] [blame] | 378 | while (ormask) { \ |
| 379 | bit = ormask & -ormask; /* get lowest bit */ \ |
| 380 | ormask ^= bit; /* remove lowest bit */ \ |
Chih-Hung Hsieh | 2c853ac | 2016-05-11 15:13:31 -0700 | [diff] [blame] | 381 | if ((dmask) & bit) { \ |
| 382 | *(dst)++ = (smask) & bit ? *(src)++ : (zero); \ |
Andy Hung | 3af2af2 | 2014-05-22 18:40:30 -0700 | [diff] [blame] | 383 | } else { /* source channel only */ \ |
Chih-Hung Hsieh | 2c853ac | 2016-05-11 15:13:31 -0700 | [diff] [blame] | 384 | ++(src); \ |
Andy Hung | 3af2af2 | 2014-05-22 18:40:30 -0700 | [diff] [blame] | 385 | } \ |
| 386 | } \ |
| 387 | } \ |
| 388 | } |
| 389 | |
| 390 | void memcpy_by_channel_mask(void *dst, uint32_t dst_mask, |
| 391 | const void *src, uint32_t src_mask, size_t sample_size, size_t count) |
| 392 | { |
| 393 | #if 0 |
| 394 | /* alternate way of handling memcpy_by_channel_mask by using the idxary */ |
| 395 | int8_t idxary[32]; |
| 396 | uint32_t src_channels = popcount(src_mask); |
| 397 | uint32_t dst_channels = |
| 398 | memcpy_by_index_array_initialization(idxary, 32, dst_mask, src_mask); |
| 399 | |
| 400 | memcpy_by_idxary(dst, dst_channels, src, src_channels, idxary, sample_size, count); |
| 401 | #else |
| 402 | if (dst_mask == src_mask) { |
| 403 | memcpy(dst, src, sample_size * popcount(dst_mask) * count); |
| 404 | return; |
| 405 | } |
| 406 | switch (sample_size) { |
| 407 | case 1: { |
| 408 | uint8_t *udst = (uint8_t*)dst; |
| 409 | const uint8_t *usrc = (const uint8_t*)src; |
| 410 | |
| 411 | copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0); |
| 412 | } break; |
| 413 | case 2: { |
| 414 | uint16_t *udst = (uint16_t*)dst; |
| 415 | const uint16_t *usrc = (const uint16_t*)src; |
| 416 | |
| 417 | copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0); |
| 418 | } break; |
| 419 | case 3: { /* could be slow. use a struct to represent 3 bytes of data. */ |
| 420 | uint8x3_t *udst = (uint8x3_t*)dst; |
| 421 | const uint8x3_t *usrc = (const uint8x3_t*)src; |
| 422 | static const uint8x3_t zero; /* tricky - we use this to zero out a sample */ |
| 423 | |
| 424 | copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, zero); |
| 425 | } break; |
| 426 | case 4: { |
| 427 | uint32_t *udst = (uint32_t*)dst; |
| 428 | const uint32_t *usrc = (const uint32_t*)src; |
| 429 | |
| 430 | copy_frame_by_mask(udst, dst_mask, usrc, src_mask, count, 0); |
| 431 | } break; |
| 432 | default: |
| 433 | abort(); /* illegal value */ |
| 434 | break; |
| 435 | } |
| 436 | #endif |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * C macro to do copying by index array, to rearrange samples |
| 441 | * within a frame. This is independent of src/dst sample type. |
| 442 | * Don't pass in any expressions for the macro arguments here. |
| 443 | */ |
| 444 | #define copy_frame_by_idx(dst, dst_channels, src, src_channels, idxary, count, zero) \ |
| 445 | { \ |
| 446 | unsigned i; \ |
| 447 | int index; \ |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 448 | for (; (count) > 0; --(count)) { \ |
Chih-Hung Hsieh | 2c853ac | 2016-05-11 15:13:31 -0700 | [diff] [blame] | 449 | for (i = 0; i < (dst_channels); ++i) { \ |
| 450 | index = (idxary)[i]; \ |
| 451 | *(dst)++ = index < 0 ? (zero) : (src)[index]; \ |
Andy Hung | 3af2af2 | 2014-05-22 18:40:30 -0700 | [diff] [blame] | 452 | } \ |
Chih-Hung Hsieh | 2c853ac | 2016-05-11 15:13:31 -0700 | [diff] [blame] | 453 | (src) += (src_channels); \ |
Andy Hung | 3af2af2 | 2014-05-22 18:40:30 -0700 | [diff] [blame] | 454 | } \ |
| 455 | } |
| 456 | |
| 457 | void memcpy_by_index_array(void *dst, uint32_t dst_channels, |
| 458 | const void *src, uint32_t src_channels, |
| 459 | const int8_t *idxary, size_t sample_size, size_t count) |
| 460 | { |
| 461 | switch (sample_size) { |
| 462 | case 1: { |
| 463 | uint8_t *udst = (uint8_t*)dst; |
| 464 | const uint8_t *usrc = (const uint8_t*)src; |
| 465 | |
| 466 | copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0); |
| 467 | } break; |
| 468 | case 2: { |
| 469 | uint16_t *udst = (uint16_t*)dst; |
| 470 | const uint16_t *usrc = (const uint16_t*)src; |
| 471 | |
| 472 | copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0); |
| 473 | } break; |
| 474 | case 3: { /* could be slow. use a struct to represent 3 bytes of data. */ |
| 475 | uint8x3_t *udst = (uint8x3_t*)dst; |
| 476 | const uint8x3_t *usrc = (const uint8x3_t*)src; |
| 477 | static const uint8x3_t zero; |
| 478 | |
| 479 | copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, zero); |
| 480 | } break; |
| 481 | case 4: { |
| 482 | uint32_t *udst = (uint32_t*)dst; |
| 483 | const uint32_t *usrc = (const uint32_t*)src; |
| 484 | |
| 485 | copy_frame_by_idx(udst, dst_channels, usrc, src_channels, idxary, count, 0); |
| 486 | } break; |
| 487 | default: |
| 488 | abort(); /* illegal value */ |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | size_t memcpy_by_index_array_initialization(int8_t *idxary, size_t idxcount, |
| 494 | uint32_t dst_mask, uint32_t src_mask) |
| 495 | { |
| 496 | size_t n = 0; |
| 497 | int srcidx = 0; |
| 498 | uint32_t bit, ormask = src_mask | dst_mask; |
| 499 | |
| 500 | while (ormask && n < idxcount) { |
| 501 | bit = ormask & -ormask; /* get lowest bit */ |
| 502 | ormask ^= bit; /* remove lowest bit */ |
| 503 | if (src_mask & dst_mask & bit) { /* matching channel */ |
| 504 | idxary[n++] = srcidx++; |
| 505 | } else if (src_mask & bit) { /* source channel only */ |
| 506 | ++srcidx; |
| 507 | } else { /* destination channel only */ |
| 508 | idxary[n++] = -1; |
| 509 | } |
| 510 | } |
| 511 | return n + popcount(ormask & dst_mask); |
| 512 | } |
Andy Hung | 5a0d028 | 2015-02-02 15:34:13 -0800 | [diff] [blame] | 513 | |
| 514 | size_t memcpy_by_index_array_initialization_src_index(int8_t *idxary, size_t idxcount, |
| 515 | uint32_t dst_mask, uint32_t src_mask) { |
| 516 | size_t dst_count = popcount(dst_mask); |
| 517 | if (idxcount == 0) { |
| 518 | return dst_count; |
| 519 | } |
| 520 | if (dst_count > idxcount) { |
| 521 | dst_count = idxcount; |
| 522 | } |
| 523 | |
| 524 | size_t src_idx, dst_idx; |
| 525 | for (src_idx = 0, dst_idx = 0; dst_idx < dst_count; ++dst_idx) { |
| 526 | if (src_mask & 1) { |
| 527 | idxary[dst_idx] = src_idx++; |
| 528 | } else { |
| 529 | idxary[dst_idx] = -1; |
| 530 | } |
| 531 | src_mask >>= 1; |
| 532 | } |
| 533 | return dst_idx; |
| 534 | } |
Andy Hung | 291a194 | 2015-02-27 14:20:33 -0800 | [diff] [blame] | 535 | |
| 536 | size_t memcpy_by_index_array_initialization_dst_index(int8_t *idxary, size_t idxcount, |
| 537 | uint32_t dst_mask, uint32_t src_mask) { |
| 538 | size_t src_idx, dst_idx; |
| 539 | size_t dst_count = __builtin_popcount(dst_mask); |
| 540 | size_t src_count = __builtin_popcount(src_mask); |
| 541 | if (idxcount == 0) { |
| 542 | return dst_count; |
| 543 | } |
| 544 | if (dst_count > idxcount) { |
| 545 | dst_count = idxcount; |
| 546 | } |
| 547 | for (src_idx = 0, dst_idx = 0; dst_idx < dst_count; ++src_idx) { |
| 548 | if (dst_mask & 1) { |
| 549 | idxary[dst_idx++] = src_idx < src_count ? (signed)src_idx : -1; |
| 550 | } |
| 551 | dst_mask >>= 1; |
| 552 | } |
| 553 | return dst_idx; |
| 554 | } |
Ari Hausman-Cohen | 90ac5f6 | 2017-08-16 18:32:22 -0700 | [diff] [blame] | 555 | |
| 556 | void accumulate_i16(int16_t *dst, const int16_t *src, size_t count) { |
| 557 | while (count--) { |
| 558 | *dst = clamp16((int32_t)*dst + *src++); |
| 559 | ++dst; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | void accumulate_u8(uint8_t *dst, const uint8_t *src, size_t count) { |
| 564 | int32_t sum; |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 565 | for (; count > 0; --count) { |
Ari Hausman-Cohen | 90ac5f6 | 2017-08-16 18:32:22 -0700 | [diff] [blame] | 566 | // 8-bit samples are centered around 0x80. |
| 567 | sum = *dst + *src++ - 0x80; |
| 568 | // Clamp to [0, 0xff]. |
| 569 | *dst++ = (sum & 0x100) ? (~sum >> 9) : sum; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | void accumulate_p24(uint8_t *dst, const uint8_t *src, size_t count) { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 574 | for (; count > 0; --count) { |
Ari Hausman-Cohen | 90ac5f6 | 2017-08-16 18:32:22 -0700 | [diff] [blame] | 575 | // Unpack. |
| 576 | int32_t dst_q8_23 = 0; |
| 577 | int32_t src_q8_23 = 0; |
| 578 | memcpy_to_q8_23_from_p24(&dst_q8_23, dst, 1); |
| 579 | memcpy_to_q8_23_from_p24(&src_q8_23, src, 1); |
| 580 | |
| 581 | // Accumulate and overwrite. |
| 582 | dst_q8_23 += src_q8_23; |
| 583 | memcpy_to_p24_from_q8_23(dst, &dst_q8_23, 1); |
| 584 | |
| 585 | // Move on to next sample. |
| 586 | dst += 3; |
| 587 | src += 3; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | void accumulate_q8_23(int32_t *dst, const int32_t *src, size_t count) { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 592 | for (; count > 0; --count) { |
Ari Hausman-Cohen | 90ac5f6 | 2017-08-16 18:32:22 -0700 | [diff] [blame] | 593 | *dst = clamp24_from_q8_23(*dst + *src++); |
| 594 | ++dst; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | void accumulate_i32(int32_t *dst, const int32_t *src, size_t count) { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 599 | for (; count > 0; --count) { |
Ari Hausman-Cohen | 90ac5f6 | 2017-08-16 18:32:22 -0700 | [diff] [blame] | 600 | *dst = clamp32((int64_t)*dst + *src++); |
| 601 | ++dst; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | void accumulate_float(float *dst, const float *src, size_t count) { |
Andy Hung | 1307aa7 | 2017-11-17 15:46:55 -0800 | [diff] [blame] | 606 | for (; count > 0; --count) { |
Ari Hausman-Cohen | 90ac5f6 | 2017-08-16 18:32:22 -0700 | [diff] [blame] | 607 | *dst++ += *src++; |
| 608 | } |
| 609 | } |