Main Page | Class List | Directories | File List | Class Members | File Members | Related Pages

mpc_decoder.c

Go to the documentation of this file.
00001 /*
00002   Copyright (c) 2005, The Musepack Development Team
00003   All rights reserved.
00004 
00005   Redistribution and use in source and binary forms, with or without
00006   modification, are permitted provided that the following conditions are
00007   met:
00008 
00009   * Redistributions of source code must retain the above copyright
00010   notice, this list of conditions and the following disclaimer.
00011 
00012   * Redistributions in binary form must reproduce the above
00013   copyright notice, this list of conditions and the following
00014   disclaimer in the documentation and/or other materials provided
00015   with the distribution.
00016 
00017   * Neither the name of the The Musepack Development Team nor the
00018   names of its contributors may be used to endorse or promote
00019   products derived from this software without specific prior
00020   written permission.
00021 
00022   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00026   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00028   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00031   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00032   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 */
00034 
00037 
00038 #include <mpcdec/mpcdec.h>
00039 #include <mpcdec/internal.h>
00040 #include <mpcdec/requant.h>
00041 #include <mpcdec/huffman.h>
00042 
00043 //------------------------------------------------------------------------------
00044 // types
00045 //------------------------------------------------------------------------------
00046 enum
00047     {
00048         EQ_TAP = 13,                        // length of FIR filter for EQ
00049         DELAY = ((EQ_TAP + 1) / 2),         // delay of FIR
00050         FIR_BANDS = 4,                      // number of subbands to be FIR filtered
00051         MEMSIZE = MPC_DECODER_MEMSIZE,      // overall buffer size
00052         MEMSIZE2 = (MEMSIZE/2),             // size of one buffer
00053         MEMMASK = (MEMSIZE-1)
00054     };
00055 
00056 //------------------------------------------------------------------------------
00057 // forward declarations
00058 //------------------------------------------------------------------------------
00059 void mpc_decoder_init_huffman_sv6(mpc_decoder *d);
00060 void mpc_decoder_init_huffman_sv7(mpc_decoder *d);
00061 void mpc_decoder_read_bitstream_sv6(mpc_decoder *d);
00062 void mpc_decoder_read_bitstream_sv7(mpc_decoder *d);
00063 void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING);
00064 mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample);
00065 void mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band);
00066 
00067 //------------------------------------------------------------------------------
00068 // utility functions
00069 //------------------------------------------------------------------------------
00070 static mpc_int32_t f_read(mpc_decoder *d, void *ptr, size_t size) 
00071 { 
00072     return d->r->read(d->r->data, ptr, size); 
00073 };
00074 
00075 static mpc_bool_t f_seek(mpc_decoder *d, mpc_int32_t offset) 
00076 { 
00077     return d->r->seek(d->r->data, offset); 
00078 };
00079 
00080 static mpc_int32_t f_read_dword(mpc_decoder *d, mpc_uint32_t * ptr, mpc_uint32_t count) 
00081 {
00082     count = f_read(d, ptr, count << 2) >> 2;
00083 #ifndef MPC_LITTLE_ENDIAN
00084     mpc_uint32_t n;
00085     for(n = 0; n< count; n++) {
00086         ptr[n] = mpc_swap32(ptr[n]);
00087     }
00088 #endif
00089     return count;
00090 }
00091 
00092 //------------------------------------------------------------------------------
00093 // huffman & bitstream functions
00094 //------------------------------------------------------------------------------
00095 static const mpc_uint32_t mask [33] = {
00096     0x00000000, 0x00000001, 0x00000003, 0x00000007,
00097     0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
00098     0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
00099     0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
00100     0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
00101     0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
00102     0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
00103     0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
00104     0xFFFFFFFF
00105 };
00106 
00107 /* F U N C T I O N S */
00108 
00109 // resets bitstream decoding
00110 static void
00111 mpc_decoder_reset_bitstream_decode(mpc_decoder *d) 
00112 {
00113     d->dword = 0;
00114     d->pos = 0;
00115     d->Zaehler = 0;
00116     d->WordsRead = 0;
00117 }
00118 
00119 // reports the number of read bits
00120 static mpc_uint32_t
00121 mpc_decoder_bits_read(mpc_decoder *d) 
00122 {
00123     return 32 * d->WordsRead + d->pos;
00124 }
00125 
00126 // read desired number of bits out of the bitstream
00127 static mpc_uint32_t
00128 mpc_decoder_bitstream_read(mpc_decoder *d, const mpc_uint32_t bits) 
00129 {
00130     mpc_uint32_t out = d->dword;
00131 
00132     d->pos += bits;
00133 
00134     if (d->pos < 32) {
00135         out >>= (32 - d->pos);
00136     }
00137     else {
00138         d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
00139         d->pos -= 32;
00140         if (d->pos) {
00141             out <<= d->pos;
00142             out |= d->dword >> (32 - d->pos);
00143         }
00144         ++(d->WordsRead);
00145     }
00146 
00147     return out & mask[bits];
00148 }
00149 
00150 // decode SCFI-bundle (sv4,5,6)
00151 static void
00152 mpc_decoder_scfi_bundle_read(
00153     mpc_decoder *d,
00154     HuffmanTyp* Table, mpc_int32_t* SCFI, mpc_int32_t* DSCF) 
00155 {
00156     // load preview and decode
00157     mpc_uint32_t code  = d->dword << d->pos;
00158     if (d->pos > 26) {
00159         code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
00160     }
00161     while (code < Table->Code) {
00162         Table++;
00163     }
00164 
00165     // set the new position within bitstream without performing a dummy-read
00166     if ((d->pos += Table->Length) >= 32) {
00167         d->pos -= 32;
00168         d->dword = d->Speicher[d->Zaehler = (d->Zaehler+1) & MEMMASK];
00169         ++(d->WordsRead);
00170     }
00171 
00172     *SCFI = Table->Value >> 1;
00173     *DSCF = Table->Value &  1;
00174 }
00175 
00176 static int
00177 mpc_decoder_huffman_typ_cmpfn(const void* p1, const void* p2)
00178 {
00179     if (((HuffmanTyp*) p1)->Code < ((HuffmanTyp*) p2)->Code ) return +1;
00180     if (((HuffmanTyp*) p1)->Code > ((HuffmanTyp*) p2)->Code ) return -1;
00181     return 0;
00182 }
00183 
00184 // sort huffman-tables by codeword
00185 // offset resulting value
00186 void
00187 mpc_decoder_resort_huff_tables(
00188     const mpc_uint32_t elements, HuffmanTyp* Table, const mpc_int32_t offset ) 
00189 {
00190     mpc_uint32_t  i;
00191 
00192     for ( i = 0; i < elements; i++ ) {
00193         Table[i].Code <<= 32 - Table[i].Length;
00194         Table[i].Value  =  i - offset;
00195     }
00196     qsort(Table, elements, sizeof(*Table), mpc_decoder_huffman_typ_cmpfn);
00197 }
00198 
00199 // basic huffman decoding routine
00200 // works with maximum lengths up to 14
00201 static mpc_int32_t
00202 mpc_decoder_huffman_decode(mpc_decoder *d, const HuffmanTyp *Table) 
00203 {
00204     // load preview and decode
00205     mpc_uint32_t code = d->dword << d->pos;
00206     if (d->pos > 18) {
00207         code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
00208     }
00209     while (code < Table->Code) {
00210         Table++;
00211     }
00212 
00213     // set the new position within bitstream without performing a dummy-read
00214     if ((d->pos += Table->Length) >= 32) {
00215         d->pos -= 32;
00216         d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
00217         ++(d->WordsRead);
00218     }
00219 
00220     return Table->Value;
00221 }
00222 
00223 // faster huffman through previewing less bits
00224 // works with maximum lengths up to 10
00225 static mpc_int32_t
00226 mpc_decoder_huffman_decode_fast(mpc_decoder *d, const HuffmanTyp* Table)
00227 {
00228     // load preview and decode
00229     mpc_uint32_t code  = d->dword << d->pos;
00230     if (d->pos > 22) {
00231         code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
00232     }
00233     while (code < Table->Code) {
00234         Table++;
00235     }
00236 
00237     // set the new position within bitstream without performing a dummy-read
00238     if ((d->pos += Table->Length) >= 32) {
00239         d->pos -= 32;
00240         d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
00241         ++(d->WordsRead);
00242     }
00243 
00244     return Table->Value;
00245 }
00246 
00247 // even faster huffman through previewing even less bits
00248 // works with maximum lengths up to 5
00249 static mpc_int32_t
00250 mpc_decoder_huffman_decode_faster(mpc_decoder *d, const HuffmanTyp* Table)
00251 {
00252     // load preview and decode
00253     mpc_uint32_t code  = d->dword << d->pos;
00254     if (d->pos > 27) {
00255         code |= d->Speicher[(d->Zaehler + 1) & MEMMASK] >> (32 - d->pos);
00256     }
00257     while (code < Table->Code) {
00258         Table++;
00259     }
00260 
00261     // set the new position within bitstream without performing a dummy-read
00262     if ((d->pos += Table->Length) >= 32) {
00263         d->pos -= 32;
00264         d->dword = d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK];
00265         ++(d->WordsRead);
00266     }
00267 
00268     return Table->Value;
00269 }
00270 
00271 static void
00272 mpc_decoder_reset_v(mpc_decoder *d) 
00273 {
00274     memset(d->V_L, 0, sizeof d->V_L);
00275     memset(d->V_R, 0, sizeof d->V_R);
00276 }
00277 
00278 static void
00279 mpc_decoder_reset_synthesis(mpc_decoder *d) 
00280 {
00281     mpc_decoder_reset_v(d);
00282 }
00283 
00284 static void
00285 mpc_decoder_reset_y(mpc_decoder *d) 
00286 {
00287     memset(d->Y_L, 0, sizeof d->Y_L);
00288     memset(d->Y_R, 0, sizeof d->Y_R);
00289 }
00290 
00291 static void
00292 mpc_decoder_reset_globals(mpc_decoder *d) 
00293 {
00294     mpc_decoder_reset_bitstream_decode(d);
00295 
00296     d->DecodedFrames  = 0;
00297     d->StreamVersion  = 0;
00298     d->MS_used        = 0;
00299 
00300     memset(d->Y_L          , 0, sizeof d->Y_L           );
00301     memset(d->Y_R          , 0, sizeof d->Y_R           );
00302     memset(d->SCF_Index_L     , 0, sizeof d->SCF_Index_L      );
00303     memset(d->SCF_Index_R     , 0, sizeof d->SCF_Index_R      );
00304     memset(d->Res_L           , 0, sizeof d->Res_L            );
00305     memset(d->Res_R           , 0, sizeof d->Res_R            );
00306     memset(d->SCFI_L          , 0, sizeof d->SCFI_L           );
00307     memset(d->SCFI_R          , 0, sizeof d->SCFI_R           );
00308     memset(d->DSCF_Flag_L     , 0, sizeof d->DSCF_Flag_L      );
00309     memset(d->DSCF_Flag_R     , 0, sizeof d->DSCF_Flag_R      );
00310     memset(d->DSCF_Reference_L, 0, sizeof d->DSCF_Reference_L );
00311     memset(d->DSCF_Reference_R, 0, sizeof d->DSCF_Reference_R );
00312     memset(d->Q               , 0, sizeof d->Q                );
00313     memset(d->MS_Flag         , 0, sizeof d->MS_Flag          );
00314 }
00315 
00316 mpc_uint32_t
00317 mpc_decoder_decode_frame(mpc_decoder *d, mpc_uint32_t *in_buffer,
00318                          mpc_uint32_t in_len, MPC_SAMPLE_FORMAT *out_buffer)
00319 {
00320   unsigned int i;
00321   mpc_decoder_reset_bitstream_decode(d);
00322   if (in_len > sizeof(d->Speicher)) in_len = sizeof(d->Speicher);
00323   memcpy(d->Speicher, in_buffer, in_len);
00324 #ifdef MPC_LITTLE_ENDIAN
00325   for (i = 0; i < (in_len + 3) / 4; i++)
00326     d->Speicher[i] = mpc_swap32(d->Speicher[i]);
00327 #endif
00328   d->dword = d->Speicher[0];
00329   switch (d->StreamVersion) {
00330     case 0x04:
00331     case 0x05:
00332     case 0x06:
00333         mpc_decoder_read_bitstream_sv6(d);
00334         break;
00335     case 0x07:
00336     case 0x17:
00337         mpc_decoder_read_bitstream_sv7(d);
00338         break;
00339     default:
00340         return (mpc_uint32_t)(-1);
00341   }
00342   mpc_decoder_requantisierung(d, d->Max_Band);
00343   mpc_decoder_synthese_filter_float(d, out_buffer);
00344   return mpc_decoder_bits_read(d);
00345 }
00346 
00347 static mpc_uint32_t
00348 mpc_decoder_decode_internal(mpc_decoder *d, MPC_SAMPLE_FORMAT *buffer) 
00349 {
00350     mpc_uint32_t output_frame_length = MPC_FRAME_LENGTH;
00351 
00352     mpc_uint32_t  FrameBitCnt = 0;
00353 
00354     if (d->DecodedFrames >= d->OverallFrames) {
00355         return (mpc_uint32_t)(-1);                           // end of file -> abort decoding
00356     }
00357 
00358     // read jump-info for validity check of frame
00359     d->FwdJumpInfo  = mpc_decoder_bitstream_read(d, 20);
00360 
00361     d->ActDecodePos = (d->Zaehler << 5) + d->pos;
00362 
00363     // decode data and check for validity of frame
00364     FrameBitCnt = mpc_decoder_bits_read(d);
00365     switch (d->StreamVersion) {
00366     case 0x04:
00367     case 0x05:
00368     case 0x06:
00369         mpc_decoder_read_bitstream_sv6(d);
00370         break;
00371     case 0x07:
00372     case 0x17:
00373         mpc_decoder_read_bitstream_sv7(d);
00374         break;
00375     default:
00376         return (mpc_uint32_t)(-1);
00377     }
00378     d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == d->FwdJumpInfo;
00379 
00380     // synthesize signal
00381     mpc_decoder_requantisierung(d, d->Max_Band);
00382 
00383     //if ( d->EQ_activated && PluginSettings.EQbyMPC )
00384     //    perform_EQ ();
00385 
00386     mpc_decoder_synthese_filter_float(d, buffer);
00387 
00388     d->DecodedFrames++;
00389 
00390     // cut off first MPC_DECODER_SYNTH_DELAY zero-samples
00391     if (d->DecodedFrames == d->OverallFrames  && d->StreamVersion >= 6) {        
00392         // reconstruct exact filelength
00393         mpc_int32_t  mod_block   = mpc_decoder_bitstream_read(d,  11);
00394         mpc_int32_t  FilterDecay;
00395 
00396         if (mod_block == 0) {
00397             // Encoder bugfix
00398             mod_block = 1152;                    
00399         }
00400         FilterDecay = (mod_block + MPC_DECODER_SYNTH_DELAY) % MPC_FRAME_LENGTH;
00401 
00402         // additional FilterDecay samples are needed for decay of synthesis filter
00403         if (MPC_DECODER_SYNTH_DELAY + mod_block >= MPC_FRAME_LENGTH) {
00404 
00405             // **********************************************************************
00406             // Rhoades 4/16/2002
00407             // Commented out are blocks of code which cause gapless playback to fail.
00408             // Temporary fix...
00409             // **********************************************************************
00410 
00411             if (!d->TrueGaplessPresent) {
00412                 mpc_decoder_reset_y(d);
00413             } 
00414             else {
00415                 //if ( MPC_FRAME_LENGTH != d->LastValidSamples ) {
00416                 mpc_decoder_bitstream_read(d, 20);
00417                 mpc_decoder_read_bitstream_sv7(d);
00418                 mpc_decoder_requantisierung(d, d->Max_Band);
00419                 //FilterDecay = d->LastValidSamples;
00420                 //}
00421                 //else {
00422                 //FilterDecay = 0;
00423                 //}
00424             }
00425 
00426             mpc_decoder_synthese_filter_float(d, buffer + 2304);
00427 
00428             output_frame_length = MPC_FRAME_LENGTH + FilterDecay;
00429         }
00430         else {                              // there are only FilterDecay samples needed for this frame
00431             output_frame_length = FilterDecay;
00432         }
00433     }
00434 
00435     if (d->samples_to_skip) {
00436         if (output_frame_length < d->samples_to_skip) {
00437             d->samples_to_skip -= output_frame_length;
00438             output_frame_length = 0;
00439         }
00440         else {
00441             output_frame_length -= d->samples_to_skip;
00442             memmove(
00443                 buffer, 
00444                 buffer + d->samples_to_skip * 2, 
00445                 output_frame_length * 2 * sizeof (MPC_SAMPLE_FORMAT));
00446             d->samples_to_skip = 0;
00447         }
00448     }
00449 
00450     return output_frame_length;
00451 }
00452 
00453 mpc_uint32_t mpc_decoder_decode(
00454     mpc_decoder *d,
00455     MPC_SAMPLE_FORMAT *buffer, 
00456     mpc_uint32_t *vbr_update_acc, 
00457     mpc_uint32_t *vbr_update_bits)
00458 {
00459     for(;;)
00460     {
00461         //const mpc_int32_t MaxBrokenFrames = 0; // PluginSettings.MaxBrokenFrames
00462 
00463         mpc_uint32_t RING = d->Zaehler;
00464         mpc_int32_t vbr_ring = (RING << 5) + d->pos;
00465 
00466         mpc_uint32_t valid_samples = mpc_decoder_decode_internal(d, buffer);
00467 
00468         if (valid_samples == (mpc_uint32_t)(-1) ) {
00469             return 0;
00470         }
00471 
00472         /**************** ERROR CONCEALMENT *****************/
00473         if (d->FrameWasValid == 0 ) {
00474             // error occurred in bitstream
00475             return (mpc_uint32_t)(-1);
00476         } 
00477         else {
00478             if (vbr_update_acc && vbr_update_bits) {
00479                 (*vbr_update_acc) ++;
00480                 vbr_ring = (d->Zaehler << 5) + d->pos - vbr_ring;
00481                 if (vbr_ring < 0) {
00482                     vbr_ring += 524288;
00483                 }
00484                 (*vbr_update_bits) += vbr_ring;
00485             }
00486 
00487         }
00488         mpc_decoder_update_buffer(d, RING);
00489 
00490         if (valid_samples > 0) {
00491             return valid_samples;
00492         }
00493     }
00494 }
00495 
00496 void
00497 mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band) 
00498 {
00499     mpc_int32_t     Band;
00500     mpc_int32_t     n;
00501     MPC_SAMPLE_FORMAT facL;
00502     MPC_SAMPLE_FORMAT facR;
00503     MPC_SAMPLE_FORMAT templ;
00504     MPC_SAMPLE_FORMAT tempr;
00505     MPC_SAMPLE_FORMAT* YL;
00506     MPC_SAMPLE_FORMAT* YR;
00507     mpc_int32_t*    L;
00508     mpc_int32_t*    R;
00509 
00510 #ifdef MPC_FIXED_POINT
00511 #if MPC_FIXED_POINT_FRACTPART == 14
00512 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
00513     MPC_MULTIPLY_EX(CcVal, d->SCF[SCF_idx], d->SCF_shift[SCF_idx])
00514 #else
00515 
00516 #error FIXME, Cc table is in 18.14 format
00517 
00518 #endif
00519 #else
00520 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
00521     MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
00522 #endif
00523     // requantization and scaling of subband-samples
00524     for ( Band = 0; Band <= Last_Band; Band++ ) {   // setting pointers
00525         YL = d->Y_L[0] + Band;
00526         YR = d->Y_R[0] + Band;
00527         L  = d->Q[Band].L;
00528         R  = d->Q[Band].R;
00529         /************************** MS-coded **************************/
00530         if ( d->MS_Flag [Band] ) {
00531             if ( d->Res_L [Band] ) {
00532                 if ( d->Res_R [Band] ) {    // M!=0, S!=0
00533                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
00534                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
00535                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00536                         *YL   = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
00537                         *YR   = templ - tempr;
00538                     }
00539                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
00540                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
00541                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
00542                         *YL   = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
00543                         *YR   = templ - tempr;
00544                     }
00545                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
00546                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
00547                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
00548                         *YL   = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
00549                         *YR   = templ - tempr;
00550                     }
00551                 } else {    // M!=0, S==0
00552                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
00553                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00554                         *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00555                     }
00556                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
00557                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
00558                         *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00559                     }
00560                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
00561                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
00562                         *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00563                     }
00564                 }
00565             } else {
00566                 if (d->Res_R[Band])    // M==0, S!=0
00567                 {
00568                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
00569                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00570                         *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
00571                     }
00572                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
00573                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
00574                         *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
00575                     }
00576                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
00577                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
00578                         *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
00579                     }
00580                 } else {    // M==0, S==0
00581                     for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
00582                         *YR = *YL = 0;
00583                     }
00584                 }
00585             }
00586         }
00587         /************************** LR-coded **************************/
00588         else {
00589             if ( d->Res_L [Band] ) {
00590                 if ( d->Res_R [Band] ) {    // L!=0, R!=0
00591                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
00592                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
00593                     for (n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00594                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00595                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00596                     }
00597                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
00598                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
00599                     for (; n < 24; n++, YL += 32, YR += 32 ) {
00600                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00601                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00602                     }
00603                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
00604                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
00605                     for (; n < 36; n++, YL += 32, YR += 32 ) {
00606                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00607                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00608                     }
00609                 } else {     // L!=0, R==0
00610                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
00611                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00612                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00613                         *YR = 0;
00614                     }
00615                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
00616                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
00617                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00618                         *YR = 0;
00619                     }
00620                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
00621                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
00622                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
00623                         *YR = 0;
00624                     }
00625                 }
00626             }
00627             else {
00628                 if ( d->Res_R [Band] ) {    // L==0, R!=0
00629                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
00630                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
00631                         *YL = 0;
00632                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00633                     }
00634                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
00635                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
00636                         *YL = 0;
00637                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00638                     }
00639                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
00640                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
00641                         *YL = 0;
00642                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
00643                     }
00644                 } else {    // L==0, R==0
00645                     for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
00646                         *YR = *YL = 0;
00647                     }
00648                 }
00649             }
00650         }
00651     }
00652 }
00653 
00654 /****************************************** SV 6 ******************************************/
00655 void
00656 mpc_decoder_read_bitstream_sv6(mpc_decoder *d) 
00657 {
00658     mpc_int32_t n,k;
00659     mpc_int32_t Max_used_Band=0;
00660     HuffmanTyp *Table;
00661     const HuffmanTyp *x1;
00662     const HuffmanTyp *x2;
00663     mpc_int32_t *L;
00664     mpc_int32_t *R;
00665     mpc_int32_t *ResL = d->Res_L;
00666     mpc_int32_t *ResR = d->Res_R;
00667 
00668     /************************ HEADER **************************/
00669     ResL = d->Res_L;
00670     ResR = d->Res_R;
00671     for (n=0; n <= d->Max_Band; ++n, ++ResL, ++ResR)
00672     {
00673         if      (n<11)           Table = d->Region_A;
00674         else if (n>=11 && n<=22) Table = d->Region_B;
00675         else /*if (n>=23)*/      Table = d->Region_C;
00676 
00677         *ResL = d->Q_res[n][mpc_decoder_huffman_decode(d, Table)];
00678         if (d->MS_used) {
00679             d->MS_Flag[n] = mpc_decoder_bitstream_read(d,  1);
00680         }
00681         *ResR = d->Q_res[n][mpc_decoder_huffman_decode(d, Table)];
00682 
00683         // only perform the following procedure up to the maximum non-zero subband
00684         if (*ResL || *ResR) Max_used_Band = n;
00685     }
00686 
00687     /************************* SCFI-Bundle *****************************/
00688     ResL = d->Res_L;
00689     ResR = d->Res_R;
00690     for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR) {
00691         if (*ResL) mpc_decoder_scfi_bundle_read(d, d->SCFI_Bundle, &(d->SCFI_L[n]), &(d->DSCF_Flag_L[n]));
00692         if (*ResR) mpc_decoder_scfi_bundle_read(d, d->SCFI_Bundle, &(d->SCFI_R[n]), &(d->DSCF_Flag_R[n]));
00693     }
00694 
00695     /***************************** SCFI ********************************/
00696     ResL = d->Res_L;
00697     ResR = d->Res_R;
00698     L    = d->SCF_Index_L[0];
00699     R    = d->SCF_Index_R[0];
00700     for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR, L+=3, R+=3)
00701     {
00702         if (*ResL)
00703         {
00704             /*********** DSCF ************/
00705             if (d->DSCF_Flag_L[n]==1)
00706             {
00707                 L[2] = d->DSCF_Reference_L[n];
00708                 switch (d->SCFI_L[n])
00709                 {
00710                 case 3:
00711                     L[0] = L[2] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00712                     L[1] = L[0];
00713                     L[2] = L[1];
00714                     break;
00715                 case 1:
00716                     L[0] = L[2] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00717                     L[1] = L[0] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00718                     L[2] = L[1];
00719                     break;
00720                 case 2:
00721                     L[0] = L[2] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00722                     L[1] = L[0];
00723                     L[2] = L[1] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00724                     break;
00725                 case 0:
00726                     L[0] = L[2] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00727                     L[1] = L[0] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00728                     L[2] = L[1] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00729                     break;
00730                 default:
00731                     return;
00732                     break;
00733                 }
00734             }
00735             /************ SCF ************/
00736             else
00737             {
00738                 switch (d->SCFI_L[n])
00739                 {
00740                 case 3:
00741                     L[0] = mpc_decoder_bitstream_read(d,  6);
00742                     L[1] = L[0];
00743                     L[2] = L[1];
00744                     break;
00745                 case 1:
00746                     L[0] = mpc_decoder_bitstream_read(d,  6);
00747                     L[1] = mpc_decoder_bitstream_read(d,  6);
00748                     L[2] = L[1];
00749                     break;
00750                 case 2:
00751                     L[0] = mpc_decoder_bitstream_read(d,  6);
00752                     L[1] = L[0];
00753                     L[2] = mpc_decoder_bitstream_read(d,  6);
00754                     break;
00755                 case 0:
00756                     L[0] = mpc_decoder_bitstream_read(d,  6);
00757                     L[1] = mpc_decoder_bitstream_read(d,  6);
00758                     L[2] = mpc_decoder_bitstream_read(d,  6);
00759                     break;
00760                 default:
00761                     return;
00762                     break;
00763                 }
00764             }
00765             // update Reference for DSCF
00766             d->DSCF_Reference_L[n] = L[2];
00767         }
00768         if (*ResR)
00769         {
00770             R[2] = d->DSCF_Reference_R[n];
00771             /*********** DSCF ************/
00772             if (d->DSCF_Flag_R[n]==1)
00773             {
00774                 switch (d->SCFI_R[n])
00775                 {
00776                 case 3:
00777                     R[0] = R[2] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00778                     R[1] = R[0];
00779                     R[2] = R[1];
00780                     break;
00781                 case 1:
00782                     R[0] = R[2] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00783                     R[1] = R[0] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00784                     R[2] = R[1];
00785                     break;
00786                 case 2:
00787                     R[0] = R[2] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00788                     R[1] = R[0];
00789                     R[2] = R[1] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00790                     break;
00791                 case 0:
00792                     R[0] = R[2] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00793                     R[1] = R[0] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00794                     R[2] = R[1] + mpc_decoder_huffman_decode_fast(d,  d->DSCF_Entropie);
00795                     break;
00796                 default:
00797                     return;
00798                     break;
00799                 }
00800             }
00801             /************ SCF ************/
00802             else
00803             {
00804                 switch (d->SCFI_R[n])
00805                 {
00806                 case 3:
00807                     R[0] = mpc_decoder_bitstream_read(d, 6);
00808                     R[1] = R[0];
00809                     R[2] = R[1];
00810                     break;
00811                 case 1:
00812                     R[0] = mpc_decoder_bitstream_read(d, 6);
00813                     R[1] = mpc_decoder_bitstream_read(d, 6);
00814                     R[2] = R[1];
00815                     break;
00816                 case 2:
00817                     R[0] = mpc_decoder_bitstream_read(d, 6);
00818                     R[1] = R[0];
00819                     R[2] = mpc_decoder_bitstream_read(d, 6);
00820                     break;
00821                 case 0:
00822                     R[0] = mpc_decoder_bitstream_read(d, 6);
00823                     R[1] = mpc_decoder_bitstream_read(d, 6);
00824                     R[2] = mpc_decoder_bitstream_read(d, 6);
00825                     break;
00826                 default:
00827                     return;
00828                     break;
00829                 }
00830             }
00831             // update Reference for DSCF
00832             d->DSCF_Reference_R[n] = R[2];
00833         }
00834     }
00835 
00836     /**************************** Samples ****************************/
00837     ResL = d->Res_L;
00838     ResR = d->Res_R;
00839     for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR)
00840     {
00841         // setting pointers
00842         x1 = d->SampleHuff[*ResL];
00843         x2 = d->SampleHuff[*ResR];
00844         L = d->Q[n].L;
00845         R = d->Q[n].R;
00846 
00847         if (x1!=NULL || x2!=NULL)
00848             for (k=0; k<36; ++k)
00849             {
00850                 if (x1 != NULL) *L++ = mpc_decoder_huffman_decode_fast(d,  x1);
00851                 if (x2 != NULL) *R++ = mpc_decoder_huffman_decode_fast(d,  x2);
00852             }
00853 
00854         if (*ResL>7 || *ResR>7)
00855             for (k=0; k<36; ++k)
00856             {
00857                 if (*ResL>7) *L++ = (mpc_int32_t)mpc_decoder_bitstream_read(d,  Res_bit[*ResL]) - Dc[*ResL];
00858                 if (*ResR>7) *R++ = (mpc_int32_t)mpc_decoder_bitstream_read(d,  Res_bit[*ResR]) - Dc[*ResR];
00859             }
00860     }
00861 }
00862 
00863 /****************************************** SV 7 ******************************************/
00864 void
00865 mpc_decoder_read_bitstream_sv7(mpc_decoder *d) 
00866 {
00867     // these arrays hold decoding results for bundled quantizers (3- and 5-step)
00868     /*static*/ mpc_int32_t idx30[] = { -1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1,-1, 0, 1};
00869     /*static*/ mpc_int32_t idx31[] = { -1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1,-1,-1,-1, 0, 0, 0, 1, 1, 1};
00870     /*static*/ mpc_int32_t idx32[] = { -1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
00871     /*static*/ mpc_int32_t idx50[] = { -2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2,-2,-1, 0, 1, 2};
00872     /*static*/ mpc_int32_t idx51[] = { -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
00873 
00874     mpc_int32_t n,k;
00875     mpc_int32_t Max_used_Band=0;
00876     const HuffmanTyp *Table;
00877     mpc_int32_t idx;
00878     mpc_int32_t *L   ,*R;
00879     mpc_int32_t *ResL,*ResR;
00880     mpc_uint32_t tmp;
00881 
00882     /***************************** Header *****************************/
00883     ResL  = d->Res_L;
00884     ResR  = d->Res_R;
00885 
00886     // first subband
00887     *ResL = mpc_decoder_bitstream_read(d, 4);
00888     *ResR = mpc_decoder_bitstream_read(d, 4);
00889     if (d->MS_used && !(*ResL==0 && *ResR==0)) {
00890         d->MS_Flag[0] = mpc_decoder_bitstream_read(d, 1);
00891     }
00892 
00893     // consecutive subbands
00894     ++ResL; ++ResR; // increase pointers
00895     for (n=1; n <= d->Max_Band; ++n, ++ResL, ++ResR)
00896     {
00897         idx   = mpc_decoder_huffman_decode_fast(d, d->HuffHdr);
00898         *ResL = (idx!=4) ? *(ResL-1) + idx : (int) mpc_decoder_bitstream_read(d, 4);
00899 
00900         idx   = mpc_decoder_huffman_decode_fast(d, d->HuffHdr);
00901         *ResR = (idx!=4) ? *(ResR-1) + idx : (int) mpc_decoder_bitstream_read(d, 4);
00902 
00903         if (d->MS_used && !(*ResL==0 && *ResR==0)) {
00904             d->MS_Flag[n] = mpc_decoder_bitstream_read(d, 1);
00905         }
00906 
00907         // only perform following procedures up to the maximum non-zero subband
00908         if (*ResL!=0 || *ResR!=0) {
00909             Max_used_Band = n;
00910         }
00911     }
00912     /****************************** SCFI ******************************/
00913     L     = d->SCFI_L;
00914     R     = d->SCFI_R;
00915     ResL  = d->Res_L;
00916     ResR  = d->Res_R;
00917     for (n=0; n <= Max_used_Band; ++n, ++L, ++R, ++ResL, ++ResR) {
00918         if (*ResL) *L = mpc_decoder_huffman_decode_faster(d, d->HuffSCFI);
00919         if (*ResR) *R = mpc_decoder_huffman_decode_faster(d, d->HuffSCFI);
00920     }
00921 
00922     /**************************** SCF/DSCF ****************************/
00923     ResL  = d->Res_L;
00924     ResR  = d->Res_R;
00925     L     = d->SCF_Index_L[0];
00926     R     = d->SCF_Index_R[0];
00927     for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR, L+=3, R+=3) {
00928         if (*ResL)
00929         {
00930             L[2] = d->DSCF_Reference_L[n];
00931             switch (d->SCFI_L[n])
00932             {
00933             case 1:
00934                 idx  = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00935                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00936                 idx  = mpc_decoder_huffman_decode_fast(d, d->HuffDSCF);
00937                 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00938                 L[2] = L[1];
00939                 break;
00940             case 3:
00941                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00942                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00943                 L[1] = L[0];
00944                 L[2] = L[1];
00945                 break;
00946             case 2:
00947                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00948                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00949                 L[1] = L[0];
00950                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00951                 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00952                 break;
00953             case 0:
00954                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00955                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00956                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00957                 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00958                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00959                 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00960                 break;
00961             default:
00962                 return;
00963                 break;
00964             }
00965             // update Reference for DSCF
00966             d->DSCF_Reference_L[n] = L[2];
00967         }
00968         if (*ResR)
00969         {
00970             R[2] = d->DSCF_Reference_R[n];
00971             switch (d->SCFI_R[n])
00972             {
00973             case 1:
00974                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00975                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00976                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00977                 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00978                 R[2] = R[1];
00979                 break;
00980             case 3:
00981                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00982                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00983                 R[1] = R[0];
00984                 R[2] = R[1];
00985                 break;
00986             case 2:
00987                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00988                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00989                 R[1] = R[0];
00990                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00991                 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00992                 break;
00993             case 0:
00994                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00995                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00996                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00997                 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
00998                 idx  = mpc_decoder_huffman_decode_fast(d,  d->HuffDSCF);
00999                 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
01000                 break;
01001             default:
01002                 return;
01003                 break;
01004             }
01005             // update Reference for DSCF
01006             d->DSCF_Reference_R[n] = R[2];
01007         }
01008     }
01009     /***************************** Samples ****************************/
01010     ResL = d->Res_L;
01011     ResR = d->Res_R;
01012     L    = d->Q[0].L;
01013     R    = d->Q[0].R;
01014     for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR, L+=36, R+=36)
01015     {
01016         /************** links **************/
01017         switch (*ResL)
01018         {
01019         case  -2: case  -3: case  -4: case  -5: case  -6: case  -7: case  -8: case  -9:
01020         case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
01021             L += 36;
01022             break;
01023         case -1:
01024             for (k=0; k<36; k++ ) {
01025                 tmp  = mpc_random_int(d);
01026                 *L++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >>  8) & 0xFF) + ((tmp >>  0) & 0xFF) - 510;
01027             }
01028             break;
01029         case 0:
01030             L += 36;// increase pointer
01031             break;
01032         case 1:
01033             Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][1];
01034             for (k=0; k<12; ++k)
01035             {
01036                 idx = mpc_decoder_huffman_decode_fast(d,  Table);
01037                 *L++ = idx30[idx];
01038                 *L++ = idx31[idx];
01039                 *L++ = idx32[idx];
01040             }
01041             break;
01042         case 2:
01043             Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][2];
01044             for (k=0; k<18; ++k)
01045             {
01046                 idx = mpc_decoder_huffman_decode_fast(d,  Table);
01047                 *L++ = idx50[idx];
01048                 *L++ = idx51[idx];
01049             }
01050             break;
01051         case 3:
01052         case 4:
01053             Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
01054             for (k=0; k<36; ++k)
01055                 *L++ = mpc_decoder_huffman_decode_faster(d, Table);
01056             break;
01057         case 5:
01058             Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
01059             for (k=0; k<36; ++k)
01060                 *L++ = mpc_decoder_huffman_decode_fast(d, Table);
01061             break;
01062         case 6:
01063         case 7:
01064             Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
01065             for (k=0; k<36; ++k)
01066                 *L++ = mpc_decoder_huffman_decode(d, Table);
01067             break;
01068         case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
01069             tmp = Dc[*ResL];
01070             for (k=0; k<36; ++k)
01071                 *L++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;
01072             break;
01073         default:
01074             return;
01075         }
01076         /************** rechts **************/
01077         switch (*ResR)
01078         {
01079         case  -2: case  -3: case  -4: case  -5: case  -6: case  -7: case  -8: case  -9:
01080         case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
01081             R += 36;
01082             break;
01083         case -1:
01084                 for (k=0; k<36; k++ ) {
01085                     tmp  = mpc_random_int(d);
01086                     *R++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >>  8) & 0xFF) + ((tmp >>  0) & 0xFF) - 510;
01087                 }
01088                 break;
01089             case 0:
01090                 R += 36;// increase pointer
01091                 break;
01092             case 1:
01093                 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][1];
01094                 for (k=0; k<12; ++k)
01095                 {
01096                     idx = mpc_decoder_huffman_decode_fast(d, Table);
01097                     *R++ = idx30[idx];
01098                     *R++ = idx31[idx];
01099                     *R++ = idx32[idx];
01100                 }
01101                 break;
01102             case 2:
01103                 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][2];
01104                 for (k=0; k<18; ++k)
01105                 {
01106                     idx = mpc_decoder_huffman_decode_fast(d, Table);
01107                     *R++ = idx50[idx];
01108                     *R++ = idx51[idx];
01109                 }
01110                 break;
01111             case 3:
01112             case 4:
01113                 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
01114                 for (k=0; k<36; ++k)
01115                     *R++ = mpc_decoder_huffman_decode_faster(d, Table);
01116                 break;
01117             case 5:
01118                 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
01119                 for (k=0; k<36; ++k)
01120                     *R++ = mpc_decoder_huffman_decode_fast(d, Table);
01121                 break;
01122             case 6:
01123             case 7:
01124                 Table = d->HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
01125                 for (k=0; k<36; ++k)
01126                     *R++ = mpc_decoder_huffman_decode(d, Table);
01127                 break;
01128             case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
01129                 tmp = Dc[*ResR];
01130                 for (k=0; k<36; ++k)
01131                     *R++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;
01132                 break;
01133             default:
01134                 return;
01135         }
01136     }
01137 }
01138 
01139 void mpc_decoder_setup(mpc_decoder *d, mpc_reader *r)
01140 {
01141   d->r = r;
01142 
01143   d->HuffQ[0][0] = 0;
01144   d->HuffQ[1][0] = 0;
01145   d->HuffQ[0][1] = d->HuffQ1[0];
01146   d->HuffQ[1][1] = d->HuffQ1[1];
01147   d->HuffQ[0][2] = d->HuffQ2[0];
01148   d->HuffQ[1][2] = d->HuffQ2[1];
01149   d->HuffQ[0][3] = d->HuffQ3[0];
01150   d->HuffQ[1][3] = d->HuffQ3[1];
01151   d->HuffQ[0][4] = d->HuffQ4[0];
01152   d->HuffQ[1][4] = d->HuffQ4[1];
01153   d->HuffQ[0][5] = d->HuffQ5[0];
01154   d->HuffQ[1][5] = d->HuffQ5[1];
01155   d->HuffQ[0][6] = d->HuffQ6[0];
01156   d->HuffQ[1][6] = d->HuffQ6[1];
01157   d->HuffQ[0][7] = d->HuffQ7[0];
01158   d->HuffQ[1][7] = d->HuffQ7[1];
01159 
01160   d->SampleHuff[0] = NULL;
01161   d->SampleHuff[1] = d->Entropie_1;
01162   d->SampleHuff[2] = d->Entropie_2;
01163   d->SampleHuff[3] = d->Entropie_3;
01164   d->SampleHuff[4] = d->Entropie_4;
01165   d->SampleHuff[5] = d->Entropie_5;
01166   d->SampleHuff[6] = d->Entropie_6;
01167   d->SampleHuff[7] = d->Entropie_7;
01168   d->SampleHuff[8] = NULL;
01169   d->SampleHuff[9] = NULL;
01170   d->SampleHuff[10] = NULL;
01171   d->SampleHuff[11] = NULL;
01172   d->SampleHuff[12] = NULL;
01173   d->SampleHuff[13] = NULL;
01174   d->SampleHuff[14] = NULL;
01175   d->SampleHuff[15] = NULL;
01176   d->SampleHuff[16] = NULL;
01177   d->SampleHuff[17] = NULL;
01178 
01179   d->EQ_activated = 0;
01180   d->MPCHeaderPos = 0;
01181   d->StreamVersion = 0;
01182   d->MS_used = 0;
01183   d->FwdJumpInfo = 0;
01184   d->ActDecodePos = 0;
01185   d->FrameWasValid = 0;
01186   d->OverallFrames = 0;
01187   d->DecodedFrames = 0;
01188   d->LastValidSamples = 0;
01189   d->TrueGaplessPresent = 0;
01190   d->WordsRead = 0;
01191   d->Max_Band = 0;
01192   d->SampleRate = 0;
01193 //  clips = 0;
01194   d->__r1 = 1;
01195   d->__r2 = 1;
01196 
01197   d->dword = 0;
01198   d->pos = 0;
01199   d->Zaehler = 0;
01200   d->WordsRead = 0;
01201   d->Max_Band = 0;
01202 
01203   mpc_decoder_initialisiere_quantisierungstabellen(d, 1.0f);
01204   mpc_decoder_init_huffman_sv6(d);
01205   mpc_decoder_init_huffman_sv7(d);
01206 }
01207 
01208 void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
01209 {
01210     mpc_decoder_reset_synthesis(d);
01211     mpc_decoder_reset_globals(d);
01212 
01213     d->StreamVersion      = si->stream_version;
01214     d->MS_used            = si->ms;
01215     d->Max_Band           = si->max_band;
01216     d->OverallFrames      = si->frames;
01217     d->MPCHeaderPos       = si->header_position;
01218     d->LastValidSamples   = si->last_frame_samples;
01219     d->TrueGaplessPresent = si->is_true_gapless;
01220     d->SampleRate         = (mpc_int32_t)si->sample_freq;
01221 
01222     d->samples_to_skip = MPC_DECODER_SYNTH_DELAY;
01223 }
01224 
01225 mpc_bool_t mpc_decoder_initialize(mpc_decoder *d, mpc_streaminfo *si) 
01226 {
01227     mpc_decoder_set_streaminfo(d, si);
01228 
01229     // AB: setting position to the beginning of the data-bitstream
01230     switch (d->StreamVersion) {
01231     case 0x04: f_seek(d, 4 + d->MPCHeaderPos); d->pos = 16; break;  // Geht auch über eine der Helperfunktionen
01232     case 0x05:
01233     case 0x06: f_seek(d, 8 + d->MPCHeaderPos); d->pos =  0; break;
01234     case 0x07:
01235     case 0x17: /*f_seek ( 24 + d->MPCHeaderPos );*/ d->pos =  8; break;
01236     default: return FALSE;
01237     }
01238 
01239     // AB: fill buffer and initialize decoder
01240     f_read_dword(d, d->Speicher, MEMSIZE );
01241     d->dword = d->Speicher[d->Zaehler = 0];
01242 
01243     return TRUE;
01244 }
01245 
01246 //---------------------------------------------------------------
01247 // will seek from the beginning of the file to the desired
01248 // position in ms (given by seek_needed)
01249 //---------------------------------------------------------------
01250 #if 0
01251 static void
01252 helper1(mpc_decoder *d, mpc_uint32_t bitpos) 
01253 {
01254     f_seek(d, (bitpos >> 5) * 4 + d->MPCHeaderPos);
01255     f_read_dword(d, d->Speicher, 2);
01256     d->dword = d->Speicher[d->Zaehler = 0];
01257     d->pos = bitpos & 31;
01258 }
01259 #endif
01260 
01261 static void
01262 helper2(mpc_decoder *d, mpc_uint32_t bitpos) 
01263 {
01264     f_seek(d, (bitpos>>5) * 4 + d->MPCHeaderPos);
01265     f_read_dword(d, d->Speicher, MEMSIZE);
01266     d->dword = d->Speicher[d->Zaehler = 0];
01267     d->pos = bitpos & 31;
01268 }
01269 
01270 #if 0
01271 static void
01272 helper3(mpc_decoder *d, mpc_uint32_t bitpos, mpc_uint32_t* buffoffs) 
01273 {
01274     d->pos = bitpos & 31;
01275     bitpos >>= 5;
01276     if ((mpc_uint32_t)(bitpos - *buffoffs) >= MEMSIZE - 2) {
01277         *buffoffs = bitpos;
01278         f_seek(d, bitpos * 4L + d->MPCHeaderPos);
01279         f_read_dword(d, d->Speicher, MEMSIZE );
01280     }
01281     d->dword = d->Speicher[d->Zaehler = bitpos - *buffoffs ];
01282 }
01283 #endif
01284 
01285 static mpc_uint32_t get_initial_fpos(mpc_decoder *d, mpc_uint32_t StreamVersion)
01286 {
01287     mpc_uint32_t fpos = 0;
01288     (void) StreamVersion;
01289     switch ( d->StreamVersion ) {                                                  // setting position to the beginning of the data-bitstream
01290     case  0x04: fpos =  48; break;
01291     case  0x05:
01292     case  0x06: fpos =  64; break;
01293     case  0x07:
01294     case  0x17: fpos = 200; break;
01295     }
01296     return fpos;
01297 }
01298 
01299 mpc_bool_t mpc_decoder_seek_seconds(mpc_decoder *d, double seconds) 
01300 {
01301     return mpc_decoder_seek_sample(d, (mpc_int64_t)(seconds * (double)d->SampleRate + 0.5));
01302 }
01303 
01304 mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample) 
01305 {
01306     mpc_uint32_t fpos;
01307     mpc_uint32_t fwd;
01308 
01309     fwd = (mpc_uint32_t) (destsample / MPC_FRAME_LENGTH);
01310     d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + (mpc_uint32_t)(destsample % MPC_FRAME_LENGTH);
01311 
01312     memset(d->Y_L          , 0, sizeof d->Y_L           );
01313     memset(d->Y_R          , 0, sizeof d->Y_R           );
01314     memset(d->SCF_Index_L     , 0, sizeof d->SCF_Index_L      );
01315     memset(d->SCF_Index_R     , 0, sizeof d->SCF_Index_R      );
01316     memset(d->Res_L           , 0, sizeof d->Res_L            );
01317     memset(d->Res_R           , 0, sizeof d->Res_R            );
01318     memset(d->SCFI_L          , 0, sizeof d->SCFI_L           );
01319     memset(d->SCFI_R          , 0, sizeof d->SCFI_R           );
01320     memset(d->DSCF_Flag_L     , 0, sizeof d->DSCF_Flag_L      );
01321     memset(d->DSCF_Flag_R     , 0, sizeof d->DSCF_Flag_R      );
01322     memset(d->DSCF_Reference_L, 0, sizeof d->DSCF_Reference_L );
01323     memset(d->DSCF_Reference_R, 0, sizeof d->DSCF_Reference_R );
01324     memset(d->Q               , 0, sizeof d->Q                );
01325     memset(d->MS_Flag         , 0, sizeof d->MS_Flag          );
01326 
01327     // resetting synthesis filter to avoid "clicks"
01328     mpc_decoder_reset_synthesis(d);
01329 
01330     // prevent from desired position out of allowed range
01331     fwd = fwd < d->OverallFrames  ?  fwd  :  d->OverallFrames;
01332 
01333     // reset number of decoded frames
01334     d->DecodedFrames = 0;
01335 
01336     fpos = get_initial_fpos(d, d->StreamVersion);
01337     if (fpos == 0) {
01338         return FALSE;
01339     }
01340 
01341     helper2(d, fpos);
01342 
01343     // read the last 32 frames before the desired position to scan the scalefactors (artifactless jumping)
01344     for ( ; d->DecodedFrames < fwd; d->DecodedFrames++ ) {
01345         mpc_uint32_t   FrameBitCnt;
01346         mpc_uint32_t   RING;
01347         RING         = d->Zaehler;
01348         d->FwdJumpInfo  = mpc_decoder_bitstream_read(d, 20);    // read jump-info
01349         d->ActDecodePos = (d->Zaehler << 5) + d->pos;
01350         FrameBitCnt  = mpc_decoder_bits_read(d);  // scanning the scalefactors and check for validity of frame
01351         if (d->StreamVersion >= 7)  {
01352             mpc_decoder_read_bitstream_sv7(d);
01353         }
01354         else {
01355             mpc_decoder_read_bitstream_sv6(d);
01356         }
01357         if (mpc_decoder_bits_read(d) - FrameBitCnt != d->FwdJumpInfo ) {
01358             // Box ("Bug in perform_jump");
01359             return FALSE;
01360         }
01361         // update buffer
01362         if ((RING ^ d->Zaehler) & MEMSIZE2) {
01363             f_read_dword(d, d->Speicher + (RING & MEMSIZE2),  MEMSIZE2);
01364         }
01365     }
01366 
01367     // LastBitsRead = BitsRead ();
01368     // LastFrame = d->DecodedFrames;
01369 
01370     return TRUE;
01371 }
01372 
01373 void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING) 
01374 {
01375     if ((RING ^ d->Zaehler) & MEMSIZE2 ) {
01376         // update buffer
01377         f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2);
01378     }
01379 }
01380 
01381 

Generated on Sun Oct 23 16:35:24 2005 for libmpcdec by  doxygen 1.4.4