lpc.c 15.2 KB
Newer Older
1
/* libFLAC - Free Lossless Audio Codec library
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
 *
4
5
6
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
Josh Coalson's avatar
Josh Coalson committed
7
 *
8
9
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
Josh Coalson's avatar
Josh Coalson committed
10
 *
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the Xiph.org Foundation nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalson's avatar
Josh Coalson committed
30
31
 */

Josh Coalson's avatar
Josh Coalson committed
32
33
34
35
#if HAVE_CONFIG_H
#  include <config.h>
#endif

Josh Coalson's avatar
Josh Coalson committed
36
#include <math.h>
37
#include "FLAC/assert.h"
Josh Coalson's avatar
Josh Coalson committed
38
#include "FLAC/format.h"
39
#include "private/bitmath.h"
Josh Coalson's avatar
Josh Coalson committed
40
#include "private/lpc.h"
41
42
43
#if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE
#include <stdio.h>
#endif
Josh Coalson's avatar
Josh Coalson committed
44

45
46
#ifndef FLAC__INTEGER_ONLY_LIBRARY

Josh Coalson's avatar
Josh Coalson committed
47
48
49
50
51
#ifndef M_LN2
/* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
#define M_LN2 0.69314718055994530942
#endif

52
53
54
55
56
57
58
void FLAC__lpc_window_data(const FLAC__real in[], const FLAC__real window[], FLAC__real out[], unsigned data_len)
{
	unsigned i;
	for(i = 0; i < data_len; i++)
		out[i] = in[i] * window[i];
}

Josh Coalson's avatar
Josh Coalson committed
59
void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[])
Josh Coalson's avatar
Josh Coalson committed
60
{
61
62
	/* a readable, but slower, version */
#if 0
Josh Coalson's avatar
Josh Coalson committed
63
	FLAC__real d;
Josh Coalson's avatar
Josh Coalson committed
64
65
	unsigned i;

66
67
	FLAC__ASSERT(lag > 0);
	FLAC__ASSERT(lag <= data_len);
Josh Coalson's avatar
Josh Coalson committed
68

69
70
71
72
73
74
75
	/*
	 * Technically we should subtract the mean first like so:
	 *   for(i = 0; i < data_len; i++)
	 *     data[i] -= mean;
	 * but it appears not to make enough of a difference to matter, and
	 * most signals are already closely centered around zero
	 */
Josh Coalson's avatar
Josh Coalson committed
76
77
78
79
80
	while(lag--) {
		for(i = lag, d = 0.0; i < data_len; i++)
			d += data[i] * data[i - lag];
		autoc[lag] = d;
	}
81
82
83
84
85
86
#endif

	/*
	 * this version tends to run faster because of better data locality
	 * ('data_len' is usually much larger than 'lag')
	 */
Josh Coalson's avatar
Josh Coalson committed
87
	FLAC__real d;
88
89
90
	unsigned sample, coeff;
	const unsigned limit = data_len - lag;

91
92
	FLAC__ASSERT(lag > 0);
	FLAC__ASSERT(lag <= data_len);
93
94
95

	for(coeff = 0; coeff < lag; coeff++)
		autoc[coeff] = 0.0;
Josh Coalson's avatar
Josh Coalson committed
96
	for(sample = 0; sample <= limit; sample++) {
97
98
99
100
		d = data[sample];
		for(coeff = 0; coeff < lag; coeff++)
			autoc[coeff] += d * data[sample+coeff];
	}
Josh Coalson's avatar
Josh Coalson committed
101
	for(; sample < data_len; sample++) {
102
103
104
105
		d = data[sample];
		for(coeff = 0; coeff < data_len - sample; coeff++)
			autoc[coeff] += d * data[sample+coeff];
	}
Josh Coalson's avatar
Josh Coalson committed
106
107
}

