Josh Coalson | 26560dd | 2001-02-08 00:38:41 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Josh Coalson | 305ae2e | 2002-01-26 17:36:39 +0000 | [diff] [blame] | 2 | * Copyright (C) 2000,2001,2002 Josh Coalson |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 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 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 20 | #include <math.h> |
| 21 | #include "private/fixed.h" |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 22 | #include "FLAC/assert.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 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 |
Josh Coalson | fe9ba6f | 2001-02-28 23:44:27 +0000 | [diff] [blame] | 37 | #define local_abs(x) ((unsigned)((x)<0? -(x) : (x))) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 38 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 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]) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 40 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 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; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 47 | unsigned i, order; |
| 48 | |
| 49 | for(i = 0; i < data_len; i++) { |
Josh Coalson | d80c18e | 2001-05-18 18:47:55 +0000 | [diff] [blame] | 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; |
Josh Coalson | 7844424 | 2001-03-30 00:43:46 +0000 | [diff] [blame] | 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 | |
Josh Coalson | 427048f | 2002-08-27 05:44:57 +0000 | [diff] [blame] | 68 | /* Estimate the expected number of bits per residual signal sample. */ |
| 69 | /* 'total_error*' is linearly related to the variance of the residual */ |
| 70 | /* signal, so we use it directly to compute E(|x|) */ |
| 71 | FLAC__ASSERT(data_len > 0 || total_error_0 == 0); |
| 72 | FLAC__ASSERT(data_len > 0 || total_error_1 == 0); |
| 73 | FLAC__ASSERT(data_len > 0 || total_error_2 == 0); |
| 74 | FLAC__ASSERT(data_len > 0 || total_error_3 == 0); |
| 75 | FLAC__ASSERT(data_len > 0 || total_error_4 == 0); |
| 76 | residual_bits_per_sample[0] = (FLAC__real)((total_error_0 > 0) ? log(M_LN2 * (double)total_error_0 / (double)data_len) / M_LN2 : 0.0); |
| 77 | residual_bits_per_sample[1] = (FLAC__real)((total_error_1 > 0) ? log(M_LN2 * (double)total_error_1 / (double)data_len) / M_LN2 : 0.0); |
| 78 | residual_bits_per_sample[2] = (FLAC__real)((total_error_2 > 0) ? log(M_LN2 * (double)total_error_2 / (double)data_len) / M_LN2 : 0.0); |
| 79 | residual_bits_per_sample[3] = (FLAC__real)((total_error_3 > 0) ? log(M_LN2 * (double)total_error_3 / (double)data_len) / M_LN2 : 0.0); |
| 80 | residual_bits_per_sample[4] = (FLAC__real)((total_error_4 > 0) ? log(M_LN2 * (double)total_error_4 / (double)data_len) / M_LN2 : 0.0); |
Josh Coalson | 7844424 | 2001-03-30 00:43:46 +0000 | [diff] [blame] | 81 | |
| 82 | return order; |
| 83 | } |
| 84 | |
Josh Coalson | e10904a | 2001-07-12 21:27:57 +0000 | [diff] [blame] | 85 | 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]) |
Josh Coalson | 7844424 | 2001-03-30 00:43:46 +0000 | [diff] [blame] | 86 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 87 | FLAC__int32 last_error_0 = data[-1]; |
| 88 | FLAC__int32 last_error_1 = data[-1] - data[-2]; |
| 89 | FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]); |
| 90 | FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]); |
| 91 | FLAC__int32 error, save; |
Josh Coalson | 7844424 | 2001-03-30 00:43:46 +0000 | [diff] [blame] | 92 | /* total_error_* are 64-bits to avoid overflow when encoding |
| 93 | * erratic signals when the bits-per-sample and blocksize are |
| 94 | * large. |
| 95 | */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 96 | FLAC__uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0; |
Josh Coalson | 7844424 | 2001-03-30 00:43:46 +0000 | [diff] [blame] | 97 | unsigned i, order; |
| 98 | |
| 99 | for(i = 0; i < data_len; i++) { |
Josh Coalson | eee20a5 | 2001-05-18 18:49:19 +0000 | [diff] [blame] | 100 | error = data[i] ; total_error_0 += local_abs(error); save = error; |
| 101 | error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error; |
| 102 | error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error; |
| 103 | error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error; |
| 104 | error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4)) |
| 108 | order = 0; |
| 109 | else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4)) |
| 110 | order = 1; |
| 111 | else if(total_error_2 < min(total_error_3, total_error_4)) |
| 112 | order = 2; |
| 113 | else if(total_error_3 < total_error_4) |
| 114 | order = 3; |
| 115 | else |
| 116 | order = 4; |
| 117 | |
| 118 | /* Estimate the expected number of bits per residual signal sample. */ |
| 119 | /* 'total_error*' is linearly related to the variance of the residual */ |
| 120 | /* signal, so we use it directly to compute E(|x|) */ |
Josh Coalson | 427048f | 2002-08-27 05:44:57 +0000 | [diff] [blame] | 121 | FLAC__ASSERT(data_len > 0 || total_error_0 == 0); |
| 122 | FLAC__ASSERT(data_len > 0 || total_error_1 == 0); |
| 123 | FLAC__ASSERT(data_len > 0 || total_error_2 == 0); |
| 124 | FLAC__ASSERT(data_len > 0 || total_error_3 == 0); |
| 125 | FLAC__ASSERT(data_len > 0 || total_error_4 == 0); |
Josh Coalson | 40333b1 | 2001-11-13 21:37:04 +0000 | [diff] [blame] | 126 | #if defined _MSC_VER || defined __MINGW32__ |
Josh Coalson | 59f4a99 | 2001-04-01 05:55:01 +0000 | [diff] [blame] | 127 | /* with VC++ you have to spoon feed it the casting */ |
Josh Coalson | 427048f | 2002-08-27 05:44:57 +0000 | [diff] [blame] | 128 | residual_bits_per_sample[0] = (FLAC__real)((total_error_0 > 0) ? log(M_LN2 * (double)(FLAC__int64)total_error_0 / (double)data_len) / M_LN2 : 0.0); |
| 129 | residual_bits_per_sample[1] = (FLAC__real)((total_error_1 > 0) ? log(M_LN2 * (double)(FLAC__int64)total_error_1 / (double)data_len) / M_LN2 : 0.0); |
| 130 | residual_bits_per_sample[2] = (FLAC__real)((total_error_2 > 0) ? log(M_LN2 * (double)(FLAC__int64)total_error_2 / (double)data_len) / M_LN2 : 0.0); |
| 131 | residual_bits_per_sample[3] = (FLAC__real)((total_error_3 > 0) ? log(M_LN2 * (double)(FLAC__int64)total_error_3 / (double)data_len) / M_LN2 : 0.0); |
| 132 | residual_bits_per_sample[4] = (FLAC__real)((total_error_4 > 0) ? log(M_LN2 * (double)(FLAC__int64)total_error_4 / (double)data_len) / M_LN2 : 0.0); |
Josh Coalson | 59f4a99 | 2001-04-01 05:55:01 +0000 | [diff] [blame] | 133 | #else |
Josh Coalson | 427048f | 2002-08-27 05:44:57 +0000 | [diff] [blame] | 134 | residual_bits_per_sample[0] = (FLAC__real)((total_error_0 > 0) ? log(M_LN2 * (double)total_error_0 / (double)data_len) / M_LN2 : 0.0); |
| 135 | residual_bits_per_sample[1] = (FLAC__real)((total_error_1 > 0) ? log(M_LN2 * (double)total_error_1 / (double)data_len) / M_LN2 : 0.0); |
| 136 | residual_bits_per_sample[2] = (FLAC__real)((total_error_2 > 0) ? log(M_LN2 * (double)total_error_2 / (double)data_len) / M_LN2 : 0.0); |
| 137 | residual_bits_per_sample[3] = (FLAC__real)((total_error_3 > 0) ? log(M_LN2 * (double)total_error_3 / (double)data_len) / M_LN2 : 0.0); |
| 138 | residual_bits_per_sample[4] = (FLAC__real)((total_error_4 > 0) ? log(M_LN2 * (double)total_error_4 / (double)data_len) / M_LN2 : 0.0); |
Josh Coalson | 59f4a99 | 2001-04-01 05:55:01 +0000 | [diff] [blame] | 139 | #endif |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 140 | |
| 141 | return order; |
| 142 | } |
| 143 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 144 | void FLAC__fixed_compute_residual(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[]) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 145 | { |
Josh Coalson | 64df715 | 2002-10-11 06:24:12 +0000 | [diff] [blame] | 146 | const int idata_len = (int)data_len; |
| 147 | int i; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 148 | |
| 149 | switch(order) { |
| 150 | case 0: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 151 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 152 | residual[i] = data[i]; |
| 153 | } |
| 154 | break; |
| 155 | case 1: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 156 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 157 | residual[i] = data[i] - data[i-1]; |
| 158 | } |
| 159 | break; |
| 160 | case 2: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 161 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 162 | /* == data[i] - 2*data[i-1] + data[i-2] */ |
| 163 | residual[i] = data[i] - (data[i-1] << 1) + data[i-2]; |
| 164 | } |
| 165 | break; |
| 166 | case 3: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 167 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 168 | /* == data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3] */ |
| 169 | residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3]; |
| 170 | } |
| 171 | break; |
| 172 | case 4: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 173 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 174 | /* == data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4] */ |
| 175 | residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4]; |
| 176 | } |
| 177 | break; |
| 178 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 179 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 180 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 183 | void FLAC__fixed_restore_signal(const FLAC__int32 residual[], unsigned data_len, unsigned order, FLAC__int32 data[]) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 184 | { |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 185 | int i, idata_len = (int)data_len; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 186 | |
| 187 | switch(order) { |
| 188 | case 0: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 189 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 190 | data[i] = residual[i]; |
| 191 | } |
| 192 | break; |
| 193 | case 1: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 194 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 195 | data[i] = residual[i] + data[i-1]; |
| 196 | } |
| 197 | break; |
| 198 | case 2: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 199 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 200 | /* == residual[i] + 2*data[i-1] - data[i-2] */ |
| 201 | data[i] = residual[i] + (data[i-1]<<1) - data[i-2]; |
| 202 | } |
| 203 | break; |
| 204 | case 3: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 205 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 206 | /* residual[i] + 3*data[i-1] - 3*data[i-2]) + data[i-3] */ |
| 207 | data[i] = residual[i] + (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) + data[i-3]; |
| 208 | } |
| 209 | break; |
| 210 | case 4: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 211 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 212 | /* == residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4] */ |
| 213 | data[i] = residual[i] + ((data[i-1]+data[i-3])<<2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4]; |
| 214 | } |
| 215 | break; |
| 216 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 217 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 218 | } |
| 219 | } |