mirror of
https://github.com/claunia/flac.git
synced 2025-12-16 18:54:26 +00:00
drop the escape-code stuff
This commit is contained in:
@@ -18,7 +18,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <math.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h> /* for malloc() */
|
#include <stdlib.h> /* for malloc() */
|
||||||
#include <string.h> /* for memcpy() */
|
#include <string.h> /* for memcpy() */
|
||||||
@@ -39,14 +38,6 @@
|
|||||||
#endif
|
#endif
|
||||||
#define max(x,y) ((x)>(y)?(x):(y))
|
#define max(x,y) ((x)>(y)?(x):(y))
|
||||||
|
|
||||||
#ifndef M_LN2
|
|
||||||
/* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
|
|
||||||
#define M_LN2 0.69314718055994530942
|
|
||||||
#endif
|
|
||||||
|
|
||||||
double smult;
|
|
||||||
unsigned rpdec;
|
|
||||||
|
|
||||||
typedef struct FLAC__EncoderPrivate {
|
typedef struct FLAC__EncoderPrivate {
|
||||||
unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
|
unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
|
||||||
int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
|
int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
|
||||||
@@ -89,7 +80,7 @@ static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 res
|
|||||||
static unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], const real lp_coeff[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe);
|
static unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], const real lp_coeff[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe);
|
||||||
static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__Subframe *subframe);
|
static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__Subframe *subframe);
|
||||||
static unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned max_partition_order, unsigned *best_partition_order, unsigned best_parameters[]);
|
static unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned max_partition_order, unsigned *best_partition_order, unsigned best_parameters[]);
|
||||||
static bool encoder_set_partitioned_rice_(const int32 residual[], const uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameter, const unsigned partition_order, unsigned parameters[], unsigned *bits);
|
static bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameter, const unsigned partition_order, unsigned parameters[], unsigned *bits);
|
||||||
|
|
||||||
const char *FLAC__EncoderWriteStatusString[] = {
|
const char *FLAC__EncoderWriteStatusString[] = {
|
||||||
"FLAC__ENCODER_WRITE_OK",
|
"FLAC__ENCODER_WRITE_OK",
|
||||||
@@ -323,11 +314,6 @@ FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWrite
|
|||||||
return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
|
return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
{unsigned r = encoder->rice_optimization_level;
|
|
||||||
smult=(double)(r / 10);
|
|
||||||
rpdec=r % 10;
|
|
||||||
encoder->rice_optimization_level=0;
|
|
||||||
}
|
|
||||||
if(encoder->rice_optimization_level >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
|
if(encoder->rice_optimization_level >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
|
||||||
encoder->rice_optimization_level = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
|
encoder->rice_optimization_level = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
|
||||||
|
|
||||||
@@ -1047,7 +1033,7 @@ unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_r
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(partition_order = 0; partition_order <= max_partition_order; partition_order++) {
|
for(partition_order = 0; partition_order <= max_partition_order; partition_order++) {
|
||||||
if(!encoder_set_partitioned_rice_(residual, abs_residual, residual_samples, predictor_order, rice_parameter, partition_order, parameters[!best_parameters_index], &residual_bits)) {
|
if(!encoder_set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, partition_order, parameters[!best_parameters_index], &residual_bits)) {
|
||||||
assert(best_residual_bits != 0);
|
assert(best_residual_bits != 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1062,83 +1048,17 @@ unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_r
|
|||||||
return best_residual_bits;
|
return best_residual_bits;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned silog21_(int v)
|
|
||||||
{
|
|
||||||
doit_:
|
|
||||||
if(v == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else if(v > 0) {
|
|
||||||
unsigned l = 0;
|
|
||||||
while(v) {
|
|
||||||
l++;
|
|
||||||
v >>= 1;
|
|
||||||
}
|
|
||||||
return l+1;
|
|
||||||
}
|
|
||||||
else if(v == -1) {
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
v = -(++v);
|
|
||||||
goto doit_;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned uilog21_(unsigned v)
|
|
||||||
{
|
|
||||||
unsigned l = 0;
|
|
||||||
while(v) {
|
|
||||||
l++;
|
|
||||||
v >>= 1;
|
|
||||||
}
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint32 get_thresh_(const int32 residual[], const unsigned residual_samples)
|
|
||||||
{
|
|
||||||
double sum, sos, r, stddev, mean;
|
|
||||||
unsigned i;
|
|
||||||
uint32 thresh;
|
|
||||||
|
|
||||||
sum = sos = 0.0;
|
|
||||||
for(i = 0; i < residual_samples; i++) {
|
|
||||||
r = (double)residual[i];
|
|
||||||
sum += r;
|
|
||||||
sos += r*r;
|
|
||||||
}
|
|
||||||
mean = sum / residual_samples;
|
|
||||||
stddev = sqrt((sos - (sum * sum / residual_samples)) / (residual_samples-1));
|
|
||||||
thresh = mean+smult*stddev;
|
|
||||||
thresh = (1u << uilog21_(thresh)) - 1;
|
|
||||||
return smult>0.0? thresh : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef VARIABLE_RICE_BITS
|
#ifdef VARIABLE_RICE_BITS
|
||||||
#undef VARIABLE_RICE_BITS
|
#undef VARIABLE_RICE_BITS
|
||||||
#endif
|
#endif
|
||||||
#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
|
#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
|
||||||
|
|
||||||
bool encoder_set_partitioned_rice_(const int32 residual[], const uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameter, const unsigned partition_order, unsigned parameters[], unsigned *bits)
|
bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameter, const unsigned partition_order, unsigned parameters[], unsigned *bits)
|
||||||
{
|
{
|
||||||
unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
|
unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
|
||||||
|
|
||||||
if(partition_order == 0) {
|
if(partition_order == 0) {
|
||||||
unsigned i;
|
unsigned i;
|
||||||
#ifdef SYMMETRIC_RICE
|
|
||||||
uint32 thresh = get_thresh_(residual, residual_samples);
|
|
||||||
bool cross = false;
|
|
||||||
|
|
||||||
for(i=0;i<residual_samples;i++) {
|
|
||||||
if(abs_residual[i] >= thresh) {
|
|
||||||
cross = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(cross) {
|
|
||||||
rice_parameter -= rpdec;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
{
|
{
|
||||||
#ifdef VARIABLE_RICE_BITS
|
#ifdef VARIABLE_RICE_BITS
|
||||||
@@ -1154,21 +1074,12 @@ bool encoder_set_partitioned_rice_(const int32 residual[], const uint32 abs_resi
|
|||||||
for(i = 0; i < residual_samples; i++) {
|
for(i = 0; i < residual_samples; i++) {
|
||||||
#ifdef VARIABLE_RICE_BITS
|
#ifdef VARIABLE_RICE_BITS
|
||||||
#ifdef SYMMETRIC_RICE
|
#ifdef SYMMETRIC_RICE
|
||||||
if(cross) {
|
bits_ += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
|
||||||
unsigned escbits, normbits;
|
|
||||||
escbits = /* VARIABLE_RICE_BITS(-0, rice_parameter) == 0 */ 5 + silog21_(residual[i]);
|
|
||||||
normbits = VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
|
|
||||||
bits_ += min(escbits, normbits);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
/*@@@ old way */
|
|
||||||
bits_ += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
bits_ += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
|
bits_ += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
|
||||||
#endif
|
#endif
|
||||||
#else
|
#else
|
||||||
bits_ += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter);
|
bits_ += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE we will need to pass in residual[] instead of abs_residual[] */
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1225,7 +1136,7 @@ mean>>=1;
|
|||||||
bits_ += VARIABLE_RICE_BITS(abs_residual[j], parameter);
|
bits_ += VARIABLE_RICE_BITS(abs_residual[j], parameter);
|
||||||
#endif
|
#endif
|
||||||
#else
|
#else
|
||||||
bits_ += FLAC__bitbuffer_rice_bits(residual[j], parameter);
|
bits_ += FLAC__bitbuffer_rice_bits(residual[j], parameter); /* NOTE we will need to pass in residual[] instead of abs_residual[] */
|
||||||
#endif
|
#endif
|
||||||
k_last = k;
|
k_last = k;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
|
||||||
#include "private/encoder_framing.h"
|
#include "private/encoder_framing.h"
|
||||||
#include "private/crc.h"
|
#include "private/crc.h"
|
||||||
|
|
||||||
@@ -320,100 +319,17 @@ bool subframe_add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__Entrop
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned silog21_(int v)
|
|
||||||
{
|
|
||||||
doit_:
|
|
||||||
if(v == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else if(v > 0) {
|
|
||||||
unsigned l = 0;
|
|
||||||
while(v) {
|
|
||||||
l++;
|
|
||||||
v >>= 1;
|
|
||||||
}
|
|
||||||
return l+1;
|
|
||||||
}
|
|
||||||
else if(v == -1) {
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
v = -(++v);
|
|
||||||
goto doit_;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned uilog21_(unsigned v)
|
|
||||||
{
|
|
||||||
unsigned l = 0;
|
|
||||||
while(v) {
|
|
||||||
l++;
|
|
||||||
v >>= 1;
|
|
||||||
}
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern const double smult;
|
|
||||||
|
|
||||||
static uint32 get_thresh_(const int32 residual[], const unsigned residual_samples)
|
|
||||||
{
|
|
||||||
double sum, sos, r, stddev, mean;
|
|
||||||
unsigned i;
|
|
||||||
uint32 thresh;
|
|
||||||
|
|
||||||
sum = sos = 0.0;
|
|
||||||
for(i = 0; i < residual_samples; i++) {
|
|
||||||
r = (double)residual[i];
|
|
||||||
sum += r;
|
|
||||||
sos += r*r;
|
|
||||||
}
|
|
||||||
mean = sum / residual_samples;
|
|
||||||
stddev = sqrt((sos - (sum * sum / residual_samples)) / (residual_samples-1));
|
|
||||||
thresh = mean+smult*stddev;
|
|
||||||
thresh = (1u << uilog21_(thresh)) - 1;
|
|
||||||
return thresh;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool subframe_add_residual_partitioned_rice_(FLAC__BitBuffer *bb, const int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned partition_order)
|
bool subframe_add_residual_partitioned_rice_(FLAC__BitBuffer *bb, const int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned partition_order)
|
||||||
{
|
{
|
||||||
if(partition_order == 0) {
|
if(partition_order == 0) {
|
||||||
unsigned i;
|
unsigned i;
|
||||||
#ifdef SYMMETRIC_RICE
|
|
||||||
uint32 thresh = get_thresh_(residual, residual_samples);
|
|
||||||
bool cross = false;
|
|
||||||
|
|
||||||
for(i=0;i<residual_samples;i++) {
|
|
||||||
uint32 a = (residual[i] < 0? -residual[i] : residual[i]);
|
|
||||||
if(a >= thresh) {
|
|
||||||
cross = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
|
if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
|
||||||
return false;
|
return false;
|
||||||
for(i = 0; i < residual_samples; i++) {
|
for(i = 0; i < residual_samples; i++) {
|
||||||
#ifdef SYMMETRIC_RICE
|
#ifdef SYMMETRIC_RICE
|
||||||
if(cross) {
|
if(!FLAC__bitbuffer_write_symmetric_rice_signed(bb, residual[i], rice_parameters[0]))
|
||||||
unsigned escbits, normbits;
|
return false;
|
||||||
uint32 a = (residual[i] < 0? -residual[i] : residual[i]);
|
|
||||||
escbits = 5 + silog21_(residual[i]);
|
|
||||||
normbits = a >> rice_parameters[0];
|
|
||||||
if(escbits < normbits) {
|
|
||||||
fprintf(stderr,"ESCAPE, k=%u, r=%d, escbits=%u, normbits=%u, saved %u bits\n", rice_parameters[0], residual[i], escbits, normbits, normbits-escbits);
|
|
||||||
if(!FLAC__bitbuffer_write_symmetric_rice_signed_escape(bb, residual[i], rice_parameters[0]))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if(!FLAC__bitbuffer_write_symmetric_rice_signed(bb, residual[i], rice_parameters[0]))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if(!FLAC__bitbuffer_write_symmetric_rice_signed(bb, residual[i], rice_parameters[0]))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
if(!FLAC__bitbuffer_write_rice_signed(bb, residual[i], rice_parameters[0]))
|
if(!FLAC__bitbuffer_write_rice_signed(bb, residual[i], rice_parameters[0]))
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user