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.

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