diff options
Diffstat (limited to 'src/pcm_format.c')
-rw-r--r-- | src/pcm_format.c | 479 |
1 files changed, 336 insertions, 143 deletions
diff --git a/src/pcm_format.c b/src/pcm_format.c index 1e4b8d705..d3ea3acb0 100644 --- a/src/pcm_format.c +++ b/src/pcm_format.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2010 The Music Player Daemon Project + * Copyright (C) 2003-2011 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -22,39 +22,92 @@ #include "pcm_dither.h" #include "pcm_buffer.h" #include "pcm_pack.h" +#include "pcm_utils.h" static void -pcm_convert_8_to_16(int16_t *out, const int8_t *in, - unsigned num_samples) +pcm_convert_8_to_16(int16_t *out, const int8_t *in, const int8_t *in_end) { - while (num_samples > 0) { + while (in < in_end) { *out++ = *in++ << 8; - --num_samples; } } static void pcm_convert_24_to_16(struct pcm_dither *dither, - int16_t *out, const int32_t *in, - unsigned num_samples) + int16_t *out, const int32_t *in, const int32_t *in_end) { - pcm_dither_24_to_16(dither, out, in, num_samples); + pcm_dither_24_to_16(dither, out, in, in_end); } static void pcm_convert_32_to_16(struct pcm_dither *dither, - int16_t *out, const int32_t *in, - unsigned num_samples) + int16_t *out, const int32_t *in, const int32_t *in_end) { - pcm_dither_32_to_16(dither, out, in, num_samples); + pcm_dither_32_to_16(dither, out, in, in_end); } -static int32_t * -pcm_convert_24_to_24p32(struct pcm_buffer *buffer, const uint8_t *src, - unsigned num_samples) +static void +pcm_convert_float_to_16(int16_t *out, const float *in, const float *in_end) +{ + const unsigned OUT_BITS = 16; + const float factor = 1 << (OUT_BITS - 1); + + while (in < in_end) { + int sample = *in++ * factor; + *out++ = pcm_clamp_16(sample); + } +} + +static int16_t * +pcm_allocate_8_to_16(struct pcm_buffer *buffer, + const int8_t *src, size_t src_size, size_t *dest_size_r) +{ + int16_t *dest; + *dest_size_r = src_size / sizeof(*src) * sizeof(*dest); + dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_8_to_16(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + +static int16_t * +pcm_allocate_24p32_to_16(struct pcm_buffer *buffer, struct pcm_dither *dither, + const int32_t *src, size_t src_size, + size_t *dest_size_r) +{ + int16_t *dest; + *dest_size_r = src_size / 2; + assert(*dest_size_r == src_size / sizeof(*src) * sizeof(*dest)); + dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_24_to_16(dither, dest, src, + pcm_end_pointer(src, src_size)); + return dest; +} + +static int16_t * +pcm_allocate_32_to_16(struct pcm_buffer *buffer, struct pcm_dither *dither, + const int32_t *src, size_t src_size, + size_t *dest_size_r) +{ + int16_t *dest; + *dest_size_r = src_size / 2; + assert(*dest_size_r == src_size / sizeof(*src) * sizeof(*dest)); + dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_32_to_16(dither, dest, src, + pcm_end_pointer(src, src_size)); + return dest; +} + +static int16_t * +pcm_allocate_float_to_16(struct pcm_buffer *buffer, + const float *src, size_t src_size, + size_t *dest_size_r) { - int32_t *dest = pcm_buffer_get(buffer, num_samples * 4); - pcm_unpack_24(dest, src, num_samples, false); + int16_t *dest; + *dest_size_r = src_size / 2; + assert(*dest_size_r == src_size / sizeof(*src) * sizeof(*dest)); + dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_float_to_16(dest, src, + pcm_end_pointer(src, src_size)); return dest; } @@ -63,176 +116,221 @@ pcm_convert_to_16(struct pcm_buffer *buffer, struct pcm_dither *dither, enum sample_format src_format, const void *src, size_t src_size, size_t *dest_size_r) { - unsigned num_samples; - int16_t *dest; - int32_t *dest32; + assert(src_size % sample_format_size(src_format) == 0); switch (src_format) { case SAMPLE_FORMAT_UNDEFINED: + case SAMPLE_FORMAT_DSD: break; case SAMPLE_FORMAT_S8: - num_samples = src_size; - *dest_size_r = src_size * sizeof(*dest); - dest = pcm_buffer_get(buffer, *dest_size_r); - - pcm_convert_8_to_16(dest, - (const int8_t *)src, - num_samples); - return dest; + return pcm_allocate_8_to_16(buffer, + src, src_size, dest_size_r); case SAMPLE_FORMAT_S16: *dest_size_r = src_size; return src; - case SAMPLE_FORMAT_S24: - /* convert to S24_P32 first */ - num_samples = src_size / 3; - - dest32 = pcm_convert_24_to_24p32(buffer, src, num_samples); - dest = (int16_t *)dest32; - - /* convert to 16 bit in-place */ - *dest_size_r = num_samples * sizeof(*dest); - pcm_convert_24_to_16(dither, dest, dest32, - num_samples); - return dest; - case SAMPLE_FORMAT_S24_P32: - num_samples = src_size / 4; - *dest_size_r = num_samples * sizeof(*dest); - dest = pcm_buffer_get(buffer, *dest_size_r); - - pcm_convert_24_to_16(dither, dest, - (const int32_t *)src, - num_samples); - return dest; + return pcm_allocate_24p32_to_16(buffer, dither, src, src_size, + dest_size_r); case SAMPLE_FORMAT_S32: - num_samples = src_size / 4; - *dest_size_r = num_samples * sizeof(*dest); - dest = pcm_buffer_get(buffer, *dest_size_r); - - pcm_convert_32_to_16(dither, dest, - (const int32_t *)src, - num_samples); - return dest; + return pcm_allocate_32_to_16(buffer, dither, src, src_size, + dest_size_r); + + case SAMPLE_FORMAT_FLOAT: + return pcm_allocate_float_to_16(buffer, src, src_size, + dest_size_r); } return NULL; } static void -pcm_convert_8_to_24(int32_t *out, const int8_t *in, - unsigned num_samples) +pcm_convert_8_to_24(int32_t *out, const int8_t *in, const int8_t *in_end) { - while (num_samples > 0) { + while (in < in_end) *out++ = *in++ << 16; - --num_samples; - } } static void -pcm_convert_16_to_24(int32_t *out, const int16_t *in, - unsigned num_samples) +pcm_convert_16_to_24(int32_t *out, const int16_t *in, const int16_t *in_end) { - while (num_samples > 0) { + while (in < in_end) *out++ = *in++ << 8; - --num_samples; - } } static void -pcm_convert_32_to_24(int32_t *out, const int32_t *in, - unsigned num_samples) +pcm_convert_32_to_24(int32_t *restrict out, + const int32_t *restrict in, + const int32_t *restrict in_end) { - while (num_samples > 0) { + while (in < in_end) *out++ = *in++ >> 8; - --num_samples; +} + +static void +pcm_convert_float_to_24(int32_t *out, const float *in, const float *in_end) +{ + const unsigned OUT_BITS = 24; + const float factor = 1 << (OUT_BITS - 1); + + while (in < in_end) { + int sample = *in++ * factor; + *out++ = pcm_clamp_24(sample); } } +static int32_t * +pcm_allocate_8_to_24(struct pcm_buffer *buffer, + const int8_t *src, size_t src_size, size_t *dest_size_r) +{ + int32_t *dest; + *dest_size_r = src_size / sizeof(*src) * sizeof(*dest); + dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_8_to_24(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + +static int32_t * +pcm_allocate_16_to_24(struct pcm_buffer *buffer, + const int16_t *src, size_t src_size, size_t *dest_size_r) +{ + int32_t *dest; + *dest_size_r = src_size * 2; + assert(*dest_size_r == src_size / sizeof(*src) * sizeof(*dest)); + dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_16_to_24(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + +static int32_t * +pcm_allocate_32_to_24(struct pcm_buffer *buffer, + const int32_t *src, size_t src_size, size_t *dest_size_r) +{ + *dest_size_r = src_size; + int32_t *dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_32_to_24(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + +static int32_t * +pcm_allocate_float_to_24(struct pcm_buffer *buffer, + const float *src, size_t src_size, + size_t *dest_size_r) +{ + *dest_size_r = src_size; + int32_t *dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_float_to_24(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + const int32_t * pcm_convert_to_24(struct pcm_buffer *buffer, enum sample_format src_format, const void *src, size_t src_size, size_t *dest_size_r) { - unsigned num_samples; - int32_t *dest; + assert(src_size % sample_format_size(src_format) == 0); switch (src_format) { case SAMPLE_FORMAT_UNDEFINED: + case SAMPLE_FORMAT_DSD: break; case SAMPLE_FORMAT_S8: - num_samples = src_size; - *dest_size_r = src_size * sizeof(*dest); - dest = pcm_buffer_get(buffer, *dest_size_r); - - pcm_convert_8_to_24(dest, (const int8_t *)src, - num_samples); - return dest; + return pcm_allocate_8_to_24(buffer, + src, src_size, dest_size_r); case SAMPLE_FORMAT_S16: - num_samples = src_size / 2; - *dest_size_r = num_samples * sizeof(*dest); - dest = pcm_buffer_get(buffer, *dest_size_r); - - pcm_convert_16_to_24(dest, (const int16_t *)src, - num_samples); - return dest; - - case SAMPLE_FORMAT_S24: - num_samples = src_size / 3; - *dest_size_r = num_samples * sizeof(*dest); - - return pcm_convert_24_to_24p32(buffer, src, num_samples); + return pcm_allocate_16_to_24(buffer, + src, src_size, dest_size_r); case SAMPLE_FORMAT_S24_P32: *dest_size_r = src_size; return src; case SAMPLE_FORMAT_S32: - num_samples = src_size / 4; - *dest_size_r = num_samples * sizeof(*dest); - dest = pcm_buffer_get(buffer, *dest_size_r); + return pcm_allocate_32_to_24(buffer, src, src_size, + dest_size_r); - pcm_convert_32_to_24(dest, (const int32_t *)src, - num_samples); - return dest; + case SAMPLE_FORMAT_FLOAT: + return pcm_allocate_float_to_24(buffer, src, src_size, + dest_size_r); } return NULL; } static void -pcm_convert_8_to_32(int32_t *out, const int8_t *in, - unsigned num_samples) +pcm_convert_8_to_32(int32_t *out, const int8_t *in, const int8_t *in_end) { - while (num_samples > 0) { + while (in < in_end) *out++ = *in++ << 24; - --num_samples; - } } static void -pcm_convert_16_to_32(int32_t *out, const int16_t *in, - unsigned num_samples) +pcm_convert_16_to_32(int32_t *out, const int16_t *in, const int16_t *in_end) { - while (num_samples > 0) { + while (in < in_end) *out++ = *in++ << 16; - --num_samples; - } } static void -pcm_convert_24_to_32(int32_t *out, const int32_t *in, - unsigned num_samples) +pcm_convert_24_to_32(int32_t *restrict out, + const int32_t *restrict in, + const int32_t *restrict in_end) { - while (num_samples > 0) { + while (in < in_end) *out++ = *in++ << 8; - --num_samples; - } +} + +static int32_t * +pcm_allocate_8_to_32(struct pcm_buffer *buffer, + const int8_t *src, size_t src_size, size_t *dest_size_r) +{ + int32_t *dest; + *dest_size_r = src_size / sizeof(*src) * sizeof(*dest); + dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_8_to_32(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + +static int32_t * +pcm_allocate_16_to_32(struct pcm_buffer *buffer, + const int16_t *src, size_t src_size, size_t *dest_size_r) +{ + int32_t *dest; + *dest_size_r = src_size * 2; + assert(*dest_size_r == src_size / sizeof(*src) * sizeof(*dest)); + dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_16_to_32(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + +static int32_t * +pcm_allocate_24p32_to_32(struct pcm_buffer *buffer, + const int32_t *src, size_t src_size, + size_t *dest_size_r) +{ + *dest_size_r = src_size; + int32_t *dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_24_to_32(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + +static int32_t * +pcm_allocate_float_to_32(struct pcm_buffer *buffer, + const float *src, size_t src_size, + size_t *dest_size_r) +{ + /* convert to S24_P32 first */ + int32_t *dest = pcm_allocate_float_to_24(buffer, src, src_size, + dest_size_r); + + /* convert to 32 bit in-place */ + pcm_convert_24_to_32(dest, dest, pcm_end_pointer(dest, *dest_size_r)); + return dest; } const int32_t * @@ -240,52 +338,147 @@ pcm_convert_to_32(struct pcm_buffer *buffer, enum sample_format src_format, const void *src, size_t src_size, size_t *dest_size_r) { - unsigned num_samples; - int32_t *dest; + assert(src_size % sample_format_size(src_format) == 0); switch (src_format) { case SAMPLE_FORMAT_UNDEFINED: + case SAMPLE_FORMAT_DSD: break; case SAMPLE_FORMAT_S8: - num_samples = src_size; - *dest_size_r = src_size * sizeof(*dest); - dest = pcm_buffer_get(buffer, *dest_size_r); - - pcm_convert_8_to_32(dest, (const int8_t *)src, - num_samples); - return dest; + return pcm_allocate_8_to_32(buffer, src, src_size, + dest_size_r); case SAMPLE_FORMAT_S16: - num_samples = src_size / 2; - *dest_size_r = num_samples * sizeof(*dest); - dest = pcm_buffer_get(buffer, *dest_size_r); + return pcm_allocate_16_to_32(buffer, src, src_size, + dest_size_r); - pcm_convert_16_to_32(dest, (const int16_t *)src, - num_samples); - return dest; + case SAMPLE_FORMAT_S24_P32: + return pcm_allocate_24p32_to_32(buffer, src, src_size, + dest_size_r); - case SAMPLE_FORMAT_S24: - /* convert to S24_P32 first */ - num_samples = src_size / 3; + case SAMPLE_FORMAT_S32: + *dest_size_r = src_size; + return src; - dest = pcm_convert_24_to_24p32(buffer, src, num_samples); + case SAMPLE_FORMAT_FLOAT: + return pcm_allocate_float_to_32(buffer, src, src_size, + dest_size_r); + } - /* convert to 32 bit in-place */ - *dest_size_r = num_samples * sizeof(*dest); - pcm_convert_24_to_32(dest, dest, num_samples); - return dest; + return NULL; +} - case SAMPLE_FORMAT_S24_P32: - num_samples = src_size / 4; - *dest_size_r = num_samples * sizeof(*dest); - dest = pcm_buffer_get(buffer, *dest_size_r); +static void +pcm_convert_8_to_float(float *out, const int8_t *in, const int8_t *in_end) +{ + enum { in_bits = sizeof(*in) * 8 }; + static const float factor = 2.0f / (1 << in_bits); + while (in < in_end) + *out++ = (float)*in++ * factor; +} - pcm_convert_24_to_32(dest, (const int32_t *)src, - num_samples); - return dest; +static void +pcm_convert_16_to_float(float *out, const int16_t *in, const int16_t *in_end) +{ + enum { in_bits = sizeof(*in) * 8 }; + static const float factor = 2.0f / (1 << in_bits); + while (in < in_end) + *out++ = (float)*in++ * factor; +} + +static void +pcm_convert_24_to_float(float *out, const int32_t *in, const int32_t *in_end) +{ + enum { in_bits = 24 }; + static const float factor = 2.0f / (1 << in_bits); + while (in < in_end) + *out++ = (float)*in++ * factor; +} + +static void +pcm_convert_32_to_float(float *out, const int32_t *in, const int32_t *in_end) +{ + enum { in_bits = sizeof(*in) * 8 }; + static const float factor = 0.5f / (1 << (in_bits - 2)); + while (in < in_end) + *out++ = (float)*in++ * factor; +} + +static float * +pcm_allocate_8_to_float(struct pcm_buffer *buffer, + const int8_t *src, size_t src_size, + size_t *dest_size_r) +{ + float *dest; + *dest_size_r = src_size / sizeof(*src) * sizeof(*dest); + dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_8_to_float(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + +static float * +pcm_allocate_16_to_float(struct pcm_buffer *buffer, + const int16_t *src, size_t src_size, + size_t *dest_size_r) +{ + float *dest; + *dest_size_r = src_size * 2; + assert(*dest_size_r == src_size / sizeof(*src) * sizeof(*dest)); + dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_16_to_float(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + +static float * +pcm_allocate_24p32_to_float(struct pcm_buffer *buffer, + const int32_t *src, size_t src_size, + size_t *dest_size_r) +{ + *dest_size_r = src_size; + float *dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_24_to_float(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + +static float * +pcm_allocate_32_to_float(struct pcm_buffer *buffer, + const int32_t *src, size_t src_size, + size_t *dest_size_r) +{ + *dest_size_r = src_size; + float *dest = pcm_buffer_get(buffer, *dest_size_r); + pcm_convert_32_to_float(dest, src, pcm_end_pointer(src, src_size)); + return dest; +} + +const float * +pcm_convert_to_float(struct pcm_buffer *buffer, + enum sample_format src_format, const void *src, + size_t src_size, size_t *dest_size_r) +{ + switch (src_format) { + case SAMPLE_FORMAT_UNDEFINED: + case SAMPLE_FORMAT_DSD: + break; + + case SAMPLE_FORMAT_S8: + return pcm_allocate_8_to_float(buffer, + src, src_size, dest_size_r); + + case SAMPLE_FORMAT_S16: + return pcm_allocate_16_to_float(buffer, + src, src_size, dest_size_r); + + case SAMPLE_FORMAT_S24_P32: + return pcm_allocate_24p32_to_float(buffer, + src, src_size, dest_size_r); case SAMPLE_FORMAT_S32: + return pcm_allocate_32_to_float(buffer, + src, src_size, dest_size_r); + + case SAMPLE_FORMAT_FLOAT: *dest_size_r = src_size; return src; } |