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.

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