You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

938 lines
30KB

  1. /*
  2. * ALAC (Apple Lossless Audio Codec) decoder
  3. * Copyright (c) 2005 David Hammerton
  4. * All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * @file alac.c
  22. * ALAC (Apple Lossless Audio Codec) decoder
  23. * @author 2005 David Hammerton
  24. *
  25. * For more information on the ALAC format, visit:
  26. * http://crazney.net/programs/itunes/alac.html
  27. *
  28. * Note: This decoder expects a 36- (0x24-)byte QuickTime atom to be
  29. * passed through the extradata[_size] fields. This atom is tacked onto
  30. * the end of an 'alac' stsd atom and has the following format:
  31. * bytes 0-3 atom size (0x24), big-endian
  32. * bytes 4-7 atom type ('alac', not the 'alac' tag from start of stsd)
  33. * bytes 8-35 data bytes needed by decoder
  34. */
  35. #include "avcodec.h"
  36. #define ALAC_EXTRADATA_SIZE 36
  37. struct alac_file {
  38. unsigned char *input_buffer;
  39. int input_buffer_index;
  40. int input_buffer_size;
  41. int input_buffer_bitaccumulator; /* used so we can do arbitary
  42. bit reads */
  43. int samplesize;
  44. int numchannels;
  45. int bytespersample;
  46. /* buffers */
  47. int32_t *predicterror_buffer_a;
  48. int32_t *predicterror_buffer_b;
  49. int32_t *outputsamples_buffer_a;
  50. int32_t *outputsamples_buffer_b;
  51. /* stuff from setinfo */
  52. uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */
  53. uint8_t setinfo_7a; /* 0x00 */
  54. uint8_t setinfo_sample_size; /* 0x10 */
  55. uint8_t setinfo_rice_historymult; /* 0x28 */
  56. uint8_t setinfo_rice_initialhistory; /* 0x0a */
  57. uint8_t setinfo_rice_kmodifier; /* 0x0e */
  58. uint8_t setinfo_7f; /* 0x02 */
  59. uint16_t setinfo_80; /* 0x00ff */
  60. uint32_t setinfo_82; /* 0x000020e7 */
  61. uint32_t setinfo_86; /* 0x00069fe4 */
  62. uint32_t setinfo_8a_rate; /* 0x0000ac44 */
  63. /* end setinfo stuff */
  64. };
  65. typedef struct alac_file alac_file;
  66. typedef struct {
  67. AVCodecContext *avctx;
  68. /* init to 0; first frame decode should initialize from extradata and
  69. * set this to 1 */
  70. int context_initialized;
  71. alac_file *alac;
  72. } ALACContext;
  73. static void allocate_buffers(alac_file *alac)
  74. {
  75. alac->predicterror_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
  76. alac->predicterror_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
  77. alac->outputsamples_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
  78. alac->outputsamples_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
  79. }
  80. void alac_set_info(alac_file *alac, char *inputbuffer)
  81. {
  82. unsigned char *ptr = inputbuffer;
  83. ptr += 4; /* size */
  84. ptr += 4; /* alac */
  85. ptr += 4; /* 0 ? */
  86. alac->setinfo_max_samples_per_frame = BE_32(ptr); /* buffer size / 2 ? */
  87. ptr += 4;
  88. alac->setinfo_7a = *ptr++;
  89. alac->setinfo_sample_size = *ptr++;
  90. alac->setinfo_rice_historymult = *ptr++;
  91. alac->setinfo_rice_initialhistory = *ptr++;
  92. alac->setinfo_rice_kmodifier = *ptr++;
  93. alac->setinfo_7f = *ptr++;
  94. alac->setinfo_80 = BE_16(ptr);
  95. ptr += 2;
  96. alac->setinfo_82 = BE_32(ptr);
  97. ptr += 4;
  98. alac->setinfo_86 = BE_32(ptr);
  99. ptr += 4;
  100. alac->setinfo_8a_rate = BE_32(ptr);
  101. ptr += 4;
  102. allocate_buffers(alac);
  103. }
  104. /* stream reading */
  105. /* supports reading 1 to 16 bits, in big endian format */
  106. static uint32_t readbits_16(alac_file *alac, int bits)
  107. {
  108. uint32_t result;
  109. int new_accumulator;
  110. if (alac->input_buffer_index + 2 >= alac->input_buffer_size) {
  111. av_log(NULL, AV_LOG_ERROR, "alac: input buffer went out of bounds (%d >= %d)\n",
  112. alac->input_buffer_index + 2, alac->input_buffer_size);
  113. }
  114. result = (alac->input_buffer[alac->input_buffer_index + 0] << 16) |
  115. (alac->input_buffer[alac->input_buffer_index + 1] << 8) |
  116. (alac->input_buffer[alac->input_buffer_index + 2]);
  117. /* shift left by the number of bits we've already read,
  118. * so that the top 'n' bits of the 24 bits we read will
  119. * be the return bits */
  120. result = result << alac->input_buffer_bitaccumulator;
  121. result = result & 0x00ffffff;
  122. /* and then only want the top 'n' bits from that, where
  123. * n is 'bits' */
  124. result = result >> (24 - bits);
  125. new_accumulator = (alac->input_buffer_bitaccumulator + bits);
  126. /* increase the buffer pointer if we've read over n bytes. */
  127. alac->input_buffer_index += (new_accumulator >> 3);
  128. /* and the remainder goes back into the bit accumulator */
  129. alac->input_buffer_bitaccumulator = (new_accumulator & 7);
  130. return result;
  131. }
  132. /* supports reading 1 to 32 bits, in big endian format */
  133. static uint32_t readbits(alac_file *alac, int bits)
  134. {
  135. int32_t result = 0;
  136. if (bits > 16) {
  137. bits -= 16;
  138. result = readbits_16(alac, 16) << bits;
  139. }
  140. result |= readbits_16(alac, bits);
  141. return result;
  142. }
  143. /* reads a single bit */
  144. static int readbit(alac_file *alac)
  145. {
  146. int result;
  147. int new_accumulator;
  148. if (alac->input_buffer_index >= alac->input_buffer_size) {
  149. av_log(NULL, AV_LOG_ERROR, "alac: input buffer went out of bounds (%d >= %d)\n",
  150. alac->input_buffer_index + 2, alac->input_buffer_size);
  151. }
  152. result = alac->input_buffer[alac->input_buffer_index];
  153. result = result << alac->input_buffer_bitaccumulator;
  154. result = result >> 7 & 1;
  155. new_accumulator = (alac->input_buffer_bitaccumulator + 1);
  156. alac->input_buffer_index += (new_accumulator / 8);
  157. alac->input_buffer_bitaccumulator = (new_accumulator % 8);
  158. return result;
  159. }
  160. static void unreadbits(alac_file *alac, int bits)
  161. {
  162. int new_accumulator = (alac->input_buffer_bitaccumulator - bits);
  163. alac->input_buffer_index += (new_accumulator >> 3);
  164. alac->input_buffer_bitaccumulator = (new_accumulator & 7);
  165. if (alac->input_buffer_bitaccumulator < 0)
  166. alac->input_buffer_bitaccumulator *= -1;
  167. }
  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. i++;
  176. input = input << 1;
  177. }
  178. return i;
  179. }
  180. void bastardized_rice_decompress(alac_file *alac,
  181. int32_t *output_buffer,
  182. int output_size,
  183. int readsamplesize, /* arg_10 */
  184. int rice_initialhistory, /* arg424->b */
  185. int rice_kmodifier, /* arg424->d */
  186. int rice_historymult, /* arg424->c */
  187. int rice_kmodifier_mask /* arg424->e */
  188. )
  189. {
  190. int output_count;
  191. unsigned int history = rice_initialhistory;
  192. int sign_modifier = 0;
  193. for (output_count = 0; output_count < output_size; output_count++) {
  194. int32_t x = 0;
  195. int32_t x_modified;
  196. int32_t final_val;
  197. /* read x - number of 1s before 0 represent the rice */
  198. while (x <= 8 && readbit(alac)) {
  199. x++;
  200. }
  201. if (x > 8) { /* RICE THRESHOLD */
  202. /* use alternative encoding */
  203. int32_t value;
  204. value = readbits(alac, readsamplesize);
  205. /* mask value to readsamplesize size */
  206. if (readsamplesize != 32)
  207. value &= (0xffffffff >> (32 - readsamplesize));
  208. x = value;
  209. } else {
  210. /* standard rice encoding */
  211. int extrabits;
  212. int k; /* size of extra bits */
  213. /* read k, that is bits as is */
  214. k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
  215. if (k < 0)
  216. k += rice_kmodifier;
  217. else
  218. k = rice_kmodifier;
  219. if (k != 1) {
  220. extrabits = readbits(alac, k);
  221. /* multiply x by 2^k - 1, as part of their strange algorithm */
  222. x = (x << k) - x;
  223. if (extrabits > 1) {
  224. x += extrabits - 1;
  225. } else
  226. unreadbits(alac, 1);
  227. }
  228. }
  229. x_modified = sign_modifier + x;
  230. final_val = (x_modified + 1) / 2;
  231. if (x_modified & 1) final_val *= -1;
  232. output_buffer[output_count] = final_val;
  233. sign_modifier = 0;
  234. /* now update the history */
  235. history += (x_modified * rice_historymult)
  236. - ((history * rice_historymult) >> 9);
  237. if (x_modified > 0xffff)
  238. history = 0xffff;
  239. /* special case: there may be compressed blocks of 0 */
  240. if ((history < 128) && (output_count+1 < output_size)) {
  241. int block_size;
  242. sign_modifier = 1;
  243. x = 0;
  244. while (x <= 8 && readbit(alac)) {
  245. x++;
  246. }
  247. if (x > 8) {
  248. block_size = readbits(alac, 16);
  249. block_size &= 0xffff;
  250. } else {
  251. int k;
  252. int extrabits;
  253. k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
  254. extrabits = readbits(alac, k);
  255. block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
  256. + extrabits - 1;
  257. if (extrabits < 2) {
  258. x = 1 - extrabits;
  259. block_size += x;
  260. unreadbits(alac, 1);
  261. }
  262. }
  263. if (block_size > 0) {
  264. memset(&output_buffer[output_count+1], 0, block_size * 4);
  265. output_count += block_size;
  266. }
  267. if (block_size > 0xffff)
  268. sign_modifier = 0;
  269. history = 0;
  270. }
  271. }
  272. }
  273. #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
  274. #define SIGN_ONLY(v) \
  275. ((v < 0) ? (-1) : \
  276. ((v > 0) ? (1) : \
  277. (0)))
  278. static void predictor_decompress_fir_adapt(int32_t *error_buffer,
  279. int32_t *buffer_out,
  280. int output_size,
  281. int readsamplesize,
  282. int16_t *predictor_coef_table,
  283. int predictor_coef_num,
  284. int predictor_quantitization)
  285. {
  286. int i;
  287. /* first sample always copies */
  288. *buffer_out = *error_buffer;
  289. if (!predictor_coef_num) {
  290. if (output_size <= 1) return;
  291. memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
  292. return;
  293. }
  294. if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
  295. /* second-best case scenario for fir decompression,
  296. * error describes a small difference from the previous sample only
  297. */
  298. if (output_size <= 1) return;
  299. for (i = 0; i < output_size - 1; i++) {
  300. int32_t prev_value;
  301. int32_t error_value;
  302. prev_value = buffer_out[i];
  303. error_value = error_buffer[i+1];
  304. buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
  305. }
  306. return;
  307. }
  308. /* read warm-up samples */
  309. if (predictor_coef_num > 0) {
  310. int i;
  311. for (i = 0; i < predictor_coef_num; i++) {
  312. int32_t val;
  313. val = buffer_out[i] + error_buffer[i+1];
  314. val = SIGN_EXTENDED32(val, readsamplesize);
  315. buffer_out[i+1] = val;
  316. }
  317. }
  318. #if 0
  319. /* 4 and 8 are very common cases (the only ones i've seen). these
  320. * should be unrolled and optimised
  321. */
  322. if (predictor_coef_num == 4) {
  323. /* FIXME: optimised general case */
  324. return;
  325. }
  326. if (predictor_coef_table == 8) {
  327. /* FIXME: optimised general case */
  328. return;
  329. }
  330. #endif
  331. /* general case */
  332. if (predictor_coef_num > 0) {
  333. for (i = predictor_coef_num + 1;
  334. i < output_size;
  335. i++) {
  336. int j;
  337. int sum = 0;
  338. int outval;
  339. int error_val = error_buffer[i];
  340. for (j = 0; j < predictor_coef_num; j++) {
  341. sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
  342. predictor_coef_table[j];
  343. }
  344. outval = (1 << (predictor_quantitization-1)) + sum;
  345. outval = outval >> predictor_quantitization;
  346. outval = outval + buffer_out[0] + error_val;
  347. outval = SIGN_EXTENDED32(outval, readsamplesize);
  348. buffer_out[predictor_coef_num+1] = outval;
  349. if (error_val > 0) {
  350. int predictor_num = predictor_coef_num - 1;
  351. while (predictor_num >= 0 && error_val > 0) {
  352. int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
  353. int sign = SIGN_ONLY(val);
  354. predictor_coef_table[predictor_num] -= sign;
  355. val *= sign; /* absolute value */
  356. error_val -= ((val >> predictor_quantitization) *
  357. (predictor_coef_num - predictor_num));
  358. predictor_num--;
  359. }
  360. } else if (error_val < 0) {
  361. int predictor_num = predictor_coef_num - 1;
  362. while (predictor_num >= 0 && error_val < 0) {
  363. int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
  364. int sign = - SIGN_ONLY(val);
  365. predictor_coef_table[predictor_num] -= sign;
  366. val *= sign; /* neg value */
  367. error_val -= ((val >> predictor_quantitization) *
  368. (predictor_coef_num - predictor_num));
  369. predictor_num--;
  370. }
  371. }
  372. buffer_out++;
  373. }
  374. }
  375. }
  376. void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
  377. int16_t *buffer_out,
  378. int numchannels, int numsamples,
  379. uint8_t interlacing_shift,
  380. uint8_t interlacing_leftweight)
  381. {
  382. int i;
  383. if (numsamples <= 0) return;
  384. /* weighted interlacing */
  385. if (interlacing_leftweight) {
  386. for (i = 0; i < numsamples; i++) {
  387. int32_t difference, midright;
  388. int16_t left;
  389. int16_t right;
  390. midright = buffer_a[i];
  391. difference = buffer_b[i];
  392. right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
  393. left = (midright - ((difference * interlacing_leftweight) >> interlacing_shift))
  394. + difference;
  395. buffer_out[i*numchannels] = left;
  396. buffer_out[i*numchannels + 1] = right;
  397. }
  398. return;
  399. }
  400. /* otherwise basic interlacing took place */
  401. for (i = 0; i < numsamples; i++) {
  402. int16_t left, right;
  403. left = buffer_a[i];
  404. right = buffer_b[i];
  405. buffer_out[i*numchannels] = left;
  406. buffer_out[i*numchannels + 1] = right;
  407. }
  408. }
  409. static int alac_decode_frame(AVCodecContext *avctx,
  410. void *outbuffer, int *outputsize,
  411. uint8_t *inbuffer, int input_buffer_size)
  412. {
  413. ALACContext *s = avctx->priv_data;
  414. alac_file *alac = s->alac;
  415. int channels;
  416. int32_t outputsamples;
  417. /* short-circuit null buffers */
  418. if (!inbuffer || !input_buffer_size)
  419. return input_buffer_size;
  420. /* initialize from the extradata */
  421. if (!s->context_initialized) {
  422. if (s->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
  423. av_log(NULL, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
  424. ALAC_EXTRADATA_SIZE);
  425. return input_buffer_size;
  426. }
  427. alac_set_info(s->alac, s->avctx->extradata);
  428. s->context_initialized = 1;
  429. }
  430. outputsamples = alac->setinfo_max_samples_per_frame;
  431. /* setup the stream */
  432. alac->input_buffer = inbuffer;
  433. alac->input_buffer_index = 0;
  434. alac->input_buffer_size = input_buffer_size;
  435. alac->input_buffer_bitaccumulator = 0;
  436. channels = readbits(alac, 3);
  437. *outputsize = outputsamples * alac->bytespersample;
  438. switch(channels) {
  439. case 0: { /* 1 channel */
  440. int hassize;
  441. int isnotcompressed;
  442. int readsamplesize;
  443. int wasted_bytes;
  444. int ricemodifier;
  445. /* 2^result = something to do with output waiting.
  446. * perhaps matters if we read > 1 frame in a pass?
  447. */
  448. readbits(alac, 4);
  449. readbits(alac, 12); /* unknown, skip 12 bits */
  450. hassize = readbits(alac, 1); /* the output sample size is stored soon */
  451. wasted_bytes = readbits(alac, 2); /* unknown ? */
  452. isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
  453. if (hassize) {
  454. /* now read the number of samples,
  455. * as a 32bit integer */
  456. outputsamples = readbits(alac, 32);
  457. *outputsize = outputsamples * alac->bytespersample;
  458. }
  459. readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8);
  460. if (!isnotcompressed) {
  461. /* so it is compressed */
  462. int16_t predictor_coef_table[32];
  463. int predictor_coef_num;
  464. int prediction_type;
  465. int prediction_quantitization;
  466. int i;
  467. /* skip 16 bits, not sure what they are. seem to be used in
  468. * two channel case */
  469. readbits(alac, 8);
  470. readbits(alac, 8);
  471. prediction_type = readbits(alac, 4);
  472. prediction_quantitization = readbits(alac, 4);
  473. ricemodifier = readbits(alac, 3);
  474. predictor_coef_num = readbits(alac, 5);
  475. /* read the predictor table */
  476. for (i = 0; i < predictor_coef_num; i++) {
  477. predictor_coef_table[i] = (int16_t)readbits(alac, 16);
  478. }
  479. if (wasted_bytes) {
  480. /* these bytes seem to have something to do with
  481. * > 2 channel files.
  482. */
  483. av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
  484. }
  485. bastardized_rice_decompress(alac,
  486. alac->predicterror_buffer_a,
  487. outputsamples,
  488. readsamplesize,
  489. alac->setinfo_rice_initialhistory,
  490. alac->setinfo_rice_kmodifier,
  491. ricemodifier * alac->setinfo_rice_historymult / 4,
  492. (1 << alac->setinfo_rice_kmodifier) - 1);
  493. if (prediction_type == 0) {
  494. /* adaptive fir */
  495. predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
  496. alac->outputsamples_buffer_a,
  497. outputsamples,
  498. readsamplesize,
  499. predictor_coef_table,
  500. predictor_coef_num,
  501. prediction_quantitization);
  502. } else {
  503. av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type);
  504. /* i think the only other prediction type (or perhaps this is just a
  505. * boolean?) runs adaptive fir twice.. like:
  506. * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
  507. * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
  508. * little strange..
  509. */
  510. }
  511. } else {
  512. /* not compressed, easy case */
  513. if (readsamplesize <= 16) {
  514. int i;
  515. for (i = 0; i < outputsamples; i++) {
  516. int32_t audiobits = readbits(alac, readsamplesize);
  517. audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
  518. alac->outputsamples_buffer_a[i] = audiobits;
  519. }
  520. } else {
  521. int i;
  522. for (i = 0; i < outputsamples; i++) {
  523. int32_t audiobits;
  524. audiobits = readbits(alac, 16);
  525. /* special case of sign extension..
  526. * as we'll be ORing the low 16bits into this */
  527. audiobits = audiobits << 16;
  528. audiobits = audiobits >> (32 - readsamplesize);
  529. audiobits |= readbits(alac, readsamplesize - 16);
  530. alac->outputsamples_buffer_a[i] = audiobits;
  531. }
  532. }
  533. /* wasted_bytes = 0; // unused */
  534. }
  535. switch(alac->setinfo_sample_size) {
  536. case 16: {
  537. int i;
  538. for (i = 0; i < outputsamples; i++) {
  539. int16_t sample = alac->outputsamples_buffer_a[i];
  540. be2me_16(sample);
  541. ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
  542. }
  543. break;
  544. }
  545. case 20:
  546. case 24:
  547. case 32:
  548. av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
  549. break;
  550. default:
  551. break;
  552. }
  553. break;
  554. }
  555. case 1: { /* 2 channels */
  556. int hassize;
  557. int isnotcompressed;
  558. int readsamplesize;
  559. int wasted_bytes;
  560. uint8_t interlacing_shift;
  561. uint8_t interlacing_leftweight;
  562. /* 2^result = something to do with output waiting.
  563. * perhaps matters if we read > 1 frame in a pass?
  564. */
  565. readbits(alac, 4);
  566. readbits(alac, 12); /* unknown, skip 12 bits */
  567. hassize = readbits(alac, 1); /* the output sample size is stored soon */
  568. wasted_bytes = readbits(alac, 2); /* unknown ? */
  569. isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
  570. if (hassize) {
  571. /* now read the number of samples,
  572. * as a 32bit integer */
  573. outputsamples = readbits(alac, 32);
  574. *outputsize = outputsamples * alac->bytespersample;
  575. }
  576. readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1;
  577. if (!isnotcompressed) {
  578. /* compressed */
  579. int16_t predictor_coef_table_a[32];
  580. int predictor_coef_num_a;
  581. int prediction_type_a;
  582. int prediction_quantitization_a;
  583. int ricemodifier_a;
  584. int16_t predictor_coef_table_b[32];
  585. int predictor_coef_num_b;
  586. int prediction_type_b;
  587. int prediction_quantitization_b;
  588. int ricemodifier_b;
  589. int i;
  590. interlacing_shift = readbits(alac, 8);
  591. interlacing_leftweight = readbits(alac, 8);
  592. /******** channel 1 ***********/
  593. prediction_type_a = readbits(alac, 4);
  594. prediction_quantitization_a = readbits(alac, 4);
  595. ricemodifier_a = readbits(alac, 3);
  596. predictor_coef_num_a = readbits(alac, 5);
  597. /* read the predictor table */
  598. for (i = 0; i < predictor_coef_num_a; i++) {
  599. predictor_coef_table_a[i] = (int16_t)readbits(alac, 16);
  600. }
  601. /******** channel 2 *********/
  602. prediction_type_b = readbits(alac, 4);
  603. prediction_quantitization_b = readbits(alac, 4);
  604. ricemodifier_b = readbits(alac, 3);
  605. predictor_coef_num_b = readbits(alac, 5);
  606. /* read the predictor table */
  607. for (i = 0; i < predictor_coef_num_b; i++) {
  608. predictor_coef_table_b[i] = (int16_t)readbits(alac, 16);
  609. }
  610. /*********************/
  611. if (wasted_bytes) {
  612. /* see mono case */
  613. av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
  614. }
  615. /* channel 1 */
  616. bastardized_rice_decompress(alac,
  617. alac->predicterror_buffer_a,
  618. outputsamples,
  619. readsamplesize,
  620. alac->setinfo_rice_initialhistory,
  621. alac->setinfo_rice_kmodifier,
  622. ricemodifier_a * alac->setinfo_rice_historymult / 4,
  623. (1 << alac->setinfo_rice_kmodifier) - 1);
  624. if (prediction_type_a == 0) {
  625. /* adaptive fir */
  626. predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
  627. alac->outputsamples_buffer_a,
  628. outputsamples,
  629. readsamplesize,
  630. predictor_coef_table_a,
  631. predictor_coef_num_a,
  632. prediction_quantitization_a);
  633. } else {
  634. /* see mono case */
  635. av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a);
  636. }
  637. /* channel 2 */
  638. bastardized_rice_decompress(alac,
  639. alac->predicterror_buffer_b,
  640. outputsamples,
  641. readsamplesize,
  642. alac->setinfo_rice_initialhistory,
  643. alac->setinfo_rice_kmodifier,
  644. ricemodifier_b * alac->setinfo_rice_historymult / 4,
  645. (1 << alac->setinfo_rice_kmodifier) - 1);
  646. if (prediction_type_b == 0) {
  647. /* adaptive fir */
  648. predictor_decompress_fir_adapt(alac->predicterror_buffer_b,
  649. alac->outputsamples_buffer_b,
  650. outputsamples,
  651. readsamplesize,
  652. predictor_coef_table_b,
  653. predictor_coef_num_b,
  654. prediction_quantitization_b);
  655. } else {
  656. av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b);
  657. }
  658. } else {
  659. /* not compressed, easy case */
  660. if (alac->setinfo_sample_size <= 16) {
  661. int i;
  662. for (i = 0; i < outputsamples; i++) {
  663. int32_t audiobits_a, audiobits_b;
  664. audiobits_a = readbits(alac, alac->setinfo_sample_size);
  665. audiobits_b = readbits(alac, alac->setinfo_sample_size);
  666. audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
  667. audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
  668. alac->outputsamples_buffer_a[i] = audiobits_a;
  669. alac->outputsamples_buffer_b[i] = audiobits_b;
  670. }
  671. } else {
  672. int i;
  673. for (i = 0; i < outputsamples; i++) {
  674. int32_t audiobits_a, audiobits_b;
  675. audiobits_a = readbits(alac, 16);
  676. audiobits_a = audiobits_a << 16;
  677. audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
  678. audiobits_a |= readbits(alac, alac->setinfo_sample_size - 16);
  679. audiobits_b = readbits(alac, 16);
  680. audiobits_b = audiobits_b << 16;
  681. audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
  682. audiobits_b |= readbits(alac, alac->setinfo_sample_size - 16);
  683. alac->outputsamples_buffer_a[i] = audiobits_a;
  684. alac->outputsamples_buffer_b[i] = audiobits_b;
  685. }
  686. }
  687. /* wasted_bytes = 0; */
  688. interlacing_shift = 0;
  689. interlacing_leftweight = 0;
  690. }
  691. switch(alac->setinfo_sample_size) {
  692. case 16: {
  693. deinterlace_16(alac->outputsamples_buffer_a,
  694. alac->outputsamples_buffer_b,
  695. (int16_t*)outbuffer,
  696. alac->numchannels,
  697. outputsamples,
  698. interlacing_shift,
  699. interlacing_leftweight);
  700. break;
  701. }
  702. case 20:
  703. case 24:
  704. case 32:
  705. av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
  706. break;
  707. default:
  708. break;
  709. }
  710. break;
  711. }
  712. }
  713. return input_buffer_size;
  714. }
  715. static int alac_decode_init(AVCodecContext * avctx)
  716. {
  717. ALACContext *s = avctx->priv_data;
  718. s->avctx = avctx;
  719. s->context_initialized = 0;
  720. s->alac = av_malloc(sizeof(alac_file));
  721. s->alac->samplesize = s->avctx->bits_per_sample;
  722. s->alac->numchannels = s->avctx->channels;
  723. s->alac->bytespersample = (s->alac->samplesize / 8) * s->alac->numchannels;
  724. return 0;
  725. }
  726. static int alac_decode_close(AVCodecContext *avctx)
  727. {
  728. ALACContext *s = avctx->priv_data;
  729. av_free(s->alac->predicterror_buffer_a);
  730. av_free(s->alac->predicterror_buffer_b);
  731. av_free(s->alac->outputsamples_buffer_a);
  732. av_free(s->alac->outputsamples_buffer_b);
  733. return 0;
  734. }
  735. AVCodec alac_decoder = {
  736. "alac",
  737. CODEC_TYPE_AUDIO,
  738. CODEC_ID_ALAC,
  739. sizeof(ALACContext),
  740. alac_decode_init,
  741. NULL,
  742. alac_decode_close,
  743. alac_decode_frame,
  744. };