FFmpeg coverage


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