]> git.sesse.net Git - ffmpeg/blob - libavcodec/dca.c
Merge commit '1961e46c15c23a041f8d8614a25388a3ee9eff63'
[ffmpeg] / libavcodec / dca.c
1 /*
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
7  *
8  * This file is part of FFmpeg.
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include <math.h>
26 #include <stddef.h>
27 #include <stdio.h>
28
29 #include "libavutil/common.h"
30 #include "libavutil/float_dsp.h"
31 #include "libavutil/intmath.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/audioconvert.h"
35 #include "avcodec.h"
36 #include "dsputil.h"
37 #include "fft.h"
38 #include "get_bits.h"
39 #include "put_bits.h"
40 #include "dcadata.h"
41 #include "dcahuff.h"
42 #include "dca.h"
43 #include "dca_parser.h"
44 #include "synth_filter.h"
45 #include "dcadsp.h"
46 #include "fmtconvert.h"
47
48 #if ARCH_ARM
49 #   include "arm/dca.h"
50 #endif
51
52 //#define TRACE
53
54 #define DCA_PRIM_CHANNELS_MAX  (7)
55 #define DCA_SUBBANDS          (64)
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)
63
64 enum DCAMode {
65     DCA_MONO = 0,
66     DCA_CHANNEL,
67     DCA_STEREO,
68     DCA_STEREO_SUMDIFF,
69     DCA_STEREO_TOTAL,
70     DCA_3F,
71     DCA_2F1R,
72     DCA_3F1R,
73     DCA_2F2R,
74     DCA_3F2R,
75     DCA_4F2R
76 };
77
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,
96 };
97
98 enum DCAExtensionMask {
99     DCA_EXT_CORE       = 0x001, ///< core in core substream
100     DCA_EXT_XXCH       = 0x002, ///< XXCh channels extension in core substream
101     DCA_EXT_X96        = 0x004, ///< 96/24 extension in core substream
102     DCA_EXT_XCH        = 0x008, ///< XCh channel extension in core substream
103     DCA_EXT_EXSS_CORE  = 0x010, ///< core in ExSS (extension substream)
104     DCA_EXT_EXSS_XBR   = 0x020, ///< extended bitrate extension in ExSS
105     DCA_EXT_EXSS_XXCH  = 0x040, ///< XXCh channels extension in ExSS
106     DCA_EXT_EXSS_X96   = 0x080, ///< 96/24 extension in ExSS
107     DCA_EXT_EXSS_LBR   = 0x100, ///< low bitrate component in ExSS
108     DCA_EXT_EXSS_XLL   = 0x200, ///< lossless extension in ExSS
109 };
110
111 /* -1 are reserved or unknown */
112 static const int dca_ext_audio_descr_mask[] = {
113     DCA_EXT_XCH,
114     -1,
115     DCA_EXT_X96,
116     DCA_EXT_XCH | DCA_EXT_X96,
117     -1,
118     -1,
119     DCA_EXT_XXCH,
120     -1,
121 };
122
123 /* extensions that reside in core substream */
124 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
125
126 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
127  * Some compromises have been made for special configurations. Most configurations
128  * are never used so complete accuracy is not needed.
129  *
130  * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
131  * S  -> side, when both rear and back are configured move one of them to the side channel
132  * OV -> center back
133  * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
134  */
135 static const uint64_t dca_core_channel_layout[] = {
136     AV_CH_FRONT_CENTER,                                                     ///< 1, A
137     AV_CH_LAYOUT_STEREO,                                                    ///< 2, A + B (dual mono)
138     AV_CH_LAYOUT_STEREO,                                                    ///< 2, L + R (stereo)
139     AV_CH_LAYOUT_STEREO,                                                    ///< 2, (L + R) + (L - R) (sum-difference)
140     AV_CH_LAYOUT_STEREO,                                                    ///< 2, LT + RT (left and right total)
141     AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER,                               ///< 3, C + L + R
142     AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER,                                ///< 3, L + R + S
143     AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER,           ///< 4, C + L + R + S
144     AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,               ///< 4, L + R + SL + SR
145
146     AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
147     AV_CH_SIDE_RIGHT,                                                       ///< 5, C + L + R + SL + SR
148
149     AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
150     AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER,               ///< 6, CL + CR + L + R + SL + SR
151
152     AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
153     AV_CH_FRONT_CENTER  | AV_CH_BACK_CENTER,                                ///< 6, C + L + R + LR + RR + OV
154
155     AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
156     AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER   |
157     AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT,                                     ///< 6, CF + CR + LF + RF + LR + RR
158
159     AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER   |
160     AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
161     AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,                                     ///< 7, CL + C + CR + L + R + SL + SR
162
163     AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
164     AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
165     AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT,                                     ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
166
167     AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER   |
168     AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
169     AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT,                 ///< 8, CL + C + CR + L + R + SL + S + SR
170 };
171
172 static const int8_t dca_lfe_index[] = {
173     1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
174 };
175
176 static const int8_t dca_channel_reorder_lfe[][9] = {
177     { 0, -1, -1, -1, -1, -1, -1, -1, -1},
178     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
179     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
180     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
181     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
182     { 2,  0,  1, -1, -1, -1, -1, -1, -1},
183     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
184     { 2,  0,  1,  4, -1, -1, -1, -1, -1},
185     { 0,  1,  3,  4, -1, -1, -1, -1, -1},
186     { 2,  0,  1,  4,  5, -1, -1, -1, -1},
187     { 3,  4,  0,  1,  5,  6, -1, -1, -1},
188     { 2,  0,  1,  4,  5,  6, -1, -1, -1},
189     { 0,  6,  4,  5,  2,  3, -1, -1, -1},
190     { 4,  2,  5,  0,  1,  6,  7, -1, -1},
191     { 5,  6,  0,  1,  7,  3,  8,  4, -1},
192     { 4,  2,  5,  0,  1,  6,  8,  7, -1},
193 };
194
195 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
196     { 0,  2, -1, -1, -1, -1, -1, -1, -1},
197     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
198     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
199     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
200     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
201     { 2,  0,  1,  4, -1, -1, -1, -1, -1},
202     { 0,  1,  3,  4, -1, -1, -1, -1, -1},
203     { 2,  0,  1,  4,  5, -1, -1, -1, -1},
204     { 0,  1,  4,  5,  3, -1, -1, -1, -1},
205     { 2,  0,  1,  5,  6,  4, -1, -1, -1},
206     { 3,  4,  0,  1,  6,  7,  5, -1, -1},
207     { 2,  0,  1,  4,  5,  6,  7, -1, -1},
208     { 0,  6,  4,  5,  2,  3,  7, -1, -1},
209     { 4,  2,  5,  0,  1,  7,  8,  6, -1},
210     { 5,  6,  0,  1,  8,  3,  9,  4,  7},
211     { 4,  2,  5,  0,  1,  6,  9,  8,  7},
212 };
213
214 static const int8_t dca_channel_reorder_nolfe[][9] = {
215     { 0, -1, -1, -1, -1, -1, -1, -1, -1},
216     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
217     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
218     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
219     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
220     { 2,  0,  1, -1, -1, -1, -1, -1, -1},
221     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
222     { 2,  0,  1,  3, -1, -1, -1, -1, -1},
223     { 0,  1,  2,  3, -1, -1, -1, -1, -1},
224     { 2,  0,  1,  3,  4, -1, -1, -1, -1},
225     { 2,  3,  0,  1,  4,  5, -1, -1, -1},
226     { 2,  0,  1,  3,  4,  5, -1, -1, -1},
227     { 0,  5,  3,  4,  1,  2, -1, -1, -1},
228     { 3,  2,  4,  0,  1,  5,  6, -1, -1},
229     { 4,  5,  0,  1,  6,  2,  7,  3, -1},
230     { 3,  2,  4,  0,  1,  5,  7,  6, -1},
231 };
232
233 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
234     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
235     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
236     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
237     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
238     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
239     { 2,  0,  1,  3, -1, -1, -1, -1, -1},
240     { 0,  1,  2,  3, -1, -1, -1, -1, -1},
241     { 2,  0,  1,  3,  4, -1, -1, -1, -1},
242     { 0,  1,  3,  4,  2, -1, -1, -1, -1},
243     { 2,  0,  1,  4,  5,  3, -1, -1, -1},
244     { 2,  3,  0,  1,  5,  6,  4, -1, -1},
245     { 2,  0,  1,  3,  4,  5,  6, -1, -1},
246     { 0,  5,  3,  4,  1,  2,  6, -1, -1},
247     { 3,  2,  4,  0,  1,  6,  7,  5, -1},
248     { 4,  5,  0,  1,  7,  2,  8,  3,  6},
249     { 3,  2,  4,  0,  1,  5,  8,  7,  6},
250 };
251
252 #define DCA_DOLBY                  101           /* FIXME */
253
254 #define DCA_CHANNEL_BITS             6
255 #define DCA_CHANNEL_MASK          0x3F
256
257 #define DCA_LFE                   0x80
258
259 #define HEADER_SIZE                 14
260
261 #define DCA_MAX_FRAME_SIZE       16384
262 #define DCA_MAX_EXSS_HEADER_SIZE  4096
263
264 #define DCA_BUFFER_PADDING_SIZE   1024
265
266 /** Bit allocation */
267 typedef struct {
268     int offset;                 ///< code values offset
269     int maxbits[8];             ///< max bits in VLC
270     int wrap;                   ///< wrap for get_vlc2()
271     VLC vlc[8];                 ///< actual codes
272 } BitAlloc;
273
274 static BitAlloc dca_bitalloc_index;    ///< indexes for samples VLC select
275 static BitAlloc dca_tmode;             ///< transition mode VLCs
276 static BitAlloc dca_scalefactor;       ///< scalefactor VLCs
277 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
278
279 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
280                                          int idx)
281 {
282     return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
283            ba->offset;
284 }
285
286 typedef struct {
287     AVCodecContext *avctx;
288     AVFrame frame;
289     /* Frame header */
290     int frame_type;             ///< type of the current frame
291     int samples_deficit;        ///< deficit sample count
292     int crc_present;            ///< crc is present in the bitstream
293     int sample_blocks;          ///< number of PCM sample blocks
294     int frame_size;             ///< primary frame byte size
295     int amode;                  ///< audio channels arrangement
296     int sample_rate;            ///< audio sampling rate
297     int bit_rate;               ///< transmission bit rate
298     int bit_rate_index;         ///< transmission bit rate index
299
300     int downmix;                ///< embedded downmix enabled
301     int dynrange;               ///< embedded dynamic range flag
302     int timestamp;              ///< embedded time stamp flag
303     int aux_data;               ///< auxiliary data flag
304     int hdcd;                   ///< source material is mastered in HDCD
305     int ext_descr;              ///< extension audio descriptor flag
306     int ext_coding;             ///< extended coding flag
307     int aspf;                   ///< audio sync word insertion flag
308     int lfe;                    ///< low frequency effects flag
309     int predictor_history;      ///< predictor history flag
310     int header_crc;             ///< header crc check bytes
311     int multirate_inter;        ///< multirate interpolator switch
312     int version;                ///< encoder software revision
313     int copy_history;           ///< copy history
314     int source_pcm_res;         ///< source pcm resolution
315     int front_sum;              ///< front sum/difference flag
316     int surround_sum;           ///< surround sum/difference flag
317     int dialog_norm;            ///< dialog normalisation parameter
318
319     /* Primary audio coding header */
320     int subframes;              ///< number of subframes
321     int total_channels;         ///< number of channels including extensions
322     int prim_channels;          ///< number of primary audio channels
323     int subband_activity[DCA_PRIM_CHANNELS_MAX];    ///< subband activity count
324     int vq_start_subband[DCA_PRIM_CHANNELS_MAX];    ///< high frequency vq start subband
325     int joint_intensity[DCA_PRIM_CHANNELS_MAX];     ///< joint intensity coding index
326     int transient_huffman[DCA_PRIM_CHANNELS_MAX];   ///< transient mode code book
327     int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
328     int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX];    ///< bit allocation quantizer select
329     int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
330     float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];   ///< scale factor adjustment
331
332     /* Primary audio coding side information */
333     int subsubframes[DCA_SUBFRAMES_MAX];                         ///< number of subsubframes
334     int partial_samples[DCA_SUBFRAMES_MAX];                      ///< partial subsubframe samples count
335     int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< prediction mode (ADPCM used or not)
336     int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];      ///< prediction VQ coefs
337     int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];           ///< bit allocation index
338     int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< transition mode (transients)
339     int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];    ///< scale factors (2 if transient)
340     int joint_huff[DCA_PRIM_CHANNELS_MAX];                       ///< joint subband scale factors codebook
341     int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
342     int downmix_coef[DCA_PRIM_CHANNELS_MAX][2];                  ///< stereo downmix coefficients
343     int dynrange_coef;                                           ///< dynamic range coefficient
344
345     int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];       ///< VQ encoded high frequency subbands
346
347     float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)];      ///< Low frequency effect data
348     int lfe_scale_factor;
349
350     /* Subband samples history (for ADPCM) */
351     DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
352     DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
353     DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
354     int hist_index[DCA_PRIM_CHANNELS_MAX];
355     DECLARE_ALIGNED(32, float, raXin)[32];
356
357     int output;                 ///< type of output
358     float scale_bias;           ///< output scale
359
360     DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
361     DECLARE_ALIGNED(32, float, samples)[(DCA_PRIM_CHANNELS_MAX + 1) * 256];
362     const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
363
364     uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
365     int dca_buffer_size;        ///< how much data is in the dca_buffer
366
367     const int8_t *channel_order_tab;  ///< channel reordering table, lfe and non lfe
368     GetBitContext gb;
369     /* Current position in DCA frame */
370     int current_subframe;
371     int current_subsubframe;
372
373     int core_ext_mask;          ///< present extensions in the core substream
374
375     /* XCh extension information */
376     int xch_present;            ///< XCh extension present and valid
377     int xch_base_channel;       ///< index of first (only) channel containing XCH data
378
379     /* ExSS header parser */
380     int static_fields;          ///< static fields present
381     int mix_metadata;           ///< mixing metadata present
382     int num_mix_configs;        ///< number of mix out configurations
383     int mix_config_num_ch[4];   ///< number of channels in each mix out configuration
384
385     int profile;
386
387     int debug_flag;             ///< used for suppressing repeated error messages output
388     AVFloatDSPContext fdsp;
389     FFTContext imdct;
390     SynthFilterContext synth;
391     DCADSPContext dcadsp;
392     FmtConvertContext fmt_conv;
393 } DCAContext;
394
395 static const uint16_t dca_vlc_offs[] = {
396         0,   512,   640,   768,  1282,  1794,  2436,  3080,  3770,  4454,  5364,
397      5372,  5380,  5388,  5392,  5396,  5412,  5420,  5428,  5460,  5492,  5508,
398      5572,  5604,  5668,  5796,  5860,  5892,  6412,  6668,  6796,  7308,  7564,
399      7820,  8076,  8620,  9132,  9388,  9910, 10166, 10680, 11196, 11726, 12240,
400     12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
401     18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
402 };
403
404 static av_cold void dca_init_vlcs(void)
405 {
406     static int vlcs_initialized = 0;
407     int i, j, c = 14;
408     static VLC_TYPE dca_table[23622][2];
409
410     if (vlcs_initialized)
411         return;
412
413     dca_bitalloc_index.offset = 1;
414     dca_bitalloc_index.wrap = 2;
415     for (i = 0; i < 5; i++) {
416         dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
417         dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
418         init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
419                  bitalloc_12_bits[i], 1, 1,
420                  bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
421     }
422     dca_scalefactor.offset = -64;
423     dca_scalefactor.wrap = 2;
424     for (i = 0; i < 5; i++) {
425         dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
426         dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
427         init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
428                  scales_bits[i], 1, 1,
429                  scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
430     }
431     dca_tmode.offset = 0;
432     dca_tmode.wrap = 1;
433     for (i = 0; i < 4; i++) {
434         dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
435         dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
436         init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
437                  tmode_bits[i], 1, 1,
438                  tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
439     }
440
441     for (i = 0; i < 10; i++)
442         for (j = 0; j < 7; j++) {
443             if (!bitalloc_codes[i][j])
444                 break;
445             dca_smpl_bitalloc[i + 1].offset                 = bitalloc_offsets[i];
446             dca_smpl_bitalloc[i + 1].wrap                   = 1 + (j > 4);
447             dca_smpl_bitalloc[i + 1].vlc[j].table           = &dca_table[dca_vlc_offs[c]];
448             dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
449
450             init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
451                      bitalloc_sizes[i],
452                      bitalloc_bits[i][j], 1, 1,
453                      bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
454             c++;
455         }
456     vlcs_initialized = 1;
457 }
458
459 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
460 {
461     while (len--)
462         *dst++ = get_bits(gb, bits);
463 }
464
465 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
466 {
467     int i, j;
468     static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
469     static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
470     static const int thr[11]    = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
471
472     s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
473     s->prim_channels  = s->total_channels;
474
475     if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
476         s->prim_channels = DCA_PRIM_CHANNELS_MAX;
477
478
479     for (i = base_channel; i < s->prim_channels; i++) {
480         s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
481         if (s->subband_activity[i] > DCA_SUBBANDS)
482             s->subband_activity[i] = DCA_SUBBANDS;
483     }
484     for (i = base_channel; i < s->prim_channels; i++) {
485         s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
486         if (s->vq_start_subband[i] > DCA_SUBBANDS)
487             s->vq_start_subband[i] = DCA_SUBBANDS;
488     }
489     get_array(&s->gb, s->joint_intensity + base_channel,     s->prim_channels - base_channel, 3);
490     get_array(&s->gb, s->transient_huffman + base_channel,   s->prim_channels - base_channel, 2);
491     get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
492     get_array(&s->gb, s->bitalloc_huffman + base_channel,    s->prim_channels - base_channel, 3);
493
494     /* Get codebooks quantization indexes */
495     if (!base_channel)
496         memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
497     for (j = 1; j < 11; j++)
498         for (i = base_channel; i < s->prim_channels; i++)
499             s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
500
501     /* Get scale factor adjustment */
502     for (j = 0; j < 11; j++)
503         for (i = base_channel; i < s->prim_channels; i++)
504             s->scalefactor_adj[i][j] = 1;
505
506     for (j = 1; j < 11; j++)
507         for (i = base_channel; i < s->prim_channels; i++)
508             if (s->quant_index_huffman[i][j] < thr[j])
509                 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
510
511     if (s->crc_present) {
512         /* Audio header CRC check */
513         get_bits(&s->gb, 16);
514     }
515
516     s->current_subframe    = 0;
517     s->current_subsubframe = 0;
518
519 #ifdef TRACE
520     av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
521     av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
522     for (i = base_channel; i < s->prim_channels; i++) {
523         av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
524                s->subband_activity[i]);
525         av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
526                s->vq_start_subband[i]);
527         av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
528                s->joint_intensity[i]);
529         av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
530                s->transient_huffman[i]);
531         av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
532                s->scalefactor_huffman[i]);
533         av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
534                s->bitalloc_huffman[i]);
535         av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
536         for (j = 0; j < 11; j++)
537             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
538         av_log(s->avctx, AV_LOG_DEBUG, "\n");
539         av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
540         for (j = 0; j < 11; j++)
541             av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
542         av_log(s->avctx, AV_LOG_DEBUG, "\n");
543     }
544 #endif
545
546     return 0;
547 }
548
549 static int dca_parse_frame_header(DCAContext *s)
550 {
551     init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
552
553     /* Sync code */
554     skip_bits_long(&s->gb, 32);
555
556     /* Frame header */
557     s->frame_type        = get_bits(&s->gb, 1);
558     s->samples_deficit   = get_bits(&s->gb, 5) + 1;
559     s->crc_present       = get_bits(&s->gb, 1);
560     s->sample_blocks     = get_bits(&s->gb, 7) + 1;
561     s->frame_size        = get_bits(&s->gb, 14) + 1;
562     if (s->frame_size < 95)
563         return AVERROR_INVALIDDATA;
564     s->amode             = get_bits(&s->gb, 6);
565     s->sample_rate       = dca_sample_rates[get_bits(&s->gb, 4)];
566     if (!s->sample_rate)
567         return AVERROR_INVALIDDATA;
568     s->bit_rate_index    = get_bits(&s->gb, 5);
569     s->bit_rate          = dca_bit_rates[s->bit_rate_index];
570     if (!s->bit_rate)
571         return AVERROR_INVALIDDATA;
572
573     s->downmix           = get_bits(&s->gb, 1);
574     s->dynrange          = get_bits(&s->gb, 1);
575     s->timestamp         = get_bits(&s->gb, 1);
576     s->aux_data          = get_bits(&s->gb, 1);
577     s->hdcd              = get_bits(&s->gb, 1);
578     s->ext_descr         = get_bits(&s->gb, 3);
579     s->ext_coding        = get_bits(&s->gb, 1);
580     s->aspf              = get_bits(&s->gb, 1);
581     s->lfe               = get_bits(&s->gb, 2);
582     s->predictor_history = get_bits(&s->gb, 1);
583
584     /* TODO: check CRC */
585     if (s->crc_present)
586         s->header_crc    = get_bits(&s->gb, 16);
587
588     s->multirate_inter   = get_bits(&s->gb, 1);
589     s->version           = get_bits(&s->gb, 4);
590     s->copy_history      = get_bits(&s->gb, 2);
591     s->source_pcm_res    = get_bits(&s->gb, 3);
592     s->front_sum         = get_bits(&s->gb, 1);
593     s->surround_sum      = get_bits(&s->gb, 1);
594     s->dialog_norm       = get_bits(&s->gb, 4);
595
596     /* FIXME: channels mixing levels */
597     s->output = s->amode;
598     if (s->lfe)
599         s->output |= DCA_LFE;
600
601 #ifdef TRACE
602     av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
603     av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
604     av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
605     av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
606            s->sample_blocks, s->sample_blocks * 32);
607     av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
608     av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
609            s->amode, dca_channels[s->amode]);
610     av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
611            s->sample_rate);
612     av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
613            s->bit_rate);
614     av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
615     av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
616     av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
617     av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
618     av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
619     av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
620     av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
621     av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
622     av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
623     av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
624            s->predictor_history);
625     av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
626     av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
627            s->multirate_inter);
628     av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
629     av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
630     av_log(s->avctx, AV_LOG_DEBUG,
631            "source pcm resolution: %i (%i bits/sample)\n",
632            s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
633     av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
634     av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
635     av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
636     av_log(s->avctx, AV_LOG_DEBUG, "\n");
637 #endif
638
639     /* Primary audio coding header */
640     s->subframes         = get_bits(&s->gb, 4) + 1;
641
642     return dca_parse_audio_coding_header(s, 0);
643 }
644
645
646 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
647 {
648     if (level < 5) {
649         /* huffman encoded */
650         value += get_bitalloc(gb, &dca_scalefactor, level);
651         value = av_clip(value, 0, (1 << log2range) - 1);
652     } else if (level < 8) {
653         if (level + 1 > log2range) {
654             skip_bits(gb, level + 1 - log2range);
655             value = get_bits(gb, log2range);
656         } else {
657             value = get_bits(gb, level + 1);
658         }
659     }
660     return value;
661 }
662
663 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
664 {
665     /* Primary audio coding side information */
666     int j, k;
667
668     if (get_bits_left(&s->gb) < 0)
669         return AVERROR_INVALIDDATA;
670
671     if (!base_channel) {
672         s->subsubframes[s->current_subframe]    = get_bits(&s->gb, 2) + 1;
673         s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
674     }
675
676     for (j = base_channel; j < s->prim_channels; j++) {
677         for (k = 0; k < s->subband_activity[j]; k++)
678             s->prediction_mode[j][k] = get_bits(&s->gb, 1);
679     }
680
681     /* Get prediction codebook */
682     for (j = base_channel; j < s->prim_channels; j++) {
683         for (k = 0; k < s->subband_activity[j]; k++) {
684             if (s->prediction_mode[j][k] > 0) {
685                 /* (Prediction coefficient VQ address) */
686                 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
687             }
688         }
689     }
690
691     /* Bit allocation index */
692     for (j = base_channel; j < s->prim_channels; j++) {
693         for (k = 0; k < s->vq_start_subband[j]; k++) {
694             if (s->bitalloc_huffman[j] == 6)
695                 s->bitalloc[j][k] = get_bits(&s->gb, 5);
696             else if (s->bitalloc_huffman[j] == 5)
697                 s->bitalloc[j][k] = get_bits(&s->gb, 4);
698             else if (s->bitalloc_huffman[j] == 7) {
699                 av_log(s->avctx, AV_LOG_ERROR,
700                        "Invalid bit allocation index\n");
701                 return AVERROR_INVALIDDATA;
702             } else {
703                 s->bitalloc[j][k] =
704                     get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
705             }
706
707             if (s->bitalloc[j][k] > 26) {
708                 // av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index [%i][%i] too big (%i)\n",
709                 //        j, k, s->bitalloc[j][k]);
710                 return AVERROR_INVALIDDATA;
711             }
712         }
713     }
714
715     /* Transition mode */
716     for (j = base_channel; j < s->prim_channels; j++) {
717         for (k = 0; k < s->subband_activity[j]; k++) {
718             s->transition_mode[j][k] = 0;
719             if (s->subsubframes[s->current_subframe] > 1 &&
720                 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
721                 s->transition_mode[j][k] =
722                     get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
723             }
724         }
725     }
726
727     if (get_bits_left(&s->gb) < 0)
728         return AVERROR_INVALIDDATA;
729
730     for (j = base_channel; j < s->prim_channels; j++) {
731         const uint32_t *scale_table;
732         int scale_sum, log_size;
733
734         memset(s->scale_factor[j], 0,
735                s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
736
737         if (s->scalefactor_huffman[j] == 6) {
738             scale_table = scale_factor_quant7;
739             log_size = 7;
740         } else {
741             scale_table = scale_factor_quant6;
742             log_size = 6;
743         }
744
745         /* When huffman coded, only the difference is encoded */
746         scale_sum = 0;
747
748         for (k = 0; k < s->subband_activity[j]; k++) {
749             if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
750                 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
751                 s->scale_factor[j][k][0] = scale_table[scale_sum];
752             }
753
754             if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
755                 /* Get second scale factor */
756                 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
757                 s->scale_factor[j][k][1] = scale_table[scale_sum];
758             }
759         }
760     }
761
762     /* Joint subband scale factor codebook select */
763     for (j = base_channel; j < s->prim_channels; j++) {
764         /* Transmitted only if joint subband coding enabled */
765         if (s->joint_intensity[j] > 0)
766             s->joint_huff[j] = get_bits(&s->gb, 3);
767     }
768
769     if (get_bits_left(&s->gb) < 0)
770         return AVERROR_INVALIDDATA;
771
772     /* Scale factors for joint subband coding */
773     for (j = base_channel; j < s->prim_channels; j++) {
774         int source_channel;
775
776         /* Transmitted only if joint subband coding enabled */
777         if (s->joint_intensity[j] > 0) {
778             int scale = 0;
779             source_channel = s->joint_intensity[j] - 1;
780
781             /* When huffman coded, only the difference is encoded
782              * (is this valid as well for joint scales ???) */
783
784             for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
785                 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
786                 s->joint_scale_factor[j][k] = scale;    /*joint_scale_table[scale]; */
787             }
788
789             if (!(s->debug_flag & 0x02)) {
790                 av_log(s->avctx, AV_LOG_DEBUG,
791                        "Joint stereo coding not supported\n");
792                 s->debug_flag |= 0x02;
793             }
794         }
795     }
796
797     /* Stereo downmix coefficients */
798     if (!base_channel && s->prim_channels > 2) {
799         if (s->downmix) {
800             for (j = base_channel; j < s->prim_channels; j++) {
801                 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
802                 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
803             }
804         } else {
805             int am = s->amode & DCA_CHANNEL_MASK;
806             if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
807                 av_log(s->avctx, AV_LOG_ERROR,
808                        "Invalid channel mode %d\n", am);
809                 return AVERROR_INVALIDDATA;
810             }
811             for (j = base_channel; j < FFMIN(s->prim_channels, FF_ARRAY_ELEMS(dca_default_coeffs[am])); j++) {
812                 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
813                 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
814             }
815         }
816     }
817
818     /* Dynamic range coefficient */
819     if (!base_channel && s->dynrange)
820         s->dynrange_coef = get_bits(&s->gb, 8);
821
822     /* Side information CRC check word */
823     if (s->crc_present) {
824         get_bits(&s->gb, 16);
825     }
826
827     /*
828      * Primary audio data arrays
829      */
830
831     /* VQ encoded high frequency subbands */
832     for (j = base_channel; j < s->prim_channels; j++)
833         for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
834             /* 1 vector -> 32 samples */
835             s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
836
837     /* Low frequency effect data */
838     if (!base_channel && s->lfe) {
839         int quant7;
840         /* LFE samples */
841         int lfe_samples = 2 * s->lfe * (4 + block_index);
842         int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
843         float lfe_scale;
844
845         for (j = lfe_samples; j < lfe_end_sample; j++) {
846             /* Signed 8 bits int */
847             s->lfe_data[j] = get_sbits(&s->gb, 8);
848         }
849
850         /* Scale factor index */
851         quant7 = get_bits(&s->gb, 8);
852         if (quant7 > 127) {
853             av_log_ask_for_sample(s->avctx, "LFEScaleIndex larger than 127\n");
854             return AVERROR_INVALIDDATA;
855         }
856         s->lfe_scale_factor = scale_factor_quant7[quant7];
857
858         /* Quantization step size * scale factor */
859         lfe_scale = 0.035 * s->lfe_scale_factor;
860
861         for (j = lfe_samples; j < lfe_end_sample; j++)
862             s->lfe_data[j] *= lfe_scale;
863     }
864
865 #ifdef TRACE
866     av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
867            s->subsubframes[s->current_subframe]);
868     av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
869            s->partial_samples[s->current_subframe]);
870
871     for (j = base_channel; j < s->prim_channels; j++) {
872         av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
873         for (k = 0; k < s->subband_activity[j]; k++)
874             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
875         av_log(s->avctx, AV_LOG_DEBUG, "\n");
876     }
877     for (j = base_channel; j < s->prim_channels; j++) {
878         for (k = 0; k < s->subband_activity[j]; k++)
879             av_log(s->avctx, AV_LOG_DEBUG,
880                    "prediction coefs: %f, %f, %f, %f\n",
881                    (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
882                    (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
883                    (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
884                    (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
885     }
886     for (j = base_channel; j < s->prim_channels; j++) {
887         av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
888         for (k = 0; k < s->vq_start_subband[j]; k++)
889             av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
890         av_log(s->avctx, AV_LOG_DEBUG, "\n");
891     }
892     for (j = base_channel; j < s->prim_channels; j++) {
893         av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
894         for (k = 0; k < s->subband_activity[j]; k++)
895             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
896         av_log(s->avctx, AV_LOG_DEBUG, "\n");
897     }
898     for (j = base_channel; j < s->prim_channels; j++) {
899         av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
900         for (k = 0; k < s->subband_activity[j]; k++) {
901             if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
902                 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
903             if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
904                 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
905         }
906         av_log(s->avctx, AV_LOG_DEBUG, "\n");
907     }
908     for (j = base_channel; j < s->prim_channels; j++) {
909         if (s->joint_intensity[j] > 0) {
910             int source_channel = s->joint_intensity[j] - 1;
911             av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
912             for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
913                 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
914             av_log(s->avctx, AV_LOG_DEBUG, "\n");
915         }
916     }
917     if (!base_channel && s->prim_channels > 2 && s->downmix) {
918         av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
919         for (j = 0; j < s->prim_channels; j++) {
920             av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
921                    dca_downmix_coeffs[s->downmix_coef[j][0]]);
922             av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
923                    dca_downmix_coeffs[s->downmix_coef[j][1]]);
924         }
925         av_log(s->avctx, AV_LOG_DEBUG, "\n");
926     }
927     for (j = base_channel; j < s->prim_channels; j++)
928         for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
929             av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
930     if (!base_channel && s->lfe) {
931         int lfe_samples = 2 * s->lfe * (4 + block_index);
932         int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
933
934         av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
935         for (j = lfe_samples; j < lfe_end_sample; j++)
936             av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
937         av_log(s->avctx, AV_LOG_DEBUG, "\n");
938     }
939 #endif
940
941     return 0;
942 }
943
944 static void qmf_32_subbands(DCAContext *s, int chans,
945                             float samples_in[32][8], float *samples_out,
946                             float scale)
947 {
948     const float *prCoeff;
949     int i;
950
951     int sb_act = s->subband_activity[chans];
952     int subindex;
953
954     scale *= sqrt(1 / 8.0);
955
956     /* Select filter */
957     if (!s->multirate_inter)    /* Non-perfect reconstruction */
958         prCoeff = fir_32bands_nonperfect;
959     else                        /* Perfect reconstruction */
960         prCoeff = fir_32bands_perfect;
961
962     for (i = sb_act; i < 32; i++)
963         s->raXin[i] = 0.0;
964
965     /* Reconstructed channel sample index */
966     for (subindex = 0; subindex < 8; subindex++) {
967         /* Load in one sample from each subband and clear inactive subbands */
968         for (i = 0; i < sb_act; i++) {
969             unsigned sign = (i - 1) & 2;
970             uint32_t v    = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
971             AV_WN32A(&s->raXin[i], v);
972         }
973
974         s->synth.synth_filter_float(&s->imdct,
975                                     s->subband_fir_hist[chans],
976                                     &s->hist_index[chans],
977                                     s->subband_fir_noidea[chans], prCoeff,
978                                     samples_out, s->raXin, scale);
979         samples_out += 32;
980     }
981 }
982
983 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
984                                   int num_deci_sample, float *samples_in,
985                                   float *samples_out, float scale)
986 {
987     /* samples_in: An array holding decimated samples.
988      *   Samples in current subframe starts from samples_in[0],
989      *   while samples_in[-1], samples_in[-2], ..., stores samples
990      *   from last subframe as history.
991      *
992      * samples_out: An array holding interpolated samples
993      */
994
995     int decifactor;
996     const float *prCoeff;
997     int deciindex;
998
999     /* Select decimation filter */
1000     if (decimation_select == 1) {
1001         decifactor = 64;
1002         prCoeff = lfe_fir_128;
1003     } else {
1004         decifactor = 32;
1005         prCoeff = lfe_fir_64;
1006     }
1007     /* Interpolation */
1008     for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1009         s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale);
1010         samples_in++;
1011         samples_out += 2 * decifactor;
1012     }
1013 }
1014
1015 /* downmixing routines */
1016 #define MIX_REAR1(samples, si1, rs, coef)           \
1017     samples[i]     += samples[si1] * coef[rs][0];   \
1018     samples[i+256] += samples[si1] * coef[rs][1];
1019
1020 #define MIX_REAR2(samples, si1, si2, rs, coef)                                     \
1021     samples[i]     += samples[si1] * coef[rs][0] + samples[si2] * coef[rs + 1][0]; \
1022     samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs + 1][1];
1023
1024 #define MIX_FRONT3(samples, coef)                                      \
1025     t = samples[i + c];                                                \
1026     u = samples[i + l];                                                \
1027     v = samples[i + r];                                                \
1028     samples[i]     = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1029     samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1030
1031 #define DOWNMIX_TO_STEREO(op1, op2)             \
1032     for (i = 0; i < 256; i++) {                 \
1033         op1                                     \
1034         op2                                     \
1035     }
1036
1037 static void dca_downmix(float *samples, int srcfmt,
1038                         int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
1039                         const int8_t *channel_mapping)
1040 {
1041     int c, l, r, sl, sr, s;
1042     int i;
1043     float t, u, v;
1044     float coef[DCA_PRIM_CHANNELS_MAX][2];
1045
1046     for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) {
1047         coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
1048         coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
1049     }
1050
1051     switch (srcfmt) {
1052     case DCA_MONO:
1053     case DCA_CHANNEL:
1054     case DCA_STEREO_TOTAL:
1055     case DCA_STEREO_SUMDIFF:
1056     case DCA_4F2R:
1057         av_log(NULL, 0, "Not implemented!\n");
1058         break;
1059     case DCA_STEREO:
1060         break;
1061     case DCA_3F:
1062         c = channel_mapping[0] * 256;
1063         l = channel_mapping[1] * 256;
1064         r = channel_mapping[2] * 256;
1065         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1066         break;
1067     case DCA_2F1R:
1068         s = channel_mapping[2] * 256;
1069         DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef), );
1070         break;
1071     case DCA_3F1R:
1072         c = channel_mapping[0] * 256;
1073         l = channel_mapping[1] * 256;
1074         r = channel_mapping[2] * 256;
1075         s = channel_mapping[3] * 256;
1076         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1077                           MIX_REAR1(samples, i + s, 3, coef));
1078         break;
1079     case DCA_2F2R:
1080         sl = channel_mapping[2] * 256;
1081         sr = channel_mapping[3] * 256;
1082         DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef), );
1083         break;
1084     case DCA_3F2R:
1085         c  = channel_mapping[0] * 256;
1086         l  = channel_mapping[1] * 256;
1087         r  = channel_mapping[2] * 256;
1088         sl = channel_mapping[3] * 256;
1089         sr = channel_mapping[4] * 256;
1090         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1091                           MIX_REAR2(samples, i + sl, i + sr, 3, coef));
1092         break;
1093     }
1094 }
1095
1096
1097 #ifndef decode_blockcodes
1098 /* Very compact version of the block code decoder that does not use table
1099  * look-up but is slightly slower */
1100 static int decode_blockcode(int code, int levels, int *values)
1101 {
1102     int i;
1103     int offset = (levels - 1) >> 1;
1104
1105     for (i = 0; i < 4; i++) {
1106         int div = FASTDIV(code, levels);
1107         values[i] = code - offset - div * levels;
1108         code = div;
1109     }
1110
1111     return code;
1112 }
1113
1114 static int decode_blockcodes(int code1, int code2, int levels, int *values)
1115 {
1116     return decode_blockcode(code1, levels, values) |
1117            decode_blockcode(code2, levels, values + 4);
1118 }
1119 #endif
1120
1121 static const uint8_t abits_sizes[7]  = { 7, 10, 12, 13, 15, 17, 19 };
1122 static const uint8_t abits_levels[7] = { 3,  5,  7,  9, 13, 17, 25 };
1123
1124 #ifndef int8x8_fmul_int32
1125 static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
1126 {
1127     float fscale = scale / 16.0;
1128     int i;
1129     for (i = 0; i < 8; i++)
1130         dst[i] = src[i] * fscale;
1131 }
1132 #endif
1133
1134 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1135 {
1136     int k, l;
1137     int subsubframe = s->current_subsubframe;
1138
1139     const float *quant_step_table;
1140
1141     /* FIXME */
1142     float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1143     LOCAL_ALIGNED_16(int, block, [8]);
1144
1145     /*
1146      * Audio data
1147      */
1148
1149     /* Select quantization step size table */
1150     if (s->bit_rate_index == 0x1f)
1151         quant_step_table = lossless_quant_d;
1152     else
1153         quant_step_table = lossy_quant_d;
1154
1155     for (k = base_channel; k < s->prim_channels; k++) {
1156         if (get_bits_left(&s->gb) < 0)
1157             return AVERROR_INVALIDDATA;
1158
1159         for (l = 0; l < s->vq_start_subband[k]; l++) {
1160             int m;
1161
1162             /* Select the mid-tread linear quantizer */
1163             int abits = s->bitalloc[k][l];
1164
1165             float quant_step_size = quant_step_table[abits];
1166
1167             /*
1168              * Determine quantization index code book and its type
1169              */
1170
1171             /* Select quantization index code book */
1172             int sel = s->quant_index_huffman[k][abits];
1173
1174             /*
1175              * Extract bits from the bit stream
1176              */
1177             if (!abits) {
1178                 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
1179             } else {
1180                 /* Deal with transients */
1181                 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1182                 float rscale = quant_step_size * s->scale_factor[k][l][sfi] *
1183                                s->scalefactor_adj[k][sel];
1184
1185                 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1186                     if (abits <= 7) {
1187                         /* Block code */
1188                         int block_code1, block_code2, size, levels, err;
1189
1190                         size   = abits_sizes[abits - 1];
1191                         levels = abits_levels[abits - 1];
1192
1193                         block_code1 = get_bits(&s->gb, size);
1194                         block_code2 = get_bits(&s->gb, size);
1195                         err = decode_blockcodes(block_code1, block_code2,
1196                                                 levels, block);
1197                         if (err) {
1198                             av_log(s->avctx, AV_LOG_ERROR,
1199                                    "ERROR: block code look-up failed\n");
1200                             return AVERROR_INVALIDDATA;
1201                         }
1202                     } else {
1203                         /* no coding */
1204                         for (m = 0; m < 8; m++)
1205                             block[m] = get_sbits(&s->gb, abits - 3);
1206                     }
1207                 } else {
1208                     /* Huffman coded */
1209                     for (m = 0; m < 8; m++)
1210                         block[m] = get_bitalloc(&s->gb,
1211                                                 &dca_smpl_bitalloc[abits], sel);
1212                 }
1213
1214                 s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
1215                                                        block, rscale, 8);
1216             }
1217
1218             /*
1219              * Inverse ADPCM if in prediction mode
1220              */
1221             if (s->prediction_mode[k][l]) {
1222                 int n;
1223                 for (m = 0; m < 8; m++) {
1224                     for (n = 1; n <= 4; n++)
1225                         if (m >= n)
1226                             subband_samples[k][l][m] +=
1227                                 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1228                                  subband_samples[k][l][m - n] / 8192);
1229                         else if (s->predictor_history)
1230                             subband_samples[k][l][m] +=
1231                                 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1232                                  s->subband_samples_hist[k][l][m - n + 4] / 8192);
1233                 }
1234             }
1235         }
1236
1237         /*
1238          * Decode VQ encoded high frequencies
1239          */
1240         for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1241             /* 1 vector -> 32 samples but we only need the 8 samples
1242              * for this subsubframe. */
1243             int hfvq = s->high_freq_vq[k][l];
1244
1245             if (!s->debug_flag & 0x01) {
1246                 av_log(s->avctx, AV_LOG_DEBUG,
1247                        "Stream with high frequencies VQ coding\n");
1248                 s->debug_flag |= 0x01;
1249             }
1250
1251             int8x8_fmul_int32(subband_samples[k][l],
1252                               &high_freq_vq[hfvq][subsubframe * 8],
1253                               s->scale_factor[k][l][0]);
1254         }
1255     }
1256
1257     /* Check for DSYNC after subsubframe */
1258     if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1259         if (0xFFFF == get_bits(&s->gb, 16)) {   /* 0xFFFF */
1260 #ifdef TRACE
1261             av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1262 #endif
1263         } else {
1264             av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1265         }
1266     }
1267
1268     /* Backup predictor history for adpcm */
1269     for (k = base_channel; k < s->prim_channels; k++)
1270         for (l = 0; l < s->vq_start_subband[k]; l++)
1271             memcpy(s->subband_samples_hist[k][l],
1272                    &subband_samples[k][l][4],
1273                    4 * sizeof(subband_samples[0][0][0]));
1274
1275     return 0;
1276 }
1277
1278 static int dca_filter_channels(DCAContext *s, int block_index)
1279 {
1280     float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1281     int k;
1282
1283     /* 32 subbands QMF */
1284     for (k = 0; k < s->prim_channels; k++) {
1285 /*        static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1286                                             0, 8388608.0, 8388608.0 };*/
1287         qmf_32_subbands(s, k, subband_samples[k],
1288                         &s->samples[256 * s->channel_order_tab[k]],
1289                         M_SQRT1_2 * s->scale_bias /* pcm_to_double[s->source_pcm_res] */);
1290     }
1291
1292     /* Down mixing */
1293     if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
1294         dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab);
1295     }
1296
1297     /* Generate LFE samples for this subsubframe FIXME!!! */
1298     if (s->output & DCA_LFE) {
1299         lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1300                               s->lfe_data + 2 * s->lfe * (block_index + 4),
1301                               &s->samples[256 * dca_lfe_index[s->amode]],
1302                               (1.0 / 256.0) * s->scale_bias);
1303         /* Outputs 20bits pcm samples */
1304     }
1305
1306     return 0;
1307 }
1308
1309
1310 static int dca_subframe_footer(DCAContext *s, int base_channel)
1311 {
1312     int aux_data_count = 0, i;
1313
1314     /*
1315      * Unpack optional information
1316      */
1317
1318     /* presumably optional information only appears in the core? */
1319     if (!base_channel) {
1320         if (s->timestamp)
1321             skip_bits_long(&s->gb, 32);
1322
1323         if (s->aux_data)
1324             aux_data_count = get_bits(&s->gb, 6);
1325
1326         for (i = 0; i < aux_data_count; i++)
1327             get_bits(&s->gb, 8);
1328
1329         if (s->crc_present && (s->downmix || s->dynrange))
1330             get_bits(&s->gb, 16);
1331     }
1332
1333     return 0;
1334 }
1335
1336 /**
1337  * Decode a dca frame block
1338  *
1339  * @param s     pointer to the DCAContext
1340  */
1341
1342 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1343 {
1344     int ret;
1345
1346     /* Sanity check */
1347     if (s->current_subframe >= s->subframes) {
1348         av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1349                s->current_subframe, s->subframes);
1350         return AVERROR_INVALIDDATA;
1351     }
1352
1353     if (!s->current_subsubframe) {
1354 #ifdef TRACE
1355         av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1356 #endif
1357         /* Read subframe header */
1358         if ((ret = dca_subframe_header(s, base_channel, block_index)))
1359             return ret;
1360     }
1361
1362     /* Read subsubframe */
1363 #ifdef TRACE
1364     av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1365 #endif
1366     if ((ret = dca_subsubframe(s, base_channel, block_index)))
1367         return ret;
1368
1369     /* Update state */
1370     s->current_subsubframe++;
1371     if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1372         s->current_subsubframe = 0;
1373         s->current_subframe++;
1374     }
1375     if (s->current_subframe >= s->subframes) {
1376 #ifdef TRACE
1377         av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1378 #endif
1379         /* Read subframe footer */
1380         if ((ret = dca_subframe_footer(s, base_channel)))
1381             return ret;
1382     }
1383
1384     return 0;
1385 }
1386
1387 /**
1388  * Return the number of channels in an ExSS speaker mask (HD)
1389  */
1390 static int dca_exss_mask2count(int mask)
1391 {
1392     /* count bits that mean speaker pairs twice */
1393     return av_popcount(mask) +
1394            av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT      |
1395                                DCA_EXSS_FRONT_LEFT_RIGHT       |
1396                                DCA_EXSS_FRONT_HIGH_LEFT_RIGHT  |
1397                                DCA_EXSS_WIDE_LEFT_RIGHT        |
1398                                DCA_EXSS_SIDE_LEFT_RIGHT        |
1399                                DCA_EXSS_SIDE_HIGH_LEFT_RIGHT   |
1400                                DCA_EXSS_SIDE_REAR_LEFT_RIGHT   |
1401                                DCA_EXSS_REAR_LEFT_RIGHT        |
1402                                DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1403 }
1404
1405 /**
1406  * Skip mixing coefficients of a single mix out configuration (HD)
1407  */
1408 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1409 {
1410     int i;
1411
1412     for (i = 0; i < channels; i++) {
1413         int mix_map_mask = get_bits(gb, out_ch);
1414         int num_coeffs = av_popcount(mix_map_mask);
1415         skip_bits_long(gb, num_coeffs * 6);
1416     }
1417 }
1418
1419 /**
1420  * Parse extension substream asset header (HD)
1421  */
1422 static int dca_exss_parse_asset_header(DCAContext *s)
1423 {
1424     int header_pos = get_bits_count(&s->gb);
1425     int header_size;
1426     int channels = 0;
1427     int embedded_stereo = 0;
1428     int embedded_6ch    = 0;
1429     int drc_code_present;
1430     int av_uninit(extensions_mask);
1431     int i, j;
1432
1433     if (get_bits_left(&s->gb) < 16)
1434         return -1;
1435
1436     /* We will parse just enough to get to the extensions bitmask with which
1437      * we can set the profile value. */
1438
1439     header_size = get_bits(&s->gb, 9) + 1;
1440     skip_bits(&s->gb, 3); // asset index
1441
1442     if (s->static_fields) {
1443         if (get_bits1(&s->gb))
1444             skip_bits(&s->gb, 4); // asset type descriptor
1445         if (get_bits1(&s->gb))
1446             skip_bits_long(&s->gb, 24); // language descriptor
1447
1448         if (get_bits1(&s->gb)) {
1449             /* How can one fit 1024 bytes of text here if the maximum value
1450              * for the asset header size field above was 512 bytes? */
1451             int text_length = get_bits(&s->gb, 10) + 1;
1452             if (get_bits_left(&s->gb) < text_length * 8)
1453                 return -1;
1454             skip_bits_long(&s->gb, text_length * 8); // info text
1455         }
1456
1457         skip_bits(&s->gb, 5); // bit resolution - 1
1458         skip_bits(&s->gb, 4); // max sample rate code
1459         channels = get_bits(&s->gb, 8) + 1;
1460
1461         if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1462             int spkr_remap_sets;
1463             int spkr_mask_size = 16;
1464             int num_spkrs[7];
1465
1466             if (channels > 2)
1467                 embedded_stereo = get_bits1(&s->gb);
1468             if (channels > 6)
1469                 embedded_6ch = get_bits1(&s->gb);
1470
1471             if (get_bits1(&s->gb)) {
1472                 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1473                 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1474             }
1475
1476             spkr_remap_sets = get_bits(&s->gb, 3);
1477
1478             for (i = 0; i < spkr_remap_sets; i++) {
1479                 /* std layout mask for each remap set */
1480                 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1481             }
1482
1483             for (i = 0; i < spkr_remap_sets; i++) {
1484                 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1485                 if (get_bits_left(&s->gb) < 0)
1486                     return -1;
1487
1488                 for (j = 0; j < num_spkrs[i]; j++) {
1489                     int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1490                     int num_dec_ch = av_popcount(remap_dec_ch_mask);
1491                     skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1492                 }
1493             }
1494
1495         } else {
1496             skip_bits(&s->gb, 3); // representation type
1497         }
1498     }
1499
1500     drc_code_present = get_bits1(&s->gb);
1501     if (drc_code_present)
1502         get_bits(&s->gb, 8); // drc code
1503
1504     if (get_bits1(&s->gb))
1505         skip_bits(&s->gb, 5); // dialog normalization code
1506
1507     if (drc_code_present && embedded_stereo)
1508         get_bits(&s->gb, 8); // drc stereo code
1509
1510     if (s->mix_metadata && get_bits1(&s->gb)) {
1511         skip_bits(&s->gb, 1); // external mix
1512         skip_bits(&s->gb, 6); // post mix gain code
1513
1514         if (get_bits(&s->gb, 2) != 3) // mixer drc code
1515             skip_bits(&s->gb, 3); // drc limit
1516         else
1517             skip_bits(&s->gb, 8); // custom drc code
1518
1519         if (get_bits1(&s->gb)) // channel specific scaling
1520             for (i = 0; i < s->num_mix_configs; i++)
1521                 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1522         else
1523             skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1524
1525         for (i = 0; i < s->num_mix_configs; i++) {
1526             if (get_bits_left(&s->gb) < 0)
1527                 return -1;
1528             dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1529             if (embedded_6ch)
1530                 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1531             if (embedded_stereo)
1532                 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1533         }
1534     }
1535
1536     switch (get_bits(&s->gb, 2)) {
1537     case 0: extensions_mask = get_bits(&s->gb, 12); break;
1538     case 1: extensions_mask = DCA_EXT_EXSS_XLL;     break;
1539     case 2: extensions_mask = DCA_EXT_EXSS_LBR;     break;
1540     case 3: extensions_mask = 0; /* aux coding */   break;
1541     }
1542
1543     /* not parsed further, we were only interested in the extensions mask */
1544
1545     if (get_bits_left(&s->gb) < 0)
1546         return -1;
1547
1548     if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1549         av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1550         return -1;
1551     }
1552     skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1553
1554     if (extensions_mask & DCA_EXT_EXSS_XLL)
1555         s->profile = FF_PROFILE_DTS_HD_MA;
1556     else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1557                                 DCA_EXT_EXSS_XXCH))
1558         s->profile = FF_PROFILE_DTS_HD_HRA;
1559
1560     if (!(extensions_mask & DCA_EXT_CORE))
1561         av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1562     if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1563         av_log(s->avctx, AV_LOG_WARNING,
1564                "DTS extensions detection mismatch (%d, %d)\n",
1565                extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1566
1567     return 0;
1568 }
1569
1570 static int dca_xbr_parse_frame(DCAContext *s)
1571 {
1572     int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1573     int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1574     int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1575     int anctemp[DCA_CHSET_CHANS_MAX];
1576     int chset_fsize[DCA_CHSETS_MAX];
1577     int n_xbr_ch[DCA_CHSETS_MAX];
1578     int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1579     int i, j, k, l, chset, chan_base;
1580
1581     av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1582
1583     /* get bit position of sync header */
1584     hdr_pos = get_bits_count(&s->gb) - 32;
1585
1586     hdr_size = get_bits(&s->gb, 6) + 1;
1587     num_chsets = get_bits(&s->gb, 2) + 1;
1588
1589     for(i = 0; i < num_chsets; i++)
1590         chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1591
1592     xbr_tmode = get_bits1(&s->gb);
1593
1594     for(i = 0; i < num_chsets; i++) {
1595         n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1596         k = get_bits(&s->gb, 2) + 5;
1597         for(j = 0; j < n_xbr_ch[i]; j++)
1598             active_bands[i][j] = get_bits(&s->gb, k) + 1;
1599     }
1600
1601     /* skip to the end of the header */
1602     i = get_bits_count(&s->gb);
1603     if(hdr_pos + hdr_size * 8 > i)
1604         skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1605
1606     /* loop over the channel data sets */
1607     /* only decode as many channels as we've decoded base data for */
1608     for(chset = 0, chan_base = 0;
1609         chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1610         chan_base += n_xbr_ch[chset++]) {
1611         int start_posn = get_bits_count(&s->gb);
1612         int subsubframe = 0;
1613         int subframe = 0;
1614
1615         /* loop over subframes */
1616         for (k = 0; k < (s->sample_blocks / 8); k++) {
1617             /* parse header if we're on first subsubframe of a block */
1618             if(subsubframe == 0) {
1619                 /* Parse subframe header */
1620                 for(i = 0; i < n_xbr_ch[chset]; i++) {
1621                     anctemp[i] = get_bits(&s->gb, 2) + 2;
1622                 }
1623
1624                 for(i = 0; i < n_xbr_ch[chset]; i++) {
1625                     get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1626                 }
1627
1628                 for(i = 0; i < n_xbr_ch[chset]; i++) {
1629                     anctemp[i] = get_bits(&s->gb, 3);
1630                     if(anctemp[i] < 1) {
1631                         av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1632                         return AVERROR_INVALIDDATA;
1633                     }
1634                 }
1635
1636                 /* generate scale factors */
1637                 for(i = 0; i < n_xbr_ch[chset]; i++) {
1638                     const uint32_t *scale_table;
1639                     int nbits;
1640
1641                     if (s->scalefactor_huffman[chan_base+i] == 6) {
1642                         scale_table = scale_factor_quant7;
1643                     } else {
1644                         scale_table = scale_factor_quant6;
1645                     }
1646
1647                     nbits = anctemp[i];
1648
1649                     for(j = 0; j < active_bands[chset][i]; j++) {
1650                         if(abits_high[i][j] > 0) {
1651                             scale_table_high[i][j][0] =
1652                                 scale_table[get_bits(&s->gb, nbits)];
1653
1654                             if(xbr_tmode && s->transition_mode[i][j]) {
1655                                 scale_table_high[i][j][1] =
1656                                     scale_table[get_bits(&s->gb, nbits)];
1657                             }
1658                         }
1659                     }
1660                 }
1661             }
1662
1663             /* decode audio array for this block */
1664             for(i = 0; i < n_xbr_ch[chset]; i++) {
1665                 for(j = 0; j < active_bands[chset][i]; j++) {
1666                     const int xbr_abits = abits_high[i][j];
1667                     const float quant_step_size = lossless_quant_d[xbr_abits];
1668                     const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1669                     const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1670                     float *subband_samples = s->subband_samples[k][chan_base+i][j];
1671                     int block[8];
1672
1673                     if(xbr_abits <= 0)
1674                         continue;
1675
1676                     if(xbr_abits > 7) {
1677                         get_array(&s->gb, block, 8, xbr_abits - 3);
1678                     } else {
1679                         int block_code1, block_code2, size, levels, err;
1680
1681                         size   = abits_sizes[xbr_abits - 1];
1682                         levels = abits_levels[xbr_abits - 1];
1683
1684                         block_code1 = get_bits(&s->gb, size);
1685                         block_code2 = get_bits(&s->gb, size);
1686                         err = decode_blockcodes(block_code1, block_code2,
1687                                                 levels, block);
1688                         if (err) {
1689                             av_log(s->avctx, AV_LOG_ERROR,
1690                                    "ERROR: DTS-XBR: block code look-up failed\n");
1691                             return AVERROR_INVALIDDATA;
1692                         }
1693                     }
1694
1695                     /* scale & sum into subband */
1696                     for(l = 0; l < 8; l++)
1697                         subband_samples[l] += (float)block[l] * rscale;
1698                 }
1699             }
1700
1701             /* check DSYNC marker */
1702             if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1703                 if(get_bits(&s->gb, 16) != 0xffff) {
1704                     av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1705                     return AVERROR_INVALIDDATA;
1706                 }
1707             }
1708
1709             /* advance sub-sub-frame index */
1710             if(++subsubframe >= s->subsubframes[subframe]) {
1711                 subsubframe = 0;
1712                 subframe++;
1713             }
1714         }
1715
1716         /* skip to next channel set */
1717         i = get_bits_count(&s->gb);
1718         if(start_posn + chset_fsize[chset] * 8 != i) {
1719             j = start_posn + chset_fsize[chset] * 8 - i;
1720             if(j < 0 || j >= 8)
1721                 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1722                        " skipping further than expected (%d bits)\n", j);
1723             skip_bits_long(&s->gb, j);
1724         }
1725     }
1726
1727     return 0;
1728 }
1729
1730 /**
1731  * Parse extension substream header (HD)
1732  */
1733 static void dca_exss_parse_header(DCAContext *s)
1734 {
1735     int ss_index;
1736     int blownup;
1737     int num_audiop = 1;
1738     int num_assets = 1;
1739     int active_ss_mask[8];
1740     int i, j;
1741     int start_posn;
1742     int hdrsize;
1743     uint32_t mkr;
1744
1745     if (get_bits_left(&s->gb) < 52)
1746         return;
1747
1748     start_posn = get_bits_count(&s->gb) - 32;
1749
1750     skip_bits(&s->gb, 8); // user data
1751     ss_index = get_bits(&s->gb, 2);
1752
1753     blownup = get_bits1(&s->gb);
1754     hdrsize = get_bits(&s->gb,  8 + 4 * blownup) + 1; // header_size
1755     skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1756
1757     s->static_fields = get_bits1(&s->gb);
1758     if (s->static_fields) {
1759         skip_bits(&s->gb, 2); // reference clock code
1760         skip_bits(&s->gb, 3); // frame duration code
1761
1762         if (get_bits1(&s->gb))
1763             skip_bits_long(&s->gb, 36); // timestamp
1764
1765         /* a single stream can contain multiple audio assets that can be
1766          * combined to form multiple audio presentations */
1767
1768         num_audiop = get_bits(&s->gb, 3) + 1;
1769         if (num_audiop > 1) {
1770             av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
1771             /* ignore such streams for now */
1772             return;
1773         }
1774
1775         num_assets = get_bits(&s->gb, 3) + 1;
1776         if (num_assets > 1) {
1777             av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
1778             /* ignore such streams for now */
1779             return;
1780         }
1781
1782         for (i = 0; i < num_audiop; i++)
1783             active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
1784
1785         for (i = 0; i < num_audiop; i++)
1786             for (j = 0; j <= ss_index; j++)
1787                 if (active_ss_mask[i] & (1 << j))
1788                     skip_bits(&s->gb, 8); // active asset mask
1789
1790         s->mix_metadata = get_bits1(&s->gb);
1791         if (s->mix_metadata) {
1792             int mix_out_mask_size;
1793
1794             skip_bits(&s->gb, 2); // adjustment level
1795             mix_out_mask_size  = (get_bits(&s->gb, 2) + 1) << 2;
1796             s->num_mix_configs =  get_bits(&s->gb, 2) + 1;
1797
1798             for (i = 0; i < s->num_mix_configs; i++) {
1799                 int mix_out_mask        = get_bits(&s->gb, mix_out_mask_size);
1800                 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
1801             }
1802         }
1803     }
1804
1805     for (i = 0; i < num_assets; i++)
1806         skip_bits_long(&s->gb, 16 + 4 * blownup);  // asset size
1807
1808     for (i = 0; i < num_assets; i++) {
1809         if (dca_exss_parse_asset_header(s))
1810             return;
1811     }
1812
1813     /* not parsed further, we were only interested in the extensions mask
1814      * from the asset header */
1815
1816     if(num_assets > 0) {
1817         j = get_bits_count(&s->gb);
1818         if(start_posn + hdrsize * 8 > j)
1819             skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
1820
1821         /* check first asset for XBR -- should also check extension mask! */
1822         mkr = get_bits_long(&s->gb, 32);
1823
1824         if(mkr == 0x655e315e)
1825             dca_xbr_parse_frame(s);
1826     }
1827 }
1828
1829 /**
1830  * Main frame decoding function
1831  * FIXME add arguments
1832  */
1833 static int dca_decode_frame(AVCodecContext *avctx, void *data,
1834                             int *got_frame_ptr, AVPacket *avpkt)
1835 {
1836     const uint8_t *buf = avpkt->data;
1837     int buf_size = avpkt->size;
1838
1839     int lfe_samples;
1840     int num_core_channels = 0;
1841     int i, ret;
1842     float   *samples_flt;
1843     int16_t *samples_s16;
1844     DCAContext *s = avctx->priv_data;
1845     int channels;
1846     int core_ss_end;
1847
1848
1849     s->xch_present = 0;
1850
1851     s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1852                                                   DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1853     if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1854         av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1855         return AVERROR_INVALIDDATA;
1856     }
1857
1858     init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
1859     if ((ret = dca_parse_frame_header(s)) < 0) {
1860         //seems like the frame is corrupt, try with the next one
1861         return ret;
1862     }
1863     //set AVCodec values with parsed data
1864     avctx->sample_rate = s->sample_rate;
1865     avctx->bit_rate    = s->bit_rate;
1866
1867     s->profile = FF_PROFILE_DTS;
1868
1869     for (i = 0; i < (s->sample_blocks / 8); i++) {
1870         if ((ret = dca_decode_block(s, 0, i))) {
1871             av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1872             return ret;
1873         }
1874     }
1875
1876     /* record number of core channels incase less than max channels are requested */
1877     num_core_channels = s->prim_channels;
1878
1879     if (s->ext_coding)
1880         s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
1881     else
1882         s->core_ext_mask = 0;
1883
1884     core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1885
1886     /* only scan for extensions if ext_descr was unknown or indicated a
1887      * supported XCh extension */
1888     if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
1889
1890         /* if ext_descr was unknown, clear s->core_ext_mask so that the
1891          * extensions scan can fill it up */
1892         s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1893
1894         /* extensions start at 32-bit boundaries into bitstream */
1895         skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1896
1897         while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1898             uint32_t bits = get_bits_long(&s->gb, 32);
1899
1900             switch (bits) {
1901             case 0x5a5a5a5a: {
1902                 int ext_amode, xch_fsize;
1903
1904                 s->xch_base_channel = s->prim_channels;
1905
1906                 /* validate sync word using XCHFSIZE field */
1907                 xch_fsize = show_bits(&s->gb, 10);
1908                 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1909                     (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1910                     continue;
1911
1912                 /* skip length-to-end-of-frame field for the moment */
1913                 skip_bits(&s->gb, 10);
1914
1915                 s->core_ext_mask |= DCA_EXT_XCH;
1916
1917                 /* extension amode(number of channels in extension) should be 1 */
1918                 /* AFAIK XCh is not used for more channels */
1919                 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1920                     av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
1921                            " supported!\n", ext_amode);
1922                     continue;
1923                 }
1924
1925                 /* much like core primary audio coding header */
1926                 dca_parse_audio_coding_header(s, s->xch_base_channel);
1927
1928                 for (i = 0; i < (s->sample_blocks / 8); i++)
1929                     if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1930                         av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1931                         continue;
1932                     }
1933
1934                 s->xch_present = 1;
1935                 break;
1936             }
1937             case 0x47004a03:
1938                 /* XXCh: extended channels */
1939                 /* usually found either in core or HD part in DTS-HD HRA streams,
1940                  * but not in DTS-ES which contains XCh extensions instead */
1941                 s->core_ext_mask |= DCA_EXT_XXCH;
1942                 break;
1943
1944             case 0x1d95f262: {
1945                 int fsize96 = show_bits(&s->gb, 12) + 1;
1946                 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1947                     continue;
1948
1949                 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1950                        get_bits_count(&s->gb));
1951                 skip_bits(&s->gb, 12);
1952                 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1953                 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1954
1955                 s->core_ext_mask |= DCA_EXT_X96;
1956                 break;
1957             }
1958             }
1959
1960             skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1961         }
1962     } else {
1963         /* no supported extensions, skip the rest of the core substream */
1964         skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1965     }
1966
1967     if (s->core_ext_mask & DCA_EXT_X96)
1968         s->profile = FF_PROFILE_DTS_96_24;
1969     else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1970         s->profile = FF_PROFILE_DTS_ES;
1971
1972     /* check for ExSS (HD part) */
1973     if (s->dca_buffer_size - s->frame_size > 32 &&
1974         get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
1975         dca_exss_parse_header(s);
1976
1977     avctx->profile = s->profile;
1978
1979     channels = s->prim_channels + !!s->lfe;
1980
1981     if (s->amode < 16) {
1982         avctx->channel_layout = dca_core_channel_layout[s->amode];
1983
1984         if (s->xch_present && (!avctx->request_channels ||
1985                                avctx->request_channels > num_core_channels + !!s->lfe)) {
1986             avctx->channel_layout |= AV_CH_BACK_CENTER;
1987             if (s->lfe) {
1988                 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1989                 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
1990             } else {
1991                 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
1992             }
1993         } else {
1994             channels = num_core_channels + !!s->lfe;
1995             s->xch_present = 0; /* disable further xch processing */
1996             if (s->lfe) {
1997                 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1998                 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1999             } else
2000                 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
2001         }
2002
2003         if (channels > !!s->lfe &&
2004             s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
2005             return AVERROR_INVALIDDATA;
2006
2007         if (avctx->request_channels == 2 && s->prim_channels > 2) {
2008             channels = 2;
2009             s->output = DCA_STEREO;
2010             avctx->channel_layout = AV_CH_LAYOUT_STEREO;
2011         }
2012         else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
2013             static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2014             s->channel_order_tab = dca_channel_order_native;
2015         }
2016     } else {
2017         av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
2018         return AVERROR_INVALIDDATA;
2019     }
2020
2021     if (avctx->channels != channels) {
2022         if (avctx->channels)
2023             av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
2024         avctx->channels = channels;
2025     }
2026
2027     /* get output buffer */
2028     s->frame.nb_samples = 256 * (s->sample_blocks / 8);
2029     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
2030         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2031         return ret;
2032     }
2033     samples_flt = (float *)   s->frame.data[0];
2034     samples_s16 = (int16_t *) s->frame.data[0];
2035
2036     /* filter to get final output */
2037     for (i = 0; i < (s->sample_blocks / 8); i++) {
2038         dca_filter_channels(s, i);
2039
2040         /* If this was marked as a DTS-ES stream we need to subtract back- */
2041         /* channel from SL & SR to remove matrixed back-channel signal */
2042         if ((s->source_pcm_res & 1) && s->xch_present) {
2043             float *back_chan = s->samples + s->channel_order_tab[s->xch_base_channel]     * 256;
2044             float *lt_chan   = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;
2045             float *rt_chan   = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;
2046             s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2047             s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2048         }
2049
2050         if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
2051             s->fmt_conv.float_interleave(samples_flt, s->samples_chanptr, 256,
2052                                          channels);
2053             samples_flt += 256 * channels;
2054         } else {
2055             s->fmt_conv.float_to_int16_interleave(samples_s16,
2056                                                   s->samples_chanptr, 256,
2057                                                   channels);
2058             samples_s16 += 256 * channels;
2059         }
2060     }
2061
2062     /* update lfe history */
2063     lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2064     for (i = 0; i < 2 * s->lfe * 4; i++)
2065         s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2066
2067     *got_frame_ptr    = 1;
2068     *(AVFrame *) data = s->frame;
2069
2070     return buf_size;
2071 }
2072
2073
2074
2075 /**
2076  * DCA initialization
2077  *
2078  * @param avctx     pointer to the AVCodecContext
2079  */
2080
2081 static av_cold int dca_decode_init(AVCodecContext *avctx)
2082 {
2083     DCAContext *s = avctx->priv_data;
2084     int i;
2085
2086     s->avctx = avctx;
2087     dca_init_vlcs();
2088
2089     avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
2090     ff_mdct_init(&s->imdct, 6, 1, 1.0);
2091     ff_synth_filter_init(&s->synth);
2092     ff_dcadsp_init(&s->dcadsp);
2093     ff_fmt_convert_init(&s->fmt_conv, avctx);
2094
2095     for (i = 0; i < DCA_PRIM_CHANNELS_MAX + 1; i++)
2096         s->samples_chanptr[i] = s->samples + i * 256;
2097
2098     if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
2099         avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
2100         s->scale_bias     = 1.0 / 32768.0;
2101     } else {
2102         avctx->sample_fmt = AV_SAMPLE_FMT_S16;
2103         s->scale_bias     = 1.0;
2104     }
2105
2106     /* allow downmixing to stereo */
2107     if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
2108         avctx->request_channels == 2) {
2109         avctx->channels = avctx->request_channels;
2110     }
2111
2112     avcodec_get_frame_defaults(&s->frame);
2113     avctx->coded_frame = &s->frame;
2114
2115     return 0;
2116 }
2117
2118 static av_cold int dca_decode_end(AVCodecContext *avctx)
2119 {
2120     DCAContext *s = avctx->priv_data;
2121     ff_mdct_end(&s->imdct);
2122     return 0;
2123 }
2124
2125 static const AVProfile profiles[] = {
2126     { FF_PROFILE_DTS,        "DTS"        },
2127     { FF_PROFILE_DTS_ES,     "DTS-ES"     },
2128     { FF_PROFILE_DTS_96_24,  "DTS 96/24"  },
2129     { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2130     { FF_PROFILE_DTS_HD_MA,  "DTS-HD MA"  },
2131     { FF_PROFILE_UNKNOWN },
2132 };
2133
2134 AVCodec ff_dca_decoder = {
2135     .name            = "dca",
2136     .type            = AVMEDIA_TYPE_AUDIO,
2137     .id              = CODEC_ID_DTS,
2138     .priv_data_size  = sizeof(DCAContext),
2139     .init            = dca_decode_init,
2140     .decode          = dca_decode_frame,
2141     .close           = dca_decode_end,
2142     .long_name       = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2143     .capabilities    = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2144     .sample_fmts     = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
2145                                                        AV_SAMPLE_FMT_S16,
2146                                                        AV_SAMPLE_FMT_NONE },
2147     .profiles        = NULL_IF_CONFIG_SMALL(profiles),
2148 };