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.

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