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.

986 lines
31KB

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