FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tta.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 151 207 72.9%
Functions: 5 6 83.3%
Branches: 78 122 63.9%

Line Branch Exec Source
1 /*
2 * TTA (The Lossless True Audio) decoder
3 * Copyright (c) 2006 Alex Beregszaszi
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * TTA (The Lossless True Audio) decoder
25 * @see http://www.true-audio.com/
26 * @see http://tta.corecodec.org/
27 * @author Alex Beregszaszi
28 */
29
30 #include <limits.h>
31
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/crc.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/opt.h"
36
37 #define BITSTREAM_READER_LE
38 #include "ttadata.h"
39 #include "ttadsp.h"
40 #include "avcodec.h"
41 #include "codec_internal.h"
42 #include "get_bits.h"
43 #include "thread.h"
44 #include "unary.h"
45
46 #define FORMAT_SIMPLE 1
47 #define FORMAT_ENCRYPTED 2
48
49 typedef struct TTAContext {
50 AVClass *class;
51 AVCodecContext *avctx;
52 const AVCRC *crc_table;
53
54 int format, channels, bps;
55 unsigned data_length;
56 int frame_length, last_frame_length;
57
58 int32_t *decode_buffer;
59
60 uint8_t crc_pass[8];
61 uint8_t *pass;
62 TTAChannel *ch_ctx;
63 TTADSPContext dsp;
64 } TTAContext;
65
66 static const int64_t tta_channel_layouts[7] = {
67 AV_CH_LAYOUT_STEREO,
68 AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
69 AV_CH_LAYOUT_QUAD,
70 0,
71 AV_CH_LAYOUT_5POINT1_BACK,
72 AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
73 AV_CH_LAYOUT_7POINT1_WIDE
74 };
75
76 static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
77 {
78 uint32_t crc, CRC;
79
80 CRC = AV_RL32(buf + buf_size);
81 crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
82 if (CRC != (crc ^ 0xFFFFFFFFU)) {
83 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
84 return AVERROR_INVALIDDATA;
85 }
86
87 return 0;
88 }
89
90 2 static uint64_t tta_check_crc64(uint8_t *pass)
91 {
92 2 uint64_t crc = UINT64_MAX, poly = 0x42F0E1EBA9EA3693U;
93 2 uint8_t *end = pass + strlen(pass);
94 int i;
95
96
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 while (pass < end) {
97 12 crc ^= (uint64_t)*pass++ << 56;
98
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 12 times.
108 for (i = 0; i < 8; i++)
99 96 crc = (crc << 1) ^ (poly & (((int64_t) crc) >> 63));
100 }
101
102 2 return crc ^ UINT64_MAX;
103 }
104
105 24 static int allocate_buffers(AVCodecContext *avctx)
106 {
107 24 TTAContext *s = avctx->priv_data;
108
109
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (s->bps < 3) {
110 48 s->decode_buffer = av_calloc(s->frame_length,
111 24 sizeof(*s->decode_buffer) * s->channels);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!s->decode_buffer)
113 return AVERROR(ENOMEM);
114 } else
115 s->decode_buffer = NULL;
116 24 s->ch_ctx = av_malloc_array(avctx->ch_layout.nb_channels, sizeof(*s->ch_ctx));
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!s->ch_ctx)
118 return AVERROR(ENOMEM);
119
120 24 return 0;
121 }
122
123 24 static av_cold int tta_decode_init(AVCodecContext * avctx)
124 {
125 24 TTAContext *s = avctx->priv_data;
126 GetBitContext gb;
127 int total_frames;
128 int ret;
129
130 24 s->avctx = avctx;
131
132 // 22 bytes for a TTA1 header
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (avctx->extradata_size < 22)
134 return AVERROR_INVALIDDATA;
135
136 24 s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
137 24 ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size);
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
139 return ret;
140
141
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 if (show_bits_long(&gb, 32) == AV_RL32("TTA1")) {
142 /* signature */
143 24 skip_bits_long(&gb, 32);
144
145 24 s->format = get_bits(&gb, 16);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (s->format > 2) {
147 av_log(avctx, AV_LOG_ERROR, "Invalid format\n");
148 return AVERROR_INVALIDDATA;
149 }
150
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 22 times.
24 if (s->format == FORMAT_ENCRYPTED) {
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->pass) {
152 av_log(avctx, AV_LOG_ERROR, "Missing password for encrypted stream. Please use the -password option\n");
153 return AVERROR(EINVAL);
154 }
155 2 AV_WL64(s->crc_pass, tta_check_crc64(s->pass));
156 }
157
158 24 s->channels = get_bits(&gb, 16);
159
160 24 av_channel_layout_uninit(&avctx->ch_layout);
161
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
24 if (s->channels > 1 && s->channels < 9) {
162 20 av_channel_layout_from_mask(&avctx->ch_layout, tta_channel_layouts[s->channels-2]);
163 }
164
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 20 times.
24 if (avctx->ch_layout.nb_channels == 0) {
165 4 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
166 4 avctx->ch_layout.nb_channels = s->channels;
167 }
168
169 24 avctx->bits_per_raw_sample = get_bits(&gb, 16);
170 24 s->bps = (avctx->bits_per_raw_sample + 7) / 8;
171 24 avctx->sample_rate = get_bits_long(&gb, 32);
172 24 s->data_length = get_bits_long(&gb, 32);
173 24 skip_bits_long(&gb, 32); // CRC32 of header
174
175
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
24 if (s->channels == 0 || s->channels > 16) {
176 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
177 return AVERROR_INVALIDDATA;
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 } else if (avctx->sample_rate == 0) {
179 av_log(avctx, AV_LOG_ERROR, "Invalid samplerate\n");
180 return AVERROR_INVALIDDATA;
181 }
182
183
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 switch(s->bps) {
184 case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
185 24 case 2:
186 24 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
187 24 break;
188 case 3:
189 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
190 break;
191 //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
192 default:
193 av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
194 return AVERROR_INVALIDDATA;
195 }
196
197 // prevent overflow
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (avctx->sample_rate > 0x7FFFFFu) {
199 av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
200 return AVERROR(EINVAL);
201 }
202 24 s->frame_length = 256 * avctx->sample_rate / 245;
203
204 24 s->last_frame_length = s->data_length % s->frame_length;
205 48 total_frames = s->data_length / s->frame_length +
206
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 (s->last_frame_length ? 1 : 0);
207
208 24 av_log(avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
209 s->format, avctx->ch_layout.nb_channels, avctx->bits_per_coded_sample, avctx->sample_rate,
210 avctx->block_align);
211 24 av_log(avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
212 s->data_length, s->frame_length, s->last_frame_length, total_frames);
213
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))) {
215 av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
216 return AVERROR_INVALIDDATA;
217 }
218 } else {
219 av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
220 return AVERROR_INVALIDDATA;
221 }
222
223 24 ff_ttadsp_init(&s->dsp);
224
225 24 return allocate_buffers(avctx);
226 }
227
228 56 static int tta_decode_frame(AVCodecContext *avctx, AVFrame *frame,
229 int *got_frame_ptr, AVPacket *avpkt)
230 {
231 56 const uint8_t *buf = avpkt->data;
232 56 int buf_size = avpkt->size;
233 56 TTAContext *s = avctx->priv_data;
234 GetBitContext gb;
235 int i, ret;
236 56 int cur_chan = 0, framelen = s->frame_length;
237 uint32_t *p;
238
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (avctx->err_recognition & AV_EF_CRCCHECK) {
240 if (buf_size < 4 ||
241 (tta_check_crc(s, buf, buf_size - 4) && avctx->err_recognition & AV_EF_EXPLODE))
242 return AVERROR_INVALIDDATA;
243 }
244
245
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
56 if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
246 return ret;
247
248 /* get output buffer */
249 56 frame->nb_samples = framelen;
250
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
56 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
251 return ret;
252
253 // decode directly to output buffer for 24-bit sample format
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (s->bps == 3)
255 s->decode_buffer = (int32_t *)frame->data[0];
256
257 // init per channel states
258
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 56 times.
164 for (i = 0; i < s->channels; i++) {
259 108 TTAFilter *filter = &s->ch_ctx[i].filter;
260 108 s->ch_ctx[i].predictor = 0;
261 108 ff_tta_filter_init(filter, ff_tta_filter_configs[s->bps-1]);
262
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 90 times.
108 if (s->format == FORMAT_ENCRYPTED) {
263 int i;
264
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 18 times.
162 for (i = 0; i < 8; i++)
265 144 filter->qm[i] = sign_extend(s->crc_pass[i], 8);
266 }
267 108 ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
268 }
269
270 56 i = 0;
271
2/2
✓ Branch 0 taken 4609174 times.
✓ Branch 1 taken 38 times.
4609212 for (p = s->decode_buffer; (int32_t*)p < s->decode_buffer + (framelen * s->channels); p++) {
272 4609174 int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
273 4609174 TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
274 4609174 TTARice *rice = &s->ch_ctx[cur_chan].rice;
275 uint32_t unary, depth, k;
276 int32_t value;
277
278 4609174 unary = get_unary(&gb, 0, get_bits_left(&gb));
279
280
2/2
✓ Branch 0 taken 2172729 times.
✓ Branch 1 taken 2436445 times.
4609174 if (unary == 0) {
281 2172729 depth = 0;
282 2172729 k = rice->k0;
283 } else {
284 2436445 depth = 1;
285 2436445 k = rice->k1;
286 2436445 unary--;
287 }
288
289
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 4609168 times.
4609174 if (get_bits_left(&gb) < k) {
290 6 ret = AVERROR_INVALIDDATA;
291 6 goto error;
292 }
293
294
2/2
✓ Branch 0 taken 4028286 times.
✓ Branch 1 taken 580882 times.
4609168 if (k) {
295
3/4
✓ Branch 0 taken 4028282 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4028282 times.
4028286 if (k > MIN_CACHE_BITS || unary > INT32_MAX >> k) {
296 4 ret = AVERROR_INVALIDDATA;
297 4 goto error;
298 }
299 4028282 value = (unary << k) + get_bits(&gb, k);
300 } else
301 580882 value = unary;
302
303 // FIXME: copy paste from original
304
2/2
✓ Branch 0 taken 2436441 times.
✓ Branch 1 taken 2172723 times.
4609164 switch (depth) {
305 2436441 case 1:
306 2436441 rice->sum1 += value - (rice->sum1 >> 4);
307
4/4
✓ Branch 0 taken 2219597 times.
✓ Branch 1 taken 216844 times.
✓ Branch 2 taken 59651 times.
✓ Branch 3 taken 2159946 times.
2436441 if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
308 59651 rice->k1--;
309
2/2
✓ Branch 0 taken 59314 times.
✓ Branch 1 taken 2317476 times.
2376790 else if (rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
310 59314 rice->k1++;
311 2436441 value += ff_tta_shift_1[rice->k0];
312 4609164 default:
313 4609164 rice->sum0 += value - (rice->sum0 >> 4);
314
4/4
✓ Branch 0 taken 4056862 times.
✓ Branch 1 taken 552302 times.
✓ Branch 2 taken 121972 times.
✓ Branch 3 taken 3934890 times.
4609164 if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
315 121972 rice->k0--;
316
2/2
✓ Branch 0 taken 121637 times.
✓ Branch 1 taken 4365555 times.
4487192 else if (rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
317 121637 rice->k0++;
318 }
319
320 // extract coded value
321 4609164 *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
322
323 // run hybrid filter
324 4609164 s->dsp.filter_process(filter->qm, filter->dx, filter->dl, &filter->error, p,
325 filter->shift, filter->round);
326
327 // fixed order prediction
328 #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
329
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4609164 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4609164 switch (s->bps) {
330 case 1: *p += PRED(*predictor, 4); break;
331 4609164 case 2:
332 4609164 case 3: *p += PRED(*predictor, 5); break;
333 case 4: *p += *predictor; break;
334 }
335 4609164 *predictor = *p;
336
337 // flip channels
338
2/2
✓ Branch 0 taken 2216386 times.
✓ Branch 1 taken 2392778 times.
4609164 if (cur_chan < (s->channels-1))
339 2216386 cur_chan++;
340 else {
341 // decorrelate in case of multiple channels
342
2/2
✓ Branch 0 taken 2216378 times.
✓ Branch 1 taken 176400 times.
2392778 if (s->channels > 1) {
343 2216378 int32_t *r = p - 1;
344
2/2
✓ Branch 0 taken 2216378 times.
✓ Branch 1 taken 2216378 times.
4432756 for (*p += *r / 2; r > (int32_t*)p - s->channels; r--)
345 2216378 *r = *(r + 1) - (unsigned)*r;
346 }
347 2392778 cur_chan = 0;
348 2392778 i++;
349 // check for last frame
350
4/4
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 2392732 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 38 times.
2392778 if (i == s->last_frame_length && get_bits_left(&gb) / 8 == 4) {
351 8 frame->nb_samples = framelen = s->last_frame_length;
352 8 break;
353 }
354 }
355 }
356
357 46 align_get_bits(&gb);
358
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if (get_bits_left(&gb) < 32) {
359 ret = AVERROR_INVALIDDATA;
360 goto error;
361 }
362 46 skip_bits_long(&gb, 32); // frame crc
363
364 // convert to output buffer
365
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
46 switch (s->bps) {
366 case 1: {
367 uint8_t *samples = (uint8_t *)frame->data[0];
368 p = s->decode_buffer;
369 for (i = 0; i < framelen * s->channels; i++)
370 samples[i] = p[i] + 0x80;
371 break;
372 }
373 46 case 2: {
374 46 int16_t *samples = (int16_t *)frame->data[0];
375 46 p = s->decode_buffer;
376
2/2
✓ Branch 0 taken 3824108 times.
✓ Branch 1 taken 46 times.
3824154 for (i = 0; i < framelen * s->channels; i++)
377 3824108 samples[i] = p[i];
378 46 break;
379 }
380 case 3: {
381 // shift samples for 24-bit sample format
382 int32_t *samples = (int32_t *)frame->data[0];
383
384 for (i = 0; i < framelen * s->channels; i++)
385 samples[i] = samples[i] * 256U;
386 // reset decode buffer
387 s->decode_buffer = NULL;
388 break;
389 }
390 }
391
392 46 *got_frame_ptr = 1;
393
394 46 return buf_size;
395 10 error:
396 // reset decode buffer
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (s->bps == 3)
398 s->decode_buffer = NULL;
399 10 return ret;
400 }
401
402 24 static av_cold int tta_decode_close(AVCodecContext *avctx)
403 {
404 24 TTAContext *s = avctx->priv_data;
405
406
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (s->bps < 3)
407 24 av_freep(&s->decode_buffer);
408 24 s->decode_buffer = NULL;
409 24 av_freep(&s->ch_ctx);
410
411 24 return 0;
412 }
413
414 #define OFFSET(x) offsetof(TTAContext, x)
415 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
416 static const AVOption options[] = {
417 { "password", "Set decoding password", OFFSET(pass), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
418 { NULL },
419 };
420
421 static const AVClass tta_decoder_class = {
422 .class_name = "TTA Decoder",
423 .item_name = av_default_item_name,
424 .option = options,
425 .version = LIBAVUTIL_VERSION_INT,
426 };
427
428 const FFCodec ff_tta_decoder = {
429 .p.name = "tta",
430 CODEC_LONG_NAME("TTA (True Audio)"),
431 .p.type = AVMEDIA_TYPE_AUDIO,
432 .p.id = AV_CODEC_ID_TTA,
433 .priv_data_size = sizeof(TTAContext),
434 .init = tta_decode_init,
435 .close = tta_decode_close,
436 FF_CODEC_DECODE_CB(tta_decode_frame),
437 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_CHANNEL_CONF,
438 .p.priv_class = &tta_decoder_class,
439 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
440 };
441