From 82b7324d702da8c0a07f78881f85a8799476e891 Mon Sep 17 00:00:00 2001 From: Josh Coalson Date: Wed, 28 Mar 2001 22:17:05 +0000 Subject: [PATCH] rewrite wasted_bits and subframe_bps stuff to be more concise --- src/libFLAC/encoder.c | 143 +++++++++++------- src/libFLAC/encoder_framing.c | 16 +- src/libFLAC/include/private/encoder_framing.h | 8 +- 3 files changed, 97 insertions(+), 70 deletions(-) diff --git a/src/libFLAC/encoder.c b/src/libFLAC/encoder.c index b59f778b..e81520b9 100644 --- a/src/libFLAC/encoder.c +++ b/src/libFLAC/encoder.c @@ -45,8 +45,8 @@ typedef struct FLAC__EncoderPrivate { int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */ real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */ real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */ - unsigned wasted_bits[FLAC__MAX_CHANNELS]; /* 'wasted_bits' value of each independent channel */ - unsigned wasted_bits_mid_side[2]; /* 'wasted_bits' value of the mid-side input signal (stereo only) */ + unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */ + unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits -/+ 1) */ int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */ int32 *residual_workspace_mid_side[2][2]; FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2]; @@ -76,12 +76,12 @@ 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); -static bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits); -static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, unsigned wasted_bits, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame); -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_(const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__Subframe *subframe); +static bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits); +static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame); +static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe); +static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], unsigned blocksize, unsigned subframe_bps, 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 subframe_bps, 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 subframe_bps, 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_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); static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples); @@ -270,7 +270,7 @@ FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWrite if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo) return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE; - if(encoder->bits_per_sample == 0 || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE) + if(encoder->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE) return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE; if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE) @@ -692,17 +692,24 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame) assert(do_independent || do_mid_side); /* - * Check for wasted bits + * Check for wasted bits; set effective bps for each subframe */ if(do_independent) { - for(channel = 0; channel < encoder->channels; channel++) - encoder->guts->wasted_bits[channel] = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize); + unsigned w; + for(channel = 0; channel < encoder->channels; channel++) { + w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize); + encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w; + encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w; + } } if(do_mid_side) { + unsigned w; assert(encoder->channels == 2); - - for(channel = 0; channel < 2; channel++) - encoder->guts->wasted_bits_mid_side[channel] = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize); + for(channel = 0; channel < 2; channel++) { + w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize); + encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w; + encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1); + } } /* @@ -710,7 +717,7 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame) */ if(do_independent) { for(channel = 0; channel < encoder->channels; channel++) { - if(!encoder_process_subframe_(encoder, max_partition_order, false, &frame_header, encoder->bits_per_sample, encoder->guts->integer_signal[channel], encoder->guts->real_signal[channel], encoder->guts->subframe_workspace_ptr[channel], encoder->guts->residual_workspace[channel], encoder->guts->best_subframe+channel, encoder->guts->best_subframe_bits+channel)) + if(!encoder_process_subframe_(encoder, max_partition_order, false, &frame_header, encoder->guts->subframe_bps[channel], encoder->guts->integer_signal[channel], encoder->guts->real_signal[channel], encoder->guts->subframe_workspace_ptr[channel], encoder->guts->residual_workspace[channel], encoder->guts->best_subframe+channel, encoder->guts->best_subframe_bits+channel)) return false; } } @@ -722,7 +729,7 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame) assert(encoder->channels == 2); for(channel = 0; channel < 2; channel++) { - if(!encoder_process_subframe_(encoder, max_partition_order, false, &frame_header, encoder->bits_per_sample+(channel==0? 0:1), encoder->guts->integer_signal_mid_side[channel], encoder->guts->real_signal_mid_side[channel], encoder->guts->subframe_workspace_ptr_mid_side[channel], encoder->guts->residual_workspace_mid_side[channel], encoder->guts->best_subframe_mid_side+channel, encoder->guts->best_subframe_bits_mid_side+channel)) + if(!encoder_process_subframe_(encoder, max_partition_order, false, &frame_header, encoder->guts->subframe_bps_mid_side[channel], encoder->guts->integer_signal_mid_side[channel], encoder->guts->real_signal_mid_side[channel], encoder->guts->subframe_workspace_ptr_mid_side[channel], encoder->guts->residual_workspace_mid_side[channel], encoder->guts->best_subframe_mid_side+channel, encoder->guts->best_subframe_bits_mid_side+channel)) return false; } } @@ -731,6 +738,8 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame) * Compose the frame bitbuffer */ if(do_mid_side) { + unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ + FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */ FLAC__ChannelAssignment channel_assignment; assert(encoder->channels == 2); @@ -767,34 +776,52 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame) } switch(channel_assignment) { - /* note that encoder_add_subframe_ sets the state for us in case of an error */ case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: - if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample -encoder->guts->wasted_bits [0], encoder->guts->wasted_bits [0], &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]], &encoder->guts->frame)) - return false; - if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample -encoder->guts->wasted_bits [1], encoder->guts->wasted_bits [1], &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]], &encoder->guts->frame)) - return false; + left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]]; + right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]]; break; case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: - if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample -encoder->guts->wasted_bits [0], encoder->guts->wasted_bits [0], &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]], &encoder->guts->frame)) - return false; - if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample+1-encoder->guts->wasted_bits_mid_side[1], encoder->guts->wasted_bits_mid_side[1], &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame)) - return false; + left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]]; + right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]]; break; case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: - if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample+1-encoder->guts->wasted_bits_mid_side[1], encoder->guts->wasted_bits_mid_side[1], &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame)) - return false; - if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample -encoder->guts->wasted_bits [1], encoder->guts->wasted_bits [1], &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]], &encoder->guts->frame)) - return false; + left_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]]; + right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]]; break; case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: - if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample -encoder->guts->wasted_bits_mid_side[0], encoder->guts->wasted_bits_mid_side[0], &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]], &encoder->guts->frame)) - return false; - if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample+1-encoder->guts->wasted_bits_mid_side[1], encoder->guts->wasted_bits_mid_side[1], &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]], &encoder->guts->frame)) - return false; + left_subframe = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]]; + right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]]; break; default: assert(0); } + + switch(channel_assignment) { + case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: + left_bps = encoder->guts->subframe_bps [0]; + right_bps = encoder->guts->subframe_bps [1]; + break; + case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: + left_bps = encoder->guts->subframe_bps [0]; + right_bps = encoder->guts->subframe_bps_mid_side[1]; + break; + case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: + left_bps = encoder->guts->subframe_bps_mid_side[1]; + right_bps = encoder->guts->subframe_bps [1]; + break; + case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: + left_bps = encoder->guts->subframe_bps_mid_side[0]; + right_bps = encoder->guts->subframe_bps_mid_side[1]; + break; + default: + assert(0); + } + + /* note that encoder_add_subframe_ sets the state for us in case of an error */ + if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame)) + return false; + if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame)) + return false; } else { if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) { @@ -803,7 +830,7 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame) } for(channel = 0; channel < encoder->channels; channel++) { - if(!encoder_add_subframe_(encoder, &frame_header, encoder->bits_per_sample-encoder->guts->wasted_bits[channel], encoder->guts->wasted_bits[channel], &encoder->guts->subframe_workspace[channel][encoder->guts->best_subframe[channel]], &encoder->guts->frame)) { + if(!encoder_add_subframe_(encoder, &frame_header, encoder->guts->subframe_bps[channel], &encoder->guts->subframe_workspace[channel][encoder->guts->best_subframe[channel]], &encoder->guts->frame)) { /* the above function sets the state for us in case of an error */ return false; } @@ -821,7 +848,7 @@ bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame) return true; } -bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits) +bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits) { real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; real lpc_residual_bits_per_sample; @@ -837,7 +864,7 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or /* verbatim subframe is the baseline against which we measure other compressed subframes */ _best_subframe = 0; - _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, bits_per_sample, subframe[_best_subframe]); + _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]); if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { /* check for constant subframe */ @@ -852,7 +879,7 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or } } if(signal_is_constant) { - _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], bits_per_sample, subframe[!_best_subframe]); + _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]); if(_candidate_bits < _best_bits) { _best_subframe = !_best_subframe; _best_bits = _candidate_bits; @@ -869,7 +896,7 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or min_fixed_order = max_fixed_order = guess_fixed_order; } for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { - if(fixed_residual_bits_per_sample[fixed_order] >= (real)bits_per_sample) + if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps) continue; /* don't even try */ rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+0.5) : 0; /* 0.5 is for rounding */ #ifndef SYMMETRIC_RICE @@ -877,7 +904,7 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or #endif if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)) rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1; - _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, frame_header->blocksize, bits_per_sample, fixed_order, rice_parameter, max_partition_order, subframe[!_best_subframe]); + _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, max_partition_order, subframe[!_best_subframe]); if(_candidate_bits < _best_bits) { _best_subframe = !_best_subframe; _best_bits = _candidate_bits; @@ -899,19 +926,19 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or min_lpc_order = 1; } else { - unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, bits_per_sample); + unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps); min_lpc_order = max_lpc_order = guess_lpc_order; } if(encoder->do_qlp_coeff_prec_search) { min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; - max_qlp_coeff_precision = min(32 - bits_per_sample - 1, (1u<qlp_coeff_precision; } for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) { lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order); - if(lpc_residual_bits_per_sample >= (real)bits_per_sample) + if(lpc_residual_bits_per_sample >= (real)subframe_bps) continue; /* don't even try */ rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */ #ifndef SYMMETRIC_RICE @@ -920,7 +947,7 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)) rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1; for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) { - _candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, lp_coeff[lpc_order-1], frame_header->blocksize, bits_per_sample, lpc_order, qlp_coeff_precision, rice_parameter, max_partition_order, subframe[!_best_subframe]); + _candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, max_partition_order, subframe[!_best_subframe]); if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ if(_candidate_bits < _best_bits) { _best_subframe = !_best_subframe; @@ -941,29 +968,29 @@ bool encoder_process_subframe_(FLAC__Encoder *encoder, unsigned max_partition_or return true; } -bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned bits_per_sample, unsigned wasted_bits, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame) +bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame) { switch(subframe->type) { case FLAC__SUBFRAME_TYPE_CONSTANT: - if(!FLAC__subframe_add_constant(&(subframe->data.constant), bits_per_sample, wasted_bits, frame)) { + if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) { encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; return false; } break; case FLAC__SUBFRAME_TYPE_FIXED: - if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, bits_per_sample, wasted_bits, frame)) { + if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) { encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; return false; } break; case FLAC__SUBFRAME_TYPE_LPC: - if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, bits_per_sample, wasted_bits, frame)) { + if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) { encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; return false; } break; case FLAC__SUBFRAME_TYPE_VERBATIM: - if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, bits_per_sample, wasted_bits, frame)) { + if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) { encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; return false; } @@ -975,15 +1002,15 @@ bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *fram return true; } -unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__Subframe *subframe) +unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe) { subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; subframe->data.constant.value = signal; - return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + bits_per_sample; + return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps; } -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 encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe) { unsigned i, residual_bits; const unsigned residual_samples = blocksize - order; @@ -1001,17 +1028,17 @@ 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_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * bits_per_sample) + residual_bits; + return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + 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__Subframe *subframe) +unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], const real lp_coeff[], unsigned blocksize, unsigned subframe_bps, 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; int quantization, ret; const unsigned residual_samples = blocksize - order; - ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, bits_per_sample, qlp_coeff, &quantization); + ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization); if(ret != 0) return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ @@ -1031,16 +1058,16 @@ 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_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + bits_per_sample)) + residual_bits; + return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits; } -unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__Subframe *subframe) +unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe) { subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; subframe->data.verbatim.data = signal; - return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * bits_per_sample); + return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps); } 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[]) diff --git a/src/libFLAC/encoder_framing.c b/src/libFLAC/encoder_framing.c index 8d6b5db1..2b1d3f02 100644 --- a/src/libFLAC/encoder_framing.c +++ b/src/libFLAC/encoder_framing.c @@ -230,20 +230,20 @@ bool FLAC__frame_add_header(const FLAC__FrameHeader *header, bool streamable_sub return true; } -bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned bits_per_sample, unsigned wasted_bits, FLAC__BitBuffer *bb) +bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb) { bool ok; ok = FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN) && (wasted_bits? FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1) : true) && - FLAC__bitbuffer_write_raw_int32(bb, subframe->value, bits_per_sample) + FLAC__bitbuffer_write_raw_int32(bb, subframe->value, subframe_bps) ; return ok; } -bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned bits_per_sample, unsigned wasted_bits, FLAC__BitBuffer *bb) +bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb) { unsigned i; @@ -254,7 +254,7 @@ bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned res return false; for(i = 0; i < subframe->order; i++) - if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], bits_per_sample)) + if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps)) return false; if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method)) @@ -271,7 +271,7 @@ bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned res return true; } -bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned bits_per_sample, unsigned wasted_bits, FLAC__BitBuffer *bb) +bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb) { unsigned i; @@ -282,7 +282,7 @@ bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residua return false; for(i = 0; i < subframe->order; i++) - if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], bits_per_sample)) + if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps)) return false; if(!FLAC__bitbuffer_write_raw_uint32(bb, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)) @@ -307,7 +307,7 @@ bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residua return true; } -bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned bits_per_sample, unsigned wasted_bits, FLAC__BitBuffer *bb) +bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb) { unsigned i; const int32 *signal = subframe->data; @@ -319,7 +319,7 @@ bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsign return false; for(i = 0; i < samples; i++) - if(!FLAC__bitbuffer_write_raw_int32(bb, signal[i], bits_per_sample)) + if(!FLAC__bitbuffer_write_raw_int32(bb, signal[i], subframe_bps)) return false; return true; diff --git a/src/libFLAC/include/private/encoder_framing.h b/src/libFLAC/include/private/encoder_framing.h index e56e6934..a61ed4f8 100644 --- a/src/libFLAC/include/private/encoder_framing.h +++ b/src/libFLAC/include/private/encoder_framing.h @@ -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(const FLAC__Subframe_Constant *subframe, unsigned bits_per_sample, unsigned wasted_bits, FLAC__BitBuffer *bb); -bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned bits_per_sample, unsigned wasted_bits, FLAC__BitBuffer *bb); -bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned bits_per_sample, unsigned wasted_bits, FLAC__BitBuffer *bb); -bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned bits_per_sample, unsigned wasted_bits, FLAC__BitBuffer *bb); +bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb); +bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb); +bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb); +bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb); #endif