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/intmath.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/samplefmt.h"
44 #include "dca_parser.h"
45 #include "synth_filter.h"
47 #include "fmtconvert.h"
55 #define DCA_PRIM_CHANNELS_MAX (7)
56 #define DCA_SUBBANDS (64)
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 /** Bit allocation */
331 int offset; ///< code values offset
332 int maxbits[8]; ///< max bits in VLC
333 int wrap; ///< wrap for get_vlc2()
334 VLC vlc[8]; ///< actual codes
337 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
338 static BitAlloc dca_tmode; ///< transition mode VLCs
339 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
340 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
342 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
345 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
350 AVCodecContext *avctx;
353 int frame_type; ///< type of the current frame
354 int samples_deficit; ///< deficit sample count
355 int crc_present; ///< crc is present in the bitstream
356 int sample_blocks; ///< number of PCM sample blocks
357 int frame_size; ///< primary frame byte size
358 int amode; ///< audio channels arrangement
359 int sample_rate; ///< audio sampling rate
360 int bit_rate; ///< transmission bit rate
361 int bit_rate_index; ///< transmission bit rate index
363 int downmix; ///< embedded downmix enabled
364 int dynrange; ///< embedded dynamic range flag
365 int timestamp; ///< embedded time stamp flag
366 int aux_data; ///< auxiliary data flag
367 int hdcd; ///< source material is mastered in HDCD
368 int ext_descr; ///< extension audio descriptor flag
369 int ext_coding; ///< extended coding flag
370 int aspf; ///< audio sync word insertion flag
371 int lfe; ///< low frequency effects flag
372 int predictor_history; ///< predictor history flag
373 int header_crc; ///< header crc check bytes
374 int multirate_inter; ///< multirate interpolator switch
375 int version; ///< encoder software revision
376 int copy_history; ///< copy history
377 int source_pcm_res; ///< source pcm resolution
378 int front_sum; ///< front sum/difference flag
379 int surround_sum; ///< surround sum/difference flag
380 int dialog_norm; ///< dialog normalisation parameter
382 /* Primary audio coding header */
383 int subframes; ///< number of subframes
384 int total_channels; ///< number of channels including extensions
385 int prim_channels; ///< number of primary audio channels
386 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
387 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
388 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
389 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
390 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
391 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
392 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
393 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
395 /* Primary audio coding side information */
396 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
397 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
398 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
399 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
400 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
401 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
402 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
403 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
404 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
405 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
406 int dynrange_coef; ///< dynamic range coefficient
408 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
410 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
411 int lfe_scale_factor;
413 /* Subband samples history (for ADPCM) */
414 DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
415 DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
416 DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
417 int hist_index[DCA_PRIM_CHANNELS_MAX];
418 DECLARE_ALIGNED(32, float, raXin)[32];
420 int output; ///< type of output
422 DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
423 float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
424 float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
425 uint8_t *extra_channels_buffer;
426 unsigned int extra_channels_buffer_size;
428 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
429 int dca_buffer_size; ///< how much data is in the dca_buffer
431 const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
433 /* Current position in DCA frame */
434 int current_subframe;
435 int current_subsubframe;
437 int core_ext_mask; ///< present extensions in the core substream
439 /* XCh extension information */
440 int xch_present; ///< XCh extension present and valid
441 int xch_base_channel; ///< index of first (only) channel containing XCH data
443 /* XXCH extension information */
445 int xxch_nbits_spk_mask;
446 uint32_t xxch_core_spkmask;
447 uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
448 int xxch_chset_nch[4];
449 float xxch_dmix_sf[DCA_CHSETS_MAX];
451 uint32_t xxch_dmix_embedded; /* lower layer has mix pre-embedded, per chset */
452 float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
454 int8_t xxch_order_tab[32];
457 /* ExSS header parser */
458 int static_fields; ///< static fields present
459 int mix_metadata; ///< mixing metadata present
460 int num_mix_configs; ///< number of mix out configurations
461 int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
465 int debug_flag; ///< used for suppressing repeated error messages output
466 AVFloatDSPContext fdsp;
468 SynthFilterContext synth;
469 DCADSPContext dcadsp;
470 FmtConvertContext fmt_conv;
473 static const uint16_t dca_vlc_offs[] = {
474 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
475 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
476 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
477 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
478 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
479 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
482 static av_cold void dca_init_vlcs(void)
484 static int vlcs_initialized = 0;
486 static VLC_TYPE dca_table[23622][2];
488 if (vlcs_initialized)
491 dca_bitalloc_index.offset = 1;
492 dca_bitalloc_index.wrap = 2;
493 for (i = 0; i < 5; i++) {
494 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
495 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
496 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
497 bitalloc_12_bits[i], 1, 1,
498 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
500 dca_scalefactor.offset = -64;
501 dca_scalefactor.wrap = 2;
502 for (i = 0; i < 5; i++) {
503 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
504 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
505 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
506 scales_bits[i], 1, 1,
507 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
509 dca_tmode.offset = 0;
511 for (i = 0; i < 4; i++) {
512 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
513 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
514 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
516 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
519 for (i = 0; i < 10; i++)
520 for (j = 0; j < 7; j++) {
521 if (!bitalloc_codes[i][j])
523 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
524 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
525 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
526 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
528 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
530 bitalloc_bits[i][j], 1, 1,
531 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
534 vlcs_initialized = 1;
537 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
540 *dst++ = get_bits(gb, bits);
543 static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
547 /* locate channel set containing the channel */
548 for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
549 i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
550 base += av_popcount(mask);
552 return base + av_popcount(mask & (xxch_ch - 1));
555 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
559 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
560 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
561 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
562 int hdr_pos = 0, hdr_size = 0;
563 float sign, mag, scale_factor;
564 int this_chans, acc_mask;
565 int embedded_downmix;
569 /* xxch has arbitrary sized audio coding headers */
571 hdr_pos = get_bits_count(&s->gb);
572 hdr_size = get_bits(&s->gb, 7) + 1;
575 nchans = get_bits(&s->gb, 3) + 1;
576 s->total_channels = nchans + base_channel;
577 s->prim_channels = s->total_channels;
579 /* obtain speaker layout mask & downmix coefficients for XXCH */
581 acc_mask = s->xxch_core_spkmask;
583 this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
584 s->xxch_spk_masks[s->xxch_chset] = this_chans;
585 s->xxch_chset_nch[s->xxch_chset] = nchans;
587 for (i = 0; i <= s->xxch_chset; i++)
588 acc_mask |= s->xxch_spk_masks[i];
590 /* check for downmixing information */
591 if (get_bits1(&s->gb)) {
592 embedded_downmix = get_bits1(&s->gb);
594 1.0f / dca_downmix_scale_factors[(get_bits(&s->gb, 6) - 1) << 2];
596 s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
598 for (i = base_channel; i < s->prim_channels; i++) {
599 mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
602 for (j = base_channel; j < s->prim_channels; j++) {
603 memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
604 s->xxch_dmix_embedded |= (embedded_downmix << j);
605 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
606 if (mask[j] & (1 << i)) {
607 if ((1 << i) == DCA_XXCH_LFE1) {
608 av_log(s->avctx, AV_LOG_WARNING,
609 "DCA-XXCH: dmix to LFE1 not supported.\n");
613 coeff = get_bits(&s->gb, 7);
614 sign = (coeff & 64) ? 1.0 : -1.0;
615 mag = dca_downmix_scale_factors[((coeff & 63) - 1) << 2];
616 ichan = dca_xxch2index(s, 1 << i);
617 s->xxch_dmix_coeff[j][ichan] = sign * mag;
624 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
625 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
628 for (i = base_channel; i < s->prim_channels; i++) {
629 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
630 if (s->subband_activity[i] > DCA_SUBBANDS)
631 s->subband_activity[i] = DCA_SUBBANDS;
633 for (i = base_channel; i < s->prim_channels; i++) {
634 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
635 if (s->vq_start_subband[i] > DCA_SUBBANDS)
636 s->vq_start_subband[i] = DCA_SUBBANDS;
638 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
639 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
640 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
641 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
643 /* Get codebooks quantization indexes */
645 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
646 for (j = 1; j < 11; j++)
647 for (i = base_channel; i < s->prim_channels; i++)
648 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
650 /* Get scale factor adjustment */
651 for (j = 0; j < 11; j++)
652 for (i = base_channel; i < s->prim_channels; i++)
653 s->scalefactor_adj[i][j] = 1;
655 for (j = 1; j < 11; j++)
656 for (i = base_channel; i < s->prim_channels; i++)
657 if (s->quant_index_huffman[i][j] < thr[j])
658 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
661 if (s->crc_present) {
662 /* Audio header CRC check */
663 get_bits(&s->gb, 16);
666 /* Skip to the end of the header, also ignore CRC if present */
667 i = get_bits_count(&s->gb);
668 if (hdr_pos + 8 * hdr_size > i)
669 skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
672 s->current_subframe = 0;
673 s->current_subsubframe = 0;
676 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
677 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
678 for (i = base_channel; i < s->prim_channels; i++) {
679 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
680 s->subband_activity[i]);
681 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
682 s->vq_start_subband[i]);
683 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
684 s->joint_intensity[i]);
685 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
686 s->transient_huffman[i]);
687 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
688 s->scalefactor_huffman[i]);
689 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
690 s->bitalloc_huffman[i]);
691 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
692 for (j = 0; j < 11; j++)
693 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
694 av_log(s->avctx, AV_LOG_DEBUG, "\n");
695 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
696 for (j = 0; j < 11; j++)
697 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
698 av_log(s->avctx, AV_LOG_DEBUG, "\n");
705 static int dca_parse_frame_header(DCAContext *s)
707 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
710 skip_bits_long(&s->gb, 32);
713 s->frame_type = get_bits(&s->gb, 1);
714 s->samples_deficit = get_bits(&s->gb, 5) + 1;
715 s->crc_present = get_bits(&s->gb, 1);
716 s->sample_blocks = get_bits(&s->gb, 7) + 1;
717 s->frame_size = get_bits(&s->gb, 14) + 1;
718 if (s->frame_size < 95)
719 return AVERROR_INVALIDDATA;
720 s->amode = get_bits(&s->gb, 6);
721 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
723 return AVERROR_INVALIDDATA;
724 s->bit_rate_index = get_bits(&s->gb, 5);
725 s->bit_rate = dca_bit_rates[s->bit_rate_index];
727 return AVERROR_INVALIDDATA;
729 s->downmix = get_bits(&s->gb, 1); /* note: this is FixedBit == 0 */
730 s->dynrange = get_bits(&s->gb, 1);
731 s->timestamp = get_bits(&s->gb, 1);
732 s->aux_data = get_bits(&s->gb, 1);
733 s->hdcd = get_bits(&s->gb, 1);
734 s->ext_descr = get_bits(&s->gb, 3);
735 s->ext_coding = get_bits(&s->gb, 1);
736 s->aspf = get_bits(&s->gb, 1);
737 s->lfe = get_bits(&s->gb, 2);
738 s->predictor_history = get_bits(&s->gb, 1);
740 /* TODO: check CRC */
742 s->header_crc = get_bits(&s->gb, 16);
744 s->multirate_inter = get_bits(&s->gb, 1);
745 s->version = get_bits(&s->gb, 4);
746 s->copy_history = get_bits(&s->gb, 2);
747 s->source_pcm_res = get_bits(&s->gb, 3);
748 s->front_sum = get_bits(&s->gb, 1);
749 s->surround_sum = get_bits(&s->gb, 1);
750 s->dialog_norm = get_bits(&s->gb, 4);
752 /* FIXME: channels mixing levels */
753 s->output = s->amode;
755 s->output |= DCA_LFE;
758 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
759 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
760 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
761 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
762 s->sample_blocks, s->sample_blocks * 32);
763 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
764 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
765 s->amode, dca_channels[s->amode]);
766 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
768 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
770 av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
771 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
772 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
773 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
774 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
775 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
776 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
777 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
778 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
779 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
780 s->predictor_history);
781 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
782 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
784 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
785 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
786 av_log(s->avctx, AV_LOG_DEBUG,
787 "source pcm resolution: %i (%i bits/sample)\n",
788 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
789 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
790 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
791 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
792 av_log(s->avctx, AV_LOG_DEBUG, "\n");
795 /* Primary audio coding header */
796 s->subframes = get_bits(&s->gb, 4) + 1;
798 return dca_parse_audio_coding_header(s, 0, 0);
802 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
805 /* huffman encoded */
806 value += get_bitalloc(gb, &dca_scalefactor, level);
807 value = av_clip(value, 0, (1 << log2range) - 1);
808 } else if (level < 8) {
809 if (level + 1 > log2range) {
810 skip_bits(gb, level + 1 - log2range);
811 value = get_bits(gb, log2range);
813 value = get_bits(gb, level + 1);
819 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
821 /* Primary audio coding side information */
824 if (get_bits_left(&s->gb) < 0)
825 return AVERROR_INVALIDDATA;
828 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
829 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
832 for (j = base_channel; j < s->prim_channels; j++) {
833 for (k = 0; k < s->subband_activity[j]; k++)
834 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
837 /* Get prediction codebook */
838 for (j = base_channel; j < s->prim_channels; j++) {
839 for (k = 0; k < s->subband_activity[j]; k++) {
840 if (s->prediction_mode[j][k] > 0) {
841 /* (Prediction coefficient VQ address) */
842 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
847 /* Bit allocation index */
848 for (j = base_channel; j < s->prim_channels; j++) {
849 for (k = 0; k < s->vq_start_subband[j]; k++) {
850 if (s->bitalloc_huffman[j] == 6)
851 s->bitalloc[j][k] = get_bits(&s->gb, 5);
852 else if (s->bitalloc_huffman[j] == 5)
853 s->bitalloc[j][k] = get_bits(&s->gb, 4);
854 else if (s->bitalloc_huffman[j] == 7) {
855 av_log(s->avctx, AV_LOG_ERROR,
856 "Invalid bit allocation index\n");
857 return AVERROR_INVALIDDATA;
860 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
863 if (s->bitalloc[j][k] > 26) {
864 av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
865 j, k, s->bitalloc[j][k]);
866 return AVERROR_INVALIDDATA;
871 /* Transition mode */
872 for (j = base_channel; j < s->prim_channels; j++) {
873 for (k = 0; k < s->subband_activity[j]; k++) {
874 s->transition_mode[j][k] = 0;
875 if (s->subsubframes[s->current_subframe] > 1 &&
876 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
877 s->transition_mode[j][k] =
878 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
883 if (get_bits_left(&s->gb) < 0)
884 return AVERROR_INVALIDDATA;
886 for (j = base_channel; j < s->prim_channels; j++) {
887 const uint32_t *scale_table;
888 int scale_sum, log_size;
890 memset(s->scale_factor[j], 0,
891 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
893 if (s->scalefactor_huffman[j] == 6) {
894 scale_table = scale_factor_quant7;
897 scale_table = scale_factor_quant6;
901 /* When huffman coded, only the difference is encoded */
904 for (k = 0; k < s->subband_activity[j]; k++) {
905 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
906 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
907 s->scale_factor[j][k][0] = scale_table[scale_sum];
910 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
911 /* Get second scale factor */
912 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
913 s->scale_factor[j][k][1] = scale_table[scale_sum];
918 /* Joint subband scale factor codebook select */
919 for (j = base_channel; j < s->prim_channels; j++) {
920 /* Transmitted only if joint subband coding enabled */
921 if (s->joint_intensity[j] > 0)
922 s->joint_huff[j] = get_bits(&s->gb, 3);
925 if (get_bits_left(&s->gb) < 0)
926 return AVERROR_INVALIDDATA;
928 /* Scale factors for joint subband coding */
929 for (j = base_channel; j < s->prim_channels; j++) {
932 /* Transmitted only if joint subband coding enabled */
933 if (s->joint_intensity[j] > 0) {
935 source_channel = s->joint_intensity[j] - 1;
937 /* When huffman coded, only the difference is encoded
938 * (is this valid as well for joint scales ???) */
940 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
941 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
942 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
945 if (!(s->debug_flag & 0x02)) {
946 av_log(s->avctx, AV_LOG_DEBUG,
947 "Joint stereo coding not supported\n");
948 s->debug_flag |= 0x02;
953 /* Stereo downmix coefficients */
954 if (!base_channel && s->prim_channels > 2) {
956 for (j = base_channel; j < s->prim_channels; j++) {
957 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
958 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
961 int am = s->amode & DCA_CHANNEL_MASK;
962 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
963 av_log(s->avctx, AV_LOG_ERROR,
964 "Invalid channel mode %d\n", am);
965 return AVERROR_INVALIDDATA;
967 for (j = base_channel; j < FFMIN(s->prim_channels, FF_ARRAY_ELEMS(dca_default_coeffs[am])); j++) {
968 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
969 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
974 /* Dynamic range coefficient */
975 if (!base_channel && s->dynrange)
976 s->dynrange_coef = get_bits(&s->gb, 8);
978 /* Side information CRC check word */
979 if (s->crc_present) {
980 get_bits(&s->gb, 16);
984 * Primary audio data arrays
987 /* VQ encoded high frequency subbands */
988 for (j = base_channel; j < s->prim_channels; j++)
989 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
990 /* 1 vector -> 32 samples */
991 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
993 /* Low frequency effect data */
994 if (!base_channel && s->lfe) {
997 int lfe_samples = 2 * s->lfe * (4 + block_index);
998 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1001 for (j = lfe_samples; j < lfe_end_sample; j++) {
1002 /* Signed 8 bits int */
1003 s->lfe_data[j] = get_sbits(&s->gb, 8);
1006 /* Scale factor index */
1007 quant7 = get_bits(&s->gb, 8);
1009 av_log_ask_for_sample(s->avctx, "LFEScaleIndex larger than 127\n");
1010 return AVERROR_INVALIDDATA;
1012 s->lfe_scale_factor = scale_factor_quant7[quant7];
1014 /* Quantization step size * scale factor */
1015 lfe_scale = 0.035 * s->lfe_scale_factor;
1017 for (j = lfe_samples; j < lfe_end_sample; j++)
1018 s->lfe_data[j] *= lfe_scale;
1022 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
1023 s->subsubframes[s->current_subframe]);
1024 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
1025 s->partial_samples[s->current_subframe]);
1027 for (j = base_channel; j < s->prim_channels; j++) {
1028 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
1029 for (k = 0; k < s->subband_activity[j]; k++)
1030 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
1031 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1033 for (j = base_channel; j < s->prim_channels; j++) {
1034 for (k = 0; k < s->subband_activity[j]; k++)
1035 av_log(s->avctx, AV_LOG_DEBUG,
1036 "prediction coefs: %f, %f, %f, %f\n",
1037 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
1038 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
1039 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
1040 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
1042 for (j = base_channel; j < s->prim_channels; j++) {
1043 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
1044 for (k = 0; k < s->vq_start_subband[j]; k++)
1045 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
1046 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1048 for (j = base_channel; j < s->prim_channels; j++) {
1049 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
1050 for (k = 0; k < s->subband_activity[j]; k++)
1051 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
1052 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1054 for (j = base_channel; j < s->prim_channels; j++) {
1055 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
1056 for (k = 0; k < s->subband_activity[j]; k++) {
1057 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
1058 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
1059 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
1060 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
1062 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1064 for (j = base_channel; j < s->prim_channels; j++) {
1065 if (s->joint_intensity[j] > 0) {
1066 int source_channel = s->joint_intensity[j] - 1;
1067 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
1068 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
1069 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
1070 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1073 if (!base_channel && s->prim_channels > 2 && s->downmix) {
1074 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
1075 for (j = 0; j < s->prim_channels; j++) {
1076 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
1077 dca_downmix_coeffs[s->downmix_coef[j][0]]);
1078 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
1079 dca_downmix_coeffs[s->downmix_coef[j][1]]);
1081 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1083 for (j = base_channel; j < s->prim_channels; j++)
1084 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
1085 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
1086 if (!base_channel && s->lfe) {
1087 int lfe_samples = 2 * s->lfe * (4 + block_index);
1088 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1090 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
1091 for (j = lfe_samples; j < lfe_end_sample; j++)
1092 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
1093 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1100 static void qmf_32_subbands(DCAContext *s, int chans,
1101 float samples_in[32][8], float *samples_out,
1104 const float *prCoeff;
1107 int sb_act = s->subband_activity[chans];
1110 scale *= sqrt(1 / 8.0);
1113 if (!s->multirate_inter) /* Non-perfect reconstruction */
1114 prCoeff = fir_32bands_nonperfect;
1115 else /* Perfect reconstruction */
1116 prCoeff = fir_32bands_perfect;
1118 for (i = sb_act; i < 32; i++)
1121 /* Reconstructed channel sample index */
1122 for (subindex = 0; subindex < 8; subindex++) {
1123 /* Load in one sample from each subband and clear inactive subbands */
1124 for (i = 0; i < sb_act; i++) {
1125 unsigned sign = (i - 1) & 2;
1126 uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
1127 AV_WN32A(&s->raXin[i], v);
1130 s->synth.synth_filter_float(&s->imdct,
1131 s->subband_fir_hist[chans],
1132 &s->hist_index[chans],
1133 s->subband_fir_noidea[chans], prCoeff,
1134 samples_out, s->raXin, scale);
1139 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
1140 int num_deci_sample, float *samples_in,
1141 float *samples_out, float scale)
1143 /* samples_in: An array holding decimated samples.
1144 * Samples in current subframe starts from samples_in[0],
1145 * while samples_in[-1], samples_in[-2], ..., stores samples
1146 * from last subframe as history.
1148 * samples_out: An array holding interpolated samples
1152 const float *prCoeff;
1155 /* Select decimation filter */
1156 if (decimation_select == 1) {
1158 prCoeff = lfe_fir_128;
1161 prCoeff = lfe_fir_64;
1164 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1165 s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale);
1167 samples_out += 2 * decifactor;
1171 /* downmixing routines */
1172 #define MIX_REAR1(samples, s1, rs, coef) \
1173 samples[0][i] += samples[s1][i] * coef[rs][0]; \
1174 samples[1][i] += samples[s1][i] * coef[rs][1];
1176 #define MIX_REAR2(samples, s1, s2, rs, coef) \
1177 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
1178 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
1180 #define MIX_FRONT3(samples, coef) \
1181 t = samples[c][i]; \
1182 u = samples[l][i]; \
1183 v = samples[r][i]; \
1184 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1185 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1187 #define DOWNMIX_TO_STEREO(op1, op2) \
1188 for (i = 0; i < 256; i++) { \
1193 static void dca_downmix(float **samples, int srcfmt,
1194 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
1195 const int8_t *channel_mapping)
1197 int c, l, r, sl, sr, s;
1200 float coef[DCA_PRIM_CHANNELS_MAX][2];
1202 for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) {
1203 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
1204 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
1210 case DCA_STEREO_TOTAL:
1211 case DCA_STEREO_SUMDIFF:
1213 av_log(NULL, AV_LOG_ERROR, "Not implemented!\n");
1218 c = channel_mapping[0];
1219 l = channel_mapping[1];
1220 r = channel_mapping[2];
1221 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1224 s = channel_mapping[2];
1225 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1228 c = channel_mapping[0];
1229 l = channel_mapping[1];
1230 r = channel_mapping[2];
1231 s = channel_mapping[3];
1232 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1233 MIX_REAR1(samples, s, 3, coef));
1236 sl = channel_mapping[2];
1237 sr = channel_mapping[3];
1238 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1241 c = channel_mapping[0];
1242 l = channel_mapping[1];
1243 r = channel_mapping[2];
1244 sl = channel_mapping[3];
1245 sr = channel_mapping[4];
1246 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1247 MIX_REAR2(samples, sl, sr, 3, coef));
1253 #ifndef decode_blockcodes
1254 /* Very compact version of the block code decoder that does not use table
1255 * look-up but is slightly slower */
1256 static int decode_blockcode(int code, int levels, int *values)
1259 int offset = (levels - 1) >> 1;
1261 for (i = 0; i < 4; i++) {
1262 int div = FASTDIV(code, levels);
1263 values[i] = code - offset - div * levels;
1270 static int decode_blockcodes(int code1, int code2, int levels, int *values)
1272 return decode_blockcode(code1, levels, values) |
1273 decode_blockcode(code2, levels, values + 4);
1277 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1278 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1280 #ifndef int8x8_fmul_int32
1281 static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
1283 float fscale = scale / 16.0;
1285 for (i = 0; i < 8; i++)
1286 dst[i] = src[i] * fscale;
1290 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1293 int subsubframe = s->current_subsubframe;
1295 const float *quant_step_table;
1298 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1299 LOCAL_ALIGNED_16(int, block, [8]);
1305 /* Select quantization step size table */
1306 if (s->bit_rate_index == 0x1f)
1307 quant_step_table = lossless_quant_d;
1309 quant_step_table = lossy_quant_d;
1311 for (k = base_channel; k < s->prim_channels; k++) {
1312 if (get_bits_left(&s->gb) < 0)
1313 return AVERROR_INVALIDDATA;
1315 for (l = 0; l < s->vq_start_subband[k]; l++) {
1318 /* Select the mid-tread linear quantizer */
1319 int abits = s->bitalloc[k][l];
1321 float quant_step_size = quant_step_table[abits];
1324 * Determine quantization index code book and its type
1327 /* Select quantization index code book */
1328 int sel = s->quant_index_huffman[k][abits];
1331 * Extract bits from the bit stream
1334 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
1336 /* Deal with transients */
1337 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1338 float rscale = quant_step_size * s->scale_factor[k][l][sfi] *
1339 s->scalefactor_adj[k][sel];
1341 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1344 int block_code1, block_code2, size, levels, err;
1346 size = abits_sizes[abits - 1];
1347 levels = abits_levels[abits - 1];
1349 block_code1 = get_bits(&s->gb, size);
1350 block_code2 = get_bits(&s->gb, size);
1351 err = decode_blockcodes(block_code1, block_code2,
1354 av_log(s->avctx, AV_LOG_ERROR,
1355 "ERROR: block code look-up failed\n");
1356 return AVERROR_INVALIDDATA;
1360 for (m = 0; m < 8; m++)
1361 block[m] = get_sbits(&s->gb, abits - 3);
1365 for (m = 0; m < 8; m++)
1366 block[m] = get_bitalloc(&s->gb,
1367 &dca_smpl_bitalloc[abits], sel);
1370 s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
1375 * Inverse ADPCM if in prediction mode
1377 if (s->prediction_mode[k][l]) {
1379 for (m = 0; m < 8; m++) {
1380 for (n = 1; n <= 4; n++)
1382 subband_samples[k][l][m] +=
1383 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1384 subband_samples[k][l][m - n] / 8192);
1385 else if (s->predictor_history)
1386 subband_samples[k][l][m] +=
1387 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1388 s->subband_samples_hist[k][l][m - n + 4] / 8192);
1394 * Decode VQ encoded high frequencies
1396 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1397 /* 1 vector -> 32 samples but we only need the 8 samples
1398 * for this subsubframe. */
1399 int hfvq = s->high_freq_vq[k][l];
1401 if (!s->debug_flag & 0x01) {
1402 av_log(s->avctx, AV_LOG_DEBUG,
1403 "Stream with high frequencies VQ coding\n");
1404 s->debug_flag |= 0x01;
1407 int8x8_fmul_int32(subband_samples[k][l],
1408 &high_freq_vq[hfvq][subsubframe * 8],
1409 s->scale_factor[k][l][0]);
1413 /* Check for DSYNC after subsubframe */
1414 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1415 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1417 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1420 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1424 /* Backup predictor history for adpcm */
1425 for (k = base_channel; k < s->prim_channels; k++)
1426 for (l = 0; l < s->vq_start_subband[k]; l++)
1427 memcpy(s->subband_samples_hist[k][l],
1428 &subband_samples[k][l][4],
1429 4 * sizeof(subband_samples[0][0][0]));
1434 static int dca_filter_channels(DCAContext *s, int block_index)
1436 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1439 /* 32 subbands QMF */
1440 for (k = 0; k < s->prim_channels; k++) {
1441 /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1442 0, 8388608.0, 8388608.0 };*/
1443 if (s->channel_order_tab[k] >= 0)
1444 qmf_32_subbands(s, k, subband_samples[k],
1445 s->samples_chanptr[s->channel_order_tab[k]],
1446 M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
1450 if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
1451 dca_downmix(s->samples_chanptr, s->amode, s->downmix_coef, s->channel_order_tab);
1454 /* Generate LFE samples for this subsubframe FIXME!!! */
1455 if (s->output & DCA_LFE) {
1456 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1457 s->lfe_data + 2 * s->lfe * (block_index + 4),
1458 s->samples_chanptr[s->lfe_index],
1459 1.0 / (256.0 * 32768.0));
1460 /* Outputs 20bits pcm samples */
1467 static int dca_subframe_footer(DCAContext *s, int base_channel)
1469 int aux_data_count = 0, i;
1472 * Unpack optional information
1475 /* presumably optional information only appears in the core? */
1476 if (!base_channel) {
1478 skip_bits_long(&s->gb, 32);
1481 aux_data_count = get_bits(&s->gb, 6);
1483 for (i = 0; i < aux_data_count; i++)
1484 get_bits(&s->gb, 8);
1486 if (s->crc_present && (s->downmix || s->dynrange))
1487 get_bits(&s->gb, 16);
1494 * Decode a dca frame block
1496 * @param s pointer to the DCAContext
1499 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1504 if (s->current_subframe >= s->subframes) {
1505 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1506 s->current_subframe, s->subframes);
1507 return AVERROR_INVALIDDATA;
1510 if (!s->current_subsubframe) {
1512 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1514 /* Read subframe header */
1515 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1519 /* Read subsubframe */
1521 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1523 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1527 s->current_subsubframe++;
1528 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1529 s->current_subsubframe = 0;
1530 s->current_subframe++;
1532 if (s->current_subframe >= s->subframes) {
1534 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1536 /* Read subframe footer */
1537 if ((ret = dca_subframe_footer(s, base_channel)))
1545 * Return the number of channels in an ExSS speaker mask (HD)
1547 static int dca_exss_mask2count(int mask)
1549 /* count bits that mean speaker pairs twice */
1550 return av_popcount(mask) +
1551 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1552 DCA_EXSS_FRONT_LEFT_RIGHT |
1553 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1554 DCA_EXSS_WIDE_LEFT_RIGHT |
1555 DCA_EXSS_SIDE_LEFT_RIGHT |
1556 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1557 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1558 DCA_EXSS_REAR_LEFT_RIGHT |
1559 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1563 * Skip mixing coefficients of a single mix out configuration (HD)
1565 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1569 for (i = 0; i < channels; i++) {
1570 int mix_map_mask = get_bits(gb, out_ch);
1571 int num_coeffs = av_popcount(mix_map_mask);
1572 skip_bits_long(gb, num_coeffs * 6);
1577 * Parse extension substream asset header (HD)
1579 static int dca_exss_parse_asset_header(DCAContext *s)
1581 int header_pos = get_bits_count(&s->gb);
1584 int embedded_stereo = 0;
1585 int embedded_6ch = 0;
1586 int drc_code_present;
1587 int av_uninit(extensions_mask);
1590 if (get_bits_left(&s->gb) < 16)
1593 /* We will parse just enough to get to the extensions bitmask with which
1594 * we can set the profile value. */
1596 header_size = get_bits(&s->gb, 9) + 1;
1597 skip_bits(&s->gb, 3); // asset index
1599 if (s->static_fields) {
1600 if (get_bits1(&s->gb))
1601 skip_bits(&s->gb, 4); // asset type descriptor
1602 if (get_bits1(&s->gb))
1603 skip_bits_long(&s->gb, 24); // language descriptor
1605 if (get_bits1(&s->gb)) {
1606 /* How can one fit 1024 bytes of text here if the maximum value
1607 * for the asset header size field above was 512 bytes? */
1608 int text_length = get_bits(&s->gb, 10) + 1;
1609 if (get_bits_left(&s->gb) < text_length * 8)
1611 skip_bits_long(&s->gb, text_length * 8); // info text
1614 skip_bits(&s->gb, 5); // bit resolution - 1
1615 skip_bits(&s->gb, 4); // max sample rate code
1616 channels = get_bits(&s->gb, 8) + 1;
1618 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1619 int spkr_remap_sets;
1620 int spkr_mask_size = 16;
1624 embedded_stereo = get_bits1(&s->gb);
1626 embedded_6ch = get_bits1(&s->gb);
1628 if (get_bits1(&s->gb)) {
1629 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1630 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1633 spkr_remap_sets = get_bits(&s->gb, 3);
1635 for (i = 0; i < spkr_remap_sets; i++) {
1636 /* std layout mask for each remap set */
1637 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1640 for (i = 0; i < spkr_remap_sets; i++) {
1641 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1642 if (get_bits_left(&s->gb) < 0)
1645 for (j = 0; j < num_spkrs[i]; j++) {
1646 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1647 int num_dec_ch = av_popcount(remap_dec_ch_mask);
1648 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1653 skip_bits(&s->gb, 3); // representation type
1657 drc_code_present = get_bits1(&s->gb);
1658 if (drc_code_present)
1659 get_bits(&s->gb, 8); // drc code
1661 if (get_bits1(&s->gb))
1662 skip_bits(&s->gb, 5); // dialog normalization code
1664 if (drc_code_present && embedded_stereo)
1665 get_bits(&s->gb, 8); // drc stereo code
1667 if (s->mix_metadata && get_bits1(&s->gb)) {
1668 skip_bits(&s->gb, 1); // external mix
1669 skip_bits(&s->gb, 6); // post mix gain code
1671 if (get_bits(&s->gb, 2) != 3) // mixer drc code
1672 skip_bits(&s->gb, 3); // drc limit
1674 skip_bits(&s->gb, 8); // custom drc code
1676 if (get_bits1(&s->gb)) // channel specific scaling
1677 for (i = 0; i < s->num_mix_configs; i++)
1678 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1680 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1682 for (i = 0; i < s->num_mix_configs; i++) {
1683 if (get_bits_left(&s->gb) < 0)
1685 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1687 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1688 if (embedded_stereo)
1689 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1693 switch (get_bits(&s->gb, 2)) {
1694 case 0: extensions_mask = get_bits(&s->gb, 12); break;
1695 case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1696 case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1697 case 3: extensions_mask = 0; /* aux coding */ break;
1700 /* not parsed further, we were only interested in the extensions mask */
1702 if (get_bits_left(&s->gb) < 0)
1705 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1706 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1709 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1711 if (extensions_mask & DCA_EXT_EXSS_XLL)
1712 s->profile = FF_PROFILE_DTS_HD_MA;
1713 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1715 s->profile = FF_PROFILE_DTS_HD_HRA;
1717 if (!(extensions_mask & DCA_EXT_CORE))
1718 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1719 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1720 av_log(s->avctx, AV_LOG_WARNING,
1721 "DTS extensions detection mismatch (%d, %d)\n",
1722 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1727 static int dca_xbr_parse_frame(DCAContext *s)
1729 int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1730 int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1731 int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1732 int anctemp[DCA_CHSET_CHANS_MAX];
1733 int chset_fsize[DCA_CHSETS_MAX];
1734 int n_xbr_ch[DCA_CHSETS_MAX];
1735 int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1736 int i, j, k, l, chset, chan_base;
1738 av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1740 /* get bit position of sync header */
1741 hdr_pos = get_bits_count(&s->gb) - 32;
1743 hdr_size = get_bits(&s->gb, 6) + 1;
1744 num_chsets = get_bits(&s->gb, 2) + 1;
1746 for(i = 0; i < num_chsets; i++)
1747 chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1749 xbr_tmode = get_bits1(&s->gb);
1751 for(i = 0; i < num_chsets; i++) {
1752 n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1753 k = get_bits(&s->gb, 2) + 5;
1754 for(j = 0; j < n_xbr_ch[i]; j++)
1755 active_bands[i][j] = get_bits(&s->gb, k) + 1;
1758 /* skip to the end of the header */
1759 i = get_bits_count(&s->gb);
1760 if(hdr_pos + hdr_size * 8 > i)
1761 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1763 /* loop over the channel data sets */
1764 /* only decode as many channels as we've decoded base data for */
1765 for(chset = 0, chan_base = 0;
1766 chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1767 chan_base += n_xbr_ch[chset++]) {
1768 int start_posn = get_bits_count(&s->gb);
1769 int subsubframe = 0;
1772 /* loop over subframes */
1773 for (k = 0; k < (s->sample_blocks / 8); k++) {
1774 /* parse header if we're on first subsubframe of a block */
1775 if(subsubframe == 0) {
1776 /* Parse subframe header */
1777 for(i = 0; i < n_xbr_ch[chset]; i++) {
1778 anctemp[i] = get_bits(&s->gb, 2) + 2;
1781 for(i = 0; i < n_xbr_ch[chset]; i++) {
1782 get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1785 for(i = 0; i < n_xbr_ch[chset]; i++) {
1786 anctemp[i] = get_bits(&s->gb, 3);
1787 if(anctemp[i] < 1) {
1788 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1789 return AVERROR_INVALIDDATA;
1793 /* generate scale factors */
1794 for(i = 0; i < n_xbr_ch[chset]; i++) {
1795 const uint32_t *scale_table;
1798 if (s->scalefactor_huffman[chan_base+i] == 6) {
1799 scale_table = scale_factor_quant7;
1801 scale_table = scale_factor_quant6;
1806 for(j = 0; j < active_bands[chset][i]; j++) {
1807 if(abits_high[i][j] > 0) {
1808 scale_table_high[i][j][0] =
1809 scale_table[get_bits(&s->gb, nbits)];
1811 if(xbr_tmode && s->transition_mode[i][j]) {
1812 scale_table_high[i][j][1] =
1813 scale_table[get_bits(&s->gb, nbits)];
1820 /* decode audio array for this block */
1821 for(i = 0; i < n_xbr_ch[chset]; i++) {
1822 for(j = 0; j < active_bands[chset][i]; j++) {
1823 const int xbr_abits = abits_high[i][j];
1824 const float quant_step_size = lossless_quant_d[xbr_abits];
1825 const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1826 const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1827 float *subband_samples = s->subband_samples[k][chan_base+i][j];
1834 get_array(&s->gb, block, 8, xbr_abits - 3);
1836 int block_code1, block_code2, size, levels, err;
1838 size = abits_sizes[xbr_abits - 1];
1839 levels = abits_levels[xbr_abits - 1];
1841 block_code1 = get_bits(&s->gb, size);
1842 block_code2 = get_bits(&s->gb, size);
1843 err = decode_blockcodes(block_code1, block_code2,
1846 av_log(s->avctx, AV_LOG_ERROR,
1847 "ERROR: DTS-XBR: block code look-up failed\n");
1848 return AVERROR_INVALIDDATA;
1852 /* scale & sum into subband */
1853 for(l = 0; l < 8; l++)
1854 subband_samples[l] += (float)block[l] * rscale;
1858 /* check DSYNC marker */
1859 if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1860 if(get_bits(&s->gb, 16) != 0xffff) {
1861 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1862 return AVERROR_INVALIDDATA;
1866 /* advance sub-sub-frame index */
1867 if(++subsubframe >= s->subsubframes[subframe]) {
1873 /* skip to next channel set */
1874 i = get_bits_count(&s->gb);
1875 if(start_posn + chset_fsize[chset] * 8 != i) {
1876 j = start_posn + chset_fsize[chset] * 8 - i;
1878 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1879 " skipping further than expected (%d bits)\n", j);
1880 skip_bits_long(&s->gb, j);
1887 /* parse initial header for XXCH and dump details */
1888 static int dca_xxch_decode_frame(DCAContext *s)
1890 int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1891 int i, chset, base_channel, chstart, fsize[8];
1893 /* assume header word has already been parsed */
1894 hdr_pos = get_bits_count(&s->gb) - 32;
1895 hdr_size = get_bits(&s->gb, 6) + 1;
1896 /*chhdr_crc =*/ skip_bits1(&s->gb);
1897 spkmsk_bits = get_bits(&s->gb, 5) + 1;
1898 num_chsets = get_bits(&s->gb, 2) + 1;
1900 for (i = 0; i < num_chsets; i++)
1901 fsize[i] = get_bits(&s->gb, 14) + 1;
1903 core_spk = get_bits(&s->gb, spkmsk_bits);
1904 s->xxch_core_spkmask = core_spk;
1905 s->xxch_nbits_spk_mask = spkmsk_bits;
1906 s->xxch_dmix_embedded = 0;
1908 /* skip to the end of the header */
1909 i = get_bits_count(&s->gb);
1910 if (hdr_pos + hdr_size * 8 > i)
1911 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1913 for (chset = 0; chset < num_chsets; chset++) {
1914 chstart = get_bits_count(&s->gb);
1915 base_channel = s->prim_channels;
1916 s->xxch_chset = chset;
1918 /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1919 5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1920 dca_parse_audio_coding_header(s, base_channel, 1);
1922 /* decode channel data */
1923 for (i = 0; i < (s->sample_blocks / 8); i++) {
1924 if (dca_decode_block(s, base_channel, i)) {
1925 av_log(s->avctx, AV_LOG_ERROR,
1926 "Error decoding DTS-XXCH extension\n");
1931 /* skip to end of this section */
1932 i = get_bits_count(&s->gb);
1933 if (chstart + fsize[chset] * 8 > i)
1934 skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1936 s->xxch_chset = num_chsets;
1942 * Parse extension substream header (HD)
1944 static void dca_exss_parse_header(DCAContext *s)
1951 int active_ss_mask[8];
1957 if (get_bits_left(&s->gb) < 52)
1960 start_posn = get_bits_count(&s->gb) - 32;
1962 skip_bits(&s->gb, 8); // user data
1963 ss_index = get_bits(&s->gb, 2);
1965 blownup = get_bits1(&s->gb);
1966 hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
1967 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1969 s->static_fields = get_bits1(&s->gb);
1970 if (s->static_fields) {
1971 skip_bits(&s->gb, 2); // reference clock code
1972 skip_bits(&s->gb, 3); // frame duration code
1974 if (get_bits1(&s->gb))
1975 skip_bits_long(&s->gb, 36); // timestamp
1977 /* a single stream can contain multiple audio assets that can be
1978 * combined to form multiple audio presentations */
1980 num_audiop = get_bits(&s->gb, 3) + 1;
1981 if (num_audiop > 1) {
1982 av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
1983 /* ignore such streams for now */
1987 num_assets = get_bits(&s->gb, 3) + 1;
1988 if (num_assets > 1) {
1989 av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
1990 /* ignore such streams for now */
1994 for (i = 0; i < num_audiop; i++)
1995 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
1997 for (i = 0; i < num_audiop; i++)
1998 for (j = 0; j <= ss_index; j++)
1999 if (active_ss_mask[i] & (1 << j))
2000 skip_bits(&s->gb, 8); // active asset mask
2002 s->mix_metadata = get_bits1(&s->gb);
2003 if (s->mix_metadata) {
2004 int mix_out_mask_size;
2006 skip_bits(&s->gb, 2); // adjustment level
2007 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
2008 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
2010 for (i = 0; i < s->num_mix_configs; i++) {
2011 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
2012 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
2017 for (i = 0; i < num_assets; i++)
2018 asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
2020 for (i = 0; i < num_assets; i++) {
2021 if (dca_exss_parse_asset_header(s))
2025 /* not parsed further, we were only interested in the extensions mask
2026 * from the asset header */
2028 if (num_assets > 0) {
2029 j = get_bits_count(&s->gb);
2030 if (start_posn + hdrsize * 8 > j)
2031 skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
2033 for (i = 0; i < num_assets; i++) {
2034 start_posn = get_bits_count(&s->gb);
2035 mkr = get_bits_long(&s->gb, 32);
2037 /* parse extensions that we know about */
2038 if (mkr == 0x655e315e) {
2039 dca_xbr_parse_frame(s);
2040 } else if (mkr == 0x47004a03) {
2041 dca_xxch_decode_frame(s);
2042 s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
2044 av_log(s->avctx, AV_LOG_DEBUG,
2045 "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
2048 /* skip to end of block */
2049 j = get_bits_count(&s->gb);
2050 if (start_posn + asset_size[i] * 8 > j)
2051 skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
2057 * Main frame decoding function
2058 * FIXME add arguments
2060 static int dca_decode_frame(AVCodecContext *avctx, void *data,
2061 int *got_frame_ptr, AVPacket *avpkt)
2063 const uint8_t *buf = avpkt->data;
2064 int buf_size = avpkt->size;
2068 int num_core_channels = 0;
2070 float **samples_flt;
2073 DCAContext *s = avctx->priv_data;
2075 int channels, full_channels;
2087 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
2088 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
2089 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
2090 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
2091 return AVERROR_INVALIDDATA;
2094 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
2095 if ((ret = dca_parse_frame_header(s)) < 0) {
2096 //seems like the frame is corrupt, try with the next one
2099 //set AVCodec values with parsed data
2100 avctx->sample_rate = s->sample_rate;
2101 avctx->bit_rate = s->bit_rate;
2103 s->profile = FF_PROFILE_DTS;
2105 for (i = 0; i < (s->sample_blocks / 8); i++) {
2106 if ((ret = dca_decode_block(s, 0, i))) {
2107 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
2112 /* record number of core channels incase less than max channels are requested */
2113 num_core_channels = s->prim_channels;
2116 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
2118 s->core_ext_mask = 0;
2120 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
2122 /* only scan for extensions if ext_descr was unknown or indicated a
2123 * supported XCh extension */
2124 if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
2126 /* if ext_descr was unknown, clear s->core_ext_mask so that the
2127 * extensions scan can fill it up */
2128 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
2130 /* extensions start at 32-bit boundaries into bitstream */
2131 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2133 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
2134 uint32_t bits = get_bits_long(&s->gb, 32);
2138 int ext_amode, xch_fsize;
2140 s->xch_base_channel = s->prim_channels;
2142 /* validate sync word using XCHFSIZE field */
2143 xch_fsize = show_bits(&s->gb, 10);
2144 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
2145 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
2148 /* skip length-to-end-of-frame field for the moment */
2149 skip_bits(&s->gb, 10);
2151 s->core_ext_mask |= DCA_EXT_XCH;
2153 /* extension amode(number of channels in extension) should be 1 */
2154 /* AFAIK XCh is not used for more channels */
2155 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
2156 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
2157 " supported!\n", ext_amode);
2161 /* much like core primary audio coding header */
2162 dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
2164 for (i = 0; i < (s->sample_blocks / 8); i++)
2165 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
2166 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
2174 /* XXCh: extended channels */
2175 /* usually found either in core or HD part in DTS-HD HRA streams,
2176 * but not in DTS-ES which contains XCh extensions instead */
2177 s->core_ext_mask |= DCA_EXT_XXCH;
2178 dca_xxch_decode_frame(s);
2182 int fsize96 = show_bits(&s->gb, 12) + 1;
2183 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
2186 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
2187 get_bits_count(&s->gb));
2188 skip_bits(&s->gb, 12);
2189 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
2190 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
2192 s->core_ext_mask |= DCA_EXT_X96;
2197 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2200 /* no supported extensions, skip the rest of the core substream */
2201 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
2204 if (s->core_ext_mask & DCA_EXT_X96)
2205 s->profile = FF_PROFILE_DTS_96_24;
2206 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
2207 s->profile = FF_PROFILE_DTS_ES;
2209 /* check for ExSS (HD part) */
2210 if (s->dca_buffer_size - s->frame_size > 32 &&
2211 get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
2212 dca_exss_parse_header(s);
2214 avctx->profile = s->profile;
2216 full_channels = channels = s->prim_channels + !!s->lfe;
2218 /* If we have XXCH then the channel layout is managed differently */
2219 /* note that XLL will also have another way to do things */
2220 if (!(s->core_ext_mask & DCA_EXT_XXCH)
2221 || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
2222 && avctx->request_channels
2223 < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
2224 { /* xxx should also do MA extensions */
2225 if (s->amode < 16) {
2226 avctx->channel_layout = dca_core_channel_layout[s->amode];
2228 if (s->xch_present && (!avctx->request_channels ||
2229 avctx->request_channels
2230 > num_core_channels + !!s->lfe)) {
2231 avctx->channel_layout |= AV_CH_BACK_CENTER;
2233 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2234 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
2236 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
2239 channels = num_core_channels + !!s->lfe;
2240 s->xch_present = 0; /* disable further xch processing */
2242 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2243 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
2245 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
2248 if (channels > !!s->lfe &&
2249 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
2250 return AVERROR_INVALIDDATA;
2252 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != channels) {
2253 av_log(avctx, AV_LOG_ERROR, "Number of channels %d mismatches layout %d\n", channels, av_get_channel_layout_nb_channels(avctx->channel_layout));
2254 return AVERROR_INVALIDDATA;
2257 if (avctx->request_channels == 2 && s->prim_channels > 2) {
2259 s->output = DCA_STEREO;
2260 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
2262 else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
2263 static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2264 s->channel_order_tab = dca_channel_order_native;
2266 s->lfe_index = dca_lfe_index[s->amode];
2268 av_log(avctx, AV_LOG_ERROR,
2269 "Non standard configuration %d !\n", s->amode);
2270 return AVERROR_INVALIDDATA;
2273 s->xxch_dmix_embedded = 0;
2275 /* we only get here if an XXCH channel set can be added to the mix */
2276 channel_mask = s->xxch_core_spkmask;
2278 if (avctx->request_channels > 0
2279 && avctx->request_channels < s->prim_channels) {
2280 channels = num_core_channels + !!s->lfe;
2281 for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
2282 <= avctx->request_channels; i++) {
2283 channels += s->xxch_chset_nch[i];
2284 channel_mask |= s->xxch_spk_masks[i];
2287 channels = s->prim_channels + !!s->lfe;
2288 for (i = 0; i < s->xxch_chset; i++) {
2289 channel_mask |= s->xxch_spk_masks[i];
2293 /* Given the DTS spec'ed channel mask, generate an avcodec version */
2295 for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
2296 if (channel_mask & (1 << i)) {
2297 channel_layout |= map_xxch_to_native[i];
2301 /* make sure that we have managed to get equivelant dts/avcodec channel
2302 * masks in some sense -- unfortunately some channels could overlap */
2303 if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
2304 av_log(avctx, AV_LOG_DEBUG,
2305 "DTS-XXCH: Inconsistant avcodec/dts channel layouts\n");
2306 return AVERROR_INVALIDDATA;
2309 avctx->channel_layout = channel_layout;
2311 if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
2312 /* Estimate DTS --> avcodec ordering table */
2313 for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
2314 mask = chset >= 0 ? s->xxch_spk_masks[chset]
2315 : s->xxch_core_spkmask;
2316 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
2317 if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
2318 lavc = map_xxch_to_native[i];
2319 posn = av_popcount(channel_layout & (lavc - 1));
2320 s->xxch_order_tab[j++] = posn;
2325 s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
2326 } else { /* native ordering */
2327 for (i = 0; i < channels; i++)
2328 s->xxch_order_tab[i] = i;
2330 s->lfe_index = channels - 1;
2333 s->channel_order_tab = s->xxch_order_tab;
2336 if (avctx->channels != channels) {
2337 if (avctx->channels)
2338 av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
2339 avctx->channels = channels;
2342 /* get output buffer */
2343 s->frame.nb_samples = 256 * (s->sample_blocks / 8);
2344 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
2345 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2348 samples_flt = (float **) s->frame.extended_data;
2350 /* allocate buffer for extra channels if downmixing */
2351 if (avctx->channels < full_channels) {
2352 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
2353 s->frame.nb_samples,
2354 avctx->sample_fmt, 0);
2358 av_fast_malloc(&s->extra_channels_buffer,
2359 &s->extra_channels_buffer_size, ret);
2360 if (!s->extra_channels_buffer)
2361 return AVERROR(ENOMEM);
2363 ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
2364 s->extra_channels_buffer,
2365 full_channels - channels,
2366 s->frame.nb_samples, avctx->sample_fmt, 0);
2371 /* filter to get final output */
2372 for (i = 0; i < (s->sample_blocks / 8); i++) {
2375 for (ch = 0; ch < channels; ch++)
2376 s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
2377 for (; ch < full_channels; ch++)
2378 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
2380 dca_filter_channels(s, i);
2382 /* If this was marked as a DTS-ES stream we need to subtract back- */
2383 /* channel from SL & SR to remove matrixed back-channel signal */
2384 if ((s->source_pcm_res & 1) && s->xch_present) {
2385 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
2386 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
2387 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
2388 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2389 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2392 /* If stream contains XXCH, we might need to undo an embedded downmix */
2393 if (s->xxch_dmix_embedded) {
2394 /* Loop over channel sets in turn */
2395 ch = num_core_channels;
2396 for (chset = 0; chset < s->xxch_chset; chset++) {
2397 endch = ch + s->xxch_chset_nch[chset];
2398 mask = s->xxch_dmix_embedded;
2401 for (j = ch; j < endch; j++) {
2402 if (mask & (1 << j)) { /* this channel has been mixed-out */
2403 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2404 for (k = 0; k < endch; k++) {
2405 achan = s->channel_order_tab[k];
2406 scale = s->xxch_dmix_coeff[j][k];
2408 dst_chan = s->samples_chanptr[achan];
2409 s->fdsp.vector_fmac_scalar(dst_chan, src_chan,
2416 /* if a downmix has been embedded then undo the pre-scaling */
2417 if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
2418 scale = s->xxch_dmix_sf[chset];
2420 for (j = 0; j < ch; j++) {
2421 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2422 for (k = 0; k < 256; k++)
2423 src_chan[k] *= scale;
2426 /* LFE channel is always part of core, scale if it exists */
2428 src_chan = s->samples_chanptr[s->lfe_index];
2429 for (k = 0; k < 256; k++)
2430 src_chan[k] *= scale;
2440 /* update lfe history */
2441 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2442 for (i = 0; i < 2 * s->lfe * 4; i++)
2443 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2446 *(AVFrame *) data = s->frame;
2454 * DCA initialization
2456 * @param avctx pointer to the AVCodecContext
2459 static av_cold int dca_decode_init(AVCodecContext *avctx)
2461 DCAContext *s = avctx->priv_data;
2466 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
2467 ff_mdct_init(&s->imdct, 6, 1, 1.0);
2468 ff_synth_filter_init(&s->synth);
2469 ff_dcadsp_init(&s->dcadsp);
2470 ff_fmt_convert_init(&s->fmt_conv, avctx);
2472 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2474 /* allow downmixing to stereo */
2475 if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
2476 avctx->request_channels == 2) {
2477 avctx->channels = avctx->request_channels;
2480 avcodec_get_frame_defaults(&s->frame);
2481 avctx->coded_frame = &s->frame;
2486 static av_cold int dca_decode_end(AVCodecContext *avctx)
2488 DCAContext *s = avctx->priv_data;
2489 ff_mdct_end(&s->imdct);
2490 av_freep(&s->extra_channels_buffer);
2494 static const AVProfile profiles[] = {
2495 { FF_PROFILE_DTS, "DTS" },
2496 { FF_PROFILE_DTS_ES, "DTS-ES" },
2497 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2498 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2499 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2500 { FF_PROFILE_UNKNOWN },
2503 AVCodec ff_dca_decoder = {
2505 .type = AVMEDIA_TYPE_AUDIO,
2506 .id = AV_CODEC_ID_DTS,
2507 .priv_data_size = sizeof(DCAContext),
2508 .init = dca_decode_init,
2509 .decode = dca_decode_frame,
2510 .close = dca_decode_end,
2511 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2512 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2513 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2514 AV_SAMPLE_FMT_NONE },
2515 .profiles = NULL_IF_CONFIG_SMALL(profiles),