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