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