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.

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