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
32 #include "libavutil/crc.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/opt.h"
36 #define BITSTREAM_READER_LE
45 #define FORMAT_SIMPLE 1
46 #define FORMAT_ENCRYPTED 2
48 typedef struct TTAContext {
50 AVCodecContext *avctx;
51 const AVCRC *crc_table;
53 int format, channels, bps;
55 int frame_length, last_frame_length;
57 int32_t *decode_buffer;
65 static const int64_t tta_channel_layouts[7] = {
67 AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
70 AV_CH_LAYOUT_5POINT1_BACK,
71 AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
72 AV_CH_LAYOUT_7POINT1_WIDE
75 static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
79 CRC = AV_RL32(buf + buf_size);
80 crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
81 if (CRC != (crc ^ 0xFFFFFFFFU)) {
82 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
83 return AVERROR_INVALIDDATA;
89 static uint64_t tta_check_crc64(uint8_t *pass)
91 uint64_t crc = UINT64_MAX, poly = 0x42F0E1EBA9EA3693U;
92 uint8_t *end = pass + strlen(pass);
96 crc ^= (uint64_t)*pass++ << 56;
97 for (i = 0; i < 8; i++)
98 crc = (crc << 1) ^ (poly & (((int64_t) crc) >> 63));
101 return crc ^ UINT64_MAX;
104 static int allocate_buffers(AVCodecContext *avctx)
106 TTAContext *s = avctx->priv_data;
109 s->decode_buffer = av_mallocz_array(sizeof(int32_t)*s->frame_length, s->channels);
110 if (!s->decode_buffer)
111 return AVERROR(ENOMEM);
113 s->decode_buffer = NULL;
114 s->ch_ctx = av_malloc_array(avctx->channels, sizeof(*s->ch_ctx));
116 av_freep(&s->decode_buffer);
117 return AVERROR(ENOMEM);
123 static av_cold int tta_decode_init(AVCodecContext * avctx)
125 TTAContext *s = avctx->priv_data;
132 // 30bytes includes TTA1 header
133 if (avctx->extradata_size < 22)
134 return AVERROR_INVALIDDATA;
136 s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
137 ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size);
141 if (show_bits_long(&gb, 32) == AV_RL32("TTA1")) {
143 skip_bits_long(&gb, 32);
145 s->format = get_bits(&gb, 16);
147 av_log(avctx, AV_LOG_ERROR, "Invalid format\n");
148 return AVERROR_INVALIDDATA;
150 if (s->format == FORMAT_ENCRYPTED) {
152 av_log(avctx, AV_LOG_ERROR, "Missing password for encrypted stream. Please use the -password option\n");
153 return AVERROR(EINVAL);
155 AV_WL64(s->crc_pass, tta_check_crc64(s->pass));
157 avctx->channels = s->channels = get_bits(&gb, 16);
158 if (s->channels > 1 && s->channels < 9)
159 avctx->channel_layout = tta_channel_layouts[s->channels-2];
160 avctx->bits_per_raw_sample = get_bits(&gb, 16);
161 s->bps = (avctx->bits_per_raw_sample + 7) / 8;
162 avctx->sample_rate = get_bits_long(&gb, 32);
163 s->data_length = get_bits_long(&gb, 32);
164 skip_bits_long(&gb, 32); // CRC32 of header
166 if (s->channels == 0) {
167 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
168 return AVERROR_INVALIDDATA;
169 } else if (avctx->sample_rate == 0) {
170 av_log(avctx, AV_LOG_ERROR, "Invalid samplerate\n");
171 return AVERROR_INVALIDDATA;
175 case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
177 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
180 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
182 //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
184 av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
185 return AVERROR_INVALIDDATA;
189 if (avctx->sample_rate > 0x7FFFFFu) {
190 av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
191 return AVERROR(EINVAL);
193 s->frame_length = 256 * avctx->sample_rate / 245;
195 s->last_frame_length = s->data_length % s->frame_length;
196 total_frames = s->data_length / s->frame_length +
197 (s->last_frame_length ? 1 : 0);
199 av_log(avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
200 s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
202 av_log(avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
203 s->data_length, s->frame_length, s->last_frame_length, total_frames);
205 if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
206 av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
207 return AVERROR_INVALIDDATA;
210 av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
211 return AVERROR_INVALIDDATA;
214 ff_ttadsp_init(&s->dsp);
216 return allocate_buffers(avctx);
219 static int tta_decode_frame(AVCodecContext *avctx, void *data,
220 int *got_frame_ptr, AVPacket *avpkt)
222 AVFrame *frame = data;
223 ThreadFrame tframe = { .f = data };
224 const uint8_t *buf = avpkt->data;
225 int buf_size = avpkt->size;
226 TTAContext *s = avctx->priv_data;
229 int cur_chan = 0, framelen = s->frame_length;
232 if (avctx->err_recognition & AV_EF_CRCCHECK) {
234 (tta_check_crc(s, buf, buf_size - 4) && avctx->err_recognition & AV_EF_EXPLODE))
235 return AVERROR_INVALIDDATA;
238 if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
241 /* get output buffer */
242 frame->nb_samples = framelen;
243 if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
246 // decode directly to output buffer for 24-bit sample format
248 s->decode_buffer = (int32_t *)frame->data[0];
250 // init per channel states
251 for (i = 0; i < s->channels; i++) {
252 TTAFilter *filter = &s->ch_ctx[i].filter;
253 s->ch_ctx[i].predictor = 0;
254 ff_tta_filter_init(filter, ff_tta_filter_configs[s->bps-1]);
255 if (s->format == FORMAT_ENCRYPTED) {
257 for (i = 0; i < 8; i++)
258 filter->qm[i] = sign_extend(s->crc_pass[i], 8);
260 ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
264 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
265 int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
266 TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
267 TTARice *rice = &s->ch_ctx[cur_chan].rice;
268 uint32_t unary, depth, k;
271 unary = get_unary(&gb, 0, get_bits_left(&gb));
282 if (get_bits_left(&gb) < k) {
283 ret = AVERROR_INVALIDDATA;
288 if (k > MIN_CACHE_BITS || unary > INT32_MAX >> k) {
289 ret = AVERROR_INVALIDDATA;
292 value = (unary << k) + get_bits(&gb, k);
296 // FIXME: copy paste from original
299 rice->sum1 += value - (rice->sum1 >> 4);
300 if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
302 else if(rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
304 value += ff_tta_shift_1[rice->k0];
306 rice->sum0 += value - (rice->sum0 >> 4);
307 if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
309 else if(rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
313 // extract coded value
314 *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
317 s->dsp.filter_process(filter->qm, filter->dx, filter->dl, &filter->error, p,
318 filter->shift, filter->round);
320 // fixed order prediction
321 #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
323 case 1: *p += PRED(*predictor, 4); break;
325 case 3: *p += PRED(*predictor, 5); break;
326 case 4: *p += *predictor; break;
331 if (cur_chan < (s->channels-1))
334 // decorrelate in case of multiple channels
335 if (s->channels > 1) {
337 for (*p += *r / 2; r > p - s->channels; r--)
342 // check for last frame
343 if (i == s->last_frame_length && get_bits_left(&gb) / 8 == 4) {
344 frame->nb_samples = framelen = s->last_frame_length;
351 if (get_bits_left(&gb) < 32) {
352 ret = AVERROR_INVALIDDATA;
355 skip_bits_long(&gb, 32); // frame crc
357 // convert to output buffer
360 uint8_t *samples = (uint8_t *)frame->data[0];
361 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
362 *samples++ = *p + 0x80;
366 int16_t *samples = (int16_t *)frame->data[0];
367 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
372 // shift samples for 24-bit sample format
373 int32_t *samples = (int32_t *)frame->data[0];
374 for (i = 0; i < framelen * s->channels; i++)
376 // reset decode buffer
377 s->decode_buffer = NULL;
386 // reset decode buffer
388 s->decode_buffer = NULL;
392 static int init_thread_copy(AVCodecContext *avctx)
394 TTAContext *s = avctx->priv_data;
396 return allocate_buffers(avctx);
399 static av_cold int tta_decode_close(AVCodecContext *avctx) {
400 TTAContext *s = avctx->priv_data;
403 av_freep(&s->decode_buffer);
404 s->decode_buffer = NULL;
405 av_freep(&s->ch_ctx);
410 #define OFFSET(x) offsetof(TTAContext, x)
411 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
412 static const AVOption options[] = {
413 { "password", "Set decoding password", OFFSET(pass), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
417 static const AVClass tta_decoder_class = {
418 .class_name = "TTA Decoder",
419 .item_name = av_default_item_name,
421 .version = LIBAVUTIL_VERSION_INT,
424 AVCodec ff_tta_decoder = {
426 .long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
427 .type = AVMEDIA_TYPE_AUDIO,
428 .id = AV_CODEC_ID_TTA,
429 .priv_data_size = sizeof(TTAContext),
430 .init = tta_decode_init,
431 .close = tta_decode_close,
432 .decode = tta_decode_frame,
433 .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
434 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
435 .priv_class = &tta_decoder_class,