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