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.

932 lines
30KB

  1. /*
  2. * TAK decoder
  3. * Copyright (c) 2012 Paul B Mahol
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. * TAK (Tom's lossless Audio Kompressor) decoder
  24. * @author Paul B Mahol
  25. */
  26. #include "libavutil/internal.h"
  27. #include "libavutil/samplefmt.h"
  28. #include "tak.h"
  29. #include "avcodec.h"
  30. #include "dsputil.h"
  31. #include "internal.h"
  32. #include "unary.h"
  33. #define MAX_SUBFRAMES 8 // max number of subframes per channel
  34. #define MAX_PREDICTORS 256
  35. typedef struct MCDParam {
  36. int8_t present; // decorrelation parameter availability for this channel
  37. int8_t index; // index into array of decorrelation types
  38. int8_t chan1;
  39. int8_t chan2;
  40. } MCDParam;
  41. typedef struct TAKDecContext {
  42. AVCodecContext *avctx; // parent AVCodecContext
  43. DSPContext dsp;
  44. TAKStreamInfo ti;
  45. GetBitContext gb; // bitstream reader initialized to start at the current frame
  46. int uval;
  47. int nb_samples; // number of samples in the current frame
  48. uint8_t *decode_buffer;
  49. unsigned int decode_buffer_size;
  50. int32_t *decoded[TAK_MAX_CHANNELS]; // decoded samples for each channel
  51. int8_t lpc_mode[TAK_MAX_CHANNELS];
  52. int8_t sample_shift[TAK_MAX_CHANNELS]; // shift applied to every sample in the channel
  53. int subframe_scale;
  54. int8_t dmode; // channel decorrelation type in the current frame
  55. MCDParam mcdparams[TAK_MAX_CHANNELS]; // multichannel decorrelation parameters
  56. int16_t *residues;
  57. unsigned int residues_buf_size;
  58. } TAKDecContext;
  59. static const int8_t mc_dmodes[] = { 1, 3, 4, 6, };
  60. static const uint16_t predictor_sizes[] = {
  61. 4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 160, 192, 224, 256, 0,
  62. };
  63. static const struct CParam {
  64. int init;
  65. int escape;
  66. int scale;
  67. int aescape;
  68. int bias;
  69. } xcodes[50] = {
  70. { 0x01, 0x0000001, 0x0000001, 0x0000003, 0x0000008 },
  71. { 0x02, 0x0000003, 0x0000001, 0x0000007, 0x0000006 },
  72. { 0x03, 0x0000005, 0x0000002, 0x000000E, 0x000000D },
  73. { 0x03, 0x0000003, 0x0000003, 0x000000D, 0x0000018 },
  74. { 0x04, 0x000000B, 0x0000004, 0x000001C, 0x0000019 },
  75. { 0x04, 0x0000006, 0x0000006, 0x000001A, 0x0000030 },
  76. { 0x05, 0x0000016, 0x0000008, 0x0000038, 0x0000032 },
  77. { 0x05, 0x000000C, 0x000000C, 0x0000034, 0x0000060 },
  78. { 0x06, 0x000002C, 0x0000010, 0x0000070, 0x0000064 },
  79. { 0x06, 0x0000018, 0x0000018, 0x0000068, 0x00000C0 },
  80. { 0x07, 0x0000058, 0x0000020, 0x00000E0, 0x00000C8 },
  81. { 0x07, 0x0000030, 0x0000030, 0x00000D0, 0x0000180 },
  82. { 0x08, 0x00000B0, 0x0000040, 0x00001C0, 0x0000190 },
  83. { 0x08, 0x0000060, 0x0000060, 0x00001A0, 0x0000300 },
  84. { 0x09, 0x0000160, 0x0000080, 0x0000380, 0x0000320 },
  85. { 0x09, 0x00000C0, 0x00000C0, 0x0000340, 0x0000600 },
  86. { 0x0A, 0x00002C0, 0x0000100, 0x0000700, 0x0000640 },
  87. { 0x0A, 0x0000180, 0x0000180, 0x0000680, 0x0000C00 },
  88. { 0x0B, 0x0000580, 0x0000200, 0x0000E00, 0x0000C80 },
  89. { 0x0B, 0x0000300, 0x0000300, 0x0000D00, 0x0001800 },
  90. { 0x0C, 0x0000B00, 0x0000400, 0x0001C00, 0x0001900 },
  91. { 0x0C, 0x0000600, 0x0000600, 0x0001A00, 0x0003000 },
  92. { 0x0D, 0x0001600, 0x0000800, 0x0003800, 0x0003200 },
  93. { 0x0D, 0x0000C00, 0x0000C00, 0x0003400, 0x0006000 },
  94. { 0x0E, 0x0002C00, 0x0001000, 0x0007000, 0x0006400 },
  95. { 0x0E, 0x0001800, 0x0001800, 0x0006800, 0x000C000 },
  96. { 0x0F, 0x0005800, 0x0002000, 0x000E000, 0x000C800 },
  97. { 0x0F, 0x0003000, 0x0003000, 0x000D000, 0x0018000 },
  98. { 0x10, 0x000B000, 0x0004000, 0x001C000, 0x0019000 },
  99. { 0x10, 0x0006000, 0x0006000, 0x001A000, 0x0030000 },
  100. { 0x11, 0x0016000, 0x0008000, 0x0038000, 0x0032000 },
  101. { 0x11, 0x000C000, 0x000C000, 0x0034000, 0x0060000 },
  102. { 0x12, 0x002C000, 0x0010000, 0x0070000, 0x0064000 },
  103. { 0x12, 0x0018000, 0x0018000, 0x0068000, 0x00C0000 },
  104. { 0x13, 0x0058000, 0x0020000, 0x00E0000, 0x00C8000 },
  105. { 0x13, 0x0030000, 0x0030000, 0x00D0000, 0x0180000 },
  106. { 0x14, 0x00B0000, 0x0040000, 0x01C0000, 0x0190000 },
  107. { 0x14, 0x0060000, 0x0060000, 0x01A0000, 0x0300000 },
  108. { 0x15, 0x0160000, 0x0080000, 0x0380000, 0x0320000 },
  109. { 0x15, 0x00C0000, 0x00C0000, 0x0340000, 0x0600000 },
  110. { 0x16, 0x02C0000, 0x0100000, 0x0700000, 0x0640000 },
  111. { 0x16, 0x0180000, 0x0180000, 0x0680000, 0x0C00000 },
  112. { 0x17, 0x0580000, 0x0200000, 0x0E00000, 0x0C80000 },
  113. { 0x17, 0x0300000, 0x0300000, 0x0D00000, 0x1800000 },
  114. { 0x18, 0x0B00000, 0x0400000, 0x1C00000, 0x1900000 },
  115. { 0x18, 0x0600000, 0x0600000, 0x1A00000, 0x3000000 },
  116. { 0x19, 0x1600000, 0x0800000, 0x3800000, 0x3200000 },
  117. { 0x19, 0x0C00000, 0x0C00000, 0x3400000, 0x6000000 },
  118. { 0x1A, 0x2C00000, 0x1000000, 0x7000000, 0x6400000 },
  119. { 0x1A, 0x1800000, 0x1800000, 0x6800000, 0xC000000 },
  120. };
  121. static av_cold void tak_init_static_data(AVCodec *codec)
  122. {
  123. ff_tak_init_crc();
  124. }
  125. static int set_bps_params(AVCodecContext *avctx)
  126. {
  127. switch (avctx->bits_per_coded_sample) {
  128. case 8:
  129. avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
  130. break;
  131. case 16:
  132. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  133. break;
  134. case 24:
  135. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  136. break;
  137. default:
  138. av_log(avctx, AV_LOG_ERROR, "unsupported bits per sample: %d\n",
  139. avctx->bits_per_coded_sample);
  140. return AVERROR_INVALIDDATA;
  141. }
  142. avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
  143. return 0;
  144. }
  145. static void set_sample_rate_params(AVCodecContext *avctx)
  146. {
  147. TAKDecContext *s = avctx->priv_data;
  148. int shift = 3 - (avctx->sample_rate / 11025);
  149. shift = FFMAX(0, shift);
  150. s->uval = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << shift;
  151. s->subframe_scale = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << 1;
  152. }
  153. static av_cold int tak_decode_init(AVCodecContext *avctx)
  154. {
  155. TAKDecContext *s = avctx->priv_data;
  156. ff_dsputil_init(&s->dsp, avctx);
  157. s->avctx = avctx;
  158. set_sample_rate_params(avctx);
  159. return set_bps_params(avctx);
  160. }
  161. static void decode_lpc(int32_t *coeffs, int mode, int length)
  162. {
  163. int i;
  164. if (length < 2)
  165. return;
  166. if (mode == 1) {
  167. int a1 = *coeffs++;
  168. for (i = 0; i < length - 1 >> 1; i++) {
  169. *coeffs += a1;
  170. coeffs[1] += *coeffs;
  171. a1 = coeffs[1];
  172. coeffs += 2;
  173. }
  174. if (length - 1 & 1)
  175. *coeffs += a1;
  176. } else if (mode == 2) {
  177. int a1 = coeffs[1];
  178. int a2 = a1 + *coeffs;
  179. coeffs[1] = a2;
  180. if (length > 2) {
  181. coeffs += 2;
  182. for (i = 0; i < length - 2 >> 1; i++) {
  183. int a3 = *coeffs + a1;
  184. int a4 = a3 + a2;
  185. *coeffs = a4;
  186. a1 = coeffs[1] + a3;
  187. a2 = a1 + a4;
  188. coeffs[1] = a2;
  189. coeffs += 2;
  190. }
  191. if (length & 1)
  192. *coeffs += a1 + a2;
  193. }
  194. } else if (mode == 3) {
  195. int a1 = coeffs[1];
  196. int a2 = a1 + *coeffs;
  197. coeffs[1] = a2;
  198. if (length > 2) {
  199. int a3 = coeffs[2];
  200. int a4 = a3 + a1;
  201. int a5 = a4 + a2;
  202. coeffs += 3;
  203. for (i = 0; i < length - 3; i++) {
  204. a3 += *coeffs;
  205. a4 += a3;
  206. a5 += a4;
  207. *coeffs = a5;
  208. coeffs++;
  209. }
  210. }
  211. }
  212. }
  213. static int decode_segment(GetBitContext *gb, int mode, int32_t *decoded,
  214. int len)
  215. {
  216. struct CParam code;
  217. int i;
  218. if (!mode) {
  219. memset(decoded, 0, len * sizeof(*decoded));
  220. return 0;
  221. }
  222. if (mode > FF_ARRAY_ELEMS(xcodes))
  223. return AVERROR_INVALIDDATA;
  224. code = xcodes[mode - 1];
  225. for (i = 0; i < len; i++) {
  226. int x = get_bits_long(gb, code.init);
  227. if (x >= code.escape && get_bits1(gb)) {
  228. x |= 1 << code.init;
  229. if (x >= code.aescape) {
  230. int scale = get_unary(gb, 1, 9);
  231. if (scale == 9) {
  232. int scale_bits = get_bits(gb, 3);
  233. if (scale_bits > 0) {
  234. if (scale_bits == 7) {
  235. scale_bits += get_bits(gb, 5);
  236. if (scale_bits > 29)
  237. return AVERROR_INVALIDDATA;
  238. }
  239. scale = get_bits_long(gb, scale_bits) + 1;
  240. x += code.scale * scale;
  241. }
  242. x += code.bias;
  243. } else
  244. x += code.scale * scale - code.escape;
  245. } else
  246. x -= code.escape;
  247. }
  248. decoded[i] = (x >> 1) ^ -(x & 1);
  249. }
  250. return 0;
  251. }
  252. static int decode_residues(TAKDecContext *s, int32_t *decoded, int length)
  253. {
  254. GetBitContext *gb = &s->gb;
  255. int i, mode, ret;
  256. if (length > s->nb_samples)
  257. return AVERROR_INVALIDDATA;
  258. if (get_bits1(gb)) {
  259. int wlength, rval;
  260. int coding_mode[128];
  261. wlength = length / s->uval;
  262. rval = length - (wlength * s->uval);
  263. if (rval < s->uval / 2)
  264. rval += s->uval;
  265. else
  266. wlength++;
  267. if (wlength <= 1 || wlength > 128)
  268. return AVERROR_INVALIDDATA;
  269. coding_mode[0] = mode = get_bits(gb, 6);
  270. for (i = 1; i < wlength; i++) {
  271. int c = get_unary(gb, 1, 6);
  272. switch (c) {
  273. case 6:
  274. mode = get_bits(gb, 6);
  275. break;
  276. case 5:
  277. case 4:
  278. case 3: {
  279. /* mode += sign ? (1 - c) : (c - 1) */
  280. int sign = get_bits1(gb);
  281. mode += (-sign ^ (c - 1)) + sign;
  282. break;
  283. }
  284. case 2:
  285. mode++;
  286. break;
  287. case 1:
  288. mode--;
  289. break;
  290. }
  291. coding_mode[i] = mode;
  292. }
  293. i = 0;
  294. while (i < wlength) {
  295. int len = 0;
  296. mode = coding_mode[i];
  297. do {
  298. if (i >= wlength - 1)
  299. len += rval;
  300. else
  301. len += s->uval;
  302. i++;
  303. if (i == wlength)
  304. break;
  305. } while (coding_mode[i] == mode);
  306. if ((ret = decode_segment(gb, mode, decoded, len)) < 0)
  307. return ret;
  308. decoded += len;
  309. }
  310. } else {
  311. mode = get_bits(gb, 6);
  312. if ((ret = decode_segment(gb, mode, decoded, length)) < 0)
  313. return ret;
  314. }
  315. return 0;
  316. }
  317. static int get_bits_esc4(GetBitContext *gb)
  318. {
  319. if (get_bits1(gb))
  320. return get_bits(gb, 4) + 1;
  321. else
  322. return 0;
  323. }
  324. static void decode_filter_coeffs(TAKDecContext *s, int filter_order, int size,
  325. int filter_quant, int16_t *filter)
  326. {
  327. GetBitContext *gb = &s->gb;
  328. int i, j, a, b;
  329. int filter_tmp[MAX_PREDICTORS];
  330. int16_t predictors[MAX_PREDICTORS];
  331. predictors[0] = get_sbits(gb, 10);
  332. predictors[1] = get_sbits(gb, 10);
  333. predictors[2] = get_sbits(gb, size) << (10 - size);
  334. predictors[3] = get_sbits(gb, size) << (10 - size);
  335. if (filter_order > 4) {
  336. int av_uninit(code_size);
  337. int code_size_base = size - get_bits1(gb);
  338. for (i = 4; i < filter_order; i++) {
  339. if (!(i & 3))
  340. code_size = code_size_base - get_bits(gb, 2);
  341. predictors[i] = get_sbits(gb, code_size) << (10 - size);
  342. }
  343. }
  344. filter_tmp[0] = predictors[0] << 6;
  345. for (i = 1; i < filter_order; i++) {
  346. int *p1 = &filter_tmp[0];
  347. int *p2 = &filter_tmp[i - 1];
  348. for (j = 0; j < (i + 1) / 2; j++) {
  349. int tmp = *p1 + (predictors[i] * *p2 + 256 >> 9);
  350. *p2 = *p2 + (predictors[i] * *p1 + 256 >> 9);
  351. *p1 = tmp;
  352. p1++;
  353. p2--;
  354. }
  355. filter_tmp[i] = predictors[i] << 6;
  356. }
  357. a = 1 << (32 - (15 - filter_quant));
  358. b = 1 << ((15 - filter_quant) - 1);
  359. for (i = 0, j = filter_order - 1; i < filter_order / 2; i++, j--) {
  360. filter[j] = a - ((filter_tmp[i] + b) >> (15 - filter_quant));
  361. filter[i] = a - ((filter_tmp[j] + b) >> (15 - filter_quant));
  362. }
  363. }
  364. static int decode_subframe(TAKDecContext *s, int32_t *decoded,
  365. int subframe_size, int prev_subframe_size)
  366. {
  367. LOCAL_ALIGNED_16(int16_t, filter, [MAX_PREDICTORS]);
  368. GetBitContext *gb = &s->gb;
  369. int i, ret;
  370. int dshift, size, filter_quant, filter_order;
  371. memset(filter, 0, MAX_PREDICTORS * sizeof(*filter));
  372. if (!get_bits1(gb))
  373. return decode_residues(s, decoded, subframe_size);
  374. filter_order = predictor_sizes[get_bits(gb, 4)];
  375. if (prev_subframe_size > 0 && get_bits1(gb)) {
  376. if (filter_order > prev_subframe_size)
  377. return AVERROR_INVALIDDATA;
  378. decoded -= filter_order;
  379. subframe_size += filter_order;
  380. if (filter_order > subframe_size)
  381. return AVERROR_INVALIDDATA;
  382. } else {
  383. int lpc_mode;
  384. if (filter_order > subframe_size)
  385. return AVERROR_INVALIDDATA;
  386. lpc_mode = get_bits(gb, 2);
  387. if (lpc_mode > 2)
  388. return AVERROR_INVALIDDATA;
  389. if ((ret = decode_residues(s, decoded, filter_order)) < 0)
  390. return ret;
  391. if (lpc_mode)
  392. decode_lpc(decoded, lpc_mode, filter_order);
  393. }
  394. dshift = get_bits_esc4(gb);
  395. size = get_bits1(gb) + 6;
  396. filter_quant = 10;
  397. if (get_bits1(gb)) {
  398. filter_quant -= get_bits(gb, 3) + 1;
  399. if (filter_quant < 3)
  400. return AVERROR_INVALIDDATA;
  401. }
  402. decode_filter_coeffs(s, filter_order, size, filter_quant, filter);
  403. if ((ret = decode_residues(s, &decoded[filter_order],
  404. subframe_size - filter_order)) < 0)
  405. return ret;
  406. av_fast_malloc(&s->residues, &s->residues_buf_size,
  407. FFALIGN(subframe_size + 16, 16) * sizeof(*s->residues));
  408. if (!s->residues)
  409. return AVERROR(ENOMEM);
  410. memset(s->residues, 0, s->residues_buf_size);
  411. for (i = 0; i < filter_order; i++)
  412. s->residues[i] = *decoded++ >> dshift;
  413. for (i = 0; i < subframe_size - filter_order; i++) {
  414. int v = 1 << (filter_quant - 1);
  415. v += s->dsp.scalarproduct_int16(&s->residues[i], filter,
  416. FFALIGN(filter_order, 16));
  417. v = (av_clip(v >> filter_quant, -8192, 8191) << dshift) - *decoded;
  418. *decoded++ = v;
  419. s->residues[filter_order + i] = v >> dshift;
  420. }
  421. emms_c();
  422. return 0;
  423. }
  424. static int decode_channel(TAKDecContext *s, int chan)
  425. {
  426. AVCodecContext *avctx = s->avctx;
  427. GetBitContext *gb = &s->gb;
  428. int32_t *decoded = s->decoded[chan];
  429. int left = s->nb_samples - 1;
  430. int i, prev, ret, nb_subframes;
  431. int subframe_len[MAX_SUBFRAMES];
  432. s->sample_shift[chan] = get_bits_esc4(gb);
  433. if (s->sample_shift[chan] >= avctx->bits_per_coded_sample)
  434. return AVERROR_INVALIDDATA;
  435. /* NOTE: TAK 2.2.0 appears to set the sample value to 0 if
  436. * bits_per_coded_sample - sample_shift is 1, but this produces
  437. * non-bit-exact output. Reading the 1 bit using get_sbits() instead
  438. * of skipping it produces bit-exact output. This has been reported
  439. * to the TAK author. */
  440. *decoded++ = get_sbits(gb,
  441. avctx->bits_per_coded_sample -
  442. s->sample_shift[chan]);
  443. s->lpc_mode[chan] = get_bits(gb, 2);
  444. nb_subframes = get_bits(gb, 3) + 1;
  445. i = 0;
  446. if (nb_subframes > 1) {
  447. if (get_bits_left(gb) < (nb_subframes - 1) * 6)
  448. return AVERROR_INVALIDDATA;
  449. prev = 0;
  450. for (; i < nb_subframes - 1; i++) {
  451. int subframe_end = get_bits(gb, 6) * s->subframe_scale;
  452. if (subframe_end <= prev)
  453. return AVERROR_INVALIDDATA;
  454. subframe_len[i] = subframe_end - prev;
  455. left -= subframe_len[i];
  456. prev = subframe_end;
  457. }
  458. if (left <= 0)
  459. return AVERROR_INVALIDDATA;
  460. }
  461. subframe_len[i] = left;
  462. prev = 0;
  463. for (i = 0; i < nb_subframes; i++) {
  464. if ((ret = decode_subframe(s, decoded, subframe_len[i], prev)) < 0)
  465. return ret;
  466. decoded += subframe_len[i];
  467. prev = subframe_len[i];
  468. }
  469. return 0;
  470. }
  471. static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
  472. {
  473. GetBitContext *gb = &s->gb;
  474. int32_t *p1 = s->decoded[c1] + 1;
  475. int32_t *p2 = s->decoded[c2] + 1;
  476. int i;
  477. int dshift, dfactor;
  478. switch (s->dmode) {
  479. case 1: /* left/side */
  480. for (i = 0; i < length; i++) {
  481. int32_t a = p1[i];
  482. int32_t b = p2[i];
  483. p2[i] = a + b;
  484. }
  485. break;
  486. case 2: /* side/right */
  487. for (i = 0; i < length; i++) {
  488. int32_t a = p1[i];
  489. int32_t b = p2[i];
  490. p1[i] = b - a;
  491. }
  492. break;
  493. case 3: /* side/mid */
  494. for (i = 0; i < length; i++) {
  495. int32_t a = p1[i];
  496. int32_t b = p2[i];
  497. a -= b >> 1;
  498. p1[i] = a;
  499. p2[i] = a + b;
  500. }
  501. break;
  502. case 4: /* side/left with scale factor */
  503. FFSWAP(int32_t*, p1, p2);
  504. case 5: /* side/right with scale factor */
  505. dshift = get_bits_esc4(gb);
  506. dfactor = get_sbits(gb, 10);
  507. for (i = 0; i < length; i++) {
  508. int32_t a = p1[i];
  509. int32_t b = p2[i];
  510. b = dfactor * (b >> dshift) + 128 >> 8 << dshift;
  511. p1[i] = b - a;
  512. }
  513. break;
  514. case 6:
  515. FFSWAP(int32_t*, p1, p2);
  516. case 7: {
  517. LOCAL_ALIGNED_16(int16_t, filter, [MAX_PREDICTORS]);
  518. int length2, order_half, filter_order, dval1, dval2;
  519. int av_uninit(code_size);
  520. memset(filter, 0, MAX_PREDICTORS * sizeof(*filter));
  521. if (length < 256)
  522. return AVERROR_INVALIDDATA;
  523. dshift = get_bits_esc4(gb);
  524. filter_order = 8 << get_bits1(gb);
  525. dval1 = get_bits1(gb);
  526. dval2 = get_bits1(gb);
  527. for (i = 0; i < filter_order; i++) {
  528. if (!(i & 3))
  529. code_size = 14 - get_bits(gb, 3);
  530. filter[i] = get_sbits(gb, code_size);
  531. }
  532. order_half = filter_order / 2;
  533. length2 = length - (filter_order - 1);
  534. /* decorrelate beginning samples */
  535. if (dval1) {
  536. for (i = 0; i < order_half; i++) {
  537. int32_t a = p1[i];
  538. int32_t b = p2[i];
  539. p1[i] = a + b;
  540. }
  541. }
  542. /* decorrelate ending samples */
  543. if (dval2) {
  544. for (i = length2 + order_half; i < length; i++) {
  545. int32_t a = p1[i];
  546. int32_t b = p2[i];
  547. p1[i] = a + b;
  548. }
  549. }
  550. av_fast_malloc(&s->residues, &s->residues_buf_size,
  551. FFALIGN(length + 16, 16) * sizeof(*s->residues));
  552. if (!s->residues)
  553. return AVERROR(ENOMEM);
  554. memset(s->residues, 0, s->residues_buf_size);
  555. for (i = 0; i < length; i++)
  556. s->residues[i] = p2[i] >> dshift;
  557. p1 += order_half;
  558. for (i = 0; i < length2; i++) {
  559. int v = 1 << 9;
  560. v += s->dsp.scalarproduct_int16(&s->residues[i], filter,
  561. FFALIGN(filter_order, 16));
  562. p1[i] = (av_clip(v >> 10, -8192, 8191) << dshift) - p1[i];
  563. }
  564. emms_c();
  565. break;
  566. }
  567. }
  568. return 0;
  569. }
  570. static int tak_decode_frame(AVCodecContext *avctx, void *data,
  571. int *got_frame_ptr, AVPacket *pkt)
  572. {
  573. TAKDecContext *s = avctx->priv_data;
  574. AVFrame *frame = data;
  575. GetBitContext *gb = &s->gb;
  576. int chan, i, ret, hsize;
  577. if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
  578. return AVERROR_INVALIDDATA;
  579. init_get_bits(gb, pkt->data, pkt->size * 8);
  580. if ((ret = ff_tak_decode_frame_header(avctx, gb, &s->ti, 0)) < 0)
  581. return ret;
  582. if (s->ti.flags & TAK_FRAME_FLAG_HAS_METADATA) {
  583. avpriv_request_sample(avctx, "Frame metadata");
  584. return AVERROR_PATCHWELCOME;
  585. }
  586. hsize = get_bits_count(gb) / 8;
  587. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  588. if (ff_tak_check_crc(pkt->data, hsize)) {
  589. av_log(avctx, AV_LOG_ERROR, "CRC error\n");
  590. return AVERROR_INVALIDDATA;
  591. }
  592. }
  593. if (s->ti.codec != TAK_CODEC_MONO_STEREO &&
  594. s->ti.codec != TAK_CODEC_MULTICHANNEL) {
  595. av_log(avctx, AV_LOG_ERROR, "unsupported codec: %d\n", s->ti.codec);
  596. return AVERROR_PATCHWELCOME;
  597. }
  598. if (s->ti.data_type) {
  599. av_log(avctx, AV_LOG_ERROR,
  600. "unsupported data type: %d\n", s->ti.data_type);
  601. return AVERROR_INVALIDDATA;
  602. }
  603. if (s->ti.codec == TAK_CODEC_MONO_STEREO && s->ti.channels > 2) {
  604. av_log(avctx, AV_LOG_ERROR,
  605. "invalid number of channels: %d\n", s->ti.channels);
  606. return AVERROR_INVALIDDATA;
  607. }
  608. if (s->ti.channels > 6) {
  609. av_log(avctx, AV_LOG_ERROR,
  610. "unsupported number of channels: %d\n", s->ti.channels);
  611. return AVERROR_INVALIDDATA;
  612. }
  613. if (s->ti.frame_samples <= 0) {
  614. av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of samples\n");
  615. return AVERROR_INVALIDDATA;
  616. }
  617. if (s->ti.bps != avctx->bits_per_coded_sample) {
  618. avctx->bits_per_coded_sample = s->ti.bps;
  619. if ((ret = set_bps_params(avctx)) < 0)
  620. return ret;
  621. }
  622. if (s->ti.sample_rate != avctx->sample_rate) {
  623. avctx->sample_rate = s->ti.sample_rate;
  624. set_sample_rate_params(avctx);
  625. }
  626. if (s->ti.ch_layout)
  627. avctx->channel_layout = s->ti.ch_layout;
  628. avctx->channels = s->ti.channels;
  629. s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples
  630. : s->ti.frame_samples;
  631. frame->nb_samples = s->nb_samples;
  632. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  633. return ret;
  634. if (avctx->bits_per_coded_sample <= 16) {
  635. int buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  636. s->nb_samples,
  637. AV_SAMPLE_FMT_S32P, 0);
  638. av_fast_malloc(&s->decode_buffer, &s->decode_buffer_size, buf_size);
  639. if (!s->decode_buffer)
  640. return AVERROR(ENOMEM);
  641. ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
  642. s->decode_buffer, avctx->channels,
  643. s->nb_samples, AV_SAMPLE_FMT_S32P, 0);
  644. if (ret < 0)
  645. return ret;
  646. } else {
  647. for (chan = 0; chan < avctx->channels; chan++)
  648. s->decoded[chan] = (int32_t *)frame->extended_data[chan];
  649. }
  650. if (s->nb_samples < 16) {
  651. for (chan = 0; chan < avctx->channels; chan++) {
  652. int32_t *decoded = s->decoded[chan];
  653. for (i = 0; i < s->nb_samples; i++)
  654. decoded[i] = get_sbits(gb, avctx->bits_per_coded_sample);
  655. }
  656. } else {
  657. if (s->ti.codec == TAK_CODEC_MONO_STEREO) {
  658. for (chan = 0; chan < avctx->channels; chan++)
  659. if (ret = decode_channel(s, chan))
  660. return ret;
  661. if (avctx->channels == 2) {
  662. if (get_bits1(gb)) {
  663. // some kind of subframe length, but it seems to be unused
  664. skip_bits(gb, 6);
  665. }
  666. s->dmode = get_bits(gb, 3);
  667. if (ret = decorrelate(s, 0, 1, s->nb_samples - 1))
  668. return ret;
  669. }
  670. } else if (s->ti.codec == TAK_CODEC_MULTICHANNEL) {
  671. if (get_bits1(gb)) {
  672. int ch_mask = 0;
  673. chan = get_bits(gb, 4) + 1;
  674. if (chan > avctx->channels)
  675. return AVERROR_INVALIDDATA;
  676. for (i = 0; i < chan; i++) {
  677. int nbit = get_bits(gb, 4);
  678. if (nbit >= avctx->channels)
  679. return AVERROR_INVALIDDATA;
  680. if (ch_mask & 1 << nbit)
  681. return AVERROR_INVALIDDATA;
  682. s->mcdparams[i].present = get_bits1(gb);
  683. if (s->mcdparams[i].present) {
  684. s->mcdparams[i].index = get_bits(gb, 2);
  685. s->mcdparams[i].chan2 = get_bits(gb, 4);
  686. if (s->mcdparams[i].index == 1) {
  687. if ((nbit == s->mcdparams[i].chan2) ||
  688. (ch_mask & 1 << s->mcdparams[i].chan2))
  689. return AVERROR_INVALIDDATA;
  690. ch_mask |= 1 << s->mcdparams[i].chan2;
  691. } else if (!(ch_mask & 1 << s->mcdparams[i].chan2)) {
  692. return AVERROR_INVALIDDATA;
  693. }
  694. }
  695. s->mcdparams[i].chan1 = nbit;
  696. ch_mask |= 1 << nbit;
  697. }
  698. } else {
  699. chan = avctx->channels;
  700. for (i = 0; i < chan; i++) {
  701. s->mcdparams[i].present = 0;
  702. s->mcdparams[i].chan1 = i;
  703. }
  704. }
  705. for (i = 0; i < chan; i++) {
  706. if (s->mcdparams[i].present && s->mcdparams[i].index == 1)
  707. if (ret = decode_channel(s, s->mcdparams[i].chan2))
  708. return ret;
  709. if (ret = decode_channel(s, s->mcdparams[i].chan1))
  710. return ret;
  711. if (s->mcdparams[i].present) {
  712. s->dmode = mc_dmodes[s->mcdparams[i].index];
  713. if (ret = decorrelate(s,
  714. s->mcdparams[i].chan2,
  715. s->mcdparams[i].chan1,
  716. s->nb_samples - 1))
  717. return ret;
  718. }
  719. }
  720. }
  721. for (chan = 0; chan < avctx->channels; chan++) {
  722. int32_t *decoded = s->decoded[chan];
  723. if (s->lpc_mode[chan])
  724. decode_lpc(decoded, s->lpc_mode[chan], s->nb_samples);
  725. if (s->sample_shift[chan] > 0)
  726. for (i = 0; i < s->nb_samples; i++)
  727. decoded[i] <<= s->sample_shift[chan];
  728. }
  729. }
  730. align_get_bits(gb);
  731. skip_bits(gb, 24);
  732. if (get_bits_left(gb) < 0)
  733. av_log(avctx, AV_LOG_DEBUG, "overread\n");
  734. else if (get_bits_left(gb) > 0)
  735. av_log(avctx, AV_LOG_DEBUG, "underread\n");
  736. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  737. if (ff_tak_check_crc(pkt->data + hsize,
  738. get_bits_count(gb) / 8 - hsize)) {
  739. av_log(avctx, AV_LOG_ERROR, "CRC error\n");
  740. return AVERROR_INVALIDDATA;
  741. }
  742. }
  743. /* convert to output buffer */
  744. switch (avctx->sample_fmt) {
  745. case AV_SAMPLE_FMT_U8P:
  746. for (chan = 0; chan < avctx->channels; chan++) {
  747. uint8_t *samples = (uint8_t *)frame->extended_data[chan];
  748. int32_t *decoded = s->decoded[chan];
  749. for (i = 0; i < s->nb_samples; i++)
  750. samples[i] = decoded[i] + 0x80;
  751. }
  752. break;
  753. case AV_SAMPLE_FMT_S16P:
  754. for (chan = 0; chan < avctx->channels; chan++) {
  755. int16_t *samples = (int16_t *)frame->extended_data[chan];
  756. int32_t *decoded = s->decoded[chan];
  757. for (i = 0; i < s->nb_samples; i++)
  758. samples[i] = decoded[i];
  759. }
  760. break;
  761. case AV_SAMPLE_FMT_S32P:
  762. for (chan = 0; chan < avctx->channels; chan++) {
  763. int32_t *samples = (int32_t *)frame->extended_data[chan];
  764. for (i = 0; i < s->nb_samples; i++)
  765. samples[i] <<= 8;
  766. }
  767. break;
  768. }
  769. *got_frame_ptr = 1;
  770. return pkt->size;
  771. }
  772. static av_cold int tak_decode_close(AVCodecContext *avctx)
  773. {
  774. TAKDecContext *s = avctx->priv_data;
  775. av_freep(&s->decode_buffer);
  776. av_freep(&s->residues);
  777. return 0;
  778. }
  779. AVCodec ff_tak_decoder = {
  780. .name = "tak",
  781. .long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
  782. .type = AVMEDIA_TYPE_AUDIO,
  783. .id = AV_CODEC_ID_TAK,
  784. .priv_data_size = sizeof(TAKDecContext),
  785. .init = tak_decode_init,
  786. .init_static_data = tak_init_static_data,
  787. .close = tak_decode_close,
  788. .decode = tak_decode_frame,
  789. .capabilities = CODEC_CAP_DR1,
  790. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
  791. AV_SAMPLE_FMT_S16P,
  792. AV_SAMPLE_FMT_S32P,
  793. AV_SAMPLE_FMT_NONE },
  794. };