Josh Coalson | 26560dd | 2001-02-08 00:38:41 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Josh Coalson | 305ae2e | 2002-01-26 17:36:39 +0000 | [diff] [blame] | 2 | * Copyright (C) 2000,2001,2002 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 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> /* for malloc() */ |
| 22 | #include <string.h> /* for memcpy() */ |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 23 | #include "FLAC/assert.h" |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 24 | #include "FLAC/stream_decoder.h" |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 25 | #include "protected/stream_encoder.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 | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 29 | #include "private/cpu.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 30 | #include "private/fixed.h" |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 31 | #include "private/format.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 32 | #include "private/lpc.h" |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 33 | #include "private/md5.h" |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 34 | #include "private/memory.h" |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 35 | #include "private/stream_encoder_framing.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 36 | |
| 37 | #ifdef min |
| 38 | #undef min |
| 39 | #endif |
| 40 | #define min(x,y) ((x)<(y)?(x):(y)) |
| 41 | |
| 42 | #ifdef max |
| 43 | #undef max |
| 44 | #endif |
| 45 | #define max(x,y) ((x)>(y)?(x):(y)) |
| 46 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 47 | typedef struct { |
| 48 | FLAC__int32 *data[FLAC__MAX_CHANNELS]; |
| 49 | unsigned size; /* of each data[] in samples */ |
| 50 | unsigned tail; |
| 51 | } verify_input_fifo; |
| 52 | |
| 53 | typedef struct { |
| 54 | const FLAC__byte *data; |
| 55 | unsigned capacity; |
| 56 | unsigned bytes; |
| 57 | } verify_output; |
| 58 | |
| 59 | typedef enum { |
| 60 | ENCODER_IN_MAGIC = 0, |
| 61 | ENCODER_IN_METADATA = 1, |
| 62 | ENCODER_IN_AUDIO = 2 |
| 63 | } EncoderStateHint; |
| 64 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 65 | /*********************************************************************** |
| 66 | * |
| 67 | * Private class method prototypes |
| 68 | * |
| 69 | ***********************************************************************/ |
| 70 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 71 | static void set_defaults_(FLAC__StreamEncoder *encoder); |
| 72 | static void free_(FLAC__StreamEncoder *encoder); |
| 73 | static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 74 | static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples); |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 75 | static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame); |
| 76 | static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame); |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 77 | |
| 78 | static FLAC__bool process_subframe_( |
| 79 | FLAC__StreamEncoder *encoder, |
| 80 | unsigned min_partition_order, |
| 81 | unsigned max_partition_order, |
| 82 | FLAC__bool precompute_partition_sums, |
| 83 | FLAC__bool verbatim_only, |
| 84 | const FLAC__FrameHeader *frame_header, |
| 85 | unsigned subframe_bps, |
| 86 | const FLAC__int32 integer_signal[], |
| 87 | const FLAC__real real_signal[], |
| 88 | FLAC__Subframe *subframe[2], |
| 89 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2], |
| 90 | FLAC__int32 *residual[2], |
| 91 | unsigned *best_subframe, |
| 92 | unsigned *best_bits |
| 93 | ); |
| 94 | |
| 95 | static FLAC__bool add_subframe_( |
| 96 | FLAC__StreamEncoder *encoder, |
| 97 | const FLAC__FrameHeader *frame_header, |
| 98 | unsigned subframe_bps, |
| 99 | const FLAC__Subframe *subframe, |
| 100 | FLAC__BitBuffer *frame |
| 101 | ); |
| 102 | |
| 103 | static unsigned evaluate_constant_subframe_( |
| 104 | const FLAC__int32 signal, |
| 105 | unsigned subframe_bps, |
| 106 | FLAC__Subframe *subframe |
| 107 | ); |
| 108 | |
| 109 | static unsigned evaluate_fixed_subframe_( |
| 110 | FLAC__StreamEncoder *encoder, |
| 111 | const FLAC__int32 signal[], |
| 112 | FLAC__int32 residual[], |
| 113 | FLAC__uint32 abs_residual[], |
| 114 | FLAC__uint64 abs_residual_partition_sums[], |
| 115 | unsigned raw_bits_per_partition[], |
| 116 | unsigned blocksize, |
| 117 | unsigned subframe_bps, |
| 118 | unsigned order, |
| 119 | unsigned rice_parameter, |
| 120 | unsigned min_partition_order, |
| 121 | unsigned max_partition_order, |
| 122 | FLAC__bool precompute_partition_sums, |
| 123 | FLAC__bool do_escape_coding, |
| 124 | unsigned rice_parameter_search_dist, |
| 125 | FLAC__Subframe *subframe, |
| 126 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 127 | ); |
| 128 | |
| 129 | static unsigned evaluate_lpc_subframe_( |
| 130 | FLAC__StreamEncoder *encoder, |
| 131 | const FLAC__int32 signal[], |
| 132 | FLAC__int32 residual[], |
| 133 | FLAC__uint32 abs_residual[], |
| 134 | FLAC__uint64 abs_residual_partition_sums[], |
| 135 | unsigned raw_bits_per_partition[], |
| 136 | const FLAC__real lp_coeff[], |
| 137 | unsigned blocksize, |
| 138 | unsigned subframe_bps, |
| 139 | unsigned order, |
| 140 | unsigned qlp_coeff_precision, |
| 141 | unsigned rice_parameter, |
| 142 | unsigned min_partition_order, |
| 143 | unsigned max_partition_order, |
| 144 | FLAC__bool precompute_partition_sums, |
| 145 | FLAC__bool do_escape_coding, |
| 146 | unsigned rice_parameter_search_dist, |
| 147 | FLAC__Subframe *subframe, |
| 148 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 149 | ); |
| 150 | |
| 151 | static unsigned evaluate_verbatim_subframe_( |
| 152 | const FLAC__int32 signal[], |
| 153 | unsigned blocksize, |
| 154 | unsigned subframe_bps, |
| 155 | FLAC__Subframe *subframe |
| 156 | ); |
| 157 | |
| 158 | static unsigned find_best_partition_order_( |
| 159 | struct FLAC__StreamEncoderPrivate *private_, |
| 160 | const FLAC__int32 residual[], |
| 161 | FLAC__uint32 abs_residual[], |
| 162 | FLAC__uint64 abs_residual_partition_sums[], |
| 163 | unsigned raw_bits_per_partition[], |
| 164 | unsigned residual_samples, |
| 165 | unsigned predictor_order, |
| 166 | unsigned rice_parameter, |
| 167 | unsigned min_partition_order, |
| 168 | unsigned max_partition_order, |
| 169 | FLAC__bool precompute_partition_sums, |
| 170 | FLAC__bool do_escape_coding, |
| 171 | unsigned rice_parameter_search_dist, |
| 172 | FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice |
| 173 | ); |
| 174 | |
| 175 | static void precompute_partition_info_sums_( |
| 176 | const FLAC__uint32 abs_residual[], |
| 177 | FLAC__uint64 abs_residual_partition_sums[], |
| 178 | unsigned residual_samples, |
| 179 | unsigned predictor_order, |
| 180 | unsigned min_partition_order, |
| 181 | unsigned max_partition_order |
| 182 | ); |
| 183 | |
| 184 | static void precompute_partition_info_escapes_( |
| 185 | const FLAC__int32 residual[], |
| 186 | unsigned raw_bits_per_partition[], |
| 187 | unsigned residual_samples, |
| 188 | unsigned predictor_order, |
| 189 | unsigned min_partition_order, |
| 190 | unsigned max_partition_order |
| 191 | ); |
| 192 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 193 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 194 | static FLAC__bool set_partitioned_rice_( |
| 195 | const FLAC__uint32 abs_residual[], |
| 196 | const FLAC__int32 residual[], |
| 197 | const unsigned residual_samples, |
| 198 | const unsigned predictor_order, |
| 199 | const unsigned suggested_rice_parameter, |
| 200 | const unsigned rice_parameter_search_dist, |
| 201 | const unsigned partition_order, |
| 202 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 203 | unsigned *bits |
| 204 | ); |
| 205 | |
| 206 | static FLAC__bool set_partitioned_rice_with_precompute_( |
| 207 | const FLAC__int32 residual[], |
| 208 | const FLAC__uint64 abs_residual_partition_sums[], |
| 209 | const unsigned raw_bits_per_partition[], |
| 210 | const unsigned residual_samples, |
| 211 | const unsigned predictor_order, |
| 212 | const unsigned suggested_rice_parameter, |
| 213 | const unsigned rice_parameter_search_dist, |
| 214 | const unsigned partition_order, |
| 215 | const FLAC__bool search_for_escapes, |
| 216 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 217 | unsigned *bits |
| 218 | ); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 219 | #else |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 220 | static FLAC__bool set_partitioned_rice_( |
| 221 | const FLAC__uint32 abs_residual[], |
| 222 | const unsigned residual_samples, |
| 223 | const unsigned predictor_order, |
| 224 | const unsigned suggested_rice_parameter, |
| 225 | const unsigned rice_parameter_search_dist, |
| 226 | const unsigned partition_order, |
| 227 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 228 | unsigned *bits |
| 229 | ); |
| 230 | |
| 231 | static FLAC__bool set_partitioned_rice_with_precompute_( |
| 232 | const FLAC__uint32 abs_residual[], |
| 233 | const FLAC__uint64 abs_residual_partition_sums[], |
| 234 | const unsigned raw_bits_per_partition[], |
| 235 | const unsigned residual_samples, |
| 236 | const unsigned predictor_order, |
| 237 | const unsigned suggested_rice_parameter, |
| 238 | const unsigned rice_parameter_search_dist, |
| 239 | const unsigned partition_order, |
| 240 | const FLAC__bool search_for_escapes, |
| 241 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 242 | unsigned *bits |
| 243 | ); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 244 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 245 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 246 | static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples); |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 247 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 248 | /* verify-related routines: */ |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 249 | static void append_to_verify_fifo_( |
| 250 | verify_input_fifo *fifo, |
| 251 | const FLAC__int32 * const input[], |
| 252 | unsigned input_offset, |
| 253 | unsigned channels, |
| 254 | unsigned wide_samples |
| 255 | ); |
| 256 | |
| 257 | static void append_to_verify_fifo_interleaved_( |
| 258 | verify_input_fifo *fifo, |
| 259 | const FLAC__int32 input[], |
| 260 | unsigned input_offset, |
| 261 | unsigned channels, |
| 262 | unsigned wide_samples |
| 263 | ); |
| 264 | |
| 265 | static FLAC__StreamDecoderReadStatus verify_read_callback_( |
| 266 | const FLAC__StreamDecoder *decoder, |
| 267 | FLAC__byte buffer[], |
| 268 | unsigned *bytes, |
| 269 | void *client_data |
| 270 | ); |
| 271 | |
| 272 | static FLAC__StreamDecoderWriteStatus verify_write_callback_( |
| 273 | const FLAC__StreamDecoder *decoder, |
| 274 | const FLAC__Frame *frame, |
| 275 | const FLAC__int32 * const buffer[], |
| 276 | void *client_data |
| 277 | ); |
| 278 | |
| 279 | static void verify_metadata_callback_( |
| 280 | const FLAC__StreamDecoder *decoder, |
| 281 | const FLAC__StreamMetadata *metadata, |
| 282 | void *client_data |
| 283 | ); |
| 284 | |
| 285 | static void verify_error_callback_( |
| 286 | const FLAC__StreamDecoder *decoder, |
| 287 | FLAC__StreamDecoderErrorStatus status, |
| 288 | void *client_data |
| 289 | ); |
| 290 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 291 | |
| 292 | /*********************************************************************** |
| 293 | * |
| 294 | * Private class data |
| 295 | * |
| 296 | ***********************************************************************/ |
| 297 | |
| 298 | typedef struct FLAC__StreamEncoderPrivate { |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 299 | unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 300 | FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */ |
| 301 | FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */ |
| 302 | FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */ |
| 303 | FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 304 | unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */ |
| 305 | 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 | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 306 | FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */ |
| 307 | FLAC__int32 *residual_workspace_mid_side[2][2]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 308 | FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2]; |
| 309 | FLAC__Subframe subframe_workspace_mid_side[2][2]; |
| 310 | FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 311 | FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2]; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 312 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2]; |
| 313 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2]; |
| 314 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 315 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 316 | unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 317 | unsigned best_subframe_mid_side[2]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 318 | unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 319 | unsigned best_subframe_bits_mid_side[2]; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 320 | FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 321 | FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 322 | unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 323 | FLAC__BitBuffer *frame; /* the current frame being worked on */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 324 | double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 325 | unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 326 | unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 327 | FLAC__ChannelAssignment last_channel_assignment; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 328 | FLAC__StreamMetadata metadata; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 329 | unsigned current_sample_number; |
| 330 | unsigned current_frame_number; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 331 | struct MD5Context md5context; |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 332 | FLAC__CPUInfo cpuinfo; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 333 | unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 334 | void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 335 | void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 336 | void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 337 | FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */ |
| 338 | FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */ |
| 339 | FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */ |
| 340 | FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */ |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 341 | FLAC__StreamEncoderWriteCallback write_callback; |
| 342 | FLAC__StreamEncoderMetadataCallback metadata_callback; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 343 | void *client_data; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 344 | /* unaligned (original) pointers to allocated data */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 345 | FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 346 | FLAC__int32 *integer_signal_mid_side_unaligned[2]; |
| 347 | FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 348 | FLAC__real *real_signal_mid_side_unaligned[2]; |
| 349 | FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2]; |
| 350 | FLAC__int32 *residual_workspace_mid_side_unaligned[2][2]; |
| 351 | FLAC__uint32 *abs_residual_unaligned; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 352 | FLAC__uint64 *abs_residual_partition_sums_unaligned; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 353 | unsigned *raw_bits_per_partition_unaligned; |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 354 | /* |
| 355 | * These fields have been moved here from private function local |
| 356 | * declarations merely to save stack space during encoding. |
| 357 | */ |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 358 | FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */ |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 359 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 360 | /* |
| 361 | * The data for the verify section |
| 362 | */ |
| 363 | struct { |
| 364 | FLAC__StreamDecoder *decoder; |
| 365 | EncoderStateHint state_hint; |
| 366 | FLAC__bool needs_magic_hack; |
| 367 | verify_input_fifo input_fifo; |
| 368 | verify_output output; |
| 369 | struct { |
| 370 | FLAC__uint64 absolute_sample; |
| 371 | unsigned frame_number; |
| 372 | unsigned channel; |
| 373 | unsigned sample; |
| 374 | FLAC__int32 expected; |
| 375 | FLAC__int32 got; |
| 376 | } error_stats; |
| 377 | } verify; |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 378 | FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 379 | } FLAC__StreamEncoderPrivate; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 380 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 381 | /*********************************************************************** |
| 382 | * |
| 383 | * Public static class data |
| 384 | * |
| 385 | ***********************************************************************/ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 386 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 387 | const char * const FLAC__StreamEncoderStateString[] = { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 388 | "FLAC__STREAM_ENCODER_OK", |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 389 | "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR", |
| 390 | "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA", |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 391 | "FLAC__STREAM_ENCODER_INVALID_CALLBACK", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 392 | "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS", |
| 393 | "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE", |
| 394 | "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE", |
| 395 | "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE", |
| 396 | "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION", |
| 397 | "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH", |
| 398 | "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH", |
| 399 | "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE", |
| 400 | "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER", |
| 401 | "FLAC__STREAM_ENCODER_NOT_STREAMABLE", |
| 402 | "FLAC__STREAM_ENCODER_FRAMING_ERROR", |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 403 | "FLAC__STREAM_ENCODER_INVALID_METADATA", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 404 | "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING", |
| 405 | "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING", |
| 406 | "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR", |
| 407 | "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED", |
| 408 | "FLAC__STREAM_ENCODER_UNINITIALIZED" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 411 | const char * const FLAC__StreamEncoderWriteStatusString[] = { |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 412 | "FLAC__STREAM_ENCODER_WRITE_STATUS_OK", |
| 413 | "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 414 | }; |
| 415 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 416 | /*********************************************************************** |
| 417 | * |
| 418 | * Class constructor/destructor |
| 419 | * |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 420 | */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 421 | FLAC__StreamEncoder *FLAC__stream_encoder_new() |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 422 | { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 423 | FLAC__StreamEncoder *encoder; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 424 | unsigned i; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 425 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 426 | FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 427 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 428 | encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder)); |
| 429 | if(encoder == 0) { |
| 430 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 431 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 432 | memset(encoder, 0, sizeof(FLAC__StreamEncoder)); |
| 433 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 434 | encoder->protected_ = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected)); |
| 435 | if(encoder->protected_ == 0) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 436 | free(encoder); |
| 437 | return 0; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 438 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 439 | memset(encoder->protected_, 0, sizeof(FLAC__StreamEncoderProtected)); |
| 440 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 441 | encoder->private_ = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate)); |
| 442 | if(encoder->private_ == 0) { |
| 443 | free(encoder->protected_); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 444 | free(encoder); |
| 445 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 446 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 447 | memset(encoder->private_, 0, sizeof(FLAC__StreamEncoderPrivate)); |
| 448 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 449 | encoder->private_->frame = FLAC__bitbuffer_new(); |
| 450 | if(encoder->private_->frame == 0) { |
| 451 | free(encoder->private_); |
| 452 | free(encoder->protected_); |
| 453 | free(encoder); |
| 454 | return 0; |
| 455 | } |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 456 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 457 | set_defaults_(encoder); |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 458 | |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 459 | encoder->private_->is_being_deleted = false; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 460 | |
| 461 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 462 | encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0]; |
| 463 | encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1]; |
| 464 | } |
| 465 | for(i = 0; i < 2; i++) { |
| 466 | encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0]; |
| 467 | encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1]; |
| 468 | } |
| 469 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 470 | encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0]; |
| 471 | encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1]; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 472 | } |
| 473 | for(i = 0; i < 2; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 474 | encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]; |
| 475 | encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 479 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]); |
| 480 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 481 | } |
| 482 | for(i = 0; i < 2; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 483 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]); |
| 484 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 485 | } |
| 486 | for(i = 0; i < 2; i++) |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 487 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 488 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 489 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 490 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 491 | return encoder; |
| 492 | } |
| 493 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 494 | void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 495 | { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 496 | unsigned i; |
| 497 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 498 | FLAC__ASSERT(0 != encoder); |
| 499 | FLAC__ASSERT(0 != encoder->protected_); |
| 500 | FLAC__ASSERT(0 != encoder->private_); |
| 501 | FLAC__ASSERT(0 != encoder->private_->frame); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 502 | |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 503 | encoder->private_->is_being_deleted = true; |
| 504 | |
| 505 | FLAC__stream_encoder_finish(encoder); |
| 506 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 507 | if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder) |
| 508 | FLAC__stream_decoder_delete(encoder->private_->verify.decoder); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 509 | |
| 510 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 511 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]); |
| 512 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 513 | } |
| 514 | for(i = 0; i < 2; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 515 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]); |
| 516 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 517 | } |
| 518 | for(i = 0; i < 2; i++) |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 519 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 520 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 521 | FLAC__bitbuffer_delete(encoder->private_->frame); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 522 | free(encoder->private_); |
| 523 | free(encoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 524 | free(encoder); |
| 525 | } |
| 526 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 527 | /*********************************************************************** |
| 528 | * |
| 529 | * Public class methods |
| 530 | * |
| 531 | ***********************************************************************/ |
| 532 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 533 | FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 534 | { |
| 535 | unsigned i; |
| 536 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 537 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 538 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 539 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 540 | return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 541 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 542 | encoder->protected_->state = FLAC__STREAM_ENCODER_OK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 543 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 544 | if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback) |
| 545 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 546 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 547 | if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS) |
| 548 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS; |
Josh Coalson | 69f1ee0 | 2001-01-24 00:54:43 +0000 | [diff] [blame] | 549 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 550 | if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2) |
| 551 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH; |
Josh Coalson | d37d135 | 2001-05-30 23:09:31 +0000 | [diff] [blame] | 552 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 553 | if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo) |
| 554 | return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 555 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 556 | if(encoder->protected_->bits_per_sample >= 32) |
| 557 | encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 558 | |
Josh Coalson | 76c68bc | 2002-05-17 06:22:02 +0000 | [diff] [blame] | 559 | if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 560 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 561 | |
Josh Coalson | 0833f34 | 2002-07-15 05:31:55 +0000 | [diff] [blame] | 562 | if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 563 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 564 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 565 | if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE) |
| 566 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 567 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 568 | if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order) |
| 569 | return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 570 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 571 | if(encoder->protected_->qlp_coeff_precision == 0) { |
| 572 | if(encoder->protected_->bits_per_sample < 16) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 573 | /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */ |
| 574 | /* @@@ until then we'll make a guess */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 575 | encoder->protected_->qlp_coeff_precision = max(5, 2 + encoder->protected_->bits_per_sample / 2); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 576 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 577 | else if(encoder->protected_->bits_per_sample == 16) { |
| 578 | if(encoder->protected_->blocksize <= 192) |
| 579 | encoder->protected_->qlp_coeff_precision = 7; |
| 580 | else if(encoder->protected_->blocksize <= 384) |
| 581 | encoder->protected_->qlp_coeff_precision = 8; |
| 582 | else if(encoder->protected_->blocksize <= 576) |
| 583 | encoder->protected_->qlp_coeff_precision = 9; |
| 584 | else if(encoder->protected_->blocksize <= 1152) |
| 585 | encoder->protected_->qlp_coeff_precision = 10; |
| 586 | else if(encoder->protected_->blocksize <= 2304) |
| 587 | encoder->protected_->qlp_coeff_precision = 11; |
| 588 | else if(encoder->protected_->blocksize <= 4608) |
| 589 | encoder->protected_->qlp_coeff_precision = 12; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 590 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 591 | encoder->protected_->qlp_coeff_precision = 13; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 592 | } |
| 593 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 594 | encoder->protected_->qlp_coeff_precision = min(13, 8*sizeof(FLAC__int32) - encoder->protected_->bits_per_sample - 1 - 2); /* @@@ -2 to keep things 32-bit safe */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 597 | else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision + encoder->protected_->bits_per_sample >= 8*sizeof(FLAC__uint32) || encoder->protected_->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)) |
| 598 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 599 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 600 | if(encoder->protected_->streamable_subset) { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 601 | /*@@@ add check for blocksize here */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 602 | if(encoder->protected_->bits_per_sample != 8 && encoder->protected_->bits_per_sample != 12 && encoder->protected_->bits_per_sample != 16 && encoder->protected_->bits_per_sample != 20 && encoder->protected_->bits_per_sample != 24) |
| 603 | return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
| 604 | if(encoder->protected_->sample_rate > 655350) |
| 605 | return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 608 | if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 609 | encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1; |
| 610 | if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order) |
| 611 | encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 612 | |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 613 | /* validate metadata */ |
| 614 | if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) |
| 615 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 616 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 617 | if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 618 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 619 | else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) { |
Josh Coalson | 0833f34 | 2002-07-15 05:31:55 +0000 | [diff] [blame] | 620 | if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table)) |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 621 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 622 | } |
| 623 | } |
| 624 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 625 | encoder->private_->input_capacity = 0; |
| 626 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 627 | encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0; |
| 628 | encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 629 | } |
| 630 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 631 | encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0; |
| 632 | encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 633 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 634 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 635 | encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0; |
| 636 | encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0; |
| 637 | encoder->private_->best_subframe[i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 638 | } |
| 639 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 640 | encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0; |
| 641 | encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0; |
| 642 | encoder->private_->best_subframe_mid_side[i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 643 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 644 | encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0; |
| 645 | encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0; |
| 646 | encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0; |
| 647 | encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize; |
| 648 | encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5); |
| 649 | if(encoder->private_->loose_mid_side_stereo_frames == 0) |
| 650 | encoder->private_->loose_mid_side_stereo_frames = 1; |
| 651 | encoder->private_->loose_mid_side_stereo_frame_count = 0; |
| 652 | encoder->private_->current_sample_number = 0; |
| 653 | encoder->private_->current_frame_number = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 654 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 655 | encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30); |
| 656 | encoder->private_->use_wide_by_order = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(max(encoder->protected_->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */ |
| 657 | encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 658 | |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 659 | /* |
| 660 | * get the CPU info and set the function pointers |
| 661 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 662 | FLAC__cpu_info(&encoder->private_->cpuinfo); |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 663 | /* first default to the non-asm routines */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 664 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation; |
| 665 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor; |
| 666 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients; |
| 667 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients; |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 668 | /* now override with asm where appropriate */ |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 669 | #ifndef FLAC__NO_ASM |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 670 | if(encoder->private_->cpuinfo.use_asm) { |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 671 | #ifdef FLAC__CPU_IA32 |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 672 | FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 673 | #ifdef FLAC__HAS_NASM |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 674 | if(0 && encoder->private_->cpuinfo.data.ia32.sse) { |
| 675 | if(encoder->protected_->max_lpc_order < 4) |
| 676 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4; |
| 677 | else if(encoder->protected_->max_lpc_order < 8) |
| 678 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8; |
| 679 | else if(encoder->protected_->max_lpc_order < 12) |
| 680 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 681 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 682 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 683 | } |
Josh Coalson | 395938e | 2001-11-15 21:53:25 +0000 | [diff] [blame] | 684 | else if(encoder->private_->cpuinfo.data.ia32._3dnow) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 685 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 686 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 687 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
| 688 | if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov) |
| 689 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov; |
| 690 | if(encoder->private_->cpuinfo.data.ia32.mmx) { |
| 691 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 692 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 693 | } |
| 694 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 695 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 696 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 697 | } |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 698 | #endif |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 699 | #endif |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 700 | } |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 701 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 702 | /* finally override based on wide-ness if necessary */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 703 | if(encoder->private_->use_wide_by_block) { |
| 704 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 705 | } |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 706 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 707 | /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 708 | encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding; |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 709 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 710 | if(!resize_buffers_(encoder, encoder->protected_->blocksize)) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 711 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 712 | return encoder->protected_->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 713 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 714 | |
| 715 | if(!FLAC__bitbuffer_init(encoder->private_->frame)) |
| 716 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 717 | |
| 718 | /* |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 719 | * Set up the verify stuff if necessary |
| 720 | */ |
| 721 | if(encoder->protected_->verify) { |
| 722 | /* |
| 723 | * First, set up the fifo which will hold the |
| 724 | * original signal to compare against |
| 725 | */ |
| 726 | encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize; |
| 727 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 728 | if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) |
| 729 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 730 | } |
| 731 | encoder->private_->verify.input_fifo.tail = 0; |
| 732 | |
| 733 | /* |
| 734 | * Now set up a stream decoder for verification |
| 735 | */ |
| 736 | encoder->private_->verify.decoder = FLAC__stream_decoder_new(); |
| 737 | if(0 == encoder->private_->verify.decoder) |
| 738 | return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 739 | |
| 740 | FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_); |
| 741 | FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_); |
| 742 | FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_); |
| 743 | FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_); |
| 744 | FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder); |
| 745 | if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA) |
| 746 | return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 747 | } |
Josh Coalson | 589f8c7 | 2002-08-07 23:54:55 +0000 | [diff] [blame] | 748 | encoder->private_->verify.error_stats.absolute_sample = 0; |
| 749 | encoder->private_->verify.error_stats.frame_number = 0; |
| 750 | encoder->private_->verify.error_stats.channel = 0; |
| 751 | encoder->private_->verify.error_stats.sample = 0; |
| 752 | encoder->private_->verify.error_stats.expected = 0; |
| 753 | encoder->private_->verify.error_stats.got = 0; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 754 | |
| 755 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 756 | * write the stream header |
| 757 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 758 | if(encoder->protected_->verify) |
| 759 | encoder->private_->verify.state_hint = ENCODER_IN_MAGIC; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 760 | if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 761 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 762 | if(!write_bitbuffer_(encoder, 0)) { |
| 763 | /* the above function sets the state for us in case of an error */ |
| 764 | return encoder->protected_->state; |
| 765 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 766 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 767 | /* |
| 768 | * write the STREAMINFO metadata block |
| 769 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 770 | if(encoder->protected_->verify) |
| 771 | encoder->private_->verify.state_hint = ENCODER_IN_METADATA; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 772 | encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO; |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 773 | encoder->private_->metadata.is_last = (encoder->protected_->num_metadata_blocks == 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 774 | encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 775 | encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */ |
| 776 | encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize; |
| 777 | encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 778 | encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 779 | encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate; |
| 780 | encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels; |
| 781 | encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample; |
| 782 | encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */ |
| 783 | memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */ |
| 784 | MD5Init(&encoder->private_->md5context); |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 785 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) { |
| 786 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 787 | return false; |
| 788 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 789 | if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 790 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 791 | if(!write_bitbuffer_(encoder, 0)) { |
| 792 | /* the above function sets the state for us in case of an error */ |
| 793 | return encoder->protected_->state; |
| 794 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 795 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 796 | /* |
| 797 | * Now that the STREAMINFO block is written, we can init this to an |
| 798 | * absurdly-high value... |
| 799 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 800 | encoder->private_->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] | 801 | /* ... and clear this to 0 */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 802 | encoder->private_->metadata.data.stream_info.total_samples = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 803 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 804 | /* |
| 805 | * write the user's metadata blocks |
| 806 | */ |
| 807 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 808 | encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1); |
| 809 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) { |
| 810 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 811 | return false; |
| 812 | } |
| 813 | if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) |
| 814 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 815 | if(!write_bitbuffer_(encoder, 0)) { |
| 816 | /* the above function sets the state for us in case of an error */ |
| 817 | return encoder->protected_->state; |
| 818 | } |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 821 | if(encoder->protected_->verify) |
| 822 | encoder->private_->verify.state_hint = ENCODER_IN_AUDIO; |
| 823 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 824 | return encoder->protected_->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 827 | void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 828 | { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 829 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 830 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 831 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 832 | return; |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 833 | |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 834 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) { |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 835 | if(encoder->private_->current_sample_number != 0) { |
| 836 | encoder->protected_->blocksize = encoder->private_->current_sample_number; |
| 837 | process_frame_(encoder, true); /* true => is last frame */ |
| 838 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 839 | } |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 840 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 841 | MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context); |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 842 | |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 843 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) { |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 844 | encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data); |
| 845 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 846 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 847 | if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder) |
| 848 | FLAC__stream_decoder_finish(encoder->private_->verify.decoder); |
| 849 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 850 | free_(encoder); |
| 851 | set_defaults_(encoder); |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 852 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 853 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 856 | FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 857 | { |
| 858 | FLAC__ASSERT(0 != encoder); |
| 859 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 860 | return false; |
| 861 | encoder->protected_->verify = value; |
| 862 | return true; |
| 863 | } |
| 864 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 865 | FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 866 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 867 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 868 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 869 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 870 | encoder->protected_->streamable_subset = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 871 | return true; |
| 872 | } |
| 873 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 874 | FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 875 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 876 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 877 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 878 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 879 | encoder->protected_->do_mid_side_stereo = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 880 | return true; |
| 881 | } |
| 882 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 883 | FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 884 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 885 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 886 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 887 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 888 | encoder->protected_->loose_mid_side_stereo = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 889 | return true; |
| 890 | } |
| 891 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 892 | FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 893 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 894 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 895 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 896 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 897 | encoder->protected_->channels = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 898 | return true; |
| 899 | } |
| 900 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 901 | FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 902 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 903 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 904 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 905 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 906 | encoder->protected_->bits_per_sample = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 907 | return true; |
| 908 | } |
| 909 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 910 | FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 911 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 912 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 913 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 914 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 915 | encoder->protected_->sample_rate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 916 | return true; |
| 917 | } |
| 918 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 919 | FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 920 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 921 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 922 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 923 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 924 | encoder->protected_->blocksize = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 925 | return true; |
| 926 | } |
| 927 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 928 | FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 929 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 930 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 931 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 932 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 933 | encoder->protected_->max_lpc_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 934 | return true; |
| 935 | } |
| 936 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 937 | FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 938 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 939 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 940 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 941 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 942 | encoder->protected_->qlp_coeff_precision = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 943 | return true; |
| 944 | } |
| 945 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 946 | FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 947 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 948 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 949 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 950 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 951 | encoder->protected_->do_qlp_coeff_prec_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 952 | return true; |
| 953 | } |
| 954 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 955 | FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 956 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 957 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 958 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 959 | return false; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 960 | #if 0 |
| 961 | /*@@@ deprecated: */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 962 | encoder->protected_->do_escape_coding = value; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 963 | #else |
| 964 | (void)value; |
| 965 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 966 | return true; |
| 967 | } |
| 968 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 969 | FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 970 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 971 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 972 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 973 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 974 | encoder->protected_->do_exhaustive_model_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 975 | return true; |
| 976 | } |
| 977 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 978 | FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 979 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 980 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 981 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 982 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 983 | encoder->protected_->min_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 984 | return true; |
| 985 | } |
| 986 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 987 | FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 988 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 989 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 990 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 991 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 992 | encoder->protected_->max_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 993 | return true; |
| 994 | } |
| 995 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 996 | FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 997 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 998 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 999 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1000 | return false; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 1001 | #if 0 |
| 1002 | /*@@@ deprecated: */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1003 | encoder->protected_->rice_parameter_search_dist = value; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 1004 | #else |
| 1005 | (void)value; |
| 1006 | #endif |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1007 | return true; |
| 1008 | } |
| 1009 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 1010 | FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1011 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1012 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1013 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1014 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1015 | encoder->protected_->total_samples_estimate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1016 | return true; |
| 1017 | } |
| 1018 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 1019 | FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1020 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1021 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1022 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1023 | return false; |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 1024 | encoder->protected_->metadata = metadata; |
| 1025 | encoder->protected_->num_metadata_blocks = num_blocks; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1026 | return true; |
| 1027 | } |
| 1028 | |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 1029 | FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1030 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1031 | FLAC__ASSERT(0 != encoder); |
| 1032 | FLAC__ASSERT(0 != value); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1033 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1034 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1035 | encoder->private_->write_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1036 | return true; |
| 1037 | } |
| 1038 | |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 1039 | FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1040 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1041 | FLAC__ASSERT(0 != encoder); |
| 1042 | FLAC__ASSERT(0 != value); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1043 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1044 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1045 | encoder->private_->metadata_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1046 | return true; |
| 1047 | } |
| 1048 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 1049 | FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1050 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1051 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1052 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1053 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1054 | encoder->private_->client_data = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1055 | return true; |
| 1056 | } |
| 1057 | |
| 1058 | FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1059 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1060 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1061 | return encoder->protected_->state; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1064 | FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder) |
| 1065 | { |
| 1066 | FLAC__ASSERT(0 != encoder); |
| 1067 | if(encoder->protected_->verify) |
| 1068 | return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder); |
| 1069 | else |
| 1070 | return FLAC__STREAM_DECODER_UNINITIALIZED; |
| 1071 | } |
| 1072 | |
Josh Coalson | 589f8c7 | 2002-08-07 23:54:55 +0000 | [diff] [blame] | 1073 | void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got) |
| 1074 | { |
| 1075 | FLAC__ASSERT(0 != encoder); |
| 1076 | if(0 != absolute_sample) |
| 1077 | *absolute_sample = encoder->private_->verify.error_stats.absolute_sample; |
| 1078 | if(0 != frame_number) |
| 1079 | *frame_number = encoder->private_->verify.error_stats.frame_number; |
| 1080 | if(0 != channel) |
| 1081 | *channel = encoder->private_->verify.error_stats.channel; |
| 1082 | if(0 != sample) |
| 1083 | *sample = encoder->private_->verify.error_stats.sample; |
| 1084 | if(0 != expected) |
| 1085 | *expected = encoder->private_->verify.error_stats.expected; |
| 1086 | if(0 != got) |
| 1087 | *got = encoder->private_->verify.error_stats.got; |
| 1088 | } |
| 1089 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1090 | FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder) |
| 1091 | { |
| 1092 | FLAC__ASSERT(0 != encoder); |
| 1093 | return encoder->protected_->verify; |
| 1094 | } |
| 1095 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1096 | FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1097 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1098 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1099 | return encoder->protected_->streamable_subset; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1102 | FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1103 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1104 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1105 | return encoder->protected_->do_mid_side_stereo; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1108 | FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1109 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1110 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1111 | return encoder->protected_->loose_mid_side_stereo; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1114 | unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1115 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1116 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1117 | return encoder->protected_->channels; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1120 | unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1121 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1122 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1123 | return encoder->protected_->bits_per_sample; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1126 | unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1127 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1128 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1129 | return encoder->protected_->sample_rate; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1132 | unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1133 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1134 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1135 | return encoder->protected_->blocksize; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1138 | unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1139 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1140 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1141 | return encoder->protected_->max_lpc_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1144 | unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1145 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1146 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1147 | return encoder->protected_->qlp_coeff_precision; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1150 | FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1151 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1152 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1153 | return encoder->protected_->do_qlp_coeff_prec_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1156 | FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder) |
| 1157 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1158 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1159 | return encoder->protected_->do_escape_coding; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1162 | FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1163 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1164 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1165 | return encoder->protected_->do_exhaustive_model_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1168 | unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1169 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1170 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1171 | return encoder->protected_->min_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1174 | unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1175 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1176 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1177 | return encoder->protected_->max_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1180 | unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1181 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1182 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1183 | return encoder->protected_->rice_parameter_search_dist; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Josh Coalson | 3a7b2c9 | 2002-08-02 07:38:20 +0000 | [diff] [blame] | 1186 | FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder) |
| 1187 | { |
| 1188 | FLAC__ASSERT(0 != encoder); |
| 1189 | return encoder->protected_->total_samples_estimate; |
| 1190 | } |
| 1191 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1192 | FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1193 | { |
| 1194 | unsigned i, j, channel; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1195 | FLAC__int32 x, mid, side; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1196 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1197 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1198 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1199 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1200 | |
| 1201 | j = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1202 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1203 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1204 | if(encoder->protected_->verify) |
| 1205 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1206 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1207 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1208 | x = mid = side = buffer[0][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1209 | encoder->private_->integer_signal[0][i] = x; |
| 1210 | encoder->private_->real_signal[0][i] = (FLAC__real)x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1211 | x = buffer[1][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1212 | encoder->private_->integer_signal[1][i] = x; |
| 1213 | encoder->private_->real_signal[1][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1214 | mid += x; |
| 1215 | side -= x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1216 | mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1217 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 1218 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
| 1219 | encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side; |
| 1220 | encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid; |
| 1221 | encoder->private_->current_sample_number++; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1222 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1223 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1224 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1225 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1226 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1227 | } while(j < samples); |
| 1228 | } |
| 1229 | else { |
| 1230 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1231 | if(encoder->protected_->verify) |
| 1232 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1233 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1234 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1235 | for(channel = 0; channel < channels; channel++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1236 | x = buffer[channel][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1237 | encoder->private_->integer_signal[channel][i] = x; |
| 1238 | encoder->private_->real_signal[channel][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1239 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1240 | encoder->private_->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1241 | } |
| 1242 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1243 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1244 | return false; |
| 1245 | } |
| 1246 | } while(j < samples); |
| 1247 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1248 | |
| 1249 | return true; |
| 1250 | } |
| 1251 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1252 | FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1253 | { |
| 1254 | unsigned i, j, k, channel; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1255 | FLAC__int32 x, mid, side; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1256 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1257 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1258 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1259 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1260 | |
| 1261 | j = k = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1262 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1263 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1264 | if(encoder->protected_->verify) |
| 1265 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1266 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1267 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1268 | x = mid = side = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1269 | encoder->private_->integer_signal[0][i] = x; |
| 1270 | encoder->private_->real_signal[0][i] = (FLAC__real)x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1271 | x = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1272 | encoder->private_->integer_signal[1][i] = x; |
| 1273 | encoder->private_->real_signal[1][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1274 | mid += x; |
| 1275 | side -= x; |
| 1276 | mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1277 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 1278 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
| 1279 | encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side; |
| 1280 | encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid; |
| 1281 | encoder->private_->current_sample_number++; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1282 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1283 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1284 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1285 | return false; |
| 1286 | } |
| 1287 | } while(j < samples); |
| 1288 | } |
| 1289 | else { |
| 1290 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1291 | if(encoder->protected_->verify) |
| 1292 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1293 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1294 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1295 | for(channel = 0; channel < channels; channel++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1296 | x = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1297 | encoder->private_->integer_signal[channel][i] = x; |
| 1298 | encoder->private_->real_signal[channel][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1299 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1300 | encoder->private_->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1301 | } |
| 1302 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1303 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1304 | return false; |
| 1305 | } |
| 1306 | } while(j < samples); |
| 1307 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1308 | |
| 1309 | return true; |
| 1310 | } |
| 1311 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1312 | /*********************************************************************** |
| 1313 | * |
| 1314 | * Private class methods |
| 1315 | * |
| 1316 | ***********************************************************************/ |
| 1317 | |
| 1318 | void set_defaults_(FLAC__StreamEncoder *encoder) |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1319 | { |
| 1320 | FLAC__ASSERT(0 != encoder); |
| 1321 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1322 | encoder->protected_->verify = false; |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1323 | encoder->protected_->streamable_subset = true; |
| 1324 | encoder->protected_->do_mid_side_stereo = false; |
| 1325 | encoder->protected_->loose_mid_side_stereo = false; |
| 1326 | encoder->protected_->channels = 2; |
| 1327 | encoder->protected_->bits_per_sample = 16; |
| 1328 | encoder->protected_->sample_rate = 44100; |
| 1329 | encoder->protected_->blocksize = 1152; |
| 1330 | encoder->protected_->max_lpc_order = 0; |
| 1331 | encoder->protected_->qlp_coeff_precision = 0; |
| 1332 | encoder->protected_->do_qlp_coeff_prec_search = false; |
| 1333 | encoder->protected_->do_exhaustive_model_search = false; |
| 1334 | encoder->protected_->do_escape_coding = false; |
| 1335 | encoder->protected_->min_residual_partition_order = 0; |
| 1336 | encoder->protected_->max_residual_partition_order = 0; |
| 1337 | encoder->protected_->rice_parameter_search_dist = 0; |
| 1338 | encoder->protected_->total_samples_estimate = 0; |
| 1339 | encoder->protected_->metadata = 0; |
| 1340 | encoder->protected_->num_metadata_blocks = 0; |
| 1341 | |
| 1342 | encoder->private_->write_callback = 0; |
| 1343 | encoder->private_->metadata_callback = 0; |
| 1344 | encoder->private_->client_data = 0; |
| 1345 | } |
| 1346 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1347 | void free_(FLAC__StreamEncoder *encoder) |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1348 | { |
| 1349 | unsigned i, channel; |
| 1350 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1351 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1352 | for(i = 0; i < encoder->protected_->channels; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1353 | if(0 != encoder->private_->integer_signal_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1354 | free(encoder->private_->integer_signal_unaligned[i]); |
| 1355 | encoder->private_->integer_signal_unaligned[i] = 0; |
| 1356 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1357 | if(0 != encoder->private_->real_signal_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1358 | free(encoder->private_->real_signal_unaligned[i]); |
| 1359 | encoder->private_->real_signal_unaligned[i] = 0; |
| 1360 | } |
| 1361 | } |
| 1362 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1363 | if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1364 | free(encoder->private_->integer_signal_mid_side_unaligned[i]); |
| 1365 | encoder->private_->integer_signal_mid_side_unaligned[i] = 0; |
| 1366 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1367 | if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1368 | free(encoder->private_->real_signal_mid_side_unaligned[i]); |
| 1369 | encoder->private_->real_signal_mid_side_unaligned[i] = 0; |
| 1370 | } |
| 1371 | } |
| 1372 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 1373 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1374 | if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1375 | free(encoder->private_->residual_workspace_unaligned[channel][i]); |
| 1376 | encoder->private_->residual_workspace_unaligned[channel][i] = 0; |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | for(channel = 0; channel < 2; channel++) { |
| 1381 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1382 | if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1383 | free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]); |
| 1384 | encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0; |
| 1385 | } |
| 1386 | } |
| 1387 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1388 | if(0 != encoder->private_->abs_residual_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1389 | free(encoder->private_->abs_residual_unaligned); |
| 1390 | encoder->private_->abs_residual_unaligned = 0; |
| 1391 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1392 | if(0 != encoder->private_->abs_residual_partition_sums_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1393 | free(encoder->private_->abs_residual_partition_sums_unaligned); |
| 1394 | encoder->private_->abs_residual_partition_sums_unaligned = 0; |
| 1395 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1396 | if(0 != encoder->private_->raw_bits_per_partition_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1397 | free(encoder->private_->raw_bits_per_partition_unaligned); |
| 1398 | encoder->private_->raw_bits_per_partition_unaligned = 0; |
| 1399 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1400 | if(encoder->protected_->verify) { |
| 1401 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 1402 | if(0 != encoder->private_->verify.input_fifo.data[i]) { |
| 1403 | free(encoder->private_->verify.input_fifo.data[i]); |
| 1404 | encoder->private_->verify.input_fifo.data[i] = 0; |
| 1405 | } |
| 1406 | } |
| 1407 | } |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1408 | FLAC__bitbuffer_free(encoder->private_->frame); |
| 1409 | } |
| 1410 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1411 | FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1412 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1413 | FLAC__bool ok; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1414 | unsigned i, channel; |
| 1415 | |
| 1416 | FLAC__ASSERT(new_size > 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1417 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 1418 | FLAC__ASSERT(encoder->private_->current_sample_number == 0); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1419 | |
| 1420 | /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1421 | if(new_size <= encoder->private_->input_capacity) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1422 | return true; |
| 1423 | |
| 1424 | ok = true; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1425 | |
| 1426 | /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx() requires that the input arrays (in our case the integer signals) have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */ |
| 1427 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1428 | for(i = 0; ok && i < encoder->protected_->channels; i++) { |
| 1429 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]); |
| 1430 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]); |
| 1431 | memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4); |
| 1432 | encoder->private_->integer_signal[i] += 4; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1433 | } |
| 1434 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1435 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]); |
| 1436 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]); |
| 1437 | memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4); |
| 1438 | encoder->private_->integer_signal_mid_side[i] += 4; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1439 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1440 | for(channel = 0; ok && channel < encoder->protected_->channels; channel++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1441 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1442 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | for(channel = 0; ok && channel < 2; channel++) { |
| 1446 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1447 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1448 | } |
| 1449 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1450 | ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual); |
| 1451 | if(encoder->private_->precompute_partition_sums || encoder->protected_->do_escape_coding) /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */ |
| 1452 | ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums); |
| 1453 | if(encoder->protected_->do_escape_coding) |
| 1454 | ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1455 | |
| 1456 | if(ok) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1457 | encoder->private_->input_capacity = new_size; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1458 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1459 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1460 | |
| 1461 | return ok; |
| 1462 | } |
| 1463 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1464 | FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples) |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1465 | { |
| 1466 | const FLAC__byte *buffer; |
| 1467 | unsigned bytes; |
| 1468 | |
| 1469 | FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame)); |
| 1470 | |
| 1471 | FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes); |
| 1472 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1473 | if(encoder->protected_->verify) { |
| 1474 | encoder->private_->verify.output.data = buffer; |
| 1475 | encoder->private_->verify.output.bytes = bytes; |
| 1476 | if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) { |
| 1477 | encoder->private_->verify.needs_magic_hack = true; |
| 1478 | } |
| 1479 | else { |
| 1480 | if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) { |
| 1481 | FLAC__bitbuffer_release_buffer(encoder->private_->frame); |
| 1482 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) |
| 1483 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 1484 | return false; |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 1490 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING; |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1491 | return false; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1492 | } |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1493 | |
| 1494 | FLAC__bitbuffer_release_buffer(encoder->private_->frame); |
| 1495 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1496 | if(samples > 0) { |
| 1497 | encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize); |
| 1498 | encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize); |
| 1499 | } |
| 1500 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1501 | return true; |
| 1502 | } |
| 1503 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1504 | FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1505 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1506 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1507 | |
| 1508 | /* |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 1509 | * Accumulate raw signal to the MD5 signature |
| 1510 | */ |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1511 | if(!FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1512 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 1513 | return false; |
| 1514 | } |
| 1515 | |
| 1516 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1517 | * Process the frame header and subframes into the frame bitbuffer |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1518 | */ |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1519 | if(!process_subframes_(encoder, is_last_frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1520 | /* 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] | 1521 | return false; |
| 1522 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1523 | |
| 1524 | /* |
| 1525 | * Zero-pad the frame to a byte_boundary |
| 1526 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1527 | if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1528 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1529 | return false; |
| 1530 | } |
| 1531 | |
| 1532 | /* |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1533 | * CRC-16 the whole thing |
| 1534 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1535 | FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame)); |
| 1536 | FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN); |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1537 | |
| 1538 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1539 | * Write it |
| 1540 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1541 | if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) { |
| 1542 | /* 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] | 1543 | return false; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
| 1547 | * Get ready for the next frame |
| 1548 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1549 | encoder->private_->current_sample_number = 0; |
| 1550 | encoder->private_->current_frame_number++; |
| 1551 | encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1552 | |
| 1553 | return true; |
| 1554 | } |
| 1555 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1556 | FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1557 | { |
| 1558 | FLAC__FrameHeader frame_header; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1559 | unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1560 | FLAC__bool do_independent, do_mid_side, precompute_partition_sums; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1561 | |
| 1562 | /* |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1563 | * Calculate the min,max Rice partition orders |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1564 | */ |
| 1565 | if(is_last_frame) { |
| 1566 | max_partition_order = 0; |
| 1567 | } |
| 1568 | else { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 1569 | max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize); |
| 1570 | max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1571 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1572 | min_partition_order = min(min_partition_order, max_partition_order); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1573 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1574 | precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1575 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1576 | /* |
| 1577 | * Setup the frame |
| 1578 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1579 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1580 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1581 | return false; |
| 1582 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1583 | frame_header.blocksize = encoder->protected_->blocksize; |
| 1584 | frame_header.sample_rate = encoder->protected_->sample_rate; |
| 1585 | frame_header.channels = encoder->protected_->channels; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1586 | frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1587 | frame_header.bits_per_sample = encoder->protected_->bits_per_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1588 | frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1589 | frame_header.number.frame_number = encoder->private_->current_frame_number; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1590 | |
| 1591 | /* |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1592 | * Figure out what channel assignments to try |
| 1593 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1594 | if(encoder->protected_->do_mid_side_stereo) { |
| 1595 | if(encoder->protected_->loose_mid_side_stereo) { |
| 1596 | if(encoder->private_->loose_mid_side_stereo_frame_count == 0) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1597 | do_independent = true; |
| 1598 | do_mid_side = true; |
| 1599 | } |
| 1600 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1601 | do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1602 | do_mid_side = !do_independent; |
| 1603 | } |
| 1604 | } |
| 1605 | else { |
| 1606 | do_independent = true; |
| 1607 | do_mid_side = true; |
| 1608 | } |
| 1609 | } |
| 1610 | else { |
| 1611 | do_independent = true; |
| 1612 | do_mid_side = false; |
| 1613 | } |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1614 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1615 | FLAC__ASSERT(do_independent || do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1616 | |
| 1617 | /* |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1618 | * Check for wasted bits; set effective bps for each subframe |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1619 | */ |
| 1620 | if(do_independent) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1621 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 1622 | const unsigned w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1623 | encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w; |
| 1624 | encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1625 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1626 | } |
| 1627 | if(do_mid_side) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1628 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1629 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 1630 | const unsigned w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1631 | encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w; |
| 1632 | encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1633 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1637 | * First do a normal encoding pass of each independent channel |
| 1638 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1639 | if(do_independent) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1640 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 1641 | if(! |
| 1642 | process_subframe_( |
| 1643 | encoder, |
| 1644 | min_partition_order, |
| 1645 | max_partition_order, |
| 1646 | precompute_partition_sums, |
| 1647 | false, |
| 1648 | &frame_header, |
| 1649 | encoder->private_->subframe_bps[channel], |
| 1650 | encoder->private_->integer_signal[channel], |
| 1651 | encoder->private_->real_signal[channel], |
| 1652 | encoder->private_->subframe_workspace_ptr[channel], |
| 1653 | encoder->private_->partitioned_rice_contents_workspace_ptr[channel], |
| 1654 | encoder->private_->residual_workspace[channel], |
| 1655 | encoder->private_->best_subframe+channel, |
| 1656 | encoder->private_->best_subframe_bits+channel |
| 1657 | ) |
| 1658 | ) |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1659 | return false; |
| 1660 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | /* |
| 1664 | * Now do mid and side channels if requested |
| 1665 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1666 | if(do_mid_side) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1667 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1668 | |
| 1669 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 1670 | if(! |
| 1671 | process_subframe_( |
| 1672 | encoder, |
| 1673 | min_partition_order, |
| 1674 | max_partition_order, |
| 1675 | precompute_partition_sums, |
| 1676 | false, |
| 1677 | &frame_header, |
| 1678 | encoder->private_->subframe_bps_mid_side[channel], |
| 1679 | encoder->private_->integer_signal_mid_side[channel], |
| 1680 | encoder->private_->real_signal_mid_side[channel], |
| 1681 | encoder->private_->subframe_workspace_ptr_mid_side[channel], |
| 1682 | encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel], |
| 1683 | encoder->private_->residual_workspace_mid_side[channel], |
| 1684 | encoder->private_->best_subframe_mid_side+channel, |
| 1685 | encoder->private_->best_subframe_bits_mid_side+channel |
| 1686 | ) |
| 1687 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1688 | return false; |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | /* |
| 1693 | * Compose the frame bitbuffer |
| 1694 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1695 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1696 | unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ |
| 1697 | 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] | 1698 | FLAC__ChannelAssignment channel_assignment; |
| 1699 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1700 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1701 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1702 | if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) { |
| 1703 | channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1704 | } |
| 1705 | else { |
| 1706 | unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */ |
| 1707 | unsigned min_bits; |
| 1708 | FLAC__ChannelAssignment ca; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1709 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1710 | FLAC__ASSERT(do_independent && do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1711 | |
| 1712 | /* We have to figure out which channel assignent results in the smallest frame */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1713 | bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1]; |
| 1714 | bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 1715 | bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 1716 | bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1]; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1717 | |
| 1718 | for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) { |
| 1719 | if(bits[ca] < min_bits) { |
| 1720 | min_bits = bits[ca]; |
| 1721 | channel_assignment = ca; |
| 1722 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1726 | frame_header.channel_assignment = channel_assignment; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1727 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1728 | if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1729 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1730 | return false; |
| 1731 | } |
| 1732 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1733 | switch(channel_assignment) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1734 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1735 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]]; |
| 1736 | right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1737 | break; |
| 1738 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1739 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]]; |
| 1740 | right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1741 | break; |
| 1742 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1743 | left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
| 1744 | right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1745 | break; |
| 1746 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1747 | left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]]; |
| 1748 | right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1749 | break; |
| 1750 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1751 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1752 | } |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1753 | |
| 1754 | switch(channel_assignment) { |
| 1755 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1756 | left_bps = encoder->private_->subframe_bps [0]; |
| 1757 | right_bps = encoder->private_->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1758 | break; |
| 1759 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1760 | left_bps = encoder->private_->subframe_bps [0]; |
| 1761 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1762 | break; |
| 1763 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1764 | left_bps = encoder->private_->subframe_bps_mid_side[1]; |
| 1765 | right_bps = encoder->private_->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1766 | break; |
| 1767 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1768 | left_bps = encoder->private_->subframe_bps_mid_side[0]; |
| 1769 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1770 | break; |
| 1771 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1772 | FLAC__ASSERT(0); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | /* note that encoder_add_subframe_ sets the state for us in case of an error */ |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1776 | if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame)) |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1777 | return false; |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1778 | if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame)) |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1779 | return false; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1780 | } |
| 1781 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1782 | if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1783 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1784 | return false; |
| 1785 | } |
| 1786 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1787 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1788 | if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1789 | /* the above function sets the state for us in case of an error */ |
| 1790 | return false; |
| 1791 | } |
| 1792 | } |
| 1793 | } |
| 1794 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1795 | if(encoder->protected_->loose_mid_side_stereo) { |
| 1796 | encoder->private_->loose_mid_side_stereo_frame_count++; |
| 1797 | if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames) |
| 1798 | encoder->private_->loose_mid_side_stereo_frame_count = 0; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1801 | encoder->private_->last_channel_assignment = frame_header.channel_assignment; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1802 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1803 | return true; |
| 1804 | } |
| 1805 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 1806 | FLAC__bool process_subframe_( |
| 1807 | FLAC__StreamEncoder *encoder, |
| 1808 | unsigned min_partition_order, |
| 1809 | unsigned max_partition_order, |
| 1810 | FLAC__bool precompute_partition_sums, |
| 1811 | FLAC__bool verbatim_only, |
| 1812 | const FLAC__FrameHeader *frame_header, |
| 1813 | unsigned subframe_bps, |
| 1814 | const FLAC__int32 integer_signal[], |
| 1815 | const FLAC__real real_signal[], |
| 1816 | FLAC__Subframe *subframe[2], |
| 1817 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2], |
| 1818 | FLAC__int32 *residual[2], |
| 1819 | unsigned *best_subframe, |
| 1820 | unsigned *best_bits |
| 1821 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1822 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1823 | FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 1824 | FLAC__real lpc_residual_bits_per_sample; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1825 | FLAC__real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->protected_->max_lpc_order might be less; some asm routines need all the space */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1826 | FLAC__real lpc_error[FLAC__MAX_LPC_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1827 | unsigned min_lpc_order, max_lpc_order, lpc_order; |
| 1828 | unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order; |
| 1829 | unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision; |
| 1830 | unsigned rice_parameter; |
| 1831 | unsigned _candidate_bits, _best_bits; |
| 1832 | unsigned _best_subframe; |
| 1833 | |
| 1834 | /* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 1835 | _best_subframe = 0; |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1836 | _best_bits = 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] | 1837 | |
| 1838 | if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { |
| 1839 | /* check for constant subframe */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1840 | guess_fixed_order = encoder->private_->local_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] | 1841 | if(fixed_residual_bits_per_sample[1] == 0.0) { |
| 1842 | /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */ |
| 1843 | unsigned i, signal_is_constant = true; |
| 1844 | for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) { |
| 1845 | if(integer_signal[0] != integer_signal[i]) { |
| 1846 | signal_is_constant = false; |
| 1847 | break; |
| 1848 | } |
| 1849 | } |
| 1850 | if(signal_is_constant) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1851 | _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1852 | if(_candidate_bits < _best_bits) { |
| 1853 | _best_subframe = !_best_subframe; |
| 1854 | _best_bits = _candidate_bits; |
| 1855 | } |
| 1856 | } |
| 1857 | } |
| 1858 | else { |
| 1859 | /* encode fixed */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1860 | if(encoder->protected_->do_exhaustive_model_search) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1861 | min_fixed_order = 0; |
| 1862 | max_fixed_order = FLAC__MAX_FIXED_ORDER; |
| 1863 | } |
| 1864 | else { |
| 1865 | min_fixed_order = max_fixed_order = guess_fixed_order; |
| 1866 | } |
| 1867 | for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1868 | if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1869 | continue; /* don't even try */ |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 1870 | 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] | 1871 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 1872 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1873 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1874 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1875 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1876 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1877 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1878 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1879 | } |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 1880 | _candidate_bits = |
| 1881 | evaluate_fixed_subframe_( |
| 1882 | encoder, |
| 1883 | integer_signal, |
| 1884 | residual[!_best_subframe], |
| 1885 | encoder->private_->abs_residual, |
| 1886 | encoder->private_->abs_residual_partition_sums, |
| 1887 | encoder->private_->raw_bits_per_partition, |
| 1888 | frame_header->blocksize, |
| 1889 | subframe_bps, |
| 1890 | fixed_order, |
| 1891 | rice_parameter, |
| 1892 | min_partition_order, |
| 1893 | max_partition_order, |
| 1894 | precompute_partition_sums, |
| 1895 | encoder->protected_->do_escape_coding, |
| 1896 | encoder->protected_->rice_parameter_search_dist, |
| 1897 | subframe[!_best_subframe], |
| 1898 | partitioned_rice_contents[!_best_subframe] |
| 1899 | ); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1900 | if(_candidate_bits < _best_bits) { |
| 1901 | _best_subframe = !_best_subframe; |
| 1902 | _best_bits = _candidate_bits; |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | /* encode lpc */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1907 | if(encoder->protected_->max_lpc_order > 0) { |
| 1908 | if(encoder->protected_->max_lpc_order >= frame_header->blocksize) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1909 | max_lpc_order = frame_header->blocksize-1; |
| 1910 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1911 | max_lpc_order = encoder->protected_->max_lpc_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1912 | if(max_lpc_order > 0) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1913 | encoder->private_->local_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] | 1914 | /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */ |
| 1915 | if(autoc[0] != 0.0) { |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1916 | FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1917 | if(encoder->protected_->do_exhaustive_model_search) { |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1918 | min_lpc_order = 1; |
| 1919 | } |
| 1920 | else { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1921 | 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] | 1922 | min_lpc_order = max_lpc_order = guess_lpc_order; |
| 1923 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1924 | if(encoder->protected_->do_qlp_coeff_prec_search) { |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1925 | min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1926 | max_qlp_coeff_precision = min(8*sizeof(FLAC__int32) - subframe_bps - 1 - 2, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1); /* -2 to keep things 32-bit safe */ |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1927 | } |
| 1928 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1929 | min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision; |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1930 | } |
| 1931 | for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) { |
| 1932 | 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 | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1933 | if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps) |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1934 | continue; /* don't even try */ |
| 1935 | 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] | 1936 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1937 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1938 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1939 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1940 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1941 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1942 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1943 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1944 | } |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1945 | for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) { |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 1946 | _candidate_bits = |
| 1947 | evaluate_lpc_subframe_( |
| 1948 | encoder, |
| 1949 | integer_signal, |
| 1950 | residual[!_best_subframe], |
| 1951 | encoder->private_->abs_residual, |
| 1952 | encoder->private_->abs_residual_partition_sums, |
| 1953 | encoder->private_->raw_bits_per_partition, |
| 1954 | encoder->private_->lp_coeff[lpc_order-1], |
| 1955 | frame_header->blocksize, |
| 1956 | subframe_bps, |
| 1957 | lpc_order, |
| 1958 | qlp_coeff_precision, |
| 1959 | rice_parameter, |
| 1960 | min_partition_order, |
| 1961 | max_partition_order, |
| 1962 | precompute_partition_sums, |
| 1963 | encoder->protected_->do_escape_coding, |
| 1964 | encoder->protected_->rice_parameter_search_dist, |
| 1965 | subframe[!_best_subframe], |
| 1966 | partitioned_rice_contents[!_best_subframe] |
| 1967 | ); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1968 | if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 1969 | if(_candidate_bits < _best_bits) { |
| 1970 | _best_subframe = !_best_subframe; |
| 1971 | _best_bits = _candidate_bits; |
| 1972 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1973 | } |
| 1974 | } |
| 1975 | } |
| 1976 | } |
| 1977 | } |
| 1978 | } |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | *best_subframe = _best_subframe; |
| 1983 | *best_bits = _best_bits; |
| 1984 | |
| 1985 | return true; |
| 1986 | } |
| 1987 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 1988 | FLAC__bool add_subframe_( |
| 1989 | FLAC__StreamEncoder *encoder, |
| 1990 | const FLAC__FrameHeader *frame_header, |
| 1991 | unsigned subframe_bps, |
| 1992 | const FLAC__Subframe *subframe, |
| 1993 | FLAC__BitBuffer *frame |
| 1994 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1995 | { |
| 1996 | switch(subframe->type) { |
| 1997 | case FLAC__SUBFRAME_TYPE_CONSTANT: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1998 | if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1999 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2000 | return false; |
| 2001 | } |
| 2002 | break; |
| 2003 | case FLAC__SUBFRAME_TYPE_FIXED: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2004 | if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2005 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2006 | return false; |
| 2007 | } |
| 2008 | break; |
| 2009 | case FLAC__SUBFRAME_TYPE_LPC: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2010 | if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2011 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2012 | return false; |
| 2013 | } |
| 2014 | break; |
| 2015 | case FLAC__SUBFRAME_TYPE_VERBATIM: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2016 | if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2017 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2018 | return false; |
| 2019 | } |
| 2020 | break; |
| 2021 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 2022 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | return true; |
| 2026 | } |
| 2027 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2028 | unsigned evaluate_constant_subframe_( |
| 2029 | const FLAC__int32 signal, |
| 2030 | unsigned subframe_bps, |
| 2031 | FLAC__Subframe *subframe |
| 2032 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2033 | { |
| 2034 | subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 2035 | subframe->data.constant.value = signal; |
| 2036 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2037 | 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] | 2038 | } |
| 2039 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2040 | unsigned evaluate_fixed_subframe_( |
| 2041 | FLAC__StreamEncoder *encoder, |
| 2042 | const FLAC__int32 signal[], |
| 2043 | FLAC__int32 residual[], |
| 2044 | FLAC__uint32 abs_residual[], |
| 2045 | FLAC__uint64 abs_residual_partition_sums[], |
| 2046 | unsigned raw_bits_per_partition[], |
| 2047 | unsigned blocksize, |
| 2048 | unsigned subframe_bps, |
| 2049 | unsigned order, |
| 2050 | unsigned rice_parameter, |
| 2051 | unsigned min_partition_order, |
| 2052 | unsigned max_partition_order, |
| 2053 | FLAC__bool precompute_partition_sums, |
| 2054 | FLAC__bool do_escape_coding, |
| 2055 | unsigned rice_parameter_search_dist, |
| 2056 | FLAC__Subframe *subframe, |
| 2057 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 2058 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2059 | { |
| 2060 | unsigned i, residual_bits; |
| 2061 | const unsigned residual_samples = blocksize - order; |
| 2062 | |
| 2063 | FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual); |
| 2064 | |
| 2065 | subframe->type = FLAC__SUBFRAME_TYPE_FIXED; |
| 2066 | |
| 2067 | subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2068 | subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2069 | subframe->data.fixed.residual = residual; |
| 2070 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2071 | residual_bits = |
| 2072 | find_best_partition_order_( |
| 2073 | encoder->private_, |
| 2074 | residual, |
| 2075 | abs_residual, |
| 2076 | abs_residual_partition_sums, |
| 2077 | raw_bits_per_partition, |
| 2078 | residual_samples, |
| 2079 | order, |
| 2080 | rice_parameter, |
| 2081 | min_partition_order, |
| 2082 | max_partition_order, |
| 2083 | precompute_partition_sums, |
| 2084 | do_escape_coding, |
| 2085 | rice_parameter_search_dist, |
| 2086 | &subframe->data.fixed.entropy_coding_method.data.partitioned_rice |
| 2087 | ); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2088 | |
| 2089 | subframe->data.fixed.order = order; |
| 2090 | for(i = 0; i < order; i++) |
| 2091 | subframe->data.fixed.warmup[i] = signal[i]; |
| 2092 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2093 | 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] | 2094 | } |
| 2095 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2096 | unsigned evaluate_lpc_subframe_( |
| 2097 | FLAC__StreamEncoder *encoder, |
| 2098 | const FLAC__int32 signal[], |
| 2099 | FLAC__int32 residual[], |
| 2100 | FLAC__uint32 abs_residual[], |
| 2101 | FLAC__uint64 abs_residual_partition_sums[], |
| 2102 | unsigned raw_bits_per_partition[], |
| 2103 | const FLAC__real lp_coeff[], |
| 2104 | unsigned blocksize, |
| 2105 | unsigned subframe_bps, |
| 2106 | unsigned order, |
| 2107 | unsigned qlp_coeff_precision, |
| 2108 | unsigned rice_parameter, |
| 2109 | unsigned min_partition_order, |
| 2110 | unsigned max_partition_order, |
| 2111 | FLAC__bool precompute_partition_sums, |
| 2112 | FLAC__bool do_escape_coding, |
| 2113 | unsigned rice_parameter_search_dist, |
| 2114 | FLAC__Subframe *subframe, |
| 2115 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 2116 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2117 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2118 | FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2119 | unsigned i, residual_bits; |
| 2120 | int quantization, ret; |
| 2121 | const unsigned residual_samples = blocksize - order; |
| 2122 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2123 | 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] | 2124 | if(ret != 0) |
| 2125 | return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ |
| 2126 | |
Josh Coalson | 92d4240 | 2001-05-31 20:53:19 +0000 | [diff] [blame] | 2127 | if(subframe_bps <= 16 && qlp_coeff_precision <= 16) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2128 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
Josh Coalson | 92d4240 | 2001-05-31 20:53:19 +0000 | [diff] [blame] | 2129 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2130 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2131 | |
| 2132 | subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 2133 | |
| 2134 | subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2135 | subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2136 | subframe->data.lpc.residual = residual; |
| 2137 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2138 | residual_bits = |
| 2139 | find_best_partition_order_( |
| 2140 | encoder->private_, |
| 2141 | residual, |
| 2142 | abs_residual, |
| 2143 | abs_residual_partition_sums, |
| 2144 | raw_bits_per_partition, |
| 2145 | residual_samples, |
| 2146 | order, |
| 2147 | rice_parameter, |
| 2148 | min_partition_order, |
| 2149 | max_partition_order, |
| 2150 | precompute_partition_sums, |
| 2151 | do_escape_coding, |
| 2152 | rice_parameter_search_dist, |
| 2153 | &subframe->data.fixed.entropy_coding_method.data.partitioned_rice |
| 2154 | ); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2155 | |
| 2156 | subframe->data.lpc.order = order; |
| 2157 | subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 2158 | subframe->data.lpc.quantization_level = quantization; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2159 | memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2160 | for(i = 0; i < order; i++) |
| 2161 | subframe->data.lpc.warmup[i] = signal[i]; |
| 2162 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2163 | 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] | 2164 | } |
| 2165 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2166 | unsigned evaluate_verbatim_subframe_( |
| 2167 | const FLAC__int32 signal[], |
| 2168 | unsigned blocksize, |
| 2169 | unsigned subframe_bps, |
| 2170 | FLAC__Subframe *subframe |
| 2171 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2172 | { |
| 2173 | subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 2174 | |
| 2175 | subframe->data.verbatim.data = signal; |
| 2176 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2177 | 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] | 2178 | } |
| 2179 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2180 | unsigned find_best_partition_order_( |
| 2181 | FLAC__StreamEncoderPrivate *private_, |
| 2182 | const FLAC__int32 residual[], |
| 2183 | FLAC__uint32 abs_residual[], |
| 2184 | FLAC__uint64 abs_residual_partition_sums[], |
| 2185 | unsigned raw_bits_per_partition[], |
| 2186 | unsigned residual_samples, |
| 2187 | unsigned predictor_order, |
| 2188 | unsigned rice_parameter, |
| 2189 | unsigned min_partition_order, |
| 2190 | unsigned max_partition_order, |
| 2191 | FLAC__bool precompute_partition_sums, |
| 2192 | FLAC__bool do_escape_coding, |
| 2193 | unsigned rice_parameter_search_dist, |
| 2194 | FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice |
| 2195 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2196 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2197 | FLAC__int32 r; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2198 | unsigned residual_bits, best_residual_bits = 0; |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 2199 | unsigned residual_sample; |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 2200 | unsigned best_parameters_index = 0; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2201 | const unsigned blocksize = residual_samples + predictor_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2202 | |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2203 | /* compute abs(residual) for use later */ |
| 2204 | for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) { |
| 2205 | r = residual[residual_sample]; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2206 | abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r); |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2209 | max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order); |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2210 | min_partition_order = min(min_partition_order, max_partition_order); |
| 2211 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2212 | if(precompute_partition_sums) { |
| 2213 | int partition_order; |
| 2214 | unsigned sum; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2215 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2216 | precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2217 | |
| 2218 | if(do_escape_coding) |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2219 | precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2220 | |
| 2221 | for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) { |
| 2222 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2223 | if(! |
| 2224 | set_partitioned_rice_with_precompute_( |
| 2225 | residual, |
| 2226 | abs_residual_partition_sums+sum, |
| 2227 | raw_bits_per_partition+sum, |
| 2228 | residual_samples, |
| 2229 | predictor_order, |
| 2230 | rice_parameter, |
| 2231 | rice_parameter_search_dist, |
| 2232 | (unsigned)partition_order, |
| 2233 | do_escape_coding, |
| 2234 | &private_->partitioned_rice_contents_extra[!best_parameters_index], |
| 2235 | &residual_bits |
| 2236 | ) |
| 2237 | ) |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 2238 | #else |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2239 | if(! |
| 2240 | set_partitioned_rice_with_precompute_( |
| 2241 | abs_residual, |
| 2242 | abs_residual_partition_sums+sum, |
| 2243 | raw_bits_per_partition+sum, |
| 2244 | residual_samples, |
| 2245 | predictor_order, |
| 2246 | rice_parameter, |
| 2247 | rice_parameter_search_dist, |
| 2248 | (unsigned)partition_order, |
| 2249 | do_escape_coding, |
| 2250 | &private_->partitioned_rice_contents_extra[!best_parameters_index], |
| 2251 | &residual_bits |
| 2252 | ) |
| 2253 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2254 | #endif |
| 2255 | { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2256 | FLAC__ASSERT(best_residual_bits != 0); |
| 2257 | break; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2258 | } |
| 2259 | sum += 1u << partition_order; |
| 2260 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 2261 | best_residual_bits = residual_bits; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2262 | best_parameters_index = !best_parameters_index; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2263 | best_partitioned_rice->order = partition_order; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2264 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 2265 | } |
| 2266 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2267 | else { |
| 2268 | unsigned partition_order; |
| 2269 | for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) { |
| 2270 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2271 | if(! |
| 2272 | set_partitioned_rice_( |
| 2273 | abs_residual, |
| 2274 | residual, |
| 2275 | residual_samples, |
| 2276 | predictor_order, |
| 2277 | rice_parameter, |
| 2278 | rice_parameter_search_dist, |
| 2279 | partition_order, |
| 2280 | &private_->partitioned_rice_contents_extra[!best_parameters_index], |
| 2281 | &residual_bits |
| 2282 | ) |
| 2283 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2284 | #else |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2285 | if(! |
| 2286 | set_partitioned_rice_( |
| 2287 | abs_residual, |
| 2288 | residual_samples, |
| 2289 | predictor_order, |
| 2290 | rice_parameter, |
| 2291 | rice_parameter_search_dist, |
| 2292 | partition_order, |
| 2293 | &private_->partitioned_rice_contents_extra[!best_parameters_index], |
| 2294 | &residual_bits |
| 2295 | ) |
| 2296 | ) |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 2297 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2298 | { |
| 2299 | FLAC__ASSERT(best_residual_bits != 0); |
| 2300 | break; |
| 2301 | } |
| 2302 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 2303 | best_residual_bits = residual_bits; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2304 | best_parameters_index = !best_parameters_index; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2305 | best_partitioned_rice->order = partition_order; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2310 | /* |
| 2311 | * We are allowed to de-const the pointer based on our special knowledhe; |
| 2312 | * it is const to the outside world. |
| 2313 | */ |
| 2314 | { |
| 2315 | FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents; |
| 2316 | FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order)); |
| 2317 | memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order))); |
| 2318 | memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order))); |
| 2319 | } |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2320 | |
| 2321 | return best_residual_bits; |
| 2322 | } |
| 2323 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2324 | void precompute_partition_info_sums_( |
| 2325 | const FLAC__uint32 abs_residual[], |
| 2326 | FLAC__uint64 abs_residual_partition_sums[], |
| 2327 | unsigned residual_samples, |
| 2328 | unsigned predictor_order, |
| 2329 | unsigned min_partition_order, |
| 2330 | unsigned max_partition_order |
| 2331 | ) |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2332 | { |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2333 | int partition_order; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 2334 | unsigned from_partition, to_partition = 0; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2335 | const unsigned blocksize = residual_samples + predictor_order; |
| 2336 | |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 2337 | /* first do max_partition_order */ |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2338 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2339 | FLAC__uint64 abs_residual_partition_sum; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2340 | FLAC__uint32 abs_r; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 2341 | unsigned partition, partition_sample, partition_samples, residual_sample; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2342 | const unsigned partitions = 1u << partition_order; |
| 2343 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 2344 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2345 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 2346 | |
| 2347 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 2348 | partition_samples = default_partition_samples; |
| 2349 | if(partition == 0) |
| 2350 | partition_samples -= predictor_order; |
| 2351 | abs_residual_partition_sum = 0; |
| 2352 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 2353 | abs_r = abs_residual[residual_sample]; |
| 2354 | abs_residual_partition_sum += abs_r; |
| 2355 | residual_sample++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2356 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2357 | abs_residual_partition_sums[partition] = abs_residual_partition_sum; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2358 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2359 | to_partition = partitions; |
| 2360 | break; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2361 | } |
Josh Coalson | f76a361 | 2001-04-18 02:28:11 +0000 | [diff] [blame] | 2362 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2363 | /* now merge partitions for lower orders */ |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame] | 2364 | for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2365 | FLAC__uint64 s; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 2366 | unsigned i; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2367 | const unsigned partitions = 1u << partition_order; |
| 2368 | for(i = 0; i < partitions; i++) { |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 2369 | s = abs_residual_partition_sums[from_partition]; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 2370 | from_partition++; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 2371 | abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition]; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 2372 | from_partition++; |
| 2373 | to_partition++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2374 | } |
| 2375 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2376 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2377 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2378 | void precompute_partition_info_escapes_( |
| 2379 | const FLAC__int32 residual[], |
| 2380 | unsigned raw_bits_per_partition[], |
| 2381 | unsigned residual_samples, |
| 2382 | unsigned predictor_order, |
| 2383 | unsigned min_partition_order, |
| 2384 | unsigned max_partition_order |
| 2385 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2386 | { |
| 2387 | int partition_order; |
| 2388 | unsigned from_partition, to_partition = 0; |
| 2389 | const unsigned blocksize = residual_samples + predictor_order; |
| 2390 | |
| 2391 | /* first do max_partition_order */ |
| 2392 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
| 2393 | FLAC__int32 r, residual_partition_min, residual_partition_max; |
| 2394 | unsigned silog2_min, silog2_max; |
| 2395 | unsigned partition, partition_sample, partition_samples, residual_sample; |
| 2396 | const unsigned partitions = 1u << partition_order; |
| 2397 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 2398 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2399 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 2400 | |
| 2401 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 2402 | partition_samples = default_partition_samples; |
| 2403 | if(partition == 0) |
| 2404 | partition_samples -= predictor_order; |
| 2405 | residual_partition_min = residual_partition_max = 0; |
| 2406 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 2407 | r = residual[residual_sample]; |
| 2408 | if(r < residual_partition_min) |
| 2409 | residual_partition_min = r; |
| 2410 | else if(r > residual_partition_max) |
| 2411 | residual_partition_max = r; |
| 2412 | residual_sample++; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2413 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2414 | silog2_min = FLAC__bitmath_silog2(residual_partition_min); |
| 2415 | silog2_max = FLAC__bitmath_silog2(residual_partition_max); |
| 2416 | raw_bits_per_partition[partition] = max(silog2_min, silog2_max); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2417 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2418 | to_partition = partitions; |
| 2419 | break; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | /* now merge partitions for lower orders */ |
| 2423 | for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
| 2424 | unsigned m; |
| 2425 | unsigned i; |
| 2426 | const unsigned partitions = 1u << partition_order; |
| 2427 | for(i = 0; i < partitions; i++) { |
| 2428 | m = raw_bits_per_partition[from_partition]; |
| 2429 | from_partition++; |
| 2430 | raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]); |
| 2431 | from_partition++; |
| 2432 | to_partition++; |
| 2433 | } |
| 2434 | } |
| 2435 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2436 | |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2437 | #ifdef VARIABLE_RICE_BITS |
| 2438 | #undef VARIABLE_RICE_BITS |
| 2439 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2440 | #ifndef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2441 | #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter)) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2442 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2443 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2444 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2445 | FLAC__bool set_partitioned_rice_( |
| 2446 | const FLAC__uint32 abs_residual[], |
| 2447 | const FLAC__int32 residual[], |
| 2448 | const unsigned residual_samples, |
| 2449 | const unsigned predictor_order, |
| 2450 | const unsigned suggested_rice_parameter, |
| 2451 | const unsigned rice_parameter_search_dist, |
| 2452 | const unsigned partition_order, |
| 2453 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 2454 | unsigned *bits |
| 2455 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2456 | #else |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2457 | FLAC__bool set_partitioned_rice_( |
| 2458 | const FLAC__uint32 abs_residual[], |
| 2459 | const unsigned residual_samples, |
| 2460 | const unsigned predictor_order, |
| 2461 | const unsigned suggested_rice_parameter, |
| 2462 | const unsigned rice_parameter_search_dist, |
| 2463 | const unsigned partition_order, |
| 2464 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 2465 | unsigned *bits |
| 2466 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2467 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2468 | { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2469 | unsigned rice_parameter, partition_bits; |
| 2470 | #ifndef NO_RICE_SEARCH |
| 2471 | unsigned best_partition_bits; |
| 2472 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 2473 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2474 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2475 | unsigned *parameters; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2476 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 2477 | FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER); |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2478 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2479 | FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order)); |
| 2480 | parameters = partitioned_rice_contents->parameters; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2481 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2482 | if(partition_order == 0) { |
| 2483 | unsigned i; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2484 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2485 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2486 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2487 | if(suggested_rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2488 | min_rice_parameter = 0; |
| 2489 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2490 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 2491 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2492 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2493 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2494 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2495 | #endif |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2496 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2497 | } |
| 2498 | } |
| 2499 | else |
| 2500 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
| 2501 | |
| 2502 | best_partition_bits = 0xffffffff; |
| 2503 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 2504 | #endif |
| 2505 | #ifdef VARIABLE_RICE_BITS |
| 2506 | #ifdef FLAC__SYMMETRIC_RICE |
| 2507 | partition_bits = (2+rice_parameter) * residual_samples; |
| 2508 | #else |
| 2509 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 2510 | partition_bits = (1+rice_parameter) * residual_samples; |
| 2511 | #endif |
| 2512 | #else |
| 2513 | partition_bits = 0; |
| 2514 | #endif |
| 2515 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 2516 | for(i = 0; i < residual_samples; i++) { |
| 2517 | #ifdef VARIABLE_RICE_BITS |
| 2518 | #ifdef FLAC__SYMMETRIC_RICE |
| 2519 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter); |
| 2520 | #else |
| 2521 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
| 2522 | #endif |
| 2523 | #else |
| 2524 | partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */ |
| 2525 | #endif |
| 2526 | } |
| 2527 | #ifndef NO_RICE_SEARCH |
| 2528 | if(partition_bits < best_partition_bits) { |
| 2529 | best_rice_parameter = rice_parameter; |
| 2530 | best_partition_bits = partition_bits; |
| 2531 | } |
| 2532 | } |
| 2533 | #endif |
| 2534 | parameters[0] = best_rice_parameter; |
| 2535 | bits_ += best_partition_bits; |
| 2536 | } |
| 2537 | else { |
| 2538 | unsigned partition, residual_sample, save_residual_sample, partition_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2539 | unsigned partition_samples; |
| 2540 | FLAC__uint64 mean, k; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2541 | const unsigned partitions = 1u << partition_order; |
| 2542 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 2543 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
| 2544 | if(partition == 0) { |
| 2545 | if(partition_samples <= predictor_order) |
| 2546 | return false; |
| 2547 | else |
| 2548 | partition_samples -= predictor_order; |
| 2549 | } |
| 2550 | mean = 0; |
| 2551 | save_residual_sample = residual_sample; |
| 2552 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2553 | mean += abs_residual[residual_sample]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2554 | residual_sample = save_residual_sample; |
| 2555 | #ifdef FLAC__SYMMETRIC_RICE |
| 2556 | mean += partition_samples >> 1; /* for rounding effect */ |
| 2557 | mean /= partition_samples; |
| 2558 | |
| 2559 | /* calc rice_parameter = floor(log2(mean)) */ |
| 2560 | rice_parameter = 0; |
| 2561 | mean>>=1; |
| 2562 | while(mean) { |
| 2563 | rice_parameter++; |
| 2564 | mean >>= 1; |
| 2565 | } |
| 2566 | #else |
| 2567 | /* calc rice_parameter ala LOCO-I */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2568 | for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2569 | ; |
| 2570 | #endif |
| 2571 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2572 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2573 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2574 | #endif |
| 2575 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 2576 | } |
| 2577 | |
| 2578 | #ifndef NO_RICE_SEARCH |
| 2579 | if(rice_parameter_search_dist) { |
| 2580 | if(rice_parameter < rice_parameter_search_dist) |
| 2581 | min_rice_parameter = 0; |
| 2582 | else |
| 2583 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 2584 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
| 2585 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2586 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2587 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2588 | #endif |
| 2589 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 2590 | } |
| 2591 | } |
| 2592 | else |
| 2593 | min_rice_parameter = max_rice_parameter = rice_parameter; |
| 2594 | |
| 2595 | best_partition_bits = 0xffffffff; |
| 2596 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 2597 | #endif |
| 2598 | #ifdef VARIABLE_RICE_BITS |
| 2599 | #ifdef FLAC__SYMMETRIC_RICE |
| 2600 | partition_bits = (2+rice_parameter) * partition_samples; |
| 2601 | #else |
| 2602 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 2603 | partition_bits = (1+rice_parameter) * partition_samples; |
| 2604 | #endif |
| 2605 | #else |
| 2606 | partition_bits = 0; |
| 2607 | #endif |
| 2608 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 2609 | save_residual_sample = residual_sample; |
| 2610 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) { |
| 2611 | #ifdef VARIABLE_RICE_BITS |
| 2612 | #ifdef FLAC__SYMMETRIC_RICE |
| 2613 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter); |
| 2614 | #else |
| 2615 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate); |
| 2616 | #endif |
| 2617 | #else |
| 2618 | partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */ |
| 2619 | #endif |
| 2620 | } |
| 2621 | #ifndef NO_RICE_SEARCH |
| 2622 | if(rice_parameter != max_rice_parameter) |
| 2623 | residual_sample = save_residual_sample; |
| 2624 | if(partition_bits < best_partition_bits) { |
| 2625 | best_rice_parameter = rice_parameter; |
| 2626 | best_partition_bits = partition_bits; |
| 2627 | } |
| 2628 | } |
| 2629 | #endif |
| 2630 | parameters[partition] = best_rice_parameter; |
| 2631 | bits_ += best_partition_bits; |
| 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | *bits = bits_; |
| 2636 | return true; |
| 2637 | } |
| 2638 | |
| 2639 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2640 | FLAC__bool set_partitioned_rice_with_precompute_( |
| 2641 | const FLAC__int32 residual[], |
| 2642 | const FLAC__uint64 abs_residual_partition_sums[], |
| 2643 | const unsigned raw_bits_per_partition[], |
| 2644 | const unsigned residual_samples, |
| 2645 | const unsigned predictor_order, |
| 2646 | const unsigned suggested_rice_parameter, |
| 2647 | const unsigned rice_parameter_search_dist, |
| 2648 | const unsigned partition_order, |
| 2649 | const FLAC__bool search_for_escapes, |
| 2650 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 2651 | unsigned *bits |
| 2652 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2653 | #else |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame^] | 2654 | FLAC__bool set_partitioned_rice_with_precompute_( |
| 2655 | const FLAC__uint32 abs_residual[], |
| 2656 | const FLAC__uint64 abs_residual_partition_sums[], |
| 2657 | const unsigned raw_bits_per_partition[], |
| 2658 | const unsigned residual_samples, |
| 2659 | const unsigned predictor_order, |
| 2660 | const unsigned suggested_rice_parameter, |
| 2661 | const unsigned rice_parameter_search_dist, |
| 2662 | const unsigned partition_order, |
| 2663 | const FLAC__bool search_for_escapes, |
| 2664 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 2665 | unsigned *bits |
| 2666 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2667 | #endif |
| 2668 | { |
| 2669 | unsigned rice_parameter, partition_bits; |
| 2670 | #ifndef NO_RICE_SEARCH |
| 2671 | unsigned best_partition_bits; |
| 2672 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 2673 | #endif |
| 2674 | unsigned flat_bits; |
| 2675 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2676 | unsigned *parameters, *raw_bits; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2677 | |
| 2678 | FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER); |
| 2679 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2680 | FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order)); |
| 2681 | parameters = partitioned_rice_contents->parameters; |
| 2682 | raw_bits = partitioned_rice_contents->raw_bits; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2683 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2684 | if(partition_order == 0) { |
| 2685 | unsigned i; |
| 2686 | |
| 2687 | #ifndef NO_RICE_SEARCH |
| 2688 | if(rice_parameter_search_dist) { |
| 2689 | if(suggested_rice_parameter < rice_parameter_search_dist) |
| 2690 | min_rice_parameter = 0; |
| 2691 | else |
| 2692 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 2693 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
| 2694 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2695 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2696 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2697 | #endif |
| 2698 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 2699 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2700 | } |
| 2701 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2702 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2703 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2704 | best_partition_bits = 0xffffffff; |
| 2705 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 2706 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2707 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2708 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2709 | partition_bits = (2+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2710 | #else |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2711 | const unsigned rice_parameter_estimate = rice_parameter-1; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2712 | partition_bits = (1+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2713 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2714 | #else |
| 2715 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2716 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2717 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2718 | for(i = 0; i < residual_samples; i++) { |
| 2719 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2720 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2721 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2722 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2723 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2724 | #endif |
| 2725 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2726 | 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] | 2727 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2728 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2729 | #ifndef NO_RICE_SEARCH |
| 2730 | if(partition_bits < best_partition_bits) { |
| 2731 | best_rice_parameter = rice_parameter; |
| 2732 | best_partition_bits = partition_bits; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2733 | } |
| 2734 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2735 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2736 | if(search_for_escapes) { |
| 2737 | flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples; |
| 2738 | if(flat_bits <= best_partition_bits) { |
| 2739 | raw_bits[0] = raw_bits_per_partition[0]; |
| 2740 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 2741 | best_partition_bits = flat_bits; |
| 2742 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2743 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2744 | parameters[0] = best_rice_parameter; |
| 2745 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2746 | } |
| 2747 | else { |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2748 | unsigned partition, residual_sample, save_residual_sample, partition_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2749 | unsigned partition_samples; |
| 2750 | FLAC__uint64 mean, k; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2751 | const unsigned partitions = 1u << partition_order; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2752 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2753 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2754 | if(partition == 0) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2755 | if(partition_samples <= predictor_order) |
| 2756 | return false; |
| 2757 | else |
| 2758 | partition_samples -= predictor_order; |
| 2759 | } |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 2760 | mean = abs_residual_partition_sums[partition]; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2761 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 2762 | mean += partition_samples >> 1; /* for rounding effect */ |
| 2763 | mean /= partition_samples; |
| 2764 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2765 | /* calc rice_parameter = floor(log2(mean)) */ |
| 2766 | rice_parameter = 0; |
| 2767 | mean>>=1; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2768 | while(mean) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2769 | rice_parameter++; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2770 | mean >>= 1; |
| 2771 | } |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2772 | #else |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 2773 | /* calc rice_parameter ala LOCO-I */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2774 | for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1) |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 2775 | ; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2776 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2777 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2778 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2779 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2780 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2781 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2782 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2783 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2784 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2785 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2786 | if(rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2787 | min_rice_parameter = 0; |
| 2788 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2789 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 2790 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2791 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2792 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2793 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2794 | #endif |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2795 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2796 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2797 | } |
| 2798 | else |
| 2799 | min_rice_parameter = max_rice_parameter = rice_parameter; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2800 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2801 | best_partition_bits = 0xffffffff; |
| 2802 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 2803 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2804 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2805 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2806 | partition_bits = (2+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2807 | #else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2808 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 2809 | partition_bits = (1+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2810 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2811 | #else |
| 2812 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2813 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2814 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2815 | save_residual_sample = residual_sample; |
| 2816 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) { |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2817 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2818 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2819 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2820 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2821 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2822 | #endif |
| 2823 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2824 | partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], 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] | 2825 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2826 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2827 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2828 | if(rice_parameter != max_rice_parameter) |
| 2829 | residual_sample = save_residual_sample; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2830 | if(partition_bits < best_partition_bits) { |
| 2831 | best_rice_parameter = rice_parameter; |
| 2832 | best_partition_bits = partition_bits; |
| 2833 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2834 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2835 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2836 | if(search_for_escapes) { |
| 2837 | flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples; |
| 2838 | if(flat_bits <= best_partition_bits) { |
| 2839 | raw_bits[partition] = raw_bits_per_partition[partition]; |
| 2840 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 2841 | best_partition_bits = flat_bits; |
| 2842 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2843 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2844 | parameters[partition] = best_rice_parameter; |
| 2845 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | *bits = bits_; |
| 2850 | return true; |
| 2851 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2852 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2853 | unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples) |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2854 | { |
| 2855 | unsigned i, shift; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2856 | FLAC__int32 x = 0; |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2857 | |
| 2858 | for(i = 0; i < samples && !(x&1); i++) |
| 2859 | x |= signal[i]; |
| 2860 | |
| 2861 | if(x == 0) { |
| 2862 | shift = 0; |
| 2863 | } |
| 2864 | else { |
| 2865 | for(shift = 0; !(x&1); shift++) |
| 2866 | x >>= 1; |
| 2867 | } |
| 2868 | |
| 2869 | if(shift > 0) { |
| 2870 | for(i = 0; i < samples; i++) |
| 2871 | signal[i] >>= shift; |
| 2872 | } |
| 2873 | |
| 2874 | return shift; |
| 2875 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2876 | |
| 2877 | void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples) |
| 2878 | { |
| 2879 | unsigned channel; |
| 2880 | |
| 2881 | for(channel = 0; channel < channels; channel++) |
| 2882 | memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples); |
| 2883 | |
| 2884 | fifo->tail += wide_samples; |
| 2885 | |
| 2886 | FLAC__ASSERT(fifo->tail <= fifo->size); |
| 2887 | } |
| 2888 | |
| 2889 | void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples) |
| 2890 | { |
| 2891 | unsigned channel; |
| 2892 | unsigned sample, wide_sample; |
| 2893 | unsigned tail = fifo->tail; |
| 2894 | |
| 2895 | sample = input_offset * channels; |
| 2896 | for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) { |
| 2897 | for(channel = 0; channel < channels; channel++) |
| 2898 | fifo->data[channel][tail] = input[sample++]; |
| 2899 | tail++; |
| 2900 | } |
| 2901 | fifo->tail = tail; |
| 2902 | |
| 2903 | FLAC__ASSERT(fifo->tail <= fifo->size); |
| 2904 | } |
| 2905 | |
| 2906 | FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) |
| 2907 | { |
| 2908 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 2909 | const unsigned encoded_bytes = encoder->private_->verify.output.bytes; |
| 2910 | (void)decoder; |
| 2911 | |
| 2912 | if(encoder->private_->verify.needs_magic_hack) { |
| 2913 | FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH); |
| 2914 | *bytes = FLAC__STREAM_SYNC_LENGTH; |
| 2915 | memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes); |
| 2916 | encoder->private_->verify.needs_magic_hack = false; |
| 2917 | } |
| 2918 | else { |
| 2919 | if(encoded_bytes == 0) { |
Josh Coalson | fc2b737 | 2002-08-16 05:39:34 +0000 | [diff] [blame] | 2920 | /* |
| 2921 | * If we get here, a FIFO underflow has occurred, |
| 2922 | * which means there is a bug somewhere. |
| 2923 | */ |
| 2924 | FLAC__ASSERT(0); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2925 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 2926 | } |
| 2927 | else if(encoded_bytes < *bytes) |
| 2928 | *bytes = encoded_bytes; |
| 2929 | memcpy(buffer, encoder->private_->verify.output.data, *bytes); |
| 2930 | encoder->private_->verify.output.data += *bytes; |
| 2931 | encoder->private_->verify.output.bytes -= *bytes; |
| 2932 | } |
| 2933 | |
| 2934 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 2935 | } |
| 2936 | |
| 2937 | FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 2938 | { |
| 2939 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data; |
| 2940 | unsigned channel; |
| 2941 | const unsigned channels = FLAC__stream_decoder_get_channels(decoder); |
| 2942 | const unsigned blocksize = frame->header.blocksize; |
| 2943 | const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize; |
| 2944 | |
| 2945 | for(channel = 0; channel < channels; channel++) { |
| 2946 | if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) { |
| 2947 | unsigned i, sample = 0; |
| 2948 | FLAC__int32 expect = 0, got = 0; |
| 2949 | |
| 2950 | for(i = 0; i < blocksize; i++) { |
| 2951 | if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) { |
| 2952 | sample = i; |
| 2953 | expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i]; |
| 2954 | got = (FLAC__int32)buffer[channel][i]; |
| 2955 | break; |
| 2956 | } |
| 2957 | } |
| 2958 | FLAC__ASSERT(i < blocksize); |
| 2959 | FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 2960 | encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample; |
| 2961 | encoder->private_->verify.error_stats.frame_number = frame->header.number.sample_number / blocksize; |
| 2962 | encoder->private_->verify.error_stats.channel = channel; |
| 2963 | encoder->private_->verify.error_stats.sample = sample; |
| 2964 | encoder->private_->verify.error_stats.expected = expect; |
| 2965 | encoder->private_->verify.error_stats.got = got; |
| 2966 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA; |
| 2967 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 2968 | } |
| 2969 | } |
| 2970 | /* dequeue the frame from the fifo */ |
| 2971 | for(channel = 0; channel < channels; channel++) { |
| 2972 | memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize); |
| 2973 | } |
| 2974 | encoder->private_->verify.input_fifo.tail -= blocksize; |
| 2975 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 2976 | } |
| 2977 | |
| 2978 | void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 2979 | { |
| 2980 | (void)decoder, (void)metadata, (void)client_data; |
| 2981 | } |
| 2982 | |
| 2983 | void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 2984 | { |
| 2985 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 2986 | (void)decoder, (void)status; |
| 2987 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 2988 | } |