Josh Coalson | 26560dd | 2001-02-08 00:38:41 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Josh Coalson | 0395dac | 2006-04-25 06:59:33 +0000 | [diff] [blame] | 2 | * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 Josh Coalson |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 7 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 10 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 32 | /*@@@@@@*/ |
| 33 | #define WINDOW_DEBUG_OUTPUT |
| 34 | |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 35 | #include <limits.h> |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> /* for malloc() */ |
| 38 | #include <string.h> /* for memcpy() */ |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 39 | #include "FLAC/assert.h" |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 40 | #include "FLAC/stream_decoder.h" |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 41 | #include "protected/stream_encoder.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 42 | #include "private/bitbuffer.h" |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 43 | #include "private/bitmath.h" |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 44 | #include "private/crc.h" |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 45 | #include "private/cpu.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 46 | #include "private/fixed.h" |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 47 | #include "private/format.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 48 | #include "private/lpc.h" |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 49 | #include "private/md5.h" |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 50 | #include "private/memory.h" |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 51 | #include "private/stream_encoder_framing.h" |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 52 | #include "private/window.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 53 | |
Josh Coalson | 5e31be1 | 2002-12-04 07:07:35 +0000 | [diff] [blame] | 54 | #ifdef HAVE_CONFIG_H |
| 55 | #include <config.h> |
| 56 | #endif |
| 57 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 58 | #ifdef min |
| 59 | #undef min |
| 60 | #endif |
| 61 | #define min(x,y) ((x)<(y)?(x):(y)) |
| 62 | |
| 63 | #ifdef max |
| 64 | #undef max |
| 65 | #endif |
| 66 | #define max(x,y) ((x)>(y)?(x):(y)) |
| 67 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 68 | typedef struct { |
| 69 | FLAC__int32 *data[FLAC__MAX_CHANNELS]; |
| 70 | unsigned size; /* of each data[] in samples */ |
| 71 | unsigned tail; |
| 72 | } verify_input_fifo; |
| 73 | |
| 74 | typedef struct { |
| 75 | const FLAC__byte *data; |
| 76 | unsigned capacity; |
| 77 | unsigned bytes; |
| 78 | } verify_output; |
| 79 | |
| 80 | typedef enum { |
| 81 | ENCODER_IN_MAGIC = 0, |
| 82 | ENCODER_IN_METADATA = 1, |
| 83 | ENCODER_IN_AUDIO = 2 |
| 84 | } EncoderStateHint; |
| 85 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 86 | /*********************************************************************** |
| 87 | * |
| 88 | * Private class method prototypes |
| 89 | * |
| 90 | ***********************************************************************/ |
| 91 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 92 | static void set_defaults_(FLAC__StreamEncoder *encoder); |
| 93 | static void free_(FLAC__StreamEncoder *encoder); |
| 94 | static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 95 | static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples); |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 96 | static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame); |
| 97 | 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] | 98 | |
| 99 | static FLAC__bool process_subframe_( |
| 100 | FLAC__StreamEncoder *encoder, |
| 101 | unsigned min_partition_order, |
| 102 | unsigned max_partition_order, |
| 103 | FLAC__bool precompute_partition_sums, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 104 | const FLAC__FrameHeader *frame_header, |
| 105 | unsigned subframe_bps, |
| 106 | const FLAC__int32 integer_signal[], |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 107 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 108 | const FLAC__real real_signal[], |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 109 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 110 | FLAC__Subframe *subframe[2], |
| 111 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2], |
| 112 | FLAC__int32 *residual[2], |
| 113 | unsigned *best_subframe, |
| 114 | unsigned *best_bits |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 115 | #ifdef WINDOW_DEBUG_OUTPUT |
| 116 | ,unsigned subframe_number |
| 117 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 118 | ); |
| 119 | |
| 120 | static FLAC__bool add_subframe_( |
| 121 | FLAC__StreamEncoder *encoder, |
| 122 | const FLAC__FrameHeader *frame_header, |
| 123 | unsigned subframe_bps, |
| 124 | const FLAC__Subframe *subframe, |
| 125 | FLAC__BitBuffer *frame |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 126 | #ifdef WINDOW_DEBUG_OUTPUT |
| 127 | ,unsigned subframe_bits |
| 128 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 129 | ); |
| 130 | |
| 131 | static unsigned evaluate_constant_subframe_( |
| 132 | const FLAC__int32 signal, |
| 133 | unsigned subframe_bps, |
| 134 | FLAC__Subframe *subframe |
| 135 | ); |
| 136 | |
| 137 | static unsigned evaluate_fixed_subframe_( |
| 138 | FLAC__StreamEncoder *encoder, |
| 139 | const FLAC__int32 signal[], |
| 140 | FLAC__int32 residual[], |
| 141 | FLAC__uint32 abs_residual[], |
| 142 | FLAC__uint64 abs_residual_partition_sums[], |
| 143 | unsigned raw_bits_per_partition[], |
| 144 | unsigned blocksize, |
| 145 | unsigned subframe_bps, |
| 146 | unsigned order, |
| 147 | unsigned rice_parameter, |
| 148 | unsigned min_partition_order, |
| 149 | unsigned max_partition_order, |
| 150 | FLAC__bool precompute_partition_sums, |
| 151 | FLAC__bool do_escape_coding, |
| 152 | unsigned rice_parameter_search_dist, |
| 153 | FLAC__Subframe *subframe, |
| 154 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 155 | ); |
| 156 | |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 157 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 158 | static unsigned evaluate_lpc_subframe_( |
| 159 | FLAC__StreamEncoder *encoder, |
| 160 | const FLAC__int32 signal[], |
| 161 | FLAC__int32 residual[], |
| 162 | FLAC__uint32 abs_residual[], |
| 163 | FLAC__uint64 abs_residual_partition_sums[], |
| 164 | unsigned raw_bits_per_partition[], |
| 165 | const FLAC__real lp_coeff[], |
| 166 | unsigned blocksize, |
| 167 | unsigned subframe_bps, |
| 168 | unsigned order, |
| 169 | unsigned qlp_coeff_precision, |
| 170 | unsigned rice_parameter, |
| 171 | unsigned min_partition_order, |
| 172 | unsigned max_partition_order, |
| 173 | FLAC__bool precompute_partition_sums, |
| 174 | FLAC__bool do_escape_coding, |
| 175 | unsigned rice_parameter_search_dist, |
| 176 | FLAC__Subframe *subframe, |
| 177 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 178 | #ifdef WINDOW_DEBUG_OUTPUT |
| 179 | ,unsigned frame_number |
| 180 | ,unsigned subframe_number |
| 181 | ,FLAC__ApodizationSpecification aspec |
| 182 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 183 | ); |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 184 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 185 | |
| 186 | static unsigned evaluate_verbatim_subframe_( |
| 187 | const FLAC__int32 signal[], |
| 188 | unsigned blocksize, |
| 189 | unsigned subframe_bps, |
| 190 | FLAC__Subframe *subframe |
| 191 | ); |
| 192 | |
| 193 | static unsigned find_best_partition_order_( |
| 194 | struct FLAC__StreamEncoderPrivate *private_, |
| 195 | const FLAC__int32 residual[], |
| 196 | FLAC__uint32 abs_residual[], |
| 197 | FLAC__uint64 abs_residual_partition_sums[], |
| 198 | unsigned raw_bits_per_partition[], |
| 199 | unsigned residual_samples, |
| 200 | unsigned predictor_order, |
| 201 | unsigned rice_parameter, |
| 202 | unsigned min_partition_order, |
| 203 | unsigned max_partition_order, |
| 204 | FLAC__bool precompute_partition_sums, |
| 205 | FLAC__bool do_escape_coding, |
| 206 | unsigned rice_parameter_search_dist, |
| 207 | FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice |
| 208 | ); |
| 209 | |
| 210 | static void precompute_partition_info_sums_( |
| 211 | const FLAC__uint32 abs_residual[], |
| 212 | FLAC__uint64 abs_residual_partition_sums[], |
| 213 | unsigned residual_samples, |
| 214 | unsigned predictor_order, |
| 215 | unsigned min_partition_order, |
| 216 | unsigned max_partition_order |
| 217 | ); |
| 218 | |
| 219 | static void precompute_partition_info_escapes_( |
| 220 | const FLAC__int32 residual[], |
| 221 | unsigned raw_bits_per_partition[], |
| 222 | unsigned residual_samples, |
| 223 | unsigned predictor_order, |
| 224 | unsigned min_partition_order, |
| 225 | unsigned max_partition_order |
| 226 | ); |
| 227 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 228 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 229 | static FLAC__bool set_partitioned_rice_( |
| 230 | const FLAC__uint32 abs_residual[], |
| 231 | const FLAC__int32 residual[], |
| 232 | const unsigned residual_samples, |
| 233 | const unsigned predictor_order, |
| 234 | const unsigned suggested_rice_parameter, |
| 235 | const unsigned rice_parameter_search_dist, |
| 236 | const unsigned partition_order, |
| 237 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 238 | unsigned *bits |
| 239 | ); |
| 240 | |
| 241 | static FLAC__bool set_partitioned_rice_with_precompute_( |
| 242 | const FLAC__int32 residual[], |
| 243 | const FLAC__uint64 abs_residual_partition_sums[], |
| 244 | const unsigned raw_bits_per_partition[], |
| 245 | const unsigned residual_samples, |
| 246 | const unsigned predictor_order, |
| 247 | const unsigned suggested_rice_parameter, |
| 248 | const unsigned rice_parameter_search_dist, |
| 249 | const unsigned partition_order, |
| 250 | const FLAC__bool search_for_escapes, |
| 251 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 252 | unsigned *bits |
| 253 | ); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 254 | #else |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 255 | static FLAC__bool set_partitioned_rice_( |
| 256 | const FLAC__uint32 abs_residual[], |
| 257 | const unsigned residual_samples, |
| 258 | const unsigned predictor_order, |
| 259 | const unsigned suggested_rice_parameter, |
| 260 | const unsigned rice_parameter_search_dist, |
| 261 | const unsigned partition_order, |
| 262 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 263 | unsigned *bits |
| 264 | ); |
| 265 | |
| 266 | static FLAC__bool set_partitioned_rice_with_precompute_( |
| 267 | const FLAC__uint32 abs_residual[], |
| 268 | const FLAC__uint64 abs_residual_partition_sums[], |
| 269 | const unsigned raw_bits_per_partition[], |
| 270 | const unsigned residual_samples, |
| 271 | const unsigned predictor_order, |
| 272 | const unsigned suggested_rice_parameter, |
| 273 | const unsigned rice_parameter_search_dist, |
| 274 | const unsigned partition_order, |
| 275 | const FLAC__bool search_for_escapes, |
| 276 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 277 | unsigned *bits |
| 278 | ); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 279 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 280 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 281 | static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples); |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 282 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 283 | /* verify-related routines: */ |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 284 | static void append_to_verify_fifo_( |
| 285 | verify_input_fifo *fifo, |
| 286 | const FLAC__int32 * const input[], |
| 287 | unsigned input_offset, |
| 288 | unsigned channels, |
| 289 | unsigned wide_samples |
| 290 | ); |
| 291 | |
| 292 | static void append_to_verify_fifo_interleaved_( |
| 293 | verify_input_fifo *fifo, |
| 294 | const FLAC__int32 input[], |
| 295 | unsigned input_offset, |
| 296 | unsigned channels, |
| 297 | unsigned wide_samples |
| 298 | ); |
| 299 | |
| 300 | static FLAC__StreamDecoderReadStatus verify_read_callback_( |
| 301 | const FLAC__StreamDecoder *decoder, |
| 302 | FLAC__byte buffer[], |
| 303 | unsigned *bytes, |
| 304 | void *client_data |
| 305 | ); |
| 306 | |
| 307 | static FLAC__StreamDecoderWriteStatus verify_write_callback_( |
| 308 | const FLAC__StreamDecoder *decoder, |
| 309 | const FLAC__Frame *frame, |
| 310 | const FLAC__int32 * const buffer[], |
| 311 | void *client_data |
| 312 | ); |
| 313 | |
| 314 | static void verify_metadata_callback_( |
| 315 | const FLAC__StreamDecoder *decoder, |
| 316 | const FLAC__StreamMetadata *metadata, |
| 317 | void *client_data |
| 318 | ); |
| 319 | |
| 320 | static void verify_error_callback_( |
| 321 | const FLAC__StreamDecoder *decoder, |
| 322 | FLAC__StreamDecoderErrorStatus status, |
| 323 | void *client_data |
| 324 | ); |
| 325 | |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 326 | #ifdef WINDOW_DEBUG_OUTPUT |
| 327 | static const char * const winstr[] = { |
| 328 | "bartlett", |
| 329 | "bartlett_hann", |
| 330 | "blackman", |
| 331 | "blackman_harris_4term_92db_sidelobe", |
| 332 | "connes", |
| 333 | "flattop", |
| 334 | "gauss", |
| 335 | "hamming", |
| 336 | "hann", |
| 337 | "kaiser_bessel", |
| 338 | "nuttall", |
| 339 | "rectangular", |
| 340 | "triangle", |
| 341 | "tukey", |
| 342 | "welch" |
| 343 | }; |
| 344 | #endif |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 345 | |
| 346 | /*********************************************************************** |
| 347 | * |
| 348 | * Private class data |
| 349 | * |
| 350 | ***********************************************************************/ |
| 351 | |
| 352 | typedef struct FLAC__StreamEncoderPrivate { |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 353 | 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] | 354 | FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */ |
| 355 | FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 356 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 357 | FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */ |
| 358 | FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */ |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 359 | FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */ |
| 360 | FLAC__real *windowed_signal; /* the real_signal[] * current window[] */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 361 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 362 | unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */ |
| 363 | 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] | 364 | FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */ |
| 365 | FLAC__int32 *residual_workspace_mid_side[2][2]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 366 | FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2]; |
| 367 | FLAC__Subframe subframe_workspace_mid_side[2][2]; |
| 368 | FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 369 | FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2]; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 370 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2]; |
| 371 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2]; |
| 372 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 373 | 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] | 374 | unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 375 | unsigned best_subframe_mid_side[2]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 376 | 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] | 377 | unsigned best_subframe_bits_mid_side[2]; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 378 | FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 379 | 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] | 380 | 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] | 381 | FLAC__BitBuffer *frame; /* the current frame being worked on */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 382 | unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 383 | 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] | 384 | FLAC__ChannelAssignment last_channel_assignment; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 385 | FLAC__StreamMetadata metadata; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 386 | unsigned current_sample_number; |
| 387 | unsigned current_frame_number; |
Josh Coalson | 3e7a96e | 2004-07-23 05:18:22 +0000 | [diff] [blame] | 388 | struct FLAC__MD5Context md5context; |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 389 | FLAC__CPUInfo cpuinfo; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 390 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 0975843 | 2004-10-20 00:21:50 +0000 | [diff] [blame] | 391 | unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 392 | #else |
| 393 | unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 394 | #endif |
| 395 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 396 | void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
Josh Coalson | 7446e18 | 2005-01-26 04:04:38 +0000 | [diff] [blame] | 397 | 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[]); |
| 398 | void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 399 | 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 | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 400 | #endif |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 401 | FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */ |
| 402 | FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */ |
| 403 | FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */ |
| 404 | FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */ |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 405 | FLAC__bool disable_constant_subframes; |
| 406 | FLAC__bool disable_fixed_subframes; |
| 407 | FLAC__bool disable_verbatim_subframes; |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 408 | FLAC__StreamEncoderWriteCallback write_callback; |
| 409 | FLAC__StreamEncoderMetadataCallback metadata_callback; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 410 | void *client_data; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 411 | /* unaligned (original) pointers to allocated data */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 412 | FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 413 | FLAC__int32 *integer_signal_mid_side_unaligned[2]; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 414 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 415 | FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 416 | FLAC__real *real_signal_mid_side_unaligned[2]; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 417 | FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS]; |
| 418 | FLAC__real *windowed_signal_unaligned; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 419 | #endif |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 420 | FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2]; |
| 421 | FLAC__int32 *residual_workspace_mid_side_unaligned[2][2]; |
| 422 | FLAC__uint32 *abs_residual_unaligned; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 423 | FLAC__uint64 *abs_residual_partition_sums_unaligned; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 424 | unsigned *raw_bits_per_partition_unaligned; |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 425 | /* |
| 426 | * These fields have been moved here from private function local |
| 427 | * declarations merely to save stack space during encoding. |
| 428 | */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 429 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 430 | FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 431 | #endif |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 432 | 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] | 433 | /* |
| 434 | * The data for the verify section |
| 435 | */ |
| 436 | struct { |
| 437 | FLAC__StreamDecoder *decoder; |
| 438 | EncoderStateHint state_hint; |
| 439 | FLAC__bool needs_magic_hack; |
| 440 | verify_input_fifo input_fifo; |
| 441 | verify_output output; |
| 442 | struct { |
| 443 | FLAC__uint64 absolute_sample; |
| 444 | unsigned frame_number; |
| 445 | unsigned channel; |
| 446 | unsigned sample; |
| 447 | FLAC__int32 expected; |
| 448 | FLAC__int32 got; |
| 449 | } error_stats; |
| 450 | } verify; |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 451 | 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] | 452 | } FLAC__StreamEncoderPrivate; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 453 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 454 | /*********************************************************************** |
| 455 | * |
| 456 | * Public static class data |
| 457 | * |
| 458 | ***********************************************************************/ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 459 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 460 | FLAC_API const char * const FLAC__StreamEncoderStateString[] = { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 461 | "FLAC__STREAM_ENCODER_OK", |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 462 | "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR", |
| 463 | "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA", |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 464 | "FLAC__STREAM_ENCODER_INVALID_CALLBACK", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 465 | "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS", |
| 466 | "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE", |
| 467 | "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE", |
| 468 | "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE", |
Josh Coalson | 20ac2c1 | 2002-08-30 05:47:14 +0000 | [diff] [blame] | 469 | "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 470 | "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION", |
| 471 | "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH", |
| 472 | "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH", |
| 473 | "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE", |
| 474 | "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER", |
| 475 | "FLAC__STREAM_ENCODER_NOT_STREAMABLE", |
| 476 | "FLAC__STREAM_ENCODER_FRAMING_ERROR", |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 477 | "FLAC__STREAM_ENCODER_INVALID_METADATA", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 478 | "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING", |
| 479 | "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING", |
| 480 | "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR", |
| 481 | "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED", |
| 482 | "FLAC__STREAM_ENCODER_UNINITIALIZED" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 483 | }; |
| 484 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 485 | FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = { |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 486 | "FLAC__STREAM_ENCODER_WRITE_STATUS_OK", |
| 487 | "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 488 | }; |
| 489 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 490 | /*********************************************************************** |
| 491 | * |
| 492 | * Class constructor/destructor |
| 493 | * |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 494 | */ |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 495 | FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new() |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 496 | { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 497 | FLAC__StreamEncoder *encoder; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 498 | unsigned i; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 499 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 500 | 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] | 501 | |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 502 | encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder)); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 503 | if(encoder == 0) { |
| 504 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 505 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 506 | |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 507 | encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected)); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 508 | if(encoder->protected_ == 0) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 509 | free(encoder); |
| 510 | return 0; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 511 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 512 | |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 513 | encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate)); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 514 | if(encoder->private_ == 0) { |
| 515 | free(encoder->protected_); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 516 | free(encoder); |
| 517 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 518 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 519 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 520 | encoder->private_->frame = FLAC__bitbuffer_new(); |
| 521 | if(encoder->private_->frame == 0) { |
| 522 | free(encoder->private_); |
| 523 | free(encoder->protected_); |
| 524 | free(encoder); |
| 525 | return 0; |
| 526 | } |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 527 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 528 | set_defaults_(encoder); |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 529 | |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 530 | encoder->private_->is_being_deleted = false; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 531 | |
| 532 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 533 | encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0]; |
| 534 | encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1]; |
| 535 | } |
| 536 | for(i = 0; i < 2; i++) { |
| 537 | encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0]; |
| 538 | encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1]; |
| 539 | } |
| 540 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 541 | encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0]; |
| 542 | 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] | 543 | } |
| 544 | for(i = 0; i < 2; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 545 | encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]; |
| 546 | 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] | 547 | } |
| 548 | |
| 549 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 550 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]); |
| 551 | 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] | 552 | } |
| 553 | for(i = 0; i < 2; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 554 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]); |
| 555 | 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] | 556 | } |
| 557 | for(i = 0; i < 2; i++) |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 558 | 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] | 559 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 560 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 561 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 562 | return encoder; |
| 563 | } |
| 564 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 565 | FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 566 | { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 567 | unsigned i; |
| 568 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 569 | FLAC__ASSERT(0 != encoder); |
| 570 | FLAC__ASSERT(0 != encoder->protected_); |
| 571 | FLAC__ASSERT(0 != encoder->private_); |
| 572 | FLAC__ASSERT(0 != encoder->private_->frame); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 573 | |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 574 | encoder->private_->is_being_deleted = true; |
| 575 | |
| 576 | FLAC__stream_encoder_finish(encoder); |
| 577 | |
Josh Coalson | 4fa9059 | 2002-12-04 07:01:37 +0000 | [diff] [blame] | 578 | if(0 != encoder->private_->verify.decoder) |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 579 | FLAC__stream_decoder_delete(encoder->private_->verify.decoder); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 580 | |
| 581 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 582 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]); |
| 583 | 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] | 584 | } |
| 585 | for(i = 0; i < 2; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 586 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]); |
| 587 | 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] | 588 | } |
| 589 | for(i = 0; i < 2; i++) |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 590 | 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] | 591 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 592 | FLAC__bitbuffer_delete(encoder->private_->frame); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 593 | free(encoder->private_); |
| 594 | free(encoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 595 | free(encoder); |
| 596 | } |
| 597 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 598 | /*********************************************************************** |
| 599 | * |
| 600 | * Public class methods |
| 601 | * |
| 602 | ***********************************************************************/ |
| 603 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 604 | FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 605 | { |
| 606 | unsigned i; |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 607 | FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 608 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 609 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 610 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 611 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 612 | return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 613 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 614 | encoder->protected_->state = FLAC__STREAM_ENCODER_OK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 615 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 616 | if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback) |
| 617 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 618 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 619 | if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS) |
| 620 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS; |
Josh Coalson | 69f1ee0 | 2001-01-24 00:54:43 +0000 | [diff] [blame] | 621 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 622 | if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2) |
| 623 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH; |
Josh Coalson | d37d135 | 2001-05-30 23:09:31 +0000 | [diff] [blame] | 624 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 625 | if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo) |
| 626 | return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 627 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 628 | if(encoder->protected_->bits_per_sample >= 32) |
| 629 | 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] | 630 | |
Josh Coalson | 76c68bc | 2002-05-17 06:22:02 +0000 | [diff] [blame] | 631 | 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] | 632 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 633 | |
Josh Coalson | 0833f34 | 2002-07-15 05:31:55 +0000 | [diff] [blame] | 634 | if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 635 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 636 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 637 | if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE) |
| 638 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 639 | |
Josh Coalson | 20ac2c1 | 2002-08-30 05:47:14 +0000 | [diff] [blame] | 640 | if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER) |
| 641 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER; |
| 642 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 643 | if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order) |
| 644 | 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] | 645 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 646 | if(encoder->protected_->qlp_coeff_precision == 0) { |
| 647 | if(encoder->protected_->bits_per_sample < 16) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 648 | /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */ |
| 649 | /* @@@ until then we'll make a guess */ |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 650 | encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 651 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 652 | else if(encoder->protected_->bits_per_sample == 16) { |
| 653 | if(encoder->protected_->blocksize <= 192) |
| 654 | encoder->protected_->qlp_coeff_precision = 7; |
| 655 | else if(encoder->protected_->blocksize <= 384) |
| 656 | encoder->protected_->qlp_coeff_precision = 8; |
| 657 | else if(encoder->protected_->blocksize <= 576) |
| 658 | encoder->protected_->qlp_coeff_precision = 9; |
| 659 | else if(encoder->protected_->blocksize <= 1152) |
| 660 | encoder->protected_->qlp_coeff_precision = 10; |
| 661 | else if(encoder->protected_->blocksize <= 2304) |
| 662 | encoder->protected_->qlp_coeff_precision = 11; |
| 663 | else if(encoder->protected_->blocksize <= 4608) |
| 664 | encoder->protected_->qlp_coeff_precision = 12; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 665 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 666 | encoder->protected_->qlp_coeff_precision = 13; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 667 | } |
| 668 | else { |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 669 | if(encoder->protected_->blocksize <= 384) |
| 670 | encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2; |
| 671 | else if(encoder->protected_->blocksize <= 1152) |
| 672 | encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1; |
| 673 | else |
| 674 | encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 675 | } |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 676 | FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 677 | } |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 678 | else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 679 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 680 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 681 | if(encoder->protected_->streamable_subset) { |
Josh Coalson | 20ac2c1 | 2002-08-30 05:47:14 +0000 | [diff] [blame] | 682 | if( |
| 683 | encoder->protected_->blocksize != 192 && |
| 684 | encoder->protected_->blocksize != 576 && |
| 685 | encoder->protected_->blocksize != 1152 && |
| 686 | encoder->protected_->blocksize != 2304 && |
| 687 | encoder->protected_->blocksize != 4608 && |
| 688 | encoder->protected_->blocksize != 256 && |
| 689 | encoder->protected_->blocksize != 512 && |
| 690 | encoder->protected_->blocksize != 1024 && |
| 691 | encoder->protected_->blocksize != 2048 && |
| 692 | encoder->protected_->blocksize != 4096 && |
| 693 | encoder->protected_->blocksize != 8192 && |
| 694 | encoder->protected_->blocksize != 16384 |
| 695 | ) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 696 | return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
Josh Coalson | 20ac2c1 | 2002-08-30 05:47:14 +0000 | [diff] [blame] | 697 | if( |
| 698 | encoder->protected_->sample_rate != 8000 && |
| 699 | encoder->protected_->sample_rate != 16000 && |
| 700 | encoder->protected_->sample_rate != 22050 && |
| 701 | encoder->protected_->sample_rate != 24000 && |
| 702 | encoder->protected_->sample_rate != 32000 && |
| 703 | encoder->protected_->sample_rate != 44100 && |
| 704 | encoder->protected_->sample_rate != 48000 && |
| 705 | encoder->protected_->sample_rate != 96000 |
| 706 | ) |
| 707 | return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
| 708 | if( |
| 709 | encoder->protected_->bits_per_sample != 8 && |
| 710 | encoder->protected_->bits_per_sample != 12 && |
| 711 | encoder->protected_->bits_per_sample != 16 && |
| 712 | encoder->protected_->bits_per_sample != 20 && |
| 713 | encoder->protected_->bits_per_sample != 24 |
| 714 | ) |
| 715 | return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
Josh Coalson | c1c8d49 | 2002-09-26 04:42:10 +0000 | [diff] [blame] | 716 | if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 717 | return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 720 | if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 721 | encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1; |
| 722 | if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order) |
| 723 | encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 724 | |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 725 | /* validate metadata */ |
| 726 | if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) |
| 727 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 728 | metadata_has_seektable = false; |
| 729 | metadata_has_vorbis_comment = false; |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 730 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 731 | if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO) |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 732 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 733 | else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) { |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 734 | if(metadata_has_seektable) /* only one is allowed */ |
| 735 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 736 | metadata_has_seektable = true; |
Josh Coalson | 0833f34 | 2002-07-15 05:31:55 +0000 | [diff] [blame] | 737 | 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] | 738 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 739 | } |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 740 | else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) { |
| 741 | if(metadata_has_vorbis_comment) /* only one is allowed */ |
| 742 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 743 | metadata_has_vorbis_comment = true; |
| 744 | } |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 745 | else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) { |
Josh Coalson | 8f0c71b | 2002-12-05 06:37:46 +0000 | [diff] [blame] | 746 | if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0)) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 747 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 748 | } |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 751 | encoder->private_->input_capacity = 0; |
| 752 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 753 | encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 754 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 755 | encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 756 | #endif |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 757 | } |
| 758 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 759 | encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 760 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 761 | encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 762 | #endif |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 763 | } |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 764 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 765 | for(i = 0; i < encoder->protected_->num_apodizations; i++) |
| 766 | encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0; |
| 767 | encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0; |
| 768 | #endif |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 769 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 770 | encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0; |
| 771 | encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0; |
| 772 | encoder->private_->best_subframe[i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 773 | } |
| 774 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 775 | encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0; |
| 776 | encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0; |
| 777 | encoder->private_->best_subframe_mid_side[i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 778 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 779 | encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0; |
| 780 | encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0; |
| 781 | encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 782 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 783 | encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5); |
| 784 | #else |
| 785 | /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */ |
| 786 | /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply÷ by hand */ |
| 787 | FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350); |
| 788 | FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535); |
| 789 | FLAC__ASSERT(encoder->protected_->sample_rate <= 655350); |
| 790 | FLAC__ASSERT(encoder->protected_->blocksize <= 65535); |
| 791 | encoder->private_->loose_mid_side_stereo_frames = (unsigned)FLAC__fixedpoint_trunc((((FLAC__uint64)(encoder->protected_->sample_rate) * (FLAC__uint64)(26214)) << 16) / (encoder->protected_->blocksize<<16) + FLAC__FP_ONE_HALF); |
| 792 | #endif |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 793 | if(encoder->private_->loose_mid_side_stereo_frames == 0) |
| 794 | encoder->private_->loose_mid_side_stereo_frames = 1; |
| 795 | encoder->private_->loose_mid_side_stereo_frame_count = 0; |
| 796 | encoder->private_->current_sample_number = 0; |
| 797 | encoder->private_->current_frame_number = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 798 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 799 | encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30); |
| 800 | 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? */ |
| 801 | encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 802 | |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 803 | /* |
| 804 | * get the CPU info and set the function pointers |
| 805 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 806 | FLAC__cpu_info(&encoder->private_->cpuinfo); |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 807 | /* first default to the non-asm routines */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 808 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 809 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 810 | #endif |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 811 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 812 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 813 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients; |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 814 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 815 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 816 | #endif |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 817 | /* now override with asm where appropriate */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 818 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 819 | # ifndef FLAC__NO_ASM |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 820 | if(encoder->private_->cpuinfo.use_asm) { |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 821 | # ifdef FLAC__CPU_IA32 |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 822 | FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 823 | # ifdef FLAC__HAS_NASM |
| 824 | # ifdef FLAC__SSE_OS |
Josh Coalson | 48cbe66 | 2002-12-30 23:38:14 +0000 | [diff] [blame] | 825 | if(encoder->private_->cpuinfo.data.ia32.sse) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 826 | if(encoder->protected_->max_lpc_order < 4) |
| 827 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4; |
| 828 | else if(encoder->protected_->max_lpc_order < 8) |
| 829 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8; |
| 830 | else if(encoder->protected_->max_lpc_order < 12) |
| 831 | 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] | 832 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 833 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 834 | } |
Josh Coalson | 48cbe66 | 2002-12-30 23:38:14 +0000 | [diff] [blame] | 835 | else |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 836 | # endif /* FLAC__SSE_OS */ |
Josh Coalson | 48cbe66 | 2002-12-30 23:38:14 +0000 | [diff] [blame] | 837 | if(encoder->private_->cpuinfo.data.ia32._3dnow) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 838 | 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] | 839 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 840 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 841 | if(encoder->private_->cpuinfo.data.ia32.mmx) { |
| 842 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 843 | 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] | 844 | } |
| 845 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 846 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 847 | 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] | 848 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 849 | if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov) |
| 850 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov; |
| 851 | # endif /* FLAC__HAS_NASM */ |
| 852 | # endif /* FLAC__CPU_IA32 */ |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 853 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 854 | # endif /* !FLAC__NO_ASM */ |
| 855 | #endif /* !FLAC__INTEGER_ONLY_LIBRARY */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 856 | /* finally override based on wide-ness if necessary */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 857 | if(encoder->private_->use_wide_by_block) { |
| 858 | 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] | 859 | } |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 860 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 861 | /* 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] | 862 | 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] | 863 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 864 | if(!resize_buffers_(encoder, encoder->protected_->blocksize)) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 865 | /* 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] | 866 | return encoder->protected_->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 867 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 868 | |
| 869 | if(!FLAC__bitbuffer_init(encoder->private_->frame)) |
| 870 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 871 | |
| 872 | /* |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 873 | * Set up the verify stuff if necessary |
| 874 | */ |
| 875 | if(encoder->protected_->verify) { |
| 876 | /* |
| 877 | * First, set up the fifo which will hold the |
| 878 | * original signal to compare against |
| 879 | */ |
| 880 | encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize; |
| 881 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 882 | if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) |
| 883 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 884 | } |
| 885 | encoder->private_->verify.input_fifo.tail = 0; |
| 886 | |
| 887 | /* |
| 888 | * Now set up a stream decoder for verification |
| 889 | */ |
| 890 | encoder->private_->verify.decoder = FLAC__stream_decoder_new(); |
| 891 | if(0 == encoder->private_->verify.decoder) |
| 892 | return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 893 | |
| 894 | FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_); |
| 895 | FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_); |
| 896 | FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_); |
| 897 | FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_); |
| 898 | FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder); |
| 899 | if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA) |
| 900 | return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 901 | } |
Josh Coalson | 589f8c7 | 2002-08-07 23:54:55 +0000 | [diff] [blame] | 902 | encoder->private_->verify.error_stats.absolute_sample = 0; |
| 903 | encoder->private_->verify.error_stats.frame_number = 0; |
| 904 | encoder->private_->verify.error_stats.channel = 0; |
| 905 | encoder->private_->verify.error_stats.sample = 0; |
| 906 | encoder->private_->verify.error_stats.expected = 0; |
| 907 | encoder->private_->verify.error_stats.got = 0; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 908 | |
| 909 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 910 | * write the stream header |
| 911 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 912 | if(encoder->protected_->verify) |
| 913 | encoder->private_->verify.state_hint = ENCODER_IN_MAGIC; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 914 | 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] | 915 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 916 | if(!write_bitbuffer_(encoder, 0)) { |
| 917 | /* the above function sets the state for us in case of an error */ |
| 918 | return encoder->protected_->state; |
| 919 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 920 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 921 | /* |
| 922 | * write the STREAMINFO metadata block |
| 923 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 924 | if(encoder->protected_->verify) |
| 925 | encoder->private_->verify.state_hint = ENCODER_IN_METADATA; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 926 | encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO; |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 927 | encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 928 | encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 929 | encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */ |
| 930 | encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize; |
| 931 | encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 932 | encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 933 | encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate; |
| 934 | encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels; |
| 935 | encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample; |
| 936 | encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */ |
| 937 | memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */ |
Josh Coalson | 3e7a96e | 2004-07-23 05:18:22 +0000 | [diff] [blame] | 938 | FLAC__MD5Init(&encoder->private_->md5context); |
Josh Coalson | 7424d2f | 2002-11-06 07:10:38 +0000 | [diff] [blame] | 939 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) |
| 940 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 941 | if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 942 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 943 | if(!write_bitbuffer_(encoder, 0)) { |
| 944 | /* the above function sets the state for us in case of an error */ |
| 945 | return encoder->protected_->state; |
| 946 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 947 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 948 | /* |
| 949 | * Now that the STREAMINFO block is written, we can init this to an |
| 950 | * absurdly-high value... |
| 951 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 952 | 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] | 953 | /* ... and clear this to 0 */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 954 | encoder->private_->metadata.data.stream_info.total_samples = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 955 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 956 | /* |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 957 | * Check to see if the supplied metadata contains a VORBIS_COMMENT; |
| 958 | * if not, we will write an empty one (FLAC__add_metadata_block() |
| 959 | * automatically supplies the vendor string). |
Josh Coalson | 69cfda7 | 2004-09-10 00:38:21 +0000 | [diff] [blame] | 960 | * |
| 961 | * WATCHOUT: libOggFLAC depends on us to write this block after the |
| 962 | * STREAMINFO since that's what the mapping requires. (In the case |
Josh Coalson | 8ddf7fb | 2004-12-30 00:58:50 +0000 | [diff] [blame] | 963 | * that metadata_has_vorbis_comment is true it will have already |
Josh Coalson | 69cfda7 | 2004-09-10 00:38:21 +0000 | [diff] [blame] | 964 | * insured that the metadata list is properly ordered.) |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 965 | */ |
| 966 | if(!metadata_has_vorbis_comment) { |
| 967 | FLAC__StreamMetadata vorbis_comment; |
| 968 | vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT; |
| 969 | vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0); |
| 970 | vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */ |
| 971 | vorbis_comment.data.vorbis_comment.vendor_string.length = 0; |
| 972 | vorbis_comment.data.vorbis_comment.vendor_string.entry = 0; |
| 973 | vorbis_comment.data.vorbis_comment.num_comments = 0; |
| 974 | vorbis_comment.data.vorbis_comment.comments = 0; |
Josh Coalson | 7424d2f | 2002-11-06 07:10:38 +0000 | [diff] [blame] | 975 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) |
| 976 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 977 | if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) |
| 978 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 979 | if(!write_bitbuffer_(encoder, 0)) { |
| 980 | /* the above function sets the state for us in case of an error */ |
| 981 | return encoder->protected_->state; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | /* |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 986 | * write the user's metadata blocks |
| 987 | */ |
| 988 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 989 | encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1); |
Josh Coalson | 7424d2f | 2002-11-06 07:10:38 +0000 | [diff] [blame] | 990 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) |
| 991 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 992 | if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) |
| 993 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 994 | if(!write_bitbuffer_(encoder, 0)) { |
| 995 | /* the above function sets the state for us in case of an error */ |
| 996 | return encoder->protected_->state; |
| 997 | } |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1000 | if(encoder->protected_->verify) |
| 1001 | encoder->private_->verify.state_hint = ENCODER_IN_AUDIO; |
| 1002 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1003 | return encoder->protected_->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1006 | FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1007 | { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1008 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 1009 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1010 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1011 | return; |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 1012 | |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 1013 | 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] | 1014 | if(encoder->private_->current_sample_number != 0) { |
| 1015 | encoder->protected_->blocksize = encoder->private_->current_sample_number; |
| 1016 | process_frame_(encoder, true); /* true => is last frame */ |
| 1017 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1018 | } |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 1019 | |
Josh Coalson | 3e7a96e | 2004-07-23 05:18:22 +0000 | [diff] [blame] | 1020 | FLAC__MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context); |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 1021 | |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 1022 | 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] | 1023 | encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data); |
| 1024 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1025 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1026 | if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder) |
| 1027 | FLAC__stream_decoder_finish(encoder->private_->verify.decoder); |
| 1028 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1029 | free_(encoder); |
| 1030 | set_defaults_(encoder); |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1031 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1032 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1035 | FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1036 | { |
| 1037 | FLAC__ASSERT(0 != encoder); |
| 1038 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1039 | return false; |
Josh Coalson | 47c7b14 | 2005-01-29 06:08:58 +0000 | [diff] [blame] | 1040 | #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1041 | encoder->protected_->verify = value; |
Josh Coalson | 47c7b14 | 2005-01-29 06:08:58 +0000 | [diff] [blame] | 1042 | #endif |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1043 | return true; |
| 1044 | } |
| 1045 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1046 | FLAC_API 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] | 1047 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1048 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1049 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1050 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1051 | encoder->protected_->streamable_subset = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1052 | return true; |
| 1053 | } |
| 1054 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1055 | FLAC_API 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] | 1056 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1057 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1058 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1059 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1060 | encoder->protected_->do_mid_side_stereo = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1061 | return true; |
| 1062 | } |
| 1063 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1064 | FLAC_API 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] | 1065 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1066 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1067 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1068 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1069 | encoder->protected_->loose_mid_side_stereo = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1070 | return true; |
| 1071 | } |
| 1072 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1073 | FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1074 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1075 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1076 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1077 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1078 | encoder->protected_->channels = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1079 | return true; |
| 1080 | } |
| 1081 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1082 | FLAC_API 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] | 1083 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1084 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1085 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1086 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1087 | encoder->protected_->bits_per_sample = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1088 | return true; |
| 1089 | } |
| 1090 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1091 | FLAC_API 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] | 1092 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1093 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1094 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1095 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1096 | encoder->protected_->sample_rate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1097 | return true; |
| 1098 | } |
| 1099 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1100 | FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1101 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1102 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1103 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1104 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1105 | encoder->protected_->blocksize = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1106 | return true; |
| 1107 | } |
| 1108 | |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 1109 | FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification) |
| 1110 | { |
| 1111 | FLAC__ASSERT(0 != encoder); |
| 1112 | FLAC__ASSERT(0 != specification); |
| 1113 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1114 | return false; |
| 1115 | #ifdef FLAC__INTEGER_ONLY_LIBRARY |
| 1116 | (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */ |
| 1117 | #else |
| 1118 | encoder->protected_->num_apodizations = 0; |
| 1119 | while(1) { |
| 1120 | const char *s = strchr(specification, ';'); |
| 1121 | const size_t n = s? (size_t)(s - specification) : strlen(specification); |
| 1122 | if (n==8 && 0 == strncmp("bartlett" , specification, n)) |
| 1123 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT; |
| 1124 | else if(n==13 && 0 == strncmp("bartlett_hann", specification, n)) |
| 1125 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN; |
| 1126 | else if(n==8 && 0 == strncmp("blackman" , specification, n)) |
| 1127 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN; |
| 1128 | else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n)) |
| 1129 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE; |
| 1130 | else if(n==6 && 0 == strncmp("connes" , specification, n)) |
| 1131 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES; |
| 1132 | else if(n==7 && 0 == strncmp("flattop" , specification, n)) |
| 1133 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP; |
| 1134 | else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) { |
| 1135 | FLAC__real stddev = (FLAC__real)strtod(specification+6, 0); |
| 1136 | if (stddev > 0.0 && stddev <= 0.5) { |
| 1137 | encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev; |
| 1138 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS; |
| 1139 | } |
| 1140 | } |
| 1141 | else if(n==7 && 0 == strncmp("hamming" , specification, n)) |
| 1142 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING; |
| 1143 | else if(n==4 && 0 == strncmp("hann" , specification, n)) |
| 1144 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN; |
| 1145 | else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n)) |
| 1146 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL; |
| 1147 | else if(n==7 && 0 == strncmp("nuttall" , specification, n)) |
| 1148 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL; |
| 1149 | else if(n==9 && 0 == strncmp("rectangle" , specification, n)) |
| 1150 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE; |
| 1151 | else if(n==8 && 0 == strncmp("triangle" , specification, n)) |
| 1152 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE; |
| 1153 | else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) { |
| 1154 | FLAC__real p = (FLAC__real)strtod(specification+6, 0); |
| 1155 | if (p >= 0.0 && p <= 1.0) { |
| 1156 | encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p; |
| 1157 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY; |
| 1158 | } |
| 1159 | } |
| 1160 | else if(n==5 && 0 == strncmp("welch" , specification, n)) |
| 1161 | encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH; |
| 1162 | if (encoder->protected_->num_apodizations == 32) |
| 1163 | break; |
| 1164 | if (s) |
| 1165 | specification = s+1; |
| 1166 | else |
| 1167 | break; |
| 1168 | } |
| 1169 | if(encoder->protected_->num_apodizations == 0) { |
| 1170 | encoder->protected_->num_apodizations = 1; |
| 1171 | encoder->protected_->apodizations[0].type = FLAC__APODIZATION_HANN; |
| 1172 | } |
| 1173 | #ifdef WINDOW_DEBUG_OUTPUT |
| 1174 | {unsigned n;for(n=0;n<encoder->protected_->num_apodizations;n++)fprintf(stderr,"@@@@@@ parsed apodization[%zu]: %s\n",n,winstr[encoder->protected_->apodizations[n].type]);} |
| 1175 | #endif |
| 1176 | #endif |
| 1177 | return true; |
| 1178 | } |
| 1179 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1180 | FLAC_API 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] | 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 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1184 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1185 | encoder->protected_->max_lpc_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1186 | return true; |
| 1187 | } |
| 1188 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1189 | FLAC_API 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] | 1190 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1191 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1192 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1193 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1194 | encoder->protected_->qlp_coeff_precision = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1195 | return true; |
| 1196 | } |
| 1197 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1198 | FLAC_API 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] | 1199 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1200 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1201 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1202 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1203 | encoder->protected_->do_qlp_coeff_prec_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1204 | return true; |
| 1205 | } |
| 1206 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1207 | FLAC_API 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] | 1208 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1209 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1210 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1211 | return false; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 1212 | #if 0 |
| 1213 | /*@@@ deprecated: */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1214 | encoder->protected_->do_escape_coding = value; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 1215 | #else |
| 1216 | (void)value; |
| 1217 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1218 | return true; |
| 1219 | } |
| 1220 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1221 | FLAC_API 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] | 1222 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1223 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1224 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1225 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1226 | encoder->protected_->do_exhaustive_model_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1227 | return true; |
| 1228 | } |
| 1229 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1230 | FLAC_API 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] | 1231 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1232 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1233 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1234 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1235 | encoder->protected_->min_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1236 | return true; |
| 1237 | } |
| 1238 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1239 | FLAC_API 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] | 1240 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1241 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1242 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1243 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1244 | encoder->protected_->max_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1245 | return true; |
| 1246 | } |
| 1247 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1248 | FLAC_API 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] | 1249 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1250 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1251 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1252 | return false; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 1253 | #if 0 |
| 1254 | /*@@@ deprecated: */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1255 | encoder->protected_->rice_parameter_search_dist = value; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 1256 | #else |
| 1257 | (void)value; |
| 1258 | #endif |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1259 | return true; |
| 1260 | } |
| 1261 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1262 | FLAC_API 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] | 1263 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1264 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1265 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1266 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1267 | encoder->protected_->total_samples_estimate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1268 | return true; |
| 1269 | } |
| 1270 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1271 | FLAC_API 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] | 1272 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1273 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1274 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1275 | return false; |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 1276 | encoder->protected_->metadata = metadata; |
| 1277 | encoder->protected_->num_metadata_blocks = num_blocks; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1278 | return true; |
| 1279 | } |
| 1280 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1281 | FLAC_API 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] | 1282 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1283 | FLAC__ASSERT(0 != encoder); |
| 1284 | FLAC__ASSERT(0 != value); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1285 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1286 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1287 | encoder->private_->write_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1288 | return true; |
| 1289 | } |
| 1290 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1291 | FLAC_API 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] | 1292 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1293 | FLAC__ASSERT(0 != encoder); |
| 1294 | FLAC__ASSERT(0 != value); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1295 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1296 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1297 | encoder->private_->metadata_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1298 | return true; |
| 1299 | } |
| 1300 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1301 | FLAC_API 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] | 1302 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1303 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1304 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1305 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1306 | encoder->private_->client_data = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1307 | return true; |
| 1308 | } |
| 1309 | |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1310 | /* |
| 1311 | * These three functions are not static, but not publically exposed in |
| 1312 | * include/FLAC/ either. They are used by the test suite. |
| 1313 | */ |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1314 | FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1315 | { |
| 1316 | FLAC__ASSERT(0 != encoder); |
| 1317 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1318 | return false; |
| 1319 | encoder->private_->disable_constant_subframes = value; |
| 1320 | return true; |
| 1321 | } |
| 1322 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1323 | FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1324 | { |
| 1325 | FLAC__ASSERT(0 != encoder); |
| 1326 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1327 | return false; |
| 1328 | encoder->private_->disable_fixed_subframes = value; |
| 1329 | return true; |
| 1330 | } |
| 1331 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1332 | FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1333 | { |
| 1334 | FLAC__ASSERT(0 != encoder); |
| 1335 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1336 | return false; |
| 1337 | encoder->private_->disable_verbatim_subframes = value; |
| 1338 | return true; |
| 1339 | } |
| 1340 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1341 | FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1342 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1343 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1344 | return encoder->protected_->state; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1347 | FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder) |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1348 | { |
| 1349 | FLAC__ASSERT(0 != encoder); |
| 1350 | if(encoder->protected_->verify) |
| 1351 | return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder); |
| 1352 | else |
| 1353 | return FLAC__STREAM_DECODER_UNINITIALIZED; |
| 1354 | } |
| 1355 | |
Josh Coalson | 0295422 | 2002-11-08 06:16:31 +0000 | [diff] [blame] | 1356 | FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder) |
| 1357 | { |
| 1358 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) |
| 1359 | return FLAC__StreamEncoderStateString[encoder->protected_->state]; |
| 1360 | else |
Josh Coalson | 807140d | 2003-09-24 22:10:51 +0000 | [diff] [blame] | 1361 | return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder); |
Josh Coalson | 0295422 | 2002-11-08 06:16:31 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1364 | FLAC_API 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) |
Josh Coalson | 589f8c7 | 2002-08-07 23:54:55 +0000 | [diff] [blame] | 1365 | { |
| 1366 | FLAC__ASSERT(0 != encoder); |
| 1367 | if(0 != absolute_sample) |
| 1368 | *absolute_sample = encoder->private_->verify.error_stats.absolute_sample; |
| 1369 | if(0 != frame_number) |
| 1370 | *frame_number = encoder->private_->verify.error_stats.frame_number; |
| 1371 | if(0 != channel) |
| 1372 | *channel = encoder->private_->verify.error_stats.channel; |
| 1373 | if(0 != sample) |
| 1374 | *sample = encoder->private_->verify.error_stats.sample; |
| 1375 | if(0 != expected) |
| 1376 | *expected = encoder->private_->verify.error_stats.expected; |
| 1377 | if(0 != got) |
| 1378 | *got = encoder->private_->verify.error_stats.got; |
| 1379 | } |
| 1380 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1381 | FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder) |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1382 | { |
| 1383 | FLAC__ASSERT(0 != encoder); |
| 1384 | return encoder->protected_->verify; |
| 1385 | } |
| 1386 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1387 | FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1388 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1389 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1390 | return encoder->protected_->streamable_subset; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1393 | FLAC_API 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] | 1394 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1395 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1396 | return encoder->protected_->do_mid_side_stereo; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1399 | FLAC_API 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] | 1400 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1401 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1402 | return encoder->protected_->loose_mid_side_stereo; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1405 | FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1406 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1407 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1408 | return encoder->protected_->channels; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1411 | FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1412 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1413 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1414 | return encoder->protected_->bits_per_sample; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1417 | FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1418 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1419 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1420 | return encoder->protected_->sample_rate; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1423 | FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1424 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1425 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1426 | return encoder->protected_->blocksize; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1429 | FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1430 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1431 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1432 | return encoder->protected_->max_lpc_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1435 | FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1436 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1437 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1438 | return encoder->protected_->qlp_coeff_precision; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1441 | FLAC_API 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] | 1442 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1443 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1444 | return encoder->protected_->do_qlp_coeff_prec_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1447 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1448 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1449 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1450 | return encoder->protected_->do_escape_coding; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1453 | FLAC_API 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] | 1454 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1455 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1456 | return encoder->protected_->do_exhaustive_model_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1459 | FLAC_API 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] | 1460 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1461 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1462 | return encoder->protected_->min_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1465 | FLAC_API 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] | 1466 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1467 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1468 | return encoder->protected_->max_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1471 | FLAC_API 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] | 1472 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1473 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1474 | return encoder->protected_->rice_parameter_search_dist; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1477 | FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 3a7b2c9 | 2002-08-02 07:38:20 +0000 | [diff] [blame] | 1478 | { |
| 1479 | FLAC__ASSERT(0 != encoder); |
| 1480 | return encoder->protected_->total_samples_estimate; |
| 1481 | } |
| 1482 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1483 | FLAC_API 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] | 1484 | { |
| 1485 | unsigned i, j, channel; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1486 | FLAC__int32 x, mid, side; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1487 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1488 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1489 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1490 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1491 | |
| 1492 | j = 0; |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1493 | /* |
| 1494 | * we have several flavors of the same basic loop, optimized for |
| 1495 | * different conditions: |
| 1496 | */ |
| 1497 | if(encoder->protected_->max_lpc_order > 0) { |
| 1498 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
| 1499 | /* |
| 1500 | * stereo coding: unroll channel loop |
| 1501 | * with LPC: calculate floating point version of signal |
| 1502 | */ |
| 1503 | do { |
| 1504 | if(encoder->protected_->verify) |
| 1505 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1506 | |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1507 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
| 1508 | x = mid = side = buffer[0][j]; |
| 1509 | encoder->private_->integer_signal[0][i] = x; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1510 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1511 | encoder->private_->real_signal[0][i] = (FLAC__real)x; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1512 | #endif |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1513 | x = buffer[1][j]; |
| 1514 | encoder->private_->integer_signal[1][i] = x; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1515 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1516 | encoder->private_->real_signal[1][i] = (FLAC__real)x; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1517 | #endif |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1518 | mid += x; |
| 1519 | side -= x; |
| 1520 | mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */ |
| 1521 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 1522 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1523 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1524 | encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side; |
| 1525 | encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1526 | #endif |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1527 | encoder->private_->current_sample_number++; |
| 1528 | } |
| 1529 | if(i == blocksize) { |
| 1530 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
| 1531 | return false; |
| 1532 | } |
| 1533 | } while(j < samples); |
| 1534 | } |
| 1535 | else { |
| 1536 | /* |
| 1537 | * independent channel coding: buffer each channel in inner loop |
| 1538 | * with LPC: calculate floating point version of signal |
| 1539 | */ |
| 1540 | do { |
| 1541 | if(encoder->protected_->verify) |
| 1542 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1543 | |
| 1544 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
| 1545 | for(channel = 0; channel < channels; channel++) { |
| 1546 | x = buffer[channel][j]; |
| 1547 | encoder->private_->integer_signal[channel][i] = x; |
| 1548 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 1549 | encoder->private_->real_signal[channel][i] = (FLAC__real)x; |
| 1550 | #endif |
| 1551 | } |
| 1552 | encoder->private_->current_sample_number++; |
| 1553 | } |
| 1554 | if(i == blocksize) { |
| 1555 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
| 1556 | return false; |
| 1557 | } |
| 1558 | } while(j < samples); |
| 1559 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1560 | } |
| 1561 | else { |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1562 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
| 1563 | /* |
| 1564 | * stereo coding: unroll channel loop |
| 1565 | * without LPC: no need to calculate floating point version of signal |
| 1566 | */ |
| 1567 | do { |
| 1568 | if(encoder->protected_->verify) |
| 1569 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1570 | |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1571 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
| 1572 | encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j]; |
| 1573 | x = buffer[1][j]; |
| 1574 | encoder->private_->integer_signal[1][i] = x; |
| 1575 | mid += x; |
| 1576 | side -= x; |
| 1577 | mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */ |
| 1578 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 1579 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
| 1580 | encoder->private_->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1581 | } |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1582 | if(i == blocksize) { |
| 1583 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
| 1584 | return false; |
| 1585 | } |
| 1586 | } while(j < samples); |
| 1587 | } |
| 1588 | else { |
| 1589 | /* |
| 1590 | * independent channel coding: buffer each channel in inner loop |
| 1591 | * without LPC: no need to calculate floating point version of signal |
| 1592 | */ |
| 1593 | do { |
| 1594 | if(encoder->protected_->verify) |
| 1595 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1596 | |
| 1597 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
| 1598 | for(channel = 0; channel < channels; channel++) |
| 1599 | encoder->private_->integer_signal[channel][i] = buffer[channel][j]; |
| 1600 | encoder->private_->current_sample_number++; |
| 1601 | } |
| 1602 | if(i == blocksize) { |
| 1603 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
| 1604 | return false; |
| 1605 | } |
| 1606 | } while(j < samples); |
| 1607 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1608 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1609 | |
| 1610 | return true; |
| 1611 | } |
| 1612 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1613 | FLAC_API 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] | 1614 | { |
| 1615 | unsigned i, j, k, channel; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1616 | FLAC__int32 x, mid, side; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1617 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1618 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1619 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1620 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1621 | |
| 1622 | j = k = 0; |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1623 | /* |
| 1624 | * we have several flavors of the same basic loop, optimized for |
| 1625 | * different conditions: |
| 1626 | */ |
| 1627 | if(encoder->protected_->max_lpc_order > 0) { |
| 1628 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
| 1629 | /* |
| 1630 | * stereo coding: unroll channel loop |
| 1631 | * with LPC: calculate floating point version of signal |
| 1632 | */ |
| 1633 | do { |
| 1634 | if(encoder->protected_->verify) |
| 1635 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1636 | |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1637 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
| 1638 | x = mid = side = buffer[k++]; |
| 1639 | encoder->private_->integer_signal[0][i] = x; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1640 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1641 | encoder->private_->real_signal[0][i] = (FLAC__real)x; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1642 | #endif |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1643 | x = buffer[k++]; |
| 1644 | encoder->private_->integer_signal[1][i] = x; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1645 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1646 | encoder->private_->real_signal[1][i] = (FLAC__real)x; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1647 | #endif |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1648 | mid += x; |
| 1649 | side -= x; |
| 1650 | mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */ |
| 1651 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 1652 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1653 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1654 | encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side; |
| 1655 | encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1656 | #endif |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1657 | encoder->private_->current_sample_number++; |
| 1658 | } |
| 1659 | if(i == blocksize) { |
| 1660 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
| 1661 | return false; |
| 1662 | } |
| 1663 | } while(j < samples); |
| 1664 | } |
| 1665 | else { |
| 1666 | /* |
| 1667 | * independent channel coding: buffer each channel in inner loop |
| 1668 | * with LPC: calculate floating point version of signal |
| 1669 | */ |
| 1670 | do { |
| 1671 | if(encoder->protected_->verify) |
| 1672 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1673 | |
| 1674 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
| 1675 | for(channel = 0; channel < channels; channel++) { |
| 1676 | x = buffer[k++]; |
| 1677 | encoder->private_->integer_signal[channel][i] = x; |
| 1678 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 1679 | encoder->private_->real_signal[channel][i] = (FLAC__real)x; |
| 1680 | #endif |
| 1681 | } |
| 1682 | encoder->private_->current_sample_number++; |
| 1683 | } |
| 1684 | if(i == blocksize) { |
| 1685 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
| 1686 | return false; |
| 1687 | } |
| 1688 | } while(j < samples); |
| 1689 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1690 | } |
| 1691 | else { |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1692 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
| 1693 | /* |
| 1694 | * stereo coding: unroll channel loop |
| 1695 | * without LPC: no need to calculate floating point version of signal |
| 1696 | */ |
| 1697 | do { |
| 1698 | if(encoder->protected_->verify) |
| 1699 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1700 | |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1701 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
| 1702 | encoder->private_->integer_signal[0][i] = mid = side = buffer[k++]; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1703 | x = buffer[k++]; |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1704 | encoder->private_->integer_signal[1][i] = x; |
| 1705 | mid += x; |
| 1706 | side -= x; |
| 1707 | mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */ |
| 1708 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 1709 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
| 1710 | encoder->private_->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1711 | } |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1712 | if(i == blocksize) { |
| 1713 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
| 1714 | return false; |
| 1715 | } |
| 1716 | } while(j < samples); |
| 1717 | } |
| 1718 | else { |
| 1719 | /* |
| 1720 | * independent channel coding: buffer each channel in inner loop |
| 1721 | * without LPC: no need to calculate floating point version of signal |
| 1722 | */ |
| 1723 | do { |
| 1724 | if(encoder->protected_->verify) |
| 1725 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1726 | |
| 1727 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
| 1728 | for(channel = 0; channel < channels; channel++) |
| 1729 | encoder->private_->integer_signal[channel][i] = buffer[k++]; |
| 1730 | encoder->private_->current_sample_number++; |
| 1731 | } |
| 1732 | if(i == blocksize) { |
| 1733 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
| 1734 | return false; |
| 1735 | } |
| 1736 | } while(j < samples); |
| 1737 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1738 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1739 | |
| 1740 | return true; |
| 1741 | } |
| 1742 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1743 | /*********************************************************************** |
| 1744 | * |
| 1745 | * Private class methods |
| 1746 | * |
| 1747 | ***********************************************************************/ |
| 1748 | |
| 1749 | void set_defaults_(FLAC__StreamEncoder *encoder) |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1750 | { |
| 1751 | FLAC__ASSERT(0 != encoder); |
| 1752 | |
Josh Coalson | 47c7b14 | 2005-01-29 06:08:58 +0000 | [diff] [blame] | 1753 | #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING |
| 1754 | encoder->protected_->verify = true; |
| 1755 | #else |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1756 | encoder->protected_->verify = false; |
Josh Coalson | 47c7b14 | 2005-01-29 06:08:58 +0000 | [diff] [blame] | 1757 | #endif |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1758 | encoder->protected_->streamable_subset = true; |
| 1759 | encoder->protected_->do_mid_side_stereo = false; |
| 1760 | encoder->protected_->loose_mid_side_stereo = false; |
| 1761 | encoder->protected_->channels = 2; |
| 1762 | encoder->protected_->bits_per_sample = 16; |
| 1763 | encoder->protected_->sample_rate = 44100; |
| 1764 | encoder->protected_->blocksize = 1152; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 1765 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 1766 | encoder->protected_->num_apodizations = 1; |
| 1767 | encoder->protected_->apodizations[0].type = FLAC__APODIZATION_HANN; |
| 1768 | #endif |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1769 | encoder->protected_->max_lpc_order = 0; |
| 1770 | encoder->protected_->qlp_coeff_precision = 0; |
| 1771 | encoder->protected_->do_qlp_coeff_prec_search = false; |
| 1772 | encoder->protected_->do_exhaustive_model_search = false; |
| 1773 | encoder->protected_->do_escape_coding = false; |
| 1774 | encoder->protected_->min_residual_partition_order = 0; |
| 1775 | encoder->protected_->max_residual_partition_order = 0; |
| 1776 | encoder->protected_->rice_parameter_search_dist = 0; |
| 1777 | encoder->protected_->total_samples_estimate = 0; |
| 1778 | encoder->protected_->metadata = 0; |
| 1779 | encoder->protected_->num_metadata_blocks = 0; |
| 1780 | |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1781 | encoder->private_->disable_constant_subframes = false; |
| 1782 | encoder->private_->disable_fixed_subframes = false; |
| 1783 | encoder->private_->disable_verbatim_subframes = false; |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1784 | encoder->private_->write_callback = 0; |
| 1785 | encoder->private_->metadata_callback = 0; |
| 1786 | encoder->private_->client_data = 0; |
| 1787 | } |
| 1788 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1789 | void free_(FLAC__StreamEncoder *encoder) |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1790 | { |
| 1791 | unsigned i, channel; |
| 1792 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1793 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1794 | for(i = 0; i < encoder->protected_->channels; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1795 | if(0 != encoder->private_->integer_signal_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1796 | free(encoder->private_->integer_signal_unaligned[i]); |
| 1797 | encoder->private_->integer_signal_unaligned[i] = 0; |
| 1798 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1799 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1800 | if(0 != encoder->private_->real_signal_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1801 | free(encoder->private_->real_signal_unaligned[i]); |
| 1802 | encoder->private_->real_signal_unaligned[i] = 0; |
| 1803 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1804 | #endif |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1805 | } |
| 1806 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1807 | if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1808 | free(encoder->private_->integer_signal_mid_side_unaligned[i]); |
| 1809 | encoder->private_->integer_signal_mid_side_unaligned[i] = 0; |
| 1810 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1811 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1812 | if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1813 | free(encoder->private_->real_signal_mid_side_unaligned[i]); |
| 1814 | encoder->private_->real_signal_mid_side_unaligned[i] = 0; |
| 1815 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1816 | #endif |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1817 | } |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 1818 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 1819 | for(i = 0; i < encoder->protected_->num_apodizations; i++) { |
| 1820 | if(0 != encoder->private_->window_unaligned[i]) { |
| 1821 | free(encoder->private_->window_unaligned[i]); |
| 1822 | encoder->private_->window_unaligned[i] = 0; |
| 1823 | } |
| 1824 | } |
| 1825 | if(0 != encoder->private_->windowed_signal_unaligned) { |
| 1826 | free(encoder->private_->windowed_signal_unaligned); |
| 1827 | encoder->private_->windowed_signal_unaligned = 0; |
| 1828 | } |
| 1829 | #endif |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1830 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 1831 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1832 | if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1833 | free(encoder->private_->residual_workspace_unaligned[channel][i]); |
| 1834 | encoder->private_->residual_workspace_unaligned[channel][i] = 0; |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 | for(channel = 0; channel < 2; channel++) { |
| 1839 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1840 | if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1841 | free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]); |
| 1842 | encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0; |
| 1843 | } |
| 1844 | } |
| 1845 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1846 | if(0 != encoder->private_->abs_residual_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1847 | free(encoder->private_->abs_residual_unaligned); |
| 1848 | encoder->private_->abs_residual_unaligned = 0; |
| 1849 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1850 | if(0 != encoder->private_->abs_residual_partition_sums_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1851 | free(encoder->private_->abs_residual_partition_sums_unaligned); |
| 1852 | encoder->private_->abs_residual_partition_sums_unaligned = 0; |
| 1853 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1854 | if(0 != encoder->private_->raw_bits_per_partition_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1855 | free(encoder->private_->raw_bits_per_partition_unaligned); |
| 1856 | encoder->private_->raw_bits_per_partition_unaligned = 0; |
| 1857 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1858 | if(encoder->protected_->verify) { |
| 1859 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 1860 | if(0 != encoder->private_->verify.input_fifo.data[i]) { |
| 1861 | free(encoder->private_->verify.input_fifo.data[i]); |
| 1862 | encoder->private_->verify.input_fifo.data[i] = 0; |
| 1863 | } |
| 1864 | } |
| 1865 | } |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1866 | FLAC__bitbuffer_free(encoder->private_->frame); |
| 1867 | } |
| 1868 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1869 | FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1870 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1871 | FLAC__bool ok; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1872 | unsigned i, channel; |
| 1873 | |
| 1874 | FLAC__ASSERT(new_size > 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1875 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 1876 | FLAC__ASSERT(encoder->private_->current_sample_number == 0); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1877 | |
| 1878 | /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1879 | if(new_size <= encoder->private_->input_capacity) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1880 | return true; |
| 1881 | |
| 1882 | ok = true; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1883 | |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 1884 | /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx() |
| 1885 | * requires that the input arrays (in our case the integer signals) |
| 1886 | * have a buffer of up to 3 zeroes in front (at negative indices) for |
| 1887 | * alignment purposes; we use 4 to keep the data well-aligned. |
| 1888 | */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1889 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1890 | for(i = 0; ok && i < encoder->protected_->channels; i++) { |
| 1891 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]); |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1892 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1893 | if(encoder->protected_->max_lpc_order > 0) |
| 1894 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]); |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1895 | #endif |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1896 | memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4); |
| 1897 | encoder->private_->integer_signal[i] += 4; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1898 | } |
| 1899 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1900 | 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]); |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1901 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 1902 | if(encoder->protected_->max_lpc_order > 0) |
| 1903 | 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]); |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1904 | #endif |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1905 | memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4); |
| 1906 | encoder->private_->integer_signal_mid_side[i] += 4; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1907 | } |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 1908 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 1909 | if(ok && encoder->protected_->max_lpc_order > 0) { |
| 1910 | for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) |
| 1911 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]); |
| 1912 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal); |
| 1913 | } |
| 1914 | #endif |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1915 | for(channel = 0; ok && channel < encoder->protected_->channels; channel++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1916 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1917 | 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] | 1918 | } |
| 1919 | } |
| 1920 | for(channel = 0; ok && channel < 2; channel++) { |
| 1921 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1922 | 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] | 1923 | } |
| 1924 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1925 | ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual); |
| 1926 | 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 */ |
| 1927 | ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums); |
| 1928 | if(encoder->protected_->do_escape_coding) |
| 1929 | 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] | 1930 | |
| 1931 | if(ok) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1932 | encoder->private_->input_capacity = new_size; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1933 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1934 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1935 | |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 1936 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 1937 | if(ok && encoder->protected_->max_lpc_order > 0) { |
| 1938 | for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) { |
| 1939 | switch(encoder->protected_->apodizations[i].type) { |
| 1940 | case FLAC__APODIZATION_BARTLETT: |
| 1941 | FLAC__window_bartlett(encoder->private_->window[i], new_size); |
| 1942 | break; |
| 1943 | case FLAC__APODIZATION_BARTLETT_HANN: |
| 1944 | FLAC__window_bartlett_hann(encoder->private_->window[i], new_size); |
| 1945 | break; |
| 1946 | case FLAC__APODIZATION_BLACKMAN: |
| 1947 | FLAC__window_blackman(encoder->private_->window[i], new_size); |
| 1948 | break; |
| 1949 | case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE: |
| 1950 | FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_size); |
| 1951 | break; |
| 1952 | case FLAC__APODIZATION_CONNES: |
| 1953 | FLAC__window_connes(encoder->private_->window[i], new_size); |
| 1954 | break; |
| 1955 | case FLAC__APODIZATION_FLATTOP: |
| 1956 | FLAC__window_flattop(encoder->private_->window[i], new_size); |
| 1957 | break; |
| 1958 | case FLAC__APODIZATION_GAUSS: |
| 1959 | FLAC__window_gauss(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.gauss.stddev); |
| 1960 | break; |
| 1961 | case FLAC__APODIZATION_HAMMING: |
| 1962 | FLAC__window_hamming(encoder->private_->window[i], new_size); |
| 1963 | break; |
| 1964 | case FLAC__APODIZATION_HANN: |
| 1965 | FLAC__window_hann(encoder->private_->window[i], new_size); |
| 1966 | break; |
| 1967 | case FLAC__APODIZATION_KAISER_BESSEL: |
| 1968 | FLAC__window_kaiser_bessel(encoder->private_->window[i], new_size); |
| 1969 | break; |
| 1970 | case FLAC__APODIZATION_NUTTALL: |
| 1971 | FLAC__window_nuttall(encoder->private_->window[i], new_size); |
| 1972 | break; |
| 1973 | case FLAC__APODIZATION_RECTANGLE: |
| 1974 | FLAC__window_rectangle(encoder->private_->window[i], new_size); |
| 1975 | break; |
| 1976 | case FLAC__APODIZATION_TRIANGLE: |
| 1977 | FLAC__window_triangle(encoder->private_->window[i], new_size); |
| 1978 | break; |
| 1979 | case FLAC__APODIZATION_TUKEY: |
| 1980 | FLAC__window_tukey(encoder->private_->window[i], new_size, encoder->protected_->apodizations[i].parameters.tukey.p); |
| 1981 | break; |
| 1982 | case FLAC__APODIZATION_WELCH: |
| 1983 | FLAC__window_welch(encoder->private_->window[i], new_size); |
| 1984 | break; |
| 1985 | default: |
| 1986 | FLAC__ASSERT(0); |
| 1987 | /* double protection */ |
Josh Coalson | df59845 | 2006-04-28 00:13:34 +0000 | [diff] [blame^] | 1988 | FLAC__window_hann(encoder->private_->window[i], new_size); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 1989 | break; |
| 1990 | } |
| 1991 | } |
| 1992 | } |
| 1993 | #endif |
| 1994 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1995 | return ok; |
| 1996 | } |
| 1997 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1998 | FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples) |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1999 | { |
| 2000 | const FLAC__byte *buffer; |
| 2001 | unsigned bytes; |
| 2002 | |
| 2003 | FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame)); |
| 2004 | |
| 2005 | FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes); |
| 2006 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2007 | if(encoder->protected_->verify) { |
| 2008 | encoder->private_->verify.output.data = buffer; |
| 2009 | encoder->private_->verify.output.bytes = bytes; |
| 2010 | if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) { |
| 2011 | encoder->private_->verify.needs_magic_hack = true; |
| 2012 | } |
| 2013 | else { |
| 2014 | if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) { |
| 2015 | FLAC__bitbuffer_release_buffer(encoder->private_->frame); |
| 2016 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) |
| 2017 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 2018 | return false; |
| 2019 | } |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
Josh Coalson | dd19023 | 2002-12-29 09:30:23 +0000 | [diff] [blame] | 2024 | FLAC__bitbuffer_release_buffer(encoder->private_->frame); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2025 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING; |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 2026 | return false; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2027 | } |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 2028 | |
| 2029 | FLAC__bitbuffer_release_buffer(encoder->private_->frame); |
| 2030 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2031 | if(samples > 0) { |
| 2032 | encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize); |
| 2033 | encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize); |
| 2034 | } |
| 2035 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 2036 | return true; |
| 2037 | } |
| 2038 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2039 | FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2040 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2041 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2042 | |
| 2043 | /* |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 2044 | * Accumulate raw signal to the MD5 signature |
| 2045 | */ |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 2046 | 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] | 2047 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 2048 | return false; |
| 2049 | } |
| 2050 | |
| 2051 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2052 | * Process the frame header and subframes into the frame bitbuffer |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2053 | */ |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2054 | if(!process_subframes_(encoder, is_last_frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2055 | /* 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] | 2056 | return false; |
| 2057 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2058 | |
| 2059 | /* |
| 2060 | * Zero-pad the frame to a byte_boundary |
| 2061 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2062 | if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2063 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2064 | return false; |
| 2065 | } |
| 2066 | |
| 2067 | /* |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 2068 | * CRC-16 the whole thing |
| 2069 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2070 | FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame)); |
| 2071 | 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] | 2072 | |
| 2073 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2074 | * Write it |
| 2075 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2076 | if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) { |
| 2077 | /* 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] | 2078 | return false; |
| 2079 | } |
| 2080 | |
| 2081 | /* |
| 2082 | * Get ready for the next frame |
| 2083 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2084 | encoder->private_->current_sample_number = 0; |
| 2085 | encoder->private_->current_frame_number++; |
| 2086 | 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] | 2087 | |
| 2088 | return true; |
| 2089 | } |
| 2090 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2091 | FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2092 | { |
| 2093 | FLAC__FrameHeader frame_header; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2094 | 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] | 2095 | FLAC__bool do_independent, do_mid_side, precompute_partition_sums; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2096 | |
| 2097 | /* |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2098 | * Calculate the min,max Rice partition orders |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2099 | */ |
| 2100 | if(is_last_frame) { |
| 2101 | max_partition_order = 0; |
| 2102 | } |
| 2103 | else { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2104 | max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize); |
| 2105 | 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] | 2106 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2107 | min_partition_order = min(min_partition_order, max_partition_order); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2108 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2109 | 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] | 2110 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2111 | /* |
| 2112 | * Setup the frame |
| 2113 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2114 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2115 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2116 | return false; |
| 2117 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2118 | frame_header.blocksize = encoder->protected_->blocksize; |
| 2119 | frame_header.sample_rate = encoder->protected_->sample_rate; |
| 2120 | frame_header.channels = encoder->protected_->channels; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2121 | 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] | 2122 | frame_header.bits_per_sample = encoder->protected_->bits_per_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2123 | frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2124 | frame_header.number.frame_number = encoder->private_->current_frame_number; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2125 | |
| 2126 | /* |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2127 | * Figure out what channel assignments to try |
| 2128 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2129 | if(encoder->protected_->do_mid_side_stereo) { |
| 2130 | if(encoder->protected_->loose_mid_side_stereo) { |
| 2131 | if(encoder->private_->loose_mid_side_stereo_frame_count == 0) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2132 | do_independent = true; |
| 2133 | do_mid_side = true; |
| 2134 | } |
| 2135 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2136 | do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2137 | do_mid_side = !do_independent; |
| 2138 | } |
| 2139 | } |
| 2140 | else { |
| 2141 | do_independent = true; |
| 2142 | do_mid_side = true; |
| 2143 | } |
| 2144 | } |
| 2145 | else { |
| 2146 | do_independent = true; |
| 2147 | do_mid_side = false; |
| 2148 | } |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2149 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 2150 | FLAC__ASSERT(do_independent || do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2151 | |
| 2152 | /* |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2153 | * Check for wasted bits; set effective bps for each subframe |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2154 | */ |
| 2155 | if(do_independent) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2156 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2157 | 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] | 2158 | encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w; |
| 2159 | encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2160 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2161 | } |
| 2162 | if(do_mid_side) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2163 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2164 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2165 | 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] | 2166 | encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w; |
| 2167 | 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] | 2168 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2172 | * First do a normal encoding pass of each independent channel |
| 2173 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2174 | if(do_independent) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2175 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2176 | if(! |
| 2177 | process_subframe_( |
| 2178 | encoder, |
| 2179 | min_partition_order, |
| 2180 | max_partition_order, |
| 2181 | precompute_partition_sums, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2182 | &frame_header, |
| 2183 | encoder->private_->subframe_bps[channel], |
| 2184 | encoder->private_->integer_signal[channel], |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2185 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2186 | encoder->private_->real_signal[channel], |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2187 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2188 | encoder->private_->subframe_workspace_ptr[channel], |
| 2189 | encoder->private_->partitioned_rice_contents_workspace_ptr[channel], |
| 2190 | encoder->private_->residual_workspace[channel], |
| 2191 | encoder->private_->best_subframe+channel, |
| 2192 | encoder->private_->best_subframe_bits+channel |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2193 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2194 | ,channel |
| 2195 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2196 | ) |
| 2197 | ) |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2198 | return false; |
| 2199 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | /* |
| 2203 | * Now do mid and side channels if requested |
| 2204 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2205 | if(do_mid_side) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2206 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2207 | |
| 2208 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2209 | if(! |
| 2210 | process_subframe_( |
| 2211 | encoder, |
| 2212 | min_partition_order, |
| 2213 | max_partition_order, |
| 2214 | precompute_partition_sums, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2215 | &frame_header, |
| 2216 | encoder->private_->subframe_bps_mid_side[channel], |
| 2217 | encoder->private_->integer_signal_mid_side[channel], |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2218 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2219 | encoder->private_->real_signal_mid_side[channel], |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2220 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2221 | encoder->private_->subframe_workspace_ptr_mid_side[channel], |
| 2222 | encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel], |
| 2223 | encoder->private_->residual_workspace_mid_side[channel], |
| 2224 | encoder->private_->best_subframe_mid_side+channel, |
| 2225 | encoder->private_->best_subframe_bits_mid_side+channel |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2226 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2227 | ,channel |
| 2228 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2229 | ) |
| 2230 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2231 | return false; |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | /* |
| 2236 | * Compose the frame bitbuffer |
| 2237 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2238 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2239 | unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ |
| 2240 | 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] | 2241 | FLAC__ChannelAssignment channel_assignment; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2242 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2243 | unsigned left_bits = 0, right_bits = 0; |
| 2244 | #endif |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2245 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2246 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2247 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2248 | if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) { |
| 2249 | 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] | 2250 | } |
| 2251 | else { |
| 2252 | unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */ |
| 2253 | unsigned min_bits; |
| 2254 | FLAC__ChannelAssignment ca; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2255 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 2256 | FLAC__ASSERT(do_independent && do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2257 | |
| 2258 | /* 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] | 2259 | bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1]; |
| 2260 | bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 2261 | bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 2262 | 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] | 2263 | |
Josh Coalson | 7424d2f | 2002-11-06 07:10:38 +0000 | [diff] [blame] | 2264 | for(channel_assignment = (FLAC__ChannelAssignment)0, min_bits = bits[0], ca = (FLAC__ChannelAssignment)1; (int)ca <= 3; ca = (FLAC__ChannelAssignment)((int)ca + 1)) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2265 | if(bits[ca] < min_bits) { |
| 2266 | min_bits = bits[ca]; |
| 2267 | channel_assignment = ca; |
| 2268 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2269 | } |
| 2270 | } |
| 2271 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2272 | frame_header.channel_assignment = channel_assignment; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2273 | |
Josh Coalson | 3e7a96e | 2004-07-23 05:18:22 +0000 | [diff] [blame] | 2274 | if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2275 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2276 | return false; |
| 2277 | } |
| 2278 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2279 | switch(channel_assignment) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2280 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2281 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]]; |
| 2282 | right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]]; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2283 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2284 | left_bits = encoder->private_->best_subframe_bits [0]; |
| 2285 | right_bits = encoder->private_->best_subframe_bits [1]; |
| 2286 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2287 | break; |
| 2288 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2289 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]]; |
| 2290 | right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2291 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2292 | left_bits = encoder->private_->best_subframe_bits [0]; |
| 2293 | right_bits = encoder->private_->best_subframe_bits_mid_side [1]; |
| 2294 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2295 | break; |
| 2296 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2297 | left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
| 2298 | right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]]; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2299 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2300 | left_bits = encoder->private_->best_subframe_bits_mid_side [1]; |
| 2301 | right_bits = encoder->private_->best_subframe_bits [1]; |
| 2302 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2303 | break; |
| 2304 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2305 | left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]]; |
| 2306 | right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2307 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2308 | left_bits = encoder->private_->best_subframe_bits_mid_side [0]; |
| 2309 | right_bits = encoder->private_->best_subframe_bits_mid_side [1]; |
| 2310 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2311 | break; |
| 2312 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 2313 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2314 | } |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2315 | |
| 2316 | switch(channel_assignment) { |
| 2317 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2318 | left_bps = encoder->private_->subframe_bps [0]; |
| 2319 | right_bps = encoder->private_->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2320 | break; |
| 2321 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2322 | left_bps = encoder->private_->subframe_bps [0]; |
| 2323 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2324 | break; |
| 2325 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2326 | left_bps = encoder->private_->subframe_bps_mid_side[1]; |
| 2327 | right_bps = encoder->private_->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2328 | break; |
| 2329 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2330 | left_bps = encoder->private_->subframe_bps_mid_side[0]; |
| 2331 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2332 | break; |
| 2333 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 2334 | FLAC__ASSERT(0); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | /* note that encoder_add_subframe_ sets the state for us in case of an error */ |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2338 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2339 | if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame, left_bits)) |
| 2340 | return false; |
| 2341 | if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame, right_bits)) |
| 2342 | return false; |
| 2343 | #else |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2344 | 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] | 2345 | return false; |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2346 | 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] | 2347 | return false; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2348 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2349 | } |
| 2350 | else { |
Josh Coalson | 3e7a96e | 2004-07-23 05:18:22 +0000 | [diff] [blame] | 2351 | if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2352 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2353 | return false; |
| 2354 | } |
| 2355 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2356 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2357 | if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame, encoder->private_->best_subframe_bits[channel])) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2358 | /* the above function sets the state for us in case of an error */ |
| 2359 | return false; |
| 2360 | } |
| 2361 | } |
| 2362 | } |
| 2363 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2364 | if(encoder->protected_->loose_mid_side_stereo) { |
| 2365 | encoder->private_->loose_mid_side_stereo_frame_count++; |
| 2366 | if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames) |
| 2367 | encoder->private_->loose_mid_side_stereo_frame_count = 0; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2370 | encoder->private_->last_channel_assignment = frame_header.channel_assignment; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2371 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2372 | return true; |
| 2373 | } |
| 2374 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2375 | FLAC__bool process_subframe_( |
| 2376 | FLAC__StreamEncoder *encoder, |
| 2377 | unsigned min_partition_order, |
| 2378 | unsigned max_partition_order, |
| 2379 | FLAC__bool precompute_partition_sums, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2380 | const FLAC__FrameHeader *frame_header, |
| 2381 | unsigned subframe_bps, |
| 2382 | const FLAC__int32 integer_signal[], |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2383 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2384 | const FLAC__real real_signal[], |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2385 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2386 | FLAC__Subframe *subframe[2], |
| 2387 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2], |
| 2388 | FLAC__int32 *residual[2], |
| 2389 | unsigned *best_subframe, |
| 2390 | unsigned *best_bits |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2391 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2392 | ,unsigned subframe_number |
| 2393 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2394 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2395 | { |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2396 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 0975843 | 2004-10-20 00:21:50 +0000 | [diff] [blame] | 2397 | FLAC__float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2398 | #else |
| 2399 | FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 2400 | #endif |
| 2401 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 0975843 | 2004-10-20 00:21:50 +0000 | [diff] [blame] | 2402 | FLAC__double lpc_residual_bits_per_sample; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2403 | 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 | 0975843 | 2004-10-20 00:21:50 +0000 | [diff] [blame] | 2404 | FLAC__double lpc_error[FLAC__MAX_LPC_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2405 | unsigned min_lpc_order, max_lpc_order, lpc_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2406 | unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2407 | #endif |
| 2408 | unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2409 | unsigned rice_parameter; |
| 2410 | unsigned _candidate_bits, _best_bits; |
| 2411 | unsigned _best_subframe; |
| 2412 | |
| 2413 | /* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 2414 | _best_subframe = 0; |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2415 | if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) |
| 2416 | _best_bits = UINT_MAX; |
| 2417 | else |
| 2418 | _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] | 2419 | |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2420 | if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { |
| 2421 | unsigned signal_is_constant = false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2422 | 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 | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2423 | /* check for constant subframe */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2424 | if( |
| 2425 | !encoder->private_->disable_constant_subframes && |
| 2426 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2427 | fixed_residual_bits_per_sample[1] == 0.0 |
| 2428 | #else |
| 2429 | fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO |
| 2430 | #endif |
| 2431 | ) { |
| 2432 | /* the above means it's possible all samples are the same value; now double-check it: */ |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2433 | unsigned i; |
| 2434 | signal_is_constant = true; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2435 | for(i = 1; i < frame_header->blocksize; i++) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2436 | if(integer_signal[0] != integer_signal[i]) { |
| 2437 | signal_is_constant = false; |
| 2438 | break; |
| 2439 | } |
| 2440 | } |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2441 | } |
| 2442 | if(signal_is_constant) { |
| 2443 | _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]); |
| 2444 | if(_candidate_bits < _best_bits) { |
| 2445 | _best_subframe = !_best_subframe; |
| 2446 | _best_bits = _candidate_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | else { |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2450 | if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) { |
| 2451 | /* encode fixed */ |
| 2452 | if(encoder->protected_->do_exhaustive_model_search) { |
| 2453 | min_fixed_order = 0; |
| 2454 | max_fixed_order = FLAC__MAX_FIXED_ORDER; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2455 | } |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2456 | else { |
| 2457 | min_fixed_order = max_fixed_order = guess_fixed_order; |
| 2458 | } |
| 2459 | for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2460 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 0975843 | 2004-10-20 00:21:50 +0000 | [diff] [blame] | 2461 | if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__float)subframe_bps) |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2462 | continue; /* don't even try */ |
| 2463 | 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 | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2464 | #else |
| 2465 | if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps) |
| 2466 | continue; /* don't even try */ |
| 2467 | rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > FLAC__FP_ZERO)? (unsigned)FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]+FLAC__FP_ONE_HALF) : 0; /* 0.5 is for rounding */ |
| 2468 | #endif |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2469 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2470 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
| 2471 | #ifdef DEBUG_VERBOSE |
| 2472 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2473 | #endif |
| 2474 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 2475 | } |
| 2476 | _candidate_bits = |
| 2477 | evaluate_fixed_subframe_( |
| 2478 | encoder, |
| 2479 | integer_signal, |
| 2480 | residual[!_best_subframe], |
| 2481 | encoder->private_->abs_residual, |
| 2482 | encoder->private_->abs_residual_partition_sums, |
| 2483 | encoder->private_->raw_bits_per_partition, |
| 2484 | frame_header->blocksize, |
| 2485 | subframe_bps, |
| 2486 | fixed_order, |
| 2487 | rice_parameter, |
| 2488 | min_partition_order, |
| 2489 | max_partition_order, |
| 2490 | precompute_partition_sums, |
| 2491 | encoder->protected_->do_escape_coding, |
| 2492 | encoder->protected_->rice_parameter_search_dist, |
| 2493 | subframe[!_best_subframe], |
| 2494 | partitioned_rice_contents[!_best_subframe] |
| 2495 | ); |
| 2496 | if(_candidate_bits < _best_bits) { |
| 2497 | _best_subframe = !_best_subframe; |
| 2498 | _best_bits = _candidate_bits; |
| 2499 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2500 | } |
| 2501 | } |
| 2502 | |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2503 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2504 | /* encode lpc */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2505 | if(encoder->protected_->max_lpc_order > 0) { |
| 2506 | if(encoder->protected_->max_lpc_order >= frame_header->blocksize) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2507 | max_lpc_order = frame_header->blocksize-1; |
| 2508 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2509 | max_lpc_order = encoder->protected_->max_lpc_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2510 | if(max_lpc_order > 0) { |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2511 | unsigned a; |
| 2512 | for (a = 0; a < encoder->protected_->num_apodizations; a++) { |
| 2513 | FLAC__lpc_apply_apodization(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize); |
| 2514 | encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc); |
| 2515 | /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */ |
| 2516 | if(autoc[0] != 0.0) { |
| 2517 | FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error); |
| 2518 | if(encoder->protected_->do_exhaustive_model_search) { |
| 2519 | min_lpc_order = 1; |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 2520 | } |
| 2521 | else { |
Josh Coalson | df59845 | 2006-04-28 00:13:34 +0000 | [diff] [blame^] | 2522 | const unsigned guess_lpc_order = |
| 2523 | FLAC__lpc_compute_best_order( |
| 2524 | lpc_error, |
| 2525 | max_lpc_order, |
| 2526 | frame_header->blocksize, |
| 2527 | subframe_bps + ( |
| 2528 | encoder->protected_->do_qlp_coeff_prec_search? |
| 2529 | FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */ |
| 2530 | encoder->protected_->qlp_coeff_precision |
| 2531 | ) |
| 2532 | ); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2533 | min_lpc_order = max_lpc_order = guess_lpc_order; |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 2534 | } |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2535 | for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) { |
| 2536 | lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order); |
| 2537 | if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps) |
| 2538 | continue; /* don't even try */ |
| 2539 | rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */ |
| 2540 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
| 2541 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | df59845 | 2006-04-28 00:13:34 +0000 | [diff] [blame^] | 2542 | #ifdef DEBUG_VERBOSE |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2543 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
Josh Coalson | df59845 | 2006-04-28 00:13:34 +0000 | [diff] [blame^] | 2544 | #endif |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2545 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 2546 | } |
| 2547 | if(encoder->protected_->do_qlp_coeff_prec_search) { |
| 2548 | min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
Josh Coalson | df59845 | 2006-04-28 00:13:34 +0000 | [diff] [blame^] | 2549 | /* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */ |
| 2550 | if(subframe_bps <= 17) { |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2551 | max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION); |
Josh Coalson | df59845 | 2006-04-28 00:13:34 +0000 | [diff] [blame^] | 2552 | max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision); |
| 2553 | } |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2554 | else |
| 2555 | max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION; |
| 2556 | } |
| 2557 | else { |
| 2558 | min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision; |
| 2559 | } |
| 2560 | for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) { |
| 2561 | _candidate_bits = |
| 2562 | evaluate_lpc_subframe_( |
| 2563 | encoder, |
| 2564 | integer_signal, |
| 2565 | residual[!_best_subframe], |
| 2566 | encoder->private_->abs_residual, |
| 2567 | encoder->private_->abs_residual_partition_sums, |
| 2568 | encoder->private_->raw_bits_per_partition, |
| 2569 | encoder->private_->lp_coeff[lpc_order-1], |
| 2570 | frame_header->blocksize, |
| 2571 | subframe_bps, |
| 2572 | lpc_order, |
| 2573 | qlp_coeff_precision, |
| 2574 | rice_parameter, |
| 2575 | min_partition_order, |
| 2576 | max_partition_order, |
| 2577 | precompute_partition_sums, |
| 2578 | encoder->protected_->do_escape_coding, |
| 2579 | encoder->protected_->rice_parameter_search_dist, |
| 2580 | subframe[!_best_subframe], |
| 2581 | partitioned_rice_contents[!_best_subframe] |
| 2582 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2583 | ,frame_header->number.frame_number |
| 2584 | ,subframe_number |
| 2585 | ,encoder->protected_->apodizations[a] |
| 2586 | #endif |
| 2587 | ); |
| 2588 | if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 2589 | if(_candidate_bits < _best_bits) { |
| 2590 | _best_subframe = !_best_subframe; |
| 2591 | _best_bits = _candidate_bits; |
| 2592 | } |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 2593 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2594 | } |
| 2595 | } |
| 2596 | } |
| 2597 | } |
| 2598 | } |
| 2599 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2600 | #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2601 | } |
| 2602 | } |
| 2603 | |
Josh Coalson | 7269580 | 2002-10-11 06:25:16 +0000 | [diff] [blame] | 2604 | /* under rare circumstances this can happen when all but lpc subframe types are disabled: */ |
| 2605 | if(_best_bits == UINT_MAX) { |
| 2606 | FLAC__ASSERT(_best_subframe == 0); |
| 2607 | _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]); |
| 2608 | } |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2609 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2610 | *best_subframe = _best_subframe; |
| 2611 | *best_bits = _best_bits; |
| 2612 | |
| 2613 | return true; |
| 2614 | } |
| 2615 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2616 | FLAC__bool add_subframe_( |
| 2617 | FLAC__StreamEncoder *encoder, |
| 2618 | const FLAC__FrameHeader *frame_header, |
| 2619 | unsigned subframe_bps, |
| 2620 | const FLAC__Subframe *subframe, |
| 2621 | FLAC__BitBuffer *frame |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2622 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2623 | ,unsigned subframe_bits |
| 2624 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2625 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2626 | { |
| 2627 | switch(subframe->type) { |
| 2628 | case FLAC__SUBFRAME_TYPE_CONSTANT: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2629 | 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] | 2630 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2631 | return false; |
| 2632 | } |
| 2633 | break; |
| 2634 | case FLAC__SUBFRAME_TYPE_FIXED: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2635 | 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] | 2636 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2637 | return false; |
| 2638 | } |
| 2639 | break; |
| 2640 | case FLAC__SUBFRAME_TYPE_LPC: |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2641 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2642 | fprintf(stderr, "WIN:\tframe=%u\tsubframe=?\torder=%u\twindow=%s\tbits=%u\n", frame_header->number.frame_number, subframe->data.lpc.order, subframe->data.lpc.window_type, subframe_bits); |
| 2643 | #endif |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2644 | 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] | 2645 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2646 | return false; |
| 2647 | } |
| 2648 | break; |
| 2649 | case FLAC__SUBFRAME_TYPE_VERBATIM: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2650 | 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] | 2651 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2652 | return false; |
| 2653 | } |
| 2654 | break; |
| 2655 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 2656 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | return true; |
| 2660 | } |
| 2661 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2662 | unsigned evaluate_constant_subframe_( |
| 2663 | const FLAC__int32 signal, |
| 2664 | unsigned subframe_bps, |
| 2665 | FLAC__Subframe *subframe |
| 2666 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2667 | { |
| 2668 | subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 2669 | subframe->data.constant.value = signal; |
| 2670 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2671 | 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] | 2672 | } |
| 2673 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2674 | unsigned evaluate_fixed_subframe_( |
| 2675 | FLAC__StreamEncoder *encoder, |
| 2676 | const FLAC__int32 signal[], |
| 2677 | FLAC__int32 residual[], |
| 2678 | FLAC__uint32 abs_residual[], |
| 2679 | FLAC__uint64 abs_residual_partition_sums[], |
| 2680 | unsigned raw_bits_per_partition[], |
| 2681 | unsigned blocksize, |
| 2682 | unsigned subframe_bps, |
| 2683 | unsigned order, |
| 2684 | unsigned rice_parameter, |
| 2685 | unsigned min_partition_order, |
| 2686 | unsigned max_partition_order, |
| 2687 | FLAC__bool precompute_partition_sums, |
| 2688 | FLAC__bool do_escape_coding, |
| 2689 | unsigned rice_parameter_search_dist, |
| 2690 | FLAC__Subframe *subframe, |
| 2691 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 2692 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2693 | { |
| 2694 | unsigned i, residual_bits; |
| 2695 | const unsigned residual_samples = blocksize - order; |
| 2696 | |
| 2697 | FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual); |
| 2698 | |
| 2699 | subframe->type = FLAC__SUBFRAME_TYPE_FIXED; |
| 2700 | |
| 2701 | 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] | 2702 | 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] | 2703 | subframe->data.fixed.residual = residual; |
| 2704 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2705 | residual_bits = |
| 2706 | find_best_partition_order_( |
| 2707 | encoder->private_, |
| 2708 | residual, |
| 2709 | abs_residual, |
| 2710 | abs_residual_partition_sums, |
| 2711 | raw_bits_per_partition, |
| 2712 | residual_samples, |
| 2713 | order, |
| 2714 | rice_parameter, |
| 2715 | min_partition_order, |
| 2716 | max_partition_order, |
| 2717 | precompute_partition_sums, |
| 2718 | do_escape_coding, |
| 2719 | rice_parameter_search_dist, |
| 2720 | &subframe->data.fixed.entropy_coding_method.data.partitioned_rice |
| 2721 | ); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2722 | |
| 2723 | subframe->data.fixed.order = order; |
| 2724 | for(i = 0; i < order; i++) |
| 2725 | subframe->data.fixed.warmup[i] = signal[i]; |
| 2726 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2727 | 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] | 2728 | } |
| 2729 | |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2730 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2731 | unsigned evaluate_lpc_subframe_( |
| 2732 | FLAC__StreamEncoder *encoder, |
| 2733 | const FLAC__int32 signal[], |
| 2734 | FLAC__int32 residual[], |
| 2735 | FLAC__uint32 abs_residual[], |
| 2736 | FLAC__uint64 abs_residual_partition_sums[], |
| 2737 | unsigned raw_bits_per_partition[], |
| 2738 | const FLAC__real lp_coeff[], |
| 2739 | unsigned blocksize, |
| 2740 | unsigned subframe_bps, |
| 2741 | unsigned order, |
| 2742 | unsigned qlp_coeff_precision, |
| 2743 | unsigned rice_parameter, |
| 2744 | unsigned min_partition_order, |
| 2745 | unsigned max_partition_order, |
| 2746 | FLAC__bool precompute_partition_sums, |
| 2747 | FLAC__bool do_escape_coding, |
| 2748 | unsigned rice_parameter_search_dist, |
| 2749 | FLAC__Subframe *subframe, |
| 2750 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2751 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2752 | ,unsigned frame_number |
| 2753 | ,unsigned subframe_number |
| 2754 | ,FLAC__ApodizationSpecification aspec |
| 2755 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2756 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2757 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2758 | FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2759 | unsigned i, residual_bits; |
| 2760 | int quantization, ret; |
| 2761 | const unsigned residual_samples = blocksize - order; |
| 2762 | |
Josh Coalson | 20ac2c1 | 2002-08-30 05:47:14 +0000 | [diff] [blame] | 2763 | /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps streams */ |
| 2764 | if(subframe_bps <= 16) { |
| 2765 | FLAC__ASSERT(order > 0); |
| 2766 | FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER); |
| 2767 | qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order)); |
| 2768 | } |
| 2769 | |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2770 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2771 | if (aspec.type == FLAC__APODIZATION_GAUSS) |
| 2772 | snprintf(subframe->data.lpc.window_type, sizeof subframe->data.lpc.window_type, "%s(%0.5f)", winstr[aspec.type], aspec.parameters.gauss.stddev); |
| 2773 | else if (aspec.type == FLAC__APODIZATION_TUKEY) |
| 2774 | snprintf(subframe->data.lpc.window_type, sizeof subframe->data.lpc.window_type, "%s(%0.5f)", winstr[aspec.type], aspec.parameters.tukey.p); |
| 2775 | else |
| 2776 | strncpy(subframe->data.lpc.window_type, winstr[aspec.type], sizeof subframe->data.lpc.window_type); |
| 2777 | #endif |
| 2778 | |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 2779 | ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2780 | if(ret != 0) |
| 2781 | return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ |
| 2782 | |
Josh Coalson | fb9d18f | 2002-10-21 07:04:07 +0000 | [diff] [blame] | 2783 | if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32) |
| 2784 | if(subframe_bps <= 16 && qlp_coeff_precision <= 16) |
| 2785 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
| 2786 | else |
| 2787 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 2788 | else |
| 2789 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2790 | |
| 2791 | subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 2792 | |
| 2793 | 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] | 2794 | 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] | 2795 | subframe->data.lpc.residual = residual; |
| 2796 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2797 | residual_bits = |
| 2798 | find_best_partition_order_( |
| 2799 | encoder->private_, |
| 2800 | residual, |
| 2801 | abs_residual, |
| 2802 | abs_residual_partition_sums, |
| 2803 | raw_bits_per_partition, |
| 2804 | residual_samples, |
| 2805 | order, |
| 2806 | rice_parameter, |
| 2807 | min_partition_order, |
| 2808 | max_partition_order, |
| 2809 | precompute_partition_sums, |
| 2810 | do_escape_coding, |
| 2811 | rice_parameter_search_dist, |
| 2812 | &subframe->data.fixed.entropy_coding_method.data.partitioned_rice |
| 2813 | ); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2814 | |
| 2815 | subframe->data.lpc.order = order; |
| 2816 | subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 2817 | subframe->data.lpc.quantization_level = quantization; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2818 | 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] | 2819 | for(i = 0; i < order; i++) |
| 2820 | subframe->data.lpc.warmup[i] = signal[i]; |
| 2821 | |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2822 | #ifdef WINDOW_DEBUG_OUTPUT |
| 2823 | fprintf(stderr, "SWIN:\tframe=%u\tsubframe=%u\torder=%u\twindow=%s\tbits=%u\n", frame_number, subframe_number, order, subframe->data.lpc.window_type, 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); |
| 2824 | #endif |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2825 | 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] | 2826 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2827 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2828 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2829 | unsigned evaluate_verbatim_subframe_( |
| 2830 | const FLAC__int32 signal[], |
| 2831 | unsigned blocksize, |
| 2832 | unsigned subframe_bps, |
| 2833 | FLAC__Subframe *subframe |
| 2834 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2835 | { |
| 2836 | subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 2837 | |
| 2838 | subframe->data.verbatim.data = signal; |
| 2839 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 2840 | 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] | 2841 | } |
| 2842 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2843 | unsigned find_best_partition_order_( |
| 2844 | FLAC__StreamEncoderPrivate *private_, |
| 2845 | const FLAC__int32 residual[], |
| 2846 | FLAC__uint32 abs_residual[], |
| 2847 | FLAC__uint64 abs_residual_partition_sums[], |
| 2848 | unsigned raw_bits_per_partition[], |
| 2849 | unsigned residual_samples, |
| 2850 | unsigned predictor_order, |
| 2851 | unsigned rice_parameter, |
| 2852 | unsigned min_partition_order, |
| 2853 | unsigned max_partition_order, |
| 2854 | FLAC__bool precompute_partition_sums, |
| 2855 | FLAC__bool do_escape_coding, |
| 2856 | unsigned rice_parameter_search_dist, |
| 2857 | FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice |
| 2858 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2859 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2860 | FLAC__int32 r; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2861 | unsigned residual_bits, best_residual_bits = 0; |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 2862 | unsigned residual_sample; |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 2863 | unsigned best_parameters_index = 0; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2864 | const unsigned blocksize = residual_samples + predictor_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2865 | |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2866 | /* compute abs(residual) for use later */ |
| 2867 | for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) { |
| 2868 | r = residual[residual_sample]; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2869 | abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r); |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2872 | 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] | 2873 | min_partition_order = min(min_partition_order, max_partition_order); |
| 2874 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2875 | if(precompute_partition_sums) { |
| 2876 | int partition_order; |
| 2877 | unsigned sum; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2878 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2879 | 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] | 2880 | |
| 2881 | if(do_escape_coding) |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2882 | 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] | 2883 | |
| 2884 | for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) { |
| 2885 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2886 | if(! |
| 2887 | set_partitioned_rice_with_precompute_( |
| 2888 | residual, |
| 2889 | abs_residual_partition_sums+sum, |
| 2890 | raw_bits_per_partition+sum, |
| 2891 | residual_samples, |
| 2892 | predictor_order, |
| 2893 | rice_parameter, |
| 2894 | rice_parameter_search_dist, |
| 2895 | (unsigned)partition_order, |
| 2896 | do_escape_coding, |
| 2897 | &private_->partitioned_rice_contents_extra[!best_parameters_index], |
| 2898 | &residual_bits |
| 2899 | ) |
| 2900 | ) |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 2901 | #else |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2902 | if(! |
| 2903 | set_partitioned_rice_with_precompute_( |
| 2904 | abs_residual, |
| 2905 | abs_residual_partition_sums+sum, |
| 2906 | raw_bits_per_partition+sum, |
| 2907 | residual_samples, |
| 2908 | predictor_order, |
| 2909 | rice_parameter, |
| 2910 | rice_parameter_search_dist, |
| 2911 | (unsigned)partition_order, |
| 2912 | do_escape_coding, |
| 2913 | &private_->partitioned_rice_contents_extra[!best_parameters_index], |
| 2914 | &residual_bits |
| 2915 | ) |
| 2916 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2917 | #endif |
| 2918 | { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2919 | FLAC__ASSERT(best_residual_bits != 0); |
| 2920 | break; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2921 | } |
| 2922 | sum += 1u << partition_order; |
| 2923 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 2924 | best_residual_bits = residual_bits; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2925 | best_parameters_index = !best_parameters_index; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2926 | best_partitioned_rice->order = partition_order; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2927 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 2928 | } |
| 2929 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2930 | else { |
| 2931 | unsigned partition_order; |
| 2932 | for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) { |
| 2933 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2934 | if(! |
| 2935 | set_partitioned_rice_( |
| 2936 | abs_residual, |
| 2937 | residual, |
| 2938 | residual_samples, |
| 2939 | predictor_order, |
| 2940 | rice_parameter, |
| 2941 | rice_parameter_search_dist, |
| 2942 | partition_order, |
| 2943 | &private_->partitioned_rice_contents_extra[!best_parameters_index], |
| 2944 | &residual_bits |
| 2945 | ) |
| 2946 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2947 | #else |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2948 | if(! |
| 2949 | set_partitioned_rice_( |
| 2950 | abs_residual, |
| 2951 | residual_samples, |
| 2952 | predictor_order, |
| 2953 | rice_parameter, |
| 2954 | rice_parameter_search_dist, |
| 2955 | partition_order, |
| 2956 | &private_->partitioned_rice_contents_extra[!best_parameters_index], |
| 2957 | &residual_bits |
| 2958 | ) |
| 2959 | ) |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 2960 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2961 | { |
| 2962 | FLAC__ASSERT(best_residual_bits != 0); |
| 2963 | break; |
| 2964 | } |
| 2965 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 2966 | best_residual_bits = residual_bits; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2967 | best_parameters_index = !best_parameters_index; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2968 | best_partitioned_rice->order = partition_order; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2969 | } |
| 2970 | } |
| 2971 | } |
| 2972 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2973 | /* |
Josh Coalson | 20ac2c1 | 2002-08-30 05:47:14 +0000 | [diff] [blame] | 2974 | * We are allowed to de-const the pointer based on our special knowledge; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2975 | * it is const to the outside world. |
| 2976 | */ |
| 2977 | { |
| 2978 | FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents; |
| 2979 | FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order)); |
| 2980 | memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order))); |
| 2981 | memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order))); |
| 2982 | } |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2983 | |
| 2984 | return best_residual_bits; |
| 2985 | } |
| 2986 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 2987 | void precompute_partition_info_sums_( |
| 2988 | const FLAC__uint32 abs_residual[], |
| 2989 | FLAC__uint64 abs_residual_partition_sums[], |
| 2990 | unsigned residual_samples, |
| 2991 | unsigned predictor_order, |
| 2992 | unsigned min_partition_order, |
| 2993 | unsigned max_partition_order |
| 2994 | ) |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2995 | { |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2996 | int partition_order; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 2997 | unsigned from_partition, to_partition = 0; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2998 | const unsigned blocksize = residual_samples + predictor_order; |
| 2999 | |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 3000 | /* first do max_partition_order */ |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 3001 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3002 | FLAC__uint64 abs_residual_partition_sum; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 3003 | FLAC__uint32 abs_r; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 3004 | unsigned partition, partition_sample, partition_samples, residual_sample; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 3005 | const unsigned partitions = 1u << partition_order; |
| 3006 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 3007 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3008 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 3009 | |
| 3010 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 3011 | partition_samples = default_partition_samples; |
| 3012 | if(partition == 0) |
| 3013 | partition_samples -= predictor_order; |
| 3014 | abs_residual_partition_sum = 0; |
| 3015 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 3016 | abs_r = abs_residual[residual_sample]; |
| 3017 | abs_residual_partition_sum += abs_r; |
| 3018 | residual_sample++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 3019 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3020 | abs_residual_partition_sums[partition] = abs_residual_partition_sum; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 3021 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3022 | to_partition = partitions; |
| 3023 | break; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 3024 | } |
Josh Coalson | f76a361 | 2001-04-18 02:28:11 +0000 | [diff] [blame] | 3025 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3026 | /* now merge partitions for lower orders */ |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame] | 3027 | 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] | 3028 | FLAC__uint64 s; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 3029 | unsigned i; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 3030 | const unsigned partitions = 1u << partition_order; |
| 3031 | for(i = 0; i < partitions; i++) { |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 3032 | s = abs_residual_partition_sums[from_partition]; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 3033 | from_partition++; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 3034 | 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] | 3035 | from_partition++; |
| 3036 | to_partition++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 3037 | } |
| 3038 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3039 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3040 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3041 | void precompute_partition_info_escapes_( |
| 3042 | const FLAC__int32 residual[], |
| 3043 | unsigned raw_bits_per_partition[], |
| 3044 | unsigned residual_samples, |
| 3045 | unsigned predictor_order, |
| 3046 | unsigned min_partition_order, |
| 3047 | unsigned max_partition_order |
| 3048 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3049 | { |
| 3050 | int partition_order; |
| 3051 | unsigned from_partition, to_partition = 0; |
| 3052 | const unsigned blocksize = residual_samples + predictor_order; |
| 3053 | |
| 3054 | /* first do max_partition_order */ |
| 3055 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
| 3056 | FLAC__int32 r, residual_partition_min, residual_partition_max; |
| 3057 | unsigned silog2_min, silog2_max; |
| 3058 | unsigned partition, partition_sample, partition_samples, residual_sample; |
| 3059 | const unsigned partitions = 1u << partition_order; |
| 3060 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 3061 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3062 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 3063 | |
| 3064 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 3065 | partition_samples = default_partition_samples; |
| 3066 | if(partition == 0) |
| 3067 | partition_samples -= predictor_order; |
| 3068 | residual_partition_min = residual_partition_max = 0; |
| 3069 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 3070 | r = residual[residual_sample]; |
| 3071 | if(r < residual_partition_min) |
| 3072 | residual_partition_min = r; |
| 3073 | else if(r > residual_partition_max) |
| 3074 | residual_partition_max = r; |
| 3075 | residual_sample++; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3076 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3077 | silog2_min = FLAC__bitmath_silog2(residual_partition_min); |
| 3078 | silog2_max = FLAC__bitmath_silog2(residual_partition_max); |
| 3079 | raw_bits_per_partition[partition] = max(silog2_min, silog2_max); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3080 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3081 | to_partition = partitions; |
| 3082 | break; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | /* now merge partitions for lower orders */ |
| 3086 | for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
| 3087 | unsigned m; |
| 3088 | unsigned i; |
| 3089 | const unsigned partitions = 1u << partition_order; |
| 3090 | for(i = 0; i < partitions; i++) { |
| 3091 | m = raw_bits_per_partition[from_partition]; |
| 3092 | from_partition++; |
| 3093 | raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]); |
| 3094 | from_partition++; |
| 3095 | to_partition++; |
| 3096 | } |
| 3097 | } |
| 3098 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3099 | |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 3100 | #ifdef VARIABLE_RICE_BITS |
| 3101 | #undef VARIABLE_RICE_BITS |
| 3102 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3103 | #ifndef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 3104 | #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter)) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3105 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 3106 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3107 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3108 | FLAC__bool set_partitioned_rice_( |
| 3109 | const FLAC__uint32 abs_residual[], |
| 3110 | const FLAC__int32 residual[], |
| 3111 | const unsigned residual_samples, |
| 3112 | const unsigned predictor_order, |
| 3113 | const unsigned suggested_rice_parameter, |
| 3114 | const unsigned rice_parameter_search_dist, |
| 3115 | const unsigned partition_order, |
| 3116 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 3117 | unsigned *bits |
| 3118 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3119 | #else |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3120 | FLAC__bool set_partitioned_rice_( |
| 3121 | const FLAC__uint32 abs_residual[], |
| 3122 | const unsigned residual_samples, |
| 3123 | const unsigned predictor_order, |
| 3124 | const unsigned suggested_rice_parameter, |
| 3125 | const unsigned rice_parameter_search_dist, |
| 3126 | const unsigned partition_order, |
| 3127 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 3128 | unsigned *bits |
| 3129 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3130 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3131 | { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3132 | unsigned rice_parameter, partition_bits; |
| 3133 | #ifndef NO_RICE_SEARCH |
| 3134 | unsigned best_partition_bits; |
| 3135 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 3136 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3137 | 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] | 3138 | unsigned *parameters; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3139 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 3140 | 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] | 3141 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 3142 | FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order)); |
| 3143 | parameters = partitioned_rice_contents->parameters; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 3144 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3145 | if(partition_order == 0) { |
| 3146 | unsigned i; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 3147 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3148 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 3149 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3150 | if(suggested_rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 3151 | min_rice_parameter = 0; |
| 3152 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3153 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 3154 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3155 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 3156 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3157 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 3158 | #endif |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 3159 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3160 | } |
| 3161 | } |
| 3162 | else |
| 3163 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
| 3164 | |
| 3165 | best_partition_bits = 0xffffffff; |
| 3166 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 3167 | #endif |
| 3168 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3169 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 3170 | partition_bits = (1+rice_parameter) * residual_samples; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3171 | #else |
| 3172 | partition_bits = 0; |
| 3173 | #endif |
| 3174 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 3175 | for(i = 0; i < residual_samples; i++) { |
| 3176 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3177 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3178 | #else |
| 3179 | partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */ |
| 3180 | #endif |
| 3181 | } |
| 3182 | #ifndef NO_RICE_SEARCH |
| 3183 | if(partition_bits < best_partition_bits) { |
| 3184 | best_rice_parameter = rice_parameter; |
| 3185 | best_partition_bits = partition_bits; |
| 3186 | } |
| 3187 | } |
| 3188 | #endif |
| 3189 | parameters[0] = best_rice_parameter; |
| 3190 | bits_ += best_partition_bits; |
| 3191 | } |
| 3192 | else { |
| 3193 | unsigned partition, residual_sample, save_residual_sample, partition_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3194 | unsigned partition_samples; |
| 3195 | FLAC__uint64 mean, k; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3196 | const unsigned partitions = 1u << partition_order; |
| 3197 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 3198 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
| 3199 | if(partition == 0) { |
| 3200 | if(partition_samples <= predictor_order) |
| 3201 | return false; |
| 3202 | else |
| 3203 | partition_samples -= predictor_order; |
| 3204 | } |
| 3205 | mean = 0; |
| 3206 | save_residual_sample = residual_sample; |
| 3207 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3208 | mean += abs_residual[residual_sample]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3209 | residual_sample = save_residual_sample; |
Josh Coalson | f81b6df | 2005-02-04 01:34:35 +0000 | [diff] [blame] | 3210 | /* we are basically calculating the size in bits of the |
| 3211 | * average residual magnitude in the partition: |
| 3212 | * rice_parameter = floor(log2(mean/partition_samples)) |
| 3213 | * 'mean' is not a good name for the variable, it is |
| 3214 | * actually the sum of magnitudes of all residual values |
| 3215 | * in the partition, so the actual mean is |
| 3216 | * mean/partition_samples |
| 3217 | */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3218 | 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] | 3219 | ; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3220 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 3221 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3222 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 3223 | #endif |
| 3224 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 3225 | } |
| 3226 | |
| 3227 | #ifndef NO_RICE_SEARCH |
| 3228 | if(rice_parameter_search_dist) { |
| 3229 | if(rice_parameter < rice_parameter_search_dist) |
| 3230 | min_rice_parameter = 0; |
| 3231 | else |
| 3232 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 3233 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
| 3234 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 3235 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3236 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 3237 | #endif |
| 3238 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 3239 | } |
| 3240 | } |
| 3241 | else |
| 3242 | min_rice_parameter = max_rice_parameter = rice_parameter; |
| 3243 | |
| 3244 | best_partition_bits = 0xffffffff; |
| 3245 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 3246 | #endif |
| 3247 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3248 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 3249 | partition_bits = (1+rice_parameter) * partition_samples; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3250 | #else |
| 3251 | partition_bits = 0; |
| 3252 | #endif |
| 3253 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 3254 | save_residual_sample = residual_sample; |
| 3255 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) { |
| 3256 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3257 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3258 | #else |
| 3259 | partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */ |
| 3260 | #endif |
| 3261 | } |
| 3262 | #ifndef NO_RICE_SEARCH |
| 3263 | if(rice_parameter != max_rice_parameter) |
| 3264 | residual_sample = save_residual_sample; |
| 3265 | if(partition_bits < best_partition_bits) { |
| 3266 | best_rice_parameter = rice_parameter; |
| 3267 | best_partition_bits = partition_bits; |
| 3268 | } |
| 3269 | } |
| 3270 | #endif |
| 3271 | parameters[partition] = best_rice_parameter; |
| 3272 | bits_ += best_partition_bits; |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | *bits = bits_; |
| 3277 | return true; |
| 3278 | } |
| 3279 | |
| 3280 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3281 | FLAC__bool set_partitioned_rice_with_precompute_( |
| 3282 | const FLAC__int32 residual[], |
| 3283 | const FLAC__uint64 abs_residual_partition_sums[], |
| 3284 | const unsigned raw_bits_per_partition[], |
| 3285 | const unsigned residual_samples, |
| 3286 | const unsigned predictor_order, |
| 3287 | const unsigned suggested_rice_parameter, |
| 3288 | const unsigned rice_parameter_search_dist, |
| 3289 | const unsigned partition_order, |
| 3290 | const FLAC__bool search_for_escapes, |
| 3291 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 3292 | unsigned *bits |
| 3293 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3294 | #else |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3295 | FLAC__bool set_partitioned_rice_with_precompute_( |
| 3296 | const FLAC__uint32 abs_residual[], |
| 3297 | const FLAC__uint64 abs_residual_partition_sums[], |
| 3298 | const unsigned raw_bits_per_partition[], |
| 3299 | const unsigned residual_samples, |
| 3300 | const unsigned predictor_order, |
| 3301 | const unsigned suggested_rice_parameter, |
| 3302 | const unsigned rice_parameter_search_dist, |
| 3303 | const unsigned partition_order, |
| 3304 | const FLAC__bool search_for_escapes, |
| 3305 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
| 3306 | unsigned *bits |
| 3307 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3308 | #endif |
| 3309 | { |
| 3310 | unsigned rice_parameter, partition_bits; |
| 3311 | #ifndef NO_RICE_SEARCH |
| 3312 | unsigned best_partition_bits; |
| 3313 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 3314 | #endif |
| 3315 | unsigned flat_bits; |
| 3316 | 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] | 3317 | unsigned *parameters, *raw_bits; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3318 | |
| 3319 | FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER); |
| 3320 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 3321 | FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order)); |
| 3322 | parameters = partitioned_rice_contents->parameters; |
| 3323 | raw_bits = partitioned_rice_contents->raw_bits; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 3324 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3325 | if(partition_order == 0) { |
| 3326 | unsigned i; |
| 3327 | |
| 3328 | #ifndef NO_RICE_SEARCH |
| 3329 | if(rice_parameter_search_dist) { |
| 3330 | if(suggested_rice_parameter < rice_parameter_search_dist) |
| 3331 | min_rice_parameter = 0; |
| 3332 | else |
| 3333 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 3334 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
| 3335 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 3336 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3337 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 3338 | #endif |
| 3339 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 3340 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 3341 | } |
| 3342 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3343 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 3344 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3345 | best_partition_bits = 0xffffffff; |
| 3346 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 3347 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 3348 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 3349 | const unsigned rice_parameter_estimate = rice_parameter-1; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3350 | partition_bits = (1+rice_parameter) * residual_samples; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3351 | #else |
| 3352 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3353 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 3354 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 3355 | for(i = 0; i < residual_samples; i++) { |
| 3356 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 3357 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 3358 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 3359 | 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] | 3360 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 3361 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3362 | #ifndef NO_RICE_SEARCH |
| 3363 | if(partition_bits < best_partition_bits) { |
| 3364 | best_rice_parameter = rice_parameter; |
| 3365 | best_partition_bits = partition_bits; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 3366 | } |
| 3367 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3368 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3369 | if(search_for_escapes) { |
| 3370 | 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; |
| 3371 | if(flat_bits <= best_partition_bits) { |
| 3372 | raw_bits[0] = raw_bits_per_partition[0]; |
| 3373 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 3374 | best_partition_bits = flat_bits; |
| 3375 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3376 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3377 | parameters[0] = best_rice_parameter; |
| 3378 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3379 | } |
| 3380 | else { |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 3381 | unsigned partition, residual_sample, save_residual_sample, partition_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3382 | unsigned partition_samples; |
| 3383 | FLAC__uint64 mean, k; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 3384 | const unsigned partitions = 1u << partition_order; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 3385 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3386 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3387 | if(partition == 0) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3388 | if(partition_samples <= predictor_order) |
| 3389 | return false; |
| 3390 | else |
| 3391 | partition_samples -= predictor_order; |
| 3392 | } |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 3393 | mean = abs_residual_partition_sums[partition]; |
Josh Coalson | f81b6df | 2005-02-04 01:34:35 +0000 | [diff] [blame] | 3394 | /* we are basically calculating the size in bits of the |
| 3395 | * average residual magnitude in the partition: |
| 3396 | * rice_parameter = floor(log2(mean/partition_samples)) |
| 3397 | * 'mean' is not a good name for the variable, it is |
| 3398 | * actually the sum of magnitudes of all residual values |
| 3399 | * in the partition, so the actual mean is |
| 3400 | * mean/partition_samples |
| 3401 | */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3402 | 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] | 3403 | ; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3404 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 3405 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3406 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 3407 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3408 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3409 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 3410 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3411 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 3412 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3413 | if(rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 3414 | min_rice_parameter = 0; |
| 3415 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3416 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 3417 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3418 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 3419 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3420 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 3421 | #endif |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 3422 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3423 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 3424 | } |
| 3425 | else |
| 3426 | min_rice_parameter = max_rice_parameter = rice_parameter; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 3427 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3428 | best_partition_bits = 0xffffffff; |
| 3429 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 3430 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 3431 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3432 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 3433 | partition_bits = (1+rice_parameter) * partition_samples; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3434 | #else |
| 3435 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3436 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3437 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 3438 | save_residual_sample = residual_sample; |
| 3439 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) { |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 3440 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 3441 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 3442 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 3443 | 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] | 3444 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3445 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3446 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 3447 | if(rice_parameter != max_rice_parameter) |
| 3448 | residual_sample = save_residual_sample; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3449 | if(partition_bits < best_partition_bits) { |
| 3450 | best_rice_parameter = rice_parameter; |
| 3451 | best_partition_bits = partition_bits; |
| 3452 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 3453 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3454 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3455 | if(search_for_escapes) { |
| 3456 | 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; |
| 3457 | if(flat_bits <= best_partition_bits) { |
| 3458 | raw_bits[partition] = raw_bits_per_partition[partition]; |
| 3459 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 3460 | best_partition_bits = flat_bits; |
| 3461 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 3462 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 3463 | parameters[partition] = best_rice_parameter; |
| 3464 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3465 | } |
| 3466 | } |
| 3467 | |
| 3468 | *bits = bits_; |
| 3469 | return true; |
| 3470 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 3471 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 3472 | unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples) |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 3473 | { |
| 3474 | unsigned i, shift; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 3475 | FLAC__int32 x = 0; |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 3476 | |
| 3477 | for(i = 0; i < samples && !(x&1); i++) |
| 3478 | x |= signal[i]; |
| 3479 | |
| 3480 | if(x == 0) { |
| 3481 | shift = 0; |
| 3482 | } |
| 3483 | else { |
| 3484 | for(shift = 0; !(x&1); shift++) |
| 3485 | x >>= 1; |
| 3486 | } |
| 3487 | |
| 3488 | if(shift > 0) { |
| 3489 | for(i = 0; i < samples; i++) |
| 3490 | signal[i] >>= shift; |
| 3491 | } |
| 3492 | |
| 3493 | return shift; |
| 3494 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 3495 | |
| 3496 | void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples) |
| 3497 | { |
| 3498 | unsigned channel; |
| 3499 | |
| 3500 | for(channel = 0; channel < channels; channel++) |
| 3501 | memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples); |
| 3502 | |
| 3503 | fifo->tail += wide_samples; |
| 3504 | |
| 3505 | FLAC__ASSERT(fifo->tail <= fifo->size); |
| 3506 | } |
| 3507 | |
| 3508 | void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples) |
| 3509 | { |
| 3510 | unsigned channel; |
| 3511 | unsigned sample, wide_sample; |
| 3512 | unsigned tail = fifo->tail; |
| 3513 | |
| 3514 | sample = input_offset * channels; |
| 3515 | for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) { |
| 3516 | for(channel = 0; channel < channels; channel++) |
| 3517 | fifo->data[channel][tail] = input[sample++]; |
| 3518 | tail++; |
| 3519 | } |
| 3520 | fifo->tail = tail; |
| 3521 | |
| 3522 | FLAC__ASSERT(fifo->tail <= fifo->size); |
| 3523 | } |
| 3524 | |
| 3525 | FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) |
| 3526 | { |
| 3527 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 3528 | const unsigned encoded_bytes = encoder->private_->verify.output.bytes; |
| 3529 | (void)decoder; |
| 3530 | |
| 3531 | if(encoder->private_->verify.needs_magic_hack) { |
| 3532 | FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH); |
| 3533 | *bytes = FLAC__STREAM_SYNC_LENGTH; |
| 3534 | memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes); |
| 3535 | encoder->private_->verify.needs_magic_hack = false; |
| 3536 | } |
| 3537 | else { |
| 3538 | if(encoded_bytes == 0) { |
Josh Coalson | fc2b737 | 2002-08-16 05:39:34 +0000 | [diff] [blame] | 3539 | /* |
| 3540 | * If we get here, a FIFO underflow has occurred, |
| 3541 | * which means there is a bug somewhere. |
| 3542 | */ |
| 3543 | FLAC__ASSERT(0); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 3544 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 3545 | } |
| 3546 | else if(encoded_bytes < *bytes) |
| 3547 | *bytes = encoded_bytes; |
| 3548 | memcpy(buffer, encoder->private_->verify.output.data, *bytes); |
| 3549 | encoder->private_->verify.output.data += *bytes; |
| 3550 | encoder->private_->verify.output.bytes -= *bytes; |
| 3551 | } |
| 3552 | |
| 3553 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 3554 | } |
| 3555 | |
| 3556 | FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 3557 | { |
| 3558 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data; |
| 3559 | unsigned channel; |
| 3560 | const unsigned channels = FLAC__stream_decoder_get_channels(decoder); |
| 3561 | const unsigned blocksize = frame->header.blocksize; |
| 3562 | const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize; |
| 3563 | |
| 3564 | for(channel = 0; channel < channels; channel++) { |
| 3565 | if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) { |
| 3566 | unsigned i, sample = 0; |
| 3567 | FLAC__int32 expect = 0, got = 0; |
| 3568 | |
| 3569 | for(i = 0; i < blocksize; i++) { |
| 3570 | if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) { |
| 3571 | sample = i; |
| 3572 | expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i]; |
| 3573 | got = (FLAC__int32)buffer[channel][i]; |
| 3574 | break; |
| 3575 | } |
| 3576 | } |
| 3577 | FLAC__ASSERT(i < blocksize); |
| 3578 | FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 3579 | encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample; |
Josh Coalson | 5f39e9f | 2002-08-21 05:27:01 +0000 | [diff] [blame] | 3580 | encoder->private_->verify.error_stats.frame_number = (unsigned)(frame->header.number.sample_number / blocksize); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 3581 | encoder->private_->verify.error_stats.channel = channel; |
| 3582 | encoder->private_->verify.error_stats.sample = sample; |
| 3583 | encoder->private_->verify.error_stats.expected = expect; |
| 3584 | encoder->private_->verify.error_stats.got = got; |
| 3585 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA; |
| 3586 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 3587 | } |
| 3588 | } |
| 3589 | /* dequeue the frame from the fifo */ |
| 3590 | for(channel = 0; channel < channels; channel++) { |
| 3591 | memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize); |
| 3592 | } |
| 3593 | encoder->private_->verify.input_fifo.tail -= blocksize; |
| 3594 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 3595 | } |
| 3596 | |
| 3597 | void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 3598 | { |
| 3599 | (void)decoder, (void)metadata, (void)client_data; |
| 3600 | } |
| 3601 | |
| 3602 | void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 3603 | { |
| 3604 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 3605 | (void)decoder, (void)status; |
| 3606 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 3607 | } |