Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / toolame-02l / toolame.c
bloba5b2de734621be0d1e41a35950aa4c2dd6783211
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 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)
106 got_it = 1;
107 else
108 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);
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);
274 nch = frame.nch;
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);
283 fflush (stderr);
284 win_buf[0] = &buffer[0][0];
285 win_buf[1] = &buffer[1][0];
287 adb = available_bits (&header, &glopts);
288 lg_frame = adb / 8;
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 */
296 if (frameNum == 1)
297 minimum = lg_frame + MINIMUM;
298 adb -= header.dab_extension * 8 + header.dab_length * 8 + 16;
302 int gr, bl, ch;
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] );
312 #ifdef REFERENCECODE
314 /* Old code. left here for reference */
315 int gr, bl, ch;
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]);
323 #endif
326 #ifdef NEWENCODE
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);
335 #else
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);
344 #endif
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];
355 } else {
356 /* calculate the psymodel */
357 switch (model) {
358 case -1:
359 psycho_n1 (smr, nch);
360 break;
361 case 0: /* Psy Model A */
362 psycho_0 (smr, nch, scalar, (FLOAT) s_freq[header.version][header.sampling_frequency] * 1000);
363 break;
364 case 1:
365 psycho_1 (buffer, max_sc, smr, &frame);
366 break;
367 case 2:
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] *
371 1000, &glopts);
373 break;
374 case 3:
375 /* Modified psy model 1 */
376 psycho_3 (buffer, max_sc, smr, &frame, &glopts);
377 break;
378 case 4:
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] *
383 1000, &glopts);
385 break;
386 case 5:
387 /* Model 5 comparse model 1 and 3 */
388 psycho_1 (buffer, max_sc, smr, &frame);
389 fprintf(stdout,"1 ");
390 smr_dump(smr,nch);
391 psycho_3 (buffer, max_sc, smr, &frame, &glopts);
392 fprintf(stdout,"3 ");
393 smr_dump(smr,nch);
394 break;
395 case 6:
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] *
400 1000, &glopts);
401 fprintf(stdout,"2 ");
402 smr_dump(smr,nch);
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] *
406 1000, &glopts);
407 fprintf(stdout,"4 ");
408 smr_dump(smr,nch);
409 break;
410 case 7:
411 fprintf(stdout,"Frame: %i\n",frameNum);
412 /* Dump the SMRs for all models */
413 psycho_1 (buffer, max_sc, smr, &frame);
414 fprintf(stdout,"1");
415 smr_dump(smr, nch);
416 psycho_3 (buffer, max_sc, smr, &frame, &glopts);
417 fprintf(stdout,"3");
418 smr_dump(smr,nch);
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] *
422 1000, &glopts);
423 fprintf(stdout,"2");
424 smr_dump(smr,nch);
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] *
428 1000, &glopts);
429 fprintf(stdout,"4");
430 smr_dump(smr,nch);
431 break;
432 case 8:
433 /* Compare 0 and 4 */
434 psycho_n1 (smr, nch);
435 fprintf(stdout,"0");
436 smr_dump(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] *
441 1000, &glopts);
442 fprintf(stdout,"4");
443 smr_dump(smr,nch);
444 break;
445 default:
446 fprintf (stderr, "Invalid psy model specification: %i\n", model);
447 toolame_error = 1;
448 pthread_mutex_unlock(&toolame_output_lock);
449 return 1;
450 exit (0);
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)
461 smr_dump(smr, nch);
468 #ifdef NEWENCODE
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,
485 *subband, &frame);
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);
490 #else
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,
501 *subband, &frame);
502 sample_encoding (*subband, bit_alloc, &frame, &bs);
503 #endif
506 /* If not all the bits were used, write out a stack of zeros */
507 for (i = 0; i < adb; i++)
508 put1bit (&bs, 0);
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");
532 toolame_error = 1;
533 pthread_mutex_unlock(&toolame_output_lock);
534 return 1;
535 exit (0);
538 sentBits += frameBits;
541 close_bit_stream_w (&bs);
543 if ((glopts.verbosity > 1) && (glopts.vbr == TRUE)) {
544 int i;
545 #ifdef NEWENCODE
546 extern int vbrstats_new[15];
547 #else
548 extern int vbrstats[15];
549 #endif
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++)
555 #ifdef NEWENCODE
556 fprintf (stdout,"%4i ",vbrstats_new[i]);
557 #else
558 fprintf (stdout, "%4i ", vbrstats[i]);
559 #endif
560 fprintf (stdout, "\n");
563 fprintf (stderr,
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);
571 return 0;
572 // if (fclose (musicin) != 0) {
573 // fprintf (stderr, "Could not close \"%s\".\n", original_file_name);
574 // exit (2);
575 // }
577 fprintf (stderr, "\nDone\n");
578 return 0;
579 exit (0);
582 /************************************************************************
584 * print_config
586 * PURPOSE: Prints the encoding parameters used
588 ************************************************************************/
590 void print_config (frame_info * frame, int *psy, char *inPath,
591 char *outPath)
593 frame_header *header = frame->header;
595 if (glopts.verbosity == 0)
596 return;
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);
609 else
610 fprintf (stderr, "Layer II %s Psy model %d \n", mode_names[header->mode],
611 *psy);
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 /************************************************************************
635 * usage
637 * PURPOSE: Writes command line syntax to the file specified by #stderr#
639 ************************************************************************/
641 void usage (void)
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",
646 toolameversion);
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",
654 DFLT_SFQ);
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",
660 DFLT_MOD);
661 fprintf (stdout, "\t-p psy psychoacoustic model 0/1/2/3 (dflt %4u)\n",
662 DFLT_PSY);
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.
669 fprintf (stdout,
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",
673 DFLT_EMP);
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");
681 fprintf (stdout,
682 "\tinput input sound file. (WAV,AIFF,PCM or use '/dev/stdin')\n");
683 fprintf (stdout, "\toutput output bit stream of encoded audio\n");
684 fprintf (stdout,
685 "\n\tAllowable bitrates for 16, 22.05 and 24kHz sample input\n");
686 fprintf (stdout,
687 "\t8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160\n");
688 fprintf (stdout,
689 "\n\tAllowable bitrates for 32, 44.1 and 48kHz sample input\n");
690 fprintf (stdout,
691 "\t32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384\n");
692 exit (1);
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",
702 toolameversion);
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);
706 exit (0);
709 /*********************************************
710 * void proginfo(void)
711 ********************************************/
712 void proginfo (void)
714 /* print a bit of info about the program */
715 fprintf (stderr,
716 "\ntooLAME version 0.2g (http://toolame.sourceforge.net)\n");
717 fprintf (stderr, "MPEG Audio Layer II encoder\n\n");
720 /************************************************************************
722 * parse_args
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
729 * syntax:
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])
758 FLOAT srate;
759 int brate;
760 frame_header *header = frame->header;
761 int err = 0, i = 0;
762 long samplerate;
764 /* preset defaults */
765 inPath[0] = '\0';
766 outPath[0] = '\0';
767 header->lay = DFLT_LAY;
768 switch (DFLT_MOD) {
769 case 's':
770 header->mode = MPG_MD_STEREO;
771 header->mode_ext = 0;
772 break;
773 case 'd':
774 header->mode = MPG_MD_DUAL_CHANNEL;
775 header->mode_ext = 0;
776 break;
777 /* in j-stereo mode, no default header->mode_ext was defined, gave error..
778 now default = 2 added by MFC 14 Dec 1999. */
779 case 'j':
780 header->mode = MPG_MD_JOINT_STEREO;
781 header->mode_ext = 2;
782 break;
783 case 'm':
784 header->mode = MPG_MD_MONO;
785 header->mode_ext = 0;
786 break;
787 default:
788 fprintf (stderr, "%s: Bad mode dflt %c\n", programName, DFLT_MOD);
789 abort ();
791 *psy = DFLT_PSY;
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);
795 abort ();
797 header->bitrate_index = 14;
798 brate = 0;
799 switch (DFLT_EMP) {
800 case 'n':
801 header->emphasis = 0;
802 break;
803 case '5':
804 header->emphasis = 1;
805 break;
806 case 'c':
807 header->emphasis = 3;
808 break;
809 default:
810 fprintf (stderr, "%s: Bad emph dflt %c\n", programName, DFLT_EMP);
811 abort ();
813 header->copyright = 0;
814 header->original = 0;
815 header->error_protection = FALSE;
816 header->dab_extension = 0;
818 /* process args */
819 while (++i < argc && err == 0) {
820 char c, *token, *arg, *nextArg;
821 int argUsed;
823 token = argv[i];
824 if (*token++ == '-') {
825 if (i + 1 < argc)
826 nextArg = argv[i + 1];
827 else
828 nextArg = "";
829 argUsed = 0;
830 if (!*token) {
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) */ )
839 arg = token;
840 else
841 arg = nextArg;
842 switch (c) {
843 case 'm':
844 argUsed = 1;
845 if (*arg == 's') {
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;
856 } else {
857 fprintf (stderr, "%s: -m mode must be s/d/j/m not %s\n",
858 programName, arg);
859 err = 1;
861 break;
862 case 'p':
863 *psy = atoi (arg);
864 argUsed = 1;
865 break;
867 case 's':
868 argUsed = 1;
869 srate = atof (arg);
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)
874 err = 1;
875 break;
877 case 'b':
878 argUsed = 1;
879 brate = atoi (arg);
880 break;
881 case 'd':
882 argUsed = 1;
883 if (*arg == 'n')
884 header->emphasis = 0;
885 else if (*arg == '5')
886 header->emphasis = 1;
887 else if (*arg == 'c')
888 header->emphasis = 3;
889 else {
890 fprintf (stderr, "%s: -d emp must be n/5/c not %s\n", programName,
891 arg);
892 err = 1;
894 break;
895 case 'D':
896 argUsed = 1;
897 header->dab_length = atoi (arg);
898 header->error_protection = TRUE;
899 header->dab_extension = 2;
900 glopts.dab = TRUE;
901 break;
902 case 'c':
903 header->copyright = 1;
904 break;
905 case 'o':
906 header->original = 1;
907 break;
908 case 'e':
909 header->error_protection = TRUE;
910 break;
911 case 'f':
912 *psy = 0;
913 /* this switch is deprecated? FIXME get rid of glopts.usepsy
914 instead us psymodel 0, i.e. "-p 0" */
915 glopts.usepsy = FALSE;
916 break;
917 case 'r':
918 glopts.usepadbit = FALSE;
919 header->padding = 0;
920 break;
921 case 'q':
922 argUsed = 1;
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;
931 break;
932 case 'a':
933 glopts.downmix = TRUE;
934 header->mode = MPG_MD_MONO;
935 header->mode_ext = 0;
936 break;
937 case 'x':
938 glopts.byteswap = TRUE;
939 break;
940 case 'v':
941 argUsed = 1;
942 glopts.vbr = TRUE;
943 glopts.vbrlevel = atof (arg);
944 glopts.usepadbit = FALSE; /* don't use padding for VBR */
945 header->padding = 0;
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;
951 break;
952 case 'l':
953 argUsed = 1;
954 glopts.athlevel = atof(arg);
955 break;
956 case 'h':
957 usage ();
958 break;
959 case 'g':
960 glopts.channelswap = TRUE;
961 break;
962 case 't':
963 argUsed = 1;
964 glopts.verbosity = atoi (arg);
965 break;
966 default:
967 fprintf (stderr, "%s: unrec option %c\n", programName, c);
968 err = 1;
969 break;
971 if (argUsed) {
972 if (arg == token)
973 token = ""; /* no more from token */
974 else
975 ++i; /* skip arg we used */
976 arg = "";
977 argUsed = 0;
980 } else {
981 if (inPath[0] == '\0')
982 strcpy (inPath, argv[i]);
983 else if (outPath[0] == '\0')
984 strcpy (outPath, argv[i]);
985 else {
986 fprintf (stderr, "%s: excess arg %s\n", programName, argv[i]);
987 err = 1;
992 if (header->dab_extension) {
993 /* in 48 kHz */
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;
1013 } else {
1014 if ((musicin = fopen (inPath, "rb")) == NULL) {
1015 fprintf (stderr, "Could not find \"%s\".\n", inPath);
1016 exit (1);
1018 parse_input_file (musicin, inPath, header, num_samples);
1021 /* check for a valid bitrate */
1022 if (brate == 0)
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)
1027 err = 1;
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) {
1038 int ch, sb;
1040 fprintf(stdout,"SMR:");
1041 for (ch = 0;ch<nch; ch++) {
1042 if (ch==1)
1043 fprintf(stdout," ");
1044 for (sb=0;sb<SBLIMIT;sb++)
1045 fprintf(stdout,"%3.0f ",smr[ch][sb]);
1046 fprintf(stdout,"\n");