Commit 6dcea51c authored by Josh Coalson's avatar Josh Coalson
Browse files

revamp in anticipation of new analysis mode

parent b10d83cd
......@@ -49,7 +49,7 @@ void FLAC__file_decoder_free_instance(FLAC__FileDecoder *decoder);
FLAC__FileDecoderState FLAC__file_decoder_init(
FLAC__FileDecoder *decoder,
const char *filename,
FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data),
FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data),
void (*metadata_callback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
void (*error_callback)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
void *client_data
......
......@@ -109,13 +109,15 @@ typedef enum {
*/
typedef struct {
int32 value;
} FLAC__SubframeHeader_Constant;
} FLAC__Subframe_Constant;
/*****************************************************************************
*
* n*i: unencoded signal; n = frame's bits-per-sample, i = frame's blocksize
*/
/* There is no (trivial) for structure FLAC__SubframeHeader_Verbatim */
typedef struct {
int32 *data;
} FLAC__Subframe_Verbatim;
/*****************************************************************************
*
......@@ -128,7 +130,8 @@ typedef struct {
FLAC__EntropyCodingMethod entropy_coding_method;
unsigned order;
int32 warmup[FLAC__MAX_FIXED_ORDER];
} FLAC__SubframeHeader_Fixed;
int32 *residual;
} FLAC__Subframe_Fixed;
/*****************************************************************************
*
......@@ -147,10 +150,11 @@ typedef struct {
int quantization_level;
int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
int32 warmup[FLAC__MAX_LPC_ORDER];
} FLAC__SubframeHeader_LPC;
int32 *residual;
} FLAC__Subframe_LPC;
extern const unsigned FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN; /* = 4 bits */
extern const unsigned FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN; /* = 5 bits */
extern const unsigned FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN; /* = 4 bits */
extern const unsigned FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN; /* = 5 bits */
/*****************************************************************************
*
......@@ -164,28 +168,24 @@ extern const unsigned FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN; /* = 5 bits */
* 001xxxx0: reserved
* 01xxxxx0: lpc, xxxxx=order-1
* 1xxxxxxx: invalid, to prevent sync-fooling string of 1s (use to check for erroneous sync)
* ?: subframe-specific header (c.f. FLAC__SubframeHeader_*)
* ?: subframe-specific data (c.f. FLAC__Subframe_*)
*/
typedef struct {
FLAC__SubframeType type;
union {
FLAC__SubframeHeader_Constant constant;
FLAC__SubframeHeader_Fixed fixed;
FLAC__SubframeHeader_LPC lpc;
} data; /* data will be undefined for FLAC__SUBFRAME_TYPE_VERBATIM */
} FLAC__SubframeHeader;
extern const unsigned FLAC__SUBFRAME_HEADER_TYPE_CONSTANT; /* = 0x00 */
extern const unsigned FLAC__SUBFRAME_HEADER_TYPE_VERBATIM; /* = 0x02 */
extern const unsigned FLAC__SUBFRAME_HEADER_TYPE_FIXED; /* = 0x10 */
extern const unsigned FLAC__SUBFRAME_HEADER_TYPE_LPC; /* = 0x40 */
extern const unsigned FLAC__SUBFRAME_HEADER_TYPE_LEN; /* = 8 bits */
typedef struct {
FLAC__SubframeHeader header;
const int32 *data;
FLAC__Subframe_Constant constant;
FLAC__Subframe_Fixed fixed;
FLAC__Subframe_LPC lpc;
FLAC__Subframe_Verbatim verbatim;
} data;
} FLAC__Subframe;
extern const unsigned FLAC__SUBFRAME_TYPE_CONSTANT_BITS; /* = 0x00 */
extern const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BITS; /* = 0x02 */
extern const unsigned FLAC__SUBFRAME_TYPE_FIXED_BITS; /* = 0x10 */
extern const unsigned FLAC__SUBFRAME_TYPE_LPC_BITS; /* = 0x40 */
extern const unsigned FLAC__SUBFRAME_TYPE_LEN; /* = 8 bits */
/*****************************************************************************/
......
......@@ -71,7 +71,7 @@ void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder);
FLAC__StreamDecoderState FLAC__stream_decoder_init(
FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data),
FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data),
void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
void *client_data
......
......@@ -51,7 +51,7 @@ static bool is_big_endian_host;
static bool init(const char *infile, stream_info_struct *stream_info);
static bool write_little_endian_uint16(FILE *f, uint16 val);
static bool write_little_endian_uint32(FILE *f, uint32 val);
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data);
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
static void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
static void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
static void print_stats(const stream_info_struct *stream_info);
......@@ -290,14 +290,14 @@ bool write_little_endian_uint32(FILE *f, uint32 val)
return fwrite(b, 1, 4, f) == 4;
}
FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data)
FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
{
stream_info_struct *stream_info = (stream_info_struct *)client_data;
FILE *fout = stream_info->fout;
unsigned bps = stream_info->bps, channels = stream_info->channels;
bool is_big_endian = (stream_info->is_wave_out? false : stream_info->is_big_endian);
bool is_unsigned_samples = (stream_info->is_wave_out? bps==8 : stream_info->is_unsigned_samples);
unsigned wide_samples = header->blocksize, wide_sample, sample, channel, byte;
unsigned wide_samples = frame->header.blocksize, wide_sample, sample, channel, byte;
static signed char scbuffer[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * ((FLAC__MAX_BITS_PER_SAMPLE+7)>>3)]; /* WATCHOUT: can be up to 2 megs */
unsigned char *ucbuffer = (unsigned char *)scbuffer;
signed short *ssbuffer = (signed short *)scbuffer;
......
......@@ -86,7 +86,7 @@ static void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsi
static FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
static void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
static FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
static FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data);
static FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
static void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
static void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
static void print_stats(const encoder_wrapper_struct *encoder_wrapper);
......@@ -688,7 +688,7 @@ FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *de
return FLAC__STREAM_DECODER_READ_CONTINUE;
}
FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data)
FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
{
encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
unsigned channel, l, r;
......@@ -702,11 +702,11 @@ FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *
}
/* dequeue the frame from the fifo */
for(channel = 0; channel < decoder->channels; channel++) {
for(l = 0, r = header->blocksize; r < encoder_wrapper->verify_fifo.tail; l++, r++) {
for(l = 0, r = frame->header.blocksize; r < encoder_wrapper->verify_fifo.tail; l++, r++) {
encoder_wrapper->verify_fifo.original[channel][l] = encoder_wrapper->verify_fifo.original[channel][r];
}
}
encoder_wrapper->verify_fifo.tail -= header->blocksize;
encoder_wrapper->verify_fifo.tail -= frame->header.blocksize;
return FLAC__STREAM_DECODER_WRITE_CONTINUE;
}
......
......@@ -51,7 +51,7 @@ typedef struct FLAC__EncoderPrivate {
FLAC__BitBuffer frame_mid_side; /* special parallel workspace for the mid-side coded version of the current frame */
FLAC__BitBuffer frame_left_side; /* special parallel workspace for the left-side coded version of the current frame */
FLAC__BitBuffer frame_right_side; /* special parallel workspace for the right-side coded version of the current frame */
FLAC__SubframeHeader best_subframe, candidate_subframe;
FLAC__Subframe best_subframe, candidate_subframe;
bool current_frame_can_do_mid_side; /* encoder sets this false when any given sample of a frame's side channel exceeds 16 bits */
FLAC__StreamMetaData metadata;
unsigned current_sample_number;
......@@ -65,15 +65,11 @@ typedef struct FLAC__EncoderPrivate {
static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame, const FLAC__FrameHeader *frame_header, unsigned channels, const int32 *integer_signal[], const real *real_signal[], FLAC__BitBuffer *bitbuffer);
static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__SubframeHeader *subframe);
static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__SubframeHeader *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__SubframeHeader *subframe);
static unsigned encoder_evaluate_verbatim_subframe_(unsigned blocksize, unsigned bits_per_sample, FLAC__SubframeHeader *subframe);
static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__Subframe *subframe);
static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], unsigned blocksize, unsigned bits_per_sample, unsigned order, 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_(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 bool encoder_generate_constant_subframe_(const FLAC__SubframeHeader *header, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer);
static bool encoder_generate_fixed_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer);
static bool encoder_generate_lpc_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer);
static bool encoder_generate_verbatim_subframe_(const FLAC__SubframeHeader *header, const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer);
static void encoder_promote_candidate_subframe_(FLAC__Encoder *encoder);
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);
......@@ -765,25 +761,28 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame, cons
/* add the best subframe */
switch(encoder->guts->best_subframe.type) {
case FLAC__SUBFRAME_TYPE_CONSTANT:
if(!encoder_generate_constant_subframe_(&(encoder->guts->best_subframe), frame_header->bits_per_sample, frame)) {
if(!FLAC__subframe_add_constant(&(encoder->guts->best_subframe.data.constant), frame_header->bits_per_sample, frame)) {
encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
return false;
}
break;
case FLAC__SUBFRAME_TYPE_FIXED:
if(!encoder_generate_fixed_subframe_(&(encoder->guts->best_subframe), encoder->guts->residual[encoder->guts->best_residual], frame_header->blocksize, frame_header->bits_per_sample, frame)) {
encoder->guts->best_subframe.data.fixed.residual = encoder->guts->residual[encoder->guts->best_residual];
if(!FLAC__subframe_add_fixed(&(encoder->guts->best_subframe.data.fixed), frame_header->blocksize - encoder->guts->best_subframe.data.fixed.order, frame_header->bits_per_sample, frame)) {
encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
return false;
}
break;
case FLAC__SUBFRAME_TYPE_LPC:
if(!encoder_generate_lpc_subframe_(&(encoder->guts->best_subframe), encoder->guts->residual[encoder->guts->best_residual], frame_header->blocksize, frame_header->bits_per_sample, frame)) {
encoder->guts->best_subframe.data.lpc.residual = encoder->guts->residual[encoder->guts->best_residual];
if(!FLAC__subframe_add_lpc(&(encoder->guts->best_subframe.data.lpc), frame_header->blocksize - encoder->guts->best_subframe.data.lpc.order, frame_header->bits_per_sample, frame)) {
encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
return false;
}
break;
case FLAC__SUBFRAME_TYPE_VERBATIM:
if(!encoder_generate_verbatim_subframe_(&(encoder->guts->best_subframe), integer_signal[channel], frame_header->blocksize, frame_header->bits_per_sample, frame)) {
encoder->guts->best_subframe.data.verbatim.data = integer_signal[channel];
if(!FLAC__subframe_add_verbatim(&(encoder->guts->best_subframe.data.verbatim), frame_header->blocksize, frame_header->bits_per_sample, frame)) {
encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
return false;
}
......@@ -794,15 +793,15 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame, cons
return true;
}
unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__SubframeHeader *subframe)
unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__Subframe *subframe)
{
subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
subframe->data.constant.value = signal;
return FLAC__SUBFRAME_HEADER_TYPE_LEN + bits_per_sample;
return FLAC__SUBFRAME_TYPE_LEN + bits_per_sample;
}
unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__SubframeHeader *subframe)
unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe)
{
unsigned i, residual_bits;
const unsigned residual_samples = blocksize - order;
......@@ -819,10 +818,10 @@ unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[]
for(i = 0; i < order; i++)
subframe->data.fixed.warmup[i] = signal[i];
return FLAC__SUBFRAME_HEADER_TYPE_LEN + (order * bits_per_sample) + residual_bits;
return FLAC__SUBFRAME_TYPE_LEN + (order * bits_per_sample) + residual_bits;
}
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__SubframeHeader *subframe)
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)
{
int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
unsigned i, residual_bits;
......@@ -848,14 +847,14 @@ unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[],
for(i = 0; i < order; i++)
subframe->data.lpc.warmup[i] = signal[i];
return FLAC__SUBFRAME_HEADER_TYPE_LEN + FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + bits_per_sample)) + residual_bits;
return FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + bits_per_sample)) + residual_bits;
}
unsigned encoder_evaluate_verbatim_subframe_(unsigned blocksize, unsigned bits_per_sample, FLAC__SubframeHeader *subframe)
unsigned encoder_evaluate_verbatim_subframe_(unsigned blocksize, unsigned bits_per_sample, FLAC__Subframe *subframe)
{
subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
return FLAC__SUBFRAME_HEADER_TYPE_LEN + (blocksize * bits_per_sample);
return FLAC__SUBFRAME_TYPE_LEN + (blocksize * bits_per_sample);
}
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[])
......@@ -887,33 +886,6 @@ unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_r
return best_residual_bits;
}
bool encoder_generate_constant_subframe_(const FLAC__SubframeHeader *header, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer)
{
assert(header->type == FLAC__SUBFRAME_TYPE_CONSTANT);
return FLAC__subframe_add_constant(bits_per_sample, header, bitbuffer);
}
bool encoder_generate_fixed_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer)
{
assert(header->type == FLAC__SUBFRAME_TYPE_FIXED);
return FLAC__subframe_add_fixed(residual, blocksize - header->data.fixed.order, bits_per_sample, header, bitbuffer);
}
bool encoder_generate_lpc_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer)
{
assert(header->type == FLAC__SUBFRAME_TYPE_LPC);
return FLAC__subframe_add_lpc(residual, blocksize - header->data.lpc.order, bits_per_sample, header, bitbuffer);
}
bool encoder_generate_verbatim_subframe_(const FLAC__SubframeHeader *header, const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer)
{
assert(header->type == FLAC__SUBFRAME_TYPE_VERBATIM);
#ifdef NDEBUG
(void)header; /* silence compiler warning about unused parameter */
#endif
return FLAC__subframe_add_verbatim(signal, blocksize, bits_per_sample, bitbuffer);
}
void encoder_promote_candidate_subframe_(FLAC__Encoder *encoder)
{
assert(encoder->state == FLAC__ENCODER_OK);
......
......@@ -215,34 +215,34 @@ bool FLAC__frame_add_header(const FLAC__FrameHeader *header, bool streamable_sub
return true;
}
bool FLAC__subframe_add_constant(unsigned bits_per_sample, const FLAC__SubframeHeader *subframe, FLAC__BitBuffer *bb)
bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned bits_per_sample, FLAC__BitBuffer *bb)
{
bool ok;
ok =
FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_HEADER_TYPE_CONSTANT, FLAC__SUBFRAME_HEADER_TYPE_LEN) &&
FLAC__bitbuffer_write_raw_int32(bb, subframe->data.constant.value, bits_per_sample)
FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_CONSTANT_BITS, FLAC__SUBFRAME_TYPE_LEN) &&
FLAC__bitbuffer_write_raw_int32(bb, subframe->value, bits_per_sample)
;
return ok;
}
bool FLAC__subframe_add_fixed(const int32 residual[], unsigned residual_samples, unsigned bits_per_sample, const FLAC__SubframeHeader *subframe, FLAC__BitBuffer *bb)
bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned bits_per_sample, FLAC__BitBuffer *bb)
{
unsigned i;
if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_HEADER_TYPE_FIXED | (subframe->data.fixed.order<<1), FLAC__SUBFRAME_HEADER_TYPE_LEN))
if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_FIXED_BITS | (subframe->order<<1), FLAC__SUBFRAME_TYPE_LEN))
return false;
for(i = 0; i < subframe->data.fixed.order; i++)
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->data.fixed.warmup[i], bits_per_sample))
for(i = 0; i < subframe->order; i++)
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], bits_per_sample))
return false;
if(!subframe_add_entropy_coding_method_(bb, &subframe->data.fixed.entropy_coding_method))
if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
return false;
switch(subframe->data.fixed.entropy_coding_method.type) {
switch(subframe->entropy_coding_method.type) {
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
if(!subframe_add_residual_partitioned_rice_(bb, residual, residual_samples, subframe->data.fixed.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order))
if(!subframe_add_residual_partitioned_rice_(bb, subframe->residual, residual_samples, subframe->order, subframe->entropy_coding_method.data.partitioned_rice.parameters, subframe->entropy_coding_method.data.partitioned_rice.order))
return false;
break;
default:
......@@ -252,30 +252,30 @@ bool FLAC__subframe_add_fixed(const int32 residual[], unsigned residual_samples,
return true;
}
bool FLAC__subframe_add_lpc(const int32 residual[], unsigned residual_samples, unsigned bits_per_sample, const FLAC__SubframeHeader *subframe, FLAC__BitBuffer *bb)
bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned bits_per_sample, FLAC__BitBuffer *bb)
{
unsigned i;
if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_HEADER_TYPE_LPC | ((subframe->data.lpc.order-1)<<1), FLAC__SUBFRAME_HEADER_TYPE_LEN))
if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_LPC_BITS | ((subframe->order-1)<<1), FLAC__SUBFRAME_TYPE_LEN))
return false;
for(i = 0; i < subframe->data.lpc.order; i++)
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->data.lpc.warmup[i], bits_per_sample))
for(i = 0; i < subframe->order; i++)
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], bits_per_sample))
return false;
if(!FLAC__bitbuffer_write_raw_uint32(bb, subframe->data.lpc.qlp_coeff_precision-1, FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN))
if(!FLAC__bitbuffer_write_raw_uint32(bb, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
return false;
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->data.lpc.quantization_level, FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN))
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
return false;
for(i = 0; i < subframe->data.lpc.order; i++)
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->data.lpc.qlp_coeff[i], subframe->data.lpc.qlp_coeff_precision))
for(i = 0; i < subframe->order; i++)
if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
return false;
if(!subframe_add_entropy_coding_method_(bb, &subframe->data.lpc.entropy_coding_method))
if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
return false;
switch(subframe->data.lpc.entropy_coding_method.type) {
switch(subframe->entropy_coding_method.type) {
case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
if(!subframe_add_residual_partitioned_rice_(bb, residual, residual_samples, subframe->data.lpc.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order))
if(!subframe_add_residual_partitioned_rice_(bb, subframe->residual, residual_samples, subframe->order, subframe->entropy_coding_method.data.partitioned_rice.parameters, subframe->entropy_coding_method.data.partitioned_rice.order))
return false;
break;
default:
......@@ -285,11 +285,12 @@ bool FLAC__subframe_add_lpc(const int32 residual[], unsigned residual_samples, u
return true;
}
bool FLAC__subframe_add_verbatim(const int32 signal[], unsigned samples, unsigned bits_per_sample, FLAC__BitBuffer *bb)
bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned bits_per_sample, FLAC__BitBuffer *bb)
{
unsigned i;
const int32 *signal = subframe->data;
if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_HEADER_TYPE_VERBATIM, FLAC__SUBFRAME_HEADER_TYPE_LEN))
if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_VERBATIM_BITS, FLAC__SUBFRAME_TYPE_LEN))
return false;
for(i = 0; i < samples; i++)
......
......@@ -26,7 +26,7 @@
#include "private/md5.h"
typedef struct FLAC__FileDecoderPrivate {
FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data);
FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
void (*metadata_callback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
void (*error_callback)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
void *client_data;
......@@ -37,12 +37,12 @@ typedef struct FLAC__FileDecoderPrivate {
byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
/* the rest of these are only used for seeking: */
FLAC__StreamMetaData_Encoding metadata; /* we keep this around so we can figure out how to seek quickly */
FLAC__FrameHeader last_frame_header; /* holds the info of the last frame we seeked to */
FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
uint64 target_sample;
} FLAC__FileDecoderPrivate;
static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data);
static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
static bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64 target_sample);
......@@ -76,7 +76,7 @@ void FLAC__file_decoder_free_instance(FLAC__FileDecoder *decoder)
FLAC__FileDecoderState FLAC__file_decoder_init(
FLAC__FileDecoder *decoder,
const char *filename,
FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data),
FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data),
void (*metadata_callback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
void (*error_callback)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
void *client_data
......@@ -291,17 +291,17 @@ FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder,
return FLAC__STREAM_DECODER_READ_ABORT; /* abort to avoid a deadlock */
}
FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data)
FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
{
FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
(void)decoder;
if(file_decoder->state == FLAC__FILE_DECODER_SEEKING) {
uint64 this_frame_sample = header->number.sample_number;
uint64 next_frame_sample = this_frame_sample + (uint64)header->blocksize;
uint64 this_frame_sample = frame->header.number.sample_number;
uint64 next_frame_sample = this_frame_sample + (uint64)frame->header.blocksize;
uint64 target_sample = file_decoder->guts->target_sample;
file_decoder->guts->last_frame_header = *header; /* save the header in the guts */
file_decoder->guts->last_frame = *frame; /* save the frame in the guts */
if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
unsigned delta = (unsigned)(target_sample - this_frame_sample);
/* kick out of seek mode */
......@@ -310,16 +310,16 @@ FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decode
if(delta > 0) {
unsigned channel;
const int32 *newbuffer[FLAC__MAX_CHANNELS];
for(channel = 0; channel < header->channels; channel++)
for(channel = 0; channel < frame->header.channels; channel++)
newbuffer[channel] = buffer[channel] + delta;
file_decoder->guts->last_frame_header.blocksize -= delta;
file_decoder->guts->last_frame_header.number.sample_number += (uint64)delta;
file_decoder->guts->last_frame.header.blocksize -= delta;
file_decoder->guts->last_frame.header.number.sample_number += (uint64)delta;
/* write the relevant samples */
return file_decoder->guts->write_callback(file_decoder, &file_decoder->guts->last_frame_header, newbuffer, file_decoder->guts->client_data);
return file_decoder->guts->write_callback(file_decoder, &file_decoder->guts->last_frame, newbuffer, file_decoder->guts->client_data);
}
else {
/* write the relevant samples */
return file_decoder->guts->write_callback(file_decoder, header, buffer, file_decoder->guts->client_data);
return file_decoder->guts->write_callback(file_decoder, frame, buffer, file_decoder->guts->client_data);
}
}
else {
......@@ -328,10 +328,10 @@ FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decode
}
else {
if(file_decoder->check_md5) {
if(!FLAC__MD5Accumulate(&file_decoder->guts->md5context, buffer, header->channels, header->blocksize, (header->bits_per_sample+7) / 8))
if(!FLAC__MD5Accumulate(&file_decoder->guts->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
return FLAC__STREAM_DECODER_WRITE_ABORT;
}
return file_decoder->guts->write_callback(file_decoder, header, buffer, file_decoder->guts->client_data);
return file_decoder->guts->write_callback(file_decoder, frame, buffer, file_decoder->guts->client_data);
}
}
......@@ -416,7 +416,7 @@ bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64
break;
}
else { /* we need to narrow the search */
uint64 this_frame_sample = decoder->guts->last_frame_header.number.sample_number;
uint64 this_frame_sample = decoder->guts->last_frame.header.number.sample_number;
if(this_frame_sample == last_frame_sample) {
/* our last move backwards wasn't big enough */
pos -= (last_pos - pos);
......@@ -425,7 +425,7 @@ bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64
else {
if(target_sample < this_frame_sample) {
last_pos = pos;
approx_bytes_per_frame = decoder->guts->last_frame_header.blocksize * decoder->guts->last_frame_header.channels * decoder->guts->last_frame_header.bits_per_sample/8 + 64;
approx_bytes_per_frame = decoder->guts->last_frame.header.blocksize * decoder->guts->last_frame.header.channels * decoder->guts->last_frame.header.bits_per_sample/8 + 64;
pos -= approx_bytes_per_frame;
needs_seek = true;
}
......
......@@ -56,12 +56,12 @@ const unsigned FLAC__ENTROPY_CODING_METHOD_TYPE_LEN = 2; /* bits */
const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN = 4; /* bits */
const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN = 4; /* bits */
const unsigned FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN = 4; /* bits */
const unsigned FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN = 5; /* bits */
const unsigned FLAC__SUBFRAME_HEADER_LPC_RICE_PARAMETER_LEN = 4; /* bits */
const unsigned FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN = 4; /* bits */
const unsigned FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN = 5; /* bits */
const unsigned FLAC__SUBFRAME_LPC_RICE_PARAMETER_LEN = 4; /* bits */
const unsigned FLAC__SUBFRAME_HEADER_TYPE_CONSTANT = 0x00;
const unsigned FLAC__SUBFRAME_HEADER_TYPE_VERBATIM = 0x02;
const unsigned FLAC__SUBFRAME_HEADER_TYPE_FIXED = 0x10;
const unsigned FLAC__SUBFRAME_HEADER_TYPE_LPC = 0x40;
const unsigned FLAC__SUBFRAME_HEADER_TYPE_LEN = 8; /* bits */
const unsigned FLAC__SUBFRAME_TYPE_CONSTANT_BITS = 0x00;
const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BITS = 0x02;
const unsigned FLAC__SUBFRAME_TYPE_FIXED_BITS = 0x10;
const unsigned FLAC__SUBFRAME_TYPE_LPC_BITS = 0x40;
const unsigned FLAC__SUBFRAME_TYPE_LEN = 8; /* bits */
......@@ -25,9 +25,9 @@
bool FLAC__add_metadata_block(const FLAC__StreamMetaData *metadata, FLAC__BitBuffer *bb);
bool FLAC__frame_add_header(const FLAC__FrameHeader *header, bool streamable_subset, bool is_last_block, FLAC__BitBuffer *bb);
bool FLAC__subframe_add_constant(unsigned bits_per_sample, const FLAC__SubframeHeader *subframe, FLAC__BitBuffer *bb);
bool FLAC__subframe_add_fixed(const int32 residual[], unsigned residual_samples, unsigned bits_per_sample, const FLAC__SubframeHeader *subframe, FLAC__BitBuffer *bb);
bool FLAC__subframe_add_lpc(const int32 residual[], unsigned residual_samples, unsigned bits_per_sample, const FLAC__SubframeHeader *subframe, FLAC__BitBuffer *bb);
bool FLAC__subframe_add_verbatim(const int32 signal[], unsigned samples, unsigned bits_per_sample, FLAC__BitBuffer *bb);
bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned bits_per_sample, FLAC__BitBuffer *bb);
bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned bits_per_sample, FLAC__BitBuffer *bb);
bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned bits_per_sample, FLAC__BitBuffer *bb);
bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned bits_per_sample, FLAC__BitBuffer *bb);
#endif
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment