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.

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