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