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