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 decoder.h
36 
37 #ifndef _mpcdec_decoder_h_
38 #define _mpcdec_decoder_h_
39 
40 #include "huffman.h"
41 #include "math.h"
42 #include "mpcdec.h"
43 #include "reader.h"
44 #include "streaminfo.h"
45 
46 #define MPC_SUPPORT_SV456
47 
48 #define SEEKING_TABLE_SIZE  256u
49 // set it to SLOW_SEEKING_WINDOW to not use fast seeking
50 #define FAST_SEEKING_WINDOW 32
51 // set it to FAST_SEEKING_WINDOW to only use fast seeking
52 #define SLOW_SEEKING_WINDOW 0x80000000
53 
54 enum {
55     MPC_V_MEM = 2304,
56     MPC_DECODER_MEMSIZE = 16384,  // overall buffer size
57 };
58 
59 typedef struct {
60     mpc_int32_t  L [36];
61     mpc_int32_t  R [36];
62 } QuantTyp;
63 
64 typedef struct mpc_decoder_t {
65     mpc_reader *r;
66 
67     /// @name internal state variables
68     //@{
69 
70     mpc_uint32_t  dword; /// currently decoded 32bit-word
71     mpc_uint32_t  pos;   /// bit-position within dword
72     mpc_uint32_t  Speicher[MPC_DECODER_MEMSIZE]; /// read-buffer
73     mpc_uint32_t  Zaehler; /// actual index within read-buffer
74 
75     mpc_uint32_t  samples_to_skip;
76 
77     mpc_uint32_t  DecodedFrames;
78     mpc_uint32_t  OverallFrames;
79     mpc_int32_t   SampleRate;                 // Sample frequency
80 
81     mpc_uint32_t  StreamVersion;              // version of bitstream
82     mpc_int32_t   Max_Band;
83     mpc_uint32_t  MPCHeaderPos;               // AB: needed to support ID3v2
84 
85     mpc_uint32_t  FrameWasValid;
86     mpc_uint32_t  MS_used;                    // MS-coding used ?
87     mpc_uint32_t  TrueGaplessPresent;
88 
89     mpc_uint32_t  WordsRead;                  // counts amount of decoded dwords
90 
91     // randomizer state variables
92     mpc_uint32_t  __r1;
93     mpc_uint32_t  __r2;
94 
95     // seeking
96     mpc_uint32_t  seeking_table[SEEKING_TABLE_SIZE];
97     mpc_uint32_t  seeking_pwr;                // distance between 2 frames in seeking_table = 2^seeking_pwr
98     mpc_uint32_t  seeking_table_frames;       // last frame in seaking table
99     mpc_uint32_t  seeking_window;             // number of frames to look for scalefactors
100 
101     mpc_int32_t   SCF_Index_L [32] [3];
102     mpc_int32_t   SCF_Index_R [32] [3];       // holds scalefactor-indices
103     QuantTyp      Q [32];                     // holds quantized samples
104     mpc_int32_t   Res_L [32];
105     mpc_int32_t   Res_R [32];                 // holds the chosen quantizer for each subband
106     mpc_bool_t    DSCF_Flag_L [32];
107     mpc_bool_t    DSCF_Flag_R [32];           // differential SCF used?
108     mpc_int32_t   SCFI_L [32];
109     mpc_int32_t   SCFI_R [32];                // describes order of transmitted SCF
110     mpc_bool_t    MS_Flag[32];                // MS used?
111 #ifdef MPC_FIXED_POINT
112     unsigned char SCF_shift[256];
113 #endif
114 
115     MPC_SAMPLE_FORMAT V_L[MPC_V_MEM + 960];
116     MPC_SAMPLE_FORMAT V_R[MPC_V_MEM + 960];
117     MPC_SAMPLE_FORMAT Y_L[36][32];
118     MPC_SAMPLE_FORMAT Y_R[36][32];
119     MPC_SAMPLE_FORMAT SCF[256]; ///< holds adapted scalefactors (for clipping prevention)
120     //@}
121 
122 } mpc_decoder;
123 
124 #endif // _mpc_decoder_h
125