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.

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