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.

519 lines
15KB

  1. /*
  2. * TTA (The Lossless True Audio) decoder
  3. * Copyright (c) 2006 Alex Beregszaszi
  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. * TTA (The Lossless True Audio) decoder
  24. * @see http://www.true-audio.com/
  25. * @see http://tta.corecodec.org/
  26. * @author Alex Beregszaszi
  27. */
  28. #define BITSTREAM_READER_LE
  29. //#define DEBUG
  30. #include <limits.h>
  31. #include "avcodec.h"
  32. #include "get_bits.h"
  33. #include "libavutil/crc.h"
  34. #define FORMAT_SIMPLE 1
  35. #define FORMAT_ENCRYPTED 2
  36. #define MAX_ORDER 16
  37. typedef struct TTAFilter {
  38. int32_t shift, round, error, mode;
  39. int32_t qm[MAX_ORDER];
  40. int32_t dx[MAX_ORDER];
  41. int32_t dl[MAX_ORDER];
  42. } TTAFilter;
  43. typedef struct TTARice {
  44. uint32_t k0, k1, sum0, sum1;
  45. } TTARice;
  46. typedef struct TTAChannel {
  47. int32_t predictor;
  48. TTAFilter filter;
  49. TTARice rice;
  50. } TTAChannel;
  51. typedef struct TTAContext {
  52. AVCodecContext *avctx;
  53. AVFrame frame;
  54. GetBitContext gb;
  55. const AVCRC *crc_table;
  56. int format, channels, bps, data_length;
  57. int frame_length, last_frame_length, total_frames;
  58. int32_t *decode_buffer;
  59. TTAChannel *ch_ctx;
  60. } TTAContext;
  61. static const uint32_t shift_1[] = {
  62. 0x00000001, 0x00000002, 0x00000004, 0x00000008,
  63. 0x00000010, 0x00000020, 0x00000040, 0x00000080,
  64. 0x00000100, 0x00000200, 0x00000400, 0x00000800,
  65. 0x00001000, 0x00002000, 0x00004000, 0x00008000,
  66. 0x00010000, 0x00020000, 0x00040000, 0x00080000,
  67. 0x00100000, 0x00200000, 0x00400000, 0x00800000,
  68. 0x01000000, 0x02000000, 0x04000000, 0x08000000,
  69. 0x10000000, 0x20000000, 0x40000000, 0x80000000,
  70. 0x80000000, 0x80000000, 0x80000000, 0x80000000,
  71. 0x80000000, 0x80000000, 0x80000000, 0x80000000
  72. };
  73. static const uint32_t * const shift_16 = shift_1 + 4;
  74. static const int32_t ttafilter_configs[4][2] = {
  75. {10, 1},
  76. {9, 1},
  77. {10, 1},
  78. {12, 0}
  79. };
  80. static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) {
  81. memset(c, 0, sizeof(TTAFilter));
  82. c->shift = shift;
  83. c->round = shift_1[shift-1];
  84. // c->round = 1 << (shift - 1);
  85. c->mode = mode;
  86. }
  87. // FIXME: copy paste from original
  88. static inline void memshl(register int32_t *a, register int32_t *b) {
  89. *a++ = *b++;
  90. *a++ = *b++;
  91. *a++ = *b++;
  92. *a++ = *b++;
  93. *a++ = *b++;
  94. *a++ = *b++;
  95. *a++ = *b++;
  96. *a = *b;
  97. }
  98. // FIXME: copy paste from original
  99. // mode=1 encoder, mode=0 decoder
  100. static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) {
  101. register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
  102. if (!c->error) {
  103. sum += *dl++ * *qm, qm++;
  104. sum += *dl++ * *qm, qm++;
  105. sum += *dl++ * *qm, qm++;
  106. sum += *dl++ * *qm, qm++;
  107. sum += *dl++ * *qm, qm++;
  108. sum += *dl++ * *qm, qm++;
  109. sum += *dl++ * *qm, qm++;
  110. sum += *dl++ * *qm, qm++;
  111. dx += 8;
  112. } else if(c->error < 0) {
  113. sum += *dl++ * (*qm -= *dx++), qm++;
  114. sum += *dl++ * (*qm -= *dx++), qm++;
  115. sum += *dl++ * (*qm -= *dx++), qm++;
  116. sum += *dl++ * (*qm -= *dx++), qm++;
  117. sum += *dl++ * (*qm -= *dx++), qm++;
  118. sum += *dl++ * (*qm -= *dx++), qm++;
  119. sum += *dl++ * (*qm -= *dx++), qm++;
  120. sum += *dl++ * (*qm -= *dx++), qm++;
  121. } else {
  122. sum += *dl++ * (*qm += *dx++), qm++;
  123. sum += *dl++ * (*qm += *dx++), qm++;
  124. sum += *dl++ * (*qm += *dx++), qm++;
  125. sum += *dl++ * (*qm += *dx++), qm++;
  126. sum += *dl++ * (*qm += *dx++), qm++;
  127. sum += *dl++ * (*qm += *dx++), qm++;
  128. sum += *dl++ * (*qm += *dx++), qm++;
  129. sum += *dl++ * (*qm += *dx++), qm++;
  130. }
  131. *(dx-0) = ((*(dl-1) >> 30) | 1) << 2;
  132. *(dx-1) = ((*(dl-2) >> 30) | 1) << 1;
  133. *(dx-2) = ((*(dl-3) >> 30) | 1) << 1;
  134. *(dx-3) = ((*(dl-4) >> 30) | 1);
  135. // compress
  136. if (mode) {
  137. *dl = *in;
  138. *in -= (sum >> c->shift);
  139. c->error = *in;
  140. } else {
  141. c->error = *in;
  142. *in += (sum >> c->shift);
  143. *dl = *in;
  144. }
  145. if (c->mode) {
  146. *(dl-1) = *dl - *(dl-1);
  147. *(dl-2) = *(dl-1) - *(dl-2);
  148. *(dl-3) = *(dl-2) - *(dl-3);
  149. }
  150. memshl(c->dl, c->dl + 1);
  151. memshl(c->dx, c->dx + 1);
  152. }
  153. static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
  154. {
  155. c->k0 = k0;
  156. c->k1 = k1;
  157. c->sum0 = shift_16[k0];
  158. c->sum1 = shift_16[k1];
  159. }
  160. static int tta_get_unary(GetBitContext *gb)
  161. {
  162. int ret = 0;
  163. // count ones
  164. while (get_bits_left(gb) > 0 && get_bits1(gb))
  165. ret++;
  166. return ret;
  167. }
  168. static const int64_t tta_channel_layouts[7] = {
  169. AV_CH_LAYOUT_STEREO,
  170. AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
  171. AV_CH_LAYOUT_QUAD,
  172. 0,
  173. AV_CH_LAYOUT_5POINT1_BACK,
  174. AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
  175. AV_CH_LAYOUT_7POINT1_WIDE
  176. };
  177. static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
  178. {
  179. uint32_t crc, CRC;
  180. CRC = AV_RL32(buf + buf_size);
  181. crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
  182. if (CRC != (crc ^ 0xFFFFFFFFU)) {
  183. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  184. return AVERROR_INVALIDDATA;
  185. }
  186. return 0;
  187. }
  188. static av_cold int tta_decode_init(AVCodecContext * avctx)
  189. {
  190. TTAContext *s = avctx->priv_data;
  191. s->avctx = avctx;
  192. // 30bytes includes a seektable with one frame
  193. if (avctx->extradata_size < 30)
  194. return -1;
  195. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
  196. if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
  197. {
  198. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  199. s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
  200. if (tta_check_crc(s, avctx->extradata, 18))
  201. return AVERROR_INVALIDDATA;
  202. }
  203. /* signature */
  204. skip_bits_long(&s->gb, 32);
  205. s->format = get_bits(&s->gb, 16);
  206. if (s->format > 2) {
  207. av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n");
  208. return -1;
  209. }
  210. if (s->format == FORMAT_ENCRYPTED) {
  211. av_log_missing_feature(s->avctx, "Encrypted TTA", 0);
  212. return AVERROR(EINVAL);
  213. }
  214. avctx->channels = s->channels = get_bits(&s->gb, 16);
  215. if (s->channels > 1 && s->channels < 9)
  216. avctx->channel_layout = tta_channel_layouts[s->channels-2];
  217. avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
  218. s->bps = (avctx->bits_per_coded_sample + 7) / 8;
  219. avctx->sample_rate = get_bits_long(&s->gb, 32);
  220. s->data_length = get_bits_long(&s->gb, 32);
  221. skip_bits_long(&s->gb, 32); // CRC32 of header
  222. if (s->channels == 0) {
  223. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  224. return AVERROR_INVALIDDATA;
  225. } else if (avctx->sample_rate == 0) {
  226. av_log(s->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
  227. return AVERROR_INVALIDDATA;
  228. }
  229. switch(s->bps) {
  230. case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
  231. case 2:
  232. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  233. avctx->bits_per_raw_sample = 16;
  234. break;
  235. case 3:
  236. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  237. avctx->bits_per_raw_sample = 24;
  238. break;
  239. //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
  240. default:
  241. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
  242. return AVERROR_INVALIDDATA;
  243. }
  244. // prevent overflow
  245. if (avctx->sample_rate > 0x7FFFFF) {
  246. av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
  247. return AVERROR(EINVAL);
  248. }
  249. s->frame_length = 256 * avctx->sample_rate / 245;
  250. s->last_frame_length = s->data_length % s->frame_length;
  251. s->total_frames = s->data_length / s->frame_length +
  252. (s->last_frame_length ? 1 : 0);
  253. av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
  254. s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
  255. avctx->block_align);
  256. av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  257. s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
  258. // FIXME: seek table
  259. if (get_bits_left(&s->gb) < 32 * s->total_frames + 32)
  260. av_log(avctx, AV_LOG_WARNING, "Seek table missing or too small\n");
  261. else if (avctx->err_recognition & AV_EF_CRCCHECK) {
  262. if (tta_check_crc(s, avctx->extradata + 22, s->total_frames * 4))
  263. return AVERROR_INVALIDDATA;
  264. }
  265. skip_bits_long(&s->gb, 32 * s->total_frames);
  266. skip_bits_long(&s->gb, 32); // CRC32 of seektable
  267. if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
  268. av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
  269. return -1;
  270. }
  271. if (s->bps < 3) {
  272. s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
  273. if (!s->decode_buffer)
  274. return AVERROR(ENOMEM);
  275. } else
  276. s->decode_buffer = NULL;
  277. s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
  278. if (!s->ch_ctx) {
  279. av_freep(&s->decode_buffer);
  280. return AVERROR(ENOMEM);
  281. }
  282. } else {
  283. av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
  284. return -1;
  285. }
  286. avcodec_get_frame_defaults(&s->frame);
  287. avctx->coded_frame = &s->frame;
  288. return 0;
  289. }
  290. static int tta_decode_frame(AVCodecContext *avctx, void *data,
  291. int *got_frame_ptr, AVPacket *avpkt)
  292. {
  293. const uint8_t *buf = avpkt->data;
  294. int buf_size = avpkt->size;
  295. TTAContext *s = avctx->priv_data;
  296. int i, ret;
  297. int cur_chan = 0, framelen = s->frame_length;
  298. int32_t *p;
  299. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  300. if (buf_size < 4 || tta_check_crc(s, buf, buf_size - 4))
  301. return AVERROR_INVALIDDATA;
  302. }
  303. init_get_bits(&s->gb, buf, buf_size*8);
  304. // FIXME: seeking
  305. s->total_frames--;
  306. if (!s->total_frames && s->last_frame_length)
  307. framelen = s->last_frame_length;
  308. /* get output buffer */
  309. s->frame.nb_samples = framelen;
  310. if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
  311. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  312. return ret;
  313. }
  314. // decode directly to output buffer for 24-bit sample format
  315. if (s->bps == 3)
  316. s->decode_buffer = (int32_t *)s->frame.data[0];
  317. // init per channel states
  318. for (i = 0; i < s->channels; i++) {
  319. s->ch_ctx[i].predictor = 0;
  320. ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
  321. rice_init(&s->ch_ctx[i].rice, 10, 10);
  322. }
  323. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  324. int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
  325. TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
  326. TTARice *rice = &s->ch_ctx[cur_chan].rice;
  327. uint32_t unary, depth, k;
  328. int32_t value;
  329. unary = tta_get_unary(&s->gb);
  330. if (unary == 0) {
  331. depth = 0;
  332. k = rice->k0;
  333. } else {
  334. depth = 1;
  335. k = rice->k1;
  336. unary--;
  337. }
  338. if (get_bits_left(&s->gb) < k) {
  339. ret = AVERROR_INVALIDDATA;
  340. goto error;
  341. }
  342. if (k) {
  343. if (k > MIN_CACHE_BITS) {
  344. ret = AVERROR_INVALIDDATA;
  345. goto error;
  346. }
  347. value = (unary << k) + get_bits(&s->gb, k);
  348. } else
  349. value = unary;
  350. // FIXME: copy paste from original
  351. switch (depth) {
  352. case 1:
  353. rice->sum1 += value - (rice->sum1 >> 4);
  354. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  355. rice->k1--;
  356. else if(rice->sum1 > shift_16[rice->k1 + 1])
  357. rice->k1++;
  358. value += shift_1[rice->k0];
  359. default:
  360. rice->sum0 += value - (rice->sum0 >> 4);
  361. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  362. rice->k0--;
  363. else if(rice->sum0 > shift_16[rice->k0 + 1])
  364. rice->k0++;
  365. }
  366. // extract coded value
  367. #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
  368. *p = UNFOLD(value);
  369. // run hybrid filter
  370. ttafilter_process(filter, p, 0);
  371. // fixed order prediction
  372. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  373. switch (s->bps) {
  374. case 1: *p += PRED(*predictor, 4); break;
  375. case 2:
  376. case 3: *p += PRED(*predictor, 5); break;
  377. case 4: *p += *predictor; break;
  378. }
  379. *predictor = *p;
  380. // flip channels
  381. if (cur_chan < (s->channels-1))
  382. cur_chan++;
  383. else {
  384. // decorrelate in case of stereo integer
  385. if (s->channels > 1) {
  386. int32_t *r = p - 1;
  387. for (*p += *r / 2; r > p - s->channels; r--)
  388. *r = *(r + 1) - *r;
  389. }
  390. cur_chan = 0;
  391. }
  392. }
  393. if (get_bits_left(&s->gb) < 32) {
  394. ret = AVERROR_INVALIDDATA;
  395. goto error;
  396. }
  397. skip_bits_long(&s->gb, 32); // frame crc
  398. // convert to output buffer
  399. switch (s->bps) {
  400. case 1: {
  401. uint8_t *samples = (uint8_t *)s->frame.data[0];
  402. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  403. *samples++ = *p + 0x80;
  404. break;
  405. }
  406. case 2: {
  407. uint16_t *samples = (int16_t *)s->frame.data[0];
  408. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  409. *samples++ = *p;
  410. break;
  411. }
  412. case 3: {
  413. // shift samples for 24-bit sample format
  414. int32_t *samples = (int32_t *)s->frame.data[0];
  415. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  416. *samples++ <<= 8;
  417. // reset decode buffer
  418. s->decode_buffer = NULL;
  419. break;
  420. }
  421. }
  422. *got_frame_ptr = 1;
  423. *(AVFrame *)data = s->frame;
  424. return buf_size;
  425. error:
  426. // reset decode buffer
  427. if (s->bps == 3)
  428. s->decode_buffer = NULL;
  429. return ret;
  430. }
  431. static av_cold int tta_decode_close(AVCodecContext *avctx) {
  432. TTAContext *s = avctx->priv_data;
  433. if (s->bps < 3)
  434. av_free(s->decode_buffer);
  435. s->decode_buffer = NULL;
  436. av_freep(&s->ch_ctx);
  437. return 0;
  438. }
  439. AVCodec ff_tta_decoder = {
  440. .name = "tta",
  441. .type = AVMEDIA_TYPE_AUDIO,
  442. .id = CODEC_ID_TTA,
  443. .priv_data_size = sizeof(TTAContext),
  444. .init = tta_decode_init,
  445. .close = tta_decode_close,
  446. .decode = tta_decode_frame,
  447. .capabilities = CODEC_CAP_DR1,
  448. .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
  449. };