dts_parse.c

00001 /*
00002  * parse.c
00003  * Copyright (C) 2004 Gildas Bazin <[email protected]>
00004  *
00005  * This file is part of dtsdec, a free DTS Coherent Acoustics stream decoder.
00006  * See http://www.videolan.org/dtsdec.html for updates.
00007  *
00008  * dtsdec is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * dtsdec is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00023 #include "config.h"
00024 
00025 #include <stdio.h>
00026 
00027 //#include <stdlib.h>
00028 
00029 void    __cdecl free(void *);
00030 void *  __cdecl malloc(size_t);
00031 
00032 #include <string.h>
00033 #include <inttypes.h>
00034 
00035 #include <math.h>
00036 
00037 #ifndef M_PI
00038 #define M_PI 3.1415926535897932384626433832795029
00039 #endif
00040 
00041 #include "dts.h"
00042 #include "dts_internal.h"
00043 #include "bitstream.h"
00044 
00045 #include "tables.h"
00046 #include "tables_huffman.h"
00047 #include "tables_quantization.h"
00048 #include "tables_adpcm.h"
00049 #include "tables_fir.h"
00050 #include "tables_vq.h"
00051 
00052 /* #define DEBUG */
00053 
00054 #if defined(HAVE_MEMALIGN) && !defined(__cplusplus)
00055 /* some systems have memalign() but no declaration for it */
00056 void * memalign (size_t align, size_t size);
00057 #else
00058 /* assume malloc alignment is sufficient */
00059 #define memalign(align,size) malloc (size)
00060 #endif
00061 
00062 static int decode_blockcode (int code, int levels, int *values);
00063 
00064 static void qmf_32_subbands (dts_state_t * state, int chans,
00065                              double samples_in[32][8], sample_t *samples_out,
00066                              double rScale, sample_t bias);
00067 
00068 static void lfe_interpolation_fir (int nDecimationSelect, int nNumDeciSample,
00069                                    double *samples_in, sample_t *samples_out,
00070                                    double rScale, sample_t bias );
00071 
00072 static void pre_calc_cosmod( dts_state_t * state );
00073 
00074 dts_state_t * dts_init (uint32_t mm_accel)
00075 {
00076     dts_state_t * state;
00077     int i;
00078 
00079     state = (dts_state_t *) malloc (sizeof (dts_state_t));
00080     if (state == NULL)
00081         return NULL;
00082 
00083     memset (state, 0, sizeof(dts_state_t));
00084 
00085     state->samples = (sample_t *) memalign (16, 256 * 12 * sizeof (sample_t));
00086     if (state->samples == NULL) {
00087         free (state);
00088         return NULL;
00089     }
00090 
00091     for (i = 0; i < 256 * 12; i++)
00092         state->samples[i] = 0;
00093 
00094     /* Pre-calculate cosine modulation coefficients */
00095     pre_calc_cosmod( state );
00096 
00097     state->downmixed = 1;
00098 
00099     return state;
00100 }
00101 
00102 sample_t * dts_samples (dts_state_t * state)
00103 {
00104     return state->samples;
00105 }
00106 
00107 int dts_blocks_num (dts_state_t * state)
00108 {
00109     /* 8 samples per subsubframe and per subband */
00110     return state->sample_blocks / 8;
00111 }
00112 
00113 static int syncinfo (dts_state_t * state, int * flags,
00114                      int * sample_rate, int * bit_rate, int * frame_length)
00115 {
00116     int frame_size;
00117 
00118     /* Sync code */
00119     bitstream_get (state, 32);
00120     /* Frame type */
00121     bitstream_get (state, 1);
00122     /* Samples deficit */
00123     bitstream_get (state, 5);
00124     /* CRC present */
00125     bitstream_get (state, 1);
00126 
00127     *frame_length = (bitstream_get (state, 7) + 1) * 32;
00128     frame_size = bitstream_get (state, 14) + 1;
00129     if (!state->word_mode) frame_size = frame_size * 8 / 14 * 2;
00130 
00131     /* Audio channel arrangement */
00132     *flags = bitstream_get (state, 6);
00133     if (*flags > 63)
00134         return 0;
00135 
00136     *sample_rate = bitstream_get (state, 4);
00137     if (*sample_rate >= sizeof (dts_sample_rates) / sizeof (int))
00138         return 0;
00139     *sample_rate = dts_sample_rates[ *sample_rate ];
00140     if (!*sample_rate) return 0;
00141 
00142     *bit_rate = bitstream_get (state, 5);
00143     if (*bit_rate >= sizeof (dts_bit_rates) / sizeof (int))
00144         return 0;
00145     *bit_rate = dts_bit_rates[ *bit_rate ];
00146     if (!*bit_rate) return 0;
00147 
00148     /* LFE */
00149     bitstream_get (state, 10);
00150     if (bitstream_get (state, 2)) *flags |= DTS_LFE;
00151 
00152     return frame_size;
00153 }
00154 
00155 int dts_syncinfo (dts_state_t * state, uint8_t * buf, int * flags,
00156                   int * sample_rate, int * bit_rate, int * frame_length)
00157 {
00158     /*
00159      * Look for sync code
00160      */
00161 
00162     /* 14 bits and little endian bitstream */
00163     if (buf[0] == 0xff && buf[1] == 0x1f &&
00164         buf[2] == 0x00 && buf[3] == 0xe8 &&
00165         (buf[4] & 0xf0) == 0xf0 && buf[5] == 0x07)
00166     {
00167         int frame_size;
00168         dts_bitstream_init (state, buf, 0, 0);
00169         frame_size = syncinfo (state, flags, sample_rate,
00170                                bit_rate, frame_length);
00171         return frame_size;
00172     }
00173 
00174     /* 14 bits and big endian bitstream */
00175     if (buf[0] == 0x1f && buf[1] == 0xff &&
00176         buf[2] == 0xe8 && buf[3] == 0x00 &&
00177         buf[4] == 0x07 && (buf[5] & 0xf0) == 0xf0)
00178     {
00179         int frame_size;
00180         dts_bitstream_init (state, buf, 0, 1);
00181         frame_size = syncinfo (state, flags, sample_rate,
00182                                bit_rate, frame_length);
00183         return frame_size;
00184     }
00185 
00186     /* 16 bits and little endian bitstream */
00187     if (buf[0] == 0xfe && buf[1] == 0x7f &&
00188         buf[2] == 0x01 && buf[3] == 0x80)
00189     {
00190         int frame_size;
00191         dts_bitstream_init (state, buf, 1, 0);
00192         frame_size = syncinfo (state, flags, sample_rate,
00193                                bit_rate, frame_length);
00194         return frame_size;
00195     }
00196 
00197     /* 16 bits and big endian bitstream */
00198     if (buf[0] == 0x7f && buf[1] == 0xfe &&
00199         buf[2] == 0x80 && buf[3] == 0x01)
00200     {
00201         int frame_size;
00202         dts_bitstream_init (state, buf, 1, 1);
00203         frame_size = syncinfo (state, flags, sample_rate,
00204                                bit_rate, frame_length);
00205         return frame_size;
00206     }
00207 
00208     return 0;
00209 }
00210 
00211 int dts_frame (dts_state_t * state, uint8_t * buf, int * flags,
00212                level_t * level, sample_t bias)
00213 {
00214     int i, j;
00215     static float adj_table[] = { 1.0, 1.1250, 1.2500, 1.4375 };
00216 
00217     dts_bitstream_init (state, buf, state->word_mode, state->bigendian_mode);
00218 
00219     /* Sync code */
00220     bitstream_get (state, 32);
00221 
00222     /* Frame header */
00223     state->frame_type = bitstream_get (state, 1);
00224     state->samples_deficit = bitstream_get (state, 5) + 1;
00225     state->crc_present = bitstream_get (state, 1);
00226     state->sample_blocks = bitstream_get (state, 7) + 1;
00227     state->frame_size = bitstream_get (state, 14) + 1;
00228     state->amode = bitstream_get (state, 6);
00229     state->sample_rate = bitstream_get (state, 4);
00230     state->bit_rate = bitstream_get (state, 5);
00231 
00232     state->downmix = bitstream_get (state, 1);
00233     state->dynrange = bitstream_get (state, 1);
00234     state->timestamp = bitstream_get (state, 1);
00235     state->aux_data = bitstream_get (state, 1);
00236     state->hdcd = bitstream_get (state, 1);
00237     state->ext_descr = bitstream_get (state, 3);
00238     state->ext_coding = bitstream_get (state, 1);
00239     state->aspf = bitstream_get (state, 1);
00240     state->lfe = bitstream_get (state, 2);
00241     state->predictor_history = bitstream_get (state, 1);
00242 
00243     /* TODO: check CRC */
00244     if (state->crc_present) state->header_crc = bitstream_get (state, 16);
00245 
00246     state->multirate_inter = bitstream_get (state, 1);
00247     state->version = bitstream_get (state, 4);
00248     state->copy_history = bitstream_get (state, 2);
00249     state->source_pcm_res = bitstream_get (state, 3);
00250     state->front_sum = bitstream_get (state, 1);
00251     state->surround_sum = bitstream_get (state, 1);
00252     state->dialog_norm = bitstream_get (state, 4);
00253 
00254     /* FIME: channels mixing levels */
00255     state->clev = state->slev = 1;
00256     state->output = dts_downmix_init (state->amode, *flags, level,
00257                                       state->clev, state->slev);
00258     if (state->output < 0)
00259         return 1;
00260 
00261     if (state->lfe && (*flags & DTS_LFE))
00262         state->output |= DTS_LFE;
00263 
00264     *flags = state->output;
00265 
00266     state->dynrng = state->level = MUL_C (*level, 2);
00267     state->bias = bias;
00268     state->dynrnge = 1;
00269     state->dynrngcall = NULL;
00270 
00271 #ifdef DEBUG
00272     fprintf (stderr, "frame type: %i\n", state->frame_type);
00273     fprintf (stderr, "samples deficit: %i\n", state->samples_deficit);
00274     fprintf (stderr, "crc present: %i\n", state->crc_present);
00275     fprintf (stderr, "sample blocks: %i (%i samples)\n",
00276              state->sample_blocks, state->sample_blocks * 32);
00277     fprintf (stderr, "frame size: %i bytes\n", state->frame_size);
00278     fprintf (stderr, "amode: %i (%i channels)\n",
00279              state->amode, dts_channels[state->amode]);
00280     fprintf (stderr, "sample rate: %i (%i Hz)\n",
00281              state->sample_rate, dts_sample_rates[state->sample_rate]);
00282     fprintf (stderr, "bit rate: %i (%i bits/s)\n",
00283              state->bit_rate, dts_bit_rates[state->bit_rate]);
00284     fprintf (stderr, "downmix: %i\n", state->downmix);
00285     fprintf (stderr, "dynrange: %i\n", state->dynrange);
00286     fprintf (stderr, "timestamp: %i\n", state->timestamp);
00287     fprintf (stderr, "aux_data: %i\n", state->aux_data);
00288     fprintf (stderr, "hdcd: %i\n", state->hdcd);
00289     fprintf (stderr, "ext descr: %i\n", state->ext_descr);
00290     fprintf (stderr, "ext coding: %i\n", state->ext_coding);
00291     fprintf (stderr, "aspf: %i\n", state->aspf);
00292     fprintf (stderr, "lfe: %i\n", state->lfe);
00293     fprintf (stderr, "predictor history: %i\n", state->predictor_history);
00294     fprintf (stderr, "header crc: %i\n", state->header_crc);
00295     fprintf (stderr, "multirate inter: %i\n", state->multirate_inter);
00296     fprintf (stderr, "version number: %i\n", state->version);
00297     fprintf (stderr, "copy history: %i\n", state->copy_history);
00298     fprintf (stderr, "source pcm resolution: %i (%i bits/sample)\n",
00299              state->source_pcm_res,
00300              dts_bits_per_sample[state->source_pcm_res]);
00301     fprintf (stderr, "front sum: %i\n", state->front_sum);
00302     fprintf (stderr, "surround sum: %i\n", state->surround_sum);
00303     fprintf (stderr, "dialog norm: %i\n", state->dialog_norm);
00304     fprintf (stderr, "\n");
00305 #endif
00306 
00307     /* Primary audio coding header */
00308     state->subframes = bitstream_get (state, 4) + 1;
00309     state->prim_channels = bitstream_get (state, 3) + 1;
00310 
00311 #ifdef DEBUG
00312     fprintf (stderr, "subframes: %i\n", state->subframes);
00313     fprintf (stderr, "prim channels: %i\n", state->prim_channels);
00314 #endif
00315 
00316     for (i = 0; i < state->prim_channels; i++)
00317     {
00318         state->subband_activity[i] = bitstream_get (state, 5) + 2;
00319 #ifdef DEBUG
00320         fprintf (stderr, "subband activity: %i\n", state->subband_activity[i]);
00321 #endif
00322         if (state->subband_activity[i] > DTS_SUBBANDS)
00323             state->subband_activity[i] = DTS_SUBBANDS;
00324     }
00325     for (i = 0; i < state->prim_channels; i++)
00326     {
00327         state->vq_start_subband[i] = bitstream_get (state, 5) + 1;
00328 #ifdef DEBUG
00329         fprintf (stderr, "vq start subband: %i\n", state->vq_start_subband[i]);
00330 #endif
00331         if (state->vq_start_subband[i] > DTS_SUBBANDS)
00332             state->vq_start_subband[i] = DTS_SUBBANDS;
00333     }
00334     for (i = 0; i < state->prim_channels; i++)
00335     {
00336         state->joint_intensity[i] = bitstream_get (state, 3);
00337 #ifdef DEBUG
00338         fprintf (stderr, "joint intensity: %i\n", state->joint_intensity[i]);
00339         if (state->joint_intensity[i]) {fprintf (stderr, "JOINTINTENSITY\n");}
00340 #endif
00341     }
00342     for (i = 0; i < state->prim_channels; i++)
00343     {
00344         state->transient_huffman[i] = bitstream_get (state, 2);
00345 #ifdef DEBUG
00346         fprintf (stderr, "transient mode codebook: %i\n",
00347                  state->transient_huffman[i]);
00348 #endif
00349     }
00350     for (i = 0; i < state->prim_channels; i++)
00351     {
00352         state->scalefactor_huffman[i] = bitstream_get (state, 3);
00353 #ifdef DEBUG
00354         fprintf (stderr, "scale factor codebook: %i\n",
00355                  state->scalefactor_huffman[i]);
00356 #endif
00357     }
00358     for (i = 0; i < state->prim_channels; i++)
00359     {
00360         state->bitalloc_huffman[i] = bitstream_get (state, 3);
00361         /* if (state->bitalloc_huffman[i] == 7) bailout */
00362 #ifdef DEBUG
00363         fprintf (stderr, "bit allocation quantizer: %i\n",
00364                  state->bitalloc_huffman[i]);
00365 #endif
00366     }
00367 
00368     /* Get codebooks quantization indexes */
00369     for (i = 0; i < state->prim_channels; i++)
00370     {
00371         state->quant_index_huffman[i][0] = 0; /* Not transmitted */
00372         state->quant_index_huffman[i][1] = bitstream_get (state, 1);
00373     }
00374     for (j = 2; j < 6; j++)
00375         for (i = 0; i < state->prim_channels; i++)
00376             state->quant_index_huffman[i][j] = bitstream_get (state, 2);
00377     for (j = 6; j < 11; j++)
00378         for (i = 0; i < state->prim_channels; i++)
00379             state->quant_index_huffman[i][j] = bitstream_get (state, 3);
00380     for (j = 11; j < 27; j++)
00381         for (i = 0; i < state->prim_channels; i++)
00382             state->quant_index_huffman[i][j] = 0; /* Not transmitted */
00383 
00384 #ifdef DEBUG
00385     for (i = 0; i < state->prim_channels; i++)
00386     {
00387         fprintf( stderr, "quant index huff:" );
00388         for (j = 0; j < 11; j++)
00389             fprintf (stderr, " %i", state->quant_index_huffman[i][j]);
00390         fprintf (stderr, "\n");
00391     }
00392 #endif
00393 
00394     /* Get scale factor adjustment */
00395     for (j = 0; j < 11; j++)
00396     {
00397         for (i = 0; i < state->prim_channels; i++)
00398             state->scalefactor_adj[i][j] = 1;
00399     }
00400     for (i = 0; i < state->prim_channels; i++)
00401     {
00402         if (state->quant_index_huffman[i][1] == 0)
00403         {
00404             /* Transmitted only if quant_index_huffman=0 (Huffman code used) */
00405             state->scalefactor_adj[i][1] = adj_table[bitstream_get (state, 2)];
00406         }
00407     }
00408     for (j = 2; j < 6; j++)
00409         for (i = 0; i < state->prim_channels; i++)
00410             if (state->quant_index_huffman[i][j] < 3)
00411             {
00412                 /* Transmitted only if quant_index_huffman < 3 */
00413                 state->scalefactor_adj[i][j] =
00414                     adj_table[bitstream_get (state, 2)];
00415             }
00416     for (j = 6; j < 11; j++)
00417         for (i = 0; i < state->prim_channels; i++)
00418             if (state->quant_index_huffman[i][j] < 7)
00419             {
00420                 /* Transmitted only if quant_index_huffman < 7 */
00421                 state->scalefactor_adj[i][j] =
00422                     adj_table[bitstream_get (state, 2)];
00423             }
00424 
00425 #ifdef DEBUG
00426     for (i = 0; i < state->prim_channels; i++)
00427     {
00428         fprintf (stderr, "scalefac adj:");
00429         for (j = 0; j < 11; j++)
00430             fprintf (stderr, " %1.3f", state->scalefactor_adj[i][j]);
00431         fprintf (stderr, "\n");
00432     }
00433 #endif
00434 
00435     if (state->crc_present)
00436     {
00437         /* Audio header CRC check */
00438         bitstream_get (state, 16);
00439     }
00440 
00441     state->current_subframe = 0;
00442     state->current_subsubframe = 0;
00443 
00444     return 0;
00445 }
00446 
00447 int dts_subframe_header (dts_state_t * state)
00448 {
00449     /* Primary audio coding side information */
00450     int j, k;
00451 
00452     /* Subsubframe count */
00453     state->subsubframes = bitstream_get (state, 2) + 1;
00454 #ifdef DEBUG
00455     fprintf (stderr, "subsubframes: %i\n", state->subsubframes);
00456 #endif
00457 
00458     /* Partial subsubframe sample count */
00459     state->partial_samples = bitstream_get (state, 3);
00460 #ifdef DEBUG
00461     fprintf (stderr, "partial samples: %i\n", state->partial_samples);
00462 #endif
00463 
00464     /* Get prediction mode for each subband */
00465     for (j = 0; j < state->prim_channels; j++)
00466     {
00467         for (k = 0; k < state->subband_activity[j]; k++)
00468             state->prediction_mode[j][k] = bitstream_get (state, 1);
00469 #ifdef DEBUG
00470         fprintf (stderr, "prediction mode:");
00471         for (k = 0; k < state->subband_activity[j]; k++)
00472             fprintf (stderr, " %i", state->prediction_mode[j][k]);
00473         fprintf (stderr, "\n");
00474 #endif
00475     }
00476 
00477     /* Get prediction codebook */
00478     for (j = 0; j < state->prim_channels; j++)
00479     {
00480         for (k = 0; k < state->subband_activity[j]; k++)
00481         {
00482             if (state->prediction_mode[j][k] > 0)
00483             {
00484                 /* (Prediction coefficient VQ address) */
00485                 state->prediction_vq[j][k] = bitstream_get (state, 12);
00486 #ifdef DEBUG
00487                 fprintf (stderr, "prediction coefs: %f, %f, %f, %f\n",
00488                          (double)adpcm_vb[state->prediction_vq[j][k]][0]/8192,
00489                          (double)adpcm_vb[state->prediction_vq[j][k]][1]/8192,
00490                          (double)adpcm_vb[state->prediction_vq[j][k]][2]/8192,
00491                          (double)adpcm_vb[state->prediction_vq[j][k]][3]/8192);
00492 #endif
00493             }
00494         }
00495     }
00496 
00497     /* Bit allocation index */
00498     for (j = 0; j < state->prim_channels; j++)
00499     {
00500         for (k = 0; k < state->vq_start_subband[j]; k++)
00501         {
00502             if (state->bitalloc_huffman[j] == 6)
00503                 state->bitalloc[j][k] = bitstream_get (state, 5);
00504             else if (state->bitalloc_huffman[j] == 5)
00505                 state->bitalloc[j][k] = bitstream_get (state, 4);
00506             else
00507             {
00508                 state->bitalloc[j][k] = InverseQ (state,
00509                     bitalloc_12[state->bitalloc_huffman[j]]);
00510             }
00511 
00512             if (state->bitalloc[j][k] > 26)
00513             {
00514                 fprintf (stderr, "bitalloc index [%i][%i] too big (%i)\n",
00515                          j, k, state->bitalloc[j][k]);
00516                 return -1;
00517             }
00518         }
00519 
00520 #ifdef DEBUG
00521         fprintf (stderr, "bitalloc index: ");
00522         for (k = 0; k < state->vq_start_subband[j]; k++)
00523             fprintf (stderr, "%2.2i ", state->bitalloc[j][k]);
00524         fprintf (stderr, "\n");
00525 #endif
00526     }
00527 
00528     /* Transition mode */
00529     for (j = 0; j < state->prim_channels; j++)
00530     {
00531         for (k = 0; k < state->subband_activity[j]; k++)
00532         {
00533             state->transition_mode[j][k] = 0;
00534             if (state->subsubframes > 1 &&
00535                 k < state->vq_start_subband[j] &&
00536                 state->bitalloc[j][k] > 0)
00537             {
00538                 state->transition_mode[j][k] = InverseQ (state,
00539                     tmode[state->transient_huffman[j]]);
00540             }
00541         }
00542 #ifdef DEBUG
00543         fprintf (stderr, "Transition mode:");
00544         for (k = 0; k < state->subband_activity[j]; k++)
00545             fprintf (stderr, " %i", state->transition_mode[j][k]);
00546         fprintf (stderr, "\n");
00547 #endif
00548     }
00549 
00550     /* Scale factors */
00551     for (j = 0; j < state->prim_channels; j++)
00552     {
00553         int *scale_table;
00554         int scale_sum;
00555 
00556         for (k = 0; k < state->subband_activity[j]; k++)
00557         {
00558             state->scale_factor[j][k][0] = 0;
00559             state->scale_factor[j][k][1] = 0;
00560         }
00561 
00562         if (state->scalefactor_huffman[j] == 6)
00563             scale_table = scale_factor_quant7;
00564         else
00565             scale_table = scale_factor_quant6;
00566 
00567         /* When huffman coded, only the difference is encoded */
00568         scale_sum = 0;
00569 
00570         for (k = 0; k < state->subband_activity[j]; k++)
00571         {
00572             if (k >= state->vq_start_subband[j] || state->bitalloc[j][k] > 0)
00573             {
00574                 if (state->scalefactor_huffman[j] < 5)
00575                 {
00576                     /* huffman encoded */
00577                     scale_sum += InverseQ (state,
00578                         scales_129[state->scalefactor_huffman[j]]);
00579                 }
00580                 else if (state->scalefactor_huffman[j] == 5)
00581                 {
00582                     scale_sum = bitstream_get (state, 6);
00583                 }
00584                 else if (state->scalefactor_huffman[j] == 6)
00585                 {
00586                     scale_sum = bitstream_get (state, 7);
00587                 }
00588 
00589                 state->scale_factor[j][k][0] = scale_table[scale_sum];
00590             }
00591 
00592             if (k < state->vq_start_subband[j] && state->transition_mode[j][k])
00593             {
00594                 /* Get second scale factor */
00595                 if (state->scalefactor_huffman[j] < 5)
00596                 {
00597                     /* huffman encoded */
00598                     scale_sum += InverseQ (state,
00599                         scales_129[state->scalefactor_huffman[j]]);
00600                 }
00601                 else if (state->scalefactor_huffman[j] == 5)
00602                 {
00603                     scale_sum = bitstream_get (state, 6);
00604                 }
00605                 else if (state->scalefactor_huffman[j] == 6)
00606                 {
00607                     scale_sum = bitstream_get (state, 7);
00608                 }
00609 
00610                 state->scale_factor[j][k][1] = scale_table[scale_sum];
00611             }
00612         }
00613 
00614 #ifdef DEBUG
00615         fprintf (stderr, "Scale factor:");
00616         for (k = 0; k < state->subband_activity[j]; k++)
00617         {
00618             if (k >= state->vq_start_subband[j] || state->bitalloc[j][k] > 0)
00619                 fprintf (stderr, " %i", state->scale_factor[j][k][0]);
00620             if (k < state->vq_start_subband[j] && state->transition_mode[j][k])
00621                 fprintf (stderr, " %i(t)", state->scale_factor[j][k][1]);
00622         }
00623         fprintf (stderr, "\n");
00624 #endif
00625     }
00626 
00627     /* Joint subband scale factor codebook select */
00628     for (j = 0; j < state->prim_channels; j++)
00629     {
00630         /* Transmitted only if joint subband coding enabled */
00631         if (state->joint_intensity[j] > 0)
00632             state->joint_huff[j] = bitstream_get (state, 3);
00633     }
00634 
00635     /* Scale factors for joint subband coding */
00636     for (j = 0; j < state->prim_channels; j++)
00637     {
00638         int source_channel;
00639 
00640         /* Transmitted only if joint subband coding enabled */
00641         if (state->joint_intensity[j] > 0)
00642         {
00643             int scale = 0;
00644             source_channel = state->joint_intensity[j] - 1;
00645 
00646             /* When huffman coded, only the difference is encoded
00647              * (is this valid as well for joint scales ???) */
00648 
00649             for (k = state->subband_activity[j];
00650                  k < state->subband_activity[source_channel]; k++)
00651             {
00652                 if (state->joint_huff[j] < 5)
00653                 {
00654                     /* huffman encoded */
00655                     scale = InverseQ (state,
00656                         scales_129[state->joint_huff[j]]);
00657                 }
00658                 else if (state->joint_huff[j] == 5)
00659                 {
00660                     scale = bitstream_get (state, 6);
00661                 }
00662                 else if (state->joint_huff[j] == 6)
00663                 {
00664                     scale = bitstream_get (state, 7);
00665                 }
00666 
00667                 scale += 64; /* bias */
00668                 state->joint_scale_factor[j][k] = scale;/*joint_scale_table[scale];*/
00669             }
00670 
00671             if (!state->debug_flag & 0x02)
00672             {
00673                 fprintf (stderr, "Joint stereo coding not supported\n");
00674                 state->debug_flag |= 0x02;
00675             }
00676 
00677 #ifdef DEBUG
00678             fprintf (stderr, "Joint scale factor index:\n");
00679             for (k = state->subband_activity[j];
00680                  k < state->subband_activity[source_channel]; k++)
00681                 fprintf (stderr, " %i", state->joint_scale_factor[j][k]);
00682             fprintf (stderr, "\n");
00683 #endif
00684         }
00685     }
00686 
00687     /* Stereo downmix coefficients */
00688     if (state->prim_channels > 2 && state->downmix)
00689     {
00690         for (j = 0; j < state->prim_channels; j++)
00691         {
00692             state->downmix_coef[j][0] = bitstream_get (state, 7);
00693             state->downmix_coef[j][1] = bitstream_get (state, 7);
00694         }
00695     }
00696 
00697     /* Dynamic range coefficient */
00698     if (state->dynrange) state->dynrange_coef = bitstream_get (state, 8);
00699 
00700     /* Side information CRC check word */
00701     if (state->crc_present)
00702     {
00703         bitstream_get (state, 16);
00704     }
00705 
00706     /*
00707      * Primary audio data arrays
00708      */
00709 
00710     /* VQ encoded high frequency subbands */
00711     for (j = 0; j < state->prim_channels; j++)
00712     {
00713         for (k = state->vq_start_subband[j];
00714              k < state->subband_activity[j]; k++)
00715         {
00716             /* 1 vector -> 32 samples */
00717             state->high_freq_vq[j][k] = bitstream_get (state, 10);
00718 
00719 #ifdef DEBUG
00720             fprintf( stderr, "VQ index: %i\n", state->high_freq_vq[j][k] );
00721 #endif
00722         }
00723     }
00724 
00725     /* Low frequency effect data */
00726     if (state->lfe)
00727     {
00728         /* LFE samples */
00729         int lfe_samples = 2 * state->lfe * state->subsubframes;
00730         double lfe_scale;
00731 
00732         for (j = lfe_samples; j < lfe_samples * 2; j++)
00733         {
00734             /* Signed 8 bits int */
00735             state->lfe_data[j] =
00736                 (signed int)(signed char)bitstream_get (state, 8);
00737         }
00738 
00739         /* Scale factor index */
00740         state->lfe_scale_factor =
00741             scale_factor_quant7[bitstream_get (state, 8)];
00742 
00743         /* Quantization step size * scale factor */
00744         lfe_scale = 0.035 * state->lfe_scale_factor;
00745 
00746         for (j = lfe_samples; j < lfe_samples * 2; j++)
00747             state->lfe_data[j] *= lfe_scale;
00748 
00749 #ifdef DEBUG
00750         fprintf (stderr, "LFE samples:\n");
00751         for (j = lfe_samples; j < lfe_samples * 2; j++)
00752             fprintf (stderr, " %f", state->lfe_data[j]);
00753         fprintf (stderr, "\n");
00754 #endif
00755 
00756     }
00757 
00758     return 0;
00759 }
00760 
00761 int dts_subsubframe (dts_state_t * state)
00762 {
00763     int k, l;
00764     int subsubframe = state->current_subsubframe;
00765 
00766     double *quant_step_table;
00767 
00768     /* FIXME */
00769     double subband_samples[DTS_PRIM_CHANNELS_MAX][DTS_SUBBANDS][8];
00770 
00771     /*
00772      * Audio data
00773      */
00774 
00775     /* Select quantization step size table */
00776     if (state->bit_rate == 0x1f) 
00777         quant_step_table = lossless_quant_d;
00778     else
00779         quant_step_table = lossy_quant_d;
00780 
00781     for (k = 0; k < state->prim_channels; k++)
00782     {
00783         for (l = 0; l < state->vq_start_subband[k] ; l++)
00784         {
00785             int m;
00786 
00787             /* Select the mid-tread linear quantizer */
00788             int abits = state->bitalloc[k][l];
00789 
00790             double quant_step_size = quant_step_table[abits];
00791             double rscale;
00792 
00793             /*
00794              * Determine quantization index code book and its type 
00795              */
00796 
00797             /* Select quantization index code book */
00798             int sel = state->quant_index_huffman[k][abits]; 
00799 
00800             /* Determine its type */
00801             int q_type = 1; /* (Assume Huffman type by default) */
00802             if (abits >= 11 || !bitalloc_select[abits][sel])
00803             {
00804                 /* Not Huffman type */
00805                 if (abits <= 7) q_type = 3; /* Block code */
00806                 else q_type = 2; /* No further encoding */
00807             }
00808 
00809             if (abits == 0) q_type = 0; /* No bits allocated */
00810 
00811             /*
00812              * Extract bits from the bit stream 
00813              */
00814             switch (q_type)
00815             {
00816             case 0: /* No bits allocated */
00817                 for (m=0; m<8; m++)
00818                     subband_samples[k][l][m] = 0;
00819                 break;
00820 
00821             case 1: /* Huffman code */
00822                 for (m=0; m<8; m++)
00823                     subband_samples[k][l][m] =
00824                         InverseQ (state, bitalloc_select[abits][sel]);
00825                 break;
00826 
00827             case 2: /* No further encoding */
00828                 for (m=0; m<8; m++)
00829                 {
00830                     /* Extract (signed) quantization index */
00831                     int q_index = bitstream_get (state, abits - 3);
00832                     if( q_index & (1 << (abits - 4)) )
00833                     {
00834                         q_index = (1 << (abits - 3)) - q_index;
00835                         q_index = -q_index;
00836                     }
00837                     subband_samples[k][l][m] = q_index;
00838                 }
00839                 break;
00840 
00841             case 3: /* Block code */
00842                 {
00843                     int block_code1, block_code2, size, levels;
00844                     int block[8];
00845 
00846                     switch (abits)
00847                     {
00848                     case 1:
00849                         size = 7;
00850                         levels = 3;
00851                         break;
00852                     case 2:
00853                         size = 10;
00854                         levels = 5;
00855                         break;
00856                     case 3:
00857                         size = 12;
00858                         levels = 7;
00859                         break;
00860                     case 4:
00861                         size = 13;
00862                         levels = 9;
00863                         break;
00864                     case 5:
00865                         size = 15;
00866                         levels = 13;
00867                         break;
00868                     case 6:
00869                         size = 17;
00870                         levels = 17;
00871                         break;
00872                     case 7:
00873                     default:
00874                         size = 19;
00875                         levels = 25;
00876                         break;
00877                     }
00878 
00879                     block_code1 = bitstream_get (state, size);
00880                     /* Should test return value */
00881                     decode_blockcode (block_code1, levels, block);
00882                     block_code2 = bitstream_get (state, size);
00883                     decode_blockcode (block_code2, levels, &block[4]);
00884                     for (m=0; m<8; m++)
00885                         subband_samples[k][l][m] = block[m];
00886 
00887                 }
00888                 break;
00889 
00890             default: /* Undefined */
00891                 fprintf (stderr, "Unknown quantization index codebook");
00892                 return -1;
00893             }
00894 
00895             /*
00896              * Account for quantization step and scale factor
00897              */
00898 
00899             /* Deal with transients */
00900             if (state->transition_mode[k][l] &&
00901                 subsubframe >= state->transition_mode[k][l])
00902                 rscale = quant_step_size * state->scale_factor[k][l][1];
00903             else
00904                 rscale = quant_step_size * state->scale_factor[k][l][0];
00905 
00906             /* Adjustment */
00907             rscale *= state->scalefactor_adj[k][sel];
00908             for (m=0; m<8; m++) subband_samples[k][l][m] *= rscale;
00909 
00910             /*
00911              * Inverse ADPCM if in prediction mode
00912              */
00913             if (state->prediction_mode[k][l])
00914             {
00915                 int n;
00916                 for (m=0; m<8; m++)
00917                 {
00918                     for (n=1; n<=4; n++)
00919                         if (m-n >= 0)
00920                             subband_samples[k][l][m] +=
00921                               (adpcm_vb[state->prediction_vq[k][l]][n-1] *
00922                                 subband_samples[k][l][m-n]/8192);
00923                         else if (state->predictor_history)
00924                             subband_samples[k][l][m] +=
00925                               (adpcm_vb[state->prediction_vq[k][l]][n-1] *
00926                                state->subband_samples_hist[k][l][m-n+4]/8192);
00927                 }
00928             }
00929         }
00930 
00931         /*
00932          * Decode VQ encoded high frequencies
00933          */
00934         for (l = state->vq_start_subband[k];
00935              l < state->subband_activity[k]; l++)
00936         {
00937             /* 1 vector -> 32 samples but we only need the 8 samples
00938              * for this subsubframe. */
00939             int m;
00940 
00941             if (!state->debug_flag & 0x01)
00942             {
00943                 fprintf (stderr, "Stream with high frequencies VQ coding\n");
00944                 state->debug_flag |= 0x01;
00945             }
00946 
00947             for (m=0; m<8; m++)
00948             {
00949                 subband_samples[k][l][m] = 
00950                     high_freq_vq[state->high_freq_vq[k][l]][subsubframe*8+m]
00951                         * (double)state->scale_factor[k][l][0] / 16.0;
00952             }
00953         }
00954     }
00955 
00956     /* Check for DSYNC after subsubframe */
00957     if (state->aspf || subsubframe == state->subsubframes - 1)
00958     {
00959         if (0xFFFF == bitstream_get (state, 16)) /* 0xFFFF */
00960         {
00961 #ifdef DEBUG
00962             fprintf( stderr, "Got subframe DSYNC\n" );
00963 #endif
00964         }
00965         else
00966         {
00967             fprintf( stderr, "Didn't get subframe DSYNC\n" );
00968         }
00969     }
00970 
00971     /* Backup predictor history for adpcm */
00972     for (k = 0; k < state->prim_channels; k++)
00973     {
00974         for (l = 0; l < state->vq_start_subband[k] ; l++)
00975         {
00976             int m;
00977             for (m = 0; m < 4; m++)
00978                 state->subband_samples_hist[k][l][m] =
00979                     subband_samples[k][l][4+m];
00980         }
00981     }
00982 
00983     /* 32 subbands QMF */
00984     for (k = 0; k < state->prim_channels; k++)
00985     {
00986         static double pcm_to_float[8] =
00987             {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};
00988 
00989         qmf_32_subbands (state, k,
00990                          subband_samples[k],
00991                          &state->samples[256*k],
00992           /*WTF ???*/    32768.0*3/2/*pcm_to_float[state->source_pcm_res]*/,
00993                          0/*state->bias*/);
00994     }
00995 
00996     /* Down/Up mixing */
00997     if (state->prim_channels < dts_channels[state->output & DTS_CHANNEL_MASK])
00998     {
00999         dts_upmix (state->samples, state->amode, state->output);
01000     } else
01001     if (state->prim_channels > dts_channels[state->output & DTS_CHANNEL_MASK])
01002     {
01003         dts_downmix (state->samples, state->amode, state->output, state->bias,
01004                      state->clev, state->slev);
01005     }
01006 
01007     /* Generate LFE samples for this subsubframe FIXME!!! */
01008     if (state->output & DTS_LFE)
01009     {
01010         int lfe_samples = 2 * state->lfe * state->subsubframes;
01011         int i_channels = dts_channels[state->output & DTS_CHANNEL_MASK];
01012 
01013         lfe_interpolation_fir (state->lfe, 2 * state->lfe,
01014                                state->lfe_data + lfe_samples +
01015                                2 * state->lfe * subsubframe,
01016                                &state->samples[256*i_channels],
01017                                8388608.0, state->bias);
01018         /* Outputs 20bits pcm samples */
01019     }
01020 
01021     return 0;
01022 }
01023 
01024 int dts_subframe_footer (dts_state_t * state)
01025 {
01026     int aux_data_count = 0, i;
01027     int lfe_samples;
01028 
01029     /*
01030      * Unpack optional information
01031      */
01032 
01033     /* Time code stamp */
01034     if (state->timestamp) bitstream_get (state, 32);
01035 
01036     /* Auxiliary data byte count */
01037     if (state->aux_data) aux_data_count = bitstream_get (state, 6);
01038 
01039     /* Auxiliary data bytes */
01040     for(i = 0; i < aux_data_count; i++)
01041         bitstream_get (state, 8);
01042 
01043     /* Optional CRC check bytes */
01044     if (state->crc_present && (state->downmix || state->dynrange))
01045         bitstream_get (state, 16);
01046 
01047     /* Backup LFE samples history */
01048     lfe_samples = 2 * state->lfe * state->subsubframes;
01049     for (i = 0; i < lfe_samples; i++)
01050     {
01051         state->lfe_data[i] = state->lfe_data[i+lfe_samples];
01052     }
01053 
01054 #ifdef DEBUG
01055     fprintf( stderr, "\n" );
01056 #endif
01057 
01058     return 0;
01059 }
01060 
01061 int dts_block (dts_state_t * state)
01062 {
01063     /* Sanity check */
01064     if (state->current_subframe >= state->subframes)
01065     {
01066         fprintf (stderr, "check failed: %i>%i",
01067                  state->current_subframe, state->subframes);
01068         return -1;
01069     }
01070 
01071     if (!state->current_subsubframe)
01072     {
01073 #ifdef DEBUG
01074         fprintf (stderr, "DSYNC dts_subframe_header\n");
01075 #endif
01076         /* Read subframe header */
01077         if (dts_subframe_header (state)) return -1;
01078     }
01079 
01080     /* Read subsubframe */
01081 #ifdef DEBUG
01082     fprintf (stderr, "DSYNC dts_subsubframe\n");
01083 #endif
01084     if (dts_subsubframe (state)) return -1;
01085 
01086     /* Update state */
01087     state->current_subsubframe++;
01088     if (state->current_subsubframe >= state->subsubframes)
01089     {
01090         state->current_subsubframe = 0;
01091         state->current_subframe++;
01092     }
01093     if (state->current_subframe >= state->subframes)
01094     {
01095 #ifdef DEBUG
01096         fprintf(stderr, "DSYNC dts_subframe_footer\n");
01097 #endif
01098         /* Read subframe footer */
01099         if (dts_subframe_footer (state)) return -1;
01100     }
01101 
01102     return 0;
01103 }
01104 
01105 /* Very compact version of the block code decoder that does not use table
01106  * look-up but is slightly slower */
01107 int decode_blockcode( int code, int levels, int *values )
01108 { 
01109     int i;
01110     int offset = (levels - 1) >> 1;
01111 
01112     for (i = 0; i < 4; i++)
01113     {
01114         values[i] = (code % levels) - offset;
01115         code /= levels;
01116     }
01117 
01118     if (code == 0)
01119         return 1;
01120     else
01121     {
01122         fprintf (stderr, "ERROR: block code look-up failed\n");
01123         return 0;
01124     }
01125 }
01126 
01127 static void pre_calc_cosmod( dts_state_t * state )
01128 {
01129     int i, j, k;
01130 
01131     for (j=0,k=0;k<16;k++)
01132         for (i=0;i<16;i++)
01133             state->cos_mod[j++] = cos((2*i+1)*(2*k+1)*M_PI/64);
01134 
01135     for (k=0;k<16;k++)
01136         for (i=0;i<16;i++)
01137             state->cos_mod[j++] = cos((i)*(2*k+1)*M_PI/32);
01138 
01139     for (k=0;k<16;k++)
01140         state->cos_mod[j++] = 0.25/(2*cos((2*k+1)*M_PI/128));
01141 
01142     for (k=0;k<16;k++)
01143         state->cos_mod[j++] = -0.25/(2.0*sin((2*k+1)*M_PI/128));
01144 }
01145 
01146 static void qmf_32_subbands (dts_state_t * state, int chans,
01147                              double samples_in[32][8], sample_t *samples_out,
01148                              double scale, sample_t bias)
01149 {
01150     double *prCoeff;
01151     int i, j, k;
01152     double raXin[32];
01153 
01154     double *subband_fir_hist = state->subband_fir_hist[chans];
01155     double *subband_fir_hist2 = state->subband_fir_noidea[chans];
01156 
01157     int nChIndex = 0, NumSubband = 32, nStart = 0, nEnd = 8, nSubIndex;
01158 
01159     /* Select filter */
01160     if (!state->multirate_inter) /* Non-perfect reconstruction */
01161         prCoeff = fir_32bands_nonperfect;
01162     else /* Perfect reconstruction */
01163         prCoeff = fir_32bands_perfect;
01164 
01165     /* Reconstructed channel sample index */
01166     for (nSubIndex=nStart; nSubIndex<nEnd; nSubIndex++)
01167     {
01168         double A[16], B[16], SUM[16], DIFF[16];
01169 
01170         /* Load in one sample from each subband */
01171         for (i=0; i<state->subband_activity[chans]; i++)
01172             raXin[i] = samples_in[i][nSubIndex];
01173 
01174         /* Clear inactive subbands */
01175         for (i=state->subband_activity[chans]; i<NumSubband; i++)
01176             raXin[i] = 0.0;
01177 
01178         /* Multiply by cosine modulation coefficients and
01179          * create temporary arrays SUM and DIFF */
01180         for (j=0,k=0;k<16;k++)
01181         {
01182             A[k] = 0.0;
01183             for (i=0;i<16;i++)
01184                 A[k]+=(raXin[2*i]+raXin[2*i+1])*state->cos_mod[j++];
01185         }
01186 
01187         for (k=0;k<16;k++)
01188         {
01189             B[k] = 0.0;
01190             for (i=0;i<16;i++)
01191             {
01192                 if(i>0) B[k]+=(raXin[2*i]+raXin[2*i-1])*state->cos_mod[j++];
01193                 else B[k]+=(raXin[2*i])*state->cos_mod[j++];
01194             }
01195             SUM[k]=A[k]+B[k];
01196             DIFF[k]=A[k]-B[k];
01197         }
01198 
01199         /* Store history */
01200         for (k=0;k<16;k++)
01201             subband_fir_hist[k]=state->cos_mod[j++]*SUM[k];
01202         for (k=0;k<16;k++)
01203             subband_fir_hist[32-k-1]=state->cos_mod[j++]*DIFF[k];
01204         /* Multiply by filter coefficients */
01205         for (k=31,i=0;i<32;i++,k--)
01206             for (j=0;j<512;j+=64)
01207                 subband_fir_hist2[i] += prCoeff[i+j]*
01208                    (subband_fir_hist[i+j] - subband_fir_hist[j+k]);
01209         for (k=31,i=0;i<32;i++,k--)
01210             for (j=0;j<512;j+=64)
01211                 subband_fir_hist2[32+i] += prCoeff[32+i+j]*
01212                     (-subband_fir_hist[i+j] - subband_fir_hist[j+k]);
01213 
01214         /* Create 32 PCM output samples */
01215         for (i=0;i<32;i++)
01216             samples_out[nChIndex++] = subband_fir_hist2[i] / scale + bias;
01217 
01218         /* Update working arrays */
01219         for (i=511;i>=32;i--)
01220             subband_fir_hist[i] = subband_fir_hist[i-32];
01221         for (i=0;i<NumSubband;i++)
01222             subband_fir_hist2[i] = subband_fir_hist2[i+32];
01223         for (i=0;i<NumSubband;i++)
01224             subband_fir_hist2[i+32] = 0.0;
01225     }
01226 }
01227 
01228 static void lfe_interpolation_fir (int nDecimationSelect, int nNumDeciSample,
01229                                    double *samples_in, sample_t *samples_out,
01230                                    double scale, sample_t bias)
01231 {
01232     /* samples_in: An array holding decimated samples.
01233      *   Samples in current subframe starts from samples_in[0],
01234      *   while samples_in[-1], samples_in[-2], ..., stores samples
01235      *   from last subframe as history.
01236      *
01237      * samples_out: An array holding interpolated samples
01238      */
01239 
01240     int nDeciFactor, k, J;
01241     double *prCoeff;
01242 
01243     int NumFIRCoef = 512; /* Number of FIR coefficients */
01244     int nInterpIndex = 0; /* Index to the interpolated samples */
01245     int nDeciIndex;
01246 
01247     /* Select decimation filter */
01248     if (nDecimationSelect==1)
01249     {
01250         /* 128 decimation */
01251         nDeciFactor = 128;
01252         /* Pointer to the FIR coefficients array */
01253         prCoeff = lfe_fir_128;
01254     } else {
01255         /* 64 decimation */
01256         nDeciFactor = 64;
01257         prCoeff = lfe_fir_64;
01258     }
01259 
01260     /* Interpolation */
01261     for (nDeciIndex=0; nDeciIndex<nNumDeciSample; nDeciIndex++)
01262     {
01263         /* One decimated sample generates nDeciFactor interpolated ones */
01264         for (k=0; k<nDeciFactor; k++)
01265         {
01266             /* Clear accumulation */
01267             double rTmp = 0.0;
01268 
01269             /* Accumulate */
01270             for (J=0; J<NumFIRCoef/nDeciFactor; J++)
01271                 rTmp += samples_in[nDeciIndex-J]*prCoeff[k+J*nDeciFactor];
01272 
01273             /* Save interpolated samples */
01274             samples_out[nInterpIndex++] = rTmp / scale + bias;
01275         }
01276     }
01277 }
01278 
01279 void dts_dynrng (dts_state_t * state,
01280                  level_t (* call) (level_t, void *), void * data)
01281 {
01282     state->dynrange = 0;
01283     if (call) {
01284         state->dynrange = 1;
01285         state->dynrngcall = call;
01286         state->dynrngdata = data;
01287     }
01288 }
01289 
01290 void dts_free (dts_state_t * state)
01291 {
01292     free (state->samples);
01293     free (state);
01294 }

Generated on Tue Dec 13 14:47:28 2005 for guliverkli by  doxygen 1.4.5