]> git.sesse.net Git - ffmpeg/blob - libavcodec/dca.c
Remove unneeded local variable, frame_size, in output_frame_end().
[ffmpeg] / libavcodec / dca.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/intmath.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavcore/audioconvert.h"
32 #include "avcodec.h"
33 #include "dsputil.h"
34 #include "fft.h"
35 #include "get_bits.h"
36 #include "put_bits.h"
37 #include "dcadata.h"
38 #include "dcahuff.h"
39 #include "dca.h"
40 #include "synth_filter.h"
41 #include "dcadsp.h"
42
43 //#define TRACE
44
45 #define DCA_PRIM_CHANNELS_MAX (7)
46 #define DCA_SUBBANDS (32)
47 #define DCA_ABITS_MAX (32)      /* Should be 28 */
48 #define DCA_SUBSUBFRAMES_MAX (4)
49 #define DCA_SUBFRAMES_MAX (16)
50 #define DCA_BLOCKS_MAX (16)
51 #define DCA_LFE_MAX (3)
52
53 enum DCAMode {
54     DCA_MONO = 0,
55     DCA_CHANNEL,
56     DCA_STEREO,
57     DCA_STEREO_SUMDIFF,
58     DCA_STEREO_TOTAL,
59     DCA_3F,
60     DCA_2F1R,
61     DCA_3F1R,
62     DCA_2F2R,
63     DCA_3F2R,
64     DCA_4F2R
65 };
66
67 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
68  * Some compromises have been made for special configurations. Most configurations
69  * are never used so complete accuracy is not needed.
70  *
71  * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
72  * S  -> side, when both rear and back are configured move one of them to the side channel
73  * OV -> center back
74  * All 2 channel configurations -> CH_LAYOUT_STEREO
75  */
76
77 static const int64_t dca_core_channel_layout[] = {
78     AV_CH_FRONT_CENTER,                                                      ///< 1, A
79     AV_CH_LAYOUT_STEREO,                                                     ///< 2, A + B (dual mono)
80     AV_CH_LAYOUT_STEREO,                                                     ///< 2, L + R (stereo)
81     AV_CH_LAYOUT_STEREO,                                                     ///< 2, (L+R) + (L-R) (sum-difference)
82     AV_CH_LAYOUT_STEREO,                                                     ///< 2, LT +RT (left and right total)
83     AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER,                                  ///< 3, C+L+R
84     AV_CH_LAYOUT_STEREO|AV_CH_BACK_CENTER,                                   ///< 3, L+R+S
85     AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|CH_BACK_CENTER,                   ///< 4, C + L + R+ S
86     AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT,                    ///< 4, L + R +SL+ SR
87     AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, ///< 5, C + L + R+ SL+SR
88     AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER,                    ///< 6, CL + CR + L + R + SL + SR
89     AV_CH_LAYOUT_STEREO|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT|AV_CH_FRONT_CENTER|AV_CH_BACK_CENTER,                                      ///< 6, C + L + R+ LR + RR + OV
90     AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_BACK_CENTER|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT,   ///< 6, CF+ CR+LF+ RF+LR + RR
91     AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
92     AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2+ SR1 + SR2
93     AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_BACK_CENTER|AV_CH_SIDE_RIGHT, ///< 8, CL + C+ CR + L + R + SL + S+ SR
94 };
95
96 static const int8_t dca_lfe_index[] = {
97     1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
98 };
99
100 static const int8_t dca_channel_reorder_lfe[][9] = {
101     { 0, -1, -1, -1, -1, -1, -1, -1, -1},
102     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
103     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
104     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
105     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
106     { 2,  0,  1, -1, -1, -1, -1, -1, -1},
107     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
108     { 2,  0,  1,  4, -1, -1, -1, -1, -1},
109     { 0,  1,  3,  4, -1, -1, -1, -1, -1},
110     { 2,  0,  1,  4,  5, -1, -1, -1, -1},
111     { 3,  4,  0,  1,  5,  6, -1, -1, -1},
112     { 2,  0,  1,  4,  5,  6, -1, -1, -1},
113     { 0,  6,  4,  5,  2,  3, -1, -1, -1},
114     { 4,  2,  5,  0,  1,  6,  7, -1, -1},
115     { 5,  6,  0,  1,  7,  3,  8,  4, -1},
116     { 4,  2,  5,  0,  1,  6,  8,  7, -1},
117 };
118
119 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
120     { 0,  2, -1, -1, -1, -1, -1, -1, -1},
121     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
122     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
123     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
124     { 0,  1,  3, -1, -1, -1, -1, -1, -1},
125     { 2,  0,  1,  4, -1, -1, -1, -1, -1},
126     { 0,  1,  3,  4, -1, -1, -1, -1, -1},
127     { 2,  0,  1,  4,  5, -1, -1, -1, -1},
128     { 0,  1,  4,  5,  3, -1, -1, -1, -1},
129     { 2,  0,  1,  5,  6,  4, -1, -1, -1},
130     { 3,  4,  0,  1,  6,  7,  5, -1, -1},
131     { 2,  0,  1,  4,  5,  6,  7, -1, -1},
132     { 0,  6,  4,  5,  2,  3,  7, -1, -1},
133     { 4,  2,  5,  0,  1,  7,  8,  6, -1},
134     { 5,  6,  0,  1,  8,  3,  9,  4,  7},
135     { 4,  2,  5,  0,  1,  6,  9,  8,  7},
136 };
137
138 static const int8_t dca_channel_reorder_nolfe[][9] = {
139     { 0, -1, -1, -1, -1, -1, -1, -1, -1},
140     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
141     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
142     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
143     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
144     { 2,  0,  1, -1, -1, -1, -1, -1, -1},
145     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
146     { 2,  0,  1,  3, -1, -1, -1, -1, -1},
147     { 0,  1,  2,  3, -1, -1, -1, -1, -1},
148     { 2,  0,  1,  3,  4, -1, -1, -1, -1},
149     { 2,  3,  0,  1,  4,  5, -1, -1, -1},
150     { 2,  0,  1,  3,  4,  5, -1, -1, -1},
151     { 0,  5,  3,  4,  1,  2, -1, -1, -1},
152     { 3,  2,  4,  0,  1,  5,  6, -1, -1},
153     { 4,  5,  0,  1,  6,  2,  7,  3, -1},
154     { 3,  2,  4,  0,  1,  5,  7,  6, -1},
155 };
156
157 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
158     { 0,  1, -1, -1, -1, -1, -1, -1, -1},
159     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
160     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
161     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
162     { 0,  1,  2, -1, -1, -1, -1, -1, -1},
163     { 2,  0,  1,  3, -1, -1, -1, -1, -1},
164     { 0,  1,  2,  3, -1, -1, -1, -1, -1},
165     { 2,  0,  1,  3,  4, -1, -1, -1, -1},
166     { 0,  1,  3,  4,  2, -1, -1, -1, -1},
167     { 2,  0,  1,  4,  5,  3, -1, -1, -1},
168     { 2,  3,  0,  1,  5,  6,  4, -1, -1},
169     { 2,  0,  1,  3,  4,  5,  6, -1, -1},
170     { 0,  5,  3,  4,  1,  2,  6, -1, -1},
171     { 3,  2,  4,  0,  1,  6,  7,  5, -1},
172     { 4,  5,  0,  1,  7,  2,  8,  3,  6},
173     { 3,  2,  4,  0,  1,  5,  8,  7,  6},
174 };
175
176 #define DCA_DOLBY 101           /* FIXME */
177
178 #define DCA_CHANNEL_BITS 6
179 #define DCA_CHANNEL_MASK 0x3F
180
181 #define DCA_LFE 0x80
182
183 #define HEADER_SIZE 14
184
185 #define DCA_MAX_FRAME_SIZE 16384
186
187 /** Bit allocation */
188 typedef struct {
189     int offset;                 ///< code values offset
190     int maxbits[8];             ///< max bits in VLC
191     int wrap;                   ///< wrap for get_vlc2()
192     VLC vlc[8];                 ///< actual codes
193 } BitAlloc;
194
195 static BitAlloc dca_bitalloc_index;    ///< indexes for samples VLC select
196 static BitAlloc dca_tmode;             ///< transition mode VLCs
197 static BitAlloc dca_scalefactor;       ///< scalefactor VLCs
198 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
199
200 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
201 {
202     return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
203 }
204
205 typedef struct {
206     AVCodecContext *avctx;
207     /* Frame header */
208     int frame_type;             ///< type of the current frame
209     int samples_deficit;        ///< deficit sample count
210     int crc_present;            ///< crc is present in the bitstream
211     int sample_blocks;          ///< number of PCM sample blocks
212     int frame_size;             ///< primary frame byte size
213     int amode;                  ///< audio channels arrangement
214     int sample_rate;            ///< audio sampling rate
215     int bit_rate;               ///< transmission bit rate
216     int bit_rate_index;         ///< transmission bit rate index
217
218     int downmix;                ///< embedded downmix enabled
219     int dynrange;               ///< embedded dynamic range flag
220     int timestamp;              ///< embedded time stamp flag
221     int aux_data;               ///< auxiliary data flag
222     int hdcd;                   ///< source material is mastered in HDCD
223     int ext_descr;              ///< extension audio descriptor flag
224     int ext_coding;             ///< extended coding flag
225     int aspf;                   ///< audio sync word insertion flag
226     int lfe;                    ///< low frequency effects flag
227     int predictor_history;      ///< predictor history flag
228     int header_crc;             ///< header crc check bytes
229     int multirate_inter;        ///< multirate interpolator switch
230     int version;                ///< encoder software revision
231     int copy_history;           ///< copy history
232     int source_pcm_res;         ///< source pcm resolution
233     int front_sum;              ///< front sum/difference flag
234     int surround_sum;           ///< surround sum/difference flag
235     int dialog_norm;            ///< dialog normalisation parameter
236
237     /* Primary audio coding header */
238     int subframes;              ///< number of subframes
239     int total_channels;         ///< number of channels including extensions
240     int prim_channels;          ///< number of primary audio channels
241     int subband_activity[DCA_PRIM_CHANNELS_MAX];    ///< subband activity count
242     int vq_start_subband[DCA_PRIM_CHANNELS_MAX];    ///< high frequency vq start subband
243     int joint_intensity[DCA_PRIM_CHANNELS_MAX];     ///< joint intensity coding index
244     int transient_huffman[DCA_PRIM_CHANNELS_MAX];   ///< transient mode code book
245     int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
246     int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX];    ///< bit allocation quantizer select
247     int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
248     float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];   ///< scale factor adjustment
249
250     /* Primary audio coding side information */
251     int subsubframes[DCA_SUBFRAMES_MAX];           ///< number of subsubframes
252     int partial_samples[DCA_SUBFRAMES_MAX];        ///< partial subsubframe samples count
253     int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< prediction mode (ADPCM used or not)
254     int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];      ///< prediction VQ coefs
255     int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];           ///< bit allocation index
256     int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< transition mode (transients)
257     int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];    ///< scale factors (2 if transient)
258     int joint_huff[DCA_PRIM_CHANNELS_MAX];                       ///< joint subband scale factors codebook
259     int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
260     int downmix_coef[DCA_PRIM_CHANNELS_MAX][2];                  ///< stereo downmix coefficients
261     int dynrange_coef;                                           ///< dynamic range coefficient
262
263     int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];       ///< VQ encoded high frequency subbands
264
265     float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)];      ///< Low frequency effect data
266     int lfe_scale_factor;
267
268     /* Subband samples history (for ADPCM) */
269     float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
270     DECLARE_ALIGNED(16, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
271     DECLARE_ALIGNED(16, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
272     int hist_index[DCA_PRIM_CHANNELS_MAX];
273     DECLARE_ALIGNED(16, float, raXin)[32];
274
275     int output;                 ///< type of output
276     float add_bias;             ///< output bias
277     float scale_bias;           ///< output scale
278
279     DECLARE_ALIGNED(16, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
280     DECLARE_ALIGNED(16, float, samples)[(DCA_PRIM_CHANNELS_MAX+1)*256];
281     const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX+1];
282
283     uint8_t dca_buffer[DCA_MAX_FRAME_SIZE];
284     int dca_buffer_size;        ///< how much data is in the dca_buffer
285
286     const int8_t* channel_order_tab;                             ///< channel reordering table, lfe and non lfe
287     GetBitContext gb;
288     /* Current position in DCA frame */
289     int current_subframe;
290     int current_subsubframe;
291
292     /* XCh extension information */
293     int xch_present;
294     int xch_base_channel;       ///< index of first (only) channel containing XCH data
295
296     int debug_flag;             ///< used for suppressing repeated error messages output
297     DSPContext dsp;
298     FFTContext imdct;
299     SynthFilterContext synth;
300     DCADSPContext dcadsp;
301 } DCAContext;
302
303 static const uint16_t dca_vlc_offs[] = {
304         0,   512,   640,   768,  1282,  1794,  2436,  3080,  3770,  4454,  5364,
305      5372,  5380,  5388,  5392,  5396,  5412,  5420,  5428,  5460,  5492,  5508,
306      5572,  5604,  5668,  5796,  5860,  5892,  6412,  6668,  6796,  7308,  7564,
307      7820,  8076,  8620,  9132,  9388,  9910, 10166, 10680, 11196, 11726, 12240,
308     12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
309     18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
310 };
311
312 static av_cold void dca_init_vlcs(void)
313 {
314     static int vlcs_initialized = 0;
315     int i, j, c = 14;
316     static VLC_TYPE dca_table[23622][2];
317
318     if (vlcs_initialized)
319         return;
320
321     dca_bitalloc_index.offset = 1;
322     dca_bitalloc_index.wrap = 2;
323     for (i = 0; i < 5; i++) {
324         dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
325         dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
326         init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
327                  bitalloc_12_bits[i], 1, 1,
328                  bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
329     }
330     dca_scalefactor.offset = -64;
331     dca_scalefactor.wrap = 2;
332     for (i = 0; i < 5; i++) {
333         dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
334         dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
335         init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
336                  scales_bits[i], 1, 1,
337                  scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
338     }
339     dca_tmode.offset = 0;
340     dca_tmode.wrap = 1;
341     for (i = 0; i < 4; i++) {
342         dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
343         dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
344         init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
345                  tmode_bits[i], 1, 1,
346                  tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
347     }
348
349     for (i = 0; i < 10; i++)
350         for (j = 0; j < 7; j++){
351             if (!bitalloc_codes[i][j]) break;
352             dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
353             dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
354             dca_smpl_bitalloc[i+1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
355             dca_smpl_bitalloc[i+1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
356             init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
357                      bitalloc_sizes[i],
358                      bitalloc_bits[i][j], 1, 1,
359                      bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
360             c++;
361         }
362     vlcs_initialized = 1;
363 }
364
365 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
366 {
367     while(len--)
368         *dst++ = get_bits(gb, bits);
369 }
370
371 static int dca_parse_audio_coding_header(DCAContext * s, int base_channel)
372 {
373     int i, j;
374     static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
375     static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
376     static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
377
378     s->total_channels    = get_bits(&s->gb, 3) + 1 + base_channel;
379     s->prim_channels     = s->total_channels;
380
381     if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
382         s->prim_channels = DCA_PRIM_CHANNELS_MAX;
383
384
385     for (i = base_channel; i < s->prim_channels; i++) {
386         s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
387         if (s->subband_activity[i] > DCA_SUBBANDS)
388             s->subband_activity[i] = DCA_SUBBANDS;
389     }
390     for (i = base_channel; i < s->prim_channels; i++) {
391         s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
392         if (s->vq_start_subband[i] > DCA_SUBBANDS)
393             s->vq_start_subband[i] = DCA_SUBBANDS;
394     }
395     get_array(&s->gb, s->joint_intensity + base_channel,     s->prim_channels - base_channel, 3);
396     get_array(&s->gb, s->transient_huffman + base_channel,   s->prim_channels - base_channel, 2);
397     get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
398     get_array(&s->gb, s->bitalloc_huffman + base_channel,    s->prim_channels - base_channel, 3);
399
400     /* Get codebooks quantization indexes */
401     if (!base_channel)
402         memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
403     for (j = 1; j < 11; j++)
404         for (i = base_channel; i < s->prim_channels; i++)
405             s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
406
407     /* Get scale factor adjustment */
408     for (j = 0; j < 11; j++)
409         for (i = base_channel; i < s->prim_channels; i++)
410             s->scalefactor_adj[i][j] = 1;
411
412     for (j = 1; j < 11; j++)
413         for (i = base_channel; i < s->prim_channels; i++)
414             if (s->quant_index_huffman[i][j] < thr[j])
415                 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
416
417     if (s->crc_present) {
418         /* Audio header CRC check */
419         get_bits(&s->gb, 16);
420     }
421
422     s->current_subframe = 0;
423     s->current_subsubframe = 0;
424
425 #ifdef TRACE
426     av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
427     av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
428     for (i = base_channel; i < s->prim_channels; i++){
429         av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
430         av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
431         av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
432         av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
433         av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
434         av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
435         av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
436         for (j = 0; j < 11; j++)
437             av_log(s->avctx, AV_LOG_DEBUG, " %i",
438                    s->quant_index_huffman[i][j]);
439         av_log(s->avctx, AV_LOG_DEBUG, "\n");
440         av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
441         for (j = 0; j < 11; j++)
442             av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
443         av_log(s->avctx, AV_LOG_DEBUG, "\n");
444     }
445 #endif
446
447   return 0;
448 }
449
450 static int dca_parse_frame_header(DCAContext * s)
451 {
452     init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
453
454     /* Sync code */
455     get_bits(&s->gb, 32);
456
457     /* Frame header */
458     s->frame_type        = get_bits(&s->gb, 1);
459     s->samples_deficit   = get_bits(&s->gb, 5) + 1;
460     s->crc_present       = get_bits(&s->gb, 1);
461     s->sample_blocks     = get_bits(&s->gb, 7) + 1;
462     s->frame_size        = get_bits(&s->gb, 14) + 1;
463     if (s->frame_size < 95)
464         return -1;
465     s->amode             = get_bits(&s->gb, 6);
466     s->sample_rate       = dca_sample_rates[get_bits(&s->gb, 4)];
467     if (!s->sample_rate)
468         return -1;
469     s->bit_rate_index    = get_bits(&s->gb, 5);
470     s->bit_rate          = dca_bit_rates[s->bit_rate_index];
471     if (!s->bit_rate)
472         return -1;
473
474     s->downmix           = get_bits(&s->gb, 1);
475     s->dynrange          = get_bits(&s->gb, 1);
476     s->timestamp         = get_bits(&s->gb, 1);
477     s->aux_data          = get_bits(&s->gb, 1);
478     s->hdcd              = get_bits(&s->gb, 1);
479     s->ext_descr         = get_bits(&s->gb, 3);
480     s->ext_coding        = get_bits(&s->gb, 1);
481     s->aspf              = get_bits(&s->gb, 1);
482     s->lfe               = get_bits(&s->gb, 2);
483     s->predictor_history = get_bits(&s->gb, 1);
484
485     /* TODO: check CRC */
486     if (s->crc_present)
487         s->header_crc    = get_bits(&s->gb, 16);
488
489     s->multirate_inter   = get_bits(&s->gb, 1);
490     s->version           = get_bits(&s->gb, 4);
491     s->copy_history      = get_bits(&s->gb, 2);
492     s->source_pcm_res    = get_bits(&s->gb, 3);
493     s->front_sum         = get_bits(&s->gb, 1);
494     s->surround_sum      = get_bits(&s->gb, 1);
495     s->dialog_norm       = get_bits(&s->gb, 4);
496
497     /* FIXME: channels mixing levels */
498     s->output = s->amode;
499     if (s->lfe) s->output |= DCA_LFE;
500
501 #ifdef TRACE
502     av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
503     av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
504     av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
505     av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
506            s->sample_blocks, s->sample_blocks * 32);
507     av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
508     av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
509            s->amode, dca_channels[s->amode]);
510     av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
511            s->sample_rate);
512     av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
513            s->bit_rate);
514     av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
515     av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
516     av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
517     av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
518     av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
519     av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
520     av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
521     av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
522     av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
523     av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
524            s->predictor_history);
525     av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
526     av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
527            s->multirate_inter);
528     av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
529     av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
530     av_log(s->avctx, AV_LOG_DEBUG,
531            "source pcm resolution: %i (%i bits/sample)\n",
532            s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
533     av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
534     av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
535     av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
536     av_log(s->avctx, AV_LOG_DEBUG, "\n");
537 #endif
538
539     /* Primary audio coding header */
540     s->subframes         = get_bits(&s->gb, 4) + 1;
541
542     return dca_parse_audio_coding_header(s, 0);
543 }
544
545
546 static inline int get_scale(GetBitContext *gb, int level, int value)
547 {
548    if (level < 5) {
549        /* huffman encoded */
550        value += get_bitalloc(gb, &dca_scalefactor, level);
551    } else if (level < 8)
552        value = get_bits(gb, level + 1);
553    return value;
554 }
555
556 static int dca_subframe_header(DCAContext * s, int base_channel, int block_index)
557 {
558     /* Primary audio coding side information */
559     int j, k;
560
561     if (!base_channel) {
562         s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
563         s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
564     }
565
566     for (j = base_channel; j < s->prim_channels; j++) {
567         for (k = 0; k < s->subband_activity[j]; k++)
568             s->prediction_mode[j][k] = get_bits(&s->gb, 1);
569     }
570
571     /* Get prediction codebook */
572     for (j = base_channel; j < s->prim_channels; j++) {
573         for (k = 0; k < s->subband_activity[j]; k++) {
574             if (s->prediction_mode[j][k] > 0) {
575                 /* (Prediction coefficient VQ address) */
576                 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
577             }
578         }
579     }
580
581     /* Bit allocation index */
582     for (j = base_channel; j < s->prim_channels; j++) {
583         for (k = 0; k < s->vq_start_subband[j]; k++) {
584             if (s->bitalloc_huffman[j] == 6)
585                 s->bitalloc[j][k] = get_bits(&s->gb, 5);
586             else if (s->bitalloc_huffman[j] == 5)
587                 s->bitalloc[j][k] = get_bits(&s->gb, 4);
588             else if (s->bitalloc_huffman[j] == 7) {
589                 av_log(s->avctx, AV_LOG_ERROR,
590                        "Invalid bit allocation index\n");
591                 return -1;
592             } else {
593                 s->bitalloc[j][k] =
594                     get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
595             }
596
597             if (s->bitalloc[j][k] > 26) {
598 //                 av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
599 //                          j, k, s->bitalloc[j][k]);
600                 return -1;
601             }
602         }
603     }
604
605     /* Transition mode */
606     for (j = base_channel; j < s->prim_channels; j++) {
607         for (k = 0; k < s->subband_activity[j]; k++) {
608             s->transition_mode[j][k] = 0;
609             if (s->subsubframes[s->current_subframe] > 1 &&
610                 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
611                 s->transition_mode[j][k] =
612                     get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
613             }
614         }
615     }
616
617     for (j = base_channel; j < s->prim_channels; j++) {
618         const uint32_t *scale_table;
619         int scale_sum;
620
621         memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
622
623         if (s->scalefactor_huffman[j] == 6)
624             scale_table = scale_factor_quant7;
625         else
626             scale_table = scale_factor_quant6;
627
628         /* When huffman coded, only the difference is encoded */
629         scale_sum = 0;
630
631         for (k = 0; k < s->subband_activity[j]; k++) {
632             if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
633                 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
634                 s->scale_factor[j][k][0] = scale_table[scale_sum];
635             }
636
637             if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
638                 /* Get second scale factor */
639                 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
640                 s->scale_factor[j][k][1] = scale_table[scale_sum];
641             }
642         }
643     }
644
645     /* Joint subband scale factor codebook select */
646     for (j = base_channel; j < s->prim_channels; j++) {
647         /* Transmitted only if joint subband coding enabled */
648         if (s->joint_intensity[j] > 0)
649             s->joint_huff[j] = get_bits(&s->gb, 3);
650     }
651
652     /* Scale factors for joint subband coding */
653     for (j = base_channel; j < s->prim_channels; j++) {
654         int source_channel;
655
656         /* Transmitted only if joint subband coding enabled */
657         if (s->joint_intensity[j] > 0) {
658             int scale = 0;
659             source_channel = s->joint_intensity[j] - 1;
660
661             /* When huffman coded, only the difference is encoded
662              * (is this valid as well for joint scales ???) */
663
664             for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
665                 scale = get_scale(&s->gb, s->joint_huff[j], 0);
666                 scale += 64;    /* bias */
667                 s->joint_scale_factor[j][k] = scale;    /*joint_scale_table[scale]; */
668             }
669
670             if (!(s->debug_flag & 0x02)) {
671                 av_log(s->avctx, AV_LOG_DEBUG,
672                        "Joint stereo coding not supported\n");
673                 s->debug_flag |= 0x02;
674             }
675         }
676     }
677
678     /* Stereo downmix coefficients */
679     if (!base_channel && s->prim_channels > 2) {
680         if (s->downmix) {
681             for (j = base_channel; j < s->prim_channels; j++) {
682                 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
683                 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
684             }
685         } else {
686             int am = s->amode & DCA_CHANNEL_MASK;
687             for (j = base_channel; j < s->prim_channels; j++) {
688                 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
689                 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
690             }
691         }
692     }
693
694     /* Dynamic range coefficient */
695     if (!base_channel && s->dynrange)
696         s->dynrange_coef = get_bits(&s->gb, 8);
697
698     /* Side information CRC check word */
699     if (s->crc_present) {
700         get_bits(&s->gb, 16);
701     }
702
703     /*
704      * Primary audio data arrays
705      */
706
707     /* VQ encoded high frequency subbands */
708     for (j = base_channel; j < s->prim_channels; j++)
709         for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
710             /* 1 vector -> 32 samples */
711             s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
712
713     /* Low frequency effect data */
714     if (!base_channel && s->lfe) {
715         /* LFE samples */
716         int lfe_samples = 2 * s->lfe * (4 + block_index);
717         int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
718         float lfe_scale;
719
720         for (j = lfe_samples; j < lfe_end_sample; j++) {
721             /* Signed 8 bits int */
722             s->lfe_data[j] = get_sbits(&s->gb, 8);
723         }
724
725         /* Scale factor index */
726         s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
727
728         /* Quantization step size * scale factor */
729         lfe_scale = 0.035 * s->lfe_scale_factor;
730
731         for (j = lfe_samples; j < lfe_end_sample; j++)
732             s->lfe_data[j] *= lfe_scale;
733     }
734
735 #ifdef TRACE
736     av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes[s->current_subframe]);
737     av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
738            s->partial_samples[s->current_subframe]);
739     for (j = base_channel; j < s->prim_channels; j++) {
740         av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
741         for (k = 0; k < s->subband_activity[j]; k++)
742             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
743         av_log(s->avctx, AV_LOG_DEBUG, "\n");
744     }
745     for (j = base_channel; j < s->prim_channels; j++) {
746         for (k = 0; k < s->subband_activity[j]; k++)
747                 av_log(s->avctx, AV_LOG_DEBUG,
748                        "prediction coefs: %f, %f, %f, %f\n",
749                        (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
750                        (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
751                        (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
752                        (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
753     }
754     for (j = base_channel; j < s->prim_channels; j++) {
755         av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
756         for (k = 0; k < s->vq_start_subband[j]; k++)
757             av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
758         av_log(s->avctx, AV_LOG_DEBUG, "\n");
759     }
760     for (j = base_channel; j < s->prim_channels; j++) {
761         av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
762         for (k = 0; k < s->subband_activity[j]; k++)
763             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
764         av_log(s->avctx, AV_LOG_DEBUG, "\n");
765     }
766     for (j = base_channel; j < s->prim_channels; j++) {
767         av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
768         for (k = 0; k < s->subband_activity[j]; k++) {
769             if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
770                 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
771             if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
772                 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
773         }
774         av_log(s->avctx, AV_LOG_DEBUG, "\n");
775     }
776     for (j = base_channel; j < s->prim_channels; j++) {
777         if (s->joint_intensity[j] > 0) {
778             int source_channel = s->joint_intensity[j] - 1;
779             av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
780             for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
781                 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
782             av_log(s->avctx, AV_LOG_DEBUG, "\n");
783         }
784     }
785     if (!base_channel && s->prim_channels > 2 && s->downmix) {
786         av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
787         for (j = 0; j < s->prim_channels; j++) {
788             av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
789             av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
790         }
791         av_log(s->avctx, AV_LOG_DEBUG, "\n");
792     }
793     for (j = base_channel; j < s->prim_channels; j++)
794         for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
795             av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
796     if (!base_channel && s->lfe) {
797         int lfe_samples = 2 * s->lfe * (4 + block_index);
798         int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
799
800         av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
801         for (j = lfe_samples; j < lfe_end_sample; j++)
802             av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
803         av_log(s->avctx, AV_LOG_DEBUG, "\n");
804     }
805 #endif
806
807     return 0;
808 }
809
810 static void qmf_32_subbands(DCAContext * s, int chans,
811                             float samples_in[32][8], float *samples_out,
812                             float scale, float bias)
813 {
814     const float *prCoeff;
815     int i;
816
817     int sb_act = s->subband_activity[chans];
818     int subindex;
819
820     scale *= sqrt(1/8.0);
821
822     /* Select filter */
823     if (!s->multirate_inter)    /* Non-perfect reconstruction */
824         prCoeff = fir_32bands_nonperfect;
825     else                        /* Perfect reconstruction */
826         prCoeff = fir_32bands_perfect;
827
828     /* Reconstructed channel sample index */
829     for (subindex = 0; subindex < 8; subindex++) {
830         /* Load in one sample from each subband and clear inactive subbands */
831         for (i = 0; i < sb_act; i++){
832             uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ ((i-1)&2)<<30;
833             AV_WN32A(&s->raXin[i], v);
834         }
835         for (; i < 32; i++)
836             s->raXin[i] = 0.0;
837
838         s->synth.synth_filter_float(&s->imdct,
839                               s->subband_fir_hist[chans], &s->hist_index[chans],
840                               s->subband_fir_noidea[chans], prCoeff,
841                               samples_out, s->raXin, scale, bias);
842         samples_out+= 32;
843
844     }
845 }
846
847 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
848                                   int num_deci_sample, float *samples_in,
849                                   float *samples_out, float scale,
850                                   float bias)
851 {
852     /* samples_in: An array holding decimated samples.
853      *   Samples in current subframe starts from samples_in[0],
854      *   while samples_in[-1], samples_in[-2], ..., stores samples
855      *   from last subframe as history.
856      *
857      * samples_out: An array holding interpolated samples
858      */
859
860     int decifactor;
861     const float *prCoeff;
862     int deciindex;
863
864     /* Select decimation filter */
865     if (decimation_select == 1) {
866         decifactor = 64;
867         prCoeff = lfe_fir_128;
868     } else {
869         decifactor = 32;
870         prCoeff = lfe_fir_64;
871     }
872     /* Interpolation */
873     for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
874         s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor,
875                           scale, bias);
876         samples_in++;
877         samples_out += 2 * decifactor;
878     }
879 }
880
881 /* downmixing routines */
882 #define MIX_REAR1(samples, si1, rs, coef) \
883      samples[i]     += (samples[si1] - add_bias) * coef[rs][0];  \
884      samples[i+256] += (samples[si1] - add_bias) * coef[rs][1];
885
886 #define MIX_REAR2(samples, si1, si2, rs, coef) \
887      samples[i]     += (samples[si1] - add_bias) * coef[rs][0] + (samples[si2] - add_bias) * coef[rs+1][0]; \
888      samples[i+256] += (samples[si1] - add_bias) * coef[rs][1] + (samples[si2] - add_bias) * coef[rs+1][1];
889
890 #define MIX_FRONT3(samples, coef) \
891     t = samples[i+c] - add_bias; \
892     u = samples[i+l] - add_bias; \
893     v = samples[i+r] - add_bias; \
894     samples[i]     = t * coef[0][0] + u * coef[1][0] + v * coef[2][0] + add_bias; \
895     samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1] + add_bias;
896
897 #define DOWNMIX_TO_STEREO(op1, op2) \
898     for (i = 0; i < 256; i++){ \
899         op1 \
900         op2 \
901     }
902
903 static void dca_downmix(float *samples, int srcfmt,
904                         int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
905                         const int8_t *channel_mapping, float add_bias)
906 {
907     int c,l,r,sl,sr,s;
908     int i;
909     float t, u, v;
910     float coef[DCA_PRIM_CHANNELS_MAX][2];
911
912     for (i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
913         coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
914         coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
915     }
916
917     switch (srcfmt) {
918     case DCA_MONO:
919     case DCA_CHANNEL:
920     case DCA_STEREO_TOTAL:
921     case DCA_STEREO_SUMDIFF:
922     case DCA_4F2R:
923         av_log(NULL, 0, "Not implemented!\n");
924         break;
925     case DCA_STEREO:
926         break;
927     case DCA_3F:
928         c = channel_mapping[0] * 256;
929         l = channel_mapping[1] * 256;
930         r = channel_mapping[2] * 256;
931         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
932         break;
933     case DCA_2F1R:
934         s = channel_mapping[2] * 256;
935         DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef),);
936         break;
937     case DCA_3F1R:
938         c = channel_mapping[0] * 256;
939         l = channel_mapping[1] * 256;
940         r = channel_mapping[2] * 256;
941         s = channel_mapping[3] * 256;
942         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
943                           MIX_REAR1(samples, i + s, 3, coef));
944         break;
945     case DCA_2F2R:
946         sl = channel_mapping[2] * 256;
947         sr = channel_mapping[3] * 256;
948         DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef),);
949         break;
950     case DCA_3F2R:
951         c =  channel_mapping[0] * 256;
952         l =  channel_mapping[1] * 256;
953         r =  channel_mapping[2] * 256;
954         sl = channel_mapping[3] * 256;
955         sr = channel_mapping[4] * 256;
956         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
957                           MIX_REAR2(samples, i + sl, i + sr, 3, coef));
958         break;
959     }
960 }
961
962
963 /* Very compact version of the block code decoder that does not use table
964  * look-up but is slightly slower */
965 static int decode_blockcode(int code, int levels, int *values)
966 {
967     int i;
968     int offset = (levels - 1) >> 1;
969
970     for (i = 0; i < 4; i++) {
971         int div = FASTDIV(code, levels);
972         values[i] = code - offset - div*levels;
973         code = div;
974     }
975
976     if (code == 0)
977         return 0;
978     else {
979         av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
980         return -1;
981     }
982 }
983
984 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
985 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
986
987 static int dca_subsubframe(DCAContext * s, int base_channel, int block_index)
988 {
989     int k, l;
990     int subsubframe = s->current_subsubframe;
991
992     const float *quant_step_table;
993
994     /* FIXME */
995     float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
996     LOCAL_ALIGNED_16(int, block, [8]);
997
998     /*
999      * Audio data
1000      */
1001
1002     /* Select quantization step size table */
1003     if (s->bit_rate_index == 0x1f)
1004         quant_step_table = lossless_quant_d;
1005     else
1006         quant_step_table = lossy_quant_d;
1007
1008     for (k = base_channel; k < s->prim_channels; k++) {
1009         for (l = 0; l < s->vq_start_subband[k]; l++) {
1010             int m;
1011
1012             /* Select the mid-tread linear quantizer */
1013             int abits = s->bitalloc[k][l];
1014
1015             float quant_step_size = quant_step_table[abits];
1016
1017             /*
1018              * Determine quantization index code book and its type
1019              */
1020
1021             /* Select quantization index code book */
1022             int sel = s->quant_index_huffman[k][abits];
1023
1024             /*
1025              * Extract bits from the bit stream
1026              */
1027             if (!abits){
1028                 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
1029             } else {
1030                 /* Deal with transients */
1031                 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1032                 float rscale = quant_step_size * s->scale_factor[k][l][sfi] * s->scalefactor_adj[k][sel];
1033
1034                 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
1035                     if (abits <= 7){
1036                         /* Block code */
1037                         int block_code1, block_code2, size, levels;
1038
1039                         size = abits_sizes[abits-1];
1040                         levels = abits_levels[abits-1];
1041
1042                         block_code1 = get_bits(&s->gb, size);
1043                         /* FIXME Should test return value */
1044                         decode_blockcode(block_code1, levels, block);
1045                         block_code2 = get_bits(&s->gb, size);
1046                         decode_blockcode(block_code2, levels, &block[4]);
1047                     }else{
1048                         /* no coding */
1049                         for (m = 0; m < 8; m++)
1050                             block[m] = get_sbits(&s->gb, abits - 3);
1051                     }
1052                 }else{
1053                     /* Huffman coded */
1054                     for (m = 0; m < 8; m++)
1055                         block[m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
1056                 }
1057
1058                 s->dsp.int32_to_float_fmul_scalar(subband_samples[k][l],
1059                                                   block, rscale, 8);
1060             }
1061
1062             /*
1063              * Inverse ADPCM if in prediction mode
1064              */
1065             if (s->prediction_mode[k][l]) {
1066                 int n;
1067                 for (m = 0; m < 8; m++) {
1068                     for (n = 1; n <= 4; n++)
1069                         if (m >= n)
1070                             subband_samples[k][l][m] +=
1071                                 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1072                                  subband_samples[k][l][m - n] / 8192);
1073                         else if (s->predictor_history)
1074                             subband_samples[k][l][m] +=
1075                                 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1076                                  s->subband_samples_hist[k][l][m - n +
1077                                                                4] / 8192);
1078                 }
1079             }
1080         }
1081
1082         /*
1083          * Decode VQ encoded high frequencies
1084          */
1085         for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1086             /* 1 vector -> 32 samples but we only need the 8 samples
1087              * for this subsubframe. */
1088             int m;
1089
1090             if (!s->debug_flag & 0x01) {
1091                 av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
1092                 s->debug_flag |= 0x01;
1093             }
1094
1095             for (m = 0; m < 8; m++) {
1096                 subband_samples[k][l][m] =
1097                     high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
1098                                                         m]
1099                     * (float) s->scale_factor[k][l][0] / 16.0;
1100             }
1101         }
1102     }
1103
1104     /* Check for DSYNC after subsubframe */
1105     if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1106         if (0xFFFF == get_bits(&s->gb, 16)) {   /* 0xFFFF */
1107 #ifdef TRACE
1108             av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1109 #endif
1110         } else {
1111             av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1112         }
1113     }
1114
1115     /* Backup predictor history for adpcm */
1116     for (k = base_channel; k < s->prim_channels; k++)
1117         for (l = 0; l < s->vq_start_subband[k]; l++)
1118             memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
1119                         4 * sizeof(subband_samples[0][0][0]));
1120
1121     return 0;
1122 }
1123
1124 static int dca_filter_channels(DCAContext * s, int block_index)
1125 {
1126     float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1127     int k;
1128
1129     /* 32 subbands QMF */
1130     for (k = 0; k < s->prim_channels; k++) {
1131 /*        static float pcm_to_double[8] =
1132             {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
1133          qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
1134                             M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ ,
1135                             s->add_bias );
1136     }
1137
1138     /* Down mixing */
1139     if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
1140         dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab, s->add_bias);
1141     }
1142
1143     /* Generate LFE samples for this subsubframe FIXME!!! */
1144     if (s->output & DCA_LFE) {
1145         lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1146                               s->lfe_data + 2 * s->lfe * (block_index + 4),
1147                               &s->samples[256 * dca_lfe_index[s->amode]],
1148                               (1.0/256.0)*s->scale_bias,  s->add_bias);
1149         /* Outputs 20bits pcm samples */
1150     }
1151
1152     return 0;
1153 }
1154
1155
1156 static int dca_subframe_footer(DCAContext * s, int base_channel)
1157 {
1158     int aux_data_count = 0, i;
1159
1160     /*
1161      * Unpack optional information
1162      */
1163
1164     /* presumably optional information only appears in the core? */
1165     if (!base_channel) {
1166         if (s->timestamp)
1167             get_bits(&s->gb, 32);
1168
1169         if (s->aux_data)
1170             aux_data_count = get_bits(&s->gb, 6);
1171
1172         for (i = 0; i < aux_data_count; i++)
1173             get_bits(&s->gb, 8);
1174
1175         if (s->crc_present && (s->downmix || s->dynrange))
1176             get_bits(&s->gb, 16);
1177     }
1178
1179     return 0;
1180 }
1181
1182 /**
1183  * Decode a dca frame block
1184  *
1185  * @param s     pointer to the DCAContext
1186  */
1187
1188 static int dca_decode_block(DCAContext * s, int base_channel, int block_index)
1189 {
1190
1191     /* Sanity check */
1192     if (s->current_subframe >= s->subframes) {
1193         av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1194                s->current_subframe, s->subframes);
1195         return -1;
1196     }
1197
1198     if (!s->current_subsubframe) {
1199 #ifdef TRACE
1200         av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1201 #endif
1202         /* Read subframe header */
1203         if (dca_subframe_header(s, base_channel, block_index))
1204             return -1;
1205     }
1206
1207     /* Read subsubframe */
1208 #ifdef TRACE
1209     av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1210 #endif
1211     if (dca_subsubframe(s, base_channel, block_index))
1212         return -1;
1213
1214     /* Update state */
1215     s->current_subsubframe++;
1216     if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1217         s->current_subsubframe = 0;
1218         s->current_subframe++;
1219     }
1220     if (s->current_subframe >= s->subframes) {
1221 #ifdef TRACE
1222         av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1223 #endif
1224         /* Read subframe footer */
1225         if (dca_subframe_footer(s, base_channel))
1226             return -1;
1227     }
1228
1229     return 0;
1230 }
1231
1232 /**
1233  * Convert bitstream to one representation based on sync marker
1234  */
1235 static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
1236                           int max_size)
1237 {
1238     uint32_t mrk;
1239     int i, tmp;
1240     const uint16_t *ssrc = (const uint16_t *) src;
1241     uint16_t *sdst = (uint16_t *) dst;
1242     PutBitContext pb;
1243
1244     if ((unsigned)src_size > (unsigned)max_size) {
1245 //        av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
1246 //        return -1;
1247         src_size = max_size;
1248     }
1249
1250     mrk = AV_RB32(src);
1251     switch (mrk) {
1252     case DCA_MARKER_RAW_BE:
1253         memcpy(dst, src, src_size);
1254         return src_size;
1255     case DCA_MARKER_RAW_LE:
1256         for (i = 0; i < (src_size + 1) >> 1; i++)
1257             *sdst++ = av_bswap16(*ssrc++);
1258         return src_size;
1259     case DCA_MARKER_14B_BE:
1260     case DCA_MARKER_14B_LE:
1261         init_put_bits(&pb, dst, max_size);
1262         for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
1263             tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
1264             put_bits(&pb, 14, tmp);
1265         }
1266         flush_put_bits(&pb);
1267         return (put_bits_count(&pb) + 7) >> 3;
1268     default:
1269         return -1;
1270     }
1271 }
1272
1273 /**
1274  * Main frame decoding function
1275  * FIXME add arguments
1276  */
1277 static int dca_decode_frame(AVCodecContext * avctx,
1278                             void *data, int *data_size,
1279                             AVPacket *avpkt)
1280 {
1281     const uint8_t *buf = avpkt->data;
1282     int buf_size = avpkt->size;
1283
1284     int lfe_samples;
1285     int num_core_channels = 0;
1286     int i;
1287     int16_t *samples = data;
1288     DCAContext *s = avctx->priv_data;
1289     int channels;
1290
1291
1292     s->xch_present = 0;
1293     s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE);
1294     if (s->dca_buffer_size == -1) {
1295         av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1296         return -1;
1297     }
1298
1299     init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
1300     if (dca_parse_frame_header(s) < 0) {
1301         //seems like the frame is corrupt, try with the next one
1302         *data_size=0;
1303         return buf_size;
1304     }
1305     //set AVCodec values with parsed data
1306     avctx->sample_rate = s->sample_rate;
1307     avctx->bit_rate = s->bit_rate;
1308
1309     for (i = 0; i < (s->sample_blocks / 8); i++) {
1310         dca_decode_block(s, 0, i);
1311     }
1312
1313     /* record number of core channels incase less than max channels are requested */
1314     num_core_channels = s->prim_channels;
1315
1316     /* extensions start at 32-bit boundaries into bitstream */
1317     skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1318
1319     while(get_bits_left(&s->gb) >= 32) {
1320         uint32_t bits = get_bits_long(&s->gb, 32);
1321
1322         switch(bits) {
1323         case 0x5a5a5a5a: {
1324             int ext_amode, xch_fsize;
1325
1326             s->xch_base_channel = s->prim_channels;
1327
1328             /* validate sync word using XCHFSIZE field */
1329             xch_fsize = show_bits(&s->gb, 10);
1330             if((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1331                (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1332                 continue;
1333
1334             /* skip length-to-end-of-frame field for the moment */
1335             skip_bits(&s->gb, 10);
1336
1337             /* extension amode should == 1, number of channels in extension */
1338             /* AFAIK XCh is not used for more channels */
1339             if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1340                 av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
1341                        " supported!\n",ext_amode);
1342                 continue;
1343             }
1344
1345             /* much like core primary audio coding header */
1346             dca_parse_audio_coding_header(s, s->xch_base_channel);
1347
1348             for (i = 0; i < (s->sample_blocks / 8); i++) {
1349                 dca_decode_block(s, s->xch_base_channel, i);
1350             }
1351
1352             s->xch_present = 1;
1353             break;
1354         }
1355         case 0x1d95f262:
1356             av_log(avctx, AV_LOG_DEBUG, "Possible X96 extension found at %d bits\n", get_bits_count(&s->gb));
1357             av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", get_bits(&s->gb, 12)+1);
1358             av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1359             break;
1360         }
1361
1362         skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1363     }
1364
1365     channels = s->prim_channels + !!s->lfe;
1366
1367     if (s->amode<16) {
1368         avctx->channel_layout = dca_core_channel_layout[s->amode];
1369
1370         if (s->xch_present && (!avctx->request_channels ||
1371                                avctx->request_channels > num_core_channels + !!s->lfe)) {
1372             avctx->channel_layout |= AV_CH_BACK_CENTER;
1373             if (s->lfe) {
1374                 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1375                 s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
1376             } else {
1377                 s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
1378             }
1379         } else {
1380             channels = num_core_channels + !!s->lfe;
1381             s->xch_present = 0; /* disable further xch processing */
1382             if (s->lfe) {
1383                 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1384                 s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
1385             } else
1386                 s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
1387         }
1388
1389         if (channels > !!s->lfe &&
1390             s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1391             return -1;
1392
1393         if (avctx->request_channels == 2 && s->prim_channels > 2) {
1394             channels = 2;
1395             s->output = DCA_STEREO;
1396             avctx->channel_layout = AV_CH_LAYOUT_STEREO;
1397         }
1398     } else {
1399         av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
1400         return -1;
1401     }
1402
1403
1404     /* There is nothing that prevents a dts frame to change channel configuration
1405        but FFmpeg doesn't support that so only set the channels if it is previously
1406        unset. Ideally during the first probe for channels the crc should be checked
1407        and only set avctx->channels when the crc is ok. Right now the decoder could
1408        set the channels based on a broken first frame.*/
1409     avctx->channels = channels;
1410
1411     if (*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels)
1412         return -1;
1413     *data_size = 256 / 8 * s->sample_blocks * sizeof(int16_t) * channels;
1414
1415     /* filter to get final output */
1416     for (i = 0; i < (s->sample_blocks / 8); i++) {
1417         dca_filter_channels(s, i);
1418
1419         /* If this was marked as a DTS-ES stream we need to subtract back- */
1420         /* channel from SL & SR to remove matrixed back-channel signal */
1421         if((s->source_pcm_res & 1) && s->xch_present) {
1422             float* back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256;
1423             float* lt_chan   = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;
1424             float* rt_chan   = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;
1425             int j;
1426             for(j = 0; j < 256; ++j) {
1427                 lt_chan[j] -= (back_chan[j] - s->add_bias) * M_SQRT1_2;
1428                 rt_chan[j] -= (back_chan[j] - s->add_bias) * M_SQRT1_2;
1429             }
1430         }
1431
1432         s->dsp.float_to_int16_interleave(samples, s->samples_chanptr, 256, channels);
1433         samples += 256 * channels;
1434     }
1435
1436     /* update lfe history */
1437     lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
1438     for (i = 0; i < 2 * s->lfe * 4; i++) {
1439         s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1440     }
1441
1442     return buf_size;
1443 }
1444
1445
1446
1447 /**
1448  * DCA initialization
1449  *
1450  * @param avctx     pointer to the AVCodecContext
1451  */
1452
1453 static av_cold int dca_decode_init(AVCodecContext * avctx)
1454 {
1455     DCAContext *s = avctx->priv_data;
1456     int i;
1457
1458     s->avctx = avctx;
1459     dca_init_vlcs();
1460
1461     dsputil_init(&s->dsp, avctx);
1462     ff_mdct_init(&s->imdct, 6, 1, 1.0);
1463     ff_synth_filter_init(&s->synth);
1464     ff_dcadsp_init(&s->dcadsp);
1465
1466     for (i = 0; i < DCA_PRIM_CHANNELS_MAX+1; i++)
1467         s->samples_chanptr[i] = s->samples + i * 256;
1468     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1469
1470     if (s->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
1471         s->add_bias = 385.0f;
1472         s->scale_bias = 1.0 / 32768.0;
1473     } else {
1474         s->add_bias = 0.0f;
1475         s->scale_bias = 1.0;
1476
1477         /* allow downmixing to stereo */
1478         if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
1479                 avctx->request_channels == 2) {
1480             avctx->channels = avctx->request_channels;
1481         }
1482     }
1483
1484
1485     return 0;
1486 }
1487
1488 static av_cold int dca_decode_end(AVCodecContext * avctx)
1489 {
1490     DCAContext *s = avctx->priv_data;
1491     ff_mdct_end(&s->imdct);
1492     return 0;
1493 }
1494
1495 AVCodec dca_decoder = {
1496     .name = "dca",
1497     .type = AVMEDIA_TYPE_AUDIO,
1498     .id = CODEC_ID_DTS,
1499     .priv_data_size = sizeof(DCAContext),
1500     .init = dca_decode_init,
1501     .decode = dca_decode_frame,
1502     .close = dca_decode_end,
1503     .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
1504     .capabilities = CODEC_CAP_CHANNEL_CONF,
1505 };