Josh Coalson | 26560dd | 2001-02-08 00:38:41 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Josh Coalson | 70118f6 | 2001-01-16 20:17:53 +0000 | [diff] [blame] | 2 | * Copyright (C) 2000,2001 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 | 0a72e18 | 2001-04-13 19:17:16 +0000 | [diff] [blame] | 24 | #include "FLAC/seek_table.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 | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 46 | /*********************************************************************** |
| 47 | * |
| 48 | * Private class method prototypes |
| 49 | * |
| 50 | ***********************************************************************/ |
| 51 | |
| 52 | static bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size); |
| 53 | static bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, bool is_last_frame); |
| 54 | static bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, bool is_last_frame); |
| 55 | static bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits); |
| 56 | static bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame); |
| 57 | static unsigned stream_encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe); |
| 58 | static unsigned stream_encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe); |
| 59 | static unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe); |
| 60 | static unsigned stream_encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe); |
| 61 | static unsigned stream_encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 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, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]); |
| 62 | #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES) |
| 63 | static unsigned stream_encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order); |
| 64 | #endif |
| 65 | static bool stream_encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 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, unsigned parameters[], unsigned raw_bits[], unsigned *bits); |
| 66 | static unsigned stream_encoder_get_wasted_bits_(int32 signal[], unsigned samples); |
| 67 | |
| 68 | /*********************************************************************** |
| 69 | * |
| 70 | * Private class data |
| 71 | * |
| 72 | ***********************************************************************/ |
| 73 | |
| 74 | typedef struct FLAC__StreamEncoderPrivate { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 75 | unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */ |
| 76 | int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */ |
| 77 | int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */ |
| 78 | real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */ |
| 79 | real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */ |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 80 | unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */ |
Josh Coalson | ac4c158 | 2001-03-28 23:10:22 +0000 | [diff] [blame] | 81 | 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 | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 82 | int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */ |
| 83 | int32 *residual_workspace_mid_side[2][2]; |
| 84 | FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2]; |
| 85 | FLAC__Subframe subframe_workspace_mid_side[2][2]; |
| 86 | FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2]; |
| 87 | FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2]; |
| 88 | unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */ |
| 89 | unsigned best_subframe_mid_side[2]; |
| 90 | unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */ |
| 91 | unsigned best_subframe_bits_mid_side[2]; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 92 | uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */ |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 93 | uint32 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */ |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 94 | unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 95 | FLAC__BitBuffer frame; /* the current frame being worked on */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 96 | double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 97 | unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */ |
| 98 | unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */ |
| 99 | FLAC__ChannelAssignment last_channel_assignment; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 100 | FLAC__StreamMetaData metadata; |
| 101 | unsigned current_sample_number; |
| 102 | unsigned current_frame_number; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 103 | struct MD5Context md5context; |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 104 | FLAC__CPUInfo cpuinfo; |
| 105 | unsigned (*local_fixed_compute_best_predictor)(const int32 data[], unsigned data_len, real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]); |
| 106 | void (*local_lpc_compute_autocorrelation)(const real data[], unsigned data_len, unsigned lag, real autoc[]); |
Josh Coalson | 8e83362 | 2001-05-29 20:49:51 +0000 | [diff] [blame] | 107 | void (*local_lpc_compute_residual_from_qlp_coefficients)(const int32 data[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 residual[]); |
Josh Coalson | 92d4240 | 2001-05-31 20:53:19 +0000 | [diff] [blame] | 108 | void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const int32 data[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 residual[]); |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 109 | bool use_slow; /* use slow 64-bit versions of some functions */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 110 | FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data); |
| 111 | void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 112 | void *client_data; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 113 | /* unaligned (original) pointers to allocated data */ |
| 114 | int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 115 | int32 *integer_signal_mid_side_unaligned[2]; |
| 116 | real *real_signal_unaligned[FLAC__MAX_CHANNELS]; |
| 117 | real *real_signal_mid_side_unaligned[2]; |
| 118 | int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2]; |
| 119 | int32 *residual_workspace_mid_side_unaligned[2][2]; |
| 120 | uint32 *abs_residual_unaligned; |
| 121 | uint32 *abs_residual_partition_sums_unaligned; |
| 122 | unsigned *raw_bits_per_partition_unaligned; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 123 | } FLAC__StreamEncoderPrivate; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 124 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 125 | /*********************************************************************** |
| 126 | * |
| 127 | * Public static class data |
| 128 | * |
| 129 | ***********************************************************************/ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 130 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 131 | const char *FLAC__StreamEncoderStateString[] = { |
| 132 | "FLAC__STREAM_ENCODER_OK", |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 133 | "FLAC__STREAM_ENCODER_INVALID_CALLBACK", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 134 | "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS", |
| 135 | "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE", |
| 136 | "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE", |
| 137 | "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE", |
| 138 | "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION", |
| 139 | "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH", |
| 140 | "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH", |
| 141 | "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE", |
| 142 | "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER", |
| 143 | "FLAC__STREAM_ENCODER_NOT_STREAMABLE", |
| 144 | "FLAC__STREAM_ENCODER_FRAMING_ERROR", |
| 145 | "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING", |
| 146 | "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING", |
| 147 | "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR", |
| 148 | "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED", |
| 149 | "FLAC__STREAM_ENCODER_UNINITIALIZED" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 152 | const char *FLAC__StreamEncoderWriteStatusString[] = { |
| 153 | "FLAC__STREAM_ENCODER_WRITE_OK", |
| 154 | "FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 157 | /*********************************************************************** |
| 158 | * |
| 159 | * Class constructor/destructor |
| 160 | * |
| 161 | ***********************************************************************/ |
| 162 | FLAC__StreamEncoder *FLAC__stream_encoder_new() |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 163 | { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 164 | FLAC__StreamEncoder *encoder; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 165 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 166 | 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] | 167 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 168 | encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder)); |
| 169 | if(encoder == 0) { |
| 170 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 171 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 172 | encoder->protected = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected)); |
| 173 | if(encoder->protected == 0) { |
| 174 | free(encoder); |
| 175 | return 0; |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 176 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 177 | encoder->private = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate)); |
| 178 | if(encoder->private == 0) { |
| 179 | free(encoder->protected); |
| 180 | free(encoder); |
| 181 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 182 | } |
Josh Coalson | d98c43d | 2001-05-13 05:17:01 +0000 | [diff] [blame] | 183 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 184 | encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 185 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 186 | encoder->protected->streamable_subset = true; |
| 187 | encoder->protected->do_mid_side_stereo = false; |
| 188 | encoder->protected->loose_mid_side_stereo = false; |
| 189 | encoder->protected->channels = 2; |
| 190 | encoder->protected->bits_per_sample = 16; |
| 191 | encoder->protected->sample_rate = 44100; |
| 192 | encoder->protected->blocksize = 1152; |
| 193 | encoder->protected->max_lpc_order = 0; |
| 194 | encoder->protected->qlp_coeff_precision = 0; |
| 195 | encoder->protected->do_qlp_coeff_prec_search = false; |
| 196 | encoder->protected->do_exhaustive_model_search = false; |
| 197 | encoder->protected->min_residual_partition_order = 0; |
| 198 | encoder->protected->max_residual_partition_order = 0; |
| 199 | encoder->protected->rice_parameter_search_dist = 0; |
| 200 | encoder->protected->total_samples_estimate = 0; |
| 201 | encoder->protected->seek_table = 0; |
| 202 | encoder->protected->padding = 0; |
| 203 | encoder->protected->last_metadata_is_last = true; |
| 204 | |
| 205 | encoder->private->write_callback = 0; |
| 206 | encoder->private->metadata_callback = 0; |
| 207 | encoder->private->client_data = 0; |
| 208 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 209 | return encoder; |
| 210 | } |
| 211 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 212 | void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 213 | { |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 214 | FLAC__ASSERT(encoder != 0); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 215 | FLAC__ASSERT(encoder->protected != 0); |
| 216 | FLAC__ASSERT(encoder->private != 0); |
| 217 | |
| 218 | free(encoder->private); |
| 219 | free(encoder->protected); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 220 | free(encoder); |
| 221 | } |
| 222 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 223 | /*********************************************************************** |
| 224 | * |
| 225 | * Public class methods |
| 226 | * |
| 227 | ***********************************************************************/ |
| 228 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 229 | FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 230 | { |
| 231 | unsigned i; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 232 | FLAC__StreamMetaData padding_block; |
| 233 | FLAC__StreamMetaData seek_table_block; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 234 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 235 | FLAC__ASSERT(encoder != 0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 236 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 237 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 238 | return encoder->protected->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 239 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 240 | encoder->protected->state = FLAC__STREAM_ENCODER_OK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 241 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 242 | if(0 == encoder->private->write_callback || 0 == encoder->private->metadata_callback) |
| 243 | return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 244 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 245 | if(encoder->protected->channels == 0 || encoder->protected->channels > FLAC__MAX_CHANNELS) |
| 246 | return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS; |
Josh Coalson | 69f1ee0 | 2001-01-24 00:54:43 +0000 | [diff] [blame] | 247 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 248 | if(encoder->protected->do_mid_side_stereo && encoder->protected->channels != 2) |
| 249 | return encoder->protected->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH; |
Josh Coalson | d37d135 | 2001-05-30 23:09:31 +0000 | [diff] [blame] | 250 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 251 | if(encoder->protected->loose_mid_side_stereo && !encoder->protected->do_mid_side_stereo) |
| 252 | return encoder->protected->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 253 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 254 | if(encoder->protected->bits_per_sample >= 32) |
| 255 | 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] | 256 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 257 | if(encoder->protected->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE) |
| 258 | return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 259 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 260 | if(encoder->protected->sample_rate == 0 || encoder->protected->sample_rate > FLAC__MAX_SAMPLE_RATE) |
| 261 | return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 262 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 263 | if(encoder->protected->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected->blocksize > FLAC__MAX_BLOCK_SIZE) |
| 264 | return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE; |
| 265 | |
| 266 | if(encoder->protected->blocksize < encoder->protected->max_lpc_order) |
| 267 | return encoder->protected->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER; |
| 268 | |
| 269 | if(encoder->protected->qlp_coeff_precision == 0) { |
| 270 | if(encoder->protected->bits_per_sample < 16) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 271 | /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */ |
| 272 | /* @@@ until then we'll make a guess */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 273 | 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] | 274 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 275 | else if(encoder->protected->bits_per_sample == 16) { |
| 276 | if(encoder->protected->blocksize <= 192) |
| 277 | encoder->protected->qlp_coeff_precision = 7; |
| 278 | else if(encoder->protected->blocksize <= 384) |
| 279 | encoder->protected->qlp_coeff_precision = 8; |
| 280 | else if(encoder->protected->blocksize <= 576) |
| 281 | encoder->protected->qlp_coeff_precision = 9; |
| 282 | else if(encoder->protected->blocksize <= 1152) |
| 283 | encoder->protected->qlp_coeff_precision = 10; |
| 284 | else if(encoder->protected->blocksize <= 2304) |
| 285 | encoder->protected->qlp_coeff_precision = 11; |
| 286 | else if(encoder->protected->blocksize <= 4608) |
| 287 | encoder->protected->qlp_coeff_precision = 12; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 288 | else |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 289 | encoder->protected->qlp_coeff_precision = 13; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 290 | } |
| 291 | else { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 292 | encoder->protected->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->protected->bits_per_sample - 1); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 295 | else if(encoder->protected->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected->qlp_coeff_precision + encoder->protected->bits_per_sample >= 8*sizeof(uint32) || encoder->protected->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)) |
| 296 | return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 297 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 298 | if(encoder->protected->streamable_subset) { |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 299 | //@@@ add check for blocksize here |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 300 | 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) |
| 301 | return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
| 302 | if(encoder->protected->sample_rate > 655350) |
| 303 | return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 306 | if(encoder->protected->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN)) |
| 307 | encoder->protected->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1; |
| 308 | if(encoder->protected->min_residual_partition_order >= encoder->protected->max_residual_partition_order) |
| 309 | encoder->protected->min_residual_partition_order = encoder->protected->max_residual_partition_order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 310 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 311 | encoder->private->input_capacity = 0; |
| 312 | for(i = 0; i < encoder->protected->channels; i++) { |
| 313 | encoder->private->integer_signal_unaligned[i] = encoder->private->integer_signal[i] = 0; |
| 314 | encoder->private->real_signal_unaligned[i] = encoder->private->real_signal[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 315 | } |
| 316 | for(i = 0; i < 2; i++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 317 | encoder->private->integer_signal_mid_side_unaligned[i] = encoder->private->integer_signal_mid_side[i] = 0; |
| 318 | 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] | 319 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 320 | for(i = 0; i < encoder->protected->channels; i++) { |
| 321 | encoder->private->residual_workspace_unaligned[i][0] = encoder->private->residual_workspace[i][0] = 0; |
| 322 | encoder->private->residual_workspace_unaligned[i][1] = encoder->private->residual_workspace[i][1] = 0; |
| 323 | encoder->private->best_subframe[i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 324 | } |
| 325 | for(i = 0; i < 2; i++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 326 | encoder->private->residual_workspace_mid_side_unaligned[i][0] = encoder->private->residual_workspace_mid_side[i][0] = 0; |
| 327 | encoder->private->residual_workspace_mid_side_unaligned[i][1] = encoder->private->residual_workspace_mid_side[i][1] = 0; |
| 328 | encoder->private->best_subframe_mid_side[i] = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 329 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 330 | for(i = 0; i < encoder->protected->channels; i++) { |
| 331 | encoder->private->subframe_workspace_ptr[i][0] = &encoder->private->subframe_workspace[i][0]; |
| 332 | 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] | 333 | } |
| 334 | for(i = 0; i < 2; i++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 335 | encoder->private->subframe_workspace_ptr_mid_side[i][0] = &encoder->private->subframe_workspace_mid_side[i][0]; |
| 336 | 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] | 337 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 338 | encoder->private->abs_residual_unaligned = encoder->private->abs_residual = 0; |
| 339 | encoder->private->abs_residual_partition_sums_unaligned = encoder->private->abs_residual_partition_sums = 0; |
| 340 | encoder->private->raw_bits_per_partition_unaligned = encoder->private->raw_bits_per_partition = 0; |
| 341 | encoder->private->loose_mid_side_stereo_frames_exact = (double)encoder->protected->sample_rate * 0.4 / (double)encoder->protected->blocksize; |
| 342 | encoder->private->loose_mid_side_stereo_frames = (unsigned)(encoder->private->loose_mid_side_stereo_frames_exact + 0.5); |
| 343 | if(encoder->private->loose_mid_side_stereo_frames == 0) |
| 344 | encoder->private->loose_mid_side_stereo_frames = 1; |
| 345 | encoder->private->loose_mid_side_stereo_frame_count = 0; |
| 346 | encoder->private->current_sample_number = 0; |
| 347 | encoder->private->current_frame_number = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 348 | |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 349 | /* |
| 350 | * get the CPU info and set the function pointers |
| 351 | */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 352 | FLAC__cpu_info(&encoder->private->cpuinfo); |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 353 | /* first default to the non-asm routines */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 354 | encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation; |
| 355 | encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor; |
| 356 | encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients; |
| 357 | 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] | 358 | /* now override with asm where appropriate */ |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 359 | #ifndef FLAC__NO_ASM |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 360 | FLAC__ASSERT(encoder->private->cpuinfo.use_asm); |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 361 | #ifdef FLAC__CPU_IA32 |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 362 | FLAC__ASSERT(encoder->private->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 363 | #ifdef FLAC__HAS_NASM |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 364 | if(0 && encoder->private->cpuinfo.data.ia32.sse) { /* SSE version lacks necessary resolution, plus SSE flag doesn't check for OS support */ |
| 365 | if(encoder->protected->max_lpc_order < 4) |
Josh Coalson | e6499bd | 2001-06-13 18:11:25 +0000 | [diff] [blame] | 366 | encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 367 | else if(encoder->protected->max_lpc_order < 8) |
Josh Coalson | e6499bd | 2001-06-13 18:11:25 +0000 | [diff] [blame] | 368 | encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 369 | else if(encoder->protected->max_lpc_order < 12) |
Josh Coalson | e6499bd | 2001-06-13 18:11:25 +0000 | [diff] [blame] | 370 | encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 371 | else |
Josh Coalson | e6499bd | 2001-06-13 18:11:25 +0000 | [diff] [blame] | 372 | encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 373 | } |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 374 | else |
Josh Coalson | e6499bd | 2001-06-13 18:11:25 +0000 | [diff] [blame] | 375 | encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 376 | if(encoder->private->cpuinfo.data.ia32.mmx && encoder->private->cpuinfo.data.ia32.cmov) |
Josh Coalson | e6499bd | 2001-06-13 18:11:25 +0000 | [diff] [blame] | 377 | encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 378 | if(encoder->private->cpuinfo.data.ia32.mmx) { |
Josh Coalson | e6499bd | 2001-06-13 18:11:25 +0000 | [diff] [blame] | 379 | encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 380 | encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx; |
Josh Coalson | 92d4240 | 2001-05-31 20:53:19 +0000 | [diff] [blame] | 381 | } |
| 382 | else { |
Josh Coalson | e6499bd | 2001-06-13 18:11:25 +0000 | [diff] [blame] | 383 | encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
| 384 | encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32; |
Josh Coalson | 92d4240 | 2001-05-31 20:53:19 +0000 | [diff] [blame] | 385 | } |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 386 | #endif |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 387 | #endif |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 388 | #endif |
Josh Coalson | cf30f50 | 2001-05-23 20:57:44 +0000 | [diff] [blame] | 389 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 390 | if(encoder->protected->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected->blocksize)+1 > 30) |
| 391 | encoder->private->use_slow = true; |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 392 | else |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 393 | encoder->private->use_slow = false; |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 394 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 395 | if(!stream_encoder_resize_buffers_(encoder, encoder->protected->blocksize)) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 396 | /* the above function sets the state for us in case of an error */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 397 | return encoder->protected->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 398 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 399 | FLAC__bitbuffer_init(&encoder->private->frame); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 400 | |
| 401 | /* |
| 402 | * write the stream header |
| 403 | */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 404 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 405 | if(!FLAC__bitbuffer_clear(&encoder->private->frame)) |
| 406 | return encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 407 | if(!FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN)) |
| 408 | return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 409 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 410 | encoder->private->metadata.type = FLAC__METADATA_TYPE_STREAMINFO; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 411 | encoder->private->metadata.is_last = (encoder->protected->seek_table == 0 && encoder->protected->padding == 0 && encoder->protected->last_metadata_is_last); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 412 | encoder->private->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; |
| 413 | encoder->private->metadata.data.stream_info.min_blocksize = encoder->protected->blocksize; /* this encoder uses the same blocksize for the whole stream */ |
| 414 | encoder->private->metadata.data.stream_info.max_blocksize = encoder->protected->blocksize; |
| 415 | encoder->private->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 416 | encoder->private->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */ |
| 417 | encoder->private->metadata.data.stream_info.sample_rate = encoder->protected->sample_rate; |
| 418 | encoder->private->metadata.data.stream_info.channels = encoder->protected->channels; |
| 419 | encoder->private->metadata.data.stream_info.bits_per_sample = encoder->protected->bits_per_sample; |
| 420 | encoder->private->metadata.data.stream_info.total_samples = encoder->protected->total_samples_estimate; /* we will replace this later with the real total */ |
| 421 | memset(encoder->private->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */ |
| 422 | MD5Init(&encoder->private->md5context); |
| 423 | if(!FLAC__add_metadata_block(&encoder->private->metadata, &encoder->private->frame)) |
| 424 | return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 425 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 426 | if(0 != encoder->protected->seek_table) { |
| 427 | if(!FLAC__seek_table_is_valid(encoder->protected->seek_table)) |
| 428 | return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE; |
| 429 | seek_table_block.type = FLAC__METADATA_TYPE_SEEKTABLE; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 430 | seek_table_block.is_last = (encoder->protected->padding == 0 && encoder->protected->last_metadata_is_last); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 431 | seek_table_block.length = encoder->protected->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN; |
| 432 | seek_table_block.data.seek_table = *encoder->protected->seek_table; |
| 433 | if(!FLAC__add_metadata_block(&seek_table_block, &encoder->private->frame)) |
| 434 | return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 0a72e18 | 2001-04-13 19:17:16 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 437 | /* add a PADDING block if requested */ |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 438 | if(encoder->protected->padding > 0) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 439 | padding_block.type = FLAC__METADATA_TYPE_PADDING; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 440 | padding_block.is_last = encoder->protected->last_metadata_is_last; |
| 441 | padding_block.length = encoder->protected->padding; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 442 | if(!FLAC__add_metadata_block(&padding_block, &encoder->private->frame)) |
| 443 | return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | c692d38 | 2001-02-23 21:05:05 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 446 | FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned before writing */ |
| 447 | FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */ |
| 448 | if(encoder->private->write_callback(encoder, encoder->private->frame.buffer, encoder->private->frame.bytes, 0, encoder->private->current_frame_number, encoder->private->client_data) != FLAC__STREAM_ENCODER_WRITE_OK) |
| 449 | return encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 450 | |
Josh Coalson | cbbbb5f | 2001-01-23 00:41:48 +0000 | [diff] [blame] | 451 | /* now that the metadata block is written, we can init this to an absurdly-high value... */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 452 | 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] | 453 | /* ... and clear this to 0 */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 454 | encoder->private->metadata.data.stream_info.total_samples = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 455 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 456 | return encoder->protected->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 459 | void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 460 | { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 461 | unsigned i, channel; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 462 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 463 | FLAC__ASSERT(encoder != 0); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 464 | if(encoder->protected->state == FLAC__STREAM_ENCODER_UNINITIALIZED) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 465 | return; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 466 | if(encoder->private->current_sample_number != 0) { |
| 467 | encoder->protected->blocksize = encoder->private->current_sample_number; |
| 468 | stream_encoder_process_frame_(encoder, true); /* true => is last frame */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 469 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 470 | MD5Final(encoder->private->metadata.data.stream_info.md5sum, &encoder->private->md5context); |
| 471 | encoder->private->metadata_callback(encoder, &encoder->private->metadata, encoder->private->client_data); |
| 472 | for(i = 0; i < encoder->protected->channels; i++) { |
| 473 | if(encoder->private->integer_signal_unaligned[i] != 0) { |
| 474 | free(encoder->private->integer_signal_unaligned[i]); |
| 475 | encoder->private->integer_signal_unaligned[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 476 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 477 | if(encoder->private->real_signal_unaligned[i] != 0) { |
| 478 | free(encoder->private->real_signal_unaligned[i]); |
| 479 | encoder->private->real_signal_unaligned[i] = 0; |
| 480 | } |
| 481 | } |
| 482 | for(i = 0; i < 2; i++) { |
| 483 | if(encoder->private->integer_signal_mid_side_unaligned[i] != 0) { |
| 484 | free(encoder->private->integer_signal_mid_side_unaligned[i]); |
| 485 | encoder->private->integer_signal_mid_side_unaligned[i] = 0; |
| 486 | } |
| 487 | if(encoder->private->real_signal_mid_side_unaligned[i] != 0) { |
| 488 | free(encoder->private->real_signal_mid_side_unaligned[i]); |
| 489 | encoder->private->real_signal_mid_side_unaligned[i] = 0; |
| 490 | } |
| 491 | } |
| 492 | for(channel = 0; channel < encoder->protected->channels; channel++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 493 | for(i = 0; i < 2; i++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 494 | if(encoder->private->residual_workspace_unaligned[channel][i] != 0) { |
| 495 | free(encoder->private->residual_workspace_unaligned[channel][i]); |
| 496 | encoder->private->residual_workspace_unaligned[channel][i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 499 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 500 | for(channel = 0; channel < 2; channel++) { |
| 501 | for(i = 0; i < 2; i++) { |
| 502 | if(encoder->private->residual_workspace_mid_side_unaligned[channel][i] != 0) { |
| 503 | free(encoder->private->residual_workspace_mid_side_unaligned[channel][i]); |
| 504 | encoder->private->residual_workspace_mid_side_unaligned[channel][i] = 0; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | if(encoder->private->abs_residual_unaligned != 0) { |
| 509 | free(encoder->private->abs_residual_unaligned); |
| 510 | encoder->private->abs_residual_unaligned = 0; |
| 511 | } |
| 512 | if(encoder->private->abs_residual_partition_sums_unaligned != 0) { |
| 513 | free(encoder->private->abs_residual_partition_sums_unaligned); |
| 514 | encoder->private->abs_residual_partition_sums_unaligned = 0; |
| 515 | } |
| 516 | if(encoder->private->raw_bits_per_partition_unaligned != 0) { |
| 517 | free(encoder->private->raw_bits_per_partition_unaligned); |
| 518 | encoder->private->raw_bits_per_partition_unaligned = 0; |
| 519 | } |
| 520 | FLAC__bitbuffer_free(&encoder->private->frame); |
| 521 | |
| 522 | encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 525 | bool FLAC__stream_encoder_set_streamable_subset(const FLAC__StreamEncoder *encoder, bool value) |
| 526 | { |
| 527 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 528 | return false; |
| 529 | encoder->protected->streamable_subset = value; |
| 530 | return true; |
| 531 | } |
| 532 | |
| 533 | bool FLAC__stream_encoder_set_do_mid_side_stereo(const FLAC__StreamEncoder *encoder, bool value) |
| 534 | { |
| 535 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 536 | return false; |
| 537 | encoder->protected->do_mid_side_stereo = value; |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | bool FLAC__stream_encoder_set_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder, bool value) |
| 542 | { |
| 543 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 544 | return false; |
| 545 | encoder->protected->loose_mid_side_stereo = value; |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | bool FLAC__stream_encoder_set_channels(const FLAC__StreamEncoder *encoder, unsigned value) |
| 550 | { |
| 551 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 552 | return false; |
| 553 | encoder->protected->channels = value; |
| 554 | return true; |
| 555 | } |
| 556 | |
| 557 | bool FLAC__stream_encoder_set_bits_per_sample(const FLAC__StreamEncoder *encoder, unsigned value) |
| 558 | { |
| 559 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 560 | return false; |
| 561 | encoder->protected->bits_per_sample = value; |
| 562 | return true; |
| 563 | } |
| 564 | |
| 565 | bool FLAC__stream_encoder_set_sample_rate(const FLAC__StreamEncoder *encoder, unsigned value) |
| 566 | { |
| 567 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 568 | return false; |
| 569 | encoder->protected->sample_rate = value; |
| 570 | return true; |
| 571 | } |
| 572 | |
| 573 | bool FLAC__stream_encoder_set_blocksize(const FLAC__StreamEncoder *encoder, unsigned value) |
| 574 | { |
| 575 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 576 | return false; |
| 577 | encoder->protected->blocksize = value; |
| 578 | return true; |
| 579 | } |
| 580 | |
| 581 | bool FLAC__stream_encoder_set_max_lpc_order(const FLAC__StreamEncoder *encoder, unsigned value) |
| 582 | { |
| 583 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 584 | return false; |
| 585 | encoder->protected->max_lpc_order = value; |
| 586 | return true; |
| 587 | } |
| 588 | |
| 589 | bool FLAC__stream_encoder_set_qlp_coeff_precision(const FLAC__StreamEncoder *encoder, unsigned value) |
| 590 | { |
| 591 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 592 | return false; |
| 593 | encoder->protected->qlp_coeff_precision = value; |
| 594 | return true; |
| 595 | } |
| 596 | |
| 597 | bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder, bool value) |
| 598 | { |
| 599 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 600 | return false; |
| 601 | encoder->protected->do_qlp_coeff_prec_search = value; |
| 602 | return true; |
| 603 | } |
| 604 | |
| 605 | bool FLAC__stream_encoder_set_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder, bool value) |
| 606 | { |
| 607 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 608 | return false; |
| 609 | encoder->protected->do_exhaustive_model_search = value; |
| 610 | return true; |
| 611 | } |
| 612 | |
| 613 | bool FLAC__stream_encoder_set_min_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value) |
| 614 | { |
| 615 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 616 | return false; |
| 617 | encoder->protected->min_residual_partition_order = value; |
| 618 | return true; |
| 619 | } |
| 620 | |
| 621 | bool FLAC__stream_encoder_set_max_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value) |
| 622 | { |
| 623 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 624 | return false; |
| 625 | encoder->protected->max_residual_partition_order = value; |
| 626 | return true; |
| 627 | } |
| 628 | |
| 629 | bool FLAC__stream_encoder_set_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder, unsigned value) |
| 630 | { |
| 631 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 632 | return false; |
| 633 | encoder->protected->rice_parameter_search_dist = value; |
| 634 | return true; |
| 635 | } |
| 636 | |
| 637 | bool FLAC__stream_encoder_set_total_samples_estimate(const FLAC__StreamEncoder *encoder, uint64 value) |
| 638 | { |
| 639 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 640 | return false; |
| 641 | encoder->protected->total_samples_estimate = value; |
| 642 | return true; |
| 643 | } |
| 644 | |
| 645 | bool FLAC__stream_encoder_set_seek_table(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData_SeekTable *value) |
| 646 | { |
| 647 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 648 | return false; |
| 649 | encoder->protected->seek_table = value; |
| 650 | return true; |
| 651 | } |
| 652 | |
| 653 | bool FLAC__stream_encoder_set_padding(const FLAC__StreamEncoder *encoder, unsigned value) |
| 654 | { |
| 655 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 656 | return false; |
| 657 | encoder->protected->padding = value; |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | bool FLAC__stream_encoder_set_last_metadata_is_last(const FLAC__StreamEncoder *encoder, bool value) |
| 662 | { |
| 663 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 664 | return false; |
| 665 | encoder->protected->last_metadata_is_last = value; |
| 666 | return true; |
| 667 | } |
| 668 | |
| 669 | bool FLAC__stream_encoder_set_write_callback(const FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteStatus (*value)(const FLAC__StreamEncoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)) |
| 670 | { |
| 671 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 672 | return false; |
| 673 | encoder->private->write_callback = value; |
| 674 | return true; |
| 675 | } |
| 676 | |
| 677 | bool FLAC__stream_encoder_set_metadata_callback(const FLAC__StreamEncoder *encoder, void (*value)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)) |
| 678 | { |
| 679 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 680 | return false; |
| 681 | encoder->private->metadata_callback = value; |
| 682 | return true; |
| 683 | } |
| 684 | |
| 685 | bool FLAC__stream_encoder_set_client_data(const FLAC__StreamEncoder *encoder, void *value) |
| 686 | { |
| 687 | if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED) |
| 688 | return false; |
| 689 | encoder->private->client_data = value; |
| 690 | return true; |
| 691 | } |
| 692 | |
| 693 | FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 694 | { |
| 695 | return encoder->protected->state; |
| 696 | } |
| 697 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 698 | bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 699 | { |
| 700 | return encoder->protected->streamable_subset; |
| 701 | } |
| 702 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 703 | 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] | 704 | { |
| 705 | return encoder->protected->do_mid_side_stereo; |
| 706 | } |
| 707 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 708 | 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] | 709 | { |
| 710 | return encoder->protected->loose_mid_side_stereo; |
| 711 | } |
| 712 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 713 | unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 714 | { |
| 715 | return encoder->protected->channels; |
| 716 | } |
| 717 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 718 | unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 719 | { |
| 720 | return encoder->protected->bits_per_sample; |
| 721 | } |
| 722 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 723 | unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 724 | { |
| 725 | return encoder->protected->sample_rate; |
| 726 | } |
| 727 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 728 | unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 729 | { |
| 730 | return encoder->protected->blocksize; |
| 731 | } |
| 732 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 733 | unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 734 | { |
| 735 | return encoder->protected->max_lpc_order; |
| 736 | } |
| 737 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 738 | unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 739 | { |
| 740 | return encoder->protected->qlp_coeff_precision; |
| 741 | } |
| 742 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 743 | 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] | 744 | { |
| 745 | return encoder->protected->do_qlp_coeff_prec_search; |
| 746 | } |
| 747 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 748 | 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] | 749 | { |
| 750 | return encoder->protected->do_exhaustive_model_search; |
| 751 | } |
| 752 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 753 | 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] | 754 | { |
| 755 | return encoder->protected->min_residual_partition_order; |
| 756 | } |
| 757 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 758 | 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] | 759 | { |
| 760 | return encoder->protected->max_residual_partition_order; |
| 761 | } |
| 762 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame^] | 763 | 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] | 764 | { |
| 765 | return encoder->protected->rice_parameter_search_dist; |
| 766 | } |
| 767 | |
| 768 | bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const int32 *buf[], unsigned samples) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 769 | { |
| 770 | unsigned i, j, channel; |
| 771 | int32 x, mid, side; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 772 | const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 773 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 774 | FLAC__ASSERT(encoder != 0); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 775 | FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 776 | |
| 777 | j = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 778 | if(encoder->protected->do_mid_side_stereo && channels == 2) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 779 | do { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 780 | 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] | 781 | x = mid = side = buf[0][j]; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 782 | encoder->private->integer_signal[0][i] = x; |
| 783 | encoder->private->real_signal[0][i] = (real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 784 | x = buf[1][j]; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 785 | encoder->private->integer_signal[1][i] = x; |
| 786 | encoder->private->real_signal[1][i] = (real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 787 | mid += x; |
| 788 | side -= x; |
| 789 | mid >>= 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 790 | encoder->private->integer_signal_mid_side[1][i] = side; |
| 791 | encoder->private->integer_signal_mid_side[0][i] = mid; |
| 792 | encoder->private->real_signal_mid_side[1][i] = (real)side; |
| 793 | encoder->private->real_signal_mid_side[0][i] = (real)mid; |
| 794 | encoder->private->current_sample_number++; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 795 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 796 | if(i == blocksize) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 797 | if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 798 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 799 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 800 | } while(j < samples); |
| 801 | } |
| 802 | else { |
| 803 | do { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 804 | 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] | 805 | for(channel = 0; channel < channels; channel++) { |
| 806 | x = buf[channel][j]; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 807 | encoder->private->integer_signal[channel][i] = x; |
| 808 | encoder->private->real_signal[channel][i] = (real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 809 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 810 | encoder->private->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 811 | } |
| 812 | if(i == blocksize) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 813 | if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 814 | return false; |
| 815 | } |
| 816 | } while(j < samples); |
| 817 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 818 | |
| 819 | return true; |
| 820 | } |
| 821 | |
| 822 | /* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 823 | bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const int32 buf[], unsigned samples) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 824 | { |
| 825 | unsigned i, j, k, channel; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 826 | int32 x, mid, side; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 827 | const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 828 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 829 | FLAC__ASSERT(encoder != 0); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 830 | FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 831 | |
| 832 | j = k = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 833 | if(encoder->protected->do_mid_side_stereo && channels == 2) { |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 834 | do { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 835 | 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] | 836 | x = mid = side = buf[k++]; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 837 | encoder->private->integer_signal[0][i] = x; |
| 838 | encoder->private->real_signal[0][i] = (real)x; |
Josh Coalson | d37d135 | 2001-05-30 23:09:31 +0000 | [diff] [blame] | 839 | x = buf[k++]; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 840 | encoder->private->integer_signal[1][i] = x; |
| 841 | encoder->private->real_signal[1][i] = (real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 842 | mid += x; |
| 843 | side -= x; |
| 844 | mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 845 | encoder->private->integer_signal_mid_side[1][i] = side; |
| 846 | encoder->private->integer_signal_mid_side[0][i] = mid; |
| 847 | encoder->private->real_signal_mid_side[1][i] = (real)side; |
| 848 | encoder->private->real_signal_mid_side[0][i] = (real)mid; |
| 849 | encoder->private->current_sample_number++; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 850 | } |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 851 | if(i == blocksize) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 852 | if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 853 | return false; |
| 854 | } |
| 855 | } while(j < samples); |
| 856 | } |
| 857 | else { |
| 858 | do { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 859 | 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] | 860 | for(channel = 0; channel < channels; channel++) { |
| 861 | x = buf[k++]; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 862 | encoder->private->integer_signal[channel][i] = x; |
| 863 | encoder->private->real_signal[channel][i] = (real)x; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 864 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 865 | encoder->private->current_sample_number++; |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 866 | } |
| 867 | if(i == blocksize) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 868 | if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */ |
Josh Coalson | aa25536 | 2001-05-31 06:17:41 +0000 | [diff] [blame] | 869 | return false; |
| 870 | } |
| 871 | } while(j < samples); |
| 872 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 873 | |
| 874 | return true; |
| 875 | } |
| 876 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 877 | bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 878 | { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 879 | bool ok; |
| 880 | unsigned i, channel; |
| 881 | |
| 882 | FLAC__ASSERT(new_size > 0); |
| 883 | FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK); |
| 884 | FLAC__ASSERT(encoder->private->current_sample_number == 0); |
| 885 | |
| 886 | /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */ |
| 887 | if(new_size <= encoder->private->input_capacity) |
| 888 | return true; |
| 889 | |
| 890 | ok = true; |
| 891 | for(i = 0; ok && i < encoder->protected->channels; i++) { |
| 892 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->integer_signal_unaligned[i], &encoder->private->integer_signal[i]); |
| 893 | ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private->real_signal_unaligned[i], &encoder->private->real_signal[i]); |
| 894 | } |
| 895 | for(i = 0; ok && i < 2; i++) { |
| 896 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->integer_signal_mid_side_unaligned[i], &encoder->private->integer_signal_mid_side[i]); |
| 897 | 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]); |
| 898 | } |
| 899 | for(channel = 0; ok && channel < encoder->protected->channels; channel++) { |
| 900 | for(i = 0; ok && i < 2; i++) { |
| 901 | ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->residual_workspace_unaligned[channel][i], &encoder->private->residual_workspace[channel][i]); |
| 902 | } |
| 903 | } |
| 904 | for(channel = 0; ok && channel < 2; channel++) { |
| 905 | for(i = 0; ok && i < 2; i++) { |
| 906 | 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]); |
| 907 | } |
| 908 | } |
| 909 | ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private->abs_residual_unaligned, &encoder->private->abs_residual); |
| 910 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
| 911 | ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size * 2, &encoder->private->abs_residual_partition_sums_unaligned, &encoder->private->abs_residual_partition_sums); |
| 912 | #endif |
| 913 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
| 914 | ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private->raw_bits_per_partition_unaligned, &encoder->private->raw_bits_per_partition); |
| 915 | #endif |
| 916 | |
| 917 | if(ok) |
| 918 | encoder->private->input_capacity = new_size; |
| 919 | else |
| 920 | encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
| 921 | |
| 922 | return ok; |
| 923 | } |
| 924 | |
| 925 | /*********************************************************************** |
| 926 | * |
| 927 | * Private class methods |
| 928 | * |
| 929 | ***********************************************************************/ |
| 930 | |
| 931 | bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, bool is_last_frame) |
| 932 | { |
| 933 | FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 934 | |
| 935 | /* |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 936 | * Accumulate raw signal to the MD5 signature |
| 937 | */ |
Josh Coalson | eae4dde | 2001-04-16 05:33:22 +0000 | [diff] [blame] | 938 | /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 939 | if(!FLAC__MD5Accumulate(&encoder->private->md5context, encoder->private->integer_signal, encoder->protected->channels, encoder->protected->blocksize, (encoder->protected->bits_per_sample+7) / 8)) { |
| 940 | encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 941 | return false; |
| 942 | } |
| 943 | |
| 944 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 945 | * Process the frame header and subframes into the frame bitbuffer |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 946 | */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 947 | if(!stream_encoder_process_subframes_(encoder, is_last_frame)) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 948 | /* 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] | 949 | return false; |
| 950 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 951 | |
| 952 | /* |
| 953 | * Zero-pad the frame to a byte_boundary |
| 954 | */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 955 | if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->private->frame)) { |
| 956 | encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 957 | return false; |
| 958 | } |
| 959 | |
| 960 | /* |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 961 | * CRC-16 the whole thing |
| 962 | */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 963 | FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned */ |
| 964 | FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */ |
| 965 | FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__crc16(encoder->private->frame.buffer, encoder->private->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN); |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 966 | |
| 967 | /* |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 968 | * Write it |
| 969 | */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 970 | if(encoder->private->write_callback(encoder, encoder->private->frame.buffer, encoder->private->frame.bytes, encoder->protected->blocksize, encoder->private->current_frame_number, encoder->private->client_data) != FLAC__STREAM_ENCODER_WRITE_OK) { |
| 971 | encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 972 | return false; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Get ready for the next frame |
| 977 | */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 978 | encoder->private->current_sample_number = 0; |
| 979 | encoder->private->current_frame_number++; |
| 980 | encoder->private->metadata.data.stream_info.total_samples += (uint64)encoder->protected->blocksize; |
| 981 | encoder->private->metadata.data.stream_info.min_framesize = min(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.min_framesize); |
| 982 | encoder->private->metadata.data.stream_info.max_framesize = max(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.max_framesize); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 983 | |
| 984 | return true; |
| 985 | } |
| 986 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 987 | bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, bool is_last_frame) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 988 | { |
| 989 | FLAC__FrameHeader frame_header; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 990 | unsigned channel, min_partition_order = encoder->protected->min_residual_partition_order, max_partition_order; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 991 | bool do_independent, do_mid_side; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 992 | |
| 993 | /* |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 994 | * Calculate the min,max Rice partition orders |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 995 | */ |
| 996 | if(is_last_frame) { |
| 997 | max_partition_order = 0; |
| 998 | } |
| 999 | else { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1000 | unsigned limit = 0, b = encoder->protected->blocksize; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1001 | while(!(b & 1)) { |
| 1002 | limit++; |
| 1003 | b >>= 1; |
| 1004 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1005 | max_partition_order = min(encoder->protected->max_residual_partition_order, limit); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1006 | } |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1007 | min_partition_order = min(min_partition_order, max_partition_order); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1008 | |
| 1009 | /* |
| 1010 | * Setup the frame |
| 1011 | */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1012 | if(!FLAC__bitbuffer_clear(&encoder->private->frame)) { |
| 1013 | encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1014 | return false; |
| 1015 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1016 | frame_header.blocksize = encoder->protected->blocksize; |
| 1017 | frame_header.sample_rate = encoder->protected->sample_rate; |
| 1018 | frame_header.channels = encoder->protected->channels; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1019 | frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1020 | frame_header.bits_per_sample = encoder->protected->bits_per_sample; |
| 1021 | frame_header.number.frame_number = encoder->private->current_frame_number; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1022 | |
| 1023 | /* |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1024 | * Figure out what channel assignments to try |
| 1025 | */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1026 | if(encoder->protected->do_mid_side_stereo) { |
| 1027 | if(encoder->protected->loose_mid_side_stereo) { |
| 1028 | if(encoder->private->loose_mid_side_stereo_frame_count == 0) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1029 | do_independent = true; |
| 1030 | do_mid_side = true; |
| 1031 | } |
| 1032 | else { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1033 | do_independent = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1034 | do_mid_side = !do_independent; |
| 1035 | } |
| 1036 | } |
| 1037 | else { |
| 1038 | do_independent = true; |
| 1039 | do_mid_side = true; |
| 1040 | } |
| 1041 | } |
| 1042 | else { |
| 1043 | do_independent = true; |
| 1044 | do_mid_side = false; |
| 1045 | } |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1046 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1047 | FLAC__ASSERT(do_independent || do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1048 | |
| 1049 | /* |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1050 | * Check for wasted bits; set effective bps for each subframe |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1051 | */ |
| 1052 | if(do_independent) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1053 | unsigned w; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1054 | for(channel = 0; channel < encoder->protected->channels; channel++) { |
| 1055 | w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal[channel], encoder->protected->blocksize); |
| 1056 | encoder->private->subframe_workspace[channel][0].wasted_bits = encoder->private->subframe_workspace[channel][1].wasted_bits = w; |
| 1057 | encoder->private->subframe_bps[channel] = encoder->protected->bits_per_sample - w; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1058 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1059 | } |
| 1060 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1061 | unsigned w; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1062 | FLAC__ASSERT(encoder->protected->channels == 2); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1063 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1064 | w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal_mid_side[channel], encoder->protected->blocksize); |
| 1065 | encoder->private->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private->subframe_workspace_mid_side[channel][1].wasted_bits = w; |
| 1066 | 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] | 1067 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | /* |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1071 | * First do a normal encoding pass of each independent channel |
| 1072 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1073 | if(do_independent) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1074 | for(channel = 0; channel < encoder->protected->channels; channel++) { |
| 1075 | if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, 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] | 1076 | return false; |
| 1077 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | /* |
| 1081 | * Now do mid and side channels if requested |
| 1082 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1083 | if(do_mid_side) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1084 | FLAC__ASSERT(encoder->protected->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1085 | |
| 1086 | for(channel = 0; channel < 2; channel++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1087 | if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, 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] | 1088 | return false; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | * Compose the frame bitbuffer |
| 1094 | */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1095 | if(do_mid_side) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1096 | unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */ |
| 1097 | 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] | 1098 | FLAC__ChannelAssignment channel_assignment; |
| 1099 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1100 | FLAC__ASSERT(encoder->protected->channels == 2); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1101 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1102 | if(encoder->protected->loose_mid_side_stereo && encoder->private->loose_mid_side_stereo_frame_count > 0) { |
| 1103 | 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] | 1104 | } |
| 1105 | else { |
| 1106 | unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */ |
| 1107 | unsigned min_bits; |
| 1108 | FLAC__ChannelAssignment ca; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1109 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1110 | FLAC__ASSERT(do_independent && do_mid_side); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1111 | |
| 1112 | /* We have to figure out which channel assignent results in the smallest frame */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1113 | bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits [1]; |
| 1114 | bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits_mid_side[1]; |
| 1115 | bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private->best_subframe_bits [1] + encoder->private->best_subframe_bits_mid_side[1]; |
| 1116 | 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] | 1117 | |
| 1118 | for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) { |
| 1119 | if(bits[ca] < min_bits) { |
| 1120 | min_bits = bits[ca]; |
| 1121 | channel_assignment = ca; |
| 1122 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1126 | frame_header.channel_assignment = channel_assignment; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1127 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1128 | if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) { |
| 1129 | encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1130 | return false; |
| 1131 | } |
| 1132 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1133 | switch(channel_assignment) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1134 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1135 | left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]]; |
| 1136 | right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1137 | break; |
| 1138 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1139 | left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]]; |
| 1140 | 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] | 1141 | break; |
| 1142 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1143 | left_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]]; |
| 1144 | right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1145 | break; |
| 1146 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1147 | left_subframe = &encoder->private->subframe_workspace_mid_side[0][encoder->private->best_subframe_mid_side[0]]; |
| 1148 | 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] | 1149 | break; |
| 1150 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1151 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1152 | } |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1153 | |
| 1154 | switch(channel_assignment) { |
| 1155 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1156 | left_bps = encoder->private->subframe_bps [0]; |
| 1157 | right_bps = encoder->private->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1158 | break; |
| 1159 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1160 | left_bps = encoder->private->subframe_bps [0]; |
| 1161 | right_bps = encoder->private->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1162 | break; |
| 1163 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1164 | left_bps = encoder->private->subframe_bps_mid_side[1]; |
| 1165 | right_bps = encoder->private->subframe_bps [1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1166 | break; |
| 1167 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1168 | left_bps = encoder->private->subframe_bps_mid_side[0]; |
| 1169 | right_bps = encoder->private->subframe_bps_mid_side[1]; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1170 | break; |
| 1171 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1172 | FLAC__ASSERT(0); |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /* note that encoder_add_subframe_ sets the state for us in case of an error */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1176 | if(!stream_encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->private->frame)) |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1177 | return false; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1178 | if(!stream_encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->private->frame)) |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1179 | return false; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1180 | } |
| 1181 | else { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1182 | if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) { |
| 1183 | encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1184 | return false; |
| 1185 | } |
| 1186 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1187 | for(channel = 0; channel < encoder->protected->channels; channel++) { |
| 1188 | if(!stream_encoder_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] | 1189 | /* the above function sets the state for us in case of an error */ |
| 1190 | return false; |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1195 | if(encoder->protected->loose_mid_side_stereo) { |
| 1196 | encoder->private->loose_mid_side_stereo_frame_count++; |
| 1197 | if(encoder->private->loose_mid_side_stereo_frame_count >= encoder->private->loose_mid_side_stereo_frames) |
| 1198 | encoder->private->loose_mid_side_stereo_frame_count = 0; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1201 | encoder->private->last_channel_assignment = frame_header.channel_assignment; |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1202 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1203 | return true; |
| 1204 | } |
| 1205 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1206 | bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const int32 integer_signal[], const real real_signal[], FLAC__Subframe *subframe[2], int32 *residual[2], unsigned *best_subframe, unsigned *best_bits) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1207 | { |
| 1208 | real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]; |
| 1209 | real lpc_residual_bits_per_sample; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1210 | 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 | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1211 | real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; |
| 1212 | real lpc_error[FLAC__MAX_LPC_ORDER]; |
| 1213 | unsigned min_lpc_order, max_lpc_order, lpc_order; |
| 1214 | unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order; |
| 1215 | unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision; |
| 1216 | unsigned rice_parameter; |
| 1217 | unsigned _candidate_bits, _best_bits; |
| 1218 | unsigned _best_subframe; |
| 1219 | |
| 1220 | /* verbatim subframe is the baseline against which we measure other compressed subframes */ |
| 1221 | _best_subframe = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1222 | _best_bits = stream_encoder_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] | 1223 | |
| 1224 | if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) { |
| 1225 | /* check for constant subframe */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1226 | if(encoder->private->use_slow) |
Josh Coalson | eef5670 | 2001-03-30 00:45:22 +0000 | [diff] [blame] | 1227 | guess_fixed_order = FLAC__fixed_compute_best_predictor_slow(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample); |
| 1228 | else |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1229 | 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] | 1230 | if(fixed_residual_bits_per_sample[1] == 0.0) { |
| 1231 | /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */ |
| 1232 | unsigned i, signal_is_constant = true; |
| 1233 | for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) { |
| 1234 | if(integer_signal[0] != integer_signal[i]) { |
| 1235 | signal_is_constant = false; |
| 1236 | break; |
| 1237 | } |
| 1238 | } |
| 1239 | if(signal_is_constant) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1240 | _candidate_bits = stream_encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1241 | if(_candidate_bits < _best_bits) { |
| 1242 | _best_subframe = !_best_subframe; |
| 1243 | _best_bits = _candidate_bits; |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | else { |
| 1248 | /* encode fixed */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1249 | if(encoder->protected->do_exhaustive_model_search) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1250 | min_fixed_order = 0; |
| 1251 | max_fixed_order = FLAC__MAX_FIXED_ORDER; |
| 1252 | } |
| 1253 | else { |
| 1254 | min_fixed_order = max_fixed_order = guess_fixed_order; |
| 1255 | } |
| 1256 | for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1257 | if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1258 | continue; /* don't even try */ |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 1259 | 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] | 1260 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | 46f2ae8 | 2001-02-08 00:27:21 +0000 | [diff] [blame] | 1261 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1262 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1263 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) |
| 1264 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1265 | _candidate_bits = stream_encoder_evaluate_fixed_subframe_(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, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1266 | if(_candidate_bits < _best_bits) { |
| 1267 | _best_subframe = !_best_subframe; |
| 1268 | _best_bits = _candidate_bits; |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | /* encode lpc */ |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1273 | if(encoder->protected->max_lpc_order > 0) { |
| 1274 | if(encoder->protected->max_lpc_order >= frame_header->blocksize) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1275 | max_lpc_order = frame_header->blocksize-1; |
| 1276 | else |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1277 | max_lpc_order = encoder->protected->max_lpc_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1278 | if(max_lpc_order > 0) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1279 | 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] | 1280 | /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */ |
| 1281 | if(autoc[0] != 0.0) { |
| 1282 | FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1283 | if(encoder->protected->do_exhaustive_model_search) { |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1284 | min_lpc_order = 1; |
| 1285 | } |
| 1286 | else { |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1287 | 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] | 1288 | min_lpc_order = max_lpc_order = guess_lpc_order; |
| 1289 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1290 | if(encoder->protected->do_qlp_coeff_prec_search) { |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1291 | min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION; |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1292 | max_qlp_coeff_precision = min(32 - subframe_bps - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1293 | } |
| 1294 | else { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1295 | 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] | 1296 | } |
| 1297 | for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) { |
| 1298 | 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 | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1299 | if(lpc_residual_bits_per_sample >= (real)subframe_bps) |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1300 | continue; /* don't even try */ |
| 1301 | 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] | 1302 | #ifndef FLAC__SYMMETRIC_RICE |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1303 | rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */ |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1304 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1305 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) |
| 1306 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1307 | for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1308 | _candidate_bits = stream_encoder_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, lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, min_partition_order, max_partition_order, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]); |
Josh Coalson | f4ce50b | 2001-02-28 23:45:15 +0000 | [diff] [blame] | 1309 | if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */ |
| 1310 | if(_candidate_bits < _best_bits) { |
| 1311 | _best_subframe = !_best_subframe; |
| 1312 | _best_bits = _candidate_bits; |
| 1313 | } |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | *best_subframe = _best_subframe; |
| 1324 | *best_bits = _best_bits; |
| 1325 | |
| 1326 | return true; |
| 1327 | } |
| 1328 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1329 | bool stream_encoder_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] | 1330 | { |
| 1331 | switch(subframe->type) { |
| 1332 | case FLAC__SUBFRAME_TYPE_CONSTANT: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1333 | if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1334 | encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1335 | return false; |
| 1336 | } |
| 1337 | break; |
| 1338 | case FLAC__SUBFRAME_TYPE_FIXED: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1339 | if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1340 | encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1341 | return false; |
| 1342 | } |
| 1343 | break; |
| 1344 | case FLAC__SUBFRAME_TYPE_LPC: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1345 | if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1346 | encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1347 | return false; |
| 1348 | } |
| 1349 | break; |
| 1350 | case FLAC__SUBFRAME_TYPE_VERBATIM: |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1351 | if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1352 | encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1353 | return false; |
| 1354 | } |
| 1355 | break; |
| 1356 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1357 | FLAC__ASSERT(0); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | return true; |
| 1361 | } |
| 1362 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1363 | unsigned stream_encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1364 | { |
| 1365 | subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT; |
| 1366 | subframe->data.constant.value = signal; |
| 1367 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1368 | 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] | 1369 | } |
| 1370 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1371 | unsigned stream_encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1372 | { |
| 1373 | unsigned i, residual_bits; |
| 1374 | const unsigned residual_samples = blocksize - order; |
| 1375 | |
| 1376 | FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual); |
| 1377 | |
| 1378 | subframe->type = FLAC__SUBFRAME_TYPE_FIXED; |
| 1379 | |
| 1380 | subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1381 | subframe->data.fixed.residual = residual; |
| 1382 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1383 | residual_bits = stream_encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, 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] | 1384 | |
| 1385 | subframe->data.fixed.order = order; |
| 1386 | for(i = 0; i < order; i++) |
| 1387 | subframe->data.fixed.warmup[i] = signal[i]; |
| 1388 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1389 | 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] | 1390 | } |
| 1391 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1392 | unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const 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, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1393 | { |
| 1394 | int32 qlp_coeff[FLAC__MAX_LPC_ORDER]; |
| 1395 | unsigned i, residual_bits; |
| 1396 | int quantization, ret; |
| 1397 | const unsigned residual_samples = blocksize - order; |
| 1398 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1399 | 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] | 1400 | if(ret != 0) |
| 1401 | return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */ |
| 1402 | |
Josh Coalson | 92d4240 | 2001-05-31 20:53:19 +0000 | [diff] [blame] | 1403 | if(subframe_bps <= 16 && qlp_coeff_precision <= 16) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1404 | 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] | 1405 | else |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1406 | 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] | 1407 | |
| 1408 | subframe->type = FLAC__SUBFRAME_TYPE_LPC; |
| 1409 | |
| 1410 | subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE; |
| 1411 | subframe->data.lpc.residual = residual; |
| 1412 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1413 | residual_bits = stream_encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, 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] | 1414 | |
| 1415 | subframe->data.lpc.order = order; |
| 1416 | subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision; |
| 1417 | subframe->data.lpc.quantization_level = quantization; |
| 1418 | memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER); |
| 1419 | for(i = 0; i < order; i++) |
| 1420 | subframe->data.lpc.warmup[i] = signal[i]; |
| 1421 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1422 | 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] | 1423 | } |
| 1424 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1425 | unsigned stream_encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1426 | { |
| 1427 | subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM; |
| 1428 | |
| 1429 | subframe->data.verbatim.data = signal; |
| 1430 | |
Josh Coalson | 82b7324 | 2001-03-28 22:17:05 +0000 | [diff] [blame] | 1431 | 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] | 1432 | } |
| 1433 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1434 | unsigned stream_encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 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, 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] | 1435 | { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1436 | int32 r; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1437 | #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES) |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1438 | unsigned sum; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1439 | int partition_order; |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1440 | #else |
| 1441 | unsigned partition_order; |
| 1442 | #endif |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1443 | unsigned residual_bits, best_residual_bits = 0; |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1444 | unsigned residual_sample; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1445 | unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER]; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1446 | |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1447 | /* compute abs(residual) for use later */ |
| 1448 | for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) { |
| 1449 | r = residual[residual_sample]; |
| 1450 | abs_residual[residual_sample] = (uint32)(r<0? -r : r); |
| 1451 | } |
| 1452 | |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1453 | #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1454 | max_partition_order = stream_encoder_precompute_partition_info_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order); |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1455 | min_partition_order = min(min_partition_order, max_partition_order); |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1456 | |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1457 | for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1458 | if(!stream_encoder_set_partitioned_rice_(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, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) { |
| 1459 | FLAC__ASSERT(0); /* stream_encoder_precompute_partition_info_ should keep this from ever happening */ |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1460 | } |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1461 | sum += 1u << partition_order; |
| 1462 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 1463 | best_residual_bits = residual_bits; |
| 1464 | *best_partition_order = partition_order; |
| 1465 | best_parameters_index = !best_parameters_index; |
| 1466 | } |
| 1467 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1468 | #else |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1469 | for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1470 | if(!stream_encoder_set_partitioned_rice_(abs_residual, 0, 0, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) { |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1471 | FLAC__ASSERT(best_residual_bits != 0); |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1472 | break; |
| 1473 | } |
| 1474 | if(best_residual_bits == 0 || residual_bits < best_residual_bits) { |
| 1475 | best_residual_bits = residual_bits; |
| 1476 | *best_partition_order = partition_order; |
| 1477 | best_parameters_index = !best_parameters_index; |
| 1478 | } |
| 1479 | } |
| 1480 | #endif |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1481 | memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order))); |
| 1482 | memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order))); |
| 1483 | |
| 1484 | return best_residual_bits; |
| 1485 | } |
| 1486 | |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1487 | #if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1488 | unsigned stream_encoder_precompute_partition_info_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], 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] | 1489 | { |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1490 | int partition_order; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1491 | unsigned from_partition, to_partition = 0; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1492 | const unsigned blocksize = residual_samples + predictor_order; |
| 1493 | |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1494 | /* first do max_partition_order */ |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1495 | for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) { |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1496 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1497 | uint32 abs_residual_partition_sum; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1498 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1499 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1500 | uint32 abs_residual_partition_max; |
| 1501 | unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */ |
| 1502 | #endif |
| 1503 | uint32 abs_r; |
| 1504 | unsigned partition, partition_sample, partition_samples, residual_sample; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1505 | const unsigned partitions = 1u << partition_order; |
| 1506 | const unsigned default_partition_samples = blocksize >> partition_order; |
| 1507 | |
| 1508 | if(default_partition_samples <= predictor_order) { |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1509 | FLAC__ASSERT(max_partition_order > 0); |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1510 | max_partition_order--; |
| 1511 | } |
| 1512 | else { |
| 1513 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
| 1514 | partition_samples = default_partition_samples; |
| 1515 | if(partition == 0) |
| 1516 | partition_samples -= predictor_order; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1517 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1518 | abs_residual_partition_sum = 0; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1519 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1520 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1521 | abs_residual_partition_max = 0; |
| 1522 | #endif |
| 1523 | for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) { |
| 1524 | abs_r = abs_residual[residual_sample]; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1525 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1526 | abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */ |
| 1527 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1528 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1529 | if(abs_r > abs_residual_partition_max) { |
| 1530 | abs_residual_partition_max = abs_r; |
| 1531 | abs_residual_partition_max_index = residual_sample; |
| 1532 | } |
| 1533 | #endif |
| 1534 | residual_sample++; |
| 1535 | } |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1536 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1537 | abs_residual_partition_sums[partition] = abs_residual_partition_sum; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1538 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1539 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1540 | if(abs_residual_partition_max > 0) |
| 1541 | raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]); |
| 1542 | else |
| 1543 | raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0); |
| 1544 | #endif |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1545 | } |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1546 | to_partition = partitions; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1547 | break; |
| 1548 | } |
| 1549 | } |
Josh Coalson | f76a361 | 2001-04-18 02:28:11 +0000 | [diff] [blame] | 1550 | |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1551 | /* now merge for lower orders */ |
Josh Coalson | 6bd1757 | 2001-05-25 19:02:01 +0000 | [diff] [blame] | 1552 | for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) { |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1553 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1554 | uint32 s; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1555 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1556 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1557 | unsigned m; |
| 1558 | #endif |
| 1559 | unsigned i; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1560 | const unsigned partitions = 1u << partition_order; |
| 1561 | for(i = 0; i < partitions; i++) { |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1562 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1563 | s = abs_residual_partition_sums[from_partition]; |
| 1564 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1565 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1566 | m = raw_bits_per_partition[from_partition]; |
| 1567 | #endif |
| 1568 | from_partition++; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1569 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1570 | abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition]; |
| 1571 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1572 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1573 | raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]); |
| 1574 | #endif |
| 1575 | from_partition++; |
| 1576 | to_partition++; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1577 | } |
| 1578 | } |
| 1579 | |
Josh Coalson | f76a361 | 2001-04-18 02:28:11 +0000 | [diff] [blame] | 1580 | return max_partition_order; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1581 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1582 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1583 | |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1584 | #ifdef VARIABLE_RICE_BITS |
| 1585 | #undef VARIABLE_RICE_BITS |
| 1586 | #endif |
| 1587 | #define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter)) |
| 1588 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1589 | bool stream_encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 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, unsigned parameters[], unsigned raw_bits[], unsigned *bits) |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1590 | { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1591 | unsigned rice_parameter, partition_bits; |
| 1592 | #ifndef NO_RICE_SEARCH |
| 1593 | unsigned best_partition_bits; |
| 1594 | unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0; |
| 1595 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1596 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1597 | unsigned flat_bits; |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1598 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1599 | unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN; |
| 1600 | |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1601 | 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] | 1602 | |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1603 | if(partition_order == 0) { |
| 1604 | unsigned i; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1605 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1606 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1607 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1608 | if(suggested_rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1609 | min_rice_parameter = 0; |
| 1610 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1611 | min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist; |
| 1612 | max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1613 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) |
| 1614 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 1615 | } |
| 1616 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1617 | min_rice_parameter = max_rice_parameter = suggested_rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1618 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1619 | best_partition_bits = 0xffffffff; |
| 1620 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 1621 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1622 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1623 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1624 | partition_bits = (2+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1625 | #else |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1626 | const unsigned rice_parameter_estimate = rice_parameter-1; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1627 | partition_bits = (1+rice_parameter) * residual_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1628 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1629 | #else |
| 1630 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1631 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1632 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1633 | for(i = 0; i < residual_samples; i++) { |
| 1634 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1635 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1636 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1637 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1638 | partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1639 | #endif |
| 1640 | #else |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1641 | 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] | 1642 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1643 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1644 | #ifndef NO_RICE_SEARCH |
| 1645 | if(partition_bits < best_partition_bits) { |
| 1646 | best_rice_parameter = rice_parameter; |
| 1647 | best_partition_bits = partition_bits; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1648 | } |
| 1649 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1650 | #endif |
| 1651 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
| 1652 | flat_bits = raw_bits_per_partition[0] * residual_samples; |
| 1653 | if(flat_bits <= best_partition_bits) { |
| 1654 | raw_bits[0] = raw_bits_per_partition[0]; |
| 1655 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 1656 | best_partition_bits = flat_bits; |
| 1657 | } |
| 1658 | #endif |
| 1659 | parameters[0] = best_rice_parameter; |
| 1660 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1661 | } |
| 1662 | else { |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 1663 | unsigned partition, residual_sample, save_residual_sample, partition_sample; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1664 | unsigned mean, partition_samples; |
Josh Coalson | d4e0ddb | 2001-04-18 02:20:52 +0000 | [diff] [blame] | 1665 | const unsigned partitions = 1u << partition_order; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 1666 | for(partition = residual_sample = 0; partition < partitions; partition++) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1667 | partition_samples = (residual_samples+predictor_order) >> partition_order; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1668 | if(partition == 0) { |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1669 | if(partition_samples <= predictor_order) |
| 1670 | return false; |
| 1671 | else |
| 1672 | partition_samples -= predictor_order; |
| 1673 | } |
| 1674 | mean = partition_samples >> 1; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1675 | #ifdef FLAC__PRECOMPUTE_PARTITION_SUMS |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1676 | mean += abs_residual_partition_sums[partition]; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1677 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 1678 | save_residual_sample = residual_sample; |
| 1679 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) |
| 1680 | mean += abs_residual[residual_sample]; |
| 1681 | residual_sample = save_residual_sample; |
Josh Coalson | aef013c | 2001-04-24 01:25:42 +0000 | [diff] [blame] | 1682 | #endif |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1683 | mean /= partition_samples; |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1684 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1685 | /* calc rice_parameter = floor(log2(mean)) */ |
| 1686 | rice_parameter = 0; |
| 1687 | mean>>=1; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1688 | while(mean) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1689 | rice_parameter++; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1690 | mean >>= 1; |
| 1691 | } |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1692 | #else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1693 | /* calc rice_parameter = floor(log2(mean)) + 1 */ |
| 1694 | rice_parameter = 0; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1695 | while(mean) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1696 | rice_parameter++; |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1697 | mean >>= 1; |
| 1698 | } |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1699 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1700 | if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) |
| 1701 | rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1702 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1703 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1704 | if(rice_parameter_search_dist) { |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1705 | if(rice_parameter < rice_parameter_search_dist) |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1706 | min_rice_parameter = 0; |
| 1707 | else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1708 | min_rice_parameter = rice_parameter - rice_parameter_search_dist; |
| 1709 | max_rice_parameter = rice_parameter + rice_parameter_search_dist; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1710 | if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) |
| 1711 | max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1; |
| 1712 | } |
| 1713 | else |
| 1714 | min_rice_parameter = max_rice_parameter = rice_parameter; |
Josh Coalson | 60f77d7 | 2001-04-25 02:16:36 +0000 | [diff] [blame] | 1715 | |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1716 | best_partition_bits = 0xffffffff; |
| 1717 | for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) { |
| 1718 | #endif |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1719 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1720 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1721 | partition_bits = (2+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1722 | #else |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1723 | const unsigned rice_parameter_estimate = rice_parameter-1; |
| 1724 | partition_bits = (1+rice_parameter) * partition_samples; |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1725 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1726 | #else |
| 1727 | partition_bits = 0; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1728 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1729 | partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN; |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 1730 | save_residual_sample = residual_sample; |
| 1731 | for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) { |
Josh Coalson | 352e0f6 | 2001-03-20 22:55:50 +0000 | [diff] [blame] | 1732 | #ifdef VARIABLE_RICE_BITS |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1733 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 1734 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter); |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1735 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 1736 | partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate); |
Josh Coalson | b9433f9 | 2001-03-17 01:07:00 +0000 | [diff] [blame] | 1737 | #endif |
| 1738 | #else |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 1739 | 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] | 1740 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1741 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1742 | #ifndef NO_RICE_SEARCH |
Josh Coalson | 4dacd19 | 2001-06-06 21:11:44 +0000 | [diff] [blame] | 1743 | if(rice_parameter != max_rice_parameter) |
| 1744 | residual_sample = save_residual_sample; |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1745 | if(partition_bits < best_partition_bits) { |
| 1746 | best_rice_parameter = rice_parameter; |
| 1747 | best_partition_bits = partition_bits; |
| 1748 | } |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1749 | } |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1750 | #endif |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1751 | #ifdef FLAC__SEARCH_FOR_ESCAPES |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1752 | flat_bits = raw_bits_per_partition[partition] * partition_samples; |
| 1753 | if(flat_bits <= best_partition_bits) { |
| 1754 | raw_bits[partition] = raw_bits_per_partition[partition]; |
| 1755 | best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER; |
| 1756 | best_partition_bits = flat_bits; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1757 | } |
Josh Coalson | afcd877 | 2001-04-18 22:59:25 +0000 | [diff] [blame] | 1758 | #endif |
Josh Coalson | 034dfab | 2001-04-27 19:10:23 +0000 | [diff] [blame] | 1759 | parameters[partition] = best_rice_parameter; |
| 1760 | bits_ += best_partition_bits; |
Josh Coalson | 94e02cd | 2001-01-25 10:41:06 +0000 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | *bits = bits_; |
| 1765 | return true; |
| 1766 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1767 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 1768 | unsigned stream_encoder_get_wasted_bits_(int32 signal[], unsigned samples) |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1769 | { |
| 1770 | unsigned i, shift; |
| 1771 | int32 x = 0; |
| 1772 | |
| 1773 | for(i = 0; i < samples && !(x&1); i++) |
| 1774 | x |= signal[i]; |
| 1775 | |
| 1776 | if(x == 0) { |
| 1777 | shift = 0; |
| 1778 | } |
| 1779 | else { |
| 1780 | for(shift = 0; !(x&1); shift++) |
| 1781 | x >>= 1; |
| 1782 | } |
| 1783 | |
| 1784 | if(shift > 0) { |
| 1785 | for(i = 0; i < samples; i++) |
| 1786 | signal[i] >>= shift; |
| 1787 | } |
| 1788 | |
| 1789 | return shift; |
| 1790 | } |