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