Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Coder 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" |
| 26 | #include "private/encoder_framing.h" |
| 27 | #include "private/fixed.h" |
| 28 | #include "private/lpc.h" |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 29 | #include "private/md5.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 30 | |
| 31 | #ifdef min |
| 32 | #undef min |
| 33 | #endif |
| 34 | #define min(x,y) ((x)<(y)?(x):(y)) |
| 35 | |
| 36 | #ifdef max |
| 37 | #undef max |
| 38 | #endif |
| 39 | #define max(x,y) ((x)>(y)?(x):(y)) |
| 40 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 41 | typedef struct FLAC__EncoderPrivate { |
| 42 | unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */ |
| 43 | int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */ |
| 44 | int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */ |
| 45 | real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */ |
| 46 | real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */ |
| 47 | int32 *residual[2]; /* where the candidate and best subframe residual signals will be stored */ |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 48 | uint32 *abs_residual; /* workspace where the abs(candidate residual) is stored */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 49 | unsigned best_residual; /* index into the above */ |
| 50 | FLAC__BitBuffer frame; /* the current frame being worked on */ |
| 51 | FLAC__BitBuffer frame_mid_side; /* special parallel workspace for the mid-side coded version of the current frame */ |
| 52 | FLAC__BitBuffer frame_left_side; /* special parallel workspace for the left-side coded version of the current frame */ |
| 53 | FLAC__BitBuffer frame_right_side; /* special parallel workspace for the right-side coded version of the current frame */ |
| 54 | FLAC__SubframeHeader best_subframe, candidate_subframe; |
| 55 | bool current_frame_can_do_mid_side; /* encoder sets this false when any given sample of a frame's side channel exceeds 16 bits */ |
| 56 | FLAC__StreamMetaData metadata; |
| 57 | unsigned current_sample_number; |
| 58 | unsigned current_frame_number; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 59 | struct MD5Context md5context; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 60 | FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data); |
| 61 | void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data); |
| 62 | void *client_data; |
| 63 | } FLAC__EncoderPrivate; |
| 64 | |
| 65 | static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size); |
| 66 | static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame); |
| 67 | static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame, const FLAC__FrameHeader *frame_header, unsigned channels, const int32 *integer_signal[], const real *real_signal[], FLAC__BitBuffer *bitbuffer); |
| 68 | static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__SubframeHeader *subframe); |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 69 | static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__SubframeHeader *subframe); |
| 70 | static unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], const real lp_coeff[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned max_partition_order, FLAC__SubframeHeader *subframe); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 71 | static unsigned encoder_evaluate_verbatim_subframe_(unsigned blocksize, unsigned bits_per_sample, FLAC__SubframeHeader *subframe); |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 72 | 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 | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 73 | static bool encoder_generate_constant_subframe_(const FLAC__SubframeHeader *header, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer); |
| 74 | static bool encoder_generate_fixed_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer); |
| 75 | static bool encoder_generate_lpc_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer); |
| 76 | static bool encoder_generate_verbatim_subframe_(const FLAC__SubframeHeader *header, const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer); |
| 77 | static void encoder_promote_candidate_subframe_(FLAC__Encoder *encoder); |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 78 | 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 | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 79 | |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 80 | const char *FLAC__EncoderWriteStatusString[] = { |
| 81 | "FLAC__ENCODER_WRITE_OK", |
| 82 | "FLAC__ENCODER_WRITE_FATAL_ERROR" |
| 83 | }; |
| 84 | |
| 85 | const char *FLAC__EncoderStateString[] = { |
| 86 | "FLAC__ENCODER_OK", |
| 87 | "FLAC__ENCODER_UNINITIALIZED", |
| 88 | "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS", |
| 89 | "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE", |
| 90 | "FLAC__ENCODER_INVALID_SAMPLE_RATE", |
| 91 | "FLAC__ENCODER_INVALID_BLOCK_SIZE", |
| 92 | "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION", |
| 93 | "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH", |
| 94 | "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH", |
| 95 | "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER", |
| 96 | "FLAC__ENCODER_NOT_STREAMABLE", |
| 97 | "FLAC__ENCODER_FRAMING_ERROR", |
| 98 | "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING", |
| 99 | "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING", |
| 100 | "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR" |
| 101 | }; |
| 102 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 103 | |
| 104 | bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size) |
| 105 | { |
| 106 | bool ok; |
| 107 | unsigned i; |
| 108 | int32 *previous_is, *current_is; |
| 109 | real *previous_rs, *current_rs; |
| 110 | int32 *residual; |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 111 | uint32 *abs_residual; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 112 | |
| 113 | assert(new_size > 0); |
| 114 | assert(encoder->state == FLAC__ENCODER_OK); |
| 115 | assert(encoder->guts->current_sample_number == 0); |
| 116 | |
| 117 | /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
| 118 | if(new_size <= encoder->guts->input_capacity) |
| 119 | return true; |
| 120 | |
| 121 | ok = 1; |
| 122 | if(ok) { |
| 123 | for(i = 0; ok && i < encoder->channels; i++) { |
| 124 | /* integer version of the signal */ |
| 125 | previous_is = encoder->guts->integer_signal[i]; |
| 126 | current_is = (int32*)malloc(sizeof(int32) * new_size); |
| 127 | if(0 == current_is) { |
| 128 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 129 | ok = 0; |
| 130 | } |
| 131 | else { |
| 132 | encoder->guts->integer_signal[i] = current_is; |
| 133 | if(previous_is != 0) |
| 134 | free(previous_is); |
| 135 | } |
| 136 | /* real version of the signal */ |
| 137 | previous_rs = encoder->guts->real_signal[i]; |
| 138 | current_rs = (real*)malloc(sizeof(real) * new_size); |
| 139 | if(0 == current_rs) { |
| 140 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 141 | ok = 0; |
| 142 | } |
| 143 | else { |
| 144 | encoder->guts->real_signal[i] = current_rs; |
| 145 | if(previous_rs != 0) |
| 146 | free(previous_rs); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | if(ok) { |
| 151 | for(i = 0; ok && i < 2; i++) { |
| 152 | /* integer version of the signal */ |
| 153 | previous_is = encoder->guts->integer_signal_mid_side[i]; |
| 154 | current_is = (int32*)malloc(sizeof(int32) * new_size); |
| 155 | if(0 == current_is) { |
| 156 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 157 | ok = 0; |
| 158 | } |
| 159 | else { |
| 160 | encoder->guts->integer_signal_mid_side[i] = current_is; |
| 161 | if(previous_is != 0) |
| 162 | free(previous_is); |
| 163 | } |
| 164 | /* real version of the signal */ |
| 165 | previous_rs = encoder->guts->real_signal_mid_side[i]; |
| 166 | current_rs = (real*)malloc(sizeof(real) * new_size); |
| 167 | if(0 == current_rs) { |
| 168 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 169 | ok = 0; |
| 170 | } |
| 171 | else { |
| 172 | encoder->guts->real_signal_mid_side[i] = current_rs; |
| 173 | if(previous_rs != 0) |
| 174 | free(previous_rs); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | if(ok) { |
| 179 | for(i = 0; i < 2; i++) { |
| 180 | residual = (int32*)malloc(sizeof(int32) * new_size); |
| 181 | if(0 == residual) { |
| 182 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 183 | ok = 0; |
| 184 | } |
| 185 | else { |
| 186 | if(encoder->guts->residual[i] != 0) |
| 187 | free(encoder->guts->residual[i]); |
| 188 | encoder->guts->residual[i] = residual; |
| 189 | } |
| 190 | } |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 191 | abs_residual = (uint32*)malloc(sizeof(uint32) * new_size); |
| 192 | if(0 == residual) { |
| 193 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 194 | ok = 0; |
| 195 | } |
| 196 | else { |
| 197 | if(encoder->guts->abs_residual != 0) |
| 198 | free(encoder->guts->abs_residual); |
| 199 | encoder->guts->abs_residual = abs_residual; |
| 200 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 201 | } |
| 202 | if(ok) |
| 203 | encoder->guts->input_capacity = new_size; |
| 204 | |
| 205 | return ok; |
| 206 | } |
| 207 | |
| 208 | FLAC__Encoder *FLAC__encoder_get_new_instance() |
| 209 | { |
| 210 | FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder)); |
| 211 | if(encoder != 0) { |
| 212 | encoder->state = FLAC__ENCODER_UNINITIALIZED; |
| 213 | encoder->guts = 0; |
| 214 | } |
| 215 | return encoder; |
| 216 | } |
| 217 | |
| 218 | void FLAC__encoder_free_instance(FLAC__Encoder *encoder) |
| 219 | { |
| 220 | assert(encoder != 0); |
| 221 | free(encoder); |
| 222 | } |
| 223 | |
| 224 | 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) |
| 225 | { |
| 226 | unsigned i; |
| 227 | |
| 228 | assert(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
| 229 | assert(encoder != 0); |
| 230 | assert(write_callback != 0); |
| 231 | assert(metadata_callback != 0); |
| 232 | assert(encoder->state == FLAC__ENCODER_UNINITIALIZED); |
| 233 | assert(encoder->guts == 0); |
| 234 | |
| 235 | encoder->state = FLAC__ENCODER_OK; |
| 236 | |
| 237 | if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS) |
| 238 | return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS; |
| 239 | |
| 240 | if(encoder->do_mid_side_stereo && encoder->channels != 2) |
| 241 | return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH; |
| 242 | |
| 243 | if(encoder->do_mid_side_stereo && encoder->bits_per_sample > 16) |
| 244 | return encoder->state = FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH; |
| 245 | |
| 246 | if(encoder->bits_per_sample == 0 || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE) |
| 247 | return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE; |
| 248 | |
| 249 | if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE) |
| 250 | return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE; |
| 251 | |
| 252 | if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE) |
| 253 | return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE; |
| 254 | |
| 255 | if(encoder->blocksize < encoder->max_lpc_order) |
| 256 | return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER; |
| 257 | |
| 258 | if(encoder->qlp_coeff_precision == 0) { |
| 259 | if(encoder->bits_per_sample < 16) { |
| 260 | /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */ |
| 261 | /* @@@ until then we'll make a guess */ |
| 262 | encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2); |
| 263 | } |
| 264 | else if(encoder->bits_per_sample == 16) { |
| 265 | if(encoder->blocksize <= 192) |
| 266 | encoder->qlp_coeff_precision = 7; |
| 267 | else if(encoder->blocksize <= 384) |
| 268 | encoder->qlp_coeff_precision = 8; |
| 269 | else if(encoder->blocksize <= 576) |
| 270 | encoder->qlp_coeff_precision = 9; |
| 271 | else if(encoder->blocksize <= 1152) |
| 272 | encoder->qlp_coeff_precision = 10; |
| 273 | else if(encoder->blocksize <= 2304) |
| 274 | encoder->qlp_coeff_precision = 11; |
| 275 | else if(encoder->blocksize <= 4608) |
| 276 | encoder->qlp_coeff_precision = 12; |
| 277 | else |
| 278 | encoder->qlp_coeff_precision = 13; |
| 279 | } |
| 280 | else { |
| 281 | encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1); |
| 282 | } |
| 283 | } |
| 284 | else if(encoder->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->qlp_coeff_precision + encoder->bits_per_sample >= 8*sizeof(uint32)) |
| 285 | return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION; |
| 286 | |
| 287 | if(encoder->streamable_subset) { |
| 288 | 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) |
| 289 | return encoder->state = FLAC__ENCODER_NOT_STREAMABLE; |
| 290 | if(encoder->sample_rate > 655350) |
| 291 | return encoder->state = FLAC__ENCODER_NOT_STREAMABLE; |
| 292 | } |
| 293 | |
| 294 | if(encoder->rice_optimization_level >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 295 | encoder->rice_optimization_level = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1; |
| 296 | |
| 297 | encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate)); |
| 298 | if(encoder->guts == 0) |
| 299 | return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 300 | |
| 301 | encoder->guts->input_capacity = 0; |
| 302 | for(i = 0; i < encoder->channels; i++) { |
| 303 | encoder->guts->integer_signal[i] = 0; |
| 304 | encoder->guts->real_signal[i] = 0; |
| 305 | } |
| 306 | for(i = 0; i < 2; i++) { |
| 307 | encoder->guts->integer_signal_mid_side[i] = 0; |
| 308 | encoder->guts->real_signal_mid_side[i] = 0; |
| 309 | } |
| 310 | encoder->guts->residual[0] = 0; |
| 311 | encoder->guts->residual[1] = 0; |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 312 | encoder->guts->abs_residual = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 313 | encoder->guts->best_residual = 0; |
| 314 | encoder->guts->current_frame_can_do_mid_side = true; |
| 315 | encoder->guts->current_sample_number = 0; |
| 316 | encoder->guts->current_frame_number = 0; |
| 317 | |
| 318 | if(!encoder_resize_buffers_(encoder, encoder->blocksize)) { |
| 319 | /* the above function sets the state for us in case of an error */ |
| 320 | return encoder->state; |
| 321 | } |
| 322 | FLAC__bitbuffer_init(&encoder->guts->frame); |
| 323 | encoder->guts->write_callback = write_callback; |
| 324 | encoder->guts->metadata_callback = metadata_callback; |
| 325 | encoder->guts->client_data = client_data; |
| 326 | |
| 327 | /* |
| 328 | * write the stream header |
| 329 | */ |
| 330 | if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) |
| 331 | return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 332 | |
| 333 | if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) |
| 334 | return encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 335 | |
| 336 | encoder->guts->metadata.type = FLAC__METADATA_TYPE_ENCODING; |
| 337 | encoder->guts->metadata.is_last = true; |
| 338 | encoder->guts->metadata.length = FLAC__STREAM_METADATA_ENCODING_LENGTH; |
| 339 | encoder->guts->metadata.data.encoding.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */ |
| 340 | encoder->guts->metadata.data.encoding.max_blocksize = encoder->blocksize; |
| 341 | encoder->guts->metadata.data.encoding.min_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 342 | encoder->guts->metadata.data.encoding.max_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 343 | encoder->guts->metadata.data.encoding.sample_rate = encoder->sample_rate; |
| 344 | encoder->guts->metadata.data.encoding.channels = encoder->channels; |
| 345 | encoder->guts->metadata.data.encoding.bits_per_sample = encoder->bits_per_sample; |
Josh Coalson | cbbbb5f | 2001-01-23 00:41:48 +0000 | [diff] [blame^] | 346 | encoder->guts->metadata.data.encoding.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */ |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 347 | memset(encoder->guts->metadata.data.encoding.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */ |
| 348 | MD5Init(&encoder->guts->md5context); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 349 | if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame)) |
| 350 | return encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 351 | |
| 352 | assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */ |
| 353 | assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */ |
| 354 | 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) |
| 355 | return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING; |
| 356 | |
Josh Coalson | cbbbb5f | 2001-01-23 00:41:48 +0000 | [diff] [blame^] | 357 | /* now that the metadata block is written, we can init this to an absurdly-high value... */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 358 | encoder->guts->metadata.data.encoding.min_framesize = (1u << FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN) - 1; |
Josh Coalson | cbbbb5f | 2001-01-23 00:41:48 +0000 | [diff] [blame^] | 359 | /* ... and clear this to 0 */ |
| 360 | encoder->guts->metadata.data.encoding.total_samples = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 361 | |
| 362 | return encoder->state; |
| 363 | } |
| 364 | |
| 365 | void FLAC__encoder_finish(FLAC__Encoder *encoder) |
| 366 | { |
| 367 | unsigned i; |
| 368 | |
| 369 | assert(encoder != 0); |
| 370 | if(encoder->state == FLAC__ENCODER_UNINITIALIZED) |
| 371 | return; |
| 372 | if(encoder->guts->current_sample_number != 0) { |
| 373 | encoder->blocksize = encoder->guts->current_sample_number; |
| 374 | encoder_process_frame_(encoder, true); /* true => is last frame */ |
| 375 | } |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 376 | MD5Final(encoder->guts->metadata.data.encoding.md5sum, &encoder->guts->md5context); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 377 | encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data); |
| 378 | if(encoder->guts != 0) { |
| 379 | for(i = 0; i < encoder->channels; i++) { |
| 380 | if(encoder->guts->integer_signal[i] != 0) { |
| 381 | free(encoder->guts->integer_signal[i]); |
| 382 | encoder->guts->integer_signal[i] = 0; |
| 383 | } |
| 384 | if(encoder->guts->real_signal[i] != 0) { |
| 385 | free(encoder->guts->real_signal[i]); |
| 386 | encoder->guts->real_signal[i] = 0; |
| 387 | } |
| 388 | } |
| 389 | for(i = 0; i < 2; i++) { |
| 390 | if(encoder->guts->integer_signal_mid_side[i] != 0) { |
| 391 | free(encoder->guts->integer_signal_mid_side[i]); |
| 392 | encoder->guts->integer_signal_mid_side[i] = 0; |
| 393 | } |
| 394 | if(encoder->guts->real_signal_mid_side[i] != 0) { |
| 395 | free(encoder->guts->real_signal_mid_side[i]); |
| 396 | encoder->guts->real_signal_mid_side[i] = 0; |
| 397 | } |
| 398 | } |
| 399 | for(i = 0; i < 2; i++) { |
| 400 | if(encoder->guts->residual[i] != 0) { |
| 401 | free(encoder->guts->residual[i]); |
| 402 | encoder->guts->residual[i] = 0; |
| 403 | } |
| 404 | } |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 405 | if(encoder->guts->abs_residual != 0) { |
| 406 | free(encoder->guts->abs_residual); |
| 407 | encoder->guts->abs_residual = 0; |
| 408 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 409 | FLAC__bitbuffer_free(&encoder->guts->frame); |
| 410 | free(encoder->guts); |
| 411 | encoder->guts = 0; |
| 412 | } |
| 413 | encoder->state = FLAC__ENCODER_UNINITIALIZED; |
| 414 | } |
| 415 | |
| 416 | bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples) |
| 417 | { |
| 418 | unsigned i, j, channel; |
| 419 | int32 x, mid, side; |
| 420 | const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2; |
| 421 | const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1)); |
| 422 | const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1; |
| 423 | |
| 424 | assert(encoder != 0); |
| 425 | assert(encoder->state == FLAC__ENCODER_OK); |
| 426 | |
| 427 | j = 0; |
| 428 | do { |
| 429 | for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) { |
| 430 | for(channel = 0; channel < encoder->channels; channel++) { |
| 431 | x = buf[channel][j]; |
| 432 | encoder->guts->integer_signal[channel][i] = x; |
| 433 | encoder->guts->real_signal[channel][i] = (real)x; |
| 434 | } |
| 435 | if(ms && encoder->guts->current_frame_can_do_mid_side) { |
| 436 | side = buf[0][j] - buf[1][j]; |
| 437 | if(side < min_side || side > max_side) { |
| 438 | encoder->guts->current_frame_can_do_mid_side = false; |
| 439 | } |
| 440 | else { |
| 441 | mid = (buf[0][j] + buf[1][j]) >> 1; /* NOTE: not the same as divide-by-two ! */ |
| 442 | encoder->guts->integer_signal_mid_side[0][i] = mid; |
| 443 | encoder->guts->integer_signal_mid_side[1][i] = side; |
| 444 | encoder->guts->real_signal_mid_side[0][i] = (real)mid; |
| 445 | encoder->guts->real_signal_mid_side[1][i] = (real)side; |
| 446 | } |
| 447 | } |
| 448 | encoder->guts->current_sample_number++; |
| 449 | } |
| 450 | if(i == encoder->blocksize) { |
| 451 | if(!encoder_process_frame_(encoder, false)) /* false => not last frame */ |
| 452 | return false; |
| 453 | } |
| 454 | } while(j < samples); |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */ |
| 460 | bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples) |
| 461 | { |
| 462 | unsigned i, j, k, channel; |
| 463 | int32 x, left = 0, mid, side; |
| 464 | const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2; |
| 465 | const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1)); |
| 466 | const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1; |
| 467 | |
| 468 | assert(encoder != 0); |
| 469 | assert(encoder->state == FLAC__ENCODER_OK); |
| 470 | |
| 471 | j = k = 0; |
| 472 | do { |
| 473 | for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) { |
| 474 | for(channel = 0; channel < encoder->channels; channel++, k++) { |
| 475 | x = buf[k]; |
| 476 | encoder->guts->integer_signal[channel][i] = x; |
| 477 | encoder->guts->real_signal[channel][i] = (real)x; |
| 478 | if(ms && encoder->guts->current_frame_can_do_mid_side) { |
| 479 | if(channel == 0) { |
| 480 | left = x; |
| 481 | } |
| 482 | else { |
| 483 | side = left - x; |
| 484 | if(side < min_side || side > max_side) { |
| 485 | encoder->guts->current_frame_can_do_mid_side = false; |
| 486 | } |
| 487 | else { |
| 488 | mid = (left + x) >> 1; /* NOTE: not the same as divide-by-two ! */ |
| 489 | encoder->guts->integer_signal_mid_side[0][i] = mid; |
| 490 | encoder->guts->integer_signal_mid_side[1][i] = side; |
| 491 | encoder->guts->real_signal_mid_side[0][i] = (real)mid; |
| 492 | encoder->guts->real_signal_mid_side[1][i] = (real)side; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | encoder->guts->current_sample_number++; |
| 498 | } |
| 499 | if(i == encoder->blocksize) { |
| 500 | if(!encoder_process_frame_(encoder, false)) /* false => not last frame */ |
| 501 | return false; |
| 502 | } |
| 503 | } while(j < samples); |
| 504 | |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame) |
| 509 | { |
| 510 | FLAC__FrameHeader frame_header; |
| 511 | FLAC__BitBuffer *smallest_frame; |
| 512 | |
| 513 | assert(encoder->state == FLAC__ENCODER_OK); |
| 514 | |
| 515 | /* |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 516 | * Accumulate raw signal to the MD5 signature |
| 517 | */ |
| 518 | if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) { |
| 519 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 524 | * First do a normal encoding pass |
| 525 | */ |
| 526 | frame_header.blocksize = encoder->blocksize; |
| 527 | frame_header.sample_rate = encoder->sample_rate; |
| 528 | frame_header.channels = encoder->channels; |
| 529 | frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */ |
| 530 | frame_header.bits_per_sample = encoder->bits_per_sample; |
| 531 | frame_header.number.frame_number = encoder->guts->current_frame_number; |
| 532 | |
| 533 | if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) { |
| 534 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 535 | return false; |
| 536 | } |
| 537 | if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) { |
| 538 | encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | if(!encoder_process_subframes_(encoder, is_last_frame, &frame_header, encoder->channels, encoder->guts->integer_signal, encoder->guts->real_signal, &encoder->guts->frame)) |
| 543 | return false; |
| 544 | |
| 545 | smallest_frame = &encoder->guts->frame; |
| 546 | |
| 547 | /* |
| 548 | * Now try a mid-side version if necessary; otherwise, just use the previous step's frame |
| 549 | */ |
| 550 | if(encoder->do_mid_side_stereo && encoder->guts->current_frame_can_do_mid_side) { |
| 551 | int32 *integer_signal[2]; |
| 552 | real *real_signal[2]; |
| 553 | |
| 554 | assert(encoder->channels == 2); |
| 555 | |
| 556 | /* mid-side */ |
| 557 | frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE; |
| 558 | if(!FLAC__bitbuffer_clear(&encoder->guts->frame_mid_side)) { |
| 559 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 560 | return false; |
| 561 | } |
| 562 | if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame_mid_side)) { |
| 563 | encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 564 | return false; |
| 565 | } |
| 566 | integer_signal[0] = encoder->guts->integer_signal_mid_side[0]; /* mid channel */ |
| 567 | integer_signal[1] = encoder->guts->integer_signal_mid_side[1]; /* side channel */ |
| 568 | real_signal[0] = encoder->guts->real_signal_mid_side[0]; /* mid channel */ |
| 569 | real_signal[1] = encoder->guts->real_signal_mid_side[1]; /* side channel */ |
| 570 | if(!encoder_process_subframes_(encoder, is_last_frame, &frame_header, encoder->channels, integer_signal, real_signal, &encoder->guts->frame_mid_side)) |
| 571 | return false; |
| 572 | if(encoder->guts->frame_mid_side.total_bits < smallest_frame->total_bits) |
| 573 | smallest_frame = &encoder->guts->frame_mid_side; |
| 574 | |
| 575 | /* left-side */ |
| 576 | frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE; |
| 577 | if(!FLAC__bitbuffer_clear(&encoder->guts->frame_left_side)) { |
| 578 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 579 | return false; |
| 580 | } |
| 581 | if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame_left_side)) { |
| 582 | encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 583 | return false; |
| 584 | } |
| 585 | integer_signal[0] = encoder->guts->integer_signal[0]; /* left channel */ |
| 586 | integer_signal[1] = encoder->guts->integer_signal_mid_side[1]; /* side channel */ |
| 587 | real_signal[0] = encoder->guts->real_signal[0]; /* left channel */ |
| 588 | real_signal[1] = encoder->guts->real_signal_mid_side[1]; /* side channel */ |
| 589 | if(!encoder_process_subframes_(encoder, is_last_frame, &frame_header, encoder->channels, integer_signal, real_signal, &encoder->guts->frame_left_side)) |
| 590 | return false; |
| 591 | if(encoder->guts->frame_left_side.total_bits < smallest_frame->total_bits) |
| 592 | smallest_frame = &encoder->guts->frame_left_side; |
| 593 | |
| 594 | /* right-side */ |
| 595 | frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE; |
| 596 | if(!FLAC__bitbuffer_clear(&encoder->guts->frame_right_side)) { |
| 597 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 598 | return false; |
| 599 | } |
| 600 | if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame_right_side)) { |
| 601 | encoder->state = FLAC__ENCODER_FRAMING_ERROR; |
| 602 | return false; |
| 603 | } |
| 604 | integer_signal[0] = encoder->guts->integer_signal_mid_side[1]; /* side channel */ |
| 605 | integer_signal[1] = encoder->guts->integer_signal[1]; /* right channel */ |
| 606 | real_signal[0] = encoder->guts->real_signal_mid_side[1]; /* side channel */ |
| 607 | real_signal[1] = encoder->guts->real_signal[1]; /* right channel */ |
| 608 | if(!encoder_process_subframes_(encoder, is_last_frame, &frame_header, encoder->channels, integer_signal, real_signal, &encoder->guts->frame_right_side)) |
| 609 | return false; |
| 610 | if(encoder->guts->frame_right_side.total_bits < smallest_frame->total_bits) |
| 611 | smallest_frame = &encoder->guts->frame_right_side; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Zero-pad the frame to a byte_boundary |
| 616 | */ |
| 617 | if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(smallest_frame)) { |
| 618 | encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR; |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Write it |
| 624 | */ |
| 625 | assert(smallest_frame->bits == 0); /* assert that we're byte-aligned before writing */ |
| 626 | assert(smallest_frame->total_consumed_bits == 0); /* assert that no reading of the buffer was done */ |
| 627 | if(encoder->guts->write_callback(encoder, smallest_frame->buffer, smallest_frame->bytes, encoder->blocksize, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK) { |
| 628 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING; |
| 629 | return false; |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * Get ready for the next frame |
| 634 | */ |
| 635 | encoder->guts->current_frame_can_do_mid_side = true; |
| 636 | encoder->guts->current_sample_number = 0; |
| 637 | encoder->guts->current_frame_number++; |
| 638 | encoder->guts->metadata.data.encoding.total_samples += (uint64)encoder->blocksize; |
| 639 | encoder->guts->metadata.data.encoding.min_framesize = min(smallest_frame->bytes, encoder->guts->metadata.data.encoding.min_framesize); |
| 640 | encoder->guts->metadata.data.encoding.max_framesize = max(smallest_frame->bytes, encoder->guts->metadata.data.encoding.max_framesize); |
| 641 | |
| 642 | return true; |
| 643 | } |
| 644 | |
| 645 | bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame, const FLAC__FrameHeader *frame_header, unsigned channels, const int32 *integer_signal[], const real *real_signal[], FLAC__BitBuffer *frame) |
| 646 | { |
| 647 | real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 648 | real lpc_residual_bits_per_sample; |
| 649 | real autoc[FLAC__MAX_LPC_ORDER+1]; |
| 650 | real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; |
| 651 | real lpc_error[FLAC__MAX_LPC_ORDER]; |
| 652 | unsigned channel; |
| 653 | unsigned min_lpc_order, max_lpc_order, lpc_order; |
| 654 | unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order; |
| 655 | unsigned max_partition_order; |
| 656 | unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision; |
| 657 | unsigned rice_parameter; |
| 658 | unsigned candidate_bits, best_bits; |
| 659 | |
| 660 | if(is_last_frame) { |
| 661 | max_partition_order = 0; |
| 662 | } |
| 663 | else { |
| 664 | unsigned limit = 0, b = encoder->blocksize; |
| 665 | while(!(b & 1)) { |
| 666 | limit++; |
| 667 | b >>= 1; |
| 668 | } |
| 669 | max_partition_order = min(encoder->rice_optimization_level, limit); |
| 670 | } |
| 671 | |
| 672 | for(channel = 0; channel < channels; channel++) { |
| 673 | /* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 674 | best_bits = encoder_evaluate_verbatim_subframe_(frame_header->blocksize, frame_header->bits_per_sample, &(encoder->guts->best_subframe)); |
| 675 | |
| 676 | if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { |
| 677 | /* check for constant subframe */ |
| 678 | guess_fixed_order = FLAC__fixed_compute_best_predictor(integer_signal[channel]+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample); |
| 679 | if(fixed_residual_bits_per_sample[1] == 0.0) { |
Josh Coalson | d44feb1 | 2000-12-17 19:07:46 +0000 | [diff] [blame] | 680 | /* the above means integer_signal[channel]+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */ |
| 681 | unsigned i, signal_is_constant = true; |
| 682 | for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) { |
| 683 | if(integer_signal[channel][0] != integer_signal[channel][i]) { |
| 684 | signal_is_constant = false; |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | if(signal_is_constant) { |
| 689 | candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[channel][0], frame_header->bits_per_sample, &(encoder->guts->candidate_subframe)); |
| 690 | if(candidate_bits < best_bits) { |
| 691 | encoder_promote_candidate_subframe_(encoder); |
| 692 | best_bits = candidate_bits; |
| 693 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | else { |
| 697 | /* encode fixed */ |
| 698 | if(encoder->do_exhaustive_model_search) { |
| 699 | min_fixed_order = 0; |
| 700 | max_fixed_order = FLAC__MAX_FIXED_ORDER; |
| 701 | } |
| 702 | else { |
| 703 | min_fixed_order = max_fixed_order = guess_fixed_order; |
| 704 | } |
| 705 | for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { |
| 706 | if(fixed_residual_bits_per_sample[fixed_order] >= (real)frame_header->bits_per_sample) |
| 707 | continue; /* don't even try */ |
Josh Coalson | 1975375 | 2001-01-06 01:24:53 +0000 | [diff] [blame] | 708 | /* 0.5 is for rounding, another 1.0 is to account for the signed->unsigned conversion during rice coding */ |
| 709 | rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+1.5) : 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 710 | if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)) |
| 711 | rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1; |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 712 | candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal[channel], encoder->guts->residual[!encoder->guts->best_residual], encoder->guts->abs_residual, frame_header->blocksize, frame_header->bits_per_sample, fixed_order, rice_parameter, max_partition_order, &(encoder->guts->candidate_subframe)); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 713 | if(candidate_bits < best_bits) { |
| 714 | encoder_promote_candidate_subframe_(encoder); |
| 715 | best_bits = candidate_bits; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /* encode lpc */ |
| 720 | if(encoder->max_lpc_order > 0) { |
| 721 | if(encoder->max_lpc_order >= frame_header->blocksize) |
| 722 | max_lpc_order = frame_header->blocksize-1; |
| 723 | else |
| 724 | max_lpc_order = encoder->max_lpc_order; |
| 725 | if(max_lpc_order > 0) { |
| 726 | FLAC__lpc_compute_autocorrelation(real_signal[channel], frame_header->blocksize, max_lpc_order+1, autoc); |
| 727 | FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error); |
| 728 | if(encoder->do_exhaustive_model_search) { |
| 729 | min_lpc_order = 1; |
| 730 | } |
| 731 | else { |
| 732 | unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, frame_header->bits_per_sample); |
| 733 | min_lpc_order = max_lpc_order = guess_lpc_order; |
| 734 | } |
| 735 | if(encoder->do_qlp_coeff_prec_search) { |
| 736 | min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
| 737 | max_qlp_coeff_precision = 32 - frame_header->bits_per_sample - 1; |
| 738 | } |
| 739 | else { |
| 740 | min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision; |
| 741 | } |
| 742 | for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) { |
| 743 | lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize); |
| 744 | if(lpc_residual_bits_per_sample >= (real)frame_header->bits_per_sample) |
| 745 | continue; /* don't even try */ |
Josh Coalson | 1975375 | 2001-01-06 01:24:53 +0000 | [diff] [blame] | 746 | /* 0.5 is for rounding, another 1.0 is to account for the signed->unsigned conversion during rice coding */ |
| 747 | rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+1.5) : 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 748 | if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)) |
| 749 | rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1; |
| 750 | for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) { |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 751 | candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal[channel], encoder->guts->residual[!encoder->guts->best_residual], encoder->guts->abs_residual, lp_coeff[lpc_order-1], frame_header->blocksize, frame_header->bits_per_sample, lpc_order, qlp_coeff_precision, rice_parameter, max_partition_order, &(encoder->guts->candidate_subframe)); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 752 | if(candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 753 | if(candidate_bits < best_bits) { |
| 754 | encoder_promote_candidate_subframe_(encoder); |
| 755 | best_bits = candidate_bits; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | /* add the best subframe */ |
| 766 | switch(encoder->guts->best_subframe.type) { |
| 767 | case FLAC__SUBFRAME_TYPE_CONSTANT: |
| 768 | if(!encoder_generate_constant_subframe_(&(encoder->guts->best_subframe), frame_header->bits_per_sample, frame)) { |
| 769 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 770 | return false; |
| 771 | } |
| 772 | break; |
| 773 | case FLAC__SUBFRAME_TYPE_FIXED: |
| 774 | if(!encoder_generate_fixed_subframe_(&(encoder->guts->best_subframe), encoder->guts->residual[encoder->guts->best_residual], frame_header->blocksize, frame_header->bits_per_sample, frame)) { |
| 775 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 776 | return false; |
| 777 | } |
| 778 | break; |
| 779 | case FLAC__SUBFRAME_TYPE_LPC: |
| 780 | if(!encoder_generate_lpc_subframe_(&(encoder->guts->best_subframe), encoder->guts->residual[encoder->guts->best_residual], frame_header->blocksize, frame_header->bits_per_sample, frame)) { |
| 781 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 782 | return false; |
| 783 | } |
| 784 | break; |
| 785 | case FLAC__SUBFRAME_TYPE_VERBATIM: |
| 786 | if(!encoder_generate_verbatim_subframe_(&(encoder->guts->best_subframe), integer_signal[channel], frame_header->blocksize, frame_header->bits_per_sample, frame)) { |
| 787 | encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING; |
| 788 | return false; |
| 789 | } |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned bits_per_sample, FLAC__SubframeHeader *subframe) |
| 798 | { |
| 799 | subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 800 | subframe->data.constant.value = signal; |
| 801 | |
Josh Coalson | 1975375 | 2001-01-06 01:24:53 +0000 | [diff] [blame] | 802 | return FLAC__SUBFRAME_HEADER_TYPE_LEN + bits_per_sample; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 805 | unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__SubframeHeader *subframe) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 806 | { |
| 807 | unsigned i, residual_bits; |
| 808 | const unsigned residual_samples = blocksize - order; |
| 809 | |
| 810 | FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual); |
| 811 | |
| 812 | subframe->type = FLAC__SUBFRAME_TYPE_FIXED; |
| 813 | |
| 814 | subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 815 | |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 816 | 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); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 817 | |
| 818 | subframe->data.fixed.order = order; |
| 819 | for(i = 0; i < order; i++) |
| 820 | subframe->data.fixed.warmup[i] = signal[i]; |
| 821 | |
Josh Coalson | 1975375 | 2001-01-06 01:24:53 +0000 | [diff] [blame] | 822 | return FLAC__SUBFRAME_HEADER_TYPE_LEN + (order * bits_per_sample) + residual_bits; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 825 | unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], const real lp_coeff[], unsigned blocksize, unsigned bits_per_sample, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned max_partition_order, FLAC__SubframeHeader *subframe) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 826 | { |
| 827 | int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; |
| 828 | unsigned i, residual_bits; |
| 829 | int quantization, ret; |
| 830 | const unsigned residual_samples = blocksize - order; |
| 831 | |
| 832 | ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, bits_per_sample, qlp_coeff, &quantization); |
| 833 | if(ret != 0) |
| 834 | return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ |
| 835 | |
| 836 | FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
| 837 | |
| 838 | subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 839 | |
| 840 | subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 841 | |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 842 | 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); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 843 | |
| 844 | subframe->data.lpc.order = order; |
| 845 | subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 846 | subframe->data.lpc.quantization_level = quantization; |
| 847 | memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER); |
| 848 | for(i = 0; i < order; i++) |
| 849 | subframe->data.lpc.warmup[i] = signal[i]; |
| 850 | |
Josh Coalson | 1975375 | 2001-01-06 01:24:53 +0000 | [diff] [blame] | 851 | return FLAC__SUBFRAME_HEADER_TYPE_LEN + FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + bits_per_sample)) + residual_bits; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | unsigned encoder_evaluate_verbatim_subframe_(unsigned blocksize, unsigned bits_per_sample, FLAC__SubframeHeader *subframe) |
| 855 | { |
| 856 | subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 857 | |
Josh Coalson | 1975375 | 2001-01-06 01:24:53 +0000 | [diff] [blame] | 858 | return FLAC__SUBFRAME_HEADER_TYPE_LEN + (blocksize * bits_per_sample); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 861 | 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 | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 862 | { |
| 863 | unsigned residual_bits, best_residual_bits = 0; |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 864 | unsigned i, partition_order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 865 | unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER]; |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 866 | int32 r; |
| 867 | |
| 868 | /* compute the abs(residual) for use later */ |
| 869 | for(i = 0; i < residual_samples; i++) { |
| 870 | r = residual[i]; |
| 871 | abs_residual[i] = (uint32)(r<0? -r : r); |
| 872 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 873 | |
| 874 | for(partition_order = 0; partition_order <= max_partition_order; partition_order++) { |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 875 | if(!encoder_set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, partition_order, parameters[!best_parameters_index], &residual_bits)) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 876 | assert(best_residual_bits != 0); |
| 877 | break; |
| 878 | } |
| 879 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 880 | best_residual_bits = residual_bits; |
| 881 | *best_partition_order = partition_order; |
| 882 | best_parameters_index = !best_parameters_index; |
| 883 | } |
| 884 | } |
| 885 | memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order))); |
| 886 | |
| 887 | return best_residual_bits; |
| 888 | } |
| 889 | |
| 890 | bool encoder_generate_constant_subframe_(const FLAC__SubframeHeader *header, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer) |
| 891 | { |
| 892 | assert(header->type == FLAC__SUBFRAME_TYPE_CONSTANT); |
| 893 | return FLAC__subframe_add_constant(bits_per_sample, header, bitbuffer); |
| 894 | } |
| 895 | |
| 896 | bool encoder_generate_fixed_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer) |
| 897 | { |
| 898 | assert(header->type == FLAC__SUBFRAME_TYPE_FIXED); |
| 899 | return FLAC__subframe_add_fixed(residual, blocksize - header->data.fixed.order, bits_per_sample, header, bitbuffer); |
| 900 | } |
| 901 | |
| 902 | bool encoder_generate_lpc_subframe_(const FLAC__SubframeHeader *header, int32 residual[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer) |
| 903 | { |
| 904 | assert(header->type == FLAC__SUBFRAME_TYPE_LPC); |
| 905 | return FLAC__subframe_add_lpc(residual, blocksize - header->data.lpc.order, bits_per_sample, header, bitbuffer); |
| 906 | } |
| 907 | |
| 908 | bool encoder_generate_verbatim_subframe_(const FLAC__SubframeHeader *header, const int32 signal[], unsigned blocksize, unsigned bits_per_sample, FLAC__BitBuffer *bitbuffer) |
| 909 | { |
| 910 | assert(header->type == FLAC__SUBFRAME_TYPE_VERBATIM); |
| 911 | #ifdef NDEBUG |
| 912 | (void)header; /* silence compiler warning about unused parameter */ |
| 913 | #endif |
| 914 | return FLAC__subframe_add_verbatim(signal, blocksize, bits_per_sample, bitbuffer); |
| 915 | } |
| 916 | |
| 917 | void encoder_promote_candidate_subframe_(FLAC__Encoder *encoder) |
| 918 | { |
| 919 | assert(encoder->state == FLAC__ENCODER_OK); |
| 920 | encoder->guts->best_subframe = encoder->guts->candidate_subframe; |
| 921 | encoder->guts->best_residual = !encoder->guts->best_residual; |
| 922 | } |
| 923 | |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 924 | #ifdef ESTIMATE_RICE_BITS |
| 925 | #undef ESTIMATE_RICE_BITS |
| 926 | #endif |
| 927 | #define ESTIMATE_RICE_BITS(value, parameter) ((value) >> (parameter)) |
| 928 | |
| 929 | 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 | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 930 | { |
Josh Coalson | 1975375 | 2001-01-06 01:24:53 +0000 | [diff] [blame] | 931 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 932 | |
| 933 | if(partition_order == 0) { |
| 934 | unsigned i; |
Josh Coalson | e55f390 | 2001-01-19 23:13:44 +0000 | [diff] [blame] | 935 | #ifdef ESTIMATE_RICE_BITS |
| 936 | const unsigned rice_parameter_estimate = rice_parameter-1; |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 937 | bits_ += (1+rice_parameter) * residual_samples; |
Josh Coalson | e55f390 | 2001-01-19 23:13:44 +0000 | [diff] [blame] | 938 | #endif |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 939 | parameters[0] = rice_parameter; |
| 940 | bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 941 | for(i = 0; i < residual_samples; i++) |
Josh Coalson | 2a52e57 | 2001-01-19 20:11:59 +0000 | [diff] [blame] | 942 | #ifdef ESTIMATE_RICE_BITS |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 943 | bits_ += ESTIMATE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
Josh Coalson | 2a52e57 | 2001-01-19 20:11:59 +0000 | [diff] [blame] | 944 | #else |
Josh Coalson | 1975375 | 2001-01-06 01:24:53 +0000 | [diff] [blame] | 945 | bits_ += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); |
Josh Coalson | 2a52e57 | 2001-01-19 20:11:59 +0000 | [diff] [blame] | 946 | #endif |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 947 | } |
| 948 | else { |
Josh Coalson | 1975375 | 2001-01-06 01:24:53 +0000 | [diff] [blame] | 949 | unsigned i, j, k = 0, k_last = 0; |
| 950 | unsigned mean, parameter, partition_samples; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 951 | const unsigned max_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1; |
| 952 | for(i = 0; i < (1u<<partition_order); i++) { |
| 953 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
| 954 | if(i == 0) { |
| 955 | if(partition_samples <= predictor_order) |
| 956 | return false; |
| 957 | else |
| 958 | partition_samples -= predictor_order; |
| 959 | } |
| 960 | mean = partition_samples >> 1; |
| 961 | for(j = 0; j < partition_samples; j++, k++) |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 962 | mean += abs_residual[k]; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 963 | mean /= partition_samples; |
Josh Coalson | 1975375 | 2001-01-06 01:24:53 +0000 | [diff] [blame] | 964 | /* calc parameter = floor(log2(mean)) + 1 */ |
| 965 | parameter = 0; |
| 966 | while(mean) { |
| 967 | parameter++; |
| 968 | mean >>= 1; |
| 969 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 970 | if(parameter > max_parameter) |
| 971 | parameter = max_parameter; |
| 972 | parameters[i] = parameter; |
| 973 | bits_ += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 2a52e57 | 2001-01-19 20:11:59 +0000 | [diff] [blame] | 974 | #ifdef ESTIMATE_RICE_BITS |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 975 | bits_ += (1+parameter) * partition_samples; |
Josh Coalson | 2a52e57 | 2001-01-19 20:11:59 +0000 | [diff] [blame] | 976 | --parameter; |
| 977 | #endif |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 978 | for(j = k_last; j < k; j++) |
Josh Coalson | 2a52e57 | 2001-01-19 20:11:59 +0000 | [diff] [blame] | 979 | #ifdef ESTIMATE_RICE_BITS |
Josh Coalson | e77287e | 2001-01-20 01:27:55 +0000 | [diff] [blame] | 980 | bits_ += ESTIMATE_RICE_BITS(abs_residual[j], parameter); |
Josh Coalson | 2a52e57 | 2001-01-19 20:11:59 +0000 | [diff] [blame] | 981 | #else |
Josh Coalson | 1975375 | 2001-01-06 01:24:53 +0000 | [diff] [blame] | 982 | bits_ += FLAC__bitbuffer_rice_bits(residual[j], parameter); |
Josh Coalson | 2a52e57 | 2001-01-19 20:11:59 +0000 | [diff] [blame] | 983 | #endif |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 984 | k_last = k; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | *bits = bits_; |
| 989 | return true; |
| 990 | } |