Josh Coalson | 26560dd | 2001-02-08 00:38:41 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Erik de Castro Lopo | b1982fb | 2013-05-25 17:11:19 +1000 | [diff] [blame] | 2 | * Copyright (C) 2000-2009 Josh Coalson |
Thomas Zander | e5498e8 | 2016-02-08 10:43:37 +0100 | [diff] [blame] | 3 | * Copyright (C) 2011-2016 Xiph.Org Foundation |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 4 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 8 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 9 | * - Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 11 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 12 | * - 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 Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
Erik de Castro Lopo | 006b835 | 2014-03-23 21:59:46 +1100 | [diff] [blame] | 33 | #ifdef HAVE_CONFIG_H |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 34 | # include <config.h> |
| 35 | #endif |
| 36 | |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 37 | #include <limits.h> |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> /* for malloc() */ |
| 40 | #include <string.h> /* for memcpy() */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 41 | #include <sys/types.h> /* for off_t */ |
Erik de Castro Lopo | 6c03ab2 | 2016-01-24 08:08:50 +1100 | [diff] [blame] | 42 | #ifdef _WIN32 |
| 43 | #include <windows.h> /* for GetFileType() */ |
| 44 | #include <io.h> /* for _get_osfhandle() */ |
| 45 | #endif |
Erik de Castro Lopo | ecd0acb | 2013-10-04 01:38:00 +1000 | [diff] [blame] | 46 | #include "share/compat.h" |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 47 | #include "FLAC/assert.h" |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 48 | #include "FLAC/stream_decoder.h" |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 49 | #include "protected/stream_encoder.h" |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 50 | #include "private/bitwriter.h" |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 51 | #include "private/bitmath.h" |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 52 | #include "private/crc.h" |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 53 | #include "private/cpu.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 54 | #include "private/fixed.h" |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 55 | #include "private/format.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 56 | #include "private/lpc.h" |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 57 | #include "private/md5.h" |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 58 | #include "private/memory.h" |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 59 | #include "private/macros.h" |
Josh Coalson | f1ac7d9 | 2006-11-16 07:20:09 +0000 | [diff] [blame] | 60 | #if FLAC__HAS_OGG |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 61 | #include "private/ogg_helper.h" |
Josh Coalson | c986d13 | 2006-11-15 08:53:32 +0000 | [diff] [blame] | 62 | #include "private/ogg_mapping.h" |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 63 | #endif |
Erik de Castro Lopo | ecd0acb | 2013-10-04 01:38:00 +1000 | [diff] [blame] | 64 | #include "private/stream_encoder.h" |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 65 | #include "private/stream_encoder_framing.h" |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 66 | #include "private/window.h" |
Erik de Castro Lopo | dd1fa80 | 2013-03-10 20:39:03 +1100 | [diff] [blame] | 67 | #include "share/alloc.h" |
Erik de Castro Lopo | dd1fa80 | 2013-03-10 20:39:03 +1100 | [diff] [blame] | 68 | #include "share/private.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 69 | |
Josh Coalson | 14b184c | 2007-03-12 05:08:21 +0000 | [diff] [blame] | 70 | |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 71 | /* 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 Lopo | 59afe36 | 2012-02-04 16:13:37 +1100 | [diff] [blame] | 81 | #undef ENABLE_RICE_PARAMETER_SEARCH |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 82 | |
Josh Coalson | df4e40f | 2007-02-04 05:49:44 +0000 | [diff] [blame] | 83 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 84 | typedef struct { |
| 85 | FLAC__int32 *data[FLAC__MAX_CHANNELS]; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 86 | uint32_t size; /* of each data[] in samples */ |
| 87 | uint32_t tail; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 88 | } verify_input_fifo; |
| 89 | |
| 90 | typedef struct { |
| 91 | const FLAC__byte *data; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 92 | uint32_t capacity; |
| 93 | uint32_t bytes; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 94 | } verify_output; |
| 95 | |
| 96 | typedef enum { |
| 97 | ENCODER_IN_MAGIC = 0, |
| 98 | ENCODER_IN_METADATA = 1, |
| 99 | ENCODER_IN_AUDIO = 2 |
| 100 | } EncoderStateHint; |
| 101 | |
Erik de Castro Lopo | 8f25d68 | 2017-01-06 20:01:18 +1100 | [diff] [blame] | 102 | static const struct CompressionLevels { |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 103 | FLAC__bool do_mid_side_stereo; |
| 104 | FLAC__bool loose_mid_side_stereo; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 105 | uint32_t max_lpc_order; |
| 106 | uint32_t qlp_coeff_precision; |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 107 | FLAC__bool do_qlp_coeff_prec_search; |
| 108 | FLAC__bool do_escape_coding; |
| 109 | FLAC__bool do_exhaustive_model_search; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 110 | uint32_t min_residual_partition_order; |
| 111 | uint32_t max_residual_partition_order; |
| 112 | uint32_t rice_parameter_search_dist; |
Erik de Castro Lopo | e0fbe71 | 2014-10-15 06:47:39 +1100 | [diff] [blame] | 113 | const char *apodization; |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 114 | } compression_levels_[] = { |
Erik de Castro Lopo | e0fbe71 | 2014-10-15 06:47:39 +1100 | [diff] [blame] | 115 | { 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 Beurden | 02891da | 2014-10-20 16:33:25 +0200 | [diff] [blame] | 121 | { 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 Lopo | e0fbe71 | 2014-10-15 06:47:39 +1100 | [diff] [blame] | 124 | /* here we use locale-independent 5e-1 instead of 0.5 or 0,5 */ |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 128 | /*********************************************************************** |
| 129 | * |
| 130 | * Private class method prototypes |
| 131 | * |
| 132 | ***********************************************************************/ |
| 133 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 134 | static void set_defaults_(FLAC__StreamEncoder *encoder); |
| 135 | static void free_(FLAC__StreamEncoder *encoder); |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 136 | static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, uint32_t new_blocksize); |
| 137 | static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, uint32_t samples, FLAC__bool is_last_block); |
| 138 | static FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, FLAC__bool is_last_block); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 139 | static void update_metadata_(const FLAC__StreamEncoder *encoder); |
Josh Coalson | 15b8eb8 | 2006-10-15 05:15:55 +0000 | [diff] [blame] | 140 | #if FLAC__HAS_OGG |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 141 | static void update_ogg_metadata_(FLAC__StreamEncoder *encoder); |
Josh Coalson | 15b8eb8 | 2006-10-15 05:15:55 +0000 | [diff] [blame] | 142 | #endif |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 143 | static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block); |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 144 | static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block); |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 145 | |
| 146 | static FLAC__bool process_subframe_( |
| 147 | FLAC__StreamEncoder *encoder, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 148 | uint32_t min_partition_order, |
| 149 | uint32_t max_partition_order, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 150 | const FLAC__FrameHeader *frame_header, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 151 | uint32_t subframe_bps, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 152 | const FLAC__int32 integer_signal[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 153 | FLAC__Subframe *subframe[2], |
| 154 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2], |
| 155 | FLAC__int32 *residual[2], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 156 | uint32_t *best_subframe, |
| 157 | uint32_t *best_bits |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 158 | ); |
| 159 | |
| 160 | static FLAC__bool add_subframe_( |
| 161 | FLAC__StreamEncoder *encoder, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 162 | uint32_t blocksize, |
| 163 | uint32_t subframe_bps, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 164 | const FLAC__Subframe *subframe, |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 165 | FLAC__BitWriter *frame |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 166 | ); |
| 167 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 168 | static uint32_t evaluate_constant_subframe_( |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 169 | FLAC__StreamEncoder *encoder, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 170 | const FLAC__int32 signal, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 171 | uint32_t blocksize, |
| 172 | uint32_t subframe_bps, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 173 | FLAC__Subframe *subframe |
| 174 | ); |
| 175 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 176 | static uint32_t evaluate_fixed_subframe_( |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 177 | FLAC__StreamEncoder *encoder, |
| 178 | const FLAC__int32 signal[], |
| 179 | FLAC__int32 residual[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 180 | FLAC__uint64 abs_residual_partition_sums[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 181 | 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 Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 189 | FLAC__bool do_escape_coding, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 190 | uint32_t rice_parameter_search_dist, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 191 | FLAC__Subframe *subframe, |
| 192 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 193 | ); |
| 194 | |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 195 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 196 | static uint32_t evaluate_lpc_subframe_( |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 197 | FLAC__StreamEncoder *encoder, |
| 198 | const FLAC__int32 signal[], |
| 199 | FLAC__int32 residual[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 200 | FLAC__uint64 abs_residual_partition_sums[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 201 | uint32_t raw_bits_per_partition[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 202 | const FLAC__real lp_coeff[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 203 | 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 Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 211 | FLAC__bool do_escape_coding, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 212 | uint32_t rice_parameter_search_dist, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 213 | FLAC__Subframe *subframe, |
| 214 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 215 | ); |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 216 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 217 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 218 | static uint32_t evaluate_verbatim_subframe_( |
Erik de Castro Lopo | 59afe36 | 2012-02-04 16:13:37 +1100 | [diff] [blame] | 219 | FLAC__StreamEncoder *encoder, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 220 | const FLAC__int32 signal[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 221 | uint32_t blocksize, |
| 222 | uint32_t subframe_bps, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 223 | FLAC__Subframe *subframe |
| 224 | ); |
| 225 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 226 | static uint32_t find_best_partition_order_( |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 227 | struct FLAC__StreamEncoderPrivate *private_, |
| 228 | const FLAC__int32 residual[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 229 | FLAC__uint64 abs_residual_partition_sums[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 230 | 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 Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 238 | FLAC__bool do_escape_coding, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 239 | uint32_t rice_parameter_search_dist, |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 240 | FLAC__EntropyCodingMethod *best_ecm |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 241 | ); |
| 242 | |
| 243 | static void precompute_partition_info_sums_( |
Josh Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 244 | const FLAC__int32 residual[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 245 | FLAC__uint64 abs_residual_partition_sums[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 246 | 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 Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 251 | ); |
| 252 | |
| 253 | static void precompute_partition_info_escapes_( |
| 254 | const FLAC__int32 residual[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 255 | 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 Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 260 | ); |
| 261 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 262 | static FLAC__bool set_partitioned_rice_( |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 263 | #ifdef EXACT_RICE_BITS_CALCULATION |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 264 | const FLAC__int32 residual[], |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 265 | #endif |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 266 | const FLAC__uint64 abs_residual_partition_sums[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 267 | 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 Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 274 | const FLAC__bool search_for_escapes, |
| 275 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 276 | uint32_t *bits |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 277 | ); |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 278 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 279 | static uint32_t get_wasted_bits_(FLAC__int32 signal[], uint32_t samples); |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 280 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 281 | /* verify-related routines: */ |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 282 | static void append_to_verify_fifo_( |
| 283 | verify_input_fifo *fifo, |
| 284 | const FLAC__int32 * const input[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 285 | uint32_t input_offset, |
| 286 | uint32_t channels, |
| 287 | uint32_t wide_samples |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 288 | ); |
| 289 | |
| 290 | static void append_to_verify_fifo_interleaved_( |
| 291 | verify_input_fifo *fifo, |
| 292 | const FLAC__int32 input[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 293 | uint32_t input_offset, |
| 294 | uint32_t channels, |
| 295 | uint32_t wide_samples |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 296 | ); |
| 297 | |
Josh Coalson | 8065a2d | 2006-10-15 08:32:56 +0000 | [diff] [blame] | 298 | static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 299 | static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); |
| 300 | static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data); |
| 301 | static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 302 | |
Josh Coalson | 8065a2d | 2006-10-15 08:32:56 +0000 | [diff] [blame] | 303 | static FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 304 | static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data); |
| 305 | static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data); |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 306 | static 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 Coalson | e3ec2ad | 2007-01-31 03:53:22 +0000 | [diff] [blame] | 307 | static FILE *get_binary_stdout_(void); |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 308 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 309 | |
| 310 | /*********************************************************************** |
| 311 | * |
| 312 | * Private class data |
| 313 | * |
| 314 | ***********************************************************************/ |
| 315 | |
| 316 | typedef struct FLAC__StreamEncoderPrivate { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 317 | uint32_t input_capacity; /* current size (in samples) of the signal and residual buffers */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 318 | 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 Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 320 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 4a040f6 | 2007-04-04 00:59:28 +0000 | [diff] [blame] | 321 | 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 Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 323 | FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */ |
Josh Coalson | 4a040f6 | 2007-04-04 00:59:28 +0000 | [diff] [blame] | 324 | FLAC__real *windowed_signal; /* the integer_signal[] * current window[] */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 325 | #endif |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 326 | 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 Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 328 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 330 | 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 Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 334 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 338 | 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 Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 342 | FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */ |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 343 | uint32_t *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */ |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 344 | FLAC__BitWriter *frame; /* the current frame being worked on */ |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 345 | 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 Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 347 | FLAC__ChannelAssignment last_channel_assignment; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 348 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 350 | uint32_t current_sample_number; |
| 351 | uint32_t current_frame_number; |
Josh Coalson | 099a289 | 2007-03-14 07:58:19 +0000 | [diff] [blame] | 352 | FLAC__MD5Context md5context; |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 353 | FLAC__CPUInfo cpuinfo; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 354 | 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 Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 355 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 356 | 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 Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 358 | #else |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 359 | 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 Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 361 | #endif |
| 362 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 363 | 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 Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 367 | #endif |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 368 | FLAC__bool disable_constant_subframes; |
| 369 | FLAC__bool disable_fixed_subframes; |
| 370 | FLAC__bool disable_verbatim_subframes; |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 371 | FLAC__bool is_ogg; |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 372 | FLAC__StreamEncoderReadCallback read_callback; /* currently only needed for Ogg FLAC */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 373 | FLAC__StreamEncoderSeekCallback seek_callback; |
| 374 | FLAC__StreamEncoderTellCallback tell_callback; |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 375 | FLAC__StreamEncoderWriteCallback write_callback; |
| 376 | FLAC__StreamEncoderMetadataCallback metadata_callback; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 377 | FLAC__StreamEncoderProgressCallback progress_callback; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 378 | void *client_data; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 379 | uint32_t first_seekpoint_to_check; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 380 | FILE *file; /* only used when encoding to a file */ |
| 381 | FLAC__uint64 bytes_written; |
| 382 | FLAC__uint64 samples_written; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 383 | uint32_t frames_written; |
| 384 | uint32_t total_frames_estimate; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 385 | /* unaligned (original) pointers to allocated data */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 386 | FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 387 | FLAC__int32 *integer_signal_mid_side_unaligned[2]; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 388 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 4a040f6 | 2007-04-04 00:59:28 +0000 | [diff] [blame] | 389 | FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) */ |
| 390 | FLAC__real *real_signal_mid_side_unaligned[2]; /* (@@@ currently unused) */ |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 391 | FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS]; |
| 392 | FLAC__real *windowed_signal_unaligned; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 393 | #endif |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 394 | FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2]; |
| 395 | FLAC__int32 *residual_workspace_mid_side_unaligned[2][2]; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 396 | FLAC__uint64 *abs_residual_partition_sums_unaligned; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 397 | uint32_t *raw_bits_per_partition_unaligned; |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 398 | /* |
| 399 | * These fields have been moved here from private function local |
| 400 | * declarations merely to save stack space during encoding. |
| 401 | */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 402 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 403 | FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 404 | #endif |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 405 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 406 | /* |
| 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 417 | uint32_t frame_number; |
| 418 | uint32_t channel; |
| 419 | uint32_t sample; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 420 | FLAC__int32 expected; |
| 421 | FLAC__int32 got; |
| 422 | } error_stats; |
| 423 | } verify; |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 424 | FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 425 | } FLAC__StreamEncoderPrivate; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 426 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 427 | /*********************************************************************** |
| 428 | * |
| 429 | * Public static class data |
| 430 | * |
| 431 | ***********************************************************************/ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 432 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 433 | FLAC_API const char * const FLAC__StreamEncoderStateString[] = { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 434 | "FLAC__STREAM_ENCODER_OK", |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 435 | "FLAC__STREAM_ENCODER_UNINITIALIZED", |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 436 | "FLAC__STREAM_ENCODER_OGG_ERROR", |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 437 | "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR", |
| 438 | "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA", |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 439 | "FLAC__STREAM_ENCODER_CLIENT_ERROR", |
| 440 | "FLAC__STREAM_ENCODER_IO_ERROR", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 441 | "FLAC__STREAM_ENCODER_FRAMING_ERROR", |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 442 | "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR" |
| 443 | }; |
| 444 | |
| 445 | FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = { |
| 446 | "FLAC__STREAM_ENCODER_INIT_STATUS_OK", |
| 447 | "FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR", |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 448 | "FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER", |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 449 | "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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 456 | "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 Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 460 | }; |
| 461 | |
Erik de Castro Lopo | 70be52b | 2012-11-28 21:49:47 +1100 | [diff] [blame] | 462 | FLAC_API const char * const FLAC__StreamEncoderReadStatusString[] = { |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 463 | "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 Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 469 | FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = { |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 470 | "FLAC__STREAM_ENCODER_WRITE_STATUS_OK", |
| 471 | "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 472 | }; |
| 473 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 474 | FLAC_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 | |
| 480 | FLAC_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 Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 486 | /* 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 497 | static const uint32_t OVERREAD_ = 1; |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 498 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 499 | /*********************************************************************** |
| 500 | * |
| 501 | * Class constructor/destructor |
| 502 | * |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 503 | */ |
Josh Coalson | e3ec2ad | 2007-01-31 03:53:22 +0000 | [diff] [blame] | 504 | FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 505 | { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 506 | FLAC__StreamEncoder *encoder; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 507 | uint32_t i; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 508 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 509 | FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 510 | |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 511 | encoder = calloc(1, sizeof(FLAC__StreamEncoder)); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 512 | if(encoder == 0) { |
| 513 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 514 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 515 | |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 516 | encoder->protected_ = calloc(1, sizeof(FLAC__StreamEncoderProtected)); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 517 | if(encoder->protected_ == 0) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 518 | free(encoder); |
| 519 | return 0; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 520 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 521 | |
Erik de Castro Lopo | 6c2040d | 2012-04-04 21:29:25 +1000 | [diff] [blame] | 522 | encoder->private_ = calloc(1, sizeof(FLAC__StreamEncoderPrivate)); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 523 | if(encoder->private_ == 0) { |
| 524 | free(encoder->protected_); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 525 | free(encoder); |
| 526 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 527 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 528 | |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 529 | encoder->private_->frame = FLAC__bitwriter_new(); |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 530 | if(encoder->private_->frame == 0) { |
| 531 | free(encoder->private_); |
| 532 | free(encoder->protected_); |
| 533 | free(encoder); |
| 534 | return 0; |
| 535 | } |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 536 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 537 | encoder->private_->file = 0; |
| 538 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 539 | set_defaults_(encoder); |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 540 | |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 541 | encoder->private_->is_being_deleted = false; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 542 | |
| 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 Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 552 | 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 Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 554 | } |
| 555 | for(i = 0; i < 2; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 556 | 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 Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 561 | 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 Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 563 | } |
| 564 | for(i = 0; i < 2; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 565 | 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 Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 567 | } |
| 568 | for(i = 0; i < 2; i++) |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 569 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 570 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 571 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 572 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 573 | return encoder; |
| 574 | } |
| 575 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 576 | FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 577 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 578 | uint32_t i; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 579 | |
Erik de Castro Lopo | feab11e | 2012-06-22 08:39:20 +1000 | [diff] [blame] | 580 | if (encoder == NULL) |
| 581 | return ; |
| 582 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 583 | FLAC__ASSERT(0 != encoder->protected_); |
| 584 | FLAC__ASSERT(0 != encoder->private_); |
| 585 | FLAC__ASSERT(0 != encoder->private_->frame); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 586 | |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 587 | encoder->private_->is_being_deleted = true; |
| 588 | |
Josh Coalson | a586226 | 2006-11-09 06:58:26 +0000 | [diff] [blame] | 589 | (void)FLAC__stream_encoder_finish(encoder); |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 590 | |
Josh Coalson | 4fa9059 | 2002-12-04 07:01:37 +0000 | [diff] [blame] | 591 | if(0 != encoder->private_->verify.decoder) |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 592 | FLAC__stream_decoder_delete(encoder->private_->verify.decoder); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 593 | |
| 594 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 595 | 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 Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 597 | } |
| 598 | for(i = 0; i < 2; i++) { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 599 | 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 Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 601 | } |
| 602 | for(i = 0; i < 2; i++) |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 603 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 604 | |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 605 | FLAC__bitwriter_delete(encoder->private_->frame); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 606 | free(encoder->private_); |
| 607 | free(encoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 608 | free(encoder); |
| 609 | } |
| 610 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 611 | /*********************************************************************** |
| 612 | * |
| 613 | * Public class methods |
| 614 | * |
| 615 | ***********************************************************************/ |
| 616 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 617 | static 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 Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 627 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 628 | uint32_t i; |
Josh Coalson | 3957c47 | 2006-09-24 16:25:42 +0000 | [diff] [blame] | 629 | FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 630 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 631 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 632 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 633 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 634 | return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 635 | |
Erik de Castro Lopo | 1be4f95 | 2016-03-20 19:28:41 +1100 | [diff] [blame] | 636 | if(FLAC__HAS_OGG == 0 && is_ogg) |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 637 | return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER; |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 638 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 639 | if(0 == write_callback || (seek_callback && 0 == tell_callback)) |
| 640 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 641 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 642 | if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 643 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS; |
Josh Coalson | 69f1ee0 | 2001-01-24 00:54:43 +0000 | [diff] [blame] | 644 | |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 645 | 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 Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 651 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 652 | if(encoder->protected_->bits_per_sample >= 32) |
luz.paz | 01eb197 | 2018-05-22 10:17:53 -0400 | [diff] [blame] | 653 | encoder->protected_->do_mid_side_stereo = false; /* since we currently do 32-bit math, the side channel would have 33 bps and overflow */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 654 | |
Josh Coalson | 76c68bc | 2002-05-17 06:22:02 +0000 | [diff] [blame] | 655 | if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 656 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 657 | |
Josh Coalson | 0833f34 | 2002-07-15 05:31:55 +0000 | [diff] [blame] | 658 | if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate)) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 659 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 660 | |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 661 | if(encoder->protected_->blocksize == 0) { |
| 662 | if(encoder->protected_->max_lpc_order == 0) |
| 663 | encoder->protected_->blocksize = 1152; |
| 664 | else |
Josh Coalson | dc2b03b | 2007-02-06 05:02:46 +0000 | [diff] [blame] | 665 | encoder->protected_->blocksize = 4096; |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 668 | if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 669 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 670 | |
Josh Coalson | 20ac2c1 | 2002-08-30 05:47:14 +0000 | [diff] [blame] | 671 | if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 672 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER; |
Josh Coalson | 20ac2c1 | 2002-08-30 05:47:14 +0000 | [diff] [blame] | 673 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 674 | if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 675 | return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 676 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 677 | if(encoder->protected_->qlp_coeff_precision == 0) { |
| 678 | if(encoder->protected_->bits_per_sample < 16) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 679 | /* @@@ 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Ãguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 681 | encoder->protected_->qlp_coeff_precision = flac_max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 682 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 683 | 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 Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 696 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 697 | encoder->protected_->qlp_coeff_precision = 13; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 698 | } |
| 699 | else { |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 700 | 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 Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 706 | } |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 707 | FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 708 | } |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 709 | else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 710 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 711 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 712 | if(encoder->protected_->streamable_subset) { |
Josh Coalson | d8a6f4a | 2008-05-27 05:20:15 +0000 | [diff] [blame] | 713 | if(!FLAC__format_blocksize_is_subset(encoder->protected_->blocksize, encoder->protected_->sample_rate)) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 714 | return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
Josh Coalson | 2258fa8 | 2007-03-30 01:02:40 +0000 | [diff] [blame] | 715 | if(!FLAC__format_sample_rate_is_subset(encoder->protected_->sample_rate)) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 716 | return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
Josh Coalson | 20ac2c1 | 2002-08-30 05:47:14 +0000 | [diff] [blame] | 717 | 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 724 | return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
Josh Coalson | c1c8d49 | 2002-09-26 04:42:10 +0000 | [diff] [blame] | 725 | if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 726 | return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE; |
Josh Coalson | d0edb97 | 2006-10-07 06:50:08 +0000 | [diff] [blame] | 727 | 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 Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 738 | 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 Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 742 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 743 | #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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 746 | uint32_t i1; |
Erik de Castro Lopo | 96a5ba7 | 2013-03-10 22:03:47 +1100 | [diff] [blame] | 747 | 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 Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 752 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 760 | uint32_t i2; |
Erik de Castro Lopo | 96a5ba7 | 2013-03-10 22:03:47 +1100 | [diff] [blame] | 761 | 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 Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 764 | break; /* take only the first one */ |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 769 | /* validate metadata */ |
| 770 | if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 771 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 772 | metadata_has_seektable = false; |
| 773 | metadata_has_vorbis_comment = false; |
Josh Coalson | 3957c47 | 2006-09-24 16:25:42 +0000 | [diff] [blame] | 774 | metadata_picture_has_type1 = false; |
| 775 | metadata_picture_has_type2 = false; |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 776 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
Josh Coalson | 3957c47 | 2006-09-24 16:25:42 +0000 | [diff] [blame] | 777 | const FLAC__StreamMetadata *m = encoder->protected_->metadata[i]; |
| 778 | if(m->type == FLAC__METADATA_TYPE_STREAMINFO) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 779 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
Josh Coalson | 3957c47 | 2006-09-24 16:25:42 +0000 | [diff] [blame] | 780 | else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) { |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 781 | if(metadata_has_seektable) /* only one is allowed */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 782 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 783 | metadata_has_seektable = true; |
Josh Coalson | 3957c47 | 2006-09-24 16:25:42 +0000 | [diff] [blame] | 784 | if(!FLAC__format_seektable_is_legal(&m->data.seek_table)) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 785 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 786 | } |
Josh Coalson | 3957c47 | 2006-09-24 16:25:42 +0000 | [diff] [blame] | 787 | else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) { |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 788 | if(metadata_has_vorbis_comment) /* only one is allowed */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 789 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 790 | metadata_has_vorbis_comment = true; |
| 791 | } |
Josh Coalson | 3957c47 | 2006-09-24 16:25:42 +0000 | [diff] [blame] | 792 | 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 794 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 795 | } |
Josh Coalson | 3957c47 | 2006-09-24 16:25:42 +0000 | [diff] [blame] | 796 | else if(m->type == FLAC__METADATA_TYPE_PICTURE) { |
| 797 | if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0)) |
Josh Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 798 | return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA; |
Josh Coalson | 3957c47 | 2006-09-24 16:25:42 +0000 | [diff] [blame] | 799 | 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 Lopo | 59afe36 | 2012-02-04 16:13:37 +1100 | [diff] [blame] | 805 | m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD && |
Josh Coalson | 3957c47 | 2006-09-24 16:25:42 +0000 | [diff] [blame] | 806 | ( |
| 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 Coalson | e343ab2 | 2006-09-23 19:21:19 +0000 | [diff] [blame] | 819 | } |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 822 | 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 Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 825 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 826 | encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 827 | #endif |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 828 | } |
| 829 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 830 | encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 831 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 832 | encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 833 | #endif |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 834 | } |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 835 | #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 Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 840 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 844 | } |
| 845 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 846 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 849 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 850 | 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 Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 852 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 853 | encoder->private_->loose_mid_side_stereo_frames = (uint32_t)((double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize + 0.5); |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 854 | #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÷ 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 861 | 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 Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 862 | #endif |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 863 | 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 Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 868 | |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 869 | /* |
| 870 | * get the CPU info and set the function pointers |
| 871 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 872 | FLAC__cpu_info(&encoder->private_->cpuinfo); |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 873 | /* first default to the non-asm routines */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 874 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 875 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 876 | #endif |
Erik de Castro Lopo | ecd0acb | 2013-10-04 01:38:00 +1000 | [diff] [blame] | 877 | encoder->private_->local_precompute_partition_info_sums = precompute_partition_info_sums_; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 878 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor; |
Erik de Castro Lopo | 93f6109 | 2014-04-11 06:21:09 +1000 | [diff] [blame] | 879 | encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 880 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 881 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients; |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 882 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 883 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 884 | #endif |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 885 | /* now override with asm where appropriate */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 886 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 887 | # ifndef FLAC__NO_ASM |
Anton Blanchard | cdb030c | 2018-07-07 14:21:46 +1000 | [diff] [blame] | 888 | #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 Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 918 | if(encoder->private_->cpuinfo.use_asm) { |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 919 | # ifdef FLAC__CPU_IA32 |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 920 | FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 921 | # ifdef FLAC__HAS_NASM |
Erik de Castro Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 922 | if (encoder->private_->cpuinfo.x86.sse) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 923 | if(encoder->protected_->max_lpc_order < 4) |
Erik de Castro Lopo | ac0b4b4 | 2014-12-04 21:03:01 +1100 | [diff] [blame] | 924 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4_old; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 925 | else if(encoder->protected_->max_lpc_order < 8) |
Erik de Castro Lopo | ac0b4b4 | 2014-12-04 21:03:01 +1100 | [diff] [blame] | 926 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8_old; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 927 | else if(encoder->protected_->max_lpc_order < 12) |
Erik de Castro Lopo | ac0b4b4 | 2014-12-04 21:03:01 +1100 | [diff] [blame] | 928 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12_old; |
Erik de Castro Lopo | deb2099 | 2013-08-31 13:40:47 +1000 | [diff] [blame] | 929 | else if(encoder->protected_->max_lpc_order < 16) |
Erik de Castro Lopo | ac0b4b4 | 2014-12-04 21:03:01 +1100 | [diff] [blame] | 930 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_16_old; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 931 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 932 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 933 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 934 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 935 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
Erik de Castro Lopo | 3d39492 | 2014-05-14 21:27:31 +1000 | [diff] [blame] | 936 | |
| 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 Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 938 | if (encoder->private_->cpuinfo.x86.mmx) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 939 | 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 Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 941 | } |
| 942 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 943 | 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 Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 945 | } |
Erik de Castro Lopo | 3d39492 | 2014-05-14 21:27:31 +1000 | [diff] [blame] | 946 | |
Erik de Castro Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 947 | if (encoder->private_->cpuinfo.x86.mmx && encoder->private_->cpuinfo.x86.cmov) |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 948 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov; |
| 949 | # endif /* FLAC__HAS_NASM */ |
Erik de Castro Lopo | 36a0ab1 | 2016-06-20 20:29:59 +1000 | [diff] [blame] | 950 | # if FLAC__HAS_X86INTRIN |
Erik de Castro Lopo | 3d39492 | 2014-05-14 21:27:31 +1000 | [diff] [blame] | 951 | # if defined FLAC__SSE_SUPPORTED |
Erik de Castro Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 952 | 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 Lopo | ac0b4b4 | 2014-12-04 21:03:01 +1100 | [diff] [blame] | 954 | 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 Lopo | d163ef4 | 2014-02-01 20:34:51 +1100 | [diff] [blame] | 977 | } |
| 978 | # endif |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 979 | |
Erik de Castro Lopo | d40e986 | 2014-01-30 21:49:51 +1100 | [diff] [blame] | 980 | # ifdef FLAC__SSE2_SUPPORTED |
Erik de Castro Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 981 | if (encoder->private_->cpuinfo.x86.sse2) { |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 982 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse2; |
Erik de Castro Lopo | ecd0acb | 2013-10-04 01:38:00 +1000 | [diff] [blame] | 983 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_sse2; |
| 984 | } |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 985 | # endif |
| 986 | # ifdef FLAC__SSE4_1_SUPPORTED |
Erik de Castro Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 987 | if (encoder->private_->cpuinfo.x86.sse41) { |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 988 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse41; |
Erik de Castro Lopo | 3d39492 | 2014-05-14 21:27:31 +1000 | [diff] [blame] | 989 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_sse41; |
| 990 | } |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 991 | # endif |
Erik de Castro Lopo | 11b5d51 | 2014-10-04 09:42:54 +1000 | [diff] [blame] | 992 | # ifdef FLAC__AVX2_SUPPORTED |
Erik de Castro Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 993 | if (encoder->private_->cpuinfo.x86.avx2) { |
Erik de Castro Lopo | 11b5d51 | 2014-10-04 09:42:54 +1000 | [diff] [blame] | 994 | 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 Lopo | 3d39492 | 2014-05-14 21:27:31 +1000 | [diff] [blame] | 999 | |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1000 | # ifdef FLAC__SSE2_SUPPORTED |
Erik de Castro Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 1001 | if (encoder->private_->cpuinfo.x86.sse2) { |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1002 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_intrin_sse2; |
Erik de Castro Lopo | 93f6109 | 2014-04-11 06:21:09 +1000 | [diff] [blame] | 1003 | encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide_intrin_sse2; |
| 1004 | } |
Erik de Castro Lopo | d40e986 | 2014-01-30 21:49:51 +1100 | [diff] [blame] | 1005 | # endif |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1006 | # ifdef FLAC__SSSE3_SUPPORTED |
Erik de Castro Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 1007 | if (encoder->private_->cpuinfo.x86.ssse3) { |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1008 | 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 Lopo | ecd0acb | 2013-10-04 01:38:00 +1000 | [diff] [blame] | 1012 | # endif /* FLAC__HAS_X86INTRIN */ |
Erik de Castro Lopo | b8d58e3 | 2014-06-15 20:29:34 +1000 | [diff] [blame] | 1013 | # elif defined FLAC__CPU_X86_64 |
Erik de Castro Lopo | 5e5ee27 | 2013-09-15 09:51:28 +1000 | [diff] [blame] | 1014 | FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_X86_64); |
Erik de Castro Lopo | 36a0ab1 | 2016-06-20 20:29:59 +1000 | [diff] [blame] | 1015 | # if FLAC__HAS_X86INTRIN |
Erik de Castro Lopo | d40e986 | 2014-01-30 21:49:51 +1100 | [diff] [blame] | 1016 | # ifdef FLAC__SSE_SUPPORTED |
Erik de Castro Lopo | ac0b4b4 | 2014-12-04 21:03:01 +1100 | [diff] [blame] | 1017 | 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 Lopo | d40e986 | 2014-01-30 21:49:51 +1100 | [diff] [blame] | 1037 | # endif |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1038 | |
Erik de Castro Lopo | d40e986 | 2014-01-30 21:49:51 +1100 | [diff] [blame] | 1039 | # ifdef FLAC__SSE2_SUPPORTED |
Erik de Castro Lopo | 5e5ee27 | 2013-09-15 09:51:28 +1000 | [diff] [blame] | 1040 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_sse2; |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1041 | # endif |
| 1042 | # ifdef FLAC__SSE4_1_SUPPORTED |
Erik de Castro Lopo | 35c14b1 | 2014-10-04 09:05:08 +1000 | [diff] [blame] | 1043 | if(encoder->private_->cpuinfo.x86.sse41) { |
Erik de Castro Lopo | 02591f6 | 2014-07-28 20:43:54 +1000 | [diff] [blame] | 1044 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse41; |
| 1045 | } |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1046 | # endif |
Erik de Castro Lopo | 11b5d51 | 2014-10-04 09:42:54 +1000 | [diff] [blame] | 1047 | # 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 Lopo | 3d39492 | 2014-05-14 21:27:31 +1000 | [diff] [blame] | 1054 | |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1055 | # 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 Lopo | 35c14b1 | 2014-10-04 09:05:08 +1000 | [diff] [blame] | 1060 | if (encoder->private_->cpuinfo.x86.ssse3) { |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1061 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_intrin_ssse3; |
Erik de Castro Lopo | 93f6109 | 2014-04-11 06:21:09 +1000 | [diff] [blame] | 1062 | encoder->private_->local_fixed_compute_best_predictor_wide = FLAC__fixed_compute_best_predictor_wide_intrin_ssse3; |
| 1063 | } |
Erik de Castro Lopo | d40e986 | 2014-01-30 21:49:51 +1100 | [diff] [blame] | 1064 | # endif |
Erik de Castro Lopo | 5e5ee27 | 2013-09-15 09:51:28 +1000 | [diff] [blame] | 1065 | # endif /* FLAC__HAS_X86INTRIN */ |
| 1066 | # endif /* FLAC__CPU_... */ |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 1067 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 1068 | # endif /* !FLAC__NO_ASM */ |
| 1069 | #endif /* !FLAC__INTEGER_ONLY_LIBRARY */ |
Erik de Castro Lopo | 36a0ab1 | 2016-06-20 20:29:59 +1000 | [diff] [blame] | 1070 | #if !defined FLAC__NO_ASM && FLAC__HAS_X86INTRIN |
Erik de Castro Lopo | ecd0acb | 2013-10-04 01:38:00 +1000 | [diff] [blame] | 1071 | if(encoder->private_->cpuinfo.use_asm) { |
| 1072 | # if defined FLAC__CPU_IA32 |
Erik de Castro Lopo | ace63cc | 2014-02-25 18:28:22 +1100 | [diff] [blame] | 1073 | # ifdef FLAC__SSE2_SUPPORTED |
Erik de Castro Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 1074 | if (encoder->private_->cpuinfo.x86.sse2) |
Erik de Castro Lopo | 6cd8b42 | 2014-01-07 21:14:55 +1100 | [diff] [blame] | 1075 | encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_sse2; |
Erik de Castro Lopo | d40e986 | 2014-01-30 21:49:51 +1100 | [diff] [blame] | 1076 | # endif |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1077 | # ifdef FLAC__SSSE3_SUPPORTED |
Erik de Castro Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 1078 | if (encoder->private_->cpuinfo.x86.ssse3) |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1079 | encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_ssse3; |
| 1080 | # endif |
Erik de Castro Lopo | 11b5d51 | 2014-10-04 09:42:54 +1000 | [diff] [blame] | 1081 | # ifdef FLAC__AVX2_SUPPORTED |
Erik de Castro Lopo | 61e7dfa | 2017-02-27 20:35:21 +1100 | [diff] [blame] | 1082 | if (encoder->private_->cpuinfo.x86.avx2) |
Erik de Castro Lopo | 11b5d51 | 2014-10-04 09:42:54 +1000 | [diff] [blame] | 1083 | encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_avx2; |
| 1084 | # endif |
Erik de Castro Lopo | b8d58e3 | 2014-06-15 20:29:34 +1000 | [diff] [blame] | 1085 | # elif defined FLAC__CPU_X86_64 |
Erik de Castro Lopo | ace63cc | 2014-02-25 18:28:22 +1100 | [diff] [blame] | 1086 | # ifdef FLAC__SSE2_SUPPORTED |
Erik de Castro Lopo | ad2ae45 | 2014-10-04 09:13:07 +1000 | [diff] [blame] | 1087 | encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_sse2; |
| 1088 | # endif |
| 1089 | # ifdef FLAC__SSSE3_SUPPORTED |
Erik de Castro Lopo | 35c14b1 | 2014-10-04 09:05:08 +1000 | [diff] [blame] | 1090 | if(encoder->private_->cpuinfo.x86.ssse3) |
Erik de Castro Lopo | 6cd8b42 | 2014-01-07 21:14:55 +1100 | [diff] [blame] | 1091 | encoder->private_->local_precompute_partition_info_sums = FLAC__precompute_partition_info_sums_intrin_ssse3; |
Erik de Castro Lopo | d40e986 | 2014-01-30 21:49:51 +1100 | [diff] [blame] | 1092 | # endif |
Erik de Castro Lopo | 11b5d51 | 2014-10-04 09:42:54 +1000 | [diff] [blame] | 1093 | # 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 Lopo | ecd0acb | 2013-10-04 01:38:00 +1000 | [diff] [blame] | 1097 | # endif /* FLAC__CPU_... */ |
| 1098 | } |
| 1099 | #endif /* !FLAC__NO_ASM && FLAC__HAS_X86INTRIN */ |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 1100 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1101 | /* 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 Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1104 | #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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1113 | 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 Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1119 | if(!resize_buffers_(encoder, encoder->protected_->blocksize)) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1120 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1121 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1122 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1123 | |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 1124 | if(!FLAC__bitwriter_init(encoder->private_->frame)) { |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1125 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1126 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1127 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1128 | |
| 1129 | /* |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1130 | * 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 Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 1137 | encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1138 | for(i = 0; i < encoder->protected_->channels; i++) { |
Erik de Castro Lopo | 8749dc2 | 2012-06-22 14:23:56 +1000 | [diff] [blame] | 1139 | 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1140 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 1141 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1142 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1143 | } |
| 1144 | encoder->private_->verify.input_fifo.tail = 0; |
| 1145 | |
| 1146 | /* |
| 1147 | * Now set up a stream decoder for verification |
| 1148 | */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1149 | if(0 == encoder->private_->verify.decoder) { |
Erik de Castro Lopo | 3b41451 | 2012-02-01 22:01:05 +1100 | [diff] [blame] | 1150 | 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1155 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1156 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1157 | 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1161 | } |
Josh Coalson | 589f8c7 | 2002-08-07 23:54:55 +0000 | [diff] [blame] | 1162 | 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1168 | |
| 1169 | /* |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1170 | * 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 Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1180 | * write the stream header |
| 1181 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1182 | if(encoder->protected_->verify) |
| 1183 | encoder->private_->verify.state_hint = ENCODER_IN_MAGIC; |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 1184 | if(!FLAC__bitwriter_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) { |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1185 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
| 1186 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
| 1187 | } |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 1188 | if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1189 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1190 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1191 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1192 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1193 | /* |
| 1194 | * write the STREAMINFO metadata block |
| 1195 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1196 | if(encoder->protected_->verify) |
| 1197 | encoder->private_->verify.state_hint = ENCODER_IN_METADATA; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1198 | 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 Coalson | 00da5ae | 2007-06-14 05:19:46 +0000 | [diff] [blame] | 1210 | if(encoder->protected_->do_md5) |
| 1211 | FLAC__MD5Init(&encoder->private_->md5context); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1212 | 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 Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 1216 | if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1217 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1218 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1219 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1220 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1221 | /* |
| 1222 | * Now that the STREAMINFO block is written, we can init this to an |
| 1223 | * absurdly-high value... |
| 1224 | */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1225 | encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1; |
Josh Coalson | cbbbb5f | 2001-01-23 00:41:48 +0000 | [diff] [blame] | 1226 | /* ... and clear this to 0 */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1227 | encoder->private_->streaminfo.data.stream_info.total_samples = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1228 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1229 | /* |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 1230 | * 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 Coalson | 69cfda7 | 2004-09-10 00:38:21 +0000 | [diff] [blame] | 1233 | * |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1234 | * 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 Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 1238 | */ |
| 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1248 | 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 Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 1252 | if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) { |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 1253 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1254 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
Josh Coalson | cb9d93a | 2002-08-25 05:27:15 +0000 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | /* |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1259 | * 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1263 | 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 Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 1267 | if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1268 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1269 | return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1270 | } |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1273 | /* 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1279 | if(encoder->protected_->verify) |
| 1280 | encoder->private_->verify.state_hint = ENCODER_IN_AUDIO; |
| 1281 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1282 | return FLAC__STREAM_ENCODER_INIT_STATUS_OK; |
| 1283 | } |
| 1284 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1285 | FLAC_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 | |
| 1306 | FLAC_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 Lopo | 59afe36 | 2012-02-04 16:13:37 +1100 | [diff] [blame] | 1327 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1328 | static 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1335 | { |
| 1336 | FLAC__StreamEncoderInitStatus init_status; |
| 1337 | |
| 1338 | FLAC__ASSERT(0 != encoder); |
| 1339 | FLAC__ASSERT(0 != file); |
| 1340 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1341 | 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 Coalson | 92f7fa9 | 2006-10-09 05:34:21 +0000 | [diff] [blame] | 1350 | /* |
| 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1355 | if(file == stdout) |
| 1356 | file = get_binary_stdout_(); /* just to be safe */ |
| 1357 | |
Erik de Castro Lopo | e863247 | 2014-09-28 08:43:24 +1000 | [diff] [blame] | 1358 | #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 Lopo | f0ee453 | 2015-12-12 08:23:22 +1100 | [diff] [blame] | 1363 | if(GetFileType((HANDLE)_get_osfhandle(_fileno(file))) == FILE_TYPE_DISK) |
| 1364 | setvbuf(file, NULL, _IOFBF, 10*1024*1024); |
Erik de Castro Lopo | e863247 | 2014-09-28 08:43:24 +1000 | [diff] [blame] | 1365 | #endif |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1366 | 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 Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1373 | init_status = init_stream_internal_( |
| 1374 | encoder, |
Josh Coalson | cf1422e | 2006-11-16 01:32:25 +0000 | [diff] [blame] | 1375 | encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0, |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1376 | file_write_callback_, |
Josh Coalson | cf1422e | 2006-11-16 01:32:25 +0000 | [diff] [blame] | 1377 | encoder->private_->file == stdout? 0 : file_seek_callback_, |
| 1378 | encoder->private_->file == stdout? 0 : file_tell_callback_, |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1379 | /*metadata_callback=*/0, |
| 1380 | client_data, |
| 1381 | is_ogg |
| 1382 | ); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1383 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1389 | uint32_t blocksize = FLAC__stream_encoder_get_blocksize(encoder); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1390 | |
| 1391 | FLAC__ASSERT(blocksize != 0); |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1392 | encoder->private_->total_frames_estimate = (uint32_t)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | return init_status; |
| 1396 | } |
Erik de Castro Lopo | 59afe36 | 2012-02-04 16:13:37 +1100 | [diff] [blame] | 1397 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1398 | FLAC_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 Lopo | 59afe36 | 2012-02-04 16:13:37 +1100 | [diff] [blame] | 1407 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1408 | FLAC_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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1417 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1418 | static 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1425 | { |
| 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 Lopo | 5705b4d | 2013-03-21 19:18:49 +1100 | [diff] [blame] | 1438 | file = filename? flac_fopen(filename, "w+b") : stdout; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1439 | |
| 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 Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1445 | return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg); |
| 1446 | } |
| 1447 | |
| 1448 | FLAC_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 | |
| 1458 | FLAC_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 Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Josh Coalson | a586226 | 2006-11-09 06:58:26 +0000 | [diff] [blame] | 1468 | FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1469 | { |
Josh Coalson | cf1422e | 2006-11-16 01:32:25 +0000 | [diff] [blame] | 1470 | FLAC__bool error = false; |
Josh Coalson | a586226 | 2006-11-09 06:58:26 +0000 | [diff] [blame] | 1471 | |
Erik de Castro Lopo | 006dcfb | 2017-01-31 06:39:22 +1100 | [diff] [blame] | 1472 | if (encoder == NULL) |
| 1473 | return false; |
| 1474 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1475 | FLAC__ASSERT(0 != encoder->private_); |
| 1476 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 1477 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1478 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | a586226 | 2006-11-09 06:58:26 +0000 | [diff] [blame] | 1479 | return true; |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 1480 | |
Josh Coalson | 3262b0d | 2002-08-14 20:58:42 +0000 | [diff] [blame] | 1481 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) { |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 1482 | if(encoder->private_->current_sample_number != 0) { |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 1483 | const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number; |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 1484 | encoder->protected_->blocksize = encoder->private_->current_sample_number; |
Josh Coalson | cf1422e | 2006-11-16 01:32:25 +0000 | [diff] [blame] | 1485 | if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true)) |
| 1486 | error = true; |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 1487 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1488 | } |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 1489 | |
Josh Coalson | 00da5ae | 2007-06-14 05:19:46 +0000 | [diff] [blame] | 1490 | if(encoder->protected_->do_md5) |
| 1491 | FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context); |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame] | 1492 | |
Josh Coalson | cf1422e | 2006-11-16 01:32:25 +0000 | [diff] [blame] | 1493 | if(!encoder->private_->is_being_deleted) { |
| 1494 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) { |
| 1495 | if(encoder->private_->seek_callback) { |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1496 | #if FLAC__HAS_OGG |
Josh Coalson | cf1422e | 2006-11-16 01:32:25 +0000 | [diff] [blame] | 1497 | if(encoder->private_->is_ogg) |
| 1498 | update_ogg_metadata_(encoder); |
| 1499 | else |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1500 | #endif |
Josh Coalson | cf1422e | 2006-11-16 01:32:25 +0000 | [diff] [blame] | 1501 | update_metadata_(encoder); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1502 | |
Josh Coalson | cf1422e | 2006-11-16 01:32:25 +0000 | [diff] [blame] | 1503 | /* 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1517 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 1518 | if(0 != encoder->private_->file) { |
| 1519 | if(encoder->private_->file != stdout) |
| 1520 | fclose(encoder->private_->file); |
| 1521 | encoder->private_->file = 0; |
| 1522 | } |
| 1523 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1524 | #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 Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1529 | free_(encoder); |
| 1530 | set_defaults_(encoder); |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1531 | |
Josh Coalson | cf1422e | 2006-11-16 01:32:25 +0000 | [diff] [blame] | 1532 | if(!error) |
Josh Coalson | a586226 | 2006-11-09 06:58:26 +0000 | [diff] [blame] | 1533 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
| 1534 | |
Josh Coalson | cf1422e | 2006-11-16 01:32:25 +0000 | [diff] [blame] | 1535 | return !error; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Josh Coalson | 71d5c25 | 2006-10-15 06:04:55 +0000 | [diff] [blame] | 1538 | FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value) |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1539 | { |
| 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 Coalson | f1ac7d9 | 2006-11-16 07:20:09 +0000 | [diff] [blame] | 1545 | #if FLAC__HAS_OGG |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1546 | /* 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 Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1555 | FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1556 | { |
| 1557 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1558 | FLAC__ASSERT(0 != encoder->private_); |
| 1559 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1560 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1561 | return false; |
Josh Coalson | 47c7b14 | 2005-01-29 06:08:58 +0000 | [diff] [blame] | 1562 | #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1563 | encoder->protected_->verify = value; |
Josh Coalson | 47c7b14 | 2005-01-29 06:08:58 +0000 | [diff] [blame] | 1564 | #endif |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1565 | return true; |
| 1566 | } |
| 1567 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1568 | FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1569 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1570 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1571 | FLAC__ASSERT(0 != encoder->private_); |
| 1572 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1573 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1574 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1575 | encoder->protected_->streamable_subset = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1576 | return true; |
| 1577 | } |
| 1578 | |
Josh Coalson | 00da5ae | 2007-06-14 05:19:46 +0000 | [diff] [blame] | 1579 | FLAC_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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1590 | FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, uint32_t value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1591 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1592 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1593 | FLAC__ASSERT(0 != encoder->private_); |
| 1594 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1595 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1596 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1597 | encoder->protected_->channels = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1598 | return true; |
| 1599 | } |
| 1600 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1601 | FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, uint32_t value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1602 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1603 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1604 | FLAC__ASSERT(0 != encoder->private_); |
| 1605 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1606 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1607 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1608 | encoder->protected_->bits_per_sample = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1609 | return true; |
| 1610 | } |
| 1611 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1612 | FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, uint32_t value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1613 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1614 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1615 | FLAC__ASSERT(0 != encoder->private_); |
| 1616 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1617 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1618 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1619 | encoder->protected_->sample_rate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1620 | return true; |
| 1621 | } |
| 1622 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1623 | FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, uint32_t value) |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 1624 | { |
| 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 Coalson | f395ec4 | 2007-06-20 01:23:02 +0000 | [diff] [blame] | 1635 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Erik de Castro Lopo | e0fbe71 | 2014-10-15 06:47:39 +1100 | [diff] [blame] | 1636 | #if 1 |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 1637 | ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization); |
Josh Coalson | 4e8fe85 | 2006-12-05 01:36:46 +0000 | [diff] [blame] | 1638 | #else |
Erik de Castro Lopo | e0fbe71 | 2014-10-15 06:47:39 +1100 | [diff] [blame] | 1639 | /* equivalent to -A tukey(0.5) */ |
Josh Coalson | 4e8fe85 | 2006-12-05 01:36:46 +0000 | [diff] [blame] | 1640 | 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 Coalson | f395ec4 | 2007-06-20 01:23:02 +0000 | [diff] [blame] | 1644 | #endif |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 1645 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1656 | FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, uint32_t value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1657 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1658 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1659 | FLAC__ASSERT(0 != encoder->private_); |
| 1660 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1661 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1662 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1663 | encoder->protected_->blocksize = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1664 | return true; |
| 1665 | } |
| 1666 | |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 1667 | FLAC_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 | |
| 1678 | FLAC_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 Coalson | 504dcaf | 2007-09-13 15:42:47 +0000 | [diff] [blame] | 1689 | /*@@@@add to tests*/ |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 1690 | FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification) |
| 1691 | { |
| 1692 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1693 | FLAC__ASSERT(0 != encoder->private_); |
| 1694 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 1695 | 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 Beurden | 29a2833 | 2014-08-10 10:59:29 +0200 | [diff] [blame] | 1743 | 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 Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 1785 | 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 Coalson | 8238936 | 2006-05-01 05:58:35 +0000 | [diff] [blame] | 1796 | encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY; |
| 1797 | encoder->protected_->apodizations[0].parameters.tukey.p = 0.5; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 1798 | } |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 1799 | #endif |
| 1800 | return true; |
| 1801 | } |
| 1802 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1803 | FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, uint32_t value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1804 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1805 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1806 | FLAC__ASSERT(0 != encoder->private_); |
| 1807 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1808 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1809 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1810 | encoder->protected_->max_lpc_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1811 | return true; |
| 1812 | } |
| 1813 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1814 | FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, uint32_t value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1815 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1816 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1817 | FLAC__ASSERT(0 != encoder->private_); |
| 1818 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1819 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1820 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1821 | encoder->protected_->qlp_coeff_precision = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1822 | return true; |
| 1823 | } |
| 1824 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1825 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1826 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1827 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1828 | FLAC__ASSERT(0 != encoder->private_); |
| 1829 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1830 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1831 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1832 | encoder->protected_->do_qlp_coeff_prec_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1833 | return true; |
| 1834 | } |
| 1835 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1836 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1837 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1838 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1839 | FLAC__ASSERT(0 != encoder->private_); |
| 1840 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1841 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1842 | return false; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 1843 | #if 0 |
| 1844 | /*@@@ deprecated: */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1845 | encoder->protected_->do_escape_coding = value; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 1846 | #else |
| 1847 | (void)value; |
| 1848 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1849 | return true; |
| 1850 | } |
| 1851 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1852 | FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1853 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1854 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1855 | FLAC__ASSERT(0 != encoder->private_); |
| 1856 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1857 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1858 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1859 | encoder->protected_->do_exhaustive_model_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1860 | return true; |
| 1861 | } |
| 1862 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1863 | FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, uint32_t value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1864 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1865 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1866 | FLAC__ASSERT(0 != encoder->private_); |
| 1867 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1868 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1869 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1870 | encoder->protected_->min_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1871 | return true; |
| 1872 | } |
| 1873 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1874 | FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, uint32_t value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1875 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1876 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1877 | FLAC__ASSERT(0 != encoder->private_); |
| 1878 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1879 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1880 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1881 | encoder->protected_->max_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1882 | return true; |
| 1883 | } |
| 1884 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1885 | FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, uint32_t value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1886 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1887 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1888 | FLAC__ASSERT(0 != encoder->private_); |
| 1889 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1890 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1891 | return false; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 1892 | #if 0 |
| 1893 | /*@@@ deprecated: */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1894 | encoder->protected_->rice_parameter_search_dist = value; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 1895 | #else |
| 1896 | (void)value; |
| 1897 | #endif |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1898 | return true; |
| 1899 | } |
| 1900 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1901 | FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1902 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1903 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1904 | FLAC__ASSERT(0 != encoder->private_); |
| 1905 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1906 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1907 | return false; |
Erik de Castro Lopo | 262505b | 2016-01-09 10:46:41 +1100 | [diff] [blame] | 1908 | value = flac_min(value, (FLAC__U64L(1) << FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN) - 1); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1909 | encoder->protected_->total_samples_estimate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1910 | return true; |
| 1911 | } |
| 1912 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 1913 | FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, uint32_t num_blocks) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1914 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1915 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1916 | FLAC__ASSERT(0 != encoder->private_); |
| 1917 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1918 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1919 | return false; |
Josh Coalson | 1c03407 | 2007-01-29 08:27:25 +0000 | [diff] [blame] | 1920 | 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 Lopo | 8749dc2 | 2012-06-22 14:23:56 +1000 | [diff] [blame] | 1932 | if(0 == (m = safe_malloc_mul_2op_p(sizeof(m[0]), /*times*/num_blocks))) |
Josh Coalson | 1c03407 | 2007-01-29 08:27:25 +0000 | [diff] [blame] | 1933 | 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 Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1938 | #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 Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 1942 | return true; |
| 1943 | } |
| 1944 | |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1945 | /* |
luz.paz | 01eb197 | 2018-05-22 10:17:53 -0400 | [diff] [blame] | 1946 | * These three functions are not static, but not publicly exposed in |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1947 | * include/FLAC/ either. They are used by the test suite. |
| 1948 | */ |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1949 | FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1950 | { |
| 1951 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1952 | FLAC__ASSERT(0 != encoder->private_); |
| 1953 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1954 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1955 | return false; |
| 1956 | encoder->private_->disable_constant_subframes = value; |
| 1957 | return true; |
| 1958 | } |
| 1959 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1960 | FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1961 | { |
| 1962 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1963 | FLAC__ASSERT(0 != encoder->private_); |
| 1964 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1965 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1966 | return false; |
| 1967 | encoder->private_->disable_fixed_subframes = value; |
| 1968 | return true; |
| 1969 | } |
| 1970 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1971 | FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1972 | { |
| 1973 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1974 | FLAC__ASSERT(0 != encoder->private_); |
| 1975 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 1976 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 1977 | return false; |
| 1978 | encoder->private_->disable_verbatim_subframes = value; |
| 1979 | return true; |
| 1980 | } |
| 1981 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1982 | FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1983 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1984 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1985 | FLAC__ASSERT(0 != encoder->private_); |
| 1986 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1987 | return encoder->protected_->state; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 1990 | FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder) |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1991 | { |
| 1992 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1993 | FLAC__ASSERT(0 != encoder->private_); |
| 1994 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1995 | 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 Coalson | 0295422 | 2002-11-08 06:16:31 +0000 | [diff] [blame] | 2001 | FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder) |
| 2002 | { |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2003 | FLAC__ASSERT(0 != encoder); |
| 2004 | FLAC__ASSERT(0 != encoder->private_); |
| 2005 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | 0295422 | 2002-11-08 06:16:31 +0000 | [diff] [blame] | 2006 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) |
| 2007 | return FLAC__StreamEncoderStateString[encoder->protected_->state]; |
| 2008 | else |
Josh Coalson | 807140d | 2003-09-24 22:10:51 +0000 | [diff] [blame] | 2009 | return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder); |
Josh Coalson | 0295422 | 2002-11-08 06:16:31 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2012 | FLAC_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 Coalson | 589f8c7 | 2002-08-07 23:54:55 +0000 | [diff] [blame] | 2013 | { |
| 2014 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2015 | FLAC__ASSERT(0 != encoder->private_); |
| 2016 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | 589f8c7 | 2002-08-07 23:54:55 +0000 | [diff] [blame] | 2017 | 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 Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 2031 | FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder) |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2032 | { |
| 2033 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2034 | FLAC__ASSERT(0 != encoder->private_); |
| 2035 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2036 | return encoder->protected_->verify; |
| 2037 | } |
| 2038 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 2039 | FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2040 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2041 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2042 | FLAC__ASSERT(0 != encoder->private_); |
| 2043 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2044 | return encoder->protected_->streamable_subset; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Josh Coalson | 00da5ae | 2007-06-14 05:19:46 +0000 | [diff] [blame] | 2047 | FLAC_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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2055 | FLAC_API uint32_t FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2056 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2057 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2058 | FLAC__ASSERT(0 != encoder->private_); |
| 2059 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2060 | return encoder->protected_->channels; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2063 | FLAC_API uint32_t FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2064 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2065 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2066 | FLAC__ASSERT(0 != encoder->private_); |
| 2067 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2068 | return encoder->protected_->bits_per_sample; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2071 | FLAC_API uint32_t FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2072 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2073 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2074 | FLAC__ASSERT(0 != encoder->private_); |
| 2075 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2076 | return encoder->protected_->sample_rate; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2079 | FLAC_API uint32_t FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2080 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2081 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2082 | FLAC__ASSERT(0 != encoder->private_); |
| 2083 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2084 | return encoder->protected_->blocksize; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Josh Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 2087 | FLAC_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 | |
| 2095 | FLAC_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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2103 | FLAC_API uint32_t FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2104 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2105 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2106 | FLAC__ASSERT(0 != encoder->private_); |
| 2107 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2108 | return encoder->protected_->max_lpc_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2111 | FLAC_API uint32_t FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2112 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2113 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2114 | FLAC__ASSERT(0 != encoder->private_); |
| 2115 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2116 | return encoder->protected_->qlp_coeff_precision; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 2119 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2120 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2121 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2122 | FLAC__ASSERT(0 != encoder->private_); |
| 2123 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2124 | return encoder->protected_->do_qlp_coeff_prec_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 2127 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2128 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2129 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2130 | FLAC__ASSERT(0 != encoder->private_); |
| 2131 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2132 | return encoder->protected_->do_escape_coding; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 2135 | FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2136 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2137 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2138 | FLAC__ASSERT(0 != encoder->private_); |
| 2139 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2140 | return encoder->protected_->do_exhaustive_model_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2143 | FLAC_API uint32_t FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2144 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2145 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2146 | FLAC__ASSERT(0 != encoder->private_); |
| 2147 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2148 | return encoder->protected_->min_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2151 | FLAC_API uint32_t FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2152 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2153 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2154 | FLAC__ASSERT(0 != encoder->private_); |
| 2155 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2156 | return encoder->protected_->max_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2159 | FLAC_API uint32_t FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2160 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2161 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2162 | FLAC__ASSERT(0 != encoder->private_); |
| 2163 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2164 | return encoder->protected_->rice_parameter_search_dist; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 2167 | FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 3a7b2c9 | 2002-08-02 07:38:20 +0000 | [diff] [blame] | 2168 | { |
| 2169 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2170 | FLAC__ASSERT(0 != encoder->private_); |
| 2171 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | 3a7b2c9 | 2002-08-02 07:38:20 +0000 | [diff] [blame] | 2172 | return encoder->protected_->total_samples_estimate; |
| 2173 | } |
| 2174 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2175 | FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], uint32_t samples) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2176 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2177 | uint32_t i, j = 0, channel; |
| 2178 | const uint32_t channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2179 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2180 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2181 | FLAC__ASSERT(0 != encoder->private_); |
| 2182 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2183 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2184 | |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2185 | do { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2186 | const uint32_t n = flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2187 | |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2188 | if(encoder->protected_->verify) |
| 2189 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, n); |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 2190 | |
Erik de Castro Lopo | c06a449 | 2015-02-17 18:35:05 +1100 | [diff] [blame] | 2191 | for(channel = 0; channel < channels; channel++) { |
| 2192 | if (buffer[channel] == NULL) { |
Erik de Castro Lopo | c06a449 | 2015-02-17 18:35:05 +1100 | [diff] [blame] | 2193 | return false; |
| 2194 | } |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2195 | memcpy(&encoder->private_->integer_signal[channel][encoder->private_->current_sample_number], &buffer[channel][j], sizeof(buffer[channel][0]) * n); |
Erik de Castro Lopo | c06a449 | 2015-02-17 18:35:05 +1100 | [diff] [blame] | 2196 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2197 | |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2198 | if(encoder->protected_->do_mid_side_stereo) { |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2199 | 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 Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2202 | 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 Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2204 | } |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 2205 | } |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2206 | else |
| 2207 | j += n; |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 2208 | |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2209 | 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 Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 2213 | 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 Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2215 | 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 Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2218 | 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 Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 2225 | } |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2226 | } while(j < samples); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2227 | |
| 2228 | return true; |
| 2229 | } |
| 2230 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2231 | FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], uint32_t samples) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2232 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2233 | uint32_t i, j, k, channel; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2234 | FLAC__int32 x, mid, side; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2235 | const uint32_t channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2236 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2237 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2238 | FLAC__ASSERT(0 != encoder->private_); |
| 2239 | FLAC__ASSERT(0 != encoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2240 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2241 | |
| 2242 | j = k = 0; |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 2243 | /* |
| 2244 | * we have several flavors of the same basic loop, optimized for |
| 2245 | * different conditions: |
| 2246 | */ |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2247 | 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Ãguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 2253 | 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2254 | |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2255 | /* "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 Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 2281 | } |
| 2282 | else { |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2283 | /* |
| 2284 | * independent channel coding: buffer each channel in inner loop |
| 2285 | */ |
| 2286 | do { |
| 2287 | if(encoder->protected_->verify) |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 2288 | 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2289 | |
Josh Coalson | dca2d1a | 2007-04-04 01:06:08 +0000 | [diff] [blame] | 2290 | /* "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 Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 2308 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2309 | |
| 2310 | return true; |
| 2311 | } |
| 2312 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2313 | /*********************************************************************** |
| 2314 | * |
| 2315 | * Private class methods |
| 2316 | * |
| 2317 | ***********************************************************************/ |
| 2318 | |
| 2319 | void set_defaults_(FLAC__StreamEncoder *encoder) |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2320 | { |
| 2321 | FLAC__ASSERT(0 != encoder); |
| 2322 | |
Josh Coalson | 47c7b14 | 2005-01-29 06:08:58 +0000 | [diff] [blame] | 2323 | #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING |
| 2324 | encoder->protected_->verify = true; |
| 2325 | #else |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2326 | encoder->protected_->verify = false; |
Josh Coalson | 47c7b14 | 2005-01-29 06:08:58 +0000 | [diff] [blame] | 2327 | #endif |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2328 | encoder->protected_->streamable_subset = true; |
Josh Coalson | 00da5ae | 2007-06-14 05:19:46 +0000 | [diff] [blame] | 2329 | encoder->protected_->do_md5 = true; |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2330 | 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 Coalson | 425609c | 2006-11-03 16:08:52 +0000 | [diff] [blame] | 2335 | encoder->protected_->blocksize = 0; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2336 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 2337 | encoder->protected_->num_apodizations = 1; |
Josh Coalson | 8238936 | 2006-05-01 05:58:35 +0000 | [diff] [blame] | 2338 | encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY; |
| 2339 | encoder->protected_->apodizations[0].parameters.tukey.p = 0.5; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2340 | #endif |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2341 | 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2353 | encoder->private_->seek_table = 0; |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 2354 | encoder->private_->disable_constant_subframes = false; |
| 2355 | encoder->private_->disable_fixed_subframes = false; |
| 2356 | encoder->private_->disable_verbatim_subframes = false; |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2357 | encoder->private_->is_ogg = false; |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2358 | encoder->private_->read_callback = 0; |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2359 | encoder->private_->write_callback = 0; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2360 | encoder->private_->seek_callback = 0; |
| 2361 | encoder->private_->tell_callback = 0; |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2362 | encoder->private_->metadata_callback = 0; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2363 | encoder->private_->progress_callback = 0; |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2364 | encoder->private_->client_data = 0; |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2365 | |
| 2366 | #if FLAC__HAS_OGG |
| 2367 | FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect); |
| 2368 | #endif |
Josh Coalson | a832ef3 | 2009-01-02 01:12:59 +0000 | [diff] [blame] | 2369 | |
| 2370 | FLAC__stream_encoder_set_compression_level(encoder, 5); |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2373 | void free_(FLAC__StreamEncoder *encoder) |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2374 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2375 | uint32_t i, channel; |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2376 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2377 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 1c03407 | 2007-01-29 08:27:25 +0000 | [diff] [blame] | 2378 | if(encoder->protected_->metadata) { |
| 2379 | free(encoder->protected_->metadata); |
| 2380 | encoder->protected_->metadata = 0; |
| 2381 | encoder->protected_->num_metadata_blocks = 0; |
| 2382 | } |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2383 | for(i = 0; i < encoder->protected_->channels; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2384 | if(0 != encoder->private_->integer_signal_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2385 | free(encoder->private_->integer_signal_unaligned[i]); |
| 2386 | encoder->private_->integer_signal_unaligned[i] = 0; |
| 2387 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2388 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2389 | if(0 != encoder->private_->real_signal_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2390 | free(encoder->private_->real_signal_unaligned[i]); |
| 2391 | encoder->private_->real_signal_unaligned[i] = 0; |
| 2392 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2393 | #endif |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2394 | } |
| 2395 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2396 | if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2397 | free(encoder->private_->integer_signal_mid_side_unaligned[i]); |
| 2398 | encoder->private_->integer_signal_mid_side_unaligned[i] = 0; |
| 2399 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2400 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2401 | if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2402 | free(encoder->private_->real_signal_mid_side_unaligned[i]); |
| 2403 | encoder->private_->real_signal_mid_side_unaligned[i] = 0; |
| 2404 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2405 | #endif |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2406 | } |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2407 | #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 Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2419 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 2420 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2421 | if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2422 | 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 Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2429 | if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2430 | 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 Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2435 | if(0 != encoder->private_->abs_residual_partition_sums_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2436 | free(encoder->private_->abs_residual_partition_sums_unaligned); |
| 2437 | encoder->private_->abs_residual_partition_sums_unaligned = 0; |
| 2438 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2439 | if(0 != encoder->private_->raw_bits_per_partition_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2440 | free(encoder->private_->raw_bits_per_partition_unaligned); |
| 2441 | encoder->private_->raw_bits_per_partition_unaligned = 0; |
| 2442 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2443 | 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 Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 2451 | FLAC__bitwriter_free(encoder->private_->frame); |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2454 | FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, uint32_t new_blocksize) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2455 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2456 | FLAC__bool ok; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2457 | uint32_t i, channel; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2458 | |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2459 | FLAC__ASSERT(new_blocksize > 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2460 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 2461 | FLAC__ASSERT(encoder->private_->current_sample_number == 0); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2462 | |
| 2463 | /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2464 | if(new_blocksize <= encoder->private_->input_capacity) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2465 | return true; |
| 2466 | |
| 2467 | ok = true; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2468 | |
Erik de Castro Lopo | 5e5ee27 | 2013-09-15 09:51:28 +1000 | [diff] [blame] | 2469 | /* 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 Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 2471 | * have a buffer of up to 3 zeroes in front (at negative indices) for |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2472 | * alignment purposes; we use 4 in front to keep the data well-aligned. |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 2473 | */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2474 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2475 | for(i = 0; ok && i < encoder->protected_->channels; i++) { |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 2476 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2477 | memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4); |
| 2478 | encoder->private_->integer_signal[i] += 4; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2479 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 4a040f6 | 2007-04-04 00:59:28 +0000 | [diff] [blame] | 2480 | #if 0 /* @@@ currently unused */ |
Josh Coalson | c549f0f | 2004-12-30 03:47:49 +0000 | [diff] [blame] | 2481 | if(encoder->protected_->max_lpc_order > 0) |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 2482 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]); |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 2483 | #endif |
Josh Coalson | 4a040f6 | 2007-04-04 00:59:28 +0000 | [diff] [blame] | 2484 | #endif |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2485 | } |
| 2486 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 2487 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2488 | memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4); |
| 2489 | encoder->private_->integer_signal_mid_side[i] += 4; |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2490 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 4a040f6 | 2007-04-04 00:59:28 +0000 | [diff] [blame] | 2491 | #if 0 /* @@@ currently unused */ |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2492 | if(encoder->protected_->max_lpc_order > 0) |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 2493 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]); |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2494 | #endif |
Josh Coalson | 4a040f6 | 2007-04-04 00:59:28 +0000 | [diff] [blame] | 2495 | #endif |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2496 | } |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2497 | #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 Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2500 | 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 Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2502 | } |
| 2503 | #endif |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2504 | for(channel = 0; ok && channel < encoder->protected_->channels; channel++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2505 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2506 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2507 | } |
| 2508 | } |
| 2509 | for(channel = 0; ok && channel < 2; channel++) { |
| 2510 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2511 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2512 | } |
| 2513 | } |
Josh Coalson | 469ba74 | 2007-02-03 02:52:32 +0000 | [diff] [blame] | 2514 | /* 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 Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 2516 | ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2517 | if(encoder->protected_->do_escape_coding) |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2518 | ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2519 | |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2520 | /* now adjust the windows if the blocksize has changed */ |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2521 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2522 | if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) { |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2523 | for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) { |
| 2524 | switch(encoder->protected_->apodizations[i].type) { |
| 2525 | case FLAC__APODIZATION_BARTLETT: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2526 | FLAC__window_bartlett(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2527 | break; |
| 2528 | case FLAC__APODIZATION_BARTLETT_HANN: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2529 | FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2530 | break; |
| 2531 | case FLAC__APODIZATION_BLACKMAN: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2532 | FLAC__window_blackman(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2533 | break; |
| 2534 | case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2535 | FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2536 | break; |
| 2537 | case FLAC__APODIZATION_CONNES: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2538 | FLAC__window_connes(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2539 | break; |
| 2540 | case FLAC__APODIZATION_FLATTOP: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2541 | FLAC__window_flattop(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2542 | break; |
| 2543 | case FLAC__APODIZATION_GAUSS: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2544 | FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2545 | break; |
| 2546 | case FLAC__APODIZATION_HAMMING: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2547 | FLAC__window_hamming(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2548 | break; |
| 2549 | case FLAC__APODIZATION_HANN: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2550 | FLAC__window_hann(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2551 | break; |
| 2552 | case FLAC__APODIZATION_KAISER_BESSEL: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2553 | FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2554 | break; |
| 2555 | case FLAC__APODIZATION_NUTTALL: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2556 | FLAC__window_nuttall(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2557 | break; |
| 2558 | case FLAC__APODIZATION_RECTANGLE: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2559 | FLAC__window_rectangle(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2560 | break; |
| 2561 | case FLAC__APODIZATION_TRIANGLE: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2562 | FLAC__window_triangle(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2563 | break; |
| 2564 | case FLAC__APODIZATION_TUKEY: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2565 | FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2566 | break; |
Martijn van Beurden | 29a2833 | 2014-08-10 10:59:29 +0200 | [diff] [blame] | 2567 | 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 Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2573 | case FLAC__APODIZATION_WELCH: |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2574 | FLAC__window_welch(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2575 | break; |
| 2576 | default: |
| 2577 | FLAC__ASSERT(0); |
| 2578 | /* double protection */ |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2579 | FLAC__window_hann(encoder->private_->window[i], new_blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 2580 | break; |
| 2581 | } |
| 2582 | } |
| 2583 | } |
| 2584 | #endif |
| 2585 | |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 2586 | if(ok) |
| 2587 | encoder->private_->input_capacity = new_blocksize; |
| 2588 | else |
| 2589 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 2590 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 2591 | return ok; |
| 2592 | } |
| 2593 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2594 | FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, uint32_t samples, FLAC__bool is_last_block) |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 2595 | { |
| 2596 | const FLAC__byte *buffer; |
Josh Coalson | 352feb5 | 2006-10-15 17:08:52 +0000 | [diff] [blame] | 2597 | size_t bytes; |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 2598 | |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 2599 | FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame)); |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 2600 | |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 2601 | 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 Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 2605 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2606 | 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 Semwal | 7803a78 | 2020-12-18 22:28:36 +0530 | [diff] [blame] | 2613 | 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 Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 2616 | FLAC__bitwriter_release_buffer(encoder->private_->frame); |
| 2617 | FLAC__bitwriter_clear(encoder->private_->frame); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2618 | 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 Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 2625 | if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 2626 | FLAC__bitwriter_release_buffer(encoder->private_->frame); |
| 2627 | FLAC__bitwriter_clear(encoder->private_->frame); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2628 | encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR; |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 2629 | return false; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2630 | } |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 2631 | |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 2632 | FLAC__bitwriter_release_buffer(encoder->private_->frame); |
| 2633 | FLAC__bitwriter_clear(encoder->private_->frame); |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 2634 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2635 | if(samples > 0) { |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 2636 | 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 2640 | return true; |
| 2641 | } |
| 2642 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2643 | FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, FLAC__bool is_last_block) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2644 | { |
| 2645 | FLAC__StreamEncoderWriteStatus status; |
| 2646 | FLAC__uint64 output_position = 0; |
| 2647 | |
Erik de Castro Lopo | b9574fe | 2013-04-07 13:44:33 +1000 | [diff] [blame] | 2648 | #if FLAC__HAS_OGG == 0 |
| 2649 | (void)is_last_block; |
| 2650 | #endif |
| 2651 | |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2652 | /* 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2675 | const uint32_t blocksize = FLAC__stream_encoder_get_blocksize(encoder); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2676 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2679 | uint32_t i; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2680 | 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 Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2703 | #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 Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2707 | buffer, |
| 2708 | bytes, |
| 2709 | samples, |
| 2710 | encoder->private_->current_frame_number, |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 2711 | is_last_block, |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2712 | (FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback, |
| 2713 | encoder, |
| 2714 | encoder->private_->client_data |
| 2715 | ); |
| 2716 | } |
| 2717 | else |
| 2718 | #endif |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2719 | 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Ãguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 2728 | encoder->private_->frames_written = flac_max(encoder->private_->frames_written, encoder->private_->current_frame_number+1); |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2729 | } |
| 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. */ |
| 2737 | void update_metadata_(const FLAC__StreamEncoder *encoder) |
| 2738 | { |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 2739 | FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)]; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2740 | const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo; |
| 2741 | const FLAC__uint64 samples = metadata->data.stream_info.total_samples; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2742 | 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2745 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2758 | const uint32_t md5_offset = |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2759 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2786 | const uint32_t total_samples_byte_offset = |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2787 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2819 | const uint32_t min_framesize_offset = |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2820 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2847 | uint32_t i; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2848 | |
| 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2861 | uint32_t x; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 2862 | 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 Coalson | 15b8eb8 | 2006-10-15 05:15:55 +0000 | [diff] [blame] | 2891 | #if FLAC__HAS_OGG |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2892 | /* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */ |
| 2893 | void update_ogg_metadata_(FLAC__StreamEncoder *encoder) |
| 2894 | { |
Josh Coalson | c986d13 | 2006-11-15 08:53:32 +0000 | [diff] [blame] | 2895 | /* the # of bytes in the 1st packet that precede the STREAMINFO */ |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2896 | static const uint32_t FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH = |
Josh Coalson | c986d13 | 2006-11-15 08:53:32 +0000 | [diff] [blame] | 2897 | 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Ãguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 2904 | FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)]; |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2905 | const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo; |
| 2906 | const FLAC__uint64 samples = metadata->data.stream_info.total_samples; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2907 | const uint32_t min_framesize = metadata->data.stream_info.min_framesize; |
| 2908 | const uint32_t max_framesize = metadata->data.stream_info.max_framesize; |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2909 | ogg_page page; |
| 2910 | |
| 2911 | FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO); |
Josh Coalson | cf1422e | 2006-11-16 01:32:25 +0000 | [diff] [blame] | 2912 | 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 Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2919 | |
| 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2938 | const uint32_t md5_offset = |
Josh Coalson | c986d13 | 2006-11-15 08:53:32 +0000 | [diff] [blame] | 2939 | FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH + |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2940 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2952 | if(md5_offset + 16 > (uint32_t)page.body_len) { |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2953 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2964 | const uint32_t total_samples_byte_offset = |
Josh Coalson | c986d13 | 2006-11-15 08:53:32 +0000 | [diff] [blame] | 2965 | FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH + |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2966 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2978 | if(total_samples_byte_offset + 5 > (uint32_t)page.body_len) { |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2979 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 2996 | const uint32_t min_framesize_offset = |
Josh Coalson | c986d13 | 2006-11-15 08:53:32 +0000 | [diff] [blame] | 2997 | FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH + |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 2998 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3004 | if(min_framesize_offset + 6 > (uint32_t)page.body_len) { |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 3005 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3027 | uint32_t i; |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 3028 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3040 | if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (uint32_t)page.body_len) { |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 3041 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3048 | uint32_t x; |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 3049 | 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 Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 3070 | 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 Coalson | 15b8eb8 | 2006-10-15 05:15:55 +0000 | [diff] [blame] | 3080 | #endif |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 3081 | |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 3082 | FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 3083 | { |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3084 | FLAC__uint16 crc; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3085 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3086 | |
| 3087 | /* |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 3088 | * Accumulate raw signal to the MD5 signature |
| 3089 | */ |
Josh Coalson | 00da5ae | 2007-06-14 05:19:46 +0000 | [diff] [blame] | 3090 | 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 Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3091 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 3092 | return false; |
| 3093 | } |
| 3094 | |
| 3095 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3096 | * Process the frame header and subframes into the frame bitbuffer |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3097 | */ |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 3098 | if(!process_subframes_(encoder, is_fractional_block)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3099 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3100 | return false; |
| 3101 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3102 | |
| 3103 | /* |
| 3104 | * Zero-pad the frame to a byte_boundary |
| 3105 | */ |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3106 | if(!FLAC__bitwriter_zero_pad_to_byte_boundary(encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3107 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3108 | return false; |
| 3109 | } |
| 3110 | |
| 3111 | /* |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 3112 | * CRC-16 the whole thing |
| 3113 | */ |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3114 | 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 Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 3122 | |
| 3123 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3124 | * Write it |
| 3125 | */ |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 3126 | if(!write_bitbuffer_(encoder, encoder->protected_->blocksize, is_last_block)) { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 3127 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3128 | return false; |
| 3129 | } |
| 3130 | |
| 3131 | /* |
| 3132 | * Get ready for the next frame |
| 3133 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3134 | encoder->private_->current_sample_number = 0; |
| 3135 | encoder->private_->current_frame_number++; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 3136 | encoder->private_->streaminfo.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3137 | |
| 3138 | return true; |
| 3139 | } |
| 3140 | |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 3141 | FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3142 | { |
| 3143 | FLAC__FrameHeader frame_header; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3144 | uint32_t channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order; |
Josh Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 3145 | FLAC__bool do_independent, do_mid_side; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3146 | |
| 3147 | /* |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 3148 | * Calculate the min,max Rice partition orders |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3149 | */ |
Josh Coalson | 85aaed8 | 2006-11-09 01:19:13 +0000 | [diff] [blame] | 3150 | if(is_fractional_block) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3151 | max_partition_order = 0; |
| 3152 | } |
| 3153 | else { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 3154 | max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize); |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 3155 | max_partition_order = flac_min(max_partition_order, encoder->protected_->max_residual_partition_order); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3156 | } |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 3157 | min_partition_order = flac_min(min_partition_order, max_partition_order); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3158 | |
| 3159 | /* |
| 3160 | * Setup the frame |
| 3161 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3162 | frame_header.blocksize = encoder->protected_->blocksize; |
| 3163 | frame_header.sample_rate = encoder->protected_->sample_rate; |
| 3164 | frame_header.channels = encoder->protected_->channels; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3165 | frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3166 | frame_header.bits_per_sample = encoder->protected_->bits_per_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3167 | frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3168 | frame_header.number.frame_number = encoder->private_->current_frame_number; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3169 | |
| 3170 | /* |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3171 | * Figure out what channel assignments to try |
| 3172 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3173 | 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 Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3176 | do_independent = true; |
| 3177 | do_mid_side = true; |
| 3178 | } |
| 3179 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3180 | do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3181 | 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 Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3193 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 3194 | FLAC__ASSERT(do_independent || do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3195 | |
| 3196 | /* |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3197 | * Check for wasted bits; set effective bps for each subframe |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 3198 | */ |
| 3199 | if(do_independent) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3200 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3201 | uint32_t w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize); |
Erik de Castro Lopo | d91eb4a | 2015-09-26 08:17:13 +1000 | [diff] [blame] | 3202 | if (w > encoder->protected_->bits_per_sample) { |
| 3203 | w = encoder->protected_->bits_per_sample; |
| 3204 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3205 | 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 Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3207 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 3208 | } |
| 3209 | if(do_mid_side) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3210 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3211 | for(channel = 0; channel < 2; channel++) { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3212 | uint32_t w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize); |
Erik de Castro Lopo | d91eb4a | 2015-09-26 08:17:13 +1000 | [diff] [blame] | 3213 | if (w > encoder->protected_->bits_per_sample) { |
| 3214 | w = encoder->protected_->bits_per_sample; |
| 3215 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3216 | 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 Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3218 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3222 | * First do a normal encoding pass of each independent channel |
| 3223 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3224 | if(do_independent) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3225 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3226 | if(! |
| 3227 | process_subframe_( |
| 3228 | encoder, |
| 3229 | min_partition_order, |
| 3230 | max_partition_order, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3231 | &frame_header, |
| 3232 | encoder->private_->subframe_bps[channel], |
| 3233 | encoder->private_->integer_signal[channel], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3234 | 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 Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3241 | return false; |
| 3242 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | /* |
| 3246 | * Now do mid and side channels if requested |
| 3247 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3248 | if(do_mid_side) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3249 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3250 | |
| 3251 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3252 | if(! |
| 3253 | process_subframe_( |
| 3254 | encoder, |
| 3255 | min_partition_order, |
| 3256 | max_partition_order, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3257 | &frame_header, |
| 3258 | encoder->private_->subframe_bps_mid_side[channel], |
| 3259 | encoder->private_->integer_signal_mid_side[channel], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3260 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3267 | return false; |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | /* |
| 3272 | * Compose the frame bitbuffer |
| 3273 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3274 | if(do_mid_side) { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3275 | uint32_t left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3276 | FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3277 | FLAC__ChannelAssignment channel_assignment; |
| 3278 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3279 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3280 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3281 | 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 Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3283 | } |
| 3284 | else { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3285 | uint32_t bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */ |
| 3286 | uint32_t min_bits; |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3287 | int ca; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3288 | |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3289 | 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 Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 3293 | FLAC__ASSERT(do_independent && do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3294 | |
| 3295 | /* We have to figure out which channel assignent results in the smallest frame */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3296 | 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 Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3300 | |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3301 | channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; |
| 3302 | min_bits = bits[channel_assignment]; |
| 3303 | for(ca = 1; ca <= 3; ca++) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3304 | if(bits[ca] < min_bits) { |
| 3305 | min_bits = bits[ca]; |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3306 | channel_assignment = (FLAC__ChannelAssignment)ca; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3307 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3308 | } |
| 3309 | } |
| 3310 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3311 | frame_header.channel_assignment = channel_assignment; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3312 | |
Josh Coalson | d0edb97 | 2006-10-07 06:50:08 +0000 | [diff] [blame] | 3313 | if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3314 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3315 | return false; |
| 3316 | } |
| 3317 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3318 | switch(channel_assignment) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3319 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3320 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3322 | break; |
| 3323 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3324 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3326 | break; |
| 3327 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3328 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3330 | break; |
| 3331 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3332 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3334 | break; |
| 3335 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 3336 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3337 | } |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3338 | |
| 3339 | switch(channel_assignment) { |
| 3340 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3341 | left_bps = encoder->private_->subframe_bps [0]; |
| 3342 | right_bps = encoder->private_->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3343 | break; |
| 3344 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3345 | left_bps = encoder->private_->subframe_bps [0]; |
| 3346 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3347 | break; |
| 3348 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3349 | left_bps = encoder->private_->subframe_bps_mid_side[1]; |
| 3350 | right_bps = encoder->private_->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3351 | break; |
| 3352 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3353 | left_bps = encoder->private_->subframe_bps_mid_side[0]; |
| 3354 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3355 | break; |
| 3356 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 3357 | FLAC__ASSERT(0); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | /* note that encoder_add_subframe_ sets the state for us in case of an error */ |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3361 | if(!add_subframe_(encoder, frame_header.blocksize, left_bps , left_subframe , encoder->private_->frame)) |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3362 | return false; |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3363 | if(!add_subframe_(encoder, frame_header.blocksize, right_bps, right_subframe, encoder->private_->frame)) |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3364 | return false; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3365 | } |
| 3366 | else { |
Josh Coalson | d0edb97 | 2006-10-07 06:50:08 +0000 | [diff] [blame] | 3367 | if(!FLAC__frame_add_header(&frame_header, encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3368 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3369 | return false; |
| 3370 | } |
| 3371 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3372 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3373 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3374 | /* the above function sets the state for us in case of an error */ |
| 3375 | return false; |
| 3376 | } |
| 3377 | } |
| 3378 | } |
| 3379 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3380 | 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 Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3386 | encoder->private_->last_channel_assignment = frame_header.channel_assignment; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 3387 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3388 | return true; |
| 3389 | } |
| 3390 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3391 | FLAC__bool process_subframe_( |
| 3392 | FLAC__StreamEncoder *encoder, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3393 | uint32_t min_partition_order, |
| 3394 | uint32_t max_partition_order, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3395 | const FLAC__FrameHeader *frame_header, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3396 | uint32_t subframe_bps, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3397 | const FLAC__int32 integer_signal[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3398 | FLAC__Subframe *subframe[2], |
| 3399 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2], |
| 3400 | FLAC__int32 *residual[2], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3401 | uint32_t *best_subframe, |
| 3402 | uint32_t *best_bits |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3403 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3404 | { |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3405 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Thomas Zander | e5498e8 | 2016-02-08 10:43:37 +0100 | [diff] [blame] | 3406 | float fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3407 | #else |
| 3408 | FLAC__fixedpoint fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 3409 | #endif |
| 3410 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Thomas Zander | e5498e8 | 2016-02-08 10:43:37 +0100 | [diff] [blame] | 3411 | double lpc_residual_bits_per_sample; |
Erik de Castro Lopo | 5e5ee27 | 2013-09-15 09:51:28 +1000 | [diff] [blame] | 3412 | 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 Zander | e5498e8 | 2016-02-08 10:43:37 +0100 | [diff] [blame] | 3413 | double lpc_error[FLAC__MAX_LPC_ORDER]; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3414 | 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 Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3416 | #endif |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3417 | 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 Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3421 | /* only use RICE2 partitions if stream bps > 16 */ |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3422 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3423 | |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3424 | FLAC__ASSERT(frame_header->blocksize > 0); |
| 3425 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3426 | /* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 3427 | _best_subframe = 0; |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3428 | if(encoder->private_->disable_verbatim_subframes && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) |
| 3429 | _best_bits = UINT_MAX; |
| 3430 | else |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3431 | _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3432 | |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3433 | if(frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3434 | uint32_t signal_is_constant = false; |
Erik de Castro Lopo | eb2af52 | 2015-11-25 05:42:40 +1100 | [diff] [blame] | 3435 | 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 Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3439 | /* check for constant subframe */ |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3440 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3449 | uint32_t i; |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3450 | signal_is_constant = true; |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3451 | for(i = 1; i < frame_header->blocksize; i++) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3452 | if(integer_signal[0] != integer_signal[i]) { |
| 3453 | signal_is_constant = false; |
| 3454 | break; |
| 3455 | } |
| 3456 | } |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3457 | } |
| 3458 | if(signal_is_constant) { |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3459 | _candidate_bits = evaluate_constant_subframe_(encoder, integer_signal[0], frame_header->blocksize, subframe_bps, subframe[!_best_subframe]); |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3460 | if(_candidate_bits < _best_bits) { |
| 3461 | _best_subframe = !_best_subframe; |
| 3462 | _best_bits = _candidate_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3463 | } |
| 3464 | } |
| 3465 | else { |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3466 | 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 Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3471 | } |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3472 | else { |
| 3473 | min_fixed_order = max_fixed_order = guess_fixed_order; |
| 3474 | } |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3475 | if(max_fixed_order >= frame_header->blocksize) |
| 3476 | max_fixed_order = frame_header->blocksize - 1; |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3477 | for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3478 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Thomas Zander | e5498e8 | 2016-02-08 10:43:37 +0100 | [diff] [blame] | 3479 | if(fixed_residual_bits_per_sample[fixed_order] >= (float)subframe_bps) |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3480 | continue; /* don't even try */ |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3481 | 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 Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3482 | #else |
| 3483 | if(FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]) >= (int)subframe_bps) |
| 3484 | continue; /* don't even try */ |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3485 | 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 Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3486 | #endif |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3487 | rice_parameter++; /* to account for the signed->uint32_t conversion during rice coding */ |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3488 | if(rice_parameter >= rice_parameter_limit) { |
David Seifert | b3ece71 | 2017-01-15 10:37:02 +0100 | [diff] [blame] | 3489 | #ifndef NDEBUG |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3490 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, rice_parameter_limit - 1); |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3491 | #endif |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3492 | rice_parameter = rice_parameter_limit - 1; |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3493 | } |
| 3494 | _candidate_bits = |
| 3495 | evaluate_fixed_subframe_( |
| 3496 | encoder, |
| 3497 | integer_signal, |
| 3498 | residual[!_best_subframe], |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3499 | 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 Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3505 | rice_parameter_limit, |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3506 | min_partition_order, |
| 3507 | max_partition_order, |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3508 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3517 | } |
| 3518 | } |
| 3519 | |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3520 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3521 | /* encode lpc */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3522 | if(encoder->protected_->max_lpc_order > 0) { |
| 3523 | if(encoder->protected_->max_lpc_order >= frame_header->blocksize) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3524 | max_lpc_order = frame_header->blocksize-1; |
| 3525 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 3526 | max_lpc_order = encoder->protected_->max_lpc_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3527 | if(max_lpc_order > 0) { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3528 | uint32_t a; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3529 | for (a = 0; a < encoder->protected_->num_apodizations; a++) { |
Josh Coalson | 4a040f6 | 2007-04-04 00:59:28 +0000 | [diff] [blame] | 3530 | FLAC__lpc_window_data(integer_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3531 | 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 Coalson | 32b9bae | 2006-11-27 16:27:41 +0000 | [diff] [blame] | 3534 | FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error); |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3535 | if(encoder->protected_->do_exhaustive_model_search) { |
| 3536 | min_lpc_order = 1; |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 3537 | } |
| 3538 | else { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3539 | const uint32_t guess_lpc_order = |
Josh Coalson | df59845 | 2006-04-28 00:13:34 +0000 | [diff] [blame] | 3540 | 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 Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3550 | min_lpc_order = max_lpc_order = guess_lpc_order; |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 3551 | } |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3552 | if(max_lpc_order >= frame_header->blocksize) |
| 3553 | max_lpc_order = frame_header->blocksize - 1; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3554 | 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 Zander | e5498e8 | 2016-02-08 10:43:37 +0100 | [diff] [blame] | 3556 | if(lpc_residual_bits_per_sample >= (double)subframe_bps) |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3557 | continue; /* don't even try */ |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3558 | 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 Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3560 | if(rice_parameter >= rice_parameter_limit) { |
David Seifert | b3ece71 | 2017-01-15 10:37:02 +0100 | [diff] [blame] | 3561 | #ifndef NDEBUG |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3562 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, rice_parameter_limit - 1); |
Josh Coalson | df59845 | 2006-04-28 00:13:34 +0000 | [diff] [blame] | 3563 | #endif |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3564 | rice_parameter = rice_parameter_limit - 1; |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3565 | } |
| 3566 | if(encoder->protected_->do_qlp_coeff_prec_search) { |
| 3567 | min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
lvqcl | 165d7b1 | 2015-10-12 00:59:08 +0300 | [diff] [blame] | 3568 | /* 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 Beurden | 2784670 | 2014-07-28 13:04:56 +0200 | [diff] [blame] | 3570 | max_qlp_coeff_precision = flac_min(32 - subframe_bps - FLAC__bitmath_ilog2(lpc_order), FLAC__MAX_QLP_COEFF_PRECISION); |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 3571 | max_qlp_coeff_precision = flac_max(max_qlp_coeff_precision, min_qlp_coeff_precision); |
Josh Coalson | df59845 | 2006-04-28 00:13:34 +0000 | [diff] [blame] | 3572 | } |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3573 | 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 Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3585 | encoder->private_->abs_residual_partition_sums, |
| 3586 | encoder->private_->raw_bits_per_partition, |
| 3587 | encoder->private_->lp_coeff[lpc_order-1], |
| 3588 | frame_header->blocksize, |
| 3589 | subframe_bps, |
| 3590 | lpc_order, |
| 3591 | qlp_coeff_precision, |
| 3592 | rice_parameter, |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3593 | rice_parameter_limit, |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3594 | min_partition_order, |
| 3595 | max_partition_order, |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3596 | encoder->protected_->do_escape_coding, |
| 3597 | encoder->protected_->rice_parameter_search_dist, |
| 3598 | subframe[!_best_subframe], |
| 3599 | partitioned_rice_contents[!_best_subframe] |
Josh Coalson | bf0f52c | 2006-04-25 06:38:43 +0000 | [diff] [blame] | 3600 | ); |
| 3601 | if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 3602 | if(_candidate_bits < _best_bits) { |
| 3603 | _best_subframe = !_best_subframe; |
| 3604 | _best_bits = _candidate_bits; |
| 3605 | } |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 3606 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3607 | } |
| 3608 | } |
| 3609 | } |
| 3610 | } |
| 3611 | } |
| 3612 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3613 | #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3614 | } |
| 3615 | } |
| 3616 | |
Josh Coalson | 7269580 | 2002-10-11 06:25:16 +0000 | [diff] [blame] | 3617 | /* under rare circumstances this can happen when all but lpc subframe types are disabled: */ |
| 3618 | if(_best_bits == UINT_MAX) { |
| 3619 | FLAC__ASSERT(_best_subframe == 0); |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3620 | _best_bits = evaluate_verbatim_subframe_(encoder, integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]); |
Josh Coalson | 7269580 | 2002-10-11 06:25:16 +0000 | [diff] [blame] | 3621 | } |
Josh Coalson | e6b3bbe | 2002-10-08 06:03:25 +0000 | [diff] [blame] | 3622 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3623 | *best_subframe = _best_subframe; |
| 3624 | *best_bits = _best_bits; |
| 3625 | |
| 3626 | return true; |
| 3627 | } |
| 3628 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3629 | FLAC__bool add_subframe_( |
| 3630 | FLAC__StreamEncoder *encoder, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3631 | uint32_t blocksize, |
| 3632 | uint32_t subframe_bps, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3633 | const FLAC__Subframe *subframe, |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3634 | FLAC__BitWriter *frame |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3635 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3636 | { |
| 3637 | switch(subframe->type) { |
| 3638 | case FLAC__SUBFRAME_TYPE_CONSTANT: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 3639 | if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 3640 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3641 | return false; |
| 3642 | } |
| 3643 | break; |
| 3644 | case FLAC__SUBFRAME_TYPE_FIXED: |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3645 | if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 3646 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3647 | return false; |
| 3648 | } |
| 3649 | break; |
| 3650 | case FLAC__SUBFRAME_TYPE_LPC: |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3651 | if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 3652 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3653 | return false; |
| 3654 | } |
| 3655 | break; |
| 3656 | case FLAC__SUBFRAME_TYPE_VERBATIM: |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3657 | if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), blocksize, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 3658 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3659 | return false; |
| 3660 | } |
| 3661 | break; |
| 3662 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 3663 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | return true; |
| 3667 | } |
| 3668 | |
Josh Coalson | 469ba74 | 2007-02-03 02:52:32 +0000 | [diff] [blame] | 3669 | #define SPOTCHECK_ESTIMATE 0 |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3670 | #if SPOTCHECK_ESTIMATE |
| 3671 | static void spotcheck_subframe_estimate_( |
| 3672 | FLAC__StreamEncoder *encoder, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3673 | uint32_t blocksize, |
| 3674 | uint32_t subframe_bps, |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3675 | const FLAC__Subframe *subframe, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3676 | uint32_t estimate |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3677 | ) |
| 3678 | { |
| 3679 | FLAC__bool ret; |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3680 | FLAC__BitWriter *frame = FLAC__bitwriter_new(); |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3681 | if(frame == 0) { |
| 3682 | fprintf(stderr, "EST: can't allocate frame\n"); |
| 3683 | return; |
| 3684 | } |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3685 | if(!FLAC__bitwriter_init(frame)) { |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3686 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3692 | const uint32_t actual = FLAC__bitwriter_get_input_bits_unconsumed(frame); |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3693 | 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 Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3696 | FLAC__bitwriter_delete(frame); |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3697 | } |
| 3698 | #endif |
| 3699 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3700 | uint32_t evaluate_constant_subframe_( |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3701 | FLAC__StreamEncoder *encoder, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3702 | const FLAC__int32 signal, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3703 | uint32_t blocksize, |
| 3704 | uint32_t subframe_bps, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3705 | FLAC__Subframe *subframe |
| 3706 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3707 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3708 | uint32_t estimate; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3709 | subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 3710 | subframe->data.constant.value = signal; |
| 3711 | |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3712 | estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + subframe_bps; |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3713 | |
| 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3723 | uint32_t evaluate_fixed_subframe_( |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3724 | FLAC__StreamEncoder *encoder, |
| 3725 | const FLAC__int32 signal[], |
| 3726 | FLAC__int32 residual[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3727 | FLAC__uint64 abs_residual_partition_sums[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3728 | 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 Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3736 | FLAC__bool do_escape_coding, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3737 | uint32_t rice_parameter_search_dist, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3738 | FLAC__Subframe *subframe, |
| 3739 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 3740 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3741 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3742 | uint32_t i, residual_bits, estimate; |
| 3743 | const uint32_t residual_samples = blocksize - order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3744 | |
| 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 Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 3750 | subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3751 | subframe->data.fixed.residual = residual; |
| 3752 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3753 | residual_bits = |
| 3754 | find_best_partition_order_( |
| 3755 | encoder->private_, |
| 3756 | residual, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3757 | abs_residual_partition_sums, |
| 3758 | raw_bits_per_partition, |
| 3759 | residual_samples, |
| 3760 | order, |
| 3761 | rice_parameter, |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3762 | rice_parameter_limit, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3763 | min_partition_order, |
| 3764 | max_partition_order, |
Josh Coalson | 0a0a10f | 2007-03-31 01:01:02 +0000 | [diff] [blame] | 3765 | subframe_bps, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3766 | do_escape_coding, |
| 3767 | rice_parameter_search_dist, |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3768 | &subframe->data.fixed.entropy_coding_method |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3769 | ); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3770 | |
| 3771 | subframe->data.fixed.order = order; |
| 3772 | for(i = 0; i < order; i++) |
| 3773 | subframe->data.fixed.warmup[i] = signal[i]; |
| 3774 | |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3775 | 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 Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3776 | |
| 3777 | #if SPOTCHECK_ESTIMATE |
| 3778 | spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate); |
| 3779 | #endif |
| 3780 | |
| 3781 | return estimate; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3784 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3785 | uint32_t evaluate_lpc_subframe_( |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3786 | FLAC__StreamEncoder *encoder, |
| 3787 | const FLAC__int32 signal[], |
| 3788 | FLAC__int32 residual[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3789 | FLAC__uint64 abs_residual_partition_sums[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3790 | uint32_t raw_bits_per_partition[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3791 | const FLAC__real lp_coeff[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3792 | 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 Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3800 | FLAC__bool do_escape_coding, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3801 | uint32_t rice_parameter_search_dist, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3802 | FLAC__Subframe *subframe, |
| 3803 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents |
| 3804 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3805 | { |
Erik de Castro Lopo | ecd0acb | 2013-10-04 01:38:00 +1000 | [diff] [blame] | 3806 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3807 | uint32_t i, residual_bits, estimate; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3808 | int quantization, ret; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3809 | const uint32_t residual_samples = blocksize - order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3810 | |
lvqcl | 165d7b1 | 2015-10-12 00:59:08 +0300 | [diff] [blame] | 3811 | /* 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 Coalson | 20ac2c1 | 2002-08-30 05:47:14 +0000 | [diff] [blame] | 3813 | FLAC__ASSERT(order > 0); |
| 3814 | FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER); |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 3815 | qlp_coeff_precision = flac_min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order)); |
Josh Coalson | 20ac2c1 | 2002-08-30 05:47:14 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
Josh Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 3818 | ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3819 | 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 Coalson | fb9d18f | 2002-10-21 07:04:07 +0000 | [diff] [blame] | 3822 | 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 Coalson | c9c0d13 | 2002-10-04 05:29:05 +0000 | [diff] [blame] | 3827 | else |
| 3828 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3829 | |
| 3830 | subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 3831 | |
| 3832 | subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 3833 | subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents = partitioned_rice_contents; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3834 | subframe->data.lpc.residual = residual; |
| 3835 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3836 | residual_bits = |
| 3837 | find_best_partition_order_( |
| 3838 | encoder->private_, |
| 3839 | residual, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3840 | abs_residual_partition_sums, |
| 3841 | raw_bits_per_partition, |
| 3842 | residual_samples, |
| 3843 | order, |
| 3844 | rice_parameter, |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3845 | rice_parameter_limit, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3846 | min_partition_order, |
| 3847 | max_partition_order, |
Josh Coalson | 0a0a10f | 2007-03-31 01:01:02 +0000 | [diff] [blame] | 3848 | subframe_bps, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3849 | do_escape_coding, |
| 3850 | rice_parameter_search_dist, |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3851 | &subframe->data.lpc.entropy_coding_method |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3852 | ); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3853 | |
| 3854 | subframe->data.lpc.order = order; |
| 3855 | subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 3856 | subframe->data.lpc.quantization_level = quantization; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 3857 | memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3858 | for(i = 0; i < order; i++) |
| 3859 | subframe->data.lpc.warmup[i] = signal[i]; |
| 3860 | |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3861 | 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 Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3862 | |
| 3863 | #if SPOTCHECK_ESTIMATE |
| 3864 | spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate); |
| 3865 | #endif |
| 3866 | |
| 3867 | return estimate; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3868 | } |
Josh Coalson | 5f2b46d | 2004-11-09 01:34:01 +0000 | [diff] [blame] | 3869 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3870 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3871 | uint32_t evaluate_verbatim_subframe_( |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3872 | FLAC__StreamEncoder *encoder, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3873 | const FLAC__int32 signal[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3874 | uint32_t blocksize, |
| 3875 | uint32_t subframe_bps, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3876 | FLAC__Subframe *subframe |
| 3877 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3878 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3879 | uint32_t estimate; |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3880 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3881 | subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 3882 | |
| 3883 | subframe->data.verbatim.data = signal; |
| 3884 | |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 3885 | estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps); |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3886 | |
| 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3896 | uint32_t find_best_partition_order_( |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3897 | FLAC__StreamEncoderPrivate *private_, |
| 3898 | const FLAC__int32 residual[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3899 | FLAC__uint64 abs_residual_partition_sums[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3900 | 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 Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3908 | FLAC__bool do_escape_coding, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3909 | uint32_t rice_parameter_search_dist, |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3910 | FLAC__EntropyCodingMethod *best_ecm |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3911 | ) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3912 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3913 | 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 Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 3917 | |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 3918 | 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Ãguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 3919 | min_partition_order = flac_min(min_partition_order, max_partition_order); |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3920 | |
Erik de Castro Lopo | ecd0acb | 2013-10-04 01:38:00 +1000 | [diff] [blame] | 3921 | private_->local_precompute_partition_info_sums(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order, bps); |
Josh Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 3922 | |
| 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 Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3927 | int partition_order; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3928 | uint32_t sum; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 3929 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3930 | for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) { |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3931 | if(! |
Josh Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 3932 | set_partitioned_rice_( |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3933 | #ifdef EXACT_RICE_BITS_CALCULATION |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3934 | residual, |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 3935 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3936 | abs_residual_partition_sums+sum, |
| 3937 | raw_bits_per_partition+sum, |
| 3938 | residual_samples, |
| 3939 | predictor_order, |
| 3940 | rice_parameter, |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3941 | rice_parameter_limit, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3942 | rice_parameter_search_dist, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3943 | (uint32_t)partition_order, |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3944 | do_escape_coding, |
| 3945 | &private_->partitioned_rice_contents_extra[!best_parameters_index], |
| 3946 | &residual_bits |
| 3947 | ) |
| 3948 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3949 | { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 3950 | FLAC__ASSERT(best_residual_bits != 0); |
| 3951 | break; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3952 | } |
| 3953 | sum += 1u << partition_order; |
| 3954 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 3955 | best_residual_bits = residual_bits; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3956 | best_parameters_index = !best_parameters_index; |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3957 | best_partition_order = partition_order; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3958 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 3959 | } |
| 3960 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 3961 | |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3962 | best_ecm->data.partitioned_rice.order = best_partition_order; |
| 3963 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 3964 | { |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3965 | /* |
| 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3970 | uint32_t partition; |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3971 | |
| 3972 | /* save best parameters and raw_bits */ |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 3973 | FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(prc, flac_max(6u, best_partition_order)); |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3974 | memcpy(prc->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(uint32_t)*(1<<(best_partition_order))); |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3975 | if(do_escape_coding) |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3976 | memcpy(prc->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(uint32_t)*(1<<(best_partition_order))); |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 3977 | /* |
| 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 Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 3988 | } |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 3989 | |
| 3990 | return best_residual_bits; |
| 3991 | } |
| 3992 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3993 | void precompute_partition_info_sums_( |
Josh Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 3994 | const FLAC__int32 residual[], |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 3995 | FLAC__uint64 abs_residual_partition_sums[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 3996 | 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 Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 4001 | ) |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 4002 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4003 | const uint32_t default_partition_samples = (residual_samples + predictor_order) >> max_partition_order; |
| 4004 | uint32_t partitions = 1u << max_partition_order; |
Josh Coalson | 0a0a10f | 2007-03-31 01:01:02 +0000 | [diff] [blame] | 4005 | |
| 4006 | FLAC__ASSERT(default_partition_samples > predictor_order); |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 4007 | |
Josh Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 4008 | /* first do max_partition_order */ |
| 4009 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4010 | 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 Lopo | 86b36d9 | 2015-11-03 18:08:49 +1100 | [diff] [blame] | 4012 | /* 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 Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 4014 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
Erik de Castro Lopo | 86b36d9 | 2015-11-03 18:08:49 +1100 | [diff] [blame] | 4015 | FLAC__uint32 abs_residual_partition_sum = 0; |
Josh Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 4016 | end += default_partition_samples; |
Josh Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 4017 | 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 Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 4023 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
Erik de Castro Lopo | 86b36d9 | 2015-11-03 18:08:49 +1100 | [diff] [blame] | 4024 | FLAC__uint64 abs_residual_partition_sum64 = 0; |
Josh Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 4025 | end += default_partition_samples; |
Josh Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 4026 | for( ; residual_sample < end; residual_sample++) |
Erik de Castro Lopo | 86b36d9 | 2015-11-03 18:08:49 +1100 | [diff] [blame] | 4027 | 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 Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 4029 | } |
Josh Coalson | 0a0a10f | 2007-03-31 01:01:02 +0000 | [diff] [blame] | 4030 | } |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 4031 | } |
Josh Coalson | f76a361 | 2001-04-18 02:28:11 +0000 | [diff] [blame] | 4032 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4033 | /* now merge partitions for lower orders */ |
Josh Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 4034 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4035 | uint32_t from_partition = 0, to_partition = partitions; |
Josh Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 4036 | int partition_order; |
| 4037 | for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4038 | uint32_t i; |
Josh Coalson | 0429d60 | 2007-04-04 01:08:24 +0000 | [diff] [blame] | 4039 | 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 Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 4046 | } |
| 4047 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 4048 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4049 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 4050 | void precompute_partition_info_escapes_( |
| 4051 | const FLAC__int32 residual[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4052 | 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 Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 4057 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4058 | { |
| 4059 | int partition_order; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4060 | uint32_t from_partition, to_partition = 0; |
| 4061 | const uint32_t blocksize = residual_samples + predictor_order; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4062 | |
| 4063 | /* first do max_partition_order */ |
| 4064 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
Josh Coalson | 90c693a | 2007-02-01 04:24:08 +0000 | [diff] [blame] | 4065 | FLAC__int32 r; |
| 4066 | FLAC__uint32 rmax; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4067 | 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 Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4070 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 4071 | 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 Coalson | 90c693a | 2007-02-01 04:24:08 +0000 | [diff] [blame] | 4077 | rmax = 0; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 4078 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
Josh Coalson | 90c693a | 2007-02-01 04:24:08 +0000 | [diff] [blame] | 4079 | r = residual[residual_sample++]; |
Josh Coalson | 8534bbe | 2007-02-28 01:10:21 +0000 | [diff] [blame] | 4080 | /* OPT: maybe faster: rmax |= r ^ (r>>31) */ |
Josh Coalson | 90c693a | 2007-02-01 04:24:08 +0000 | [diff] [blame] | 4081 | if(r < 0) |
| 4082 | rmax |= ~r; |
| 4083 | else |
| 4084 | rmax |= r; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4085 | } |
Josh Coalson | 90c693a | 2007-02-01 04:24:08 +0000 | [diff] [blame] | 4086 | /* 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 Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4088 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 4089 | to_partition = partitions; |
Josh Coalson | 369a6da | 2006-10-10 00:37:48 +0000 | [diff] [blame] | 4090 | break; /*@@@ yuck, should remove the 'for' loop instead */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4091 | } |
| 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4095 | uint32_t m; |
| 4096 | uint32_t i; |
| 4097 | const uint32_t partitions = 1u << partition_order; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4098 | for(i = 0; i < partitions; i++) { |
| 4099 | m = raw_bits_per_partition[from_partition]; |
| 4100 | from_partition++; |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 4101 | raw_bits_per_partition[to_partition] = flac_max(m, raw_bits_per_partition[from_partition]); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4102 | from_partition++; |
| 4103 | to_partition++; |
| 4104 | } |
| 4105 | } |
| 4106 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 4107 | |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4108 | #ifdef EXACT_RICE_BITS_CALCULATION |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4109 | static inline uint32_t count_rice_bits_in_partition_( |
| 4110 | const uint32_t rice_parameter, |
| 4111 | const uint32_t partition_samples, |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4112 | const FLAC__int32 *residual |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 4113 | ) |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4114 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4115 | uint32_t i, partition_bits = |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4116 | 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 Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 4117 | (1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */ |
| 4118 | ; |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4119 | for(i = 0; i < partition_samples; i++) |
Josh Coalson | 423f804 | 2007-01-28 17:40:26 +0000 | [diff] [blame] | 4120 | partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter ); |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4121 | return partition_bits; |
| 4122 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4123 | #else |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4124 | static inline uint32_t count_rice_bits_in_partition_( |
| 4125 | const uint32_t rice_parameter, |
| 4126 | const uint32_t partition_samples, |
Josh Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 4127 | const FLAC__uint64 abs_residual_partition_sum |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4128 | ) |
| 4129 | { |
Josh Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 4130 | return |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4131 | 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 Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 4132 | (1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */ |
| 4133 | ( |
| 4134 | rice_parameter? |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4135 | (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 Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 4137 | ) |
| 4138 | - (partition_samples >> 1) |
| 4139 | /* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum. |
Josh Coalson | 5981e4f | 2007-03-06 06:51:57 +0000 | [diff] [blame] | 4140 | * 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 Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 4141 | * 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 Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4146 | } |
| 4147 | #endif |
| 4148 | |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 4149 | FLAC__bool set_partitioned_rice_( |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4150 | #ifdef EXACT_RICE_BITS_CALCULATION |
| 4151 | const FLAC__int32 residual[], |
| 4152 | #endif |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 4153 | const FLAC__uint64 abs_residual_partition_sums[], |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4154 | 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 Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 4161 | const FLAC__bool search_for_escapes, |
| 4162 | FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4163 | uint32_t *bits |
Josh Coalson | 6fe72f7 | 2002-08-20 04:01:59 +0000 | [diff] [blame] | 4164 | ) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4165 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4166 | 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 Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4170 | #ifdef ENABLE_RICE_PARAMETER_SEARCH |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4171 | uint32_t min_rice_parameter, max_rice_parameter; |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4172 | #else |
| 4173 | (void)rice_parameter_search_dist; |
| 4174 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4175 | |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4176 | 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 Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4178 | |
Cristian RodrÃguez | f029625 | 2012-04-05 19:39:37 -0300 | [diff] [blame] | 4179 | FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order)); |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 4180 | parameters = partitioned_rice_contents->parameters; |
| 4181 | raw_bits = partitioned_rice_contents->raw_bits; |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 4182 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4183 | if(partition_order == 0) { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4184 | best_partition_bits = (uint32_t)(-1); |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4185 | #ifdef ENABLE_RICE_PARAMETER_SEARCH |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4186 | 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 Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4192 | if(max_rice_parameter >= rice_parameter_limit) { |
David Seifert | b3ece71 | 2017-01-15 10:37:02 +0100 | [diff] [blame] | 4193 | #ifndef NDEBUG |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4194 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, rice_parameter_limit - 1); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4195 | #endif |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4196 | max_rice_parameter = rice_parameter_limit - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4197 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 4198 | } |
| 4199 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4200 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 4201 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4202 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4203 | #else |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4204 | rice_parameter = suggested_rice_parameter; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 4205 | #endif |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4206 | #ifdef EXACT_RICE_BITS_CALCULATION |
Josh Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 4207 | partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, residual); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 4208 | #else |
Josh Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 4209 | partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, abs_residual_partition_sums[0]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 4210 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4211 | if(partition_bits < best_partition_bits) { |
| 4212 | best_rice_parameter = rice_parameter; |
| 4213 | best_partition_bits = partition_bits; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 4214 | } |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4215 | #ifdef ENABLE_RICE_PARAMETER_SEARCH |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 4216 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4217 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4218 | if(search_for_escapes) { |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4219 | 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 Coalson | 90c693a | 2007-02-01 04:24:08 +0000 | [diff] [blame] | 4220 | if(partition_bits <= best_partition_bits) { |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4221 | raw_bits[0] = raw_bits_per_partition[0]; |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4222 | best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */ |
Josh Coalson | 90c693a | 2007-02-01 04:24:08 +0000 | [diff] [blame] | 4223 | best_partition_bits = partition_bits; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4224 | } |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4225 | else |
| 4226 | raw_bits[0] = 0; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4227 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4228 | parameters[0] = best_rice_parameter; |
| 4229 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 4230 | } |
| 4231 | else { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4232 | uint32_t partition, residual_sample; |
| 4233 | uint32_t partition_samples; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 4234 | FLAC__uint64 mean, k; |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4235 | const uint32_t partitions = 1u << partition_order; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 4236 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 4237 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4238 | if(partition == 0) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 4239 | if(partition_samples <= predictor_order) |
| 4240 | return false; |
| 4241 | else |
| 4242 | partition_samples -= predictor_order; |
| 4243 | } |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 4244 | mean = abs_residual_partition_sums[partition]; |
Josh Coalson | f81b6df | 2005-02-04 01:34:35 +0000 | [diff] [blame] | 4245 | /* 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 Lopo | 64f34e6 | 2013-10-10 21:31:50 +1100 | [diff] [blame] | 4253 | #if 0 /* old simple code */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 4254 | for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1) |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 4255 | ; |
Erik de Castro Lopo | 64f34e6 | 2013-10-10 21:31:50 +1100 | [diff] [blame] | 4256 | #else |
Erik de Castro Lopo | b8d58e3 | 2014-06-15 20:29:34 +1000 | [diff] [blame] | 4257 | #if defined FLAC__CPU_X86_64 /* and other 64-bit arch, too */ |
Erik de Castro Lopo | 64f34e6 | 2013-10-10 21:31:50 +1100 | [diff] [blame] | 4258 | 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 Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4282 | if(rice_parameter >= rice_parameter_limit) { |
David Seifert | b3ece71 | 2017-01-15 10:37:02 +0100 | [diff] [blame] | 4283 | #ifndef NDEBUG |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4284 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, rice_parameter_limit - 1); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4285 | #endif |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4286 | rice_parameter = rice_parameter_limit - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4287 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 4288 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4289 | best_partition_bits = (uint32_t)(-1); |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4290 | #ifdef ENABLE_RICE_PARAMETER_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 4291 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4292 | if(rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 4293 | min_rice_parameter = 0; |
| 4294 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4295 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 4296 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4297 | if(max_rice_parameter >= rice_parameter_limit) { |
David Seifert | b3ece71 | 2017-01-15 10:37:02 +0100 | [diff] [blame] | 4298 | #ifndef NDEBUG |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4299 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, rice_parameter_limit - 1); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4300 | #endif |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4301 | max_rice_parameter = rice_parameter_limit - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4302 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 4303 | } |
| 4304 | else |
| 4305 | min_rice_parameter = max_rice_parameter = rice_parameter; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 4306 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4307 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 4308 | #endif |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4309 | #ifdef EXACT_RICE_BITS_CALCULATION |
Josh Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 4310 | partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, residual+residual_sample); |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4311 | #else |
Josh Coalson | 1109e7f | 2007-01-24 04:26:15 +0000 | [diff] [blame] | 4312 | partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, abs_residual_partition_sums[partition]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 4313 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4314 | if(partition_bits < best_partition_bits) { |
| 4315 | best_rice_parameter = rice_parameter; |
| 4316 | best_partition_bits = partition_bits; |
| 4317 | } |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4318 | #ifdef ENABLE_RICE_PARAMETER_SEARCH |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 4319 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4320 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4321 | if(search_for_escapes) { |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4322 | 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 Coalson | 90c693a | 2007-02-01 04:24:08 +0000 | [diff] [blame] | 4323 | if(partition_bits <= best_partition_bits) { |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4324 | raw_bits[partition] = raw_bits_per_partition[partition]; |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4325 | best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */ |
Josh Coalson | 90c693a | 2007-02-01 04:24:08 +0000 | [diff] [blame] | 4326 | best_partition_bits = partition_bits; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 4327 | } |
Josh Coalson | 05f710f | 2007-07-31 00:51:26 +0000 | [diff] [blame] | 4328 | else |
| 4329 | raw_bits[partition] = 0; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 4330 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 4331 | parameters[partition] = best_rice_parameter; |
| 4332 | bits_ += best_partition_bits; |
Josh Coalson | ce1d07c | 2007-01-23 05:00:46 +0000 | [diff] [blame] | 4333 | residual_sample += partition_samples; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 4334 | } |
| 4335 | } |
| 4336 | |
| 4337 | *bits = bits_; |
| 4338 | return true; |
| 4339 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 4340 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4341 | uint32_t get_wasted_bits_(FLAC__int32 signal[], uint32_t samples) |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 4342 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4343 | uint32_t i, shift; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 4344 | FLAC__int32 x = 0; |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 4345 | |
| 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4364 | |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4365 | void 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4366 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4367 | uint32_t channel; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4368 | |
| 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4377 | void 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4378 | { |
Erik de Castro Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4379 | uint32_t channel; |
| 4380 | uint32_t sample, wide_sample; |
| 4381 | uint32_t tail = fifo->tail; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4382 | |
| 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 Coalson | 8065a2d | 2006-10-15 08:32:56 +0000 | [diff] [blame] | 4394 | FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4395 | { |
| 4396 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
Josh Coalson | 8065a2d | 2006-10-15 08:32:56 +0000 | [diff] [blame] | 4397 | const size_t encoded_bytes = encoder->private_->verify.output.bytes; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4398 | (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 Coalson | fc2b737 | 2002-08-16 05:39:34 +0000 | [diff] [blame] | 4408 | /* |
| 4409 | * If we get here, a FIFO underflow has occurred, |
| 4410 | * which means there is a bug somewhere. |
| 4411 | */ |
| 4412 | FLAC__ASSERT(0); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4413 | 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 | |
| 4425 | FLAC__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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4428 | 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4432 | |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 4433 | (void)decoder; |
| 4434 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4435 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4437 | uint32_t i, sample = 0; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4438 | 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4451 | encoder->private_->verify.error_stats.frame_number = (uint32_t)(frame->header.number.sample_number / blocksize); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4452 | 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4461 | encoder->private_->verify.input_fifo.tail -= blocksize; |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 4462 | FLAC__ASSERT(encoder->private_->verify.input_fifo.tail <= OVERREAD_); |
Josh Coalson | b7b57ef | 2006-11-09 07:06:33 +0000 | [diff] [blame] | 4463 | 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 Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 4465 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 4466 | } |
| 4467 | |
| 4468 | void 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 | |
| 4473 | void 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 4479 | |
Josh Coalson | 8065a2d | 2006-10-15 08:32:56 +0000 | [diff] [blame] | 4480 | FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data) |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 4481 | { |
| 4482 | (void)client_data; |
| 4483 | |
Josh Coalson | 8065a2d | 2006-10-15 08:32:56 +0000 | [diff] [blame] | 4484 | *bytes = fread(buffer, 1, *bytes, encoder->private_->file); |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 4485 | 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 4494 | FLAC__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 Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 4498 | if(fseeko(encoder->private_->file, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 4499 | return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR; |
| 4500 | else |
| 4501 | return FLAC__STREAM_ENCODER_SEEK_STATUS_OK; |
| 4502 | } |
| 4503 | |
| 4504 | FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) |
| 4505 | { |
Erik de Castro Lopo | f25b260 | 2013-03-14 18:22:29 +1100 | [diff] [blame] | 4506 | FLAC__off_t offset; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 4507 | |
| 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 |
| 4522 | static 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 Lopo | c6318e9 | 2017-01-14 17:26:39 +1100 | [diff] [blame] | 4533 | 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 4534 | { |
Josh Coalson | 2d6b8c6 | 2006-10-11 06:30:38 +0000 | [diff] [blame] | 4535 | (void)client_data, (void)current_frame; |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 4536 | |
| 4537 | if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) { |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 4538 | 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 Coalson | 2d6b8c6 | 2006-10-11 06:30:38 +0000 | [diff] [blame] | 4551 | /* 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 4558 | 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 Coalson | e3ec2ad | 2007-01-31 03:53:22 +0000 | [diff] [blame] | 4567 | FILE *get_binary_stdout_(void) |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 4568 | { |
| 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 Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 4575 | #elif defined __EMX__ |
| 4576 | setmode(fileno(stdout), O_BINARY); |
| 4577 | #endif |
| 4578 | |
| 4579 | return stdout; |
| 4580 | } |