FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dca_parser.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 144 183 78.7%
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 43932 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 43932 ParseContext *pc = &pc1->pc;
75
76 43932 start_found = pc->frame_start_found;
77 43932 state = pc->state64;
78 43932 size = pc1->size;
79
80 43932 i = 0;
81
2/2
✓ Branch 0 taken 7550 times.
✓ Branch 1 taken 36382 times.
43932 if (!start_found) {
82
1/2
✓ Branch 0 taken 45276 times.
✗ Branch 1 not taken.
45276 for (; i < buf_size; i++) {
83 45276 size++;
84 45276 state = (state << 8) | buf[i];
85
86
8/10
✓ Branch 0 taken 45262 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 45262 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45262 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 37744 times.
✓ Branch 7 taken 7518 times.
✓ Branch 8 taken 18 times.
✓ Branch 9 taken 37726 times.
45276 if (IS_MARKER(state) &&
87
2/2
✓ Branch 0 taken 7470 times.
✓ Branch 1 taken 80 times.
7550 (!pc1->lastmarker ||
88
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7458 times.
7470 pc1->lastmarker == CORE_MARKER(state) ||
89
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
90
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 7470 times.
7550 if (!pc1->lastmarker)
91
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 74 times.
80 pc1->startpos = IS_EXSS_MARKER(state) ? size - 4 : size - 6;
92
93
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7532 times.
7550 if (IS_EXSS_MARKER(state))
94 18 pc1->lastmarker = EXSS_MARKER(state);
95 else
96 7532 pc1->lastmarker = CORE_MARKER(state);
97
98 7550 start_found = 1;
99 7550 size = 0;
100
101 7550 i++;
102 7550 break;
103 }
104 }
105 }
106
107
1/2
✓ Branch 0 taken 43932 times.
✗ Branch 1 not taken.
43932 if (start_found) {
108
2/2
✓ Branch 0 taken 37437348 times.
✓ Branch 1 taken 36462 times.
37473810 for (; i < buf_size; i++) {
109 37437348 size++;
110 37437348 state = (state << 8) | buf[i];
111
112
2/2
✓ Branch 0 taken 15200 times.
✓ Branch 1 taken 37422148 times.
37437348 if (start_found == 1) {
113
3/6
✓ Branch 0 taken 15036 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
15200 switch (pc1->lastmarker) {
114 15036 case DCA_SYNCWORD_CORE_BE:
115
2/2
✓ Branch 0 taken 7518 times.
✓ Branch 1 taken 7518 times.
15036 if (size == 2) {
116 7518 pc1->framesize = CORE_FRAMESIZE(state);
117 7518 start_found = 2;
118 }
119 15036 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 108 case DCA_SYNCWORD_SUBSTREAM:
139
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 90 times.
108 if (size == 6) {
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 pc1->framesize = EXSS_FRAMESIZE(state);
141 18 start_found = 4;
142 }
143 108 break;
144 default:
145 av_assert0(0);
146 }
147 15200 continue;
148 }
149
150
4/4
✓ Branch 0 taken 14316916 times.
✓ Branch 1 taken 23105232 times.
✓ Branch 2 taken 6196 times.
✓ Branch 3 taken 14310720 times.
37422148 if (start_found == 2 && IS_EXSS_MARKER(state) &&
151
1/2
✓ Branch 0 taken 6196 times.
✗ Branch 1 not taken.
6196 pc1->framesize <= size + 2) {
152 6196 pc1->framesize = size + 2;
153 6196 start_found = 3;
154 6196 continue;
155 }
156
157
2/2
✓ Branch 0 taken 37176 times.
✓ Branch 1 taken 37378776 times.
37415952 if (start_found == 3) {
158
2/2
✓ Branch 0 taken 6196 times.
✓ Branch 1 taken 30980 times.
37176 if (size == pc1->framesize + 4) {
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6196 times.
6196 pc1->framesize += EXSS_FRAMESIZE(state);
160 6196 start_found = 4;
161 }
162 37176 continue;
163 }
164
165
2/2
✓ Branch 0 taken 37364059 times.
✓ Branch 1 taken 14717 times.
37378776 if (pc1->framesize > size)
166 37364059 continue;
167
168
8/10
✓ Branch 0 taken 14704 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 14704 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14704 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7259 times.
✓ Branch 7 taken 7445 times.
✓ Branch 8 taken 12 times.
✓ Branch 9 taken 7247 times.
14717 if (IS_MARKER(state) &&
169
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7458 times.
7470 (pc1->lastmarker == CORE_MARKER(state) ||
170
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 pc1->lastmarker == DCA_SYNCWORD_SUBSTREAM)) {
171 7470 pc->frame_start_found = 0;
172 7470 pc->state64 = -1;
173 7470 pc1->size = 0;
174
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7458 times.
7470 return IS_EXSS_MARKER(state) ? i - 3 : i - 5;
175 }
176 }
177 }
178
179 36462 pc->frame_start_found = start_found;
180 36462 pc->state64 = state;
181 36462 pc1->size = size;
182 36462 return END_NOT_FOUND;
183 }
184
185 83 static av_cold int dca_parse_init(AVCodecParserContext *s)
186 {
187 83 DCAParseContext *pc1 = s->priv_data;
188
189 83 pc1->lastmarker = 0;
190 83 pc1->sr_code = -1;
191 83 return 0;
192 }
193
194 9450 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 9450 DCAExssAsset *asset = &pc1->exss.assets[0];
199 GetBitContext gb;
200 DCACoreFrameHeader h;
201 9450 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 83 times.
✓ Branch 1 taken 9367 times.
9450 if (buf_size < DCA_CORE_FRAME_HEADER_SIZE)
205 83 return AVERROR_INVALIDDATA;
206
207
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9349 times.
9367 if (AV_RB32(buf) == DCA_SYNCWORD_SUBSTREAM) {
208
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 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 18 times.
18 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 break;
222 case DCA_LBR_HEADER_SYNC_ONLY:
223 break;
224 default:
225 return AVERROR_INVALIDDATA;
226 }
227
228 if (pc1->sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs))
229 return AVERROR_INVALIDDATA;
230
231 *sample_rate = ff_dca_sampling_freqs[pc1->sr_code];
232 *duration = 1024 << ff_dca_freq_ranges[pc1->sr_code];
233 *profile = AV_PROFILE_DTS_EXPRESS;
234 return 0;
235 }
236
237
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (asset->extension_mask & DCA_EXSS_XLL) {
238 int nsamples_log2;
239
240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = init_get_bits8(&gb, buf + asset->xll_offset, asset->xll_size)) < 0)
241 return ret;
242
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if (get_bits_long(&gb, 32) != DCA_SYNCWORD_XLL)
244 return AVERROR_INVALIDDATA;
245
246
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if (get_bits(&gb, 4))
247 return AVERROR_INVALIDDATA;
248
249 18 skip_bits(&gb, 8);
250 18 skip_bits_long(&gb, get_bits(&gb, 5) + 1);
251 18 skip_bits(&gb, 4);
252 18 nsamples_log2 = get_bits(&gb, 4) + get_bits(&gb, 4);
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (nsamples_log2 > 24)
254 return AVERROR_INVALIDDATA;
255
256 18 *sample_rate = asset->max_sample_rate;
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 *duration = (1 + (*sample_rate > 96000)) << nsamples_log2;
258 18 *profile = AV_PROFILE_DTS_HD_MA;
259 18 return 0;
260 }
261
262 return AVERROR_INVALIDDATA;
263 }
264
265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9349 times.
9349 if ((ret = avpriv_dca_convert_bitstream(buf, DCA_CORE_FRAME_HEADER_SIZE,
266 hdr, DCA_CORE_FRAME_HEADER_SIZE)) < 0)
267 return ret;
268
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9349 times.
9349 if (avpriv_dca_parse_core_frame_header(&h, hdr, ret) < 0)
269 return AVERROR_INVALIDDATA;
270
271 9349 *duration = h.npcmblocks * DCA_PCMBLOCK_SAMPLES;
272 9349 *sample_rate = ff_dca_sample_rates[h.sr_code];
273
2/2
✓ Branch 0 taken 9273 times.
✓ Branch 1 taken 76 times.
9349 if (*profile != AV_PROFILE_UNKNOWN)
274 9273 return 0;
275
276 76 *profile = AV_PROFILE_DTS;
277
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 58 times.
76 if (h.ext_audio_present) {
278
2/3
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
18 switch (h.ext_audio_type) {
279 11 case DCA_EXT_AUDIO_XCH:
280 case DCA_EXT_AUDIO_XXCH:
281 11 *profile = AV_PROFILE_DTS_ES;
282 11 break;
283 7 case DCA_EXT_AUDIO_X96:
284 7 *profile = AV_PROFILE_DTS_96_24;
285 7 break;
286 }
287 }
288
289 76 frame_size = FFALIGN(h.frame_size, 4);
290
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 62 times.
76 if (buf_size - 4 < frame_size)
291 14 return 0;
292
293 62 buf += frame_size;
294 62 buf_size -= frame_size;
295
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 61 times.
62 if (AV_RB32(buf) != DCA_SYNCWORD_SUBSTREAM)
296 1 return 0;
297
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
61 if (ff_dca_exss_parse(&pc1->exss, buf, buf_size) < 0)
298 return 0;
299
300
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 8 times.
61 if (asset->extension_mask & DCA_EXSS_XLL)
301 53 *profile = AV_PROFILE_DTS_HD_MA;
302
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))
303 8 *profile = AV_PROFILE_DTS_HD_HRA;
304
305 61 return 0;
306 }
307
308 45752 static int dca_parse(AVCodecParserContext *s, AVCodecContext *avctx,
309 const uint8_t **poutbuf, int *poutbuf_size,
310 const uint8_t *buf, int buf_size)
311 {
312 45752 DCAParseContext *pc1 = s->priv_data;
313 45752 ParseContext *pc = &pc1->pc;
314 int next, duration, sample_rate;
315
316
2/2
✓ Branch 0 taken 1820 times.
✓ Branch 1 taken 43932 times.
45752 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
317 1820 next = buf_size;
318 } else {
319 43932 next = dca_find_frame_end(pc1, buf, buf_size);
320
321
2/2
✓ Branch 1 taken 36302 times.
✓ Branch 2 taken 7630 times.
43932 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
322 36302 *poutbuf = NULL;
323 36302 *poutbuf_size = 0;
324 36302 return buf_size;
325 }
326
327 /* skip initial padding */
328
2/2
✓ Branch 0 taken 7550 times.
✓ Branch 1 taken 80 times.
7630 if (buf_size > pc1->startpos) {
329 7550 buf += pc1->startpos;
330 7550 buf_size -= pc1->startpos;
331 }
332 7630 pc1->startpos = 0;
333 }
334
335 /* read the duration and sample rate from the frame header */
336
2/2
✓ Branch 1 taken 9367 times.
✓ Branch 2 taken 83 times.
9450 if (!dca_parse_params(pc1, buf, buf_size, &duration, &sample_rate, &avctx->profile)) {
337
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 9356 times.
9367 if (!avctx->sample_rate)
338 11 avctx->sample_rate = sample_rate;
339 9367 s->duration = av_rescale(duration, avctx->sample_rate, sample_rate);
340 } else
341 83 s->duration = 0;
342
343 9450 *poutbuf = buf;
344 9450 *poutbuf_size = buf_size;
345 9450 return next;
346 }
347
348 const FFCodecParser ff_dca_parser = {
349 PARSER_CODEC_LIST(AV_CODEC_ID_DTS),
350 .priv_data_size = sizeof(DCAParseContext),
351 .init = dca_parse_init,
352 .parse = dca_parse,
353 .close = ff_parse_close,
354 };
355