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) ((unsigned)((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 uint32 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|) */
77 /* with VC++ you have to spoon feed it the casting */
78 residual_bits_per_sample[0] = (real)((data_len > 0) ? log(M_LN2 * (real)(int64)total_error_0 / (real) data_len) / M_LN2 : 0.0);
79 residual_bits_per_sample[1] = (real)((data_len > 0) ? log(M_LN2 * (real)(int64)total_error_1 / (real) data_len) / M_LN2 : 0.0);
80 residual_bits_per_sample[2] = (real)((data_len > 0) ? log(M_LN2 * (real)(int64)total_error_2 / (real) data_len) / M_LN2 : 0.0);
81 residual_bits_per_sample[3] = (real)((data_len > 0) ? log(M_LN2 * (real)(int64)total_error_3 / (real) data_len) / M_LN2 : 0.0);
82 residual_bits_per_sample[4] = (real)((data_len > 0) ? log(M_LN2 * (real)(int64)total_error_4 / (real) data_len) / M_LN2 : 0.0);
84 residual_bits_per_sample[0] = (real)((data_len > 0) ? log(M_LN2 * (real)total_error_0 / (real) data_len) / M_LN2 : 0.0);
85 residual_bits_per_sample[1] = (real)((data_len > 0) ? log(M_LN2 * (real)total_error_1 / (real) data_len) / M_LN2 : 0.0);
86 residual_bits_per_sample[2] = (real)((data_len > 0) ? log(M_LN2 * (real)total_error_2 / (real) data_len) / M_LN2 : 0.0);
87 residual_bits_per_sample[3] = (real)((data_len > 0) ? log(M_LN2 * (real)total_error_3 / (real) data_len) / M_LN2 : 0.0);
88 residual_bits_per_sample[4] = (real)((data_len > 0) ? log(M_LN2 * (real)total_error_4 / (real) data_len) / M_LN2 : 0.0);
94 unsigned FLAC__fixed_compute_best_predictor_slow(const int32 data[], unsigned data_len, real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
96 int32 last_error_0 = data[-1];
97 int32 last_error_1 = data[-1] - data[-2];
98 int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
99 int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
100 int32 error_0, error_1, error_2, error_3, error_4;
101 /* total_error_* are 64-bits to avoid overflow when encoding
102 * erratic signals when the bits-per-sample and blocksize are
105 uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
108 for(i = 0; i < data_len; i++) {
109 error_0 = data[i] ; total_error_0 += local_abs(error_0);
110 error_1 = error_0 - last_error_0; total_error_1 += local_abs(error_1);
111 error_2 = error_1 - last_error_1; total_error_2 += local_abs(error_2);
112 error_3 = error_2 - last_error_2; total_error_3 += local_abs(error_3);
113 error_4 = error_3 - last_error_3; total_error_4 += local_abs(error_4);
115 last_error_0 = error_0;
116 last_error_1 = error_1;
117 last_error_2 = error_2;
118 last_error_3 = error_3;
121 if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4))
123 else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4))
125 else if(total_error_2 < min(total_error_3, total_error_4))
127 else if(total_error_3 < total_error_4)
132 /* Estimate the expected number of bits per residual signal sample. */
133 /* 'total_error*' is linearly related to the variance of the residual */
134 /* signal, so we use it directly to compute E(|x|) */
136 /* with VC++ you have to spoon feed it the casting */
137 residual_bits_per_sample[0] = (real)((data_len > 0) ? log(M_LN2 * (real)(int64)total_error_0 / (real) data_len) / M_LN2 : 0.0);
138 residual_bits_per_sample[1] = (real)((data_len > 0) ? log(M_LN2 * (real)(int64)total_error_1 / (real) data_len) / M_LN2 : 0.0);
139 residual_bits_per_sample[2] = (real)((data_len > 0) ? log(M_LN2 * (real)(int64)total_error_2 / (real) data_len) / M_LN2 : 0.0);
140 residual_bits_per_sample[3] = (real)((data_len > 0) ? log(M_LN2 * (real)(int64)total_error_3 / (real) data_len) / M_LN2 : 0.0);
141 residual_bits_per_sample[4] = (real)((data_len > 0) ? log(M_LN2 * (real)(int64)total_error_4 / (real) data_len) / M_LN2 : 0.0);
143 residual_bits_per_sample[0] = (real)((data_len > 0) ? log(M_LN2 * (real)total_error_0 / (real) data_len) / M_LN2 : 0.0);
144 residual_bits_per_sample[1] = (real)((data_len > 0) ? log(M_LN2 * (real)total_error_1 / (real) data_len) / M_LN2 : 0.0);
145 residual_bits_per_sample[2] = (real)((data_len > 0) ? log(M_LN2 * (real)total_error_2 / (real) data_len) / M_LN2 : 0.0);
146 residual_bits_per_sample[3] = (real)((data_len > 0) ? log(M_LN2 * (real)total_error_3 / (real) data_len) / M_LN2 : 0.0);
147 residual_bits_per_sample[4] = (real)((data_len > 0) ? log(M_LN2 * (real)total_error_4 / (real) data_len) / M_LN2 : 0.0);
153 void FLAC__fixed_compute_residual(const int32 data[], unsigned data_len, unsigned order, int32 residual[])
159 for(i = 0; i < data_len; i++) {
160 residual[i] = data[i];
164 for(i = 0; i < data_len; i++) {
165 residual[i] = data[i] - data[i-1];
169 for(i = 0; i < data_len; i++) {
170 /* == data[i] - 2*data[i-1] + data[i-2] */
171 residual[i] = data[i] - (data[i-1] << 1) + data[i-2];
175 for(i = 0; i < data_len; i++) {
176 /* == data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3] */
177 residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3];
181 for(i = 0; i < data_len; i++) {
182 /* == data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4] */
183 residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4];
191 void FLAC__fixed_restore_signal(const int32 residual[], unsigned data_len, unsigned order, int32 data[])
197 for(i = 0; i < data_len; i++) {
198 data[i] = residual[i];
202 for(i = 0; i < data_len; i++) {
203 data[i] = residual[i] + data[i-1];
207 for(i = 0; i < data_len; i++) {
208 /* == residual[i] + 2*data[i-1] - data[i-2] */
209 data[i] = residual[i] + (data[i-1]<<1) - data[i-2];
213 for(i = 0; i < data_len; i++) {
214 /* residual[i] + 3*data[i-1] - 3*data[i-2]) + data[i-3] */
215 data[i] = residual[i] + (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) + data[i-3];
219 for(i = 0; i < data_len; i++) {
220 /* == residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4] */
221 data[i] = residual[i] + ((data[i-1]+data[i-3])<<2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4];