lpc.c 8.44 KB
Newer Older
1
/* libFLAC - Free Lossless Audio Codec library
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2000,2001  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
21
22
23
24
25
26
27
28
29
30
 *
 * 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 <assert.h>
#include <math.h>
#include <stdio.h>
#include "FLAC/format.h"
#include "private/lpc.h"

#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
31
#define LOCAL_FABS(x) ((x)<0.0? -(x):(x))
Josh Coalson's avatar
Josh Coalson committed
32

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

	assert(lag > 0);
	assert(lag <= data_len);

	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
54
55
56
57
58
59
60
61
62
#endif

	/*
	 * this version tends to run faster because of better data locality
	 * ('data_len' is usually much larger than 'lag')
	 */
	real d;
	unsigned sample, coeff;
	const unsigned limit = data_len - lag;

	assert(lag > 0);
	assert(lag <= data_len);

	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
75
76
77
78
79
80
81
82
83
84
85
86
87
}

void FLAC__lpc_compute_lp_coefficients(const real autoc[], unsigned max_order, real lp_coeff[][FLAC__MAX_LPC_ORDER], real error[])
{
	unsigned i, j;
	real r, err, ref[FLAC__MAX_LPC_ORDER], lpc[FLAC__MAX_LPC_ORDER];

	assert(0 < max_order);
	assert(max_order <= FLAC__MAX_LPC_ORDER);
	assert(autoc[0] != 0.0);

	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
96
97
98
99
100
101
102
103
104
105
106
		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++) {
			real tmp = lpc[j];
			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++)
Josh Coalson's avatar
Josh Coalson committed
107
			lp_coeff[i][j] = -lpc[j]; /* negate FIR filter coeff to get predictor coeff */
Josh Coalson's avatar
Josh Coalson committed
108
109
110
111
		error[i] = err;
	}
}

112
int FLAC__lpc_quantize_coefficients(const real lp_coeff[], unsigned order, unsigned precision, unsigned bits_per_sample, int32 qlp_coeff[], int *shift)
Josh Coalson's avatar
Josh Coalson committed
113
114
{
	unsigned i;
115
	real d, cmax = -1e10;
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

	assert(bits_per_sample > 0);
	assert(bits_per_sample <= sizeof(int32)*8);
	assert(precision > 0);
	assert(precision >= FLAC__MIN_QLP_COEFF_PRECISION);
	assert(precision + bits_per_sample < sizeof(int32)*8);
#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--;

	for(i = 0; i < order; i++) {
		if(lp_coeff[i] == 0.0)
			continue;
Josh Coalson's avatar
Josh Coalson committed
132
		d = LOCAL_FABS(lp_coeff[i]);
133
134
135
		if(d > cmax)
			cmax = d;
	}
Josh Coalson's avatar
Josh Coalson committed
136
137
	if(cmax < 0.0) {
		/* => coefficients are all 0, which means our constant-detect didn't work */
138
139
140
		return 2;
	}
	else {
Josh Coalson's avatar
Josh Coalson committed
141
		const int maxshift = (int)precision - (int)floor(log(cmax) / M_LN2) - 1;
142
143
144
145
146
147
148
149
150
151
152
153
154
		const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1;
		const int min_shiftlimit = -max_shiftlimit - 1;

		*shift = maxshift;

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

	if(*shift != 0) { /* just to avoid wasting time... */
		for(i = 0; i < order; i++)
			qlp_coeff[i] = (int32)floor(lp_coeff[i] * (real)(1 << *shift));
Josh Coalson's avatar
Josh Coalson committed
155
156
157
158
159
160
	}
	return 0;
}

void FLAC__lpc_compute_residual_from_qlp_coefficients(const int32 data[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 residual[])
{
Josh Coalson's avatar
Josh Coalson committed
161
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
162
163
164
165
166
167
	int64 sumo;
#endif
	unsigned i, j;
	int32 sum;
	const int32 *history;

Josh Coalson's avatar
Josh Coalson committed
168
#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
Josh Coalson's avatar
Josh Coalson committed
169
170
171
172
173
174
175
176
	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
	assert(order > 0);

	for(i = 0; i < data_len; i++) {
Josh Coalson's avatar
Josh Coalson committed
177
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
178
179
180
181
182
183
		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
184
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
185
			sumo += (int64)qlp_coeff[j] * (int64)(*history);
186
187
188
			if(sumo > 2147483647ll || sumo < -2147483648ll) {
				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);
			}
Josh Coalson's avatar
Josh Coalson committed
189
190
191
192
193
#endif
		}
		*(residual++) = *(data++) - (sum >> lp_quantization);
	}

194
	/* Here's a slower but clearer version:
Josh Coalson's avatar
Josh Coalson committed
195
196
197
	for(i = 0; i < data_len; i++) {
		sum = 0;
		for(j = 0; j < order; j++)
198
			sum += qlp_coeff[j] * data[i-j-1];
Josh Coalson's avatar
Josh Coalson committed
199
200
201
202
203
204
205
		residual[i] = data[i] - (sum >> lp_quantization);
	}
	*/
}

