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 FFmpeg.
10 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
44 #include "fmtconvert.h"
48 #include "synth_filter.h"
56 #define DCA_PRIM_CHANNELS_MAX (7)
57 #define DCA_ABITS_MAX (32) /* Should be 28 */
58 #define DCA_SUBSUBFRAMES_MAX (4)
59 #define DCA_SUBFRAMES_MAX (16)
60 #define DCA_BLOCKS_MAX (16)
61 #define DCA_LFE_MAX (3)
62 #define DCA_CHSETS_MAX (4)
63 #define DCA_CHSET_CHANS_MAX (8)
79 /* these are unconfirmed but should be mostly correct */
80 enum DCAExSSSpeakerMask
{
81 DCA_EXSS_FRONT_CENTER
= 0x0001,
82 DCA_EXSS_FRONT_LEFT_RIGHT
= 0x0002,
83 DCA_EXSS_SIDE_REAR_LEFT_RIGHT
= 0x0004,
84 DCA_EXSS_LFE
= 0x0008,
85 DCA_EXSS_REAR_CENTER
= 0x0010,
86 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT
= 0x0020,
87 DCA_EXSS_REAR_LEFT_RIGHT
= 0x0040,
88 DCA_EXSS_FRONT_HIGH_CENTER
= 0x0080,
89 DCA_EXSS_OVERHEAD
= 0x0100,
90 DCA_EXSS_CENTER_LEFT_RIGHT
= 0x0200,
91 DCA_EXSS_WIDE_LEFT_RIGHT
= 0x0400,
92 DCA_EXSS_SIDE_LEFT_RIGHT
= 0x0800,
93 DCA_EXSS_LFE2
= 0x1000,
94 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT
= 0x2000,
95 DCA_EXSS_REAR_HIGH_CENTER
= 0x4000,
96 DCA_EXSS_REAR_HIGH_LEFT_RIGHT
= 0x8000,
99 enum DCAXxchSpeakerMask
{
100 DCA_XXCH_FRONT_CENTER
= 0x0000001,
101 DCA_XXCH_FRONT_LEFT
= 0x0000002,
102 DCA_XXCH_FRONT_RIGHT
= 0x0000004,
103 DCA_XXCH_SIDE_REAR_LEFT
= 0x0000008,
104 DCA_XXCH_SIDE_REAR_RIGHT
= 0x0000010,
105 DCA_XXCH_LFE1
= 0x0000020,
106 DCA_XXCH_REAR_CENTER
= 0x0000040,
107 DCA_XXCH_SURROUND_REAR_LEFT
= 0x0000080,
108 DCA_XXCH_SURROUND_REAR_RIGHT
= 0x0000100,
109 DCA_XXCH_SIDE_SURROUND_LEFT
= 0x0000200,
110 DCA_XXCH_SIDE_SURROUND_RIGHT
= 0x0000400,
111 DCA_XXCH_FRONT_CENTER_LEFT
= 0x0000800,
112 DCA_XXCH_FRONT_CENTER_RIGHT
= 0x0001000,
113 DCA_XXCH_FRONT_HIGH_LEFT
= 0x0002000,
114 DCA_XXCH_FRONT_HIGH_CENTER
= 0x0004000,
115 DCA_XXCH_FRONT_HIGH_RIGHT
= 0x0008000,
116 DCA_XXCH_LFE2
= 0x0010000,
117 DCA_XXCH_SIDE_FRONT_LEFT
= 0x0020000,
118 DCA_XXCH_SIDE_FRONT_RIGHT
= 0x0040000,
119 DCA_XXCH_OVERHEAD
= 0x0080000,
120 DCA_XXCH_SIDE_HIGH_LEFT
= 0x0100000,
121 DCA_XXCH_SIDE_HIGH_RIGHT
= 0x0200000,
122 DCA_XXCH_REAR_HIGH_CENTER
= 0x0400000,
123 DCA_XXCH_REAR_HIGH_LEFT
= 0x0800000,
124 DCA_XXCH_REAR_HIGH_RIGHT
= 0x1000000,
125 DCA_XXCH_REAR_LOW_CENTER
= 0x2000000,
126 DCA_XXCH_REAR_LOW_LEFT
= 0x4000000,
127 DCA_XXCH_REAR_LOW_RIGHT
= 0x8000000,
130 static const uint32_t map_xxch_to_native
[28] = {
140 AV_CH_SIDE_LEFT
, /* side surround left -- dup sur side L */
141 AV_CH_SIDE_RIGHT
, /* side surround right -- dup sur side R */
142 AV_CH_FRONT_LEFT_OF_CENTER
,
143 AV_CH_FRONT_RIGHT_OF_CENTER
,
144 AV_CH_TOP_FRONT_LEFT
,
145 AV_CH_TOP_FRONT_CENTER
,
146 AV_CH_TOP_FRONT_RIGHT
,
147 AV_CH_LOW_FREQUENCY
, /* lfe2 -- duplicate lfe1 position */
148 AV_CH_FRONT_LEFT_OF_CENTER
, /* side front left -- dup front cntr L */
149 AV_CH_FRONT_RIGHT_OF_CENTER
,/* side front right -- dup front cntr R */
150 AV_CH_TOP_CENTER
, /* overhead */
151 AV_CH_TOP_FRONT_LEFT
, /* side high left -- dup */
152 AV_CH_TOP_FRONT_RIGHT
, /* side high right -- dup */
153 AV_CH_TOP_BACK_CENTER
,
155 AV_CH_TOP_BACK_RIGHT
,
156 AV_CH_BACK_CENTER
, /* rear low center -- dup */
157 AV_CH_BACK_LEFT
, /* rear low left -- dup */
158 AV_CH_BACK_RIGHT
/* read low right -- dup */
161 enum DCAExtensionMask
{
162 DCA_EXT_CORE
= 0x001, ///< core in core substream
163 DCA_EXT_XXCH
= 0x002, ///< XXCh channels extension in core substream
164 DCA_EXT_X96
= 0x004, ///< 96/24 extension in core substream
165 DCA_EXT_XCH
= 0x008, ///< XCh channel extension in core substream
166 DCA_EXT_EXSS_CORE
= 0x010, ///< core in ExSS (extension substream)
167 DCA_EXT_EXSS_XBR
= 0x020, ///< extended bitrate extension in ExSS
168 DCA_EXT_EXSS_XXCH
= 0x040, ///< XXCh channels extension in ExSS
169 DCA_EXT_EXSS_X96
= 0x080, ///< 96/24 extension in ExSS
170 DCA_EXT_EXSS_LBR
= 0x100, ///< low bitrate component in ExSS
171 DCA_EXT_EXSS_XLL
= 0x200, ///< lossless extension in ExSS
174 /* -1 are reserved or unknown */
175 static const int dca_ext_audio_descr_mask
[] = {
179 DCA_EXT_XCH
| DCA_EXT_X96
,
186 /* extensions that reside in core substream */
187 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
189 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
190 * Some compromises have been made for special configurations. Most configurations
191 * are never used so complete accuracy is not needed.
193 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
194 * S -> side, when both rear and back are configured move one of them to the side channel
196 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
198 static const uint64_t dca_core_channel_layout
[] = {
199 AV_CH_FRONT_CENTER
, ///< 1, A
200 AV_CH_LAYOUT_STEREO
, ///< 2, A + B (dual mono)
201 AV_CH_LAYOUT_STEREO
, ///< 2, L + R (stereo)
202 AV_CH_LAYOUT_STEREO
, ///< 2, (L + R) + (L - R) (sum-difference)
203 AV_CH_LAYOUT_STEREO
, ///< 2, LT + RT (left and right total)
204 AV_CH_LAYOUT_STEREO
| AV_CH_FRONT_CENTER
, ///< 3, C + L + R
205 AV_CH_LAYOUT_STEREO
| AV_CH_BACK_CENTER
, ///< 3, L + R + S
206 AV_CH_LAYOUT_STEREO
| AV_CH_FRONT_CENTER
| AV_CH_BACK_CENTER
, ///< 4, C + L + R + S
207 AV_CH_LAYOUT_STEREO
| AV_CH_SIDE_LEFT
| AV_CH_SIDE_RIGHT
, ///< 4, L + R + SL + SR
209 AV_CH_LAYOUT_STEREO
| AV_CH_FRONT_CENTER
| AV_CH_SIDE_LEFT
|
210 AV_CH_SIDE_RIGHT
, ///< 5, C + L + R + SL + SR
212 AV_CH_LAYOUT_STEREO
| AV_CH_SIDE_LEFT
| AV_CH_SIDE_RIGHT
|
213 AV_CH_FRONT_LEFT_OF_CENTER
| AV_CH_FRONT_RIGHT_OF_CENTER
, ///< 6, CL + CR + L + R + SL + SR
215 AV_CH_LAYOUT_STEREO
| AV_CH_BACK_LEFT
| AV_CH_BACK_RIGHT
|
216 AV_CH_FRONT_CENTER
| AV_CH_BACK_CENTER
, ///< 6, C + L + R + LR + RR + OV
218 AV_CH_FRONT_CENTER
| AV_CH_FRONT_RIGHT_OF_CENTER
|
219 AV_CH_FRONT_LEFT_OF_CENTER
| AV_CH_BACK_CENTER
|
220 AV_CH_BACK_LEFT
| AV_CH_BACK_RIGHT
, ///< 6, CF + CR + LF + RF + LR + RR
222 AV_CH_FRONT_LEFT_OF_CENTER
| AV_CH_FRONT_CENTER
|
223 AV_CH_FRONT_RIGHT_OF_CENTER
| AV_CH_LAYOUT_STEREO
|
224 AV_CH_SIDE_LEFT
| AV_CH_SIDE_RIGHT
, ///< 7, CL + C + CR + L + R + SL + SR
226 AV_CH_FRONT_LEFT_OF_CENTER
| AV_CH_FRONT_RIGHT_OF_CENTER
|
227 AV_CH_LAYOUT_STEREO
| AV_CH_SIDE_LEFT
| AV_CH_SIDE_RIGHT
|
228 AV_CH_BACK_LEFT
| AV_CH_BACK_RIGHT
, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
230 AV_CH_FRONT_LEFT_OF_CENTER
| AV_CH_FRONT_CENTER
|
231 AV_CH_FRONT_RIGHT_OF_CENTER
| AV_CH_LAYOUT_STEREO
|
232 AV_CH_SIDE_LEFT
| AV_CH_BACK_CENTER
| AV_CH_SIDE_RIGHT
, ///< 8, CL + C + CR + L + R + SL + S + SR
235 static const int8_t dca_lfe_index
[] = {
236 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
239 static const int8_t dca_channel_reorder_lfe
[][9] = {
240 { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
241 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
242 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
243 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
244 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
245 { 2, 0, 1, -1, -1, -1, -1, -1, -1 },
246 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
247 { 2, 0, 1, 4, -1, -1, -1, -1, -1 },
248 { 0, 1, 3, 4, -1, -1, -1, -1, -1 },
249 { 2, 0, 1, 4, 5, -1, -1, -1, -1 },
250 { 3, 4, 0, 1, 5, 6, -1, -1, -1 },
251 { 2, 0, 1, 4, 5, 6, -1, -1, -1 },
252 { 0, 6, 4, 5, 2, 3, -1, -1, -1 },
253 { 4, 2, 5, 0, 1, 6, 7, -1, -1 },
254 { 5, 6, 0, 1, 7, 3, 8, 4, -1 },
255 { 4, 2, 5, 0, 1, 6, 8, 7, -1 },
258 static const int8_t dca_channel_reorder_lfe_xch
[][9] = {
259 { 0, 2, -1, -1, -1, -1, -1, -1, -1 },
260 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
261 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
262 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
263 { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
264 { 2, 0, 1, 4, -1, -1, -1, -1, -1 },
265 { 0, 1, 3, 4, -1, -1, -1, -1, -1 },
266 { 2, 0, 1, 4, 5, -1, -1, -1, -1 },
267 { 0, 1, 4, 5, 3, -1, -1, -1, -1 },
268 { 2, 0, 1, 5, 6, 4, -1, -1, -1 },
269 { 3, 4, 0, 1, 6, 7, 5, -1, -1 },
270 { 2, 0, 1, 4, 5, 6, 7, -1, -1 },
271 { 0, 6, 4, 5, 2, 3, 7, -1, -1 },
272 { 4, 2, 5, 0, 1, 7, 8, 6, -1 },
273 { 5, 6, 0, 1, 8, 3, 9, 4, 7 },
274 { 4, 2, 5, 0, 1, 6, 9, 8, 7 },
277 static const int8_t dca_channel_reorder_nolfe
[][9] = {
278 { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
279 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
280 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
281 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
282 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
283 { 2, 0, 1, -1, -1, -1, -1, -1, -1 },
284 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
285 { 2, 0, 1, 3, -1, -1, -1, -1, -1 },
286 { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
287 { 2, 0, 1, 3, 4, -1, -1, -1, -1 },
288 { 2, 3, 0, 1, 4, 5, -1, -1, -1 },
289 { 2, 0, 1, 3, 4, 5, -1, -1, -1 },
290 { 0, 5, 3, 4, 1, 2, -1, -1, -1 },
291 { 3, 2, 4, 0, 1, 5, 6, -1, -1 },
292 { 4, 5, 0, 1, 6, 2, 7, 3, -1 },
293 { 3, 2, 4, 0, 1, 5, 7, 6, -1 },
296 static const int8_t dca_channel_reorder_nolfe_xch
[][9] = {
297 { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
298 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
299 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
300 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
301 { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
302 { 2, 0, 1, 3, -1, -1, -1, -1, -1 },
303 { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
304 { 2, 0, 1, 3, 4, -1, -1, -1, -1 },
305 { 0, 1, 3, 4, 2, -1, -1, -1, -1 },
306 { 2, 0, 1, 4, 5, 3, -1, -1, -1 },
307 { 2, 3, 0, 1, 5, 6, 4, -1, -1 },
308 { 2, 0, 1, 3, 4, 5, 6, -1, -1 },
309 { 0, 5, 3, 4, 1, 2, 6, -1, -1 },
310 { 3, 2, 4, 0, 1, 6, 7, 5, -1 },
311 { 4, 5, 0, 1, 7, 2, 8, 3, 6 },
312 { 3, 2, 4, 0, 1, 5, 8, 7, 6 },
315 #define DCA_DOLBY 101 /* FIXME */
317 #define DCA_CHANNEL_BITS 6
318 #define DCA_CHANNEL_MASK 0x3F
322 #define HEADER_SIZE 14
324 #define DCA_MAX_FRAME_SIZE 16384
325 #define DCA_MAX_EXSS_HEADER_SIZE 4096
327 #define DCA_BUFFER_PADDING_SIZE 1024
329 #define DCA_NSYNCAUX 0x9A1105A0
331 /** Bit allocation */
333 int offset
; ///< code values offset
334 int maxbits
[8]; ///< max bits in VLC
335 int wrap
; ///< wrap for get_vlc2()
336 VLC vlc
[8]; ///< actual codes
339 static BitAlloc dca_bitalloc_index
; ///< indexes for samples VLC select
340 static BitAlloc dca_tmode
; ///< transition mode VLCs
341 static BitAlloc dca_scalefactor
; ///< scalefactor VLCs
342 static BitAlloc dca_smpl_bitalloc
[11]; ///< samples VLCs
344 static av_always_inline
int get_bitalloc(GetBitContext
*gb
, BitAlloc
*ba
,
347 return get_vlc2(gb
, ba
->vlc
[idx
].table
, ba
->vlc
[idx
].bits
, ba
->wrap
) +
352 const AVClass
*class; ///< class for AVOptions
353 AVCodecContext
*avctx
;
355 int frame_type
; ///< type of the current frame
356 int samples_deficit
; ///< deficit sample count
357 int crc_present
; ///< crc is present in the bitstream
358 int sample_blocks
; ///< number of PCM sample blocks
359 int frame_size
; ///< primary frame byte size
360 int amode
; ///< audio channels arrangement
361 int sample_rate
; ///< audio sampling rate
362 int bit_rate
; ///< transmission bit rate
363 int bit_rate_index
; ///< transmission bit rate index
365 int dynrange
; ///< embedded dynamic range flag
366 int timestamp
; ///< embedded time stamp flag
367 int aux_data
; ///< auxiliary data flag
368 int hdcd
; ///< source material is mastered in HDCD
369 int ext_descr
; ///< extension audio descriptor flag
370 int ext_coding
; ///< extended coding flag
371 int aspf
; ///< audio sync word insertion flag
372 int lfe
; ///< low frequency effects flag
373 int predictor_history
; ///< predictor history flag
374 int header_crc
; ///< header crc check bytes
375 int multirate_inter
; ///< multirate interpolator switch
376 int version
; ///< encoder software revision
377 int copy_history
; ///< copy history
378 int source_pcm_res
; ///< source pcm resolution
379 int front_sum
; ///< front sum/difference flag
380 int surround_sum
; ///< surround sum/difference flag
381 int dialog_norm
; ///< dialog normalisation parameter
383 /* Primary audio coding header */
384 int subframes
; ///< number of subframes
385 int total_channels
; ///< number of channels including extensions
386 int prim_channels
; ///< number of primary audio channels
387 int subband_activity
[DCA_PRIM_CHANNELS_MAX
]; ///< subband activity count
388 int vq_start_subband
[DCA_PRIM_CHANNELS_MAX
]; ///< high frequency vq start subband
389 int joint_intensity
[DCA_PRIM_CHANNELS_MAX
]; ///< joint intensity coding index
390 int transient_huffman
[DCA_PRIM_CHANNELS_MAX
]; ///< transient mode code book
391 int scalefactor_huffman
[DCA_PRIM_CHANNELS_MAX
]; ///< scale factor code book
392 int bitalloc_huffman
[DCA_PRIM_CHANNELS_MAX
]; ///< bit allocation quantizer select
393 int quant_index_huffman
[DCA_PRIM_CHANNELS_MAX
][DCA_ABITS_MAX
]; ///< quantization index codebook select
394 float scalefactor_adj
[DCA_PRIM_CHANNELS_MAX
][DCA_ABITS_MAX
]; ///< scale factor adjustment
396 /* Primary audio coding side information */
397 int subsubframes
[DCA_SUBFRAMES_MAX
]; ///< number of subsubframes
398 int partial_samples
[DCA_SUBFRAMES_MAX
]; ///< partial subsubframe samples count
399 int prediction_mode
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< prediction mode (ADPCM used or not)
400 int prediction_vq
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< prediction VQ coefs
401 int bitalloc
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< bit allocation index
402 int transition_mode
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< transition mode (transients)
403 int32_t scale_factor
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
][2];///< scale factors (2 if transient)
404 int joint_huff
[DCA_PRIM_CHANNELS_MAX
]; ///< joint subband scale factors codebook
405 int joint_scale_factor
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< joint subband scale factors
406 float downmix_coef
[DCA_PRIM_CHANNELS_MAX
+ 1][2]; ///< stereo downmix coefficients
407 int dynrange_coef
; ///< dynamic range coefficient
409 /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
410 * Input: primary audio channels (incl. LFE if present)
411 * Output: downmix audio channels (up to 4, no LFE) */
412 uint8_t core_downmix
; ///< embedded downmix coefficients available
413 uint8_t core_downmix_amode
; ///< audio channel arrangement of embedded downmix
414 uint16_t core_downmix_codes
[DCA_PRIM_CHANNELS_MAX
+ 1][4]; ///< embedded downmix coefficients (9-bit codes)
416 int32_t high_freq_vq
[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
]; ///< VQ encoded high frequency subbands
418 float lfe_data
[2 * DCA_LFE_MAX
* (DCA_BLOCKS_MAX
+ 4)]; ///< Low frequency effect data
419 int lfe_scale_factor
;
421 /* Subband samples history (for ADPCM) */
422 DECLARE_ALIGNED(16, float, subband_samples_hist
)[DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
][4];
423 DECLARE_ALIGNED(32, float, subband_fir_hist
)[DCA_PRIM_CHANNELS_MAX
][512];
424 DECLARE_ALIGNED(32, float, subband_fir_noidea
)[DCA_PRIM_CHANNELS_MAX
][32];
425 int hist_index
[DCA_PRIM_CHANNELS_MAX
];
426 DECLARE_ALIGNED(32, float, raXin
)[32];
428 int output
; ///< type of output
430 DECLARE_ALIGNED(32, float, subband_samples
)[DCA_BLOCKS_MAX
][DCA_PRIM_CHANNELS_MAX
][DCA_SUBBANDS
][8];
431 float *samples_chanptr
[DCA_PRIM_CHANNELS_MAX
+ 1];
432 float *extra_channels
[DCA_PRIM_CHANNELS_MAX
+ 1];
433 uint8_t *extra_channels_buffer
;
434 unsigned int extra_channels_buffer_size
;
436 uint8_t dca_buffer
[DCA_MAX_FRAME_SIZE
+ DCA_MAX_EXSS_HEADER_SIZE
+ DCA_BUFFER_PADDING_SIZE
];
437 int dca_buffer_size
; ///< how much data is in the dca_buffer
439 const int8_t *channel_order_tab
; ///< channel reordering table, lfe and non lfe
441 /* Current position in DCA frame */
442 int current_subframe
;
443 int current_subsubframe
;
445 int core_ext_mask
; ///< present extensions in the core substream
447 /* XCh extension information */
448 int xch_present
; ///< XCh extension present and valid
449 int xch_base_channel
; ///< index of first (only) channel containing XCH data
450 int xch_disable
; ///< whether the XCh extension should be decoded or not
452 /* XXCH extension information */
454 int xxch_nbits_spk_mask
;
455 uint32_t xxch_core_spkmask
;
456 uint32_t xxch_spk_masks
[4]; /* speaker masks, last element is core mask */
457 int xxch_chset_nch
[4];
458 float xxch_dmix_sf
[DCA_CHSETS_MAX
];
460 uint32_t xxch_dmix_embedded
; /* lower layer has mix pre-embedded, per chset */
461 float xxch_dmix_coeff
[DCA_PRIM_CHANNELS_MAX
][32]; /* worst case sizing */
463 int8_t xxch_order_tab
[32];
466 /* ExSS header parser */
467 int static_fields
; ///< static fields present
468 int mix_metadata
; ///< mixing metadata present
469 int num_mix_configs
; ///< number of mix out configurations
470 int mix_config_num_ch
[4]; ///< number of channels in each mix out configuration
474 int debug_flag
; ///< used for suppressing repeated error messages output
475 AVFloatDSPContext
*fdsp
;
477 SynthFilterContext synth
;
478 DCADSPContext dcadsp
;
479 FmtConvertContext fmt_conv
;
482 static float dca_dmix_code(unsigned code
);
484 static const uint16_t dca_vlc_offs
[] = {
485 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
486 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
487 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
488 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
489 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
490 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
493 static av_cold
void dca_init_vlcs(void)
495 static int vlcs_initialized
= 0;
497 static VLC_TYPE dca_table
[23622][2];
499 if (vlcs_initialized
)
502 dca_bitalloc_index
.offset
= 1;
503 dca_bitalloc_index
.wrap
= 2;
504 for (i
= 0; i
< 5; i
++) {
505 dca_bitalloc_index
.vlc
[i
].table
= &dca_table
[dca_vlc_offs
[i
]];
506 dca_bitalloc_index
.vlc
[i
].table_allocated
= dca_vlc_offs
[i
+ 1] - dca_vlc_offs
[i
];
507 init_vlc(&dca_bitalloc_index
.vlc
[i
], bitalloc_12_vlc_bits
[i
], 12,
508 bitalloc_12_bits
[i
], 1, 1,
509 bitalloc_12_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
511 dca_scalefactor
.offset
= -64;
512 dca_scalefactor
.wrap
= 2;
513 for (i
= 0; i
< 5; i
++) {
514 dca_scalefactor
.vlc
[i
].table
= &dca_table
[dca_vlc_offs
[i
+ 5]];
515 dca_scalefactor
.vlc
[i
].table_allocated
= dca_vlc_offs
[i
+ 6] - dca_vlc_offs
[i
+ 5];
516 init_vlc(&dca_scalefactor
.vlc
[i
], SCALES_VLC_BITS
, 129,
517 scales_bits
[i
], 1, 1,
518 scales_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
520 dca_tmode
.offset
= 0;
522 for (i
= 0; i
< 4; i
++) {
523 dca_tmode
.vlc
[i
].table
= &dca_table
[dca_vlc_offs
[i
+ 10]];
524 dca_tmode
.vlc
[i
].table_allocated
= dca_vlc_offs
[i
+ 11] - dca_vlc_offs
[i
+ 10];
525 init_vlc(&dca_tmode
.vlc
[i
], tmode_vlc_bits
[i
], 4,
527 tmode_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
530 for (i
= 0; i
< 10; i
++)
531 for (j
= 0; j
< 7; j
++) {
532 if (!bitalloc_codes
[i
][j
])
534 dca_smpl_bitalloc
[i
+ 1].offset
= bitalloc_offsets
[i
];
535 dca_smpl_bitalloc
[i
+ 1].wrap
= 1 + (j
> 4);
536 dca_smpl_bitalloc
[i
+ 1].vlc
[j
].table
= &dca_table
[dca_vlc_offs
[c
]];
537 dca_smpl_bitalloc
[i
+ 1].vlc
[j
].table_allocated
= dca_vlc_offs
[c
+ 1] - dca_vlc_offs
[c
];
539 init_vlc(&dca_smpl_bitalloc
[i
+ 1].vlc
[j
], bitalloc_maxbits
[i
][j
],
541 bitalloc_bits
[i
][j
], 1, 1,
542 bitalloc_codes
[i
][j
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
545 vlcs_initialized
= 1;
548 static inline void get_array(GetBitContext
*gb
, int *dst
, int len
, int bits
)
551 *dst
++ = get_bits(gb
, bits
);
554 static inline int dca_xxch2index(DCAContext
*s
, int xxch_ch
)
558 /* locate channel set containing the channel */
559 for (i
= -1, base
= 0, mask
= (s
->xxch_core_spkmask
& ~DCA_XXCH_LFE1
);
560 i
<= s
->xxch_chset
&& !(mask
& xxch_ch
); mask
= s
->xxch_spk_masks
[++i
])
561 base
+= av_popcount(mask
);
563 return base
+ av_popcount(mask
& (xxch_ch
- 1));
566 static int dca_parse_audio_coding_header(DCAContext
*s
, int base_channel
,
570 static const float adj_table
[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
571 static const int bitlen
[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
572 static const int thr
[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
573 int hdr_pos
= 0, hdr_size
= 0;
575 int this_chans
, acc_mask
;
576 int embedded_downmix
;
580 /* xxch has arbitrary sized audio coding headers */
582 hdr_pos
= get_bits_count(&s
->gb
);
583 hdr_size
= get_bits(&s
->gb
, 7) + 1;
586 nchans
= get_bits(&s
->gb
, 3) + 1;
587 s
->total_channels
= nchans
+ base_channel
;
588 s
->prim_channels
= s
->total_channels
;
590 /* obtain speaker layout mask & downmix coefficients for XXCH */
592 acc_mask
= s
->xxch_core_spkmask
;
594 this_chans
= get_bits(&s
->gb
, s
->xxch_nbits_spk_mask
- 6) << 6;
595 s
->xxch_spk_masks
[s
->xxch_chset
] = this_chans
;
596 s
->xxch_chset_nch
[s
->xxch_chset
] = nchans
;
598 for (i
= 0; i
<= s
->xxch_chset
; i
++)
599 acc_mask
|= s
->xxch_spk_masks
[i
];
601 /* check for downmixing information */
602 if (get_bits1(&s
->gb
)) {
603 embedded_downmix
= get_bits1(&s
->gb
);
604 coeff
= get_bits(&s
->gb
, 6);
606 if (coeff
<1 || coeff
>61) {
607 av_log(s
->avctx
, AV_LOG_ERROR
, "6bit coeff %d is out of range\n", coeff
);
608 return AVERROR_INVALIDDATA
;
611 scale_factor
= -1.0f
/ dca_dmix_code((coeff
<<2)-3);
613 s
->xxch_dmix_sf
[s
->xxch_chset
] = scale_factor
;
615 for (i
= base_channel
; i
< s
->prim_channels
; i
++) {
616 mask
[i
] = get_bits(&s
->gb
, s
->xxch_nbits_spk_mask
);
619 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
620 memset(s
->xxch_dmix_coeff
[j
], 0, sizeof(s
->xxch_dmix_coeff
[0]));
621 s
->xxch_dmix_embedded
|= (embedded_downmix
<< j
);
622 for (i
= 0; i
< s
->xxch_nbits_spk_mask
; i
++) {
623 if (mask
[j
] & (1 << i
)) {
624 if ((1 << i
) == DCA_XXCH_LFE1
) {
625 av_log(s
->avctx
, AV_LOG_WARNING
,
626 "DCA-XXCH: dmix to LFE1 not supported.\n");
630 coeff
= get_bits(&s
->gb
, 7);
631 ichan
= dca_xxch2index(s
, 1 << i
);
632 if ((coeff
&63)<1 || (coeff
&63)>61) {
633 av_log(s
->avctx
, AV_LOG_ERROR
, "7bit coeff %d is out of range\n", coeff
);
634 return AVERROR_INVALIDDATA
;
636 s
->xxch_dmix_coeff
[j
][ichan
] = dca_dmix_code((coeff
<<2)-3);
643 if (s
->prim_channels
> DCA_PRIM_CHANNELS_MAX
)
644 s
->prim_channels
= DCA_PRIM_CHANNELS_MAX
;
646 for (i
= base_channel
; i
< s
->prim_channels
; i
++) {
647 s
->subband_activity
[i
] = get_bits(&s
->gb
, 5) + 2;
648 if (s
->subband_activity
[i
] > DCA_SUBBANDS
)
649 s
->subband_activity
[i
] = DCA_SUBBANDS
;
651 for (i
= base_channel
; i
< s
->prim_channels
; i
++) {
652 s
->vq_start_subband
[i
] = get_bits(&s
->gb
, 5) + 1;
653 if (s
->vq_start_subband
[i
] > DCA_SUBBANDS
)
654 s
->vq_start_subband
[i
] = DCA_SUBBANDS
;
656 get_array(&s
->gb
, s
->joint_intensity
+ base_channel
, s
->prim_channels
- base_channel
, 3);
657 get_array(&s
->gb
, s
->transient_huffman
+ base_channel
, s
->prim_channels
- base_channel
, 2);
658 get_array(&s
->gb
, s
->scalefactor_huffman
+ base_channel
, s
->prim_channels
- base_channel
, 3);
659 get_array(&s
->gb
, s
->bitalloc_huffman
+ base_channel
, s
->prim_channels
- base_channel
, 3);
661 /* Get codebooks quantization indexes */
663 memset(s
->quant_index_huffman
, 0, sizeof(s
->quant_index_huffman
));
664 for (j
= 1; j
< 11; j
++)
665 for (i
= base_channel
; i
< s
->prim_channels
; i
++)
666 s
->quant_index_huffman
[i
][j
] = get_bits(&s
->gb
, bitlen
[j
]);
668 /* Get scale factor adjustment */
669 for (j
= 0; j
< 11; j
++)
670 for (i
= base_channel
; i
< s
->prim_channels
; i
++)
671 s
->scalefactor_adj
[i
][j
] = 1;
673 for (j
= 1; j
< 11; j
++)
674 for (i
= base_channel
; i
< s
->prim_channels
; i
++)
675 if (s
->quant_index_huffman
[i
][j
] < thr
[j
])
676 s
->scalefactor_adj
[i
][j
] = adj_table
[get_bits(&s
->gb
, 2)];
679 if (s
->crc_present
) {
680 /* Audio header CRC check */
681 get_bits(&s
->gb
, 16);
684 /* Skip to the end of the header, also ignore CRC if present */
685 i
= get_bits_count(&s
->gb
);
686 if (hdr_pos
+ 8 * hdr_size
> i
)
687 skip_bits_long(&s
->gb
, hdr_pos
+ 8 * hdr_size
- i
);
690 s
->current_subframe
= 0;
691 s
->current_subsubframe
= 0;
694 av_log(s
->avctx
, AV_LOG_DEBUG
, "subframes: %i\n", s
->subframes
);
695 av_log(s
->avctx
, AV_LOG_DEBUG
, "prim channels: %i\n", s
->prim_channels
);
696 for (i
= base_channel
; i
< s
->prim_channels
; i
++) {
697 av_log(s
->avctx
, AV_LOG_DEBUG
, "subband activity: %i\n",
698 s
->subband_activity
[i
]);
699 av_log(s
->avctx
, AV_LOG_DEBUG
, "vq start subband: %i\n",
700 s
->vq_start_subband
[i
]);
701 av_log(s
->avctx
, AV_LOG_DEBUG
, "joint intensity: %i\n",
702 s
->joint_intensity
[i
]);
703 av_log(s
->avctx
, AV_LOG_DEBUG
, "transient mode codebook: %i\n",
704 s
->transient_huffman
[i
]);
705 av_log(s
->avctx
, AV_LOG_DEBUG
, "scale factor codebook: %i\n",
706 s
->scalefactor_huffman
[i
]);
707 av_log(s
->avctx
, AV_LOG_DEBUG
, "bit allocation quantizer: %i\n",
708 s
->bitalloc_huffman
[i
]);
709 av_log(s
->avctx
, AV_LOG_DEBUG
, "quant index huff:");
710 for (j
= 0; j
< 11; j
++)
711 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i", s
->quant_index_huffman
[i
][j
]);
712 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
713 av_log(s
->avctx
, AV_LOG_DEBUG
, "scalefac adj:");
714 for (j
= 0; j
< 11; j
++)
715 av_log(s
->avctx
, AV_LOG_DEBUG
, " %1.3f", s
->scalefactor_adj
[i
][j
]);
716 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
723 static int dca_parse_frame_header(DCAContext
*s
)
725 init_get_bits(&s
->gb
, s
->dca_buffer
, s
->dca_buffer_size
* 8);
728 skip_bits_long(&s
->gb
, 32);
731 s
->frame_type
= get_bits(&s
->gb
, 1);
732 s
->samples_deficit
= get_bits(&s
->gb
, 5) + 1;
733 s
->crc_present
= get_bits(&s
->gb
, 1);
734 s
->sample_blocks
= get_bits(&s
->gb
, 7) + 1;
735 s
->frame_size
= get_bits(&s
->gb
, 14) + 1;
736 if (s
->frame_size
< 95)
737 return AVERROR_INVALIDDATA
;
738 s
->amode
= get_bits(&s
->gb
, 6);
739 s
->sample_rate
= avpriv_dca_sample_rates
[get_bits(&s
->gb
, 4)];
741 return AVERROR_INVALIDDATA
;
742 s
->bit_rate_index
= get_bits(&s
->gb
, 5);
743 s
->bit_rate
= dca_bit_rates
[s
->bit_rate_index
];
745 return AVERROR_INVALIDDATA
;
747 skip_bits1(&s
->gb
); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
748 s
->dynrange
= get_bits(&s
->gb
, 1);
749 s
->timestamp
= get_bits(&s
->gb
, 1);
750 s
->aux_data
= get_bits(&s
->gb
, 1);
751 s
->hdcd
= get_bits(&s
->gb
, 1);
752 s
->ext_descr
= get_bits(&s
->gb
, 3);
753 s
->ext_coding
= get_bits(&s
->gb
, 1);
754 s
->aspf
= get_bits(&s
->gb
, 1);
755 s
->lfe
= get_bits(&s
->gb
, 2);
756 s
->predictor_history
= get_bits(&s
->gb
, 1);
760 av_log(s
->avctx
, AV_LOG_ERROR
, "Invalid LFE value: %d\n", s
->lfe
);
761 return AVERROR_INVALIDDATA
;
764 /* TODO: check CRC */
766 s
->header_crc
= get_bits(&s
->gb
, 16);
768 s
->multirate_inter
= get_bits(&s
->gb
, 1);
769 s
->version
= get_bits(&s
->gb
, 4);
770 s
->copy_history
= get_bits(&s
->gb
, 2);
771 s
->source_pcm_res
= get_bits(&s
->gb
, 3);
772 s
->front_sum
= get_bits(&s
->gb
, 1);
773 s
->surround_sum
= get_bits(&s
->gb
, 1);
774 s
->dialog_norm
= get_bits(&s
->gb
, 4);
776 /* FIXME: channels mixing levels */
777 s
->output
= s
->amode
;
779 s
->output
|= DCA_LFE
;
782 av_log(s
->avctx
, AV_LOG_DEBUG
, "frame type: %i\n", s
->frame_type
);
783 av_log(s
->avctx
, AV_LOG_DEBUG
, "samples deficit: %i\n", s
->samples_deficit
);
784 av_log(s
->avctx
, AV_LOG_DEBUG
, "crc present: %i\n", s
->crc_present
);
785 av_log(s
->avctx
, AV_LOG_DEBUG
, "sample blocks: %i (%i samples)\n",
786 s
->sample_blocks
, s
->sample_blocks
* 32);
787 av_log(s
->avctx
, AV_LOG_DEBUG
, "frame size: %i bytes\n", s
->frame_size
);
788 av_log(s
->avctx
, AV_LOG_DEBUG
, "amode: %i (%i channels)\n",
789 s
->amode
, dca_channels
[s
->amode
]);
790 av_log(s
->avctx
, AV_LOG_DEBUG
, "sample rate: %i Hz\n",
792 av_log(s
->avctx
, AV_LOG_DEBUG
, "bit rate: %i bits/s\n",
794 av_log(s
->avctx
, AV_LOG_DEBUG
, "dynrange: %i\n", s
->dynrange
);
795 av_log(s
->avctx
, AV_LOG_DEBUG
, "timestamp: %i\n", s
->timestamp
);
796 av_log(s
->avctx
, AV_LOG_DEBUG
, "aux_data: %i\n", s
->aux_data
);
797 av_log(s
->avctx
, AV_LOG_DEBUG
, "hdcd: %i\n", s
->hdcd
);
798 av_log(s
->avctx
, AV_LOG_DEBUG
, "ext descr: %i\n", s
->ext_descr
);
799 av_log(s
->avctx
, AV_LOG_DEBUG
, "ext coding: %i\n", s
->ext_coding
);
800 av_log(s
->avctx
, AV_LOG_DEBUG
, "aspf: %i\n", s
->aspf
);
801 av_log(s
->avctx
, AV_LOG_DEBUG
, "lfe: %i\n", s
->lfe
);
802 av_log(s
->avctx
, AV_LOG_DEBUG
, "predictor history: %i\n",
803 s
->predictor_history
);
804 av_log(s
->avctx
, AV_LOG_DEBUG
, "header crc: %i\n", s
->header_crc
);
805 av_log(s
->avctx
, AV_LOG_DEBUG
, "multirate inter: %i\n",
807 av_log(s
->avctx
, AV_LOG_DEBUG
, "version number: %i\n", s
->version
);
808 av_log(s
->avctx
, AV_LOG_DEBUG
, "copy history: %i\n", s
->copy_history
);
809 av_log(s
->avctx
, AV_LOG_DEBUG
,
810 "source pcm resolution: %i (%i bits/sample)\n",
811 s
->source_pcm_res
, dca_bits_per_sample
[s
->source_pcm_res
]);
812 av_log(s
->avctx
, AV_LOG_DEBUG
, "front sum: %i\n", s
->front_sum
);
813 av_log(s
->avctx
, AV_LOG_DEBUG
, "surround sum: %i\n", s
->surround_sum
);
814 av_log(s
->avctx
, AV_LOG_DEBUG
, "dialog norm: %i\n", s
->dialog_norm
);
815 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
818 /* Primary audio coding header */
819 s
->subframes
= get_bits(&s
->gb
, 4) + 1;
821 return dca_parse_audio_coding_header(s
, 0, 0);
824 static inline int get_scale(GetBitContext
*gb
, int level
, int value
, int log2range
)
827 /* huffman encoded */
828 value
+= get_bitalloc(gb
, &dca_scalefactor
, level
);
829 value
= av_clip(value
, 0, (1 << log2range
) - 1);
830 } else if (level
< 8) {
831 if (level
+ 1 > log2range
) {
832 skip_bits(gb
, level
+ 1 - log2range
);
833 value
= get_bits(gb
, log2range
);
835 value
= get_bits(gb
, level
+ 1);
841 static int dca_subframe_header(DCAContext
*s
, int base_channel
, int block_index
)
843 /* Primary audio coding side information */
846 if (get_bits_left(&s
->gb
) < 0)
847 return AVERROR_INVALIDDATA
;
850 s
->subsubframes
[s
->current_subframe
] = get_bits(&s
->gb
, 2) + 1;
851 s
->partial_samples
[s
->current_subframe
] = get_bits(&s
->gb
, 3);
854 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
855 for (k
= 0; k
< s
->subband_activity
[j
]; k
++)
856 s
->prediction_mode
[j
][k
] = get_bits(&s
->gb
, 1);
859 /* Get prediction codebook */
860 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
861 for (k
= 0; k
< s
->subband_activity
[j
]; k
++) {
862 if (s
->prediction_mode
[j
][k
] > 0) {
863 /* (Prediction coefficient VQ address) */
864 s
->prediction_vq
[j
][k
] = get_bits(&s
->gb
, 12);
869 /* Bit allocation index */
870 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
871 for (k
= 0; k
< s
->vq_start_subband
[j
]; k
++) {
872 if (s
->bitalloc_huffman
[j
] == 6)
873 s
->bitalloc
[j
][k
] = get_bits(&s
->gb
, 5);
874 else if (s
->bitalloc_huffman
[j
] == 5)
875 s
->bitalloc
[j
][k
] = get_bits(&s
->gb
, 4);
876 else if (s
->bitalloc_huffman
[j
] == 7) {
877 av_log(s
->avctx
, AV_LOG_ERROR
,
878 "Invalid bit allocation index\n");
879 return AVERROR_INVALIDDATA
;
882 get_bitalloc(&s
->gb
, &dca_bitalloc_index
, s
->bitalloc_huffman
[j
]);
885 if (s
->bitalloc
[j
][k
] > 26) {
886 av_dlog(s
->avctx
, "bitalloc index [%i][%i] too big (%i)\n",
887 j
, k
, s
->bitalloc
[j
][k
]);
888 return AVERROR_INVALIDDATA
;
893 /* Transition mode */
894 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
895 for (k
= 0; k
< s
->subband_activity
[j
]; k
++) {
896 s
->transition_mode
[j
][k
] = 0;
897 if (s
->subsubframes
[s
->current_subframe
] > 1 &&
898 k
< s
->vq_start_subband
[j
] && s
->bitalloc
[j
][k
] > 0) {
899 s
->transition_mode
[j
][k
] =
900 get_bitalloc(&s
->gb
, &dca_tmode
, s
->transient_huffman
[j
]);
905 if (get_bits_left(&s
->gb
) < 0)
906 return AVERROR_INVALIDDATA
;
908 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
909 const uint32_t *scale_table
;
910 int scale_sum
, log_size
;
912 memset(s
->scale_factor
[j
], 0,
913 s
->subband_activity
[j
] * sizeof(s
->scale_factor
[0][0][0]) * 2);
915 if (s
->scalefactor_huffman
[j
] == 6) {
916 scale_table
= scale_factor_quant7
;
919 scale_table
= scale_factor_quant6
;
923 /* When huffman coded, only the difference is encoded */
926 for (k
= 0; k
< s
->subband_activity
[j
]; k
++) {
927 if (k
>= s
->vq_start_subband
[j
] || s
->bitalloc
[j
][k
] > 0) {
928 scale_sum
= get_scale(&s
->gb
, s
->scalefactor_huffman
[j
], scale_sum
, log_size
);
929 s
->scale_factor
[j
][k
][0] = scale_table
[scale_sum
];
932 if (k
< s
->vq_start_subband
[j
] && s
->transition_mode
[j
][k
]) {
933 /* Get second scale factor */
934 scale_sum
= get_scale(&s
->gb
, s
->scalefactor_huffman
[j
], scale_sum
, log_size
);
935 s
->scale_factor
[j
][k
][1] = scale_table
[scale_sum
];
940 /* Joint subband scale factor codebook select */
941 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
942 /* Transmitted only if joint subband coding enabled */
943 if (s
->joint_intensity
[j
] > 0)
944 s
->joint_huff
[j
] = get_bits(&s
->gb
, 3);
947 if (get_bits_left(&s
->gb
) < 0)
948 return AVERROR_INVALIDDATA
;
950 /* Scale factors for joint subband coding */
951 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
954 /* Transmitted only if joint subband coding enabled */
955 if (s
->joint_intensity
[j
] > 0) {
957 source_channel
= s
->joint_intensity
[j
] - 1;
959 /* When huffman coded, only the difference is encoded
960 * (is this valid as well for joint scales ???) */
962 for (k
= s
->subband_activity
[j
]; k
< s
->subband_activity
[source_channel
]; k
++) {
963 scale
= get_scale(&s
->gb
, s
->joint_huff
[j
], 64 /* bias */, 7);
964 s
->joint_scale_factor
[j
][k
] = scale
; /*joint_scale_table[scale]; */
967 if (!(s
->debug_flag
& 0x02)) {
968 av_log(s
->avctx
, AV_LOG_DEBUG
,
969 "Joint stereo coding not supported\n");
970 s
->debug_flag
|= 0x02;
975 /* Dynamic range coefficient */
976 if (!base_channel
&& s
->dynrange
)
977 s
->dynrange_coef
= get_bits(&s
->gb
, 8);
979 /* Side information CRC check word */
980 if (s
->crc_present
) {
981 get_bits(&s
->gb
, 16);
985 * Primary audio data arrays
988 /* VQ encoded high frequency subbands */
989 for (j
= base_channel
; j
< s
->prim_channels
; j
++)
990 for (k
= s
->vq_start_subband
[j
]; k
< s
->subband_activity
[j
]; k
++)
991 /* 1 vector -> 32 samples */
992 s
->high_freq_vq
[j
][k
] = get_bits(&s
->gb
, 10);
994 /* Low frequency effect data */
995 if (!base_channel
&& s
->lfe
) {
998 int lfe_samples
= 2 * s
->lfe
* (4 + block_index
);
999 int lfe_end_sample
= 2 * s
->lfe
* (4 + block_index
+ s
->subsubframes
[s
->current_subframe
]);
1002 for (j
= lfe_samples
; j
< lfe_end_sample
; j
++) {
1003 /* Signed 8 bits int */
1004 s
->lfe_data
[j
] = get_sbits(&s
->gb
, 8);
1007 /* Scale factor index */
1008 quant7
= get_bits(&s
->gb
, 8);
1010 avpriv_request_sample(s
->avctx
, "LFEScaleIndex larger than 127");
1011 return AVERROR_INVALIDDATA
;
1013 s
->lfe_scale_factor
= scale_factor_quant7
[quant7
];
1015 /* Quantization step size * scale factor */
1016 lfe_scale
= 0.035 * s
->lfe_scale_factor
;
1018 for (j
= lfe_samples
; j
< lfe_end_sample
; j
++)
1019 s
->lfe_data
[j
] *= lfe_scale
;
1023 av_log(s
->avctx
, AV_LOG_DEBUG
, "subsubframes: %i\n",
1024 s
->subsubframes
[s
->current_subframe
]);
1025 av_log(s
->avctx
, AV_LOG_DEBUG
, "partial samples: %i\n",
1026 s
->partial_samples
[s
->current_subframe
]);
1028 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
1029 av_log(s
->avctx
, AV_LOG_DEBUG
, "prediction mode:");
1030 for (k
= 0; k
< s
->subband_activity
[j
]; k
++)
1031 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i", s
->prediction_mode
[j
][k
]);
1032 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
1034 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
1035 for (k
= 0; k
< s
->subband_activity
[j
]; k
++)
1036 av_log(s
->avctx
, AV_LOG_DEBUG
,
1037 "prediction coefs: %f, %f, %f, %f\n",
1038 (float) adpcm_vb
[s
->prediction_vq
[j
][k
]][0] / 8192,
1039 (float) adpcm_vb
[s
->prediction_vq
[j
][k
]][1] / 8192,
1040 (float) adpcm_vb
[s
->prediction_vq
[j
][k
]][2] / 8192,
1041 (float) adpcm_vb
[s
->prediction_vq
[j
][k
]][3] / 8192);
1043 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
1044 av_log(s
->avctx
, AV_LOG_DEBUG
, "bitalloc index: ");
1045 for (k
= 0; k
< s
->vq_start_subband
[j
]; k
++)
1046 av_log(s
->avctx
, AV_LOG_DEBUG
, "%2.2i ", s
->bitalloc
[j
][k
]);
1047 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
1049 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
1050 av_log(s
->avctx
, AV_LOG_DEBUG
, "Transition mode:");
1051 for (k
= 0; k
< s
->subband_activity
[j
]; k
++)
1052 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i", s
->transition_mode
[j
][k
]);
1053 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
1055 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
1056 av_log(s
->avctx
, AV_LOG_DEBUG
, "Scale factor:");
1057 for (k
= 0; k
< s
->subband_activity
[j
]; k
++) {
1058 if (k
>= s
->vq_start_subband
[j
] || s
->bitalloc
[j
][k
] > 0)
1059 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i", s
->scale_factor
[j
][k
][0]);
1060 if (k
< s
->vq_start_subband
[j
] && s
->transition_mode
[j
][k
])
1061 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i(t)", s
->scale_factor
[j
][k
][1]);
1063 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
1065 for (j
= base_channel
; j
< s
->prim_channels
; j
++) {
1066 if (s
->joint_intensity
[j
] > 0) {
1067 int source_channel
= s
->joint_intensity
[j
] - 1;
1068 av_log(s
->avctx
, AV_LOG_DEBUG
, "Joint scale factor index:\n");
1069 for (k
= s
->subband_activity
[j
]; k
< s
->subband_activity
[source_channel
]; k
++)
1070 av_log(s
->avctx
, AV_LOG_DEBUG
, " %i", s
->joint_scale_factor
[j
][k
]);
1071 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
1074 for (j
= base_channel
; j
< s
->prim_channels
; j
++)
1075 for (k
= s
->vq_start_subband
[j
]; k
< s
->subband_activity
[j
]; k
++)
1076 av_log(s
->avctx
, AV_LOG_DEBUG
, "VQ index: %i\n", s
->high_freq_vq
[j
][k
]);
1077 if (!base_channel
&& s
->lfe
) {
1078 int lfe_samples
= 2 * s
->lfe
* (4 + block_index
);
1079 int lfe_end_sample
= 2 * s
->lfe
* (4 + block_index
+ s
->subsubframes
[s
->current_subframe
]);
1081 av_log(s
->avctx
, AV_LOG_DEBUG
, "LFE samples:\n");
1082 for (j
= lfe_samples
; j
< lfe_end_sample
; j
++)
1083 av_log(s
->avctx
, AV_LOG_DEBUG
, " %f", s
->lfe_data
[j
]);
1084 av_log(s
->avctx
, AV_LOG_DEBUG
, "\n");
1091 static void qmf_32_subbands(DCAContext
*s
, int chans
,
1092 float samples_in
[32][8], float *samples_out
,
1095 const float *prCoeff
;
1097 int sb_act
= s
->subband_activity
[chans
];
1099 scale
*= sqrt(1 / 8.0);
1102 if (!s
->multirate_inter
) /* Non-perfect reconstruction */
1103 prCoeff
= fir_32bands_nonperfect
;
1104 else /* Perfect reconstruction */
1105 prCoeff
= fir_32bands_perfect
;
1107 s
->dcadsp
.qmf_32_subbands(samples_in
, sb_act
, &s
->synth
, &s
->imdct
,
1108 s
->subband_fir_hist
[chans
],
1109 &s
->hist_index
[chans
],
1110 s
->subband_fir_noidea
[chans
], prCoeff
,
1111 samples_out
, s
->raXin
, scale
);
1114 static void lfe_interpolation_fir(DCAContext
*s
, int decimation_select
,
1115 int num_deci_sample
, float *samples_in
,
1118 /* samples_in: An array holding decimated samples.
1119 * Samples in current subframe starts from samples_in[0],
1120 * while samples_in[-1], samples_in[-2], ..., stores samples
1121 * from last subframe as history.
1123 * samples_out: An array holding interpolated samples
1127 const float *prCoeff
;
1130 /* Select decimation filter */
1131 if (decimation_select
== 1) {
1133 prCoeff
= lfe_fir_128
;
1136 prCoeff
= lfe_fir_64
;
1139 for (deciindex
= 0; deciindex
< num_deci_sample
; deciindex
++) {
1140 s
->dcadsp
.lfe_fir
[idx
](samples_out
, samples_in
, prCoeff
);
1142 samples_out
+= 2 * 32 * (1 + idx
);
1146 /* downmixing routines */
1147 #define MIX_REAR1(samples, s1, rs, coef) \
1148 samples[0][i] += samples[s1][i] * coef[rs][0]; \
1149 samples[1][i] += samples[s1][i] * coef[rs][1];
1151 #define MIX_REAR2(samples, s1, s2, rs, coef) \
1152 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
1153 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
1155 #define MIX_FRONT3(samples, coef) \
1156 t = samples[c][i]; \
1157 u = samples[l][i]; \
1158 v = samples[r][i]; \
1159 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1160 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1162 #define DOWNMIX_TO_STEREO(op1, op2) \
1163 for (i = 0; i < 256; i++) { \
1168 static void dca_downmix(float **samples
, int srcfmt
, int lfe_present
,
1169 float coef
[DCA_PRIM_CHANNELS_MAX
+ 1][2],
1170 const int8_t *channel_mapping
)
1172 int c
, l
, r
, sl
, sr
, s
;
1179 av_log(NULL
, AV_LOG_ERROR
, "Not implemented!\n");
1183 case DCA_STEREO_TOTAL
:
1184 case DCA_STEREO_SUMDIFF
:
1187 c
= channel_mapping
[0];
1188 l
= channel_mapping
[1];
1189 r
= channel_mapping
[2];
1190 DOWNMIX_TO_STEREO(MIX_FRONT3(samples
, coef
), );
1193 s
= channel_mapping
[2];
1194 DOWNMIX_TO_STEREO(MIX_REAR1(samples
, s
, 2, coef
), );
1197 c
= channel_mapping
[0];
1198 l
= channel_mapping
[1];
1199 r
= channel_mapping
[2];
1200 s
= channel_mapping
[3];
1201 DOWNMIX_TO_STEREO(MIX_FRONT3(samples
, coef
),
1202 MIX_REAR1(samples
, s
, 3, coef
));
1205 sl
= channel_mapping
[2];
1206 sr
= channel_mapping
[3];
1207 DOWNMIX_TO_STEREO(MIX_REAR2(samples
, sl
, sr
, 2, coef
), );
1210 c
= channel_mapping
[0];
1211 l
= channel_mapping
[1];
1212 r
= channel_mapping
[2];
1213 sl
= channel_mapping
[3];
1214 sr
= channel_mapping
[4];
1215 DOWNMIX_TO_STEREO(MIX_FRONT3(samples
, coef
),
1216 MIX_REAR2(samples
, sl
, sr
, 3, coef
));
1220 int lf_buf
= dca_lfe_index
[srcfmt
];
1221 int lf_idx
= dca_channels
[srcfmt
];
1222 for (i
= 0; i
< 256; i
++) {
1223 samples
[0][i
] += samples
[lf_buf
][i
] * coef
[lf_idx
][0];
1224 samples
[1][i
] += samples
[lf_buf
][i
] * coef
[lf_idx
][1];
1229 #ifndef decode_blockcodes
1230 /* Very compact version of the block code decoder that does not use table
1231 * look-up but is slightly slower */
1232 static int decode_blockcode(int code
, int levels
, int32_t *values
)
1235 int offset
= (levels
- 1) >> 1;
1237 for (i
= 0; i
< 4; i
++) {
1238 int div
= FASTDIV(code
, levels
);
1239 values
[i
] = code
- offset
- div
* levels
;
1246 static int decode_blockcodes(int code1
, int code2
, int levels
, int32_t *values
)
1248 return decode_blockcode(code1
, levels
, values
) |
1249 decode_blockcode(code2
, levels
, values
+ 4);
1253 static const uint8_t abits_sizes
[7] = { 7, 10, 12, 13, 15, 17, 19 };
1254 static const uint8_t abits_levels
[7] = { 3, 5, 7, 9, 13, 17, 25 };
1256 static int dca_subsubframe(DCAContext
*s
, int base_channel
, int block_index
)
1259 int subsubframe
= s
->current_subsubframe
;
1261 const float *quant_step_table
;
1264 float (*subband_samples
)[DCA_SUBBANDS
][8] = s
->subband_samples
[block_index
];
1265 LOCAL_ALIGNED_16(int32_t, block
, [8 * DCA_SUBBANDS
]);
1271 /* Select quantization step size table */
1272 if (s
->bit_rate_index
== 0x1f)
1273 quant_step_table
= lossless_quant_d
;
1275 quant_step_table
= lossy_quant_d
;
1277 for (k
= base_channel
; k
< s
->prim_channels
; k
++) {
1278 float rscale
[DCA_SUBBANDS
];
1280 if (get_bits_left(&s
->gb
) < 0)
1281 return AVERROR_INVALIDDATA
;
1283 for (l
= 0; l
< s
->vq_start_subband
[k
]; l
++) {
1286 /* Select the mid-tread linear quantizer */
1287 int abits
= s
->bitalloc
[k
][l
];
1289 float quant_step_size
= quant_step_table
[abits
];
1292 * Determine quantization index code book and its type
1295 /* Select quantization index code book */
1296 int sel
= s
->quant_index_huffman
[k
][abits
];
1299 * Extract bits from the bit stream
1303 memset(block
+ 8 * l
, 0, 8 * sizeof(block
[0]));
1305 /* Deal with transients */
1306 int sfi
= s
->transition_mode
[k
][l
] && subsubframe
>= s
->transition_mode
[k
][l
];
1307 rscale
[l
] = quant_step_size
* s
->scale_factor
[k
][l
][sfi
] *
1308 s
->scalefactor_adj
[k
][sel
];
1310 if (abits
>= 11 || !dca_smpl_bitalloc
[abits
].vlc
[sel
].table
) {
1313 int block_code1
, block_code2
, size
, levels
, err
;
1315 size
= abits_sizes
[abits
- 1];
1316 levels
= abits_levels
[abits
- 1];
1318 block_code1
= get_bits(&s
->gb
, size
);
1319 block_code2
= get_bits(&s
->gb
, size
);
1320 err
= decode_blockcodes(block_code1
, block_code2
,
1321 levels
, block
+ 8 * l
);
1323 av_log(s
->avctx
, AV_LOG_ERROR
,
1324 "ERROR: block code look-up failed\n");
1325 return AVERROR_INVALIDDATA
;
1329 for (m
= 0; m
< 8; m
++)
1330 block
[8 * l
+ m
] = get_sbits(&s
->gb
, abits
- 3);
1334 for (m
= 0; m
< 8; m
++)
1335 block
[8 * l
+ m
] = get_bitalloc(&s
->gb
,
1336 &dca_smpl_bitalloc
[abits
], sel
);
1341 s
->fmt_conv
.int32_to_float_fmul_array8(&s
->fmt_conv
, subband_samples
[k
][0],
1342 block
, rscale
, 8 * s
->vq_start_subband
[k
]);
1344 for (l
= 0; l
< s
->vq_start_subband
[k
]; l
++) {
1347 * Inverse ADPCM if in prediction mode
1349 if (s
->prediction_mode
[k
][l
]) {
1351 if (s
->predictor_history
)
1352 subband_samples
[k
][l
][0] += (adpcm_vb
[s
->prediction_vq
[k
][l
]][0] *
1353 s
->subband_samples_hist
[k
][l
][3] +
1354 adpcm_vb
[s
->prediction_vq
[k
][l
]][1] *
1355 s
->subband_samples_hist
[k
][l
][2] +
1356 adpcm_vb
[s
->prediction_vq
[k
][l
]][2] *
1357 s
->subband_samples_hist
[k
][l
][1] +
1358 adpcm_vb
[s
->prediction_vq
[k
][l
]][3] *
1359 s
->subband_samples_hist
[k
][l
][0]) *
1361 for (m
= 1; m
< 8; m
++) {
1362 float sum
= adpcm_vb
[s
->prediction_vq
[k
][l
]][0] *
1363 subband_samples
[k
][l
][m
- 1];
1364 for (n
= 2; n
<= 4; n
++)
1366 sum
+= adpcm_vb
[s
->prediction_vq
[k
][l
]][n
- 1] *
1367 subband_samples
[k
][l
][m
- n
];
1368 else if (s
->predictor_history
)
1369 sum
+= adpcm_vb
[s
->prediction_vq
[k
][l
]][n
- 1] *
1370 s
->subband_samples_hist
[k
][l
][m
- n
+ 4];
1371 subband_samples
[k
][l
][m
] += sum
* (1.0f
/ 8192);
1377 * Decode VQ encoded high frequencies
1379 if (s
->subband_activity
[k
] > s
->vq_start_subband
[k
]) {
1380 if (!(s
->debug_flag
& 0x01)) {
1381 av_log(s
->avctx
, AV_LOG_DEBUG
,
1382 "Stream with high frequencies VQ coding\n");
1383 s
->debug_flag
|= 0x01;
1385 s
->dcadsp
.decode_hf(subband_samples
[k
], s
->high_freq_vq
[k
],
1386 high_freq_vq
, subsubframe
* 8,
1387 s
->scale_factor
[k
], s
->vq_start_subband
[k
],
1388 s
->subband_activity
[k
]);
1392 /* Check for DSYNC after subsubframe */
1393 if (s
->aspf
|| subsubframe
== s
->subsubframes
[s
->current_subframe
] - 1) {
1394 if (0xFFFF == get_bits(&s
->gb
, 16)) { /* 0xFFFF */
1396 av_log(s
->avctx
, AV_LOG_DEBUG
, "Got subframe DSYNC\n");
1399 av_log(s
->avctx
, AV_LOG_ERROR
, "Didn't get subframe DSYNC\n");
1400 return AVERROR_INVALIDDATA
;
1404 /* Backup predictor history for adpcm */
1405 for (k
= base_channel
; k
< s
->prim_channels
; k
++)
1406 for (l
= 0; l
< s
->vq_start_subband
[k
]; l
++)
1407 AV_COPY128(s
->subband_samples_hist
[k
][l
], &subband_samples
[k
][l
][4]);
1412 static int dca_filter_channels(DCAContext
*s
, int block_index
)
1414 float (*subband_samples
)[DCA_SUBBANDS
][8] = s
->subband_samples
[block_index
];
1417 /* 32 subbands QMF */
1418 for (k
= 0; k
< s
->prim_channels
; k
++) {
1419 if (s
->channel_order_tab
[k
] >= 0)
1420 qmf_32_subbands(s
, k
, subband_samples
[k
],
1421 s
->samples_chanptr
[s
->channel_order_tab
[k
]],
1422 M_SQRT1_2
/ 32768.0);
1425 /* Generate LFE samples for this subsubframe FIXME!!! */
1427 lfe_interpolation_fir(s
, s
->lfe
, 2 * s
->lfe
,
1428 s
->lfe_data
+ 2 * s
->lfe
* (block_index
+ 4),
1429 s
->samples_chanptr
[s
->lfe_index
]);
1430 /* Outputs 20bits pcm samples */
1433 /* Downmixing to Stereo */
1434 if (s
->prim_channels
+ !!s
->lfe
> 2 &&
1435 s
->avctx
->request_channel_layout
== AV_CH_LAYOUT_STEREO
) {
1436 dca_downmix(s
->samples_chanptr
, s
->amode
, !!s
->lfe
, s
->downmix_coef
,
1437 s
->channel_order_tab
);
1443 static int dca_subframe_footer(DCAContext
*s
, int base_channel
)
1445 int in
, out
, aux_data_count
, aux_data_end
, reserved
;
1449 * Unpack optional information
1452 /* presumably optional information only appears in the core? */
1453 if (!base_channel
) {
1455 skip_bits_long(&s
->gb
, 32);
1458 aux_data_count
= get_bits(&s
->gb
, 6);
1461 skip_bits_long(&s
->gb
, (-get_bits_count(&s
->gb
)) & 31);
1463 aux_data_end
= 8 * aux_data_count
+ get_bits_count(&s
->gb
);
1465 if ((nsyncaux
= get_bits_long(&s
->gb
, 32)) != DCA_NSYNCAUX
) {
1466 av_log(s
->avctx
, AV_LOG_ERROR
, "nSYNCAUX mismatch %#"PRIx32
"\n",
1468 return AVERROR_INVALIDDATA
;
1471 if (get_bits1(&s
->gb
)) { // bAUXTimeStampFlag
1472 avpriv_request_sample(s
->avctx
,
1473 "Auxiliary Decode Time Stamp Flag");
1475 skip_bits(&s
->gb
, (-get_bits_count(&s
->gb
)) & 4);
1476 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1477 skip_bits_long(&s
->gb
, 44);
1480 if ((s
->core_downmix
= get_bits1(&s
->gb
))) {
1481 int am
= get_bits(&s
->gb
, 3);
1484 s
->core_downmix_amode
= DCA_MONO
;
1487 s
->core_downmix_amode
= DCA_STEREO
;
1490 s
->core_downmix_amode
= DCA_STEREO_TOTAL
;
1493 s
->core_downmix_amode
= DCA_3F
;
1496 s
->core_downmix_amode
= DCA_2F1R
;
1499 s
->core_downmix_amode
= DCA_2F2R
;
1502 s
->core_downmix_amode
= DCA_3F1R
;
1505 av_log(s
->avctx
, AV_LOG_ERROR
,
1506 "Invalid mode %d for embedded downmix coefficients\n",
1508 return AVERROR_INVALIDDATA
;
1510 for (out
= 0; out
< dca_channels
[s
->core_downmix_amode
]; out
++) {
1511 for (in
= 0; in
< s
->prim_channels
+ !!s
->lfe
; in
++) {
1512 uint16_t tmp
= get_bits(&s
->gb
, 9);
1513 if ((tmp
& 0xFF) > 241) {
1514 av_log(s
->avctx
, AV_LOG_ERROR
,
1515 "Invalid downmix coefficient code %"PRIu16
"\n",
1517 return AVERROR_INVALIDDATA
;
1519 s
->core_downmix_codes
[in
][out
] = tmp
;
1524 align_get_bits(&s
->gb
); // byte align
1525 skip_bits(&s
->gb
, 16); // nAUXCRC16
1527 // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1528 if ((reserved
= (aux_data_end
- get_bits_count(&s
->gb
))) < 0) {
1529 av_log(s
->avctx
, AV_LOG_ERROR
,
1530 "Overread auxiliary data by %d bits\n", -reserved
);
1531 return AVERROR_INVALIDDATA
;
1532 } else if (reserved
) {
1533 avpriv_request_sample(s
->avctx
,
1534 "Core auxiliary data reserved content");
1535 skip_bits_long(&s
->gb
, reserved
);
1539 if (s
->crc_present
&& s
->dynrange
)
1540 get_bits(&s
->gb
, 16);
1547 * Decode a dca frame block
1549 * @param s pointer to the DCAContext
1552 static int dca_decode_block(DCAContext
*s
, int base_channel
, int block_index
)
1557 if (s
->current_subframe
>= s
->subframes
) {
1558 av_log(s
->avctx
, AV_LOG_DEBUG
, "check failed: %i>%i",
1559 s
->current_subframe
, s
->subframes
);
1560 return AVERROR_INVALIDDATA
;
1563 if (!s
->current_subsubframe
) {
1565 av_log(s
->avctx
, AV_LOG_DEBUG
, "DSYNC dca_subframe_header\n");
1567 /* Read subframe header */
1568 if ((ret
= dca_subframe_header(s
, base_channel
, block_index
)))
1572 /* Read subsubframe */
1574 av_log(s
->avctx
, AV_LOG_DEBUG
, "DSYNC dca_subsubframe\n");
1576 if ((ret
= dca_subsubframe(s
, base_channel
, block_index
)))
1580 s
->current_subsubframe
++;
1581 if (s
->current_subsubframe
>= s
->subsubframes
[s
->current_subframe
]) {
1582 s
->current_subsubframe
= 0;
1583 s
->current_subframe
++;
1585 if (s
->current_subframe
>= s
->subframes
) {
1587 av_log(s
->avctx
, AV_LOG_DEBUG
, "DSYNC dca_subframe_footer\n");
1589 /* Read subframe footer */
1590 if ((ret
= dca_subframe_footer(s
, base_channel
)))
1598 * Return the number of channels in an ExSS speaker mask (HD)
1600 static int dca_exss_mask2count(int mask
)
1602 /* count bits that mean speaker pairs twice */
1603 return av_popcount(mask
) +
1604 av_popcount(mask
& (DCA_EXSS_CENTER_LEFT_RIGHT
|
1605 DCA_EXSS_FRONT_LEFT_RIGHT
|
1606 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT
|
1607 DCA_EXSS_WIDE_LEFT_RIGHT
|
1608 DCA_EXSS_SIDE_LEFT_RIGHT
|
1609 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT
|
1610 DCA_EXSS_SIDE_REAR_LEFT_RIGHT
|
1611 DCA_EXSS_REAR_LEFT_RIGHT
|
1612 DCA_EXSS_REAR_HIGH_LEFT_RIGHT
));
1616 * Skip mixing coefficients of a single mix out configuration (HD)
1618 static void dca_exss_skip_mix_coeffs(GetBitContext
*gb
, int channels
, int out_ch
)
1622 for (i
= 0; i
< channels
; i
++) {
1623 int mix_map_mask
= get_bits(gb
, out_ch
);
1624 int num_coeffs
= av_popcount(mix_map_mask
);
1625 skip_bits_long(gb
, num_coeffs
* 6);
1630 * Parse extension substream asset header (HD)
1632 static int dca_exss_parse_asset_header(DCAContext
*s
)
1634 int header_pos
= get_bits_count(&s
->gb
);
1637 int embedded_stereo
= 0;
1638 int embedded_6ch
= 0;
1639 int drc_code_present
;
1640 int extensions_mask
= 0;
1643 if (get_bits_left(&s
->gb
) < 16)
1646 /* We will parse just enough to get to the extensions bitmask with which
1647 * we can set the profile value. */
1649 header_size
= get_bits(&s
->gb
, 9) + 1;
1650 skip_bits(&s
->gb
, 3); // asset index
1652 if (s
->static_fields
) {
1653 if (get_bits1(&s
->gb
))
1654 skip_bits(&s
->gb
, 4); // asset type descriptor
1655 if (get_bits1(&s
->gb
))
1656 skip_bits_long(&s
->gb
, 24); // language descriptor
1658 if (get_bits1(&s
->gb
)) {
1659 /* How can one fit 1024 bytes of text here if the maximum value
1660 * for the asset header size field above was 512 bytes? */
1661 int text_length
= get_bits(&s
->gb
, 10) + 1;
1662 if (get_bits_left(&s
->gb
) < text_length
* 8)
1664 skip_bits_long(&s
->gb
, text_length
* 8); // info text
1667 skip_bits(&s
->gb
, 5); // bit resolution - 1
1668 skip_bits(&s
->gb
, 4); // max sample rate code
1669 channels
= get_bits(&s
->gb
, 8) + 1;
1671 if (get_bits1(&s
->gb
)) { // 1-to-1 channels to speakers
1672 int spkr_remap_sets
;
1673 int spkr_mask_size
= 16;
1677 embedded_stereo
= get_bits1(&s
->gb
);
1679 embedded_6ch
= get_bits1(&s
->gb
);
1681 if (get_bits1(&s
->gb
)) {
1682 spkr_mask_size
= (get_bits(&s
->gb
, 2) + 1) << 2;
1683 skip_bits(&s
->gb
, spkr_mask_size
); // spkr activity mask
1686 spkr_remap_sets
= get_bits(&s
->gb
, 3);
1688 for (i
= 0; i
< spkr_remap_sets
; i
++) {
1689 /* std layout mask for each remap set */
1690 num_spkrs
[i
] = dca_exss_mask2count(get_bits(&s
->gb
, spkr_mask_size
));
1693 for (i
= 0; i
< spkr_remap_sets
; i
++) {
1694 int num_dec_ch_remaps
= get_bits(&s
->gb
, 5) + 1;
1695 if (get_bits_left(&s
->gb
) < 0)
1698 for (j
= 0; j
< num_spkrs
[i
]; j
++) {
1699 int remap_dec_ch_mask
= get_bits_long(&s
->gb
, num_dec_ch_remaps
);
1700 int num_dec_ch
= av_popcount(remap_dec_ch_mask
);
1701 skip_bits_long(&s
->gb
, num_dec_ch
* 5); // remap codes
1705 skip_bits(&s
->gb
, 3); // representation type
1709 drc_code_present
= get_bits1(&s
->gb
);
1710 if (drc_code_present
)
1711 get_bits(&s
->gb
, 8); // drc code
1713 if (get_bits1(&s
->gb
))
1714 skip_bits(&s
->gb
, 5); // dialog normalization code
1716 if (drc_code_present
&& embedded_stereo
)
1717 get_bits(&s
->gb
, 8); // drc stereo code
1719 if (s
->mix_metadata
&& get_bits1(&s
->gb
)) {
1720 skip_bits(&s
->gb
, 1); // external mix
1721 skip_bits(&s
->gb
, 6); // post mix gain code
1723 if (get_bits(&s
->gb
, 2) != 3) // mixer drc code
1724 skip_bits(&s
->gb
, 3); // drc limit
1726 skip_bits(&s
->gb
, 8); // custom drc code
1728 if (get_bits1(&s
->gb
)) // channel specific scaling
1729 for (i
= 0; i
< s
->num_mix_configs
; i
++)
1730 skip_bits_long(&s
->gb
, s
->mix_config_num_ch
[i
] * 6); // scale codes
1732 skip_bits_long(&s
->gb
, s
->num_mix_configs
* 6); // scale codes
1734 for (i
= 0; i
< s
->num_mix_configs
; i
++) {
1735 if (get_bits_left(&s
->gb
) < 0)
1737 dca_exss_skip_mix_coeffs(&s
->gb
, channels
, s
->mix_config_num_ch
[i
]);
1739 dca_exss_skip_mix_coeffs(&s
->gb
, 6, s
->mix_config_num_ch
[i
]);
1740 if (embedded_stereo
)
1741 dca_exss_skip_mix_coeffs(&s
->gb
, 2, s
->mix_config_num_ch
[i
]);
1745 switch (get_bits(&s
->gb
, 2)) {
1747 extensions_mask
= get_bits(&s
->gb
, 12);
1750 extensions_mask
= DCA_EXT_EXSS_XLL
;
1753 extensions_mask
= DCA_EXT_EXSS_LBR
;
1756 extensions_mask
= 0; /* aux coding */
1760 /* not parsed further, we were only interested in the extensions mask */
1762 if (get_bits_left(&s
->gb
) < 0)
1765 if (get_bits_count(&s
->gb
) - header_pos
> header_size
* 8) {
1766 av_log(s
->avctx
, AV_LOG_WARNING
, "Asset header size mismatch.\n");
1769 skip_bits_long(&s
->gb
, header_pos
+ header_size
* 8 - get_bits_count(&s
->gb
));
1771 if (extensions_mask
& DCA_EXT_EXSS_XLL
)
1772 s
->profile
= FF_PROFILE_DTS_HD_MA
;
1773 else if (extensions_mask
& (DCA_EXT_EXSS_XBR
| DCA_EXT_EXSS_X96
|
1775 s
->profile
= FF_PROFILE_DTS_HD_HRA
;
1777 if (!(extensions_mask
& DCA_EXT_CORE
))
1778 av_log(s
->avctx
, AV_LOG_WARNING
, "DTS core detection mismatch.\n");
1779 if ((extensions_mask
& DCA_CORE_EXTS
) != s
->core_ext_mask
)
1780 av_log(s
->avctx
, AV_LOG_WARNING
,
1781 "DTS extensions detection mismatch (%d, %d)\n",
1782 extensions_mask
& DCA_CORE_EXTS
, s
->core_ext_mask
);
1787 static int dca_xbr_parse_frame(DCAContext
*s
)
1789 int scale_table_high
[DCA_CHSET_CHANS_MAX
][DCA_SUBBANDS
][2];
1790 int active_bands
[DCA_CHSETS_MAX
][DCA_CHSET_CHANS_MAX
];
1791 int abits_high
[DCA_CHSET_CHANS_MAX
][DCA_SUBBANDS
];
1792 int anctemp
[DCA_CHSET_CHANS_MAX
];
1793 int chset_fsize
[DCA_CHSETS_MAX
];
1794 int n_xbr_ch
[DCA_CHSETS_MAX
];
1795 int hdr_size
, num_chsets
, xbr_tmode
, hdr_pos
;
1796 int i
, j
, k
, l
, chset
, chan_base
;
1798 av_log(s
->avctx
, AV_LOG_DEBUG
, "DTS-XBR: decoding XBR extension\n");
1800 /* get bit position of sync header */
1801 hdr_pos
= get_bits_count(&s
->gb
) - 32;
1803 hdr_size
= get_bits(&s
->gb
, 6) + 1;
1804 num_chsets
= get_bits(&s
->gb
, 2) + 1;
1806 for(i
= 0; i
< num_chsets
; i
++)
1807 chset_fsize
[i
] = get_bits(&s
->gb
, 14) + 1;
1809 xbr_tmode
= get_bits1(&s
->gb
);
1811 for(i
= 0; i
< num_chsets
; i
++) {
1812 n_xbr_ch
[i
] = get_bits(&s
->gb
, 3) + 1;
1813 k
= get_bits(&s
->gb
, 2) + 5;
1814 for(j
= 0; j
< n_xbr_ch
[i
]; j
++)
1815 active_bands
[i
][j
] = get_bits(&s
->gb
, k
) + 1;
1818 /* skip to the end of the header */
1819 i
= get_bits_count(&s
->gb
);
1820 if(hdr_pos
+ hdr_size
* 8 > i
)
1821 skip_bits_long(&s
->gb
, hdr_pos
+ hdr_size
* 8 - i
);
1823 /* loop over the channel data sets */
1824 /* only decode as many channels as we've decoded base data for */
1825 for(chset
= 0, chan_base
= 0;
1826 chset
< num_chsets
&& chan_base
+ n_xbr_ch
[chset
] <= s
->prim_channels
;
1827 chan_base
+= n_xbr_ch
[chset
++]) {
1828 int start_posn
= get_bits_count(&s
->gb
);
1829 int subsubframe
= 0;
1832 /* loop over subframes */
1833 for (k
= 0; k
< (s
->sample_blocks
/ 8); k
++) {
1834 /* parse header if we're on first subsubframe of a block */
1835 if(subsubframe
== 0) {
1836 /* Parse subframe header */
1837 for(i
= 0; i
< n_xbr_ch
[chset
]; i
++) {
1838 anctemp
[i
] = get_bits(&s
->gb
, 2) + 2;
1841 for(i
= 0; i
< n_xbr_ch
[chset
]; i
++) {
1842 get_array(&s
->gb
, abits_high
[i
], active_bands
[chset
][i
], anctemp
[i
]);
1845 for(i
= 0; i
< n_xbr_ch
[chset
]; i
++) {
1846 anctemp
[i
] = get_bits(&s
->gb
, 3);
1847 if(anctemp
[i
] < 1) {
1848 av_log(s
->avctx
, AV_LOG_ERROR
, "DTS-XBR: SYNC ERROR\n");
1849 return AVERROR_INVALIDDATA
;
1853 /* generate scale factors */
1854 for(i
= 0; i
< n_xbr_ch
[chset
]; i
++) {
1855 const uint32_t *scale_table
;
1858 if (s
->scalefactor_huffman
[chan_base
+i
] == 6) {
1859 scale_table
= scale_factor_quant7
;
1861 scale_table
= scale_factor_quant6
;
1866 for(j
= 0; j
< active_bands
[chset
][i
]; j
++) {
1867 if(abits_high
[i
][j
] > 0) {
1868 scale_table_high
[i
][j
][0] =
1869 scale_table
[get_bits(&s
->gb
, nbits
)];
1871 if(xbr_tmode
&& s
->transition_mode
[i
][j
]) {
1872 scale_table_high
[i
][j
][1] =
1873 scale_table
[get_bits(&s
->gb
, nbits
)];
1880 /* decode audio array for this block */
1881 for(i
= 0; i
< n_xbr_ch
[chset
]; i
++) {
1882 for(j
= 0; j
< active_bands
[chset
][i
]; j
++) {
1883 const int xbr_abits
= abits_high
[i
][j
];
1884 const float quant_step_size
= lossless_quant_d
[xbr_abits
];
1885 const int sfi
= xbr_tmode
&& s
->transition_mode
[i
][j
] && subsubframe
>= s
->transition_mode
[i
][j
];
1886 const float rscale
= quant_step_size
* scale_table_high
[i
][j
][sfi
];
1887 float *subband_samples
= s
->subband_samples
[k
][chan_base
+i
][j
];
1894 get_array(&s
->gb
, block
, 8, xbr_abits
- 3);
1896 int block_code1
, block_code2
, size
, levels
, err
;
1898 size
= abits_sizes
[xbr_abits
- 1];
1899 levels
= abits_levels
[xbr_abits
- 1];
1901 block_code1
= get_bits(&s
->gb
, size
);
1902 block_code2
= get_bits(&s
->gb
, size
);
1903 err
= decode_blockcodes(block_code1
, block_code2
,
1906 av_log(s
->avctx
, AV_LOG_ERROR
,
1907 "ERROR: DTS-XBR: block code look-up failed\n");
1908 return AVERROR_INVALIDDATA
;
1912 /* scale & sum into subband */
1913 for(l
= 0; l
< 8; l
++)
1914 subband_samples
[l
] += (float)block
[l
] * rscale
;
1918 /* check DSYNC marker */
1919 if(s
->aspf
|| subsubframe
== s
->subsubframes
[subframe
] - 1) {
1920 if(get_bits(&s
->gb
, 16) != 0xffff) {
1921 av_log(s
->avctx
, AV_LOG_ERROR
, "DTS-XBR: Didn't get subframe DSYNC\n");
1922 return AVERROR_INVALIDDATA
;
1926 /* advance sub-sub-frame index */
1927 if(++subsubframe
>= s
->subsubframes
[subframe
]) {
1933 /* skip to next channel set */
1934 i
= get_bits_count(&s
->gb
);
1935 if(start_posn
+ chset_fsize
[chset
] * 8 != i
) {
1936 j
= start_posn
+ chset_fsize
[chset
] * 8 - i
;
1938 av_log(s
->avctx
, AV_LOG_ERROR
, "DTS-XBR: end of channel set,"
1939 " skipping further than expected (%d bits)\n", j
);
1940 skip_bits_long(&s
->gb
, j
);
1947 /* parse initial header for XXCH and dump details */
1948 static int dca_xxch_decode_frame(DCAContext
*s
)
1950 int hdr_size
, spkmsk_bits
, num_chsets
, core_spk
, hdr_pos
;
1951 int i
, chset
, base_channel
, chstart
, fsize
[8];
1953 /* assume header word has already been parsed */
1954 hdr_pos
= get_bits_count(&s
->gb
) - 32;
1955 hdr_size
= get_bits(&s
->gb
, 6) + 1;
1956 /*chhdr_crc =*/ skip_bits1(&s
->gb
);
1957 spkmsk_bits
= get_bits(&s
->gb
, 5) + 1;
1958 num_chsets
= get_bits(&s
->gb
, 2) + 1;
1960 for (i
= 0; i
< num_chsets
; i
++)
1961 fsize
[i
] = get_bits(&s
->gb
, 14) + 1;
1963 core_spk
= get_bits(&s
->gb
, spkmsk_bits
);
1964 s
->xxch_core_spkmask
= core_spk
;
1965 s
->xxch_nbits_spk_mask
= spkmsk_bits
;
1966 s
->xxch_dmix_embedded
= 0;
1968 /* skip to the end of the header */
1969 i
= get_bits_count(&s
->gb
);
1970 if (hdr_pos
+ hdr_size
* 8 > i
)
1971 skip_bits_long(&s
->gb
, hdr_pos
+ hdr_size
* 8 - i
);
1973 for (chset
= 0; chset
< num_chsets
; chset
++) {
1974 chstart
= get_bits_count(&s
->gb
);
1975 base_channel
= s
->prim_channels
;
1976 s
->xxch_chset
= chset
;
1978 /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1979 5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1980 dca_parse_audio_coding_header(s
, base_channel
, 1);
1982 /* decode channel data */
1983 for (i
= 0; i
< (s
->sample_blocks
/ 8); i
++) {
1984 if (dca_decode_block(s
, base_channel
, i
)) {
1985 av_log(s
->avctx
, AV_LOG_ERROR
,
1986 "Error decoding DTS-XXCH extension\n");
1991 /* skip to end of this section */
1992 i
= get_bits_count(&s
->gb
);
1993 if (chstart
+ fsize
[chset
] * 8 > i
)
1994 skip_bits_long(&s
->gb
, chstart
+ fsize
[chset
] * 8 - i
);
1996 s
->xxch_chset
= num_chsets
;
2002 * Parse extension substream header (HD)
2004 static void dca_exss_parse_header(DCAContext
*s
)
2011 int active_ss_mask
[8];
2017 if (get_bits_left(&s
->gb
) < 52)
2020 start_posn
= get_bits_count(&s
->gb
) - 32;
2022 skip_bits(&s
->gb
, 8); // user data
2023 ss_index
= get_bits(&s
->gb
, 2);
2025 blownup
= get_bits1(&s
->gb
);
2026 hdrsize
= get_bits(&s
->gb
, 8 + 4 * blownup
) + 1; // header_size
2027 skip_bits(&s
->gb
, 16 + 4 * blownup
); // hd_size
2029 s
->static_fields
= get_bits1(&s
->gb
);
2030 if (s
->static_fields
) {
2031 skip_bits(&s
->gb
, 2); // reference clock code
2032 skip_bits(&s
->gb
, 3); // frame duration code
2034 if (get_bits1(&s
->gb
))
2035 skip_bits_long(&s
->gb
, 36); // timestamp
2037 /* a single stream can contain multiple audio assets that can be
2038 * combined to form multiple audio presentations */
2040 num_audiop
= get_bits(&s
->gb
, 3) + 1;
2041 if (num_audiop
> 1) {
2042 avpriv_request_sample(s
->avctx
,
2043 "Multiple DTS-HD audio presentations");
2044 /* ignore such streams for now */
2048 num_assets
= get_bits(&s
->gb
, 3) + 1;
2049 if (num_assets
> 1) {
2050 avpriv_request_sample(s
->avctx
, "Multiple DTS-HD audio assets");
2051 /* ignore such streams for now */
2055 for (i
= 0; i
< num_audiop
; i
++)
2056 active_ss_mask
[i
] = get_bits(&s
->gb
, ss_index
+ 1);
2058 for (i
= 0; i
< num_audiop
; i
++)
2059 for (j
= 0; j
<= ss_index
; j
++)
2060 if (active_ss_mask
[i
] & (1 << j
))
2061 skip_bits(&s
->gb
, 8); // active asset mask
2063 s
->mix_metadata
= get_bits1(&s
->gb
);
2064 if (s
->mix_metadata
) {
2065 int mix_out_mask_size
;
2067 skip_bits(&s
->gb
, 2); // adjustment level
2068 mix_out_mask_size
= (get_bits(&s
->gb
, 2) + 1) << 2;
2069 s
->num_mix_configs
= get_bits(&s
->gb
, 2) + 1;
2071 for (i
= 0; i
< s
->num_mix_configs
; i
++) {
2072 int mix_out_mask
= get_bits(&s
->gb
, mix_out_mask_size
);
2073 s
->mix_config_num_ch
[i
] = dca_exss_mask2count(mix_out_mask
);
2078 av_assert0(num_assets
> 0); // silence a warning
2080 for (i
= 0; i
< num_assets
; i
++)
2081 asset_size
[i
] = get_bits_long(&s
->gb
, 16 + 4 * blownup
);
2083 for (i
= 0; i
< num_assets
; i
++) {
2084 if (dca_exss_parse_asset_header(s
))
2088 /* not parsed further, we were only interested in the extensions mask
2089 * from the asset header */
2091 j
= get_bits_count(&s
->gb
);
2092 if (start_posn
+ hdrsize
* 8 > j
)
2093 skip_bits_long(&s
->gb
, start_posn
+ hdrsize
* 8 - j
);
2095 for (i
= 0; i
< num_assets
; i
++) {
2096 start_posn
= get_bits_count(&s
->gb
);
2097 mkr
= get_bits_long(&s
->gb
, 32);
2099 /* parse extensions that we know about */
2100 if (mkr
== 0x655e315e) {
2101 dca_xbr_parse_frame(s
);
2102 } else if (mkr
== 0x47004a03) {
2103 dca_xxch_decode_frame(s
);
2104 s
->core_ext_mask
|= DCA_EXT_XXCH
; /* xxx use for chan reordering */
2106 av_log(s
->avctx
, AV_LOG_DEBUG
,
2107 "DTS-ExSS: unknown marker = 0x%08x\n", mkr
);
2110 /* skip to end of block */
2111 j
= get_bits_count(&s
->gb
);
2112 if (start_posn
+ asset_size
[i
] * 8 > j
)
2113 skip_bits_long(&s
->gb
, start_posn
+ asset_size
[i
] * 8 - j
);
2117 static float dca_dmix_code(unsigned code
)
2119 int sign
= (code
>> 8) - 1;
2121 return ((dca_dmixtable
[code
] ^ sign
) - sign
) * (1.0 / (1 << 15));
2125 * Main frame decoding function
2126 * FIXME add arguments
2128 static int dca_decode_frame(AVCodecContext
*avctx
, void *data
,
2129 int *got_frame_ptr
, AVPacket
*avpkt
)
2131 AVFrame
*frame
= data
;
2132 const uint8_t *buf
= avpkt
->data
;
2133 int buf_size
= avpkt
->size
;
2137 int num_core_channels
= 0;
2139 float **samples_flt
;
2142 DCAContext
*s
= avctx
->priv_data
;
2144 int channels
, full_channels
;
2156 s
->dca_buffer_size
= avpriv_dca_convert_bitstream(buf
, buf_size
, s
->dca_buffer
,
2157 DCA_MAX_FRAME_SIZE
+ DCA_MAX_EXSS_HEADER_SIZE
);
2158 if (s
->dca_buffer_size
== AVERROR_INVALIDDATA
) {
2159 av_log(avctx
, AV_LOG_ERROR
, "Not a valid DCA frame\n");
2160 return AVERROR_INVALIDDATA
;
2163 if ((ret
= dca_parse_frame_header(s
)) < 0) {
2164 // seems like the frame is corrupt, try with the next one
2167 // set AVCodec values with parsed data
2168 avctx
->sample_rate
= s
->sample_rate
;
2169 avctx
->bit_rate
= s
->bit_rate
;
2171 s
->profile
= FF_PROFILE_DTS
;
2173 for (i
= 0; i
< (s
->sample_blocks
/ 8); i
++) {
2174 if ((ret
= dca_decode_block(s
, 0, i
))) {
2175 av_log(avctx
, AV_LOG_ERROR
, "error decoding block\n");
2180 /* record number of core channels incase less than max channels are requested */
2181 num_core_channels
= s
->prim_channels
;
2183 if (s
->prim_channels
+ !!s
->lfe
> 2 &&
2184 avctx
->request_channel_layout
== AV_CH_LAYOUT_STEREO
) {
2185 /* Stereo downmix coefficients
2187 * The decoder can only downmix to 2-channel, so we need to ensure
2188 * embedded downmix coefficients are actually targeting 2-channel.
2190 if (s
->core_downmix
&& (s
->core_downmix_amode
== DCA_STEREO
||
2191 s
->core_downmix_amode
== DCA_STEREO_TOTAL
)) {
2192 for (i
= 0; i
< num_core_channels
+ !!s
->lfe
; i
++) {
2193 /* Range checked earlier */
2194 s
->downmix_coef
[i
][0] = dca_dmix_code(s
->core_downmix_codes
[i
][0]);
2195 s
->downmix_coef
[i
][1] = dca_dmix_code(s
->core_downmix_codes
[i
][1]);
2197 s
->output
= s
->core_downmix_amode
;
2199 int am
= s
->amode
& DCA_CHANNEL_MASK
;
2200 if (am
>= FF_ARRAY_ELEMS(dca_default_coeffs
)) {
2201 av_log(s
->avctx
, AV_LOG_ERROR
,
2202 "Invalid channel mode %d\n", am
);
2203 return AVERROR_INVALIDDATA
;
2205 if (num_core_channels
+ !!s
->lfe
>
2206 FF_ARRAY_ELEMS(dca_default_coeffs
[0])) {
2207 avpriv_request_sample(s
->avctx
, "Downmixing %d channels",
2208 s
->prim_channels
+ !!s
->lfe
);
2209 return AVERROR_PATCHWELCOME
;
2211 for (i
= 0; i
< num_core_channels
+ !!s
->lfe
; i
++) {
2212 s
->downmix_coef
[i
][0] = dca_default_coeffs
[am
][i
][0];
2213 s
->downmix_coef
[i
][1] = dca_default_coeffs
[am
][i
][1];
2216 av_dlog(s
->avctx
, "Stereo downmix coeffs:\n");
2217 for (i
= 0; i
< num_core_channels
+ !!s
->lfe
; i
++) {
2218 av_dlog(s
->avctx
, "L, input channel %d = %f\n", i
,
2219 s
->downmix_coef
[i
][0]);
2220 av_dlog(s
->avctx
, "R, input channel %d = %f\n", i
,
2221 s
->downmix_coef
[i
][1]);
2223 av_dlog(s
->avctx
, "\n");
2227 s
->core_ext_mask
= dca_ext_audio_descr_mask
[s
->ext_descr
];
2229 s
->core_ext_mask
= 0;
2231 core_ss_end
= FFMIN(s
->frame_size
, s
->dca_buffer_size
) * 8;
2233 /* only scan for extensions if ext_descr was unknown or indicated a
2234 * supported XCh extension */
2235 if (s
->core_ext_mask
< 0 || s
->core_ext_mask
& (DCA_EXT_XCH
| DCA_EXT_XXCH
)) {
2236 /* if ext_descr was unknown, clear s->core_ext_mask so that the
2237 * extensions scan can fill it up */
2238 s
->core_ext_mask
= FFMAX(s
->core_ext_mask
, 0);
2240 /* extensions start at 32-bit boundaries into bitstream */
2241 skip_bits_long(&s
->gb
, (-get_bits_count(&s
->gb
)) & 31);
2243 while (core_ss_end
- get_bits_count(&s
->gb
) >= 32) {
2244 uint32_t bits
= get_bits_long(&s
->gb
, 32);
2248 int ext_amode
, xch_fsize
;
2250 s
->xch_base_channel
= s
->prim_channels
;
2252 /* validate sync word using XCHFSIZE field */
2253 xch_fsize
= show_bits(&s
->gb
, 10);
2254 if ((s
->frame_size
!= (get_bits_count(&s
->gb
) >> 3) - 4 + xch_fsize
) &&
2255 (s
->frame_size
!= (get_bits_count(&s
->gb
) >> 3) - 4 + xch_fsize
+ 1))
2258 /* skip length-to-end-of-frame field for the moment */
2259 skip_bits(&s
->gb
, 10);
2261 s
->core_ext_mask
|= DCA_EXT_XCH
;
2263 /* extension amode(number of channels in extension) should be 1 */
2264 /* AFAIK XCh is not used for more channels */
2265 if ((ext_amode
= get_bits(&s
->gb
, 4)) != 1) {
2266 av_log(avctx
, AV_LOG_ERROR
,
2267 "XCh extension amode %d not supported!\n",
2272 if (s
->xch_base_channel
< 2) {
2273 avpriv_request_sample(avctx
, "XCh with fewer than 2 base channels");
2277 /* much like core primary audio coding header */
2278 dca_parse_audio_coding_header(s
, s
->xch_base_channel
, 0);
2280 for (i
= 0; i
< (s
->sample_blocks
/ 8); i
++)
2281 if ((ret
= dca_decode_block(s
, s
->xch_base_channel
, i
))) {
2282 av_log(avctx
, AV_LOG_ERROR
, "error decoding XCh extension\n");
2290 /* XXCh: extended channels */
2291 /* usually found either in core or HD part in DTS-HD HRA streams,
2292 * but not in DTS-ES which contains XCh extensions instead */
2293 s
->core_ext_mask
|= DCA_EXT_XXCH
;
2294 dca_xxch_decode_frame(s
);
2298 int fsize96
= show_bits(&s
->gb
, 12) + 1;
2299 if (s
->frame_size
!= (get_bits_count(&s
->gb
) >> 3) - 4 + fsize96
)
2302 av_log(avctx
, AV_LOG_DEBUG
, "X96 extension found at %d bits\n",
2303 get_bits_count(&s
->gb
));
2304 skip_bits(&s
->gb
, 12);
2305 av_log(avctx
, AV_LOG_DEBUG
, "FSIZE96 = %d bytes\n", fsize96
);
2306 av_log(avctx
, AV_LOG_DEBUG
, "REVNO = %d\n", get_bits(&s
->gb
, 4));
2308 s
->core_ext_mask
|= DCA_EXT_X96
;
2313 skip_bits_long(&s
->gb
, (-get_bits_count(&s
->gb
)) & 31);
2316 /* no supported extensions, skip the rest of the core substream */
2317 skip_bits_long(&s
->gb
, core_ss_end
- get_bits_count(&s
->gb
));
2320 if (s
->core_ext_mask
& DCA_EXT_X96
)
2321 s
->profile
= FF_PROFILE_DTS_96_24
;
2322 else if (s
->core_ext_mask
& (DCA_EXT_XCH
| DCA_EXT_XXCH
))
2323 s
->profile
= FF_PROFILE_DTS_ES
;
2325 /* check for ExSS (HD part) */
2326 if (s
->dca_buffer_size
- s
->frame_size
> 32 &&
2327 get_bits_long(&s
->gb
, 32) == DCA_HD_MARKER
)
2328 dca_exss_parse_header(s
);
2330 avctx
->profile
= s
->profile
;
2332 full_channels
= channels
= s
->prim_channels
+ !!s
->lfe
;
2334 /* If we have XXCH then the channel layout is managed differently */
2335 /* note that XLL will also have another way to do things */
2336 if (!(s
->core_ext_mask
& DCA_EXT_XXCH
)
2337 || (s
->core_ext_mask
& DCA_EXT_XXCH
&& avctx
->request_channels
> 0
2338 && avctx
->request_channels
2339 < num_core_channels
+ !!s
->lfe
+ s
->xxch_chset_nch
[0]))
2340 { /* xxx should also do MA extensions */
2341 if (s
->amode
< 16) {
2342 avctx
->channel_layout
= dca_core_channel_layout
[s
->amode
];
2344 if (s
->prim_channels
+ !!s
->lfe
> 2 &&
2345 avctx
->request_channel_layout
== AV_CH_LAYOUT_STEREO
) {
2347 * Neither the core's auxiliary data nor our default tables contain
2348 * downmix coefficients for the additional channel coded in the XCh
2349 * extension, so when we're doing a Stereo downmix, don't decode it.
2354 #if FF_API_REQUEST_CHANNELS
2355 FF_DISABLE_DEPRECATION_WARNINGS
2356 if (s
->xch_present
&& !s
->xch_disable
&&
2357 (!avctx
->request_channels
||
2358 avctx
->request_channels
> num_core_channels
+ !!s
->lfe
)) {
2359 FF_ENABLE_DEPRECATION_WARNINGS
2361 if (s
->xch_present
&& !s
->xch_disable
) {
2363 if (avctx
->channel_layout
& AV_CH_BACK_CENTER
) {
2364 avpriv_request_sample(avctx
, "XCh with Back center channel");
2365 return AVERROR_INVALIDDATA
;
2367 avctx
->channel_layout
|= AV_CH_BACK_CENTER
;
2369 avctx
->channel_layout
|= AV_CH_LOW_FREQUENCY
;
2370 s
->channel_order_tab
= dca_channel_reorder_lfe_xch
[s
->amode
];
2372 s
->channel_order_tab
= dca_channel_reorder_nolfe_xch
[s
->amode
];
2374 if (s
->channel_order_tab
[s
->xch_base_channel
] < 0)
2375 return AVERROR_INVALIDDATA
;
2377 channels
= num_core_channels
+ !!s
->lfe
;
2378 s
->xch_present
= 0; /* disable further xch processing */
2380 avctx
->channel_layout
|= AV_CH_LOW_FREQUENCY
;
2381 s
->channel_order_tab
= dca_channel_reorder_lfe
[s
->amode
];
2383 s
->channel_order_tab
= dca_channel_reorder_nolfe
[s
->amode
];
2386 if (channels
> !!s
->lfe
&&
2387 s
->channel_order_tab
[channels
- 1 - !!s
->lfe
] < 0)
2388 return AVERROR_INVALIDDATA
;
2390 if (av_get_channel_layout_nb_channels(avctx
->channel_layout
) != channels
) {
2391 av_log(avctx
, AV_LOG_ERROR
, "Number of channels %d mismatches layout %d\n", channels
, av_get_channel_layout_nb_channels(avctx
->channel_layout
));
2392 return AVERROR_INVALIDDATA
;
2395 if (num_core_channels
+ !!s
->lfe
> 2 &&
2396 avctx
->request_channel_layout
== AV_CH_LAYOUT_STEREO
) {
2398 s
->output
= s
->prim_channels
== 2 ? s
->amode
: DCA_STEREO
;
2399 avctx
->channel_layout
= AV_CH_LAYOUT_STEREO
;
2401 else if (avctx
->request_channel_layout
& AV_CH_LAYOUT_NATIVE
) {
2402 static const int8_t dca_channel_order_native
[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2403 s
->channel_order_tab
= dca_channel_order_native
;
2405 s
->lfe_index
= dca_lfe_index
[s
->amode
];
2407 av_log(avctx
, AV_LOG_ERROR
,
2408 "Non standard configuration %d !\n", s
->amode
);
2409 return AVERROR_INVALIDDATA
;
2412 s
->xxch_dmix_embedded
= 0;
2414 /* we only get here if an XXCH channel set can be added to the mix */
2415 channel_mask
= s
->xxch_core_spkmask
;
2417 if (avctx
->request_channels
> 0
2418 && avctx
->request_channels
< s
->prim_channels
) {
2419 channels
= num_core_channels
+ !!s
->lfe
;
2420 for (i
= 0; i
< s
->xxch_chset
&& channels
+ s
->xxch_chset_nch
[i
]
2421 <= avctx
->request_channels
; i
++) {
2422 channels
+= s
->xxch_chset_nch
[i
];
2423 channel_mask
|= s
->xxch_spk_masks
[i
];
2426 channels
= s
->prim_channels
+ !!s
->lfe
;
2427 for (i
= 0; i
< s
->xxch_chset
; i
++) {
2428 channel_mask
|= s
->xxch_spk_masks
[i
];
2432 /* Given the DTS spec'ed channel mask, generate an avcodec version */
2434 for (i
= 0; i
< s
->xxch_nbits_spk_mask
; ++i
) {
2435 if (channel_mask
& (1 << i
)) {
2436 channel_layout
|= map_xxch_to_native
[i
];
2440 /* make sure that we have managed to get equivalent dts/avcodec channel
2441 * masks in some sense -- unfortunately some channels could overlap */
2442 if (av_popcount(channel_mask
) != av_popcount(channel_layout
)) {
2443 av_log(avctx
, AV_LOG_DEBUG
,
2444 "DTS-XXCH: Inconsistent avcodec/dts channel layouts\n");
2445 return AVERROR_INVALIDDATA
;
2448 avctx
->channel_layout
= channel_layout
;
2450 if (!(avctx
->request_channel_layout
& AV_CH_LAYOUT_NATIVE
)) {
2451 /* Estimate DTS --> avcodec ordering table */
2452 for (chset
= -1, j
= 0; chset
< s
->xxch_chset
; ++chset
) {
2453 mask
= chset
>= 0 ? s
->xxch_spk_masks
[chset
]
2454 : s
->xxch_core_spkmask
;
2455 for (i
= 0; i
< s
->xxch_nbits_spk_mask
; i
++) {
2456 if (mask
& ~(DCA_XXCH_LFE1
| DCA_XXCH_LFE2
) & (1 << i
)) {
2457 lavc
= map_xxch_to_native
[i
];
2458 posn
= av_popcount(channel_layout
& (lavc
- 1));
2459 s
->xxch_order_tab
[j
++] = posn
;
2465 s
->lfe_index
= av_popcount(channel_layout
& (AV_CH_LOW_FREQUENCY
-1));
2466 } else { /* native ordering */
2467 for (i
= 0; i
< channels
; i
++)
2468 s
->xxch_order_tab
[i
] = i
;
2470 s
->lfe_index
= channels
- 1;
2473 s
->channel_order_tab
= s
->xxch_order_tab
;
2476 if (avctx
->channels
!= channels
) {
2477 if (avctx
->channels
)
2478 av_log(avctx
, AV_LOG_INFO
, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx
->channels
, channels
);
2479 avctx
->channels
= channels
;
2482 /* get output buffer */
2483 frame
->nb_samples
= 256 * (s
->sample_blocks
/ 8);
2484 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
2486 samples_flt
= (float **) frame
->extended_data
;
2488 /* allocate buffer for extra channels if downmixing */
2489 if (avctx
->channels
< full_channels
) {
2490 ret
= av_samples_get_buffer_size(NULL
, full_channels
- channels
,
2492 avctx
->sample_fmt
, 0);
2496 av_fast_malloc(&s
->extra_channels_buffer
,
2497 &s
->extra_channels_buffer_size
, ret
);
2498 if (!s
->extra_channels_buffer
)
2499 return AVERROR(ENOMEM
);
2501 ret
= av_samples_fill_arrays((uint8_t **) s
->extra_channels
, NULL
,
2502 s
->extra_channels_buffer
,
2503 full_channels
- channels
,
2504 frame
->nb_samples
, avctx
->sample_fmt
, 0);
2509 /* filter to get final output */
2510 for (i
= 0; i
< (s
->sample_blocks
/ 8); i
++) {
2513 for (ch
= 0; ch
< channels
; ch
++)
2514 s
->samples_chanptr
[ch
] = samples_flt
[ch
] + i
* 256;
2515 for (; ch
< full_channels
; ch
++)
2516 s
->samples_chanptr
[ch
] = s
->extra_channels
[ch
- channels
] + i
* 256;
2518 dca_filter_channels(s
, i
);
2520 /* If this was marked as a DTS-ES stream we need to subtract back- */
2521 /* channel from SL & SR to remove matrixed back-channel signal */
2522 if ((s
->source_pcm_res
& 1) && s
->xch_present
) {
2523 float *back_chan
= s
->samples_chanptr
[s
->channel_order_tab
[s
->xch_base_channel
]];
2524 float *lt_chan
= s
->samples_chanptr
[s
->channel_order_tab
[s
->xch_base_channel
- 2]];
2525 float *rt_chan
= s
->samples_chanptr
[s
->channel_order_tab
[s
->xch_base_channel
- 1]];
2526 s
->fdsp
->vector_fmac_scalar(lt_chan
, back_chan
, -M_SQRT1_2
, 256);
2527 s
->fdsp
->vector_fmac_scalar(rt_chan
, back_chan
, -M_SQRT1_2
, 256);
2530 /* If stream contains XXCH, we might need to undo an embedded downmix */
2531 if (s
->xxch_dmix_embedded
) {
2532 /* Loop over channel sets in turn */
2533 ch
= num_core_channels
;
2534 for (chset
= 0; chset
< s
->xxch_chset
; chset
++) {
2535 endch
= ch
+ s
->xxch_chset_nch
[chset
];
2536 mask
= s
->xxch_dmix_embedded
;
2539 for (j
= ch
; j
< endch
; j
++) {
2540 if (mask
& (1 << j
)) { /* this channel has been mixed-out */
2541 src_chan
= s
->samples_chanptr
[s
->channel_order_tab
[j
]];
2542 for (k
= 0; k
< endch
; k
++) {
2543 achan
= s
->channel_order_tab
[k
];
2544 scale
= s
->xxch_dmix_coeff
[j
][k
];
2546 dst_chan
= s
->samples_chanptr
[achan
];
2547 s
->fdsp
->vector_fmac_scalar(dst_chan
, src_chan
,
2554 /* if a downmix has been embedded then undo the pre-scaling */
2555 if ((mask
& (1 << ch
)) && s
->xxch_dmix_sf
[chset
] != 1.0f
) {
2556 scale
= s
->xxch_dmix_sf
[chset
];
2558 for (j
= 0; j
< ch
; j
++) {
2559 src_chan
= s
->samples_chanptr
[s
->channel_order_tab
[j
]];
2560 for (k
= 0; k
< 256; k
++)
2561 src_chan
[k
] *= scale
;
2564 /* LFE channel is always part of core, scale if it exists */
2566 src_chan
= s
->samples_chanptr
[s
->lfe_index
];
2567 for (k
= 0; k
< 256; k
++)
2568 src_chan
[k
] *= scale
;
2578 /* update lfe history */
2579 lfe_samples
= 2 * s
->lfe
* (s
->sample_blocks
/ 8);
2580 for (i
= 0; i
< 2 * s
->lfe
* 4; i
++)
2581 s
->lfe_data
[i
] = s
->lfe_data
[i
+ lfe_samples
];
2585 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
2586 ret
= ff_side_data_update_matrix_encoding(frame
,
2587 (s
->output
& ~DCA_LFE
) == DCA_STEREO_TOTAL
?
2588 AV_MATRIX_ENCODING_DOLBY
: AV_MATRIX_ENCODING_NONE
);
2598 * DCA initialization
2600 * @param avctx pointer to the AVCodecContext
2603 static av_cold
int dca_decode_init(AVCodecContext
*avctx
)
2605 DCAContext
*s
= avctx
->priv_data
;
2610 s
->fdsp
= avpriv_float_dsp_alloc(avctx
->flags
& CODEC_FLAG_BITEXACT
);
2612 return AVERROR(ENOMEM
);
2614 ff_mdct_init(&s
->imdct
, 6, 1, 1.0);
2615 ff_synth_filter_init(&s
->synth
);
2616 ff_dcadsp_init(&s
->dcadsp
);
2617 ff_fmt_convert_init(&s
->fmt_conv
, avctx
);
2619 avctx
->sample_fmt
= AV_SAMPLE_FMT_FLTP
;
2621 /* allow downmixing to stereo */
2622 #if FF_API_REQUEST_CHANNELS
2623 FF_DISABLE_DEPRECATION_WARNINGS
2624 if (avctx
->request_channels
== 2)
2625 avctx
->request_channel_layout
= AV_CH_LAYOUT_STEREO
;
2626 FF_ENABLE_DEPRECATION_WARNINGS
2628 if (avctx
->channels
> 2 &&
2629 avctx
->request_channel_layout
== AV_CH_LAYOUT_STEREO
)
2630 avctx
->channels
= 2;
2635 static av_cold
int dca_decode_end(AVCodecContext
*avctx
)
2637 DCAContext
*s
= avctx
->priv_data
;
2638 ff_mdct_end(&s
->imdct
);
2639 av_freep(&s
->extra_channels_buffer
);
2644 static const AVProfile profiles
[] = {
2645 { FF_PROFILE_DTS
, "DTS" },
2646 { FF_PROFILE_DTS_ES
, "DTS-ES" },
2647 { FF_PROFILE_DTS_96_24
, "DTS 96/24" },
2648 { FF_PROFILE_DTS_HD_HRA
, "DTS-HD HRA" },
2649 { FF_PROFILE_DTS_HD_MA
, "DTS-HD MA" },
2650 { FF_PROFILE_UNKNOWN
},
2653 static const AVOption options
[] = {
2654 { "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
},
2658 static const AVClass dca_decoder_class
= {
2659 .class_name
= "DCA decoder",
2660 .item_name
= av_default_item_name
,
2662 .version
= LIBAVUTIL_VERSION_INT
,
2663 .category
= AV_CLASS_CATEGORY_DECODER
,
2666 AVCodec ff_dca_decoder
= {
2668 .long_name
= NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2669 .type
= AVMEDIA_TYPE_AUDIO
,
2670 .id
= AV_CODEC_ID_DTS
,
2671 .priv_data_size
= sizeof(DCAContext
),
2672 .init
= dca_decode_init
,
2673 .decode
= dca_decode_frame
,
2674 .close
= dca_decode_end
,
2675 .capabilities
= CODEC_CAP_CHANNEL_CONF
| CODEC_CAP_DR1
,
2676 .sample_fmts
= (const enum AVSampleFormat
[]) { AV_SAMPLE_FMT_FLTP
,
2677 AV_SAMPLE_FMT_NONE
},
2678 .profiles
= NULL_IF_CONFIG_SMALL(profiles
),
2679 .priv_class
= &dca_decoder_class
,