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.

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