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.

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