1 #include "common.h"
2 #include "encoder.h"
3 #include <math.h>
4 #include "encode.h"
5 #include "psycho_1.h"
6 #include "psycho_2.h"
7 #include "lfe.h"
8 #include "predisto.h"
9 #include "lingual.h"
10 #include "crc.h"
11 #include "audio_read.h"
12 #include "subband.h"
13 #include "config.h"
14 #include "musicin.h"
15
16 /* Global variable definitions for "musicin.c" */
17
18 FILE *musicin;
19 FILE *musicin_ml; /* 08/03/1995 JMZ Multilingual */
20 Bit_stream_struc bs;
21 Bit_stream_struc bs_ext;
22 Bit_stream_struc bs_mpg;
23 char *programName;
24 int mesg_index = 0; /* YBKim060695 ancillary */
25 int verbosity;
26
27 char multilingual_file_name[MAX_NAME_SIZE]; /* 08/03/1995 JMZ Multilingual */
28
29 /* Implementations */
30
31
32 /************************************************************************
33 *
34 * main
35 *
36 * PURPOSE: MPEG I Encoder supporting layers 1 and 2, and
37 * psychoacoustic models 1 (MUSICAM) and 2 (AT&T),now portated
38 * to multichannel (two front channels, one center and three surround
39 * channels. There are different possibilities to code the AIFF-signal,
40 * like coding only the surround channels or the front channels,
41 * different bitrates for front and surropund channels etc.
42 * dec1992 sr.
43 *
44 * SEMANTICS: One overlapping frame of audio of up to 2 channels are
45 * processed at a time in the following order:
46 * (associated routines are in parentheses)
47 *
48 * 1. Filter sliding window of data to get 32 subband
49 * samples per channel.
50 * (window_subband,filter_subband)
51 *
52 * 2. If joint stereo mode, combine left and right channels
53 * for subbands above #jsbound#.
54 * (*_combine_LR)
55 *
56 * 3. Calculate scalefactors for the frame, and if layer 2,
57 * also calculate scalefactor select information.
58 * (*_scale_factor_calc)
59 *
60 * 4. Calculate psychoacoustic masking levels using selected
61 * psychoacoustic model.
62 * (*_Psycho_One, psycho_anal)
63 *
64 * 5. Perform iterative bit allocation for subbands with low
65 * mask_to_noise ratios using masking levels from step 4.
66 * (*_main_bit_allocation)
67 *
68 * 6. If error protection flag is active, add redundancy for
69 * error protection.
70 * (*_CRC_calc)
71 *
72 * 7. Pack bit allocation, scalefactors, and scalefactor select
73 * information (layer 2) onto bitstream.
74 * (*_encode_bit_alloc,*_encode_scale,II_transmission_pattern)
75 *
76 * 8. Quantize subbands and pack them into bitstream
77 * (*_subband_quantization, *_sample_encoding)
78 *
79 ************************************************************************/
80
81
82 typedef double JSBS[2][3][12][SBLIMIT];
83 typedef double IN[14][HAN_SIZE]; /* JMZ 08/03/1995 */
84 typedef unsigned int SUB[14][3][12][SBLIMIT]; /* JMZ 08/03/1995 */
85 typedef double SAM[12];
86 typedef unsigned int LSB[12];
87
main(int argc,char ** argv)88 int main (int argc, char **argv)
89 {
90 double sb_sample[14][3][12][SBLIMIT]; /* JMZ 08/03/1995 */
91 JSBS *j_sample;
92 IN *win_que;
93 SUB *subband;
94 SAM *lfe_sample;
95 LSB *lfe_sbband;
96
97 frame_params fr_ps;
98 layer info;
99 char original_file_name[MAX_NAME_SIZE];
100 char encoded_file_name[MAX_NAME_SIZE];
101 char encoded_file_name_ext[MAX_NAME_SIZE];
102 char encoded_file_name_mpg[MAX_NAME_SIZE];
103 double *win_buf[14]; /* JMZ 08/03/1995 */
104 static double buffer[14][1152]; /* JMZ 08/03/1995 */
105 double spiki[14][SBLIMIT]; /* JMZ 08/03/1995 */
106 static unsigned int bit_alloc[14][SBLIMIT], scfsi[14][SBLIMIT], scfsi_dyn[14][SBLIMIT]; /* JMZ 08/03/1995 */
107 static unsigned int scalar[14][3][SBLIMIT], j_scale[2][3][SBLIMIT],
108 scalar_dyn[14][3][SBLIMIT];
109 static double ltmin[14][SBLIMIT], lgmin[14][SBLIMIT], max_sc[14][SBLIMIT], smr[14][SBLIMIT]; /* JMZ 08/03/1995 */
110
111 static unsigned int lfe_alloc;
112 static unsigned int lfe_scalar;
113
114 FLOAT snr32[32];
115 short sam[14][1056]; /* JMZ 08/03/1995 */
116 double buffer_matr[14][1152]; /* JMZ 08/03/1995 */
117 int whole_SpF, extra_slot = 0;
118 double avg_slots_per_frame, frac_SpF, slot_lag;
119 int model, stereo, error_protection, stereomc, stereoaug;
120 static unsigned int crc, crcmc, crcext; //crcaug MFC
121 int i, j, k, adb, l, m, lfe;
122 // int p, s
123 unsigned long bitsPerSlot, samplesPerFrame, frameNum = 0;
124 unsigned long frameBits, sentBits = 0;
125 unsigned long num_samples;
126 unsigned long num_samples_ml; /* 08/03/1995 JMZ Multilingual */
127 int odd_ml = 1; /* LSF MultiLingual: audio inputting only at odd frames , WtK 7/8/95 */
128 int ml_eof = 0; /* MultiLingual: frame loop stops if either audio or ML-file has been exhausted , WtK 7/8/95 */
129
130 IFF_AIFF aiff_ptr;
131 IFF_AIFF aiff_ptr_ml; /* 08/03/1995 JMZ Multilingual */
132 int aiff = 0; /* misused to indicate mc-input */
133 int byte_per_sample = 0;
134
135 int mat_mode = 0;
136 int aug_mat_mode = 0;
137 int cha_sw = -2;
138 int aug_cha_sw = 0;
139 int dyn_cr = 0;
140 int dyn_cr_LR = 0;
141 int aug_dyn_cr = 0;
142 int predis = 0;
143 int bit_rate = 0;
144 unsigned int crc_buffer[15];
145
146 int lfe_bits = 0;
147 // int sentBits1, frameBits1;
148 //int hi, hu, ho;
149
150 /* char *blubb = "rm ";*/
151 char blubb[128]; /* Corrected BUG JMZ 29/03/1995 */
152
153 int ancillaryUse = 0; /* YBKim060695 ancillary */
154
155
156 #ifdef PRINTOUT
157 int loop_channel, loop_subband;
158
159 al_table *loop_alloc; /* a pointer to a table */
160 // sb_alloc loop_struct; /* a structure of 4 int's */
161 //alloc_ptr loop_str_ptr; /* a pointer to an sb_alloc structure */
162 float loop_bits;
163 #endif
164
165
166 program_information ();
167
168 /* Most large variables are declared dynamically to ensure
169 compatibility with smaller machines */
170
171 j_sample = (JSBS *) mem_alloc (sizeof (JSBS), "j_sample");
172
173 win_que = (IN *) mem_alloc (sizeof (IN), "Win_que");
174 subband = (SUB *) mem_alloc (sizeof (SUB), "subband");
175
176 lfe_sample = (SAM *) mem_alloc (sizeof (SAM), "lfe_sample");
177 lfe_sbband = (LSB *) mem_alloc (sizeof (LSB), "lfe_sbband");
178
179 /* clear buffers */
180 memset ((char *) buffer, 0, sizeof (buffer));
181 memset ((char *) bit_alloc, 0, sizeof (bit_alloc));
182 memset ((char *) scalar, 0, sizeof (scalar));
183 memset ((char *) j_scale, 0, sizeof (j_scale));
184 memset ((char *) scfsi, 0, sizeof (scfsi));
185 memset ((char *) ltmin, 0, sizeof (ltmin));
186 memset ((char *) lgmin, 0, sizeof (lgmin));
187 memset ((char *) max_sc, 0, sizeof (max_sc));
188 memset ((char *) snr32, 0, sizeof (snr32));
189 memset ((char *) sam, 0, sizeof (sam));
190
191 fr_ps.header = &info;
192 info.mode_ext = 0;
193 fr_ps.tab_num = -1; /* no table loaded */
194 fr_ps.tab_num_mc = -1;
195 fr_ps.tab_num_ml = -1;
196 fr_ps.alloc = NULL;
197 fr_ps.alloc_mc = NULL;
198 fr_ps.alloc_ml = NULL;
199
200 info.version = MPEG_AUDIO_ID;
201 info.bitrate_index = 0;
202 info.lfe = 0; /* no low frequency effect channel present! */
203 info.center = 0;
204 info.surround = 0;
205
206 info.multiling_ch = 0;
207 info.multiling_fs = 0;
208 info.multiling_lay = 0;
209
210 info.matrix = 0;
211 info.ext_bit_stream_present = 0;
212 info.n_ad_bytes = DFLT_NADB;
213 info.audio_mix = 0;
214 info.dyn_cross_LR = 0; /* C out of Lo */
215 info.mc_prediction_on = 0;
216 info.ext_length = 0;
217 info.ext_bit = 0;
218 info.copy_ident_bit = 0;
219 info.copy_ident_start = 0;
220
221 fr_ps.config = 0;
222 fr_ps.phantom_c = 0;
223 fr_ps.stereomc = 0;
224 fr_ps.stereoaug = 0;
225
226 programName = argv[0];
227 if (argc == 1)
228 usage();
229 #ifdef DONTUSEINTERACTIVE
230 /* no command-line args */
231 obtain_parameters (&fr_ps, &model, &num_samples, &num_samples_ml,
232 original_file_name, encoded_file_name, &aiff_ptr,
233 &aiff_ptr_ml, &aiff, &byte_per_sample, &mat_mode,
234 &aug_mat_mode, &cha_sw, &aug_cha_sw,
235 encoded_file_name_ext, &bit_rate, encoded_file_name_mpg,
236 &dyn_cr, &dyn_cr_LR, &aug_dyn_cr, &ancillaryUse,
237 &verbosity);
238 #endif
239 else
240 parse_args (argc, argv, &fr_ps, &model, &num_samples, &num_samples_ml,
241 original_file_name, encoded_file_name, &aiff_ptr, &aiff_ptr_ml,
242 &aiff, &byte_per_sample, &mat_mode, &aug_mat_mode, &cha_sw,
243 &aug_cha_sw, encoded_file_name_ext, &bit_rate,
244 encoded_file_name_mpg, &dyn_cr, &dyn_cr_LR, &aug_dyn_cr,
245 &ancillaryUse, &verbosity);
246
247 hdr_to_frps (&fr_ps);
248
249 if (aiff != 1)
250 fr_ps.stereomc = 0;
251
252 print_config (&fr_ps, &model, &num_samples,
253 original_file_name, encoded_file_name, &aiff);
254
255 stereo = fr_ps.stereo;
256 stereomc = fr_ps.stereomc;
257 stereoaug = fr_ps.stereoaug;
258 lfe = info.lfe;
259 error_protection = info.error_protection;
260
261 if (info.lay == 1) {
262 bitsPerSlot = 32;
263 samplesPerFrame = 384;
264 } else {
265 bitsPerSlot = 8;
266 samplesPerFrame = 1152;
267 }
268 /* Figure average number of 'slots' per frame. */
269 /* Bitrate means TOTAL for both channels, not per side. */
270 if (bit_rate == 0)
271 bit_rate = bitrate[info.lay - 1][info.bitrate_index];
272 if (verbosity >= 2)
273 printf ("bit_rate = %d\n", bit_rate);
274 fflush (stderr);
275
276 avg_slots_per_frame =
277 ((double) samplesPerFrame / s_freq[info.sampling_frequency]) *
278 ((double) bit_rate / (double) bitsPerSlot);
279
280 whole_SpF = (int) avg_slots_per_frame; /* Bytes per frame within datastream */
281 if (verbosity >= 2)
282 printf ("slots/frame = %d\n", whole_SpF);
283 frac_SpF = avg_slots_per_frame - (double) whole_SpF;
284 slot_lag = -frac_SpF;
285 if (verbosity >= 2)
286 printf ("frac SpF=%.3f, tot bit_rate=%d kbps, s freq=%.1f kHz\n",
287 frac_SpF, bit_rate, s_freq[info.sampling_frequency]);
288
289 if (frac_SpF != 0) {
290 if (verbosity >= 2)
291 printf ("Fractional number of slots, padding required\n");
292 } else
293 info.padding = 0;
294
295 #ifdef PRINTOUT
296 if (verbosity >= 3) {
297 printf ("\nFrame ");
298 for (loop_subband = 0; loop_subband < SBLIMIT; loop_subband++)
299 printf ("%3d", loop_subband);
300 printf ("\n");
301 }
302 #endif
303
304 while ((get_audio(musicin, buffer, num_samples, stereo, &aiff_ptr,
305 stereomc, stereoaug, &fr_ps, &aiff, &byte_per_sample,
306 buffer_matr) > 0) && (!ml_eof)
307 ) {
308 /****************************************************************/
309 /* */
310 /* START OF FRAME LOOP */
311 /* */
312 /****************************************************************/
313
314 /* the following allocation must happen within the while-loop. 1/5/93, SR */
315
316 if (mat_mode == -1) {
317 if (fr_ps.config == 320 || fr_ps.config == 310)
318 info.matrix = rand () % 4;
319 else {
320 info.matrix = rand () % 3;
321 if (info.matrix == 2)
322 info.matrix++;
323 }
324 } else
325 info.matrix = mat_mode;
326
327 init_mc_pred (&fr_ps);
328 info.tc_sbgr_select = 1;
329 info.dyn_cross_on = 0;
330 if (dyn_cr != 0 || fr_ps.config == 302 || fr_ps.config == 202)
331 if (dyn_cr_LR == -1)
332 info.dyn_cross_LR = rand () % 2;
333 else
334 info.dyn_cross_LR = dyn_cr_LR;
335 else
336 info.dyn_cross_LR = 0;
337
338 for (i = 0; i < 12; i++) {
339 info.tc_alloc[i] = 0;
340 info.dyn_cross[i] = 0;
341 info.dyn_second_stereo[i] = 0;
342 }
343
344 lfe_alloc = 0;
345
346 if (stereomc > 0
347 && (fr_ps.header->center == 1 || fr_ps.header->center == 3)) {
348 if (fr_ps.phantom_c == 1)
349 fr_ps.header->center = 3;
350 if (fr_ps.phantom_c == 0)
351 fr_ps.header->center = 1;
352 }
353
354 if (verbosity >= 1) {
355 fprintf (stderr, "{%4lu}\r", frameNum++);
356 fflush (stderr);
357 }
358
359 win_buf[0] = &buffer[0][0];
360 win_buf[1] = &buffer[1][0];
361 win_buf[2] = &buffer[2][0];
362 win_buf[3] = &buffer[3][0];
363 win_buf[4] = &buffer[4][0];
364 win_buf[5] = &buffer[5][0];
365 win_buf[6] = &buffer[6][0];
366 win_buf[7] = &buffer[7][0]; /* JMZ 08/03/1995 Multilingual */
367 win_buf[8] = &buffer[8][0]; /* JMZ 08/03/1995 Multilingual */
368 win_buf[9] = &buffer[9][0]; /* JMZ 08/03/1995 Multilingual */
369 win_buf[10] = &buffer[10][0]; /* JMZ 08/03/1995 Multilingual */
370 win_buf[11] = &buffer[11][0]; /* JMZ 08/03/1995 Multilingual */
371 win_buf[12] = &buffer[12][0]; /* JMZ 08/03/1995 Multilingual */
372 win_buf[13] = &buffer[13][0]; /* JMZ 08/03/1995 Multilingual */
373
374 if (frac_SpF != 0) {
375 if (slot_lag > (frac_SpF - 1.0)) {
376 slot_lag -= frac_SpF;
377 extra_slot = 0;
378 info.padding = 0;
379 if (verbosity >= 2)
380 printf ("No padding for this frame\n");
381 } else {
382 extra_slot = 1;
383 info.padding = 1;
384 slot_lag += (1 - frac_SpF);
385 }
386 }
387 adb = (whole_SpF + extra_slot) * bitsPerSlot;
388 if (verbosity >= 2)
389 printf ("Adb0: %i\n", adb);
390
391 /**************************************************************************/
392 /* 08/03/1995 JMZ Multilingual , WtK 7/8/95 */
393
394 if (info.multiling_ch > 0) {
395 if (odd_ml) {
396 if (get_audio_ml (musicin_ml, buffer, num_samples_ml, &aiff_ptr, &fr_ps,
397 &aiff, &byte_per_sample,
398 buffer_matr) != 1152 * info.multiling_ch) {
399 if (verbosity >= 2)
400 printf ("\nHit end of MultiLingual file.\n");
401 ml_eof = 1;
402 }
403
404 for (i = 0; i < 3; i++)
405 for (j = 0; j < 12; j++)
406 for (k = 7; k < 7 + info.multiling_ch; k++) {
407 window_subband (&win_buf[k], &(*win_que)[k][0], k);
408 filter_subband (&(*win_que)[k][0], &(sb_sample)[k][i][j][0]);
409 }
410 if (info.multiling_fs == 1)
411 ml_sb_sample_swap (7, 7 + info.multiling_ch, sb_sample); /* half fs */
412 } else
413 ml_sb_sample_shift (7, 7 + info.multiling_ch, sb_sample);
414 }
415
416 /* 08/03/1995 JMZ Multilingual */
417 /**************************************************************************/
418
419
420 switch (info.lay) {
421
422 case 2:
423 if (aiff != 1) {
424 for (i = 0; i < 3; i++)
425 for (j = 0; j < 12; j++)
426 for (k = 0; k < stereo; k++) {
427 window_subband (&win_buf[k], &(*win_que)[k][0], k);
428 filter_subband (&(*win_que)[k][0], &(sb_sample)[k][i][j][0]);
429 }
430
431 l = 0;
432 m = stereo;
433
434 II_scale_factor_calc (&fr_ps, sb_sample, scalar, fr_ps.sblimit, l, m);
435 if (info.multiling_ch > 0)
436 II_scale_factor_calc (&fr_ps, sb_sample, scalar, fr_ps.sblimit_ml, 7,
437 7 + info.multiling_ch);
438
439 pick_scale (scalar, &fr_ps, max_sc, cha_sw, aug_cha_sw, aiff);
440 if (fr_ps.actual_mode == MPG_MD_JOINT_STEREO) {
441 II_combine_LR (sb_sample, *j_sample, fr_ps.sblimit);
442 II_scale_factor_calc (&fr_ps, *j_sample, j_scale, fr_ps.sblimit, 0,
443 1);
444 }
445
446
447 /* this way we calculate more mono than we need */
448 /* but it is cheap */
449
450 psycho_1 (buffer, max_sc, ltmin, &fr_ps, smr, spiki, aiff);
451 if (info.multiling_ch > 0) {
452 if (odd_ml)
453 II_Psycho_One_ml (buffer, max_sc, ltmin, &fr_ps, smr, spiki);
454 if (info.multiling_fs) {
455 pick_scale_ml_2 (&fr_ps, sb_sample, max_sc);
456 for (k = 7; k < 7 + info.multiling_ch; k++)
457 II_smr (<min[k][0], &smr[k][0], &spiki[k][0], &max_sc[k][0],
458 fr_ps.sblimit_ml, 0, fr_ps.sblimit_ml);
459 }
460 }
461
462 II_transmission_pattern (scalar, scfsi, &fr_ps);
463
464 II_main_bit_allocation (smr, ltmin, scfsi, bit_alloc, &adb,
465 &fr_ps, &aiff, sb_sample,
466 scalar, max_sc, buffer_matr,
467 spiki, *j_sample, j_scale,
468 dyn_cr, aug_dyn_cr, scfsi_dyn, scalar_dyn);
469
470 if (error_protection)
471 II_CRC_calc (&fr_ps, bit_alloc, scfsi, &crc);
472
473 encode_info (&fr_ps, &bs);
474
475 if (error_protection)
476 encode_CRC (crc, &bs);
477
478 II_encode_bit_alloc (bit_alloc, &fr_ps, &bs);
479
480 k = 0;
481 i = stereo;
482 II_encode_scale (bit_alloc, scfsi, scalar, lfe_alloc,
483 lfe_scalar, &fr_ps, &bs, &k, &i);
484 II_subband_quantization (scalar, sb_sample, j_scale, *j_sample,
485 bit_alloc, *subband, &fr_ps);
486 II_sample_encoding (*subband, bit_alloc, &fr_ps, &bs);
487
488 /*******************************************************/
489 /* 08/03/1995 JMZ Multilingual */
490 if (info.multiling_ch > 0) {
491 k = 7;
492 i = info.multiling_ch + 7;
493 II_encode_bit_alloc_ml (bit_alloc, &fr_ps, &bs);
494 II_encode_scale_ml (bit_alloc, scfsi, scalar, &fr_ps, &bs, &k, &i);
495 II_subband_quantization_ml (scalar, sb_sample, j_scale,
496 *j_sample, bit_alloc, *subband, &fr_ps);
497 II_sample_encoding_ml (*subband, bit_alloc, &fr_ps, &bs);
498 }
499 /* 08/03/1995 JMZ Multilingual */
500 /*******************************************************/
501
502
503 /* YBKim060695 ancillary */
504 if (ancillaryUse)
505 ancillary_encode (&fr_ps, &bs, adb);
506 else
507 for (i = 0; i < adb; i++)
508 put1bit (&bs, 0);
509 }
510
511 /*********************** Now Layer 2 with MC extension **************************/
512
513 else {
514 if (verbosity >= 2)
515 printf ("Adb1: %i\n", adb);
516 if (lfe) {
517 lfe_bits = adb;
518 lfe_filter (&win_buf[fr_ps.lfe_pos], *lfe_sample);
519 lf_scalefactor (*lfe_sample, &lfe_scalar);
520 lfe_allocation (&lfe_alloc, &adb);
521 lfe_bits -= adb;
522 }
523
524 for (i = 0; i < 3; i++)
525 for (j = 0; j < 12; j++) {
526 l = 0;
527
528 for (k = 0; k < 7; k++) {
529 if (lfe && (k == fr_ps.lfe_pos))
530 l += 1;
531 window_subband (&win_buf[l], &(*win_que)[k][0], k);
532 filter_subband (&(*win_que)[k][0], &(sb_sample)[k][i][j][0]);
533 l++;
534 }
535 }
536
537 normalizing (sb_sample, &fr_ps);
538
539 matricing (sb_sample, &fr_ps);
540
541 l = 0;
542 if (stereoaug == 2)
543 m = 12;
544 else
545 m = 7;
546
547 II_scale_factor_calc (&fr_ps, sb_sample, scalar, fr_ps.sblimit, 0,
548 stereo);
549 II_scale_factor_calc (&fr_ps, sb_sample, scalar, fr_ps.sblimit_mc,
550 stereo, m);
551 if (info.multiling_ch > 0)
552 II_scale_factor_calc (&fr_ps, sb_sample, scalar, fr_ps.sblimit_ml, 7,
553 7 + info.multiling_ch);
554
555 pick_scale (scalar, &fr_ps, max_sc, cha_sw, aug_cha_sw, aiff);
556
557 if (fr_ps.actual_mode == MPG_MD_JOINT_STEREO) {
558 if (verbosity >= 2) {
559 printf ("JOINT!!\n");
560 fflush (stdout);
561 }
562 i = 0;
563 II_combine_LR (sb_sample, *j_sample, fr_ps.sblimit);
564 II_scale_factor_calc1 (*j_sample, j_scale, fr_ps.sblimit, i);
565 }
566
567 psycho_1 (buffer_matr, max_sc, ltmin, &fr_ps, smr, spiki, aiff);
568 if (info.multiling_ch > 0) {
569 if (odd_ml)
570 II_Psycho_One_ml (buffer_matr, max_sc, ltmin, &fr_ps, smr, spiki);
571
572 if (info.multiling_fs) {
573 pick_scale_ml_2 (&fr_ps, sb_sample, max_sc);
574 for (k = 7; k < 7 + info.multiling_ch; k++)
575 II_smr (<min[k][0], &smr[k][0], &spiki[k][0], &max_sc[k][0],
576 fr_ps.sblimit_ml, 0, fr_ps.sblimit_ml);
577 }
578 }
579
580 II_transmission_pattern (scalar, scfsi, &fr_ps);
581
582 /*in case of ext_bit_stream adb is the complete number of bits!! 06/22/94, SR */
583
584 II_main_bit_allocation (smr, ltmin, scfsi, bit_alloc, &adb,
585 &fr_ps, &aiff, sb_sample, scalar,
586 max_sc, buffer_matr, spiki,
587 *j_sample, j_scale, dyn_cr, aug_dyn_cr,
588 scfsi_dyn, scalar_dyn);
589 if (verbosity >= 2)
590 printf ("Adb2: %i\n", adb);
591
592 if (info.mc_prediction_on)
593 set_mc_pred (&fr_ps);
594
595 /* PREDISTORTION, 4/7/94, SR */
596 if (fr_ps.config == 320 && FALSE && /* still a problem 7/2/97 FdB */
597 info.matrix == 0 &&
598 info.mode != MPG_MD_JOINT_STEREO && info.dyn_cross_on == 0 &&
599 info.mc_prediction_on == 0) {
600 adb = (whole_SpF + extra_slot) * bitsPerSlot - lfe_bits;
601 predistortion (sb_sample, scalar, bit_alloc, *subband,
602 &fr_ps, smr, scfsi, &adb, scfsi_dyn);
603 predis = 1;
604 if (verbosity >= 2)
605 printf ("Adb3: %i\n", adb);
606 }
607 /*
608 */
609 /********** PREDISTORTION-END*****/
610
611 if (error_protection)
612 II_CRC_calc (&fr_ps, bit_alloc, scfsi, &crc);
613
614 lfe_alloc = 4;
615 II_CRC_calc_mc (&fr_ps, bit_alloc, lfe_alloc, scfsi, &crcmc);
616 lfe_alloc = 0;
617
618
619 encode_info (&fr_ps, &bs);
620
621 if (error_protection)
622 encode_CRC (crc, &bs);
623
624 II_encode_bit_alloc (bit_alloc, &fr_ps, &bs);
625 k = 0;
626 i = stereo;
627
628 II_encode_scale (bit_alloc, scfsi, scalar, lfe_alloc, lfe_scalar,
629 &fr_ps, &bs, &k, &i);
630
631 II_subband_quantization (scalar, sb_sample, j_scale,
632 *j_sample, bit_alloc, *subband, &fr_ps);
633
634
635 II_sample_encoding (*subband, bit_alloc, &fr_ps, &bs);
636
637 /***** Now bitstream for the surround channels **************/
638
639 encode_info_mc1 (&fr_ps, &bs);
640
641 encode_CRC (crcmc, &bs);
642
643 encode_info_mc2 (&fr_ps, &bs); /*new draft 5.7.93 SR */
644
645 if (lfe) {
646 lfe_alloc = 4;
647 II_encode_lfe_alloc (lfe_alloc, &fr_ps, &bs);
648 }
649
650 II_encode_bit_alloc_mc (bit_alloc, &fr_ps, &bs);
651 k = stereo;
652 i = stereo + stereomc;
653
654 II_encode_scale (bit_alloc, scfsi, scalar, lfe_alloc, lfe_scalar,
655 &fr_ps, &bs, &k, &i);
656
657 II_subband_quantization_mc (scalar, sb_sample, j_scale,
658 *j_sample, bit_alloc, *subband, &fr_ps);
659
660 if (lfe)
661 II_lfe_quantisation (lfe_scalar, *lfe_sample, lfe_alloc,
662 *lfe_sbband, &fr_ps);
663
664 II_sample_encoding_mc (*subband, *lfe_sbband, bit_alloc, lfe_alloc,
665 &fr_ps, &bs);
666
667 if (info.multiling_ch > 0) {
668 k = 7;
669 i = info.multiling_ch + 7;
670 II_encode_bit_alloc_ml (bit_alloc, &fr_ps, &bs);
671 II_encode_scale_ml (bit_alloc, scfsi, scalar, &fr_ps, &bs, &k, &i);
672
673 II_subband_quantization_ml (scalar, sb_sample, j_scale,
674 *j_sample, bit_alloc, *subband, &fr_ps);
675
676
677 II_sample_encoding_ml (*subband, bit_alloc, &fr_ps, &bs);
678 }
679
680 frameBits = sstell (&bs) - sentBits;
681 frameBits += info.n_ad_bytes * 8;
682 if (info.ext_bit_stream_present)
683 frameBits += 40;
684
685 if (frameBits < (whole_SpF + extra_slot) * bitsPerSlot) {
686 adb = (whole_SpF + extra_slot) * bitsPerSlot - frameBits;
687 if (verbosity >= 2)
688 printf ("Adb4: %i\n", adb);
689 adb += info.n_ad_bytes * 8;
690 if (info.ext_bit_stream_present)
691 adb += 40;
692 } else if (verbosity >= 2)
693 printf ("Adb4: %i but %li bits overflow\n",
694 adb, frameBits - (whole_SpF + extra_slot) * bitsPerSlot);
695
696 /* YBKim060695 ancillary */
697 if (ancillaryUse)
698 ancillary_encode (&fr_ps, &bs, adb);
699 else
700 for (i = 0; i < adb; i++)
701 put1bit (&bs, 0);
702 }
703 break;
704 default:
705 fprintf(stdout,"LayerII only\n");
706 exit(0);
707 }
708
709 frameBits = sstell (&bs) - sentBits;
710 if (frameBits % bitsPerSlot) /* a program failure */
711 fprintf (stderr, "Sent %li bits = %li slots plus %ld adb: %i\n",
712 frameBits, frameBits / bitsPerSlot,
713 frameBits % bitsPerSlot, adb);
714 sentBits += frameBits;
715
716 #ifdef DEBUG
717 if (verbosity >= 3) {
718 if ((info.lay == 2) &&
719 ((int) aiff_ptr.sampleRate == 48000) &&
720 (info.ext_bit_stream_present == 0) &&
721 (frameBits != 24 * bitrate[info.lay - 1][info.bitrate_index])
722 ) /* for debugging purposes WtK */
723 fprintf (stderr,
724 "\n***Error. %li bits have been written instead of %i bits.\n",
725 frameBits, 24 * bitrate[info.lay - 1][info.bitrate_index]);
726 }
727 #endif
728
729 #ifdef PRINTOUT
730 if (verbosity >= 3) {
731 printf
732 ("\nFrame %4lu channel 1 channel 2 channel 3 channel 4 channel 5\n",
733 frameNum - 1);
734
735 for (loop_subband = 0; loop_subband < SBLIMIT; loop_subband++) {
736 l = sbgrp[i];
737 printf ("subband %2d :", loop_subband);
738 for (loop_channel = 0; loop_channel < stereo + stereomc + stereoaug;
739 loop_channel++) {
740 /* make loop_alloc point to the alloc-table in fr_ps */
741 k = transmission_channel (&fr_ps, l, loop_channel);
742 if (loop_channel < stereo)
743 loop_alloc = fr_ps.alloc;
744 else
745 loop_alloc = fr_ps.alloc_mc;
746 loop_bits =
747 (*loop_alloc)[loop_subband][bit_alloc[k][loop_subband]].bits;
748 if ((k == 1) && (info.mode == MPG_MD_JOINT_STEREO)
749 && (loop_subband >= fr_ps.jsbound))
750 printf (" js ");
751 else {
752 if ((*loop_alloc)[loop_subband][bit_alloc[k][loop_subband]].steps ==
753 0)
754 printf (" - bits ");
755 else {
756 if ((*loop_alloc)[loop_subband][bit_alloc[k][loop_subband]].
757 group == 1)
758 loop_bits /= 3;
759 printf ("%5.1f bits ", loop_bits);
760 }
761 }
762 } /* for (loop_channel..) */
763 printf ("\n");
764 } /* for (loop_subband..) */
765
766 /* Now the ML part , WtK */
767 if (info.multiling_ch > 0) {
768 printf ("\nFrame %4lu ", frameNum - 1);
769 for (loop_channel = 7; loop_channel < 7 + info.multiling_ch;
770 loop_channel++)
771 printf (" MLchann %d", loop_channel);
772 printf ("\n");
773 for (loop_subband = 0; loop_subband < SBLIMIT; loop_subband++) {
774 printf ("subband %2d :", loop_subband);
775 for (loop_channel = 0; loop_channel < info.multiling_ch;
776 loop_channel++) {
777 /* make loop_alloc point to the alloc-table in fr_ps */
778 loop_alloc = fr_ps.alloc_ml;
779 loop_bits =
780 (*loop_alloc)[loop_subband][bit_alloc[loop_channel]
781 [loop_subband]].bits;
782 if ((*loop_alloc)[loop_subband]
783 [bit_alloc[loop_channel][loop_subband]].steps == 0)
784 printf (" - bits ");
785 else {
786 if ((*loop_alloc)[loop_subband]
787 [bit_alloc[loop_channel][loop_subband]].group == 1)
788 loop_bits /= 3;
789 printf ("%5.1f bits ", loop_bits);
790 }
791 } /* for (loop_channel..) */
792 printf ("\n");
793 } /* for (loop_subband..) */
794 } /*n_ml_ch>0 */
795 fflush (stdout);
796 } /* verbosity */
797 #endif
798
799 if (info.multiling_fs == 1)
800 odd_ml = 1 - odd_ml; /* ML half fs */
801 /****************************************************************/
802 /* */
803 /* END OF FRAME LOOP */
804 /* */
805 /****************************************************************/
806 } /* end of while (get_audio) - loop */
807
808
809 close_bit_stream_w (&bs);
810
811 if (info.ext_bit_stream_present == 1) {
812 open_bit_stream_r (&bs, encoded_file_name, BUFFER_SIZE);
813 if (encoded_file_name_ext[0] == NULL_CHAR)
814 strcat (strcpy (encoded_file_name_ext, encoded_file_name), DFLT_EXT_EXT);
815 if (verbosity >= 2)
816 printf (">>> file name of extension bitstream is: %s\n",
817 encoded_file_name_ext);
818 open_bit_stream_w (&bs_ext, encoded_file_name_ext, BUFFER_SIZE);
819 if (encoded_file_name_mpg[0] == NULL_CHAR)
820 strcat (strcpy (encoded_file_name_mpg, encoded_file_name), DFLT_EXT);
821 open_bit_stream_w (&bs_mpg, encoded_file_name_mpg, BUFFER_SIZE);
822
823 for (m = 0; m < frameNum; m++) {
824 i = (((double) samplesPerFrame / s_freq[info.sampling_frequency]) *
825 (double) bitrate[info.lay - 1][info.bitrate_index]) -
826 (info.n_ad_bytes * 8);
827
828 for (j = 0; j < i; j++) {
829 crc_buffer[0] = get1bit (&bs);
830 put1bit (&bs_mpg, crc_buffer[0]);
831 }
832 for (j = 0; j < info.n_ad_bytes * 8; j++)
833 put1bit (&bs_mpg, 0);
834
835 encode_info_ext1 (&fr_ps, &bs_ext);
836
837 for (i = 0; i < 14; i++)
838 crc_buffer[i] = getbits (&bs, 8);
839 crc_buffer[14] = getbits (&bs, 4);
840
841 II_CRC_calc_ext (&fr_ps, crc_buffer, &crcext);
842
843 encode_info_ext2 (&fr_ps, &bs_ext, crcext);
844
845 for (i = 0; i < 14; i++)
846 putbits (&bs_ext, crc_buffer[i], 8);
847 putbits (&bs_ext, crc_buffer[14], 4);
848
849 i = (info.ext_length * 8) - 40 - 116;
850 for (j = 0; j < i; j++) {
851 crc_buffer[0] = get1bit (&bs);
852 put1bit (&bs_ext, crc_buffer[0]);
853 }
854
855 for (j = 0; j < 5 + info.n_ad_bytes; j++)
856 crc_buffer[i] = getbits (&bs, 8);
857 }
858
859 close_bit_stream_r (&bs);
860 close_bit_stream_w (&bs_ext);
861 close_bit_stream_w (&bs_mpg);
862
863 strcpy (blubb, "rm ");
864 strcat (blubb, encoded_file_name);
865 #ifdef DEBUG
866 if (verbosity >= 3)
867 printf ("%s\n", blubb);
868 #endif
869 system (blubb);
870
871 strcpy (blubb, "mv ");
872 strcat (blubb, encoded_file_name_mpg);
873 strcat (blubb, " ");
874 strcat (blubb, encoded_file_name);
875 #ifdef DEBUG
876 if (verbosity >= 3)
877 printf ("%s\n", blubb);
878 #endif
879 system (blubb);
880 }
881
882 if (verbosity >= 2)
883 printf ("Avg slots/frame = %.3f; b/smp = %.2f; br = %.3f kbps\n",
884 (FLOAT) sentBits / (frameNum * bitsPerSlot),
885 (FLOAT) sentBits / (frameNum * samplesPerFrame),
886 (FLOAT) sentBits / (frameNum * samplesPerFrame) *
887 s_freq[info.sampling_frequency]);
888
889 if (fclose (musicin) != 0) {
890 printf ("Could not close \"%s\".\n", original_file_name);
891 exit (2);
892 }
893 #ifdef MACINTOSH
894 set_mac_file_attr (encoded_file_name, VOL_REF_NUM, CREATOR_ENCODE,
895 FILETYPE_ENCODE);
896 #endif
897
898 if (verbosity >= 2) {
899 printf ("Encoding of \"%s\" with psychoacoustic model %d is finished\n",
900 original_file_name, model);
901 if (aiff == 1)
902 printf (" It is a multichannel file !\n");
903 else
904 printf (" It is a twochannel file!\n");
905 printf ("The MPEG encoded output file name is \"%s\"\n", encoded_file_name);
906 }
907 exit(0);
908 }
909
910 /************************************************************************
911 *
912 * usage
913 *
914 * PURPOSE: Writes command line syntax to the file specified by #stderr#
915 *
916 ************************************************************************/
917
usage(void)918 static void usage (void)
919 { /* print syntax & exit */
920 fprintf (stderr,
921 "usage: %s queries for all arguments, or\n",
922 programName);
923 fprintf (stderr,
924 " %s [-l lay][-m mode][-r sfrq][-b br][-B br][-d emp][-C config]\n",
925 programName);
926 fprintf (stderr, " [-x matr][-k tca][-y dynX]\n");
927 fprintf (stderr,
928 " [-c][-o][-e][-w][-g][-v verb][-L][-a][-P][-D] inputPCM [outBS]\n");
929 fprintf (stderr, "where\n");
930 fprintf (stderr, " -l lay use layer <lay> coding (dflt %4u)\n", DFLT_LAY);
931 fprintf (stderr, " -m mode channel mode : s/d/j/m (dflt %4c)\n", DFLT_MOD);
932 fprintf (stderr, " -n mode surround mode : s/d/j/m (dflt %4c)\n", DFLT_MOD);
933 fprintf (stderr, " -r sfrq input smpl rate in kHz (dflt %4.1f)\n",
934 DFLT_SFQ);
935 fprintf (stderr, " -b br total bitrate in kbps (dflt %4u)\n", DFLT_BRT);
936 fprintf (stderr, " -B br MPEG1 bitrate in kbps (dflt %4u)\n", DFLT_BRT);
937 fprintf (stderr, " -d emp de-emphasis n/5/c (dflt %4c)\n", DFLT_EMP);
938 fprintf (stderr, " -c mark as copyright\n");
939 fprintf (stderr, " -o mark as original\n");
940 fprintf (stderr, " -e add error protection\n");
941 fprintf (stderr, " -x matr matrix (dflt 0)\n");
942 fprintf (stderr, " -k tca set channel-switching (dflt -2)\n");
943 fprintf (stderr, " -y dynX set dyn_cross mode (dflt 0)\n");
944 fprintf (stderr, " -w set phantom coding (dflt off)\n");
945 fprintf (stderr, " -L set LFE on (dflt off)\n");
946 fprintf (stderr, " -C mode set configuration (dflt 320)\n");
947 fprintf (stderr,
948 " -g set MultiLingual file (dflt off) input ML filename = inputPCM+\"%s\"\n",
949 DFLT_ML_EXT);
950 fprintf (stderr, " -P set mc prediction on (dflt off)\n");
951 fprintf (stderr, " -D set dyn_cross_LR on (dflt off)\n");
952 fprintf (stderr, " -n nr number of anc data Bytes (dflt %d)\n", DFLT_NADB);
953 fprintf (stderr, " -a set ancillary data on (dflt off)\n");
954 fprintf (stderr, " -v verb level of verbosity 0-3 (dflt %d)\n",
955 DFLT_VERB);
956 fprintf (stderr, " inputPCM input PCM sound file (standard or AIFF)\n");
957 fprintf (stderr,
958 " outBS output bit stream of encoded audio (dflt inName+%s)\n",
959 DFLT_EXT);
960 exit (1);
961 }
962
963 /************************************************************************
964 *
965 * aiff_check
966 *
967 * PURPOSE: Checks AIFF header information to make sure it is valid.
968 * Exits if not.
969 *
970 ************************************************************************/
971
aiff_check(char * file_name,IFF_AIFF * pcm_aiff_data)972 void aiff_check (char *file_name, IFF_AIFF * pcm_aiff_data)
973 /* Pointer to name of AIFF file */
974 /* Pointer to AIFF data structure */
975 {
976
977 if (strcmp (pcm_aiff_data->sampleType, IFF_ID_SSND) != 0) {
978 printf ("Sound data is not PCM in \"%s\".\n", file_name);
979 exit (1);
980 }
981
982 if (SmpFrqIndex ((long) pcm_aiff_data->sampleRate) < 0) {
983 printf ("in \"%s\".\n", file_name);
984 exit (1);
985 }
986
987 if (pcm_aiff_data->sampleSize != sizeof (short) * BITS_IN_A_BYTE) {
988 printf ("Sound data is not %d bits in \"%s\".\n",
989 sizeof (short) * BITS_IN_A_BYTE, file_name);
990 exit (1);
991 }
992
993 if (pcm_aiff_data->numChannels < 1 || pcm_aiff_data->numChannels > 8) {
994 printf ("Sound data is not 1 - 8 channel in \"%s\".\n", file_name);
995 exit (1);
996 }
997
998 if (pcm_aiff_data->blkAlgn.blockSize != 0) {
999 printf ("Block size is not %i bytes in \"%s\".\n", 0, file_name);
1000 exit (1);
1001 }
1002
1003 if (pcm_aiff_data->blkAlgn.offset != 0) {
1004 printf ("Block offset is not %i bytes in \"%s\".\n", 0, file_name);
1005 exit (1);
1006 }
1007 }
1008
1009 /************************************************************************
1010 *
1011 * parse_args
1012 *
1013 * PURPOSE: Sets encoding parameters to the specifications of the
1014 * command line. Default settings are used for parameters
1015 * not specified in the command line.
1016 *
1017 * SEMANTICS: The command line is parsed according to the following
1018 * syntax:
1019 *
1020 * -l is followed by the layer number
1021 * -m is followed by the mode of the two front channels
1022 * -r is followed by the sampling rate
1023 * -b is followed by the total bitrate, irrespective of the mode
1024 * -B is followed by the MPEG1 bitrate, irrespective of the mode
1025 * -d is followed by the emphasis flag
1026 * -c turns the copyright flag on
1027 * -o turns the original flag on
1028 * -e turns the error_protection flag on
1029 * -C is followed by the configuration code "???" (front,rear,2ndstereo) channels
1030 * -L turns the LFE flag on
1031 * -x is followed by the matrix
1032 * -k is followed by a fixed tca value, or if '-1' a random value
1033 * -y is followed by the dynamic crosstalk mode, or if '-1' a random value
1034 * -w sets phantom coding 27/07/95: '-p' already reserved for psy
1035 * -g sets MultiLingual coding
1036 * -D turns the dyn_cross_LR flag on
1037 * -P turns the 'random prediction values' flag on
1038 * -n is followed by the number of ancillary data bytes
1039 * -a turns the ancillary flag on
1040 * -v sets level of verbosity of debug and progress messages
1041 *
1042 * If the input file is in AIFF format, the sampling frequency is read
1043 * from the AIFF header.
1044 *
1045 * The input and output filenames are read into #inpath# and #outpath#.
1046 *
1047 ************************************************************************/
1048
1049 void
parse_args(int argc,char ** argv,frame_params * fr_ps,int * psy,long unsigned int * num_samples,long unsigned int * num_samples_ml,char * original_file_name,char * encoded_file_name,IFF_AIFF * pcm_aiff_data,IFF_AIFF * pcm_aiff_data_ml,int * aiff,int * byte_per_sample,int * mat_mode,int * aug_mat_mode,int * cha_sw,int * aug_cha_sw,char * encoded_file_name_ext,int * bit_rate,char * encoded_file_name_mpg,int * dyn_cr,int * dyn_cr_LR,int * aug_dyn_cr,int * ancillaryUse,int * verbosity)1050 parse_args (int argc, char **argv,
1051 frame_params * fr_ps,
1052 int *psy,
1053 long unsigned int *num_samples,
1054 long unsigned int *num_samples_ml,
1055 char *original_file_name,
1056 char *encoded_file_name,
1057 IFF_AIFF * pcm_aiff_data,
1058 IFF_AIFF * pcm_aiff_data_ml,
1059 int *aiff,
1060 int *byte_per_sample,
1061 int *mat_mode,
1062 int *aug_mat_mode,
1063 int *cha_sw,
1064 int *aug_cha_sw,
1065 char *encoded_file_name_ext,
1066 int *bit_rate,
1067 char *encoded_file_name_mpg,
1068 int *dyn_cr,
1069 int *dyn_cr_LR, int *aug_dyn_cr, int *ancillaryUse, int *verbosity)
1070 {
1071 FLOAT srate;
1072 int brate = DFLT_BRT; /* MPEG1 bitrate */
1073 layer *info = fr_ps->header;
1074 int err = 0, bool, i = 0;
1075 int ml_present = 0; /* MultiLingual coding */
1076
1077 fr_ps->mnr_min = 0.0;
1078 *aiff = 0;
1079 *bit_rate = DFLT_BRT; /* total bitrate */
1080 fr_ps->phantom_c = 0;
1081 *verbosity = DFLT_VERB;
1082
1083 /* preset defaults */
1084 original_file_name[0] = NULL_CHAR;
1085 encoded_file_name[0] = NULL_CHAR;
1086 info->lay = DFLT_LAY;
1087 switch (DFLT_MOD) {
1088 case 'r':
1089 info->mode = MPG_MD_STEREO;
1090 info->mode_ext = 0;
1091 break;
1092 case 'd':
1093 info->mode = MPG_MD_DUAL_CHANNEL;
1094 info->mode_ext = 0;
1095 break;
1096 case 'j':
1097 info->mode = MPG_MD_JOINT_STEREO;
1098 break;
1099 case 'm':
1100 info->mode = MPG_MD_MONO;
1101 info->mode_ext = 0;
1102 break;
1103 default:
1104 fprintf (stderr, "%s: Bad mode dflt %c\n", programName, DFLT_MOD);
1105 abort ();
1106 }
1107 *psy = DFLT_PSY;
1108 if ((info->sampling_frequency = SmpFrqIndex ((long) (1000 * DFLT_SFQ))) < 0) {
1109 fprintf (stderr, "%s: bad sfrq default %.2f\n", programName, DFLT_SFQ);
1110 abort ();
1111 }
1112 info->bitrate_index = DFLT_BRT;
1113 if ((info->bitrate_index = BitrateIndex (info->lay, DFLT_BRT)) < 0) {
1114 fprintf (stderr, "%s: bad default bitrate %u\n", programName, DFLT_BRT);
1115 abort ();
1116 }
1117 switch (DFLT_EMP) {
1118 case 'n':
1119 info->emphasis = 0;
1120 break;
1121 case '5':
1122 info->emphasis = 1;
1123 break;
1124 case 'c':
1125 info->emphasis = 3;
1126 break;
1127 default:
1128 fprintf (stderr, "%s: Bad emph dflt %c\n", programName, DFLT_EMP);
1129 abort ();
1130 }
1131 info->copyright = 0;
1132 info->original = 0;
1133 info->error_protection = FALSE;
1134
1135 /* process args */
1136 while (++i < argc && err == 0) {
1137 char c, *token, *arg, *nextArg;
1138 int argUsed;
1139
1140 token = argv[i];
1141 if (*token++ == '-') {
1142 if (i + 1 < argc)
1143 nextArg = argv[i + 1];
1144 else
1145 nextArg = "";
1146
1147 argUsed = 0;
1148 while ((c = *token++)) {
1149 if (*token /* NumericQ(token) */ )
1150 arg = token;
1151 else
1152 arg = nextArg;
1153
1154 switch (c) {
1155 case 'l':
1156 info->lay = atoi (arg);
1157 argUsed = 1;
1158 if (info->lay < 1 || info->lay > 2) {
1159 fprintf (stderr, "%s: -l layer must be 1 or 2, not %s\n",
1160 programName, arg);
1161 err = 1;
1162 }
1163 break;
1164
1165 case 'm':
1166 argUsed = 1;
1167 if (*arg == 's') {
1168 info->mode = MPG_MD_STEREO;
1169 info->mode_ext = 0;
1170 } else if (*arg == 'd') {
1171 info->mode = MPG_MD_DUAL_CHANNEL;
1172 info->mode_ext = 0;
1173 } else if (*arg == 'j') {
1174 info->mode = MPG_MD_JOINT_STEREO;
1175 } else if (*arg == 'm') {
1176 info->mode = MPG_MD_MONO;
1177 info->mode_ext = 0;
1178 } else if (*arg == 'n') {
1179 info->mode = MPG_MD_NONE;
1180 info->mode_ext = 0;
1181 } else {
1182 fprintf (stderr, "%s: -m mode must be s/d/j/m not %s\n",
1183 programName, arg);
1184 err = 1;
1185 }
1186 break;
1187
1188 case 'r':
1189 srate = atof (arg);
1190 argUsed = 1;
1191 if ((info->sampling_frequency =
1192 SmpFrqIndex ((long) (1000 * srate))) < 0)
1193 err = 1;
1194 break;
1195
1196 case 'b':
1197 *bit_rate = atoi (arg);
1198 argUsed = 1;
1199 if (*bit_rate > brate) {
1200 info->ext_bit_stream_present = 1;
1201 info->ext_length =
1202 ((*bit_rate - brate) * 144) / s_freq[info->sampling_frequency];
1203 } else {
1204 info->ext_bit_stream_present = 0;
1205 brate = *bit_rate;
1206 }
1207 break;
1208
1209 case 'B':
1210 brate = atoi (arg);
1211 argUsed = 1;
1212 if (*bit_rate > brate) {
1213 info->ext_bit_stream_present = 1;
1214 info->ext_length =
1215 ((*bit_rate - brate) * 144) / s_freq[info->sampling_frequency];
1216 } else
1217 info->ext_bit_stream_present = 0;
1218 break;
1219
1220 case 'C':
1221 fr_ps->config = atoi (arg);
1222 argUsed = 1;
1223 break;
1224 case 'h':
1225 usage();
1226 break;
1227 case 'd':
1228 argUsed = 1;
1229 if (*arg == 'n')
1230 info->emphasis = 0;
1231 else if (*arg == '5')
1232 info->emphasis = 1;
1233 else if (*arg == 'c')
1234 info->emphasis = 3;
1235 else {
1236 fprintf (stderr, "%s: -d emp must be n/5/c not %s\n",
1237 programName, arg);
1238 err = 1;
1239 }
1240 break;
1241
1242 case 'c':
1243 info->copyright = 1;
1244 break;
1245 case 'o':
1246 info->original = 1;
1247 break;
1248 case 'e':
1249 info->error_protection = TRUE;
1250 break;
1251
1252 case 'x':
1253 info->matrix = *mat_mode = atoi (arg);
1254 if (info->matrix == 3)
1255 *cha_sw = 0;
1256 argUsed = 1;
1257 break;
1258 case 'k':
1259 *cha_sw = atoi (arg);
1260 argUsed = 1;
1261 break;
1262 case 'y':
1263 *dyn_cr = atoi (arg);
1264 argUsed = 1;
1265 break;
1266 case 'D':
1267 *dyn_cr_LR = 1;
1268 break;
1269 case 'L':
1270 info->lfe = 1;
1271 break;
1272 case 'w':
1273 fr_ps->phantom_c = 1;
1274 break;
1275 case 'P':
1276 info->mc_prediction_on = 1;
1277 break;
1278 case 'g':
1279 ml_present = 1;
1280 break;
1281 case 'n':
1282 info->n_ad_bytes = atoi (arg);
1283 argUsed = 1;
1284 break;
1285 case 'a':
1286 *ancillaryUse = 1;
1287 break;
1288 case 'v':
1289 *verbosity = atoi (arg);
1290 argUsed = 1;
1291 break;
1292
1293 default:
1294 fprintf (stderr, "%s: unrec option %c\n", programName, c);
1295 err = 1;
1296 break;
1297 }
1298
1299
1300 if (argUsed) {
1301 if (arg == token)
1302 token = ""; /* no more from token */
1303 else
1304 i++; /* skip arg we used */
1305 arg = "";
1306 argUsed = 0;
1307 }
1308 }
1309 } else {
1310 if (original_file_name[0] == NULL_CHAR)
1311 strcpy (original_file_name, argv[i]);
1312 else if (encoded_file_name[0] == NULL_CHAR)
1313 strcpy (encoded_file_name, argv[i]);
1314 else {
1315 fprintf (stderr, "%s: excess arg %s\n", programName, argv[i]);
1316 err = 1;
1317 }
1318 }
1319 }
1320
1321 if ((info->bitrate_index = BitrateIndex (info->lay, brate)) < 0)
1322 err = 1;
1323 if (err || original_file_name[0] == NULL_CHAR)
1324 usage (); /* never returns */
1325
1326 if (encoded_file_name[0] == NULL_CHAR) {
1327 strcat (strcpy (encoded_file_name_ext, original_file_name), DFLT_EXT_EXT);
1328 strcat (strcpy (encoded_file_name, original_file_name), DFLT_EXT);
1329 strcat (strcpy (encoded_file_name_mpg, encoded_file_name), DFLT_EXT);
1330 } else {
1331 strcat (strcpy (encoded_file_name_ext, encoded_file_name), DFLT_EXT_EXT);
1332 strcat (encoded_file_name, DFLT_EXT);
1333 strcat (strcpy (encoded_file_name_mpg, encoded_file_name), DFLT_EXT);
1334 }
1335
1336 if ((musicin = fopen (original_file_name, "rb")) == NULL) {
1337 printf ("Could not find \"%s\".\n", original_file_name);
1338 exit (0);
1339 }
1340
1341 open_bit_stream_w (&bs, encoded_file_name, BUFFER_SIZE);
1342
1343 if (aiff_read_headers (musicin, pcm_aiff_data, byte_per_sample) == 0) {
1344 int wrong_nr_channels = 0;
1345
1346 *aiff = 1;
1347 if (*verbosity >= 2)
1348 printf (">>> Using Audio IFF sound file headers\n");
1349
1350 aiff_check (original_file_name, pcm_aiff_data);
1351
1352 if (fr_ps->config) {
1353 /* check if config and number of channels is consistent */
1354 switch (fr_ps->config) {
1355 case 100:
1356 wrong_nr_channels = pcm_aiff_data->numChannels != 1;
1357 info->mode = MPG_MD_MONO;
1358 info->mode_ext = 0;
1359 break;
1360 case 200:
1361 wrong_nr_channels = pcm_aiff_data->numChannels - info->lfe != 2;
1362 fr_ps->lfe_pos = 2;
1363 break;
1364 case 102:
1365 wrong_nr_channels = pcm_aiff_data->numChannels - info->lfe != 3;
1366 info->mode = MPG_MD_MONO;
1367 info->mode_ext = 0;
1368 info->surround = 3;
1369 fr_ps->stereomc = 2;
1370 fr_ps->lfe_pos = 1;
1371 break;
1372 case 202:
1373 wrong_nr_channels = pcm_aiff_data->numChannels - info->lfe != 4;
1374 info->surround = 3;
1375 fr_ps->stereomc = 2;
1376 fr_ps->lfe_pos = 2;
1377 break;
1378 case 210:
1379 wrong_nr_channels = pcm_aiff_data->numChannels - info->lfe != 3;
1380 info->surround = 1;
1381 fr_ps->stereomc = 1;
1382 fr_ps->lfe_pos = 2;
1383 break;
1384 case 220:
1385 wrong_nr_channels = pcm_aiff_data->numChannels - info->lfe != 4;
1386 info->surround = 2;
1387 fr_ps->stereomc = 2;
1388 fr_ps->lfe_pos = 2;
1389 break;
1390 case 302:
1391 wrong_nr_channels = pcm_aiff_data->numChannels - info->lfe != 5;
1392 info->center = 1;
1393 info->surround = 3;
1394 fr_ps->stereomc = 3;
1395 break;
1396 case 310:
1397 wrong_nr_channels = pcm_aiff_data->numChannels - info->lfe != 4;
1398 info->center = 1;
1399 info->surround = 1;
1400 fr_ps->stereomc = 2;
1401 fr_ps->lfe_pos = 3;
1402 break;
1403 case 320:
1404 wrong_nr_channels = pcm_aiff_data->numChannels - info->lfe != 5;
1405 info->center = 1;
1406 info->surround = 2;
1407 fr_ps->stereomc = 3;
1408 fr_ps->lfe_pos = 3;
1409 break;
1410 default:
1411 printf ("Configuration: %d not supported\n", fr_ps->config);
1412 exit (1);
1413 }
1414 } else {
1415 if (pcm_aiff_data->numChannels == 1) {
1416 fr_ps->config = 100;
1417 info->mode = MPG_MD_MONO;
1418 info->mode_ext = 0;
1419 } else {
1420 switch (pcm_aiff_data->numChannels - info->lfe) {
1421 case 2:
1422 fr_ps->config = 200;
1423 fr_ps->lfe_pos = 2;
1424 break;
1425 case 3:
1426 fr_ps->config = 210;
1427 info->surround = 1;
1428 fr_ps->stereomc = 1;
1429 fr_ps->lfe_pos = 2;
1430 break;
1431 case 4:
1432 fr_ps->config = 310;
1433 info->center = 1;
1434 info->surround = 1;
1435 fr_ps->stereomc = 2;
1436 fr_ps->lfe_pos = 3;
1437 break;
1438 case 5:
1439 fr_ps->config = 320;
1440 info->center = 1;
1441 info->surround = 2;
1442 fr_ps->stereomc = 3;
1443 fr_ps->lfe_pos = 3;
1444 break;
1445 default:
1446 wrong_nr_channels = 1;
1447 }
1448 }
1449 }
1450
1451 if (wrong_nr_channels) {
1452 printf
1453 ("Configuration: %d lfe: %d do not match with audio channels: %d\n",
1454 fr_ps->config, info->lfe, pcm_aiff_data->numChannels);
1455 exit (1);
1456 }
1457
1458 switch (fr_ps->config) {
1459 case 100:
1460 printf (">>> Using 1/0 configuration.\n");
1461 break;
1462 case 200:
1463 printf (">>> Using 2/0 configuration.\n");
1464 break;
1465 case 102:
1466 printf (">>> Using 1/0 + 2/0 configuration.\n");
1467 break;
1468 case 202:
1469 printf (">>> Using 2/0 + 2/0 configuration.\n");
1470 break;
1471 case 210:
1472 printf (">>> Using 2/1 configuration.\n");
1473 break;
1474 case 220:
1475 printf (">>> Using 2/2 configuration.\n");
1476 break;
1477 case 302:
1478 printf (">>> Using 3/0 + 2/0 configuration.\n");
1479 break;
1480 case 310:
1481 printf (">>> Using 3/1 configuration.\n");
1482 break;
1483 case 320:
1484 if (fr_ps->stereoaug == 0)
1485 printf (">>> Using 3/2 configuration.\n");
1486 break;
1487 }
1488
1489 info->sampling_frequency = SmpFrqIndex ((long) pcm_aiff_data->sampleRate);
1490 if (*verbosity >= 2)
1491 printf (">>> %.f Hz sampling frequency selected\n",
1492 pcm_aiff_data->sampleRate);
1493
1494 /* Determine number of samples in sound file */
1495 #ifndef MS_DOS
1496 *num_samples = pcm_aiff_data->numChannels * pcm_aiff_data->numSampleFrames;
1497 #else
1498 *num_samples = (long) (pcm_aiff_data->numChannels) *
1499 (long) (pcm_aiff_data->numSampleFrames);
1500 #endif
1501
1502 if (*cha_sw > 0) {
1503 if (fr_ps->config == 320)
1504 bool = *cha_sw > 7;
1505 else if (fr_ps->config == 310 && info->matrix == 2)
1506 bool = *cha_sw > 5;
1507 else if (fr_ps->config == 310)
1508 bool = *cha_sw > 4;
1509 else if (fr_ps->config == 220)
1510 bool = *cha_sw > 3;
1511 else if (fr_ps->config == 300 || fr_ps->config == 302
1512 || fr_ps->config == 210)
1513 bool = *cha_sw > 2;
1514 else
1515 bool = 1;
1516
1517 if (bool) {
1518 printf
1519 ("!!! Channel-switching mode %d cannot be used together with Configuration: %d !!!\n",
1520 *cha_sw, fr_ps->config);
1521 exit (1);
1522 }
1523 }
1524 if (*dyn_cr > 0) {
1525 if (fr_ps->config == 320)
1526 bool = *dyn_cr > 14;
1527 else if (fr_ps->config == 310 || fr_ps->config == 220)
1528 bool = *dyn_cr > 4;
1529 else if (fr_ps->config == 300 || fr_ps->config == 302
1530 || fr_ps->config == 210)
1531 bool = *dyn_cr > 1;
1532 else
1533 bool = 1;
1534
1535 if (bool) {
1536 printf
1537 ("!!! Dynamic Crosstalk mode %d cannot be used for Configuration: %d !!!\n",
1538 *dyn_cr, fr_ps->config);
1539 exit (1);
1540 }
1541
1542 if (!bool && fr_ps->phantom_c) {
1543 if (fr_ps->config == 320)
1544 bool = ((*dyn_cr == 9) || (*dyn_cr == 10) || (*dyn_cr == 11)
1545 || (*dyn_cr == 12) || (*dyn_cr == 14));
1546 else if (fr_ps->config == 310)
1547 bool = (*dyn_cr == 4);
1548
1549 if (bool) {
1550 printf
1551 ("!!! Dynamic Crosstalk mode %d cannot be used together with Phantom Coding for Configuration: %d!!!\n",
1552 *dyn_cr, fr_ps->config);
1553 exit (1);
1554 }
1555 }
1556 }
1557 } else {
1558 /* Not using Audio IFF sound file headers. */
1559
1560 if (*verbosity >= 2)
1561 printf (" NO MULTICHANNEL CODING!!\n");
1562 ml_present = 0;
1563
1564 if (fseek (musicin, 0, SEEK_SET) != 0) {
1565 printf ("Could not seek to PCM sound data in \"%s\".\n",
1566 original_file_name);
1567 exit (0);
1568 }
1569
1570 /* Declare sound file to have "infinite" number of samples. */
1571 *num_samples = MAX_U_32_NUM;
1572 }
1573
1574 /***************************************************************************/
1575 /* 7/8/95 Multi lingual extension WtK */
1576
1577 if (ml_present) {
1578 strcat (strcpy (multilingual_file_name, original_file_name), DFLT_ML_EXT);
1579 if ((musicin_ml = fopen (multilingual_file_name, "rb")) == NULL) {
1580 printf ("Could not find \"%s\".\nNo MultiLingual Coding\n",
1581 multilingual_file_name);
1582 info->multiling_ch = 0;
1583 } else if (aiff_read_headers (musicin_ml, pcm_aiff_data_ml, byte_per_sample)
1584 == 0) {
1585 if (*verbosity >= 2)
1586 printf (">>> Using Audio IFF multilingual file headers\n");
1587 info->multiling_ch = pcm_aiff_data_ml->numChannels;
1588 if (*verbosity >= 2)
1589 printf (">>> Using %d multilingual channels\n", info->multiling_ch);
1590 *num_samples_ml = pcm_aiff_data_ml->numChannels *
1591 pcm_aiff_data_ml->numSampleFrames;
1592 } else { /* Not using Audio IFF sound file headers. */
1593
1594 printf
1595 ("***WARNING: Could not read ML AIFF header - No MultiLingual coding!!\n");
1596 info->multiling_ch = 0;
1597 }
1598
1599 if (info->multiling_ch > 0) {
1600 switch (SmpFrqIndex ((long) pcm_aiff_data_ml->sampleRate) -
1601 info->sampling_frequency) {
1602 case 16:
1603 if (*verbosity >= 2)
1604 printf ("MultiLingual is LSF coded.\n");
1605 info->multiling_fs = 1;
1606 break;
1607 case 0:
1608 if (*verbosity >= 2)
1609 printf ("MultiLingual sample rate equals that of Audio.\n");
1610 info->multiling_fs = 0;
1611 break;
1612 default:
1613 printf
1614 ("***WARNING: MultiLingual sample rate unequals (half) mc sample rate.\n");
1615 printf (" No MultiLingual coding!!!\n");
1616 info->multiling_ch = 0;
1617 break;
1618 } /*switch */
1619 } /*if (ml_ch>0) */
1620 } else {
1621 info->multiling_ch = 0;
1622 } /* if (ml_present) */
1623
1624 /* 7/8/95 Multi lingual extension WtK */
1625 /***************************************************************************/
1626 }
1627
1628 /************************************************************************
1629 *
1630 * print_config
1631 *
1632 * PURPOSE: Prints the encoding parameters used
1633 *
1634 ************************************************************************/
1635
1636 void
print_config(frame_params * fr_ps,int * psy,long unsigned int * num_samples,char * inPath,char * outPath,int * aiff)1637 print_config (frame_params * fr_ps, int *psy, long unsigned int *num_samples,
1638 char *inPath, char *outPath, int *aiff)
1639 {
1640 layer *info = fr_ps->header;
1641
1642 printf ("Encoding configuration:\n");
1643 if (*aiff == 1)
1644 printf ("Layer=%s mode=%s extn=%d psy model=%d\n",
1645 layer_names[info->lay - 1], mode_names[info->mode],
1646 info->mode_ext, *psy);
1647 else
1648 printf ("Layer=%s mode=%s extn=%d psy model=%d\n",
1649 layer_names[info->lay - 1], mode_names[info->mode],
1650 info->mode_ext, *psy);
1651
1652 if (info->bitrate_index != 0) {
1653 if (bitrate[info->lay - 1][info->bitrate_index] == 1000)
1654 printf ("samp frq=%.1f kHz total bitrate=dynamic bitrate\n",
1655 s_freq[info->sampling_frequency]);
1656 else
1657 printf ("samp frq=%.1f kHz mpeg1 bitrate=%d kbps\n",
1658 s_freq[info->sampling_frequency],
1659 bitrate[info->lay - 1][info->bitrate_index]);
1660 if (info->ext_bit_stream_present)
1661 printf ("ext bitrate=%d kbps\n",
1662 (int) (info->ext_length * s_freq[info->sampling_frequency] /
1663 144));
1664 }
1665
1666 if (info->multiling_ch > 0) {
1667 printf ("%d multilingual channels ", info->multiling_ch);
1668 if (info->multiling_fs == 1)
1669 printf ("LSF coded.\n");
1670 else
1671 printf ("full sample rate coded.\n");
1672 } else
1673 printf ("No multilingual coding.\n");
1674
1675 printf ("de-emph=%d c/right=%d orig=%d errprot=%d\n",
1676 info->emphasis, info->copyright, info->original,
1677 info->error_protection);
1678 printf
1679 ("matrix=%d center=%d surround=%d stereo=%d stereomc=%d stereoaug=%d\n",
1680 info->matrix, info->center, info->surround, fr_ps->stereo, fr_ps->stereomc,
1681 fr_ps->stereoaug);
1682 printf ("input file: '%s' output file: '%s'\n", inPath, outPath);
1683 fflush (stdout);
1684 }
1685