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.

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