• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/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 
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 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
00063  * Some compromises have been made for special configurations. Most configurations
00064  * are never used so complete accuracy is not needed.
00065  *
00066  * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
00067  * S  -> side, when both rear and back are configured move one of them to the side channel
00068  * OV -> center back
00069  * All 2 channel configurations -> CH_LAYOUT_STEREO
00070  */
00071 
00072 static const int64_t dca_core_channel_layout[] = {
00073     CH_FRONT_CENTER,                                               
00074     CH_LAYOUT_STEREO,                                              
00075     CH_LAYOUT_STEREO,                                              
00076     CH_LAYOUT_STEREO,                                              
00077     CH_LAYOUT_STEREO,                                              
00078     CH_LAYOUT_STEREO|CH_FRONT_CENTER,                              
00079     CH_LAYOUT_STEREO|CH_BACK_CENTER,                               
00080     CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_BACK_CENTER,               
00081     CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT,                   
00082     CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_SIDE_LEFT|CH_SIDE_RIGHT,   
00083     CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT|CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER,                 
00084     CH_LAYOUT_STEREO|CH_BACK_LEFT|CH_BACK_RIGHT|CH_FRONT_CENTER|CH_BACK_CENTER,                                   
00085     CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_FRONT_LEFT_OF_CENTER|CH_BACK_CENTER|CH_BACK_LEFT|CH_BACK_RIGHT,   
00086     CH_FRONT_LEFT_OF_CENTER|CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT, 
00087     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, 
00088     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, 
00089 };
00090 
00091 static const int8_t dca_lfe_index[] = {
00092     1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
00093 };
00094 
00095 static const int8_t dca_channel_reorder_lfe[][8] = {
00096     { 0, -1, -1, -1, -1, -1, -1, -1},
00097     { 0,  1, -1, -1, -1, -1, -1, -1},
00098     { 0,  1, -1, -1, -1, -1, -1, -1},
00099     { 0,  1, -1, -1, -1, -1, -1, -1},
00100     { 0,  1, -1, -1, -1, -1, -1, -1},
00101     { 2,  0,  1, -1, -1, -1, -1, -1},
00102     { 0,  1,  3, -1, -1, -1, -1, -1},
00103     { 2,  0,  1,  4, -1, -1, -1, -1},
00104     { 0,  1,  3,  4, -1, -1, -1, -1},
00105     { 2,  0,  1,  4,  5, -1, -1, -1},
00106     { 3,  4,  0,  1,  5,  6, -1, -1},
00107     { 2,  0,  1,  4,  5,  6, -1, -1},
00108     { 0,  6,  4,  5,  2,  3, -1, -1},
00109     { 4,  2,  5,  0,  1,  6,  7, -1},
00110     { 5,  6,  0,  1,  7,  3,  8,  4},
00111     { 4,  2,  5,  0,  1,  6,  8,  7},
00112 };
00113 
00114 static const int8_t dca_channel_reorder_nolfe[][8] = {
00115     { 0, -1, -1, -1, -1, -1, -1, -1},
00116     { 0,  1, -1, -1, -1, -1, -1, -1},
00117     { 0,  1, -1, -1, -1, -1, -1, -1},
00118     { 0,  1, -1, -1, -1, -1, -1, -1},
00119     { 0,  1, -1, -1, -1, -1, -1, -1},
00120     { 2,  0,  1, -1, -1, -1, -1, -1},
00121     { 0,  1,  2, -1, -1, -1, -1, -1},
00122     { 2,  0,  1,  3, -1, -1, -1, -1},
00123     { 0,  1,  2,  3, -1, -1, -1, -1},
00124     { 2,  0,  1,  3,  4, -1, -1, -1},
00125     { 2,  3,  0,  1,  4,  5, -1, -1},
00126     { 2,  0,  1,  3,  4,  5, -1, -1},
00127     { 0,  5,  3,  4,  1,  2, -1, -1},
00128     { 3,  2,  4,  0,  1,  5,  6, -1},
00129     { 4,  5,  0,  1,  6,  2,  7,  3},
00130     { 3,  2,  4,  0,  1,  5,  7,  6},
00131 };
00132 
00133 
00134 #define DCA_DOLBY 101           /* FIXME */
00135 
00136 #define DCA_CHANNEL_BITS 6
00137 #define DCA_CHANNEL_MASK 0x3F
00138 
00139 #define DCA_LFE 0x80
00140 
00141 #define HEADER_SIZE 14
00142 
00143 #define DCA_MAX_FRAME_SIZE 16384
00144 
00146 typedef struct {
00147     int offset;                 
00148     int maxbits[8];             
00149     int wrap;                   
00150     VLC vlc[8];                 
00151 } BitAlloc;
00152 
00153 static BitAlloc dca_bitalloc_index;    
00154 static BitAlloc dca_tmode;             
00155 static BitAlloc dca_scalefactor;       
00156 static BitAlloc dca_smpl_bitalloc[11]; 
00157 
00158 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
00159 {
00160     return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
00161 }
00162 
00163 typedef struct {
00164     AVCodecContext *avctx;
00165     /* Frame header */
00166     int frame_type;             
00167     int samples_deficit;        
00168     int crc_present;            
00169     int sample_blocks;          
00170     int frame_size;             
00171     int amode;                  
00172     int sample_rate;            
00173     int bit_rate;               
00174     int bit_rate_index;         
00175 
00176     int downmix;                
00177     int dynrange;               
00178     int timestamp;              
00179     int aux_data;               
00180     int hdcd;                   
00181     int ext_descr;              
00182     int ext_coding;             
00183     int aspf;                   
00184     int lfe;                    
00185     int predictor_history;      
00186     int header_crc;             
00187     int multirate_inter;        
00188     int version;                
00189     int copy_history;           
00190     int source_pcm_res;         
00191     int front_sum;              
00192     int surround_sum;           
00193     int dialog_norm;            
00194 
00195     /* Primary audio coding header */
00196     int subframes;              
00197     int total_channels;         
00198     int prim_channels;          
00199     int subband_activity[DCA_PRIM_CHANNELS_MAX];    
00200     int vq_start_subband[DCA_PRIM_CHANNELS_MAX];    
00201     int joint_intensity[DCA_PRIM_CHANNELS_MAX];     
00202     int transient_huffman[DCA_PRIM_CHANNELS_MAX];   
00203     int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; 
00204     int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX];    
00205     int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; 
00206     float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];   
00207 
00208     /* Primary audio coding side information */
00209     int subsubframes;           
00210     int partial_samples;        
00211     int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    
00212     int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];      
00213     int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];           
00214     int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    
00215     int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];    
00216     int joint_huff[DCA_PRIM_CHANNELS_MAX];                       
00217     int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; 
00218     int downmix_coef[DCA_PRIM_CHANNELS_MAX][2];                  
00219     int dynrange_coef;                                           
00220 
00221     int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];       
00222 
00223     float lfe_data[2 * DCA_SUBSUBFAMES_MAX * DCA_LFE_MAX *
00224                    2 /*history */ ];    
00225     int lfe_scale_factor;
00226 
00227     /* Subband samples history (for ADPCM) */
00228     float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
00229     DECLARE_ALIGNED_16(float, subband_fir_hist[DCA_PRIM_CHANNELS_MAX][512]);
00230     float subband_fir_noidea[DCA_PRIM_CHANNELS_MAX][32];
00231     int hist_index[DCA_PRIM_CHANNELS_MAX];
00232 
00233     int output;                 
00234     float add_bias;             
00235     float scale_bias;           
00236 
00237     DECLARE_ALIGNED_16(float, samples[1536]);  /* 6 * 256 = 1536, might only need 5 */
00238     const float *samples_chanptr[6];
00239 
00240     uint8_t dca_buffer[DCA_MAX_FRAME_SIZE];
00241     int dca_buffer_size;        
00242 
00243     const int8_t* channel_order_tab;                             
00244     GetBitContext gb;
00245     /* Current position in DCA frame */
00246     int current_subframe;
00247     int current_subsubframe;
00248 
00249     int debug_flag;             
00250     DSPContext dsp;
00251     MDCTContext imdct;
00252 } DCAContext;
00253 
00254 static av_cold void dca_init_vlcs(void)
00255 {
00256     static int vlcs_initialized = 0;
00257     int i, j;
00258 
00259     if (vlcs_initialized)
00260         return;
00261 
00262     dca_bitalloc_index.offset = 1;
00263     dca_bitalloc_index.wrap = 2;
00264     for (i = 0; i < 5; i++)
00265         init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
00266                  bitalloc_12_bits[i], 1, 1,
00267                  bitalloc_12_codes[i], 2, 2, 1);
00268     dca_scalefactor.offset = -64;
00269     dca_scalefactor.wrap = 2;
00270     for (i = 0; i < 5; i++)
00271         init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
00272                  scales_bits[i], 1, 1,
00273                  scales_codes[i], 2, 2, 1);
00274     dca_tmode.offset = 0;
00275     dca_tmode.wrap = 1;
00276     for (i = 0; i < 4; i++)
00277         init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
00278                  tmode_bits[i], 1, 1,
00279                  tmode_codes[i], 2, 2, 1);
00280 
00281     for(i = 0; i < 10; i++)
00282         for(j = 0; j < 7; j++){
00283             if(!bitalloc_codes[i][j]) break;
00284             dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
00285             dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
00286             init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
00287                      bitalloc_sizes[i],
00288                      bitalloc_bits[i][j], 1, 1,
00289                      bitalloc_codes[i][j], 2, 2, 1);
00290         }
00291     vlcs_initialized = 1;
00292 }
00293 
00294 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
00295 {
00296     while(len--)
00297         *dst++ = get_bits(gb, bits);
00298 }
00299 
00300 static int dca_parse_frame_header(DCAContext * s)
00301 {
00302     int i, j;
00303     static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
00304     static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
00305     static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
00306 
00307     init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
00308 
00309     /* Sync code */
00310     get_bits(&s->gb, 32);
00311 
00312     /* Frame header */
00313     s->frame_type        = get_bits(&s->gb, 1);
00314     s->samples_deficit   = get_bits(&s->gb, 5) + 1;
00315     s->crc_present       = get_bits(&s->gb, 1);
00316     s->sample_blocks     = get_bits(&s->gb, 7) + 1;
00317     s->frame_size        = get_bits(&s->gb, 14) + 1;
00318     if (s->frame_size < 95)
00319         return -1;
00320     s->amode             = get_bits(&s->gb, 6);
00321     s->sample_rate       = dca_sample_rates[get_bits(&s->gb, 4)];
00322     if (!s->sample_rate)
00323         return -1;
00324     s->bit_rate_index    = get_bits(&s->gb, 5);
00325     s->bit_rate          = dca_bit_rates[s->bit_rate_index];
00326     if (!s->bit_rate)
00327         return -1;
00328 
00329     s->downmix           = get_bits(&s->gb, 1);
00330     s->dynrange          = get_bits(&s->gb, 1);
00331     s->timestamp         = get_bits(&s->gb, 1);
00332     s->aux_data          = get_bits(&s->gb, 1);
00333     s->hdcd              = get_bits(&s->gb, 1);
00334     s->ext_descr         = get_bits(&s->gb, 3);
00335     s->ext_coding        = get_bits(&s->gb, 1);
00336     s->aspf              = get_bits(&s->gb, 1);
00337     s->lfe               = get_bits(&s->gb, 2);
00338     s->predictor_history = get_bits(&s->gb, 1);
00339 
00340     /* TODO: check CRC */
00341     if (s->crc_present)
00342         s->header_crc    = get_bits(&s->gb, 16);
00343 
00344     s->multirate_inter   = get_bits(&s->gb, 1);
00345     s->version           = get_bits(&s->gb, 4);
00346     s->copy_history      = get_bits(&s->gb, 2);
00347     s->source_pcm_res    = get_bits(&s->gb, 3);
00348     s->front_sum         = get_bits(&s->gb, 1);
00349     s->surround_sum      = get_bits(&s->gb, 1);
00350     s->dialog_norm       = get_bits(&s->gb, 4);
00351 
00352     /* FIXME: channels mixing levels */
00353     s->output = s->amode;
00354     if(s->lfe) s->output |= DCA_LFE;
00355 
00356 #ifdef TRACE
00357     av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
00358     av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
00359     av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
00360     av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
00361            s->sample_blocks, s->sample_blocks * 32);
00362     av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
00363     av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
00364            s->amode, dca_channels[s->amode]);
00365     av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
00366            s->sample_rate);
00367     av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
00368            s->bit_rate);
00369     av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
00370     av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
00371     av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
00372     av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
00373     av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
00374     av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
00375     av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
00376     av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
00377     av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
00378     av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
00379            s->predictor_history);
00380     av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
00381     av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
00382            s->multirate_inter);
00383     av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
00384     av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
00385     av_log(s->avctx, AV_LOG_DEBUG,
00386            "source pcm resolution: %i (%i bits/sample)\n",
00387            s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
00388     av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
00389     av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
00390     av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
00391     av_log(s->avctx, AV_LOG_DEBUG, "\n");
00392 #endif
00393 
00394     /* Primary audio coding header */
00395     s->subframes         = get_bits(&s->gb, 4) + 1;
00396     s->total_channels    = get_bits(&s->gb, 3) + 1;
00397     s->prim_channels     = s->total_channels;
00398     if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
00399         s->prim_channels = DCA_PRIM_CHANNELS_MAX;   /* We only support DTS core */
00400 
00401 
00402     for (i = 0; i < s->prim_channels; i++) {
00403         s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
00404         if (s->subband_activity[i] > DCA_SUBBANDS)
00405             s->subband_activity[i] = DCA_SUBBANDS;
00406     }
00407     for (i = 0; i < s->prim_channels; i++) {
00408         s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
00409         if (s->vq_start_subband[i] > DCA_SUBBANDS)
00410             s->vq_start_subband[i] = DCA_SUBBANDS;
00411     }
00412     get_array(&s->gb, s->joint_intensity,     s->prim_channels, 3);
00413     get_array(&s->gb, s->transient_huffman,   s->prim_channels, 2);
00414     get_array(&s->gb, s->scalefactor_huffman, s->prim_channels, 3);
00415     get_array(&s->gb, s->bitalloc_huffman,    s->prim_channels, 3);
00416 
00417     /* Get codebooks quantization indexes */
00418     memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
00419     for (j = 1; j < 11; j++)
00420         for (i = 0; i < s->prim_channels; i++)
00421             s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
00422 
00423     /* Get scale factor adjustment */
00424     for (j = 0; j < 11; j++)
00425         for (i = 0; i < s->prim_channels; i++)
00426             s->scalefactor_adj[i][j] = 1;
00427 
00428     for (j = 1; j < 11; j++)
00429         for (i = 0; i < s->prim_channels; i++)
00430             if (s->quant_index_huffman[i][j] < thr[j])
00431                 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
00432 
00433     if (s->crc_present) {
00434         /* Audio header CRC check */
00435         get_bits(&s->gb, 16);
00436     }
00437 
00438     s->current_subframe = 0;
00439     s->current_subsubframe = 0;
00440 
00441 #ifdef TRACE
00442     av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
00443     av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
00444     for(i = 0; i < s->prim_channels; i++){
00445         av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
00446         av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
00447         av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
00448         av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
00449         av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
00450         av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
00451         av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
00452         for (j = 0; j < 11; j++)
00453             av_log(s->avctx, AV_LOG_DEBUG, " %i",
00454                    s->quant_index_huffman[i][j]);
00455         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00456         av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
00457         for (j = 0; j < 11; j++)
00458             av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
00459         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00460     }
00461 #endif
00462 
00463     return 0;
00464 }
00465 
00466 
00467 static inline int get_scale(GetBitContext *gb, int level, int value)
00468 {
00469    if (level < 5) {
00470        /* huffman encoded */
00471        value += get_bitalloc(gb, &dca_scalefactor, level);
00472    } else if(level < 8)
00473        value = get_bits(gb, level + 1);
00474    return value;
00475 }
00476 
00477 static int dca_subframe_header(DCAContext * s)
00478 {
00479     /* Primary audio coding side information */
00480     int j, k;
00481 
00482     s->subsubframes = get_bits(&s->gb, 2) + 1;
00483     s->partial_samples = get_bits(&s->gb, 3);
00484     for (j = 0; j < s->prim_channels; j++) {
00485         for (k = 0; k < s->subband_activity[j]; k++)
00486             s->prediction_mode[j][k] = get_bits(&s->gb, 1);
00487     }
00488 
00489     /* Get prediction codebook */
00490     for (j = 0; j < s->prim_channels; j++) {
00491         for (k = 0; k < s->subband_activity[j]; k++) {
00492             if (s->prediction_mode[j][k] > 0) {
00493                 /* (Prediction coefficient VQ address) */
00494                 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
00495             }
00496         }
00497     }
00498 
00499     /* Bit allocation index */
00500     for (j = 0; j < s->prim_channels; j++) {
00501         for (k = 0; k < s->vq_start_subband[j]; k++) {
00502             if (s->bitalloc_huffman[j] == 6)
00503                 s->bitalloc[j][k] = get_bits(&s->gb, 5);
00504             else if (s->bitalloc_huffman[j] == 5)
00505                 s->bitalloc[j][k] = get_bits(&s->gb, 4);
00506             else if (s->bitalloc_huffman[j] == 7) {
00507                 av_log(s->avctx, AV_LOG_ERROR,
00508                        "Invalid bit allocation index\n");
00509                 return -1;
00510             } else {
00511                 s->bitalloc[j][k] =
00512                     get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
00513             }
00514 
00515             if (s->bitalloc[j][k] > 26) {
00516 //                 av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
00517 //                          j, k, s->bitalloc[j][k]);
00518                 return -1;
00519             }
00520         }
00521     }
00522 
00523     /* Transition mode */
00524     for (j = 0; j < s->prim_channels; j++) {
00525         for (k = 0; k < s->subband_activity[j]; k++) {
00526             s->transition_mode[j][k] = 0;
00527             if (s->subsubframes > 1 &&
00528                 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
00529                 s->transition_mode[j][k] =
00530                     get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
00531             }
00532         }
00533     }
00534 
00535     for (j = 0; j < s->prim_channels; j++) {
00536         const uint32_t *scale_table;
00537         int scale_sum;
00538 
00539         memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
00540 
00541         if (s->scalefactor_huffman[j] == 6)
00542             scale_table = scale_factor_quant7;
00543         else
00544             scale_table = scale_factor_quant6;
00545 
00546         /* When huffman coded, only the difference is encoded */
00547         scale_sum = 0;
00548 
00549         for (k = 0; k < s->subband_activity[j]; k++) {
00550             if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
00551                 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
00552                 s->scale_factor[j][k][0] = scale_table[scale_sum];
00553             }
00554 
00555             if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
00556                 /* Get second scale factor */
00557                 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
00558                 s->scale_factor[j][k][1] = scale_table[scale_sum];
00559             }
00560         }
00561     }
00562 
00563     /* Joint subband scale factor codebook select */
00564     for (j = 0; j < s->prim_channels; j++) {
00565         /* Transmitted only if joint subband coding enabled */
00566         if (s->joint_intensity[j] > 0)
00567             s->joint_huff[j] = get_bits(&s->gb, 3);
00568     }
00569 
00570     /* Scale factors for joint subband coding */
00571     for (j = 0; j < s->prim_channels; j++) {
00572         int source_channel;
00573 
00574         /* Transmitted only if joint subband coding enabled */
00575         if (s->joint_intensity[j] > 0) {
00576             int scale = 0;
00577             source_channel = s->joint_intensity[j] - 1;
00578 
00579             /* When huffman coded, only the difference is encoded
00580              * (is this valid as well for joint scales ???) */
00581 
00582             for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
00583                 scale = get_scale(&s->gb, s->joint_huff[j], 0);
00584                 scale += 64;    /* bias */
00585                 s->joint_scale_factor[j][k] = scale;    /*joint_scale_table[scale]; */
00586             }
00587 
00588             if (!s->debug_flag & 0x02) {
00589                 av_log(s->avctx, AV_LOG_DEBUG,
00590                        "Joint stereo coding not supported\n");
00591                 s->debug_flag |= 0x02;
00592             }
00593         }
00594     }
00595 
00596     /* Stereo downmix coefficients */
00597     if (s->prim_channels > 2) {
00598         if(s->downmix) {
00599             for (j = 0; j < s->prim_channels; j++) {
00600                 s->downmix_coef[j][0] = get_bits(&s->gb, 7);
00601                 s->downmix_coef[j][1] = get_bits(&s->gb, 7);
00602             }
00603         } else {
00604             int am = s->amode & DCA_CHANNEL_MASK;
00605             for (j = 0; j < s->prim_channels; j++) {
00606                 s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
00607                 s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
00608             }
00609         }
00610     }
00611 
00612     /* Dynamic range coefficient */
00613     if (s->dynrange)
00614         s->dynrange_coef = get_bits(&s->gb, 8);
00615 
00616     /* Side information CRC check word */
00617     if (s->crc_present) {
00618         get_bits(&s->gb, 16);
00619     }
00620 
00621     /*
00622      * Primary audio data arrays
00623      */
00624 
00625     /* VQ encoded high frequency subbands */
00626     for (j = 0; j < s->prim_channels; j++)
00627         for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
00628             /* 1 vector -> 32 samples */
00629             s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
00630 
00631     /* Low frequency effect data */
00632     if (s->lfe) {
00633         /* LFE samples */
00634         int lfe_samples = 2 * s->lfe * s->subsubframes;
00635         float lfe_scale;
00636 
00637         for (j = lfe_samples; j < lfe_samples * 2; j++) {
00638             /* Signed 8 bits int */
00639             s->lfe_data[j] = get_sbits(&s->gb, 8);
00640         }
00641 
00642         /* Scale factor index */
00643         s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
00644 
00645         /* Quantization step size * scale factor */
00646         lfe_scale = 0.035 * s->lfe_scale_factor;
00647 
00648         for (j = lfe_samples; j < lfe_samples * 2; j++)
00649             s->lfe_data[j] *= lfe_scale;
00650     }
00651 
00652 #ifdef TRACE
00653     av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes);
00654     av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
00655            s->partial_samples);
00656     for (j = 0; j < s->prim_channels; j++) {
00657         av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
00658         for (k = 0; k < s->subband_activity[j]; k++)
00659             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
00660         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00661     }
00662     for (j = 0; j < s->prim_channels; j++) {
00663         for (k = 0; k < s->subband_activity[j]; k++)
00664                 av_log(s->avctx, AV_LOG_DEBUG,
00665                        "prediction coefs: %f, %f, %f, %f\n",
00666                        (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
00667                        (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
00668                        (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
00669                        (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
00670     }
00671     for (j = 0; j < s->prim_channels; j++) {
00672         av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
00673         for (k = 0; k < s->vq_start_subband[j]; k++)
00674             av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
00675         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00676     }
00677     for (j = 0; j < s->prim_channels; j++) {
00678         av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
00679         for (k = 0; k < s->subband_activity[j]; k++)
00680             av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
00681         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00682     }
00683     for (j = 0; j < s->prim_channels; j++) {
00684         av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
00685         for (k = 0; k < s->subband_activity[j]; k++) {
00686             if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
00687                 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
00688             if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
00689                 av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
00690         }
00691         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00692     }
00693     for (j = 0; j < s->prim_channels; j++) {
00694         if (s->joint_intensity[j] > 0) {
00695             int source_channel = s->joint_intensity[j] - 1;
00696             av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
00697             for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
00698                 av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
00699             av_log(s->avctx, AV_LOG_DEBUG, "\n");
00700         }
00701     }
00702     if (s->prim_channels > 2 && s->downmix) {
00703         av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
00704         for (j = 0; j < s->prim_channels; j++) {
00705             av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
00706             av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
00707         }
00708         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00709     }
00710     for (j = 0; j < s->prim_channels; j++)
00711         for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
00712             av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
00713     if(s->lfe){
00714         int lfe_samples = 2 * s->lfe * s->subsubframes;
00715         av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
00716         for (j = lfe_samples; j < lfe_samples * 2; j++)
00717             av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
00718         av_log(s->avctx, AV_LOG_DEBUG, "\n");
00719     }
00720 #endif
00721 
00722     return 0;
00723 }
00724 
00725 static void qmf_32_subbands(DCAContext * s, int chans,
00726                             float samples_in[32][8], float *samples_out,
00727                             float scale, float bias)
00728 {
00729     const float *prCoeff;
00730     int i, j;
00731     DECLARE_ALIGNED_16(float, raXin[32]);
00732 
00733     int hist_index= s->hist_index[chans];
00734     float *subband_fir_hist2 = s->subband_fir_noidea[chans];
00735 
00736     int subindex;
00737 
00738     scale *= sqrt(1/8.0);
00739 
00740     /* Select filter */
00741     if (!s->multirate_inter)    /* Non-perfect reconstruction */
00742         prCoeff = fir_32bands_nonperfect;
00743     else                        /* Perfect reconstruction */
00744         prCoeff = fir_32bands_perfect;
00745 
00746     /* Reconstructed channel sample index */
00747     for (subindex = 0; subindex < 8; subindex++) {
00748         float *subband_fir_hist = s->subband_fir_hist[chans] + hist_index;
00749         /* Load in one sample from each subband and clear inactive subbands */
00750         for (i = 0; i < s->subband_activity[chans]; i++){
00751             if((i-1)&2) raXin[i] = -samples_in[i][subindex];
00752             else        raXin[i] =  samples_in[i][subindex];
00753         }
00754         for (; i < 32; i++)
00755             raXin[i] = 0.0;
00756 
00757         ff_imdct_half(&s->imdct, subband_fir_hist, raXin);
00758 
00759         /* Multiply by filter coefficients */
00760         for (i = 0; i < 16; i++){
00761             float a= subband_fir_hist2[i   ];
00762             float b= subband_fir_hist2[i+16];
00763             float c= 0;
00764             float d= 0;
00765             for (j = 0; j < 512-hist_index; j += 64){
00766                 a += prCoeff[i+j   ]*(-subband_fir_hist[15-i+j]);
00767                 b += prCoeff[i+j+16]*( subband_fir_hist[   i+j]);
00768                 c += prCoeff[i+j+32]*( subband_fir_hist[16+i+j]);
00769                 d += prCoeff[i+j+48]*( subband_fir_hist[31-i+j]);
00770             }
00771             for (     ; j < 512; j += 64){
00772                 a += prCoeff[i+j   ]*(-subband_fir_hist[15-i+j-512]);
00773                 b += prCoeff[i+j+16]*( subband_fir_hist[   i+j-512]);
00774                 c += prCoeff[i+j+32]*( subband_fir_hist[16+i+j-512]);
00775                 d += prCoeff[i+j+48]*( subband_fir_hist[31-i+j-512]);
00776             }
00777             samples_out[i   ] = a * scale + bias;
00778             samples_out[i+16] = b * scale + bias;
00779             subband_fir_hist2[i   ] = c;
00780             subband_fir_hist2[i+16] = d;
00781         }
00782         samples_out+= 32;
00783 
00784         hist_index = (hist_index-32)&511;
00785     }
00786     s->hist_index[chans]= hist_index;
00787 }
00788 
00789 static void lfe_interpolation_fir(int decimation_select,
00790                                   int num_deci_sample, float *samples_in,
00791                                   float *samples_out, float scale,
00792                                   float bias)
00793 {
00794     /* samples_in: An array holding decimated samples.
00795      *   Samples in current subframe starts from samples_in[0],
00796      *   while samples_in[-1], samples_in[-2], ..., stores samples
00797      *   from last subframe as history.
00798      *
00799      * samples_out: An array holding interpolated samples
00800      */
00801 
00802     int decifactor, k, j;
00803     const float *prCoeff;
00804 
00805     int interp_index = 0;       /* Index to the interpolated samples */
00806     int deciindex;
00807 
00808     /* Select decimation filter */
00809     if (decimation_select == 1) {
00810         decifactor = 128;
00811         prCoeff = lfe_fir_128;
00812     } else {
00813         decifactor = 64;
00814         prCoeff = lfe_fir_64;
00815     }
00816     /* Interpolation */
00817     for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
00818         /* One decimated sample generates decifactor interpolated ones */
00819         for (k = 0; k < decifactor; k++) {
00820             float rTmp = 0.0;
00821             //FIXME the coeffs are symetric, fix that
00822             for (j = 0; j < 512 / decifactor; j++)
00823                 rTmp += samples_in[deciindex - j] * prCoeff[k + j * decifactor];
00824             samples_out[interp_index++] = (rTmp * scale) + bias;
00825         }
00826     }
00827 }
00828 
00829 /* downmixing routines */
00830 #define MIX_REAR1(samples, si1, rs, coef) \
00831      samples[i]     += samples[si1] * coef[rs][0]; \
00832      samples[i+256] += samples[si1] * coef[rs][1];
00833 
00834 #define MIX_REAR2(samples, si1, si2, rs, coef) \
00835      samples[i]     += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
00836      samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
00837 
00838 #define MIX_FRONT3(samples, coef) \
00839     t = samples[i]; \
00840     samples[i]     = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \
00841     samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1];
00842 
00843 #define DOWNMIX_TO_STEREO(op1, op2) \
00844     for(i = 0; i < 256; i++){ \
00845         op1 \
00846         op2 \
00847     }
00848 
00849 static void dca_downmix(float *samples, int srcfmt,
00850                         int downmix_coef[DCA_PRIM_CHANNELS_MAX][2])
00851 {
00852     int i;
00853     float t;
00854     float coef[DCA_PRIM_CHANNELS_MAX][2];
00855 
00856     for(i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
00857         coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
00858         coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
00859     }
00860 
00861     switch (srcfmt) {
00862     case DCA_MONO:
00863     case DCA_CHANNEL:
00864     case DCA_STEREO_TOTAL:
00865     case DCA_STEREO_SUMDIFF:
00866     case DCA_4F2R:
00867         av_log(NULL, 0, "Not implemented!\n");
00868         break;
00869     case DCA_STEREO:
00870         break;
00871     case DCA_3F:
00872         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
00873         break;
00874     case DCA_2F1R:
00875         DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),);
00876         break;
00877     case DCA_3F1R:
00878         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
00879                           MIX_REAR1(samples, i + 768, 3, coef));
00880         break;
00881     case DCA_2F2R:
00882         DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768, 2, coef),);
00883         break;
00884     case DCA_3F2R:
00885         DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
00886                           MIX_REAR2(samples, i + 768, i + 1024, 3, coef));
00887         break;
00888     }
00889 }
00890 
00891 
00892 /* Very compact version of the block code decoder that does not use table
00893  * look-up but is slightly slower */
00894 static int decode_blockcode(int code, int levels, int *values)
00895 {
00896     int i;
00897     int offset = (levels - 1) >> 1;
00898 
00899     for (i = 0; i < 4; i++) {
00900         values[i] = (code % levels) - offset;
00901         code /= levels;
00902     }
00903 
00904     if (code == 0)
00905         return 0;
00906     else {
00907         av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
00908         return -1;
00909     }
00910 }
00911 
00912 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
00913 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
00914 
00915 static int dca_subsubframe(DCAContext * s)
00916 {
00917     int k, l;
00918     int subsubframe = s->current_subsubframe;
00919 
00920     const float *quant_step_table;
00921 
00922     /* FIXME */
00923     float subband_samples[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
00924 
00925     /*
00926      * Audio data
00927      */
00928 
00929     /* Select quantization step size table */
00930     if (s->bit_rate_index == 0x1f)
00931         quant_step_table = lossless_quant_d;
00932     else
00933         quant_step_table = lossy_quant_d;
00934 
00935     for (k = 0; k < s->prim_channels; k++) {
00936         for (l = 0; l < s->vq_start_subband[k]; l++) {
00937             int m;
00938 
00939             /* Select the mid-tread linear quantizer */
00940             int abits = s->bitalloc[k][l];
00941 
00942             float quant_step_size = quant_step_table[abits];
00943             float rscale;
00944 
00945             /*
00946              * Determine quantization index code book and its type
00947              */
00948 
00949             /* Select quantization index code book */
00950             int sel = s->quant_index_huffman[k][abits];
00951 
00952             /*
00953              * Extract bits from the bit stream
00954              */
00955             if(!abits){
00956                 memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
00957             }else if(abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
00958                 if(abits <= 7){
00959                     /* Block code */
00960                     int block_code1, block_code2, size, levels;
00961                     int block[8];
00962 
00963                     size = abits_sizes[abits-1];
00964                     levels = abits_levels[abits-1];
00965 
00966                     block_code1 = get_bits(&s->gb, size);
00967                     /* FIXME Should test return value */
00968                     decode_blockcode(block_code1, levels, block);
00969                     block_code2 = get_bits(&s->gb, size);
00970                     decode_blockcode(block_code2, levels, &block[4]);
00971                     for (m = 0; m < 8; m++)
00972                         subband_samples[k][l][m] = block[m];
00973                 }else{
00974                     /* no coding */
00975                     for (m = 0; m < 8; m++)
00976                         subband_samples[k][l][m] = get_sbits(&s->gb, abits - 3);
00977                 }
00978             }else{
00979                 /* Huffman coded */
00980                 for (m = 0; m < 8; m++)
00981                     subband_samples[k][l][m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
00982             }
00983 
00984             /* Deal with transients */
00985             if (s->transition_mode[k][l] &&
00986                 subsubframe >= s->transition_mode[k][l])
00987                 rscale = quant_step_size * s->scale_factor[k][l][1];
00988             else
00989                 rscale = quant_step_size * s->scale_factor[k][l][0];
00990 
00991             rscale *= s->scalefactor_adj[k][sel];
00992 
00993             for (m = 0; m < 8; m++)
00994                 subband_samples[k][l][m] *= rscale;
00995 
00996             /*
00997              * Inverse ADPCM if in prediction mode
00998              */
00999             if (s->prediction_mode[k][l]) {
01000                 int n;
01001                 for (m = 0; m < 8; m++) {
01002                     for (n = 1; n <= 4; n++)
01003                         if (m >= n)
01004                             subband_samples[k][l][m] +=
01005                                 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
01006                                  subband_samples[k][l][m - n] / 8192);
01007                         else if (s->predictor_history)
01008                             subband_samples[k][l][m] +=
01009                                 (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
01010                                  s->subband_samples_hist[k][l][m - n +
01011                                                                4] / 8192);
01012                 }
01013             }
01014         }
01015 
01016         /*
01017          * Decode VQ encoded high frequencies
01018          */
01019         for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
01020             /* 1 vector -> 32 samples but we only need the 8 samples
01021              * for this subsubframe. */
01022             int m;
01023 
01024             if (!s->debug_flag & 0x01) {
01025                 av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
01026                 s->debug_flag |= 0x01;
01027             }
01028 
01029             for (m = 0; m < 8; m++) {
01030                 subband_samples[k][l][m] =
01031                     high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
01032                                                         m]
01033                     * (float) s->scale_factor[k][l][0] / 16.0;
01034             }
01035         }
01036     }
01037 
01038     /* Check for DSYNC after subsubframe */
01039     if (s->aspf || subsubframe == s->subsubframes - 1) {
01040         if (0xFFFF == get_bits(&s->gb, 16)) {   /* 0xFFFF */
01041 #ifdef TRACE
01042             av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
01043 #endif
01044         } else {
01045             av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
01046         }
01047     }
01048 
01049     /* Backup predictor history for adpcm */
01050     for (k = 0; k < s->prim_channels; k++)
01051         for (l = 0; l < s->vq_start_subband[k]; l++)
01052             memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
01053                         4 * sizeof(subband_samples[0][0][0]));
01054 
01055     /* 32 subbands QMF */
01056     for (k = 0; k < s->prim_channels; k++) {
01057 /*        static float pcm_to_double[8] =
01058             {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
01059          qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
01060                             M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ ,
01061                             s->add_bias );
01062     }
01063 
01064     /* Down mixing */
01065 
01066     if (s->prim_channels > dca_channels[s->output & DCA_CHANNEL_MASK]) {
01067         dca_downmix(s->samples, s->amode, s->downmix_coef);
01068     }
01069 
01070     /* Generate LFE samples for this subsubframe FIXME!!! */
01071     if (s->output & DCA_LFE) {
01072         int lfe_samples = 2 * s->lfe * s->subsubframes;
01073 
01074         lfe_interpolation_fir(s->lfe, 2 * s->lfe,
01075                               s->lfe_data + lfe_samples +
01076                               2 * s->lfe * subsubframe,
01077                               &s->samples[256 * dca_lfe_index[s->amode]],
01078                               (1.0/256.0)*s->scale_bias,  s->add_bias);
01079         /* Outputs 20bits pcm samples */
01080     }
01081 
01082     return 0;
01083 }
01084 
01085 
01086 static int dca_subframe_footer(DCAContext * s)
01087 {
01088     int aux_data_count = 0, i;
01089     int lfe_samples;
01090 
01091     /*
01092      * Unpack optional information
01093      */
01094 
01095     if (s->timestamp)
01096         get_bits(&s->gb, 32);
01097 
01098     if (s->aux_data)
01099         aux_data_count = get_bits(&s->gb, 6);
01100 
01101     for (i = 0; i < aux_data_count; i++)
01102         get_bits(&s->gb, 8);
01103 
01104     if (s->crc_present && (s->downmix || s->dynrange))
01105         get_bits(&s->gb, 16);
01106 
01107     lfe_samples = 2 * s->lfe * s->subsubframes;
01108     for (i = 0; i < lfe_samples; i++) {
01109         s->lfe_data[i] = s->lfe_data[i + lfe_samples];
01110     }
01111 
01112     return 0;
01113 }
01114 
01121 static int dca_decode_block(DCAContext * s)
01122 {
01123 
01124     /* Sanity check */
01125     if (s->current_subframe >= s->subframes) {
01126         av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
01127                s->current_subframe, s->subframes);
01128         return -1;
01129     }
01130 
01131     if (!s->current_subsubframe) {
01132 #ifdef TRACE
01133         av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
01134 #endif
01135         /* Read subframe header */
01136         if (dca_subframe_header(s))
01137             return -1;
01138     }
01139 
01140     /* Read subsubframe */
01141 #ifdef TRACE
01142     av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
01143 #endif
01144     if (dca_subsubframe(s))
01145         return -1;
01146 
01147     /* Update state */
01148     s->current_subsubframe++;
01149     if (s->current_subsubframe >= s->subsubframes) {
01150         s->current_subsubframe = 0;
01151         s->current_subframe++;
01152     }
01153     if (s->current_subframe >= s->subframes) {
01154 #ifdef TRACE
01155         av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
01156 #endif
01157         /* Read subframe footer */
01158         if (dca_subframe_footer(s))
01159             return -1;
01160     }
01161 
01162     return 0;
01163 }
01164 
01168 static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
01169                           int max_size)
01170 {
01171     uint32_t mrk;
01172     int i, tmp;
01173     const uint16_t *ssrc = (const uint16_t *) src;
01174     uint16_t *sdst = (uint16_t *) dst;
01175     PutBitContext pb;
01176 
01177     if((unsigned)src_size > (unsigned)max_size) {
01178 //        av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
01179 //        return -1;
01180         src_size = max_size;
01181     }
01182 
01183     mrk = AV_RB32(src);
01184     switch (mrk) {
01185     case DCA_MARKER_RAW_BE:
01186         memcpy(dst, src, src_size);
01187         return src_size;
01188     case DCA_MARKER_RAW_LE:
01189         for (i = 0; i < (src_size + 1) >> 1; i++)
01190             *sdst++ = bswap_16(*ssrc++);
01191         return src_size;
01192     case DCA_MARKER_14B_BE:
01193     case DCA_MARKER_14B_LE:
01194         init_put_bits(&pb, dst, max_size);
01195         for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
01196             tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
01197             put_bits(&pb, 14, tmp);
01198         }
01199         flush_put_bits(&pb);
01200         return (put_bits_count(&pb) + 7) >> 3;
01201     default:
01202         return -1;
01203     }
01204 }
01205 
01210 static int dca_decode_frame(AVCodecContext * avctx,
01211                             void *data, int *data_size,
01212                             const uint8_t * buf, int buf_size)
01213 {
01214 
01215     int i;
01216     int16_t *samples = data;
01217     DCAContext *s = avctx->priv_data;
01218     int channels;
01219 
01220 
01221     s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE);
01222     if (s->dca_buffer_size == -1) {
01223         av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
01224         return -1;
01225     }
01226 
01227     init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
01228     if (dca_parse_frame_header(s) < 0) {
01229         //seems like the frame is corrupt, try with the next one
01230         *data_size=0;
01231         return buf_size;
01232     }
01233     //set AVCodec values with parsed data
01234     avctx->sample_rate = s->sample_rate;
01235     avctx->bit_rate = s->bit_rate;
01236 
01237     channels = s->prim_channels + !!s->lfe;
01238 
01239     if (s->amode<16) {
01240         avctx->channel_layout = dca_core_channel_layout[s->amode];
01241 
01242         if (s->lfe) {
01243             avctx->channel_layout |= CH_LOW_FREQUENCY;
01244             s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
01245         } else
01246             s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
01247 
01248         if(avctx->request_channels == 2 && s->prim_channels > 2) {
01249             channels = 2;
01250             s->output = DCA_STEREO;
01251             avctx->channel_layout = CH_LAYOUT_STEREO;
01252         }
01253     } else {
01254         av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
01255         return -1;
01256     }
01257 
01258 
01259     /* There is nothing that prevents a dts frame to change channel configuration
01260        but FFmpeg doesn't support that so only set the channels if it is previously
01261        unset. Ideally during the first probe for channels the crc should be checked
01262        and only set avctx->channels when the crc is ok. Right now the decoder could
01263        set the channels based on a broken first frame.*/
01264     if (!avctx->channels)
01265         avctx->channels = channels;
01266 
01267     if(*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels)
01268         return -1;
01269     *data_size = 256 / 8 * s->sample_blocks * sizeof(int16_t) * channels;
01270     for (i = 0; i < (s->sample_blocks / 8); i++) {
01271         dca_decode_block(s);
01272         s->dsp.float_to_int16_interleave(samples, s->samples_chanptr, 256, channels);
01273         samples += 256 * channels;
01274     }
01275 
01276     return buf_size;
01277 }
01278 
01279 
01280 
01287 static av_cold int dca_decode_init(AVCodecContext * avctx)
01288 {
01289     DCAContext *s = avctx->priv_data;
01290     int i;
01291 
01292     s->avctx = avctx;
01293     dca_init_vlcs();
01294 
01295     dsputil_init(&s->dsp, avctx);
01296     ff_mdct_init(&s->imdct, 6, 1);
01297 
01298     for(i = 0; i < 6; i++)
01299         s->samples_chanptr[i] = s->samples + i * 256;
01300     avctx->sample_fmt = SAMPLE_FMT_S16;
01301 
01302     if(s->dsp.float_to_int16 == ff_float_to_int16_c) {
01303         s->add_bias = 385.0f;
01304         s->scale_bias = 1.0 / 32768.0;
01305     } else {
01306         s->add_bias = 0.0f;
01307         s->scale_bias = 1.0;
01308 
01309         /* allow downmixing to stereo */
01310         if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
01311                 avctx->request_channels == 2) {
01312             avctx->channels = avctx->request_channels;
01313         }
01314     }
01315 
01316 
01317     return 0;
01318 }
01319 
01320 static av_cold int dca_decode_end(AVCodecContext * avctx)
01321 {
01322     DCAContext *s = avctx->priv_data;
01323     ff_mdct_end(&s->imdct);
01324     return 0;
01325 }
01326 
01327 AVCodec dca_decoder = {
01328     .name = "dca",
01329     .type = CODEC_TYPE_AUDIO,
01330     .id = CODEC_ID_DTS,
01331     .priv_data_size = sizeof(DCAContext),
01332     .init = dca_decode_init,
01333     .decode = dca_decode_frame,
01334     .close = dca_decode_end,
01335     .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
01336 };

Generated on Tue Nov 4 2014 12:59:21 for ffmpeg by  doxygen 1.7.1