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