blob: acf97cd4d9bc04516f3d615d99fe3e63eeb927e0 [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
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000020#include <stdio.h>
21#include <stdlib.h> /* for malloc() */
22#include <string.h> /* for memcpy() */
Josh Coalson1b689822001-05-31 20:11:02 +000023#include "FLAC/assert.h"
Josh Coalson0a72e182001-04-13 19:17:16 +000024#include "FLAC/seek_table.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000025#include "protected/stream_encoder.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 Coalson0a15c142001-06-13 17:59:57 +000030#include "private/stream_encoder_framing.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000031#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 Coalson0a15c142001-06-13 17:59:57 +000046/***********************************************************************
47 *
48 * Private class method prototypes
49 *
50 ***********************************************************************/
51
Josh Coalson77e3f312001-06-23 03:03:24 +000052static FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
53static FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
54static FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
Josh Coalson8395d022001-07-12 21:25:22 +000055static FLAC__bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits);
Josh Coalson77e3f312001-06-23 03:03:24 +000056static FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame);
57static unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalsonb3347bd2001-07-16 18:06:41 +000058static unsigned stream_encoder_evaluate_fixed_subframe_(const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
59static unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe);
Josh Coalson77e3f312001-06-23 03:03:24 +000060static unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe);
Josh Coalsonb3347bd2001-07-16 18:06:41 +000061static unsigned stream_encoder_find_best_partition_order_(const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[]);
62static void stream_encoder_precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
Josh Coalson8395d022001-07-12 21:25:22 +000063static void stream_encoder_precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order);
64#ifdef DONT_ESTIMATE_RICE_BITS
65static FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits);
Josh Coalsonb3347bd2001-07-16 18:06:41 +000066static FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
Josh Coalson8395d022001-07-12 21:25:22 +000067#else
68static FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits);
Josh Coalsonb3347bd2001-07-16 18:06:41 +000069static FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits);
Josh Coalson0a15c142001-06-13 17:59:57 +000070#endif
Josh Coalson77e3f312001-06-23 03:03:24 +000071static unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
Josh Coalson0a15c142001-06-13 17:59:57 +000072
73/***********************************************************************
74 *
75 * Private class data
76 *
77 ***********************************************************************/
78
79typedef struct FLAC__StreamEncoderPrivate {
Josh Coalson8395d022001-07-12 21:25:22 +000080 unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
Josh Coalson77e3f312001-06-23 03:03:24 +000081 FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
82 FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
83 FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
84 FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
Josh Coalson8395d022001-07-12 21:25:22 +000085 unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
86 unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
Josh Coalson77e3f312001-06-23 03:03:24 +000087 FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
88 FLAC__int32 *residual_workspace_mid_side[2][2];
Josh Coalson94e02cd2001-01-25 10:41:06 +000089 FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
90 FLAC__Subframe subframe_workspace_mid_side[2][2];
91 FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
92 FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
Josh Coalson8395d022001-07-12 21:25:22 +000093 unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
Josh Coalson94e02cd2001-01-25 10:41:06 +000094 unsigned best_subframe_mid_side[2];
Josh Coalson8395d022001-07-12 21:25:22 +000095 unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
Josh Coalson94e02cd2001-01-25 10:41:06 +000096 unsigned best_subframe_bits_mid_side[2];
Josh Coalson77e3f312001-06-23 03:03:24 +000097 FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
Josh Coalsonb3347bd2001-07-16 18:06:41 +000098 FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
Josh Coalson8395d022001-07-12 21:25:22 +000099 unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
100 FLAC__BitBuffer frame; /* the current frame being worked on */
101 double loose_mid_side_stereo_frames_exact; /* exact number of frames the encoder will use before trying both independent and mid/side frames again */
102 unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
103 unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
Josh Coalsonb5e60e52001-01-28 09:27:27 +0000104 FLAC__ChannelAssignment last_channel_assignment;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000105 FLAC__StreamMetaData metadata;
106 unsigned current_sample_number;
107 unsigned current_frame_number;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000108 struct MD5Context md5context;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000109 FLAC__CPUInfo cpuinfo;
Josh Coalson77e3f312001-06-23 03:03:24 +0000110 unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
111 void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
112 void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
113 void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
Josh Coalson8395d022001-07-12 21:25:22 +0000114 FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
115 FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
116 FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
117 FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
Josh Coalson77e3f312001-06-23 03:03:24 +0000118 FLAC__StreamEncoderWriteStatus (*write_callback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
Josh Coalson0a15c142001-06-13 17:59:57 +0000119 void (*metadata_callback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000120 void *client_data;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000121 /* unaligned (original) pointers to allocated data */
Josh Coalson77e3f312001-06-23 03:03:24 +0000122 FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
123 FLAC__int32 *integer_signal_mid_side_unaligned[2];
124 FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
125 FLAC__real *real_signal_mid_side_unaligned[2];
126 FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
127 FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
128 FLAC__uint32 *abs_residual_unaligned;
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000129 FLAC__uint64 *abs_residual_partition_sums_unaligned;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000130 unsigned *raw_bits_per_partition_unaligned;
Josh Coalson0a15c142001-06-13 17:59:57 +0000131} FLAC__StreamEncoderPrivate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000132
Josh Coalson0a15c142001-06-13 17:59:57 +0000133/***********************************************************************
134 *
135 * Public static class data
136 *
137 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000138
Josh Coalson0a15c142001-06-13 17:59:57 +0000139const char *FLAC__StreamEncoderStateString[] = {
140 "FLAC__STREAM_ENCODER_OK",
Josh Coalson00e53872001-06-16 07:32:25 +0000141 "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
Josh Coalson0a15c142001-06-13 17:59:57 +0000142 "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
143 "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
144 "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
145 "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
146 "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
147 "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
148 "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
149 "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
150 "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
151 "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
152 "FLAC__STREAM_ENCODER_FRAMING_ERROR",
Josh Coalson8395d022001-07-12 21:25:22 +0000153 "FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE",
Josh Coalson0a15c142001-06-13 17:59:57 +0000154 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
155 "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
156 "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
157 "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
158 "FLAC__STREAM_ENCODER_UNINITIALIZED"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000159};
160
Josh Coalson0a15c142001-06-13 17:59:57 +0000161const char *FLAC__StreamEncoderWriteStatusString[] = {
162 "FLAC__STREAM_ENCODER_WRITE_OK",
163 "FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000164};
165
Josh Coalson0a15c142001-06-13 17:59:57 +0000166/***********************************************************************
167 *
168 * Class constructor/destructor
169 *
170 ***********************************************************************/
171FLAC__StreamEncoder *FLAC__stream_encoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000172{
Josh Coalson0a15c142001-06-13 17:59:57 +0000173 FLAC__StreamEncoder *encoder;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000174
Josh Coalson0a15c142001-06-13 17:59:57 +0000175 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000176
Josh Coalson0a15c142001-06-13 17:59:57 +0000177 encoder = (FLAC__StreamEncoder*)malloc(sizeof(FLAC__StreamEncoder));
178 if(encoder == 0) {
179 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000180 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000181 encoder->protected = (FLAC__StreamEncoderProtected*)malloc(sizeof(FLAC__StreamEncoderProtected));
182 if(encoder->protected == 0) {
183 free(encoder);
184 return 0;
Josh Coalsond98c43d2001-05-13 05:17:01 +0000185 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000186 encoder->private = (FLAC__StreamEncoderPrivate*)malloc(sizeof(FLAC__StreamEncoderPrivate));
187 if(encoder->private == 0) {
188 free(encoder->protected);
189 free(encoder);
190 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000191 }
Josh Coalsond98c43d2001-05-13 05:17:01 +0000192
Josh Coalson0a15c142001-06-13 17:59:57 +0000193 encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000194
Josh Coalson00e53872001-06-16 07:32:25 +0000195 encoder->protected->streamable_subset = true;
196 encoder->protected->do_mid_side_stereo = false;
197 encoder->protected->loose_mid_side_stereo = false;
198 encoder->protected->channels = 2;
199 encoder->protected->bits_per_sample = 16;
200 encoder->protected->sample_rate = 44100;
201 encoder->protected->blocksize = 1152;
202 encoder->protected->max_lpc_order = 0;
203 encoder->protected->qlp_coeff_precision = 0;
204 encoder->protected->do_qlp_coeff_prec_search = false;
205 encoder->protected->do_exhaustive_model_search = false;
Josh Coalson8395d022001-07-12 21:25:22 +0000206 encoder->protected->do_escape_coding = false;
Josh Coalson00e53872001-06-16 07:32:25 +0000207 encoder->protected->min_residual_partition_order = 0;
208 encoder->protected->max_residual_partition_order = 0;
209 encoder->protected->rice_parameter_search_dist = 0;
210 encoder->protected->total_samples_estimate = 0;
211 encoder->protected->seek_table = 0;
212 encoder->protected->padding = 0;
213 encoder->protected->last_metadata_is_last = true;
214
215 encoder->private->write_callback = 0;
216 encoder->private->metadata_callback = 0;
217 encoder->private->client_data = 0;
218
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000219 return encoder;
220}
221
Josh Coalson0a15c142001-06-13 17:59:57 +0000222void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000223{
Josh Coalson1b689822001-05-31 20:11:02 +0000224 FLAC__ASSERT(encoder != 0);
Josh Coalson0a15c142001-06-13 17:59:57 +0000225 FLAC__ASSERT(encoder->protected != 0);
226 FLAC__ASSERT(encoder->private != 0);
227
228 free(encoder->private);
229 free(encoder->protected);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000230 free(encoder);
231}
232
Josh Coalson0a15c142001-06-13 17:59:57 +0000233/***********************************************************************
234 *
235 * Public class methods
236 *
237 ***********************************************************************/
238
Josh Coalson00e53872001-06-16 07:32:25 +0000239FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000240{
241 unsigned i;
Josh Coalson0a15c142001-06-13 17:59:57 +0000242 FLAC__StreamMetaData padding_block;
243 FLAC__StreamMetaData seek_table_block;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000244
Josh Coalson1b689822001-05-31 20:11:02 +0000245 FLAC__ASSERT(encoder != 0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000246
Josh Coalson0a15c142001-06-13 17:59:57 +0000247 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
248 return encoder->protected->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000249
Josh Coalson0a15c142001-06-13 17:59:57 +0000250 encoder->protected->state = FLAC__STREAM_ENCODER_OK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000251
Josh Coalson00e53872001-06-16 07:32:25 +0000252 if(0 == encoder->private->write_callback || 0 == encoder->private->metadata_callback)
253 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000254
Josh Coalson0a15c142001-06-13 17:59:57 +0000255 if(encoder->protected->channels == 0 || encoder->protected->channels > FLAC__MAX_CHANNELS)
256 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
Josh Coalson69f1ee02001-01-24 00:54:43 +0000257
Josh Coalson0a15c142001-06-13 17:59:57 +0000258 if(encoder->protected->do_mid_side_stereo && encoder->protected->channels != 2)
259 return encoder->protected->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
Josh Coalsond37d1352001-05-30 23:09:31 +0000260
Josh Coalson0a15c142001-06-13 17:59:57 +0000261 if(encoder->protected->loose_mid_side_stereo && !encoder->protected->do_mid_side_stereo)
262 return encoder->protected->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000263
Josh Coalson0a15c142001-06-13 17:59:57 +0000264 if(encoder->protected->bits_per_sample >= 32)
265 encoder->protected->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000266
Josh Coalson0a15c142001-06-13 17:59:57 +0000267 if(encoder->protected->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected->bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE)
268 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000269
Josh Coalson0a15c142001-06-13 17:59:57 +0000270 if(encoder->protected->sample_rate == 0 || encoder->protected->sample_rate > FLAC__MAX_SAMPLE_RATE)
271 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000272
Josh Coalson0a15c142001-06-13 17:59:57 +0000273 if(encoder->protected->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected->blocksize > FLAC__MAX_BLOCK_SIZE)
274 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
275
276 if(encoder->protected->blocksize < encoder->protected->max_lpc_order)
277 return encoder->protected->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
278
279 if(encoder->protected->qlp_coeff_precision == 0) {
280 if(encoder->protected->bits_per_sample < 16) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000281 /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
282 /* @@@ until then we'll make a guess */
Josh Coalson0a15c142001-06-13 17:59:57 +0000283 encoder->protected->qlp_coeff_precision = max(5, 2 + encoder->protected->bits_per_sample / 2);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000284 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000285 else if(encoder->protected->bits_per_sample == 16) {
286 if(encoder->protected->blocksize <= 192)
287 encoder->protected->qlp_coeff_precision = 7;
288 else if(encoder->protected->blocksize <= 384)
289 encoder->protected->qlp_coeff_precision = 8;
290 else if(encoder->protected->blocksize <= 576)
291 encoder->protected->qlp_coeff_precision = 9;
292 else if(encoder->protected->blocksize <= 1152)
293 encoder->protected->qlp_coeff_precision = 10;
294 else if(encoder->protected->blocksize <= 2304)
295 encoder->protected->qlp_coeff_precision = 11;
296 else if(encoder->protected->blocksize <= 4608)
297 encoder->protected->qlp_coeff_precision = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000298 else
Josh Coalson0a15c142001-06-13 17:59:57 +0000299 encoder->protected->qlp_coeff_precision = 13;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000300 }
301 else {
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000302 encoder->protected->qlp_coeff_precision = min(13, 8*sizeof(FLAC__int32) - encoder->protected->bits_per_sample - 1 - 2); /* @@@ -2 to keep things 32-bit safe */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000303 }
304 }
Josh Coalson77e3f312001-06-23 03:03:24 +0000305 else if(encoder->protected->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected->qlp_coeff_precision + encoder->protected->bits_per_sample >= 8*sizeof(FLAC__uint32) || encoder->protected->qlp_coeff_precision >= (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
Josh Coalson0a15c142001-06-13 17:59:57 +0000306 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000307
Josh Coalson0a15c142001-06-13 17:59:57 +0000308 if(encoder->protected->streamable_subset) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000309 /*@@@ add check for blocksize here */
Josh Coalson0a15c142001-06-13 17:59:57 +0000310 if(encoder->protected->bits_per_sample != 8 && encoder->protected->bits_per_sample != 12 && encoder->protected->bits_per_sample != 16 && encoder->protected->bits_per_sample != 20 && encoder->protected->bits_per_sample != 24)
311 return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
312 if(encoder->protected->sample_rate > 655350)
313 return encoder->protected->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000314 }
315
Josh Coalson0a15c142001-06-13 17:59:57 +0000316 if(encoder->protected->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
317 encoder->protected->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
318 if(encoder->protected->min_residual_partition_order >= encoder->protected->max_residual_partition_order)
319 encoder->protected->min_residual_partition_order = encoder->protected->max_residual_partition_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000320
Josh Coalson0a15c142001-06-13 17:59:57 +0000321 encoder->private->input_capacity = 0;
322 for(i = 0; i < encoder->protected->channels; i++) {
323 encoder->private->integer_signal_unaligned[i] = encoder->private->integer_signal[i] = 0;
324 encoder->private->real_signal_unaligned[i] = encoder->private->real_signal[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000325 }
326 for(i = 0; i < 2; i++) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000327 encoder->private->integer_signal_mid_side_unaligned[i] = encoder->private->integer_signal_mid_side[i] = 0;
328 encoder->private->real_signal_mid_side_unaligned[i] = encoder->private->real_signal_mid_side[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000329 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000330 for(i = 0; i < encoder->protected->channels; i++) {
331 encoder->private->residual_workspace_unaligned[i][0] = encoder->private->residual_workspace[i][0] = 0;
332 encoder->private->residual_workspace_unaligned[i][1] = encoder->private->residual_workspace[i][1] = 0;
333 encoder->private->best_subframe[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000334 }
335 for(i = 0; i < 2; i++) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000336 encoder->private->residual_workspace_mid_side_unaligned[i][0] = encoder->private->residual_workspace_mid_side[i][0] = 0;
337 encoder->private->residual_workspace_mid_side_unaligned[i][1] = encoder->private->residual_workspace_mid_side[i][1] = 0;
338 encoder->private->best_subframe_mid_side[i] = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +0000339 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000340 for(i = 0; i < encoder->protected->channels; i++) {
341 encoder->private->subframe_workspace_ptr[i][0] = &encoder->private->subframe_workspace[i][0];
342 encoder->private->subframe_workspace_ptr[i][1] = &encoder->private->subframe_workspace[i][1];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000343 }
344 for(i = 0; i < 2; i++) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000345 encoder->private->subframe_workspace_ptr_mid_side[i][0] = &encoder->private->subframe_workspace_mid_side[i][0];
346 encoder->private->subframe_workspace_ptr_mid_side[i][1] = &encoder->private->subframe_workspace_mid_side[i][1];
Josh Coalson94e02cd2001-01-25 10:41:06 +0000347 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000348 encoder->private->abs_residual_unaligned = encoder->private->abs_residual = 0;
349 encoder->private->abs_residual_partition_sums_unaligned = encoder->private->abs_residual_partition_sums = 0;
350 encoder->private->raw_bits_per_partition_unaligned = encoder->private->raw_bits_per_partition = 0;
351 encoder->private->loose_mid_side_stereo_frames_exact = (double)encoder->protected->sample_rate * 0.4 / (double)encoder->protected->blocksize;
352 encoder->private->loose_mid_side_stereo_frames = (unsigned)(encoder->private->loose_mid_side_stereo_frames_exact + 0.5);
353 if(encoder->private->loose_mid_side_stereo_frames == 0)
354 encoder->private->loose_mid_side_stereo_frames = 1;
355 encoder->private->loose_mid_side_stereo_frame_count = 0;
356 encoder->private->current_sample_number = 0;
357 encoder->private->current_frame_number = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000358
Josh Coalson8395d022001-07-12 21:25:22 +0000359 encoder->private->use_wide_by_block = (encoder->protected->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected->blocksize)+1 > 30);
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000360 encoder->private->use_wide_by_order = (encoder->protected->bits_per_sample + FLAC__bitmath_ilog2(max(encoder->protected->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */
Josh Coalson8395d022001-07-12 21:25:22 +0000361 encoder->private->use_wide_by_partition = (false); /*@@@ need to set this */
362
Josh Coalsoncf30f502001-05-23 20:57:44 +0000363 /*
364 * get the CPU info and set the function pointers
365 */
Josh Coalson0a15c142001-06-13 17:59:57 +0000366 FLAC__cpu_info(&encoder->private->cpuinfo);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000367 /* first default to the non-asm routines */
Josh Coalson0a15c142001-06-13 17:59:57 +0000368 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
369 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
370 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
371 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
Josh Coalsoncf30f502001-05-23 20:57:44 +0000372 /* now override with asm where appropriate */
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000373#ifndef FLAC__NO_ASM
Josh Coalson0a15c142001-06-13 17:59:57 +0000374 FLAC__ASSERT(encoder->private->cpuinfo.use_asm);
Josh Coalsoncf30f502001-05-23 20:57:44 +0000375#ifdef FLAC__CPU_IA32
Josh Coalson0a15c142001-06-13 17:59:57 +0000376 FLAC__ASSERT(encoder->private->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson034d38e2001-05-24 19:29:30 +0000377#ifdef FLAC__HAS_NASM
Josh Coalson0a15c142001-06-13 17:59:57 +0000378 if(0 && encoder->private->cpuinfo.data.ia32.sse) { /* SSE version lacks necessary resolution, plus SSE flag doesn't check for OS support */
379 if(encoder->protected->max_lpc_order < 4)
Josh Coalsone6499bd2001-06-13 18:11:25 +0000380 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
Josh Coalson0a15c142001-06-13 17:59:57 +0000381 else if(encoder->protected->max_lpc_order < 8)
Josh Coalsone6499bd2001-06-13 18:11:25 +0000382 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
Josh Coalson0a15c142001-06-13 17:59:57 +0000383 else if(encoder->protected->max_lpc_order < 12)
Josh Coalsone6499bd2001-06-13 18:11:25 +0000384 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
Josh Coalsonaa255362001-05-31 06:17:41 +0000385 else
Josh Coalsone6499bd2001-06-13 18:11:25 +0000386 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalsonaa255362001-05-31 06:17:41 +0000387 }
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000388 else
Josh Coalsone6499bd2001-06-13 18:11:25 +0000389 encoder->private->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
Josh Coalson0a15c142001-06-13 17:59:57 +0000390 if(encoder->private->cpuinfo.data.ia32.mmx && encoder->private->cpuinfo.data.ia32.cmov)
Josh Coalsone6499bd2001-06-13 18:11:25 +0000391 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
Josh Coalson0a15c142001-06-13 17:59:57 +0000392 if(encoder->private->cpuinfo.data.ia32.mmx) {
Josh Coalsone6499bd2001-06-13 18:11:25 +0000393 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
394 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
Josh Coalson92d42402001-05-31 20:53:19 +0000395 }
396 else {
Josh Coalsone6499bd2001-06-13 18:11:25 +0000397 encoder->private->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
398 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
Josh Coalson92d42402001-05-31 20:53:19 +0000399 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000400#endif
Josh Coalson034d38e2001-05-24 19:29:30 +0000401#endif
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000402#endif
Josh Coalson8395d022001-07-12 21:25:22 +0000403 /* finally override based on wide-ness if necessary */
404 if(encoder->private->use_wide_by_block) {
405 encoder->private->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
406 }
Josh Coalsoncf30f502001-05-23 20:57:44 +0000407
Josh Coalson8395d022001-07-12 21:25:22 +0000408 /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
409 encoder->private->precompute_partition_sums = (encoder->protected->max_residual_partition_order > encoder->protected->min_residual_partition_order) || encoder->protected->do_escape_coding;
Josh Coalsoneef56702001-03-30 00:45:22 +0000410
Josh Coalson0a15c142001-06-13 17:59:57 +0000411 if(!stream_encoder_resize_buffers_(encoder, encoder->protected->blocksize)) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000412 /* the above function sets the state for us in case of an error */
Josh Coalson0a15c142001-06-13 17:59:57 +0000413 return encoder->protected->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000414 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000415 FLAC__bitbuffer_init(&encoder->private->frame);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000416
417 /*
418 * write the stream header
419 */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000420
Josh Coalson0a15c142001-06-13 17:59:57 +0000421 if(!FLAC__bitbuffer_clear(&encoder->private->frame))
422 return encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
423 if(!FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
424 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000425
Josh Coalson0a15c142001-06-13 17:59:57 +0000426 encoder->private->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
Josh Coalson00e53872001-06-16 07:32:25 +0000427 encoder->private->metadata.is_last = (encoder->protected->seek_table == 0 && encoder->protected->padding == 0 && encoder->protected->last_metadata_is_last);
Josh Coalson0a15c142001-06-13 17:59:57 +0000428 encoder->private->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
429 encoder->private->metadata.data.stream_info.min_blocksize = encoder->protected->blocksize; /* this encoder uses the same blocksize for the whole stream */
430 encoder->private->metadata.data.stream_info.max_blocksize = encoder->protected->blocksize;
431 encoder->private->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
432 encoder->private->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
433 encoder->private->metadata.data.stream_info.sample_rate = encoder->protected->sample_rate;
434 encoder->private->metadata.data.stream_info.channels = encoder->protected->channels;
435 encoder->private->metadata.data.stream_info.bits_per_sample = encoder->protected->bits_per_sample;
436 encoder->private->metadata.data.stream_info.total_samples = encoder->protected->total_samples_estimate; /* we will replace this later with the real total */
437 memset(encoder->private->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
438 MD5Init(&encoder->private->md5context);
439 if(!FLAC__add_metadata_block(&encoder->private->metadata, &encoder->private->frame))
440 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000441
Josh Coalson0a15c142001-06-13 17:59:57 +0000442 if(0 != encoder->protected->seek_table) {
443 if(!FLAC__seek_table_is_valid(encoder->protected->seek_table))
444 return encoder->protected->state = FLAC__STREAM_ENCODER_INVALID_SEEK_TABLE;
445 seek_table_block.type = FLAC__METADATA_TYPE_SEEKTABLE;
Josh Coalson00e53872001-06-16 07:32:25 +0000446 seek_table_block.is_last = (encoder->protected->padding == 0 && encoder->protected->last_metadata_is_last);
Josh Coalson0a15c142001-06-13 17:59:57 +0000447 seek_table_block.length = encoder->protected->seek_table->num_points * FLAC__STREAM_METADATA_SEEKPOINT_LEN;
448 seek_table_block.data.seek_table = *encoder->protected->seek_table;
449 if(!FLAC__add_metadata_block(&seek_table_block, &encoder->private->frame))
450 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson0a72e182001-04-13 19:17:16 +0000451 }
452
Josh Coalsonc692d382001-02-23 21:05:05 +0000453 /* add a PADDING block if requested */
Josh Coalson00e53872001-06-16 07:32:25 +0000454 if(encoder->protected->padding > 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000455 padding_block.type = FLAC__METADATA_TYPE_PADDING;
Josh Coalson00e53872001-06-16 07:32:25 +0000456 padding_block.is_last = encoder->protected->last_metadata_is_last;
457 padding_block.length = encoder->protected->padding;
Josh Coalson0a15c142001-06-13 17:59:57 +0000458 if(!FLAC__add_metadata_block(&padding_block, &encoder->private->frame))
459 return encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalsonc692d382001-02-23 21:05:05 +0000460 }
461
Josh Coalson0a15c142001-06-13 17:59:57 +0000462 FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned before writing */
463 FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
464 if(encoder->private->write_callback(encoder, encoder->private->frame.buffer, encoder->private->frame.bytes, 0, encoder->private->current_frame_number, encoder->private->client_data) != FLAC__STREAM_ENCODER_WRITE_OK)
465 return encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000466
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000467 /* now that the metadata block is written, we can init this to an absurdly-high value... */
Josh Coalson0a15c142001-06-13 17:59:57 +0000468 encoder->private->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
Josh Coalsoncbbbb5f2001-01-23 00:41:48 +0000469 /* ... and clear this to 0 */
Josh Coalson0a15c142001-06-13 17:59:57 +0000470 encoder->private->metadata.data.stream_info.total_samples = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000471
Josh Coalson0a15c142001-06-13 17:59:57 +0000472 return encoder->protected->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000473}
474
Josh Coalson0a15c142001-06-13 17:59:57 +0000475void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000476{
Josh Coalson94e02cd2001-01-25 10:41:06 +0000477 unsigned i, channel;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000478
Josh Coalson1b689822001-05-31 20:11:02 +0000479 FLAC__ASSERT(encoder != 0);
Josh Coalson0a15c142001-06-13 17:59:57 +0000480 if(encoder->protected->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000481 return;
Josh Coalson0a15c142001-06-13 17:59:57 +0000482 if(encoder->private->current_sample_number != 0) {
483 encoder->protected->blocksize = encoder->private->current_sample_number;
484 stream_encoder_process_frame_(encoder, true); /* true => is last frame */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000485 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000486 MD5Final(encoder->private->metadata.data.stream_info.md5sum, &encoder->private->md5context);
487 encoder->private->metadata_callback(encoder, &encoder->private->metadata, encoder->private->client_data);
488 for(i = 0; i < encoder->protected->channels; i++) {
489 if(encoder->private->integer_signal_unaligned[i] != 0) {
490 free(encoder->private->integer_signal_unaligned[i]);
491 encoder->private->integer_signal_unaligned[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000492 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000493 if(encoder->private->real_signal_unaligned[i] != 0) {
494 free(encoder->private->real_signal_unaligned[i]);
495 encoder->private->real_signal_unaligned[i] = 0;
496 }
497 }
498 for(i = 0; i < 2; i++) {
499 if(encoder->private->integer_signal_mid_side_unaligned[i] != 0) {
500 free(encoder->private->integer_signal_mid_side_unaligned[i]);
501 encoder->private->integer_signal_mid_side_unaligned[i] = 0;
502 }
503 if(encoder->private->real_signal_mid_side_unaligned[i] != 0) {
504 free(encoder->private->real_signal_mid_side_unaligned[i]);
505 encoder->private->real_signal_mid_side_unaligned[i] = 0;
506 }
507 }
508 for(channel = 0; channel < encoder->protected->channels; channel++) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000509 for(i = 0; i < 2; i++) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000510 if(encoder->private->residual_workspace_unaligned[channel][i] != 0) {
511 free(encoder->private->residual_workspace_unaligned[channel][i]);
512 encoder->private->residual_workspace_unaligned[channel][i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000513 }
514 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000515 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000516 for(channel = 0; channel < 2; channel++) {
517 for(i = 0; i < 2; i++) {
518 if(encoder->private->residual_workspace_mid_side_unaligned[channel][i] != 0) {
519 free(encoder->private->residual_workspace_mid_side_unaligned[channel][i]);
520 encoder->private->residual_workspace_mid_side_unaligned[channel][i] = 0;
521 }
522 }
523 }
524 if(encoder->private->abs_residual_unaligned != 0) {
525 free(encoder->private->abs_residual_unaligned);
526 encoder->private->abs_residual_unaligned = 0;
527 }
528 if(encoder->private->abs_residual_partition_sums_unaligned != 0) {
529 free(encoder->private->abs_residual_partition_sums_unaligned);
530 encoder->private->abs_residual_partition_sums_unaligned = 0;
531 }
532 if(encoder->private->raw_bits_per_partition_unaligned != 0) {
533 free(encoder->private->raw_bits_per_partition_unaligned);
534 encoder->private->raw_bits_per_partition_unaligned = 0;
535 }
536 FLAC__bitbuffer_free(&encoder->private->frame);
537
538 encoder->protected->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000539}
540
Josh Coalson77e3f312001-06-23 03:03:24 +0000541FLAC__bool FLAC__stream_encoder_set_streamable_subset(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000542{
543 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
544 return false;
545 encoder->protected->streamable_subset = value;
546 return true;
547}
548
Josh Coalson77e3f312001-06-23 03:03:24 +0000549FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000550{
551 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
552 return false;
553 encoder->protected->do_mid_side_stereo = value;
554 return true;
555}
556
Josh Coalson77e3f312001-06-23 03:03:24 +0000557FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000558{
559 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
560 return false;
561 encoder->protected->loose_mid_side_stereo = value;
562 return true;
563}
564
Josh Coalson77e3f312001-06-23 03:03:24 +0000565FLAC__bool FLAC__stream_encoder_set_channels(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000566{
567 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
568 return false;
569 encoder->protected->channels = value;
570 return true;
571}
572
Josh Coalson77e3f312001-06-23 03:03:24 +0000573FLAC__bool FLAC__stream_encoder_set_bits_per_sample(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000574{
575 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
576 return false;
577 encoder->protected->bits_per_sample = value;
578 return true;
579}
580
Josh Coalson77e3f312001-06-23 03:03:24 +0000581FLAC__bool FLAC__stream_encoder_set_sample_rate(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000582{
583 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
584 return false;
585 encoder->protected->sample_rate = value;
586 return true;
587}
588
Josh Coalson77e3f312001-06-23 03:03:24 +0000589FLAC__bool FLAC__stream_encoder_set_blocksize(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000590{
591 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
592 return false;
593 encoder->protected->blocksize = value;
594 return true;
595}
596
Josh Coalson77e3f312001-06-23 03:03:24 +0000597FLAC__bool FLAC__stream_encoder_set_max_lpc_order(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000598{
599 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
600 return false;
601 encoder->protected->max_lpc_order = value;
602 return true;
603}
604
Josh Coalson77e3f312001-06-23 03:03:24 +0000605FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000606{
607 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
608 return false;
609 encoder->protected->qlp_coeff_precision = value;
610 return true;
611}
612
Josh Coalson77e3f312001-06-23 03:03:24 +0000613FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000614{
615 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
616 return false;
617 encoder->protected->do_qlp_coeff_prec_search = value;
618 return true;
619}
620
Josh Coalson8395d022001-07-12 21:25:22 +0000621FLAC__bool FLAC__stream_encoder_set_do_escape_coding(const FLAC__StreamEncoder *encoder, FLAC__bool value)
622{
623 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
624 return false;
625 encoder->protected->do_escape_coding = value;
626 return true;
627}
628
Josh Coalson77e3f312001-06-23 03:03:24 +0000629FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000630{
631 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
632 return false;
633 encoder->protected->do_exhaustive_model_search = value;
634 return true;
635}
636
Josh Coalson77e3f312001-06-23 03:03:24 +0000637FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000638{
639 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
640 return false;
641 encoder->protected->min_residual_partition_order = value;
642 return true;
643}
644
Josh Coalson77e3f312001-06-23 03:03:24 +0000645FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000646{
647 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
648 return false;
649 encoder->protected->max_residual_partition_order = value;
650 return true;
651}
652
Josh Coalson77e3f312001-06-23 03:03:24 +0000653FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000654{
655 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
656 return false;
657 encoder->protected->rice_parameter_search_dist = value;
658 return true;
659}
660
Josh Coalson77e3f312001-06-23 03:03:24 +0000661FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(const FLAC__StreamEncoder *encoder, FLAC__uint64 value)
Josh Coalson00e53872001-06-16 07:32:25 +0000662{
663 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
664 return false;
665 encoder->protected->total_samples_estimate = value;
666 return true;
667}
668
Josh Coalson77e3f312001-06-23 03:03:24 +0000669FLAC__bool FLAC__stream_encoder_set_seek_table(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData_SeekTable *value)
Josh Coalson00e53872001-06-16 07:32:25 +0000670{
671 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
672 return false;
673 encoder->protected->seek_table = value;
674 return true;
675}
676
Josh Coalson77e3f312001-06-23 03:03:24 +0000677FLAC__bool FLAC__stream_encoder_set_padding(const FLAC__StreamEncoder *encoder, unsigned value)
Josh Coalson00e53872001-06-16 07:32:25 +0000678{
679 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
680 return false;
681 encoder->protected->padding = value;
682 return true;
683}
684
Josh Coalson77e3f312001-06-23 03:03:24 +0000685FLAC__bool FLAC__stream_encoder_set_last_metadata_is_last(const FLAC__StreamEncoder *encoder, FLAC__bool value)
Josh Coalson00e53872001-06-16 07:32:25 +0000686{
687 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
688 return false;
689 encoder->protected->last_metadata_is_last = value;
690 return true;
691}
692
Josh Coalson77e3f312001-06-23 03:03:24 +0000693FLAC__bool FLAC__stream_encoder_set_write_callback(const FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteStatus (*value)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data))
Josh Coalson00e53872001-06-16 07:32:25 +0000694{
695 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
696 return false;
697 encoder->private->write_callback = value;
698 return true;
699}
700
Josh Coalson77e3f312001-06-23 03:03:24 +0000701FLAC__bool FLAC__stream_encoder_set_metadata_callback(const FLAC__StreamEncoder *encoder, void (*value)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data))
Josh Coalson00e53872001-06-16 07:32:25 +0000702{
703 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
704 return false;
705 encoder->private->metadata_callback = value;
706 return true;
707}
708
Josh Coalson77e3f312001-06-23 03:03:24 +0000709FLAC__bool FLAC__stream_encoder_set_client_data(const FLAC__StreamEncoder *encoder, void *value)
Josh Coalson00e53872001-06-16 07:32:25 +0000710{
711 if(encoder->protected->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
712 return false;
713 encoder->private->client_data = value;
714 return true;
715}
716
717FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000718{
719 return encoder->protected->state;
720}
721
Josh Coalson77e3f312001-06-23 03:03:24 +0000722FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000723{
724 return encoder->protected->streamable_subset;
725}
726
Josh Coalson77e3f312001-06-23 03:03:24 +0000727FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000728{
729 return encoder->protected->do_mid_side_stereo;
730}
731
Josh Coalson77e3f312001-06-23 03:03:24 +0000732FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000733{
734 return encoder->protected->loose_mid_side_stereo;
735}
736
Josh Coalson00e53872001-06-16 07:32:25 +0000737unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000738{
739 return encoder->protected->channels;
740}
741
Josh Coalson00e53872001-06-16 07:32:25 +0000742unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000743{
744 return encoder->protected->bits_per_sample;
745}
746
Josh Coalson00e53872001-06-16 07:32:25 +0000747unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000748{
749 return encoder->protected->sample_rate;
750}
751
Josh Coalson00e53872001-06-16 07:32:25 +0000752unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000753{
754 return encoder->protected->blocksize;
755}
756
Josh Coalson00e53872001-06-16 07:32:25 +0000757unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000758{
759 return encoder->protected->max_lpc_order;
760}
761
Josh Coalson00e53872001-06-16 07:32:25 +0000762unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000763{
764 return encoder->protected->qlp_coeff_precision;
765}
766
Josh Coalson77e3f312001-06-23 03:03:24 +0000767FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000768{
769 return encoder->protected->do_qlp_coeff_prec_search;
770}
771
Josh Coalson8395d022001-07-12 21:25:22 +0000772FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
773{
774 return encoder->protected->do_escape_coding;
775}
776
Josh Coalson77e3f312001-06-23 03:03:24 +0000777FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000778{
779 return encoder->protected->do_exhaustive_model_search;
780}
781
Josh Coalson00e53872001-06-16 07:32:25 +0000782unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000783{
784 return encoder->protected->min_residual_partition_order;
785}
786
Josh Coalson00e53872001-06-16 07:32:25 +0000787unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000788{
789 return encoder->protected->max_residual_partition_order;
790}
791
Josh Coalson00e53872001-06-16 07:32:25 +0000792unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000793{
794 return encoder->protected->rice_parameter_search_dist;
795}
796
Josh Coalson77e3f312001-06-23 03:03:24 +0000797FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 *buf[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000798{
799 unsigned i, j, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +0000800 FLAC__int32 x, mid, side;
Josh Coalson0a15c142001-06-13 17:59:57 +0000801 const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000802
Josh Coalson1b689822001-05-31 20:11:02 +0000803 FLAC__ASSERT(encoder != 0);
Josh Coalson0a15c142001-06-13 17:59:57 +0000804 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000805
806 j = 0;
Josh Coalson0a15c142001-06-13 17:59:57 +0000807 if(encoder->protected->do_mid_side_stereo && channels == 2) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000808 do {
Josh Coalson0a15c142001-06-13 17:59:57 +0000809 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000810 x = mid = side = buf[0][j];
Josh Coalson0a15c142001-06-13 17:59:57 +0000811 encoder->private->integer_signal[0][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000812 encoder->private->real_signal[0][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +0000813 x = buf[1][j];
Josh Coalson0a15c142001-06-13 17:59:57 +0000814 encoder->private->integer_signal[1][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000815 encoder->private->real_signal[1][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +0000816 mid += x;
817 side -= x;
818 mid >>= 1; /* NOTE: not the same as 'mid = (buf[0][j] + buf[1][j]) / 2' ! */
Josh Coalson0a15c142001-06-13 17:59:57 +0000819 encoder->private->integer_signal_mid_side[1][i] = side;
820 encoder->private->integer_signal_mid_side[0][i] = mid;
Josh Coalson77e3f312001-06-23 03:03:24 +0000821 encoder->private->real_signal_mid_side[1][i] = (FLAC__real)side;
822 encoder->private->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson0a15c142001-06-13 17:59:57 +0000823 encoder->private->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000824 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000825 if(i == blocksize) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000826 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +0000827 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000828 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000829 } while(j < samples);
830 }
831 else {
832 do {
Josh Coalson0a15c142001-06-13 17:59:57 +0000833 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000834 for(channel = 0; channel < channels; channel++) {
835 x = buf[channel][j];
Josh Coalson0a15c142001-06-13 17:59:57 +0000836 encoder->private->integer_signal[channel][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000837 encoder->private->real_signal[channel][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +0000838 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000839 encoder->private->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +0000840 }
841 if(i == blocksize) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000842 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +0000843 return false;
844 }
845 } while(j < samples);
846 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000847
848 return true;
849}
850
851/* 'samples' is channel-wide samples, e.g. for 1 second at 44100Hz, 'samples' = 44100 regardless of the number of channels */
Josh Coalson77e3f312001-06-23 03:03:24 +0000852FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buf[], unsigned samples)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000853{
854 unsigned i, j, k, channel;
Josh Coalson77e3f312001-06-23 03:03:24 +0000855 FLAC__int32 x, mid, side;
Josh Coalson0a15c142001-06-13 17:59:57 +0000856 const unsigned channels = encoder->protected->channels, blocksize = encoder->protected->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000857
Josh Coalson1b689822001-05-31 20:11:02 +0000858 FLAC__ASSERT(encoder != 0);
Josh Coalson0a15c142001-06-13 17:59:57 +0000859 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000860
861 j = k = 0;
Josh Coalson0a15c142001-06-13 17:59:57 +0000862 if(encoder->protected->do_mid_side_stereo && channels == 2) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000863 do {
Josh Coalson0a15c142001-06-13 17:59:57 +0000864 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000865 x = mid = side = buf[k++];
Josh Coalson0a15c142001-06-13 17:59:57 +0000866 encoder->private->integer_signal[0][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000867 encoder->private->real_signal[0][i] = (FLAC__real)x;
Josh Coalsond37d1352001-05-30 23:09:31 +0000868 x = buf[k++];
Josh Coalson0a15c142001-06-13 17:59:57 +0000869 encoder->private->integer_signal[1][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000870 encoder->private->real_signal[1][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +0000871 mid += x;
872 side -= x;
873 mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
Josh Coalson0a15c142001-06-13 17:59:57 +0000874 encoder->private->integer_signal_mid_side[1][i] = side;
875 encoder->private->integer_signal_mid_side[0][i] = mid;
Josh Coalson77e3f312001-06-23 03:03:24 +0000876 encoder->private->real_signal_mid_side[1][i] = (FLAC__real)side;
877 encoder->private->real_signal_mid_side[0][i] = (FLAC__real)mid;
Josh Coalson0a15c142001-06-13 17:59:57 +0000878 encoder->private->current_sample_number++;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000879 }
Josh Coalsonaa255362001-05-31 06:17:41 +0000880 if(i == blocksize) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000881 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +0000882 return false;
883 }
884 } while(j < samples);
885 }
886 else {
887 do {
Josh Coalson0a15c142001-06-13 17:59:57 +0000888 for(i = encoder->private->current_sample_number; i < blocksize && j < samples; i++, j++) {
Josh Coalsonaa255362001-05-31 06:17:41 +0000889 for(channel = 0; channel < channels; channel++) {
890 x = buf[k++];
Josh Coalson0a15c142001-06-13 17:59:57 +0000891 encoder->private->integer_signal[channel][i] = x;
Josh Coalson77e3f312001-06-23 03:03:24 +0000892 encoder->private->real_signal[channel][i] = (FLAC__real)x;
Josh Coalsonaa255362001-05-31 06:17:41 +0000893 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000894 encoder->private->current_sample_number++;
Josh Coalsonaa255362001-05-31 06:17:41 +0000895 }
896 if(i == blocksize) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000897 if(!stream_encoder_process_frame_(encoder, false)) /* false => not last frame */
Josh Coalsonaa255362001-05-31 06:17:41 +0000898 return false;
899 }
900 } while(j < samples);
901 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000902
903 return true;
904}
905
Josh Coalson77e3f312001-06-23 03:03:24 +0000906FLAC__bool stream_encoder_resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000907{
Josh Coalson77e3f312001-06-23 03:03:24 +0000908 FLAC__bool ok;
Josh Coalson0a15c142001-06-13 17:59:57 +0000909 unsigned i, channel;
910
911 FLAC__ASSERT(new_size > 0);
912 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
913 FLAC__ASSERT(encoder->private->current_sample_number == 0);
914
915 /* To avoid excessive malloc'ing, we only grow the buffer; no shrinking. */
916 if(new_size <= encoder->private->input_capacity)
917 return true;
918
919 ok = true;
Josh Coalson8395d022001-07-12 21:25:22 +0000920
921 /* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx() requires that the input arrays (in our case the integer signals) have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
922
Josh Coalson0a15c142001-06-13 17:59:57 +0000923 for(i = 0; ok && i < encoder->protected->channels; i++) {
Josh Coalson8395d022001-07-12 21:25:22 +0000924 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private->integer_signal_unaligned[i], &encoder->private->integer_signal[i]);
Josh Coalson0a15c142001-06-13 17:59:57 +0000925 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private->real_signal_unaligned[i], &encoder->private->real_signal[i]);
Josh Coalson8395d022001-07-12 21:25:22 +0000926 memset(encoder->private->integer_signal[i], 0, sizeof(FLAC__int32)*4);
927 encoder->private->integer_signal[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +0000928 }
929 for(i = 0; ok && i < 2; i++) {
Josh Coalson8395d022001-07-12 21:25:22 +0000930 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private->integer_signal_mid_side_unaligned[i], &encoder->private->integer_signal_mid_side[i]);
Josh Coalson0a15c142001-06-13 17:59:57 +0000931 ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private->real_signal_mid_side_unaligned[i], &encoder->private->real_signal_mid_side[i]);
Josh Coalson8395d022001-07-12 21:25:22 +0000932 memset(encoder->private->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
933 encoder->private->integer_signal_mid_side[i] += 4;
Josh Coalson0a15c142001-06-13 17:59:57 +0000934 }
935 for(channel = 0; ok && channel < encoder->protected->channels; channel++) {
936 for(i = 0; ok && i < 2; i++) {
937 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->residual_workspace_unaligned[channel][i], &encoder->private->residual_workspace[channel][i]);
938 }
939 }
940 for(channel = 0; ok && channel < 2; channel++) {
941 for(i = 0; ok && i < 2; i++) {
942 ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private->residual_workspace_mid_side_unaligned[channel][i], &encoder->private->residual_workspace_mid_side[channel][i]);
943 }
944 }
945 ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private->abs_residual_unaligned, &encoder->private->abs_residual);
Josh Coalson8395d022001-07-12 21:25:22 +0000946 if(encoder->private->precompute_partition_sums || encoder->protected->do_escape_coding) /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
Josh Coalsonb3347bd2001-07-16 18:06:41 +0000947 ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private->abs_residual_partition_sums_unaligned, &encoder->private->abs_residual_partition_sums);
Josh Coalson8395d022001-07-12 21:25:22 +0000948 if(encoder->protected->do_escape_coding)
949 ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private->raw_bits_per_partition_unaligned, &encoder->private->raw_bits_per_partition);
Josh Coalson0a15c142001-06-13 17:59:57 +0000950
951 if(ok)
952 encoder->private->input_capacity = new_size;
953 else
954 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
955
956 return ok;
957}
958
959/***********************************************************************
960 *
961 * Private class methods
962 *
963 ***********************************************************************/
964
Josh Coalson77e3f312001-06-23 03:03:24 +0000965FLAC__bool stream_encoder_process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson0a15c142001-06-13 17:59:57 +0000966{
967 FLAC__ASSERT(encoder->protected->state == FLAC__STREAM_ENCODER_OK);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000968
969 /*
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000970 * Accumulate raw signal to the MD5 signature
971 */
Josh Coalsoneae4dde2001-04-16 05:33:22 +0000972 /* 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 Coalson0a15c142001-06-13 17:59:57 +0000973 if(!FLAC__MD5Accumulate(&encoder->private->md5context, encoder->private->integer_signal, encoder->protected->channels, encoder->protected->blocksize, (encoder->protected->bits_per_sample+7) / 8)) {
974 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonfa37f1c2001-01-12 23:55:11 +0000975 return false;
976 }
977
978 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +0000979 * Process the frame header and subframes into the frame bitbuffer
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000980 */
Josh Coalson0a15c142001-06-13 17:59:57 +0000981 if(!stream_encoder_process_subframes_(encoder, is_last_frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +0000982 /* the above function sets the state for us in case of an error */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000983 return false;
984 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000985
986 /*
987 * Zero-pad the frame to a byte_boundary
988 */
Josh Coalson0a15c142001-06-13 17:59:57 +0000989 if(!FLAC__bitbuffer_zero_pad_to_byte_boundary(&encoder->private->frame)) {
990 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000991 return false;
992 }
993
994 /*
Josh Coalson215af572001-03-27 01:15:58 +0000995 * CRC-16 the whole thing
996 */
Josh Coalson0a15c142001-06-13 17:59:57 +0000997 FLAC__ASSERT(encoder->private->frame.bits == 0); /* assert that we're byte-aligned */
998 FLAC__ASSERT(encoder->private->frame.total_consumed_bits == 0); /* assert that no reading of the buffer was done */
999 FLAC__bitbuffer_write_raw_uint32(&encoder->private->frame, FLAC__crc16(encoder->private->frame.buffer, encoder->private->frame.bytes), FLAC__FRAME_FOOTER_CRC_LEN);
Josh Coalson215af572001-03-27 01:15:58 +00001000
1001 /*
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001002 * Write it
1003 */
Josh Coalson0a15c142001-06-13 17:59:57 +00001004 if(encoder->private->write_callback(encoder, encoder->private->frame.buffer, encoder->private->frame.bytes, encoder->protected->blocksize, encoder->private->current_frame_number, encoder->private->client_data) != FLAC__STREAM_ENCODER_WRITE_OK) {
1005 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001006 return false;
1007 }
1008
1009 /*
1010 * Get ready for the next frame
1011 */
Josh Coalson0a15c142001-06-13 17:59:57 +00001012 encoder->private->current_sample_number = 0;
1013 encoder->private->current_frame_number++;
Josh Coalson77e3f312001-06-23 03:03:24 +00001014 encoder->private->metadata.data.stream_info.total_samples += (FLAC__uint64)encoder->protected->blocksize;
Josh Coalson0a15c142001-06-13 17:59:57 +00001015 encoder->private->metadata.data.stream_info.min_framesize = min(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.min_framesize);
1016 encoder->private->metadata.data.stream_info.max_framesize = max(encoder->private->frame.bytes, encoder->private->metadata.data.stream_info.max_framesize);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001017
1018 return true;
1019}
1020
Josh Coalson77e3f312001-06-23 03:03:24 +00001021FLAC__bool stream_encoder_process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001022{
1023 FLAC__FrameHeader frame_header;
Josh Coalson0a15c142001-06-13 17:59:57 +00001024 unsigned channel, min_partition_order = encoder->protected->min_residual_partition_order, max_partition_order;
Josh Coalson8395d022001-07-12 21:25:22 +00001025 FLAC__bool do_independent, do_mid_side, precompute_partition_sums;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001026
1027 /*
Josh Coalson60f77d72001-04-25 02:16:36 +00001028 * Calculate the min,max Rice partition orders
Josh Coalson94e02cd2001-01-25 10:41:06 +00001029 */
1030 if(is_last_frame) {
1031 max_partition_order = 0;
1032 }
1033 else {
Josh Coalson0a15c142001-06-13 17:59:57 +00001034 unsigned limit = 0, b = encoder->protected->blocksize;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001035 while(!(b & 1)) {
1036 limit++;
1037 b >>= 1;
1038 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001039 max_partition_order = min(encoder->protected->max_residual_partition_order, limit);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001040 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001041 min_partition_order = min(min_partition_order, max_partition_order);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001042
Josh Coalson8395d022001-07-12 21:25:22 +00001043 precompute_partition_sums = encoder->private->precompute_partition_sums && ((max_partition_order > min_partition_order) || encoder->protected->do_escape_coding);
1044
Josh Coalson94e02cd2001-01-25 10:41:06 +00001045 /*
1046 * Setup the frame
1047 */
Josh Coalson0a15c142001-06-13 17:59:57 +00001048 if(!FLAC__bitbuffer_clear(&encoder->private->frame)) {
1049 encoder->protected->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001050 return false;
1051 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001052 frame_header.blocksize = encoder->protected->blocksize;
1053 frame_header.sample_rate = encoder->protected->sample_rate;
1054 frame_header.channels = encoder->protected->channels;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001055 frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; /* the default unless the encoder determines otherwise */
Josh Coalson0a15c142001-06-13 17:59:57 +00001056 frame_header.bits_per_sample = encoder->protected->bits_per_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001057 frame_header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
Josh Coalson0a15c142001-06-13 17:59:57 +00001058 frame_header.number.frame_number = encoder->private->current_frame_number;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001059
1060 /*
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001061 * Figure out what channel assignments to try
1062 */
Josh Coalson0a15c142001-06-13 17:59:57 +00001063 if(encoder->protected->do_mid_side_stereo) {
1064 if(encoder->protected->loose_mid_side_stereo) {
1065 if(encoder->private->loose_mid_side_stereo_frame_count == 0) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001066 do_independent = true;
1067 do_mid_side = true;
1068 }
1069 else {
Josh Coalson0a15c142001-06-13 17:59:57 +00001070 do_independent = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001071 do_mid_side = !do_independent;
1072 }
1073 }
1074 else {
1075 do_independent = true;
1076 do_mid_side = true;
1077 }
1078 }
1079 else {
1080 do_independent = true;
1081 do_mid_side = false;
1082 }
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001083
Josh Coalson1b689822001-05-31 20:11:02 +00001084 FLAC__ASSERT(do_independent || do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001085
1086 /*
Josh Coalson82b73242001-03-28 22:17:05 +00001087 * Check for wasted bits; set effective bps for each subframe
Josh Coalson859bc542001-03-27 22:22:27 +00001088 */
1089 if(do_independent) {
Josh Coalson82b73242001-03-28 22:17:05 +00001090 unsigned w;
Josh Coalson0a15c142001-06-13 17:59:57 +00001091 for(channel = 0; channel < encoder->protected->channels; channel++) {
1092 w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal[channel], encoder->protected->blocksize);
1093 encoder->private->subframe_workspace[channel][0].wasted_bits = encoder->private->subframe_workspace[channel][1].wasted_bits = w;
1094 encoder->private->subframe_bps[channel] = encoder->protected->bits_per_sample - w;
Josh Coalson82b73242001-03-28 22:17:05 +00001095 }
Josh Coalson859bc542001-03-27 22:22:27 +00001096 }
1097 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00001098 unsigned w;
Josh Coalson0a15c142001-06-13 17:59:57 +00001099 FLAC__ASSERT(encoder->protected->channels == 2);
Josh Coalson82b73242001-03-28 22:17:05 +00001100 for(channel = 0; channel < 2; channel++) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001101 w = stream_encoder_get_wasted_bits_(encoder->private->integer_signal_mid_side[channel], encoder->protected->blocksize);
1102 encoder->private->subframe_workspace_mid_side[channel][0].wasted_bits = encoder->private->subframe_workspace_mid_side[channel][1].wasted_bits = w;
1103 encoder->private->subframe_bps_mid_side[channel] = encoder->protected->bits_per_sample - w + (channel==0? 0:1);
Josh Coalson82b73242001-03-28 22:17:05 +00001104 }
Josh Coalson859bc542001-03-27 22:22:27 +00001105 }
1106
1107 /*
Josh Coalson94e02cd2001-01-25 10:41:06 +00001108 * First do a normal encoding pass of each independent channel
1109 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001110 if(do_independent) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001111 for(channel = 0; channel < encoder->protected->channels; channel++) {
Josh Coalson8395d022001-07-12 21:25:22 +00001112 if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private->subframe_bps[channel], encoder->private->integer_signal[channel], encoder->private->real_signal[channel], encoder->private->subframe_workspace_ptr[channel], encoder->private->residual_workspace[channel], encoder->private->best_subframe+channel, encoder->private->best_subframe_bits+channel))
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001113 return false;
1114 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001115 }
1116
1117 /*
1118 * Now do mid and side channels if requested
1119 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001120 if(do_mid_side) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001121 FLAC__ASSERT(encoder->protected->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001122
1123 for(channel = 0; channel < 2; channel++) {
Josh Coalson8395d022001-07-12 21:25:22 +00001124 if(!stream_encoder_process_subframe_(encoder, min_partition_order, max_partition_order, precompute_partition_sums, false, &frame_header, encoder->private->subframe_bps_mid_side[channel], encoder->private->integer_signal_mid_side[channel], encoder->private->real_signal_mid_side[channel], encoder->private->subframe_workspace_ptr_mid_side[channel], encoder->private->residual_workspace_mid_side[channel], encoder->private->best_subframe_mid_side+channel, encoder->private->best_subframe_bits_mid_side+channel))
Josh Coalson94e02cd2001-01-25 10:41:06 +00001125 return false;
1126 }
1127 }
1128
1129 /*
1130 * Compose the frame bitbuffer
1131 */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001132 if(do_mid_side) {
Josh Coalson82b73242001-03-28 22:17:05 +00001133 unsigned left_bps = 0, right_bps = 0; /* initialized only to prevent superfluous compiler warning */
1134 FLAC__Subframe *left_subframe = 0, *right_subframe = 0; /* initialized only to prevent superfluous compiler warning */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001135 FLAC__ChannelAssignment channel_assignment;
1136
Josh Coalson0a15c142001-06-13 17:59:57 +00001137 FLAC__ASSERT(encoder->protected->channels == 2);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001138
Josh Coalson0a15c142001-06-13 17:59:57 +00001139 if(encoder->protected->loose_mid_side_stereo && encoder->private->loose_mid_side_stereo_frame_count > 0) {
1140 channel_assignment = (encoder->private->last_channel_assignment == FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT? FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT : FLAC__CHANNEL_ASSIGNMENT_MID_SIDE);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001141 }
1142 else {
1143 unsigned bits[4]; /* WATCHOUT - indexed by FLAC__ChannelAssignment */
1144 unsigned min_bits;
1145 FLAC__ChannelAssignment ca;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001146
Josh Coalson1b689822001-05-31 20:11:02 +00001147 FLAC__ASSERT(do_independent && do_mid_side);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001148
1149 /* We have to figure out which channel assignent results in the smallest frame */
Josh Coalson0a15c142001-06-13 17:59:57 +00001150 bits[FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits [1];
1151 bits[FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE ] = encoder->private->best_subframe_bits [0] + encoder->private->best_subframe_bits_mid_side[1];
1152 bits[FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE ] = encoder->private->best_subframe_bits [1] + encoder->private->best_subframe_bits_mid_side[1];
1153 bits[FLAC__CHANNEL_ASSIGNMENT_MID_SIDE ] = encoder->private->best_subframe_bits_mid_side[0] + encoder->private->best_subframe_bits_mid_side[1];
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001154
1155 for(channel_assignment = 0, min_bits = bits[0], ca = 1; ca <= 3; ca++) {
1156 if(bits[ca] < min_bits) {
1157 min_bits = bits[ca];
1158 channel_assignment = ca;
1159 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001160 }
1161 }
1162
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001163 frame_header.channel_assignment = channel_assignment;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001164
Josh Coalson0a15c142001-06-13 17:59:57 +00001165 if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
1166 encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001167 return false;
1168 }
1169
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001170 switch(channel_assignment) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001171 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalson0a15c142001-06-13 17:59:57 +00001172 left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]];
1173 right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001174 break;
1175 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001176 left_subframe = &encoder->private->subframe_workspace [0][encoder->private->best_subframe [0]];
1177 right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001178 break;
1179 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001180 left_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
1181 right_subframe = &encoder->private->subframe_workspace [1][encoder->private->best_subframe [1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001182 break;
1183 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001184 left_subframe = &encoder->private->subframe_workspace_mid_side[0][encoder->private->best_subframe_mid_side[0]];
1185 right_subframe = &encoder->private->subframe_workspace_mid_side[1][encoder->private->best_subframe_mid_side[1]];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001186 break;
1187 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001188 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001189 }
Josh Coalson82b73242001-03-28 22:17:05 +00001190
1191 switch(channel_assignment) {
1192 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
Josh Coalson0a15c142001-06-13 17:59:57 +00001193 left_bps = encoder->private->subframe_bps [0];
1194 right_bps = encoder->private->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00001195 break;
1196 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001197 left_bps = encoder->private->subframe_bps [0];
1198 right_bps = encoder->private->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00001199 break;
1200 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001201 left_bps = encoder->private->subframe_bps_mid_side[1];
1202 right_bps = encoder->private->subframe_bps [1];
Josh Coalson82b73242001-03-28 22:17:05 +00001203 break;
1204 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalson0a15c142001-06-13 17:59:57 +00001205 left_bps = encoder->private->subframe_bps_mid_side[0];
1206 right_bps = encoder->private->subframe_bps_mid_side[1];
Josh Coalson82b73242001-03-28 22:17:05 +00001207 break;
1208 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001209 FLAC__ASSERT(0);
Josh Coalson82b73242001-03-28 22:17:05 +00001210 }
1211
1212 /* note that encoder_add_subframe_ sets the state for us in case of an error */
Josh Coalson0a15c142001-06-13 17:59:57 +00001213 if(!stream_encoder_add_subframe_(encoder, &frame_header, left_bps , left_subframe , &encoder->private->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00001214 return false;
Josh Coalson0a15c142001-06-13 17:59:57 +00001215 if(!stream_encoder_add_subframe_(encoder, &frame_header, right_bps, right_subframe, &encoder->private->frame))
Josh Coalson82b73242001-03-28 22:17:05 +00001216 return false;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001217 }
1218 else {
Josh Coalson0a15c142001-06-13 17:59:57 +00001219 if(!FLAC__frame_add_header(&frame_header, encoder->protected->streamable_subset, is_last_frame, &encoder->private->frame)) {
1220 encoder->protected->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001221 return false;
1222 }
1223
Josh Coalson0a15c142001-06-13 17:59:57 +00001224 for(channel = 0; channel < encoder->protected->channels; channel++) {
1225 if(!stream_encoder_add_subframe_(encoder, &frame_header, encoder->private->subframe_bps[channel], &encoder->private->subframe_workspace[channel][encoder->private->best_subframe[channel]], &encoder->private->frame)) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001226 /* the above function sets the state for us in case of an error */
1227 return false;
1228 }
1229 }
1230 }
1231
Josh Coalson0a15c142001-06-13 17:59:57 +00001232 if(encoder->protected->loose_mid_side_stereo) {
1233 encoder->private->loose_mid_side_stereo_frame_count++;
1234 if(encoder->private->loose_mid_side_stereo_frame_count >= encoder->private->loose_mid_side_stereo_frames)
1235 encoder->private->loose_mid_side_stereo_frame_count = 0;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001236 }
1237
Josh Coalson0a15c142001-06-13 17:59:57 +00001238 encoder->private->last_channel_assignment = frame_header.channel_assignment;
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001239
Josh Coalson94e02cd2001-01-25 10:41:06 +00001240 return true;
1241}
1242
Josh Coalson8395d022001-07-12 21:25:22 +00001243FLAC__bool stream_encoder_process_subframe_(FLAC__StreamEncoder *encoder, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool verbatim_only, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__int32 integer_signal[], const FLAC__real real_signal[], FLAC__Subframe *subframe[2], FLAC__int32 *residual[2], unsigned *best_subframe, unsigned *best_bits)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001244{
Josh Coalson77e3f312001-06-23 03:03:24 +00001245 FLAC__real fixed_residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1];
1246 FLAC__real lpc_residual_bits_per_sample;
1247 FLAC__real autoc[FLAC__MAX_LPC_ORDER+1]; /* WATCHOUT: the size is important even though encoder->protected->max_lpc_order might be less; some asm routines need all the space */
1248 FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER];
1249 FLAC__real lpc_error[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001250 unsigned min_lpc_order, max_lpc_order, lpc_order;
1251 unsigned min_fixed_order, max_fixed_order, guess_fixed_order, fixed_order;
1252 unsigned min_qlp_coeff_precision, max_qlp_coeff_precision, qlp_coeff_precision;
1253 unsigned rice_parameter;
1254 unsigned _candidate_bits, _best_bits;
1255 unsigned _best_subframe;
1256
1257 /* verbatim subframe is the baseline against which we measure other compressed subframes */
1258 _best_subframe = 0;
Josh Coalson0a15c142001-06-13 17:59:57 +00001259 _best_bits = stream_encoder_evaluate_verbatim_subframe_(integer_signal, frame_header->blocksize, subframe_bps, subframe[_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001260
1261 if(!verbatim_only && frame_header->blocksize >= FLAC__MAX_FIXED_ORDER) {
1262 /* check for constant subframe */
Josh Coalson8395d022001-07-12 21:25:22 +00001263 guess_fixed_order = encoder->private->local_fixed_compute_best_predictor(integer_signal+FLAC__MAX_FIXED_ORDER, frame_header->blocksize-FLAC__MAX_FIXED_ORDER, fixed_residual_bits_per_sample);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001264 if(fixed_residual_bits_per_sample[1] == 0.0) {
1265 /* the above means integer_signal+FLAC__MAX_FIXED_ORDER is constant, now we just have to check the warmup samples */
1266 unsigned i, signal_is_constant = true;
1267 for(i = 1; i <= FLAC__MAX_FIXED_ORDER; i++) {
1268 if(integer_signal[0] != integer_signal[i]) {
1269 signal_is_constant = false;
1270 break;
1271 }
1272 }
1273 if(signal_is_constant) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001274 _candidate_bits = stream_encoder_evaluate_constant_subframe_(integer_signal[0], subframe_bps, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001275 if(_candidate_bits < _best_bits) {
1276 _best_subframe = !_best_subframe;
1277 _best_bits = _candidate_bits;
1278 }
1279 }
1280 }
1281 else {
1282 /* encode fixed */
Josh Coalson0a15c142001-06-13 17:59:57 +00001283 if(encoder->protected->do_exhaustive_model_search) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001284 min_fixed_order = 0;
1285 max_fixed_order = FLAC__MAX_FIXED_ORDER;
1286 }
1287 else {
1288 min_fixed_order = max_fixed_order = guess_fixed_order;
1289 }
1290 for(fixed_order = min_fixed_order; fixed_order <= max_fixed_order; fixed_order++) {
Josh Coalson77e3f312001-06-23 03:03:24 +00001291 if(fixed_residual_bits_per_sample[fixed_order] >= (FLAC__real)subframe_bps)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001292 continue; /* don't even try */
Josh Coalson46f2ae82001-02-08 00:27:21 +00001293 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 +00001294#ifndef FLAC__SYMMETRIC_RICE
Josh Coalson46f2ae82001-02-08 00:27:21 +00001295 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +00001296#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001297 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1298#ifdef DEBUG
1299 fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1300#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001301 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00001302 }
1303 _candidate_bits = stream_encoder_evaluate_fixed_subframe_(integer_signal, residual[!_best_subframe], encoder->private->abs_residual, encoder->private->abs_residual_partition_sums, encoder->private->raw_bits_per_partition, frame_header->blocksize, subframe_bps, fixed_order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected->do_escape_coding, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001304 if(_candidate_bits < _best_bits) {
1305 _best_subframe = !_best_subframe;
1306 _best_bits = _candidate_bits;
1307 }
1308 }
1309
1310 /* encode lpc */
Josh Coalson0a15c142001-06-13 17:59:57 +00001311 if(encoder->protected->max_lpc_order > 0) {
1312 if(encoder->protected->max_lpc_order >= frame_header->blocksize)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001313 max_lpc_order = frame_header->blocksize-1;
1314 else
Josh Coalson0a15c142001-06-13 17:59:57 +00001315 max_lpc_order = encoder->protected->max_lpc_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001316 if(max_lpc_order > 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001317 encoder->private->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001318 /* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
1319 if(autoc[0] != 0.0) {
1320 FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, lp_coeff, lpc_error);
Josh Coalson0a15c142001-06-13 17:59:57 +00001321 if(encoder->protected->do_exhaustive_model_search) {
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001322 min_lpc_order = 1;
1323 }
1324 else {
Josh Coalson82b73242001-03-28 22:17:05 +00001325 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 +00001326 min_lpc_order = max_lpc_order = guess_lpc_order;
1327 }
Josh Coalson0a15c142001-06-13 17:59:57 +00001328 if(encoder->protected->do_qlp_coeff_prec_search) {
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001329 min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001330 max_qlp_coeff_precision = min(8*sizeof(FLAC__int32) - subframe_bps - 1 - 2, (1u<<FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN)-1); /* -2 to keep things 32-bit safe */
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001331 }
1332 else {
Josh Coalson0a15c142001-06-13 17:59:57 +00001333 min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected->qlp_coeff_precision;
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001334 }
1335 for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
1336 lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
Josh Coalson77e3f312001-06-23 03:03:24 +00001337 if(lpc_residual_bits_per_sample >= (FLAC__real)subframe_bps)
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001338 continue; /* don't even try */
1339 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 +00001340#ifndef FLAC__SYMMETRIC_RICE
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001341 rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
Josh Coalsonb9433f92001-03-17 01:07:00 +00001342#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001343 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1344#ifdef DEBUG
1345 fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1346#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001347 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00001348 }
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001349 for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
Josh Coalson8395d022001-07-12 21:25:22 +00001350 _candidate_bits = stream_encoder_evaluate_lpc_subframe_(encoder, integer_signal, residual[!_best_subframe], encoder->private->abs_residual, encoder->private->abs_residual_partition_sums, encoder->private->raw_bits_per_partition, lp_coeff[lpc_order-1], frame_header->blocksize, subframe_bps, lpc_order, qlp_coeff_precision, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, encoder->protected->do_escape_coding, encoder->protected->rice_parameter_search_dist, subframe[!_best_subframe]);
Josh Coalsonf4ce50b2001-02-28 23:45:15 +00001351 if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
1352 if(_candidate_bits < _best_bits) {
1353 _best_subframe = !_best_subframe;
1354 _best_bits = _candidate_bits;
1355 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001356 }
1357 }
1358 }
1359 }
1360 }
1361 }
1362 }
1363 }
1364
1365 *best_subframe = _best_subframe;
1366 *best_bits = _best_bits;
1367
1368 return true;
1369}
1370
Josh Coalson77e3f312001-06-23 03:03:24 +00001371FLAC__bool stream_encoder_add_subframe_(FLAC__StreamEncoder *encoder, const FLAC__FrameHeader *frame_header, unsigned subframe_bps, const FLAC__Subframe *subframe, FLAC__BitBuffer *frame)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001372{
1373 switch(subframe->type) {
1374 case FLAC__SUBFRAME_TYPE_CONSTANT:
Josh Coalson82b73242001-03-28 22:17:05 +00001375 if(!FLAC__subframe_add_constant(&(subframe->data.constant), subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001376 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001377 return false;
1378 }
1379 break;
1380 case FLAC__SUBFRAME_TYPE_FIXED:
Josh Coalson82b73242001-03-28 22:17:05 +00001381 if(!FLAC__subframe_add_fixed(&(subframe->data.fixed), frame_header->blocksize - subframe->data.fixed.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001382 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001383 return false;
1384 }
1385 break;
1386 case FLAC__SUBFRAME_TYPE_LPC:
Josh Coalson82b73242001-03-28 22:17:05 +00001387 if(!FLAC__subframe_add_lpc(&(subframe->data.lpc), frame_header->blocksize - subframe->data.lpc.order, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001388 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001389 return false;
1390 }
1391 break;
1392 case FLAC__SUBFRAME_TYPE_VERBATIM:
Josh Coalson82b73242001-03-28 22:17:05 +00001393 if(!FLAC__subframe_add_verbatim(&(subframe->data.verbatim), frame_header->blocksize, subframe_bps, subframe->wasted_bits, frame)) {
Josh Coalson0a15c142001-06-13 17:59:57 +00001394 encoder->protected->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001395 return false;
1396 }
1397 break;
1398 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001399 FLAC__ASSERT(0);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001400 }
1401
1402 return true;
1403}
1404
Josh Coalson77e3f312001-06-23 03:03:24 +00001405unsigned stream_encoder_evaluate_constant_subframe_(const FLAC__int32 signal, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001406{
1407 subframe->type = FLAC__SUBFRAME_TYPE_CONSTANT;
1408 subframe->data.constant.value = signal;
1409
Josh Coalson82b73242001-03-28 22:17:05 +00001410 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 +00001411}
1412
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001413unsigned stream_encoder_evaluate_fixed_subframe_(const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001414{
1415 unsigned i, residual_bits;
1416 const unsigned residual_samples = blocksize - order;
1417
1418 FLAC__fixed_compute_residual(signal+order, residual_samples, order, residual);
1419
1420 subframe->type = FLAC__SUBFRAME_TYPE_FIXED;
1421
1422 subframe->data.fixed.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1423 subframe->data.fixed.residual = residual;
1424
Josh Coalson8395d022001-07-12 21:25:22 +00001425 residual_bits = stream_encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, &subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.raw_bits);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001426
1427 subframe->data.fixed.order = order;
1428 for(i = 0; i < order; i++)
1429 subframe->data.fixed.warmup[i] = signal[i];
1430
Josh Coalson82b73242001-03-28 22:17:05 +00001431 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 +00001432}
1433
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001434unsigned stream_encoder_evaluate_lpc_subframe_(FLAC__StreamEncoder *encoder, const FLAC__int32 signal[], FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], const FLAC__real lp_coeff[], unsigned blocksize, unsigned subframe_bps, unsigned order, unsigned qlp_coeff_precision, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001435{
Josh Coalson77e3f312001-06-23 03:03:24 +00001436 FLAC__int32 qlp_coeff[FLAC__MAX_LPC_ORDER];
Josh Coalson94e02cd2001-01-25 10:41:06 +00001437 unsigned i, residual_bits;
1438 int quantization, ret;
1439 const unsigned residual_samples = blocksize - order;
1440
Josh Coalson82b73242001-03-28 22:17:05 +00001441 ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, subframe_bps, qlp_coeff, &quantization);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001442 if(ret != 0)
1443 return 0; /* this is a hack to indicate to the caller that we can't do lp at this order on this subframe */
1444
Josh Coalson92d42402001-05-31 20:53:19 +00001445 if(subframe_bps <= 16 && qlp_coeff_precision <= 16)
Josh Coalson0a15c142001-06-13 17:59:57 +00001446 encoder->private->local_lpc_compute_residual_from_qlp_coefficients_16bit(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
Josh Coalson92d42402001-05-31 20:53:19 +00001447 else
Josh Coalson0a15c142001-06-13 17:59:57 +00001448 encoder->private->local_lpc_compute_residual_from_qlp_coefficients(signal+order, residual_samples, qlp_coeff, order, quantization, residual);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001449
1450 subframe->type = FLAC__SUBFRAME_TYPE_LPC;
1451
1452 subframe->data.lpc.entropy_coding_method.type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE;
1453 subframe->data.lpc.residual = residual;
1454
Josh Coalson8395d022001-07-12 21:25:22 +00001455 residual_bits = stream_encoder_find_best_partition_order_(residual, abs_residual, abs_residual_partition_sums, raw_bits_per_partition, residual_samples, order, rice_parameter, min_partition_order, max_partition_order, precompute_partition_sums, do_escape_coding, rice_parameter_search_dist, &subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.raw_bits);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001456
1457 subframe->data.lpc.order = order;
1458 subframe->data.lpc.qlp_coeff_precision = qlp_coeff_precision;
1459 subframe->data.lpc.quantization_level = quantization;
Josh Coalson77e3f312001-06-23 03:03:24 +00001460 memcpy(subframe->data.lpc.qlp_coeff, qlp_coeff, sizeof(FLAC__int32)*FLAC__MAX_LPC_ORDER);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001461 for(i = 0; i < order; i++)
1462 subframe->data.lpc.warmup[i] = signal[i];
1463
Josh Coalson82b73242001-03-28 22:17:05 +00001464 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 +00001465}
1466
Josh Coalson77e3f312001-06-23 03:03:24 +00001467unsigned stream_encoder_evaluate_verbatim_subframe_(const FLAC__int32 signal[], unsigned blocksize, unsigned subframe_bps, FLAC__Subframe *subframe)
Josh Coalson94e02cd2001-01-25 10:41:06 +00001468{
1469 subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
1470
1471 subframe->data.verbatim.data = signal;
1472
Josh Coalson82b73242001-03-28 22:17:05 +00001473 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 +00001474}
1475
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001476unsigned stream_encoder_find_best_partition_order_(const FLAC__int32 residual[], FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned rice_parameter, unsigned min_partition_order, unsigned max_partition_order, FLAC__bool precompute_partition_sums, FLAC__bool do_escape_coding, unsigned rice_parameter_search_dist, unsigned *best_partition_order, unsigned best_parameters[], unsigned best_raw_bits[])
Josh Coalson94e02cd2001-01-25 10:41:06 +00001477{
Josh Coalson77e3f312001-06-23 03:03:24 +00001478 FLAC__int32 r;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001479 unsigned residual_bits, best_residual_bits = 0;
Josh Coalsonafcd8772001-04-18 22:59:25 +00001480 unsigned residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001481 unsigned best_parameters_index = 0, parameters[2][1 << FLAC__MAX_RICE_PARTITION_ORDER], raw_bits[2][1 << FLAC__MAX_RICE_PARTITION_ORDER];
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001482 const unsigned blocksize = residual_samples + predictor_order;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001483
Josh Coalson2051dd42001-04-12 22:22:34 +00001484 /* compute abs(residual) for use later */
1485 for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
1486 r = residual[residual_sample];
Josh Coalson77e3f312001-06-23 03:03:24 +00001487 abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
Josh Coalson2051dd42001-04-12 22:22:34 +00001488 }
1489
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001490 while(max_partition_order > 0 && blocksize >> max_partition_order <= predictor_order)
1491 max_partition_order--;
1492 FLAC__ASSERT(blocksize >> max_partition_order > predictor_order);
1493 min_partition_order = min(min_partition_order, max_partition_order);
1494
Josh Coalson8395d022001-07-12 21:25:22 +00001495 if(precompute_partition_sums) {
1496 int partition_order;
1497 unsigned sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001498
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001499 stream_encoder_precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
Josh Coalson8395d022001-07-12 21:25:22 +00001500
1501 if(do_escape_coding)
1502 stream_encoder_precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
1503
1504 for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
1505#ifdef DONT_ESTIMATE_RICE_BITS
1506 if(!stream_encoder_set_partitioned_rice_with_precompute_(residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits))
Josh Coalsonafcd8772001-04-18 22:59:25 +00001507#else
Josh Coalson8395d022001-07-12 21:25:22 +00001508 if(!stream_encoder_set_partitioned_rice_with_precompute_(abs_residual, abs_residual_partition_sums+sum, raw_bits_per_partition+sum, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, (unsigned)partition_order, do_escape_coding, parameters[!best_parameters_index], raw_bits[!best_parameters_index], &residual_bits))
1509#endif
1510 {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001511 FLAC__ASSERT(best_residual_bits != 0);
1512 break;
Josh Coalson8395d022001-07-12 21:25:22 +00001513 }
1514 sum += 1u << partition_order;
1515 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1516 best_residual_bits = residual_bits;
1517 *best_partition_order = partition_order;
1518 best_parameters_index = !best_parameters_index;
1519 }
Josh Coalsonafcd8772001-04-18 22:59:25 +00001520 }
1521 }
Josh Coalson8395d022001-07-12 21:25:22 +00001522 else {
1523 unsigned partition_order;
1524 for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
1525#ifdef DONT_ESTIMATE_RICE_BITS
1526 if(!stream_encoder_set_partitioned_rice_(abs_residual, residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], &residual_bits))
1527#else
1528 if(!stream_encoder_set_partitioned_rice_(abs_residual, residual_samples, predictor_order, rice_parameter, rice_parameter_search_dist, partition_order, parameters[!best_parameters_index], &residual_bits))
Josh Coalsonafcd8772001-04-18 22:59:25 +00001529#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001530 {
1531 FLAC__ASSERT(best_residual_bits != 0);
1532 break;
1533 }
1534 if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
1535 best_residual_bits = residual_bits;
1536 *best_partition_order = partition_order;
1537 best_parameters_index = !best_parameters_index;
1538 }
1539 }
1540 }
1541
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001542 memcpy(best_parameters, parameters[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1543 memcpy(best_raw_bits, raw_bits[best_parameters_index], sizeof(unsigned)*(1<<(*best_partition_order)));
1544
1545 return best_residual_bits;
1546}
1547
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001548void stream_encoder_precompute_partition_info_sums_(const FLAC__uint32 abs_residual[], FLAC__uint64 abs_residual_partition_sums[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001549{
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001550 int partition_order;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001551 unsigned from_partition, to_partition = 0;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001552 const unsigned blocksize = residual_samples + predictor_order;
1553
Josh Coalsonaef013c2001-04-24 01:25:42 +00001554 /* first do max_partition_order */
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001555 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001556 FLAC__uint64 abs_residual_partition_sum;
Josh Coalson77e3f312001-06-23 03:03:24 +00001557 FLAC__uint32 abs_r;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001558 unsigned partition, partition_sample, partition_samples, residual_sample;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001559 const unsigned partitions = 1u << partition_order;
1560 const unsigned default_partition_samples = blocksize >> partition_order;
1561
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001562 FLAC__ASSERT(default_partition_samples > predictor_order);
1563
1564 for(partition = residual_sample = 0; partition < partitions; partition++) {
1565 partition_samples = default_partition_samples;
1566 if(partition == 0)
1567 partition_samples -= predictor_order;
1568 abs_residual_partition_sum = 0;
1569 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1570 abs_r = abs_residual[residual_sample];
1571 abs_residual_partition_sum += abs_r;
1572 residual_sample++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001573 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001574 abs_residual_partition_sums[partition] = abs_residual_partition_sum;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001575 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001576 to_partition = partitions;
1577 break;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001578 }
Josh Coalsonf76a3612001-04-18 02:28:11 +00001579
Josh Coalson8395d022001-07-12 21:25:22 +00001580 /* now merge partitions for lower orders */
Josh Coalson6bd17572001-05-25 19:02:01 +00001581 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001582 FLAC__uint64 s;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001583 unsigned i;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001584 const unsigned partitions = 1u << partition_order;
1585 for(i = 0; i < partitions; i++) {
Josh Coalsonaef013c2001-04-24 01:25:42 +00001586 s = abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00001587 from_partition++;
Josh Coalsonaef013c2001-04-24 01:25:42 +00001588 abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
Josh Coalsonaef013c2001-04-24 01:25:42 +00001589 from_partition++;
1590 to_partition++;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001591 }
1592 }
Josh Coalson94e02cd2001-01-25 10:41:06 +00001593}
Josh Coalson8395d022001-07-12 21:25:22 +00001594
1595void stream_encoder_precompute_partition_info_escapes_(const FLAC__int32 residual[], unsigned raw_bits_per_partition[], unsigned residual_samples, unsigned predictor_order, unsigned min_partition_order, unsigned max_partition_order)
1596{
1597 int partition_order;
1598 unsigned from_partition, to_partition = 0;
1599 const unsigned blocksize = residual_samples + predictor_order;
1600
1601 /* first do max_partition_order */
1602 for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
1603 FLAC__int32 r, residual_partition_min, residual_partition_max;
1604 unsigned silog2_min, silog2_max;
1605 unsigned partition, partition_sample, partition_samples, residual_sample;
1606 const unsigned partitions = 1u << partition_order;
1607 const unsigned default_partition_samples = blocksize >> partition_order;
1608
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001609 FLAC__ASSERT(default_partition_samples > predictor_order);
1610
1611 for(partition = residual_sample = 0; partition < partitions; partition++) {
1612 partition_samples = default_partition_samples;
1613 if(partition == 0)
1614 partition_samples -= predictor_order;
1615 residual_partition_min = residual_partition_max = 0;
1616 for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
1617 r = residual[residual_sample];
1618 if(r < residual_partition_min)
1619 residual_partition_min = r;
1620 else if(r > residual_partition_max)
1621 residual_partition_max = r;
1622 residual_sample++;
Josh Coalson8395d022001-07-12 21:25:22 +00001623 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001624 silog2_min = FLAC__bitmath_silog2(residual_partition_min);
1625 silog2_max = FLAC__bitmath_silog2(residual_partition_max);
1626 raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
Josh Coalson8395d022001-07-12 21:25:22 +00001627 }
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001628 to_partition = partitions;
1629 break;
Josh Coalson8395d022001-07-12 21:25:22 +00001630 }
1631
1632 /* now merge partitions for lower orders */
1633 for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
1634 unsigned m;
1635 unsigned i;
1636 const unsigned partitions = 1u << partition_order;
1637 for(i = 0; i < partitions; i++) {
1638 m = raw_bits_per_partition[from_partition];
1639 from_partition++;
1640 raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
1641 from_partition++;
1642 to_partition++;
1643 }
1644 }
1645}
Josh Coalson94e02cd2001-01-25 10:41:06 +00001646
Josh Coalson352e0f62001-03-20 22:55:50 +00001647#ifdef VARIABLE_RICE_BITS
1648#undef VARIABLE_RICE_BITS
1649#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001650#ifndef DONT_ESTIMATE_RICE_BITS
Josh Coalson352e0f62001-03-20 22:55:50 +00001651#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
Josh Coalson8395d022001-07-12 21:25:22 +00001652#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001653
Josh Coalson8395d022001-07-12 21:25:22 +00001654#ifdef DONT_ESTIMATE_RICE_BITS
1655FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits)
1656#else
1657FLAC__bool stream_encoder_set_partitioned_rice_(const FLAC__uint32 abs_residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, unsigned parameters[], unsigned *bits)
1658#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001659{
Josh Coalson034dfab2001-04-27 19:10:23 +00001660 unsigned rice_parameter, partition_bits;
1661#ifndef NO_RICE_SEARCH
1662 unsigned best_partition_bits;
1663 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1664#endif
Josh Coalson94e02cd2001-01-25 10:41:06 +00001665 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1666
Josh Coalson1b689822001-05-31 20:11:02 +00001667 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
Josh Coalson2051dd42001-04-12 22:22:34 +00001668
Josh Coalson94e02cd2001-01-25 10:41:06 +00001669 if(partition_order == 0) {
1670 unsigned i;
Josh Coalson352e0f62001-03-20 22:55:50 +00001671
Josh Coalson034dfab2001-04-27 19:10:23 +00001672#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001673 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001674 if(suggested_rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001675 min_rice_parameter = 0;
1676 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001677 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1678 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00001679 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1680#ifdef DEBUG
1681 fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1682#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00001683 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00001684 }
1685 }
1686 else
1687 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
1688
1689 best_partition_bits = 0xffffffff;
1690 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1691#endif
1692#ifdef VARIABLE_RICE_BITS
1693#ifdef FLAC__SYMMETRIC_RICE
1694 partition_bits = (2+rice_parameter) * residual_samples;
1695#else
1696 const unsigned rice_parameter_estimate = rice_parameter-1;
1697 partition_bits = (1+rice_parameter) * residual_samples;
1698#endif
1699#else
1700 partition_bits = 0;
1701#endif
1702 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1703 for(i = 0; i < residual_samples; i++) {
1704#ifdef VARIABLE_RICE_BITS
1705#ifdef FLAC__SYMMETRIC_RICE
1706 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
1707#else
1708 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
1709#endif
1710#else
1711 partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
1712#endif
1713 }
1714#ifndef NO_RICE_SEARCH
1715 if(partition_bits < best_partition_bits) {
1716 best_rice_parameter = rice_parameter;
1717 best_partition_bits = partition_bits;
1718 }
1719 }
1720#endif
1721 parameters[0] = best_rice_parameter;
1722 bits_ += best_partition_bits;
1723 }
1724 else {
1725 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001726 unsigned partition_samples;
1727 FLAC__uint64 mean, k;
Josh Coalson8395d022001-07-12 21:25:22 +00001728 const unsigned partitions = 1u << partition_order;
1729 for(partition = residual_sample = 0; partition < partitions; partition++) {
1730 partition_samples = (residual_samples+predictor_order) >> partition_order;
1731 if(partition == 0) {
1732 if(partition_samples <= predictor_order)
1733 return false;
1734 else
1735 partition_samples -= predictor_order;
1736 }
1737 mean = 0;
1738 save_residual_sample = residual_sample;
1739 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001740 mean += abs_residual[residual_sample];
Josh Coalson8395d022001-07-12 21:25:22 +00001741 residual_sample = save_residual_sample;
1742#ifdef FLAC__SYMMETRIC_RICE
1743 mean += partition_samples >> 1; /* for rounding effect */
1744 mean /= partition_samples;
1745
1746 /* calc rice_parameter = floor(log2(mean)) */
1747 rice_parameter = 0;
1748 mean>>=1;
1749 while(mean) {
1750 rice_parameter++;
1751 mean >>= 1;
1752 }
1753#else
1754 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001755 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson8395d022001-07-12 21:25:22 +00001756 ;
1757#endif
1758 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1759#ifdef DEBUG
1760 fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1761#endif
1762 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1763 }
1764
1765#ifndef NO_RICE_SEARCH
1766 if(rice_parameter_search_dist) {
1767 if(rice_parameter < rice_parameter_search_dist)
1768 min_rice_parameter = 0;
1769 else
1770 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1771 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
1772 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1773#ifdef DEBUG
1774 fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1775#endif
1776 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1777 }
1778 }
1779 else
1780 min_rice_parameter = max_rice_parameter = rice_parameter;
1781
1782 best_partition_bits = 0xffffffff;
1783 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1784#endif
1785#ifdef VARIABLE_RICE_BITS
1786#ifdef FLAC__SYMMETRIC_RICE
1787 partition_bits = (2+rice_parameter) * partition_samples;
1788#else
1789 const unsigned rice_parameter_estimate = rice_parameter-1;
1790 partition_bits = (1+rice_parameter) * partition_samples;
1791#endif
1792#else
1793 partition_bits = 0;
1794#endif
1795 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
1796 save_residual_sample = residual_sample;
1797 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
1798#ifdef VARIABLE_RICE_BITS
1799#ifdef FLAC__SYMMETRIC_RICE
1800 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
1801#else
1802 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
1803#endif
1804#else
1805 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
1806#endif
1807 }
1808#ifndef NO_RICE_SEARCH
1809 if(rice_parameter != max_rice_parameter)
1810 residual_sample = save_residual_sample;
1811 if(partition_bits < best_partition_bits) {
1812 best_rice_parameter = rice_parameter;
1813 best_partition_bits = partition_bits;
1814 }
1815 }
1816#endif
1817 parameters[partition] = best_rice_parameter;
1818 bits_ += best_partition_bits;
1819 }
1820 }
1821
1822 *bits = bits_;
1823 return true;
1824}
1825
1826#ifdef DONT_ESTIMATE_RICE_BITS
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001827FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__int32 residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
Josh Coalson8395d022001-07-12 21:25:22 +00001828#else
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001829FLAC__bool stream_encoder_set_partitioned_rice_with_precompute_(const FLAC__uint32 abs_residual[], const FLAC__uint64 abs_residual_partition_sums[], const unsigned raw_bits_per_partition[], const unsigned residual_samples, const unsigned predictor_order, const unsigned suggested_rice_parameter, const unsigned rice_parameter_search_dist, const unsigned partition_order, const FLAC__bool search_for_escapes, unsigned parameters[], unsigned raw_bits[], unsigned *bits)
Josh Coalson8395d022001-07-12 21:25:22 +00001830#endif
1831{
1832 unsigned rice_parameter, partition_bits;
1833#ifndef NO_RICE_SEARCH
1834 unsigned best_partition_bits;
1835 unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
1836#endif
1837 unsigned flat_bits;
1838 unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
1839
1840 FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
1841
1842 if(partition_order == 0) {
1843 unsigned i;
1844
1845#ifndef NO_RICE_SEARCH
1846 if(rice_parameter_search_dist) {
1847 if(suggested_rice_parameter < rice_parameter_search_dist)
1848 min_rice_parameter = 0;
1849 else
1850 min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
1851 max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
1852 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1853#ifdef DEBUG
1854 fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1855#endif
1856 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
1857 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001858 }
1859 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001860 min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00001861
Josh Coalson034dfab2001-04-27 19:10:23 +00001862 best_partition_bits = 0xffffffff;
1863 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1864#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001865#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001866#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001867 partition_bits = (2+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001868#else
Josh Coalson352e0f62001-03-20 22:55:50 +00001869 const unsigned rice_parameter_estimate = rice_parameter-1;
Josh Coalson034dfab2001-04-27 19:10:23 +00001870 partition_bits = (1+rice_parameter) * residual_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001871#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001872#else
1873 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001874#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001875 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson352e0f62001-03-20 22:55:50 +00001876 for(i = 0; i < residual_samples; i++) {
1877#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001878#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson2051dd42001-04-12 22:22:34 +00001879 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001880#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001881 partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001882#endif
1883#else
Josh Coalson2051dd42001-04-12 22:22:34 +00001884 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 +00001885#endif
Josh Coalson2051dd42001-04-12 22:22:34 +00001886 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001887#ifndef NO_RICE_SEARCH
1888 if(partition_bits < best_partition_bits) {
1889 best_rice_parameter = rice_parameter;
1890 best_partition_bits = partition_bits;
Josh Coalson352e0f62001-03-20 22:55:50 +00001891 }
1892 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001893#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001894 if(search_for_escapes) {
1895 flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
1896 if(flat_bits <= best_partition_bits) {
1897 raw_bits[0] = raw_bits_per_partition[0];
1898 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1899 best_partition_bits = flat_bits;
1900 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001901 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001902 parameters[0] = best_rice_parameter;
1903 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001904 }
1905 else {
Josh Coalson4dacd192001-06-06 21:11:44 +00001906 unsigned partition, residual_sample, save_residual_sample, partition_sample;
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001907 unsigned partition_samples;
1908 FLAC__uint64 mean, k;
Josh Coalsond4e0ddb2001-04-18 02:20:52 +00001909 const unsigned partitions = 1u << partition_order;
Josh Coalson4dacd192001-06-06 21:11:44 +00001910 for(partition = residual_sample = 0; partition < partitions; partition++) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001911 partition_samples = (residual_samples+predictor_order) >> partition_order;
Josh Coalson034dfab2001-04-27 19:10:23 +00001912 if(partition == 0) {
Josh Coalson94e02cd2001-01-25 10:41:06 +00001913 if(partition_samples <= predictor_order)
1914 return false;
1915 else
1916 partition_samples -= predictor_order;
1917 }
Josh Coalson05d20792001-06-29 23:12:26 +00001918 mean = abs_residual_partition_sums[partition];
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001919#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson05d20792001-06-29 23:12:26 +00001920 mean += partition_samples >> 1; /* for rounding effect */
1921 mean /= partition_samples;
1922
Josh Coalson034dfab2001-04-27 19:10:23 +00001923 /* calc rice_parameter = floor(log2(mean)) */
1924 rice_parameter = 0;
1925 mean>>=1;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001926 while(mean) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001927 rice_parameter++;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001928 mean >>= 1;
1929 }
Josh Coalsonb9433f92001-03-17 01:07:00 +00001930#else
Josh Coalson05d20792001-06-29 23:12:26 +00001931 /* calc rice_parameter ala LOCO-I */
Josh Coalsonb3347bd2001-07-16 18:06:41 +00001932 for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
Josh Coalson05d20792001-06-29 23:12:26 +00001933 ;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001934#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001935 if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1936#ifdef DEBUG
1937 fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1938#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001939 rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00001940 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001941
Josh Coalson034dfab2001-04-27 19:10:23 +00001942#ifndef NO_RICE_SEARCH
Josh Coalson60f77d72001-04-25 02:16:36 +00001943 if(rice_parameter_search_dist) {
Josh Coalson034dfab2001-04-27 19:10:23 +00001944 if(rice_parameter < rice_parameter_search_dist)
Josh Coalson60f77d72001-04-25 02:16:36 +00001945 min_rice_parameter = 0;
1946 else
Josh Coalson034dfab2001-04-27 19:10:23 +00001947 min_rice_parameter = rice_parameter - rice_parameter_search_dist;
1948 max_rice_parameter = rice_parameter + rice_parameter_search_dist;
Josh Coalson8395d022001-07-12 21:25:22 +00001949 if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1950#ifdef DEBUG
1951 fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
1952#endif
Josh Coalson60f77d72001-04-25 02:16:36 +00001953 max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
Josh Coalson8395d022001-07-12 21:25:22 +00001954 }
Josh Coalson60f77d72001-04-25 02:16:36 +00001955 }
1956 else
1957 min_rice_parameter = max_rice_parameter = rice_parameter;
Josh Coalson60f77d72001-04-25 02:16:36 +00001958
Josh Coalson034dfab2001-04-27 19:10:23 +00001959 best_partition_bits = 0xffffffff;
1960 for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
1961#endif
Josh Coalson352e0f62001-03-20 22:55:50 +00001962#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001963#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson034dfab2001-04-27 19:10:23 +00001964 partition_bits = (2+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001965#else
Josh Coalson034dfab2001-04-27 19:10:23 +00001966 const unsigned rice_parameter_estimate = rice_parameter-1;
1967 partition_bits = (1+rice_parameter) * partition_samples;
Josh Coalsonb9433f92001-03-17 01:07:00 +00001968#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001969#else
1970 partition_bits = 0;
Josh Coalson94e02cd2001-01-25 10:41:06 +00001971#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001972 partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
Josh Coalson4dacd192001-06-06 21:11:44 +00001973 save_residual_sample = residual_sample;
1974 for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
Josh Coalson352e0f62001-03-20 22:55:50 +00001975#ifdef VARIABLE_RICE_BITS
Josh Coalsonbb6712e2001-04-24 22:54:07 +00001976#ifdef FLAC__SYMMETRIC_RICE
Josh Coalson4dacd192001-06-06 21:11:44 +00001977 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
Josh Coalson94e02cd2001-01-25 10:41:06 +00001978#else
Josh Coalson4dacd192001-06-06 21:11:44 +00001979 partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
Josh Coalsonb9433f92001-03-17 01:07:00 +00001980#endif
1981#else
Josh Coalson4dacd192001-06-06 21:11:44 +00001982 partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
Josh Coalson94e02cd2001-01-25 10:41:06 +00001983#endif
Josh Coalson034dfab2001-04-27 19:10:23 +00001984 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001985#ifndef NO_RICE_SEARCH
Josh Coalson4dacd192001-06-06 21:11:44 +00001986 if(rice_parameter != max_rice_parameter)
1987 residual_sample = save_residual_sample;
Josh Coalson034dfab2001-04-27 19:10:23 +00001988 if(partition_bits < best_partition_bits) {
1989 best_rice_parameter = rice_parameter;
1990 best_partition_bits = partition_bits;
1991 }
Josh Coalson2051dd42001-04-12 22:22:34 +00001992 }
Josh Coalson034dfab2001-04-27 19:10:23 +00001993#endif
Josh Coalson8395d022001-07-12 21:25:22 +00001994 if(search_for_escapes) {
1995 flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
1996 if(flat_bits <= best_partition_bits) {
1997 raw_bits[partition] = raw_bits_per_partition[partition];
1998 best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
1999 best_partition_bits = flat_bits;
2000 }
Josh Coalson2051dd42001-04-12 22:22:34 +00002001 }
Josh Coalson034dfab2001-04-27 19:10:23 +00002002 parameters[partition] = best_rice_parameter;
2003 bits_ += best_partition_bits;
Josh Coalson94e02cd2001-01-25 10:41:06 +00002004 }
2005 }
2006
2007 *bits = bits_;
2008 return true;
2009}
Josh Coalson859bc542001-03-27 22:22:27 +00002010
Josh Coalson77e3f312001-06-23 03:03:24 +00002011unsigned stream_encoder_get_wasted_bits_(FLAC__int32 signal[], unsigned samples)
Josh Coalson859bc542001-03-27 22:22:27 +00002012{
2013 unsigned i, shift;
Josh Coalson77e3f312001-06-23 03:03:24 +00002014 FLAC__int32 x = 0;
Josh Coalson859bc542001-03-27 22:22:27 +00002015
2016 for(i = 0; i < samples && !(x&1); i++)
2017 x |= signal[i];
2018
2019 if(x == 0) {
2020 shift = 0;
2021 }
2022 else {
2023 for(shift = 0; !(x&1); shift++)
2024 x >>= 1;
2025 }
2026
2027 if(shift > 0) {
2028 for(i = 0; i < samples; i++)
2029 signal[i] >>= shift;
2030 }
2031
2032 return shift;
2033}