FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dcadec.c
Date: 2021-09-24 03:35:22
Exec Total Coverage
Lines: 142 202 70.3%
Branches: 86 155 55.5%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2016 foo86
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/opt.h"
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/thread.h"
24
25 #include "dcadec.h"
26 #include "dcahuff.h"
27 #include "dca_syncwords.h"
28 #include "profiles.h"
29
30 #define MIN_PACKET_SIZE 16
31 #define MAX_PACKET_SIZE 0x104000
32
33 2476 int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask)
34 {
35 static const uint8_t dca2wav_norm[28] = {
36 2, 0, 1, 9, 10, 3, 8, 4, 5, 9, 10, 6, 7, 12,
37 13, 14, 3, 6, 7, 11, 12, 14, 16, 15, 17, 8, 4, 5,
38 };
39
40 static const uint8_t dca2wav_wide[28] = {
41 2, 0, 1, 4, 5, 3, 8, 4, 5, 9, 10, 6, 7, 12,
42 13, 14, 3, 9, 10, 11, 12, 14, 16, 15, 17, 8, 4, 5,
43 };
44
45 2476 int dca_ch, wav_ch, nchannels = 0;
46
47
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2476 times.
2476 if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
48 for (dca_ch = 0; dca_ch < DCA_SPEAKER_COUNT; dca_ch++)
49 if (dca_mask & (1U << dca_ch))
50 ch_remap[nchannels++] = dca_ch;
51 avctx->channel_layout = dca_mask;
52 } else {
53 2476 int wav_mask = 0;
54 int wav_map[18];
55 const uint8_t *dca2wav;
56
3/4
✓ Branch 0 taken 2476 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1174 times.
✓ Branch 3 taken 1302 times.
2476 if (dca_mask == DCA_SPEAKER_LAYOUT_7POINT0_WIDE ||
57 dca_mask == DCA_SPEAKER_LAYOUT_7POINT1_WIDE)
58 1174 dca2wav = dca2wav_wide;
59 else
60 1302 dca2wav = dca2wav_norm;
61
2/2
✓ Branch 0 taken 69328 times.
✓ Branch 1 taken 2476 times.
71804 for (dca_ch = 0; dca_ch < 28; dca_ch++) {
62
2/2
✓ Branch 0 taken 15383 times.
✓ Branch 1 taken 53945 times.
69328 if (dca_mask & (1 << dca_ch)) {
63 15383 wav_ch = dca2wav[dca_ch];
64
1/2
✓ Branch 0 taken 15383 times.
✗ Branch 1 not taken.
15383 if (!(wav_mask & (1 << wav_ch))) {
65 15383 wav_map[wav_ch] = dca_ch;
66 15383 wav_mask |= 1 << wav_ch;
67 }
68 }
69 }
70
2/2
✓ Branch 0 taken 44568 times.
✓ Branch 1 taken 2476 times.
47044 for (wav_ch = 0; wav_ch < 18; wav_ch++)
71
2/2
✓ Branch 0 taken 15383 times.
✓ Branch 1 taken 29185 times.
44568 if (wav_mask & (1 << wav_ch))
72 15383 ch_remap[nchannels++] = wav_map[wav_ch];
73 2476 avctx->channel_layout = wav_mask;
74 }
75
76 2476 avctx->channels = nchannels;
77 2476 return nchannels;
78 }
79
80 7 void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples,
81 int *coeff_l, int nsamples, int ch_mask)
82 {
83 7 int pos, spkr, max_spkr = av_log2(ch_mask);
84 7 int *coeff_r = coeff_l + av_popcount(ch_mask);
85
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 av_assert0(DCA_HAS_STEREO(ch_mask));
87
88 // Scale left and right channels
89 7 pos = (ch_mask & DCA_SPEAKER_MASK_C);
90 7 dcadsp->dmix_scale(samples[DCA_SPEAKER_L], coeff_l[pos ], nsamples);
91 7 dcadsp->dmix_scale(samples[DCA_SPEAKER_R], coeff_r[pos + 1], nsamples);
92
93 // Downmix remaining channels
94
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7 times.
49 for (spkr = 0; spkr <= max_spkr; spkr++) {
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!(ch_mask & (1U << spkr)))
96 continue;
97
98
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
42 if (*coeff_l && spkr != DCA_SPEAKER_L)
99 7 dcadsp->dmix_add(samples[DCA_SPEAKER_L], samples[spkr],
100 *coeff_l, nsamples);
101
102
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
42 if (*coeff_r && spkr != DCA_SPEAKER_R)
103 7 dcadsp->dmix_add(samples[DCA_SPEAKER_R], samples[spkr],
104 *coeff_r, nsamples);
105
106 42 coeff_l++;
107 42 coeff_r++;
108 }
109 7 }
110
111 7 void ff_dca_downmix_to_stereo_float(AVFloatDSPContext *fdsp, float **samples,
112 int *coeff_l, int nsamples, int ch_mask)
113 {
114 7 int pos, spkr, max_spkr = av_log2(ch_mask);
115 7 int *coeff_r = coeff_l + av_popcount(ch_mask);
116 7 const float scale = 1.0f / (1 << 15);
117
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 av_assert0(DCA_HAS_STEREO(ch_mask));
119
120 // Scale left and right channels
121 7 pos = (ch_mask & DCA_SPEAKER_MASK_C);
122 7 fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_L], samples[DCA_SPEAKER_L],
123 7 coeff_l[pos ] * scale, nsamples);
124 7 fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_R], samples[DCA_SPEAKER_R],
125 7 coeff_r[pos + 1] * scale, nsamples);
126
127 // Downmix remaining channels
128
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7 times.
49 for (spkr = 0; spkr <= max_spkr; spkr++) {
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!(ch_mask & (1U << spkr)))
130 continue;
131
132
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
42 if (*coeff_l && spkr != DCA_SPEAKER_L)
133 7 fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_L], samples[spkr],
134 7 *coeff_l * scale, nsamples);
135
136
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
42 if (*coeff_r && spkr != DCA_SPEAKER_R)
137 7 fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_R], samples[spkr],
138 7 *coeff_r * scale, nsamples);
139
140 42 coeff_l++;
141 42 coeff_r++;
142 }
143 7 }
144
145 2476 static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
146 int *got_frame_ptr, AVPacket *avpkt)
147 {
148 2476 DCAContext *s = avctx->priv_data;
149 2476 AVFrame *frame = data;
150 2476 uint8_t *input = avpkt->data;
151 2476 int input_size = avpkt->size;
152 2476 int i, ret, prev_packet = s->packet;
153 uint32_t mrk;
154
155
2/4
✓ Branch 0 taken 2476 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2476 times.
2476 if (input_size < MIN_PACKET_SIZE || input_size > MAX_PACKET_SIZE) {
156 av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
157 return AVERROR_INVALIDDATA;
158 }
159
160 // Convert input to BE format
161 2476 mrk = AV_RB32(input);
162
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2464 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
2476 if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
163 av_fast_padded_malloc(&s->buffer, &s->buffer_size, input_size);
164 if (!s->buffer)
165 return AVERROR(ENOMEM);
166
167 for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - MIN_PACKET_SIZE + 1 && ret < 0; i++)
168 ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
169
170 if (ret < 0) {
171 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
172 return ret;
173 }
174
175 input = s->buffer;
176 input_size = ret;
177 }
178
179 2476 s->packet = 0;
180
181 // Parse backward compatible core sub-stream
182
2/2
✓ Branch 0 taken 2464 times.
✓ Branch 1 taken 12 times.
2476 if (AV_RB32(input) == DCA_SYNCWORD_CORE_BE) {
183 int frame_size;
184
185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
2464 if ((ret = ff_dca_core_parse(&s->core, input, input_size)) < 0)
186 return ret;
187
188 2464 s->packet |= DCA_PACKET_CORE;
189
190 // EXXS data must be aligned on 4-byte boundary
191 2464 frame_size = FFALIGN(s->core.frame_size, 4);
192
2/2
✓ Branch 0 taken 1398 times.
✓ Branch 1 taken 1066 times.
2464 if (input_size - 4 > frame_size) {
193 1398 input += frame_size;
194 1398 input_size -= frame_size;
195 }
196 }
197
198
1/2
✓ Branch 0 taken 2476 times.
✗ Branch 1 not taken.
2476 if (!s->core_only) {
199 2476 DCAExssAsset *asset = NULL;
200
201 // Parse extension sub-stream (EXSS)
202
2/2
✓ Branch 0 taken 1410 times.
✓ Branch 1 taken 1066 times.
2476 if (AV_RB32(input) == DCA_SYNCWORD_SUBSTREAM) {
203
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1410 times.
1410 if ((ret = ff_dca_exss_parse(&s->exss, input, input_size)) < 0) {
204 if (avctx->err_recognition & AV_EF_EXPLODE)
205 return ret;
206 } else {
207 1410 s->packet |= DCA_PACKET_EXSS;
208 1410 asset = &s->exss.assets[0];
209 }
210 }
211
212 // Parse XLL component in EXSS
213
4/4
✓ Branch 0 taken 1410 times.
✓ Branch 1 taken 1066 times.
✓ Branch 2 taken 1354 times.
✓ Branch 3 taken 56 times.
2476 if (asset && (asset->extension_mask & DCA_EXSS_XLL)) {
214
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1354 times.
1354 if ((ret = ff_dca_xll_parse(&s->xll, input, asset)) < 0) {
215 // Conceal XLL synchronization error
216 if (ret == AVERROR(EAGAIN)
217 && (prev_packet & DCA_PACKET_XLL)
218 && (s->packet & DCA_PACKET_CORE))
219 s->packet |= DCA_PACKET_XLL | DCA_PACKET_RECOVERY;
220 else if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
221 return ret;
222 } else {
223 1354 s->packet |= DCA_PACKET_XLL;
224 }
225 }
226
227 // Parse LBR component in EXSS
228
3/4
✓ Branch 0 taken 1410 times.
✓ Branch 1 taken 1066 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1410 times.
2476 if (asset && (asset->extension_mask & DCA_EXSS_LBR)) {
229 if ((ret = ff_dca_lbr_parse(&s->lbr, input, asset)) < 0) {
230 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
231 return ret;
232 } else {
233 s->packet |= DCA_PACKET_LBR;
234 }
235 }
236
237 // Parse core extensions in EXSS or backward compatible core sub-stream
238
2/2
✓ Branch 0 taken 2464 times.
✓ Branch 1 taken 12 times.
2476 if ((s->packet & DCA_PACKET_CORE)
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
2464 && (ret = ff_dca_core_parse_exss(&s->core, input, asset)) < 0)
240 return ret;
241 }
242
243 // Filter the frame
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2476 times.
2476 if (s->packet & DCA_PACKET_LBR) {
245 if ((ret = ff_dca_lbr_filter_frame(&s->lbr, frame)) < 0)
246 return ret;
247
2/2
✓ Branch 0 taken 1354 times.
✓ Branch 1 taken 1122 times.
2476 } else if (s->packet & DCA_PACKET_XLL) {
248
2/2
✓ Branch 0 taken 1342 times.
✓ Branch 1 taken 12 times.
1354 if (s->packet & DCA_PACKET_CORE) {
249 1342 int x96_synth = -1;
250
251 // Enable X96 synthesis if needed
252
3/4
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1258 times.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
1342 if (s->xll.chset[0].freq == 96000 && s->core.sample_rate == 48000)
253 84 x96_synth = 1;
254
255
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1342 times.
1342 if ((ret = ff_dca_core_filter_fixed(&s->core, x96_synth)) < 0)
256 return ret;
257
258 // Force lossy downmixed output on the first core frame filtered.
259 // This prevents audible clicks when seeking and is consistent with
260 // what reference decoder does when there are multiple channel sets.
261
3/4
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 1291 times.
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
1342 if (!(prev_packet & DCA_PACKET_RESIDUAL) && s->xll.nreschsets > 0
262
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 18 times.
51 && s->xll.nchsets > 1) {
263 33 av_log(avctx, AV_LOG_VERBOSE, "Forcing XLL recovery mode\n");
264 33 s->packet |= DCA_PACKET_RECOVERY;
265 }
266
267 // Set 'residual ok' flag for the next frame
268 1342 s->packet |= DCA_PACKET_RESIDUAL;
269 }
270
271
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1354 times.
1354 if ((ret = ff_dca_xll_filter_frame(&s->xll, frame)) < 0) {
272 // Fall back to core unless hard error
273 if (!(s->packet & DCA_PACKET_CORE))
274 return ret;
275 if (ret != AVERROR_INVALIDDATA || (avctx->err_recognition & AV_EF_EXPLODE))
276 return ret;
277 if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0)
278 return ret;
279 }
280
1/2
✓ Branch 0 taken 1122 times.
✗ Branch 1 not taken.
1122 } else if (s->packet & DCA_PACKET_CORE) {
281
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1122 times.
1122 if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0)
282 return ret;
283
2/2
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 604 times.
1122 if (s->core.filter_mode & DCA_FILTER_MODE_FIXED)
284 518 s->packet |= DCA_PACKET_RESIDUAL;
285 } else {
286 av_log(avctx, AV_LOG_ERROR, "No valid DCA sub-stream found\n");
287 if (s->core_only)
288 av_log(avctx, AV_LOG_WARNING, "Consider disabling 'core_only' option\n");
289 return AVERROR_INVALIDDATA;
290 }
291
292 2476 *got_frame_ptr = 1;
293
294 2476 return avpkt->size;
295 }
296
297 static av_cold void dcadec_flush(AVCodecContext *avctx)
298 {
299 DCAContext *s = avctx->priv_data;
300
301 ff_dca_core_flush(&s->core);
302 ff_dca_xll_flush(&s->xll);
303 ff_dca_lbr_flush(&s->lbr);
304
305 s->packet &= DCA_PACKET_MASK;
306 }
307
308 92 static av_cold int dcadec_close(AVCodecContext *avctx)
309 {
310 92 DCAContext *s = avctx->priv_data;
311
312 92 ff_dca_core_close(&s->core);
313 92 ff_dca_xll_close(&s->xll);
314 92 ff_dca_lbr_close(&s->lbr);
315
316 92 av_freep(&s->buffer);
317 92 s->buffer_size = 0;
318
319 92 return 0;
320 }
321
322 47 static av_cold void dcadec_init_static(void)
323 {
324 47 ff_dca_lbr_init_tables();
325 47 ff_dca_init_vlcs();
326 47 }
327
328 92 static av_cold int dcadec_init(AVCodecContext *avctx)
329 {
330 static AVOnce init_static_once = AV_ONCE_INIT;
331 92 DCAContext *s = avctx->priv_data;
332
333 92 s->avctx = avctx;
334 92 s->core.avctx = avctx;
335 92 s->exss.avctx = avctx;
336 92 s->xll.avctx = avctx;
337 92 s->lbr.avctx = avctx;
338
339
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
92 if (ff_dca_core_init(&s->core) < 0)
340 return AVERROR(ENOMEM);
341
342
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
92 if (ff_dca_lbr_init(&s->lbr) < 0)
343 return AVERROR(ENOMEM);
344
345 92 ff_dcadsp_init(&s->dcadsp);
346 92 s->core.dcadsp = &s->dcadsp;
347 92 s->xll.dcadsp = &s->dcadsp;
348 92 s->lbr.dcadsp = &s->dcadsp;
349
350 92 s->crctab = av_crc_get_table(AV_CRC_16_CCITT);
351
352
3/5
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
✗ Branch 4 not taken.
92 switch (avctx->request_channel_layout & ~AV_CH_LAYOUT_NATIVE) {
353 48 case 0:
354 48 s->request_channel_layout = 0;
355 48 break;
356 22 case AV_CH_LAYOUT_STEREO:
357 case AV_CH_LAYOUT_STEREO_DOWNMIX:
358 22 s->request_channel_layout = DCA_SPEAKER_LAYOUT_STEREO;
359 22 break;
360 case AV_CH_LAYOUT_5POINT0:
361 s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT0;
362 break;
363 22 case AV_CH_LAYOUT_5POINT1:
364 22 s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT1;
365 22 break;
366 default:
367 av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
368 break;
369 }
370
371 92 ff_thread_once(&init_static_once, dcadec_init_static);
372
373 92 return 0;
374 }
375
376 #define OFFSET(x) offsetof(DCAContext, x)
377 #define PARAM AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
378
379 static const AVOption dcadec_options[] = {
380 { "core_only", "Decode core only without extensions", OFFSET(core_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, PARAM },
381 { NULL }
382 };
383
384 static const AVClass dcadec_class = {
385 .class_name = "DCA decoder",
386 .item_name = av_default_item_name,
387 .option = dcadec_options,
388 .version = LIBAVUTIL_VERSION_INT,
389 .category = AV_CLASS_CATEGORY_DECODER,
390 };
391
392 const AVCodec ff_dca_decoder = {
393 .name = "dca",
394 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
395 .type = AVMEDIA_TYPE_AUDIO,
396 .id = AV_CODEC_ID_DTS,
397 .priv_data_size = sizeof(DCAContext),
398 .init = dcadec_init,
399 .decode = dcadec_decode_frame,
400 .close = dcadec_close,
401 .flush = dcadec_flush,
402 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
403 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
404 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
405 .priv_class = &dcadec_class,
406 .profiles = NULL_IF_CONFIG_SMALL(ff_dca_profiles),
407 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
408 };
409