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
39 #include "libavutil/crc.h"
40 #include "libavutil/intreadwrite.h"
41 #include "libavutil/opt.h"
43 #define FORMAT_SIMPLE 1
44 #define FORMAT_ENCRYPTED 2
46 typedef struct TTAContext
{
48 AVCodecContext
*avctx
;
49 const AVCRC
*crc_table
;
51 int format
, channels
, bps
;
53 int frame_length
, last_frame_length
;
55 int32_t *decode_buffer
;
63 static const int64_t tta_channel_layouts
[7] = {
65 AV_CH_LAYOUT_STEREO
|AV_CH_LOW_FREQUENCY
,
68 AV_CH_LAYOUT_5POINT1_BACK
,
69 AV_CH_LAYOUT_5POINT1_BACK
|AV_CH_BACK_CENTER
,
70 AV_CH_LAYOUT_7POINT1_WIDE
73 static int tta_check_crc(TTAContext
*s
, const uint8_t *buf
, int buf_size
)
77 CRC
= AV_RL32(buf
+ buf_size
);
78 crc
= av_crc(s
->crc_table
, 0xFFFFFFFFU
, buf
, buf_size
);
79 if (CRC
!= (crc
^ 0xFFFFFFFFU
)) {
80 av_log(s
->avctx
, AV_LOG_ERROR
, "CRC error\n");
81 return AVERROR_INVALIDDATA
;
87 static uint64_t tta_check_crc64(uint8_t *pass
)
89 uint64_t crc
= UINT64_MAX
, poly
= 0x42F0E1EBA9EA3693U
;
90 uint8_t *end
= pass
+ strlen(pass
);
94 crc
^= (uint64_t)*pass
++ << 56;
95 for (i
= 0; i
< 8; i
++)
96 crc
= (crc
<< 1) ^ (poly
& (((int64_t) crc
) >> 63));
99 return crc
^ UINT64_MAX
;
102 static int allocate_buffers(AVCodecContext
*avctx
)
104 TTAContext
*s
= avctx
->priv_data
;
107 s
->decode_buffer
= av_mallocz_array(sizeof(int32_t)*s
->frame_length
, s
->channels
);
108 if (!s
->decode_buffer
)
109 return AVERROR(ENOMEM
);
111 s
->decode_buffer
= NULL
;
112 s
->ch_ctx
= av_malloc_array(avctx
->channels
, sizeof(*s
->ch_ctx
));
114 av_freep(&s
->decode_buffer
);
115 return AVERROR(ENOMEM
);
121 static av_cold
int tta_decode_init(AVCodecContext
* avctx
)
123 TTAContext
*s
= avctx
->priv_data
;
129 // 30bytes includes TTA1 header
130 if (avctx
->extradata_size
< 22)
131 return AVERROR_INVALIDDATA
;
133 s
->crc_table
= av_crc_get_table(AV_CRC_32_IEEE_LE
);
134 init_get_bits8(&gb
, avctx
->extradata
, avctx
->extradata_size
);
135 if (show_bits_long(&gb
, 32) == AV_RL32("TTA1")) {
137 skip_bits_long(&gb
, 32);
139 s
->format
= get_bits(&gb
, 16);
141 av_log(avctx
, AV_LOG_ERROR
, "Invalid format\n");
142 return AVERROR_INVALIDDATA
;
144 if (s
->format
== FORMAT_ENCRYPTED
) {
146 av_log(avctx
, AV_LOG_ERROR
, "Missing password for encrypted stream. Please use the -password option\n");
147 return AVERROR(EINVAL
);
149 AV_WL64(s
->crc_pass
, tta_check_crc64(s
->pass
));
151 avctx
->channels
= s
->channels
= get_bits(&gb
, 16);
152 if (s
->channels
> 1 && s
->channels
< 9)
153 avctx
->channel_layout
= tta_channel_layouts
[s
->channels
-2];
154 avctx
->bits_per_raw_sample
= get_bits(&gb
, 16);
155 s
->bps
= (avctx
->bits_per_raw_sample
+ 7) / 8;
156 avctx
->sample_rate
= get_bits_long(&gb
, 32);
157 s
->data_length
= get_bits_long(&gb
, 32);
158 skip_bits_long(&gb
, 32); // CRC32 of header
160 if (s
->channels
== 0) {
161 av_log(avctx
, AV_LOG_ERROR
, "Invalid number of channels\n");
162 return AVERROR_INVALIDDATA
;
163 } else if (avctx
->sample_rate
== 0) {
164 av_log(avctx
, AV_LOG_ERROR
, "Invalid samplerate\n");
165 return AVERROR_INVALIDDATA
;
169 case 1: avctx
->sample_fmt
= AV_SAMPLE_FMT_U8
; break;
171 avctx
->sample_fmt
= AV_SAMPLE_FMT_S16
;
174 avctx
->sample_fmt
= AV_SAMPLE_FMT_S32
;
176 //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
178 av_log(avctx
, AV_LOG_ERROR
, "Invalid/unsupported sample format.\n");
179 return AVERROR_INVALIDDATA
;
183 if (avctx
->sample_rate
> 0x7FFFFFu
) {
184 av_log(avctx
, AV_LOG_ERROR
, "sample_rate too large\n");
185 return AVERROR(EINVAL
);
187 s
->frame_length
= 256 * avctx
->sample_rate
/ 245;
189 s
->last_frame_length
= s
->data_length
% s
->frame_length
;
190 total_frames
= s
->data_length
/ s
->frame_length
+
191 (s
->last_frame_length
? 1 : 0);
193 av_log(avctx
, AV_LOG_DEBUG
, "format: %d chans: %d bps: %d rate: %d block: %d\n",
194 s
->format
, avctx
->channels
, avctx
->bits_per_coded_sample
, avctx
->sample_rate
,
196 av_log(avctx
, AV_LOG_DEBUG
, "data_length: %d frame_length: %d last: %d total: %d\n",
197 s
->data_length
, s
->frame_length
, s
->last_frame_length
, total_frames
);
199 if(s
->frame_length
>= UINT_MAX
/ (s
->channels
* sizeof(int32_t))){
200 av_log(avctx
, AV_LOG_ERROR
, "frame_length too large\n");
201 return AVERROR_INVALIDDATA
;
204 av_log(avctx
, AV_LOG_ERROR
, "Wrong extradata present\n");
205 return AVERROR_INVALIDDATA
;
208 ff_ttadsp_init(&s
->dsp
);
210 return allocate_buffers(avctx
);
213 static int tta_decode_frame(AVCodecContext
*avctx
, void *data
,
214 int *got_frame_ptr
, AVPacket
*avpkt
)
216 AVFrame
*frame
= data
;
217 ThreadFrame tframe
= { .f
= data
};
218 const uint8_t *buf
= avpkt
->data
;
219 int buf_size
= avpkt
->size
;
220 TTAContext
*s
= avctx
->priv_data
;
223 int cur_chan
= 0, framelen
= s
->frame_length
;
226 if (avctx
->err_recognition
& AV_EF_CRCCHECK
) {
228 (tta_check_crc(s
, buf
, buf_size
- 4) && avctx
->err_recognition
& AV_EF_EXPLODE
))
229 return AVERROR_INVALIDDATA
;
232 if ((ret
= init_get_bits8(&gb
, avpkt
->data
, avpkt
->size
)) < 0)
235 /* get output buffer */
236 frame
->nb_samples
= framelen
;
237 if ((ret
= ff_thread_get_buffer(avctx
, &tframe
, 0)) < 0)
240 // decode directly to output buffer for 24-bit sample format
242 s
->decode_buffer
= (int32_t *)frame
->data
[0];
244 // init per channel states
245 for (i
= 0; i
< s
->channels
; i
++) {
246 TTAFilter
*filter
= &s
->ch_ctx
[i
].filter
;
247 s
->ch_ctx
[i
].predictor
= 0;
248 ff_tta_filter_init(filter
, ff_tta_filter_configs
[s
->bps
-1]);
249 if (s
->format
== FORMAT_ENCRYPTED
) {
251 for (i
= 0; i
< 8; i
++)
252 filter
->qm
[i
] = sign_extend(s
->crc_pass
[i
], 8);
254 ff_tta_rice_init(&s
->ch_ctx
[i
].rice
, 10, 10);
258 for (p
= s
->decode_buffer
; p
< s
->decode_buffer
+ (framelen
* s
->channels
); p
++) {
259 int32_t *predictor
= &s
->ch_ctx
[cur_chan
].predictor
;
260 TTAFilter
*filter
= &s
->ch_ctx
[cur_chan
].filter
;
261 TTARice
*rice
= &s
->ch_ctx
[cur_chan
].rice
;
262 uint32_t unary
, depth
, k
;
265 unary
= get_unary(&gb
, 0, get_bits_left(&gb
));
276 if (get_bits_left(&gb
) < k
) {
277 ret
= AVERROR_INVALIDDATA
;
282 if (k
> MIN_CACHE_BITS
) {
283 ret
= AVERROR_INVALIDDATA
;
286 value
= (unary
<< k
) + get_bits(&gb
, k
);
290 // FIXME: copy paste from original
293 rice
->sum1
+= value
- (rice
->sum1
>> 4);
294 if (rice
->k1
> 0 && rice
->sum1
< ff_tta_shift_16
[rice
->k1
])
296 else if(rice
->sum1
> ff_tta_shift_16
[rice
->k1
+ 1])
298 value
+= ff_tta_shift_1
[rice
->k0
];
300 rice
->sum0
+= value
- (rice
->sum0
>> 4);
301 if (rice
->k0
> 0 && rice
->sum0
< ff_tta_shift_16
[rice
->k0
])
303 else if(rice
->sum0
> ff_tta_shift_16
[rice
->k0
+ 1])
307 // extract coded value
308 *p
= 1 + ((value
>> 1) ^ ((value
& 1) - 1));
311 s
->dsp
.ttafilter_process_dec(filter
->qm
, filter
->dx
, filter
->dl
, &filter
->error
, p
,
312 filter
->shift
, filter
->round
);
314 // fixed order prediction
315 #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
317 case 1: *p
+= PRED(*predictor
, 4); break;
319 case 3: *p
+= PRED(*predictor
, 5); break;
320 case 4: *p
+= *predictor
; break;
325 if (cur_chan
< (s
->channels
-1))
328 // decorrelate in case of multiple channels
329 if (s
->channels
> 1) {
331 for (*p
+= *r
/ 2; r
> p
- s
->channels
; r
--)
336 // check for last frame
337 if (i
== s
->last_frame_length
&& get_bits_left(&gb
) / 8 == 4) {
338 frame
->nb_samples
= framelen
= s
->last_frame_length
;
345 if (get_bits_left(&gb
) < 32) {
346 ret
= AVERROR_INVALIDDATA
;
349 skip_bits_long(&gb
, 32); // frame crc
351 // convert to output buffer
354 uint8_t *samples
= (uint8_t *)frame
->data
[0];
355 for (p
= s
->decode_buffer
; p
< s
->decode_buffer
+ (framelen
* s
->channels
); p
++)
356 *samples
++ = *p
+ 0x80;
360 int16_t *samples
= (int16_t *)frame
->data
[0];
361 for (p
= s
->decode_buffer
; p
< s
->decode_buffer
+ (framelen
* s
->channels
); p
++)
366 // shift samples for 24-bit sample format
367 int32_t *samples
= (int32_t *)frame
->data
[0];
368 for (i
= 0; i
< framelen
* s
->channels
; i
++)
370 // reset decode buffer
371 s
->decode_buffer
= NULL
;
380 // reset decode buffer
382 s
->decode_buffer
= NULL
;
386 static int init_thread_copy(AVCodecContext
*avctx
)
388 TTAContext
*s
= avctx
->priv_data
;
390 return allocate_buffers(avctx
);
393 static av_cold
int tta_decode_close(AVCodecContext
*avctx
) {
394 TTAContext
*s
= avctx
->priv_data
;
397 av_freep(&s
->decode_buffer
);
398 s
->decode_buffer
= NULL
;
399 av_freep(&s
->ch_ctx
);
404 #define OFFSET(x) offsetof(TTAContext, x)
405 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
406 static const AVOption options
[] = {
407 { "password", "Set decoding password", OFFSET(pass
), AV_OPT_TYPE_STRING
, { .str
= NULL
}, 0, 0, DEC
},
411 static const AVClass tta_decoder_class
= {
412 .class_name
= "TTA Decoder",
413 .item_name
= av_default_item_name
,
415 .version
= LIBAVUTIL_VERSION_INT
,
418 AVCodec ff_tta_decoder
= {
420 .long_name
= NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
421 .type
= AVMEDIA_TYPE_AUDIO
,
422 .id
= AV_CODEC_ID_TTA
,
423 .priv_data_size
= sizeof(TTAContext
),
424 .init
= tta_decode_init
,
425 .close
= tta_decode_close
,
426 .decode
= tta_decode_frame
,
427 .init_thread_copy
= ONLY_IF_THREADS_ENABLED(init_thread_copy
),
428 .capabilities
= CODEC_CAP_DR1
| CODEC_CAP_FRAME_THREADS
,
429 .priv_class
= &tta_decoder_class
,