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.

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