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, data_length;
  57. int frame_length, last_frame_length, total_frames;
  58. int32_t *decode_buffer;
  59. TTAChannel *ch_ctx;
  60. } TTAContext;
  61. static const uint32_t shift_1[] = {
  62. 0x00000001, 0x00000002, 0x00000004, 0x00000008,
  63. 0x00000010, 0x00000020, 0x00000040, 0x00000080,
  64. 0x00000100, 0x00000200, 0x00000400, 0x00000800,
  65. 0x00001000, 0x00002000, 0x00004000, 0x00008000,
  66. 0x00010000, 0x00020000, 0x00040000, 0x00080000,
  67. 0x00100000, 0x00200000, 0x00400000, 0x00800000,
  68. 0x01000000, 0x02000000, 0x04000000, 0x08000000,
  69. 0x10000000, 0x20000000, 0x40000000, 0x80000000,
  70. 0x80000000, 0x80000000, 0x80000000, 0x80000000,
  71. 0x80000000, 0x80000000, 0x80000000, 0x80000000
  72. };
  73. static const uint32_t * const shift_16 = shift_1 + 4;
  74. static const int32_t ttafilter_configs[4] = {
  75. 10,
  76. 9,
  77. 10,
  78. 12
  79. };
  80. static void ttafilter_init(TTAFilter *c, int32_t shift) {
  81. memset(c, 0, sizeof(TTAFilter));
  82. c->shift = shift;
  83. c->round = shift_1[shift-1];
  84. // c->round = 1 << (shift - 1);
  85. }
  86. static inline void ttafilter_process(TTAFilter *c, int32_t *in)
  87. {
  88. register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
  89. if (c->error < 0) {
  90. qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
  91. qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
  92. } else if (c->error > 0) {
  93. qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
  94. qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
  95. }
  96. sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
  97. dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
  98. dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
  99. dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
  100. dx[4] = ((dl[4] >> 30) | 1);
  101. dx[5] = ((dl[5] >> 30) | 2) & ~1;
  102. dx[6] = ((dl[6] >> 30) | 2) & ~1;
  103. dx[7] = ((dl[7] >> 30) | 4) & ~3;
  104. c->error = *in;
  105. *in += (sum >> c->shift);
  106. dl[4] = -dl[5]; dl[5] = -dl[6];
  107. dl[6] = *in - dl[7]; dl[7] = *in;
  108. dl[5] += dl[6]; dl[4] += dl[5];
  109. }
  110. static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
  111. {
  112. c->k0 = k0;
  113. c->k1 = k1;
  114. c->sum0 = shift_16[k0];
  115. c->sum1 = shift_16[k1];
  116. }
  117. static int tta_get_unary(GetBitContext *gb)
  118. {
  119. int ret = 0;
  120. // count ones
  121. while (get_bits_left(gb) > 0 && get_bits1(gb))
  122. ret++;
  123. return ret;
  124. }
  125. static const int64_t tta_channel_layouts[7] = {
  126. AV_CH_LAYOUT_STEREO,
  127. AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
  128. AV_CH_LAYOUT_QUAD,
  129. 0,
  130. AV_CH_LAYOUT_5POINT1_BACK,
  131. AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
  132. AV_CH_LAYOUT_7POINT1_WIDE
  133. };
  134. static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
  135. {
  136. uint32_t crc, CRC;
  137. CRC = AV_RL32(buf + buf_size);
  138. crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
  139. if (CRC != (crc ^ 0xFFFFFFFFU)) {
  140. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  141. return AVERROR_INVALIDDATA;
  142. }
  143. return 0;
  144. }
  145. static av_cold int tta_decode_init(AVCodecContext * avctx)
  146. {
  147. TTAContext *s = avctx->priv_data;
  148. s->avctx = avctx;
  149. // 30bytes includes a seektable with one frame
  150. if (avctx->extradata_size < 30)
  151. return -1;
  152. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
  153. if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
  154. {
  155. if (avctx->err_recognition & AV_EF_CRCCHECK) {
  156. s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
  157. tta_check_crc(s, avctx->extradata, 18);
  158. }
  159. /* signature */
  160. skip_bits_long(&s->gb, 32);
  161. s->format = get_bits(&s->gb, 16);
  162. if (s->format > 2) {
  163. av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n");
  164. return -1;
  165. }
  166. if (s->format == FORMAT_ENCRYPTED) {
  167. av_log_missing_feature(s->avctx, "Encrypted TTA", 0);
  168. return AVERROR(EINVAL);
  169. }
  170. avctx->channels = s->channels = get_bits(&s->gb, 16);
  171. if (s->channels > 1 && s->channels < 9)
  172. avctx->channel_layout = tta_channel_layouts[s->channels-2];
  173. avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
  174. s->bps = (avctx->bits_per_coded_sample + 7) / 8;
  175. avctx->sample_rate = get_bits_long(&s->gb, 32);
  176. s->data_length = get_bits_long(&s->gb, 32);
  177. skip_bits_long(&s->gb, 32); // CRC32 of header
  178. if (s->channels == 0) {
  179. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  180. return AVERROR_INVALIDDATA;
  181. } else if (avctx->sample_rate == 0) {
  182. av_log(s->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
  183. return AVERROR_INVALIDDATA;
  184. }
  185. switch(s->bps) {
  186. case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
  187. case 2:
  188. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  189. avctx->bits_per_raw_sample = 16;
  190. break;
  191. case 3:
  192. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  193. avctx->bits_per_raw_sample = 24;
  194. break;
  195. //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
  196. default:
  197. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
  198. return AVERROR_INVALIDDATA;
  199. }
  200. // prevent overflow
  201. if (avctx->sample_rate > 0x7FFFFF) {
  202. av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
  203. return AVERROR(EINVAL);
  204. }
  205. s->frame_length = 256 * avctx->sample_rate / 245;
  206. s->last_frame_length = s->data_length % s->frame_length;
  207. s->total_frames = s->data_length / s->frame_length +
  208. (s->last_frame_length ? 1 : 0);
  209. av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
  210. s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
  211. avctx->block_align);
  212. av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  213. s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
  214. if (s->total_frames < 0)
  215. return AVERROR_INVALIDDATA;
  216. // FIXME: seek table
  217. if (get_bits_left(&s->gb) < 32 * s->total_frames + 32)
  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 -1;
  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 -1;
  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 stereo integer
  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. uint16_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 = 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("True Audio (TTA)"),
  406. };