diff options
Diffstat (limited to 'src/pulsecore/sample-util.c')
-rw-r--r-- | src/pulsecore/sample-util.c | 329 |
1 files changed, 306 insertions, 23 deletions
diff --git a/src/pulsecore/sample-util.c b/src/pulsecore/sample-util.c index 3be08efd..905ba5df 100644 --- a/src/pulsecore/sample-util.c +++ b/src/pulsecore/sample-util.c @@ -83,6 +83,10 @@ static uint8_t silence_byte(pa_sample_format_t format) { case PA_SAMPLE_S32BE: case PA_SAMPLE_FLOAT32LE: case PA_SAMPLE_FLOAT32BE: + case PA_SAMPLE_S24LE: + case PA_SAMPLE_S24BE: + case PA_SAMPLE_S24_32LE: + case PA_SAMPLE_S24_32BE: return 0; case PA_SAMPLE_ALAW: return 0xd5; @@ -209,13 +213,22 @@ size_t pa_mix( for (i = 0; i < nstreams; i++) { pa_mix_info *m = streams + i; - int32_t v, cv = m->linear[channel].i; + int32_t v, lo, hi, cv = m->linear[channel].i; if (PA_UNLIKELY(cv <= 0)) continue; + /* Multiplying the 32bit volume factor with the + * 16bit sample might result in an 48bit value. We + * want to do without 64 bit integers and hence do + * the multiplication independantly for the HI and + * LO part of the volume. */ + + hi = cv >> 16; + lo = cv & 0xFFFF; + v = *((int16_t*) m->ptr); - v = (v * cv) / 0x10000; + v = ((v * lo) >> 16) + (v * hi); sum += v; m->ptr = (uint8_t*) m->ptr + sizeof(int16_t); @@ -244,13 +257,16 @@ size_t pa_mix( for (i = 0; i < nstreams; i++) { pa_mix_info *m = streams + i; - int32_t v, cv = m->linear[channel].i; + int32_t v, lo, hi, cv = m->linear[channel].i; if (PA_UNLIKELY(cv <= 0)) continue; + hi = cv >> 16; + lo = cv & 0xFFFF; + v = PA_INT16_SWAP(*((int16_t*) m->ptr)); - v = (v * cv) / 0x10000; + v = ((v * lo) >> 16) + (v * hi); sum += v; m->ptr = (uint8_t*) m->ptr + sizeof(int16_t); @@ -286,7 +302,7 @@ size_t pa_mix( continue; v = *((int32_t*) m->ptr); - v = (v * cv) / 0x10000; + v = (v * cv) >> 16; sum += v; m->ptr = (uint8_t*) m->ptr + sizeof(int32_t); @@ -322,7 +338,7 @@ size_t pa_mix( continue; v = PA_INT32_SWAP(*((int32_t*) m->ptr)); - v = (v * cv) / 0x10000; + v = (v * cv) >> 16; sum += v; m->ptr = (uint8_t*) m->ptr + sizeof(int32_t); @@ -340,6 +356,150 @@ size_t pa_mix( break; } + case PA_SAMPLE_S24NE: { + unsigned channel = 0; + + calc_linear_integer_stream_volumes(streams, nstreams, volume, spec); + + while (data < end) { + int64_t sum = 0; + unsigned i; + + for (i = 0; i < nstreams; i++) { + pa_mix_info *m = streams + i; + int32_t cv = m->linear[channel].i; + int64_t v; + + if (PA_UNLIKELY(cv <= 0)) + continue; + + v = (int32_t) (PA_READ24NE(m->ptr) << 8); + v = (v * cv) >> 16; + sum += v; + + m->ptr = (uint8_t*) m->ptr + 3; + } + + sum = PA_CLAMP_UNLIKELY(sum, -0x80000000LL, 0x7FFFFFFFLL); + PA_WRITE24NE(data, ((uint32_t) sum) >> 8); + + data = (uint8_t*) data + 3; + + if (PA_UNLIKELY(++channel >= spec->channels)) + channel = 0; + } + + break; + } + + case PA_SAMPLE_S24RE: { + unsigned channel = 0; + + calc_linear_integer_stream_volumes(streams, nstreams, volume, spec); + + while (data < end) { + int64_t sum = 0; + unsigned i; + + for (i = 0; i < nstreams; i++) { + pa_mix_info *m = streams + i; + int32_t cv = m->linear[channel].i; + int64_t v; + + if (PA_UNLIKELY(cv <= 0)) + continue; + + v = (int32_t) (PA_READ24RE(m->ptr) << 8); + v = (v * cv) >> 16; + sum += v; + + m->ptr = (uint8_t*) m->ptr + 3; + } + + sum = PA_CLAMP_UNLIKELY(sum, -0x80000000LL, 0x7FFFFFFFLL); + PA_WRITE24RE(data, ((uint32_t) sum) >> 8); + + data = (uint8_t*) data + 3; + + if (PA_UNLIKELY(++channel >= spec->channels)) + channel = 0; + } + + break; + } + + case PA_SAMPLE_S24_32NE: { + unsigned channel = 0; + + calc_linear_integer_stream_volumes(streams, nstreams, volume, spec); + + while (data < end) { + int64_t sum = 0; + unsigned i; + + for (i = 0; i < nstreams; i++) { + pa_mix_info *m = streams + i; + int32_t cv = m->linear[channel].i; + int64_t v; + + if (PA_UNLIKELY(cv <= 0)) + continue; + + v = (int32_t) (*((uint32_t*)m->ptr) << 8); + v = (v * cv) >> 16; + sum += v; + + m->ptr = (uint8_t*) m->ptr + sizeof(int32_t); + } + + sum = PA_CLAMP_UNLIKELY(sum, -0x80000000LL, 0x7FFFFFFFLL); + *((uint32_t*) data) = ((uint32_t) (int32_t) sum) >> 8; + + data = (uint8_t*) data + sizeof(uint32_t); + + if (PA_UNLIKELY(++channel >= spec->channels)) + channel = 0; + } + + break; + } + + case PA_SAMPLE_S24_32RE: { + unsigned channel = 0; + + calc_linear_integer_stream_volumes(streams, nstreams, volume, spec); + + while (data < end) { + int64_t sum = 0; + unsigned i; + + for (i = 0; i < nstreams; i++) { + pa_mix_info *m = streams + i; + int32_t cv = m->linear[channel].i; + int64_t v; + + if (PA_UNLIKELY(cv <= 0)) + continue; + + v = (int32_t) (PA_UINT32_SWAP(*((uint32_t*) m->ptr)) << 8); + v = (v * cv) >> 16; + sum += v; + + m->ptr = (uint8_t*) m->ptr + 3; + } + + sum = PA_CLAMP_UNLIKELY(sum, -0x80000000LL, 0x7FFFFFFFLL); + *((uint32_t*) data) = PA_INT32_SWAP(((uint32_t) (int32_t) sum) >> 8); + + data = (uint8_t*) data + sizeof(uint32_t); + + if (PA_UNLIKELY(++channel >= spec->channels)) + channel = 0; + } + + break; + } + case PA_SAMPLE_U8: { unsigned channel = 0; @@ -357,7 +517,7 @@ size_t pa_mix( continue; v = (int32_t) *((uint8_t*) m->ptr) - 0x80; - v = (v * cv) / 0x10000; + v = (v * cv) >> 16; sum += v; m->ptr = (uint8_t*) m->ptr + 1; @@ -386,13 +546,16 @@ size_t pa_mix( for (i = 0; i < nstreams; i++) { pa_mix_info *m = streams + i; - int32_t v, cv = m->linear[channel].i; + int32_t v, hi, lo, cv = m->linear[channel].i; if (PA_UNLIKELY(cv <= 0)) continue; + hi = cv >> 16; + lo = cv & 0xFFFF; + v = (int32_t) st_ulaw2linear16(*((uint8_t*) m->ptr)); - v = (v * cv) / 0x10000; + v = ((v * lo) >> 16) + (v * hi); sum += v; m->ptr = (uint8_t*) m->ptr + 1; @@ -421,13 +584,16 @@ size_t pa_mix( for (i = 0; i < nstreams; i++) { pa_mix_info *m = streams + i; - int32_t v, cv = m->linear[channel].i; + int32_t v, hi, lo, cv = m->linear[channel].i; if (PA_UNLIKELY(cv <= 0)) continue; + hi = cv >> 16; + lo = cv & 0xFFFF; + v = (int32_t) st_alaw2linear16(*((uint8_t*) m->ptr)); - v = (v * cv) / 0x10000; + v = ((v * lo) >> 16) + (v * hi); sum += v; m->ptr = (uint8_t*) m->ptr + 1; @@ -562,16 +728,26 @@ void pa_volume_memchunk( e = (int16_t*) ptr + c->length/sizeof(int16_t); for (channel = 0, d = ptr; d < e; d++) { - int32_t t; + int32_t t, hi, lo; + + /* Multiplying the 32bit volume factor with the 16bit + * sample might result in an 48bit value. We want to + * do without 64 bit integers and hence do the + * multiplication independantly for the HI and LO part + * of the volume. */ + + hi = linear[channel] >> 16; + lo = linear[channel] & 0xFFFF; t = (int32_t)(*d); - t = (t * linear[channel]) / 0x10000; + t = ((t * lo) >> 16) + (t * hi); t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF); *d = (int16_t) t; if (PA_UNLIKELY(++channel >= spec->channels)) channel = 0; } + break; } @@ -585,10 +761,13 @@ void pa_volume_memchunk( e = (int16_t*) ptr + c->length/sizeof(int16_t); for (channel = 0, d = ptr; d < e; d++) { - int32_t t; + int32_t t, hi, lo; + + hi = linear[channel] >> 16; + lo = linear[channel] & 0xFFFF; t = (int32_t) PA_INT16_SWAP(*d); - t = (t * linear[channel]) / 0x10000; + t = ((t * lo) >> 16) + (t * hi); t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF); *d = PA_INT16_SWAP((int16_t) t); @@ -612,7 +791,7 @@ void pa_volume_memchunk( int64_t t; t = (int64_t)(*d); - t = (t * linear[channel]) / 0x10000; + t = (t * linear[channel]) >> 16; t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL); *d = (int32_t) t; @@ -635,14 +814,105 @@ void pa_volume_memchunk( int64_t t; t = (int64_t) PA_INT32_SWAP(*d); - t = (t * linear[channel]) / 0x10000; + t = (t * linear[channel]) >> 16; t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL); *d = PA_INT32_SWAP((int32_t) t); if (PA_UNLIKELY(++channel >= spec->channels)) channel = 0; } + break; + } + + case PA_SAMPLE_S24NE: { + uint8_t *d, *e; + unsigned channel; + int32_t linear[PA_CHANNELS_MAX]; + + calc_linear_integer_volume(linear, volume); + e = (uint8_t*) ptr + c->length/3; + + for (channel = 0, d = ptr; d < e; d++) { + int64_t t; + + t = (int64_t)((int32_t) (PA_READ24NE(d) << 8)); + t = (t * linear[channel]) >> 16; + t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL); + PA_WRITE24NE(d, ((uint32_t) (int32_t) t) >> 8); + + if (PA_UNLIKELY(++channel >= spec->channels)) + channel = 0; + } + break; + } + + case PA_SAMPLE_S24RE: { + uint8_t *d, *e; + unsigned channel; + int32_t linear[PA_CHANNELS_MAX]; + + calc_linear_integer_volume(linear, volume); + + e = (uint8_t*) ptr + c->length/3; + + for (channel = 0, d = ptr; d < e; d++) { + int64_t t; + + t = (int64_t)((int32_t) (PA_READ24RE(d) << 8)); + t = (t * linear[channel]) >> 16; + t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL); + PA_WRITE24RE(d, ((uint32_t) (int32_t) t) >> 8); + + if (PA_UNLIKELY(++channel >= spec->channels)) + channel = 0; + } + break; + } + + case PA_SAMPLE_S24_32NE: { + uint32_t *d, *e; + unsigned channel; + int32_t linear[PA_CHANNELS_MAX]; + + calc_linear_integer_volume(linear, volume); + + e = (uint32_t*) ptr + c->length/sizeof(uint32_t); + + for (channel = 0, d = ptr; d < e; d++) { + int64_t t; + + t = (int64_t) ((int32_t) (*d << 8)); + t = (t * linear[channel]) >> 16; + t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL); + *d = ((uint32_t) ((int32_t) t)) >> 8; + + if (PA_UNLIKELY(++channel >= spec->channels)) + channel = 0; + } + break; + } + + case PA_SAMPLE_S24_32RE: { + uint32_t *d, *e; + unsigned channel; + int32_t linear[PA_CHANNELS_MAX]; + + calc_linear_integer_volume(linear, volume); + + e = (uint32_t*) ptr + c->length/sizeof(uint32_t); + + for (channel = 0, d = ptr; d < e; d++) { + int64_t t; + + t = (int64_t) ((int32_t) (PA_UINT32_SWAP(*d) << 8)); + t = (t * linear[channel]) >> 16; + t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL); + *d = PA_UINT32_SWAP(((uint32_t) ((int32_t) t)) >> 8); + + if (PA_UNLIKELY(++channel >= spec->channels)) + channel = 0; + } break; } @@ -656,10 +926,13 @@ void pa_volume_memchunk( e = (uint8_t*) ptr + c->length; for (channel = 0, d = ptr; d < e; d++) { - int32_t t; + int32_t t, hi, lo; + + hi = linear[channel] >> 16; + lo = linear[channel] & 0xFFFF; t = (int32_t) *d - 0x80; - t = (t * linear[channel]) / 0x10000; + t = ((t * lo) >> 16) + (t * hi); t = PA_CLAMP_UNLIKELY(t, -0x80, 0x7F); *d = (uint8_t) (t + 0x80); @@ -679,10 +952,13 @@ void pa_volume_memchunk( e = (uint8_t*) ptr + c->length; for (channel = 0, d = ptr; d < e; d++) { - int32_t t; + int32_t t, hi, lo; + + hi = linear[channel] >> 16; + lo = linear[channel] & 0xFFFF; t = (int32_t) st_ulaw2linear16(*d); - t = (t * linear[channel]) / 0x10000; + t = ((t * lo) >> 16) + (t * hi); t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF); *d = (uint8_t) st_14linear2ulaw((int16_t) t >> 2); @@ -702,10 +978,13 @@ void pa_volume_memchunk( e = (uint8_t*) ptr + c->length; for (channel = 0, d = ptr; d < e; d++) { - int32_t t; + int32_t t, hi, lo; + + hi = linear[channel] >> 16; + lo = linear[channel] & 0xFFFF; t = (int32_t) st_alaw2linear16(*d); - t = (t * linear[channel]) / 0x10000; + t = ((t * lo) >> 16) + (t * hi); t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF); *d = (uint8_t) st_13linear2alaw((int16_t) t >> 3); @@ -900,12 +1179,16 @@ pa_memchunk* pa_silence_memchunk_get(pa_silence_cache *cache, pa_mempool *pool, case PA_SAMPLE_S16BE: case PA_SAMPLE_S32LE: case PA_SAMPLE_S32BE: + case PA_SAMPLE_S24LE: + case PA_SAMPLE_S24BE: case PA_SAMPLE_FLOAT32LE: case PA_SAMPLE_FLOAT32BE: cache->blocks[PA_SAMPLE_S16LE] = b = silence_memblock_new(pool, 0); cache->blocks[PA_SAMPLE_S16BE] = pa_memblock_ref(b); cache->blocks[PA_SAMPLE_S32LE] = pa_memblock_ref(b); cache->blocks[PA_SAMPLE_S32BE] = pa_memblock_ref(b); + cache->blocks[PA_SAMPLE_S24LE] = pa_memblock_ref(b); + cache->blocks[PA_SAMPLE_S24BE] = pa_memblock_ref(b); cache->blocks[PA_SAMPLE_FLOAT32LE] = pa_memblock_ref(b); cache->blocks[PA_SAMPLE_FLOAT32BE] = pa_memblock_ref(b); break; |