1 /*
2  *	TwoLAME: an optimized MPEG Audio Layer Two encoder
3  *
4  *	Copyright (C) 2001-2004 Michael Cheng
5  *	Copyright (C) 2004-2006 The TwoLAME Project
6  *
7  *	This library is free software; you can redistribute it and/or
8  *	modify it under the terms of the GNU Lesser General Public
9  *	License as published by the Free Software Foundation; either
10  *	version 2.1 of the License, or (at your option) any later version.
11  *
12  *	This library is distributed in the hope that it will be useful,
13  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *	Lesser General Public License for more details.
16  *
17  *	You should have received a copy of the GNU Lesser General Public
18  *	License along with this library; if not, write to the Free Software
19  *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  *  $Id$
22  *
23  */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29 #include <assert.h>
30 
31 #include "twolame.h"
32 #include "common.h"
33 #include "util.h"
34 
35 
36 // Return string containg version number
37 // of this library
get_twolame_version(void)38 const char *get_twolame_version(void)
39 {
40     const char *str = PACKAGE_VERSION;
41 
42     return str;
43 }
44 
45 // Return string containg version number
46 // of this library
get_twolame_url(void)47 const char *get_twolame_url(void)
48 {
49     const char *str = "http://www.twolame.org";
50 
51     return str;
52 }
53 
54 
55 /* 1: MPEG-1, 0: MPEG-2 LSF, 1995-07-11 shn */
56 static const int bitrate_table[2][15] = {
57     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
58     {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384}
59 };
60 
61 //
62 // Returns a name string for MPEG version enumeration
63 //
twolame_mpeg_version_name(int version)64 const char *twolame_mpeg_version_name(int version)
65 {
66     static const char *version_name[3] = { "MPEG-2 LSF", "MPEG-1", "Illegal Version" };
67     if (version == 0)
68         return (version_name[0]);
69     if (version == 1)
70         return (version_name[1]);
71     return (version_name[2]);
72 }
73 
74 
75 //  Returns the index associated with a bitrate for
76 //  the specified version of MPEG.
77 //
78 //  Returns -1 for invalid bitrates
79 //
twolame_get_bitrate_index(int bitrate,TWOLAME_MPEG_version version)80 int twolame_get_bitrate_index(int bitrate, TWOLAME_MPEG_version version)
81 {
82     int index = 0;
83     int found = 0;
84 
85     // MFC sanity check.
86     if (version != 0 && version != 1) {
87         fprintf(stderr, "twolame_get_bitrate_index: invalid version index %i\n", version);
88         return -1;
89     }
90 
91     while (!found && index < 15) {
92         if (bitrate_table[version][index] == bitrate)
93             found = 1;
94         else
95             ++index;
96     }
97 
98     if (found)
99         return (index);
100     else {
101         fprintf(stderr,
102                 "twolame_get_bitrate_index: %d is not a legal bitrate for version '%s'\n",
103                 bitrate, twolame_mpeg_version_name(version));
104         return -1;
105     }
106 }
107 
108 // convert samp frq in Hz to index
109 // legal rates 16000, 22050, 24000, 32000, 44100, 48000
110 //  -1 is returned for invalid samplerates
twolame_get_samplerate_index(long sample_rate)111 int twolame_get_samplerate_index(long sample_rate)
112 {
113 
114     switch (sample_rate) {
115     case 44100L:
116         return 0;
117     case 48000L:
118         return 1;
119     case 32000L:
120         return 2;
121     case 22050L:
122         return 0;
123     case 24000L:
124         return 1;
125     case 16000L:
126         return 2;
127     }
128 
129     // Invalid choice of samplerate
130     fprintf(stderr, "twolame_get_samplerate_index: %ld is not a legal sample rate\n", sample_rate);
131     return -1;
132 }
133 
134 
135 // Return the MPEG Version to use for the specified samplerate
136 //  -1 is returned for invalid samplerates
twolame_get_version_for_samplerate(long sample_rate)137 int twolame_get_version_for_samplerate(long sample_rate)
138 {
139 
140     switch (sample_rate) {
141     case 48000L:
142         return TWOLAME_MPEG1;
143     case 44100L:
144         return TWOLAME_MPEG1;
145     case 32000L:
146         return TWOLAME_MPEG1;
147     case 24000L:
148         return TWOLAME_MPEG2;
149     case 22050L:
150         return TWOLAME_MPEG2;
151     case 16000L:
152         return TWOLAME_MPEG2;
153     }
154 
155     // Invalid choice of samplerate
156     fprintf(stderr, "twolame_get_version_for_samplerate: %ld is not a legal sample rate\n",
157             sample_rate);
158     return -1;
159 }
160 
161 
162 // Get the number of bytes per frame, for current settings
twolame_get_framelength(twolame_options * glopts)163 int twolame_get_framelength(twolame_options * glopts)
164 {
165     int bytes = 144 * (glopts->bitrate * 1000) / glopts->samplerate_out;
166 
167     if (glopts->padding)
168         bytes++;
169 
170     return bytes;
171 }
172 
173 
174 
175 // Print the library version and
176 //  encoder parameter settings to STDERR
twolame_print_config(twolame_options * glopts)177 void twolame_print_config(twolame_options * glopts)
178 {
179     FILE *fd = stderr;
180 
181     // Are we being silent ?
182     if (glopts->verbosity <= 0)
183         return;
184 
185 
186 
187     // Are we being brief ?
188     if (glopts->verbosity == 1) {
189 
190         fprintf(fd, "LibTwoLame version %s (%s)\n", get_twolame_version(), get_twolame_url());
191         fprintf(fd, "Encoding as %dHz, ", twolame_get_out_samplerate(glopts));
192         fprintf(fd, "%d kbps, ", twolame_get_bitrate(glopts));
193         if (twolame_get_VBR(glopts))
194             fprintf(fd, "VBR, ");
195         else
196             fprintf(fd, "CBR, ");
197         fprintf(fd, "%s Layer II\n", twolame_get_version_name(glopts));
198 
199     } else {
200 
201         fprintf(fd, "---------------------------------------------------------\n");
202         fprintf(fd, "LibTwoLame %s (%s)\n", get_twolame_version(), get_twolame_url());
203         fprintf(fd, "Input : %d Hz, %d channels\n",
204                 twolame_get_in_samplerate(glopts), twolame_get_num_channels(glopts));
205         fprintf(fd, "Output: %d Hz, %s\n",
206                 twolame_get_out_samplerate(glopts), twolame_get_mode_name(glopts));
207         fprintf(fd, "%d kbps ", twolame_get_bitrate(glopts));
208         if (twolame_get_VBR(glopts))
209             fprintf(fd, "VBR ");
210         else
211             fprintf(fd, "CBR ");
212         fprintf(fd, "%s Layer II ", twolame_get_version_name(glopts));
213         fprintf(fd, "psycho model=%d \n", twolame_get_psymodel(glopts));
214 
215         fprintf(fd, "[De-emph:%s     Copyright:%s    Original:%s]\n",
216                 ((twolame_get_emphasis(glopts)) ? "On " : "Off"),
217                 ((twolame_get_copyright(glopts)) ? "Yes" : "No "),
218                 ((twolame_get_original(glopts)) ? "Yes" : "No "));
219 
220         fprintf(fd, "[Padding:%s  CRC:%s          Energy:%s  ]\n",
221                 ((twolame_get_padding(glopts)) ? "Normal" : "Off   "),
222                 ((twolame_get_error_protection(glopts)) ? "On " : "Off"),
223                 ((twolame_get_energy_levels(glopts)) ? "On " : "Off"));
224 
225         if (glopts->verbosity >= 3) {
226             if (twolame_get_VBR(glopts)) {
227                 fprintf(fd, " - VBR Enabled. Using MNR boost of %f\n",
228                         twolame_get_VBR_level(glopts));
229                 fprintf(fd, " - VBR bitrate index limits [%i -> %i]\n", glopts->lower_index,
230                         glopts->upper_index);
231             }
232 
233             fprintf(fd, " - ATH adjustment %f\n", twolame_get_ATH_level(glopts));
234             if (twolame_get_num_ancillary_bits(glopts))
235                 fprintf(fd, " - Reserving %i ancillary bits\n",
236                         twolame_get_num_ancillary_bits(glopts));
237 
238             if (twolame_get_scale(glopts) != 1.0f)
239                 fprintf(fd, " - Scaling audio by %f\n", twolame_get_scale(glopts));
240             if (twolame_get_scale_left(glopts) != 1.0f)
241                 fprintf(fd, " - Scaling left channel by %f\n", twolame_get_scale_left(glopts));
242             if (twolame_get_scale_right(glopts) != 1.0f)
243                 fprintf(fd, " - Scaling right channel by %f\n", twolame_get_scale_right(glopts));
244 
245             // if (glopts->num_channels_in == 2 && glopts->num_channels_out == 1 ) {
246             // fprintf(fd, " - Downmixing from stereo to mono.\n");
247             // } else if (glopts->num_channels_in == 1 && glopts->num_channels_out == 2 ) {
248             // fprintf(fd, " - Upmixing from mono to stereo.\n");
249             // }
250         }
251 
252         fprintf(fd, "---------------------------------------------------------\n");
253 
254     }
255 }
256 
257 
258 // vim:ts=4:sw=4:nowrap:
259