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