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.

460 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 ALT_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. GetBitContext gb;
  53. int format, channels, bps, data_length;
  54. int frame_length, last_frame_length, total_frames;
  55. int32_t *decode_buffer;
  56. TTAChannel *ch_ctx;
  57. } TTAContext;
  58. static const uint32_t shift_1[] = {
  59. 0x00000001, 0x00000002, 0x00000004, 0x00000008,
  60. 0x00000010, 0x00000020, 0x00000040, 0x00000080,
  61. 0x00000100, 0x00000200, 0x00000400, 0x00000800,
  62. 0x00001000, 0x00002000, 0x00004000, 0x00008000,
  63. 0x00010000, 0x00020000, 0x00040000, 0x00080000,
  64. 0x00100000, 0x00200000, 0x00400000, 0x00800000,
  65. 0x01000000, 0x02000000, 0x04000000, 0x08000000,
  66. 0x10000000, 0x20000000, 0x40000000, 0x80000000,
  67. 0x80000000, 0x80000000, 0x80000000, 0x80000000,
  68. 0x80000000, 0x80000000, 0x80000000, 0x80000000
  69. };
  70. static const uint32_t * const shift_16 = shift_1 + 4;
  71. static const int32_t ttafilter_configs[4][2] = {
  72. {10, 1},
  73. {9, 1},
  74. {10, 1},
  75. {12, 0}
  76. };
  77. static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) {
  78. memset(c, 0, sizeof(TTAFilter));
  79. c->shift = shift;
  80. c->round = shift_1[shift-1];
  81. // c->round = 1 << (shift - 1);
  82. c->mode = mode;
  83. }
  84. // FIXME: copy paste from original
  85. static inline void memshl(register int32_t *a, register int32_t *b) {
  86. *a++ = *b++;
  87. *a++ = *b++;
  88. *a++ = *b++;
  89. *a++ = *b++;
  90. *a++ = *b++;
  91. *a++ = *b++;
  92. *a++ = *b++;
  93. *a = *b;
  94. }
  95. // FIXME: copy paste from original
  96. // mode=1 encoder, mode=0 decoder
  97. static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) {
  98. register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
  99. if (!c->error) {
  100. sum += *dl++ * *qm, qm++;
  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. dx += 8;
  109. } else if(c->error < 0) {
  110. sum += *dl++ * (*qm -= *dx++), qm++;
  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. } else {
  119. sum += *dl++ * (*qm += *dx++), qm++;
  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. }
  128. *(dx-0) = ((*(dl-1) >> 30) | 1) << 2;
  129. *(dx-1) = ((*(dl-2) >> 30) | 1) << 1;
  130. *(dx-2) = ((*(dl-3) >> 30) | 1) << 1;
  131. *(dx-3) = ((*(dl-4) >> 30) | 1);
  132. // compress
  133. if (mode) {
  134. *dl = *in;
  135. *in -= (sum >> c->shift);
  136. c->error = *in;
  137. } else {
  138. c->error = *in;
  139. *in += (sum >> c->shift);
  140. *dl = *in;
  141. }
  142. if (c->mode) {
  143. *(dl-1) = *dl - *(dl-1);
  144. *(dl-2) = *(dl-1) - *(dl-2);
  145. *(dl-3) = *(dl-2) - *(dl-3);
  146. }
  147. memshl(c->dl, c->dl + 1);
  148. memshl(c->dx, c->dx + 1);
  149. }
  150. static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
  151. {
  152. c->k0 = k0;
  153. c->k1 = k1;
  154. c->sum0 = shift_16[k0];
  155. c->sum1 = shift_16[k1];
  156. }
  157. static int tta_get_unary(GetBitContext *gb)
  158. {
  159. int ret = 0;
  160. // count ones
  161. while (get_bits_left(gb) > 0 && get_bits1(gb))
  162. ret++;
  163. return ret;
  164. }
  165. static const int64_t tta_channel_layouts[7] = {
  166. AV_CH_LAYOUT_STEREO,
  167. AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
  168. AV_CH_LAYOUT_QUAD,
  169. 0,
  170. AV_CH_LAYOUT_5POINT1_BACK,
  171. AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
  172. AV_CH_LAYOUT_7POINT1_WIDE
  173. };
  174. static av_cold int tta_decode_init(AVCodecContext * avctx)
  175. {
  176. TTAContext *s = avctx->priv_data;
  177. int i;
  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(&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(&s->gb, 32); // CRC32 of header
  204. switch(s->bps) {
  205. case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
  206. case 2:
  207. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  208. avctx->bits_per_raw_sample = 16;
  209. break;
  210. case 3:
  211. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  212. avctx->bits_per_raw_sample = 24;
  213. break;
  214. //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
  215. default:
  216. av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
  217. return AVERROR_INVALIDDATA;
  218. }
  219. // prevent overflow
  220. if (avctx->sample_rate > 0x7FFFFF) {
  221. av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
  222. return AVERROR(EINVAL);
  223. }
  224. s->frame_length = 256 * avctx->sample_rate / 245;
  225. s->last_frame_length = s->data_length % s->frame_length;
  226. s->total_frames = s->data_length / s->frame_length +
  227. (s->last_frame_length ? 1 : 0);
  228. av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
  229. s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
  230. avctx->block_align);
  231. av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  232. s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
  233. // FIXME: seek table
  234. for (i = 0; i < s->total_frames; i++)
  235. skip_bits(&s->gb, 32);
  236. skip_bits(&s->gb, 32); // CRC32 of seektable
  237. if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
  238. av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
  239. return -1;
  240. }
  241. s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
  242. if (!s->decode_buffer)
  243. return AVERROR(ENOMEM);
  244. s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
  245. if (!s->ch_ctx) {
  246. av_freep(&s->decode_buffer);
  247. return AVERROR(ENOMEM);
  248. }
  249. } else {
  250. av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
  251. return -1;
  252. }
  253. return 0;
  254. }
  255. static int tta_decode_frame(AVCodecContext *avctx,
  256. void *data, int *data_size,
  257. AVPacket *avpkt)
  258. {
  259. const uint8_t *buf = avpkt->data;
  260. int buf_size = avpkt->size;
  261. TTAContext *s = avctx->priv_data;
  262. int i, out_size;
  263. int cur_chan = 0, framelen = s->frame_length;
  264. int32_t *p;
  265. init_get_bits(&s->gb, buf, buf_size*8);
  266. // FIXME: seeking
  267. s->total_frames--;
  268. if (!s->total_frames && s->last_frame_length)
  269. framelen = s->last_frame_length;
  270. out_size = framelen * s->channels * av_get_bytes_per_sample(avctx->sample_fmt);
  271. if (*data_size < out_size) {
  272. av_log(avctx, AV_LOG_ERROR, "Output buffer size is too small.\n");
  273. return -1;
  274. }
  275. // decode directly to output buffer for 24-bit sample format
  276. if (s->bps == 3)
  277. s->decode_buffer = data;
  278. // init per channel states
  279. for (i = 0; i < s->channels; i++) {
  280. s->ch_ctx[i].predictor = 0;
  281. ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
  282. rice_init(&s->ch_ctx[i].rice, 10, 10);
  283. }
  284. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  285. int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
  286. TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
  287. TTARice *rice = &s->ch_ctx[cur_chan].rice;
  288. uint32_t unary, depth, k;
  289. int32_t value;
  290. unary = tta_get_unary(&s->gb);
  291. if (unary == 0) {
  292. depth = 0;
  293. k = rice->k0;
  294. } else {
  295. depth = 1;
  296. k = rice->k1;
  297. unary--;
  298. }
  299. if (get_bits_left(&s->gb) < k)
  300. return -1;
  301. if (k) {
  302. if (k > MIN_CACHE_BITS)
  303. return -1;
  304. value = (unary << k) + get_bits(&s->gb, k);
  305. } else
  306. value = unary;
  307. // FIXME: copy paste from original
  308. switch (depth) {
  309. case 1:
  310. rice->sum1 += value - (rice->sum1 >> 4);
  311. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  312. rice->k1--;
  313. else if(rice->sum1 > shift_16[rice->k1 + 1])
  314. rice->k1++;
  315. value += shift_1[rice->k0];
  316. default:
  317. rice->sum0 += value - (rice->sum0 >> 4);
  318. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  319. rice->k0--;
  320. else if(rice->sum0 > shift_16[rice->k0 + 1])
  321. rice->k0++;
  322. }
  323. // extract coded value
  324. #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
  325. *p = UNFOLD(value);
  326. // run hybrid filter
  327. ttafilter_process(filter, p, 0);
  328. // fixed order prediction
  329. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  330. switch (s->bps) {
  331. case 1: *p += PRED(*predictor, 4); break;
  332. case 2:
  333. case 3: *p += PRED(*predictor, 5); break;
  334. case 4: *p += *predictor; break;
  335. }
  336. *predictor = *p;
  337. // flip channels
  338. if (cur_chan < (s->channels-1))
  339. cur_chan++;
  340. else {
  341. // decorrelate in case of stereo integer
  342. if (s->channels > 1) {
  343. int32_t *r = p - 1;
  344. for (*p += *r / 2; r > p - s->channels; r--)
  345. *r = *(r + 1) - *r;
  346. }
  347. cur_chan = 0;
  348. }
  349. }
  350. if (get_bits_left(&s->gb) < 32)
  351. return -1;
  352. skip_bits(&s->gb, 32); // frame crc
  353. // convert to output buffer
  354. switch(s->bps) {
  355. case 1: {
  356. uint8_t *samples = data;
  357. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  358. *samples++ = *p + 0x80;
  359. break;
  360. }
  361. case 2: {
  362. uint16_t *samples = data;
  363. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  364. *samples++ = *p;
  365. break;
  366. }
  367. case 3: {
  368. // shift samples for 24-bit sample format
  369. int32_t *samples = data;
  370. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
  371. *samples++ <<= 8;
  372. // reset decode buffer
  373. s->decode_buffer = NULL;
  374. break;
  375. }
  376. default:
  377. av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n");
  378. }
  379. *data_size = out_size;
  380. return buf_size;
  381. }
  382. static av_cold int tta_decode_close(AVCodecContext *avctx) {
  383. TTAContext *s = avctx->priv_data;
  384. av_free(s->decode_buffer);
  385. av_freep(&s->ch_ctx);
  386. return 0;
  387. }
  388. AVCodec ff_tta_decoder = {
  389. .name = "tta",
  390. .type = AVMEDIA_TYPE_AUDIO,
  391. .id = CODEC_ID_TTA,
  392. .priv_data_size = sizeof(TTAContext),
  393. .init = tta_decode_init,
  394. .close = tta_decode_close,
  395. .decode = tta_decode_frame,
  396. .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
  397. };