FFmpeg coverage


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