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.

939 lines
30KB

  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 "avcodec.h"
  30. #include "dsputil.h"
  31. #include "internal.h"
  32. #include "unary.h"
  33. #define MAX_SUBFRAMES 8 ///< max number of subframes per channel
  34. #define MAX_PREDICTORS 256
  35. typedef struct MCDParam {
  36. int8_t present; ///< decorrelation parameter availability for this channel
  37. int8_t index; ///< index into array of decorrelation types
  38. int8_t chan1;
  39. int8_t chan2;
  40. } MCDParam;
  41. typedef struct TAKDecContext {
  42. AVCodecContext *avctx; ///< parent AVCodecContext
  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. avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
  159. set_sample_rate_params(avctx);
  160. return set_bps_params(avctx);
  161. }
  162. static void decode_lpc(int32_t *coeffs, int mode, int length)
  163. {
  164. int i;
  165. if (length < 2)
  166. return;
  167. if (mode == 1) {
  168. int a1 = *coeffs++;
  169. for (i = 0; i < length - 1 >> 1; i++) {
  170. *coeffs += a1;
  171. coeffs[1] += *coeffs;
  172. a1 = coeffs[1];
  173. coeffs += 2;
  174. }
  175. if (length - 1 & 1)
  176. *coeffs += a1;
  177. } else if (mode == 2) {
  178. int a1 = coeffs[1];
  179. int a2 = a1 + *coeffs;
  180. coeffs[1] = a2;
  181. if (length > 2) {
  182. coeffs += 2;
  183. for (i = 0; i < length - 2 >> 1; i++) {
  184. int a3 = *coeffs + a1;
  185. int a4 = a3 + a2;
  186. *coeffs = a4;
  187. a1 = coeffs[1] + a3;
  188. a2 = a1 + a4;
  189. coeffs[1] = a2;
  190. coeffs += 2;
  191. }
  192. if (length & 1)
  193. *coeffs += a1 + a2;
  194. }
  195. } else if (mode == 3) {
  196. int a1 = coeffs[1];
  197. int a2 = a1 + *coeffs;
  198. coeffs[1] = a2;
  199. if (length > 2) {
  200. int a3 = coeffs[2];
  201. int a4 = a3 + a1;
  202. int a5 = a4 + a2;
  203. coeffs += 3;
  204. for (i = 0; i < length - 3; i++) {
  205. a3 += *coeffs;
  206. a4 += a3;
  207. a5 += a4;
  208. *coeffs = a5;
  209. coeffs++;
  210. }
  211. }
  212. }
  213. }
  214. static int decode_segment(TAKDecContext *s, int8_t mode, int32_t *decoded, int len)
  215. {
  216. struct CParam code;
  217. GetBitContext *gb = &s->gb;
  218. int i;
  219. if (!mode) {
  220. memset(decoded, 0, len * sizeof(*decoded));
  221. return 0;
  222. }
  223. if (mode > FF_ARRAY_ELEMS(xcodes))
  224. return AVERROR_INVALIDDATA;
  225. code = xcodes[mode - 1];
  226. for (i = 0; i < len; i++) {
  227. int x = get_bits_long(gb, code.init);
  228. if (x >= code.escape && get_bits1(gb)) {
  229. x |= 1 << code.init;
  230. if (x >= code.aescape) {
  231. int scale = get_unary(gb, 1, 9);
  232. if (scale == 9) {
  233. int scale_bits = get_bits(gb, 3);
  234. if (scale_bits > 0) {
  235. if (scale_bits == 7) {
  236. scale_bits += get_bits(gb, 5);
  237. if (scale_bits > 29)
  238. return AVERROR_INVALIDDATA;
  239. }
  240. scale = get_bits_long(gb, scale_bits) + 1;
  241. x += code.scale * scale;
  242. }
  243. x += code.bias;
  244. } else
  245. x += code.scale * scale - code.escape;
  246. } else
  247. x -= code.escape;
  248. }
  249. decoded[i] = (x >> 1) ^ -(x & 1);
  250. }
  251. return 0;
  252. }
  253. static int decode_residues(TAKDecContext *s, int32_t *decoded, int length)
  254. {
  255. GetBitContext *gb = &s->gb;
  256. int i, mode, ret;
  257. if (length > s->nb_samples)
  258. return AVERROR_INVALIDDATA;
  259. if (get_bits1(gb)) {
  260. int wlength, rval;
  261. wlength = length / s->uval;
  262. rval = length - (wlength * s->uval);
  263. if (rval < s->uval / 2)
  264. rval += s->uval;
  265. else
  266. wlength++;
  267. if (wlength <= 1 || wlength > 128)
  268. return AVERROR_INVALIDDATA;
  269. s->coding_mode[0] = mode = get_bits(gb, 6);
  270. for (i = 1; i < wlength; i++) {
  271. int c = get_unary(gb, 1, 6);
  272. switch (c) {
  273. case 6:
  274. mode = get_bits(gb, 6);
  275. break;
  276. case 5:
  277. case 4:
  278. case 3: {
  279. /* mode += sign ? (1 - c) : (c - 1) */
  280. int sign = get_bits1(gb);
  281. mode += (-sign ^ (c - 1)) + sign;
  282. break;
  283. }
  284. case 2:
  285. mode++;
  286. break;
  287. case 1:
  288. mode--;
  289. break;
  290. }
  291. s->coding_mode[i] = mode;
  292. }
  293. i = 0;
  294. while (i < wlength) {
  295. int len = 0;
  296. mode = s->coding_mode[i];
  297. do {
  298. if (i >= wlength - 1)
  299. len += rval;
  300. else
  301. len += s->uval;
  302. i++;
  303. if (i == wlength)
  304. break;
  305. } while (s->coding_mode[i] == mode);
  306. if ((ret = decode_segment(s, mode, decoded, len)) < 0)
  307. return ret;
  308. decoded += len;
  309. }
  310. } else {
  311. mode = get_bits(gb, 6);
  312. if ((ret = decode_segment(s, mode, decoded, length)) < 0)
  313. return ret;
  314. }
  315. return 0;
  316. }
  317. static int get_bits_esc4(GetBitContext *gb)
  318. {
  319. if (get_bits1(gb))
  320. return get_bits(gb, 4) + 1;
  321. else
  322. return 0;
  323. }
  324. static int decode_subframe(TAKDecContext *s, int32_t *decoded,
  325. int subframe_size, int prev_subframe_size)
  326. {
  327. GetBitContext *gb = &s->gb;
  328. int tmp, x, y, i, j, ret = 0;
  329. int dshift, size, filter_quant, filter_order;
  330. int tfilter[MAX_PREDICTORS];
  331. if (!get_bits1(gb))
  332. return decode_residues(s, decoded, subframe_size);
  333. filter_order = predictor_sizes[get_bits(gb, 4)];
  334. if (prev_subframe_size > 0 && get_bits1(gb)) {
  335. if (filter_order > prev_subframe_size)
  336. return AVERROR_INVALIDDATA;
  337. decoded -= filter_order;
  338. subframe_size += filter_order;
  339. if (filter_order > subframe_size)
  340. return AVERROR_INVALIDDATA;
  341. } else {
  342. int lpc_mode;
  343. if (filter_order > subframe_size)
  344. return AVERROR_INVALIDDATA;
  345. lpc_mode = get_bits(gb, 2);
  346. if (lpc_mode > 2)
  347. return AVERROR_INVALIDDATA;
  348. if ((ret = decode_residues(s, decoded, filter_order)) < 0)
  349. return ret;
  350. if (lpc_mode)
  351. decode_lpc(decoded, lpc_mode, filter_order);
  352. }
  353. dshift = get_bits_esc4(gb);
  354. size = get_bits1(gb) + 6;
  355. filter_quant = 10;
  356. if (get_bits1(gb)) {
  357. filter_quant -= get_bits(gb, 3) + 1;
  358. if (filter_quant < 3)
  359. return AVERROR_INVALIDDATA;
  360. }
  361. s->predictors[0] = get_sbits(gb, 10);
  362. s->predictors[1] = get_sbits(gb, 10);
  363. s->predictors[2] = get_sbits(gb, size) << (10 - size);
  364. s->predictors[3] = get_sbits(gb, size) << (10 - size);
  365. if (filter_order > 4) {
  366. tmp = size - get_bits1(gb);
  367. for (i = 4; i < filter_order; i++) {
  368. if (!(i & 3))
  369. x = tmp - get_bits(gb, 2);
  370. s->predictors[i] = get_sbits(gb, x) << (10 - size);
  371. }
  372. }
  373. tfilter[0] = s->predictors[0] << 6;
  374. for (i = 1; i < filter_order; i++) {
  375. int32_t *p1 = &tfilter[0];
  376. int32_t *p2 = &tfilter[i - 1];
  377. for (j = 0; j < (i + 1) / 2; j++) {
  378. x = *p1 + (s->predictors[i] * *p2 + 256 >> 9);
  379. *p2 += s->predictors[i] * *p1 + 256 >> 9;
  380. *p1++ = x;
  381. p2--;
  382. }
  383. tfilter[i] = s->predictors[i] << 6;
  384. }
  385. x = 1 << (32 - (15 - filter_quant));
  386. y = 1 << ((15 - filter_quant) - 1);
  387. for (i = 0, j = filter_order - 1; i < filter_order / 2; i++, j--) {
  388. tmp = y + tfilter[j];
  389. s->filter[j] = x - ((tfilter[i] + y) >> (15 - filter_quant));
  390. s->filter[i] = x - ((tfilter[j] + y) >> (15 - filter_quant));
  391. }
  392. if ((ret = decode_residues(s, &decoded[filter_order],
  393. subframe_size - filter_order)) < 0)
  394. return ret;
  395. for (i = 0; i < filter_order; i++)
  396. s->residues[i] = *decoded++ >> dshift;
  397. y = FF_ARRAY_ELEMS(s->residues) - filter_order;
  398. x = subframe_size - filter_order;
  399. while (x > 0) {
  400. tmp = FFMIN(y, x);
  401. for (i = 0; i < tmp; i++) {
  402. int v = 1 << (filter_quant - 1);
  403. if (!(filter_order & 15)) {
  404. v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
  405. filter_order);
  406. } else if (filter_order & 4) {
  407. for (j = 0; j < filter_order; j += 4) {
  408. v += s->residues[i + j + 3] * s->filter[j + 3] +
  409. s->residues[i + j + 2] * s->filter[j + 2] +
  410. s->residues[i + j + 1] * s->filter[j + 1] +
  411. s->residues[i + j ] * s->filter[j ];
  412. }
  413. } else {
  414. for (j = 0; j < filter_order; j += 8) {
  415. v += s->residues[i + j + 7] * s->filter[j + 7] +
  416. s->residues[i + j + 6] * s->filter[j + 6] +
  417. s->residues[i + j + 5] * s->filter[j + 5] +
  418. s->residues[i + j + 4] * s->filter[j + 4] +
  419. 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. }
  425. v = (av_clip(v >> filter_quant, -8192, 8191) << dshift) - *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] + 1;
  477. int32_t *p2 = s->decoded[c2] + 1;
  478. int i;
  479. int dshift, dfactor;
  480. switch (s->dmode) {
  481. case 1: /* left/side */
  482. for (i = 0; i < length; i++) {
  483. int32_t a = p1[i];
  484. int32_t b = p2[i];
  485. p2[i] = a + b;
  486. }
  487. break;
  488. case 2: /* side/right */
  489. for (i = 0; i < length; i++) {
  490. int32_t a = p1[i];
  491. int32_t b = p2[i];
  492. p1[i] = b - a;
  493. }
  494. break;
  495. case 3: /* side/mid */
  496. for (i = 0; i < length; i++) {
  497. int32_t a = p1[i];
  498. int32_t b = p2[i];
  499. a -= b >> 1;
  500. p1[i] = a;
  501. p2[i] = a + b;
  502. }
  503. break;
  504. case 4: /* side/left with scale factor */
  505. FFSWAP(int32_t*, p1, p2);
  506. case 5: /* side/right with scale factor */
  507. dshift = get_bits_esc4(gb);
  508. dfactor = get_sbits(gb, 10);
  509. for (i = 0; i < length; i++) {
  510. int32_t a = p1[i];
  511. int32_t b = p2[i];
  512. b = dfactor * (b >> dshift) + 128 >> 8 << dshift;
  513. p1[i] = b - a;
  514. }
  515. break;
  516. case 6:
  517. FFSWAP(int32_t*, p1, p2);
  518. case 7: {
  519. int length2, order_half, filter_order, dval1, dval2;
  520. int tmp, x, code_size;
  521. if (length < 256)
  522. return AVERROR_INVALIDDATA;
  523. dshift = get_bits_esc4(gb);
  524. filter_order = 8 << get_bits1(gb);
  525. dval1 = get_bits1(gb);
  526. dval2 = get_bits1(gb);
  527. for (i = 0; i < filter_order; i++) {
  528. if (!(i & 3))
  529. code_size = 14 - get_bits(gb, 3);
  530. s->filter[i] = get_sbits(gb, code_size);
  531. }
  532. order_half = filter_order / 2;
  533. length2 = length - (filter_order - 1);
  534. /* decorrelate beginning samples */
  535. if (dval1) {
  536. for (i = 0; i < order_half; i++) {
  537. int32_t a = p1[i];
  538. int32_t b = p2[i];
  539. p1[i] = a + b;
  540. }
  541. }
  542. /* decorrelate ending samples */
  543. if (dval2) {
  544. for (i = length2 + order_half; i < length; i++) {
  545. int32_t a = p1[i];
  546. int32_t b = p2[i];
  547. p1[i] = a + b;
  548. }
  549. }
  550. for (i = 0; i < filter_order; i++)
  551. s->residues[i] = *p2++ >> dshift;
  552. p1 += order_half;
  553. x = FF_ARRAY_ELEMS(s->residues) - filter_order;
  554. for (; length2 > 0; length2 -= tmp) {
  555. tmp = FFMIN(length2, x);
  556. for (i = 0; i < tmp; i++)
  557. s->residues[filter_order + i] = *p2++ >> dshift;
  558. for (i = 0; i < tmp; i++) {
  559. int v = 1 << 9;
  560. if (filter_order == 16) {
  561. v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
  562. filter_order);
  563. } else {
  564. v += s->residues[i + 7] * s->filter[7] +
  565. s->residues[i + 6] * s->filter[6] +
  566. s->residues[i + 5] * s->filter[5] +
  567. s->residues[i + 4] * s->filter[4] +
  568. s->residues[i + 3] * s->filter[3] +
  569. s->residues[i + 2] * s->filter[2] +
  570. s->residues[i + 1] * s->filter[1] +
  571. s->residues[i ] * s->filter[0];
  572. }
  573. v = (av_clip(v >> 10, -8192, 8191) << dshift) - *p1;
  574. *p1++ = v;
  575. }
  576. memcpy(s->residues, &s->residues[tmp], 2 * filter_order);
  577. }
  578. emms_c();
  579. break;
  580. }
  581. }
  582. return 0;
  583. }
  584. static int tak_decode_frame(AVCodecContext *avctx, void *data,
  585. int *got_frame_ptr, AVPacket *pkt)
  586. {
  587. TAKDecContext *s = avctx->priv_data;
  588. AVFrame *frame = data;
  589. GetBitContext *gb = &s->gb;
  590. int chan, i, ret, hsize;
  591. if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
  592. return AVERROR_INVALIDDATA;
  593. if ((ret = init_get_bits8(gb, pkt->data, pkt->size)) < 0)
  594. return ret;
  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. frame->nb_samples = s->nb_samples;
  643. if ((ret = ff_get_buffer(avctx, 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 *)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 *)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 *)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 *)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. return pkt->size;
  782. }
  783. static av_cold int tak_decode_close(AVCodecContext *avctx)
  784. {
  785. TAKDecContext *s = avctx->priv_data;
  786. av_freep(&s->decode_buffer);
  787. return 0;
  788. }
  789. AVCodec ff_tak_decoder = {
  790. .name = "tak",
  791. .type = AVMEDIA_TYPE_AUDIO,
  792. .id = AV_CODEC_ID_TAK,
  793. .priv_data_size = sizeof(TAKDecContext),
  794. .init = tak_decode_init,
  795. .close = tak_decode_close,
  796. .decode = tak_decode_frame,
  797. .capabilities = CODEC_CAP_DR1,
  798. .long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
  799. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
  800. AV_SAMPLE_FMT_S16P,
  801. AV_SAMPLE_FMT_S32P,
  802. AV_SAMPLE_FMT_NONE },
  803. };