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.

860 lines
28KB

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