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