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