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.

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