Josh Coalson | 26560dd | 2001-02-08 00:38:41 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Josh Coalson | 305ae2e | 2002-01-26 17:36:39 +0000 | [diff] [blame] | 2 | * Copyright (C) 2000,2001,2002 Josh Coalson |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public |
| 15 | * License along with this library; if not, write to the |
| 16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | * Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> /* for malloc() */ |
| 22 | #include <string.h> /* for memcpy() */ |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 23 | #include "FLAC/assert.h" |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 24 | #include "FLAC/stream_decoder.h" |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 25 | #include "protected/stream_encoder.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 26 | #include "private/bitbuffer.h" |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 27 | #include "private/bitmath.h" |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 28 | #include "private/crc.h" |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 29 | #include "private/cpu.h" |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 30 | #include "private/stream_encoder_framing.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 31 | #include "private/fixed.h" |
| 32 | #include "private/lpc.h" |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 33 | #include "private/md5.h" |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 34 | #include "private/memory.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 35 | |
| 36 | #ifdef min |
| 37 | #undef min |
| 38 | #endif |
| 39 | #define min(x,y) ((x)<(y)?(x):(y)) |
| 40 | |
| 41 | #ifdef max |
| 42 | #undef max |
| 43 | #endif |
| 44 | #define max(x,y) ((x)>(y)?(x):(y)) |
| 45 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 46 | typedef struct { |
| 47 | FLAC__int32 *data[FLAC__MAX_CHANNELS]; |
| 48 | unsigned size; /* of each data[] in samples */ |
| 49 | unsigned tail; |
| 50 | } verify_input_fifo; |
| 51 | |
| 52 | typedef struct { |
| 53 | const FLAC__byte *data; |
| 54 | unsigned capacity; |
| 55 | unsigned bytes; |
| 56 | } verify_output; |
| 57 | |
| 58 | typedef enum { |
| 59 | ENCODER_IN_MAGIC = 0, |
| 60 | ENCODER_IN_METADATA = 1, |
| 61 | ENCODER_IN_AUDIO = 2 |
| 62 | } EncoderStateHint; |
| 63 | |
| 64 | /*@@@@ function for getting the error_stats */ |
| 65 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 66 | /*********************************************************************** |
| 67 | * |
| 68 | * Private class method prototypes |
| 69 | * |
| 70 | ***********************************************************************/ |
| 71 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 72 | static void set_defaults_(FLAC__StreamEncoder *encoder); |
| 73 | static void free_(FLAC__StreamEncoder *encoder); |
| 74 | static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 75 | static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples); |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 76 | static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame); |
| 77 | static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame); |
| 78 | static FLAC__bool process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits); |
| 79 | static FLAC__bool add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame); |
| 80 | static unsigned evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe); |
| 81 | static unsigned evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe); |
| 82 | static unsigned evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe); |
| 83 | static unsigned evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe); |
| 84 | static unsigned find_best_partition_order_(struct FLAC__StreamEncoderPrivate *private_, const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]); |
| 85 | static void precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order); |
| 86 | static void precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 87 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 88 | static FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits); |
| 89 | static FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 90 | #else |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 91 | static FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits); |
| 92 | static FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 93 | #endif |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 94 | static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 95 | /* verify-related routines: */ |
| 96 | static void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples); |
| 97 | static void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples); |
| 98 | static FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data); |
| 99 | static FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); |
| 100 | static void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data); |
| 101 | static void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 102 | |
| 103 | /*********************************************************************** |
| 104 | * |
| 105 | * Private class data |
| 106 | * |
| 107 | ***********************************************************************/ |
| 108 | |
| 109 | typedef struct FLAC__StreamEncoderPrivate { |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 110 | unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 111 | FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */ |
| 112 | FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */ |
| 113 | FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */ |
| 114 | FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 115 | unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */ |
| 116 | unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 117 | FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */ |
| 118 | FLAC__int32 *residual_workspace_mid_side[2][2]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 119 | FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2]; |
| 120 | FLAC__Subframe subframe_workspace_mid_side[2][2]; |
| 121 | FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 122 | FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 123 | unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 124 | unsigned best_subframe_mid_side[2]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 125 | unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 126 | unsigned best_subframe_bits_mid_side[2]; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 127 | FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 128 | FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 129 | unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 130 | FLAC__BitBuffer *frame; /* the current frame being worked on */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 131 | double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 132 | unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 133 | unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 134 | FLAC__ChannelAssignment last_channel_assignment; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 135 | FLAC__StreamMetadata metadata; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 136 | unsigned current_sample_number; |
| 137 | unsigned current_frame_number; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 138 | struct MD5Context md5context; |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 139 | FLAC__CPUInfo cpuinfo; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 140 | unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 141 | void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 142 | void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 143 | void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 144 | FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */ |
| 145 | FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */ |
| 146 | FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */ |
| 147 | FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */ |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 148 | FLAC__StreamEncoderWriteCallback write_callback; |
| 149 | FLAC__StreamEncoderMetadataCallback metadata_callback; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 150 | void *client_data; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 151 | /* unaligned (original) pointers to allocated data */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 152 | FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 153 | FLAC__int32 *integer_signal_mid_side_unaligned[2]; |
| 154 | FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 155 | FLAC__real *real_signal_mid_side_unaligned[2]; |
| 156 | FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2]; |
| 157 | FLAC__int32 *residual_workspace_mid_side_unaligned[2][2]; |
| 158 | FLAC__uint32 *abs_residual_unaligned; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 159 | FLAC__uint64 *abs_residual_partition_sums_unaligned; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 160 | unsigned *raw_bits_per_partition_unaligned; |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 161 | /* |
| 162 | * These fields have been moved here from private function local |
| 163 | * declarations merely to save stack space during encoding. |
| 164 | */ |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 165 | FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */ |
| 166 | unsigned parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER]; /* from find_best_partition_order_() */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 167 | /* |
| 168 | * The data for the verify section |
| 169 | */ |
| 170 | struct { |
| 171 | FLAC__StreamDecoder *decoder; |
| 172 | EncoderStateHint state_hint; |
| 173 | FLAC__bool needs_magic_hack; |
| 174 | verify_input_fifo input_fifo; |
| 175 | verify_output output; |
| 176 | struct { |
| 177 | FLAC__uint64 absolute_sample; |
| 178 | unsigned frame_number; |
| 179 | unsigned channel; |
| 180 | unsigned sample; |
| 181 | FLAC__int32 expected; |
| 182 | FLAC__int32 got; |
| 183 | } error_stats; |
| 184 | } verify; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 185 | } FLAC__StreamEncoderPrivate; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 186 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 187 | /*********************************************************************** |
| 188 | * |
| 189 | * Public static class data |
| 190 | * |
| 191 | ***********************************************************************/ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 192 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 193 | const char * const FLAC__StreamEncoderStateString[] = { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 194 | "FLAC__STREAM_ENCODER_OK", |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 195 | "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR", |
| 196 | "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA", |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 197 | "FLAC__STREAM_ENCODER_INVALID_CALLBACK", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 198 | "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS", |
| 199 | "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE", |
| 200 | "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE", |
| 201 | "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE", |
| 202 | "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION", |
| 203 | "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH", |
| 204 | "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH", |
| 205 | "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE", |
| 206 | "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER", |
| 207 | "FLAC__STREAM_ENCODER_NOT_STREAMABLE", |
| 208 | "FLAC__STREAM_ENCODER_FRAMING_ERROR", |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 209 | "FLAC__STREAM_ENCODER_INVALID_METADATA", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 210 | "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING", |
| 211 | "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING", |
| 212 | "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR", |
| 213 | "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED", |
| 214 | "FLAC__STREAM_ENCODER_UNINITIALIZED" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 217 | const char * const FLAC__StreamEncoderWriteStatusString[] = { |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 218 | "FLAC__STREAM_ENCODER_WRITE_STATUS_OK", |
| 219 | "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 222 | /*********************************************************************** |
| 223 | * |
| 224 | * Class constructor/destructor |
| 225 | * |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 226 | */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 227 | FLAC__StreamEncoder *FLAC__stream_encoder_new() |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 228 | { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 229 | FLAC__StreamEncoder *encoder; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 230 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 231 | 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] | 232 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 233 | encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder)); |
| 234 | if(encoder == 0) { |
| 235 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 236 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 237 | memset(encoder, 0, sizeof(FLAC__StreamEncoder)); |
| 238 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 239 | encoder->protected_ = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected)); |
| 240 | if(encoder->protected_ == 0) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 241 | free(encoder); |
| 242 | return 0; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 243 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 244 | memset(encoder->protected_, 0, sizeof(FLAC__StreamEncoderProtected)); |
| 245 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 246 | encoder->private_ = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate)); |
| 247 | if(encoder->private_ == 0) { |
| 248 | free(encoder->protected_); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 249 | free(encoder); |
| 250 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 251 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 252 | memset(encoder->private_, 0, sizeof(FLAC__StreamEncoderPrivate)); |
| 253 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 254 | encoder->private_->frame = FLAC__bitbuffer_new(); |
| 255 | if(encoder->private_->frame == 0) { |
| 256 | free(encoder->private_); |
| 257 | free(encoder->protected_); |
| 258 | free(encoder); |
| 259 | return 0; |
| 260 | } |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 261 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 262 | set_defaults_(encoder); |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 263 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 264 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 265 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 266 | return encoder; |
| 267 | } |
| 268 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 269 | void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 270 | { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 271 | FLAC__ASSERT(0 != encoder); |
| 272 | FLAC__ASSERT(0 != encoder->protected_); |
| 273 | FLAC__ASSERT(0 != encoder->private_); |
| 274 | FLAC__ASSERT(0 != encoder->private_->frame); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 275 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 276 | free_(encoder); |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 277 | if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder) |
| 278 | FLAC__stream_decoder_delete(encoder->private_->verify.decoder); |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 279 | FLAC__bitbuffer_delete(encoder->private_->frame); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 280 | free(encoder->private_); |
| 281 | free(encoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 282 | free(encoder); |
| 283 | } |
| 284 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 285 | /*********************************************************************** |
| 286 | * |
| 287 | * Public class methods |
| 288 | * |
| 289 | ***********************************************************************/ |
| 290 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 291 | FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 292 | { |
| 293 | unsigned i; |
| 294 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 295 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 296 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 297 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 298 | return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 299 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 300 | encoder->protected_->state = FLAC__STREAM_ENCODER_OK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 301 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 302 | if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback) |
| 303 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 304 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 305 | if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS) |
| 306 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS; |
Josh Coalson | 69f1ee0 | 2001-01-24 00:54:43 +0000 | [diff] [blame] | 307 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 308 | if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2) |
| 309 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH; |
Josh Coalson | d37d135 | 2001-05-30 23:09:31 +0000 | [diff] [blame] | 310 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 311 | if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo) |
| 312 | return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 313 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 314 | if(encoder->protected_->bits_per_sample >= 32) |
| 315 | encoder->protected_->do_mid_side_stereo = false; /* since we 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] | 316 | |
Josh Coalson | 76c68bc | 2002-05-17 06:22:02 +0000 | [diff] [blame] | 317 | 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 | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 318 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 319 | |
Josh Coalson | 0833f34 | 2002-07-15 05:31:55 +0000 | [diff] [blame] | 320 | if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 321 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 322 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 323 | if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE) |
| 324 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 325 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 326 | if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order) |
| 327 | return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 328 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 329 | if(encoder->protected_->qlp_coeff_precision == 0) { |
| 330 | if(encoder->protected_->bits_per_sample < 16) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 331 | /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */ |
| 332 | /* @@@ until then we'll make a guess */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 333 | encoder->protected_->qlp_coeff_precision = max(5, 2 + encoder->protected_->bits_per_sample / 2); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 334 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 335 | else if(encoder->protected_->bits_per_sample == 16) { |
| 336 | if(encoder->protected_->blocksize <= 192) |
| 337 | encoder->protected_->qlp_coeff_precision = 7; |
| 338 | else if(encoder->protected_->blocksize <= 384) |
| 339 | encoder->protected_->qlp_coeff_precision = 8; |
| 340 | else if(encoder->protected_->blocksize <= 576) |
| 341 | encoder->protected_->qlp_coeff_precision = 9; |
| 342 | else if(encoder->protected_->blocksize <= 1152) |
| 343 | encoder->protected_->qlp_coeff_precision = 10; |
| 344 | else if(encoder->protected_->blocksize <= 2304) |
| 345 | encoder->protected_->qlp_coeff_precision = 11; |
| 346 | else if(encoder->protected_->blocksize <= 4608) |
| 347 | encoder->protected_->qlp_coeff_precision = 12; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 348 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 349 | encoder->protected_->qlp_coeff_precision = 13; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 350 | } |
| 351 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 352 | encoder->protected_->qlp_coeff_precision = min(13, 8*sizeof(FLAC__int32) - encoder->protected_->bits_per_sample - 1 - 2); /* @@@ -2 to keep things 32-bit safe */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 355 | else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision + encoder->protected_->bits_per_sample >= 8*sizeof(FLAC__uint32) || encoder->protected_->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)) |
| 356 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 357 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 358 | if(encoder->protected_->streamable_subset) { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 359 | /*@@@ add check for blocksize here */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 360 | if(encoder->protected_->bits_per_sample != 8 && encoder->protected_->bits_per_sample != 12 && encoder->protected_->bits_per_sample != 16 && encoder->protected_->bits_per_sample != 20 && encoder->protected_->bits_per_sample != 24) |
| 361 | return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
| 362 | if(encoder->protected_->sample_rate > 655350) |
| 363 | return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 366 | if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 367 | encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1; |
| 368 | if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order) |
| 369 | encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 370 | |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 371 | /* validate metadata */ |
| 372 | if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) |
| 373 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 374 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 375 | if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO) |
| 376 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 377 | else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) { |
Josh Coalson | 0833f34 | 2002-07-15 05:31:55 +0000 | [diff] [blame] | 378 | if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table)) |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 379 | return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA; |
| 380 | } |
| 381 | } |
| 382 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 383 | encoder->private_->input_capacity = 0; |
| 384 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 385 | encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0; |
| 386 | encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 387 | } |
| 388 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 389 | encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0; |
| 390 | encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 391 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 392 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 393 | encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0; |
| 394 | encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0; |
| 395 | encoder->private_->best_subframe[i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 396 | } |
| 397 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 398 | encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0; |
| 399 | encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0; |
| 400 | encoder->private_->best_subframe_mid_side[i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 401 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 402 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 403 | encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0]; |
| 404 | encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 405 | } |
| 406 | for(i = 0; i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 407 | encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0]; |
| 408 | encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 409 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 410 | encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0; |
| 411 | encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0; |
| 412 | encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0; |
| 413 | encoder->private_->loose_mid_side_stereo_frames_exact = (double)encoder->protected_->sample_rate * 0.4 / (double)encoder->protected_->blocksize; |
| 414 | encoder->private_->loose_mid_side_stereo_frames = (unsigned)(encoder->private_->loose_mid_side_stereo_frames_exact + 0.5); |
| 415 | if(encoder->private_->loose_mid_side_stereo_frames == 0) |
| 416 | encoder->private_->loose_mid_side_stereo_frames = 1; |
| 417 | encoder->private_->loose_mid_side_stereo_frame_count = 0; |
| 418 | encoder->private_->current_sample_number = 0; |
| 419 | encoder->private_->current_frame_number = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 420 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 421 | encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30); |
| 422 | encoder->private_->use_wide_by_order = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(max(encoder->protected_->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */ |
| 423 | encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */ |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 424 | |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 425 | /* |
| 426 | * get the CPU info and set the function pointers |
| 427 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 428 | FLAC__cpu_info(&encoder->private_->cpuinfo); |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 429 | /* first default to the non-asm routines */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 430 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation; |
| 431 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor; |
| 432 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients; |
| 433 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients; |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 434 | /* now override with asm where appropriate */ |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 435 | #ifndef FLAC__NO_ASM |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 436 | if(encoder->private_->cpuinfo.use_asm) { |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 437 | #ifdef FLAC__CPU_IA32 |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 438 | FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 439 | #ifdef FLAC__HAS_NASM |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 440 | if(0 && encoder->private_->cpuinfo.data.ia32.sse) { |
| 441 | if(encoder->protected_->max_lpc_order < 4) |
| 442 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4; |
| 443 | else if(encoder->protected_->max_lpc_order < 8) |
| 444 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8; |
| 445 | else if(encoder->protected_->max_lpc_order < 12) |
| 446 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 447 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 448 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 449 | } |
Josh Coalson | 395938e | 2001-11-15 21:53:25 +0000 | [diff] [blame] | 450 | else if(encoder->private_->cpuinfo.data.ia32._3dnow) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 451 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 452 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 453 | encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
| 454 | if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov) |
| 455 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov; |
| 456 | if(encoder->private_->cpuinfo.data.ia32.mmx) { |
| 457 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 458 | 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] | 459 | } |
| 460 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 461 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 462 | 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] | 463 | } |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 464 | #endif |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 465 | #endif |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 466 | } |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 467 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 468 | /* finally override based on wide-ness if necessary */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 469 | if(encoder->private_->use_wide_by_block) { |
| 470 | encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 471 | } |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 472 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 473 | /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 474 | encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding; |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 475 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 476 | if(!resize_buffers_(encoder, encoder->protected_->blocksize)) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 477 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 478 | return encoder->protected_->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 479 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 480 | |
| 481 | if(!FLAC__bitbuffer_init(encoder->private_->frame)) |
| 482 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 483 | |
| 484 | /* |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 485 | * Set up the verify stuff if necessary |
| 486 | */ |
| 487 | if(encoder->protected_->verify) { |
| 488 | /* |
| 489 | * First, set up the fifo which will hold the |
| 490 | * original signal to compare against |
| 491 | */ |
| 492 | encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize; |
| 493 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 494 | if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size))) |
| 495 | return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 496 | } |
| 497 | encoder->private_->verify.input_fifo.tail = 0; |
| 498 | |
| 499 | /* |
| 500 | * Now set up a stream decoder for verification |
| 501 | */ |
| 502 | encoder->private_->verify.decoder = FLAC__stream_decoder_new(); |
| 503 | if(0 == encoder->private_->verify.decoder) |
| 504 | return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 505 | |
| 506 | FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_); |
| 507 | FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_); |
| 508 | FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_); |
| 509 | FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_); |
| 510 | FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder); |
| 511 | if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA) |
| 512 | return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 513 | } |
| 514 | |
| 515 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 516 | * write the stream header |
| 517 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 518 | if(encoder->protected_->verify) |
| 519 | encoder->private_->verify.state_hint = ENCODER_IN_MAGIC; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 520 | if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 521 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 522 | if(!write_bitbuffer_(encoder, 0)) { |
| 523 | /* the above function sets the state for us in case of an error */ |
| 524 | return encoder->protected_->state; |
| 525 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 526 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 527 | /* |
| 528 | * write the STREAMINFO metadata block |
| 529 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 530 | if(encoder->protected_->verify) |
| 531 | encoder->private_->verify.state_hint = ENCODER_IN_METADATA; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 532 | encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO; |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 533 | encoder->private_->metadata.is_last = (encoder->protected_->num_metadata_blocks == 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 534 | encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 535 | encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */ |
| 536 | encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize; |
| 537 | encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 538 | encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 539 | encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate; |
| 540 | encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels; |
| 541 | encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample; |
| 542 | encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */ |
| 543 | memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */ |
| 544 | MD5Init(&encoder->private_->md5context); |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 545 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) { |
| 546 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 547 | return false; |
| 548 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 549 | if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame)) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 550 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 551 | if(!write_bitbuffer_(encoder, 0)) { |
| 552 | /* the above function sets the state for us in case of an error */ |
| 553 | return encoder->protected_->state; |
| 554 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 555 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 556 | /* |
| 557 | * Now that the STREAMINFO block is written, we can init this to an |
| 558 | * absurdly-high value... |
| 559 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 560 | encoder->private_->metadata.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] | 561 | /* ... and clear this to 0 */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 562 | encoder->private_->metadata.data.stream_info.total_samples = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 563 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 564 | /* |
| 565 | * write the user's metadata blocks |
| 566 | */ |
| 567 | for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) { |
| 568 | encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1); |
| 569 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) { |
| 570 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 571 | return false; |
| 572 | } |
| 573 | if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) |
| 574 | return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 575 | if(!write_bitbuffer_(encoder, 0)) { |
| 576 | /* the above function sets the state for us in case of an error */ |
| 577 | return encoder->protected_->state; |
| 578 | } |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 581 | if(encoder->protected_->verify) |
| 582 | encoder->private_->verify.state_hint = ENCODER_IN_AUDIO; |
| 583 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 584 | return encoder->protected_->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 587 | void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 588 | { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 589 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 590 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 591 | return; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 592 | if(encoder->private_->current_sample_number != 0) { |
| 593 | encoder->protected_->blocksize = encoder->private_->current_sample_number; |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 594 | process_frame_(encoder, true); /* true => is last frame */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 595 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 596 | MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context); |
| 597 | encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 598 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 599 | if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder) |
| 600 | FLAC__stream_decoder_finish(encoder->private_->verify.decoder); |
| 601 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 602 | free_(encoder); |
| 603 | set_defaults_(encoder); |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 604 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 605 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 608 | FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 609 | { |
| 610 | FLAC__ASSERT(0 != encoder); |
| 611 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 612 | return false; |
| 613 | encoder->protected_->verify = value; |
| 614 | return true; |
| 615 | } |
| 616 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 617 | 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] | 618 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 619 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 620 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 621 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 622 | encoder->protected_->streamable_subset = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 623 | return true; |
| 624 | } |
| 625 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 626 | FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 627 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 628 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 629 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 630 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 631 | encoder->protected_->do_mid_side_stereo = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 632 | return true; |
| 633 | } |
| 634 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 635 | FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 636 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 637 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 638 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 639 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 640 | encoder->protected_->loose_mid_side_stereo = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 641 | return true; |
| 642 | } |
| 643 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 644 | FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 645 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 646 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 647 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 648 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 649 | encoder->protected_->channels = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 650 | return true; |
| 651 | } |
| 652 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 653 | FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 654 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 655 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 656 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 657 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 658 | encoder->protected_->bits_per_sample = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 659 | return true; |
| 660 | } |
| 661 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 662 | FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 663 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 664 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 665 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 666 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 667 | encoder->protected_->sample_rate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 668 | return true; |
| 669 | } |
| 670 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 671 | FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 672 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 673 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 674 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 675 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 676 | encoder->protected_->blocksize = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 677 | return true; |
| 678 | } |
| 679 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 680 | FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 681 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 682 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 683 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 684 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 685 | encoder->protected_->max_lpc_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 686 | return true; |
| 687 | } |
| 688 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 689 | FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 690 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 691 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 692 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 693 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 694 | encoder->protected_->qlp_coeff_precision = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 695 | return true; |
| 696 | } |
| 697 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 698 | 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] | 699 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 700 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 701 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 702 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 703 | encoder->protected_->do_qlp_coeff_prec_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 704 | return true; |
| 705 | } |
| 706 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 707 | 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] | 708 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 709 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 710 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 711 | return false; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 712 | #if 0 |
| 713 | /*@@@ deprecated: */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 714 | encoder->protected_->do_escape_coding = value; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 715 | #else |
| 716 | (void)value; |
| 717 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 718 | return true; |
| 719 | } |
| 720 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 721 | 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] | 722 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 723 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 724 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 725 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 726 | encoder->protected_->do_exhaustive_model_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 727 | return true; |
| 728 | } |
| 729 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 730 | FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 731 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 732 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 733 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 734 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 735 | encoder->protected_->min_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 736 | return true; |
| 737 | } |
| 738 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 739 | FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 740 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 741 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 742 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 743 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 744 | encoder->protected_->max_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 745 | return true; |
| 746 | } |
| 747 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 748 | FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 749 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 750 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 751 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 752 | return false; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 753 | #if 0 |
| 754 | /*@@@ deprecated: */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 755 | encoder->protected_->rice_parameter_search_dist = value; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 756 | #else |
| 757 | (void)value; |
| 758 | #endif |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 759 | return true; |
| 760 | } |
| 761 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 762 | 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] | 763 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 764 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 765 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 766 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 767 | encoder->protected_->total_samples_estimate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 768 | return true; |
| 769 | } |
| 770 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 771 | FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 772 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 773 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 774 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 775 | return false; |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 776 | encoder->protected_->metadata = metadata; |
| 777 | encoder->protected_->num_metadata_blocks = num_blocks; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 778 | return true; |
| 779 | } |
| 780 | |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 781 | FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 782 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 783 | FLAC__ASSERT(0 != encoder); |
| 784 | FLAC__ASSERT(0 != value); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 785 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 786 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 787 | encoder->private_->write_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 788 | return true; |
| 789 | } |
| 790 | |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 791 | FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 792 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 793 | FLAC__ASSERT(0 != encoder); |
| 794 | FLAC__ASSERT(0 != value); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 795 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 796 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 797 | encoder->private_->metadata_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 798 | return true; |
| 799 | } |
| 800 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 801 | FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 802 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 803 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 804 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 805 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 806 | encoder->private_->client_data = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 807 | return true; |
| 808 | } |
| 809 | |
| 810 | FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 811 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 812 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 813 | return encoder->protected_->state; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 816 | FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder) |
| 817 | { |
| 818 | FLAC__ASSERT(0 != encoder); |
| 819 | if(encoder->protected_->verify) |
| 820 | return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder); |
| 821 | else |
| 822 | return FLAC__STREAM_DECODER_UNINITIALIZED; |
| 823 | } |
| 824 | |
| 825 | FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder) |
| 826 | { |
| 827 | FLAC__ASSERT(0 != encoder); |
| 828 | return encoder->protected_->verify; |
| 829 | } |
| 830 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 831 | FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 832 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 833 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 834 | return encoder->protected_->streamable_subset; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 837 | FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 838 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 839 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 840 | return encoder->protected_->do_mid_side_stereo; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 843 | FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 844 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 845 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 846 | return encoder->protected_->loose_mid_side_stereo; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 849 | unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 850 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 851 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 852 | return encoder->protected_->channels; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 855 | unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 856 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 857 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 858 | return encoder->protected_->bits_per_sample; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 861 | unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 862 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 863 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 864 | return encoder->protected_->sample_rate; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 867 | unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 868 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 869 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 870 | return encoder->protected_->blocksize; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 873 | unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 874 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 875 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 876 | return encoder->protected_->max_lpc_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 879 | unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 880 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 881 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 882 | return encoder->protected_->qlp_coeff_precision; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 885 | 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] | 886 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 887 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 888 | return encoder->protected_->do_qlp_coeff_prec_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 891 | FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder) |
| 892 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 893 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 894 | return encoder->protected_->do_escape_coding; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 897 | 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] | 898 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 899 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 900 | return encoder->protected_->do_exhaustive_model_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 903 | unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 904 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 905 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 906 | return encoder->protected_->min_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 909 | unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 910 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 911 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 912 | return encoder->protected_->max_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 915 | unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 916 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 917 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 918 | return encoder->protected_->rice_parameter_search_dist; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Josh Coalson | 3a7b2c9 | 2002-08-02 07:38:20 +0000 | [diff] [blame] | 921 | FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder) |
| 922 | { |
| 923 | FLAC__ASSERT(0 != encoder); |
| 924 | return encoder->protected_->total_samples_estimate; |
| 925 | } |
| 926 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 927 | FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 928 | { |
| 929 | unsigned i, j, channel; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 930 | FLAC__int32 x, mid, side; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 931 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 932 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 933 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 934 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 935 | |
| 936 | j = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 937 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 938 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 939 | if(encoder->protected_->verify) |
| 940 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 941 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 942 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 943 | x = mid = side = buffer[0][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 944 | encoder->private_->integer_signal[0][i] = x; |
| 945 | encoder->private_->real_signal[0][i] = (FLAC__real)x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 946 | x = buffer[1][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 947 | encoder->private_->integer_signal[1][i] = x; |
| 948 | encoder->private_->real_signal[1][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 949 | mid += x; |
| 950 | side -= x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 951 | mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 952 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 953 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
| 954 | encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side; |
| 955 | encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid; |
| 956 | encoder->private_->current_sample_number++; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 957 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 958 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 959 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 960 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 961 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 962 | } while(j < samples); |
| 963 | } |
| 964 | else { |
| 965 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 966 | if(encoder->protected_->verify) |
| 967 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 968 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 969 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 970 | for(channel = 0; channel < channels; channel++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 971 | x = buffer[channel][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 972 | encoder->private_->integer_signal[channel][i] = x; |
| 973 | encoder->private_->real_signal[channel][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 974 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 975 | encoder->private_->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 976 | } |
| 977 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 978 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 979 | return false; |
| 980 | } |
| 981 | } while(j < samples); |
| 982 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 983 | |
| 984 | return true; |
| 985 | } |
| 986 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 987 | FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 988 | { |
| 989 | unsigned i, j, k, channel; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 990 | FLAC__int32 x, mid, side; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 991 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 992 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 993 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 994 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 995 | |
| 996 | j = k = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 997 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 998 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 999 | if(encoder->protected_->verify) |
| 1000 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1001 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1002 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1003 | x = mid = side = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1004 | encoder->private_->integer_signal[0][i] = x; |
| 1005 | encoder->private_->real_signal[0][i] = (FLAC__real)x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1006 | x = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1007 | encoder->private_->integer_signal[1][i] = x; |
| 1008 | encoder->private_->real_signal[1][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1009 | mid += x; |
| 1010 | side -= x; |
| 1011 | mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1012 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 1013 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
| 1014 | encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side; |
| 1015 | encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid; |
| 1016 | encoder->private_->current_sample_number++; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1017 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1018 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1019 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1020 | return false; |
| 1021 | } |
| 1022 | } while(j < samples); |
| 1023 | } |
| 1024 | else { |
| 1025 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 1026 | if(encoder->protected_->verify) |
| 1027 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1028 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1029 | for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1030 | for(channel = 0; channel < channels; channel++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1031 | x = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1032 | encoder->private_->integer_signal[channel][i] = x; |
| 1033 | encoder->private_->real_signal[channel][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1034 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1035 | encoder->private_->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1036 | } |
| 1037 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1038 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1039 | return false; |
| 1040 | } |
| 1041 | } while(j < samples); |
| 1042 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1043 | |
| 1044 | return true; |
| 1045 | } |
| 1046 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1047 | /*********************************************************************** |
| 1048 | * |
| 1049 | * Private class methods |
| 1050 | * |
| 1051 | ***********************************************************************/ |
| 1052 | |
| 1053 | void set_defaults_(FLAC__StreamEncoder *encoder) |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1054 | { |
| 1055 | FLAC__ASSERT(0 != encoder); |
| 1056 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 1057 | encoder->protected_->verify = false; |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1058 | encoder->protected_->streamable_subset = true; |
| 1059 | encoder->protected_->do_mid_side_stereo = false; |
| 1060 | encoder->protected_->loose_mid_side_stereo = false; |
| 1061 | encoder->protected_->channels = 2; |
| 1062 | encoder->protected_->bits_per_sample = 16; |
| 1063 | encoder->protected_->sample_rate = 44100; |
| 1064 | encoder->protected_->blocksize = 1152; |
| 1065 | encoder->protected_->max_lpc_order = 0; |
| 1066 | encoder->protected_->qlp_coeff_precision = 0; |
| 1067 | encoder->protected_->do_qlp_coeff_prec_search = false; |
| 1068 | encoder->protected_->do_exhaustive_model_search = false; |
| 1069 | encoder->protected_->do_escape_coding = false; |
| 1070 | encoder->protected_->min_residual_partition_order = 0; |
| 1071 | encoder->protected_->max_residual_partition_order = 0; |
| 1072 | encoder->protected_->rice_parameter_search_dist = 0; |
| 1073 | encoder->protected_->total_samples_estimate = 0; |
| 1074 | encoder->protected_->metadata = 0; |
| 1075 | encoder->protected_->num_metadata_blocks = 0; |
| 1076 | |
| 1077 | encoder->private_->write_callback = 0; |
| 1078 | encoder->private_->metadata_callback = 0; |
| 1079 | encoder->private_->client_data = 0; |
| 1080 | } |
| 1081 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1082 | void free_(FLAC__StreamEncoder *encoder) |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1083 | { |
| 1084 | unsigned i, channel; |
| 1085 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1086 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1087 | for(i = 0; i < encoder->protected_->channels; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1088 | if(0 != encoder->private_->integer_signal_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1089 | free(encoder->private_->integer_signal_unaligned[i]); |
| 1090 | encoder->private_->integer_signal_unaligned[i] = 0; |
| 1091 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1092 | if(0 != encoder->private_->real_signal_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1093 | free(encoder->private_->real_signal_unaligned[i]); |
| 1094 | encoder->private_->real_signal_unaligned[i] = 0; |
| 1095 | } |
| 1096 | } |
| 1097 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1098 | if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1099 | free(encoder->private_->integer_signal_mid_side_unaligned[i]); |
| 1100 | encoder->private_->integer_signal_mid_side_unaligned[i] = 0; |
| 1101 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1102 | if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1103 | free(encoder->private_->real_signal_mid_side_unaligned[i]); |
| 1104 | encoder->private_->real_signal_mid_side_unaligned[i] = 0; |
| 1105 | } |
| 1106 | } |
| 1107 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
| 1108 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1109 | if(0 != encoder->private_->residual_workspace_unaligned[channel][i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1110 | free(encoder->private_->residual_workspace_unaligned[channel][i]); |
| 1111 | encoder->private_->residual_workspace_unaligned[channel][i] = 0; |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | for(channel = 0; channel < 2; channel++) { |
| 1116 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1117 | if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1118 | free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]); |
| 1119 | encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0; |
| 1120 | } |
| 1121 | } |
| 1122 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1123 | if(0 != encoder->private_->abs_residual_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1124 | free(encoder->private_->abs_residual_unaligned); |
| 1125 | encoder->private_->abs_residual_unaligned = 0; |
| 1126 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1127 | if(0 != encoder->private_->abs_residual_partition_sums_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1128 | free(encoder->private_->abs_residual_partition_sums_unaligned); |
| 1129 | encoder->private_->abs_residual_partition_sums_unaligned = 0; |
| 1130 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1131 | if(0 != encoder->private_->raw_bits_per_partition_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1132 | free(encoder->private_->raw_bits_per_partition_unaligned); |
| 1133 | encoder->private_->raw_bits_per_partition_unaligned = 0; |
| 1134 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 1135 | if(encoder->protected_->verify) { |
| 1136 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 1137 | if(0 != encoder->private_->verify.input_fifo.data[i]) { |
| 1138 | free(encoder->private_->verify.input_fifo.data[i]); |
| 1139 | encoder->private_->verify.input_fifo.data[i] = 0; |
| 1140 | } |
| 1141 | } |
| 1142 | } |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1143 | FLAC__bitbuffer_free(encoder->private_->frame); |
| 1144 | } |
| 1145 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1146 | FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1147 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1148 | FLAC__bool ok; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1149 | unsigned i, channel; |
| 1150 | |
| 1151 | FLAC__ASSERT(new_size > 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1152 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 1153 | FLAC__ASSERT(encoder->private_->current_sample_number == 0); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1154 | |
| 1155 | /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1156 | if(new_size <= encoder->private_->input_capacity) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1157 | return true; |
| 1158 | |
| 1159 | ok = true; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1160 | |
| 1161 | /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx() requires that the input arrays (in our case the integer signals) have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */ |
| 1162 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1163 | for(i = 0; ok && i < encoder->protected_->channels; i++) { |
| 1164 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]); |
| 1165 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]); |
| 1166 | memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4); |
| 1167 | encoder->private_->integer_signal[i] += 4; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1168 | } |
| 1169 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1170 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]); |
| 1171 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]); |
| 1172 | memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4); |
| 1173 | encoder->private_->integer_signal_mid_side[i] += 4; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1174 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1175 | for(channel = 0; ok && channel < encoder->protected_->channels; channel++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1176 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1177 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | for(channel = 0; ok && channel < 2; channel++) { |
| 1181 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1182 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1183 | } |
| 1184 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1185 | ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual); |
| 1186 | if(encoder->private_->precompute_partition_sums || encoder->protected_->do_escape_coding) /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */ |
| 1187 | ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums); |
| 1188 | if(encoder->protected_->do_escape_coding) |
| 1189 | ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1190 | |
| 1191 | if(ok) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1192 | encoder->private_->input_capacity = new_size; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1193 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1194 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1195 | |
| 1196 | return ok; |
| 1197 | } |
| 1198 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 1199 | FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples) |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1200 | { |
| 1201 | const FLAC__byte *buffer; |
| 1202 | unsigned bytes; |
| 1203 | |
| 1204 | FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame)); |
| 1205 | |
| 1206 | FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes); |
| 1207 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 1208 | if(encoder->protected_->verify) { |
| 1209 | encoder->private_->verify.output.data = buffer; |
| 1210 | encoder->private_->verify.output.bytes = bytes; |
| 1211 | if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) { |
| 1212 | encoder->private_->verify.needs_magic_hack = true; |
| 1213 | } |
| 1214 | else { |
| 1215 | if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) { |
| 1216 | FLAC__bitbuffer_release_buffer(encoder->private_->frame); |
| 1217 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) |
| 1218 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 1219 | return false; |
| 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 1225 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING; |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1226 | return false; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 1227 | } |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1228 | |
| 1229 | FLAC__bitbuffer_release_buffer(encoder->private_->frame); |
| 1230 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 1231 | if(samples > 0) { |
| 1232 | encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize); |
| 1233 | encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize); |
| 1234 | } |
| 1235 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1236 | return true; |
| 1237 | } |
| 1238 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1239 | FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1240 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1241 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1242 | |
| 1243 | /* |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 1244 | * Accumulate raw signal to the MD5 signature |
| 1245 | */ |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1246 | if(!FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1247 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 1248 | return false; |
| 1249 | } |
| 1250 | |
| 1251 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1252 | * Process the frame header and subframes into the frame bitbuffer |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1253 | */ |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1254 | if(!process_subframes_(encoder, is_last_frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1255 | /* 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] | 1256 | return false; |
| 1257 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1258 | |
| 1259 | /* |
| 1260 | * Zero-pad the frame to a byte_boundary |
| 1261 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1262 | if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1263 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1264 | return false; |
| 1265 | } |
| 1266 | |
| 1267 | /* |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1268 | * CRC-16 the whole thing |
| 1269 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1270 | FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame)); |
| 1271 | FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__bitbuffer_get_write_crc16(encoder->private_->frame), FLAC__FRAME_FOOTER_CRC_LEN); |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1272 | |
| 1273 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1274 | * Write it |
| 1275 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 1276 | if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) { |
| 1277 | /* 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] | 1278 | return false; |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | * Get ready for the next frame |
| 1283 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1284 | encoder->private_->current_sample_number = 0; |
| 1285 | encoder->private_->current_frame_number++; |
| 1286 | encoder->private_->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1287 | |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1291 | FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1292 | { |
| 1293 | FLAC__FrameHeader frame_header; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1294 | unsigned channel, min_partition_order = encoder->protected_->min_residual_partition_order, max_partition_order; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1295 | FLAC__bool do_independent, do_mid_side, precompute_partition_sums; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1296 | |
| 1297 | /* |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1298 | * Calculate the min,max Rice partition orders |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1299 | */ |
| 1300 | if(is_last_frame) { |
| 1301 | max_partition_order = 0; |
| 1302 | } |
| 1303 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1304 | unsigned limit = 0, b = encoder->protected_->blocksize; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1305 | while(!(b & 1)) { |
| 1306 | limit++; |
| 1307 | b >>= 1; |
| 1308 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1309 | max_partition_order = min(encoder->protected_->max_residual_partition_order, limit); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1310 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1311 | min_partition_order = min(min_partition_order, max_partition_order); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1312 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1313 | precompute_partition_sums = encoder->private_->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected_->do_escape_coding); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1314 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1315 | /* |
| 1316 | * Setup the frame |
| 1317 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1318 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1319 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1320 | return false; |
| 1321 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1322 | frame_header.blocksize = encoder->protected_->blocksize; |
| 1323 | frame_header.sample_rate = encoder->protected_->sample_rate; |
| 1324 | frame_header.channels = encoder->protected_->channels; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1325 | 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] | 1326 | frame_header.bits_per_sample = encoder->protected_->bits_per_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1327 | frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1328 | frame_header.number.frame_number = encoder->private_->current_frame_number; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1329 | |
| 1330 | /* |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1331 | * Figure out what channel assignments to try |
| 1332 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1333 | if(encoder->protected_->do_mid_side_stereo) { |
| 1334 | if(encoder->protected_->loose_mid_side_stereo) { |
| 1335 | if(encoder->private_->loose_mid_side_stereo_frame_count == 0) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1336 | do_independent = true; |
| 1337 | do_mid_side = true; |
| 1338 | } |
| 1339 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1340 | do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1341 | do_mid_side = !do_independent; |
| 1342 | } |
| 1343 | } |
| 1344 | else { |
| 1345 | do_independent = true; |
| 1346 | do_mid_side = true; |
| 1347 | } |
| 1348 | } |
| 1349 | else { |
| 1350 | do_independent = true; |
| 1351 | do_mid_side = false; |
| 1352 | } |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1353 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1354 | FLAC__ASSERT(do_independent || do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1355 | |
| 1356 | /* |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1357 | * Check for wasted bits; set effective bps for each subframe |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1358 | */ |
| 1359 | if(do_independent) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1360 | unsigned w; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1361 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1362 | w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1363 | encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w; |
| 1364 | encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1365 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1366 | } |
| 1367 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1368 | unsigned w; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1369 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1370 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1371 | w = get_wasted_bits_(encoder->private_->integer_signal_mid_side[channel], encoder->protected_->blocksize); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1372 | encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w; |
| 1373 | 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] | 1374 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1378 | * First do a normal encoding pass of each independent channel |
| 1379 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1380 | if(do_independent) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1381 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1382 | if(!process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private_->subframe_bps[channel], encoder->private_->integer_signal[channel], encoder->private_->real_signal[channel], encoder->private_->subframe_workspace_ptr[channel], encoder->private_->residual_workspace[channel], encoder->private_->best_subframe+channel, encoder->private_->best_subframe_bits+channel)) |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1383 | return false; |
| 1384 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /* |
| 1388 | * Now do mid and side channels if requested |
| 1389 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1390 | if(do_mid_side) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1391 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1392 | |
| 1393 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1394 | if(!process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private_->subframe_bps_mid_side[channel], encoder->private_->integer_signal_mid_side[channel], encoder->private_->real_signal_mid_side[channel], encoder->private_->subframe_workspace_ptr_mid_side[channel], encoder->private_->residual_workspace_mid_side[channel], encoder->private_->best_subframe_mid_side+channel, encoder->private_->best_subframe_bits_mid_side+channel)) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1395 | return false; |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | * Compose the frame bitbuffer |
| 1401 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1402 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1403 | unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ |
| 1404 | 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] | 1405 | FLAC__ChannelAssignment channel_assignment; |
| 1406 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1407 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1408 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1409 | if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) { |
| 1410 | 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] | 1411 | } |
| 1412 | else { |
| 1413 | unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */ |
| 1414 | unsigned min_bits; |
| 1415 | FLAC__ChannelAssignment ca; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1416 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1417 | FLAC__ASSERT(do_independent && do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1418 | |
| 1419 | /* 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] | 1420 | bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1]; |
| 1421 | bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 1422 | bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 1423 | 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] | 1424 | |
| 1425 | for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) { |
| 1426 | if(bits[ca] < min_bits) { |
| 1427 | min_bits = bits[ca]; |
| 1428 | channel_assignment = ca; |
| 1429 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1433 | frame_header.channel_assignment = channel_assignment; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1434 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1435 | if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1436 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1437 | return false; |
| 1438 | } |
| 1439 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1440 | switch(channel_assignment) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1441 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1442 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]]; |
| 1443 | right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1444 | break; |
| 1445 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1446 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]]; |
| 1447 | 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] | 1448 | break; |
| 1449 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1450 | left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
| 1451 | right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1452 | break; |
| 1453 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1454 | left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]]; |
| 1455 | 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] | 1456 | break; |
| 1457 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1458 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1459 | } |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1460 | |
| 1461 | switch(channel_assignment) { |
| 1462 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1463 | left_bps = encoder->private_->subframe_bps [0]; |
| 1464 | right_bps = encoder->private_->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1465 | break; |
| 1466 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1467 | left_bps = encoder->private_->subframe_bps [0]; |
| 1468 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1469 | break; |
| 1470 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1471 | left_bps = encoder->private_->subframe_bps_mid_side[1]; |
| 1472 | right_bps = encoder->private_->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1473 | break; |
| 1474 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1475 | left_bps = encoder->private_->subframe_bps_mid_side[0]; |
| 1476 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1477 | break; |
| 1478 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1479 | FLAC__ASSERT(0); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | /* note that encoder_add_subframe_ sets the state for us in case of an error */ |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1483 | if(!add_subframe_(encoder, &frame_header, left_bps , left_subframe , encoder->private_->frame)) |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1484 | return false; |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1485 | if(!add_subframe_(encoder, &frame_header, right_bps, right_subframe, encoder->private_->frame)) |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1486 | return false; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1487 | } |
| 1488 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1489 | if(!FLAC__frame_add_header(&frame_header, encoder->protected_->streamable_subset, is_last_frame, encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1490 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1491 | return false; |
| 1492 | } |
| 1493 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1494 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1495 | if(!add_subframe_(encoder, &frame_header, encoder->private_->subframe_bps[channel], &encoder->private_->subframe_workspace[channel][encoder->private_->best_subframe[channel]], encoder->private_->frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1496 | /* the above function sets the state for us in case of an error */ |
| 1497 | return false; |
| 1498 | } |
| 1499 | } |
| 1500 | } |
| 1501 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1502 | if(encoder->protected_->loose_mid_side_stereo) { |
| 1503 | encoder->private_->loose_mid_side_stereo_frame_count++; |
| 1504 | if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames) |
| 1505 | encoder->private_->loose_mid_side_stereo_frame_count = 0; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1508 | encoder->private_->last_channel_assignment = frame_header.channel_assignment; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1509 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1510 | return true; |
| 1511 | } |
| 1512 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1513 | FLAC__bool process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1514 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1515 | FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 1516 | FLAC__real lpc_residual_bits_per_sample; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1517 | FLAC__real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->protected_->max_lpc_order might be less; some asm routines need all the space */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1518 | FLAC__real lpc_error[FLAC__MAX_LPC_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1519 | unsigned min_lpc_order, max_lpc_order, lpc_order; |
| 1520 | unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order; |
| 1521 | unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision; |
| 1522 | unsigned rice_parameter; |
| 1523 | unsigned _candidate_bits, _best_bits; |
| 1524 | unsigned _best_subframe; |
| 1525 | |
| 1526 | /* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 1527 | _best_subframe = 0; |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1528 | _best_bits = evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1529 | |
| 1530 | if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { |
| 1531 | /* check for constant subframe */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1532 | guess_fixed_order = encoder->private_->local_fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1533 | if(fixed_residual_bits_per_sample[1] == 0.0) { |
| 1534 | /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */ |
| 1535 | unsigned i, signal_is_constant = true; |
| 1536 | for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) { |
| 1537 | if(integer_signal[0] != integer_signal[i]) { |
| 1538 | signal_is_constant = false; |
| 1539 | break; |
| 1540 | } |
| 1541 | } |
| 1542 | if(signal_is_constant) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1543 | _candidate_bits = evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1544 | if(_candidate_bits < _best_bits) { |
| 1545 | _best_subframe = !_best_subframe; |
| 1546 | _best_bits = _candidate_bits; |
| 1547 | } |
| 1548 | } |
| 1549 | } |
| 1550 | else { |
| 1551 | /* encode fixed */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1552 | if(encoder->protected_->do_exhaustive_model_search) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1553 | min_fixed_order = 0; |
| 1554 | max_fixed_order = FLAC__MAX_FIXED_ORDER; |
| 1555 | } |
| 1556 | else { |
| 1557 | min_fixed_order = max_fixed_order = guess_fixed_order; |
| 1558 | } |
| 1559 | for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1560 | if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1561 | continue; /* don't even try */ |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 1562 | rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > 0.0)? (unsigned)(fixed_residual_bits_per_sample[fixed_order]+0.5) : 0; /* 0.5 is for rounding */ |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1563 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 1564 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1565 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1566 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1567 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1568 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1569 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1570 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1571 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1572 | _candidate_bits = evaluate_fixed_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private_->abs_residual, encoder->private_->abs_residual_partition_sums, encoder->private_->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected_->do_escape_coding, encoder->protected_->rice_parameter_search_dist, subframe[!_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1573 | if(_candidate_bits < _best_bits) { |
| 1574 | _best_subframe = !_best_subframe; |
| 1575 | _best_bits = _candidate_bits; |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | /* encode lpc */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1580 | if(encoder->protected_->max_lpc_order > 0) { |
| 1581 | if(encoder->protected_->max_lpc_order >= frame_header->blocksize) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1582 | max_lpc_order = frame_header->blocksize-1; |
| 1583 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1584 | max_lpc_order = encoder->protected_->max_lpc_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1585 | if(max_lpc_order > 0) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1586 | encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1587 | /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */ |
| 1588 | if(autoc[0] != 0.0) { |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1589 | FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1590 | if(encoder->protected_->do_exhaustive_model_search) { |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1591 | min_lpc_order = 1; |
| 1592 | } |
| 1593 | else { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1594 | unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1595 | min_lpc_order = max_lpc_order = guess_lpc_order; |
| 1596 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1597 | if(encoder->protected_->do_qlp_coeff_prec_search) { |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1598 | min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1599 | max_qlp_coeff_precision = min(8*sizeof(FLAC__int32) - subframe_bps - 1 - 2, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1); /* -2 to keep things 32-bit safe */ |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1600 | } |
| 1601 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1602 | min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision; |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1603 | } |
| 1604 | for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) { |
| 1605 | lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order); |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1606 | if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps) |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1607 | continue; /* don't even try */ |
| 1608 | rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */ |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1609 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1610 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1611 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1612 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1613 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1614 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1615 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1616 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1617 | } |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1618 | for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1619 | _candidate_bits = evaluate_lpc_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private_->abs_residual, encoder->private_->abs_residual_partition_sums, encoder->private_->raw_bits_per_partition, encoder->private_->lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected_->do_escape_coding, encoder->protected_->rice_parameter_search_dist, subframe[!_best_subframe]); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1620 | if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 1621 | if(_candidate_bits < _best_bits) { |
| 1622 | _best_subframe = !_best_subframe; |
| 1623 | _best_bits = _candidate_bits; |
| 1624 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1625 | } |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | *best_subframe = _best_subframe; |
| 1635 | *best_bits = _best_bits; |
| 1636 | |
| 1637 | return true; |
| 1638 | } |
| 1639 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1640 | FLAC__bool add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1641 | { |
| 1642 | switch(subframe->type) { |
| 1643 | case FLAC__SUBFRAME_TYPE_CONSTANT: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1644 | if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1645 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1646 | return false; |
| 1647 | } |
| 1648 | break; |
| 1649 | case FLAC__SUBFRAME_TYPE_FIXED: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1650 | if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1651 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1652 | return false; |
| 1653 | } |
| 1654 | break; |
| 1655 | case FLAC__SUBFRAME_TYPE_LPC: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1656 | if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1657 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1658 | return false; |
| 1659 | } |
| 1660 | break; |
| 1661 | case FLAC__SUBFRAME_TYPE_VERBATIM: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1662 | if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1663 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1664 | return false; |
| 1665 | } |
| 1666 | break; |
| 1667 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1668 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | return true; |
| 1672 | } |
| 1673 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1674 | unsigned evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1675 | { |
| 1676 | subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 1677 | subframe->data.constant.value = signal; |
| 1678 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1679 | return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1682 | unsigned evaluate_fixed_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1683 | { |
| 1684 | unsigned i, residual_bits; |
| 1685 | const unsigned residual_samples = blocksize - order; |
| 1686 | |
| 1687 | FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual); |
| 1688 | |
| 1689 | subframe->type = FLAC__SUBFRAME_TYPE_FIXED; |
| 1690 | |
| 1691 | subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1692 | subframe->data.fixed.residual = residual; |
| 1693 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1694 | residual_bits = find_best_partition_order_(encoder->private_, residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, &subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.raw_bits); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1695 | |
| 1696 | subframe->data.fixed.order = order; |
| 1697 | for(i = 0; i < order; i++) |
| 1698 | subframe->data.fixed.warmup[i] = signal[i]; |
| 1699 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1700 | return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1703 | unsigned evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1704 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1705 | FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1706 | unsigned i, residual_bits; |
| 1707 | int quantization, ret; |
| 1708 | const unsigned residual_samples = blocksize - order; |
| 1709 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1710 | ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1711 | if(ret != 0) |
| 1712 | return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ |
| 1713 | |
Josh Coalson | 92d4240 | 2001-05-31 20:53:19 +0000 | [diff] [blame] | 1714 | if(subframe_bps <= 16 && qlp_coeff_precision <= 16) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1715 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
Josh Coalson | 92d4240 | 2001-05-31 20:53:19 +0000 | [diff] [blame] | 1716 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1717 | encoder->private_->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1718 | |
| 1719 | subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 1720 | |
| 1721 | subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1722 | subframe->data.lpc.residual = residual; |
| 1723 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1724 | residual_bits = find_best_partition_order_(encoder->private_, residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, &subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.raw_bits); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1725 | |
| 1726 | subframe->data.lpc.order = order; |
| 1727 | subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 1728 | subframe->data.lpc.quantization_level = quantization; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1729 | 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] | 1730 | for(i = 0; i < order; i++) |
| 1731 | subframe->data.lpc.warmup[i] = signal[i]; |
| 1732 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1733 | return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1736 | unsigned evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1737 | { |
| 1738 | subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 1739 | |
| 1740 | subframe->data.verbatim.data = signal; |
| 1741 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1742 | return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1745 | unsigned find_best_partition_order_(FLAC__StreamEncoderPrivate *private_, const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1746 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1747 | FLAC__int32 r; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1748 | unsigned residual_bits, best_residual_bits = 0; |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1749 | unsigned residual_sample; |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1750 | unsigned best_parameters_index = 0; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1751 | const unsigned blocksize = residual_samples + predictor_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1752 | |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1753 | /* compute abs(residual) for use later */ |
| 1754 | for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) { |
| 1755 | r = residual[residual_sample]; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1756 | abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r); |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1759 | while(max_partition_order > 0 && blocksize >> max_partition_order <= predictor_order) |
| 1760 | max_partition_order--; |
| 1761 | FLAC__ASSERT(blocksize >> max_partition_order > predictor_order); |
| 1762 | min_partition_order = min(min_partition_order, max_partition_order); |
| 1763 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1764 | if(precompute_partition_sums) { |
| 1765 | int partition_order; |
| 1766 | unsigned sum; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1767 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1768 | precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1769 | |
| 1770 | if(do_escape_coding) |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1771 | precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1772 | |
| 1773 | for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) { |
| 1774 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1775 | if(!set_partitioned_rice_with_precompute_(residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, private_->parameters[!best_parameters_index], private_->raw_bits[!best_parameters_index], &residual_bits)) |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1776 | #else |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1777 | if(!set_partitioned_rice_with_precompute_(abs_residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, private_->parameters[!best_parameters_index], private_->raw_bits[!best_parameters_index], &residual_bits)) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1778 | #endif |
| 1779 | { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1780 | FLAC__ASSERT(best_residual_bits != 0); |
| 1781 | break; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1782 | } |
| 1783 | sum += 1u << partition_order; |
| 1784 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 1785 | best_residual_bits = residual_bits; |
| 1786 | *best_partition_order = partition_order; |
| 1787 | best_parameters_index = !best_parameters_index; |
| 1788 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1789 | } |
| 1790 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1791 | else { |
| 1792 | unsigned partition_order; |
| 1793 | for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) { |
| 1794 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1795 | if(!set_partitioned_rice_(abs_residual, residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, private_->parameters[!best_parameters_index], &residual_bits)) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1796 | #else |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1797 | if(!set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, private_->parameters[!best_parameters_index], &residual_bits)) |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1798 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1799 | { |
| 1800 | FLAC__ASSERT(best_residual_bits != 0); |
| 1801 | break; |
| 1802 | } |
| 1803 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 1804 | best_residual_bits = residual_bits; |
| 1805 | *best_partition_order = partition_order; |
| 1806 | best_parameters_index = !best_parameters_index; |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1811 | memcpy(best_parameters, private_->parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order))); |
| 1812 | memcpy(best_raw_bits, private_->raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order))); |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1813 | |
| 1814 | return best_residual_bits; |
| 1815 | } |
| 1816 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1817 | void precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order) |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1818 | { |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1819 | int partition_order; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1820 | unsigned from_partition, to_partition = 0; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1821 | const unsigned blocksize = residual_samples + predictor_order; |
| 1822 | |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1823 | /* first do max_partition_order */ |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1824 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1825 | FLAC__uint64 abs_residual_partition_sum; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1826 | FLAC__uint32 abs_r; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1827 | unsigned partition, partition_sample, partition_samples, residual_sample; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1828 | const unsigned partitions = 1u << partition_order; |
| 1829 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 1830 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1831 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 1832 | |
| 1833 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 1834 | partition_samples = default_partition_samples; |
| 1835 | if(partition == 0) |
| 1836 | partition_samples -= predictor_order; |
| 1837 | abs_residual_partition_sum = 0; |
| 1838 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 1839 | abs_r = abs_residual[residual_sample]; |
| 1840 | abs_residual_partition_sum += abs_r; |
| 1841 | residual_sample++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1842 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1843 | abs_residual_partition_sums[partition] = abs_residual_partition_sum; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1844 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1845 | to_partition = partitions; |
| 1846 | break; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1847 | } |
Josh Coalson | f76a361 | 2001-04-18 02:28:11 +0000 | [diff] [blame] | 1848 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1849 | /* now merge partitions for lower orders */ |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame] | 1850 | for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1851 | FLAC__uint64 s; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1852 | unsigned i; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1853 | const unsigned partitions = 1u << partition_order; |
| 1854 | for(i = 0; i < partitions; i++) { |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1855 | s = abs_residual_partition_sums[from_partition]; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1856 | from_partition++; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1857 | abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition]; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1858 | from_partition++; |
| 1859 | to_partition++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1860 | } |
| 1861 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1862 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1863 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1864 | void precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1865 | { |
| 1866 | int partition_order; |
| 1867 | unsigned from_partition, to_partition = 0; |
| 1868 | const unsigned blocksize = residual_samples + predictor_order; |
| 1869 | |
| 1870 | /* first do max_partition_order */ |
| 1871 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
| 1872 | FLAC__int32 r, residual_partition_min, residual_partition_max; |
| 1873 | unsigned silog2_min, silog2_max; |
| 1874 | unsigned partition, partition_sample, partition_samples, residual_sample; |
| 1875 | const unsigned partitions = 1u << partition_order; |
| 1876 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 1877 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1878 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 1879 | |
| 1880 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 1881 | partition_samples = default_partition_samples; |
| 1882 | if(partition == 0) |
| 1883 | partition_samples -= predictor_order; |
| 1884 | residual_partition_min = residual_partition_max = 0; |
| 1885 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 1886 | r = residual[residual_sample]; |
| 1887 | if(r < residual_partition_min) |
| 1888 | residual_partition_min = r; |
| 1889 | else if(r > residual_partition_max) |
| 1890 | residual_partition_max = r; |
| 1891 | residual_sample++; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1892 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1893 | silog2_min = FLAC__bitmath_silog2(residual_partition_min); |
| 1894 | silog2_max = FLAC__bitmath_silog2(residual_partition_max); |
| 1895 | raw_bits_per_partition[partition] = max(silog2_min, silog2_max); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1896 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1897 | to_partition = partitions; |
| 1898 | break; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /* now merge partitions for lower orders */ |
| 1902 | for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
| 1903 | unsigned m; |
| 1904 | unsigned i; |
| 1905 | const unsigned partitions = 1u << partition_order; |
| 1906 | for(i = 0; i < partitions; i++) { |
| 1907 | m = raw_bits_per_partition[from_partition]; |
| 1908 | from_partition++; |
| 1909 | raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]); |
| 1910 | from_partition++; |
| 1911 | to_partition++; |
| 1912 | } |
| 1913 | } |
| 1914 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1915 | |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1916 | #ifdef VARIABLE_RICE_BITS |
| 1917 | #undef VARIABLE_RICE_BITS |
| 1918 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1919 | #ifndef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1920 | #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter)) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1921 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1922 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1923 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1924 | FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1925 | #else |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1926 | FLAC__bool set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1927 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1928 | { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1929 | unsigned rice_parameter, partition_bits; |
| 1930 | #ifndef NO_RICE_SEARCH |
| 1931 | unsigned best_partition_bits; |
| 1932 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 1933 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1934 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
| 1935 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1936 | FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER); |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1937 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1938 | if(partition_order == 0) { |
| 1939 | unsigned i; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1940 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1941 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1942 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1943 | if(suggested_rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1944 | min_rice_parameter = 0; |
| 1945 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1946 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 1947 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1948 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1949 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1950 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1951 | #endif |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1952 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1953 | } |
| 1954 | } |
| 1955 | else |
| 1956 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
| 1957 | |
| 1958 | best_partition_bits = 0xffffffff; |
| 1959 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 1960 | #endif |
| 1961 | #ifdef VARIABLE_RICE_BITS |
| 1962 | #ifdef FLAC__SYMMETRIC_RICE |
| 1963 | partition_bits = (2+rice_parameter) * residual_samples; |
| 1964 | #else |
| 1965 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 1966 | partition_bits = (1+rice_parameter) * residual_samples; |
| 1967 | #endif |
| 1968 | #else |
| 1969 | partition_bits = 0; |
| 1970 | #endif |
| 1971 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 1972 | for(i = 0; i < residual_samples; i++) { |
| 1973 | #ifdef VARIABLE_RICE_BITS |
| 1974 | #ifdef FLAC__SYMMETRIC_RICE |
| 1975 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter); |
| 1976 | #else |
| 1977 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
| 1978 | #endif |
| 1979 | #else |
| 1980 | partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */ |
| 1981 | #endif |
| 1982 | } |
| 1983 | #ifndef NO_RICE_SEARCH |
| 1984 | if(partition_bits < best_partition_bits) { |
| 1985 | best_rice_parameter = rice_parameter; |
| 1986 | best_partition_bits = partition_bits; |
| 1987 | } |
| 1988 | } |
| 1989 | #endif |
| 1990 | parameters[0] = best_rice_parameter; |
| 1991 | bits_ += best_partition_bits; |
| 1992 | } |
| 1993 | else { |
| 1994 | unsigned partition, residual_sample, save_residual_sample, partition_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1995 | unsigned partition_samples; |
| 1996 | FLAC__uint64 mean, k; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1997 | const unsigned partitions = 1u << partition_order; |
| 1998 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 1999 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
| 2000 | if(partition == 0) { |
| 2001 | if(partition_samples <= predictor_order) |
| 2002 | return false; |
| 2003 | else |
| 2004 | partition_samples -= predictor_order; |
| 2005 | } |
| 2006 | mean = 0; |
| 2007 | save_residual_sample = residual_sample; |
| 2008 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2009 | mean += abs_residual[residual_sample]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2010 | residual_sample = save_residual_sample; |
| 2011 | #ifdef FLAC__SYMMETRIC_RICE |
| 2012 | mean += partition_samples >> 1; /* for rounding effect */ |
| 2013 | mean /= partition_samples; |
| 2014 | |
| 2015 | /* calc rice_parameter = floor(log2(mean)) */ |
| 2016 | rice_parameter = 0; |
| 2017 | mean>>=1; |
| 2018 | while(mean) { |
| 2019 | rice_parameter++; |
| 2020 | mean >>= 1; |
| 2021 | } |
| 2022 | #else |
| 2023 | /* calc rice_parameter ala LOCO-I */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2024 | for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2025 | ; |
| 2026 | #endif |
| 2027 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2028 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2029 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2030 | #endif |
| 2031 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 2032 | } |
| 2033 | |
| 2034 | #ifndef NO_RICE_SEARCH |
| 2035 | if(rice_parameter_search_dist) { |
| 2036 | if(rice_parameter < rice_parameter_search_dist) |
| 2037 | min_rice_parameter = 0; |
| 2038 | else |
| 2039 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 2040 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
| 2041 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2042 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2043 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2044 | #endif |
| 2045 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 2046 | } |
| 2047 | } |
| 2048 | else |
| 2049 | min_rice_parameter = max_rice_parameter = rice_parameter; |
| 2050 | |
| 2051 | best_partition_bits = 0xffffffff; |
| 2052 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 2053 | #endif |
| 2054 | #ifdef VARIABLE_RICE_BITS |
| 2055 | #ifdef FLAC__SYMMETRIC_RICE |
| 2056 | partition_bits = (2+rice_parameter) * partition_samples; |
| 2057 | #else |
| 2058 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 2059 | partition_bits = (1+rice_parameter) * partition_samples; |
| 2060 | #endif |
| 2061 | #else |
| 2062 | partition_bits = 0; |
| 2063 | #endif |
| 2064 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 2065 | save_residual_sample = residual_sample; |
| 2066 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) { |
| 2067 | #ifdef VARIABLE_RICE_BITS |
| 2068 | #ifdef FLAC__SYMMETRIC_RICE |
| 2069 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter); |
| 2070 | #else |
| 2071 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate); |
| 2072 | #endif |
| 2073 | #else |
| 2074 | partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */ |
| 2075 | #endif |
| 2076 | } |
| 2077 | #ifndef NO_RICE_SEARCH |
| 2078 | if(rice_parameter != max_rice_parameter) |
| 2079 | residual_sample = save_residual_sample; |
| 2080 | if(partition_bits < best_partition_bits) { |
| 2081 | best_rice_parameter = rice_parameter; |
| 2082 | best_partition_bits = partition_bits; |
| 2083 | } |
| 2084 | } |
| 2085 | #endif |
| 2086 | parameters[partition] = best_rice_parameter; |
| 2087 | bits_ += best_partition_bits; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | *bits = bits_; |
| 2092 | return true; |
| 2093 | } |
| 2094 | |
| 2095 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2096 | FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2097 | #else |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2098 | FLAC__bool set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2099 | #endif |
| 2100 | { |
| 2101 | unsigned rice_parameter, partition_bits; |
| 2102 | #ifndef NO_RICE_SEARCH |
| 2103 | unsigned best_partition_bits; |
| 2104 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 2105 | #endif |
| 2106 | unsigned flat_bits; |
| 2107 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
| 2108 | |
| 2109 | FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER); |
| 2110 | |
| 2111 | if(partition_order == 0) { |
| 2112 | unsigned i; |
| 2113 | |
| 2114 | #ifndef NO_RICE_SEARCH |
| 2115 | if(rice_parameter_search_dist) { |
| 2116 | if(suggested_rice_parameter < rice_parameter_search_dist) |
| 2117 | min_rice_parameter = 0; |
| 2118 | else |
| 2119 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 2120 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
| 2121 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2122 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2123 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2124 | #endif |
| 2125 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 2126 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2127 | } |
| 2128 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2129 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2130 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2131 | best_partition_bits = 0xffffffff; |
| 2132 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 2133 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2134 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2135 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2136 | partition_bits = (2+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2137 | #else |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2138 | const unsigned rice_parameter_estimate = rice_parameter-1; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2139 | partition_bits = (1+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2140 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2141 | #else |
| 2142 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2143 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2144 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2145 | for(i = 0; i < residual_samples; i++) { |
| 2146 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2147 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2148 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2149 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2150 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2151 | #endif |
| 2152 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2153 | partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2154 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2155 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2156 | #ifndef NO_RICE_SEARCH |
| 2157 | if(partition_bits < best_partition_bits) { |
| 2158 | best_rice_parameter = rice_parameter; |
| 2159 | best_partition_bits = partition_bits; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2160 | } |
| 2161 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2162 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2163 | if(search_for_escapes) { |
| 2164 | flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples; |
| 2165 | if(flat_bits <= best_partition_bits) { |
| 2166 | raw_bits[0] = raw_bits_per_partition[0]; |
| 2167 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 2168 | best_partition_bits = flat_bits; |
| 2169 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2170 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2171 | parameters[0] = best_rice_parameter; |
| 2172 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2173 | } |
| 2174 | else { |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2175 | unsigned partition, residual_sample, save_residual_sample, partition_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2176 | unsigned partition_samples; |
| 2177 | FLAC__uint64 mean, k; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2178 | const unsigned partitions = 1u << partition_order; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2179 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2180 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2181 | if(partition == 0) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2182 | if(partition_samples <= predictor_order) |
| 2183 | return false; |
| 2184 | else |
| 2185 | partition_samples -= predictor_order; |
| 2186 | } |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 2187 | mean = abs_residual_partition_sums[partition]; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2188 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 2189 | mean += partition_samples >> 1; /* for rounding effect */ |
| 2190 | mean /= partition_samples; |
| 2191 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2192 | /* calc rice_parameter = floor(log2(mean)) */ |
| 2193 | rice_parameter = 0; |
| 2194 | mean>>=1; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2195 | while(mean) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2196 | rice_parameter++; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2197 | mean >>= 1; |
| 2198 | } |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2199 | #else |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 2200 | /* calc rice_parameter ala LOCO-I */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2201 | 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] | 2202 | ; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2203 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2204 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2205 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2206 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2207 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2208 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2209 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2210 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2211 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2212 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2213 | if(rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2214 | min_rice_parameter = 0; |
| 2215 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2216 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 2217 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2218 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2219 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2220 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2221 | #endif |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2222 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2223 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2224 | } |
| 2225 | else |
| 2226 | min_rice_parameter = max_rice_parameter = rice_parameter; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2227 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2228 | best_partition_bits = 0xffffffff; |
| 2229 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 2230 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2231 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2232 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2233 | partition_bits = (2+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2234 | #else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2235 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 2236 | partition_bits = (1+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2237 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2238 | #else |
| 2239 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2240 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2241 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2242 | save_residual_sample = residual_sample; |
| 2243 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) { |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2244 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2245 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2246 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2247 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2248 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2249 | #endif |
| 2250 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2251 | partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2252 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2253 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2254 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2255 | if(rice_parameter != max_rice_parameter) |
| 2256 | residual_sample = save_residual_sample; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2257 | if(partition_bits < best_partition_bits) { |
| 2258 | best_rice_parameter = rice_parameter; |
| 2259 | best_partition_bits = partition_bits; |
| 2260 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2261 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2262 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2263 | if(search_for_escapes) { |
| 2264 | flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples; |
| 2265 | if(flat_bits <= best_partition_bits) { |
| 2266 | raw_bits[partition] = raw_bits_per_partition[partition]; |
| 2267 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 2268 | best_partition_bits = flat_bits; |
| 2269 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2270 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2271 | parameters[partition] = best_rice_parameter; |
| 2272 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | *bits = bits_; |
| 2277 | return true; |
| 2278 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2279 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2280 | unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples) |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2281 | { |
| 2282 | unsigned i, shift; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2283 | FLAC__int32 x = 0; |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2284 | |
| 2285 | for(i = 0; i < samples && !(x&1); i++) |
| 2286 | x |= signal[i]; |
| 2287 | |
| 2288 | if(x == 0) { |
| 2289 | shift = 0; |
| 2290 | } |
| 2291 | else { |
| 2292 | for(shift = 0; !(x&1); shift++) |
| 2293 | x >>= 1; |
| 2294 | } |
| 2295 | |
| 2296 | if(shift > 0) { |
| 2297 | for(i = 0; i < samples; i++) |
| 2298 | signal[i] >>= shift; |
| 2299 | } |
| 2300 | |
| 2301 | return shift; |
| 2302 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame^] | 2303 | |
| 2304 | void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples) |
| 2305 | { |
| 2306 | unsigned channel; |
| 2307 | |
| 2308 | for(channel = 0; channel < channels; channel++) |
| 2309 | memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples); |
| 2310 | |
| 2311 | fifo->tail += wide_samples; |
| 2312 | |
| 2313 | FLAC__ASSERT(fifo->tail <= fifo->size); |
| 2314 | } |
| 2315 | |
| 2316 | void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples) |
| 2317 | { |
| 2318 | unsigned channel; |
| 2319 | unsigned sample, wide_sample; |
| 2320 | unsigned tail = fifo->tail; |
| 2321 | |
| 2322 | sample = input_offset * channels; |
| 2323 | for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) { |
| 2324 | for(channel = 0; channel < channels; channel++) |
| 2325 | fifo->data[channel][tail] = input[sample++]; |
| 2326 | tail++; |
| 2327 | } |
| 2328 | fifo->tail = tail; |
| 2329 | |
| 2330 | FLAC__ASSERT(fifo->tail <= fifo->size); |
| 2331 | } |
| 2332 | |
| 2333 | FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) |
| 2334 | { |
| 2335 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 2336 | const unsigned encoded_bytes = encoder->private_->verify.output.bytes; |
| 2337 | (void)decoder; |
| 2338 | |
| 2339 | if(encoder->private_->verify.needs_magic_hack) { |
| 2340 | FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH); |
| 2341 | *bytes = FLAC__STREAM_SYNC_LENGTH; |
| 2342 | memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes); |
| 2343 | encoder->private_->verify.needs_magic_hack = false; |
| 2344 | } |
| 2345 | else { |
| 2346 | if(encoded_bytes == 0) { |
| 2347 | //@@@@ underflow happened, should we do something else here? is this an assert failure? |
| 2348 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 2349 | } |
| 2350 | else if(encoded_bytes < *bytes) |
| 2351 | *bytes = encoded_bytes; |
| 2352 | memcpy(buffer, encoder->private_->verify.output.data, *bytes); |
| 2353 | encoder->private_->verify.output.data += *bytes; |
| 2354 | encoder->private_->verify.output.bytes -= *bytes; |
| 2355 | } |
| 2356 | |
| 2357 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 2358 | } |
| 2359 | |
| 2360 | FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 2361 | { |
| 2362 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data; |
| 2363 | unsigned channel; |
| 2364 | const unsigned channels = FLAC__stream_decoder_get_channels(decoder); |
| 2365 | const unsigned blocksize = frame->header.blocksize; |
| 2366 | const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize; |
| 2367 | |
| 2368 | for(channel = 0; channel < channels; channel++) { |
| 2369 | if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) { |
| 2370 | unsigned i, sample = 0; |
| 2371 | FLAC__int32 expect = 0, got = 0; |
| 2372 | |
| 2373 | for(i = 0; i < blocksize; i++) { |
| 2374 | if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) { |
| 2375 | sample = i; |
| 2376 | expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i]; |
| 2377 | got = (FLAC__int32)buffer[channel][i]; |
| 2378 | break; |
| 2379 | } |
| 2380 | } |
| 2381 | FLAC__ASSERT(i < blocksize); |
| 2382 | FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 2383 | encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample; |
| 2384 | encoder->private_->verify.error_stats.frame_number = frame->header.number.sample_number / blocksize; |
| 2385 | encoder->private_->verify.error_stats.channel = channel; |
| 2386 | encoder->private_->verify.error_stats.sample = sample; |
| 2387 | encoder->private_->verify.error_stats.expected = expect; |
| 2388 | encoder->private_->verify.error_stats.got = got; |
| 2389 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA; |
| 2390 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 2391 | } |
| 2392 | } |
| 2393 | /* dequeue the frame from the fifo */ |
| 2394 | for(channel = 0; channel < channels; channel++) { |
| 2395 | memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize); |
| 2396 | } |
| 2397 | encoder->private_->verify.input_fifo.tail -= blocksize; |
| 2398 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 2399 | } |
| 2400 | |
| 2401 | void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 2402 | { |
| 2403 | (void)decoder, (void)metadata, (void)client_data; |
| 2404 | } |
| 2405 | |
| 2406 | void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 2407 | { |
| 2408 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 2409 | (void)decoder, (void)status; |
| 2410 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 2411 | } |