mirror of
https://github.com/claunia/flac.git
synced 2025-12-16 18:54:26 +00:00
Miroslav's speed optimization patch
This commit is contained in:
@@ -99,15 +99,16 @@ static FLAC__INLINE FLAC__int32 linear_dither(unsigned source_bps, unsigned targ
|
|||||||
return output >> scalebits;
|
return output >> scalebits;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, FLAC__int32 *input, unsigned wide_samples, unsigned channels, unsigned source_bps, unsigned target_bps)
|
unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, const FLAC__int32 * const input[], unsigned wide_samples, unsigned channels, unsigned source_bps, unsigned target_bps)
|
||||||
{
|
{
|
||||||
static dither_state dither[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
|
static dither_state dither[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
|
||||||
FLAC__byte * const start = data;
|
FLAC__byte * const start = data;
|
||||||
FLAC__int32 sample;
|
FLAC__int32 sample;
|
||||||
unsigned samples = wide_samples * channels;
|
const FLAC__int32 *input_;
|
||||||
|
unsigned samples, channel;
|
||||||
const unsigned bytes_per_sample = target_bps / 8;
|
const unsigned bytes_per_sample = target_bps / 8;
|
||||||
|
unsigned inc = bytes_per_sample * channels;
|
||||||
|
|
||||||
FLAC__ASSERT(FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS == 2);
|
|
||||||
FLAC__ASSERT(channels > 0 && channels <= FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS);
|
FLAC__ASSERT(channels > 0 && channels <= FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS);
|
||||||
FLAC__ASSERT(source_bps < 32);
|
FLAC__ASSERT(source_bps < 32);
|
||||||
FLAC__ASSERT(target_bps <= 24);
|
FLAC__ASSERT(target_bps <= 24);
|
||||||
@@ -116,47 +117,57 @@ unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, FL
|
|||||||
FLAC__ASSERT((target_bps & 7) == 0);
|
FLAC__ASSERT((target_bps & 7) == 0);
|
||||||
|
|
||||||
if(source_bps != target_bps) {
|
if(source_bps != target_bps) {
|
||||||
const FLAC__int32 MIN = -(1L << source_bps);
|
const FLAC__int32 MIN = -(1L << (source_bps - 1));
|
||||||
const FLAC__int32 MAX = ~MIN; /*(1L << (source_bps-1)) - 1 */
|
const FLAC__int32 MAX = ~MIN; /*(1L << (source_bps-1)) - 1 */
|
||||||
const unsigned dither_twiggle = channels - 1;
|
|
||||||
unsigned dither_source = 0;
|
|
||||||
|
|
||||||
while(samples--) {
|
for(channel = 0; channel < channels; channel++) {
|
||||||
sample = linear_dither(source_bps, target_bps, *input++, &dither[dither_source], MIN, MAX);
|
|
||||||
dither_source ^= dither_twiggle;
|
samples = wide_samples;
|
||||||
|
data = start + bytes_per_sample * channel;
|
||||||
|
input_ = input[channel];
|
||||||
|
|
||||||
switch(target_bps) {
|
while(samples--) {
|
||||||
case 8:
|
sample = linear_dither(source_bps, target_bps, *input_++, &dither[channel], MIN, MAX);
|
||||||
data[0] = sample ^ 0x80;
|
|
||||||
break;
|
switch(target_bps) {
|
||||||
case 24:
|
case 8:
|
||||||
data[2] = (FLAC__byte)(sample >> 16);
|
data[0] = sample ^ 0x80;
|
||||||
/* fall through */
|
break;
|
||||||
case 16:
|
case 24:
|
||||||
data[1] = (FLAC__byte)(sample >> 8);
|
data[2] = (FLAC__byte)(sample >> 16);
|
||||||
data[0] = (FLAC__byte)sample;
|
/* fall through */
|
||||||
|
case 16:
|
||||||
|
data[1] = (FLAC__byte)(sample >> 8);
|
||||||
|
data[0] = (FLAC__byte)sample;
|
||||||
|
}
|
||||||
|
|
||||||
|
data += inc;
|
||||||
}
|
}
|
||||||
|
|
||||||
data += bytes_per_sample;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
while(samples--) {
|
for(channel = 0; channel < channels; channel++) {
|
||||||
sample = *input++;
|
samples = wide_samples;
|
||||||
|
data = start + bytes_per_sample * channel;
|
||||||
|
input_ = input[channel];
|
||||||
|
|
||||||
switch(target_bps) {
|
while(samples--) {
|
||||||
case 8:
|
sample = *input_++;
|
||||||
data[0] = sample ^ 0x80;
|
|
||||||
break;
|
switch(target_bps) {
|
||||||
case 24:
|
case 8:
|
||||||
data[2] = (FLAC__byte)(sample >> 16);
|
data[0] = sample ^ 0x80;
|
||||||
/* fall through */
|
break;
|
||||||
case 16:
|
case 24:
|
||||||
data[1] = (FLAC__byte)(sample >> 8);
|
data[2] = (FLAC__byte)(sample >> 16);
|
||||||
data[0] = (FLAC__byte)sample;
|
/* fall through */
|
||||||
|
case 16:
|
||||||
|
data[1] = (FLAC__byte)(sample >> 8);
|
||||||
|
data[0] = (FLAC__byte)sample;
|
||||||
|
}
|
||||||
|
|
||||||
|
data += inc;
|
||||||
}
|
}
|
||||||
|
|
||||||
data += bytes_per_sample;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,6 @@
|
|||||||
#include "defs.h" /* buy FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS for the caller */
|
#include "defs.h" /* buy FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS for the caller */
|
||||||
#include "FLAC/ordinals.h"
|
#include "FLAC/ordinals.h"
|
||||||
|
|
||||||
unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, FLAC__int32 *input, unsigned wide_samples, unsigned channels, unsigned source_bps, unsigned target_bps);
|
unsigned FLAC__plugin_common__pack_pcm_signed_little_endian(FLAC__byte *data, const FLAC__int32 * const input[], unsigned wide_samples, unsigned channels, unsigned source_bps, unsigned target_bps);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -208,6 +208,7 @@ void FLAC__plugin_common__init_dither_context(DitherContext *d, int bits, int sh
|
|||||||
|
|
||||||
if (shapingtype < 0) shapingtype = 0;
|
if (shapingtype < 0) shapingtype = 0;
|
||||||
if (shapingtype > 3) shapingtype = 3;
|
if (shapingtype > 3) shapingtype = 3;
|
||||||
|
d->ShapingType = (NoiseShaping)shapingtype;
|
||||||
index = bits - 11 - shapingtype;
|
index = bits - 11 - shapingtype;
|
||||||
if (index < 0) index = 0;
|
if (index < 0) index = 0;
|
||||||
if (index > 9) index = 9;
|
if (index > 9) index = 9;
|
||||||
@@ -219,6 +220,7 @@ void FLAC__plugin_common__init_dither_context(DitherContext *d, int bits, int sh
|
|||||||
d->Mask = ((FLAC__uint64)-1) << (32 - bits);
|
d->Mask = ((FLAC__uint64)-1) << (32 - bits);
|
||||||
d->Add = 0.5 * ((1L << (32 - bits)) - 1);
|
d->Add = 0.5 * ((1L << (32 - bits)) - 1);
|
||||||
d->Dither = 0.01f*default_dither[index] / (((FLAC__int64)1) << bits);
|
d->Dither = 0.01f*default_dither[index] / (((FLAC__int64)1) << bits);
|
||||||
|
d->LastHistoryIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -286,7 +288,7 @@ static FLAC__INLINE FLAC__int64 dither_output_(DitherContext *d, FLAC__bool do_d
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
int FLAC__plugin_common__apply_gain(FLAC__byte *data_out, FLAC__int32 *input, unsigned wide_samples, unsigned channels, const unsigned source_bps, const unsigned target_bps, const float scale, const FLAC__bool hard_limit, FLAC__bool do_dithering, NoiseShaping noise_shaping, DitherContext *dither_context)
|
int FLAC__plugin_common__apply_gain(FLAC__byte *data_out, const FLAC__int32 * const input[], unsigned wide_samples, unsigned channels, const unsigned source_bps, const unsigned target_bps, const float scale, const FLAC__bool hard_limit, FLAC__bool do_dithering, DitherContext *dither_context)
|
||||||
{
|
{
|
||||||
static const FLAC__int32 conv_factors_[33] = {
|
static const FLAC__int32 conv_factors_[33] = {
|
||||||
-1, /* 0 bits-per-sample (not supported) */
|
-1, /* 0 bits-per-sample (not supported) */
|
||||||
@@ -369,16 +371,15 @@ int FLAC__plugin_common__apply_gain(FLAC__byte *data_out, FLAC__int32 *input, un
|
|||||||
const double multi_scale = scale / (double)(1u << (source_bps-1));
|
const double multi_scale = scale / (double)(1u << (source_bps-1));
|
||||||
|
|
||||||
FLAC__byte * const start = data_out;
|
FLAC__byte * const start = data_out;
|
||||||
const unsigned samples = wide_samples * channels;
|
unsigned i, channel;
|
||||||
#ifdef FLAC__PLUGIN_COMMON__DONT_UNROLL
|
const FLAC__int32 *input_;
|
||||||
const unsigned dither_twiggle = channels - 1;
|
|
||||||
unsigned dither_source = 0;
|
|
||||||
#endif
|
|
||||||
unsigned i;
|
|
||||||
int coeff;
|
|
||||||
double sample;
|
double sample;
|
||||||
|
const unsigned bytes_per_sample = target_bps / 8;
|
||||||
|
unsigned inc = bytes_per_sample * channels, last_history_index = dither_context->LastHistoryIndex;
|
||||||
|
NoiseShaping noise_shaping = dither_context->ShapingType;
|
||||||
|
FLAC__int64 val64;
|
||||||
|
FLAC__int32 val32;
|
||||||
|
|
||||||
FLAC__ASSERT(FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS == 2);
|
|
||||||
FLAC__ASSERT(channels > 0 && channels <= FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS);
|
FLAC__ASSERT(channels > 0 && channels <= FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS);
|
||||||
FLAC__ASSERT(source_bps >= 4);
|
FLAC__ASSERT(source_bps >= 4);
|
||||||
FLAC__ASSERT(target_bps >= 4);
|
FLAC__ASSERT(target_bps >= 4);
|
||||||
@@ -386,67 +387,11 @@ int FLAC__plugin_common__apply_gain(FLAC__byte *data_out, FLAC__int32 *input, un
|
|||||||
FLAC__ASSERT(target_bps < 32);
|
FLAC__ASSERT(target_bps < 32);
|
||||||
FLAC__ASSERT((target_bps & 7) == 0);
|
FLAC__ASSERT((target_bps & 7) == 0);
|
||||||
|
|
||||||
#ifdef FLAC__PLUGIN_COMMON__DONT_UNROLL
|
for(channel = 0; channel < channels; channel++) {
|
||||||
/*
|
data_out = start + bytes_per_sample * channel;
|
||||||
* This flavor handles 1 or 2 channels with the same code
|
input_ = input[channel];
|
||||||
*/
|
for(i = 0; i < wide_samples; i++, data_out += inc) {
|
||||||
coeff = 0;
|
sample = (double)input_[i] * multi_scale;
|
||||||
for(i = 0; i < samples; i++, coeff++) {
|
|
||||||
sample = (double)input[i] * multi_scale;
|
|
||||||
|
|
||||||
if(hard_limit) {
|
|
||||||
/* hard 6dB limiting */
|
|
||||||
if(sample < -0.5)
|
|
||||||
sample = tanh((sample + 0.5) / (1-0.5)) * (1-0.5) - 0.5;
|
|
||||||
else if(sample > 0.5)
|
|
||||||
sample = tanh((sample - 0.5) / (1-0.5)) * (1-0.5) + 0.5;
|
|
||||||
}
|
|
||||||
sample *= 2147483647.f;
|
|
||||||
|
|
||||||
{
|
|
||||||
FLAC__int64 val64;
|
|
||||||
FLAC__int32 val32;
|
|
||||||
|
|
||||||
if(coeff >= (32<<dither_twiggle))
|
|
||||||
coeff = 0;
|
|
||||||
|
|
||||||
/* 'coeff>>dither_twiggle' is the same as 'coeff/channels' */
|
|
||||||
val64 = dither_output_(dither_context, do_dithering, noise_shaping, coeff>>dither_twiggle, sample, dither_source) / conv_factor;
|
|
||||||
|
|
||||||
dither_source ^= dither_twiggle;
|
|
||||||
|
|
||||||
val32 = (FLAC__int32)val64;
|
|
||||||
if(val64 >= -hard_clip_factor)
|
|
||||||
val32 = (FLAC__int32)(-(hard_clip_factor+1));
|
|
||||||
else if(val64 < hard_clip_factor)
|
|
||||||
val32 = (FLAC__int32)hard_clip_factor;
|
|
||||||
|
|
||||||
switch(target_bps) {
|
|
||||||
case 8:
|
|
||||||
data_out[0] = val32 ^ 0x80;
|
|
||||||
break;
|
|
||||||
case 24:
|
|
||||||
data_out[2] = (FLAC__byte)(val32 >> 16);
|
|
||||||
/* fall through */
|
|
||||||
case 16:
|
|
||||||
data_out[1] = (FLAC__byte)(val32 >> 8);
|
|
||||||
data_out[0] = (FLAC__byte)val32;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data_out += target_bps/8;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
/*
|
|
||||||
* This flavor has optimized versions for 1 or 2 channels
|
|
||||||
*/
|
|
||||||
if(channels == 2) {
|
|
||||||
FLAC__int64 val64;
|
|
||||||
FLAC__int32 val32;
|
|
||||||
|
|
||||||
coeff = 0;
|
|
||||||
for(i = 0; i < samples; ) {
|
|
||||||
sample = (double)input[i] * multi_scale;
|
|
||||||
|
|
||||||
if(hard_limit) {
|
if(hard_limit) {
|
||||||
/* hard 6dB limiting */
|
/* hard 6dB limiting */
|
||||||
@@ -457,7 +402,7 @@ int FLAC__plugin_common__apply_gain(FLAC__byte *data_out, FLAC__int32 *input, un
|
|||||||
}
|
}
|
||||||
sample *= 2147483647.f;
|
sample *= 2147483647.f;
|
||||||
|
|
||||||
val64 = dither_output_(dither_context, do_dithering, noise_shaping, coeff, sample, 0) / conv_factor;
|
val64 = dither_output_(dither_context, do_dithering, noise_shaping, (i + last_history_index) % 32, sample, channel) / conv_factor;
|
||||||
|
|
||||||
val32 = (FLAC__int32)val64;
|
val32 = (FLAC__int32)val64;
|
||||||
if(val64 >= -hard_clip_factor)
|
if(val64 >= -hard_clip_factor)
|
||||||
@@ -476,94 +421,9 @@ int FLAC__plugin_common__apply_gain(FLAC__byte *data_out, FLAC__int32 *input, un
|
|||||||
data_out[1] = (FLAC__byte)(val32 >> 8);
|
data_out[1] = (FLAC__byte)(val32 >> 8);
|
||||||
data_out[0] = (FLAC__byte)val32;
|
data_out[0] = (FLAC__byte)val32;
|
||||||
}
|
}
|
||||||
|
|
||||||
data_out += target_bps/8;
|
|
||||||
|
|
||||||
i++;
|
|
||||||
|
|
||||||
sample = (double)input[i] * multi_scale;
|
|
||||||
|
|
||||||
if(hard_limit) {
|
|
||||||
/* hard 6dB limiting */
|
|
||||||
if(sample < -0.5)
|
|
||||||
sample = tanh((sample + 0.5) / (1-0.5)) * (1-0.5) - 0.5;
|
|
||||||
else if(sample > 0.5)
|
|
||||||
sample = tanh((sample - 0.5) / (1-0.5)) * (1-0.5) + 0.5;
|
|
||||||
}
|
|
||||||
sample *= 2147483647.f;
|
|
||||||
|
|
||||||
val64 = dither_output_(dither_context, do_dithering, noise_shaping, coeff, sample, 1) / conv_factor;
|
|
||||||
|
|
||||||
val32 = (FLAC__int32)val64;
|
|
||||||
if(val64 >= -hard_clip_factor)
|
|
||||||
val32 = (FLAC__int32)(-(hard_clip_factor+1));
|
|
||||||
else if(val64 < hard_clip_factor)
|
|
||||||
val32 = (FLAC__int32)hard_clip_factor;
|
|
||||||
|
|
||||||
switch(target_bps) {
|
|
||||||
case 8:
|
|
||||||
data_out[0] = val32 ^ 0x80;
|
|
||||||
break;
|
|
||||||
case 24:
|
|
||||||
data_out[2] = (FLAC__byte)(val32 >> 16);
|
|
||||||
/* fall through */
|
|
||||||
case 16:
|
|
||||||
data_out[1] = (FLAC__byte)(val32 >> 8);
|
|
||||||
data_out[0] = (FLAC__byte)val32;
|
|
||||||
}
|
|
||||||
|
|
||||||
data_out += target_bps/8;
|
|
||||||
|
|
||||||
i++;
|
|
||||||
coeff++;
|
|
||||||
if(coeff >= 32)
|
|
||||||
coeff = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
dither_context->LastHistoryIndex = (last_history_index + wide_samples) % 32;
|
||||||
FLAC__int64 val64;
|
|
||||||
FLAC__int32 val32;
|
|
||||||
|
|
||||||
coeff = 0;
|
|
||||||
for(i = 0; i < samples; i++, coeff++) {
|
|
||||||
if(coeff >= 32)
|
|
||||||
coeff = 0;
|
|
||||||
|
|
||||||
sample = (double)input[i] * multi_scale;
|
|
||||||
|
|
||||||
if(hard_limit) {
|
|
||||||
/* hard 6dB limiting */
|
|
||||||
if(sample < -0.5)
|
|
||||||
sample = tanh((sample + 0.5) / (1-0.5)) * (1-0.5) - 0.5;
|
|
||||||
else if(sample > 0.5)
|
|
||||||
sample = tanh((sample - 0.5) / (1-0.5)) * (1-0.5) + 0.5;
|
|
||||||
}
|
|
||||||
sample *= 2147483647.f;
|
|
||||||
|
|
||||||
val64 = dither_output_(dither_context, do_dithering, noise_shaping, coeff, sample, 0) / conv_factor;
|
|
||||||
|
|
||||||
val32 = (FLAC__int32)val64;
|
|
||||||
if(val64 >= -hard_clip_factor)
|
|
||||||
val32 = (FLAC__int32)(-(hard_clip_factor+1));
|
|
||||||
else if(val64 < hard_clip_factor)
|
|
||||||
val32 = (FLAC__int32)hard_clip_factor;
|
|
||||||
|
|
||||||
switch(target_bps) {
|
|
||||||
case 8:
|
|
||||||
data_out[0] = val32 ^ 0x80;
|
|
||||||
break;
|
|
||||||
case 24:
|
|
||||||
data_out[2] = (FLAC__byte)(val32 >> 16);
|
|
||||||
/* fall through */
|
|
||||||
case 16:
|
|
||||||
data_out[1] = (FLAC__byte)(val32 >> 8);
|
|
||||||
data_out[0] = (FLAC__byte)val32;
|
|
||||||
}
|
|
||||||
|
|
||||||
data_out += target_bps/8;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return data_out - start;
|
return data_out - start;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,13 @@
|
|||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
#include "FLAC/ordinals.h"
|
#include "FLAC/ordinals.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
NOISE_SHAPING_NONE = 0,
|
||||||
|
NOISE_SHAPING_LOW = 1,
|
||||||
|
NOISE_SHAPING_MEDUIM = 2,
|
||||||
|
NOISE_SHAPING_HIGH = 3
|
||||||
|
} NoiseShaping;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const float* FilterCoeff;
|
const float* FilterCoeff;
|
||||||
FLAC__uint64 Mask;
|
FLAC__uint64 Mask;
|
||||||
@@ -30,18 +37,13 @@ typedef struct {
|
|||||||
float ErrorHistory [FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS] [16]; /* 16th order Noise shaping */
|
float ErrorHistory [FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS] [16]; /* 16th order Noise shaping */
|
||||||
float DitherHistory [FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS] [16];
|
float DitherHistory [FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS] [16];
|
||||||
int LastRandomNumber [FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
|
int LastRandomNumber [FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
|
||||||
|
unsigned LastHistoryIndex;
|
||||||
|
NoiseShaping ShapingType;
|
||||||
} DitherContext;
|
} DitherContext;
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
NOISE_SHAPING_NONE = 0,
|
|
||||||
NOISE_SHAPING_LOW = 1,
|
|
||||||
NOISE_SHAPING_MEDUIM = 2,
|
|
||||||
NOISE_SHAPING_HIGH = 3
|
|
||||||
} NoiseShaping;
|
|
||||||
|
|
||||||
void FLAC__plugin_common__init_dither_context(DitherContext *dither, int bits, int shapingtype);
|
void FLAC__plugin_common__init_dither_context(DitherContext *dither, int bits, int shapingtype);
|
||||||
|
|
||||||
/* scale = (float) pow(10., (double)replaygain * 0.05); */
|
/* scale = (float) pow(10., (double)replaygain * 0.05); */
|
||||||
int FLAC__plugin_common__apply_gain(FLAC__byte *data_out, FLAC__int32 *input, unsigned wide_samples, unsigned channels, const unsigned source_bps, const unsigned target_bps, const float scale, const FLAC__bool hard_limit, FLAC__bool do_dithering, NoiseShaping noise_shaping, DitherContext *dither_context);
|
int FLAC__plugin_common__apply_gain(FLAC__byte *data_out, const FLAC__int32 * const input[], unsigned wide_samples, unsigned channels, const unsigned source_bps, const unsigned target_bps, const float scale, const FLAC__bool hard_limit, FLAC__bool do_dithering, DitherContext *dither_context);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ typedef struct {
|
|||||||
unsigned length_in_msec;
|
unsigned length_in_msec;
|
||||||
gchar *title;
|
gchar *title;
|
||||||
AFormat sample_format;
|
AFormat sample_format;
|
||||||
|
unsigned sample_format_bytes_per_sample;
|
||||||
int seek_to_in_sec;
|
int seek_to_in_sec;
|
||||||
FLAC__bool has_replaygain;
|
FLAC__bool has_replaygain;
|
||||||
double replay_scale;
|
double replay_scale;
|
||||||
@@ -114,9 +115,9 @@ InputPlugin flac_ip =
|
|||||||
};
|
};
|
||||||
|
|
||||||
#define SAMPLES_PER_WRITE 512
|
#define SAMPLES_PER_WRITE 512
|
||||||
static FLAC__int32 reservoir_[FLAC__MAX_BLOCK_SIZE * 2/*for overflow*/ * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
|
#define SAMPLE_BUFFER_SIZE ((FLAC__MAX_BLOCK_SIZE + SAMPLES_PER_WRITE) * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8))
|
||||||
static FLAC__byte sample_buffer_[SAMPLES_PER_WRITE * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8)]; /* (24/8) for max bytes per sample */
|
static FLAC__byte sample_buffer_[SAMPLE_BUFFER_SIZE];
|
||||||
static unsigned wide_samples_in_reservoir_ = 0;
|
static unsigned sample_buffer_first_, sample_buffer_last_;
|
||||||
|
|
||||||
static FLAC__FileDecoder *decoder_ = 0;
|
static FLAC__FileDecoder *decoder_ = 0;
|
||||||
static file_info_struct file_info_;
|
static file_info_struct file_info_;
|
||||||
@@ -198,7 +199,7 @@ void FLAC_XMMS__play_file(char *filename)
|
|||||||
{
|
{
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
|
||||||
wide_samples_in_reservoir_ = 0;
|
sample_buffer_first_ = sample_buffer_last_ = 0;
|
||||||
audio_error_ = false;
|
audio_error_ = false;
|
||||||
file_info_.abort_flag = false;
|
file_info_.abort_flag = false;
|
||||||
file_info_.is_playing = false;
|
file_info_.is_playing = false;
|
||||||
@@ -216,9 +217,39 @@ void FLAC_XMMS__play_file(char *filename)
|
|||||||
if(!safe_decoder_init_(filename, decoder_))
|
if(!safe_decoder_init_(filename, decoder_))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable && flac_cfg.output.resolution.replaygain.dither)
|
if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable) {
|
||||||
FLAC__plugin_common__init_dither_context(&file_info_.dither_context, file_info_.bits_per_sample, flac_cfg.output.resolution.replaygain.noise_shaping);
|
if(flac_cfg.output.resolution.replaygain.bps_out == 8) {
|
||||||
|
file_info_.sample_format = FMT_U8;
|
||||||
|
file_info_.sample_format_bytes_per_sample = 1;
|
||||||
|
}
|
||||||
|
else if(flac_cfg.output.resolution.replaygain.bps_out == 16) {
|
||||||
|
file_info_.sample_format = FMT_S16_LE;
|
||||||
|
file_info_.sample_format_bytes_per_sample = 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samples\n", "ERROR: plugin can only handle 8/16-bit samples", 0); */
|
||||||
|
fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", flac_cfg.output.resolution.replaygain.bps_out);
|
||||||
|
safe_decoder_finish_(decoder_);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if(file_info_.bits_per_sample == 8) {
|
||||||
|
file_info_.sample_format = FMT_U8;
|
||||||
|
file_info_.sample_format_bytes_per_sample = 1;
|
||||||
|
}
|
||||||
|
else if(file_info_.bits_per_sample == 16 || (file_info_.bits_per_sample == 24 && flac_cfg.output.resolution.normal.dither_24_to_16)) {
|
||||||
|
file_info_.sample_format = FMT_S16_LE;
|
||||||
|
file_info_.sample_format_bytes_per_sample = 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samples\n", "ERROR: plugin can only handle 8/16-bit samples", 0); */
|
||||||
|
fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", file_info_.bits_per_sample);
|
||||||
|
safe_decoder_finish_(decoder_);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FLAC__plugin_common__init_dither_context(&file_info_.dither_context, file_info_.sample_format_bytes_per_sample * 8, flac_cfg.output.resolution.replaygain.noise_shaping);
|
||||||
file_info_.is_playing = true;
|
file_info_.is_playing = true;
|
||||||
|
|
||||||
if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
|
if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
|
||||||
@@ -317,10 +348,10 @@ void *play_loop_(void *arg)
|
|||||||
|
|
||||||
while(file_info_.is_playing) {
|
while(file_info_.is_playing) {
|
||||||
if(!file_info_.eof) {
|
if(!file_info_.eof) {
|
||||||
while(wide_samples_in_reservoir_ < SAMPLES_PER_WRITE) {
|
while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) {
|
||||||
unsigned s;
|
unsigned s;
|
||||||
|
|
||||||
s = wide_samples_in_reservoir_;
|
s = sample_buffer_last_ - sample_buffer_first_;
|
||||||
if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
|
if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
|
||||||
file_info_.eof = true;
|
file_info_.eof = true;
|
||||||
break;
|
break;
|
||||||
@@ -331,57 +362,24 @@ void *play_loop_(void *arg)
|
|||||||
file_info_.eof = true;
|
file_info_.eof = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
blocksize = wide_samples_in_reservoir_ - s;
|
blocksize = sample_buffer_last_ - sample_buffer_first_ - s;
|
||||||
decode_position_frame_last = decode_position_frame;
|
decode_position_frame_last = decode_position_frame;
|
||||||
if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
|
if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
|
||||||
decode_position_frame = 0;
|
decode_position_frame = 0;
|
||||||
}
|
}
|
||||||
if(wide_samples_in_reservoir_ > 0) {
|
if(sample_buffer_last_ - sample_buffer_first_ > 0) {
|
||||||
const unsigned channels = file_info_.channels;
|
const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE);
|
||||||
const unsigned bits_per_sample = file_info_.bits_per_sample;
|
int bytes = n * file_info_.channels * file_info_.sample_format_bytes_per_sample;
|
||||||
const unsigned n = min(wide_samples_in_reservoir_, SAMPLES_PER_WRITE);
|
FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * file_info_.channels * file_info_.sample_format_bytes_per_sample;
|
||||||
const unsigned delta = n * channels;
|
unsigned written_time, bh_index_w;
|
||||||
int bytes;
|
|
||||||
unsigned i, written_time, bh_index_w;
|
|
||||||
FLAC__uint64 decode_position;
|
FLAC__uint64 decode_position;
|
||||||
|
|
||||||
if(flac_cfg.output.replaygain.enable && file_info_.has_replaygain) {
|
sample_buffer_first_ += n;
|
||||||
bytes = (int)FLAC__plugin_common__apply_gain(
|
flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, file_info_.channels, bytes, sample_buffer_start);
|
||||||
sample_buffer_,
|
|
||||||
reservoir_,
|
|
||||||
n,
|
|
||||||
channels,
|
|
||||||
bits_per_sample,
|
|
||||||
flac_cfg.output.resolution.replaygain.bps_out,
|
|
||||||
file_info_.replay_scale,
|
|
||||||
flac_cfg.output.replaygain.hard_limit,
|
|
||||||
flac_cfg.output.resolution.replaygain.dither,
|
|
||||||
(NoiseShaping)flac_cfg.output.resolution.replaygain.noise_shaping,
|
|
||||||
&file_info_.dither_context
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
bytes = (int)FLAC__plugin_common__pack_pcm_signed_little_endian(
|
|
||||||
sample_buffer_,
|
|
||||||
reservoir_,
|
|
||||||
n,
|
|
||||||
channels,
|
|
||||||
bits_per_sample,
|
|
||||||
flac_cfg.output.resolution.normal.dither_24_to_16?
|
|
||||||
min(bits_per_sample, 16) :
|
|
||||||
bits_per_sample
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(i = delta; i < wide_samples_in_reservoir_ * channels; i++)
|
|
||||||
reservoir_[i-delta] = reservoir_[i];
|
|
||||||
wide_samples_in_reservoir_ -= n;
|
|
||||||
|
|
||||||
flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, channels, bytes, sample_buffer_);
|
|
||||||
while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
|
while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
|
||||||
xmms_usleep(10000);
|
xmms_usleep(10000);
|
||||||
if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
|
if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
|
||||||
flac_ip.output->write_audio(sample_buffer_, bytes);
|
flac_ip.output->write_audio(sample_buffer_start, bytes);
|
||||||
|
|
||||||
/* compute current bitrate */
|
/* compute current bitrate */
|
||||||
|
|
||||||
@@ -389,7 +387,7 @@ void *play_loop_(void *arg)
|
|||||||
bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
|
bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
|
||||||
if(bh_index_w != bh_index_last_w) {
|
if(bh_index_w != bh_index_last_w) {
|
||||||
bh_index_last_w = bh_index_w;
|
bh_index_last_w = bh_index_w;
|
||||||
decode_position = decode_position_frame - (double)wide_samples_in_reservoir_ * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize;
|
decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize;
|
||||||
bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
|
bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
|
||||||
decode_position > decode_position_last && written_time > written_time_last ?
|
decode_position > decode_position_last && written_time > written_time_last ?
|
||||||
8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
|
8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
|
||||||
@@ -415,7 +413,7 @@ void *play_loop_(void *arg)
|
|||||||
decode_position_frame = 0;
|
decode_position_frame = 0;
|
||||||
file_info_.seek_to_in_sec = -1;
|
file_info_.seek_to_in_sec = -1;
|
||||||
file_info_.eof = false;
|
file_info_.eof = false;
|
||||||
wide_samples_in_reservoir_ = 0;
|
sample_buffer_first_ = sample_buffer_last_ = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -483,18 +481,46 @@ FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder,
|
|||||||
{
|
{
|
||||||
file_info_struct *file_info = (file_info_struct *)client_data;
|
file_info_struct *file_info = (file_info_struct *)client_data;
|
||||||
const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
|
const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
|
||||||
unsigned wide_sample, offset_sample, channel;
|
const unsigned bits_per_sample = file_info->bits_per_sample;
|
||||||
|
FLAC__byte *sample_buffer_start;
|
||||||
|
|
||||||
(void)decoder;
|
(void)decoder;
|
||||||
|
|
||||||
if(file_info->abort_flag)
|
if(file_info->abort_flag)
|
||||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||||
|
|
||||||
for(offset_sample = wide_samples_in_reservoir_ * channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
|
if((sample_buffer_last_ + wide_samples) > (SAMPLE_BUFFER_SIZE / (channels * file_info->sample_format_bytes_per_sample))) {
|
||||||
for(channel = 0; channel < channels; channel++, offset_sample++)
|
memmove(sample_buffer_, sample_buffer_ + sample_buffer_first_ * channels * file_info->sample_format_bytes_per_sample, (sample_buffer_last_ - sample_buffer_first_) * channels * file_info->sample_format_bytes_per_sample);
|
||||||
reservoir_[offset_sample] = buffer[channel][wide_sample];
|
sample_buffer_last_ -= sample_buffer_first_;
|
||||||
|
sample_buffer_first_ = 0;
|
||||||
|
}
|
||||||
|
sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * file_info->sample_format_bytes_per_sample;
|
||||||
|
if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable) {
|
||||||
|
FLAC__plugin_common__apply_gain(
|
||||||
|
sample_buffer_start,
|
||||||
|
buffer,
|
||||||
|
wide_samples,
|
||||||
|
channels,
|
||||||
|
bits_per_sample,
|
||||||
|
file_info->sample_format_bytes_per_sample * 8,
|
||||||
|
file_info_.replay_scale,
|
||||||
|
flac_cfg.output.replaygain.hard_limit,
|
||||||
|
flac_cfg.output.resolution.replaygain.dither,
|
||||||
|
&file_info_.dither_context
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
FLAC__plugin_common__pack_pcm_signed_little_endian(
|
||||||
|
sample_buffer_start,
|
||||||
|
buffer,
|
||||||
|
wide_samples,
|
||||||
|
channels,
|
||||||
|
bits_per_sample,
|
||||||
|
file_info->sample_format_bytes_per_sample * 8
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
wide_samples_in_reservoir_ += wide_samples;
|
sample_buffer_last_ += wide_samples;
|
||||||
|
|
||||||
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
||||||
}
|
}
|
||||||
@@ -509,32 +535,6 @@ void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMeta
|
|||||||
file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
|
file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
|
||||||
file_info->channels = metadata->data.stream_info.channels;
|
file_info->channels = metadata->data.stream_info.channels;
|
||||||
file_info->sample_rate = metadata->data.stream_info.sample_rate;
|
file_info->sample_rate = metadata->data.stream_info.sample_rate;
|
||||||
|
|
||||||
#ifdef FLAC__DO_DITHER
|
|
||||||
if(file_info->bits_per_sample == 8) {
|
|
||||||
file_info->sample_format = FMT_S8;
|
|
||||||
}
|
|
||||||
else if(file_info->bits_per_sample == 16 || file_info->bits_per_sample == 24) {
|
|
||||||
file_info->sample_format = FMT_S16_LE;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
/*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16/24-bit samples\n", "ERROR: plugin can only handle 8/16/24-bit samples", 0); */
|
|
||||||
file_info->abort_flag = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
if(file_info->bits_per_sample == 8) {
|
|
||||||
file_info->sample_format = FMT_S8;
|
|
||||||
}
|
|
||||||
else if(file_info->bits_per_sample == 16) {
|
|
||||||
file_info->sample_format = FMT_S16_LE;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
/*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samples\n", "ERROR: plugin can only handle 8/16-bit samples", 0); */
|
|
||||||
file_info->abort_flag = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
|
file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
|
||||||
}
|
}
|
||||||
else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
|
else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
|
||||||
|
|||||||
Reference in New Issue
Block a user