1 
2 #define PRINTOUT
3 
4 #define	VERSION	"$Revision: 2.0 $"
5 
6 #ifdef  UNIX
7 #define         TABLES_PATH     "tables/"  /* to find data files */
8 /* name of environment variable holding path of table files */
9 #define         MPEGTABENV      "MPEGTABLES"
10 #define         PATH_SEPARATOR  "/"        /* how to build paths */
11 #define         PROTO_ARGS                 /* unix gcc uses arg. prototypes */
12 #endif  /* UNIX */
13 
14 /***********************************************************************
15 *
16 *  Global Include Files
17 *
18 ***********************************************************************/
19 
20 #include        <stdio.h>
21 #include        <string.h>
22 #include        <math.h>
23 
24 #ifdef  UNIX
25 #include        <unistd.h>	 /* removed 92-08-05 shn */
26 #include	<stdlib.h>       /* put in 92-08-05 shn */
27 #endif  /* UNIX */
28 
29 
30 /***********************************************************************
31 *
32 *  Global Definitions
33 *
34 ***********************************************************************/
35 
36 /* General Definitions */
37 
38 #define         FLOAT                   float
39 
40 #define         FALSE                   0
41 #define         TRUE                    (!FALSE)
42 #define         NULL_CHAR               '\0'
43 
44 #define         MAX_U_32_NUM            0xFFFFFFFF
45 #define         PI                      3.14159265358979
46 #define         PI4                     PI/4
47 #define         PI64                    PI/64
48 #define         LN_TO_LOG10             0.2302585093
49 
50 #define         VOL_REF_NUM             0
51 #define         MPEG_AUDIO_ID           1
52 #define         MAC_WINDOW_SIZE         24
53 
54 #define         MONO                    1
55 #define         STEREO                  2
56 #define         BITS_IN_A_BYTE          8
57 #define         WORD                    16
58 #define         MAX_NAME_SIZE           81
59 #define         SBLIMIT                 32
60 #define         FFT_SIZE                1024
61 #define         HAN_SIZE                512
62 #define         SCALE_BLOCK             12
63 #define         SCALE_RANGE             63
64 #define         SCALE                   32768
65 #define         CRC16_POLYNOMIAL        0x8005
66 
67 #define         MAX_PRED_COEFF          3.96875     /* gleichfoermige Quantisierung der */
68                                                     /* Praediktorkoeff. im Bereich */
69                                                     /* [-MAX_PRED_COEF .. +MAX_PRED_COEF] */
70 
71 #define CHANMAX1        5
72 #define CHANMAX2        7
73 #define CHANMAX3       12
74 #define SBGRS          12
75 
76 /* Channels */
77 
78 #define	    L0	    0
79 #define	    R0	    1
80 #define	    C	    2
81 #define	    LS	    3
82 #define	    RS	    4
83 #define	    L	    5
84 #define	    R	    6
85 #define	    L7	    7
86 #define	    R7	    8
87 #define	    C7	    9
88 #define	    LC	    10
89 #define	    RC	    11
90 
91 
92 /* Sums */
93 
94 #define	    T23	    0
95 #define	    T24	    1
96 #define	    T34	    2
97 #define	    T234    3
98 #define	    T56     4
99 
100 /* MPEG Header Definitions - Mode Values */
101 
102 #define         MPG_MD_STEREO           0
103 #define         MPG_MD_JOINT_STEREO     1
104 #define         MPG_MD_DUAL_CHANNEL     2
105 #define		MPG_MD_MONO		3
106 #define		MPG_MD_NONE		4
107 
108 /* AIFF Definitions */
109 
110 #ifndef	MS_DOS
111 #define         IFF_ID_FORM             "FORM" /* HP400 unix v8.0: double qoutes 1992-07-24 shn */
112 #define         IFF_ID_AIFF             "AIFF"
113 #define         IFF_ID_COMM             "COMM"
114 #define         IFF_ID_SSND             "SSND"
115 #define         IFF_ID_MPEG             "MPEG"
116 #else
117 #define         IFF_ID_FORM             "FORM"
118 #define         IFF_ID_AIFF             "AIFF"
119 #define         IFF_ID_COMM             "COMM"
120 #define         IFF_ID_SSND             "SSND"
121 #define         IFF_ID_MPEG             "MPEG"
122 #endif
123 
124 /* "bit_stream.h" Definitions */
125 
126 #define         MINIMUM         4    /* Minimum size of the buffer in bytes */
127 #define         MAX_LENGTH      32   /* Maximum length of word written or
128                                         read from bit stream */
129 #define         READ_MODE       0
130 #define         WRITE_MODE      1
131 #define         ALIGNING        8
132 #define         BINARY          0
133 #define         ASCII           1
134 #define         BS_FORMAT       BINARY /* BINARY or ASCII = 2x bytes */
135 #define         BUFFER_SIZE     4096
136 
137 #define         MIN(A, B)       ((A) < (B) ? (A) : (B))
138 #define         MAX(A, B)       ((A) > (B) ? (A) : (B))
139 
140 /***********************************************************************
141 *
142 *  Global Type Definitions
143 *
144 ***********************************************************************/
145 
146 /* Structure for Reading Layer II Allocation Tables from File */
147 
148 typedef struct {
149         unsigned int    steps;
150         unsigned int    bits;
151         unsigned int    group;
152         unsigned int    quant;
153 } sb_alloc, *alloc_ptr;
154 
155 typedef sb_alloc        al_table[SBLIMIT][16];
156 
157 /* Header Information Structure */
158 
159 typedef struct {
160     int version;
161     int lay;
162     int error_protection;
163     int bitrate_index;
164     int sampling_frequency;
165     int padding;
166     int extension;
167     int mode;
168     int mode_ext;
169     int copyright;
170     int original;
171     int emphasis;
172     int center;			 /* center present */
173     int surround; 		 /* surrounds present*/
174     int audio_mix;		 /* large listening room or not? */
175     int matrix;			 /* type of dematricing */
176     int lfe;			 /* low frequency effect channel*/
177     int multiling_ch;		 /* number of multilingual channels */
178     int multiling_fs;		 /* fs of ML channels is half fs of main channels */
179     int multiling_lay;		 /* Layer IIML or Layer II ML is used*/
180     int ext_bit_stream_present;  /* extension bitstream present? */
181     int n_ad_bytes;              /* if yes, how many bits anc.data for mpeg2 */
182     int mc_pred[8];		 /* prediction bit*/
183     int tc_alloc[12];		 /* transmission channel allocation in each sbgroup*/
184     int predsi[8][6];		 /* predictor select information*/
185     int delay_comp[8][6];
186     int pred_coef[8][6][3];
187     int mc_prediction_on;
188     int tc_sbgr_select;
189     int tc_allocation;
190     int dyn_cross_on;
191     int dyn_cross_LR;            /* center out of Lo or Ro*/
192     int dyn_cross[12];
193     int ext_length;
194     int ext_sync;
195     int ext_bit;
196     int copy_ident_bit;
197     int copy_ident_start;
198     int dyn_second_stereo[12];
199 #ifdef Augmentation_7ch
200     int aug_mtx_proc;
201     int aug_dyn_cross_on;
202     int aug_future_ext;
203     int aug_tc_alloc[12];	   /* transmission channel allocation in each sbgroup */
204     int aug_dyn_cross[12];
205 #endif
206 } layer, *the_layer;
207 
208 
209 
210 /* Parent Structure Interpreting some Frame Parameters in Header */
211 
212 typedef struct {
213          layer                   *header;        /* raw header information */
214 	 int			 actual_mode;	 /* when writing IS, may forget if 0 chs */
215          al_table                *alloc;         /* bit allocation table read in */
216 	 al_table		 *alloc_mc;      /* MC bit allocation table read in */
217          al_table                *alloc_ml;      /* ML bit allocation table read in */
218          int                     tab_num;        /* number of table as loaded */
219          int                     tab_num_mc;     /* number of MC table as loaded */
220          int                     tab_num_ml;     /* number of ML table as loaded */
221 	 int			 stereo; 	 /* 1 for mono, 2 for stereo */
222 	 int			 stereomc;
223 	 int			 stereoaug;
224 	 int			 lfe_pos;
225 	 int			 jsbound;	 /* first band of joint stereo coding */
226 	 double		         mnr_min;	 /* mnr for dynamic bitallocation */
227          int                     sblimit;        /* total number of sub bands */
228          int                     sblimit_mc;     /* total number of MC sub bands */
229          int                     sblimit_ml;     /* total number of ML sub bands */
230 	 int                     phantom_c;      /* phantom coding of Center signal */
231 	 int                     config;      	 /* config code: 320, 310, 300, 302, 220, 210, 200, 202, 100, 102 */
232 } frame_params;
233 
234 /* Double and SANE Floating Point Type Definitions */
235 
236 typedef struct  IEEE_DBL_struct {
237                 unsigned long   hi;
238                 unsigned long   lo;
239 } IEEE_DBL;
240 
241 typedef struct  SANE_EXT_struct {
242                 unsigned long   l1;
243                 unsigned long   l2;
244                 unsigned short  s1;
245 } SANE_EXT;
246 
247 /* AIFF Type Definitions */
248 
249 typedef char	 ID[4];
250 
251 typedef struct  identifier_struct{
252 					 ID name;
253 					 long ck_length;
254 }identifier;
255 
256 
257 typedef struct  ChunkHeader_struct {
258                 ID      ckID;
259                 long    ckSize;
260 } ChunkHeader;
261 
262 typedef struct  Chunk_struct {
263                 ID      ckID;
264                 long    ckSize;
265                 ID      formType;
266 } Chunk;
267 
268 typedef struct  CommonChunk_struct {
269                 ID              ckID;
270                 long            ckSize;
271                 short           numChannels;
272                 unsigned long   numSampleFrames;
273                 short           sampleSize;
274                 char            sampleRate[10];
275 } CommonChunk;
276 
277 typedef struct  SoundDataChunk_struct {
278                 ID              ckID;
279                 long            ckSize;
280                 unsigned long   offset;
281                 unsigned long   blockSize;
282 } SoundDataChunk;
283 
284 typedef struct  blockAlign_struct {
285                 unsigned long   offset;
286                 unsigned long   blockSize;
287 } blockAlign;
288 
289 typedef struct  IFF_AIFF_struct {
290                 short           numChannels;
291                 unsigned long   numSampleFrames;
292                 short           sampleSize;
293                 double          sampleRate;
294                 ID/*char**/     sampleType;/*must be allocated 21.6.93 SR*/
295                 blockAlign      blkAlgn;
296 } IFF_AIFF;
297 
298 /* "bit_stream.h" Type Definitions */
299 
300 typedef struct  bit_stream_struc {
301     FILE        *pt;            /* pointer to bit stream device */
302     unsigned char *buf;         /* bit stream buffer */
303     int         buf_size;       /* size of buffer (in number of bytes) */
304     long        totbit;         /* bit counter of bit stream */
305     int         buf_byte_idx;   /* pointer to top byte in buffer */
306     int         buf_bit_idx;    /* pointer to top bit of top byte in buffer */
307     int         mode;           /* bit stream open in read or write mode */
308     int         eob;            /* end of buffer index */
309     int         eobs;           /* end of bit stream flag */
310     char        format;
311 /* format of file in rd mode (BINARY/ASCII) */
312     } Bit_stream_struc;
313 
314 /* JMZ 08/03/1995 begin SORT*/
315 typedef struct {
316          int    tc_channel;
317          int    audio_channel;
318          int    subband;
319          int    sbgroup;
320          int    used;
321          double mnr;
322          int    next;
323 } channel_sort;
324 /* JMZ 08/03/1995 end SORT*/
325 
326 /***********************************************************************
327 *
328 *  Global Variable External Declarations
329 *
330 ***********************************************************************/
331 
332 extern char     *mode_names[4];
333 extern char     *layer_names[3];
334 extern double   s_freq[4];
335 extern int	bitrate[3][16];
336 extern double	multiple[64];
337 extern int      sbgrp[32];
338 extern int      sb_groups[12];
339 extern int      n_pred_coef[16];
340 extern int	no_channel[8][2];
341 extern int	T2[SBGRS], T3[SBGRS], T4[SBGRS];
342 #ifdef Augmentation_7ch
343 extern int	T5[SBGRS], T6[SBGRS];
344 #endif
345 extern int	T2outof[12], T3outof[12], T4outof[12]; /* L0 or R0 */
346 extern int	T2from[12], T3from[12], T4from[12];
347 extern int	verbosity;
348 
349 /***********************************************************************
350 *
351 *  Global Function Prototype Declarations
352 *
353 ***********************************************************************/
354 
355 /* The following functions are in the file "common.c" */
356 
357 void	      program_information(void);
358 
359 FILE           *OpenTableFile(char *name);
360 //int            read_bit_alloc(int table, al_table (*alloc));
361 //int            pick_table(frame_params *fr_ps);
362 int            js_bound(int lay, int m_ext);
363 void           hdr_to_frps(frame_params *fr_ps);
364 int            BitrateIndex(int layr, int bRate);
365 int            SmpFrqIndex(long int sRate);
366 void           *mem_alloc(long unsigned int block, char *item);
367 void           mem_free(void **ptr_addr);
368 void           double_to_extended(double *pd, char *ps);
369 void           extended_to_double(char *ps, double *pd);
370 int            aiff_read_headers(FILE *file_ptr, IFF_AIFF *aiff_ptr, int *byte_per_sample);
371 int            aiff_seek_to_sound_data(FILE *file_ptr);
372 int            aiff_write_headers(FILE *file_ptr, IFF_AIFF *aiff_ptr);
373 int            refill_buffer(Bit_stream_struc *bs);
374 void           empty_buffer(Bit_stream_struc *bs, int minimum);
375 void           open_bit_stream_w(Bit_stream_struc *bs, char *bs_filenam, int size);
376 void           open_bit_stream_r(Bit_stream_struc *bs, char *bs_filenam, int size);
377 void           close_bit_stream_r(Bit_stream_struc *bs);
378 void           close_bit_stream_w(Bit_stream_struc *bs);
379 void           alloc_buffer(Bit_stream_struc *bs, int size);
380 void           desalloc_buffer(Bit_stream_struc *bs);
381 unsigned int   get1bit(Bit_stream_struc *bs);
382 void           put1bit(Bit_stream_struc *bs, int bit);
383 unsigned long  getbits(Bit_stream_struc *bs, int N);
384 void           putbits(Bit_stream_struc *bs, unsigned int val, int N);
385 unsigned long  sstell (Bit_stream_struc *bs);
386 int            end_bs (Bit_stream_struc *bs);
387 int            transmission_channel (frame_params *fr_ps, int sbgr, int m);
388 int            dyn_ch (frame_params *fr_ps, int sbgr, int m);
389 int            dyn_bbal (int config, int center, int dynx, int m);
390 int            dyn_bbal_2ndst (int dynx, int m);
391 void           init_mc_pred (frame_params *fr_ps);
392 void           set_mc_pred (frame_params *fr_ps);
393