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.

498 lines
15KB

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