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.

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