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.

479 lines
14KB

  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;
  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] = {
  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. static inline void ttafilter_process(TTAFilter *c, int32_t *in)
  88. {
  89. register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
  90. if (c->error < 0) {
  91. qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
  92. qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
  93. } else if (c->error > 0) {
  94. qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
  95. qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
  96. }
  97. sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
  98. dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
  99. dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
  100. dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
  101. dx[4] = ((dl[4] >> 30) | 1);
  102. dx[5] = ((dl[5] >> 30) | 2) & ~1;
  103. dx[6] = ((dl[6] >> 30) | 2) & ~1;
  104. dx[7] = ((dl[7] >> 30) | 4) & ~3;
  105. c->error = *in;
  106. *in += (sum >> c->shift);
  107. dl[4] = -dl[5]; dl[5] = -dl[6];
  108. dl[6] = *in - dl[7]; dl[7] = *in;
  109. dl[5] += dl[6]; dl[4] += dl[5];
  110. }
  111. static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
  112. {
  113. c->k0 = k0;
  114. c->k1 = k1;
  115. c->sum0 = shift_16[k0];
  116. c->sum1 = shift_16[k1];
  117. }
  118. static int tta_get_unary(GetBitContext *gb)
  119. {
  120. int ret = 0;
  121. // count ones
  122. while (get_bits_left(gb) > 0 && get_bits1(gb))
  123. ret++;
  124. return ret;
  125. }
  126. static const int64_t tta_channel_layouts[7] = {
  127. AV_CH_LAYOUT_STEREO,
  128. AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
  129. AV_CH_LAYOUT_QUAD,
  130. 0,
  131. AV_CH_LAYOUT_5POINT1_BACK,
  132. AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
  133. AV_CH_LAYOUT_7POINT1_WIDE
  134. };
  135. static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
  136. {
  137. uint32_t crc, CRC;
  138. CRC = AV_RL32(buf + buf_size);
  139. crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
  140. if (CRC != (crc ^ 0xFFFFFFFFU)) {
  141. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  142. return AVERROR_INVALIDDATA;
  143. }
  144. return 0;
  145. }
  146. static av_cold int tta_decode_init(AVCodecContext * avctx)
  147. {
  148. TTAContext *s = avctx->priv_data;
  149. s->avctx = avctx;
  150. // 30bytes includes a seektable with one frame
  151. if (avctx->extradata_size < 30)
  152. return -1;
  153. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
  154. if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
  155. {
  156. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  157. s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
  158. tta_check_crc(s, avctx->extradata, 18);
  159. }
  160. /* signature */
  161. skip_bits_long(&s->gb, 32);
  162. s->format = get_bits(&s->gb, 16);
  163. if (s->format > 2) {
  164. av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n");
  165. return -1;
  166. }
  167. if (s->format == FORMAT_ENCRYPTED) {
  168. av_log_missing_feature(s->avctx, "Encrypted TTA", 0);
  169. return AVERROR(EINVAL);
  170. }
  171. avctx->channels = s->channels = get_bits(&s->gb, 16);
  172. if (s->channels > 1 && s->channels < 9)
  173. avctx->channel_layout = tta_channel_layouts[s->channels-2];
  174. avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
  175. s->bps = (avctx->bits_per_coded_sample + 7) / 8;
  176. avctx->sample_rate = get_bits_long(&s->gb, 32);
  177. s->data_length = get_bits_long(&s->gb, 32);
  178. skip_bits_long(&s->gb, 32); // CRC32 of header
  179. if (s->channels == 0) {
  180. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  181. return AVERROR_INVALIDDATA;
  182. } else if (avctx->sample_rate == 0) {
  183. av_log(s->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
  184. return AVERROR_INVALIDDATA;
  185. }
  186. switch(s->bps) {
  187. case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
  188. case 2:
  189. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  190. avctx->bits_per_raw_sample = 16;
  191. break;
  192. case 3:
  193. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  194. avctx->bits_per_raw_sample = 24;
  195. break;
  196. //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
  197. default:
  198. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
  199. return AVERROR_INVALIDDATA;
  200. }
  201. // prevent overflow
  202. if (avctx->sample_rate > 0x7FFFFFu) {
  203. av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
  204. return AVERROR(EINVAL);
  205. }
  206. s->frame_length = 256 * avctx->sample_rate / 245;
  207. s->last_frame_length = s->data_length % s->frame_length;
  208. s->total_frames = s->data_length / s->frame_length +
  209. (s->last_frame_length ? 1 : 0);
  210. av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
  211. s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
  212. avctx->block_align);
  213. av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  214. s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
  215. if (s->total_frames < 0)
  216. return AVERROR_INVALIDDATA;
  217. // FIXME: seek table
  218. if (avctx->extradata_size <= 26 || s->total_frames > INT_MAX / 4 ||
  219. avctx->extradata_size - 26 < s->total_frames * 4)
  220. av_log(avctx, AV_LOG_WARNING, "Seek table missing or too small\n");
  221. else if (avctx->err_recognition & AV_EF_CRCCHECK) {
  222. if (avctx->extradata_size < 26 + s->total_frames * 4 || tta_check_crc(s, avctx->extradata + 22, s->total_frames * 4))
  223. return AVERROR_INVALIDDATA;
  224. }
  225. skip_bits_long(&s->gb, 32 * s->total_frames);
  226. skip_bits_long(&s->gb, 32); // CRC32 of seektable
  227. if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
  228. av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
  229. return -1;
  230. }
  231. if (s->bps < 3) {
  232. s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
  233. if (!s->decode_buffer)
  234. return AVERROR(ENOMEM);
  235. } else
  236. s->decode_buffer = NULL;
  237. s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
  238. if (!s->ch_ctx) {
  239. av_freep(&s->decode_buffer);
  240. return AVERROR(ENOMEM);
  241. }
  242. } else {
  243. av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
  244. return -1;
  245. }
  246. avcodec_get_frame_defaults(&s->frame);
  247. avctx->coded_frame = &s->frame;
  248. return 0;
  249. }
  250. static int tta_decode_frame(AVCodecContext *avctx, void *data,
  251. int *got_frame_ptr, AVPacket *avpkt)
  252. {
  253. const uint8_t *buf = avpkt->data;
  254. int buf_size = avpkt->size;
  255. TTAContext *s = avctx->priv_data;
  256. int i, ret;
  257. int cur_chan = 0, framelen = s->frame_length;
  258. int32_t *p;
  259. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  260. if (buf_size < 4 || tta_check_crc(s, buf, buf_size - 4))
  261. return AVERROR_INVALIDDATA;
  262. }
  263. init_get_bits(&s->gb, buf, buf_size*8);
  264. // FIXME: seeking
  265. s->total_frames--;
  266. if (!s->total_frames && s->last_frame_length)
  267. framelen = s->last_frame_length;
  268. /* get output buffer */
  269. s->frame.nb_samples = framelen;
  270. if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
  271. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  272. return ret;
  273. }
  274. // decode directly to output buffer for 24-bit sample format
  275. if (s->bps == 3)
  276. s->decode_buffer = (int32_t *)s->frame.data[0];
  277. // init per channel states
  278. for (i = 0; i < s->channels; i++) {
  279. s->ch_ctx[i].predictor = 0;
  280. ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1]);
  281. rice_init(&s->ch_ctx[i].rice, 10, 10);
  282. }
  283. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  284. int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
  285. TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
  286. TTARice *rice = &s->ch_ctx[cur_chan].rice;
  287. uint32_t unary, depth, k;
  288. int32_t value;
  289. unary = tta_get_unary(&s->gb);
  290. if (unary == 0) {
  291. depth = 0;
  292. k = rice->k0;
  293. } else {
  294. depth = 1;
  295. k = rice->k1;
  296. unary--;
  297. }
  298. if (get_bits_left(&s->gb) < k) {
  299. ret = AVERROR_INVALIDDATA;
  300. goto error;
  301. }
  302. if (k) {
  303. if (k > MIN_CACHE_BITS) {
  304. ret = AVERROR_INVALIDDATA;
  305. goto error;
  306. }
  307. value = (unary << k) + get_bits(&s->gb, k);
  308. } else
  309. value = unary;
  310. // FIXME: copy paste from original
  311. switch (depth) {
  312. case 1:
  313. rice->sum1 += value - (rice->sum1 >> 4);
  314. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  315. rice->k1--;
  316. else if(rice->sum1 > shift_16[rice->k1 + 1])
  317. rice->k1++;
  318. value += shift_1[rice->k0];
  319. default:
  320. rice->sum0 += value - (rice->sum0 >> 4);
  321. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  322. rice->k0--;
  323. else if(rice->sum0 > shift_16[rice->k0 + 1])
  324. rice->k0++;
  325. }
  326. // extract coded value
  327. *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
  328. // run hybrid filter
  329. ttafilter_process(filter, p);
  330. // fixed order prediction
  331. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  332. switch (s->bps) {
  333. case 1: *p += PRED(*predictor, 4); break;
  334. case 2:
  335. case 3: *p += PRED(*predictor, 5); break;
  336. case 4: *p += *predictor; break;
  337. }
  338. *predictor = *p;
  339. // flip channels
  340. if (cur_chan < (s->channels-1))
  341. cur_chan++;
  342. else {
  343. // decorrelate in case of multiple channels
  344. if (s->channels > 1) {
  345. int32_t *r = p - 1;
  346. for (*p += *r / 2; r > p - s->channels; r--)
  347. *r = *(r + 1) - *r;
  348. }
  349. cur_chan = 0;
  350. }
  351. }
  352. if (get_bits_left(&s->gb) < 32) {
  353. ret = AVERROR_INVALIDDATA;
  354. goto error;
  355. }
  356. skip_bits_long(&s->gb, 32); // frame crc
  357. // convert to output buffer
  358. switch (s->bps) {
  359. case 1: {
  360. uint8_t *samples = (uint8_t *)s->frame.data[0];
  361. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  362. *samples++ = *p + 0x80;
  363. break;
  364. }
  365. case 2: {
  366. uint16_t *samples = (int16_t *)s->frame.data[0];
  367. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  368. *samples++ = *p;
  369. break;
  370. }
  371. case 3: {
  372. // shift samples for 24-bit sample format
  373. int32_t *samples = (int32_t *)s->frame.data[0];
  374. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  375. *samples++ <<= 8;
  376. // reset decode buffer
  377. s->decode_buffer = NULL;
  378. break;
  379. }
  380. }
  381. *got_frame_ptr = 1;
  382. *(AVFrame *)data = s->frame;
  383. return buf_size;
  384. error:
  385. // reset decode buffer
  386. if (s->bps == 3)
  387. s->decode_buffer = NULL;
  388. return ret;
  389. }
  390. static av_cold int tta_decode_close(AVCodecContext *avctx) {
  391. TTAContext *s = avctx->priv_data;
  392. if (s->bps < 3)
  393. av_free(s->decode_buffer);
  394. s->decode_buffer = NULL;
  395. av_freep(&s->ch_ctx);
  396. return 0;
  397. }
  398. AVCodec ff_tta_decoder = {
  399. .name = "tta",
  400. .type = AVMEDIA_TYPE_AUDIO,
  401. .id = AV_CODEC_ID_TTA,
  402. .priv_data_size = sizeof(TTAContext),
  403. .init = tta_decode_init,
  404. .close = tta_decode_close,
  405. .decode = tta_decode_frame,
  406. .capabilities = CODEC_CAP_DR1,
  407. .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
  408. };