Josh Coalson | 26560dd | 2001-02-08 00:38:41 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Josh Coalson | afae69f | 2003-01-02 07:03:16 +0000 | [diff] [blame] | 2 | * Copyright (C) 2000,2001,2002,2003 Josh Coalson |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame^] | 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 7 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame^] | 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 10 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame^] | 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 32 | #include <math.h> |
| 33 | #include "private/fixed.h" |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 34 | #include "FLAC/assert.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 35 | |
| 36 | #ifndef M_LN2 |
| 37 | /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */ |
| 38 | #define M_LN2 0.69314718055994530942 |
| 39 | #endif |
| 40 | |
| 41 | #ifdef min |
| 42 | #undef min |
| 43 | #endif |
| 44 | #define min(x,y) ((x) < (y)? (x) : (y)) |
| 45 | |
| 46 | #ifdef local_abs |
| 47 | #undef local_abs |
| 48 | #endif |
Josh Coalson | fe9ba6f | 2001-02-28 23:44:27 +0000 | [diff] [blame] | 49 | #define local_abs(x) ((unsigned)((x)<0? -(x) : (x))) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 50 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 51 | 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] | 52 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 53 | FLAC__int32 last_error_0 = data[-1]; |
| 54 | FLAC__int32 last_error_1 = data[-1] - data[-2]; |
| 55 | FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]); |
| 56 | FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]); |
| 57 | FLAC__int32 error, save; |
| 58 | 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] | 59 | unsigned i, order; |
| 60 | |
| 61 | for(i = 0; i < data_len; i++) { |
Josh Coalson | d80c18e | 2001-05-18 18:47:55 +0000 | [diff] [blame] | 62 | error = data[i] ; total_error_0 += local_abs(error); save = error; |
| 63 | error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error; |
| 64 | error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error; |
| 65 | error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error; |
| 66 | 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] | 67 | } |
| 68 | |
| 69 | if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4)) |
| 70 | order = 0; |
| 71 | else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4)) |
| 72 | order = 1; |
| 73 | else if(total_error_2 < min(total_error_3, total_error_4)) |
| 74 | order = 2; |
| 75 | else if(total_error_3 < total_error_4) |
| 76 | order = 3; |
| 77 | else |
| 78 | order = 4; |
| 79 | |
Josh Coalson | 427048f | 2002-08-27 05:44:57 +0000 | [diff] [blame] | 80 | /* Estimate the expected number of bits per residual signal sample. */ |
| 81 | /* 'total_error*' is linearly related to the variance of the residual */ |
| 82 | /* signal, so we use it directly to compute E(|x|) */ |
| 83 | FLAC__ASSERT(data_len > 0 || total_error_0 == 0); |
| 84 | FLAC__ASSERT(data_len > 0 || total_error_1 == 0); |
| 85 | FLAC__ASSERT(data_len > 0 || total_error_2 == 0); |
| 86 | FLAC__ASSERT(data_len > 0 || total_error_3 == 0); |
| 87 | FLAC__ASSERT(data_len > 0 || total_error_4 == 0); |
| 88 | 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); |
| 89 | 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); |
| 90 | 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); |
| 91 | 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); |
| 92 | 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] | 93 | |
| 94 | return order; |
| 95 | } |
| 96 | |
Josh Coalson | e10904a | 2001-07-12 21:27:57 +0000 | [diff] [blame] | 97 | 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] | 98 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 99 | FLAC__int32 last_error_0 = data[-1]; |
| 100 | FLAC__int32 last_error_1 = data[-1] - data[-2]; |
| 101 | FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]); |
| 102 | FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]); |
| 103 | FLAC__int32 error, save; |
Josh Coalson | 7844424 | 2001-03-30 00:43:46 +0000 | [diff] [blame] | 104 | /* total_error_* are 64-bits to avoid overflow when encoding |
| 105 | * erratic signals when the bits-per-sample and blocksize are |
| 106 | * large. |
| 107 | */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 108 | 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] | 109 | unsigned i, order; |
| 110 | |
| 111 | for(i = 0; i < data_len; i++) { |
Josh Coalson | eee20a5 | 2001-05-18 18:49:19 +0000 | [diff] [blame] | 112 | error = data[i] ; total_error_0 += local_abs(error); save = error; |
| 113 | error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error; |
| 114 | error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error; |
| 115 | error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error; |
| 116 | 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] | 117 | } |
| 118 | |
| 119 | if(total_error_0 < min(min(min(total_error_1, total_error_2), total_error_3), total_error_4)) |
| 120 | order = 0; |
| 121 | else if(total_error_1 < min(min(total_error_2, total_error_3), total_error_4)) |
| 122 | order = 1; |
| 123 | else if(total_error_2 < min(total_error_3, total_error_4)) |
| 124 | order = 2; |
| 125 | else if(total_error_3 < total_error_4) |
| 126 | order = 3; |
| 127 | else |
| 128 | order = 4; |
| 129 | |
| 130 | /* Estimate the expected number of bits per residual signal sample. */ |
| 131 | /* 'total_error*' is linearly related to the variance of the residual */ |
| 132 | /* signal, so we use it directly to compute E(|x|) */ |
Josh Coalson | 427048f | 2002-08-27 05:44:57 +0000 | [diff] [blame] | 133 | FLAC__ASSERT(data_len > 0 || total_error_0 == 0); |
| 134 | FLAC__ASSERT(data_len > 0 || total_error_1 == 0); |
| 135 | FLAC__ASSERT(data_len > 0 || total_error_2 == 0); |
| 136 | FLAC__ASSERT(data_len > 0 || total_error_3 == 0); |
| 137 | FLAC__ASSERT(data_len > 0 || total_error_4 == 0); |
Josh Coalson | 40333b1 | 2001-11-13 21:37:04 +0000 | [diff] [blame] | 138 | #if defined _MSC_VER || defined __MINGW32__ |
Josh Coalson | 59f4a99 | 2001-04-01 05:55:01 +0000 | [diff] [blame] | 139 | /* with VC++ you have to spoon feed it the casting */ |
Josh Coalson | 427048f | 2002-08-27 05:44:57 +0000 | [diff] [blame] | 140 | 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); |
| 141 | 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); |
| 142 | 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); |
| 143 | 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); |
| 144 | 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] | 145 | #else |
Josh Coalson | 427048f | 2002-08-27 05:44:57 +0000 | [diff] [blame] | 146 | 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); |
| 147 | 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); |
| 148 | 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); |
| 149 | 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); |
| 150 | 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] | 151 | #endif |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 152 | |
| 153 | return order; |
| 154 | } |
| 155 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 156 | 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] | 157 | { |
Josh Coalson | 64df715 | 2002-10-11 06:24:12 +0000 | [diff] [blame] | 158 | const int idata_len = (int)data_len; |
| 159 | int i; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 160 | |
| 161 | switch(order) { |
| 162 | case 0: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 163 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 164 | residual[i] = data[i]; |
| 165 | } |
| 166 | break; |
| 167 | case 1: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 168 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 169 | residual[i] = data[i] - data[i-1]; |
| 170 | } |
| 171 | break; |
| 172 | case 2: |
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] - 2*data[i-1] + data[i-2] */ |
| 175 | residual[i] = data[i] - (data[i-1] << 1) + data[i-2]; |
| 176 | } |
| 177 | break; |
| 178 | case 3: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 179 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 180 | /* == data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3] */ |
| 181 | residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3]; |
| 182 | } |
| 183 | break; |
| 184 | case 4: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 185 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 186 | /* == data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4] */ |
| 187 | residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4]; |
| 188 | } |
| 189 | break; |
| 190 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 191 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 192 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 195 | 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] | 196 | { |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 197 | int i, idata_len = (int)data_len; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 198 | |
| 199 | switch(order) { |
| 200 | case 0: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 201 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 202 | data[i] = residual[i]; |
| 203 | } |
| 204 | break; |
| 205 | case 1: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 206 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 207 | data[i] = residual[i] + data[i-1]; |
| 208 | } |
| 209 | break; |
| 210 | case 2: |
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] + 2*data[i-1] - data[i-2] */ |
| 213 | data[i] = residual[i] + (data[i-1]<<1) - data[i-2]; |
| 214 | } |
| 215 | break; |
| 216 | case 3: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 217 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 218 | /* residual[i] + 3*data[i-1] - 3*data[i-2]) + data[i-3] */ |
| 219 | data[i] = residual[i] + (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) + data[i-3]; |
| 220 | } |
| 221 | break; |
| 222 | case 4: |
Josh Coalson | a16d8ad | 2001-05-21 23:43:35 +0000 | [diff] [blame] | 223 | for(i = 0; i < idata_len; i++) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 224 | /* == residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4] */ |
| 225 | data[i] = residual[i] + ((data[i-1]+data[i-3])<<2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4]; |
| 226 | } |
| 227 | break; |
| 228 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 229 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 230 | } |
| 231 | } |