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.

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