FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dca_parser.c
Date: 2026-01-19 14:53:03
Exec Total Coverage
Lines: 144 182 79.1%
Functions: 4 4 100.0%
Branches: 100 140 71.4%

Line Branch Exec Source
1 /*
2 * DCA parser
3 * Copyright (C) 2004 Gildas Bazin
4 * Copyright (C) 2004 Benjamin Zores
5 * Copyright (C) 2006 Benjamin Larsson
6 * Copyright (C) 2007 Konstantin Shishkov
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "dca.h"
26 #include "dca_core.h"
27 #include "dca_exss.h"
28 #include "dca_lbr.h"
29 #include "dca_syncwords.h"
30 #include "get_bits.h"
31 #include "parser.h"
32 #include "parser_internal.h"
33
34 typedef struct DCAParseContext {
35 ParseContext pc;
36 uint32_t lastmarker;
37 int size;
38 int framesize;
39 unsigned int startpos;
40 DCAExssParser exss;
41 unsigned int sr_code;
42 } DCAParseContext;
43
44 #define IS_CORE_MARKER(state) \
45 (((state & 0xFFFFFFFFF0FF) == (((uint64_t)DCA_SYNCWORD_CORE_14B_LE << 16) | 0xF007)) || \
46 ((state & 0xFFFFFFFFFFF0) == (((uint64_t)DCA_SYNCWORD_CORE_14B_BE << 16) | 0x07F0)) || \
47 ((state & 0xFFFFFFFF00FC) == (((uint64_t)DCA_SYNCWORD_CORE_LE << 16) | 0x00FC)) || \
48 ((state & 0xFFFFFFFFFC00) == (((uint64_t)DCA_SYNCWORD_CORE_BE << 16) | 0xFC00)))
49
50 #define IS_EXSS_MARKER(state) ((state & 0xFFFFFFFF) == DCA_SYNCWORD_SUBSTREAM)
51
52 #define IS_MARKER(state) (IS_CORE_MARKER(state) || IS_EXSS_MARKER(state))
53
54 #define CORE_MARKER(state) ((state >> 16) & 0xFFFFFFFF)
55 #define EXSS_MARKER(state) (state & 0xFFFFFFFF)
56
57 #define STATE_LE(state) (((state & 0xFF00FF00) >> 8) | ((state & 0x00FF00FF) << 8))
58 #define STATE_14(state) (((state & 0x3FFF0000) >> 8) | ((state & 0x00003FFF) >> 6))
59
60 #define CORE_FRAMESIZE(state) (((state >> 4) & 0x3FFF) + 1)
61 #define EXSS_FRAMESIZE(state) ((state & 0x2000000000) ? \
62 ((state >> 5) & 0xFFFFF) + 1 : \
63 ((state >> 13) & 0x0FFFF) + 1)
64
65 /**
66 * Find the end of the current frame in the bitstream.
67 * @return the position of the first byte of the next frame, or -1
68 */
69 42744 static int dca_find_frame_end(DCAParseContext *pc1, const uint8_t *buf,
70 int buf_size)
71 {
72 int start_found, size, i;
73 uint64_t state;
74 42744 ParseContext *pc = &pc1->pc;
75
76 42744 start_found = pc->frame_start_found;
77 42744 state = pc->state64;
78 42744 size = pc1->size;
79
80 42744 i = 0;
81
2/2
✓ Branch 0 taken 7397 times.
✓ Branch 1 taken 35347 times.
42744 if (!start_found) {
82
1/2
✓ Branch 0 taken 44376 times.
✗ Branch 1 not taken.
44376 for (; i < buf_size; i++) {
83 44376 size++;
84 44376 state = (state << 8) | buf[i];
85
86
8/10
✓ Branch 0 taken 44362 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 44362 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 44362 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 36988 times.
✓ Branch 7 taken 7374 times.
✓ Branch 8 taken 9 times.
✓ Branch 9 taken 36979 times.
44376 if (IS_MARKER(state) &&
87
2/2
✓ Branch 0 taken 7344 times.
✓ Branch 1 taken 53 times.
7397 (!pc1->lastmarker ||
88
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7338 times.
7344 pc1->lastmarker == CORE_MARKER(state) ||
89
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
90
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 7344 times.
7397 if (!pc1->lastmarker)
91
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 50 times.
53 pc1->startpos = IS_EXSS_MARKER(state) ? size - 4 : size - 6;
92
93
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 7388 times.
7397 if (IS_EXSS_MARKER(state))
94 9 pc1->lastmarker = EXSS_MARKER(state);
95 else
96 7388 pc1->lastmarker = CORE_MARKER(state);
97
98 7397 start_found = 1;
99 7397 size = 0;
100
101 7397 i++;
102 7397 break;
103 }
104 }
105 }
106
107
1/2
✓ Branch 0 taken 42744 times.
✗ Branch 1 not taken.
42744 if (start_found) {
108
2/2
✓ Branch 0 taken 36421932 times.
✓ Branch 1 taken 35400 times.
36457332 for (; i < buf_size; i++) {
109 36421932 size++;
110 36421932 state = (state << 8) | buf[i];
111
112
2/2
✓ Branch 0 taken 14858 times.
✓ Branch 1 taken 36407074 times.
36421932 if (start_found == 1) {
113
3/6
✓ Branch 0 taken 14748 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
14858 switch (pc1->lastmarker) {
114 14748 case DCA_SYNCWORD_CORE_BE:
115
2/2
✓ Branch 0 taken 7374 times.
✓ Branch 1 taken 7374 times.
14748 if (size == 2) {
116 7374 pc1->framesize = CORE_FRAMESIZE(state);
117 7374 start_found = 2;
118 }
119 14748 break;
120 case DCA_SYNCWORD_CORE_LE:
121 if (size == 2) {
122 pc1->framesize = CORE_FRAMESIZE(STATE_LE(state));
123 start_found = 4;
124 }
125 break;
126 case DCA_SYNCWORD_CORE_14B_BE:
127 if (size == 4) {
128 pc1->framesize = CORE_FRAMESIZE(STATE_14(state));
129 start_found = 4;
130 }
131 break;
132 56 case DCA_SYNCWORD_CORE_14B_LE:
133
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 42 times.
56 if (size == 4) {
134 14 pc1->framesize = CORE_FRAMESIZE(STATE_14(STATE_LE(state)));
135 14 start_found = 4;
136 }
137 56 break;
138 54 case DCA_SYNCWORD_SUBSTREAM:
139
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 45 times.
54 if (size == 6) {
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 pc1->framesize = EXSS_FRAMESIZE(state);
141 9 start_found = 4;
142 }
143 54 break;
144 default:
145 av_assert0(0);
146 }
147 14858 continue;
148 }
149
150
4/4
✓ Branch 0 taken 14152252 times.
✓ Branch 1 taken 22254822 times.
✓ Branch 2 taken 6052 times.
✓ Branch 3 taken 14146200 times.
36407074 if (start_found == 2 && IS_EXSS_MARKER(state) &&
151
1/2
✓ Branch 0 taken 6052 times.
✗ Branch 1 not taken.
6052 pc1->framesize <= size + 2) {
152 6052 pc1->framesize = size + 2;
153 6052 start_found = 3;
154 6052 continue;
155 }
156
157
2/2
✓ Branch 0 taken 36312 times.
✓ Branch 1 taken 36364710 times.
36401022 if (start_found == 3) {
158
2/2
✓ Branch 0 taken 6052 times.
✓ Branch 1 taken 30260 times.
36312 if (size == pc1->framesize + 4) {
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6052 times.
6052 pc1->framesize += EXSS_FRAMESIZE(state);
160 6052 start_found = 4;
161 }
162 36312 continue;
163 }
164
165
2/2
✓ Branch 0 taken 36350119 times.
✓ Branch 1 taken 14591 times.
36364710 if (pc1->framesize > size)
166 36350119 continue;
167
168
8/10
✓ Branch 0 taken 14578 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 14578 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14578 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7253 times.
✓ Branch 7 taken 7325 times.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 7247 times.
14591 if (IS_MARKER(state) &&
169
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7338 times.
7344 (pc1->lastmarker == CORE_MARKER(state) ||
170
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
171 7344 pc->frame_start_found = 0;
172 7344 pc->state64 = -1;
173 7344 pc1->size = 0;
174
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7338 times.
7344 return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
175 }
176 }
177 }
178
179 35400 pc->frame_start_found = start_found;
180 35400 pc->state64 = state;
181 35400 pc1->size = size;
182 35400 return END_NOT_FOUND;
183 }
184
185 56 static av_cold int dca_parse_init(AVCodecParserContext *s)
186 {
187 56 DCAParseContext *pc1 = s->priv_data;
188
189 56 pc1->lastmarker = 0;
190 56 pc1->sr_code = -1;
191 56 return 0;
192 }
193
194 9270 static int dca_parse_params(DCAParseContext *pc1, const uint8_t *buf,
195 int buf_size, int *duration, int *sample_rate,
196 int *profile)
197 {
198 9270 DCAExssAsset *asset = &pc1->exss.assets[0];
199 GetBitContext gb;
200 DCACoreFrameHeader h;
201 9270 uint8_t hdr[DCA_CORE_FRAME_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 };
202 int ret, frame_size;
203
204
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 9214 times.
9270 if (buf_size < DCA_CORE_FRAME_HEADER_SIZE)
205 56 return AVERROR_INVALIDDATA;
206
207
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9205 times.
9214 if (AV_RB32(buf) == DCA_SYNCWORD_SUBSTREAM) {
208
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if ((ret = ff_dca_exss_parse(&pc1->exss, buf, buf_size)) < 0)
209 return ret;
210
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (asset->extension_mask & DCA_EXSS_LBR) {
212 if ((ret = init_get_bits8(&gb, buf + asset->lbr_offset, asset->lbr_size)) < 0)
213 return ret;
214
215 if (get_bits_long(&gb, 32) != DCA_SYNCWORD_LBR)
216 return AVERROR_INVALIDDATA;
217
218 switch (get_bits(&gb, 8)) {
219 case DCA_LBR_HEADER_DECODER_INIT:
220 pc1->sr_code = get_bits(&gb, 8);
221 case DCA_LBR_HEADER_SYNC_ONLY:
222 break;
223 default:
224 return AVERROR_INVALIDDATA;
225 }
226
227 if (pc1->sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs))
228 return AVERROR_INVALIDDATA;
229
230 *sample_rate = ff_dca_sampling_freqs[pc1->sr_code];
231 *duration = 1024 << ff_dca_freq_ranges[pc1->sr_code];
232 *profile = AV_PROFILE_DTS_EXPRESS;
233 return 0;
234 }
235
236
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (asset->extension_mask & DCA_EXSS_XLL) {
237 int nsamples_log2;
238
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if ((ret = init_get_bits8(&gb, buf + asset->xll_offset, asset->xll_size)) < 0)
240 return ret;
241
242
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (get_bits_long(&gb, 32) != DCA_SYNCWORD_XLL)
243 return AVERROR_INVALIDDATA;
244
245
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (get_bits(&gb, 4))
246 return AVERROR_INVALIDDATA;
247
248 9 skip_bits(&gb, 8);
249 9 skip_bits_long(&gb, get_bits(&gb, 5) + 1);
250 9 skip_bits(&gb, 4);
251 9 nsamples_log2 = get_bits(&gb, 4) + get_bits(&gb, 4);
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (nsamples_log2 > 24)
253 return AVERROR_INVALIDDATA;
254
255 9 *sample_rate = asset->max_sample_rate;
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 *duration = (1 + (*sample_rate > 96000)) << nsamples_log2;
257 9 *profile = AV_PROFILE_DTS_HD_MA;
258 9 return 0;
259 }
260
261 return AVERROR_INVALIDDATA;
262 }
263
264
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9205 times.
9205 if ((ret = avpriv_dca_convert_bitstream(buf, DCA_CORE_FRAME_HEADER_SIZE,
265 hdr, DCA_CORE_FRAME_HEADER_SIZE)) < 0)
266 return ret;
267
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9205 times.
9205 if (avpriv_dca_parse_core_frame_header(&h, hdr, ret) < 0)
268 return AVERROR_INVALIDDATA;
269
270 9205 *duration = h.npcmblocks * DCA_PCMBLOCK_SAMPLES;
271 9205 *sample_rate = ff_dca_sample_rates[h.sr_code];
272
2/2
✓ Branch 0 taken 9153 times.
✓ Branch 1 taken 52 times.
9205 if (*profile != AV_PROFILE_UNKNOWN)
273 9153 return 0;
274
275 52 *profile = AV_PROFILE_DTS;
276
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 40 times.
52 if (h.ext_audio_present) {
277
2/3
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
12 switch (h.ext_audio_type) {
278 8 case DCA_EXT_AUDIO_XCH:
279 case DCA_EXT_AUDIO_XXCH:
280 8 *profile = AV_PROFILE_DTS_ES;
281 8 break;
282 4 case DCA_EXT_AUDIO_X96:
283 4 *profile = AV_PROFILE_DTS_96_24;
284 4 break;
285 }
286 }
287
288 52 frame_size = FFALIGN(h.frame_size, 4);
289
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 38 times.
52 if (buf_size - 4 < frame_size)
290 14 return 0;
291
292 38 buf += frame_size;
293 38 buf_size -= frame_size;
294
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37 times.
38 if (AV_RB32(buf) != DCA_SYNCWORD_SUBSTREAM)
295 1 return 0;
296
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if (ff_dca_exss_parse(&pc1->exss, buf, buf_size) < 0)
297 return 0;
298
299
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 8 times.
37 if (asset->extension_mask & DCA_EXSS_XLL)
300 29 *profile = AV_PROFILE_DTS_HD_MA;
301
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 else if (asset->extension_mask & (DCA_EXSS_XBR | DCA_EXSS_XXCH | DCA_EXSS_X96))
302 8 *profile = AV_PROFILE_DTS_HD_HRA;
303
304 37 return 0;
305 }
306
307 44564 static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
308 const uint8_t **poutbuf, int *poutbuf_size,
309 const uint8_t *buf, int buf_size)
310 {
311 44564 DCAParseContext *pc1 = s->priv_data;
312 44564 ParseContext *pc = &pc1->pc;
313 int next, duration, sample_rate;
314
315
2/2
✓ Branch 0 taken 1820 times.
✓ Branch 1 taken 42744 times.
44564 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
316 1820 next = buf_size;
317 } else {
318 42744 next = dca_find_frame_end(pc1, buf, buf_size);
319
320
2/2
✓ Branch 1 taken 35294 times.
✓ Branch 2 taken 7450 times.
42744 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
321 35294 *poutbuf = NULL;
322 35294 *poutbuf_size = 0;
323 35294 return buf_size;
324 }
325
326 /* skip initial padding */
327
2/2
✓ Branch 0 taken 7397 times.
✓ Branch 1 taken 53 times.
7450 if (buf_size > pc1->startpos) {
328 7397 buf += pc1->startpos;
329 7397 buf_size -= pc1->startpos;
330 }
331 7450 pc1->startpos = 0;
332 }
333
334 /* read the duration and sample rate from the frame header */
335
2/2
✓ Branch 1 taken 9214 times.
✓ Branch 2 taken 56 times.
9270 if (!dca_parse_params(pc1, buf, buf_size, &duration, &sample_rate, &avctx->profile)) {
336
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 9203 times.
9214 if (!avctx->sample_rate)
337 11 avctx->sample_rate = sample_rate;
338 9214 s->duration = av_rescale(duration, avctx->sample_rate, sample_rate);
339 } else
340 56 s->duration = 0;
341
342 9270 *poutbuf = buf;
343 9270 *poutbuf_size = buf_size;
344 9270 return next;
345 }
346
347 const FFCodecParser ff_dca_parser = {
348 PARSER_CODEC_LIST(AV_CODEC_ID_DTS),
349 .priv_data_size = sizeof(DCAParseContext),
350 .init = dca_parse_init,
351 .parse = dca_parse,
352 .close = ff_parse_close,
353 };
354