2 * DCA compatible decoder
3 * Copyright (C) 2004 Gildas Bazin
4 * Copyright (C) 2004 Benjamin Zores
5 * Copyright (C) 2006 Benjamin Larsson
6 * Copyright (C) 2007 Konstantin Shishkov
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #include "libavutil/float_dsp.h"
32 #include "libavutil/intmath.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/samplefmt.h"
44 #include "dca_parser.h"
45 #include "synth_filter.h"
47 #include "fmtconvert.h"
56 #define DCA_PRIM_CHANNELS_MAX (7)
57 #define DCA_SUBBANDS (64)
58 #define DCA_ABITS_MAX (32) /* Should be 28 */
59 #define DCA_SUBSUBFRAMES_MAX (4)
60 #define DCA_SUBFRAMES_MAX (16)
61 #define DCA_BLOCKS_MAX (16)
62 #define DCA_LFE_MAX (3)
63 #define DCA_CHSETS_MAX (4)
64 #define DCA_CHSET_CHANS_MAX (8)
80 /* these are unconfirmed but should be mostly correct */
81 enum DCAExSSSpeakerMask {
82 DCA_EXSS_FRONT_CENTER = 0x0001,
83 DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
84 DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
85 DCA_EXSS_LFE = 0x0008,
86 DCA_EXSS_REAR_CENTER = 0x0010,
87 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
88 DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
89 DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
90 DCA_EXSS_OVERHEAD = 0x0100,
91 DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
92 DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
93 DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
94 DCA_EXSS_LFE2 = 0x1000,
95 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
96 DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
97 DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
100 enum DCAXxchSpeakerMask {
101 DCA_XXCH_FRONT_CENTER = 0x0000001,
102 DCA_XXCH_FRONT_LEFT = 0x0000002,
103 DCA_XXCH_FRONT_RIGHT = 0x0000004,
104 DCA_XXCH_SIDE_REAR_LEFT = 0x0000008,
105 DCA_XXCH_SIDE_REAR_RIGHT = 0x0000010,
106 DCA_XXCH_LFE1 = 0x0000020,
107 DCA_XXCH_REAR_CENTER = 0x0000040,
108 DCA_XXCH_SURROUND_REAR_LEFT = 0x0000080,
109 DCA_XXCH_SURROUND_REAR_RIGHT = 0x0000100,
110 DCA_XXCH_SIDE_SURROUND_LEFT = 0x0000200,
111 DCA_XXCH_SIDE_SURROUND_RIGHT = 0x0000400,
112 DCA_XXCH_FRONT_CENTER_LEFT = 0x0000800,
113 DCA_XXCH_FRONT_CENTER_RIGHT = 0x0001000,
114 DCA_XXCH_FRONT_HIGH_LEFT = 0x0002000,
115 DCA_XXCH_FRONT_HIGH_CENTER = 0x0004000,
116 DCA_XXCH_FRONT_HIGH_RIGHT = 0x0008000,
117 DCA_XXCH_LFE2 = 0x0010000,
118 DCA_XXCH_SIDE_FRONT_LEFT = 0x0020000,
119 DCA_XXCH_SIDE_FRONT_RIGHT = 0x0040000,
120 DCA_XXCH_OVERHEAD = 0x0080000,
121 DCA_XXCH_SIDE_HIGH_LEFT = 0x0100000,
122 DCA_XXCH_SIDE_HIGH_RIGHT = 0x0200000,
123 DCA_XXCH_REAR_HIGH_CENTER = 0x0400000,
124 DCA_XXCH_REAR_HIGH_LEFT = 0x0800000,
125 DCA_XXCH_REAR_HIGH_RIGHT = 0x1000000,
126 DCA_XXCH_REAR_LOW_CENTER = 0x2000000,
127 DCA_XXCH_REAR_LOW_LEFT = 0x4000000,
128 DCA_XXCH_REAR_LOW_RIGHT = 0x8000000,
131 static const uint32_t map_xxch_to_native[28] = {
141 AV_CH_SIDE_LEFT, /* side surround left -- dup sur side L */
142 AV_CH_SIDE_RIGHT, /* side surround right -- dup sur side R */
143 AV_CH_FRONT_LEFT_OF_CENTER,
144 AV_CH_FRONT_RIGHT_OF_CENTER,
145 AV_CH_TOP_FRONT_LEFT,
146 AV_CH_TOP_FRONT_CENTER,
147 AV_CH_TOP_FRONT_RIGHT,
148 AV_CH_LOW_FREQUENCY, /* lfe2 -- duplicate lfe1 position */
149 AV_CH_FRONT_LEFT_OF_CENTER, /* side front left -- dup front cntr L */
150 AV_CH_FRONT_RIGHT_OF_CENTER,/* side front right -- dup front cntr R */
151 AV_CH_TOP_CENTER, /* overhead */
152 AV_CH_TOP_FRONT_LEFT, /* side high left -- dup */
153 AV_CH_TOP_FRONT_RIGHT, /* side high right -- dup */
154 AV_CH_TOP_BACK_CENTER,
156 AV_CH_TOP_BACK_RIGHT,
157 AV_CH_BACK_CENTER, /* rear low center -- dup */
158 AV_CH_BACK_LEFT, /* rear low left -- dup */
159 AV_CH_BACK_RIGHT /* read low right -- dup */
162 enum DCAExtensionMask {
163 DCA_EXT_CORE = 0x001, ///< core in core substream
164 DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
165 DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
166 DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
167 DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
168 DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
169 DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
170 DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
171 DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
172 DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
175 /* -1 are reserved or unknown */
176 static const int dca_ext_audio_descr_mask[] = {
180 DCA_EXT_XCH | DCA_EXT_X96,
187 /* extensions that reside in core substream */
188 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
190 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
191 * Some compromises have been made for special configurations. Most configurations
192 * are never used so complete accuracy is not needed.
194 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
195 * S -> side, when both rear and back are configured move one of them to the side channel
197 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
199 static const uint64_t dca_core_channel_layout[] = {
200 AV_CH_FRONT_CENTER, ///< 1, A
201 AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
202 AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
203 AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
204 AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
205 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
206 AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
207 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
208 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
210 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
211 AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
213 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
214 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
216 AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
217 AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
219 AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
220 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
221 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
223 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
224 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
225 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
227 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
228 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
229 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
231 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
232 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
233 AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
236 static const int8_t dca_lfe_index[] = {
237 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
240 static const int8_t dca_channel_reorder_lfe[][9] = {
241 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
242 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
243 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
244 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
245 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
246 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
247 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
248 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
249 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
250 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
251 { 3, 4, 0, 1, 5, 6, -1, -1, -1},
252 { 2, 0, 1, 4, 5, 6, -1, -1, -1},
253 { 0, 6, 4, 5, 2, 3, -1, -1, -1},
254 { 4, 2, 5, 0, 1, 6, 7, -1, -1},
255 { 5, 6, 0, 1, 7, 3, 8, 4, -1},
256 { 4, 2, 5, 0, 1, 6, 8, 7, -1},
259 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
260 { 0, 2, -1, -1, -1, -1, -1, -1, -1},
261 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
262 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
263 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
264 { 0, 1, 3, -1, -1, -1, -1, -1, -1},
265 { 2, 0, 1, 4, -1, -1, -1, -1, -1},
266 { 0, 1, 3, 4, -1, -1, -1, -1, -1},
267 { 2, 0, 1, 4, 5, -1, -1, -1, -1},
268 { 0, 1, 4, 5, 3, -1, -1, -1, -1},
269 { 2, 0, 1, 5, 6, 4, -1, -1, -1},
270 { 3, 4, 0, 1, 6, 7, 5, -1, -1},
271 { 2, 0, 1, 4, 5, 6, 7, -1, -1},
272 { 0, 6, 4, 5, 2, 3, 7, -1, -1},
273 { 4, 2, 5, 0, 1, 7, 8, 6, -1},
274 { 5, 6, 0, 1, 8, 3, 9, 4, 7},
275 { 4, 2, 5, 0, 1, 6, 9, 8, 7},
278 static const int8_t dca_channel_reorder_nolfe[][9] = {
279 { 0, -1, -1, -1, -1, -1, -1, -1, -1},
280 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
281 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
282 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
283 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
284 { 2, 0, 1, -1, -1, -1, -1, -1, -1},
285 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
286 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
287 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
288 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
289 { 2, 3, 0, 1, 4, 5, -1, -1, -1},
290 { 2, 0, 1, 3, 4, 5, -1, -1, -1},
291 { 0, 5, 3, 4, 1, 2, -1, -1, -1},
292 { 3, 2, 4, 0, 1, 5, 6, -1, -1},
293 { 4, 5, 0, 1, 6, 2, 7, 3, -1},
294 { 3, 2, 4, 0, 1, 5, 7, 6, -1},
297 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
298 { 0, 1, -1, -1, -1, -1, -1, -1, -1},
299 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
300 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
301 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
302 { 0, 1, 2, -1, -1, -1, -1, -1, -1},
303 { 2, 0, 1, 3, -1, -1, -1, -1, -1},
304 { 0, 1, 2, 3, -1, -1, -1, -1, -1},
305 { 2, 0, 1, 3, 4, -1, -1, -1, -1},
306 { 0, 1, 3, 4, 2, -1, -1, -1, -1},
307 { 2, 0, 1, 4, 5, 3, -1, -1, -1},
308 { 2, 3, 0, 1, 5, 6, 4, -1, -1},
309 { 2, 0, 1, 3, 4, 5, 6, -1, -1},
310 { 0, 5, 3, 4, 1, 2, 6, -1, -1},
311 { 3, 2, 4, 0, 1, 6, 7, 5, -1},
312 { 4, 5, 0, 1, 7, 2, 8, 3, 6},
313 { 3, 2, 4, 0, 1, 5, 8, 7, 6},
316 #define DCA_DOLBY 101 /* FIXME */
318 #define DCA_CHANNEL_BITS 6
319 #define DCA_CHANNEL_MASK 0x3F
323 #define HEADER_SIZE 14
325 #define DCA_MAX_FRAME_SIZE 16384
326 #define DCA_MAX_EXSS_HEADER_SIZE 4096
328 #define DCA_BUFFER_PADDING_SIZE 1024
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 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 downmix; ///< embedded downmix enabled
365 int dynrange; ///< embedded dynamic range flag
366 int timestamp; ///< embedded time stamp flag
367 int aux_data; ///< auxiliary data flag
368 int hdcd; ///< source material is mastered in HDCD
369 int ext_descr; ///< extension audio descriptor flag
370 int ext_coding; ///< extended coding flag
371 int aspf; ///< audio sync word insertion flag
372 int lfe; ///< low frequency effects flag
373 int predictor_history; ///< predictor history flag
374 int header_crc; ///< header crc check bytes
375 int multirate_inter; ///< multirate interpolator switch
376 int version; ///< encoder software revision
377 int copy_history; ///< copy history
378 int source_pcm_res; ///< source pcm resolution
379 int front_sum; ///< front sum/difference flag
380 int surround_sum; ///< surround sum/difference flag
381 int dialog_norm; ///< dialog normalisation parameter
383 /* Primary audio coding header */
384 int subframes; ///< number of subframes
385 int total_channels; ///< number of channels including extensions
386 int prim_channels; ///< number of primary audio channels
387 int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
388 int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
389 int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
390 int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
391 int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
392 int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
393 int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
394 float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
396 /* Primary audio coding side information */
397 int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
398 int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
399 int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
400 int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
401 int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
402 int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
403 int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
404 int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
405 int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
406 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
407 int dynrange_coef; ///< dynamic range coefficient
409 int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
411 float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
412 int lfe_scale_factor;
414 /* Subband samples history (for ADPCM) */
415 DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
416 DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
417 DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
418 int hist_index[DCA_PRIM_CHANNELS_MAX];
419 DECLARE_ALIGNED(32, float, raXin)[32];
421 int output; ///< type of output
423 DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
424 float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
425 float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
426 uint8_t *extra_channels_buffer;
427 unsigned int extra_channels_buffer_size;
429 uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
430 int dca_buffer_size; ///< how much data is in the dca_buffer
432 const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
434 /* Current position in DCA frame */
435 int current_subframe;
436 int current_subsubframe;
438 int core_ext_mask; ///< present extensions in the core substream
440 /* XCh extension information */
441 int xch_present; ///< XCh extension present and valid
442 int xch_base_channel; ///< index of first (only) channel containing XCH data
444 /* XXCH extension information */
446 int xxch_nbits_spk_mask;
447 uint32_t xxch_core_spkmask;
448 uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
449 int xxch_chset_nch[4];
450 float xxch_dmix_sf[DCA_CHSETS_MAX];
452 uint32_t xxch_dmix_embedded; /* lower layer has mix pre-embedded, per chset */
453 float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
455 int8_t xxch_order_tab[32];
458 /* ExSS header parser */
459 int static_fields; ///< static fields present
460 int mix_metadata; ///< mixing metadata present
461 int num_mix_configs; ///< number of mix out configurations
462 int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
466 int debug_flag; ///< used for suppressing repeated error messages output
467 AVFloatDSPContext fdsp;
469 SynthFilterContext synth;
470 DCADSPContext dcadsp;
471 FmtConvertContext fmt_conv;
474 static const uint16_t dca_vlc_offs[] = {
475 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
476 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
477 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
478 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
479 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
480 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
483 static av_cold void dca_init_vlcs(void)
485 static int vlcs_initialized = 0;
487 static VLC_TYPE dca_table[23622][2];
489 if (vlcs_initialized)
492 dca_bitalloc_index.offset = 1;
493 dca_bitalloc_index.wrap = 2;
494 for (i = 0; i < 5; i++) {
495 dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
496 dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
497 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
498 bitalloc_12_bits[i], 1, 1,
499 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
501 dca_scalefactor.offset = -64;
502 dca_scalefactor.wrap = 2;
503 for (i = 0; i < 5; i++) {
504 dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
505 dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
506 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
507 scales_bits[i], 1, 1,
508 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
510 dca_tmode.offset = 0;
512 for (i = 0; i < 4; i++) {
513 dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
514 dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
515 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
517 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
520 for (i = 0; i < 10; i++)
521 for (j = 0; j < 7; j++) {
522 if (!bitalloc_codes[i][j])
524 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
525 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
526 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
527 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
529 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
531 bitalloc_bits[i][j], 1, 1,
532 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
535 vlcs_initialized = 1;
538 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
541 *dst++ = get_bits(gb, bits);
544 static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
548 /* locate channel set containing the channel */
549 for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
550 i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
551 base += av_popcount(mask);
553 return base + av_popcount(mask & (xxch_ch - 1));
556 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
560 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
561 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
562 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
563 int hdr_pos = 0, hdr_size = 0;
564 float sign, mag, scale_factor;
565 int this_chans, acc_mask;
566 int embedded_downmix;
570 /* xxch has arbitrary sized audio coding headers */
572 hdr_pos = get_bits_count(&s->gb);
573 hdr_size = get_bits(&s->gb, 7) + 1;
576 nchans = get_bits(&s->gb, 3) + 1;
577 s->total_channels = nchans + base_channel;
578 s->prim_channels = s->total_channels;
580 /* obtain speaker layout mask & downmix coefficients for XXCH */
582 acc_mask = s->xxch_core_spkmask;
584 this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
585 s->xxch_spk_masks[s->xxch_chset] = this_chans;
586 s->xxch_chset_nch[s->xxch_chset] = nchans;
588 for (i = 0; i <= s->xxch_chset; i++)
589 acc_mask |= s->xxch_spk_masks[i];
591 /* check for downmixing information */
592 if (get_bits1(&s->gb)) {
593 embedded_downmix = get_bits1(&s->gb);
595 1.0f / dca_downmix_scale_factors[(get_bits(&s->gb, 6) - 1) << 2];
597 s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
599 for (i = base_channel; i < s->prim_channels; i++) {
600 mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
603 for (j = base_channel; j < s->prim_channels; j++) {
604 memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
605 s->xxch_dmix_embedded |= (embedded_downmix << j);
606 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
607 if (mask[j] & (1 << i)) {
608 if ((1 << i) == DCA_XXCH_LFE1) {
609 av_log(s->avctx, AV_LOG_WARNING,
610 "DCA-XXCH: dmix to LFE1 not supported.\n");
614 coeff = get_bits(&s->gb, 7);
615 sign = (coeff & 64) ? 1.0 : -1.0;
616 mag = dca_downmix_scale_factors[((coeff & 63) - 1) << 2];
617 ichan = dca_xxch2index(s, 1 << i);
618 s->xxch_dmix_coeff[j][ichan] = sign * mag;
625 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
626 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
629 for (i = base_channel; i < s->prim_channels; i++) {
630 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
631 if (s->subband_activity[i] > DCA_SUBBANDS)
632 s->subband_activity[i] = DCA_SUBBANDS;
634 for (i = base_channel; i < s->prim_channels; i++) {
635 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
636 if (s->vq_start_subband[i] > DCA_SUBBANDS)
637 s->vq_start_subband[i] = DCA_SUBBANDS;
639 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
640 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
641 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
642 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
644 /* Get codebooks quantization indexes */
646 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
647 for (j = 1; j < 11; j++)
648 for (i = base_channel; i < s->prim_channels; i++)
649 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
651 /* Get scale factor adjustment */
652 for (j = 0; j < 11; j++)
653 for (i = base_channel; i < s->prim_channels; i++)
654 s->scalefactor_adj[i][j] = 1;
656 for (j = 1; j < 11; j++)
657 for (i = base_channel; i < s->prim_channels; i++)
658 if (s->quant_index_huffman[i][j] < thr[j])
659 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
662 if (s->crc_present) {
663 /* Audio header CRC check */
664 get_bits(&s->gb, 16);
667 /* Skip to the end of the header, also ignore CRC if present */
668 i = get_bits_count(&s->gb);
669 if (hdr_pos + 8 * hdr_size > i)
670 skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
673 s->current_subframe = 0;
674 s->current_subsubframe = 0;
677 av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
678 av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
679 for (i = base_channel; i < s->prim_channels; i++) {
680 av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
681 s->subband_activity[i]);
682 av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
683 s->vq_start_subband[i]);
684 av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
685 s->joint_intensity[i]);
686 av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
687 s->transient_huffman[i]);
688 av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
689 s->scalefactor_huffman[i]);
690 av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
691 s->bitalloc_huffman[i]);
692 av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
693 for (j = 0; j < 11; j++)
694 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
695 av_log(s->avctx, AV_LOG_DEBUG, "\n");
696 av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
697 for (j = 0; j < 11; j++)
698 av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
699 av_log(s->avctx, AV_LOG_DEBUG, "\n");
706 static int dca_parse_frame_header(DCAContext *s)
708 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
711 skip_bits_long(&s->gb, 32);
714 s->frame_type = get_bits(&s->gb, 1);
715 s->samples_deficit = get_bits(&s->gb, 5) + 1;
716 s->crc_present = get_bits(&s->gb, 1);
717 s->sample_blocks = get_bits(&s->gb, 7) + 1;
718 s->frame_size = get_bits(&s->gb, 14) + 1;
719 if (s->frame_size < 95)
720 return AVERROR_INVALIDDATA;
721 s->amode = get_bits(&s->gb, 6);
722 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
724 return AVERROR_INVALIDDATA;
725 s->bit_rate_index = get_bits(&s->gb, 5);
726 s->bit_rate = dca_bit_rates[s->bit_rate_index];
728 return AVERROR_INVALIDDATA;
730 s->downmix = get_bits(&s->gb, 1); /* note: this is FixedBit == 0 */
731 s->dynrange = get_bits(&s->gb, 1);
732 s->timestamp = get_bits(&s->gb, 1);
733 s->aux_data = get_bits(&s->gb, 1);
734 s->hdcd = get_bits(&s->gb, 1);
735 s->ext_descr = get_bits(&s->gb, 3);
736 s->ext_coding = get_bits(&s->gb, 1);
737 s->aspf = get_bits(&s->gb, 1);
738 s->lfe = get_bits(&s->gb, 2);
739 s->predictor_history = get_bits(&s->gb, 1);
743 av_log_ask_for_sample(s->avctx, "LFE is 3\n");
744 return AVERROR_PATCHWELCOME;
747 /* TODO: check CRC */
749 s->header_crc = get_bits(&s->gb, 16);
751 s->multirate_inter = get_bits(&s->gb, 1);
752 s->version = get_bits(&s->gb, 4);
753 s->copy_history = get_bits(&s->gb, 2);
754 s->source_pcm_res = get_bits(&s->gb, 3);
755 s->front_sum = get_bits(&s->gb, 1);
756 s->surround_sum = get_bits(&s->gb, 1);
757 s->dialog_norm = get_bits(&s->gb, 4);
759 /* FIXME: channels mixing levels */
760 s->output = s->amode;
762 s->output |= DCA_LFE;
765 av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
766 av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
767 av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
768 av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
769 s->sample_blocks, s->sample_blocks * 32);
770 av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
771 av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
772 s->amode, dca_channels[s->amode]);
773 av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
775 av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
777 av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
778 av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
779 av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
780 av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
781 av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
782 av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
783 av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
784 av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
785 av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
786 av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
787 s->predictor_history);
788 av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
789 av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
791 av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
792 av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
793 av_log(s->avctx, AV_LOG_DEBUG,
794 "source pcm resolution: %i (%i bits/sample)\n",
795 s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
796 av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
797 av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
798 av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
799 av_log(s->avctx, AV_LOG_DEBUG, "\n");
802 /* Primary audio coding header */
803 s->subframes = get_bits(&s->gb, 4) + 1;
805 return dca_parse_audio_coding_header(s, 0, 0);
809 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
812 /* huffman encoded */
813 value += get_bitalloc(gb, &dca_scalefactor, level);
814 value = av_clip(value, 0, (1 << log2range) - 1);
815 } else if (level < 8) {
816 if (level + 1 > log2range) {
817 skip_bits(gb, level + 1 - log2range);
818 value = get_bits(gb, log2range);
820 value = get_bits(gb, level + 1);
826 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
828 /* Primary audio coding side information */
831 if (get_bits_left(&s->gb) < 0)
832 return AVERROR_INVALIDDATA;
835 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
836 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
839 for (j = base_channel; j < s->prim_channels; j++) {
840 for (k = 0; k < s->subband_activity[j]; k++)
841 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
844 /* Get prediction codebook */
845 for (j = base_channel; j < s->prim_channels; j++) {
846 for (k = 0; k < s->subband_activity[j]; k++) {
847 if (s->prediction_mode[j][k] > 0) {
848 /* (Prediction coefficient VQ address) */
849 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
854 /* Bit allocation index */
855 for (j = base_channel; j < s->prim_channels; j++) {
856 for (k = 0; k < s->vq_start_subband[j]; k++) {
857 if (s->bitalloc_huffman[j] == 6)
858 s->bitalloc[j][k] = get_bits(&s->gb, 5);
859 else if (s->bitalloc_huffman[j] == 5)
860 s->bitalloc[j][k] = get_bits(&s->gb, 4);
861 else if (s->bitalloc_huffman[j] == 7) {
862 av_log(s->avctx, AV_LOG_ERROR,
863 "Invalid bit allocation index\n");
864 return AVERROR_INVALIDDATA;
867 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
870 if (s->bitalloc[j][k] > 26) {
871 av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
872 j, k, s->bitalloc[j][k]);
873 return AVERROR_INVALIDDATA;
878 /* Transition mode */
879 for (j = base_channel; j < s->prim_channels; j++) {
880 for (k = 0; k < s->subband_activity[j]; k++) {
881 s->transition_mode[j][k] = 0;
882 if (s->subsubframes[s->current_subframe] > 1 &&
883 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
884 s->transition_mode[j][k] =
885 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
890 if (get_bits_left(&s->gb) < 0)
891 return AVERROR_INVALIDDATA;
893 for (j = base_channel; j < s->prim_channels; j++) {
894 const uint32_t *scale_table;
895 int scale_sum, log_size;
897 memset(s->scale_factor[j], 0,
898 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
900 if (s->scalefactor_huffman[j] == 6) {
901 scale_table = scale_factor_quant7;
904 scale_table = scale_factor_quant6;
908 /* When huffman coded, only the difference is encoded */
911 for (k = 0; k < s->subband_activity[j]; k++) {
912 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
913 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
914 s->scale_factor[j][k][0] = scale_table[scale_sum];
917 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
918 /* Get second scale factor */
919 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
920 s->scale_factor[j][k][1] = scale_table[scale_sum];
925 /* Joint subband scale factor codebook select */
926 for (j = base_channel; j < s->prim_channels; j++) {
927 /* Transmitted only if joint subband coding enabled */
928 if (s->joint_intensity[j] > 0)
929 s->joint_huff[j] = get_bits(&s->gb, 3);
932 if (get_bits_left(&s->gb) < 0)
933 return AVERROR_INVALIDDATA;
935 /* Scale factors for joint subband coding */
936 for (j = base_channel; j < s->prim_channels; j++) {
939 /* Transmitted only if joint subband coding enabled */
940 if (s->joint_intensity[j] > 0) {
942 source_channel = s->joint_intensity[j] - 1;
944 /* When huffman coded, only the difference is encoded
945 * (is this valid as well for joint scales ???) */
947 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
948 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
949 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
952 if (!(s->debug_flag & 0x02)) {
953 av_log(s->avctx, AV_LOG_DEBUG,
954 "Joint stereo coding not supported\n");
955 s->debug_flag |= 0x02;
960 /* Stereo downmix coefficients */
961 if (!base_channel && s->prim_channels > 2) {
963 for (j = base_channel; j < s->prim_channels; j++) {
964 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
965 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
968 int am = s->amode & DCA_CHANNEL_MASK;
969 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
970 av_log(s->avctx, AV_LOG_ERROR,
971 "Invalid channel mode %d\n", am);
972 return AVERROR_INVALIDDATA;
974 for (j = base_channel; j < FFMIN(s->prim_channels, FF_ARRAY_ELEMS(dca_default_coeffs[am])); j++) {
975 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
976 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
981 /* Dynamic range coefficient */
982 if (!base_channel && s->dynrange)
983 s->dynrange_coef = get_bits(&s->gb, 8);
985 /* Side information CRC check word */
986 if (s->crc_present) {
987 get_bits(&s->gb, 16);
991 * Primary audio data arrays
994 /* VQ encoded high frequency subbands */
995 for (j = base_channel; j < s->prim_channels; j++)
996 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
997 /* 1 vector -> 32 samples */
998 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
1000 /* Low frequency effect data */
1001 if (!base_channel && s->lfe) {
1004 int lfe_samples = 2 * s->lfe * (4 + block_index);
1005 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1008 for (j = lfe_samples; j < lfe_end_sample; j++) {
1009 /* Signed 8 bits int */
1010 s->lfe_data[j] = get_sbits(&s->gb, 8);
1013 /* Scale factor index */
1014 quant7 = get_bits(&s->gb, 8);
1016 av_log_ask_for_sample(s->avctx, "LFEScaleIndex larger than 127\n");
1017 return AVERROR_INVALIDDATA;
1019 s->lfe_scale_factor = scale_factor_quant7[quant7];
1021 /* Quantization step size * scale factor */
1022 lfe_scale = 0.035 * s->lfe_scale_factor;
1024 for (j = lfe_samples; j < lfe_end_sample; j++)
1025 s->lfe_data[j] *= lfe_scale;
1029 av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
1030 s->subsubframes[s->current_subframe]);
1031 av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
1032 s->partial_samples[s->current_subframe]);
1034 for (j = base_channel; j < s->prim_channels; j++) {
1035 av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
1036 for (k = 0; k < s->subband_activity[j]; k++)
1037 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
1038 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1040 for (j = base_channel; j < s->prim_channels; j++) {
1041 for (k = 0; k < s->subband_activity[j]; k++)
1042 av_log(s->avctx, AV_LOG_DEBUG,
1043 "prediction coefs: %f, %f, %f, %f\n",
1044 (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
1045 (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
1046 (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
1047 (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
1049 for (j = base_channel; j < s->prim_channels; j++) {
1050 av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
1051 for (k = 0; k < s->vq_start_subband[j]; k++)
1052 av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
1053 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1055 for (j = base_channel; j < s->prim_channels; j++) {
1056 av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
1057 for (k = 0; k < s->subband_activity[j]; k++)
1058 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
1059 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1061 for (j = base_channel; j < s->prim_channels; j++) {
1062 av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
1063 for (k = 0; k < s->subband_activity[j]; k++) {
1064 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
1065 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
1066 if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
1067 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
1069 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1071 for (j = base_channel; j < s->prim_channels; j++) {
1072 if (s->joint_intensity[j] > 0) {
1073 int source_channel = s->joint_intensity[j] - 1;
1074 av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
1075 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
1076 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
1077 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1080 if (!base_channel && s->prim_channels > 2 && s->downmix) {
1081 av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
1082 for (j = 0; j < s->prim_channels; j++) {
1083 av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
1084 dca_downmix_coeffs[s->downmix_coef[j][0]]);
1085 av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
1086 dca_downmix_coeffs[s->downmix_coef[j][1]]);
1088 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1090 for (j = base_channel; j < s->prim_channels; j++)
1091 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
1092 av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
1093 if (!base_channel && s->lfe) {
1094 int lfe_samples = 2 * s->lfe * (4 + block_index);
1095 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1097 av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
1098 for (j = lfe_samples; j < lfe_end_sample; j++)
1099 av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
1100 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1107 static void qmf_32_subbands(DCAContext *s, int chans,
1108 float samples_in[32][8], float *samples_out,
1111 const float *prCoeff;
1114 int sb_act = s->subband_activity[chans];
1117 scale *= sqrt(1 / 8.0);
1120 if (!s->multirate_inter) /* Non-perfect reconstruction */
1121 prCoeff = fir_32bands_nonperfect;
1122 else /* Perfect reconstruction */
1123 prCoeff = fir_32bands_perfect;
1125 for (i = sb_act; i < 32; i++)
1128 /* Reconstructed channel sample index */
1129 for (subindex = 0; subindex < 8; subindex++) {
1130 /* Load in one sample from each subband and clear inactive subbands */
1131 for (i = 0; i < sb_act; i++) {
1132 unsigned sign = (i - 1) & 2;
1133 uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
1134 AV_WN32A(&s->raXin[i], v);
1137 s->synth.synth_filter_float(&s->imdct,
1138 s->subband_fir_hist[chans],
1139 &s->hist_index[chans],
1140 s->subband_fir_noidea[chans], prCoeff,
1141 samples_out, s->raXin, scale);
1146 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
1147 int num_deci_sample, float *samples_in,
1148 float *samples_out, float scale)
1150 /* samples_in: An array holding decimated samples.
1151 * Samples in current subframe starts from samples_in[0],
1152 * while samples_in[-1], samples_in[-2], ..., stores samples
1153 * from last subframe as history.
1155 * samples_out: An array holding interpolated samples
1159 const float *prCoeff;
1162 /* Select decimation filter */
1163 if (decimation_select == 1) {
1165 prCoeff = lfe_fir_128;
1168 prCoeff = lfe_fir_64;
1171 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1172 s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale);
1174 samples_out += 2 * decifactor;
1178 /* downmixing routines */
1179 #define MIX_REAR1(samples, s1, rs, coef) \
1180 samples[0][i] += samples[s1][i] * coef[rs][0]; \
1181 samples[1][i] += samples[s1][i] * coef[rs][1];
1183 #define MIX_REAR2(samples, s1, s2, rs, coef) \
1184 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
1185 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
1187 #define MIX_FRONT3(samples, coef) \
1188 t = samples[c][i]; \
1189 u = samples[l][i]; \
1190 v = samples[r][i]; \
1191 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1192 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1194 #define DOWNMIX_TO_STEREO(op1, op2) \
1195 for (i = 0; i < 256; i++) { \
1200 static void dca_downmix(float **samples, int srcfmt,
1201 int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
1202 const int8_t *channel_mapping)
1204 int c, l, r, sl, sr, s;
1207 float coef[DCA_PRIM_CHANNELS_MAX][2];
1209 for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) {
1210 coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
1211 coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
1217 case DCA_STEREO_TOTAL:
1218 case DCA_STEREO_SUMDIFF:
1220 av_log(NULL, AV_LOG_ERROR, "Not implemented!\n");
1225 c = channel_mapping[0];
1226 l = channel_mapping[1];
1227 r = channel_mapping[2];
1228 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1231 s = channel_mapping[2];
1232 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1235 c = channel_mapping[0];
1236 l = channel_mapping[1];
1237 r = channel_mapping[2];
1238 s = channel_mapping[3];
1239 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1240 MIX_REAR1(samples, s, 3, coef));
1243 sl = channel_mapping[2];
1244 sr = channel_mapping[3];
1245 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1248 c = channel_mapping[0];
1249 l = channel_mapping[1];
1250 r = channel_mapping[2];
1251 sl = channel_mapping[3];
1252 sr = channel_mapping[4];
1253 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1254 MIX_REAR2(samples, sl, sr, 3, coef));
1260 #ifndef decode_blockcodes
1261 /* Very compact version of the block code decoder that does not use table
1262 * look-up but is slightly slower */
1263 static int decode_blockcode(int code, int levels, int *values)
1266 int offset = (levels - 1) >> 1;
1268 for (i = 0; i < 4; i++) {
1269 int div = FASTDIV(code, levels);
1270 values[i] = code - offset - div * levels;
1277 static int decode_blockcodes(int code1, int code2, int levels, int *values)
1279 return decode_blockcode(code1, levels, values) |
1280 decode_blockcode(code2, levels, values + 4);
1284 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1285 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1287 #ifndef int8x8_fmul_int32
1288 static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
1290 float fscale = scale / 16.0;
1292 for (i = 0; i < 8; i++)
1293 dst[i] = src[i] * fscale;
1297 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1300 int subsubframe = s->current_subsubframe;
1302 const float *quant_step_table;
1305 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1306 LOCAL_ALIGNED_16(int, block, [8]);
1312 /* Select quantization step size table */
1313 if (s->bit_rate_index == 0x1f)
1314 quant_step_table = lossless_quant_d;
1316 quant_step_table = lossy_quant_d;
1318 for (k = base_channel; k < s->prim_channels; k++) {
1319 if (get_bits_left(&s->gb) < 0)
1320 return AVERROR_INVALIDDATA;
1322 for (l = 0; l < s->vq_start_subband[k]; l++) {
1325 /* Select the mid-tread linear quantizer */
1326 int abits = s->bitalloc[k][l];
1328 float quant_step_size = quant_step_table[abits];
1331 * Determine quantization index code book and its type
1334 /* Select quantization index code book */
1335 int sel = s->quant_index_huffman[k][abits];
1338 * Extract bits from the bit stream
1341 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
1343 /* Deal with transients */
1344 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1345 float rscale = quant_step_size * s->scale_factor[k][l][sfi] *
1346 s->scalefactor_adj[k][sel];
1348 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1351 int block_code1, block_code2, size, levels, err;
1353 size = abits_sizes[abits - 1];
1354 levels = abits_levels[abits - 1];
1356 block_code1 = get_bits(&s->gb, size);
1357 block_code2 = get_bits(&s->gb, size);
1358 err = decode_blockcodes(block_code1, block_code2,
1361 av_log(s->avctx, AV_LOG_ERROR,
1362 "ERROR: block code look-up failed\n");
1363 return AVERROR_INVALIDDATA;
1367 for (m = 0; m < 8; m++)
1368 block[m] = get_sbits(&s->gb, abits - 3);
1372 for (m = 0; m < 8; m++)
1373 block[m] = get_bitalloc(&s->gb,
1374 &dca_smpl_bitalloc[abits], sel);
1377 s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
1382 * Inverse ADPCM if in prediction mode
1384 if (s->prediction_mode[k][l]) {
1386 for (m = 0; m < 8; m++) {
1387 for (n = 1; n <= 4; n++)
1389 subband_samples[k][l][m] +=
1390 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1391 subband_samples[k][l][m - n] / 8192);
1392 else if (s->predictor_history)
1393 subband_samples[k][l][m] +=
1394 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1395 s->subband_samples_hist[k][l][m - n + 4] / 8192);
1401 * Decode VQ encoded high frequencies
1403 for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1404 /* 1 vector -> 32 samples but we only need the 8 samples
1405 * for this subsubframe. */
1406 int hfvq = s->high_freq_vq[k][l];
1408 if (!s->debug_flag & 0x01) {
1409 av_log(s->avctx, AV_LOG_DEBUG,
1410 "Stream with high frequencies VQ coding\n");
1411 s->debug_flag |= 0x01;
1414 int8x8_fmul_int32(subband_samples[k][l],
1415 &high_freq_vq[hfvq][subsubframe * 8],
1416 s->scale_factor[k][l][0]);
1420 /* Check for DSYNC after subsubframe */
1421 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1422 if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1424 av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1427 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1431 /* Backup predictor history for adpcm */
1432 for (k = base_channel; k < s->prim_channels; k++)
1433 for (l = 0; l < s->vq_start_subband[k]; l++)
1434 memcpy(s->subband_samples_hist[k][l],
1435 &subband_samples[k][l][4],
1436 4 * sizeof(subband_samples[0][0][0]));
1441 static int dca_filter_channels(DCAContext *s, int block_index)
1443 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1446 /* 32 subbands QMF */
1447 for (k = 0; k < s->prim_channels; k++) {
1448 /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1449 0, 8388608.0, 8388608.0 };*/
1450 if (s->channel_order_tab[k] >= 0)
1451 qmf_32_subbands(s, k, subband_samples[k],
1452 s->samples_chanptr[s->channel_order_tab[k]],
1453 M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
1457 if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
1458 dca_downmix(s->samples_chanptr, s->amode, s->downmix_coef, s->channel_order_tab);
1461 /* Generate LFE samples for this subsubframe FIXME!!! */
1462 if (s->output & DCA_LFE) {
1463 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1464 s->lfe_data + 2 * s->lfe * (block_index + 4),
1465 s->samples_chanptr[s->lfe_index],
1466 1.0 / (256.0 * 32768.0));
1467 /* Outputs 20bits pcm samples */
1474 static int dca_subframe_footer(DCAContext *s, int base_channel)
1476 int aux_data_count = 0, i;
1479 * Unpack optional information
1482 /* presumably optional information only appears in the core? */
1483 if (!base_channel) {
1485 skip_bits_long(&s->gb, 32);
1488 aux_data_count = get_bits(&s->gb, 6);
1490 for (i = 0; i < aux_data_count; i++)
1491 get_bits(&s->gb, 8);
1493 if (s->crc_present && (s->downmix || s->dynrange))
1494 get_bits(&s->gb, 16);
1501 * Decode a dca frame block
1503 * @param s pointer to the DCAContext
1506 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1511 if (s->current_subframe >= s->subframes) {
1512 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1513 s->current_subframe, s->subframes);
1514 return AVERROR_INVALIDDATA;
1517 if (!s->current_subsubframe) {
1519 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1521 /* Read subframe header */
1522 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1526 /* Read subsubframe */
1528 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1530 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1534 s->current_subsubframe++;
1535 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1536 s->current_subsubframe = 0;
1537 s->current_subframe++;
1539 if (s->current_subframe >= s->subframes) {
1541 av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1543 /* Read subframe footer */
1544 if ((ret = dca_subframe_footer(s, base_channel)))
1552 * Return the number of channels in an ExSS speaker mask (HD)
1554 static int dca_exss_mask2count(int mask)
1556 /* count bits that mean speaker pairs twice */
1557 return av_popcount(mask) +
1558 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1559 DCA_EXSS_FRONT_LEFT_RIGHT |
1560 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1561 DCA_EXSS_WIDE_LEFT_RIGHT |
1562 DCA_EXSS_SIDE_LEFT_RIGHT |
1563 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1564 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1565 DCA_EXSS_REAR_LEFT_RIGHT |
1566 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1570 * Skip mixing coefficients of a single mix out configuration (HD)
1572 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1576 for (i = 0; i < channels; i++) {
1577 int mix_map_mask = get_bits(gb, out_ch);
1578 int num_coeffs = av_popcount(mix_map_mask);
1579 skip_bits_long(gb, num_coeffs * 6);
1584 * Parse extension substream asset header (HD)
1586 static int dca_exss_parse_asset_header(DCAContext *s)
1588 int header_pos = get_bits_count(&s->gb);
1591 int embedded_stereo = 0;
1592 int embedded_6ch = 0;
1593 int drc_code_present;
1594 int av_uninit(extensions_mask);
1597 if (get_bits_left(&s->gb) < 16)
1600 /* We will parse just enough to get to the extensions bitmask with which
1601 * we can set the profile value. */
1603 header_size = get_bits(&s->gb, 9) + 1;
1604 skip_bits(&s->gb, 3); // asset index
1606 if (s->static_fields) {
1607 if (get_bits1(&s->gb))
1608 skip_bits(&s->gb, 4); // asset type descriptor
1609 if (get_bits1(&s->gb))
1610 skip_bits_long(&s->gb, 24); // language descriptor
1612 if (get_bits1(&s->gb)) {
1613 /* How can one fit 1024 bytes of text here if the maximum value
1614 * for the asset header size field above was 512 bytes? */
1615 int text_length = get_bits(&s->gb, 10) + 1;
1616 if (get_bits_left(&s->gb) < text_length * 8)
1618 skip_bits_long(&s->gb, text_length * 8); // info text
1621 skip_bits(&s->gb, 5); // bit resolution - 1
1622 skip_bits(&s->gb, 4); // max sample rate code
1623 channels = get_bits(&s->gb, 8) + 1;
1625 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1626 int spkr_remap_sets;
1627 int spkr_mask_size = 16;
1631 embedded_stereo = get_bits1(&s->gb);
1633 embedded_6ch = get_bits1(&s->gb);
1635 if (get_bits1(&s->gb)) {
1636 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1637 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1640 spkr_remap_sets = get_bits(&s->gb, 3);
1642 for (i = 0; i < spkr_remap_sets; i++) {
1643 /* std layout mask for each remap set */
1644 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1647 for (i = 0; i < spkr_remap_sets; i++) {
1648 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1649 if (get_bits_left(&s->gb) < 0)
1652 for (j = 0; j < num_spkrs[i]; j++) {
1653 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1654 int num_dec_ch = av_popcount(remap_dec_ch_mask);
1655 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1660 skip_bits(&s->gb, 3); // representation type
1664 drc_code_present = get_bits1(&s->gb);
1665 if (drc_code_present)
1666 get_bits(&s->gb, 8); // drc code
1668 if (get_bits1(&s->gb))
1669 skip_bits(&s->gb, 5); // dialog normalization code
1671 if (drc_code_present && embedded_stereo)
1672 get_bits(&s->gb, 8); // drc stereo code
1674 if (s->mix_metadata && get_bits1(&s->gb)) {
1675 skip_bits(&s->gb, 1); // external mix
1676 skip_bits(&s->gb, 6); // post mix gain code
1678 if (get_bits(&s->gb, 2) != 3) // mixer drc code
1679 skip_bits(&s->gb, 3); // drc limit
1681 skip_bits(&s->gb, 8); // custom drc code
1683 if (get_bits1(&s->gb)) // channel specific scaling
1684 for (i = 0; i < s->num_mix_configs; i++)
1685 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1687 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1689 for (i = 0; i < s->num_mix_configs; i++) {
1690 if (get_bits_left(&s->gb) < 0)
1692 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1694 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1695 if (embedded_stereo)
1696 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1700 switch (get_bits(&s->gb, 2)) {
1701 case 0: extensions_mask = get_bits(&s->gb, 12); break;
1702 case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1703 case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1704 case 3: extensions_mask = 0; /* aux coding */ break;
1707 /* not parsed further, we were only interested in the extensions mask */
1709 if (get_bits_left(&s->gb) < 0)
1712 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1713 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1716 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1718 if (extensions_mask & DCA_EXT_EXSS_XLL)
1719 s->profile = FF_PROFILE_DTS_HD_MA;
1720 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1722 s->profile = FF_PROFILE_DTS_HD_HRA;
1724 if (!(extensions_mask & DCA_EXT_CORE))
1725 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1726 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1727 av_log(s->avctx, AV_LOG_WARNING,
1728 "DTS extensions detection mismatch (%d, %d)\n",
1729 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1734 static int dca_xbr_parse_frame(DCAContext *s)
1736 int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1737 int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1738 int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1739 int anctemp[DCA_CHSET_CHANS_MAX];
1740 int chset_fsize[DCA_CHSETS_MAX];
1741 int n_xbr_ch[DCA_CHSETS_MAX];
1742 int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1743 int i, j, k, l, chset, chan_base;
1745 av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1747 /* get bit position of sync header */
1748 hdr_pos = get_bits_count(&s->gb) - 32;
1750 hdr_size = get_bits(&s->gb, 6) + 1;
1751 num_chsets = get_bits(&s->gb, 2) + 1;
1753 for(i = 0; i < num_chsets; i++)
1754 chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1756 xbr_tmode = get_bits1(&s->gb);
1758 for(i = 0; i < num_chsets; i++) {
1759 n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1760 k = get_bits(&s->gb, 2) + 5;
1761 for(j = 0; j < n_xbr_ch[i]; j++)
1762 active_bands[i][j] = get_bits(&s->gb, k) + 1;
1765 /* skip to the end of the header */
1766 i = get_bits_count(&s->gb);
1767 if(hdr_pos + hdr_size * 8 > i)
1768 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1770 /* loop over the channel data sets */
1771 /* only decode as many channels as we've decoded base data for */
1772 for(chset = 0, chan_base = 0;
1773 chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1774 chan_base += n_xbr_ch[chset++]) {
1775 int start_posn = get_bits_count(&s->gb);
1776 int subsubframe = 0;
1779 /* loop over subframes */
1780 for (k = 0; k < (s->sample_blocks / 8); k++) {
1781 /* parse header if we're on first subsubframe of a block */
1782 if(subsubframe == 0) {
1783 /* Parse subframe header */
1784 for(i = 0; i < n_xbr_ch[chset]; i++) {
1785 anctemp[i] = get_bits(&s->gb, 2) + 2;
1788 for(i = 0; i < n_xbr_ch[chset]; i++) {
1789 get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1792 for(i = 0; i < n_xbr_ch[chset]; i++) {
1793 anctemp[i] = get_bits(&s->gb, 3);
1794 if(anctemp[i] < 1) {
1795 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1796 return AVERROR_INVALIDDATA;
1800 /* generate scale factors */
1801 for(i = 0; i < n_xbr_ch[chset]; i++) {
1802 const uint32_t *scale_table;
1805 if (s->scalefactor_huffman[chan_base+i] == 6) {
1806 scale_table = scale_factor_quant7;
1808 scale_table = scale_factor_quant6;
1813 for(j = 0; j < active_bands[chset][i]; j++) {
1814 if(abits_high[i][j] > 0) {
1815 scale_table_high[i][j][0] =
1816 scale_table[get_bits(&s->gb, nbits)];
1818 if(xbr_tmode && s->transition_mode[i][j]) {
1819 scale_table_high[i][j][1] =
1820 scale_table[get_bits(&s->gb, nbits)];
1827 /* decode audio array for this block */
1828 for(i = 0; i < n_xbr_ch[chset]; i++) {
1829 for(j = 0; j < active_bands[chset][i]; j++) {
1830 const int xbr_abits = abits_high[i][j];
1831 const float quant_step_size = lossless_quant_d[xbr_abits];
1832 const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1833 const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1834 float *subband_samples = s->subband_samples[k][chan_base+i][j];
1841 get_array(&s->gb, block, 8, xbr_abits - 3);
1843 int block_code1, block_code2, size, levels, err;
1845 size = abits_sizes[xbr_abits - 1];
1846 levels = abits_levels[xbr_abits - 1];
1848 block_code1 = get_bits(&s->gb, size);
1849 block_code2 = get_bits(&s->gb, size);
1850 err = decode_blockcodes(block_code1, block_code2,
1853 av_log(s->avctx, AV_LOG_ERROR,
1854 "ERROR: DTS-XBR: block code look-up failed\n");
1855 return AVERROR_INVALIDDATA;
1859 /* scale & sum into subband */
1860 for(l = 0; l < 8; l++)
1861 subband_samples[l] += (float)block[l] * rscale;
1865 /* check DSYNC marker */
1866 if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1867 if(get_bits(&s->gb, 16) != 0xffff) {
1868 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1869 return AVERROR_INVALIDDATA;
1873 /* advance sub-sub-frame index */
1874 if(++subsubframe >= s->subsubframes[subframe]) {
1880 /* skip to next channel set */
1881 i = get_bits_count(&s->gb);
1882 if(start_posn + chset_fsize[chset] * 8 != i) {
1883 j = start_posn + chset_fsize[chset] * 8 - i;
1885 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1886 " skipping further than expected (%d bits)\n", j);
1887 skip_bits_long(&s->gb, j);
1894 /* parse initial header for XXCH and dump details */
1895 static int dca_xxch_decode_frame(DCAContext *s)
1897 int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1898 int i, chset, base_channel, chstart, fsize[8];
1900 /* assume header word has already been parsed */
1901 hdr_pos = get_bits_count(&s->gb) - 32;
1902 hdr_size = get_bits(&s->gb, 6) + 1;
1903 /*chhdr_crc =*/ skip_bits1(&s->gb);
1904 spkmsk_bits = get_bits(&s->gb, 5) + 1;
1905 num_chsets = get_bits(&s->gb, 2) + 1;
1907 for (i = 0; i < num_chsets; i++)
1908 fsize[i] = get_bits(&s->gb, 14) + 1;
1910 core_spk = get_bits(&s->gb, spkmsk_bits);
1911 s->xxch_core_spkmask = core_spk;
1912 s->xxch_nbits_spk_mask = spkmsk_bits;
1913 s->xxch_dmix_embedded = 0;
1915 /* skip to the end of the header */
1916 i = get_bits_count(&s->gb);
1917 if (hdr_pos + hdr_size * 8 > i)
1918 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1920 for (chset = 0; chset < num_chsets; chset++) {
1921 chstart = get_bits_count(&s->gb);
1922 base_channel = s->prim_channels;
1923 s->xxch_chset = chset;
1925 /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1926 5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1927 dca_parse_audio_coding_header(s, base_channel, 1);
1929 /* decode channel data */
1930 for (i = 0; i < (s->sample_blocks / 8); i++) {
1931 if (dca_decode_block(s, base_channel, i)) {
1932 av_log(s->avctx, AV_LOG_ERROR,
1933 "Error decoding DTS-XXCH extension\n");
1938 /* skip to end of this section */
1939 i = get_bits_count(&s->gb);
1940 if (chstart + fsize[chset] * 8 > i)
1941 skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1943 s->xxch_chset = num_chsets;
1949 * Parse extension substream header (HD)
1951 static void dca_exss_parse_header(DCAContext *s)
1958 int active_ss_mask[8];
1964 if (get_bits_left(&s->gb) < 52)
1967 start_posn = get_bits_count(&s->gb) - 32;
1969 skip_bits(&s->gb, 8); // user data
1970 ss_index = get_bits(&s->gb, 2);
1972 blownup = get_bits1(&s->gb);
1973 hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
1974 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1976 s->static_fields = get_bits1(&s->gb);
1977 if (s->static_fields) {
1978 skip_bits(&s->gb, 2); // reference clock code
1979 skip_bits(&s->gb, 3); // frame duration code
1981 if (get_bits1(&s->gb))
1982 skip_bits_long(&s->gb, 36); // timestamp
1984 /* a single stream can contain multiple audio assets that can be
1985 * combined to form multiple audio presentations */
1987 num_audiop = get_bits(&s->gb, 3) + 1;
1988 if (num_audiop > 1) {
1989 av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
1990 /* ignore such streams for now */
1994 num_assets = get_bits(&s->gb, 3) + 1;
1995 if (num_assets > 1) {
1996 av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
1997 /* ignore such streams for now */
2001 for (i = 0; i < num_audiop; i++)
2002 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
2004 for (i = 0; i < num_audiop; i++)
2005 for (j = 0; j <= ss_index; j++)
2006 if (active_ss_mask[i] & (1 << j))
2007 skip_bits(&s->gb, 8); // active asset mask
2009 s->mix_metadata = get_bits1(&s->gb);
2010 if (s->mix_metadata) {
2011 int mix_out_mask_size;
2013 skip_bits(&s->gb, 2); // adjustment level
2014 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
2015 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
2017 for (i = 0; i < s->num_mix_configs; i++) {
2018 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
2019 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
2024 for (i = 0; i < num_assets; i++)
2025 asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
2027 for (i = 0; i < num_assets; i++) {
2028 if (dca_exss_parse_asset_header(s))
2032 /* not parsed further, we were only interested in the extensions mask
2033 * from the asset header */
2035 if (num_assets > 0) {
2036 j = get_bits_count(&s->gb);
2037 if (start_posn + hdrsize * 8 > j)
2038 skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
2040 for (i = 0; i < num_assets; i++) {
2041 start_posn = get_bits_count(&s->gb);
2042 mkr = get_bits_long(&s->gb, 32);
2044 /* parse extensions that we know about */
2045 if (mkr == 0x655e315e) {
2046 dca_xbr_parse_frame(s);
2047 } else if (mkr == 0x47004a03) {
2048 dca_xxch_decode_frame(s);
2049 s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
2051 av_log(s->avctx, AV_LOG_DEBUG,
2052 "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
2055 /* skip to end of block */
2056 j = get_bits_count(&s->gb);
2057 if (start_posn + asset_size[i] * 8 > j)
2058 skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
2064 * Main frame decoding function
2065 * FIXME add arguments
2067 static int dca_decode_frame(AVCodecContext *avctx, void *data,
2068 int *got_frame_ptr, AVPacket *avpkt)
2070 const uint8_t *buf = avpkt->data;
2071 int buf_size = avpkt->size;
2075 int num_core_channels = 0;
2077 float **samples_flt;
2080 DCAContext *s = avctx->priv_data;
2082 int channels, full_channels;
2094 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
2095 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
2096 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
2097 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
2098 return AVERROR_INVALIDDATA;
2101 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
2102 if ((ret = dca_parse_frame_header(s)) < 0) {
2103 //seems like the frame is corrupt, try with the next one
2106 //set AVCodec values with parsed data
2107 avctx->sample_rate = s->sample_rate;
2108 avctx->bit_rate = s->bit_rate;
2110 s->profile = FF_PROFILE_DTS;
2112 for (i = 0; i < (s->sample_blocks / 8); i++) {
2113 if ((ret = dca_decode_block(s, 0, i))) {
2114 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
2119 /* record number of core channels incase less than max channels are requested */
2120 num_core_channels = s->prim_channels;
2123 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
2125 s->core_ext_mask = 0;
2127 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
2129 /* only scan for extensions if ext_descr was unknown or indicated a
2130 * supported XCh extension */
2131 if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
2133 /* if ext_descr was unknown, clear s->core_ext_mask so that the
2134 * extensions scan can fill it up */
2135 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
2137 /* extensions start at 32-bit boundaries into bitstream */
2138 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2140 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
2141 uint32_t bits = get_bits_long(&s->gb, 32);
2145 int ext_amode, xch_fsize;
2147 s->xch_base_channel = s->prim_channels;
2149 /* validate sync word using XCHFSIZE field */
2150 xch_fsize = show_bits(&s->gb, 10);
2151 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
2152 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
2155 /* skip length-to-end-of-frame field for the moment */
2156 skip_bits(&s->gb, 10);
2158 s->core_ext_mask |= DCA_EXT_XCH;
2160 /* extension amode(number of channels in extension) should be 1 */
2161 /* AFAIK XCh is not used for more channels */
2162 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
2163 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
2164 " supported!\n", ext_amode);
2168 if (s->xch_base_channel < 2) {
2169 av_log_ask_for_sample(avctx, "XCh with fewer than 2 base channels is not supported\n");
2173 /* much like core primary audio coding header */
2174 dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
2176 for (i = 0; i < (s->sample_blocks / 8); i++)
2177 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
2178 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
2186 /* XXCh: extended channels */
2187 /* usually found either in core or HD part in DTS-HD HRA streams,
2188 * but not in DTS-ES which contains XCh extensions instead */
2189 s->core_ext_mask |= DCA_EXT_XXCH;
2190 dca_xxch_decode_frame(s);
2194 int fsize96 = show_bits(&s->gb, 12) + 1;
2195 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
2198 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
2199 get_bits_count(&s->gb));
2200 skip_bits(&s->gb, 12);
2201 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
2202 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
2204 s->core_ext_mask |= DCA_EXT_X96;
2209 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2212 /* no supported extensions, skip the rest of the core substream */
2213 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
2216 if (s->core_ext_mask & DCA_EXT_X96)
2217 s->profile = FF_PROFILE_DTS_96_24;
2218 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
2219 s->profile = FF_PROFILE_DTS_ES;
2221 /* check for ExSS (HD part) */
2222 if (s->dca_buffer_size - s->frame_size > 32 &&
2223 get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
2224 dca_exss_parse_header(s);
2226 avctx->profile = s->profile;
2228 full_channels = channels = s->prim_channels + !!s->lfe;
2230 /* If we have XXCH then the channel layout is managed differently */
2231 /* note that XLL will also have another way to do things */
2232 if (!(s->core_ext_mask & DCA_EXT_XXCH)
2233 || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
2234 && avctx->request_channels
2235 < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
2236 { /* xxx should also do MA extensions */
2237 if (s->amode < 16) {
2238 avctx->channel_layout = dca_core_channel_layout[s->amode];
2240 if (s->xch_present && (!avctx->request_channels ||
2241 avctx->request_channels
2242 > num_core_channels + !!s->lfe)) {
2243 avctx->channel_layout |= AV_CH_BACK_CENTER;
2245 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2246 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
2248 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
2250 if (s->channel_order_tab[s->xch_base_channel] < 0)
2251 return AVERROR_INVALIDDATA;
2253 channels = num_core_channels + !!s->lfe;
2254 s->xch_present = 0; /* disable further xch processing */
2256 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2257 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
2259 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
2262 if (channels > !!s->lfe &&
2263 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
2264 return AVERROR_INVALIDDATA;
2266 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != channels) {
2267 av_log(avctx, AV_LOG_ERROR, "Number of channels %d mismatches layout %d\n", channels, av_get_channel_layout_nb_channels(avctx->channel_layout));
2268 return AVERROR_INVALIDDATA;
2271 if (avctx->request_channels == 2 && s->prim_channels > 2) {
2273 s->output = DCA_STEREO;
2274 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
2276 else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
2277 static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2278 s->channel_order_tab = dca_channel_order_native;
2280 s->lfe_index = dca_lfe_index[s->amode];
2282 av_log(avctx, AV_LOG_ERROR,
2283 "Non standard configuration %d !\n", s->amode);
2284 return AVERROR_INVALIDDATA;
2287 s->xxch_dmix_embedded = 0;
2289 /* we only get here if an XXCH channel set can be added to the mix */
2290 channel_mask = s->xxch_core_spkmask;
2292 if (avctx->request_channels > 0
2293 && avctx->request_channels < s->prim_channels) {
2294 channels = num_core_channels + !!s->lfe;
2295 for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
2296 <= avctx->request_channels; i++) {
2297 channels += s->xxch_chset_nch[i];
2298 channel_mask |= s->xxch_spk_masks[i];
2301 channels = s->prim_channels + !!s->lfe;
2302 for (i = 0; i < s->xxch_chset; i++) {
2303 channel_mask |= s->xxch_spk_masks[i];
2307 /* Given the DTS spec'ed channel mask, generate an avcodec version */
2309 for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
2310 if (channel_mask & (1 << i)) {
2311 channel_layout |= map_xxch_to_native[i];
2315 /* make sure that we have managed to get equivelant dts/avcodec channel
2316 * masks in some sense -- unfortunately some channels could overlap */
2317 if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
2318 av_log(avctx, AV_LOG_DEBUG,
2319 "DTS-XXCH: Inconsistant avcodec/dts channel layouts\n");
2320 return AVERROR_INVALIDDATA;
2323 avctx->channel_layout = channel_layout;
2325 if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
2326 /* Estimate DTS --> avcodec ordering table */
2327 for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
2328 mask = chset >= 0 ? s->xxch_spk_masks[chset]
2329 : s->xxch_core_spkmask;
2330 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
2331 if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
2332 lavc = map_xxch_to_native[i];
2333 posn = av_popcount(channel_layout & (lavc - 1));
2334 s->xxch_order_tab[j++] = posn;
2339 s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
2340 } else { /* native ordering */
2341 for (i = 0; i < channels; i++)
2342 s->xxch_order_tab[i] = i;
2344 s->lfe_index = channels - 1;
2347 s->channel_order_tab = s->xxch_order_tab;
2350 if (avctx->channels != channels) {
2351 if (avctx->channels)
2352 av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
2353 avctx->channels = channels;
2356 /* get output buffer */
2357 s->frame.nb_samples = 256 * (s->sample_blocks / 8);
2358 if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
2359 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2362 samples_flt = (float **) s->frame.extended_data;
2364 /* allocate buffer for extra channels if downmixing */
2365 if (avctx->channels < full_channels) {
2366 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
2367 s->frame.nb_samples,
2368 avctx->sample_fmt, 0);
2372 av_fast_malloc(&s->extra_channels_buffer,
2373 &s->extra_channels_buffer_size, ret);
2374 if (!s->extra_channels_buffer)
2375 return AVERROR(ENOMEM);
2377 ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
2378 s->extra_channels_buffer,
2379 full_channels - channels,
2380 s->frame.nb_samples, avctx->sample_fmt, 0);
2385 /* filter to get final output */
2386 for (i = 0; i < (s->sample_blocks / 8); i++) {
2389 for (ch = 0; ch < channels; ch++)
2390 s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
2391 for (; ch < full_channels; ch++)
2392 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
2394 dca_filter_channels(s, i);
2396 /* If this was marked as a DTS-ES stream we need to subtract back- */
2397 /* channel from SL & SR to remove matrixed back-channel signal */
2398 if ((s->source_pcm_res & 1) && s->xch_present) {
2399 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
2400 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
2401 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
2402 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2403 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2406 /* If stream contains XXCH, we might need to undo an embedded downmix */
2407 if (s->xxch_dmix_embedded) {
2408 /* Loop over channel sets in turn */
2409 ch = num_core_channels;
2410 for (chset = 0; chset < s->xxch_chset; chset++) {
2411 endch = ch + s->xxch_chset_nch[chset];
2412 mask = s->xxch_dmix_embedded;
2415 for (j = ch; j < endch; j++) {
2416 if (mask & (1 << j)) { /* this channel has been mixed-out */
2417 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2418 for (k = 0; k < endch; k++) {
2419 achan = s->channel_order_tab[k];
2420 scale = s->xxch_dmix_coeff[j][k];
2422 dst_chan = s->samples_chanptr[achan];
2423 s->fdsp.vector_fmac_scalar(dst_chan, src_chan,
2430 /* if a downmix has been embedded then undo the pre-scaling */
2431 if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
2432 scale = s->xxch_dmix_sf[chset];
2434 for (j = 0; j < ch; j++) {
2435 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2436 for (k = 0; k < 256; k++)
2437 src_chan[k] *= scale;
2440 /* LFE channel is always part of core, scale if it exists */
2442 src_chan = s->samples_chanptr[s->lfe_index];
2443 for (k = 0; k < 256; k++)
2444 src_chan[k] *= scale;
2454 /* update lfe history */
2455 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2456 for (i = 0; i < 2 * s->lfe * 4; i++)
2457 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2460 *(AVFrame *) data = s->frame;
2468 * DCA initialization
2470 * @param avctx pointer to the AVCodecContext
2473 static av_cold int dca_decode_init(AVCodecContext *avctx)
2475 DCAContext *s = avctx->priv_data;
2480 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
2481 ff_mdct_init(&s->imdct, 6, 1, 1.0);
2482 ff_synth_filter_init(&s->synth);
2483 ff_dcadsp_init(&s->dcadsp);
2484 ff_fmt_convert_init(&s->fmt_conv, avctx);
2486 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2488 /* allow downmixing to stereo */
2489 if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
2490 avctx->request_channels == 2) {
2491 avctx->channels = avctx->request_channels;
2494 avcodec_get_frame_defaults(&s->frame);
2495 avctx->coded_frame = &s->frame;
2500 static av_cold int dca_decode_end(AVCodecContext *avctx)
2502 DCAContext *s = avctx->priv_data;
2503 ff_mdct_end(&s->imdct);
2504 av_freep(&s->extra_channels_buffer);
2508 static const AVProfile profiles[] = {
2509 { FF_PROFILE_DTS, "DTS" },
2510 { FF_PROFILE_DTS_ES, "DTS-ES" },
2511 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2512 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2513 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2514 { FF_PROFILE_UNKNOWN },
2517 AVCodec ff_dca_decoder = {
2519 .type = AVMEDIA_TYPE_AUDIO,
2520 .id = AV_CODEC_ID_DTS,
2521 .priv_data_size = sizeof(DCAContext),
2522 .init = dca_decode_init,
2523 .decode = dca_decode_frame,
2524 .close = dca_decode_end,
2525 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2526 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2527 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2528 AV_SAMPLE_FMT_NONE },
2529 .profiles = NULL_IF_CONFIG_SMALL(profiles),