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.

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