2 * Copyright (C) 2011 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 #ifndef ANDROID_AUDIO_PRIMITIVES_H
18 #define ANDROID_AUDIO_PRIMITIVES_H
22 #include <sys/cdefs.h>
30 * The memcpy_* conversion routines are designed to work in-place on same dst as src
31 * buffers only if the types shrink on copy, with the exception of memcpy_to_i16_from_u8().
32 * This allows the loops to go upwards for faster cache access (and may be more flexible
33 * for future optimization later).
37 * Dither and clamp pairs of 32-bit input samples (sums) to 16-bit output samples (out).
38 * Each 32-bit input sample can be viewed as a signed fixed-point Q19.12 of which the
39 * .12 fraction bits are dithered and the 19 integer bits are clamped to signed 16 bits.
40 * Alternatively the input can be viewed as Q4.27, of which the lowest .12 of the fraction
41 * is dithered and the remaining fraction is converted to the output Q.15, with clamping
42 * on the 4 integer guard bits.
44 * For interleaved stereo, c is the number of sample pairs,
45 * and out is an array of interleaved pairs of 16-bit samples per channel.
46 * For mono, c is the number of samples / 2, and out is an array of 16-bit samples.
47 * The name "dither" is a misnomer; the current implementation does not actually dither
48 * but uses truncation. This may change.
49 * The out and sums buffers must either be completely separate (non-overlapping), or
50 * they must both start at the same address. Partially overlapping buffers are not supported.
52 void ditherAndClamp(int32_t* out, const int32_t *sums, size_t c);
55 * Expand and copy samples from unsigned 8-bit offset by 0x80 to signed 16-bit.
57 * \param dst Destination buffer
58 * \param src Source buffer
59 * \param count Number of samples to copy
61 * The destination and source buffers must either be completely separate (non-overlapping), or
62 * they must both start at the same address. Partially overlapping buffers are not supported.
64 void memcpy_to_i16_from_u8(int16_t *dst, const uint8_t *src, size_t count);
67 * Shrink and copy samples from signed 16-bit to unsigned 8-bit offset by 0x80.
69 * \param dst Destination buffer
70 * \param src Source buffer
71 * \param count Number of samples to copy
73 * The destination and source buffers must either be completely separate (non-overlapping), or
74 * they must both start at the same address. Partially overlapping buffers are not supported.
75 * The conversion is done by truncation, without dithering, so it loses resolution.
77 void memcpy_to_u8_from_i16(uint8_t *dst, const int16_t *src, size_t count);
80 * Copy samples from float to unsigned 8-bit offset by 0x80.
82 * \param dst Destination buffer
83 * \param src Source buffer
84 * \param count Number of samples to copy
86 * The destination and source buffers must either be completely separate (non-overlapping), or
87 * they must both start at the same address. Partially overlapping buffers are not supported.
88 * The conversion is done by truncation, without dithering, so it loses resolution.
90 void memcpy_to_u8_from_float(uint8_t *dst, const float *src, size_t count);
93 * Shrink and copy samples from signed 32-bit fixed-point Q0.31 to signed 16-bit Q0.15.
95 * \param dst Destination buffer
96 * \param src Source buffer
97 * \param count Number of samples to copy
99 * The destination and source buffers must either be completely separate (non-overlapping), or
100 * they must both start at the same address. Partially overlapping buffers are not supported.
101 * The conversion is done by truncation, without dithering, so it loses resolution.
103 void memcpy_to_i16_from_i32(int16_t *dst, const int32_t *src, size_t count);
106 * Shrink and copy samples from single-precision floating-point to signed 16-bit.
107 * Each float should be in the range -1.0 to 1.0. Values outside that range are clamped,
108 * refer to clamp16_from_float().
110 * \param dst Destination buffer
111 * \param src Source buffer
112 * \param count Number of samples to copy
114 * The destination and source buffers must either be completely separate (non-overlapping), or
115 * they must both start at the same address. Partially overlapping buffers are not supported.
116 * The conversion is done by truncation, without dithering, so it loses resolution.
118 void memcpy_to_i16_from_float(int16_t *dst, const float *src, size_t count);
121 * Copy samples from signed fixed-point 32-bit Q4.27 to single-precision floating-point.
122 * The nominal output float range is [-1.0, 1.0] if the fixed-point range is
123 * [0xf8000000, 0x07ffffff]. The full float range is [-16.0, 16.0]. Note the closed range
124 * at 1.0 and 16.0 is due to rounding on conversion to float. See float_from_q4_27() for details.
126 * \param dst Destination buffer
127 * \param src Source buffer
128 * \param count Number of samples to copy
130 * The destination and source buffers must either be completely separate (non-overlapping), or
131 * they must both start at the same address. Partially overlapping buffers are not supported.
133 void memcpy_to_float_from_q4_27(float *dst, const int32_t *src, size_t count);
136 * Copy samples from signed fixed-point 16 bit Q0.15 to single-precision floating-point.
137 * The output float range is [-1.0, 1.0) for the fixed-point range [0x8000, 0x7fff].
138 * No rounding is needed as the representation is exact.
140 * \param dst Destination buffer
141 * \param src Source buffer
142 * \param count Number of samples to copy
144 * The destination and source buffers must be completely separate.
146 void memcpy_to_float_from_i16(float *dst, const int16_t *src, size_t count);
149 * Copy samples from unsigned fixed-point 8 bit to single-precision floating-point.
150 * The output float range is [-1.0, 1.0) for the fixed-point range [0x00, 0xFF].
151 * No rounding is needed as the representation is exact.
153 * \param dst Destination buffer
154 * \param src Source buffer
155 * \param count Number of samples to copy
157 * The destination and source buffers must be completely separate.
159 void memcpy_to_float_from_u8(float *dst, const uint8_t *src, size_t count);
162 * Copy samples from signed fixed-point packed 24 bit Q0.23 to single-precision floating-point.
163 * The packed 24 bit input is stored in native endian format in a uint8_t byte array.
164 * The output float range is [-1.0, 1.0) for the fixed-point range [0x800000, 0x7fffff].
165 * No rounding is needed as the representation is exact.
167 * \param dst Destination buffer
168 * \param src Source buffer
169 * \param count Number of samples to copy
171 * The destination and source buffers must be completely separate.
173 void memcpy_to_float_from_p24(float *dst, const uint8_t *src, size_t count);
176 * Copy samples from signed fixed-point packed 24 bit Q0.23 to signed fixed point 16 bit Q0.15.
177 * The packed 24 bit output is stored in native endian format in a uint8_t byte array.
178 * The data is truncated without rounding.
180 * \param dst Destination buffer
181 * \param src Source buffer
182 * \param count Number of samples to copy
184 * The destination and source buffers must either be completely separate (non-overlapping), or
185 * they must both start at the same address. Partially overlapping buffers are not supported.
187 void memcpy_to_i16_from_p24(int16_t *dst, const uint8_t *src, size_t count);
190 * Copy samples from signed fixed-point packed 24 bit Q0.23 to signed fixed-point 32-bit Q0.31.
191 * The packed 24 bit input is stored in native endian format in a uint8_t byte array.
192 * The output data range is [0x80000000, 0x7fffff00] at intervals of 0x100.
194 * \param dst Destination buffer
195 * \param src Source buffer
196 * \param count Number of samples to copy
198 * The destination and source buffers must be completely separate.
200 void memcpy_to_i32_from_p24(int32_t *dst, const uint8_t *src, size_t count);
203 * Copy samples from signed fixed point 16 bit Q0.15 to signed fixed-point packed 24 bit Q0.23.
204 * The packed 24 bit output is assumed to be a native-endian uint8_t byte array.
205 * The output data range is [0x800000, 0x7fff00] (not full).
206 * Nevertheless there is no DC offset on the output, if the input has no DC offset.
208 * \param dst Destination buffer
209 * \param src Source buffer
210 * \param count Number of samples to copy
212 * The destination and source buffers must be completely separate.
214 void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count);
217 * Copy samples from single-precision floating-point to signed fixed-point packed 24 bit Q0.23.
218 * The packed 24 bit output is assumed to be a native-endian uint8_t byte array.
219 * The data is clamped and rounded to nearest, ties away from zero. See clamp24_from_float()
222 * \param dst Destination buffer
223 * \param src Source buffer
224 * \param count Number of samples to copy
226 * The destination and source buffers must either be completely separate (non-overlapping), or
227 * they must both start at the same address. Partially overlapping buffers are not supported.
229 void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count);
232 * Copy samples from signed fixed-point 32-bit Q8.23 to signed fixed-point packed 24 bit Q0.23.
233 * The packed 24 bit output is assumed to be a native-endian uint8_t byte array.
234 * The data is clamped to the range is [0x800000, 0x7fffff].
236 * \param dst Destination buffer
237 * \param src Source buffer
238 * \param count Number of samples to copy
240 * The destination and source buffers must be completely separate.
242 void memcpy_to_p24_from_q8_23(uint8_t *dst, const int32_t *src, size_t count);
245 * Shrink and copy samples from signed 32-bit fixed-point Q0.31
246 * to signed fixed-point packed 24 bit Q0.23.
247 * The packed 24 bit output is assumed to be a native-endian uint8_t byte array.
249 * \param dst Destination buffer
250 * \param src Source buffer
251 * \param count Number of samples to copy
253 * The destination and source buffers must either be completely separate (non-overlapping), or
254 * they must both start at the same address. Partially overlapping buffers are not supported.
255 * The conversion is done by truncation, without dithering, so it loses resolution.
257 void memcpy_to_p24_from_i32(uint8_t *dst, const int32_t *src, size_t count);
260 * Copy samples from signed fixed point 16-bit Q0.15 to signed fixed-point 32-bit Q8.23.
261 * The output data range is [0xff800000, 0x007fff00] at intervals of 0x100.
263 * \param dst Destination buffer
264 * \param src Source buffer
265 * \param count Number of samples to copy
267 * The destination and source buffers must be completely separate.
269 void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count);
272 * Copy samples from single-precision floating-point to signed fixed-point 32-bit Q8.23.
273 * This copy will clamp the Q8.23 representation to [0xff800000, 0x007fffff] even though there
274 * are guard bits available. Fractional lsb is rounded to nearest, ties away from zero.
275 * See clamp24_from_float() for details.
277 * \param dst Destination buffer
278 * \param src Source buffer
279 * \param count Number of samples to copy
281 * The destination and source buffers must either be completely separate (non-overlapping), or
282 * they must both start at the same address. Partially overlapping buffers are not supported.
284 void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count);
287 * Copy samples from signed fixed point packed 24-bit Q0.23 to signed fixed-point 32-bit Q8.23.
288 * The output data range is [0xff800000, 0x007fffff].
290 * \param dst Destination buffer
291 * \param src Source buffer
292 * \param count Number of samples to copy
294 * The destination and source buffers must be completely separate.
296 void memcpy_to_q8_23_from_p24(int32_t *dst, const uint8_t *src, size_t count);
299 * Copy samples from single-precision floating-point to signed fixed-point 32-bit Q4.27.
300 * The conversion will use the full available Q4.27 range, including guard bits.
301 * Fractional lsb is rounded to nearest, ties away from zero.
302 * See clampq4_27_from_float() for details.
304 * \param dst Destination buffer
305 * \param src Source buffer
306 * \param count Number of samples to copy
308 * The destination and source buffers must either be completely separate (non-overlapping), or
309 * they must both start at the same address. Partially overlapping buffers are not supported.
311 void memcpy_to_q4_27_from_float(int32_t *dst, const float *src, size_t count);
314 * Copy samples from signed fixed-point 32-bit Q8.23 to signed fixed point 16-bit Q0.15.
315 * The data is clamped, and truncated without rounding.
317 * \param dst Destination buffer
318 * \param src Source buffer
319 * \param count Number of samples to copy
321 * The destination and source buffers must either be completely separate (non-overlapping), or
322 * they must both start at the same address. Partially overlapping buffers are not supported.
324 void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count);
327 * Copy samples from signed fixed-point 32-bit Q8.23 to single-precision floating-point.
328 * The nominal output float range is [-1.0, 1.0) for the fixed-point
329 * range [0xff800000, 0x007fffff]. The maximum output float range is [-256.0, 256.0).
330 * No rounding is needed as the representation is exact for nominal values.
331 * Rounding for overflow values is to nearest, ties to even.
333 * \param dst Destination buffer
334 * \param src Source buffer
335 * \param count Number of samples to copy
337 * The destination and source buffers must either be completely separate (non-overlapping), or
338 * they must both start at the same address. Partially overlapping buffers are not supported.
340 void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count);
343 * Copy samples from signed fixed point 16-bit Q0.15 to signed fixed-point 32-bit Q0.31.
344 * The output data range is [0x80000000, 0x7fff0000] at intervals of 0x10000.
346 * \param dst Destination buffer
347 * \param src Source buffer
348 * \param count Number of samples to copy
350 * The destination and source buffers must be completely separate.
352 void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count);
355 * Copy samples from single-precision floating-point to signed fixed-point 32-bit Q0.31.
356 * If rounding is needed on truncation, the fractional lsb is rounded to nearest,
357 * ties away from zero. See clamp32_from_float() for details.
359 * \param dst Destination buffer
360 * \param src Source buffer
361 * \param count Number of samples to copy
363 * The destination and source buffers must either be completely separate (non-overlapping), or
364 * they must both start at the same address. Partially overlapping buffers are not supported.
366 void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count);
369 * Copy samples from signed fixed-point 32-bit Q0.31 to single-precision floating-point.
370 * The float range is [-1.0, 1.0] for the fixed-point range [0x80000000, 0x7fffffff].
371 * Rounding is done according to float_from_i32().
373 * \param dst Destination buffer
374 * \param src Source buffer
375 * \param count Number of samples to copy
377 * The destination and source buffers must either be completely separate (non-overlapping), or
378 * they must both start at the same address. Partially overlapping buffers are not supported.
380 void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count);
383 * Downmix pairs of interleaved stereo input 16-bit samples to mono output 16-bit samples.
385 * \param dst Destination buffer
386 * \param src Source buffer
387 * \param count Number of stereo frames to downmix
389 * The destination and source buffers must be completely separate (non-overlapping).
390 * The current implementation truncates the mean rather than dither, but this may change.
392 void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count);
395 * Upmix mono input 16-bit samples to pairs of interleaved stereo output 16-bit samples by
398 * \param dst Destination buffer
399 * \param src Source buffer
400 * \param count Number of mono samples to upmix
402 * The destination and source buffers must be completely separate (non-overlapping).
404 void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count);
407 * Downmix pairs of interleaved stereo input float samples to mono output float samples
408 * by averaging the stereo pair together.
410 * \param dst Destination buffer
411 * \param src Source buffer
412 * \param count Number of stereo frames to downmix
414 * The destination and source buffers must be completely separate (non-overlapping),
415 * or they must both start at the same address.
417 void downmix_to_mono_float_from_stereo_float(float *dst, const float *src, size_t count);
420 * Upmix mono input float samples to pairs of interleaved stereo output float samples by
423 * \param dst Destination buffer
424 * \param src Source buffer
425 * \param count Number of mono samples to upmix
427 * The destination and source buffers must be completely separate (non-overlapping).
429 void upmix_to_stereo_float_from_mono_float(float *dst, const float *src, size_t count);
432 * \return the total number of non-zero 32-bit samples.
434 size_t nonZeroMono32(const int32_t *samples, size_t count);
437 * \return the total number of non-zero 16-bit samples.
439 size_t nonZeroMono16(const int16_t *samples, size_t count);
442 * \return the total number of non-zero stereo frames, where a frame is considered non-zero
443 * if either of its constituent 32-bit samples is non-zero.
445 size_t nonZeroStereo32(const int32_t *frames, size_t count);
448 * \return the total number of non-zero stereo frames, where a frame is considered non-zero
449 * if either of its constituent 16-bit samples is non-zero.
451 size_t nonZeroStereo16(const int16_t *frames, size_t count);
454 * Copy frames, selecting source samples based on a source channel mask to fit
455 * the destination channel mask. Unmatched channels in the destination channel mask
456 * are zero filled. Unmatched channels in the source channel mask are dropped.
457 * Channels present in the channel mask are represented by set bits in the
458 * uint32_t value and are matched without further interpretation.
460 * \param dst Destination buffer
461 * \param dst_mask Bit mask corresponding to destination channels present
462 * \param src Source buffer
463 * \param src_mask Bit mask corresponding to source channels present
464 * \param sample_size Size of each sample in bytes. Must be 1, 2, 3, or 4.
465 * \param count Number of frames to copy
467 * The destination and source buffers must be completely separate (non-overlapping).
468 * If the sample size is not in range, the function will abort.
470 void memcpy_by_channel_mask(void *dst, uint32_t dst_mask,
471 const void *src, uint32_t src_mask, size_t sample_size, size_t count);
474 * Copy frames, selecting source samples based on an index array (idxary).
475 * The idxary[] consists of dst_channels number of elements.
476 * The ith element if idxary[] corresponds the ith destination channel.
477 * A non-negative value is the channel index in the source frame.
478 * A negative index (-1) represents filling with 0.
480 * Example: Swapping L and R channels for stereo streams
486 * Example: Copying a mono source to the front center 5.1 channel
496 * This copy allows swizzling of channels or replication of channels.
498 * \param dst Destination buffer
499 * \param dst_channels Number of destination channels per frame
500 * \param src Source buffer
501 * \param src_channels Number of source channels per frame
502 * \param idxary Array of indices representing channels in the source frame
503 * \param sample_size Size of each sample in bytes. Must be 1, 2, 3, or 4.
504 * \param count Number of frames to copy
506 * The destination and source buffers must be completely separate (non-overlapping).
507 * If the sample size is not in range, the function will abort.
509 void memcpy_by_index_array(void *dst, uint32_t dst_channels,
510 const void *src, uint32_t src_channels,
511 const int8_t *idxary, size_t sample_size, size_t count);
514 * Prepares an index array (idxary) from channel masks, which can be later
515 * used by memcpy_by_index_array().
517 * \return the number of array elements required.
518 * This may be greater than idxcount, so the return value should be checked
519 * if idxary size is less than 32.
521 * Note that idxary is a caller allocated array
522 * of at least as many channels as present in the dst_mask.
523 * Channels present in the channel mask are represented by set bits in the
524 * uint32_t value and are matched without further interpretation.
526 * This function is typically used for converting audio data with different
527 * channel position masks.
529 * \param idxary Updated array of indices of channels in the src frame for the dst frame
530 * \param idxcount Number of caller allocated elements in idxary
531 * \param dst_mask Bit mask corresponding to destination channels present
532 * \param src_mask Bit mask corresponding to source channels present
534 size_t memcpy_by_index_array_initialization(int8_t *idxary, size_t idxcount,
535 uint32_t dst_mask, uint32_t src_mask);
538 * Prepares an index array (idxary) from channel masks, which can be later
539 * used by memcpy_by_index_array().
541 * \return the number of array elements required.
543 * For a source channel index mask, the source channels will map to the destination
544 * channels as if counting the set bits in dst_mask in order from lsb to msb
545 * (zero bits are ignored). The ith bit of the src_mask corresponds to the
546 * ith SET bit of dst_mask and the ith destination channel. Hence, a zero ith
547 * bit of the src_mask indicates that the ith destination channel plays silence.
549 * \param idxary Updated array of indices of channels in the src frame for the dst frame
550 * \param idxcount Number of caller allocated elements in idxary
551 * \param dst_mask Bit mask corresponding to destination channels present
552 * \param src_mask Bit mask corresponding to source channels present
554 size_t memcpy_by_index_array_initialization_src_index(int8_t *idxary, size_t idxcount,
555 uint32_t dst_mask, uint32_t src_mask);
558 * Prepares an index array (idxary) from channel mask bits, which can be later
559 * used by memcpy_by_index_array().
561 * \return the number of array elements required.
563 * This initialization is for a destination channel index mask from a positional
566 * For an destination channel index mask, the input channels will map
567 * to the destination channels, with the ith SET bit in the source bits corresponding
568 * to the ith bit in the destination bits. If there is a zero bit in the middle
569 * of set destination bits (unlikely), the corresponding source channel will
572 * \param idxary Updated array of indices of channels in the src frame for the dst frame
573 * \param idxcount Number of caller allocated elements in idxary
574 * \param dst_mask Bit mask corresponding to destination channels present
575 * \param src_mask Bit mask corresponding to source channels present
577 size_t memcpy_by_index_array_initialization_dst_index(int8_t *idxary, size_t idxcount,
578 uint32_t dst_mask, uint32_t src_mask);
581 * Clamp (aka hard limit or clip) a signed 32-bit sample to 16-bit range.
583 static inline int16_t clamp16(int32_t sample)
585 if ((sample>>15) ^ (sample>>31))
586 sample = 0x7FFF ^ (sample>>31);
591 * Convert a IEEE 754 single precision float [-1.0, 1.0) to int16_t [-32768, 32767]
592 * with clamping. Note the open bound at 1.0, values within 1/65536 of 1.0 map
593 * to 32767 instead of 32768 (early clamping due to the smaller positive integer subrange).
595 * Values outside the range [-1.0, 1.0) are properly clamped to -32768 and 32767,
596 * including -Inf and +Inf. NaN will generally be treated either as -32768 or 32767,
597 * depending on the sign bit inside NaN (whose representation is not unique).
598 * Nevertheless, strictly speaking, NaN behavior should be considered undefined.
600 * Rounding of 0.5 lsb is to even (default for IEEE 754).
602 static inline int16_t clamp16_from_float(float f)
604 /* Offset is used to expand the valid range of [-1.0, 1.0) into the 16 lsbs of the
605 * floating point significand. The normal shift is 3<<22, but the -15 offset
606 * is used to multiply by 32768.
608 static const float offset = (float)(3 << (22 - 15));
609 /* zero = (0x10f << 22) = 0x43c00000 (not directly used) */
610 static const int32_t limneg = (0x10f << 22) /*zero*/ - 32768; /* 0x43bf8000 */
611 static const int32_t limpos = (0x10f << 22) /*zero*/ + 32767; /* 0x43c07fff */
618 u.f = f + offset; /* recenter valid range */
619 /* Now the valid range is represented as integers between [limneg, limpos].
620 * Clamp using the fact that float representation (as an integer) is an ordered set.
624 else if (u.i > limpos)
626 return u.i; /* Return lower 16 bits, the part of interest in the significand. */
630 * Convert a IEEE 754 single precision float [-1.0, 1.0) to uint8_t [0, 0xff]
631 * with clamping. Note the open bound at 1.0, values within 1/128 of 1.0 map
632 * to 255 instead of 256 (early clamping due to the smaller positive integer subrange).
634 * Values outside the range [-1.0, 1.0) are properly clamped to 0 and 255,
635 * including -Inf and +Inf. NaN will generally be treated either as 0 or 255,
636 * depending on the sign bit inside NaN (whose representation is not unique).
637 * Nevertheless, strictly speaking, NaN behavior should be considered undefined.
639 * Rounding of 0.5 lsb is to even (default for IEEE 754).
641 static inline uint8_t clamp8_from_float(float f)
643 /* Offset is used to expand the valid range of [-1.0, 1.0) into the 16 lsbs of the
644 * floating point significand. The normal shift is 3<<22, but the -7 offset
645 * is used to multiply by 128.
647 static const float offset = (float)((3 << (22 - 7)) + 1 /* to cancel -1.0 */);
648 /* zero = (0x11f << 22) = 0x47c00000 */
649 static const int32_t limneg = (0x11f << 22) /*zero*/;
650 static const int32_t limpos = (0x11f << 22) /*zero*/ + 255; /* 0x47c000ff */
657 u.f = f + offset; /* recenter valid range */
658 /* Now the valid range is represented as integers between [limneg, limpos].
659 * Clamp using the fact that float representation (as an integer) is an ordered set.
665 return u.i; /* Return lower 8 bits, the part of interest in the significand. */
669 * Convert a single-precision floating point value to a Q0.23 integer value, stored in a
670 * 32 bit signed integer (technically stored as Q8.23, but clamped to Q0.23).
672 * Rounds to nearest, ties away from 0.
674 * Values outside the range [-1.0, 1.0) are properly clamped to -8388608 and 8388607,
675 * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
676 * depending on hardware and future implementation of this function.
678 static inline int32_t clamp24_from_float(float f)
680 static const float scale = (float)(1 << 23);
681 static const float limpos = 0x7fffff / scale;
682 static const float limneg = -0x800000 / scale;
686 } else if (f >= limpos) {
690 /* integer conversion is through truncation (though int to float is not).
691 * ensure that we round to nearest, ties away from 0.
693 return f > 0 ? f + 0.5 : f - 0.5;
697 * Convert a signed fixed-point 32-bit Q8.23 value to a Q0.23 integer value,
698 * stored in a 32-bit signed integer (technically stored as Q8.23, but clamped to Q0.23).
700 * Values outside the range [-0x800000, 0x7fffff] are clamped to that range.
702 static inline int32_t clamp24_from_q8_23(int32_t ival)
704 static const int32_t limpos = 0x7fffff;
705 static const int32_t limneg = -0x800000;
708 } else if (ival > limpos) {
716 * Convert a single-precision floating point value to a Q4.27 integer value.
717 * Rounds to nearest, ties away from 0.
719 * Values outside the range [-16.0, 16.0) are properly clamped to -2147483648 and 2147483647,
720 * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
721 * depending on hardware and future implementation of this function.
723 static inline int32_t clampq4_27_from_float(float f)
725 static const float scale = (float)(1UL << 27);
726 static const float limpos = 16.;
727 static const float limneg = -16.;
730 return -0x80000000; /* or 0x80000000 */
731 } else if (f >= limpos) {
735 /* integer conversion is through truncation (though int to float is not).
736 * ensure that we round to nearest, ties away from 0.
738 return f > 0 ? f + 0.5 : f - 0.5;
742 * Convert a single-precision floating point value to a Q0.31 integer value.
743 * Rounds to nearest, ties away from 0.
745 * Values outside the range [-1.0, 1.0) are properly clamped to -2147483648 and 2147483647,
746 * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
747 * depending on hardware and future implementation of this function.
749 static inline int32_t clamp32_from_float(float f)
751 static const float scale = (float)(1UL << 31);
752 static const float limpos = 1.;
753 static const float limneg = -1.;
756 return -0x80000000; /* or 0x80000000 */
757 } else if (f >= limpos) {
761 /* integer conversion is through truncation (though int to float is not).
762 * ensure that we round to nearest, ties away from 0.
764 return f > 0 ? f + 0.5 : f - 0.5;
768 * Convert a signed fixed-point 32-bit Q4.27 value to single-precision floating-point.
769 * The nominal output float range is [-1.0, 1.0] if the fixed-point range is
770 * [0xf8000000, 0x07ffffff]. The full float range is [-16.0, 16.0].
772 * Note the closed range at 1.0 and 16.0 is due to rounding on conversion to float.
773 * In more detail: if the fixed-point integer exceeds 24 bit significand of single
774 * precision floating point, the 0.5 lsb in the significand conversion will round
775 * towards even, as per IEEE 754 default.
777 static inline float float_from_q4_27(int32_t ival)
779 /* The scale factor is the reciprocal of the fractional bits.
781 * Since the scale factor is a power of 2, the scaling is exact, and there
782 * is no rounding due to the multiplication - the bit pattern is preserved.
783 * However, there may be rounding due to the fixed-point to float conversion,
784 * as described above.
786 static const float scale = 1. / (float)(1UL << 27);
792 * Convert an unsigned fixed-point 32-bit U4.28 value to single-precision floating-point.
793 * The nominal output float range is [0.0, 1.0] if the fixed-point range is
794 * [0x00000000, 0x10000000]. The full float range is [0.0, 16.0].
796 * Note the closed range at 1.0 and 16.0 is due to rounding on conversion to float.
797 * In more detail: if the fixed-point integer exceeds 24 bit significand of single
798 * precision floating point, the 0.5 lsb in the significand conversion will round
799 * towards even, as per IEEE 754 default.
801 static inline float float_from_u4_28(uint32_t uval)
803 static const float scale = 1. / (float)(1UL << 28);
809 * Convert an unsigned fixed-point 16-bit U4.12 value to single-precision floating-point.
810 * The nominal output float range is [0.0, 1.0] if the fixed-point range is
811 * [0x0000, 0x1000]. The full float range is [0.0, 16.0).
813 static inline float float_from_u4_12(uint16_t uval)
815 static const float scale = 1. / (float)(1UL << 12);
821 * Convert a single-precision floating point value to a U4.28 integer value.
822 * Rounds to nearest, ties away from 0.
824 * Values outside the range [0, 16.0] are properly clamped to [0, 4294967295]
825 * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
826 * depending on hardware and future implementation of this function.
828 static inline uint32_t u4_28_from_float(float f)
830 static const float scale = (float)(1 << 28);
831 static const float limpos = 0xffffffffUL / scale;
835 } else if (f >= limpos) {
838 /* integer conversion is through truncation (though int to float is not).
839 * ensure that we round to nearest, ties away from 0.
841 return f * scale + 0.5;
845 * Convert a single-precision floating point value to a U4.12 integer value.
846 * Rounds to nearest, ties away from 0.
848 * Values outside the range [0, 16.0) are properly clamped to [0, 65535]
849 * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
850 * depending on hardware and future implementation of this function.
852 static inline uint16_t u4_12_from_float(float f)
854 static const float scale = (float)(1 << 12);
855 static const float limpos = 0xffff / scale;
859 } else if (f >= limpos) {
862 /* integer conversion is through truncation (though int to float is not).
863 * ensure that we round to nearest, ties away from 0.
865 return f * scale + 0.5;
869 * Convert a signed fixed-point 16-bit Q0.15 value to single-precision floating-point.
870 * The output float range is [-1.0, 1.0) for the fixed-point range
873 * There is no rounding, the conversion and representation is exact.
875 static inline float float_from_i16(int16_t ival)
877 /* The scale factor is the reciprocal of the nominal 16 bit integer
878 * half-sided range (32768).
880 * Since the scale factor is a power of 2, the scaling is exact, and there
881 * is no rounding due to the multiplication - the bit pattern is preserved.
883 static const float scale = 1. / (float)(1UL << 15);
889 * Convert an unsigned fixed-point 8-bit U0.8 value to single-precision floating-point.
890 * The nominal output float range is [-1.0, 1.0) if the fixed-point range is
893 static inline float float_from_u8(uint8_t uval)
895 static const float scale = 1. / (float)(1UL << 7);
897 return ((int)uval - 128) * scale;
901 * Convert a packed 24bit Q0.23 value stored native-endian in a uint8_t ptr
902 * to a signed fixed-point 32 bit integer Q0.31 value. The output Q0.31 range
903 * is [0x80000000, 0x7fffff00] for the fixed-point range [0x800000, 0x7fffff].
904 * Even though the output range is limited on the positive side, there is no
905 * DC offset on the output, if the input has no DC offset.
907 * Avoid relying on the limited output range, as future implementations may go
910 static inline int32_t i32_from_p24(const uint8_t *packed24)
913 return (packed24[0] << 8) | (packed24[1] << 16) | (packed24[2] << 24);
917 * Convert a 32-bit Q0.31 value to single-precision floating-point.
918 * The output float range is [-1.0, 1.0] for the fixed-point range
919 * [0x80000000, 0x7fffffff].
921 * Rounding may occur in the least significant 8 bits for large fixed point
922 * values due to storage into the 24-bit floating-point significand.
923 * Rounding will be to nearest, ties to even.
925 static inline float float_from_i32(int32_t ival)
927 static const float scale = 1. / (float)(1UL << 31);
933 * Convert a packed 24bit Q0.23 value stored native endian in a uint8_t ptr
934 * to single-precision floating-point. The output float range is [-1.0, 1.0)
935 * for the fixed-point range [0x800000, 0x7fffff].
937 * There is no rounding, the conversion and representation is exact.
939 static inline float float_from_p24(const uint8_t *packed24)
941 return float_from_i32(i32_from_p24(packed24));
945 * Convert a 24-bit Q8.23 value to single-precision floating-point.
946 * The nominal output float range is [-1.0, 1.0) for the fixed-point
947 * range [0xff800000, 0x007fffff]. The maximum float range is [-256.0, 256.0).
949 * There is no rounding in the nominal range, the conversion and representation
950 * is exact. For values outside the nominal range, rounding is to nearest, ties to even.
952 static inline float float_from_q8_23(int32_t ival)
954 static const float scale = 1. / (float)(1UL << 23);
960 * Multiply-accumulate 16-bit terms with 32-bit result: return a + in*v.
963 int32_t mulAdd(int16_t in, int16_t v, int32_t a)
965 #if defined(__arm__) && !defined(__thumb__)
967 asm( "smlabb %[out], %[in], %[v], %[a] \n"
969 : [in]"%r"(in), [v]"r"(v), [a]"r"(a)
973 return a + in * (int32_t)v;
978 * Multiply 16-bit terms with 32-bit result: return in*v.
981 int32_t mul(int16_t in, int16_t v)
983 #if defined(__arm__) && !defined(__thumb__)
985 asm( "smulbb %[out], %[in], %[v] \n"
987 : [in]"%r"(in), [v]"r"(v)
991 return in * (int32_t)v;
996 * Similar to mulAdd, but the 16-bit terms are extracted from a 32-bit interleaved stereo pair.
999 int32_t mulAddRL(int left, uint32_t inRL, uint32_t vRL, int32_t a)
1001 #if defined(__arm__) && !defined(__thumb__)
1004 asm( "smlabb %[out], %[inRL], %[vRL], %[a] \n"
1006 : [inRL]"%r"(inRL), [vRL]"r"(vRL), [a]"r"(a)
1009 asm( "smlatt %[out], %[inRL], %[vRL], %[a] \n"
1011 : [inRL]"%r"(inRL), [vRL]"r"(vRL), [a]"r"(a)
1017 return a + (int16_t)(inRL&0xFFFF) * (int16_t)(vRL&0xFFFF);
1019 return a + (int16_t)(inRL>>16) * (int16_t)(vRL>>16);
1025 * Similar to mul, but the 16-bit terms are extracted from a 32-bit interleaved stereo pair.
1028 int32_t mulRL(int left, uint32_t inRL, uint32_t vRL)
1030 #if defined(__arm__) && !defined(__thumb__)
1033 asm( "smulbb %[out], %[inRL], %[vRL] \n"
1035 : [inRL]"%r"(inRL), [vRL]"r"(vRL)
1038 asm( "smultt %[out], %[inRL], %[vRL] \n"
1040 : [inRL]"%r"(inRL), [vRL]"r"(vRL)
1046 return (int16_t)(inRL&0xFFFF) * (int16_t)(vRL&0xFFFF);
1048 return (int16_t)(inRL>>16) * (int16_t)(vRL>>16);
1057 #endif // ANDROID_AUDIO_PRIMITIVES_H