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.

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