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.

973 lines
30KB

  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 void tak_set_bps(AVCodecContext *avctx)
  136. {
  137. switch (avctx->bits_per_coded_sample) {
  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. }
  148. }
  149. static int get_shift(int sample_rate)
  150. {
  151. int shift;
  152. if (sample_rate < 11025)
  153. shift = 3;
  154. else if (sample_rate < 22050)
  155. shift = 2;
  156. else if (sample_rate < 44100)
  157. shift = 1;
  158. else
  159. shift = 0;
  160. return shift;
  161. }
  162. static int get_scale(int sample_rate, int shift)
  163. {
  164. return FFALIGN(sample_rate + 511 >> 9, 4) << shift;
  165. }
  166. static av_cold int tak_decode_init(AVCodecContext *avctx)
  167. {
  168. TAKDecContext *s = avctx->priv_data;
  169. ff_tak_init_crc();
  170. ff_dsputil_init(&s->dsp, avctx);
  171. s->avctx = avctx;
  172. avcodec_get_frame_defaults(&s->frame);
  173. avctx->coded_frame = &s->frame;
  174. s->uval = get_scale(avctx->sample_rate, get_shift(avctx->sample_rate));
  175. s->subframe_scale = get_scale(avctx->sample_rate, 1);
  176. tak_set_bps(avctx);
  177. return 0;
  178. }
  179. static int get_code(GetBitContext *gb, int nbits)
  180. {
  181. if (nbits == 1) {
  182. skip_bits1(gb);
  183. return 0;
  184. } else {
  185. return get_sbits(gb, nbits);
  186. }
  187. }
  188. static void decode_lpc(int32_t *coeffs, int mode, int length)
  189. {
  190. int i, a1, a2, a3, a4, a5;
  191. if (length < 2)
  192. return;
  193. if (mode == 1) {
  194. a1 = *coeffs++;
  195. for (i = 0; i < (length - 1 >> 1); i++) {
  196. *coeffs += a1;
  197. coeffs[1] += *coeffs;
  198. a1 = coeffs[1];
  199. coeffs += 2;
  200. }
  201. if ((length - 1) & 1)
  202. *coeffs += a1;
  203. } else if (mode == 2) {
  204. a1 = coeffs[1];
  205. a2 = a1 + *coeffs;
  206. coeffs[1] = a2;
  207. if (length > 2) {
  208. coeffs += 2;
  209. for (i = 0; i < (length - 2 >> 1); i++) {
  210. a3 = *coeffs + a1;
  211. a4 = a3 + a2;
  212. *coeffs = a4;
  213. a1 = coeffs[1] + a3;
  214. a2 = a1 + a4;
  215. coeffs[1] = a2;
  216. coeffs += 2;
  217. }
  218. if (length & 1)
  219. *coeffs += a1 + a2;
  220. }
  221. } else if (mode == 3) {
  222. a1 = coeffs[1];
  223. a2 = a1 + *coeffs;
  224. coeffs[1] = a2;
  225. if (length > 2) {
  226. a3 = coeffs[2];
  227. a4 = a3 + a1;
  228. a5 = a4 + a2;
  229. coeffs += 3;
  230. for (i = 0; i < length - 3; i++) {
  231. a3 += *coeffs;
  232. a4 += a3;
  233. a5 += a4;
  234. *coeffs = a5;
  235. coeffs++;
  236. }
  237. }
  238. }
  239. }
  240. static int decode_segment(TAKDecContext *s, int8_t value, int32_t *dst, int len)
  241. {
  242. GetBitContext *gb = &s->gb;
  243. if (!value) {
  244. memset(dst, 0, len * 4);
  245. } else {
  246. int x, y, z, i = 0;
  247. value--;
  248. do {
  249. while (1) {
  250. x = get_bits_long(gb, xcodes[value].init);
  251. if (x >= xcodes[value].escape)
  252. break;
  253. dst[i++] = (x >> 1) ^ -(x & 1);
  254. if (i >= len)
  255. return 0;
  256. }
  257. y = get_bits1(gb);
  258. x = (y << xcodes[value].init) | x;
  259. if (x >= xcodes[value].aescape) {
  260. int c = get_unary(gb, 1, 9);
  261. if (c == 9) {
  262. int d;
  263. z = x + xcodes[value].bias;
  264. d = get_bits(gb, 3);
  265. if (d == 7) {
  266. d = get_bits(gb, 5) + 7;
  267. if (d > 29)
  268. return AVERROR_INVALIDDATA;
  269. }
  270. if (d)
  271. z += xcodes[value].scale * (get_bits_long(gb, d) + 1);
  272. } else {
  273. z = xcodes[value].scale * c + x - xcodes[value].escape;
  274. }
  275. } else {
  276. z = x - (xcodes[value].escape & -y);
  277. }
  278. dst[i++] = (z >> 1) ^ -(z & 1);
  279. } while (i < len);
  280. }
  281. return 0;
  282. }
  283. static int xget(TAKDecContext *s, int d, int q)
  284. {
  285. int x;
  286. x = d / q;
  287. s->rval = d - (x * q);
  288. if (s->rval < q / 2) {
  289. s->rval += q;
  290. } else {
  291. x++;
  292. }
  293. if (x <= 1 || x > 128)
  294. return -1;
  295. return x;
  296. }
  297. static int get_len(TAKDecContext *s, int b)
  298. {
  299. if (b >= s->wlength - 1)
  300. return s->rval;
  301. else
  302. return s->uval;
  303. }
  304. static int decode_coeffs(TAKDecContext *s, int32_t *dst, int length)
  305. {
  306. GetBitContext *gb = &s->gb;
  307. int i, v, ret;
  308. if (length > s->nb_samples)
  309. return AVERROR_INVALIDDATA;
  310. if (get_bits1(gb)) {
  311. if ((s->wlength = xget(s, length, s->uval)) < 0)
  312. return AVERROR_INVALIDDATA;
  313. s->coding_mode[0] = v = get_bits(gb, 6);
  314. if (s->coding_mode[0] > FF_ARRAY_ELEMS(xcodes))
  315. return AVERROR_INVALIDDATA;
  316. for (i = 1; i < s->wlength; i++) {
  317. int c = get_unary(gb, 1, 6);
  318. if (c > 5) {
  319. v = get_bits(gb, 6);
  320. } else if (c > 2) {
  321. int t = get_bits1(gb);
  322. v += (-t ^ (c - 1)) + t;
  323. } else {
  324. v += (-(c & 1) ^ (((c & 1) + c) >> 1)) + (c & 1);
  325. }
  326. if (v > FF_ARRAY_ELEMS(xcodes))
  327. return AVERROR_INVALIDDATA;
  328. s->coding_mode[i] = v;
  329. }
  330. i = 0;
  331. while (i < s->wlength) {
  332. int len = 0;
  333. v = s->coding_mode[i];
  334. do {
  335. len += get_len(s, i);
  336. i++;
  337. if (i == s->wlength)
  338. break;
  339. } while (v == s->coding_mode[i]);
  340. if ((ret = decode_segment(s, v, dst, len)) < 0)
  341. return ret;
  342. dst += len;
  343. }
  344. } else {
  345. v = get_bits(gb, 6);
  346. if (v > FF_ARRAY_ELEMS(xcodes))
  347. return AVERROR_INVALIDDATA;
  348. if ((ret = decode_segment(s, v, dst, length)) < 0)
  349. return ret;
  350. }
  351. return 0;
  352. }
  353. static int get_b(GetBitContext *gb)
  354. {
  355. if (get_bits1(gb))
  356. return get_bits(gb, 4) + 1;
  357. else
  358. return 0;
  359. }
  360. static int decode_subframe(TAKDecContext *s, int32_t *ptr, int subframe_size,
  361. int prev_subframe_size)
  362. {
  363. GetBitContext *gb = &s->gb;
  364. int tmp, x, y, i, j, ret = 0;
  365. int tfilter[MAX_PREDICTORS];
  366. if (get_bits1(gb)) {
  367. s->filter_order = predictor_sizes[get_bits(gb, 4)];
  368. if (prev_subframe_size > 0 && get_bits1(gb)) {
  369. if (s->filter_order > prev_subframe_size)
  370. return AVERROR_INVALIDDATA;
  371. ptr -= s->filter_order;
  372. subframe_size += s->filter_order;
  373. if (s->filter_order > subframe_size)
  374. return AVERROR_INVALIDDATA;
  375. } else {
  376. int lpc;
  377. if (s->filter_order > subframe_size)
  378. return AVERROR_INVALIDDATA;
  379. lpc = get_bits(gb, 2);
  380. if (lpc > 2)
  381. return AVERROR_INVALIDDATA;
  382. if ((ret = decode_coeffs(s, ptr, s->filter_order)) < 0)
  383. return ret;
  384. decode_lpc(ptr, lpc, s->filter_order);
  385. }
  386. s->xred = get_b(gb);
  387. s->size = get_bits1(gb) + 5;
  388. if (get_bits1(gb)) {
  389. s->ared = get_bits(gb, 3) + 1;
  390. if (s->ared > 7)
  391. return AVERROR_INVALIDDATA;
  392. } else {
  393. s->ared = 0;
  394. }
  395. s->predictors[0] = get_code(gb, 10);
  396. s->predictors[1] = get_code(gb, 10);
  397. s->predictors[2] = get_code(gb, s->size + 1) << (9 - s->size);
  398. s->predictors[3] = get_code(gb, s->size + 1) << (9 - s->size);
  399. if (s->filter_order > 4) {
  400. tmp = s->size + 1 - get_bits1(gb);
  401. for (i = 4; i < s->filter_order; i++) {
  402. if (!(i & 3))
  403. x = tmp - get_bits(gb, 2);
  404. s->predictors[i] = get_code(gb, x) << (9 - s->size);
  405. }
  406. }
  407. tfilter[0] = s->predictors[0] << 6;
  408. for (i = 1; i < s->filter_order; i++) {
  409. int32_t *p1 = &tfilter[0];
  410. int32_t *p2 = &tfilter[i - 1];
  411. for (j = 0; j < (i + 1) / 2; j++) {
  412. x = *p1 + (s->predictors[i] * *p2 + 256 >> 9);
  413. *p2 += s->predictors[i] * *p1 + 256 >> 9;
  414. *p1++ = x;
  415. p2--;
  416. }
  417. tfilter[i] = s->predictors[i] << 6;
  418. }
  419. x = -1 << (32 - (s->ared + 5));
  420. y = 1 << ((s->ared + 5) - 1);
  421. for (i = 0, j = s->filter_order - 1; i < s->filter_order / 2; i++, j--) {
  422. tmp = y + tfilter[j];
  423. s->filter[j] = -(x & -(y + tfilter[i] >> 31) |
  424. (y + tfilter[i]) >> (s->ared + 5));
  425. s->filter[i] = -(x & -(tmp >> 31) | (tmp >> s->ared + 5));
  426. }
  427. if ((ret = decode_coeffs(s, &ptr[s->filter_order],
  428. subframe_size - s->filter_order)) < 0)
  429. return ret;
  430. for (i = 0; i < s->filter_order; i++)
  431. s->residues[i] = *ptr++ >> s->xred;
  432. y = FF_ARRAY_ELEMS(s->residues) - s->filter_order;
  433. x = subframe_size - s->filter_order;
  434. while (x > 0) {
  435. tmp = FFMIN(y, x);
  436. for (i = 0; i < tmp; i++) {
  437. int v, w, m;
  438. v = 1 << (10 - s->ared - 1);
  439. if (!(s->filter_order & 15)) {
  440. v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
  441. s->filter_order);
  442. } else if (s->filter_order & 4) {
  443. for (j = 0; j < s->filter_order; j += 4) {
  444. v += s->residues[i + j + 3] * s->filter[j + 3] +
  445. s->residues[i + j + 2] * s->filter[j + 2] +
  446. s->residues[i + j + 1] * s->filter[j + 1] +
  447. s->residues[i + j ] * s->filter[j ];
  448. }
  449. } else {
  450. for (j = 0; j < s->filter_order; j += 8) {
  451. v += s->residues[i + j + 7] * s->filter[j + 7] +
  452. s->residues[i + j + 6] * s->filter[j + 6] +
  453. s->residues[i + j + 5] * s->filter[j + 5] +
  454. s->residues[i + j + 4] * s->filter[j + 4] +
  455. s->residues[i + j + 3] * s->filter[j + 3] +
  456. s->residues[i + j + 2] * s->filter[j + 2] +
  457. s->residues[i + j + 1] * s->filter[j + 1] +
  458. s->residues[i + j ] * s->filter[j ];
  459. }
  460. }
  461. m = (-1 << (32 - (10 - s->ared))) & -(v >> 31) | (v >> 10 - s->ared);
  462. m = av_clip(m, -8192, 8191);
  463. w = (m << s->xred) - *ptr;
  464. *ptr++ = w;
  465. s->residues[s->filter_order + i] = w >> s->xred;
  466. }
  467. x -= tmp;
  468. if (x > 0)
  469. memcpy(s->residues, &s->residues[y], 2 * s->filter_order);
  470. }
  471. emms_c();
  472. } else {
  473. ret = decode_coeffs(s, ptr, subframe_size);
  474. }
  475. return ret;
  476. }
  477. static int decode_channel(TAKDecContext *s, int chan)
  478. {
  479. AVCodecContext *avctx = s->avctx;
  480. GetBitContext *gb = &s->gb;
  481. int32_t *dst = s->decoded[chan];
  482. int i = 0, ret, prev = 0;
  483. int left = s->nb_samples - 1;
  484. s->sample_shift[chan] = get_b(gb);
  485. if (s->sample_shift[chan] >= avctx->bits_per_coded_sample)
  486. return AVERROR_INVALIDDATA;
  487. *dst++ = get_code(gb, avctx->bits_per_coded_sample - s->sample_shift[chan]);
  488. s->lpc_mode[chan] = get_bits(gb, 2);
  489. s->nb_subframes = get_bits(gb, 3) + 1;
  490. if (s->nb_subframes > 1) {
  491. if (get_bits_left(gb) < (s->nb_subframes - 1) * 6)
  492. return AVERROR_INVALIDDATA;
  493. for (; i < s->nb_subframes - 1; i++) {
  494. int v = get_bits(gb, 6);
  495. s->subframe_len[i] = (v - prev) * s->subframe_scale;
  496. if (s->subframe_len[i] <= 0)
  497. return AVERROR_INVALIDDATA;
  498. left -= s->subframe_len[i];
  499. prev = v;
  500. }
  501. if (left <= 0)
  502. return AVERROR_INVALIDDATA;
  503. }
  504. s->subframe_len[i] = left;
  505. prev = 0;
  506. for (i = 0; i < s->nb_subframes; i++) {
  507. if ((ret = decode_subframe(s, dst, s->subframe_len[i], prev)) < 0)
  508. return ret;
  509. dst += s->subframe_len[i];
  510. prev = s->subframe_len[i];
  511. }
  512. return 0;
  513. }
  514. static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
  515. {
  516. GetBitContext *gb = &s->gb;
  517. uint32_t *p1 = s->decoded[c1] + 1;
  518. uint32_t *p2 = s->decoded[c2] + 1;
  519. int a, b, i, x, tmp;
  520. if (s->dmode > 3) {
  521. s->dshift = get_b(gb);
  522. if (s->dmode > 5) {
  523. if (get_bits1(gb))
  524. s->filter_order = 16;
  525. else
  526. s->filter_order = 8;
  527. s->dval1 = get_bits1(gb);
  528. s->dval2 = get_bits1(gb);
  529. for (i = 0; i < s->filter_order; i++) {
  530. if (!(i & 3))
  531. x = 14 - get_bits(gb, 3);
  532. s->filter[i] = get_code(gb, x);
  533. }
  534. } else {
  535. s->dfactor = get_code(gb, 10);
  536. }
  537. }
  538. switch (s->dmode) {
  539. case 1:
  540. for (i = 0; i < length; i++, p1++, p2++)
  541. *p2 += *p1;
  542. break;
  543. case 2:
  544. for (i = 0; i < length; i++, p1++, p2++)
  545. *p1 = *p2 - *p1;
  546. break;
  547. case 3:
  548. for (i = 0; i < length; i++, p1++, p2++) {
  549. x = (*p2 & 1) + 2 * *p1;
  550. a = -*p2 + x;
  551. b = *p2 + x;
  552. *p1 = a & 0x80000000 | (a >> 1);
  553. *p2 = b & 0x80000000 | (b >> 1);
  554. }
  555. break;
  556. case 4:
  557. FFSWAP(uint32_t *, p1, p2);
  558. case 5:
  559. if (s->dshift)
  560. tmp = -1 << (32 - s->dshift);
  561. else
  562. tmp = 0;
  563. for (i = 0; i < length; i++, p1++, p2++) {
  564. x = s->dfactor * (tmp & -(*p2 >> 31) | (*p2 >> s->dshift)) + 128;
  565. *p1 = ((-(x >> 31) & 0xFF000000 | (x >> 8)) << s->dshift) - *p1;
  566. }
  567. break;
  568. case 6:
  569. FFSWAP(uint32_t *, p1, p2);
  570. case 7:
  571. if (length < 256)
  572. return AVERROR_INVALIDDATA;
  573. a = s->filter_order / 2;
  574. b = length - (s->filter_order - 1);
  575. if (s->dval1) {
  576. for (i = 0; i < a; i++)
  577. p1[i] += p2[i];
  578. }
  579. if (s->dval2) {
  580. x = a + b;
  581. for (i = 0; i < length - x; i++)
  582. p1[x + i] += p2[x + i];
  583. }
  584. for (i = 0; i < s->filter_order; i++)
  585. s->residues[i] = *p2++ >> s->dshift;
  586. p1 += a;
  587. x = FF_ARRAY_ELEMS(s->residues) - s->filter_order;
  588. for (; b > 0; b -= tmp) {
  589. tmp = FFMIN(b, x);
  590. for (i = 0; i < tmp; i++)
  591. s->residues[s->filter_order + i] = *p2++ >> s->dshift;
  592. for (i = 0; i < tmp; i++) {
  593. int v, w, m;
  594. v = 1 << 9;
  595. if (s->filter_order == 16) {
  596. v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
  597. s->filter_order);
  598. } else {
  599. v += s->residues[i + 7] * s->filter[7] +
  600. s->residues[i + 6] * s->filter[6] +
  601. s->residues[i + 5] * s->filter[5] +
  602. s->residues[i + 4] * s->filter[4] +
  603. s->residues[i + 3] * s->filter[3] +
  604. s->residues[i + 2] * s->filter[2] +
  605. s->residues[i + 1] * s->filter[1] +
  606. s->residues[i ] * s->filter[0];
  607. }
  608. m = (-1 << 22) & -(v >> 31) | (v >> 10);
  609. m = av_clip(m, -8192, 8191);
  610. w = (m << s->dshift) - *p1;
  611. *p1++ = w;
  612. }
  613. memcpy(s->residues, &s->residues[tmp], 2 * s->filter_order);
  614. }
  615. emms_c();
  616. break;
  617. }
  618. return 0;
  619. }
  620. static int tak_decode_frame(AVCodecContext *avctx, void *data,
  621. int *got_frame_ptr, AVPacket *pkt)
  622. {
  623. TAKDecContext *s = avctx->priv_data;
  624. GetBitContext *gb = &s->gb;
  625. int chan, i, ret, hsize;
  626. int32_t *p;
  627. if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
  628. return AVERROR_INVALIDDATA;
  629. init_get_bits(gb, pkt->data, pkt->size * 8);
  630. if ((ret = ff_tak_decode_frame_header(avctx, gb, &s->ti, 0)) < 0)
  631. return ret;
  632. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  633. hsize = get_bits_count(gb) / 8;
  634. if (ff_tak_check_crc(pkt->data, hsize)) {
  635. av_log(avctx, AV_LOG_ERROR, "CRC error\n");
  636. return AVERROR_INVALIDDATA;
  637. }
  638. }
  639. if (s->ti.codec != 2 && s->ti.codec != 4) {
  640. av_log(avctx, AV_LOG_ERROR, "unsupported codec: %d\n", s->ti.codec);
  641. return AVERROR_PATCHWELCOME;
  642. }
  643. if (s->ti.data_type) {
  644. av_log(avctx, AV_LOG_ERROR, "unsupported data type: %d\n", s->ti.data_type);
  645. return AVERROR_INVALIDDATA;
  646. }
  647. if (s->ti.codec == 2 && s->ti.channels > 2) {
  648. av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", s->ti.channels);
  649. return AVERROR_INVALIDDATA;
  650. }
  651. if (s->ti.channels > 6) {
  652. av_log(avctx, AV_LOG_ERROR, "unsupported number of channels: %d\n", s->ti.channels);
  653. return AVERROR_INVALIDDATA;
  654. }
  655. if (s->ti.frame_samples <= 0) {
  656. av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of samples\n");
  657. return AVERROR_INVALIDDATA;
  658. }
  659. if (s->ti.bps != avctx->bits_per_coded_sample) {
  660. avctx->bits_per_coded_sample = s->ti.bps;
  661. tak_set_bps(avctx);
  662. }
  663. if (s->ti.sample_rate != avctx->sample_rate) {
  664. avctx->sample_rate = s->ti.sample_rate;
  665. s->uval = get_scale(avctx->sample_rate, get_shift(avctx->sample_rate));
  666. s->subframe_scale = get_scale(avctx->sample_rate, 1);
  667. }
  668. if (s->ti.ch_layout)
  669. avctx->channel_layout = s->ti.ch_layout;
  670. avctx->channels = s->ti.channels;
  671. s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples :
  672. s->ti.frame_samples;
  673. s->frame.nb_samples = s->nb_samples;
  674. if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0)
  675. return ret;
  676. if (avctx->bits_per_coded_sample <= 16) {
  677. av_fast_malloc(&s->decode_buffer, &s->decode_buffer_size,
  678. sizeof(*s->decode_buffer) * FFALIGN(s->nb_samples, 8) *
  679. avctx->channels + FF_INPUT_BUFFER_PADDING_SIZE);
  680. if (!s->decode_buffer)
  681. return AVERROR(ENOMEM);
  682. for (chan = 0; chan < avctx->channels; chan++)
  683. s->decoded[chan] = s->decode_buffer +
  684. chan * FFALIGN(s->nb_samples, 8);
  685. } else {
  686. for (chan = 0; chan < avctx->channels; chan++)
  687. s->decoded[chan] = (int32_t *)s->frame.data[chan];
  688. }
  689. if (s->nb_samples < 16) {
  690. for (chan = 0; chan < avctx->channels; chan++) {
  691. p = s->decoded[chan];
  692. for (i = 0; i < s->nb_samples; i++)
  693. *p++ = get_code(gb, avctx->bits_per_coded_sample);
  694. }
  695. } else {
  696. if (s->ti.codec == 2) {
  697. for (chan = 0; chan < avctx->channels; chan++) {
  698. if (ret = decode_channel(s, chan))
  699. return ret;
  700. }
  701. if (avctx->channels == 2) {
  702. s->nb_subframes = get_bits(gb, 1) + 1;
  703. if (s->nb_subframes > 1)
  704. s->subframe_len[1] = get_bits(gb, 6);
  705. s->dmode = get_bits(gb, 3);
  706. if (ret = decorrelate(s, 0, 1, s->nb_samples - 1))
  707. return ret;
  708. }
  709. } else if (s->ti.codec == 4) {
  710. if (get_bits1(gb)) {
  711. int ch_mask = 0;
  712. chan = get_bits(gb, 4) + 1;
  713. if (chan > avctx->channels)
  714. return AVERROR_INVALIDDATA;
  715. for (i = 0; i < chan; i++) {
  716. int nbit = get_bits(gb, 4);
  717. if (nbit >= avctx->channels)
  718. return AVERROR_INVALIDDATA;
  719. if (ch_mask & 1 << nbit)
  720. return AVERROR_INVALIDDATA;
  721. s->mcdparams[i].present = get_bits1(gb);
  722. if (s->mcdparams[i].present) {
  723. s->mcdparams[i].index = get_bits(gb, 2);
  724. s->mcdparams[i].chan2 = get_bits(gb, 4);
  725. if (s->mcdparams[i].index == 1) {
  726. if ((nbit == s->mcdparams[i].chan2) ||
  727. (ch_mask & 1 << s->mcdparams[i].chan2))
  728. return AVERROR_INVALIDDATA;
  729. ch_mask |= 1 << s->mcdparams[i].chan2;
  730. } else if (!(ch_mask & 1 << s->mcdparams[i].chan2)) {
  731. return AVERROR_INVALIDDATA;
  732. }
  733. }
  734. s->mcdparams[i].chan1 = nbit;
  735. ch_mask |= 1 << nbit;
  736. }
  737. } else {
  738. chan = avctx->channels;
  739. for (i = 0; i < chan; i++) {
  740. s->mcdparams[i].present = 0;
  741. s->mcdparams[i].chan1 = i;
  742. }
  743. }
  744. for (i = 0; i < chan; i++) {
  745. if (s->mcdparams[i].present && s->mcdparams[i].index == 1) {
  746. if (ret = decode_channel(s, s->mcdparams[i].chan2))
  747. return ret;
  748. }
  749. if (ret = decode_channel(s, s->mcdparams[i].chan1))
  750. return ret;
  751. if (s->mcdparams[i].present) {
  752. s->dmode = mc_dmodes[s->mcdparams[i].index];
  753. if (ret = decorrelate(s, s->mcdparams[i].chan2,
  754. s->mcdparams[i].chan1,
  755. s->nb_samples - 1))
  756. return ret;
  757. }
  758. }
  759. }
  760. for (chan = 0; chan < avctx->channels; chan++) {
  761. p = s->decoded[chan];
  762. decode_lpc(p, s->lpc_mode[chan], s->nb_samples);
  763. if (s->sample_shift[chan] > 0) {
  764. for (i = 0; i < s->nb_samples; i++)
  765. *p++ <<= s->sample_shift[chan];
  766. }
  767. }
  768. }
  769. align_get_bits(gb);
  770. skip_bits(gb, 24);
  771. if (get_bits_left(gb) < 0)
  772. av_log(avctx, AV_LOG_DEBUG, "overread\n");
  773. else if (get_bits_left(gb) > 0)
  774. av_log(avctx, AV_LOG_DEBUG, "underread\n");
  775. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  776. if (ff_tak_check_crc(pkt->data + hsize,
  777. get_bits_count(gb) / 8 - hsize)) {
  778. av_log(avctx, AV_LOG_ERROR, "CRC error\n");
  779. return AVERROR_INVALIDDATA;
  780. }
  781. }
  782. // convert to output buffer
  783. switch (avctx->bits_per_coded_sample) {
  784. case 8:
  785. for (chan = 0; chan < avctx->channels; chan++) {
  786. uint8_t *samples = (uint8_t *)s->frame.data[chan];
  787. p = s->decoded[chan];
  788. for (i = 0; i < s->nb_samples; i++, p++)
  789. *samples++ = *p + 0x80;
  790. }
  791. break;
  792. case 16:
  793. for (chan = 0; chan < avctx->channels; chan++) {
  794. int16_t *samples = (int16_t *)s->frame.data[chan];
  795. p = s->decoded[chan];
  796. for (i = 0; i < s->nb_samples; i++, p++)
  797. *samples++ = *p;
  798. }
  799. break;
  800. case 24:
  801. for (chan = 0; chan < avctx->channels; chan++) {
  802. int32_t *samples = (int32_t *)s->frame.data[chan];
  803. for (i = 0; i < s->nb_samples; i++)
  804. *samples++ <<= 8;
  805. }
  806. break;
  807. }
  808. *got_frame_ptr = 1;
  809. *(AVFrame *)data = s->frame;
  810. return pkt->size;
  811. }
  812. static av_cold int tak_decode_close(AVCodecContext *avctx)
  813. {
  814. TAKDecContext *s = avctx->priv_data;
  815. av_freep(&s->decode_buffer);
  816. return 0;
  817. }
  818. AVCodec ff_tak_decoder = {
  819. .name = "tak",
  820. .type = AVMEDIA_TYPE_AUDIO,
  821. .id = AV_CODEC_ID_TAK,
  822. .priv_data_size = sizeof(TAKDecContext),
  823. .init = tak_decode_init,
  824. .close = tak_decode_close,
  825. .decode = tak_decode_frame,
  826. .capabilities = CODEC_CAP_DR1,
  827. .long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
  828. };