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.

477 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 AVERROR_INVALIDDATA;
  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 AVERROR_INVALIDDATA;
  166. }
  167. if (s->format == FORMAT_ENCRYPTED) {
  168. av_log_missing_feature(s->avctx, "Encrypted TTA", 0);
  169. return AVERROR_PATCHWELCOME;
  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_raw_sample = get_bits(&s->gb, 16);
  175. s->bps = (avctx->bits_per_raw_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. break;
  191. case 3:
  192. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  193. break;
  194. //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
  195. default:
  196. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
  197. return AVERROR_INVALIDDATA;
  198. }
  199. // prevent overflow
  200. if (avctx->sample_rate > 0x7FFFFFu) {
  201. av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
  202. return AVERROR(EINVAL);
  203. }
  204. s->frame_length = 256 * avctx->sample_rate / 245;
  205. s->last_frame_length = s->data_length % s->frame_length;
  206. s->total_frames = s->data_length / s->frame_length +
  207. (s->last_frame_length ? 1 : 0);
  208. av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
  209. s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
  210. avctx->block_align);
  211. av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  212. s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
  213. if (s->total_frames < 0)
  214. return AVERROR_INVALIDDATA;
  215. // FIXME: seek table
  216. if (avctx->extradata_size <= 26 || s->total_frames > INT_MAX / 4 ||
  217. avctx->extradata_size - 26 < s->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 (avctx->extradata_size < 26 + s->total_frames * 4 || tta_check_crc(s, avctx->extradata + 22, s->total_frames * 4))
  221. return AVERROR_INVALIDDATA;
  222. }
  223. skip_bits_long(&s->gb, 32 * s->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. // FIXME: seeking
  263. s->total_frames--;
  264. if (!s->total_frames && s->last_frame_length)
  265. framelen = s->last_frame_length;
  266. /* get output buffer */
  267. s->frame.nb_samples = framelen;
  268. if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
  269. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  270. return ret;
  271. }
  272. // decode directly to output buffer for 24-bit sample format
  273. if (s->bps == 3)
  274. s->decode_buffer = (int32_t *)s->frame.data[0];
  275. // init per channel states
  276. for (i = 0; i < s->channels; i++) {
  277. s->ch_ctx[i].predictor = 0;
  278. ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1]);
  279. rice_init(&s->ch_ctx[i].rice, 10, 10);
  280. }
  281. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  282. int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
  283. TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
  284. TTARice *rice = &s->ch_ctx[cur_chan].rice;
  285. uint32_t unary, depth, k;
  286. int32_t value;
  287. unary = tta_get_unary(&s->gb);
  288. if (unary == 0) {
  289. depth = 0;
  290. k = rice->k0;
  291. } else {
  292. depth = 1;
  293. k = rice->k1;
  294. unary--;
  295. }
  296. if (get_bits_left(&s->gb) < k) {
  297. ret = AVERROR_INVALIDDATA;
  298. goto error;
  299. }
  300. if (k) {
  301. if (k > MIN_CACHE_BITS) {
  302. ret = AVERROR_INVALIDDATA;
  303. goto error;
  304. }
  305. value = (unary << k) + get_bits(&s->gb, k);
  306. } else
  307. value = unary;
  308. // FIXME: copy paste from original
  309. switch (depth) {
  310. case 1:
  311. rice->sum1 += value - (rice->sum1 >> 4);
  312. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  313. rice->k1--;
  314. else if(rice->sum1 > shift_16[rice->k1 + 1])
  315. rice->k1++;
  316. value += shift_1[rice->k0];
  317. default:
  318. rice->sum0 += value - (rice->sum0 >> 4);
  319. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  320. rice->k0--;
  321. else if(rice->sum0 > shift_16[rice->k0 + 1])
  322. rice->k0++;
  323. }
  324. // extract coded value
  325. *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
  326. // run hybrid filter
  327. ttafilter_process(filter, p);
  328. // fixed order prediction
  329. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  330. switch (s->bps) {
  331. case 1: *p += PRED(*predictor, 4); break;
  332. case 2:
  333. case 3: *p += PRED(*predictor, 5); break;
  334. case 4: *p += *predictor; break;
  335. }
  336. *predictor = *p;
  337. // flip channels
  338. if (cur_chan < (s->channels-1))
  339. cur_chan++;
  340. else {
  341. // decorrelate in case of multiple channels
  342. if (s->channels > 1) {
  343. int32_t *r = p - 1;
  344. for (*p += *r / 2; r > p - s->channels; r--)
  345. *r = *(r + 1) - *r;
  346. }
  347. cur_chan = 0;
  348. }
  349. }
  350. if (get_bits_left(&s->gb) < 32) {
  351. ret = AVERROR_INVALIDDATA;
  352. goto error;
  353. }
  354. skip_bits_long(&s->gb, 32); // frame crc
  355. // convert to output buffer
  356. switch (s->bps) {
  357. case 1: {
  358. uint8_t *samples = (uint8_t *)s->frame.data[0];
  359. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  360. *samples++ = *p + 0x80;
  361. break;
  362. }
  363. case 2: {
  364. int16_t *samples = (int16_t *)s->frame.data[0];
  365. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  366. *samples++ = *p;
  367. break;
  368. }
  369. case 3: {
  370. // shift samples for 24-bit sample format
  371. int32_t *samples = (int32_t *)s->frame.data[0];
  372. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  373. *samples++ <<= 8;
  374. // reset decode buffer
  375. s->decode_buffer = NULL;
  376. break;
  377. }
  378. }
  379. *got_frame_ptr = 1;
  380. *(AVFrame *)data = s->frame;
  381. return buf_size;
  382. error:
  383. // reset decode buffer
  384. if (s->bps == 3)
  385. s->decode_buffer = NULL;
  386. return ret;
  387. }
  388. static av_cold int tta_decode_close(AVCodecContext *avctx) {
  389. TTAContext *s = avctx->priv_data;
  390. if (s->bps < 3)
  391. av_free(s->decode_buffer);
  392. s->decode_buffer = NULL;
  393. av_freep(&s->ch_ctx);
  394. return 0;
  395. }
  396. AVCodec ff_tta_decoder = {
  397. .name = "tta",
  398. .type = AVMEDIA_TYPE_AUDIO,
  399. .id = AV_CODEC_ID_TTA,
  400. .priv_data_size = sizeof(TTAContext),
  401. .init = tta_decode_init,
  402. .close = tta_decode_close,
  403. .decode = tta_decode_frame,
  404. .capabilities = CODEC_CAP_DR1,
  405. .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
  406. };