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.

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