6 /* Global variable definitions for "musicin.c" */
12 /************************************************************************
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#.
31 * 3. Calculate scalefactors for the frame, and if layer 2,
32 * also calculate scalefactor select information.
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
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 ************************************************************************/
57 main (int argc
, char **argv
)
59 typedef double SBS
[2][3][SCALE_BLOCK
][SBLIMIT
];
61 typedef double JSBS
[3][SCALE_BLOCK
][SBLIMIT
];
63 typedef double IN
[2][HAN_SIZE
];
65 typedef unsigned int SUB
[2][3][SCALE_BLOCK
][SBLIMIT
];
67 FILE *stdin_fd
= stdin
;
75 char original_file_name
[MAX_NAME_SIZE
];
76 char encoded_file_name
[MAX_NAME_SIZE
];
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
];
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
;
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");
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 */
118 info
.version
= MPEG_AUDIO_ID
; /* Default: MPEG-1 */
120 programName
= argv
[0];
121 if (argc
== 1) /* no command-line args */
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
;
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
]);
153 fprintf (stderr
, "Fractional number of slots, padding required\n");
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
);
167 win_buf
[0] = &buffer
[0][0];
168 win_buf
[1] = &buffer
[1][0];
171 if (slot_lag
> (frac_SpF
- 1.0))
173 slot_lag
-= frac_SpF
;
176 /* fprintf(stderr,"No padding for this frame\n"); */
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 */
207 psycho_one (buffer
, max_sc
, ltmin
, &fr_ps
);
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
.
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
++)
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
);
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",
272 cleanup_psycho_two();
284 /************************************************************************
288 * PURPOSE: Writes command line syntax to the file specified by #stderr#
290 ************************************************************************/
293 usage () /* print syntax & exit */
299 " %s [-m mode][-p psy][-s sfrq][-b br][-d emp]\n",
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",
305 fprintf (stdout
, " -p psy psychoacoustic model 1/2 (dflt %4u)\n",
307 fprintf (stdout
, " -s sfrq input smpl rate in kHz (dflt %4.1f)\n",
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",
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");
317 " outBS output bit stream of encoded audio (dflt inName+%s)\n",
322 /************************************************************************
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
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 ************************************************************************/
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
])
356 layer
*info
= fr_ps
->header
;
358 IFF_AIFF pcm_aiff_data
;
362 /* preset defaults */
369 info
->mode
= MPG_MD_STEREO
;
373 info
->mode
= MPG_MD_DUAL_CHANNEL
;
377 info
->mode
= MPG_MD_JOINT_STEREO
;
380 info
->mode
= MPG_MD_MONO
;
384 fprintf (stderr
, "%s: Bad mode dflt %c\n", programName
, DFLT_MOD
);
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
);
394 info
->bitrate_index
= 14;
408 fprintf (stderr
, "%s: Bad emph dflt %c\n", programName
, DFLT_EMP
);
413 info
->error_protection
= FALSE
;
416 while (++i
< argc
&& err
== 0)
418 char c
, *token
, *arg
, *nextArg
;
425 nextArg
= argv
[i
+ 1];
438 while ((c
= *token
++))
440 if (*token
/* NumericQ(token) */ )
457 info
->mode
= MPG_MD_STEREO
;
460 else if (*arg
== 'd')
462 info
->mode
= MPG_MD_DUAL_CHANNEL
;
465 else if (*arg
== 'j')
467 info
->mode
= MPG_MD_JOINT_STEREO
;
469 else if (*arg
== 'm')
471 info
->mode
= MPG_MD_MONO
;
476 fprintf (stderr
, "%s: -m mode must be s/d/j/m not %s\n",
484 if (*psy
< 1 || *psy
> 2)
487 "%s: -p model must be 1 or 2, not %s\n",
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)
511 else if (*arg
== '5')
513 else if (*arg
== 'c')
517 fprintf (stderr
, "%s: -d emp must be n/5/c not %s\n",
529 info
->error_protection
= TRUE
;
532 fprintf (stderr
, "%s: unrec option %c\n", programName
, c
);
539 token
= ""; /* no more from token */
541 ++i
; /* skip arg we used */
549 if (inPath
[0] == '\0' && !*use_stdin
)
550 strcpy (inPath
, argv
[i
]);
551 else if (outPath
[0] == '\0')
552 strcpy (outPath
, argv
[i
]);
555 fprintf (stderr
, "%s: excess arg %s\n", programName
, argv
[i
]);
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
);
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",
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
;
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",
616 /* Declare sound file to have "infinite" number of samples. */
617 *num_samples
= MAX_U_32_NUM
;
621 brate
= bitrate
[info
->version
][14];
622 if ((info
->bitrate_index
= BitrateIndex (brate
, info
->version
)) < 0)
624 if (err
|| (!*use_stdin
&& inPath
[0] == '\0'))
625 usage (); /* never returns */
629 /************************************************************************
633 * PURPOSE: Prints the encoding parameters used
635 ************************************************************************/
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
);
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 /************************************************************************
664 * PURPOSE: Checks AIFF header information to make sure it is valid.
667 ************************************************************************/
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
);
678 if (SmpFrqIndex ((long) pcm_aiff_data
->sampleRate
, version
) < 0)
680 fprintf (stderr
, "in \"%s\".\n", file_name
);
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
);
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",
699 if (pcm_aiff_data
->blkAlgn
.blockSize
!= 0)
701 fprintf (stderr
, "Block size is not %d bytes in \"%s\".\n", 0,
706 if (pcm_aiff_data
->blkAlgn
.offset
!= 0)
708 fprintf (stderr
, "Block offset is not %d bytes in \"%s\".\n", 0,