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.

484 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. #include <limits.h>
  29. #include "libavutil/crc.h"
  30. #define BITSTREAM_READER_LE
  31. #include "avcodec.h"
  32. #include "get_bits.h"
  33. #include "internal.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. GetBitContext gb;
  54. const AVCRC *crc_table;
  55. int format, channels, bps;
  56. unsigned data_length;
  57. int frame_length, last_frame_length;
  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 int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
  158. {
  159. uint32_t crc, CRC;
  160. CRC = AV_RL32(buf + buf_size);
  161. crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
  162. if (CRC != (crc ^ 0xFFFFFFFFU)) {
  163. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  164. return AVERROR_INVALIDDATA;
  165. }
  166. return 0;
  167. }
  168. static av_cold int tta_decode_init(AVCodecContext * avctx)
  169. {
  170. TTAContext *s = avctx->priv_data;
  171. int total_frames;
  172. s->avctx = avctx;
  173. // 30bytes includes a seektable with one frame
  174. if (avctx->extradata_size < 30)
  175. return -1;
  176. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
  177. if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
  178. {
  179. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  180. s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
  181. tta_check_crc(s, avctx->extradata, 18);
  182. }
  183. /* signature */
  184. skip_bits_long(&s->gb, 32);
  185. s->format = get_bits(&s->gb, 16);
  186. if (s->format > 2) {
  187. av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n");
  188. return -1;
  189. }
  190. if (s->format == FORMAT_ENCRYPTED) {
  191. avpriv_report_missing_feature(s->avctx, "Encrypted TTA");
  192. return AVERROR_PATCHWELCOME;
  193. }
  194. avctx->channels = s->channels = get_bits(&s->gb, 16);
  195. avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
  196. s->bps = (avctx->bits_per_coded_sample + 7) / 8;
  197. avctx->sample_rate = get_bits_long(&s->gb, 32);
  198. s->data_length = get_bits_long(&s->gb, 32);
  199. skip_bits_long(&s->gb, 32); // CRC32 of header
  200. if (s->channels == 0) {
  201. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  202. return AVERROR_INVALIDDATA;
  203. } else if (avctx->sample_rate == 0) {
  204. av_log(s->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
  205. return AVERROR_INVALIDDATA;
  206. }
  207. switch(s->bps) {
  208. case 2:
  209. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  210. avctx->bits_per_raw_sample = 16;
  211. break;
  212. case 3:
  213. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  214. avctx->bits_per_raw_sample = 24;
  215. break;
  216. default:
  217. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
  218. return AVERROR_INVALIDDATA;
  219. }
  220. // prevent overflow
  221. if (avctx->sample_rate > 0x7FFFFFu) {
  222. av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
  223. return AVERROR(EINVAL);
  224. }
  225. s->frame_length = 256 * avctx->sample_rate / 245;
  226. s->last_frame_length = s->data_length % s->frame_length;
  227. total_frames = s->data_length / s->frame_length +
  228. (s->last_frame_length ? 1 : 0);
  229. av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
  230. s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
  231. avctx->block_align);
  232. av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  233. s->data_length, s->frame_length, s->last_frame_length, total_frames);
  234. // FIXME: seek table
  235. if (avctx->extradata_size <= 26 || total_frames > INT_MAX / 4 ||
  236. avctx->extradata_size - 26 < total_frames * 4)
  237. av_log(avctx, AV_LOG_WARNING, "Seek table missing or too small\n");
  238. else if (avctx->err_recognition & AV_EF_CRCCHECK) {
  239. int ret = tta_check_crc(s, avctx->extradata + 22, total_frames * 4);
  240. if (ret < 0 && avctx->err_recognition & AV_EF_EXPLODE)
  241. return AVERROR_INVALIDDATA;
  242. }
  243. skip_bits_long(&s->gb, 32 * total_frames);
  244. skip_bits_long(&s->gb, 32); // CRC32 of seektable
  245. if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
  246. av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
  247. return -1;
  248. }
  249. if (s->bps == 2) {
  250. s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
  251. if (!s->decode_buffer)
  252. return AVERROR(ENOMEM);
  253. }
  254. s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
  255. if (!s->ch_ctx) {
  256. av_freep(&s->decode_buffer);
  257. return AVERROR(ENOMEM);
  258. }
  259. } else {
  260. av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
  261. return -1;
  262. }
  263. return 0;
  264. }
  265. static int tta_decode_frame(AVCodecContext *avctx, void *data,
  266. int *got_frame_ptr, AVPacket *avpkt)
  267. {
  268. AVFrame *frame = data;
  269. const uint8_t *buf = avpkt->data;
  270. int buf_size = avpkt->size;
  271. TTAContext *s = avctx->priv_data;
  272. int i, ret;
  273. int cur_chan = 0, framelen = s->frame_length;
  274. int32_t *p;
  275. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  276. if (buf_size < 4 ||
  277. (tta_check_crc(s, buf, buf_size - 4) && avctx->err_recognition & AV_EF_EXPLODE))
  278. return AVERROR_INVALIDDATA;
  279. }
  280. init_get_bits(&s->gb, buf, buf_size*8);
  281. /* get output buffer */
  282. frame->nb_samples = framelen;
  283. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  284. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  285. return ret;
  286. }
  287. // decode directly to output buffer for 24-bit sample format
  288. if (s->bps == 3)
  289. s->decode_buffer = (int32_t *)frame->data[0];
  290. // init per channel states
  291. for (i = 0; i < s->channels; i++) {
  292. s->ch_ctx[i].predictor = 0;
  293. ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1]);
  294. rice_init(&s->ch_ctx[i].rice, 10, 10);
  295. }
  296. i = 0;
  297. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  298. int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
  299. TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
  300. TTARice *rice = &s->ch_ctx[cur_chan].rice;
  301. uint32_t unary, depth, k;
  302. int32_t value;
  303. unary = tta_get_unary(&s->gb);
  304. if (unary == 0) {
  305. depth = 0;
  306. k = rice->k0;
  307. } else {
  308. depth = 1;
  309. k = rice->k1;
  310. unary--;
  311. }
  312. if (get_bits_left(&s->gb) < k) {
  313. ret = AVERROR_INVALIDDATA;
  314. goto error;
  315. }
  316. if (k) {
  317. if (k > MIN_CACHE_BITS) {
  318. ret = AVERROR_INVALIDDATA;
  319. goto error;
  320. }
  321. value = (unary << k) + get_bits(&s->gb, k);
  322. } else
  323. value = unary;
  324. // FIXME: copy paste from original
  325. switch (depth) {
  326. case 1:
  327. rice->sum1 += value - (rice->sum1 >> 4);
  328. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  329. rice->k1--;
  330. else if(rice->sum1 > shift_16[rice->k1 + 1])
  331. rice->k1++;
  332. value += shift_1[rice->k0];
  333. default:
  334. rice->sum0 += value - (rice->sum0 >> 4);
  335. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  336. rice->k0--;
  337. else if(rice->sum0 > shift_16[rice->k0 + 1])
  338. rice->k0++;
  339. }
  340. // extract coded value
  341. *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
  342. // run hybrid filter
  343. ttafilter_process(filter, p);
  344. // fixed order prediction
  345. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  346. switch (s->bps) {
  347. case 1: *p += PRED(*predictor, 4); break;
  348. case 2:
  349. case 3: *p += PRED(*predictor, 5); break;
  350. case 4: *p += *predictor; break;
  351. }
  352. *predictor = *p;
  353. // flip channels
  354. if (cur_chan < (s->channels-1))
  355. cur_chan++;
  356. else {
  357. // decorrelate in case of multiple channels
  358. if (s->channels > 1) {
  359. int32_t *r = p - 1;
  360. for (*p += *r / 2; r > p - s->channels; r--)
  361. *r = *(r + 1) - *r;
  362. }
  363. cur_chan = 0;
  364. i++;
  365. // check for last frame
  366. if (i == s->last_frame_length && get_bits_left(&s->gb) / 8 == 4) {
  367. frame->nb_samples = framelen = s->last_frame_length;
  368. break;
  369. }
  370. }
  371. }
  372. align_get_bits(&s->gb);
  373. if (get_bits_left(&s->gb) < 32) {
  374. ret = AVERROR_INVALIDDATA;
  375. goto error;
  376. }
  377. skip_bits_long(&s->gb, 32); // frame crc
  378. // convert to output buffer
  379. if (s->bps == 2) {
  380. int16_t *samples = (int16_t *)frame->data[0];
  381. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  382. *samples++ = *p;
  383. } else {
  384. // shift samples for 24-bit sample format
  385. int32_t *samples = (int32_t *)frame->data[0];
  386. for (i = 0; i < framelen * s->channels; i++)
  387. *samples++ <<= 8;
  388. // reset decode buffer
  389. s->decode_buffer = NULL;
  390. }
  391. *got_frame_ptr = 1;
  392. return buf_size;
  393. error:
  394. // reset decode buffer
  395. if (s->bps == 3)
  396. s->decode_buffer = NULL;
  397. return ret;
  398. }
  399. static av_cold int tta_decode_close(AVCodecContext *avctx) {
  400. TTAContext *s = avctx->priv_data;
  401. av_free(s->decode_buffer);
  402. av_freep(&s->ch_ctx);
  403. return 0;
  404. }
  405. AVCodec ff_tta_decoder = {
  406. .name = "tta",
  407. .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
  408. .type = AVMEDIA_TYPE_AUDIO,
  409. .id = AV_CODEC_ID_TTA,
  410. .priv_data_size = sizeof(TTAContext),
  411. .init = tta_decode_init,
  412. .close = tta_decode_close,
  413. .decode = tta_decode_frame,
  414. .capabilities = AV_CODEC_CAP_DR1,
  415. };