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.

680 lines
22KB

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