108
void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[])
Josh Coalson's avatar
Josh Coalson committed
109
110
{
	unsigned i, j;
111
	FLAC__double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
Josh Coalson's avatar
Josh Coalson committed
112

113
114
115
	FLAC__ASSERT(0 != max_order);
	FLAC__ASSERT(0 < *max_order);
	FLAC__ASSERT(*max_order <= FLAC__MAX_LPC_ORDER);
116
	FLAC__ASSERT(autoc[0] != 0.0);
Josh Coalson's avatar
Josh Coalson committed
117
118
119

	err = autoc[0];

120
	for(i = 0; i < *max_order; i++) {
Josh Coalson's avatar
Josh Coalson committed
121
		/* Sum up this iteration's reflection coefficient. */
122
		r = -autoc[i+1];
Josh Coalson's avatar
Josh Coalson committed
123
124
125
126
127
128
129
		for(j = 0; j < i; j++)
			r -= lpc[j] * autoc[i-j];
		ref[i] = (r/=err);

		/* Update LPC coefficients and total error. */
		lpc[i]=r;
		for(j = 0; j < (i>>1); j++) {
130
			FLAC__double tmp = lpc[j];
Josh Coalson's avatar
Josh Coalson committed
131
132
133
134
135
136
137
138
139
140
			lpc[j] += r * lpc[i-1-j];
			lpc[i-1-j] += r * tmp;
		}
		if(i & 1)
			lpc[j] += lpc[j] * r;

		err *= (1.0 - r * r);

		/* save this order */
		for(j = 0; j <= i; j++)
141
			lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */
142
		error[i] = err;
143

Josh Coalson's avatar
Josh Coalson committed
144
		/* see SF bug #1601812 http://sourceforge.net/tracker/index.php?func=detail&aid=1601812&group_id=13478&atid=113478 */
145
146
147
148
		if(err == 0.0) {
			*max_order = i+1;
			return;
		}
Josh Coalson's avatar
Josh Coalson committed
149
150
151
	}
}

152
int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift)
Josh Coalson's avatar
Josh Coalson committed
153
154
{
	unsigned i;
155
	FLAC__double cmax;
156
	FLAC__int32 qmax, qmin;
157

158
159
	FLAC__ASSERT(precision > 0);
	FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
160
161
162

	/* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
	precision--;
163
164
165
	qmax = 1 << precision;
	qmin = -qmax;
	qmax--;
166

167
168
	/* calc cmax = max( |lp_coeff[i]| ) */
	cmax = 0.0;
169
	for(i = 0; i < order; i++) {
170
		const FLAC__double d = fabs(lp_coeff[i]);
171
172
173
		if(d > cmax)
			cmax = d;
	}
174

175
	if(cmax <= 0.0) {
Josh Coalson's avatar
Josh Coalson committed
176
		/* => coefficients are all 0, which means our constant-detect didn't work */
177
178
179
		return 2;
	}
	else {
180
181
		const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
		const int min_shiftlimit = -max_shiftlimit - 1;
182
		int log2cmax;
183

Josh Coalson's avatar
Josh Coalson committed
184
		(void)frexp(cmax, &log2cmax);
185
186
		log2cmax--;
		*shift = (int)precision - log2cmax - 1;
187
188

		if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
189
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
190
			fprintf(stderr,"FLAC__lpc_quantize_coefficients: shift out of limit: shift=%d cmax=%f precision=%u\n",*shift,cmax,precision+1);
191
#endif
192
193
194
195
196
197
198
#if 0
			/*@@@ this does not seem to help at all, but was not extensively tested either: */
			if(*shift > max_shiftlimit)
				*shift = max_shiftlimit;
			else
#endif
				return 1;
199
200
201
		}
	}

202
	if(*shift >= 0) {
203
204
		FLAC__double error = 0.0;
		FLAC__int32 q;
205
		for(i = 0; i < order; i++) {
206
207
			error += lp_coeff[i] * (1 << *shift);
			q = lround(error); /* round() is also suitable */
208
#ifdef FLAC__OVERFLOW_DETECT
209
210
211
212
			if(q > qmax)
				fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
			else if(q < qmin)
				fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
213
#endif
214
215
216
217
218
219
			if(q > qmax)
				q = qmax;
			else if(q < qmin)
				q = qmin;
			error -= q;
			qlp_coeff[i] = q;
220
		}
221
	}
222
223
224
225
226
	/* negative shift is very rare but due to design flaw, negative shift is
	 * a NOP in the decoder, so it must be handled specially by scaling down
	 * coeffs
	 */
	else {
227
		const int nshift = -(*shift);
228
229
		FLAC__double error = 0.0;
		FLAC__int32 q;
Josh Coalson's avatar
Josh Coalson committed
230
#ifdef DEBUG
Josh Coalson's avatar
Josh Coalson committed
231
		fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift=%d order=%u cmax=%f\n", *shift, order, cmax);
Josh Coalson's avatar
Josh Coalson committed
232
#endif
233
		for(i = 0; i < order; i++) {
234
235
			error += lp_coeff[i] / (1 << nshift);
			q = lround(error); /* round() is also suitable */
236
#ifdef FLAC__OVERFLOW_DETECT
237
238
239
240
			if(q > qmax)
				fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]);
			else if(q < qmin)
				fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]);
241
#endif
242
243
244
245
246
247
			if(q > qmax)
				q = qmax;
			else if(q < qmin)
				q = qmin;
			error -= q;
			qlp_coeff[i] = q;
