Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/dca_core.c |
Date: | 2022-07-07 01:21:54 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 904 | 1253 | 72.1% |
Branches: | 632 | 954 | 66.2% |
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/channel_layout.h" | ||
22 | #include "dcaadpcm.h" | ||
23 | #include "dcadec.h" | ||
24 | #include "dcadata.h" | ||
25 | #include "dcahuff.h" | ||
26 | #include "dcamath.h" | ||
27 | #include "dca_syncwords.h" | ||
28 | #include "internal.h" | ||
29 | |||
30 | #if ARCH_ARM | ||
31 | #include "arm/dca.h" | ||
32 | #endif | ||
33 | |||
34 | enum HeaderType { | ||
35 | HEADER_CORE, | ||
36 | HEADER_XCH, | ||
37 | HEADER_XXCH | ||
38 | }; | ||
39 | |||
40 | static const int8_t prm_ch_to_spkr_map[DCA_AMODE_COUNT][5] = { | ||
41 | { DCA_SPEAKER_C, -1, -1, -1, -1 }, | ||
42 | { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, | ||
43 | { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, | ||
44 | { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, | ||
45 | { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 }, | ||
46 | { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , -1, -1 }, | ||
47 | { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Cs, -1, -1 }, | ||
48 | { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , DCA_SPEAKER_Cs, -1 }, | ||
49 | { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs, -1 }, | ||
50 | { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs } | ||
51 | }; | ||
52 | |||
53 | static const uint8_t audio_mode_ch_mask[DCA_AMODE_COUNT] = { | ||
54 | DCA_SPEAKER_LAYOUT_MONO, | ||
55 | DCA_SPEAKER_LAYOUT_STEREO, | ||
56 | DCA_SPEAKER_LAYOUT_STEREO, | ||
57 | DCA_SPEAKER_LAYOUT_STEREO, | ||
58 | DCA_SPEAKER_LAYOUT_STEREO, | ||
59 | DCA_SPEAKER_LAYOUT_3_0, | ||
60 | DCA_SPEAKER_LAYOUT_2_1, | ||
61 | DCA_SPEAKER_LAYOUT_3_1, | ||
62 | DCA_SPEAKER_LAYOUT_2_2, | ||
63 | DCA_SPEAKER_LAYOUT_5POINT0 | ||
64 | }; | ||
65 | |||
66 | static const uint8_t block_code_nbits[7] = { | ||
67 | 7, 10, 12, 13, 15, 17, 19 | ||
68 | }; | ||
69 | |||
70 | 387206 | static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i) | |
71 | { | ||
72 | 387206 | return get_vlc2(s, v->vlc[i].table, v->vlc[i].bits, v->max_depth) + v->offset; | |
73 | } | ||
74 | |||
75 | 289302 | static void get_array(GetBitContext *s, int32_t *array, int size, int n) | |
76 | { | ||
77 | int i; | ||
78 | |||
79 |
2/2✓ Branch 0 taken 2314416 times.
✓ Branch 1 taken 289302 times.
|
2603718 | for (i = 0; i < size; i++) |
80 | 2314416 | array[i] = get_sbits(s, n); | |
81 | 289302 | } | |
82 | |||
83 | // 5.3.1 - Bit stream header | ||
84 | 2464 | static int parse_frame_header(DCACoreDecoder *s) | |
85 | { | ||
86 | 2464 | DCACoreFrameHeader h = { 0 }; | |
87 | 2464 | int err = ff_dca_parse_core_frame_header(&h, &s->gb); | |
88 | |||
89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
|
2464 | if (err < 0) { |
90 | ✗ | switch (err) { | |
91 | ✗ | case DCA_PARSE_ERROR_DEFICIT_SAMPLES: | |
92 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n"); | |
93 | ✗ | return h.normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME; | |
94 | |||
95 | ✗ | case DCA_PARSE_ERROR_PCM_BLOCKS: | |
96 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", h.npcmblocks); | |
97 | ✗ | return (h.npcmblocks < 6 || h.normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME; | |
98 | |||
99 | ✗ | case DCA_PARSE_ERROR_FRAME_SIZE: | |
100 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", h.frame_size); | |
101 | ✗ | return AVERROR_INVALIDDATA; | |
102 | |||
103 | ✗ | case DCA_PARSE_ERROR_AMODE: | |
104 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", h.audio_mode); | |
105 | ✗ | return AVERROR_PATCHWELCOME; | |
106 | |||
107 | ✗ | case DCA_PARSE_ERROR_SAMPLE_RATE: | |
108 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n"); | |
109 | ✗ | return AVERROR_INVALIDDATA; | |
110 | |||
111 | ✗ | case DCA_PARSE_ERROR_RESERVED_BIT: | |
112 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Reserved bit set\n"); | |
113 | ✗ | return AVERROR_INVALIDDATA; | |
114 | |||
115 | ✗ | case DCA_PARSE_ERROR_LFE_FLAG: | |
116 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n"); | |
117 | ✗ | return AVERROR_INVALIDDATA; | |
118 | |||
119 | ✗ | case DCA_PARSE_ERROR_PCM_RES: | |
120 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n"); | |
121 | ✗ | return AVERROR_INVALIDDATA; | |
122 | |||
123 | ✗ | default: | |
124 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Unknown core frame header error\n"); | |
125 | ✗ | return AVERROR_INVALIDDATA; | |
126 | } | ||
127 | } | ||
128 | |||
129 | 2464 | s->crc_present = h.crc_present; | |
130 | 2464 | s->npcmblocks = h.npcmblocks; | |
131 | 2464 | s->frame_size = h.frame_size; | |
132 | 2464 | s->audio_mode = h.audio_mode; | |
133 | 2464 | s->sample_rate = ff_dca_sample_rates[h.sr_code]; | |
134 | 2464 | s->bit_rate = ff_dca_bit_rates[h.br_code]; | |
135 | 2464 | s->drc_present = h.drc_present; | |
136 | 2464 | s->ts_present = h.ts_present; | |
137 | 2464 | s->aux_present = h.aux_present; | |
138 | 2464 | s->ext_audio_type = h.ext_audio_type; | |
139 | 2464 | s->ext_audio_present = h.ext_audio_present; | |
140 | 2464 | s->sync_ssf = h.sync_ssf; | |
141 | 2464 | s->lfe_present = h.lfe_present; | |
142 | 2464 | s->predictor_history = h.predictor_history; | |
143 | 2464 | s->filter_perfect = h.filter_perfect; | |
144 | 2464 | s->source_pcm_res = ff_dca_bits_per_sample[h.pcmr_code]; | |
145 | 2464 | s->es_format = h.pcmr_code & 1; | |
146 | 2464 | s->sumdiff_front = h.sumdiff_front; | |
147 | 2464 | s->sumdiff_surround = h.sumdiff_surround; | |
148 | |||
149 | 2464 | return 0; | |
150 | } | ||
151 | |||
152 | // 5.3.2 - Primary audio coding header | ||
153 | 2764 | static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base) | |
154 | { | ||
155 | 2764 | int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb); | |
156 | unsigned int mask, index; | ||
157 | |||
158 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
|
2764 | if (get_bits_left(&s->gb) < 0) |
159 | ✗ | return AVERROR_INVALIDDATA; | |
160 | |||
161 |
3/4✓ Branch 0 taken 2464 times.
✓ Branch 1 taken 279 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
2764 | switch (header) { |
162 | 2464 | case HEADER_CORE: | |
163 | // Number of subframes | ||
164 | 2464 | s->nsubframes = get_bits(&s->gb, 4) + 1; | |
165 | |||
166 | // Number of primary audio channels | ||
167 | 2464 | s->nchannels = get_bits(&s->gb, 3) + 1; | |
168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
|
2464 | if (s->nchannels != ff_dca_channels[s->audio_mode]) { |
169 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid number of primary audio channels (%d) for audio channel arrangement (%d)\n", s->nchannels, s->audio_mode); | |
170 | ✗ | return AVERROR_INVALIDDATA; | |
171 | } | ||
172 | av_assert1(s->nchannels <= DCA_CHANNELS - 2); | ||
173 | |||
174 | 2464 | s->ch_mask = audio_mode_ch_mask[s->audio_mode]; | |
175 | |||
176 | // Add LFE channel if present | ||
177 |
2/2✓ Branch 0 taken 1946 times.
✓ Branch 1 taken 518 times.
|
2464 | if (s->lfe_present) |
178 | 1946 | s->ch_mask |= DCA_SPEAKER_MASK_LFE1; | |
179 | 2464 | break; | |
180 | |||
181 | 279 | case HEADER_XCH: | |
182 | 279 | s->nchannels = ff_dca_channels[s->audio_mode] + 1; | |
183 | av_assert1(s->nchannels <= DCA_CHANNELS - 1); | ||
184 | 279 | s->ch_mask |= DCA_SPEAKER_MASK_Cs; | |
185 | 279 | break; | |
186 | |||
187 | 21 | case HEADER_XXCH: | |
188 | // Channel set header length | ||
189 | 21 | header_size = get_bits(&s->gb, 7) + 1; | |
190 | |||
191 | // Check CRC | ||
192 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 | if (s->xxch_crc_present |
193 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) { |
194 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n"); | |
195 | ✗ | return AVERROR_INVALIDDATA; | |
196 | } | ||
197 | |||
198 | // Number of channels in a channel set | ||
199 | 21 | nchannels = get_bits(&s->gb, 3) + 1; | |
200 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (nchannels > DCA_XXCH_CHANNELS_MAX) { |
201 | ✗ | avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels); | |
202 | ✗ | return AVERROR_PATCHWELCOME; | |
203 | } | ||
204 | 21 | s->nchannels = ff_dca_channels[s->audio_mode] + nchannels; | |
205 | av_assert1(s->nchannels <= DCA_CHANNELS); | ||
206 | |||
207 | // Loudspeaker layout mask | ||
208 | 21 | mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs); | |
209 | 21 | s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs; | |
210 | |||
211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (av_popcount(s->xxch_spkr_mask) != nchannels) { |
212 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask); | |
213 | ✗ | return AVERROR_INVALIDDATA; | |
214 | } | ||
215 | |||
216 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (s->xxch_core_mask & s->xxch_spkr_mask) { |
217 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "XXCH speaker layout mask (%#x) overlaps with core (%#x)\n", s->xxch_spkr_mask, s->xxch_core_mask); | |
218 | ✗ | return AVERROR_INVALIDDATA; | |
219 | } | ||
220 | |||
221 | // Combine core and XXCH masks together | ||
222 | 21 | s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask; | |
223 | |||
224 | // Downmix coefficients present in stream | ||
225 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | if (get_bits1(&s->gb)) { |
226 | 21 | int *coeff_ptr = s->xxch_dmix_coeff; | |
227 | |||
228 | // Downmix already performed by encoder | ||
229 | 21 | s->xxch_dmix_embedded = get_bits1(&s->gb); | |
230 | |||
231 | // Downmix scale factor | ||
232 | 21 | index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3; | |
233 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (index >= FF_DCA_INV_DMIXTABLE_SIZE) { |
234 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index); | |
235 | ✗ | return AVERROR_INVALIDDATA; | |
236 | } | ||
237 | 21 | s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index]; | |
238 | |||
239 | // Downmix channel mapping mask | ||
240 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
|
63 | for (ch = 0; ch < nchannels; ch++) { |
241 | 42 | mask = get_bits_long(&s->gb, s->xxch_mask_nbits); | |
242 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if ((mask & s->xxch_core_mask) != mask) { |
243 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask); | |
244 | ✗ | return AVERROR_INVALIDDATA; | |
245 | } | ||
246 | 42 | s->xxch_dmix_mask[ch] = mask; | |
247 | } | ||
248 | |||
249 | // Downmix coefficients | ||
250 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
|
63 | for (ch = 0; ch < nchannels; ch++) { |
251 |
2/2✓ Branch 0 taken 462 times.
✓ Branch 1 taken 42 times.
|
504 | for (n = 0; n < s->xxch_mask_nbits; n++) { |
252 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 420 times.
|
462 | if (s->xxch_dmix_mask[ch] & (1U << n)) { |
253 | 42 | int code = get_bits(&s->gb, 7); | |
254 | 42 | int sign = (code >> 6) - 1; | |
255 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (code &= 63) { |
256 | 42 | index = code * 4 - 3; | |
257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (index >= FF_DCA_DMIXTABLE_SIZE) { |
258 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index); | |
259 | ✗ | return AVERROR_INVALIDDATA; | |
260 | } | ||
261 | 42 | *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign; | |
262 | } else { | ||
263 | ✗ | *coeff_ptr++ = 0; | |
264 | } | ||
265 | } | ||
266 | } | ||
267 | } | ||
268 | } else { | ||
269 | ✗ | s->xxch_dmix_embedded = 0; | |
270 | } | ||
271 | |||
272 | 21 | break; | |
273 | } | ||
274 | |||
275 | // Subband activity count | ||
276 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
277 | 11087 | s->nsubbands[ch] = get_bits(&s->gb, 5) + 2; | |
278 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11087 times.
|
11087 | if (s->nsubbands[ch] > DCA_SUBBANDS) { |
279 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n"); | |
280 | ✗ | return AVERROR_INVALIDDATA; | |
281 | } | ||
282 | } | ||
283 | |||
284 | // High frequency VQ start subband | ||
285 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) |
286 | 11087 | s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1; | |
287 | |||
288 | // Joint intensity coding index | ||
289 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
290 |
3/4✓ Branch 1 taken 14 times.
✓ Branch 2 taken 11073 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
|
11087 | if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH) |
291 | ✗ | n += xch_base - 1; | |
292 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11087 times.
|
11087 | if (n > s->nchannels) { |
293 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n"); | |
294 | ✗ | return AVERROR_INVALIDDATA; | |
295 | } | ||
296 | 11087 | s->joint_intensity_index[ch] = n; | |
297 | } | ||
298 | |||
299 | // Transient mode code book | ||
300 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) |
301 | 11087 | s->transition_mode_sel[ch] = get_bits(&s->gb, 2); | |
302 | |||
303 | // Scale factor code book | ||
304 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
305 | 11087 | s->scale_factor_sel[ch] = get_bits(&s->gb, 3); | |
306 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11087 times.
|
11087 | if (s->scale_factor_sel[ch] == 7) { |
307 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n"); | |
308 | ✗ | return AVERROR_INVALIDDATA; | |
309 | } | ||
310 | } | ||
311 | |||
312 | // Bit allocation quantizer select | ||
313 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
314 | 11087 | s->bit_allocation_sel[ch] = get_bits(&s->gb, 3); | |
315 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11087 times.
|
11087 | if (s->bit_allocation_sel[ch] == 7) { |
316 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n"); | |
317 | ✗ | return AVERROR_INVALIDDATA; | |
318 | } | ||
319 | } | ||
320 | |||
321 | // Quantization index codebook select | ||
322 |
2/2✓ Branch 0 taken 27640 times.
✓ Branch 1 taken 2764 times.
|
30404 | for (n = 0; n < DCA_CODE_BOOKS; n++) |
323 |
2/2✓ Branch 0 taken 110870 times.
✓ Branch 1 taken 27640 times.
|
138510 | for (ch = xch_base; ch < s->nchannels; ch++) |
324 | 110870 | s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]); | |
325 | |||
326 | // Scale factor adjustment index | ||
327 |
2/2✓ Branch 0 taken 27640 times.
✓ Branch 1 taken 2764 times.
|
30404 | for (n = 0; n < DCA_CODE_BOOKS; n++) |
328 |
2/2✓ Branch 0 taken 110870 times.
✓ Branch 1 taken 27640 times.
|
138510 | for (ch = xch_base; ch < s->nchannels; ch++) |
329 |
2/2✓ Branch 0 taken 2922 times.
✓ Branch 1 taken 107948 times.
|
110870 | if (s->quant_index_sel[ch][n] < ff_dca_quant_index_group_size[n]) |
330 | 2922 | s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)]; | |
331 | |||
332 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2743 times.
|
2764 | if (header == HEADER_XXCH) { |
333 | // Reserved | ||
334 | // Byte align | ||
335 | // CRC16 of channel set header | ||
336 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) { |
337 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n"); | |
338 | ✗ | return AVERROR_INVALIDDATA; | |
339 | } | ||
340 | } else { | ||
341 | // Audio header CRC check word | ||
342 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2743 times.
|
2743 | if (s->crc_present) |
343 | ✗ | skip_bits(&s->gb, 16); | |
344 | } | ||
345 | |||
346 | 2764 | return 0; | |
347 | } | ||
348 | |||
349 | 360692 | static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel) | |
350 | { | ||
351 | const uint32_t *scale_table; | ||
352 | unsigned int scale_size; | ||
353 | |||
354 | // Select the root square table | ||
355 |
2/2✓ Branch 0 taken 290350 times.
✓ Branch 1 taken 70342 times.
|
360692 | if (sel > 5) { |
356 | 290350 | scale_table = ff_dca_scale_factor_quant7; | |
357 | 290350 | scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7); | |
358 | } else { | ||
359 | 70342 | scale_table = ff_dca_scale_factor_quant6; | |
360 | 70342 | scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6); | |
361 | } | ||
362 | |||
363 | // If Huffman code was used, the difference of scales was encoded | ||
364 |
2/2✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 351557 times.
|
360692 | if (sel < 5) |
365 | 9135 | *scale_index += dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel); | |
366 | else | ||
367 | 351557 | *scale_index = get_bits(&s->gb, sel + 1); | |
368 | |||
369 | // Look up scale factor from the root square table | ||
370 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 360692 times.
|
360692 | if ((unsigned int)*scale_index >= scale_size) { |
371 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n"); | |
372 | ✗ | return AVERROR_INVALIDDATA; | |
373 | } | ||
374 | |||
375 | 360692 | return scale_table[*scale_index]; | |
376 | } | ||
377 | |||
378 | 2541 | static inline int parse_joint_scale(DCACoreDecoder *s, int sel) | |
379 | { | ||
380 | int scale_index; | ||
381 | |||
382 | // Absolute value was encoded even when Huffman code was used | ||
383 |
1/2✓ Branch 0 taken 2541 times.
✗ Branch 1 not taken.
|
2541 | if (sel < 5) |
384 | 2541 | scale_index = dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel); | |
385 | else | ||
386 | ✗ | scale_index = get_bits(&s->gb, sel + 1); | |
387 | |||
388 | // Bias by 64 | ||
389 | 2541 | scale_index += 64; | |
390 | |||
391 | // Look up joint scale factor | ||
392 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2541 times.
|
2541 | if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) { |
393 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n"); | |
394 | ✗ | return AVERROR_INVALIDDATA; | |
395 | } | ||
396 | |||
397 | 2541 | return ff_dca_joint_scale_factors[scale_index]; | |
398 | } | ||
399 | |||
400 | // 5.4.1 - Primary audio coding side information | ||
401 | 2764 | static int parse_subframe_header(DCACoreDecoder *s, int sf, | |
402 | enum HeaderType header, int xch_base) | ||
403 | { | ||
404 | int ch, band, ret; | ||
405 | |||
406 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
|
2764 | if (get_bits_left(&s->gb) < 0) |
407 | ✗ | return AVERROR_INVALIDDATA; | |
408 | |||
409 |
2/2✓ Branch 0 taken 2464 times.
✓ Branch 1 taken 300 times.
|
2764 | if (header == HEADER_CORE) { |
410 | // Subsubframe count | ||
411 | 2464 | s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1; | |
412 | |||
413 | // Partial subsubframe sample count | ||
414 | 2464 | skip_bits(&s->gb, 3); | |
415 | } | ||
416 | |||
417 | // Prediction mode | ||
418 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) |
419 |
2/2✓ Branch 0 taken 341543 times.
✓ Branch 1 taken 11087 times.
|
352630 | for (band = 0; band < s->nsubbands[ch]; band++) |
420 | 341543 | s->prediction_mode[ch][band] = get_bits1(&s->gb); | |
421 | |||
422 | // Prediction coefficients VQ address | ||
423 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) |
424 |
2/2✓ Branch 0 taken 341543 times.
✓ Branch 1 taken 11087 times.
|
352630 | for (band = 0; band < s->nsubbands[ch]; band++) |
425 |
2/2✓ Branch 0 taken 24484 times.
✓ Branch 1 taken 317059 times.
|
341543 | if (s->prediction_mode[ch][band]) |
426 | 24484 | s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12); | |
427 | |||
428 | // Bit allocation index | ||
429 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
430 | 11087 | int sel = s->bit_allocation_sel[ch]; | |
431 | |||
432 |
2/2✓ Branch 0 taken 298239 times.
✓ Branch 1 taken 11087 times.
|
309326 | for (band = 0; band < s->subband_vq_start[ch]; band++) { |
433 | int abits; | ||
434 | |||
435 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 298239 times.
|
298239 | if (sel < 5) |
436 | ✗ | abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation, sel); | |
437 | else | ||
438 | 298239 | abits = get_bits(&s->gb, sel - 1); | |
439 | |||
440 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 298239 times.
|
298239 | if (abits > DCA_ABITS_MAX) { |
441 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n"); | |
442 | ✗ | return AVERROR_INVALIDDATA; | |
443 | } | ||
444 | |||
445 | 298239 | s->bit_allocation[ch][band] = abits; | |
446 | } | ||
447 | } | ||
448 | |||
449 | // Transition mode | ||
450 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
451 | // Clear transition mode for all subbands | ||
452 | 11087 | memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0])); | |
453 | |||
454 | // Transient possible only if more than one subsubframe | ||
455 |
1/2✓ Branch 0 taken 11087 times.
✗ Branch 1 not taken.
|
11087 | if (s->nsubsubframes[sf] > 1) { |
456 | 11087 | int sel = s->transition_mode_sel[ch]; | |
457 |
2/2✓ Branch 0 taken 298239 times.
✓ Branch 1 taken 11087 times.
|
309326 | for (band = 0; band < s->subband_vq_start[ch]; band++) |
458 |
1/2✓ Branch 0 taken 298239 times.
✗ Branch 1 not taken.
|
298239 | if (s->bit_allocation[ch][band]) |
459 | 298239 | s->transition_mode[sf][ch][band] = dca_get_vlc(&s->gb, &ff_dca_vlc_transition_mode, sel); | |
460 | } | ||
461 | } | ||
462 | |||
463 | // Scale factors | ||
464 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
465 | 11087 | int sel = s->scale_factor_sel[ch]; | |
466 | 11087 | int scale_index = 0; | |
467 | |||
468 | // Extract scales for subbands up to VQ | ||
469 |
2/2✓ Branch 0 taken 298239 times.
✓ Branch 1 taken 11087 times.
|
309326 | for (band = 0; band < s->subband_vq_start[ch]; band++) { |
470 |
1/2✓ Branch 0 taken 298239 times.
✗ Branch 1 not taken.
|
298239 | if (s->bit_allocation[ch][band]) { |
471 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 298239 times.
|
298239 | if ((ret = parse_scale(s, &scale_index, sel)) < 0) |
472 | ✗ | return ret; | |
473 | 298239 | s->scale_factors[ch][band][0] = ret; | |
474 |
2/2✓ Branch 0 taken 10014 times.
✓ Branch 1 taken 288225 times.
|
298239 | if (s->transition_mode[sf][ch][band]) { |
475 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10014 times.
|
10014 | if ((ret = parse_scale(s, &scale_index, sel)) < 0) |
476 | ✗ | return ret; | |
477 | 10014 | s->scale_factors[ch][band][1] = ret; | |
478 | } | ||
479 | } else { | ||
480 | ✗ | s->scale_factors[ch][band][0] = 0; | |
481 | } | ||
482 | } | ||
483 | |||
484 | // High frequency VQ subbands | ||
485 |
2/2✓ Branch 0 taken 43304 times.
✓ Branch 1 taken 11087 times.
|
54391 | for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) { |
486 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 43304 times.
|
43304 | if ((ret = parse_scale(s, &scale_index, sel)) < 0) |
487 | ✗ | return ret; | |
488 | 43304 | s->scale_factors[ch][band][0] = ret; | |
489 | } | ||
490 | } | ||
491 | |||
492 | // Joint subband codebook select | ||
493 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
494 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 11073 times.
|
11087 | if (s->joint_intensity_index[ch]) { |
495 | 14 | s->joint_scale_sel[ch] = get_bits(&s->gb, 3); | |
496 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (s->joint_scale_sel[ch] == 7) { |
497 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor code book\n"); | |
498 | ✗ | return AVERROR_INVALIDDATA; | |
499 | } | ||
500 | } | ||
501 | } | ||
502 | |||
503 | // Scale factors for joint subband coding | ||
504 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
505 | 11087 | int src_ch = s->joint_intensity_index[ch] - 1; | |
506 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 11073 times.
|
11087 | if (src_ch >= 0) { |
507 | 14 | int sel = s->joint_scale_sel[ch]; | |
508 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 14 times.
|
91 | for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) { |
509 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
|
77 | if ((ret = parse_joint_scale(s, sel)) < 0) |
510 | ✗ | return ret; | |
511 | 77 | s->joint_scale_factors[ch][band] = ret; | |
512 | } | ||
513 | } | ||
514 | } | ||
515 | |||
516 | // Dynamic range coefficient | ||
517 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2764 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2764 | if (s->drc_present && header == HEADER_CORE) |
518 | ✗ | skip_bits(&s->gb, 8); | |
519 | |||
520 | // Side information CRC check word | ||
521 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2764 times.
|
2764 | if (s->crc_present) |
522 | ✗ | skip_bits(&s->gb, 16); | |
523 | |||
524 | 2764 | return 0; | |
525 | } | ||
526 | |||
527 | #ifndef decode_blockcodes | ||
528 | 318078 | static inline int decode_blockcodes(int code1, int code2, int levels, int32_t *audio) | |
529 | { | ||
530 | 318078 | int offset = (levels - 1) / 2; | |
531 | int n, div; | ||
532 | |||
533 |
2/2✓ Branch 0 taken 1272312 times.
✓ Branch 1 taken 318078 times.
|
1590390 | for (n = 0; n < DCA_SUBBAND_SAMPLES / 2; n++) { |
534 | 1272312 | div = FASTDIV(code1, levels); | |
535 | 1272312 | audio[n] = code1 - div * levels - offset; | |
536 | 1272312 | code1 = div; | |
537 | } | ||
538 |
2/2✓ Branch 0 taken 1272312 times.
✓ Branch 1 taken 318078 times.
|
1590390 | for (; n < DCA_SUBBAND_SAMPLES; n++) { |
539 | 1272312 | div = FASTDIV(code2, levels); | |
540 | 1272312 | audio[n] = code2 - div * levels - offset; | |
541 | 1272312 | code2 = div; | |
542 | } | ||
543 | |||
544 | 318078 | return code1 | code2; | |
545 | } | ||
546 | #endif | ||
547 | |||
548 | 318078 | static inline int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits) | |
549 | { | ||
550 | // Extract block code indices from the bit stream | ||
551 | 318078 | int code1 = get_bits(&s->gb, block_code_nbits[abits - 1]); | |
552 | 318078 | int code2 = get_bits(&s->gb, block_code_nbits[abits - 1]); | |
553 | 318078 | int levels = ff_dca_quant_levels[abits]; | |
554 | |||
555 | // Look up samples from the block code book | ||
556 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 318078 times.
|
318078 | if (decode_blockcodes(code1, code2, levels, audio)) { |
557 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Failed to decode block code(s)\n"); | |
558 | ✗ | return AVERROR_INVALIDDATA; | |
559 | } | ||
560 | |||
561 | 318078 | return 0; | |
562 | } | ||
563 | |||
564 | 8522 | static inline int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel) | |
565 | { | ||
566 | int i; | ||
567 | |||
568 | // Extract Huffman codes from the bit stream | ||
569 |
2/2✓ Branch 0 taken 68176 times.
✓ Branch 1 taken 8522 times.
|
76698 | for (i = 0; i < DCA_SUBBAND_SAMPLES; i++) |
570 | 68176 | audio[i] = dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[abits - 1], sel); | |
571 | |||
572 | 8522 | return 1; | |
573 | } | ||
574 | |||
575 | 609294 | static inline int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch) | |
576 | { | ||
577 | av_assert1(abits >= 0 && abits <= DCA_ABITS_MAX); | ||
578 | |||
579 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 609294 times.
|
609294 | if (abits == 0) { |
580 | // No bits allocated | ||
581 | ✗ | memset(audio, 0, DCA_SUBBAND_SAMPLES * sizeof(*audio)); | |
582 | ✗ | return 0; | |
583 | } | ||
584 | |||
585 |
2/2✓ Branch 0 taken 444098 times.
✓ Branch 1 taken 165196 times.
|
609294 | if (abits <= DCA_CODE_BOOKS) { |
586 | 444098 | int sel = s->quant_index_sel[ch][abits - 1]; | |
587 |
2/2✓ Branch 0 taken 8522 times.
✓ Branch 1 taken 435576 times.
|
444098 | if (sel < ff_dca_quant_index_group_size[abits - 1]) { |
588 | // Huffman codes | ||
589 | 8522 | return parse_huffman_codes(s, audio, abits, sel); | |
590 | } | ||
591 |
2/2✓ Branch 0 taken 316984 times.
✓ Branch 1 taken 118592 times.
|
435576 | if (abits <= 7) { |
592 | // Block codes | ||
593 | 316984 | return parse_block_codes(s, audio, abits); | |
594 | } | ||
595 | } | ||
596 | |||
597 | // No further encoding | ||
598 | 283788 | get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3); | |
599 | 283788 | return 0; | |
600 | } | ||
601 | |||
602 | 11283 | static inline void inverse_adpcm(int32_t **subband_samples, | |
603 | const int16_t *vq_index, | ||
604 | const int8_t *prediction_mode, | ||
605 | int sb_start, int sb_end, | ||
606 | int ofs, int len) | ||
607 | { | ||
608 | int i, j; | ||
609 | |||
610 |
2/2✓ Branch 0 taken 350678 times.
✓ Branch 1 taken 11283 times.
|
361961 | for (i = sb_start; i < sb_end; i++) { |
611 |
2/2✓ Branch 0 taken 24531 times.
✓ Branch 1 taken 326147 times.
|
350678 | if (prediction_mode[i]) { |
612 | 24531 | const int pred_id = vq_index[i]; | |
613 | 24531 | int32_t *ptr = subband_samples[i] + ofs; | |
614 |
2/2✓ Branch 0 taken 392496 times.
✓ Branch 1 taken 24531 times.
|
417027 | for (j = 0; j < len; j++) { |
615 | 392496 | int32_t x = ff_dcaadpcm_predict(pred_id, ptr + j - DCA_ADPCM_COEFFS); | |
616 | 392496 | ptr[j] = clip23(ptr[j] + x); | |
617 | } | ||
618 | } | ||
619 | } | ||
620 | 11283 | } | |
621 | |||
622 | // 5.5 - Primary audio data arrays | ||
623 | 2764 | static int parse_subframe_audio(DCACoreDecoder *s, int sf, enum HeaderType header, | |
624 | int xch_base, int *sub_pos, int *lfe_pos) | ||
625 | { | ||
626 | int32_t audio[16], scale; | ||
627 | int n, ssf, ofs, ch, band; | ||
628 | |||
629 | // Check number of subband samples in this subframe | ||
630 | 2764 | int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES; | |
631 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2764 times.
|
2764 | if (*sub_pos + nsamples > s->npcmblocks) { |
632 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n"); | |
633 | ✗ | return AVERROR_INVALIDDATA; | |
634 | } | ||
635 | |||
636 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
|
2764 | if (get_bits_left(&s->gb) < 0) |
637 | ✗ | return AVERROR_INVALIDDATA; | |
638 | |||
639 | // VQ encoded subbands | ||
640 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
641 | int32_t vq_index[DCA_SUBBANDS]; | ||
642 | |||
643 |
2/2✓ Branch 0 taken 43304 times.
✓ Branch 1 taken 11087 times.
|
54391 | for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) |
644 | // Extract the VQ address from the bit stream | ||
645 | 43304 | vq_index[band] = get_bits(&s->gb, 10); | |
646 | |||
647 |
2/2✓ Branch 0 taken 9736 times.
✓ Branch 1 taken 1351 times.
|
11087 | if (s->subband_vq_start[ch] < s->nsubbands[ch]) { |
648 | 9736 | s->dcadsp->decode_hf(s->subband_samples[ch], vq_index, | |
649 | 9736 | ff_dca_high_freq_vq, s->scale_factors[ch], | |
650 | 9736 | s->subband_vq_start[ch], s->nsubbands[ch], | |
651 | 9736 | *sub_pos, nsamples); | |
652 | } | ||
653 | } | ||
654 | |||
655 | // Low frequency effect data | ||
656 |
4/4✓ Branch 0 taken 2246 times.
✓ Branch 1 taken 518 times.
✓ Branch 2 taken 1946 times.
✓ Branch 3 taken 300 times.
|
2764 | if (s->lfe_present && header == HEADER_CORE) { |
657 | unsigned int index; | ||
658 | |||
659 | // Determine number of LFE samples in this subframe | ||
660 | 1946 | int nlfesamples = 2 * s->lfe_present * s->nsubsubframes[sf]; | |
661 | av_assert1((unsigned int)nlfesamples <= FF_ARRAY_ELEMS(audio)); | ||
662 | |||
663 | // Extract LFE samples from the bit stream | ||
664 | 1946 | get_array(&s->gb, audio, nlfesamples, 8); | |
665 | |||
666 | // Extract scale factor index from the bit stream | ||
667 | 1946 | index = get_bits(&s->gb, 8); | |
668 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1946 times.
|
1946 | if (index >= FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7)) { |
669 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE scale factor index\n"); | |
670 | ✗ | return AVERROR_INVALIDDATA; | |
671 | } | ||
672 | |||
673 | // Look up the 7-bit root square quantization table | ||
674 | 1946 | scale = ff_dca_scale_factor_quant7[index]; | |
675 | |||
676 | // Account for quantizer step size which is 0.035 | ||
677 | 1946 | scale = mul23(4697620 /* 0.035 * (1 << 27) */, scale); | |
678 | |||
679 | // Scale and take the LFE samples | ||
680 |
2/2✓ Branch 0 taken 15568 times.
✓ Branch 1 taken 1946 times.
|
17514 | for (n = 0, ofs = *lfe_pos; n < nlfesamples; n++, ofs++) |
681 | 15568 | s->lfe_samples[ofs] = clip23(audio[n] * scale >> 4); | |
682 | |||
683 | // Advance LFE sample pointer for the next subframe | ||
684 | 1946 | *lfe_pos = ofs; | |
685 | } | ||
686 | |||
687 | // Audio data | ||
688 |
2/2✓ Branch 0 taken 5528 times.
✓ Branch 1 taken 2764 times.
|
8292 | for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) { |
689 |
2/2✓ Branch 0 taken 22174 times.
✓ Branch 1 taken 5528 times.
|
27702 | for (ch = xch_base; ch < s->nchannels; ch++) { |
690 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 22174 times.
|
22174 | if (get_bits_left(&s->gb) < 0) |
691 | ✗ | return AVERROR_INVALIDDATA; | |
692 | |||
693 | // Not high frequency VQ subbands | ||
694 |
2/2✓ Branch 0 taken 596478 times.
✓ Branch 1 taken 22174 times.
|
618652 | for (band = 0; band < s->subband_vq_start[ch]; band++) { |
695 | 596478 | int ret, trans_ssf, abits = s->bit_allocation[ch][band]; | |
696 | int32_t step_size; | ||
697 | |||
698 | // Extract bits from the bit stream | ||
699 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 596478 times.
|
596478 | if ((ret = extract_audio(s, audio, abits, ch)) < 0) |
700 | ✗ | return ret; | |
701 | |||
702 | // Select quantization step size table and look up | ||
703 | // quantization step size | ||
704 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 596478 times.
|
596478 | if (s->bit_rate == 3) |
705 | ✗ | step_size = ff_dca_lossless_quant[abits]; | |
706 | else | ||
707 | 596478 | step_size = ff_dca_lossy_quant[abits]; | |
708 | |||
709 | // Identify transient location | ||
710 | 596478 | trans_ssf = s->transition_mode[sf][ch][band]; | |
711 | |||
712 | // Determine proper scale factor | ||
713 |
4/4✓ Branch 0 taken 20028 times.
✓ Branch 1 taken 576450 times.
✓ Branch 2 taken 10014 times.
✓ Branch 3 taken 10014 times.
|
596478 | if (trans_ssf == 0 || ssf < trans_ssf) |
714 | 586464 | scale = s->scale_factors[ch][band][0]; | |
715 | else | ||
716 | 10014 | scale = s->scale_factors[ch][band][1]; | |
717 | |||
718 | // Adjust scale factor when SEL indicates Huffman code | ||
719 |
2/2✓ Branch 0 taken 8054 times.
✓ Branch 1 taken 588424 times.
|
596478 | if (ret > 0) { |
720 | 8054 | int64_t adj = s->scale_factor_adj[ch][abits - 1]; | |
721 | 8054 | scale = clip23(adj * scale >> 22); | |
722 | } | ||
723 | |||
724 | 596478 | ff_dca_core_dequantize(s->subband_samples[ch][band] + ofs, | |
725 | audio, step_size, scale, 0, DCA_SUBBAND_SAMPLES); | ||
726 | } | ||
727 | } | ||
728 | |||
729 | // DSYNC | ||
730 |
5/6✓ Branch 0 taken 2764 times.
✓ Branch 1 taken 2764 times.
✓ Branch 2 taken 2050 times.
✓ Branch 3 taken 714 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4814 times.
|
5528 | if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) { |
731 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "DSYNC check failed\n"); | |
732 | ✗ | return AVERROR_INVALIDDATA; | |
733 | } | ||
734 | |||
735 | 5528 | ofs += DCA_SUBBAND_SAMPLES; | |
736 | } | ||
737 | |||
738 | // Inverse ADPCM | ||
739 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
740 | 11087 | inverse_adpcm(s->subband_samples[ch], s->prediction_vq_index[ch], | |
741 | 11087 | s->prediction_mode[ch], 0, s->nsubbands[ch], | |
742 | *sub_pos, nsamples); | ||
743 | } | ||
744 | |||
745 | // Joint subband coding | ||
746 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
747 | 11087 | int src_ch = s->joint_intensity_index[ch] - 1; | |
748 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 11073 times.
|
11087 | if (src_ch >= 0) { |
749 | 14 | s->dcadsp->decode_joint(s->subband_samples[ch], s->subband_samples[src_ch], | |
750 | 14 | s->joint_scale_factors[ch], s->nsubbands[ch], | |
751 | 14 | s->nsubbands[src_ch], *sub_pos, nsamples); | |
752 | } | ||
753 | } | ||
754 | |||
755 | // Advance subband sample pointer for the next subframe | ||
756 | 2764 | *sub_pos = ofs; | |
757 | 2764 | return 0; | |
758 | } | ||
759 | |||
760 | ✗ | static void erase_adpcm_history(DCACoreDecoder *s) | |
761 | { | ||
762 | int ch, band; | ||
763 | |||
764 | // Erase ADPCM history from previous frame if | ||
765 | // predictor history switch was disabled | ||
766 | ✗ | for (ch = 0; ch < DCA_CHANNELS; ch++) | |
767 | ✗ | for (band = 0; band < DCA_SUBBANDS; band++) | |
768 | ✗ | AV_ZERO128(s->subband_samples[ch][band] - DCA_ADPCM_COEFFS); | |
769 | |||
770 | ✗ | emms_c(); | |
771 | } | ||
772 | |||
773 | 2464 | static int alloc_sample_buffer(DCACoreDecoder *s) | |
774 | { | ||
775 | 2464 | int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks; | |
776 | 2464 | int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS; | |
777 | 2464 | int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2; | |
778 | 2464 | unsigned int size = s->subband_size; | |
779 | int ch, band; | ||
780 | |||
781 | // Reallocate subband sample buffer | ||
782 | 2464 | av_fast_mallocz(&s->subband_buffer, &s->subband_size, | |
783 | 2464 | (nframesamples + nlfesamples) * sizeof(int32_t)); | |
784 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
|
2464 | if (!s->subband_buffer) |
785 | ✗ | return AVERROR(ENOMEM); | |
786 | |||
787 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 2378 times.
|
2464 | if (size != s->subband_size) { |
788 |
2/2✓ Branch 0 taken 602 times.
✓ Branch 1 taken 86 times.
|
688 | for (ch = 0; ch < DCA_CHANNELS; ch++) |
789 |
2/2✓ Branch 0 taken 19264 times.
✓ Branch 1 taken 602 times.
|
19866 | for (band = 0; band < DCA_SUBBANDS; band++) |
790 | 19264 | s->subband_samples[ch][band] = s->subband_buffer + | |
791 | 19264 | (ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS; | |
792 | 86 | s->lfe_samples = s->subband_buffer + nframesamples; | |
793 | } | ||
794 | |||
795 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
|
2464 | if (!s->predictor_history) |
796 | ✗ | erase_adpcm_history(s); | |
797 | |||
798 | 2464 | return 0; | |
799 | } | ||
800 | |||
801 | 2764 | static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base) | |
802 | { | ||
803 | int sf, ch, ret, band, sub_pos, lfe_pos; | ||
804 | |||
805 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
|
2764 | if ((ret = parse_coding_header(s, header, xch_base)) < 0) |
806 | ✗ | return ret; | |
807 | |||
808 |
2/2✓ Branch 0 taken 2764 times.
✓ Branch 1 taken 2764 times.
|
5528 | for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) { |
809 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
|
2764 | if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0) |
810 | ✗ | return ret; | |
811 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
|
2764 | if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0) |
812 | ✗ | return ret; | |
813 | } | ||
814 | |||
815 |
2/2✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
|
13851 | for (ch = xch_base; ch < s->nchannels; ch++) { |
816 | // Determine number of active subbands for this channel | ||
817 | 11087 | int nsubbands = s->nsubbands[ch]; | |
818 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 11073 times.
|
11087 | if (s->joint_intensity_index[ch]) |
819 | 14 | nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]); | |
820 | |||
821 | // Update history for ADPCM | ||
822 |
2/2✓ Branch 0 taken 341620 times.
✓ Branch 1 taken 11087 times.
|
352707 | for (band = 0; band < nsubbands; band++) { |
823 | 341620 | int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS; | |
824 | 341620 | AV_COPY128(samples, samples + s->npcmblocks); | |
825 | } | ||
826 | |||
827 | // Clear inactive subbands | ||
828 |
2/2✓ Branch 0 taken 13164 times.
✓ Branch 1 taken 11087 times.
|
24251 | for (; band < DCA_SUBBANDS; band++) { |
829 | 13164 | int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS; | |
830 | 13164 | memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t)); | |
831 | } | ||
832 | } | ||
833 | |||
834 | 2764 | emms_c(); | |
835 | |||
836 | 2764 | return 0; | |
837 | } | ||
838 | |||
839 | 279 | static int parse_xch_frame(DCACoreDecoder *s) | |
840 | { | ||
841 | int ret; | ||
842 | |||
843 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
|
279 | if (s->ch_mask & DCA_SPEAKER_MASK_Cs) { |
844 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n"); | |
845 | ✗ | return AVERROR_INVALIDDATA; | |
846 | } | ||
847 | |||
848 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 279 times.
|
279 | if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0) |
849 | ✗ | return ret; | |
850 | |||
851 | // Seek to the end of core frame, don't trust XCH frame size | ||
852 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 279 times.
|
279 | if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) { |
853 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n"); | |
854 | ✗ | return AVERROR_INVALIDDATA; | |
855 | } | ||
856 | |||
857 | 279 | return 0; | |
858 | } | ||
859 | |||
860 | 21 | static int parse_xxch_frame(DCACoreDecoder *s) | |
861 | { | ||
862 | int xxch_nchsets, xxch_frame_size; | ||
863 | 21 | int ret, mask, header_size, header_pos = get_bits_count(&s->gb); | |
864 | |||
865 | // XXCH sync word | ||
866 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) { |
867 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n"); | |
868 | ✗ | return AVERROR_INVALIDDATA; | |
869 | } | ||
870 | |||
871 | // XXCH frame header length | ||
872 | 21 | header_size = get_bits(&s->gb, 6) + 1; | |
873 | |||
874 | // Check XXCH frame header CRC | ||
875 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) { |
876 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n"); | |
877 | ✗ | return AVERROR_INVALIDDATA; | |
878 | } | ||
879 | |||
880 | // CRC presence flag for channel set header | ||
881 | 21 | s->xxch_crc_present = get_bits1(&s->gb); | |
882 | |||
883 | // Number of bits for loudspeaker mask | ||
884 | 21 | s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1; | |
885 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) { |
886 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits); | |
887 | ✗ | return AVERROR_INVALIDDATA; | |
888 | } | ||
889 | |||
890 | // Number of channel sets | ||
891 | 21 | xxch_nchsets = get_bits(&s->gb, 2) + 1; | |
892 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (xxch_nchsets > 1) { |
893 | ✗ | avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets); | |
894 | ✗ | return AVERROR_PATCHWELCOME; | |
895 | } | ||
896 | |||
897 | // Channel set 0 data byte size | ||
898 | 21 | xxch_frame_size = get_bits(&s->gb, 14) + 1; | |
899 | |||
900 | // Core loudspeaker activity mask | ||
901 | 21 | s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits); | |
902 | |||
903 | // Validate the core mask | ||
904 | 21 | mask = s->ch_mask; | |
905 | |||
906 |
2/4✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
21 | if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss)) |
907 | 21 | mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss; | |
908 | |||
909 |
2/4✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
21 | if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss)) |
910 | 21 | mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss; | |
911 | |||
912 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (mask != s->xxch_core_mask) { |
913 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask); | |
914 | ✗ | return AVERROR_INVALIDDATA; | |
915 | } | ||
916 | |||
917 | // Reserved | ||
918 | // Byte align | ||
919 | // CRC16 of XXCH frame header | ||
920 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) { |
921 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n"); | |
922 | ✗ | return AVERROR_INVALIDDATA; | |
923 | } | ||
924 | |||
925 | // Parse XXCH channel set 0 | ||
926 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0) |
927 | ✗ | return ret; | |
928 | |||
929 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) { |
930 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n"); | |
931 | ✗ | return AVERROR_INVALIDDATA; | |
932 | } | ||
933 | |||
934 | 21 | return 0; | |
935 | } | ||
936 | |||
937 | 28 | static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels, | |
938 | int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos) | ||
939 | { | ||
940 | int xbr_nabits[DCA_CHANNELS]; | ||
941 | int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS]; | ||
942 | int xbr_scale_nbits[DCA_CHANNELS]; | ||
943 | int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2]; | ||
944 | int ssf, ch, band, ofs; | ||
945 | |||
946 | // Check number of subband samples in this subframe | ||
947 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) { |
948 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n"); | |
949 | ✗ | return AVERROR_INVALIDDATA; | |
950 | } | ||
951 | |||
952 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if (get_bits_left(&s->gb) < 0) |
953 | ✗ | return AVERROR_INVALIDDATA; | |
954 | |||
955 | // Number of bits for XBR bit allocation index | ||
956 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
|
140 | for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) |
957 | 112 | xbr_nabits[ch] = get_bits(&s->gb, 2) + 2; | |
958 | |||
959 | // XBR bit allocation index | ||
960 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
|
140 | for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) { |
961 |
2/2✓ Branch 0 taken 3584 times.
✓ Branch 1 taken 112 times.
|
3696 | for (band = 0; band < xbr_nsubbands[ch]; band++) { |
962 | 3584 | xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]); | |
963 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
|
3584 | if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) { |
964 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n"); | |
965 | ✗ | return AVERROR_INVALIDDATA; | |
966 | } | ||
967 | } | ||
968 | } | ||
969 | |||
970 | // Number of bits for scale indices | ||
971 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
|
140 | for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) { |
972 | 112 | xbr_scale_nbits[ch] = get_bits(&s->gb, 3); | |
973 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if (!xbr_scale_nbits[ch]) { |
974 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n"); | |
975 | ✗ | return AVERROR_INVALIDDATA; | |
976 | } | ||
977 | } | ||
978 | |||
979 | // XBR scale factors | ||
980 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
|
140 | for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) { |
981 | const uint32_t *scale_table; | ||
982 | int scale_size; | ||
983 | |||
984 | // Select the root square table | ||
985 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | if (s->scale_factor_sel[ch] > 5) { |
986 | 112 | scale_table = ff_dca_scale_factor_quant7; | |
987 | 112 | scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7); | |
988 | } else { | ||
989 | ✗ | scale_table = ff_dca_scale_factor_quant6; | |
990 | ✗ | scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6); | |
991 | } | ||
992 | |||
993 | // Parse scale factor indices and look up scale factors from the root | ||
994 | // square table | ||
995 |
2/2✓ Branch 0 taken 3584 times.
✓ Branch 1 taken 112 times.
|
3696 | for (band = 0; band < xbr_nsubbands[ch]; band++) { |
996 |
2/2✓ Branch 0 taken 2331 times.
✓ Branch 1 taken 1253 times.
|
3584 | if (xbr_bit_allocation[ch][band]) { |
997 | 2331 | int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]); | |
998 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2331 times.
|
2331 | if (scale_index >= scale_size) { |
999 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n"); | |
1000 | ✗ | return AVERROR_INVALIDDATA; | |
1001 | } | ||
1002 | 2331 | xbr_scale_factors[ch][band][0] = scale_table[scale_index]; | |
1003 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2331 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2331 | if (xbr_transition_mode && s->transition_mode[sf][ch][band]) { |
1004 | ✗ | scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]); | |
1005 | ✗ | if (scale_index >= scale_size) { | |
1006 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n"); | |
1007 | ✗ | return AVERROR_INVALIDDATA; | |
1008 | } | ||
1009 | ✗ | xbr_scale_factors[ch][band][1] = scale_table[scale_index]; | |
1010 | } | ||
1011 | } | ||
1012 | } | ||
1013 | } | ||
1014 | |||
1015 | // Audio data | ||
1016 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
|
84 | for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) { |
1017 |
2/2✓ Branch 0 taken 224 times.
✓ Branch 1 taken 56 times.
|
280 | for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) { |
1018 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 224 times.
|
224 | if (get_bits_left(&s->gb) < 0) |
1019 | ✗ | return AVERROR_INVALIDDATA; | |
1020 | |||
1021 |
2/2✓ Branch 0 taken 7168 times.
✓ Branch 1 taken 224 times.
|
7392 | for (band = 0; band < xbr_nsubbands[ch]; band++) { |
1022 | 7168 | int ret, trans_ssf, abits = xbr_bit_allocation[ch][band]; | |
1023 | int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale; | ||
1024 | |||
1025 | // Extract bits from the bit stream | ||
1026 |
2/2✓ Branch 0 taken 3568 times.
✓ Branch 1 taken 3600 times.
|
7168 | if (abits > 7) { |
1027 | // No further encoding | ||
1028 | 3568 | get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3); | |
1029 |
2/2✓ Branch 0 taken 1094 times.
✓ Branch 1 taken 2506 times.
|
3600 | } else if (abits > 0) { |
1030 | // Block codes | ||
1031 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1094 times.
|
1094 | if ((ret = parse_block_codes(s, audio, abits)) < 0) |
1032 | ✗ | return ret; | |
1033 | } else { | ||
1034 | // No bits allocated | ||
1035 | 2506 | continue; | |
1036 | } | ||
1037 | |||
1038 | // Look up quantization step size | ||
1039 | 4662 | step_size = ff_dca_lossless_quant[abits]; | |
1040 | |||
1041 | // Identify transient location | ||
1042 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4662 times.
|
4662 | if (xbr_transition_mode) |
1043 | ✗ | trans_ssf = s->transition_mode[sf][ch][band]; | |
1044 | else | ||
1045 | 4662 | trans_ssf = 0; | |
1046 | |||
1047 | // Determine proper scale factor | ||
1048 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 4662 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4662 | if (trans_ssf == 0 || ssf < trans_ssf) |
1049 | 4662 | scale = xbr_scale_factors[ch][band][0]; | |
1050 | else | ||
1051 | ✗ | scale = xbr_scale_factors[ch][band][1]; | |
1052 | |||
1053 | 4662 | ff_dca_core_dequantize(s->subband_samples[ch][band] + ofs, | |
1054 | audio, step_size, scale, 1, DCA_SUBBAND_SAMPLES); | ||
1055 | } | ||
1056 | } | ||
1057 | |||
1058 | // DSYNC | ||
1059 |
4/6✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 56 times.
|
56 | if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) { |
1060 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n"); | |
1061 | ✗ | return AVERROR_INVALIDDATA; | |
1062 | } | ||
1063 | |||
1064 | 56 | ofs += DCA_SUBBAND_SAMPLES; | |
1065 | } | ||
1066 | |||
1067 | // Advance subband sample pointer for the next subframe | ||
1068 | 28 | *sub_pos = ofs; | |
1069 | 28 | return 0; | |
1070 | } | ||
1071 | |||
1072 | 21 | static int parse_xbr_frame(DCACoreDecoder *s) | |
1073 | { | ||
1074 | int xbr_frame_size[DCA_EXSS_CHSETS_MAX]; | ||
1075 | int xbr_nchannels[DCA_EXSS_CHSETS_MAX]; | ||
1076 | int xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX]; | ||
1077 | int xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch; | ||
1078 | 21 | int i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb); | |
1079 | |||
1080 | // XBR sync word | ||
1081 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) { |
1082 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n"); | |
1083 | ✗ | return AVERROR_INVALIDDATA; | |
1084 | } | ||
1085 | |||
1086 | // XBR frame header length | ||
1087 | 21 | header_size = get_bits(&s->gb, 6) + 1; | |
1088 | |||
1089 | // Check XBR frame header CRC | ||
1090 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) { |
1091 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n"); | |
1092 | ✗ | return AVERROR_INVALIDDATA; | |
1093 | } | ||
1094 | |||
1095 | // Number of channel sets | ||
1096 | 21 | xbr_nchsets = get_bits(&s->gb, 2) + 1; | |
1097 | |||
1098 | // Channel set data byte size | ||
1099 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 21 times.
|
49 | for (i = 0; i < xbr_nchsets; i++) |
1100 | 28 | xbr_frame_size[i] = get_bits(&s->gb, 14) + 1; | |
1101 | |||
1102 | // Transition mode flag | ||
1103 | 21 | xbr_transition_mode = get_bits1(&s->gb); | |
1104 | |||
1105 | // Channel set headers | ||
1106 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 21 times.
|
49 | for (i = 0, ch2 = 0; i < xbr_nchsets; i++) { |
1107 | 28 | xbr_nchannels[i] = get_bits(&s->gb, 3) + 1; | |
1108 | 28 | xbr_band_nbits = get_bits(&s->gb, 2) + 5; | |
1109 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
|
140 | for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) { |
1110 | 112 | xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1; | |
1111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
|
112 | if (xbr_nsubbands[ch2] > DCA_SUBBANDS) { |
1112 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]); | |
1113 | ✗ | return AVERROR_INVALIDDATA; | |
1114 | } | ||
1115 | } | ||
1116 | } | ||
1117 | |||
1118 | // Reserved | ||
1119 | // Byte align | ||
1120 | // CRC16 of XBR frame header | ||
1121 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) { |
1122 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n"); | |
1123 | ✗ | return AVERROR_INVALIDDATA; | |
1124 | } | ||
1125 | |||
1126 | // Channel set data | ||
1127 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 21 times.
|
49 | for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) { |
1128 | 28 | header_pos = get_bits_count(&s->gb); | |
1129 | |||
1130 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) { |
1131 | int sf, sub_pos; | ||
1132 | |||
1133 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
|
56 | for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) { |
1134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if ((ret = parse_xbr_subframe(s, xbr_base_ch, |
1135 | 28 | xbr_base_ch + xbr_nchannels[i], | |
1136 | xbr_nsubbands, xbr_transition_mode, | ||
1137 | sf, &sub_pos)) < 0) | ||
1138 | ✗ | return ret; | |
1139 | } | ||
1140 | } | ||
1141 | |||
1142 | 28 | xbr_base_ch += xbr_nchannels[i]; | |
1143 | |||
1144 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) { |
1145 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n"); | |
1146 | ✗ | return AVERROR_INVALIDDATA; | |
1147 | } | ||
1148 | } | ||
1149 | |||
1150 | 21 | return 0; | |
1151 | } | ||
1152 | |||
1153 | // Modified ISO/IEC 9899 linear congruential generator | ||
1154 | // Returns pseudorandom integer in range [-2^30, 2^30 - 1] | ||
1155 | ✗ | static int rand_x96(DCACoreDecoder *s) | |
1156 | { | ||
1157 | ✗ | s->x96_rand = 1103515245U * s->x96_rand + 12345U; | |
1158 | ✗ | return (s->x96_rand & 0x7fffffff) - 0x40000000; | |
1159 | } | ||
1160 | |||
1161 | 49 | static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos) | |
1162 | { | ||
1163 | int n, ssf, ch, band, ofs; | ||
1164 | |||
1165 | // Check number of subband samples in this subframe | ||
1166 | 49 | int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES; | |
1167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | if (*sub_pos + nsamples > s->npcmblocks) { |
1168 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n"); | |
1169 | ✗ | return AVERROR_INVALIDDATA; | |
1170 | } | ||
1171 | |||
1172 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
|
49 | if (get_bits_left(&s->gb) < 0) |
1173 | ✗ | return AVERROR_INVALIDDATA; | |
1174 | |||
1175 | // VQ encoded or unallocated subbands | ||
1176 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1177 |
2/2✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 196 times.
|
9331 | for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) { |
1178 | // Get the sample pointer and scale factor | ||
1179 | 9135 | int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos; | |
1180 | 9135 | int32_t scale = s->scale_factors[ch][band >> 1][band & 1]; | |
1181 | |||
1182 |
2/3✗ Branch 0 not taken.
✓ Branch 1 taken 2727 times.
✓ Branch 2 taken 6408 times.
|
9135 | switch (s->bit_allocation[ch][band]) { |
1183 | ✗ | case 0: // No bits allocated for subband | |
1184 | ✗ | if (scale <= 1) | |
1185 | ✗ | memset(samples, 0, nsamples * sizeof(int32_t)); | |
1186 | ✗ | else for (n = 0; n < nsamples; n++) | |
1187 | // Generate scaled random samples | ||
1188 | ✗ | samples[n] = mul31(rand_x96(s), scale); | |
1189 | ✗ | break; | |
1190 | |||
1191 | 2727 | case 1: // VQ encoded subband | |
1192 |
2/2✓ Branch 0 taken 2727 times.
✓ Branch 1 taken 2727 times.
|
5454 | for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) { |
1193 | // Extract the VQ address from the bit stream and look up | ||
1194 | // the VQ code book for up to 16 subband samples | ||
1195 | 2727 | const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)]; | |
1196 | // Scale and take the samples | ||
1197 |
2/2✓ Branch 0 taken 43632 times.
✓ Branch 1 taken 2727 times.
|
46359 | for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++) |
1198 | 43632 | *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4); | |
1199 | } | ||
1200 | 2727 | break; | |
1201 | } | ||
1202 | } | ||
1203 | } | ||
1204 | |||
1205 | // Audio data | ||
1206 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 49 times.
|
147 | for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) { |
1207 |
2/2✓ Branch 0 taken 392 times.
✓ Branch 1 taken 98 times.
|
490 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1208 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 392 times.
|
392 | if (get_bits_left(&s->gb) < 0) |
1209 | ✗ | return AVERROR_INVALIDDATA; | |
1210 | |||
1211 |
2/2✓ Branch 0 taken 18270 times.
✓ Branch 1 taken 392 times.
|
18662 | for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) { |
1212 | 18270 | int ret, abits = s->bit_allocation[ch][band] - 1; | |
1213 | int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale; | ||
1214 | |||
1215 | // Not VQ encoded or unallocated subbands | ||
1216 |
2/2✓ Branch 0 taken 5454 times.
✓ Branch 1 taken 12816 times.
|
18270 | if (abits < 1) |
1217 | 5454 | continue; | |
1218 | |||
1219 | // Extract bits from the bit stream | ||
1220 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12816 times.
|
12816 | if ((ret = extract_audio(s, audio, abits, ch)) < 0) |
1221 | ✗ | return ret; | |
1222 | |||
1223 | // Select quantization step size table and look up quantization | ||
1224 | // step size | ||
1225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12816 times.
|
12816 | if (s->bit_rate == 3) |
1226 | ✗ | step_size = ff_dca_lossless_quant[abits]; | |
1227 | else | ||
1228 | 12816 | step_size = ff_dca_lossy_quant[abits]; | |
1229 | |||
1230 | // Get the scale factor | ||
1231 | 12816 | scale = s->scale_factors[ch][band >> 1][band & 1]; | |
1232 | |||
1233 | 12816 | ff_dca_core_dequantize(s->x96_subband_samples[ch][band] + ofs, | |
1234 | audio, step_size, scale, 0, DCA_SUBBAND_SAMPLES); | ||
1235 | } | ||
1236 | } | ||
1237 | |||
1238 | // DSYNC | ||
1239 |
4/6✓ Branch 0 taken 49 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 98 times.
|
98 | if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) { |
1240 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n"); | |
1241 | ✗ | return AVERROR_INVALIDDATA; | |
1242 | } | ||
1243 | |||
1244 | 98 | ofs += DCA_SUBBAND_SAMPLES; | |
1245 | } | ||
1246 | |||
1247 | // Inverse ADPCM | ||
1248 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1249 | 196 | inverse_adpcm(s->x96_subband_samples[ch], s->prediction_vq_index[ch], | |
1250 | 196 | s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch], | |
1251 | *sub_pos, nsamples); | ||
1252 | } | ||
1253 | |||
1254 | // Joint subband coding | ||
1255 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1256 | 196 | int src_ch = s->joint_intensity_index[ch] - 1; | |
1257 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 119 times.
|
196 | if (src_ch >= 0) { |
1258 | 77 | s->dcadsp->decode_joint(s->x96_subband_samples[ch], s->x96_subband_samples[src_ch], | |
1259 | 77 | s->joint_scale_factors[ch], s->nsubbands[ch], | |
1260 | 77 | s->nsubbands[src_ch], *sub_pos, nsamples); | |
1261 | } | ||
1262 | } | ||
1263 | |||
1264 | // Advance subband sample pointer for the next subframe | ||
1265 | 49 | *sub_pos = ofs; | |
1266 | 49 | return 0; | |
1267 | } | ||
1268 | |||
1269 | ✗ | static void erase_x96_adpcm_history(DCACoreDecoder *s) | |
1270 | { | ||
1271 | int ch, band; | ||
1272 | |||
1273 | // Erase ADPCM history from previous frame if | ||
1274 | // predictor history switch was disabled | ||
1275 | ✗ | for (ch = 0; ch < DCA_CHANNELS; ch++) | |
1276 | ✗ | for (band = 0; band < DCA_SUBBANDS_X96; band++) | |
1277 | ✗ | AV_ZERO128(s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS); | |
1278 | |||
1279 | ✗ | emms_c(); | |
1280 | } | ||
1281 | |||
1282 | 35 | static int alloc_x96_sample_buffer(DCACoreDecoder *s) | |
1283 | { | ||
1284 | 35 | int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks; | |
1285 | 35 | int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96; | |
1286 | 35 | unsigned int size = s->x96_subband_size; | |
1287 | int ch, band; | ||
1288 | |||
1289 | // Reallocate subband sample buffer | ||
1290 | 35 | av_fast_mallocz(&s->x96_subband_buffer, &s->x96_subband_size, | |
1291 | nframesamples * sizeof(int32_t)); | ||
1292 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
|
35 | if (!s->x96_subband_buffer) |
1293 | ✗ | return AVERROR(ENOMEM); | |
1294 | |||
1295 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 25 times.
|
35 | if (size != s->x96_subband_size) { |
1296 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 10 times.
|
80 | for (ch = 0; ch < DCA_CHANNELS; ch++) |
1297 |
2/2✓ Branch 0 taken 4480 times.
✓ Branch 1 taken 70 times.
|
4550 | for (band = 0; band < DCA_SUBBANDS_X96; band++) |
1298 | 4480 | s->x96_subband_samples[ch][band] = s->x96_subband_buffer + | |
1299 | 4480 | (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS; | |
1300 | } | ||
1301 | |||
1302 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
|
35 | if (!s->predictor_history) |
1303 | ✗ | erase_x96_adpcm_history(s); | |
1304 | |||
1305 | 35 | return 0; | |
1306 | } | ||
1307 | |||
1308 | 49 | static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base) | |
1309 | { | ||
1310 | int ch, band, ret; | ||
1311 | |||
1312 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
|
49 | if (get_bits_left(&s->gb) < 0) |
1313 | ✗ | return AVERROR_INVALIDDATA; | |
1314 | |||
1315 | // Prediction mode | ||
1316 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) |
1317 |
2/2✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 196 times.
|
9331 | for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) |
1318 | 9135 | s->prediction_mode[ch][band] = get_bits1(&s->gb); | |
1319 | |||
1320 | // Prediction coefficients VQ address | ||
1321 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) |
1322 |
2/2✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 196 times.
|
9331 | for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) |
1323 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 9088 times.
|
9135 | if (s->prediction_mode[ch][band]) |
1324 | 47 | s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12); | |
1325 | |||
1326 | // Bit allocation index | ||
1327 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1328 | 196 | int sel = s->bit_allocation_sel[ch]; | |
1329 | 196 | int abits = 0; | |
1330 | |||
1331 |
2/2✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 196 times.
|
9331 | for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) { |
1332 | // If Huffman code was used, the difference of abits was encoded | ||
1333 |
2/2✓ Branch 0 taken 9115 times.
✓ Branch 1 taken 20 times.
|
9135 | if (sel < 7) |
1334 | 9115 | abits += dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[5 + 2 * s->x96_high_res], sel); | |
1335 | else | ||
1336 | 20 | abits = get_bits(&s->gb, 3 + s->x96_high_res); | |
1337 | |||
1338 |
2/4✓ Branch 0 taken 9135 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9135 times.
|
9135 | if (abits < 0 || abits > 7 + 8 * s->x96_high_res) { |
1339 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n"); | |
1340 | ✗ | return AVERROR_INVALIDDATA; | |
1341 | } | ||
1342 | |||
1343 | 9135 | s->bit_allocation[ch][band] = abits; | |
1344 | } | ||
1345 | } | ||
1346 | |||
1347 | // Scale factors | ||
1348 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1349 | 196 | int sel = s->scale_factor_sel[ch]; | |
1350 | 196 | int scale_index = 0; | |
1351 | |||
1352 | // Extract scales for subbands which are transmitted even for | ||
1353 | // unallocated subbands | ||
1354 |
2/2✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 196 times.
|
9331 | for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) { |
1355 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9135 times.
|
9135 | if ((ret = parse_scale(s, &scale_index, sel)) < 0) |
1356 | ✗ | return ret; | |
1357 | 9135 | s->scale_factors[ch][band >> 1][band & 1] = ret; | |
1358 | } | ||
1359 | } | ||
1360 | |||
1361 | // Joint subband codebook select | ||
1362 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1363 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 119 times.
|
196 | if (s->joint_intensity_index[ch]) { |
1364 | 77 | s->joint_scale_sel[ch] = get_bits(&s->gb, 3); | |
1365 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
|
77 | if (s->joint_scale_sel[ch] == 7) { |
1366 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n"); | |
1367 | ✗ | return AVERROR_INVALIDDATA; | |
1368 | } | ||
1369 | } | ||
1370 | } | ||
1371 | |||
1372 | // Scale factors for joint subband coding | ||
1373 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1374 | 196 | int src_ch = s->joint_intensity_index[ch] - 1; | |
1375 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 119 times.
|
196 | if (src_ch >= 0) { |
1376 | 77 | int sel = s->joint_scale_sel[ch]; | |
1377 |
2/2✓ Branch 0 taken 2464 times.
✓ Branch 1 taken 77 times.
|
2541 | for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) { |
1378 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
|
2464 | if ((ret = parse_joint_scale(s, sel)) < 0) |
1379 | ✗ | return ret; | |
1380 | 2464 | s->joint_scale_factors[ch][band] = ret; | |
1381 | } | ||
1382 | } | ||
1383 | } | ||
1384 | |||
1385 | // Side information CRC check word | ||
1386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | if (s->crc_present) |
1387 | ✗ | skip_bits(&s->gb, 16); | |
1388 | |||
1389 | 49 | return 0; | |
1390 | } | ||
1391 | |||
1392 | 49 | static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base) | |
1393 | { | ||
1394 | 49 | int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb); | |
1395 | |||
1396 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
|
49 | if (get_bits_left(&s->gb) < 0) |
1397 | ✗ | return AVERROR_INVALIDDATA; | |
1398 | |||
1399 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7 times.
|
49 | if (exss) { |
1400 | // Channel set header length | ||
1401 | 42 | header_size = get_bits(&s->gb, 7) + 1; | |
1402 | |||
1403 | // Check CRC | ||
1404 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (s->x96_crc_present |
1405 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
|
42 | && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) { |
1406 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n"); | |
1407 | ✗ | return AVERROR_INVALIDDATA; | |
1408 | } | ||
1409 | } | ||
1410 | |||
1411 | // High resolution flag | ||
1412 | 49 | s->x96_high_res = get_bits1(&s->gb); | |
1413 | |||
1414 | // First encoded subband | ||
1415 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | if (s->x96_rev_no < 8) { |
1416 | 49 | s->x96_subband_start = get_bits(&s->gb, 5); | |
1417 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | if (s->x96_subband_start > 27) { |
1418 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start); | |
1419 | ✗ | return AVERROR_INVALIDDATA; | |
1420 | } | ||
1421 | } else { | ||
1422 | ✗ | s->x96_subband_start = DCA_SUBBANDS; | |
1423 | } | ||
1424 | |||
1425 | // Subband activity count | ||
1426 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1427 | 196 | s->nsubbands[ch] = get_bits(&s->gb, 6) + 1; | |
1428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
|
196 | if (s->nsubbands[ch] < DCA_SUBBANDS) { |
1429 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]); | |
1430 | ✗ | return AVERROR_INVALIDDATA; | |
1431 | } | ||
1432 | } | ||
1433 | |||
1434 | // Joint intensity coding index | ||
1435 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1436 |
4/4✓ Branch 1 taken 77 times.
✓ Branch 2 taken 119 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 70 times.
|
196 | if ((n = get_bits(&s->gb, 3)) && xch_base) |
1437 | 7 | n += xch_base - 1; | |
1438 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
|
196 | if (n > s->x96_nchannels) { |
1439 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n"); | |
1440 | ✗ | return AVERROR_INVALIDDATA; | |
1441 | } | ||
1442 | 196 | s->joint_intensity_index[ch] = n; | |
1443 | } | ||
1444 | |||
1445 | // Scale factor code book | ||
1446 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1447 | 196 | s->scale_factor_sel[ch] = get_bits(&s->gb, 3); | |
1448 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
|
196 | if (s->scale_factor_sel[ch] >= 6) { |
1449 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n"); | |
1450 | ✗ | return AVERROR_INVALIDDATA; | |
1451 | } | ||
1452 | } | ||
1453 | |||
1454 | // Bit allocation quantizer select | ||
1455 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) |
1456 | 196 | s->bit_allocation_sel[ch] = get_bits(&s->gb, 3); | |
1457 | |||
1458 | // Quantization index codebook select | ||
1459 |
2/2✓ Branch 0 taken 350 times.
✓ Branch 1 taken 49 times.
|
399 | for (n = 0; n < 6 + 4 * s->x96_high_res; n++) |
1460 |
2/2✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 350 times.
|
1694 | for (ch = xch_base; ch < s->x96_nchannels; ch++) |
1461 | 1344 | s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]); | |
1462 | |||
1463 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7 times.
|
49 | if (exss) { |
1464 | // Reserved | ||
1465 | // Byte align | ||
1466 | // CRC16 of channel set header | ||
1467 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
|
42 | if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) { |
1468 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n"); | |
1469 | ✗ | return AVERROR_INVALIDDATA; | |
1470 | } | ||
1471 | } else { | ||
1472 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (s->crc_present) |
1473 | ✗ | skip_bits(&s->gb, 16); | |
1474 | } | ||
1475 | |||
1476 | 49 | return 0; | |
1477 | } | ||
1478 | |||
1479 | 49 | static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base) | |
1480 | { | ||
1481 | int sf, ch, ret, band, sub_pos; | ||
1482 | |||
1483 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
|
49 | if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0) |
1484 | ✗ | return ret; | |
1485 | |||
1486 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 49 times.
|
98 | for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) { |
1487 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
|
49 | if ((ret = parse_x96_subframe_header(s, xch_base)) < 0) |
1488 | ✗ | return ret; | |
1489 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
|
49 | if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0) |
1490 | ✗ | return ret; | |
1491 | } | ||
1492 | |||
1493 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
|
245 | for (ch = xch_base; ch < s->x96_nchannels; ch++) { |
1494 | // Determine number of active subbands for this channel | ||
1495 | 196 | int nsubbands = s->nsubbands[ch]; | |
1496 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 119 times.
|
196 | if (s->joint_intensity_index[ch]) |
1497 | 77 | nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]); | |
1498 | |||
1499 | // Update history for ADPCM and clear inactive subbands | ||
1500 |
2/2✓ Branch 0 taken 12544 times.
✓ Branch 1 taken 196 times.
|
12740 | for (band = 0; band < DCA_SUBBANDS_X96; band++) { |
1501 | 12544 | int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS; | |
1502 |
3/4✓ Branch 0 taken 11599 times.
✓ Branch 1 taken 945 times.
✓ Branch 2 taken 11599 times.
✗ Branch 3 not taken.
|
12544 | if (band >= s->x96_subband_start && band < nsubbands) |
1503 | 11599 | AV_COPY128(samples, samples + s->npcmblocks); | |
1504 | else | ||
1505 | 945 | memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t)); | |
1506 | } | ||
1507 | } | ||
1508 | |||
1509 | 49 | emms_c(); | |
1510 | |||
1511 | 49 | return 0; | |
1512 | } | ||
1513 | |||
1514 | 7 | static int parse_x96_frame(DCACoreDecoder *s) | |
1515 | { | ||
1516 | int ret; | ||
1517 | |||
1518 | // Revision number | ||
1519 | 7 | s->x96_rev_no = get_bits(&s->gb, 4); | |
1520 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
7 | if (s->x96_rev_no < 1 || s->x96_rev_no > 8) { |
1521 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no); | |
1522 | ✗ | return AVERROR_INVALIDDATA; | |
1523 | } | ||
1524 | |||
1525 | 7 | s->x96_crc_present = 0; | |
1526 | 7 | s->x96_nchannels = s->nchannels; | |
1527 | |||
1528 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if ((ret = alloc_x96_sample_buffer(s)) < 0) |
1529 | ✗ | return ret; | |
1530 | |||
1531 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if ((ret = parse_x96_frame_data(s, 0, 0)) < 0) |
1532 | ✗ | return ret; | |
1533 | |||
1534 | // Seek to the end of core frame | ||
1535 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) { |
1536 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n"); | |
1537 | ✗ | return AVERROR_INVALIDDATA; | |
1538 | } | ||
1539 | |||
1540 | 7 | return 0; | |
1541 | } | ||
1542 | |||
1543 | 28 | static int parse_x96_frame_exss(DCACoreDecoder *s) | |
1544 | { | ||
1545 | int x96_frame_size[DCA_EXSS_CHSETS_MAX]; | ||
1546 | int x96_nchannels[DCA_EXSS_CHSETS_MAX]; | ||
1547 | int x96_nchsets, x96_base_ch; | ||
1548 | 28 | int i, ret, header_size, header_pos = get_bits_count(&s->gb); | |
1549 | |||
1550 | // X96 sync word | ||
1551 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) { |
1552 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n"); | |
1553 | ✗ | return AVERROR_INVALIDDATA; | |
1554 | } | ||
1555 | |||
1556 | // X96 frame header length | ||
1557 | 28 | header_size = get_bits(&s->gb, 6) + 1; | |
1558 | |||
1559 | // Check X96 frame header CRC | ||
1560 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) { |
1561 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n"); | |
1562 | ✗ | return AVERROR_INVALIDDATA; | |
1563 | } | ||
1564 | |||
1565 | // Revision number | ||
1566 | 28 | s->x96_rev_no = get_bits(&s->gb, 4); | |
1567 |
2/4✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
|
28 | if (s->x96_rev_no < 1 || s->x96_rev_no > 8) { |
1568 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no); | |
1569 | ✗ | return AVERROR_INVALIDDATA; | |
1570 | } | ||
1571 | |||
1572 | // CRC presence flag for channel set header | ||
1573 | 28 | s->x96_crc_present = get_bits1(&s->gb); | |
1574 | |||
1575 | // Number of channel sets | ||
1576 | 28 | x96_nchsets = get_bits(&s->gb, 2) + 1; | |
1577 | |||
1578 | // Channel set data byte size | ||
1579 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
|
84 | for (i = 0; i < x96_nchsets; i++) |
1580 | 56 | x96_frame_size[i] = get_bits(&s->gb, 12) + 1; | |
1581 | |||
1582 | // Number of channels in channel set | ||
1583 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
|
84 | for (i = 0; i < x96_nchsets; i++) |
1584 | 56 | x96_nchannels[i] = get_bits(&s->gb, 3) + 1; | |
1585 | |||
1586 | // Reserved | ||
1587 | // Byte align | ||
1588 | // CRC16 of X96 frame header | ||
1589 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) { |
1590 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n"); | |
1591 | ✗ | return AVERROR_INVALIDDATA; | |
1592 | } | ||
1593 | |||
1594 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if ((ret = alloc_x96_sample_buffer(s)) < 0) |
1595 | ✗ | return ret; | |
1596 | |||
1597 | // Channel set data | ||
1598 | 28 | s->x96_nchannels = 0; | |
1599 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
|
84 | for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) { |
1600 | 56 | header_pos = get_bits_count(&s->gb); | |
1601 | |||
1602 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 14 times.
|
56 | if (x96_base_ch + x96_nchannels[i] <= s->nchannels) { |
1603 | 42 | s->x96_nchannels = x96_base_ch + x96_nchannels[i]; | |
1604 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
|
42 | if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0) |
1605 | ✗ | return ret; | |
1606 | } | ||
1607 | |||
1608 | 56 | x96_base_ch += x96_nchannels[i]; | |
1609 | |||
1610 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
|
56 | if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) { |
1611 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n"); | |
1612 | ✗ | return AVERROR_INVALIDDATA; | |
1613 | } | ||
1614 | } | ||
1615 | |||
1616 | 28 | return 0; | |
1617 | } | ||
1618 | |||
1619 | 77 | static int parse_aux_data(DCACoreDecoder *s) | |
1620 | { | ||
1621 | int aux_pos; | ||
1622 | |||
1623 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
|
77 | if (get_bits_left(&s->gb) < 0) |
1624 | ✗ | return AVERROR_INVALIDDATA; | |
1625 | |||
1626 | // Auxiliary data byte count (can't be trusted) | ||
1627 | 77 | skip_bits(&s->gb, 6); | |
1628 | |||
1629 | // 4-byte align | ||
1630 | 77 | skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31); | |
1631 | |||
1632 | // Auxiliary data sync word | ||
1633 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
|
77 | if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) { |
1634 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n"); | |
1635 | ✗ | return AVERROR_INVALIDDATA; | |
1636 | } | ||
1637 | |||
1638 | 77 | aux_pos = get_bits_count(&s->gb); | |
1639 | |||
1640 | // Auxiliary decode time stamp flag | ||
1641 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
|
77 | if (get_bits1(&s->gb)) |
1642 | ✗ | skip_bits_long(&s->gb, 47); | |
1643 | |||
1644 | // Auxiliary dynamic downmix flag | ||
1645 |
1/2✓ Branch 1 taken 77 times.
✗ Branch 2 not taken.
|
77 | if (s->prim_dmix_embedded = get_bits1(&s->gb)) { |
1646 | int i, m, n; | ||
1647 | |||
1648 | // Auxiliary primary channel downmix type | ||
1649 | 77 | s->prim_dmix_type = get_bits(&s->gb, 3); | |
1650 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
|
77 | if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) { |
1651 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n"); | |
1652 | ✗ | return AVERROR_INVALIDDATA; | |
1653 | } | ||
1654 | |||
1655 | // Size of downmix coefficients matrix | ||
1656 | 77 | m = ff_dca_dmix_primary_nch[s->prim_dmix_type]; | |
1657 | 77 | n = ff_dca_channels[s->audio_mode] + !!s->lfe_present; | |
1658 | |||
1659 | // Dynamic downmix code coefficients | ||
1660 |
2/2✓ Branch 0 taken 924 times.
✓ Branch 1 taken 77 times.
|
1001 | for (i = 0; i < m * n; i++) { |
1661 | 924 | int code = get_bits(&s->gb, 9); | |
1662 | 924 | int sign = (code >> 8) - 1; | |
1663 | 924 | unsigned int index = code & 0xff; | |
1664 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 924 times.
|
924 | if (index >= FF_DCA_DMIXTABLE_SIZE) { |
1665 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n"); | |
1666 | ✗ | return AVERROR_INVALIDDATA; | |
1667 | } | ||
1668 | 924 | s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign; | |
1669 | } | ||
1670 | } | ||
1671 | |||
1672 | // Byte align | ||
1673 | 77 | skip_bits(&s->gb, -get_bits_count(&s->gb) & 7); | |
1674 | |||
1675 | // CRC16 of auxiliary data | ||
1676 | 77 | skip_bits(&s->gb, 16); | |
1677 | |||
1678 | // Check CRC | ||
1679 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 77 times.
|
77 | if (ff_dca_check_crc(s->avctx, &s->gb, aux_pos, get_bits_count(&s->gb))) { |
1680 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n"); | |
1681 | ✗ | return AVERROR_INVALIDDATA; | |
1682 | } | ||
1683 | |||
1684 | 77 | return 0; | |
1685 | } | ||
1686 | |||
1687 | 2464 | static int parse_optional_info(DCACoreDecoder *s) | |
1688 | { | ||
1689 | 2464 | DCAContext *dca = s->avctx->priv_data; | |
1690 | 2464 | int ret = -1; | |
1691 | |||
1692 | // Time code stamp | ||
1693 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
|
2464 | if (s->ts_present) |
1694 | ✗ | skip_bits_long(&s->gb, 32); | |
1695 | |||
1696 | // Auxiliary data | ||
1697 |
3/4✓ Branch 0 taken 77 times.
✓ Branch 1 taken 2387 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 77 times.
|
2464 | if (s->aux_present && (ret = parse_aux_data(s)) < 0 |
1698 | ✗ | && (s->avctx->err_recognition & AV_EF_EXPLODE)) | |
1699 | ✗ | return ret; | |
1700 | |||
1701 |
2/2✓ Branch 0 taken 2387 times.
✓ Branch 1 taken 77 times.
|
2464 | if (ret < 0) |
1702 | 2387 | s->prim_dmix_embedded = 0; | |
1703 | |||
1704 | // Core extensions | ||
1705 |
3/4✓ Branch 0 taken 328 times.
✓ Branch 1 taken 2136 times.
✓ Branch 2 taken 328 times.
✗ Branch 3 not taken.
|
2464 | if (s->ext_audio_present && !dca->core_only) { |
1706 | 328 | int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1; | |
1707 | 328 | int last_pos = get_bits_count(&s->gb) / 32; | |
1708 | int size, dist; | ||
1709 | 328 | uint32_t w1, w2 = 0; | |
1710 | |||
1711 | // Search for extension sync words aligned on 4-byte boundary. Search | ||
1712 | // must be done backwards from the end of core frame to work around | ||
1713 | // sync word aliasing issues. | ||
1714 |
2/4✓ Branch 0 taken 300 times.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
328 | switch (s->ext_audio_type) { |
1715 | 300 | case DCA_EXT_AUDIO_XCH: | |
1716 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 279 times.
|
300 | if (dca->request_channel_layout) |
1717 | 21 | break; | |
1718 | |||
1719 | // The distance between XCH sync word and end of the core frame | ||
1720 | // must be equal to XCH frame size. Off by one error is allowed for | ||
1721 | // compatibility with legacy bitstreams. Minimum XCH frame size is | ||
1722 | // 96 bytes. AMODE and PCHS are further checked to reduce | ||
1723 | // probability of alias sync detection. | ||
1724 |
1/2✓ Branch 0 taken 19253 times.
✗ Branch 1 not taken.
|
19253 | for (; sync_pos >= last_pos; sync_pos--, w2 = w1) { |
1725 | 19253 | w1 = AV_RB32(s->gb.buffer + sync_pos * 4); | |
1726 |
2/2✓ Branch 0 taken 279 times.
✓ Branch 1 taken 18974 times.
|
19253 | if (w1 == DCA_SYNCWORD_XCH) { |
1727 | 279 | size = (w2 >> 22) + 1; | |
1728 | 279 | dist = s->frame_size - sync_pos * 4; | |
1729 |
1/2✓ Branch 0 taken 279 times.
✗ Branch 1 not taken.
|
279 | if (size >= 96 |
1730 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
279 | && (size == dist || size - 1 == dist) |
1731 |
1/2✓ Branch 0 taken 279 times.
✗ Branch 1 not taken.
|
279 | && (w2 >> 15 & 0x7f) == 0x08) { |
1732 | 279 | s->xch_pos = sync_pos * 32 + 49; | |
1733 | 279 | break; | |
1734 | } | ||
1735 | } | ||
1736 | } | ||
1737 | |||
1738 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
|
279 | if (!s->xch_pos) { |
1739 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n"); | |
1740 | ✗ | if (s->avctx->err_recognition & AV_EF_EXPLODE) | |
1741 | ✗ | return AVERROR_INVALIDDATA; | |
1742 | } | ||
1743 | 279 | break; | |
1744 | |||
1745 | 28 | case DCA_EXT_AUDIO_X96: | |
1746 | // The distance between X96 sync word and end of the core frame | ||
1747 | // must be equal to X96 frame size. Minimum X96 frame size is 96 | ||
1748 | // bytes. | ||
1749 |
1/2✓ Branch 0 taken 3172 times.
✗ Branch 1 not taken.
|
3172 | for (; sync_pos >= last_pos; sync_pos--, w2 = w1) { |
1750 | 3172 | w1 = AV_RB32(s->gb.buffer + sync_pos * 4); | |
1751 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 3144 times.
|
3172 | if (w1 == DCA_SYNCWORD_X96) { |
1752 | 28 | size = (w2 >> 20) + 1; | |
1753 | 28 | dist = s->frame_size - sync_pos * 4; | |
1754 |
2/4✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
|
28 | if (size >= 96 && size == dist) { |
1755 | 28 | s->x96_pos = sync_pos * 32 + 44; | |
1756 | 28 | break; | |
1757 | } | ||
1758 | } | ||
1759 | } | ||
1760 | |||
1761 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!s->x96_pos) { |
1762 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n"); | |
1763 | ✗ | if (s->avctx->err_recognition & AV_EF_EXPLODE) | |
1764 | ✗ | return AVERROR_INVALIDDATA; | |
1765 | } | ||
1766 | 28 | break; | |
1767 | |||
1768 | ✗ | case DCA_EXT_AUDIO_XXCH: | |
1769 | ✗ | if (dca->request_channel_layout) | |
1770 | ✗ | break; | |
1771 | |||
1772 | // XXCH frame header CRC must be valid. Minimum XXCH frame header | ||
1773 | // size is 11 bytes. | ||
1774 | ✗ | for (; sync_pos >= last_pos; sync_pos--, w2 = w1) { | |
1775 | ✗ | w1 = AV_RB32(s->gb.buffer + sync_pos * 4); | |
1776 | ✗ | if (w1 == DCA_SYNCWORD_XXCH) { | |
1777 | ✗ | size = (w2 >> 26) + 1; | |
1778 | ✗ | dist = s->gb.size_in_bits / 8 - sync_pos * 4; | |
1779 | ✗ | if (size >= 11 && size <= dist && | |
1780 | ✗ | !av_crc(dca->crctab, 0xffff, s->gb.buffer + | |
1781 | ✗ | (sync_pos + 1) * 4, size - 4)) { | |
1782 | ✗ | s->xxch_pos = sync_pos * 32; | |
1783 | ✗ | break; | |
1784 | } | ||
1785 | } | ||
1786 | } | ||
1787 | |||
1788 | ✗ | if (!s->xxch_pos) { | |
1789 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n"); | |
1790 | ✗ | if (s->avctx->err_recognition & AV_EF_EXPLODE) | |
1791 | ✗ | return AVERROR_INVALIDDATA; | |
1792 | } | ||
1793 | ✗ | break; | |
1794 | } | ||
1795 | } | ||
1796 | |||
1797 | 2464 | return 0; | |
1798 | } | ||
1799 | |||
1800 | 2464 | int ff_dca_core_parse(DCACoreDecoder *s, const uint8_t *data, int size) | |
1801 | { | ||
1802 | int ret; | ||
1803 | |||
1804 | 2464 | s->ext_audio_mask = 0; | |
1805 | 2464 | s->xch_pos = s->xxch_pos = s->x96_pos = 0; | |
1806 | |||
1807 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
|
2464 | if ((ret = init_get_bits8(&s->gb, data, size)) < 0) |
1808 | ✗ | return ret; | |
1809 | 2464 | s->gb_in = s->gb; | |
1810 | |||
1811 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
|
2464 | if ((ret = parse_frame_header(s)) < 0) |
1812 | ✗ | return ret; | |
1813 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
|
2464 | if ((ret = alloc_sample_buffer(s)) < 0) |
1814 | ✗ | return ret; | |
1815 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
|
2464 | if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0) |
1816 | ✗ | return ret; | |
1817 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
|
2464 | if ((ret = parse_optional_info(s)) < 0) |
1818 | ✗ | return ret; | |
1819 | |||
1820 | // Workaround for DTS in WAV | ||
1821 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
|
2464 | if (s->frame_size > size) |
1822 | ✗ | s->frame_size = size; | |
1823 | |||
1824 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
|
2464 | if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) { |
1825 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n"); | |
1826 | ✗ | if (s->avctx->err_recognition & AV_EF_EXPLODE) | |
1827 | ✗ | return AVERROR_INVALIDDATA; | |
1828 | } | ||
1829 | |||
1830 | 2464 | return 0; | |
1831 | } | ||
1832 | |||
1833 | 2464 | int ff_dca_core_parse_exss(DCACoreDecoder *s, const uint8_t *data, DCAExssAsset *asset) | |
1834 | { | ||
1835 | 2464 | AVCodecContext *avctx = s->avctx; | |
1836 | 2464 | DCAContext *dca = avctx->priv_data; | |
1837 |
2/2✓ Branch 0 taken 1398 times.
✓ Branch 1 taken 1066 times.
|
2464 | int exss_mask = asset ? asset->extension_mask : 0; |
1838 | 2464 | int ret = 0, ext = 0; | |
1839 | |||
1840 | // Parse (X)XCH unless downmixing | ||
1841 |
2/2✓ Branch 0 taken 2324 times.
✓ Branch 1 taken 140 times.
|
2464 | if (!dca->request_channel_layout) { |
1842 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2303 times.
|
2324 | if (exss_mask & DCA_EXSS_XXCH) { |
1843 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0) |
1844 | ✗ | return ret; | |
1845 | 21 | ret = parse_xxch_frame(s); | |
1846 | 21 | ext = DCA_EXSS_XXCH; | |
1847 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2303 times.
|
2303 | } else if (s->xxch_pos) { |
1848 | ✗ | s->gb = s->gb_in; | |
1849 | ✗ | skip_bits_long(&s->gb, s->xxch_pos); | |
1850 | ✗ | ret = parse_xxch_frame(s); | |
1851 | ✗ | ext = DCA_CSS_XXCH; | |
1852 |
2/2✓ Branch 0 taken 279 times.
✓ Branch 1 taken 2024 times.
|
2303 | } else if (s->xch_pos) { |
1853 | 279 | s->gb = s->gb_in; | |
1854 | 279 | skip_bits_long(&s->gb, s->xch_pos); | |
1855 | 279 | ret = parse_xch_frame(s); | |
1856 | 279 | ext = DCA_CSS_XCH; | |
1857 | } | ||
1858 | |||
1859 | // Revert to primary channel set in case (X)XCH parsing fails | ||
1860 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2324 times.
|
2324 | if (ret < 0) { |
1861 | ✗ | if (avctx->err_recognition & AV_EF_EXPLODE) | |
1862 | ✗ | return ret; | |
1863 | ✗ | s->nchannels = ff_dca_channels[s->audio_mode]; | |
1864 | ✗ | s->ch_mask = audio_mode_ch_mask[s->audio_mode]; | |
1865 | ✗ | if (s->lfe_present) | |
1866 | ✗ | s->ch_mask |= DCA_SPEAKER_MASK_LFE1; | |
1867 | } else { | ||
1868 | 2324 | s->ext_audio_mask |= ext; | |
1869 | } | ||
1870 | } | ||
1871 | |||
1872 | // Parse XBR | ||
1873 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2443 times.
|
2464 | if (exss_mask & DCA_EXSS_XBR) { |
1874 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0) |
1875 | ✗ | return ret; | |
1876 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
|
21 | if ((ret = parse_xbr_frame(s)) < 0) { |
1877 | ✗ | if (avctx->err_recognition & AV_EF_EXPLODE) | |
1878 | ✗ | return ret; | |
1879 | } else { | ||
1880 | 21 | s->ext_audio_mask |= DCA_EXSS_XBR; | |
1881 | } | ||
1882 | } | ||
1883 | |||
1884 | // Parse X96 unless decoding XLL | ||
1885 |
2/2✓ Branch 0 taken 1122 times.
✓ Branch 1 taken 1342 times.
|
2464 | if (!(dca->packet & DCA_PACKET_XLL)) { |
1886 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1094 times.
|
1122 | if (exss_mask & DCA_EXSS_X96) { |
1887 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0) |
1888 | ✗ | return ret; | |
1889 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if ((ret = parse_x96_frame_exss(s)) < 0) { |
1890 | ✗ | if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE)) | |
1891 | ✗ | return ret; | |
1892 | } else { | ||
1893 | 28 | s->ext_audio_mask |= DCA_EXSS_X96; | |
1894 | } | ||
1895 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1087 times.
|
1094 | } else if (s->x96_pos) { |
1896 | 7 | s->gb = s->gb_in; | |
1897 | 7 | skip_bits_long(&s->gb, s->x96_pos); | |
1898 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if ((ret = parse_x96_frame(s)) < 0) { |
1899 | ✗ | if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE)) | |
1900 | ✗ | return ret; | |
1901 | } else { | ||
1902 | 7 | s->ext_audio_mask |= DCA_CSS_X96; | |
1903 | } | ||
1904 | } | ||
1905 | } | ||
1906 | |||
1907 | 2464 | return 0; | |
1908 | } | ||
1909 | |||
1910 | 11129 | static int map_prm_ch_to_spkr(DCACoreDecoder *s, int ch) | |
1911 | { | ||
1912 | int pos, spkr; | ||
1913 | |||
1914 | // Try to map this channel to core first | ||
1915 | 11129 | pos = ff_dca_channels[s->audio_mode]; | |
1916 |
2/2✓ Branch 0 taken 10766 times.
✓ Branch 1 taken 363 times.
|
11129 | if (ch < pos) { |
1917 | 10766 | spkr = prm_ch_to_spkr_map[s->audio_mode][ch]; | |
1918 |
2/2✓ Branch 0 taken 105 times.
✓ Branch 1 taken 10661 times.
|
10766 | if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) { |
1919 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 42 times.
|
105 | if (s->xxch_core_mask & (1U << spkr)) |
1920 | 63 | return spkr; | |
1921 |
3/4✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
42 | if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss)) |
1922 | 21 | return DCA_SPEAKER_Lss; | |
1923 |
2/4✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
21 | if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss)) |
1924 | 21 | return DCA_SPEAKER_Rss; | |
1925 | ✗ | return -1; | |
1926 | } | ||
1927 | 10661 | return spkr; | |
1928 | } | ||
1929 | |||
1930 | // Then XCH | ||
1931 |
3/4✓ Branch 0 taken 279 times.
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 279 times.
✗ Branch 3 not taken.
|
363 | if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos) |
1932 | 279 | return DCA_SPEAKER_Cs; | |
1933 | |||
1934 | // Then XXCH | ||
1935 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) { |
1936 |
1/2✓ Branch 0 taken 210 times.
✗ Branch 1 not taken.
|
210 | for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++) |
1937 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 84 times.
|
210 | if (s->xxch_spkr_mask & (1U << spkr)) |
1938 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 42 times.
|
126 | if (pos++ == ch) |
1939 | 84 | return spkr; | |
1940 | } | ||
1941 | |||
1942 | // No mapping | ||
1943 | ✗ | return -1; | |
1944 | } | ||
1945 | |||
1946 | 63 | static void erase_dsp_history(DCACoreDecoder *s) | |
1947 | { | ||
1948 | 63 | memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data)); | |
1949 | 63 | s->output_history_lfe_fixed = 0; | |
1950 | 63 | s->output_history_lfe_float = 0; | |
1951 | 63 | } | |
1952 | |||
1953 | 2464 | static void set_filter_mode(DCACoreDecoder *s, int mode) | |
1954 | { | ||
1955 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 2401 times.
|
2464 | if (s->filter_mode != mode) { |
1956 | 63 | erase_dsp_history(s); | |
1957 | 63 | s->filter_mode = mode; | |
1958 | } | ||
1959 | 2464 | } | |
1960 | |||
1961 | 1860 | int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth) | |
1962 | { | ||
1963 | 1860 | int n, ch, spkr, nsamples, x96_nchannels = 0; | |
1964 | const int32_t *filter_coeff; | ||
1965 | int32_t *ptr; | ||
1966 | |||
1967 | // Externally set x96_synth flag implies that X96 synthesis should be | ||
1968 | // enabled, yet actual X96 subband data should be discarded. This is a | ||
1969 | // special case for lossless residual decoder that ignores X96 data if | ||
1970 | // present. | ||
1971 |
3/4✓ Branch 0 taken 518 times.
✓ Branch 1 taken 1342 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 518 times.
|
1860 | if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) { |
1972 | ✗ | x96_nchannels = s->x96_nchannels; | |
1973 | ✗ | x96_synth = 1; | |
1974 | } | ||
1975 |
2/2✓ Branch 0 taken 1258 times.
✓ Branch 1 taken 602 times.
|
1860 | if (x96_synth < 0) |
1976 | 1258 | x96_synth = 0; | |
1977 | |||
1978 | 1860 | s->output_rate = s->sample_rate << x96_synth; | |
1979 | 1860 | s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth; | |
1980 | |||
1981 | // Reallocate PCM output buffer | ||
1982 | 1860 | av_fast_malloc(&s->output_buffer, &s->output_size, | |
1983 | 1860 | nsamples * av_popcount(s->ch_mask) * sizeof(int32_t)); | |
1984 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1860 times.
|
1860 | if (!s->output_buffer) |
1985 | ✗ | return AVERROR(ENOMEM); | |
1986 | |||
1987 | 1860 | ptr = (int32_t *)s->output_buffer; | |
1988 |
2/2✓ Branch 0 taken 59520 times.
✓ Branch 1 taken 1860 times.
|
61380 | for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) { |
1989 |
2/2✓ Branch 0 taken 9099 times.
✓ Branch 1 taken 50421 times.
|
59520 | if (s->ch_mask & (1U << spkr)) { |
1990 | 9099 | s->output_samples[spkr] = ptr; | |
1991 | 9099 | ptr += nsamples; | |
1992 | } else { | ||
1993 | 50421 | s->output_samples[spkr] = NULL; | |
1994 | } | ||
1995 | } | ||
1996 | |||
1997 | // Handle change of filtering mode | ||
1998 | 1860 | set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED); | |
1999 | |||
2000 | // Select filter | ||
2001 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1776 times.
|
1860 | if (x96_synth) |
2002 | 84 | filter_coeff = ff_dca_fir_64bands_fixed; | |
2003 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1776 times.
|
1776 | else if (s->filter_perfect) |
2004 | ✗ | filter_coeff = ff_dca_fir_32bands_perfect_fixed; | |
2005 | else | ||
2006 | 1776 | filter_coeff = ff_dca_fir_32bands_nonperfect_fixed; | |
2007 | |||
2008 | // Filter primary channels | ||
2009 |
2/2✓ Branch 0 taken 7756 times.
✓ Branch 1 taken 1860 times.
|
9616 | for (ch = 0; ch < s->nchannels; ch++) { |
2010 | // Map this primary channel to speaker | ||
2011 | 7756 | spkr = map_prm_ch_to_spkr(s, ch); | |
2012 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7756 times.
|
7756 | if (spkr < 0) |
2013 | ✗ | return AVERROR(EINVAL); | |
2014 | |||
2015 | // Filter bank reconstruction | ||
2016 | 7756 | s->dcadsp->sub_qmf_fixed[x96_synth]( | |
2017 | &s->synth, | ||
2018 | &s->dcadct, | ||
2019 | s->output_samples[spkr], | ||
2020 | 7756 | s->subband_samples[ch], | |
2021 | ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL, | ||
2022 | 7756 | s->dcadsp_data[ch].u.fix.hist1, | |
2023 | &s->dcadsp_data[ch].offset, | ||
2024 | 7756 | s->dcadsp_data[ch].u.fix.hist2, | |
2025 | filter_coeff, | ||
2026 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7756 times.
|
7756 | s->npcmblocks); |
2027 | } | ||
2028 | |||
2029 | // Filter LFE channel | ||
2030 |
2/2✓ Branch 0 taken 1343 times.
✓ Branch 1 taken 517 times.
|
1860 | if (s->lfe_present) { |
2031 | 1343 | int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1]; | |
2032 | 1343 | int nlfesamples = s->npcmblocks >> 1; | |
2033 | |||
2034 | // Check LFF | ||
2035 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1343 times.
|
1343 | if (s->lfe_present == DCA_LFE_FLAG_128) { |
2036 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n"); | |
2037 | ✗ | return AVERROR(EINVAL); | |
2038 | } | ||
2039 | |||
2040 | // Offset intermediate buffer for X96 | ||
2041 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1259 times.
|
1343 | if (x96_synth) |
2042 | 84 | samples += nsamples / 2; | |
2043 | |||
2044 | // Interpolate LFE channel | ||
2045 | 1343 | s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY, | |
2046 | 1343 | ff_dca_lfe_fir_64_fixed, s->npcmblocks); | |
2047 | |||
2048 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1259 times.
|
1343 | if (x96_synth) { |
2049 | // Filter 96 kHz oversampled LFE PCM to attenuate high frequency | ||
2050 | // (47.6 - 48.0 kHz) components of interpolation image | ||
2051 | 84 | s->dcadsp->lfe_x96_fixed(s->output_samples[DCA_SPEAKER_LFE1], | |
2052 | samples, &s->output_history_lfe_fixed, | ||
2053 | 84 | nsamples / 2); | |
2054 | |||
2055 | } | ||
2056 | |||
2057 | // Update LFE history | ||
2058 |
2/2✓ Branch 0 taken 10744 times.
✓ Branch 1 taken 1343 times.
|
12087 | for (n = DCA_LFE_HISTORY - 1; n >= 0; n--) |
2059 | 10744 | s->lfe_samples[n] = s->lfe_samples[nlfesamples + n]; | |
2060 | } | ||
2061 | |||
2062 | 1860 | return 0; | |
2063 | } | ||
2064 | |||
2065 | 518 | static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame) | |
2066 | { | ||
2067 | 518 | AVCodecContext *avctx = s->avctx; | |
2068 | 518 | DCAContext *dca = avctx->priv_data; | |
2069 | int i, n, ch, ret, spkr, nsamples; | ||
2070 | |||
2071 | // Don't filter twice when falling back from XLL | ||
2072 |
2/4✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 518 times.
|
518 | if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0) |
2073 | ✗ | return ret; | |
2074 | |||
2075 | 518 | avctx->sample_rate = s->output_rate; | |
2076 | 518 | avctx->sample_fmt = AV_SAMPLE_FMT_S32P; | |
2077 | 518 | avctx->bits_per_raw_sample = 24; | |
2078 | |||
2079 | 518 | frame->nb_samples = nsamples = s->npcmsamples; | |
2080 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 518 times.
|
518 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
2081 | ✗ | return ret; | |
2082 | |||
2083 | // Undo embedded XCH downmix | ||
2084 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
518 | if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH) |
2085 | ✗ | && s->audio_mode >= DCA_AMODE_2F2R) { | |
2086 | ✗ | s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls], | |
2087 | s->output_samples[DCA_SPEAKER_Rs], | ||
2088 | ✗ | s->output_samples[DCA_SPEAKER_Cs], | |
2089 | nsamples); | ||
2090 | |||
2091 | } | ||
2092 | |||
2093 | // Undo embedded XXCH downmix | ||
2094 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
|
518 | if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) |
2095 | ✗ | && s->xxch_dmix_embedded) { | |
2096 | ✗ | int scale_inv = s->xxch_dmix_scale_inv; | |
2097 | ✗ | int *coeff_ptr = s->xxch_dmix_coeff; | |
2098 | ✗ | int xch_base = ff_dca_channels[s->audio_mode]; | |
2099 | av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX); | ||
2100 | |||
2101 | // Undo embedded core downmix pre-scaling | ||
2102 | ✗ | for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) { | |
2103 | ✗ | if (s->xxch_core_mask & (1U << spkr)) { | |
2104 | ✗ | s->dcadsp->dmix_scale_inv(s->output_samples[spkr], | |
2105 | scale_inv, nsamples); | ||
2106 | } | ||
2107 | } | ||
2108 | |||
2109 | // Undo downmix | ||
2110 | ✗ | for (ch = xch_base; ch < s->nchannels; ch++) { | |
2111 | ✗ | int src_spkr = map_prm_ch_to_spkr(s, ch); | |
2112 | ✗ | if (src_spkr < 0) | |
2113 | ✗ | return AVERROR(EINVAL); | |
2114 | ✗ | for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) { | |
2115 | ✗ | if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) { | |
2116 | ✗ | int coeff = mul16(*coeff_ptr++, scale_inv); | |
2117 | ✗ | if (coeff) { | |
2118 | ✗ | s->dcadsp->dmix_sub(s->output_samples[spkr ], | |
2119 | ✗ | s->output_samples[src_spkr], | |
2120 | coeff, nsamples); | ||
2121 | } | ||
2122 | } | ||
2123 | } | ||
2124 | } | ||
2125 | } | ||
2126 | |||
2127 |
1/2✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
|
518 | if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) { |
2128 | // Front sum/difference decoding | ||
2129 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
518 | if ((s->sumdiff_front && s->audio_mode > DCA_AMODE_MONO) |
2130 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
|
518 | || s->audio_mode == DCA_AMODE_STEREO_SUMDIFF) { |
2131 | ✗ | s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L], | |
2132 | ✗ | s->output_samples[DCA_SPEAKER_R], | |
2133 | nsamples); | ||
2134 | } | ||
2135 | |||
2136 | // Surround sum/difference decoding | ||
2137 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
518 | if (s->sumdiff_surround && s->audio_mode >= DCA_AMODE_2F2R) { |
2138 | ✗ | s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls], | |
2139 | ✗ | s->output_samples[DCA_SPEAKER_Rs], | |
2140 | nsamples); | ||
2141 | } | ||
2142 | } | ||
2143 | |||
2144 | // Downmix primary channel set to stereo | ||
2145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
|
518 | if (s->request_mask != s->ch_mask) { |
2146 | ✗ | ff_dca_downmix_to_stereo_fixed(s->dcadsp, | |
2147 | ✗ | s->output_samples, | |
2148 | ✗ | s->prim_dmix_coeff, | |
2149 | nsamples, s->ch_mask); | ||
2150 | } | ||
2151 | |||
2152 |
2/2✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 518 times.
|
1558 | for (i = 0; i < avctx->ch_layout.nb_channels; i++) { |
2153 | 1040 | int32_t *samples = s->output_samples[s->ch_remap[i]]; | |
2154 | 1040 | int32_t *plane = (int32_t *)frame->extended_data[i]; | |
2155 |
2/2✓ Branch 0 taken 532480 times.
✓ Branch 1 taken 1040 times.
|
533520 | for (n = 0; n < nsamples; n++) |
2156 | 532480 | plane[n] = clip23(samples[n]) * (1 << 8); | |
2157 | } | ||
2158 | |||
2159 | 518 | return 0; | |
2160 | } | ||
2161 | |||
2162 | 604 | static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame) | |
2163 | { | ||
2164 | 604 | AVCodecContext *avctx = s->avctx; | |
2165 | 604 | int x96_nchannels = 0, x96_synth = 0; | |
2166 | int i, n, ch, ret, spkr, nsamples, nchannels; | ||
2167 | 604 | float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr; | |
2168 | const float *filter_coeff; | ||
2169 | |||
2170 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 569 times.
|
604 | if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) { |
2171 | 35 | x96_nchannels = s->x96_nchannels; | |
2172 | 35 | x96_synth = 1; | |
2173 | } | ||
2174 | |||
2175 | 604 | avctx->sample_rate = s->sample_rate << x96_synth; | |
2176 | 604 | avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; | |
2177 | 604 | avctx->bits_per_raw_sample = 0; | |
2178 | |||
2179 | 604 | frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth; | |
2180 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 604 times.
|
604 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
2181 | ✗ | return ret; | |
2182 | |||
2183 | // Build reverse speaker to channel mapping | ||
2184 |
2/2✓ Branch 0 taken 3906 times.
✓ Branch 1 taken 604 times.
|
4510 | for (i = 0; i < avctx->ch_layout.nb_channels; i++) |
2185 | 3906 | output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i]; | |
2186 | |||
2187 | // Allocate space for extra channels | ||
2188 | 604 | nchannels = av_popcount(s->ch_mask) - avctx->ch_layout.nb_channels; | |
2189 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 597 times.
|
604 | if (nchannels > 0) { |
2190 | 7 | av_fast_malloc(&s->output_buffer, &s->output_size, | |
2191 | 7 | nsamples * nchannels * sizeof(float)); | |
2192 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (!s->output_buffer) |
2193 | ✗ | return AVERROR(ENOMEM); | |
2194 | |||
2195 | 7 | ptr = (float *)s->output_buffer; | |
2196 |
2/2✓ Branch 0 taken 224 times.
✓ Branch 1 taken 7 times.
|
231 | for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) { |
2197 |
2/2✓ Branch 0 taken 182 times.
✓ Branch 1 taken 42 times.
|
224 | if (!(s->ch_mask & (1U << spkr))) |
2198 | 182 | continue; | |
2199 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
|
42 | if (output_samples[spkr]) |
2200 | 14 | continue; | |
2201 | 28 | output_samples[spkr] = ptr; | |
2202 | 28 | ptr += nsamples; | |
2203 | } | ||
2204 | } | ||
2205 | |||
2206 | // Handle change of filtering mode | ||
2207 | 604 | set_filter_mode(s, x96_synth); | |
2208 | |||
2209 | // Select filter | ||
2210 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 569 times.
|
604 | if (x96_synth) |
2211 | 35 | filter_coeff = ff_dca_fir_64bands; | |
2212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 569 times.
|
569 | else if (s->filter_perfect) |
2213 | ✗ | filter_coeff = ff_dca_fir_32bands_perfect; | |
2214 | else | ||
2215 | 569 | filter_coeff = ff_dca_fir_32bands_nonperfect; | |
2216 | |||
2217 | // Filter primary channels | ||
2218 |
2/2✓ Branch 0 taken 3331 times.
✓ Branch 1 taken 604 times.
|
3935 | for (ch = 0; ch < s->nchannels; ch++) { |
2219 | // Map this primary channel to speaker | ||
2220 | 3331 | spkr = map_prm_ch_to_spkr(s, ch); | |
2221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3331 times.
|
3331 | if (spkr < 0) |
2222 | ✗ | return AVERROR(EINVAL); | |
2223 | |||
2224 | // Filter bank reconstruction | ||
2225 | 3331 | s->dcadsp->sub_qmf_float[x96_synth]( | |
2226 | &s->synth, | ||
2227 | &s->imdct[x96_synth], | ||
2228 | output_samples[spkr], | ||
2229 | 3331 | s->subband_samples[ch], | |
2230 | ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL, | ||
2231 | 3331 | s->dcadsp_data[ch].u.flt.hist1, | |
2232 | &s->dcadsp_data[ch].offset, | ||
2233 | 3331 | s->dcadsp_data[ch].u.flt.hist2, | |
2234 | filter_coeff, | ||
2235 | 3331 | s->npcmblocks, | |
2236 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 3135 times.
|
3331 | 1.0f / (1 << (17 - x96_synth))); |
2237 | } | ||
2238 | |||
2239 | // Filter LFE channel | ||
2240 |
2/2✓ Branch 0 taken 603 times.
✓ Branch 1 taken 1 times.
|
604 | if (s->lfe_present) { |
2241 | 603 | int dec_select = (s->lfe_present == DCA_LFE_FLAG_128); | |
2242 | 603 | float *samples = output_samples[DCA_SPEAKER_LFE1]; | |
2243 | 603 | int nlfesamples = s->npcmblocks >> (dec_select + 1); | |
2244 | |||
2245 | // Offset intermediate buffer for X96 | ||
2246 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 568 times.
|
603 | if (x96_synth) |
2247 | 35 | samples += nsamples / 2; | |
2248 | |||
2249 | // Select filter | ||
2250 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 603 times.
|
603 | if (dec_select) |
2251 | ✗ | filter_coeff = ff_dca_lfe_fir_128; | |
2252 | else | ||
2253 | 603 | filter_coeff = ff_dca_lfe_fir_64; | |
2254 | |||
2255 | // Interpolate LFE channel | ||
2256 | 603 | s->dcadsp->lfe_fir_float[dec_select]( | |
2257 | 603 | samples, s->lfe_samples + DCA_LFE_HISTORY, | |
2258 | 603 | filter_coeff, s->npcmblocks); | |
2259 | |||
2260 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 568 times.
|
603 | if (x96_synth) { |
2261 | // Filter 96 kHz oversampled LFE PCM to attenuate high frequency | ||
2262 | // (47.6 - 48.0 kHz) components of interpolation image | ||
2263 | 35 | s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1], | |
2264 | samples, &s->output_history_lfe_float, | ||
2265 | 35 | nsamples / 2); | |
2266 | } | ||
2267 | |||
2268 | // Update LFE history | ||
2269 |
2/2✓ Branch 0 taken 4824 times.
✓ Branch 1 taken 603 times.
|
5427 | for (n = DCA_LFE_HISTORY - 1; n >= 0; n--) |
2270 | 4824 | s->lfe_samples[n] = s->lfe_samples[nlfesamples + n]; | |
2271 | } | ||
2272 | |||
2273 | // Undo embedded XCH downmix | ||
2274 |
4/4✓ Branch 0 taken 279 times.
✓ Branch 1 taken 325 times.
✓ Branch 2 taken 272 times.
✓ Branch 3 taken 7 times.
|
604 | if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH) |
2275 |
1/2✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
|
272 | && s->audio_mode >= DCA_AMODE_2F2R) { |
2276 | 272 | s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls], | |
2277 | 272 | output_samples[DCA_SPEAKER_Cs], | |
2278 | -M_SQRT1_2, nsamples); | ||
2279 | 272 | s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs], | |
2280 | 272 | output_samples[DCA_SPEAKER_Cs], | |
2281 | -M_SQRT1_2, nsamples); | ||
2282 | } | ||
2283 | |||
2284 | // Undo embedded XXCH downmix | ||
2285 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 583 times.
|
604 | if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) |
2286 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 | && s->xxch_dmix_embedded) { |
2287 | 21 | float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16)); | |
2288 | 21 | int *coeff_ptr = s->xxch_dmix_coeff; | |
2289 | 21 | int xch_base = ff_dca_channels[s->audio_mode]; | |
2290 | av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX); | ||
2291 | |||
2292 | // Undo downmix | ||
2293 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
|
63 | for (ch = xch_base; ch < s->nchannels; ch++) { |
2294 | 42 | int src_spkr = map_prm_ch_to_spkr(s, ch); | |
2295 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (src_spkr < 0) |
2296 | ✗ | return AVERROR(EINVAL); | |
2297 |
2/2✓ Branch 0 taken 462 times.
✓ Branch 1 taken 42 times.
|
504 | for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) { |
2298 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 420 times.
|
462 | if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) { |
2299 | 42 | int coeff = *coeff_ptr++; | |
2300 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (coeff) { |
2301 | 42 | s->float_dsp->vector_fmac_scalar(output_samples[ spkr], | |
2302 | 42 | output_samples[src_spkr], | |
2303 | coeff * (-1.0f / (1 << 15)), | ||
2304 | nsamples); | ||
2305 | } | ||
2306 | } | ||
2307 | } | ||
2308 | } | ||
2309 | |||
2310 | // Undo embedded core downmix pre-scaling | ||
2311 |
2/2✓ Branch 0 taken 231 times.
✓ Branch 1 taken 21 times.
|
252 | for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) { |
2312 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 105 times.
|
231 | if (s->xxch_core_mask & (1U << spkr)) { |
2313 | 126 | s->float_dsp->vector_fmul_scalar(output_samples[spkr], | |
2314 | 126 | output_samples[spkr], | |
2315 | scale_inv, nsamples); | ||
2316 | } | ||
2317 | } | ||
2318 | } | ||
2319 | |||
2320 |
2/2✓ Branch 0 taken 311 times.
✓ Branch 1 taken 293 times.
|
604 | if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) { |
2321 | // Front sum/difference decoding | ||
2322 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 311 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
311 | if ((s->sumdiff_front && s->audio_mode > DCA_AMODE_MONO) |
2323 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 311 times.
|
311 | || s->audio_mode == DCA_AMODE_STEREO_SUMDIFF) { |
2324 | ✗ | s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L], | |
2325 | output_samples[DCA_SPEAKER_R], | ||
2326 | nsamples); | ||
2327 | } | ||
2328 | |||
2329 | // Surround sum/difference decoding | ||
2330 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 311 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
311 | if (s->sumdiff_surround && s->audio_mode >= DCA_AMODE_2F2R) { |
2331 | ✗ | s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls], | |
2332 | output_samples[DCA_SPEAKER_Rs], | ||
2333 | nsamples); | ||
2334 | } | ||
2335 | } | ||
2336 | |||
2337 | // Downmix primary channel set to stereo | ||
2338 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 597 times.
|
604 | if (s->request_mask != s->ch_mask) { |
2339 | 7 | ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples, | |
2340 | 7 | s->prim_dmix_coeff, | |
2341 | nsamples, s->ch_mask); | ||
2342 | } | ||
2343 | |||
2344 | 604 | return 0; | |
2345 | } | ||
2346 | |||
2347 | 1122 | int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame) | |
2348 | { | ||
2349 | 1122 | AVCodecContext *avctx = s->avctx; | |
2350 | 1122 | DCAContext *dca = avctx->priv_data; | |
2351 | 1122 | DCAExssAsset *asset = &dca->exss.assets[0]; | |
2352 | enum AVMatrixEncoding matrix_encoding; | ||
2353 | int ret; | ||
2354 | |||
2355 | // Handle downmixing to stereo request | ||
2356 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1108 times.
|
1122 | if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO |
2357 |
3/4✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
|
14 | && s->audio_mode > DCA_AMODE_MONO && s->prim_dmix_embedded |
2358 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo || |
2359 | ✗ | s->prim_dmix_type == DCA_DMIX_TYPE_LtRt)) | |
2360 | 7 | s->request_mask = DCA_SPEAKER_LAYOUT_STEREO; | |
2361 | else | ||
2362 | 1115 | s->request_mask = s->ch_mask; | |
2363 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1122 times.
|
1122 | if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask)) |
2364 | ✗ | return AVERROR(EINVAL); | |
2365 | |||
2366 | // Force fixed point mode when falling back from XLL | ||
2367 |
4/4✓ Branch 0 taken 604 times.
✓ Branch 1 taken 518 times.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 548 times.
|
1122 | if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS) |
2368 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | && (asset->extension_mask & DCA_EXSS_XLL))) |
2369 | 518 | ret = filter_frame_fixed(s, frame); | |
2370 | else | ||
2371 | 604 | ret = filter_frame_float(s, frame); | |
2372 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1122 times.
|
1122 | if (ret < 0) |
2373 | ✗ | return ret; | |
2374 | |||
2375 | // Set profile, bit rate, etc | ||
2376 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1066 times.
|
1122 | if (s->ext_audio_mask & DCA_EXSS_MASK) |
2377 | 56 | avctx->profile = FF_PROFILE_DTS_HD_HRA; | |
2378 |
2/2✓ Branch 0 taken 258 times.
✓ Branch 1 taken 808 times.
|
1066 | else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH)) |
2379 | 258 | avctx->profile = FF_PROFILE_DTS_ES; | |
2380 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 801 times.
|
808 | else if (s->ext_audio_mask & DCA_CSS_X96) |
2381 | 7 | avctx->profile = FF_PROFILE_DTS_96_24; | |
2382 | else | ||
2383 | 801 | avctx->profile = FF_PROFILE_DTS; | |
2384 | |||
2385 |
3/4✓ Branch 0 taken 1122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1066 times.
✓ Branch 3 taken 56 times.
|
1122 | if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK)) |
2386 | 1066 | avctx->bit_rate = s->bit_rate; | |
2387 | else | ||
2388 | 56 | avctx->bit_rate = 0; | |
2389 | |||
2390 |
3/4✓ Branch 0 taken 1122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 1115 times.
|
1122 | if (s->audio_mode == DCA_AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask && |
2391 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | s->prim_dmix_type == DCA_DMIX_TYPE_LtRt)) |
2392 | ✗ | matrix_encoding = AV_MATRIX_ENCODING_DOLBY; | |
2393 | else | ||
2394 | 1122 | matrix_encoding = AV_MATRIX_ENCODING_NONE; | |
2395 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1122 times.
|
1122 | if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0) |
2396 | ✗ | return ret; | |
2397 | |||
2398 | 1122 | return 0; | |
2399 | } | ||
2400 | |||
2401 | ✗ | av_cold void ff_dca_core_flush(DCACoreDecoder *s) | |
2402 | { | ||
2403 | ✗ | if (s->subband_buffer) { | |
2404 | ✗ | erase_adpcm_history(s); | |
2405 | ✗ | memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t)); | |
2406 | } | ||
2407 | |||
2408 | ✗ | if (s->x96_subband_buffer) | |
2409 | ✗ | erase_x96_adpcm_history(s); | |
2410 | |||
2411 | ✗ | erase_dsp_history(s); | |
2412 | } | ||
2413 | |||
2414 | 92 | av_cold int ff_dca_core_init(DCACoreDecoder *s) | |
2415 | { | ||
2416 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
|
92 | if (!(s->float_dsp = avpriv_float_dsp_alloc(0))) |
2417 | ✗ | return -1; | |
2418 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
|
92 | if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0))) |
2419 | ✗ | return -1; | |
2420 | |||
2421 | 92 | ff_dcadct_init(&s->dcadct); | |
2422 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
|
92 | if (ff_mdct_init(&s->imdct[0], 6, 1, 1.0) < 0) |
2423 | ✗ | return -1; | |
2424 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
|
92 | if (ff_mdct_init(&s->imdct[1], 7, 1, 1.0) < 0) |
2425 | ✗ | return -1; | |
2426 | 92 | ff_synth_filter_init(&s->synth); | |
2427 | |||
2428 | 92 | s->x96_rand = 1; | |
2429 | 92 | return 0; | |
2430 | } | ||
2431 | |||
2432 | 92 | av_cold void ff_dca_core_close(DCACoreDecoder *s) | |
2433 | { | ||
2434 | 92 | av_freep(&s->float_dsp); | |
2435 | 92 | av_freep(&s->fixed_dsp); | |
2436 | |||
2437 | 92 | ff_mdct_end(&s->imdct[0]); | |
2438 | 92 | ff_mdct_end(&s->imdct[1]); | |
2439 | |||
2440 | 92 | av_freep(&s->subband_buffer); | |
2441 | 92 | s->subband_size = 0; | |
2442 | |||
2443 | 92 | av_freep(&s->x96_subband_buffer); | |
2444 | 92 | s->x96_subband_size = 0; | |
2445 | |||
2446 | 92 | av_freep(&s->output_buffer); | |
2447 | 92 | s->output_size = 0; | |
2448 | 92 | } | |
2449 |