]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/sample-util.h
core: Refactor code to multiply s16 by volume
[pulseaudio] / src / pulsecore / sample-util.h
index d308caa0bdf17379d4938642f61b2ce5ffca6e82..dda1be5e585555169ca5902e2bc5b2c04a7a837e 100644 (file)
@@ -23,6 +23,9 @@
   USA.
 ***/
 
+#include <inttypes.h>
+#include <limits.h>
+
 #include <pulse/gccmacro.h>
 #include <pulse/sample.h>
 #include <pulse/volume.h>
@@ -53,6 +56,23 @@ void pa_deinterleave(const void *src, void *dst[], unsigned channels, size_t ss,
 
 void pa_sample_clamp(pa_sample_format_t format, void *dst, size_t dstr, const void *src, size_t sstr, unsigned n);
 
+static inline int32_t pa_mult_s16_volume(int16_t v, int32_t cv) {
+#if __WORDSIZE == 64 || ((ULONG_MAX) > (UINT_MAX))
+    /* Multiply with 64 bit integers on 64 bit platforms */
+    return (v * (int64_t) cv) >> 16;
+#else
+    /* Multiplying the 32 bit volume factor with the
+     * 16 bit sample might result in an 48 bit value. We
+     * want to do without 64 bit integers and hence do
+     * the multiplication independently for the HI and
+     * LO part of the volume. */
+
+    int32_t hi = cv >> 16;
+    int32_t lo = cv & 0xFFFF;
+    return ((v * lo) >> 16) + (v * hi);
+#endif
+}
+
 pa_usec_t pa_bytes_to_usec_round_up(uint64_t length, const pa_sample_spec *spec);
 size_t pa_usec_to_bytes_round_up(pa_usec_t t, const pa_sample_spec *spec);