1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001 Josh Coalson
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.
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.
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.
22 #include "private/fixed.h"
25 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
26 #define M_LN2 0.69314718055994530942
32 #define min(x,y) ((x) < (y)? (x) : (y))
37 #define local_abs(x) ((x)<0? -(x) : (x))
39 unsigned FLAC__fixed_compute_best_predictor(const int32 data[], unsigned data_len, real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
41 int32 last_error_0 = data[-1];
42 int32 last_error_1 = data[-1] - data[-2];
43 int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
44 int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
45 int32 error_0, error_1, error_2, error_3, error_4;
46 int32 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
49 for(i = 0; i < data_len; i++) {
50 error_0 = data[i] ; total_error_0 += local_abs(error_0);
51 error_1 = error_0 - last_error_0; total_error_1 += local_abs(error_1);
52 error_2 = error_1 - last_error_1; total_error_2 += local_abs(error_2);
53 error_3 = error_2 - last_error_2; total_error_3 += local_abs(error_3);
54 error_4 = error_3 - last_error_3; total_error_4 += local_abs(error_4);
56 last_error_0 = error_0;
57 last_error_1 = error_1;
58 last_error_2 = error_2;
59 last_error_3 = error_3;
62 if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4))
64 else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4))
66 else if(total_error_2 < min(total_error_3, total_error_4))
68 else if(total_error_3 < total_error_4)
73 /* Estimate the expected number of bits per residual signal sample. */
74 /* 'total_error*' is linearly related to the variance of the residual */
75 /* signal, so we use it directly to compute E(|x|) */
76 residual_bits_per_sample[0] = (real)((total_error_0 > 0 && data_len > 0) ? log(M_LN2 * total_error_0 / (real) data_len) / M_LN2 : 0.0);
77 residual_bits_per_sample[1] = (real)((total_error_1 > 0 && data_len > 0) ? log(M_LN2 * total_error_1 / (real) data_len) / M_LN2 : 0.0);
78 residual_bits_per_sample[2] = (real)((total_error_2 > 0 && data_len > 0) ? log(M_LN2 * total_error_2 / (real) data_len) / M_LN2 : 0.0);
79 residual_bits_per_sample[3] = (real)((total_error_3 > 0 && data_len > 0) ? log(M_LN2 * total_error_3 / (real) data_len) / M_LN2 : 0.0);
80 residual_bits_per_sample[4] = (real)((total_error_4 > 0 && data_len > 0) ? log(M_LN2 * total_error_4 / (real) data_len) / M_LN2 : 0.0);
85 void FLAC__fixed_compute_residual(const int32 data[], unsigned data_len, unsigned order, int32 residual[])
91 for(i = 0; i < data_len; i++) {
92 residual[i] = data[i];
96 for(i = 0; i < data_len; i++) {
97 residual[i] = data[i] - data[i-1];
101 for(i = 0; i < data_len; i++) {
102 /* == data[i] - 2*data[i-1] + data[i-2] */
103 residual[i] = data[i] - (data[i-1] << 1) + data[i-2];
107 for(i = 0; i < data_len; i++) {
108 /* == data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3] */
109 residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3];
113 for(i = 0; i < data_len; i++) {
114 /* == data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4] */
115 residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4];
123 void FLAC__fixed_restore_signal(const int32 residual[], unsigned data_len, unsigned order, int32 data[])
129 for(i = 0; i < data_len; i++) {
130 data[i] = residual[i];
134 for(i = 0; i < data_len; i++) {
135 data[i] = residual[i] + data[i-1];
139 for(i = 0; i < data_len; i++) {
140 /* == residual[i] + 2*data[i-1] - data[i-2] */
141 data[i] = residual[i] + (data[i-1]<<1) - data[i-2];
145 for(i = 0; i < data_len; i++) {
146 /* residual[i] + 3*data[i-1] - 3*data[i-2]) + data[i-3] */
147 data[i] = residual[i] + (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) + data[i-3];
151 for(i = 0; i < data_len; i++) {
152 /* == residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4] */
153 data[i] = residual[i] + ((data[i-1]+data[i-3])<<2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4];