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.

673 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, int32_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. int32_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. int 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 + (1 << (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 && 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 *= 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, "Compression with rice limit 0");
  274. return AVERROR(ENOSYS);
  275. }
  276. decorr_shift = get_bits(&alac->gb, 8);
  277. decorr_left_weight = get_bits(&alac->gb, 8);
  278. for (ch = 0; ch < channels; ch++) {
  279. prediction_type[ch] = get_bits(&alac->gb, 4);
  280. lpc_quant[ch] = get_bits(&alac->gb, 4);
  281. rice_history_mult[ch] = get_bits(&alac->gb, 3);
  282. lpc_order[ch] = get_bits(&alac->gb, 5);
  283. if (lpc_order[ch] >= alac->max_samples_per_frame)
  284. return AVERROR_INVALIDDATA;
  285. /* read the predictor table */
  286. for (i = lpc_order[ch] - 1; i >= 0; i--)
  287. lpc_coefs[ch][i] = get_sbits(&alac->gb, 16);
  288. }
  289. if (alac->extra_bits) {
  290. for (i = 0; i < alac->nb_samples; i++) {
  291. if(get_bits_left(&alac->gb) <= 0)
  292. return -1;
  293. for (ch = 0; ch < channels; ch++)
  294. alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);
  295. }
  296. }
  297. for (ch = 0; ch < channels; ch++) {
  298. int ret=rice_decompress(alac, alac->predict_error_buffer[ch],
  299. alac->nb_samples, bps,
  300. rice_history_mult[ch] * alac->rice_history_mult / 4);
  301. if(ret<0)
  302. return ret;
  303. /* adaptive FIR filter */
  304. if (prediction_type[ch] == 15) {
  305. /* Prediction type 15 runs the adaptive FIR twice.
  306. * The first pass uses the special-case coef_num = 31, while
  307. * the second pass uses the coefs from the bitstream.
  308. *
  309. * However, this prediction type is not currently used by the
  310. * reference encoder.
  311. */
  312. lpc_prediction(alac->predict_error_buffer[ch],
  313. alac->predict_error_buffer[ch],
  314. alac->nb_samples, bps, NULL, 31, 0);
  315. } else if (prediction_type[ch] > 0) {
  316. av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n",
  317. prediction_type[ch]);
  318. }
  319. lpc_prediction(alac->predict_error_buffer[ch],
  320. alac->output_samples_buffer[ch], alac->nb_samples,
  321. bps, lpc_coefs[ch], lpc_order[ch], lpc_quant[ch]);
  322. }
  323. } else {
  324. /* not compressed, easy case */
  325. for (i = 0; i < alac->nb_samples; i++) {
  326. if(get_bits_left(&alac->gb) <= 0)
  327. return -1;
  328. for (ch = 0; ch < channels; ch++) {
  329. alac->output_samples_buffer[ch][i] =
  330. get_sbits_long(&alac->gb, alac->sample_size);
  331. }
  332. }
  333. alac->extra_bits = 0;
  334. decorr_shift = 0;
  335. decorr_left_weight = 0;
  336. }
  337. if (alac->extra_bits && alac->extra_bit_bug) {
  338. append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer,
  339. alac->extra_bits, channels, alac->nb_samples);
  340. }
  341. if (channels == 2 && decorr_left_weight) {
  342. decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples,
  343. decorr_shift, decorr_left_weight);
  344. }
  345. if (alac->extra_bits && !alac->extra_bit_bug) {
  346. append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer,
  347. alac->extra_bits, channels, alac->nb_samples);
  348. }
  349. if(av_sample_fmt_is_planar(avctx->sample_fmt)) {
  350. switch(alac->sample_size) {
  351. case 16: {
  352. for (ch = 0; ch < channels; ch++) {
  353. int16_t *outbuffer = (int16_t *)frame->extended_data[ch_index + ch];
  354. for (i = 0; i < alac->nb_samples; i++)
  355. *outbuffer++ = alac->output_samples_buffer[ch][i];
  356. }}
  357. break;
  358. case 24: {
  359. for (ch = 0; ch < channels; ch++) {
  360. for (i = 0; i < alac->nb_samples; i++)
  361. alac->output_samples_buffer[ch][i] <<= 8;
  362. }}
  363. break;
  364. }
  365. }else{
  366. switch(alac->sample_size) {
  367. case 16: {
  368. int16_t *outbuffer = ((int16_t *)frame->extended_data[0]) + ch_index;
  369. for (i = 0; i < alac->nb_samples; i++) {
  370. for (ch = 0; ch < channels; ch++)
  371. *outbuffer++ = alac->output_samples_buffer[ch][i];
  372. outbuffer += alac->channels - channels;
  373. }
  374. }
  375. break;
  376. case 24: {
  377. int32_t *outbuffer = ((int32_t *)frame->extended_data[0]) + ch_index;
  378. for (i = 0; i < alac->nb_samples; i++) {
  379. for (ch = 0; ch < channels; ch++)
  380. *outbuffer++ = alac->output_samples_buffer[ch][i] << 8;
  381. outbuffer += alac->channels - channels;
  382. }
  383. }
  384. break;
  385. case 32: {
  386. int32_t *outbuffer = ((int32_t *)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. }
  395. }
  396. return 0;
  397. }
  398. static int alac_decode_frame(AVCodecContext *avctx, void *data,
  399. int *got_frame_ptr, AVPacket *avpkt)
  400. {
  401. ALACContext *alac = avctx->priv_data;
  402. AVFrame *frame = data;
  403. enum AlacRawDataBlockType element;
  404. int channels;
  405. int ch, ret, got_end;
  406. if ((ret = init_get_bits8(&alac->gb, avpkt->data, avpkt->size)) < 0)
  407. return ret;
  408. got_end = 0;
  409. alac->nb_samples = 0;
  410. ch = 0;
  411. while (get_bits_left(&alac->gb) >= 3) {
  412. element = get_bits(&alac->gb, 3);
  413. if (element == TYPE_END) {
  414. got_end = 1;
  415. break;
  416. }
  417. if (element > TYPE_CPE && element != TYPE_LFE) {
  418. av_log(avctx, AV_LOG_ERROR, "syntax element unsupported: %d\n", element);
  419. return AVERROR_PATCHWELCOME;
  420. }
  421. channels = (element == TYPE_CPE) ? 2 : 1;
  422. if (ch + channels > alac->channels ||
  423. ff_alac_channel_layout_offsets[alac->channels - 1][ch] + channels > alac->channels) {
  424. av_log(avctx, AV_LOG_ERROR, "invalid element channel count\n");
  425. return AVERROR_INVALIDDATA;
  426. }
  427. ret = decode_element(avctx, frame,
  428. ff_alac_channel_layout_offsets[alac->channels - 1][ch],
  429. channels);
  430. if (ret < 0 && get_bits_left(&alac->gb))
  431. return ret;
  432. ch += channels;
  433. }
  434. if (!got_end) {
  435. av_log(avctx, AV_LOG_ERROR, "no end tag found. incomplete packet.\n");
  436. return AVERROR_INVALIDDATA;
  437. }
  438. if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) {
  439. av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n",
  440. avpkt->size * 8 - get_bits_count(&alac->gb));
  441. }
  442. if (alac->channels == ch)
  443. *got_frame_ptr = 1;
  444. else
  445. av_log(avctx, AV_LOG_WARNING, "Failed to decode all channels\n");
  446. return avpkt->size;
  447. }
  448. static av_cold int alac_decode_close(AVCodecContext *avctx)
  449. {
  450. ALACContext *alac = avctx->priv_data;
  451. int ch;
  452. for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
  453. av_freep(&alac->predict_error_buffer[ch]);
  454. if (!alac->direct_output)
  455. av_freep(&alac->output_samples_buffer[ch]);
  456. av_freep(&alac->extra_bits_buffer[ch]);
  457. }
  458. return 0;
  459. }
  460. static int allocate_buffers(ALACContext *alac)
  461. {
  462. int ch;
  463. int buf_size = alac->max_samples_per_frame * sizeof(int32_t);
  464. for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
  465. FF_ALLOC_OR_GOTO(alac->avctx, alac->predict_error_buffer[ch],
  466. buf_size, buf_alloc_fail);
  467. alac->direct_output = alac->sample_size > 16 && av_sample_fmt_is_planar(alac->avctx->sample_fmt);
  468. if (!alac->direct_output) {
  469. FF_ALLOC_OR_GOTO(alac->avctx, alac->output_samples_buffer[ch],
  470. buf_size, buf_alloc_fail);
  471. }
  472. FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch],
  473. buf_size, buf_alloc_fail);
  474. }
  475. return 0;
  476. buf_alloc_fail:
  477. alac_decode_close(alac->avctx);
  478. return AVERROR(ENOMEM);
  479. }
  480. static int alac_set_info(ALACContext *alac)
  481. {
  482. GetByteContext gb;
  483. bytestream2_init(&gb, alac->avctx->extradata,
  484. alac->avctx->extradata_size);
  485. bytestream2_skipu(&gb, 12); // size:4, alac:4, version:4
  486. alac->max_samples_per_frame = bytestream2_get_be32u(&gb);
  487. if (!alac->max_samples_per_frame ||
  488. alac->max_samples_per_frame > INT_MAX / sizeof(int32_t)) {
  489. av_log(alac->avctx, AV_LOG_ERROR,
  490. "max samples per frame invalid: %"PRIu32"\n",
  491. alac->max_samples_per_frame);
  492. return AVERROR_INVALIDDATA;
  493. }
  494. bytestream2_skipu(&gb, 1); // compatible version
  495. alac->sample_size = bytestream2_get_byteu(&gb);
  496. alac->rice_history_mult = bytestream2_get_byteu(&gb);
  497. alac->rice_initial_history = bytestream2_get_byteu(&gb);
  498. alac->rice_limit = bytestream2_get_byteu(&gb);
  499. alac->channels = bytestream2_get_byteu(&gb);
  500. bytestream2_get_be16u(&gb); // maxRun
  501. bytestream2_get_be32u(&gb); // max coded frame size
  502. bytestream2_get_be32u(&gb); // average bitrate
  503. bytestream2_get_be32u(&gb); // samplerate
  504. return 0;
  505. }
  506. static av_cold int alac_decode_init(AVCodecContext * avctx)
  507. {
  508. int ret;
  509. int req_packed;
  510. ALACContext *alac = avctx->priv_data;
  511. alac->avctx = avctx;
  512. /* initialize from the extradata */
  513. if (alac->avctx->extradata_size < ALAC_EXTRADATA_SIZE) {
  514. av_log(avctx, AV_LOG_ERROR, "extradata is too small\n");
  515. return AVERROR_INVALIDDATA;
  516. }
  517. if (alac_set_info(alac)) {
  518. av_log(avctx, AV_LOG_ERROR, "set_info failed\n");
  519. return -1;
  520. }
  521. req_packed = LIBAVCODEC_VERSION_MAJOR < 55 && !av_sample_fmt_is_planar(avctx->request_sample_fmt);
  522. switch (alac->sample_size) {
  523. case 16: avctx->sample_fmt = req_packed ? AV_SAMPLE_FMT_S16 : AV_SAMPLE_FMT_S16P;
  524. break;
  525. case 24:
  526. case 32: avctx->sample_fmt = req_packed ? AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S32P;
  527. break;
  528. default: avpriv_request_sample(avctx, "Sample depth %d", alac->sample_size);
  529. return AVERROR_PATCHWELCOME;
  530. }
  531. avctx->bits_per_raw_sample = alac->sample_size;
  532. if (alac->channels < 1) {
  533. av_log(avctx, AV_LOG_WARNING, "Invalid channel count\n");
  534. alac->channels = avctx->channels;
  535. } else {
  536. if (alac->channels > ALAC_MAX_CHANNELS)
  537. alac->channels = avctx->channels;
  538. else
  539. avctx->channels = alac->channels;
  540. }
  541. if (avctx->channels > ALAC_MAX_CHANNELS || avctx->channels <= 0 ) {
  542. av_log(avctx, AV_LOG_ERROR, "Unsupported channel count: %d\n",
  543. avctx->channels);
  544. return AVERROR_PATCHWELCOME;
  545. }
  546. avctx->channel_layout = ff_alac_channel_layouts[alac->channels - 1];
  547. if ((ret = allocate_buffers(alac)) < 0) {
  548. av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n");
  549. return ret;
  550. }
  551. return 0;
  552. }
  553. static int init_thread_copy(AVCodecContext *avctx)
  554. {
  555. ALACContext *alac = avctx->priv_data;
  556. alac->avctx = avctx;
  557. return allocate_buffers(alac);
  558. }
  559. static const AVOption options[] = {
  560. { "extra_bits_bug", "Force non-standard decoding process",
  561. offsetof(ALACContext, extra_bit_bug), AV_OPT_TYPE_INT, { .i64 = 0 },
  562. 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM },
  563. { NULL },
  564. };
  565. static const AVClass alac_class = {
  566. .class_name = "alac",
  567. .item_name = av_default_item_name,
  568. .option = options,
  569. .version = LIBAVUTIL_VERSION_INT,
  570. };
  571. AVCodec ff_alac_decoder = {
  572. .name = "alac",
  573. .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
  574. .type = AVMEDIA_TYPE_AUDIO,
  575. .id = AV_CODEC_ID_ALAC,
  576. .priv_data_size = sizeof(ALACContext),
  577. .init = alac_decode_init,
  578. .close = alac_decode_close,
  579. .decode = alac_decode_frame,
  580. .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
  581. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  582. .priv_class = &alac_class
  583. };