tta.c
Go to the documentation of this file.
1 /*
2  * TTA (The Lossless True Audio) decoder
3  * Copyright (c) 2006 Alex Beregszaszi
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
30 #define BITSTREAM_READER_LE
31 //#define DEBUG
32 #include <limits.h>
33 #include "avcodec.h"
34 #include "get_bits.h"
35 #include "libavutil/crc.h"
36 
37 #define FORMAT_SIMPLE 1
38 #define FORMAT_ENCRYPTED 2
39 
40 #define MAX_ORDER 16
41 typedef struct TTAFilter {
42  int32_t shift, round, error, mode;
43  int32_t qm[MAX_ORDER];
44  int32_t dx[MAX_ORDER];
45  int32_t dl[MAX_ORDER];
46 } TTAFilter;
47 
48 typedef struct TTARice {
49  uint32_t k0, k1, sum0, sum1;
50 } TTARice;
51 
52 typedef struct TTAChannel {
53  int32_t predictor;
56 } TTAChannel;
57 
58 typedef struct TTAContext {
62  const AVCRC *crc_table;
63 
65  unsigned data_length;
67 
68  int32_t *decode_buffer;
69 
71 } TTAContext;
72 
73 static const uint32_t shift_1[] = {
74  0x00000001, 0x00000002, 0x00000004, 0x00000008,
75  0x00000010, 0x00000020, 0x00000040, 0x00000080,
76  0x00000100, 0x00000200, 0x00000400, 0x00000800,
77  0x00001000, 0x00002000, 0x00004000, 0x00008000,
78  0x00010000, 0x00020000, 0x00040000, 0x00080000,
79  0x00100000, 0x00200000, 0x00400000, 0x00800000,
80  0x01000000, 0x02000000, 0x04000000, 0x08000000,
81  0x10000000, 0x20000000, 0x40000000, 0x80000000,
82  0x80000000, 0x80000000, 0x80000000, 0x80000000,
83  0x80000000, 0x80000000, 0x80000000, 0x80000000
84 };
85 
86 static const uint32_t * const shift_16 = shift_1 + 4;
87 
88 static const int32_t ttafilter_configs[4][2] = {
89  {10, 1},
90  {9, 1},
91  {10, 1},
92  {12, 0}
93 };
94 
95 static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) {
96  memset(c, 0, sizeof(TTAFilter));
97  c->shift = shift;
98  c->round = shift_1[shift-1];
99 // c->round = 1 << (shift - 1);
100  c->mode = mode;
101 }
102 
103 // FIXME: copy paste from original
104 static inline void memshl(register int32_t *a, register int32_t *b) {
105  *a++ = *b++;
106  *a++ = *b++;
107  *a++ = *b++;
108  *a++ = *b++;
109  *a++ = *b++;
110  *a++ = *b++;
111  *a++ = *b++;
112  *a = *b;
113 }
114 
115 // FIXME: copy paste from original
116 // mode=1 encoder, mode=0 decoder
117 static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) {
118  register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
119 
120  if (!c->error) {
121  sum += *dl++ * *qm, qm++;
122  sum += *dl++ * *qm, qm++;
123  sum += *dl++ * *qm, qm++;
124  sum += *dl++ * *qm, qm++;
125  sum += *dl++ * *qm, qm++;
126  sum += *dl++ * *qm, qm++;
127  sum += *dl++ * *qm, qm++;
128  sum += *dl++ * *qm, qm++;
129  dx += 8;
130  } else if(c->error < 0) {
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  sum += *dl++ * (*qm -= *dx++), qm++;
136  sum += *dl++ * (*qm -= *dx++), qm++;
137  sum += *dl++ * (*qm -= *dx++), qm++;
138  sum += *dl++ * (*qm -= *dx++), qm++;
139  } else {
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  sum += *dl++ * (*qm += *dx++), qm++;
145  sum += *dl++ * (*qm += *dx++), qm++;
146  sum += *dl++ * (*qm += *dx++), qm++;
147  sum += *dl++ * (*qm += *dx++), qm++;
148  }
149 
150  *(dx-0) = ((*(dl-1) >> 30) | 1) << 2;
151  *(dx-1) = ((*(dl-2) >> 30) | 1) << 1;
152  *(dx-2) = ((*(dl-3) >> 30) | 1) << 1;
153  *(dx-3) = ((*(dl-4) >> 30) | 1);
154 
155  // compress
156  if (mode) {
157  *dl = *in;
158  *in -= (sum >> c->shift);
159  c->error = *in;
160  } else {
161  c->error = *in;
162  *in += (sum >> c->shift);
163  *dl = *in;
164  }
165 
166  if (c->mode) {
167  *(dl-1) = *dl - *(dl-1);
168  *(dl-2) = *(dl-1) - *(dl-2);
169  *(dl-3) = *(dl-2) - *(dl-3);
170  }
171 
172  memshl(c->dl, c->dl + 1);
173  memshl(c->dx, c->dx + 1);
174 }
175 
176 static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
177 {
178  c->k0 = k0;
179  c->k1 = k1;
180  c->sum0 = shift_16[k0];
181  c->sum1 = shift_16[k1];
182 }
183 
185 {
186  int ret = 0;
187 
188  // count ones
189  while (get_bits_left(gb) > 0 && get_bits1(gb))
190  ret++;
191  return ret;
192 }
193 
194 static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
195 {
196  uint32_t crc, CRC;
197 
198  CRC = AV_RL32(buf + buf_size);
199  crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
200  if (CRC != (crc ^ 0xFFFFFFFFU)) {
201  av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
202  return AVERROR_INVALIDDATA;
203  }
204 
205  return 0;
206 }
207 
209 {
210  TTAContext *s = avctx->priv_data;
211 
212  s->avctx = avctx;
213 
214  // 30bytes includes a seektable with one frame
215  if (avctx->extradata_size < 30)
216  return -1;
217 
218  init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
219  if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
220  {
221  if (avctx->err_recognition & AV_EF_CRCCHECK) {
223  if (tta_check_crc(s, avctx->extradata, 18))
224  return AVERROR_INVALIDDATA;
225  }
226 
227  /* signature */
228  skip_bits_long(&s->gb, 32);
229 
230  s->format = get_bits(&s->gb, 16);
231  if (s->format > 2) {
232  av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n");
233  return -1;
234  }
235  if (s->format == FORMAT_ENCRYPTED) {
236  av_log_missing_feature(s->avctx, "Encrypted TTA", 0);
237  return AVERROR(EINVAL);
238  }
239  avctx->channels = s->channels = get_bits(&s->gb, 16);
240  avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
241  s->bps = (avctx->bits_per_coded_sample + 7) / 8;
242  avctx->sample_rate = get_bits_long(&s->gb, 32);
243  s->data_length = get_bits_long(&s->gb, 32);
244  skip_bits_long(&s->gb, 32); // CRC32 of header
245 
246  if (s->channels == 0) {
247  av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
248  return AVERROR_INVALIDDATA;
249  } else if (avctx->sample_rate == 0) {
250  av_log(s->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
251  return AVERROR_INVALIDDATA;
252  }
253 
254  switch(s->bps) {
255  case 2:
256  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
257  avctx->bits_per_raw_sample = 16;
258  break;
259  case 3:
260  avctx->sample_fmt = AV_SAMPLE_FMT_S32;
261  avctx->bits_per_raw_sample = 24;
262  break;
263  default:
264  av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
265  return AVERROR_INVALIDDATA;
266  }
267 
268  // prevent overflow
269  if (avctx->sample_rate > 0x7FFFFFu) {
270  av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
271  return AVERROR(EINVAL);
272  }
273  s->frame_length = 256 * avctx->sample_rate / 245;
274 
276  s->total_frames = s->data_length / s->frame_length +
277  (s->last_frame_length ? 1 : 0);
278 
279  av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
280  s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
281  avctx->block_align);
282  av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
284 
285  // FIXME: seek table
286  if (avctx->extradata_size <= 26 || s->total_frames > INT_MAX / 4 ||
287  avctx->extradata_size - 26 < s->total_frames * 4)
288  av_log(avctx, AV_LOG_WARNING, "Seek table missing or too small\n");
289  else if (avctx->err_recognition & AV_EF_CRCCHECK) {
290  if (tta_check_crc(s, avctx->extradata + 22, s->total_frames * 4))
291  return AVERROR_INVALIDDATA;
292  }
293  skip_bits_long(&s->gb, 32 * s->total_frames);
294  skip_bits_long(&s->gb, 32); // CRC32 of seektable
295 
296  if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
297  av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
298  return -1;
299  }
300 
301  if (s->bps == 2) {
302  s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
303  if (!s->decode_buffer)
304  return AVERROR(ENOMEM);
305  }
306  s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
307  if (!s->ch_ctx) {
308  av_freep(&s->decode_buffer);
309  return AVERROR(ENOMEM);
310  }
311  } else {
312  av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
313  return -1;
314  }
315 
317  avctx->coded_frame = &s->frame;
318 
319  return 0;
320 }
321 
322 static int tta_decode_frame(AVCodecContext *avctx, void *data,
323  int *got_frame_ptr, AVPacket *avpkt)
324 {
325  const uint8_t *buf = avpkt->data;
326  int buf_size = avpkt->size;
327  TTAContext *s = avctx->priv_data;
328  int i, ret;
329  int cur_chan = 0, framelen = s->frame_length;
330  int32_t *p;
331 
332  if (avctx->err_recognition & AV_EF_CRCCHECK) {
333  if (buf_size < 4 || tta_check_crc(s, buf, buf_size - 4))
334  return AVERROR_INVALIDDATA;
335  }
336 
337  init_get_bits(&s->gb, buf, buf_size*8);
338 
339  // FIXME: seeking
340  s->total_frames--;
341  if (!s->total_frames && s->last_frame_length)
342  framelen = s->last_frame_length;
343 
344  /* get output buffer */
345  s->frame.nb_samples = framelen;
346  if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
347  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
348  return ret;
349  }
350 
351  // decode directly to output buffer for 24-bit sample format
352  if (s->bps == 3)
353  s->decode_buffer = (int32_t *)s->frame.data[0];
354 
355  // init per channel states
356  for (i = 0; i < s->channels; i++) {
357  s->ch_ctx[i].predictor = 0;
359  rice_init(&s->ch_ctx[i].rice, 10, 10);
360  }
361 
362  for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
363  int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
364  TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
365  TTARice *rice = &s->ch_ctx[cur_chan].rice;
366  uint32_t unary, depth, k;
367  int32_t value;
368 
369  unary = tta_get_unary(&s->gb);
370 
371  if (unary == 0) {
372  depth = 0;
373  k = rice->k0;
374  } else {
375  depth = 1;
376  k = rice->k1;
377  unary--;
378  }
379 
380  if (get_bits_left(&s->gb) < k)
381  return -1;
382 
383  if (k) {
384  if (k > MIN_CACHE_BITS)
385  return -1;
386  value = (unary << k) + get_bits(&s->gb, k);
387  } else
388  value = unary;
389 
390  // FIXME: copy paste from original
391  switch (depth) {
392  case 1:
393  rice->sum1 += value - (rice->sum1 >> 4);
394  if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
395  rice->k1--;
396  else if(rice->sum1 > shift_16[rice->k1 + 1])
397  rice->k1++;
398  value += shift_1[rice->k0];
399  default:
400  rice->sum0 += value - (rice->sum0 >> 4);
401  if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
402  rice->k0--;
403  else if(rice->sum0 > shift_16[rice->k0 + 1])
404  rice->k0++;
405  }
406 
407  // extract coded value
408 #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
409  *p = UNFOLD(value);
410 
411  // run hybrid filter
412  ttafilter_process(filter, p, 0);
413 
414  // fixed order prediction
415 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
416  switch (s->bps) {
417  case 1: *p += PRED(*predictor, 4); break;
418  case 2:
419  case 3: *p += PRED(*predictor, 5); break;
420  case 4: *p += *predictor; break;
421  }
422  *predictor = *p;
423 
424  // flip channels
425  if (cur_chan < (s->channels-1))
426  cur_chan++;
427  else {
428  // decorrelate in case of stereo integer
429  if (s->channels > 1) {
430  int32_t *r = p - 1;
431  for (*p += *r / 2; r > p - s->channels; r--)
432  *r = *(r + 1) - *r;
433  }
434  cur_chan = 0;
435  }
436  }
437 
438  if (get_bits_left(&s->gb) < 32)
439  return -1;
440  skip_bits_long(&s->gb, 32); // frame crc
441 
442  // convert to output buffer
443  if (s->bps == 2) {
444  int16_t *samples = (int16_t *)s->frame.data[0];
445  for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
446  *samples++ = *p;
447  } else {
448  // shift samples for 24-bit sample format
449  int32_t *samples = (int32_t *)s->frame.data[0];
450  for (i = 0; i < framelen * s->channels; i++)
451  *samples++ <<= 8;
452  // reset decode buffer
453  s->decode_buffer = NULL;
454  }
455 
456  *got_frame_ptr = 1;
457  *(AVFrame *)data = s->frame;
458 
459  return buf_size;
460 }
461 
463  TTAContext *s = avctx->priv_data;
464 
466  av_freep(&s->ch_ctx);
467 
468  return 0;
469 }
470 
472  .name = "tta",
473  .type = AVMEDIA_TYPE_AUDIO,
474  .id = CODEC_ID_TTA,
475  .priv_data_size = sizeof(TTAContext),
479  .capabilities = CODEC_CAP_DR1,
480  .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
481 };