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.

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