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.

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