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
8 * This file is part of Libav.
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.
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.
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
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"
45 #include "fmtconvert.h"
50 #include "synth_filter.h"
70 /* -1 are reserved or unknown */
71 static const int dca_ext_audio_descr_mask
[] = {
75 DCA_EXT_XCH
| DCA_EXT_X96
,
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.
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
89 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
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
102 AV_CH_LAYOUT_STEREO
| AV_CH_FRONT_CENTER
| AV_CH_SIDE_LEFT
|
103 AV_CH_SIDE_RIGHT
, ///< 5, C + L + R + SL + SR
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
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
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
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
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
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
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
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 },
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 },
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 },
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 },
208 #define DCA_DOLBY 101 /* FIXME */
210 #define DCA_CHANNEL_BITS 6
211 #define DCA_CHANNEL_MASK 0x3F
215 #define HEADER_SIZE 14
217 #define DCA_NSYNCAUX 0x9A1105A0
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
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
232 static av_always_inline
int get_bitalloc(GetBitContext
*gb
, BitAlloc
*ba
,
235 return get_vlc2(gb
, ba
->vlc
[idx
].table
, ba
->vlc
[idx
].bits
, ba
->wrap
) +
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,
248 static av_cold
void dca_init_vlcs(void)
250 static int vlcs_initialized
= 0;
252 static VLC_TYPE dca_table
[23622][2];
254 if (vlcs_initialized
)
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
);
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
);
275 dca_tmode
.offset
= 0;
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,
282 tmode_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
285 for (i
= 0; i
< 10; i
++)
286 for (j
= 0; j
< 7; j
++) {
287 if (!bitalloc_codes
[i
][j
])
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
];
294 init_vlc(&dca_smpl_bitalloc
[i
+ 1].vlc
[j
], bitalloc_maxbits
[i
][j
],
296 bitalloc_bits
[i
][j
], 1, 1,
297 bitalloc_codes
[i
][j
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
300 vlcs_initialized
= 1;
303 static inline void get_array(GetBitContext
*gb
, int *dst
, int len
, int bits
)
306 *dst
++ = get_bits(gb
, bits
);
309 static int dca_parse_audio_coding_header(DCAContext
*s
, int base_channel
)
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 };
316 s
->total_channels
= get_bits(&s
->gb
, 3) + 1 + base_channel
;
317 s
->prim_channels
= s
->total_channels
;
319 if (s
->prim_channels
> DCA_PRIM_CHANNELS_MAX
)
320 s
->prim_channels
= DCA_PRIM_CHANNELS_MAX
;
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
;
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
;
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);
337 /* Get codebooks quantization indexes */
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
]);
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;
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)];
354 if (s
->crc_present
) {
355 /* Audio header CRC check */
356 get_bits(&s
->gb
, 16);
359 s
->current_subframe
= 0;
360 s
->current_subsubframe
= 0;
365 static int dca_parse_frame_header(DCAContext
*s
)
367 init_get_bits(&s
->gb
, s
->dca_buffer
, s
->dca_buffer_size
* 8);
370 skip_bits_long(&s
->gb
, 32);
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)];
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
];
387 return AVERROR_INVALIDDATA
;
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);
401 av_log(s
->avctx
, AV_LOG_ERROR
, "Invalid LFE value: %d\n", s
->lfe
);
402 return AVERROR_INVALIDDATA
;
405 /* TODO: check CRC */
407 s
->header_crc
= get_bits(&s
->gb
, 16);
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);
417 /* FIXME: channels mixing levels */
418 s
->output
= s
->amode
;
420 s
->output
|= DCA_LFE
;
422 /* Primary audio coding header */
423 s
->subframes
= get_bits(&s
->gb
, 4) + 1;
425 return dca_parse_audio_coding_header(s
, 0);
428 static inline int get_scale(GetBitContext
*gb
, int level
, int value
, int log2range
)
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
);
439 value
= get_bits(gb
, level
+ 1);
445 static int dca_subframe_header(DCAContext
*s
, int base_channel
, int block_index
)
447 /* Primary audio coding side information */
450 if (get_bits_left(&s
->gb
) < 0)
451 return AVERROR_INVALIDDATA
;
454 s
->subsubframes
[s
->current_subframe
] = get_bits(&s
->gb
, 2) + 1;
455 s
->partial_samples
[s
->current_subframe
] = get_bits(&s
->gb
, 3);
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);
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);
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
;
486 get_bitalloc(&s
->gb
, &dca_bitalloc_index
, s
->bitalloc_huffman
[j
]);
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
;
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
]);
509 if (get_bits_left(&s
->gb
) < 0)
510 return AVERROR_INVALIDDATA
;
512 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
513 const uint32_t *scale_table
;
514 int scale_sum
, log_size
;
516 memset(s
->scale_factor
[j
], 0,
517 s
->subband_activity
[j
] * sizeof(s
->scale_factor
[0][0][0]) * 2);
519 if (s
->scalefactor_huffman
[j
] == 6) {
520 scale_table
= scale_factor_quant7
;
523 scale_table
= scale_factor_quant6
;
527 /* When huffman coded, only the difference is encoded */
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
];
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
];
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);
551 if (get_bits_left(&s
->gb
) < 0)
552 return AVERROR_INVALIDDATA
;
554 /* Scale factors for joint subband coding */
555 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
558 /* Transmitted only if joint subband coding enabled */
559 if (s
->joint_intensity
[j
] > 0) {
561 source_channel
= s
->joint_intensity
[j
] - 1;
563 /* When huffman coded, only the difference is encoded
564 * (is this valid as well for joint scales ???) */
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]; */
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;
579 /* Dynamic range coefficient */
580 if (!base_channel
&& s
->dynrange
)
581 s
->dynrange_coef
= get_bits(&s
->gb
, 8);
583 /* Side information CRC check word */
584 if (s
->crc_present
) {
585 get_bits(&s
->gb
, 16);
589 * Primary audio data arrays
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);
598 /* Low frequency effect data */
599 if (!base_channel
&& s
->lfe
) {
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
]);
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);
610 /* Scale factor index */
611 skip_bits(&s
->gb
, 1);
612 s
->lfe_scale_factor
= scale_factor_quant7
[get_bits(&s
->gb
, 7)];
614 /* Quantization step size * scale factor */
615 lfe_scale
= 0.035 * s
->lfe_scale_factor
;
617 for (j
= lfe_samples
; j
< lfe_end_sample
; j
++)
618 s
->lfe_data
[j
] *= lfe_scale
;
624 static void qmf_32_subbands(DCAContext
*s
, int chans
,
625 float samples_in
[32][8], float *samples_out
,
628 const float *prCoeff
;
630 int sb_act
= s
->subband_activity
[chans
];
632 scale
*= sqrt(1 / 8.0);
635 if (!s
->multirate_inter
) /* Non-perfect reconstruction */
636 prCoeff
= fir_32bands_nonperfect
;
637 else /* Perfect reconstruction */
638 prCoeff
= fir_32bands_perfect
;
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
);
647 static void lfe_interpolation_fir(DCAContext
*s
, int decimation_select
,
648 int num_deci_sample
, float *samples_in
,
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.
656 * samples_out: An array holding interpolated samples
660 const float *prCoeff
;
663 /* Select decimation filter */
664 if (decimation_select
== 1) {
666 prCoeff
= lfe_fir_128
;
669 prCoeff
= lfe_fir_64
;
672 for (deciindex
= 0; deciindex
< num_deci_sample
; deciindex
++) {
673 s
->dcadsp
.lfe_fir
[idx
](samples_out
, samples_in
, prCoeff
);
675 samples_out
+= 2 * 32 * (1 + idx
);
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];
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];
688 #define MIX_FRONT3(samples, coef) \
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];
695 #define DOWNMIX_TO_STEREO(op1, op2) \
696 for (i = 0; i < 256; i++) { \
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
)
705 int c
, l
, r
, sl
, sr
, s
;
712 av_log(NULL
, 0, "Not implemented!\n");
716 case DCA_STEREO_TOTAL
:
717 case DCA_STEREO_SUMDIFF
:
720 c
= channel_mapping
[0];
721 l
= channel_mapping
[1];
722 r
= channel_mapping
[2];
723 DOWNMIX_TO_STEREO(MIX_FRONT3(samples
, coef
), );
726 s
= channel_mapping
[2];
727 DOWNMIX_TO_STEREO(MIX_REAR1(samples
, s
, 2, coef
), );
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
));
738 sl
= channel_mapping
[2];
739 sr
= channel_mapping
[3];
740 DOWNMIX_TO_STEREO(MIX_REAR2(samples
, sl
, sr
, 2, coef
), );
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
));
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];
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
)
768 int offset
= (levels
- 1) >> 1;
770 for (i
= 0; i
< 4; i
++) {
771 int div
= FASTDIV(code
, levels
);
772 values
[i
] = code
- offset
- div
* levels
;
779 static int decode_blockcodes(int code1
, int code2
, int levels
, int32_t *values
)
781 return decode_blockcode(code1
, levels
, values
) |
782 decode_blockcode(code2
, levels
, values
+ 4);
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 };
789 static int dca_subsubframe(DCAContext
*s
, int base_channel
, int block_index
)
792 int subsubframe
= s
->current_subsubframe
;
794 const float *quant_step_table
;
797 float (*subband_samples
)[DCA_SUBBANDS
][8] = s
->subband_samples
[block_index
];
798 LOCAL_ALIGNED_16(int32_t, block
, [8 * DCA_SUBBANDS
]);
804 /* Select quantization step size table */
805 if (s
->bit_rate_index
== 0x1f)
806 quant_step_table
= lossless_quant_d
;
808 quant_step_table
= lossy_quant_d
;
810 for (k
= base_channel
; k
< s
->prim_channels
; k
++) {
811 float rscale
[DCA_SUBBANDS
];
813 if (get_bits_left(&s
->gb
) < 0)
814 return AVERROR_INVALIDDATA
;
816 for (l
= 0; l
< s
->vq_start_subband
[k
]; l
++) {
819 /* Select the mid-tread linear quantizer */
820 int abits
= s
->bitalloc
[k
][l
];
822 float quant_step_size
= quant_step_table
[abits
];
825 * Determine quantization index code book and its type
828 /* Select quantization index code book */
829 int sel
= s
->quant_index_huffman
[k
][abits
];
832 * Extract bits from the bit stream
836 memset(block
+ 8 * l
, 0, 8 * sizeof(block
[0]));
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
];
843 if (abits
>= 11 || !dca_smpl_bitalloc
[abits
].vlc
[sel
].table
) {
846 int block_code1
, block_code2
, size
, levels
, err
;
848 size
= abits_sizes
[abits
- 1];
849 levels
= abits_levels
[abits
- 1];
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
);
856 av_log(s
->avctx
, AV_LOG_ERROR
,
857 "ERROR: block code look-up failed\n");
858 return AVERROR_INVALIDDATA
;
862 for (m
= 0; m
< 8; m
++)
863 block
[8 * l
+ m
] = get_sbits(&s
->gb
, abits
- 3);
867 for (m
= 0; m
< 8; m
++)
868 block
[8 * l
+ m
] = get_bitalloc(&s
->gb
,
869 &dca_smpl_bitalloc
[abits
], sel
);
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
]);
877 for (l
= 0; l
< s
->vq_start_subband
[k
]; l
++) {
880 * Inverse ADPCM if in prediction mode
882 if (s
->prediction_mode
[k
][l
]) {
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]) *
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
++)
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;
910 * Decode VQ encoded high frequencies
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;
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
]);
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
;
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]);
941 static int dca_filter_channels(DCAContext
*s
, int block_index
)
943 float (*subband_samples
)[DCA_SUBBANDS
][8] = s
->subband_samples
[block_index
];
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);
954 /* Generate LFE samples for this subsubframe FIXME!!! */
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 */
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
);
972 static int dca_subframe_footer(DCAContext
*s
, int base_channel
)
974 int in
, out
, aux_data_count
, aux_data_end
, reserved
;
978 * Unpack optional information
981 /* presumably optional information only appears in the core? */
984 skip_bits_long(&s
->gb
, 32);
987 aux_data_count
= get_bits(&s
->gb
, 6);
990 skip_bits_long(&s
->gb
, (-get_bits_count(&s
->gb
)) & 31);
992 aux_data_end
= 8 * aux_data_count
+ get_bits_count(&s
->gb
);
994 if ((nsyncaux
= get_bits_long(&s
->gb
, 32)) != DCA_NSYNCAUX
) {
995 av_log(s
->avctx
, AV_LOG_ERROR
, "nSYNCAUX mismatch %#"PRIx32
"\n",
997 return AVERROR_INVALIDDATA
;
1000 if (get_bits1(&s
->gb
)) { // bAUXTimeStampFlag
1001 avpriv_request_sample(s
->avctx
,
1002 "Auxiliary Decode Time Stamp Flag");
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);
1009 if ((s
->core_downmix
= get_bits1(&s
->gb
))) {
1010 int am
= get_bits(&s
->gb
, 3);
1013 s
->core_downmix_amode
= DCA_MONO
;
1016 s
->core_downmix_amode
= DCA_STEREO
;
1019 s
->core_downmix_amode
= DCA_STEREO_TOTAL
;
1022 s
->core_downmix_amode
= DCA_3F
;
1025 s
->core_downmix_amode
= DCA_2F1R
;
1028 s
->core_downmix_amode
= DCA_2F2R
;
1031 s
->core_downmix_amode
= DCA_3F1R
;
1034 av_log(s
->avctx
, AV_LOG_ERROR
,
1035 "Invalid mode %d for embedded downmix coefficients\n",
1037 return AVERROR_INVALIDDATA
;
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",
1046 return AVERROR_INVALIDDATA
;
1048 s
->core_downmix_codes
[in
][out
] = tmp
;
1053 align_get_bits(&s
->gb
); // byte align
1054 skip_bits(&s
->gb
, 16); // nAUXCRC16
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
);
1068 if (s
->crc_present
&& s
->dynrange
)
1069 get_bits(&s
->gb
, 16);
1076 * Decode a dca frame block
1078 * @param s pointer to the DCAContext
1081 static int dca_decode_block(DCAContext
*s
, int base_channel
, int block_index
)
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
;
1092 if (!s
->current_subsubframe
) {
1093 /* Read subframe header */
1094 if ((ret
= dca_subframe_header(s
, base_channel
, block_index
)))
1098 /* Read subsubframe */
1099 if ((ret
= dca_subsubframe(s
, base_channel
, block_index
)))
1103 s
->current_subsubframe
++;
1104 if (s
->current_subsubframe
>= s
->subsubframes
[s
->current_subframe
]) {
1105 s
->current_subsubframe
= 0;
1106 s
->current_subframe
++;
1108 if (s
->current_subframe
>= s
->subframes
) {
1109 /* Read subframe footer */
1110 if ((ret
= dca_subframe_footer(s
, base_channel
)))
1117 static float dca_dmix_code(unsigned code
)
1119 int sign
= (code
>> 8) - 1;
1121 return ((dca_dmixtable
[code
] ^ sign
) - sign
) * (1.0 / (1U << 15));
1125 * Main frame decoding function
1126 * FIXME add arguments
1128 static int dca_decode_frame(AVCodecContext
*avctx
, void *data
,
1129 int *got_frame_ptr
, AVPacket
*avpkt
)
1131 AVFrame
*frame
= data
;
1132 const uint8_t *buf
= avpkt
->data
;
1133 int buf_size
= avpkt
->size
;
1136 int num_core_channels
= 0;
1138 float **samples_flt
;
1139 DCAContext
*s
= avctx
->priv_data
;
1140 int channels
, full_channels
;
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
;
1152 if ((ret
= dca_parse_frame_header(s
)) < 0) {
1153 // seems like the frame is corrupt, try with the next one
1156 // set AVCodec values with parsed data
1157 avctx
->sample_rate
= s
->sample_rate
;
1158 avctx
->bit_rate
= s
->bit_rate
;
1160 s
->profile
= FF_PROFILE_DTS
;
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");
1169 /* record number of core channels incase less than max channels are requested */
1170 num_core_channels
= s
->prim_channels
;
1173 s
->core_ext_mask
= dca_ext_audio_descr_mask
[s
->ext_descr
];
1175 s
->core_ext_mask
= 0;
1177 core_ss_end
= FFMIN(s
->frame_size
, s
->dca_buffer_size
) * 8;
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);
1186 /* extensions start at 32-bit boundaries into bitstream */
1187 skip_bits_long(&s
->gb
, (-get_bits_count(&s
->gb
)) & 31);
1189 while (core_ss_end
- get_bits_count(&s
->gb
) >= 32) {
1190 uint32_t bits
= get_bits_long(&s
->gb
, 32);
1194 int ext_amode
, xch_fsize
;
1196 s
->xch_base_channel
= s
->prim_channels
;
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))
1204 /* skip length-to-end-of-frame field for the moment */
1205 skip_bits(&s
->gb
, 10);
1207 s
->core_ext_mask
|= DCA_EXT_XCH
;
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",
1218 /* much like core primary audio coding header */
1219 dca_parse_audio_coding_header(s
, s
->xch_base_channel
);
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");
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
;
1238 int fsize96
= show_bits(&s
->gb
, 12) + 1;
1239 if (s
->frame_size
!= (get_bits_count(&s
->gb
) >> 3) - 4 + fsize96
)
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));
1248 s
->core_ext_mask
|= DCA_EXT_X96
;
1253 skip_bits_long(&s
->gb
, (-get_bits_count(&s
->gb
)) & 31);
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
));
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
;
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
);
1270 avctx
->profile
= s
->profile
;
1272 full_channels
= channels
= s
->prim_channels
+ !!s
->lfe
;
1274 if (s
->amode
< 16) {
1275 avctx
->channel_layout
= dca_core_channel_layout
[s
->amode
];
1277 if (s
->prim_channels
+ !!s
->lfe
> 2 &&
1278 avctx
->request_channel_layout
== AV_CH_LAYOUT_STEREO
) {
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.
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
1294 if (s
->xch_present
&& !s
->xch_disable
) {
1296 avctx
->channel_layout
|= AV_CH_BACK_CENTER
;
1298 avctx
->channel_layout
|= AV_CH_LOW_FREQUENCY
;
1299 s
->channel_order_tab
= dca_channel_reorder_lfe_xch
[s
->amode
];
1301 s
->channel_order_tab
= dca_channel_reorder_nolfe_xch
[s
->amode
];
1304 channels
= num_core_channels
+ !!s
->lfe
;
1305 s
->xch_present
= 0; /* disable further xch processing */
1307 avctx
->channel_layout
|= AV_CH_LOW_FREQUENCY
;
1308 s
->channel_order_tab
= dca_channel_reorder_lfe
[s
->amode
];
1310 s
->channel_order_tab
= dca_channel_reorder_nolfe
[s
->amode
];
1313 if (channels
> !!s
->lfe
&&
1314 s
->channel_order_tab
[channels
- 1 - !!s
->lfe
] < 0)
1315 return AVERROR_INVALIDDATA
;
1317 if (num_core_channels
+ !!s
->lfe
> 2 &&
1318 avctx
->request_channel_layout
== AV_CH_LAYOUT_STEREO
) {
1320 s
->output
= s
->prim_channels
== 2 ? s
->amode
: DCA_STEREO
;
1321 avctx
->channel_layout
= AV_CH_LAYOUT_STEREO
;
1323 /* Stereo downmix coefficients
1325 * The decoder can only downmix to 2-channel, so we need to ensure
1326 * embedded downmix coefficients are actually targeting 2-channel.
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]);
1335 s
->output
= s
->core_downmix_amode
;
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
;
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
;
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];
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]);
1361 av_dlog(s
->avctx
, "\n");
1364 av_log(avctx
, AV_LOG_ERROR
, "Non standard configuration %d !\n", s
->amode
);
1365 return AVERROR_INVALIDDATA
;
1367 avctx
->channels
= channels
;
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");
1375 samples_flt
= (float **) frame
->extended_data
;
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
,
1381 avctx
->sample_fmt
, 0);
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
);
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);
1398 /* filter to get final output */
1399 for (i
= 0; i
< (s
->sample_blocks
/ 8); i
++) {
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;
1407 dca_filter_channels(s
, i
);
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);
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
];
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
);
1440 * DCA initialization
1442 * @param avctx pointer to the AVCodecContext
1445 static av_cold
int dca_decode_init(AVCodecContext
*avctx
)
1447 DCAContext
*s
= avctx
->priv_data
;
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
);
1458 avctx
->sample_fmt
= AV_SAMPLE_FMT_FLTP
;
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
1467 if (avctx
->channels
> 2 &&
1468 avctx
->request_channel_layout
== AV_CH_LAYOUT_STEREO
)
1469 avctx
->channels
= 2;
1474 static av_cold
int dca_decode_end(AVCodecContext
*avctx
)
1476 DCAContext
*s
= avctx
->priv_data
;
1477 ff_mdct_end(&s
->imdct
);
1478 av_freep(&s
->extra_channels_buffer
);
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
},
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
},
1496 static const AVClass dca_decoder_class
= {
1497 .class_name
= "DCA decoder",
1498 .item_name
= av_default_item_name
,
1500 .version
= LIBAVUTIL_VERSION_INT
,
1503 AVCodec ff_dca_decoder
= {
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
,