c4e31ebebfb9bec6b30c3e18d003d5dc805041bf
[libav.git] / libavcodec / dcadec.c
1 /*
2 * DCA compatible decoder
3 * Copyright (C) 2004 Gildas Bazin
4 * Copyright (C) 2004 Benjamin Zores
5 * Copyright (C) 2006 Benjamin Larsson
6 * Copyright (C) 2007 Konstantin Shishkov
7 *
8 * This file is part of Libav.
9 *
10 * Libav is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * Libav is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with Libav; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include <math.h>
26 #include <stddef.h>
27 #include <stdio.h>
28
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #include "libavutil/float_dsp.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/samplefmt.h"
37
38 #include "avcodec.h"
39 #include "dca.h"
40 #include "dcadata.h"
41 #include "dcadsp.h"
42 #include "dcahuff.h"
43 #include "dca_exss.h"
44 #include "fft.h"
45 #include "fmtconvert.h"
46 #include "get_bits.h"
47 #include "internal.h"
48 #include "mathops.h"
49 #include "put_bits.h"
50 #include "synth_filter.h"
51
52 #if ARCH_ARM
53 # include "arm/dca.h"
54 #endif
55
56 enum DCAMode {
57 DCA_MONO = 0,
58 DCA_CHANNEL,
59 DCA_STEREO,
60 DCA_STEREO_SUMDIFF,
61 DCA_STEREO_TOTAL,
62 DCA_3F,
63 DCA_2F1R,
64 DCA_3F1R,
65 DCA_2F2R,
66 DCA_3F2R,
67 DCA_4F2R
68 };
69
70 /* -1 are reserved or unknown */
71 static const int dca_ext_audio_descr_mask[] = {
72 DCA_EXT_XCH,
73 -1,
74 DCA_EXT_X96,
75 DCA_EXT_XCH | DCA_EXT_X96,
76 -1,
77 -1,
78 DCA_EXT_XXCH,
79 -1,
80 };
81
82 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
83 * Some compromises have been made for special configurations. Most configurations
84 * are never used so complete accuracy is not needed.
85 *
86 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
87 * S -> side, when both rear and back are configured move one of them to the side channel
88 * OV -> center back
89 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
90 */
91 static const uint64_t dca_core_channel_layout[] = {
92 AV_CH_FRONT_CENTER, ///< 1, A
93 AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
94 AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
95 AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
96 AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
97 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
98 AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
99 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
100 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
101
102 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
103 AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
104
105 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
106 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
107
108 AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
109 AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
110
111 AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
112 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
113 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
114
115 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
116 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
117 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
118
119 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
120 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
121 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
122
123 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
124 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
125 AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
126 };
127
128 static const int8_t dca_lfe_index[] = {
129 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
130 };
131
132 static const int8_t dca_channel_reorder_lfe[][9] = {
133 { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
134 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
135 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
136 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
137 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
138 { 2, 0, 1, -1, -1, -1, -1, -1, -1 },
139 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
140 { 2, 0, 1, 4, -1, -1, -1, -1, -1 },
141 { 0, 1, 3, 4, -1, -1, -1, -1, -1 },
142 { 2, 0, 1, 4, 5, -1, -1, -1, -1 },
143 { 3, 4, 0, 1, 5, 6, -1, -1, -1 },
144 { 2, 0, 1, 4, 5, 6, -1, -1, -1 },
145 { 0, 6, 4, 5, 2, 3, -1, -1, -1 },
146 { 4, 2, 5, 0, 1, 6, 7, -1, -1 },
147 { 5, 6, 0, 1, 7, 3, 8, 4, -1 },
148 { 4, 2, 5, 0, 1, 6, 8, 7, -1 },
149 };
150
151 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
152 { 0, 2, -1, -1, -1, -1, -1, -1, -1 },
153 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
154 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
155 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
156 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
157 { 2, 0, 1, 4, -1, -1, -1, -1, -1 },
158 { 0, 1, 3, 4, -1, -1, -1, -1, -1 },
159 { 2, 0, 1, 4, 5, -1, -1, -1, -1 },
160 { 0, 1, 4, 5, 3, -1, -1, -1, -1 },
161 { 2, 0, 1, 5, 6, 4, -1, -1, -1 },
162 { 3, 4, 0, 1, 6, 7, 5, -1, -1 },
163 { 2, 0, 1, 4, 5, 6, 7, -1, -1 },
164 { 0, 6, 4, 5, 2, 3, 7, -1, -1 },
165 { 4, 2, 5, 0, 1, 7, 8, 6, -1 },
166 { 5, 6, 0, 1, 8, 3, 9, 4, 7 },
167 { 4, 2, 5, 0, 1, 6, 9, 8, 7 },
168 };
169
170 static const int8_t dca_channel_reorder_nolfe[][9] = {
171 { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
172 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
173 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
174 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
175 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
176 { 2, 0, 1, -1, -1, -1, -1, -1, -1 },
177 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
178 { 2, 0, 1, 3, -1, -1, -1, -1, -1 },
179 { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
180 { 2, 0, 1, 3, 4, -1, -1, -1, -1 },
181 { 2, 3, 0, 1, 4, 5, -1, -1, -1 },
182 { 2, 0, 1, 3, 4, 5, -1, -1, -1 },
183 { 0, 5, 3, 4, 1, 2, -1, -1, -1 },
184 { 3, 2, 4, 0, 1, 5, 6, -1, -1 },
185 { 4, 5, 0, 1, 6, 2, 7, 3, -1 },
186 { 3, 2, 4, 0, 1, 5, 7, 6, -1 },
187 };
188
189 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
190 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
191 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
192 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
193 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
194 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
195 { 2, 0, 1, 3, -1, -1, -1, -1, -1 },
196 { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
197 { 2, 0, 1, 3, 4, -1, -1, -1, -1 },
198 { 0, 1, 3, 4, 2, -1, -1, -1, -1 },
199 { 2, 0, 1, 4, 5, 3, -1, -1, -1 },
200 { 2, 3, 0, 1, 5, 6, 4, -1, -1 },
201 { 2, 0, 1, 3, 4, 5, 6, -1, -1 },
202 { 0, 5, 3, 4, 1, 2, 6, -1, -1 },
203 { 3, 2, 4, 0, 1, 6, 7, 5, -1 },
204 { 4, 5, 0, 1, 7, 2, 8, 3, 6 },
205 { 3, 2, 4, 0, 1, 5, 8, 7, 6 },
206 };
207
208 #define DCA_DOLBY 101 /* FIXME */
209
210 #define DCA_CHANNEL_BITS 6
211 #define DCA_CHANNEL_MASK 0x3F
212
213 #define DCA_LFE 0x80
214
215 #define HEADER_SIZE 14
216
217 #define DCA_NSYNCAUX 0x9A1105A0
218
219 /** Bit allocation */
220 typedef struct BitAlloc {
221 int offset; ///< code values offset
222 int maxbits[8]; ///< max bits in VLC
223 int wrap; ///< wrap for get_vlc2()
224 VLC vlc[8]; ///< actual codes
225 } BitAlloc;
226
227 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
228 static BitAlloc dca_tmode; ///< transition mode VLCs
229 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
230 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
231
232 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
233 int idx)
234 {
235 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
236 ba->offset;
237 }
238
239 static const uint16_t dca_vlc_offs[] = {
240 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
241 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
242 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
243 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
244 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
245 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
246 };
247
248 static av_cold void dca_init_vlcs(void)
249 {
250 static int vlcs_initialized = 0;
251 int i, j, c = 14;
252 static VLC_TYPE dca_table[23622][2];
253
254 if (vlcs_initialized)
255 return;
256
257 dca_bitalloc_index.offset = 1;
258 dca_bitalloc_index.wrap = 2;
259 for (i = 0; i < 5; i++) {
260 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
261 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
262 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
263 bitalloc_12_bits[i], 1, 1,
264 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
265 }
266 dca_scalefactor.offset = -64;
267 dca_scalefactor.wrap = 2;
268 for (i = 0; i < 5; i++) {
269 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
270 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
271 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
272 scales_bits[i], 1, 1,
273 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
274 }
275 dca_tmode.offset = 0;
276 dca_tmode.wrap = 1;
277 for (i = 0; i < 4; i++) {
278 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
279 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
280 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
281 tmode_bits[i], 1, 1,
282 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
283 }
284
285 for (i = 0; i < 10; i++)
286 for (j = 0; j < 7; j++) {
287 if (!bitalloc_codes[i][j])
288 break;
289 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
290 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
291 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
292 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
293
294 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
295 bitalloc_sizes[i],
296 bitalloc_bits[i][j], 1, 1,
297 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
298 c++;
299 }
300 vlcs_initialized = 1;
301 }
302
303 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
304 {
305 while (len--)
306 *dst++ = get_bits(gb, bits);
307 }
308
309 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
310 {
311 int i, j;
312 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
313 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
314 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
315
316 s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
317 s->prim_channels = s->total_channels;
318
319 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
320 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
321
322 for (i = base_channel; i < s->prim_channels; i++) {
323 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
324 if (s->subband_activity[i] > DCA_SUBBANDS)
325 s->subband_activity[i] = DCA_SUBBANDS;
326 }
327 for (i = base_channel; i < s->prim_channels; i++) {
328 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
329 if (s->vq_start_subband[i] > DCA_SUBBANDS)
330 s->vq_start_subband[i] = DCA_SUBBANDS;
331 }
332 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
333 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
334 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
335 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
336
337 /* Get codebooks quantization indexes */
338 if (!base_channel)
339 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
340 for (j = 1; j < 11; j++)
341 for (i = base_channel; i < s->prim_channels; i++)
342 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
343
344 /* Get scale factor adjustment */
345 for (j = 0; j < 11; j++)
346 for (i = base_channel; i < s->prim_channels; i++)
347 s->scalefactor_adj[i][j] = 1;
348
349 for (j = 1; j < 11; j++)
350 for (i = base_channel; i < s->prim_channels; i++)
351 if (s->quant_index_huffman[i][j] < thr[j])
352 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
353
354 if (s->crc_present) {
355 /* Audio header CRC check */
356 get_bits(&s->gb, 16);
357 }
358
359 s->current_subframe = 0;
360 s->current_subsubframe = 0;
361
362 return 0;
363 }
364
365 static int dca_parse_frame_header(DCAContext *s)
366 {
367 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
368
369 /* Sync code */
370 skip_bits_long(&s->gb, 32);
371
372 /* Frame header */
373 s->frame_type = get_bits(&s->gb, 1);
374 s->samples_deficit = get_bits(&s->gb, 5) + 1;
375 s->crc_present = get_bits(&s->gb, 1);
376 s->sample_blocks = get_bits(&s->gb, 7) + 1;
377 s->frame_size = get_bits(&s->gb, 14) + 1;
378 if (s->frame_size < 95)
379 return AVERROR_INVALIDDATA;
380 s->amode = get_bits(&s->gb, 6);
381 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
382 if (!s->sample_rate)
383 return AVERROR_INVALIDDATA;
384 s->bit_rate_index = get_bits(&s->gb, 5);
385 s->bit_rate = dca_bit_rates[s->bit_rate_index];
386 if (!s->bit_rate)
387 return AVERROR_INVALIDDATA;
388
389 skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
390 s->dynrange = get_bits(&s->gb, 1);
391 s->timestamp = get_bits(&s->gb, 1);
392 s->aux_data = get_bits(&s->gb, 1);
393 s->hdcd = get_bits(&s->gb, 1);
394 s->ext_descr = get_bits(&s->gb, 3);
395 s->ext_coding = get_bits(&s->gb, 1);
396 s->aspf = get_bits(&s->gb, 1);
397 s->lfe = get_bits(&s->gb, 2);
398 s->predictor_history = get_bits(&s->gb, 1);
399
400 if (s->lfe > 2) {
401 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
402 return AVERROR_INVALIDDATA;
403 }
404
405 /* TODO: check CRC */
406 if (s->crc_present)
407 s->header_crc = get_bits(&s->gb, 16);
408
409 s->multirate_inter = get_bits(&s->gb, 1);
410 s->version = get_bits(&s->gb, 4);
411 s->copy_history = get_bits(&s->gb, 2);
412 s->source_pcm_res = get_bits(&s->gb, 3);
413 s->front_sum = get_bits(&s->gb, 1);
414 s->surround_sum = get_bits(&s->gb, 1);
415 s->dialog_norm = get_bits(&s->gb, 4);
416
417 /* FIXME: channels mixing levels */
418 s->output = s->amode;
419 if (s->lfe)
420 s->output |= DCA_LFE;
421
422 /* Primary audio coding header */
423 s->subframes = get_bits(&s->gb, 4) + 1;
424
425 return dca_parse_audio_coding_header(s, 0);
426 }
427
428 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
429 {
430 if (level < 5) {
431 /* huffman encoded */
432 value += get_bitalloc(gb, &dca_scalefactor, level);
433 value = av_clip(value, 0, (1 << log2range) - 1);
434 } else if (level < 8) {
435 if (level + 1 > log2range) {
436 skip_bits(gb, level + 1 - log2range);
437 value = get_bits(gb, log2range);
438 } else {
439 value = get_bits(gb, level + 1);
440 }
441 }
442 return value;
443 }
444
445 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
446 {
447 /* Primary audio coding side information */
448 int j, k;
449
450 if (get_bits_left(&s->gb) < 0)
451 return AVERROR_INVALIDDATA;
452
453 if (!base_channel) {
454 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
455 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
456 }
457
458 for (j = base_channel; j < s->prim_channels; j++) {
459 for (k = 0; k < s->subband_activity[j]; k++)
460 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
461 }
462
463 /* Get prediction codebook */
464 for (j = base_channel; j < s->prim_channels; j++) {
465 for (k = 0; k < s->subband_activity[j]; k++) {
466 if (s->prediction_mode[j][k] > 0) {
467 /* (Prediction coefficient VQ address) */
468 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
469 }
470 }
471 }
472
473 /* Bit allocation index */
474 for (j = base_channel; j < s->prim_channels; j++) {
475 for (k = 0; k < s->vq_start_subband[j]; k++) {
476 if (s->bitalloc_huffman[j] == 6)
477 s->bitalloc[j][k] = get_bits(&s->gb, 5);
478 else if (s->bitalloc_huffman[j] == 5)
479 s->bitalloc[j][k] = get_bits(&s->gb, 4);
480 else if (s->bitalloc_huffman[j] == 7) {
481 av_log(s->avctx, AV_LOG_ERROR,
482 "Invalid bit allocation index\n");
483 return AVERROR_INVALIDDATA;
484 } else {
485 s->bitalloc[j][k] =
486 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
487 }
488
489 if (s->bitalloc[j][k] > 26) {
490 av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
491 j, k, s->bitalloc[j][k]);
492 return AVERROR_INVALIDDATA;
493 }
494 }
495 }
496
497 /* Transition mode */
498 for (j = base_channel; j < s->prim_channels; j++) {
499 for (k = 0; k < s->subband_activity[j]; k++) {
500 s->transition_mode[j][k] = 0;
501 if (s->subsubframes[s->current_subframe] > 1 &&
502 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
503 s->transition_mode[j][k] =
504 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
505 }
506 }
507 }
508
509 if (get_bits_left(&s->gb) < 0)
510 return AVERROR_INVALIDDATA;
511
512 for (j = base_channel; j < s->prim_channels; j++) {
513 const uint32_t *scale_table;
514 int scale_sum, log_size;
515
516 memset(s->scale_factor[j], 0,
517 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
518
519 if (s->scalefactor_huffman[j] == 6) {
520 scale_table = scale_factor_quant7;
521 log_size = 7;
522 } else {
523 scale_table = scale_factor_quant6;
524 log_size = 6;
525 }
526
527 /* When huffman coded, only the difference is encoded */
528 scale_sum = 0;
529
530 for (k = 0; k < s->subband_activity[j]; k++) {
531 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
532 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
533 s->scale_factor[j][k][0] = scale_table[scale_sum];
534 }
535
536 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
537 /* Get second scale factor */
538 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
539 s->scale_factor[j][k][1] = scale_table[scale_sum];
540 }
541 }
542 }
543
544 /* Joint subband scale factor codebook select */
545 for (j = base_channel; j < s->prim_channels; j++) {
546 /* Transmitted only if joint subband coding enabled */
547 if (s->joint_intensity[j] > 0)
548 s->joint_huff[j] = get_bits(&s->gb, 3);
549 }
550
551 if (get_bits_left(&s->gb) < 0)
552 return AVERROR_INVALIDDATA;
553
554 /* Scale factors for joint subband coding */
555 for (j = base_channel; j < s->prim_channels; j++) {
556 int source_channel;
557
558 /* Transmitted only if joint subband coding enabled */
559 if (s->joint_intensity[j] > 0) {
560 int scale = 0;
561 source_channel = s->joint_intensity[j] - 1;
562
563 /* When huffman coded, only the difference is encoded
564 * (is this valid as well for joint scales ???) */
565
566 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
567 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
568 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
569 }
570
571 if (!(s->debug_flag & 0x02)) {
572 av_log(s->avctx, AV_LOG_DEBUG,
573 "Joint stereo coding not supported\n");
574 s->debug_flag |= 0x02;
575 }
576 }
577 }
578
579 /* Dynamic range coefficient */
580 if (!base_channel && s->dynrange)
581 s->dynrange_coef = get_bits(&s->gb, 8);
582
583 /* Side information CRC check word */
584 if (s->crc_present) {
585 get_bits(&s->gb, 16);
586 }
587
588 /*
589 * Primary audio data arrays
590 */
591
592 /* VQ encoded high frequency subbands */
593 for (j = base_channel; j < s->prim_channels; j++)
594 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
595 /* 1 vector -> 32 samples */
596 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
597
598 /* Low frequency effect data */
599 if (!base_channel && s->lfe) {
600 /* LFE samples */
601 int lfe_samples = 2 * s->lfe * (4 + block_index);
602 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
603 float lfe_scale;
604
605 for (j = lfe_samples; j < lfe_end_sample; j++) {
606 /* Signed 8 bits int */
607 s->lfe_data[j] = get_sbits(&s->gb, 8);
608 }
609
610 /* Scale factor index */
611 skip_bits(&s->gb, 1);
612 s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 7)];
613
614 /* Quantization step size * scale factor */
615 lfe_scale = 0.035 * s->lfe_scale_factor;
616
617 for (j = lfe_samples; j < lfe_end_sample; j++)
618 s->lfe_data[j] *= lfe_scale;
619 }
620
621 return 0;
622 }
623
624 static void qmf_32_subbands(DCAContext *s, int chans,
625 float samples_in[32][8], float *samples_out,
626 float scale)
627 {
628 const float *prCoeff;
629
630 int sb_act = s->subband_activity[chans];
631
632 scale *= sqrt(1 / 8.0);
633
634 /* Select filter */
635 if (!s->multirate_inter) /* Non-perfect reconstruction */
636 prCoeff = fir_32bands_nonperfect;
637 else /* Perfect reconstruction */
638 prCoeff = fir_32bands_perfect;
639
640 s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
641 s->subband_fir_hist[chans],
642 &s->hist_index[chans],
643 s->subband_fir_noidea[chans], prCoeff,
644 samples_out, s->raXin, scale);
645 }
646
647 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
648 int num_deci_sample, float *samples_in,
649 float *samples_out)
650 {
651 /* samples_in: An array holding decimated samples.
652 * Samples in current subframe starts from samples_in[0],
653 * while samples_in[-1], samples_in[-2], ..., stores samples
654 * from last subframe as history.
655 *
656 * samples_out: An array holding interpolated samples
657 */
658
659 int idx;
660 const float *prCoeff;
661 int deciindex;
662
663 /* Select decimation filter */
664 if (decimation_select == 1) {
665 idx = 1;
666 prCoeff = lfe_fir_128;
667 } else {
668 idx = 0;
669 prCoeff = lfe_fir_64;
670 }
671 /* Interpolation */
672 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
673 s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
674 samples_in++;
675 samples_out += 2 * 32 * (1 + idx);
676 }
677 }
678
679 /* downmixing routines */
680 #define MIX_REAR1(samples, s1, rs, coef) \
681 samples[0][i] += samples[s1][i] * coef[rs][0]; \
682 samples[1][i] += samples[s1][i] * coef[rs][1];
683
684 #define MIX_REAR2(samples, s1, s2, rs, coef) \
685 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
686 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
687
688 #define MIX_FRONT3(samples, coef) \
689 t = samples[c][i]; \
690 u = samples[l][i]; \
691 v = samples[r][i]; \
692 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
693 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
694
695 #define DOWNMIX_TO_STEREO(op1, op2) \
696 for (i = 0; i < 256; i++) { \
697 op1 \
698 op2 \
699 }
700
701 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
702 float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
703 const int8_t *channel_mapping)
704 {
705 int c, l, r, sl, sr, s;
706 int i;
707 float t, u, v;
708
709 switch (srcfmt) {
710 case DCA_MONO:
711 case DCA_4F2R:
712 av_log(NULL, 0, "Not implemented!\n");
713 break;
714 case DCA_CHANNEL:
715 case DCA_STEREO:
716 case DCA_STEREO_TOTAL:
717 case DCA_STEREO_SUMDIFF:
718 break;
719 case DCA_3F:
720 c = channel_mapping[0];
721 l = channel_mapping[1];
722 r = channel_mapping[2];
723 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
724 break;
725 case DCA_2F1R:
726 s = channel_mapping[2];
727 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
728 break;
729 case DCA_3F1R:
730 c = channel_mapping[0];
731 l = channel_mapping[1];
732 r = channel_mapping[2];
733 s = channel_mapping[3];
734 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
735 MIX_REAR1(samples, s, 3, coef));
736 break;
737 case DCA_2F2R:
738 sl = channel_mapping[2];
739 sr = channel_mapping[3];
740 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
741 break;
742 case DCA_3F2R:
743 c = channel_mapping[0];
744 l = channel_mapping[1];
745 r = channel_mapping[2];
746 sl = channel_mapping[3];
747 sr = channel_mapping[4];
748 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
749 MIX_REAR2(samples, sl, sr, 3, coef));
750 break;
751 }
752 if (lfe_present) {
753 int lf_buf = dca_lfe_index[srcfmt];
754 int lf_idx = dca_channels[srcfmt];
755 for (i = 0; i < 256; i++) {
756 samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
757 samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
758 }
759 }
760 }
761
762 #ifndef decode_blockcodes
763 /* Very compact version of the block code decoder that does not use table
764 * look-up but is slightly slower */
765 static int decode_blockcode(int code, int levels, int32_t *values)
766 {
767 int i;
768 int offset = (levels - 1) >> 1;
769
770 for (i = 0; i < 4; i++) {
771 int div = FASTDIV(code, levels);
772 values[i] = code - offset - div * levels;
773 code = div;
774 }
775
776 return code;
777 }
778
779 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
780 {
781 return decode_blockcode(code1, levels, values) |
782 decode_blockcode(code2, levels, values + 4);
783 }
784 #endif
785
786 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
787 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
788
789 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
790 {
791 int k, l;
792 int subsubframe = s->current_subsubframe;
793
794 const float *quant_step_table;
795
796 /* FIXME */
797 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
798 LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
799
800 /*
801 * Audio data
802 */
803
804 /* Select quantization step size table */
805 if (s->bit_rate_index == 0x1f)
806 quant_step_table = lossless_quant_d;
807 else
808 quant_step_table = lossy_quant_d;
809
810 for (k = base_channel; k < s->prim_channels; k++) {
811 float rscale[DCA_SUBBANDS];
812
813 if (get_bits_left(&s->gb) < 0)
814 return AVERROR_INVALIDDATA;
815
816 for (l = 0; l < s->vq_start_subband[k]; l++) {
817 int m;
818
819 /* Select the mid-tread linear quantizer */
820 int abits = s->bitalloc[k][l];
821
822 float quant_step_size = quant_step_table[abits];
823
824 /*
825 * Determine quantization index code book and its type
826 */
827
828 /* Select quantization index code book */
829 int sel = s->quant_index_huffman[k][abits];
830
831 /*
832 * Extract bits from the bit stream
833 */
834 if (!abits) {
835 rscale[l] = 0;
836 memset(block + 8 * l, 0, 8 * sizeof(block[0]));
837 } else {
838 /* Deal with transients */
839 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
840 rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
841 s->scalefactor_adj[k][sel];
842
843 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
844 if (abits <= 7) {
845 /* Block code */
846 int block_code1, block_code2, size, levels, err;
847
848 size = abits_sizes[abits - 1];
849 levels = abits_levels[abits - 1];
850
851 block_code1 = get_bits(&s->gb, size);
852 block_code2 = get_bits(&s->gb, size);
853 err = decode_blockcodes(block_code1, block_code2,
854 levels, block + 8 * l);
855 if (err) {
856 av_log(s->avctx, AV_LOG_ERROR,
857 "ERROR: block code look-up failed\n");
858 return AVERROR_INVALIDDATA;
859 }
860 } else {
861 /* no coding */
862 for (m = 0; m < 8; m++)
863 block[8 * l + m] = get_sbits(&s->gb, abits - 3);
864 }
865 } else {
866 /* Huffman coded */
867 for (m = 0; m < 8; m++)
868 block[8 * l + m] = get_bitalloc(&s->gb,
869 &dca_smpl_bitalloc[abits], sel);
870 }
871 }
872 }
873
874 s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
875 block, rscale, 8 * s->vq_start_subband[k]);
876
877 for (l = 0; l < s->vq_start_subband[k]; l++) {
878 int m;
879 /*
880 * Inverse ADPCM if in prediction mode
881 */
882 if (s->prediction_mode[k][l]) {
883 int n;
884 if (s->predictor_history)
885 subband_samples[k][l][0] += (adpcm_vb[s->prediction_vq[k][l]][0] *
886 s->subband_samples_hist[k][l][3] +
887 adpcm_vb[s->prediction_vq[k][l]][1] *
888 s->subband_samples_hist[k][l][2] +
889 adpcm_vb[s->prediction_vq[k][l]][2] *
890 s->subband_samples_hist[k][l][1] +
891 adpcm_vb[s->prediction_vq[k][l]][3] *
892 s->subband_samples_hist[k][l][0]) *
893 (1.0f / 8192);
894 for (m = 1; m < 8; m++) {
895 float sum = adpcm_vb[s->prediction_vq[k][l]][0] *
896 subband_samples[k][l][m - 1];
897 for (n = 2; n <= 4; n++)
898 if (m >= n)
899 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
900 subband_samples[k][l][m - n];
901 else if (s->predictor_history)
902 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
903 s->subband_samples_hist[k][l][m - n + 4];
904 subband_samples[k][l][m] += sum * 1.0f / 8192;
905 }
906 }
907 }
908
909 /*
910 * Decode VQ encoded high frequencies
911 */
912 if (s->subband_activity[k] > s->vq_start_subband[k]) {
913 if (!s->debug_flag & 0x01) {
914 av_log(s->avctx, AV_LOG_DEBUG,
915 "Stream with high frequencies VQ coding\n");
916 s->debug_flag |= 0x01;
917 }
918 s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
919 high_freq_vq, subsubframe * 8,
920 s->scale_factor[k], s->vq_start_subband[k],
921 s->subband_activity[k]);
922 }
923 }
924
925 /* Check for DSYNC after subsubframe */
926 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
927 if (get_bits(&s->gb, 16) != 0xFFFF) {
928 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
929 return AVERROR_INVALIDDATA;
930 }
931 }
932
933 /* Backup predictor history for adpcm */
934 for (k = base_channel; k < s->prim_channels; k++)
935 for (l = 0; l < s->vq_start_subband[k]; l++)
936 AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
937
938 return 0;
939 }
940
941 static int dca_filter_channels(DCAContext *s, int block_index)
942 {
943 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
944 int k;
945
946 /* 32 subbands QMF */
947 for (k = 0; k < s->prim_channels; k++) {
948 if (s->channel_order_tab[k] >= 0)
949 qmf_32_subbands(s, k, subband_samples[k],
950 s->samples_chanptr[s->channel_order_tab[k]],
951 M_SQRT1_2 / 32768.0);
952 }
953
954 /* Generate LFE samples for this subsubframe FIXME!!! */
955 if (s->lfe) {
956 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
957 s->lfe_data + 2 * s->lfe * (block_index + 4),
958 s->samples_chanptr[dca_lfe_index[s->amode]]);
959 /* Outputs 20bits pcm samples */
960 }
961
962 /* Downmixing to Stereo */
963 if (s->prim_channels + !!s->lfe > 2 &&
964 s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
965 dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
966 s->channel_order_tab);
967 }
968
969 return 0;
970 }
971
972 static int dca_subframe_footer(DCAContext *s, int base_channel)
973 {
974 int in, out, aux_data_count, aux_data_end, reserved;
975 uint32_t nsyncaux;
976
977 /*
978 * Unpack optional information
979 */
980
981 /* presumably optional information only appears in the core? */
982 if (!base_channel) {
983 if (s->timestamp)
984 skip_bits_long(&s->gb, 32);
985
986 if (s->aux_data) {
987 aux_data_count = get_bits(&s->gb, 6);
988
989 // align (32-bit)
990 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
991
992 aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
993
994 if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
995 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
996 nsyncaux);
997 return AVERROR_INVALIDDATA;
998 }
999
1000 if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1001 avpriv_request_sample(s->avctx,
1002 "Auxiliary Decode Time Stamp Flag");
1003 // align (4-bit)
1004 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1005 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1006 skip_bits_long(&s->gb, 44);
1007 }
1008
1009 if ((s->core_downmix = get_bits1(&s->gb))) {
1010 int am = get_bits(&s->gb, 3);
1011 switch (am) {
1012 case 0:
1013 s->core_downmix_amode = DCA_MONO;
1014 break;
1015 case 1:
1016 s->core_downmix_amode = DCA_STEREO;
1017 break;
1018 case 2:
1019 s->core_downmix_amode = DCA_STEREO_TOTAL;
1020 break;
1021 case 3:
1022 s->core_downmix_amode = DCA_3F;
1023 break;
1024 case 4:
1025 s->core_downmix_amode = DCA_2F1R;
1026 break;
1027 case 5:
1028 s->core_downmix_amode = DCA_2F2R;
1029 break;
1030 case 6:
1031 s->core_downmix_amode = DCA_3F1R;
1032 break;
1033 default:
1034 av_log(s->avctx, AV_LOG_ERROR,
1035 "Invalid mode %d for embedded downmix coefficients\n",
1036 am);
1037 return AVERROR_INVALIDDATA;
1038 }
1039 for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
1040 for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1041 uint16_t tmp = get_bits(&s->gb, 9);
1042 if ((tmp & 0xFF) > 241) {
1043 av_log(s->avctx, AV_LOG_ERROR,
1044 "Invalid downmix coefficient code %"PRIu16"\n",
1045 tmp);
1046 return AVERROR_INVALIDDATA;
1047 }
1048 s->core_downmix_codes[in][out] = tmp;
1049 }
1050 }
1051 }
1052
1053 align_get_bits(&s->gb); // byte align
1054 skip_bits(&s->gb, 16); // nAUXCRC16
1055
1056 // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1057 if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
1058 av_log(s->avctx, AV_LOG_ERROR,
1059 "Overread auxiliary data by %d bits\n", -reserved);
1060 return AVERROR_INVALIDDATA;
1061 } else if (reserved) {
1062 avpriv_request_sample(s->avctx,
1063 "Core auxiliary data reserved content");
1064 skip_bits_long(&s->gb, reserved);
1065 }
1066 }
1067
1068 if (s->crc_present && s->dynrange)
1069 get_bits(&s->gb, 16);
1070 }
1071
1072 return 0;
1073 }
1074
1075 /**
1076 * Decode a dca frame block
1077 *
1078 * @param s pointer to the DCAContext
1079 */
1080
1081 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1082 {
1083 int ret;
1084
1085 /* Sanity check */
1086 if (s->current_subframe >= s->subframes) {
1087 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1088 s->current_subframe, s->subframes);
1089 return AVERROR_INVALIDDATA;
1090 }
1091
1092 if (!s->current_subsubframe) {
1093 /* Read subframe header */
1094 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1095 return ret;
1096 }
1097
1098 /* Read subsubframe */
1099 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1100 return ret;
1101
1102 /* Update state */
1103 s->current_subsubframe++;
1104 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1105 s->current_subsubframe = 0;
1106 s->current_subframe++;
1107 }
1108 if (s->current_subframe >= s->subframes) {
1109 /* Read subframe footer */
1110 if ((ret = dca_subframe_footer(s, base_channel)))
1111 return ret;
1112 }
1113
1114 return 0;
1115 }
1116
1117 static float dca_dmix_code(unsigned code)
1118 {
1119 int sign = (code >> 8) - 1;
1120 code &= 0xff;
1121 return ((dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1U << 15));
1122 }
1123
1124 /**
1125 * Main frame decoding function
1126 * FIXME add arguments
1127 */
1128 static int dca_decode_frame(AVCodecContext *avctx, void *data,
1129 int *got_frame_ptr, AVPacket *avpkt)
1130 {
1131 AVFrame *frame = data;
1132 const uint8_t *buf = avpkt->data;
1133 int buf_size = avpkt->size;
1134
1135 int lfe_samples;
1136 int num_core_channels = 0;
1137 int i, ret;
1138 float **samples_flt;
1139 DCAContext *s = avctx->priv_data;
1140 int channels, full_channels;
1141 int core_ss_end;
1142
1143 s->xch_present = 0;
1144
1145 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1146 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1147 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1148 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1149 return AVERROR_INVALIDDATA;
1150 }
1151
1152 if ((ret = dca_parse_frame_header(s)) < 0) {
1153 // seems like the frame is corrupt, try with the next one
1154 return ret;
1155 }
1156 // set AVCodec values with parsed data
1157 avctx->sample_rate = s->sample_rate;
1158 avctx->bit_rate = s->bit_rate;
1159
1160 s->profile = FF_PROFILE_DTS;
1161
1162 for (i = 0; i < (s->sample_blocks / 8); i++) {
1163 if ((ret = dca_decode_block(s, 0, i))) {
1164 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1165 return ret;
1166 }
1167 }
1168
1169 /* record number of core channels incase less than max channels are requested */
1170 num_core_channels = s->prim_channels;
1171
1172 if (s->ext_coding)
1173 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
1174 else
1175 s->core_ext_mask = 0;
1176
1177 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1178
1179 /* only scan for extensions if ext_descr was unknown or indicated a
1180 * supported XCh extension */
1181 if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
1182 /* if ext_descr was unknown, clear s->core_ext_mask so that the
1183 * extensions scan can fill it up */
1184 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1185
1186 /* extensions start at 32-bit boundaries into bitstream */
1187 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1188
1189 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1190 uint32_t bits = get_bits_long(&s->gb, 32);
1191
1192 switch (bits) {
1193 case 0x5a5a5a5a: {
1194 int ext_amode, xch_fsize;
1195
1196 s->xch_base_channel = s->prim_channels;
1197
1198 /* validate sync word using XCHFSIZE field */
1199 xch_fsize = show_bits(&s->gb, 10);
1200 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1201 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1202 continue;
1203
1204 /* skip length-to-end-of-frame field for the moment */
1205 skip_bits(&s->gb, 10);
1206
1207 s->core_ext_mask |= DCA_EXT_XCH;
1208
1209 /* extension amode(number of channels in extension) should be 1 */
1210 /* AFAIK XCh is not used for more channels */
1211 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1212 av_log(avctx, AV_LOG_ERROR,
1213 "XCh extension amode %d not supported!\n",
1214 ext_amode);
1215 continue;
1216 }
1217
1218 /* much like core primary audio coding header */
1219 dca_parse_audio_coding_header(s, s->xch_base_channel);
1220
1221 for (i = 0; i < (s->sample_blocks / 8); i++)
1222 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1223 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1224 continue;
1225 }
1226
1227 s->xch_present = 1;
1228 break;
1229 }
1230 case 0x47004a03:
1231 /* XXCh: extended channels */
1232 /* usually found either in core or HD part in DTS-HD HRA streams,
1233 * but not in DTS-ES which contains XCh extensions instead */
1234 s->core_ext_mask |= DCA_EXT_XXCH;
1235 break;
1236
1237 case 0x1d95f262: {
1238 int fsize96 = show_bits(&s->gb, 12) + 1;
1239 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1240 continue;
1241
1242 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1243 get_bits_count(&s->gb));
1244 skip_bits(&s->gb, 12);
1245 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1246 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1247
1248 s->core_ext_mask |= DCA_EXT_X96;
1249 break;
1250 }
1251 }
1252
1253 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1254 }
1255 } else {
1256 /* no supported extensions, skip the rest of the core substream */
1257 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1258 }
1259
1260 if (s->core_ext_mask & DCA_EXT_X96)
1261 s->profile = FF_PROFILE_DTS_96_24;
1262 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1263 s->profile = FF_PROFILE_DTS_ES;
1264
1265 /* check for ExSS (HD part) */
1266 if (s->dca_buffer_size - s->frame_size > 32 &&
1267 get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
1268 ff_dca_exss_parse_header(s);
1269
1270 avctx->profile = s->profile;
1271
1272 full_channels = channels = s->prim_channels + !!s->lfe;
1273
1274 if (s->amode < 16) {
1275 avctx->channel_layout = dca_core_channel_layout[s->amode];
1276
1277 if (s->prim_channels + !!s->lfe > 2 &&
1278 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1279 /*
1280 * Neither the core's auxiliary data nor our default tables contain
1281 * downmix coefficients for the additional channel coded in the XCh
1282 * extension, so when we're doing a Stereo downmix, don't decode it.
1283 */
1284 s->xch_disable = 1;
1285 }
1286
1287 #if FF_API_REQUEST_CHANNELS
1288 FF_DISABLE_DEPRECATION_WARNINGS
1289 if (s->xch_present && !s->xch_disable &&
1290 (!avctx->request_channels ||
1291 avctx->request_channels > num_core_channels + !!s->lfe)) {
1292 FF_ENABLE_DEPRECATION_WARNINGS
1293 #else
1294 if (s->xch_present && !s->xch_disable) {
1295 #endif
1296 avctx->channel_layout |= AV_CH_BACK_CENTER;
1297 if (s->lfe) {
1298 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1299 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
1300 } else {
1301 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
1302 }
1303 } else {
1304 channels = num_core_channels + !!s->lfe;
1305 s->xch_present = 0; /* disable further xch processing */
1306 if (s->lfe) {
1307 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1308 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1309 } else
1310 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
1311 }
1312
1313 if (channels > !!s->lfe &&
1314 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1315 return AVERROR_INVALIDDATA;
1316
1317 if (num_core_channels + !!s->lfe > 2 &&
1318 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1319 channels = 2;
1320 s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
1321 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
1322
1323 /* Stereo downmix coefficients
1324 *
1325 * The decoder can only downmix to 2-channel, so we need to ensure
1326 * embedded downmix coefficients are actually targeting 2-channel.
1327 */
1328 if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
1329 s->core_downmix_amode == DCA_STEREO_TOTAL)) {
1330 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1331 /* Range checked earlier */
1332 s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
1333 s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
1334 }
1335 s->output = s->core_downmix_amode;
1336 } else {
1337 int am = s->amode & DCA_CHANNEL_MASK;
1338 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
1339 av_log(s->avctx, AV_LOG_ERROR,
1340 "Invalid channel mode %d\n", am);
1341 return AVERROR_INVALIDDATA;
1342 }
1343 if (num_core_channels + !!s->lfe >
1344 FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
1345 avpriv_request_sample(s->avctx, "Downmixing %d channels",
1346 s->prim_channels + !!s->lfe);
1347 return AVERROR_PATCHWELCOME;
1348 }
1349 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1350 s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
1351 s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
1352 }
1353 }
1354 av_dlog(s->avctx, "Stereo downmix coeffs:\n");
1355 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1356 av_dlog(s->avctx, "L, input channel %d = %f\n", i,
1357 s->downmix_coef[i][0]);
1358 av_dlog(s->avctx, "R, input channel %d = %f\n", i,
1359 s->downmix_coef[i][1]);
1360 }
1361 av_dlog(s->avctx, "\n");
1362 }
1363 } else {
1364 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
1365 return AVERROR_INVALIDDATA;
1366 }
1367 avctx->channels = channels;
1368
1369 /* get output buffer */
1370 frame->nb_samples = 256 * (s->sample_blocks / 8);
1371 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1372 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1373 return ret;
1374 }
1375 samples_flt = (float **) frame->extended_data;
1376
1377 /* allocate buffer for extra channels if downmixing */
1378 if (avctx->channels < full_channels) {
1379 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
1380 frame->nb_samples,
1381 avctx->sample_fmt, 0);
1382 if (ret < 0)
1383 return ret;
1384
1385 av_fast_malloc(&s->extra_channels_buffer,
1386 &s->extra_channels_buffer_size, ret);
1387 if (!s->extra_channels_buffer)
1388 return AVERROR(ENOMEM);
1389
1390 ret = av_samples_fill_arrays((uint8_t **) s->extra_channels, NULL,
1391 s->extra_channels_buffer,
1392 full_channels - channels,
1393 frame->nb_samples, avctx->sample_fmt, 0);
1394 if (ret < 0)
1395 return ret;
1396 }
1397
1398 /* filter to get final output */
1399 for (i = 0; i < (s->sample_blocks / 8); i++) {
1400 int ch;
1401
1402 for (ch = 0; ch < channels; ch++)
1403 s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
1404 for (; ch < full_channels; ch++)
1405 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
1406
1407 dca_filter_channels(s, i);
1408
1409 /* If this was marked as a DTS-ES stream we need to subtract back- */
1410 /* channel from SL & SR to remove matrixed back-channel signal */
1411 if ((s->source_pcm_res & 1) && s->xch_present) {
1412 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
1413 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
1414 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
1415 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1416 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
1417 }
1418 }
1419
1420 /* update lfe history */
1421 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
1422 for (i = 0; i < 2 * s->lfe * 4; i++)
1423 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1424
1425 /* AVMatrixEncoding
1426 *
1427 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
1428 ret = ff_side_data_update_matrix_encoding(frame,
1429 (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
1430 AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
1431 if (ret < 0)
1432 return ret;
1433
1434 *got_frame_ptr = 1;
1435
1436 return buf_size;
1437 }
1438
1439 /**
1440 * DCA initialization
1441 *
1442 * @param avctx pointer to the AVCodecContext
1443 */
1444
1445 static av_cold int dca_decode_init(AVCodecContext *avctx)
1446 {
1447 DCAContext *s = avctx->priv_data;
1448
1449 s->avctx = avctx;
1450 dca_init_vlcs();
1451
1452 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
1453 ff_mdct_init(&s->imdct, 6, 1, 1.0);
1454 ff_synth_filter_init(&s->synth);
1455 ff_dcadsp_init(&s->dcadsp);
1456 ff_fmt_convert_init(&s->fmt_conv, avctx);
1457
1458 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1459
1460 /* allow downmixing to stereo */
1461 #if FF_API_REQUEST_CHANNELS
1462 FF_DISABLE_DEPRECATION_WARNINGS
1463 if (avctx->request_channels == 2)
1464 avctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
1465 FF_ENABLE_DEPRECATION_WARNINGS
1466 #endif
1467 if (avctx->channels > 2 &&
1468 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
1469 avctx->channels = 2;
1470
1471 return 0;
1472 }
1473
1474 static av_cold int dca_decode_end(AVCodecContext *avctx)
1475 {
1476 DCAContext *s = avctx->priv_data;
1477 ff_mdct_end(&s->imdct);
1478 av_freep(&s->extra_channels_buffer);
1479 return 0;
1480 }
1481
1482 static const AVProfile profiles[] = {
1483 { FF_PROFILE_DTS, "DTS" },
1484 { FF_PROFILE_DTS_ES, "DTS-ES" },
1485 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
1486 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
1487 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
1488 { FF_PROFILE_UNKNOWN },
1489 };
1490
1491 static const AVOption options[] = {
1492 { "disable_xch", "disable decoding of the XCh extension", offsetof(DCAContext, xch_disable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
1493 { NULL },
1494 };
1495
1496 static const AVClass dca_decoder_class = {
1497 .class_name = "DCA decoder",
1498 .item_name = av_default_item_name,
1499 .option = options,
1500 .version = LIBAVUTIL_VERSION_INT,
1501 };
1502
1503 AVCodec ff_dca_decoder = {
1504 .name = "dca",
1505 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
1506 .type = AVMEDIA_TYPE_AUDIO,
1507 .id = AV_CODEC_ID_DTS,
1508 .priv_data_size = sizeof(DCAContext),
1509 .init = dca_decode_init,
1510 .decode = dca_decode_frame,
1511 .close = dca_decode_end,
1512 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
1513 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1514 AV_SAMPLE_FMT_NONE },
1515 .profiles = NULL_IF_CONFIG_SMALL(profiles),
1516 .priv_class = &dca_decoder_class,
1517 };