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