1 #include "common.h"
2 #include "decoder.h"
3 #include "audio_write.h"
4 #include "decode.h"
5 #include "decode_mc.h"
6 #include "decode_ml.h"
7 #include "decode_aug.h"
8 #include "crc.h"
9 #include "subband.h"
10 #include "musicout.h"
11
12 /********************************************************************
13 *
14 * This part contains the MPEG I / II decoder for Layer II.
15 *
16 *********************************************************************/
17
18 /* Global variable definitions for "musicout.c" */
19
20 char *programName;
21 int Frame_Bits, Bitrate;
22 double S_freq;
23
24 /* Implementations */
25
main(int argc,char ** argv)26 int main (int argc, char **argv)
27 { /* R.S. 7 channels for ML */
28 typedef long PCM[7][3][SBLIMIT];
29 PCM *pcm_sample;
30 PCM *pcm_sample_ml; /* 10/03/1995 JMZ Multilingual */
31 typedef unsigned int SAM[7][3][SBLIMIT];
32 SAM *sample;
33 SAM *sample_ml; /* 10/03/1995 JMZ Multilingual */
34 typedef double FRA[7][SBLIMIT][3][12]; /* 7.10.93 R.S. mem - alloc for DOS */
35 FRA *fraction;
36 FRA *fraction_ml; /*JMZ 09/03/1995 Multilingual */
37 typedef double FRA_HELP[12][7][3][SBLIMIT]; /* 10/03/1995 JMZ Multilingual */
38 FRA_HELP *fraction_help;
39 FRA_HELP *fraction_help_ml;
40 typedef double FRA_BUF[2][8][36 + PREDDEL];
41 FRA_BUF *pred_buf;
42 typedef double VE[7][HAN_SIZE];
43 VE *w;
44
45 frame_params fr_ps;
46 layer info;
47 Bit_stream bs_mpg, bs_ext, bs_mc;
48 FILE *musicout, *lfe=NULL;
49 FILE *musicout_ml=NULL; /* 10/03/1995 JMZ Multilingual */
50 unsigned long sample_frames;
51 unsigned long sample_frames_ml;
52 int i, jj, j, k, ii, stereo=1, done = FALSE, clip=0, sync, f;
53 int error_protection, crc_check = 1;
54 int crc_error_count, total_error_count;
55 int crc_error_count_mc, total_error_count_mc;
56 #ifdef Augmentation_7ch
57 int crc_error_count_aug, total_error_count_aug;
58 #endif
59 unsigned int old_crc, new_crc;
60 unsigned int I_bit_alloc[3][7][SBLIMIT];
61 unsigned int bit_alloc[7][SBLIMIT], scfsi[7][SBLIMIT],
62 scale_index[7][3][SBLIMIT];
63 unsigned int bit_alloc_ml[7][SBLIMIT], scfsi_ml[7][SBLIMIT], scale_index_ml[7][3][SBLIMIT]; /* 09/03/1995 JMZ Multilingual */
64 unsigned long bitsPerSlot, samplesPerFrame=8;
65 IFF_AIFF pcm_aiff_data;
66 char encoded_file_name[MAX_NAME_SIZE];
67 char encoded_file_name1[MAX_NAME_SIZE]; /* 8/11/92.sr */
68 char decoded_file_name[MAX_NAME_SIZE];
69 char decoded_file_name_ml[MAX_NAME_SIZE]; /* 10/03/1995 JMZ Multilingual */
70 char ext_bitstream_name[MAX_NAME_SIZE];
71 //MFC char frame_name[MAX_NAME_SIZE];
72 char lfe_file_name[MAX_NAME_SIZE];
73 char t[50];
74 int need_aiff;
75 int m, print_out = 0;
76 int ch_start, lfe_init = 1;
77 int tca_log = 0, dynx_log = 0, bits_log = 0, rate_log = 0, scfsi_log =
78 0, any_log = 0;
79 short int lfe_tmp[12];
80 unsigned int sym_sample_ml[12][7][3][SBLIMIT]; /* 18/03/1996 FDB Multilingual */
81 int layer_I_frames=3;
82 int mpeg = 2; /* R.S. mpeg default for mc */
83 int channels = 2, mc_channel = 0;
84 unsigned long frameNum = 0L;
85 unsigned long frameMod = 1L;
86 unsigned long frameBits;
87 /*****************************************************************************/
88 int hi, hu, ho;
89 FILE *fp1=NULL;
90 FILE *fp2=NULL;
91 FILE *fp3=NULL;
92 FILE *fp4=NULL;
93 FILE *fp5=NULL;
94 FILE *fp6=NULL;
95 /******************************************************************************/
96
97 /* Most large variables are declared dynamically to ensure
98 compatibility with smaller machines */
99
100 pcm_sample = (PCM *) mem_alloc ((long) sizeof (PCM), "PCM Samp");
101 pcm_sample_ml = (PCM *) mem_alloc ((long) sizeof (PCM), "PCM Samp");
102 sample = (SAM *) mem_alloc ((long) sizeof (SAM), "Sample");
103 sample_ml = (SAM *) mem_alloc ((long) sizeof (SAM), "Sample");
104 fraction = (FRA *) mem_alloc ((long) sizeof (FRA), "fraction"); /* R.S. */
105 fraction_ml = (FRA *) mem_alloc ((long) sizeof (FRA), "fraction"); /* R.S. */
106 fraction_help = (FRA_HELP *) mem_alloc ((long) sizeof (FRA_HELP), "fraction");
107 fraction_help_ml =
108 (FRA_HELP *) mem_alloc ((long) sizeof (FRA_HELP), "fraction");
109 pred_buf = (FRA_BUF *) mem_alloc ((long) sizeof (FRA_BUF), "pred_buf");
110
111 w = (VE *) mem_alloc ((long) sizeof (VE), "w");
112
113 bs_mpg.header_size = 0;
114 bs_mpg.bits = (unsigned char *) mem_alloc ((long) 13824, "MPG Bits");
115 bs_ext.header_size = 0;
116 bs_ext.bits = (unsigned char *) mem_alloc ((long) 16376, "EXT Bits");
117 bs_mc.bits = (unsigned char *) mem_alloc ((long) 30208, "MC Bits");
118
119 fr_ps.header = &info;
120 fr_ps.bs_mpg = &bs_mpg;
121 fr_ps.bs_ext = &bs_ext;
122 fr_ps.bs_mc = &bs_mc;
123 fr_ps.tab_num = -1; /* no table loaded */
124 fr_ps.tab_num_mc = -1; /* no table loaded */
125 fr_ps.tab_num_ml = -1;
126 fr_ps.alloc = NULL;
127 fr_ps.alloc_mc = NULL;
128 fr_ps.alloc_ml = NULL;
129
130 info.mode_ext = 0;
131 info.version = MPEG_AUDIO_ID;
132 info.bitrate_index = 0;
133 info.lfe = 0; /* no low frequency effect channel present! */
134
135 info.no_of_multi_lingual_ch = 0;
136 info.multi_lingual_fs = 0;
137 info.multi_lingual_layer = 0;
138
139 info.ext_bit_stream_present = 0;
140 info.ext_length = 0;
141 info.n_ad_bytes = 0; /* 7.12.94 R.S. */
142
143 info.lfe = 0;
144
145 for (i = 0; i < HAN_SIZE; i++)
146 for (j = 0; j < 5; j++)
147 (*w)[j][i] = 0.0;
148
149 program_information ();
150 programName = argv[0];
151 if (argc == 1) { /* no command line args -> interact */
152 do {
153 printf ("Enter encoded file name <required>: ");
154 fgets(encoded_file_name, sizeof(encoded_file_name), stdin);
155 f = strlen (encoded_file_name) - 4; /*cut off extension.8/11/92.sr */
156 if (encoded_file_name[0] == NULL_CHAR)
157 printf ("Encoded file name is required. \n");
158 } while (encoded_file_name[0] == NULL_CHAR);
159
160 printf (">>> Encoded file name is: %s \n", encoded_file_name);
161 strcpy (encoded_file_name1, encoded_file_name); /*8/11/92.sr */
162 strcpy (&encoded_file_name1[f], DFLT_OPEXT_DEC); /*.dec-extension.8/11/92.sr */
163 printf ("Enter MPEG decoded file name <%s>: ", encoded_file_name1);
164 fgets(decoded_file_name, sizeof(decoded_file_name), stdin);
165 if (decoded_file_name[0] == NULL_CHAR)
166 strcpy (decoded_file_name, encoded_file_name1);
167
168 /* JMZ 10/03/1995 Multilingual */
169
170 printf (">>> MPEG decoded file name is: %s \n", decoded_file_name);
171
172 /* encoded_file_name1 only for help */
173 strcpy (encoded_file_name1, encoded_file_name);
174 strcpy (&encoded_file_name1[f], DFLT_IPEXT_EXT); /* .ext */
175 printf (">>> Enter MPEG 2 decoded extension filename <%s>: ",
176 encoded_file_name1);
177 fgets(ext_bitstream_name, sizeof(ext_bitstream_name), stdin);
178 if (ext_bitstream_name[0] == NULL_CHAR)
179 strcpy (ext_bitstream_name, encoded_file_name1);
180 printf ("Extension bitstream <%s> will be decoded \n", ext_bitstream_name);
181 /* 6/21/95 Ralf Schwalbe: stand a chance to decode MPEG1 compatible part */
182 if ((open_bit_stream_r (&bs_ext, ext_bitstream_name, BUFFER_SIZE)) == 0) {
183 printf ("No extension bitstream <%s> present \n", ext_bitstream_name);
184 printf ("Do you want to decode an MPEG 2 bitstream ? (<y>/n) : ");
185 fgets(t, sizeof(t), stdin);
186 if (*t == 'N' || *t == 'n') {
187 mpeg = 1;
188 printf ("An MPEG-1 Layer II bitstream will be decoded \n");
189 } else {
190 mpeg = 2;
191 printf ("An MPEG-2 Layer II bitstream will be decoded \n");
192 }
193 }
194 printf ("Do you wish to write an AIFF compatible sound file ? (<y>/n) : ");
195 fgets(t, sizeof(t), stdin);
196 if (*t == 'N' || *t == 'n')
197 need_aiff = FALSE;
198 else
199 need_aiff = TRUE;
200 if (need_aiff)
201 printf (">>> An AIFF compatible sound file will be written\n");
202 else
203 printf (">>> A non-headered PCM sound file will be written\n");
204
205 printf ("Do you want to print out all decoding information ? (y/<n>) :");
206 fgets(t, sizeof(t), stdin);
207 if (*t == 'y' || *t == 'Y')
208 print_out = 1;
209 else
210 print_out = 0;
211
212 printf ("Do you wish to exit (last chance before decoding) ? (y/<n>) : ");
213 fgets(t, sizeof(t), stdin);
214 if (*t == 'y' || *t == 'Y')
215 exit (0);
216 }
217
218 else {
219 /************** interpret CL Args *****************/
220 /* default: -write an AIFF file
221 -print encoded bitstream information */
222 int i = 0, err = 0;
223
224 /* default values */
225 need_aiff = TRUE;
226 print_out = 0; /* 10/31/95 Ralf Schwalbe: set print_out off in CL Args */
227 mpeg = 2;
228 encoded_file_name[0] = NULL_CHAR;
229 decoded_file_name[0] = NULL_CHAR;
230 ext_bitstream_name[0] = NULL_CHAR;
231
232 while (++i < argc && err == 0) {
233 char c, *token, *arg, *nextArg;
234 int argUsed;
235
236 token = argv[i];
237 if (*token++ == '-') {
238 if (i + 1 < argc)
239 nextArg = argv[i + 1];
240 else
241 nextArg = "";
242 argUsed = 0;
243 while ((c = *token++) != 0) {
244 if (*token /* NumericQ(token) */ )
245 arg = token;
246 else
247 arg = nextArg;
248 switch (c) {
249 case 'a':
250 need_aiff = FALSE;
251 break;
252
253 case 'b':
254 bits_log = 1;
255 break;
256
257 case 'd':
258 dynx_log = 1;
259 break;
260
261 case 'h':
262 bs_mpg.header_size = 2304;
263 bs_ext.header_size = 2304;
264 break;
265
266 case 'i':
267 print_out = 1;
268 break;
269
270 case 'm':
271 mpeg = 1;
272 break;
273 #ifdef Augmentation_7ch
274 case 'n':
275 mpeg = 3;
276 break;
277 #endif
278 case 'r':
279 rate_log = 1;
280 break;
281
282 case 's':
283 scfsi_log = 1;
284 break;
285
286 case 't':
287 tca_log = 1;
288 break;
289
290 default:
291 fprintf (stderr, "%s: unrecognized option %c\n", programName, c);
292 err = 1;
293 break;
294 }
295 if (argUsed) {
296 if (arg == token)
297 token = ""; /* no more from token */
298 else
299 ++i; /* skip arg we used */
300 arg = "";
301 argUsed = 0;
302 }
303 }
304 } else {
305 if (encoded_file_name[0] == NULL_CHAR) {
306 strcpy (encoded_file_name, argv[i]);
307 f = strlen (encoded_file_name);
308 strcpy (&encoded_file_name[f], DFLT_IPEXT_MPG);
309 strcpy (ext_bitstream_name, encoded_file_name);
310 strcpy (&ext_bitstream_name[f], DFLT_IPEXT_EXT);
311 } else if (decoded_file_name[0] == NULL_CHAR) {
312 strcpy (decoded_file_name, argv[i]);
313 f = strlen (decoded_file_name);
314 strcpy (&decoded_file_name[f], DFLT_OPEXT_DEC);
315 } else {
316 fprintf (stderr, "%s: excess arg %s\n", programName, argv[i]);
317 err = 1;
318 }
319 }
320 } /* end while argc */
321
322 if (bits_log || dynx_log || rate_log || scfsi_log || tca_log)
323 any_log = 1;
324
325
326 if (err || encoded_file_name[0] == NULL_CHAR)
327 usage (); /* never returns */
328
329 if ((open_bit_stream_r (&bs_ext, ext_bitstream_name, BUFFER_SIZE)) == 0) {
330 printf ("No extension bitstream <%s> present \n", ext_bitstream_name);
331 ext_bitstream_name[0] = NULL_CHAR;
332 }
333
334 if (decoded_file_name[0] == NULL_CHAR) {
335 f = strlen (encoded_file_name) - 4;
336 strcpy (decoded_file_name, encoded_file_name);
337 strcpy (&decoded_file_name[f], DFLT_OPEXT_DEC);
338 }
339
340 } /* else arg-line end */
341
342 /* JMZ 10/03/1995 Multilingual */
343 strcpy (decoded_file_name_ml, decoded_file_name);
344 strcat (decoded_file_name_ml, DFLT_OPEXT_ML);
345
346 /* report results of dialog / command line */
347 printf ("Input file = '%s' \nOutput file = '%s' ",
348 encoded_file_name, decoded_file_name);
349 if (mpeg >= 2 && ext_bitstream_name[0] != NULL_CHAR)
350 printf ("Ext_bitstream = '%s'\n", ext_bitstream_name);
351 if (mpeg == 1)
352 printf (">>>\nA MPEG 1 Layer-1/2 bitstream will be decoded\n");
353 else
354 printf (">>>\nA MPEG 2 Layer-1/2 bitstream will be decoded\n");
355
356 if (need_aiff)
357 printf ("Output file written in AIFF format\n");
358
359 if ((musicout = fopen (decoded_file_name, "w+b")) == NULL) {
360 fprintf (stderr, "Could not create \"%s\".\n", decoded_file_name);
361 exit (1);
362 }
363 if (open_bit_stream_r (&bs_mpg, encoded_file_name, BUFFER_SIZE) == 0) {
364 fprintf (stderr, "Could not open \"%s\".\n", encoded_file_name);
365 exit (1);
366 }
367
368 if (need_aiff)
369 if (aiff_seek_to_sound_data (musicout) == -1) {
370 fprintf (stderr, "Could not seek to PCM sound data in \"%s\".\n",
371 decoded_file_name);
372 exit (1);
373 }
374
375 sample_frames = 0;
376 sample_frames_ml = 0;
377 total_error_count = total_error_count_mc = 0;
378 #ifdef Augmentation_7ch
379 total_error_count_aug = 0;
380 #endif
381
382 while (!end_bs (&bs_mpg)) {
383 /****************************************************************/
384 /* */
385 /* START OF FRAME LOOP */
386 /* */
387 /****************************************************************/
388
389 sync = seek_sync_mpg (&bs_mpg);
390 frameBits = bs_mpg.totbits;
391 if (!sync) {
392 printf ("\nFrame cannot be located, end of input stream \n");
393 done = TRUE;
394 /* finally write out the buffer */
395 out_fifo (*pcm_sample, 3, &fr_ps, done, musicout, &sample_frames,
396 channels);
397 out_fifo_ml (*pcm_sample_ml, 3, &fr_ps, done, musicout_ml,
398 &sample_frames_ml);
399 deinit_audio_outputs();
400 break;
401 }
402
403 info.lfe = 0; /* R.S. ! */
404 decode_info (&bs_mpg, &fr_ps);
405 hdr_to_frps (&fr_ps);
406
407 stereo = fr_ps.stereo;
408 error_protection = info.error_protection;
409 crc_error_count = 0;
410 if (frameNum == 0)
411 WriteHdr (&fr_ps, stdout); /* printout layer/mode */
412
413 if (frameNum % frameMod == 0) {
414 fprintf (stderr, "{%4lu}\r", frameNum++);
415 fflush (stderr);
416 if (frameNum / frameMod >= 100)
417 frameMod *= 10;
418 } else
419 frameNum++;
420
421 Frame_Bits = 144 * Bitrate / S_freq;
422 if (info.padding)
423 Frame_Bits++; /* R.Schwalbe add a padding byte */
424 Frame_Bits *= 8; /* R.Schwalbe to get bits per frame *8 */
425 if (error_protection)
426 buffer_CRC (&bs_mpg, &old_crc);
427
428
429 /* 7/8/95 added message WtK 7/8/95 */
430 if (info.lay == 3) {
431 fprintf (stderr, "\nLayer = %d, not supported.\nexit.\n\n", info.lay);
432 break;
433 exit (1);
434 }
435
436 bs_mc.totbits = bs_mc.curpos = 0;
437
438 if (info.lay == 2) {
439 layer_I_frames = 3;
440 bitsPerSlot = 8;
441 samplesPerFrame = 1152;
442 ch_start = 0;
443 channels = stereo;
444 II_decode_bitalloc (&bs_mpg, &fr_ps, bit_alloc, bits_log);
445 II_decode_scale (&bs_mpg, &fr_ps, scfsi, bit_alloc, scale_index,
446 &ch_start, &channels, scfsi_log);
447
448 if (error_protection) {
449 II_CRC_calc (&fr_ps, bit_alloc, scfsi, &new_crc);
450 if (new_crc != old_crc) {
451 printf (" \n ERROR in LAYER 2 - CRC! \n");
452 crc_error_count++;
453 total_error_count++;
454 recover_CRC_error (*pcm_sample, crc_error_count,
455 &fr_ps, musicout, &sample_frames, channels);
456 } else
457 crc_error_count = 0;
458 }
459
460 clip = 0;
461
462 for (i = 0; i < 12; i++) {
463 II_buffer_sample (&bs_mpg, &fr_ps, (*sample), bit_alloc);
464 if (!any_log) {
465 II_dequantize_sample ((*sample), bit_alloc, *fraction, &fr_ps, &i);
466 II_denormalize_sample (*fraction, scale_index, &fr_ps, i >> 2, &i);
467 }
468 } /* end of for loop */
469
470 if (mpeg > 1) {
471 while (bs_mpg.curpos < bs_mpg.totbits)
472 bs_mc.bits[bs_mc.totbits++] = bs_mpg.bits[bs_mpg.curpos++];
473
474 mc_header (&bs_mc, &fr_ps);
475 mc_hdr_to_frps (&fr_ps);
476
477 bs_mc.totbits -= fr_ps.header->n_ad_bytes * 8;
478
479 if (fr_ps.header->ext_bit_stream_present) {
480 sync = seek_sync_ext (&bs_ext, &fr_ps);
481 frameBits += bs_ext.totbits;
482 if (!sync) {
483 fprintf (stderr,
484 "Extension frame cannot be located, end of input stream\n");
485 exit (1);
486 }
487
488 while (bs_ext.curpos < bs_ext.totbits)
489 bs_mc.bits[bs_mc.totbits++] = bs_ext.bits[bs_ext.curpos++];
490 }
491 }
492 }
493
494 /**********************************************************/
495 /* multichannel - decoding */
496 /* 7.07.93 Susanne Ritscher */
497 /* 13.10.93 Ralf Schwalbe */
498 /* 30.05.94 Ralf Schwalbe */
499 /**********************************************************/
500
501 if (rate_log)
502 printf ("frame: %5li bitrate: %8.3f kbps\n", frameNum,
503 frameBits * S_freq / samplesPerFrame);
504 else if (mpeg >= 2) {
505 if ((frameNum == 1) && (fr_ps.header->no_of_multi_lingual_ch > 0)) {
506 /*frameNum==1 NOT frameNum==0 */
507 if (fr_ps.header->no_of_multi_lingual_ch > 7) {
508 printf ("\n\n***Warning. There are %d multilingual channels.\n",
509 fr_ps.header->no_of_multi_lingual_ch);
510 printf
511 (" This decoder supports only upto 7 channels.\n\n");
512 }
513 printf ("There are %d multilingual channels, coded at ",
514 fr_ps.header->no_of_multi_lingual_ch);
515 if (fr_ps.header->multi_lingual_fs == 0)
516 printf ("full");
517 else
518 printf ("half");
519 printf (" sampling rate.\n They are outputted in \"%s\".\n",
520 decoded_file_name_ml);
521 if ((musicout_ml = fopen (decoded_file_name_ml, "w+b")) == NULL) {
522 fprintf (stderr, "***Error. Could not create \"%s\".\nExit\n",
523 decoded_file_name_ml);
524 exit (1);
525 }
526 if (need_aiff) {
527 printf ("Multilingual file written in AIFF format\n");
528 if (aiff_seek_to_sound_data (musicout_ml) == -1) {
529 fprintf (stderr, "Could not seek to ML PCM sound data in \"%s\".\n",
530 decoded_file_name_ml);
531 exit (1);
532 }
533 }
534 }
535
536 mc_channel = fr_ps.mc_channel;
537 crc_error_count_mc = 0;
538 buffer_CRC (&bs_mc, &old_crc); /* read CRC - check from header */
539 mc_composite_status_info (&bs_mc, &fr_ps, tca_log, dynx_log);
540
541 if (frameNum == 1) {
542 if (info.lfe)
543 printf
544 ("mc-channel=%d, MPEG2-ext-length=%d kbit/s lfe-channel present\n",
545 mc_channel,
546 (int) (info.ext_length * s_freq[info.sampling_frequency] / 144));
547 else
548 printf ("mc-channel=%d, MPEG2-ext-length=%d kbit/s\n",
549 mc_channel,
550 (int) (info.ext_length * s_freq[info.sampling_frequency] /
551 144));
552 } //MFC
553 /* Achtung: fr_ps.jsbound = 27; wird in Abh�ngikeit von fs gesetzt */
554 ch_start = stereo;
555 channels = stereo + mc_channel;
556
557 /* decoding bitallocation's */
558
559 II_decode_bitalloc_mc (&bs_mc, &fr_ps, bit_alloc, &ch_start, &channels,
560 bits_log);
561 II_decode_scale (&bs_mc, &fr_ps, scfsi, bit_alloc, scale_index, &ch_start,
562 &channels, scfsi_log);
563
564 /* mandatory CRC check is set */
565 if (crc_check) {
566 mc_error_check (&fr_ps, bit_alloc, scfsi, &new_crc, ch_start, channels);
567 if (new_crc != old_crc) {
568 crc_error_count_mc++;
569 total_error_count_mc++;
570 for (i = 0; i < SBLIMIT; i++)
571 for (ii = ch_start; ii < channels; ++ii)
572 bit_alloc[ii][i] = 0;
573 printf ("\nERROR in MC-CRC -> mc frame can't be decoded !\n");
574 } else
575 crc_error_count_mc = 0;
576 }
577 /* endif crc_check */
578 clip = 0;
579 for (i = 0; i < 12; i++) {
580 II_buffer_sample_mc (&bs_mc, &fr_ps, (*sample), bit_alloc, ch_start,
581 channels, i);
582 if (!any_log) {
583 II_dequantize_sample_mc ((*sample), bit_alloc, *fraction, &fr_ps,
584 ch_start, channels, &i);
585 II_denormalize_sample_mc (*fraction, scale_index, &fr_ps, i >> 2,
586 ch_start, channels, &i);
587 }
588 }
589
590 /* 10/31/95 Ralf Schwalbe LFE */
591 if (info.lfe) {
592 if (lfe_init) {
593 f = strlen (encoded_file_name) - 4;
594 strcpy (lfe_file_name, encoded_file_name);
595 strcpy (&lfe_file_name[f], DFLT_OPEXT_LFE);
596 if ((lfe = fopen (lfe_file_name, "w+b")) == NULL) {
597 fprintf (stderr, "Could not create \"%s\".\n", lfe_file_name);
598 exit (1);
599 }
600 if (need_aiff) {
601 printf ("LFE file written in AIFF format\n");
602 if (aiff_seek_to_sound_data (lfe) == -1) {
603 fprintf (stderr,
604 "Could not seek to LFE PCM sound data in \"%s\".\n",
605 lfe_file_name);
606 exit (1);
607 }
608 }
609 }
610 lfe_init = 0; /* R.S. only one time */
611 II_lfe_calc (&fr_ps);
612 }
613 /*********************************************************************/
614 /* JMZ 09/03/1995 Multilingual , WtK 7/8/95 */
615
616 if (info.no_of_multi_lingual_ch > 0) {
617 /* SWKim060695 add the condion with 32, 44.1kHz case */
618 if (info.sampling_frequency == 1)
619 fr_ps.jsbound = 27;
620 else
621 fr_ps.jsbound = 30;
622
623 m = info.no_of_multi_lingual_ch;
624
625 II_decode_bitalloc_ml (&bs_mc, &fr_ps, bit_alloc_ml, &m);
626 II_decode_scale_ml (&bs_mc, &fr_ps, scfsi_ml, bit_alloc_ml,
627 scale_index_ml, &m);
628
629
630 clip = 0;
631 for (i = 0; i < ((info.multi_lingual_fs == 0) ? 12 : 6); i++) { /* ngr : n_of_granules loop */
632 II_buffer_sample_ml (&bs_mc, &fr_ps, (*sample_ml), bit_alloc_ml, &m);
633 if (!any_log) {
634 II_dequantize_sample_ml ((*sample_ml), bit_alloc_ml, *fraction_ml,
635 &fr_ps, &m, &i);
636 II_denormalize_sample_ml (*fraction_ml, scale_index_ml, &fr_ps,
637 ((info.multi_lingual_fs ==
638 0) ? (i >> 2) : (i >> 1)), &m, &i);
639 }
640 for (k = 0; k < info.no_of_multi_lingual_ch; k++)
641 for (ii = 0; ii < 3; ii++)
642 for (j = 0; j < 32; j++)
643 sym_sample_ml[i][k][ii][j] = (*sample_ml)[k][ii][j];
644 }
645 }
646
647 /* JMZ 09/03/1995 Multilingual */
648 /*********************************************************************/
649
650 #ifdef Augmentation_7ch
651 if (mpeg == 3)
652 if (stereo != 2 || fr_ps.header->surround != 2
653 || fr_ps.header->center == 0) {
654 /* no 3/2 mode */
655 printf ("WARNING: Stream does not contain 5/2 info\n");
656 printf (" Continue decoding of MPEG2 MC part\n");
657 mpeg = 2;
658 }
659
660 if (mpeg == 3) {
661 crc_error_count_aug = 0;
662 mc_aug_composite_status_info (&bs_mc, &fr_ps, tca_log, dynx_log);
663
664 buffer_CRC (&bs_mc, &old_crc); /* read CRC - check from header */
665
666 /* Achtung: fr_ps.jsbound = 27; wird in Abh�ngikeit von fs gesetzt */
667 ch_start = 5;
668 channels = 7;
669
670 /* decoding bitallocation's */
671
672 II_decode_bitalloc_aug (&bs_mc, &fr_ps, bit_alloc, &ch_start, &channels,
673 bits_log);
674 II_decode_scale (&bs_mc, &fr_ps, scfsi, bit_alloc, scale_index,
675 &ch_start, &channels, scfsi_log);
676
677 /* mandatory CRC check is set */
678 if (crc_check) {
679 mc_aug_error_check (&fr_ps, bit_alloc, scfsi, &new_crc);
680 if (new_crc != old_crc) {
681 crc_error_count_aug++;
682 total_error_count_aug++;
683 for (i = 0; i < SBLIMIT; i++)
684 for (ii = ch_start; ii < channels; ++ii)
685 bit_alloc[ii][i] = 0;
686
687 printf ("ERROR in MC-AUG-CRC -> mc aug frame can't be decoded !\n");
688 } else
689 crc_error_count_aug = 0;
690 }
691 /* endif crc_check */
692 clip = 0;
693 for (i = 0; i < 12; i++) {
694 II_buffer_sample_aug (&bs_mc, &fr_ps, (*sample), bit_alloc, i);
695 if (!any_log) {
696 II_dequantize_sample_aug ((*sample), bit_alloc, *fraction, &fr_ps,
697 &i);
698 II_denormalize_sample_aug (*fraction, scale_index, &fr_ps, i >> 2,
699 &i);
700 }
701 }
702 } /* endif mpeg == 3 */
703 #endif
704
705 if (info.mc_prediction_on)
706 for (k = 0; k < 2; k++)
707 for (ii = 0; ii < 8; ii++) {
708 for (j = 0; j < PREDDEL; j++)
709 (*pred_buf)[k][ii][j] = (*pred_buf)[k][ii][j + 36];
710 for (j = 0; j < 3; j++)
711 for (jj = 0; jj < 12; jj++)
712 (*pred_buf)[k][ii][PREDDEL + j + 3 * jj] =
713 (*fraction)[k][ii][j][jj];
714 }
715
716 /* prediction & dematricing MPEG2 part */
717 if (!any_log)
718 if (crc_error_count_mc == 0)
719 dematricing_mc (*fraction, &fr_ps, *pred_buf);
720
721 #ifdef Augmentation_7ch
722 if (mpeg == 3) {
723 if (!any_log)
724 if (crc_error_count_aug == 0) {
725 dematricing_aug (*fraction, &fr_ps);
726 denormalizing_aug (*fraction, &fr_ps);
727 }
728 } else
729 #endif
730 if (crc_error_count_mc == 0)
731 denormalizing_mc (*fraction, &fr_ps, channels);
732 }
733 /* endif mpeg >= 2 */
734 if (!any_log)
735 for (jj = 0; jj < 4 * layer_I_frames; jj++)
736 for (k = 0; k < channels; k++)
737 for (j = 0; j < 3; j++)
738 for (ii = 0; ii < SBLIMIT; ii++)
739 (*fraction_help)[jj][k][j][ii] = (*fraction)[k][ii][j][jj];
740 if (!any_log)
741 for (i = 0; i < 4 * layer_I_frames; i++) {
742 for (j = 0; j < 3; j++)
743 for (k = 0; k < channels; k++)
744 clip += SubBandSynthesis (&((*fraction_help)[i][k][j][0]), k,
745 &((*pcm_sample)[k][j][0]));
746
747 out_fifo (*pcm_sample, 3, &fr_ps, done, musicout,
748 &sample_frames, channels);
749 }
750
751 /* 31/10/95 Ralf Schwalbe LFE output */
752 if (!any_log)
753 if (info.lfe) {
754 for (jj = 0; jj < 12; jj++) {
755 /* lfe_tmp[jj] = (short int) (info.lfe_spl_fraction[jj] * SCALE); */
756 double foo = floor (info.lfe_spl_fraction[jj] * SCALE + 0.5);
757
758 if (foo >= (long) SCALE) {
759 lfe_tmp[jj] = SCALE - 1;
760 clip++;
761 } else if (foo < (long) -SCALE) {
762 lfe_tmp[jj] = -SCALE;
763 clip++;
764 } else
765 lfe_tmp[jj] = foo;
766 }
767 fwrite (&lfe_tmp[0], 2, 12, lfe);
768 }
769 if (clip > 0)
770 printf ("\n%d samples clipped\n", clip);
771
772 /***************************************************************/
773 /* 10/03/1995 JMZ Multilingual , WtK 7/8/95 */
774 if (info.no_of_multi_lingual_ch > 0) {
775 for (jj = 0; jj < ((info.multi_lingual_fs == 0) ? 12 : 6); jj++)
776 for (k = 0; k < info.no_of_multi_lingual_ch; k++)
777 for (j = 0; j < 3; j++)
778 for (ii = 0; ii < SBLIMIT; ii++)
779 (*fraction_help_ml)[jj][k][j][ii] = (*fraction_ml)[k][ii][j][jj];
780
781 clip = 0;
782 for (i = 0; i < ((info.multi_lingual_fs == 0) ? 12 : 6); i++) {
783 for (j = 0; j < 3; j++)
784 for (k = 0; k < info.no_of_multi_lingual_ch; k++)
785 clip += SubBandSynthesis_ml (&((*fraction_help_ml)[i][k][j][0]), k,
786 &((*pcm_sample_ml)[k][j][0]));
787
788 out_fifo_ml (*pcm_sample_ml, 3, &fr_ps, done, musicout_ml,
789 &sample_frames_ml);
790 }
791 if (clip > 0)
792 printf ("\n%d MultiLingual samples clipped\n", clip);
793 }
794 /* 10/03/1995 JMZ Multilingual */
795 /**************************************************************/
796
797 if (print_out == 1) {
798 if (frameNum == 1) {
799 fp6 = fopen ("SUB_ML", "w+");
800 fp5 = fopen ("DYN_CROSS", "w+");
801 fp4 = fopen ("INFODEC", "w+");
802 fp2 = fopen ("BALDEC", "w+");
803 fp3 = fopen ("SCFSIDEC", "w+");
804 fp1 = fopen ("SCFDEC", "w+");
805 }
806 /*********************Ausgabe***************************************************/
807 fprintf (fp1,
808 "******************** FRAME %li *****************************\n",
809 frameNum - 1);
810 fprintf (fp2,
811 "\n******************** FRAME %li *****************************\n",
812 frameNum - 1);
813 fprintf (fp3,
814 "\n******************** FRAME %li *****************************\n",
815 frameNum - 1);
816 fprintf (fp4,
817 "******************** FRAME %li *****************************\n",
818 frameNum - 1);
819 fprintf (fp6,
820 "******************** FRAME %li *****************************\n",
821 frameNum - 1);
822
823 fprintf (fp4, "version = %d, ", info.version);
824 fprintf (fp4, "lay = %d, ", info.lay);
825 fprintf (fp4, "error_protection = %d\n", info.error_protection);
826
827 fprintf (fp4, "bitrate_index = %d, ", info.bitrate_index);
828
829 fprintf (fp4, "sampling_frequency = %d,", info.sampling_frequency);
830 fprintf (fp4, "padding = %d, ", info.padding);
831 fprintf (fp4, "extension = %d\n", info.extension);
832
833 fprintf (fp4, "mode = %d, ", info.mode);
834 fprintf (fp4, "mode_ext = %d,", info.mode_ext);
835 fprintf (fp4, "copyright = %d, ", info.copyright);
836 fprintf (fp4, "original = %d, ", info.original);
837 fprintf (fp4, "emphasis = %d\n", info.emphasis);
838
839 fprintf (fp4, "center = %d, ", info.center);
840 fprintf (fp4, "surround = %d, ", info.surround);
841 fprintf (fp4, "dematrix_procedure = %d, ", info.dematrix_procedure);
842 fprintf (fp4, "lfe = %d\n", info.lfe);
843
844 fprintf (fp4, "no_of_multi_lingual_ch = %d, ",
845 info.no_of_multi_lingual_ch);
846 fprintf (fp4, "multi_lingual_fs = %d, ", info.multi_lingual_fs);
847 fprintf (fp4, "multi_lingual_layer = %d\n", info.multi_lingual_layer);
848
849 fprintf (fp4, "dyn_cross_on = %d\n", info.dyn_cross_on);
850 fprintf (fp4, "mc_prediction_on = %d\n", info.mc_prediction_on);
851
852 #ifdef DEBUG_PREDICTION
853 if (info.mc_prediction_on == 1)
854 for (hu = 0; hu < 8; ++hu) {
855 fprintf (fp4, "mc_prediction[%d] = %d ", hu, info.mc_prediction[hu]);
856 for (ho = 0; ho < 4; ho++) {
857 fprintf (fp4, "mc_predsi[%d][%d] = %d ", hu, ho,
858 info.mc_predsi[hu][ho]);
859 fprintf (fp4, "mc_delay[%d][%d] = %d ", hu, ho,
860 info.mc_delay_comp[hu][ho]);
861 fprintf (fp4, " \n");
862 for (hi = 0; hi < 3; hi++)
863 fprintf (fp4, "pred_coeff[%d][%d][%d] = %d ", hu, ho, hi,
864 info.mc_pred_coeff[hu][ho][hi]);
865 fprintf (fp4, " \n");
866 }
867 }
868 #endif
869
870 if (info.dyn_cross_on == 1) {
871 fprintf (fp5,
872 "\n******************** FRAME %li *****************************\n",
873 frameNum - 1);
874 fprintf (fp5, "dyn_cross_LR = %d\n", info.dyn_cross_LR);
875 for (ho = 0; ho < 12; ho++) {
876 fprintf (fp5, "dyn_cross[%d] = %d ", ho, info.dyn_cross_mode[ho]);
877 if ((ho + 1) % 4 == 0)
878 fprintf (fp5, "\n");
879 }
880 if (info.surround == 3)
881 for (ho = 0; ho < 12; ho++) {
882 fprintf (fp5, "dyn_second_stereo[%d] = %d ", ho,
883 info.dyn_second_stereo[ho]);
884 if ((ho + 1) % 4 == 0)
885 fprintf (fp5, "\n");
886 }
887 }
888
889 fprintf (fp4, "tc_sbgr_select = %d\n", info.tc_sbgr_select);
890 if (info.tc_sbgr_select == 1)
891 fprintf (fp4, "tc_allocation = %d\n", info.tc_allocation);
892 else {
893 for (ho = 0; ho < 12; ho++) {
894 if (info.mc_prediction[ho] == 0)
895 fprintf (fp4, "tc_alloc[%d] = %d\n", ho, info.tc_alloc[ho]);
896 else {
897 for (hu = 0; hu < 4; hu++)
898 fprintf (fp4, "predsi[%d][%d] = %d\n", ho, hu,
899 info.mc_predsi[ho][hu]);
900 }
901 }
902 fprintf (fp4, "\n");
903 }
904
905 for (hu = 0; hu < SBLIMIT; ++hu) {
906 fprintf (fp2, "\n");
907 fprintf (fp3, "\n");
908
909 for (hi = 0; hi < channels; hi++) {
910 fprintf (fp1, "\n");
911 if (layer_I_frames == 1)
912 fprintf (fp2, "BAL[%d][%02d] = %d ", hi, hu,
913 I_bit_alloc[0][hi][hu]);
914 else
915 fprintf (fp2, "BAL[%d][%02d] = %d ", hi, hu, bit_alloc[hi][hu]);
916 fprintf (fp3, "SCFSI[%d][%02d] = %d ", hi, hu, scfsi[hi][hu]);
917
918 for (ho = 0; ho < layer_I_frames; ho++) {
919 fprintf (fp1, "SCF[%d][%d][%02d] = %d ", hi, ho, hu,
920 scale_index[hi][ho][hu]);
921 }
922 }
923 for (hi = 0; hi < info.no_of_multi_lingual_ch; hi++) {
924 fprintf (fp1, "\n");
925 /* 960315 added FDB Multi-Lingual bitalloc */
926 fprintf (fp2, "BAL_ML[%d][%02d] = %d ", hi, hu,
927 bit_alloc_ml[hi][hu]);
928 /* 960315 added FDB Multi-Lingual scf select info */
929 fprintf (fp3, "SCFSI_ML[%d][%02d] = %d ", hi, hu, scfsi_ml[hi][hu]);
930 /* 960315 added FDB Multi-Lingual scfs */
931 for (ho = 0; ho < 3; ho++)
932 fprintf (fp1, "SCF_ML[%d][%d][%02d] = %d ", hi, ho, hu,
933 scale_index_ml[hi][ho][hu]);
934 }
935 fprintf (fp1, "\n");
936 }
937
938 /* 960318 added FDB Multi-Lingual subband samples symbols*/
939 for (k = 0; k < info.no_of_multi_lingual_ch; k++)
940 for (ii = 0; ii < SBLIMIT; ii++) {
941 fprintf (fp6, "SB_SYM_ML[%d][%02d]: ", k, ii);
942 for (jj = 0; jj < ((info.multi_lingual_fs == 0) ? 12 : 6); jj++)
943 for (j = 0; j < 3; j++)
944 fprintf (fp6, " %6d", sym_sample_ml[jj][k][j][ii]);
945 fprintf (fp6, "\n");
946 }
947
948
949 /* 960318 added FDB Multi-Lingual subband samples */
950 for (k = 0; k < info.no_of_multi_lingual_ch; k++)
951 for (ii = 0; ii < SBLIMIT; ii++) {
952 fprintf (fp6, "SB_ML[%d][%02d]: ", k, ii);
953 for (jj = 0; jj < ((info.multi_lingual_fs == 0) ? 12 : 6); jj++)
954 for (j = 0; j < 3; j++)
955 fprintf (fp6, " %12.5e", (*fraction_help_ml)[jj][k][j][ii]);
956 fprintf (fp6, "\n");
957 }
958 fflush (fp1);
959 fflush (fp2);
960 fflush (fp3);
961 fflush (fp4);
962 fflush (fp5);
963 fflush (fp6);
964 /*******************************************************************************/
965
966 }
967
968
969
970
971
972
973 /* endif of print_out == 1 */
974 /****************************************************************/
975 /* */
976 /* END OF FRAME LOOP */
977 /* */
978 /****************************************************************/
979 } /*end of while(!endof(bs)) - loop */
980
981 if (need_aiff) {
982 #ifdef Augmentation_7ch
983 if (mpeg == 3)
984 pcm_aiff_data.numChannels = 7;
985 else
986 #endif
987 pcm_aiff_data.numChannels = stereo + mc_channel;
988 pcm_aiff_data.numSampleFrames = sample_frames;
989 pcm_aiff_data.sampleSize = 16;
990 pcm_aiff_data.sampleRate = s_freq[info.sampling_frequency] * 1000;
991 strcpy (pcm_aiff_data.sampleType, IFF_ID_SSND);
992 pcm_aiff_data.blkAlgn.offset = 0;
993 pcm_aiff_data.blkAlgn.blockSize = 0;
994
995 if (aiff_write_headers (musicout, &pcm_aiff_data) == -1) {
996 fprintf (stderr, "Could not write AIFF headers to \"%s\"\n",
997 decoded_file_name);
998 exit (1);
999 }
1000
1001 if (fr_ps.header->no_of_multi_lingual_ch > 0) {
1002 pcm_aiff_data.numChannels = fr_ps.header->no_of_multi_lingual_ch;
1003 pcm_aiff_data.numSampleFrames = sample_frames;
1004 pcm_aiff_data.sampleSize = 16;
1005 pcm_aiff_data.sampleRate = s_freq[info.sampling_frequency] * 1000;
1006 if (fr_ps.header->multi_lingual_fs == 1)
1007 pcm_aiff_data.sampleRate *= 0.5;
1008 strcpy (pcm_aiff_data.sampleType, IFF_ID_SSND);
1009 pcm_aiff_data.blkAlgn.offset = 0;
1010 pcm_aiff_data.blkAlgn.blockSize = 0;
1011
1012 if (aiff_write_headers (musicout_ml, &pcm_aiff_data) == -1) {
1013 fprintf (stderr, "Could not write AIFF headers to \"%s\"\n",
1014 decoded_file_name_ml);
1015 exit (1);
1016 }
1017 }
1018 if (lfe_init == 0) {
1019 pcm_aiff_data.numChannels = 1;
1020 pcm_aiff_data.numSampleFrames = sample_frames / 96;
1021 pcm_aiff_data.sampleSize = 16;
1022 pcm_aiff_data.sampleRate = (s_freq[info.sampling_frequency] * 1000) / 96;
1023 strcpy (pcm_aiff_data.sampleType, IFF_ID_SSND);
1024 pcm_aiff_data.blkAlgn.offset = 0;
1025 pcm_aiff_data.blkAlgn.blockSize = 0;
1026
1027 if (aiff_write_headers (lfe, &pcm_aiff_data) == -1) {
1028 fprintf (stderr, "Could not write AIFF headers to \"%s\"\n",
1029 lfe_file_name);
1030 exit (1);
1031 }
1032 }
1033 }
1034
1035 close_bit_stream_r (&bs_mpg);
1036
1037 if (info.ext_bit_stream_present) {
1038 close_bit_stream_r (&bs_ext);
1039
1040 }
1041 fclose (musicout);
1042 if (info.lfe)
1043 fclose (lfe);
1044
1045
1046 #ifdef MACINTOSH
1047 if (need_aiff)
1048 set_mac_file_attr (decoded_file_name, VOL_REF_NUM,
1049 CREATR_DEC_AIFF, FILTYP_DEC_AIFF);
1050 else
1051 set_mac_file_attr (decoded_file_name, VOL_REF_NUM,
1052 CREATR_DEC_BNRY, FILTYP_DEC_BNRY);
1053 #endif
1054
1055 printf ("Decoding of \"%s\" with %lu frames is finished\n", encoded_file_name,
1056 frameNum);
1057 printf ("The decoded PCM output file name is \"%s\"\n", decoded_file_name);
1058 if (need_aiff)
1059 printf ("\"%s\" has been written with AIFF header information\n",
1060 decoded_file_name);
1061
1062 if (fr_ps.header->no_of_multi_lingual_ch > 0) {
1063 printf ("There are %d multilingual channels, coded at ",
1064 fr_ps.header->no_of_multi_lingual_ch);
1065 if (fr_ps.header->multi_lingual_fs == 0)
1066 printf ("full");
1067 else
1068 printf ("half");
1069 printf (" sampling rate.\n They are outputted in \"%s\".\n",
1070 decoded_file_name_ml);
1071 if (need_aiff)
1072 printf
1073 ("Multilingual file has been written with AIFF header information\n");
1074 }
1075
1076 if (total_error_count_mc != 0)
1077 printf ("There were %d frames, which were not in multichannel!!\n",
1078 total_error_count_mc);
1079 #ifdef Augmentation_7ch
1080 if (total_error_count_aug != 0)
1081 printf
1082 ("There were %d frames, which were not in augmented multichannel!!\n",
1083 total_error_count_aug);
1084 #endif
1085 if (print_out == 1) {
1086 fclose (fp1);
1087 fclose (fp2);
1088 fclose (fp3);
1089 fclose (fp4);
1090 fclose (fp5);
1091 fclose (fp6);
1092 }
1093 return (1);
1094 }
1095
1096
usage(void)1097 void usage (void)
1098 { /* print syntax & exit */
1099 fprintf (stderr,
1100 "usage: %s queries for all arguments, or\n",
1101 programName);
1102 fprintf (stderr,
1103 " %s [-a] [-h] [-i] [-m] inputBS [outPCM]\n", programName);
1104 fprintf (stderr, "where\n");
1105 fprintf (stderr,
1106 " -a write a RAW data sound file (default: write an AIFF sound file)\n");
1107 fprintf (stderr,
1108 " -b write alloc bits per subband to stdout (default: off)\n");
1109 fprintf (stderr, " -d write dynx modes to stdout (default: off)\n");
1110 fprintf (stderr,
1111 " -h bitstreams contain a 2304 byte header (default: no header)\n");
1112 fprintf (stderr, " -i write decoded bitstream-information in a file\n");
1113 /* default: write encoded information */
1114 fprintf (stderr, " -m encoded bitstream is a MPEG 1 Layer-2 file\n");
1115 /* default: decode a MPEG 2 Layer-2 bitstream */
1116 #ifdef Augmentation_7ch
1117 fprintf (stderr,
1118 " -n encoded bitstream is a MPEG 2 Layer-2 file with 7.1 augmentation\n");
1119 /* default: decode a MPEG 2 Layer-2 bitstream */
1120 #endif
1121 fprintf (stderr,
1122 " -s write scf bits per subband to stdout (default: off)\n");
1123 fprintf (stderr, " -t write tca modes to stdout (default: off)\n");
1124 fprintf (stderr, " inputBS input bit stream of encoded audio\n");
1125 fprintf (stderr, " outPCM output PCM sound file (dflt inName)\n");
1126 exit (1);
1127 }
1128