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