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 "synth_filter.h"
46 #include "fmtconvert.h"
55 #define DCA_PRIM_CHANNELS_MAX (7)
56 #define DCA_ABITS_MAX (32) /* Should be 28 */
57 #define DCA_SUBSUBFRAMES_MAX (4)
58 #define DCA_SUBFRAMES_MAX (16)
59 #define DCA_BLOCKS_MAX (16)
60 #define DCA_LFE_MAX (3)
61 #define DCA_CHSETS_MAX (4)
62 #define DCA_CHSET_CHANS_MAX (8)
78 /* these are unconfirmed but should be mostly correct */
79 enum DCAExSSSpeakerMask {
80 DCA_EXSS_FRONT_CENTER = 0x0001,
81 DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
82 DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
83 DCA_EXSS_LFE = 0x0008,
84 DCA_EXSS_REAR_CENTER = 0x0010,
85 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
86 DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
87 DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
88 DCA_EXSS_OVERHEAD = 0x0100,
89 DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
90 DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
91 DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
92 DCA_EXSS_LFE2 = 0x1000,
93 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
94 DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
95 DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
98 enum DCAXxchSpeakerMask {
99 DCA_XXCH_FRONT_CENTER = 0x0000001,
100 DCA_XXCH_FRONT_LEFT = 0x0000002,
101 DCA_XXCH_FRONT_RIGHT = 0x0000004,
102 DCA_XXCH_SIDE_REAR_LEFT = 0x0000008,
103 DCA_XXCH_SIDE_REAR_RIGHT = 0x0000010,
104 DCA_XXCH_LFE1 = 0x0000020,
105 DCA_XXCH_REAR_CENTER = 0x0000040,
106 DCA_XXCH_SURROUND_REAR_LEFT = 0x0000080,
107 DCA_XXCH_SURROUND_REAR_RIGHT = 0x0000100,
108 DCA_XXCH_SIDE_SURROUND_LEFT = 0x0000200,
109 DCA_XXCH_SIDE_SURROUND_RIGHT = 0x0000400,
110 DCA_XXCH_FRONT_CENTER_LEFT = 0x0000800,
111 DCA_XXCH_FRONT_CENTER_RIGHT = 0x0001000,
112 DCA_XXCH_FRONT_HIGH_LEFT = 0x0002000,
113 DCA_XXCH_FRONT_HIGH_CENTER = 0x0004000,
114 DCA_XXCH_FRONT_HIGH_RIGHT = 0x0008000,
115 DCA_XXCH_LFE2 = 0x0010000,
116 DCA_XXCH_SIDE_FRONT_LEFT = 0x0020000,
117 DCA_XXCH_SIDE_FRONT_RIGHT = 0x0040000,
118 DCA_XXCH_OVERHEAD = 0x0080000,
119 DCA_XXCH_SIDE_HIGH_LEFT = 0x0100000,
120 DCA_XXCH_SIDE_HIGH_RIGHT = 0x0200000,
121 DCA_XXCH_REAR_HIGH_CENTER = 0x0400000,
122 DCA_XXCH_REAR_HIGH_LEFT = 0x0800000,
123 DCA_XXCH_REAR_HIGH_RIGHT = 0x1000000,
124 DCA_XXCH_REAR_LOW_CENTER = 0x2000000,
125 DCA_XXCH_REAR_LOW_LEFT = 0x4000000,
126 DCA_XXCH_REAR_LOW_RIGHT = 0x8000000,
129 static const uint32_t map_xxch_to_native[28] = {
139 AV_CH_SIDE_LEFT, /* side surround left -- dup sur side L */
140 AV_CH_SIDE_RIGHT, /* side surround right -- dup sur side R */
141 AV_CH_FRONT_LEFT_OF_CENTER,
142 AV_CH_FRONT_RIGHT_OF_CENTER,
143 AV_CH_TOP_FRONT_LEFT,
144 AV_CH_TOP_FRONT_CENTER,
145 AV_CH_TOP_FRONT_RIGHT,
146 AV_CH_LOW_FREQUENCY, /* lfe2 -- duplicate lfe1 position */
147 AV_CH_FRONT_LEFT_OF_CENTER, /* side front left -- dup front cntr L */
148 AV_CH_FRONT_RIGHT_OF_CENTER,/* side front right -- dup front cntr R */
149 AV_CH_TOP_CENTER, /* overhead */
150 AV_CH_TOP_FRONT_LEFT, /* side high left -- dup */
151 AV_CH_TOP_FRONT_RIGHT, /* side high right -- dup */
152 AV_CH_TOP_BACK_CENTER,
154 AV_CH_TOP_BACK_RIGHT,
155 AV_CH_BACK_CENTER, /* rear low center -- dup */
156 AV_CH_BACK_LEFT, /* rear low left -- dup */
157 AV_CH_BACK_RIGHT /* read low right -- dup */
160 enum DCAExtensionMask {
161 DCA_EXT_CORE = 0x001, ///< core in core substream
162 DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
163 DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
164 DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
165 DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
166 DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
167 DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
168 DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
169 DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
170 DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
173 /* -1 are reserved or unknown */
174 static const int dca_ext_audio_descr_mask[] = {
178 DCA_EXT_XCH | DCA_EXT_X96,
185 /* extensions that reside in core substream */
186 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
188 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
189 * Some compromises have been made for special configurations. Most configurations
190 * are never used so complete accuracy is not needed.
192 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
193 * S -> side, when both rear and back are configured move one of them to the side channel
195 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
197 static const uint64_t dca_core_channel_layout[] = {
198 AV_CH_FRONT_CENTER, ///< 1, A
199 AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
200 AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
201 AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
202 AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
203 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
204 AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
205 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
206 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
208 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
209 AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
211 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
212 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
214 AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
215 AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
217 AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
218 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
219 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
221 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
222 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
223 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
225 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
226 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
227 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
229 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
230 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
231 AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
234 static const int8_t dca_lfe_index[] = {
235 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
238 static const int8_t dca_channel_reorder_lfe[][9] = {
239 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
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 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
245 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
246 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
247 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
248 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
249 { 3, 4, 0, 1, 5, 6, -1, -1, -1},
250 { 2, 0, 1, 4, 5, 6, -1, -1, -1},
251 { 0, 6, 4, 5, 2, 3, -1, -1, -1},
252 { 4, 2, 5, 0, 1, 6, 7, -1, -1},
253 { 5, 6, 0, 1, 7, 3, 8, 4, -1},
254 { 4, 2, 5, 0, 1, 6, 8, 7, -1},
257 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
258 { 0, 2, -1, -1, -1, -1, -1, -1, -1},
259 { 0, 1, 3, -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 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
264 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
265 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
266 { 0, 1, 4, 5, 3, -1, -1, -1, -1},
267 { 2, 0, 1, 5, 6, 4, -1, -1, -1},
268 { 3, 4, 0, 1, 6, 7, 5, -1, -1},
269 { 2, 0, 1, 4, 5, 6, 7, -1, -1},
270 { 0, 6, 4, 5, 2, 3, 7, -1, -1},
271 { 4, 2, 5, 0, 1, 7, 8, 6, -1},
272 { 5, 6, 0, 1, 8, 3, 9, 4, 7},
273 { 4, 2, 5, 0, 1, 6, 9, 8, 7},
276 static const int8_t dca_channel_reorder_nolfe[][9] = {
277 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
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 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
283 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
284 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
285 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
286 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
287 { 2, 3, 0, 1, 4, 5, -1, -1, -1},
288 { 2, 0, 1, 3, 4, 5, -1, -1, -1},
289 { 0, 5, 3, 4, 1, 2, -1, -1, -1},
290 { 3, 2, 4, 0, 1, 5, 6, -1, -1},
291 { 4, 5, 0, 1, 6, 2, 7, 3, -1},
292 { 3, 2, 4, 0, 1, 5, 7, 6, -1},
295 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
296 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
297 { 0, 1, 2, -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 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
302 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
303 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
304 { 0, 1, 3, 4, 2, -1, -1, -1, -1},
305 { 2, 0, 1, 4, 5, 3, -1, -1, -1},
306 { 2, 3, 0, 1, 5, 6, 4, -1, -1},
307 { 2, 0, 1, 3, 4, 5, 6, -1, -1},
308 { 0, 5, 3, 4, 1, 2, 6, -1, -1},
309 { 3, 2, 4, 0, 1, 6, 7, 5, -1},
310 { 4, 5, 0, 1, 7, 2, 8, 3, 6},
311 { 3, 2, 4, 0, 1, 5, 8, 7, 6},
314 #define DCA_DOLBY 101 /* FIXME */
316 #define DCA_CHANNEL_BITS 6
317 #define DCA_CHANNEL_MASK 0x3F
321 #define HEADER_SIZE 14
323 #define DCA_MAX_FRAME_SIZE 16384
324 #define DCA_MAX_EXSS_HEADER_SIZE 4096
326 #define DCA_BUFFER_PADDING_SIZE 1024
328 #define DCA_NSYNCAUX 0x9A1105A0
330 /** Bit allocation */
332 int offset; ///< code values offset
333 int maxbits[8]; ///< max bits in VLC
334 int wrap; ///< wrap for get_vlc2()
335 VLC vlc[8]; ///< actual codes
338 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
339 static BitAlloc dca_tmode; ///< transition mode VLCs
340 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
341 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
343 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
346 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
351 const AVClass *class; ///< class for AVOptions
352 AVCodecContext *avctx;
354 int frame_type; ///< type of the current frame
355 int samples_deficit; ///< deficit sample count
356 int crc_present; ///< crc is present in the bitstream
357 int sample_blocks; ///< number of PCM sample blocks
358 int frame_size; ///< primary frame byte size
359 int amode; ///< audio channels arrangement
360 int sample_rate; ///< audio sampling rate
361 int bit_rate; ///< transmission bit rate
362 int bit_rate_index; ///< transmission bit rate index
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 int32_t 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 float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2]; ///< stereo downmix coefficients
406 int dynrange_coef; ///< dynamic range coefficient
408 /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
409 * Input: primary audio channels (incl. LFE if present)
410 * Output: downmix audio channels (up to 4, no LFE) */
411 uint8_t core_downmix; ///< embedded downmix coefficients available
412 uint8_t core_downmix_amode; ///< audio channel arrangement of embedded downmix
413 uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4]; ///< embedded downmix coefficients (9-bit codes)
415 int32_t high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
417 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
418 int lfe_scale_factor;
420 /* Subband samples history (for ADPCM) */
421 DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
422 DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
423 DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
424 int hist_index[DCA_PRIM_CHANNELS_MAX];
425 DECLARE_ALIGNED(32, float, raXin)[32];
427 int output; ///< type of output
429 DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
430 float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
431 float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
432 uint8_t *extra_channels_buffer;
433 unsigned int extra_channels_buffer_size;
435 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
436 int dca_buffer_size; ///< how much data is in the dca_buffer
438 const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
440 /* Current position in DCA frame */
441 int current_subframe;
442 int current_subsubframe;
444 int core_ext_mask; ///< present extensions in the core substream
446 /* XCh extension information */
447 int xch_present; ///< XCh extension present and valid
448 int xch_base_channel; ///< index of first (only) channel containing XCH data
449 int xch_disable; ///< whether the XCh extension should be decoded or not
451 /* XXCH extension information */
453 int xxch_nbits_spk_mask;
454 uint32_t xxch_core_spkmask;
455 uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
456 int xxch_chset_nch[4];
457 float xxch_dmix_sf[DCA_CHSETS_MAX];
459 uint32_t xxch_dmix_embedded; /* lower layer has mix pre-embedded, per chset */
460 float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
462 int8_t xxch_order_tab[32];
465 /* ExSS header parser */
466 int static_fields; ///< static fields present
467 int mix_metadata; ///< mixing metadata present
468 int num_mix_configs; ///< number of mix out configurations
469 int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
473 int debug_flag; ///< used for suppressing repeated error messages output
474 AVFloatDSPContext fdsp;
476 SynthFilterContext synth;
477 DCADSPContext dcadsp;
478 FmtConvertContext fmt_conv;
481 static float dca_dmix_code(unsigned code);
483 static const uint16_t dca_vlc_offs[] = {
484 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
485 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
486 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
487 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
488 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
489 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
492 static av_cold void dca_init_vlcs(void)
494 static int vlcs_initialized = 0;
496 static VLC_TYPE dca_table[23622][2];
498 if (vlcs_initialized)
501 dca_bitalloc_index.offset = 1;
502 dca_bitalloc_index.wrap = 2;
503 for (i = 0; i < 5; i++) {
504 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
505 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
506 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
507 bitalloc_12_bits[i], 1, 1,
508 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
510 dca_scalefactor.offset = -64;
511 dca_scalefactor.wrap = 2;
512 for (i = 0; i < 5; i++) {
513 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
514 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
515 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
516 scales_bits[i], 1, 1,
517 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
519 dca_tmode.offset = 0;
521 for (i = 0; i < 4; i++) {
522 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
523 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
524 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
526 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
529 for (i = 0; i < 10; i++)
530 for (j = 0; j < 7; j++) {
531 if (!bitalloc_codes[i][j])
533 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
534 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
535 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
536 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
538 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
540 bitalloc_bits[i][j], 1, 1,
541 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
544 vlcs_initialized = 1;
547 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
550 *dst++ = get_bits(gb, bits);
553 static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
557 /* locate channel set containing the channel */
558 for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
559 i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
560 base += av_popcount(mask);
562 return base + av_popcount(mask & (xxch_ch - 1));
565 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
569 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
570 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
571 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
572 int hdr_pos = 0, hdr_size = 0;
574 int this_chans, acc_mask;
575 int embedded_downmix;
579 /* xxch has arbitrary sized audio coding headers */
581 hdr_pos = get_bits_count(&s->gb);
582 hdr_size = get_bits(&s->gb, 7) + 1;
585 nchans = get_bits(&s->gb, 3) + 1;
586 s->total_channels = nchans + base_channel;
587 s->prim_channels = s->total_channels;
589 /* obtain speaker layout mask & downmix coefficients for XXCH */
591 acc_mask = s->xxch_core_spkmask;
593 this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
594 s->xxch_spk_masks[s->xxch_chset] = this_chans;
595 s->xxch_chset_nch[s->xxch_chset] = nchans;
597 for (i = 0; i <= s->xxch_chset; i++)
598 acc_mask |= s->xxch_spk_masks[i];
600 /* check for downmixing information */
601 if (get_bits1(&s->gb)) {
602 embedded_downmix = get_bits1(&s->gb);
603 coeff = get_bits(&s->gb, 6);
604 scale_factor = -1.0f / dca_dmix_code(FFMAX(coeff<<2, 4)-3);
606 s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
608 for (i = base_channel; i < s->prim_channels; i++) {
609 mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
612 for (j = base_channel; j < s->prim_channels; j++) {
613 memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
614 s->xxch_dmix_embedded |= (embedded_downmix << j);
615 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
616 if (mask[j] & (1 << i)) {
617 if ((1 << i) == DCA_XXCH_LFE1) {
618 av_log(s->avctx, AV_LOG_WARNING,
619 "DCA-XXCH: dmix to LFE1 not supported.\n");
623 coeff = get_bits(&s->gb, 7);
624 ichan = dca_xxch2index(s, 1 << i);
625 s->xxch_dmix_coeff[j][ichan] = dca_dmix_code(FFMAX(coeff<<2, 3)-3);
632 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
633 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
636 for (i = base_channel; i < s->prim_channels; i++) {
637 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
638 if (s->subband_activity[i] > DCA_SUBBANDS)
639 s->subband_activity[i] = DCA_SUBBANDS;
641 for (i = base_channel; i < s->prim_channels; i++) {
642 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
643 if (s->vq_start_subband[i] > DCA_SUBBANDS)
644 s->vq_start_subband[i] = DCA_SUBBANDS;
646 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
647 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
648 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
649 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
651 /* Get codebooks quantization indexes */
653 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
654 for (j = 1; j < 11; j++)
655 for (i = base_channel; i < s->prim_channels; i++)
656 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
658 /* Get scale factor adjustment */
659 for (j = 0; j < 11; j++)
660 for (i = base_channel; i < s->prim_channels; i++)
661 s->scalefactor_adj[i][j] = 1;
663 for (j = 1; j < 11; j++)
664 for (i = base_channel; i < s->prim_channels; i++)
665 if (s->quant_index_huffman[i][j] < thr[j])
666 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
669 if (s->crc_present) {
670 /* Audio header CRC check */
671 get_bits(&s->gb, 16);
674 /* Skip to the end of the header, also ignore CRC if present */
675 i = get_bits_count(&s->gb);
676 if (hdr_pos + 8 * hdr_size > i)
677 skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
680 s->current_subframe = 0;
681 s->current_subsubframe = 0;
684 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
685 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
686 for (i = base_channel; i < s->prim_channels; i++) {
687 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
688 s->subband_activity[i]);
689 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
690 s->vq_start_subband[i]);
691 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
692 s->joint_intensity[i]);
693 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
694 s->transient_huffman[i]);
695 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
696 s->scalefactor_huffman[i]);
697 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
698 s->bitalloc_huffman[i]);
699 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
700 for (j = 0; j < 11; j++)
701 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
702 av_log(s->avctx, AV_LOG_DEBUG, "\n");
703 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
704 for (j = 0; j < 11; j++)
705 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
706 av_log(s->avctx, AV_LOG_DEBUG, "\n");
713 static int dca_parse_frame_header(DCAContext *s)
715 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
718 skip_bits_long(&s->gb, 32);
721 s->frame_type = get_bits(&s->gb, 1);
722 s->samples_deficit = get_bits(&s->gb, 5) + 1;
723 s->crc_present = get_bits(&s->gb, 1);
724 s->sample_blocks = get_bits(&s->gb, 7) + 1;
725 s->frame_size = get_bits(&s->gb, 14) + 1;
726 if (s->frame_size < 95)
727 return AVERROR_INVALIDDATA;
728 s->amode = get_bits(&s->gb, 6);
729 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
731 return AVERROR_INVALIDDATA;
732 s->bit_rate_index = get_bits(&s->gb, 5);
733 s->bit_rate = dca_bit_rates[s->bit_rate_index];
735 return AVERROR_INVALIDDATA;
737 skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
738 s->dynrange = get_bits(&s->gb, 1);
739 s->timestamp = get_bits(&s->gb, 1);
740 s->aux_data = get_bits(&s->gb, 1);
741 s->hdcd = get_bits(&s->gb, 1);
742 s->ext_descr = get_bits(&s->gb, 3);
743 s->ext_coding = get_bits(&s->gb, 1);
744 s->aspf = get_bits(&s->gb, 1);
745 s->lfe = get_bits(&s->gb, 2);
746 s->predictor_history = get_bits(&s->gb, 1);
750 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
751 return AVERROR_INVALIDDATA;
754 /* TODO: check CRC */
756 s->header_crc = get_bits(&s->gb, 16);
758 s->multirate_inter = get_bits(&s->gb, 1);
759 s->version = get_bits(&s->gb, 4);
760 s->copy_history = get_bits(&s->gb, 2);
761 s->source_pcm_res = get_bits(&s->gb, 3);
762 s->front_sum = get_bits(&s->gb, 1);
763 s->surround_sum = get_bits(&s->gb, 1);
764 s->dialog_norm = get_bits(&s->gb, 4);
766 /* FIXME: channels mixing levels */
767 s->output = s->amode;
769 s->output |= DCA_LFE;
772 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
773 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
774 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
775 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
776 s->sample_blocks, s->sample_blocks * 32);
777 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
778 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
779 s->amode, dca_channels[s->amode]);
780 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
782 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
784 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
785 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
786 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
787 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
788 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
789 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
790 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
791 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
792 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
793 s->predictor_history);
794 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
795 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
797 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
798 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
799 av_log(s->avctx, AV_LOG_DEBUG,
800 "source pcm resolution: %i (%i bits/sample)\n",
801 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
802 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
803 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
804 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
805 av_log(s->avctx, AV_LOG_DEBUG, "\n");
808 /* Primary audio coding header */
809 s->subframes = get_bits(&s->gb, 4) + 1;
811 return dca_parse_audio_coding_header(s, 0, 0);
815 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
818 /* huffman encoded */
819 value += get_bitalloc(gb, &dca_scalefactor, level);
820 value = av_clip(value, 0, (1 << log2range) - 1);
821 } else if (level < 8) {
822 if (level + 1 > log2range) {
823 skip_bits(gb, level + 1 - log2range);
824 value = get_bits(gb, log2range);
826 value = get_bits(gb, level + 1);
832 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
834 /* Primary audio coding side information */
837 if (get_bits_left(&s->gb) < 0)
838 return AVERROR_INVALIDDATA;
841 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
842 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
845 for (j = base_channel; j < s->prim_channels; j++) {
846 for (k = 0; k < s->subband_activity[j]; k++)
847 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
850 /* Get prediction codebook */
851 for (j = base_channel; j < s->prim_channels; j++) {
852 for (k = 0; k < s->subband_activity[j]; k++) {
853 if (s->prediction_mode[j][k] > 0) {
854 /* (Prediction coefficient VQ address) */
855 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
860 /* Bit allocation index */
861 for (j = base_channel; j < s->prim_channels; j++) {
862 for (k = 0; k < s->vq_start_subband[j]; k++) {
863 if (s->bitalloc_huffman[j] == 6)
864 s->bitalloc[j][k] = get_bits(&s->gb, 5);
865 else if (s->bitalloc_huffman[j] == 5)
866 s->bitalloc[j][k] = get_bits(&s->gb, 4);
867 else if (s->bitalloc_huffman[j] == 7) {
868 av_log(s->avctx, AV_LOG_ERROR,
869 "Invalid bit allocation index\n");
870 return AVERROR_INVALIDDATA;
873 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
876 if (s->bitalloc[j][k] > 26) {
877 av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
878 j, k, s->bitalloc[j][k]);
879 return AVERROR_INVALIDDATA;
884 /* Transition mode */
885 for (j = base_channel; j < s->prim_channels; j++) {
886 for (k = 0; k < s->subband_activity[j]; k++) {
887 s->transition_mode[j][k] = 0;
888 if (s->subsubframes[s->current_subframe] > 1 &&
889 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
890 s->transition_mode[j][k] =
891 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
896 if (get_bits_left(&s->gb) < 0)
897 return AVERROR_INVALIDDATA;
899 for (j = base_channel; j < s->prim_channels; j++) {
900 const uint32_t *scale_table;
901 int scale_sum, log_size;
903 memset(s->scale_factor[j], 0,
904 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
906 if (s->scalefactor_huffman[j] == 6) {
907 scale_table = scale_factor_quant7;
910 scale_table = scale_factor_quant6;
914 /* When huffman coded, only the difference is encoded */
917 for (k = 0; k < s->subband_activity[j]; k++) {
918 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
919 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
920 s->scale_factor[j][k][0] = scale_table[scale_sum];
923 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
924 /* Get second scale factor */
925 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
926 s->scale_factor[j][k][1] = scale_table[scale_sum];
931 /* Joint subband scale factor codebook select */
932 for (j = base_channel; j < s->prim_channels; j++) {
933 /* Transmitted only if joint subband coding enabled */
934 if (s->joint_intensity[j] > 0)
935 s->joint_huff[j] = get_bits(&s->gb, 3);
938 if (get_bits_left(&s->gb) < 0)
939 return AVERROR_INVALIDDATA;
941 /* Scale factors for joint subband coding */
942 for (j = base_channel; j < s->prim_channels; j++) {
945 /* Transmitted only if joint subband coding enabled */
946 if (s->joint_intensity[j] > 0) {
948 source_channel = s->joint_intensity[j] - 1;
950 /* When huffman coded, only the difference is encoded
951 * (is this valid as well for joint scales ???) */
953 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
954 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
955 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
958 if (!(s->debug_flag & 0x02)) {
959 av_log(s->avctx, AV_LOG_DEBUG,
960 "Joint stereo coding not supported\n");
961 s->debug_flag |= 0x02;
966 /* Dynamic range coefficient */
967 if (!base_channel && s->dynrange)
968 s->dynrange_coef = get_bits(&s->gb, 8);
970 /* Side information CRC check word */
971 if (s->crc_present) {
972 get_bits(&s->gb, 16);
976 * Primary audio data arrays
979 /* VQ encoded high frequency subbands */
980 for (j = base_channel; j < s->prim_channels; j++)
981 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
982 /* 1 vector -> 32 samples */
983 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
985 /* Low frequency effect data */
986 if (!base_channel && s->lfe) {
989 int lfe_samples = 2 * s->lfe * (4 + block_index);
990 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
993 for (j = lfe_samples; j < lfe_end_sample; j++) {
994 /* Signed 8 bits int */
995 s->lfe_data[j] = get_sbits(&s->gb, 8);
998 /* Scale factor index */
999 quant7 = get_bits(&s->gb, 8);
1001 avpriv_request_sample(s->avctx, "LFEScaleIndex larger than 127");
1002 return AVERROR_INVALIDDATA;
1004 s->lfe_scale_factor = scale_factor_quant7[quant7];
1006 /* Quantization step size * scale factor */
1007 lfe_scale = 0.035 * s->lfe_scale_factor;
1009 for (j = lfe_samples; j < lfe_end_sample; j++)
1010 s->lfe_data[j] *= lfe_scale;
1014 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
1015 s->subsubframes[s->current_subframe]);
1016 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
1017 s->partial_samples[s->current_subframe]);
1019 for (j = base_channel; j < s->prim_channels; j++) {
1020 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
1021 for (k = 0; k < s->subband_activity[j]; k++)
1022 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
1023 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1025 for (j = base_channel; j < s->prim_channels; j++) {
1026 for (k = 0; k < s->subband_activity[j]; k++)
1027 av_log(s->avctx, AV_LOG_DEBUG,
1028 "prediction coefs: %f, %f, %f, %f\n",
1029 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
1030 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
1031 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
1032 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
1034 for (j = base_channel; j < s->prim_channels; j++) {
1035 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
1036 for (k = 0; k < s->vq_start_subband[j]; k++)
1037 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
1038 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1040 for (j = base_channel; j < s->prim_channels; j++) {
1041 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
1042 for (k = 0; k < s->subband_activity[j]; k++)
1043 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
1044 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1046 for (j = base_channel; j < s->prim_channels; j++) {
1047 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
1048 for (k = 0; k < s->subband_activity[j]; k++) {
1049 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
1050 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
1051 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
1052 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
1054 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1056 for (j = base_channel; j < s->prim_channels; j++) {
1057 if (s->joint_intensity[j] > 0) {
1058 int source_channel = s->joint_intensity[j] - 1;
1059 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
1060 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
1061 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
1062 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1065 for (j = base_channel; j < s->prim_channels; j++)
1066 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
1067 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
1068 if (!base_channel && s->lfe) {
1069 int lfe_samples = 2 * s->lfe * (4 + block_index);
1070 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1072 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
1073 for (j = lfe_samples; j < lfe_end_sample; j++)
1074 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
1075 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1082 static void qmf_32_subbands(DCAContext *s, int chans,
1083 float samples_in[32][8], float *samples_out,
1086 const float *prCoeff;
1088 int sb_act = s->subband_activity[chans];
1090 scale *= sqrt(1 / 8.0);
1093 if (!s->multirate_inter) /* Non-perfect reconstruction */
1094 prCoeff = fir_32bands_nonperfect;
1095 else /* Perfect reconstruction */
1096 prCoeff = fir_32bands_perfect;
1098 s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
1099 s->subband_fir_hist[chans],
1100 &s->hist_index[chans],
1101 s->subband_fir_noidea[chans], prCoeff,
1102 samples_out, s->raXin, scale);
1105 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
1106 int num_deci_sample, float *samples_in,
1109 /* samples_in: An array holding decimated samples.
1110 * Samples in current subframe starts from samples_in[0],
1111 * while samples_in[-1], samples_in[-2], ..., stores samples
1112 * from last subframe as history.
1114 * samples_out: An array holding interpolated samples
1118 const float *prCoeff;
1121 /* Select decimation filter */
1122 if (decimation_select == 1) {
1124 prCoeff = lfe_fir_128;
1127 prCoeff = lfe_fir_64;
1130 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1131 s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
1133 samples_out += 2 * 32 * (1 + idx);
1137 /* downmixing routines */
1138 #define MIX_REAR1(samples, s1, rs, coef) \
1139 samples[0][i] += samples[s1][i] * coef[rs][0]; \
1140 samples[1][i] += samples[s1][i] * coef[rs][1];
1142 #define MIX_REAR2(samples, s1, s2, rs, coef) \
1143 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
1144 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
1146 #define MIX_FRONT3(samples, coef) \
1147 t = samples[c][i]; \
1148 u = samples[l][i]; \
1149 v = samples[r][i]; \
1150 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1151 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1153 #define DOWNMIX_TO_STEREO(op1, op2) \
1154 for (i = 0; i < 256; i++) { \
1159 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
1160 float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
1161 const int8_t *channel_mapping)
1163 int c, l, r, sl, sr, s;
1170 av_log(NULL, AV_LOG_ERROR, "Not implemented!\n");
1174 case DCA_STEREO_TOTAL:
1175 case DCA_STEREO_SUMDIFF:
1178 c = channel_mapping[0];
1179 l = channel_mapping[1];
1180 r = channel_mapping[2];
1181 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1184 s = channel_mapping[2];
1185 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1188 c = channel_mapping[0];
1189 l = channel_mapping[1];
1190 r = channel_mapping[2];
1191 s = channel_mapping[3];
1192 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1193 MIX_REAR1(samples, s, 3, coef));
1196 sl = channel_mapping[2];
1197 sr = channel_mapping[3];
1198 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1201 c = channel_mapping[0];
1202 l = channel_mapping[1];
1203 r = channel_mapping[2];
1204 sl = channel_mapping[3];
1205 sr = channel_mapping[4];
1206 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1207 MIX_REAR2(samples, sl, sr, 3, coef));
1211 int lf_buf = dca_lfe_index[srcfmt];
1212 int lf_idx = dca_channels [srcfmt];
1213 for (i = 0; i < 256; i++) {
1214 samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
1215 samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
1221 #ifndef decode_blockcodes
1222 /* Very compact version of the block code decoder that does not use table
1223 * look-up but is slightly slower */
1224 static int decode_blockcode(int code, int levels, int32_t *values)
1227 int offset = (levels - 1) >> 1;
1229 for (i = 0; i < 4; i++) {
1230 int div = FASTDIV(code, levels);
1231 values[i] = code - offset - div * levels;
1238 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
1240 return decode_blockcode(code1, levels, values) |
1241 decode_blockcode(code2, levels, values + 4);
1245 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1246 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1248 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1251 int subsubframe = s->current_subsubframe;
1253 const float *quant_step_table;
1256 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1257 LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
1263 /* Select quantization step size table */
1264 if (s->bit_rate_index == 0x1f)
1265 quant_step_table = lossless_quant_d;
1267 quant_step_table = lossy_quant_d;
1269 for (k = base_channel; k < s->prim_channels; k++) {
1270 float rscale[DCA_SUBBANDS];
1272 if (get_bits_left(&s->gb) < 0)
1273 return AVERROR_INVALIDDATA;
1275 for (l = 0; l < s->vq_start_subband[k]; l++) {
1278 /* Select the mid-tread linear quantizer */
1279 int abits = s->bitalloc[k][l];
1281 float quant_step_size = quant_step_table[abits];
1284 * Determine quantization index code book and its type
1287 /* Select quantization index code book */
1288 int sel = s->quant_index_huffman[k][abits];
1291 * Extract bits from the bit stream
1295 memset(block + 8 * l, 0, 8 * sizeof(block[0]));
1297 /* Deal with transients */
1298 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1299 rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
1300 s->scalefactor_adj[k][sel];
1302 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1305 int block_code1, block_code2, size, levels, err;
1307 size = abits_sizes[abits - 1];
1308 levels = abits_levels[abits - 1];
1310 block_code1 = get_bits(&s->gb, size);
1311 block_code2 = get_bits(&s->gb, size);
1312 err = decode_blockcodes(block_code1, block_code2,
1313 levels, block + 8 * l);
1315 av_log(s->avctx, AV_LOG_ERROR,
1316 "ERROR: block code look-up failed\n");
1317 return AVERROR_INVALIDDATA;
1321 for (m = 0; m < 8; m++)
1322 block[8 * l + m] = get_sbits(&s->gb, abits - 3);
1326 for (m = 0; m < 8; m++)
1327 block[8 * l + m] = get_bitalloc(&s->gb,
1328 &dca_smpl_bitalloc[abits], sel);
1334 s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
1335 block, rscale, 8 * s->vq_start_subband[k]);
1337 for (l = 0; l < s->vq_start_subband[k]; l++) {
1340 * Inverse ADPCM if in prediction mode
1342 if (s->prediction_mode[k][l]) {
1344 if (s->predictor_history)
1345 subband_samples[k][l][0] += (adpcm_vb[s->prediction_vq[k][l]][0] *
1346 s->subband_samples_hist[k][l][3] +
1347 adpcm_vb[s->prediction_vq[k][l]][1] *
1348 s->subband_samples_hist[k][l][2] +
1349 adpcm_vb[s->prediction_vq[k][l]][2] *
1350 s->subband_samples_hist[k][l][1] +
1351 adpcm_vb[s->prediction_vq[k][l]][3] *
1352 s->subband_samples_hist[k][l][0]) *
1354 for (m = 1; m < 8; m++) {
1355 float sum = adpcm_vb[s->prediction_vq[k][l]][0] *
1356 subband_samples[k][l][m - 1];
1357 for (n = 2; n <= 4; n++)
1359 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1360 subband_samples[k][l][m - n];
1361 else if (s->predictor_history)
1362 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1363 s->subband_samples_hist[k][l][m - n + 4];
1364 subband_samples[k][l][m] += sum * (1.0f / 8192);
1370 * Decode VQ encoded high frequencies
1372 if (s->subband_activity[k] > s->vq_start_subband[k]) {
1373 if (!(s->debug_flag & 0x01)) {
1374 av_log(s->avctx, AV_LOG_DEBUG,
1375 "Stream with high frequencies VQ coding\n");
1376 s->debug_flag |= 0x01;
1378 s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
1379 high_freq_vq, subsubframe * 8,
1380 s->scale_factor[k], s->vq_start_subband[k],
1381 s->subband_activity[k]);
1385 /* Check for DSYNC after subsubframe */
1386 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1387 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1389 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1392 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1393 return AVERROR_INVALIDDATA;
1397 /* Backup predictor history for adpcm */
1398 for (k = base_channel; k < s->prim_channels; k++)
1399 for (l = 0; l < s->vq_start_subband[k]; l++)
1400 AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
1405 static int dca_filter_channels(DCAContext *s, int block_index)
1407 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1410 /* 32 subbands QMF */
1411 for (k = 0; k < s->prim_channels; k++) {
1412 /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1413 0, 8388608.0, 8388608.0 };*/
1414 if (s->channel_order_tab[k] >= 0)
1415 qmf_32_subbands(s, k, subband_samples[k],
1416 s->samples_chanptr[s->channel_order_tab[k]],
1417 M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
1420 /* Generate LFE samples for this subsubframe FIXME!!! */
1422 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1423 s->lfe_data + 2 * s->lfe * (block_index + 4),
1424 s->samples_chanptr[s->lfe_index]);
1425 /* Outputs 20bits pcm samples */
1428 /* Downmixing to Stereo */
1429 if (s->prim_channels + !!s->lfe > 2 &&
1430 s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1431 dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
1432 s->channel_order_tab);
1439 static int dca_subframe_footer(DCAContext *s, int base_channel)
1441 int in, out, aux_data_count, aux_data_end, reserved;
1445 * Unpack optional information
1448 /* presumably optional information only appears in the core? */
1449 if (!base_channel) {
1451 skip_bits_long(&s->gb, 32);
1454 aux_data_count = get_bits(&s->gb, 6);
1457 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1459 aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1461 if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1462 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1464 return AVERROR_INVALIDDATA;
1467 if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1468 avpriv_request_sample(s->avctx,
1469 "Auxiliary Decode Time Stamp Flag");
1471 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1472 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1473 skip_bits_long(&s->gb, 44);
1476 if ((s->core_downmix = get_bits1(&s->gb))) {
1477 int am = get_bits(&s->gb, 3);
1480 s->core_downmix_amode = DCA_MONO;
1483 s->core_downmix_amode = DCA_STEREO;
1486 s->core_downmix_amode = DCA_STEREO_TOTAL;
1489 s->core_downmix_amode = DCA_3F;
1492 s->core_downmix_amode = DCA_2F1R;
1495 s->core_downmix_amode = DCA_2F2R;
1498 s->core_downmix_amode = DCA_3F1R;
1501 av_log(s->avctx, AV_LOG_ERROR,
1502 "Invalid mode %d for embedded downmix coefficients\n",
1504 return AVERROR_INVALIDDATA;
1506 for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
1507 for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1508 uint16_t tmp = get_bits(&s->gb, 9);
1509 if ((tmp & 0xFF) > 241) {
1510 av_log(s->avctx, AV_LOG_ERROR,
1511 "Invalid downmix coefficient code %"PRIu16"\n",
1513 return AVERROR_INVALIDDATA;
1515 s->core_downmix_codes[in][out] = tmp;
1520 align_get_bits(&s->gb); // byte align
1521 skip_bits(&s->gb, 16); // nAUXCRC16
1523 // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1524 if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
1525 av_log(s->avctx, AV_LOG_ERROR,
1526 "Overread auxiliary data by %d bits\n", -reserved);
1527 return AVERROR_INVALIDDATA;
1528 } else if (reserved) {
1529 avpriv_request_sample(s->avctx,
1530 "Core auxiliary data reserved content");
1531 skip_bits_long(&s->gb, reserved);
1535 if (s->crc_present && s->dynrange)
1536 get_bits(&s->gb, 16);
1543 * Decode a dca frame block
1545 * @param s pointer to the DCAContext
1548 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1553 if (s->current_subframe >= s->subframes) {
1554 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1555 s->current_subframe, s->subframes);
1556 return AVERROR_INVALIDDATA;
1559 if (!s->current_subsubframe) {
1561 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1563 /* Read subframe header */
1564 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1568 /* Read subsubframe */
1570 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1572 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1576 s->current_subsubframe++;
1577 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1578 s->current_subsubframe = 0;
1579 s->current_subframe++;
1581 if (s->current_subframe >= s->subframes) {
1583 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1585 /* Read subframe footer */
1586 if ((ret = dca_subframe_footer(s, base_channel)))
1594 * Return the number of channels in an ExSS speaker mask (HD)
1596 static int dca_exss_mask2count(int mask)
1598 /* count bits that mean speaker pairs twice */
1599 return av_popcount(mask) +
1600 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1601 DCA_EXSS_FRONT_LEFT_RIGHT |
1602 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1603 DCA_EXSS_WIDE_LEFT_RIGHT |
1604 DCA_EXSS_SIDE_LEFT_RIGHT |
1605 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1606 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1607 DCA_EXSS_REAR_LEFT_RIGHT |
1608 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1612 * Skip mixing coefficients of a single mix out configuration (HD)
1614 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1618 for (i = 0; i < channels; i++) {
1619 int mix_map_mask = get_bits(gb, out_ch);
1620 int num_coeffs = av_popcount(mix_map_mask);
1621 skip_bits_long(gb, num_coeffs * 6);
1626 * Parse extension substream asset header (HD)
1628 static int dca_exss_parse_asset_header(DCAContext *s)
1630 int header_pos = get_bits_count(&s->gb);
1633 int embedded_stereo = 0;
1634 int embedded_6ch = 0;
1635 int drc_code_present;
1636 int av_uninit(extensions_mask);
1639 if (get_bits_left(&s->gb) < 16)
1642 /* We will parse just enough to get to the extensions bitmask with which
1643 * we can set the profile value. */
1645 header_size = get_bits(&s->gb, 9) + 1;
1646 skip_bits(&s->gb, 3); // asset index
1648 if (s->static_fields) {
1649 if (get_bits1(&s->gb))
1650 skip_bits(&s->gb, 4); // asset type descriptor
1651 if (get_bits1(&s->gb))
1652 skip_bits_long(&s->gb, 24); // language descriptor
1654 if (get_bits1(&s->gb)) {
1655 /* How can one fit 1024 bytes of text here if the maximum value
1656 * for the asset header size field above was 512 bytes? */
1657 int text_length = get_bits(&s->gb, 10) + 1;
1658 if (get_bits_left(&s->gb) < text_length * 8)
1660 skip_bits_long(&s->gb, text_length * 8); // info text
1663 skip_bits(&s->gb, 5); // bit resolution - 1
1664 skip_bits(&s->gb, 4); // max sample rate code
1665 channels = get_bits(&s->gb, 8) + 1;
1667 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1668 int spkr_remap_sets;
1669 int spkr_mask_size = 16;
1673 embedded_stereo = get_bits1(&s->gb);
1675 embedded_6ch = get_bits1(&s->gb);
1677 if (get_bits1(&s->gb)) {
1678 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1679 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1682 spkr_remap_sets = get_bits(&s->gb, 3);
1684 for (i = 0; i < spkr_remap_sets; i++) {
1685 /* std layout mask for each remap set */
1686 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1689 for (i = 0; i < spkr_remap_sets; i++) {
1690 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1691 if (get_bits_left(&s->gb) < 0)
1694 for (j = 0; j < num_spkrs[i]; j++) {
1695 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1696 int num_dec_ch = av_popcount(remap_dec_ch_mask);
1697 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1702 skip_bits(&s->gb, 3); // representation type
1706 drc_code_present = get_bits1(&s->gb);
1707 if (drc_code_present)
1708 get_bits(&s->gb, 8); // drc code
1710 if (get_bits1(&s->gb))
1711 skip_bits(&s->gb, 5); // dialog normalization code
1713 if (drc_code_present && embedded_stereo)
1714 get_bits(&s->gb, 8); // drc stereo code
1716 if (s->mix_metadata && get_bits1(&s->gb)) {
1717 skip_bits(&s->gb, 1); // external mix
1718 skip_bits(&s->gb, 6); // post mix gain code
1720 if (get_bits(&s->gb, 2) != 3) // mixer drc code
1721 skip_bits(&s->gb, 3); // drc limit
1723 skip_bits(&s->gb, 8); // custom drc code
1725 if (get_bits1(&s->gb)) // channel specific scaling
1726 for (i = 0; i < s->num_mix_configs; i++)
1727 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1729 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1731 for (i = 0; i < s->num_mix_configs; i++) {
1732 if (get_bits_left(&s->gb) < 0)
1734 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1736 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1737 if (embedded_stereo)
1738 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1742 switch (get_bits(&s->gb, 2)) {
1743 case 0: extensions_mask = get_bits(&s->gb, 12); break;
1744 case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1745 case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1746 case 3: extensions_mask = 0; /* aux coding */ break;
1749 /* not parsed further, we were only interested in the extensions mask */
1751 if (get_bits_left(&s->gb) < 0)
1754 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1755 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1758 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1760 if (extensions_mask & DCA_EXT_EXSS_XLL)
1761 s->profile = FF_PROFILE_DTS_HD_MA;
1762 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1764 s->profile = FF_PROFILE_DTS_HD_HRA;
1766 if (!(extensions_mask & DCA_EXT_CORE))
1767 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1768 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1769 av_log(s->avctx, AV_LOG_WARNING,
1770 "DTS extensions detection mismatch (%d, %d)\n",
1771 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1776 static int dca_xbr_parse_frame(DCAContext *s)
1778 int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1779 int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1780 int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1781 int anctemp[DCA_CHSET_CHANS_MAX];
1782 int chset_fsize[DCA_CHSETS_MAX];
1783 int n_xbr_ch[DCA_CHSETS_MAX];
1784 int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1785 int i, j, k, l, chset, chan_base;
1787 av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1789 /* get bit position of sync header */
1790 hdr_pos = get_bits_count(&s->gb) - 32;
1792 hdr_size = get_bits(&s->gb, 6) + 1;
1793 num_chsets = get_bits(&s->gb, 2) + 1;
1795 for(i = 0; i < num_chsets; i++)
1796 chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1798 xbr_tmode = get_bits1(&s->gb);
1800 for(i = 0; i < num_chsets; i++) {
1801 n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1802 k = get_bits(&s->gb, 2) + 5;
1803 for(j = 0; j < n_xbr_ch[i]; j++)
1804 active_bands[i][j] = get_bits(&s->gb, k) + 1;
1807 /* skip to the end of the header */
1808 i = get_bits_count(&s->gb);
1809 if(hdr_pos + hdr_size * 8 > i)
1810 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1812 /* loop over the channel data sets */
1813 /* only decode as many channels as we've decoded base data for */
1814 for(chset = 0, chan_base = 0;
1815 chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1816 chan_base += n_xbr_ch[chset++]) {
1817 int start_posn = get_bits_count(&s->gb);
1818 int subsubframe = 0;
1821 /* loop over subframes */
1822 for (k = 0; k < (s->sample_blocks / 8); k++) {
1823 /* parse header if we're on first subsubframe of a block */
1824 if(subsubframe == 0) {
1825 /* Parse subframe header */
1826 for(i = 0; i < n_xbr_ch[chset]; i++) {
1827 anctemp[i] = get_bits(&s->gb, 2) + 2;
1830 for(i = 0; i < n_xbr_ch[chset]; i++) {
1831 get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1834 for(i = 0; i < n_xbr_ch[chset]; i++) {
1835 anctemp[i] = get_bits(&s->gb, 3);
1836 if(anctemp[i] < 1) {
1837 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1838 return AVERROR_INVALIDDATA;
1842 /* generate scale factors */
1843 for(i = 0; i < n_xbr_ch[chset]; i++) {
1844 const uint32_t *scale_table;
1847 if (s->scalefactor_huffman[chan_base+i] == 6) {
1848 scale_table = scale_factor_quant7;
1850 scale_table = scale_factor_quant6;
1855 for(j = 0; j < active_bands[chset][i]; j++) {
1856 if(abits_high[i][j] > 0) {
1857 scale_table_high[i][j][0] =
1858 scale_table[get_bits(&s->gb, nbits)];
1860 if(xbr_tmode && s->transition_mode[i][j]) {
1861 scale_table_high[i][j][1] =
1862 scale_table[get_bits(&s->gb, nbits)];
1869 /* decode audio array for this block */
1870 for(i = 0; i < n_xbr_ch[chset]; i++) {
1871 for(j = 0; j < active_bands[chset][i]; j++) {
1872 const int xbr_abits = abits_high[i][j];
1873 const float quant_step_size = lossless_quant_d[xbr_abits];
1874 const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1875 const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1876 float *subband_samples = s->subband_samples[k][chan_base+i][j];
1883 get_array(&s->gb, block, 8, xbr_abits - 3);
1885 int block_code1, block_code2, size, levels, err;
1887 size = abits_sizes[xbr_abits - 1];
1888 levels = abits_levels[xbr_abits - 1];
1890 block_code1 = get_bits(&s->gb, size);
1891 block_code2 = get_bits(&s->gb, size);
1892 err = decode_blockcodes(block_code1, block_code2,
1895 av_log(s->avctx, AV_LOG_ERROR,
1896 "ERROR: DTS-XBR: block code look-up failed\n");
1897 return AVERROR_INVALIDDATA;
1901 /* scale & sum into subband */
1902 for(l = 0; l < 8; l++)
1903 subband_samples[l] += (float)block[l] * rscale;
1907 /* check DSYNC marker */
1908 if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1909 if(get_bits(&s->gb, 16) != 0xffff) {
1910 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1911 return AVERROR_INVALIDDATA;
1915 /* advance sub-sub-frame index */
1916 if(++subsubframe >= s->subsubframes[subframe]) {
1922 /* skip to next channel set */
1923 i = get_bits_count(&s->gb);
1924 if(start_posn + chset_fsize[chset] * 8 != i) {
1925 j = start_posn + chset_fsize[chset] * 8 - i;
1927 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1928 " skipping further than expected (%d bits)\n", j);
1929 skip_bits_long(&s->gb, j);
1936 /* parse initial header for XXCH and dump details */
1937 static int dca_xxch_decode_frame(DCAContext *s)
1939 int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1940 int i, chset, base_channel, chstart, fsize[8];
1942 /* assume header word has already been parsed */
1943 hdr_pos = get_bits_count(&s->gb) - 32;
1944 hdr_size = get_bits(&s->gb, 6) + 1;
1945 /*chhdr_crc =*/ skip_bits1(&s->gb);
1946 spkmsk_bits = get_bits(&s->gb, 5) + 1;
1947 num_chsets = get_bits(&s->gb, 2) + 1;
1949 for (i = 0; i < num_chsets; i++)
1950 fsize[i] = get_bits(&s->gb, 14) + 1;
1952 core_spk = get_bits(&s->gb, spkmsk_bits);
1953 s->xxch_core_spkmask = core_spk;
1954 s->xxch_nbits_spk_mask = spkmsk_bits;
1955 s->xxch_dmix_embedded = 0;
1957 /* skip to the end of the header */
1958 i = get_bits_count(&s->gb);
1959 if (hdr_pos + hdr_size * 8 > i)
1960 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1962 for (chset = 0; chset < num_chsets; chset++) {
1963 chstart = get_bits_count(&s->gb);
1964 base_channel = s->prim_channels;
1965 s->xxch_chset = chset;
1967 /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1968 5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1969 dca_parse_audio_coding_header(s, base_channel, 1);
1971 /* decode channel data */
1972 for (i = 0; i < (s->sample_blocks / 8); i++) {
1973 if (dca_decode_block(s, base_channel, i)) {
1974 av_log(s->avctx, AV_LOG_ERROR,
1975 "Error decoding DTS-XXCH extension\n");
1980 /* skip to end of this section */
1981 i = get_bits_count(&s->gb);
1982 if (chstart + fsize[chset] * 8 > i)
1983 skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1985 s->xxch_chset = num_chsets;
1991 * Parse extension substream header (HD)
1993 static void dca_exss_parse_header(DCAContext *s)
2000 int active_ss_mask[8];
2006 if (get_bits_left(&s->gb) < 52)
2009 start_posn = get_bits_count(&s->gb) - 32;
2011 skip_bits(&s->gb, 8); // user data
2012 ss_index = get_bits(&s->gb, 2);
2014 blownup = get_bits1(&s->gb);
2015 hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
2016 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
2018 s->static_fields = get_bits1(&s->gb);
2019 if (s->static_fields) {
2020 skip_bits(&s->gb, 2); // reference clock code
2021 skip_bits(&s->gb, 3); // frame duration code
2023 if (get_bits1(&s->gb))
2024 skip_bits_long(&s->gb, 36); // timestamp
2026 /* a single stream can contain multiple audio assets that can be
2027 * combined to form multiple audio presentations */
2029 num_audiop = get_bits(&s->gb, 3) + 1;
2030 if (num_audiop > 1) {
2031 avpriv_request_sample(s->avctx,
2032 "Multiple DTS-HD audio presentations");
2033 /* ignore such streams for now */
2037 num_assets = get_bits(&s->gb, 3) + 1;
2038 if (num_assets > 1) {
2039 avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
2040 /* ignore such streams for now */
2044 for (i = 0; i < num_audiop; i++)
2045 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
2047 for (i = 0; i < num_audiop; i++)
2048 for (j = 0; j <= ss_index; j++)
2049 if (active_ss_mask[i] & (1 << j))
2050 skip_bits(&s->gb, 8); // active asset mask
2052 s->mix_metadata = get_bits1(&s->gb);
2053 if (s->mix_metadata) {
2054 int mix_out_mask_size;
2056 skip_bits(&s->gb, 2); // adjustment level
2057 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
2058 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
2060 for (i = 0; i < s->num_mix_configs; i++) {
2061 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
2062 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
2067 av_assert0(num_assets > 0); // silence a warning
2069 for (i = 0; i < num_assets; i++)
2070 asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
2072 for (i = 0; i < num_assets; i++) {
2073 if (dca_exss_parse_asset_header(s))
2077 /* not parsed further, we were only interested in the extensions mask
2078 * from the asset header */
2080 j = get_bits_count(&s->gb);
2081 if (start_posn + hdrsize * 8 > j)
2082 skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
2084 for (i = 0; i < num_assets; i++) {
2085 start_posn = get_bits_count(&s->gb);
2086 mkr = get_bits_long(&s->gb, 32);
2088 /* parse extensions that we know about */
2089 if (mkr == 0x655e315e) {
2090 dca_xbr_parse_frame(s);
2091 } else if (mkr == 0x47004a03) {
2092 dca_xxch_decode_frame(s);
2093 s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
2095 av_log(s->avctx, AV_LOG_DEBUG,
2096 "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
2099 /* skip to end of block */
2100 j = get_bits_count(&s->gb);
2101 if (start_posn + asset_size[i] * 8 > j)
2102 skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
2106 static float dca_dmix_code(unsigned code)
2108 int sign = (code >> 8) - 1;
2110 #define POW2_MINUS15 .000030517578125
2111 return ((dca_dmixtable[code] ^ sign) - sign) * POW2_MINUS15;
2115 * Main frame decoding function
2116 * FIXME add arguments
2118 static int dca_decode_frame(AVCodecContext *avctx, void *data,
2119 int *got_frame_ptr, AVPacket *avpkt)
2121 AVFrame *frame = data;
2122 const uint8_t *buf = avpkt->data;
2123 int buf_size = avpkt->size;
2127 int num_core_channels = 0;
2129 float **samples_flt;
2132 DCAContext *s = avctx->priv_data;
2134 int channels, full_channels;
2146 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
2147 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
2148 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
2149 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
2150 return AVERROR_INVALIDDATA;
2153 if ((ret = dca_parse_frame_header(s)) < 0) {
2154 //seems like the frame is corrupt, try with the next one
2157 //set AVCodec values with parsed data
2158 avctx->sample_rate = s->sample_rate;
2159 avctx->bit_rate = s->bit_rate;
2161 s->profile = FF_PROFILE_DTS;
2163 for (i = 0; i < (s->sample_blocks / 8); i++) {
2164 if ((ret = dca_decode_block(s, 0, i))) {
2165 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
2170 /* record number of core channels incase less than max channels are requested */
2171 num_core_channels = s->prim_channels;
2173 if (s->prim_channels + !!s->lfe > 2 &&
2174 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
2175 /* Stereo downmix coefficients
2177 * The decoder can only downmix to 2-channel, so we need to ensure
2178 * embedded downmix coefficients are actually targeting 2-channel.
2180 if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
2181 s->core_downmix_amode == DCA_STEREO_TOTAL)) {
2182 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2183 /* Range checked earlier */
2184 s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
2185 s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
2187 s->output = s->core_downmix_amode;
2189 int am = s->amode & DCA_CHANNEL_MASK;
2190 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
2191 av_log(s->avctx, AV_LOG_ERROR,
2192 "Invalid channel mode %d\n", am);
2193 return AVERROR_INVALIDDATA;
2195 if (num_core_channels + !!s->lfe >
2196 FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
2197 avpriv_request_sample(s->avctx, "Downmixing %d channels",
2198 s->prim_channels + !!s->lfe);
2199 return AVERROR_PATCHWELCOME;
2201 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2202 s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
2203 s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
2206 av_dlog(s->avctx, "Stereo downmix coeffs:\n");
2207 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2208 av_dlog(s->avctx, "L, input channel %d = %f\n", i,
2209 s->downmix_coef[i][0]);
2210 av_dlog(s->avctx, "R, input channel %d = %f\n", i,
2211 s->downmix_coef[i][1]);
2213 av_dlog(s->avctx, "\n");
2217 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
2219 s->core_ext_mask = 0;
2221 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
2223 /* only scan for extensions if ext_descr was unknown or indicated a
2224 * supported XCh extension */
2225 if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
2227 /* if ext_descr was unknown, clear s->core_ext_mask so that the
2228 * extensions scan can fill it up */
2229 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
2231 /* extensions start at 32-bit boundaries into bitstream */
2232 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2234 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
2235 uint32_t bits = get_bits_long(&s->gb, 32);
2239 int ext_amode, xch_fsize;
2241 s->xch_base_channel = s->prim_channels;
2243 /* validate sync word using XCHFSIZE field */
2244 xch_fsize = show_bits(&s->gb, 10);
2245 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
2246 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
2249 /* skip length-to-end-of-frame field for the moment */
2250 skip_bits(&s->gb, 10);
2252 s->core_ext_mask |= DCA_EXT_XCH;
2254 /* extension amode(number of channels in extension) should be 1 */
2255 /* AFAIK XCh is not used for more channels */
2256 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
2257 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
2258 " supported!\n", ext_amode);
2262 if (s->xch_base_channel < 2) {
2263 avpriv_request_sample(avctx, "XCh with fewer than 2 base channels");
2267 /* much like core primary audio coding header */
2268 dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
2270 for (i = 0; i < (s->sample_blocks / 8); i++)
2271 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
2272 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
2280 /* XXCh: extended channels */
2281 /* usually found either in core or HD part in DTS-HD HRA streams,
2282 * but not in DTS-ES which contains XCh extensions instead */
2283 s->core_ext_mask |= DCA_EXT_XXCH;
2284 dca_xxch_decode_frame(s);
2288 int fsize96 = show_bits(&s->gb, 12) + 1;
2289 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
2292 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
2293 get_bits_count(&s->gb));
2294 skip_bits(&s->gb, 12);
2295 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
2296 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
2298 s->core_ext_mask |= DCA_EXT_X96;
2303 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2306 /* no supported extensions, skip the rest of the core substream */
2307 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
2310 if (s->core_ext_mask & DCA_EXT_X96)
2311 s->profile = FF_PROFILE_DTS_96_24;
2312 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
2313 s->profile = FF_PROFILE_DTS_ES;
2315 /* check for ExSS (HD part) */
2316 if (s->dca_buffer_size - s->frame_size > 32 &&
2317 get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
2318 dca_exss_parse_header(s);
2320 avctx->profile = s->profile;
2322 full_channels = channels = s->prim_channels + !!s->lfe;
2324 /* If we have XXCH then the channel layout is managed differently */
2325 /* note that XLL will also have another way to do things */
2326 if (!(s->core_ext_mask & DCA_EXT_XXCH)
2327 || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
2328 && avctx->request_channels
2329 < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
2330 { /* xxx should also do MA extensions */
2331 if (s->amode < 16) {
2332 avctx->channel_layout = dca_core_channel_layout[s->amode];
2334 if (s->prim_channels + !!s->lfe > 2 &&
2335 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
2337 * Neither the core's auxiliary data nor our default tables contain
2338 * downmix coefficients for the additional channel coded in the XCh
2339 * extension, so when we're doing a Stereo downmix, don't decode it.
2344 #if FF_API_REQUEST_CHANNELS
2345 FF_DISABLE_DEPRECATION_WARNINGS
2346 if (s->xch_present && !s->xch_disable &&
2347 (!avctx->request_channels ||
2348 avctx->request_channels > num_core_channels + !!s->lfe)) {
2349 FF_ENABLE_DEPRECATION_WARNINGS
2351 if (s->xch_present && !s->xch_disable) {
2353 avctx->channel_layout |= AV_CH_BACK_CENTER;
2355 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2356 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
2358 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
2360 if (s->channel_order_tab[s->xch_base_channel] < 0)
2361 return AVERROR_INVALIDDATA;
2363 channels = num_core_channels + !!s->lfe;
2364 s->xch_present = 0; /* disable further xch processing */
2366 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2367 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
2369 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
2372 if (channels > !!s->lfe &&
2373 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
2374 return AVERROR_INVALIDDATA;
2376 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != channels) {
2377 av_log(avctx, AV_LOG_ERROR, "Number of channels %d mismatches layout %d\n", channels, av_get_channel_layout_nb_channels(avctx->channel_layout));
2378 return AVERROR_INVALIDDATA;
2381 if (num_core_channels + !!s->lfe > 2 &&
2382 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
2384 s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
2385 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
2387 else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
2388 static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2389 s->channel_order_tab = dca_channel_order_native;
2391 s->lfe_index = dca_lfe_index[s->amode];
2393 av_log(avctx, AV_LOG_ERROR,
2394 "Non standard configuration %d !\n", s->amode);
2395 return AVERROR_INVALIDDATA;
2398 s->xxch_dmix_embedded = 0;
2400 /* we only get here if an XXCH channel set can be added to the mix */
2401 channel_mask = s->xxch_core_spkmask;
2403 if (avctx->request_channels > 0
2404 && avctx->request_channels < s->prim_channels) {
2405 channels = num_core_channels + !!s->lfe;
2406 for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
2407 <= avctx->request_channels; i++) {
2408 channels += s->xxch_chset_nch[i];
2409 channel_mask |= s->xxch_spk_masks[i];
2412 channels = s->prim_channels + !!s->lfe;
2413 for (i = 0; i < s->xxch_chset; i++) {
2414 channel_mask |= s->xxch_spk_masks[i];
2418 /* Given the DTS spec'ed channel mask, generate an avcodec version */
2420 for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
2421 if (channel_mask & (1 << i)) {
2422 channel_layout |= map_xxch_to_native[i];
2426 /* make sure that we have managed to get equivelant dts/avcodec channel
2427 * masks in some sense -- unfortunately some channels could overlap */
2428 if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
2429 av_log(avctx, AV_LOG_DEBUG,
2430 "DTS-XXCH: Inconsistent avcodec/dts channel layouts\n");
2431 return AVERROR_INVALIDDATA;
2434 avctx->channel_layout = channel_layout;
2436 if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
2437 /* Estimate DTS --> avcodec ordering table */
2438 for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
2439 mask = chset >= 0 ? s->xxch_spk_masks[chset]
2440 : s->xxch_core_spkmask;
2441 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
2442 if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
2443 lavc = map_xxch_to_native[i];
2444 posn = av_popcount(channel_layout & (lavc - 1));
2445 s->xxch_order_tab[j++] = posn;
2451 s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
2452 } else { /* native ordering */
2453 for (i = 0; i < channels; i++)
2454 s->xxch_order_tab[i] = i;
2456 s->lfe_index = channels - 1;
2459 s->channel_order_tab = s->xxch_order_tab;
2462 if (avctx->channels != channels) {
2463 if (avctx->channels)
2464 av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
2465 avctx->channels = channels;
2468 /* get output buffer */
2469 frame->nb_samples = 256 * (s->sample_blocks / 8);
2470 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2472 samples_flt = (float **)frame->extended_data;
2474 /* allocate buffer for extra channels if downmixing */
2475 if (avctx->channels < full_channels) {
2476 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
2478 avctx->sample_fmt, 0);
2482 av_fast_malloc(&s->extra_channels_buffer,
2483 &s->extra_channels_buffer_size, ret);
2484 if (!s->extra_channels_buffer)
2485 return AVERROR(ENOMEM);
2487 ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
2488 s->extra_channels_buffer,
2489 full_channels - channels,
2490 frame->nb_samples, avctx->sample_fmt, 0);
2495 /* filter to get final output */
2496 for (i = 0; i < (s->sample_blocks / 8); i++) {
2499 for (ch = 0; ch < channels; ch++)
2500 s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
2501 for (; ch < full_channels; ch++)
2502 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
2504 dca_filter_channels(s, i);
2506 /* If this was marked as a DTS-ES stream we need to subtract back- */
2507 /* channel from SL & SR to remove matrixed back-channel signal */
2508 if ((s->source_pcm_res & 1) && s->xch_present) {
2509 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
2510 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
2511 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
2512 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2513 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2516 /* If stream contains XXCH, we might need to undo an embedded downmix */
2517 if (s->xxch_dmix_embedded) {
2518 /* Loop over channel sets in turn */
2519 ch = num_core_channels;
2520 for (chset = 0; chset < s->xxch_chset; chset++) {
2521 endch = ch + s->xxch_chset_nch[chset];
2522 mask = s->xxch_dmix_embedded;
2525 for (j = ch; j < endch; j++) {
2526 if (mask & (1 << j)) { /* this channel has been mixed-out */
2527 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2528 for (k = 0; k < endch; k++) {
2529 achan = s->channel_order_tab[k];
2530 scale = s->xxch_dmix_coeff[j][k];
2532 dst_chan = s->samples_chanptr[achan];
2533 s->fdsp.vector_fmac_scalar(dst_chan, src_chan,
2540 /* if a downmix has been embedded then undo the pre-scaling */
2541 if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
2542 scale = s->xxch_dmix_sf[chset];
2544 for (j = 0; j < ch; j++) {
2545 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2546 for (k = 0; k < 256; k++)
2547 src_chan[k] *= scale;
2550 /* LFE channel is always part of core, scale if it exists */
2552 src_chan = s->samples_chanptr[s->lfe_index];
2553 for (k = 0; k < 256; k++)
2554 src_chan[k] *= scale;
2564 /* update lfe history */
2565 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2566 for (i = 0; i < 2 * s->lfe * 4; i++)
2567 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2571 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
2572 ret = ff_side_data_update_matrix_encoding(frame,
2573 (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
2574 AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
2586 * DCA initialization
2588 * @param avctx pointer to the AVCodecContext
2591 static av_cold int dca_decode_init(AVCodecContext *avctx)
2593 DCAContext *s = avctx->priv_data;
2598 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
2599 ff_mdct_init(&s->imdct, 6, 1, 1.0);
2600 ff_synth_filter_init(&s->synth);
2601 ff_dcadsp_init(&s->dcadsp);
2602 ff_fmt_convert_init(&s->fmt_conv, avctx);
2604 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2606 /* allow downmixing to stereo */
2607 #if FF_API_REQUEST_CHANNELS
2608 FF_DISABLE_DEPRECATION_WARNINGS
2609 if (avctx->request_channels == 2)
2610 avctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
2611 FF_ENABLE_DEPRECATION_WARNINGS
2613 if (avctx->channels > 2 &&
2614 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
2615 avctx->channels = 2;
2620 static av_cold int dca_decode_end(AVCodecContext *avctx)
2622 DCAContext *s = avctx->priv_data;
2623 ff_mdct_end(&s->imdct);
2624 av_freep(&s->extra_channels_buffer);
2628 static const AVProfile profiles[] = {
2629 { FF_PROFILE_DTS, "DTS" },
2630 { FF_PROFILE_DTS_ES, "DTS-ES" },
2631 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2632 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2633 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2634 { FF_PROFILE_UNKNOWN },
2637 static const AVOption options[] = {
2638 { "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 },
2642 static const AVClass dca_decoder_class = {
2643 .class_name = "DCA decoder",
2644 .item_name = av_default_item_name,
2646 .version = LIBAVUTIL_VERSION_INT,
2647 .category = AV_CLASS_CATEGORY_DECODER,
2650 AVCodec ff_dca_decoder = {
2652 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2653 .type = AVMEDIA_TYPE_AUDIO,
2654 .id = AV_CODEC_ID_DTS,
2655 .priv_data_size = sizeof(DCAContext),
2656 .init = dca_decode_init,
2657 .decode = dca_decode_frame,
2658 .close = dca_decode_end,
2659 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2660 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2661 AV_SAMPLE_FMT_NONE },
2662 .profiles = NULL_IF_CONFIG_SMALL(profiles),
2663 .priv_class = &dca_decoder_class,