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