r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / toolame-02l / toolame.c
blob3f2be5fa453407fe6b621ed0ced721af9ced9236
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "common.h"
5 #include "encoder.h"
6 #include "musicin.h"
7 #include "options.h"
8 #include "audio_read.h"
9 #include "bitstream.h"
10 #include "mem.h"
11 #include "crc.h"
12 #include "psycho_n1.h"
13 #include "psycho_0.h"
14 #include "psycho_1.h"
15 #include "psycho_2.h"
16 #include "psycho_3.h"
17 #include "psycho_4.h"
18 #include "encode.h"
19 #include "availbits.h"
20 #include "subband.h"
21 #include "encode_new.h"
22 #include "toolame.h"
24 #include <assert.h>
26 FILE *musicin;
27 Bit_stream_struc bs;
28 char *programName;
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;
38 int toolame_eof = 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;
49 toolame_error = 0;
50 toolame_eof = 0;
53 int toolame_send_buffer(char *data, int bytes)
55 int got_it = 0;
56 if(bytes > TOOLAME_BUFFER_BYTES)
58 fprintf(stderr,
59 "toolame_send_buffer: bytes %d exceed maximum bytes %d\n",
60 bytes,
61 TOOLAME_BUFFER_BYTES);
62 return 1;
64 if(toolame_error) return 1;
66 if(!bytes)
68 // pthread_mutex_lock(&toolame_output_lock);
69 toolame_eof = 1;
70 pthread_mutex_unlock(&toolame_input_lock);
71 return 0;
74 while(!got_it)
76 pthread_mutex_lock(&toolame_output_lock);
77 if(toolame_error)
79 pthread_mutex_unlock(&toolame_input_lock);
80 return 1;
83 if(toolame_buffer_bytes < TOOLAME_BUFFER_BYTES - bytes)
85 memcpy(toolame_buffer + toolame_buffer_bytes, data, bytes);
86 toolame_buffer_bytes += bytes;
87 got_it = 1;
89 pthread_mutex_unlock(&toolame_input_lock);
92 return 0;
95 int toolame_buffer_read(char *dst, int size, int n)
97 int got_it = 0;
98 int result = 0;
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)
107 got_it = 1;
108 else
109 pthread_mutex_unlock(&toolame_output_lock);
111 result = size * n;
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);
123 return 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;
149 glopts.vbr = FALSE;
150 glopts.vbrlevel = 0;
151 glopts.athlevel = 0;
152 glopts.verbosity = 2;
155 /************************************************************************
157 * main
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#.
172 * (combine_LR)
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
187 * error protection.
188 * (*_CRC_calc)
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 ************************************************************************/
199 int frameNum = 0;
201 int toolame (int argc, char **argv)
203 typedef double SBS[2][3][SCALE_BLOCK][SBLIMIT];
204 SBS *sb_sample;
205 typedef double JSBS[3][SCALE_BLOCK][SBLIMIT];
206 JSBS *j_sample;
207 typedef double IN[2][HAN_SIZE];
208 IN *win_que;
209 typedef unsigned int SUB[2][3][SCALE_BLOCK][SBLIMIT];
210 SUB *subband;
212 frame_info frame;
213 frame_header header;
214 char original_file_name[MAX_NAME_SIZE];
215 char encoded_file_name[MAX_NAME_SIZE];
216 short **win_buf;
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];
221 // FLOAT snr32[32];
222 short sam[2][1344]; /* was [1056]; */
223 int model, nch, error_protection;
224 static unsigned int crc;
225 int sb, ch, adb;
226 unsigned long frameBits, sentBits = 0;
227 unsigned long num_samples;
228 int lg_frame;
229 int i;
231 /* Used to keep the SNR values for the fast/quick psy models */
232 static FLOAT smrdef[2][32];
234 static int psycount = 0;
235 extern int minimum;
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");
243 /* clear buffers */
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));
255 global_init ();
257 header.extension = 0;
258 frame.header = &header;
259 frame.tab_num = -1; /* no table loaded */
260 frame.alloc = NULL;
261 header.version = MPEG_AUDIO_ID; /* Default: MPEG-1 */
263 programName = argv[0];
264 if (argc == 1) /* no command-line args */
265 short_usage ();
266 else
267 parse_args (argc, argv, &frame, &model, &num_samples, original_file_name,
268 encoded_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);
273 nch = frame.nch;
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);
280 fflush (stderr);
281 win_buf[0] = &buffer[0][0];
282 win_buf[1] = &buffer[1][0];
284 adb = available_bits (&header, &glopts);
285 lg_frame = adb / 8;
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 */
293 if (frameNum == 1)
294 minimum = lg_frame + MINIMUM;
295 adb -= header.dab_extension * 8 + header.dab_length * 8 + 16;
299 int gr, bl, ch;
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] );
309 #ifdef REFERENCECODE
311 /* Old code. left here for reference */
312 int gr, bl, ch;
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]);
320 #endif
323 #ifdef NEWENCODE
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);
332 #else
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);
341 #endif
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];
352 } else {
353 /* calculate the psymodel */
354 switch (model) {
355 case -1:
356 psycho_n1 (smr, nch);
357 break;
358 case 0: /* Psy Model A */
359 psycho_0 (smr, nch, scalar, (FLOAT) s_freq[header.version][header.sampling_frequency] * 1000);
360 break;
361 case 1:
362 psycho_1 (buffer, max_sc, smr, &frame);
363 break;
364 case 2:
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] *
368 1000, &glopts);
370 break;
371 case 3:
372 /* Modified psy model 1 */
373 psycho_3 (buffer, max_sc, smr, &frame, &glopts);
374 break;
375 case 4:
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] *
380 1000, &glopts);
382 break;
383 case 5:
384 /* Model 5 comparse model 1 and 3 */
385 psycho_1 (buffer, max_sc, smr, &frame);
386 fprintf(stdout,"1 ");
387 smr_dump(smr,nch);
388 psycho_3 (buffer, max_sc, smr, &frame, &glopts);
389 fprintf(stdout,"3 ");
390 smr_dump(smr,nch);
391 break;
392 case 6:
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] *
397 1000, &glopts);
398 fprintf(stdout,"2 ");
399 smr_dump(smr,nch);
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] *
403 1000, &glopts);
404 fprintf(stdout,"4 ");
405 smr_dump(smr,nch);
406 break;
407 case 7:
408 fprintf(stdout,"Frame: %i\n",frameNum);
409 /* Dump the SMRs for all models */
410 psycho_1 (buffer, max_sc, smr, &frame);
411 fprintf(stdout,"1");
412 smr_dump(smr, nch);
413 psycho_3 (buffer, max_sc, smr, &frame, &glopts);
414 fprintf(stdout,"3");
415 smr_dump(smr,nch);
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] *
419 1000, &glopts);
420 fprintf(stdout,"2");
421 smr_dump(smr,nch);
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] *
425 1000, &glopts);
426 fprintf(stdout,"4");
427 smr_dump(smr,nch);
428 break;
429 case 8:
430 /* Compare 0 and 4 */
431 psycho_n1 (smr, nch);
432 fprintf(stdout,"0");
433 smr_dump(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] *
438 1000, &glopts);
439 fprintf(stdout,"4");
440 smr_dump(smr,nch);
441 break;
442 default:
443 fprintf (stderr, "Invalid psy model specification: %i\n", model);
444 toolame_error = 1;
445 pthread_mutex_unlock(&toolame_output_lock);
446 return 1;
447 exit (0);
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)
458 smr_dump(smr, nch);
465 #ifdef NEWENCODE
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,
482 *subband, &frame);
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);
487 #else
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,
498 *subband, &frame);
499 sample_encoding (*subband, bit_alloc, &frame, &bs);
500 #endif
503 /* If not all the bits were used, write out a stack of zeros */
504 for (i = 0; i < adb; i++)
505 put1bit (&bs, 0);
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");
529 toolame_error = 1;
530 pthread_mutex_unlock(&toolame_output_lock);
531 return 1;
532 exit (0);
535 sentBits += frameBits;
538 close_bit_stream_w (&bs);
540 if ((glopts.verbosity > 1) && (glopts.vbr == TRUE)) {
541 int i;
542 #ifdef NEWENCODE
543 extern int vbrstats_new[15];
544 #else
545 extern int vbrstats[15];
546 #endif
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++)
552 #ifdef NEWENCODE
553 fprintf (stdout,"%4i ",vbrstats_new[i]);
554 #else
555 fprintf (stdout, "%4i ", vbrstats[i]);
556 #endif
557 fprintf (stdout, "\n");
560 fprintf (stderr,
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);
568 return 0;
569 // if (fclose (musicin) != 0) {
570 // fprintf (stderr, "Could not close \"%s\".\n", original_file_name);
571 // exit (2);
572 // }
574 fprintf (stderr, "\nDone\n");
575 return 0;
576 exit (0);
579 /************************************************************************
581 * print_config
583 * PURPOSE: Prints the encoding parameters used
585 ************************************************************************/
587 void print_config (frame_info * frame, int *psy, char *inPath,
588 char *outPath)
590 frame_header *header = frame->header;
592 if (glopts.verbosity == 0)
593 return;
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);
606 else
607 fprintf (stderr, "Layer II %s Psy model %d \n", mode_names[header->mode],
608 *psy);
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 /************************************************************************
632 * usage
634 * PURPOSE: Writes command line syntax to the file specified by #stderr#
636 ************************************************************************/
638 void usage (void)
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",
643 toolameversion);
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",
651 DFLT_SFQ);
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",
657 DFLT_MOD);
658 fprintf (stdout, "\t-p psy psychoacoustic model 0/1/2/3 (dflt %4u)\n",
659 DFLT_PSY);
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.
666 fprintf (stdout,
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",
670 DFLT_EMP);
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");
678 fprintf (stdout,
679 "\tinput input sound file. (WAV,AIFF,PCM or use '/dev/stdin')\n");
680 fprintf (stdout, "\toutput output bit stream of encoded audio\n");
681 fprintf (stdout,
682 "\n\tAllowable bitrates for 16, 22.05 and 24kHz sample input\n");
683 fprintf (stdout,
684 "\t8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160\n");
685 fprintf (stdout,
686 "\n\tAllowable bitrates for 32, 44.1 and 48kHz sample input\n");
687 fprintf (stdout,
688 "\t32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384\n");
689 exit (1);
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",
699 toolameversion);
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);
703 exit (0);
706 /*********************************************
707 * void proginfo(void)
708 ********************************************/
709 void proginfo (void)
711 /* print a bit of info about the program */
712 fprintf (stderr,
713 "\ntooLAME version 0.2g (http://toolame.sourceforge.net)\n");
714 fprintf (stderr, "MPEG Audio Layer II encoder\n\n");
717 /************************************************************************
719 * parse_args
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
726 * syntax:
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])
755 FLOAT srate;
756 int brate;
757 frame_header *header = frame->header;
758 int err = 0, i = 0;
759 long samplerate;
761 /* preset defaults */
762 inPath[0] = '\0';
763 outPath[0] = '\0';
764 header->lay = DFLT_LAY;
765 switch (DFLT_MOD) {
766 case 's':
767 header->mode = MPG_MD_STEREO;
768 header->mode_ext = 0;
769 break;
770 case 'd':
771 header->mode = MPG_MD_DUAL_CHANNEL;
772 header->mode_ext = 0;
773 break;
774 /* in j-stereo mode, no default header->mode_ext was defined, gave error..
775 now default = 2 added by MFC 14 Dec 1999. */
776 case 'j':
777 header->mode = MPG_MD_JOINT_STEREO;
778 header->mode_ext = 2;
779 break;
780 case 'm':
781 header->mode = MPG_MD_MONO;
782 header->mode_ext = 0;
783 break;
784 default:
785 fprintf (stderr, "%s: Bad mode dflt %c\n", programName, DFLT_MOD);
786 abort ();
788 *psy = DFLT_PSY;
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);
792 abort ();
794 header->bitrate_index = 14;
795 brate = 0;
796 switch (DFLT_EMP) {
797 case 'n':
798 header->emphasis = 0;
799 break;
800 case '5':
801 header->emphasis = 1;
802 break;
803 case 'c':
804 header->emphasis = 3;
805 break;
806 default:
807 fprintf (stderr, "%s: Bad emph dflt %c\n", programName, DFLT_EMP);
808 abort ();
810 header->copyright = 0;
811 header->original = 0;
812 header->error_protection = FALSE;
813 header->dab_extension = 0;
815 /* process args */
816 while (++i < argc && err == 0) {
817 char c, *token, *arg, *nextArg;
818 int argUsed;
820 token = argv[i];
821 if (*token++ == '-') {
822 if (i + 1 < argc)
823 nextArg = argv[i + 1];
824 else
825 nextArg = "";
826 argUsed = 0;
827 if (!*token) {
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) */ )
836 arg = token;
837 else
838 arg = nextArg;
839 switch (c) {
840 case 'm':
841 argUsed = 1;
842 if (*arg == 's') {
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;
853 } else {
854 fprintf (stderr, "%s: -m mode must be s/d/j/m not %s\n",
855 programName, arg);
856 err = 1;
858 break;
859 case 'p':
860 *psy = atoi (arg);
861 argUsed = 1;
862 break;
864 case 's':
865 argUsed = 1;
866 srate = atof (arg);
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)
871 err = 1;
872 break;
874 case 'b':
875 argUsed = 1;
876 brate = atoi (arg);
877 break;
878 case 'd':
879 argUsed = 1;
880 if (*arg == 'n')
881 header->emphasis = 0;
882 else if (*arg == '5')
883 header->emphasis = 1;
884 else if (*arg == 'c')
885 header->emphasis = 3;
886 else {
887 fprintf (stderr, "%s: -d emp must be n/5/c not %s\n", programName,
888 arg);
889 err = 1;
891 break;
892 case 'D':
893 argUsed = 1;
894 header->dab_length = atoi (arg);
895 header->error_protection = TRUE;
896 header->dab_extension = 2;
897 glopts.dab = TRUE;
898 break;
899 case 'c':
900 header->copyright = 1;
901 break;
902 case 'o':
903 header->original = 1;
904 break;
905 case 'e':
906 header->error_protection = TRUE;
907 break;
908 case 'f':
909 *psy = 0;
910 /* this switch is deprecated? FIXME get rid of glopts.usepsy
911 instead us psymodel 0, i.e. "-p 0" */
912 glopts.usepsy = FALSE;
913 break;
914 case 'r':
915 glopts.usepadbit = FALSE;
916 header->padding = 0;
917 break;
918 case 'q':
919 argUsed = 1;
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;
928 break;
929 case 'a':
930 glopts.downmix = TRUE;
931 header->mode = MPG_MD_MONO;
932 header->mode_ext = 0;
933 break;
934 case 'x':
935 glopts.byteswap = TRUE;
936 break;
937 case 'v':
938 argUsed = 1;
939 glopts.vbr = TRUE;
940 glopts.vbrlevel = atof (arg);
941 glopts.usepadbit = FALSE; /* don't use padding for VBR */
942 header->padding = 0;
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;
948 break;
949 case 'l':
950 argUsed = 1;
951 glopts.athlevel = atof(arg);
952 break;
953 case 'h':
954 usage ();
955 break;
956 case 'g':
957 glopts.channelswap = TRUE;
958 break;
959 case 't':
960 argUsed = 1;
961 glopts.verbosity = atoi (arg);
962 break;
963 default:
964 fprintf (stderr, "%s: unrec option %c\n", programName, c);
965 err = 1;
966 break;
968 if (argUsed) {
969 if (arg == token)
970 token = ""; /* no more from token */
971 else
972 ++i; /* skip arg we used */
973 arg = "";
974 argUsed = 0;
977 } else {
978 if (inPath[0] == '\0')
979 strcpy (inPath, argv[i]);
980 else if (outPath[0] == '\0')
981 strcpy (outPath, argv[i]);
982 else {
983 fprintf (stderr, "%s: excess arg %s\n", programName, argv[i]);
984 err = 1;
989 if (header->dab_extension) {
990 /* in 48 kHz */
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;
1010 } else {
1011 if ((musicin = fopen (inPath, "rb")) == NULL) {
1012 fprintf (stderr, "Could not find \"%s\".\n", inPath);
1013 exit (1);
1015 parse_input_file (musicin, inPath, header, num_samples);
1018 /* check for a valid bitrate */
1019 if (brate == 0)
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)
1024 err = 1;
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) {
1033 int ch, sb;
1035 fprintf(stdout,"SMR:");
1036 for (ch = 0;ch<nch; ch++) {
1037 if (ch==1)
1038 fprintf(stdout," ");
1039 for (sb=0;sb<SBLIMIT;sb++)
1040 fprintf(stdout,"%3.0f ",smr[ch][sb]);
1041 fprintf(stdout,"\n");