Debian Codegraph @

libtwolame/util.c

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