tta.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 libavcodec/tta.c
  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 "bitstream.h"
  33. #define FORMAT_INT 1
  34. #define FORMAT_FLOAT 3
  35. typedef struct TTAContext {
  36. AVCodecContext *avctx;
  37. GetBitContext gb;
  38. int flags, channels, bps, is_float, data_length;
  39. int frame_length, last_frame_length, total_frames;
  40. int32_t *decode_buffer;
  41. } TTAContext;
  42. #if 0
  43. static inline int shift_1(int i)
  44. {
  45. if (i < 32)
  46. return 1 << i;
  47. else
  48. return 0x80000000; // 16 << 31
  49. }
  50. static inline int shift_16(int i)
  51. {
  52. if (i < 28)
  53. return 16 << i;
  54. else
  55. return 0x80000000; // 16 << 27
  56. }
  57. #else
  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. #endif
  72. #define MAX_ORDER 16
  73. typedef struct TTAFilter {
  74. int32_t shift, round, error, mode;
  75. int32_t qm[MAX_ORDER];
  76. int32_t dx[MAX_ORDER];
  77. int32_t dl[MAX_ORDER];
  78. } TTAFilter;
  79. static const int32_t ttafilter_configs[4][2] = {
  80. {10, 1},
  81. {9, 1},
  82. {10, 1},
  83. {12, 0}
  84. };
  85. static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) {
  86. memset(c, 0, sizeof(TTAFilter));
  87. c->shift = shift;
  88. c->round = shift_1[shift-1];
  89. // c->round = 1 << (shift - 1);
  90. c->mode = mode;
  91. }
  92. // FIXME: copy paste from original
  93. static inline void memshl(register int32_t *a, register int32_t *b) {
  94. *a++ = *b++;
  95. *a++ = *b++;
  96. *a++ = *b++;
  97. *a++ = *b++;
  98. *a++ = *b++;
  99. *a++ = *b++;
  100. *a++ = *b++;
  101. *a = *b;
  102. }
  103. // FIXME: copy paste from original
  104. // mode=1 encoder, mode=0 decoder
  105. static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) {
  106. register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
  107. if (!c->error) {
  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. sum += *dl++ * *qm, qm++;
  115. sum += *dl++ * *qm, qm++;
  116. dx += 8;
  117. } else if(c->error < 0) {
  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. sum += *dl++ * (*qm -= *dx++), qm++;
  125. sum += *dl++ * (*qm -= *dx++), qm++;
  126. } else {
  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. }
  136. *(dx-0) = ((*(dl-1) >> 30) | 1) << 2;
  137. *(dx-1) = ((*(dl-2) >> 30) | 1) << 1;
  138. *(dx-2) = ((*(dl-3) >> 30) | 1) << 1;
  139. *(dx-3) = ((*(dl-4) >> 30) | 1);
  140. // compress
  141. if (mode) {
  142. *dl = *in;
  143. *in -= (sum >> c->shift);
  144. c->error = *in;
  145. } else {
  146. c->error = *in;
  147. *in += (sum >> c->shift);
  148. *dl = *in;
  149. }
  150. if (c->mode) {
  151. *(dl-1) = *dl - *(dl-1);
  152. *(dl-2) = *(dl-1) - *(dl-2);
  153. *(dl-3) = *(dl-2) - *(dl-3);
  154. }
  155. memshl(c->dl, c->dl + 1);
  156. memshl(c->dx, c->dx + 1);
  157. }
  158. typedef struct TTARice {
  159. uint32_t k0, k1, sum0, sum1;
  160. } TTARice;
  161. static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
  162. {
  163. c->k0 = k0;
  164. c->k1 = k1;
  165. c->sum0 = shift_16[k0];
  166. c->sum1 = shift_16[k1];
  167. }
  168. static int tta_get_unary(GetBitContext *gb)
  169. {
  170. int ret = 0;
  171. // count ones
  172. while(get_bits1(gb))
  173. ret++;
  174. return ret;
  175. }
  176. static av_cold int tta_decode_init(AVCodecContext * avctx)
  177. {
  178. TTAContext *s = avctx->priv_data;
  179. int i;
  180. s->avctx = avctx;
  181. // 30bytes includes a seektable with one frame
  182. if (avctx->extradata_size < 30)
  183. return -1;
  184. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size);
  185. if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
  186. {
  187. /* signature */
  188. skip_bits(&s->gb, 32);
  189. // if (get_bits_long(&s->gb, 32) != bswap_32(AV_RL32("TTA1"))) {
  190. // av_log(s->avctx, AV_LOG_ERROR, "Missing magic\n");
  191. // return -1;
  192. // }
  193. s->flags = get_bits(&s->gb, 16);
  194. if (s->flags != 1 && s->flags != 3)
  195. {
  196. av_log(s->avctx, AV_LOG_ERROR, "Invalid flags\n");
  197. return -1;
  198. }
  199. s->is_float = (s->flags == FORMAT_FLOAT);
  200. avctx->channels = s->channels = get_bits(&s->gb, 16);
  201. avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
  202. s->bps = (avctx->bits_per_coded_sample + 7) / 8;
  203. avctx->sample_rate = get_bits_long(&s->gb, 32);
  204. if(avctx->sample_rate > 1000000){ //prevent FRAME_TIME * avctx->sample_rate from overflowing and sanity check
  205. av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
  206. return -1;
  207. }
  208. s->data_length = get_bits_long(&s->gb, 32);
  209. skip_bits(&s->gb, 32); // CRC32 of header
  210. if (s->is_float)
  211. {
  212. avctx->sample_fmt = SAMPLE_FMT_FLT;
  213. av_log(s->avctx, AV_LOG_ERROR, "Unsupported sample format. Please contact the developers.\n");
  214. return -1;
  215. }
  216. else switch(s->bps) {
  217. // case 1: avctx->sample_fmt = SAMPLE_FMT_U8; break;
  218. case 2: avctx->sample_fmt = SAMPLE_FMT_S16; break;
  219. // case 3: avctx->sample_fmt = SAMPLE_FMT_S24; break;
  220. case 4: avctx->sample_fmt = SAMPLE_FMT_S32; break;
  221. default:
  222. av_log(s->avctx, AV_LOG_ERROR, "Invalid/unsupported sample format. Please contact the developers.\n");
  223. return -1;
  224. }
  225. // FIXME: horribly broken, but directly from reference source
  226. #define FRAME_TIME 1.04489795918367346939
  227. s->frame_length = (int)(FRAME_TIME * avctx->sample_rate);
  228. s->last_frame_length = s->data_length % s->frame_length;
  229. s->total_frames = s->data_length / s->frame_length +
  230. (s->last_frame_length ? 1 : 0);
  231. av_log(s->avctx, AV_LOG_DEBUG, "flags: %x chans: %d bps: %d rate: %d block: %d\n",
  232. s->flags, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
  233. avctx->block_align);
  234. av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
  235. s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
  236. // FIXME: seek table
  237. for (i = 0; i < s->total_frames; i++)
  238. skip_bits(&s->gb, 32);
  239. skip_bits(&s->gb, 32); // CRC32 of seektable
  240. if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
  241. av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
  242. return -1;
  243. }
  244. s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
  245. } else {
  246. av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. static int tta_decode_frame(AVCodecContext *avctx,
  252. void *data, int *data_size,
  253. const uint8_t *buf, int buf_size)
  254. {
  255. TTAContext *s = avctx->priv_data;
  256. int i;
  257. init_get_bits(&s->gb, buf, buf_size*8);
  258. {
  259. int32_t predictors[s->channels];
  260. TTAFilter filters[s->channels];
  261. TTARice rices[s->channels];
  262. int cur_chan = 0, framelen = s->frame_length;
  263. int32_t *p;
  264. // FIXME: seeking
  265. s->total_frames--;
  266. if (!s->total_frames && s->last_frame_length)
  267. framelen = s->last_frame_length;
  268. // init per channel states
  269. for (i = 0; i < s->channels; i++) {
  270. predictors[i] = 0;
  271. ttafilter_init(&(filters[i]), ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
  272. rice_init(&(rices[i]), 10, 10);
  273. }
  274. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  275. int32_t *predictor = &(predictors[cur_chan]);
  276. TTAFilter *filter = &(filters[cur_chan]);
  277. TTARice *rice = &(rices[cur_chan]);
  278. uint32_t unary, depth, k;
  279. int32_t value;
  280. unary = tta_get_unary(&s->gb);
  281. if (unary == 0) {
  282. depth = 0;
  283. k = rice->k0;
  284. } else {
  285. depth = 1;
  286. k = rice->k1;
  287. unary--;
  288. }
  289. if (k)
  290. value = (unary << k) + get_bits(&s->gb, k);
  291. else
  292. value = unary;
  293. // FIXME: copy paste from original
  294. switch (depth) {
  295. case 1:
  296. rice->sum1 += value - (rice->sum1 >> 4);
  297. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  298. rice->k1--;
  299. else if(rice->sum1 > shift_16[rice->k1 + 1])
  300. rice->k1++;
  301. value += shift_1[rice->k0];
  302. default:
  303. rice->sum0 += value - (rice->sum0 >> 4);
  304. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  305. rice->k0--;
  306. else if(rice->sum0 > shift_16[rice->k0 + 1])
  307. rice->k0++;
  308. }
  309. // extract coded value
  310. #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
  311. *p = UNFOLD(value);
  312. // run hybrid filter
  313. ttafilter_process(filter, p, 0);
  314. // fixed order prediction
  315. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  316. switch (s->bps) {
  317. case 1: *p += PRED(*predictor, 4); break;
  318. case 2:
  319. case 3: *p += PRED(*predictor, 5); break;
  320. case 4: *p += *predictor; break;
  321. }
  322. *predictor = *p;
  323. #if 0
  324. // extract 32bit float from last two int samples
  325. if (s->is_float && ((p - data) & 1)) {
  326. uint32_t neg = *p & 0x80000000;
  327. uint32_t hi = *(p - 1);
  328. uint32_t lo = abs(*p) - 1;
  329. hi += (hi || lo) ? 0x3f80 : 0;
  330. // SWAP16: swap all the 16 bits
  331. *(p - 1) = (hi << 16) | SWAP16(lo) | neg;
  332. }
  333. #endif
  334. /*if ((get_bits_count(&s->gb)+7)/8 > buf_size)
  335. {
  336. av_log(NULL, AV_LOG_INFO, "overread!!\n");
  337. break;
  338. }*/
  339. // flip channels
  340. if (cur_chan < (s->channels-1))
  341. cur_chan++;
  342. else {
  343. // decorrelate in case of stereo integer
  344. if (!s->is_float && (s->channels > 1)) {
  345. int32_t *r = p - 1;
  346. for (*p += *r / 2; r > p - s->channels; r--)
  347. *r = *(r + 1) - *r;
  348. }
  349. cur_chan = 0;
  350. }
  351. }
  352. skip_bits(&s->gb, 32); // frame crc
  353. // convert to output buffer
  354. switch(s->bps) {
  355. case 2: {
  356. uint16_t *samples = data;
  357. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  358. // *samples++ = (unsigned char)*p;
  359. // *samples++ = (unsigned char)(*p >> 8);
  360. *samples++ = *p;
  361. }
  362. *data_size = (uint8_t *)samples - (uint8_t *)data;
  363. break;
  364. }
  365. default:
  366. av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n");
  367. }
  368. }
  369. // return get_bits_count(&s->gb)+7)/8;
  370. return buf_size;
  371. }
  372. static av_cold int tta_decode_close(AVCodecContext *avctx) {
  373. TTAContext *s = avctx->priv_data;
  374. if (s->decode_buffer)
  375. av_free(s->decode_buffer);
  376. return 0;
  377. }
  378. AVCodec tta_decoder = {
  379. "tta",
  380. CODEC_TYPE_AUDIO,
  381. CODEC_ID_TTA,
  382. sizeof(TTAContext),
  383. tta_decode_init,
  384. NULL,
  385. tta_decode_close,
  386. tta_decode_frame,
  387. .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
  388. };