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.

450 lines
13KB

  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 "get_bits.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. AVPacket *avpkt)
  254. {
  255. const uint8_t *buf = avpkt->data;
  256. int buf_size = avpkt->size;
  257. TTAContext *s = avctx->priv_data;
  258. int i;
  259. init_get_bits(&s->gb, buf, buf_size*8);
  260. {
  261. int32_t predictors[s->channels];
  262. TTAFilter filters[s->channels];
  263. TTARice rices[s->channels];
  264. int cur_chan = 0, framelen = s->frame_length;
  265. int32_t *p;
  266. // FIXME: seeking
  267. s->total_frames--;
  268. if (!s->total_frames && s->last_frame_length)
  269. framelen = s->last_frame_length;
  270. // init per channel states
  271. for (i = 0; i < s->channels; i++) {
  272. predictors[i] = 0;
  273. ttafilter_init(&(filters[i]), ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
  274. rice_init(&(rices[i]), 10, 10);
  275. }
  276. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  277. int32_t *predictor = &(predictors[cur_chan]);
  278. TTAFilter *filter = &(filters[cur_chan]);
  279. TTARice *rice = &(rices[cur_chan]);
  280. uint32_t unary, depth, k;
  281. int32_t value;
  282. unary = tta_get_unary(&s->gb);
  283. if (unary == 0) {
  284. depth = 0;
  285. k = rice->k0;
  286. } else {
  287. depth = 1;
  288. k = rice->k1;
  289. unary--;
  290. }
  291. if (k)
  292. value = (unary << k) + get_bits(&s->gb, k);
  293. else
  294. value = unary;
  295. // FIXME: copy paste from original
  296. switch (depth) {
  297. case 1:
  298. rice->sum1 += value - (rice->sum1 >> 4);
  299. if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
  300. rice->k1--;
  301. else if(rice->sum1 > shift_16[rice->k1 + 1])
  302. rice->k1++;
  303. value += shift_1[rice->k0];
  304. default:
  305. rice->sum0 += value - (rice->sum0 >> 4);
  306. if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
  307. rice->k0--;
  308. else if(rice->sum0 > shift_16[rice->k0 + 1])
  309. rice->k0++;
  310. }
  311. // extract coded value
  312. #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
  313. *p = UNFOLD(value);
  314. // run hybrid filter
  315. ttafilter_process(filter, p, 0);
  316. // fixed order prediction
  317. #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
  318. switch (s->bps) {
  319. case 1: *p += PRED(*predictor, 4); break;
  320. case 2:
  321. case 3: *p += PRED(*predictor, 5); break;
  322. case 4: *p += *predictor; break;
  323. }
  324. *predictor = *p;
  325. #if 0
  326. // extract 32bit float from last two int samples
  327. if (s->is_float && ((p - data) & 1)) {
  328. uint32_t neg = *p & 0x80000000;
  329. uint32_t hi = *(p - 1);
  330. uint32_t lo = abs(*p) - 1;
  331. hi += (hi || lo) ? 0x3f80 : 0;
  332. // SWAP16: swap all the 16 bits
  333. *(p - 1) = (hi << 16) | SWAP16(lo) | neg;
  334. }
  335. #endif
  336. /*if ((get_bits_count(&s->gb)+7)/8 > buf_size)
  337. {
  338. av_log(NULL, AV_LOG_INFO, "overread!!\n");
  339. break;
  340. }*/
  341. // flip channels
  342. if (cur_chan < (s->channels-1))
  343. cur_chan++;
  344. else {
  345. // decorrelate in case of stereo integer
  346. if (!s->is_float && (s->channels > 1)) {
  347. int32_t *r = p - 1;
  348. for (*p += *r / 2; r > p - s->channels; r--)
  349. *r = *(r + 1) - *r;
  350. }
  351. cur_chan = 0;
  352. }
  353. }
  354. skip_bits(&s->gb, 32); // frame crc
  355. // convert to output buffer
  356. switch(s->bps) {
  357. case 2: {
  358. uint16_t *samples = data;
  359. for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
  360. // *samples++ = (unsigned char)*p;
  361. // *samples++ = (unsigned char)(*p >> 8);
  362. *samples++ = *p;
  363. }
  364. *data_size = (uint8_t *)samples - (uint8_t *)data;
  365. break;
  366. }
  367. default:
  368. av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n");
  369. }
  370. }
  371. // return get_bits_count(&s->gb)+7)/8;
  372. return buf_size;
  373. }
  374. static av_cold int tta_decode_close(AVCodecContext *avctx) {
  375. TTAContext *s = avctx->priv_data;
  376. if (s->decode_buffer)
  377. av_free(s->decode_buffer);
  378. return 0;
  379. }
  380. AVCodec tta_decoder = {
  381. "tta",
  382. CODEC_TYPE_AUDIO,
  383. CODEC_ID_TTA,
  384. sizeof(TTAContext),
  385. tta_decode_init,
  386. NULL,
  387. tta_decode_close,
  388. tta_decode_frame,
  389. .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
  390. };