dca.c

Go to the documentation of this file.
00001 /*
00002  * DCA compatible decoder
00003  * Copyright (C) 2004 Gildas Bazin
00004  * Copyright (C) 2004 Benjamin Zores
00005  * Copyright (C) 2006 Benjamin Larsson
00006  * Copyright (C) 2007 Konstantin Shishkov
00007  *
00008  * This file is part of FFmpeg.
00009  *
00010  * FFmpeg is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * FFmpeg is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with FFmpeg; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00025 /**
00026  * @file dca.c
00027  */
00028 
00029 #include <math.h>
00030 #include <stddef.h>
00031 #include <stdio.h>
00032 
00033 #include "avcodec.h"
00034 #include "dsputil.h"
00035 #include "bitstream.h"
00036 #include "dcadata.h"
00037 #include "dcahuff.h"
00038 #include "dca.h"
00039 
00040 //#define TRACE
00041 
00042 #define DCA_PRIM_CHANNELS_MAX (5)
00043 #define DCA_SUBBANDS (32)
00044 #define DCA_ABITS_MAX (32)      /* Should be 28 */
00045 #define DCA_SUBSUBFAMES_MAX (4)
00046 #define DCA_LFE_MAX (3)
00047 
00048 enum DCAMode {
00049     DCA_MONO = 0,
00050     DCA_CHANNEL,
00051     DCA_STEREO,
00052     DCA_STEREO_SUMDIFF,
00053     DCA_STEREO_TOTAL,
00054     DCA_3F,
00055     DCA_2F1R,
00056     DCA_3F1R,
00057     DCA_2F2R,
00058     DCA_3F2R,
00059     DCA_4F2R
00060 };
00061 
00062 #define DCA_DOLBY 101           /* FIXME */
00063 
00064 #define DCA_CHANNEL_BITS 6
00065 #define DCA_CHANNEL_MASK 0x3F
00066 
00067 #define DCA_LFE 0x80
00068 
00069 #define HEADER_SIZE 14
00070 #define CONVERT_BIAS 384
00071 
00072 #define DCA_MAX_FRAME_SIZE 16383
00073 
00074 /** Bit allocation */
00075 typedef struct {
00076     int offset;                 ///< code values offset
00077     int maxbits[8];             ///< max bits in VLC
00078     int wrap;                   ///< wrap for get_vlc2()
00079     VLC vlc[8];                 ///< actual codes
00080 } BitAlloc;
00081 
00082 static BitAlloc dca_bitalloc_index;    ///< indexes for samples VLC select
00083 static BitAlloc dca_tmode;             ///< transition mode VLCs
00084 static BitAlloc dca_scalefactor;       ///< scalefactor VLCs
00085 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
00086 
00087 /** Pre-calculated cosine modulation coefs for the QMF */
00088 static float cos_mod[544];
00089 
00090 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
00091 {
00092     return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
00093 }
00094 
00095 typedef struct {
00096     AVCodecContext *avctx;
00097     /* Frame header */
00098     int frame_type;             ///< type of the current frame
00099     int samples_deficit;        ///< deficit sample count
00100     int crc_present;            ///< crc is present in the bitstream
00101     int sample_blocks;          ///< number of PCM sample blocks
00102     int frame_size;             ///< primary frame byte size
00103     int amode;                  ///< audio channels arrangement
00104     int sample_rate;            ///< audio sampling rate
00105     int bit_rate;               ///< transmission bit rate
00106 
00107     int downmix;                ///< embedded downmix enabled
00108     int dynrange;               ///< embedded dynamic range flag
00109     int timestamp;              ///< embedded time stamp flag
00110     int aux_data;               ///< auxiliary data flag
00111     int hdcd;                   ///< source material is mastered in HDCD
00112     int ext_descr;              ///< extension audio descriptor flag
00113     int ext_coding;             ///< extended coding flag
00114     int aspf;                   ///< audio sync word insertion flag
00115     int lfe;                    ///< low frequency effects flag
00116     int predictor_history;      ///< predictor history flag
00117     int header_crc;             ///< header crc check bytes
00118     int multirate_inter;        ///< multirate interpolator switch
00119     int version;                ///< encoder software revision
00120     int copy_history;           ///< copy history
00121     int source_pcm_res;         ///< source pcm resolution
00122     int front_sum;              ///< front sum/difference flag
00123     int surround_sum;           ///< surround sum/difference flag
00124     int dialog_norm;            ///< dialog normalisation parameter
00125 
00126     /* Primary audio coding header */
00127     int subframes;              ///< number of subframes
00128     int total_channels;         ///< number of channels including extensions
00129     int prim_channels;          ///< number of primary audio channels
00130     int subband_activity[DCA_PRIM_CHANNELS_MAX];    ///< subband activity count
00131     int vq_start_subband[DCA_PRIM_CHANNELS_MAX];    ///< high frequency vq start subband
00132     int joint_intensity[DCA_PRIM_CHANNELS_MAX];     ///< joint intensity coding index
00133     int transient_huffman[DCA_PRIM_CHANNELS_MAX];   ///< transient mode code book
00134     int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
00135     int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX];    ///< bit allocation quantizer select
00136     int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
00137     float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];   ///< scale factor adjustment
00138 
00139     /* Primary audio coding side information */
00140     int subsubframes;           ///< number of subsubframes
00141     int partial_samples;        ///< partial subsubframe samples count
00142     int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< prediction mode (ADPCM used or not)
00143     int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];      ///< prediction VQ coefs
00144     int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];           ///< bit allocation index
00145     int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< transition mode (transients)
00146     int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];    ///< scale factors (2 if transient)
00147     int joint_huff[DCA_PRIM_CHANNELS_MAX];                       ///< joint subband scale factors codebook
00148     int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
00149     int downmix_coef[DCA_PRIM_CHANNELS_MAX][2];                  ///< stereo downmix coefficients
00150     int dynrange_coef;                                           ///< dynamic range coefficient
00151 
00152     int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];       ///< VQ encoded high frequency subbands
00153 
00154     float lfe_data[2 * DCA_SUBSUBFAMES_MAX * DCA_LFE_MAX *
00155                    2 /*history */ ];    ///< Low frequency effect data
00156     int lfe_scale_factor;
00157 
00158     /* Subband samples history (for ADPCM) */
00159     float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
00160     float subband_fir_hist[DCA_PRIM_CHANNELS_MAX][512];
00161     float subband_fir_noidea[DCA_PRIM_CHANNELS_MAX][64];
00162 
00163     int output;                 ///< type of output
00164     int bias;                   ///< output bias
00165 
00166     DECLARE_ALIGNED_16(float, samples[1536]);  /* 6 * 256 = 1536, might only need 5 */
00167     DECLARE_ALIGNED_16(int16_t, tsamples[1536]);
00168 
00169     uint8_t dca_buffer[DCA_MAX_FRAME_SIZE];
00170     int dca_buffer_size;        ///< how much data is in the dca_buffer
00171 
00172     GetBitContext gb;
00173     /* Current position in DCA frame */
00174     int current_subframe;
00175     int current_subsubframe;
00176 
00177     int debug_flag;             ///< used for suppressing repeated error messages output
00178     DSPContext dsp;
00179 } DCAContext;
00180 
00181 static av_cold void dca_init_vlcs(void)
00182 {
00183     static int vlcs_initialized = 0;
00184     int i, j;
00185 
00186     if (vlcs_initialized)
00187         return;
00188 
00189     dca_bitalloc_index.offset = 1;
00190     dca_bitalloc_index.wrap = 2;
00191     for (i = 0; i < 5; i++)
00192         init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
00193                  bitalloc_12_bits[i], 1, 1,
00194                  bitalloc_12_codes[i], 2, 2, 1);
00195     dca_scalefactor.offset = -64;
00196     dca_scalefactor.wrap = 2;
00197     for (i = 0; i < 5; i++)
00198         init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
00199                  scales_bits[i], 1, 1,
00200                  scales_codes[i], 2, 2, 1);
00201     dca_tmode.offset = 0;
00202     dca_tmode.wrap = 1;
00203     for (i = 0; i < 4; i++)
00204         init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
00205                  tmode_bits[i], 1, 1,
00206                  tmode_codes[i], 2, 2, 1);
00207 
00208     for(i = 0; i < 10; i++)
00209         for(j = 0; j < 7; j++){
00210             if(!bitalloc_codes[i][j]) break;
00211             dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
00212             dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
00213             init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
00214                      bitalloc_sizes[i],
00215                      bitalloc_bits[i][j], 1, 1,
00216                      bitalloc_codes[i][j], 2, 2, 1);
00217         }
00218     vlcs_initialized = 1;
00219 }
00220 
00221 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
00222 {
00223     while(len--)
00224         *dst++ = get_bits(gb, bits);
00225 }
00226 
00227 static int dca_parse_frame_header(DCAContext * s)
00228 {
00229     int i, j;
00230     static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
00231     static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
00232     static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
00233 
00234     s->bias = CONVERT_BIAS;
00235 
00236     init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
00237 
00238     /* Sync code */
00239     get_bits(&s->gb, 32);
00240 
00241     /* Frame header */
00242     s->frame_type        = get_bits(&s->gb, 1);
00243     s->samples_deficit   = get_bits(&s->gb, 5) + 1;
00244     s->crc_present       = get_bits(&s->gb, 1);
00245     s->sample_blocks     = get_bits(&s->gb, 7) + 1;
00246     s->frame_size        = get_bits(&s->gb, 14) + 1;
00247     if (s->frame_size < 95)
00248         return -1;
00249     s->amode             = get_bits(&s->gb, 6);
00250     s->sample_rate       = dca_sample_rates[get_bits(&s->gb, 4)];
00251     if (!s->sample_rate)
00252         return -1;
00253     s->bit_rate          = dca_bit_rates[get_bits(&s->gb, 5)];
00254     if (!s->bit_rate)
00255         return -1;
00256 
00257     s->downmix           = get_bits(&s->gb, 1);
00258     s->dynrange          = get_bits(&s->gb, 1);
00259     s->timestamp         = get_bits(&s->gb, 1);
00260     s->aux_data          = get_bits(&s->gb, 1);
00261     s->hdcd              = get_bits(&s->gb, 1);
00262     s->ext_descr         = get_bits(&s->gb, 3);
00263     s->ext_coding        = get_bits(&s->gb, 1);
00264     s->aspf              = get_bits(&s->gb, 1);
00265     s->lfe               = get_bits(&s->gb, 2);
00266     s->predictor_history = get_bits(&s->gb, 1);
00267 
00268     /* TODO: check CRC */
00269     if (s->crc_present)
00270         s->header_crc    = get_bits(&s->gb, 16);
00271 
00272     s->multirate_inter   = get_bits(&s->gb, 1);
00273     s->version           = get_bits(&s->gb, 4);
00274     s->copy_history      = get_bits(&s->gb, 2);
00275     s->source_pcm_res    = get_bits(&s->gb, 3);
00276     s->front_sum         = get_bits(&s->gb, 1);
00277     s->surround_sum      = get_bits(&s->gb, 1);
00278     s->dialog_norm       = get_bits(&s->gb, 4);
00279 
00280     /* FIXME: channels mixing levels */
00281     s->output = s->amode;
00282     if(s->lfe) s->output |= DCA_LFE;
00283 
00284 #ifdef TRACE
00285     av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
00286     av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
00287     av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
00288     av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
00289            s->sample_blocks, s->sample_blocks * 32);
00290     av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
00291     av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
00292            s->amode, dca_channels[s->amode]);
00293     av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i (%i Hz)\n",
00294            s->sample_rate, dca_sample_rates[s->sample_rate]);
00295     av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i (%i bits/s)\n",
00296            s->bit_rate, dca_bit_rates[s->bit_rate]);
00297     av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
00298     av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
00299     av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
00300     av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
00301     av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
00302     av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
00303     av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
00304     av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
00305     av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
00306     av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
00307            s->predictor_history);
00308     av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
00309     av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
00310            s->multirate_inter);
00311     av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
00312     av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
00313     av_log(s->avctx, AV_LOG_DEBUG,
00314            "source pcm resolution: %i (%i bits/sample)\n",
00315            s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
00316     av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
00317     av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
00318     av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
00319     av_log(s->avctx, AV_LOG_DEBUG, "\n");
00320 #endif
00321 
00322     /* Primary audio coding header */
00323     s->subframes         = get_bits(&s->gb, 4) + 1;
00324     s->total_channels    = get_bits(&s->gb, 3) + 1;
00325     s->prim_channels     = s->total_channels;
00326     if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
00327         s->prim_channels = DCA_PRIM_CHANNELS_MAX;   /* We only support DTS core */
00328 
00329 
00330     for (i = 0; i < s->prim_channels; i++) {
00331         s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
00332         if (s->subband_activity[i] > DCA_SUBBANDS)
00333             s->subband_activity[i] = DCA_SUBBANDS;
00334     }
00335     for (i = 0; i < s->prim_channels; i++) {
00336         s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
00337         if (s->vq_start_subband[i] > DCA_SUBBANDS)
00338             s->vq_start_subband[i] = DCA_SUBBANDS;
00339     }
00340     get_array(&s->gb, s->joint_intensity,     s->prim_channels, 3);
00341     get_array(&s->gb, s->transient_huffman,   s->prim_channels, 2);
00342     get_array(&s->gb, s->scalefactor_huffman, s->prim_channels, 3);
00343     get_array(&s->gb, s->bitalloc_huffman,    s->prim_channels, 3);
00344 
00345     /* Get codebooks quantization indexes */
00346     memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
00347     for (j = 1; j < 11; j++)
00348         for (i = 0; i < s->prim_channels; i++)
00349             s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
00350 
00351     /* Get scale factor adjustment */
00352     for (j = 0; j < 11; j++)
00353         for (i = 0; i < s->prim_channels; i++)
00354             s->scalefactor_adj[i][j] = 1;
00355 
00356     for (j = 1; j < 11; j++)
00357         for (i = 0; i < s->prim_channels; i++)
00358             if (s->quant_index_huffman[i][j] < thr[j])
00359                 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
00360 
00361     if (s->crc_present) {
00362         /* Audio header CRC check */
00363         get_bits(&s->gb, 16);
00364     }
00365 
00366     s->current_subframe = 0;
00367     s->current_subsubframe = 0;
00368 
00369 #ifdef TRACE
00370     av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
00371     av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
00372     for(i = 0; i < s->prim_channels; i++){
00373         av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
00374         av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
00375         av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
00376         av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
00377         av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
00378         av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
00379         av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
00380         for (j = 0; j < 11; j++)
00381             av_log(s->avctx, AV_LOG_DEBUG, " %i",
00382                    s->quant_index_huffman[i][j]);
00383         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00384         av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
00385         for (j = 0; j < 11; j++)
00386             av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
00387         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00388     }
00389 #endif
00390 
00391     return 0;
00392 }
00393 
00394 
00395 static inline int get_scale(GetBitContext *gb, int level, int value)
00396 {
00397    if (level < 5) {
00398        /* huffman encoded */
00399        value += get_bitalloc(gb, &dca_scalefactor, level);
00400    } else if(level < 8)
00401        value = get_bits(gb, level + 1);
00402    return value;
00403 }
00404 
00405 static int dca_subframe_header(DCAContext * s)
00406 {
00407     /* Primary audio coding side information */
00408     int j, k;
00409 
00410     s->subsubframes = get_bits(&s->gb, 2) + 1;
00411     s->partial_samples = get_bits(&s->gb, 3);
00412     for (j = 0; j < s->prim_channels; j++) {
00413         for (k = 0; k < s->subband_activity[j]; k++)
00414             s->prediction_mode[j][k] = get_bits(&s->gb, 1);
00415     }
00416 
00417     /* Get prediction codebook */
00418     for (j = 0; j < s->prim_channels; j++) {
00419         for (k = 0; k < s->subband_activity[j]; k++) {
00420             if (s->prediction_mode[j][k] > 0) {
00421                 /* (Prediction coefficient VQ address) */
00422                 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
00423             }
00424         }
00425     }
00426 
00427     /* Bit allocation index */
00428     for (j = 0; j < s->prim_channels; j++) {
00429         for (k = 0; k < s->vq_start_subband[j]; k++) {
00430             if (s->bitalloc_huffman[j] == 6)
00431                 s->bitalloc[j][k] = get_bits(&s->gb, 5);
00432             else if (s->bitalloc_huffman[j] == 5)
00433                 s->bitalloc[j][k] = get_bits(&s->gb, 4);
00434             else if (s->bitalloc_huffman[j] == 7) {
00435                 av_log(s->avctx, AV_LOG_ERROR,
00436                        "Invalid bit allocation index\n");
00437                 return -1;
00438             } else {
00439                 s->bitalloc[j][k] =
00440                     get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
00441             }
00442 
00443             if (s->bitalloc[j][k] > 26) {
00444 //                 av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
00445 //                          j, k, s->bitalloc[j][k]);
00446                 return -1;
00447             }
00448         }
00449     }
00450 
00451     /* Transition mode */
00452     for (j = 0; j < s->prim_channels; j++) {
00453         for (k = 0; k < s->subband_activity[j]; k++) {
00454             s->transition_mode[j][k] = 0;
00455             if (s->subsubframes > 1 &&
00456                 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
00457                 s->transition_mode[j][k] =
00458                     get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
00459             }
00460         }
00461     }
00462 
00463     for (j = 0; j < s->prim_channels; j++) {
00464         const uint32_t *scale_table;
00465         int scale_sum;
00466 
00467         memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
00468 
00469         if (s->scalefactor_huffman[j] == 6)
00470             scale_table = scale_factor_quant7;
00471         else
00472             scale_table = scale_factor_quant6;
00473 
00474         /* When huffman coded, only the difference is encoded */
00475         scale_sum = 0;
00476 
00477         for (k = 0; k < s->subband_activity[j]; k++) {
00478             if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
00479                 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
00480                 s->scale_factor[j][k][0] = scale_table[scale_sum];
00481             }
00482 
00483             if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
00484                 /* Get second scale factor */
00485                 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
00486                 s->scale_factor[j][k][1] = scale_table[scale_sum];
00487             }
00488         }
00489     }
00490 
00491     /* Joint subband scale factor codebook select */
00492     for (j = 0; j < s->prim_channels; j++) {
00493         /* Transmitted only if joint subband coding enabled */
00494         if (s->joint_intensity[j] > 0)
00495             s->joint_huff[j] = get_bits(&s->gb, 3);
00496     }
00497 
00498     /* Scale factors for joint subband coding */
00499     for (j = 0; j < s->prim_channels; j++) {
00500         int source_channel;
00501 
00502         /* Transmitted only if joint subband coding enabled */
00503         if (s->joint_intensity[j] > 0) {
00504             int scale = 0;
00505             source_channel = s->joint_intensity[j] - 1;
00506 
00507             /* When huffman coded, only the difference is encoded
00508              * (is this valid as well for joint scales ???) */
00509 
00510             for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
00511                 scale = get_scale(&s->gb, s->joint_huff[j], 0);
00512                 scale += 64;    /* bias */
00513                 s->joint_scale_factor[j][k] = scale;    /*joint_scale_table[scale]; */
00514             }
00515 
00516             if (!s->debug_flag & 0x02) {
00517                 av_log(s->avctx, AV_LOG_DEBUG,
00518                        "Joint stereo coding not supported\n");
00519                 s->debug_flag |= 0x02;
00520             }
00521         }
00522     }
00523 
00524     /* Stereo downmix coefficients */
00525     if (s->prim_channels > 2) {
00526         if(s->downmix) {
00527             for (j = 0; j < s->prim_channels; j++) {
00528                 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
00529                 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
00530             }
00531         } else {
00532             int am = s->amode & DCA_CHANNEL_MASK;
00533             for (j = 0; j < s->prim_channels; j++) {
00534                 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
00535                 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
00536             }
00537         }
00538     }
00539 
00540     /* Dynamic range coefficient */
00541     if (s->dynrange)
00542         s->dynrange_coef = get_bits(&s->gb, 8);
00543 
00544     /* Side information CRC check word */
00545     if (s->crc_present) {
00546         get_bits(&s->gb, 16);
00547     }
00548 
00549     /*
00550      * Primary audio data arrays
00551      */
00552 
00553     /* VQ encoded high frequency subbands */
00554     for (j = 0; j < s->prim_channels; j++)
00555         for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
00556             /* 1 vector -> 32 samples */
00557             s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
00558 
00559     /* Low frequency effect data */
00560     if (s->lfe) {
00561         /* LFE samples */
00562         int lfe_samples = 2 * s->lfe * s->subsubframes;
00563         float lfe_scale;
00564 
00565         for (j = lfe_samples; j < lfe_samples * 2; j++) {
00566             /* Signed 8 bits int */
00567             s->lfe_data[j] = get_sbits(&s->gb, 8);
00568         }
00569 
00570         /* Scale factor index */
00571         s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
00572 
00573         /* Quantization step size * scale factor */
00574         lfe_scale = 0.035 * s->lfe_scale_factor;
00575 
00576         for (j = lfe_samples; j < lfe_samples * 2; j++)
00577             s->lfe_data[j] *= lfe_scale;
00578     }
00579 
00580 #ifdef TRACE
00581     av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes);
00582     av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
00583            s->partial_samples);
00584     for (j = 0; j < s->prim_channels; j++) {
00585         av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
00586         for (k = 0; k < s->subband_activity[j]; k++)
00587             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
00588         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00589     }
00590     for (j = 0; j < s->prim_channels; j++) {
00591         for (k = 0; k < s->subband_activity[j]; k++)
00592                 av_log(s->avctx, AV_LOG_DEBUG,
00593                        "prediction coefs: %f, %f, %f, %f\n",
00594                        (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
00595                        (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
00596                        (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
00597                        (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
00598     }
00599     for (j = 0; j < s->prim_channels; j++) {
00600         av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
00601         for (k = 0; k < s->vq_start_subband[j]; k++)
00602             av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
00603         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00604     }
00605     for (j = 0; j < s->prim_channels; j++) {
00606         av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
00607         for (k = 0; k < s->subband_activity[j]; k++)
00608             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
00609         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00610     }
00611     for (j = 0; j < s->prim_channels; j++) {
00612         av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
00613         for (k = 0; k < s->subband_activity[j]; k++) {
00614             if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
00615                 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
00616             if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
00617                 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
00618         }
00619         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00620     }
00621     for (j = 0; j < s->prim_channels; j++) {
00622         if (s->joint_intensity[j] > 0) {
00623             int source_channel = s->joint_intensity[j] - 1;
00624             av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
00625             for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
00626                 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
00627             av_log(s->avctx, AV_LOG_DEBUG, "\n");
00628         }
00629     }
00630     if (s->prim_channels > 2 && s->downmix) {
00631         av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
00632         for (j = 0; j < s->prim_channels; j++) {
00633             av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
00634             av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
00635         }
00636         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00637     }
00638     for (j = 0; j < s->prim_channels; j++)
00639         for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
00640             av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
00641     if(s->lfe){
00642         int lfe_samples = 2 * s->lfe * s->subsubframes;
00643         av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
00644         for (j = lfe_samples; j < lfe_samples * 2; j++)
00645             av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
00646         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00647     }
00648 #endif
00649 
00650     return 0;
00651 }
00652 
00653 static void qmf_32_subbands(DCAContext * s, int chans,
00654                             float samples_in[32][8], float *samples_out,
00655                             float scale, float bias)
00656 {
00657     const float *prCoeff;
00658     int i, j, k;
00659     float praXin[33], *raXin = &praXin[1];
00660 
00661     float *subband_fir_hist = s->subband_fir_hist[chans];
00662     float *subband_fir_hist2 = s->subband_fir_noidea[chans];
00663 
00664     int chindex = 0, subindex;
00665 
00666     praXin[0] = 0.0;
00667 
00668     /* Select filter */
00669     if (!s->multirate_inter)    /* Non-perfect reconstruction */
00670         prCoeff = fir_32bands_nonperfect;
00671     else                        /* Perfect reconstruction */
00672         prCoeff = fir_32bands_perfect;
00673 
00674     /* Reconstructed channel sample index */
00675     for (subindex = 0; subindex < 8; subindex++) {
00676         float t1, t2, sum[16], diff[16];
00677 
00678         /* Load in one sample from each subband and clear inactive subbands */
00679         for (i = 0; i < s->subband_activity[chans]; i++)
00680             raXin[i] = samples_in[i][subindex];
00681         for (; i < 32; i++)
00682             raXin[i] = 0.0;
00683 
00684         /* Multiply by cosine modulation coefficients and
00685          * create temporary arrays SUM and DIFF */
00686         for (j = 0, k = 0; k < 16; k++) {
00687             t1 = 0.0;
00688             t2 = 0.0;
00689             for (i = 0; i < 16; i++, j++){
00690                 t1 += (raXin[2 * i] + raXin[2 * i + 1]) * cos_mod[j];
00691                 t2 += (raXin[2 * i] + raXin[2 * i - 1]) * cos_mod[j + 256];
00692             }
00693             sum[k] = t1 + t2;
00694             diff[k] = t1 - t2;
00695         }
00696 
00697         j = 512;
00698         /* Store history */
00699         for (k = 0; k < 16; k++)
00700             subband_fir_hist[k] = cos_mod[j++] * sum[k];
00701         for (k = 0; k < 16; k++)
00702             subband_fir_hist[32-k-1] = cos_mod[j++] * diff[k];
00703 
00704         /* Multiply by filter coefficients */
00705         for (k = 31, i = 0; i < 32; i++, k--)
00706             for (j = 0; j < 512; j += 64){
00707                 subband_fir_hist2[i]    += prCoeff[i+j]  * ( subband_fir_hist[i+j] - subband_fir_hist[j+k]);
00708                 subband_fir_hist2[i+32] += prCoeff[i+j+32]*(-subband_fir_hist[i+j] - subband_fir_hist[j+k]);
00709             }
00710 
00711         /* Create 32 PCM output samples */
00712         for (i = 0; i < 32; i++)
00713             samples_out[chindex++] = subband_fir_hist2[i] * scale + bias;
00714 
00715         /* Update working arrays */
00716         memmove(&subband_fir_hist[32], &subband_fir_hist[0], (512 - 32) * sizeof(float));
00717         memmove(&subband_fir_hist2[0], &subband_fir_hist2[32], 32 * sizeof(float));
00718         memset(&subband_fir_hist2[32], 0, 32 * sizeof(float));
00719     }
00720 }
00721 
00722 static void lfe_interpolation_fir(int decimation_select,
00723                                   int num_deci_sample, float *samples_in,
00724                                   float *samples_out, float scale,
00725                                   float bias)
00726 {
00727     /* samples_in: An array holding decimated samples.
00728      *   Samples in current subframe starts from samples_in[0],
00729      *   while samples_in[-1], samples_in[-2], ..., stores samples
00730      *   from last subframe as history.
00731      *
00732      * samples_out: An array holding interpolated samples
00733      */
00734 
00735     int decifactor, k, j;
00736     const float *prCoeff;
00737 
00738     int interp_index = 0;       /* Index to the interpolated samples */
00739     int deciindex;
00740 
00741     /* Select decimation filter */
00742     if (decimation_select == 1) {
00743         decifactor = 128;
00744         prCoeff = lfe_fir_128;
00745     } else {
00746         decifactor = 64;
00747         prCoeff = lfe_fir_64;
00748     }
00749     /* Interpolation */
00750     for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
00751         /* One decimated sample generates decifactor interpolated ones */
00752         for (k = 0; k < decifactor; k++) {
00753             float rTmp = 0.0;
00754             //FIXME the coeffs are symetric, fix that
00755             for (j = 0; j < 512 / decifactor; j++)
00756                 rTmp += samples_in[deciindex - j] * prCoeff[k + j * decifactor];
00757             samples_out[interp_index++] = rTmp / scale + bias;
00758         }
00759     }
00760 }
00761 
00762 /* downmixing routines */
00763 #define MIX_REAR1(samples, si1, rs, coef) \
00764      samples[i]     += samples[si1] * coef[rs][0]; \
00765      samples[i+256] += samples[si1] * coef[rs][1];
00766 
00767 #define MIX_REAR2(samples, si1, si2, rs, coef) \
00768      samples[i]     += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
00769      samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
00770 
00771 #define MIX_FRONT3(samples, coef) \
00772     t = samples[i]; \
00773     samples[i]     = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \
00774     samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1];
00775 
00776 #define DOWNMIX_TO_STEREO(op1, op2) \
00777     for(i = 0; i < 256; i++){ \
00778         op1 \
00779         op2 \
00780     }
00781 
00782 static void dca_downmix(float *samples, int srcfmt,
00783                         int downmix_coef[DCA_PRIM_CHANNELS_MAX][2])
00784 {
00785     int i;
00786     float t;
00787     float coef[DCA_PRIM_CHANNELS_MAX][2];
00788 
00789     for(i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
00790         coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
00791         coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
00792     }
00793 
00794     switch (srcfmt) {
00795     case DCA_MONO:
00796     case DCA_CHANNEL:
00797     case DCA_STEREO_TOTAL:
00798     case DCA_STEREO_SUMDIFF:
00799     case DCA_4F2R:
00800         av_log(NULL, 0, "Not implemented!\n");
00801         break;
00802     case DCA_STEREO:
00803         break;
00804     case DCA_3F:
00805         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
00806         break;
00807     case DCA_2F1R:
00808         DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),);
00809         break;
00810     case DCA_3