void FLAC__lpc_restore_signal(const int32 residual[], unsigned data_len, const int32 qlp_coeff[], unsigned order, int lp_quantization, int32 data[])
{
Josh Coalson's avatar
Josh Coalson committed
206
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
207
208
209
	int64 sumo;
#endif
	unsigned i, j;
210
211
	int32 sum;
	const int32 *history;
Josh Coalson's avatar
Josh Coalson committed
212

Josh Coalson's avatar
Josh Coalson committed
213
#ifdef FLAC__OVERFLOW_DETECT_VERBOSE
Josh Coalson's avatar
Josh Coalson committed
214
215
216
217
218
219
220
221
	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
	assert(order > 0);

	for(i = 0; i < data_len; i++) {
Josh Coalson's avatar
Josh Coalson committed
222
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
223
224
225
		sumo = 0;
#endif
		sum = 0;
226
		history = data;
Josh Coalson's avatar
Josh Coalson committed
227
228
		for(j = 0; j < order; j++) {
			sum += qlp_coeff[j] * (*(--history));
Josh Coalson's avatar
Josh Coalson committed
229
#ifdef FLAC__OVERFLOW_DETECT
Josh Coalson's avatar
Josh Coalson committed
230
			sumo += (int64)qlp_coeff[j] * (int64)(*history);
231
232
233
			if(sumo > 2147483647ll || sumo < -2147483648ll) {
				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);
			}
Josh Coalson's avatar
Josh Coalson committed
234
235
#endif
		}
236
237
238
239
240
241
242
243
		*(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
244
245
		data[i] = residual[i] + (sum >> lp_quantization);
	}
246
	*/
Josh Coalson's avatar
Josh Coalson committed
247
248
249
250
}

real FLAC__lpc_compute_expected_bits_per_residual_sample(real lpc_error, unsigned total_samples)
{
251
	real error_scale;
Josh Coalson's avatar
Josh Coalson committed
252
253
254

	assert(total_samples > 0);

255
	error_scale = 0.5 * M_LN2 * M_LN2 / (real)total_samples;
Josh Coalson's avatar
Josh Coalson committed
256

257
	if(lpc_error > 0.0) {
258
		real bps = 0.5 * log(error_scale * lpc_error) / M_LN2;
259
260
261
262
263
		if(bps >= 0.0)
			return bps;
		else
			return 0.0;
	}
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
	else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate float resolution */
		return 1e10;
	}
	else {
		return 0.0;
	}
}

real FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(real lpc_error, real error_scale)
{
	if(lpc_error > 0.0) {
		real bps = 0.5 * log(error_scale * lpc_error) / M_LN2;
		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 */
		return 1e10;
	}
284
	else {
Josh Coalson's avatar
Josh Coalson committed
285
		return 0.0;
286
	}
Josh Coalson's avatar
Josh Coalson committed
287
288
289
290
291
}

unsigned FLAC__lpc_compute_best_order(const real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample)
{
	unsigned order, best_order;
Josh Coalson's avatar
Josh Coalson committed
292
	real best_bits, tmp_bits, error_scale;
Josh Coalson's avatar
Josh Coalson committed
293
294

	assert(max_order > 0);
295
296
297
	assert(total_samples > 0);

	error_scale = 0.5 * M_LN2 * M_LN2 / (real)total_samples;
Josh Coalson's avatar
Josh Coalson committed
298
299

	best_order = 0;
300
	best_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[0], error_scale) * (real)total_samples;
Josh Coalson's avatar
Josh Coalson committed
301
302

	for(order = 1; order < max_order; order++) {
303
		tmp_bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[order], error_scale) * (real)(total_samples - order) + (real)(order * bits_per_signal_sample);
Josh Coalson's avatar
Josh Coalson committed
304
305
306
307
308
309
310
311
		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 */
}