00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include "common.h"
00005 #include "encoder.h"
00006 #include "musicin.h"
00007 #include "options.h"
00008 #include "audio_read.h"
00009 #include "bitstream.h"
00010 #include "mem.h"
00011 #include "crc.h"
00012 #include "psycho_n1.h"
00013 #include "psycho_0.h"
00014 #include "psycho_1.h"
00015 #include "psycho_2.h"
00016 #include "psycho_3.h"
00017 #include "psycho_4.h"
00018 #include "encode.h"
00019 #include "availbits.h"
00020 #include "subband.h"
00021 #include "encode_new.h"
00022 #include "toolame.h"
00023
00024 #include <assert.h>
00025
00026 FILE *musicin;
00027 Bit_stream_struc bs;
00028 char *programName;
00029 char toolameversion[10] = "0.2l";
00030
00031
00032
00033 pthread_mutex_t toolame_input_lock;
00034 pthread_mutex_t toolame_output_lock;
00035 char *toolame_buffer = 0;
00036 int toolame_buffer_bytes = 0;
00037 int toolame_error = 0;
00038 int toolame_eof = 0;
00039
00040
00041
00042 void toolame_init_buffers()
00043 {
00044 pthread_mutex_init(&toolame_input_lock, 0);
00045 pthread_mutex_init(&toolame_output_lock, 0);
00046 pthread_mutex_lock(&toolame_input_lock);
00047 if(!toolame_buffer) toolame_buffer = malloc(TOOLAME_BUFFER_BYTES);
00048 toolame_buffer_bytes = 0;
00049 toolame_error = 0;
00050 toolame_eof = 0;
00051 }
00052
00053 int toolame_send_buffer(char *data, int bytes)
00054 {
00055 int got_it = 0;
00056 if(bytes > TOOLAME_BUFFER_BYTES)
00057 {
00058 fprintf(stderr,
00059 "toolame_send_buffer: bytes %d exceed maximum bytes %d\n",
00060 bytes,
00061 TOOLAME_BUFFER_BYTES);
00062 return 1;
00063 }
00064 if(toolame_error) return 1;
00065
00066 if(!bytes)
00067 {
00068
00069 toolame_eof = 1;
00070 pthread_mutex_unlock(&toolame_input_lock);
00071 return 0;
00072 }
00073
00074 while(!got_it)
00075 {
00076 pthread_mutex_lock(&toolame_output_lock);
00077 if(toolame_error)
00078 {
00079 pthread_mutex_unlock(&toolame_input_lock);
00080 return 1;
00081 }
00082
00083 if(toolame_buffer_bytes < TOOLAME_BUFFER_BYTES - bytes)
00084 {
00085 memcpy(toolame_buffer + toolame_buffer_bytes, data, bytes);
00086 toolame_buffer_bytes += bytes;
00087 got_it = 1;
00088 }
00089 pthread_mutex_unlock(&toolame_input_lock);
00090 }
00091
00092 return 0;
00093 }
00094
00095 int toolame_buffer_read(char *dst, int size, int n)
00096 {
00097 int got_it = 0;
00098 int result = 0;
00099
00100 while(!got_it && !toolame_eof && !toolame_error)
00101 {
00102 pthread_mutex_lock(&toolame_input_lock);
00103
00104
00105 if(toolame_eof || toolame_buffer_bytes >= size * n)
00106 got_it = 1;
00107 else
00108 pthread_mutex_unlock(&toolame_output_lock);
00109 }
00110
00111 result = size * n;
00112 if(result > toolame_buffer_bytes)
00113 result = toolame_buffer_bytes;
00114 memcpy(dst, toolame_buffer, result);
00115
00116 if(size * n > result)
00117 bzero(dst + result, size * n - result);
00118 memcpy(toolame_buffer, toolame_buffer + result, toolame_buffer_bytes - result);
00119 toolame_buffer_bytes -= result;
00120 pthread_mutex_unlock(&toolame_output_lock);
00121
00122
00123 return result;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 void global_init (void)
00141 {
00142 glopts.usepsy = TRUE;
00143 glopts.usepadbit = TRUE;
00144 glopts.quickmode = FALSE;
00145 glopts.quickcount = 10;
00146 glopts.downmix = FALSE;
00147 glopts.byteswap = FALSE;
00148 glopts.channelswap = FALSE;
00149 glopts.vbr = FALSE;
00150 glopts.vbrlevel = 0;
00151 glopts.athlevel = 0;
00152 glopts.verbosity = 2;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 int frameNum = 0;
00200
00201 int toolame (int argc, char **argv)
00202 {
00203 typedef double SBS[2][3][SCALE_BLOCK][SBLIMIT];
00204 SBS *sb_sample;
00205 typedef double JSBS[3][SCALE_BLOCK][SBLIMIT];
00206 JSBS *j_sample;
00207 typedef double IN[2][HAN_SIZE];
00208 IN *win_que;
00209 typedef unsigned int SUB[2][3][SCALE_BLOCK][SBLIMIT];
00210 SUB *subband;
00211
00212 frame_info frame;
00213 frame_header header;
00214 char original_file_name[MAX_NAME_SIZE];
00215 char encoded_file_name[MAX_NAME_SIZE];
00216 short **win_buf;
00217 static short buffer[2][1152];
00218 static unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];
00219 static unsigned int scalar[2][3][SBLIMIT], j_scale[3][SBLIMIT];
00220 static double smr[2][SBLIMIT], lgmin[2][SBLIMIT], max_sc[2][SBLIMIT];
00221
00222 short sam[2][1344];
00223 int model, nch, error_protection;
00224 static unsigned int crc;
00225 int sb, ch, adb;
00226 unsigned long frameBits, sentBits = 0;
00227 unsigned long num_samples;
00228 int lg_frame;
00229 int i;
00230
00231
00232 static FLOAT smrdef[2][32];
00233
00234 static int psycount = 0;
00235 extern int minimum;
00236
00237 sb_sample = (SBS *) mem_alloc (sizeof (SBS), "sb_sample");
00238 j_sample = (JSBS *) mem_alloc (sizeof (JSBS), "j_sample");
00239 win_que = (IN *) mem_alloc (sizeof (IN), "Win_que");
00240 subband = (SUB *) mem_alloc (sizeof (SUB), "subband");
00241 win_buf = (short **) mem_alloc (sizeof (short *) * 2, "win_buf");
00242
00243
00244 memset ((char *) buffer, 0, sizeof (buffer));
00245 memset ((char *) bit_alloc, 0, sizeof (bit_alloc));
00246 memset ((char *) scalar, 0, sizeof (scalar));
00247 memset ((char *) j_scale, 0, sizeof (j_scale));
00248 memset ((char *) scfsi, 0, sizeof (scfsi));
00249 memset ((char *) smr, 0, sizeof (smr));
00250 memset ((char *) lgmin, 0, sizeof (lgmin));
00251 memset ((char *) max_sc, 0, sizeof (max_sc));
00252
00253 memset ((char *) sam, 0, sizeof (sam));
00254
00255 global_init ();
00256
00257 header.extension = 0;
00258 frame.header = &header;
00259 frame.tab_num = -1;
00260 frame.alloc = NULL;
00261 header.version = MPEG_AUDIO_ID;
00262
00263 programName = argv[0];
00264 if (argc == 1)
00265 short_usage ();
00266 else
00267 parse_args (argc, argv, &frame, &model, &num_samples, original_file_name,
00268 encoded_file_name);
00269
00270 print_config (&frame, &model, original_file_name, encoded_file_name);
00271
00272
00273 hdr_to_frps (&frame);
00274 nch = frame.nch;
00275 error_protection = header.error_protection;
00276
00277
00278
00279 while (get_audio (musicin, buffer, num_samples, nch, &header) > 0) {
00280 if (glopts.verbosity > 1)
00281 if (++frameNum % 10 == 0)
00282 fprintf (stderr, "[%4u]\r", frameNum);
00283 fflush (stderr);
00284 win_buf[0] = &buffer[0][0];
00285 win_buf[1] = &buffer[1][0];
00286
00287 adb = available_bits (&header, &glopts);
00288 lg_frame = adb / 8;
00289 if (header.dab_extension) {
00290
00291 if (header.sampling_frequency == 1)
00292 header.dab_extension = 4;
00293
00294
00295
00296 if (frameNum == 1)
00297 minimum = lg_frame + MINIMUM;
00298 adb -= header.dab_extension * 8 + header.dab_length * 8 + 16;
00299 }
00300
00301 {
00302 int gr, bl, ch;
00303
00304
00305 for( gr = 0; gr < 3; gr++ )
00306 for ( bl = 0; bl < 12; bl++ )
00307 for ( ch = 0; ch < nch; ch++ )
00308 WindowFilterSubband( &buffer[ch][gr * 12 * 32 + 32 * bl], ch,
00309 &(*sb_sample)[ch][gr][bl][0] );
00310 }
00311
00312 #ifdef REFERENCECODE
00313 {
00314
00315 int gr, bl, ch;
00316 for (gr = 0; gr < 3; gr++)
00317 for (bl = 0; bl < SCALE_BLOCK; bl++)
00318 for (ch = 0; ch < nch; ch++) {
00319 window_subband (&win_buf[ch], &(*win_que)[ch][0], ch);
00320 filter_subband (&(*win_que)[ch][0], &(*sb_sample)[ch][gr][bl][0]);
00321 }
00322 }
00323 #endif
00324
00325
00326 #ifdef NEWENCODE
00327 scalefactor_calc_new(*sb_sample, scalar, nch, frame.sblimit);
00328 find_sf_max (scalar, &frame, max_sc);
00329 if (frame.actual_mode == MPG_MD_JOINT_STEREO) {
00330
00331
00332 combine_LR_new (*sb_sample, *j_sample, frame.sblimit);
00333 scalefactor_calc_new (j_sample, &j_scale, 1, frame.sblimit);
00334 }
00335 #else
00336 scale_factor_calc (*sb_sample, scalar, nch, frame.sblimit);
00337 pick_scale (scalar, &frame, max_sc);
00338 if (frame.actual_mode == MPG_MD_JOINT_STEREO) {
00339
00340
00341 combine_LR (*sb_sample, *j_sample, frame.sblimit);
00342 scale_factor_calc (j_sample, &j_scale, 1, frame.sblimit);
00343 }
00344 #endif
00345
00346
00347
00348 if ((glopts.quickmode == TRUE) && (++psycount % glopts.quickcount != 0)) {
00349
00350
00351 for (ch = 0; ch < nch; ch++) {
00352 for (sb = 0; sb < SBLIMIT; sb++)
00353 smr[ch][sb] = smrdef[ch][sb];
00354 }
00355 } else {
00356
00357 switch (model) {
00358 case -1:
00359 psycho_n1 (smr, nch);
00360 break;
00361 case 0:
00362 psycho_0 (smr, nch, scalar, (FLOAT) s_freq[header.version][header.sampling_frequency] * 1000);
00363 break;
00364 case 1:
00365 psycho_1 (buffer, max_sc, smr, &frame);
00366 break;
00367 case 2:
00368 for (ch = 0; ch < nch; ch++) {
00369 psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0],
00370 (FLOAT) s_freq[header.version][header.sampling_frequency] *
00371 1000, &glopts);
00372 }
00373 break;
00374 case 3:
00375
00376 psycho_3 (buffer, max_sc, smr, &frame, &glopts);
00377 break;
00378 case 4:
00379
00380 for (ch = 0; ch < nch; ch++) {
00381 psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0],
00382 (FLOAT) s_freq[header.version][header.sampling_frequency] *
00383 1000, &glopts);
00384 }
00385 break;
00386 case 5:
00387
00388 psycho_1 (buffer, max_sc, smr, &frame);
00389 fprintf(stdout,"1 ");
00390 smr_dump(smr,nch);
00391 psycho_3 (buffer, max_sc, smr, &frame, &glopts);
00392 fprintf(stdout,"3 ");
00393 smr_dump(smr,nch);
00394 break;
00395 case 6:
00396
00397 for (ch = 0; ch < nch; ch++)
00398 psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0],
00399 (FLOAT) s_freq[header.version][header.sampling_frequency] *
00400 1000, &glopts);
00401 fprintf(stdout,"2 ");
00402 smr_dump(smr,nch);
00403 for (ch = 0; ch < nch; ch++)
00404 psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0],
00405 (FLOAT) s_freq[header.version][header.sampling_frequency] *
00406 1000, &glopts);
00407 fprintf(stdout,"4 ");
00408 smr_dump(smr,nch);
00409 break;
00410 case 7:
00411 fprintf(stdout,"Frame: %i\n",frameNum);
00412
00413 psycho_1 (buffer, max_sc, smr, &frame);
00414 fprintf(stdout,"1");
00415 smr_dump(smr, nch);
00416 psycho_3 (buffer, max_sc, smr, &frame, &glopts);
00417 fprintf(stdout,"3");
00418 smr_dump(smr,nch);
00419 for (ch = 0; ch < nch; ch++)
00420 psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0],
00421 (FLOAT) s_freq[header.version][header.sampling_frequency] *
00422 1000, &glopts);
00423 fprintf(stdout,"2");
00424 smr_dump(smr,nch);
00425 for (ch = 0; ch < nch; ch++)
00426 psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0],
00427 (FLOAT) s_freq[header.version][header.sampling_frequency] *
00428 1000, &glopts);
00429 fprintf(stdout,"4");
00430 smr_dump(smr,nch);
00431 break;
00432 case 8:
00433
00434 psycho_n1 (smr, nch);
00435 fprintf(stdout,"0");
00436 smr_dump(smr,nch);
00437
00438 for (ch = 0; ch < nch; ch++)
00439 psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0],
00440 (FLOAT) s_freq[header.version][header.sampling_frequency] *
00441 1000, &glopts);
00442 fprintf(stdout,"4");
00443 smr_dump(smr,nch);
00444 break;
00445 default:
00446 fprintf (stderr, "Invalid psy model specification: %i\n", model);
00447 toolame_error = 1;
00448 pthread_mutex_unlock(&toolame_output_lock);
00449 return 1;
00450 exit (0);
00451 }
00452
00453 if (glopts.quickmode == TRUE)
00454
00455 for (ch = 0; ch < nch; ch++) {
00456 for (sb = 0; sb < SBLIMIT; sb++)
00457 smrdef[ch][sb] = smr[ch][sb];
00458 }
00459
00460 if (glopts.verbosity > 4)
00461 smr_dump(smr, nch);
00462
00463
00464
00465
00466 }
00467
00468 #ifdef NEWENCODE
00469 sf_transmission_pattern (scalar, scfsi, &frame);
00470 main_bit_allocation_new (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
00471
00472
00473 if (error_protection)
00474 CRC_calc (&frame, bit_alloc, scfsi, &crc);
00475
00476 write_header (&frame, &bs);
00477
00478 if (error_protection)
00479 putbits (&bs, crc, 16);
00480 write_bit_alloc (bit_alloc, &frame, &bs);
00481
00482 write_scalefactors(bit_alloc, scfsi, scalar, &frame, &bs);
00483
00484 subband_quantization_new (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
00485 *subband, &frame);
00486
00487
00488 write_samples_new(*subband, bit_alloc, &frame, &bs);
00489
00490 #else
00491 transmission_pattern (scalar, scfsi, &frame);
00492 main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
00493 if (error_protection)
00494 CRC_calc (&frame, bit_alloc, scfsi, &crc);
00495 encode_info (&frame, &bs);
00496 if (error_protection)
00497 encode_CRC (crc, &bs);
00498 encode_bit_alloc (bit_alloc, &frame, &bs);
00499 encode_scale (bit_alloc, scfsi, scalar, &frame, &bs);
00500 subband_quantization (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
00501 *subband, &frame);
00502 sample_encoding (*subband, bit_alloc, &frame, &bs);
00503 #endif
00504
00505
00506
00507 for (i = 0; i < adb; i++)
00508 put1bit (&bs, 0);
00509 if (header.dab_extension) {
00510
00511 putbits (&bs, 0, header.dab_length * 8);
00512
00513 for (i = header.dab_extension - 1; i >= 0; i--) {
00514 CRC_calcDAB (&frame, bit_alloc, scfsi, scalar, &crc, i);
00515
00516 if (bs.buf_byte_idx + lg_frame < bs.buf_size)
00517 bs.buf[bs.buf_byte_idx + lg_frame] = crc;
00518
00519 putbits (&bs, crc, 8);
00520 }
00521 putbits (&bs, 0, 16);
00522 }
00523
00524 frameBits = sstell (&bs) - sentBits;
00525
00526 if (frameBits % 8) {
00527 fprintf (stderr, "Sent %ld bits = %ld slots plus %ld\n", frameBits,
00528 frameBits / 8, frameBits % 8);
00529 fprintf (stderr, "If you are reading this, the program is broken\n");
00530 fprintf (stderr, "email [mfc at NOTplanckenerg.com] without the NOT\n");
00531 fprintf (stderr, "with the command line arguments and other info\n");
00532 toolame_error = 1;
00533 pthread_mutex_unlock(&toolame_output_lock);
00534 return 1;
00535 exit (0);
00536 }
00537
00538 sentBits += frameBits;
00539 }
00540
00541 close_bit_stream_w (&bs);
00542
00543 if ((glopts.verbosity > 1) && (glopts.vbr == TRUE)) {
00544 int i;
00545 #ifdef NEWENCODE
00546 extern int vbrstats_new[15];
00547 #else
00548 extern int vbrstats[15];
00549 #endif
00550 fprintf (stdout, "VBR stats:\n");
00551 for (i = 1; i < 15; i++)
00552 fprintf (stdout, "%4i ", bitrate[header.version][i]);
00553 fprintf (stdout, "\n");
00554 for (i = 1; i < 15; i++)
00555 #ifdef NEWENCODE
00556 fprintf (stdout,"%4i ",vbrstats_new[i]);
00557 #else
00558 fprintf (stdout, "%4i ", vbrstats[i]);
00559 #endif
00560 fprintf (stdout, "\n");
00561 }
00562
00563 fprintf (stderr,
00564 "Avg slots/frame = %.3f; b/smp = %.2f; bitrate = %.3f kbps\n",
00565 (FLOAT) sentBits / (frameNum * 8),
00566 (FLOAT) sentBits / (frameNum * 1152),
00567 (FLOAT) sentBits / (frameNum * 1152) *
00568 s_freq[header.version][header.sampling_frequency]);
00569
00570 pthread_mutex_unlock(&toolame_output_lock);
00571 return 0;
00572
00573
00574
00575
00576
00577 fprintf (stderr, "\nDone\n");
00578 return 0;
00579 exit (0);
00580 }
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590 void print_config (frame_info * frame, int *psy, char *inPath,
00591 char *outPath)
00592 {
00593 frame_header *header = frame->header;
00594
00595 if (glopts.verbosity == 0)
00596 return;
00597
00598 fprintf (stderr, "--------------------------------------------\n");
00599 fprintf (stderr, "Input File : '%s' %.1f kHz\n",
00600 (strcmp (inPath, "-") ? inPath : "stdin"),
00601 s_freq[header->version][header->sampling_frequency]);
00602 fprintf (stderr, "Output File: '%s'\n",
00603 (strcmp (outPath, "-") ? outPath : "stdout"));
00604 fprintf (stderr, "%d kbps ", bitrate[header->version][header->bitrate_index]);
00605 fprintf (stderr, "%s ", version_names[header->version]);
00606 if (header->mode != MPG_MD_JOINT_STEREO)
00607 fprintf (stderr, "Layer II %s Psycho model=%d (Mode_Extension=%d)\n",
00608 mode_names[header->mode], *psy, header->mode_ext);
00609 else
00610 fprintf (stderr, "Layer II %s Psy model %d \n", mode_names[header->mode],
00611 *psy);
00612
00613 fprintf (stderr, "[De-emph:%s\tCopyright:%s\tOriginal:%s\tCRC:%s]\n",
00614 ((header->emphasis) ? "On" : "Off"),
00615 ((header->copyright) ? "Yes" : "No"),
00616 ((header->original) ? "Yes" : "No"),
00617 ((header->error_protection) ? "On" : "Off"));
00618
00619 fprintf (stderr, "[Padding:%s\tByte-swap:%s\tChanswap:%s\tDAB:%s]\n",
00620 ((glopts.usepadbit) ? "Normal" : "Off"),
00621 ((glopts.byteswap) ? "On" : "Off"),
00622 ((glopts.channelswap) ? "On" : "Off"),
00623 ((glopts.dab) ? "On" : "Off"));
00624
00625 if (glopts.vbr == TRUE)
00626 fprintf (stderr, "VBR Enabled. Using MNR boost of %f\n", glopts.vbrlevel);
00627 fprintf(stderr,"ATH adjustment %f\n",glopts.athlevel);
00628
00629 fprintf (stderr, "--------------------------------------------\n");
00630 }
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641 void usage (void)
00642 {
00643
00644
00645 fprintf (stdout, "\ntooLAME version %s (http://toolame.sourceforge.net)\n",
00646 toolameversion);
00647 fprintf (stdout, "MPEG Audio Layer II encoder\n\n");
00648 fprintf (stdout, "usage: \n");
00649 fprintf (stdout, "\t%s [options] <input> <output>\n\n", programName);
00650
00651 fprintf (stdout, "Options:\n");
00652 fprintf (stdout, "Input\n");
00653 fprintf (stdout, "\t-s sfrq input smpl rate in kHz (dflt %4.1f)\n",
00654 DFLT_SFQ);
00655 fprintf (stdout, "\t-a downmix from stereo to mono\n");
00656 fprintf (stdout, "\t-x force byte-swapping of input\n");
00657 fprintf (stdout, "\t-g swap channels of input file\n");
00658 fprintf (stdout, "Output\n");
00659 fprintf (stdout, "\t-m mode channel mode : s/d/j/m (dflt %4c)\n",
00660 DFLT_MOD);
00661 fprintf (stdout, "\t-p psy psychoacoustic model 0/1/2/3 (dflt %4u)\n",
00662 DFLT_PSY);
00663 fprintf (stdout, "\t-b br total bitrate in kbps (dflt 192)\n");
00664 fprintf (stdout, "\t-v lev vbr mode\n");
00665 fprintf (stdout, "\t-l lev ATH level (dflt 0)\n");
00666 fprintf (stdout, "Operation\n");
00667
00668
00669 fprintf (stdout,
00670 "\t-q num quick mode. only calculate psy model every num frames\n");
00671 fprintf (stdout, "Misc\n");
00672 fprintf (stdout, "\t-d emp de-emphasis n/5/c (dflt %4c)\n",
00673 DFLT_EMP);
00674 fprintf (stdout, "\t-c mark as copyright\n");
00675 fprintf (stdout, "\t-o mark as original\n");
00676 fprintf (stdout, "\t-e add error protection\n");
00677 fprintf (stdout, "\t-r force padding bit/frame off\n");
00678 fprintf (stdout, "\t-D len add DAB extensions of length [len]\n");
00679 fprintf (stdout, "\t-t talkativity 0=no messages (dflt 2)");
00680 fprintf (stdout, "Files\n");
00681 fprintf (stdout,
00682 "\tinput input sound file. (WAV,AIFF,PCM or use '/dev/stdin')\n");
00683 fprintf (stdout, "\toutput output bit stream of encoded audio\n");
00684 fprintf (stdout,
00685 "\n\tAllowable bitrates for 16, 22.05 and 24kHz sample input\n");
00686 fprintf (stdout,
00687 "\t8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160\n");
00688 fprintf (stdout,
00689 "\n\tAllowable bitrates for 32, 44.1 and 48kHz sample input\n");
00690 fprintf (stdout,
00691 "\t32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384\n");
00692 exit (1);
00693 }
00694
00695
00696
00697
00698 void short_usage (void)
00699 {
00700
00701 fprintf (stderr, "tooLAME version %s\n (http://toolame.sourceforge.net)\n",
00702 toolameversion);
00703 fprintf (stderr, "MPEG Audio Layer II encoder\n\n");
00704 fprintf (stderr, "USAGE: %s [options] <infile> [outfile]\n\n", programName);
00705 fprintf (stderr, "Try \"%s -h\" for more information.\n", programName);
00706 exit (0);
00707 }
00708
00709
00710
00711
00712 void proginfo (void)
00713 {
00714
00715 fprintf (stderr,
00716 "\ntooLAME version 0.2g (http://toolame.sourceforge.net)\n");
00717 fprintf (stderr, "MPEG Audio Layer II encoder\n\n");
00718 }
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754 void parse_args (int argc, char **argv, frame_info * frame, int *psy,
00755 unsigned long *num_samples, char inPath[MAX_NAME_SIZE],
00756 char outPath[MAX_NAME_SIZE])
00757 {
00758 FLOAT srate;
00759 int brate;
00760 frame_header *header = frame->header;
00761 int err = 0, i = 0;
00762 long samplerate;
00763
00764
00765 inPath[0] = '\0';
00766 outPath[0] = '\0';
00767 header->lay = DFLT_LAY;
00768 switch (DFLT_MOD) {
00769 case 's':
00770 header->mode = MPG_MD_STEREO;
00771 header->mode_ext = 0;
00772 break;
00773 case 'd':
00774 header->mode = MPG_MD_DUAL_CHANNEL;
00775 header->mode_ext = 0;
00776 break;
00777
00778
00779 case 'j':
00780 header->mode = MPG_MD_JOINT_STEREO;
00781 header->mode_ext = 2;
00782 break;
00783 case 'm':
00784 header->mode = MPG_MD_MONO;
00785 header->mode_ext = 0;
00786 break;
00787 default:
00788 fprintf (stderr, "%s: Bad mode dflt %c\n", programName, DFLT_MOD);
00789 abort ();
00790 }
00791 *psy = DFLT_PSY;
00792 if ((header->sampling_frequency =
00793 toolame_SmpFrqIndex ((long) (1000 * DFLT_SFQ), &header->version)) < 0) {
00794 fprintf (stderr, "%s: bad sfrq default %.2f\n", programName, DFLT_SFQ);
00795 abort ();
00796 }
00797 header->bitrate_index = 14;
00798 brate = 0;
00799 switch (DFLT_EMP) {
00800 case 'n':
00801 header->emphasis = 0;
00802 break;
00803 case '5':
00804 header->emphasis = 1;
00805 break;
00806 case 'c':
00807 header->emphasis = 3;
00808 break;
00809 default:
00810 fprintf (stderr, "%s: Bad emph dflt %c\n", programName, DFLT_EMP);
00811 abort ();
00812 }
00813 header->copyright = 0;
00814 header->original = 0;
00815 header->error_protection = FALSE;
00816 header->dab_extension = 0;
00817
00818
00819 while (++i < argc && err == 0) {
00820 char c, *token, *arg, *nextArg;
00821 int argUsed;
00822
00823 token = argv[i];
00824 if (*token++ == '-') {
00825 if (i + 1 < argc)
00826 nextArg = argv[i + 1];
00827 else
00828 nextArg = "";
00829 argUsed = 0;
00830 if (!*token) {
00831
00832 if (inPath[0] == '\0')
00833 strncpy (inPath, argv[i], MAX_NAME_SIZE);
00834 else if (outPath[0] == '\0')
00835 strncpy (outPath, argv[i], MAX_NAME_SIZE);
00836 }
00837 while ((c = *token++)) {
00838 if (*token )
00839 arg = token;
00840 else
00841 arg = nextArg;
00842 switch (c) {
00843 case 'm':
00844 argUsed = 1;
00845 if (*arg == 's') {
00846 header->mode = MPG_MD_STEREO;
00847 header->mode_ext = 0;
00848 } else if (*arg == 'd') {
00849 header->mode = MPG_MD_DUAL_CHANNEL;
00850 header->mode_ext = 0;
00851 } else if (*arg == 'j') {
00852 header->mode = MPG_MD_JOINT_STEREO;
00853 } else if (*arg == 'm') {
00854 header->mode = MPG_MD_MONO;
00855 header->mode_ext = 0;
00856 } else {
00857 fprintf (stderr, "%s: -m mode must be s/d/j/m not %s\n",
00858 programName, arg);
00859 err = 1;
00860 }
00861 break;
00862 case 'p':
00863 *psy = atoi (arg);
00864 argUsed = 1;
00865 break;
00866
00867 case 's':
00868 argUsed = 1;
00869 srate = atof (arg);
00870
00871 samplerate = (long) ((1000.0 * srate) + 0.5);
00872 if ((header->sampling_frequency =
00873 toolame_SmpFrqIndex ((long) samplerate, &header->version)) < 0)
00874 err = 1;
00875 break;
00876
00877 case 'b':
00878 argUsed = 1;
00879 brate = atoi (arg);
00880 break;
00881 case 'd':
00882 argUsed = 1;
00883 if (*arg == 'n')
00884 header->emphasis = 0;
00885 else if (*arg == '5')
00886 header->emphasis = 1;
00887 else if (*arg == 'c')
00888 header->emphasis = 3;
00889 else {
00890 fprintf (stderr, "%s: -d emp must be n/5/c not %s\n", programName,
00891 arg);
00892 err = 1;
00893 }
00894 break;
00895 case 'D':
00896 argUsed = 1;
00897 header->dab_length = atoi (arg);
00898 header->error_protection = TRUE;
00899 header->dab_extension = 2;
00900 glopts.dab = TRUE;
00901 break;
00902 case 'c':
00903 header->copyright = 1;
00904 break;
00905 case 'o':
00906 header->original = 1;
00907 break;
00908 case 'e':
00909 header->error_protection = TRUE;
00910 break;
00911 case 'f':
00912 *psy = 0;
00913
00914
00915 glopts.usepsy = FALSE;
00916 break;
00917 case 'r':
00918 glopts.usepadbit = FALSE;
00919 header->padding = 0;
00920 break;
00921 case 'q':
00922 argUsed = 1;
00923 glopts.quickmode = TRUE;
00924 glopts.usepsy = TRUE;
00925 glopts.quickcount = atoi (arg);
00926 if (glopts.quickcount == 0) {
00927
00928 glopts.usepsy = FALSE;
00929 glopts.quickcount = FALSE;
00930 }
00931 break;
00932 case 'a':
00933 glopts.downmix = TRUE;
00934 header->mode = MPG_MD_MONO;
00935 header->mode_ext = 0;
00936 break;
00937 case 'x':
00938 glopts.byteswap = TRUE;
00939 break;
00940 case 'v':
00941 argUsed = 1;
00942 glopts.vbr = TRUE;
00943 glopts.vbrlevel = atof (arg);
00944 glopts.usepadbit = FALSE;
00945 header->padding = 0;
00946
00947
00948
00949 header->mode = MPG_MD_STEREO;
00950 header->mode_ext = 0;
00951 break;
00952 case 'l':
00953 argUsed = 1;
00954 glopts.athlevel = atof(arg);
00955 break;
00956 case 'h':
00957 usage ();
00958 break;
00959 case 'g':
00960 glopts.channelswap = TRUE;
00961 break;
00962 case 't':
00963 argUsed = 1;
00964 glopts.verbosity = atoi (arg);
00965 break;
00966 default:
00967 fprintf (stderr, "%s: unrec option %c\n", programName, c);
00968 err = 1;
00969 break;
00970 }
00971 if (argUsed) {
00972 if (arg == token)
00973 token = "";
00974 else
00975 ++i;
00976 arg = "";
00977 argUsed = 0;
00978 }
00979 }
00980 } else {
00981 if (inPath[0] == '\0')
00982 strcpy (inPath, argv[i]);
00983 else if (outPath[0] == '\0')
00984 strcpy (outPath, argv[i]);
00985 else {
00986 fprintf (stderr, "%s: excess arg %s\n", programName, argv[i]);
00987 err = 1;
00988 }
00989 }
00990 }
00991
00992 if (header->dab_extension) {
00993
00994
00995
00996
00997 if (brate / (header->mode == MPG_MD_MONO ? 1 : 2) >= 56)
00998 header->dab_extension = 4;
00999 }
01000
01001
01002 if (err || inPath[0] == '\0')
01003 usage ();
01004
01005 if (outPath[0] == '\0') {
01006
01007 new_ext (inPath, DFLT_EXT, outPath);
01008 }
01009
01010 if (!strcmp (inPath, "-")) {
01011 musicin = stdin;
01012 *num_samples = MAX_U_32_NUM;
01013 } else {
01014 if ((musicin = fopen (inPath, "rb")) == NULL) {
01015 fprintf (stderr, "Could not find \"%s\".\n", inPath);
01016 exit (1);
01017 }
01018 parse_input_file (musicin, inPath, header, num_samples);
01019 }
01020
01021
01022 if (brate == 0)
01023 brate = bitrate[header->version][10];
01024
01025
01026 if ((header->bitrate_index = toolame_BitrateIndex (brate, header->version)) < 0)
01027 err = 1;
01028
01029
01030
01031
01032 open_bit_stream_w (&bs, outPath, BUFFER_SIZE);
01033
01034 }
01035
01036
01037 void smr_dump(double smr[2][SBLIMIT], int nch) {
01038 int ch, sb;
01039
01040 fprintf(stdout,"SMR:");
01041 for (ch = 0;ch<nch; ch++) {
01042 if (ch==1)
01043 fprintf(stdout," ");
01044 for (sb=0;sb<SBLIMIT;sb++)
01045 fprintf(stdout,"%3.0f ",smr[ch][sb]);
01046 fprintf(stdout,"\n");
01047 }
01048 }