8 #include "audio_read.h"
12 #include "psycho_n1.h"
19 #include "availbits.h"
21 #include "encode_new.h"
29 char toolameversion
[10] = "0.2l";
31 // Input buffer management. This is not reentrant but neither is toolame.
33 pthread_mutex_t toolame_input_lock
;
34 pthread_mutex_t toolame_output_lock
;
35 char *toolame_buffer
= 0;
36 int toolame_buffer_bytes
= 0;
37 int toolame_error
= 0;
40 // functions for library access
42 void toolame_init_buffers()
44 pthread_mutex_init(&toolame_input_lock
, 0);
45 pthread_mutex_init(&toolame_output_lock
, 0);
46 pthread_mutex_lock(&toolame_input_lock
);
47 if(!toolame_buffer
) toolame_buffer
= malloc(TOOLAME_BUFFER_BYTES
);
48 toolame_buffer_bytes
= 0;
53 int toolame_send_buffer(char *data
, int bytes
)
56 if(bytes
> TOOLAME_BUFFER_BYTES
)
59 "toolame_send_buffer: bytes %d exceed maximum bytes %d\n",
61 TOOLAME_BUFFER_BYTES
);
64 if(toolame_error
) return 1;
68 // pthread_mutex_lock(&toolame_output_lock);
70 pthread_mutex_unlock(&toolame_input_lock
);
76 pthread_mutex_lock(&toolame_output_lock
);
79 pthread_mutex_unlock(&toolame_input_lock
);
83 if(toolame_buffer_bytes
< TOOLAME_BUFFER_BYTES
- bytes
)
85 memcpy(toolame_buffer
+ toolame_buffer_bytes
, data
, bytes
);
86 toolame_buffer_bytes
+= bytes
;
89 pthread_mutex_unlock(&toolame_input_lock
);
95 int toolame_buffer_read(char *dst
, int size
, int n
)
100 //printf("toolame_buffer_read 1 %d %d %d\n", got_it, toolame_eof, toolame_error);
101 while(!got_it
&& !toolame_eof
&& !toolame_error
)
103 //printf("toolame_buffer_read 2\n");
104 pthread_mutex_lock(&toolame_input_lock
);
105 //printf("toolame_buffer_read 3\n");
106 if(toolame_eof
|| toolame_buffer_bytes
>= size
* n
)
109 pthread_mutex_unlock(&toolame_output_lock
);
112 if(result
> toolame_buffer_bytes
)
113 result
= toolame_buffer_bytes
;
114 memcpy(dst
, toolame_buffer
, result
);
116 if(size
* n
> result
)
117 bzero(dst
+ result
, size
* n
- result
);
118 memcpy(toolame_buffer
, toolame_buffer
+ result
, toolame_buffer_bytes
- result
);
119 toolame_buffer_bytes
-= result
;
120 pthread_mutex_unlock(&toolame_output_lock
);
122 //printf("toolame_buffer_read 100 %d\n", result);
140 void global_init (void)
142 glopts
.usepsy
= TRUE
;
143 glopts
.usepadbit
= TRUE
;
144 glopts
.quickmode
= FALSE
;
145 glopts
.quickcount
= 10;
146 glopts
.downmix
= FALSE
;
147 glopts
.byteswap
= FALSE
;
148 glopts
.channelswap
= FALSE
;
152 glopts
.verbosity
= 2;
155 /************************************************************************
159 * PURPOSE: MPEG II Encoder with
160 * psychoacoustic models 1 (MUSICAM) and 2 (AT&T)
162 * SEMANTICS: One overlapping frame of audio of up to 2 channels are
163 * processed at a time in the following order:
164 * (associated routines are in parentheses)
166 * 1. Filter sliding window of data to get 32 subband
167 * samples per channel.
168 * (window_subband,filter_subband)
170 * 2. If joint stereo mode, combine left and right channels
171 * for subbands above #jsbound#.
174 * 3. Calculate scalefactors for the frame, and
175 * also calculate scalefactor select information.
176 * (*_scale_factor_calc)
178 * 4. Calculate psychoacoustic masking levels using selected
179 * psychoacoustic model.
180 * (psycho_i, psycho_ii)
182 * 5. Perform iterative bit allocation for subbands with low
183 * mask_to_noise ratios using masking levels from step 4.
184 * (*_main_bit_allocation)
186 * 6. If error protection flag is active, add redundancy for
190 * 7. Pack bit allocation, scalefactors, and scalefactor select
191 *headerrmation onto bitstream.
192 * (*_encode_bit_alloc,*_encode_scale,transmission_pattern)
194 * 8. Quantize subbands and pack them into bitstream
195 * (*_subband_quantization, *_sample_encoding)
197 ************************************************************************/
201 int toolame (int argc
, char **argv
)
203 typedef double SBS
[2][3][SCALE_BLOCK
][SBLIMIT
];
205 typedef double JSBS
[3][SCALE_BLOCK
][SBLIMIT
];
207 typedef double IN
[2][HAN_SIZE
];
209 typedef unsigned int SUB
[2][3][SCALE_BLOCK
][SBLIMIT
];
214 char original_file_name
[MAX_NAME_SIZE
];
215 char encoded_file_name
[MAX_NAME_SIZE
];
217 static short buffer
[2][1152];
218 static unsigned int bit_alloc
[2][SBLIMIT
], scfsi
[2][SBLIMIT
];
219 static unsigned int scalar
[2][3][SBLIMIT
], j_scale
[3][SBLIMIT
];
220 static double smr
[2][SBLIMIT
], lgmin
[2][SBLIMIT
], max_sc
[2][SBLIMIT
];
222 short sam
[2][1344]; /* was [1056]; */
223 int model
, nch
, error_protection
;
224 static unsigned int crc
;
226 unsigned long frameBits
, sentBits
= 0;
227 unsigned long num_samples
;
231 /* Used to keep the SNR values for the fast/quick psy models */
232 static FLOAT smrdef
[2][32];
234 static int psycount
= 0;
237 sb_sample
= (SBS
*) mem_alloc (sizeof (SBS
), "sb_sample");
238 j_sample
= (JSBS
*) mem_alloc (sizeof (JSBS
), "j_sample");
239 win_que
= (IN
*) mem_alloc (sizeof (IN
), "Win_que");
240 subband
= (SUB
*) mem_alloc (sizeof (SUB
), "subband");
241 win_buf
= (short **) mem_alloc (sizeof (short *) * 2, "win_buf");
244 memset ((char *) buffer
, 0, sizeof (buffer
));
245 memset ((char *) bit_alloc
, 0, sizeof (bit_alloc
));
246 memset ((char *) scalar
, 0, sizeof (scalar
));
247 memset ((char *) j_scale
, 0, sizeof (j_scale
));
248 memset ((char *) scfsi
, 0, sizeof (scfsi
));
249 memset ((char *) smr
, 0, sizeof (smr
));
250 memset ((char *) lgmin
, 0, sizeof (lgmin
));
251 memset ((char *) max_sc
, 0, sizeof (max_sc
));
252 //memset ((char *) snr32, 0, sizeof (snr32));
253 memset ((char *) sam
, 0, sizeof (sam
));
257 header
.extension
= 0;
258 frame
.header
= &header
;
259 frame
.tab_num
= -1; /* no table loaded */
261 header
.version
= MPEG_AUDIO_ID
; /* Default: MPEG-1 */
263 programName
= argv
[0];
264 if (argc
== 1) /* no command-line args */
267 parse_args (argc
, argv
, &frame
, &model
, &num_samples
, original_file_name
,
269 print_config (&frame
, &model
, original_file_name
, encoded_file_name
);
271 /* this will load the alloc tables and do some other stuff */
272 hdr_to_frps (&frame
);
274 error_protection
= header
.error_protection
;
276 while (get_audio (musicin
, buffer
, num_samples
, nch
, &header
) > 0) {
277 if (glopts
.verbosity
> 1)
278 if (++frameNum
% 10 == 0)
279 fprintf (stderr
, "[%4u]\r", frameNum
);
281 win_buf
[0] = &buffer
[0][0];
282 win_buf
[1] = &buffer
[1][0];
284 adb
= available_bits (&header
, &glopts
);
286 if (header
.dab_extension
) {
287 /* in 24 kHz we always have 4 bytes */
288 if (header
.sampling_frequency
== 1)
289 header
.dab_extension
= 4;
290 /* You must have one frame in memory if you are in DAB mode */
291 /* in conformity of the norme ETS 300 401 http://www.etsi.org */
292 /* see bitstream.c */
294 minimum
= lg_frame
+ MINIMUM
;
295 adb
-= header
.dab_extension
* 8 + header
.dab_length
* 8 + 16;
300 /* New polyphase filter
301 Combines windowing and filtering. Ricardo Feb'03 */
302 for( gr
= 0; gr
< 3; gr
++ )
303 for ( bl
= 0; bl
< 12; bl
++ )
304 for ( ch
= 0; ch
< nch
; ch
++ )
305 WindowFilterSubband( &buffer
[ch
][gr
* 12 * 32 + 32 * bl
], ch
,
306 &(*sb_sample
)[ch
][gr
][bl
][0] );
311 /* Old code. left here for reference */
313 for (gr
= 0; gr
< 3; gr
++)
314 for (bl
= 0; bl
< SCALE_BLOCK
; bl
++)
315 for (ch
= 0; ch
< nch
; ch
++) {
316 window_subband (&win_buf
[ch
], &(*win_que
)[ch
][0], ch
);
317 filter_subband (&(*win_que
)[ch
][0], &(*sb_sample
)[ch
][gr
][bl
][0]);
324 scalefactor_calc_new(*sb_sample
, scalar
, nch
, frame
.sblimit
);
325 find_sf_max (scalar
, &frame
, max_sc
);
326 if (frame
.actual_mode
== MPG_MD_JOINT_STEREO
) {
327 /* this way we calculate more mono than we need */
328 /* but it is cheap */
329 combine_LR_new (*sb_sample
, *j_sample
, frame
.sblimit
);
330 scalefactor_calc_new (j_sample
, &j_scale
, 1, frame
.sblimit
);
333 scale_factor_calc (*sb_sample
, scalar
, nch
, frame
.sblimit
);
334 pick_scale (scalar
, &frame
, max_sc
);
335 if (frame
.actual_mode
== MPG_MD_JOINT_STEREO
) {
336 /* this way we calculate more mono than we need */
337 /* but it is cheap */
338 combine_LR (*sb_sample
, *j_sample
, frame
.sblimit
);
339 scale_factor_calc (j_sample
, &j_scale
, 1, frame
.sblimit
);
345 if ((glopts
.quickmode
== TRUE
) && (++psycount
% glopts
.quickcount
!= 0)) {
346 /* We're using quick mode, so we're only calculating the model every
347 'quickcount' frames. Otherwise, just copy the old ones across */
348 for (ch
= 0; ch
< nch
; ch
++) {
349 for (sb
= 0; sb
< SBLIMIT
; sb
++)
350 smr
[ch
][sb
] = smrdef
[ch
][sb
];
353 /* calculate the psymodel */
356 psycho_n1 (smr
, nch
);
358 case 0: /* Psy Model A */
359 psycho_0 (smr
, nch
, scalar
, (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] * 1000);
362 psycho_1 (buffer
, max_sc
, smr
, &frame
);
365 for (ch
= 0; ch
< nch
; ch
++) {
366 psycho_2 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], //snr32,
367 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
372 /* Modified psy model 1 */
373 psycho_3 (buffer
, max_sc
, smr
, &frame
, &glopts
);
376 /* Modified Psycho Model 2 */
377 for (ch
= 0; ch
< nch
; ch
++) {
378 psycho_4 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], // snr32,
379 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
384 /* Model 5 comparse model 1 and 3 */
385 psycho_1 (buffer
, max_sc
, smr
, &frame
);
386 fprintf(stdout
,"1 ");
388 psycho_3 (buffer
, max_sc
, smr
, &frame
, &glopts
);
389 fprintf(stdout
,"3 ");
393 /* Model 6 compares model 2 and 4 */
394 for (ch
= 0; ch
< nch
; ch
++)
395 psycho_2 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], //snr32,
396 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
398 fprintf(stdout
,"2 ");
400 for (ch
= 0; ch
< nch
; ch
++)
401 psycho_4 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], // snr32,
402 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
404 fprintf(stdout
,"4 ");
408 fprintf(stdout
,"Frame: %i\n",frameNum
);
409 /* Dump the SMRs for all models */
410 psycho_1 (buffer
, max_sc
, smr
, &frame
);
413 psycho_3 (buffer
, max_sc
, smr
, &frame
, &glopts
);
416 for (ch
= 0; ch
< nch
; ch
++)
417 psycho_2 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], //snr32,
418 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
422 for (ch
= 0; ch
< nch
; ch
++)
423 psycho_4 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], // snr32,
424 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
430 /* Compare 0 and 4 */
431 psycho_n1 (smr
, nch
);
435 for (ch
= 0; ch
< nch
; ch
++)
436 psycho_4 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], // snr32,
437 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
443 fprintf (stderr
, "Invalid psy model specification: %i\n", model
);
445 pthread_mutex_unlock(&toolame_output_lock
);
450 if (glopts
.quickmode
== TRUE
)
451 /* copy the smr values and reuse them later */
452 for (ch
= 0; ch
< nch
; ch
++) {
453 for (sb
= 0; sb
< SBLIMIT
; sb
++)
454 smrdef
[ch
][sb
] = smr
[ch
][sb
];
457 if (glopts
.verbosity
> 4)
466 sf_transmission_pattern (scalar
, scfsi
, &frame
);
467 main_bit_allocation_new (smr
, scfsi
, bit_alloc
, &adb
, &frame
, &glopts
);
468 //main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
470 if (error_protection
)
471 CRC_calc (&frame
, bit_alloc
, scfsi
, &crc
);
473 write_header (&frame
, &bs
);
474 //encode_info (&frame, &bs);
475 if (error_protection
)
476 putbits (&bs
, crc
, 16);
477 write_bit_alloc (bit_alloc
, &frame
, &bs
);
478 //encode_bit_alloc (bit_alloc, &frame, &bs);
479 write_scalefactors(bit_alloc
, scfsi
, scalar
, &frame
, &bs
);
480 //encode_scale (bit_alloc, scfsi, scalar, &frame, &bs);
481 subband_quantization_new (scalar
, *sb_sample
, j_scale
, *j_sample
, bit_alloc
,
483 //subband_quantization (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
484 // *subband, &frame);
485 write_samples_new(*subband
, bit_alloc
, &frame
, &bs
);
486 //sample_encoding (*subband, bit_alloc, &frame, &bs);
488 transmission_pattern (scalar
, scfsi
, &frame
);
489 main_bit_allocation (smr
, scfsi
, bit_alloc
, &adb
, &frame
, &glopts
);
490 if (error_protection
)
491 CRC_calc (&frame
, bit_alloc
, scfsi
, &crc
);
492 encode_info (&frame
, &bs
);
493 if (error_protection
)
494 encode_CRC (crc
, &bs
);
495 encode_bit_alloc (bit_alloc
, &frame
, &bs
);
496 encode_scale (bit_alloc
, scfsi
, scalar
, &frame
, &bs
);
497 subband_quantization (scalar
, *sb_sample
, j_scale
, *j_sample
, bit_alloc
,
499 sample_encoding (*subband
, bit_alloc
, &frame
, &bs
);
503 /* If not all the bits were used, write out a stack of zeros */
504 for (i
= 0; i
< adb
; i
++)
506 if (header
.dab_extension
) {
507 /* Reserve some bytes for X-PAD in DAB mode */
508 putbits (&bs
, 0, header
.dab_length
* 8);
510 for (i
= header
.dab_extension
- 1; i
>= 0; i
--) {
511 CRC_calcDAB (&frame
, bit_alloc
, scfsi
, scalar
, &crc
, i
);
512 /* this crc is for the previous frame in DAB mode */
513 if (bs
.buf_byte_idx
+ lg_frame
< bs
.buf_size
)
514 bs
.buf
[bs
.buf_byte_idx
+ lg_frame
] = crc
;
515 /* reserved 2 bytes for F-PAD in DAB mode */
516 putbits (&bs
, crc
, 8);
518 putbits (&bs
, 0, 16);
521 frameBits
= sstell (&bs
) - sentBits
;
523 if (frameBits
% 8) { /* a program failure */
524 fprintf (stderr
, "Sent %ld bits = %ld slots plus %ld\n", frameBits
,
525 frameBits
/ 8, frameBits
% 8);
526 fprintf (stderr
, "If you are reading this, the program is broken\n");
527 fprintf (stderr
, "email [mfc at NOTplanckenerg.com] without the NOT\n");
528 fprintf (stderr
, "with the command line arguments and other info\n");
530 pthread_mutex_unlock(&toolame_output_lock
);
535 sentBits
+= frameBits
;
538 close_bit_stream_w (&bs
);
540 if ((glopts
.verbosity
> 1) && (glopts
.vbr
== TRUE
)) {
543 extern int vbrstats_new
[15];
545 extern int vbrstats
[15];
547 fprintf (stdout
, "VBR stats:\n");
548 for (i
= 1; i
< 15; i
++)
549 fprintf (stdout
, "%4i ", bitrate
[header
.version
][i
]);
550 fprintf (stdout
, "\n");
551 for (i
= 1; i
< 15; i
++)
553 fprintf (stdout
,"%4i ",vbrstats_new
[i
]);
555 fprintf (stdout
, "%4i ", vbrstats
[i
]);
557 fprintf (stdout
, "\n");
561 "Avg slots/frame = %.3f; b/smp = %.2f; bitrate = %.3f kbps\n",
562 (FLOAT
) sentBits
/ (frameNum
* 8),
563 (FLOAT
) sentBits
/ (frameNum
* 1152),
564 (FLOAT
) sentBits
/ (frameNum
* 1152) *
565 s_freq
[header
.version
][header
.sampling_frequency
]);
567 pthread_mutex_unlock(&toolame_output_lock
);
569 // if (fclose (musicin) != 0) {
570 // fprintf (stderr, "Could not close \"%s\".\n", original_file_name);
574 fprintf (stderr
, "\nDone\n");
579 /************************************************************************
583 * PURPOSE: Prints the encoding parameters used
585 ************************************************************************/
587 void print_config (frame_info
* frame
, int *psy
, char *inPath
,
590 frame_header
*header
= frame
->header
;
592 if (glopts
.verbosity
== 0)
595 fprintf (stderr
, "--------------------------------------------\n");
596 fprintf (stderr
, "Input File : '%s' %.1f kHz\n",
597 (strcmp (inPath
, "-") ? inPath
: "stdin"),
598 s_freq
[header
->version
][header
->sampling_frequency
]);
599 fprintf (stderr
, "Output File: '%s'\n",
600 (strcmp (outPath
, "-") ? outPath
: "stdout"));
601 fprintf (stderr
, "%d kbps ", bitrate
[header
->version
][header
->bitrate_index
]);
602 fprintf (stderr
, "%s ", version_names
[header
->version
]);
603 if (header
->mode
!= MPG_MD_JOINT_STEREO
)
604 fprintf (stderr
, "Layer II %s Psycho model=%d (Mode_Extension=%d)\n",
605 mode_names
[header
->mode
], *psy
, header
->mode_ext
);
607 fprintf (stderr
, "Layer II %s Psy model %d \n", mode_names
[header
->mode
],
610 fprintf (stderr
, "[De-emph:%s\tCopyright:%s\tOriginal:%s\tCRC:%s]\n",
611 ((header
->emphasis
) ? "On" : "Off"),
612 ((header
->copyright
) ? "Yes" : "No"),
613 ((header
->original
) ? "Yes" : "No"),
614 ((header
->error_protection
) ? "On" : "Off"));
616 fprintf (stderr
, "[Padding:%s\tByte-swap:%s\tChanswap:%s\tDAB:%s]\n",
617 ((glopts
.usepadbit
) ? "Normal" : "Off"),
618 ((glopts
.byteswap
) ? "On" : "Off"),
619 ((glopts
.channelswap
) ? "On" : "Off"),
620 ((glopts
.dab
) ? "On" : "Off"));
622 if (glopts
.vbr
== TRUE
)
623 fprintf (stderr
, "VBR Enabled. Using MNR boost of %f\n", glopts
.vbrlevel
);
624 fprintf(stderr
,"ATH adjustment %f\n",glopts
.athlevel
);
626 fprintf (stderr
, "--------------------------------------------\n");
630 /************************************************************************
634 * PURPOSE: Writes command line syntax to the file specified by #stderr#
636 ************************************************************************/
639 { /* print syntax & exit */
640 /* FIXME: maybe have an option to display better definitions of help codes, and
641 long equivalents of the flags */
642 fprintf (stdout
, "\ntooLAME version %s (http://toolame.sourceforge.net)\n",
644 fprintf (stdout
, "MPEG Audio Layer II encoder\n\n");
645 fprintf (stdout
, "usage: \n");
646 fprintf (stdout
, "\t%s [options] <input> <output>\n\n", programName
);
648 fprintf (stdout
, "Options:\n");
649 fprintf (stdout
, "Input\n");
650 fprintf (stdout
, "\t-s sfrq input smpl rate in kHz (dflt %4.1f)\n",
652 fprintf (stdout
, "\t-a downmix from stereo to mono\n");
653 fprintf (stdout
, "\t-x force byte-swapping of input\n");
654 fprintf (stdout
, "\t-g swap channels of input file\n");
655 fprintf (stdout
, "Output\n");
656 fprintf (stdout
, "\t-m mode channel mode : s/d/j/m (dflt %4c)\n",
658 fprintf (stdout
, "\t-p psy psychoacoustic model 0/1/2/3 (dflt %4u)\n",
660 fprintf (stdout
, "\t-b br total bitrate in kbps (dflt 192)\n");
661 fprintf (stdout
, "\t-v lev vbr mode\n");
662 fprintf (stdout
, "\t-l lev ATH level (dflt 0)\n");
663 fprintf (stdout
, "Operation\n");
664 // fprintf (stdout, "\t-f fast mode (turns off psy model)\n");
665 // deprecate the -f switch. use "-p 0" instead.
667 "\t-q num quick mode. only calculate psy model every num frames\n");
668 fprintf (stdout
, "Misc\n");
669 fprintf (stdout
, "\t-d emp de-emphasis n/5/c (dflt %4c)\n",
671 fprintf (stdout
, "\t-c mark as copyright\n");
672 fprintf (stdout
, "\t-o mark as original\n");
673 fprintf (stdout
, "\t-e add error protection\n");
674 fprintf (stdout
, "\t-r force padding bit/frame off\n");
675 fprintf (stdout
, "\t-D len add DAB extensions of length [len]\n");
676 fprintf (stdout
, "\t-t talkativity 0=no messages (dflt 2)");
677 fprintf (stdout
, "Files\n");
679 "\tinput input sound file. (WAV,AIFF,PCM or use '/dev/stdin')\n");
680 fprintf (stdout
, "\toutput output bit stream of encoded audio\n");
682 "\n\tAllowable bitrates for 16, 22.05 and 24kHz sample input\n");
684 "\t8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160\n");
686 "\n\tAllowable bitrates for 32, 44.1 and 48kHz sample input\n");
688 "\t32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384\n");
692 /*********************************************
693 * void short_usage(void)
694 ********************************************/
695 void short_usage (void)
697 /* print a bit of info about the program */
698 fprintf (stderr
, "tooLAME version %s\n (http://toolame.sourceforge.net)\n",
700 fprintf (stderr
, "MPEG Audio Layer II encoder\n\n");
701 fprintf (stderr
, "USAGE: %s [options] <infile> [outfile]\n\n", programName
);
702 fprintf (stderr
, "Try \"%s -h\" for more information.\n", programName
);
706 /*********************************************
707 * void proginfo(void)
708 ********************************************/
711 /* print a bit of info about the program */
713 "\ntooLAME version 0.2g (http://toolame.sourceforge.net)\n");
714 fprintf (stderr
, "MPEG Audio Layer II encoder\n\n");
717 /************************************************************************
721 * PURPOSE: Sets encoding parameters to the specifications of the
722 * command line. Default settings are used for parameters
723 * not specified in the command line.
725 * SEMANTICS: The command line is parsed according to the following
728 * -m is followed by the mode
729 * -p is followed by the psychoacoustic model number
730 * -s is followed by the sampling rate
731 * -b is followed by the total bitrate, irrespective of the mode
732 * -d is followed by the emphasis flag
733 * -c is followed by the copyright/no_copyright flag
734 * -o is followed by the original/not_original flag
735 * -e is followed by the error_protection on/off flag
736 * -f turns off psy model (fast mode)
737 * -q <i> only calculate psy model every ith frame
738 * -a downmix from stereo to mono
739 * -r turn off padding bits in frames.
740 * -x force byte swapping of input
741 * -g swap the channels on an input file
742 * -t talkativity. how verbose should the program be. 0 = no messages.
744 * If the input file is in AIFF format, the sampling frequency is read
745 * from the AIFF header.
747 * The input and output filenames are read into #inpath# and #outpath#.
749 ************************************************************************/
751 void parse_args (int argc
, char **argv
, frame_info
* frame
, int *psy
,
752 unsigned long *num_samples
, char inPath
[MAX_NAME_SIZE
],
753 char outPath
[MAX_NAME_SIZE
])
757 frame_header
*header
= frame
->header
;
761 /* preset defaults */
764 header
->lay
= DFLT_LAY
;
767 header
->mode
= MPG_MD_STEREO
;
768 header
->mode_ext
= 0;
771 header
->mode
= MPG_MD_DUAL_CHANNEL
;
772 header
->mode_ext
= 0;
774 /* in j-stereo mode, no default header->mode_ext was defined, gave error..
775 now default = 2 added by MFC 14 Dec 1999. */
777 header
->mode
= MPG_MD_JOINT_STEREO
;
778 header
->mode_ext
= 2;
781 header
->mode
= MPG_MD_MONO
;
782 header
->mode_ext
= 0;
785 fprintf (stderr
, "%s: Bad mode dflt %c\n", programName
, DFLT_MOD
);
789 if ((header
->sampling_frequency
=
790 toolame_SmpFrqIndex ((long) (1000 * DFLT_SFQ
), &header
->version
)) < 0) {
791 fprintf (stderr
, "%s: bad sfrq default %.2f\n", programName
, DFLT_SFQ
);
794 header
->bitrate_index
= 14;
798 header
->emphasis
= 0;
801 header
->emphasis
= 1;
804 header
->emphasis
= 3;
807 fprintf (stderr
, "%s: Bad emph dflt %c\n", programName
, DFLT_EMP
);
810 header
->copyright
= 0;
811 header
->original
= 0;
812 header
->error_protection
= FALSE
;
813 header
->dab_extension
= 0;
816 while (++i
< argc
&& err
== 0) {
817 char c
, *token
, *arg
, *nextArg
;
821 if (*token
++ == '-') {
823 nextArg
= argv
[i
+ 1];
828 /* The user wants to use stdin and/or stdout. */
829 if (inPath
[0] == '\0')
830 strncpy (inPath
, argv
[i
], MAX_NAME_SIZE
);
831 else if (outPath
[0] == '\0')
832 strncpy (outPath
, argv
[i
], MAX_NAME_SIZE
);
834 while ((c
= *token
++)) {
835 if (*token
/* NumericQ(token) */ )
843 header
->mode
= MPG_MD_STEREO
;
844 header
->mode_ext
= 0;
845 } else if (*arg
== 'd') {
846 header
->mode
= MPG_MD_DUAL_CHANNEL
;
847 header
->mode_ext
= 0;
848 } else if (*arg
== 'j') {
849 header
->mode
= MPG_MD_JOINT_STEREO
;
850 } else if (*arg
== 'm') {
851 header
->mode
= MPG_MD_MONO
;
852 header
->mode_ext
= 0;
854 fprintf (stderr
, "%s: -m mode must be s/d/j/m not %s\n",
867 /* samplerate = rint( 1000.0 * srate ); $A */
868 samplerate
= (long) ((1000.0 * srate
) + 0.5);
869 if ((header
->sampling_frequency
=
870 toolame_SmpFrqIndex ((long) samplerate
, &header
->version
)) < 0)
881 header
->emphasis
= 0;
882 else if (*arg
== '5')
883 header
->emphasis
= 1;
884 else if (*arg
== 'c')
885 header
->emphasis
= 3;
887 fprintf (stderr
, "%s: -d emp must be n/5/c not %s\n", programName
,
894 header
->dab_length
= atoi (arg
);
895 header
->error_protection
= TRUE
;
896 header
->dab_extension
= 2;
900 header
->copyright
= 1;
903 header
->original
= 1;
906 header
->error_protection
= TRUE
;
910 /* this switch is deprecated? FIXME get rid of glopts.usepsy
911 instead us psymodel 0, i.e. "-p 0" */
912 glopts
.usepsy
= FALSE
;
915 glopts
.usepadbit
= FALSE
;
920 glopts
.quickmode
= TRUE
;
921 glopts
.usepsy
= TRUE
;
922 glopts
.quickcount
= atoi (arg
);
923 if (glopts
.quickcount
== 0) {
924 /* just don't use psy model */
925 glopts
.usepsy
= FALSE
;
926 glopts
.quickcount
= FALSE
;
930 glopts
.downmix
= TRUE
;
931 header
->mode
= MPG_MD_MONO
;
932 header
->mode_ext
= 0;
935 glopts
.byteswap
= TRUE
;
940 glopts
.vbrlevel
= atof (arg
);
941 glopts
.usepadbit
= FALSE
; /* don't use padding for VBR */
943 /* MFC Feb 2003: in VBR mode, joint stereo doesn't make
944 any sense at the moment, as there are no noisy subbands
945 according to bits_for_nonoise in vbr mode */
946 header
->mode
= MPG_MD_STEREO
; /* force stereo mode */
947 header
->mode_ext
= 0;
951 glopts
.athlevel
= atof(arg
);
957 glopts
.channelswap
= TRUE
;
961 glopts
.verbosity
= atoi (arg
);
964 fprintf (stderr
, "%s: unrec option %c\n", programName
, c
);
970 token
= ""; /* no more from token */
972 ++i
; /* skip arg we used */
978 if (inPath
[0] == '\0')
979 strcpy (inPath
, argv
[i
]);
980 else if (outPath
[0] == '\0')
981 strcpy (outPath
, argv
[i
]);
983 fprintf (stderr
, "%s: excess arg %s\n", programName
, argv
[i
]);
989 if (header
->dab_extension
) {
991 /* if the bit rate per channel is less then 56 kbit/s, we have 2 scf-crc */
992 /* else we have 4 scf-crc */
993 /* in 24 kHz, we have 4 scf-crc, see main loop */
994 if (brate
/ (header
->mode
== MPG_MD_MONO
? 1 : 2) >= 56)
995 header
->dab_extension
= 4;
999 if (err
|| inPath
[0] == '\0')
1000 usage (); /* If no infile defined, or err has occured, then call usage() */
1002 if (outPath
[0] == '\0') {
1003 /* replace old extension with new one, 1992-08-19, 1995-06-12 shn */
1004 new_ext (inPath
, DFLT_EXT
, outPath
);
1007 if (!strcmp (inPath
, "-")) {
1008 musicin
= stdin
; /* read from stdin */
1009 *num_samples
= MAX_U_32_NUM
;
1011 if ((musicin
= fopen (inPath
, "rb")) == NULL
) {
1012 fprintf (stderr
, "Could not find \"%s\".\n", inPath
);
1015 parse_input_file (musicin
, inPath
, header
, num_samples
);
1018 /* check for a valid bitrate */
1020 brate
= bitrate
[header
->version
][10];
1022 /* Check to see we have a sane value for the bitrate for this version */
1023 if ((header
->bitrate_index
= toolame_BitrateIndex (brate
, header
->version
)) < 0)
1026 /* All options are hunky dory, open the input audio file and
1027 return to the main drag */
1028 open_bit_stream_w (&bs
, outPath
, BUFFER_SIZE
);
1032 void smr_dump(double smr
[2][SBLIMIT
], int nch
) {
1035 fprintf(stdout
,"SMR:");
1036 for (ch
= 0;ch
<nch
; ch
++) {
1038 fprintf(stdout
," ");
1039 for (sb
=0;sb
<SBLIMIT
;sb
++)
1040 fprintf(stdout
,"%3.0f ",smr
[ch
][sb
]);
1041 fprintf(stdout
,"\n");