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.

829 lines
27KB

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