Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / 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 FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #include <math.h>
26 #include <stddef.h>
27 #include <stdio.h>
28
29 #include "libavutil/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 "dcadata.h"
41 #include "dcahuff.h"
42 #include "dca.h"
43 #include "mathops.h"
44 #include "synth_filter.h"
45 #include "dcadsp.h"
46 #include "fmtconvert.h"
47 #include "internal.h"
48
49 #if ARCH_ARM
50 #   include "arm/dca.h"
51 #endif
52
53 //#define TRACE
54
55 #define DCA_PRIM_CHANNELS_MAX  (7)
56 #define DCA_ABITS_MAX         (32)      /* Should be 28 */
57 #define DCA_SUBSUBFRAMES_MAX   (4)
58 #define DCA_SUBFRAMES_MAX     (16)
59 #define DCA_BLOCKS_MAX        (16)
60 #define DCA_LFE_MAX            (3)
61 #define DCA_CHSETS_MAX         (4)
62 #define DCA_CHSET_CHANS_MAX    (8)
63
64 enum DCAMode {
65     DCA_MONO = 0,
66     DCA_CHANNEL,
67     DCA_STEREO,
68     DCA_STEREO_SUMDIFF,
69     DCA_STEREO_TOTAL,
70     DCA_3F,
71     DCA_2F1R,
72     DCA_3F1R,
73     DCA_2F2R,
74     DCA_3F2R,
75     DCA_4F2R
76 };
77
78 /* these are unconfirmed but should be mostly correct */
79 enum DCAExSSSpeakerMask {
80     DCA_EXSS_FRONT_CENTER          = 0x0001,
81     DCA_EXSS_FRONT_LEFT_RIGHT      = 0x0002,
82     DCA_EXSS_SIDE_REAR_LEFT_RIGHT  = 0x0004,
83     DCA_EXSS_LFE                   = 0x0008,
84     DCA_EXSS_REAR_CENTER           = 0x0010,
85     DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
86     DCA_EXSS_REAR_LEFT_RIGHT       = 0x0040,
87     DCA_EXSS_FRONT_HIGH_CENTER     = 0x0080,
88     DCA_EXSS_OVERHEAD              = 0x0100,
89     DCA_EXSS_CENTER_LEFT_RIGHT     = 0x0200,
90     DCA_EXSS_WIDE_LEFT_RIGHT       = 0x0400,
91     DCA_EXSS_SIDE_LEFT_RIGHT       = 0x0800,
92     DCA_EXSS_LFE2                  = 0x1000,
93     DCA_EXSS_SIDE_HIGH_LEFT_RIGHT  = 0x2000,
94     DCA_EXSS_REAR_HIGH_CENTER      = 0x4000,
95     DCA_EXSS_REAR_HIGH_LEFT_RIGHT  = 0x8000,
96 };
97
98 enum DCAXxchSpeakerMask {
99     DCA_XXCH_FRONT_CENTER          = 0x0000001,
100     DCA_XXCH_FRONT_LEFT            = 0x0000002,
101     DCA_XXCH_FRONT_RIGHT           = 0x0000004,
102     DCA_XXCH_SIDE_REAR_LEFT        = 0x0000008,
103     DCA_XXCH_SIDE_REAR_RIGHT       = 0x0000010,
104     DCA_XXCH_LFE1                  = 0x0000020,
105     DCA_XXCH_REAR_CENTER           = 0x0000040,
106     DCA_XXCH_SURROUND_REAR_LEFT    = 0x0000080,
107     DCA_XXCH_SURROUND_REAR_RIGHT   = 0x0000100,
108     DCA_XXCH_SIDE_SURROUND_LEFT    = 0x0000200,
109     DCA_XXCH_SIDE_SURROUND_RIGHT   = 0x0000400,
110     DCA_XXCH_FRONT_CENTER_LEFT     = 0x0000800,
111     DCA_XXCH_FRONT_CENTER_RIGHT    = 0x0001000,
112     DCA_XXCH_FRONT_HIGH_LEFT       = 0x0002000,
113     DCA_XXCH_FRONT_HIGH_CENTER     = 0x0004000,
114     DCA_XXCH_FRONT_HIGH_RIGHT      = 0x0008000,
115     DCA_XXCH_LFE2                  = 0x0010000,
116     DCA_XXCH_SIDE_FRONT_LEFT       = 0x0020000,
117     DCA_XXCH_SIDE_FRONT_RIGHT      = 0x0040000,
118     DCA_XXCH_OVERHEAD              = 0x0080000,
119     DCA_XXCH_SIDE_HIGH_LEFT        = 0x0100000,
120     DCA_XXCH_SIDE_HIGH_RIGHT       = 0x0200000,
121     DCA_XXCH_REAR_HIGH_CENTER      = 0x0400000,
122     DCA_XXCH_REAR_HIGH_LEFT        = 0x0800000,
123     DCA_XXCH_REAR_HIGH_RIGHT       = 0x1000000,
124     DCA_XXCH_REAR_LOW_CENTER       = 0x2000000,
125     DCA_XXCH_REAR_LOW_LEFT         = 0x4000000,
126     DCA_XXCH_REAR_LOW_RIGHT        = 0x8000000,
127 };
128
129 static const uint32_t map_xxch_to_native[28] = {
130     AV_CH_FRONT_CENTER,
131     AV_CH_FRONT_LEFT,
132     AV_CH_FRONT_RIGHT,
133     AV_CH_SIDE_LEFT,
134     AV_CH_SIDE_RIGHT,
135     AV_CH_LOW_FREQUENCY,
136     AV_CH_BACK_CENTER,
137     AV_CH_BACK_LEFT,
138     AV_CH_BACK_RIGHT,
139     AV_CH_SIDE_LEFT,           /* side surround left -- dup sur side L */
140     AV_CH_SIDE_RIGHT,          /* side surround right -- dup sur side R */
141     AV_CH_FRONT_LEFT_OF_CENTER,
142     AV_CH_FRONT_RIGHT_OF_CENTER,
143     AV_CH_TOP_FRONT_LEFT,
144     AV_CH_TOP_FRONT_CENTER,
145     AV_CH_TOP_FRONT_RIGHT,
146     AV_CH_LOW_FREQUENCY,        /* lfe2 -- duplicate lfe1 position */
147     AV_CH_FRONT_LEFT_OF_CENTER, /* side front left -- dup front cntr L */
148     AV_CH_FRONT_RIGHT_OF_CENTER,/* side front right -- dup front cntr R */
149     AV_CH_TOP_CENTER,           /* overhead */
150     AV_CH_TOP_FRONT_LEFT,       /* side high left -- dup */
151     AV_CH_TOP_FRONT_RIGHT,      /* side high right -- dup */
152     AV_CH_TOP_BACK_CENTER,
153     AV_CH_TOP_BACK_LEFT,
154     AV_CH_TOP_BACK_RIGHT,
155     AV_CH_BACK_CENTER,          /* rear low center -- dup */
156     AV_CH_BACK_LEFT,            /* rear low left -- dup */
157     AV_CH_BACK_RIGHT            /* read low right -- dup  */
158 };
159
160 enum DCAExtensionMask {
161     DCA_EXT_CORE       = 0x001, ///< core in core substream
162     DCA_EXT_XXCH       = 0x002, ///< XXCh channels extension in core substream
163     DCA_EXT_X96        = 0x004, ///< 96/24 extension in core substream
164     DCA_EXT_XCH        = 0x008, ///< XCh channel extension in core substream
165     DCA_EXT_EXSS_CORE  = 0x010, ///< core in ExSS (extension substream)
166     DCA_EXT_EXSS_XBR   = 0x020, ///< extended bitrate extension in ExSS
167     DCA_EXT_EXSS_XXCH  = 0x040, ///< XXCh channels extension in ExSS
168     DCA_EXT_EXSS_X96   = 0x080, ///< 96/24 extension in ExSS
169     DCA_EXT_EXSS_LBR   = 0x100, ///< low bitrate component in ExSS
170     DCA_EXT_EXSS_XLL   = 0x200, ///< lossless extension in ExSS
171 };
172
173 /* -1 are reserved or unknown */
174 static const int dca_ext_audio_descr_mask[] = {
175     DCA_EXT_XCH,
176     -1,
177     DCA_EXT_X96,
178     DCA_EXT_XCH | DCA_EXT_X96,
179     -1,
180     -1,
181     DCA_EXT_XXCH,
182     -1,
183 };
184
185 /* extensions that reside in core substream */
186 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
187
188 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
189  * Some compromises have been made for special configurations. Most configurations
190  * are never used so complete accuracy is not needed.
191  *
192  * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
193  * S  -> side, when both rear and back are configured move one of them to the side channel
194  * OV -> center back
195  * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
196  */
197 static const uint64_t dca_core_channel_layout[] = {
198     AV_CH_FRONT_CENTER,                                                     ///< 1, A
199     AV_CH_LAYOUT_STEREO,                                                    ///< 2, A + B (dual mono)
200     AV_CH_LAYOUT_STEREO,                                                    ///< 2, L + R (stereo)
201     AV_CH_LAYOUT_STEREO,                                                    ///< 2, (L + R) + (L - R) (sum-difference)
202     AV_CH_LAYOUT_STEREO,                                                    ///< 2, LT + RT (left and right total)
203     AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER,                               ///< 3, C + L + R
204     AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER,                                ///< 3, L + R + S
205     AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER,           ///< 4, C + L + R + S
206     AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,               ///< 4, L + R + SL + SR
207
208     AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
209     AV_CH_SIDE_RIGHT,                                                       ///< 5, C + L + R + SL + SR
210
211     AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
212     AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER,               ///< 6, CL + CR + L + R + SL + SR
213
214     AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
215     AV_CH_FRONT_CENTER  | AV_CH_BACK_CENTER,                                ///< 6, C + L + R + LR + RR + OV
216
217     AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
218     AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER   |
219     AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT,                                     ///< 6, CF + CR + LF + RF + LR + RR
220
221     AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER   |
222     AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
223     AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,                                     ///< 7, CL + C + CR + L + R + SL + SR
224
225     AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
226     AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
227     AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT,                                     ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
228
229     AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER   |
230     AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
231     AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT,                 ///< 8, CL + C + CR + L + R + SL + S + SR
232 };
233
234 static const int8_t dca_lfe_index[] = {
235     1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
236 };
237
238 static const int8_t dca_channel_reorder_lfe[][9] = {
239     { 0, -1, -1, -1, -1, -1, -1, -1, -1},
240     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
241     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
242     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
243     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
244     { 2,  0,  1, -1, -1, -1, -1, -1, -1},
245     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
246     { 2,  0,  1,  4, -1, -1, -1, -1, -1},
247     { 0,  1,  3,  4, -1, -1, -1, -1, -1},
248     { 2,  0,  1,  4,  5, -1, -1, -1, -1},
249     { 3,  4,  0,  1,  5,  6, -1, -1, -1},
250     { 2,  0,  1,  4,  5,  6, -1, -1, -1},
251     { 0,  6,  4,  5,  2,  3, -1, -1, -1},
252     { 4,  2,  5,  0,  1,  6,  7, -1, -1},
253     { 5,  6,  0,  1,  7,  3,  8,  4, -1},
254     { 4,  2,  5,  0,  1,  6,  8,  7, -1},
255 };
256
257 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
258     { 0,  2, -1, -1, -1, -1, -1, -1, -1},
259     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
260     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
261     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
262     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
263     { 2,  0,  1,  4, -1, -1, -1, -1, -1},
264     { 0,  1,  3,  4, -1, -1, -1, -1, -1},
265     { 2,  0,  1,  4,  5, -1, -1, -1, -1},
266     { 0,  1,  4,  5,  3, -1, -1, -1, -1},
267     { 2,  0,  1,  5,  6,  4, -1, -1, -1},
268     { 3,  4,  0,  1,  6,  7,  5, -1, -1},
269     { 2,  0,  1,  4,  5,  6,  7, -1, -1},
270     { 0,  6,  4,  5,  2,  3,  7, -1, -1},
271     { 4,  2,  5,  0,  1,  7,  8,  6, -1},
272     { 5,  6,  0,  1,  8,  3,  9,  4,  7},
273     { 4,  2,  5,  0,  1,  6,  9,  8,  7},
274 };
275
276 static const int8_t dca_channel_reorder_nolfe[][9] = {
277     { 0, -1, -1, -1, -1, -1, -1, -1, -1},
278     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
279     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
280     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
281     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
282     { 2,  0,  1, -1, -1, -1, -1, -1, -1},
283     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
284     { 2,  0,  1,  3, -1, -1, -1, -1, -1},
285     { 0,  1,  2,  3, -1, -1, -1, -1, -1},
286     { 2,  0,  1,  3,  4, -1, -1, -1, -1},
287     { 2,  3,  0,  1,  4,  5, -1, -1, -1},
288     { 2,  0,  1,  3,  4,  5, -1, -1, -1},
289     { 0,  5,  3,  4,  1,  2, -1, -1, -1},
290     { 3,  2,  4,  0,  1,  5,  6, -1, -1},
291     { 4,  5,  0,  1,  6,  2,  7,  3, -1},
292     { 3,  2,  4,  0,  1,  5,  7,  6, -1},
293 };
294
295 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
296     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
297     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
298     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
299     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
300     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
301     { 2,  0,  1,  3, -1, -1, -1, -1, -1},
302     { 0,  1,  2,  3, -1, -1, -1, -1, -1},
303     { 2,  0,  1,  3,  4, -1, -1, -1, -1},
304     { 0,  1,  3,  4,  2, -1, -1, -1, -1},
305     { 2,  0,  1,  4,  5,  3, -1, -1, -1},
306     { 2,  3,  0,  1,  5,  6,  4, -1, -1},
307     { 2,  0,  1,  3,  4,  5,  6, -1, -1},
308     { 0,  5,  3,  4,  1,  2,  6, -1, -1},
309     { 3,  2,  4,  0,  1,  6,  7,  5, -1},
310     { 4,  5,  0,  1,  7,  2,  8,  3,  6},
311     { 3,  2,  4,  0,  1,  5,  8,  7,  6},
312 };
313
314 #define DCA_DOLBY                  101           /* FIXME */
315
316 #define DCA_CHANNEL_BITS             6
317 #define DCA_CHANNEL_MASK          0x3F
318
319 #define DCA_LFE                   0x80
320
321 #define HEADER_SIZE                 14
322
323 #define DCA_MAX_FRAME_SIZE       16384
324 #define DCA_MAX_EXSS_HEADER_SIZE  4096
325
326 #define DCA_BUFFER_PADDING_SIZE   1024
327
328 #define DCA_NSYNCAUX        0x9A1105A0
329
330 /** Bit allocation */
331 typedef struct {
332     int offset;                 ///< code values offset
333     int maxbits[8];             ///< max bits in VLC
334     int wrap;                   ///< wrap for get_vlc2()
335     VLC vlc[8];                 ///< actual codes
336 } BitAlloc;
337
338 static BitAlloc dca_bitalloc_index;    ///< indexes for samples VLC select
339 static BitAlloc dca_tmode;             ///< transition mode VLCs
340 static BitAlloc dca_scalefactor;       ///< scalefactor VLCs
341 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
342
343 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
344                                          int idx)
345 {
346     return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
347            ba->offset;
348 }
349
350 typedef struct {
351     const AVClass *class;       ///< class for AVOptions
352     AVCodecContext *avctx;
353     /* Frame header */
354     int frame_type;             ///< type of the current frame
355     int samples_deficit;        ///< deficit sample count
356     int crc_present;            ///< crc is present in the bitstream
357     int sample_blocks;          ///< number of PCM sample blocks
358     int frame_size;             ///< primary frame byte size
359     int amode;                  ///< audio channels arrangement
360     int sample_rate;            ///< audio sampling rate
361     int bit_rate;               ///< transmission bit rate
362     int bit_rate_index;         ///< transmission bit rate index
363
364     int dynrange;               ///< embedded dynamic range flag
365     int timestamp;              ///< embedded time stamp flag
366     int aux_data;               ///< auxiliary data flag
367     int hdcd;                   ///< source material is mastered in HDCD
368     int ext_descr;              ///< extension audio descriptor flag
369     int ext_coding;             ///< extended coding flag
370     int aspf;                   ///< audio sync word insertion flag
371     int lfe;                    ///< low frequency effects flag
372     int predictor_history;      ///< predictor history flag
373     int header_crc;             ///< header crc check bytes
374     int multirate_inter;        ///< multirate interpolator switch
375     int version;                ///< encoder software revision
376     int copy_history;           ///< copy history
377     int source_pcm_res;         ///< source pcm resolution
378     int front_sum;              ///< front sum/difference flag
379     int surround_sum;           ///< surround sum/difference flag
380     int dialog_norm;            ///< dialog normalisation parameter
381
382     /* Primary audio coding header */
383     int subframes;              ///< number of subframes
384     int total_channels;         ///< number of channels including extensions
385     int prim_channels;          ///< number of primary audio channels
386     int subband_activity[DCA_PRIM_CHANNELS_MAX];    ///< subband activity count
387     int vq_start_subband[DCA_PRIM_CHANNELS_MAX];    ///< high frequency vq start subband
388     int joint_intensity[DCA_PRIM_CHANNELS_MAX];     ///< joint intensity coding index
389     int transient_huffman[DCA_PRIM_CHANNELS_MAX];   ///< transient mode code book
390     int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
391     int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX];    ///< bit allocation quantizer select
392     int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
393     float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];   ///< scale factor adjustment
394
395     /* Primary audio coding side information */
396     int subsubframes[DCA_SUBFRAMES_MAX];                         ///< number of subsubframes
397     int partial_samples[DCA_SUBFRAMES_MAX];                      ///< partial subsubframe samples count
398     int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< prediction mode (ADPCM used or not)
399     int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];      ///< prediction VQ coefs
400     int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];           ///< bit allocation index
401     int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< transition mode (transients)
402     int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];///< scale factors (2 if transient)
403     int joint_huff[DCA_PRIM_CHANNELS_MAX];                       ///< joint subband scale factors codebook
404     int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
405     float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2];            ///< stereo downmix coefficients
406     int dynrange_coef;                                           ///< dynamic range coefficient
407
408     /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
409      * Input:  primary audio channels (incl. LFE if present)
410      * Output: downmix audio channels (up to 4, no LFE) */
411     uint8_t  core_downmix;                                       ///< embedded downmix coefficients available
412     uint8_t  core_downmix_amode;                                 ///< audio channel arrangement of embedded downmix
413     uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4];   ///< embedded downmix coefficients (9-bit codes)
414
415     int32_t  high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];  ///< VQ encoded high frequency subbands
416
417     float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)];      ///< Low frequency effect data
418     int lfe_scale_factor;
419
420     /* Subband samples history (for ADPCM) */
421     DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
422     DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
423     DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
424     int hist_index[DCA_PRIM_CHANNELS_MAX];
425     DECLARE_ALIGNED(32, float, raXin)[32];
426
427     int output;                 ///< type of output
428
429     DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
430     float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
431     float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
432     uint8_t *extra_channels_buffer;
433     unsigned int extra_channels_buffer_size;
434
435     uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
436     int dca_buffer_size;        ///< how much data is in the dca_buffer
437
438     const int8_t *channel_order_tab;  ///< channel reordering table, lfe and non lfe
439     GetBitContext gb;
440     /* Current position in DCA frame */
441     int current_subframe;
442     int current_subsubframe;
443
444     int core_ext_mask;          ///< present extensions in the core substream
445
446     /* XCh extension information */
447     int xch_present;            ///< XCh extension present and valid
448     int xch_base_channel;       ///< index of first (only) channel containing XCH data
449     int xch_disable;            ///< whether the XCh extension should be decoded or not
450
451     /* XXCH extension information */
452     int xxch_chset;
453     int xxch_nbits_spk_mask;
454     uint32_t xxch_core_spkmask;
455     uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
456     int xxch_chset_nch[4];
457     float xxch_dmix_sf[DCA_CHSETS_MAX];
458
459     uint32_t xxch_dmix_embedded;  /* lower layer has mix pre-embedded, per chset */
460     float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
461
462     int8_t xxch_order_tab[32];
463     int8_t lfe_index;
464
465     /* ExSS header parser */
466     int static_fields;          ///< static fields present
467     int mix_metadata;           ///< mixing metadata present
468     int num_mix_configs;        ///< number of mix out configurations
469     int mix_config_num_ch[4];   ///< number of channels in each mix out configuration
470
471     int profile;
472
473     int debug_flag;             ///< used for suppressing repeated error messages output
474     AVFloatDSPContext fdsp;
475     FFTContext imdct;
476     SynthFilterContext synth;
477     DCADSPContext dcadsp;
478     FmtConvertContext fmt_conv;
479 } DCAContext;
480
481 static float dca_dmix_code(unsigned code);
482
483 static const uint16_t dca_vlc_offs[] = {
484         0,   512,   640,   768,  1282,  1794,  2436,  3080,  3770,  4454,  5364,
485      5372,  5380,  5388,  5392,  5396,  5412,  5420,  5428,  5460,  5492,  5508,
486      5572,  5604,  5668,  5796,  5860,  5892,  6412,  6668,  6796,  7308,  7564,
487      7820,  8076,  8620,  9132,  9388,  9910, 10166, 10680, 11196, 11726, 12240,
488     12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
489     18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
490 };
491
492 static av_cold void dca_init_vlcs(void)
493 {
494     static int vlcs_initialized = 0;
495     int i, j, c = 14;
496     static VLC_TYPE dca_table[23622][2];
497
498     if (vlcs_initialized)
499         return;
500
501     dca_bitalloc_index.offset = 1;
502     dca_bitalloc_index.wrap = 2;
503     for (i = 0; i < 5; i++) {
504         dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
505         dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
506         init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
507                  bitalloc_12_bits[i], 1, 1,
508                  bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
509     }
510     dca_scalefactor.offset = -64;
511     dca_scalefactor.wrap = 2;
512     for (i = 0; i < 5; i++) {
513         dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
514         dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
515         init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
516                  scales_bits[i], 1, 1,
517                  scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
518     }
519     dca_tmode.offset = 0;
520     dca_tmode.wrap = 1;
521     for (i = 0; i < 4; i++) {
522         dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
523         dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
524         init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
525                  tmode_bits[i], 1, 1,
526                  tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
527     }
528
529     for (i = 0; i < 10; i++)
530         for (j = 0; j < 7; j++) {
531             if (!bitalloc_codes[i][j])
532                 break;
533             dca_smpl_bitalloc[i + 1].offset                 = bitalloc_offsets[i];
534             dca_smpl_bitalloc[i + 1].wrap                   = 1 + (j > 4);
535             dca_smpl_bitalloc[i + 1].vlc[j].table           = &dca_table[dca_vlc_offs[c]];
536             dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
537
538             init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
539                      bitalloc_sizes[i],
540                      bitalloc_bits[i][j], 1, 1,
541                      bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
542             c++;
543         }
544     vlcs_initialized = 1;
545 }
546
547 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
548 {
549     while (len--)
550         *dst++ = get_bits(gb, bits);
551 }
552
553 static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
554 {
555     int i, base, mask;
556
557     /* locate channel set containing the channel */
558     for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
559          i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
560         base += av_popcount(mask);
561
562     return base + av_popcount(mask & (xxch_ch - 1));
563 }
564
565 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
566                                          int xxch)
567 {
568     int i, j;
569     static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
570     static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
571     static const int thr[11]    = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
572     int hdr_pos = 0, hdr_size = 0;
573     float scale_factor;
574     int this_chans, acc_mask;
575     int embedded_downmix;
576     int nchans, mask[8];
577     int coeff, ichan;
578
579     /* xxch has arbitrary sized audio coding headers */
580     if (xxch) {
581         hdr_pos  = get_bits_count(&s->gb);
582         hdr_size = get_bits(&s->gb, 7) + 1;
583     }
584
585     nchans = get_bits(&s->gb, 3) + 1;
586     s->total_channels = nchans + base_channel;
587     s->prim_channels  = s->total_channels;
588
589     /* obtain speaker layout mask & downmix coefficients for XXCH */
590     if (xxch) {
591         acc_mask = s->xxch_core_spkmask;
592
593         this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
594         s->xxch_spk_masks[s->xxch_chset] = this_chans;
595         s->xxch_chset_nch[s->xxch_chset] = nchans;
596
597         for (i = 0; i <= s->xxch_chset; i++)
598             acc_mask |= s->xxch_spk_masks[i];
599
600         /* check for downmixing information */
601         if (get_bits1(&s->gb)) {
602             embedded_downmix = get_bits1(&s->gb);
603             coeff            = get_bits(&s->gb, 6);
604
605             if (coeff<1 || coeff>61) {
606                 av_log(s->avctx, AV_LOG_ERROR, "6bit coeff %d is out of range\n", coeff);
607                 return AVERROR_INVALIDDATA;
608             }
609
610             scale_factor     = -1.0f / dca_dmix_code((coeff<<2)-3);
611
612             s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
613
614             for (i = base_channel; i < s->prim_channels; i++) {
615                 mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
616             }
617
618             for (j = base_channel; j < s->prim_channels; j++) {
619                 memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
620                 s->xxch_dmix_embedded |= (embedded_downmix << j);
621                 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
622                     if (mask[j] & (1 << i)) {
623                         if ((1 << i) == DCA_XXCH_LFE1) {
624                             av_log(s->avctx, AV_LOG_WARNING,
625                                    "DCA-XXCH: dmix to LFE1 not supported.\n");
626                             continue;
627                         }
628
629                         coeff = get_bits(&s->gb, 7);
630                         ichan = dca_xxch2index(s, 1 << i);
631                         if ((coeff&63)<1 || (coeff&63)>61) {
632                             av_log(s->avctx, AV_LOG_ERROR, "7bit coeff %d is out of range\n", coeff);
633                             return AVERROR_INVALIDDATA;
634                         }
635                         s->xxch_dmix_coeff[j][ichan] = dca_dmix_code((coeff<<2)-3);
636                     }
637                 }
638             }
639         }
640     }
641
642     if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
643         s->prim_channels = DCA_PRIM_CHANNELS_MAX;
644
645
646     for (i = base_channel; i < s->prim_channels; i++) {
647         s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
648         if (s->subband_activity[i] > DCA_SUBBANDS)
649             s->subband_activity[i] = DCA_SUBBANDS;
650     }
651     for (i = base_channel; i < s->prim_channels; i++) {
652         s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
653         if (s->vq_start_subband[i] > DCA_SUBBANDS)
654             s->vq_start_subband[i] = DCA_SUBBANDS;
655     }
656     get_array(&s->gb, s->joint_intensity + base_channel,     s->prim_channels - base_channel, 3);
657     get_array(&s->gb, s->transient_huffman + base_channel,   s->prim_channels - base_channel, 2);
658     get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
659     get_array(&s->gb, s->bitalloc_huffman + base_channel,    s->prim_channels - base_channel, 3);
660
661     /* Get codebooks quantization indexes */
662     if (!base_channel)
663         memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
664     for (j = 1; j < 11; j++)
665         for (i = base_channel; i < s->prim_channels; i++)
666             s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
667
668     /* Get scale factor adjustment */
669     for (j = 0; j < 11; j++)
670         for (i = base_channel; i < s->prim_channels; i++)
671             s->scalefactor_adj[i][j] = 1;
672
673     for (j = 1; j < 11; j++)
674         for (i = base_channel; i < s->prim_channels; i++)
675             if (s->quant_index_huffman[i][j] < thr[j])
676                 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
677
678     if (!xxch) {
679         if (s->crc_present) {
680             /* Audio header CRC check */
681             get_bits(&s->gb, 16);
682         }
683     } else {
684         /* Skip to the end of the header, also ignore CRC if present  */
685         i = get_bits_count(&s->gb);
686         if (hdr_pos + 8 * hdr_size > i)
687             skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
688     }
689
690     s->current_subframe    = 0;
691     s->current_subsubframe = 0;
692
693 #ifdef TRACE
694     av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
695     av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
696     for (i = base_channel; i < s->prim_channels; i++) {
697         av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
698                s->subband_activity[i]);
699         av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
700                s->vq_start_subband[i]);
701         av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
702                s->joint_intensity[i]);
703         av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
704                s->transient_huffman[i]);
705         av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
706                s->scalefactor_huffman[i]);
707         av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
708                s->bitalloc_huffman[i]);
709         av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
710         for (j = 0; j < 11; j++)
711             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
712         av_log(s->avctx, AV_LOG_DEBUG, "\n");
713         av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
714         for (j = 0; j < 11; j++)
715             av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
716         av_log(s->avctx, AV_LOG_DEBUG, "\n");
717     }
718 #endif
719
720     return 0;
721 }
722
723 static int dca_parse_frame_header(DCAContext *s)
724 {
725     init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
726
727     /* Sync code */
728     skip_bits_long(&s->gb, 32);
729
730     /* Frame header */
731     s->frame_type        = get_bits(&s->gb, 1);
732     s->samples_deficit   = get_bits(&s->gb, 5) + 1;
733     s->crc_present       = get_bits(&s->gb, 1);
734     s->sample_blocks     = get_bits(&s->gb, 7) + 1;
735     s->frame_size        = get_bits(&s->gb, 14) + 1;
736     if (s->frame_size < 95)
737         return AVERROR_INVALIDDATA;
738     s->amode             = get_bits(&s->gb, 6);
739     s->sample_rate       = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
740     if (!s->sample_rate)
741         return AVERROR_INVALIDDATA;
742     s->bit_rate_index    = get_bits(&s->gb, 5);
743     s->bit_rate          = dca_bit_rates[s->bit_rate_index];
744     if (!s->bit_rate)
745         return AVERROR_INVALIDDATA;
746
747     skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
748     s->dynrange          = get_bits(&s->gb, 1);
749     s->timestamp         = get_bits(&s->gb, 1);
750     s->aux_data          = get_bits(&s->gb, 1);
751     s->hdcd              = get_bits(&s->gb, 1);
752     s->ext_descr         = get_bits(&s->gb, 3);
753     s->ext_coding        = get_bits(&s->gb, 1);
754     s->aspf              = get_bits(&s->gb, 1);
755     s->lfe               = get_bits(&s->gb, 2);
756     s->predictor_history = get_bits(&s->gb, 1);
757
758     if (s->lfe > 2) {
759         s->lfe = 0;
760         av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
761         return AVERROR_INVALIDDATA;
762     }
763
764     /* TODO: check CRC */
765     if (s->crc_present)
766         s->header_crc    = get_bits(&s->gb, 16);
767
768     s->multirate_inter   = get_bits(&s->gb, 1);
769     s->version           = get_bits(&s->gb, 4);
770     s->copy_history      = get_bits(&s->gb, 2);
771     s->source_pcm_res    = get_bits(&s->gb, 3);
772     s->front_sum         = get_bits(&s->gb, 1);
773     s->surround_sum      = get_bits(&s->gb, 1);
774     s->dialog_norm       = get_bits(&s->gb, 4);
775
776     /* FIXME: channels mixing levels */
777     s->output = s->amode;
778     if (s->lfe)
779         s->output |= DCA_LFE;
780
781 #ifdef TRACE
782     av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
783     av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
784     av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
785     av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
786            s->sample_blocks, s->sample_blocks * 32);
787     av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
788     av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
789            s->amode, dca_channels[s->amode]);
790     av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
791            s->sample_rate);
792     av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
793            s->bit_rate);
794     av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
795     av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
796     av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
797     av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
798     av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
799     av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
800     av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
801     av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
802     av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
803            s->predictor_history);
804     av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
805     av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
806            s->multirate_inter);
807     av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
808     av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
809     av_log(s->avctx, AV_LOG_DEBUG,
810            "source pcm resolution: %i (%i bits/sample)\n",
811            s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
812     av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
813     av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
814     av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
815     av_log(s->avctx, AV_LOG_DEBUG, "\n");
816 #endif
817
818     /* Primary audio coding header */
819     s->subframes         = get_bits(&s->gb, 4) + 1;
820
821     return dca_parse_audio_coding_header(s, 0, 0);
822 }
823
824
825 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
826 {
827     if (level < 5) {
828         /* huffman encoded */
829         value += get_bitalloc(gb, &dca_scalefactor, level);
830         value = av_clip(value, 0, (1 << log2range) - 1);
831     } else if (level < 8) {
832         if (level + 1 > log2range) {
833             skip_bits(gb, level + 1 - log2range);
834             value = get_bits(gb, log2range);
835         } else {
836             value = get_bits(gb, level + 1);
837         }
838     }
839     return value;
840 }
841
842 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
843 {
844     /* Primary audio coding side information */
845     int j, k;
846
847     if (get_bits_left(&s->gb) < 0)
848         return AVERROR_INVALIDDATA;
849
850     if (!base_channel) {
851         s->subsubframes[s->current_subframe]    = get_bits(&s->gb, 2) + 1;
852         s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
853     }
854
855     for (j = base_channel; j < s->prim_channels; j++) {
856         for (k = 0; k < s->subband_activity[j]; k++)
857             s->prediction_mode[j][k] = get_bits(&s->gb, 1);
858     }
859
860     /* Get prediction codebook */
861     for (j = base_channel; j < s->prim_channels; j++) {
862         for (k = 0; k < s->subband_activity[j]; k++) {
863             if (s->prediction_mode[j][k] > 0) {
864                 /* (Prediction coefficient VQ address) */
865                 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
866             }
867         }
868     }
869
870     /* Bit allocation index */
871     for (j = base_channel; j < s->prim_channels; j++) {
872         for (k = 0; k < s->vq_start_subband[j]; k++) {
873             if (s->bitalloc_huffman[j] == 6)
874                 s->bitalloc[j][k] = get_bits(&s->gb, 5);
875             else if (s->bitalloc_huffman[j] == 5)
876                 s->bitalloc[j][k] = get_bits(&s->gb, 4);
877             else if (s->bitalloc_huffman[j] == 7) {
878                 av_log(s->avctx, AV_LOG_ERROR,
879                        "Invalid bit allocation index\n");
880                 return AVERROR_INVALIDDATA;
881             } else {
882                 s->bitalloc[j][k] =
883                     get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
884             }
885
886             if (s->bitalloc[j][k] > 26) {
887                 av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
888                         j, k, s->bitalloc[j][k]);
889                 return AVERROR_INVALIDDATA;
890             }
891         }
892     }
893
894     /* Transition mode */
895     for (j = base_channel; j < s->prim_channels; j++) {
896         for (k = 0; k < s->subband_activity[j]; k++) {
897             s->transition_mode[j][k] = 0;
898             if (s->subsubframes[s->current_subframe] > 1 &&
899                 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
900                 s->transition_mode[j][k] =
901                     get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
902             }
903         }
904     }
905
906     if (get_bits_left(&s->gb) < 0)
907         return AVERROR_INVALIDDATA;
908
909     for (j = base_channel; j < s->prim_channels; j++) {
910         const uint32_t *scale_table;
911         int scale_sum, log_size;
912
913         memset(s->scale_factor[j], 0,
914                s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
915
916         if (s->scalefactor_huffman[j] == 6) {
917             scale_table = scale_factor_quant7;
918             log_size = 7;
919         } else {
920             scale_table = scale_factor_quant6;
921             log_size = 6;
922         }
923
924         /* When huffman coded, only the difference is encoded */
925         scale_sum = 0;
926
927         for (k = 0; k < s->subband_activity[j]; k++) {
928             if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
929                 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
930                 s->scale_factor[j][k][0] = scale_table[scale_sum];
931             }
932
933             if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
934                 /* Get second scale factor */
935                 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
936                 s->scale_factor[j][k][1] = scale_table[scale_sum];
937             }
938         }
939     }
940
941     /* Joint subband scale factor codebook select */
942     for (j = base_channel; j < s->prim_channels; j++) {
943         /* Transmitted only if joint subband coding enabled */
944         if (s->joint_intensity[j] > 0)
945             s->joint_huff[j] = get_bits(&s->gb, 3);
946     }
947
948     if (get_bits_left(&s->gb) < 0)
949         return AVERROR_INVALIDDATA;
950
951     /* Scale factors for joint subband coding */
952     for (j = base_channel; j < s->prim_channels; j++) {
953         int source_channel;
954
955         /* Transmitted only if joint subband coding enabled */
956         if (s->joint_intensity[j] > 0) {
957             int scale = 0;
958             source_channel = s->joint_intensity[j] - 1;
959
960             /* When huffman coded, only the difference is encoded
961              * (is this valid as well for joint scales ???) */
962
963             for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
964                 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
965                 s->joint_scale_factor[j][k] = scale;    /*joint_scale_table[scale]; */
966             }
967
968             if (!(s->debug_flag & 0x02)) {
969                 av_log(s->avctx, AV_LOG_DEBUG,
970                        "Joint stereo coding not supported\n");
971                 s->debug_flag |= 0x02;
972             }
973         }
974     }
975
976     /* Dynamic range coefficient */
977     if (!base_channel && s->dynrange)
978         s->dynrange_coef = get_bits(&s->gb, 8);
979
980     /* Side information CRC check word */
981     if (s->crc_present) {
982         get_bits(&s->gb, 16);
983     }
984
985     /*
986      * Primary audio data arrays
987      */
988
989     /* VQ encoded high frequency subbands */
990     for (j = base_channel; j < s->prim_channels; j++)
991         for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
992             /* 1 vector -> 32 samples */
993             s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
994
995     /* Low frequency effect data */
996     if (!base_channel && s->lfe) {
997         int quant7;
998         /* LFE samples */
999         int lfe_samples = 2 * s->lfe * (4 + block_index);
1000         int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1001         float lfe_scale;
1002
1003         for (j = lfe_samples; j < lfe_end_sample; j++) {
1004             /* Signed 8 bits int */
1005             s->lfe_data[j] = get_sbits(&s->gb, 8);
1006         }
1007
1008         /* Scale factor index */
1009         quant7 = get_bits(&s->gb, 8);
1010         if (quant7 > 127) {
1011             avpriv_request_sample(s->avctx, "LFEScaleIndex larger than 127");
1012             return AVERROR_INVALIDDATA;
1013         }
1014         s->lfe_scale_factor = scale_factor_quant7[quant7];
1015
1016         /* Quantization step size * scale factor */
1017         lfe_scale = 0.035 * s->lfe_scale_factor;
1018
1019         for (j = lfe_samples; j < lfe_end_sample; j++)
1020             s->lfe_data[j] *= lfe_scale;
1021     }
1022
1023 #ifdef TRACE
1024     av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
1025            s->subsubframes[s->current_subframe]);
1026     av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
1027            s->partial_samples[s->current_subframe]);
1028
1029     for (j = base_channel; j < s->prim_channels; j++) {
1030         av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
1031         for (k = 0; k < s->subband_activity[j]; k++)
1032             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
1033         av_log(s->avctx, AV_LOG_DEBUG, "\n");
1034     }
1035     for (j = base_channel; j < s->prim_channels; j++) {
1036         for (k = 0; k < s->subband_activity[j]; k++)
1037             av_log(s->avctx, AV_LOG_DEBUG,
1038                    "prediction coefs: %f, %f, %f, %f\n",
1039                    (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
1040                    (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
1041                    (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
1042                    (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
1043     }
1044     for (j = base_channel; j < s->prim_channels; j++) {
1045         av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
1046         for (k = 0; k < s->vq_start_subband[j]; k++)
1047             av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
1048         av_log(s->avctx, AV_LOG_DEBUG, "\n");
1049     }
1050     for (j = base_channel; j < s->prim_channels; j++) {
1051         av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
1052         for (k = 0; k < s->subband_activity[j]; k++)
1053             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
1054         av_log(s->avctx, AV_LOG_DEBUG, "\n");
1055     }
1056     for (j = base_channel; j < s->prim_channels; j++) {
1057         av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
1058         for (k = 0; k < s->subband_activity[j]; k++) {
1059             if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
1060                 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
1061             if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
1062                 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
1063         }
1064         av_log(s->avctx, AV_LOG_DEBUG, "\n");
1065     }
1066     for (j = base_channel; j < s->prim_channels; j++) {
1067         if (s->joint_intensity[j] > 0) {
1068             int source_channel = s->joint_intensity[j] - 1;
1069             av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
1070             for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
1071                 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
1072             av_log(s->avctx, AV_LOG_DEBUG, "\n");
1073         }
1074     }
1075     for (j = base_channel; j < s->prim_channels; j++)
1076         for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
1077             av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
1078     if (!base_channel && s->lfe) {
1079         int lfe_samples = 2 * s->lfe * (4 + block_index);
1080         int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1081
1082         av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
1083         for (j = lfe_samples; j < lfe_end_sample; j++)
1084             av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
1085         av_log(s->avctx, AV_LOG_DEBUG, "\n");
1086     }
1087 #endif
1088
1089     return 0;
1090 }
1091
1092 static void qmf_32_subbands(DCAContext *s, int chans,
1093                             float samples_in[32][8], float *samples_out,
1094                             float scale)
1095 {
1096     const float *prCoeff;
1097
1098     int sb_act = s->subband_activity[chans];
1099
1100     scale *= sqrt(1 / 8.0);
1101
1102     /* Select filter */
1103     if (!s->multirate_inter)    /* Non-perfect reconstruction */
1104         prCoeff = fir_32bands_nonperfect;
1105     else                        /* Perfect reconstruction */
1106         prCoeff = fir_32bands_perfect;
1107
1108     s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
1109                               s->subband_fir_hist[chans],
1110                               &s->hist_index[chans],
1111                               s->subband_fir_noidea[chans], prCoeff,
1112                               samples_out, s->raXin, scale);
1113 }
1114
1115 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
1116                                   int num_deci_sample, float *samples_in,
1117                                   float *samples_out)
1118 {
1119     /* samples_in: An array holding decimated samples.
1120      *   Samples in current subframe starts from samples_in[0],
1121      *   while samples_in[-1], samples_in[-2], ..., stores samples
1122      *   from last subframe as history.
1123      *
1124      * samples_out: An array holding interpolated samples
1125      */
1126
1127     int idx;
1128     const float *prCoeff;
1129     int deciindex;
1130
1131     /* Select decimation filter */
1132     if (decimation_select == 1) {
1133         idx = 1;
1134         prCoeff = lfe_fir_128;
1135     } else {
1136         idx = 0;
1137         prCoeff = lfe_fir_64;
1138     }
1139     /* Interpolation */
1140     for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1141         s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
1142         samples_in++;
1143         samples_out += 2 * 32 * (1 + idx);
1144     }
1145 }
1146
1147 /* downmixing routines */
1148 #define MIX_REAR1(samples, s1, rs, coef)            \
1149     samples[0][i] += samples[s1][i] * coef[rs][0];  \
1150     samples[1][i] += samples[s1][i] * coef[rs][1];
1151
1152 #define MIX_REAR2(samples, s1, s2, rs, coef)                                          \
1153     samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
1154     samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
1155
1156 #define MIX_FRONT3(samples, coef)                                      \
1157     t = samples[c][i];                                                 \
1158     u = samples[l][i];                                                 \
1159     v = samples[r][i];                                                 \
1160     samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0];  \
1161     samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1162
1163 #define DOWNMIX_TO_STEREO(op1, op2)             \
1164     for (i = 0; i < 256; i++) {                 \
1165         op1                                     \
1166         op2                                     \
1167     }
1168
1169 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
1170                         float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
1171                         const int8_t *channel_mapping)
1172 {
1173     int c, l, r, sl, sr, s;
1174     int i;
1175     float t, u, v;
1176
1177     switch (srcfmt) {
1178     case DCA_MONO:
1179     case DCA_4F2R:
1180         av_log(NULL, AV_LOG_ERROR, "Not implemented!\n");
1181         break;
1182     case DCA_CHANNEL:
1183     case DCA_STEREO:
1184     case DCA_STEREO_TOTAL:
1185     case DCA_STEREO_SUMDIFF:
1186         break;
1187     case DCA_3F:
1188         c = channel_mapping[0];
1189         l = channel_mapping[1];
1190         r = channel_mapping[2];
1191         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1192         break;
1193     case DCA_2F1R:
1194         s = channel_mapping[2];
1195         DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1196         break;
1197     case DCA_3F1R:
1198         c = channel_mapping[0];
1199         l = channel_mapping[1];
1200         r = channel_mapping[2];
1201         s = channel_mapping[3];
1202         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1203                           MIX_REAR1(samples, s, 3, coef));
1204         break;
1205     case DCA_2F2R:
1206         sl = channel_mapping[2];
1207         sr = channel_mapping[3];
1208         DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1209         break;
1210     case DCA_3F2R:
1211         c  = channel_mapping[0];
1212         l  = channel_mapping[1];
1213         r  = channel_mapping[2];
1214         sl = channel_mapping[3];
1215         sr = channel_mapping[4];
1216         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1217                           MIX_REAR2(samples, sl, sr, 3, coef));
1218         break;
1219     }
1220     if (lfe_present) {
1221         int lf_buf = dca_lfe_index[srcfmt];
1222         int lf_idx = dca_channels [srcfmt];
1223         for (i = 0; i < 256; i++) {
1224             samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
1225             samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
1226         }
1227     }
1228 }
1229
1230
1231 #ifndef decode_blockcodes
1232 /* Very compact version of the block code decoder that does not use table
1233  * look-up but is slightly slower */
1234 static int decode_blockcode(int code, int levels, int32_t *values)
1235 {
1236     int i;
1237     int offset = (levels - 1) >> 1;
1238
1239     for (i = 0; i < 4; i++) {
1240         int div = FASTDIV(code, levels);
1241         values[i] = code - offset - div * levels;
1242         code = div;
1243     }
1244
1245     return code;
1246 }
1247
1248 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
1249 {
1250     return decode_blockcode(code1, levels, values) |
1251            decode_blockcode(code2, levels, values + 4);
1252 }
1253 #endif
1254
1255 static const uint8_t abits_sizes[7]  = { 7, 10, 12, 13, 15, 17, 19 };
1256 static const uint8_t abits_levels[7] = { 3,  5,  7,  9, 13, 17, 25 };
1257
1258 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1259 {
1260     int k, l;
1261     int subsubframe = s->current_subsubframe;
1262
1263     const float *quant_step_table;
1264
1265     /* FIXME */
1266     float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1267     LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
1268
1269     /*
1270      * Audio data
1271      */
1272
1273     /* Select quantization step size table */
1274     if (s->bit_rate_index == 0x1f)
1275         quant_step_table = lossless_quant_d;
1276     else
1277         quant_step_table = lossy_quant_d;
1278
1279     for (k = base_channel; k < s->prim_channels; k++) {
1280         float rscale[DCA_SUBBANDS];
1281
1282         if (get_bits_left(&s->gb) < 0)
1283             return AVERROR_INVALIDDATA;
1284
1285         for (l = 0; l < s->vq_start_subband[k]; l++) {
1286             int m;
1287
1288             /* Select the mid-tread linear quantizer */
1289             int abits = s->bitalloc[k][l];
1290
1291             float quant_step_size = quant_step_table[abits];
1292
1293             /*
1294              * Determine quantization index code book and its type
1295              */
1296
1297             /* Select quantization index code book */
1298             int sel = s->quant_index_huffman[k][abits];
1299
1300             /*
1301              * Extract bits from the bit stream
1302              */
1303             if (!abits) {
1304                 rscale[l] = 0;
1305                 memset(block + 8 * l, 0, 8 * sizeof(block[0]));
1306             } else {
1307                 /* Deal with transients */
1308                 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1309                 rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
1310                                s->scalefactor_adj[k][sel];
1311
1312                 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1313                     if (abits <= 7) {
1314                         /* Block code */
1315                         int block_code1, block_code2, size, levels, err;
1316
1317                         size   = abits_sizes[abits - 1];
1318                         levels = abits_levels[abits - 1];
1319
1320                         block_code1 = get_bits(&s->gb, size);
1321                         block_code2 = get_bits(&s->gb, size);
1322                         err = decode_blockcodes(block_code1, block_code2,
1323                                                 levels, block + 8 * l);
1324                         if (err) {
1325                             av_log(s->avctx, AV_LOG_ERROR,
1326                                    "ERROR: block code look-up failed\n");
1327                             return AVERROR_INVALIDDATA;
1328                         }
1329                     } else {
1330                         /* no coding */
1331                         for (m = 0; m < 8; m++)
1332                             block[8 * l + m] = get_sbits(&s->gb, abits - 3);
1333                     }
1334                 } else {
1335                     /* Huffman coded */
1336                     for (m = 0; m < 8; m++)
1337                         block[8 * l + m] = get_bitalloc(&s->gb,
1338                                                 &dca_smpl_bitalloc[abits], sel);
1339                 }
1340
1341             }
1342         }
1343
1344         s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
1345                                                block, rscale, 8 * s->vq_start_subband[k]);
1346
1347         for (l = 0; l < s->vq_start_subband[k]; l++) {
1348             int m;
1349             /*
1350              * Inverse ADPCM if in prediction mode
1351              */
1352             if (s->prediction_mode[k][l]) {
1353                 int n;
1354                 if (s->predictor_history)
1355                     subband_samples[k][l][0] += (adpcm_vb[s->prediction_vq[k][l]][0] *
1356                                                  s->subband_samples_hist[k][l][3] +
1357                                                  adpcm_vb[s->prediction_vq[k][l]][1] *
1358                                                  s->subband_samples_hist[k][l][2] +
1359                                                  adpcm_vb[s->prediction_vq[k][l]][2] *
1360                                                  s->subband_samples_hist[k][l][1] +
1361                                                  adpcm_vb[s->prediction_vq[k][l]][3] *
1362                                                  s->subband_samples_hist[k][l][0]) *
1363                                                 (1.0f / 8192);
1364                 for (m = 1; m < 8; m++) {
1365                     float sum = adpcm_vb[s->prediction_vq[k][l]][0] *
1366                                 subband_samples[k][l][m - 1];
1367                     for (n = 2; n <= 4; n++)
1368                         if (m >= n)
1369                             sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1370                                    subband_samples[k][l][m - n];
1371                         else if (s->predictor_history)
1372                             sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1373                                    s->subband_samples_hist[k][l][m - n + 4];
1374                     subband_samples[k][l][m] += sum * (1.0f / 8192);
1375                 }
1376             }
1377         }
1378
1379         /*
1380          * Decode VQ encoded high frequencies
1381          */
1382         if (s->subband_activity[k] > s->vq_start_subband[k]) {
1383             if (!(s->debug_flag & 0x01)) {
1384                 av_log(s->avctx, AV_LOG_DEBUG,
1385                        "Stream with high frequencies VQ coding\n");
1386                 s->debug_flag |= 0x01;
1387             }
1388             s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
1389                                 high_freq_vq, subsubframe * 8,
1390                                 s->scale_factor[k], s->vq_start_subband[k],
1391                                 s->subband_activity[k]);
1392         }
1393     }
1394
1395     /* Check for DSYNC after subsubframe */
1396     if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1397         if (0xFFFF == get_bits(&s->gb, 16)) {   /* 0xFFFF */
1398 #ifdef TRACE
1399             av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1400 #endif
1401         } else {
1402             av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1403             return AVERROR_INVALIDDATA;
1404         }
1405     }
1406
1407     /* Backup predictor history for adpcm */
1408     for (k = base_channel; k < s->prim_channels; k++)
1409         for (l = 0; l < s->vq_start_subband[k]; l++)
1410             AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
1411
1412     return 0;
1413 }
1414
1415 static int dca_filter_channels(DCAContext *s, int block_index)
1416 {
1417     float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1418     int k;
1419
1420     /* 32 subbands QMF */
1421     for (k = 0; k < s->prim_channels; k++) {
1422 /*        static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1423                                             0, 8388608.0, 8388608.0 };*/
1424         if (s->channel_order_tab[k] >= 0)
1425             qmf_32_subbands(s, k, subband_samples[k],
1426                             s->samples_chanptr[s->channel_order_tab[k]],
1427                             M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
1428     }
1429
1430     /* Generate LFE samples for this subsubframe FIXME!!! */
1431     if (s->lfe) {
1432         lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1433                               s->lfe_data + 2 * s->lfe * (block_index + 4),
1434                               s->samples_chanptr[s->lfe_index]);
1435         /* Outputs 20bits pcm samples */
1436     }
1437
1438     /* Downmixing to Stereo */
1439     if (s->prim_channels + !!s->lfe > 2 &&
1440         s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1441         dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
1442                     s->channel_order_tab);
1443     }
1444
1445     return 0;
1446 }
1447
1448
1449 static int dca_subframe_footer(DCAContext *s, int base_channel)
1450 {
1451     int in, out, aux_data_count, aux_data_end, reserved;
1452     uint32_t nsyncaux;
1453
1454     /*
1455      * Unpack optional information
1456      */
1457
1458     /* presumably optional information only appears in the core? */
1459     if (!base_channel) {
1460         if (s->timestamp)
1461             skip_bits_long(&s->gb, 32);
1462
1463         if (s->aux_data) {
1464             aux_data_count = get_bits(&s->gb, 6);
1465
1466             // align (32-bit)
1467             skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1468
1469             aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1470
1471             if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1472                 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1473                        nsyncaux);
1474                 return AVERROR_INVALIDDATA;
1475             }
1476
1477             if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1478                 avpriv_request_sample(s->avctx,
1479                                       "Auxiliary Decode Time Stamp Flag");
1480                 // align (4-bit)
1481                 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1482                 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1483                 skip_bits_long(&s->gb, 44);
1484             }
1485
1486             if ((s->core_downmix = get_bits1(&s->gb))) {
1487                 int am = get_bits(&s->gb, 3);
1488                 switch (am) {
1489                 case 0:
1490                     s->core_downmix_amode = DCA_MONO;
1491                     break;
1492                 case 1:
1493                     s->core_downmix_amode = DCA_STEREO;
1494                     break;
1495                 case 2:
1496                     s->core_downmix_amode = DCA_STEREO_TOTAL;
1497                     break;
1498                 case 3:
1499                     s->core_downmix_amode = DCA_3F;
1500                     break;
1501                 case 4:
1502                     s->core_downmix_amode = DCA_2F1R;
1503                     break;
1504                 case 5:
1505                     s->core_downmix_amode = DCA_2F2R;
1506                     break;
1507                 case 6:
1508                     s->core_downmix_amode = DCA_3F1R;
1509                     break;
1510                 default:
1511                     av_log(s->avctx, AV_LOG_ERROR,
1512                            "Invalid mode %d for embedded downmix coefficients\n",
1513                            am);
1514                     return AVERROR_INVALIDDATA;
1515                 }
1516                 for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
1517                     for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1518                         uint16_t tmp = get_bits(&s->gb, 9);
1519                         if ((tmp & 0xFF) > 241) {
1520                             av_log(s->avctx, AV_LOG_ERROR,
1521                                    "Invalid downmix coefficient code %"PRIu16"\n",
1522                                    tmp);
1523                             return AVERROR_INVALIDDATA;
1524                         }
1525                         s->core_downmix_codes[in][out] = tmp;
1526                     }
1527                 }
1528             }
1529
1530             align_get_bits(&s->gb); // byte align
1531             skip_bits(&s->gb, 16);  // nAUXCRC16
1532
1533             // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1534             if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
1535                  av_log(s->avctx, AV_LOG_ERROR,
1536                         "Overread auxiliary data by %d bits\n", -reserved);
1537                 return AVERROR_INVALIDDATA;
1538             } else if (reserved) {
1539                 avpriv_request_sample(s->avctx,
1540                                       "Core auxiliary data reserved content");
1541                 skip_bits_long(&s->gb, reserved);
1542             }
1543         }
1544
1545         if (s->crc_present && s->dynrange)
1546             get_bits(&s->gb, 16);
1547     }
1548
1549     return 0;
1550 }
1551
1552 /**
1553  * Decode a dca frame block
1554  *
1555  * @param s     pointer to the DCAContext
1556  */
1557
1558 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1559 {
1560     int ret;
1561
1562     /* Sanity check */
1563     if (s->current_subframe >= s->subframes) {
1564         av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1565                s->current_subframe, s->subframes);
1566         return AVERROR_INVALIDDATA;
1567     }
1568
1569     if (!s->current_subsubframe) {
1570 #ifdef TRACE
1571         av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1572 #endif
1573         /* Read subframe header */
1574         if ((ret = dca_subframe_header(s, base_channel, block_index)))
1575             return ret;
1576     }
1577
1578     /* Read subsubframe */
1579 #ifdef TRACE
1580     av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1581 #endif
1582     if ((ret = dca_subsubframe(s, base_channel, block_index)))
1583         return ret;
1584
1585     /* Update state */
1586     s->current_subsubframe++;
1587     if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1588         s->current_subsubframe = 0;
1589         s->current_subframe++;
1590     }
1591     if (s->current_subframe >= s->subframes) {
1592 #ifdef TRACE
1593         av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1594 #endif
1595         /* Read subframe footer */
1596         if ((ret = dca_subframe_footer(s, base_channel)))
1597             return ret;
1598     }
1599
1600     return 0;
1601 }
1602
1603 /**
1604  * Return the number of channels in an ExSS speaker mask (HD)
1605  */
1606 static int dca_exss_mask2count(int mask)
1607 {
1608     /* count bits that mean speaker pairs twice */
1609     return av_popcount(mask) +
1610            av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT      |
1611                                DCA_EXSS_FRONT_LEFT_RIGHT       |
1612                                DCA_EXSS_FRONT_HIGH_LEFT_RIGHT  |
1613                                DCA_EXSS_WIDE_LEFT_RIGHT        |
1614                                DCA_EXSS_SIDE_LEFT_RIGHT        |
1615                                DCA_EXSS_SIDE_HIGH_LEFT_RIGHT   |
1616                                DCA_EXSS_SIDE_REAR_LEFT_RIGHT   |
1617                                DCA_EXSS_REAR_LEFT_RIGHT        |
1618                                DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1619 }
1620
1621 /**
1622  * Skip mixing coefficients of a single mix out configuration (HD)
1623  */
1624 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1625 {
1626     int i;
1627
1628     for (i = 0; i < channels; i++) {
1629         int mix_map_mask = get_bits(gb, out_ch);
1630         int num_coeffs = av_popcount(mix_map_mask);
1631         skip_bits_long(gb, num_coeffs * 6);
1632     }
1633 }
1634
1635 /**
1636  * Parse extension substream asset header (HD)
1637  */
1638 static int dca_exss_parse_asset_header(DCAContext *s)
1639 {
1640     int header_pos = get_bits_count(&s->gb);
1641     int header_size;
1642     int channels = 0;
1643     int embedded_stereo = 0;
1644     int embedded_6ch    = 0;
1645     int drc_code_present;
1646     int av_uninit(extensions_mask);
1647     int i, j;
1648
1649     if (get_bits_left(&s->gb) < 16)
1650         return -1;
1651
1652     /* We will parse just enough to get to the extensions bitmask with which
1653      * we can set the profile value. */
1654
1655     header_size = get_bits(&s->gb, 9) + 1;
1656     skip_bits(&s->gb, 3); // asset index
1657
1658     if (s->static_fields) {
1659         if (get_bits1(&s->gb))
1660             skip_bits(&s->gb, 4); // asset type descriptor
1661         if (get_bits1(&s->gb))
1662             skip_bits_long(&s->gb, 24); // language descriptor
1663
1664         if (get_bits1(&s->gb)) {
1665             /* How can one fit 1024 bytes of text here if the maximum value
1666              * for the asset header size field above was 512 bytes? */
1667             int text_length = get_bits(&s->gb, 10) + 1;
1668             if (get_bits_left(&s->gb) < text_length * 8)
1669                 return -1;
1670             skip_bits_long(&s->gb, text_length * 8); // info text
1671         }
1672
1673         skip_bits(&s->gb, 5); // bit resolution - 1
1674         skip_bits(&s->gb, 4); // max sample rate code
1675         channels = get_bits(&s->gb, 8) + 1;
1676
1677         if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1678             int spkr_remap_sets;
1679             int spkr_mask_size = 16;
1680             int num_spkrs[7];
1681
1682             if (channels > 2)
1683                 embedded_stereo = get_bits1(&s->gb);
1684             if (channels > 6)
1685                 embedded_6ch = get_bits1(&s->gb);
1686
1687             if (get_bits1(&s->gb)) {
1688                 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1689                 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1690             }
1691
1692             spkr_remap_sets = get_bits(&s->gb, 3);
1693
1694             for (i = 0; i < spkr_remap_sets; i++) {
1695                 /* std layout mask for each remap set */
1696                 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1697             }
1698
1699             for (i = 0; i < spkr_remap_sets; i++) {
1700                 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1701                 if (get_bits_left(&s->gb) < 0)
1702                     return -1;
1703
1704                 for (j = 0; j < num_spkrs[i]; j++) {
1705                     int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1706                     int num_dec_ch = av_popcount(remap_dec_ch_mask);
1707                     skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1708                 }
1709             }
1710
1711         } else {
1712             skip_bits(&s->gb, 3); // representation type
1713         }
1714     }
1715
1716     drc_code_present = get_bits1(&s->gb);
1717     if (drc_code_present)
1718         get_bits(&s->gb, 8); // drc code
1719
1720     if (get_bits1(&s->gb))
1721         skip_bits(&s->gb, 5); // dialog normalization code
1722
1723     if (drc_code_present && embedded_stereo)
1724         get_bits(&s->gb, 8); // drc stereo code
1725
1726     if (s->mix_metadata && get_bits1(&s->gb)) {
1727         skip_bits(&s->gb, 1); // external mix
1728         skip_bits(&s->gb, 6); // post mix gain code
1729
1730         if (get_bits(&s->gb, 2) != 3) // mixer drc code
1731             skip_bits(&s->gb, 3); // drc limit
1732         else
1733             skip_bits(&s->gb, 8); // custom drc code
1734
1735         if (get_bits1(&s->gb)) // channel specific scaling
1736             for (i = 0; i < s->num_mix_configs; i++)
1737                 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1738         else
1739             skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1740
1741         for (i = 0; i < s->num_mix_configs; i++) {
1742             if (get_bits_left(&s->gb) < 0)
1743                 return -1;
1744             dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1745             if (embedded_6ch)
1746                 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1747             if (embedded_stereo)
1748                 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1749         }
1750     }
1751
1752     switch (get_bits(&s->gb, 2)) {
1753     case 0: extensions_mask = get_bits(&s->gb, 12); break;
1754     case 1: extensions_mask = DCA_EXT_EXSS_XLL;     break;
1755     case 2: extensions_mask = DCA_EXT_EXSS_LBR;     break;
1756     case 3: extensions_mask = 0; /* aux coding */   break;
1757     }
1758
1759     /* not parsed further, we were only interested in the extensions mask */
1760
1761     if (get_bits_left(&s->gb) < 0)
1762         return -1;
1763
1764     if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1765         av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1766         return -1;
1767     }
1768     skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1769
1770     if (extensions_mask & DCA_EXT_EXSS_XLL)
1771         s->profile = FF_PROFILE_DTS_HD_MA;
1772     else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1773                                 DCA_EXT_EXSS_XXCH))
1774         s->profile = FF_PROFILE_DTS_HD_HRA;
1775
1776     if (!(extensions_mask & DCA_EXT_CORE))
1777         av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1778     if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1779         av_log(s->avctx, AV_LOG_WARNING,
1780                "DTS extensions detection mismatch (%d, %d)\n",
1781                extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1782
1783     return 0;
1784 }
1785
1786 static int dca_xbr_parse_frame(DCAContext *s)
1787 {
1788     int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1789     int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1790     int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1791     int anctemp[DCA_CHSET_CHANS_MAX];
1792     int chset_fsize[DCA_CHSETS_MAX];
1793     int n_xbr_ch[DCA_CHSETS_MAX];
1794     int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1795     int i, j, k, l, chset, chan_base;
1796
1797     av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1798
1799     /* get bit position of sync header */
1800     hdr_pos = get_bits_count(&s->gb) - 32;
1801
1802     hdr_size = get_bits(&s->gb, 6) + 1;
1803     num_chsets = get_bits(&s->gb, 2) + 1;
1804
1805     for(i = 0; i < num_chsets; i++)
1806         chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1807
1808     xbr_tmode = get_bits1(&s->gb);
1809
1810     for(i = 0; i < num_chsets; i++) {
1811         n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1812         k = get_bits(&s->gb, 2) + 5;
1813         for(j = 0; j < n_xbr_ch[i]; j++)
1814             active_bands[i][j] = get_bits(&s->gb, k) + 1;
1815     }
1816
1817     /* skip to the end of the header */
1818     i = get_bits_count(&s->gb);
1819     if(hdr_pos + hdr_size * 8 > i)
1820         skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1821
1822     /* loop over the channel data sets */
1823     /* only decode as many channels as we've decoded base data for */
1824     for(chset = 0, chan_base = 0;
1825         chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1826         chan_base += n_xbr_ch[chset++]) {
1827         int start_posn = get_bits_count(&s->gb);
1828         int subsubframe = 0;
1829         int subframe = 0;
1830
1831         /* loop over subframes */
1832         for (k = 0; k < (s->sample_blocks / 8); k++) {
1833             /* parse header if we're on first subsubframe of a block */
1834             if(subsubframe == 0) {
1835                 /* Parse subframe header */
1836                 for(i = 0; i < n_xbr_ch[chset]; i++) {
1837                     anctemp[i] = get_bits(&s->gb, 2) + 2;
1838                 }
1839
1840                 for(i = 0; i < n_xbr_ch[chset]; i++) {
1841                     get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1842                 }
1843
1844                 for(i = 0; i < n_xbr_ch[chset]; i++) {
1845                     anctemp[i] = get_bits(&s->gb, 3);
1846                     if(anctemp[i] < 1) {
1847                         av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1848                         return AVERROR_INVALIDDATA;
1849                     }
1850                 }
1851
1852                 /* generate scale factors */
1853                 for(i = 0; i < n_xbr_ch[chset]; i++) {
1854                     const uint32_t *scale_table;
1855                     int nbits;
1856
1857                     if (s->scalefactor_huffman[chan_base+i] == 6) {
1858                         scale_table = scale_factor_quant7;
1859                     } else {
1860                         scale_table = scale_factor_quant6;
1861                     }
1862
1863                     nbits = anctemp[i];
1864
1865                     for(j = 0; j < active_bands[chset][i]; j++) {
1866                         if(abits_high[i][j] > 0) {
1867                             scale_table_high[i][j][0] =
1868                                 scale_table[get_bits(&s->gb, nbits)];
1869
1870                             if(xbr_tmode && s->transition_mode[i][j]) {
1871                                 scale_table_high[i][j][1] =
1872                                     scale_table[get_bits(&s->gb, nbits)];
1873                             }
1874                         }
1875                     }
1876                 }
1877             }
1878
1879             /* decode audio array for this block */
1880             for(i = 0; i < n_xbr_ch[chset]; i++) {
1881                 for(j = 0; j < active_bands[chset][i]; j++) {
1882                     const int xbr_abits = abits_high[i][j];
1883                     const float quant_step_size = lossless_quant_d[xbr_abits];
1884                     const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1885                     const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1886                     float *subband_samples = s->subband_samples[k][chan_base+i][j];
1887                     int block[8];
1888
1889                     if(xbr_abits <= 0)
1890                         continue;
1891
1892                     if(xbr_abits > 7) {
1893                         get_array(&s->gb, block, 8, xbr_abits - 3);
1894                     } else {
1895                         int block_code1, block_code2, size, levels, err;
1896
1897                         size   = abits_sizes[xbr_abits - 1];
1898                         levels = abits_levels[xbr_abits - 1];
1899
1900                         block_code1 = get_bits(&s->gb, size);
1901                         block_code2 = get_bits(&s->gb, size);
1902                         err = decode_blockcodes(block_code1, block_code2,
1903                                                 levels, block);
1904                         if (err) {
1905                             av_log(s->avctx, AV_LOG_ERROR,
1906                                    "ERROR: DTS-XBR: block code look-up failed\n");
1907                             return AVERROR_INVALIDDATA;
1908                         }
1909                     }
1910
1911                     /* scale & sum into subband */
1912                     for(l = 0; l < 8; l++)
1913                         subband_samples[l] += (float)block[l] * rscale;
1914                 }
1915             }
1916
1917             /* check DSYNC marker */
1918             if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1919                 if(get_bits(&s->gb, 16) != 0xffff) {
1920                     av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1921                     return AVERROR_INVALIDDATA;
1922                 }
1923             }
1924
1925             /* advance sub-sub-frame index */
1926             if(++subsubframe >= s->subsubframes[subframe]) {
1927                 subsubframe = 0;
1928                 subframe++;
1929             }
1930         }
1931
1932         /* skip to next channel set */
1933         i = get_bits_count(&s->gb);
1934         if(start_posn + chset_fsize[chset] * 8 != i) {
1935             j = start_posn + chset_fsize[chset] * 8 - i;
1936             if(j < 0 || j >= 8)
1937                 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1938                        " skipping further than expected (%d bits)\n", j);
1939             skip_bits_long(&s->gb, j);
1940         }
1941     }
1942
1943     return 0;
1944 }
1945
1946 /* parse initial header for XXCH and dump details */
1947 static int dca_xxch_decode_frame(DCAContext *s)
1948 {
1949     int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1950     int i, chset, base_channel, chstart, fsize[8];
1951
1952     /* assume header word has already been parsed */
1953     hdr_pos     = get_bits_count(&s->gb) - 32;
1954     hdr_size    = get_bits(&s->gb, 6) + 1;
1955   /*chhdr_crc   =*/ skip_bits1(&s->gb);
1956     spkmsk_bits = get_bits(&s->gb, 5) + 1;
1957     num_chsets  = get_bits(&s->gb, 2) + 1;
1958
1959     for (i = 0; i < num_chsets; i++)
1960         fsize[i] = get_bits(&s->gb, 14) + 1;
1961
1962     core_spk               = get_bits(&s->gb, spkmsk_bits);
1963     s->xxch_core_spkmask   = core_spk;
1964     s->xxch_nbits_spk_mask = spkmsk_bits;
1965     s->xxch_dmix_embedded  = 0;
1966
1967     /* skip to the end of the header */
1968     i = get_bits_count(&s->gb);
1969     if (hdr_pos + hdr_size * 8 > i)
1970         skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1971
1972     for (chset = 0; chset < num_chsets; chset++) {
1973         chstart       = get_bits_count(&s->gb);
1974         base_channel  = s->prim_channels;
1975         s->xxch_chset = chset;
1976
1977         /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1978            5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1979         dca_parse_audio_coding_header(s, base_channel, 1);
1980
1981         /* decode channel data */
1982         for (i = 0; i < (s->sample_blocks / 8); i++) {
1983             if (dca_decode_block(s, base_channel, i)) {
1984                 av_log(s->avctx, AV_LOG_ERROR,
1985                        "Error decoding DTS-XXCH extension\n");
1986                 continue;
1987             }
1988         }
1989
1990         /* skip to end of this section */
1991         i = get_bits_count(&s->gb);
1992         if (chstart + fsize[chset] * 8 > i)
1993             skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1994     }
1995     s->xxch_chset = num_chsets;
1996
1997     return 0;
1998 }
1999
2000 /**
2001  * Parse extension substream header (HD)
2002  */
2003 static void dca_exss_parse_header(DCAContext *s)
2004 {
2005     int asset_size[8];
2006     int ss_index;
2007     int blownup;
2008     int num_audiop = 1;
2009     int num_assets = 1;
2010     int active_ss_mask[8];
2011     int i, j;
2012     int start_posn;
2013     int hdrsize;
2014     uint32_t mkr;
2015
2016     if (get_bits_left(&s->gb) < 52)
2017         return;
2018
2019     start_posn = get_bits_count(&s->gb) - 32;
2020
2021     skip_bits(&s->gb, 8); // user data
2022     ss_index = get_bits(&s->gb, 2);
2023
2024     blownup = get_bits1(&s->gb);
2025     hdrsize = get_bits(&s->gb,  8 + 4 * blownup) + 1; // header_size
2026     skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
2027
2028     s->static_fields = get_bits1(&s->gb);
2029     if (s->static_fields) {
2030         skip_bits(&s->gb, 2); // reference clock code
2031         skip_bits(&s->gb, 3); // frame duration code
2032
2033         if (get_bits1(&s->gb))
2034             skip_bits_long(&s->gb, 36); // timestamp
2035
2036         /* a single stream can contain multiple audio assets that can be
2037          * combined to form multiple audio presentations */
2038
2039         num_audiop = get_bits(&s->gb, 3) + 1;
2040         if (num_audiop > 1) {
2041             avpriv_request_sample(s->avctx,
2042                                   "Multiple DTS-HD audio presentations");
2043             /* ignore such streams for now */
2044             return;
2045         }
2046
2047         num_assets = get_bits(&s->gb, 3) + 1;
2048         if (num_assets > 1) {
2049             avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
2050             /* ignore such streams for now */
2051             return;
2052         }
2053
2054         for (i = 0; i < num_audiop; i++)
2055             active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
2056
2057         for (i = 0; i < num_audiop; i++)
2058             for (j = 0; j <= ss_index; j++)
2059                 if (active_ss_mask[i] & (1 << j))
2060                     skip_bits(&s->gb, 8); // active asset mask
2061
2062         s->mix_metadata = get_bits1(&s->gb);
2063         if (s->mix_metadata) {
2064             int mix_out_mask_size;
2065
2066             skip_bits(&s->gb, 2); // adjustment level
2067             mix_out_mask_size  = (get_bits(&s->gb, 2) + 1) << 2;
2068             s->num_mix_configs =  get_bits(&s->gb, 2) + 1;
2069
2070             for (i = 0; i < s->num_mix_configs; i++) {
2071                 int mix_out_mask        = get_bits(&s->gb, mix_out_mask_size);
2072                 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
2073             }
2074         }
2075     }
2076
2077     av_assert0(num_assets > 0); // silence a warning
2078
2079     for (i = 0; i < num_assets; i++)
2080         asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
2081
2082     for (i = 0; i < num_assets; i++) {
2083         if (dca_exss_parse_asset_header(s))
2084             return;
2085     }
2086
2087     /* not parsed further, we were only interested in the extensions mask
2088      * from the asset header */
2089
2090         j = get_bits_count(&s->gb);
2091         if (start_posn + hdrsize * 8 > j)
2092             skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
2093
2094         for (i = 0; i < num_assets; i++) {
2095             start_posn = get_bits_count(&s->gb);
2096             mkr        = get_bits_long(&s->gb, 32);
2097
2098             /* parse extensions that we know about */
2099             if (mkr == 0x655e315e) {
2100                 dca_xbr_parse_frame(s);
2101             } else if (mkr == 0x47004a03) {
2102                 dca_xxch_decode_frame(s);
2103                 s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
2104             } else {
2105                 av_log(s->avctx, AV_LOG_DEBUG,
2106                        "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
2107             }
2108
2109             /* skip to end of block */
2110             j = get_bits_count(&s->gb);
2111             if (start_posn + asset_size[i] * 8 > j)
2112                 skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
2113         }
2114 }
2115
2116 static float dca_dmix_code(unsigned code)
2117 {
2118     int sign = (code >> 8) - 1;
2119     code &= 0xff;
2120     return ((dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1 << 15));
2121 }
2122
2123 /**
2124  * Main frame decoding function
2125  * FIXME add arguments
2126  */
2127 static int dca_decode_frame(AVCodecContext *avctx, void *data,
2128                             int *got_frame_ptr, AVPacket *avpkt)
2129 {
2130     AVFrame *frame     = data;
2131     const uint8_t *buf = avpkt->data;
2132     int buf_size = avpkt->size;
2133     int channel_mask;
2134     int channel_layout;
2135     int lfe_samples;
2136     int num_core_channels = 0;
2137     int i, ret;
2138     float **samples_flt;
2139     float *src_chan;
2140     float *dst_chan;
2141     DCAContext *s = avctx->priv_data;
2142     int core_ss_end;
2143     int channels, full_channels;
2144     float scale;
2145     int achan;
2146     int chset;
2147     int mask;
2148     int lavc;
2149     int posn;
2150     int j, k;
2151     int endch;
2152
2153     s->xch_present = 0;
2154
2155     s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
2156                                                   DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
2157     if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
2158         av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
2159         return AVERROR_INVALIDDATA;
2160     }
2161
2162     if ((ret = dca_parse_frame_header(s)) < 0) {
2163         //seems like the frame is corrupt, try with the next one
2164         return ret;
2165     }
2166     //set AVCodec values with parsed data
2167     avctx->sample_rate = s->sample_rate;
2168     avctx->bit_rate    = s->bit_rate;
2169
2170     s->profile = FF_PROFILE_DTS;
2171
2172     for (i = 0; i < (s->sample_blocks / 8); i++) {
2173         if ((ret = dca_decode_block(s, 0, i))) {
2174             av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
2175             return ret;
2176         }
2177     }
2178
2179     /* record number of core channels incase less than max channels are requested */
2180     num_core_channels = s->prim_channels;
2181
2182     if (s->prim_channels + !!s->lfe > 2 &&
2183         avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
2184             /* Stereo downmix coefficients
2185              *
2186              * The decoder can only downmix to 2-channel, so we need to ensure
2187              * embedded downmix coefficients are actually targeting 2-channel.
2188              */
2189             if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
2190                                     s->core_downmix_amode == DCA_STEREO_TOTAL)) {
2191                 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2192                     /* Range checked earlier */
2193                     s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
2194                     s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
2195                 }
2196                 s->output = s->core_downmix_amode;
2197             } else {
2198                 int am = s->amode & DCA_CHANNEL_MASK;
2199                 if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
2200                     av_log(s->avctx, AV_LOG_ERROR,
2201                            "Invalid channel mode %d\n", am);
2202                     return AVERROR_INVALIDDATA;
2203                 }
2204                 if (num_core_channels + !!s->lfe >
2205                     FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
2206                     avpriv_request_sample(s->avctx, "Downmixing %d channels",
2207                                           s->prim_channels + !!s->lfe);
2208                     return AVERROR_PATCHWELCOME;
2209                 }
2210                 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2211                     s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
2212                     s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
2213                 }
2214             }
2215             av_dlog(s->avctx, "Stereo downmix coeffs:\n");
2216             for (i = 0; i < num_core_channels + !!s->lfe; i++) {
2217                 av_dlog(s->avctx, "L, input channel %d = %f\n", i,
2218                         s->downmix_coef[i][0]);
2219                 av_dlog(s->avctx, "R, input channel %d = %f\n", i,
2220                         s->downmix_coef[i][1]);
2221             }
2222             av_dlog(s->avctx, "\n");
2223     }
2224
2225     if (s->ext_coding)
2226         s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
2227     else
2228         s->core_ext_mask = 0;
2229
2230     core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
2231
2232     /* only scan for extensions if ext_descr was unknown or indicated a
2233      * supported XCh extension */
2234     if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
2235
2236         /* if ext_descr was unknown, clear s->core_ext_mask so that the
2237          * extensions scan can fill it up */
2238         s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
2239
2240         /* extensions start at 32-bit boundaries into bitstream */
2241         skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2242
2243         while (core_ss_end - get_bits_count(&s->gb) >= 32) {
2244             uint32_t bits = get_bits_long(&s->gb, 32);
2245
2246             switch (bits) {
2247             case 0x5a5a5a5a: {
2248                 int ext_amode, xch_fsize;
2249
2250                 s->xch_base_channel = s->prim_channels;
2251
2252                 /* validate sync word using XCHFSIZE field */
2253                 xch_fsize = show_bits(&s->gb, 10);
2254                 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
2255                     (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
2256                     continue;
2257
2258                 /* skip length-to-end-of-frame field for the moment */
2259                 skip_bits(&s->gb, 10);
2260
2261                 s->core_ext_mask |= DCA_EXT_XCH;
2262
2263                 /* extension amode(number of channels in extension) should be 1 */
2264                 /* AFAIK XCh is not used for more channels */
2265                 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
2266                     av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
2267                            " supported!\n", ext_amode);
2268                     continue;
2269                 }
2270
2271                 if (s->xch_base_channel < 2) {
2272                     avpriv_request_sample(avctx, "XCh with fewer than 2 base channels");
2273                     continue;
2274                 }
2275
2276                 /* much like core primary audio coding header */
2277                 dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
2278
2279                 for (i = 0; i < (s->sample_blocks / 8); i++)
2280                     if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
2281                         av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
2282                         continue;
2283                     }
2284
2285                 s->xch_present = 1;
2286                 break;
2287             }
2288             case 0x47004a03:
2289                 /* XXCh: extended channels */
2290                 /* usually found either in core or HD part in DTS-HD HRA streams,
2291                  * but not in DTS-ES which contains XCh extensions instead */
2292                 s->core_ext_mask |= DCA_EXT_XXCH;
2293                 dca_xxch_decode_frame(s);
2294                 break;
2295
2296             case 0x1d95f262: {
2297                 int fsize96 = show_bits(&s->gb, 12) + 1;
2298                 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
2299                     continue;
2300
2301                 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
2302                        get_bits_count(&s->gb));
2303                 skip_bits(&s->gb, 12);
2304                 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
2305                 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
2306
2307                 s->core_ext_mask |= DCA_EXT_X96;
2308                 break;
2309             }
2310             }
2311
2312             skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2313         }
2314     } else {
2315         /* no supported extensions, skip the rest of the core substream */
2316         skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
2317     }
2318
2319     if (s->core_ext_mask & DCA_EXT_X96)
2320         s->profile = FF_PROFILE_DTS_96_24;
2321     else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
2322         s->profile = FF_PROFILE_DTS_ES;
2323
2324     /* check for ExSS (HD part) */
2325     if (s->dca_buffer_size - s->frame_size > 32 &&
2326         get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
2327         dca_exss_parse_header(s);
2328
2329     avctx->profile = s->profile;
2330
2331     full_channels = channels = s->prim_channels + !!s->lfe;
2332
2333     /* If we have XXCH then the channel layout is managed differently */
2334     /* note that XLL will also have another way to do things */
2335     if (!(s->core_ext_mask & DCA_EXT_XXCH)
2336         || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
2337             && avctx->request_channels
2338             < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
2339     { /* xxx should also do MA extensions */
2340         if (s->amode < 16) {
2341             avctx->channel_layout = dca_core_channel_layout[s->amode];
2342
2343             if (s->prim_channels + !!s->lfe > 2 &&
2344                 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
2345                 /*
2346                  * Neither the core's auxiliary data nor our default tables contain
2347                  * downmix coefficients for the additional channel coded in the XCh
2348                  * extension, so when we're doing a Stereo downmix, don't decode it.
2349                  */
2350                 s->xch_disable = 1;
2351             }
2352
2353 #if FF_API_REQUEST_CHANNELS
2354 FF_DISABLE_DEPRECATION_WARNINGS
2355             if (s->xch_present && !s->xch_disable &&
2356                 (!avctx->request_channels ||
2357                  avctx->request_channels > num_core_channels + !!s->lfe)) {
2358 FF_ENABLE_DEPRECATION_WARNINGS
2359 #else
2360             if (s->xch_present && !s->xch_disable) {
2361 #endif
2362                 avctx->channel_layout |= AV_CH_BACK_CENTER;
2363                 if (s->lfe) {
2364                     avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2365                     s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
2366                 } else {
2367                     s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
2368                 }
2369                 if (s->channel_order_tab[s->xch_base_channel] < 0)
2370                     return AVERROR_INVALIDDATA;
2371             } else {
2372                 channels = num_core_channels + !!s->lfe;
2373                 s->xch_present = 0; /* disable further xch processing */
2374                 if (s->lfe) {
2375                     avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2376                     s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
2377                 } else
2378                     s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
2379             }
2380
2381             if (channels > !!s->lfe &&
2382                 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
2383                 return AVERROR_INVALIDDATA;
2384
2385             if (av_get_channel_layout_nb_channels(avctx->channel_layout) != channels) {
2386                 av_log(avctx, AV_LOG_ERROR, "Number of channels %d mismatches layout %d\n", channels, av_get_channel_layout_nb_channels(avctx->channel_layout));
2387                 return AVERROR_INVALIDDATA;
2388             }
2389
2390             if (num_core_channels + !!s->lfe > 2 &&
2391                 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
2392                 channels = 2;
2393                 s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
2394                 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
2395             }
2396             else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
2397                 static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2398                 s->channel_order_tab = dca_channel_order_native;
2399             }
2400             s->lfe_index = dca_lfe_index[s->amode];
2401         } else {
2402             av_log(avctx, AV_LOG_ERROR,
2403                    "Non standard configuration %d !\n", s->amode);
2404             return AVERROR_INVALIDDATA;
2405         }
2406
2407         s->xxch_dmix_embedded = 0;
2408     } else {
2409         /* we only get here if an XXCH channel set can be added to the mix */
2410         channel_mask = s->xxch_core_spkmask;
2411
2412         if (avctx->request_channels > 0
2413             && avctx->request_channels < s->prim_channels) {
2414             channels = num_core_channels + !!s->lfe;
2415             for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
2416                                               <= avctx->request_channels; i++) {
2417                 channels += s->xxch_chset_nch[i];
2418                 channel_mask |= s->xxch_spk_masks[i];
2419             }
2420         } else {
2421             channels = s->prim_channels + !!s->lfe;
2422             for (i = 0; i < s->xxch_chset; i++) {
2423                 channel_mask |= s->xxch_spk_masks[i];
2424             }
2425         }
2426
2427         /* Given the DTS spec'ed channel mask, generate an avcodec version */
2428         channel_layout = 0;
2429         for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
2430             if (channel_mask & (1 << i)) {
2431                 channel_layout |= map_xxch_to_native[i];
2432             }
2433         }
2434
2435         /* make sure that we have managed to get equivalent dts/avcodec channel
2436          * masks in some sense -- unfortunately some channels could overlap */
2437         if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
2438             av_log(avctx, AV_LOG_DEBUG,
2439                    "DTS-XXCH: Inconsistent avcodec/dts channel layouts\n");
2440             return AVERROR_INVALIDDATA;
2441         }
2442
2443         avctx->channel_layout = channel_layout;
2444
2445         if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
2446             /* Estimate DTS --> avcodec ordering table */
2447             for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
2448                 mask = chset >= 0 ? s->xxch_spk_masks[chset]
2449                                   : s->xxch_core_spkmask;
2450                 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
2451                     if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
2452                         lavc = map_xxch_to_native[i];
2453                         posn = av_popcount(channel_layout & (lavc - 1));
2454                         s->xxch_order_tab[j++] = posn;
2455                     }
2456                 }
2457
2458             }
2459
2460             s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
2461         } else { /* native ordering */
2462             for (i = 0; i < channels; i++)
2463                 s->xxch_order_tab[i] = i;
2464
2465             s->lfe_index = channels - 1;
2466         }
2467
2468         s->channel_order_tab = s->xxch_order_tab;
2469     }
2470
2471     if (avctx->channels != channels) {
2472         if (avctx->channels)
2473             av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
2474         avctx->channels = channels;
2475     }
2476
2477     /* get output buffer */
2478     frame->nb_samples = 256 * (s->sample_blocks / 8);
2479     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2480         return ret;
2481     samples_flt = (float **)frame->extended_data;
2482
2483     /* allocate buffer for extra channels if downmixing */
2484     if (avctx->channels < full_channels) {
2485         ret = av_samples_get_buffer_size(NULL, full_channels - channels,
2486                                          frame->nb_samples,
2487                                          avctx->sample_fmt, 0);
2488         if (ret < 0)
2489             return ret;
2490
2491         av_fast_malloc(&s->extra_channels_buffer,
2492                        &s->extra_channels_buffer_size, ret);
2493         if (!s->extra_channels_buffer)
2494             return AVERROR(ENOMEM);
2495
2496         ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
2497                                      s->extra_channels_buffer,
2498                                      full_channels - channels,
2499                                      frame->nb_samples, avctx->sample_fmt, 0);
2500         if (ret < 0)
2501             return ret;
2502     }
2503
2504     /* filter to get final output */
2505     for (i = 0; i < (s->sample_blocks / 8); i++) {
2506         int ch;
2507
2508         for (ch = 0; ch < channels; ch++)
2509             s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
2510         for (; ch < full_channels; ch++)
2511             s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
2512
2513         dca_filter_channels(s, i);
2514
2515         /* If this was marked as a DTS-ES stream we need to subtract back- */
2516         /* channel from SL & SR to remove matrixed back-channel signal */
2517         if ((s->source_pcm_res & 1) && s->xch_present) {
2518             float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
2519             float *lt_chan   = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
2520             float *rt_chan   = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
2521             s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2522             s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2523         }
2524
2525         /* If stream contains XXCH, we might need to undo an embedded downmix */
2526         if (s->xxch_dmix_embedded) {
2527             /* Loop over channel sets in turn */
2528             ch = num_core_channels;
2529             for (chset = 0; chset < s->xxch_chset; chset++) {
2530                 endch = ch + s->xxch_chset_nch[chset];
2531                 mask = s->xxch_dmix_embedded;
2532
2533                 /* undo downmix */
2534                 for (j = ch; j < endch; j++) {
2535                     if (mask & (1 << j)) { /* this channel has been mixed-out */
2536                         src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2537                         for (k = 0; k < endch; k++) {
2538                             achan = s->channel_order_tab[k];
2539                             scale = s->xxch_dmix_coeff[j][k];
2540                             if (scale != 0.0) {
2541                                 dst_chan = s->samples_chanptr[achan];
2542                                 s->fdsp.vector_fmac_scalar(dst_chan, src_chan,
2543                                                            -scale, 256);
2544                             }
2545                         }
2546                     }
2547                 }
2548
2549                 /* if a downmix has been embedded then undo the pre-scaling */
2550                 if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
2551                     scale = s->xxch_dmix_sf[chset];
2552
2553                     for (j = 0; j < ch; j++) {
2554                         src_chan = s->samples_chanptr[s->channel_order_tab[j]];
2555                         for (k = 0; k < 256; k++)
2556                             src_chan[k] *= scale;
2557                     }
2558
2559                     /* LFE channel is always part of core, scale if it exists */
2560                     if (s->lfe) {
2561                         src_chan = s->samples_chanptr[s->lfe_index];
2562                         for (k = 0; k < 256; k++)
2563                             src_chan[k] *= scale;
2564                     }
2565                 }
2566
2567                 ch = endch;
2568             }
2569
2570         }
2571     }
2572
2573     /* update lfe history */
2574     lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2575     for (i = 0; i < 2 * s->lfe * 4; i++)
2576         s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2577
2578     /* AVMatrixEncoding
2579      *
2580      * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
2581     ret = ff_side_data_update_matrix_encoding(frame,
2582                                               (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
2583                                               AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
2584     if (ret < 0)
2585         return ret;
2586
2587     *got_frame_ptr = 1;
2588
2589     return buf_size;
2590 }
2591
2592
2593
2594 /**
2595  * DCA initialization
2596  *
2597  * @param avctx     pointer to the AVCodecContext
2598  */
2599
2600 static av_cold int dca_decode_init(AVCodecContext *avctx)
2601 {
2602     DCAContext *s = avctx->priv_data;
2603
2604     s->avctx = avctx;
2605     dca_init_vlcs();
2606
2607     avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
2608     ff_mdct_init(&s->imdct, 6, 1, 1.0);
2609     ff_synth_filter_init(&s->synth);
2610     ff_dcadsp_init(&s->dcadsp);
2611     ff_fmt_convert_init(&s->fmt_conv, avctx);
2612
2613     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2614
2615     /* allow downmixing to stereo */
2616 #if FF_API_REQUEST_CHANNELS
2617 FF_DISABLE_DEPRECATION_WARNINGS
2618     if (avctx->request_channels == 2)
2619         avctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
2620 FF_ENABLE_DEPRECATION_WARNINGS
2621 #endif
2622     if (avctx->channels > 2 &&
2623         avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
2624         avctx->channels = 2;
2625
2626     return 0;
2627 }
2628
2629 static av_cold int dca_decode_end(AVCodecContext *avctx)
2630 {
2631     DCAContext *s = avctx->priv_data;
2632     ff_mdct_end(&s->imdct);
2633     av_freep(&s->extra_channels_buffer);
2634     return 0;
2635 }
2636
2637 static const AVProfile profiles[] = {
2638     { FF_PROFILE_DTS,        "DTS"        },
2639     { FF_PROFILE_DTS_ES,     "DTS-ES"     },
2640     { FF_PROFILE_DTS_96_24,  "DTS 96/24"  },
2641     { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2642     { FF_PROFILE_DTS_HD_MA,  "DTS-HD MA"  },
2643     { FF_PROFILE_UNKNOWN },
2644 };
2645
2646 static const AVOption options[] = {
2647     { "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 },
2648     { NULL },
2649 };
2650
2651 static const AVClass dca_decoder_class = {
2652     .class_name = "DCA decoder",
2653     .item_name  = av_default_item_name,
2654     .option     = options,
2655     .version    = LIBAVUTIL_VERSION_INT,
2656     .category   = AV_CLASS_CATEGORY_DECODER,
2657 };
2658
2659 AVCodec ff_dca_decoder = {
2660     .name            = "dca",
2661     .long_name       = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2662     .type            = AVMEDIA_TYPE_AUDIO,
2663     .id              = AV_CODEC_ID_DTS,
2664     .priv_data_size  = sizeof(DCAContext),
2665     .init            = dca_decode_init,
2666     .decode          = dca_decode_frame,
2667     .close           = dca_decode_end,
2668     .capabilities    = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2669     .sample_fmts     = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2670                                                        AV_SAMPLE_FMT_NONE },
2671     .profiles        = NULL_IF_CONFIG_SMALL(profiles),
2672     .priv_class      = &dca_decoder_class,
2673 };