2 * TTA (The Lossless True Audio) decoder
3 * Copyright (c) 2006 Alex Beregszaszi
5 * This file is part of FFmpeg.
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.
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.
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
24 * TTA (The Lossless True Audio) decoder
25 * @see http://www.true-audio.com/
26 * @see http://tta.corecodec.org/
27 * @author Alex Beregszaszi
30 #define BITSTREAM_READER_LE
37 #include "libavutil/crc.h"
38 #include "libavutil/intreadwrite.h"
39 #include "libavutil/opt.h"
41 #define FORMAT_SIMPLE 1
42 #define FORMAT_ENCRYPTED 2
44 typedef struct TTAContext {
46 AVCodecContext *avctx;
47 const AVCRC *crc_table;
49 int format, channels, bps;
51 int frame_length, last_frame_length;
53 int32_t *decode_buffer;
60 static inline void ttafilter_process(TTAFilter *c, int32_t *in)
62 register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
65 qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
66 qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
67 } else if (c->error > 0) {
68 qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
69 qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
72 sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
73 dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
75 dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
76 dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
78 dx[4] = ((dl[4] >> 30) | 1);
79 dx[5] = ((dl[5] >> 30) | 2) & ~1;
80 dx[6] = ((dl[6] >> 30) | 2) & ~1;
81 dx[7] = ((dl[7] >> 30) | 4) & ~3;
84 *in += (sum >> c->shift);
86 dl[4] = -dl[5]; dl[5] = -dl[6];
87 dl[6] = *in - dl[7]; dl[7] = *in;
88 dl[5] += dl[6]; dl[4] += dl[5];
91 static const int64_t tta_channel_layouts[7] = {
93 AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
96 AV_CH_LAYOUT_5POINT1_BACK,
97 AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
98 AV_CH_LAYOUT_7POINT1_WIDE
101 static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
105 CRC = AV_RL32(buf + buf_size);
106 crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
107 if (CRC != (crc ^ 0xFFFFFFFFU)) {
108 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
109 return AVERROR_INVALIDDATA;
115 static uint64_t tta_check_crc64(uint8_t *pass)
117 uint64_t crc = UINT64_MAX, poly = 0x42F0E1EBA9EA3693U;
118 uint8_t *end = pass + strlen(pass);
122 crc ^= (uint64_t)*pass++ << 56;
123 for (i = 0; i < 8; i++)
124 crc = (crc << 1) ^ (poly & (((int64_t) crc) >> 63));
127 return crc ^ UINT64_MAX;
130 static av_cold int tta_decode_init(AVCodecContext * avctx)
132 TTAContext *s = avctx->priv_data;
138 // 30bytes includes TTA1 header
139 if (avctx->extradata_size < 22)
140 return AVERROR_INVALIDDATA;
142 s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
143 init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
144 if (show_bits_long(&gb, 32) == AV_RL32("TTA1")) {
146 skip_bits_long(&gb, 32);
148 s->format = get_bits(&gb, 16);
150 av_log(avctx, AV_LOG_ERROR, "Invalid format\n");
151 return AVERROR_INVALIDDATA;
153 if (s->format == FORMAT_ENCRYPTED) {
155 av_log(avctx, AV_LOG_ERROR, "Missing password for encrypted stream. Please use the -password option\n");
156 return AVERROR(EINVAL);
158 AV_WL64(s->crc_pass, tta_check_crc64(s->pass));
160 avctx->channels = s->channels = get_bits(&gb, 16);
161 if (s->channels > 1 && s->channels < 9)
162 avctx->channel_layout = tta_channel_layouts[s->channels-2];
163 avctx->bits_per_raw_sample = get_bits(&gb, 16);
164 s->bps = (avctx->bits_per_raw_sample + 7) / 8;
165 avctx->sample_rate = get_bits_long(&gb, 32);
166 s->data_length = get_bits_long(&gb, 32);
167 skip_bits_long(&gb, 32); // CRC32 of header
169 if (s->channels == 0) {
170 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
171 return AVERROR_INVALIDDATA;
172 } else if (avctx->sample_rate == 0) {
173 av_log(avctx, AV_LOG_ERROR, "Invalid samplerate\n");
174 return AVERROR_INVALIDDATA;
178 case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
180 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
183 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
185 //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
187 av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
188 return AVERROR_INVALIDDATA;
192 if (avctx->sample_rate > 0x7FFFFFu) {
193 av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
194 return AVERROR(EINVAL);
196 s->frame_length = 256 * avctx->sample_rate / 245;
198 s->last_frame_length = s->data_length % s->frame_length;
199 total_frames = s->data_length / s->frame_length +
200 (s->last_frame_length ? 1 : 0);
202 av_log(avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
203 s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
205 av_log(avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
206 s->data_length, s->frame_length, s->last_frame_length, total_frames);
208 if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
209 av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
210 return AVERROR_INVALIDDATA;
214 s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
215 if (!s->decode_buffer)
216 return AVERROR(ENOMEM);
218 s->decode_buffer = NULL;
219 s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
221 av_freep(&s->decode_buffer);
222 return AVERROR(ENOMEM);
225 av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
226 return AVERROR_INVALIDDATA;
232 static int tta_decode_frame(AVCodecContext *avctx, void *data,
233 int *got_frame_ptr, AVPacket *avpkt)
235 AVFrame *frame = data;
236 const uint8_t *buf = avpkt->data;
237 int buf_size = avpkt->size;
238 TTAContext *s = avctx->priv_data;
241 int cur_chan = 0, framelen = s->frame_length;
244 if (avctx->err_recognition & AV_EF_CRCCHECK) {
245 if (buf_size < 4 || tta_check_crc(s, buf, buf_size - 4))
246 return AVERROR_INVALIDDATA;
249 if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
252 /* get output buffer */
253 frame->nb_samples = framelen;
254 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
257 // decode directly to output buffer for 24-bit sample format
259 s->decode_buffer = (int32_t *)frame->data[0];
261 // init per channel states
262 for (i = 0; i < s->channels; i++) {
263 TTAFilter *filter = &s->ch_ctx[i].filter;
264 s->ch_ctx[i].predictor = 0;
265 ff_tta_filter_init(filter, ff_tta_filter_configs[s->bps-1]);
266 if (s->format == FORMAT_ENCRYPTED) {
268 for (i = 0; i < 8; i++)
269 filter->qm[i] = sign_extend(s->crc_pass[i], 8);
271 ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
275 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
276 int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
277 TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
278 TTARice *rice = &s->ch_ctx[cur_chan].rice;
279 uint32_t unary, depth, k;
282 unary = get_unary(&gb, 0, get_bits_left(&gb));
293 if (get_bits_left(&gb) < k) {
294 ret = AVERROR_INVALIDDATA;
299 if (k > MIN_CACHE_BITS) {
300 ret = AVERROR_INVALIDDATA;
303 value = (unary << k) + get_bits(&gb, k);
307 // FIXME: copy paste from original
310 rice->sum1 += value - (rice->sum1 >> 4);
311 if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
313 else if(rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
315 value += ff_tta_shift_1[rice->k0];
317 rice->sum0 += value - (rice->sum0 >> 4);
318 if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
320 else if(rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
324 // extract coded value
325 *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
328 ttafilter_process(filter, p);
330 // fixed order prediction
331 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
333 case 1: *p += PRED(*predictor, 4); break;
335 case 3: *p += PRED(*predictor, 5); break;
336 case 4: *p += *predictor; break;
341 if (cur_chan < (s->channels-1))
344 // decorrelate in case of multiple channels
345 if (s->channels > 1) {
347 for (*p += *r / 2; r > p - s->channels; r--)
352 // check for last frame
353 if (i == s->last_frame_length && get_bits_left(&gb) / 8 == 4) {
354 frame->nb_samples = framelen = s->last_frame_length;
361 if (get_bits_left(&gb) < 32) {
362 ret = AVERROR_INVALIDDATA;
365 skip_bits_long(&gb, 32); // frame crc
367 // convert to output buffer
370 uint8_t *samples = (uint8_t *)frame->data[0];
371 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
372 *samples++ = *p + 0x80;
376 int16_t *samples = (int16_t *)frame->data[0];
377 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
382 // shift samples for 24-bit sample format
383 int32_t *samples = (int32_t *)frame->data[0];
384 for (i = 0; i < framelen * s->channels; i++)
386 // reset decode buffer
387 s->decode_buffer = NULL;
396 // reset decode buffer
398 s->decode_buffer = NULL;
402 static av_cold int tta_decode_close(AVCodecContext *avctx) {
403 TTAContext *s = avctx->priv_data;
406 av_free(s->decode_buffer);
407 s->decode_buffer = NULL;
408 av_freep(&s->ch_ctx);
413 #define OFFSET(x) offsetof(TTAContext, x)
414 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
415 static const AVOption options[] = {
416 { "password", "Set decoding password", OFFSET(pass), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
420 static const AVClass tta_decoder_class = {
421 .class_name = "TTA Decoder",
422 .item_name = av_default_item_name,
424 .version = LIBAVUTIL_VERSION_INT,
427 AVCodec ff_tta_decoder = {
429 .type = AVMEDIA_TYPE_AUDIO,
430 .id = AV_CODEC_ID_TTA,
431 .priv_data_size = sizeof(TTAContext),
432 .init = tta_decode_init,
433 .close = tta_decode_close,
434 .decode = tta_decode_frame,
435 .capabilities = CODEC_CAP_DR1,
436 .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
437 .priv_class = &tta_decoder_class,