tta.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * TTA (The Lossless True Audio) decoder
  3. * Copyright (c) 2006 Alex Beregszaszi
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file tta.c
  21. * TTA (The Lossless True Audio) decoder
  22. * (www.true-audio.com or tta.corecodec.org)
  23. * @author Alex Beregszaszi
  24. *
  25. */
  26. #define ALT_BITSTREAM_READER_LE
  27. //#define DEBUG
  28. #include <limits.h>
  29. #include "avcodec.h"
  30. #include "bitstream.h"
  31. #define FORMAT_INT 1
  32. #define FORMAT_FLOAT 3
  33. typedef struct TTAContext {
  34. AVCodecContext *avctx;
  35. GetBitContext gb;
  36. int flags, channels, bps, is_float, data_length;
  37. int frame_length, last_frame_length, total_frames;
  38. int32_t *decode_buffer;
  39. } TTAContext;
  40. #if 0
  41. static inline int shift_1(int i)
  42. {
  43. if (i < 32)
  44. return 1 << i;
  45. else
  46. return 0x80000000; // 16 << 31
  47. }
  48. static inline int shift_16(int i)
  49. {
  50. if (i < 28)
  51. return 16 << i;
  52. else
  53. return 0x80000000; // 16 << 27
  54. }
  55. #else
  56. static const uint32_t shift_1[] = {
  57. 0x00000001, 0x00000002, 0x00000004, 0x00000008,
  58. 0x00000010, 0x00000020, 0x00000040, 0x00000080,
  59. 0x00000100, 0x00000200, 0x00000400, 0x00000800,
  60. 0x00001000, 0x00002000, 0x00004000, 0x00008000,
  61. 0x00010000, 0x00020000, 0x00040000, 0x00080000,
  62. 0x00100000, 0x00200000, 0x00400000, 0x00800000,
  63. 0x01000000, 0x02000000, 0x04000000, 0x08000000,
  64. 0x10000000, 0x20000000, 0x40000000, 0x80000000,
  65. 0x80000000, 0x80000000, 0x80000000, 0x80000000,
  66. 0x80000000, 0x80000000, 0x80000000, 0x80000000
  67. };
  68. static const uint32_t *shift_16 = shift_1 + 4;
  69. #endif
  70. #define MAX_ORDER 16
  71. typedef struct TTAFilter {
  72. int32_t shift, round, error, mode;
  73. int32_t qm[MAX_ORDER];
  74. int32_t dx[MAX_ORDER];
  75. int32_t dl[MAX_ORDER];
  76. } TTAFilter;
  77. static int32_t ttafilter_configs[4][2] = {
  78. {10, 1},
  79. {9, 1},
  80. {10, 1},
  81. {12, 0}
  82. };
  83. static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) {
  84. memset(c, 0, sizeof(TTAFilter));
  85. c->shift = shift;
  86. c->round = shift_1[shift-1];
  87. // c->round = 1 << (shift - 1);
  88. c->mode = mode;
  89. }
  90. // FIXME: copy paste from original
  91. static inline void memshl(register int32_t *a, register int32_t *b) {
  92. *a++ = *b++;
  93. *a++ = *b++;
  94. *a++ = *b++;
  95. *a++ = *b++;
  96. *a++ = *b++;
  97. *a++ = *b++;
  98. *a++ = *b++;
  99. *a = *b;
  100. }
  101. // FIXME: copy paste from original
  102. // mode=1 encoder, mode=0 decoder
  103. static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) {
  104. register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
  105. if (!c->error) {
  106. sum += *dl++ * *qm, qm++;
  107. sum += *dl++ * *qm, qm++;
  108. sum += *dl++ * *qm, qm++;
  109. sum += *dl++ * *qm, qm++;
  110. sum += *dl++ * *qm, qm++;
  111. sum += *dl++ * *qm, qm++;
  112. sum += *dl++ * *qm, qm++;
  113. sum += *dl++ * *qm, qm++;
  114. dx += 8;
  115. } else if(c->error < 0) {
  116. sum += *dl++ * (*qm -= *dx++), qm++;
  117. sum += *dl++ * (*qm -= *dx++), qm++;
  118. sum += *dl++ * (*qm -= *dx++), qm++;
  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. } else {
  125. sum += *dl++ * (*qm += *dx++), qm++;
  126. sum += *dl++ * (*qm += *dx++), qm++;
  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. }
  134. *(dx-0) = ((*(dl-1) >> 30) | 1) << 2;
  135. *(dx-1) = ((*(dl-2) >> 30) | 1) << 1;
  136. *(dx-2) = ((*(dl-3) >> 30) | 1) << 1;
  137. *(dx-3) = ((*(dl-4) >> 30) | 1);
  138. // compress
  139. if (mode) {
  140. *dl = *in;
  141. *in -= (sum >> c->shift);
  142. c->error = *in;
  143. } else {
  144. c->error = *in;
  145. *in += (sum >> c->shift);
  146. *dl = *in;
  147. }
  148. if (c->mode) {
  149. *(dl-1) = *dl - *(dl-1);
  150. *(dl-2) = *(dl-1) - *(dl-2);
  151. *(dl-3) = *(dl-2) - *(dl-3);
  152. }
  153. memshl(c->dl, c->dl + 1);
  154. memshl(c->dx, c->dx + 1);
  155. }
  156. typedef struct TTARice {
  157. uint32_t k0, k1, sum0, sum1;
  158. } TTARice;
  159. static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
  160. {
  161. c->k0 = k0;
  162. c->k1 = k1;
  163. c->sum0 = shift_16[k0];
  164. c->sum1 = shift_16[k1];
  165. }
  166. static int tta_get_unary(GetBitContext *gb)
  167. {
  168. int ret = 0;
  169. // count ones
  170. while(get_bits1(gb))
  171. ret++;
  172. return ret;
  173. }
  174. // shamelessly copied from shorten.c
  175. static int inline get_le16(GetBitContext *gb)
  176. {
  177. return bswap_16(get_bits_long(gb, 16));
  178. }
  179. static int inline get_le32(GetBitContext *gb)
  180. {
  181. return bswap_32(get_bits_long(gb, 32));
  182. }
  183. static int tta_decode_init(AVCodecContext * avctx)
  184. {
  185. TTAContext *s = avctx->priv_data;
  186. int i;
  187. s->avctx = avctx;
  188. // 30bytes includes a seektable with one frame
  189. if (avctx->extradata_size < 30)
  190. return -1;
  191. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size);
  192. if (show_bits_long(&s->gb, 32) == bswap_32(ff_get_fourcc("TTA1")))
  193. {
  194. /* signature */
  195. skip_bits(&s->gb, 32);
  196. // if (get_bits_long(&s->gb, 32) != bswap_32(ff_get_fourcc("TTA1"))) {
  197. // av_log(s->avctx, AV_LOG_ERROR, "Missing magic\n");
  198. // return -1;
  199. // }
  200. s->flags = get_le16(&s->gb);
  201. if (s->flags != 1 && s->flags != 3)
  202. {
  203. av_log(s->avctx, AV_LOG_ERROR, "Invalid flags\n");
  204. return -1;
  205. }
  206. s->is_float = (s->flags == FORMAT_FLOAT);
  207. avctx->channels = s->channels = get_le16(&s->gb);
  208. avctx->bits_per_sample = get_le16(&s->gb);
  209. s->bps = (avctx->bits_per_sample + 7) / 8;
  210. avctx->sample_rate = get_le32(&s->gb);
  211. if(avctx->sample_rate > 1000000){ //prevent FRAME_TIME * avctx->sample_rate from overflowing and sanity check
  212. av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
  213. return -1;
  214. }
  215. s->data_length = get_le32(&s->gb);
  216. skip_bits(&s->gb, 32); // CRC32 of header
  217. if (s->is_float)
  218. {
  219. avctx->sample_fmt = SAMPLE_FMT_FLT;
  220. av_log(s->avctx, AV_LOG_ERROR, "Unsupported sample format. Please contact the developers.\n");
  221. return -1;
  222. }
  223. else switch(s->bps) {
  224. // case 1: avctx->sample_fmt = SAMPLE_FMT_U8; break;
  225. case 2: avctx->sample_fmt = SAMPLE_FMT_S16; break;
  226. // case 3: avctx->sample_fmt = SAMPLE_FMT_S24; break;
  227. case 4: avctx->sample_fmt = SAMPLE_FMT_S32; break;
  228. default:
  229. av_log(s->avctx, AV_LOG_ERROR, "Invalid/unsupported sample format. Please contact the developers.\n");
  230. return -1;
  231. }
  232. // FIXME: horribly broken, but directly from reference source
  233. #define FRAME_TIME 1.04489795918367346939
  234. s->frame_length = (int)(FRAME_TIME * avctx->sample_rate);
  235. s->last_frame_length = s->data_length % s->frame_length;
  236. s->total_frames = s->data_length / s->frame_length +
  237. (s->last_frame_length ? 1 : 0);
  238. av_log(s->avctx, AV_LOG_DEBUG, "flags: %x chans: %d bps: %d rate: %d block: %d\n",
  239. s->flags, avctx->channels, avctx->bits_per_sample, avctx->sample_rate,
  240. avctx->block_align);
  241. av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  242. s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
  243. // FIXME: seek table
  244. for (i = 0; i < s->total_frames; i++)
  245. skip_bits(&s->gb, 32);
  246. skip_bits(&s->gb, 32); // CRC32 of seektable
  247. if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
  248. av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
  249. return -1;
  250. }
  251. s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
  252. } else {
  253. av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
  254. return -1;
  255. }
  256. return 0;
  257. }
  258. static int tta_decode_frame(AVCodecContext *avctx,
  259. void *data, int *data_size,
  260. uint8_t *buf, int buf_size)
  261. {
  262. TTAContext *s = avctx->priv_data;
  263. int i;
  264. init_get_bits(&s->gb, buf, buf_size*8);
  265. {
  266. int32_t predictors[s->channels];
  267. TTAFilter filters[s->channels];
  268. TTARice rices[s->channels];
  269. int cur_chan = 0, framelen = s->frame_length;
  270. int32_t *p;
  271. // FIXME: seeking
  272. s->total_frames--;
  273. if (!s->total_frames && s->last_frame_length)
  274. framelen = s->last_frame_length;
  275. // init per channel states
  276. for (i = 0; i < s->channels; i++) {
  277. predictors[i] = 0;
  278. ttafilter_init(&(filters[i]), ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
  279. rice_init(&(rices[i]), 10, 10);
  280. }
  281. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  282. int32_t *predictor = &(predictors[cur_chan]);
  283. TTAFilter *filter = &(filters[cur_chan]);
  284. TTARice *rice = &(rices[cur_chan]);
  285. uint32_t unary, depth, k;
  286. int32_t value;
  287. unary = tta_get_unary(&s->gb);
  288. if (unary == 0) {
  289. depth = 0;
  290. k = rice->k0;
  291. } else {
  292. depth = 1;
  293. k = rice->k1;
  294. unary--;
  295. }
  296. if (k)
  297. value = (unary << k) + get_bits(&s->gb, k);
  298. else
  299. value = unary;
  300. // FIXME: copy paste from original
  301. switch (depth) {
  302. case 1:
  303. rice->sum1 += value - (rice->sum1 >> 4);
  304. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  305. rice->k1--;
  306. else if(rice->sum1 > shift_16[rice->k1 + 1])
  307. rice->k1++;
  308. value += shift_1[rice->k0];
  309. default:
  310. rice->sum0 += value - (rice->sum0 >> 4);
  311. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  312. rice->k0--;
  313. else if(rice->sum0 > shift_16[rice->k0 + 1])
  314. rice->k0++;
  315. }
  316. // extract sign
  317. #define SIGN(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
  318. *p = SIGN(value);
  319. // run hybrid filter
  320. ttafilter_process(filter, p, 0);
  321. // fixed order prediction
  322. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  323. switch (s->bps) {
  324. case 1: *p += PRED(*predictor, 4); break;
  325. case 2:
  326. case 3: *p += PRED(*predictor, 5); break;
  327. case 4: *p += *predictor; break;
  328. }
  329. *predictor = *p;
  330. #if 0
  331. // extract 32bit float from last two int samples
  332. if (s->is_float && ((p - data) & 1)) {
  333. uint32_t neg = *p & 0x80000000;
  334. uint32_t hi = *(p - 1);
  335. uint32_t lo = abs(*p) - 1;
  336. hi += (hi || lo) ? 0x3f80 : 0;
  337. // SWAP16: swap all the 16 bits
  338. *(p - 1) = (hi << 16) | SWAP16(lo) | neg;
  339. }
  340. #endif
  341. /*if ((get_bits_count(&s->gb)+7)/8 > buf_size)
  342. {
  343. av_log(NULL, AV_LOG_INFO, "overread!!\n");
  344. break;
  345. }*/
  346. // flip channels
  347. if (cur_chan < (s->channels-1))
  348. cur_chan++;
  349. else {
  350. // decorrelate in case of stereo integer
  351. if (!s->is_float && (s->channels > 1)) {
  352. int32_t *r = p - 1;
  353. for (*p += *r / 2; r > p - s->channels; r--)
  354. *r = *(r + 1) - *r;
  355. }
  356. cur_chan = 0;
  357. }
  358. }
  359. skip_bits(&s->gb, 32); // frame crc
  360. // convert to output buffer
  361. switch(s->bps) {
  362. case 2: {
  363. uint16_t *samples = data;
  364. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  365. // *samples++ = (unsigned char)*p;
  366. // *samples++ = (unsigned char)(*p >> 8);
  367. *samples++ = *p;
  368. }
  369. *data_size = (uint8_t *)samples - (uint8_t *)data;
  370. break;
  371. }
  372. default:
  373. av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n");
  374. }
  375. }
  376. // return get_bits_count(&s->gb)+7)/8;
  377. return buf_size;
  378. }
  379. static int tta_decode_close(AVCodecContext *avctx) {
  380. TTAContext *s = avctx->priv_data;
  381. if (s->decode_buffer)
  382. av_free(s->decode_buffer);
  383. return 0;
  384. }
  385. AVCodec tta_decoder = {
  386. "tta",
  387. CODEC_TYPE_AUDIO,
  388. CODEC_ID_TTA,
  389. sizeof(TTAContext),
  390. tta_decode_init,
  391. NULL,
  392. tta_decode_close,
  393. tta_decode_frame,
  394. };