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