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.

439 lines
13KB

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