Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ALAC (Apple Lossless Audio Codec) decoder |
| 3 | * Copyright (c) 2005 David Hammerton |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This is the actual decoder. |
| 7 | * |
| 8 | * http://crazney.net/programs/itunes/alac.html |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person |
| 11 | * obtaining a copy of this software and associated documentation |
| 12 | * files (the "Software"), to deal in the Software without |
| 13 | * restriction, including without limitation the rights to use, |
| 14 | * copy, modify, merge, publish, distribute, sublicense, and/or |
| 15 | * sell copies of the Software, and to permit persons to whom the |
| 16 | * Software is furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be |
| 19 | * included in all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 28 | * OTHER DEALINGS IN THE SOFTWARE. |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
Dave Chapman | debbe97 | 2005-09-22 20:46:58 +0000 | [diff] [blame] | 36 | #include <inttypes.h> |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 37 | |
Björn Stenberg | c6b3d38 | 2008-11-20 11:27:31 +0000 | [diff] [blame] | 38 | #include "codeclib.h" |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 39 | #include "decomp.h" |
| 40 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 41 | #define SIGNEXTEND24(val) (((signed)val<<8)>>8) |
| 42 | |
Nils Wallménius | 7c6056b | 2011-06-01 10:28:26 +0000 | [diff] [blame] | 43 | static int16_t predictor_coef_table[32] IBSS_ATTR; |
| 44 | static int16_t predictor_coef_table_a[32] IBSS_ATTR; |
| 45 | static int16_t predictor_coef_table_b[32] IBSS_ATTR; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 46 | |
Dave Chapman | aaacb70 | 2007-12-01 01:01:35 +0000 | [diff] [blame] | 47 | |
| 48 | /* Endian/aligment safe functions - only used in alac_set_info() */ |
| 49 | static uint32_t get_uint32be(unsigned char* p) |
| 50 | { |
| 51 | return((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]); |
| 52 | } |
| 53 | |
| 54 | static uint16_t get_uint16be(unsigned char* p) |
| 55 | { |
| 56 | return((p[0]<<8) | p[1]); |
| 57 | } |
| 58 | |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 59 | void alac_set_info(alac_file *alac, char *inputbuffer) |
| 60 | { |
Dave Chapman | aaacb70 | 2007-12-01 01:01:35 +0000 | [diff] [blame] | 61 | unsigned char* ptr = (unsigned char*)inputbuffer; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 62 | ptr += 4; /* size */ |
| 63 | ptr += 4; /* frma */ |
| 64 | ptr += 4; /* alac */ |
| 65 | ptr += 4; /* size */ |
| 66 | ptr += 4; /* alac */ |
| 67 | |
| 68 | ptr += 4; /* 0 ? */ |
| 69 | |
Dave Chapman | aaacb70 | 2007-12-01 01:01:35 +0000 | [diff] [blame] | 70 | alac->setinfo_max_samples_per_frame = get_uint32be(ptr); /* buffer size / 2 ? */ |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 71 | ptr += 4; |
Dave Chapman | aaacb70 | 2007-12-01 01:01:35 +0000 | [diff] [blame] | 72 | alac->setinfo_7a = *ptr++; |
| 73 | alac->setinfo_sample_size = *ptr++; |
| 74 | alac->setinfo_rice_historymult = *ptr++; |
| 75 | alac->setinfo_rice_initialhistory = *ptr++; |
| 76 | alac->setinfo_rice_kmodifier = *ptr++; |
| 77 | alac->setinfo_7f = *ptr++; |
aozima | 975e309 | 2016-08-09 20:17:59 +0800 | [diff] [blame^] | 78 | |
Dave Chapman | aaacb70 | 2007-12-01 01:01:35 +0000 | [diff] [blame] | 79 | alac->setinfo_80 = get_uint16be(ptr); |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 80 | ptr += 2; |
Dave Chapman | aaacb70 | 2007-12-01 01:01:35 +0000 | [diff] [blame] | 81 | alac->setinfo_82 = get_uint32be(ptr); |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 82 | ptr += 4; |
Dave Chapman | aaacb70 | 2007-12-01 01:01:35 +0000 | [diff] [blame] | 83 | alac->setinfo_86 = get_uint32be(ptr); |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 84 | ptr += 4; |
Dave Chapman | aaacb70 | 2007-12-01 01:01:35 +0000 | [diff] [blame] | 85 | alac->setinfo_8a_rate = get_uint32be(ptr); |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 86 | ptr += 4; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* stream reading */ |
| 90 | |
| 91 | /* supports reading 1 to 16 bits, in big endian format */ |
Dave Chapman | 439ba9b | 2005-11-03 18:14:37 +0000 | [diff] [blame] | 92 | static inline uint32_t readbits_16(alac_file *alac, int bits) |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 93 | { |
| 94 | uint32_t result; |
| 95 | int new_accumulator; |
| 96 | |
| 97 | result = (alac->input_buffer[0] << 16) | |
| 98 | (alac->input_buffer[1] << 8) | |
| 99 | (alac->input_buffer[2]); |
| 100 | |
| 101 | /* shift left by the number of bits we've already read, |
| 102 | * so that the top 'n' bits of the 24 bits we read will |
| 103 | * be the return bits */ |
| 104 | result = result << alac->input_buffer_bitaccumulator; |
| 105 | |
| 106 | result = result & 0x00ffffff; |
| 107 | |
| 108 | /* and then only want the top 'n' bits from that, where |
| 109 | * n is 'bits' */ |
| 110 | result = result >> (24 - bits); |
| 111 | |
| 112 | new_accumulator = (alac->input_buffer_bitaccumulator + bits); |
| 113 | |
| 114 | /* increase the buffer pointer if we've read over n bytes. */ |
| 115 | alac->input_buffer += (new_accumulator >> 3); |
| 116 | |
| 117 | /* and the remainder goes back into the bit accumulator */ |
| 118 | alac->input_buffer_bitaccumulator = (new_accumulator & 7); |
| 119 | |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | /* supports reading 1 to 32 bits, in big endian format */ |
Dave Chapman | 439ba9b | 2005-11-03 18:14:37 +0000 | [diff] [blame] | 124 | static inline uint32_t readbits(alac_file *alac, int bits) |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 125 | { |
| 126 | int32_t result = 0; |
| 127 | |
| 128 | if (bits > 16) |
| 129 | { |
| 130 | bits -= 16; |
| 131 | result = readbits_16(alac, 16) << bits; |
| 132 | } |
| 133 | |
| 134 | result |= readbits_16(alac, bits); |
| 135 | |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | /* reads a single bit */ |
Dave Chapman | 439ba9b | 2005-11-03 18:14:37 +0000 | [diff] [blame] | 140 | static inline int readbit(alac_file *alac) |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 141 | { |
| 142 | int result; |
| 143 | int new_accumulator; |
| 144 | |
| 145 | result = alac->input_buffer[0]; |
| 146 | |
| 147 | result = result << alac->input_buffer_bitaccumulator; |
| 148 | |
| 149 | result = result >> 7 & 1; |
| 150 | |
| 151 | new_accumulator = (alac->input_buffer_bitaccumulator + 1); |
| 152 | |
| 153 | alac->input_buffer += (new_accumulator / 8); |
| 154 | |
| 155 | alac->input_buffer_bitaccumulator = (new_accumulator % 8); |
| 156 | |
| 157 | return result; |
| 158 | } |
| 159 | |
Dave Chapman | 439ba9b | 2005-11-03 18:14:37 +0000 | [diff] [blame] | 160 | static inline void unreadbits(alac_file *alac, int bits) |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 161 | { |
| 162 | int new_accumulator = (alac->input_buffer_bitaccumulator - bits); |
| 163 | |
| 164 | alac->input_buffer += (new_accumulator >> 3); |
| 165 | |
| 166 | alac->input_buffer_bitaccumulator = (new_accumulator & 7); |
| 167 | if (alac->input_buffer_bitaccumulator < 0) |
| 168 | alac->input_buffer_bitaccumulator *= -1; |
| 169 | } |
| 170 | |
Andrew Mahone | 85aad9b | 2009-12-09 02:24:45 +0000 | [diff] [blame] | 171 | #define count_leading_zeros(x) bs_generic(x, BS_CLZ|BS_SHORT) |
Dave Chapman | 1a03c37 | 2006-02-05 09:12:57 +0000 | [diff] [blame] | 172 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 173 | #define RICE_THRESHOLD 8 // maximum number of bits for a rice prefix. |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 174 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 175 | static inline int32_t entropy_decode_value(alac_file* alac, |
| 176 | int readsamplesize, |
| 177 | int k) ICODE_ATTR_ALAC; |
| 178 | static inline int32_t entropy_decode_value(alac_file* alac, |
| 179 | int readsamplesize, |
| 180 | int k) |
| 181 | { |
| 182 | int32_t x = 0; // decoded value |
| 183 | |
| 184 | // read x, number of 1s before 0 represent the rice value. |
| 185 | while (x <= RICE_THRESHOLD && readbit(alac)) |
| 186 | { |
| 187 | x++; |
| 188 | } |
| 189 | |
| 190 | if (x > RICE_THRESHOLD) |
| 191 | { |
| 192 | // read the number from the bit stream (raw value) |
| 193 | int32_t value; |
| 194 | |
| 195 | value = readbits(alac, readsamplesize); |
| 196 | |
| 197 | /* mask value to readsamplesize size */ |
| 198 | if (readsamplesize != 32) |
| 199 | value &= (((uint32_t)0xffffffff) >> (32 - readsamplesize)); |
| 200 | |
| 201 | x = value; |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | if (k != 1) |
| 206 | { |
| 207 | int extrabits = readbits(alac, k); |
| 208 | |
| 209 | // x = x * (2^k - 1) |
| 210 | x = (x << k) - x; |
| 211 | |
| 212 | if (extrabits > 1) |
| 213 | x += extrabits - 1; |
| 214 | else |
| 215 | unreadbits(alac, 1); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return x; |
| 220 | } |
| 221 | |
| 222 | static void entropy_rice_decode(alac_file* alac, |
| 223 | int32_t* output_buffer, |
| 224 | int output_size, |
| 225 | int readsamplesize, |
| 226 | int rice_initialhistory, |
| 227 | int rice_kmodifier, |
| 228 | int rice_historymult, |
| 229 | int rice_kmodifier_mask) ICODE_ATTR_ALAC; |
| 230 | static void entropy_rice_decode(alac_file* alac, |
| 231 | int32_t* output_buffer, |
| 232 | int output_size, |
| 233 | int readsamplesize, |
| 234 | int rice_initialhistory, |
| 235 | int rice_kmodifier, |
| 236 | int rice_historymult, |
| 237 | int rice_kmodifier_mask) |
| 238 | { |
| 239 | int output_count; |
| 240 | int history = rice_initialhistory; |
| 241 | int sign_modifier = 0; |
| 242 | |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 243 | for (output_count = 0; output_count < output_size; output_count++) |
| 244 | { |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 245 | int32_t decoded_value; |
| 246 | int32_t final_value; |
| 247 | int32_t k; |
| 248 | |
| 249 | k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3); |
| 250 | |
| 251 | if (k < 0) k += rice_kmodifier; |
| 252 | else k = rice_kmodifier; |
| 253 | |
| 254 | decoded_value = entropy_decode_value(alac, readsamplesize, k); |
| 255 | |
| 256 | decoded_value += sign_modifier; |
| 257 | final_value = (decoded_value + 1) / 2; // inc by 1 and shift out sign bit |
| 258 | if (decoded_value & 1) // the sign is stored in the low bit |
| 259 | final_value *= -1; |
| 260 | |
| 261 | output_buffer[output_count] = final_value; |
| 262 | |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 263 | sign_modifier = 0; |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 264 | |
| 265 | // update history |
| 266 | history += (decoded_value * rice_historymult) |
| 267 | - ((history * rice_historymult) >> 9); |
| 268 | |
| 269 | if (decoded_value > 0xFFFF) |
| 270 | history = 0xFFFF; |
| 271 | |
| 272 | // special case, for compressed blocks of 0 |
| 273 | if ((history < 128) && (output_count + 1 < output_size)) |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 274 | { |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 275 | int32_t block_size; |
| 276 | |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 277 | sign_modifier = 1; |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 278 | |
| 279 | k = count_leading_zeros(history) + ((history + 16) / 64) - 24; |
| 280 | |
| 281 | // note: block_size is always 16bit |
| 282 | block_size = entropy_decode_value(alac, 16, k) & rice_kmodifier_mask; |
| 283 | |
| 284 | // got block_size 0s |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 285 | if (block_size > 0) |
| 286 | { |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 287 | memset(&output_buffer[output_count + 1], 0, |
| 288 | block_size * sizeof(*output_buffer)); |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 289 | output_count += block_size; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 290 | } |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 291 | |
| 292 | if (block_size > 0xFFFF) |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 293 | sign_modifier = 0; |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 294 | |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 295 | history = 0; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits)) |
| 301 | |
| 302 | #define SIGN_ONLY(v) \ |
| 303 | ((v < 0) ? (-1) : \ |
| 304 | ((v > 0) ? (1) : \ |
| 305 | (0))) |
| 306 | |
| 307 | static void predictor_decompress_fir_adapt(int32_t *error_buffer, |
| 308 | int32_t *buffer_out, |
| 309 | int output_size, |
| 310 | int readsamplesize, |
| 311 | int16_t *predictor_coef_table, |
| 312 | int predictor_coef_num, |
Tomasz Malesinski | 5c54ba4 | 2006-11-09 21:59:27 +0000 | [diff] [blame] | 313 | int predictor_quantitization) ICODE_ATTR_ALAC; |
Dave Chapman | 439ba9b | 2005-11-03 18:14:37 +0000 | [diff] [blame] | 314 | static void predictor_decompress_fir_adapt(int32_t *error_buffer, |
| 315 | int32_t *buffer_out, |
| 316 | int output_size, |
| 317 | int readsamplesize, |
| 318 | int16_t *predictor_coef_table, |
| 319 | int predictor_coef_num, |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 320 | int predictor_quantitization) |
| 321 | { |
| 322 | int i; |
| 323 | |
| 324 | /* first sample always copies */ |
| 325 | *buffer_out = *error_buffer; |
| 326 | |
| 327 | if (!predictor_coef_num) |
| 328 | { |
| 329 | if (output_size <= 1) return; |
| 330 | memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | if (predictor_coef_num == 0x1f) /* 11111 - max value of predictor_coef_num */ |
| 335 | { /* second-best case scenario for fir decompression, |
| 336 | * error describes a small difference from the previous sample only |
| 337 | */ |
| 338 | if (output_size <= 1) return; |
| 339 | for (i = 0; i < output_size - 1; i++) |
| 340 | { |
| 341 | int32_t prev_value; |
| 342 | int32_t error_value; |
| 343 | |
| 344 | prev_value = buffer_out[i]; |
| 345 | error_value = error_buffer[i+1]; |
| 346 | buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize); |
| 347 | } |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | /* read warm-up samples */ |
| 352 | if (predictor_coef_num > 0) |
| 353 | { |
| 354 | int i; |
| 355 | for (i = 0; i < predictor_coef_num; i++) |
| 356 | { |
| 357 | int32_t val; |
| 358 | |
| 359 | val = buffer_out[i] + error_buffer[i+1]; |
| 360 | |
| 361 | val = SIGN_EXTENDED32(val, readsamplesize); |
| 362 | |
| 363 | buffer_out[i+1] = val; |
| 364 | } |
| 365 | } |
| 366 | |
Dave Chapman | debbe97 | 2005-09-22 20:46:58 +0000 | [diff] [blame] | 367 | /* 4 and 8 are very common cases (the only ones i've seen). |
| 368 | |
| 369 | The following code is an initial attempt to unroll and optimise |
| 370 | these two cases by the Rockbox project. More work is needed. |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 371 | */ |
Dave Chapman | debbe97 | 2005-09-22 20:46:58 +0000 | [diff] [blame] | 372 | |
| 373 | /* optimised case: 4 */ |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 374 | if (predictor_coef_num == 4) |
| 375 | { |
Dave Chapman | debbe97 | 2005-09-22 20:46:58 +0000 | [diff] [blame] | 376 | for (i = 4 + 1; i < output_size; i++) |
| 377 | { |
| 378 | int sum = 0; |
| 379 | int outval; |
| 380 | int error_val = error_buffer[i]; |
| 381 | |
| 382 | sum = (buffer_out[4] - buffer_out[0]) * predictor_coef_table[0] |
| 383 | + (buffer_out[3] - buffer_out[0]) * predictor_coef_table[1] |
| 384 | + (buffer_out[2] - buffer_out[0]) * predictor_coef_table[2] |
| 385 | + (buffer_out[1] - buffer_out[0]) * predictor_coef_table[3]; |
| 386 | |
| 387 | outval = (1 << (predictor_quantitization-1)) + sum; |
| 388 | outval = outval >> predictor_quantitization; |
| 389 | outval = outval + buffer_out[0] + error_val; |
| 390 | outval = SIGN_EXTENDED32(outval, readsamplesize); |
| 391 | |
| 392 | buffer_out[4+1] = outval; |
| 393 | |
| 394 | if (error_val > 0) |
| 395 | { |
| 396 | int predictor_num = 4 - 1; |
| 397 | |
| 398 | while (predictor_num >= 0 && error_val > 0) |
| 399 | { |
| 400 | int val = buffer_out[0] - buffer_out[4 - predictor_num]; |
| 401 | |
| 402 | if (val!=0) { |
| 403 | if (val < 0) { |
| 404 | predictor_coef_table[predictor_num]++; |
| 405 | val=-val; |
| 406 | } else { |
Bertrik Sikken | 4f87abf | 2009-03-04 18:15:06 +0000 | [diff] [blame] | 407 | predictor_coef_table[predictor_num]--; |
Dave Chapman | debbe97 | 2005-09-22 20:46:58 +0000 | [diff] [blame] | 408 | } |
| 409 | error_val -= ((val >> predictor_quantitization) * (4 - predictor_num)); |
| 410 | } |
| 411 | predictor_num--; |
| 412 | } |
| 413 | } |
| 414 | else if (error_val < 0) |
| 415 | { |
| 416 | int predictor_num = 4 - 1; |
| 417 | |
| 418 | while (predictor_num >= 0 && error_val < 0) |
| 419 | { |
| 420 | int val = buffer_out[0] - buffer_out[4 - predictor_num]; |
| 421 | |
| 422 | if (val != 0) { |
| 423 | if (val > 0) { |
| 424 | predictor_coef_table[predictor_num]++; |
| 425 | val=-val; /* neg value */ |
| 426 | } else { |
| 427 | predictor_coef_table[predictor_num]--; |
| 428 | } |
| 429 | error_val -= ((val >> predictor_quantitization) * (4 - predictor_num)); |
| 430 | } |
| 431 | predictor_num--; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | buffer_out++; |
| 436 | } |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | |
Dave Chapman | debbe97 | 2005-09-22 20:46:58 +0000 | [diff] [blame] | 440 | /* optimised case: 8 */ |
| 441 | if (predictor_coef_num == 8) |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 442 | { |
Dave Chapman | debbe97 | 2005-09-22 20:46:58 +0000 | [diff] [blame] | 443 | for (i = 8 + 1; |
| 444 | i < output_size; |
| 445 | i++) |
| 446 | { |
| 447 | int sum; |
| 448 | int outval; |
| 449 | int error_val = error_buffer[i]; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 450 | |
Dave Chapman | debbe97 | 2005-09-22 20:46:58 +0000 | [diff] [blame] | 451 | sum = (buffer_out[8] - buffer_out[0]) * predictor_coef_table[0] |
| 452 | + (buffer_out[7] - buffer_out[0]) * predictor_coef_table[1] |
| 453 | + (buffer_out[6] - buffer_out[0]) * predictor_coef_table[2] |
| 454 | + (buffer_out[5] - buffer_out[0]) * predictor_coef_table[3] |
| 455 | + (buffer_out[4] - buffer_out[0]) * predictor_coef_table[4] |
| 456 | + (buffer_out[3] - buffer_out[0]) * predictor_coef_table[5] |
| 457 | + (buffer_out[2] - buffer_out[0]) * predictor_coef_table[6] |
| 458 | + (buffer_out[1] - buffer_out[0]) * predictor_coef_table[7]; |
| 459 | |
| 460 | outval = (1 << (predictor_quantitization-1)) + sum; |
| 461 | outval = outval >> predictor_quantitization; |
| 462 | outval = outval + buffer_out[0] + error_val; |
| 463 | outval = SIGN_EXTENDED32(outval, readsamplesize); |
| 464 | |
| 465 | buffer_out[8+1] = outval; |
| 466 | |
| 467 | if (error_val > 0) |
| 468 | { |
| 469 | int predictor_num = 8 - 1; |
| 470 | |
| 471 | while (predictor_num >= 0 && error_val > 0) |
| 472 | { |
| 473 | int val = buffer_out[0] - buffer_out[8 - predictor_num]; |
| 474 | |
| 475 | if (val!=0) { |
| 476 | if (val < 0) { |
| 477 | predictor_coef_table[predictor_num]++; |
| 478 | val=-val; |
| 479 | } else { |
Bertrik Sikken | 4f87abf | 2009-03-04 18:15:06 +0000 | [diff] [blame] | 480 | predictor_coef_table[predictor_num]--; |
Dave Chapman | debbe97 | 2005-09-22 20:46:58 +0000 | [diff] [blame] | 481 | } |
| 482 | error_val -= ((val >> predictor_quantitization) * (8 - predictor_num)); |
| 483 | } |
| 484 | predictor_num--; |
| 485 | } |
| 486 | } |
| 487 | else if (error_val < 0) |
| 488 | { |
| 489 | int predictor_num = 8 - 1; |
| 490 | |
| 491 | while (predictor_num >= 0 && error_val < 0) |
| 492 | { |
| 493 | int val = buffer_out[0] - buffer_out[8 - predictor_num]; |
| 494 | if (val != 0) { |
| 495 | if (val > 0) { |
| 496 | predictor_coef_table[predictor_num]++; |
| 497 | val=-val; /* neg value */ |
| 498 | } else { |
| 499 | predictor_coef_table[predictor_num]--; |
| 500 | } |
| 501 | error_val -= ((val >> predictor_quantitization) * (8 - predictor_num)); |
| 502 | } |
| 503 | predictor_num--; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | buffer_out++; |
| 508 | } |
| 509 | return; |
| 510 | } |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 511 | |
| 512 | /* general case */ |
| 513 | if (predictor_coef_num > 0) |
| 514 | { |
| 515 | for (i = predictor_coef_num + 1; |
| 516 | i < output_size; |
| 517 | i++) |
| 518 | { |
| 519 | int j; |
| 520 | int sum = 0; |
| 521 | int outval; |
| 522 | int error_val = error_buffer[i]; |
| 523 | |
| 524 | for (j = 0; j < predictor_coef_num; j++) |
| 525 | { |
| 526 | sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) * |
| 527 | predictor_coef_table[j]; |
| 528 | } |
| 529 | |
| 530 | outval = (1 << (predictor_quantitization-1)) + sum; |
| 531 | outval = outval >> predictor_quantitization; |
| 532 | outval = outval + buffer_out[0] + error_val; |
| 533 | outval = SIGN_EXTENDED32(outval, readsamplesize); |
| 534 | |
| 535 | buffer_out[predictor_coef_num+1] = outval; |
| 536 | |
| 537 | if (error_val > 0) |
| 538 | { |
| 539 | int predictor_num = predictor_coef_num - 1; |
| 540 | |
| 541 | while (predictor_num >= 0 && error_val > 0) |
| 542 | { |
| 543 | int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; |
| 544 | int sign = SIGN_ONLY(val); |
| 545 | |
| 546 | predictor_coef_table[predictor_num] -= sign; |
| 547 | |
| 548 | val *= sign; /* absolute value */ |
| 549 | |
| 550 | error_val -= ((val >> predictor_quantitization) * |
| 551 | (predictor_coef_num - predictor_num)); |
| 552 | |
| 553 | predictor_num--; |
| 554 | } |
| 555 | } |
| 556 | else if (error_val < 0) |
| 557 | { |
| 558 | int predictor_num = predictor_coef_num - 1; |
| 559 | |
| 560 | while (predictor_num >= 0 && error_val < 0) |
| 561 | { |
| 562 | int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; |
| 563 | int sign = - SIGN_ONLY(val); |
| 564 | |
| 565 | predictor_coef_table[predictor_num] -= sign; |
| 566 | |
| 567 | val *= sign; /* neg value */ |
| 568 | |
| 569 | error_val -= ((val >> predictor_quantitization) * |
| 570 | (predictor_coef_num - predictor_num)); |
| 571 | |
| 572 | predictor_num--; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | buffer_out++; |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
Nils Wallménius | 7c6056b | 2011-06-01 10:28:26 +0000 | [diff] [blame] | 581 | static void deinterlace_16(int32_t* buffer0, |
| 582 | int32_t* buffer1, |
| 583 | int numsamples, |
| 584 | uint8_t interlacing_shift, |
| 585 | uint8_t interlacing_leftweight) ICODE_ATTR_ALAC; |
| 586 | static void deinterlace_16(int32_t* buffer0, |
| 587 | int32_t* buffer1, |
| 588 | int numsamples, |
| 589 | uint8_t interlacing_shift, |
| 590 | uint8_t interlacing_leftweight) |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 591 | { |
| 592 | int i; |
| 593 | if (numsamples <= 0) return; |
| 594 | |
| 595 | /* weighted interlacing */ |
| 596 | if (interlacing_leftweight) |
| 597 | { |
| 598 | for (i = 0; i < numsamples; i++) |
| 599 | { |
| 600 | int32_t difference, midright; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 601 | |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 602 | midright = buffer0[i]; |
| 603 | difference = buffer1[i]; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 604 | |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 605 | buffer0[i] = ((midright - ((difference * interlacing_leftweight) |
| 606 | >> interlacing_shift)) + difference) << SCALE16; |
| 607 | buffer1[i] = (midright - ((difference * interlacing_leftweight) |
| 608 | >> interlacing_shift)) << SCALE16; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | /* otherwise basic interlacing took place */ |
| 615 | for (i = 0; i < numsamples; i++) |
| 616 | { |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 617 | buffer0[i] = buffer0[i] << SCALE16; |
| 618 | buffer1[i] = buffer1[i] << SCALE16; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Nils Wallménius | 7c6056b | 2011-06-01 10:28:26 +0000 | [diff] [blame] | 622 | static void deinterlace_24(int32_t *buffer0, int32_t *buffer1, |
| 623 | int uncompressed_bytes, |
| 624 | int32_t *uncompressed_bytes_buffer0, |
| 625 | int32_t *uncompressed_bytes_buffer1, |
| 626 | int numsamples, |
| 627 | uint8_t interlacing_shift, |
| 628 | uint8_t interlacing_leftweight) ICODE_ATTR_ALAC; |
| 629 | static void deinterlace_24(int32_t *buffer0, int32_t *buffer1, |
| 630 | int uncompressed_bytes, |
| 631 | int32_t *uncompressed_bytes_buffer0, |
| 632 | int32_t *uncompressed_bytes_buffer1, |
| 633 | int numsamples, |
| 634 | uint8_t interlacing_shift, |
| 635 | uint8_t interlacing_leftweight) |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 636 | { |
| 637 | int i; |
| 638 | if (numsamples <= 0) return; |
| 639 | |
| 640 | /* weighted interlacing */ |
| 641 | if (interlacing_leftweight) |
| 642 | { |
| 643 | for (i = 0; i < numsamples; i++) |
| 644 | { |
| 645 | int32_t difference, midright; |
| 646 | |
| 647 | midright = buffer0[i]; |
| 648 | difference = buffer1[i]; |
| 649 | |
| 650 | buffer0[i] = ((midright - ((difference * interlacing_leftweight) |
| 651 | >> interlacing_shift)) + difference) << SCALE24; |
| 652 | buffer1[i] = (midright - ((difference * interlacing_leftweight) |
| 653 | >> interlacing_shift)) << SCALE24; |
| 654 | |
| 655 | if (uncompressed_bytes) |
| 656 | { |
| 657 | uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8)); |
| 658 | buffer0[i] <<= (uncompressed_bytes * 8); |
| 659 | buffer1[i] <<= (uncompressed_bytes * 8); |
| 660 | |
| 661 | buffer0[i] |= uncompressed_bytes_buffer0[i] & mask; |
| 662 | buffer1[i] |= uncompressed_bytes_buffer1[i] & mask; |
| 663 | } |
| 664 | |
| 665 | } |
| 666 | |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | /* otherwise basic interlacing took place */ |
| 671 | for (i = 0; i < numsamples; i++) |
| 672 | { |
| 673 | if (uncompressed_bytes) |
| 674 | { |
| 675 | uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8)); |
| 676 | buffer0[i] <<= (uncompressed_bytes * 8); |
| 677 | buffer1[i] <<= (uncompressed_bytes * 8); |
| 678 | |
| 679 | buffer0[i] |= uncompressed_bytes_buffer0[i] & mask; |
| 680 | buffer1[i] |= uncompressed_bytes_buffer1[i] & mask; |
| 681 | } |
| 682 | |
| 683 | buffer0[i] = buffer0[i] << SCALE24; |
| 684 | buffer1[i] = buffer1[i] << SCALE24; |
| 685 | } |
| 686 | |
| 687 | } |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 688 | |
| 689 | static inline int decode_frame_mono( |
| 690 | alac_file *alac, |
| 691 | int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE], |
| 692 | void (*yield)(void)) |
| 693 | { |
| 694 | int hassize; |
| 695 | int isnotcompressed; |
| 696 | int readsamplesize; |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 697 | int infosamplesize = alac->setinfo_sample_size; |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 698 | int outputsamples = alac->setinfo_max_samples_per_frame; |
| 699 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 700 | int uncompressed_bytes; |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 701 | int ricemodifier; |
| 702 | |
| 703 | |
| 704 | /* 2^result = something to do with output waiting. |
| 705 | * perhaps matters if we read > 1 frame in a pass? |
| 706 | */ |
| 707 | readbits(alac, 4); |
| 708 | |
| 709 | readbits(alac, 12); /* unknown, skip 12 bits */ |
| 710 | |
| 711 | hassize = readbits(alac, 1); /* the output sample size is stored soon */ |
| 712 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 713 | /* number of bytes in the (compressed) stream that are not compressed */ |
| 714 | uncompressed_bytes = readbits(alac, 2); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 715 | |
| 716 | isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */ |
| 717 | |
| 718 | if (hassize) |
| 719 | { |
| 720 | /* now read the number of samples, |
| 721 | * as a 32bit integer */ |
| 722 | outputsamples = readbits(alac, 32); |
| 723 | } |
| 724 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 725 | readsamplesize = infosamplesize - (uncompressed_bytes * 8); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 726 | |
| 727 | if (!isnotcompressed) |
| 728 | { /* so it is compressed */ |
| 729 | int predictor_coef_num; |
| 730 | int prediction_type; |
| 731 | int prediction_quantitization; |
| 732 | int i; |
| 733 | |
| 734 | /* skip 16 bits, not sure what they are. seem to be used in |
| 735 | * two channel case */ |
| 736 | readbits(alac, 8); |
| 737 | readbits(alac, 8); |
| 738 | |
| 739 | prediction_type = readbits(alac, 4); |
| 740 | prediction_quantitization = readbits(alac, 4); |
| 741 | |
| 742 | ricemodifier = readbits(alac, 3); |
| 743 | predictor_coef_num = readbits(alac, 5); |
| 744 | |
| 745 | /* read the predictor table */ |
| 746 | for (i = 0; i < predictor_coef_num; i++) |
| 747 | { |
| 748 | predictor_coef_table[i] = (int16_t)readbits(alac, 16); |
| 749 | } |
| 750 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 751 | if (uncompressed_bytes) |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 752 | { |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 753 | int i; |
| 754 | for (i = 0; i < outputsamples; i++) |
| 755 | { |
| 756 | outputbuffer[0][i] = readbits(alac, uncompressed_bytes * 8); |
| 757 | outputbuffer[1][i] = outputbuffer[0][i]; |
| 758 | } |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | yield(); |
| 762 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 763 | entropy_rice_decode(alac, |
| 764 | outputbuffer[0], |
| 765 | outputsamples, |
| 766 | readsamplesize, |
| 767 | alac->setinfo_rice_initialhistory, |
| 768 | alac->setinfo_rice_kmodifier, |
| 769 | ricemodifier * alac->setinfo_rice_historymult / 4, |
| 770 | (1 << alac->setinfo_rice_kmodifier) - 1); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 771 | |
| 772 | yield(); |
| 773 | |
| 774 | if (prediction_type == 0) |
| 775 | { /* adaptive fir */ |
| 776 | predictor_decompress_fir_adapt(outputbuffer[0], |
| 777 | outputbuffer[0], |
| 778 | outputsamples, |
| 779 | readsamplesize, |
| 780 | predictor_coef_table, |
| 781 | predictor_coef_num, |
| 782 | prediction_quantitization); |
| 783 | } |
| 784 | else |
| 785 | { |
| 786 | //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type); |
| 787 | /* i think the only other prediction type (or perhaps this is just a |
| 788 | * boolean?) runs adaptive fir twice.. like: |
| 789 | * predictor_decompress_fir_adapt(predictor_error, tempout, ...) |
| 790 | * predictor_decompress_fir_adapt(predictor_error, outputsamples ...) |
| 791 | * little strange.. |
| 792 | */ |
| 793 | } |
| 794 | |
| 795 | } |
| 796 | else |
| 797 | { /* not compressed, easy case */ |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 798 | if (infosamplesize <= 16) |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 799 | { |
| 800 | int i; |
| 801 | for (i = 0; i < outputsamples; i++) |
| 802 | { |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 803 | int32_t audiobits = readbits(alac, infosamplesize); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 804 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 805 | audiobits = SIGN_EXTENDED32(audiobits, infosamplesize); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 806 | |
| 807 | outputbuffer[0][i] = audiobits; |
| 808 | } |
| 809 | } |
| 810 | else |
| 811 | { |
| 812 | int i; |
| 813 | for (i = 0; i < outputsamples; i++) |
| 814 | { |
| 815 | int32_t audiobits; |
| 816 | |
| 817 | audiobits = readbits(alac, 16); |
| 818 | /* special case of sign extension.. |
| 819 | * as we'll be ORing the low 16bits into this */ |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 820 | audiobits = audiobits << (infosamplesize - 16); |
| 821 | audiobits |= readbits(alac, infosamplesize - 16); |
| 822 | audiobits = SIGNEXTEND24(audiobits); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 823 | |
| 824 | outputbuffer[0][i] = audiobits; |
| 825 | } |
| 826 | } |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 827 | uncompressed_bytes = 0; // always 0 for uncompressed |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | yield(); |
| 831 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 832 | switch(infosamplesize) |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 833 | { |
| 834 | case 16: |
| 835 | { |
| 836 | int i; |
| 837 | for (i = 0; i < outputsamples; i++) |
| 838 | { |
| 839 | /* Output mono data as stereo */ |
| 840 | outputbuffer[0][i] = outputbuffer[0][i] << SCALE16; |
| 841 | outputbuffer[1][i] = outputbuffer[0][i]; |
| 842 | } |
| 843 | break; |
| 844 | } |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 845 | case 24: |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 846 | { |
| 847 | int i; |
| 848 | for (i = 0; i < outputsamples; i++) |
| 849 | { |
| 850 | int32_t sample = outputbuffer[0][i]; |
| 851 | |
| 852 | if (uncompressed_bytes) |
| 853 | { |
| 854 | uint32_t mask; |
| 855 | sample = sample << (uncompressed_bytes * 8); |
| 856 | mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8)); |
| 857 | sample |= outputbuffer[0][i] & mask; |
| 858 | } |
| 859 | |
| 860 | outputbuffer[0][i] = sample << SCALE24; |
| 861 | outputbuffer[1][i] = outputbuffer[0][i]; |
| 862 | } |
| 863 | break; |
| 864 | } |
| 865 | case 20: |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 866 | case 32: |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 867 | //fprintf(stderr, "FIXME: unimplemented sample size %i\n", infosamplesize); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 868 | break; |
| 869 | default: |
| 870 | break; |
| 871 | } |
| 872 | |
| 873 | return outputsamples; |
| 874 | } |
| 875 | |
| 876 | static inline int decode_frame_stereo( |
| 877 | alac_file *alac, |
| 878 | int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE], |
| 879 | void (*yield)(void)) |
| 880 | { |
| 881 | int hassize; |
| 882 | int isnotcompressed; |
| 883 | int readsamplesize; |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 884 | int infosamplesize = alac->setinfo_sample_size; |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 885 | int outputsamples = alac->setinfo_max_samples_per_frame; |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 886 | int uncompressed_bytes; |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 887 | |
| 888 | uint8_t interlacing_shift; |
| 889 | uint8_t interlacing_leftweight; |
| 890 | |
| 891 | /* 2^result = something to do with output waiting. |
| 892 | * perhaps matters if we read > 1 frame in a pass? |
| 893 | */ |
| 894 | readbits(alac, 4); |
| 895 | |
| 896 | readbits(alac, 12); /* unknown, skip 12 bits */ |
| 897 | |
| 898 | hassize = readbits(alac, 1); /* the output sample size is stored soon */ |
| 899 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 900 | /* the number of bytes in the (compressed) stream that are not compressed */ |
| 901 | uncompressed_bytes = readbits(alac, 2); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 902 | |
| 903 | isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */ |
| 904 | |
| 905 | if (hassize) |
| 906 | { |
| 907 | /* now read the number of samples, |
| 908 | * as a 32bit integer */ |
| 909 | outputsamples = readbits(alac, 32); |
| 910 | } |
| 911 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 912 | readsamplesize = infosamplesize - (uncompressed_bytes * 8) + 1; |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 913 | |
| 914 | yield(); |
| 915 | if (!isnotcompressed) |
| 916 | { /* compressed */ |
| 917 | int predictor_coef_num_a; |
| 918 | int prediction_type_a; |
| 919 | int prediction_quantitization_a; |
| 920 | int ricemodifier_a; |
| 921 | |
| 922 | int predictor_coef_num_b; |
| 923 | int prediction_type_b; |
| 924 | int prediction_quantitization_b; |
| 925 | int ricemodifier_b; |
| 926 | |
| 927 | int i; |
| 928 | |
| 929 | interlacing_shift = readbits(alac, 8); |
| 930 | interlacing_leftweight = readbits(alac, 8); |
| 931 | |
| 932 | /******** channel 1 ***********/ |
| 933 | prediction_type_a = readbits(alac, 4); |
| 934 | prediction_quantitization_a = readbits(alac, 4); |
| 935 | |
| 936 | ricemodifier_a = readbits(alac, 3); |
| 937 | predictor_coef_num_a = readbits(alac, 5); |
| 938 | |
| 939 | /* read the predictor table */ |
| 940 | for (i = 0; i < predictor_coef_num_a; i++) |
| 941 | { |
| 942 | predictor_coef_table_a[i] = (int16_t)readbits(alac, 16); |
| 943 | } |
| 944 | |
| 945 | /******** channel 2 *********/ |
| 946 | prediction_type_b = readbits(alac, 4); |
| 947 | prediction_quantitization_b = readbits(alac, 4); |
| 948 | |
| 949 | ricemodifier_b = readbits(alac, 3); |
| 950 | predictor_coef_num_b = readbits(alac, 5); |
| 951 | |
| 952 | /* read the predictor table */ |
| 953 | for (i = 0; i < predictor_coef_num_b; i++) |
| 954 | { |
| 955 | predictor_coef_table_b[i] = (int16_t)readbits(alac, 16); |
| 956 | } |
| 957 | |
| 958 | /*********************/ |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 959 | if (uncompressed_bytes) |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 960 | { /* see mono case */ |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 961 | int i; |
| 962 | for (i = 0; i < outputsamples; i++) |
| 963 | { |
| 964 | outputbuffer[0][i] = readbits(alac, uncompressed_bytes * 8); |
| 965 | outputbuffer[1][i] = readbits(alac, uncompressed_bytes * 8); |
| 966 | } |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | yield(); |
| 970 | /* channel 1 */ |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 971 | entropy_rice_decode(alac, |
| 972 | outputbuffer[0], |
| 973 | outputsamples, |
| 974 | readsamplesize, |
| 975 | alac->setinfo_rice_initialhistory, |
| 976 | alac->setinfo_rice_kmodifier, |
| 977 | ricemodifier_a * alac->setinfo_rice_historymult / 4, |
| 978 | (1 << alac->setinfo_rice_kmodifier) - 1); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 979 | |
| 980 | yield(); |
| 981 | if (prediction_type_a == 0) |
| 982 | { /* adaptive fir */ |
| 983 | predictor_decompress_fir_adapt(outputbuffer[0], |
| 984 | outputbuffer[0], |
| 985 | outputsamples, |
| 986 | readsamplesize, |
| 987 | predictor_coef_table_a, |
| 988 | predictor_coef_num_a, |
| 989 | prediction_quantitization_a); |
| 990 | } |
| 991 | else |
| 992 | { /* see mono case */ |
| 993 | //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_a); |
| 994 | } |
| 995 | |
| 996 | yield(); |
| 997 | |
| 998 | /* channel 2 */ |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 999 | entropy_rice_decode(alac, |
| 1000 | outputbuffer[1], |
| 1001 | outputsamples, |
| 1002 | readsamplesize, |
| 1003 | alac->setinfo_rice_initialhistory, |
| 1004 | alac->setinfo_rice_kmodifier, |
| 1005 | ricemodifier_b * alac->setinfo_rice_historymult / 4, |
| 1006 | (1 << alac->setinfo_rice_kmodifier) - 1); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1007 | |
| 1008 | yield(); |
| 1009 | if (prediction_type_b == 0) |
| 1010 | { /* adaptive fir */ |
| 1011 | predictor_decompress_fir_adapt(outputbuffer[1], |
| 1012 | outputbuffer[1], |
| 1013 | outputsamples, |
| 1014 | readsamplesize, |
| 1015 | predictor_coef_table_b, |
| 1016 | predictor_coef_num_b, |
| 1017 | prediction_quantitization_b); |
| 1018 | } |
| 1019 | else |
| 1020 | { |
| 1021 | //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_b); |
| 1022 | } |
| 1023 | } |
| 1024 | else |
| 1025 | { /* not compressed, easy case */ |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 1026 | if (infosamplesize <= 16) |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1027 | { |
| 1028 | int i; |
| 1029 | for (i = 0; i < outputsamples; i++) |
| 1030 | { |
| 1031 | int32_t audiobits_a, audiobits_b; |
| 1032 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 1033 | audiobits_a = readbits(alac, infosamplesize); |
| 1034 | audiobits_b = readbits(alac, infosamplesize); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1035 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 1036 | audiobits_a = SIGN_EXTENDED32(audiobits_a, infosamplesize); |
| 1037 | audiobits_b = SIGN_EXTENDED32(audiobits_b, infosamplesize); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1038 | |
| 1039 | outputbuffer[0][i] = audiobits_a; |
| 1040 | outputbuffer[1][i] = audiobits_b; |
| 1041 | } |
| 1042 | } |
| 1043 | else |
| 1044 | { |
| 1045 | int i; |
| 1046 | for (i = 0; i < outputsamples; i++) |
| 1047 | { |
| 1048 | int32_t audiobits_a, audiobits_b; |
| 1049 | |
| 1050 | audiobits_a = readbits(alac, 16); |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 1051 | audiobits_a = audiobits_a << (infosamplesize - 16); |
| 1052 | audiobits_a |= readbits(alac, infosamplesize - 16); |
| 1053 | audiobits_a = SIGNEXTEND24(audiobits_a); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1054 | |
| 1055 | audiobits_b = readbits(alac, 16); |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 1056 | audiobits_b = audiobits_b << (infosamplesize - 16); |
| 1057 | audiobits_b |= readbits(alac, infosamplesize - 16); |
| 1058 | audiobits_b = SIGNEXTEND24(audiobits_b); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1059 | |
| 1060 | outputbuffer[0][i] = audiobits_a; |
| 1061 | outputbuffer[1][i] = audiobits_b; |
| 1062 | } |
| 1063 | } |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 1064 | uncompressed_bytes = 0; // always 0 for uncompressed |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1065 | interlacing_shift = 0; |
| 1066 | interlacing_leftweight = 0; |
| 1067 | } |
| 1068 | |
| 1069 | yield(); |
| 1070 | |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 1071 | switch(infosamplesize) |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1072 | { |
| 1073 | case 16: |
| 1074 | { |
| 1075 | deinterlace_16(outputbuffer[0], |
| 1076 | outputbuffer[1], |
| 1077 | outputsamples, |
| 1078 | interlacing_shift, |
| 1079 | interlacing_leftweight); |
| 1080 | break; |
| 1081 | } |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1082 | case 24: |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 1083 | { |
| 1084 | deinterlace_24(outputbuffer[0], |
| 1085 | outputbuffer[1], |
| 1086 | uncompressed_bytes, |
| 1087 | outputbuffer[0], |
| 1088 | outputbuffer[1], |
| 1089 | outputsamples, |
| 1090 | interlacing_shift, |
| 1091 | interlacing_leftweight); |
| 1092 | break; |
| 1093 | } |
| 1094 | case 20: |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1095 | case 32: |
Tom Ross | db64bf9 | 2010-02-03 00:37:24 +0000 | [diff] [blame] | 1096 | //fprintf(stderr, "FIXME: unimplemented sample size %i\n", infosamplesize); |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1097 | break; |
| 1098 | default: |
| 1099 | break; |
| 1100 | } |
| 1101 | return outputsamples; |
| 1102 | } |
| 1103 | |
| 1104 | int alac_decode_frame(alac_file *alac, |
| 1105 | unsigned char *inbuffer, |
| 1106 | int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE], |
| 1107 | void (*yield)(void)) |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 1108 | { |
| 1109 | int channels; |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1110 | int outputsamples; |
Andree Buschmann | 8055b48 | 2011-04-16 19:26:07 +0000 | [diff] [blame] | 1111 | unsigned char *input_buffer_start; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 1112 | |
| 1113 | /* setup the stream */ |
| 1114 | alac->input_buffer = inbuffer; |
| 1115 | alac->input_buffer_bitaccumulator = 0; |
Andree Buschmann | 19f9fd0 | 2011-04-16 19:08:50 +0000 | [diff] [blame] | 1116 | |
| 1117 | /* save to gather byte consumption */ |
Andree Buschmann | 8055b48 | 2011-04-16 19:26:07 +0000 | [diff] [blame] | 1118 | input_buffer_start = alac->input_buffer; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 1119 | |
| 1120 | channels = readbits(alac, 3); |
| 1121 | |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1122 | /* TODO: The mono and stereo functions should be combined. */ |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 1123 | switch(channels) |
| 1124 | { |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1125 | case 0: /* 1 channel */ |
| 1126 | outputsamples=decode_frame_mono(alac,outputbuffer,yield); |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 1127 | break; |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1128 | case 1: /* 2 channels */ |
| 1129 | outputsamples=decode_frame_stereo(alac,outputbuffer,yield); |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 1130 | break; |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1131 | default: /* Unsupported */ |
| 1132 | return -1; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 1133 | } |
Andree Buschmann | 19f9fd0 | 2011-04-16 19:08:50 +0000 | [diff] [blame] | 1134 | |
| 1135 | /* calculate consumed bytes */ |
Andree Buschmann | 8055b48 | 2011-04-16 19:26:07 +0000 | [diff] [blame] | 1136 | alac->bytes_consumed = (int)(alac->input_buffer - input_buffer_start); |
Andree Buschmann | 19f9fd0 | 2011-04-16 19:08:50 +0000 | [diff] [blame] | 1137 | alac->bytes_consumed += (alac->input_buffer_bitaccumulator>5) ? 2 : 1; |
| 1138 | |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 1139 | return outputsamples; |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Nils Wallménius | 4909e09 | 2011-06-02 14:59:15 +0000 | [diff] [blame] | 1142 | /* rockbox: not used |
Dave Chapman | debbe97 | 2005-09-22 20:46:58 +0000 | [diff] [blame] | 1143 | void create_alac(int samplesize, int numchannels, alac_file* alac) |
Dave Chapman | 711b2e3 | 2005-09-22 18:47:04 +0000 | [diff] [blame] | 1144 | { |
Dave Chapman | debbe97 | 2005-09-22 20:46:58 +0000 | [diff] [blame] | 1145 | alac->samplesize = samplesize; |
| 1146 | alac->numchannels = numchannels; |
| 1147 | alac->bytespersample = (samplesize / 8) * numchannels; |
Nils Wallménius | 4909e09 | 2011-06-02 14:59:15 +0000 | [diff] [blame] | 1148 | } */ |