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);
605 if (coeff<1 || coeff>61) {
606 av_log(s->avctx, AV_LOG_ERROR, "6bit coeff %d is out of range\n", coeff);
607 return AVERROR_INVALIDDATA;
610 scale_factor = -1.0f / dca_dmix_code((coeff<<2)-3);
612 s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
614 for (i = base_channel; i < s->prim_channels; i++) {
615 mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
618 for (j = base_channel; j < s->prim_channels; j++) {
619 memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
620 s->xxch_dmix_embedded |= (embedded_downmix << j);
621 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
622 if (mask[j] & (1 << i)) {
623 if ((1 << i) == DCA_XXCH_LFE1) {
624 av_log(s->avctx, AV_LOG_WARNING,
625 "DCA-XXCH: dmix to LFE1 not supported.\n");
629 coeff = get_bits(&s->gb, 7);
630 ichan = dca_xxch2index(s, 1 << i);
631 if ((coeff&63)<1 || (coeff&63)>61) {
632 av_log(s->avctx, AV_LOG_ERROR, "7bit coeff %d is out of range\n", coeff);
633 return AVERROR_INVALIDDATA;
635 s->xxch_dmix_coeff[j][ichan] = dca_dmix_code((coeff<<2)-3);
642 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
643 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
646 for (i = base_channel; i < s->prim_channels; i++) {
647 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
648 if (s->subband_activity[i] > DCA_SUBBANDS)
649 s->subband_activity[i] = DCA_SUBBANDS;
651 for (i = base_channel; i < s->prim_channels; i++) {
652 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
653 if (s->vq_start_subband[i] > DCA_SUBBANDS)
654 s->vq_start_subband[i] = DCA_SUBBANDS;
656 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
657 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
658 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
659 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
661 /* Get codebooks quantization indexes */
663 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
664 for (j = 1; j < 11; j++)
665 for (i = base_channel; i < s->prim_channels; i++)
666 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
668 /* Get scale factor adjustment */
669 for (j = 0; j < 11; j++)
670 for (i = base_channel; i < s->prim_channels; i++)
671 s->scalefactor_adj[i][j] = 1;
673 for (j = 1; j < 11; j++)
674 for (i = base_channel; i < s->prim_channels; i++)
675 if (s->quant_index_huffman[i][j] < thr[j])
676 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
679 if (s->crc_present) {
680 /* Audio header CRC check */
681 get_bits(&s->gb, 16);
684 /* Skip to the end of the header, also ignore CRC if present */
685 i = get_bits_count(&s->gb);
686 if (hdr_pos + 8 * hdr_size > i)
687 skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
690 s->current_subframe = 0;
691 s->current_subsubframe = 0;
694 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
695 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
696 for (i = base_channel; i < s->prim_channels; i++) {
697 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
698 s->subband_activity[i]);
699 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
700 s->vq_start_subband[i]);
701 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
702 s->joint_intensity[i]);
703 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
704 s->transient_huffman[i]);
705 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
706 s->scalefactor_huffman[i]);
707 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
708 s->bitalloc_huffman[i]);
709 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
710 for (j = 0; j < 11; j++)
711 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
712 av_log(s->avctx, AV_LOG_DEBUG, "\n");
713 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
714 for (j = 0; j < 11; j++)
715 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
716 av_log(s->avctx, AV_LOG_DEBUG, "\n");
723 static int dca_parse_frame_header(DCAContext *s)
725 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
728 skip_bits_long(&s->gb, 32);
731 s->frame_type = get_bits(&s->gb, 1);
732 s->samples_deficit = get_bits(&s->gb, 5) + 1;
733 s->crc_present = get_bits(&s->gb, 1);
734 s->sample_blocks = get_bits(&s->gb, 7) + 1;
735 s->frame_size = get_bits(&s->gb, 14) + 1;
736 if (s->frame_size < 95)
737 return AVERROR_INVALIDDATA;
738 s->amode = get_bits(&s->gb, 6);
739 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
741 return AVERROR_INVALIDDATA;
742 s->bit_rate_index = get_bits(&s->gb, 5);
743 s->bit_rate = dca_bit_rates[s->bit_rate_index];
745 return AVERROR_INVALIDDATA;
747 skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
748 s->dynrange = get_bits(&s->gb, 1);
749 s->timestamp = get_bits(&s->gb, 1);
750 s->aux_data = get_bits(&s->gb, 1);
751 s->hdcd = get_bits(&s->gb, 1);
752 s->ext_descr = get_bits(&s->gb, 3);
753 s->ext_coding = get_bits(&s->gb, 1);
754 s->aspf = get_bits(&s->gb, 1);
755 s->lfe = get_bits(&s->gb, 2);
756 s->predictor_history = get_bits(&s->gb, 1);
760 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
761 return AVERROR_INVALIDDATA;
764 /* TODO: check CRC */
766 s->header_crc = get_bits(&s->gb, 16);
768 s->multirate_inter = get_bits(&s->gb, 1);
769 s->version = get_bits(&s->gb, 4);
770 s->copy_history = get_bits(&s->gb, 2);
771 s->source_pcm_res = get_bits(&s->gb, 3);
772 s->front_sum = get_bits(&s->gb, 1);
773 s->surround_sum = get_bits(&s->gb, 1);
774 s->dialog_norm = get_bits(&s->gb, 4);
776 /* FIXME: channels mixing levels */
777 s->output = s->amode;
779 s->output |= DCA_LFE;
782 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
783 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
784 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
785 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
786 s->sample_blocks, s->sample_blocks * 32);
787 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
788 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
789 s->amode, dca_channels[s->amode]);
790 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
792 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
794 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
795 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
796 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
797 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
798 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
799 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
800 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
801 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
802 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
803 s->predictor_history);
804 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
805 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
807 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
808 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
809 av_log(s->avctx, AV_LOG_DEBUG,
810 "source pcm resolution: %i (%i bits/sample)\n",
811 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
812 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
813 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
814 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
815 av_log(s->avctx, AV_LOG_DEBUG, "\n");
818 /* Primary audio coding header */
819 s->subframes = get_bits(&s->gb, 4) + 1;
821 return dca_parse_audio_coding_header(s, 0, 0);
825 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
828 /* huffman encoded */
829 value += get_bitalloc(gb, &dca_scalefactor, level);
830 value = av_clip(value, 0, (1 << log2range) - 1);
831 } else if (level < 8) {
832 if (level + 1 > log2range) {
833 skip_bits(gb, level + 1 - log2range);
834 value = get_bits(gb, log2range);
836 value = get_bits(gb, level + 1);
842 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
844 /* Primary audio coding side information */
847 if (get_bits_left(&s->gb) < 0)
848 return AVERROR_INVALIDDATA;
851 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
852 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
855 for (j = base_channel; j < s->prim_channels; j++) {
856 for (k = 0; k < s->subband_activity[j]; k++)
857 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
860 /* Get prediction codebook */
861 for (j = base_channel; j < s->prim_channels; j++) {
862 for (k = 0; k < s->subband_activity[j]; k++) {
863 if (s->prediction_mode[j][k] > 0) {
864 /* (Prediction coefficient VQ address) */
865 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
870 /* Bit allocation index */
871 for (j = base_channel; j < s->prim_channels; j++) {
872 for (k = 0; k < s->vq_start_subband[j]; k++) {
873 if (s->bitalloc_huffman[j] == 6)
874 s->bitalloc[j][k] = get_bits(&s->gb, 5);
875 else if (s->bitalloc_huffman[j] == 5)
876 s->bitalloc[j][k] = get_bits(&s->gb, 4);
877 else if (s->bitalloc_huffman[j] == 7) {
878 av_log(s->avctx, AV_LOG_ERROR,
879 "Invalid bit allocation index\n");
880 return AVERROR_INVALIDDATA;
883 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
886 if (s->bitalloc[j][k] > 26) {
887 av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
888 j, k, s->bitalloc[j][k]);
889 return AVERROR_INVALIDDATA;
894 /* Transition mode */
895 for (j = base_channel; j < s->prim_channels; j++) {
896 for (k = 0; k < s->subband_activity[j]; k++) {
897 s->transition_mode[j][k] = 0;
898 if (s->subsubframes[s->current_subframe] > 1 &&
899 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
900 s->transition_mode[j][k] =
901 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
906 if (get_bits_left(&s->gb) < 0)
907 return AVERROR_INVALIDDATA;
909 for (j = base_channel; j < s->prim_channels; j++) {
910 const uint32_t *scale_table;
911 int scale_sum, log_size;
913 memset(s->scale_factor[j], 0,
914 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
916 if (s->scalefactor_huffman[j] == 6) {
917 scale_table = scale_factor_quant7;
920 scale_table = scale_factor_quant6;
924 /* When huffman coded, only the difference is encoded */
927 for (k = 0; k < s->subband_activity[j]; k++) {
928 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
929 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
930 s->scale_factor[j][k][0] = scale_table[scale_sum];
933 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
934 /* Get second scale factor */
935 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
936 s->scale_factor[j][k][1] = scale_table[scale_sum];
941 /* Joint subband scale factor codebook select */
942 for (j = base_channel; j < s->prim_channels; j++) {
943 /* Transmitted only if joint subband coding enabled */
944 if (s->joint_intensity[j] > 0)
945 s->joint_huff[j] = get_bits(&s->gb, 3);
948 if (get_bits_left(&s->gb) < 0)
949 return AVERROR_INVALIDDATA;
951 /* Scale factors for joint subband coding */
952 for (j = base_channel; j < s->prim_channels; j++) {
955 /* Transmitted only if joint subband coding enabled */
956 if (s->joint_intensity[j] > 0) {
958 source_channel = s->joint_intensity[j] - 1;
960 /* When huffman coded, only the difference is encoded
961 * (is this valid as well for joint scales ???) */
963 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
964 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
965 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
968 if (!(s->debug_flag & 0x02)) {
969 av_log(s->avctx, AV_LOG_DEBUG,
970 "Joint stereo coding not supported\n");
971 s->debug_flag |= 0x02;
976 /* Dynamic range coefficient */
977 if (!base_channel && s->dynrange)
978 s->dynrange_coef = get_bits(&s->gb, 8);
980 /* Side information CRC check word */
981 if (s->crc_present) {
982 get_bits(&s->gb, 16);
986 * Primary audio data arrays
989 /* VQ encoded high frequency subbands */
990 for (j = base_channel; j < s->prim_channels; j++)
991 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
992 /* 1 vector -> 32 samples */
993 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
995 /* Low frequency effect data */
996 if (!base_channel && s->lfe) {
999 int lfe_samples = 2 * s->lfe * (4 + block_index);
1000 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1003 for (j = lfe_samples; j < lfe_end_sample; j++) {
1004 /* Signed 8 bits int */
1005 s->lfe_data[j] = get_sbits(&s->gb, 8);
1008 /* Scale factor index */
1009 quant7 = get_bits(&s->gb, 8);
1011 avpriv_request_sample(s->avctx, "LFEScaleIndex larger than 127");
1012 return AVERROR_INVALIDDATA;
1014 s->lfe_scale_factor = scale_factor_quant7[quant7];
1016 /* Quantization step size * scale factor */
1017 lfe_scale = 0.035 * s->lfe_scale_factor;
1019 for (j = lfe_samples; j < lfe_end_sample; j++)
1020 s->lfe_data[j] *= lfe_scale;
1024 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
1025 s->subsubframes[s->current_subframe]);
1026 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
1027 s->partial_samples[s->current_subframe]);
1029 for (j = base_channel; j < s->prim_channels; j++) {
1030 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
1031 for (k = 0; k < s->subband_activity[j]; k++)
1032 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
1033 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1035 for (j = base_channel; j < s->prim_channels; j++) {
1036 for (k = 0; k < s->subband_activity[j]; k++)
1037 av_log(s->avctx, AV_LOG_DEBUG,
1038 "prediction coefs: %f, %f, %f, %f\n",
1039 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
1040 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
1041 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
1042 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
1044 for (j = base_channel; j < s->prim_channels; j++) {
1045 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
1046 for (k = 0; k < s->vq_start_subband[j]; k++)
1047 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
1048 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1050 for (j = base_channel; j < s->prim_channels; j++) {
1051 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
1052 for (k = 0; k < s->subband_activity[j]; k++)
1053 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
1054 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1056 for (j = base_channel; j < s->prim_channels; j++) {
1057 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
1058 for (k = 0; k < s->subband_activity[j]; k++) {
1059 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
1060 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
1061 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
1062 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
1064 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1066 for (j = base_channel; j < s->prim_channels; j++) {
1067 if (s->joint_intensity[j] > 0) {
1068 int source_channel = s->joint_intensity[j] - 1;
1069 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
1070 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
1071 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
1072 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1075 for (j = base_channel; j < s->prim_channels; j++)
1076 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
1077 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
1078 if (!base_channel && s->lfe) {
1079 int lfe_samples = 2 * s->lfe * (4 + block_index);
1080 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1082 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
1083 for (j = lfe_samples; j < lfe_end_sample; j++)
1084 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
1085 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1092 static void qmf_32_subbands(DCAContext *s, int chans,
1093 float samples_in[32][8], float *samples_out,
1096 const float *prCoeff;
1098 int sb_act = s->subband_activity[chans];
1100 scale *= sqrt(1 / 8.0);
1103 if (!s->multirate_inter) /* Non-perfect reconstruction */
1104 prCoeff = fir_32bands_nonperfect;
1105 else /* Perfect reconstruction */
1106 prCoeff = fir_32bands_perfect;
1108 s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
1109 s->subband_fir_hist[chans],
1110 &s->hist_index[chans],
1111 s->subband_fir_noidea[chans], prCoeff,
1112 samples_out, s->raXin, scale);
1115 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
1116 int num_deci_sample, float *samples_in,
1119 /* samples_in: An array holding decimated samples.
1120 * Samples in current subframe starts from samples_in[0],
1121 * while samples_in[-1], samples_in[-2], ..., stores samples
1122 * from last subframe as history.
1124 * samples_out: An array holding interpolated samples
1128 const float *prCoeff;
1131 /* Select decimation filter */
1132 if (decimation_select == 1) {
1134 prCoeff = lfe_fir_128;
1137 prCoeff = lfe_fir_64;
1140 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1141 s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
1143 samples_out += 2 * 32 * (1 + idx);
1147 /* downmixing routines */
1148 #define MIX_REAR1(samples, s1, rs, coef) \
1149 samples[0][i] += samples[s1][i] * coef[rs][0]; \
1150 samples[1][i] += samples[s1][i] * coef[rs][1];
1152 #define MIX_REAR2(samples, s1, s2, rs, coef) \
1153 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
1154 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
1156 #define MIX_FRONT3(samples, coef) \
1157 t = samples[c][i]; \
1158 u = samples[l][i]; \
1159 v = samples[r][i]; \
1160 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1161 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1163 #define DOWNMIX_TO_STEREO(op1, op2) \
1164 for (i = 0; i < 256; i++) { \
1169 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
1170 float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
1171 const int8_t *channel_mapping)
1173 int c, l, r, sl, sr, s;
1180 av_log(NULL, AV_LOG_ERROR, "Not implemented!\n");
1184 case DCA_STEREO_TOTAL:
1185 case DCA_STEREO_SUMDIFF:
1188 c = channel_mapping[0];
1189 l = channel_mapping[1];
1190 r = channel_mapping[2];
1191 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1194 s = channel_mapping[2];
1195 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1198 c = channel_mapping[0];
1199 l = channel_mapping[1];
1200 r = channel_mapping[2];
1201 s = channel_mapping[3];
1202 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1203 MIX_REAR1(samples, s, 3, coef));
1206 sl = channel_mapping[2];
1207 sr = channel_mapping[3];
1208 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1211 c = channel_mapping[0];
1212 l = channel_mapping[1];
1213 r = channel_mapping[2];
1214 sl = channel_mapping[3];
1215 sr = channel_mapping[4];
1216 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1217 MIX_REAR2(samples, sl, sr, 3, coef));
1221 int lf_buf = dca_lfe_index[srcfmt];
1222 int lf_idx = dca_channels [srcfmt];
1223 for (i = 0; i < 256; i++) {
1224 samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
1225 samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
1231 #ifndef decode_blockcodes
1232 /* Very compact version of the block code decoder that does not use table
1233 * look-up but is slightly slower */
1234 static int decode_blockcode(int code, int levels, int32_t *values)
1237 int offset = (levels - 1) >> 1;
1239 for (i = 0; i < 4; i++) {
1240 int div = FASTDIV(code, levels);
1241 values[i] = code - offset - div * levels;
1248 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
1250 return decode_blockcode(code1, levels, values) |
1251 decode_blockcode(code2, levels, values + 4);
1255 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1256 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1258 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1261 int subsubframe = s->current_subsubframe;
1263 const float *quant_step_table;
1266 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1267 LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
1273 /* Select quantization step size table */
1274 if (s->bit_rate_index == 0x1f)
1275 quant_step_table = lossless_quant_d;
1277 quant_step_table = lossy_quant_d;
1279 for (k = base_channel; k < s->prim_channels; k++) {
1280 float rscale[DCA_SUBBANDS];
1282 if (get_bits_left(&s->gb) < 0)
1283 return AVERROR_INVALIDDATA;
1285 for (l = 0; l < s->vq_start_subband[k]; l++) {
1288 /* Select the mid-tread linear quantizer */
1289 int abits = s->bitalloc[k][l];
1291 float quant_step_size = quant_step_table[abits];
1294 * Determine quantization index code book and its type
1297 /* Select quantization index code book */
1298 int sel = s->quant_index_huffman[k][abits];
1301 * Extract bits from the bit stream
1305 memset(block + 8 * l, 0, 8 * sizeof(block[0]));
1307 /* Deal with transients */
1308 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1309 rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
1310 s->scalefactor_adj[k][sel];
1312 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1315 int block_code1, block_code2, size, levels, err;
1317 size = abits_sizes[abits - 1];
1318 levels = abits_levels[abits - 1];
1320 block_code1 = get_bits(&s->gb, size);
1321 block_code2 = get_bits(&s->gb, size);
1322 err = decode_blockcodes(block_code1, block_code2,
1323 levels, block + 8 * l);
1325 av_log(s->avctx, AV_LOG_ERROR,
1326 "ERROR: block code look-up failed\n");
1327 return AVERROR_INVALIDDATA;
1331 for (m = 0; m < 8; m++)
1332 block[8 * l + m] = get_sbits(&s->gb, abits - 3);
1336 for (m = 0; m < 8; m++)
1337 block[8 * l + m] = get_bitalloc(&s->gb,
1338 &dca_smpl_bitalloc[abits], sel);
1344 s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
1345 block, rscale, 8 * s->vq_start_subband[k]);
1347 for (l = 0; l < s->vq_start_subband[k]; l++) {
1350 * Inverse ADPCM if in prediction mode
1352 if (s->prediction_mode[k][l]) {
1354 if (s->predictor_history)
1355 subband_samples[k][l][0] += (adpcm_vb[s->prediction_vq[k][l]][0] *
1356 s->subband_samples_hist[k][l][3] +
1357 adpcm_vb[s->prediction_vq[k][l]][1] *
1358 s->subband_samples_hist[k][l][2] +
1359 adpcm_vb[s->prediction_vq[k][l]][2] *
1360 s->subband_samples_hist[k][l][1] +
1361 adpcm_vb[s->prediction_vq[k][l]][3] *
1362 s->subband_samples_hist[k][l][0]) *
1364 for (m = 1; m < 8; m++) {
1365 float sum = adpcm_vb[s->prediction_vq[k][l]][0] *
1366 subband_samples[k][l][m - 1];
1367 for (n = 2; n <= 4; n++)
1369 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1370 subband_samples[k][l][m - n];
1371 else if (s->predictor_history)
1372 sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1373 s->subband_samples_hist[k][l][m - n + 4];
1374 subband_samples[k][l][m] += sum * (1.0f / 8192);
1380 * Decode VQ encoded high frequencies
1382 if (s->subband_activity[k] > s->vq_start_subband[k]) {
1383 if (!(s->debug_flag & 0x01)) {
1384 av_log(s->avctx, AV_LOG_DEBUG,
1385 "Stream with high frequencies VQ coding\n");
1386 s->debug_flag |= 0x01;
1388 s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
1389 high_freq_vq, subsubframe * 8,
1390 s->scale_factor[k], s->vq_start_subband[k],
1391 s->subband_activity[k]);
1395 /* Check for DSYNC after subsubframe */
1396 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1397 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1399 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1402 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1403 return AVERROR_INVALIDDATA;
1407 /* Backup predictor history for adpcm */
1408 for (k = base_channel; k < s->prim_channels; k++)
1409 for (l = 0; l < s->vq_start_subband[k]; l++)
1410 AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
1415 static int dca_filter_channels(DCAContext *s, int block_index)
1417 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1420 /* 32 subbands QMF */
1421 for (k = 0; k < s->prim_channels; k++) {
1422 /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1423 0, 8388608.0, 8388608.0 };*/
1424 if (s->channel_order_tab[k] >= 0)
1425 qmf_32_subbands(s, k, subband_samples[k],
1426 s->samples_chanptr[s->channel_order_tab[k]],
1427 M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
1430 /* Generate LFE samples for this subsubframe FIXME!!! */
1432 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1433 s->lfe_data + 2 * s->lfe * (block_index + 4),
1434 s->samples_chanptr[s->lfe_index]);
1435 /* Outputs 20bits pcm samples */
1438 /* Downmixing to Stereo */
1439 if (s->prim_channels + !!s->lfe > 2 &&
1440 s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1441 dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
1442 s->channel_order_tab);
1449 static int dca_subframe_footer(DCAContext *s, int base_channel)
1451 int in, out, aux_data_count, aux_data_end, reserved;
1455 * Unpack optional information
1458 /* presumably optional information only appears in the core? */
1459 if (!base_channel) {
1461 skip_bits_long(&s->gb, 32);
1464 aux_data_count = get_bits(&s->gb, 6);
1467 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1469 aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1471 if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1472 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1474 return AVERROR_INVALIDDATA;
1477 if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1478 avpriv_request_sample(s->avctx,
1479 "Auxiliary Decode Time Stamp Flag");
1481 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1482 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1483 skip_bits_long(&s->gb, 44);
1486 if ((s->core_downmix = get_bits1(&s->gb))) {
1487 int am = get_bits(&s->gb, 3);
1490 s->core_downmix_amode = DCA_MONO;
1493 s->core_downmix_amode = DCA_STEREO;
1496 s->core_downmix_amode = DCA_STEREO_TOTAL;
1499 s->core_downmix_amode = DCA_3F;
1502 s->core_downmix_amode = DCA_2F1R;
1505 s->core_downmix_amode = DCA_2F2R;
1508 s->core_downmix_amode = DCA_3F1R;
1511 av_log(s->avctx, AV_LOG_ERROR,
1512 "Invalid mode %d for embedded downmix coefficients\n",
1514 return AVERROR_INVALIDDATA;
1516 for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
1517 for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1518 uint16_t tmp = get_bits(&s->gb, 9);
1519 if ((tmp & 0xFF) > 241) {
1520 av_log(s->avctx, AV_LOG_ERROR,
1521 "Invalid downmix coefficient code %"PRIu16"\n",
1523 return AVERROR_INVALIDDATA;
1525 s->core_downmix_codes[in][out] = tmp;
1530 align_get_bits(&s->gb); // byte align
1531 skip_bits(&s->gb, 16); // nAUXCRC16
1533 // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1534 if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
1535 av_log(s->avctx, AV_LOG_ERROR,
1536 "Overread auxiliary data by %d bits\n", -reserved);
1537 return AVERROR_INVALIDDATA;
1538 } else if (reserved) {
1539 avpriv_request_sample(s->avctx,
1540 "Core auxiliary data reserved content");
1541 skip_bits_long(&s->gb, reserved);
1545 if (s->crc_present && s->dynrange)
1546 get_bits(&s->gb, 16);
1553 * Decode a dca frame block
1555 * @param s pointer to the DCAContext
1558 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1563 if (s->current_subframe >= s->subframes) {
1564 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1565 s->current_subframe, s->subframes);
1566 return AVERROR_INVALIDDATA;
1569 if (!s->current_subsubframe) {
1571 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1573 /* Read subframe header */
1574 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1578 /* Read subsubframe */
1580 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1582 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1586 s->current_subsubframe++;
1587 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1588 s->current_subsubframe = 0;
1589 s->current_subframe++;
1591 if (s->current_subframe >= s->subframes) {
1593 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1595 /* Read subframe footer */
1596 if ((ret = dca_subframe_footer(s, base_channel)))
1604 * Return the number of channels in an ExSS speaker mask (HD)
1606 static int dca_exss_mask2count(int mask)
1608 /* count bits that mean speaker pairs twice */
1609 return av_popcount(mask) +
1610 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1611 DCA_EXSS_FRONT_LEFT_RIGHT |
1612 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1613 DCA_EXSS_WIDE_LEFT_RIGHT |
1614 DCA_EXSS_SIDE_LEFT_RIGHT |
1615 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1616 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1617 DCA_EXSS_REAR_LEFT_RIGHT |
1618 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1622 * Skip mixing coefficients of a single mix out configuration (HD)
1624 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1628 for (i = 0; i < channels; i++) {
1629 int mix_map_mask = get_bits(gb, out_ch);
1630 int num_coeffs = av_popcount(mix_map_mask);
1631 skip_bits_long(gb, num_coeffs * 6);
1636 * Parse extension substream asset header (HD)
1638 static int dca_exss_parse_asset_header(DCAContext *s)
1640 int header_pos = get_bits_count(&s->gb);
1643 int embedded_stereo = 0;
1644 int embedded_6ch = 0;
1645 int drc_code_present;
1646 int av_uninit(extensions_mask);
1649 if (get_bits_left(&s->gb) < 16)
1652 /* We will parse just enough to get to the extensions bitmask with which
1653 * we can set the profile value. */
1655 header_size = get_bits(&s->gb, 9) + 1;
1656 skip_bits(&s->gb, 3); // asset index
1658 if (s->static_fields) {
1659 if (get_bits1(&s->gb))
1660 skip_bits(&s->gb, 4); // asset type descriptor
1661 if (get_bits1(&s->gb))
1662 skip_bits_long(&s->gb, 24); // language descriptor
1664 if (get_bits1(&s->gb)) {
1665 /* How can one fit 1024 bytes of text here if the maximum value
1666 * for the asset header size field above was 512 bytes? */
1667 int text_length = get_bits(&s->gb, 10) + 1;
1668 if (get_bits_left(&s->gb) < text_length * 8)
1670 skip_bits_long(&s->gb, text_length * 8); // info text
1673 skip_bits(&s->gb, 5); // bit resolution - 1
1674 skip_bits(&s->gb, 4); // max sample rate code
1675 channels = get_bits(&s->gb, 8) + 1;
1677 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1678 int spkr_remap_sets;
1679 int spkr_mask_size = 16;
1683 embedded_stereo = get_bits1(&s->gb);
1685 embedded_6ch = get_bits1(&s->gb);
1687 if (get_bits1(&s->gb)) {
1688 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1689 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1692 spkr_remap_sets = get_bits(&s->gb, 3);
1694 for (i = 0; i < spkr_remap_sets; i++) {
1695 /* std layout mask for each remap set */
1696 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1699 for (i = 0; i < spkr_remap_sets; i++) {
1700 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1701 if (get_bits_left(&s->gb) < 0)
1704 for (j = 0; j < num_spkrs[i]; j++) {
1705 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1706 int num_dec_ch = av_popcount(remap_dec_ch_mask);
1707 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1712 skip_bits(&s->gb, 3); // representation type
1716 drc_code_present = get_bits1(&s->gb);
1717 if (drc_code_present)
1718 get_bits(&s->gb, 8); // drc code
1720 if (get_bits1(&s->gb))
1721 skip_bits(&s->gb, 5); // dialog normalization code
1723 if (drc_code_present && embedded_stereo)
1724 get_bits(&s->gb, 8); // drc stereo code
1726 if (s->mix_metadata && get_bits1(&s->gb)) {
1727 skip_bits(&s->gb, 1); // external mix
1728 skip_bits(&s->gb, 6); // post mix gain code
1730 if (get_bits(&s->gb, 2) != 3) // mixer drc code
1731 skip_bits(&s->gb, 3); // drc limit
1733 skip_bits(&s->gb, 8); // custom drc code
1735 if (get_bits1(&s->gb)) // channel specific scaling
1736 for (i = 0; i < s->num_mix_configs; i++)
1737 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1739 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1741 for (i = 0; i < s->num_mix_configs; i++) {
1742 if (get_bits_left(&s->gb) < 0)
1744 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1746 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1747 if (embedded_stereo)
1748 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1752 switch (get_bits(&s->gb, 2)) {
1753 case 0: extensions_mask = get_bits(&s->gb, 12); break;
1754 case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1755 case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1756 case 3: extensions_mask = 0; /* aux coding */ break;
1759 /* not parsed further, we were only interested in the extensions mask */
1761 if (get_bits_left(&s->gb) < 0)
1764 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1765 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1768 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1770 if (extensions_mask & DCA_EXT_EXSS_XLL)
1771 s->profile = FF_PROFILE_DTS_HD_MA;
1772 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1774 s->profile = FF_PROFILE_DTS_HD_HRA;
1776 if (!(extensions_mask & DCA_EXT_CORE))
1777 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1778 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1779 av_log(s->avctx, AV_LOG_WARNING,
1780 "DTS extensions detection mismatch (%d, %d)\n",
1781 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1786 static int dca_xbr_parse_frame(DCAContext *s)
1788 int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1789 int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1790 int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1791 int anctemp[DCA_CHSET_CHANS_MAX];
1792 int chset_fsize[DCA_CHSETS_MAX];
1793 int n_xbr_ch[DCA_CHSETS_MAX];
1794 int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1795 int i, j, k, l, chset, chan_base;
1797 av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1799 /* get bit position of sync header */
1800 hdr_pos = get_bits_count(&s->gb) - 32;
1802 hdr_size = get_bits(&s->gb, 6) + 1;
1803 num_chsets = get_bits(&s->gb, 2) + 1;
1805 for(i = 0; i < num_chsets; i++)
1806 chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1808 xbr_tmode = get_bits1(&s->gb);
1810 for(i = 0; i < num_chsets; i++) {
1811 n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1812 k = get_bits(&s->gb, 2) + 5;
1813 for(j = 0; j < n_xbr_ch[i]; j++)
1814 active_bands[i][j] = get_bits(&s->gb, k) + 1;
1817 /* skip to the end of the header */
1818 i = get_bits_count(&s->gb);
1819 if(hdr_pos + hdr_size * 8 > i)
1820 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1822 /* loop over the channel data sets */
1823 /* only decode as many channels as we've decoded base data for */
1824 for(chset = 0, chan_base = 0;
1825 chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1826 chan_base += n_xbr_ch[chset++]) {
1827 int start_posn = get_bits_count(&s->gb);
1828 int subsubframe = 0;
1831 /* loop over subframes */
1832 for (k = 0; k < (s->sample_blocks / 8); k++) {
1833 /* parse header if we're on first subsubframe of a block */
1834 if(subsubframe == 0) {
1835 /* Parse subframe header */
1836 for(i = 0; i < n_xbr_ch[chset]; i++) {
1837 anctemp[i] = get_bits(&s->gb, 2) + 2;
1840 for(i = 0; i < n_xbr_ch[chset]; i++) {
1841 get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1844 for(i = 0; i < n_xbr_ch[chset]; i++) {
1845 anctemp[i] = get_bits(&s->gb, 3);
1846 if(anctemp[i] < 1) {
1847 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1848 return AVERROR_INVALIDDATA;
1852 /* generate scale factors */
1853 for(i = 0; i < n_xbr_ch[chset]; i++) {
1854 const uint32_t *scale_table;
1857 if (s->scalefactor_huffman[chan_base+i] == 6) {
1858 scale_table = scale_factor_quant7;
1860 scale_table = scale_factor_quant6;
1865 for(j = 0; j < active_bands[chset][i]; j++) {
1866 if(abits_high[i][j] > 0) {
1867 scale_table_high[i][j][0] =
1868 scale_table[get_bits(&s->gb, nbits)];
1870 if(xbr_tmode && s->transition_mode[i][j]) {
1871 scale_table_high[i][j][1] =
1872 scale_table[get_bits(&s->gb, nbits)];
1879 /* decode audio array for this block */
1880 for(i = 0; i < n_xbr_ch[chset]; i++) {
1881 for(j = 0; j < active_bands[chset][i]; j++) {
1882 const int xbr_abits = abits_high[i][j];
1883 const float quant_step_size = lossless_quant_d[xbr_abits];
1884 const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1885 const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1886 float *subband_samples = s->subband_samples[k][chan_base+i][j];
1893 get_array(&s->gb, block, 8, xbr_abits - 3);
1895 int block_code1, block_code2, size, levels, err;
1897 size = abits_sizes[xbr_abits - 1];
1898 levels = abits_levels[xbr_abits - 1];
1900 block_code1 = get_bits(&s->gb, size);
1901 block_code2 = get_bits(&s->gb, size);
1902 err = decode_blockcodes(block_code1, block_code2,
1905 av_log(s->avctx, AV_LOG_ERROR,
1906 "ERROR: DTS-XBR: block code look-up failed\n");
1907 return AVERROR_INVALIDDATA;
1911 /* scale & sum into subband */
1912 for(l = 0; l < 8; l++)
1913 subband_samples[l] += (float)block[l] * rscale;
1917 /* check DSYNC marker */
1918 if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1919 if(get_bits(&s->gb, 16) != 0xffff) {
1920 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1921 return AVERROR_INVALIDDATA;
1925 /* advance sub-sub-frame index */
1926 if(++subsubframe >= s->subsubframes[subframe]) {
1932 /* skip to next channel set */
1933 i = get_bits_count(&s->gb);
1934 if(start_posn + chset_fsize[chset] * 8 != i) {
1935 j = start_posn + chset_fsize[chset] * 8 - i;
1937 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1938 " skipping further than expected (%d bits)\n", j);
1939 skip_bits_long(&s->gb, j);
1946 /* parse initial header for XXCH and dump details */
1947 static int dca_xxch_decode_frame(DCAContext *s)
1949 int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1950 int i, chset, base_channel, chstart, fsize[8];
1952 /* assume header word has already been parsed */
1953 hdr_pos = get_bits_count(&s->gb) - 32;
1954 hdr_size = get_bits(&s->gb, 6) + 1;
1955 /*chhdr_crc =*/ skip_bits1(&s->gb);
1956 spkmsk_bits = get_bits(&s->gb, 5) + 1;
1957 num_chsets = get_bits(&s->gb, 2) + 1;
1959 for (i = 0; i < num_chsets; i++)
1960 fsize[i] = get_bits(&s->gb, 14) + 1;
1962 core_spk = get_bits(&s->gb, spkmsk_bits);
1963 s->xxch_core_spkmask = core_spk;
1964 s->xxch_nbits_spk_mask = spkmsk_bits;
1965 s->xxch_dmix_embedded = 0;
1967 /* skip to the end of the header */
1968 i = get_bits_count(&s->gb);
1969 if (hdr_pos + hdr_size * 8 > i)
1970 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1972 for (chset = 0; chset < num_chsets; chset++) {
1973 chstart = get_bits_count(&s->gb);
1974 base_channel = s->prim_channels;
1975 s->xxch_chset = chset;
1977 /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1978 5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1979 dca_parse_audio_coding_header(s, base_channel, 1);
1981 /* decode channel data */
1982 for (i = 0; i < (s->sample_blocks / 8); i++) {
1983 if (dca_decode_block(s, base_channel, i)) {
1984 av_log(s->avctx, AV_LOG_ERROR,
1985 "Error decoding DTS-XXCH extension\n");
1990 /* skip to end of this section */
1991 i = get_bits_count(&s->gb);
1992 if (chstart + fsize[chset] * 8 > i)
1993 skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1995 s->xxch_chset = num_chsets;
2001 * Parse extension substream header (HD)
2003 static void dca_exss_parse_header(DCAContext *s)
2010 int active_ss_mask[8];
2016 if (get_bits_left(&s->gb) < 52)
2019 start_posn = get_bits_count(&s->gb) - 32;
2021 skip_bits(&s->gb, 8); // user data
2022 ss_index = get_bits(&s->gb, 2);
2024 blownup = get_bits1(&s->gb);
2025 hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
2026 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
2028 s->static_fields = get_bits1(&s->gb);
2029 if (s->static_fields) {
2030 skip_bits(&s->gb, 2); // reference clock code
2031 skip_bits(&s->gb, 3); // frame duration code
2033 if (get_bits1(&s->gb))
2034 skip_bits_long(&s->gb, 36); // timestamp
2036 /* a single stream can contain multiple audio assets that can be
2037 * combined to form multiple audio presentations */
2039 num_audiop = get_bits(&s->gb, 3) + 1;
2040 if (num_audiop > 1) {
2041 avpriv_request_sample(s->avctx,
2042 "Multiple DTS-HD audio presentations");
2043 /* ignore such streams for now */
2047 num_assets = get_bits(&s->gb, 3) + 1;
2048 if (num_assets > 1) {
2049 avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
2050 /* ignore such streams for now */
2054 for (i = 0; i < num_audiop; i++)
2055 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
2057 for (i = 0; i < num_audiop; i++)
2058 for (j = 0; j <= ss_index; j++)
2059 if (active_ss_mask[i] & (1 << j))
2060 skip_bits(&s->gb, 8); // active asset mask
2062 s->mix_metadata = get_bits1(&s->gb);
2063 if (s->mix_metadata) {
2064 int mix_out_mask_size;
2066 skip_bits(&s->gb, 2); // adjustment level
2067 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
2068 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
2070 for (i = 0; i < s->num_mix_configs; i++) {
2071 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
2072 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
2077 av_assert0(num_assets > 0); // silence a warning
2079 for (i = 0; i < num_assets; i++)
2080 asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
2082 for (i = 0; i < num_assets; i++) {
2083 if (dca_exss_parse_asset_header(s))
2087 /* not parsed further, we were only interested in the extensions mask
2088 * from the asset header */
2090 j = get_bits_count(&s->gb);
2091 if (start_posn + hdrsize * 8 > j)
2092 skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
2094 for (i = 0; i < num_assets; i++) {
2095 start_posn = get_bits_count(&s->gb);
2096 mkr = get_bits_long(&s->gb, 32);
2098 /* parse extensions that we know about */
2099 if (mkr == 0x655e315e) {
2100 dca_xbr_parse_frame(s);
2101 } else if (mkr == 0x47004a03) {
2102 dca_xxch_decode_frame(s);
2103 s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
2105 av_log(s->avctx, AV_LOG_DEBUG,
2106 "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
2109 /* skip to end of block */
2110 j = get_bits_count(&s->gb);
2111 if (start_posn + asset_size[i] * 8 > j)
2112 skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
2116 static float dca_dmix_code(unsigned code)
2118 int sign = (code >> 8) - 1;
2120 #define POW2_MINUS15 .000030517578125
2121 return ((dca_dmixtable[code] ^ sign) - sign) * POW2_MINUS15;
2125 * Main frame decoding function
2126 * FIXME add arguments
2128 static int dca_decode_frame(AVCodecContext *avctx, void *data,
2129 int *got_frame_ptr, AVPacket *avpkt)
2131 AVFrame *frame = data;
2132 const uint8_t *buf = avpkt->data;
2133 int buf_size = avpkt->size;
2137 int num_core_channels = 0;
2139 float **samples_flt;
2142 DCAContext *s = avctx->priv_data;
2144 int channels, full_channels;
2156 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
2157 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
2158 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
2159 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
2160 return AVERROR_INVALIDDATA;
2163 if ((ret = dca_parse_frame_header(s)) < 0) {
2164 //seems like the frame is corrupt, try with the next one
2167 //set AVCodec values with parsed data
2168 avctx->sample_rate = s->sample_rate;
2169 avctx->bit_rate = s->bit_rate;
2171 s->profile = FF_PROFILE_DTS;
2173 for (i = 0; i < (s->sample_blocks / 8); i++) {
2174 if ((ret = dca_decode_block(s, 0, i))) {
2175 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
2180 /* record number of core channels incase less than max channels are requested */
2181 num_core_channels = s->prim_channels;
2183 if (s->prim_channels + !!s->lfe > 2 &&
2184 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
2185 /* Stereo downmix coefficients
2187 * The decoder can only downmix to 2-channel, so we need to ensure
2188 * embedded downmix coefficients are actually targeting 2-channel.
2190 if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
2191 s->core_downmix_amode == DCA_STEREO_TOTAL)) {
2192 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2193 /* Range checked earlier */
2194 s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
2195 s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
2197 s->output = s->core_downmix_amode;
2199 int am = s->amode & DCA_CHANNEL_MASK;
2200 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
2201 av_log(s->avctx, AV_LOG_ERROR,
2202 "Invalid channel mode %d\n", am);
2203 return AVERROR_INVALIDDATA;
2205 if (num_core_channels + !!s->lfe >
2206 FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
2207 avpriv_request_sample(s->avctx, "Downmixing %d channels",
2208 s->prim_channels + !!s->lfe);
2209 return AVERROR_PATCHWELCOME;
2211 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2212 s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
2213 s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
2216 av_dlog(s->avctx, "Stereo downmix coeffs:\n");
2217 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2218 av_dlog(s->avctx, "L, input channel %d = %f\n", i,
2219 s->downmix_coef[i][0]);
2220 av_dlog(s->avctx, "R, input channel %d = %f\n", i,
2221 s->downmix_coef[i][1]);
2223 av_dlog(s->avctx, "\n");
2227 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
2229 s->core_ext_mask = 0;
2231 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
2233 /* only scan for extensions if ext_descr was unknown or indicated a
2234 * supported XCh extension */
2235 if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
2237 /* if ext_descr was unknown, clear s->core_ext_mask so that the
2238 * extensions scan can fill it up */
2239 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
2241 /* extensions start at 32-bit boundaries into bitstream */
2242 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2244 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
2245 uint32_t bits = get_bits_long(&s->gb, 32);
2249 int ext_amode, xch_fsize;
2251 s->xch_base_channel = s->prim_channels;
2253 /* validate sync word using XCHFSIZE field */
2254 xch_fsize = show_bits(&s->gb, 10);
2255 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
2256 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
2259 /* skip length-to-end-of-frame field for the moment */
2260 skip_bits(&s->gb, 10);
2262 s->core_ext_mask |= DCA_EXT_XCH;
2264 /* extension amode(number of channels in extension) should be 1 */
2265 /* AFAIK XCh is not used for more channels */
2266 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
2267 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
2268 " supported!\n", ext_amode);
2272 if (s->xch_base_channel < 2) {
2273 avpriv_request_sample(avctx, "XCh with fewer than 2 base channels");
2277 /* much like core primary audio coding header */
2278 dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
2280 for (i = 0; i < (s->sample_blocks / 8); i++)
2281 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
2282 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
2290 /* XXCh: extended channels */
2291 /* usually found either in core or HD part in DTS-HD HRA streams,
2292 * but not in DTS-ES which contains XCh extensions instead */
2293 s->core_ext_mask |= DCA_EXT_XXCH;
2294 dca_xxch_decode_frame(s);
2298 int fsize96 = show_bits(&s->gb, 12) + 1;
2299 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
2302 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
2303 get_bits_count(&s->gb));
2304 skip_bits(&s->gb, 12);
2305 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
2306 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
2308 s->core_ext_mask |= DCA_EXT_X96;
2313 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2316 /* no supported extensions, skip the rest of the core substream */
2317 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
2320 if (s->core_ext_mask & DCA_EXT_X96)
2321 s->profile = FF_PROFILE_DTS_96_24;
2322 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
2323 s->profile = FF_PROFILE_DTS_ES;
2325 /* check for ExSS (HD part) */
2326 if (s->dca_buffer_size - s->frame_size > 32 &&
2327 get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
2328 dca_exss_parse_header(s);
2330 avctx->profile = s->profile;
2332 full_channels = channels = s->prim_channels + !!s->lfe;
2334 /* If we have XXCH then the channel layout is managed differently */
2335 /* note that XLL will also have another way to do things */
2336 if (!(s->core_ext_mask & DCA_EXT_XXCH)
2337 || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
2338 && avctx->request_channels
2339 < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
2340 { /* xxx should also do MA extensions */
2341 if (s->amode < 16) {
2342 avctx->channel_layout = dca_core_channel_layout[s->amode];
2344 if (s->prim_channels + !!s->lfe > 2 &&
2345 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
2347 * Neither the core's auxiliary data nor our default tables contain
2348 * downmix coefficients for the additional channel coded in the XCh
2349 * extension, so when we're doing a Stereo downmix, don't decode it.
2354 #if FF_API_REQUEST_CHANNELS
2355 FF_DISABLE_DEPRECATION_WARNINGS
2356 if (s->xch_present && !s->xch_disable &&
2357 (!avctx->request_channels ||
2358 avctx->request_channels > num_core_channels + !!s->lfe)) {
2359 FF_ENABLE_DEPRECATION_WARNINGS
2361 if (s->xch_present && !s->xch_disable) {
2363 avctx->channel_layout |= AV_CH_BACK_CENTER;
2365 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2366 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
2368 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
2370 if (s->channel_order_tab[s->xch_base_channel] < 0)
2371 return AVERROR_INVALIDDATA;
2373 channels = num_core_channels + !!s->lfe;
2374 s->xch_present = 0; /* disable further xch processing */
2376 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2377 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
2379 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
2382 if (channels > !!s->lfe &&
2383 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
2384 return AVERROR_INVALIDDATA;
2386 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != channels) {
2387 av_log(avctx, AV_LOG_ERROR, "Number of channels %d mismatches layout %d\n", channels, av_get_channel_layout_nb_channels(avctx->channel_layout));
2388 return AVERROR_INVALIDDATA;
2391 if (num_core_channels + !!s->lfe > 2 &&
2392 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
2394 s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
2395 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
2397 else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
2398 static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2399 s->channel_order_tab = dca_channel_order_native;
2401 s->lfe_index = dca_lfe_index[s->amode];
2403 av_log(avctx, AV_LOG_ERROR,
2404 "Non standard configuration %d !\n", s->amode);
2405 return AVERROR_INVALIDDATA;
2408 s->xxch_dmix_embedded = 0;
2410 /* we only get here if an XXCH channel set can be added to the mix */
2411 channel_mask = s->xxch_core_spkmask;
2413 if (avctx->request_channels > 0
2414 && avctx->request_channels < s->prim_channels) {
2415 channels = num_core_channels + !!s->lfe;
2416 for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
2417 <= avctx->request_channels; i++) {
2418 channels += s->xxch_chset_nch[i];
2419 channel_mask |= s->xxch_spk_masks[i];
2422 channels = s->prim_channels + !!s->lfe;
2423 for (i = 0; i < s->xxch_chset; i++) {
2424 channel_mask |= s->xxch_spk_masks[i];
2428 /* Given the DTS spec'ed channel mask, generate an avcodec version */
2430 for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
2431 if (channel_mask & (1 << i)) {
2432 channel_layout |= map_xxch_to_native[i];
2436 /* make sure that we have managed to get equivalent dts/avcodec channel
2437 * masks in some sense -- unfortunately some channels could overlap */
2438 if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
2439 av_log(avctx, AV_LOG_DEBUG,
2440 "DTS-XXCH: Inconsistent avcodec/dts channel layouts\n");
2441 return AVERROR_INVALIDDATA;
2444 avctx->channel_layout = channel_layout;
2446 if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
2447 /* Estimate DTS --> avcodec ordering table */
2448 for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
2449 mask = chset >= 0 ? s->xxch_spk_masks[chset]
2450 : s->xxch_core_spkmask;
2451 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
2452 if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
2453 lavc = map_xxch_to_native[i];
2454 posn = av_popcount(channel_layout & (lavc - 1));
2455 s->xxch_order_tab[j++] = posn;
2461 s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
2462 } else { /* native ordering */
2463 for (i = 0; i < channels; i++)
2464 s->xxch_order_tab[i] = i;
2466 s->lfe_index = channels - 1;
2469 s->channel_order_tab = s->xxch_order_tab;
2472 if (avctx->channels != channels) {
2473 if (avctx->channels)
2474 av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
2475 avctx->channels = channels;
2478 /* get output buffer */
2479 frame->nb_samples = 256 * (s->sample_blocks / 8);
2480 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2482 samples_flt = (float **)frame->extended_data;
2484 /* allocate buffer for extra channels if downmixing */
2485 if (avctx->channels < full_channels) {
2486 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
2488 avctx->sample_fmt, 0);
2492 av_fast_malloc(&s->extra_channels_buffer,
2493 &s->extra_channels_buffer_size, ret);
2494 if (!s->extra_channels_buffer)
2495 return AVERROR(ENOMEM);
2497 ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
2498 s->extra_channels_buffer,
2499 full_channels - channels,
2500 frame->nb_samples, avctx->sample_fmt, 0);
2505 /* filter to get final output */
2506 for (i = 0; i < (s->sample_blocks / 8); i++) {
2509 for (ch = 0; ch < channels; ch++)
2510 s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
2511 for (; ch < full_channels; ch++)
2512 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
2514 dca_filter_channels(s, i);
2516 /* If this was marked as a DTS-ES stream we need to subtract back- */
2517 /* channel from SL & SR to remove matrixed back-channel signal */
2518 if ((s->source_pcm_res & 1) && s->xch_present) {
2519 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
2520 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
2521 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
2522 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2523 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2526 /* If stream contains XXCH, we might need to undo an embedded downmix */
2527 if (s->xxch_dmix_embedded) {
2528 /* Loop over channel sets in turn */
2529 ch = num_core_channels;
2530 for (chset = 0; chset < s->xxch_chset; chset++) {
2531 endch = ch + s->xxch_chset_nch[chset];
2532 mask = s->xxch_dmix_embedded;
2535 for (j = ch; j < endch; j++) {
2536 if (mask & (1 << j)) { /* this channel has been mixed-out */
2537 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2538 for (k = 0; k < endch; k++) {
2539 achan = s->channel_order_tab[k];
2540 scale = s->xxch_dmix_coeff[j][k];
2542 dst_chan = s->samples_chanptr[achan];
2543 s->fdsp.vector_fmac_scalar(dst_chan, src_chan,
2550 /* if a downmix has been embedded then undo the pre-scaling */
2551 if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
2552 scale = s->xxch_dmix_sf[chset];
2554 for (j = 0; j < ch; j++) {
2555 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2556 for (k = 0; k < 256; k++)
2557 src_chan[k] *= scale;
2560 /* LFE channel is always part of core, scale if it exists */
2562 src_chan = s->samples_chanptr[s->lfe_index];
2563 for (k = 0; k < 256; k++)
2564 src_chan[k] *= scale;
2574 /* update lfe history */
2575 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2576 for (i = 0; i < 2 * s->lfe * 4; i++)
2577 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2581 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
2582 ret = ff_side_data_update_matrix_encoding(frame,
2583 (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
2584 AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
2596 * DCA initialization
2598 * @param avctx pointer to the AVCodecContext
2601 static av_cold int dca_decode_init(AVCodecContext *avctx)
2603 DCAContext *s = avctx->priv_data;
2608 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
2609 ff_mdct_init(&s->imdct, 6, 1, 1.0);
2610 ff_synth_filter_init(&s->synth);
2611 ff_dcadsp_init(&s->dcadsp);
2612 ff_fmt_convert_init(&s->fmt_conv, avctx);
2614 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2616 /* allow downmixing to stereo */
2617 #if FF_API_REQUEST_CHANNELS
2618 FF_DISABLE_DEPRECATION_WARNINGS
2619 if (avctx->request_channels == 2)
2620 avctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
2621 FF_ENABLE_DEPRECATION_WARNINGS
2623 if (avctx->channels > 2 &&
2624 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
2625 avctx->channels = 2;
2630 static av_cold int dca_decode_end(AVCodecContext *avctx)
2632 DCAContext *s = avctx->priv_data;
2633 ff_mdct_end(&s->imdct);
2634 av_freep(&s->extra_channels_buffer);
2638 static const AVProfile profiles[] = {
2639 { FF_PROFILE_DTS, "DTS" },
2640 { FF_PROFILE_DTS_ES, "DTS-ES" },
2641 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2642 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2643 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2644 { FF_PROFILE_UNKNOWN },
2647 static const AVOption options[] = {
2648 { "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 },
2652 static const AVClass dca_decoder_class = {
2653 .class_name = "DCA decoder",
2654 .item_name = av_default_item_name,
2656 .version = LIBAVUTIL_VERSION_INT,
2657 .category = AV_CLASS_CATEGORY_DECODER,
2660 AVCodec ff_dca_decoder = {
2662 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2663 .type = AVMEDIA_TYPE_AUDIO,
2664 .id = AV_CODEC_ID_DTS,
2665 .priv_data_size = sizeof(DCAContext),
2666 .init = dca_decode_init,
2667 .decode = dca_decode_frame,
2668 .close = dca_decode_end,
2669 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2670 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2671 AV_SAMPLE_FMT_NONE },
2672 .profiles = NULL_IF_CONFIG_SMALL(profiles),
2673 .priv_class = &dca_decoder_class,