rework so that rice parameters and raw_bits from the entropy coding method struct...
[platform/upstream/flac.git] / src / libFLAC / fixed.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <math.h>
21 #include "private/fixed.h"
22 #include "FLAC/assert.h"
23
24 #ifndef M_LN2
25 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
26 #define M_LN2 0.69314718055994530942
27 #endif
28
29 #ifdef min
30 #undef min
31 #endif
32 #define min(x,y) ((x) < (y)? (x) : (y))
33
34 #ifdef local_abs
35 #undef local_abs
36 #endif
37 #define local_abs(x) ((unsigned)((x)<0? -(x) : (x)))
38
39 unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
40 {
41         FLAC__int32 last_error_0 = data[-1];
42         FLAC__int32 last_error_1 = data[-1] - data[-2];
43         FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
44         FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
45         FLAC__int32 error, save;
46         FLAC__uint32 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
47         unsigned i, order;
48
49         for(i = 0; i < data_len; i++) {
50                 error  = data[i]     ; total_error_0 += local_abs(error);                      save = error;
51                 error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error;
52                 error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error;
53                 error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error;
54                 error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
55         }
56
57         if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4))
58                 order = 0;
59         else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4))
60                 order = 1;
61         else if(total_error_2 < min(total_error_3, total_error_4))
62                 order = 2;
63         else if(total_error_3 < total_error_4)
64                 order = 3;
65         else
66                 order = 4;
67
68         residual_bits_per_sample[0] = (FLAC__real)((data_len > 0 && total_error_0 > 0) ? log(M_LN2 * (double)total_error_0 / (double) data_len) / M_LN2 : 0.0);
69         residual_bits_per_sample[1] = (FLAC__real)((data_len > 0 && total_error_1 > 0) ? log(M_LN2 * (double)total_error_1 / (double) data_len) / M_LN2 : 0.0);
70         residual_bits_per_sample[2] = (FLAC__real)((data_len > 0 && total_error_2 > 0) ? log(M_LN2 * (double)total_error_2 / (double) data_len) / M_LN2 : 0.0);
71         residual_bits_per_sample[3] = (FLAC__real)((data_len > 0 && total_error_3 > 0) ? log(M_LN2 * (double)total_error_3 / (double) data_len) / M_LN2 : 0.0);
72         residual_bits_per_sample[4] = (FLAC__real)((data_len > 0 && total_error_4 > 0) ? log(M_LN2 * (double)total_error_4 / (double) data_len) / M_LN2 : 0.0);
73
74         return order;
75 }
76
77 unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__real residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
78 {
79         FLAC__int32 last_error_0 = data[-1];
80         FLAC__int32 last_error_1 = data[-1] - data[-2];
81         FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
82         FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
83         FLAC__int32 error, save;
84         /* total_error_* are 64-bits to avoid overflow when encoding
85          * erratic signals when the bits-per-sample and blocksize are
86          * large.
87          */
88         FLAC__uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
89         unsigned i, order;
90
91         for(i = 0; i < data_len; i++) {
92                 error  = data[i]     ; total_error_0 += local_abs(error);                      save = error;
93                 error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error;
94                 error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error;
95                 error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error;
96                 error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
97         }
98
99         if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4))
100                 order = 0;
101         else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4))
102                 order = 1;
103         else if(total_error_2 < min(total_error_3, total_error_4))
104                 order = 2;
105         else if(total_error_3 < total_error_4)
106                 order = 3;
107         else
108                 order = 4;
109
110         /* Estimate the expected number of bits per residual signal sample. */
111         /* 'total_error*' is linearly related to the variance of the residual */
112         /* signal, so we use it directly to compute E(|x|) */
113 #if defined _MSC_VER || defined __MINGW32__
114         /* with VC++ you have to spoon feed it the casting */
115         residual_bits_per_sample[0] = (FLAC__real)((data_len > 0 && total_error_0 > 0) ? log(M_LN2 * (double)(FLAC__int64)total_error_0 / (double) data_len) / M_LN2 : 0.0);
116         residual_bits_per_sample[1] = (FLAC__real)((data_len > 0 && total_error_1 > 0) ? log(M_LN2 * (double)(FLAC__int64)total_error_1 / (double) data_len) / M_LN2 : 0.0);
117         residual_bits_per_sample[2] = (FLAC__real)((data_len > 0 && total_error_2 > 0) ? log(M_LN2 * (double)(FLAC__int64)total_error_2 / (double) data_len) / M_LN2 : 0.0);
118         residual_bits_per_sample[3] = (FLAC__real)((data_len > 0 && total_error_3 > 0) ? log(M_LN2 * (double)(FLAC__int64)total_error_3 / (double) data_len) / M_LN2 : 0.0);
119         residual_bits_per_sample[4] = (FLAC__real)((data_len > 0 && total_error_4 > 0) ? log(M_LN2 * (double)(FLAC__int64)total_error_4 / (double) data_len) / M_LN2 : 0.0);
120 #else
121         residual_bits_per_sample[0] = (FLAC__real)((data_len > 0 && total_error_0 > 0) ? log(M_LN2 * (double)total_error_0 / (double) data_len) / M_LN2 : 0.0);
122         residual_bits_per_sample[1] = (FLAC__real)((data_len > 0 && total_error_1 > 0) ? log(M_LN2 * (double)total_error_1 / (double) data_len) / M_LN2 : 0.0);
123         residual_bits_per_sample[2] = (FLAC__real)((data_len > 0 && total_error_2 > 0) ? log(M_LN2 * (double)total_error_2 / (double) data_len) / M_LN2 : 0.0);
124         residual_bits_per_sample[3] = (FLAC__real)((data_len > 0 && total_error_3 > 0) ? log(M_LN2 * (double)total_error_3 / (double) data_len) / M_LN2 : 0.0);
125         residual_bits_per_sample[4] = (FLAC__real)((data_len > 0 && total_error_4 > 0) ? log(M_LN2 * (double)total_error_4 / (double) data_len) / M_LN2 : 0.0);
126 #endif
127
128         return order;
129 }
130
131 void FLAC__fixed_compute_residual(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[])
132 {
133         int i, idata_len = (int)data_len;
134
135         switch(order) {
136                 case 0:
137                         for(i = 0; i < idata_len; i++) {
138                                 residual[i] = data[i];
139                         }
140                         break;
141                 case 1:
142                         for(i = 0; i < idata_len; i++) {
143                                 residual[i] = data[i] - data[i-1];
144                         }
145                         break;
146                 case 2:
147                         for(i = 0; i < idata_len; i++) {
148                                 /* == data[i] - 2*data[i-1] + data[i-2] */
149                                 residual[i] = data[i] - (data[i-1] << 1) + data[i-2];
150                         }
151                         break;
152                 case 3:
153                         for(i = 0; i < idata_len; i++) {
154                                 /* == data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3] */
155                                 residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3];
156                         }
157                         break;
158                 case 4:
159                         for(i = 0; i < idata_len; i++) {
160                                 /* == data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4] */
161                                 residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4];
162                         }
163                         break;
164                 default:
165                         FLAC__ASSERT(0);
166         }
167 }
168
169 void FLAC__fixed_restore_signal(const FLAC__int32 residual[], unsigned data_len, unsigned order, FLAC__int32 data[])
170 {
171         int i, idata_len = (int)data_len;
172
173         switch(order) {
174                 case 0:
175                         for(i = 0; i < idata_len; i++) {
176                                 data[i] = residual[i];
177                         }
178                         break;
179                 case 1:
180                         for(i = 0; i < idata_len; i++) {
181                                 data[i] = residual[i] + data[i-1];
182                         }
183                         break;
184                 case 2:
185                         for(i = 0; i < idata_len; i++) {
186                                 /* == residual[i] + 2*data[i-1] - data[i-2] */
187                                 data[i] = residual[i] + (data[i-1]<<1) - data[i-2];
188                         }
189                         break;
190                 case 3:
191                         for(i = 0; i < idata_len; i++) {
192                                 /* residual[i] + 3*data[i-1] - 3*data[i-2]) + data[i-3] */
193                                 data[i] = residual[i] + (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) + data[i-3];
194                         }
195                         break;
196                 case 4:
197                         for(i = 0; i < idata_len; i++) {
198                                 /* == residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4] */
199                                 data[i] = residual[i] + ((data[i-1]+data[i-3])<<2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4];
200                         }
201                         break;
202                 default:
203                         FLAC__ASSERT(0);
204         }
205 }