aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/command.c9
-rw-r--r--src/inputPlugins/_flac_common.c127
-rw-r--r--src/inputPlugins/_flac_common.h13
-rw-r--r--src/inputPlugins/flac_plugin.c74
-rw-r--r--src/inputPlugins/oggflac_plugin.c45
-rw-r--r--src/pcm_utils.c46
6 files changed, 165 insertions, 149 deletions
diff --git a/src/command.c b/src/command.c
index 70bfa78b5..3f80dc916 100644
--- a/src/command.c
+++ b/src/command.c
@@ -1409,16 +1409,19 @@ static CommandEntry *getCommandEntryAndCheckArgcAndPermission(int fd,
static CommandEntry *getCommandEntryFromString(char *string, int *permission)
{
- CommandEntry *cmd;
+ CommandEntry *cmd = NULL;
char *argv[COMMAND_ARGV_MAX] = { NULL };
- int argc = buffer2array(string, argv, COMMAND_ARGV_MAX);
+ char *duplicated = xstrdup(string);
+ int argc = buffer2array(duplicated, argv, COMMAND_ARGV_MAX);
if (0 == argc)
- return NULL;
+ goto out;
cmd = getCommandEntryAndCheckArgcAndPermission(0, permission,
argc, argv);
+out:
+ free(duplicated);
return cmd;
}
diff --git a/src/inputPlugins/_flac_common.c b/src/inputPlugins/_flac_common.c
index 9950f75db..beff5d431 100644
--- a/src/inputPlugins/_flac_common.c
+++ b/src/inputPlugins/_flac_common.c
@@ -32,7 +32,6 @@
void init_FlacData(FlacData * data, InputStream * inStream)
{
- data->chunk_length = 0;
data->time = 0;
data->position = 0;
data->bitRate = 0;
@@ -194,4 +193,130 @@ void flac_error_common_cb(const char *plugin,
}
}
+static void flac_convert_stereo16(int16_t *dest,
+ const FLAC__int32 * const buf[],
+ unsigned int position, unsigned int end)
+{
+ for (; position < end; ++position) {
+ *dest++ = buf[0][position];
+ *dest++ = buf[1][position];
+ }
+}
+
+static void
+flac_convert_16(int16_t *dest,
+ unsigned int num_channels,
+ const FLAC__int32 * const buf[],
+ unsigned int position, unsigned int end)
+{
+ unsigned int c_chan;
+
+ for (; position < end; ++position)
+ for (c_chan = 0; c_chan < num_channels; c_chan++)
+ *dest++ = buf[c_chan][position];
+}
+
+/**
+ * Note: this function also handles 24 bit files!
+ */
+static void
+flac_convert_32(int32_t *dest,
+ unsigned int num_channels,
+ const FLAC__int32 * const buf[],
+ unsigned int position, unsigned int end)
+{
+ unsigned int c_chan;
+
+ for (; position < end; ++position)
+ for (c_chan = 0; c_chan < num_channels; c_chan++)
+ *dest++ = buf[c_chan][position];
+}
+
+static void
+flac_convert_8(int8_t *dest,
+ unsigned int num_channels,
+ const FLAC__int32 * const buf[],
+ unsigned int position, unsigned int end)
+{
+ unsigned int c_chan;
+
+ for (; position < end; ++position)
+ for (c_chan = 0; c_chan < num_channels; c_chan++)
+ *dest++ = buf[c_chan][position];
+}
+
+static void flac_convert(unsigned char *dest,
+ unsigned int num_channels,
+ unsigned int bytes_per_sample,
+ const FLAC__int32 * const buf[],
+ unsigned int position, unsigned int end)
+{
+ switch (bytes_per_sample) {
+ case 2:
+ if (num_channels == 2)
+ flac_convert_stereo16((int16_t*)dest, buf,
+ position, end);
+ else
+ flac_convert_16((int16_t*)dest, num_channels, buf,
+ position, end);
+ break;
+
+ case 4:
+ flac_convert_32((int32_t*)dest, num_channels, buf,
+ position, end);
+ break;
+
+ case 1:
+ flac_convert_8((int8_t*)dest, num_channels, buf,
+ position, end);
+ break;
+ }
+}
+
+FLAC__StreamDecoderWriteStatus
+flac_common_write(FlacData *data, const FLAC__Frame * frame,
+ const FLAC__int32 *const buf[])
+{
+ unsigned int c_samp;
+ const unsigned int num_channels = frame->header.channels;
+ const unsigned int bytes_per_sample = (dc.audio_format.bits / 8);
+ const unsigned int bytes_per_channel =
+ bytes_per_sample * frame->header.channels;
+ const unsigned int max_samples = FLAC_CHUNK_SIZE / bytes_per_channel;
+ unsigned int num_samples;
+ enum dc_action action;
+
+ assert(dc.audio_format.bits > 0);
+
+ if (bytes_per_sample != 1 && bytes_per_sample != 2 &&
+ bytes_per_sample != 4)
+ /* exotic unsupported bit rate */
+ return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+
+ for (c_samp = 0; c_samp < frame->header.blocksize;
+ c_samp += num_samples) {
+ num_samples = frame->header.blocksize - c_samp;
+ if (num_samples > max_samples)
+ num_samples = max_samples;
+
+ flac_convert(data->chunk,
+ num_channels, bytes_per_sample, buf,
+ c_samp, c_samp + num_samples);
+
+ action = ob_send(data->chunk,
+ num_samples * bytes_per_channel,
+ data->time, data->bitRate,
+ data->replayGainInfo);
+ switch (action) {
+ case DC_ACTION_STOP:
+ return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+ case DC_ACTION_SEEK:
+ return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
+ default: break; /* compilers are complainers */
+ }
+ }
+
+ return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
+}
+
#endif /* HAVE_FLAC || HAVE_OGGFLAC */
diff --git a/src/inputPlugins/_flac_common.h b/src/inputPlugins/_flac_common.h
index a1b0cac8f..f88866b08 100644
--- a/src/inputPlugins/_flac_common.h
+++ b/src/inputPlugins/_flac_common.h
@@ -140,7 +140,6 @@ typedef size_t flac_read_status_size_t;
typedef struct {
unsigned char chunk[FLAC_CHUNK_SIZE];
- size_t chunk_length;
float time;
unsigned int bitRate;
FLAC__uint64 position;
@@ -160,15 +159,9 @@ void flac_error_common_cb(const char *plugin,
struct mpd_tag *copyVorbisCommentBlockToMpdTag(const FLAC__StreamMetadata * block,
struct mpd_tag *tag);
-/* keep this inlined, this is just macro but prettier :) */
-static inline enum dc_action flacSendChunk(FlacData * data)
-{
- enum dc_action ret = ob_send(data->chunk, data->chunk_length,
- data->time, data->bitRate,
- data->replayGainInfo);
- data->chunk_length = 0;
- return ret;
-}
+FLAC__StreamDecoderWriteStatus
+flac_common_write(FlacData *data, const FLAC__Frame * frame,
+ const FLAC__int32 *const buf[]);
#endif /* HAVE_FLAC || HAVE_OGGFLAC */
diff --git a/src/inputPlugins/flac_plugin.c b/src/inputPlugins/flac_plugin.c
index 89e988a03..228301291 100644
--- a/src/inputPlugins/flac_plugin.c
+++ b/src/inputPlugins/flac_plugin.c
@@ -199,39 +199,6 @@ static void flacMetadata(mpd_unused const flac_decoder * dec,
flac_metadata_common_cb(block, (FlacData *) vdata);
}
-static void flac_convert_stereo16(unsigned char *dest,
- const FLAC__int32 * const buf[],
- unsigned int position, unsigned int end)
-{
- for (; position < end; ++position) {
- *(uint16_t*)dest = buf[0][position];
- dest += 2;
- *(uint16_t*)dest = buf[1][position];
- dest += 2;
- }
-}
-
-static void flac_convert(unsigned char *dest,
- unsigned int num_channels,
- unsigned int bytes_per_sample,
- const FLAC__int32 * const buf[],
- unsigned int position, unsigned int end)
-{
- unsigned int c_chan, i;
- FLAC__uint16 u16;
- unsigned char *uc;
-
- for (; position < end; ++position) {
- for (c_chan = 0; c_chan < num_channels; c_chan++) {
- u16 = buf[c_chan][position];
- uc = (unsigned char *)&u16;
- for (i = 0; i < bytes_per_sample; i++) {
- *dest++ = *uc++;
- }
- }
- }
-}
-
static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec,
const FLAC__Frame * frame,
const FLAC__int32 * const buf[],
@@ -239,18 +206,9 @@ static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec,
{
FlacData *data = (FlacData *) vdata;
FLAC__uint32 samples = frame->header.blocksize;
- unsigned int c_samp;
- const unsigned int num_channels = frame->header.channels;
- const unsigned int bytes_per_sample = (dc.audio_format.bits / 8);
- const unsigned int bytes_per_channel =
- bytes_per_sample * frame->header.channels;
- const unsigned int max_samples = FLAC_CHUNK_SIZE / bytes_per_channel;
- unsigned int num_samples;
float timeChange;
FLAC__uint64 newPosition = 0;
- assert(dc.audio_format.bits > 0);
-
timeChange = ((float)samples) / frame->header.sample_rate;
data->time += timeChange;
@@ -264,32 +222,7 @@ static FLAC__StreamDecoderWriteStatus flacWrite(const flac_decoder *dec,
}
data->position = newPosition;
- for (c_samp = 0; c_samp < frame->header.blocksize;
- c_samp += num_samples) {
- num_samples = frame->header.blocksize - c_samp;
- if (num_samples > max_samples)
- num_samples = max_samples;
-
- if (num_channels == 2 && bytes_per_sample == 2)
- flac_convert_stereo16(data->chunk + data->chunk_length,
- buf, c_samp,
- c_samp + num_samples);
- else
- flac_convert(data->chunk + data->chunk_length,
- num_channels, bytes_per_sample, buf,
- c_samp, c_samp + num_samples);
- data->chunk_length = num_samples * bytes_per_channel;
-
- switch (flacSendChunk(data)) {
- case DC_ACTION_STOP:
- return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
- case DC_ACTION_SEEK:
- return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
- default: break; /* compilers are complainers */
- }
- }
-
- return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
+ return flac_common_write(data, frame, buf);
}
static struct mpd_tag *flacMetadataDup(char *file, int *vorbisCommentFound)
@@ -426,7 +359,6 @@ static int flac_decode_internal(InputStream * inStream, int is_ogg)
data.time = ((float)sampleToSeek) /
dc.audio_format.sampleRate;
data.position = 0;
- data.chunk_length = 0;
} else {
dc.seek_where = DC_SEEK_ERROR;
}
@@ -437,10 +369,6 @@ static int flac_decode_internal(InputStream * inStream, int is_ogg)
flacPrintErroredState(flac_get_state(flacDec));
flac_finish(flacDec);
}
- /* send last little bit */
- if (data.chunk_length > 0 && !dc_intr())
- flacSendChunk(&data);
-
fail:
if (data.replayGainInfo)
freeReplayGainInfo(data.replayGainInfo);
diff --git a/src/inputPlugins/oggflac_plugin.c b/src/inputPlugins/oggflac_plugin.c
index 841030481..36bd9dced 100644
--- a/src/inputPlugins/oggflac_plugin.c
+++ b/src/inputPlugins/oggflac_plugin.c
@@ -162,51 +162,12 @@ static FLAC__StreamDecoderWriteStatus oggflacWrite(mpd_unused const
{
FlacData *data = (FlacData *) vdata;
FLAC__uint32 samples = frame->header.blocksize;
- FLAC__uint16 u16;
- unsigned char *uc;
- unsigned int c_samp, c_chan;
- int i;
float timeChange;
timeChange = ((float)samples) / frame->header.sample_rate;
data->time += timeChange;
- /* ogg123 uses a complicated method of calculating bitrate
- * with averaging which I'm not too fond of.
- * (waste of memory/CPU cycles, especially given this is _lossless_)
- * a get_decode_position() is not available in OggFLAC, either
- *
- * this does not give an accurate bitrate:
- * (bytes_last_read was set in the read callback)
- data->bitRate = ((8.0 * data->bytes_last_read *
- frame->header.sample_rate)
- /((float)samples * 1000)) + 0.5;
- */
-
- for (c_samp = 0; c_samp < frame->header.blocksize; c_samp++) {
- for (c_chan = 0; c_chan < frame->header.channels;
- c_chan++) {
- u16 = buf[c_chan][c_samp];
- uc = (unsigned char *)&u16;
- for (i = 0; i < (dc.audio_format.bits / 8); i++) {
- if (data->chunk_length >= FLAC_CHUNK_SIZE) {
- /* FIXME: line wrapping */
- switch (flacSendChunk(data)) {
- case DC_ACTION_STOP:
- return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
- case DC_ACTION_SEEK:
- return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
- default:
- /* compilers are complainers */
- break;
- }
- }
- data->chunk[data->chunk_length++] = *(uc++);
- }
- }
- }
-
- return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
+ return flac_common_write(data, frame, buf);
}
/* used by TagDup */
@@ -367,7 +328,6 @@ static int oggflac_decode(InputStream * inStream)
data.time = ((float)sampleToSeek) /
dc.audio_format.sampleRate;
data.position = 0;
- data.chunk_length = 0;
} else {
dc.seek_where = DC_SEEK_ERROR;
}
@@ -380,9 +340,6 @@ static int oggflac_decode(InputStream * inStream)
(OggFLAC__seekable_stream_decoder_get_state(decoder));
OggFLAC__seekable_stream_decoder_finish(decoder);
}
- /* send last little bit */
- if (data.chunk_length > 0 && !dc_intr())
- flacSendChunk(&data);
fail:
oggflac_cleanup(&data, decoder);
diff --git a/src/pcm_utils.c b/src/pcm_utils.c
index 90856fa1d..077010062 100644
--- a/src/pcm_utils.c
+++ b/src/pcm_utils.c
@@ -24,6 +24,26 @@
#include "conf.h"
#include "os_compat.h"
+static inline int
+pcm_dither(void)
+{
+ return (rand() & 511) - (rand() & 511);
+}
+
+/**
+ * Check if the value is within the range of the provided bit size,
+ * and caps it if necessary.
+ */
+static mpd_sint32
+pcm_range(mpd_sint32 sample, unsigned bits)
+{
+ if (mpd_unlikely(sample < (-1 << (bits - 1))))
+ return -1 << (bits - 1);
+ if (mpd_unlikely(sample >= (1 << (bits - 1))))
+ return (1 << (bits - 1)) - 1;
+ return sample;
+}
+
void pcm_volumeChange(char *buffer, int bufferSize, const AudioFormat * format,
int volume)
{
@@ -44,12 +64,10 @@ void pcm_volumeChange(char *buffer, int bufferSize, const AudioFormat * format,
while (bufferSize > 0) {
temp32 = *buffer16;
temp32 *= volume;
- temp32 += rand() & 511;
- temp32 -= rand() & 511;
+ temp32 += pcm_dither();
temp32 += 500;
temp32 /= 1000;
- *buffer16 = temp32 > 32767 ? 32767 :
- (temp32 < -32768 ? -32768 : temp32);
+ *buffer16 = pcm_range(temp32, 16);
buffer16++;
bufferSize -= 2;
}
@@ -58,12 +76,10 @@ void pcm_volumeChange(char *buffer, int bufferSize, const AudioFormat * format,
while (bufferSize > 0) {
temp32 = *buffer8;
temp32 *= volume;
- temp32 += rand() & 511;
- temp32 -= rand() & 511;
+ temp32 += pcm_dither();
temp32 += 500;
temp32 /= 1000;
- *buffer8 = temp32 > 127 ? 127 :
- (temp32 < -128 ? -128 : temp32);
+ *buffer8 = pcm_range(temp32, 8);
buffer8++;
bufferSize--;
}
@@ -90,13 +106,10 @@ static void pcm_add(char *buffer1, const char *buffer2, size_t bufferSize1,
temp32 =
(vol1 * (*buffer16_1) +
vol2 * (*buffer16_2));
- temp32 += rand() & 511;
- temp32 -= rand() & 511;
+ temp32 += pcm_dither();
temp32 += 500;
temp32 /= 1000;
- *buffer16_1 =
- temp32 > 32767 ? 32767 : (temp32 <
- -32768 ? -32768 : temp32);
+ *buffer16_1 = pcm_range(temp32, 16);
buffer16_1++;
buffer16_2++;
bufferSize1 -= 2;
@@ -109,13 +122,10 @@ static void pcm_add(char *buffer1, const char *buffer2, size_t bufferSize1,
while (bufferSize1 > 0 && bufferSize2 > 0) {
temp32 =
(vol1 * (*buffer8_1) + vol2 * (*buffer8_2));
- temp32 += rand() & 511;
- temp32 -= rand() & 511;
+ temp32 += pcm_dither();
temp32 += 500;
temp32 /= 1000;
- *buffer8_1 =
- temp32 > 127 ? 127 : (temp32 <
- -128 ? -128 : temp32);
+ *buffer8_1 = pcm_range(temp32, 8);
buffer8_1++;
buffer8_2++;
bufferSize1--;