]>
Commit | Line | Data |
---|---|---|
31cf954d PM |
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 | |
532a468a | 9 | * |
31cf954d PM |
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 | static const int host_bigendian = 0; | |
33 | ||
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <string.h> | |
37 | #ifdef _WIN32 | |
064bd293 | 38 | #include "stdint_win.h" |
31cf954d | 39 | #else |
064bd293 | 40 | #include <stdint.h> |
31cf954d PM |
41 | #endif |
42 | ||
43 | #include "alac.h" | |
44 | ||
064bd293 MB |
45 | #define _Swap32(v) \ |
46 | do { \ | |
47 | v = (((v)&0x000000FF) << 0x18) | (((v)&0x0000FF00) << 0x08) | (((v)&0x00FF0000) >> 0x08) | \ | |
48 | (((v)&0xFF000000) >> 0x18); \ | |
49 | } while (0) | |
31cf954d | 50 | |
064bd293 MB |
51 | #define _Swap16(v) \ |
52 | do { \ | |
53 | v = (((v)&0x00FF) << 0x08) | (((v)&0xFF00) >> 0x08); \ | |
54 | } while (0) | |
31cf954d | 55 | |
064bd293 MB |
56 | struct { |
57 | signed int x : 24; | |
58 | } se_struct_24; | |
31cf954d PM |
59 | #define SignExtend24(val) (se_struct_24.x = val) |
60 | ||
c627bae0 | 61 | void alac_free(alac_file *alac) { |
064bd293 MB |
62 | if (alac->predicterror_buffer_a) |
63 | free(alac->predicterror_buffer_a); | |
64 | if (alac->predicterror_buffer_b) | |
65 | free(alac->predicterror_buffer_b); | |
66 | ||
67 | if (alac->outputsamples_buffer_a) | |
68 | free(alac->outputsamples_buffer_a); | |
69 | if (alac->outputsamples_buffer_b) | |
70 | free(alac->outputsamples_buffer_b); | |
71 | ||
72 | if (alac->uncompressed_bytes_buffer_a) | |
73 | free(alac->uncompressed_bytes_buffer_a); | |
74 | if (alac->uncompressed_bytes_buffer_b) | |
75 | free(alac->uncompressed_bytes_buffer_b); | |
76 | ||
77 | free(alac); | |
c627bae0 JL |
78 | } |
79 | ||
064bd293 MB |
80 | void alac_allocate_buffers(alac_file *alac) { |
81 | alac->predicterror_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4); | |
82 | alac->predicterror_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4); | |
31cf954d | 83 | |
064bd293 MB |
84 | alac->outputsamples_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4); |
85 | alac->outputsamples_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4); | |
532a468a | 86 | |
064bd293 MB |
87 | alac->uncompressed_bytes_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4); |
88 | alac->uncompressed_bytes_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4); | |
31cf954d PM |
89 | } |
90 | ||
064bd293 | 91 | void alac_set_info(alac_file *alac, char *inputbuffer) { |
31cf954d PM |
92 | char *ptr = inputbuffer; |
93 | ptr += 4; /* size */ | |
94 | ptr += 4; /* frma */ | |
95 | ptr += 4; /* alac */ | |
96 | ptr += 4; /* size */ | |
97 | ptr += 4; /* alac */ | |
98 | ||
99 | ptr += 4; /* 0 ? */ | |
100 | ||
064bd293 | 101 | alac->setinfo_max_samples_per_frame = *(uint32_t *)ptr; /* buffer size / 2 ? */ |
31cf954d | 102 | if (!host_bigendian) |
064bd293 | 103 | _Swap32(alac->setinfo_max_samples_per_frame); |
31cf954d | 104 | ptr += 4; |
064bd293 | 105 | alac->setinfo_7a = *(uint8_t *)ptr; |
31cf954d | 106 | ptr += 1; |
064bd293 | 107 | alac->setinfo_sample_size = *(uint8_t *)ptr; |
31cf954d | 108 | ptr += 1; |
064bd293 | 109 | alac->setinfo_rice_historymult = *(uint8_t *)ptr; |
31cf954d | 110 | ptr += 1; |
064bd293 | 111 | alac->setinfo_rice_initialhistory = *(uint8_t *)ptr; |
31cf954d | 112 | ptr += 1; |
064bd293 | 113 | alac->setinfo_rice_kmodifier = *(uint8_t *)ptr; |
31cf954d | 114 | ptr += 1; |
064bd293 | 115 | alac->setinfo_7f = *(uint8_t *)ptr; |
31cf954d | 116 | ptr += 1; |
064bd293 | 117 | alac->setinfo_80 = *(uint16_t *)ptr; |
31cf954d | 118 | if (!host_bigendian) |
064bd293 | 119 | _Swap16(alac->setinfo_80); |
31cf954d | 120 | ptr += 2; |
064bd293 | 121 | alac->setinfo_82 = *(uint32_t *)ptr; |
31cf954d | 122 | if (!host_bigendian) |
064bd293 | 123 | _Swap32(alac->setinfo_82); |
31cf954d | 124 | ptr += 4; |
064bd293 | 125 | alac->setinfo_86 = *(uint32_t *)ptr; |
31cf954d | 126 | if (!host_bigendian) |
064bd293 | 127 | _Swap32(alac->setinfo_86); |
31cf954d | 128 | ptr += 4; |
064bd293 | 129 | alac->setinfo_8a_rate = *(uint32_t *)ptr; |
31cf954d | 130 | if (!host_bigendian) |
064bd293 | 131 | _Swap32(alac->setinfo_8a_rate); |
31cf954d | 132 | |
a2fb5d21 | 133 | alac_allocate_buffers(alac); |
31cf954d PM |
134 | } |
135 | ||
136 | /* stream reading */ | |
137 | ||
138 | /* supports reading 1 to 16 bits, in big endian format */ | |
064bd293 MB |
139 | static uint32_t readbits_16(alac_file *alac, int bits) { |
140 | uint32_t result; | |
141 | int new_accumulator; | |
31cf954d | 142 | |
064bd293 | 143 | result = (alac->input_buffer[0] << 16) | (alac->input_buffer[1] << 8) | (alac->input_buffer[2]); |
31cf954d | 144 | |
064bd293 MB |
145 | /* shift left by the number of bits we've already read, |
146 | * so that the top 'n' bits of the 24 bits we read will | |
147 | * be the return bits */ | |
148 | result = result << alac->input_buffer_bitaccumulator; | |
31cf954d | 149 | |
064bd293 | 150 | result = result & 0x00ffffff; |
31cf954d | 151 | |
064bd293 MB |
152 | /* and then only want the top 'n' bits from that, where |
153 | * n is 'bits' */ | |
154 | result = result >> (24 - bits); | |
31cf954d | 155 | |
064bd293 | 156 | new_accumulator = (alac->input_buffer_bitaccumulator + bits); |
31cf954d | 157 | |
064bd293 MB |
158 | /* increase the buffer pointer if we've read over n bytes. */ |
159 | alac->input_buffer += (new_accumulator >> 3); | |
31cf954d | 160 | |
064bd293 MB |
161 | /* and the remainder goes back into the bit accumulator */ |
162 | alac->input_buffer_bitaccumulator = (new_accumulator & 7); | |
31cf954d | 163 | |
064bd293 | 164 | return result; |
31cf954d PM |
165 | } |
166 | ||
167 | /* supports reading 1 to 32 bits, in big endian format */ | |
064bd293 MB |
168 | static uint32_t readbits(alac_file *alac, int bits) { |
169 | int32_t result = 0; | |
31cf954d | 170 | |
064bd293 MB |
171 | if (bits > 16) { |
172 | bits -= 16; | |
173 | result = readbits_16(alac, 16) << bits; | |
174 | } | |
31cf954d | 175 | |
064bd293 | 176 | result |= readbits_16(alac, bits); |
31cf954d | 177 | |
064bd293 | 178 | return result; |
31cf954d PM |
179 | } |
180 | ||
181 | /* reads a single bit */ | |
064bd293 MB |
182 | static int readbit(alac_file *alac) { |
183 | int result; | |
184 | int new_accumulator; | |
31cf954d | 185 | |
064bd293 | 186 | result = alac->input_buffer[0]; |
31cf954d | 187 | |
064bd293 | 188 | result = result << alac->input_buffer_bitaccumulator; |
31cf954d | 189 | |
064bd293 | 190 | result = result >> 7 & 1; |
31cf954d | 191 | |
064bd293 | 192 | new_accumulator = (alac->input_buffer_bitaccumulator + 1); |
31cf954d | 193 | |
064bd293 | 194 | alac->input_buffer += (new_accumulator / 8); |
31cf954d | 195 | |
064bd293 | 196 | alac->input_buffer_bitaccumulator = (new_accumulator % 8); |
31cf954d | 197 | |
064bd293 | 198 | return result; |
31cf954d PM |
199 | } |
200 | ||
064bd293 MB |
201 | static void unreadbits(alac_file *alac, int bits) { |
202 | int new_accumulator = (alac->input_buffer_bitaccumulator - bits); | |
31cf954d | 203 | |
064bd293 | 204 | alac->input_buffer += (new_accumulator >> 3); |
31cf954d | 205 | |
064bd293 MB |
206 | alac->input_buffer_bitaccumulator = (new_accumulator & 7); |
207 | if (alac->input_buffer_bitaccumulator < 0) | |
208 | alac->input_buffer_bitaccumulator *= -1; | |
31cf954d PM |
209 | } |
210 | ||
211 | /* various implementations of count_leading_zero: | |
212 | * the first one is the original one, the simplest and most | |
213 | * obvious for what it's doing. never use this. | |
214 | * then there are the asm ones. fill in as necessary | |
215 | * and finally an unrolled and optimised c version | |
216 | * to fall back to | |
217 | */ | |
218 | #if 0 | |
219 | /* hideously inefficient. could use a bitmask search, | |
220 | * alternatively bsr on x86, | |
221 | */ | |
222 | static int count_leading_zeros(int32_t input) | |
223 | { | |
224 | int i = 0; | |
225 | while (!(0x80000000 & input) && i < 32) | |
226 | { | |
227 | i++; | |
228 | input = input << 1; | |
229 | } | |
230 | return i; | |
231 | } | |
2a002388 | 232 | #elif defined(__GNUC__) |
31cf954d PM |
233 | /* for some reason the unrolled version (below) is |
234 | * actually faster than this. yay intel! | |
235 | */ | |
064bd293 | 236 | static int count_leading_zeros(int input) { return __builtin_clz(input); } |
31cf954d | 237 | #elif defined(_MSC_VER) && defined(_M_IX86) |
064bd293 MB |
238 | static int count_leading_zeros(int input) { |
239 | int output = 0; | |
240 | if (!input) | |
241 | return 32; | |
242 | __asm | |
243 | { | |
31cf954d PM |
244 | mov eax, input; |
245 | mov edx, 0x1f; | |
246 | bsr ecx, eax; | |
247 | sub edx, ecx; | |
248 | mov output, edx; | |
064bd293 MB |
249 | } |
250 | return output; | |
31cf954d PM |
251 | } |
252 | #else | |
253 | #warning using generic count leading zeroes. You may wish to write one for your CPU / compiler | |
064bd293 MB |
254 | static int count_leading_zeros(int input) { |
255 | int output = 0; | |
256 | int curbyte = 0; | |
31cf954d | 257 | |
064bd293 MB |
258 | curbyte = input >> 24; |
259 | if (curbyte) | |
260 | goto found; | |
261 | output += 8; | |
31cf954d | 262 | |
064bd293 MB |
263 | curbyte = input >> 16; |
264 | if (curbyte & 0xff) | |
265 | goto found; | |
266 | output += 8; | |
31cf954d | 267 | |
064bd293 MB |
268 | curbyte = input >> 8; |
269 | if (curbyte & 0xff) | |
270 | goto found; | |
271 | output += 8; | |
31cf954d | 272 | |
064bd293 MB |
273 | curbyte = input; |
274 | if (curbyte & 0xff) | |
275 | goto found; | |
276 | output += 8; | |
31cf954d | 277 | |
064bd293 | 278 | return output; |
31cf954d PM |
279 | |
280 | found: | |
064bd293 MB |
281 | if (!(curbyte & 0xf0)) { |
282 | output += 4; | |
283 | } else | |
284 | curbyte >>= 4; | |
285 | ||
286 | if (curbyte & 0x8) | |
287 | return output; | |
288 | if (curbyte & 0x4) | |
289 | return output + 1; | |
290 | if (curbyte & 0x2) | |
291 | return output + 2; | |
292 | if (curbyte & 0x1) | |
293 | return output + 3; | |
294 | ||
295 | /* shouldn't get here: */ | |
296 | return output + 4; | |
31cf954d PM |
297 | } |
298 | #endif | |
299 | ||
300 | #define RICE_THRESHOLD 8 // maximum number of bits for a rice prefix. | |
301 | ||
064bd293 MB |
302 | static int32_t entropy_decode_value(alac_file *alac, int readSampleSize, int k, |
303 | int rice_kmodifier_mask) { | |
304 | int32_t x = 0; // decoded value | |
532a468a | 305 | |
064bd293 MB |
306 | // read x, number of 1s before 0 represent the rice value. |
307 | while (x <= RICE_THRESHOLD && readbit(alac)) { | |
308 | x++; | |
309 | } | |
532a468a | 310 | |
064bd293 MB |
311 | if (x > RICE_THRESHOLD) { |
312 | // read the number from the bit stream (raw value) | |
313 | int32_t value; | |
532a468a | 314 | |
064bd293 | 315 | value = readbits(alac, readSampleSize); |
532a468a | 316 | |
064bd293 MB |
317 | // mask value |
318 | value &= (((uint32_t)0xffffffff) >> (32 - readSampleSize)); | |
532a468a | 319 | |
064bd293 MB |
320 | x = value; |
321 | } else { | |
322 | if (k != 1) { | |
323 | int extraBits = readbits(alac, k); | |
532a468a | 324 | |
064bd293 MB |
325 | // x = x * (2^k - 1) |
326 | x *= (((1 << k) - 1) & rice_kmodifier_mask); | |
532a468a | 327 | |
064bd293 MB |
328 | if (extraBits > 1) |
329 | x += extraBits - 1; | |
330 | else | |
331 | unreadbits(alac, 1); | |
532a468a | 332 | } |
064bd293 | 333 | } |
532a468a | 334 | |
064bd293 | 335 | return x; |
31cf954d PM |
336 | } |
337 | ||
064bd293 MB |
338 | static void entropy_rice_decode(alac_file *alac, int32_t *outputBuffer, int outputSize, |
339 | int readSampleSize, int rice_initialhistory, int rice_kmodifier, | |
340 | int rice_historymult, int rice_kmodifier_mask) { | |
341 | int outputCount; | |
342 | int history = rice_initialhistory; | |
343 | int signModifier = 0; | |
532a468a | 344 | |
064bd293 MB |
345 | for (outputCount = 0; outputCount < outputSize; outputCount++) { |
346 | int32_t decodedValue; | |
347 | int32_t finalValue; | |
348 | int32_t k; | |
532a468a | 349 | |
064bd293 | 350 | k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3); |
532a468a | 351 | |
064bd293 MB |
352 | if (k < 0) |
353 | k += rice_kmodifier; | |
354 | else | |
355 | k = rice_kmodifier; | |
532a468a | 356 | |
064bd293 MB |
357 | // note: don't use rice_kmodifier_mask here (set mask to 0xFFFFFFFF) |
358 | decodedValue = entropy_decode_value(alac, readSampleSize, k, 0xFFFFFFFF); | |
532a468a | 359 | |
064bd293 MB |
360 | decodedValue += signModifier; |
361 | finalValue = (decodedValue + 1) / 2; // inc by 1 and shift out sign bit | |
362 | if (decodedValue & 1) // the sign is stored in the low bit | |
363 | finalValue *= -1; | |
532a468a | 364 | |
064bd293 | 365 | outputBuffer[outputCount] = finalValue; |
532a468a | 366 | |
064bd293 | 367 | signModifier = 0; |
532a468a | 368 | |
064bd293 MB |
369 | // update history |
370 | history += (decodedValue * rice_historymult) - ((history * rice_historymult) >> 9); | |
532a468a | 371 | |
064bd293 MB |
372 | if (decodedValue > 0xFFFF) |
373 | history = 0xFFFF; | |
532a468a | 374 | |
064bd293 MB |
375 | // special case, for compressed blocks of 0 |
376 | if ((history < 128) && (outputCount + 1 < outputSize)) { | |
377 | int32_t blockSize; | |
532a468a | 378 | |
064bd293 | 379 | signModifier = 1; |
532a468a | 380 | |
064bd293 | 381 | k = count_leading_zeros(history) + ((history + 16) / 64) - 24; |
532a468a | 382 | |
064bd293 MB |
383 | // note: blockSize is always 16bit |
384 | blockSize = entropy_decode_value(alac, 16, k, rice_kmodifier_mask); | |
532a468a | 385 | |
064bd293 MB |
386 | // got blockSize 0s |
387 | if (blockSize > 0) { | |
388 | memset(&outputBuffer[outputCount + 1], 0, blockSize * sizeof(*outputBuffer)); | |
389 | outputCount += blockSize; | |
390 | } | |
532a468a | 391 | |
064bd293 MB |
392 | if (blockSize > 0xFFFF) |
393 | signModifier = 0; | |
532a468a | 394 | |
064bd293 | 395 | history = 0; |
532a468a | 396 | } |
064bd293 | 397 | } |
31cf954d PM |
398 | } |
399 | ||
400 | #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits)) | |
401 | ||
064bd293 MB |
402 | #define SIGN_ONLY(v) ((v < 0) ? (-1) : ((v > 0) ? (1) : (0))) |
403 | ||
404 | static void predictor_decompress_fir_adapt(int32_t *error_buffer, int32_t *buffer_out, | |
405 | int output_size, int readsamplesize, | |
406 | int16_t *predictor_coef_table, int predictor_coef_num, | |
407 | int predictor_quantitization) { | |
408 | int i; | |
409 | ||
410 | /* first sample always copies */ | |
411 | *buffer_out = *error_buffer; | |
412 | ||
413 | if (!predictor_coef_num) { | |
414 | if (output_size <= 1) | |
415 | return; | |
416 | memcpy(buffer_out + 1, error_buffer + 1, (output_size - 1) * 4); | |
417 | return; | |
418 | } | |
419 | ||
420 | if (predictor_coef_num == 0x1f) /* 11111 - max value of predictor_coef_num */ | |
421 | { /* second-best case scenario for fir decompression, | |
422 | * error describes a small difference from the previous sample only | |
423 | */ | |
424 | if (output_size <= 1) | |
425 | return; | |
426 | for (i = 0; i < output_size - 1; i++) { | |
427 | int32_t prev_value; | |
428 | int32_t error_value; | |
429 | ||
430 | prev_value = buffer_out[i]; | |
431 | error_value = error_buffer[i + 1]; | |
432 | buffer_out[i + 1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize); | |
31cf954d | 433 | } |
064bd293 MB |
434 | return; |
435 | } | |
31cf954d | 436 | |
064bd293 MB |
437 | /* read warm-up samples */ |
438 | if (predictor_coef_num > 0) { | |
439 | int i; | |
440 | for (i = 0; i < predictor_coef_num; i++) { | |
441 | int32_t val; | |
31cf954d | 442 | |
064bd293 | 443 | val = buffer_out[i] + error_buffer[i + 1]; |
31cf954d | 444 | |
064bd293 | 445 | val = SIGN_EXTENDED32(val, readsamplesize); |
31cf954d | 446 | |
064bd293 | 447 | buffer_out[i + 1] = val; |
31cf954d | 448 | } |
064bd293 | 449 | } |
31cf954d PM |
450 | |
451 | #if 0 | |
452 | /* 4 and 8 are very common cases (the only ones i've seen). these | |
453 | * should be unrolled and optimised | |
454 | */ | |
455 | if (predictor_coef_num == 4) | |
456 | { | |
457 | /* FIXME: optimised general case */ | |
458 | return; | |
459 | } | |
460 | ||
461 | if (predictor_coef_table == 8) | |
462 | { | |
463 | /* FIXME: optimised general case */ | |
464 | return; | |
465 | } | |
466 | #endif | |
467 | ||
064bd293 MB |
468 | /* general case */ |
469 | if (predictor_coef_num > 0) { | |
470 | for (i = predictor_coef_num + 1; i < output_size; i++) { | |
471 | int j; | |
472 | int sum = 0; | |
473 | int outval; | |
474 | int error_val = error_buffer[i]; | |
31cf954d | 475 | |
064bd293 MB |
476 | for (j = 0; j < predictor_coef_num; j++) { |
477 | sum += (buffer_out[predictor_coef_num - j] - buffer_out[0]) * predictor_coef_table[j]; | |
478 | } | |
31cf954d | 479 | |
064bd293 MB |
480 | outval = (1 << (predictor_quantitization - 1)) + sum; |
481 | outval = outval >> predictor_quantitization; | |
482 | outval = outval + buffer_out[0] + error_val; | |
483 | outval = SIGN_EXTENDED32(outval, readsamplesize); | |
31cf954d | 484 | |
064bd293 | 485 | buffer_out[predictor_coef_num + 1] = outval; |
31cf954d | 486 | |
064bd293 MB |
487 | if (error_val > 0) { |
488 | int predictor_num = predictor_coef_num - 1; | |
31cf954d | 489 | |
064bd293 MB |
490 | while (predictor_num >= 0 && error_val > 0) { |
491 | int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; | |
492 | int sign = SIGN_ONLY(val); | |
31cf954d | 493 | |
064bd293 | 494 | predictor_coef_table[predictor_num] -= sign; |
31cf954d | 495 | |
064bd293 | 496 | val *= sign; /* absolute value */ |
31cf954d | 497 | |
064bd293 | 498 | error_val -= ((val >> predictor_quantitization) * (predictor_coef_num - predictor_num)); |
31cf954d | 499 | |
064bd293 MB |
500 | predictor_num--; |
501 | } | |
502 | } else if (error_val < 0) { | |
503 | int predictor_num = predictor_coef_num - 1; | |
31cf954d | 504 | |
064bd293 MB |
505 | while (predictor_num >= 0 && error_val < 0) { |
506 | int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; | |
507 | int sign = -SIGN_ONLY(val); | |
31cf954d | 508 | |
064bd293 | 509 | predictor_coef_table[predictor_num] -= sign; |
31cf954d | 510 | |
064bd293 | 511 | val *= sign; /* neg value */ |
31cf954d | 512 | |
064bd293 | 513 | error_val -= ((val >> predictor_quantitization) * (predictor_coef_num - predictor_num)); |
31cf954d | 514 | |
064bd293 | 515 | predictor_num--; |
31cf954d | 516 | } |
064bd293 MB |
517 | } |
518 | ||
519 | buffer_out++; | |
31cf954d | 520 | } |
064bd293 | 521 | } |
31cf954d PM |
522 | } |
523 | ||
064bd293 MB |
524 | static void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b, int16_t *buffer_out, |
525 | int numchannels, int numsamples, uint8_t interlacing_shift, | |
526 | uint8_t interlacing_leftweight) { | |
527 | int i; | |
528 | if (numsamples <= 0) | |
529 | return; | |
530 | ||
531 | /* weighted interlacing */ | |
532 | if (interlacing_leftweight) { | |
533 | for (i = 0; i < numsamples; i++) { | |
534 | int32_t difference, midright; | |
535 | int16_t left; | |
536 | int16_t right; | |
537 | ||
538 | midright = buffer_a[i]; | |
539 | difference = buffer_b[i]; | |
540 | ||
541 | right = midright - ((difference * interlacing_leftweight) >> interlacing_shift); | |
542 | left = right + difference; | |
543 | ||
544 | /* output is always little endian */ | |
545 | if (host_bigendian) { | |
546 | _Swap16(left); | |
547 | _Swap16(right); | |
548 | } | |
549 | ||
550 | buffer_out[i * numchannels] = left; | |
551 | buffer_out[i * numchannels + 1] = right; | |
552 | } | |
31cf954d | 553 | |
064bd293 MB |
554 | return; |
555 | } | |
31cf954d | 556 | |
064bd293 MB |
557 | /* otherwise basic interlacing took place */ |
558 | for (i = 0; i < numsamples; i++) { | |
559 | int16_t left, right; | |
31cf954d | 560 | |
064bd293 MB |
561 | left = buffer_a[i]; |
562 | right = buffer_b[i]; | |
31cf954d | 563 | |
064bd293 MB |
564 | /* output is always little endian */ |
565 | if (host_bigendian) { | |
566 | _Swap16(left); | |
567 | _Swap16(right); | |
568 | } | |
31cf954d | 569 | |
064bd293 MB |
570 | buffer_out[i * numchannels] = left; |
571 | buffer_out[i * numchannels + 1] = right; | |
572 | } | |
573 | } | |
31cf954d | 574 | |
064bd293 MB |
575 | static void deinterlace_24(int32_t *buffer_a, int32_t *buffer_b, int uncompressed_bytes, |
576 | int32_t *uncompressed_bytes_buffer_a, | |
577 | int32_t *uncompressed_bytes_buffer_b, void *buffer_out, int numchannels, | |
578 | int numsamples, uint8_t interlacing_shift, | |
579 | uint8_t interlacing_leftweight) { | |
580 | int i; | |
581 | if (numsamples <= 0) | |
582 | return; | |
583 | ||
584 | /* weighted interlacing */ | |
585 | if (interlacing_leftweight) { | |
586 | for (i = 0; i < numsamples; i++) { | |
587 | int32_t difference, midright; | |
588 | int32_t left; | |
589 | int32_t right; | |
590 | ||
591 | midright = buffer_a[i]; | |
592 | difference = buffer_b[i]; | |
593 | ||
594 | right = midright - ((difference * interlacing_leftweight) >> interlacing_shift); | |
595 | left = right + difference; | |
596 | ||
597 | if (uncompressed_bytes) { | |
598 | uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8)); | |
599 | left <<= (uncompressed_bytes * 8); | |
600 | right <<= (uncompressed_bytes * 8); | |
601 | ||
602 | left |= uncompressed_bytes_buffer_a[i] & mask; | |
603 | right |= uncompressed_bytes_buffer_b[i] & mask; | |
604 | } | |
605 | ||
606 | ((uint8_t *)buffer_out)[i * numchannels * 3] = (left)&0xFF; | |
607 | ((uint8_t *)buffer_out)[i * numchannels * 3 + 1] = (left >> 8) & 0xFF; | |
608 | ((uint8_t *)buffer_out)[i * numchannels * 3 + 2] = (left >> 16) & 0xFF; | |
609 | ||
610 | ((uint8_t *)buffer_out)[i * numchannels * 3 + 3] = (right)&0xFF; | |
611 | ((uint8_t *)buffer_out)[i * numchannels * 3 + 4] = (right >> 8) & 0xFF; | |
612 | ((uint8_t *)buffer_out)[i * numchannels * 3 + 5] = (right >> 16) & 0xFF; | |
31cf954d PM |
613 | } |
614 | ||
064bd293 MB |
615 | return; |
616 | } | |
31cf954d | 617 | |
064bd293 MB |
618 | /* otherwise basic interlacing took place */ |
619 | for (i = 0; i < numsamples; i++) { | |
620 | int32_t left, right; | |
31cf954d | 621 | |
064bd293 MB |
622 | left = buffer_a[i]; |
623 | right = buffer_b[i]; | |
624 | ||
625 | if (uncompressed_bytes) { | |
626 | uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8)); | |
627 | left <<= (uncompressed_bytes * 8); | |
628 | right <<= (uncompressed_bytes * 8); | |
31cf954d | 629 | |
064bd293 MB |
630 | left |= uncompressed_bytes_buffer_a[i] & mask; |
631 | right |= uncompressed_bytes_buffer_b[i] & mask; | |
31cf954d | 632 | } |
31cf954d | 633 | |
064bd293 MB |
634 | ((uint8_t *)buffer_out)[i * numchannels * 3] = (left)&0xFF; |
635 | ((uint8_t *)buffer_out)[i * numchannels * 3 + 1] = (left >> 8) & 0xFF; | |
636 | ((uint8_t *)buffer_out)[i * numchannels * 3 + 2] = (left >> 16) & 0xFF; | |
532a468a | 637 | |
064bd293 MB |
638 | ((uint8_t *)buffer_out)[i * numchannels * 3 + 3] = (right)&0xFF; |
639 | ((uint8_t *)buffer_out)[i * numchannels * 3 + 4] = (right >> 8) & 0xFF; | |
640 | ((uint8_t *)buffer_out)[i * numchannels * 3 + 5] = (right >> 16) & 0xFF; | |
641 | } | |
642 | } | |
532a468a | 643 | |
064bd293 MB |
644 | void alac_decode_frame(alac_file *alac, unsigned char *inbuffer, void *outbuffer, int *outputsize) { |
645 | int outbuffer_allocation_size = *outputsize; // initial value | |
646 | int channels; | |
647 | int32_t outputsamples = alac->setinfo_max_samples_per_frame; | |
532a468a | 648 | |
064bd293 MB |
649 | /* setup the stream */ |
650 | alac->input_buffer = inbuffer; | |
651 | alac->input_buffer_bitaccumulator = 0; | |
532a468a | 652 | |
064bd293 | 653 | channels = readbits(alac, 3); |
532a468a | 654 | |
064bd293 MB |
655 | *outputsize = outputsamples * alac->bytespersample; |
656 | if (*outputsize > outbuffer_allocation_size) { | |
657 | fprintf(stderr, "FIXME: Not enough space if the output buffer for audio frame - E1.\n"); | |
658 | *outputsize = 0; | |
659 | return; | |
660 | } | |
532a468a | 661 | |
064bd293 MB |
662 | switch (channels) { |
663 | case 0: /* 1 channel */ | |
664 | { | |
665 | int hassize; | |
666 | int isnotcompressed; | |
667 | int readsamplesize; | |
532a468a | 668 | |
064bd293 MB |
669 | int uncompressed_bytes; |
670 | int ricemodifier; | |
532a468a | 671 | |
064bd293 MB |
672 | /* 2^result = something to do with output waiting. |
673 | * perhaps matters if we read > 1 frame in a pass? | |
674 | */ | |
675 | readbits(alac, 4); | |
532a468a | 676 | |
064bd293 | 677 | readbits(alac, 12); /* unknown, skip 12 bits */ |
532a468a | 678 | |
064bd293 | 679 | hassize = readbits(alac, 1); /* the output sample size is stored soon */ |
31cf954d | 680 | |
064bd293 MB |
681 | uncompressed_bytes = |
682 | readbits(alac, 2); /* number of bytes in the (compressed) stream that are not compressed */ | |
31cf954d | 683 | |
064bd293 | 684 | isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */ |
31cf954d | 685 | |
064bd293 MB |
686 | if (hassize) { |
687 | /* now read the number of samples, | |
688 | * as a 32bit integer */ | |
689 | outputsamples = readbits(alac, 32); | |
690 | *outputsize = outputsamples * alac->bytespersample; | |
691 | if (*outputsize > outbuffer_allocation_size) { | |
692 | fprintf(stderr, "FIXME: Not enough space if the output buffer for audio frame - E2.\n"); | |
693 | *outputsize = 0; | |
694 | return; | |
695 | } | |
696 | } | |
31cf954d | 697 | |
064bd293 | 698 | readsamplesize = alac->setinfo_sample_size - (uncompressed_bytes * 8); |
31cf954d | 699 | |
064bd293 MB |
700 | if (!isnotcompressed) { /* so it is compressed */ |
701 | int16_t predictor_coef_table[32]; | |
702 | int predictor_coef_num; | |
703 | int prediction_type; | |
704 | int prediction_quantitization; | |
705 | int i; | |
31cf954d | 706 | |
064bd293 MB |
707 | /* skip 16 bits, not sure what they are. seem to be used in |
708 | * two channel case */ | |
709 | readbits(alac, 8); | |
710 | readbits(alac, 8); | |
31cf954d | 711 | |
064bd293 MB |
712 | prediction_type = readbits(alac, 4); |
713 | prediction_quantitization = readbits(alac, 4); | |
31cf954d | 714 | |
064bd293 MB |
715 | ricemodifier = readbits(alac, 3); |
716 | predictor_coef_num = readbits(alac, 5); | |
31cf954d | 717 | |
064bd293 MB |
718 | /* read the predictor table */ |
719 | for (i = 0; i < predictor_coef_num; i++) { | |
720 | predictor_coef_table[i] = (int16_t)readbits(alac, 16); | |
721 | } | |
31cf954d | 722 | |
064bd293 MB |
723 | if (uncompressed_bytes) { |
724 | int i; | |
725 | for (i = 0; i < outputsamples; i++) { | |
726 | alac->uncompressed_bytes_buffer_a[i] = readbits(alac, uncompressed_bytes * 8); | |
727 | } | |
728 | } | |
729 | ||
730 | entropy_rice_decode(alac, alac->predicterror_buffer_a, outputsamples, readsamplesize, | |
731 | alac->setinfo_rice_initialhistory, alac->setinfo_rice_kmodifier, | |
732 | ricemodifier * alac->setinfo_rice_historymult / 4, | |
733 | (1 << alac->setinfo_rice_kmodifier) - 1); | |
734 | ||
735 | if (prediction_type == 0) { /* adaptive fir */ | |
736 | predictor_decompress_fir_adapt(alac->predicterror_buffer_a, alac->outputsamples_buffer_a, | |
737 | outputsamples, readsamplesize, predictor_coef_table, | |
738 | predictor_coef_num, prediction_quantitization); | |
739 | } else { | |
740 | fprintf(stderr, "FIXME: unhandled prediction type for compressed case: %i\n", | |
741 | prediction_type); | |
742 | /* i think the only other prediction type (or perhaps this is just a | |
743 | * boolean?) runs adaptive fir twice.. like: | |
744 | * predictor_decompress_fir_adapt(predictor_error, tempout, ...) | |
745 | * predictor_decompress_fir_adapt(predictor_error, outputsamples ...) | |
746 | * little strange.. | |
747 | */ | |
748 | } | |
31cf954d | 749 | |
064bd293 MB |
750 | } else { /* not compressed, easy case */ |
751 | if (alac->setinfo_sample_size <= 16) { | |
752 | int i; | |
753 | for (i = 0; i < outputsamples; i++) { | |
754 | int32_t audiobits = readbits(alac, alac->setinfo_sample_size); | |
31cf954d | 755 | |
064bd293 | 756 | audiobits = SIGN_EXTENDED32(audiobits, alac->setinfo_sample_size); |
6c485fa5 | 757 | |
064bd293 | 758 | alac->outputsamples_buffer_a[i] = audiobits; |
31cf954d | 759 | } |
064bd293 MB |
760 | } else { |
761 | int i; | |
762 | for (i = 0; i < outputsamples; i++) { | |
763 | int32_t audiobits; | |
31cf954d | 764 | |
064bd293 MB |
765 | audiobits = readbits(alac, 16); |
766 | /* special case of sign extension.. | |
767 | * as we'll be ORing the low 16bits into this */ | |
768 | audiobits = audiobits << (alac->setinfo_sample_size - 16); | |
769 | audiobits |= readbits(alac, alac->setinfo_sample_size - 16); | |
770 | audiobits = SignExtend24(audiobits); | |
31cf954d | 771 | |
064bd293 | 772 | alac->outputsamples_buffer_a[i] = audiobits; |
31cf954d | 773 | } |
064bd293 MB |
774 | } |
775 | uncompressed_bytes = 0; // always 0 for uncompressed | |
776 | } | |
31cf954d | 777 | |
064bd293 MB |
778 | switch (alac->setinfo_sample_size) { |
779 | case 16: { | |
780 | int i; | |
781 | for (i = 0; i < outputsamples; i++) { | |
782 | int16_t sample = alac->outputsamples_buffer_a[i]; | |
783 | if (host_bigendian) | |
784 | _Swap16(sample); | |
785 | ((int16_t *)outbuffer)[i * alac->numchannels] = sample; | |
786 | } | |
787 | break; | |
788 | } | |
789 | case 24: { | |
790 | int i; | |
791 | for (i = 0; i < outputsamples; i++) { | |
792 | int32_t sample = alac->outputsamples_buffer_a[i]; | |
793 | ||
794 | if (uncompressed_bytes) { | |
795 | uint32_t mask; | |
796 | sample = sample << (uncompressed_bytes * 8); | |
797 | mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8)); | |
798 | sample |= alac->uncompressed_bytes_buffer_a[i] & mask; | |
31cf954d | 799 | } |
064bd293 MB |
800 | |
801 | ((uint8_t *)outbuffer)[i * alac->numchannels * 3] = (sample)&0xFF; | |
802 | ((uint8_t *)outbuffer)[i * alac->numchannels * 3 + 1] = (sample >> 8) & 0xFF; | |
803 | ((uint8_t *)outbuffer)[i * alac->numchannels * 3 + 2] = (sample >> 16) & 0xFF; | |
804 | } | |
805 | break; | |
31cf954d | 806 | } |
064bd293 MB |
807 | case 20: |
808 | case 32: | |
809 | fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size); | |
810 | break; | |
811 | default: | |
812 | break; | |
813 | } | |
814 | break; | |
815 | } | |
816 | case 1: /* 2 channels */ | |
817 | { | |
818 | int hassize; | |
819 | int isnotcompressed; | |
820 | int readsamplesize; | |
31cf954d | 821 | |
064bd293 | 822 | int uncompressed_bytes; |
31cf954d | 823 | |
064bd293 MB |
824 | uint8_t interlacing_shift; |
825 | uint8_t interlacing_leftweight; | |
31cf954d | 826 | |
064bd293 MB |
827 | /* 2^result = something to do with output waiting. |
828 | * perhaps matters if we read > 1 frame in a pass? | |
829 | */ | |
830 | readbits(alac, 4); | |
31cf954d | 831 | |
064bd293 | 832 | readbits(alac, 12); /* unknown, skip 12 bits */ |
31cf954d | 833 | |
064bd293 | 834 | hassize = readbits(alac, 1); /* the output sample size is stored soon */ |
31cf954d | 835 | |
064bd293 MB |
836 | uncompressed_bytes = readbits( |
837 | alac, 2); /* the number of bytes in the (compressed) stream that are not compressed */ | |
31cf954d | 838 | |
064bd293 | 839 | isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */ |
31cf954d | 840 | |
064bd293 MB |
841 | if (hassize) { |
842 | /* now read the number of samples, | |
843 | * as a 32bit integer */ | |
844 | outputsamples = readbits(alac, 32); | |
845 | *outputsize = outputsamples * alac->bytespersample; | |
846 | if (*outputsize > outbuffer_allocation_size) { | |
847 | fprintf(stderr, "FIXME: Not enough space if the output buffer for audio frame - E3.\n"); | |
848 | *outputsize = 0; | |
849 | return; | |
850 | } | |
851 | } | |
31cf954d | 852 | |
064bd293 MB |
853 | readsamplesize = alac->setinfo_sample_size - (uncompressed_bytes * 8) + 1; |
854 | ||
855 | if (!isnotcompressed) { /* compressed */ | |
856 | int16_t predictor_coef_table_a[32]; | |
857 | int predictor_coef_num_a; | |
858 | int prediction_type_a; | |
859 | int prediction_quantitization_a; | |
860 | int ricemodifier_a; | |
861 | ||
862 | int16_t predictor_coef_table_b[32]; | |
863 | int predictor_coef_num_b; | |
864 | int prediction_type_b; | |
865 | int prediction_quantitization_b; | |
866 | int ricemodifier_b; | |
867 | ||
868 | int i; | |
869 | ||
870 | interlacing_shift = readbits(alac, 8); | |
871 | interlacing_leftweight = readbits(alac, 8); | |
872 | ||
873 | /******** channel 1 ***********/ | |
874 | prediction_type_a = readbits(alac, 4); | |
875 | prediction_quantitization_a = readbits(alac, 4); | |
876 | ||
877 | ricemodifier_a = readbits(alac, 3); | |
878 | predictor_coef_num_a = readbits(alac, 5); | |
879 | ||
880 | /* read the predictor table */ | |
881 | for (i = 0; i < predictor_coef_num_a; i++) { | |
882 | predictor_coef_table_a[i] = (int16_t)readbits(alac, 16); | |
883 | } | |
884 | ||
885 | /******** channel 2 *********/ | |
886 | prediction_type_b = readbits(alac, 4); | |
887 | prediction_quantitization_b = readbits(alac, 4); | |
31cf954d | 888 | |
064bd293 MB |
889 | ricemodifier_b = readbits(alac, 3); |
890 | predictor_coef_num_b = readbits(alac, 5); | |
891 | ||
892 | /* read the predictor table */ | |
893 | for (i = 0; i < predictor_coef_num_b; i++) { | |
894 | predictor_coef_table_b[i] = (int16_t)readbits(alac, 16); | |
895 | } | |
896 | ||
897 | /*********************/ | |
898 | if (uncompressed_bytes) { /* see mono case */ | |
899 | int i; | |
900 | for (i = 0; i < outputsamples; i++) { | |
901 | alac->uncompressed_bytes_buffer_a[i] = readbits(alac, uncompressed_bytes * 8); | |
902 | alac->uncompressed_bytes_buffer_b[i] = readbits(alac, uncompressed_bytes * 8); | |
31cf954d | 903 | } |
064bd293 MB |
904 | } |
905 | ||
906 | /* channel 1 */ | |
907 | entropy_rice_decode(alac, alac->predicterror_buffer_a, outputsamples, readsamplesize, | |
908 | alac->setinfo_rice_initialhistory, alac->setinfo_rice_kmodifier, | |
909 | ricemodifier_a * alac->setinfo_rice_historymult / 4, | |
910 | (1 << alac->setinfo_rice_kmodifier) - 1); | |
911 | ||
912 | if (prediction_type_a == 0) { /* adaptive fir */ | |
913 | predictor_decompress_fir_adapt(alac->predicterror_buffer_a, alac->outputsamples_buffer_a, | |
914 | outputsamples, readsamplesize, predictor_coef_table_a, | |
915 | predictor_coef_num_a, prediction_quantitization_a); | |
916 | } else { /* see mono case */ | |
917 | fprintf(stderr, "FIXME: unhandled prediction type on channel 1: %i\n", prediction_type_a); | |
918 | } | |
919 | ||
920 | /* channel 2 */ | |
921 | entropy_rice_decode(alac, alac->predicterror_buffer_b, outputsamples, readsamplesize, | |
922 | alac->setinfo_rice_initialhistory, alac->setinfo_rice_kmodifier, | |
923 | ricemodifier_b * alac->setinfo_rice_historymult / 4, | |
924 | (1 << alac->setinfo_rice_kmodifier) - 1); | |
925 | ||
926 | if (prediction_type_b == 0) { /* adaptive fir */ | |
927 | predictor_decompress_fir_adapt(alac->predicterror_buffer_b, alac->outputsamples_buffer_b, | |
928 | outputsamples, readsamplesize, predictor_coef_table_b, | |
929 | predictor_coef_num_b, prediction_quantitization_b); | |
930 | } else { | |
931 | fprintf(stderr, "FIXME: unhandled prediction type on channel 2: %i\n", prediction_type_b); | |
932 | } | |
933 | } else { /* not compressed, easy case */ | |
934 | if (alac->setinfo_sample_size <= 16) { | |
935 | int i; | |
936 | for (i = 0; i < outputsamples; i++) { | |
937 | int32_t audiobits_a, audiobits_b; | |
938 | ||
939 | audiobits_a = readbits(alac, alac->setinfo_sample_size); | |
940 | audiobits_b = readbits(alac, alac->setinfo_sample_size); | |
941 | ||
942 | audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size); | |
943 | audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size); | |
944 | ||
945 | alac->outputsamples_buffer_a[i] = audiobits_a; | |
946 | alac->outputsamples_buffer_b[i] = audiobits_b; | |
532a468a | 947 | } |
064bd293 MB |
948 | } else { |
949 | int i; | |
950 | for (i = 0; i < outputsamples; i++) { | |
951 | int32_t audiobits_a, audiobits_b; | |
952 | ||
953 | audiobits_a = readbits(alac, 16); | |
954 | audiobits_a = audiobits_a << (alac->setinfo_sample_size - 16); | |
955 | audiobits_a |= readbits(alac, alac->setinfo_sample_size - 16); | |
956 | audiobits_a = SignExtend24(audiobits_a); | |
957 | ||
958 | audiobits_b = readbits(alac, 16); | |
959 | audiobits_b = audiobits_b << (alac->setinfo_sample_size - 16); | |
960 | audiobits_b |= readbits(alac, alac->setinfo_sample_size - 16); | |
961 | audiobits_b = SignExtend24(audiobits_b); | |
962 | ||
963 | alac->outputsamples_buffer_a[i] = audiobits_a; | |
964 | alac->outputsamples_buffer_b[i] = audiobits_b; | |
31cf954d | 965 | } |
064bd293 MB |
966 | } |
967 | uncompressed_bytes = 0; // always 0 for uncompressed | |
968 | interlacing_shift = 0; | |
969 | interlacing_leftweight = 0; | |
970 | } | |
31cf954d | 971 | |
064bd293 MB |
972 | switch (alac->setinfo_sample_size) { |
973 | case 16: { | |
974 | deinterlace_16(alac->outputsamples_buffer_a, alac->outputsamples_buffer_b, | |
975 | (int16_t *)outbuffer, alac->numchannels, outputsamples, interlacing_shift, | |
976 | interlacing_leftweight); | |
977 | break; | |
31cf954d | 978 | } |
064bd293 MB |
979 | case 24: { |
980 | deinterlace_24(alac->outputsamples_buffer_a, alac->outputsamples_buffer_b, uncompressed_bytes, | |
981 | alac->uncompressed_bytes_buffer_a, alac->uncompressed_bytes_buffer_b, | |
982 | (int16_t *)outbuffer, alac->numchannels, outputsamples, interlacing_shift, | |
983 | interlacing_leftweight); | |
984 | break; | |
31cf954d | 985 | } |
064bd293 MB |
986 | case 20: |
987 | case 32: | |
988 | fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size); | |
989 | break; | |
990 | default: | |
991 | break; | |
992 | } | |
993 | ||
994 | break; | |
995 | } | |
996 | } | |
31cf954d PM |
997 | } |
998 | ||
064bd293 MB |
999 | alac_file *alac_create(int samplesize, int numchannels) { |
1000 | alac_file *newfile = malloc(sizeof(alac_file)); | |
31a09bb3 MB |
1001 | if (newfile) { |
1002 | memset(newfile, 0, sizeof(alac_file)); | |
1003 | newfile->samplesize = samplesize; | |
1004 | newfile->numchannels = numchannels; | |
1005 | newfile->bytespersample = (samplesize / 8) * numchannels; | |
1006 | } else { | |
3001f39b | 1007 | fprintf(stderr, "FIXME: can not allocate memory for a new file in alac_cxreate."); |
31a09bb3 | 1008 | } |
064bd293 | 1009 | return newfile; |
31cf954d | 1010 | } |