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.

447 lines
13KB

  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. s->data_length = get_le32(&s->gb);
  212. skip_bits(&s->gb, 32); // CRC32 of header
  213. if (s->is_float)
  214. {
  215. avctx->sample_fmt = SAMPLE_FMT_FLT;
  216. av_log(s->avctx, AV_LOG_ERROR, "Unsupported sample format. Please contact the developers.\n");
  217. return -1;
  218. }
  219. else switch(s->bps) {
  220. // case 1: avctx->sample_fmt = SAMPLE_FMT_U8; break;
  221. case 2: avctx->sample_fmt = SAMPLE_FMT_S16; break;
  222. // case 3: avctx->sample_fmt = SAMPLE_FMT_S24; break;
  223. case 4: avctx->sample_fmt = SAMPLE_FMT_S32; break;
  224. default:
  225. av_log(s->avctx, AV_LOG_ERROR, "Invalid/unsupported sample format. Please contact the developers.\n");
  226. return -1;
  227. }
  228. // FIXME: horribly broken, but directly from reference source
  229. #define FRAME_TIME 1.04489795918367346939
  230. s->frame_length = (int)(FRAME_TIME * avctx->sample_rate);
  231. s->last_frame_length = s->data_length % s->frame_length;
  232. s->total_frames = s->data_length / s->frame_length +
  233. (s->last_frame_length ? 1 : 0);
  234. av_log(s->avctx, AV_LOG_DEBUG, "flags: %x chans: %d bps: %d rate: %d block: %d\n",
  235. s->flags, avctx->channels, avctx->bits_per_sample, avctx->sample_rate,
  236. avctx->block_align);
  237. av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  238. s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
  239. // FIXME: seek table
  240. for (i = 0; i < s->total_frames; i++)
  241. skip_bits(&s->gb, 32);
  242. skip_bits(&s->gb, 32); // CRC32 of seektable
  243. s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
  244. } else {
  245. av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
  246. return -1;
  247. }
  248. return 0;
  249. }
  250. static int tta_decode_frame(AVCodecContext *avctx,
  251. void *data, int *data_size,
  252. uint8_t *buf, int buf_size)
  253. {
  254. TTAContext *s = avctx->priv_data;
  255. int i;
  256. init_get_bits(&s->gb, buf, buf_size*8);
  257. {
  258. int32_t predictors[s->channels];
  259. TTAFilter filters[s->channels];
  260. TTARice rices[s->channels];
  261. int cur_chan = 0, framelen = s->frame_length;
  262. int32_t *p;
  263. // FIXME: seeking
  264. s->total_frames--;
  265. if (!s->total_frames && s->last_frame_length)
  266. framelen = s->last_frame_length;
  267. // init per channel states
  268. for (i = 0; i < s->channels; i++) {
  269. predictors[i] = 0;
  270. ttafilter_init(&(filters[i]), ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
  271. rice_init(&(rices[i]), 10, 10);
  272. }
  273. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  274. int32_t *predictor = &(predictors[cur_chan]);
  275. TTAFilter *filter = &(filters[cur_chan]);
  276. TTARice *rice = &(rices[cur_chan]);
  277. uint32_t unary, depth, k;
  278. int32_t value;
  279. unary = tta_get_unary(&s->gb);
  280. if (unary == 0) {
  281. depth = 0;
  282. k = rice->k0;
  283. } else {
  284. depth = 1;
  285. k = rice->k1;
  286. unary--;
  287. }
  288. if (k)
  289. value = (unary << k) + get_bits(&s->gb, k);
  290. else
  291. value = unary;
  292. // FIXME: copy paste from original
  293. switch (depth) {
  294. case 1:
  295. rice->sum1 += value - (rice->sum1 >> 4);
  296. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  297. rice->k1--;
  298. else if(rice->sum1 > shift_16[rice->k1 + 1])
  299. rice->k1++;
  300. value += shift_1[rice->k0];
  301. default:
  302. rice->sum0 += value - (rice->sum0 >> 4);
  303. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  304. rice->k0--;
  305. else if(rice->sum0 > shift_16[rice->k0 + 1])
  306. rice->k0++;
  307. }
  308. // extract sign
  309. #define SIGN(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
  310. *p = SIGN(value);
  311. // run hybrid filter
  312. ttafilter_process(filter, p, 0);
  313. // fixed order prediction
  314. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  315. switch (s->bps) {
  316. case 1: *p += PRED(*predictor, 4); break;
  317. case 2:
  318. case 3: *p += PRED(*predictor, 5); break;
  319. case 4: *p += *predictor; break;
  320. }
  321. *predictor = *p;
  322. #if 0
  323. // extract 32bit float from last two int samples
  324. if (s->is_float && ((p - data) & 1)) {
  325. uint32_t neg = *p & 0x80000000;
  326. uint32_t hi = *(p - 1);
  327. uint32_t lo = abs(*p) - 1;
  328. hi += (hi || lo) ? 0x3f80 : 0;
  329. // SWAP16: swap all the 16 bits
  330. *(p - 1) = (hi << 16) | SWAP16(lo) | neg;
  331. }
  332. #endif
  333. /*if ((get_bits_count(&s->gb)+7)/8 > buf_size)
  334. {
  335. av_log(NULL, AV_LOG_INFO, "overread!!\n");
  336. break;
  337. }*/
  338. // flip channels
  339. if (cur_chan < (s->channels-1))
  340. cur_chan++;
  341. else {
  342. // decorrelate in case of stereo integer
  343. if (!s->is_float && (s->channels > 1)) {
  344. int32_t *r = p - 1;
  345. for (*p += *r / 2; r > p - s->channels; r--)
  346. *r = *(r + 1) - *r;
  347. }
  348. cur_chan = 0;
  349. }
  350. }
  351. skip_bits(&s->gb, 32); // frame crc
  352. // convert to output buffer
  353. switch(s->bps) {
  354. case 2: {
  355. uint16_t *samples = data;
  356. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  357. // *samples++ = (unsigned char)*p;
  358. // *samples++ = (unsigned char)(*p >> 8);
  359. *samples++ = *p;
  360. }
  361. *data_size = (uint8_t *)samples - (uint8_t *)data;
  362. break;
  363. }
  364. default:
  365. av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n");
  366. }
  367. }
  368. // return get_bits_count(&s->gb)+7)/8;
  369. return buf_size;
  370. }
  371. static int tta_decode_close(AVCodecContext *avctx) {
  372. TTAContext *s = avctx->priv_data;
  373. if (s->decode_buffer)
  374. av_free(s->decode_buffer);
  375. return 0;
  376. }
  377. AVCodec tta_decoder = {
  378. "tta",
  379. CODEC_TYPE_AUDIO,
  380. CODEC_ID_TTA,
  381. sizeof(TTAContext),
  382. tta_decode_init,
  383. NULL,
  384. tta_decode_close,
  385. tta_decode_frame,
  386. };