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.

470 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. * (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 av_cold int tta_decode_init(AVCodecContext * avctx)
  183. {
  184. TTAContext *s = avctx->priv_data;
  185. int i;
  186. s->avctx = avctx;
  187. // 30bytes includes a seektable with one frame
  188. if (avctx->extradata_size < 30)
  189. return -1;
  190. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size);
  191. if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
  192. {
  193. /* signature */
  194. skip_bits(&s->gb, 32);
  195. // if (get_bits_long(&s->gb, 32) != bswap_32(AV_RL32("TTA1"))) {
  196. // av_log(s->avctx, AV_LOG_ERROR, "Missing magic\n");
  197. // return -1;
  198. // }
  199. s->flags = get_bits(&s->gb, 16);
  200. if (s->flags != 1 && s->flags != 3)
  201. {
  202. av_log(s->avctx, AV_LOG_ERROR, "Invalid flags\n");
  203. return -1;
  204. }
  205. s->is_float = (s->flags == FORMAT_FLOAT);
  206. avctx->channels = s->channels = get_bits(&s->gb, 16);
  207. avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
  208. s->bps = (avctx->bits_per_coded_sample + 7) / 8;
  209. avctx->sample_rate = get_bits_long(&s->gb, 32);
  210. if(avctx->sample_rate > 1000000){ //prevent FRAME_TIME * avctx->sample_rate from overflowing and sanity check
  211. av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
  212. return -1;
  213. }
  214. s->data_length = get_bits_long(&s->gb, 32);
  215. skip_bits(&s->gb, 32); // CRC32 of header
  216. if (s->is_float)
  217. {
  218. avctx->sample_fmt = SAMPLE_FMT_FLT;
  219. av_log(s->avctx, AV_LOG_ERROR, "Unsupported sample format. Please contact the developers.\n");
  220. return -1;
  221. }
  222. else switch(s->bps) {
  223. // case 1: avctx->sample_fmt = SAMPLE_FMT_U8; break;
  224. case 2: avctx->sample_fmt = SAMPLE_FMT_S16; break;
  225. // case 3: avctx->sample_fmt = SAMPLE_FMT_S24; break;
  226. case 4: avctx->sample_fmt = SAMPLE_FMT_S32; break;
  227. default:
  228. av_log(s->avctx, AV_LOG_ERROR, "Invalid/unsupported sample format. Please contact the developers.\n");
  229. return -1;
  230. }
  231. // FIXME: horribly broken, but directly from reference source
  232. #define FRAME_TIME 1.04489795918367346939
  233. s->frame_length = (int)(FRAME_TIME * avctx->sample_rate);
  234. s->last_frame_length = s->data_length % s->frame_length;
  235. s->total_frames = s->data_length / s->frame_length +
  236. (s->last_frame_length ? 1 : 0);
  237. av_log(s->avctx, AV_LOG_DEBUG, "flags: %x chans: %d bps: %d rate: %d block: %d\n",
  238. s->flags, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
  239. avctx->block_align);
  240. av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  241. s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
  242. // FIXME: seek table
  243. for (i = 0; i < s->total_frames; i++)
  244. skip_bits(&s->gb, 32);
  245. skip_bits(&s->gb, 32); // CRC32 of seektable
  246. if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
  247. av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
  248. return -1;
  249. }
  250. s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
  251. s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
  252. if (!s->ch_ctx)
  253. return AVERROR(ENOMEM);
  254. } else {
  255. av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. static int tta_decode_frame(AVCodecContext *avctx,
  261. void *data, int *data_size,
  262. AVPacket *avpkt)
  263. {
  264. const uint8_t *buf = avpkt->data;
  265. int buf_size = avpkt->size;
  266. TTAContext *s = avctx->priv_data;
  267. int i;
  268. init_get_bits(&s->gb, buf, buf_size*8);
  269. {
  270. int cur_chan = 0, framelen = s->frame_length;
  271. int32_t *p;
  272. if (*data_size < (framelen * s->channels * 2)) {
  273. av_log(avctx, AV_LOG_ERROR, "Output buffer size is too small.\n");
  274. return -1;
  275. }
  276. // FIXME: seeking
  277. s->total_frames--;
  278. if (!s->total_frames && s->last_frame_length)
  279. framelen = s->last_frame_length;
  280. // init per channel states
  281. for (i = 0; i < s->channels; i++) {
  282. s->ch_ctx[i].predictor = 0;
  283. ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
  284. rice_init(&s->ch_ctx[i].rice, 10, 10);
  285. }
  286. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  287. int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
  288. TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
  289. TTARice *rice = &s->ch_ctx[cur_chan].rice;
  290. uint32_t unary, depth, k;
  291. int32_t value;
  292. unary = tta_get_unary(&s->gb);
  293. if (unary == 0) {
  294. depth = 0;
  295. k = rice->k0;
  296. } else {
  297. depth = 1;
  298. k = rice->k1;
  299. unary--;
  300. }
  301. if (get_bits_left(&s->gb) < k)
  302. return -1;
  303. if (k) {
  304. if (k > MIN_CACHE_BITS)
  305. return -1;
  306. value = (unary << k) + get_bits(&s->gb, k);
  307. } else
  308. value = unary;
  309. // FIXME: copy paste from original
  310. switch (depth) {
  311. case 1:
  312. rice->sum1 += value - (rice->sum1 >> 4);
  313. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  314. rice->k1--;
  315. else if(rice->sum1 > shift_16[rice->k1 + 1])
  316. rice->k1++;
  317. value += shift_1[rice->k0];
  318. default:
  319. rice->sum0 += value - (rice->sum0 >> 4);
  320. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  321. rice->k0--;
  322. else if(rice->sum0 > shift_16[rice->k0 + 1])
  323. rice->k0++;
  324. }
  325. // extract coded value
  326. #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
  327. *p = UNFOLD(value);
  328. // run hybrid filter
  329. ttafilter_process(filter, p, 0);
  330. // fixed order prediction
  331. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  332. switch (s->bps) {
  333. case 1: *p += PRED(*predictor, 4); break;
  334. case 2:
  335. case 3: *p += PRED(*predictor, 5); break;
  336. case 4: *p += *predictor; break;
  337. }
  338. *predictor = *p;
  339. #if 0
  340. // extract 32bit float from last two int samples
  341. if (s->is_float && ((p - data) & 1)) {
  342. uint32_t neg = *p & 0x80000000;
  343. uint32_t hi = *(p - 1);
  344. uint32_t lo = abs(*p) - 1;
  345. hi += (hi || lo) ? 0x3f80 : 0;
  346. // SWAP16: swap all the 16 bits
  347. *(p - 1) = (hi << 16) | SWAP16(lo) | neg;
  348. }
  349. #endif
  350. /*if ((get_bits_count(&s->gb)+7)/8 > buf_size)
  351. {
  352. av_log(NULL, AV_LOG_INFO, "overread!!\n");
  353. break;
  354. }*/
  355. // flip channels
  356. if (cur_chan < (s->channels-1))
  357. cur_chan++;
  358. else {
  359. // decorrelate in case of stereo integer
  360. if (!s->is_float && (s->channels > 1)) {
  361. int32_t *r = p - 1;
  362. for (*p += *r / 2; r > p - s->channels; r--)
  363. *r = *(r + 1) - *r;
  364. }
  365. cur_chan = 0;
  366. }
  367. }
  368. if (get_bits_left(&s->gb) < 32)
  369. return -1;
  370. skip_bits(&s->gb, 32); // frame crc
  371. // convert to output buffer
  372. switch(s->bps) {
  373. case 2: {
  374. uint16_t *samples = data;
  375. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  376. // *samples++ = (unsigned char)*p;
  377. // *samples++ = (unsigned char)(*p >> 8);
  378. *samples++ = *p;
  379. }
  380. *data_size = (uint8_t *)samples - (uint8_t *)data;
  381. break;
  382. }
  383. default:
  384. av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n");
  385. }
  386. }
  387. // return get_bits_count(&s->gb)+7)/8;
  388. return buf_size;
  389. }
  390. static av_cold int tta_decode_close(AVCodecContext *avctx) {
  391. TTAContext *s = avctx->priv_data;
  392. if (s->decode_buffer)
  393. av_free(s->decode_buffer);
  394. av_freep(&s->ch_ctx);
  395. return 0;
  396. }
  397. AVCodec tta_decoder = {
  398. "tta",
  399. AVMEDIA_TYPE_AUDIO,
  400. CODEC_ID_TTA,
  401. sizeof(TTAContext),
  402. tta_decode_init,
  403. NULL,
  404. tta_decode_close,
  405. tta_decode_frame,
  406. .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
  407. };