alac.c 35 KB

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