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 | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame^] | 590 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 591 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 592 | return; |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame^] | 593 | |
| 594 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) { |
| 595 | if(encoder->private_->current_sample_number != 0) { |
| 596 | encoder->protected_->blocksize = encoder->private_->current_sample_number; |
| 597 | process_frame_(encoder, true); /* true => is last frame */ |
| 598 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 599 | } |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame^] | 600 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 601 | MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context); |
Josh Coalson | 2b245f2 | 2002-08-07 17:10:50 +0000 | [diff] [blame^] | 602 | |
| 603 | if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) { |
| 604 | encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data); |
| 605 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 606 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 607 | if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder) |
| 608 | FLAC__stream_decoder_finish(encoder->private_->verify.decoder); |
| 609 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 610 | free_(encoder); |
| 611 | set_defaults_(encoder); |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 612 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 613 | encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 616 | FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value) |
| 617 | { |
| 618 | FLAC__ASSERT(0 != encoder); |
| 619 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 620 | return false; |
| 621 | encoder->protected_->verify = value; |
| 622 | return true; |
| 623 | } |
| 624 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 625 | 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] | 626 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 627 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 628 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 629 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 630 | encoder->protected_->streamable_subset = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 631 | return true; |
| 632 | } |
| 633 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 634 | 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] | 635 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 636 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 637 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 638 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 639 | encoder->protected_->do_mid_side_stereo = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 640 | return true; |
| 641 | } |
| 642 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 643 | 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] | 644 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 645 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 646 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 647 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 648 | encoder->protected_->loose_mid_side_stereo = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 649 | return true; |
| 650 | } |
| 651 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 652 | FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 653 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 654 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 655 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 656 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 657 | encoder->protected_->channels = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 658 | return true; |
| 659 | } |
| 660 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 661 | 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] | 662 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 663 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 664 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 665 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 666 | encoder->protected_->bits_per_sample = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 667 | return true; |
| 668 | } |
| 669 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 670 | 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] | 671 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 672 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 673 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 674 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 675 | encoder->protected_->sample_rate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 676 | return true; |
| 677 | } |
| 678 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 679 | FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 680 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 681 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 682 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 683 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 684 | encoder->protected_->blocksize = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 685 | return true; |
| 686 | } |
| 687 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 688 | 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] | 689 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 690 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 691 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 692 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 693 | encoder->protected_->max_lpc_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 694 | return true; |
| 695 | } |
| 696 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 697 | 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] | 698 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 699 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 700 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 701 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 702 | encoder->protected_->qlp_coeff_precision = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 703 | return true; |
| 704 | } |
| 705 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 706 | 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] | 707 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 708 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 709 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 710 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 711 | encoder->protected_->do_qlp_coeff_prec_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 712 | return true; |
| 713 | } |
| 714 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 715 | 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] | 716 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 717 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 718 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 719 | return false; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 720 | #if 0 |
| 721 | /*@@@ deprecated: */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 722 | encoder->protected_->do_escape_coding = value; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 723 | #else |
| 724 | (void)value; |
| 725 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 726 | return true; |
| 727 | } |
| 728 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 729 | 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] | 730 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 731 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 732 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 733 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 734 | encoder->protected_->do_exhaustive_model_search = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 735 | return true; |
| 736 | } |
| 737 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 738 | 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] | 739 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 740 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 741 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 742 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 743 | encoder->protected_->min_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 744 | return true; |
| 745 | } |
| 746 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 747 | 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] | 748 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 749 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 750 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 751 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 752 | encoder->protected_->max_residual_partition_order = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 753 | return true; |
| 754 | } |
| 755 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 756 | 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] | 757 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 758 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 759 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 760 | return false; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 761 | #if 0 |
| 762 | /*@@@ deprecated: */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 763 | encoder->protected_->rice_parameter_search_dist = value; |
Josh Coalson | 680e3aa | 2002-08-01 07:32:17 +0000 | [diff] [blame] | 764 | #else |
| 765 | (void)value; |
| 766 | #endif |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 767 | return true; |
| 768 | } |
| 769 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 770 | 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] | 771 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 772 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 773 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 774 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 775 | encoder->protected_->total_samples_estimate = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 776 | return true; |
| 777 | } |
| 778 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 779 | 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] | 780 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 781 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 782 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 783 | return false; |
Josh Coalson | 66075c1 | 2002-06-01 05:39:38 +0000 | [diff] [blame] | 784 | encoder->protected_->metadata = metadata; |
| 785 | encoder->protected_->num_metadata_blocks = num_blocks; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 786 | return true; |
| 787 | } |
| 788 | |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 789 | 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] | 790 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 791 | FLAC__ASSERT(0 != encoder); |
| 792 | FLAC__ASSERT(0 != value); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 793 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 794 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 795 | encoder->private_->write_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 796 | return true; |
| 797 | } |
| 798 | |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 799 | 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] | 800 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 801 | FLAC__ASSERT(0 != encoder); |
| 802 | FLAC__ASSERT(0 != value); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 803 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 804 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 805 | encoder->private_->metadata_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 806 | return true; |
| 807 | } |
| 808 | |
Josh Coalson | 1655604 | 2002-05-29 05:51:24 +0000 | [diff] [blame] | 809 | 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] | 810 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 811 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 812 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 813 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 814 | encoder->private_->client_data = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 815 | return true; |
| 816 | } |
| 817 | |
| 818 | FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 819 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 820 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 821 | return encoder->protected_->state; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 824 | FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder) |
| 825 | { |
| 826 | FLAC__ASSERT(0 != encoder); |
| 827 | if(encoder->protected_->verify) |
| 828 | return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder); |
| 829 | else |
| 830 | return FLAC__STREAM_DECODER_UNINITIALIZED; |
| 831 | } |
| 832 | |
| 833 | FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder) |
| 834 | { |
| 835 | FLAC__ASSERT(0 != encoder); |
| 836 | return encoder->protected_->verify; |
| 837 | } |
| 838 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 839 | FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 840 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 841 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 842 | return encoder->protected_->streamable_subset; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 845 | 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] | 846 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 847 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 848 | return encoder->protected_->do_mid_side_stereo; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 851 | 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] | 852 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 853 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 854 | return encoder->protected_->loose_mid_side_stereo; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 857 | unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 858 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 859 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 860 | return encoder->protected_->channels; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 863 | unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 864 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 865 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 866 | return encoder->protected_->bits_per_sample; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 869 | unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 870 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 871 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 872 | return encoder->protected_->sample_rate; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 875 | unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 876 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 877 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 878 | return encoder->protected_->blocksize; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 881 | unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 882 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 883 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 884 | return encoder->protected_->max_lpc_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 887 | unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 888 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 889 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 890 | return encoder->protected_->qlp_coeff_precision; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 893 | 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] | 894 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 895 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 896 | return encoder->protected_->do_qlp_coeff_prec_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 899 | FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder) |
| 900 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 901 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 902 | return encoder->protected_->do_escape_coding; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 905 | 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] | 906 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 907 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 908 | return encoder->protected_->do_exhaustive_model_search; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 911 | 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] | 912 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 913 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 914 | return encoder->protected_->min_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 917 | 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] | 918 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 919 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 920 | return encoder->protected_->max_residual_partition_order; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 923 | 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] | 924 | { |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 925 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 926 | return encoder->protected_->rice_parameter_search_dist; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Josh Coalson | 3a7b2c9 | 2002-08-02 07:38:20 +0000 | [diff] [blame] | 929 | FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder) |
| 930 | { |
| 931 | FLAC__ASSERT(0 != encoder); |
| 932 | return encoder->protected_->total_samples_estimate; |
| 933 | } |
| 934 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 935 | 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] | 936 | { |
| 937 | unsigned i, j, channel; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 938 | FLAC__int32 x, mid, side; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 939 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 940 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 941 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 942 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 943 | |
| 944 | j = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 945 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 946 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 947 | if(encoder->protected_->verify) |
| 948 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 949 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 950 | 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] | 951 | x = mid = side = buffer[0][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 952 | encoder->private_->integer_signal[0][i] = x; |
| 953 | encoder->private_->real_signal[0][i] = (FLAC__real)x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 954 | x = buffer[1][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 955 | encoder->private_->integer_signal[1][i] = x; |
| 956 | encoder->private_->real_signal[1][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 957 | mid += x; |
| 958 | side -= x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 959 | 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] | 960 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 961 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
| 962 | encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side; |
| 963 | encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid; |
| 964 | encoder->private_->current_sample_number++; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 965 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 966 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 967 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 968 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 969 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 970 | } while(j < samples); |
| 971 | } |
| 972 | else { |
| 973 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 974 | if(encoder->protected_->verify) |
| 975 | append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 976 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 977 | 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] | 978 | for(channel = 0; channel < channels; channel++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 979 | x = buffer[channel][j]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 980 | encoder->private_->integer_signal[channel][i] = x; |
| 981 | encoder->private_->real_signal[channel][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 982 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 983 | encoder->private_->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 984 | } |
| 985 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 986 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 987 | return false; |
| 988 | } |
| 989 | } while(j < samples); |
| 990 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 991 | |
| 992 | return true; |
| 993 | } |
| 994 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 995 | 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] | 996 | { |
| 997 | unsigned i, j, k, channel; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 998 | FLAC__int32 x, mid, side; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 999 | const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1000 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1001 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1002 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1003 | |
| 1004 | j = k = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1005 | if(encoder->protected_->do_mid_side_stereo && channels == 2) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1006 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1007 | if(encoder->protected_->verify) |
| 1008 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1009 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1010 | 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] | 1011 | x = mid = side = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1012 | encoder->private_->integer_signal[0][i] = x; |
| 1013 | encoder->private_->real_signal[0][i] = (FLAC__real)x; |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1014 | x = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1015 | encoder->private_->integer_signal[1][i] = x; |
| 1016 | encoder->private_->real_signal[1][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1017 | mid += x; |
| 1018 | side -= x; |
| 1019 | mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1020 | encoder->private_->integer_signal_mid_side[1][i] = side; |
| 1021 | encoder->private_->integer_signal_mid_side[0][i] = mid; |
| 1022 | encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side; |
| 1023 | encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid; |
| 1024 | encoder->private_->current_sample_number++; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1025 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1026 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1027 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1028 | return false; |
| 1029 | } |
| 1030 | } while(j < samples); |
| 1031 | } |
| 1032 | else { |
| 1033 | do { |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1034 | if(encoder->protected_->verify) |
| 1035 | append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j)); |
| 1036 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1037 | 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] | 1038 | for(channel = 0; channel < channels; channel++) { |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1039 | x = buffer[k++]; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1040 | encoder->private_->integer_signal[channel][i] = x; |
| 1041 | encoder->private_->real_signal[channel][i] = (FLAC__real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1042 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1043 | encoder->private_->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1044 | } |
| 1045 | if(i == blocksize) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1046 | if(!process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 1047 | return false; |
| 1048 | } |
| 1049 | } while(j < samples); |
| 1050 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1051 | |
| 1052 | return true; |
| 1053 | } |
| 1054 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1055 | /*********************************************************************** |
| 1056 | * |
| 1057 | * Private class methods |
| 1058 | * |
| 1059 | ***********************************************************************/ |
| 1060 | |
| 1061 | void set_defaults_(FLAC__StreamEncoder *encoder) |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1062 | { |
| 1063 | FLAC__ASSERT(0 != encoder); |
| 1064 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1065 | encoder->protected_->verify = false; |
Josh Coalson | 9203160 | 2002-07-24 06:02:11 +0000 | [diff] [blame] | 1066 | encoder->protected_->streamable_subset = true; |
| 1067 | encoder->protected_->do_mid_side_stereo = false; |
| 1068 | encoder->protected_->loose_mid_side_stereo = false; |
| 1069 | encoder->protected_->channels = 2; |
| 1070 | encoder->protected_->bits_per_sample = 16; |
| 1071 | encoder->protected_->sample_rate = 44100; |
| 1072 | encoder->protected_->blocksize = 1152; |
| 1073 | encoder->protected_->max_lpc_order = 0; |
| 1074 | encoder->protected_->qlp_coeff_precision = 0; |
| 1075 | encoder->protected_->do_qlp_coeff_prec_search = false; |
| 1076 | encoder->protected_->do_exhaustive_model_search = false; |
| 1077 | encoder->protected_->do_escape_coding = false; |
| 1078 | encoder->protected_->min_residual_partition_order = 0; |
| 1079 | encoder->protected_->max_residual_partition_order = 0; |
| 1080 | encoder->protected_->rice_parameter_search_dist = 0; |
| 1081 | encoder->protected_->total_samples_estimate = 0; |
| 1082 | encoder->protected_->metadata = 0; |
| 1083 | encoder->protected_->num_metadata_blocks = 0; |
| 1084 | |
| 1085 | encoder->private_->write_callback = 0; |
| 1086 | encoder->private_->metadata_callback = 0; |
| 1087 | encoder->private_->client_data = 0; |
| 1088 | } |
| 1089 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1090 | void free_(FLAC__StreamEncoder *encoder) |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1091 | { |
| 1092 | unsigned i, channel; |
| 1093 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1094 | FLAC__ASSERT(0 != encoder); |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1095 | for(i = 0; i < encoder->protected_->channels; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1096 | if(0 != encoder->private_->integer_signal_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1097 | free(encoder->private_->integer_signal_unaligned[i]); |
| 1098 | encoder->private_->integer_signal_unaligned[i] = 0; |
| 1099 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1100 | if(0 != encoder->private_->real_signal_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1101 | free(encoder->private_->real_signal_unaligned[i]); |
| 1102 | encoder->private_->real_signal_unaligned[i] = 0; |
| 1103 | } |
| 1104 | } |
| 1105 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1106 | if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1107 | free(encoder->private_->integer_signal_mid_side_unaligned[i]); |
| 1108 | encoder->private_->integer_signal_mid_side_unaligned[i] = 0; |
| 1109 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1110 | if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1111 | free(encoder->private_->real_signal_mid_side_unaligned[i]); |
| 1112 | encoder->private_->real_signal_mid_side_unaligned[i] = 0; |
| 1113 | } |
| 1114 | } |
| 1115 | for(channel = 0; channel < encoder->protected_->channels; 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_unaligned[channel][i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1118 | free(encoder->private_->residual_workspace_unaligned[channel][i]); |
| 1119 | encoder->private_->residual_workspace_unaligned[channel][i] = 0; |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | for(channel = 0; channel < 2; channel++) { |
| 1124 | for(i = 0; i < 2; i++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1125 | if(0 != encoder->private_->residual_workspace_mid_side_unaligned[channel][i]) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1126 | free(encoder->private_->residual_workspace_mid_side_unaligned[channel][i]); |
| 1127 | encoder->private_->residual_workspace_mid_side_unaligned[channel][i] = 0; |
| 1128 | } |
| 1129 | } |
| 1130 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1131 | if(0 != encoder->private_->abs_residual_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1132 | free(encoder->private_->abs_residual_unaligned); |
| 1133 | encoder->private_->abs_residual_unaligned = 0; |
| 1134 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1135 | if(0 != encoder->private_->abs_residual_partition_sums_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1136 | free(encoder->private_->abs_residual_partition_sums_unaligned); |
| 1137 | encoder->private_->abs_residual_partition_sums_unaligned = 0; |
| 1138 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1139 | if(0 != encoder->private_->raw_bits_per_partition_unaligned) { |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1140 | free(encoder->private_->raw_bits_per_partition_unaligned); |
| 1141 | encoder->private_->raw_bits_per_partition_unaligned = 0; |
| 1142 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1143 | if(encoder->protected_->verify) { |
| 1144 | for(i = 0; i < encoder->protected_->channels; i++) { |
| 1145 | if(0 != encoder->private_->verify.input_fifo.data[i]) { |
| 1146 | free(encoder->private_->verify.input_fifo.data[i]); |
| 1147 | encoder->private_->verify.input_fifo.data[i] = 0; |
| 1148 | } |
| 1149 | } |
| 1150 | } |
Josh Coalson | 639aeb0 | 2002-07-25 05:38:23 +0000 | [diff] [blame] | 1151 | FLAC__bitbuffer_free(encoder->private_->frame); |
| 1152 | } |
| 1153 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1154 | FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1155 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1156 | FLAC__bool ok; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1157 | unsigned i, channel; |
| 1158 | |
| 1159 | FLAC__ASSERT(new_size > 0); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1160 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
| 1161 | FLAC__ASSERT(encoder->private_->current_sample_number == 0); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1162 | |
| 1163 | /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1164 | if(new_size <= encoder->private_->input_capacity) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1165 | return true; |
| 1166 | |
| 1167 | ok = true; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1168 | |
| 1169 | /* 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. */ |
| 1170 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1171 | for(i = 0; ok && i < encoder->protected_->channels; i++) { |
| 1172 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]); |
| 1173 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]); |
| 1174 | memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4); |
| 1175 | encoder->private_->integer_signal[i] += 4; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1176 | } |
| 1177 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1178 | 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]); |
| 1179 | 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]); |
| 1180 | memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4); |
| 1181 | encoder->private_->integer_signal_mid_side[i] += 4; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1182 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1183 | for(channel = 0; ok && channel < encoder->protected_->channels; channel++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1184 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1185 | 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] | 1186 | } |
| 1187 | } |
| 1188 | for(channel = 0; ok && channel < 2; channel++) { |
| 1189 | for(i = 0; ok && i < 2; i++) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1190 | 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] | 1191 | } |
| 1192 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1193 | ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual); |
| 1194 | 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 */ |
| 1195 | ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums); |
| 1196 | if(encoder->protected_->do_escape_coding) |
| 1197 | 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] | 1198 | |
| 1199 | if(ok) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1200 | encoder->private_->input_capacity = new_size; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1201 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1202 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1203 | |
| 1204 | return ok; |
| 1205 | } |
| 1206 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1207 | FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples) |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1208 | { |
| 1209 | const FLAC__byte *buffer; |
| 1210 | unsigned bytes; |
| 1211 | |
| 1212 | FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame)); |
| 1213 | |
| 1214 | FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes); |
| 1215 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1216 | if(encoder->protected_->verify) { |
| 1217 | encoder->private_->verify.output.data = buffer; |
| 1218 | encoder->private_->verify.output.bytes = bytes; |
| 1219 | if(encoder->private_->verify.state_hint == ENCODER_IN_MAGIC) { |
| 1220 | encoder->private_->verify.needs_magic_hack = true; |
| 1221 | } |
| 1222 | else { |
| 1223 | if(!FLAC__stream_decoder_process_single(encoder->private_->verify.decoder)) { |
| 1224 | FLAC__bitbuffer_release_buffer(encoder->private_->frame); |
| 1225 | if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) |
| 1226 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 1227 | return false; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) { |
| 1233 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING; |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1234 | return false; |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1235 | } |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1236 | |
| 1237 | FLAC__bitbuffer_release_buffer(encoder->private_->frame); |
| 1238 | |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1239 | if(samples > 0) { |
| 1240 | encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize); |
| 1241 | encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize); |
| 1242 | } |
| 1243 | |
Josh Coalson | 5c491a1 | 2002-08-01 06:39:40 +0000 | [diff] [blame] | 1244 | return true; |
| 1245 | } |
| 1246 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1247 | FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1248 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1249 | FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1250 | |
| 1251 | /* |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 1252 | * Accumulate raw signal to the MD5 signature |
| 1253 | */ |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1254 | 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] | 1255 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 1256 | return false; |
| 1257 | } |
| 1258 | |
| 1259 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1260 | * Process the frame header and subframes into the frame bitbuffer |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1261 | */ |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1262 | if(!process_subframes_(encoder, is_last_frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1263 | /* 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] | 1264 | return false; |
| 1265 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1266 | |
| 1267 | /* |
| 1268 | * Zero-pad the frame to a byte_boundary |
| 1269 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1270 | if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1271 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1272 | return false; |
| 1273 | } |
| 1274 | |
| 1275 | /* |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1276 | * CRC-16 the whole thing |
| 1277 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1278 | FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame)); |
| 1279 | 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] | 1280 | |
| 1281 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1282 | * Write it |
| 1283 | */ |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 1284 | if(!write_bitbuffer_(encoder, encoder->protected_->blocksize)) { |
| 1285 | /* 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] | 1286 | return false; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * Get ready for the next frame |
| 1291 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1292 | encoder->private_->current_sample_number = 0; |
| 1293 | encoder->private_->current_frame_number++; |
| 1294 | 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] | 1295 | |
| 1296 | return true; |
| 1297 | } |
| 1298 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1299 | FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1300 | { |
| 1301 | FLAC__FrameHeader frame_header; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1302 | 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] | 1303 | FLAC__bool do_independent, do_mid_side, precompute_partition_sums; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1304 | |
| 1305 | /* |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1306 | * Calculate the min,max Rice partition orders |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1307 | */ |
| 1308 | if(is_last_frame) { |
| 1309 | max_partition_order = 0; |
| 1310 | } |
| 1311 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1312 | unsigned limit = 0, b = encoder->protected_->blocksize; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1313 | while(!(b & 1)) { |
| 1314 | limit++; |
| 1315 | b >>= 1; |
| 1316 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1317 | max_partition_order = min(encoder->protected_->max_residual_partition_order, limit); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1318 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1319 | min_partition_order = min(min_partition_order, max_partition_order); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1320 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1321 | 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] | 1322 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1323 | /* |
| 1324 | * Setup the frame |
| 1325 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1326 | if(!FLAC__bitbuffer_clear(encoder->private_->frame)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1327 | encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1328 | return false; |
| 1329 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1330 | frame_header.blocksize = encoder->protected_->blocksize; |
| 1331 | frame_header.sample_rate = encoder->protected_->sample_rate; |
| 1332 | frame_header.channels = encoder->protected_->channels; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1333 | 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] | 1334 | frame_header.bits_per_sample = encoder->protected_->bits_per_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1335 | frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1336 | frame_header.number.frame_number = encoder->private_->current_frame_number; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1337 | |
| 1338 | /* |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1339 | * Figure out what channel assignments to try |
| 1340 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1341 | if(encoder->protected_->do_mid_side_stereo) { |
| 1342 | if(encoder->protected_->loose_mid_side_stereo) { |
| 1343 | if(encoder->private_->loose_mid_side_stereo_frame_count == 0) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1344 | do_independent = true; |
| 1345 | do_mid_side = true; |
| 1346 | } |
| 1347 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1348 | do_independent = (encoder->private_->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1349 | do_mid_side = !do_independent; |
| 1350 | } |
| 1351 | } |
| 1352 | else { |
| 1353 | do_independent = true; |
| 1354 | do_mid_side = true; |
| 1355 | } |
| 1356 | } |
| 1357 | else { |
| 1358 | do_independent = true; |
| 1359 | do_mid_side = false; |
| 1360 | } |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1361 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1362 | FLAC__ASSERT(do_independent || do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1363 | |
| 1364 | /* |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1365 | * Check for wasted bits; set effective bps for each subframe |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1366 | */ |
| 1367 | if(do_independent) { |
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 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1370 | w = get_wasted_bits_(encoder->private_->integer_signal[channel], encoder->protected_->blocksize); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1371 | encoder->private_->subframe_workspace[channel][0].wasted_bits = encoder->private_->subframe_workspace[channel][1].wasted_bits = w; |
| 1372 | encoder->private_->subframe_bps[channel] = encoder->protected_->bits_per_sample - w; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1373 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1374 | } |
| 1375 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1376 | unsigned w; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1377 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1378 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1379 | 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] | 1380 | encoder->private_->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private_->subframe_workspace_mid_side[channel][1].wasted_bits = w; |
| 1381 | 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] | 1382 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1386 | * First do a normal encoding pass of each independent channel |
| 1387 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1388 | if(do_independent) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1389 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1390 | 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] | 1391 | return false; |
| 1392 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | * Now do mid and side channels if requested |
| 1397 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1398 | if(do_mid_side) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1399 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1400 | |
| 1401 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1402 | 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] | 1403 | return false; |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | * Compose the frame bitbuffer |
| 1409 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1410 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1411 | unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ |
| 1412 | 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] | 1413 | FLAC__ChannelAssignment channel_assignment; |
| 1414 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1415 | FLAC__ASSERT(encoder->protected_->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1416 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1417 | if(encoder->protected_->loose_mid_side_stereo && encoder->private_->loose_mid_side_stereo_frame_count > 0) { |
| 1418 | 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] | 1419 | } |
| 1420 | else { |
| 1421 | unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */ |
| 1422 | unsigned min_bits; |
| 1423 | FLAC__ChannelAssignment ca; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1424 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1425 | FLAC__ASSERT(do_independent && do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1426 | |
| 1427 | /* 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] | 1428 | bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits [1]; |
| 1429 | bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private_->best_subframe_bits [0] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 1430 | bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private_->best_subframe_bits [1] + encoder->private_->best_subframe_bits_mid_side[1]; |
| 1431 | 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] | 1432 | |
| 1433 | for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) { |
| 1434 | if(bits[ca] < min_bits) { |
| 1435 | min_bits = bits[ca]; |
| 1436 | channel_assignment = ca; |
| 1437 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1441 | frame_header.channel_assignment = channel_assignment; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1442 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1443 | 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] | 1444 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1445 | return false; |
| 1446 | } |
| 1447 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1448 | switch(channel_assignment) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1449 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1450 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [0]]; |
| 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_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1454 | left_subframe = &encoder->private_->subframe_workspace [0][encoder->private_->best_subframe [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 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1458 | left_subframe = &encoder->private_->subframe_workspace_mid_side[1][encoder->private_->best_subframe_mid_side[1]]; |
| 1459 | right_subframe = &encoder->private_->subframe_workspace [1][encoder->private_->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1460 | break; |
| 1461 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1462 | left_subframe = &encoder->private_->subframe_workspace_mid_side[0][encoder->private_->best_subframe_mid_side[0]]; |
| 1463 | 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] | 1464 | break; |
| 1465 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1466 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1467 | } |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1468 | |
| 1469 | switch(channel_assignment) { |
| 1470 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1471 | left_bps = encoder->private_->subframe_bps [0]; |
| 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_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1475 | left_bps = encoder->private_->subframe_bps [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 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1479 | left_bps = encoder->private_->subframe_bps_mid_side[1]; |
| 1480 | right_bps = encoder->private_->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1481 | break; |
| 1482 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1483 | left_bps = encoder->private_->subframe_bps_mid_side[0]; |
| 1484 | right_bps = encoder->private_->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1485 | break; |
| 1486 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1487 | FLAC__ASSERT(0); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | /* 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] | 1491 | 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] | 1492 | return false; |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1493 | 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] | 1494 | return false; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1495 | } |
| 1496 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1497 | 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] | 1498 | encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1499 | return false; |
| 1500 | } |
| 1501 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1502 | for(channel = 0; channel < encoder->protected_->channels; channel++) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1503 | 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] | 1504 | /* the above function sets the state for us in case of an error */ |
| 1505 | return false; |
| 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1510 | if(encoder->protected_->loose_mid_side_stereo) { |
| 1511 | encoder->private_->loose_mid_side_stereo_frame_count++; |
| 1512 | if(encoder->private_->loose_mid_side_stereo_frame_count >= encoder->private_->loose_mid_side_stereo_frames) |
| 1513 | encoder->private_->loose_mid_side_stereo_frame_count = 0; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1516 | encoder->private_->last_channel_assignment = frame_header.channel_assignment; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1517 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1518 | return true; |
| 1519 | } |
| 1520 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1521 | 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] | 1522 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1523 | FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 1524 | FLAC__real lpc_residual_bits_per_sample; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1525 | 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] | 1526 | FLAC__real lpc_error[FLAC__MAX_LPC_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1527 | unsigned min_lpc_order, max_lpc_order, lpc_order; |
| 1528 | unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order; |
| 1529 | unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision; |
| 1530 | unsigned rice_parameter; |
| 1531 | unsigned _candidate_bits, _best_bits; |
| 1532 | unsigned _best_subframe; |
| 1533 | |
| 1534 | /* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 1535 | _best_subframe = 0; |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1536 | _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] | 1537 | |
| 1538 | if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { |
| 1539 | /* check for constant subframe */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1540 | 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] | 1541 | if(fixed_residual_bits_per_sample[1] == 0.0) { |
| 1542 | /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */ |
| 1543 | unsigned i, signal_is_constant = true; |
| 1544 | for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) { |
| 1545 | if(integer_signal[0] != integer_signal[i]) { |
| 1546 | signal_is_constant = false; |
| 1547 | break; |
| 1548 | } |
| 1549 | } |
| 1550 | if(signal_is_constant) { |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1551 | _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] | 1552 | if(_candidate_bits < _best_bits) { |
| 1553 | _best_subframe = !_best_subframe; |
| 1554 | _best_bits = _candidate_bits; |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | else { |
| 1559 | /* encode fixed */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1560 | if(encoder->protected_->do_exhaustive_model_search) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1561 | min_fixed_order = 0; |
| 1562 | max_fixed_order = FLAC__MAX_FIXED_ORDER; |
| 1563 | } |
| 1564 | else { |
| 1565 | min_fixed_order = max_fixed_order = guess_fixed_order; |
| 1566 | } |
| 1567 | 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] | 1568 | if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1569 | continue; /* don't even try */ |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 1570 | 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] | 1571 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 1572 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1573 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1574 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1575 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1576 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1577 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1578 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1579 | } |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1580 | _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] | 1581 | if(_candidate_bits < _best_bits) { |
| 1582 | _best_subframe = !_best_subframe; |
| 1583 | _best_bits = _candidate_bits; |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | /* encode lpc */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1588 | if(encoder->protected_->max_lpc_order > 0) { |
| 1589 | if(encoder->protected_->max_lpc_order >= frame_header->blocksize) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1590 | max_lpc_order = frame_header->blocksize-1; |
| 1591 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1592 | max_lpc_order = encoder->protected_->max_lpc_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1593 | if(max_lpc_order > 0) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1594 | 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] | 1595 | /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */ |
| 1596 | if(autoc[0] != 0.0) { |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1597 | 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] | 1598 | if(encoder->protected_->do_exhaustive_model_search) { |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1599 | min_lpc_order = 1; |
| 1600 | } |
| 1601 | else { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1602 | 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] | 1603 | min_lpc_order = max_lpc_order = guess_lpc_order; |
| 1604 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1605 | if(encoder->protected_->do_qlp_coeff_prec_search) { |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1606 | min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1607 | 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] | 1608 | } |
| 1609 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1610 | 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] | 1611 | } |
| 1612 | for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) { |
| 1613 | 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] | 1614 | if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps) |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1615 | continue; /* don't even try */ |
| 1616 | 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] | 1617 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1618 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1619 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1620 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1621 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1622 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1623 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1624 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1625 | } |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1626 | 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] | 1627 | _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] | 1628 | if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 1629 | if(_candidate_bits < _best_bits) { |
| 1630 | _best_subframe = !_best_subframe; |
| 1631 | _best_bits = _candidate_bits; |
| 1632 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | *best_subframe = _best_subframe; |
| 1643 | *best_bits = _best_bits; |
| 1644 | |
| 1645 | return true; |
| 1646 | } |
| 1647 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1648 | 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] | 1649 | { |
| 1650 | switch(subframe->type) { |
| 1651 | case FLAC__SUBFRAME_TYPE_CONSTANT: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1652 | 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] | 1653 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1654 | return false; |
| 1655 | } |
| 1656 | break; |
| 1657 | case FLAC__SUBFRAME_TYPE_FIXED: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1658 | 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] | 1659 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1660 | return false; |
| 1661 | } |
| 1662 | break; |
| 1663 | case FLAC__SUBFRAME_TYPE_LPC: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1664 | 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] | 1665 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1666 | return false; |
| 1667 | } |
| 1668 | break; |
| 1669 | case FLAC__SUBFRAME_TYPE_VERBATIM: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1670 | 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] | 1671 | encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1672 | return false; |
| 1673 | } |
| 1674 | break; |
| 1675 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1676 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | return true; |
| 1680 | } |
| 1681 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1682 | 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] | 1683 | { |
| 1684 | subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 1685 | subframe->data.constant.value = signal; |
| 1686 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1687 | 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] | 1688 | } |
| 1689 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1690 | 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] | 1691 | { |
| 1692 | unsigned i, residual_bits; |
| 1693 | const unsigned residual_samples = blocksize - order; |
| 1694 | |
| 1695 | FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual); |
| 1696 | |
| 1697 | subframe->type = FLAC__SUBFRAME_TYPE_FIXED; |
| 1698 | |
| 1699 | subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1700 | subframe->data.fixed.residual = residual; |
| 1701 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1702 | 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] | 1703 | |
| 1704 | subframe->data.fixed.order = order; |
| 1705 | for(i = 0; i < order; i++) |
| 1706 | subframe->data.fixed.warmup[i] = signal[i]; |
| 1707 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1708 | 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] | 1709 | } |
| 1710 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1711 | 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] | 1712 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1713 | FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1714 | unsigned i, residual_bits; |
| 1715 | int quantization, ret; |
| 1716 | const unsigned residual_samples = blocksize - order; |
| 1717 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1718 | 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] | 1719 | if(ret != 0) |
| 1720 | return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ |
| 1721 | |
Josh Coalson | 92d4240 | 2001-05-31 20:53:19 +0000 | [diff] [blame] | 1722 | if(subframe_bps <= 16 && qlp_coeff_precision <= 16) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1723 | 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] | 1724 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1725 | 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] | 1726 | |
| 1727 | subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 1728 | |
| 1729 | subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1730 | subframe->data.lpc.residual = residual; |
| 1731 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1732 | 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] | 1733 | |
| 1734 | subframe->data.lpc.order = order; |
| 1735 | subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 1736 | subframe->data.lpc.quantization_level = quantization; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1737 | 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] | 1738 | for(i = 0; i < order; i++) |
| 1739 | subframe->data.lpc.warmup[i] = signal[i]; |
| 1740 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1741 | 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] | 1742 | } |
| 1743 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1744 | 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] | 1745 | { |
| 1746 | subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 1747 | |
| 1748 | subframe->data.verbatim.data = signal; |
| 1749 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1750 | 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] | 1751 | } |
| 1752 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1753 | 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] | 1754 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1755 | FLAC__int32 r; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1756 | unsigned residual_bits, best_residual_bits = 0; |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1757 | unsigned residual_sample; |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1758 | unsigned best_parameters_index = 0; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1759 | const unsigned blocksize = residual_samples + predictor_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1760 | |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1761 | /* compute abs(residual) for use later */ |
| 1762 | for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) { |
| 1763 | r = residual[residual_sample]; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1764 | abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r); |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1767 | while(max_partition_order > 0 && blocksize >> max_partition_order <= predictor_order) |
| 1768 | max_partition_order--; |
| 1769 | FLAC__ASSERT(blocksize >> max_partition_order > predictor_order); |
| 1770 | min_partition_order = min(min_partition_order, max_partition_order); |
| 1771 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1772 | if(precompute_partition_sums) { |
| 1773 | int partition_order; |
| 1774 | unsigned sum; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1775 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1776 | 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] | 1777 | |
| 1778 | if(do_escape_coding) |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1779 | 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] | 1780 | |
| 1781 | for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) { |
| 1782 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1783 | 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] | 1784 | #else |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1785 | 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] | 1786 | #endif |
| 1787 | { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1788 | FLAC__ASSERT(best_residual_bits != 0); |
| 1789 | break; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1790 | } |
| 1791 | sum += 1u << partition_order; |
| 1792 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 1793 | best_residual_bits = residual_bits; |
| 1794 | *best_partition_order = partition_order; |
| 1795 | best_parameters_index = !best_parameters_index; |
| 1796 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1797 | } |
| 1798 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1799 | else { |
| 1800 | unsigned partition_order; |
| 1801 | for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) { |
| 1802 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1803 | 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] | 1804 | #else |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1805 | 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] | 1806 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1807 | { |
| 1808 | FLAC__ASSERT(best_residual_bits != 0); |
| 1809 | break; |
| 1810 | } |
| 1811 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 1812 | best_residual_bits = residual_bits; |
| 1813 | *best_partition_order = partition_order; |
| 1814 | best_parameters_index = !best_parameters_index; |
| 1815 | } |
| 1816 | } |
| 1817 | } |
| 1818 | |
Josh Coalson | 8084b05 | 2001-11-01 00:27:29 +0000 | [diff] [blame] | 1819 | memcpy(best_parameters, private_->parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order))); |
| 1820 | 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] | 1821 | |
| 1822 | return best_residual_bits; |
| 1823 | } |
| 1824 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1825 | 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] | 1826 | { |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1827 | int partition_order; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1828 | unsigned from_partition, to_partition = 0; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1829 | const unsigned blocksize = residual_samples + predictor_order; |
| 1830 | |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1831 | /* first do max_partition_order */ |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1832 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1833 | FLAC__uint64 abs_residual_partition_sum; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1834 | FLAC__uint32 abs_r; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1835 | unsigned partition, partition_sample, partition_samples, residual_sample; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1836 | const unsigned partitions = 1u << partition_order; |
| 1837 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 1838 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1839 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 1840 | |
| 1841 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 1842 | partition_samples = default_partition_samples; |
| 1843 | if(partition == 0) |
| 1844 | partition_samples -= predictor_order; |
| 1845 | abs_residual_partition_sum = 0; |
| 1846 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 1847 | abs_r = abs_residual[residual_sample]; |
| 1848 | abs_residual_partition_sum += abs_r; |
| 1849 | residual_sample++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1850 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1851 | abs_residual_partition_sums[partition] = abs_residual_partition_sum; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1852 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1853 | to_partition = partitions; |
| 1854 | break; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1855 | } |
Josh Coalson | f76a361 | 2001-04-18 02:28:11 +0000 | [diff] [blame] | 1856 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1857 | /* now merge partitions for lower orders */ |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame] | 1858 | 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] | 1859 | FLAC__uint64 s; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1860 | unsigned i; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1861 | const unsigned partitions = 1u << partition_order; |
| 1862 | for(i = 0; i < partitions; i++) { |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1863 | s = abs_residual_partition_sums[from_partition]; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1864 | from_partition++; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1865 | 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] | 1866 | from_partition++; |
| 1867 | to_partition++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1868 | } |
| 1869 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1870 | } |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1871 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1872 | 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] | 1873 | { |
| 1874 | int partition_order; |
| 1875 | unsigned from_partition, to_partition = 0; |
| 1876 | const unsigned blocksize = residual_samples + predictor_order; |
| 1877 | |
| 1878 | /* first do max_partition_order */ |
| 1879 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
| 1880 | FLAC__int32 r, residual_partition_min, residual_partition_max; |
| 1881 | unsigned silog2_min, silog2_max; |
| 1882 | unsigned partition, partition_sample, partition_samples, residual_sample; |
| 1883 | const unsigned partitions = 1u << partition_order; |
| 1884 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 1885 | |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1886 | FLAC__ASSERT(default_partition_samples > predictor_order); |
| 1887 | |
| 1888 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 1889 | partition_samples = default_partition_samples; |
| 1890 | if(partition == 0) |
| 1891 | partition_samples -= predictor_order; |
| 1892 | residual_partition_min = residual_partition_max = 0; |
| 1893 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 1894 | r = residual[residual_sample]; |
| 1895 | if(r < residual_partition_min) |
| 1896 | residual_partition_min = r; |
| 1897 | else if(r > residual_partition_max) |
| 1898 | residual_partition_max = r; |
| 1899 | residual_sample++; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1900 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1901 | silog2_min = FLAC__bitmath_silog2(residual_partition_min); |
| 1902 | silog2_max = FLAC__bitmath_silog2(residual_partition_max); |
| 1903 | raw_bits_per_partition[partition] = max(silog2_min, silog2_max); |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1904 | } |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 1905 | to_partition = partitions; |
| 1906 | break; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | /* now merge partitions for lower orders */ |
| 1910 | for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
| 1911 | unsigned m; |
| 1912 | unsigned i; |
| 1913 | const unsigned partitions = 1u << partition_order; |
| 1914 | for(i = 0; i < partitions; i++) { |
| 1915 | m = raw_bits_per_partition[from_partition]; |
| 1916 | from_partition++; |
| 1917 | raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]); |
| 1918 | from_partition++; |
| 1919 | to_partition++; |
| 1920 | } |
| 1921 | } |
| 1922 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1923 | |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1924 | #ifdef VARIABLE_RICE_BITS |
| 1925 | #undef VARIABLE_RICE_BITS |
| 1926 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1927 | #ifndef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1928 | #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter)) |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1929 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1930 | |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1931 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1932 | 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] | 1933 | #else |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 1934 | 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] | 1935 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1936 | { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1937 | unsigned rice_parameter, partition_bits; |
| 1938 | #ifndef NO_RICE_SEARCH |
| 1939 | unsigned best_partition_bits; |
| 1940 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 1941 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1942 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
| 1943 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1944 | 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] | 1945 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1946 | if(partition_order == 0) { |
| 1947 | unsigned i; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1948 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1949 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1950 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1951 | if(suggested_rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1952 | min_rice_parameter = 0; |
| 1953 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1954 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 1955 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1956 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 1957 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1958 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 1959 | #endif |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1960 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | else |
| 1964 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
| 1965 | |
| 1966 | best_partition_bits = 0xffffffff; |
| 1967 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 1968 | #endif |
| 1969 | #ifdef VARIABLE_RICE_BITS |
| 1970 | #ifdef FLAC__SYMMETRIC_RICE |
| 1971 | partition_bits = (2+rice_parameter) * residual_samples; |
| 1972 | #else |
| 1973 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 1974 | partition_bits = (1+rice_parameter) * residual_samples; |
| 1975 | #endif |
| 1976 | #else |
| 1977 | partition_bits = 0; |
| 1978 | #endif |
| 1979 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 1980 | for(i = 0; i < residual_samples; i++) { |
| 1981 | #ifdef VARIABLE_RICE_BITS |
| 1982 | #ifdef FLAC__SYMMETRIC_RICE |
| 1983 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter); |
| 1984 | #else |
| 1985 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
| 1986 | #endif |
| 1987 | #else |
| 1988 | partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */ |
| 1989 | #endif |
| 1990 | } |
| 1991 | #ifndef NO_RICE_SEARCH |
| 1992 | if(partition_bits < best_partition_bits) { |
| 1993 | best_rice_parameter = rice_parameter; |
| 1994 | best_partition_bits = partition_bits; |
| 1995 | } |
| 1996 | } |
| 1997 | #endif |
| 1998 | parameters[0] = best_rice_parameter; |
| 1999 | bits_ += best_partition_bits; |
| 2000 | } |
| 2001 | else { |
| 2002 | unsigned partition, residual_sample, save_residual_sample, partition_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2003 | unsigned partition_samples; |
| 2004 | FLAC__uint64 mean, k; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2005 | const unsigned partitions = 1u << partition_order; |
| 2006 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 2007 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
| 2008 | if(partition == 0) { |
| 2009 | if(partition_samples <= predictor_order) |
| 2010 | return false; |
| 2011 | else |
| 2012 | partition_samples -= predictor_order; |
| 2013 | } |
| 2014 | mean = 0; |
| 2015 | save_residual_sample = residual_sample; |
| 2016 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2017 | mean += abs_residual[residual_sample]; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2018 | residual_sample = save_residual_sample; |
| 2019 | #ifdef FLAC__SYMMETRIC_RICE |
| 2020 | mean += partition_samples >> 1; /* for rounding effect */ |
| 2021 | mean /= partition_samples; |
| 2022 | |
| 2023 | /* calc rice_parameter = floor(log2(mean)) */ |
| 2024 | rice_parameter = 0; |
| 2025 | mean>>=1; |
| 2026 | while(mean) { |
| 2027 | rice_parameter++; |
| 2028 | mean >>= 1; |
| 2029 | } |
| 2030 | #else |
| 2031 | /* calc rice_parameter ala LOCO-I */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2032 | 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] | 2033 | ; |
| 2034 | #endif |
| 2035 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2036 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2037 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2038 | #endif |
| 2039 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 2040 | } |
| 2041 | |
| 2042 | #ifndef NO_RICE_SEARCH |
| 2043 | if(rice_parameter_search_dist) { |
| 2044 | if(rice_parameter < rice_parameter_search_dist) |
| 2045 | min_rice_parameter = 0; |
| 2046 | else |
| 2047 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 2048 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
| 2049 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2050 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2051 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2052 | #endif |
| 2053 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 2054 | } |
| 2055 | } |
| 2056 | else |
| 2057 | min_rice_parameter = max_rice_parameter = rice_parameter; |
| 2058 | |
| 2059 | best_partition_bits = 0xffffffff; |
| 2060 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 2061 | #endif |
| 2062 | #ifdef VARIABLE_RICE_BITS |
| 2063 | #ifdef FLAC__SYMMETRIC_RICE |
| 2064 | partition_bits = (2+rice_parameter) * partition_samples; |
| 2065 | #else |
| 2066 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 2067 | partition_bits = (1+rice_parameter) * partition_samples; |
| 2068 | #endif |
| 2069 | #else |
| 2070 | partition_bits = 0; |
| 2071 | #endif |
| 2072 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
| 2073 | save_residual_sample = residual_sample; |
| 2074 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) { |
| 2075 | #ifdef VARIABLE_RICE_BITS |
| 2076 | #ifdef FLAC__SYMMETRIC_RICE |
| 2077 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter); |
| 2078 | #else |
| 2079 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate); |
| 2080 | #endif |
| 2081 | #else |
| 2082 | partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */ |
| 2083 | #endif |
| 2084 | } |
| 2085 | #ifndef NO_RICE_SEARCH |
| 2086 | if(rice_parameter != max_rice_parameter) |
| 2087 | residual_sample = save_residual_sample; |
| 2088 | if(partition_bits < best_partition_bits) { |
| 2089 | best_rice_parameter = rice_parameter; |
| 2090 | best_partition_bits = partition_bits; |
| 2091 | } |
| 2092 | } |
| 2093 | #endif |
| 2094 | parameters[partition] = best_rice_parameter; |
| 2095 | bits_ += best_partition_bits; |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | *bits = bits_; |
| 2100 | return true; |
| 2101 | } |
| 2102 | |
| 2103 | #ifdef DONT_ESTIMATE_RICE_BITS |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2104 | 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] | 2105 | #else |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2106 | 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] | 2107 | #endif |
| 2108 | { |
| 2109 | unsigned rice_parameter, partition_bits; |
| 2110 | #ifndef NO_RICE_SEARCH |
| 2111 | unsigned best_partition_bits; |
| 2112 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 2113 | #endif |
| 2114 | unsigned flat_bits; |
| 2115 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
| 2116 | |
| 2117 | FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER); |
| 2118 | |
| 2119 | if(partition_order == 0) { |
| 2120 | unsigned i; |
| 2121 | |
| 2122 | #ifndef NO_RICE_SEARCH |
| 2123 | if(rice_parameter_search_dist) { |
| 2124 | if(suggested_rice_parameter < rice_parameter_search_dist) |
| 2125 | min_rice_parameter = 0; |
| 2126 | else |
| 2127 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 2128 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
| 2129 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2130 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2131 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2132 | #endif |
| 2133 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 2134 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2135 | } |
| 2136 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2137 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2138 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2139 | best_partition_bits = 0xffffffff; |
| 2140 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 2141 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2142 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2143 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2144 | partition_bits = (2+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2145 | #else |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2146 | const unsigned rice_parameter_estimate = rice_parameter-1; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2147 | partition_bits = (1+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2148 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2149 | #else |
| 2150 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2151 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2152 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2153 | for(i = 0; i < residual_samples; i++) { |
| 2154 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2155 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2156 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2157 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2158 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2159 | #endif |
| 2160 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2161 | 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] | 2162 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2163 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2164 | #ifndef NO_RICE_SEARCH |
| 2165 | if(partition_bits < best_partition_bits) { |
| 2166 | best_rice_parameter = rice_parameter; |
| 2167 | best_partition_bits = partition_bits; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2168 | } |
| 2169 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2170 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2171 | if(search_for_escapes) { |
| 2172 | 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; |
| 2173 | if(flat_bits <= best_partition_bits) { |
| 2174 | raw_bits[0] = raw_bits_per_partition[0]; |
| 2175 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 2176 | best_partition_bits = flat_bits; |
| 2177 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2178 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2179 | parameters[0] = best_rice_parameter; |
| 2180 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2181 | } |
| 2182 | else { |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2183 | unsigned partition, residual_sample, save_residual_sample, partition_sample; |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2184 | unsigned partition_samples; |
| 2185 | FLAC__uint64 mean, k; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 2186 | const unsigned partitions = 1u << partition_order; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2187 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2188 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2189 | if(partition == 0) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2190 | if(partition_samples <= predictor_order) |
| 2191 | return false; |
| 2192 | else |
| 2193 | partition_samples -= predictor_order; |
| 2194 | } |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 2195 | mean = abs_residual_partition_sums[partition]; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2196 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 2197 | mean += partition_samples >> 1; /* for rounding effect */ |
| 2198 | mean /= partition_samples; |
| 2199 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2200 | /* calc rice_parameter = floor(log2(mean)) */ |
| 2201 | rice_parameter = 0; |
| 2202 | mean>>=1; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2203 | while(mean) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2204 | rice_parameter++; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2205 | mean >>= 1; |
| 2206 | } |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2207 | #else |
Josh Coalson | 05d2079 | 2001-06-29 23:12:26 +0000 | [diff] [blame] | 2208 | /* calc rice_parameter ala LOCO-I */ |
Josh Coalson | b3347bd | 2001-07-16 18:06:41 +0000 | [diff] [blame] | 2209 | 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] | 2210 | ; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2211 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2212 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2213 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2214 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2215 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2216 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2217 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2218 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2219 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2220 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2221 | if(rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2222 | min_rice_parameter = 0; |
| 2223 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2224 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 2225 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2226 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | 3120949 | 2001-07-18 23:43:01 +0000 | [diff] [blame] | 2227 | #ifdef DEBUG_VERBOSE |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2228 | fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1); |
| 2229 | #endif |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2230 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2231 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2232 | } |
| 2233 | else |
| 2234 | min_rice_parameter = max_rice_parameter = rice_parameter; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 2235 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2236 | best_partition_bits = 0xffffffff; |
| 2237 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 2238 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2239 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2240 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2241 | partition_bits = (2+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2242 | #else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2243 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 2244 | partition_bits = (1+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2245 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2246 | #else |
| 2247 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2248 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2249 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2250 | save_residual_sample = residual_sample; |
| 2251 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) { |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 2252 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 2253 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2254 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2255 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2256 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 2257 | #endif |
| 2258 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2259 | 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] | 2260 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2261 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2262 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 2263 | if(rice_parameter != max_rice_parameter) |
| 2264 | residual_sample = save_residual_sample; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2265 | if(partition_bits < best_partition_bits) { |
| 2266 | best_rice_parameter = rice_parameter; |
| 2267 | best_partition_bits = partition_bits; |
| 2268 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2269 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2270 | #endif |
Josh Coalson | 8395d02 | 2001-07-12 21:25:22 +0000 | [diff] [blame] | 2271 | if(search_for_escapes) { |
| 2272 | 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; |
| 2273 | if(flat_bits <= best_partition_bits) { |
| 2274 | raw_bits[partition] = raw_bits_per_partition[partition]; |
| 2275 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 2276 | best_partition_bits = flat_bits; |
| 2277 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2278 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 2279 | parameters[partition] = best_rice_parameter; |
| 2280 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | *bits = bits_; |
| 2285 | return true; |
| 2286 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2287 | |
Josh Coalson | f1eff45 | 2002-07-31 07:05:33 +0000 | [diff] [blame] | 2288 | unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples) |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2289 | { |
| 2290 | unsigned i, shift; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2291 | FLAC__int32 x = 0; |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 2292 | |
| 2293 | for(i = 0; i < samples && !(x&1); i++) |
| 2294 | x |= signal[i]; |
| 2295 | |
| 2296 | if(x == 0) { |
| 2297 | shift = 0; |
| 2298 | } |
| 2299 | else { |
| 2300 | for(shift = 0; !(x&1); shift++) |
| 2301 | x >>= 1; |
| 2302 | } |
| 2303 | |
| 2304 | if(shift > 0) { |
| 2305 | for(i = 0; i < samples; i++) |
| 2306 | signal[i] >>= shift; |
| 2307 | } |
| 2308 | |
| 2309 | return shift; |
| 2310 | } |
Josh Coalson | d86e03b | 2002-08-03 21:56:15 +0000 | [diff] [blame] | 2311 | |
| 2312 | void append_to_verify_fifo_(verify_input_fifo *fifo, const FLAC__int32 * const input[], unsigned input_offset, unsigned channels, unsigned wide_samples) |
| 2313 | { |
| 2314 | unsigned channel; |
| 2315 | |
| 2316 | for(channel = 0; channel < channels; channel++) |
| 2317 | memcpy(&fifo->data[channel][fifo->tail], &input[channel][input_offset], sizeof(FLAC__int32) * wide_samples); |
| 2318 | |
| 2319 | fifo->tail += wide_samples; |
| 2320 | |
| 2321 | FLAC__ASSERT(fifo->tail <= fifo->size); |
| 2322 | } |
| 2323 | |
| 2324 | void append_to_verify_fifo_interleaved_(verify_input_fifo *fifo, const FLAC__int32 input[], unsigned input_offset, unsigned channels, unsigned wide_samples) |
| 2325 | { |
| 2326 | unsigned channel; |
| 2327 | unsigned sample, wide_sample; |
| 2328 | unsigned tail = fifo->tail; |
| 2329 | |
| 2330 | sample = input_offset * channels; |
| 2331 | for(wide_sample = 0; wide_sample < wide_samples; wide_sample++) { |
| 2332 | for(channel = 0; channel < channels; channel++) |
| 2333 | fifo->data[channel][tail] = input[sample++]; |
| 2334 | tail++; |
| 2335 | } |
| 2336 | fifo->tail = tail; |
| 2337 | |
| 2338 | FLAC__ASSERT(fifo->tail <= fifo->size); |
| 2339 | } |
| 2340 | |
| 2341 | FLAC__StreamDecoderReadStatus verify_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) |
| 2342 | { |
| 2343 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 2344 | const unsigned encoded_bytes = encoder->private_->verify.output.bytes; |
| 2345 | (void)decoder; |
| 2346 | |
| 2347 | if(encoder->private_->verify.needs_magic_hack) { |
| 2348 | FLAC__ASSERT(*bytes >= FLAC__STREAM_SYNC_LENGTH); |
| 2349 | *bytes = FLAC__STREAM_SYNC_LENGTH; |
| 2350 | memcpy(buffer, FLAC__STREAM_SYNC_STRING, *bytes); |
| 2351 | encoder->private_->verify.needs_magic_hack = false; |
| 2352 | } |
| 2353 | else { |
| 2354 | if(encoded_bytes == 0) { |
| 2355 | //@@@@ underflow happened, should we do something else here? is this an assert failure? |
| 2356 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
| 2357 | } |
| 2358 | else if(encoded_bytes < *bytes) |
| 2359 | *bytes = encoded_bytes; |
| 2360 | memcpy(buffer, encoder->private_->verify.output.data, *bytes); |
| 2361 | encoder->private_->verify.output.data += *bytes; |
| 2362 | encoder->private_->verify.output.bytes -= *bytes; |
| 2363 | } |
| 2364 | |
| 2365 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
| 2366 | } |
| 2367 | |
| 2368 | FLAC__StreamDecoderWriteStatus verify_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 2369 | { |
| 2370 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder *)client_data; |
| 2371 | unsigned channel; |
| 2372 | const unsigned channels = FLAC__stream_decoder_get_channels(decoder); |
| 2373 | const unsigned blocksize = frame->header.blocksize; |
| 2374 | const unsigned bytes_per_block = sizeof(FLAC__int32) * blocksize; |
| 2375 | |
| 2376 | for(channel = 0; channel < channels; channel++) { |
| 2377 | if(0 != memcmp(buffer[channel], encoder->private_->verify.input_fifo.data[channel], bytes_per_block)) { |
| 2378 | unsigned i, sample = 0; |
| 2379 | FLAC__int32 expect = 0, got = 0; |
| 2380 | |
| 2381 | for(i = 0; i < blocksize; i++) { |
| 2382 | if(buffer[channel][i] != encoder->private_->verify.input_fifo.data[channel][i]) { |
| 2383 | sample = i; |
| 2384 | expect = (FLAC__int32)encoder->private_->verify.input_fifo.data[channel][i]; |
| 2385 | got = (FLAC__int32)buffer[channel][i]; |
| 2386 | break; |
| 2387 | } |
| 2388 | } |
| 2389 | FLAC__ASSERT(i < blocksize); |
| 2390 | FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 2391 | encoder->private_->verify.error_stats.absolute_sample = frame->header.number.sample_number + sample; |
| 2392 | encoder->private_->verify.error_stats.frame_number = frame->header.number.sample_number / blocksize; |
| 2393 | encoder->private_->verify.error_stats.channel = channel; |
| 2394 | encoder->private_->verify.error_stats.sample = sample; |
| 2395 | encoder->private_->verify.error_stats.expected = expect; |
| 2396 | encoder->private_->verify.error_stats.got = got; |
| 2397 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA; |
| 2398 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
| 2399 | } |
| 2400 | } |
| 2401 | /* dequeue the frame from the fifo */ |
| 2402 | for(channel = 0; channel < channels; channel++) { |
| 2403 | memmove(&encoder->private_->verify.input_fifo.data[channel][0], &encoder->private_->verify.input_fifo.data[channel][blocksize], encoder->private_->verify.input_fifo.tail - blocksize); |
| 2404 | } |
| 2405 | encoder->private_->verify.input_fifo.tail -= blocksize; |
| 2406 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 2407 | } |
| 2408 | |
| 2409 | void verify_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) |
| 2410 | { |
| 2411 | (void)decoder, (void)metadata, (void)client_data; |
| 2412 | } |
| 2413 | |
| 2414 | void verify_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 2415 | { |
| 2416 | FLAC__StreamEncoder *encoder = (FLAC__StreamEncoder*)client_data; |
| 2417 | (void)decoder, (void)status; |
| 2418 | encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR; |
| 2419 | } |