r123: Merged HEAD and TEST. New stuff shall be committed to HEAD from now on.
[cinelerra_cv/mob.git] / plugins / toolame / musicin.c
blobb2c2d9aae54e968340ece57f7250954e2c7284ad
1 #include <stdlib.h>
2 #include "common.h"
3 #include "encoder.h"
4 #include <assert.h>
6 /* Global variable definitions for "musicin.c" */
8 FILE *musicin;
9 Bit_stream_struc bs;
10 char *programName;
12 /************************************************************************
14 * main
16 * PURPOSE: MPEG I Encoder supporting layer 2 only
17 * psychoacoustic models 1 (MUSICAM) and 2 (AT&T)
19 * SEMANTICS: One overlapping frame of audio of up to 2 channels are
20 * processed at a time in the following order:
21 * (associated routines are in parentheses)
23 * 1. Filter sliding window of data to get 32 subband
24 * samples per channel.
25 * (window_subband,filter_subband)
27 * 2. If joint stereo mode, combine left and right channels
28 * for subbands above #jsbound#.
29 * (combine_LR)
31 * 3. Calculate scalefactors for the frame, and if layer 2,
32 * also calculate scalefactor select information.
33 * (scale_factor_calc)
35 * 4. Calculate psychoacoustic masking levels using selected
36 * psychoacoustic model.
37 * (tonal.c/psycho_one, psy.c/psycho_two)
39 * 5. Perform iterative bit allocation for subbands with low
40 * mask_to_noise ratios using masking levels from step 4.
41 * (main_bit_allocation)
43 * 6. If error protection flag is active, add redundancy for
44 * error protection.
45 * (CRC_calc)
47 * 7. Pack bit allocation, scalefactors, and scalefactor select
48 * information (layer 2) onto bitstream.
49 * (encode_bit_alloc,encode_scale,transmission_pattern)
51 * 8. Quantize subbands and pack them into bitstream
52 * (subband_quantization, sample_encoding)
54 ************************************************************************/
56 int
57 main (int argc, char **argv)
59 typedef double SBS[2][3][SCALE_BLOCK][SBLIMIT];
60 SBS *sb_sample;
61 typedef double JSBS[3][SCALE_BLOCK][SBLIMIT];
62 JSBS *j_sample;
63 typedef double IN[2][HAN_SIZE];
64 IN *win_que;
65 typedef unsigned int SUB[2][3][SCALE_BLOCK][SBLIMIT];
66 SUB *subband;
67 FILE *stdin_fd = stdin;
69 int use_stdin = 0;
71 int frameNum = 0;
73 frame_params fr_ps;
74 layer info;
75 char original_file_name[MAX_NAME_SIZE];
76 char encoded_file_name[MAX_NAME_SIZE];
77 short **win_buf;
78 static short buffer[2][1152];
79 static unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];
80 static unsigned int scalar[2][3][SBLIMIT], j_scale[3][SBLIMIT];
81 static double ltmin[2][SBLIMIT], lgmin[2][SBLIMIT], max_sc[2][SBLIMIT];
82 FLOAT snr32[32];
83 short sam[2][1344]; /* was [1056]; */
84 int whole_SpF, extra_slot = 0;
85 double avg_slots_per_frame, frac_SpF, slot_lag;
86 int model, stereo, error_protection;
87 static unsigned int crc;
88 int i, j, k, adb;
89 unsigned long bitsPerSlot, samplesPerFrame;
90 unsigned long frameBits, sentBits = 0;
91 unsigned long num_samples;
94 /* Most large variables are declared dynamically to ensure
95 compatibility with smaller machines */
97 sb_sample = (SBS *) mem_alloc (sizeof (SBS), "sb_sample");
98 j_sample = (JSBS *) mem_alloc (sizeof (JSBS), "j_sample");
99 win_que = (IN *) mem_alloc (sizeof (IN), "Win_que");
100 subband = (SUB *) mem_alloc (sizeof (SUB), "subband");
101 win_buf = (short **) mem_alloc (sizeof (short *) * 2, "win_buf");
103 /* clear buffers */
104 memset ((char *) buffer, 0, sizeof (buffer));
105 memset ((char *) bit_alloc, 0, sizeof (bit_alloc));
106 memset ((char *) scalar, 0, sizeof (scalar));
107 memset ((char *) j_scale, 0, sizeof (j_scale));
108 memset ((char *) scfsi, 0, sizeof (scfsi));
109 memset ((char *) ltmin, 0, sizeof (ltmin));
110 memset ((char *) lgmin, 0, sizeof (lgmin));
111 memset ((char *) max_sc, 0, sizeof (max_sc));
112 memset ((char *) snr32, 0, sizeof (snr32));
113 memset ((char *) sam, 0, sizeof (sam));
115 fr_ps.header = &info;
116 fr_ps.tab_num = -1; /* no table loaded */
117 fr_ps.alloc = NULL;
118 info.version = MPEG_AUDIO_ID; /* Default: MPEG-1 */
120 programName = argv[0];
121 if (argc == 1) /* no command-line args */
122 usage ();
123 else
124 parse_args (argc, argv, &fr_ps, &model, &num_samples, &use_stdin,
125 original_file_name, encoded_file_name);
129 print_config (&fr_ps, &model, original_file_name, encoded_file_name);
131 hdr_to_frps (&fr_ps);
132 stereo = fr_ps.stereo;
133 error_protection = info.error_protection;
135 bitsPerSlot = 8;
136 samplesPerFrame = 1152;
138 /* Figure average number of 'slots' per frame. */
139 /* Bitrate means TOTAL for both channels, not per side. */
140 avg_slots_per_frame = ((double) samplesPerFrame /
141 s_freq[info.version][info.sampling_frequency]) *
142 ((double) bitrate[info.version][info.bitrate_index] /
143 (double) bitsPerSlot);
144 whole_SpF = (int) avg_slots_per_frame;
145 fprintf (stderr, "slots/frame = %d\n", whole_SpF);
146 frac_SpF = avg_slots_per_frame - (double) whole_SpF;
147 slot_lag = -frac_SpF;
148 fprintf (stderr, "frac SpF=%.3f, tot bitrate=%d kbps, s freq=%.1f kHz\n",
149 frac_SpF, bitrate[info.version][info.bitrate_index],
150 s_freq[info.version][info.sampling_frequency]);
152 if (frac_SpF != 0)
153 fprintf (stderr, "Fractional number of slots, padding required\n");
154 else
155 info.padding = 0;
157 if (model == 2)
158 init_psycho_two((FLOAT) s_freq[info.version][info.sampling_frequency] * 1000);
160 while (get_audio (stdin_fd, musicin, buffer, num_samples, stereo, &info) > 0)
162 if (++frameNum % 10 == 0)
164 fprintf (stderr, "{%4u}\r", frameNum);
165 fflush (stderr);
167 win_buf[0] = &buffer[0][0];
168 win_buf[1] = &buffer[1][0];
169 if (frac_SpF != 0)
171 if (slot_lag > (frac_SpF - 1.0))
173 slot_lag -= frac_SpF;
174 extra_slot = 0;
175 info.padding = 0;
176 /* fprintf(stderr,"No padding for this frame\n"); */
178 else
180 extra_slot = 1;
181 info.padding = 1;
182 slot_lag += (1 - frac_SpF);
183 /* fprintf(stderr,"Padding for this frame\n"); */
186 adb = (whole_SpF + extra_slot) * bitsPerSlot;
189 for (i = 0; i < 3; i++)
190 for (j = 0; j < SCALE_BLOCK; j++)
191 for (k = 0; k < stereo; k++)
193 window_subband (&win_buf[k], &(*win_que)[k][0], k);
194 filter_subband (&(*win_que)[k][0], &(*sb_sample)[k][i][j][0]);
197 scale_factor_calc (*sb_sample, scalar, stereo, fr_ps.sblimit);
198 pick_scale (scalar, &fr_ps, max_sc);
199 if (fr_ps.actual_mode == MPG_MD_JOINT_STEREO)
201 combine_LR (*sb_sample, *j_sample, fr_ps.sblimit);
202 scale_factor_calc (j_sample, &j_scale, 1, fr_ps.sblimit);
203 } /* this way we calculate more mono than we need */
204 /* but it is cheap */
206 if (model == 1)
207 psycho_one (buffer, max_sc, ltmin, &fr_ps);
208 else
210 for (k = 0; k < stereo; k++)
212 psycho_two (&buffer[k][0], &sam[k][0], k, snr32,
213 (FLOAT) s_freq[info.version][info.
214 sampling_frequency]
215 * 1000);
216 for (i = 0; i < SBLIMIT; i++)
217 ltmin[k][i] = (double) snr32[i];
221 transmission_pattern (scalar, scfsi, &fr_ps);
222 main_bit_allocation (ltmin, scfsi, bit_alloc, &adb, &fr_ps);
224 if (error_protection)
225 CRC_calc (&fr_ps, bit_alloc, scfsi, &crc);
227 encode_info (&fr_ps, &bs);
229 if (error_protection)
230 encode_CRC (crc, &bs);
232 encode_bit_alloc (bit_alloc, &fr_ps, &bs);
233 encode_scale (bit_alloc, scfsi, scalar, &fr_ps, &bs);
234 subband_quantization (scalar, *sb_sample, j_scale,
235 *j_sample, bit_alloc, *subband, &fr_ps);
236 sample_encoding (*subband, bit_alloc, &fr_ps, &bs);
237 for (i = 0; i < adb; i++)
238 put1bit (&bs, 0);
242 frameBits = sstell (&bs) - sentBits;
243 if (frameBits % bitsPerSlot) /* a program failure */
244 fprintf (stderr, "Sent %ld bits = %ld slots plus %ld\n",
245 frameBits, frameBits / bitsPerSlot, frameBits % bitsPerSlot);
246 sentBits += frameBits;
250 close_bit_stream_w (&bs);
252 fprintf (stderr, "Avg slots/frame = %.3f; b/smp = %.2f; br = %.3f kbps\n",
253 (FLOAT) sentBits / (frameNum * bitsPerSlot),
254 (FLOAT) sentBits / (frameNum * samplesPerFrame),
255 (FLOAT) sentBits / (frameNum * samplesPerFrame) *
256 s_freq[info.version][info.sampling_frequency]);
258 if (use_stdin && fclose (musicin) != 0)
260 fprintf (stderr, "Could not close \"%s\".\n", original_file_name);
261 exit (2);
264 fprintf (stderr,
265 "Encoding of \"%s\" with psychoacoustic model %d is finished\n",
266 original_file_name, model);
267 fprintf (stderr, "The MPEG encoded output file name is \"%s\"\n",
268 encoded_file_name);
270 /* Cleanup */
271 if (model == 2)
272 cleanup_psycho_two();
274 free (sb_sample);
275 free (j_sample);
276 free (win_que);
277 free (subband);
278 free (win_buf);
279 free (fr_ps.alloc);
281 exit (0);
284 /************************************************************************
286 * usage
288 * PURPOSE: Writes command line syntax to the file specified by #stderr#
290 ************************************************************************/
292 void
293 usage () /* print syntax & exit */
295 fprintf (stdout,
296 "usage: %s\n",
297 programName);
298 fprintf (stdout,
299 " %s [-m mode][-p psy][-s sfrq][-b br][-d emp]\n",
300 programName);
301 fprintf (stdout, " [-c][-o][-e] inputPCM [outBS]\n");
302 fprintf (stdout, "where\n");
303 fprintf (stdout, " -m mode channel mode : s/d/j/m (dflt %4c)\n",
304 DFLT_MOD);
305 fprintf (stdout, " -p psy psychoacoustic model 1/2 (dflt %4u)\n",
306 DFLT_PSY);
307 fprintf (stdout, " -s sfrq input smpl rate in kHz (dflt %4.1f)\n",
308 DFLT_SFQ);
309 fprintf (stdout, " -b br total bitrate in kbps (dflt highest)\n");
310 fprintf (stdout, " -d emp de-emphasis n/5/c (dflt %4c)\n",
311 DFLT_EMP);
312 fprintf (stdout, " -c mark as copyright\n");
313 fprintf (stdout, " -o mark as original\n");
314 fprintf (stdout, " -e add error protection\n");
315 fprintf (stdout, " inputPCM input PCM sound file (standard or AIFF)\n");
316 fprintf (stdout,
317 " outBS output bit stream of encoded audio (dflt inName+%s)\n",
318 DFLT_EXT);
319 exit (1);
322 /************************************************************************
324 * parse_args
326 * PURPOSE: Sets encoding parameters to the specifications of the
327 * command line. Default settings are used for parameters
328 * not specified in the command line.
330 * SEMANTICS: The command line is parsed according to the following
331 * syntax:
333 * -m is followed by the mode
334 * -p is followed by the psychoacoustic model number
335 * -s is followed by the sampling rate
336 * -b is followed by the total bitrate, irrespective of the mode
337 * -d is followed by the emphasis flag
338 * -c is followed by the copyright/no_copyright flag
339 * -o is followed by the original/not_original flag
340 * -e is followed by the error_protection on/off flag
342 * If the input file is in AIFF format, the sampling frequency is read
343 * from the AIFF header.
345 * The input and output filenames are read into #inpath# and #outpath#.
347 ************************************************************************/
349 void
350 parse_args (int argc, char **argv, frame_params * fr_ps, int *psy,
351 unsigned long *num_samples, int *use_stdin, char inPath[MAX_NAME_SIZE],
352 char outPath[MAX_NAME_SIZE])
354 FLOAT srate;
355 int brate;
356 layer *info = fr_ps->header;
357 int err = 0, i = 0;
358 IFF_AIFF pcm_aiff_data;
359 long samplerate;
360 long soundPosition;
362 /* preset defaults */
363 inPath[0] = '\0';
364 outPath[0] = '\0';
365 info->lay = 2;
366 switch (DFLT_MOD)
368 case 's':
369 info->mode = MPG_MD_STEREO;
370 info->mode_ext = 0;
371 break;
372 case 'd':
373 info->mode = MPG_MD_DUAL_CHANNEL;
374 info->mode_ext = 0;
375 break;
376 case 'j':
377 info->mode = MPG_MD_JOINT_STEREO;
378 break;
379 case 'm':
380 info->mode = MPG_MD_MONO;
381 info->mode_ext = 0;
382 break;
383 default:
384 fprintf (stderr, "%s: Bad mode dflt %c\n", programName, DFLT_MOD);
385 abort ();
387 *psy = DFLT_PSY;
388 if ((info->sampling_frequency =
389 SmpFrqIndex ((long) (1000 * DFLT_SFQ), &info->version)) < 0)
391 fprintf (stderr, "%s: bad sfrq default %.2f\n", programName, DFLT_SFQ);
392 abort ();
394 info->bitrate_index = 14;
395 brate = 0;
396 switch (DFLT_EMP)
398 case 'n':
399 info->emphasis = 0;
400 break;
401 case '5':
402 info->emphasis = 1;
403 break;
404 case 'c':
405 info->emphasis = 3;
406 break;
407 default:
408 fprintf (stderr, "%s: Bad emph dflt %c\n", programName, DFLT_EMP);
409 abort ();
411 info->copyright = 0;
412 info->original = 0;
413 info->error_protection = FALSE;
415 /* process args */
416 while (++i < argc && err == 0)
418 char c, *token, *arg, *nextArg;
419 int argUsed;
421 token = argv[i];
422 if (*token++ == '-')
424 if (i + 1 < argc)
425 nextArg = argv[i + 1];
426 else
427 nextArg = "";
428 argUsed = 0;
430 // Plain '-'
431 if(*token == 0)
433 *use_stdin = 1;
438 while ((c = *token++))
440 if (*token /* NumericQ(token) */ )
441 arg = token;
442 else
443 arg = nextArg;
451 switch (c)
453 case 'm':
454 argUsed = 1;
455 if (*arg == 's')
457 info->mode = MPG_MD_STEREO;
458 info->mode_ext = 0;
460 else if (*arg == 'd')
462 info->mode = MPG_MD_DUAL_CHANNEL;
463 info->mode_ext = 0;
465 else if (*arg == 'j')
467 info->mode = MPG_MD_JOINT_STEREO;
469 else if (*arg == 'm')
471 info->mode = MPG_MD_MONO;
472 info->mode_ext = 0;
474 else
476 fprintf (stderr, "%s: -m mode must be s/d/j/m not %s\n",
477 programName, arg);
478 err = 1;
480 break;
481 case 'p':
482 *psy = atoi (arg);
483 argUsed = 1;
484 if (*psy < 1 || *psy > 2)
486 fprintf (stderr,
487 "%s: -p model must be 1 or 2, not %s\n",
488 programName, arg);
489 err = 1;
491 break;
493 case 's':
494 argUsed = 1;
495 srate = atof (arg);
496 /* samplerate = rint( 1000.0 * srate ); $A */
497 samplerate = (long) ((1000.0 * srate) + 0.5);
498 if ((info->sampling_frequency =
499 SmpFrqIndex ((long) samplerate, &info->version)) < 0)
500 err = 1;
501 break;
503 case 'b':
504 argUsed = 1;
505 brate = atoi (arg);
506 break;
507 case 'd':
508 argUsed = 1;
509 if (*arg == 'n')
510 info->emphasis = 0;
511 else if (*arg == '5')
512 info->emphasis = 1;
513 else if (*arg == 'c')
514 info->emphasis = 3;
515 else
517 fprintf (stderr, "%s: -d emp must be n/5/c not %s\n",
518 programName, arg);
519 err = 1;
521 break;
522 case 'c':
523 info->copyright = 1;
524 break;
525 case 'o':
526 info->original = 1;
527 break;
528 case 'e':
529 info->error_protection = TRUE;
530 break;
531 default:
532 fprintf (stderr, "%s: unrec option %c\n", programName, c);
533 err = 1;
534 break;
536 if (argUsed)
538 if (arg == token)
539 token = ""; /* no more from token */
540 else
541 ++i; /* skip arg we used */
542 arg = "";
543 argUsed = 0;
547 else
549 if (inPath[0] == '\0' && !*use_stdin)
550 strcpy (inPath, argv[i]);
551 else if (outPath[0] == '\0')
552 strcpy (outPath, argv[i]);
553 else
555 fprintf (stderr, "%s: excess arg %s\n", programName, argv[i]);
556 err = 1;
561 if (err || (inPath[0] == '\0' && !*use_stdin))
562 usage (); /* never returns */
564 if (outPath[0] == '\0')
566 /* replace old extension with new one, 1992-08-19, 1995-06-12 shn */
567 new_ext (inPath, DFLT_EXT, outPath);
570 if (!*use_stdin && (musicin = fopen (inPath, "rb")) == NULL)
572 fprintf (stderr, "Could not find \"%s\".\n", inPath);
573 exit (1);
576 open_bit_stream_w (&bs, outPath, BUFFER_SIZE);
578 if (!*use_stdin && (soundPosition = aiff_read_headers (musicin, &pcm_aiff_data)) != -1)
581 fprintf (stderr, ">>> Using Audio IFF sound file headers\n");
583 aiff_check (inPath, &pcm_aiff_data, &info->version);
585 if (fseek (musicin, soundPosition, SEEK_SET) != 0)
587 fprintf (stderr, "Could not seek to PCM sound data in \"%s\".\n",
588 inPath);
589 exit (1);
592 info->sampling_frequency =
593 SmpFrqIndex ((long) pcm_aiff_data.sampleRate, &info->version);
594 fprintf (stderr, ">>> %f Hz sampling frequency selected\n",
595 pcm_aiff_data.sampleRate);
597 /* Determine number of samples in sound file */
598 *num_samples = pcm_aiff_data.numChannels *
599 pcm_aiff_data.numSampleFrames;
600 if (pcm_aiff_data.numChannels == 1)
602 info->mode = MPG_MD_MONO;
603 info->mode_ext = 0;
606 else
607 { /* Not using Audio IFF sound file headers. */
609 if (!*use_stdin && fseek (musicin, 0, SEEK_SET) != 0)
611 fprintf (stderr, "Could not seek to PCM sound data in \"%s\".\n",
612 inPath);
613 exit (1);
616 /* Declare sound file to have "infinite" number of samples. */
617 *num_samples = MAX_U_32_NUM;
620 if (brate == 0)
621 brate = bitrate[info->version][14];
622 if ((info->bitrate_index = BitrateIndex (brate, info->version)) < 0)
623 err = 1;
624 if (err || (!*use_stdin && inPath[0] == '\0'))
625 usage (); /* never returns */
629 /************************************************************************
631 * print_config
633 * PURPOSE: Prints the encoding parameters used
635 ************************************************************************/
637 void
638 print_config (frame_params * fr_ps, int *psy, char *inPath, char *outPath)
640 layer *info = fr_ps->header;
642 fprintf (stderr, "Encoding configuration:\n");
643 fprintf (stderr, "Algorithm=%s\n", version_names[info->version]);
644 if (info->mode != MPG_MD_JOINT_STEREO)
645 fprintf (stderr, "Layer=II mode=%s extn=%d psy model=%d\n",
646 mode_names[info->mode], info->mode_ext, *psy);
647 else
648 fprintf (stderr,
649 "Layer=II mode=%s extn=data dependant psy model=%d\n",
650 mode_names[info->mode], *psy);
651 fprintf (stderr, "samp frq=%.1f kHz total bitrate=%d kbps\n",
652 s_freq[info->version][info->sampling_frequency],
653 bitrate[info->version][info->bitrate_index]);
654 fprintf (stderr, "de-emph=%d c/right=%d orig=%d errprot=%s\n",
655 info->emphasis, info->copyright, info->original,
656 ((info->error_protection) ? "on" : "off"));
657 fprintf (stderr, "input file: '%s'\noutput file: '%s'\n", inPath, outPath);
660 /************************************************************************
662 * aiff_check
664 * PURPOSE: Checks AIFF header information to make sure it is valid.
665 * Exits if not.
667 ************************************************************************/
669 void
670 aiff_check (char *file_name, IFF_AIFF * pcm_aiff_data, int *version)
672 if (pcm_aiff_data->sampleType != IFF_ID_SSND)
674 fprintf (stderr, "Sound data is not PCM in \"%s\".\n", file_name);
675 exit (1);
678 if (SmpFrqIndex ((long) pcm_aiff_data->sampleRate, version) < 0)
680 fprintf (stderr, "in \"%s\".\n", file_name);
681 exit (1);
684 if (pcm_aiff_data->sampleSize != sizeof (short) * BITS_IN_A_BYTE)
686 fprintf (stderr, "Sound data is not %d bits in \"%s\".\n",
687 sizeof (short) * BITS_IN_A_BYTE, file_name);
688 exit (1);
691 if (pcm_aiff_data->numChannels != MONO &&
692 pcm_aiff_data->numChannels != STEREO)
694 fprintf (stderr, "Sound data is not mono or stereo in \"%s\".\n",
695 file_name);
696 exit (1);
699 if (pcm_aiff_data->blkAlgn.blockSize != 0)
701 fprintf (stderr, "Block size is not %d bytes in \"%s\".\n", 0,
702 file_name);
703 exit (1);
706 if (pcm_aiff_data->blkAlgn.offset != 0)
708 fprintf (stderr, "Block offset is not %d bytes in \"%s\".\n", 0,
709 file_name);
710 exit (1);