File: util.c

package info (click to toggle)
twolame 0.3.12-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,436 kB
  • ctags: 1,064
  • sloc: sh: 9,013; ansic: 8,868; xml: 510; makefile: 135
file content (239 lines) | stat: -rw-r--r-- 7,086 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 *	TwoLAME: an optimized MPEG Audio Layer Two encoder
 *
 *	Copyright (C) 2001-2004 Michael Cheng
 *	Copyright (C) 2004-2006 The TwoLAME Project
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation; either
 *	version 2.1 of the License, or (at your option) any later version.
 *
 *	This library is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *	Lesser General Public License for more details.
 *
 *	You should have received a copy of the GNU Lesser General Public
 *	License along with this library; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id: util.c 156 2007-03-20 23:57:35Z nhumfrey $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>

#include "twolame.h"
#include "common.h"
#include "util.h"


// Return string containg version number
// of this library
const char* get_twolame_version( void )
{
	const char* str = PACKAGE_VERSION;
	
	return str;
}

// Return string containg version number
// of this library
const char* get_twolame_url( void )
{
	const char* str = "http://www.twolame.org";
	
	return str;
}


/* 1: MPEG-1, 0: MPEG-2 LSF, 1995-07-11 shn */
static const int bitrate_table[2][15] = {
	{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
	{0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384}
};

//
// Returns a name string for MPEG version enumeration
//
const char* twolame_mpeg_version_name (int version)
{
	static const char *version_name[3] = { "MPEG-2 LSF", "MPEG-1", "Illegal Version" };
	if (version==0)
		return(version_name[0]);
	if (version==1)
		return(version_name[1]);
	return(version_name[2]);
}


//	Returns the index associated with a bitrate for
//	the specified version of MPEG.
//
//	Returns -1 for invalid bitrates
//
int twolame_get_bitrate_index(int bitrate, TWOLAME_MPEG_version version)
{
	int index = 0;
	int found = 0;

	// MFC sanity check.
	if (version!=0 && version!=1) {
		fprintf(stderr,"twolame_get_bitrate_index: invalid version index %i\n",version);
		return -1;
	}

	while (!found && index < 15) {
		if (bitrate_table[version][index] == bitrate)
			found = 1;
		else
			++index;
	}
	
	if (found)	return (index);
	else {
		fprintf (stderr,
			"twolame_get_bitrate_index: %d is not a legal bitrate for version '%s'\n",
			bitrate, twolame_mpeg_version_name(version));
		return -1;
	}
}

// convert samp frq in Hz to index
// legal rates 16000, 22050, 24000, 32000, 44100, 48000
//	-1 is returned for invalid samplerates
int twolame_get_samplerate_index (long sample_rate)
{

	switch (sample_rate) {
		case 44100L: return 0;
		case 48000L: return 1;
		case 32000L: return 2;
		case 22050L: return 0;
		case 24000L: return 1;
		case 16000L: return 2;
	}

	// Invalid choice of samplerate 
	fprintf (stderr, "twolame_get_samplerate_index: %ld is not a legal sample rate\n", sample_rate);
	return -1;
}


// Return the MPEG Version to use for the specified samplerate
//	-1 is returned for invalid samplerates
int twolame_get_version_for_samplerate (long sample_rate)
{

	switch (sample_rate) {
		case 48000L: return TWOLAME_MPEG1;
		case 44100L: return TWOLAME_MPEG1;
		case 32000L: return TWOLAME_MPEG1;
		case 24000L: return TWOLAME_MPEG2;
		case 22050L: return TWOLAME_MPEG2;
		case 16000L: return TWOLAME_MPEG2;
	}

	// Invalid choice of samplerate 
	fprintf (stderr, "twolame_get_version_for_samplerate: %ld is not a legal sample rate\n", sample_rate);
	return -1;
}


// Get the number of bytes per frame, for current settings
int twolame_get_framelength( twolame_options *glopts )
{
	int bytes = 144 * (glopts->bitrate*1000) / glopts->samplerate_out;
	
	if (glopts->padding)
		bytes++;
		
	return bytes;
}



// Print the library version and 
//	encoder parameter settings to STDERR
void twolame_print_config(twolame_options *glopts)
{
	FILE* fd = stderr;

	// Are we being silent ?
	if (glopts->verbosity<=0) return; 
	


	// Are we being brief ?
	if (glopts->verbosity==1) {
	
		fprintf(fd, "LibTwoLame version %s (%s)\n", get_twolame_version(), get_twolame_url());
		fprintf (fd, "Encoding as %dHz, ", twolame_get_out_samplerate(glopts));
		fprintf (fd, "%d kbps, ", twolame_get_bitrate(glopts) );
		if (twolame_get_VBR(glopts))	fprintf (fd, "VBR, " );
		else							fprintf (fd, "CBR, " );
		fprintf (fd, "%s Layer II\n", twolame_get_version_name(glopts));
		
	} else {
	
		fprintf (fd, "---------------------------------------------------------\n");
		fprintf (fd, "LibTwoLame %s (%s)\n", get_twolame_version(), get_twolame_url());
		fprintf (fd, "Input : %d Hz, %d channels\n",
					twolame_get_in_samplerate(glopts),
					twolame_get_num_channels(glopts));
		fprintf (fd, "Output: %d Hz, %s\n",
					twolame_get_out_samplerate(glopts), 
					twolame_get_mode_name(glopts));
		fprintf (fd, "%d kbps ", twolame_get_bitrate(glopts) );
		if (twolame_get_VBR(glopts))	fprintf (fd, "VBR " );
		else							fprintf (fd, "CBR " );
		fprintf (fd, "%s Layer II ", twolame_get_version_name(glopts));
		fprintf (fd, "psycho model=%d \n", twolame_get_psymodel(glopts));
		
		fprintf (fd, "[De-emph:%s     Copyright:%s    Original:%s]\n",
		((twolame_get_emphasis(glopts)) ? "On " : "Off"),
		((twolame_get_copyright(glopts)) ? "Yes" : "No "),
		((twolame_get_original(glopts)) ? "Yes" : "No "));
		
		fprintf (fd, "[Padding:%s  CRC:%s          Energy:%s  ]\n",
		((twolame_get_padding(glopts)) ? "Normal" : "Off   "),
		((twolame_get_error_protection(glopts)) ? "On " : "Off"),
		((twolame_get_energy_levels(glopts)) ? "On " : "Off")); 
		
		if (glopts->verbosity>=3) {
			if (twolame_get_VBR(glopts)) {
				fprintf (fd, " - VBR Enabled. Using MNR boost of %f\n", twolame_get_VBR_level(glopts));
				fprintf (fd, " - VBR bitrate index limits [%i -> %i]\n", glopts->lower_index, glopts->upper_index);
			}
			
			fprintf(fd," - ATH adjustment %f\n", twolame_get_ATH_level(glopts));
			if (twolame_get_num_ancillary_bits(glopts)) 
				fprintf(fd," - Reserving %i ancillary bits\n", twolame_get_num_ancillary_bits(glopts));
			
			if (twolame_get_scale(glopts)!=1.0f)
				fprintf(fd," - Scaling audio by %f\n", twolame_get_scale(glopts));
			if (twolame_get_scale_left(glopts)!=1.0f)
				fprintf(fd," - Scaling left channel by %f\n", twolame_get_scale_left(glopts));
			if (twolame_get_scale_right(glopts)!=1.0f)
				fprintf(fd," - Scaling right channel by %f\n", twolame_get_scale_right(glopts));

			//if (glopts->num_channels_in == 2 && glopts->num_channels_out == 1 ) {
			//	fprintf(fd, " - Downmixing from stereo to mono.\n");
			//} else if (glopts->num_channels_in == 1 && glopts->num_channels_out == 2 ) {
			//	fprintf(fd, " - Upmixing from mono to stereo.\n");
			//}
		}
		
		fprintf (fd, "---------------------------------------------------------\n");
		
	}
}


// vim:ts=4:sw=4:nowrap: