FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tta.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 143 206 69.4%
Functions: 5 6 83.3%
Branches: 73 120 60.8%

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 20 static int allocate_buffers(AVCodecContext *avctx)
106 {
107 20 TTAContext *s = avctx->priv_data;
108
109
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (s->bps < 3) {
110 40 s->decode_buffer = av_calloc(s->frame_length,
111 20 sizeof(*s->decode_buffer) * s->channels);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!s->decode_buffer)
113 return AVERROR(ENOMEM);
114 } else
115 s->decode_buffer = NULL;
116 20 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 20 times.
20 if (!s->ch_ctx)
118 return AVERROR(ENOMEM);
119
120 20 return 0;
121 }
122
123 20 static av_cold int tta_decode_init(AVCodecContext * avctx)
124 {
125 20 TTAContext *s = avctx->priv_data;
126 GetBitContext gb;
127 int total_frames;
128 int ret;
129
130 20 s->avctx = avctx;
131
132 // 22 bytes for a TTA1 header
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (avctx->extradata_size < 22)
134 return AVERROR_INVALIDDATA;
135
136 20 s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
137 20 ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size);
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
139 return ret;
140
141
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 if (show_bits_long(&gb, 32) == AV_RL32("TTA1")) {
142 /* signature */
143 20 skip_bits_long(&gb, 32);
144
145 20 s->format = get_bits(&gb, 16);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 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 18 times.
20 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 20 s->channels = get_bits(&gb, 16);
159
160 20 av_channel_layout_uninit(&avctx->ch_layout);
161
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
20 if (s->channels > 1 && s->channels < 9) {
162 16 av_channel_layout_from_mask(&avctx->ch_layout, tta_channel_layouts[s->channels-2]);
163 } else {
164 4 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
165 4 avctx->ch_layout.nb_channels = s->channels;
166 }
167
168 20 avctx->bits_per_raw_sample = get_bits(&gb, 16);
169 20 s->bps = (avctx->bits_per_raw_sample + 7) / 8;
170 20 avctx->sample_rate = get_bits_long(&gb, 32);
171 20 s->data_length = get_bits_long(&gb, 32);
172 20 skip_bits_long(&gb, 32); // CRC32 of header
173
174
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 if (s->channels == 0 || s->channels > 16) {
175 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
176 return AVERROR_INVALIDDATA;
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 } else if (avctx->sample_rate == 0) {
178 av_log(avctx, AV_LOG_ERROR, "Invalid samplerate\n");
179 return AVERROR_INVALIDDATA;
180 }
181
182
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20 switch(s->bps) {
183 case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
184 20 case 2:
185 20 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
186 20 break;
187 case 3:
188 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
189 break;
190 //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
191 default:
192 av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
193 return AVERROR_INVALIDDATA;
194 }
195
196 // prevent overflow
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (avctx->sample_rate > 0x7FFFFFu) {
198 av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
199 return AVERROR(EINVAL);
200 }
201 20 s->frame_length = 256 * avctx->sample_rate / 245;
202
203 20 s->last_frame_length = s->data_length % s->frame_length;
204 40 total_frames = s->data_length / s->frame_length +
205
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 (s->last_frame_length ? 1 : 0);
206
207 20 av_log(avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
208 s->format, avctx->ch_layout.nb_channels, avctx->bits_per_coded_sample, avctx->sample_rate,
209 avctx->block_align);
210 20 av_log(avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
211 s->data_length, s->frame_length, s->last_frame_length, total_frames);
212
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
214 av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
215 return AVERROR_INVALIDDATA;
216 }
217 } else {
218 av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
219 return AVERROR_INVALIDDATA;
220 }
221
222 20 ff_ttadsp_init(&s->dsp);
223
224 20 return allocate_buffers(avctx);
225 }
226
227 46 static int tta_decode_frame(AVCodecContext *avctx, AVFrame *frame,
228 int *got_frame_ptr, AVPacket *avpkt)
229 {
230 46 const uint8_t *buf = avpkt->data;
231 46 int buf_size = avpkt->size;
232 46 TTAContext *s = avctx->priv_data;
233 GetBitContext gb;
234 int i, ret;
235 46 int cur_chan = 0, framelen = s->frame_length;
236 uint32_t *p;
237
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (avctx->err_recognition & AV_EF_CRCCHECK) {
239 if (buf_size < 4 ||
240 (tta_check_crc(s, buf, buf_size - 4) && avctx->err_recognition & AV_EF_EXPLODE))
241 return AVERROR_INVALIDDATA;
242 }
243
244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
245 return ret;
246
247 /* get output buffer */
248 46 frame->nb_samples = framelen;
249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
250 return ret;
251
252 // decode directly to output buffer for 24-bit sample format
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (s->bps == 3)
254 s->decode_buffer = (int32_t *)frame->data[0];
255
256 // init per channel states
257
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 46 times.
134 for (i = 0; i < s->channels; i++) {
258 88 TTAFilter *filter = &s->ch_ctx[i].filter;
259 88 s->ch_ctx[i].predictor = 0;
260 88 ff_tta_filter_init(filter, ff_tta_filter_configs[s->bps-1]);
261
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 70 times.
88 if (s->format == FORMAT_ENCRYPTED) {
262 int i;
263
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 18 times.
162 for (i = 0; i < 8; i++)
264 144 filter->qm[i] = sign_extend(s->crc_pass[i], 8);
265 }
266 88 ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
267 }
268
269 46 i = 0;
270
2/2
✓ Branch 0 taken 3824108 times.
✓ Branch 1 taken 38 times.
3824146 for (p = s->decode_buffer; (int32_t*)p < s->decode_buffer + (framelen * s->channels); p++) {
271 3824108 int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
272 3824108 TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
273 3824108 TTARice *rice = &s->ch_ctx[cur_chan].rice;
274 uint32_t unary, depth, k;
275 int32_t value;
276
277 3824108 unary = get_unary(&gb, 0, get_bits_left(&gb));
278
279
2/2
✓ Branch 0 taken 1800079 times.
✓ Branch 1 taken 2024029 times.
3824108 if (unary == 0) {
280 1800079 depth = 0;
281 1800079 k = rice->k0;
282 } else {
283 2024029 depth = 1;
284 2024029 k = rice->k1;
285 2024029 unary--;
286 }
287
288
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3824108 times.
3824108 if (get_bits_left(&gb) < k) {
289 ret = AVERROR_INVALIDDATA;
290 goto error;
291 }
292
293
2/2
✓ Branch 0 taken 3602502 times.
✓ Branch 1 taken 221606 times.
3824108 if (k) {
294
2/4
✓ Branch 0 taken 3602502 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3602502 times.
3602502 if (k > MIN_CACHE_BITS || unary > INT32_MAX >> k) {
295 ret = AVERROR_INVALIDDATA;
296 goto error;
297 }
298 3602502 value = (unary << k) + get_bits(&gb, k);
299 } else
300 221606 value = unary;
301
302 // FIXME: copy paste from original
303
2/2
✓ Branch 0 taken 2024029 times.
✓ Branch 1 taken 1800079 times.
3824108 switch (depth) {
304 2024029 case 1:
305 2024029 rice->sum1 += value - (rice->sum1 >> 4);
306
4/4
✓ Branch 0 taken 2020669 times.
✓ Branch 1 taken 3360 times.
✓ Branch 2 taken 55307 times.
✓ Branch 3 taken 1965362 times.
2024029 if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
307 55307 rice->k1--;
308
2/2
✓ Branch 0 taken 54952 times.
✓ Branch 1 taken 1913770 times.
1968722 else if(rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
309 54952 rice->k1++;
310 2024029 value += ff_tta_shift_1[rice->k0];
311 3824108 default:
312 3824108 rice->sum0 += value - (rice->sum0 >> 4);
313
4/4
✓ Branch 0 taken 3604464 times.
✓ Branch 1 taken 219644 times.
✓ Branch 2 taken 105062 times.
✓ Branch 3 taken 3499402 times.
3824108 if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
314 105062 rice->k0--;
315
2/2
✓ Branch 0 taken 104701 times.
✓ Branch 1 taken 3614345 times.
3719046 else if(rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
316 104701 rice->k0++;
317 }
318
319 // extract coded value
320 3824108 *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
321
322 // run hybrid filter
323 3824108 s->dsp.filter_process(filter->qm, filter->dx, filter->dl, &filter->error, p,
324 filter->shift, filter->round);
325
326 // fixed order prediction
327 #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
328
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3824108 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3824108 switch (s->bps) {
329 case 1: *p += PRED(*predictor, 4); break;
330 3824108 case 2:
331 3824108 case 3: *p += PRED(*predictor, 5); break;
332 case 4: *p += *predictor; break;
333 }
334 3824108 *predictor = *p;
335
336 // flip channels
337
2/2
✓ Branch 0 taken 1823854 times.
✓ Branch 1 taken 2000254 times.
3824108 if (cur_chan < (s->channels-1))
338 1823854 cur_chan++;
339 else {
340 // decorrelate in case of multiple channels
341
2/2
✓ Branch 0 taken 1823854 times.
✓ Branch 1 taken 176400 times.
2000254 if (s->channels > 1) {
342 1823854 int32_t *r = p - 1;
343
2/2
✓ Branch 0 taken 1823854 times.
✓ Branch 1 taken 1823854 times.
3647708 for (*p += *r / 2; r > (int32_t*)p - s->channels; r--)
344 1823854 *r = *(r + 1) - *r;
345 }
346 2000254 cur_chan = 0;
347 2000254 i++;
348 // check for last frame
349
4/4
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 2000208 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 38 times.
2000254 if (i == s->last_frame_length && get_bits_left(&gb) / 8 == 4) {
350 8 frame->nb_samples = framelen = s->last_frame_length;
351 8 break;
352 }
353 }
354 }
355
356 46 align_get_bits(&gb);
357
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
46 if (get_bits_left(&gb) < 32) {
358 ret = AVERROR_INVALIDDATA;
359 goto error;
360 }
361 46 skip_bits_long(&gb, 32); // frame crc
362
363 // convert to output buffer
364
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
46 switch (s->bps) {
365 case 1: {
366 uint8_t *samples = (uint8_t *)frame->data[0];
367 p = s->decode_buffer;
368 for (i = 0; i < framelen * s->channels; i++)
369 samples[i] = p[i] + 0x80;
370 break;
371 }
372 46 case 2: {
373 46 int16_t *samples = (int16_t *)frame->data[0];
374 46 p = s->decode_buffer;
375
2/2
✓ Branch 0 taken 3824108 times.
✓ Branch 1 taken 46 times.
3824154 for (i = 0; i < framelen * s->channels; i++)
376 3824108 samples[i] = p[i];
377 46 break;
378 }
379 case 3: {
380 // shift samples for 24-bit sample format
381 int32_t *samples = (int32_t *)frame->data[0];
382
383 for (i = 0; i < framelen * s->channels; i++)
384 samples[i] = samples[i] * 256U;
385 // reset decode buffer
386 s->decode_buffer = NULL;
387 break;
388 }
389 }
390
391 46 *got_frame_ptr = 1;
392
393 46 return buf_size;
394 error:
395 // reset decode buffer
396 if (s->bps == 3)
397 s->decode_buffer = NULL;
398 return ret;
399 }
400
401 20 static av_cold int tta_decode_close(AVCodecContext *avctx) {
402 20 TTAContext *s = avctx->priv_data;
403
404
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (s->bps < 3)
405 20 av_freep(&s->decode_buffer);
406 20 s->decode_buffer = NULL;
407 20 av_freep(&s->ch_ctx);
408
409 20 return 0;
410 }
411
412 #define OFFSET(x) offsetof(TTAContext, x)
413 #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
414 static const AVOption options[] = {
415 { "password", "Set decoding password", OFFSET(pass), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
416 { NULL },
417 };
418
419 static const AVClass tta_decoder_class = {
420 .class_name = "TTA Decoder",
421 .item_name = av_default_item_name,
422 .option = options,
423 .version = LIBAVUTIL_VERSION_INT,
424 };
425
426 const FFCodec ff_tta_decoder = {
427 .p.name = "tta",
428 CODEC_LONG_NAME("TTA (True Audio)"),
429 .p.type = AVMEDIA_TYPE_AUDIO,
430 .p.id = AV_CODEC_ID_TTA,
431 .priv_data_size = sizeof(TTAContext),
432 .init = tta_decode_init,
433 .close = tta_decode_close,
434 FF_CODEC_DECODE_CB(tta_decode_frame),
435 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_CHANNEL_CONF,
436 .p.priv_class = &tta_decoder_class,
437 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
438 };
439