blob: 8bb0ef30496cc4a63abb9b3da3616d0b44db92a1 [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Erik de Castro Lopob1982fb2013-05-25 17:11:19 +10002 * Copyright (C) 2000-2009 Josh Coalson
Thomas Zandere5498e82016-02-08 10:43:37 +01003 * Copyright (C) 2011-2016 Xiph.Org Foundation
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00004 *
Josh Coalsonafd81072003-01-31 23:34:56 +00005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00008 *
Josh Coalsonafd81072003-01-31 23:34:56 +00009 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000011 *
Josh Coalsonafd81072003-01-31 23:34:56 +000012 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000031 */
32
Erik de Castro Lopo006b8352014-03-23 21:59:46 +110033#ifdef HAVE_CONFIG_H
Josh Coalsonb1ec7962006-05-24 04:41:36 +000034# include <config.h>
35#endif
36
Josh Coalsone6b3bbe2002-10-08 06:03:25 +000037#include <limits.h>
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000038#include <stdio.h>
39#include <stdlib.h> /* for malloc() */
40#include <string.h> /* for memcpy() */
Josh Coalson6b21f662006-09-13 01:42:27 +000041#include <sys/types.h> /* for off_t */
Erik de Castro Lopo6c03ab22016-01-24 08:08:50 +110042#ifdef _WIN32
43#include <windows.h> /* for GetFileType() */
44#include <io.h> /* for _get_osfhandle() */
45#endif
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100046#include "share/compat.h"
Josh Coalson1b689822001-05-31 20:11:02 +000047#include "FLAC/assert.h"
Josh Coalsond86e03b2002-08-03 21:56:15 +000048#include "FLAC/stream_decoder.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000049#include "protected/stream_encoder.h"
Josh Coalson423f8042007-01-28 17:40:26 +000050#include "private/bitwriter.h"
Josh Coalsoneef56702001-03-30 00:45:22 +000051#include "private/bitmath.h"
Josh Coalson215af572001-03-27 01:15:58 +000052#include "private/crc.h"
Josh Coalsoncf30f502001-05-23 20:57:44 +000053#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000054#include "private/fixed.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000055#include "private/format.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000056#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000057#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000058#include "private/memory.h"
Cristian Rodríguezf0296252012-04-05 19:39:37 -030059#include "private/macros.h"
Josh Coalsonf1ac7d92006-11-16 07:20:09 +000060#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +000061#include "private/ogg_helper.h"
Josh Coalsonc986d132006-11-15 08:53:32 +000062#include "private/ogg_mapping.h"
Josh Coalson8da98c82006-10-15 04:24:05 +000063#endif
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +100064#include "private/stream_encoder.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000065#include "private/stream_encoder_framing.h"
Josh Coalsonbf0f52c2006-04-25 06:38:43 +000066#include "private/window.h"
Erik de Castro Lopodd1fa802013-03-10 20:39:03 +110067#include "share/alloc.h"
Erik de Castro Lopodd1fa802013-03-10 20:39:03 +110068#include "share/private.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000069
Josh Coalson14b184c2007-03-12 05:08:21 +000070
Josh Coalsonce1d07c2007-01-23 05:00:46 +000071/* Exact Rice codeword length calculation is off by default. The simple
72 * (and fast) estimation (of how many bits a residual value will be
73 * encoded with) in this encoder is very good, almost always yielding
74 * compression within 0.1% of exact calculation.
75 */
76#undef EXACT_RICE_BITS_CALCULATION
77/* Rice parameter searching is off by default. The simple (and fast)
78 * parameter estimation in this encoder is very good, almost always
79 * yielding compression within 0.1% of the optimal parameters.
80 */
Erik de Castro Lopo59afe362012-02-04 16:13:37 +110081#undef ENABLE_RICE_PARAMETER_SEARCH
Josh Coalsonce1d07c2007-01-23 05:00:46 +000082
Josh Coalsondf4e40f2007-02-04 05:49:44 +000083
Josh Coalsond86e03b2002-08-03 21:56:15 +000084typedef struct {
85 FLAC__int32 *data[FLAC__MAX_CHANNELS];
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +110086 uint32_t size; /* of each data[] in samples */
87 uint32_t tail;
Josh Coalsond86e03b2002-08-03 21:56:15 +000088} verify_input_fifo;
89
90typedef struct {
91 const FLAC__byte *data;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +110092 uint32_t capacity;
93 uint32_t bytes;
Josh Coalsond86e03b2002-08-03 21:56:15 +000094} verify_output;
95
96typedef enum {
97 ENCODER_IN_MAGIC = 0,
98 ENCODER_IN_METADATA = 1,
99 ENCODER_IN_AUDIO = 2
100} EncoderStateHint;
101
Erik de Castro Lopo8f25d682017-01-06 20:01:18 +1100102static const struct CompressionLevels {
Josh Coalson425609c2006-11-03 16:08:52 +0000103 FLAC__bool do_mid_side_stereo;
104 FLAC__bool loose_mid_side_stereo;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100105 uint32_t max_lpc_order;
106 uint32_t qlp_coeff_precision;
Josh Coalson425609c2006-11-03 16:08:52 +0000107 FLAC__bool do_qlp_coeff_prec_search;
108 FLAC__bool do_escape_coding;
109 FLAC__bool do_exhaustive_model_search;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100110 uint32_t min_residual_partition_order;
111 uint32_t max_residual_partition_order;
112 uint32_t rice_parameter_search_dist;
Erik de Castro Lopoe0fbe712014-10-15 06:47:39 +1100113 const char *apodization;
Josh Coalson425609c2006-11-03 16:08:52 +0000114} compression_levels_[] = {
Erik de Castro Lopoe0fbe712014-10-15 06:47:39 +1100115 { false, false, 0, 0, false, false, false, 0, 3, 0, "tukey(5e-1)" },
116 { true , true , 0, 0, false, false, false, 0, 3, 0, "tukey(5e-1)" },
117 { true , false, 0, 0, false, false, false, 0, 3, 0, "tukey(5e-1)" },
118 { false, false, 6, 0, false, false, false, 0, 4, 0, "tukey(5e-1)" },
119 { true , true , 8, 0, false, false, false, 0, 4, 0, "tukey(5e-1)" },
120 { true , false, 8, 0, false, false, false, 0, 5, 0, "tukey(5e-1)" },
Martijn van Beurden02891da2014-10-20 16:33:25 +0200121 { true , false, 8, 0, false, false, false, 0, 6, 0, "tukey(5e-1);partial_tukey(2)" },
122 { true , false, 12, 0, false, false, false, 0, 6, 0, "tukey(5e-1);partial_tukey(2)" },
123 { true , false, 12, 0, false, false, false, 0, 6, 0, "tukey(5e-1);partial_tukey(2);punchout_tukey(3)" }
Erik de Castro Lopoe0fbe712014-10-15 06:47:39 +1100124 /* here we use locale-independent 5e-1 instead of 0.5 or 0,5 */
Josh Coalson425609c2006-11-03 16:08:52 +0000125};
126
127
Josh Coalson0a15c142001-06-13 17:59:57 +0000128/***********************************************************************
129 *
130 * Private class method prototypes
131 *
132 ***********************************************************************/
133
Josh Coalsonf1eff452002-07-31 07:05:33 +0000134static void set_defaults_(FLAC__StreamEncoder *encoder);
135static void free_(FLAC__StreamEncoder *encoder);
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100136static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, uint32_t new_blocksize);
137static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, uint32_t samples, FLAC__bool is_last_block);
138static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, FLAC__bool is_last_block);
Josh Coalson6b21f662006-09-13 01:42:27 +0000139static void update_metadata_(const FLAC__StreamEncoder *encoder);
Josh Coalson15b8eb82006-10-15 05:15:55 +0000140#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +0000141static void update_ogg_metadata_(FLAC__StreamEncoder *encoder);
Josh Coalson15b8eb82006-10-15 05:15:55 +0000142#endif
Josh Coalson49f2f162006-11-09 16:54:52 +0000143static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block);
Josh Coalson85aaed82006-11-09 01:19:13 +0000144static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000145
146static FLAC__bool process_subframe_(
147 FLAC__StreamEncoder *encoder,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100148 uint32_t min_partition_order,
149 uint32_t max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000150 const FLAC__FrameHeader *frame_header,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100151 uint32_t subframe_bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000152 const FLAC__int32 integer_signal[],
Josh Coalson6fe72f72002-08-20 04:01:59 +0000153 FLAC__Subframe *subframe[2],
154 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
155 FLAC__int32 *residual[2],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100156 uint32_t *best_subframe,
157 uint32_t *best_bits
Josh Coalson6fe72f72002-08-20 04:01:59 +0000158);
159
160static FLAC__bool add_subframe_(
161 FLAC__StreamEncoder *encoder,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100162 uint32_t blocksize,
163 uint32_t subframe_bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000164 const FLAC__Subframe *subframe,
Josh Coalson423f8042007-01-28 17:40:26 +0000165 FLAC__BitWriter *frame
Josh Coalson6fe72f72002-08-20 04:01:59 +0000166);
167
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100168static uint32_t evaluate_constant_subframe_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000169 FLAC__StreamEncoder *encoder,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000170 const FLAC__int32 signal,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100171 uint32_t blocksize,
172 uint32_t subframe_bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000173 FLAC__Subframe *subframe
174);
175
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100176static uint32_t evaluate_fixed_subframe_(
Josh Coalson6fe72f72002-08-20 04:01:59 +0000177 FLAC__StreamEncoder *encoder,
178 const FLAC__int32 signal[],
179 FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +0000180 FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100181 uint32_t raw_bits_per_partition[],
182 uint32_t blocksize,
183 uint32_t subframe_bps,
184 uint32_t order,
185 uint32_t rice_parameter,
186 uint32_t rice_parameter_limit,
187 uint32_t min_partition_order,
188 uint32_t max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000189 FLAC__bool do_escape_coding,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100190 uint32_t rice_parameter_search_dist,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000191 FLAC__Subframe *subframe,
192 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
193);
194
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000195#ifndef FLAC__INTEGER_ONLY_LIBRARY
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100196static uint32_t evaluate_lpc_subframe_(
Josh Coalson6fe72f72002-08-20 04:01:59 +0000197 FLAC__StreamEncoder *encoder,
198 const FLAC__int32 signal[],
199 FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +0000200 FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100201 uint32_t raw_bits_per_partition[],
Josh Coalson6fe72f72002-08-20 04:01:59 +0000202 const FLAC__real lp_coeff[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100203 uint32_t blocksize,
204 uint32_t subframe_bps,
205 uint32_t order,
206 uint32_t qlp_coeff_precision,
207 uint32_t rice_parameter,
208 uint32_t rice_parameter_limit,
209 uint32_t min_partition_order,
210 uint32_t max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000211 FLAC__bool do_escape_coding,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100212 uint32_t rice_parameter_search_dist,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000213 FLAC__Subframe *subframe,
214 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
215);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000216#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +0000217
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100218static uint32_t evaluate_verbatim_subframe_(
Erik de Castro Lopo59afe362012-02-04 16:13:37 +1100219 FLAC__StreamEncoder *encoder,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000220 const FLAC__int32 signal[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100221 uint32_t blocksize,
222 uint32_t subframe_bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000223 FLAC__Subframe *subframe
224);
225
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100226static uint32_t find_best_partition_order_(
Josh Coalson6fe72f72002-08-20 04:01:59 +0000227 struct FLAC__StreamEncoderPrivate *private_,
228 const FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +0000229 FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100230 uint32_t raw_bits_per_partition[],
231 uint32_t residual_samples,
232 uint32_t predictor_order,
233 uint32_t rice_parameter,
234 uint32_t rice_parameter_limit,
235 uint32_t min_partition_order,
236 uint32_t max_partition_order,
237 uint32_t bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +0000238 FLAC__bool do_escape_coding,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100239 uint32_t rice_parameter_search_dist,
Josh Coalson05f710f2007-07-31 00:51:26 +0000240 FLAC__EntropyCodingMethod *best_ecm
Josh Coalson6fe72f72002-08-20 04:01:59 +0000241);
242
243static void precompute_partition_info_sums_(
Josh Coalson1109e7f2007-01-24 04:26:15 +0000244 const FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +0000245 FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100246 uint32_t residual_samples,
247 uint32_t predictor_order,
248 uint32_t min_partition_order,
249 uint32_t max_partition_order,
250 uint32_t bps
Josh Coalson6fe72f72002-08-20 04:01:59 +0000251);
252
253static void precompute_partition_info_escapes_(
254 const FLAC__int32 residual[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100255 uint32_t raw_bits_per_partition[],
256 uint32_t residual_samples,
257 uint32_t predictor_order,
258 uint32_t min_partition_order,
259 uint32_t max_partition_order
Josh Coalson6fe72f72002-08-20 04:01:59 +0000260);
261
Josh Coalson6fe72f72002-08-20 04:01:59 +0000262static FLAC__bool set_partitioned_rice_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000263#ifdef EXACT_RICE_BITS_CALCULATION
Josh Coalson6fe72f72002-08-20 04:01:59 +0000264 const FLAC__int32 residual[],
Josh Coalson0a15c142001-06-13 17:59:57 +0000265#endif
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000266 const FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100267 const uint32_t raw_bits_per_partition[],
268 const uint32_t residual_samples,
269 const uint32_t predictor_order,
270 const uint32_t suggested_rice_parameter,
271 const uint32_t rice_parameter_limit,
272 const uint32_t rice_parameter_search_dist,
273 const uint32_t partition_order,
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000274 const FLAC__bool search_for_escapes,
275 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100276 uint32_t *bits
Josh Coalsonce1d07c2007-01-23 05:00:46 +0000277);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000278
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100279static uint32_t get_wasted_bits_(FLAC__int32 signal[], uint32_t samples);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000280
Josh Coalsond86e03b2002-08-03 21:56:15 +0000281/* verify-related routines: */
Josh Coalson6fe72f72002-08-20 04:01:59 +0000282static void append_to_verify_fifo_(
283 verify_input_fifo *fifo,
284 const FLAC__int32 * const input[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100285 uint32_t input_offset,
286 uint32_t channels,
287 uint32_t wide_samples
Josh Coalson6fe72f72002-08-20 04:01:59 +0000288);
289
290static void append_to_verify_fifo_interleaved_(
291 verify_input_fifo *fifo,
292 const FLAC__int32 input[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100293 uint32_t input_offset,
294 uint32_t channels,
295 uint32_t wide_samples
Josh Coalson6fe72f72002-08-20 04:01:59 +0000296);
297
Josh Coalson8065a2d2006-10-15 08:32:56 +0000298static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
Josh Coalson6b21f662006-09-13 01:42:27 +0000299static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
300static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
301static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000302
Josh Coalson8065a2d2006-10-15 08:32:56 +0000303static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
Josh Coalson6b21f662006-09-13 01:42:27 +0000304static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
305static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100306static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, uint32_t current_frame, void *client_data);
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000307static FILE *get_binary_stdout_(void);
Josh Coalson6fe72f72002-08-20 04:01:59 +0000308
Josh Coalson0a15c142001-06-13 17:59:57 +0000309
310/***********************************************************************
311 *
312 * Private class data
313 *
314 ***********************************************************************/
315
316typedef struct FLAC__StreamEncoderPrivate {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100317 uint32_t input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +0000318 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
319 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000320#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson4a040f62007-04-04 00:59:28 +0000321 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) the floating-point version of the input signal */
322 FLAC__real *real_signal_mid_side[2]; /* (@@@ currently unused) the floating-point version of the mid-side input signal (stereo only) */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000323 FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
Josh Coalson4a040f62007-04-04 00:59:28 +0000324 FLAC__real *windowed_signal; /* the integer_signal[] * current window[] */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000325#endif
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100326 uint32_t subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
327 uint32_t subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
Josh Coalson77e3f312001-06-23 03:03:24 +0000328 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
329 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000330 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
331 FLAC__Subframe subframe_workspace_mid_side[2][2];
332 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
333 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalsona37ba462002-08-19 21:36:39 +0000334 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
335 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
336 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
337 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100338 uint32_t best_subframe[FLAC__MAX_CHANNELS]; /* index (0 or 1) into 2nd dimension of the above workspaces */
339 uint32_t best_subframe_mid_side[2];
340 uint32_t best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
341 uint32_t best_subframe_bits_mid_side[2];
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000342 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100343 uint32_t *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
Josh Coalson423f8042007-01-28 17:40:26 +0000344 FLAC__BitWriter *frame; /* the current frame being worked on */
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100345 uint32_t loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
346 uint32_t loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000347 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalson6b21f662006-09-13 01:42:27 +0000348 FLAC__StreamMetadata streaminfo; /* scratchpad for STREAMINFO as it is built */
349 FLAC__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100350 uint32_t current_sample_number;
351 uint32_t current_frame_number;
Josh Coalson099a2892007-03-14 07:58:19 +0000352 FLAC__MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000353 FLAC__CPUInfo cpuinfo;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100354 void (*local_precompute_partition_info_sums)(const FLAC__int32 residual[], FLAC__uint64 abs_residual_partition_sums[], uint32_t residual_samples, uint32_t predictor_order, uint32_t min_partition_order, uint32_t max_partition_order, uint32_t bps);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000355#ifndef FLAC__INTEGER_ONLY_LIBRARY
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100356 uint32_t (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
357 uint32_t (*local_fixed_compute_best_predictor_wide)(const FLAC__int32 data[], uint32_t data_len, float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000358#else
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100359 uint32_t (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
360 uint32_t (*local_fixed_compute_best_predictor_wide)(const FLAC__int32 data[], uint32_t data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000361#endif
362#ifndef FLAC__INTEGER_ONLY_LIBRARY
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100363 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], uint32_t data_len, uint32_t lag, FLAC__real autoc[]);
364 void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
365 void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
366 void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000367#endif
Josh Coalsone6b3bbe2002-10-08 06:03:25 +0000368 FLAC__bool disable_constant_subframes;
369 FLAC__bool disable_fixed_subframes;
370 FLAC__bool disable_verbatim_subframes;
Josh Coalson8da98c82006-10-15 04:24:05 +0000371 FLAC__bool is_ogg;
Josh Coalson8da98c82006-10-15 04:24:05 +0000372 FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */
Josh Coalson6b21f662006-09-13 01:42:27 +0000373 FLAC__StreamEncoderSeekCallback seek_callback;
374 FLAC__StreamEncoderTellCallback tell_callback;
Josh Coalson681c2932002-08-01 08:19:37 +0000375 FLAC__StreamEncoderWriteCallback write_callback;
376 FLAC__StreamEncoderMetadataCallback metadata_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +0000377 FLAC__StreamEncoderProgressCallback progress_callback;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000378 void *client_data;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100379 uint32_t first_seekpoint_to_check;
Josh Coalson6b21f662006-09-13 01:42:27 +0000380 FILE *file; /* only used when encoding to a file */
381 FLAC__uint64 bytes_written;
382 FLAC__uint64 samples_written;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100383 uint32_t frames_written;
384 uint32_t total_frames_estimate;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000385 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000386 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
387 FLAC__int32 *integer_signal_mid_side_unaligned[2];
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000388#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson4a040f62007-04-04 00:59:28 +0000389 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) */
390 FLAC__real *real_signal_mid_side_unaligned[2]; /* (@@@ currently unused) */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000391 FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
392 FLAC__real *windowed_signal_unaligned;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000393#endif
Josh Coalson77e3f312001-06-23 03:03:24 +0000394 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
395 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000396 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100397 uint32_t *raw_bits_per_partition_unaligned;
Josh Coalson8084b052001-11-01 00:27:29 +0000398 /*
399 * These fields have been moved here from private function local
400 * declarations merely to save stack space during encoding.
401 */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000402#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +0000403 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000404#endif
Josh Coalsona37ba462002-08-19 21:36:39 +0000405 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
Josh Coalsond86e03b2002-08-03 21:56:15 +0000406 /*
407 * The data for the verify section
408 */
409 struct {
410 FLAC__StreamDecoder *decoder;
411 EncoderStateHint state_hint;
412 FLAC__bool needs_magic_hack;
413 verify_input_fifo input_fifo;
414 verify_output output;
415 struct {
416 FLAC__uint64 absolute_sample;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100417 uint32_t frame_number;
418 uint32_t channel;
419 uint32_t sample;
Josh Coalsond86e03b2002-08-03 21:56:15 +0000420 FLAC__int32 expected;
421 FLAC__int32 got;
422 } error_stats;
423 } verify;
Josh Coalson3262b0d2002-08-14 20:58:42 +0000424 FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
Josh Coalson0a15c142001-06-13 17:59:57 +0000425} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000426
Josh Coalson0a15c142001-06-13 17:59:57 +0000427/***********************************************************************
428 *
429 * Public static class data
430 *
431 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000432
Josh Coalson6afed9f2002-10-16 22:29:47 +0000433FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
Josh Coalson0a15c142001-06-13 17:59:57 +0000434 "FLAC__STREAM_ENCODER_OK",
Josh Coalson6b21f662006-09-13 01:42:27 +0000435 "FLAC__STREAM_ENCODER_UNINITIALIZED",
Josh Coalson8da98c82006-10-15 04:24:05 +0000436 "FLAC__STREAM_ENCODER_OGG_ERROR",
Josh Coalsond86e03b2002-08-03 21:56:15 +0000437 "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
438 "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
Josh Coalson6b21f662006-09-13 01:42:27 +0000439 "FLAC__STREAM_ENCODER_CLIENT_ERROR",
440 "FLAC__STREAM_ENCODER_IO_ERROR",
Josh Coalson0a15c142001-06-13 17:59:57 +0000441 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson6b21f662006-09-13 01:42:27 +0000442 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
443};
444
445FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
446 "FLAC__STREAM_ENCODER_INIT_STATUS_OK",
447 "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
Josh Coalson8da98c82006-10-15 04:24:05 +0000448 "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
Josh Coalson6b21f662006-09-13 01:42:27 +0000449 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
450 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
451 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
452 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
453 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
454 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
455 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
Josh Coalson6b21f662006-09-13 01:42:27 +0000456 "FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
457 "FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
458 "FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
459 "FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000460};
461
Erik de Castro Lopo70be52b2012-11-28 21:49:47 +1100462FLAC_API const char * const FLAC__StreamEncoderReadStatusString[] = {
Josh Coalson8da98c82006-10-15 04:24:05 +0000463 "FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
464 "FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
465 "FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
466 "FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
467};
468
Josh Coalson6afed9f2002-10-16 22:29:47 +0000469FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
Josh Coalson5c491a12002-08-01 06:39:40 +0000470 "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
471 "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000472};
473
Josh Coalson6b21f662006-09-13 01:42:27 +0000474FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[] = {
475 "FLAC__STREAM_ENCODER_SEEK_STATUS_OK",
476 "FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR",
477 "FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED"
478};
479
480FLAC_API const char * const FLAC__StreamEncoderTellStatusString[] = {
481 "FLAC__STREAM_ENCODER_TELL_STATUS_OK",
482 "FLAC__STREAM_ENCODER_TELL_STATUS_ERROR",
483 "FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED"
484};
485
Josh Coalson49f2f162006-11-09 16:54:52 +0000486/* Number of samples that will be overread to watch for end of stream. By
487 * 'overread', we mean that the FLAC__stream_encoder_process*() calls will
488 * always try to read blocksize+1 samples before encoding a block, so that
489 * even if the stream has a total sample count that is an integral multiple
490 * of the blocksize, we will still notice when we are encoding the last
491 * block. This is needed, for example, to correctly set the end-of-stream
492 * marker in Ogg FLAC.
493 *
494 * WATCHOUT: some parts of the code assert that OVERREAD_ == 1 and there's
495 * not really any reason to change it.
496 */
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100497static const uint32_t OVERREAD_ = 1;
Josh Coalson49f2f162006-11-09 16:54:52 +0000498
Josh Coalson0a15c142001-06-13 17:59:57 +0000499/***********************************************************************
500 *
501 * Class constructor/destructor
502 *
Josh Coalsond86e03b2002-08-03 21:56:15 +0000503 */
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000504FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000505{
Josh Coalson0a15c142001-06-13 17:59:57 +0000506 FLAC__StreamEncoder *encoder;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100507 uint32_t i;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000508
Josh Coalson0a15c142001-06-13 17:59:57 +0000509 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000510
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000511 encoder = calloc(1, sizeof(FLAC__StreamEncoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000512 if(encoder == 0) {
513 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000514 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000515
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000516 encoder->protected_ = calloc(1, sizeof(FLAC__StreamEncoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000517 if(encoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000518 free(encoder);
519 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000520 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000521
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000522 encoder->private_ = calloc(1, sizeof(FLAC__StreamEncoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000523 if(encoder->private_ == 0) {
524 free(encoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000525 free(encoder);
526 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000527 }
Josh Coalsond86e03b2002-08-03 21:56:15 +0000528
Josh Coalson423f8042007-01-28 17:40:26 +0000529 encoder->private_->frame = FLAC__bitwriter_new();
Josh Coalsonaec256b2002-03-12 16:19:54 +0000530 if(encoder->private_->frame == 0) {
531 free(encoder->private_);
532 free(encoder->protected_);
533 free(encoder);
534 return 0;
535 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000536
Josh Coalson6b21f662006-09-13 01:42:27 +0000537 encoder->private_->file = 0;
538
Josh Coalsonf1eff452002-07-31 07:05:33 +0000539 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +0000540
Josh Coalson3262b0d2002-08-14 20:58:42 +0000541 encoder->private_->is_being_deleted = false;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000542
543 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
544 encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
545 encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
546 }
547 for(i = 0; i < 2; i++) {
548 encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
549 encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
550 }
551 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000552 encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
553 encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000554 }
555 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000556 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
557 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000558 }
559
560 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000561 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
562 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000563 }
564 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000565 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
566 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000567 }
568 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000569 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000570
Josh Coalsonfa697a92001-08-16 20:07:29 +0000571 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000572
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000573 return encoder;
574}
575
Josh Coalson6afed9f2002-10-16 22:29:47 +0000576FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000577{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100578 uint32_t i;
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000579
Erik de Castro Lopofeab11e2012-06-22 08:39:20 +1000580 if (encoder == NULL)
581 return ;
582
Josh Coalsonf1eff452002-07-31 07:05:33 +0000583 FLAC__ASSERT(0 != encoder->protected_);
584 FLAC__ASSERT(0 != encoder->private_);
585 FLAC__ASSERT(0 != encoder->private_->frame);
Josh Coalson0a15c142001-06-13 17:59:57 +0000586
Josh Coalson3262b0d2002-08-14 20:58:42 +0000587 encoder->private_->is_being_deleted = true;
588
Josh Coalsona5862262006-11-09 06:58:26 +0000589 (void)FLAC__stream_encoder_finish(encoder);
Josh Coalson3262b0d2002-08-14 20:58:42 +0000590
Josh Coalson4fa90592002-12-04 07:01:37 +0000591 if(0 != encoder->private_->verify.decoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +0000592 FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000593
594 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000595 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
596 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000597 }
598 for(i = 0; i < 2; i++) {
Josh Coalsona37ba462002-08-19 21:36:39 +0000599 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
600 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000601 }
602 for(i = 0; i < 2; i++)
Josh Coalsona37ba462002-08-19 21:36:39 +0000603 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000604
Josh Coalson423f8042007-01-28 17:40:26 +0000605 FLAC__bitwriter_delete(encoder->private_->frame);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000606 free(encoder->private_);
607 free(encoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000608 free(encoder);
609}
610
Josh Coalson0a15c142001-06-13 17:59:57 +0000611/***********************************************************************
612 *
613 * Public class methods
614 *
615 ***********************************************************************/
616
Josh Coalson8da98c82006-10-15 04:24:05 +0000617static FLAC__StreamEncoderInitStatus init_stream_internal_(
618 FLAC__StreamEncoder *encoder,
619 FLAC__StreamEncoderReadCallback read_callback,
620 FLAC__StreamEncoderWriteCallback write_callback,
621 FLAC__StreamEncoderSeekCallback seek_callback,
622 FLAC__StreamEncoderTellCallback tell_callback,
623 FLAC__StreamEncoderMetadataCallback metadata_callback,
624 void *client_data,
625 FLAC__bool is_ogg
626)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000627{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100628 uint32_t i;
Josh Coalson3957c472006-09-24 16:25:42 +0000629 FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000630
Josh Coalsonf1eff452002-07-31 07:05:33 +0000631 FLAC__ASSERT(0 != encoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000632
Josh Coalsonfa697a92001-08-16 20:07:29 +0000633 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson6b21f662006-09-13 01:42:27 +0000634 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000635
Erik de Castro Lopo1be4f952016-03-20 19:28:41 +1100636 if(FLAC__HAS_OGG == 0 && is_ogg)
Josh Coalson8da98c82006-10-15 04:24:05 +0000637 return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
Josh Coalson8da98c82006-10-15 04:24:05 +0000638
Josh Coalson6b21f662006-09-13 01:42:27 +0000639 if(0 == write_callback || (seek_callback && 0 == tell_callback))
640 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000641
Josh Coalsonfa697a92001-08-16 20:07:29 +0000642 if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
Josh Coalson6b21f662006-09-13 01:42:27 +0000643 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000644
Josh Coalson425609c2006-11-03 16:08:52 +0000645 if(encoder->protected_->channels != 2) {
646 encoder->protected_->do_mid_side_stereo = false;
647 encoder->protected_->loose_mid_side_stereo = false;
648 }
649 else if(!encoder->protected_->do_mid_side_stereo)
650 encoder->protected_->loose_mid_side_stereo = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000651
Josh Coalsonfa697a92001-08-16 20:07:29 +0000652 if(encoder->protected_->bits_per_sample >= 32)
luz.paz01eb1972018-05-22 10:17:53 -0400653 encoder->protected_->do_mid_side_stereo = false; /* since we currently do 32-bit math, the side channel would have 33 bps and overflow */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000654
Josh Coalson76c68bc2002-05-17 06:22:02 +0000655 if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
Josh Coalson6b21f662006-09-13 01:42:27 +0000656 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000657
Josh Coalson0833f342002-07-15 05:31:55 +0000658 if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
Josh Coalson6b21f662006-09-13 01:42:27 +0000659 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000660
Josh Coalson425609c2006-11-03 16:08:52 +0000661 if(encoder->protected_->blocksize == 0) {
662 if(encoder->protected_->max_lpc_order == 0)
663 encoder->protected_->blocksize = 1152;
664 else
Josh Coalsondc2b03b2007-02-06 05:02:46 +0000665 encoder->protected_->blocksize = 4096;
Josh Coalson425609c2006-11-03 16:08:52 +0000666 }
667
Josh Coalsonfa697a92001-08-16 20:07:29 +0000668 if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
Josh Coalson6b21f662006-09-13 01:42:27 +0000669 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
Josh Coalson0a15c142001-06-13 17:59:57 +0000670
Josh Coalson20ac2c12002-08-30 05:47:14 +0000671 if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000672 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000673
Josh Coalsonfa697a92001-08-16 20:07:29 +0000674 if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
Josh Coalson6b21f662006-09-13 01:42:27 +0000675 return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
Josh Coalson0a15c142001-06-13 17:59:57 +0000676
Josh Coalsonfa697a92001-08-16 20:07:29 +0000677 if(encoder->protected_->qlp_coeff_precision == 0) {
678 if(encoder->protected_->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000679 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
680 /* @@@ until then we'll make a guess */
Cristian Rodríguezf0296252012-04-05 19:39:37 -0300681 encoder->protected_->qlp_coeff_precision = flac_max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000682 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000683 else if(encoder->protected_->bits_per_sample == 16) {
684 if(encoder->protected_->blocksize <= 192)
685 encoder->protected_->qlp_coeff_precision = 7;
686 else if(encoder->protected_->blocksize <= 384)
687 encoder->protected_->qlp_coeff_precision = 8;
688 else if(encoder->protected_->blocksize <= 576)
689 encoder->protected_->qlp_coeff_precision = 9;
690 else if(encoder->protected_->blocksize <= 1152)
691 encoder->protected_->qlp_coeff_precision = 10;
692 else if(encoder->protected_->blocksize <= 2304)
693 encoder->protected_->qlp_coeff_precision = 11;
694 else if(encoder->protected_->blocksize <= 4608)
695 encoder->protected_->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000696 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000697 encoder->protected_->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000698 }
699 else {
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000700 if(encoder->protected_->blocksize <= 384)
701 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
702 else if(encoder->protected_->blocksize <= 1152)
703 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
704 else
705 encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000706 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000707 FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000708 }
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000709 else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
Josh Coalson6b21f662006-09-13 01:42:27 +0000710 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000711
Josh Coalsonfa697a92001-08-16 20:07:29 +0000712 if(encoder->protected_->streamable_subset) {
Josh Coalsond8a6f4a2008-05-27 05:20:15 +0000713 if(!FLAC__format_blocksize_is_subset(encoder->protected_->blocksize, encoder->protected_->sample_rate))
Josh Coalson6b21f662006-09-13 01:42:27 +0000714 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson2258fa82007-03-30 01:02:40 +0000715 if(!FLAC__format_sample_rate_is_subset(encoder->protected_->sample_rate))
Josh Coalson6b21f662006-09-13 01:42:27 +0000716 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalson20ac2c12002-08-30 05:47:14 +0000717 if(
718 encoder->protected_->bits_per_sample != 8 &&
719 encoder->protected_->bits_per_sample != 12 &&
720 encoder->protected_->bits_per_sample != 16 &&
721 encoder->protected_->bits_per_sample != 20 &&
722 encoder->protected_->bits_per_sample != 24
723 )
Josh Coalson6b21f662006-09-13 01:42:27 +0000724 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsonc1c8d492002-09-26 04:42:10 +0000725 if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
Josh Coalson6b21f662006-09-13 01:42:27 +0000726 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
Josh Coalsond0edb972006-10-07 06:50:08 +0000727 if(
728 encoder->protected_->sample_rate <= 48000 &&
729 (
730 encoder->protected_->blocksize > FLAC__SUBSET_MAX_BLOCK_SIZE_48000HZ ||
731 encoder->protected_->max_lpc_order > FLAC__SUBSET_MAX_LPC_ORDER_48000HZ
732 )
733 ) {
734 return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
735 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000736 }
737
Josh Coalsonfa697a92001-08-16 20:07:29 +0000738 if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
739 encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
740 if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
741 encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000742
Josh Coalson8da98c82006-10-15 04:24:05 +0000743#if FLAC__HAS_OGG
744 /* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
745 if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100746 uint32_t i1;
Erik de Castro Lopo96a5ba72013-03-10 22:03:47 +1100747 for(i1 = 1; i1 < encoder->protected_->num_metadata_blocks; i1++) {
748 if(0 != encoder->protected_->metadata[i1] && encoder->protected_->metadata[i1]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
749 FLAC__StreamMetadata *vc = encoder->protected_->metadata[i1];
750 for( ; i1 > 0; i1--)
751 encoder->protected_->metadata[i1] = encoder->protected_->metadata[i1-1];
Josh Coalson8da98c82006-10-15 04:24:05 +0000752 encoder->protected_->metadata[0] = vc;
753 break;
754 }
755 }
756 }
757#endif
758 /* keep track of any SEEKTABLE block */
759 if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100760 uint32_t i2;
Erik de Castro Lopo96a5ba72013-03-10 22:03:47 +1100761 for(i2 = 0; i2 < encoder->protected_->num_metadata_blocks; i2++) {
762 if(0 != encoder->protected_->metadata[i2] && encoder->protected_->metadata[i2]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
763 encoder->private_->seek_table = &encoder->protected_->metadata[i2]->data.seek_table;
Josh Coalson8da98c82006-10-15 04:24:05 +0000764 break; /* take only the first one */
765 }
766 }
767 }
768
Josh Coalson66075c12002-06-01 05:39:38 +0000769 /* validate metadata */
770 if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
Josh Coalson6b21f662006-09-13 01:42:27 +0000771 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000772 metadata_has_seektable = false;
773 metadata_has_vorbis_comment = false;
Josh Coalson3957c472006-09-24 16:25:42 +0000774 metadata_picture_has_type1 = false;
775 metadata_picture_has_type2 = false;
Josh Coalson66075c12002-06-01 05:39:38 +0000776 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
Josh Coalson3957c472006-09-24 16:25:42 +0000777 const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
778 if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
Josh Coalson6b21f662006-09-13 01:42:27 +0000779 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000780 else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000781 if(metadata_has_seektable) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000782 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000783 metadata_has_seektable = true;
Josh Coalson3957c472006-09-24 16:25:42 +0000784 if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
Josh Coalson6b21f662006-09-13 01:42:27 +0000785 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson66075c12002-06-01 05:39:38 +0000786 }
Josh Coalson3957c472006-09-24 16:25:42 +0000787 else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000788 if(metadata_has_vorbis_comment) /* only one is allowed */
Josh Coalson6b21f662006-09-13 01:42:27 +0000789 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +0000790 metadata_has_vorbis_comment = true;
791 }
Josh Coalson3957c472006-09-24 16:25:42 +0000792 else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
793 if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
Josh Coalson6b21f662006-09-13 01:42:27 +0000794 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalsone4869382002-11-15 05:41:48 +0000795 }
Josh Coalson3957c472006-09-24 16:25:42 +0000796 else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
797 if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
Josh Coalsone343ab22006-09-23 19:21:19 +0000798 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
Josh Coalson3957c472006-09-24 16:25:42 +0000799 if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
800 if(metadata_picture_has_type1) /* there should only be 1 per stream */
801 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
802 metadata_picture_has_type1 = true;
803 /* standard icon must be 32x32 pixel PNG */
804 if(
Erik de Castro Lopo59afe362012-02-04 16:13:37 +1100805 m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
Josh Coalson3957c472006-09-24 16:25:42 +0000806 (
807 (strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
808 m->data.picture.width != 32 ||
809 m->data.picture.height != 32
810 )
811 )
812 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
813 }
814 else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
815 if(metadata_picture_has_type2) /* there should only be 1 per stream */
816 return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
817 metadata_picture_has_type2 = true;
818 }
Josh Coalsone343ab22006-09-23 19:21:19 +0000819 }
Josh Coalson66075c12002-06-01 05:39:38 +0000820 }
821
Josh Coalsonfa697a92001-08-16 20:07:29 +0000822 encoder->private_->input_capacity = 0;
823 for(i = 0; i < encoder->protected_->channels; i++) {
824 encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000825#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000826 encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000827#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000828 }
829 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000830 encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000831#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000832 encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000833#endif
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000834 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +0000835#ifndef FLAC__INTEGER_ONLY_LIBRARY
836 for(i = 0; i < encoder->protected_->num_apodizations; i++)
837 encoder->private_->window_unaligned[i] = encoder->private_->window[i] = 0;
838 encoder->private_->windowed_signal_unaligned = encoder->private_->windowed_signal = 0;
839#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000840 for(i = 0; i < encoder->protected_->channels; i++) {
841 encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
842 encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
843 encoder->private_->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000844 }
845 for(i = 0; i < 2; i++) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000846 encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
847 encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
848 encoder->private_->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000849 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000850 encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
851 encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000852#ifndef FLAC__INTEGER_ONLY_LIBRARY
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100853 encoder->private_->loose_mid_side_stereo_frames = (uint32_t)((double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize + 0.5);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000854#else
855 /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
856 /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply&divide by hand */
857 FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
858 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
859 FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
860 FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +1100861 encoder->private_->loose_mid_side_stereo_frames = (uint32_t)FLAC__fixedpoint_trunc((((FLAC__uint64)(encoder->protected_->sample_rate) * (FLAC__uint64)(26214)) << 16) / (encoder->protected_->blocksize<<16) + FLAC__FP_ONE_HALF);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000862#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000863 if(encoder->private_->loose_mid_side_stereo_frames == 0)
864 encoder->private_->loose_mid_side_stereo_frames = 1;
865 encoder->private_->loose_mid_side_stereo_frame_count = 0;
866 encoder->private_->current_sample_number = 0;
867 encoder->private_->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000868
Josh Coalsoncf30f502001-05-23 20:57:44 +0000869 /*
870 * get the CPU info and set the function pointers
871 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000872 FLAC__cpu_info(&encoder->private_->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000873 /* first default to the non-asm routines */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000874#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000875 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000876#endif
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000877 encoder->private_->local_precompute_partition_info_sums = precompute_partition_info_sums_;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000878 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
Erik de Castro Lopo93f61092014-04-11 06:21:09 +1000879 encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000880#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonfa697a92001-08-16 20:07:29 +0000881 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsonc9c0d132002-10-04 05:29:05 +0000882 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000883 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000884#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000885 /* now override with asm where appropriate */
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000886#ifndef FLAC__INTEGER_ONLY_LIBRARY
887# ifndef FLAC__NO_ASM
Anton Blanchardcdb030c2018-07-07 14:21:46 +1000888#if defined(FLAC__CPU_PPC64) && defined(FLAC__USE_VSX)
889#ifdef FLAC__HAS_TARGET_POWER8
890#ifdef FLAC__HAS_TARGET_POWER9
891 if (encoder->private_->cpuinfo.ppc.arch_3_00) {
892 if(encoder->protected_->max_lpc_order < 4)
893 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_power9_vsx_lag_4;
894 else if(encoder->protected_->max_lpc_order < 8)
895 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_power9_vsx_lag_8;
896 else if(encoder->protected_->max_lpc_order < 12)
897 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_power9_vsx_lag_12;
898 else if(encoder->protected_->max_lpc_order < 16)
899 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_power9_vsx_lag_16;
900 else
901 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
902 } else
903#endif
904 if (encoder->private_->cpuinfo.ppc.arch_2_07) {
905 if(encoder->protected_->max_lpc_order < 4)
906 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_power8_vsx_lag_4;
907 else if(encoder->protected_->max_lpc_order < 8)
908 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_power8_vsx_lag_8;
909 else if(encoder->protected_->max_lpc_order < 12)
910 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_power8_vsx_lag_12;
911 else if(encoder->protected_->max_lpc_order < 16)
912 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_power8_vsx_lag_16;
913 else
914 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
915 }
916#endif
917#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +0000918 if(encoder->private_->cpuinfo.use_asm) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000919# ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000920 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000921# ifdef FLAC__HAS_NASM
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +1100922 if (encoder->private_->cpuinfo.x86.sse) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000923 if(encoder->protected_->max_lpc_order < 4)
Erik de Castro Lopoac0b4b42014-12-04 21:03:01 +1100924 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4_old;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000925 else if(encoder->protected_->max_lpc_order < 8)
Erik de Castro Lopoac0b4b42014-12-04 21:03:01 +1100926 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8_old;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000927 else if(encoder->protected_->max_lpc_order < 12)
Erik de Castro Lopoac0b4b42014-12-04 21:03:01 +1100928 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12_old;
Erik de Castro Lopodeb20992013-08-31 13:40:47 +1000929 else if(encoder->protected_->max_lpc_order < 16)
Erik de Castro Lopoac0b4b42014-12-04 21:03:01 +1100930 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_16_old;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000931 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000932 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000933 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000934 else
Josh Coalsonfa697a92001-08-16 20:07:29 +0000935 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Erik de Castro Lopo3d394922014-05-14 21:27:31 +1000936
937 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide_asm_ia32; /* OPT_IA32: was really necessary for GCC < 4.9 */
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +1100938 if (encoder->private_->cpuinfo.x86.mmx) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000939 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
940 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000941 }
942 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000943 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
944 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000945 }
Erik de Castro Lopo3d394922014-05-14 21:27:31 +1000946
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +1100947 if (encoder->private_->cpuinfo.x86.mmx && encoder->private_->cpuinfo.x86.cmov)
Josh Coalson5f2b46d2004-11-09 01:34:01 +0000948 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
949# endif /* FLAC__HAS_NASM */
Erik de Castro Lopo36a0ab12016-06-20 20:29:59 +1000950# if FLAC__HAS_X86INTRIN
Erik de Castro Lopo3d394922014-05-14 21:27:31 +1000951# if defined FLAC__SSE_SUPPORTED
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +1100952 if (encoder->private_->cpuinfo.x86.sse) {
953 if (encoder->private_->cpuinfo.x86.sse42 || !encoder->private_->cpuinfo.x86.intel) { /* use new autocorrelation functions */
Erik de Castro Lopoac0b4b42014-12-04 21:03:01 +1100954 if(encoder->protected_->max_lpc_order < 4)
955 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_4_new;
956 else if(encoder->protected_->max_lpc_order < 8)
957 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_8_new;
958 else if(encoder->protected_->max_lpc_order < 12)
959 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_12_new;
960 else if(encoder->protected_->max_lpc_order < 16)
961 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_16_new;
962 else
963 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
964 }
965 else { /* use old autocorrelation functions */
966 if(encoder->protected_->max_lpc_order < 4)
967 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_4_old;
968 else if(encoder->protected_->max_lpc_order < 8)
969 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_8_old;
970 else if(encoder->protected_->max_lpc_order < 12)
971 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_12_old;
972 else if(encoder->protected_->max_lpc_order < 16)
973 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_16_old;
974 else
975 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
976 }
Erik de Castro Lopod163ef42014-02-01 20:34:51 +1100977 }
978# endif
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +1000979
Erik de Castro Lopod40e9862014-01-30 21:49:51 +1100980# ifdef FLAC__SSE2_SUPPORTED
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +1100981 if (encoder->private_->cpuinfo.x86.sse2) {
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +1000982 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse2;
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +1000983 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_sse2;
984 }
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +1000985# endif
986# ifdef FLAC__SSE4_1_SUPPORTED
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +1100987 if (encoder->private_->cpuinfo.x86.sse41) {
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +1000988 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse41;
Erik de Castro Lopo3d394922014-05-14 21:27:31 +1000989 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_sse41;
990 }
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +1000991# endif
Erik de Castro Lopo11b5d512014-10-04 09:42:54 +1000992# ifdef FLAC__AVX2_SUPPORTED
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +1100993 if (encoder->private_->cpuinfo.x86.avx2) {
Erik de Castro Lopo11b5d512014-10-04 09:42:54 +1000994 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_avx2;
995 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_avx2;
996 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_avx2;
997 }
998# endif
Erik de Castro Lopo3d394922014-05-14 21:27:31 +1000999
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001000# ifdef FLAC__SSE2_SUPPORTED
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +11001001 if (encoder->private_->cpuinfo.x86.sse2) {
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001002 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_intrin_sse2;
Erik de Castro Lopo93f61092014-04-11 06:21:09 +10001003 encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide_intrin_sse2;
1004 }
Erik de Castro Lopod40e9862014-01-30 21:49:51 +11001005# endif
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001006# ifdef FLAC__SSSE3_SUPPORTED
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +11001007 if (encoder->private_->cpuinfo.x86.ssse3) {
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001008 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_intrin_ssse3;
1009 encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide_intrin_ssse3;
1010 }
1011# endif
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +10001012# endif /* FLAC__HAS_X86INTRIN */
Erik de Castro Lopob8d58e32014-06-15 20:29:34 +10001013# elif defined FLAC__CPU_X86_64
Erik de Castro Lopo5e5ee272013-09-15 09:51:28 +10001014 FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_X86_64);
Erik de Castro Lopo36a0ab12016-06-20 20:29:59 +10001015# if FLAC__HAS_X86INTRIN
Erik de Castro Lopod40e9862014-01-30 21:49:51 +11001016# ifdef FLAC__SSE_SUPPORTED
Erik de Castro Lopoac0b4b42014-12-04 21:03:01 +11001017 if(encoder->private_->cpuinfo.x86.sse42 || !encoder->private_->cpuinfo.x86.intel) { /* use new autocorrelation functions */
1018 if(encoder->protected_->max_lpc_order < 4)
1019 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_4_new;
1020 else if(encoder->protected_->max_lpc_order < 8)
1021 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_8_new;
1022 else if(encoder->protected_->max_lpc_order < 12)
1023 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_12_new;
1024 else if(encoder->protected_->max_lpc_order < 16)
1025 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_16_new;
1026 }
1027 else {
1028 if(encoder->protected_->max_lpc_order < 4)
1029 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_4_old;
1030 else if(encoder->protected_->max_lpc_order < 8)
1031 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_8_old;
1032 else if(encoder->protected_->max_lpc_order < 12)
1033 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_12_old;
1034 else if(encoder->protected_->max_lpc_order < 16)
1035 encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_intrin_sse_lag_16_old;
1036 }
Erik de Castro Lopod40e9862014-01-30 21:49:51 +11001037# endif
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001038
Erik de Castro Lopod40e9862014-01-30 21:49:51 +11001039# ifdef FLAC__SSE2_SUPPORTED
Erik de Castro Lopo5e5ee272013-09-15 09:51:28 +10001040 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_sse2;
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001041# endif
1042# ifdef FLAC__SSE4_1_SUPPORTED
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +10001043 if(encoder->private_->cpuinfo.x86.sse41) {
Erik de Castro Lopo02591f62014-07-28 20:43:54 +10001044 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse41;
1045 }
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001046# endif
Erik de Castro Lopo11b5d512014-10-04 09:42:54 +10001047# ifdef FLAC__AVX2_SUPPORTED
1048 if(encoder->private_->cpuinfo.x86.avx2) {
1049 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_avx2;
1050 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_avx2;
1051 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_avx2;
1052 }
1053# endif
Erik de Castro Lopo3d394922014-05-14 21:27:31 +10001054
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001055# ifdef FLAC__SSE2_SUPPORTED
1056 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_intrin_sse2;
1057 encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide_intrin_sse2;
1058# endif
1059# ifdef FLAC__SSSE3_SUPPORTED
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +10001060 if (encoder->private_->cpuinfo.x86.ssse3) {
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001061 encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_intrin_ssse3;
Erik de Castro Lopo93f61092014-04-11 06:21:09 +10001062 encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide_intrin_ssse3;
1063 }
Erik de Castro Lopod40e9862014-01-30 21:49:51 +11001064# endif
Erik de Castro Lopo5e5ee272013-09-15 09:51:28 +10001065# endif /* FLAC__HAS_X86INTRIN */
1066# endif /* FLAC__CPU_... */
Josh Coalson021ad3b2001-07-18 00:25:52 +00001067 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00001068# endif /* !FLAC__NO_ASM */
1069#endif /* !FLAC__INTEGER_ONLY_LIBRARY */
Erik de Castro Lopo36a0ab12016-06-20 20:29:59 +10001070#if !defined FLAC__NO_ASM && FLAC__HAS_X86INTRIN
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +10001071 if(encoder->private_->cpuinfo.use_asm) {
1072# if defined FLAC__CPU_IA32
Erik de Castro Lopoace63cc2014-02-25 18:28:22 +11001073# ifdef FLAC__SSE2_SUPPORTED
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +11001074 if (encoder->private_->cpuinfo.x86.sse2)
Erik de Castro Lopo6cd8b422014-01-07 21:14:55 +11001075 encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_sse2;
Erik de Castro Lopod40e9862014-01-30 21:49:51 +11001076# endif
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001077# ifdef FLAC__SSSE3_SUPPORTED
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +11001078 if (encoder->private_->cpuinfo.x86.ssse3)
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001079 encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_ssse3;
1080# endif
Erik de Castro Lopo11b5d512014-10-04 09:42:54 +10001081# ifdef FLAC__AVX2_SUPPORTED
Erik de Castro Lopo61e7dfa2017-02-27 20:35:21 +11001082 if (encoder->private_->cpuinfo.x86.avx2)
Erik de Castro Lopo11b5d512014-10-04 09:42:54 +10001083 encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_avx2;
1084# endif
Erik de Castro Lopob8d58e32014-06-15 20:29:34 +10001085# elif defined FLAC__CPU_X86_64
Erik de Castro Lopoace63cc2014-02-25 18:28:22 +11001086# ifdef FLAC__SSE2_SUPPORTED
Erik de Castro Lopoad2ae452014-10-04 09:13:07 +10001087 encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_sse2;
1088# endif
1089# ifdef FLAC__SSSE3_SUPPORTED
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +10001090 if(encoder->private_->cpuinfo.x86.ssse3)
Erik de Castro Lopo6cd8b422014-01-07 21:14:55 +11001091 encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_ssse3;
Erik de Castro Lopod40e9862014-01-30 21:49:51 +11001092# endif
Erik de Castro Lopo11b5d512014-10-04 09:42:54 +10001093# ifdef FLAC__AVX2_SUPPORTED
1094 if(encoder->private_->cpuinfo.x86.avx2)
1095 encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_avx2;
1096# endif
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +10001097# endif /* FLAC__CPU_... */
1098 }
1099#endif /* !FLAC__NO_ASM && FLAC__HAS_X86INTRIN */
Josh Coalsoncf30f502001-05-23 20:57:44 +00001100
Josh Coalson6b21f662006-09-13 01:42:27 +00001101 /* set state to OK; from here on, errors are fatal and we'll override the state then */
1102 encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
1103
Josh Coalson8da98c82006-10-15 04:24:05 +00001104#if FLAC__HAS_OGG
1105 encoder->private_->is_ogg = is_ogg;
1106 if(is_ogg && !FLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) {
1107 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
1108 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1109 }
1110#endif
1111
1112 encoder->private_->read_callback = read_callback;
Josh Coalson6b21f662006-09-13 01:42:27 +00001113 encoder->private_->write_callback = write_callback;
1114 encoder->private_->seek_callback = seek_callback;
1115 encoder->private_->tell_callback = tell_callback;
1116 encoder->private_->metadata_callback = metadata_callback;
1117 encoder->private_->client_data = client_data;
1118
Josh Coalsonf1eff452002-07-31 07:05:33 +00001119 if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001120 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001121 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001122 }
Josh Coalsonaec256b2002-03-12 16:19:54 +00001123
Josh Coalson423f8042007-01-28 17:40:26 +00001124 if(!FLAC__bitwriter_init(encoder->private_->frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00001125 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1126 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1127 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001128
1129 /*
Josh Coalsond86e03b2002-08-03 21:56:15 +00001130 * Set up the verify stuff if necessary
1131 */
1132 if(encoder->protected_->verify) {
1133 /*
1134 * First, set up the fifo which will hold the
1135 * original signal to compare against
1136 */
Josh Coalson49f2f162006-11-09 16:54:52 +00001137 encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001138 for(i = 0; i < encoder->protected_->channels; i++) {
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +10001139 if(0 == (encoder->private_->verify.input_fifo.data[i] = safe_malloc_mul_2op_p(sizeof(FLAC__int32), /*times*/encoder->private_->verify.input_fifo.size))) {
Josh Coalson6b21f662006-09-13 01:42:27 +00001140 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1141 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1142 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001143 }
1144 encoder->private_->verify.input_fifo.tail = 0;
1145
1146 /*
1147 * Now set up a stream decoder for verification
1148 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001149 if(0 == encoder->private_->verify.decoder) {
Erik de Castro Lopo3b414512012-02-01 22:01:05 +11001150 encoder->private_->verify.decoder = FLAC__stream_decoder_new();
1151 if(0 == encoder->private_->verify.decoder) {
1152 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1153 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1154 }
Josh Coalson6b21f662006-09-13 01:42:27 +00001155 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001156
Josh Coalson6b21f662006-09-13 01:42:27 +00001157 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) {
1158 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
1159 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1160 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001161 }
Josh Coalson589f8c72002-08-07 23:54:55 +00001162 encoder->private_->verify.error_stats.absolute_sample = 0;
1163 encoder->private_->verify.error_stats.frame_number = 0;
1164 encoder->private_->verify.error_stats.channel = 0;
1165 encoder->private_->verify.error_stats.sample = 0;
1166 encoder->private_->verify.error_stats.expected = 0;
1167 encoder->private_->verify.error_stats.got = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001168
1169 /*
Josh Coalson6b21f662006-09-13 01:42:27 +00001170 * These must be done before we write any metadata, because that
1171 * calls the write_callback, which uses these values.
1172 */
1173 encoder->private_->first_seekpoint_to_check = 0;
1174 encoder->private_->samples_written = 0;
1175 encoder->protected_->streaminfo_offset = 0;
1176 encoder->protected_->seektable_offset = 0;
1177 encoder->protected_->audio_offset = 0;
1178
1179 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001180 * write the stream header
1181 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001182 if(encoder->protected_->verify)
1183 encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
Josh Coalson423f8042007-01-28 17:40:26 +00001184 if(!FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00001185 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1186 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1187 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001188 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001189 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001190 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001191 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001192
Josh Coalson5c491a12002-08-01 06:39:40 +00001193 /*
1194 * write the STREAMINFO metadata block
1195 */
Josh Coalsond86e03b2002-08-03 21:56:15 +00001196 if(encoder->protected_->verify)
1197 encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
Josh Coalson6b21f662006-09-13 01:42:27 +00001198 encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1199 encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1200 encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1201 encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1202 encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1203 encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1204 encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1205 encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1206 encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1207 encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1208 encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1209 memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
Josh Coalson00da5ae2007-06-14 05:19:46 +00001210 if(encoder->protected_->do_md5)
1211 FLAC__MD5Init(&encoder->private_->md5context);
Josh Coalson6b21f662006-09-13 01:42:27 +00001212 if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1213 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1214 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1215 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001216 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001217 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001218 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001219 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001220
Josh Coalson5c491a12002-08-01 06:39:40 +00001221 /*
1222 * Now that the STREAMINFO block is written, we can init this to an
1223 * absurdly-high value...
1224 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001225 encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +00001226 /* ... and clear this to 0 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001227 encoder->private_->streaminfo.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001228
Josh Coalson5c491a12002-08-01 06:39:40 +00001229 /*
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001230 * Check to see if the supplied metadata contains a VORBIS_COMMENT;
1231 * if not, we will write an empty one (FLAC__add_metadata_block()
1232 * automatically supplies the vendor string).
Josh Coalson69cfda72004-09-10 00:38:21 +00001233 *
Josh Coalson8da98c82006-10-15 04:24:05 +00001234 * WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1235 * the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1236 * true it will have already insured that the metadata list is properly
1237 * ordered.)
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001238 */
1239 if(!metadata_has_vorbis_comment) {
1240 FLAC__StreamMetadata vorbis_comment;
1241 vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
1242 vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
1243 vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
1244 vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
1245 vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
1246 vorbis_comment.data.vorbis_comment.num_comments = 0;
1247 vorbis_comment.data.vorbis_comment.comments = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00001248 if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame)) {
1249 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1250 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1251 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001252 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001253 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001254 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsoncb9d93a2002-08-25 05:27:15 +00001255 }
1256 }
1257
1258 /*
Josh Coalson5c491a12002-08-01 06:39:40 +00001259 * write the user's metadata blocks
1260 */
1261 for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
1262 encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
Josh Coalson6b21f662006-09-13 01:42:27 +00001263 if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1264 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1265 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1266 }
Josh Coalson49f2f162006-11-09 16:54:52 +00001267 if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00001268 /* the above function sets the state for us in case of an error */
Josh Coalson6b21f662006-09-13 01:42:27 +00001269 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
Josh Coalsond86e03b2002-08-03 21:56:15 +00001270 }
Josh Coalson5c491a12002-08-01 06:39:40 +00001271 }
1272
Josh Coalson6b21f662006-09-13 01:42:27 +00001273 /* now that all the metadata is written, we save the stream offset */
1274 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 */
1275 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1276 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1277 }
1278
Josh Coalsond86e03b2002-08-03 21:56:15 +00001279 if(encoder->protected_->verify)
1280 encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
1281
Josh Coalson6b21f662006-09-13 01:42:27 +00001282 return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1283}
1284
Josh Coalson8da98c82006-10-15 04:24:05 +00001285FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1286 FLAC__StreamEncoder *encoder,
1287 FLAC__StreamEncoderWriteCallback write_callback,
1288 FLAC__StreamEncoderSeekCallback seek_callback,
1289 FLAC__StreamEncoderTellCallback tell_callback,
1290 FLAC__StreamEncoderMetadataCallback metadata_callback,
1291 void *client_data
1292)
1293{
1294 return init_stream_internal_(
1295 encoder,
1296 /*read_callback=*/0,
1297 write_callback,
1298 seek_callback,
1299 tell_callback,
1300 metadata_callback,
1301 client_data,
1302 /*is_ogg=*/false
1303 );
1304}
1305
1306FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1307 FLAC__StreamEncoder *encoder,
1308 FLAC__StreamEncoderReadCallback read_callback,
1309 FLAC__StreamEncoderWriteCallback write_callback,
1310 FLAC__StreamEncoderSeekCallback seek_callback,
1311 FLAC__StreamEncoderTellCallback tell_callback,
1312 FLAC__StreamEncoderMetadataCallback metadata_callback,
1313 void *client_data
1314)
1315{
1316 return init_stream_internal_(
1317 encoder,
1318 read_callback,
1319 write_callback,
1320 seek_callback,
1321 tell_callback,
1322 metadata_callback,
1323 client_data,
1324 /*is_ogg=*/true
1325 );
1326}
Erik de Castro Lopo59afe362012-02-04 16:13:37 +11001327
Josh Coalson8da98c82006-10-15 04:24:05 +00001328static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1329 FLAC__StreamEncoder *encoder,
1330 FILE *file,
1331 FLAC__StreamEncoderProgressCallback progress_callback,
1332 void *client_data,
1333 FLAC__bool is_ogg
1334)
Josh Coalson6b21f662006-09-13 01:42:27 +00001335{
1336 FLAC__StreamEncoderInitStatus init_status;
1337
1338 FLAC__ASSERT(0 != encoder);
1339 FLAC__ASSERT(0 != file);
1340
Josh Coalson6b21f662006-09-13 01:42:27 +00001341 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1342 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1343
1344 /* double protection */
1345 if(file == 0) {
1346 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1347 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1348 }
1349
Josh Coalson92f7fa92006-10-09 05:34:21 +00001350 /*
1351 * To make sure that our file does not go unclosed after an error, we
1352 * must assign the FILE pointer before any further error can occur in
1353 * this routine.
1354 */
Josh Coalson6b21f662006-09-13 01:42:27 +00001355 if(file == stdout)
1356 file = get_binary_stdout_(); /* just to be safe */
1357
Erik de Castro Lopoe8632472014-09-28 08:43:24 +10001358#ifdef _WIN32
1359 /*
1360 * Windows can suffer quite badly from disk fragmentation. This can be
1361 * reduced significantly by setting the output buffer size to be 10MB.
1362 */
Erik de Castro Lopof0ee4532015-12-12 08:23:22 +11001363 if(GetFileType((HANDLE)_get_osfhandle(_fileno(file))) == FILE_TYPE_DISK)
1364 setvbuf(file, NULL, _IOFBF, 10*1024*1024);
Erik de Castro Lopoe8632472014-09-28 08:43:24 +10001365#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00001366 encoder->private_->file = file;
1367
1368 encoder->private_->progress_callback = progress_callback;
1369 encoder->private_->bytes_written = 0;
1370 encoder->private_->samples_written = 0;
1371 encoder->private_->frames_written = 0;
1372
Josh Coalson8da98c82006-10-15 04:24:05 +00001373 init_status = init_stream_internal_(
1374 encoder,
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001375 encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0,
Josh Coalson8da98c82006-10-15 04:24:05 +00001376 file_write_callback_,
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001377 encoder->private_->file == stdout? 0 : file_seek_callback_,
1378 encoder->private_->file == stdout? 0 : file_tell_callback_,
Josh Coalson8da98c82006-10-15 04:24:05 +00001379 /*metadata_callback=*/0,
1380 client_data,
1381 is_ogg
1382 );
Josh Coalson6b21f662006-09-13 01:42:27 +00001383 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1384 /* the above function sets the state for us in case of an error */
1385 return init_status;
1386 }
1387
1388 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001389 uint32_t blocksize = FLAC__stream_encoder_get_blocksize(encoder);
Josh Coalson6b21f662006-09-13 01:42:27 +00001390
1391 FLAC__ASSERT(blocksize != 0);
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001392 encoder->private_->total_frames_estimate = (uint32_t)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
Josh Coalson6b21f662006-09-13 01:42:27 +00001393 }
1394
1395 return init_status;
1396}
Erik de Castro Lopo59afe362012-02-04 16:13:37 +11001397
Josh Coalson8da98c82006-10-15 04:24:05 +00001398FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1399 FLAC__StreamEncoder *encoder,
1400 FILE *file,
1401 FLAC__StreamEncoderProgressCallback progress_callback,
1402 void *client_data
1403)
1404{
1405 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1406}
Erik de Castro Lopo59afe362012-02-04 16:13:37 +11001407
Josh Coalson8da98c82006-10-15 04:24:05 +00001408FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1409 FLAC__StreamEncoder *encoder,
1410 FILE *file,
1411 FLAC__StreamEncoderProgressCallback progress_callback,
1412 void *client_data
1413)
1414{
1415 return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1416}
Josh Coalson6b21f662006-09-13 01:42:27 +00001417
Josh Coalson8da98c82006-10-15 04:24:05 +00001418static FLAC__StreamEncoderInitStatus init_file_internal_(
1419 FLAC__StreamEncoder *encoder,
1420 const char *filename,
1421 FLAC__StreamEncoderProgressCallback progress_callback,
1422 void *client_data,
1423 FLAC__bool is_ogg
1424)
Josh Coalson6b21f662006-09-13 01:42:27 +00001425{
1426 FILE *file;
1427
1428 FLAC__ASSERT(0 != encoder);
1429
1430 /*
1431 * To make sure that our file does not go unclosed after an error, we
1432 * have to do the same entrance checks here that are later performed
1433 * in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1434 */
1435 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1436 return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1437
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +11001438 file = filename? flac_fopen(filename, "w+b") : stdout;
Josh Coalson6b21f662006-09-13 01:42:27 +00001439
1440 if(file == 0) {
1441 encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1442 return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1443 }
1444
Josh Coalson8da98c82006-10-15 04:24:05 +00001445 return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1446}
1447
1448FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1449 FLAC__StreamEncoder *encoder,
1450 const char *filename,
1451 FLAC__StreamEncoderProgressCallback progress_callback,
1452 void *client_data
1453)
1454{
1455 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1456}
1457
1458FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1459 FLAC__StreamEncoder *encoder,
1460 const char *filename,
1461 FLAC__StreamEncoderProgressCallback progress_callback,
1462 void *client_data
1463)
1464{
1465 return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001466}
1467
Josh Coalsona5862262006-11-09 06:58:26 +00001468FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001469{
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001470 FLAC__bool error = false;
Josh Coalsona5862262006-11-09 06:58:26 +00001471
Erik de Castro Lopo006dcfb2017-01-31 06:39:22 +11001472 if (encoder == NULL)
1473 return false;
1474
Josh Coalson6b21f662006-09-13 01:42:27 +00001475 FLAC__ASSERT(0 != encoder->private_);
1476 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson2b245f22002-08-07 17:10:50 +00001477
Josh Coalsonfa697a92001-08-16 20:07:29 +00001478 if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsona5862262006-11-09 06:58:26 +00001479 return true;
Josh Coalson2b245f22002-08-07 17:10:50 +00001480
Josh Coalson3262b0d2002-08-14 20:58:42 +00001481 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
Josh Coalson2b245f22002-08-07 17:10:50 +00001482 if(encoder->private_->current_sample_number != 0) {
Josh Coalson49f2f162006-11-09 16:54:52 +00001483 const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
Josh Coalson2b245f22002-08-07 17:10:50 +00001484 encoder->protected_->blocksize = encoder->private_->current_sample_number;
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001485 if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
1486 error = true;
Josh Coalson2b245f22002-08-07 17:10:50 +00001487 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001488 }
Josh Coalson2b245f22002-08-07 17:10:50 +00001489
Josh Coalson00da5ae2007-06-14 05:19:46 +00001490 if(encoder->protected_->do_md5)
1491 FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
Josh Coalson2b245f22002-08-07 17:10:50 +00001492
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001493 if(!encoder->private_->is_being_deleted) {
1494 if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
1495 if(encoder->private_->seek_callback) {
Josh Coalson8da98c82006-10-15 04:24:05 +00001496#if FLAC__HAS_OGG
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001497 if(encoder->private_->is_ogg)
1498 update_ogg_metadata_(encoder);
1499 else
Josh Coalson8da98c82006-10-15 04:24:05 +00001500#endif
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001501 update_metadata_(encoder);
Josh Coalson0a15c142001-06-13 17:59:57 +00001502
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001503 /* check if an error occurred while updating metadata */
1504 if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
1505 error = true;
1506 }
1507 if(encoder->private_->metadata_callback)
1508 encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1509 }
1510
1511 if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
1512 if(!error)
1513 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1514 error = true;
1515 }
1516 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00001517
Josh Coalson6b21f662006-09-13 01:42:27 +00001518 if(0 != encoder->private_->file) {
1519 if(encoder->private_->file != stdout)
1520 fclose(encoder->private_->file);
1521 encoder->private_->file = 0;
1522 }
1523
Josh Coalson8da98c82006-10-15 04:24:05 +00001524#if FLAC__HAS_OGG
1525 if(encoder->private_->is_ogg)
1526 FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
1527#endif
1528
Josh Coalsonf1eff452002-07-31 07:05:33 +00001529 free_(encoder);
1530 set_defaults_(encoder);
Josh Coalson92031602002-07-24 06:02:11 +00001531
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001532 if(!error)
Josh Coalsona5862262006-11-09 06:58:26 +00001533 encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1534
Josh Coalsoncf1422e2006-11-16 01:32:25 +00001535 return !error;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001536}
1537
Josh Coalson71d5c252006-10-15 06:04:55 +00001538FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
Josh Coalson8da98c82006-10-15 04:24:05 +00001539{
1540 FLAC__ASSERT(0 != encoder);
1541 FLAC__ASSERT(0 != encoder->private_);
1542 FLAC__ASSERT(0 != encoder->protected_);
1543 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1544 return false;
Josh Coalsonf1ac7d92006-11-16 07:20:09 +00001545#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +00001546 /* can't check encoder->private_->is_ogg since that's not set until init time */
1547 FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
1548 return true;
1549#else
1550 (void)value;
1551 return false;
1552#endif
1553}
1554
Josh Coalson6afed9f2002-10-16 22:29:47 +00001555FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001556{
1557 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001558 FLAC__ASSERT(0 != encoder->private_);
1559 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001560 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1561 return false;
Josh Coalson47c7b142005-01-29 06:08:58 +00001562#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
Josh Coalsond86e03b2002-08-03 21:56:15 +00001563 encoder->protected_->verify = value;
Josh Coalson47c7b142005-01-29 06:08:58 +00001564#endif
Josh Coalsond86e03b2002-08-03 21:56:15 +00001565 return true;
1566}
1567
Josh Coalson6afed9f2002-10-16 22:29:47 +00001568FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001569{
Josh Coalson92031602002-07-24 06:02:11 +00001570 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001571 FLAC__ASSERT(0 != encoder->private_);
1572 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001573 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001574 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001575 encoder->protected_->streamable_subset = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001576 return true;
1577}
1578
Josh Coalson00da5ae2007-06-14 05:19:46 +00001579FLAC_API FLAC__bool FLAC__stream_encoder_set_do_md5(FLAC__StreamEncoder *encoder, FLAC__bool value)
1580{
1581 FLAC__ASSERT(0 != encoder);
1582 FLAC__ASSERT(0 != encoder->private_);
1583 FLAC__ASSERT(0 != encoder->protected_);
1584 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1585 return false;
1586 encoder->protected_->do_md5 = value;
1587 return true;
1588}
1589
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001590FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, uint32_t value)
Josh Coalson00e53872001-06-16 07:32:25 +00001591{
Josh Coalson92031602002-07-24 06:02:11 +00001592 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001593 FLAC__ASSERT(0 != encoder->private_);
1594 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001595 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001596 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001597 encoder->protected_->channels = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001598 return true;
1599}
1600
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001601FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, uint32_t value)
Josh Coalson00e53872001-06-16 07:32:25 +00001602{
Josh Coalson92031602002-07-24 06:02:11 +00001603 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001604 FLAC__ASSERT(0 != encoder->private_);
1605 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001606 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001607 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001608 encoder->protected_->bits_per_sample = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001609 return true;
1610}
1611
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001612FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, uint32_t value)
Josh Coalson00e53872001-06-16 07:32:25 +00001613{
Josh Coalson92031602002-07-24 06:02:11 +00001614 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001615 FLAC__ASSERT(0 != encoder->private_);
1616 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001617 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001618 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001619 encoder->protected_->sample_rate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001620 return true;
1621}
1622
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001623FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, uint32_t value)
Josh Coalson425609c2006-11-03 16:08:52 +00001624{
1625 FLAC__bool ok = true;
1626 FLAC__ASSERT(0 != encoder);
1627 FLAC__ASSERT(0 != encoder->private_);
1628 FLAC__ASSERT(0 != encoder->protected_);
1629 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1630 return false;
1631 if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1632 value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1633 ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
1634 ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
Josh Coalsonf395ec42007-06-20 01:23:02 +00001635#ifndef FLAC__INTEGER_ONLY_LIBRARY
Erik de Castro Lopoe0fbe712014-10-15 06:47:39 +11001636#if 1
Josh Coalson425609c2006-11-03 16:08:52 +00001637 ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
Josh Coalson4e8fe852006-12-05 01:36:46 +00001638#else
Erik de Castro Lopoe0fbe712014-10-15 06:47:39 +11001639 /* equivalent to -A tukey(0.5) */
Josh Coalson4e8fe852006-12-05 01:36:46 +00001640 encoder->protected_->num_apodizations = 1;
1641 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1642 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1643#endif
Josh Coalsonf395ec42007-06-20 01:23:02 +00001644#endif
Josh Coalson425609c2006-11-03 16:08:52 +00001645 ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1646 ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1647 ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1648 ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1649 ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1650 ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1651 ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1652 ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1653 return ok;
1654}
1655
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001656FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, uint32_t value)
Josh Coalson00e53872001-06-16 07:32:25 +00001657{
Josh Coalson92031602002-07-24 06:02:11 +00001658 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001659 FLAC__ASSERT(0 != encoder->private_);
1660 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001661 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001662 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001663 encoder->protected_->blocksize = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001664 return true;
1665}
1666
Josh Coalson425609c2006-11-03 16:08:52 +00001667FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1668{
1669 FLAC__ASSERT(0 != encoder);
1670 FLAC__ASSERT(0 != encoder->private_);
1671 FLAC__ASSERT(0 != encoder->protected_);
1672 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1673 return false;
1674 encoder->protected_->do_mid_side_stereo = value;
1675 return true;
1676}
1677
1678FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1679{
1680 FLAC__ASSERT(0 != encoder);
1681 FLAC__ASSERT(0 != encoder->private_);
1682 FLAC__ASSERT(0 != encoder->protected_);
1683 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1684 return false;
1685 encoder->protected_->loose_mid_side_stereo = value;
1686 return true;
1687}
1688
Josh Coalson504dcaf2007-09-13 15:42:47 +00001689/*@@@@add to tests*/
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001690FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1691{
1692 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001693 FLAC__ASSERT(0 != encoder->private_);
1694 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001695 FLAC__ASSERT(0 != specification);
1696 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1697 return false;
1698#ifdef FLAC__INTEGER_ONLY_LIBRARY
1699 (void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1700#else
1701 encoder->protected_->num_apodizations = 0;
1702 while(1) {
1703 const char *s = strchr(specification, ';');
1704 const size_t n = s? (size_t)(s - specification) : strlen(specification);
1705 if (n==8 && 0 == strncmp("bartlett" , specification, n))
1706 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1707 else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1708 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1709 else if(n==8 && 0 == strncmp("blackman" , specification, n))
1710 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1711 else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1712 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1713 else if(n==6 && 0 == strncmp("connes" , specification, n))
1714 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1715 else if(n==7 && 0 == strncmp("flattop" , specification, n))
1716 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1717 else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1718 FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1719 if (stddev > 0.0 && stddev <= 0.5) {
1720 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1721 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1722 }
1723 }
1724 else if(n==7 && 0 == strncmp("hamming" , specification, n))
1725 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1726 else if(n==4 && 0 == strncmp("hann" , specification, n))
1727 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1728 else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1729 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1730 else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1731 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1732 else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1733 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1734 else if(n==8 && 0 == strncmp("triangle" , specification, n))
1735 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1736 else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1737 FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1738 if (p >= 0.0 && p <= 1.0) {
1739 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1740 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1741 }
1742 }
Martijn van Beurden29a28332014-08-10 10:59:29 +02001743 else if(n>15 && 0 == strncmp("partial_tukey(" , specification, 14)) {
1744 FLAC__int32 tukey_parts = (FLAC__int32)strtod(specification+14, 0);
1745 const char *si_1 = strchr(specification, '/');
1746 FLAC__real overlap = si_1?flac_min((FLAC__real)strtod(si_1+1, 0),0.99f):0.1f;
1747 FLAC__real overlap_units = 1.0f/(1.0f - overlap) - 1.0f;
1748 const char *si_2 = strchr((si_1?(si_1+1):specification), '/');
1749 FLAC__real tukey_p = si_2?(FLAC__real)strtod(si_2+1, 0):0.2f;
1750
1751 if (tukey_parts <= 1) {
1752 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = tukey_p;
1753 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1754 }else if (encoder->protected_->num_apodizations + tukey_parts < 32){
1755 FLAC__int32 m;
1756 for(m = 0; m < tukey_parts; m++){
1757 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.p = tukey_p;
1758 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.start = m/(tukey_parts+overlap_units);
1759 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.end = (m+1+overlap_units)/(tukey_parts+overlap_units);
1760 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_PARTIAL_TUKEY;
1761 }
1762 }
1763 }
1764 else if(n>16 && 0 == strncmp("punchout_tukey(" , specification, 15)) {
1765 FLAC__int32 tukey_parts = (FLAC__int32)strtod(specification+15, 0);
1766 const char *si_1 = strchr(specification, '/');
1767 FLAC__real overlap = si_1?flac_min((FLAC__real)strtod(si_1+1, 0),0.99f):0.2f;
1768 FLAC__real overlap_units = 1.0f/(1.0f - overlap) - 1.0f;
1769 const char *si_2 = strchr((si_1?(si_1+1):specification), '/');
1770 FLAC__real tukey_p = si_2?(FLAC__real)strtod(si_2+1, 0):0.2f;
1771
1772 if (tukey_parts <= 1) {
1773 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = tukey_p;
1774 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1775 }else if (encoder->protected_->num_apodizations + tukey_parts < 32){
1776 FLAC__int32 m;
1777 for(m = 0; m < tukey_parts; m++){
1778 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.p = tukey_p;
1779 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.start = m/(tukey_parts+overlap_units);
1780 encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.multiple_tukey.end = (m+1+overlap_units)/(tukey_parts+overlap_units);
1781 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_PUNCHOUT_TUKEY;
1782 }
1783 }
1784 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001785 else if(n==5 && 0 == strncmp("welch" , specification, n))
1786 encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1787 if (encoder->protected_->num_apodizations == 32)
1788 break;
1789 if (s)
1790 specification = s+1;
1791 else
1792 break;
1793 }
1794 if(encoder->protected_->num_apodizations == 0) {
1795 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00001796 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1797 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001798 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00001799#endif
1800 return true;
1801}
1802
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001803FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, uint32_t value)
Josh Coalson00e53872001-06-16 07:32:25 +00001804{
Josh Coalson92031602002-07-24 06:02:11 +00001805 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001806 FLAC__ASSERT(0 != encoder->private_);
1807 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001808 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001809 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001810 encoder->protected_->max_lpc_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001811 return true;
1812}
1813
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001814FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, uint32_t value)
Josh Coalson00e53872001-06-16 07:32:25 +00001815{
Josh Coalson92031602002-07-24 06:02:11 +00001816 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001817 FLAC__ASSERT(0 != encoder->private_);
1818 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001819 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001820 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001821 encoder->protected_->qlp_coeff_precision = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001822 return true;
1823}
1824
Josh Coalson6afed9f2002-10-16 22:29:47 +00001825FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001826{
Josh Coalson92031602002-07-24 06:02:11 +00001827 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001828 FLAC__ASSERT(0 != encoder->private_);
1829 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001830 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001831 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001832 encoder->protected_->do_qlp_coeff_prec_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001833 return true;
1834}
1835
Josh Coalson6afed9f2002-10-16 22:29:47 +00001836FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson8395d022001-07-12 21:25:22 +00001837{
Josh Coalson92031602002-07-24 06:02:11 +00001838 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001839 FLAC__ASSERT(0 != encoder->private_);
1840 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001841 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson8395d022001-07-12 21:25:22 +00001842 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001843#if 0
1844 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001845 encoder->protected_->do_escape_coding = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001846#else
1847 (void)value;
1848#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001849 return true;
1850}
1851
Josh Coalson6afed9f2002-10-16 22:29:47 +00001852FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +00001853{
Josh Coalson92031602002-07-24 06:02:11 +00001854 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001855 FLAC__ASSERT(0 != encoder->private_);
1856 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001857 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001858 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001859 encoder->protected_->do_exhaustive_model_search = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001860 return true;
1861}
1862
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001863FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, uint32_t value)
Josh Coalson00e53872001-06-16 07:32:25 +00001864{
Josh Coalson92031602002-07-24 06:02:11 +00001865 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001866 FLAC__ASSERT(0 != encoder->private_);
1867 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001868 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001869 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001870 encoder->protected_->min_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001871 return true;
1872}
1873
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001874FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, uint32_t value)
Josh Coalson00e53872001-06-16 07:32:25 +00001875{
Josh Coalson92031602002-07-24 06:02:11 +00001876 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001877 FLAC__ASSERT(0 != encoder->private_);
1878 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001879 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001880 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001881 encoder->protected_->max_residual_partition_order = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001882 return true;
1883}
1884
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001885FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, uint32_t value)
Josh Coalson00e53872001-06-16 07:32:25 +00001886{
Josh Coalson92031602002-07-24 06:02:11 +00001887 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001888 FLAC__ASSERT(0 != encoder->private_);
1889 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001890 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001891 return false;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001892#if 0
1893 /*@@@ deprecated: */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001894 encoder->protected_->rice_parameter_search_dist = value;
Josh Coalson680e3aa2002-08-01 07:32:17 +00001895#else
1896 (void)value;
1897#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001898 return true;
1899}
1900
Josh Coalson6afed9f2002-10-16 22:29:47 +00001901FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +00001902{
Josh Coalson92031602002-07-24 06:02:11 +00001903 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001904 FLAC__ASSERT(0 != encoder->private_);
1905 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001906 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001907 return false;
Erik de Castro Lopo262505b2016-01-09 10:46:41 +11001908 value = flac_min(value, (FLAC__U64L(1) << FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN) - 1);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001909 encoder->protected_->total_samples_estimate = value;
Josh Coalson00e53872001-06-16 07:32:25 +00001910 return true;
1911}
1912
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11001913FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, uint32_t num_blocks)
Josh Coalson00e53872001-06-16 07:32:25 +00001914{
Josh Coalson92031602002-07-24 06:02:11 +00001915 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001916 FLAC__ASSERT(0 != encoder->private_);
1917 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001918 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +00001919 return false;
Josh Coalson1c034072007-01-29 08:27:25 +00001920 if(0 == metadata)
1921 num_blocks = 0;
1922 if(0 == num_blocks)
1923 metadata = 0;
1924 /* realloc() does not do exactly what we want so... */
1925 if(encoder->protected_->metadata) {
1926 free(encoder->protected_->metadata);
1927 encoder->protected_->metadata = 0;
1928 encoder->protected_->num_metadata_blocks = 0;
1929 }
1930 if(num_blocks) {
1931 FLAC__StreamMetadata **m;
Erik de Castro Lopo8749dc22012-06-22 14:23:56 +10001932 if(0 == (m = safe_malloc_mul_2op_p(sizeof(m[0]), /*times*/num_blocks)))
Josh Coalson1c034072007-01-29 08:27:25 +00001933 return false;
1934 memcpy(m, metadata, sizeof(m[0]) * num_blocks);
1935 encoder->protected_->metadata = m;
1936 encoder->protected_->num_metadata_blocks = num_blocks;
1937 }
Josh Coalson8da98c82006-10-15 04:24:05 +00001938#if FLAC__HAS_OGG
1939 if(!FLAC__ogg_encoder_aspect_set_num_metadata(&encoder->protected_->ogg_encoder_aspect, num_blocks))
1940 return false;
1941#endif
Josh Coalson00e53872001-06-16 07:32:25 +00001942 return true;
1943}
1944
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001945/*
luz.paz01eb1972018-05-22 10:17:53 -04001946 * These three functions are not static, but not publicly exposed in
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001947 * include/FLAC/ either. They are used by the test suite.
1948 */
Josh Coalson6afed9f2002-10-16 22:29:47 +00001949FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001950{
1951 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001952 FLAC__ASSERT(0 != encoder->private_);
1953 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001954 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1955 return false;
1956 encoder->private_->disable_constant_subframes = value;
1957 return true;
1958}
1959
Josh Coalson6afed9f2002-10-16 22:29:47 +00001960FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001961{
1962 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001963 FLAC__ASSERT(0 != encoder->private_);
1964 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001965 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1966 return false;
1967 encoder->private_->disable_fixed_subframes = value;
1968 return true;
1969}
1970
Josh Coalson6afed9f2002-10-16 22:29:47 +00001971FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001972{
1973 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001974 FLAC__ASSERT(0 != encoder->private_);
1975 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00001976 if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1977 return false;
1978 encoder->private_->disable_verbatim_subframes = value;
1979 return true;
1980}
1981
Josh Coalson6afed9f2002-10-16 22:29:47 +00001982FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00001983{
Josh Coalson92031602002-07-24 06:02:11 +00001984 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001985 FLAC__ASSERT(0 != encoder->private_);
1986 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001987 return encoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +00001988}
1989
Josh Coalson6afed9f2002-10-16 22:29:47 +00001990FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00001991{
1992 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00001993 FLAC__ASSERT(0 != encoder->private_);
1994 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00001995 if(encoder->protected_->verify)
1996 return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
1997 else
1998 return FLAC__STREAM_DECODER_UNINITIALIZED;
1999}
2000
Josh Coalson02954222002-11-08 06:16:31 +00002001FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
2002{
Josh Coalson8da98c82006-10-15 04:24:05 +00002003 FLAC__ASSERT(0 != encoder);
2004 FLAC__ASSERT(0 != encoder->private_);
2005 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson02954222002-11-08 06:16:31 +00002006 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
2007 return FLAC__StreamEncoderStateString[encoder->protected_->state];
2008 else
Josh Coalson807140d2003-09-24 22:10:51 +00002009 return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
Josh Coalson02954222002-11-08 06:16:31 +00002010}
2011
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002012FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, uint32_t *frame_number, uint32_t *channel, uint32_t *sample, FLAC__int32 *expected, FLAC__int32 *got)
Josh Coalson589f8c72002-08-07 23:54:55 +00002013{
2014 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002015 FLAC__ASSERT(0 != encoder->private_);
2016 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson589f8c72002-08-07 23:54:55 +00002017 if(0 != absolute_sample)
2018 *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
2019 if(0 != frame_number)
2020 *frame_number = encoder->private_->verify.error_stats.frame_number;
2021 if(0 != channel)
2022 *channel = encoder->private_->verify.error_stats.channel;
2023 if(0 != sample)
2024 *sample = encoder->private_->verify.error_stats.sample;
2025 if(0 != expected)
2026 *expected = encoder->private_->verify.error_stats.expected;
2027 if(0 != got)
2028 *got = encoder->private_->verify.error_stats.got;
2029}
2030
Josh Coalson6afed9f2002-10-16 22:29:47 +00002031FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
Josh Coalsond86e03b2002-08-03 21:56:15 +00002032{
2033 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002034 FLAC__ASSERT(0 != encoder->private_);
2035 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsond86e03b2002-08-03 21:56:15 +00002036 return encoder->protected_->verify;
2037}
2038
Josh Coalson6afed9f2002-10-16 22:29:47 +00002039FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002040{
Josh Coalson92031602002-07-24 06:02:11 +00002041 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002042 FLAC__ASSERT(0 != encoder->private_);
2043 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002044 return encoder->protected_->streamable_subset;
Josh Coalson0a15c142001-06-13 17:59:57 +00002045}
2046
Josh Coalson00da5ae2007-06-14 05:19:46 +00002047FLAC_API FLAC__bool FLAC__stream_encoder_get_do_md5(const FLAC__StreamEncoder *encoder)
2048{
2049 FLAC__ASSERT(0 != encoder);
2050 FLAC__ASSERT(0 != encoder->private_);
2051 FLAC__ASSERT(0 != encoder->protected_);
2052 return encoder->protected_->do_md5;
2053}
2054
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002055FLAC_API uint32_t FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002056{
Josh Coalson92031602002-07-24 06:02:11 +00002057 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002058 FLAC__ASSERT(0 != encoder->private_);
2059 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002060 return encoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +00002061}
2062
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002063FLAC_API uint32_t FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002064{
Josh Coalson92031602002-07-24 06:02:11 +00002065 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002066 FLAC__ASSERT(0 != encoder->private_);
2067 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002068 return encoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +00002069}
2070
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002071FLAC_API uint32_t FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002072{
Josh Coalson92031602002-07-24 06:02:11 +00002073 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002074 FLAC__ASSERT(0 != encoder->private_);
2075 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002076 return encoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +00002077}
2078
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002079FLAC_API uint32_t FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002080{
Josh Coalson92031602002-07-24 06:02:11 +00002081 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002082 FLAC__ASSERT(0 != encoder->private_);
2083 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002084 return encoder->protected_->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00002085}
2086
Josh Coalson425609c2006-11-03 16:08:52 +00002087FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
2088{
2089 FLAC__ASSERT(0 != encoder);
2090 FLAC__ASSERT(0 != encoder->private_);
2091 FLAC__ASSERT(0 != encoder->protected_);
2092 return encoder->protected_->do_mid_side_stereo;
2093}
2094
2095FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
2096{
2097 FLAC__ASSERT(0 != encoder);
2098 FLAC__ASSERT(0 != encoder->private_);
2099 FLAC__ASSERT(0 != encoder->protected_);
2100 return encoder->protected_->loose_mid_side_stereo;
2101}
2102
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002103FLAC_API uint32_t FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002104{
Josh Coalson92031602002-07-24 06:02:11 +00002105 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002106 FLAC__ASSERT(0 != encoder->private_);
2107 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002108 return encoder->protected_->max_lpc_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00002109}
2110
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002111FLAC_API uint32_t FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002112{
Josh Coalson92031602002-07-24 06:02:11 +00002113 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002114 FLAC__ASSERT(0 != encoder->private_);
2115 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002116 return encoder->protected_->qlp_coeff_precision;
Josh Coalson0a15c142001-06-13 17:59:57 +00002117}
2118
Josh Coalson6afed9f2002-10-16 22:29:47 +00002119FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002120{
Josh Coalson92031602002-07-24 06:02:11 +00002121 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002122 FLAC__ASSERT(0 != encoder->private_);
2123 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002124 return encoder->protected_->do_qlp_coeff_prec_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00002125}
2126
Josh Coalson6afed9f2002-10-16 22:29:47 +00002127FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
Josh Coalson8395d022001-07-12 21:25:22 +00002128{
Josh Coalson92031602002-07-24 06:02:11 +00002129 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002130 FLAC__ASSERT(0 != encoder->private_);
2131 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002132 return encoder->protected_->do_escape_coding;
Josh Coalson8395d022001-07-12 21:25:22 +00002133}
2134
Josh Coalson6afed9f2002-10-16 22:29:47 +00002135FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002136{
Josh Coalson92031602002-07-24 06:02:11 +00002137 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002138 FLAC__ASSERT(0 != encoder->private_);
2139 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002140 return encoder->protected_->do_exhaustive_model_search;
Josh Coalson0a15c142001-06-13 17:59:57 +00002141}
2142
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002143FLAC_API uint32_t FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002144{
Josh Coalson92031602002-07-24 06:02:11 +00002145 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002146 FLAC__ASSERT(0 != encoder->private_);
2147 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002148 return encoder->protected_->min_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00002149}
2150
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002151FLAC_API uint32_t FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002152{
Josh Coalson92031602002-07-24 06:02:11 +00002153 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002154 FLAC__ASSERT(0 != encoder->private_);
2155 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002156 return encoder->protected_->max_residual_partition_order;
Josh Coalson0a15c142001-06-13 17:59:57 +00002157}
2158
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002159FLAC_API uint32_t FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +00002160{
Josh Coalson92031602002-07-24 06:02:11 +00002161 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002162 FLAC__ASSERT(0 != encoder->private_);
2163 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002164 return encoder->protected_->rice_parameter_search_dist;
Josh Coalson0a15c142001-06-13 17:59:57 +00002165}
2166
Josh Coalson6afed9f2002-10-16 22:29:47 +00002167FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
Josh Coalson3a7b2c92002-08-02 07:38:20 +00002168{
2169 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002170 FLAC__ASSERT(0 != encoder->private_);
2171 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalson3a7b2c92002-08-02 07:38:20 +00002172 return encoder->protected_->total_samples_estimate;
2173}
2174
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002175FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], uint32_t samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002176{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002177 uint32_t i, j = 0, channel;
2178 const uint32_t channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002179
Josh Coalsonf1eff452002-07-31 07:05:33 +00002180 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002181 FLAC__ASSERT(0 != encoder->private_);
2182 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002183 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002184
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002185 do {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002186 const uint32_t n = flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j);
Josh Coalsond86e03b2002-08-03 21:56:15 +00002187
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002188 if(encoder->protected_->verify)
2189 append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, n);
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002190
Erik de Castro Lopoc06a4492015-02-17 18:35:05 +11002191 for(channel = 0; channel < channels; channel++) {
2192 if (buffer[channel] == NULL) {
Erik de Castro Lopoc06a4492015-02-17 18:35:05 +11002193 return false;
2194 }
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002195 memcpy(&encoder->private_->integer_signal[channel][encoder->private_->current_sample_number], &buffer[channel][j], sizeof(buffer[channel][0]) * n);
Erik de Castro Lopoc06a4492015-02-17 18:35:05 +11002196 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00002197
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002198 if(encoder->protected_->do_mid_side_stereo) {
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002199 FLAC__ASSERT(channels == 2);
2200 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2201 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002202 encoder->private_->integer_signal_mid_side[1][i] = buffer[0][j] - buffer[1][j];
2203 encoder->private_->integer_signal_mid_side[0][i] = (buffer[0][j] + buffer[1][j]) >> 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002204 }
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002205 }
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002206 else
2207 j += n;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002208
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002209 encoder->private_->current_sample_number += n;
2210
2211 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2212 if(encoder->private_->current_sample_number > blocksize) {
Josh Coalson0429d602007-04-04 01:08:24 +00002213 FLAC__ASSERT(encoder->private_->current_sample_number == blocksize+OVERREAD_);
2214 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002215 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2216 return false;
2217 /* move unprocessed overread samples to beginnings of arrays */
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002218 for(channel = 0; channel < channels; channel++)
2219 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
2220 if(encoder->protected_->do_mid_side_stereo) {
2221 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
2222 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
2223 }
2224 encoder->private_->current_sample_number = 1;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002225 }
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002226 } while(j < samples);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002227
2228 return true;
2229}
2230
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002231FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], uint32_t samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002232{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002233 uint32_t i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +00002234 FLAC__int32 x, mid, side;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002235 const uint32_t channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002236
Josh Coalsonf1eff452002-07-31 07:05:33 +00002237 FLAC__ASSERT(0 != encoder);
Josh Coalson8da98c82006-10-15 04:24:05 +00002238 FLAC__ASSERT(0 != encoder->private_);
2239 FLAC__ASSERT(0 != encoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002240 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002241
2242 j = k = 0;
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002243 /*
2244 * we have several flavors of the same basic loop, optimized for
2245 * different conditions:
2246 */
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002247 if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2248 /*
2249 * stereo coding: unroll channel loop
2250 */
2251 do {
2252 if(encoder->protected_->verify)
Cristian Rodríguezf0296252012-04-05 19:39:37 -03002253 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00002254
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002255 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2256 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2257 encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
2258 x = buffer[k++];
2259 encoder->private_->integer_signal[1][i] = x;
2260 mid += x;
2261 side -= x;
2262 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2263 encoder->private_->integer_signal_mid_side[1][i] = side;
2264 encoder->private_->integer_signal_mid_side[0][i] = mid;
2265 }
2266 encoder->private_->current_sample_number = i;
2267 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2268 if(i > blocksize) {
2269 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2270 return false;
2271 /* move unprocessed overread samples to beginnings of arrays */
2272 FLAC__ASSERT(i == blocksize+OVERREAD_);
2273 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2274 encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][blocksize];
2275 encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][blocksize];
2276 encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
2277 encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
2278 encoder->private_->current_sample_number = 1;
2279 }
2280 } while(j < samples);
Josh Coalsonaa255362001-05-31 06:17:41 +00002281 }
2282 else {
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002283 /*
2284 * independent channel coding: buffer each channel in inner loop
2285 */
2286 do {
2287 if(encoder->protected_->verify)
Cristian Rodríguezf0296252012-04-05 19:39:37 -03002288 append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
Josh Coalsond86e03b2002-08-03 21:56:15 +00002289
Josh Coalsondca2d1a2007-04-04 01:06:08 +00002290 /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2291 for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2292 for(channel = 0; channel < channels; channel++)
2293 encoder->private_->integer_signal[channel][i] = buffer[k++];
2294 }
2295 encoder->private_->current_sample_number = i;
2296 /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2297 if(i > blocksize) {
2298 if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2299 return false;
2300 /* move unprocessed overread samples to beginnings of arrays */
2301 FLAC__ASSERT(i == blocksize+OVERREAD_);
2302 FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2303 for(channel = 0; channel < channels; channel++)
2304 encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
2305 encoder->private_->current_sample_number = 1;
2306 }
2307 } while(j < samples);
Josh Coalsonaa255362001-05-31 06:17:41 +00002308 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002309
2310 return true;
2311}
2312
Josh Coalsonf1eff452002-07-31 07:05:33 +00002313/***********************************************************************
2314 *
2315 * Private class methods
2316 *
2317 ***********************************************************************/
2318
2319void set_defaults_(FLAC__StreamEncoder *encoder)
Josh Coalson92031602002-07-24 06:02:11 +00002320{
2321 FLAC__ASSERT(0 != encoder);
2322
Josh Coalson47c7b142005-01-29 06:08:58 +00002323#ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
2324 encoder->protected_->verify = true;
2325#else
Josh Coalsond86e03b2002-08-03 21:56:15 +00002326 encoder->protected_->verify = false;
Josh Coalson47c7b142005-01-29 06:08:58 +00002327#endif
Josh Coalson92031602002-07-24 06:02:11 +00002328 encoder->protected_->streamable_subset = true;
Josh Coalson00da5ae2007-06-14 05:19:46 +00002329 encoder->protected_->do_md5 = true;
Josh Coalson92031602002-07-24 06:02:11 +00002330 encoder->protected_->do_mid_side_stereo = false;
2331 encoder->protected_->loose_mid_side_stereo = false;
2332 encoder->protected_->channels = 2;
2333 encoder->protected_->bits_per_sample = 16;
2334 encoder->protected_->sample_rate = 44100;
Josh Coalson425609c2006-11-03 16:08:52 +00002335 encoder->protected_->blocksize = 0;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002336#ifndef FLAC__INTEGER_ONLY_LIBRARY
2337 encoder->protected_->num_apodizations = 1;
Josh Coalson82389362006-05-01 05:58:35 +00002338 encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
2339 encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002340#endif
Josh Coalson92031602002-07-24 06:02:11 +00002341 encoder->protected_->max_lpc_order = 0;
2342 encoder->protected_->qlp_coeff_precision = 0;
2343 encoder->protected_->do_qlp_coeff_prec_search = false;
2344 encoder->protected_->do_exhaustive_model_search = false;
2345 encoder->protected_->do_escape_coding = false;
2346 encoder->protected_->min_residual_partition_order = 0;
2347 encoder->protected_->max_residual_partition_order = 0;
2348 encoder->protected_->rice_parameter_search_dist = 0;
2349 encoder->protected_->total_samples_estimate = 0;
2350 encoder->protected_->metadata = 0;
2351 encoder->protected_->num_metadata_blocks = 0;
2352
Josh Coalson6b21f662006-09-13 01:42:27 +00002353 encoder->private_->seek_table = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00002354 encoder->private_->disable_constant_subframes = false;
2355 encoder->private_->disable_fixed_subframes = false;
2356 encoder->private_->disable_verbatim_subframes = false;
Josh Coalson8da98c82006-10-15 04:24:05 +00002357 encoder->private_->is_ogg = false;
Josh Coalson8da98c82006-10-15 04:24:05 +00002358 encoder->private_->read_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002359 encoder->private_->write_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002360 encoder->private_->seek_callback = 0;
2361 encoder->private_->tell_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002362 encoder->private_->metadata_callback = 0;
Josh Coalson6b21f662006-09-13 01:42:27 +00002363 encoder->private_->progress_callback = 0;
Josh Coalson92031602002-07-24 06:02:11 +00002364 encoder->private_->client_data = 0;
Josh Coalson8da98c82006-10-15 04:24:05 +00002365
2366#if FLAC__HAS_OGG
2367 FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
2368#endif
Josh Coalsona832ef32009-01-02 01:12:59 +00002369
2370 FLAC__stream_encoder_set_compression_level(encoder, 5);
Josh Coalson92031602002-07-24 06:02:11 +00002371}
2372
Josh Coalsonf1eff452002-07-31 07:05:33 +00002373void free_(FLAC__StreamEncoder *encoder)
Josh Coalson639aeb02002-07-25 05:38:23 +00002374{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002375 uint32_t i, channel;
Josh Coalson639aeb02002-07-25 05:38:23 +00002376
Josh Coalsonf1eff452002-07-31 07:05:33 +00002377 FLAC__ASSERT(0 != encoder);
Josh Coalson1c034072007-01-29 08:27:25 +00002378 if(encoder->protected_->metadata) {
2379 free(encoder->protected_->metadata);
2380 encoder->protected_->metadata = 0;
2381 encoder->protected_->num_metadata_blocks = 0;
2382 }
Josh Coalson639aeb02002-07-25 05:38:23 +00002383 for(i = 0; i < encoder->protected_->channels; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002384 if(0 != encoder->private_->integer_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002385 free(encoder->private_->integer_signal_unaligned[i]);
2386 encoder->private_->integer_signal_unaligned[i] = 0;
2387 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002388#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002389 if(0 != encoder->private_->real_signal_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002390 free(encoder->private_->real_signal_unaligned[i]);
2391 encoder->private_->real_signal_unaligned[i] = 0;
2392 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002393#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002394 }
2395 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002396 if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002397 free(encoder->private_->integer_signal_mid_side_unaligned[i]);
2398 encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
2399 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002400#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalsonf1eff452002-07-31 07:05:33 +00002401 if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002402 free(encoder->private_->real_signal_mid_side_unaligned[i]);
2403 encoder->private_->real_signal_mid_side_unaligned[i] = 0;
2404 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002405#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002406 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002407#ifndef FLAC__INTEGER_ONLY_LIBRARY
2408 for(i = 0; i < encoder->protected_->num_apodizations; i++) {
2409 if(0 != encoder->private_->window_unaligned[i]) {
2410 free(encoder->private_->window_unaligned[i]);
2411 encoder->private_->window_unaligned[i] = 0;
2412 }
2413 }
2414 if(0 != encoder->private_->windowed_signal_unaligned) {
2415 free(encoder->private_->windowed_signal_unaligned);
2416 encoder->private_->windowed_signal_unaligned = 0;
2417 }
2418#endif
Josh Coalson639aeb02002-07-25 05:38:23 +00002419 for(channel = 0; channel < encoder->protected_->channels; channel++) {
2420 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002421 if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002422 free(encoder->private_->residual_workspace_unaligned[channel][i]);
2423 encoder->private_->residual_workspace_unaligned[channel][i] = 0;
2424 }
2425 }
2426 }
2427 for(channel = 0; channel < 2; channel++) {
2428 for(i = 0; i < 2; i++) {
Josh Coalsonf1eff452002-07-31 07:05:33 +00002429 if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002430 free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]);
2431 encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0;
2432 }
2433 }
2434 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002435 if(0 != encoder->private_->abs_residual_partition_sums_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002436 free(encoder->private_->abs_residual_partition_sums_unaligned);
2437 encoder->private_->abs_residual_partition_sums_unaligned = 0;
2438 }
Josh Coalsonf1eff452002-07-31 07:05:33 +00002439 if(0 != encoder->private_->raw_bits_per_partition_unaligned) {
Josh Coalson639aeb02002-07-25 05:38:23 +00002440 free(encoder->private_->raw_bits_per_partition_unaligned);
2441 encoder->private_->raw_bits_per_partition_unaligned = 0;
2442 }
Josh Coalsond86e03b2002-08-03 21:56:15 +00002443 if(encoder->protected_->verify) {
2444 for(i = 0; i < encoder->protected_->channels; i++) {
2445 if(0 != encoder->private_->verify.input_fifo.data[i]) {
2446 free(encoder->private_->verify.input_fifo.data[i]);
2447 encoder->private_->verify.input_fifo.data[i] = 0;
2448 }
2449 }
2450 }
Josh Coalson423f8042007-01-28 17:40:26 +00002451 FLAC__bitwriter_free(encoder->private_->frame);
Josh Coalson639aeb02002-07-25 05:38:23 +00002452}
2453
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002454FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, uint32_t new_blocksize)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002455{
Josh Coalson77e3f312001-06-23 03:03:24 +00002456 FLAC__bool ok;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002457 uint32_t i, channel;
Josh Coalson0a15c142001-06-13 17:59:57 +00002458
Josh Coalson85aaed82006-11-09 01:19:13 +00002459 FLAC__ASSERT(new_blocksize > 0);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002460 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2461 FLAC__ASSERT(encoder->private_->current_sample_number == 0);
Josh Coalson0a15c142001-06-13 17:59:57 +00002462
2463 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
Josh Coalson85aaed82006-11-09 01:19:13 +00002464 if(new_blocksize <= encoder->private_->input_capacity)
Josh Coalson0a15c142001-06-13 17:59:57 +00002465 return true;
2466
2467 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +00002468
Erik de Castro Lopo5e5ee272013-09-15 09:51:28 +10002469 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx() and ..._intrin_sse2()
2470 * require that the input arrays (in our case the integer signals)
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002471 * have a buffer of up to 3 zeroes in front (at negative indices) for
Josh Coalson85aaed82006-11-09 01:19:13 +00002472 * alignment purposes; we use 4 in front to keep the data well-aligned.
Josh Coalsonc9c0d132002-10-04 05:29:05 +00002473 */
Josh Coalson8395d022001-07-12 21:25:22 +00002474
Josh Coalsonfa697a92001-08-16 20:07:29 +00002475 for(i = 0; ok && i < encoder->protected_->channels; i++) {
Josh Coalson49f2f162006-11-09 16:54:52 +00002476 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002477 memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
2478 encoder->private_->integer_signal[i] += 4;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002479#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson4a040f62007-04-04 00:59:28 +00002480#if 0 /* @@@ currently unused */
Josh Coalsonc549f0f2004-12-30 03:47:49 +00002481 if(encoder->protected_->max_lpc_order > 0)
Josh Coalson49f2f162006-11-09 16:54:52 +00002482 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
Josh Coalson5f2b46d2004-11-09 01:34:01 +00002483#endif
Josh Coalson4a040f62007-04-04 00:59:28 +00002484#endif
Josh Coalson85aaed82006-11-09 01:19:13 +00002485 }
2486 for(i = 0; ok && i < 2; i++) {
Josh Coalson49f2f162006-11-09 16:54:52 +00002487 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 Coalsonfa697a92001-08-16 20:07:29 +00002488 memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
2489 encoder->private_->integer_signal_mid_side[i] += 4;
Josh Coalson85aaed82006-11-09 01:19:13 +00002490#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson4a040f62007-04-04 00:59:28 +00002491#if 0 /* @@@ currently unused */
Josh Coalson85aaed82006-11-09 01:19:13 +00002492 if(encoder->protected_->max_lpc_order > 0)
Josh Coalson49f2f162006-11-09 16:54:52 +00002493 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 Coalson85aaed82006-11-09 01:19:13 +00002494#endif
Josh Coalson4a040f62007-04-04 00:59:28 +00002495#endif
Josh Coalson0a15c142001-06-13 17:59:57 +00002496 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002497#ifndef FLAC__INTEGER_ONLY_LIBRARY
2498 if(ok && encoder->protected_->max_lpc_order > 0) {
2499 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
Josh Coalson85aaed82006-11-09 01:19:13 +00002500 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2501 ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002502 }
2503#endif
Josh Coalsonfa697a92001-08-16 20:07:29 +00002504 for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00002505 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002506 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
Josh Coalson0a15c142001-06-13 17:59:57 +00002507 }
2508 }
2509 for(channel = 0; ok && channel < 2; channel++) {
2510 for(i = 0; ok && i < 2; i++) {
Josh Coalson85aaed82006-11-09 01:19:13 +00002511 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 Coalson0a15c142001-06-13 17:59:57 +00002512 }
2513 }
Josh Coalson469ba742007-02-03 02:52:32 +00002514 /* the *2 is an approximation to the series 1 + 1/2 + 1/4 + ... that sums tree occupies in a flat array */
2515 /*@@@ new_blocksize*2 is too pessimistic, but to fix, we need smarter logic because a smaller new_blocksize can actually increase the # of partitions; would require moving this out into a separate function, then checking its capacity against the need of the current blocksize&min/max_partition_order (and maybe predictor order) */
Josh Coalson1109e7f2007-01-24 04:26:15 +00002516 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 Coalsonfa697a92001-08-16 20:07:29 +00002517 if(encoder->protected_->do_escape_coding)
Josh Coalson85aaed82006-11-09 01:19:13 +00002518 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 Coalson0a15c142001-06-13 17:59:57 +00002519
Josh Coalson85aaed82006-11-09 01:19:13 +00002520 /* now adjust the windows if the blocksize has changed */
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002521#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson85aaed82006-11-09 01:19:13 +00002522 if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002523 for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2524 switch(encoder->protected_->apodizations[i].type) {
2525 case FLAC__APODIZATION_BARTLETT:
Josh Coalson85aaed82006-11-09 01:19:13 +00002526 FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002527 break;
2528 case FLAC__APODIZATION_BARTLETT_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002529 FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002530 break;
2531 case FLAC__APODIZATION_BLACKMAN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002532 FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002533 break;
2534 case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002535 FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002536 break;
2537 case FLAC__APODIZATION_CONNES:
Josh Coalson85aaed82006-11-09 01:19:13 +00002538 FLAC__window_connes(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002539 break;
2540 case FLAC__APODIZATION_FLATTOP:
Josh Coalson85aaed82006-11-09 01:19:13 +00002541 FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002542 break;
2543 case FLAC__APODIZATION_GAUSS:
Josh Coalson85aaed82006-11-09 01:19:13 +00002544 FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002545 break;
2546 case FLAC__APODIZATION_HAMMING:
Josh Coalson85aaed82006-11-09 01:19:13 +00002547 FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002548 break;
2549 case FLAC__APODIZATION_HANN:
Josh Coalson85aaed82006-11-09 01:19:13 +00002550 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002551 break;
2552 case FLAC__APODIZATION_KAISER_BESSEL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002553 FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002554 break;
2555 case FLAC__APODIZATION_NUTTALL:
Josh Coalson85aaed82006-11-09 01:19:13 +00002556 FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002557 break;
2558 case FLAC__APODIZATION_RECTANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002559 FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002560 break;
2561 case FLAC__APODIZATION_TRIANGLE:
Josh Coalson85aaed82006-11-09 01:19:13 +00002562 FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002563 break;
2564 case FLAC__APODIZATION_TUKEY:
Josh Coalson85aaed82006-11-09 01:19:13 +00002565 FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002566 break;
Martijn van Beurden29a28332014-08-10 10:59:29 +02002567 case FLAC__APODIZATION_PARTIAL_TUKEY:
2568 FLAC__window_partial_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.multiple_tukey.p, encoder->protected_->apodizations[i].parameters.multiple_tukey.start, encoder->protected_->apodizations[i].parameters.multiple_tukey.end);
2569 break;
2570 case FLAC__APODIZATION_PUNCHOUT_TUKEY:
2571 FLAC__window_punchout_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.multiple_tukey.p, encoder->protected_->apodizations[i].parameters.multiple_tukey.start, encoder->protected_->apodizations[i].parameters.multiple_tukey.end);
2572 break;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002573 case FLAC__APODIZATION_WELCH:
Josh Coalson85aaed82006-11-09 01:19:13 +00002574 FLAC__window_welch(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002575 break;
2576 default:
2577 FLAC__ASSERT(0);
2578 /* double protection */
Josh Coalson85aaed82006-11-09 01:19:13 +00002579 FLAC__window_hann(encoder->private_->window[i], new_blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00002580 break;
2581 }
2582 }
2583 }
2584#endif
2585
Josh Coalson85aaed82006-11-09 01:19:13 +00002586 if(ok)
2587 encoder->private_->input_capacity = new_blocksize;
2588 else
2589 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2590
Josh Coalson0a15c142001-06-13 17:59:57 +00002591 return ok;
2592}
2593
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002594FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, uint32_t samples, FLAC__bool is_last_block)
Josh Coalson5c491a12002-08-01 06:39:40 +00002595{
2596 const FLAC__byte *buffer;
Josh Coalson352feb52006-10-15 17:08:52 +00002597 size_t bytes;
Josh Coalson5c491a12002-08-01 06:39:40 +00002598
Josh Coalson423f8042007-01-28 17:40:26 +00002599 FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
Josh Coalson5c491a12002-08-01 06:39:40 +00002600
Josh Coalson423f8042007-01-28 17:40:26 +00002601 if(!FLAC__bitwriter_get_buffer(encoder->private_->frame, &buffer, &bytes)) {
2602 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2603 return false;
2604 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002605
Josh Coalsond86e03b2002-08-03 21:56:15 +00002606 if(encoder->protected_->verify) {
2607 encoder->private_->verify.output.data = buffer;
2608 encoder->private_->verify.output.bytes = bytes;
2609 if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) {
2610 encoder->private_->verify.needs_magic_hack = true;
2611 }
2612 else {
Neelkamal Semwal7803a782020-12-18 22:28:36 +05302613 if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)
2614 || (!is_last_block
2615 && (FLAC__stream_encoder_get_verify_decoder_state(encoder) == FLAC__STREAM_DECODER_END_OF_STREAM))) {
Josh Coalson423f8042007-01-28 17:40:26 +00002616 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2617 FLAC__bitwriter_clear(encoder->private_->frame);
Josh Coalsond86e03b2002-08-03 21:56:15 +00002618 if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
2619 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
2620 return false;
2621 }
2622 }
2623 }
2624
Josh Coalson49f2f162006-11-09 16:54:52 +00002625 if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
Josh Coalson423f8042007-01-28 17:40:26 +00002626 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2627 FLAC__bitwriter_clear(encoder->private_->frame);
Josh Coalson6b21f662006-09-13 01:42:27 +00002628 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
Josh Coalson5c491a12002-08-01 06:39:40 +00002629 return false;
Josh Coalsond86e03b2002-08-03 21:56:15 +00002630 }
Josh Coalson5c491a12002-08-01 06:39:40 +00002631
Josh Coalson423f8042007-01-28 17:40:26 +00002632 FLAC__bitwriter_release_buffer(encoder->private_->frame);
2633 FLAC__bitwriter_clear(encoder->private_->frame);
Josh Coalson5c491a12002-08-01 06:39:40 +00002634
Josh Coalsond86e03b2002-08-03 21:56:15 +00002635 if(samples > 0) {
Cristian Rodríguezf0296252012-04-05 19:39:37 -03002636 encoder->private_->streaminfo.data.stream_info.min_framesize = flac_min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2637 encoder->private_->streaminfo.data.stream_info.max_framesize = flac_max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00002638 }
2639
Josh Coalson5c491a12002-08-01 06:39:40 +00002640 return true;
2641}
2642
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002643FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, FLAC__bool is_last_block)
Josh Coalson6b21f662006-09-13 01:42:27 +00002644{
2645 FLAC__StreamEncoderWriteStatus status;
2646 FLAC__uint64 output_position = 0;
2647
Erik de Castro Lopob9574fe2013-04-07 13:44:33 +10002648#if FLAC__HAS_OGG == 0
2649 (void)is_last_block;
2650#endif
2651
Josh Coalson6b21f662006-09-13 01:42:27 +00002652 /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2653 if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2654 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2655 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2656 }
2657
2658 /*
2659 * Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2660 */
2661 if(samples == 0) {
2662 FLAC__MetadataType type = (buffer[0] & 0x7f);
2663 if(type == FLAC__METADATA_TYPE_STREAMINFO)
2664 encoder->protected_->streaminfo_offset = output_position;
2665 else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2666 encoder->protected_->seektable_offset = output_position;
2667 }
2668
2669 /*
2670 * Mark the current seek point if hit (if audio_offset == 0 that
2671 * means we're still writing metadata and haven't hit the first
2672 * frame yet)
2673 */
2674 if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002675 const uint32_t blocksize = FLAC__stream_encoder_get_blocksize(encoder);
Josh Coalson6b21f662006-09-13 01:42:27 +00002676 const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2677 const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2678 FLAC__uint64 test_sample;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002679 uint32_t i;
Josh Coalson6b21f662006-09-13 01:42:27 +00002680 for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2681 test_sample = encoder->private_->seek_table->points[i].sample_number;
2682 if(test_sample > frame_last_sample) {
2683 break;
2684 }
2685 else if(test_sample >= frame_first_sample) {
2686 encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2687 encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2688 encoder->private_->seek_table->points[i].frame_samples = blocksize;
2689 encoder->private_->first_seekpoint_to_check++;
2690 /* DO NOT: "break;" and here's why:
2691 * The seektable template may contain more than one target
2692 * sample for any given frame; we will keep looping, generating
2693 * duplicate seekpoints for them, and we'll clean it up later,
2694 * just before writing the seektable back to the metadata.
2695 */
2696 }
2697 else {
2698 encoder->private_->first_seekpoint_to_check++;
2699 }
2700 }
2701 }
2702
Josh Coalson8da98c82006-10-15 04:24:05 +00002703#if FLAC__HAS_OGG
2704 if(encoder->private_->is_ogg) {
2705 status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2706 &encoder->protected_->ogg_encoder_aspect,
Josh Coalson8da98c82006-10-15 04:24:05 +00002707 buffer,
2708 bytes,
2709 samples,
2710 encoder->private_->current_frame_number,
Josh Coalson49f2f162006-11-09 16:54:52 +00002711 is_last_block,
Josh Coalson8da98c82006-10-15 04:24:05 +00002712 (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2713 encoder,
2714 encoder->private_->client_data
2715 );
2716 }
2717 else
2718#endif
Josh Coalson6b21f662006-09-13 01:42:27 +00002719 status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2720
2721 if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2722 encoder->private_->bytes_written += bytes;
2723 encoder->private_->samples_written += samples;
2724 /* we keep a high watermark on the number of frames written because
2725 * when the encoder goes back to write metadata, 'current_frame'
2726 * will drop back to 0.
2727 */
Cristian Rodríguezf0296252012-04-05 19:39:37 -03002728 encoder->private_->frames_written = flac_max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
Josh Coalson6b21f662006-09-13 01:42:27 +00002729 }
2730 else
2731 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2732
2733 return status;
2734}
2735
2736/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2737void update_metadata_(const FLAC__StreamEncoder *encoder)
2738{
Cristian Rodríguezf0296252012-04-05 19:39:37 -03002739 FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
Josh Coalson6b21f662006-09-13 01:42:27 +00002740 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2741 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002742 const uint32_t min_framesize = metadata->data.stream_info.min_framesize;
2743 const uint32_t max_framesize = metadata->data.stream_info.max_framesize;
2744 const uint32_t bps = metadata->data.stream_info.bits_per_sample;
Josh Coalson6b21f662006-09-13 01:42:27 +00002745 FLAC__StreamEncoderSeekStatus seek_status;
2746
2747 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2748
2749 /* All this is based on intimate knowledge of the stream header
2750 * layout, but a change to the header format that would break this
2751 * would also break all streams encoded in the previous format.
2752 */
2753
2754 /*
2755 * Write MD5 signature
2756 */
2757 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002758 const uint32_t md5_offset =
Josh Coalson6b21f662006-09-13 01:42:27 +00002759 FLAC__STREAM_METADATA_HEADER_LENGTH +
2760 (
2761 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2762 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2763 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2764 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2765 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2766 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2767 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2768 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2769 ) / 8;
2770
2771 if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2772 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2773 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2774 return;
2775 }
2776 if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2777 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2778 return;
2779 }
2780 }
2781
2782 /*
2783 * Write total samples
2784 */
2785 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002786 const uint32_t total_samples_byte_offset =
Josh Coalson6b21f662006-09-13 01:42:27 +00002787 FLAC__STREAM_METADATA_HEADER_LENGTH +
2788 (
2789 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2790 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2791 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2792 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2793 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2794 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2795 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2796 - 4
2797 ) / 8;
2798
2799 b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2800 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2801 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2802 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2803 b[4] = (FLAC__byte)(samples & 0xFF);
2804 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) {
2805 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2806 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2807 return;
2808 }
2809 if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2810 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2811 return;
2812 }
2813 }
2814
2815 /*
2816 * Write min/max framesize
2817 */
2818 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002819 const uint32_t min_framesize_offset =
Josh Coalson6b21f662006-09-13 01:42:27 +00002820 FLAC__STREAM_METADATA_HEADER_LENGTH +
2821 (
2822 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2823 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2824 ) / 8;
2825
2826 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2827 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2828 b[2] = (FLAC__byte)(min_framesize & 0xFF);
2829 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2830 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2831 b[5] = (FLAC__byte)(max_framesize & 0xFF);
2832 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) {
2833 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2834 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2835 return;
2836 }
2837 if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2838 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2839 return;
2840 }
2841 }
2842
2843 /*
2844 * Write seektable
2845 */
2846 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002847 uint32_t i;
Josh Coalson6b21f662006-09-13 01:42:27 +00002848
2849 FLAC__format_seektable_sort(encoder->private_->seek_table);
2850
2851 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2852
2853 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) {
2854 if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2855 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2856 return;
2857 }
2858
2859 for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2860 FLAC__uint64 xx;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002861 uint32_t x;
Josh Coalson6b21f662006-09-13 01:42:27 +00002862 xx = encoder->private_->seek_table->points[i].sample_number;
2863 b[7] = (FLAC__byte)xx; xx >>= 8;
2864 b[6] = (FLAC__byte)xx; xx >>= 8;
2865 b[5] = (FLAC__byte)xx; xx >>= 8;
2866 b[4] = (FLAC__byte)xx; xx >>= 8;
2867 b[3] = (FLAC__byte)xx; xx >>= 8;
2868 b[2] = (FLAC__byte)xx; xx >>= 8;
2869 b[1] = (FLAC__byte)xx; xx >>= 8;
2870 b[0] = (FLAC__byte)xx; xx >>= 8;
2871 xx = encoder->private_->seek_table->points[i].stream_offset;
2872 b[15] = (FLAC__byte)xx; xx >>= 8;
2873 b[14] = (FLAC__byte)xx; xx >>= 8;
2874 b[13] = (FLAC__byte)xx; xx >>= 8;
2875 b[12] = (FLAC__byte)xx; xx >>= 8;
2876 b[11] = (FLAC__byte)xx; xx >>= 8;
2877 b[10] = (FLAC__byte)xx; xx >>= 8;
2878 b[9] = (FLAC__byte)xx; xx >>= 8;
2879 b[8] = (FLAC__byte)xx; xx >>= 8;
2880 x = encoder->private_->seek_table->points[i].frame_samples;
2881 b[17] = (FLAC__byte)x; x >>= 8;
2882 b[16] = (FLAC__byte)x; x >>= 8;
2883 if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2884 encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2885 return;
2886 }
2887 }
2888 }
2889}
2890
Josh Coalson15b8eb82006-10-15 05:15:55 +00002891#if FLAC__HAS_OGG
Josh Coalson8da98c82006-10-15 04:24:05 +00002892/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2893void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2894{
Josh Coalsonc986d132006-11-15 08:53:32 +00002895 /* the # of bytes in the 1st packet that precede the STREAMINFO */
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002896 static const uint32_t FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
Josh Coalsonc986d132006-11-15 08:53:32 +00002897 FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
2898 FLAC__OGG_MAPPING_MAGIC_LENGTH +
2899 FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
2900 FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
2901 FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
2902 FLAC__STREAM_SYNC_LENGTH
2903 ;
Cristian Rodríguezf0296252012-04-05 19:39:37 -03002904 FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
Josh Coalson8da98c82006-10-15 04:24:05 +00002905 const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2906 const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002907 const uint32_t min_framesize = metadata->data.stream_info.min_framesize;
2908 const uint32_t max_framesize = metadata->data.stream_info.max_framesize;
Josh Coalson8da98c82006-10-15 04:24:05 +00002909 ogg_page page;
2910
2911 FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
Josh Coalsoncf1422e2006-11-16 01:32:25 +00002912 FLAC__ASSERT(0 != encoder->private_->seek_callback);
2913
2914 /* Pre-check that client supports seeking, since we don't want the
2915 * ogg_helper code to ever have to deal with this condition.
2916 */
2917 if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
2918 return;
Josh Coalson8da98c82006-10-15 04:24:05 +00002919
2920 /* All this is based on intimate knowledge of the stream header
2921 * layout, but a change to the header format that would break this
2922 * would also break all streams encoded in the previous format.
2923 */
2924
2925 /**
2926 ** Write STREAMINFO stats
2927 **/
2928 simple_ogg_page__init(&page);
2929 if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2930 simple_ogg_page__clear(&page);
2931 return; /* state already set */
2932 }
2933
2934 /*
2935 * Write MD5 signature
2936 */
2937 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002938 const uint32_t md5_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00002939 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00002940 FLAC__STREAM_METADATA_HEADER_LENGTH +
2941 (
2942 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2943 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2944 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2945 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2946 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2947 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2948 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2949 FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2950 ) / 8;
2951
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002952 if(md5_offset + 16 > (uint32_t)page.body_len) {
Josh Coalson8da98c82006-10-15 04:24:05 +00002953 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2954 simple_ogg_page__clear(&page);
2955 return;
2956 }
2957 memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2958 }
2959
2960 /*
2961 * Write total samples
2962 */
2963 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002964 const uint32_t total_samples_byte_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00002965 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00002966 FLAC__STREAM_METADATA_HEADER_LENGTH +
2967 (
2968 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2969 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2970 FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2971 FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2972 FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2973 FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2974 FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2975 - 4
2976 ) / 8;
2977
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002978 if(total_samples_byte_offset + 5 > (uint32_t)page.body_len) {
Josh Coalson8da98c82006-10-15 04:24:05 +00002979 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2980 simple_ogg_page__clear(&page);
2981 return;
2982 }
2983 b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2984 b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2985 b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2986 b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2987 b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2988 b[4] = (FLAC__byte)(samples & 0xFF);
2989 memcpy(page.body + total_samples_byte_offset, b, 5);
2990 }
2991
2992 /*
2993 * Write min/max framesize
2994 */
2995 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11002996 const uint32_t min_framesize_offset =
Josh Coalsonc986d132006-11-15 08:53:32 +00002997 FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
Josh Coalson8da98c82006-10-15 04:24:05 +00002998 FLAC__STREAM_METADATA_HEADER_LENGTH +
2999 (
3000 FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
3001 FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
3002 ) / 8;
3003
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003004 if(min_framesize_offset + 6 > (uint32_t)page.body_len) {
Josh Coalson8da98c82006-10-15 04:24:05 +00003005 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3006 simple_ogg_page__clear(&page);
3007 return;
3008 }
3009 b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
3010 b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
3011 b[2] = (FLAC__byte)(min_framesize & 0xFF);
3012 b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
3013 b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
3014 b[5] = (FLAC__byte)(max_framesize & 0xFF);
3015 memcpy(page.body + min_framesize_offset, b, 6);
3016 }
3017 if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3018 simple_ogg_page__clear(&page);
3019 return; /* state already set */
3020 }
3021 simple_ogg_page__clear(&page);
3022
3023 /*
3024 * Write seektable
3025 */
3026 if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003027 uint32_t i;
Josh Coalson8da98c82006-10-15 04:24:05 +00003028 FLAC__byte *p;
3029
3030 FLAC__format_seektable_sort(encoder->private_->seek_table);
3031
3032 FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
3033
3034 simple_ogg_page__init(&page);
3035 if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
3036 simple_ogg_page__clear(&page);
3037 return; /* state already set */
3038 }
3039
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003040 if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (uint32_t)page.body_len) {
Josh Coalson8da98c82006-10-15 04:24:05 +00003041 encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3042 simple_ogg_page__clear(&page);
3043 return;
3044 }
3045
3046 for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
3047 FLAC__uint64 xx;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003048 uint32_t x;
Josh Coalson8da98c82006-10-15 04:24:05 +00003049 xx = encoder->private_->seek_table->points[i].sample_number;
3050 b[7] = (FLAC__byte)xx; xx >>= 8;
3051 b[6] = (FLAC__byte)xx; xx >>= 8;
3052 b[5] = (FLAC__byte)xx; xx >>= 8;
3053 b[4] = (FLAC__byte)xx; xx >>= 8;
3054 b[3] = (FLAC__byte)xx; xx >>= 8;
3055 b[2] = (FLAC__byte)xx; xx >>= 8;
3056 b[1] = (FLAC__byte)xx; xx >>= 8;
3057 b[0] = (FLAC__byte)xx; xx >>= 8;
3058 xx = encoder->private_->seek_table->points[i].stream_offset;
3059 b[15] = (FLAC__byte)xx; xx >>= 8;
3060 b[14] = (FLAC__byte)xx; xx >>= 8;
3061 b[13] = (FLAC__byte)xx; xx >>= 8;
3062 b[12] = (FLAC__byte)xx; xx >>= 8;
3063 b[11] = (FLAC__byte)xx; xx >>= 8;
3064 b[10] = (FLAC__byte)xx; xx >>= 8;
3065 b[9] = (FLAC__byte)xx; xx >>= 8;
3066 b[8] = (FLAC__byte)xx; xx >>= 8;
3067 x = encoder->private_->seek_table->points[i].frame_samples;
3068 b[17] = (FLAC__byte)x; x >>= 8;
3069 b[16] = (FLAC__byte)x; x >>= 8;
Josh Coalson8da98c82006-10-15 04:24:05 +00003070 memcpy(p, b, 18);
3071 }
3072
3073 if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3074 simple_ogg_page__clear(&page);
3075 return; /* state already set */
3076 }
3077 simple_ogg_page__clear(&page);
3078 }
3079}
Josh Coalson15b8eb82006-10-15 05:15:55 +00003080#endif
Josh Coalson8da98c82006-10-15 04:24:05 +00003081
Josh Coalson49f2f162006-11-09 16:54:52 +00003082FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
Josh Coalson0a15c142001-06-13 17:59:57 +00003083{
Josh Coalson423f8042007-01-28 17:40:26 +00003084 FLAC__uint16 crc;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003085 FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003086
3087 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00003088 * Accumulate raw signal to the MD5 signature
3089 */
Josh Coalson00da5ae2007-06-14 05:19:46 +00003090 if(encoder->protected_->do_md5 && !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 Coalsonfa697a92001-08-16 20:07:29 +00003091 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +00003092 return false;
3093 }
3094
3095 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003096 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003097 */
Josh Coalson85aaed82006-11-09 01:19:13 +00003098 if(!process_subframes_(encoder, is_fractional_block)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003099 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003100 return false;
3101 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003102
3103 /*
3104 * Zero-pad the frame to a byte_boundary
3105 */
Josh Coalson423f8042007-01-28 17:40:26 +00003106 if(!FLAC__bitwriter_zero_pad_to_byte_boundary(encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003107 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003108 return false;
3109 }
3110
3111 /*
Josh Coalson215af572001-03-27 01:15:58 +00003112 * CRC-16 the whole thing
3113 */
Josh Coalson423f8042007-01-28 17:40:26 +00003114 FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
3115 if(
3116 !FLAC__bitwriter_get_write_crc16(encoder->private_->frame, &crc) ||
3117 !FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, crc, FLAC__FRAME_FOOTER_CRC_LEN)
3118 ) {
3119 encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
3120 return false;
3121 }
Josh Coalson215af572001-03-27 01:15:58 +00003122
3123 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003124 * Write it
3125 */
Josh Coalson49f2f162006-11-09 16:54:52 +00003126 if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) {
Josh Coalsond86e03b2002-08-03 21:56:15 +00003127 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003128 return false;
3129 }
3130
3131 /*
3132 * Get ready for the next frame
3133 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003134 encoder->private_->current_sample_number = 0;
3135 encoder->private_->current_frame_number++;
Josh Coalson6b21f662006-09-13 01:42:27 +00003136 encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003137
3138 return true;
3139}
3140
Josh Coalson85aaed82006-11-09 01:19:13 +00003141FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003142{
3143 FLAC__FrameHeader frame_header;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003144 uint32_t channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order;
Josh Coalson1109e7f2007-01-24 04:26:15 +00003145 FLAC__bool do_independent, do_mid_side;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003146
3147 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00003148 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00003149 */
Josh Coalson85aaed82006-11-09 01:19:13 +00003150 if(is_fractional_block) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003151 max_partition_order = 0;
3152 }
3153 else {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003154 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
Cristian Rodríguezf0296252012-04-05 19:39:37 -03003155 max_partition_order = flac_min(max_partition_order, encoder->protected_->max_residual_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003156 }
Cristian Rodríguezf0296252012-04-05 19:39:37 -03003157 min_partition_order = flac_min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003158
3159 /*
3160 * Setup the frame
3161 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003162 frame_header.blocksize = encoder->protected_->blocksize;
3163 frame_header.sample_rate = encoder->protected_->sample_rate;
3164 frame_header.channels = encoder->protected_->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003165 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003166 frame_header.bits_per_sample = encoder->protected_->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003167 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalsonfa697a92001-08-16 20:07:29 +00003168 frame_header.number.frame_number = encoder->private_->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003169
3170 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003171 * Figure out what channel assignments to try
3172 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003173 if(encoder->protected_->do_mid_side_stereo) {
3174 if(encoder->protected_->loose_mid_side_stereo) {
3175 if(encoder->private_->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003176 do_independent = true;
3177 do_mid_side = true;
3178 }
3179 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003180 do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003181 do_mid_side = !do_independent;
3182 }
3183 }
3184 else {
3185 do_independent = true;
3186 do_mid_side = true;
3187 }
3188 }
3189 else {
3190 do_independent = true;
3191 do_mid_side = false;
3192 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003193
Josh Coalson1b689822001-05-31 20:11:02 +00003194 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003195
3196 /*
Josh Coalson82b73242001-03-28 22:17:05 +00003197 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00003198 */
3199 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003200 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003201 uint32_t w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize);
Erik de Castro Lopod91eb4a2015-09-26 08:17:13 +10003202 if (w > encoder->protected_->bits_per_sample) {
3203 w = encoder->protected_->bits_per_sample;
3204 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00003205 encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w;
3206 encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00003207 }
Josh Coalson859bc542001-03-27 22:22:27 +00003208 }
3209 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003210 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00003211 for(channel = 0; channel < 2; channel++) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003212 uint32_t w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize);
Erik de Castro Lopod91eb4a2015-09-26 08:17:13 +10003213 if (w > encoder->protected_->bits_per_sample) {
3214 w = encoder->protected_->bits_per_sample;
3215 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00003216 encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w;
3217 encoder->private_->subframe_bps_mid_side[channel] = encoder->protected_->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00003218 }
Josh Coalson859bc542001-03-27 22:22:27 +00003219 }
3220
3221 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00003222 * First do a normal encoding pass of each independent channel
3223 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003224 if(do_independent) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003225 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003226 if(!
3227 process_subframe_(
3228 encoder,
3229 min_partition_order,
3230 max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003231 &frame_header,
3232 encoder->private_->subframe_bps[channel],
3233 encoder->private_->integer_signal[channel],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003234 encoder->private_->subframe_workspace_ptr[channel],
3235 encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
3236 encoder->private_->residual_workspace[channel],
3237 encoder->private_->best_subframe+channel,
3238 encoder->private_->best_subframe_bits+channel
3239 )
3240 )
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003241 return false;
3242 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003243 }
3244
3245 /*
3246 * Now do mid and side channels if requested
3247 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003248 if(do_mid_side) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003249 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003250
3251 for(channel = 0; channel < 2; channel++) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003252 if(!
3253 process_subframe_(
3254 encoder,
3255 min_partition_order,
3256 max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003257 &frame_header,
3258 encoder->private_->subframe_bps_mid_side[channel],
3259 encoder->private_->integer_signal_mid_side[channel],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003260 encoder->private_->subframe_workspace_ptr_mid_side[channel],
3261 encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
3262 encoder->private_->residual_workspace_mid_side[channel],
3263 encoder->private_->best_subframe_mid_side+channel,
3264 encoder->private_->best_subframe_bits_mid_side+channel
3265 )
3266 )
Josh Coalson94e02cd2001-01-25 10:41:06 +00003267 return false;
3268 }
3269 }
3270
3271 /*
3272 * Compose the frame bitbuffer
3273 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003274 if(do_mid_side) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003275 uint32_t left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalson82b73242001-03-28 22:17:05 +00003276 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003277 FLAC__ChannelAssignment channel_assignment;
3278
Josh Coalsonfa697a92001-08-16 20:07:29 +00003279 FLAC__ASSERT(encoder->protected_->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003280
Josh Coalsonfa697a92001-08-16 20:07:29 +00003281 if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) {
3282 channel_assignment = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003283 }
3284 else {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003285 uint32_t bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
3286 uint32_t min_bits;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003287 int ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003288
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003289 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT == 0);
3290 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE == 1);
3291 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE == 2);
3292 FLAC__ASSERT(FLAC__CHANNEL_ASSIGNMENT_MID_SIDE == 3);
Josh Coalson1b689822001-05-31 20:11:02 +00003293 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003294
3295 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003296 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1];
3297 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1];
3298 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1];
3299 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private_->best_subframe_bits_mid_side[0] + encoder->private_->best_subframe_bits_mid_side[1];
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003300
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003301 channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
3302 min_bits = bits[channel_assignment];
3303 for(ca = 1; ca <= 3; ca++) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003304 if(bits[ca] < min_bits) {
3305 min_bits = bits[ca];
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003306 channel_assignment = (FLAC__ChannelAssignment)ca;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003307 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003308 }
3309 }
3310
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003311 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003312
Josh Coalsond0edb972006-10-07 06:50:08 +00003313 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003314 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003315 return false;
3316 }
3317
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003318 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003319 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003320 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3321 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003322 break;
3323 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003324 left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]];
3325 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003326 break;
3327 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003328 left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
3329 right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003330 break;
3331 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003332 left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]];
3333 right_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00003334 break;
3335 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003336 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003337 }
Josh Coalson82b73242001-03-28 22:17:05 +00003338
3339 switch(channel_assignment) {
3340 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003341 left_bps = encoder->private_->subframe_bps [0];
3342 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003343 break;
3344 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003345 left_bps = encoder->private_->subframe_bps [0];
3346 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003347 break;
3348 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003349 left_bps = encoder->private_->subframe_bps_mid_side[1];
3350 right_bps = encoder->private_->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00003351 break;
3352 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00003353 left_bps = encoder->private_->subframe_bps_mid_side[0];
3354 right_bps = encoder->private_->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00003355 break;
3356 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003357 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00003358 }
3359
3360 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003361 if(!add_subframe_(encoder, frame_header.blocksize, left_bps , left_subframe , encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003362 return false;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003363 if(!add_subframe_(encoder, frame_header.blocksize, right_bps, right_subframe, encoder->private_->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00003364 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003365 }
3366 else {
Josh Coalsond0edb972006-10-07 06:50:08 +00003367 if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00003368 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003369 return false;
3370 }
3371
Josh Coalsonfa697a92001-08-16 20:07:29 +00003372 for(channel = 0; channel < encoder->protected_->channels; channel++) {
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003373 if(!add_subframe_(encoder, frame_header.blocksize, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003374 /* the above function sets the state for us in case of an error */
3375 return false;
3376 }
3377 }
3378 }
3379
Josh Coalsonfa697a92001-08-16 20:07:29 +00003380 if(encoder->protected_->loose_mid_side_stereo) {
3381 encoder->private_->loose_mid_side_stereo_frame_count++;
3382 if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames)
3383 encoder->private_->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003384 }
3385
Josh Coalsonfa697a92001-08-16 20:07:29 +00003386 encoder->private_->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00003387
Josh Coalson94e02cd2001-01-25 10:41:06 +00003388 return true;
3389}
3390
Josh Coalson6fe72f72002-08-20 04:01:59 +00003391FLAC__bool process_subframe_(
3392 FLAC__StreamEncoder *encoder,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003393 uint32_t min_partition_order,
3394 uint32_t max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003395 const FLAC__FrameHeader *frame_header,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003396 uint32_t subframe_bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003397 const FLAC__int32 integer_signal[],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003398 FLAC__Subframe *subframe[2],
3399 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
3400 FLAC__int32 *residual[2],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003401 uint32_t *best_subframe,
3402 uint32_t *best_bits
Josh Coalson6fe72f72002-08-20 04:01:59 +00003403)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003404{
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003405#ifndef FLAC__INTEGER_ONLY_LIBRARY
Thomas Zandere5498e82016-02-08 10:43:37 +01003406 float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003407#else
3408 FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
3409#endif
3410#ifndef FLAC__INTEGER_ONLY_LIBRARY
Thomas Zandere5498e82016-02-08 10:43:37 +01003411 double lpc_residual_bits_per_sample;
Erik de Castro Lopo5e5ee272013-09-15 09:51:28 +10003412 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 and x86 intrinsic routines need all the space */
Thomas Zandere5498e82016-02-08 10:43:37 +01003413 double lpc_error[FLAC__MAX_LPC_ORDER];
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003414 uint32_t min_lpc_order, max_lpc_order, lpc_order;
3415 uint32_t min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003416#endif
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003417 uint32_t min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
3418 uint32_t rice_parameter;
3419 uint32_t _candidate_bits, _best_bits;
3420 uint32_t _best_subframe;
Josh Coalson05f710f2007-07-31 00:51:26 +00003421 /* only use RICE2 partitions if stream bps > 16 */
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003422 const uint32_t rice_parameter_limit = FLAC__stream_encoder_get_bits_per_sample(encoder) > 16? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003423
Josh Coalson423f8042007-01-28 17:40:26 +00003424 FLAC__ASSERT(frame_header->blocksize > 0);
3425
Josh Coalson94e02cd2001-01-25 10:41:06 +00003426 /* verbatim subframe is the baseline against which we measure other compressed subframes */
3427 _best_subframe = 0;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003428 if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER)
3429 _best_bits = UINT_MAX;
3430 else
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003431 _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003432
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003433 if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003434 uint32_t signal_is_constant = false;
Erik de Castro Lopoeb2af522015-11-25 05:42:40 +11003435 if(subframe_bps + 4 + FLAC__bitmath_ilog2((frame_header->blocksize-FLAC__MAX_FIXED_ORDER)|1) <= 32)
3436 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);
3437 else
3438 guess_fixed_order = encoder->private_->local_fixed_compute_best_predictor_wide(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003439 /* check for constant subframe */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003440 if(
3441 !encoder->private_->disable_constant_subframes &&
3442#ifndef FLAC__INTEGER_ONLY_LIBRARY
3443 fixed_residual_bits_per_sample[1] == 0.0
3444#else
3445 fixed_residual_bits_per_sample[1] == FLAC__FP_ZERO
3446#endif
3447 ) {
3448 /* the above means it's possible all samples are the same value; now double-check it: */
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003449 uint32_t i;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003450 signal_is_constant = true;
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003451 for(i = 1; i < frame_header->blocksize; i++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00003452 if(integer_signal[0] != integer_signal[i]) {
3453 signal_is_constant = false;
3454 break;
3455 }
3456 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003457 }
3458 if(signal_is_constant) {
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003459 _candidate_bits = evaluate_constant_subframe_(encoder, integer_signal[0], frame_header->blocksize, subframe_bps, subframe[!_best_subframe]);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003460 if(_candidate_bits < _best_bits) {
3461 _best_subframe = !_best_subframe;
3462 _best_bits = _candidate_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003463 }
3464 }
3465 else {
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003466 if(!encoder->private_->disable_fixed_subframes || (encoder->protected_->max_lpc_order == 0 && _best_bits == UINT_MAX)) {
3467 /* encode fixed */
3468 if(encoder->protected_->do_exhaustive_model_search) {
3469 min_fixed_order = 0;
3470 max_fixed_order = FLAC__MAX_FIXED_ORDER;
Josh Coalson8395d022001-07-12 21:25:22 +00003471 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003472 else {
3473 min_fixed_order = max_fixed_order = guess_fixed_order;
3474 }
Josh Coalson423f8042007-01-28 17:40:26 +00003475 if(max_fixed_order >= frame_header->blocksize)
3476 max_fixed_order = frame_header->blocksize - 1;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003477 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003478#ifndef FLAC__INTEGER_ONLY_LIBRARY
Thomas Zandere5498e82016-02-08 10:43:37 +01003479 if(fixed_residual_bits_per_sample[fixed_order] >= (float)subframe_bps)
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003480 continue; /* don't even try */
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003481 rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (uint32_t)(fixed_residual_bits_per_sample[fixed_order]+0.5) : 0; /* 0.5 is for rounding */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003482#else
3483 if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps)
3484 continue; /* don't even try */
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003485 rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > FLAC__FP_ZERO)? (uint32_t)FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]+FLAC__FP_ONE_HALF) : 0; /* 0.5 is for rounding */
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003486#endif
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003487 rice_parameter++; /* to account for the signed->uint32_t conversion during rice coding */
Josh Coalson05f710f2007-07-31 00:51:26 +00003488 if(rice_parameter >= rice_parameter_limit) {
David Seifertb3ece712017-01-15 10:37:02 +01003489#ifndef NDEBUG
Josh Coalson05f710f2007-07-31 00:51:26 +00003490 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, rice_parameter_limit - 1);
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003491#endif
Josh Coalson05f710f2007-07-31 00:51:26 +00003492 rice_parameter = rice_parameter_limit - 1;
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003493 }
3494 _candidate_bits =
3495 evaluate_fixed_subframe_(
3496 encoder,
3497 integer_signal,
3498 residual[!_best_subframe],
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003499 encoder->private_->abs_residual_partition_sums,
3500 encoder->private_->raw_bits_per_partition,
3501 frame_header->blocksize,
3502 subframe_bps,
3503 fixed_order,
3504 rice_parameter,
Josh Coalson05f710f2007-07-31 00:51:26 +00003505 rice_parameter_limit,
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003506 min_partition_order,
3507 max_partition_order,
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003508 encoder->protected_->do_escape_coding,
3509 encoder->protected_->rice_parameter_search_dist,
3510 subframe[!_best_subframe],
3511 partitioned_rice_contents[!_best_subframe]
3512 );
3513 if(_candidate_bits < _best_bits) {
3514 _best_subframe = !_best_subframe;
3515 _best_bits = _candidate_bits;
3516 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003517 }
3518 }
3519
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003520#ifndef FLAC__INTEGER_ONLY_LIBRARY
Josh Coalson94e02cd2001-01-25 10:41:06 +00003521 /* encode lpc */
Josh Coalsonfa697a92001-08-16 20:07:29 +00003522 if(encoder->protected_->max_lpc_order > 0) {
3523 if(encoder->protected_->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003524 max_lpc_order = frame_header->blocksize-1;
3525 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00003526 max_lpc_order = encoder->protected_->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003527 if(max_lpc_order > 0) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003528 uint32_t a;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003529 for (a = 0; a < encoder->protected_->num_apodizations; a++) {
Josh Coalson4a040f62007-04-04 00:59:28 +00003530 FLAC__lpc_window_data(integer_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003531 encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3532 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3533 if(autoc[0] != 0.0) {
Josh Coalson32b9bae2006-11-27 16:27:41 +00003534 FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error);
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003535 if(encoder->protected_->do_exhaustive_model_search) {
3536 min_lpc_order = 1;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003537 }
3538 else {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003539 const uint32_t guess_lpc_order =
Josh Coalsondf598452006-04-28 00:13:34 +00003540 FLAC__lpc_compute_best_order(
3541 lpc_error,
3542 max_lpc_order,
3543 frame_header->blocksize,
3544 subframe_bps + (
3545 encoder->protected_->do_qlp_coeff_prec_search?
3546 FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3547 encoder->protected_->qlp_coeff_precision
3548 )
3549 );
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003550 min_lpc_order = max_lpc_order = guess_lpc_order;
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003551 }
Josh Coalson423f8042007-01-28 17:40:26 +00003552 if(max_lpc_order >= frame_header->blocksize)
3553 max_lpc_order = frame_header->blocksize - 1;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003554 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3555 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
Thomas Zandere5498e82016-02-08 10:43:37 +01003556 if(lpc_residual_bits_per_sample >= (double)subframe_bps)
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003557 continue; /* don't even try */
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003558 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (uint32_t)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3559 rice_parameter++; /* to account for the signed->uint32_t conversion during rice coding */
Josh Coalson05f710f2007-07-31 00:51:26 +00003560 if(rice_parameter >= rice_parameter_limit) {
David Seifertb3ece712017-01-15 10:37:02 +01003561#ifndef NDEBUG
Josh Coalson05f710f2007-07-31 00:51:26 +00003562 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, rice_parameter_limit - 1);
Josh Coalsondf598452006-04-28 00:13:34 +00003563#endif
Josh Coalson05f710f2007-07-31 00:51:26 +00003564 rice_parameter = rice_parameter_limit - 1;
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003565 }
3566 if(encoder->protected_->do_qlp_coeff_prec_search) {
3567 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
lvqcl165d7b12015-10-12 00:59:08 +03003568 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps(+1bps for side channel) streams */
3569 if(subframe_bps <= 17) {
Martijn van Beurden27846702014-07-28 13:04:56 +02003570 max_qlp_coeff_precision = flac_min(32 - subframe_bps - FLAC__bitmath_ilog2(lpc_order), FLAC__MAX_QLP_COEFF_PRECISION);
Cristian Rodríguezf0296252012-04-05 19:39:37 -03003571 max_qlp_coeff_precision = flac_max(max_qlp_coeff_precision, min_qlp_coeff_precision);
Josh Coalsondf598452006-04-28 00:13:34 +00003572 }
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003573 else
3574 max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3575 }
3576 else {
3577 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3578 }
3579 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3580 _candidate_bits =
3581 evaluate_lpc_subframe_(
3582 encoder,
3583 integer_signal,
3584 residual[!_best_subframe],
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003585 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,
Josh Coalson05f710f2007-07-31 00:51:26 +00003593 rice_parameter_limit,
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003594 min_partition_order,
3595 max_partition_order,
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003596 encoder->protected_->do_escape_coding,
3597 encoder->protected_->rice_parameter_search_dist,
3598 subframe[!_best_subframe],
3599 partitioned_rice_contents[!_best_subframe]
Josh Coalsonbf0f52c2006-04-25 06:38:43 +00003600 );
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 Coalsonf4ce50b2001-02-28 23:45:15 +00003606 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00003607 }
3608 }
3609 }
3610 }
3611 }
3612 }
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003613#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
Josh Coalson94e02cd2001-01-25 10:41:06 +00003614 }
3615 }
3616
Josh Coalson72695802002-10-11 06:25:16 +00003617 /* 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);
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003620 _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson72695802002-10-11 06:25:16 +00003621 }
Josh Coalsone6b3bbe2002-10-08 06:03:25 +00003622
Josh Coalson94e02cd2001-01-25 10:41:06 +00003623 *best_subframe = _best_subframe;
3624 *best_bits = _best_bits;
3625
3626 return true;
3627}
3628
Josh Coalson6fe72f72002-08-20 04:01:59 +00003629FLAC__bool add_subframe_(
3630 FLAC__StreamEncoder *encoder,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003631 uint32_t blocksize,
3632 uint32_t subframe_bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003633 const FLAC__Subframe *subframe,
Josh Coalson423f8042007-01-28 17:40:26 +00003634 FLAC__BitWriter *frame
Josh Coalson6fe72f72002-08-20 04:01:59 +00003635)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003636{
3637 switch(subframe->type) {
3638 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00003639 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003640 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003641 return false;
3642 }
3643 break;
3644 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003645 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003646 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003647 return false;
3648 }
3649 break;
3650 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003651 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003652 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003653 return false;
3654 }
3655 break;
3656 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003657 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson6b21f662006-09-13 01:42:27 +00003658 encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003659 return false;
3660 }
3661 break;
3662 default:
Josh Coalson1b689822001-05-31 20:11:02 +00003663 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003664 }
3665
3666 return true;
3667}
3668
Josh Coalson469ba742007-02-03 02:52:32 +00003669#define SPOTCHECK_ESTIMATE 0
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003670#if SPOTCHECK_ESTIMATE
3671static void spotcheck_subframe_estimate_(
3672 FLAC__StreamEncoder *encoder,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003673 uint32_t blocksize,
3674 uint32_t subframe_bps,
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003675 const FLAC__Subframe *subframe,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003676 uint32_t estimate
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003677)
3678{
3679 FLAC__bool ret;
Josh Coalson423f8042007-01-28 17:40:26 +00003680 FLAC__BitWriter *frame = FLAC__bitwriter_new();
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003681 if(frame == 0) {
3682 fprintf(stderr, "EST: can't allocate frame\n");
3683 return;
3684 }
Josh Coalson423f8042007-01-28 17:40:26 +00003685 if(!FLAC__bitwriter_init(frame)) {
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003686 fprintf(stderr, "EST: can't init frame\n");
3687 return;
3688 }
3689 ret = add_subframe_(encoder, blocksize, subframe_bps, subframe, frame);
3690 FLAC__ASSERT(ret);
3691 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003692 const uint32_t actual = FLAC__bitwriter_get_input_bits_unconsumed(frame);
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003693 if(estimate != actual)
3694 fprintf(stderr, "EST: bad, frame#%u sub#%%d type=%8s est=%u, actual=%u, delta=%d\n", encoder->private_->current_frame_number, FLAC__SubframeTypeString[subframe->type], estimate, actual, (int)actual-(int)estimate);
3695 }
Josh Coalson423f8042007-01-28 17:40:26 +00003696 FLAC__bitwriter_delete(frame);
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003697}
3698#endif
3699
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003700uint32_t evaluate_constant_subframe_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003701 FLAC__StreamEncoder *encoder,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003702 const FLAC__int32 signal,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003703 uint32_t blocksize,
3704 uint32_t subframe_bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003705 FLAC__Subframe *subframe
3706)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003707{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003708 uint32_t estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003709 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
3710 subframe->data.constant.value = signal;
3711
Josh Coalson423f8042007-01-28 17:40:26 +00003712 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + subframe_bps;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003713
3714#if SPOTCHECK_ESTIMATE
3715 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3716#else
3717 (void)encoder, (void)blocksize;
3718#endif
3719
3720 return estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003721}
3722
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003723uint32_t evaluate_fixed_subframe_(
Josh Coalson6fe72f72002-08-20 04:01:59 +00003724 FLAC__StreamEncoder *encoder,
3725 const FLAC__int32 signal[],
3726 FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003727 FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003728 uint32_t raw_bits_per_partition[],
3729 uint32_t blocksize,
3730 uint32_t subframe_bps,
3731 uint32_t order,
3732 uint32_t rice_parameter,
3733 uint32_t rice_parameter_limit,
3734 uint32_t min_partition_order,
3735 uint32_t max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003736 FLAC__bool do_escape_coding,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003737 uint32_t rice_parameter_search_dist,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003738 FLAC__Subframe *subframe,
3739 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3740)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003741{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003742 uint32_t i, residual_bits, estimate;
3743 const uint32_t residual_samples = blocksize - order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003744
3745 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
3746
3747 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
3748
3749 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003750 subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003751 subframe->data.fixed.residual = residual;
3752
Josh Coalson6fe72f72002-08-20 04:01:59 +00003753 residual_bits =
3754 find_best_partition_order_(
3755 encoder->private_,
3756 residual,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003757 abs_residual_partition_sums,
3758 raw_bits_per_partition,
3759 residual_samples,
3760 order,
3761 rice_parameter,
Josh Coalson05f710f2007-07-31 00:51:26 +00003762 rice_parameter_limit,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003763 min_partition_order,
3764 max_partition_order,
Josh Coalson0a0a10f2007-03-31 01:01:02 +00003765 subframe_bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003766 do_escape_coding,
3767 rice_parameter_search_dist,
Josh Coalson05f710f2007-07-31 00:51:26 +00003768 &subframe->data.fixed.entropy_coding_method
Josh Coalson6fe72f72002-08-20 04:01:59 +00003769 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003770
3771 subframe->data.fixed.order = order;
3772 for(i = 0; i < order; i++)
3773 subframe->data.fixed.warmup[i] = signal[i];
3774
Josh Coalson423f8042007-01-28 17:40:26 +00003775 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (order * subframe_bps) + residual_bits;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003776
3777#if SPOTCHECK_ESTIMATE
3778 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3779#endif
3780
3781 return estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003782}
3783
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003784#ifndef FLAC__INTEGER_ONLY_LIBRARY
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003785uint32_t evaluate_lpc_subframe_(
Josh Coalson6fe72f72002-08-20 04:01:59 +00003786 FLAC__StreamEncoder *encoder,
3787 const FLAC__int32 signal[],
3788 FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003789 FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003790 uint32_t raw_bits_per_partition[],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003791 const FLAC__real lp_coeff[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003792 uint32_t blocksize,
3793 uint32_t subframe_bps,
3794 uint32_t order,
3795 uint32_t qlp_coeff_precision,
3796 uint32_t rice_parameter,
3797 uint32_t rice_parameter_limit,
3798 uint32_t min_partition_order,
3799 uint32_t max_partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003800 FLAC__bool do_escape_coding,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003801 uint32_t rice_parameter_search_dist,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003802 FLAC__Subframe *subframe,
3803 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
3804)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003805{
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +10003806 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; /* WATCHOUT: the size is important; some x86 intrinsic routines need more than lpc order elements */
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003807 uint32_t i, residual_bits, estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003808 int quantization, ret;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003809 const uint32_t residual_samples = blocksize - order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003810
lvqcl165d7b12015-10-12 00:59:08 +03003811 /* try to keep qlp coeff precision such that only 32-bit math is required for decode of <=16bps(+1bps for side channel) streams */
3812 if(subframe_bps <= 17) {
Josh Coalson20ac2c12002-08-30 05:47:14 +00003813 FLAC__ASSERT(order > 0);
3814 FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
Cristian Rodríguezf0296252012-04-05 19:39:37 -03003815 qlp_coeff_precision = flac_min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
Josh Coalson20ac2c12002-08-30 05:47:14 +00003816 }
3817
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003818 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003819 if(ret != 0)
3820 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
3821
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00003822 if(subframe_bps + qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
3823 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
3824 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
3825 else
3826 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
Josh Coalsonc9c0d132002-10-04 05:29:05 +00003827 else
3828 encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003829
3830 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
3831
3832 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
Josh Coalsona37ba462002-08-19 21:36:39 +00003833 subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003834 subframe->data.lpc.residual = residual;
3835
Josh Coalson6fe72f72002-08-20 04:01:59 +00003836 residual_bits =
3837 find_best_partition_order_(
3838 encoder->private_,
3839 residual,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003840 abs_residual_partition_sums,
3841 raw_bits_per_partition,
3842 residual_samples,
3843 order,
3844 rice_parameter,
Josh Coalson05f710f2007-07-31 00:51:26 +00003845 rice_parameter_limit,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003846 min_partition_order,
3847 max_partition_order,
Josh Coalson0a0a10f2007-03-31 01:01:02 +00003848 subframe_bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003849 do_escape_coding,
3850 rice_parameter_search_dist,
Josh Coalson05f710f2007-07-31 00:51:26 +00003851 &subframe->data.lpc.entropy_coding_method
Josh Coalson6fe72f72002-08-20 04:01:59 +00003852 );
Josh Coalson94e02cd2001-01-25 10:41:06 +00003853
3854 subframe->data.lpc.order = order;
3855 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
3856 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00003857 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00003858 for(i = 0; i < order; i++)
3859 subframe->data.lpc.warmup[i] = signal[i];
3860
Josh Coalson423f8042007-01-28 17:40:26 +00003861 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003862
3863#if SPOTCHECK_ESTIMATE
3864 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3865#endif
3866
3867 return estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003868}
Josh Coalson5f2b46d2004-11-09 01:34:01 +00003869#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00003870
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003871uint32_t evaluate_verbatim_subframe_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003872 FLAC__StreamEncoder *encoder,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003873 const FLAC__int32 signal[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003874 uint32_t blocksize,
3875 uint32_t subframe_bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003876 FLAC__Subframe *subframe
3877)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003878{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003879 uint32_t estimate;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003880
Josh Coalson94e02cd2001-01-25 10:41:06 +00003881 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
3882
3883 subframe->data.verbatim.data = signal;
3884
Josh Coalson423f8042007-01-28 17:40:26 +00003885 estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps);
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003886
3887#if SPOTCHECK_ESTIMATE
3888 spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
3889#else
3890 (void)encoder;
3891#endif
3892
3893 return estimate;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003894}
3895
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003896uint32_t find_best_partition_order_(
Josh Coalson6fe72f72002-08-20 04:01:59 +00003897 FLAC__StreamEncoderPrivate *private_,
3898 const FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003899 FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003900 uint32_t raw_bits_per_partition[],
3901 uint32_t residual_samples,
3902 uint32_t predictor_order,
3903 uint32_t rice_parameter,
3904 uint32_t rice_parameter_limit,
3905 uint32_t min_partition_order,
3906 uint32_t max_partition_order,
3907 uint32_t bps,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003908 FLAC__bool do_escape_coding,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003909 uint32_t rice_parameter_search_dist,
Josh Coalson05f710f2007-07-31 00:51:26 +00003910 FLAC__EntropyCodingMethod *best_ecm
Josh Coalson6fe72f72002-08-20 04:01:59 +00003911)
Josh Coalson94e02cd2001-01-25 10:41:06 +00003912{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003913 uint32_t residual_bits, best_residual_bits = 0;
3914 uint32_t best_parameters_index = 0;
3915 uint32_t best_partition_order = 0;
3916 const uint32_t blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00003917
Josh Coalsonb7023aa2002-08-17 15:23:43 +00003918 max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
Cristian Rodríguezf0296252012-04-05 19:39:37 -03003919 min_partition_order = flac_min(min_partition_order, max_partition_order);
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003920
Erik de Castro Lopoecd0acb2013-10-04 01:38:00 +10003921 private_->local_precompute_partition_info_sums(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order, bps);
Josh Coalson1109e7f2007-01-24 04:26:15 +00003922
3923 if(do_escape_coding)
3924 precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
3925
3926 {
Josh Coalson8395d022001-07-12 21:25:22 +00003927 int partition_order;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003928 uint32_t sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003929
Josh Coalson8395d022001-07-12 21:25:22 +00003930 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalson6fe72f72002-08-20 04:01:59 +00003931 if(!
Josh Coalson1109e7f2007-01-24 04:26:15 +00003932 set_partitioned_rice_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003933#ifdef EXACT_RICE_BITS_CALCULATION
Josh Coalson6fe72f72002-08-20 04:01:59 +00003934 residual,
Josh Coalsonce1d07c2007-01-23 05:00:46 +00003935#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00003936 abs_residual_partition_sums+sum,
3937 raw_bits_per_partition+sum,
3938 residual_samples,
3939 predictor_order,
3940 rice_parameter,
Josh Coalson05f710f2007-07-31 00:51:26 +00003941 rice_parameter_limit,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003942 rice_parameter_search_dist,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003943 (uint32_t)partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00003944 do_escape_coding,
3945 &private_->partitioned_rice_contents_extra[!best_parameters_index],
3946 &residual_bits
3947 )
3948 )
Josh Coalson8395d022001-07-12 21:25:22 +00003949 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00003950 FLAC__ASSERT(best_residual_bits != 0);
3951 break;
Josh Coalson8395d022001-07-12 21:25:22 +00003952 }
3953 sum += 1u << partition_order;
3954 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
3955 best_residual_bits = residual_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00003956 best_parameters_index = !best_parameters_index;
Josh Coalson05f710f2007-07-31 00:51:26 +00003957 best_partition_order = partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00003958 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00003959 }
3960 }
Josh Coalson8395d022001-07-12 21:25:22 +00003961
Josh Coalson05f710f2007-07-31 00:51:26 +00003962 best_ecm->data.partitioned_rice.order = best_partition_order;
3963
Josh Coalsona37ba462002-08-19 21:36:39 +00003964 {
Josh Coalson05f710f2007-07-31 00:51:26 +00003965 /*
3966 * We are allowed to de-const the pointer based on our special
3967 * knowledge; it is const to the outside world.
3968 */
3969 FLAC__EntropyCodingMethod_PartitionedRiceContents* prc = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_ecm->data.partitioned_rice.contents;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003970 uint32_t partition;
Josh Coalson05f710f2007-07-31 00:51:26 +00003971
3972 /* save best parameters and raw_bits */
Cristian Rodríguezf0296252012-04-05 19:39:37 -03003973 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(prc, flac_max(6u, best_partition_order));
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003974 memcpy(prc->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(uint32_t)*(1<<(best_partition_order)));
Josh Coalson05f710f2007-07-31 00:51:26 +00003975 if(do_escape_coding)
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003976 memcpy(prc->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(uint32_t)*(1<<(best_partition_order)));
Josh Coalson05f710f2007-07-31 00:51:26 +00003977 /*
3978 * Now need to check if the type should be changed to
3979 * FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2 based on the
3980 * size of the rice parameters.
3981 */
3982 for(partition = 0; partition < (1u<<best_partition_order); partition++) {
3983 if(prc->parameters[partition] >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3984 best_ecm->type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2;
3985 break;
3986 }
3987 }
Josh Coalsona37ba462002-08-19 21:36:39 +00003988 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00003989
3990 return best_residual_bits;
3991}
3992
Josh Coalson6fe72f72002-08-20 04:01:59 +00003993void precompute_partition_info_sums_(
Josh Coalson1109e7f2007-01-24 04:26:15 +00003994 const FLAC__int32 residual[],
Josh Coalson6fe72f72002-08-20 04:01:59 +00003995 FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11003996 uint32_t residual_samples,
3997 uint32_t predictor_order,
3998 uint32_t min_partition_order,
3999 uint32_t max_partition_order,
4000 uint32_t bps
Josh Coalson6fe72f72002-08-20 04:01:59 +00004001)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004002{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004003 const uint32_t default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
4004 uint32_t partitions = 1u << max_partition_order;
Josh Coalson0a0a10f2007-03-31 01:01:02 +00004005
4006 FLAC__ASSERT(default_partition_samples > predictor_order);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004007
Josh Coalson0429d602007-04-04 01:08:24 +00004008 /* first do max_partition_order */
4009 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004010 const uint32_t threshold = 32 - FLAC__bitmath_ilog2(default_partition_samples);
4011 uint32_t partition, residual_sample, end = (uint32_t)(-(int)predictor_order);
Erik de Castro Lopo86b36d92015-11-03 18:08:49 +11004012 /* WATCHOUT: "bps + FLAC__MAX_EXTRA_RESIDUAL_BPS" is the maximum assumed size of the average residual magnitude */
4013 if(bps + FLAC__MAX_EXTRA_RESIDUAL_BPS < threshold) {
Josh Coalson0429d602007-04-04 01:08:24 +00004014 for(partition = residual_sample = 0; partition < partitions; partition++) {
Erik de Castro Lopo86b36d92015-11-03 18:08:49 +11004015 FLAC__uint32 abs_residual_partition_sum = 0;
Josh Coalson0429d602007-04-04 01:08:24 +00004016 end += default_partition_samples;
Josh Coalson0429d602007-04-04 01:08:24 +00004017 for( ; residual_sample < end; residual_sample++)
4018 abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
4019 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
4020 }
4021 }
4022 else { /* have to pessimistically use 64 bits for accumulator */
Josh Coalson0429d602007-04-04 01:08:24 +00004023 for(partition = residual_sample = 0; partition < partitions; partition++) {
Erik de Castro Lopo86b36d92015-11-03 18:08:49 +11004024 FLAC__uint64 abs_residual_partition_sum64 = 0;
Josh Coalson0429d602007-04-04 01:08:24 +00004025 end += default_partition_samples;
Josh Coalson0429d602007-04-04 01:08:24 +00004026 for( ; residual_sample < end; residual_sample++)
Erik de Castro Lopo86b36d92015-11-03 18:08:49 +11004027 abs_residual_partition_sum64 += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
4028 abs_residual_partition_sums[partition] = abs_residual_partition_sum64;
Josh Coalson0429d602007-04-04 01:08:24 +00004029 }
Josh Coalson0a0a10f2007-03-31 01:01:02 +00004030 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004031 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00004032
Josh Coalson8395d022001-07-12 21:25:22 +00004033 /* now merge partitions for lower orders */
Josh Coalson0429d602007-04-04 01:08:24 +00004034 {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004035 uint32_t from_partition = 0, to_partition = partitions;
Josh Coalson0429d602007-04-04 01:08:24 +00004036 int partition_order;
4037 for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004038 uint32_t i;
Josh Coalson0429d602007-04-04 01:08:24 +00004039 partitions >>= 1;
4040 for(i = 0; i < partitions; i++) {
4041 abs_residual_partition_sums[to_partition++] =
4042 abs_residual_partition_sums[from_partition ] +
4043 abs_residual_partition_sums[from_partition+1];
4044 from_partition += 2;
4045 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00004046 }
4047 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00004048}
Josh Coalson8395d022001-07-12 21:25:22 +00004049
Josh Coalson6fe72f72002-08-20 04:01:59 +00004050void precompute_partition_info_escapes_(
4051 const FLAC__int32 residual[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004052 uint32_t raw_bits_per_partition[],
4053 uint32_t residual_samples,
4054 uint32_t predictor_order,
4055 uint32_t min_partition_order,
4056 uint32_t max_partition_order
Josh Coalson6fe72f72002-08-20 04:01:59 +00004057)
Josh Coalson8395d022001-07-12 21:25:22 +00004058{
4059 int partition_order;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004060 uint32_t from_partition, to_partition = 0;
4061 const uint32_t blocksize = residual_samples + predictor_order;
Josh Coalson8395d022001-07-12 21:25:22 +00004062
4063 /* first do max_partition_order */
4064 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalson90c693a2007-02-01 04:24:08 +00004065 FLAC__int32 r;
4066 FLAC__uint32 rmax;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004067 uint32_t partition, partition_sample, partition_samples, residual_sample;
4068 const uint32_t partitions = 1u << partition_order;
4069 const uint32_t default_partition_samples = blocksize >> partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00004070
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004071 FLAC__ASSERT(default_partition_samples > predictor_order);
4072
4073 for(partition = residual_sample = 0; partition < partitions; partition++) {
4074 partition_samples = default_partition_samples;
4075 if(partition == 0)
4076 partition_samples -= predictor_order;
Josh Coalson90c693a2007-02-01 04:24:08 +00004077 rmax = 0;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004078 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
Josh Coalson90c693a2007-02-01 04:24:08 +00004079 r = residual[residual_sample++];
Josh Coalson8534bbe2007-02-28 01:10:21 +00004080 /* OPT: maybe faster: rmax |= r ^ (r>>31) */
Josh Coalson90c693a2007-02-01 04:24:08 +00004081 if(r < 0)
4082 rmax |= ~r;
4083 else
4084 rmax |= r;
Josh Coalson8395d022001-07-12 21:25:22 +00004085 }
Josh Coalson90c693a2007-02-01 04:24:08 +00004086 /* now we know all residual values are in the range [-rmax-1,rmax] */
4087 raw_bits_per_partition[partition] = rmax? FLAC__bitmath_ilog2(rmax) + 2 : 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004088 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004089 to_partition = partitions;
Josh Coalson369a6da2006-10-10 00:37:48 +00004090 break; /*@@@ yuck, should remove the 'for' loop instead */
Josh Coalson8395d022001-07-12 21:25:22 +00004091 }
4092
4093 /* now merge partitions for lower orders */
4094 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004095 uint32_t m;
4096 uint32_t i;
4097 const uint32_t partitions = 1u << partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00004098 for(i = 0; i < partitions; i++) {
4099 m = raw_bits_per_partition[from_partition];
4100 from_partition++;
Cristian Rodríguezf0296252012-04-05 19:39:37 -03004101 raw_bits_per_partition[to_partition] = flac_max(m, raw_bits_per_partition[from_partition]);
Josh Coalson8395d022001-07-12 21:25:22 +00004102 from_partition++;
4103 to_partition++;
4104 }
4105 }
4106}
Josh Coalson94e02cd2001-01-25 10:41:06 +00004107
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004108#ifdef EXACT_RICE_BITS_CALCULATION
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004109static inline uint32_t count_rice_bits_in_partition_(
4110 const uint32_t rice_parameter,
4111 const uint32_t partition_samples,
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004112 const FLAC__int32 *residual
Josh Coalson6fe72f72002-08-20 04:01:59 +00004113)
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004114{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004115 uint32_t i, partition_bits =
Josh Coalson05f710f2007-07-31 00:51:26 +00004116 FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
Josh Coalson423f8042007-01-28 17:40:26 +00004117 (1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */
4118 ;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004119 for(i = 0; i < partition_samples; i++)
Josh Coalson423f8042007-01-28 17:40:26 +00004120 partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter );
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004121 return partition_bits;
4122}
Josh Coalson8395d022001-07-12 21:25:22 +00004123#else
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004124static inline uint32_t count_rice_bits_in_partition_(
4125 const uint32_t rice_parameter,
4126 const uint32_t partition_samples,
Josh Coalson1109e7f2007-01-24 04:26:15 +00004127 const FLAC__uint64 abs_residual_partition_sum
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004128)
4129{
Josh Coalson1109e7f2007-01-24 04:26:15 +00004130 return
Josh Coalson05f710f2007-07-31 00:51:26 +00004131 FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
Josh Coalson1109e7f2007-01-24 04:26:15 +00004132 (1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */
4133 (
4134 rice_parameter?
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004135 (uint32_t)(abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */
4136 : (uint32_t)(abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */
Josh Coalson1109e7f2007-01-24 04:26:15 +00004137 )
4138 - (partition_samples >> 1)
4139 /* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum.
Josh Coalson5981e4f2007-03-06 06:51:57 +00004140 * The actual number of bits used is closer to the sum(for all i in the partition) of abs(residual[i])>>(rice_parameter-1)
Josh Coalson1109e7f2007-01-24 04:26:15 +00004141 * By using the abs_residual_partition sum, we also add in bits in the LSBs that would normally be shifted out.
4142 * So the subtraction term tries to guess how many extra bits were contributed.
4143 * If the LSBs are randomly distributed, this should average to 0.5 extra bits per sample.
4144 */
4145 ;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004146}
4147#endif
4148
Josh Coalson6fe72f72002-08-20 04:01:59 +00004149FLAC__bool set_partitioned_rice_(
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004150#ifdef EXACT_RICE_BITS_CALCULATION
4151 const FLAC__int32 residual[],
4152#endif
Josh Coalson6fe72f72002-08-20 04:01:59 +00004153 const FLAC__uint64 abs_residual_partition_sums[],
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004154 const uint32_t raw_bits_per_partition[],
4155 const uint32_t residual_samples,
4156 const uint32_t predictor_order,
4157 const uint32_t suggested_rice_parameter,
4158 const uint32_t rice_parameter_limit,
4159 const uint32_t rice_parameter_search_dist,
4160 const uint32_t partition_order,
Josh Coalson6fe72f72002-08-20 04:01:59 +00004161 const FLAC__bool search_for_escapes,
4162 FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004163 uint32_t *bits
Josh Coalson6fe72f72002-08-20 04:01:59 +00004164)
Josh Coalson8395d022001-07-12 21:25:22 +00004165{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004166 uint32_t rice_parameter, partition_bits;
4167 uint32_t best_partition_bits, best_rice_parameter = 0;
4168 uint32_t bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
4169 uint32_t *parameters, *raw_bits;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004170#ifdef ENABLE_RICE_PARAMETER_SEARCH
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004171 uint32_t min_rice_parameter, max_rice_parameter;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004172#else
4173 (void)rice_parameter_search_dist;
4174#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004175
Josh Coalson05f710f2007-07-31 00:51:26 +00004176 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
4177 FLAC__ASSERT(rice_parameter_limit <= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
Josh Coalson8395d022001-07-12 21:25:22 +00004178
Cristian Rodríguezf0296252012-04-05 19:39:37 -03004179 FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order));
Josh Coalsona37ba462002-08-19 21:36:39 +00004180 parameters = partitioned_rice_contents->parameters;
4181 raw_bits = partitioned_rice_contents->raw_bits;
Josh Coalsonb7023aa2002-08-17 15:23:43 +00004182
Josh Coalson8395d022001-07-12 21:25:22 +00004183 if(partition_order == 0) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004184 best_partition_bits = (uint32_t)(-1);
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004185#ifdef ENABLE_RICE_PARAMETER_SEARCH
Josh Coalson8395d022001-07-12 21:25:22 +00004186 if(rice_parameter_search_dist) {
4187 if(suggested_rice_parameter < rice_parameter_search_dist)
4188 min_rice_parameter = 0;
4189 else
4190 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
4191 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson05f710f2007-07-31 00:51:26 +00004192 if(max_rice_parameter >= rice_parameter_limit) {
David Seifertb3ece712017-01-15 10:37:02 +01004193#ifndef NDEBUG
Josh Coalson05f710f2007-07-31 00:51:26 +00004194 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, rice_parameter_limit - 1);
Josh Coalson8395d022001-07-12 21:25:22 +00004195#endif
Josh Coalson05f710f2007-07-31 00:51:26 +00004196 max_rice_parameter = rice_parameter_limit - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004197 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004198 }
4199 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004200 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00004201
Josh Coalson034dfab2001-04-27 19:10:23 +00004202 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004203#else
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004204 rice_parameter = suggested_rice_parameter;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004205#endif
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004206#ifdef EXACT_RICE_BITS_CALCULATION
Josh Coalson1109e7f2007-01-24 04:26:15 +00004207 partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, residual);
Josh Coalsonb9433f92001-03-17 01:07:00 +00004208#else
Josh Coalson1109e7f2007-01-24 04:26:15 +00004209 partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, abs_residual_partition_sums[0]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00004210#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004211 if(partition_bits < best_partition_bits) {
4212 best_rice_parameter = rice_parameter;
4213 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00004214 }
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004215#ifdef ENABLE_RICE_PARAMETER_SEARCH
Josh Coalson352e0f62001-03-20 22:55:50 +00004216 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004217#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004218 if(search_for_escapes) {
Josh Coalson05f710f2007-07-31 00:51:26 +00004219 partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
Josh Coalson90c693a2007-02-01 04:24:08 +00004220 if(partition_bits <= best_partition_bits) {
Josh Coalson8395d022001-07-12 21:25:22 +00004221 raw_bits[0] = raw_bits_per_partition[0];
Josh Coalson05f710f2007-07-31 00:51:26 +00004222 best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
Josh Coalson90c693a2007-02-01 04:24:08 +00004223 best_partition_bits = partition_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00004224 }
Josh Coalson05f710f2007-07-31 00:51:26 +00004225 else
4226 raw_bits[0] = 0;
Josh Coalson034dfab2001-04-27 19:10:23 +00004227 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004228 parameters[0] = best_rice_parameter;
4229 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004230 }
4231 else {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004232 uint32_t partition, residual_sample;
4233 uint32_t partition_samples;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004234 FLAC__uint64 mean, k;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004235 const uint32_t partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00004236 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004237 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00004238 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00004239 if(partition_samples <= predictor_order)
4240 return false;
4241 else
4242 partition_samples -= predictor_order;
4243 }
Josh Coalson05d20792001-06-29 23:12:26 +00004244 mean = abs_residual_partition_sums[partition];
Josh Coalsonf81b6df2005-02-04 01:34:35 +00004245 /* we are basically calculating the size in bits of the
4246 * average residual magnitude in the partition:
4247 * rice_parameter = floor(log2(mean/partition_samples))
4248 * 'mean' is not a good name for the variable, it is
4249 * actually the sum of magnitudes of all residual values
4250 * in the partition, so the actual mean is
4251 * mean/partition_samples
4252 */
Erik de Castro Lopo64f34e62013-10-10 21:31:50 +11004253#if 0 /* old simple code */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00004254 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00004255 ;
Erik de Castro Lopo64f34e62013-10-10 21:31:50 +11004256#else
Erik de Castro Lopob8d58e32014-06-15 20:29:34 +10004257#if defined FLAC__CPU_X86_64 /* and other 64-bit arch, too */
Erik de Castro Lopo64f34e62013-10-10 21:31:50 +11004258 if(mean <= 0x80000000/512) { /* 512: more or less optimal for both 16- and 24-bit input */
4259#else
4260 if(mean <= 0x80000000/8) { /* 32-bit arch: use 32-bit math if possible */
4261#endif
4262 FLAC__uint32 k2, mean2 = (FLAC__uint32) mean;
4263 rice_parameter = 0; k2 = partition_samples;
4264 while(k2*8 < mean2) { /* requires: mean <= (2^31)/8 */
4265 rice_parameter += 4; k2 <<= 4; /* tuned for 16-bit input */
4266 }
4267 while(k2 < mean2) { /* requires: mean <= 2^31 */
4268 rice_parameter++; k2 <<= 1;
4269 }
4270 }
4271 else {
4272 rice_parameter = 0; k = partition_samples;
4273 if(mean <= FLAC__U64L(0x8000000000000000)/128) /* usually mean is _much_ smaller than this value */
4274 while(k*128 < mean) { /* requires: mean <= (2^63)/128 */
4275 rice_parameter += 8; k <<= 8; /* tuned for 24-bit input */
4276 }
4277 while(k < mean) { /* requires: mean <= 2^63 */
4278 rice_parameter++; k <<= 1;
4279 }
4280 }
4281#endif
Josh Coalson05f710f2007-07-31 00:51:26 +00004282 if(rice_parameter >= rice_parameter_limit) {
David Seifertb3ece712017-01-15 10:37:02 +01004283#ifndef NDEBUG
Josh Coalson05f710f2007-07-31 00:51:26 +00004284 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, rice_parameter_limit - 1);
Josh Coalson8395d022001-07-12 21:25:22 +00004285#endif
Josh Coalson05f710f2007-07-31 00:51:26 +00004286 rice_parameter = rice_parameter_limit - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004287 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004288
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004289 best_partition_bits = (uint32_t)(-1);
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004290#ifdef ENABLE_RICE_PARAMETER_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00004291 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00004292 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00004293 min_rice_parameter = 0;
4294 else
Josh Coalson034dfab2001-04-27 19:10:23 +00004295 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
4296 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson05f710f2007-07-31 00:51:26 +00004297 if(max_rice_parameter >= rice_parameter_limit) {
David Seifertb3ece712017-01-15 10:37:02 +01004298#ifndef NDEBUG
Josh Coalson05f710f2007-07-31 00:51:26 +00004299 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, rice_parameter_limit - 1);
Josh Coalson8395d022001-07-12 21:25:22 +00004300#endif
Josh Coalson05f710f2007-07-31 00:51:26 +00004301 max_rice_parameter = rice_parameter_limit - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00004302 }
Josh Coalson60f77d72001-04-25 02:16:36 +00004303 }
4304 else
4305 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00004306
Josh Coalson034dfab2001-04-27 19:10:23 +00004307 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
4308#endif
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004309#ifdef EXACT_RICE_BITS_CALCULATION
Josh Coalson1109e7f2007-01-24 04:26:15 +00004310 partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, residual+residual_sample);
Josh Coalson034dfab2001-04-27 19:10:23 +00004311#else
Josh Coalson1109e7f2007-01-24 04:26:15 +00004312 partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, abs_residual_partition_sums[partition]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00004313#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00004314 if(partition_bits < best_partition_bits) {
4315 best_rice_parameter = rice_parameter;
4316 best_partition_bits = partition_bits;
4317 }
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004318#ifdef ENABLE_RICE_PARAMETER_SEARCH
Josh Coalson2051dd42001-04-12 22:22:34 +00004319 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004320#endif
Josh Coalson8395d022001-07-12 21:25:22 +00004321 if(search_for_escapes) {
Josh Coalson05f710f2007-07-31 00:51:26 +00004322 partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
Josh Coalson90c693a2007-02-01 04:24:08 +00004323 if(partition_bits <= best_partition_bits) {
Josh Coalson8395d022001-07-12 21:25:22 +00004324 raw_bits[partition] = raw_bits_per_partition[partition];
Josh Coalson05f710f2007-07-31 00:51:26 +00004325 best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
Josh Coalson90c693a2007-02-01 04:24:08 +00004326 best_partition_bits = partition_bits;
Josh Coalson8395d022001-07-12 21:25:22 +00004327 }
Josh Coalson05f710f2007-07-31 00:51:26 +00004328 else
4329 raw_bits[partition] = 0;
Josh Coalson2051dd42001-04-12 22:22:34 +00004330 }
Josh Coalson034dfab2001-04-27 19:10:23 +00004331 parameters[partition] = best_rice_parameter;
4332 bits_ += best_partition_bits;
Josh Coalsonce1d07c2007-01-23 05:00:46 +00004333 residual_sample += partition_samples;
Josh Coalson94e02cd2001-01-25 10:41:06 +00004334 }
4335 }
4336
4337 *bits = bits_;
4338 return true;
4339}
Josh Coalson859bc542001-03-27 22:22:27 +00004340
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004341uint32_t get_wasted_bits_(FLAC__int32 signal[], uint32_t samples)
Josh Coalson859bc542001-03-27 22:22:27 +00004342{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004343 uint32_t i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00004344 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00004345
4346 for(i = 0; i < samples && !(x&1); i++)
4347 x |= signal[i];
4348
4349 if(x == 0) {
4350 shift = 0;
4351 }
4352 else {
4353 for(shift = 0; !(x&1); shift++)
4354 x >>= 1;
4355 }
4356
4357 if(shift > 0) {
4358 for(i = 0; i < samples; i++)
4359 signal[i] >>= shift;
4360 }
4361
4362 return shift;
4363}
Josh Coalsond86e03b2002-08-03 21:56:15 +00004364
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004365void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], uint32_t input_offset, uint32_t channels, uint32_t wide_samples)
Josh Coalsond86e03b2002-08-03 21:56:15 +00004366{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004367 uint32_t channel;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004368
4369 for(channel = 0; channel < channels; channel++)
4370 memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples);
4371
4372 fifo->tail += wide_samples;
4373
4374 FLAC__ASSERT(fifo->tail <= fifo->size);
4375}
4376
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004377void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], uint32_t input_offset, uint32_t channels, uint32_t wide_samples)
Josh Coalsond86e03b2002-08-03 21:56:15 +00004378{
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004379 uint32_t channel;
4380 uint32_t sample, wide_sample;
4381 uint32_t tail = fifo->tail;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004382
4383 sample = input_offset * channels;
4384 for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) {
4385 for(channel = 0; channel < channels; channel++)
4386 fifo->data[channel][tail] = input[sample++];
4387 tail++;
4388 }
4389 fifo->tail = tail;
4390
4391 FLAC__ASSERT(fifo->tail <= fifo->size);
4392}
4393
Josh Coalson8065a2d2006-10-15 08:32:56 +00004394FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalsond86e03b2002-08-03 21:56:15 +00004395{
4396 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
Josh Coalson8065a2d2006-10-15 08:32:56 +00004397 const size_t encoded_bytes = encoder->private_->verify.output.bytes;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004398 (void)decoder;
4399
4400 if(encoder->private_->verify.needs_magic_hack) {
4401 FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH);
4402 *bytes = FLAC__STREAM_SYNC_LENGTH;
4403 memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes);
4404 encoder->private_->verify.needs_magic_hack = false;
4405 }
4406 else {
4407 if(encoded_bytes == 0) {
Josh Coalsonfc2b7372002-08-16 05:39:34 +00004408 /*
4409 * If we get here, a FIFO underflow has occurred,
4410 * which means there is a bug somewhere.
4411 */
4412 FLAC__ASSERT(0);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004413 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4414 }
4415 else if(encoded_bytes < *bytes)
4416 *bytes = encoded_bytes;
4417 memcpy(buffer, encoder->private_->verify.output.data, *bytes);
4418 encoder->private_->verify.output.data += *bytes;
4419 encoder->private_->verify.output.bytes -= *bytes;
4420 }
4421
4422 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4423}
4424
4425FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
4426{
4427 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004428 uint32_t channel;
4429 const uint32_t channels = frame->header.channels;
4430 const uint32_t blocksize = frame->header.blocksize;
4431 const uint32_t bytes_per_block = sizeof(FLAC__int32) * blocksize;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004432
Josh Coalson49f2f162006-11-09 16:54:52 +00004433 (void)decoder;
4434
Josh Coalsond86e03b2002-08-03 21:56:15 +00004435 for(channel = 0; channel < channels; channel++) {
4436 if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) {
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004437 uint32_t i, sample = 0;
Josh Coalsond86e03b2002-08-03 21:56:15 +00004438 FLAC__int32 expect = 0, got = 0;
4439
4440 for(i = 0; i < blocksize; i++) {
4441 if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) {
4442 sample = i;
4443 expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i];
4444 got = (FLAC__int32)buffer[channel][i];
4445 break;
4446 }
4447 }
4448 FLAC__ASSERT(i < blocksize);
4449 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4450 encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample;
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004451 encoder->private_->verify.error_stats.frame_number = (uint32_t)(frame->header.number.sample_number / blocksize);
Josh Coalsond86e03b2002-08-03 21:56:15 +00004452 encoder->private_->verify.error_stats.channel = channel;
4453 encoder->private_->verify.error_stats.sample = sample;
4454 encoder->private_->verify.error_stats.expected = expect;
4455 encoder->private_->verify.error_stats.got = got;
4456 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
4457 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
4458 }
4459 }
4460 /* dequeue the frame from the fifo */
Josh Coalsond86e03b2002-08-03 21:56:15 +00004461 encoder->private_->verify.input_fifo.tail -= blocksize;
Josh Coalson49f2f162006-11-09 16:54:52 +00004462 FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_);
Josh Coalsonb7b57ef2006-11-09 07:06:33 +00004463 for(channel = 0; channel < channels; channel++)
4464 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 Coalsond86e03b2002-08-03 21:56:15 +00004465 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
4466}
4467
4468void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
4469{
4470 (void)decoder, (void)metadata, (void)client_data;
4471}
4472
4473void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
4474{
4475 FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data;
4476 (void)decoder, (void)status;
4477 encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4478}
Josh Coalson6b21f662006-09-13 01:42:27 +00004479
Josh Coalson8065a2d2006-10-15 08:32:56 +00004480FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
Josh Coalson8da98c82006-10-15 04:24:05 +00004481{
4482 (void)client_data;
4483
Josh Coalson8065a2d2006-10-15 08:32:56 +00004484 *bytes = fread(buffer, 1, *bytes, encoder->private_->file);
Josh Coalson8da98c82006-10-15 04:24:05 +00004485 if (*bytes == 0) {
4486 if (feof(encoder->private_->file))
4487 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4488 else if (ferror(encoder->private_->file))
4489 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4490 }
4491 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4492}
4493
Josh Coalson6b21f662006-09-13 01:42:27 +00004494FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4495{
4496 (void)client_data;
4497
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11004498 if(fseeko(encoder->private_->file, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0)
Josh Coalson6b21f662006-09-13 01:42:27 +00004499 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4500 else
4501 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4502}
4503
4504FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4505{
Erik de Castro Lopof25b2602013-03-14 18:22:29 +11004506 FLAC__off_t offset;
Josh Coalson6b21f662006-09-13 01:42:27 +00004507
4508 (void)client_data;
4509
4510 offset = ftello(encoder->private_->file);
4511
4512 if(offset < 0) {
4513 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4514 }
4515 else {
4516 *absolute_byte_offset = (FLAC__uint64)offset;
4517 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4518 }
4519}
4520
4521#ifdef FLAC__VALGRIND_TESTING
4522static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4523{
4524 size_t ret = fwrite(ptr, size, nmemb, stream);
4525 if(!ferror(stream))
4526 fflush(stream);
4527 return ret;
4528}
4529#else
4530#define local__fwrite fwrite
4531#endif
4532
Erik de Castro Lopoc6318e92017-01-14 17:26:39 +11004533FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, uint32_t current_frame, void *client_data)
Josh Coalson6b21f662006-09-13 01:42:27 +00004534{
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004535 (void)client_data, (void)current_frame;
Josh Coalson6b21f662006-09-13 01:42:27 +00004536
4537 if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
Josh Coalson8da98c82006-10-15 04:24:05 +00004538 FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4539#if FLAC__HAS_OGG
4540 /* We would like to be able to use 'samples > 0' in the
4541 * clause here but currently because of the nature of our
4542 * Ogg writing implementation, 'samples' is always 0 (see
4543 * ogg_encoder_aspect.c). The downside is extra progress
4544 * callbacks.
4545 */
4546 encoder->private_->is_ogg? true :
4547#endif
4548 samples > 0
4549 );
4550 if(call_it) {
Josh Coalson2d6b8c62006-10-11 06:30:38 +00004551 /* NOTE: We have to add +bytes, +samples, and +1 to the stats
4552 * because at this point in the callback chain, the stats
4553 * have not been updated. Only after we return and control
4554 * gets back to write_frame_() are the stats updated
4555 */
4556 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);
4557 }
Josh Coalson6b21f662006-09-13 01:42:27 +00004558 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4559 }
4560 else
4561 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4562}
4563
4564/*
4565 * This will forcibly set stdout to binary mode (for OSes that require it)
4566 */
Josh Coalsone3ec2ad2007-01-31 03:53:22 +00004567FILE *get_binary_stdout_(void)
Josh Coalson6b21f662006-09-13 01:42:27 +00004568{
4569 /* if something breaks here it is probably due to the presence or
4570 * absence of an underscore before the identifiers 'setmode',
4571 * 'fileno', and/or 'O_BINARY'; check your system header files.
4572 */
4573#if defined _MSC_VER || defined __MINGW32__
4574 _setmode(_fileno(stdout), _O_BINARY);
Josh Coalson6b21f662006-09-13 01:42:27 +00004575#elif defined __EMX__
4576 setmode(fileno(stdout), O_BINARY);
4577#endif
4578
4579 return stdout;
4580}