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