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.

492 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. #include <limits.h>
  30. #include "avcodec.h"
  31. #include "get_bits.h"
  32. #include "unary.h"
  33. #include "internal.h"
  34. #include "libavutil/crc.h"
  35. #include "libavutil/intreadwrite.h"
  36. #include "libavutil/opt.h"
  37. #define FORMAT_SIMPLE 1
  38. #define FORMAT_ENCRYPTED 2
  39. #define MAX_ORDER 16
  40. typedef struct TTAFilter {
  41. int32_t shift, round, error;
  42. int32_t qm[MAX_ORDER];
  43. int32_t dx[MAX_ORDER];
  44. int32_t dl[MAX_ORDER];
  45. } TTAFilter;
  46. typedef struct TTARice {
  47. uint32_t k0, k1, sum0, sum1;
  48. } TTARice;
  49. typedef struct TTAChannel {
  50. int32_t predictor;
  51. TTAFilter filter;
  52. TTARice rice;
  53. } TTAChannel;
  54. typedef struct TTAContext {
  55. AVClass *class;
  56. AVCodecContext *avctx;
  57. GetBitContext gb;
  58. const AVCRC *crc_table;
  59. int format, channels, bps;
  60. unsigned data_length;
  61. int frame_length, last_frame_length;
  62. int32_t *decode_buffer;
  63. uint8_t crc_pass[8];
  64. uint8_t *pass;
  65. TTAChannel *ch_ctx;
  66. } TTAContext;
  67. static const uint32_t shift_1[] = {
  68. 0x00000001, 0x00000002, 0x00000004, 0x00000008,
  69. 0x00000010, 0x00000020, 0x00000040, 0x00000080,
  70. 0x00000100, 0x00000200, 0x00000400, 0x00000800,
  71. 0x00001000, 0x00002000, 0x00004000, 0x00008000,
  72. 0x00010000, 0x00020000, 0x00040000, 0x00080000,
  73. 0x00100000, 0x00200000, 0x00400000, 0x00800000,
  74. 0x01000000, 0x02000000, 0x04000000, 0x08000000,
  75. 0x10000000, 0x20000000, 0x40000000, 0x80000000,
  76. 0x80000000, 0x80000000, 0x80000000, 0x80000000,
  77. 0x80000000, 0x80000000, 0x80000000, 0x80000000
  78. };
  79. static const uint32_t * const shift_16 = shift_1 + 4;
  80. static const int32_t ttafilter_configs[4] = {
  81. 10,
  82. 9,
  83. 10,
  84. 12
  85. };
  86. static void ttafilter_init(TTAContext *s, TTAFilter *c, int32_t shift) {
  87. memset(c, 0, sizeof(TTAFilter));
  88. if (s->format == FORMAT_ENCRYPTED) {
  89. int i;
  90. for (i = 0; i < 8; i++)
  91. c->qm[i] = sign_extend(s->crc_pass[i], 8);
  92. }
  93. c->shift = shift;
  94. c->round = shift_1[shift-1];
  95. // c->round = 1 << (shift - 1);
  96. }
  97. static inline void ttafilter_process(TTAFilter *c, int32_t *in)
  98. {
  99. register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
  100. if (c->error < 0) {
  101. qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
  102. qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
  103. } else if (c->error > 0) {
  104. qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
  105. qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
  106. }
  107. sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
  108. dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
  109. dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
  110. dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
  111. dx[4] = ((dl[4] >> 30) | 1);
  112. dx[5] = ((dl[5] >> 30) | 2) & ~1;
  113. dx[6] = ((dl[6] >> 30) | 2) & ~1;
  114. dx[7] = ((dl[7] >> 30) | 4) & ~3;
  115. c->error = *in;
  116. *in += (sum >> c->shift);
  117. dl[4] = -dl[5]; dl[5] = -dl[6];
  118. dl[6] = *in - dl[7]; dl[7] = *in;
  119. dl[5] += dl[6]; dl[4] += dl[5];
  120. }
  121. static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
  122. {
  123. c->k0 = k0;
  124. c->k1 = k1;
  125. c->sum0 = shift_16[k0];
  126. c->sum1 = shift_16[k1];
  127. }
  128. static const int64_t tta_channel_layouts[7] = {
  129. AV_CH_LAYOUT_STEREO,
  130. AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
  131. AV_CH_LAYOUT_QUAD,
  132. 0,
  133. AV_CH_LAYOUT_5POINT1_BACK,
  134. AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
  135. AV_CH_LAYOUT_7POINT1_WIDE
  136. };
  137. static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
  138. {
  139. uint32_t crc, CRC;
  140. CRC = AV_RL32(buf + buf_size);
  141. crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
  142. if (CRC != (crc ^ 0xFFFFFFFFU)) {
  143. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  144. return AVERROR_INVALIDDATA;
  145. }
  146. return 0;
  147. }
  148. static uint64_t tta_check_crc64(uint8_t *pass)
  149. {
  150. uint64_t crc = UINT64_MAX, poly = 0x42F0E1EBA9EA3693U;
  151. uint8_t *end = pass + strlen(pass);
  152. int i;
  153. while (pass < end) {
  154. crc ^= (uint64_t)*pass++ << 56;
  155. for (i = 0; i < 8; i++)
  156. crc = (crc << 1) ^ (poly & (((int64_t) crc) >> 63));
  157. }
  158. return crc ^ UINT64_MAX;
  159. }
  160. static av_cold int tta_decode_init(AVCodecContext * avctx)
  161. {
  162. TTAContext *s = avctx->priv_data;
  163. int total_frames;
  164. s->avctx = avctx;
  165. // 30bytes includes TTA1 header
  166. if (avctx->extradata_size < 22)
  167. return AVERROR_INVALIDDATA;
  168. s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
  169. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
  170. if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
  171. {
  172. /* signature */
  173. skip_bits_long(&s->gb, 32);
  174. s->format = get_bits(&s->gb, 16);
  175. if (s->format > 2) {
  176. av_log(avctx, AV_LOG_ERROR, "Invalid format\n");
  177. return AVERROR_INVALIDDATA;
  178. }
  179. if (s->format == FORMAT_ENCRYPTED) {
  180. if (!s->pass) {
  181. av_log(avctx, AV_LOG_ERROR, "Missing password for encrypted stream. Please use the -password option\n");
  182. return AVERROR(EINVAL);
  183. }
  184. AV_WL64(s->crc_pass, tta_check_crc64(s->pass));
  185. }
  186. avctx->channels = s->channels = get_bits(&s->gb, 16);
  187. if (s->channels > 1 && s->channels < 9)
  188. avctx->channel_layout = tta_channel_layouts[s->channels-2];
  189. avctx->bits_per_raw_sample = get_bits(&s->gb, 16);
  190. s->bps = (avctx->bits_per_raw_sample + 7) / 8;
  191. avctx->sample_rate = get_bits_long(&s->gb, 32);
  192. s->data_length = get_bits_long(&s->gb, 32);
  193. skip_bits_long(&s->gb, 32); // CRC32 of header
  194. if (s->channels == 0) {
  195. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  196. return AVERROR_INVALIDDATA;
  197. } else if (avctx->sample_rate == 0) {
  198. av_log(avctx, AV_LOG_ERROR, "Invalid samplerate\n");
  199. return AVERROR_INVALIDDATA;
  200. }
  201. switch(s->bps) {
  202. case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
  203. case 2:
  204. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  205. break;
  206. case 3:
  207. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  208. break;
  209. //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
  210. default:
  211. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
  212. return AVERROR_INVALIDDATA;
  213. }
  214. // prevent overflow
  215. if (avctx->sample_rate > 0x7FFFFFu) {
  216. av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
  217. return AVERROR(EINVAL);
  218. }
  219. s->frame_length = 256 * avctx->sample_rate / 245;
  220. s->last_frame_length = s->data_length % s->frame_length;
  221. total_frames = s->data_length / s->frame_length +
  222. (s->last_frame_length ? 1 : 0);
  223. av_log(avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
  224. s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
  225. avctx->block_align);
  226. av_log(avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  227. s->data_length, s->frame_length, s->last_frame_length, total_frames);
  228. if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
  229. av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
  230. return AVERROR_INVALIDDATA;
  231. }
  232. if (s->bps < 3) {
  233. s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
  234. if (!s->decode_buffer)
  235. return AVERROR(ENOMEM);
  236. } else
  237. s->decode_buffer = NULL;
  238. s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
  239. if (!s->ch_ctx) {
  240. av_freep(&s->decode_buffer);
  241. return AVERROR(ENOMEM);
  242. }
  243. } else {
  244. av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
  245. return AVERROR_INVALIDDATA;
  246. }
  247. return 0;
  248. }
  249. static int tta_decode_frame(AVCodecContext *avctx, void *data,
  250. int *got_frame_ptr, AVPacket *avpkt)
  251. {
  252. AVFrame *frame = data;
  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. if ((ret = init_get_bits8(&s->gb, avpkt->data, avpkt->size)) < 0)
  264. return ret;
  265. /* get output buffer */
  266. frame->nb_samples = framelen;
  267. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  268. return ret;
  269. // decode directly to output buffer for 24-bit sample format
  270. if (s->bps == 3)
  271. s->decode_buffer = (int32_t *)frame->data[0];
  272. // init per channel states
  273. for (i = 0; i < s->channels; i++) {
  274. s->ch_ctx[i].predictor = 0;
  275. ttafilter_init(s, &s->ch_ctx[i].filter, ttafilter_configs[s->bps-1]);
  276. rice_init(&s->ch_ctx[i].rice, 10, 10);
  277. }
  278. i = 0;
  279. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  280. int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
  281. TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
  282. TTARice *rice = &s->ch_ctx[cur_chan].rice;
  283. uint32_t unary, depth, k;
  284. int32_t value;
  285. unary = get_unary(&s->gb, 0, get_bits_left(&s->gb));
  286. if (unary == 0) {
  287. depth = 0;
  288. k = rice->k0;
  289. } else {
  290. depth = 1;
  291. k = rice->k1;
  292. unary--;
  293. }
  294. if (get_bits_left(&s->gb) < k) {
  295. ret = AVERROR_INVALIDDATA;
  296. goto error;
  297. }
  298. if (k) {
  299. if (k > MIN_CACHE_BITS) {
  300. ret = AVERROR_INVALIDDATA;
  301. goto error;
  302. }
  303. value = (unary << k) + get_bits(&s->gb, k);
  304. } else
  305. value = unary;
  306. // FIXME: copy paste from original
  307. switch (depth) {
  308. case 1:
  309. rice->sum1 += value - (rice->sum1 >> 4);
  310. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  311. rice->k1--;
  312. else if(rice->sum1 > shift_16[rice->k1 + 1])
  313. rice->k1++;
  314. value += shift_1[rice->k0];
  315. default:
  316. rice->sum0 += value - (rice->sum0 >> 4);
  317. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  318. rice->k0--;
  319. else if(rice->sum0 > shift_16[rice->k0 + 1])
  320. rice->k0++;
  321. }
  322. // extract coded value
  323. *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
  324. // run hybrid filter
  325. ttafilter_process(filter, p);
  326. // fixed order prediction
  327. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  328. switch (s->bps) {
  329. case 1: *p += PRED(*predictor, 4); break;
  330. case 2:
  331. case 3: *p += PRED(*predictor, 5); break;
  332. case 4: *p += *predictor; break;
  333. }
  334. *predictor = *p;
  335. // flip channels
  336. if (cur_chan < (s->channels-1))
  337. cur_chan++;
  338. else {
  339. // decorrelate in case of multiple channels
  340. if (s->channels > 1) {
  341. int32_t *r = p - 1;
  342. for (*p += *r / 2; r > p - s->channels; r--)
  343. *r = *(r + 1) - *r;
  344. }
  345. cur_chan = 0;
  346. i++;
  347. // check for last frame
  348. if (i == s->last_frame_length && get_bits_left(&s->gb) / 8 == 4) {
  349. frame->nb_samples = framelen = s->last_frame_length;
  350. break;
  351. }
  352. }
  353. }
  354. align_get_bits(&s->gb);
  355. if (get_bits_left(&s->gb) < 32) {
  356. ret = AVERROR_INVALIDDATA;
  357. goto error;
  358. }
  359. skip_bits_long(&s->gb, 32); // frame crc
  360. // convert to output buffer
  361. switch (s->bps) {
  362. case 1: {
  363. uint8_t *samples = (uint8_t *)frame->data[0];
  364. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  365. *samples++ = *p + 0x80;
  366. break;
  367. }
  368. case 2: {
  369. int16_t *samples = (int16_t *)frame->data[0];
  370. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  371. *samples++ = *p;
  372. break;
  373. }
  374. case 3: {
  375. // shift samples for 24-bit sample format
  376. int32_t *samples = (int32_t *)frame->data[0];
  377. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  378. *samples++ <<= 8;
  379. // reset decode buffer
  380. s->decode_buffer = NULL;
  381. break;
  382. }
  383. }
  384. *got_frame_ptr = 1;
  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. #define OFFSET(x) offsetof(TTAContext, x)
  401. #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
  402. static const AVOption options[] = {
  403. { "password", "Set decoding password", OFFSET(pass), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
  404. { NULL },
  405. };
  406. static const AVClass tta_decoder_class = {
  407. .class_name = "TTA Decoder",
  408. .item_name = av_default_item_name,
  409. .option = options,
  410. .version = LIBAVUTIL_VERSION_INT,
  411. };
  412. AVCodec ff_tta_decoder = {
  413. .name = "tta",
  414. .type = AVMEDIA_TYPE_AUDIO,
  415. .id = AV_CODEC_ID_TTA,
  416. .priv_data_size = sizeof(TTAContext),
  417. .init = tta_decode_init,
  418. .close = tta_decode_close,
  419. .decode = tta_decode_frame,
  420. .capabilities = CODEC_CAP_DR1,
  421. .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
  422. .priv_class = &tta_decoder_class,
  423. };