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.

671 lines
21KB

  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
  23. * ALAC (Apple Lossless Audio Codec) decoder
  24. * @author 2005 David Hammerton
  25. * @see http://crazney.net/programs/itunes/alac.html
  26. *
  27. * Note: This decoder expects a 36-byte QuickTime atom to be
  28. * passed through the extradata[_size] fields. This atom is tacked onto
  29. * the end of an 'alac' stsd atom and has the following format:
  30. *
  31. * 32bit atom size
  32. * 32bit tag ("alac")
  33. * 32bit tag version (0)
  34. * 32bit samples per frame (used when not set explicitly in the frames)
  35. * 8bit compatible version (0)
  36. * 8bit sample size
  37. * 8bit history mult (40)
  38. * 8bit initial history (14)
  39. * 8bit rice param limit (10)
  40. * 8bit channels
  41. * 16bit maxRun (255)
  42. * 32bit max coded frame size (0 means unknown)
  43. * 32bit average bitrate (0 means unknown)
  44. * 32bit samplerate
  45. */
  46. #include "libavutil/channel_layout.h"
  47. #include "avcodec.h"
  48. #include "get_bits.h"
  49. #include "bytestream.h"
  50. #include "internal.h"
  51. #include "unary.h"
  52. #include "mathops.h"
  53. #define ALAC_EXTRADATA_SIZE 36
  54. #define MAX_CHANNELS 8
  55. typedef struct {
  56. AVCodecContext *avctx;
  57. AVFrame frame;
  58. GetBitContext gb;
  59. int channels;
  60. int32_t *predict_error_buffer[2];
  61. int32_t *output_samples_buffer[2];
  62. int32_t *extra_bits_buffer[2];
  63. uint32_t max_samples_per_frame;
  64. uint8_t sample_size;
  65. uint8_t rice_history_mult;
  66. uint8_t rice_initial_history;
  67. uint8_t rice_limit;
  68. int extra_bits; /**< number of extra bits beyond 16-bit */
  69. int nb_samples; /**< number of samples in the current frame */
  70. int direct_output;
  71. } ALACContext;
  72. enum RawDataBlockType {
  73. /* At the moment, only SCE, CPE, LFE, and END are recognized. */
  74. TYPE_SCE,
  75. TYPE_CPE,
  76. TYPE_CCE,
  77. TYPE_LFE,
  78. TYPE_DSE,
  79. TYPE_PCE,
  80. TYPE_FIL,
  81. TYPE_END
  82. };
  83. static const uint8_t alac_channel_layout_offsets[8][8] = {
  84. { 0 },
  85. { 0, 1 },
  86. { 2, 0, 1 },
  87. { 2, 0, 1, 3 },
  88. { 2, 0, 1, 3, 4 },
  89. { 2, 0, 1, 4, 5, 3 },
  90. { 2, 0, 1, 4, 5, 6, 3 },
  91. { 2, 6, 7, 0, 1, 4, 5, 3 }
  92. };
  93. static const uint16_t alac_channel_layouts[8] = {
  94. AV_CH_LAYOUT_MONO,
  95. AV_CH_LAYOUT_STEREO,
  96. AV_CH_LAYOUT_SURROUND,
  97. AV_CH_LAYOUT_4POINT0,
  98. AV_CH_LAYOUT_5POINT0_BACK,
  99. AV_CH_LAYOUT_5POINT1_BACK,
  100. AV_CH_LAYOUT_6POINT1_BACK,
  101. AV_CH_LAYOUT_7POINT1_WIDE_BACK
  102. };
  103. static inline unsigned int decode_scalar(GetBitContext *gb, int k, int bps)
  104. {
  105. unsigned int x = get_unary_0_9(gb);
  106. if (x > 8) { /* RICE THRESHOLD */
  107. /* use alternative encoding */
  108. x = get_bits_long(gb, bps);
  109. } else if (k != 1) {
  110. int extrabits = show_bits(gb, k);
  111. /* multiply x by 2^k - 1, as part of their strange algorithm */
  112. x = (x << k) - x;
  113. if (extrabits > 1) {
  114. x += extrabits - 1;
  115. skip_bits(gb, k);
  116. } else
  117. skip_bits(gb, k - 1);
  118. }
  119. return x;
  120. }
  121. static int rice_decompress(ALACContext *alac, int32_t *output_buffer,
  122. int nb_samples, int bps, int rice_history_mult)
  123. {
  124. int i;
  125. unsigned int history = alac->rice_initial_history;
  126. int sign_modifier = 0;
  127. for (i = 0; i < nb_samples; i++) {
  128. int k;
  129. unsigned int x;
  130. if(get_bits_left(&alac->gb) <= 0)
  131. return -1;
  132. /* calculate rice param and decode next value */
  133. k = av_log2((history >> 9) + 3);
  134. k = FFMIN(k, alac->rice_limit);
  135. x = decode_scalar(&alac->gb, k, bps);
  136. x += sign_modifier;
  137. sign_modifier = 0;
  138. output_buffer[i] = (x >> 1) ^ -(x & 1);
  139. /* update the history */
  140. if (x > 0xffff)
  141. history = 0xffff;
  142. else
  143. history += x * rice_history_mult -
  144. ((history * rice_history_mult) >> 9);
  145. /* special case: there may be compressed blocks of 0 */
  146. if ((history < 128) && (i + 1 < nb_samples)) {
  147. int block_size;
  148. /* calculate rice param and decode block size */
  149. k = 7 - av_log2(history) + ((history + 16) >> 6);
  150. k = FFMIN(k, alac->rice_limit);
  151. block_size = decode_scalar(&alac->gb, k, 16);
  152. if (block_size > 0) {
  153. if (block_size >= nb_samples - i) {
  154. av_log(alac->avctx, AV_LOG_ERROR,
  155. "invalid zero block size of %d %d %d\n", block_size,
  156. nb_samples, i);
  157. block_size = nb_samples - i - 1;
  158. }
  159. memset(&output_buffer[i + 1], 0,
  160. block_size * sizeof(*output_buffer));
  161. i += block_size;
  162. }
  163. if (block_size <= 0xffff)
  164. sign_modifier = 1;
  165. history = 0;
  166. }
  167. }
  168. return 0;
  169. }
  170. static inline int sign_only(int v)
  171. {
  172. return v ? FFSIGN(v) : 0;
  173. }
  174. static void lpc_prediction(int32_t *error_buffer, int32_t *buffer_out,
  175. int nb_samples, int bps, int16_t *lpc_coefs,
  176. int lpc_order, int lpc_quant)
  177. {
  178. int i;
  179. int32_t *pred = buffer_out;
  180. /* first sample always copies */
  181. *buffer_out = *error_buffer;
  182. if (nb_samples <= 1)
  183. return;
  184. if (!lpc_order) {
  185. memcpy(&buffer_out[1], &error_buffer[1],
  186. (nb_samples - 1) * sizeof(*buffer_out));
  187. return;
  188. }
  189. if (lpc_order == 31) {
  190. /* simple 1st-order prediction */
  191. for (i = 1; i < nb_samples; i++) {
  192. buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i],
  193. bps);
  194. }
  195. return;
  196. }
  197. /* read warm-up samples */
  198. for (i = 1; i <= lpc_order && i < nb_samples; i++)
  199. buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i], bps);
  200. /* NOTE: 4 and 8 are very common cases that could be optimized. */
  201. for (; i < nb_samples; i++) {
  202. int j;
  203. int val = 0;
  204. int error_val = error_buffer[i];
  205. int error_sign;
  206. int d = *pred++;
  207. /* LPC prediction */
  208. for (j = 0; j < lpc_order; j++)
  209. val += (pred[j] - d) * lpc_coefs[j];
  210. val = (val + (1 << (lpc_quant - 1))) >> lpc_quant;
  211. val += d + error_val;
  212. buffer_out[i] = sign_extend(val, bps);
  213. /* adapt LPC coefficients */
  214. error_sign = sign_only(error_val);
  215. if (error_sign) {
  216. for (j = 0; j < lpc_order && error_val * error_sign > 0; j++) {
  217. int sign;
  218. val = d - pred[j];
  219. sign = sign_only(val) * error_sign;
  220. lpc_coefs[j] -= sign;
  221. val *= sign;
  222. error_val -= (val >> lpc_quant) * (j + 1);
  223. }
  224. }
  225. }
  226. }
  227. static void decorrelate_stereo(int32_t *buffer[2], int nb_samples,
  228. int decorr_shift, int decorr_left_weight)
  229. {
  230. int i;
  231. for (i = 0; i < nb_samples; i++) {
  232. int32_t a, b;
  233. a = buffer[0][i];
  234. b = buffer[1][i];
  235. a -= (b * decorr_left_weight) >> decorr_shift;
  236. b += a;
  237. buffer[0][i] = b;
  238. buffer[1][i] = a;
  239. }
  240. }
  241. static void append_extra_bits(int32_t *buffer[2], int32_t *extra_bits_buffer[2],
  242. int extra_bits, int channels, int nb_samples)
  243. {
  244. int i, ch;
  245. for (ch = 0; ch < channels; ch++)
  246. for (i = 0; i < nb_samples; i++)
  247. buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i];
  248. }
  249. static int decode_element(AVCodecContext *avctx, void *data, int ch_index,
  250. int channels)
  251. {
  252. ALACContext *alac = avctx->priv_data;
  253. int has_size, bps, is_compressed, decorr_shift, decorr_left_weight, ret;
  254. uint32_t output_samples;
  255. int i, ch;
  256. skip_bits(&alac->gb, 4); /* element instance tag */
  257. skip_bits(&alac->gb, 12); /* unused header bits */
  258. /* the number of output samples is stored in the frame */
  259. has_size = get_bits1(&alac->gb);
  260. alac->extra_bits = get_bits(&alac->gb, 2) << 3;
  261. bps = alac->sample_size - alac->extra_bits + channels - 1;
  262. if (bps > 32U) {
  263. av_log(avctx, AV_LOG_ERROR, "bps is unsupported: %d\n", bps);
  264. return AVERROR_PATCHWELCOME;
  265. }
  266. /* whether the frame is compressed */
  267. is_compressed = !get_bits1(&alac->gb);
  268. if (has_size)
  269. output_samples = get_bits_long(&alac->gb, 32);
  270. else
  271. output_samples = alac->max_samples_per_frame;
  272. if (!output_samples || output_samples > alac->max_samples_per_frame) {
  273. av_log(avctx, AV_LOG_ERROR, "invalid samples per frame: %d\n",
  274. output_samples);
  275. return AVERROR_INVALIDDATA;
  276. }
  277. if (!alac->nb_samples) {
  278. /* get output buffer */
  279. alac->frame.nb_samples = output_samples;
  280. if ((ret = ff_get_buffer(avctx, &alac->frame)) < 0) {
  281. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  282. return ret;
  283. }
  284. } else if (output_samples != alac->nb_samples) {
  285. av_log(avctx, AV_LOG_ERROR, "sample count mismatch: %u != %d\n",
  286. output_samples, alac->nb_samples);
  287. return AVERROR_INVALIDDATA;
  288. }
  289. alac->nb_samples = output_samples;
  290. if (alac->direct_output) {
  291. for (ch = 0; ch < channels; ch++)
  292. alac->output_samples_buffer[ch] = (int32_t *)alac->frame.extended_data[ch_index + ch];
  293. }
  294. if (is_compressed) {
  295. int16_t lpc_coefs[2][32];
  296. int lpc_order[2];
  297. int prediction_type[2];
  298. int lpc_quant[2];
  299. int rice_history_mult[2];
  300. decorr_shift = get_bits(&alac->gb, 8);
  301. decorr_left_weight = get_bits(&alac->gb, 8);
  302. for (ch = 0; ch < channels; ch++) {
  303. prediction_type[ch] = get_bits(&alac->gb, 4);
  304. lpc_quant[ch] = get_bits(&alac->gb, 4);
  305. rice_history_mult[ch] = get_bits(&alac->gb, 3);
  306. lpc_order[ch] = get_bits(&alac->gb, 5);
  307. /* read the predictor table */
  308. for (i = lpc_order[ch] - 1; i >= 0; i--)
  309. lpc_coefs[ch][i] = get_sbits(&alac->gb, 16);
  310. }
  311. if (alac->extra_bits) {
  312. for (i = 0; i < alac->nb_samples; i++) {
  313. if(get_bits_left(&alac->gb) <= 0)
  314. return -1;
  315. for (ch = 0; ch < channels; ch++)
  316. alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);
  317. }
  318. }
  319. for (ch = 0; ch < channels; ch++) {
  320. int ret=rice_decompress(alac, alac->predict_error_buffer[ch],
  321. alac->nb_samples, bps,
  322. rice_history_mult[ch] * alac->rice_history_mult / 4);
  323. if(ret<0)
  324. return ret;
  325. /* adaptive FIR filter */
  326. if (prediction_type[ch] == 15) {
  327. /* Prediction type 15 runs the adaptive FIR twice.
  328. * The first pass uses the special-case coef_num = 31, while
  329. * the second pass uses the coefs from the bitstream.
  330. *
  331. * However, this prediction type is not currently used by the
  332. * reference encoder.
  333. */
  334. lpc_prediction(alac->predict_error_buffer[ch],
  335. alac->predict_error_buffer[ch],
  336. alac->nb_samples, bps, NULL, 31, 0);
  337. } else if (prediction_type[ch] > 0) {
  338. av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n",
  339. prediction_type[ch]);
  340. }
  341. lpc_prediction(alac->predict_error_buffer[ch],
  342. alac->output_samples_buffer[ch], alac->nb_samples,
  343. bps, lpc_coefs[ch], lpc_order[ch], lpc_quant[ch]);
  344. }
  345. } else {
  346. /* not compressed, easy case */
  347. for (i = 0; i < alac->nb_samples; i++) {
  348. if(get_bits_left(&alac->gb) <= 0)
  349. return -1;
  350. for (ch = 0; ch < channels; ch++) {
  351. alac->output_samples_buffer[ch][i] =
  352. get_sbits_long(&alac->gb, alac->sample_size);
  353. }
  354. }
  355. alac->extra_bits = 0;
  356. decorr_shift = 0;
  357. decorr_left_weight = 0;
  358. }
  359. if (channels == 2 && decorr_left_weight) {
  360. decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples,
  361. decorr_shift, decorr_left_weight);
  362. }
  363. if (alac->extra_bits) {
  364. append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer,
  365. alac->extra_bits, channels, alac->nb_samples);
  366. }
  367. if(av_sample_fmt_is_planar(avctx->sample_fmt)) {
  368. switch(alac->sample_size) {
  369. case 16: {
  370. for (ch = 0; ch < channels; ch++) {
  371. int16_t *outbuffer = (int16_t *)alac->frame.extended_data[ch_index + ch];
  372. for (i = 0; i < alac->nb_samples; i++)
  373. *outbuffer++ = alac->output_samples_buffer[ch][i];
  374. }}
  375. break;
  376. case 24: {
  377. for (ch = 0; ch < channels; ch++) {
  378. for (i = 0; i < alac->nb_samples; i++)
  379. alac->output_samples_buffer[ch][i] <<= 8;
  380. }}
  381. break;
  382. }
  383. }else{
  384. switch(alac->sample_size) {
  385. case 16: {
  386. int16_t *outbuffer = ((int16_t *)alac->frame.extended_data[0]) + ch_index;
  387. for (i = 0; i < alac->nb_samples; i++) {
  388. for (ch = 0; ch < channels; ch++)
  389. *outbuffer++ = alac->output_samples_buffer[ch][i];
  390. outbuffer += alac->channels - channels;
  391. }
  392. }
  393. break;
  394. case 24: {
  395. int32_t *outbuffer = ((int32_t *)alac->frame.extended_data[0]) + ch_index;
  396. for (i = 0; i < alac->nb_samples; i++) {
  397. for (ch = 0; ch < channels; ch++)
  398. *outbuffer++ = alac->output_samples_buffer[ch][i] << 8;
  399. outbuffer += alac->channels - channels;
  400. }
  401. }
  402. break;
  403. case 32: {
  404. int32_t *outbuffer = ((int32_t *)alac->frame.extended_data[0]) + ch_index;
  405. for (i = 0; i < alac->nb_samples; i++) {
  406. for (ch = 0; ch < channels; ch++)
  407. *outbuffer++ = alac->output_samples_buffer[ch][i];
  408. outbuffer += alac->channels - channels;
  409. }
  410. }
  411. break;
  412. }
  413. }
  414. return 0;
  415. }
  416. static int alac_decode_frame(AVCodecContext *avctx, void *data,
  417. int *got_frame_ptr, AVPacket *avpkt)
  418. {
  419. ALACContext *alac = avctx->priv_data;
  420. enum RawDataBlockType element;
  421. int channels;
  422. int ch, ret, got_end;
  423. init_get_bits(&alac->gb, avpkt->data, avpkt->size * 8);
  424. got_end = 0;
  425. alac->nb_samples = 0;
  426. ch = 0;
  427. while (get_bits_left(&alac->gb) >= 3) {
  428. element = get_bits(&alac->gb, 3);
  429. if (element == TYPE_END) {
  430. got_end = 1;
  431. break;
  432. }
  433. if (element > TYPE_CPE && element != TYPE_LFE) {
  434. av_log(avctx, AV_LOG_ERROR, "syntax element unsupported: %d\n", element);
  435. return AVERROR_PATCHWELCOME;
  436. }
  437. channels = (element == TYPE_CPE) ? 2 : 1;
  438. if ( ch + channels > alac->channels
  439. || alac_channel_layout_offsets[alac->channels - 1][ch] + channels > alac->channels
  440. ) {
  441. av_log(avctx, AV_LOG_ERROR, "invalid element channel count\n");
  442. return AVERROR_INVALIDDATA;
  443. }
  444. ret = decode_element(avctx, data,
  445. alac_channel_layout_offsets[alac->channels - 1][ch],
  446. channels);
  447. if (ret < 0 && get_bits_left(&alac->gb))
  448. return ret;
  449. ch += channels;
  450. }
  451. if (!got_end) {
  452. av_log(avctx, AV_LOG_ERROR, "no end tag found. incomplete packet.\n");
  453. return AVERROR_INVALIDDATA;
  454. }
  455. if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) {
  456. av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n",
  457. avpkt->size * 8 - get_bits_count(&alac->gb));
  458. }
  459. *got_frame_ptr = 1;
  460. *(AVFrame *)data = alac->frame;
  461. return avpkt->size;
  462. }
  463. static av_cold int alac_decode_close(AVCodecContext *avctx)
  464. {
  465. ALACContext *alac = avctx->priv_data;
  466. int ch;
  467. for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
  468. av_freep(&alac->predict_error_buffer[ch]);
  469. if (!alac->direct_output)
  470. av_freep(&alac->output_samples_buffer[ch]);
  471. av_freep(&alac->extra_bits_buffer[ch]);
  472. }
  473. return 0;
  474. }
  475. static int allocate_buffers(ALACContext *alac)
  476. {
  477. int ch;
  478. int buf_size;
  479. if (alac->max_samples_per_frame > INT_MAX / sizeof(int32_t))
  480. goto buf_alloc_fail;
  481. buf_size = alac->max_samples_per_frame * sizeof(int32_t);
  482. for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
  483. FF_ALLOC_OR_GOTO(alac->avctx, alac->predict_error_buffer[ch],
  484. buf_size, buf_alloc_fail);
  485. alac->direct_output = alac->sample_size > 16 && av_sample_fmt_is_planar(alac->avctx->sample_fmt);
  486. if (!alac->direct_output) {
  487. FF_ALLOC_OR_GOTO(alac->avctx, alac->output_samples_buffer[ch],
  488. buf_size, buf_alloc_fail);
  489. }
  490. FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch],
  491. buf_size, buf_alloc_fail);
  492. }
  493. return 0;
  494. buf_alloc_fail:
  495. alac_decode_close(alac->avctx);
  496. return AVERROR(ENOMEM);
  497. }
  498. static int alac_set_info(ALACContext *alac)
  499. {
  500. GetByteContext gb;
  501. bytestream2_init(&gb, alac->avctx->extradata,
  502. alac->avctx->extradata_size);
  503. bytestream2_skipu(&gb, 12); // size:4, alac:4, version:4
  504. alac->max_samples_per_frame = bytestream2_get_be32u(&gb);
  505. if (!alac->max_samples_per_frame || alac->max_samples_per_frame > INT_MAX) {
  506. av_log(alac->avctx, AV_LOG_ERROR, "max samples per frame invalid: %u\n",
  507. alac->max_samples_per_frame);
  508. return AVERROR_INVALIDDATA;
  509. }
  510. bytestream2_skipu(&gb, 1); // compatible version
  511. alac->sample_size = bytestream2_get_byteu(&gb);
  512. alac->rice_history_mult = bytestream2_get_byteu(&gb);
  513. alac->rice_initial_history = bytestream2_get_byteu(&gb);
  514. alac->rice_limit = bytestream2_get_byteu(&gb);
  515. alac->channels = bytestream2_get_byteu(&gb);
  516. bytestream2_get_be16u(&gb); // maxRun
  517. bytestream2_get_be32u(&gb); // max coded frame size
  518. bytestream2_get_be32u(&gb); // average bitrate
  519. bytestream2_get_be32u(&gb); // samplerate
  520. return 0;
  521. }
  522. static av_cold int alac_decode_init(AVCodecContext * avctx)
  523. {
  524. int ret;
  525. int req_packed;
  526. ALACContext *alac = avctx->priv_data;
  527. alac->avctx = avctx;
  528. /* initialize from the extradata */
  529. if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
  530. av_log(avctx, AV_LOG_ERROR, "expected %d extradata bytes\n",
  531. ALAC_EXTRADATA_SIZE);
  532. return -1;
  533. }
  534. if (alac_set_info(alac)) {
  535. av_log(avctx, AV_LOG_ERROR, "set_info failed\n");
  536. return -1;
  537. }
  538. req_packed = LIBAVCODEC_VERSION_MAJOR < 55 && !av_sample_fmt_is_planar(avctx->request_sample_fmt);
  539. switch (alac->sample_size) {
  540. case 16: avctx->sample_fmt = req_packed ? AV_SAMPLE_FMT_S16 : AV_SAMPLE_FMT_S16P;
  541. break;
  542. case 24:
  543. case 32: avctx->sample_fmt = req_packed ? AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S32P;
  544. break;
  545. default: av_log_ask_for_sample(avctx, "Sample depth %d is not supported.\n",
  546. alac->sample_size);
  547. return AVERROR_PATCHWELCOME;
  548. }
  549. avctx->bits_per_raw_sample = alac->sample_size;
  550. if (alac->channels < 1) {
  551. av_log(avctx, AV_LOG_WARNING, "Invalid channel count\n");
  552. alac->channels = avctx->channels;
  553. } else {
  554. if (alac->channels > MAX_CHANNELS)
  555. alac->channels = avctx->channels;
  556. else
  557. avctx->channels = alac->channels;
  558. }
  559. if (avctx->channels > MAX_CHANNELS || avctx->channels <= 0 ) {
  560. av_log(avctx, AV_LOG_ERROR, "Unsupported channel count: %d\n",
  561. avctx->channels);
  562. return AVERROR_PATCHWELCOME;
  563. }
  564. avctx->channel_layout = alac_channel_layouts[alac->channels - 1];
  565. if ((ret = allocate_buffers(alac)) < 0) {
  566. av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n");
  567. return ret;
  568. }
  569. avcodec_get_frame_defaults(&alac->frame);
  570. avctx->coded_frame = &alac->frame;
  571. return 0;
  572. }
  573. AVCodec ff_alac_decoder = {
  574. .name = "alac",
  575. .type = AVMEDIA_TYPE_AUDIO,
  576. .id = AV_CODEC_ID_ALAC,
  577. .priv_data_size = sizeof(ALACContext),
  578. .init = alac_decode_init,
  579. .close = alac_decode_close,
  580. .decode = alac_decode_frame,
  581. .capabilities = CODEC_CAP_DR1,
  582. .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
  583. };