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.

962 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;
  151. if (avctx->sample_rate < 11025) {
  152. shift = 3;
  153. } else if (avctx->sample_rate < 22050) {
  154. shift = 2;
  155. } else if (avctx->sample_rate < 44100) {
  156. shift = 1;
  157. } else {
  158. shift = 0;
  159. }
  160. s->uval = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << shift;
  161. s->subframe_scale = FFALIGN(avctx->sample_rate + 511 >> 9, 4) << 1;
  162. }
  163. static av_cold int tak_decode_init(AVCodecContext *avctx)
  164. {
  165. TAKDecContext *s = avctx->priv_data;
  166. ff_audiodsp_init(&s->adsp);
  167. ff_takdsp_init(&s->tdsp);
  168. s->avctx = avctx;
  169. avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
  170. set_sample_rate_params(avctx);
  171. return set_bps_params(avctx);
  172. }
  173. static void decode_lpc(int32_t *coeffs, int mode, int length)
  174. {
  175. int i;
  176. if (length < 2)
  177. return;
  178. if (mode == 1) {
  179. int a1 = *coeffs++;
  180. for (i = 0; i < length - 1 >> 1; i++) {
  181. *coeffs += a1;
  182. coeffs[1] += *coeffs;
  183. a1 = coeffs[1];
  184. coeffs += 2;
  185. }
  186. if (length - 1 & 1)
  187. *coeffs += a1;
  188. } else if (mode == 2) {
  189. int a1 = coeffs[1];
  190. int a2 = a1 + *coeffs;
  191. coeffs[1] = a2;
  192. if (length > 2) {
  193. coeffs += 2;
  194. for (i = 0; i < length - 2 >> 1; i++) {
  195. int a3 = *coeffs + a1;
  196. int a4 = a3 + a2;
  197. *coeffs = a4;
  198. a1 = coeffs[1] + a3;
  199. a2 = a1 + a4;
  200. coeffs[1] = a2;
  201. coeffs += 2;
  202. }
  203. if (length & 1)
  204. *coeffs += a1 + a2;
  205. }
  206. } else if (mode == 3) {
  207. int a1 = coeffs[1];
  208. int a2 = a1 + *coeffs;
  209. coeffs[1] = a2;
  210. if (length > 2) {
  211. int a3 = coeffs[2];
  212. int a4 = a3 + a1;
  213. int a5 = a4 + a2;
  214. coeffs[2] = a5;
  215. coeffs += 3;
  216. for (i = 0; i < length - 3; i++) {
  217. a3 += *coeffs;
  218. a4 += a3;
  219. a5 += a4;
  220. *coeffs = a5;
  221. coeffs++;
  222. }
  223. }
  224. }
  225. }
  226. static int decode_segment(TAKDecContext *s, int8_t mode, int32_t *decoded, int len)
  227. {
  228. struct CParam code;
  229. GetBitContext *gb = &s->gb;
  230. int i;
  231. if (!mode) {
  232. memset(decoded, 0, len * sizeof(*decoded));
  233. return 0;
  234. }
  235. if (mode > FF_ARRAY_ELEMS(xcodes))
  236. return AVERROR_INVALIDDATA;
  237. code = xcodes[mode - 1];
  238. for (i = 0; i < len; i++) {
  239. unsigned x = get_bits_long(gb, code.init);
  240. if (x >= code.escape && get_bits1(gb)) {
  241. x |= 1 << code.init;
  242. if (x >= code.aescape) {
  243. unsigned scale = get_unary(gb, 1, 9);
  244. if (scale == 9) {
  245. int scale_bits = get_bits(gb, 3);
  246. if (scale_bits > 0) {
  247. if (scale_bits == 7) {
  248. scale_bits += get_bits(gb, 5);
  249. if (scale_bits > 29)
  250. return AVERROR_INVALIDDATA;
  251. }
  252. scale = get_bits_long(gb, scale_bits) + 1;
  253. x += code.scale * scale;
  254. }
  255. x += code.bias;
  256. } else
  257. x += code.scale * scale - code.escape;
  258. } else
  259. x -= code.escape;
  260. }
  261. decoded[i] = (x >> 1) ^ -(x & 1);
  262. }
  263. return 0;
  264. }
  265. static int decode_residues(TAKDecContext *s, int32_t *decoded, int length)
  266. {
  267. GetBitContext *gb = &s->gb;
  268. int i, mode, ret;
  269. if (length > s->nb_samples)
  270. return AVERROR_INVALIDDATA;
  271. if (get_bits1(gb)) {
  272. int wlength, rval;
  273. wlength = length / s->uval;
  274. rval = length - (wlength * s->uval);
  275. if (rval < s->uval / 2)
  276. rval += s->uval;
  277. else
  278. wlength++;
  279. if (wlength <= 1 || wlength > 128)
  280. return AVERROR_INVALIDDATA;
  281. s->coding_mode[0] = mode = get_bits(gb, 6);
  282. for (i = 1; i < wlength; i++) {
  283. int c = get_unary(gb, 1, 6);
  284. switch (c) {
  285. case 6:
  286. mode = get_bits(gb, 6);
  287. break;
  288. case 5:
  289. case 4:
  290. case 3: {
  291. /* mode += sign ? (1 - c) : (c - 1) */
  292. int sign = get_bits1(gb);
  293. mode += (-sign ^ (c - 1)) + sign;
  294. break;
  295. }
  296. case 2:
  297. mode++;
  298. break;
  299. case 1:
  300. mode--;
  301. break;
  302. }
  303. s->coding_mode[i] = mode;
  304. }
  305. i = 0;
  306. while (i < wlength) {
  307. int len = 0;
  308. mode = s->coding_mode[i];
  309. do {
  310. if (i >= wlength - 1)
  311. len += rval;
  312. else
  313. len += s->uval;
  314. i++;
  315. if (i == wlength)
  316. break;
  317. } while (s->coding_mode[i] == mode);
  318. if ((ret = decode_segment(s, mode, decoded, len)) < 0)
  319. return ret;
  320. decoded += len;
  321. }
  322. } else {
  323. mode = get_bits(gb, 6);
  324. if ((ret = decode_segment(s, mode, decoded, length)) < 0)
  325. return ret;
  326. }
  327. return 0;
  328. }
  329. static int get_bits_esc4(GetBitContext *gb)
  330. {
  331. if (get_bits1(gb))
  332. return get_bits(gb, 4) + 1;
  333. else
  334. return 0;
  335. }
  336. static int decode_subframe(TAKDecContext *s, int32_t *decoded,
  337. int subframe_size, int prev_subframe_size)
  338. {
  339. GetBitContext *gb = &s->gb;
  340. int x, y, i, j, ret = 0;
  341. int dshift, size, filter_quant, filter_order;
  342. int tfilter[MAX_PREDICTORS];
  343. if (!get_bits1(gb))
  344. return decode_residues(s, decoded, subframe_size);
  345. filter_order = predictor_sizes[get_bits(gb, 4)];
  346. if (prev_subframe_size > 0 && get_bits1(gb)) {
  347. if (filter_order > prev_subframe_size)
  348. return AVERROR_INVALIDDATA;
  349. decoded -= filter_order;
  350. subframe_size += filter_order;
  351. if (filter_order > subframe_size)
  352. return AVERROR_INVALIDDATA;
  353. } else {
  354. int lpc_mode;
  355. if (filter_order > subframe_size)
  356. return AVERROR_INVALIDDATA;
  357. lpc_mode = get_bits(gb, 2);
  358. if (lpc_mode > 2)
  359. return AVERROR_INVALIDDATA;
  360. if ((ret = decode_residues(s, decoded, filter_order)) < 0)
  361. return ret;
  362. if (lpc_mode)
  363. decode_lpc(decoded, lpc_mode, filter_order);
  364. }
  365. dshift = get_bits_esc4(gb);
  366. size = get_bits1(gb) + 6;
  367. filter_quant = 10;
  368. if (get_bits1(gb)) {
  369. filter_quant -= get_bits(gb, 3) + 1;
  370. if (filter_quant < 3)
  371. return AVERROR_INVALIDDATA;
  372. }
  373. s->predictors[0] = get_sbits(gb, 10);
  374. s->predictors[1] = get_sbits(gb, 10);
  375. s->predictors[2] = get_sbits(gb, size) * (1 << (10 - size));
  376. s->predictors[3] = get_sbits(gb, size) * (1 << (10 - size));
  377. if (filter_order > 4) {
  378. int tmp = size - get_bits1(gb);
  379. for (i = 4; i < filter_order; i++) {
  380. if (!(i & 3))
  381. x = tmp - get_bits(gb, 2);
  382. s->predictors[i] = get_sbits(gb, x) * (1 << (10 - size));
  383. }
  384. }
  385. tfilter[0] = s->predictors[0] * 64;
  386. for (i = 1; i < filter_order; i++) {
  387. int32_t *p1 = &tfilter[0];
  388. int32_t *p2 = &tfilter[i - 1];
  389. for (j = 0; j < (i + 1) / 2; j++) {
  390. x = *p1 + (s->predictors[i] * *p2 + 256 >> 9);
  391. *p2 += s->predictors[i] * *p1 + 256 >> 9;
  392. *p1++ = x;
  393. p2--;
  394. }
  395. tfilter[i] = s->predictors[i] * 64;
  396. }
  397. x = 1 << (32 - (15 - filter_quant));
  398. y = 1 << ((15 - filter_quant) - 1);
  399. for (i = 0, j = filter_order - 1; i < filter_order / 2; i++, j--) {
  400. s->filter[j] = x - ((tfilter[i] + y) >> (15 - filter_quant));
  401. s->filter[i] = x - ((tfilter[j] + y) >> (15 - filter_quant));
  402. }
  403. if ((ret = decode_residues(s, &decoded[filter_order],
  404. subframe_size - filter_order)) < 0)
  405. return ret;
  406. for (i = 0; i < filter_order; i++)
  407. s->residues[i] = *decoded++ >> dshift;
  408. y = FF_ARRAY_ELEMS(s->residues) - filter_order;
  409. x = subframe_size - filter_order;
  410. while (x > 0) {
  411. int tmp = FFMIN(y, x);
  412. for (i = 0; i < tmp; i++) {
  413. int v = 1 << (filter_quant - 1);
  414. if (filter_order & -16)
  415. v += s->adsp.scalarproduct_int16(&s->residues[i], s->filter,
  416. filter_order & -16);
  417. for (j = filter_order & -16; j < filter_order; j += 4) {
  418. v += s->residues[i + j + 3] * s->filter[j + 3] +
  419. s->residues[i + j + 2] * s->filter[j + 2] +
  420. s->residues[i + j + 1] * s->filter[j + 1] +
  421. s->residues[i + j ] * s->filter[j ];
  422. }
  423. v = (av_clip_intp2(v >> filter_quant, 13) * (1 << dshift)) - *decoded;
  424. *decoded++ = v;
  425. s->residues[filter_order + i] = v >> dshift;
  426. }
  427. x -= tmp;
  428. if (x > 0)
  429. memcpy(s->residues, &s->residues[y], 2 * filter_order);
  430. }
  431. emms_c();
  432. return 0;
  433. }
  434. static int decode_channel(TAKDecContext *s, int chan)
  435. {
  436. AVCodecContext *avctx = s->avctx;
  437. GetBitContext *gb = &s->gb;
  438. int32_t *decoded = s->decoded[chan];
  439. int left = s->nb_samples - 1;
  440. int i = 0, ret, prev = 0;
  441. s->sample_shift[chan] = get_bits_esc4(gb);
  442. if (s->sample_shift[chan] >= avctx->bits_per_raw_sample)
  443. return AVERROR_INVALIDDATA;
  444. *decoded++ = get_sbits(gb, avctx->bits_per_raw_sample - s->sample_shift[chan]);
  445. s->lpc_mode[chan] = get_bits(gb, 2);
  446. s->nb_subframes = get_bits(gb, 3) + 1;
  447. if (s->nb_subframes > 1) {
  448. if (get_bits_left(gb) < (s->nb_subframes - 1) * 6)
  449. return AVERROR_INVALIDDATA;
  450. for (; i < s->nb_subframes - 1; i++) {
  451. int v = get_bits(gb, 6);
  452. s->subframe_len[i] = (v - prev) * s->subframe_scale;
  453. if (s->subframe_len[i] <= 0)
  454. return AVERROR_INVALIDDATA;
  455. left -= s->subframe_len[i];
  456. prev = v;
  457. }
  458. if (left <= 0)
  459. return AVERROR_INVALIDDATA;
  460. }
  461. s->subframe_len[i] = left;
  462. prev = 0;
  463. for (i = 0; i < s->nb_subframes; i++) {
  464. if ((ret = decode_subframe(s, decoded, s->subframe_len[i], prev)) < 0)
  465. return ret;
  466. decoded += s->subframe_len[i];
  467. prev = s->subframe_len[i];
  468. }
  469. return 0;
  470. }
  471. static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
  472. {
  473. GetBitContext *gb = &s->gb;
  474. int32_t *p1 = s->decoded[c1] + (s->dmode > 5);
  475. int32_t *p2 = s->decoded[c2] + (s->dmode > 5);
  476. int32_t bp1 = p1[0];
  477. int32_t bp2 = p2[0];
  478. int i;
  479. int dshift, dfactor;
  480. length += s->dmode < 6;
  481. switch (s->dmode) {
  482. case 1: /* left/side */
  483. s->tdsp.decorrelate_ls(p1, p2, length);
  484. break;
  485. case 2: /* side/right */
  486. s->tdsp.decorrelate_sr(p1, p2, length);
  487. break;
  488. case 3: /* side/mid */
  489. s->tdsp.decorrelate_sm(p1, p2, length);
  490. break;
  491. case 4: /* side/left with scale factor */
  492. FFSWAP(int32_t*, p1, p2);
  493. FFSWAP(int32_t, bp1, bp2);
  494. case 5: /* side/right with scale factor */
  495. dshift = get_bits_esc4(gb);
  496. dfactor = get_sbits(gb, 10);
  497. s->tdsp.decorrelate_sf(p1, p2, length, dshift, dfactor);
  498. break;
  499. case 6:
  500. FFSWAP(int32_t*, p1, p2);
  501. case 7: {
  502. int length2, order_half, filter_order, dval1, dval2;
  503. int tmp, x, code_size;
  504. if (length < 256)
  505. return AVERROR_INVALIDDATA;
  506. dshift = get_bits_esc4(gb);
  507. filter_order = 8 << get_bits1(gb);
  508. dval1 = get_bits1(gb);
  509. dval2 = get_bits1(gb);
  510. for (i = 0; i < filter_order; i++) {
  511. if (!(i & 3))
  512. code_size = 14 - get_bits(gb, 3);
  513. s->filter[i] = get_sbits(gb, code_size);
  514. }
  515. order_half = filter_order / 2;
  516. length2 = length - (filter_order - 1);
  517. /* decorrelate beginning samples */
  518. if (dval1) {
  519. for (i = 0; i < order_half; i++) {
  520. int32_t a = p1[i];
  521. int32_t b = p2[i];
  522. p1[i] = a + b;
  523. }
  524. }
  525. /* decorrelate ending samples */
  526. if (dval2) {
  527. for (i = length2 + order_half; i < length; i++) {
  528. int32_t a = p1[i];
  529. int32_t b = p2[i];
  530. p1[i] = a + b;
  531. }
  532. }
  533. for (i = 0; i < filter_order; i++)
  534. s->residues[i] = *p2++ >> dshift;
  535. p1 += order_half;
  536. x = FF_ARRAY_ELEMS(s->residues) - filter_order;
  537. for (; length2 > 0; length2 -= tmp) {
  538. tmp = FFMIN(length2, x);
  539. for (i = 0; i < tmp - (tmp == length2); i++)
  540. s->residues[filter_order + i] = *p2++ >> dshift;
  541. for (i = 0; i < tmp; i++) {
  542. int v = 1 << 9;
  543. if (filter_order == 16) {
  544. v += s->adsp.scalarproduct_int16(&s->residues[i], s->filter,
  545. filter_order);
  546. } else {
  547. v += s->residues[i + 7] * s->filter[7] +
  548. s->residues[i + 6] * s->filter[6] +
  549. s->residues[i + 5] * s->filter[5] +
  550. s->residues[i + 4] * s->filter[4] +
  551. s->residues[i + 3] * s->filter[3] +
  552. s->residues[i + 2] * s->filter[2] +
  553. s->residues[i + 1] * s->filter[1] +
  554. s->residues[i ] * s->filter[0];
  555. }
  556. v = (av_clip_intp2(v >> 10, 13) << dshift) - *p1;
  557. *p1++ = v;
  558. }
  559. memmove(s->residues, &s->residues[tmp], 2 * filter_order);
  560. }
  561. emms_c();
  562. break;
  563. }
  564. }
  565. if (s->dmode > 0 && s->dmode < 6) {
  566. p1[0] = bp1;
  567. p2[0] = bp2;
  568. }
  569. return 0;
  570. }
  571. static int tak_decode_frame(AVCodecContext *avctx, void *data,
  572. int *got_frame_ptr, AVPacket *pkt)
  573. {
  574. TAKDecContext *s = avctx->priv_data;
  575. AVFrame *frame = data;
  576. ThreadFrame tframe = { .f = data };
  577. GetBitContext *gb = &s->gb;
  578. int chan, i, ret, hsize;
  579. if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
  580. return AVERROR_INVALIDDATA;
  581. if ((ret = init_get_bits8(gb, pkt->data, pkt->size)) < 0)
  582. return ret;
  583. if ((ret = ff_tak_decode_frame_header(avctx, gb, &s->ti, 0)) < 0)
  584. return ret;
  585. hsize = get_bits_count(gb) / 8;
  586. if (avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) {
  587. if (ff_tak_check_crc(pkt->data, hsize)) {
  588. av_log(avctx, AV_LOG_ERROR, "CRC error\n");
  589. if (avctx->err_recognition & AV_EF_EXPLODE)
  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. avctx->bits_per_raw_sample = s->ti.bps;
  618. if ((ret = set_bps_params(avctx)) < 0)
  619. return ret;
  620. if (s->ti.sample_rate != avctx->sample_rate) {
  621. avctx->sample_rate = s->ti.sample_rate;
  622. set_sample_rate_params(avctx);
  623. }
  624. if (s->ti.ch_layout)
  625. avctx->channel_layout = s->ti.ch_layout;
  626. avctx->channels = s->ti.channels;
  627. s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples
  628. : s->ti.frame_samples;
  629. frame->nb_samples = s->nb_samples;
  630. if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
  631. return ret;
  632. ff_thread_finish_setup(avctx);
  633. if (avctx->bits_per_raw_sample <= 16) {
  634. int buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  635. s->nb_samples,
  636. AV_SAMPLE_FMT_S32P, 0);
  637. if (buf_size < 0)
  638. return buf_size;
  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].chan2 >= avctx->channels) {
  688. av_log(avctx, AV_LOG_ERROR,
  689. "invalid channel 2 (%d) for %d channel(s)\n",
  690. s->mcdparams[i].chan2, avctx->channels);
  691. return AVERROR_INVALIDDATA;
  692. }
  693. if (s->mcdparams[i].index == 1) {
  694. if ((nbit == s->mcdparams[i].chan2) ||
  695. (ch_mask & 1 << s->mcdparams[i].chan2))
  696. return AVERROR_INVALIDDATA;
  697. ch_mask |= 1 << s->mcdparams[i].chan2;
  698. } else if (!(ch_mask & 1 << s->mcdparams[i].chan2)) {
  699. return AVERROR_INVALIDDATA;
  700. }
  701. }
  702. s->mcdparams[i].chan1 = nbit;
  703. ch_mask |= 1 << nbit;
  704. }
  705. } else {
  706. chan = avctx->channels;
  707. for (i = 0; i < chan; i++) {
  708. s->mcdparams[i].present = 0;
  709. s->mcdparams[i].chan1 = i;
  710. }
  711. }
  712. for (i = 0; i < chan; i++) {
  713. if (s->mcdparams[i].present && s->mcdparams[i].index == 1)
  714. if (ret = decode_channel(s, s->mcdparams[i].chan2))
  715. return ret;
  716. if (ret = decode_channel(s, s->mcdparams[i].chan1))
  717. return ret;
  718. if (s->mcdparams[i].present) {
  719. s->dmode = mc_dmodes[s->mcdparams[i].index];
  720. if (ret = decorrelate(s,
  721. s->mcdparams[i].chan2,
  722. s->mcdparams[i].chan1,
  723. s->nb_samples - 1))
  724. return ret;
  725. }
  726. }
  727. }
  728. for (chan = 0; chan < avctx->channels; chan++) {
  729. int32_t *decoded = s->decoded[chan];
  730. if (s->lpc_mode[chan])
  731. decode_lpc(decoded, s->lpc_mode[chan], s->nb_samples);
  732. if (s->sample_shift[chan] > 0)
  733. for (i = 0; i < s->nb_samples; i++)
  734. decoded[i] *= 1 << s->sample_shift[chan];
  735. }
  736. }
  737. align_get_bits(gb);
  738. skip_bits(gb, 24);
  739. if (get_bits_left(gb) < 0)
  740. av_log(avctx, AV_LOG_DEBUG, "overread\n");
  741. else if (get_bits_left(gb) > 0)
  742. av_log(avctx, AV_LOG_DEBUG, "underread\n");
  743. if (avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_COMPLIANT)) {
  744. if (ff_tak_check_crc(pkt->data + hsize,
  745. get_bits_count(gb) / 8 - hsize)) {
  746. av_log(avctx, AV_LOG_ERROR, "CRC error\n");
  747. if (avctx->err_recognition & AV_EF_EXPLODE)
  748. return AVERROR_INVALIDDATA;
  749. }
  750. }
  751. /* convert to output buffer */
  752. switch (avctx->sample_fmt) {
  753. case AV_SAMPLE_FMT_U8P:
  754. for (chan = 0; chan < avctx->channels; chan++) {
  755. uint8_t *samples = (uint8_t *)frame->extended_data[chan];
  756. int32_t *decoded = s->decoded[chan];
  757. for (i = 0; i < s->nb_samples; i++)
  758. samples[i] = decoded[i] + 0x80;
  759. }
  760. break;
  761. case AV_SAMPLE_FMT_S16P:
  762. for (chan = 0; chan < avctx->channels; chan++) {
  763. int16_t *samples = (int16_t *)frame->extended_data[chan];
  764. int32_t *decoded = s->decoded[chan];
  765. for (i = 0; i < s->nb_samples; i++)
  766. samples[i] = decoded[i];
  767. }
  768. break;
  769. case AV_SAMPLE_FMT_S32P:
  770. for (chan = 0; chan < avctx->channels; chan++) {
  771. int32_t *samples = (int32_t *)frame->extended_data[chan];
  772. for (i = 0; i < s->nb_samples; i++)
  773. samples[i] *= 1 << 8;
  774. }
  775. break;
  776. }
  777. *got_frame_ptr = 1;
  778. return pkt->size;
  779. }
  780. #if HAVE_THREADS
  781. static int init_thread_copy(AVCodecContext *avctx)
  782. {
  783. TAKDecContext *s = avctx->priv_data;
  784. s->avctx = avctx;
  785. return 0;
  786. }
  787. static int update_thread_context(AVCodecContext *dst,
  788. const AVCodecContext *src)
  789. {
  790. TAKDecContext *tsrc = src->priv_data;
  791. TAKDecContext *tdst = dst->priv_data;
  792. if (dst == src)
  793. return 0;
  794. memcpy(&tdst->ti, &tsrc->ti, sizeof(TAKStreamInfo));
  795. return 0;
  796. }
  797. #endif
  798. static av_cold int tak_decode_close(AVCodecContext *avctx)
  799. {
  800. TAKDecContext *s = avctx->priv_data;
  801. av_freep(&s->decode_buffer);
  802. return 0;
  803. }
  804. AVCodec ff_tak_decoder = {
  805. .name = "tak",
  806. .long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
  807. .type = AVMEDIA_TYPE_AUDIO,
  808. .id = AV_CODEC_ID_TAK,
  809. .priv_data_size = sizeof(TAKDecContext),
  810. .init = tak_decode_init,
  811. .close = tak_decode_close,
  812. .decode = tak_decode_frame,
  813. .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
  814. .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
  815. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  816. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
  817. AV_SAMPLE_FMT_S16P,
  818. AV_SAMPLE_FMT_S32P,
  819. AV_SAMPLE_FMT_NONE },
  820. };