lpc.c 10.6 KB
Newer Older
1
/* libFLAC - Free Lossless Audio Codec library
2
 * Copyright (C) 2000,2001,2002  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 */

#include <math.h>
21
#include "FLAC/assert.h"
Josh Coalson's avatar
Josh Coalson committed
22
23
#include "FLAC/format.h"
#include "private/lpc.h"
24
25
26
#if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE
#include <stdio.h>
#endif
Josh Coalson's avatar
Josh Coalson committed
27
28
29
30
31
32

#ifndef M_LN2
/* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
#define M_LN2 0.69314718055994530942
#endif

Josh Coalson's avatar
Josh Coalson committed
33
void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[])
Josh Coalson's avatar
Josh Coalson committed
34
{
35
36
	/* a readable, but slower, version */
#if 0
Josh Coalson's avatar
Josh Coalson committed
37
	FLAC__real d;
Josh Coalson's avatar
Josh Coalson committed
38
39
	unsigned i;

40
41
	FLAC__ASSERT(lag > 0);
	FLAC__ASSERT(lag <= data_len);
Josh Coalson's avatar
Josh Coalson committed
42
43
44
45
46
47

	while(lag--) {
		for(i = lag, d = 0.0; i < data_len; i++)
			d += data[i] * data[i - lag];
		autoc[lag] = d;
	}
48
49
50
51
52
53
#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
54
	FLAC__real d;
55
56
57
	unsigned sample, coeff;
	const unsigned limit = data_len - lag;

58
59
	FLAC__ASSERT(lag > 0);
	FLAC__ASSERT(lag <= data_len);
60
61
62

	for(coeff = 0; coeff < lag; coeff++)
		autoc[coeff] = 0.0;
Josh Coalson's avatar
Josh Coalson committed
63
	for(sample = 0; sample <= limit; sample++) {
64
65
66
67
		d = data[sample];
		for(coeff = 0; coeff < lag; coeff++)
			autoc[coeff] += d * data[sample+coeff];
	}
Josh Coalson's avatar
Josh Coalson committed
68
	for(; sample < data_len; sample++) {
69
70
71
72
		d = data[sample];
		for(coeff = 0; coeff < data_len - sample; coeff++)
			autoc[coeff] += d * data[sample+coeff];
	}
Josh Coalson's avatar
Josh Coalson committed
73
74
}

Josh Coalson's avatar
Josh Coalson committed
75
void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__real error[])
Josh Coalson's avatar
Josh Coalson committed
76
77
{
	unsigned i, j;
78
	double r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];
Josh Coalson's avatar
Josh Coalson committed
79

80
81
82
	FLAC__ASSERT(0 < max_order);
	FLAC__ASSERT(max_order <= FLAC__MAX_LPC_ORDER);
	FLAC__ASSERT(autoc[0] != 0.0);
Josh Coalson's avatar
Josh Coalson committed
83
84
85
86
87

	err = autoc[0];

	for(i = 0; i < max_order; i++) {
		/* Sum up this iteration's reflection coefficient. */
88
		r = -autoc[i+1];
Josh Coalson's avatar
Josh Coalson committed
89
90
91
92
93
94
95
		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++) {
96
			double tmp = lpc[j];
Josh Coalson's avatar
Josh Coalson committed
97
98
99
100
101
102
103
104
105
106
			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++)
107
108
			lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */
		error[i] = (FLAC__real)err;
Josh Coalson's avatar
Josh Coalson committed
109
110
111
	}
}

