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.

971 lines
31KB

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