alac.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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 kmodifier (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 "avcodec.h"
  47. #include "get_bits.h"
  48. #include "bytestream.h"
  49. #include "unary.h"
  50. #include "mathops.h"
  51. #define ALAC_EXTRADATA_SIZE 36
  52. #define MAX_CHANNELS 2
  53. typedef struct {
  54. AVCodecContext *avctx;
  55. AVFrame frame;
  56. GetBitContext gb;
  57. int numchannels;
  58. /* buffers */
  59. int32_t *predicterror_buffer[MAX_CHANNELS];
  60. int32_t *outputsamples_buffer[MAX_CHANNELS];
  61. int32_t *extra_bits_buffer[MAX_CHANNELS];
  62. /* stuff from setinfo */
  63. uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */
  64. uint8_t setinfo_sample_size; /* 0x10 */
  65. uint8_t setinfo_rice_historymult; /* 0x28 */
  66. uint8_t setinfo_rice_initialhistory; /* 0x0a */
  67. uint8_t setinfo_rice_kmodifier; /* 0x0e */
  68. /* end setinfo stuff */
  69. int extra_bits; /**< number of extra bits beyond 16-bit */
  70. } ALACContext;
  71. static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize){
  72. /* read x - number of 1s before 0 represent the rice */
  73. int x = get_unary_0_9(gb);
  74. if (x > 8) { /* RICE THRESHOLD */
  75. /* use alternative encoding */
  76. x = get_bits(gb, readsamplesize);
  77. } else {
  78. if (k >= limit)
  79. k = limit;
  80. if (k != 1) {
  81. int extrabits = show_bits(gb, k);
  82. /* multiply x by 2^k - 1, as part of their strange algorithm */
  83. x = (x << k) - x;
  84. if (extrabits > 1) {
  85. x += extrabits - 1;
  86. skip_bits(gb, k);
  87. } else
  88. skip_bits(gb, k - 1);
  89. }
  90. }
  91. return x;
  92. }
  93. static int bastardized_rice_decompress(ALACContext *alac,
  94. int32_t *output_buffer,
  95. int output_size,
  96. int readsamplesize, /* arg_10 */
  97. int rice_initialhistory, /* arg424->b */
  98. int rice_kmodifier, /* arg424->d */
  99. int rice_historymult, /* arg424->c */
  100. int rice_kmodifier_mask /* arg424->e */
  101. )
  102. {
  103. int output_count;
  104. unsigned int history = rice_initialhistory;
  105. int sign_modifier = 0;
  106. for (output_count = 0; output_count < output_size; output_count++) {
  107. int32_t x;
  108. int32_t x_modified;
  109. int32_t final_val;
  110. /* standard rice encoding */
  111. int k; /* size of extra bits */
  112. if(get_bits_left(&alac->gb) <= 0)
  113. return -1;
  114. /* read k, that is bits as is */
  115. k = av_log2((history >> 9) + 3);
  116. x= decode_scalar(&alac->gb, k, rice_kmodifier, readsamplesize);
  117. x_modified = sign_modifier + x;
  118. final_val = (x_modified + 1) / 2;
  119. if (x_modified & 1) final_val *= -1;
  120. output_buffer[output_count] = final_val;
  121. sign_modifier = 0;
  122. /* now update the history */
  123. history += x_modified * rice_historymult
  124. - ((history * rice_historymult) >> 9);
  125. if (x_modified > 0xffff)
  126. history = 0xffff;
  127. /* special case: there may be compressed blocks of 0 */
  128. if ((history < 128) && (output_count+1 < output_size)) {
  129. int k;
  130. unsigned int block_size;
  131. sign_modifier = 1;
  132. k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */);
  133. block_size= decode_scalar(&alac->gb, k, rice_kmodifier, 16);
  134. if (block_size > 0) {
  135. if(block_size >= output_size - output_count){
  136. av_log(alac->avctx, AV_LOG_ERROR, "invalid zero block size of %d %d %d\n", block_size, output_size, output_count);
  137. block_size= output_size - output_count - 1;
  138. }
  139. memset(&output_buffer[output_count+1], 0, block_size * 4);
  140. output_count += block_size;
  141. }
  142. if (block_size > 0xffff)
  143. sign_modifier = 0;
  144. history = 0;
  145. }
  146. }
  147. return 0;
  148. }
  149. static inline int sign_only(int v)
  150. {
  151. return v ? FFSIGN(v) : 0;
  152. }
  153. static void predictor_decompress_fir_adapt(int32_t *error_buffer,
  154. int32_t *buffer_out,
  155. int output_size,
  156. int readsamplesize,
  157. int16_t *predictor_coef_table,
  158. int predictor_coef_num,
  159. int predictor_quantitization)
  160. {
  161. int i;
  162. /* first sample always copies */
  163. *buffer_out = *error_buffer;
  164. if (!predictor_coef_num) {
  165. if (output_size <= 1)
  166. return;
  167. memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
  168. return;
  169. }
  170. if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
  171. /* second-best case scenario for fir decompression,
  172. * error describes a small difference from the previous sample only
  173. */
  174. if (output_size <= 1)
  175. return;
  176. for (i = 0; i < output_size - 1; i++) {
  177. int32_t prev_value;
  178. int32_t error_value;
  179. prev_value = buffer_out[i];
  180. error_value = error_buffer[i+1];
  181. buffer_out[i+1] =
  182. sign_extend((prev_value + error_value), readsamplesize);
  183. }
  184. return;
  185. }
  186. /* read warm-up samples */
  187. if (predictor_coef_num > 0)
  188. for (i = 0; i < predictor_coef_num; i++) {
  189. int32_t val;
  190. val = buffer_out[i] + error_buffer[i+1];
  191. val = sign_extend(val, readsamplesize);
  192. buffer_out[i+1] = val;
  193. }
  194. /* 4 and 8 are very common cases (the only ones i've seen). these
  195. * should be unrolled and optimized
  196. */
  197. /* general case */
  198. if (predictor_coef_num > 0) {
  199. for (i = predictor_coef_num + 1; i < output_size; i++) {
  200. int j;
  201. int sum = 0;
  202. int outval;
  203. int error_val = error_buffer[i];
  204. for (j = 0; j < predictor_coef_num; j++) {
  205. sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
  206. predictor_coef_table[j];
  207. }
  208. outval = (1 << (predictor_quantitization-1)) + sum;
  209. outval = outval >> predictor_quantitization;
  210. outval = outval + buffer_out[0] + error_val;
  211. outval = sign_extend(outval, readsamplesize);
  212. buffer_out[predictor_coef_num+1] = outval;
  213. if (error_val > 0) {
  214. int predictor_num = predictor_coef_num - 1;
  215. while (predictor_num >= 0 && error_val > 0) {
  216. int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
  217. int sign = sign_only(val);
  218. predictor_coef_table[predictor_num] -= sign;
  219. val *= sign; /* absolute value */
  220. error_val -= ((val >> predictor_quantitization) *
  221. (predictor_coef_num - predictor_num));
  222. predictor_num--;
  223. }
  224. } else if (error_val < 0) {
  225. int predictor_num = predictor_coef_num - 1;
  226. while (predictor_num >= 0 && error_val < 0) {
  227. int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
  228. int sign = - sign_only(val);
  229. predictor_coef_table[predictor_num] -= sign;
  230. val *= sign; /* neg value */
  231. error_val -= ((val >> predictor_quantitization) *
  232. (predictor_coef_num - predictor_num));
  233. predictor_num--;
  234. }
  235. }
  236. buffer_out++;
  237. }
  238. }
  239. }
  240. static void decorrelate_stereo(int32_t *buffer[MAX_CHANNELS],
  241. int numsamples, uint8_t interlacing_shift,
  242. uint8_t interlacing_leftweight)
  243. {
  244. int i;
  245. for (i = 0; i < numsamples; i++) {
  246. int32_t a, b;
  247. a = buffer[0][i];
  248. b = buffer[1][i];
  249. a -= (b * interlacing_leftweight) >> interlacing_shift;
  250. b += a;
  251. buffer[0][i] = b;
  252. buffer[1][i] = a;
  253. }
  254. }
  255. static void append_extra_bits(int32_t *buffer[MAX_CHANNELS],
  256. int32_t *extra_bits_buffer[MAX_CHANNELS],
  257. int extra_bits, int numchannels, int numsamples)
  258. {
  259. int i, ch;
  260. for (ch = 0; ch < numchannels; ch++)
  261. for (i = 0; i < numsamples; i++)
  262. buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i];
  263. }
  264. static void interleave_stereo_16(int32_t *buffer[MAX_CHANNELS],
  265. int16_t *buffer_out, int numsamples)
  266. {
  267. int i;
  268. for (i = 0; i < numsamples; i++) {
  269. *buffer_out++ = buffer[0][i];
  270. *buffer_out++ = buffer[1][i];
  271. }
  272. }
  273. static void interleave_stereo_24(int32_t *buffer[MAX_CHANNELS],
  274. int32_t *buffer_out, int numsamples)
  275. {
  276. int i;
  277. for (i = 0; i < numsamples; i++) {
  278. *buffer_out++ = buffer[0][i] << 8;
  279. *buffer_out++ = buffer[1][i] << 8;
  280. }
  281. }
  282. static void interleave_stereo_32(int32_t *buffer[MAX_CHANNELS],
  283. int32_t *buffer_out, int numsamples)
  284. {
  285. int i;
  286. for (i = 0; i < numsamples; i++) {
  287. *buffer_out++ = buffer[0][i];
  288. *buffer_out++ = buffer[1][i];
  289. }
  290. }
  291. static int alac_decode_frame(AVCodecContext *avctx, void *data,
  292. int *got_frame_ptr, AVPacket *avpkt)
  293. {
  294. const uint8_t *inbuffer = avpkt->data;
  295. int input_buffer_size = avpkt->size;
  296. ALACContext *alac = avctx->priv_data;
  297. int channels;
  298. unsigned int outputsamples;
  299. int hassize;
  300. unsigned int readsamplesize;
  301. int isnotcompressed;
  302. uint8_t interlacing_shift;
  303. uint8_t interlacing_leftweight;
  304. int i, ch, ret;
  305. init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
  306. channels = get_bits(&alac->gb, 3) + 1;
  307. if (channels != avctx->channels) {
  308. av_log(avctx, AV_LOG_ERROR, "frame header channel count mismatch\n");
  309. return AVERROR_INVALIDDATA;
  310. }
  311. /* 2^result = something to do with output waiting.
  312. * perhaps matters if we read > 1 frame in a pass?
  313. */
  314. skip_bits(&alac->gb, 4);
  315. skip_bits(&alac->gb, 12); /* unknown, skip 12 bits */
  316. /* the output sample size is stored soon */
  317. hassize = get_bits1(&alac->gb);
  318. alac->extra_bits = get_bits(&alac->gb, 2) << 3;
  319. /* whether the frame is compressed */
  320. isnotcompressed = get_bits1(&alac->gb);
  321. if (hassize) {
  322. /* now read the number of samples as a 32bit integer */
  323. outputsamples = get_bits_long(&alac->gb, 32);
  324. if(outputsamples > alac->setinfo_max_samples_per_frame){
  325. av_log(avctx, AV_LOG_ERROR, "outputsamples %d > %d\n", outputsamples, alac->setinfo_max_samples_per_frame);
  326. return -1;
  327. }
  328. } else
  329. outputsamples = alac->setinfo_max_samples_per_frame;
  330. /* get output buffer */
  331. if (outputsamples > INT32_MAX) {
  332. av_log(avctx, AV_LOG_ERROR, "unsupported block size: %u\n", outputsamples);
  333. return AVERROR_INVALIDDATA;
  334. }
  335. alac->frame.nb_samples = outputsamples;
  336. if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) {
  337. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  338. return ret;
  339. }
  340. readsamplesize = alac->setinfo_sample_size - alac->extra_bits + channels - 1;
  341. if (readsamplesize > MIN_CACHE_BITS) {
  342. av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize);
  343. return -1;
  344. }
  345. if (!isnotcompressed) {
  346. /* so it is compressed */
  347. int16_t predictor_coef_table[MAX_CHANNELS][32];
  348. int predictor_coef_num[MAX_CHANNELS];
  349. int prediction_type[MAX_CHANNELS];
  350. int prediction_quantitization[MAX_CHANNELS];
  351. int ricemodifier[MAX_CHANNELS];
  352. interlacing_shift = get_bits(&alac->gb, 8);
  353. interlacing_leftweight = get_bits(&alac->gb, 8);
  354. for (ch = 0; ch < channels; ch++) {
  355. prediction_type[ch] = get_bits(&alac->gb, 4);
  356. prediction_quantitization[ch] = get_bits(&alac->gb, 4);
  357. ricemodifier[ch] = get_bits(&alac->gb, 3);
  358. predictor_coef_num[ch] = get_bits(&alac->gb, 5);
  359. /* read the predictor table */
  360. for (i = 0; i < predictor_coef_num[ch]; i++)
  361. predictor_coef_table[ch][i] = (int16_t)get_bits(&alac->gb, 16);
  362. }
  363. if (alac->extra_bits) {
  364. for (i = 0; i < outputsamples; i++) {
  365. if(get_bits_left(&alac->gb) <= 0)
  366. return -1;
  367. for (ch = 0; ch < channels; ch++)
  368. alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);
  369. }
  370. }
  371. for (ch = 0; ch < channels; ch++) {
  372. int ret = bastardized_rice_decompress(alac,
  373. alac->predicterror_buffer[ch],
  374. outputsamples,
  375. readsamplesize,
  376. alac->setinfo_rice_initialhistory,
  377. alac->setinfo_rice_kmodifier,
  378. ricemodifier[ch] * alac->setinfo_rice_historymult / 4,
  379. (1 << alac->setinfo_rice_kmodifier) - 1);
  380. if(ret<0)
  381. return ret;
  382. /* adaptive FIR filter */
  383. if (prediction_type[ch] == 15) {
  384. /* Prediction type 15 runs the adaptive FIR twice.
  385. * The first pass uses the special-case coef_num = 31, while
  386. * the second pass uses the coefs from the bitstream.
  387. *
  388. * However, this prediction type is not currently used by the
  389. * reference encoder.
  390. */
  391. predictor_decompress_fir_adapt(alac->predicterror_buffer[ch],
  392. alac->predicterror_buffer[ch],
  393. outputsamples, readsamplesize,
  394. NULL, 31, 0);
  395. } else if (prediction_type[ch] > 0) {
  396. av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n",
  397. prediction_type[ch]);
  398. }
  399. predictor_decompress_fir_adapt(alac->predicterror_buffer[ch],
  400. alac->outputsamples_buffer[ch],
  401. outputsamples, readsamplesize,
  402. predictor_coef_table[ch],
  403. predictor_coef_num[ch],
  404. prediction_quantitization[ch]);
  405. }
  406. } else {
  407. /* not compressed, easy case */
  408. for (i = 0; i < outputsamples; i++) {
  409. if(get_bits_left(&alac->gb) <= 0)
  410. return -1;
  411. for (ch = 0; ch < channels; ch++) {
  412. alac->outputsamples_buffer[ch][i] = get_sbits_long(&alac->gb,
  413. alac->setinfo_sample_size);
  414. }
  415. }
  416. alac->extra_bits = 0;
  417. interlacing_shift = 0;
  418. interlacing_leftweight = 0;
  419. }
  420. if (get_bits(&alac->gb, 3) != 7)
  421. av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n");
  422. if (channels == 2 && interlacing_leftweight) {
  423. decorrelate_stereo(alac->outputsamples_buffer, outputsamples,
  424. interlacing_shift, interlacing_leftweight);
  425. }
  426. if (alac->extra_bits) {
  427. append_extra_bits(alac->outputsamples_buffer, alac->extra_bits_buffer,
  428. alac->extra_bits, alac->numchannels, outputsamples);
  429. }
  430. switch(alac->setinfo_sample_size) {
  431. case 16:
  432. if (channels == 2) {
  433. interleave_stereo_16(alac->outputsamples_buffer,
  434. (int16_t *)alac->frame.data[0], outputsamples);
  435. } else {
  436. int16_t *outbuffer = (int16_t *)alac->frame.data[0];
  437. for (i = 0; i < outputsamples; i++) {
  438. outbuffer[i] = alac->outputsamples_buffer[0][i];
  439. }
  440. }
  441. break;
  442. case 24:
  443. if (channels == 2) {
  444. interleave_stereo_24(alac->outputsamples_buffer,
  445. (int32_t *)alac->frame.data[0], outputsamples);
  446. } else {
  447. int32_t *outbuffer = (int32_t *)alac->frame.data[0];
  448. for (i = 0; i < outputsamples; i++)
  449. outbuffer[i] = alac->outputsamples_buffer[0][i] << 8;
  450. }
  451. break;
  452. case 32:
  453. if (channels == 2) {
  454. interleave_stereo_32(alac->outputsamples_buffer,
  455. (int32_t *)alac->frame.data[0], outputsamples);
  456. } else {
  457. int32_t *outbuffer = (int32_t *)alac->frame.data[0];
  458. for (i = 0; i < outputsamples; i++)
  459. outbuffer[i] = alac->outputsamples_buffer[0][i];
  460. }
  461. break;
  462. }
  463. if (input_buffer_size * 8 - get_bits_count(&alac->gb) > 8)
  464. av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", input_buffer_size * 8 - get_bits_count(&alac->gb));
  465. *got_frame_ptr = 1;
  466. *(AVFrame *)data = alac->frame;
  467. return input_buffer_size;
  468. }
  469. static av_cold int alac_decode_close(AVCodecContext *avctx)
  470. {
  471. ALACContext *alac = avctx->priv_data;
  472. int ch;
  473. for (ch = 0; ch < alac->numchannels; ch++) {
  474. av_freep(&alac->predicterror_buffer[ch]);
  475. av_freep(&alac->outputsamples_buffer[ch]);
  476. av_freep(&alac->extra_bits_buffer[ch]);
  477. }
  478. return 0;
  479. }
  480. static int allocate_buffers(ALACContext *alac)
  481. {
  482. int ch;
  483. for (ch = 0; ch < alac->numchannels; ch++) {
  484. int buf_size = alac->setinfo_max_samples_per_frame * sizeof(int32_t);
  485. FF_ALLOC_OR_GOTO(alac->avctx, alac->predicterror_buffer[ch],
  486. buf_size, buf_alloc_fail);
  487. FF_ALLOC_OR_GOTO(alac->avctx, alac->outputsamples_buffer[ch],
  488. buf_size, buf_alloc_fail);
  489. FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch],
  490. buf_size, buf_alloc_fail);
  491. }
  492. return 0;
  493. buf_alloc_fail:
  494. alac_decode_close(alac->avctx);
  495. return AVERROR(ENOMEM);
  496. }
  497. static int alac_set_info(ALACContext *alac)
  498. {
  499. GetByteContext gb;
  500. bytestream2_init(&gb, alac->avctx->extradata,
  501. alac->avctx->extradata_size);
  502. bytestream2_skipu(&gb, 12); // size:4, alac:4, version:4
  503. /* buffer size / 2 ? */
  504. alac->setinfo_max_samples_per_frame = bytestream2_get_be32u(&gb);
  505. if (alac->setinfo_max_samples_per_frame >= UINT_MAX/4){
  506. av_log(alac->avctx, AV_LOG_ERROR,
  507. "setinfo_max_samples_per_frame too large\n");
  508. return AVERROR_INVALIDDATA;
  509. }
  510. bytestream2_skipu(&gb, 1); // compatible version
  511. alac->setinfo_sample_size = bytestream2_get_byteu(&gb);
  512. alac->setinfo_rice_historymult = bytestream2_get_byteu(&gb);
  513. alac->setinfo_rice_initialhistory = bytestream2_get_byteu(&gb);
  514. alac->setinfo_rice_kmodifier = bytestream2_get_byteu(&gb);
  515. alac->numchannels = 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. ALACContext *alac = avctx->priv_data;
  526. alac->avctx = avctx;
  527. /* initialize from the extradata */
  528. if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
  529. av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
  530. ALAC_EXTRADATA_SIZE);
  531. return -1;
  532. }
  533. if (alac_set_info(alac)) {
  534. av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
  535. return -1;
  536. }
  537. switch (alac->setinfo_sample_size) {
  538. case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  539. break;
  540. case 32:
  541. case 24: avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  542. break;
  543. default: av_log_ask_for_sample(avctx, "Sample depth %d is not supported.\n",
  544. alac->setinfo_sample_size);
  545. return AVERROR_PATCHWELCOME;
  546. }
  547. if (alac->numchannels < 1) {
  548. av_log(avctx, AV_LOG_WARNING, "Invalid channel count\n");
  549. alac->numchannels = avctx->channels;
  550. } else {
  551. if (alac->numchannels > MAX_CHANNELS)
  552. alac->numchannels = avctx->channels;
  553. else
  554. avctx->channels = alac->numchannels;
  555. }
  556. if (avctx->channels > MAX_CHANNELS) {
  557. av_log(avctx, AV_LOG_ERROR, "Unsupported channel count: %d\n",
  558. avctx->channels);
  559. return AVERROR_PATCHWELCOME;
  560. }
  561. if ((ret = allocate_buffers(alac)) < 0) {
  562. av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n");
  563. return ret;
  564. }
  565. avcodec_get_frame_defaults(&alac->frame);
  566. avctx->coded_frame = &alac->frame;
  567. return 0;
  568. }
  569. AVCodec ff_alac_decoder = {
  570. .name = "alac",
  571. .type = AVMEDIA_TYPE_AUDIO,
  572. .id = CODEC_ID_ALAC,
  573. .priv_data_size = sizeof(ALACContext),
  574. .init = alac_decode_init,
  575. .close = alac_decode_close,
  576. .decode = alac_decode_frame,
  577. .capabilities = CODEC_CAP_DR1,
  578. .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
  579. };