248
		}
249
		*shift = 0;
Josh Coalson's avatar
Josh Coalson committed
250
	}
251

Josh Coalson's avatar
Josh Coalson committed
252
253
254
	return 0;
}

255
void FLAC__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[])
Josh Coalson's avatar
Josh Coalson committed
256
{
Josh Coalson's avatar
Josh Coalson committed
257
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
258
	FLAC__int64 sumo;
Josh Coalson's avatar
Josh Coalson committed
259
260
#endif
	unsigned i, j;
Josh Coalson's avatar
Josh Coalson committed
261
262
	FLAC__int32 sum;
	const FLAC__int32 *history;
Josh Coalson's avatar
Josh Coalson committed
263

Josh Coalson's avatar
Josh Coalson committed
264
#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
Josh Coalson's avatar
Josh Coalson committed
265
266
267
268
269
	fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
	for(i=0;i<order;i++)
		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
	fprintf(stderr,"\n");
#endif
270
	FLAC__ASSERT(order > 0);
Josh Coalson's avatar
Josh Coalson committed
271
272

	for(i = 0; i < data_len; i++) {
Josh Coalson's avatar
Josh Coalson committed
273
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
274
275
276
277
278
279
		sumo = 0;
#endif
		sum = 0;
		history = data;
		for(j = 0; j < order; j++) {
			sum += qlp_coeff[j] * (*(--history));
Josh Coalson's avatar
Josh Coalson committed
280
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
281
			sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
Josh Coalson's avatar
Josh Coalson committed
282
#if defined _MSC_VER
283
			if(sumo > 2147483647I64 || sumo < -2147483648I64)
284
				fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
285
286
#else
			if(sumo > 2147483647ll || sumo < -2147483648ll)
Josh Coalson's avatar
Josh Coalson committed
287
				fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo);
288
#endif
Josh Coalson's avatar
Josh Coalson committed
289
290
291
292
293
#endif
		}
		*(residual++) = *(data++) - (sum >> lp_quantization);
	}

294
	/* Here's a slower but clearer version:
Josh Coalson's avatar
Josh Coalson committed
295
296
297
	for(i = 0; i < data_len; i++) {
		sum = 0;
		for(j = 0; j < order; j++)
298
			sum += qlp_coeff[j] * data[i-j-1];
Josh Coalson's avatar
Josh Coalson committed
299
300
301
302
303
		residual[i] = data[i] - (sum >> lp_quantization);
	}
	*/
}

304
void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[])
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
{
	unsigned i, j;
	FLAC__int64 sum;
	const FLAC__int32 *history;

#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
	fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
	for(i=0;i<order;i++)
		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
	fprintf(stderr,"\n");
#endif
	FLAC__ASSERT(order > 0);

	for(i = 0; i < data_len; i++) {
		sum = 0;
		history = data;
		for(j = 0; j < order; j++)
			sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
#ifdef FLAC__OVERFLOW_DETECT
		if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
Josh Coalson's avatar
Josh Coalson committed
325
			fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
326
327
328
			break;
		}
		if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) {
Josh Coalson's avatar
Josh Coalson committed
329
			fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%lld, residual=%lld\n", i, *data, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*data) - (sum >> lp_quantization)));
330
331
332
333
334
335
336
			break;
		}
#endif
		*(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization);
	}
}

337
338
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */

Josh Coalson's avatar
Josh Coalson committed
339
void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[])
Josh Coalson's avatar
Josh Coalson committed
340
{
Josh Coalson's avatar
Josh Coalson committed
341
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
342
	FLAC__int64 sumo;
Josh Coalson's avatar
Josh Coalson committed
343
344
#endif
	unsigned i, j;
Josh Coalson's avatar
Josh Coalson committed
345
	FLAC__int32 sum;
346
	const FLAC__int32 *r = residual, *history;
Josh Coalson's avatar
Josh Coalson committed
347

Josh Coalson's avatar
Josh Coalson committed
348
#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
Josh Coalson's avatar
Josh Coalson committed
349
350
351
352
353
	fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
	for(i=0;i<order;i++)
		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
	fprintf(stderr,"\n");
#endif
354
	FLAC__ASSERT(order > 0);
Josh Coalson's avatar
Josh Coalson committed
355
356

	for(i = 0; i < data_len; i++) {
Josh Coalson's avatar
Josh Coalson committed
357
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
358
359
360
		sumo = 0;
#endif
		sum = 0;
361
		history = data;
Josh Coalson's avatar
Josh Coalson committed
362
363
		for(j = 0; j < order; j++) {
			sum += qlp_coeff[j] * (*(--history));
Josh Coalson's avatar
Josh Coalson committed
364
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
365
			sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
Josh Coalson's avatar
Josh Coalson committed
366
#if defined _MSC_VER
367
			if(sumo > 2147483647I64 || sumo < -2147483648I64)
368
				fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%I64d\n",i,j,qlp_coeff[j],*history,sumo);
369
370
#else
			if(sumo > 2147483647ll || sumo < -2147483648ll)
Josh Coalson's avatar
Josh Coalson committed
371
				fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,(long long)sumo);
372
#endif
Josh Coalson's avatar
Josh Coalson committed
373
374
#endif
		}
375
		*(data++) = *(r++) + (sum >> lp_quantization);
376
377
378
379
380
381
382
	}

	/* Here's a slower but clearer version:
	for(i = 0; i < data_len; i++) {
		sum = 0;
		for(j = 0; j < order; j++)
			sum += qlp_coeff[j] * data[i-j-1];
Josh Coalson's avatar
Josh Coalson committed
383
384
		data[i] = residual[i] + (sum >> lp_quantization);
	}
385
	*/
