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