Josh Coalson's avatar
Josh Coalson committed
112
int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, unsigned bits_per_sample, FLAC__int32 qlp_coeff[], int *shift)
Josh Coalson's avatar
Josh Coalson committed
113
114
{
	unsigned i;
115
	double d, cmax = -1e32;
116
117
118
	FLAC__int32 qmax, qmin;
	const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
	const int min_shiftlimit = -max_shiftlimit - 1;
119

120
	FLAC__ASSERT(bits_per_sample > 0);
Josh Coalson's avatar
Josh Coalson committed
121
	FLAC__ASSERT(bits_per_sample <= sizeof(FLAC__int32)*8);
122
123
	FLAC__ASSERT(precision > 0);
	FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
Josh Coalson's avatar
Josh Coalson committed
124
	FLAC__ASSERT(precision + bits_per_sample < sizeof(FLAC__int32)*8);
125
126
127
128
129
130
#ifdef NDEBUG
	(void)bits_per_sample; /* silence compiler warning about unused parameter */
#endif

	/* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */
	precision--;
131
132
133
	qmax = 1 << precision;
	qmin = -qmax;
	qmax--;
134
135
136
137

	for(i = 0; i < order; i++) {
		if(lp_coeff[i] == 0.0)
			continue;
138
		d = fabs(lp_coeff[i]);
139
140
141
		if(d > cmax)
			cmax = d;
	}
142
redo_it:
143
	if(cmax <= 0.0) {
Josh Coalson's avatar
Josh Coalson committed
144
		/* => coefficients are all 0, which means our constant-detect didn't work */
145
146
147
		return 2;
	}
	else {
148
		int log2cmax;
149

150
151
152
		(void)frexp(cmax, &log2cmax);	
		log2cmax--;
		*shift = (int)precision - log2cmax - 1;
153
154
155
156
157
158

		if(*shift < min_shiftlimit || *shift > max_shiftlimit) {
			return 1;
		}
	}

159
160
161
	if(*shift >= 0) {
		for(i = 0; i < order; i++) {
			qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] * (double)(1 << *shift));
162

163
			/* double-check the result */
164
			if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
165
#ifdef FLAC__OVERFLOW_DETECT
166
				fprintf(stderr,"FLAC__lpc_quantize_coefficients: compensating for overflow, qlp_coeff[%u]=%d, lp_coeff[%u]=%f, cmax=%f, precision=%u, shift=%d, q=%f, f(q)=%f\n", i, qlp_coeff[i], i, lp_coeff[i], cmax, precision, *shift, (double)lp_coeff[i] * (double)(1 << *shift), floor((double)lp_coeff[i] * (double)(1 << *shift)));
167
#endif
168
169
				cmax *= 2.0;
				goto redo_it;
170
171
			}
		}
172
173
174
	}
	else { /* (*shift < 0) */
		const int nshift = -(*shift);
Josh Coalson's avatar
Josh Coalson committed
175
#ifdef DEBUG
176
		fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift = %d\n", *shift);
Josh Coalson's avatar
Josh Coalson committed
177
#endif
178
179
180
		for(i = 0; i < order; i++) {
			qlp_coeff[i] = (FLAC__int32)floor((double)lp_coeff[i] / (double)(1 << nshift));

181
			/* double-check the result */
182
			if(qlp_coeff[i] > qmax || qlp_coeff[i] < qmin) {
183
#ifdef FLAC__OVERFLOW_DETECT
184
				fprintf(stderr,"FLAC__lpc_quantize_coefficients: compensating for overflow, qlp_coeff[%u]=%d, lp_coeff[%u]=%f, cmax=%f, precision=%u, shift=%d, q=%f, f(q)=%f\n", i, qlp_coeff[i], i, lp_coeff[i], cmax, precision, *shift, (double)lp_coeff[i] / (double)(1 << nshift), floor((double)lp_coeff[i] / (double)(1 << nshift)));
185
#endif
186
187
				cmax *= 2.0;
				goto redo_it;
188
189
			}
		}
Josh Coalson's avatar
Josh Coalson committed
190
	}
191

Josh Coalson's avatar
Josh Coalson committed
192
193
194
	return 0;
}

Josh Coalson's avatar
Josh Coalson committed
195
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
196
{
Josh Coalson's avatar
Josh Coalson committed
197
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
198
	FLAC__int64 sumo;
Josh Coalson's avatar
Josh Coalson committed
199
200
#endif
	unsigned i, j;
Josh Coalson's avatar
Josh Coalson committed
201
202
	FLAC__int32 sum;
	const FLAC__int32 *history;
Josh Coalson's avatar
Josh Coalson committed
203

Josh Coalson's avatar
Josh Coalson committed
204
#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
Josh Coalson's avatar
Josh Coalson committed
205
206
207
208
209
	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
210
	FLAC__ASSERT(order > 0);
Josh Coalson's avatar
Josh Coalson committed
211
212

	for(i = 0; i < data_len; i++) {
Josh Coalson's avatar
Josh Coalson committed
213
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
214
215
216
217
218
219
		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
220
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
221
			sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
222
#if defined _MSC_VER || defined __MINGW32__ /* don't know how to do 64-bit literals in VC++ */
223
224
225
226
227
228
			if(sumo < 0) sumo = -sumo;
			if(sumo > 2147483647)
#else
			if(sumo > 2147483647ll || sumo < -2147483648ll)
#endif
			{
229
				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,sumo);
230
			}
Josh Coalson's avatar
Josh Coalson committed
231
232
233
234
235
#endif
		}
		*(residual++) = *(data++) - (sum >> lp_quantization);
	}

236
	/* Here's a slower but clearer version:
Josh Coalson's avatar
Josh Coalson committed
237
238
239
	for(i = 0; i < data_len; i++) {
		sum = 0;
		for(j = 0; j < order; j++)
240
			sum += qlp_coeff[j] * data[i-j-1];
Josh Coalson's avatar
Josh Coalson committed
241
242
243
244
245
		residual[i] = data[i] - (sum >> lp_quantization);
	}
	*/
}