Josh Coalson's avatar
Josh Coalson committed
386
387
}

388
389
390
391
void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[])
{
	unsigned i, j;
	FLAC__int64 sum;
392
	const FLAC__int32 *r = residual, *history;
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
	fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization);
	for(i=0;i<order;i++)
		fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]);
	fprintf(stderr,"\n");
#endif
	FLAC__ASSERT(order > 0);

	for(i = 0; i < data_len; i++) {
		sum = 0;
		history = data;
		for(j = 0; j < order; j++)
			sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history));
#ifdef FLAC__OVERFLOW_DETECT
		if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) {
Josh Coalson's avatar
Josh Coalson committed
409
			fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%lld\n", i, (long long)(sum >> lp_quantization));
410
411
			break;
		}
412
413
		if(FLAC__bitmath_silog2_wide((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) {
			fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%lld, data=%lld\n", i, *r, (long long)(sum >> lp_quantization), (long long)((FLAC__int64)(*r) + (sum >> lp_quantization)));
414
415
416
			break;
		}
#endif
417
		*(data++) = *(r++) + (FLAC__int32)(sum >> lp_quantization);
418
419
420
	}
}

421
422
#ifndef FLAC__INTEGER_ONLY_LIBRARY

423
FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples)
Josh Coalson's avatar
Josh Coalson committed
424
{
425
	FLAC__double error_scale;
Josh Coalson's avatar
Josh Coalson committed
426

427
	FLAC__ASSERT(total_samples > 0);
Josh Coalson's avatar
Josh Coalson committed
428

429
	error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
Josh Coalson's avatar
Josh Coalson committed
430

Josh Coalson's avatar
Josh Coalson committed
431
	return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale);
432
433
}

434
FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale)
435
436
{
	if(lpc_error > 0.0) {
437
		FLAC__double bps = (FLAC__double)0.5 * log(error_scale * lpc_error) / M_LN2;
438
439
440
441
442
		if(bps >= 0.0)
			return bps;
		else
			return 0.0;
	}
443
444
	else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate floating-point resolution */
		return 1e32;
445
	}
446
	else {
Josh Coalson's avatar
Josh Coalson committed
447
		return 0.0;
448
	}
Josh Coalson's avatar
Josh Coalson committed
449
450
}

451
unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order)
Josh Coalson's avatar
Josh Coalson committed
452
{
453
454
	unsigned order, index, best_index; /* 'index' the index into lpc_error; index==order-1 since lpc_error[0] is for order==1, lpc_error[1] is for order==2, etc */
	FLAC__double bits, best_bits, error_scale;
Josh Coalson's avatar
Josh Coalson committed
455

456
457
	FLAC__ASSERT(max_order > 0);
	FLAC__ASSERT(total_samples > 0);
458

459
	error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__double)total_samples;
Josh Coalson's avatar
Josh Coalson committed
460

461
462
	best_index = 0;
	best_bits = (unsigned)(-1);
Josh Coalson's avatar
Josh Coalson committed
463

464
465
466
467
468
	for(index = 0, order = 1; index < max_order; index++, order++) {
		bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[index], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * overhead_bits_per_order);
		if(bits < best_bits) {
			best_index = index;
			best_bits = bits;
Josh Coalson's avatar
Josh Coalson committed
469
470
471
		}
	}

472
	return best_index+1; /* +1 since index of lpc_error[] is order-1 */
Josh Coalson's avatar
Josh Coalson committed
473
}
474
475

#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */