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.

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