blob: 5acc4f96c68402c4bd89bc83d9fb20be6a92c815 [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalson70118f62001-01-16 20:17:53 +00002 * Copyright (C) 2000,2001 Josh Coalson
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003 *
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
20#include <assert.h>
21#include <stdio.h>
22#include <stdlib.h> /* for malloc() */
23#include <string.h> /* for memcpy() */
24#include "FLAC/encoder.h"
Josh Coalson0a72e182001-04-13 19:17:16 +000025#include "FLAC/seek_table.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000026#include "private/bitbuffer.h"
Josh Coalsoneef56702001-03-30 00:45:22 +000027#include "private/bitmath.h"
Josh Coalson215af572001-03-27 01:15:58 +000028#include "private/crc.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000029#include "private/encoder_framing.h"
30#include "private/fixed.h"
31#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000032#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000033#include "private/memory.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000034
35#ifdef min
36#undef min
37#endif
38#define min(x,y) ((x)<(y)?(x):(y))
39
40#ifdef max
41#undef max
42#endif
43#define max(x,y) ((x)>(y)?(x):(y))
44
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000045typedef struct FLAC__EncoderPrivate {
46 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
47 int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
48 int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
49 real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
50 real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
Josh Coalson82b73242001-03-28 22:17:05 +000051 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
Josh Coalsonac4c1582001-03-28 23:10:22 +000052 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
Josh Coalson94e02cd2001-01-25 10:41:06 +000053 int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
54 int32 *residual_workspace_mid_side[2][2];
55 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
56 FLAC__Subframe subframe_workspace_mid_side[2][2];
57 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
58 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
59 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
60 unsigned best_subframe_mid_side[2];
61 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
62 unsigned best_subframe_bits_mid_side[2];
Josh Coalson2051dd42001-04-12 22:22:34 +000063 uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +000064 uint32 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
Josh Coalsonaef013c2001-04-24 01:25:42 +000065 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000066 FLAC__BitBuffer frame; /* the current frame being worked on */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000067 bool current_frame_can_do_mid_side; /* encoder sets this false when any given sample of a frame's side channel exceeds 16 bits */
Josh Coalsonb5e60e52001-01-28 09:27:27 +000068 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
69 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
70 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
71 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000072 FLAC__StreamMetaData metadata;
73 unsigned current_sample_number;
74 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000075 struct MD5Context md5context;
Josh Coalsoneef56702001-03-30 00:45:22 +000076 bool use_slow; /* use slow 64-bit versions of some functions */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000077 FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
78 void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
79 void *client_data;
Josh Coalsond98c43d2001-05-13 05:17:01 +000080 /* unaligned (original) pointers to allocated data */
81 int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
82 int32 *integer_signal_mid_side_unaligned[2];
83 real *real_signal_unaligned[FLAC__MAX_CHANNELS];
84 real *real_signal_mid_side_unaligned[2];
85 int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
86 int32 *residual_workspace_mid_side_unaligned[2][2];
87 uint32 *abs_residual_unaligned;
88 uint32 *abs_residual_partition_sums_unaligned;
89 unsigned *raw_bits_per_partition_unaligned;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000090} FLAC__EncoderPrivate;
91
92static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
93static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson94e02cd2001-01-25 10:41:06 +000094static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson60f77d72001-04-25 02:16:36 +000095static bool encoder_process_subframe_(FLAC__Encoder *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 Coalson82b73242001-03-28 22:17:05 +000096static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
97static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +000098static unsigned 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);
99static unsigned encoder_evaluate_lpc_subframe_(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 Coalson82b73242001-03-28 22:17:05 +0000100static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +0000101static unsigned 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 Coalsonbb6712e2001-04-24 22:54:07 +0000102#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +0000103static unsigned 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 Coalsonafcd8772001-04-18 22:59:25 +0000104#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000105static bool 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 Coalson859bc542001-03-27 22:22:27 +0000106static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000107
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000108const char *FLAC__EncoderWriteStatusString[] = {
109 "FLAC__ENCODER_WRITE_OK",
110 "FLAC__ENCODER_WRITE_FATAL_ERROR"
111};
112
113const char *FLAC__EncoderStateString[] = {
114 "FLAC__ENCODER_OK",
115 "FLAC__ENCODER_UNINITIALIZED",
116 "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
117 "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
118 "FLAC__ENCODER_INVALID_SAMPLE_RATE",
119 "FLAC__ENCODER_INVALID_BLOCK_SIZE",
120 "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
121 "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
122 "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
Josh Coalson69f1ee02001-01-24 00:54:43 +0000123 "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000124 "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
125 "FLAC__ENCODER_NOT_STREAMABLE",
126 "FLAC__ENCODER_FRAMING_ERROR",
127 "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
128 "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
129 "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
130};
131
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000132
133bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
134{
135 bool ok;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000136 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000137
138 assert(new_size > 0);
139 assert(encoder->state == FLAC__ENCODER_OK);
140 assert(encoder->guts->current_sample_number == 0);
141
142 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
143 if(new_size <= encoder->guts->input_capacity)
144 return true;
145
Josh Coalsond98c43d2001-05-13 05:17:01 +0000146 ok = true;
147 for(i = 0; ok && i < encoder->channels; i++) {
148 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_unaligned[i], &encoder->guts->integer_signal[i]);
149 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_unaligned[i], &encoder->guts->real_signal[i]);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000150 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000151 for(i = 0; ok && i < 2; i++) {
152 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_mid_side_unaligned[i], &encoder->guts->integer_signal_mid_side[i]);
153 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->guts->real_signal_mid_side_unaligned[i], &encoder->guts->real_signal_mid_side[i]);
154 }
155 for(channel = 0; ok && channel < encoder->channels; channel++) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000156 for(i = 0; ok && i < 2; i++) {
Josh Coalsond98c43d2001-05-13 05:17:01 +0000157 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_unaligned[channel][i], &encoder->guts->residual_workspace[channel][i]);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000158 }
159 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000160 for(channel = 0; ok && channel < 2; channel++) {
161 for(i = 0; ok && i < 2; i++) {
162 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->residual_workspace_mid_side_unaligned[channel][i], &encoder->guts->residual_workspace_mid_side[channel][i]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000163 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000164 }
165 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->guts->abs_residual_unaligned, &encoder->guts->abs_residual);
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000166#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond98c43d2001-05-13 05:17:01 +0000167 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size * 2, &encoder->guts->abs_residual_partition_sums_unaligned, &encoder->guts->abs_residual_partition_sums);
Josh Coalson96611082001-04-24 01:30:10 +0000168#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000169#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsond98c43d2001-05-13 05:17:01 +0000170 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->guts->raw_bits_per_partition_unaligned, &encoder->guts->raw_bits_per_partition);
Josh Coalson96611082001-04-24 01:30:10 +0000171#endif
Josh Coalsond98c43d2001-05-13 05:17:01 +0000172
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000173 if(ok)
174 encoder->guts->input_capacity = new_size;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000175 else
176 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000177
178 return ok;
179}
180
181FLAC__Encoder *FLAC__encoder_get_new_instance()
182{
183 FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
184 if(encoder != 0) {
185 encoder->state = FLAC__ENCODER_UNINITIALIZED;
186 encoder->guts = 0;
187 }
188 return encoder;
189}
190
191void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
192{
193 assert(encoder != 0);
194 free(encoder);
195}
196
197FLAC__EncoderState FLAC__encoder_init(FLAC__Encoder *encoder, FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data), void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data), void *client_data)
198{
199 unsigned i;
Josh Coalsonc692d382001-02-23 21:05:05 +0000200 FLAC__StreamMetaData padding;
Josh Coalson0a72e182001-04-13 19:17:16 +0000201 FLAC__StreamMetaData seek_table;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000202
203 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
204 assert(encoder != 0);
205 assert(write_callback != 0);
206 assert(metadata_callback != 0);
207 assert(encoder->state == FLAC__ENCODER_UNINITIALIZED);
208 assert(encoder->guts == 0);
209
210 encoder->state = FLAC__ENCODER_OK;
211
212 if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
213 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
214
215 if(encoder->do_mid_side_stereo && encoder->channels != 2)
216 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
217
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000218 if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
Josh Coalson69f1ee02001-01-24 00:54:43 +0000219 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
220
Josh Coalson82b73242001-03-28 22:17:05 +0000221 if(encoder->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000222 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
223
224 if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
225 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
226
227 if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
228 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
229
230 if(encoder->blocksize < encoder->max_lpc_order)
231 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
232
233 if(encoder->qlp_coeff_precision == 0) {
234 if(encoder->bits_per_sample < 16) {
235 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
236 /* @@@ until then we'll make a guess */
237 encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
238 }
239 else if(encoder->bits_per_sample == 16) {
240 if(encoder->blocksize <= 192)
241 encoder->qlp_coeff_precision = 7;
242 else if(encoder->blocksize <= 384)
243 encoder->qlp_coeff_precision = 8;
244 else if(encoder->blocksize <= 576)
245 encoder->qlp_coeff_precision = 9;
246 else if(encoder->blocksize <= 1152)
247 encoder->qlp_coeff_precision = 10;
248 else if(encoder->blocksize <= 2304)
249 encoder->qlp_coeff_precision = 11;
250 else if(encoder->blocksize <= 4608)
251 encoder->qlp_coeff_precision = 12;
252 else
253 encoder->qlp_coeff_precision = 13;
254 }
255 else {
256 encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
257 }
258 }
Josh Coalson0552fdf2001-02-26 20:29:56 +0000259 else if(encoder->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->qlp_coeff_precision + encoder->bits_per_sample >= 8*sizeof(uint32) || encoder->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000260 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
261
262 if(encoder->streamable_subset) {
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000263 //@@@ add check for blocksize here
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000264 if(encoder->bits_per_sample != 8 && encoder->bits_per_sample != 12 && encoder->bits_per_sample != 16 && encoder->bits_per_sample != 20 && encoder->bits_per_sample != 24)
265 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
266 if(encoder->sample_rate > 655350)
267 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
268 }
269
Josh Coalson60f77d72001-04-25 02:16:36 +0000270 if(encoder->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
271 encoder->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
272 if(encoder->min_residual_partition_order >= encoder->max_residual_partition_order)
273 encoder->min_residual_partition_order = encoder->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000274
275 encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
276 if(encoder->guts == 0)
277 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
278
279 encoder->guts->input_capacity = 0;
280 for(i = 0; i < encoder->channels; i++) {
281 encoder->guts->integer_signal[i] = 0;
282 encoder->guts->real_signal[i] = 0;
283 }
284 for(i = 0; i < 2; i++) {
285 encoder->guts->integer_signal_mid_side[i] = 0;
286 encoder->guts->real_signal_mid_side[i] = 0;
287 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000288 for(i = 0; i < encoder->channels; i++) {
289 encoder->guts->residual_workspace[i][0] = encoder->guts->residual_workspace[i][1] = 0;
290 encoder->guts->best_subframe[i] = 0;
291 }
292 for(i = 0; i < 2; i++) {
293 encoder->guts->residual_workspace_mid_side[i][0] = encoder->guts->residual_workspace_mid_side[i][1] = 0;
294 encoder->guts->best_subframe_mid_side[i] = 0;
295 }
296 for(i = 0; i < encoder->channels; i++) {
297 encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
298 encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
299 }
300 for(i = 0; i < 2; i++) {
301 encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
302 encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
303 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000304 encoder->guts->abs_residual = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000305 encoder->guts->abs_residual_partition_sums = 0;
Josh Coalsonaef013c2001-04-24 01:25:42 +0000306 encoder->guts->raw_bits_per_partition = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000307 encoder->guts->current_frame_can_do_mid_side = true;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000308 encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
309 encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
310 if(encoder->guts->loose_mid_side_stereo_frames == 0)
311 encoder->guts->loose_mid_side_stereo_frames = 1;
312 encoder->guts->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000313 encoder->guts->current_sample_number = 0;
314 encoder->guts->current_frame_number = 0;
315
Josh Coalsoneef56702001-03-30 00:45:22 +0000316 if(encoder->bits_per_sample + FLAC__bitmath_ilog2(encoder->blocksize)+1 > 30)
317 encoder->guts->use_slow = true;
318 else
319 encoder->guts->use_slow = false;
320
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000321 if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
322 /* the above function sets the state for us in case of an error */
323 return encoder->state;
324 }
325 FLAC__bitbuffer_init(&encoder->guts->frame);
326 encoder->guts->write_callback = write_callback;
327 encoder->guts->metadata_callback = metadata_callback;
328 encoder->guts->client_data = client_data;
329
330 /*
331 * write the stream header
332 */
333 if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
334 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
335
336 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
337 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
338
Josh Coalsonc692d382001-02-23 21:05:05 +0000339 encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalson0a72e182001-04-13 19:17:16 +0000340 encoder->guts->metadata.is_last = (encoder->seek_table == 0 && encoder->padding == 0);
Josh Coalsonc692d382001-02-23 21:05:05 +0000341 encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
342 encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
343 encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
344 encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
345 encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
346 encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
347 encoder->guts->metadata.data.stream_info.channels = encoder->channels;
348 encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
349 encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
350 memset(encoder->guts->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000351 MD5Init(&encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000352 if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
353 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
354
Josh Coalson0a72e182001-04-13 19:17:16 +0000355 if(0 != encoder->seek_table) {
356 if(!FLAC__seek_table_is_valid(encoder->seek_table))
357 return encoder->state = FLAC__ENCODER_INVALID_SEEK_TABLE;
358 seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
359 seek_table.is_last = (encoder->padding == 0);
360 seek_table.length = encoder->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
361 seek_table.data.seek_table = *encoder->seek_table;
362 if(!FLAC__add_metadata_block(&seek_table, &encoder->guts->frame))
363 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
364 }
365
Josh Coalsonc692d382001-02-23 21:05:05 +0000366 /* add a PADDING block if requested */
367 if(encoder->padding > 0) {
368 padding.type = FLAC__METADATA_TYPE_PADDING;
369 padding.is_last = true;
370 padding.length = encoder->padding;
371 if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
372 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
373 }
374
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000375 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
376 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
377 if(encoder->guts->write_callback(encoder, encoder->guts->frame.buffer, encoder->guts->frame.bytes, 0, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK)
378 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
379
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000380 /* now that the metadata block is written, we can init this to an absurdly-high value... */
Josh Coalsonc692d382001-02-23 21:05:05 +0000381 encoder->guts->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000382 /* ... and clear this to 0 */
Josh Coalsonc692d382001-02-23 21:05:05 +0000383 encoder->guts->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000384
385 return encoder->state;
386}
387
388void FLAC__encoder_finish(FLAC__Encoder *encoder)
389{
Josh Coalson94e02cd2001-01-25 10:41:06 +0000390 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000391
392 assert(encoder != 0);
393 if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
394 return;
395 if(encoder->guts->current_sample_number != 0) {
396 encoder->blocksize = encoder->guts->current_sample_number;
397 encoder_process_frame_(encoder, true); /* true => is last frame */
398 }
Josh Coalsonc692d382001-02-23 21:05:05 +0000399 MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000400 encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
401 if(encoder->guts != 0) {
402 for(i = 0; i < encoder->channels; i++) {
403 if(encoder->guts->integer_signal[i] != 0) {
404 free(encoder->guts->integer_signal[i]);
405 encoder->guts->integer_signal[i] = 0;
406 }
407 if(encoder->guts->real_signal[i] != 0) {
408 free(encoder->guts->real_signal[i]);
409 encoder->guts->real_signal[i] = 0;
410 }
411 }
412 for(i = 0; i < 2; i++) {
413 if(encoder->guts->integer_signal_mid_side[i] != 0) {
414 free(encoder->guts->integer_signal_mid_side[i]);
415 encoder->guts->integer_signal_mid_side[i] = 0;
416 }
417 if(encoder->guts->real_signal_mid_side[i] != 0) {
418 free(encoder->guts->real_signal_mid_side[i]);
419 encoder->guts->real_signal_mid_side[i] = 0;
420 }
421 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000422 for(channel = 0; channel < encoder->channels; channel++) {
423 for(i = 0; i < 2; i++) {
424 if(encoder->guts->residual_workspace[channel][i] != 0) {
425 free(encoder->guts->residual_workspace[channel][i]);
426 encoder->guts->residual_workspace[channel][i] = 0;
427 }
428 }
429 }
430 for(channel = 0; channel < 2; channel++) {
431 for(i = 0; i < 2; i++) {
432 if(encoder->guts->residual_workspace_mid_side[channel][i] != 0) {
433 free(encoder->guts->residual_workspace_mid_side[channel][i]);
434 encoder->guts->residual_workspace_mid_side[channel][i] = 0;
435 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000436 }
437 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000438 if(encoder->guts->abs_residual != 0) {
439 free(encoder->guts->abs_residual);
440 encoder->guts->abs_residual = 0;
441 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000442 if(encoder->guts->abs_residual_partition_sums != 0) {
443 free(encoder->guts->abs_residual_partition_sums);
444 encoder->guts->abs_residual_partition_sums = 0;
445 }
Josh Coalsonaef013c2001-04-24 01:25:42 +0000446 if(encoder->guts->raw_bits_per_partition != 0) {
447 free(encoder->guts->raw_bits_per_partition);
448 encoder->guts->raw_bits_per_partition = 0;
Josh Coalson2051dd42001-04-12 22:22:34 +0000449 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000450 FLAC__bitbuffer_free(&encoder->guts->frame);
451 free(encoder->guts);
452 encoder->guts = 0;
453 }
454 encoder->state = FLAC__ENCODER_UNINITIALIZED;
455}
456
457bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
458{
459 unsigned i, j, channel;
460 int32 x, mid, side;
461 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
462 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
463 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
464
465 assert(encoder != 0);
466 assert(encoder->state == FLAC__ENCODER_OK);
467
468 j = 0;
469 do {
470 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) {
471 for(channel = 0; channel < encoder->channels; channel++) {
472 x = buf[channel][j];
473 encoder->guts->integer_signal[channel][i] = x;
474 encoder->guts->real_signal[channel][i] = (real)x;
475 }
476 if(ms && encoder->guts->current_frame_can_do_mid_side) {
477 side = buf[0][j] - buf[1][j];
478 if(side < min_side || side > max_side) {
479 encoder->guts->current_frame_can_do_mid_side = false;
480 }
481 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000482 mid = (buf[0][j] + buf[1][j]) >> 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000483 encoder->guts->integer_signal_mid_side[0][i] = mid;
484 encoder->guts->integer_signal_mid_side[1][i] = side;
485 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
486 encoder->guts->real_signal_mid_side[1][i] = (real)side;
487 }
488 }
489 encoder->guts->current_sample_number++;
490 }
491 if(i == encoder->blocksize) {
492 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
493 return false;
494 }
495 } while(j < samples);
496
497 return true;
498}
499
500/* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
501bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
502{
503 unsigned i, j, k, channel;
504 int32 x, left = 0, mid, side;
505 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
506 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
507 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
508
509 assert(encoder != 0);
510 assert(encoder->state == FLAC__ENCODER_OK);
511
512 j = k = 0;
513 do {
514 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) {
515 for(channel = 0; channel < encoder->channels; channel++, k++) {
516 x = buf[k];
517 encoder->guts->integer_signal[channel][i] = x;
518 encoder->guts->real_signal[channel][i] = (real)x;
519 if(ms && encoder->guts->current_frame_can_do_mid_side) {
520 if(channel == 0) {
521 left = x;
522 }
523 else {
524 side = left - x;
525 if(side < min_side || side > max_side) {
526 encoder->guts->current_frame_can_do_mid_side = false;
527 }
528 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000529 mid = (left + x) >> 1; /* NOTE: not the same as 'mid = (left + x) / 2' ! */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000530 encoder->guts->integer_signal_mid_side[0][i] = mid;
531 encoder->guts->integer_signal_mid_side[1][i] = side;
532 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
533 encoder->guts->real_signal_mid_side[1][i] = (real)side;
534 }
535 }
536 }
537 }
538 encoder->guts->current_sample_number++;
539 }
540 if(i == encoder->blocksize) {
541 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
542 return false;
543 }
544 } while(j < samples);
545
546 return true;
547}
548
549bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
550{
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000551 assert(encoder->state == FLAC__ENCODER_OK);
552
553 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000554 * Accumulate raw signal to the MD5 signature
555 */
Josh Coalsoneae4dde2001-04-16 05:33:22 +0000556 /* 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 Coalsonfa37f1c2001-01-12 23:55:11 +0000557 if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
558 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
559 return false;
560 }
561
562 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000563 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000564 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000565 if(!encoder_process_subframes_(encoder, is_last_frame)) {
566 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000567 return false;
568 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000569
570 /*
571 * Zero-pad the frame to a byte_boundary
572 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000573 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000574 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
575 return false;
576 }
577
578 /*
Josh Coalson215af572001-03-27 01:15:58 +0000579 * CRC-16 the whole thing
580 */
581 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */
582 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
583 FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
584
585 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000586 * Write it
587 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000588 if(encoder->guts->write_callback(encoder, encoder->guts->frame.buffer, encoder->guts->frame.bytes, encoder->blocksize, encoder->guts->current_frame_number, encoder->guts->client_data) != FLAC__ENCODER_WRITE_OK) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000589 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
590 return false;
591 }
592
593 /*
594 * Get ready for the next frame
595 */
596 encoder->guts->current_frame_can_do_mid_side = true;
597 encoder->guts->current_sample_number = 0;
598 encoder->guts->current_frame_number++;
Josh Coalsonc692d382001-02-23 21:05:05 +0000599 encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
600 encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
601 encoder->guts->metadata.data.stream_info.max_framesize = max(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.max_framesize);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000602
603 return true;
604}
605
Josh Coalson94e02cd2001-01-25 10:41:06 +0000606bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
607{
608 FLAC__FrameHeader frame_header;
Josh Coalson034dfab2001-04-27 19:10:23 +0000609 unsigned channel, min_partition_order = encoder->min_residual_partition_order, max_partition_order;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000610 bool do_independent, do_mid_side;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000611
612 /*
Josh Coalson60f77d72001-04-25 02:16:36 +0000613 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +0000614 */
615 if(is_last_frame) {
616 max_partition_order = 0;
617 }
618 else {
619 unsigned limit = 0, b = encoder->blocksize;
620 while(!(b & 1)) {
621 limit++;
622 b >>= 1;
623 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000624 max_partition_order = min(encoder->max_residual_partition_order, limit);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000625 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000626 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000627
628 /*
629 * Setup the frame
630 */
631 if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
632 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
633 return false;
634 }
635 frame_header.blocksize = encoder->blocksize;
636 frame_header.sample_rate = encoder->sample_rate;
637 frame_header.channels = encoder->channels;
638 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
639 frame_header.bits_per_sample = encoder->bits_per_sample;
640 frame_header.number.frame_number = encoder->guts->current_frame_number;
641
642 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000643 * Figure out what channel assignments to try
644 */
645 if(encoder->do_mid_side_stereo) {
646 if(encoder->loose_mid_side_stereo) {
647 if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
648 do_independent = true;
649 do_mid_side = true;
650 }
651 else {
652 do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
653 do_mid_side = !do_independent;
654 }
655 }
656 else {
657 do_independent = true;
658 do_mid_side = true;
659 }
660 }
661 else {
662 do_independent = true;
663 do_mid_side = false;
664 }
665 if(do_mid_side && !encoder->guts->current_frame_can_do_mid_side) {
666 do_independent = true;
667 do_mid_side = false;
668 }
669
670 assert(do_independent || do_mid_side);
671
672 /*
Josh Coalson82b73242001-03-28 22:17:05 +0000673 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +0000674 */
675 if(do_independent) {
Josh Coalson82b73242001-03-28 22:17:05 +0000676 unsigned w;
677 for(channel = 0; channel < encoder->channels; channel++) {
678 w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize);
679 encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w;
680 encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w;
681 }
Josh Coalson859bc542001-03-27 22:22:27 +0000682 }
683 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000684 unsigned w;
Josh Coalson859bc542001-03-27 22:22:27 +0000685 assert(encoder->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +0000686 for(channel = 0; channel < 2; channel++) {
687 w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize);
688 encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w;
689 encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1);
690 }
Josh Coalson859bc542001-03-27 22:22:27 +0000691 }
692
693 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000694 * First do a normal encoding pass of each independent channel
695 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000696 if(do_independent) {
697 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000698 if(!encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->guts->subframe_bps[channel], encoder->guts->integer_signal[channel], encoder->guts->real_signal[channel], encoder->guts->subframe_workspace_ptr[channel], encoder->guts->residual_workspace[channel], encoder->guts->best_subframe+channel, encoder->guts->best_subframe_bits+channel))
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000699 return false;
700 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000701 }
702
703 /*
704 * Now do mid and side channels if requested
705 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000706 if(do_mid_side) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000707 assert(encoder->channels == 2);
708
709 for(channel = 0; channel < 2; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000710 if(!encoder_process_subframe_(encoder, min_partition_order, max_partition_order, false, &frame_header, encoder->guts->subframe_bps_mid_side[channel], encoder->guts->integer_signal_mid_side[channel], encoder->guts->real_signal_mid_side[channel], encoder->guts->subframe_workspace_ptr_mid_side[channel], encoder->guts->residual_workspace_mid_side[channel], encoder->guts->best_subframe_mid_side+channel, encoder->guts->best_subframe_bits_mid_side+channel))
Josh Coalson94e02cd2001-01-25 10:41:06 +0000711 return false;
712 }
713 }
714
715 /*
716 * Compose the frame bitbuffer
717 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000718 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000719 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
720 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000721 FLAC__ChannelAssignment channel_assignment;
722
Josh Coalson94e02cd2001-01-25 10:41:06 +0000723 assert(encoder->channels == 2);
724
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000725 if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
726 channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
727 }
728 else {
729 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
730 unsigned min_bits;
731 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000732
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000733 assert(do_independent && do_mid_side);
734
735 /* We have to figure out which channel assignent results in the smallest frame */
736 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1];
737 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1];
738 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1];
739 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
740
741 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
742 if(bits[ca] < min_bits) {
743 min_bits = bits[ca];
744 channel_assignment = ca;
745 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000746 }
747 }
748
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000749 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000750
751 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
752 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
753 return false;
754 }
755
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000756 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000757 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalson82b73242001-03-28 22:17:05 +0000758 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
759 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000760 break;
761 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000762 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
763 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000764 break;
765 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000766 left_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
767 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000768 break;
769 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000770 left_subframe = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]];
771 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000772 break;
773 default:
774 assert(0);
775 }
Josh Coalson82b73242001-03-28 22:17:05 +0000776
777 switch(channel_assignment) {
778 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
779 left_bps = encoder->guts->subframe_bps [0];
780 right_bps = encoder->guts->subframe_bps [1];
781 break;
782 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
783 left_bps = encoder->guts->subframe_bps [0];
784 right_bps = encoder->guts->subframe_bps_mid_side[1];
785 break;
786 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
787 left_bps = encoder->guts->subframe_bps_mid_side[1];
788 right_bps = encoder->guts->subframe_bps [1];
789 break;
790 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
791 left_bps = encoder->guts->subframe_bps_mid_side[0];
792 right_bps = encoder->guts->subframe_bps_mid_side[1];
793 break;
794 default:
795 assert(0);
796 }
797
798 /* note that encoder_add_subframe_ sets the state for us in case of an error */
799 if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame))
800 return false;
801 if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame))
802 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000803 }
804 else {
805 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
806 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
807 return false;
808 }
809
810 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000811 if(!encoder_add_subframe_(encoder, &frame_header, encoder->guts->subframe_bps[channel], &encoder->guts->subframe_workspace[channel][encoder->guts->best_subframe[channel]], &encoder->guts->frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000812 /* the above function sets the state for us in case of an error */
813 return false;
814 }
815 }
816 }
817
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000818 if(encoder->loose_mid_side_stereo) {
819 encoder->guts->loose_mid_side_stereo_frame_count++;
820 if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
821 encoder->guts->loose_mid_side_stereo_frame_count = 0;
822 }
823
824 encoder->guts->last_channel_assignment = frame_header.channel_assignment;
825
Josh Coalson94e02cd2001-01-25 10:41:06 +0000826 return true;
827}
828
Josh Coalson60f77d72001-04-25 02:16:36 +0000829bool encoder_process_subframe_(FLAC__Encoder *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 Coalson94e02cd2001-01-25 10:41:06 +0000830{
831 real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
832 real lpc_residual_bits_per_sample;
833 real autoc[FLAC__MAX_LPC_ORDER+1];
834 real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
835 real lpc_error[FLAC__MAX_LPC_ORDER];
836 unsigned min_lpc_order, max_lpc_order, lpc_order;
837 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
838 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
839 unsigned rice_parameter;
840 unsigned _candidate_bits, _best_bits;
841 unsigned _best_subframe;
842
843 /* verbatim subframe is the baseline against which we measure other compressed subframes */
844 _best_subframe = 0;
Josh Coalson82b73242001-03-28 22:17:05 +0000845 _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000846
847 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
848 /* check for constant subframe */
Josh Coalsoneef56702001-03-30 00:45:22 +0000849 if(encoder->guts->use_slow)
850 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);
851 else
852 guess_fixed_order = FLAC__fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000853 if(fixed_residual_bits_per_sample[1] == 0.0) {
854 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
855 unsigned i, signal_is_constant = true;
856 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
857 if(integer_signal[0] != integer_signal[i]) {
858 signal_is_constant = false;
859 break;
860 }
861 }
862 if(signal_is_constant) {
Josh Coalson82b73242001-03-28 22:17:05 +0000863 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000864 if(_candidate_bits < _best_bits) {
865 _best_subframe = !_best_subframe;
866 _best_bits = _candidate_bits;
867 }
868 }
869 }
870 else {
871 /* encode fixed */
872 if(encoder->do_exhaustive_model_search) {
873 min_fixed_order = 0;
874 max_fixed_order = FLAC__MAX_FIXED_ORDER;
875 }
876 else {
877 min_fixed_order = max_fixed_order = guess_fixed_order;
878 }
879 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000880 if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000881 continue; /* don't even try */
Josh Coalson46f2ae82001-02-08 00:27:21 +0000882 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 Coalsonbb6712e2001-04-24 22:54:07 +0000883#ifndef FLAC__SYMMETRIC_RICE
Josh Coalson46f2ae82001-02-08 00:27:21 +0000884 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000885#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000886 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
887 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +0000888 _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, encoder->rice_parameter_search_dist, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000889 if(_candidate_bits < _best_bits) {
890 _best_subframe = !_best_subframe;
891 _best_bits = _candidate_bits;
892 }
893 }
894
895 /* encode lpc */
896 if(encoder->max_lpc_order > 0) {
897 if(encoder->max_lpc_order >= frame_header->blocksize)
898 max_lpc_order = frame_header->blocksize-1;
899 else
900 max_lpc_order = encoder->max_lpc_order;
901 if(max_lpc_order > 0) {
902 FLAC__lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000903 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
904 if(autoc[0] != 0.0) {
905 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
906 if(encoder->do_exhaustive_model_search) {
907 min_lpc_order = 1;
908 }
909 else {
Josh Coalson82b73242001-03-28 22:17:05 +0000910 unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000911 min_lpc_order = max_lpc_order = guess_lpc_order;
912 }
913 if(encoder->do_qlp_coeff_prec_search) {
914 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalson82b73242001-03-28 22:17:05 +0000915 max_qlp_coeff_precision = min(32 - subframe_bps - 1, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000916 }
917 else {
918 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
919 }
920 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
921 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
Josh Coalson82b73242001-03-28 22:17:05 +0000922 if(lpc_residual_bits_per_sample >= (real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000923 continue; /* don't even try */
924 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000925#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000926 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000927#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000928 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
929 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000930 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000931 _candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->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->rice_parameter_search_dist, subframe[!_best_subframe]);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000932 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
933 if(_candidate_bits < _best_bits) {
934 _best_subframe = !_best_subframe;
935 _best_bits = _candidate_bits;
936 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000937 }
938 }
939 }
940 }
941 }
942 }
943 }
944 }
945
946 *best_subframe = _best_subframe;
947 *best_bits = _best_bits;
948
949 return true;
950}
951
Josh Coalson82b73242001-03-28 22:17:05 +0000952bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000953{
954 switch(subframe->type) {
955 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +0000956 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000957 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
958 return false;
959 }
960 break;
961 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +0000962 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000963 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
964 return false;
965 }
966 break;
967 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +0000968 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000969 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
970 return false;
971 }
972 break;
973 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +0000974 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000975 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
976 return false;
977 }
978 break;
979 default:
980 assert(0);
981 }
982
983 return true;
984}
985
Josh Coalson82b73242001-03-28 22:17:05 +0000986unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000987{
988 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
989 subframe->data.constant.value = signal;
990
Josh Coalson82b73242001-03-28 22:17:05 +0000991 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe_bps;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000992}
993
Josh Coalson60f77d72001-04-25 02:16:36 +0000994unsigned 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 Coalson94e02cd2001-01-25 10:41:06 +0000995{
996 unsigned i, residual_bits;
997 const unsigned residual_samples = blocksize - order;
998
999 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1000
1001 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1002
1003 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1004 subframe->data.fixed.residual = residual;
1005
Josh Coalson60f77d72001-04-25 02:16:36 +00001006 residual_bits = 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 Coalson94e02cd2001-01-25 10:41:06 +00001007
1008 subframe->data.fixed.order = order;
1009 for(i = 0; i < order; i++)
1010 subframe->data.fixed.warmup[i] = signal[i];
1011
Josh Coalson82b73242001-03-28 22:17:05 +00001012 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (order * subframe_bps) + residual_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001013}
1014
Josh Coalson60f77d72001-04-25 02:16:36 +00001015unsigned encoder_evaluate_lpc_subframe_(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 Coalson94e02cd2001-01-25 10:41:06 +00001016{
1017 int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1018 unsigned i, residual_bits;
1019 int quantization, ret;
1020 const unsigned residual_samples = blocksize - order;
1021
Josh Coalson82b73242001-03-28 22:17:05 +00001022 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001023 if(ret != 0)
1024 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1025
1026 FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1027
1028 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1029
1030 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1031 subframe->data.lpc.residual = residual;
1032
Josh Coalson60f77d72001-04-25 02:16:36 +00001033 residual_bits = 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 Coalson94e02cd2001-01-25 10:41:06 +00001034
1035 subframe->data.lpc.order = order;
1036 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1037 subframe->data.lpc.quantization_level = quantization;
1038 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1039 for(i = 0; i < order; i++)
1040 subframe->data.lpc.warmup[i] = signal[i];
1041
Josh Coalson82b73242001-03-28 22:17:05 +00001042 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 Coalson94e02cd2001-01-25 10:41:06 +00001043}
1044
Josh Coalson82b73242001-03-28 22:17:05 +00001045unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001046{
1047 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1048
1049 subframe->data.verbatim.data = signal;
1050
Josh Coalson82b73242001-03-28 22:17:05 +00001051 return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001052}
1053
Josh Coalson60f77d72001-04-25 02:16:36 +00001054unsigned 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 Coalson94e02cd2001-01-25 10:41:06 +00001055{
Josh Coalson94e02cd2001-01-25 10:41:06 +00001056 int32 r;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001057#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalsonafcd8772001-04-18 22:59:25 +00001058 unsigned sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001059 int partition_order;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001060#else
1061 unsigned partition_order;
1062#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001063 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001064 unsigned residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001065 unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001066
Josh Coalson2051dd42001-04-12 22:22:34 +00001067 /* compute abs(residual) for use later */
1068 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1069 r = residual[residual_sample];
1070 abs_residual[residual_sample] = (uint32)(r<0? -r : r);
1071 }
1072
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001073#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001074 max_partition_order = 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);
1075 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001076
Josh Coalson60f77d72001-04-25 02:16:36 +00001077 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1078 if(!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)) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00001079 assert(0); /* encoder_precompute_partition_info_ should keep this from ever happening */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001080 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001081 sum += 1u << partition_order;
1082 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1083 best_residual_bits = residual_bits;
1084 *best_partition_order = partition_order;
1085 best_parameters_index = !best_parameters_index;
1086 }
1087 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001088#else
Josh Coalson60f77d72001-04-25 02:16:36 +00001089 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1090 if(!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 Coalsonafcd8772001-04-18 22:59:25 +00001091 assert(best_residual_bits != 0);
1092 break;
1093 }
1094 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1095 best_residual_bits = residual_bits;
1096 *best_partition_order = partition_order;
1097 best_parameters_index = !best_parameters_index;
1098 }
1099 }
1100#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001101 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1102 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1103
1104 return best_residual_bits;
1105}
1106
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001107#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001108unsigned 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 Coalsond4e0ddb2001-04-18 02:20:52 +00001109{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001110 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001111 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001112 const unsigned blocksize = residual_samples + predictor_order;
1113
Josh Coalsonaef013c2001-04-24 01:25:42 +00001114 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001115 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001116#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001117 uint32 abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001118#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001119#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001120 uint32 abs_residual_partition_max;
1121 unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */
1122#endif
1123 uint32 abs_r;
1124 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001125 const unsigned partitions = 1u << partition_order;
1126 const unsigned default_partition_samples = blocksize >> partition_order;
1127
1128 if(default_partition_samples <= predictor_order) {
1129 assert(max_partition_order > 0);
1130 max_partition_order--;
1131 }
1132 else {
1133 for(partition = residual_sample = 0; partition < partitions; partition++) {
1134 partition_samples = default_partition_samples;
1135 if(partition == 0)
1136 partition_samples -= predictor_order;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001137#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001138 abs_residual_partition_sum = 0;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001139#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001140#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001141 abs_residual_partition_max = 0;
1142#endif
1143 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1144 abs_r = abs_residual[residual_sample];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001145#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001146 abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */
1147#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001148#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001149 if(abs_r > abs_residual_partition_max) {
1150 abs_residual_partition_max = abs_r;
1151 abs_residual_partition_max_index = residual_sample;
1152 }
1153#endif
1154 residual_sample++;
1155 }
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001156#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001157 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001158#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001159#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001160 if(abs_residual_partition_max > 0)
1161 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]);
1162 else
1163 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0);
1164#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001165 }
Josh Coalsonaef013c2001-04-24 01:25:42 +00001166 to_partition = partitions;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001167 break;
1168 }
1169 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00001170
Josh Coalsonaef013c2001-04-24 01:25:42 +00001171 /* now merge for lower orders */
Josh Coalson034dfab2001-04-27 19:10:23 +00001172 for(from_partition = 0; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001173#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001174 uint32 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001175#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001176#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001177 unsigned m;
1178#endif
1179 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001180 const unsigned partitions = 1u << partition_order;
1181 for(i = 0; i < partitions; i++) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001182#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001183 s = abs_residual_partition_sums[from_partition];
1184#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001185#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001186 m = raw_bits_per_partition[from_partition];
1187#endif
1188 from_partition++;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001189#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001190 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1191#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001192#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001193 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1194#endif
1195 from_partition++;
1196 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001197 }
1198 }
1199
Josh Coalsonf76a3612001-04-18 02:28:11 +00001200 return max_partition_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001201}
Josh Coalsonafcd8772001-04-18 22:59:25 +00001202#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001203
Josh Coalson352e0f62001-03-20 22:55:50 +00001204#ifdef VARIABLE_RICE_BITS
1205#undef VARIABLE_RICE_BITS
1206#endif
1207#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1208
Josh Coalson034dfab2001-04-27 19:10:23 +00001209bool 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 Coalson94e02cd2001-01-25 10:41:06 +00001210{
Josh Coalson034dfab2001-04-27 19:10:23 +00001211 unsigned rice_parameter, partition_bits;
1212#ifndef NO_RICE_SEARCH
1213 unsigned best_partition_bits;
1214 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1215#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001216#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001217 unsigned flat_bits;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001218#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001219 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1220
Josh Coalson034dfab2001-04-27 19:10:23 +00001221 assert(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00001222
Josh Coalson94e02cd2001-01-25 10:41:06 +00001223 if(partition_order == 0) {
1224 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00001225
Josh Coalson034dfab2001-04-27 19:10:23 +00001226#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001227 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001228 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001229 min_rice_parameter = 0;
1230 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001231 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1232 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson60f77d72001-04-25 02:16:36 +00001233 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1234 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1235 }
1236 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001237 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001238
Josh Coalson034dfab2001-04-27 19:10:23 +00001239 best_partition_bits = 0xffffffff;
1240 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1241#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001242#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001243#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001244 partition_bits = (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001245#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001246 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00001247 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001248#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001249#else
1250 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001251#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001252 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001253 for(i = 0; i < residual_samples; i++) {
1254#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001255#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001256 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001257#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001258 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001259#endif
1260#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001261 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001262#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001263 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001264#ifndef NO_RICE_SEARCH
1265 if(partition_bits < best_partition_bits) {
1266 best_rice_parameter = rice_parameter;
1267 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00001268 }
1269 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001270#endif
1271#ifdef FLAC__SEARCH_FOR_ESCAPES
1272 flat_bits = raw_bits_per_partition[0] * residual_samples;
1273 if(flat_bits <= best_partition_bits) {
1274 raw_bits[0] = raw_bits_per_partition[0];
1275 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1276 best_partition_bits = flat_bits;
1277 }
1278#endif
1279 parameters[0] = best_rice_parameter;
1280 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001281 }
1282 else {
Josh Coalson034dfab2001-04-27 19:10:23 +00001283 unsigned partition, j, save_j, k;
1284 unsigned mean, partition_samples;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001285 const unsigned partitions = 1u << partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00001286 for(partition = j = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001287 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00001288 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001289 if(partition_samples <= predictor_order)
1290 return false;
1291 else
1292 partition_samples -= predictor_order;
1293 }
1294 mean = partition_samples >> 1;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001295#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalson034dfab2001-04-27 19:10:23 +00001296 mean += abs_residual_partition_sums[partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00001297#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001298 save_j = j;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001299 for(k = 0; k < partition_samples; j++, k++)
1300 mean += abs_residual[j];
Josh Coalson034dfab2001-04-27 19:10:23 +00001301 j = save_j;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001302#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001303 mean /= partition_samples;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001304#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001305 /* calc rice_parameter = floor(log2(mean)) */
1306 rice_parameter = 0;
1307 mean>>=1;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001308 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001309 rice_parameter++;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001310 mean >>= 1;
1311 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001312#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001313 /* calc rice_parameter = floor(log2(mean)) + 1 */
1314 rice_parameter = 0;
Josh Coalson352e0f62001-03-20 22:55:50 +00001315 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001316 rice_parameter++;
Josh Coalson352e0f62001-03-20 22:55:50 +00001317 mean >>= 1;
1318 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001319#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001320 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1321 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +00001322
Josh Coalson034dfab2001-04-27 19:10:23 +00001323#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001324 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001325 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001326 min_rice_parameter = 0;
1327 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001328 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1329 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson60f77d72001-04-25 02:16:36 +00001330 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1331 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1332 }
1333 else
1334 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00001335
Josh Coalson034dfab2001-04-27 19:10:23 +00001336 best_partition_bits = 0xffffffff;
1337 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1338#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001339#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001340#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001341 partition_bits = (2+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001342#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001343 const unsigned rice_parameter_estimate = rice_parameter-1;
1344 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001345#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001346#else
1347 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001348#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001349 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1350 save_j = j;
1351 for(k = 0; k < partition_samples; j++, k++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00001352#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001353#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001354 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001355#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001356 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001357#endif
1358#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001359 partition_bits += FLAC__bitbuffer_rice_bits(residual[j], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001360#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001361 }
1362 if(rice_parameter != max_rice_parameter)
1363 j = save_j;
1364#ifndef NO_RICE_SEARCH
1365 if(partition_bits < best_partition_bits) {
1366 best_rice_parameter = rice_parameter;
1367 best_partition_bits = partition_bits;
1368 }
Josh Coalson2051dd42001-04-12 22:22:34 +00001369 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001370#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001371#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalson034dfab2001-04-27 19:10:23 +00001372 flat_bits = raw_bits_per_partition[partition] * partition_samples;
1373 if(flat_bits <= best_partition_bits) {
1374 raw_bits[partition] = raw_bits_per_partition[partition];
1375 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1376 best_partition_bits = flat_bits;
Josh Coalson2051dd42001-04-12 22:22:34 +00001377 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001378#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001379 parameters[partition] = best_rice_parameter;
1380 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001381 }
1382 }
1383
1384 *bits = bits_;
1385 return true;
1386}
Josh Coalson859bc542001-03-27 22:22:27 +00001387
Josh Coalson8e92f212001-05-01 18:50:45 +00001388unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00001389{
1390 unsigned i, shift;
1391 int32 x = 0;
1392
1393 for(i = 0; i < samples && !(x&1); i++)
1394 x |= signal[i];
1395
1396 if(x == 0) {
1397 shift = 0;
1398 }
1399 else {
1400 for(shift = 0; !(x&1); shift++)
1401 x >>= 1;
1402 }
1403
1404 if(shift > 0) {
1405 for(i = 0; i < samples; i++)
1406 signal[i] >>= shift;
1407 }
1408
1409 return shift;
1410}