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