blob: 099d0767b05098ae106c4f3d8a8d86265bf801ee [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 Coalsonbb7f6b92000-12-10 04:09:52 +000033
34#ifdef min
35#undef min
36#endif
37#define min(x,y) ((x)<(y)?(x):(y))
38
39#ifdef max
40#undef max
41#endif
42#define max(x,y) ((x)>(y)?(x):(y))
43
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000044typedef struct FLAC__EncoderPrivate {
45 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
46 int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
47 int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
48 real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
49 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 +000050 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 +000051 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 +000052 int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
53 int32 *residual_workspace_mid_side[2][2];
54 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
55 FLAC__Subframe subframe_workspace_mid_side[2][2];
56 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
57 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
58 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
59 unsigned best_subframe_mid_side[2];
60 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
61 unsigned best_subframe_bits_mid_side[2];
Josh Coalson2051dd42001-04-12 22:22:34 +000062 uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +000063 uint32 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
Josh Coalson2051dd42001-04-12 22:22:34 +000064 unsigned *bits_per_residual_sample; /* workspace where silog2(candidate residual) is stored */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000065 FLAC__BitBuffer frame; /* the current frame being worked on */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000066 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 +000067 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
68 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
69 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
70 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000071 FLAC__StreamMetaData metadata;
72 unsigned current_sample_number;
73 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +000074 struct MD5Context md5context;
Josh Coalsoneef56702001-03-30 00:45:22 +000075 bool use_slow; /* use slow 64-bit versions of some functions */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000076 FLAC__EncoderWriteStatus (*write_callback)(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
77 void (*metadata_callback)(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
78 void *client_data;
79} FLAC__EncoderPrivate;
80
81static bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size);
82static bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson94e02cd2001-01-25 10:41:06 +000083static bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame);
Josh Coalson82b73242001-03-28 22:17:05 +000084static bool encoder_process_subframe_(FLAC__Encoder *encoder, 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);
85static bool encoder_add_subframe_(FLAC__Encoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
86static unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +000087static unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned bits_per_residual_sample[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe);
88static unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned bits_per_residual_sample[], const real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe);
Josh Coalson82b73242001-03-28 22:17:05 +000089static unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +000090static unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned bits_per_residual_sample[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned max_partition_order, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]);
Josh Coalsonf76a3612001-04-18 02:28:11 +000091static unsigned encoder_precompute_partition_sums_(uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned max_partition_order);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +000092static bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 abs_residual_partition_sums[], const unsigned bits_per_residual_sample[], const unsigned residual_samples, const unsigned predictor_order, unsigned rice_parameter, const unsigned partition_order, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
Josh Coalson859bc542001-03-27 22:22:27 +000093static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000094
Josh Coalsoncbf595f2000-12-22 22:35:33 +000095const char *FLAC__EncoderWriteStatusString[] = {
96 "FLAC__ENCODER_WRITE_OK",
97 "FLAC__ENCODER_WRITE_FATAL_ERROR"
98};
99
100const char *FLAC__EncoderStateString[] = {
101 "FLAC__ENCODER_OK",
102 "FLAC__ENCODER_UNINITIALIZED",
103 "FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS",
104 "FLAC__ENCODER_INVALID_BITS_PER_SAMPLE",
105 "FLAC__ENCODER_INVALID_SAMPLE_RATE",
106 "FLAC__ENCODER_INVALID_BLOCK_SIZE",
107 "FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION",
108 "FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH",
109 "FLAC__ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
Josh Coalson69f1ee02001-01-24 00:54:43 +0000110 "FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000111 "FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
112 "FLAC__ENCODER_NOT_STREAMABLE",
113 "FLAC__ENCODER_FRAMING_ERROR",
114 "FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING",
115 "FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING",
116 "FLAC__ENCODER_MEMORY_ALLOCATION_ERROR"
117};
118
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000119
120bool encoder_resize_buffers_(FLAC__Encoder *encoder, unsigned new_size)
121{
122 bool ok;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000123 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000124 int32 *previous_is, *current_is;
125 real *previous_rs, *current_rs;
126 int32 *residual;
Josh Coalsone77287e2001-01-20 01:27:55 +0000127 uint32 *abs_residual;
Josh Coalson2051dd42001-04-12 22:22:34 +0000128 unsigned *bits_per_residual_sample;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000129
130 assert(new_size > 0);
131 assert(encoder->state == FLAC__ENCODER_OK);
132 assert(encoder->guts->current_sample_number == 0);
133
134 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
135 if(new_size <= encoder->guts->input_capacity)
136 return true;
137
138 ok = 1;
139 if(ok) {
140 for(i = 0; ok && i < encoder->channels; i++) {
141 /* integer version of the signal */
142 previous_is = encoder->guts->integer_signal[i];
143 current_is = (int32*)malloc(sizeof(int32) * new_size);
144 if(0 == current_is) {
145 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
146 ok = 0;
147 }
148 else {
149 encoder->guts->integer_signal[i] = current_is;
150 if(previous_is != 0)
151 free(previous_is);
152 }
153 /* real version of the signal */
154 previous_rs = encoder->guts->real_signal[i];
155 current_rs = (real*)malloc(sizeof(real) * new_size);
156 if(0 == current_rs) {
157 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
158 ok = 0;
159 }
160 else {
161 encoder->guts->real_signal[i] = current_rs;
162 if(previous_rs != 0)
163 free(previous_rs);
164 }
165 }
166 }
167 if(ok) {
168 for(i = 0; ok && i < 2; i++) {
169 /* integer version of the signal */
170 previous_is = encoder->guts->integer_signal_mid_side[i];
171 current_is = (int32*)malloc(sizeof(int32) * new_size);
172 if(0 == current_is) {
173 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
174 ok = 0;
175 }
176 else {
177 encoder->guts->integer_signal_mid_side[i] = current_is;
178 if(previous_is != 0)
179 free(previous_is);
180 }
181 /* real version of the signal */
182 previous_rs = encoder->guts->real_signal_mid_side[i];
183 current_rs = (real*)malloc(sizeof(real) * new_size);
184 if(0 == current_rs) {
185 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
186 ok = 0;
187 }
188 else {
189 encoder->guts->real_signal_mid_side[i] = current_rs;
190 if(previous_rs != 0)
191 free(previous_rs);
192 }
193 }
194 }
195 if(ok) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000196 for(channel = 0; channel < encoder->channels; channel++) {
197 for(i = 0; i < 2; i++) {
198 residual = (int32*)malloc(sizeof(int32) * new_size);
199 if(0 == residual) {
200 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
201 ok = 0;
202 }
203 else {
204 if(encoder->guts->residual_workspace[channel][i] != 0)
205 free(encoder->guts->residual_workspace[channel][i]);
206 encoder->guts->residual_workspace[channel][i] = residual;
207 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000208 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000209 }
210 for(channel = 0; channel < 2; channel++) {
211 for(i = 0; i < 2; i++) {
212 residual = (int32*)malloc(sizeof(int32) * new_size);
213 if(0 == residual) {
214 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
215 ok = 0;
216 }
217 else {
218 if(encoder->guts->residual_workspace_mid_side[channel][i] != 0)
219 free(encoder->guts->residual_workspace_mid_side[channel][i]);
220 encoder->guts->residual_workspace_mid_side[channel][i] = residual;
221 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000222 }
223 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000224 abs_residual = (uint32*)malloc(sizeof(uint32) * new_size);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000225 if(0 == abs_residual) {
Josh Coalsone77287e2001-01-20 01:27:55 +0000226 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
227 ok = 0;
228 }
229 else {
230 if(encoder->guts->abs_residual != 0)
231 free(encoder->guts->abs_residual);
232 encoder->guts->abs_residual = abs_residual;
233 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000234 abs_residual = (uint32*)malloc(sizeof(uint32) * (new_size * 2));
235 if(0 == abs_residual) {
236 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
237 ok = 0;
238 }
239 else {
240 if(encoder->guts->abs_residual_partition_sums != 0)
241 free(encoder->guts->abs_residual_partition_sums);
242 encoder->guts->abs_residual_partition_sums = abs_residual;
243 }
Josh Coalson2051dd42001-04-12 22:22:34 +0000244 bits_per_residual_sample = (unsigned*)malloc(sizeof(unsigned) * new_size);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000245 if(0 == bits_per_residual_sample) {
Josh Coalson2051dd42001-04-12 22:22:34 +0000246 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
247 ok = 0;
248 }
249 else {
250 if(encoder->guts->bits_per_residual_sample != 0)
251 free(encoder->guts->bits_per_residual_sample);
252 encoder->guts->bits_per_residual_sample = bits_per_residual_sample;
253 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000254 }
255 if(ok)
256 encoder->guts->input_capacity = new_size;
257
258 return ok;
259}
260
261FLAC__Encoder *FLAC__encoder_get_new_instance()
262{
263 FLAC__Encoder *encoder = (FLAC__Encoder*)malloc(sizeof(FLAC__Encoder));
264 if(encoder != 0) {
265 encoder->state = FLAC__ENCODER_UNINITIALIZED;
266 encoder->guts = 0;
267 }
268 return encoder;
269}
270
271void FLAC__encoder_free_instance(FLAC__Encoder *encoder)
272{
273 assert(encoder != 0);
274 free(encoder);
275}
276
277FLAC__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)
278{
279 unsigned i;
Josh Coalsonc692d382001-02-23 21:05:05 +0000280 FLAC__StreamMetaData padding;
Josh Coalson0a72e182001-04-13 19:17:16 +0000281 FLAC__StreamMetaData seek_table;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000282
283 assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
284 assert(encoder != 0);
285 assert(write_callback != 0);
286 assert(metadata_callback != 0);
287 assert(encoder->state == FLAC__ENCODER_UNINITIALIZED);
288 assert(encoder->guts == 0);
289
290 encoder->state = FLAC__ENCODER_OK;
291
292 if(encoder->channels == 0 || encoder->channels > FLAC__MAX_CHANNELS)
293 return encoder->state = FLAC__ENCODER_INVALID_NUMBER_OF_CHANNELS;
294
295 if(encoder->do_mid_side_stereo && encoder->channels != 2)
296 return encoder->state = FLAC__ENCODER_MID_SIDE_CHANNELS_MISMATCH;
297
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000298 if(encoder->loose_mid_side_stereo && !encoder->do_mid_side_stereo)
Josh Coalson69f1ee02001-01-24 00:54:43 +0000299 return encoder->state = FLAC__ENCODER_ILLEGAL_MID_SIDE_FORCE;
300
Josh Coalson82b73242001-03-28 22:17:05 +0000301 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 +0000302 return encoder->state = FLAC__ENCODER_INVALID_BITS_PER_SAMPLE;
303
304 if(encoder->sample_rate == 0 || encoder->sample_rate > FLAC__MAX_SAMPLE_RATE)
305 return encoder->state = FLAC__ENCODER_INVALID_SAMPLE_RATE;
306
307 if(encoder->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->blocksize > FLAC__MAX_BLOCK_SIZE)
308 return encoder->state = FLAC__ENCODER_INVALID_BLOCK_SIZE;
309
310 if(encoder->blocksize < encoder->max_lpc_order)
311 return encoder->state = FLAC__ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
312
313 if(encoder->qlp_coeff_precision == 0) {
314 if(encoder->bits_per_sample < 16) {
315 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
316 /* @@@ until then we'll make a guess */
317 encoder->qlp_coeff_precision = max(5, 2 + encoder->bits_per_sample / 2);
318 }
319 else if(encoder->bits_per_sample == 16) {
320 if(encoder->blocksize <= 192)
321 encoder->qlp_coeff_precision = 7;
322 else if(encoder->blocksize <= 384)
323 encoder->qlp_coeff_precision = 8;
324 else if(encoder->blocksize <= 576)
325 encoder->qlp_coeff_precision = 9;
326 else if(encoder->blocksize <= 1152)
327 encoder->qlp_coeff_precision = 10;
328 else if(encoder->blocksize <= 2304)
329 encoder->qlp_coeff_precision = 11;
330 else if(encoder->blocksize <= 4608)
331 encoder->qlp_coeff_precision = 12;
332 else
333 encoder->qlp_coeff_precision = 13;
334 }
335 else {
336 encoder->qlp_coeff_precision = min(13, 8*sizeof(int32) - encoder->bits_per_sample - 1);
337 }
338 }
Josh Coalson0552fdf2001-02-26 20:29:56 +0000339 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 +0000340 return encoder->state = FLAC__ENCODER_INVALID_QLP_COEFF_PRECISION;
341
342 if(encoder->streamable_subset) {
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000343 //@@@ add check for blocksize here
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000344 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)
345 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
346 if(encoder->sample_rate > 655350)
347 return encoder->state = FLAC__ENCODER_NOT_STREAMABLE;
348 }
349
350 if(encoder->rice_optimization_level >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
351 encoder->rice_optimization_level = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
352
353 encoder->guts = (FLAC__EncoderPrivate*)malloc(sizeof(FLAC__EncoderPrivate));
354 if(encoder->guts == 0)
355 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
356
357 encoder->guts->input_capacity = 0;
358 for(i = 0; i < encoder->channels; i++) {
359 encoder->guts->integer_signal[i] = 0;
360 encoder->guts->real_signal[i] = 0;
361 }
362 for(i = 0; i < 2; i++) {
363 encoder->guts->integer_signal_mid_side[i] = 0;
364 encoder->guts->real_signal_mid_side[i] = 0;
365 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000366 for(i = 0; i < encoder->channels; i++) {
367 encoder->guts->residual_workspace[i][0] = encoder->guts->residual_workspace[i][1] = 0;
368 encoder->guts->best_subframe[i] = 0;
369 }
370 for(i = 0; i < 2; i++) {
371 encoder->guts->residual_workspace_mid_side[i][0] = encoder->guts->residual_workspace_mid_side[i][1] = 0;
372 encoder->guts->best_subframe_mid_side[i] = 0;
373 }
374 for(i = 0; i < encoder->channels; i++) {
375 encoder->guts->subframe_workspace_ptr[i][0] = &encoder->guts->subframe_workspace[i][0];
376 encoder->guts->subframe_workspace_ptr[i][1] = &encoder->guts->subframe_workspace[i][1];
377 }
378 for(i = 0; i < 2; i++) {
379 encoder->guts->subframe_workspace_ptr_mid_side[i][0] = &encoder->guts->subframe_workspace_mid_side[i][0];
380 encoder->guts->subframe_workspace_ptr_mid_side[i][1] = &encoder->guts->subframe_workspace_mid_side[i][1];
381 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000382 encoder->guts->abs_residual = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000383 encoder->guts->abs_residual_partition_sums = 0;
Josh Coalson2051dd42001-04-12 22:22:34 +0000384 encoder->guts->bits_per_residual_sample = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000385 encoder->guts->current_frame_can_do_mid_side = true;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000386 encoder->guts->loose_mid_side_stereo_frames_exact = (double)encoder->sample_rate * 0.4 / (double)encoder->blocksize;
387 encoder->guts->loose_mid_side_stereo_frames = (unsigned)(encoder->guts->loose_mid_side_stereo_frames_exact + 0.5);
388 if(encoder->guts->loose_mid_side_stereo_frames == 0)
389 encoder->guts->loose_mid_side_stereo_frames = 1;
390 encoder->guts->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000391 encoder->guts->current_sample_number = 0;
392 encoder->guts->current_frame_number = 0;
393
Josh Coalsoneef56702001-03-30 00:45:22 +0000394 if(encoder->bits_per_sample + FLAC__bitmath_ilog2(encoder->blocksize)+1 > 30)
395 encoder->guts->use_slow = true;
396 else
397 encoder->guts->use_slow = false;
398
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000399 if(!encoder_resize_buffers_(encoder, encoder->blocksize)) {
400 /* the above function sets the state for us in case of an error */
401 return encoder->state;
402 }
403 FLAC__bitbuffer_init(&encoder->guts->frame);
404 encoder->guts->write_callback = write_callback;
405 encoder->guts->metadata_callback = metadata_callback;
406 encoder->guts->client_data = client_data;
407
408 /*
409 * write the stream header
410 */
411 if(!FLAC__bitbuffer_clear(&encoder->guts->frame))
412 return encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
413
414 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
415 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
416
Josh Coalsonc692d382001-02-23 21:05:05 +0000417 encoder->guts->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalson0a72e182001-04-13 19:17:16 +0000418 encoder->guts->metadata.is_last = (encoder->seek_table == 0 && encoder->padding == 0);
Josh Coalsonc692d382001-02-23 21:05:05 +0000419 encoder->guts->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
420 encoder->guts->metadata.data.stream_info.min_blocksize = encoder->blocksize; /* this encoder uses the same blocksize for the whole stream */
421 encoder->guts->metadata.data.stream_info.max_blocksize = encoder->blocksize;
422 encoder->guts->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
423 encoder->guts->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
424 encoder->guts->metadata.data.stream_info.sample_rate = encoder->sample_rate;
425 encoder->guts->metadata.data.stream_info.channels = encoder->channels;
426 encoder->guts->metadata.data.stream_info.bits_per_sample = encoder->bits_per_sample;
427 encoder->guts->metadata.data.stream_info.total_samples = encoder->total_samples_estimate; /* we will replace this later with the real total */
428 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 +0000429 MD5Init(&encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000430 if(!FLAC__add_metadata_block(&encoder->guts->metadata, &encoder->guts->frame))
431 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
432
Josh Coalson0a72e182001-04-13 19:17:16 +0000433 if(0 != encoder->seek_table) {
434 if(!FLAC__seek_table_is_valid(encoder->seek_table))
435 return encoder->state = FLAC__ENCODER_INVALID_SEEK_TABLE;
436 seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
437 seek_table.is_last = (encoder->padding == 0);
438 seek_table.length = encoder->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
439 seek_table.data.seek_table = *encoder->seek_table;
440 if(!FLAC__add_metadata_block(&seek_table, &encoder->guts->frame))
441 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
442 }
443
Josh Coalsonc692d382001-02-23 21:05:05 +0000444 /* add a PADDING block if requested */
445 if(encoder->padding > 0) {
446 padding.type = FLAC__METADATA_TYPE_PADDING;
447 padding.is_last = true;
448 padding.length = encoder->padding;
449 if(!FLAC__add_metadata_block(&padding, &encoder->guts->frame))
450 return encoder->state = FLAC__ENCODER_FRAMING_ERROR;
451 }
452
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000453 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned before writing */
454 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
455 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)
456 return encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
457
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000458 /* now that the metadata block is written, we can init this to an absurdly-high value... */
Josh Coalsonc692d382001-02-23 21:05:05 +0000459 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 +0000460 /* ... and clear this to 0 */
Josh Coalsonc692d382001-02-23 21:05:05 +0000461 encoder->guts->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000462
463 return encoder->state;
464}
465
466void FLAC__encoder_finish(FLAC__Encoder *encoder)
467{
Josh Coalson94e02cd2001-01-25 10:41:06 +0000468 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000469
470 assert(encoder != 0);
471 if(encoder->state == FLAC__ENCODER_UNINITIALIZED)
472 return;
473 if(encoder->guts->current_sample_number != 0) {
474 encoder->blocksize = encoder->guts->current_sample_number;
475 encoder_process_frame_(encoder, true); /* true => is last frame */
476 }
Josh Coalsonc692d382001-02-23 21:05:05 +0000477 MD5Final(encoder->guts->metadata.data.stream_info.md5sum, &encoder->guts->md5context);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000478 encoder->guts->metadata_callback(encoder, &encoder->guts->metadata, encoder->guts->client_data);
479 if(encoder->guts != 0) {
480 for(i = 0; i < encoder->channels; i++) {
481 if(encoder->guts->integer_signal[i] != 0) {
482 free(encoder->guts->integer_signal[i]);
483 encoder->guts->integer_signal[i] = 0;
484 }
485 if(encoder->guts->real_signal[i] != 0) {
486 free(encoder->guts->real_signal[i]);
487 encoder->guts->real_signal[i] = 0;
488 }
489 }
490 for(i = 0; i < 2; i++) {
491 if(encoder->guts->integer_signal_mid_side[i] != 0) {
492 free(encoder->guts->integer_signal_mid_side[i]);
493 encoder->guts->integer_signal_mid_side[i] = 0;
494 }
495 if(encoder->guts->real_signal_mid_side[i] != 0) {
496 free(encoder->guts->real_signal_mid_side[i]);
497 encoder->guts->real_signal_mid_side[i] = 0;
498 }
499 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000500 for(channel = 0; channel < encoder->channels; channel++) {
501 for(i = 0; i < 2; i++) {
502 if(encoder->guts->residual_workspace[channel][i] != 0) {
503 free(encoder->guts->residual_workspace[channel][i]);
504 encoder->guts->residual_workspace[channel][i] = 0;
505 }
506 }
507 }
508 for(channel = 0; channel < 2; channel++) {
509 for(i = 0; i < 2; i++) {
510 if(encoder->guts->residual_workspace_mid_side[channel][i] != 0) {
511 free(encoder->guts->residual_workspace_mid_side[channel][i]);
512 encoder->guts->residual_workspace_mid_side[channel][i] = 0;
513 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000514 }
515 }
Josh Coalsone77287e2001-01-20 01:27:55 +0000516 if(encoder->guts->abs_residual != 0) {
517 free(encoder->guts->abs_residual);
518 encoder->guts->abs_residual = 0;
519 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000520 if(encoder->guts->abs_residual_partition_sums != 0) {
521 free(encoder->guts->abs_residual_partition_sums);
522 encoder->guts->abs_residual_partition_sums = 0;
523 }
Josh Coalson2051dd42001-04-12 22:22:34 +0000524 if(encoder->guts->bits_per_residual_sample != 0) {
525 free(encoder->guts->bits_per_residual_sample);
526 encoder->guts->bits_per_residual_sample = 0;
527 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000528 FLAC__bitbuffer_free(&encoder->guts->frame);
529 free(encoder->guts);
530 encoder->guts = 0;
531 }
532 encoder->state = FLAC__ENCODER_UNINITIALIZED;
533}
534
535bool FLAC__encoder_process(FLAC__Encoder *encoder, const int32 *buf[], unsigned samples)
536{
537 unsigned i, j, channel;
538 int32 x, mid, side;
539 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
540 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
541 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
542
543 assert(encoder != 0);
544 assert(encoder->state == FLAC__ENCODER_OK);
545
546 j = 0;
547 do {
548 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++) {
549 for(channel = 0; channel < encoder->channels; channel++) {
550 x = buf[channel][j];
551 encoder->guts->integer_signal[channel][i] = x;
552 encoder->guts->real_signal[channel][i] = (real)x;
553 }
554 if(ms && encoder->guts->current_frame_can_do_mid_side) {
555 side = buf[0][j] - buf[1][j];
556 if(side < min_side || side > max_side) {
557 encoder->guts->current_frame_can_do_mid_side = false;
558 }
559 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000560 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 +0000561 encoder->guts->integer_signal_mid_side[0][i] = mid;
562 encoder->guts->integer_signal_mid_side[1][i] = side;
563 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
564 encoder->guts->real_signal_mid_side[1][i] = (real)side;
565 }
566 }
567 encoder->guts->current_sample_number++;
568 }
569 if(i == encoder->blocksize) {
570 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
571 return false;
572 }
573 } while(j < samples);
574
575 return true;
576}
577
578/* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
579bool FLAC__encoder_process_interleaved(FLAC__Encoder *encoder, const int32 buf[], unsigned samples)
580{
581 unsigned i, j, k, channel;
582 int32 x, left = 0, mid, side;
583 const bool ms = encoder->do_mid_side_stereo && encoder->channels == 2;
584 const int32 min_side = -((int64)1 << (encoder->bits_per_sample-1));
585 const int32 max_side = ((int64)1 << (encoder->bits_per_sample-1)) - 1;
586
587 assert(encoder != 0);
588 assert(encoder->state == FLAC__ENCODER_OK);
589
590 j = k = 0;
591 do {
592 for(i = encoder->guts->current_sample_number; i < encoder->blocksize && j < samples; i++, j++, k++) {
593 for(channel = 0; channel < encoder->channels; channel++, k++) {
594 x = buf[k];
595 encoder->guts->integer_signal[channel][i] = x;
596 encoder->guts->real_signal[channel][i] = (real)x;
597 if(ms && encoder->guts->current_frame_can_do_mid_side) {
598 if(channel == 0) {
599 left = x;
600 }
601 else {
602 side = left - x;
603 if(side < min_side || side > max_side) {
604 encoder->guts->current_frame_can_do_mid_side = false;
605 }
606 else {
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000607 mid = (left + x) >> 1; /* NOTE: not the same as 'mid = (left + x) / 2' ! */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000608 encoder->guts->integer_signal_mid_side[0][i] = mid;
609 encoder->guts->integer_signal_mid_side[1][i] = side;
610 encoder->guts->real_signal_mid_side[0][i] = (real)mid;
611 encoder->guts->real_signal_mid_side[1][i] = (real)side;
612 }
613 }
614 }
615 }
616 encoder->guts->current_sample_number++;
617 }
618 if(i == encoder->blocksize) {
619 if(!encoder_process_frame_(encoder, false)) /* false => not last frame */
620 return false;
621 }
622 } while(j < samples);
623
624 return true;
625}
626
627bool encoder_process_frame_(FLAC__Encoder *encoder, bool is_last_frame)
628{
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000629 assert(encoder->state == FLAC__ENCODER_OK);
630
631 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000632 * Accumulate raw signal to the MD5 signature
633 */
Josh Coalsoneae4dde2001-04-16 05:33:22 +0000634 /* 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 +0000635 if(!FLAC__MD5Accumulate(&encoder->guts->md5context, encoder->guts->integer_signal, encoder->channels, encoder->blocksize, (encoder->bits_per_sample+7) / 8)) {
636 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
637 return false;
638 }
639
640 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000641 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000642 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000643 if(!encoder_process_subframes_(encoder, is_last_frame)) {
644 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000645 return false;
646 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000647
648 /*
649 * Zero-pad the frame to a byte_boundary
650 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000651 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->guts->frame)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000652 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
653 return false;
654 }
655
656 /*
Josh Coalson215af572001-03-27 01:15:58 +0000657 * CRC-16 the whole thing
658 */
659 assert(encoder->guts->frame.bits == 0); /* assert that we're byte-aligned */
660 assert(encoder->guts->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
661 FLAC__bitbuffer_write_raw_uint32(&encoder->guts->frame, FLAC__crc16(encoder->guts->frame.buffer, encoder->guts->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
662
663 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000664 * Write it
665 */
Josh Coalson94e02cd2001-01-25 10:41:06 +0000666 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 +0000667 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_WRITING;
668 return false;
669 }
670
671 /*
672 * Get ready for the next frame
673 */
674 encoder->guts->current_frame_can_do_mid_side = true;
675 encoder->guts->current_sample_number = 0;
676 encoder->guts->current_frame_number++;
Josh Coalsonc692d382001-02-23 21:05:05 +0000677 encoder->guts->metadata.data.stream_info.total_samples += (uint64)encoder->blocksize;
678 encoder->guts->metadata.data.stream_info.min_framesize = min(encoder->guts->frame.bytes, encoder->guts->metadata.data.stream_info.min_framesize);
679 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 +0000680
681 return true;
682}
683
Josh Coalson94e02cd2001-01-25 10:41:06 +0000684bool encoder_process_subframes_(FLAC__Encoder *encoder, bool is_last_frame)
685{
686 FLAC__FrameHeader frame_header;
687 unsigned channel, max_partition_order;
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000688 bool do_independent, do_mid_side;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000689
690 /*
691 * Calculate the max Rice partition order
692 */
693 if(is_last_frame) {
694 max_partition_order = 0;
695 }
696 else {
697 unsigned limit = 0, b = encoder->blocksize;
698 while(!(b & 1)) {
699 limit++;
700 b >>= 1;
701 }
702 max_partition_order = min(encoder->rice_optimization_level, limit);
703 }
704
705 /*
706 * Setup the frame
707 */
708 if(!FLAC__bitbuffer_clear(&encoder->guts->frame)) {
709 encoder->state = FLAC__ENCODER_MEMORY_ALLOCATION_ERROR;
710 return false;
711 }
712 frame_header.blocksize = encoder->blocksize;
713 frame_header.sample_rate = encoder->sample_rate;
714 frame_header.channels = encoder->channels;
715 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
716 frame_header.bits_per_sample = encoder->bits_per_sample;
717 frame_header.number.frame_number = encoder->guts->current_frame_number;
718
719 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000720 * Figure out what channel assignments to try
721 */
722 if(encoder->do_mid_side_stereo) {
723 if(encoder->loose_mid_side_stereo) {
724 if(encoder->guts->loose_mid_side_stereo_frame_count == 0) {
725 do_independent = true;
726 do_mid_side = true;
727 }
728 else {
729 do_independent = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
730 do_mid_side = !do_independent;
731 }
732 }
733 else {
734 do_independent = true;
735 do_mid_side = true;
736 }
737 }
738 else {
739 do_independent = true;
740 do_mid_side = false;
741 }
742 if(do_mid_side && !encoder->guts->current_frame_can_do_mid_side) {
743 do_independent = true;
744 do_mid_side = false;
745 }
746
747 assert(do_independent || do_mid_side);
748
749 /*
Josh Coalson82b73242001-03-28 22:17:05 +0000750 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +0000751 */
752 if(do_independent) {
Josh Coalson82b73242001-03-28 22:17:05 +0000753 unsigned w;
754 for(channel = 0; channel < encoder->channels; channel++) {
755 w = encoder_get_wasted_bits_(encoder->guts->integer_signal[channel], encoder->blocksize);
756 encoder->guts->subframe_workspace[channel][0].wasted_bits = encoder->guts->subframe_workspace[channel][1].wasted_bits = w;
757 encoder->guts->subframe_bps[channel] = encoder->bits_per_sample - w;
758 }
Josh Coalson859bc542001-03-27 22:22:27 +0000759 }
760 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000761 unsigned w;
Josh Coalson859bc542001-03-27 22:22:27 +0000762 assert(encoder->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +0000763 for(channel = 0; channel < 2; channel++) {
764 w = encoder_get_wasted_bits_(encoder->guts->integer_signal_mid_side[channel], encoder->blocksize);
765 encoder->guts->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->guts->subframe_workspace_mid_side[channel][1].wasted_bits = w;
766 encoder->guts->subframe_bps_mid_side[channel] = encoder->bits_per_sample - w + (channel==0? 0:1);
767 }
Josh Coalson859bc542001-03-27 22:22:27 +0000768 }
769
770 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000771 * First do a normal encoding pass of each independent channel
772 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000773 if(do_independent) {
774 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000775 if(!encoder_process_subframe_(encoder, 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 +0000776 return false;
777 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000778 }
779
780 /*
781 * Now do mid and side channels if requested
782 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000783 if(do_mid_side) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000784 assert(encoder->channels == 2);
785
786 for(channel = 0; channel < 2; channel++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000787 if(!encoder_process_subframe_(encoder, 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 +0000788 return false;
789 }
790 }
791
792 /*
793 * Compose the frame bitbuffer
794 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000795 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +0000796 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
797 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000798 FLAC__ChannelAssignment channel_assignment;
799
Josh Coalson94e02cd2001-01-25 10:41:06 +0000800 assert(encoder->channels == 2);
801
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000802 if(encoder->loose_mid_side_stereo && encoder->guts->loose_mid_side_stereo_frame_count > 0) {
803 channel_assignment = (encoder->guts->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
804 }
805 else {
806 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
807 unsigned min_bits;
808 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000809
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000810 assert(do_independent && do_mid_side);
811
812 /* We have to figure out which channel assignent results in the smallest frame */
813 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits [1];
814 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->guts->best_subframe_bits [0] + encoder->guts->best_subframe_bits_mid_side[1];
815 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->guts->best_subframe_bits [1] + encoder->guts->best_subframe_bits_mid_side[1];
816 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->guts->best_subframe_bits_mid_side[0] + encoder->guts->best_subframe_bits_mid_side[1];
817
818 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
819 if(bits[ca] < min_bits) {
820 min_bits = bits[ca];
821 channel_assignment = ca;
822 }
Josh Coalson94e02cd2001-01-25 10:41:06 +0000823 }
824 }
825
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000826 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000827
828 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
829 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
830 return false;
831 }
832
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000833 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000834 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalson82b73242001-03-28 22:17:05 +0000835 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
836 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000837 break;
838 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000839 left_subframe = &encoder->guts->subframe_workspace [0][encoder->guts->best_subframe [0]];
840 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000841 break;
842 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000843 left_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
844 right_subframe = &encoder->guts->subframe_workspace [1][encoder->guts->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000845 break;
846 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalson82b73242001-03-28 22:17:05 +0000847 left_subframe = &encoder->guts->subframe_workspace_mid_side[0][encoder->guts->best_subframe_mid_side[0]];
848 right_subframe = &encoder->guts->subframe_workspace_mid_side[1][encoder->guts->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000849 break;
850 default:
851 assert(0);
852 }
Josh Coalson82b73242001-03-28 22:17:05 +0000853
854 switch(channel_assignment) {
855 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
856 left_bps = encoder->guts->subframe_bps [0];
857 right_bps = encoder->guts->subframe_bps [1];
858 break;
859 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
860 left_bps = encoder->guts->subframe_bps [0];
861 right_bps = encoder->guts->subframe_bps_mid_side[1];
862 break;
863 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
864 left_bps = encoder->guts->subframe_bps_mid_side[1];
865 right_bps = encoder->guts->subframe_bps [1];
866 break;
867 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
868 left_bps = encoder->guts->subframe_bps_mid_side[0];
869 right_bps = encoder->guts->subframe_bps_mid_side[1];
870 break;
871 default:
872 assert(0);
873 }
874
875 /* note that encoder_add_subframe_ sets the state for us in case of an error */
876 if(!encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->guts->frame))
877 return false;
878 if(!encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->guts->frame))
879 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000880 }
881 else {
882 if(!FLAC__frame_add_header(&frame_header, encoder->streamable_subset, is_last_frame, &encoder->guts->frame)) {
883 encoder->state = FLAC__ENCODER_FRAMING_ERROR;
884 return false;
885 }
886
887 for(channel = 0; channel < encoder->channels; channel++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000888 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 +0000889 /* the above function sets the state for us in case of an error */
890 return false;
891 }
892 }
893 }
894
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000895 if(encoder->loose_mid_side_stereo) {
896 encoder->guts->loose_mid_side_stereo_frame_count++;
897 if(encoder->guts->loose_mid_side_stereo_frame_count >= encoder->guts->loose_mid_side_stereo_frames)
898 encoder->guts->loose_mid_side_stereo_frame_count = 0;
899 }
900
901 encoder->guts->last_channel_assignment = frame_header.channel_assignment;
902
Josh Coalson94e02cd2001-01-25 10:41:06 +0000903 return true;
904}
905
Josh Coalson82b73242001-03-28 22:17:05 +0000906bool encoder_process_subframe_(FLAC__Encoder *encoder, 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 +0000907{
908 real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
909 real lpc_residual_bits_per_sample;
910 real autoc[FLAC__MAX_LPC_ORDER+1];
911 real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
912 real lpc_error[FLAC__MAX_LPC_ORDER];
913 unsigned min_lpc_order, max_lpc_order, lpc_order;
914 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
915 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
916 unsigned rice_parameter;
917 unsigned _candidate_bits, _best_bits;
918 unsigned _best_subframe;
919
920 /* verbatim subframe is the baseline against which we measure other compressed subframes */
921 _best_subframe = 0;
Josh Coalson82b73242001-03-28 22:17:05 +0000922 _best_bits = encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000923
924 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
925 /* check for constant subframe */
Josh Coalsoneef56702001-03-30 00:45:22 +0000926 if(encoder->guts->use_slow)
927 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);
928 else
929 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 +0000930 if(fixed_residual_bits_per_sample[1] == 0.0) {
931 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
932 unsigned i, signal_is_constant = true;
933 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
934 if(integer_signal[0] != integer_signal[i]) {
935 signal_is_constant = false;
936 break;
937 }
938 }
939 if(signal_is_constant) {
Josh Coalson82b73242001-03-28 22:17:05 +0000940 _candidate_bits = encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000941 if(_candidate_bits < _best_bits) {
942 _best_subframe = !_best_subframe;
943 _best_bits = _candidate_bits;
944 }
945 }
946 }
947 else {
948 /* encode fixed */
949 if(encoder->do_exhaustive_model_search) {
950 min_fixed_order = 0;
951 max_fixed_order = FLAC__MAX_FIXED_ORDER;
952 }
953 else {
954 min_fixed_order = max_fixed_order = guess_fixed_order;
955 }
956 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson82b73242001-03-28 22:17:05 +0000957 if(fixed_residual_bits_per_sample[fixed_order] >= (real)subframe_bps)
Josh Coalson94e02cd2001-01-25 10:41:06 +0000958 continue; /* don't even try */
Josh Coalson46f2ae82001-02-08 00:27:21 +0000959 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 Coalson352e0f62001-03-20 22:55:50 +0000960#ifndef SYMMETRIC_RICE
Josh Coalson46f2ae82001-02-08 00:27:21 +0000961 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +0000962#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +0000963 if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
964 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +0000965 _candidate_bits = encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->bits_per_residual_sample, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, max_partition_order, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +0000966 if(_candidate_bits < _best_bits) {
967 _best_subframe = !_best_subframe;
968 _best_bits = _candidate_bits;
969 }
970 }
971
972 /* encode lpc */
973 if(encoder->max_lpc_order > 0) {
974 if(encoder->max_lpc_order >= frame_header->blocksize)
975 max_lpc_order = frame_header->blocksize-1;
976 else
977 max_lpc_order = encoder->max_lpc_order;
978 if(max_lpc_order > 0) {
979 FLAC__lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +0000980 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
981 if(autoc[0] != 0.0) {
982 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
983 if(encoder->do_exhaustive_model_search) {
984 min_lpc_order = 1;
985 }
986 else {
Josh Coalson82b73242001-03-28 22:17:05 +0000987 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 +0000988 min_lpc_order = max_lpc_order = guess_lpc_order;
989 }
990 if(encoder->do_qlp_coeff_prec_search) {
991 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalson82b73242001-03-28 22:17:05 +0000992 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 +0000993 }
994 else {
995 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->qlp_coeff_precision;
996 }
997 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
998 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 +0000999 if(lpc_residual_bits_per_sample >= (real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001000 continue; /* don't even try */
1001 rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
Josh Coalson352e0f62001-03-20 22:55:50 +00001002#ifndef SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001003 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +00001004#endif
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001005 if(rice_parameter >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
1006 rice_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
1007 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001008 _candidate_bits = encoder_evaluate_lpc_subframe_(integer_signal, residual[!_best_subframe], encoder->guts->abs_residual, encoder->guts->abs_residual_partition_sums, encoder->guts->bits_per_residual_sample, lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, max_partition_order, subframe[!_best_subframe]);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001009 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1010 if(_candidate_bits < _best_bits) {
1011 _best_subframe = !_best_subframe;
1012 _best_bits = _candidate_bits;
1013 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001014 }
1015 }
1016 }
1017 }
1018 }
1019 }
1020 }
1021 }
1022
1023 *best_subframe = _best_subframe;
1024 *best_bits = _best_bits;
1025
1026 return true;
1027}
1028
Josh Coalson82b73242001-03-28 22:17:05 +00001029bool 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 +00001030{
1031 switch(subframe->type) {
1032 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00001033 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001034 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1035 return false;
1036 }
1037 break;
1038 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00001039 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 +00001040 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1041 return false;
1042 }
1043 break;
1044 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00001045 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 +00001046 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1047 return false;
1048 }
1049 break;
1050 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00001051 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001052 encoder->state = FLAC__ENCODER_FATAL_ERROR_WHILE_ENCODING;
1053 return false;
1054 }
1055 break;
1056 default:
1057 assert(0);
1058 }
1059
1060 return true;
1061}
1062
Josh Coalson82b73242001-03-28 22:17:05 +00001063unsigned encoder_evaluate_constant_subframe_(const int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001064{
1065 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1066 subframe->data.constant.value = signal;
1067
Josh Coalson82b73242001-03-28 22:17:05 +00001068 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 +00001069}
1070
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001071unsigned encoder_evaluate_fixed_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned bits_per_residual_sample[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001072{
1073 unsigned i, residual_bits;
1074 const unsigned residual_samples = blocksize - order;
1075
1076 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1077
1078 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1079
1080 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1081 subframe->data.fixed.residual = residual;
1082
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001083 residual_bits = encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, bits_per_residual_sample, residual_samples, order, rice_parameter, max_partition_order, &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 +00001084
1085 subframe->data.fixed.order = order;
1086 for(i = 0; i < order; i++)
1087 subframe->data.fixed.warmup[i] = signal[i];
1088
Josh Coalson82b73242001-03-28 22:17:05 +00001089 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 +00001090}
1091
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001092unsigned encoder_evaluate_lpc_subframe_(const int32 signal[], int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned bits_per_residual_sample[], const real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned max_partition_order, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001093{
1094 int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
1095 unsigned i, residual_bits;
1096 int quantization, ret;
1097 const unsigned residual_samples = blocksize - order;
1098
Josh Coalson82b73242001-03-28 22:17:05 +00001099 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001100 if(ret != 0)
1101 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1102
1103 FLAC__lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
1104
1105 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1106
1107 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1108 subframe->data.lpc.residual = residual;
1109
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001110 residual_bits = encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, bits_per_residual_sample, residual_samples, order, rice_parameter, max_partition_order, &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 +00001111
1112 subframe->data.lpc.order = order;
1113 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1114 subframe->data.lpc.quantization_level = quantization;
1115 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(int32)*FLAC__MAX_LPC_ORDER);
1116 for(i = 0; i < order; i++)
1117 subframe->data.lpc.warmup[i] = signal[i];
1118
Josh Coalson82b73242001-03-28 22:17:05 +00001119 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 +00001120}
1121
Josh Coalson82b73242001-03-28 22:17:05 +00001122unsigned encoder_evaluate_verbatim_subframe_(const int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001123{
1124 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1125
1126 subframe->data.verbatim.data = signal;
1127
Josh Coalson82b73242001-03-28 22:17:05 +00001128 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 +00001129}
1130
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001131unsigned encoder_find_best_partition_order_(const int32 residual[], uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned bits_per_residual_sample[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned max_partition_order, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[])
Josh Coalson94e02cd2001-01-25 10:41:06 +00001132{
Josh Coalson94e02cd2001-01-25 10:41:06 +00001133 int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001134 int partition_order;
1135 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonf76a3612001-04-18 02:28:11 +00001136 unsigned residual_sample, sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001137 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 +00001138
Josh Coalson2051dd42001-04-12 22:22:34 +00001139 /* compute abs(residual) for use later */
1140 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1141 r = residual[residual_sample];
1142 abs_residual[residual_sample] = (uint32)(r<0? -r : r);
1143 }
1144
1145 /* compute silog2(residual) for use later */
1146 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1147 bits_per_residual_sample[residual_sample] = FLAC__bitmath_silog2(residual[residual_sample]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001148 }
1149
Josh Coalsonf76a3612001-04-18 02:28:11 +00001150 max_partition_order = encoder_precompute_partition_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, max_partition_order);
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001151
1152 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= 0; partition_order--) {
1153 if(!encoder_set_partitioned_rice_(abs_residual, abs_residual_partition_sums+sum, bits_per_residual_sample, residual_samples, predictor_order, rice_parameter, (unsigned)partition_order, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001154 assert(best_residual_bits != 0);
1155 break;
1156 }
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001157 sum += 1u << partition_order;
1158 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1159 best_residual_bits = residual_bits;
1160 *best_partition_order = partition_order;
1161 best_parameters_index = !best_parameters_index;
1162 }
1163 }
1164 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1165 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1166
1167 return best_residual_bits;
1168}
1169
Josh Coalsonf76a3612001-04-18 02:28:11 +00001170unsigned encoder_precompute_partition_sums_(uint32 abs_residual[], uint32 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned max_partition_order)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001171{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001172 int partition_order;
Josh Coalsonf76a3612001-04-18 02:28:11 +00001173 unsigned sum, merged_sum = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001174 const unsigned blocksize = residual_samples + predictor_order;
1175
Josh Coalsonf76a3612001-04-18 02:28:11 +00001176 /* first do the sums for max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001177 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1178 unsigned partition, partition_sample, partition_samples, residual_sample;
1179 uint32 abs_residual_partition_sum;
1180 const unsigned partitions = 1u << partition_order;
1181 const unsigned default_partition_samples = blocksize >> partition_order;
1182
1183 if(default_partition_samples <= predictor_order) {
1184 assert(max_partition_order > 0);
1185 max_partition_order--;
1186 }
1187 else {
1188 for(partition = residual_sample = 0; partition < partitions; partition++) {
1189 partition_samples = default_partition_samples;
1190 if(partition == 0)
1191 partition_samples -= predictor_order;
1192 abs_residual_partition_sum = 0;
1193 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++)
1194 abs_residual_partition_sum += abs_residual[residual_sample++]; /* @@@ this can overflow with small max_partition_order and (large blocksizes or bits-per-sample), FIX! */
1195 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
1196 }
1197 merged_sum = partitions;
1198 break;
1199 }
1200 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00001201
1202 /* now merge partition sums for lower orders */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001203 for(sum = 0; partition_order > 0; partition_order--) {
1204 unsigned i;
1205 uint32 s;
1206 const unsigned partitions = 1u << partition_order;
1207 for(i = 0; i < partitions; i++) {
1208 s = abs_residual_partition_sums[sum++];
1209 s += abs_residual_partition_sums[sum++];
1210 abs_residual_partition_sums[merged_sum++] = s;
1211 }
1212 }
1213
Josh Coalsonf76a3612001-04-18 02:28:11 +00001214 return max_partition_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001215}
1216
Josh Coalson352e0f62001-03-20 22:55:50 +00001217#ifdef VARIABLE_RICE_BITS
1218#undef VARIABLE_RICE_BITS
1219#endif
1220#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
1221
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001222bool encoder_set_partitioned_rice_(const uint32 abs_residual[], const uint32 abs_residual_partition_sums[], const unsigned bits_per_residual_sample[], const unsigned residual_samples, const unsigned predictor_order, unsigned rice_parameter, const unsigned partition_order, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001223{
Josh Coalson2051dd42001-04-12 22:22:34 +00001224 unsigned partition_bits, flat_bits, partition_max_bits_per_residual_sample;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001225 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1226
Josh Coalson2051dd42001-04-12 22:22:34 +00001227 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1228 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1229
Josh Coalson94e02cd2001-01-25 10:41:06 +00001230 if(partition_order == 0) {
1231 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00001232
Josh Coalson2051dd42001-04-12 22:22:34 +00001233 partition_bits = 0;
1234
Josh Coalson352e0f62001-03-20 22:55:50 +00001235 {
1236#ifdef VARIABLE_RICE_BITS
1237#ifdef SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001238 partition_bits += (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001239#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001240 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson2051dd42001-04-12 22:22:34 +00001241 partition_bits += (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001242#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001243#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001244 parameters[0] = rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001245 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1246 partition_max_bits_per_residual_sample = 0;
Josh Coalson352e0f62001-03-20 22:55:50 +00001247 for(i = 0; i < residual_samples; i++) {
1248#ifdef VARIABLE_RICE_BITS
1249#ifdef SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001250 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001251#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001252 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001253#endif
1254#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001255 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 +00001256#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001257 if(bits_per_residual_sample[i] > partition_max_bits_per_residual_sample)
1258 partition_max_bits_per_residual_sample = bits_per_residual_sample[i];
1259 }
1260 flat_bits = partition_max_bits_per_residual_sample * residual_samples + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN;
1261 if(flat_bits < partition_bits) {
1262 parameters[0] = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1263 raw_bits[0] = partition_max_bits_per_residual_sample;
1264 partition_bits = flat_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00001265 }
1266 }
Josh Coalson2051dd42001-04-12 22:22:34 +00001267 bits_ += partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001268 }
1269 else {
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001270 unsigned i, j, k;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001271 unsigned mean, parameter, partition_samples;
1272 const unsigned max_parameter = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN) - 1;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001273 const unsigned partitions = 1u << partition_order;
1274 for(i = j = 0; i < partitions; i++) {
Josh Coalson2051dd42001-04-12 22:22:34 +00001275 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001276 partition_samples = (residual_samples+predictor_order) >> partition_order;
1277 if(i == 0) {
1278 if(partition_samples <= predictor_order)
1279 return false;
1280 else
1281 partition_samples -= predictor_order;
1282 }
1283 mean = partition_samples >> 1;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001284 mean += abs_residual_partition_sums[i];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001285 mean /= partition_samples;
Josh Coalson352e0f62001-03-20 22:55:50 +00001286#ifdef SYMMETRIC_RICE
1287 /* calc parameter = floor(log2(mean)) */
Josh Coalsonb9433f92001-03-17 01:07:00 +00001288 parameter = 0;
1289mean>>=1;
1290 while(mean) {
1291 parameter++;
1292 mean >>= 1;
1293 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001294#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001295 /* calc parameter = floor(log2(mean)) + 1 */
1296 parameter = 0;
1297 while(mean) {
1298 parameter++;
1299 mean >>= 1;
1300 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001301#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001302 if(parameter > max_parameter)
1303 parameter = max_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001304 if(parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
1305 parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001306 parameters[i] = parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001307 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001308#ifdef VARIABLE_RICE_BITS
1309#ifdef SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001310 partition_bits += (2+parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001311#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001312 partition_bits += (1+parameter) * partition_samples;
Josh Coalson352e0f62001-03-20 22:55:50 +00001313 --parameter;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001314#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001315#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001316 partition_max_bits_per_residual_sample = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001317 for(k = 0; k < partition_samples; j++, k++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00001318#ifdef VARIABLE_RICE_BITS
1319#ifdef SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001320 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001321#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001322 partition_bits += VARIABLE_RICE_BITS(abs_residual[j], parameter);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001323#endif
1324#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001325 partition_bits += FLAC__bitbuffer_rice_bits(residual[j], parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001326#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001327 if(bits_per_residual_sample[j] > partition_max_bits_per_residual_sample)
1328 partition_max_bits_per_residual_sample = bits_per_residual_sample[j];
1329 }
Josh Coalson2051dd42001-04-12 22:22:34 +00001330 flat_bits = partition_max_bits_per_residual_sample * partition_samples + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN;
1331 if(flat_bits < partition_bits) {
1332 parameters[i] = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1333 raw_bits[i] = partition_max_bits_per_residual_sample;
1334 partition_bits = flat_bits;
1335 }
1336 bits_ += partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001337 }
1338 }
1339
1340 *bits = bits_;
1341 return true;
1342}
Josh Coalson859bc542001-03-27 22:22:27 +00001343
1344static unsigned encoder_get_wasted_bits_(int32 signal[], unsigned samples)
1345{
1346 unsigned i, shift;
1347 int32 x = 0;
1348
1349 for(i = 0; i < samples && !(x&1); i++)
1350 x |= signal[i];
1351
1352 if(x == 0) {
1353 shift = 0;
1354 }
1355 else {
1356 for(shift = 0; !(x&1); shift++)
1357 x >>= 1;
1358 }
1359
1360 if(shift > 0) {
1361 for(i = 0; i < samples; i++)
1362 signal[i] >>= shift;
1363 }
1364
1365 return shift;
1366}