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