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 while(!got_it
&& !toolame_eof
&& !toolame_error
)
102 pthread_mutex_lock(&toolame_input_lock
);
103 // printf("toolame_buffer_read 1 size=%d n=%d eof=%d %d %d\n",
104 // size, n, toolame_eof, got_it, toolame_error);
105 if(toolame_eof
|| toolame_buffer_bytes
>= size
* n
)
108 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
,
270 print_config (&frame
, &model
, original_file_name
, encoded_file_name
);
272 /* this will load the alloc tables and do some other stuff */
273 hdr_to_frps (&frame
);
275 error_protection
= header
.error_protection
;
279 while (get_audio (musicin
, buffer
, num_samples
, nch
, &header
) > 0) {
280 if (glopts
.verbosity
> 1)
281 if (++frameNum
% 10 == 0)
282 fprintf (stderr
, "[%4u]\r", frameNum
);
284 win_buf
[0] = &buffer
[0][0];
285 win_buf
[1] = &buffer
[1][0];
287 adb
= available_bits (&header
, &glopts
);
289 if (header
.dab_extension
) {
290 /* in 24 kHz we always have 4 bytes */
291 if (header
.sampling_frequency
== 1)
292 header
.dab_extension
= 4;
293 /* You must have one frame in memory if you are in DAB mode */
294 /* in conformity of the norme ETS 300 401 http://www.etsi.org */
295 /* see bitstream.c */
297 minimum
= lg_frame
+ MINIMUM
;
298 adb
-= header
.dab_extension
* 8 + header
.dab_length
* 8 + 16;
303 /* New polyphase filter
304 Combines windowing and filtering. Ricardo Feb'03 */
305 for( gr
= 0; gr
< 3; gr
++ )
306 for ( bl
= 0; bl
< 12; bl
++ )
307 for ( ch
= 0; ch
< nch
; ch
++ )
308 WindowFilterSubband( &buffer
[ch
][gr
* 12 * 32 + 32 * bl
], ch
,
309 &(*sb_sample
)[ch
][gr
][bl
][0] );
314 /* Old code. left here for reference */
316 for (gr
= 0; gr
< 3; gr
++)
317 for (bl
= 0; bl
< SCALE_BLOCK
; bl
++)
318 for (ch
= 0; ch
< nch
; ch
++) {
319 window_subband (&win_buf
[ch
], &(*win_que
)[ch
][0], ch
);
320 filter_subband (&(*win_que
)[ch
][0], &(*sb_sample
)[ch
][gr
][bl
][0]);
327 scalefactor_calc_new(*sb_sample
, scalar
, nch
, frame
.sblimit
);
328 find_sf_max (scalar
, &frame
, max_sc
);
329 if (frame
.actual_mode
== MPG_MD_JOINT_STEREO
) {
330 /* this way we calculate more mono than we need */
331 /* but it is cheap */
332 combine_LR_new (*sb_sample
, *j_sample
, frame
.sblimit
);
333 scalefactor_calc_new (j_sample
, &j_scale
, 1, frame
.sblimit
);
336 scale_factor_calc (*sb_sample
, scalar
, nch
, frame
.sblimit
);
337 pick_scale (scalar
, &frame
, max_sc
);
338 if (frame
.actual_mode
== MPG_MD_JOINT_STEREO
) {
339 /* this way we calculate more mono than we need */
340 /* but it is cheap */
341 combine_LR (*sb_sample
, *j_sample
, frame
.sblimit
);
342 scale_factor_calc (j_sample
, &j_scale
, 1, frame
.sblimit
);
348 if ((glopts
.quickmode
== TRUE
) && (++psycount
% glopts
.quickcount
!= 0)) {
349 /* We're using quick mode, so we're only calculating the model every
350 'quickcount' frames. Otherwise, just copy the old ones across */
351 for (ch
= 0; ch
< nch
; ch
++) {
352 for (sb
= 0; sb
< SBLIMIT
; sb
++)
353 smr
[ch
][sb
] = smrdef
[ch
][sb
];
356 /* calculate the psymodel */
359 psycho_n1 (smr
, nch
);
361 case 0: /* Psy Model A */
362 psycho_0 (smr
, nch
, scalar
, (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] * 1000);
365 psycho_1 (buffer
, max_sc
, smr
, &frame
);
368 for (ch
= 0; ch
< nch
; ch
++) {
369 psycho_2 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], //snr32,
370 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
375 /* Modified psy model 1 */
376 psycho_3 (buffer
, max_sc
, smr
, &frame
, &glopts
);
379 /* Modified Psycho Model 2 */
380 for (ch
= 0; ch
< nch
; ch
++) {
381 psycho_4 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], // snr32,
382 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
387 /* Model 5 comparse model 1 and 3 */
388 psycho_1 (buffer
, max_sc
, smr
, &frame
);
389 fprintf(stdout
,"1 ");
391 psycho_3 (buffer
, max_sc
, smr
, &frame
, &glopts
);
392 fprintf(stdout
,"3 ");
396 /* Model 6 compares model 2 and 4 */
397 for (ch
= 0; ch
< nch
; ch
++)
398 psycho_2 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], //snr32,
399 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
401 fprintf(stdout
,"2 ");
403 for (ch
= 0; ch
< nch
; ch
++)
404 psycho_4 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], // snr32,
405 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
407 fprintf(stdout
,"4 ");
411 fprintf(stdout
,"Frame: %i\n",frameNum
);
412 /* Dump the SMRs for all models */
413 psycho_1 (buffer
, max_sc
, smr
, &frame
);
416 psycho_3 (buffer
, max_sc
, smr
, &frame
, &glopts
);
419 for (ch
= 0; ch
< nch
; ch
++)
420 psycho_2 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], //snr32,
421 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
425 for (ch
= 0; ch
< nch
; ch
++)
426 psycho_4 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], // snr32,
427 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
433 /* Compare 0 and 4 */
434 psycho_n1 (smr
, nch
);
438 for (ch
= 0; ch
< nch
; ch
++)
439 psycho_4 (&buffer
[ch
][0], &sam
[ch
][0], ch
, &smr
[ch
][0], // snr32,
440 (FLOAT
) s_freq
[header
.version
][header
.sampling_frequency
] *
446 fprintf (stderr
, "Invalid psy model specification: %i\n", model
);
448 pthread_mutex_unlock(&toolame_output_lock
);
453 if (glopts
.quickmode
== TRUE
)
454 /* copy the smr values and reuse them later */
455 for (ch
= 0; ch
< nch
; ch
++) {
456 for (sb
= 0; sb
< SBLIMIT
; sb
++)
457 smrdef
[ch
][sb
] = smr
[ch
][sb
];
460 if (glopts
.verbosity
> 4)
469 sf_transmission_pattern (scalar
, scfsi
, &frame
);
470 main_bit_allocation_new (smr
, scfsi
, bit_alloc
, &adb
, &frame
, &glopts
);
471 //main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
473 if (error_protection
)
474 CRC_calc (&frame
, bit_alloc
, scfsi
, &crc
);
476 write_header (&frame
, &bs
);
477 //encode_info (&frame, &bs);
478 if (error_protection
)
479 putbits (&bs
, crc
, 16);
480 write_bit_alloc (bit_alloc
, &frame
, &bs
);
481 //encode_bit_alloc (bit_alloc, &frame, &bs);
482 write_scalefactors(bit_alloc
, scfsi
, scalar
, &frame
, &bs
);
483 //encode_scale (bit_alloc, scfsi, scalar, &frame, &bs);
484 subband_quantization_new (scalar
, *sb_sample
, j_scale
, *j_sample
, bit_alloc
,
486 //subband_quantization (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
487 // *subband, &frame);
488 write_samples_new(*subband
, bit_alloc
, &frame
, &bs
);
489 //sample_encoding (*subband, bit_alloc, &frame, &bs);
491 transmission_pattern (scalar
, scfsi
, &frame
);
492 main_bit_allocation (smr
, scfsi
, bit_alloc
, &adb
, &frame
, &glopts
);
493 if (error_protection
)
494 CRC_calc (&frame
, bit_alloc
, scfsi
, &crc
);
495 encode_info (&frame
, &bs
);
496 if (error_protection
)
497 encode_CRC (crc
, &bs
);
498 encode_bit_alloc (bit_alloc
, &frame
, &bs
);
499 encode_scale (bit_alloc
, scfsi
, scalar
, &frame
, &bs
);
500 subband_quantization (scalar
, *sb_sample
, j_scale
, *j_sample
, bit_alloc
,
502 sample_encoding (*subband
, bit_alloc
, &frame
, &bs
);
506 /* If not all the bits were used, write out a stack of zeros */
507 for (i
= 0; i
< adb
; i
++)
509 if (header
.dab_extension
) {
510 /* Reserve some bytes for X-PAD in DAB mode */
511 putbits (&bs
, 0, header
.dab_length
* 8);
513 for (i
= header
.dab_extension
- 1; i
>= 0; i
--) {
514 CRC_calcDAB (&frame
, bit_alloc
, scfsi
, scalar
, &crc
, i
);
515 /* this crc is for the previous frame in DAB mode */
516 if (bs
.buf_byte_idx
+ lg_frame
< bs
.buf_size
)
517 bs
.buf
[bs
.buf_byte_idx
+ lg_frame
] = crc
;
518 /* reserved 2 bytes for F-PAD in DAB mode */
519 putbits (&bs
, crc
, 8);
521 putbits (&bs
, 0, 16);
524 frameBits
= sstell (&bs
) - sentBits
;
526 if (frameBits
% 8) { /* a program failure */
527 fprintf (stderr
, "Sent %ld bits = %ld slots plus %ld\n", frameBits
,
528 frameBits
/ 8, frameBits
% 8);
529 fprintf (stderr
, "If you are reading this, the program is broken\n");
530 fprintf (stderr
, "email [mfc at NOTplanckenerg.com] without the NOT\n");
531 fprintf (stderr
, "with the command line arguments and other info\n");
533 pthread_mutex_unlock(&toolame_output_lock
);
538 sentBits
+= frameBits
;
541 close_bit_stream_w (&bs
);
543 if ((glopts
.verbosity
> 1) && (glopts
.vbr
== TRUE
)) {
546 extern int vbrstats_new
[15];
548 extern int vbrstats
[15];
550 fprintf (stdout
, "VBR stats:\n");
551 for (i
= 1; i
< 15; i
++)
552 fprintf (stdout
, "%4i ", bitrate
[header
.version
][i
]);
553 fprintf (stdout
, "\n");
554 for (i
= 1; i
< 15; i
++)
556 fprintf (stdout
,"%4i ",vbrstats_new
[i
]);
558 fprintf (stdout
, "%4i ", vbrstats
[i
]);
560 fprintf (stdout
, "\n");
564 "Avg slots/frame = %.3f; b/smp = %.2f; bitrate = %.3f kbps\n",
565 (FLOAT
) sentBits
/ (frameNum
* 8),
566 (FLOAT
) sentBits
/ (frameNum
* 1152),
567 (FLOAT
) sentBits
/ (frameNum
* 1152) *
568 s_freq
[header
.version
][header
.sampling_frequency
]);
570 pthread_mutex_unlock(&toolame_output_lock
);
572 // if (fclose (musicin) != 0) {
573 // fprintf (stderr, "Could not close \"%s\".\n", original_file_name);
577 fprintf (stderr
, "\nDone\n");
582 /************************************************************************
586 * PURPOSE: Prints the encoding parameters used
588 ************************************************************************/
590 void print_config (frame_info
* frame
, int *psy
, char *inPath
,
593 frame_header
*header
= frame
->header
;
595 if (glopts
.verbosity
== 0)
598 fprintf (stderr
, "--------------------------------------------\n");
599 fprintf (stderr
, "Input File : '%s' %.1f kHz\n",
600 (strcmp (inPath
, "-") ? inPath
: "stdin"),
601 s_freq
[header
->version
][header
->sampling_frequency
]);
602 fprintf (stderr
, "Output File: '%s'\n",
603 (strcmp (outPath
, "-") ? outPath
: "stdout"));
604 fprintf (stderr
, "%d kbps ", bitrate
[header
->version
][header
->bitrate_index
]);
605 fprintf (stderr
, "%s ", version_names
[header
->version
]);
606 if (header
->mode
!= MPG_MD_JOINT_STEREO
)
607 fprintf (stderr
, "Layer II %s Psycho model=%d (Mode_Extension=%d)\n",
608 mode_names
[header
->mode
], *psy
, header
->mode_ext
);
610 fprintf (stderr
, "Layer II %s Psy model %d \n", mode_names
[header
->mode
],
613 fprintf (stderr
, "[De-emph:%s\tCopyright:%s\tOriginal:%s\tCRC:%s]\n",
614 ((header
->emphasis
) ? "On" : "Off"),
615 ((header
->copyright
) ? "Yes" : "No"),
616 ((header
->original
) ? "Yes" : "No"),
617 ((header
->error_protection
) ? "On" : "Off"));
619 fprintf (stderr
, "[Padding:%s\tByte-swap:%s\tChanswap:%s\tDAB:%s]\n",
620 ((glopts
.usepadbit
) ? "Normal" : "Off"),
621 ((glopts
.byteswap
) ? "On" : "Off"),
622 ((glopts
.channelswap
) ? "On" : "Off"),
623 ((glopts
.dab
) ? "On" : "Off"));
625 if (glopts
.vbr
== TRUE
)
626 fprintf (stderr
, "VBR Enabled. Using MNR boost of %f\n", glopts
.vbrlevel
);
627 fprintf(stderr
,"ATH adjustment %f\n",glopts
.athlevel
);
629 fprintf (stderr
, "--------------------------------------------\n");
633 /************************************************************************
637 * PURPOSE: Writes command line syntax to the file specified by #stderr#
639 ************************************************************************/
642 { /* print syntax & exit */
643 /* FIXME: maybe have an option to display better definitions of help codes, and
644 long equivalents of the flags */
645 fprintf (stdout
, "\ntooLAME version %s (http://toolame.sourceforge.net)\n",
647 fprintf (stdout
, "MPEG Audio Layer II encoder\n\n");
648 fprintf (stdout
, "usage: \n");
649 fprintf (stdout
, "\t%s [options] <input> <output>\n\n", programName
);
651 fprintf (stdout
, "Options:\n");
652 fprintf (stdout
, "Input\n");
653 fprintf (stdout
, "\t-s sfrq input smpl rate in kHz (dflt %4.1f)\n",
655 fprintf (stdout
, "\t-a downmix from stereo to mono\n");
656 fprintf (stdout
, "\t-x force byte-swapping of input\n");
657 fprintf (stdout
, "\t-g swap channels of input file\n");
658 fprintf (stdout
, "Output\n");
659 fprintf (stdout
, "\t-m mode channel mode : s/d/j/m (dflt %4c)\n",
661 fprintf (stdout
, "\t-p psy psychoacoustic model 0/1/2/3 (dflt %4u)\n",
663 fprintf (stdout
, "\t-b br total bitrate in kbps (dflt 192)\n");
664 fprintf (stdout
, "\t-v lev vbr mode\n");
665 fprintf (stdout
, "\t-l lev ATH level (dflt 0)\n");
666 fprintf (stdout
, "Operation\n");
667 // fprintf (stdout, "\t-f fast mode (turns off psy model)\n");
668 // deprecate the -f switch. use "-p 0" instead.
670 "\t-q num quick mode. only calculate psy model every num frames\n");
671 fprintf (stdout
, "Misc\n");
672 fprintf (stdout
, "\t-d emp de-emphasis n/5/c (dflt %4c)\n",
674 fprintf (stdout
, "\t-c mark as copyright\n");
675 fprintf (stdout
, "\t-o mark as original\n");
676 fprintf (stdout
, "\t-e add error protection\n");
677 fprintf (stdout
, "\t-r force padding bit/frame off\n");
678 fprintf (stdout
, "\t-D len add DAB extensions of length [len]\n");
679 fprintf (stdout
, "\t-t talkativity 0=no messages (dflt 2)");
680 fprintf (stdout
, "Files\n");
682 "\tinput input sound file. (WAV,AIFF,PCM or use '/dev/stdin')\n");
683 fprintf (stdout
, "\toutput output bit stream of encoded audio\n");
685 "\n\tAllowable bitrates for 16, 22.05 and 24kHz sample input\n");
687 "\t8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160\n");
689 "\n\tAllowable bitrates for 32, 44.1 and 48kHz sample input\n");
691 "\t32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384\n");
695 /*********************************************
696 * void short_usage(void)
697 ********************************************/
698 void short_usage (void)
700 /* print a bit of info about the program */
701 fprintf (stderr
, "tooLAME version %s\n (http://toolame.sourceforge.net)\n",
703 fprintf (stderr
, "MPEG Audio Layer II encoder\n\n");
704 fprintf (stderr
, "USAGE: %s [options] <infile> [outfile]\n\n", programName
);
705 fprintf (stderr
, "Try \"%s -h\" for more information.\n", programName
);
709 /*********************************************
710 * void proginfo(void)
711 ********************************************/
714 /* print a bit of info about the program */
716 "\ntooLAME version 0.2g (http://toolame.sourceforge.net)\n");
717 fprintf (stderr
, "MPEG Audio Layer II encoder\n\n");
720 /************************************************************************
724 * PURPOSE: Sets encoding parameters to the specifications of the
725 * command line. Default settings are used for parameters
726 * not specified in the command line.
728 * SEMANTICS: The command line is parsed according to the following
731 * -m is followed by the mode
732 * -p is followed by the psychoacoustic model number
733 * -s is followed by the sampling rate
734 * -b is followed by the total bitrate, irrespective of the mode
735 * -d is followed by the emphasis flag
736 * -c is followed by the copyright/no_copyright flag
737 * -o is followed by the original/not_original flag
738 * -e is followed by the error_protection on/off flag
739 * -f turns off psy model (fast mode)
740 * -q <i> only calculate psy model every ith frame
741 * -a downmix from stereo to mono
742 * -r turn off padding bits in frames.
743 * -x force byte swapping of input
744 * -g swap the channels on an input file
745 * -t talkativity. how verbose should the program be. 0 = no messages.
747 * If the input file is in AIFF format, the sampling frequency is read
748 * from the AIFF header.
750 * The input and output filenames are read into #inpath# and #outpath#.
752 ************************************************************************/
754 void parse_args (int argc
, char **argv
, frame_info
* frame
, int *psy
,
755 unsigned long *num_samples
, char inPath
[MAX_NAME_SIZE
],
756 char outPath
[MAX_NAME_SIZE
])
760 frame_header
*header
= frame
->header
;
764 /* preset defaults */
767 header
->lay
= DFLT_LAY
;
770 header
->mode
= MPG_MD_STEREO
;
771 header
->mode_ext
= 0;
774 header
->mode
= MPG_MD_DUAL_CHANNEL
;
775 header
->mode_ext
= 0;
777 /* in j-stereo mode, no default header->mode_ext was defined, gave error..
778 now default = 2 added by MFC 14 Dec 1999. */
780 header
->mode
= MPG_MD_JOINT_STEREO
;
781 header
->mode_ext
= 2;
784 header
->mode
= MPG_MD_MONO
;
785 header
->mode_ext
= 0;
788 fprintf (stderr
, "%s: Bad mode dflt %c\n", programName
, DFLT_MOD
);
792 if ((header
->sampling_frequency
=
793 toolame_SmpFrqIndex ((long) (1000 * DFLT_SFQ
), &header
->version
)) < 0) {
794 fprintf (stderr
, "%s: bad sfrq default %.2f\n", programName
, DFLT_SFQ
);
797 header
->bitrate_index
= 14;
801 header
->emphasis
= 0;
804 header
->emphasis
= 1;
807 header
->emphasis
= 3;
810 fprintf (stderr
, "%s: Bad emph dflt %c\n", programName
, DFLT_EMP
);
813 header
->copyright
= 0;
814 header
->original
= 0;
815 header
->error_protection
= FALSE
;
816 header
->dab_extension
= 0;
819 while (++i
< argc
&& err
== 0) {
820 char c
, *token
, *arg
, *nextArg
;
824 if (*token
++ == '-') {
826 nextArg
= argv
[i
+ 1];
831 /* The user wants to use stdin and/or stdout. */
832 if (inPath
[0] == '\0')
833 strncpy (inPath
, argv
[i
], MAX_NAME_SIZE
);
834 else if (outPath
[0] == '\0')
835 strncpy (outPath
, argv
[i
], MAX_NAME_SIZE
);
837 while ((c
= *token
++)) {
838 if (*token
/* NumericQ(token) */ )
846 header
->mode
= MPG_MD_STEREO
;
847 header
->mode_ext
= 0;
848 } else if (*arg
== 'd') {
849 header
->mode
= MPG_MD_DUAL_CHANNEL
;
850 header
->mode_ext
= 0;
851 } else if (*arg
== 'j') {
852 header
->mode
= MPG_MD_JOINT_STEREO
;
853 } else if (*arg
== 'm') {
854 header
->mode
= MPG_MD_MONO
;
855 header
->mode_ext
= 0;
857 fprintf (stderr
, "%s: -m mode must be s/d/j/m not %s\n",
870 /* samplerate = rint( 1000.0 * srate ); $A */
871 samplerate
= (long) ((1000.0 * srate
) + 0.5);
872 if ((header
->sampling_frequency
=
873 toolame_SmpFrqIndex ((long) samplerate
, &header
->version
)) < 0)
884 header
->emphasis
= 0;
885 else if (*arg
== '5')
886 header
->emphasis
= 1;
887 else if (*arg
== 'c')
888 header
->emphasis
= 3;
890 fprintf (stderr
, "%s: -d emp must be n/5/c not %s\n", programName
,
897 header
->dab_length
= atoi (arg
);
898 header
->error_protection
= TRUE
;
899 header
->dab_extension
= 2;
903 header
->copyright
= 1;
906 header
->original
= 1;
909 header
->error_protection
= TRUE
;
913 /* this switch is deprecated? FIXME get rid of glopts.usepsy
914 instead us psymodel 0, i.e. "-p 0" */
915 glopts
.usepsy
= FALSE
;
918 glopts
.usepadbit
= FALSE
;
923 glopts
.quickmode
= TRUE
;
924 glopts
.usepsy
= TRUE
;
925 glopts
.quickcount
= atoi (arg
);
926 if (glopts
.quickcount
== 0) {
927 /* just don't use psy model */
928 glopts
.usepsy
= FALSE
;
929 glopts
.quickcount
= FALSE
;
933 glopts
.downmix
= TRUE
;
934 header
->mode
= MPG_MD_MONO
;
935 header
->mode_ext
= 0;
938 glopts
.byteswap
= TRUE
;
943 glopts
.vbrlevel
= atof (arg
);
944 glopts
.usepadbit
= FALSE
; /* don't use padding for VBR */
946 /* MFC Feb 2003: in VBR mode, joint stereo doesn't make
947 any sense at the moment, as there are no noisy subbands
948 according to bits_for_nonoise in vbr mode */
949 header
->mode
= MPG_MD_STEREO
; /* force stereo mode */
950 header
->mode_ext
= 0;
954 glopts
.athlevel
= atof(arg
);
960 glopts
.channelswap
= TRUE
;
964 glopts
.verbosity
= atoi (arg
);
967 fprintf (stderr
, "%s: unrec option %c\n", programName
, c
);
973 token
= ""; /* no more from token */
975 ++i
; /* skip arg we used */
981 if (inPath
[0] == '\0')
982 strcpy (inPath
, argv
[i
]);
983 else if (outPath
[0] == '\0')
984 strcpy (outPath
, argv
[i
]);
986 fprintf (stderr
, "%s: excess arg %s\n", programName
, argv
[i
]);
992 if (header
->dab_extension
) {
994 /* if the bit rate per channel is less then 56 kbit/s, we have 2 scf-crc */
995 /* else we have 4 scf-crc */
996 /* in 24 kHz, we have 4 scf-crc, see main loop */
997 if (brate
/ (header
->mode
== MPG_MD_MONO
? 1 : 2) >= 56)
998 header
->dab_extension
= 4;
1002 if (err
|| inPath
[0] == '\0')
1003 usage (); /* If no infile defined, or err has occured, then call usage() */
1005 if (outPath
[0] == '\0') {
1006 /* replace old extension with new one, 1992-08-19, 1995-06-12 shn */
1007 new_ext (inPath
, DFLT_EXT
, outPath
);
1010 if (!strcmp (inPath
, "-")) {
1011 musicin
= stdin
; /* read from stdin */
1012 *num_samples
= MAX_U_32_NUM
;
1014 if ((musicin
= fopen (inPath
, "rb")) == NULL
) {
1015 fprintf (stderr
, "Could not find \"%s\".\n", inPath
);
1018 parse_input_file (musicin
, inPath
, header
, num_samples
);
1021 /* check for a valid bitrate */
1023 brate
= bitrate
[header
->version
][10];
1025 /* Check to see we have a sane value for the bitrate for this version */
1026 if ((header
->bitrate_index
= toolame_BitrateIndex (brate
, header
->version
)) < 0)
1030 /* All options are hunky dory, open the input audio file and
1031 return to the main drag */
1032 open_bit_stream_w (&bs
, outPath
, BUFFER_SIZE
);
1037 void smr_dump(double smr
[2][SBLIMIT
], int nch
) {
1040 fprintf(stdout
,"SMR:");
1041 for (ch
= 0;ch
<nch
; ch
++) {
1043 fprintf(stdout
," ");
1044 for (sb
=0;sb
<SBLIMIT
;sb
++)
1045 fprintf(stdout
,"%3.0f ",smr
[ch
][sb
]);
1046 fprintf(stdout
,"\n");