blob: f764caad11c68c555cc0605d28e4c75c22921a7f [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 Coalsoncf30f502001-05-23 20:57:44 +000029#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000030#include "private/encoder_framing.h"
31#include "private/fixed.h"
32#include "private/lpc.h"
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000033#include "private/md5.h"
Josh Coalsond98c43d2001-05-13 05:17:01 +000034#include "private/memory.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000035
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 Coalsonbb7f6b92000-12-10 04:09:52 +000046typedef struct FLAC__EncoderPrivate {
47 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
48 int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
49 int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
50 real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
51 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 +000052 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 +000053 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 +000054 int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
55 int32 *residual_workspace_mid_side[2][2];
56 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
57 FLAC__Subframe subframe_workspace_mid_side[2][2];
58 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
59 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
60 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
61 unsigned best_subframe_mid_side[2];
62 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
63 unsigned best_subframe_bits_mid_side[2];
Josh Coalson2051dd42001-04-12 22:22:34 +000064 uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +000065 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 +000066 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 +000067 FLAC__BitBuffer frame; /* the current frame being worked on */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000068 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 +000069 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
70 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
71 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
72 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000073 FLAC__StreamMetaData metadata;
74 unsigned current_sample_number;
75 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000076 struct MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +000077 FLAC__CPUInfo cpuinfo;
78 unsigned (*local_fixed_compute_best_predictor)(const int32 data[], unsigned data_len, real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
79 void (*local_lpc_compute_autocorrelation)(const real data[], unsigned data_len, unsigned lag, real autoc[]);
Josh Coalsoneef56702001-03-30 00:45:22 +000080 bool use_slow; /* use slow 64-bit versions of some functions */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000081 FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
82 void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
83 void *client_data;
Josh Coalsond98c43d2001-05-13 05:17:01 +000084 /* unaligned (original) pointers to allocated data */
85 int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
86 int32 *integer_signal_mid_side_unaligned[2];
87 real *real_signal_unaligned[FLAC__MAX_CHANNELS];
88 real *real_signal_mid_side_unaligned[2];
89 int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
90 int32 *residual_workspace_mid_side_unaligned[2][2];
91 uint32 *abs_residual_unaligned;
92 uint32 *abs_residual_partition_sums_unaligned;
93 unsigned *raw_bits_per_partition_unaligned;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000094} FLAC__EncoderPrivate;
95
96static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
97static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson94e02cd2001-01-25 10:41:06 +000098static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson60f77d72001-04-25 02:16:36 +000099static 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 +0000100static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
101static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +0000102static 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);
103static 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 +0000104static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalson60f77d72001-04-25 02:16:36 +0000105static 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 +0000106#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +0000107static 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 +0000108#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000109static 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 +0000110static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000111
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000112const char *FLAC__EncoderWriteStatusString[] = {
113 "FLAC__ENCODER_WRITE_OK",
114 "FLAC__ENCODER_WRITE_FATAL_ERROR"
115};
116
117const char *FLAC__EncoderStateString[] = {
118 "FLAC__ENCODER_OK",
119 "FLAC__ENCODER_UNINITIALIZED",
120 "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
121 "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
122 "FLAC__ENCODER_INVALID_SAMPLE_RATE",
123 "FLAC__ENCODER_INVALID_BLOCK_SIZE",
124 "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
125 "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
126 "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
Josh Coalson69f1ee02001-01-24 00:54:43 +0000127 "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000128 "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
129 "FLAC__ENCODER_NOT_STREAMABLE",
130 "FLAC__ENCODER_FRAMING_ERROR",
131 "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
132 "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
133 "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
134};
135
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000136
137bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
138{
139 bool ok;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000140 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000141
142 assert(new_size > 0);
143 assert(encoder->state == FLAC__ENCODER_OK);
144 assert(encoder->guts->current_sample_number == 0);
145
146 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
147 if(new_size <= encoder->guts->input_capacity)
148 return true;
149
Josh Coalsond98c43d2001-05-13 05:17:01 +0000150 ok = true;
151 for(i = 0; ok && i < encoder->channels; i++) {
152 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->guts->integer_signal_unaligned[i], &encoder->guts->integer_signal[i]);
153 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 +0000154 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000155 for(i = 0; ok && i < 2; i++) {
156 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]);
157 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]);
158 }
159 for(channel = 0; ok && channel < encoder->channels; channel++) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000160 for(i = 0; ok && i < 2; i++) {
Josh Coalsond98c43d2001-05-13 05:17:01 +0000161 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 +0000162 }
163 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000164 for(channel = 0; ok && channel < 2; channel++) {
165 for(i = 0; ok && i < 2; i++) {
166 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 +0000167 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000168 }
169 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 +0000170#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond98c43d2001-05-13 05:17:01 +0000171 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 +0000172#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +0000173#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsond98c43d2001-05-13 05:17:01 +0000174 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 +0000175#endif
Josh Coalsond98c43d2001-05-13 05:17:01 +0000176
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000177 if(ok)
178 encoder->guts->input_capacity = new_size;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000179 else
180 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000181
182 return ok;
183}
184
185FLAC__Encoder *FLAC__encoder_get_new_instance()
186{
187 FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
188 if(encoder != 0) {
189 encoder->state = FLAC__ENCODER_UNINITIALIZED;
190 encoder->guts = 0;
191 }
192 return encoder;
193}
194
195void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
196{
197 assert(encoder != 0);
198 free(encoder);
199}
200
201FLAC__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)
202{
203 unsigned i;
Josh Coalsonc692d382001-02-23 21:05:05 +0000204 FLAC__StreamMetaData padding;
Josh Coalson0a72e182001-04-13 19:17:16 +0000205 FLAC__StreamMetaData seek_table;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000206
207 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
208 assert(encoder != 0);
209 assert(write_callback != 0);
210 assert(metadata_callback != 0);
211 assert(encoder->state == FLAC__ENCODER_UNINITIALIZED);
212 assert(encoder->guts == 0);
213
214 encoder->state = FLAC__ENCODER_OK;
215
216 if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
217 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
218
219 if(encoder->do_mid_side_stereo && encoder->channels != 2)
220 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
221
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000222 if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
Josh Coalson69f1ee02001-01-24 00:54:43 +0000223 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
224
Josh Coalson82b73242001-03-28 22:17:05 +0000225 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 +0000226 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
227
228 if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
229 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
230
231 if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
232 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
233
234 if(encoder->blocksize < encoder->max_lpc_order)
235 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
236
237 if(encoder->qlp_coeff_precision == 0) {
238 if(encoder->bits_per_sample < 16) {
239 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
240 /* @@@ until then we'll make a guess */
241 encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
242 }
243 else if(encoder->bits_per_sample == 16) {
244 if(encoder->blocksize <= 192)
245 encoder->qlp_coeff_precision = 7;
246 else if(encoder->blocksize <= 384)
247 encoder->qlp_coeff_precision = 8;
248 else if(encoder->blocksize <= 576)
249 encoder->qlp_coeff_precision = 9;
250 else if(encoder->blocksize <= 1152)
251 encoder->qlp_coeff_precision = 10;
252 else if(encoder->blocksize <= 2304)
253 encoder->qlp_coeff_precision = 11;
254 else if(encoder->blocksize <= 4608)
255 encoder->qlp_coeff_precision = 12;
256 else
257 encoder->qlp_coeff_precision = 13;
258 }
259 else {
260 encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
261 }
262 }
Josh Coalson0552fdf2001-02-26 20:29:56 +0000263 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 +0000264 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
265
266 if(encoder->streamable_subset) {
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000267 //@@@ add check for blocksize here
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000268 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)
269 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
270 if(encoder->sample_rate > 655350)
271 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
272 }
273
Josh Coalson60f77d72001-04-25 02:16:36 +0000274 if(encoder->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
275 encoder->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
276 if(encoder->min_residual_partition_order >= encoder->max_residual_partition_order)
277 encoder->min_residual_partition_order = encoder->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000278
279 encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
280 if(encoder->guts == 0)
281 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
282
283 encoder->guts->input_capacity = 0;
284 for(i = 0; i < encoder->channels; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000285 encoder->guts->integer_signal_unaligned[i] = encoder->guts->integer_signal[i] = 0;
286 encoder->guts->real_signal_unaligned[i] = encoder->guts->real_signal[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000287 }
288 for(i = 0; i < 2; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000289 encoder->guts->integer_signal_mid_side_unaligned[i] = encoder->guts->integer_signal_mid_side[i] = 0;
290 encoder->guts->real_signal_mid_side_unaligned[i] = encoder->guts->real_signal_mid_side[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000291 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000292 for(i = 0; i < encoder->channels; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000293 encoder->guts->residual_workspace_unaligned[i][0] = encoder->guts->residual_workspace[i][0] = 0;
294 encoder->guts->residual_workspace_unaligned[i][1] = encoder->guts->residual_workspace[i][1] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000295 encoder->guts->best_subframe[i] = 0;
296 }
297 for(i = 0; i < 2; i++) {
Josh Coalson6bd17572001-05-25 19:02:01 +0000298 encoder->guts->residual_workspace_mid_side_unaligned[i][0] = encoder->guts->residual_workspace_mid_side[i][0] = 0;
299 encoder->guts->residual_workspace_mid_side_unaligned[i][1] = encoder->guts->residual_workspace_mid_side[i][1] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000300 encoder->guts->best_subframe_mid_side[i] = 0;
301 }
302 for(i = 0; i < encoder->channels; i++) {
303 encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
304 encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
305 }
306 for(i = 0; i < 2; i++) {
307 encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
308 encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
309 }
Josh Coalson6bd17572001-05-25 19:02:01 +0000310 encoder->guts->abs_residual_unaligned = encoder->guts->abs_residual = 0;
311 encoder->guts->abs_residual_partition_sums_unaligned = encoder->guts->abs_residual_partition_sums = 0;
312 encoder->guts->raw_bits_per_partition_unaligned = encoder->guts->raw_bits_per_partition = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000313 encoder->guts->current_frame_can_do_mid_side = true;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000314 encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
315 encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
316 if(encoder->guts->loose_mid_side_stereo_frames == 0)
317 encoder->guts->loose_mid_side_stereo_frames = 1;
318 encoder->guts->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000319 encoder->guts->current_sample_number = 0;
320 encoder->guts->current_frame_number = 0;
321
Josh Coalsoncf30f502001-05-23 20:57:44 +0000322 /*
323 * get the CPU info and set the function pointers
324 */
325 FLAC__cpu_info(&encoder->guts->cpuinfo);
326 /* first default to the non-asm routines */
327 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
328 encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
329 /* now override with asm where appropriate */
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000330#ifndef FLAC__NO_ASM
331 assert(encoder->guts->cpuinfo.use_asm);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000332#ifdef FLAC__CPU_IA32
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000333 assert(encoder->guts->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson034d38e2001-05-24 19:29:30 +0000334#ifdef FLAC__HAS_NASM
Josh Coalsoncf30f502001-05-23 20:57:44 +0000335#if 0
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000336 /* @@@ SSE version not working yet */
337 if(encoder->guts->cpuinfo.data.ia32.sse)
338 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386_sse;
339 else
Josh Coalsoncf30f502001-05-23 20:57:44 +0000340#endif
341fprintf(stderr,"@@@ got _asm_i386 of lpc_compute_autocorrelation()\n");
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000342 encoder->guts->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_i386;
343 if(encoder->guts->cpuinfo.data.ia32.mmx && encoder->guts->cpuinfo.data.ia32.cmov)
Josh Coalsoncf30f502001-05-23 20:57:44 +0000344{
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000345 encoder->guts->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_i386_mmx_cmov;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000346fprintf(stderr,"@@@ got _asm_i386_mmx_cmov of fixed_compute_best_predictor()\n");}
347#endif
Josh Coalson034d38e2001-05-24 19:29:30 +0000348#endif
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000349#endif
Josh Coalsoncf30f502001-05-23 20:57:44 +0000350
Josh Coalsoneef56702001-03-30 00:45:22 +0000351 if(encoder->bits_per_sample + FLAC__bitmath_ilog2(encoder->blocksize)+1 > 30)
352 encoder->guts->use_slow = true;
353 else
354 encoder->guts->use_slow = false;
355
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000356 if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
357 /* the above function sets the state for us in case of an error */
358 return encoder->state;
359 }
360 FLAC__bitbuffer_init(&encoder->guts->frame);
361 encoder->guts->write_callback = write_callback;
362 encoder->guts->metadata_callback = metadata_callback;
363 encoder->guts->client_data = client_data;
364
365 /*
366 * write the stream header
367 */
368 if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
369 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
370
371 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
372 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
373
Josh Coalsonc692d382001-02-23 21:05:05 +0000374 encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalson0a72e182001-04-13 19:17:16 +0000375 encoder->guts->metadata.is_last = (encoder->seek_table == 0 && encoder->padding == 0);
Josh Coalsonc692d382001-02-23 21:05:05 +0000376 encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
377 encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
378 encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
379 encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
380 encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
381 encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
382 encoder->guts->metadata.data.stream_info.channels = encoder->channels;
383 encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
384 encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
385 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 +0000386 MD5Init(&encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000387 if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
388 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
389
Josh Coalson0a72e182001-04-13 19:17:16 +0000390 if(0 != encoder->seek_table) {
391 if(!FLAC__seek_table_is_valid(encoder->seek_table))
392 return encoder->state = FLAC__ENCODER_INVALID_SEEK_TABLE;
393 seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
394 seek_table.is_last = (encoder->padding == 0);
395 seek_table.length = encoder->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
396 seek_table.data.seek_table = *encoder->seek_table;
397 if(!FLAC__add_metadata_block(&seek_table, &encoder->guts->frame))
398 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
399 }
400
Josh Coalsonc692d382001-02-23 21:05:05 +0000401 /* add a PADDING block if requested */
402 if(encoder->padding > 0) {
403 padding.type = FLAC__METADATA_TYPE_PADDING;
404 padding.is_last = true;
405 padding.length = encoder->padding;
406 if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
407 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
408 }
409
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000410 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
411 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
412 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)
413 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
414
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000415 /* now that the metadata block is written, we can init this to an absurdly-high value... */
Josh Coalsonc692d382001-02-23 21:05:05 +0000416 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 +0000417 /* ... and clear this to 0 */
Josh Coalsonc692d382001-02-23 21:05:05 +0000418 encoder->guts->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000419
420 return encoder->state;
421}
422
423void FLAC__encoder_finish(FLAC__Encoder *encoder)
424{
Josh Coalson94e02cd2001-01-25 10:41:06 +0000425 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000426
427 assert(encoder != 0);
428 if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
429 return;
430 if(encoder->guts->current_sample_number != 0) {
431 encoder->blocksize = encoder->guts->current_sample_number;
432 encoder_process_frame_(encoder, true); /* true => is last frame */
433 }
Josh Coalsonc692d382001-02-23 21:05:05 +0000434 MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000435 encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
436 if(encoder->guts != 0) {
437 for(i = 0; i < encoder->channels; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000438 if(encoder->guts->integer_signal_unaligned[i] != 0) {
439 free(encoder->guts->integer_signal_unaligned[i]);
440 encoder->guts->integer_signal_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000441 }
Josh Coalson18301942001-05-16 19:25:33 +0000442 if(encoder->guts->real_signal_unaligned[i] != 0) {
443 free(encoder->guts->real_signal_unaligned[i]);
444 encoder->guts->real_signal_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000445 }
446 }
447 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000448 if(encoder->guts->integer_signal_mid_side_unaligned[i] != 0) {
449 free(encoder->guts->integer_signal_mid_side_unaligned[i]);
450 encoder->guts->integer_signal_mid_side_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000451 }
Josh Coalson18301942001-05-16 19:25:33 +0000452 if(encoder->guts->real_signal_mid_side_unaligned[i] != 0) {
453 free(encoder->guts->real_signal_mid_side_unaligned[i]);
454 encoder->guts->real_signal_mid_side_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000455 }
456 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000457 for(channel = 0; channel < encoder->channels; channel++) {
458 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000459 if(encoder->guts->residual_workspace_unaligned[channel][i] != 0) {
460 free(encoder->guts->residual_workspace_unaligned[channel][i]);
461 encoder->guts->residual_workspace_unaligned[channel][i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000462 }
463 }
464 }
465 for(channel = 0; channel < 2; channel++) {
466 for(i = 0; i < 2; i++) {
Josh Coalson18301942001-05-16 19:25:33 +0000467 if(encoder->guts->residual_workspace_mid_side_unaligned[channel][i] != 0) {
468 free(encoder->guts->residual_workspace_mid_side_unaligned[channel][i]);
469 encoder->guts->residual_workspace_mid_side_unaligned[channel][i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000470 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000471 }
472 }
Josh Coalson18301942001-05-16 19:25:33 +0000473 if(encoder->guts->abs_residual_unaligned != 0) {
474 free(encoder->guts->abs_residual_unaligned);
475 encoder->guts->abs_residual_unaligned = 0;
Josh Coalsone77287e2001-01-20 01:27:55 +0000476 }
Josh Coalson18301942001-05-16 19:25:33 +0000477 if(encoder->guts->abs_residual_partition_sums_unaligned != 0) {
478 free(encoder->guts->abs_residual_partition_sums_unaligned);
479 encoder->guts->abs_residual_partition_sums_unaligned = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000480 }
Josh Coalson18301942001-05-16 19:25:33 +0000481 if(encoder->guts->raw_bits_per_partition_unaligned != 0) {
482 free(encoder->guts->raw_bits_per_partition_unaligned);
483 encoder->guts->raw_bits_per_partition_unaligned = 0;
Josh Coalson2051dd42001-04-12 22:22:34 +0000484 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000485 FLAC__bitbuffer_free(&encoder->guts->frame);
486 free(encoder->guts);
487 encoder->guts = 0;
488 }
489 encoder->state = FLAC__ENCODER_UNINITIALIZED;
490}
491
492bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
493{
494 unsigned i, j, channel;
495 int32 x, mid, side;
496 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
497 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
498 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
499
500 assert(encoder != 0);
501 assert(encoder->state == FLAC__ENCODER_OK);
502
503 j = 0;
504 do {
505 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) {
506 for(channel = 0; channel < encoder->channels; channel++) {
507 x = buf[channel][j];
508 encoder->guts->integer_signal[channel][i] = x;
509 encoder->guts->real_signal[channel][i] = (real)x;
510 }
511 if(ms && encoder->guts->current_frame_can_do_mid_side) {
512 side = buf[0][j] - buf[1][j];
513 if(side < min_side || side > max_side) {
514 encoder->guts->current_frame_can_do_mid_side = false;
515 }
516 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000517 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 +0000518 encoder->guts->integer_signal_mid_side[0][i] = mid;
519 encoder->guts->integer_signal_mid_side[1][i] = side;
520 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
521 encoder->guts->real_signal_mid_side[1][i] = (real)side;
522 }
523 }
524 encoder->guts->current_sample_number++;
525 }
526 if(i == encoder->blocksize) {
527 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
528 return false;
529 }
530 } while(j < samples);
531
532 return true;
533}
534
535/* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
536bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
537{
538 unsigned i, j, k, channel;
539 int32 x, left = 0, mid, side;
540 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
541 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
542 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
543
544 assert(encoder != 0);
545 assert(encoder->state == FLAC__ENCODER_OK);
546
547 j = k = 0;
548 do {
549 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) {
550 for(channel = 0; channel < encoder->channels; channel++, k++) {
551 x = buf[k];
552 encoder->guts->integer_signal[channel][i] = x;
553 encoder->guts->real_signal[channel][i] = (real)x;
554 if(ms && encoder->guts->current_frame_can_do_mid_side) {
555 if(channel == 0) {
556 left = x;
557 }
558 else {
559 side = left - x;
560 if(side < min_side || side > max_side) {
561 encoder->guts->current_frame_can_do_mid_side = false;
562 }
563 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000564 mid = (left + x) >> 1; /* NOTE: not the same as 'mid = (left + x) / 2' ! */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000565 encoder->guts->integer_signal_mid_side[0][i] = mid;
566 encoder->guts->integer_signal_mid_side[1][i] = side;
567 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
568 encoder->guts->real_signal_mid_side[1][i] = (real)side;
569 }
570 }
571 }
572 }
573 encoder->guts->current_sample_number++;
574 }
575 if(i == encoder->blocksize) {
576 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
577 return false;
578 }
579 } while(j < samples);
580
581 return true;
582}
583
584bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
585{
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000586 assert(encoder->state == FLAC__ENCODER_OK);
587
588 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000589 * Accumulate raw signal to the MD5 signature
590 */
Josh Coalsoneae4dde2001-04-16 05:33:22 +0000591 /* 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 +0000592 if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
593 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
594 return false;
595 }
596
597 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000598 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000599 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000600 if(!encoder_process_subframes_(encoder, is_last_frame)) {
601 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000602 return false;
603 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000604
605 /*
606 * Zero-pad the frame to a byte_boundary
607 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000608 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000609 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
610 return false;
611 }
612
613 /*
Josh Coalson215af572001-03-27 01:15:58 +0000614 * CRC-16 the whole thing
615 */
616 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */
617 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
618 FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
619
620 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000621 * Write it
622 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000623 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 +0000624 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
625 return false;
626 }
627
628 /*
629 * Get ready for the next frame
630 */
631 encoder->guts->current_frame_can_do_mid_side = true;
632 encoder->guts->current_sample_number = 0;
633 encoder->guts->current_frame_number++;
Josh Coalsonc692d382001-02-23 21:05:05 +0000634 encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
635 encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
636 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 +0000637
638 return true;
639}
640
Josh Coalson94e02cd2001-01-25 10:41:06 +0000641bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
642{
643 FLAC__FrameHeader frame_header;
Josh Coalson034dfab2001-04-27 19:10:23 +0000644 unsigned channel, min_partition_order = encoder->min_residual_partition_order, max_partition_order;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000645 bool do_independent, do_mid_side;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000646
647 /*
Josh Coalson60f77d72001-04-25 02:16:36 +0000648 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +0000649 */
650 if(is_last_frame) {
651 max_partition_order = 0;
652 }
653 else {
654 unsigned limit = 0, b = encoder->blocksize;
655 while(!(b & 1)) {
656 limit++;
657 b >>= 1;
658 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000659 max_partition_order = min(encoder->max_residual_partition_order, limit);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000660 }
Josh Coalson60f77d72001-04-25 02:16:36 +0000661 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000662
663 /*
664 * Setup the frame
665 */
666 if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
667 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
668 return false;
669 }
670 frame_header.blocksize = encoder->blocksize;
671 frame_header.sample_rate = encoder->sample_rate;
672 frame_header.channels = encoder->channels;
673 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
674 frame_header.bits_per_sample = encoder->bits_per_sample;
675 frame_header.number.frame_number = encoder->guts->current_frame_number;
676
677 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000678 * Figure out what channel assignments to try
679 */
680 if(encoder->do_mid_side_stereo) {
681 if(encoder->loose_mid_side_stereo) {
682 if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
683 do_independent = true;
684 do_mid_side = true;
685 }
686 else {
687 do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
688 do_mid_side = !do_independent;
689 }
690 }
691 else {
692 do_independent = true;
693 do_mid_side = true;
694 }
695 }
696 else {
697 do_independent = true;
698 do_mid_side = false;
699 }
700 if(do_mid_side && !encoder->guts->current_frame_can_do_mid_side) {
701 do_independent = true;
702 do_mid_side = false;
703 }
704
705 assert(do_independent || do_mid_side);
706
707 /*
Josh Coalson82b73242001-03-28 22:17:05 +0000708 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +0000709 */
710 if(do_independent) {
Josh Coalson82b73242001-03-28 22:17:05 +0000711 unsigned w;
712 for(channel = 0; channel < encoder->channels; channel++) {
713 w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize);
714 encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w;
715 encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w;
716 }
Josh Coalson859bc542001-03-27 22:22:27 +0000717 }
718 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000719 unsigned w;
Josh Coalson859bc542001-03-27 22:22:27 +0000720 assert(encoder->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +0000721 for(channel = 0; channel < 2; channel++) {
722 w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize);
723 encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w;
724 encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1);
725 }
Josh Coalson859bc542001-03-27 22:22:27 +0000726 }
727
728 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000729 * First do a normal encoding pass of each independent channel
730 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000731 if(do_independent) {
732 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000733 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 +0000734 return false;
735 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000736 }
737
738 /*
739 * Now do mid and side channels if requested
740 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000741 if(do_mid_side) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000742 assert(encoder->channels == 2);
743
744 for(channel = 0; channel < 2; channel++) {
Josh Coalson60f77d72001-04-25 02:16:36 +0000745 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 +0000746 return false;
747 }
748 }
749
750 /*
751 * Compose the frame bitbuffer
752 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000753 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000754 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
755 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000756 FLAC__ChannelAssignment channel_assignment;
757
Josh Coalson94e02cd2001-01-25 10:41:06 +0000758 assert(encoder->channels == 2);
759
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000760 if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
761 channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
762 }
763 else {
764 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
765 unsigned min_bits;
766 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000767
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000768 assert(do_independent && do_mid_side);
769
770 /* We have to figure out which channel assignent results in the smallest frame */
771 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1];
772 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1];
773 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1];
774 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
775
776 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
777 if(bits[ca] < min_bits) {
778 min_bits = bits[ca];
779 channel_assignment = ca;
780 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000781 }
782 }
783
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000784 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000785
786 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
787 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
788 return false;
789 }
790
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000791 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000792 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalson82b73242001-03-28 22:17:05 +0000793 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
794 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000795 break;
796 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000797 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
798 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000799 break;
800 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000801 left_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
802 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000803 break;
804 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000805 left_subframe = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]];
806 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000807 break;
808 default:
809 assert(0);
810 }
Josh Coalson82b73242001-03-28 22:17:05 +0000811
812 switch(channel_assignment) {
813 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
814 left_bps = encoder->guts->subframe_bps [0];
815 right_bps = encoder->guts->subframe_bps [1];
816 break;
817 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
818 left_bps = encoder->guts->subframe_bps [0];
819 right_bps = encoder->guts->subframe_bps_mid_side[1];
820 break;
821 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
822 left_bps = encoder->guts->subframe_bps_mid_side[1];
823 right_bps = encoder->guts->subframe_bps [1];
824 break;
825 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
826 left_bps = encoder->guts->subframe_bps_mid_side[0];
827 right_bps = encoder->guts->subframe_bps_mid_side[1];
828 break;
829 default:
830 assert(0);
831 }
832
833 /* note that encoder_add_subframe_ sets the state for us in case of an error */
834 if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame))
835 return false;
836 if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame))
837 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000838 }
839 else {
840 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
841 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
842 return false;
843 }
844
845 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000846 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 +0000847 /* the above function sets the state for us in case of an error */
848 return false;
849 }
850 }
851 }
852
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000853 if(encoder->loose_mid_side_stereo) {
854 encoder->guts->loose_mid_side_stereo_frame_count++;
855 if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
856 encoder->guts->loose_mid_side_stereo_frame_count = 0;
857 }
858
859 encoder->guts->last_channel_assignment = frame_header.channel_assignment;
860
Josh Coalson94e02cd2001-01-25 10:41:06 +0000861 return true;
862}
863
Josh Coalson60f77d72001-04-25 02:16:36 +0000864bool 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 +0000865{
866 real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
867 real lpc_residual_bits_per_sample;
868 real autoc[FLAC__MAX_LPC_ORDER+1];
869 real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
870 real lpc_error[FLAC__MAX_LPC_ORDER];
871 unsigned min_lpc_order, max_lpc_order, lpc_order;
872 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
873 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
874 unsigned rice_parameter;
875 unsigned _candidate_bits, _best_bits;
876 unsigned _best_subframe;
877
878 /* verbatim subframe is the baseline against which we measure other compressed subframes */
879 _best_subframe = 0;
Josh Coalson82b73242001-03-28 22:17:05 +0000880 _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000881
882 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
883 /* check for constant subframe */
Josh Coalsoneef56702001-03-30 00:45:22 +0000884 if(encoder->guts->use_slow)
885 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);
886 else
Josh Coalsoncf30f502001-05-23 20:57:44 +0000887 guess_fixed_order = encoder->guts->local_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 +0000888 if(fixed_residual_bits_per_sample[1] == 0.0) {
889 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
890 unsigned i, signal_is_constant = true;
891 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
892 if(integer_signal[0] != integer_signal[i]) {
893 signal_is_constant = false;
894 break;
895 }
896 }
897 if(signal_is_constant) {
Josh Coalson82b73242001-03-28 22:17:05 +0000898 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000899 if(_candidate_bits < _best_bits) {
900 _best_subframe = !_best_subframe;
901 _best_bits = _candidate_bits;
902 }
903 }
904 }
905 else {
906 /* encode fixed */
907 if(encoder->do_exhaustive_model_search) {
908 min_fixed_order = 0;
909 max_fixed_order = FLAC__MAX_FIXED_ORDER;
910 }
911 else {
912 min_fixed_order = max_fixed_order = guess_fixed_order;
913 }
914 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000915 if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000916 continue; /* don't even try */
Josh Coalson46f2ae82001-02-08 00:27:21 +0000917 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 +0000918#ifndef FLAC__SYMMETRIC_RICE
Josh Coalson46f2ae82001-02-08 00:27:21 +0000919 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000920#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000921 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
922 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +0000923 _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 +0000924 if(_candidate_bits < _best_bits) {
925 _best_subframe = !_best_subframe;
926 _best_bits = _candidate_bits;
927 }
928 }
929
930 /* encode lpc */
931 if(encoder->max_lpc_order > 0) {
932 if(encoder->max_lpc_order >= frame_header->blocksize)
933 max_lpc_order = frame_header->blocksize-1;
934 else
935 max_lpc_order = encoder->max_lpc_order;
936 if(max_lpc_order > 0) {
Josh Coalsoncf30f502001-05-23 20:57:44 +0000937 encoder->guts->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000938 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
939 if(autoc[0] != 0.0) {
940 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
941 if(encoder->do_exhaustive_model_search) {
942 min_lpc_order = 1;
943 }
944 else {
Josh Coalson82b73242001-03-28 22:17:05 +0000945 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 +0000946 min_lpc_order = max_lpc_order = guess_lpc_order;
947 }
948 if(encoder->do_qlp_coeff_prec_search) {
949 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalson82b73242001-03-28 22:17:05 +0000950 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 +0000951 }
952 else {
953 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
954 }
955 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
956 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 +0000957 if(lpc_residual_bits_per_sample >= (real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000958 continue; /* don't even try */
959 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 +0000960#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000961 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000962#endif
Josh Coalson034dfab2001-04-27 19:10:23 +0000963 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
964 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000965 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 +0000966 _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 +0000967 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
968 if(_candidate_bits < _best_bits) {
969 _best_subframe = !_best_subframe;
970 _best_bits = _candidate_bits;
971 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000972 }
973 }
974 }
975 }
976 }
977 }
978 }
979 }
980
981 *best_subframe = _best_subframe;
982 *best_bits = _best_bits;
983
984 return true;
985}
986
Josh Coalson82b73242001-03-28 22:17:05 +0000987bool 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 +0000988{
989 switch(subframe->type) {
990 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +0000991 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000992 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
993 return false;
994 }
995 break;
996 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +0000997 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 +0000998 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
999 return false;
1000 }
1001 break;
1002 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00001003 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 +00001004 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1005 return false;
1006 }
1007 break;
1008 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00001009 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001010 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1011 return false;
1012 }
1013 break;
1014 default:
1015 assert(0);
1016 }
1017
1018 return true;
1019}
1020
Josh Coalson82b73242001-03-28 22:17:05 +00001021unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001022{
1023 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1024 subframe->data.constant.value = signal;
1025
Josh Coalson82b73242001-03-28 22:17:05 +00001026 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 +00001027}
1028
Josh Coalson60f77d72001-04-25 02:16:36 +00001029unsigned 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 +00001030{
1031 unsigned i, residual_bits;
1032 const unsigned residual_samples = blocksize - order;
1033
1034 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1035
1036 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1037
1038 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1039 subframe->data.fixed.residual = residual;
1040
Josh Coalson60f77d72001-04-25 02:16:36 +00001041 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 +00001042
1043 subframe->data.fixed.order = order;
1044 for(i = 0; i < order; i++)
1045 subframe->data.fixed.warmup[i] = signal[i];
1046
Josh Coalson82b73242001-03-28 22:17:05 +00001047 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 +00001048}
1049
Josh Coalson60f77d72001-04-25 02:16:36 +00001050unsigned 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 +00001051{
1052 int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1053 unsigned i, residual_bits;
1054 int quantization, ret;
1055 const unsigned residual_samples = blocksize - order;
1056
Josh Coalson82b73242001-03-28 22:17:05 +00001057 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001058 if(ret != 0)
1059 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1060
1061 FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1062
1063 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1064
1065 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1066 subframe->data.lpc.residual = residual;
1067
Josh Coalson60f77d72001-04-25 02:16:36 +00001068 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 +00001069
1070 subframe->data.lpc.order = order;
1071 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1072 subframe->data.lpc.quantization_level = quantization;
1073 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1074 for(i = 0; i < order; i++)
1075 subframe->data.lpc.warmup[i] = signal[i];
1076
Josh Coalson82b73242001-03-28 22:17:05 +00001077 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 +00001078}
1079
Josh Coalson82b73242001-03-28 22:17:05 +00001080unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001081{
1082 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1083
1084 subframe->data.verbatim.data = signal;
1085
Josh Coalson82b73242001-03-28 22:17:05 +00001086 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 +00001087}
1088
Josh Coalson60f77d72001-04-25 02:16:36 +00001089unsigned 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 +00001090{
Josh Coalson94e02cd2001-01-25 10:41:06 +00001091 int32 r;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001092#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalsonafcd8772001-04-18 22:59:25 +00001093 unsigned sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001094 int partition_order;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001095#else
1096 unsigned partition_order;
1097#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001098 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001099 unsigned residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001100 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 +00001101
Josh Coalson2051dd42001-04-12 22:22:34 +00001102 /* compute abs(residual) for use later */
1103 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1104 r = residual[residual_sample];
1105 abs_residual[residual_sample] = (uint32)(r<0? -r : r);
1106 }
1107
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001108#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001109 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);
1110 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001111
Josh Coalson60f77d72001-04-25 02:16:36 +00001112 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1113 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 +00001114 assert(0); /* encoder_precompute_partition_info_ should keep this from ever happening */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001115 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001116 sum += 1u << partition_order;
1117 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1118 best_residual_bits = residual_bits;
1119 *best_partition_order = partition_order;
1120 best_parameters_index = !best_parameters_index;
1121 }
1122 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001123#else
Josh Coalson60f77d72001-04-25 02:16:36 +00001124 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1125 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 +00001126 assert(best_residual_bits != 0);
1127 break;
1128 }
1129 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1130 best_residual_bits = residual_bits;
1131 *best_partition_order = partition_order;
1132 best_parameters_index = !best_parameters_index;
1133 }
1134 }
1135#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001136 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1137 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1138
1139 return best_residual_bits;
1140}
1141
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001142#if (defined FLAC__PRECOMPUTE_PARTITION_SUMS) || (defined FLAC__SEARCH_FOR_ESCAPES)
Josh Coalson60f77d72001-04-25 02:16:36 +00001143unsigned 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 +00001144{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001145 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001146 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001147 const unsigned blocksize = residual_samples + predictor_order;
1148
Josh Coalsonaef013c2001-04-24 01:25:42 +00001149 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001150 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001151#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001152 uint32 abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001153#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001154#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001155 uint32 abs_residual_partition_max;
1156 unsigned abs_residual_partition_max_index = 0; /* initialized to silence superfluous compiler warning */
1157#endif
1158 uint32 abs_r;
1159 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001160 const unsigned partitions = 1u << partition_order;
1161 const unsigned default_partition_samples = blocksize >> partition_order;
1162
1163 if(default_partition_samples <= predictor_order) {
1164 assert(max_partition_order > 0);
1165 max_partition_order--;
1166 }
1167 else {
1168 for(partition = residual_sample = 0; partition < partitions; partition++) {
1169 partition_samples = default_partition_samples;
1170 if(partition == 0)
1171 partition_samples -= predictor_order;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001172#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001173 abs_residual_partition_sum = 0;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001174#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001175#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001176 abs_residual_partition_max = 0;
1177#endif
1178 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1179 abs_r = abs_residual[residual_sample];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001180#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001181 abs_residual_partition_sum += abs_r; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */
1182#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001183#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001184 if(abs_r > abs_residual_partition_max) {
1185 abs_residual_partition_max = abs_r;
1186 abs_residual_partition_max_index = residual_sample;
1187 }
1188#endif
1189 residual_sample++;
1190 }
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001191#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001192 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001193#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001194#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001195 if(abs_residual_partition_max > 0)
1196 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(residual[abs_residual_partition_max_index]);
1197 else
1198 raw_bits_per_partition[partition] = FLAC__bitmath_silog2(0);
1199#endif
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001200 }
Josh Coalsonaef013c2001-04-24 01:25:42 +00001201 to_partition = partitions;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001202 break;
1203 }
1204 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00001205
Josh Coalsonaef013c2001-04-24 01:25:42 +00001206 /* now merge for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00001207 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001208#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001209 uint32 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001210#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001211#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001212 unsigned m;
1213#endif
1214 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001215 const unsigned partitions = 1u << partition_order;
1216 for(i = 0; i < partitions; i++) {
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001217#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001218 s = abs_residual_partition_sums[from_partition];
1219#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001220#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001221 m = raw_bits_per_partition[from_partition];
1222#endif
1223 from_partition++;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001224#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalsonaef013c2001-04-24 01:25:42 +00001225 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
1226#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001227#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001228 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1229#endif
1230 from_partition++;
1231 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001232 }
1233 }
1234
Josh Coalsonf76a3612001-04-18 02:28:11 +00001235 return max_partition_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001236}
Josh Coalsonafcd8772001-04-18 22:59:25 +00001237#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001238
Josh Coalson352e0f62001-03-20 22:55:50 +00001239#ifdef VARIABLE_RICE_BITS
1240#undef VARIABLE_RICE_BITS
1241#endif
1242#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1243
Josh Coalson034dfab2001-04-27 19:10:23 +00001244bool 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 +00001245{
Josh Coalson034dfab2001-04-27 19:10:23 +00001246 unsigned rice_parameter, partition_bits;
1247#ifndef NO_RICE_SEARCH
1248 unsigned best_partition_bits;
1249 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1250#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001251#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalsonaef013c2001-04-24 01:25:42 +00001252 unsigned flat_bits;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001253#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001254 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1255
Josh Coalson034dfab2001-04-27 19:10:23 +00001256 assert(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00001257
Josh Coalson94e02cd2001-01-25 10:41:06 +00001258 if(partition_order == 0) {
1259 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00001260
Josh Coalson034dfab2001-04-27 19:10:23 +00001261#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001262 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001263 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001264 min_rice_parameter = 0;
1265 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001266 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1267 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson60f77d72001-04-25 02:16:36 +00001268 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1269 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1270 }
1271 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001272 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001273
Josh Coalson034dfab2001-04-27 19:10:23 +00001274 best_partition_bits = 0xffffffff;
1275 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1276#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001277#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001278#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001279 partition_bits = (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001280#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001281 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00001282 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001283#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001284#else
1285 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001286#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001287 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001288 for(i = 0; i < residual_samples; i++) {
1289#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001290#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001291 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001292#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001293 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001294#endif
1295#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001296 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 +00001297#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001298 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001299#ifndef NO_RICE_SEARCH
1300 if(partition_bits < best_partition_bits) {
1301 best_rice_parameter = rice_parameter;
1302 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00001303 }
1304 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001305#endif
1306#ifdef FLAC__SEARCH_FOR_ESCAPES
1307 flat_bits = raw_bits_per_partition[0] * residual_samples;
1308 if(flat_bits <= best_partition_bits) {
1309 raw_bits[0] = raw_bits_per_partition[0];
1310 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1311 best_partition_bits = flat_bits;
1312 }
1313#endif
1314 parameters[0] = best_rice_parameter;
1315 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001316 }
1317 else {
Josh Coalson034dfab2001-04-27 19:10:23 +00001318 unsigned partition, j, save_j, k;
1319 unsigned mean, partition_samples;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001320 const unsigned partitions = 1u << partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00001321 for(partition = j = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001322 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00001323 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001324 if(partition_samples <= predictor_order)
1325 return false;
1326 else
1327 partition_samples -= predictor_order;
1328 }
1329 mean = partition_samples >> 1;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001330#ifdef FLAC__PRECOMPUTE_PARTITION_SUMS
Josh Coalson034dfab2001-04-27 19:10:23 +00001331 mean += abs_residual_partition_sums[partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00001332#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001333 save_j = j;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001334 for(k = 0; k < partition_samples; j++, k++)
1335 mean += abs_residual[j];
Josh Coalson034dfab2001-04-27 19:10:23 +00001336 j = save_j;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001337#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001338 mean /= partition_samples;
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001339#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001340 /* calc rice_parameter = floor(log2(mean)) */
1341 rice_parameter = 0;
1342 mean>>=1;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001343 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001344 rice_parameter++;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001345 mean >>= 1;
1346 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001347#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001348 /* calc rice_parameter = floor(log2(mean)) + 1 */
1349 rice_parameter = 0;
Josh Coalson352e0f62001-03-20 22:55:50 +00001350 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001351 rice_parameter++;
Josh Coalson352e0f62001-03-20 22:55:50 +00001352 mean >>= 1;
1353 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001354#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001355 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1356 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson60f77d72001-04-25 02:16:36 +00001357
Josh Coalson034dfab2001-04-27 19:10:23 +00001358#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001359 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001360 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001361 min_rice_parameter = 0;
1362 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001363 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1364 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson60f77d72001-04-25 02:16:36 +00001365 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1366 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1367 }
1368 else
1369 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00001370
Josh Coalson034dfab2001-04-27 19:10:23 +00001371 best_partition_bits = 0xffffffff;
1372 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1373#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001374#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001375#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001376 partition_bits = (2+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001377#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001378 const unsigned rice_parameter_estimate = rice_parameter-1;
1379 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001380#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001381#else
1382 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001383#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001384 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1385 save_j = j;
1386 for(k = 0; k < partition_samples; j++, k++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00001387#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001388#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001389 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001390#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001391 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001392#endif
1393#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001394 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 +00001395#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001396 }
1397 if(rice_parameter != max_rice_parameter)
1398 j = save_j;
1399#ifndef NO_RICE_SEARCH
1400 if(partition_bits < best_partition_bits) {
1401 best_rice_parameter = rice_parameter;
1402 best_partition_bits = partition_bits;
1403 }
Josh Coalson2051dd42001-04-12 22:22:34 +00001404 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001405#endif
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001406#ifdef FLAC__SEARCH_FOR_ESCAPES
Josh Coalson034dfab2001-04-27 19:10:23 +00001407 flat_bits = raw_bits_per_partition[partition] * partition_samples;
1408 if(flat_bits <= best_partition_bits) {
1409 raw_bits[partition] = raw_bits_per_partition[partition];
1410 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1411 best_partition_bits = flat_bits;
Josh Coalson2051dd42001-04-12 22:22:34 +00001412 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001413#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001414 parameters[partition] = best_rice_parameter;
1415 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001416 }
1417 }
1418
1419 *bits = bits_;
1420 return true;
1421}
Josh Coalson859bc542001-03-27 22:22:27 +00001422
Josh Coalson8e92f212001-05-01 18:50:45 +00001423unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00001424{
1425 unsigned i, shift;
1426 int32 x = 0;
1427
1428 for(i = 0; i < samples && !(x&1); i++)
1429 x |= signal[i];
1430
1431 if(x == 0) {
1432 shift = 0;
1433 }
1434 else {
1435 for(shift = 0; !(x&1); shift++)
1436 x >>= 1;
1437 }
1438
1439 if(shift > 0) {
1440 for(i = 0; i < samples; i++)
1441 signal[i] >>= shift;
1442 }
1443
1444 return shift;
1445}