1 /*
2   Copyright (c) 2005, The Musepack Development Team
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are
7   met:
8 
9   * Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11 
12   * Redistributions in binary form must reproduce the above
13   copyright notice, this list of conditions and the following
14   disclaimer in the documentation and/or other materials provided
15   with the distribution.
16 
17   * Neither the name of the The Musepack Development Team nor the
18   names of its contributors may be used to endorse or promote
19   products derived from this software without specific prior
20   written permission.
21 
22   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 /// \file mpc_decoder.c
36 /// Core decoding routines and logic.
37 
38 #include <mpcdec/mpcdec.h>
39 #include <mpcdec/internal.h>
40 #include <mpcdec/requant.h>
41 #include <mpcdec/huffman.h>
42 
43 //SV7 tables
44 extern const HuffmanTyp*   mpc_table_HuffQ [2] [8];
45 extern const HuffmanTyp    mpc_table_HuffHdr  [10];
46 extern const HuffmanTyp    mpc_table_HuffSCFI [ 4];
47 extern const HuffmanTyp    mpc_table_HuffDSCF [16];
48 
49 
50 #ifdef MPC_SUPPORT_SV456
51 //SV4/5/6 tables
52 extern const HuffmanTyp*   mpc_table_SampleHuff [18];
53 extern const HuffmanTyp    mpc_table_SCFI_Bundle   [ 8];
54 extern const HuffmanTyp    mpc_table_DSCF_Entropie [13];
55 extern const HuffmanTyp    mpc_table_Region_A [16];
56 extern const HuffmanTyp    mpc_table_Region_B [ 8];
57 extern const HuffmanTyp    mpc_table_Region_C [ 4];
58 
59 #endif
60 
61 #ifndef MPC_LITTLE_ENDIAN
62 #define SWAP(X) mpc_swap32(X)
63 #else
64 #define SWAP(X) (X)
65 #endif
66 
67 //------------------------------------------------------------------------------
68 // types
69 //------------------------------------------------------------------------------
70 enum
71     {
72         EQ_TAP = 13,                        // length of FIR filter for EQ
73         DELAY = ((EQ_TAP + 1) / 2),         // delay of FIR
74         FIR_BANDS = 4,                      // number of subbands to be FIR filtered
75         MEMSIZE = MPC_DECODER_MEMSIZE,      // overall buffer size
76         MEMSIZE2 = (MEMSIZE/2),             // size of one buffer
77         MEMMASK = (MEMSIZE-1)
78     };
79 
80 //------------------------------------------------------------------------------
81 // forward declarations
82 //------------------------------------------------------------------------------
83 void mpc_decoder_read_bitstream_sv6(mpc_decoder *d, mpc_bool_t seeking);
84 void mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t seeking);
85 mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample);
86 void mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band);
87 
88 //------------------------------------------------------------------------------
89 // utility functions
90 //------------------------------------------------------------------------------
f_read(mpc_decoder * d,void * ptr,mpc_int32_t size)91 static mpc_int32_t f_read(mpc_decoder *d, void *ptr, mpc_int32_t size)
92 {
93     return d->r->read(d->r->data, ptr, size);
94 }
95 
f_seek(mpc_decoder * d,mpc_int32_t offset)96 static mpc_bool_t f_seek(mpc_decoder *d, mpc_int32_t offset)
97 {
98     return d->r->seek(d->r->data, offset);
99 }
100 
f_read_dword(mpc_decoder * d,mpc_uint32_t * ptr,mpc_uint32_t count)101 static mpc_int32_t f_read_dword(mpc_decoder *d, mpc_uint32_t * ptr, mpc_uint32_t count)
102 {
103     return f_read(d, ptr, count << 2) >> 2;
104 }
105 
mpc_decoder_seek(mpc_decoder * d,mpc_uint32_t bitpos)106 static void mpc_decoder_seek(mpc_decoder *d, mpc_uint32_t bitpos)
107 {
108     f_seek(d, (bitpos>>5) * 4 + d->MPCHeaderPos);
109     f_read_dword(d, d->Speicher, MEMSIZE);
110     d->dword = SWAP(d->Speicher[d->Zaehler = 0]);
111     d->pos = bitpos & 31;
112     d->WordsRead = bitpos >> 5;
113 }
114 
115 // jump desired number of bits out of the bitstream
mpc_decoder_bitstream_jump(mpc_decoder * d,const mpc_uint32_t bits)116 static void mpc_decoder_bitstream_jump(mpc_decoder *d, const mpc_uint32_t bits)
117 {
118     d->pos += bits;
119 
120     if (d->pos >= 32) {
121         d->Zaehler = (d->Zaehler + (d->pos >> 5)) & MEMMASK;
122         d->dword = SWAP(d->Speicher[d->Zaehler]);
123         d->WordsRead += d->pos >> 5;
124         d->pos &= 31;
125     }
126 }
127 
mpc_decoder_update_buffer(mpc_decoder * d,mpc_uint32_t RING)128 static void mpc_decoder_update_buffer(mpc_decoder *d, mpc_uint32_t RING)
129 {
130     if ((RING ^ d->Zaehler) & MEMSIZE2 ) {
131         // update buffer
132         f_read_dword(d, d->Speicher + (RING & MEMSIZE2), MEMSIZE2);
133     }
134 }
135 
136 //------------------------------------------------------------------------------
137 // huffman & bitstream functions
138 //------------------------------------------------------------------------------
139 
140 /* F U N C T I O N S */
141 
142 // resets bitstream decoding
143 static void
mpc_decoder_reset_bitstream_decode(mpc_decoder * d)144 mpc_decoder_reset_bitstream_decode(mpc_decoder *d)
145 {
146     d->dword = 0;
147     d->pos = 0;
148     d->Zaehler = 0;
149     d->WordsRead = 0;
150 }
151 
152 // reports the number of read bits
153 static mpc_uint32_t
mpc_decoder_bits_read(mpc_decoder * d)154 mpc_decoder_bits_read(mpc_decoder *d)
155 {
156     return 32 * d->WordsRead + d->pos;
157 }
158 
159 // read desired number of bits out of the bitstream (max 31)
160 static mpc_uint32_t
mpc_decoder_bitstream_read(mpc_decoder * d,const mpc_uint32_t bits)161 mpc_decoder_bitstream_read(mpc_decoder *d, const mpc_uint32_t bits)
162 {
163     mpc_uint32_t out = d->dword;
164 
165     d->pos += bits;
166 
167     if (d->pos < 32) {
168         out >>= (32 - d->pos);
169     } else {
170         d->dword = SWAP(d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]);
171         d->pos -= 32;
172         if (d->pos) {
173             out <<= d->pos;
174             out |= d->dword >> (32 - d->pos);
175         }
176         d->WordsRead++;
177     }
178 
179     return out & ((1 << bits) - 1);
180 }
181 
182 // basic huffman decoding routine
183 // works with maximum lengths up to max_length
184 static mpc_int32_t
mpc_decoder_huffman_decode(mpc_decoder * d,const HuffmanTyp * Table,const mpc_uint32_t max_length)185 mpc_decoder_huffman_decode(mpc_decoder *d, const HuffmanTyp *Table,
186                            const mpc_uint32_t max_length)
187 {
188     // load preview and decode
189     mpc_uint32_t code = d->dword << d->pos;
190     if (32 - d->pos < max_length)
191         code |= SWAP(d->Speicher[(d->Zaehler + 1) & MEMMASK]) >> (32 - d->pos);
192 
193     while (code < Table->Code) Table++;
194 
195     // set the new position within bitstream without performing a dummy-read
196     if ((d->pos += Table->Length) >= 32) {
197         d->pos -= 32;
198         d->dword = SWAP(d->Speicher[d->Zaehler = (d->Zaehler + 1) & MEMMASK]);
199         d->WordsRead++;
200     }
201 
202     return Table->Value;
203 }
204 
205 // decode SCFI-bundle (sv4,5,6)
206 static void
mpc_decoder_scfi_bundle_read(mpc_decoder * d,const HuffmanTyp * Table,mpc_int32_t * SCFI,mpc_bool_t * DSCF)207 mpc_decoder_scfi_bundle_read(mpc_decoder *d, const HuffmanTyp* Table,
208                              mpc_int32_t* SCFI, mpc_bool_t* DSCF)
209 {
210     mpc_uint32_t value = mpc_decoder_huffman_decode(d, Table, 6);
211 
212     *SCFI = value >> 1;
213     *DSCF = value &  1;
214 }
215 
216 static void
mpc_decoder_reset_v(mpc_decoder * d)217 mpc_decoder_reset_v(mpc_decoder *d)
218 {
219     memset(d->V_L, 0, sizeof d->V_L);
220     memset(d->V_R, 0, sizeof d->V_R);
221 }
222 
223 static void
mpc_decoder_reset_synthesis(mpc_decoder * d)224 mpc_decoder_reset_synthesis(mpc_decoder *d)
225 {
226     mpc_decoder_reset_v(d);
227 }
228 
229 static void
mpc_decoder_reset_y(mpc_decoder * d)230 mpc_decoder_reset_y(mpc_decoder *d)
231 {
232     memset(d->Y_L, 0, sizeof d->Y_L);
233     memset(d->Y_R, 0, sizeof d->Y_R);
234 }
235 
236 static void
mpc_decoder_reset_globals(mpc_decoder * d)237 mpc_decoder_reset_globals(mpc_decoder *d)
238 {
239     mpc_decoder_reset_bitstream_decode(d);
240 
241     d->DecodedFrames  = 0;
242     d->StreamVersion  = 0;
243     d->MS_used        = 0;
244 
245     memset(d->Y_L             , 0, sizeof d->Y_L              );
246     memset(d->Y_R             , 0, sizeof d->Y_R              );
247     memset(d->SCF_Index_L     , 0, sizeof d->SCF_Index_L      );
248     memset(d->SCF_Index_R     , 0, sizeof d->SCF_Index_R      );
249     memset(d->Res_L           , 0, sizeof d->Res_L            );
250     memset(d->Res_R           , 0, sizeof d->Res_R            );
251     memset(d->SCFI_L          , 0, sizeof d->SCFI_L           );
252     memset(d->SCFI_R          , 0, sizeof d->SCFI_R           );
253     memset(d->DSCF_Flag_L     , 0, sizeof d->DSCF_Flag_L      );
254     memset(d->DSCF_Flag_R     , 0, sizeof d->DSCF_Flag_R      );
255     memset(d->Q               , 0, sizeof d->Q                );
256     memset(d->MS_Flag         , 0, sizeof d->MS_Flag          );
257     memset(d->seeking_table   , 0, sizeof d->seeking_table    );
258 }
259 
260 // Frame decoding. Takes big endian 32 bits words as input
261 mpc_uint32_t
mpc_decoder_decode_frame(mpc_decoder * d,mpc_uint32_t * in_buffer,mpc_uint32_t in_len,MPC_SAMPLE_FORMAT * out_buffer)262 mpc_decoder_decode_frame(mpc_decoder *d, mpc_uint32_t *in_buffer,
263                          mpc_uint32_t in_len, MPC_SAMPLE_FORMAT *out_buffer)
264 {
265   unsigned int i;
266   mpc_decoder_reset_bitstream_decode(d);
267   if (in_len > sizeof(d->Speicher)) in_len = sizeof(d->Speicher);
268   memcpy(d->Speicher, in_buffer, in_len);
269   for (i = 0; i < (in_len + 3) / 4; i++)
270     d->Speicher[i] = mpc_swap32(d->Speicher[i]);
271   d->dword = SWAP(d->Speicher[0]);
272   switch (d->StreamVersion) {
273 #ifdef MPC_SUPPORT_SV456
274     case 0x04:
275     case 0x05:
276     case 0x06:
277         mpc_decoder_read_bitstream_sv6(d, FALSE);
278         break;
279 #endif
280     case 0x07:
281     case 0x17:
282         mpc_decoder_read_bitstream_sv7(d, FALSE);
283         break;
284     default:
285         return (mpc_uint32_t)(-1);
286   }
287   mpc_decoder_requantisierung(d, d->Max_Band);
288   mpc_decoder_synthese_filter_float(d, out_buffer);
289   return mpc_decoder_bits_read(d);
290 }
291 
292 static mpc_uint32_t
mpc_decoder_decode_internal(mpc_decoder * d,MPC_SAMPLE_FORMAT * buffer)293 mpc_decoder_decode_internal(mpc_decoder *d, MPC_SAMPLE_FORMAT *buffer)
294 {
295     mpc_uint32_t output_frame_length = MPC_FRAME_LENGTH;
296     mpc_uint32_t FwdJumpInfo = 0;
297     mpc_uint32_t  FrameBitCnt = 0;
298 
299     if (d->DecodedFrames >= d->OverallFrames) {
300         return (mpc_uint32_t)(-1);                           // end of file -> abort decoding
301     }
302 
303     // add seeking info
304     if (d->seeking_table_frames < d->DecodedFrames &&
305        (d->DecodedFrames & ((1 << d->seeking_pwr) - 1)) == 0) {
306         d->seeking_table[d->DecodedFrames >> d->seeking_pwr] = mpc_decoder_bits_read(d);
307         d->seeking_table_frames = d->DecodedFrames;
308     }
309 
310     // read jump-info for validity check of frame
311     FwdJumpInfo  = mpc_decoder_bitstream_read(d, 20);
312 
313     // decode data and check for validity of frame
314     FrameBitCnt = mpc_decoder_bits_read(d);
315     switch (d->StreamVersion) {
316 #ifdef MPC_SUPPORT_SV456
317     case 0x04:
318     case 0x05:
319     case 0x06:
320         mpc_decoder_read_bitstream_sv6(d, FALSE);
321         break;
322 #endif
323     case 0x07:
324     case 0x17:
325         mpc_decoder_read_bitstream_sv7(d, FALSE);
326         break;
327     default:
328         return (mpc_uint32_t)(-1);
329     }
330     d->FrameWasValid = mpc_decoder_bits_read(d) - FrameBitCnt == FwdJumpInfo;
331 
332     // synthesize signal
333     mpc_decoder_requantisierung(d, d->Max_Band);
334     mpc_decoder_synthese_filter_float(d, buffer);
335 
336     d->DecodedFrames++;
337 
338     // cut off first MPC_DECODER_SYNTH_DELAY zero-samples
339     if (d->DecodedFrames == d->OverallFrames  && d->StreamVersion >= 6) {
340         // reconstruct exact filelength
341         mpc_int32_t  mod_block   = mpc_decoder_bitstream_read(d,  11);
342         mpc_int32_t  FilterDecay;
343 
344         if (mod_block == 0) {
345             // Encoder bugfix
346             mod_block = 1152;
347         }
348         FilterDecay = (mod_block + MPC_DECODER_SYNTH_DELAY) % MPC_FRAME_LENGTH;
349 
350         // additional FilterDecay samples are needed for decay of synthesis filter
351         if (MPC_DECODER_SYNTH_DELAY + mod_block >= MPC_FRAME_LENGTH) {
352             if (!d->TrueGaplessPresent) {
353                 mpc_decoder_reset_y(d);
354             } else {
355                 mpc_decoder_bitstream_read(d, 20);
356                 mpc_decoder_read_bitstream_sv7(d, FALSE);
357                 mpc_decoder_requantisierung(d, d->Max_Band);
358             }
359 
360             mpc_decoder_synthese_filter_float(d, buffer + 2304);
361 
362             output_frame_length = MPC_FRAME_LENGTH + FilterDecay;
363         }
364         else {                              // there are only FilterDecay samples needed for this frame
365             output_frame_length = FilterDecay;
366         }
367     }
368 
369     if (d->samples_to_skip) {
370         if (output_frame_length < d->samples_to_skip) {
371             d->samples_to_skip -= output_frame_length;
372             output_frame_length = 0;
373         }
374         else {
375             output_frame_length -= d->samples_to_skip;
376             memmove(
377                 buffer,
378                 buffer + d->samples_to_skip * 2,
379                 output_frame_length * 2 * sizeof (MPC_SAMPLE_FORMAT));
380             d->samples_to_skip = 0;
381         }
382     }
383 
384     return output_frame_length;
385 }
386 
mpc_decoder_decode(mpc_decoder * d,MPC_SAMPLE_FORMAT * buffer,mpc_uint32_t * vbr_update_acc,mpc_uint32_t * vbr_update_bits)387 mpc_uint32_t mpc_decoder_decode(
388     mpc_decoder *d,
389     MPC_SAMPLE_FORMAT *buffer,
390     mpc_uint32_t *vbr_update_acc,
391     mpc_uint32_t *vbr_update_bits)
392 {
393     for(;;)
394     {
395         //const mpc_int32_t MaxBrokenFrames = 0; // PluginSettings.MaxBrokenFrames
396 
397         mpc_uint32_t RING = d->Zaehler;
398         mpc_int32_t vbr_ring = (RING << 5) + d->pos;
399 
400         mpc_uint32_t valid_samples = mpc_decoder_decode_internal(d, buffer);
401 
402         if (valid_samples == (mpc_uint32_t)(-1) ) {
403             return 0;
404         }
405 
406         /**************** ERROR CONCEALMENT *****************/
407         if (d->FrameWasValid == 0 ) {
408             // error occurred in bitstream
409             return (mpc_uint32_t)(-1);
410         }
411         else {
412             if (vbr_update_acc && vbr_update_bits) {
413                 (*vbr_update_acc) ++;
414                 vbr_ring = (d->Zaehler << 5) + d->pos - vbr_ring;
415                 if (vbr_ring < 0) {
416                     vbr_ring += 524288;
417                 }
418                 (*vbr_update_bits) += vbr_ring;
419             }
420 
421         }
422         mpc_decoder_update_buffer(d, RING);
423 
424         if (valid_samples > 0) {
425             return valid_samples;
426         }
427     }
428 }
429 
430 void
mpc_decoder_requantisierung(mpc_decoder * d,const mpc_int32_t Last_Band)431 mpc_decoder_requantisierung(mpc_decoder *d, const mpc_int32_t Last_Band)
432 {
433     mpc_int32_t     Band;
434     mpc_int32_t     n;
435     MPC_SAMPLE_FORMAT facL;
436     MPC_SAMPLE_FORMAT facR;
437     MPC_SAMPLE_FORMAT templ;
438     MPC_SAMPLE_FORMAT tempr;
439     MPC_SAMPLE_FORMAT* YL;
440     MPC_SAMPLE_FORMAT* YR;
441     mpc_int32_t*    L;
442     mpc_int32_t*    R;
443 
444 #ifdef MPC_FIXED_POINT
445 #if MPC_FIXED_POINT_FRACTPART == 14
446 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
447     MPC_MULTIPLY_EX(CcVal, d->SCF[SCF_idx], d->SCF_shift[SCF_idx])
448 #else
449 
450 #error FIXME, Cc table is in 18.14 format
451 
452 #endif
453 #else
454 #define MPC_MULTIPLY_SCF(CcVal, SCF_idx) \
455     MPC_MULTIPLY(CcVal, d->SCF[SCF_idx])
456 #endif
457     // requantization and scaling of subband-samples
458     for ( Band = 0; Band <= Last_Band; Band++ ) {   // setting pointers
459         YL = d->Y_L[0] + Band;
460         YR = d->Y_R[0] + Band;
461         L  = d->Q[Band].L;
462         R  = d->Q[Band].R;
463         /************************** MS-coded **************************/
464         if ( d->MS_Flag [Band] ) {
465             if ( d->Res_L [Band] ) {
466                 if ( d->Res_R [Band] ) {    // M!=0, S!=0
467                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
468                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
469                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
470                         *YL   = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
471                         *YR   = templ - tempr;
472                     }
473                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
474                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
475                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
476                         *YL   = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
477                         *YR   = templ - tempr;
478                     }
479                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
480                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
481                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
482                         *YL   = (templ = MPC_MULTIPLY_FLOAT_INT(facL,*L++))+(tempr = MPC_MULTIPLY_FLOAT_INT(facR,*R++));
483                         *YR   = templ - tempr;
484                     }
485                 } else {    // M!=0, S==0
486                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
487                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
488                         *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
489                     }
490                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
491                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
492                         *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
493                     }
494                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
495                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
496                         *YR = *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
497                     }
498                 }
499             } else {
500                 if (d->Res_R[Band])    // M==0, S!=0
501                 {
502                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
503                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
504                         *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
505                     }
506                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
507                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
508                         *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
509                     }
510                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
511                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
512                         *YR = - (*YL = MPC_MULTIPLY_FLOAT_INT(facR,*(R++)));
513                     }
514                 } else {    // M==0, S==0
515                     for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
516                         *YR = *YL = 0;
517                     }
518                 }
519             }
520         }
521         /************************** LR-coded **************************/
522         else {
523             if ( d->Res_L [Band] ) {
524                 if ( d->Res_R [Band] ) {    // L!=0, R!=0
525                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
526                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
527                     for (n = 0; n < 12; n++, YL += 32, YR += 32 ) {
528                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
529                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
530                     }
531                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
532                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
533                     for (; n < 24; n++, YL += 32, YR += 32 ) {
534                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
535                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
536                     }
537                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
538                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
539                     for (; n < 36; n++, YL += 32, YR += 32 ) {
540                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
541                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
542                     }
543                 } else {     // L!=0, R==0
544                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][0]);
545                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
546                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
547                         *YR = 0;
548                     }
549                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][1]);
550                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
551                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
552                         *YR = 0;
553                     }
554                     facL = MPC_MULTIPLY_SCF( Cc[d->Res_L[Band]] , (unsigned char)d->SCF_Index_L[Band][2]);
555                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
556                         *YL = MPC_MULTIPLY_FLOAT_INT(facL,*L++);
557                         *YR = 0;
558                     }
559                 }
560             }
561             else {
562                 if ( d->Res_R [Band] ) {    // L==0, R!=0
563                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][0]);
564                     for ( n = 0; n < 12; n++, YL += 32, YR += 32 ) {
565                         *YL = 0;
566                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
567                     }
568                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][1]);
569                     for ( ; n < 24; n++, YL += 32, YR += 32 ) {
570                         *YL = 0;
571                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
572                     }
573                     facR = MPC_MULTIPLY_SCF( Cc[d->Res_R[Band]] , (unsigned char)d->SCF_Index_R[Band][2]);
574                     for ( ; n < 36; n++, YL += 32, YR += 32 ) {
575                         *YL = 0;
576                         *YR = MPC_MULTIPLY_FLOAT_INT(facR,*R++);
577                     }
578                 } else {    // L==0, R==0
579                     for ( n = 0; n < 36; n++, YL += 32, YR += 32 ) {
580                         *YR = *YL = 0;
581                     }
582                 }
583             }
584         }
585     }
586 }
587 
588 #ifdef MPC_SUPPORT_SV456
589 static const unsigned char Q_res[32][16] = {
590 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
591 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
592 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
593 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
594 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
595 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
596 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
597 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
598 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
599 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
600 {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,17},
601 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
602 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
603 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
604 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
605 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
606 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
607 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
608 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
609 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
610 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
611 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
612 {0,1,2,3,4,5,6,17,0,0,0,0,0,0,0,0},
613 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
614 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
615 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
616 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
617 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
618 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
619 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
620 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
621 {0,1,2,17,0,0,0,0,0,0,0,0,0,0,0,0},
622 };
623 
624 /****************************************** SV 6 ******************************************/
625 void
mpc_decoder_read_bitstream_sv6(mpc_decoder * d,mpc_bool_t seeking)626 mpc_decoder_read_bitstream_sv6(mpc_decoder *d, mpc_bool_t seeking)
627 {
628     mpc_int32_t n,k;
629     mpc_int32_t Max_used_Band=0;
630     const HuffmanTyp *Table;
631     const HuffmanTyp *x1;
632     const HuffmanTyp *x2;
633     mpc_int32_t *L;
634     mpc_int32_t *R;
635     mpc_int32_t *ResL = d->Res_L;
636     mpc_int32_t *ResR = d->Res_R;
637 
638     /************************ HEADER **************************/
639     ResL = d->Res_L;
640     ResR = d->Res_R;
641     for (n=0; n <= d->Max_Band; ++n, ++ResL, ++ResR)
642     {
643         if      (n<11)           Table = mpc_table_Region_A;
644         else if (n>=11 && n<=22) Table = mpc_table_Region_B;
645         else /*if (n>=23)*/      Table = mpc_table_Region_C;
646 
647         *ResL = Q_res[n][mpc_decoder_huffman_decode(d, Table, 14)];
648         if (d->MS_used) {
649             d->MS_Flag[n] = mpc_decoder_bitstream_read(d,  1);
650         }
651         *ResR = Q_res[n][mpc_decoder_huffman_decode(d, Table, 14)];
652 
653         // only perform the following procedure up to the maximum non-zero subband
654         if (*ResL || *ResR) Max_used_Band = n;
655     }
656 
657     /************************* SCFI-Bundle *****************************/
658     ResL = d->Res_L;
659     ResR = d->Res_R;
660     for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR) {
661         if (*ResL) mpc_decoder_scfi_bundle_read(d, mpc_table_SCFI_Bundle, &(d->SCFI_L[n]), &(d->DSCF_Flag_L[n]));
662         if (*ResR) mpc_decoder_scfi_bundle_read(d, mpc_table_SCFI_Bundle, &(d->SCFI_R[n]), &(d->DSCF_Flag_R[n]));
663     }
664 
665     /***************************** SCFI ********************************/
666     ResL = d->Res_L;
667     ResR = d->Res_R;
668     L    = d->SCF_Index_L[0];
669     R    = d->SCF_Index_R[0];
670     for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR, L+=3, R+=3)
671     {
672         if (*ResL)
673         {
674             /*********** DSCF ************/
675             if (d->DSCF_Flag_L[n]==1)
676             {
677                 switch (d->SCFI_L[n])
678                 {
679                 case 3:
680                     L[0] = L[2] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
681                     L[1] = L[0];
682                     L[2] = L[1];
683                     break;
684                 case 1:
685                     L[0] = L[2] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
686                     L[1] = L[0] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
687                     L[2] = L[1];
688                     break;
689                 case 2:
690                     L[0] = L[2] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
691                     L[1] = L[0];
692                     L[2] = L[1] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
693                     break;
694                 case 0:
695                     L[0] = L[2] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
696                     L[1] = L[0] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
697                     L[2] = L[1] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
698                     break;
699                 default:
700                     return;
701                 }
702                 if (L[0] > 1024)
703                     L[0] = 0x8080;
704                 if (L[1] > 1024)
705                     L[1] = 0x8080;
706                 if (L[2] > 1024)
707                     L[2] = 0x8080;
708             }
709             /************ SCF ************/
710             else
711             {
712                 switch (d->SCFI_L[n])
713                 {
714                 case 3:
715                     L[0] = mpc_decoder_bitstream_read(d,  6);
716                     L[1] = L[0];
717                     L[2] = L[1];
718                     break;
719                 case 1:
720                     L[0] = mpc_decoder_bitstream_read(d,  6);
721                     L[1] = mpc_decoder_bitstream_read(d,  6);
722                     L[2] = L[1];
723                     break;
724                 case 2:
725                     L[0] = mpc_decoder_bitstream_read(d,  6);
726                     L[1] = L[0];
727                     L[2] = mpc_decoder_bitstream_read(d,  6);
728                     break;
729                 case 0:
730                     L[0] = mpc_decoder_bitstream_read(d,  6);
731                     L[1] = mpc_decoder_bitstream_read(d,  6);
732                     L[2] = mpc_decoder_bitstream_read(d,  6);
733                     break;
734                 default:
735                     return;
736                 }
737             }
738         }
739         if (*ResR)
740         {
741             /*********** DSCF ************/
742             if (d->DSCF_Flag_R[n]==1)
743             {
744                 switch (d->SCFI_R[n])
745                 {
746                 case 3:
747                     R[0] = R[2] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
748                     R[1] = R[0];
749                     R[2] = R[1];
750                     break;
751                 case 1:
752                     R[0] = R[2] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
753                     R[1] = R[0] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
754                     R[2] = R[1];
755                     break;
756                 case 2:
757                     R[0] = R[2] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
758                     R[1] = R[0];
759                     R[2] = R[1] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
760                     break;
761                 case 0:
762                     R[0] = R[2] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
763                     R[1] = R[0] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
764                     R[2] = R[1] + mpc_decoder_huffman_decode(d,  mpc_table_DSCF_Entropie, 6);
765                     break;
766                 default:
767                     return;
768                 }
769                 if (R[0] > 1024)
770                     R[0] = 0x8080;
771                 if (R[1] > 1024)
772                     R[1] = 0x8080;
773                 if (R[2] > 1024)
774                     R[2] = 0x8080;
775             }
776             /************ SCF ************/
777             else
778             {
779                 switch (d->SCFI_R[n])
780                 {
781                 case 3:
782                     R[0] = mpc_decoder_bitstream_read(d, 6);
783                     R[1] = R[0];
784                     R[2] = R[1];
785                     break;
786                 case 1:
787                     R[0] = mpc_decoder_bitstream_read(d, 6);
788                     R[1] = mpc_decoder_bitstream_read(d, 6);
789                     R[2] = R[1];
790                     break;
791                 case 2:
792                     R[0] = mpc_decoder_bitstream_read(d, 6);
793                     R[1] = R[0];
794                     R[2] = mpc_decoder_bitstream_read(d, 6);
795                     break;
796                 case 0:
797                     R[0] = mpc_decoder_bitstream_read(d, 6);
798                     R[1] = mpc_decoder_bitstream_read(d, 6);
799                     R[2] = mpc_decoder_bitstream_read(d, 6);
800                     break;
801                 default:
802                     return;
803                     break;
804                 }
805             }
806         }
807     }
808 
809     if (seeking == TRUE)
810         return;
811 
812     /**************************** Samples ****************************/
813     ResL = d->Res_L;
814     ResR = d->Res_R;
815     for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR)
816     {
817         // setting pointers
818         x1 = mpc_table_SampleHuff[*ResL];
819         x2 = mpc_table_SampleHuff[*ResR];
820         L = d->Q[n].L;
821         R = d->Q[n].R;
822 
823         if (x1!=NULL || x2!=NULL)
824             for (k=0; k<36; ++k)
825             {
826                 if (x1 != NULL) *L++ = mpc_decoder_huffman_decode(d,  x1, 8);
827                 if (x2 != NULL) *R++ = mpc_decoder_huffman_decode(d,  x2, 8);
828             }
829 
830         if (*ResL>7 || *ResR>7)
831             for (k=0; k<36; ++k)
832             {
833                 if (*ResL>7) *L++ = (mpc_int32_t)mpc_decoder_bitstream_read(d,  Res_bit[*ResL]) - Dc[*ResL];
834                 if (*ResR>7) *R++ = (mpc_int32_t)mpc_decoder_bitstream_read(d,  Res_bit[*ResR]) - Dc[*ResR];
835             }
836     }
837 }
838 #endif //MPC_SUPPORT_SV456
839 /****************************************** SV 7 ******************************************/
840 void
mpc_decoder_read_bitstream_sv7(mpc_decoder * d,mpc_bool_t seeking)841 mpc_decoder_read_bitstream_sv7(mpc_decoder *d, mpc_bool_t seeking)
842 {
843     // these arrays hold decoding results for bundled quantizers (3- and 5-step)
844     static const 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};
845     static const 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};
846     static const 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};
847     static const 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};
848     static const 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};
849 
850     mpc_int32_t n,k;
851     mpc_int32_t Max_used_Band=0;
852     const HuffmanTyp *Table;
853     mpc_int32_t idx;
854     mpc_int32_t *L   ,*R;
855     mpc_int32_t *ResL,*ResR;
856     mpc_uint32_t tmp;
857 
858     /***************************** Header *****************************/
859     ResL  = d->Res_L;
860     ResR  = d->Res_R;
861 
862     // first subband
863     *ResL = mpc_decoder_bitstream_read(d, 4);
864     *ResR = mpc_decoder_bitstream_read(d, 4);
865     if (d->MS_used && !(*ResL==0 && *ResR==0)) {
866         d->MS_Flag[0] = mpc_decoder_bitstream_read(d, 1);
867     }
868 
869     // consecutive subbands
870     ++ResL; ++ResR; // increase pointers
871     for (n=1; n <= d->Max_Band; ++n, ++ResL, ++ResR)
872     {
873         idx   = mpc_decoder_huffman_decode(d, mpc_table_HuffHdr, 9);
874         *ResL = (idx!=4) ? *(ResL-1) + idx : (int) mpc_decoder_bitstream_read(d, 4);
875 
876         idx   = mpc_decoder_huffman_decode(d, mpc_table_HuffHdr, 9);
877         *ResR = (idx!=4) ? *(ResR-1) + idx : (int) mpc_decoder_bitstream_read(d, 4);
878 
879         if (d->MS_used && !(*ResL==0 && *ResR==0)) {
880             d->MS_Flag[n] = mpc_decoder_bitstream_read(d, 1);
881         }
882 
883         // only perform following procedures up to the maximum non-zero subband
884         if (*ResL!=0 || *ResR!=0) {
885             Max_used_Band = n;
886         }
887     }
888     /****************************** SCFI ******************************/
889     L     = d->SCFI_L;
890     R     = d->SCFI_R;
891     ResL  = d->Res_L;
892     ResR  = d->Res_R;
893     for (n=0; n <= Max_used_Band; ++n, ++L, ++R, ++ResL, ++ResR) {
894         if (*ResL) *L = mpc_decoder_huffman_decode(d, mpc_table_HuffSCFI, 3);
895         if (*ResR) *R = mpc_decoder_huffman_decode(d, mpc_table_HuffSCFI, 3);
896     }
897 
898     /**************************** SCF/DSCF ****************************/
899     ResL  = d->Res_L;
900     ResR  = d->Res_R;
901     L     = d->SCF_Index_L[0];
902     R     = d->SCF_Index_R[0];
903     for (n=0; n<=Max_used_Band; ++n, ++ResL, ++ResR, L+=3, R+=3) {
904         if (*ResL)
905         {
906             switch (d->SCFI_L[n])
907             {
908             case 1:
909                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
910                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
911                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
912                 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
913                 L[2] = L[1];
914                 break;
915             case 3:
916                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
917                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
918                 L[1] = L[0];
919                 L[2] = L[1];
920                 break;
921             case 2:
922                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
923                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
924                 L[1] = L[0];
925                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
926                 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
927                 break;
928             case 0:
929                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
930                 L[0] = (idx!=8) ? L[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
931                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
932                 L[1] = (idx!=8) ? L[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
933                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
934                 L[2] = (idx!=8) ? L[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
935                 break;
936             default:
937                 return;
938             }
939             if (L[0] > 1024)
940                 L[0] = 0x8080;
941             if (L[1] > 1024)
942                 L[1] = 0x8080;
943             if (L[2] > 1024)
944                 L[2] = 0x8080;
945         }
946         if (*ResR)
947         {
948             switch (d->SCFI_R[n])
949             {
950             case 1:
951                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
952                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
953                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
954                 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
955                 R[2] = R[1];
956                 break;
957             case 3:
958                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
959                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
960                 R[1] = R[0];
961                 R[2] = R[1];
962                 break;
963             case 2:
964                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
965                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
966                 R[1] = R[0];
967                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
968                 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
969                 break;
970             case 0:
971                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
972                 R[0] = (idx!=8) ? R[2] + idx : (int) mpc_decoder_bitstream_read(d, 6);
973                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
974                 R[1] = (idx!=8) ? R[0] + idx : (int) mpc_decoder_bitstream_read(d, 6);
975                 idx  = mpc_decoder_huffman_decode(d, mpc_table_HuffDSCF, 6);
976                 R[2] = (idx!=8) ? R[1] + idx : (int) mpc_decoder_bitstream_read(d, 6);
977                 break;
978             default:
979                 return;
980             }
981             if (R[0] > 1024)
982                 R[0] = 0x8080;
983             if (R[1] > 1024)
984                 R[1] = 0x8080;
985             if (R[2] > 1024)
986                 R[2] = 0x8080;
987         }
988     }
989 
990     if (seeking == TRUE)
991         return;
992 
993     /***************************** Samples ****************************/
994     ResL = d->Res_L;
995     ResR = d->Res_R;
996     L    = d->Q[0].L;
997     R    = d->Q[0].R;
998     for (n=0; n <= Max_used_Band; ++n, ++ResL, ++ResR, L+=36, R+=36)
999     {
1000         /************** links **************/
1001         switch (*ResL)
1002         {
1003         case  -2: case  -3: case  -4: case  -5: case  -6: case  -7: case  -8: case  -9:
1004         case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
1005             L += 36;
1006             break;
1007         case -1:
1008             for (k=0; k<36; k++ ) {
1009                 tmp  = mpc_random_int(d);
1010                 *L++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >>  8) & 0xFF) + ((tmp >>  0) & 0xFF) - 510;
1011             }
1012             break;
1013         case 0:
1014             L += 36;// increase pointer
1015             break;
1016         case 1:
1017             Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][1];
1018             for (k=0; k<12; ++k)
1019             {
1020                 idx = mpc_decoder_huffman_decode(d, Table, 9);
1021                 *L++ = idx30[idx];
1022                 *L++ = idx31[idx];
1023                 *L++ = idx32[idx];
1024             }
1025             break;
1026         case 2:
1027             Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][2];
1028             for (k=0; k<18; ++k)
1029             {
1030                 idx = mpc_decoder_huffman_decode(d, Table, 10);
1031                 *L++ = idx50[idx];
1032                 *L++ = idx51[idx];
1033             }
1034             break;
1035         case 3:
1036         case 4:
1037             Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
1038             for (k=0; k<36; ++k)
1039                 *L++ = mpc_decoder_huffman_decode(d, Table, 5);
1040             break;
1041         case 5:
1042             Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
1043             for (k=0; k<36; ++k)
1044                 *L++ = mpc_decoder_huffman_decode(d, Table, 8);
1045             break;
1046         case 6:
1047         case 7:
1048             Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResL];
1049             for (k=0; k<36; ++k)
1050                 *L++ = mpc_decoder_huffman_decode(d, Table, 14);
1051             break;
1052         case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
1053             tmp = Dc[*ResL];
1054             for (k=0; k<36; ++k)
1055                 *L++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResL]) - tmp;
1056             break;
1057         default:
1058             return;
1059         }
1060         /************** rechts **************/
1061         switch (*ResR)
1062         {
1063         case  -2: case  -3: case  -4: case  -5: case  -6: case  -7: case  -8: case  -9:
1064         case -10: case -11: case -12: case -13: case -14: case -15: case -16: case -17:
1065             R += 36;
1066             break;
1067         case -1:
1068                 for (k=0; k<36; k++ ) {
1069                     tmp  = mpc_random_int(d);
1070                     *R++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >>  8) & 0xFF) + ((tmp >>  0) & 0xFF) - 510;
1071                 }
1072                 break;
1073             case 0:
1074                 R += 36;// increase pointer
1075                 break;
1076             case 1:
1077                 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][1];
1078                 for (k=0; k<12; ++k)
1079                 {
1080                     idx = mpc_decoder_huffman_decode(d, Table, 9);
1081                     *R++ = idx30[idx];
1082                     *R++ = idx31[idx];
1083                     *R++ = idx32[idx];
1084                 }
1085                 break;
1086             case 2:
1087                 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][2];
1088                 for (k=0; k<18; ++k)
1089                 {
1090                     idx = mpc_decoder_huffman_decode(d, Table, 10);
1091                     *R++ = idx50[idx];
1092                     *R++ = idx51[idx];
1093                 }
1094                 break;
1095             case 3:
1096             case 4:
1097                 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
1098                 for (k=0; k<36; ++k)
1099                     *R++ = mpc_decoder_huffman_decode(d, Table, 5);
1100                 break;
1101             case 5:
1102                 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
1103                 for (k=0; k<36; ++k)
1104                     *R++ = mpc_decoder_huffman_decode(d, Table, 8);
1105                 break;
1106             case 6:
1107             case 7:
1108                 Table = mpc_table_HuffQ[mpc_decoder_bitstream_read(d, 1)][*ResR];
1109                 for (k=0; k<36; ++k)
1110                     *R++ = mpc_decoder_huffman_decode(d, Table, 14);
1111                 break;
1112             case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
1113                 tmp = Dc[*ResR];
1114                 for (k=0; k<36; ++k)
1115                     *R++ = (mpc_int32_t)mpc_decoder_bitstream_read(d, Res_bit[*ResR]) - tmp;
1116                 break;
1117             default:
1118                 return;
1119         }
1120     }
1121 }
1122 
mpc_decoder_setup(mpc_decoder * d,mpc_reader * r)1123 void mpc_decoder_setup(mpc_decoder *d, mpc_reader *r)
1124 {
1125   d->r = r;
1126 
1127   d->MPCHeaderPos = 0;
1128   d->StreamVersion = 0;
1129   d->MS_used = 0;
1130   d->FrameWasValid = 0;
1131   d->OverallFrames = 0;
1132   d->DecodedFrames = 0;
1133   d->TrueGaplessPresent = 0;
1134   d->WordsRead = 0;
1135   d->Max_Band = 0;
1136   d->SampleRate = 0;
1137   d->__r1 = 1;
1138   d->__r2 = 1;
1139 
1140   d->Max_Band = 0;
1141   d->seeking_window = FAST_SEEKING_WINDOW;
1142 
1143   mpc_decoder_reset_bitstream_decode(d);
1144   mpc_decoder_initialisiere_quantisierungstabellen(d, 1.0f);
1145 #if 0
1146   mpc_decoder_init_huffman_sv6(d);
1147   mpc_decoder_init_huffman_sv7(d);
1148 #endif
1149 }
1150 
get_initial_fpos(mpc_decoder * d)1151 static mpc_uint32_t get_initial_fpos(mpc_decoder *d)
1152 {
1153     mpc_uint32_t fpos = 0;
1154     switch ( d->StreamVersion ) {   // setting position to the beginning of the data-bitstream
1155     case  0x04: fpos =  48; break;
1156     case  0x05:
1157     case  0x06: fpos =  64; break;
1158     case  0x07:
1159     case  0x17: fpos = 200; break;
1160     }
1161     return fpos;
1162 }
1163 
mpc_decoder_set_streaminfo(mpc_decoder * d,mpc_streaminfo * si)1164 void mpc_decoder_set_streaminfo(mpc_decoder *d, mpc_streaminfo *si)
1165 {
1166     mpc_decoder_reset_synthesis(d);
1167     mpc_decoder_reset_globals(d);
1168 
1169     d->StreamVersion      = si->stream_version;
1170     d->MS_used            = si->ms;
1171     d->Max_Band           = si->max_band;
1172     d->OverallFrames      = si->frames;
1173     d->MPCHeaderPos       = si->header_position;
1174     d->TrueGaplessPresent = si->is_true_gapless;
1175     d->SampleRate         = (mpc_int32_t)si->sample_freq;
1176 
1177     d->samples_to_skip = MPC_DECODER_SYNTH_DELAY;
1178 }
1179 
mpc_decoder_initialize(mpc_decoder * d,mpc_streaminfo * si)1180 mpc_bool_t mpc_decoder_initialize(mpc_decoder *d, mpc_streaminfo *si)
1181 {
1182     mpc_decoder_set_streaminfo(d, si);
1183 
1184     // AB: setting position to the beginning of the data-bitstream
1185     mpc_decoder_seek(d, get_initial_fpos(d));
1186 
1187     d->seeking_pwr = 0;
1188     while( d->OverallFrames > ((mpc_int64_t) SEEKING_TABLE_SIZE << d->seeking_pwr) )
1189         d->seeking_pwr++;
1190     d->seeking_table_frames = 0;
1191     d->seeking_table[0] = get_initial_fpos(d);
1192 
1193     return TRUE;
1194 }
1195 
mpc_decoder_set_seeking(mpc_decoder * d,mpc_streaminfo * si,mpc_bool_t fast_seeking)1196 void mpc_decoder_set_seeking(mpc_decoder *d, mpc_streaminfo *si, mpc_bool_t fast_seeking)
1197 {
1198     d->seeking_window = FAST_SEEKING_WINDOW;
1199     if (si->fast_seek == 0 && fast_seeking == 0)
1200         d->seeking_window = SLOW_SEEKING_WINDOW;
1201 }
1202 
mpc_decoder_seek_seconds(mpc_decoder * d,double seconds)1203 mpc_bool_t mpc_decoder_seek_seconds(mpc_decoder *d, double seconds)
1204 {
1205     return mpc_decoder_seek_sample(d, (mpc_int64_t)(seconds * (double)d->SampleRate + 0.5));
1206 }
1207 
mpc_decoder_seek_sample(mpc_decoder * d,mpc_int64_t destsample)1208 mpc_bool_t mpc_decoder_seek_sample(mpc_decoder *d, mpc_int64_t destsample)
1209 {
1210     mpc_uint32_t fpos;
1211     mpc_uint32_t fwd;
1212 
1213     fwd = (mpc_uint32_t) (destsample / MPC_FRAME_LENGTH);
1214     d->samples_to_skip = MPC_DECODER_SYNTH_DELAY + (mpc_uint32_t)(destsample % MPC_FRAME_LENGTH);
1215 
1216     // resetting synthesis filter to avoid "clicks"
1217     mpc_decoder_reset_synthesis(d);
1218 
1219     // prevent from desired position out of allowed range
1220     fwd = fwd < d->OverallFrames  ?  fwd  :  d->OverallFrames;
1221 
1222     if (fwd > d->DecodedFrames + d->seeking_window || fwd < d->DecodedFrames) {
1223         memset(d->SCF_Index_L, 1, sizeof d->SCF_Index_L );
1224         memset(d->SCF_Index_R, 1, sizeof d->SCF_Index_R );
1225     }
1226 
1227     if (d->seeking_table_frames > d->DecodedFrames || fwd < d->DecodedFrames) {
1228         d->DecodedFrames = 0;
1229         if (fwd > d->seeking_window)
1230             d->DecodedFrames = (fwd - d->seeking_window) & ((~0u) << d->seeking_pwr);
1231         if (d->DecodedFrames > d->seeking_table_frames)
1232             d->DecodedFrames = d->seeking_table_frames;
1233         fpos = d->seeking_table[d->DecodedFrames >> d->seeking_pwr];
1234         mpc_decoder_seek(d, fpos);
1235     }
1236 
1237     // read the last 32 frames before the desired position to scan the scalefactors (artifactless jumping)
1238     for ( ; d->DecodedFrames < fwd; d->DecodedFrames++ ) {
1239         mpc_uint32_t RING = d->Zaehler;
1240         mpc_uint32_t FwdJumpInfo;
1241 
1242         // add seeking info
1243         if (d->seeking_table_frames < d->DecodedFrames &&
1244            (d->DecodedFrames & ((1 << d->seeking_pwr) - 1)) == 0) {
1245             d->seeking_table[d->DecodedFrames >> d->seeking_pwr] = mpc_decoder_bits_read(d);
1246             d->seeking_table_frames = d->DecodedFrames;
1247         }
1248 
1249         // read jump-info
1250         FwdJumpInfo  = mpc_decoder_bitstream_read(d, 20);
1251         FwdJumpInfo += mpc_decoder_bits_read(d);
1252 
1253         if (fwd <= d->DecodedFrames + d->seeking_window) {
1254             if (d->StreamVersion >= 7) {
1255                 mpc_decoder_read_bitstream_sv7(d, TRUE);
1256             } else {
1257 #ifdef MPC_SUPPORT_SV456
1258                 mpc_decoder_read_bitstream_sv6(d, TRUE);
1259 #else
1260                 return FALSE;
1261 #endif
1262             }
1263         }
1264         mpc_decoder_bitstream_jump(d, FwdJumpInfo - mpc_decoder_bits_read(d));
1265 
1266         // update buffer
1267         mpc_decoder_update_buffer(d, RING);
1268     }
1269 
1270     return TRUE;
1271 }
1272