Josh Coalson's avatar
Josh Coalson committed
246
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
247
{
Josh Coalson's avatar
Josh Coalson committed
248
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
249
	FLAC__int64 sumo;
Josh Coalson's avatar
Josh Coalson committed
250
251
#endif
	unsigned i, j;
Josh Coalson's avatar
Josh Coalson committed
252
253
	FLAC__int32 sum;
	const FLAC__int32 *history;
Josh Coalson's avatar
Josh Coalson committed
254

Josh Coalson's avatar
Josh Coalson committed
255
#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
Josh Coalson's avatar
Josh Coalson committed
256
257
258
259
260
	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
261
	FLAC__ASSERT(order > 0);
Josh Coalson's avatar
Josh Coalson committed
262
263

	for(i = 0; i < data_len; i++) {
Josh Coalson's avatar
Josh Coalson committed
264
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
265
266
267
		sumo = 0;
#endif
		sum = 0;
268
		history = data;
Josh Coalson's avatar
Josh Coalson committed
269
270
		for(j = 0; j < order; j++) {
			sum += qlp_coeff[j] * (*(--history));
Josh Coalson's avatar
Josh Coalson committed
271
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
272
			sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history);
273
#if defined _MSC_VER || defined __MINGW32__ /* don't know how to do 64-bit literals in VC++ */
274
275
276
277
278
279
			if(sumo < 0) sumo = -sumo;
			if(sumo > 2147483647)
#else
			if(sumo > 2147483647ll || sumo < -2147483648ll)
#endif
			{
280
				fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%lld\n",i,j,qlp_coeff[j],*history,sumo);
281
			}
Josh Coalson's avatar
Josh Coalson committed
282
283
#endif
		}
284
285
286
287
288
289
290
291
		*(data++) = *(residual++) + (sum >> lp_quantization);
	}

	/* 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
292
293
		data[i] = residual[i] + (sum >> lp_quantization);
	}
294
	*/
Josh Coalson's avatar
Josh Coalson committed
295
296
}

Josh Coalson's avatar
Josh Coalson committed
297
FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__real lpc_error, unsigned total_samples)
Josh Coalson's avatar
Josh Coalson committed
298
{
299
	double error_scale;
Josh Coalson's avatar
Josh Coalson committed
300

301
	FLAC__ASSERT(total_samples > 0);
Josh Coalson's avatar
Josh Coalson committed
302

Josh Coalson's avatar
Josh Coalson committed
303
	error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
Josh Coalson's avatar
Josh Coalson committed
304

305
	if(lpc_error > 0.0) {
306
		FLAC__real bps = (FLAC__real)((double)0.5 * log(error_scale * lpc_error) / M_LN2);
307
308
309
310
311
		if(bps >= 0.0)
			return bps;
		else
			return 0.0;
	}
312
	else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
313
		return (FLAC__real)1e32;
314
315
316
317
318
319
	}
	else {
		return 0.0;
	}
}

320
FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__real lpc_error, double error_scale)
321
322
{
	if(lpc_error > 0.0) {
323
		FLAC__real bps = (FLAC__real)((double)0.5 * log(error_scale * lpc_error) / M_LN2);
324
325
326
327
328
329
		if(bps >= 0.0)
			return bps;
		else
			return 0.0;
	}
	else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
330
		return (FLAC__real)1e32;
331
	}
332
	else {
Josh Coalson's avatar
Josh Coalson committed
333
		return 0.0;
334
	}
Josh Coalson's avatar
Josh Coalson committed
335
336
}

Josh Coalson's avatar
Josh Coalson committed
337
unsigned FLAC__lpc_compute_best_order(const FLAC__real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
Josh Coalson's avatar
Josh Coalson committed
338
339
{
	unsigned order, best_order;
340
341
	FLAC__real best_bits, tmp_bits;
	double error_scale;
Josh Coalson's avatar
Josh Coalson committed
342

343
344
	FLAC__ASSERT(max_order > 0);
	FLAC__ASSERT(total_samples > 0);
345

Josh Coalson's avatar
Josh Coalson committed
346
	error_scale = 0.5 * M_LN2 * M_LN2 / (FLAC__real)total_samples;
Josh Coalson's avatar
Josh Coalson committed
347
348

	best_order = 0;
Josh Coalson's avatar
Josh Coalson committed
349
	best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (FLAC__real)total_samples;
Josh Coalson's avatar
Josh Coalson committed
350
351

	for(order = 1; order < max_order; order++) {
Josh Coalson's avatar
Josh Coalson committed
352
		tmp_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[order], error_scale) * (FLAC__real)(total_samples - order) + (FLAC__real)(order * bits_per_signal_sample);
Josh Coalson's avatar
Josh Coalson committed
353
354
355
356
357
358
359
360
		if(tmp_bits < best_bits) {
			best_order = order;
			best_bits = tmp_bits;
		}
	}

	return best_order+1; /* +1 since index of lpc_error[] is order-1 */
}