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