Josh Coalson | 26560dd | 2001-02-08 00:38:41 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Josh Coalson | 70118f6 | 2001-01-16 20:17:53 +0000 | [diff] [blame] | 2 | * Copyright (C) 2000,2001 Josh Coalson |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public |
| 15 | * License along with this library; if not, write to the |
| 16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | * Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <assert.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> /* for malloc() */ |
| 23 | #include <string.h> /* for memcpy() */ |
| 24 | #include "FLAC/encoder.h" |
| 25 | #include "private/bitbuffer.h" |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 26 | #include "private/crc.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 27 | #include "private/encoder_framing.h" |
| 28 | #include "private/fixed.h" |
| 29 | #include "private/lpc.h" |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 30 | #include "private/md5.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 31 | |
| 32 | #ifdef min |
| 33 | #undef min |
| 34 | #endif |
| 35 | #define min(x,y) ((x)<(y)?(x):(y)) |
| 36 | |
| 37 | #ifdef max |
| 38 | #undef max |
| 39 | #endif |
| 40 | #define max(x,y) ((x)>(y)?(x):(y)) |
| 41 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 42 | typedef struct FLAC__EncoderPrivate { |
| 43 | unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */ |
| 44 | int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */ |
| 45 | int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */ |
| 46 | real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */ |
| 47 | real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */ |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 48 | unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */ |
| 49 | unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits -/+ 1) */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 50 | int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */ |
| 51 | int32 *residual_workspace_mid_side[2][2]; |
| 52 | FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2]; |
| 53 | FLAC__Subframe subframe_workspace_mid_side[2][2]; |
| 54 | FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 55 | FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2]; |
| 56 | unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */ |
| 57 | unsigned best_subframe_mid_side[2]; |
| 58 | unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */ |
| 59 | unsigned best_subframe_bits_mid_side[2]; |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 60 | uint32 *abs_residual; /* workspace where the abs(candidate residual) is stored */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 61 | FLAC__BitBuffer frame; /* the current frame being worked on */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 62 | bool current_frame_can_do_mid_side; /* encoder sets this false when any given sample of a frame's side channel exceeds 16 bits */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 63 | double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 64 | unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 65 | unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */ |
| 66 | FLAC__ChannelAssignment last_channel_assignment; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 67 | FLAC__StreamMetaData metadata; |
| 68 | unsigned current_sample_number; |
| 69 | unsigned current_frame_number; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 70 | struct MD5Context md5context; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 71 | FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data); |
| 72 | void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data); |
| 73 | void *client_data; |
| 74 | } FLAC__EncoderPrivate; |
| 75 | |
| 76 | static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size); |
| 77 | static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 78 | static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 79 | 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); |
| 80 | static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame); |
| 81 | static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe); |
| 82 | 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); |
| 83 | 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); |
| 84 | static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe); |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 85 | 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[]); |
Josh Coalson | 3b5f471 | 2001-03-21 22:53:43 +0000 | [diff] [blame] | 86 | 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); |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 87 | static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 88 | |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 89 | const char *FLAC__EncoderWriteStatusString[] = { |
| 90 | "FLAC__ENCODER_WRITE_OK", |
| 91 | "FLAC__ENCODER_WRITE_FATAL_ERROR" |
| 92 | }; |
| 93 | |
| 94 | const char *FLAC__EncoderStateString[] = { |
| 95 | "FLAC__ENCODER_OK", |
| 96 | "FLAC__ENCODER_UNINITIALIZED", |
| 97 | "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS", |
| 98 | "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE", |
| 99 | "FLAC__ENCODER_INVALID_SAMPLE_RATE", |
| 100 | "FLAC__ENCODER_INVALID_BLOCK_SIZE", |
| 101 | "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION", |
| 102 | "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH", |
| 103 | "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH", |
Josh Coalson | 69f1ee0 | 2001-01-24 00:54:43 +0000 | [diff] [blame] | 104 | "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE", |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 105 | "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER", |
| 106 | "FLAC__ENCODER_NOT_STREAMABLE", |
| 107 | "FLAC__ENCODER_FRAMING_ERROR", |
| 108 | "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING", |
| 109 | "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING", |
| 110 | "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR" |
| 111 | }; |
| 112 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 113 | |
| 114 | bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size) |
| 115 | { |
| 116 | bool ok; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 117 | unsigned i, channel; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 118 | int32 *previous_is, *current_is; |
| 119 | real *previous_rs, *current_rs; |
| 120 | int32 *residual; |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 121 | uint32 *abs_residual; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 122 | |
| 123 | assert(new_size > 0); |
| 124 | assert(encoder->state == FLAC__ENCODER_OK); |
| 125 | assert(encoder->guts->current_sample_number == 0); |
| 126 | |
| 127 | /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
| 128 | if(new_size <= encoder->guts->input_capacity) |
| 129 | return true; |
| 130 | |
| 131 | ok = 1; |
| 132 | if(ok) { |
| 133 | for(i = 0; ok && i < encoder->channels; i++) { |
| 134 | /* integer version of the signal */ |
| 135 | previous_is = encoder->guts->integer_signal[i]; |
| 136 | current_is = (int32*)malloc(sizeof(int32) * new_size); |
| 137 | if(0 == current_is) { |
| 138 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 139 | ok = 0; |
| 140 | } |
| 141 | else { |
| 142 | encoder->guts->integer_signal[i] = current_is; |
| 143 | if(previous_is != 0) |
| 144 | free(previous_is); |
| 145 | } |
| 146 | /* real version of the signal */ |
| 147 | previous_rs = encoder->guts->real_signal[i]; |
| 148 | current_rs = (real*)malloc(sizeof(real) * new_size); |
| 149 | if(0 == current_rs) { |
| 150 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 151 | ok = 0; |
| 152 | } |
| 153 | else { |
| 154 | encoder->guts->real_signal[i] = current_rs; |
| 155 | if(previous_rs != 0) |
| 156 | free(previous_rs); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | if(ok) { |
| 161 | for(i = 0; ok && i < 2; i++) { |
| 162 | /* integer version of the signal */ |
| 163 | previous_is = encoder->guts->integer_signal_mid_side[i]; |
| 164 | current_is = (int32*)malloc(sizeof(int32) * new_size); |
| 165 | if(0 == current_is) { |
| 166 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 167 | ok = 0; |
| 168 | } |
| 169 | else { |
| 170 | encoder->guts->integer_signal_mid_side[i] = current_is; |
| 171 | if(previous_is != 0) |
| 172 | free(previous_is); |
| 173 | } |
| 174 | /* real version of the signal */ |
| 175 | previous_rs = encoder->guts->real_signal_mid_side[i]; |
| 176 | current_rs = (real*)malloc(sizeof(real) * new_size); |
| 177 | if(0 == current_rs) { |
| 178 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 179 | ok = 0; |
| 180 | } |
| 181 | else { |
| 182 | encoder->guts->real_signal_mid_side[i] = current_rs; |
| 183 | if(previous_rs != 0) |
| 184 | free(previous_rs); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | if(ok) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 189 | for(channel = 0; channel < encoder->channels; channel++) { |
| 190 | for(i = 0; i < 2; i++) { |
| 191 | residual = (int32*)malloc(sizeof(int32) * new_size); |
| 192 | if(0 == residual) { |
| 193 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 194 | ok = 0; |
| 195 | } |
| 196 | else { |
| 197 | if(encoder->guts->residual_workspace[channel][i] != 0) |
| 198 | free(encoder->guts->residual_workspace[channel][i]); |
| 199 | encoder->guts->residual_workspace[channel][i] = residual; |
| 200 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 201 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 202 | } |
| 203 | for(channel = 0; channel < 2; channel++) { |
| 204 | for(i = 0; i < 2; i++) { |
| 205 | residual = (int32*)malloc(sizeof(int32) * new_size); |
| 206 | if(0 == residual) { |
| 207 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 208 | ok = 0; |
| 209 | } |
| 210 | else { |
| 211 | if(encoder->guts->residual_workspace_mid_side[channel][i] != 0) |
| 212 | free(encoder->guts->residual_workspace_mid_side[channel][i]); |
| 213 | encoder->guts->residual_workspace_mid_side[channel][i] = residual; |
| 214 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 217 | abs_residual = (uint32*)malloc(sizeof(uint32) * new_size); |
| 218 | if(0 == residual) { |
| 219 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 220 | ok = 0; |
| 221 | } |
| 222 | else { |
| 223 | if(encoder->guts->abs_residual != 0) |
| 224 | free(encoder->guts->abs_residual); |
| 225 | encoder->guts->abs_residual = abs_residual; |
| 226 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 227 | } |
| 228 | if(ok) |
| 229 | encoder->guts->input_capacity = new_size; |
| 230 | |
| 231 | return ok; |
| 232 | } |
| 233 | |
| 234 | FLAC__Encoder *FLAC__encoder_get_new_instance() |
| 235 | { |
| 236 | FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder)); |
| 237 | if(encoder != 0) { |
| 238 | encoder->state = FLAC__ENCODER_UNINITIALIZED; |
| 239 | encoder->guts = 0; |
| 240 | } |
| 241 | return encoder; |
| 242 | } |
| 243 | |
| 244 | void FLAC__encoder_free_instance(FLAC__Encoder *encoder) |
| 245 | { |
| 246 | assert(encoder != 0); |
| 247 | free(encoder); |
| 248 | } |
| 249 | |
| 250 | FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data), void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data), void *client_data) |
| 251 | { |
| 252 | unsigned i; |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 253 | FLAC__StreamMetaData padding; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 254 | |
| 255 | assert(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
| 256 | assert(encoder != 0); |
| 257 | assert(write_callback != 0); |
| 258 | assert(metadata_callback != 0); |
| 259 | assert(encoder->state == FLAC__ENCODER_UNINITIALIZED); |
| 260 | assert(encoder->guts == 0); |
| 261 | |
| 262 | encoder->state = FLAC__ENCODER_OK; |
| 263 | |
| 264 | if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS) |
| 265 | return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS; |
| 266 | |
| 267 | if(encoder->do_mid_side_stereo && encoder->channels != 2) |
| 268 | return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH; |
| 269 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 270 | if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo) |
Josh Coalson | 69f1ee0 | 2001-01-24 00:54:43 +0000 | [diff] [blame] | 271 | return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE; |
| 272 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 273 | if(encoder->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 274 | return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE; |
| 275 | |
| 276 | if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE) |
| 277 | return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE; |
| 278 | |
| 279 | if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE) |
| 280 | return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE; |
| 281 | |
| 282 | if(encoder->blocksize < encoder->max_lpc_order) |
| 283 | return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER; |
| 284 | |
| 285 | if(encoder->qlp_coeff_precision == 0) { |
| 286 | if(encoder->bits_per_sample < 16) { |
| 287 | /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */ |
| 288 | /* @@@ until then we'll make a guess */ |
| 289 | encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2); |
| 290 | } |
| 291 | else if(encoder->bits_per_sample == 16) { |
| 292 | if(encoder->blocksize <= 192) |
| 293 | encoder->qlp_coeff_precision = 7; |
| 294 | else if(encoder->blocksize <= 384) |
| 295 | encoder->qlp_coeff_precision = 8; |
| 296 | else if(encoder->blocksize <= 576) |
| 297 | encoder->qlp_coeff_precision = 9; |
| 298 | else if(encoder->blocksize <= 1152) |
| 299 | encoder->qlp_coeff_precision = 10; |
| 300 | else if(encoder->blocksize <= 2304) |
| 301 | encoder->qlp_coeff_precision = 11; |
| 302 | else if(encoder->blocksize <= 4608) |
| 303 | encoder->qlp_coeff_precision = 12; |
| 304 | else |
| 305 | encoder->qlp_coeff_precision = 13; |
| 306 | } |
| 307 | else { |
| 308 | encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1); |
| 309 | } |
| 310 | } |
Josh Coalson | 0552fdf | 2001-02-26 20:29:56 +0000 | [diff] [blame] | 311 | else if(encoder->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->qlp_coeff_precision + encoder->bits_per_sample >= 8*sizeof(uint32) || encoder->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 312 | return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION; |
| 313 | |
| 314 | if(encoder->streamable_subset) { |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 315 | //@@@ add check for blocksize here |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 316 | if(encoder->bits_per_sample != 8 && encoder->bits_per_sample != 12 && encoder->bits_per_sample != 16 && encoder->bits_per_sample != 20 && encoder->bits_per_sample != 24) |
| 317 | return encoder->state = FLAC__ENCODER_NOT_STREAMABLE; |
| 318 | if(encoder->sample_rate > 655350) |
| 319 | return encoder->state = FLAC__ENCODER_NOT_STREAMABLE; |
| 320 | } |
| 321 | |
| 322 | if(encoder->rice_optimization_level >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 323 | encoder->rice_optimization_level = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1; |
| 324 | |
| 325 | encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate)); |
| 326 | if(encoder->guts == 0) |
| 327 | return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 328 | |
| 329 | encoder->guts->input_capacity = 0; |
| 330 | for(i = 0; i < encoder->channels; i++) { |
| 331 | encoder->guts->integer_signal[i] = 0; |
| 332 | encoder->guts->real_signal[i] = 0; |
| 333 | } |
| 334 | for(i = 0; i < 2; i++) { |
| 335 | encoder->guts->integer_signal_mid_side[i] = 0; |
| 336 | encoder->guts->real_signal_mid_side[i] = 0; |
| 337 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 338 | for(i = 0; i < encoder->channels; i++) { |
| 339 | encoder->guts->residual_workspace[i][0] = encoder->guts->residual_workspace[i][1] = 0; |
| 340 | encoder->guts->best_subframe[i] = 0; |
| 341 | } |
| 342 | for(i = 0; i < 2; i++) { |
| 343 | encoder->guts->residual_workspace_mid_side[i][0] = encoder->guts->residual_workspace_mid_side[i][1] = 0; |
| 344 | encoder->guts->best_subframe_mid_side[i] = 0; |
| 345 | } |
| 346 | for(i = 0; i < encoder->channels; i++) { |
| 347 | encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0]; |
| 348 | encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1]; |
| 349 | } |
| 350 | for(i = 0; i < 2; i++) { |
| 351 | encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0]; |
| 352 | encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1]; |
| 353 | } |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 354 | encoder->guts->abs_residual = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 355 | encoder->guts->current_frame_can_do_mid_side = true; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 356 | encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize; |
| 357 | encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5); |
| 358 | if(encoder->guts->loose_mid_side_stereo_frames == 0) |
| 359 | encoder->guts->loose_mid_side_stereo_frames = 1; |
| 360 | encoder->guts->loose_mid_side_stereo_frame_count = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 361 | encoder->guts->current_sample_number = 0; |
| 362 | encoder->guts->current_frame_number = 0; |
| 363 | |
| 364 | if(!encoder_resize_buffers_(encoder, encoder->blocksize)) { |
| 365 | /* the above function sets the state for us in case of an error */ |
| 366 | return encoder->state; |
| 367 | } |
| 368 | FLAC__bitbuffer_init(&encoder->guts->frame); |
| 369 | encoder->guts->write_callback = write_callback; |
| 370 | encoder->guts->metadata_callback = metadata_callback; |
| 371 | encoder->guts->client_data = client_data; |
| 372 | |
| 373 | /* |
| 374 | * write the stream header |
| 375 | */ |
| 376 | if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) |
| 377 | return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 378 | |
| 379 | if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) |
| 380 | return encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 381 | |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 382 | encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO; |
| 383 | encoder->guts->metadata.is_last = (encoder->padding == 0); |
| 384 | encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 385 | encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */ |
| 386 | encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize; |
| 387 | encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 388 | encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 389 | encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate; |
| 390 | encoder->guts->metadata.data.stream_info.channels = encoder->channels; |
| 391 | encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample; |
| 392 | encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */ |
| 393 | memset(encoder->guts->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */ |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 394 | MD5Init(&encoder->guts->md5context); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 395 | if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame)) |
| 396 | return encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 397 | |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 398 | /* add a PADDING block if requested */ |
| 399 | if(encoder->padding > 0) { |
| 400 | padding.type = FLAC__METADATA_TYPE_PADDING; |
| 401 | padding.is_last = true; |
| 402 | padding.length = encoder->padding; |
| 403 | if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame)) |
| 404 | return encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 405 | } |
| 406 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 407 | assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */ |
| 408 | assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */ |
| 409 | if(encoder->guts->write_callback(encoder, encoder->guts->frame.buffer, encoder->guts->frame.bytes, 0, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK) |
| 410 | return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING; |
| 411 | |
Josh Coalson | cbbbb5f | 2001-01-23 00:41:48 +0000 | [diff] [blame] | 412 | /* now that the metadata block is written, we can init this to an absurdly-high value... */ |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 413 | encoder->guts->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1; |
Josh Coalson | cbbbb5f | 2001-01-23 00:41:48 +0000 | [diff] [blame] | 414 | /* ... and clear this to 0 */ |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 415 | encoder->guts->metadata.data.stream_info.total_samples = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 416 | |
| 417 | return encoder->state; |
| 418 | } |
| 419 | |
| 420 | void FLAC__encoder_finish(FLAC__Encoder *encoder) |
| 421 | { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 422 | unsigned i, channel; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 423 | |
| 424 | assert(encoder != 0); |
| 425 | if(encoder->state == FLAC__ENCODER_UNINITIALIZED) |
| 426 | return; |
| 427 | if(encoder->guts->current_sample_number != 0) { |
| 428 | encoder->blocksize = encoder->guts->current_sample_number; |
| 429 | encoder_process_frame_(encoder, true); /* true => is last frame */ |
| 430 | } |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 431 | MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 432 | encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data); |
| 433 | if(encoder->guts != 0) { |
| 434 | for(i = 0; i < encoder->channels; i++) { |
| 435 | if(encoder->guts->integer_signal[i] != 0) { |
| 436 | free(encoder->guts->integer_signal[i]); |
| 437 | encoder->guts->integer_signal[i] = 0; |
| 438 | } |
| 439 | if(encoder->guts->real_signal[i] != 0) { |
| 440 | free(encoder->guts->real_signal[i]); |
| 441 | encoder->guts->real_signal[i] = 0; |
| 442 | } |
| 443 | } |
| 444 | for(i = 0; i < 2; i++) { |
| 445 | if(encoder->guts->integer_signal_mid_side[i] != 0) { |
| 446 | free(encoder->guts->integer_signal_mid_side[i]); |
| 447 | encoder->guts->integer_signal_mid_side[i] = 0; |
| 448 | } |
| 449 | if(encoder->guts->real_signal_mid_side[i] != 0) { |
| 450 | free(encoder->guts->real_signal_mid_side[i]); |
| 451 | encoder->guts->real_signal_mid_side[i] = 0; |
| 452 | } |
| 453 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 454 | for(channel = 0; channel < encoder->channels; channel++) { |
| 455 | for(i = 0; i < 2; i++) { |
| 456 | if(encoder->guts->residual_workspace[channel][i] != 0) { |
| 457 | free(encoder->guts->residual_workspace[channel][i]); |
| 458 | encoder->guts->residual_workspace[channel][i] = 0; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | for(channel = 0; channel < 2; channel++) { |
| 463 | for(i = 0; i < 2; i++) { |
| 464 | if(encoder->guts->residual_workspace_mid_side[channel][i] != 0) { |
| 465 | free(encoder->guts->residual_workspace_mid_side[channel][i]); |
| 466 | encoder->guts->residual_workspace_mid_side[channel][i] = 0; |
| 467 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 470 | if(encoder->guts->abs_residual != 0) { |
| 471 | free(encoder->guts->abs_residual); |
| 472 | encoder->guts->abs_residual = 0; |
| 473 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 474 | FLAC__bitbuffer_free(&encoder->guts->frame); |
| 475 | free(encoder->guts); |
| 476 | encoder->guts = 0; |
| 477 | } |
| 478 | encoder->state = FLAC__ENCODER_UNINITIALIZED; |
| 479 | } |
| 480 | |
| 481 | bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples) |
| 482 | { |
| 483 | unsigned i, j, channel; |
| 484 | int32 x, mid, side; |
| 485 | const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2; |
| 486 | const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1)); |
| 487 | const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1; |
| 488 | |
| 489 | assert(encoder != 0); |
| 490 | assert(encoder->state == FLAC__ENCODER_OK); |
| 491 | |
| 492 | j = 0; |
| 493 | do { |
| 494 | for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) { |
| 495 | for(channel = 0; channel < encoder->channels; channel++) { |
| 496 | x = buf[channel][j]; |
| 497 | encoder->guts->integer_signal[channel][i] = x; |
| 498 | encoder->guts->real_signal[channel][i] = (real)x; |
| 499 | } |
| 500 | if(ms && encoder->guts->current_frame_can_do_mid_side) { |
| 501 | side = buf[0][j] - buf[1][j]; |
| 502 | if(side < min_side || side > max_side) { |
| 503 | encoder->guts->current_frame_can_do_mid_side = false; |
| 504 | } |
| 505 | else { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 506 | mid = (buf[0][j] + buf[1][j]) >> 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 507 | encoder->guts->integer_signal_mid_side[0][i] = mid; |
| 508 | encoder->guts->integer_signal_mid_side[1][i] = side; |
| 509 | encoder->guts->real_signal_mid_side[0][i] = (real)mid; |
| 510 | encoder->guts->real_signal_mid_side[1][i] = (real)side; |
| 511 | } |
| 512 | } |
| 513 | encoder->guts->current_sample_number++; |
| 514 | } |
| 515 | if(i == encoder->blocksize) { |
| 516 | if(!encoder_process_frame_(encoder, false)) /* false => not last frame */ |
| 517 | return false; |
| 518 | } |
| 519 | } while(j < samples); |
| 520 | |
| 521 | return true; |
| 522 | } |
| 523 | |
| 524 | /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */ |
| 525 | bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples) |
| 526 | { |
| 527 | unsigned i, j, k, channel; |
| 528 | int32 x, left = 0, mid, side; |
| 529 | const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2; |
| 530 | const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1)); |
| 531 | const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1; |
| 532 | |
| 533 | assert(encoder != 0); |
| 534 | assert(encoder->state == FLAC__ENCODER_OK); |
| 535 | |
| 536 | j = k = 0; |
| 537 | do { |
| 538 | for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) { |
| 539 | for(channel = 0; channel < encoder->channels; channel++, k++) { |
| 540 | x = buf[k]; |
| 541 | encoder->guts->integer_signal[channel][i] = x; |
| 542 | encoder->guts->real_signal[channel][i] = (real)x; |
| 543 | if(ms && encoder->guts->current_frame_can_do_mid_side) { |
| 544 | if(channel == 0) { |
| 545 | left = x; |
| 546 | } |
| 547 | else { |
| 548 | side = left - x; |
| 549 | if(side < min_side || side > max_side) { |
| 550 | encoder->guts->current_frame_can_do_mid_side = false; |
| 551 | } |
| 552 | else { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 553 | mid = (left + x) >> 1; /* NOTE: not the same as 'mid = (left + x) / 2' ! */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 554 | encoder->guts->integer_signal_mid_side[0][i] = mid; |
| 555 | encoder->guts->integer_signal_mid_side[1][i] = side; |
| 556 | encoder->guts->real_signal_mid_side[0][i] = (real)mid; |
| 557 | encoder->guts->real_signal_mid_side[1][i] = (real)side; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | encoder->guts->current_sample_number++; |
| 563 | } |
| 564 | if(i == encoder->blocksize) { |
| 565 | if(!encoder_process_frame_(encoder, false)) /* false => not last frame */ |
| 566 | return false; |
| 567 | } |
| 568 | } while(j < samples); |
| 569 | |
| 570 | return true; |
| 571 | } |
| 572 | |
| 573 | bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame) |
| 574 | { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 575 | assert(encoder->state == FLAC__ENCODER_OK); |
| 576 | |
| 577 | /* |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 578 | * Accumulate raw signal to the MD5 signature |
| 579 | */ |
| 580 | if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) { |
| 581 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 586 | * Process the frame header and subframes into the frame bitbuffer |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 587 | */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 588 | if(!encoder_process_subframes_(encoder, is_last_frame)) { |
| 589 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 590 | return false; |
| 591 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 592 | |
| 593 | /* |
| 594 | * Zero-pad the frame to a byte_boundary |
| 595 | */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 596 | if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 597 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | /* |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 602 | * CRC-16 the whole thing |
| 603 | */ |
| 604 | assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */ |
| 605 | assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */ |
| 606 | FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN); |
| 607 | |
| 608 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 609 | * Write it |
| 610 | */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 611 | if(encoder->guts->write_callback(encoder, encoder->guts->frame.buffer, encoder->guts->frame.bytes, encoder->blocksize, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 612 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING; |
| 613 | return false; |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Get ready for the next frame |
| 618 | */ |
| 619 | encoder->guts->current_frame_can_do_mid_side = true; |
| 620 | encoder->guts->current_sample_number = 0; |
| 621 | encoder->guts->current_frame_number++; |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 622 | encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize; |
| 623 | encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize); |
| 624 | encoder->guts->metadata.data.stream_info.max_framesize = max(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.max_framesize); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 625 | |
| 626 | return true; |
| 627 | } |
| 628 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 629 | bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame) |
| 630 | { |
| 631 | FLAC__FrameHeader frame_header; |
| 632 | unsigned channel, max_partition_order; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 633 | bool do_independent, do_mid_side; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 634 | |
| 635 | /* |
| 636 | * Calculate the max Rice partition order |
| 637 | */ |
| 638 | if(is_last_frame) { |
| 639 | max_partition_order = 0; |
| 640 | } |
| 641 | else { |
| 642 | unsigned limit = 0, b = encoder->blocksize; |
| 643 | while(!(b & 1)) { |
| 644 | limit++; |
| 645 | b >>= 1; |
| 646 | } |
| 647 | max_partition_order = min(encoder->rice_optimization_level, limit); |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Setup the frame |
| 652 | */ |
| 653 | if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) { |
| 654 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 655 | return false; |
| 656 | } |
| 657 | frame_header.blocksize = encoder->blocksize; |
| 658 | frame_header.sample_rate = encoder->sample_rate; |
| 659 | frame_header.channels = encoder->channels; |
| 660 | frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */ |
| 661 | frame_header.bits_per_sample = encoder->bits_per_sample; |
| 662 | frame_header.number.frame_number = encoder->guts->current_frame_number; |
| 663 | |
| 664 | /* |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 665 | * Figure out what channel assignments to try |
| 666 | */ |
| 667 | if(encoder->do_mid_side_stereo) { |
| 668 | if(encoder->loose_mid_side_stereo) { |
| 669 | if(encoder->guts->loose_mid_side_stereo_frame_count == 0) { |
| 670 | do_independent = true; |
| 671 | do_mid_side = true; |
| 672 | } |
| 673 | else { |
| 674 | do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT); |
| 675 | do_mid_side = !do_independent; |
| 676 | } |
| 677 | } |
| 678 | else { |
| 679 | do_independent = true; |
| 680 | do_mid_side = true; |
| 681 | } |
| 682 | } |
| 683 | else { |
| 684 | do_independent = true; |
| 685 | do_mid_side = false; |
| 686 | } |
| 687 | if(do_mid_side && !encoder->guts->current_frame_can_do_mid_side) { |
| 688 | do_independent = true; |
| 689 | do_mid_side = false; |
| 690 | } |
| 691 | |
| 692 | assert(do_independent || do_mid_side); |
| 693 | |
| 694 | /* |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 695 | * Check for wasted bits; set effective bps for each subframe |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 696 | */ |
| 697 | if(do_independent) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 698 | unsigned w; |
| 699 | for(channel = 0; channel < encoder->channels; channel++) { |
| 700 | w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize); |
| 701 | encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w; |
| 702 | encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w; |
| 703 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 704 | } |
| 705 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 706 | unsigned w; |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 707 | assert(encoder->channels == 2); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 708 | for(channel = 0; channel < 2; channel++) { |
| 709 | w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize); |
| 710 | encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w; |
| 711 | encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1); |
| 712 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 716 | * First do a normal encoding pass of each independent channel |
| 717 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 718 | if(do_independent) { |
| 719 | for(channel = 0; channel < encoder->channels; channel++) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 720 | 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)) |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 721 | return false; |
| 722 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /* |
| 726 | * Now do mid and side channels if requested |
| 727 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 728 | if(do_mid_side) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 729 | assert(encoder->channels == 2); |
| 730 | |
| 731 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 732 | 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)) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 733 | return false; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | * Compose the frame bitbuffer |
| 739 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 740 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 741 | unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ |
| 742 | FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 743 | FLAC__ChannelAssignment channel_assignment; |
| 744 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 745 | assert(encoder->channels == 2); |
| 746 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 747 | if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) { |
| 748 | channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE); |
| 749 | } |
| 750 | else { |
| 751 | unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */ |
| 752 | unsigned min_bits; |
| 753 | FLAC__ChannelAssignment ca; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 754 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 755 | assert(do_independent && do_mid_side); |
| 756 | |
| 757 | /* We have to figure out which channel assignent results in the smallest frame */ |
| 758 | bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1]; |
| 759 | bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1]; |
| 760 | bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1]; |
| 761 | bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1]; |
| 762 | |
| 763 | for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) { |
| 764 | if(bits[ca] < min_bits) { |
| 765 | min_bits = bits[ca]; |
| 766 | channel_assignment = ca; |
| 767 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 771 | frame_header.channel_assignment = channel_assignment; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 772 | |
| 773 | if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) { |
| 774 | encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 775 | return false; |
| 776 | } |
| 777 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 778 | switch(channel_assignment) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 779 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 780 | left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]]; |
| 781 | right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 782 | break; |
| 783 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 784 | left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]]; |
| 785 | right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 786 | break; |
| 787 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 788 | left_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]]; |
| 789 | right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 790 | break; |
| 791 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 792 | left_subframe = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]]; |
| 793 | right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 794 | break; |
| 795 | default: |
| 796 | assert(0); |
| 797 | } |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 798 | |
| 799 | switch(channel_assignment) { |
| 800 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 801 | left_bps = encoder->guts->subframe_bps [0]; |
| 802 | right_bps = encoder->guts->subframe_bps [1]; |
| 803 | break; |
| 804 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
| 805 | left_bps = encoder->guts->subframe_bps [0]; |
| 806 | right_bps = encoder->guts->subframe_bps_mid_side[1]; |
| 807 | break; |
| 808 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
| 809 | left_bps = encoder->guts->subframe_bps_mid_side[1]; |
| 810 | right_bps = encoder->guts->subframe_bps [1]; |
| 811 | break; |
| 812 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
| 813 | left_bps = encoder->guts->subframe_bps_mid_side[0]; |
| 814 | right_bps = encoder->guts->subframe_bps_mid_side[1]; |
| 815 | break; |
| 816 | default: |
| 817 | assert(0); |
| 818 | } |
| 819 | |
| 820 | /* note that encoder_add_subframe_ sets the state for us in case of an error */ |
| 821 | if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame)) |
| 822 | return false; |
| 823 | if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame)) |
| 824 | return false; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 825 | } |
| 826 | else { |
| 827 | if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) { |
| 828 | encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 829 | return false; |
| 830 | } |
| 831 | |
| 832 | for(channel = 0; channel < encoder->channels; channel++) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 833 | 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)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 834 | /* the above function sets the state for us in case of an error */ |
| 835 | return false; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 840 | if(encoder->loose_mid_side_stereo) { |
| 841 | encoder->guts->loose_mid_side_stereo_frame_count++; |
| 842 | if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames) |
| 843 | encoder->guts->loose_mid_side_stereo_frame_count = 0; |
| 844 | } |
| 845 | |
| 846 | encoder->guts->last_channel_assignment = frame_header.channel_assignment; |
| 847 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 848 | return true; |
| 849 | } |
| 850 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 851 | 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) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 852 | { |
| 853 | real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 854 | real lpc_residual_bits_per_sample; |
| 855 | real autoc[FLAC__MAX_LPC_ORDER+1]; |
| 856 | real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; |
| 857 | real lpc_error[FLAC__MAX_LPC_ORDER]; |
| 858 | unsigned min_lpc_order, max_lpc_order, lpc_order; |
| 859 | unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order; |
| 860 | unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision; |
| 861 | unsigned rice_parameter; |
| 862 | unsigned _candidate_bits, _best_bits; |
| 863 | unsigned _best_subframe; |
| 864 | |
| 865 | /* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 866 | _best_subframe = 0; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 867 | _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 868 | |
| 869 | if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { |
| 870 | /* check for constant subframe */ |
| 871 | guess_fixed_order = FLAC__fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample); |
| 872 | if(fixed_residual_bits_per_sample[1] == 0.0) { |
| 873 | /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */ |
| 874 | unsigned i, signal_is_constant = true; |
| 875 | for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) { |
| 876 | if(integer_signal[0] != integer_signal[i]) { |
| 877 | signal_is_constant = false; |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | if(signal_is_constant) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 882 | _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 883 | if(_candidate_bits < _best_bits) { |
| 884 | _best_subframe = !_best_subframe; |
| 885 | _best_bits = _candidate_bits; |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | else { |
| 890 | /* encode fixed */ |
| 891 | if(encoder->do_exhaustive_model_search) { |
| 892 | min_fixed_order = 0; |
| 893 | max_fixed_order = FLAC__MAX_FIXED_ORDER; |
| 894 | } |
| 895 | else { |
| 896 | min_fixed_order = max_fixed_order = guess_fixed_order; |
| 897 | } |
| 898 | for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 899 | if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 900 | continue; /* don't even try */ |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 901 | 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 */ |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 902 | #ifndef SYMMETRIC_RICE |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 903 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 904 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 905 | if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)) |
| 906 | rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 907 | _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]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 908 | if(_candidate_bits < _best_bits) { |
| 909 | _best_subframe = !_best_subframe; |
| 910 | _best_bits = _candidate_bits; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | /* encode lpc */ |
| 915 | if(encoder->max_lpc_order > 0) { |
| 916 | if(encoder->max_lpc_order >= frame_header->blocksize) |
| 917 | max_lpc_order = frame_header->blocksize-1; |
| 918 | else |
| 919 | max_lpc_order = encoder->max_lpc_order; |
| 920 | if(max_lpc_order > 0) { |
| 921 | FLAC__lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 922 | /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */ |
| 923 | if(autoc[0] != 0.0) { |
| 924 | FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error); |
| 925 | if(encoder->do_exhaustive_model_search) { |
| 926 | min_lpc_order = 1; |
| 927 | } |
| 928 | else { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 929 | unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 930 | min_lpc_order = max_lpc_order = guess_lpc_order; |
| 931 | } |
| 932 | if(encoder->do_qlp_coeff_prec_search) { |
| 933 | min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 934 | max_qlp_coeff_precision = min(32 - subframe_bps - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 935 | } |
| 936 | else { |
| 937 | min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision; |
| 938 | } |
| 939 | for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) { |
| 940 | lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 941 | if(lpc_residual_bits_per_sample >= (real)subframe_bps) |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 942 | continue; /* don't even try */ |
| 943 | rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */ |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 944 | #ifndef SYMMETRIC_RICE |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 945 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 946 | #endif |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 947 | if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)) |
| 948 | rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1; |
| 949 | for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 950 | _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]); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 951 | if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 952 | if(_candidate_bits < _best_bits) { |
| 953 | _best_subframe = !_best_subframe; |
| 954 | _best_bits = _candidate_bits; |
| 955 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | *best_subframe = _best_subframe; |
| 966 | *best_bits = _best_bits; |
| 967 | |
| 968 | return true; |
| 969 | } |
| 970 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 971 | bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 972 | { |
| 973 | switch(subframe->type) { |
| 974 | case FLAC__SUBFRAME_TYPE_CONSTANT: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 975 | if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 976 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 977 | return false; |
| 978 | } |
| 979 | break; |
| 980 | case FLAC__SUBFRAME_TYPE_FIXED: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 981 | if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 982 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 983 | return false; |
| 984 | } |
| 985 | break; |
| 986 | case FLAC__SUBFRAME_TYPE_LPC: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 987 | if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 988 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 989 | return false; |
| 990 | } |
| 991 | break; |
| 992 | case FLAC__SUBFRAME_TYPE_VERBATIM: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 993 | if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 994 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 995 | return false; |
| 996 | } |
| 997 | break; |
| 998 | default: |
| 999 | assert(0); |
| 1000 | } |
| 1001 | |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 1005 | unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1006 | { |
| 1007 | subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 1008 | subframe->data.constant.value = signal; |
| 1009 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 1010 | return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 1013 | 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) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1014 | { |
| 1015 | unsigned i, residual_bits; |
| 1016 | const unsigned residual_samples = blocksize - order; |
| 1017 | |
| 1018 | FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual); |
| 1019 | |
| 1020 | subframe->type = FLAC__SUBFRAME_TYPE_FIXED; |
| 1021 | |
| 1022 | subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1023 | subframe->data.fixed.residual = residual; |
| 1024 | |
| 1025 | residual_bits = encoder_find_best_partition_order_(residual, abs_residual, residual_samples, order, rice_parameter, max_partition_order, &subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters); |
| 1026 | |
| 1027 | subframe->data.fixed.order = order; |
| 1028 | for(i = 0; i < order; i++) |
| 1029 | subframe->data.fixed.warmup[i] = signal[i]; |
| 1030 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 1031 | return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 1034 | 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) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1035 | { |
| 1036 | int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; |
| 1037 | unsigned i, residual_bits; |
| 1038 | int quantization, ret; |
| 1039 | const unsigned residual_samples = blocksize - order; |
| 1040 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 1041 | ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1042 | if(ret != 0) |
| 1043 | return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ |
| 1044 | |
| 1045 | FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
| 1046 | |
| 1047 | subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 1048 | |
| 1049 | subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1050 | subframe->data.lpc.residual = residual; |
| 1051 | |
| 1052 | residual_bits = encoder_find_best_partition_order_(residual, abs_residual, residual_samples, order, rice_parameter, max_partition_order, &subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.parameters); |
| 1053 | |
| 1054 | subframe->data.lpc.order = order; |
| 1055 | subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 1056 | subframe->data.lpc.quantization_level = quantization; |
| 1057 | memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER); |
| 1058 | for(i = 0; i < order; i++) |
| 1059 | subframe->data.lpc.warmup[i] = signal[i]; |
| 1060 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 1061 | 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; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 1064 | unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1065 | { |
| 1066 | subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 1067 | |
| 1068 | subframe->data.verbatim.data = signal; |
| 1069 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame^] | 1070 | return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | 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[]) |
| 1074 | { |
| 1075 | unsigned residual_bits, best_residual_bits = 0; |
| 1076 | unsigned i, partition_order; |
| 1077 | unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER]; |
| 1078 | int32 r; |
| 1079 | |
| 1080 | /* compute the abs(residual) for use later */ |
| 1081 | for(i = 0; i < residual_samples; i++) { |
| 1082 | r = residual[i]; |
| 1083 | abs_residual[i] = (uint32)(r<0? -r : r); |
| 1084 | } |
| 1085 | |
| 1086 | for(partition_order = 0; partition_order <= max_partition_order; partition_order++) { |
Josh Coalson | 3b5f471 | 2001-03-21 22:53:43 +0000 | [diff] [blame] | 1087 | if(!encoder_set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, partition_order, parameters[!best_parameters_index], &residual_bits)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1088 | assert(best_residual_bits != 0); |
| 1089 | break; |
| 1090 | } |
| 1091 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 1092 | best_residual_bits = residual_bits; |
| 1093 | *best_partition_order = partition_order; |
| 1094 | best_parameters_index = !best_parameters_index; |
| 1095 | } |
| 1096 | } |
| 1097 | memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order))); |
| 1098 | |
| 1099 | return best_residual_bits; |
| 1100 | } |
| 1101 | |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1102 | #ifdef VARIABLE_RICE_BITS |
| 1103 | #undef VARIABLE_RICE_BITS |
| 1104 | #endif |
| 1105 | #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter)) |
| 1106 | |
Josh Coalson | 3b5f471 | 2001-03-21 22:53:43 +0000 | [diff] [blame] | 1107 | 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) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1108 | { |
| 1109 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
| 1110 | |
| 1111 | if(partition_order == 0) { |
| 1112 | unsigned i; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1113 | |
| 1114 | { |
| 1115 | #ifdef VARIABLE_RICE_BITS |
| 1116 | #ifdef SYMMETRIC_RICE |
| 1117 | bits_ += (2+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1118 | #else |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1119 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 1120 | bits_ += (1+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1121 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1122 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1123 | parameters[0] = rice_parameter; |
| 1124 | bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 1125 | for(i = 0; i < residual_samples; i++) { |
| 1126 | #ifdef VARIABLE_RICE_BITS |
| 1127 | #ifdef SYMMETRIC_RICE |
Josh Coalson | 3b5f471 | 2001-03-21 22:53:43 +0000 | [diff] [blame] | 1128 | bits_ += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1129 | #else |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1130 | bits_ += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1131 | #endif |
| 1132 | #else |
Josh Coalson | 3b5f471 | 2001-03-21 22:53:43 +0000 | [diff] [blame] | 1133 | bits_ += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE we will need to pass in residual[] instead of abs_residual[] */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1134 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1135 | } |
| 1136 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1137 | } |
| 1138 | else { |
| 1139 | unsigned i, j, k = 0, k_last = 0; |
| 1140 | unsigned mean, parameter, partition_samples; |
| 1141 | const unsigned max_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1; |
| 1142 | for(i = 0; i < (1u<<partition_order); i++) { |
| 1143 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
| 1144 | if(i == 0) { |
| 1145 | if(partition_samples <= predictor_order) |
| 1146 | return false; |
| 1147 | else |
| 1148 | partition_samples -= predictor_order; |
| 1149 | } |
| 1150 | mean = partition_samples >> 1; |
| 1151 | for(j = 0; j < partition_samples; j++, k++) |
| 1152 | mean += abs_residual[k]; |
| 1153 | mean /= partition_samples; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1154 | #ifdef SYMMETRIC_RICE |
| 1155 | /* calc parameter = floor(log2(mean)) */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1156 | parameter = 0; |
| 1157 | mean>>=1; |
| 1158 | while(mean) { |
| 1159 | parameter++; |
| 1160 | mean >>= 1; |
| 1161 | } |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1162 | #else |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1163 | /* calc parameter = floor(log2(mean)) + 1 */ |
| 1164 | parameter = 0; |
| 1165 | while(mean) { |
| 1166 | parameter++; |
| 1167 | mean >>= 1; |
| 1168 | } |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1169 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1170 | if(parameter > max_parameter) |
| 1171 | parameter = max_parameter; |
| 1172 | parameters[i] = parameter; |
| 1173 | bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1174 | #ifdef VARIABLE_RICE_BITS |
| 1175 | #ifdef SYMMETRIC_RICE |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1176 | bits_ += (2+parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1177 | #else |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1178 | bits_ += (1+parameter) * partition_samples; |
| 1179 | --parameter; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1180 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1181 | #endif |
| 1182 | for(j = k_last; j < k; j++) |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1183 | #ifdef VARIABLE_RICE_BITS |
| 1184 | #ifdef SYMMETRIC_RICE |
| 1185 | bits_ += VARIABLE_RICE_BITS(abs_residual[j], parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1186 | #else |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1187 | bits_ += VARIABLE_RICE_BITS(abs_residual[j], parameter); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1188 | #endif |
| 1189 | #else |
Josh Coalson | 3b5f471 | 2001-03-21 22:53:43 +0000 | [diff] [blame] | 1190 | bits_ += FLAC__bitbuffer_rice_bits(residual[j], parameter); /* NOTE we will need to pass in residual[] instead of abs_residual[] */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1191 | #endif |
| 1192 | k_last = k; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | *bits = bits_; |
| 1197 | return true; |
| 1198 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1199 | |
| 1200 | static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples) |
| 1201 | { |
| 1202 | unsigned i, shift; |
| 1203 | int32 x = 0; |
| 1204 | |
| 1205 | for(i = 0; i < samples && !(x&1); i++) |
| 1206 | x |= signal[i]; |
| 1207 | |
| 1208 | if(x == 0) { |
| 1209 | shift = 0; |
| 1210 | } |
| 1211 | else { |
| 1212 | for(shift = 0; !(x&1); shift++) |
| 1213 | x >>= 1; |
| 1214 | } |
| 1215 | |
| 1216 | if(shift > 0) { |
| 1217 | for(i = 0; i < samples; i++) |
| 1218 | signal[i] >>= shift; |
| 1219 | } |
| 1220 | |
| 1221 | return shift; |
| 1222 | } |