1 #include "common.h"
2 #include "alloc_tables.h"
3 #include <ctype.h>
4 
5 /***********************************************************************
6 *
7 *  Global Variable Definitions
8 *
9 ***********************************************************************/
10 
11 char *mode_names[4] = { "stereo", "j-stereo", "dual-ch", "single-ch" };
12 char *layer_names[3] = { "I", "II", "III" };
13 
14 double s_freq[4] = { 44.1, 48, 32, 0 };
15 
16 int bitrate[3][16] = {
17   {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
18   {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
19   {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}
20 };
21 
22 double /*far */ multiple[64] = {
23   2.00000000000000, 1.58740105196820, 1.25992104989487,
24   1.00000000000000, 0.79370052598410, 0.62996052494744, 0.50000000000000,
25   0.39685026299205, 0.31498026247372, 0.25000000000000, 0.19842513149602,
26   0.15749013123686, 0.12500000000000, 0.09921256574801, 0.07874506561843,
27   0.06250000000000, 0.04960628287401, 0.03937253280921, 0.03125000000000,
28   0.02480314143700, 0.01968626640461, 0.01562500000000, 0.01240157071850,
29   0.00984313320230, 0.00781250000000, 0.00620078535925, 0.00492156660115,
30   0.00390625000000, 0.00310039267963, 0.00246078330058, 0.00195312500000,
31   0.00155019633981, 0.00123039165029, 0.00097656250000, 0.00077509816991,
32   0.00061519582514, 0.00048828125000, 0.00038754908495, 0.00030759791257,
33   0.00024414062500, 0.00019377454248, 0.00015379895629, 0.00012207031250,
34   0.00009688727124, 0.00007689947814, 0.00006103515625, 0.00004844363562,
35   0.00003844973907, 0.00003051757813, 0.00002422181781, 0.00001922486954,
36   0.00001525878906, 0.00001211090890, 0.00000961243477, 0.00000762939453,
37   0.00000605545445, 0.00000480621738, 0.00000381469727, 0.00000302772723,
38   0.00000240310869, 0.00000190734863, 0.00000151386361, 0.00000120155435,
39   1E-20
40 };
41 
42 int sbgrp[32] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 10, 10,
43   11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11
44 };
45 int sb_groups[12] = { 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 15, 26 /*31 */  };
46 
47 
48 int transmission_channel7[8][2] = {	/* for 5/2 config */
49   {10, 11},
50   {7, 11},
51   {10, 8},
52   {7, 8},
53   {10, 9},
54   {7, 9},
55   {9, 11},
56   {9, 8},
57 };
58 
59 int transmission_channel5[8][5] = {	/* for 3/2 config */
60   {0, 1, 2, 3, 4},
61   {0, 1, 5, 3, 4},
62   {0, 1, 6, 3, 4},
63   {0, 1, 2, 5, 4},
64   {0, 1, 2, 3, 6},
65   {0, 1, 2, 5, 6},
66   {0, 1, 6, 5, 4},
67   {0, 1, 5, 3, 6},
68 };
69 
70 int transmission_channel4a[6][4] = {	/* for 3/1 config */
71   {0, 1, 2, 3},
72   {0, 1, 5, 3},
73   {0, 1, 6, 3},
74   {0, 1, 2, 5},
75   {0, 1, 2, 6},
76   {0, 1, 5, 6},
77 };
78 
79 int transmission_channel4b[4][4] = {	/* for 2/2 config */
80   {0, 1, 2, 3},
81   {0, 1, 5, 3},
82   {0, 1, 2, 6},
83   {0, 1, 5, 6},
84 };
85 
86 int transmission_channel3[3][5] = {	/* for 3/0 (+ 2/0) and 2/1 config's */
87   {0, 1, 2, 3, 4},
88   {0, 1, 5, 3, 4},
89   {0, 1, 6, 3, 4},
90 };
91 
92 int no_channel[8][2] = {
93   {5, 6},
94   {2, 6},
95   {5, 2},
96   {3, 6},
97   {5, 4},
98   {3, 4},
99   {3, 2},
100   {2, 4},
101 };
102 
103 
104 int n_pred_coef[16];
105 int n_pred_coef4[16] = { 6, 4, 4, 4, 2, 2, 2, 0, 2, 2, 2, 0, 0, 0, 0, 0 };
106 int n_pred_coef3[16] = { 4, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
107 int n_pred_coef1[16] = { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
108 
109 int dyn_bbal5[19][12] = {	/* for 5/2 config */
110   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
111   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
112   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
113   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
114   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
115   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
116   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
117   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
118   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
119   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
120   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
121   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
122   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
123   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
124   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
125   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
126   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
127   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
128   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
129 };
130 int dyn_bbal4[15][12] = {	/* for 3/2 config */
131   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
132   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
133   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
134   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
135   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
136   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
137   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
138   {12, 12, 12, 12, 12, 12, 12, 12, 24, 21, 36, 87},
139   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
140   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
141   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
142   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
143   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
144   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
145   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
146 };
147 int dyn_bbal4PhC[15][12] = {	/* for 3/2 config in case of Phantom center coding */
148   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
149   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
150   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
151   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 0, 0},
152   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
153   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 12, 29},
154   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 12, 29},
155   {12, 12, 12, 12, 12, 12, 12, 12, 24, 21, 24, 58},
156   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
157   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
158   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
159   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
160   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
161   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 12, 29},
162   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
163 };
164 int dyn_bbal3[5][12] = {	/* for 3/1 and 2/2 config */
165   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
166   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
167   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
168   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 24, 58},
169   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
170 };
171 int dyn_bbal3PhC[5][12] = {	/* for 3/1 config in case of Phantom center coding */
172   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
173   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
174   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 0, 0},
175   {8, 8, 8, 8, 8, 8, 8, 8, 16, 14, 12, 29},
176   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
177 };
178 int dyn_bbal1[2][12] = {	/* for 3/0 (+2/0) and 2/1 config */
179   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
180   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 12, 29},
181 };
182 int dyn_bbal1PhC[2][12] = {	/* for 3/0 (+2/0) config in case of Phantom center coding */
183   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
184   {4, 4, 4, 4, 4, 4, 4, 4, 8, 7, 0, 0},
185 };
186 
187 
188 int dyn_ch5[19][2] = {		/* for 5/2 config */
189   {1, 1},
190   {1, 0},
191   {1, 0},
192   {1, 0},
193   {1, 0},
194   {0, 1},
195   {0, 0},
196   {0, 0},
197   {0, 0},
198   {0, 0},
199   {0, 1},
200   {0, 0},
201   {0, 0},
202   {0, 0},
203   {0, 1},
204   {0, 0},
205   {0, 0},
206   {0, 0},
207   {0, 0},
208 };
209 
210 int dyn_ch4[15][3] = {		/* for 3/2 config */
211   {1, 1, 1},
212   {1, 1, 0},
213   {1, 0, 1},
214   {0, 1, 1},
215   {1, 0, 0},
216   {0, 1, 0},
217   {0, 0, 1},
218   {0, 0, 0},
219   {1, 1, 0},
220   {1, 0, 1},
221   {1, 1, 0},
222   {1, 0, 0},
223   {1, 0, 0},
224   {0, 1, 0},
225   {1, 0, 0},
226 };
227 
228 int dyn_ch3[5][2] = {		/* for 3/1 and 2/2 config */
229   {1, 1},
230   {1, 0},
231   {0, 1},
232   {0, 0},
233   {1, 0},
234 };
235 
236 int dyn_ch1[2][3] = {		/* for 3/0 (+2/0) and 2/1 config */
237   {1, 1, 1},
238   {0, 1, 1},
239 };
240 
241 int T2[12], T3[12], T4[12], T5[12], T6[12];
242 int T2outof[12], T3outof[12], T4outof[12];	/* L0 or R0 */
243 int T2from[12], T3from[12], T4from[12];
244 
245 
246 /***********************************************************************
247 *
248 *  Global Function Definitions
249 *
250 ***********************************************************************/
251 
program_information(void)252 void program_information (void)
253 {
254   printf ("ISO MPEG Audio Subgroup Software Simulation Group (1996)\n");
255   printf ("ISO 13818-3 MPEG-2 Audio Multichannel Encoder\n");
256   printf ("%s\n", VERSION);
257 }
258 
259 /* The system uses a variety of data files.  By opening them via this
260    function, we can accommodate various locations. */
261 
OpenTableFile(char * name)262 FILE *OpenTableFile (char *name)
263 {
264   char fulname[80];
265   char *envdir;
266   FILE *f;
267 
268   fulname[0] = '\0';
269 
270 #ifdef TABLES_PATH
271   strcpy (fulname, TABLES_PATH);	/* default relative path for tables */
272 #endif	/* TABLES_PATH */		 /* (includes terminal path seperator */
273 
274 #ifdef UNIX			/* envir. variables for UNIX only */
275   {
276     char *getenv (const char *);
277 
278     envdir = getenv (MPEGTABENV);	/* check for environment */
279     if (envdir != NULL)
280       strcpy (fulname, envdir);
281     strcat (fulname, PATH_SEPARATOR);	/* add a "/" on the end */
282   }
283 #endif /* UNIX */
284 
285   strcat (fulname, name);
286   if ((f = fopen (fulname, "r")) == NULL) {
287     fprintf (stderr, "OpenTable: could not find %s\n", fulname);
288 
289 #ifdef UNIX
290     if (envdir != NULL)
291       fprintf (stderr, "Check %s directory '%s'\n", MPEGTABENV, envdir);
292     else
293       fprintf (stderr, "Check local directory './%s' or setenv %s\n",
294 	       TABLES_PATH, MPEGTABENV);
295 #else /* not unix : no environment variables */
296 
297 #ifdef TABLES_PATH
298     fprintf (stderr, "Check local directory './%s'\n", TABLES_PATH);
299 #endif /* TABLES_PATH */
300 
301 #endif /* UNIX */
302 
303   }
304   return f;
305 }
306 
307 
308 
js_bound(int lay,int m_ext)309 int js_bound (int lay, int m_ext)
310 {
311   static int jsb_table[3][4] = { {4, 8, 12, 16}, {4, 8, 12, 16},
312   {0, 4, 8, 16}
313   };				/* lay+m_e -> jsbound */
314 
315   if (lay < 1 || lay > 3 || m_ext < 0 || m_ext > 3) {
316     fprintf (stderr, "js_bound bad layer/modext (%d/%d)\n", lay, m_ext);
317     exit (1);
318   }
319 
320   return (jsb_table[lay - 1][m_ext]);
321 }
322 
hdr_to_frps(frame_params * fr_ps)323 void hdr_to_frps (frame_params * fr_ps)
324 {				/* interpret data in hdr str to fields in fr_ps */
325   layer *hdr = fr_ps->header;	/* (or pass in as arg?) */
326 
327   fr_ps->actual_mode = hdr->mode;
328   if (hdr->mode != MPG_MD_NONE)
329     fr_ps->stereo = (hdr->mode == MPG_MD_MONO) ? 1 : 2;
330   else
331     fr_ps->stereo = 0;
332 
333   if (verbosity >= 2)
334     printf ("stereo = %d stereomc = %d stereoaug = %d\n",
335 	    fr_ps->stereo, fr_ps->stereomc, fr_ps->stereoaug);
336 
337   if (hdr->lay == 2)
338     fr_ps->sblimit = pick_table (fr_ps);
339   else
340     fr_ps->sblimit = SBLIMIT;
341 
342   if (hdr->mode == MPG_MD_JOINT_STEREO)
343     fr_ps->jsbound = js_bound (hdr->lay, hdr->mode_ext);
344   else
345     fr_ps->jsbound = fr_ps->sblimit;
346 
347   if (hdr->multiling_ch > 0 && hdr->multiling_lay > 0) {
348     fprintf (stderr, "MultiLingual not in Layer 2!\n exit.\n");
349     exit (1);
350   }
351 
352   if (fr_ps->stereomc > 0 || hdr->lfe || hdr->multiling_ch > 0)
353     mc_pick_table (fr_ps);
354 }
355 
BitrateIndex(int layr,int bRate)356 int BitrateIndex (int layr, int bRate)
357 
358 {				/* convert bitrate in kbps to index */
359   /* 1 or 2 */
360   /* legal rates from 32 to 448 */
361   int index = 0;
362   int found = 0;
363 
364   while (!found && index < 15) {
365     if (bitrate[layr - 1][index] == bRate)
366       found = 1;
367     else
368       ++index;
369   }
370   if (found)
371     return (index);
372   else {
373     fprintf (stderr, "BitrateIndex: %d (layer %d) is not a legal bitrate\n",
374 	     bRate, layr);
375     return (-1);		/* Error! */
376   }
377 }
378 
SmpFrqIndex(long int sRate)379 int SmpFrqIndex (long int sRate)
380 
381 
382 
383 {				/* convert samp frq in Hz to index */
384   /* for MultiLingual LSF                                                       */
385   /* Note this function differs from the one called upon MPEG2 Audio-LSF coding */
386   /* The value '16' is used to detect half sample rate of ML wrt. MC            */
387   /* 7/8/95 WtK                                                                 */
388   switch (sRate) {
389   case 44100:
390     return (0);
391     break;
392   case 48000:
393     return (1);
394     break;
395   case 32000:
396     return (2);
397     break;
398   case 22050:
399     return (16);
400     break;
401   case 24000:
402     return (17);
403     break;
404   case 16000:
405     return (18);
406     break;
407   default:
408     fprintf (stderr, "SmpFrqIndex: %ld is not a legal sample rate\n", sRate);
409     return (-1);		/* Error! */
410   }
411 }
412 
413 /*******************************************************************************
414 *
415 *  Allocate number of bytes of memory equal to "block".
416 *
417 *******************************************************************************/
418 
mem_alloc(long unsigned int block,char * item)419 void *mem_alloc (long unsigned int block, char *item)
420 {
421   void *ptr;
422 
423   ptr = (void *) malloc (block);
424 
425   if (ptr != NULL)
426     memset (ptr, 0, block);
427   else {
428     printf ("Unable to allocate %s\n", item);
429     exit (1);
430   }
431 
432   return (ptr);
433 }
434 
435 /****************************************************************************
436 *
437 *  Free memory pointed to by "*ptr_addr".
438 *
439 *****************************************************************************/
440 
mem_free(void ** ptr_addr)441 void mem_free (void **ptr_addr)
442 {
443   if (*ptr_addr != NULL) {
444     free (*ptr_addr);
445     *ptr_addr = NULL;
446   }
447 }
448 
449 /****************************************************************************
450 *
451 *  Routines to convert between the Apple SANE extended floating point format
452 *  and the IEEE double precision floating point format.  These routines are
453 *  called from within the Audio Interchange File Format (AIFF) routines.
454 *
455 *****************************************************************************/
456 
457 /*
458 *** Apple's 80-bit SANE extended has the following format:
459 
460  1       15      1            63
461 +-+-------------+-+-----------------------------+
462 |s|       e     |i|            f                |
463 +-+-------------+-+-----------------------------+
464   msb        lsb   msb                       lsb
465 
466 The value v of the number is determined by these fields as follows:
467 If 0 <= e < 32767,              then v = (-1)^s * 2^(e-16383) * (i.f).
468 If e == 32767 and f == 0,       then v = (-1)^s * (infinity), regardless of i.
469 If e == 32767 and f != 0,       then v is a NaN, regardless of i.
470 
471 *** IEEE Draft Standard 754 Double Precision has the following format:
472 
473 MSB
474 +-+---------+-----------------------------+
475 |1| 11 Bits |           52 Bits           |
476 +-+---------+-----------------------------+
477  ^     ^                ^
478  |     |                |
479  Sign  Exponent         Mantissa
480 */
481 
482 /*****************************************************************************
483 *
484 *  double_to_extended()
485 *
486 *  Purpose:     Convert from IEEE double precision format to SANE extended
487 *               format.
488 *
489 *  Passed:      Pointer to the double precision number and a pointer to what
490 *               will hold the Apple SANE extended format value.
491 *
492 *  Outputs:     The SANE extended format pointer will be filled with the
493 *               converted value.
494 *
495 *  Returned:    Nothing.
496 *
497 *****************************************************************************/
498 
499 
double_to_extended(double * pd,char * ps)500 void double_to_extended (double *pd, char *ps)
501 {
502 
503 #ifdef  MACINTOSH
504 
505   x96tox80 (pd, (extended *) ps);
506 
507 #else
508 
509 /* fixed bus alignment error, HP 27-may-93 */
510 
511   register unsigned long top2bits;
512 
513   register unsigned short *ps2;
514   register IEEE_DBL *p_dbl;
515   register SANE_EXT *p_ext;
516   SANE_EXT ext_align;
517   char *c_align;
518   int i;
519 
520   p_dbl = (IEEE_DBL *) pd;
521   p_ext = &ext_align;
522   top2bits = p_dbl->hi & 0xc0000000;
523   p_ext->l1 = ((p_dbl->hi >> 4) & 0x3ff0000) | top2bits;
524   p_ext->l1 |= ((p_dbl->hi >> 5) & 0x7fff) | 0x8000;
525   p_ext->l2 = (p_dbl->hi << 27) & 0xf8000000;
526   p_ext->l2 |= ((p_dbl->lo >> 5) & 0x07ffffff);
527   ps2 = (unsigned short *) &(p_dbl->lo);
528   ps2++;
529   p_ext->s1 = (*ps2 << 11) & 0xf800;
530 
531   c_align = (char *) p_ext;
532   for (i = 0; i < 10; i++)
533     ps[i] = c_align[i];
534 
535 #endif
536 
537 }
538 
539 
540 /*****************************************************************************
541 *
542 *  extended_to_double()
543 *
544 *  Purpose:     Convert from SANE extended format to IEEE double precision
545 *               format.
546 *
547 *  Passed:      Pointer to the Apple SANE extended format value and a pointer
548 *               to what will hold the the IEEE double precision number.
549 *
550 *  Outputs:     The IEEE double precision format pointer will be filled with
551 *               the converted value.
552 *
553 *  Returned:    Nothing.
554 *
555 *****************************************************************************/
556 
extended_to_double(char * ps,double * pd)557 void extended_to_double (char *ps, double *pd)
558 {
559 
560 #ifdef  MACINTOSH
561 
562   x80tox96 ((extended *) ps, pd);
563 
564 #else
565 
566 /* fixed bus alignment error, HP 27-may-93 */
567 
568   register unsigned long top2bits;
569 
570   register IEEE_DBL *p_dbl;
571   register SANE_EXT *p_ext;
572   SANE_EXT ext_align;
573   char *c_align;
574   int i;
575 
576   p_dbl = (IEEE_DBL *) pd;
577   p_ext = &ext_align;
578 
579   c_align = (char *) p_ext;
580   for (i = 0; i < 10; i++)
581     c_align[i] = ps[i];
582 
583   top2bits = p_ext->l1 & 0xc0000000;
584   p_dbl->hi = ((p_ext->l1 << 4) & 0x3ff00000) | top2bits;
585   p_dbl->hi |= (p_ext->l1 << 5) & 0xffff0;
586   p_dbl->hi |= (p_ext->l2 >> 27) & 0x1f;
587   p_dbl->lo = (p_ext->l2 << 5) & 0xffffffe0;
588   p_dbl->lo |= (unsigned long) ((p_ext->s1 >> 11) & 0x1f);
589 
590 #endif
591 
592 }
593 
594 /*****************************************************************************
595 *
596 *  Read Audio Interchange File Format (AIFF) headers.
597 *
598 *****************************************************************************/
599 
aiff_read_headers(FILE * file_ptr,IFF_AIFF * aiff_ptr,int * byte_per_sample)600 int aiff_read_headers (FILE * file_ptr, IFF_AIFF * aiff_ptr,
601 		       int *byte_per_sample)
602 {
603 
604 
605   register int i;
606   //  register long seek_offset;
607 
608   char temp_sampleRate[10];
609   char *dummy;
610   //MFC char holder;
611   Chunk FormChunk;
612   CommonChunk CommChunk;
613   SoundDataChunk SndDChunk;
614   identifier ident;
615 
616   if (fseek (file_ptr, 0, SEEK_SET) != 0)
617     return (-1);
618 
619   if (fread (&FormChunk, sizeof (Chunk), 1, file_ptr) != 1)
620     return (-1);
621 
622 #ifdef MSDOS
623   holder = FormChunk.ckID[0];
624   FormChunk.ckID[0] = FormChunk.ckID[3];
625   FormChunk.ckID[3] = holder;
626   holder = FormChunk.ckID[1];
627   FormChunk.ckID[1] = FormChunk.ckID[2];
628   FormChunk.ckID[2] = holder;
629 /* fixed bug in next line, HP 27-may-93 */
630   holder = FormChunk.formType[0];
631   FormChunk.formType[0] = FormChunk.formType[3];
632   FormChunk.formType[3] = holder;
633   holder = FormChunk.formType[1];
634   FormChunk.formType[1] = FormChunk.formType[2];
635   FormChunk.formType[2] = holder;
636   FormChunk.ckSize = _lrotl (FormChunk.ckSize, 8);
637 #endif
638 
639 
640 /* fixed bug in next line, HP 27-may-93 */
641   if (strncmp (FormChunk.ckID, IFF_ID_FORM, 4) != 0 ||
642       strncmp (FormChunk.formType, IFF_ID_AIFF, 4) != 0)
643     return (-1);		/* warning: different levels of indirection.7/8/92.sr */
644 
645   if (fread (&ident, sizeof (identifier), 1, file_ptr) != 1)
646     return (-1);
647 
648 #ifdef MSDOS
649   holder = ident.name[0];
650   ident.name[0] = ident.name[3];
651   ident.name[3] = holder;
652   holder = ident.name[1];
653   ident.name[1] = ident.name[2];
654   ident.name[2] = holder;
655   ident.ck_length = _lrotl (ident.ck_length, 8);
656 #endif
657 
658 /* fixed bug in next line, HP 27-may-93 */
659   while (strncmp (ident.name, IFF_ID_COMM, 4) != 0) {
660     dummy = (char *) mem_alloc (ident.ck_length * sizeof (char), "dummy");
661     if (fread (dummy, ident.ck_length, 1, file_ptr) != 1)
662       return (-1);
663     mem_free ((void **) &dummy);
664 /* fixed bug in next line, HP 27-may-93 */
665     if (fread (&ident, sizeof (identifier), 1, file_ptr) != 1)
666       return (-1);
667 
668 #ifdef MSDOS
669     holder = ident.name[0];
670     ident.name[0] = ident.name[3];
671     ident.name[3] = holder;
672     holder = ident.name[1];
673     ident.name[1] = ident.name[2];
674     ident.name[2] = holder;
675     ident.ck_length = _lrotl (ident.ck_length, 8);
676 #endif
677   }
678 
679   for (i = 0; i < 4; ++i)
680     CommChunk.ckID[i] = ident.name[i];
681 
682   CommChunk.ckSize = ident.ck_length;
683 
684   if (fread (&CommChunk.numChannels, sizeof (short), 1, file_ptr) != 1)
685     return (-1);
686 
687   if (fread (&CommChunk.numSampleFrames, sizeof (unsigned long), 1, file_ptr) !=
688       1)
689     return (-1);
690 
691   if (fread (&CommChunk.sampleSize, sizeof (short), 1, file_ptr) != 1)
692     return (-1);
693 
694   if (fread (CommChunk.sampleRate, sizeof (char[10]), 1, file_ptr) != 1)
695     return (-1);
696 
697 #ifdef MSDOS
698   CommChunk.sampleSize = _rotl (CommChunk.sampleSize, 8);
699   CommChunk.ckSize = _lrotl (CommChunk.ckSize, 8);
700   CommChunk.numChannels = _rotl (CommChunk.numChannels, 8);
701   CommChunk.numSampleFrames = _lrotl (CommChunk.numSampleFrames, 8);
702 #endif
703 
704   *byte_per_sample = ceil ((double) CommChunk.sampleSize / 8);
705 
706   for (i = 0; i < sizeof (char[10]); i++)
707     temp_sampleRate[i] = CommChunk.sampleRate[i];
708 
709   extended_to_double (temp_sampleRate, &aiff_ptr->sampleRate);
710 
711 /* to start the search again from the beginning, HP 27-may-93 */
712   fseek (file_ptr, sizeof (Chunk), SEEK_SET);
713 
714   if (fread (&ident, sizeof (identifier), 1, file_ptr) != 1)
715     return (-1);
716 
717 #ifdef MSDOS
718   holder = ident.name[0];
719   ident.name[0] = ident.name[3];
720   ident.name[3] = holder;
721   holder = ident.name[1];
722   ident.name[1] = ident.name[2];
723   ident.name[2] = holder;
724   ident.ck_length = _lrotl (ident.ck_length, 8);
725 #endif
726 
727 /* fixed bug in next line, HP 27-may-93 */
728   while (strncmp (ident.name, IFF_ID_SSND, 4) != 0) {
729     dummy = (char *) mem_alloc (ident.ck_length * sizeof (char), "dummy");
730     if (fread (dummy, ident.ck_length, 1, file_ptr) != 1)
731       return (-1);
732     mem_free ((void **) &dummy);
733     if (fread (&ident, sizeof (identifier), 1, file_ptr) != 1)
734       return (-1);
735 /* the following lines are not necessary, HP 27-may-93 */
736 /*
737 	{
738 	fseek (file_ptr, 0, SEEK_SET);
739 	if (fread (&ident, sizeof (identifier), 1, file_ptr) != 1)
740 	return (-1);
741 	}
742 */
743 
744 #ifdef MSDOS
745     holder = ident.name[0];
746     ident.name[0] = ident.name[3];
747     ident.name[3] = holder;
748     holder = ident.name[1];
749     ident.name[1] = ident.name[2];
750     ident.name[2] = holder;
751     ident.ck_length = _lrotl (ident.ck_length, 8);
752 #endif
753   }
754 
755   for (i = 0; i < 4; ++i)
756     SndDChunk.ckID[i] = ident.name[i];
757   SndDChunk.ckSize = ident.ck_length;
758 
759   if (fread (&SndDChunk.offset, sizeof (unsigned long), 1, file_ptr) != 1)
760     return (-1);
761 
762   if (fread (&SndDChunk.blockSize, sizeof (unsigned long), 1, file_ptr) != 1)
763     return (-1);
764 
765 #ifdef MSDOS
766   SndDChunk.offset = _lrotl (SndDChunk.offset, 8);
767   SndDChunk.blockSize = _lrotl (SndDChunk.blockSize, 8);
768 #endif
769 
770 /* why seek behinde the SSND Chunk ????, HP 27-may-93 */
771 /*
772     seek_offset = SndDChunk.ckSize - sizeof (SoundDataChunk) + sizeof (ChunkHeader);
773 
774     if (fseek (file_ptr, seek_offset, SEEK_CUR) != 0)
775 	return (-1);
776 */
777 
778   aiff_ptr->numChannels = CommChunk.numChannels;
779   aiff_ptr->numSampleFrames = CommChunk.numSampleFrames;
780   aiff_ptr->sampleSize = CommChunk.sampleSize;
781   aiff_ptr->blkAlgn.offset = SndDChunk.offset;
782   aiff_ptr->blkAlgn.blockSize = SndDChunk.blockSize;
783   strncpy (aiff_ptr->sampleType, SndDChunk.ckID, 4);
784 
785   return (0);
786 }
787 
788 
789 /*****************************************************************************
790 *
791 *  Seek past some Audio Interchange File Format (AIFF) headers to sound data.
792 *
793 *****************************************************************************/
794 
aiff_seek_to_sound_data(FILE * file_ptr)795 int aiff_seek_to_sound_data (FILE * file_ptr)
796 {
797   if (fseek (file_ptr, sizeof (Chunk) + sizeof (SoundDataChunk), SEEK_SET) != 0)
798     return (-1);
799   else
800     return (0);
801 }
802 
803 /*******************************************************************************
804 *
805 *  Write Audio Interchange File Format (AIFF) headers.
806 *
807 *******************************************************************************/
808 
aiff_write_headers(FILE * file_ptr,IFF_AIFF * aiff_ptr)809 int aiff_write_headers (FILE * file_ptr, IFF_AIFF * aiff_ptr)
810 {
811   register short int i;
812   register long seek_offset;
813 
814   char temp_sampleRate[10];
815 
816   Chunk FormChunk;
817   CommonChunk CommChunk;
818   SoundDataChunk SndDChunk;
819 
820   strcpy (FormChunk.ckID, IFF_ID_FORM);
821   strcpy (FormChunk.formType, IFF_ID_AIFF);
822   strcpy (CommChunk.ckID, IFF_ID_COMM);	/*7/7/93,SR,changed FormChunk to CommChunk */
823 
824   double_to_extended (&aiff_ptr->sampleRate, temp_sampleRate);
825 
826   for (i = 0; i < sizeof (char[10]); i++)
827     CommChunk.sampleRate[i] = temp_sampleRate[i];
828 
829   CommChunk.numChannels = aiff_ptr->numChannels;
830   CommChunk.numSampleFrames = aiff_ptr->numSampleFrames;
831   CommChunk.sampleSize = aiff_ptr->sampleSize;
832   SndDChunk.offset = aiff_ptr->blkAlgn.offset;
833   SndDChunk.blockSize = aiff_ptr->blkAlgn.blockSize;
834   strncpy (SndDChunk.ckID, aiff_ptr->sampleType, 4);
835 
836   CommChunk.ckSize = sizeof (CommChunk.numChannels) +
837     sizeof (CommChunk.numSampleFrames) +
838     sizeof (CommChunk.sampleSize) + sizeof (CommChunk.sampleRate);
839 
840   SndDChunk.ckSize = sizeof (SoundDataChunk) - sizeof (ChunkHeader) +
841     (CommChunk.sampleSize + BITS_IN_A_BYTE - 1) / BITS_IN_A_BYTE *
842     CommChunk.numChannels * CommChunk.numSampleFrames;
843 
844   FormChunk.ckSize = sizeof (Chunk) + SndDChunk.ckSize + sizeof (ChunkHeader) +
845     CommChunk.ckSize;
846 
847   if (fseek (file_ptr, 0, SEEK_SET) != 0)
848     return (-1);
849 
850   if (fwrite (&FormChunk, sizeof (Chunk), 1, file_ptr) != 1)
851     return (-1);
852 
853   if (fwrite (&SndDChunk, sizeof (SoundDataChunk), 1, file_ptr) != 1)
854     return (-1);
855 
856   seek_offset =
857     SndDChunk.ckSize - sizeof (SoundDataChunk) + sizeof (ChunkHeader);
858 
859   if (fseek (file_ptr, seek_offset, SEEK_CUR) != 0)
860     return (-1);
861 
862   if (fwrite (CommChunk.ckID, sizeof (ID), 1, file_ptr) != 1)
863     return (-1);
864 
865   if (fwrite (&CommChunk.ckSize, sizeof (long), 1, file_ptr) != 1)
866     return (-1);
867 
868   if (fwrite (&CommChunk.numChannels, sizeof (short), 1, file_ptr) != 1)
869     return (-1);
870 
871   if (fwrite (&CommChunk.numSampleFrames, sizeof (unsigned long), 1, file_ptr)
872       != 1)
873     return (-1);
874 
875   if (fwrite (&CommChunk.sampleSize, sizeof (short), 1, file_ptr) != 1)
876     return (-1);
877 
878   if (fwrite (CommChunk.sampleRate, sizeof (char[10]), 1, file_ptr) != 1)
879     return (-1);
880 
881   return (0);
882 }
883 
884 /*****************************************************************************
885 *
886 *  bit_stream.c package
887 *  Author:  Jean-Georges Fritsch, C-Cube Microsystems
888 *
889 *****************************************************************************/
890 
891 /********************************************************************
892   This package provides functions to write (exclusive or read)
893   information from (exclusive or to) the bit stream.
894 
895   If the bit stream is opened in read mode only the get functions are
896   available. If the bit stream is opened in write mode only the put
897   functions are available.
898 ********************************************************************/
899 
900 /* refill the buffer from the input device when the buffer becomes empty    */
refill_buffer(Bit_stream_struc * bs)901 int refill_buffer (Bit_stream_struc * bs)
902 			/* bit stream structure */
903 {
904   register int i = bs->buf_size - 2 - bs->buf_byte_idx;
905   register unsigned long n=0;
906   register int index = 0;
907   char val[2];
908 
909   while ((i >= 0) && (!bs->eob)) {
910 
911     if (bs->format == BINARY)
912       n = fread (&bs->buf[i--], sizeof (unsigned char), 1, bs->pt);
913 
914     else {
915       while ((index < 2) && n) {
916 	n = fread (&val[index], sizeof (char), 1, bs->pt);
917 	switch (val[index]) {
918 	case 0x30:
919 	case 0x31:
920 	case 0x32:
921 	case 0x33:
922 	case 0x34:
923 	case 0x35:
924 	case 0x36:
925 	case 0x37:
926 	case 0x38:
927 	case 0x39:
928 	case 0x41:
929 	case 0x42:
930 	case 0x43:
931 	case 0x44:
932 	case 0x45:
933 	case 0x46:
934 	  index++;
935 	  break;
936 	default:
937 	  break;
938 	}
939       }
940 
941       if (val[0] <= 0x39)
942 	bs->buf[i] = (val[0] - 0x30) << 4;
943       else
944 	bs->buf[i] = (val[0] - 0x37) << 4;
945       if (val[1] <= 0x39)
946 	bs->buf[i--] |= (val[1] - 0x30);
947       else
948 	bs->buf[i--] |= (val[1] - 0x37);
949       index = 0;
950     }
951 
952     if (!n) {
953       bs->eob = i + 1;
954     }
955 
956   }
957   return (0); //MFC fixme: this func may as well be void()
958 }
959 
960 // MFC static char *he = "0123456789ABCDEF";
961 
962 /* empty the buffer to the output device when the buffer becomes full */
empty_buffer(Bit_stream_struc * bs,int minimum)963 void empty_buffer (Bit_stream_struc * bs, int minimum)
964 			/* bit stream structure */
965 			/* end of the buffer to empty */
966 {
967   register int i;
968 
969 #if BS_FORMAT == BINARY
970   for (i = bs->buf_size - 1; i >= minimum; i--)
971     fwrite (&bs->buf[i], sizeof (unsigned char), 1, bs->pt);
972 #else
973   for (i = bs->buf_size - 1; i >= minimum; i--) {
974     char val[2];
975     val[0] = he[((bs->buf[i] >> 4) & 0x0F)];
976     val[1] = he[(bs->buf[i] & 0x0F)];
977     fwrite (val, sizeof (char), 2, bs->pt);
978   }
979 #endif
980 
981   for (i = minimum - 1; i >= 0; i--)
982     bs->buf[bs->buf_size - minimum + i] = bs->buf[i];
983 
984   bs->buf_byte_idx = bs->buf_size - 1 - minimum;
985   bs->buf_bit_idx = 8;
986 }
987 
988 /* open the device to write the bit stream into it */
open_bit_stream_w(Bit_stream_struc * bs,char * bs_filenam,int size)989 void open_bit_stream_w (Bit_stream_struc * bs, char *bs_filenam, int size)
990 			/* bit stream structure */
991 			/* name of the bit stream file */
992 			/* size of the buffer */
993 {
994   if ((bs->pt = fopen (bs_filenam, "w+")) == NULL) {
995     printf ("Could not create \"%s\".\n", bs_filenam);
996     exit (0);
997   }
998   alloc_buffer (bs, size);
999   bs->buf_byte_idx = size - 1;
1000   bs->buf_bit_idx = 8;
1001   bs->totbit = 0;
1002   bs->mode = WRITE_MODE;
1003   bs->eob = FALSE;
1004   bs->eobs = FALSE;
1005 }
1006 
1007 /* open the device to read the bit stream from it */
open_bit_stream_r(Bit_stream_struc * bs,char * bs_filenam,int size)1008 void open_bit_stream_r (Bit_stream_struc * bs, char *bs_filenam, int size)
1009 			/* bit stream structure */
1010 			/* name of the bit stream file */
1011 			/* size of the buffer */
1012 {
1013   register unsigned long n;
1014   register int i = 0;
1015   register unsigned char flag = 1;
1016   unsigned char val;
1017 
1018   if ((bs->pt = fopen (bs_filenam, "rb")) == NULL) {
1019     printf ("Could not find \"%s\".\n", bs_filenam);
1020     exit (0);
1021   }
1022 
1023   do {
1024     n = fread (&val, sizeof (unsigned char), 1, bs->pt);
1025     switch (val) {
1026     case 0x30:
1027     case 0x31:
1028     case 0x32:
1029     case 0x33:
1030     case 0x34:
1031     case 0x35:
1032     case 0x36:
1033     case 0x37:
1034     case 0x38:
1035     case 0x39:
1036     case 0x41:
1037     case 0x42:
1038     case 0x43:
1039     case 0x44:
1040     case 0x45:
1041     case 0x46:
1042     case 0xa:			/* \n */
1043       break;
1044 
1045     default:			/* detection of an binary character */
1046       flag--;
1047       i = 300;
1048       break;
1049     }
1050 
1051   } while (flag & n);
1052 
1053   if (flag) {
1054     if (verbosity >= 2)
1055       printf ("the bit stream file %s is an ASCII file\n", bs_filenam);
1056     bs->format = ASCII;
1057   } else {
1058     bs->format = BINARY;
1059     if (verbosity >= 2)
1060       printf ("the bit stream file %s is a BINARY file\n", bs_filenam);
1061   }
1062 
1063   fclose (bs->pt);
1064 
1065   if ((bs->pt = fopen (bs_filenam, "rb")) == NULL) {
1066     printf ("Could not find \"%s\".\n", bs_filenam);
1067     exit (0);
1068   }
1069 
1070   alloc_buffer (bs, size);
1071   bs->buf_byte_idx = 0;
1072   bs->buf_bit_idx = 0;
1073   bs->totbit = 0;
1074   bs->mode = READ_MODE;
1075   bs->eob = FALSE;
1076   bs->eobs = FALSE;
1077 }
1078 
1079 /* close the device containing the bit stream after a read process */
close_bit_stream_r(Bit_stream_struc * bs)1080 void close_bit_stream_r (Bit_stream_struc * bs)
1081 {
1082   fclose (bs->pt);
1083   desalloc_buffer (bs);
1084 }
1085 
1086 /*close the device containing the bit stream after a write process*/
close_bit_stream_w(Bit_stream_struc * bs)1087 void close_bit_stream_w (Bit_stream_struc * bs)
1088 {
1089   empty_buffer (bs, bs->buf_byte_idx + 1);
1090   fclose (bs->pt);
1091   desalloc_buffer (bs);
1092 }
1093 
1094 /* open and initialize the buffer; */
alloc_buffer(Bit_stream_struc * bs,int size)1095 void alloc_buffer (Bit_stream_struc * bs, int size)
1096 {
1097   bs->buf =
1098     (unsigned char *) mem_alloc (size * sizeof (unsigned char), "buffer");
1099   bs->buf_size = size;
1100 }
1101 
1102 /* empty and close the buffer */
desalloc_buffer(Bit_stream_struc * bs)1103 void desalloc_buffer (Bit_stream_struc * bs)
1104 {
1105   free (bs->buf);
1106 }
1107 
1108 int putmask[9] = { 0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff };
1109 int mask[8] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };
1110 
1111 /*read 1 bit from the bit stream */
get1bit(Bit_stream_struc * bs)1112 unsigned int get1bit (Bit_stream_struc * bs)
1113 			/* bit stream structure */
1114 {
1115   unsigned int bit;
1116   register int i;
1117 
1118   bs->totbit++;
1119 
1120   if (!bs->buf_bit_idx) {
1121     bs->buf_bit_idx = 8;
1122     bs->buf_byte_idx--;
1123     if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
1124       if (bs->eob)
1125 	bs->eobs = TRUE;
1126       else {
1127 	for (i = bs->buf_byte_idx; i >= 0; i--)
1128 	  bs->buf[bs->buf_size - 1 - bs->buf_byte_idx + i] = bs->buf[i];
1129 	refill_buffer (bs);
1130 	bs->buf_byte_idx = bs->buf_size - 1;
1131       }
1132     }
1133   }
1134   bit = bs->buf[bs->buf_byte_idx] & mask[bs->buf_bit_idx - 1];
1135   bit = bit >> (bs->buf_bit_idx - 1);
1136   bs->buf_bit_idx--;
1137 #ifdef	PrintBitDebug
1138   printf ("pos: %5d getbits: %2d code: %4x val: %5d\n",
1139 	  bs->totbit - 1, 1, bit, bit);
1140   fflush (stdout);
1141 #endif
1142   return (bit);
1143 }
1144 
1145 /*write 1 bit from the bit stream */
put1bit(Bit_stream_struc * bs,int bit)1146 void put1bit (Bit_stream_struc * bs, int bit)
1147 			/* bit stream structure */
1148 			/* bit to write into the buffer */
1149 {
1150   //  register int i;
1151 
1152 #ifdef	PrintBitDebug
1153   printf ("pos: %5d putbits: %2d code: %4x val: %5d\n",
1154 	  bs->totbit, 1, bit, bit);
1155   fflush (stdout);
1156 #endif
1157 
1158   bs->totbit++;
1159 
1160   bs->buf[bs->buf_byte_idx] |= (bit & 0x1) << (bs->buf_bit_idx - 1);
1161   bs->buf_bit_idx--;
1162   if (!bs->buf_bit_idx) {
1163     bs->buf_bit_idx = 8;
1164     bs->buf_byte_idx--;
1165     if (bs->buf_byte_idx < 0)
1166       empty_buffer (bs, MINIMUM);
1167     bs->buf[bs->buf_byte_idx] = 0;
1168   }
1169 }
1170 
1171 /*read N bit from the bit stream */
getbits(Bit_stream_struc * bs,int N)1172 unsigned long getbits (Bit_stream_struc * bs, int N)
1173 			/* bit stream structure */
1174 			/* number of bits to read from the bit stream */
1175 {
1176   unsigned long val = 0;
1177   register int i;
1178   register int j = N;
1179   register int k, tmp;
1180 
1181   if (N > MAX_LENGTH)
1182     printf ("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
1183 
1184   bs->totbit += N;
1185   while (j > 0) {
1186     if (!bs->buf_bit_idx) {
1187       bs->buf_bit_idx = 8;
1188       bs->buf_byte_idx--;
1189       if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
1190 	if (bs->eob)
1191 	  bs->eobs = TRUE;
1192 	else {
1193 	  for (i = bs->buf_byte_idx; i >= 0; i--)
1194 	    bs->buf[bs->buf_size - 1 - bs->buf_byte_idx + i] = bs->buf[i];
1195 	  refill_buffer (bs);
1196 	  bs->buf_byte_idx = bs->buf_size - 1;
1197 	}
1198       }
1199     }
1200     k = MIN (j, bs->buf_bit_idx);
1201     tmp = bs->buf[bs->buf_byte_idx] & putmask[bs->buf_bit_idx];
1202     tmp = tmp >> (bs->buf_bit_idx - k);
1203     val |= tmp << (j - k);
1204     bs->buf_bit_idx -= k;
1205     j -= k;
1206   }
1207 #ifdef	PrintBitDebug
1208   printf ("pos: %5d getbits: %2d code: %4x val: %5d\n",
1209 	  bs->totbit - N, N, val, val);
1210   fflush (stdout);
1211 #endif
1212   return (val);
1213 }
1214 
1215 /*write N bits into the bit stream */
putbits(Bit_stream_struc * bs,unsigned int val,int N)1216 void putbits (Bit_stream_struc * bs, unsigned int val, int N)
1217 			/* bit stream structure */
1218 			/* val to write into the buffer */
1219 			/* number of bits of val */
1220 {
1221   //  register int i;
1222   register int j = N;
1223   register int k, tmp;
1224 
1225   if (N > MAX_LENGTH)
1226     printf ("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
1227 
1228 #ifdef	PrintBitDebug
1229   printf ("pos: %5d putbits: %2d code: %4x val: %5d\n",
1230 	  bs->totbit, N, val, val);
1231   fflush (stdout);
1232 #endif
1233 
1234   bs->totbit += N;
1235   while (j > 0) {
1236     k = MIN (j, bs->buf_bit_idx);
1237     tmp = val >> (j - k);
1238     bs->buf[bs->buf_byte_idx] |= (tmp & putmask[k]) << (bs->buf_bit_idx - k);
1239     bs->buf_bit_idx -= k;
1240     if (!bs->buf_bit_idx) {
1241       bs->buf_bit_idx = 8;
1242       bs->buf_byte_idx--;
1243       if (bs->buf_byte_idx < 0)
1244 	empty_buffer (bs, MINIMUM);
1245       bs->buf[bs->buf_byte_idx] = 0;
1246     }
1247     j -= k;
1248   }
1249 }
1250 
1251 /*return the current bit stream length (in bits)*/
sstell(Bit_stream_struc * bs)1252 unsigned long sstell (Bit_stream_struc * bs)
1253 			/* bit stream structure */
1254 {
1255   return (bs->totbit);
1256 }
1257 
1258 /*return the status of the bit stream*/
1259 /* returns 1 if end of bit stream was reached */
1260 /* returns 0 if end of bit stream was not reached */
end_bs(Bit_stream_struc * bs)1261 int end_bs (Bit_stream_struc * bs)
1262 			/* bit stream structure */
1263 {
1264   return (bs->eobs);
1265 }
1266 
1267 /*****************************************************************************
1268 *
1269 *  End of bit_stream.c package
1270 *
1271 *****************************************************************************/
1272 
transmission_channel(frame_params * fr_ps,int sbgr,int m)1273 int transmission_channel (frame_params * fr_ps, int sbgr, int m)
1274 {
1275   int config = fr_ps->config;
1276   int tca = fr_ps->header->tc_alloc[sbgr];
1277 
1278   /* 960627 FdB TCA table dependent on configuration */
1279   if (config == 320)
1280       return (transmission_channel5[tca][m]);	/* 3/2 */
1281   else if (config == 310)
1282     return (transmission_channel4a[tca][m]);	/* 3/1 */
1283   else if (config == 220)
1284     return (transmission_channel4b[tca][m]);	/* 2/2 */
1285   else if (config == 300 || config == 302 || config == 210)
1286     return (transmission_channel3[tca][m]);	/* 3/0 (+2/0) and 2/1 */
1287   else
1288     return (m);
1289 }
1290 
dyn_ch(frame_params * fr_ps,int sbgr,int m)1291 int dyn_ch (frame_params * fr_ps, int sbgr, int m)
1292 {
1293   int config = fr_ps->config;
1294   int dynx = fr_ps->header->dyn_cross[sbgr];
1295   int dynx2stereo = fr_ps->header->dyn_second_stereo[sbgr];
1296   /* 960627 FdB Dyn_ch table dependent on configuration */
1297   if (config == 320)
1298       return (dyn_ch4[dynx][m - 2]);
1299   else if (config == 310 || config == 220)
1300     return (dyn_ch3[dynx][m - 2]);
1301   else if (config == 300 || config == 302 || config == 210) {
1302     if (config == 302 && dynx2stereo && m == 4)
1303       return (0);
1304     else
1305       return (dyn_ch1[dynx][m - 2]);
1306   } else if (config == 202 && dynx2stereo && m == 3)
1307     return (0);
1308   else if (config == 102 && dynx2stereo && m == 2)
1309     return (0);
1310   else
1311     return (1);
1312 }
1313 
dyn_bbal(int config,int center,int dynx,int sbgr)1314 int dyn_bbal (int config, int center, int dynx, int sbgr)
1315 {
1316   /* 960627 FdB Dyn_ch table dependent on configuration */
1317   if (config == 320) {
1318     if (center == 3)
1319       return (dyn_bbal4PhC[dynx][sbgr]);
1320     else
1321       return (dyn_bbal4[dynx][sbgr]);
1322   } else if (config == 310 || config == 220) {
1323     if (center == 3)
1324       return (dyn_bbal3PhC[dynx][sbgr]);
1325     else
1326       return (dyn_bbal3[dynx][sbgr]);
1327   } else if (config == 300 || config == 302 || config == 210) {
1328     if (center == 3)
1329       return (dyn_bbal1PhC[dynx][sbgr]);
1330     else
1331       return (dyn_bbal1[dynx][sbgr]);
1332   } else
1333     return (0);
1334 }
1335 
dyn_bbal_2ndst(int dynx,int sbgr)1336 int dyn_bbal_2ndst (int dynx, int sbgr)
1337 {
1338   return (dyn_bbal4[dynx][sbgr]);
1339 }
1340 
1341 
1342 
init_mc_pred(frame_params * fr_ps)1343 void init_mc_pred (frame_params * fr_ps)
1344 {
1345   layer *info = fr_ps->header;
1346   int i, j, k;
1347 
1348   switch (fr_ps->config) {
1349   case 320:
1350     for (i = 0; i < 16; i++)
1351       n_pred_coef[i] = n_pred_coef4[i];
1352     break;
1353   case 310:
1354   case 220:
1355     for (i = 0; i < 16; i++)
1356       n_pred_coef[i] = n_pred_coef3[i];
1357     break;
1358   case 300:
1359   case 302:
1360   case 210:
1361     for (i = 0; i < 16; i++)
1362       n_pred_coef[i] = n_pred_coef1[i];
1363     break;
1364   default:
1365     for (i = 0; i < 16; i++)
1366       n_pred_coef[i] = 0;
1367     break;
1368   }
1369 
1370   for (i = 0; i < 8; i++) {
1371     info->mc_pred[i] = 0;
1372     for (j = 0; j < 6; j++) {
1373       info->delay_comp[i][j] = 0;
1374       info->predsi[i][j] = 0;
1375       for (k = 0; k < 3; k++)
1376 	info->pred_coef[i][j][k] = 127;
1377     }
1378   }
1379 }
1380 
set_mc_pred(frame_params * fr_ps)1381 void set_mc_pred (frame_params * fr_ps)
1382 {
1383   layer *info = fr_ps->header;
1384   //int config = fr_ps->config;
1385   int dynx;
1386   int i, j, k, npred;
1387 
1388   for (i = 0; i < 8; i++) {
1389     info->mc_pred[i] = rand () % 2;
1390     if (info->mc_pred[i]) {
1391       dynx = fr_ps->header->dyn_cross[i];
1392       npred = n_pred_coef[dynx];
1393       for (j = 0; j < npred; j++) {
1394 	info->predsi[i][j] = rand () % 4;
1395 	if (info->predsi[i][j]) {
1396 	  info->delay_comp[i][j] = rand () % 8;
1397 	  for (k = 0; k < info->predsi[i][j]; k++)
1398 	    info->pred_coef[i][j][k] = rand () % 256;
1399 	}
1400       }
1401     }
1402   }
1403 }
1404 
1405 
1406