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.

827 lines
27KB

  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 */ /* max sample size?? */
  80. uint32_t setinfo_86; /* 0x00069fe4 */ /* bit rate (average)?? */
  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. int hassize;
  373. int readsamplesize;
  374. int wasted_bytes;
  375. int isnotcompressed;
  376. /* short-circuit null buffers */
  377. if (!inbuffer || !input_buffer_size)
  378. return input_buffer_size;
  379. /* initialize from the extradata */
  380. if (!alac->context_initialized) {
  381. if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
  382. av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
  383. ALAC_EXTRADATA_SIZE);
  384. return input_buffer_size;
  385. }
  386. if (alac_set_info(alac)) {
  387. av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
  388. return input_buffer_size;
  389. }
  390. alac->context_initialized = 1;
  391. }
  392. init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
  393. channels = get_bits(&alac->gb, 3);
  394. /* 2^result = something to do with output waiting.
  395. * perhaps matters if we read > 1 frame in a pass?
  396. */
  397. get_bits(&alac->gb, 4);
  398. get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
  399. hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
  400. wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
  401. isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
  402. if (hassize) {
  403. /* now read the number of samples,
  404. * as a 32bit integer */
  405. outputsamples = get_bits(&alac->gb, 32);
  406. } else
  407. outputsamples = alac->setinfo_max_samples_per_frame;
  408. *outputsize = outputsamples * alac->bytespersample;
  409. readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + channels;
  410. switch(channels) {
  411. case 0: { /* 1 channel */
  412. int ricemodifier;
  413. if (!isnotcompressed) {
  414. /* so it is compressed */
  415. int16_t predictor_coef_table[32];
  416. int predictor_coef_num;
  417. int prediction_type;
  418. int prediction_quantitization;
  419. int i;
  420. /* FIXME: skip 16 bits, not sure what they are. seem to be used in
  421. * two channel case */
  422. get_bits(&alac->gb, 8);
  423. get_bits(&alac->gb, 8);
  424. prediction_type = get_bits(&alac->gb, 4);
  425. prediction_quantitization = get_bits(&alac->gb, 4);
  426. ricemodifier = get_bits(&alac->gb, 3);
  427. predictor_coef_num = get_bits(&alac->gb, 5);
  428. /* read the predictor table */
  429. for (i = 0; i < predictor_coef_num; i++) {
  430. predictor_coef_table[i] = (int16_t)get_bits(&alac->gb, 16);
  431. }
  432. if (wasted_bytes) {
  433. /* these bytes seem to have something to do with
  434. * > 2 channel files.
  435. */
  436. av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
  437. }
  438. bastardized_rice_decompress(alac,
  439. alac->predicterror_buffer_a,
  440. outputsamples,
  441. readsamplesize,
  442. alac->setinfo_rice_initialhistory,
  443. alac->setinfo_rice_kmodifier,
  444. ricemodifier * alac->setinfo_rice_historymult / 4,
  445. (1 << alac->setinfo_rice_kmodifier) - 1);
  446. if (prediction_type == 0) {
  447. /* adaptive fir */
  448. predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
  449. alac->outputsamples_buffer_a,
  450. outputsamples,
  451. readsamplesize,
  452. predictor_coef_table,
  453. predictor_coef_num,
  454. prediction_quantitization);
  455. } else {
  456. av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type);
  457. /* i think the only other prediction type (or perhaps this is just a
  458. * boolean?) runs adaptive fir twice.. like:
  459. * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
  460. * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
  461. * little strange..
  462. */
  463. }
  464. } else {
  465. /* not compressed, easy case */
  466. if (readsamplesize <= 16) {
  467. int i;
  468. for (i = 0; i < outputsamples; i++) {
  469. int32_t audiobits = get_bits(&alac->gb, readsamplesize);
  470. audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
  471. alac->outputsamples_buffer_a[i] = audiobits;
  472. }
  473. } else {
  474. int i;
  475. for (i = 0; i < outputsamples; i++) {
  476. int32_t audiobits;
  477. audiobits = get_bits(&alac->gb, 16);
  478. /* special case of sign extension..
  479. * as we'll be ORing the low 16bits into this */
  480. audiobits = audiobits << 16;
  481. audiobits = audiobits >> (32 - readsamplesize);
  482. audiobits |= get_bits(&alac->gb, readsamplesize - 16);
  483. alac->outputsamples_buffer_a[i] = audiobits;
  484. }
  485. }
  486. /* wasted_bytes = 0; // unused */
  487. }
  488. switch(alac->setinfo_sample_size) {
  489. case 16: {
  490. int i;
  491. for (i = 0; i < outputsamples; i++) {
  492. int16_t sample = alac->outputsamples_buffer_a[i];
  493. ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
  494. }
  495. break;
  496. }
  497. case 20:
  498. case 24:
  499. case 32:
  500. av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
  501. break;
  502. default:
  503. break;
  504. }
  505. break;
  506. }
  507. case 1: { /* 2 channels */
  508. uint8_t interlacing_shift;
  509. uint8_t interlacing_leftweight;
  510. if (!isnotcompressed) {
  511. /* compressed */
  512. int16_t predictor_coef_table_a[32];
  513. int predictor_coef_num_a;
  514. int prediction_type_a;
  515. int prediction_quantitization_a;
  516. int ricemodifier_a;
  517. int16_t predictor_coef_table_b[32];
  518. int predictor_coef_num_b;
  519. int prediction_type_b;
  520. int prediction_quantitization_b;
  521. int ricemodifier_b;
  522. int i;
  523. interlacing_shift = get_bits(&alac->gb, 8);
  524. interlacing_leftweight = get_bits(&alac->gb, 8);
  525. /******** channel 1 ***********/
  526. prediction_type_a = get_bits(&alac->gb, 4);
  527. prediction_quantitization_a = get_bits(&alac->gb, 4);
  528. ricemodifier_a = get_bits(&alac->gb, 3);
  529. predictor_coef_num_a = get_bits(&alac->gb, 5);
  530. /* read the predictor table */
  531. for (i = 0; i < predictor_coef_num_a; i++) {
  532. predictor_coef_table_a[i] = (int16_t)get_bits(&alac->gb, 16);
  533. }
  534. /******** channel 2 *********/
  535. prediction_type_b = get_bits(&alac->gb, 4);
  536. prediction_quantitization_b = get_bits(&alac->gb, 4);
  537. ricemodifier_b = get_bits(&alac->gb, 3);
  538. predictor_coef_num_b = get_bits(&alac->gb, 5);
  539. /* read the predictor table */
  540. for (i = 0; i < predictor_coef_num_b; i++) {
  541. predictor_coef_table_b[i] = (int16_t)get_bits(&alac->gb, 16);
  542. }
  543. /*********************/
  544. if (wasted_bytes) {
  545. /* see mono case */
  546. av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
  547. }
  548. /* channel 1 */
  549. bastardized_rice_decompress(alac,
  550. alac->predicterror_buffer_a,
  551. outputsamples,
  552. readsamplesize,
  553. alac->setinfo_rice_initialhistory,
  554. alac->setinfo_rice_kmodifier,
  555. ricemodifier_a * alac->setinfo_rice_historymult / 4,
  556. (1 << alac->setinfo_rice_kmodifier) - 1);
  557. if (prediction_type_a == 0) {
  558. /* adaptive fir */
  559. predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
  560. alac->outputsamples_buffer_a,
  561. outputsamples,
  562. readsamplesize,
  563. predictor_coef_table_a,
  564. predictor_coef_num_a,
  565. prediction_quantitization_a);
  566. } else {
  567. /* see mono case */
  568. av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a);
  569. }
  570. /* channel 2 */
  571. bastardized_rice_decompress(alac,
  572. alac->predicterror_buffer_b,
  573. outputsamples,
  574. readsamplesize,
  575. alac->setinfo_rice_initialhistory,
  576. alac->setinfo_rice_kmodifier,
  577. ricemodifier_b * alac->setinfo_rice_historymult / 4,
  578. (1 << alac->setinfo_rice_kmodifier) - 1);
  579. if (prediction_type_b == 0) {
  580. /* adaptive fir */
  581. predictor_decompress_fir_adapt(alac->predicterror_buffer_b,
  582. alac->outputsamples_buffer_b,
  583. outputsamples,
  584. readsamplesize,
  585. predictor_coef_table_b,
  586. predictor_coef_num_b,
  587. prediction_quantitization_b);
  588. } else {
  589. av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b);
  590. }
  591. } else {
  592. /* not compressed, easy case */
  593. if (alac->setinfo_sample_size <= 16) {
  594. int i;
  595. for (i = 0; i < outputsamples; i++) {
  596. int32_t audiobits_a, audiobits_b;
  597. audiobits_a = get_bits(&alac->gb, alac->setinfo_sample_size);
  598. audiobits_b = get_bits(&alac->gb, alac->setinfo_sample_size);
  599. audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
  600. audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
  601. alac->outputsamples_buffer_a[i] = audiobits_a;
  602. alac->outputsamples_buffer_b[i] = audiobits_b;
  603. }
  604. } else {
  605. int i;
  606. for (i = 0; i < outputsamples; i++) {
  607. int32_t audiobits_a, audiobits_b;
  608. audiobits_a = get_bits(&alac->gb, 16);
  609. audiobits_a = audiobits_a << 16;
  610. audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
  611. audiobits_a |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
  612. audiobits_b = get_bits(&alac->gb, 16);
  613. audiobits_b = audiobits_b << 16;
  614. audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
  615. audiobits_b |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
  616. alac->outputsamples_buffer_a[i] = audiobits_a;
  617. alac->outputsamples_buffer_b[i] = audiobits_b;
  618. }
  619. }
  620. /* wasted_bytes = 0; */
  621. interlacing_shift = 0;
  622. interlacing_leftweight = 0;
  623. }
  624. switch(alac->setinfo_sample_size) {
  625. case 16: {
  626. deinterlace_16(alac->outputsamples_buffer_a,
  627. alac->outputsamples_buffer_b,
  628. (int16_t*)outbuffer,
  629. alac->numchannels,
  630. outputsamples,
  631. interlacing_shift,
  632. interlacing_leftweight);
  633. break;
  634. }
  635. case 20:
  636. case 24:
  637. case 32:
  638. av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
  639. break;
  640. default:
  641. break;
  642. }
  643. break;
  644. }
  645. }
  646. return input_buffer_size;
  647. }
  648. static int alac_decode_init(AVCodecContext * avctx)
  649. {
  650. ALACContext *alac = avctx->priv_data;
  651. alac->avctx = avctx;
  652. alac->context_initialized = 0;
  653. alac->samplesize = alac->avctx->bits_per_sample;
  654. alac->numchannels = alac->avctx->channels;
  655. alac->bytespersample = (alac->samplesize / 8) * alac->numchannels;
  656. return 0;
  657. }
  658. static int alac_decode_close(AVCodecContext *avctx)
  659. {
  660. ALACContext *alac = avctx->priv_data;
  661. av_free(alac->predicterror_buffer_a);
  662. av_free(alac->predicterror_buffer_b);
  663. av_free(alac->outputsamples_buffer_a);
  664. av_free(alac->outputsamples_buffer_b);
  665. return 0;
  666. }
  667. AVCodec alac_decoder = {
  668. "alac",
  669. CODEC_TYPE_AUDIO,
  670. CODEC_ID_ALAC,
  671. sizeof(ALACContext),
  672. alac_decode_init,
  673. NULL,
  674. alac_decode_close,
  675. alac_decode_frame,
  676. };