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.

482 lines
14KB

  1. /*
  2. * TTA (The Lossless True Audio) decoder
  3. * Copyright (c) 2006 Alex Beregszaszi
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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;
  57. unsigned data_length;
  58. int frame_length, last_frame_length, total_frames;
  59. int32_t *decode_buffer;
  60. TTAChannel *ch_ctx;
  61. } TTAContext;
  62. static const uint32_t shift_1[] = {
  63. 0x00000001, 0x00000002, 0x00000004, 0x00000008,
  64. 0x00000010, 0x00000020, 0x00000040, 0x00000080,
  65. 0x00000100, 0x00000200, 0x00000400, 0x00000800,
  66. 0x00001000, 0x00002000, 0x00004000, 0x00008000,
  67. 0x00010000, 0x00020000, 0x00040000, 0x00080000,
  68. 0x00100000, 0x00200000, 0x00400000, 0x00800000,
  69. 0x01000000, 0x02000000, 0x04000000, 0x08000000,
  70. 0x10000000, 0x20000000, 0x40000000, 0x80000000,
  71. 0x80000000, 0x80000000, 0x80000000, 0x80000000,
  72. 0x80000000, 0x80000000, 0x80000000, 0x80000000
  73. };
  74. static const uint32_t * const shift_16 = shift_1 + 4;
  75. static const int32_t ttafilter_configs[4][2] = {
  76. {10, 1},
  77. {9, 1},
  78. {10, 1},
  79. {12, 0}
  80. };
  81. static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) {
  82. memset(c, 0, sizeof(TTAFilter));
  83. c->shift = shift;
  84. c->round = shift_1[shift-1];
  85. // c->round = 1 << (shift - 1);
  86. c->mode = mode;
  87. }
  88. // FIXME: copy paste from original
  89. static inline void memshl(register int32_t *a, register int32_t *b) {
  90. *a++ = *b++;
  91. *a++ = *b++;
  92. *a++ = *b++;
  93. *a++ = *b++;
  94. *a++ = *b++;
  95. *a++ = *b++;
  96. *a++ = *b++;
  97. *a = *b;
  98. }
  99. // FIXME: copy paste from original
  100. // mode=1 encoder, mode=0 decoder
  101. static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) {
  102. register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
  103. if (!c->error) {
  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. sum += *dl++ * *qm, qm++;
  112. dx += 8;
  113. } else if(c->error < 0) {
  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. sum += *dl++ * (*qm -= *dx++), qm++;
  122. } else {
  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. sum += *dl++ * (*qm += *dx++), qm++;
  131. }
  132. *(dx-0) = ((*(dl-1) >> 30) | 1) << 2;
  133. *(dx-1) = ((*(dl-2) >> 30) | 1) << 1;
  134. *(dx-2) = ((*(dl-3) >> 30) | 1) << 1;
  135. *(dx-3) = ((*(dl-4) >> 30) | 1);
  136. // compress
  137. if (mode) {
  138. *dl = *in;
  139. *in -= (sum >> c->shift);
  140. c->error = *in;
  141. } else {
  142. c->error = *in;
  143. *in += (sum >> c->shift);
  144. *dl = *in;
  145. }
  146. if (c->mode) {
  147. *(dl-1) = *dl - *(dl-1);
  148. *(dl-2) = *(dl-1) - *(dl-2);
  149. *(dl-3) = *(dl-2) - *(dl-3);
  150. }
  151. memshl(c->dl, c->dl + 1);
  152. memshl(c->dx, c->dx + 1);
  153. }
  154. static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
  155. {
  156. c->k0 = k0;
  157. c->k1 = k1;
  158. c->sum0 = shift_16[k0];
  159. c->sum1 = shift_16[k1];
  160. }
  161. static int tta_get_unary(GetBitContext *gb)
  162. {
  163. int ret = 0;
  164. // count ones
  165. while (get_bits_left(gb) > 0 && get_bits1(gb))
  166. ret++;
  167. return ret;
  168. }
  169. static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
  170. {
  171. uint32_t crc, CRC;
  172. CRC = AV_RL32(buf + buf_size);
  173. crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
  174. if (CRC != (crc ^ 0xFFFFFFFFU)) {
  175. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  176. return AVERROR_INVALIDDATA;
  177. }
  178. return 0;
  179. }
  180. static av_cold int tta_decode_init(AVCodecContext * avctx)
  181. {
  182. TTAContext *s = avctx->priv_data;
  183. s->avctx = avctx;
  184. // 30bytes includes a seektable with one frame
  185. if (avctx->extradata_size < 30)
  186. return -1;
  187. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
  188. if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
  189. {
  190. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  191. s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
  192. if (tta_check_crc(s, avctx->extradata, 18))
  193. return AVERROR_INVALIDDATA;
  194. }
  195. /* signature */
  196. skip_bits_long(&s->gb, 32);
  197. s->format = get_bits(&s->gb, 16);
  198. if (s->format > 2) {
  199. av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n");
  200. return -1;
  201. }
  202. if (s->format == FORMAT_ENCRYPTED) {
  203. av_log_missing_feature(s->avctx, "Encrypted TTA", 0);
  204. return AVERROR(EINVAL);
  205. }
  206. avctx->channels = s->channels = get_bits(&s->gb, 16);
  207. avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
  208. s->bps = (avctx->bits_per_coded_sample + 7) / 8;
  209. avctx->sample_rate = get_bits_long(&s->gb, 32);
  210. s->data_length = get_bits_long(&s->gb, 32);
  211. skip_bits_long(&s->gb, 32); // CRC32 of header
  212. if (s->channels == 0) {
  213. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  214. return AVERROR_INVALIDDATA;
  215. } else if (avctx->sample_rate == 0) {
  216. av_log(s->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
  217. return AVERROR_INVALIDDATA;
  218. }
  219. switch(s->bps) {
  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. default:
  229. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
  230. return AVERROR_INVALIDDATA;
  231. }
  232. // prevent overflow
  233. if (avctx->sample_rate > 0x7FFFFFu) {
  234. av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
  235. return AVERROR(EINVAL);
  236. }
  237. s->frame_length = 256 * avctx->sample_rate / 245;
  238. s->last_frame_length = s->data_length % s->frame_length;
  239. s->total_frames = s->data_length / s->frame_length +
  240. (s->last_frame_length ? 1 : 0);
  241. av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
  242. s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
  243. avctx->block_align);
  244. av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  245. s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
  246. // FIXME: seek table
  247. if (avctx->extradata_size <= 26 || s->total_frames > INT_MAX / 4 ||
  248. avctx->extradata_size - 26 < s->total_frames * 4)
  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 == 2) {
  261. s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
  262. if (!s->decode_buffer)
  263. return AVERROR(ENOMEM);
  264. }
  265. s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
  266. if (!s->ch_ctx) {
  267. av_freep(&s->decode_buffer);
  268. return AVERROR(ENOMEM);
  269. }
  270. } else {
  271. av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
  272. return -1;
  273. }
  274. avcodec_get_frame_defaults(&s->frame);
  275. avctx->coded_frame = &s->frame;
  276. return 0;
  277. }
  278. static int tta_decode_frame(AVCodecContext *avctx, void *data,
  279. int *got_frame_ptr, AVPacket *avpkt)
  280. {
  281. const uint8_t *buf = avpkt->data;
  282. int buf_size = avpkt->size;
  283. TTAContext *s = avctx->priv_data;
  284. int i, ret;
  285. int cur_chan = 0, framelen = s->frame_length;
  286. int32_t *p;
  287. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  288. if (buf_size < 4 || tta_check_crc(s, buf, buf_size - 4))
  289. return AVERROR_INVALIDDATA;
  290. }
  291. init_get_bits(&s->gb, buf, buf_size*8);
  292. // FIXME: seeking
  293. s->total_frames--;
  294. if (!s->total_frames && s->last_frame_length)
  295. framelen = s->last_frame_length;
  296. /* get output buffer */
  297. s->frame.nb_samples = framelen;
  298. if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
  299. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  300. return ret;
  301. }
  302. // decode directly to output buffer for 24-bit sample format
  303. if (s->bps == 3)
  304. s->decode_buffer = (int32_t *)s->frame.data[0];
  305. // init per channel states
  306. for (i = 0; i < s->channels; i++) {
  307. s->ch_ctx[i].predictor = 0;
  308. ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
  309. rice_init(&s->ch_ctx[i].rice, 10, 10);
  310. }
  311. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  312. int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
  313. TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
  314. TTARice *rice = &s->ch_ctx[cur_chan].rice;
  315. uint32_t unary, depth, k;
  316. int32_t value;
  317. unary = tta_get_unary(&s->gb);
  318. if (unary == 0) {
  319. depth = 0;
  320. k = rice->k0;
  321. } else {
  322. depth = 1;
  323. k = rice->k1;
  324. unary--;
  325. }
  326. if (get_bits_left(&s->gb) < k)
  327. return -1;
  328. if (k) {
  329. if (k > MIN_CACHE_BITS)
  330. return -1;
  331. value = (unary << k) + get_bits(&s->gb, k);
  332. } else
  333. value = unary;
  334. // FIXME: copy paste from original
  335. switch (depth) {
  336. case 1:
  337. rice->sum1 += value - (rice->sum1 >> 4);
  338. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  339. rice->k1--;
  340. else if(rice->sum1 > shift_16[rice->k1 + 1])
  341. rice->k1++;
  342. value += shift_1[rice->k0];
  343. default:
  344. rice->sum0 += value - (rice->sum0 >> 4);
  345. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  346. rice->k0--;
  347. else if(rice->sum0 > shift_16[rice->k0 + 1])
  348. rice->k0++;
  349. }
  350. // extract coded value
  351. #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
  352. *p = UNFOLD(value);
  353. // run hybrid filter
  354. ttafilter_process(filter, p, 0);
  355. // fixed order prediction
  356. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  357. switch (s->bps) {
  358. case 1: *p += PRED(*predictor, 4); break;
  359. case 2:
  360. case 3: *p += PRED(*predictor, 5); break;
  361. case 4: *p += *predictor; break;
  362. }
  363. *predictor = *p;
  364. // flip channels
  365. if (cur_chan < (s->channels-1))
  366. cur_chan++;
  367. else {
  368. // decorrelate in case of stereo integer
  369. if (s->channels > 1) {
  370. int32_t *r = p - 1;
  371. for (*p += *r / 2; r > p - s->channels; r--)
  372. *r = *(r + 1) - *r;
  373. }
  374. cur_chan = 0;
  375. }
  376. }
  377. if (get_bits_left(&s->gb) < 32)
  378. return -1;
  379. skip_bits_long(&s->gb, 32); // frame crc
  380. // convert to output buffer
  381. if (s->bps == 2) {
  382. int16_t *samples = (int16_t *)s->frame.data[0];
  383. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  384. *samples++ = *p;
  385. } else {
  386. // shift samples for 24-bit sample format
  387. int32_t *samples = (int32_t *)s->frame.data[0];
  388. for (i = 0; i < framelen * s->channels; i++)
  389. *samples++ <<= 8;
  390. // reset decode buffer
  391. s->decode_buffer = NULL;
  392. }
  393. *got_frame_ptr = 1;
  394. *(AVFrame *)data = s->frame;
  395. return buf_size;
  396. }
  397. static av_cold int tta_decode_close(AVCodecContext *avctx) {
  398. TTAContext *s = avctx->priv_data;
  399. av_free(s->decode_buffer);
  400. av_freep(&s->ch_ctx);
  401. return 0;
  402. }
  403. AVCodec ff_tta_decoder = {
  404. .name = "tta",
  405. .type = AVMEDIA_TYPE_AUDIO,
  406. .id = CODEC_ID_TTA,
  407. .priv_data_size = sizeof(TTAContext),
  408. .init = tta_decode_init,
  409. .close = tta_decode_close,
  410. .decode = tta_decode_frame,
  411. .capabilities = CODEC_CAP_DR1,
  412. .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
  413. };