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
38 #include "libavutil/crc.h"
39 #include "libavutil/intreadwrite.h"
40 #include "libavutil/opt.h"
42 #define FORMAT_SIMPLE 1
43 #define FORMAT_ENCRYPTED 2
45 typedef struct TTAContext {
47 AVCodecContext *avctx;
48 const AVCRC *crc_table;
50 int format, channels, bps;
52 int frame_length, last_frame_length;
54 int32_t *decode_buffer;
61 static inline void ttafilter_process(TTAFilter *c, int32_t *in)
63 register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
66 qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
67 qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
68 } else if (c->error > 0) {
69 qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
70 qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
73 sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
74 dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
76 dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
77 dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
79 dx[4] = ((dl[4] >> 30) | 1);
80 dx[5] = ((dl[5] >> 30) | 2) & ~1;
81 dx[6] = ((dl[6] >> 30) | 2) & ~1;
82 dx[7] = ((dl[7] >> 30) | 4) & ~3;
85 *in += (sum >> c->shift);
87 dl[4] = -dl[5]; dl[5] = -dl[6];
88 dl[6] = *in - dl[7]; dl[7] = *in;
89 dl[5] += dl[6]; dl[4] += dl[5];
92 static const int64_t tta_channel_layouts[7] = {
94 AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
97 AV_CH_LAYOUT_5POINT1_BACK,
98 AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
99 AV_CH_LAYOUT_7POINT1_WIDE
102 static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
106 CRC = AV_RL32(buf + buf_size);
107 crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
108 if (CRC != (crc ^ 0xFFFFFFFFU)) {
109 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
110 return AVERROR_INVALIDDATA;
116 static uint64_t tta_check_crc64(uint8_t *pass)
118 uint64_t crc = UINT64_MAX, poly = 0x42F0E1EBA9EA3693U;
119 uint8_t *end = pass + strlen(pass);
123 crc ^= (uint64_t)*pass++ << 56;
124 for (i = 0; i < 8; i++)
125 crc = (crc << 1) ^ (poly & (((int64_t) crc) >> 63));
128 return crc ^ UINT64_MAX;
131 static int allocate_buffers(AVCodecContext *avctx)
133 TTAContext *s = avctx->priv_data;
136 s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
137 if (!s->decode_buffer)
138 return AVERROR(ENOMEM);
140 s->decode_buffer = NULL;
141 s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
143 av_freep(&s->decode_buffer);
144 return AVERROR(ENOMEM);
150 static av_cold int tta_decode_init(AVCodecContext * avctx)
152 TTAContext *s = avctx->priv_data;
158 // 30bytes includes TTA1 header
159 if (avctx->extradata_size < 22)
160 return AVERROR_INVALIDDATA;
162 s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
163 init_get_bits8(&gb, avctx->extradata, avctx->extradata_size);
164 if (show_bits_long(&gb, 32) == AV_RL32("TTA1")) {
166 skip_bits_long(&gb, 32);
168 s->format = get_bits(&gb, 16);
170 av_log(avctx, AV_LOG_ERROR, "Invalid format\n");
171 return AVERROR_INVALIDDATA;
173 if (s->format == FORMAT_ENCRYPTED) {
175 av_log(avctx, AV_LOG_ERROR, "Missing password for encrypted stream. Please use the -password option\n");
176 return AVERROR(EINVAL);
178 AV_WL64(s->crc_pass, tta_check_crc64(s->pass));
180 avctx->channels = s->channels = get_bits(&gb, 16);
181 if (s->channels > 1 && s->channels < 9)
182 avctx->channel_layout = tta_channel_layouts[s->channels-2];
183 avctx->bits_per_raw_sample = get_bits(&gb, 16);
184 s->bps = (avctx->bits_per_raw_sample + 7) / 8;
185 avctx->sample_rate = get_bits_long(&gb, 32);
186 s->data_length = get_bits_long(&gb, 32);
187 skip_bits_long(&gb, 32); // CRC32 of header
189 if (s->channels == 0) {
190 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
191 return AVERROR_INVALIDDATA;
192 } else if (avctx->sample_rate == 0) {
193 av_log(avctx, AV_LOG_ERROR, "Invalid samplerate\n");
194 return AVERROR_INVALIDDATA;
198 case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
200 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
203 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
205 //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
207 av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
208 return AVERROR_INVALIDDATA;
212 if (avctx->sample_rate > 0x7FFFFFu) {
213 av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
214 return AVERROR(EINVAL);
216 s->frame_length = 256 * avctx->sample_rate / 245;
218 s->last_frame_length = s->data_length % s->frame_length;
219 total_frames = s->data_length / s->frame_length +
220 (s->last_frame_length ? 1 : 0);
222 av_log(avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
223 s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
225 av_log(avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
226 s->data_length, s->frame_length, s->last_frame_length, total_frames);
228 if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
229 av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
230 return AVERROR_INVALIDDATA;
233 av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
234 return AVERROR_INVALIDDATA;
237 return allocate_buffers(avctx);
240 static int tta_decode_frame(AVCodecContext *avctx, void *data,
241 int *got_frame_ptr, AVPacket *avpkt)
243 AVFrame *frame = data;
244 ThreadFrame tframe = { .f = data };
245 const uint8_t *buf = avpkt->data;
246 int buf_size = avpkt->size;
247 TTAContext *s = avctx->priv_data;
250 int cur_chan = 0, framelen = s->frame_length;
253 if (avctx->err_recognition & AV_EF_CRCCHECK) {
255 (tta_check_crc(s, buf, buf_size - 4) && avctx->err_recognition & AV_EF_EXPLODE))
256 return AVERROR_INVALIDDATA;
259 if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
262 /* get output buffer */
263 frame->nb_samples = framelen;
264 if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
267 // decode directly to output buffer for 24-bit sample format
269 s->decode_buffer = (int32_t *)frame->data[0];
271 // init per channel states
272 for (i = 0; i < s->channels; i++) {
273 TTAFilter *filter = &s->ch_ctx[i].filter;
274 s->ch_ctx[i].predictor = 0;
275 ff_tta_filter_init(filter, ff_tta_filter_configs[s->bps-1]);
276 if (s->format == FORMAT_ENCRYPTED) {
278 for (i = 0; i < 8; i++)
279 filter->qm[i] = sign_extend(s->crc_pass[i], 8);
281 ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
285 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
286 int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
287 TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
288 TTARice *rice = &s->ch_ctx[cur_chan].rice;
289 uint32_t unary, depth, k;
292 unary = get_unary(&gb, 0, get_bits_left(&gb));
303 if (get_bits_left(&gb) < k) {
304 ret = AVERROR_INVALIDDATA;
309 if (k > MIN_CACHE_BITS) {
310 ret = AVERROR_INVALIDDATA;
313 value = (unary << k) + get_bits(&gb, k);
317 // FIXME: copy paste from original
320 rice->sum1 += value - (rice->sum1 >> 4);
321 if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
323 else if(rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
325 value += ff_tta_shift_1[rice->k0];
327 rice->sum0 += value - (rice->sum0 >> 4);
328 if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
330 else if(rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
334 // extract coded value
335 *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
338 ttafilter_process(filter, p);
340 // fixed order prediction
341 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
343 case 1: *p += PRED(*predictor, 4); break;
345 case 3: *p += PRED(*predictor, 5); break;
346 case 4: *p += *predictor; break;
351 if (cur_chan < (s->channels-1))
354 // decorrelate in case of multiple channels
355 if (s->channels > 1) {
357 for (*p += *r / 2; r > p - s->channels; r--)
362 // check for last frame
363 if (i == s->last_frame_length && get_bits_left(&gb) / 8 == 4) {
364 frame->nb_samples = framelen = s->last_frame_length;
371 if (get_bits_left(&gb) < 32) {
372 ret = AVERROR_INVALIDDATA;
375 skip_bits_long(&gb, 32); // frame crc
377 // convert to output buffer
380 uint8_t *samples = (uint8_t *)frame->data[0];
381 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
382 *samples++ = *p + 0x80;
386 int16_t *samples = (int16_t *)frame->data[0];
387 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
392 // shift samples for 24-bit sample format
393 int32_t *samples = (int32_t *)frame->data[0];
394 for (i = 0; i < framelen * s->channels; i++)
396 // reset decode buffer
397 s->decode_buffer = NULL;
406 // reset decode buffer
408 s->decode_buffer = NULL;
412 static int init_thread_copy(AVCodecContext *avctx)
414 TTAContext *s = avctx->priv_data;
416 return allocate_buffers(avctx);
419 static av_cold int tta_decode_close(AVCodecContext *avctx) {
420 TTAContext *s = avctx->priv_data;
423 av_free(s->decode_buffer);
424 s->decode_buffer = NULL;
425 av_freep(&s->ch_ctx);
430 #define OFFSET(x) offsetof(TTAContext, x)
431 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
432 static const AVOption options[] = {
433 { "password", "Set decoding password", OFFSET(pass), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
437 static const AVClass tta_decoder_class = {
438 .class_name = "TTA Decoder",
439 .item_name = av_default_item_name,
441 .version = LIBAVUTIL_VERSION_INT,
444 AVCodec ff_tta_decoder = {
446 .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
447 .type = AVMEDIA_TYPE_AUDIO,
448 .id = AV_CODEC_ID_TTA,
449 .priv_data_size = sizeof(TTAContext),
450 .init = tta_decode_init,
451 .close = tta_decode_close,
452 .decode = tta_decode_frame,
453 .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
454 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
455 .priv_class = &tta_decoder_class,