1 /*
2     MPEG Maaate: An Australian MPEG audio analysis toolkit
3     Copyright (C) 2000 Commonwealth Scientific and Industrial Research Organisation
4     (CSIRO), Australia.
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 #ifndef LAYER3_H
22 #define LAYER3_H
23 
24 #include "allLayers.H"
25 #include "MPEGfile.H"
26 #include "mpegPlatform.h"
27 
28 using namespace std;
29 
30 //--------------------------------------------------------------------------
31 
32 // class definition
33 class CSAPI_MPEG Layer3 : public AllLayers {
34 
35   // Layer 3 specific header
36   struct Sideinfo {
37     unsigned int main_data_begin;  // 9 bit
38     unsigned int private_bits;     // 3/5 bit
39     struct {
40       unsigned scfsi[4];         // 1 bit
41       struct gr_info_s {
42 	unsigned part2_3_length;         // 12 bit
43 	unsigned big_values;             // 9 bit
44 	unsigned global_gain;            // 8 bit
45 	unsigned scalefac_compress;      // 4 bit
46 	unsigned window_switching_flag;  // 1 bit
47 	unsigned block_type;             // 2 bit
48 	unsigned mixed_block_flag;       // 1 bit
49 	unsigned table_select[3];        // 5 bit
50 	unsigned subblock_gain[3];       // 3 bit
51 	unsigned region0_count;          // 4 bit
52 	unsigned region1_count;          // 3 bit
53 	unsigned preflag;                // 1 bit
54 	unsigned scalefac_scale;         // 1 bit
55 	unsigned count1table_select;     // 1 bit
56       } gr[2]; // for each granule
57     } ch[2]; // for each channel
58   };
59 
60   // structure to store scalefactors in
61   struct ScaleFacTable {
62     int l[22];            // sfb (long windows)
63     int s[3][13];         // window*sfb (short windows)
64   };
65 
66 public:
67     Layer3(MPEGfile *mp);
68 
bitallocation(unsigned int ch,unsigned int sb)69     int    bitallocation (unsigned int ch, unsigned int sb) {
70       /* Layer 3 does not have any bitallocation information */
71       return -1;
72     };
73     int    scfsi (unsigned int ch, unsigned int scfclass);
74            float  scalefactor (unsigned int ch, unsigned int ss,
75 			       unsigned int gr);
76     int    sample (unsigned int ch, unsigned int sb,
77 		   unsigned int ss, unsigned int gr);
78     double restored_sample (unsigned int ch,  unsigned int sb,
79 			    unsigned int ss, unsigned int gr);
80     short  pcm_sample (unsigned int ch, unsigned int gr,
81 			      unsigned int sb, unsigned int ss);
82 
83     // layer-3 specific access function
84     double mdct_sample (unsigned int ch, unsigned int gr,
85 			       unsigned int ssb);
86     // more audio data information
87     unsigned int part2_3_length(unsigned int ch, unsigned int gr);
88     unsigned int big_values (unsigned int ch, unsigned int gr);
89     unsigned int count1_values (unsigned int ch, unsigned int gr);
90     unsigned int global_gain (unsigned int ch, unsigned int gr);
91     unsigned int scalefac_compress (unsigned int ch, unsigned int gr);
92     bool         window_switching (unsigned int ch, unsigned int gr);
93     unsigned int blocktype (unsigned int ch, unsigned int gr);
94     const char*  blocktype_str (unsigned int ch, unsigned int gr);
95     bool         mixedblock (unsigned int ch, unsigned int gr);
96     unsigned int table_select(unsigned int ch, unsigned int gr,
97 				     unsigned int area);
98     unsigned int subblock_gain(unsigned int ch, unsigned int gr,
99 				      unsigned int area);
100     unsigned int region0_samps (unsigned int ch, unsigned int gr);
101     unsigned int region1_samps (unsigned int ch, unsigned int gr);
102     unsigned int region2_samps (unsigned int ch, unsigned int gr);
103     unsigned int preflag (unsigned int ch, unsigned int gr);
104     unsigned int scalefac_scale (unsigned int ch, unsigned int gr);
105     unsigned int count1table_select(unsigned int ch, unsigned int gr);
106     unsigned int scf_band_bound_l (unsigned int sbindex);
107     unsigned int scf_band_bound_s(unsigned int sbindex);
108     unsigned int slen1 (unsigned int ch, unsigned int gr);
109     unsigned int slen2 (unsigned int ch, unsigned int gr);
110     unsigned int granules();
111 
112     unsigned int calc_CRC();
113     bool parse_data(DecodeLevel decode);
114 
115 
116     void printSideinfo();
117 
118     bool clearinterbuffer();
119 
120 private:
121     MPEGfile *frame;
122 
123 #ifdef DEBUG
124     long noReadBits;
125 #endif
126 
127     // audio data stored in intermediate bitbuffer.
128     // This is a "circular buffer" and takes any spare bytes from
129     // the frame's buffer to store between indexes bufstart and bufend.
130 #define MAX_INTER_BUFSIZE 1920 // max no of bytes stored (7680 bit/granule)
131     unsigned char inter_buffer[MAX_INTER_BUFSIZE];
132     unsigned int bufstart, bufend;
133     unsigned int whichbit;
134     unsigned int main_data_bitsread;
135     unsigned int endSpectralBits;   // to remember how many bits to read
136     unsigned int spectral_bitsread; // to calculate count1 values to read
137     unsigned int readbits(unsigned int nobits);
138     bool         setbufstart();
139     bool         savetointerbuffer();
140 
141     // read huffman decoder table
142     bool read_decoder_table();
143 
144     // remember how many count1-values were processed
145     // granules()*channels() count1 values
146     int count1_vals[2][2];
147 
148     // AUDIO DATA CONTENT:
149     // 9, 17 or 32 byte to be read into sideinfo header
150     Sideinfo si;
151     // 0-4 bit read per entry
152     // granules()*channels() scalefactors
153     ScaleFacTable scalefac[2][2];
154     // quantized value for frequency lines (0-19 bit to be read per entry)
155     // granules()*channels()*subbands()(32)*scalefactorbands()(18)
156     long int is[2][2][SBLIMIT][SSLIMIT];
157     // granules()*channels()*subbands()*scalefactorbands() restored samples
158     double xr[2][2][SBLIMIT][SSLIMIT];
159     // granules()*channels()*subbands()*scalefactorbands() restored stereo
160     double lr[2][2][SBLIMIT][SSLIMIT];
161     // granules()*channels()*subbands()*scalefactorbands() reordered samples
162     double ro[2][2][SBLIMIT][SSLIMIT];
163     // granules()*channels()*subbands()*scalefactorbands() hybrid filter input
164     double hybridIn[2][2][SBLIMIT][SSLIMIT];
165     // granules()*channels()*scalefactorbands()*subbands() hybrid filter output
166     double hybridOut[2][2][SSLIMIT][SBLIMIT];
167     // granules()*channels()*subsubbands()*subbands() pcm samples
168     short pcm[2][2][SSLIMIT][SBLIMIT];
169 
170     // frame processing functions
171     void decode_sideinfo();
172     void decode_scale_I(int gr, int ch);
173     void decode_scale_II(int gr, int ch); // for MPEG2
174     void decode_huffmanbits(int gr, int ch); // sample data
175     bool huffman_decoder(struct huffcodetab *h, // huffman code record
176 			 unsigned int *x,       // decoded x value
177 			 unsigned int *y,       // decoded y value
178 			 unsigned int *v,       // decoded v value
179 			 unsigned int *w);
180     void restore_samples(int gr, int ch);
181     void decode_jstereo(int gr);
182     void i_stereo_k_values(int ch, int gr, char is_pos, int i,
183 			   double k[2][SBSSLIMIT]); // MPEG2
184     void reorder(int gr, int ch);
185     void antialias(int gr, int ch);
186     void hybrid(int gr, int ch);
187     void inv_mdct(double in[18], double out[36], int bt);
188     void polyphase(int ch, int gr);
189 
190     // access to class member values
191     unsigned int main_data_begin();
192     unsigned int private_bits();
193     unsigned int region0_count (unsigned int ch, unsigned int gr);
194     unsigned int region1_count (unsigned int ch, unsigned int gr);
195     unsigned int scfsi_group (unsigned int ch, unsigned int group);
196     unsigned int sideinfo_length();
197     unsigned int main_data_slots();
198 
199     unsigned int scf_bands (unsigned int ch, unsigned int gr);
200 
201 
202 };
203 
204 //--------------------------------------------------------------------------
205 
206 #endif
207