2 * Copyright 2017 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 // #define LOG_NDEBUG 0
18 #define LOG_TAG "audio_utils_power"
24 #include <audio_utils/power.h>
25 #include <audio_utils/primitives.h>
27 #if defined(__aarch64__) || defined(__ARM_NEON__)
34 constexpr inline bool isFormatSupported(audio_format_t format) {
36 case AUDIO_FORMAT_PCM_8_BIT:
37 case AUDIO_FORMAT_PCM_16_BIT:
38 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
39 case AUDIO_FORMAT_PCM_8_24_BIT:
40 case AUDIO_FORMAT_PCM_32_BIT:
41 case AUDIO_FORMAT_PCM_FLOAT:
49 inline T getPtrPtrValueAndIncrement(const void **data)
51 return *(*reinterpret_cast<const T **>(data))++;
54 template <audio_format_t FORMAT>
55 inline float convertToFloatAndIncrement(const void **data)
58 case AUDIO_FORMAT_PCM_8_BIT:
59 return float_from_u8(getPtrPtrValueAndIncrement<uint8_t>(data));
61 case AUDIO_FORMAT_PCM_16_BIT:
62 return float_from_i16(getPtrPtrValueAndIncrement<int16_t>(data));
64 case AUDIO_FORMAT_PCM_24_BIT_PACKED: {
65 const uint8_t *uptr = reinterpret_cast<const uint8_t *>(*data);
67 return float_from_p24(uptr);
70 case AUDIO_FORMAT_PCM_8_24_BIT:
71 return float_from_q8_23(getPtrPtrValueAndIncrement<int32_t>(data));
73 case AUDIO_FORMAT_PCM_32_BIT:
74 return float_from_i32(getPtrPtrValueAndIncrement<int32_t>(data));
76 case AUDIO_FORMAT_PCM_FLOAT:
77 return getPtrPtrValueAndIncrement<float>(data);
80 // static_assert cannot use false because the compiler may interpret it
81 // even though this code path may never be taken.
82 static_assert(isFormatSupported(FORMAT), "unsupported format");
86 // used to normalize integer fixed point value to the floating point equivalent.
87 template <audio_format_t FORMAT>
88 constexpr inline float normalizeAmplitude()
91 case AUDIO_FORMAT_PCM_8_BIT:
92 return 1.f / (1 << 7);
94 case AUDIO_FORMAT_PCM_16_BIT:
95 return 1.f / (1 << 15);
97 case AUDIO_FORMAT_PCM_24_BIT_PACKED: // fall through
98 case AUDIO_FORMAT_PCM_8_24_BIT:
99 return 1.f / (1 << 23);
101 case AUDIO_FORMAT_PCM_32_BIT:
102 return 1.f / (1U << 31);
104 case AUDIO_FORMAT_PCM_FLOAT:
108 // static_assert cannot use false because the compiler may interpret it
109 // even though this code path may never be taken.
110 static_assert(isFormatSupported(FORMAT), "unsupported format");
114 template <audio_format_t FORMAT>
115 constexpr inline float normalizeEnergy()
117 const float val = normalizeAmplitude<FORMAT>();
121 template <audio_format_t FORMAT>
122 inline float energyMonoRef(const void *amplitudes, size_t size)
125 for (size_t i = 0; i < size; ++i) {
126 const float amplitude = convertToFloatAndIncrement<FORMAT>(&litudes);
127 accum += amplitude * amplitude;
132 template <audio_format_t FORMAT>
133 inline float energyMono(const void *amplitudes, size_t size)
135 return energyMonoRef<FORMAT>(amplitudes, size);
138 // fast float power computation for ARM processors that support NEON.
141 template <typename T>
142 float32x4_t convertToFloatVectorAmplitude(T vamplitude) = delete;
145 float32x4_t convertToFloatVectorAmplitude<float32x4_t>(float32x4_t vamplitude) {
150 float32x4_t convertToFloatVectorAmplitude<int16x4_t>(int16x4_t vamplitude) {
151 const int32x4_t iamplitude = vmovl_s16(vamplitude); // expand s16 to s32 first
152 return vcvtq_f32_s32(iamplitude);
156 float32x4_t convertToFloatVectorAmplitude<int32x4_t>(int32x4_t vamplitude) {
157 return vcvtq_f32_s32(vamplitude);
160 template <typename Vector, typename Scalar>
161 inline float energyMonoVector(const void *amplitudes, size_t size)
163 static_assert(sizeof(Vector) % sizeof(Scalar) == 0,
164 "Vector size must be a multiple of scalar size");
165 const size_t vectorLength = sizeof(Vector) / sizeof(Scalar); // typically 4 (a const)
167 // check pointer validity, must be aligned with scalar type.
168 const Scalar *samplitudes = reinterpret_cast<const Scalar *>(amplitudes);
169 LOG_ALWAYS_FATAL_IF((uintptr_t)samplitudes % alignof(Scalar) != 0,
170 "Non-element aligned address: %p %zu", samplitudes, alignof(Scalar));
172 float accumulator = 0;
174 // handle pointer unaligned to vector type.
175 while ((uintptr_t)samplitudes % alignof(Vector) != 0 /* compiler optimized */ && size > 0) {
176 const float amp = (float)*samplitudes++;
177 accumulator += amp * amp;
181 // samplitudes is now adjusted for proper vector alignment, cast to Vector *
182 const Vector *vamplitudes = reinterpret_cast<const Vector *>(samplitudes);
184 // clear vector accumulator
185 float32x4_t accum = vdupq_n_f32(0);
187 // iterate over array getting sum of squares in vectorLength lanes.
189 for (i = 0; i < size - size % vectorLength /* compiler optimized */; i += vectorLength) {
190 const float32x4_t famplitude = convertToFloatVectorAmplitude(*vamplitudes++);
191 accum = vmlaq_f32(accum, famplitude, famplitude);
194 // narrow vectorLength lanes of floats
195 float32x2_t accum2 = vadd_f32(vget_low_f32(accum), vget_high_f32(accum)); // get stereo volume
196 accum2 = vpadd_f32(accum2, accum2); // combine to mono
199 accumulator += vget_lane_f32(accum2, 0);
201 // accumulate any trailing elements too small for vector size
202 for (; i < size; ++i) {
203 const float amp = (float)samplitudes[i];
204 accumulator += amp * amp;
210 inline float energyMono<AUDIO_FORMAT_PCM_FLOAT>(const void *amplitudes, size_t size)
212 return energyMonoVector<float32x4_t, float>(amplitudes, size);
216 inline float energyMono<AUDIO_FORMAT_PCM_16_BIT>(const void *amplitudes, size_t size)
218 return energyMonoVector<int16x4_t, int16_t>(amplitudes, size)
219 * normalizeEnergy<AUDIO_FORMAT_PCM_16_BIT>();
222 // fast int32_t power computation for PCM_32
224 inline float energyMono<AUDIO_FORMAT_PCM_32_BIT>(const void *amplitudes, size_t size)
226 return energyMonoVector<int32x4_t, int32_t>(amplitudes, size)
227 * normalizeEnergy<AUDIO_FORMAT_PCM_32_BIT>();
230 // fast int32_t power computation for PCM_8_24 (essentially identical to PCM_32 above)
232 inline float energyMono<AUDIO_FORMAT_PCM_8_24_BIT>(const void *amplitudes, size_t size)
234 return energyMonoVector<int32x4_t, int32_t>(amplitudes, size)
235 * normalizeEnergy<AUDIO_FORMAT_PCM_8_24_BIT>();
242 float audio_utils_compute_energy_mono(const void *buffer, audio_format_t format, size_t samples)
245 case AUDIO_FORMAT_PCM_8_BIT:
246 return energyMono<AUDIO_FORMAT_PCM_8_BIT>(buffer, samples);
248 case AUDIO_FORMAT_PCM_16_BIT:
249 return energyMono<AUDIO_FORMAT_PCM_16_BIT>(buffer, samples);
251 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
252 return energyMono<AUDIO_FORMAT_PCM_24_BIT_PACKED>(buffer, samples);
254 case AUDIO_FORMAT_PCM_8_24_BIT:
255 return energyMono<AUDIO_FORMAT_PCM_8_24_BIT>(buffer, samples);
257 case AUDIO_FORMAT_PCM_32_BIT:
258 return energyMono<AUDIO_FORMAT_PCM_32_BIT>(buffer, samples);
260 case AUDIO_FORMAT_PCM_FLOAT:
261 return energyMono<AUDIO_FORMAT_PCM_FLOAT>(buffer, samples);
264 LOG_ALWAYS_FATAL("invalid format: %#x", format);
268 float audio_utils_compute_power_mono(const void *buffer, audio_format_t format, size_t samples)
270 return audio_utils_power_from_energy(
271 audio_utils_compute_energy_mono(buffer, format, samples) / samples);
274 bool audio_utils_is_compute_power_format_supported(audio_format_t format)
276 return isFormatSupported(format);