1 /*
2 ** Copyright (C) 2007-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #else
27 #include "sf_unistd.h"
28 #endif
29
30 #include <math.h>
31
32 #include <sndfile.h>
33
34 #include "utils.h"
35 #include "dft_cmp.h"
36
37 #define SAMPLE_RATE 16000
38 #define DATA_LENGTH (SAMPLE_RATE)
39
40 static const char CMP_TEST_PREFIX[] = "cmp" ;
41
42 static float data_out [DATA_LENGTH] ;
43
44 static inline float
max_float(float a,float b)45 max_float (float a, float b)
46 { return a > b ? a : b ;
47 } /* max_float */
48
49 static void
vorbis_test(void)50 vorbis_test (void)
51 { static float float_data [DFT_DATA_LENGTH] ;
52 const char * filename = "vorbis_test.oga" ;
53 SNDFILE * file ;
54 SF_INFO sfinfo ;
55 float max_abs = 0.0 ;
56 unsigned k ;
57
58 get_unique_test_name (&filename, CMP_TEST_PREFIX) ;
59
60 print_test_name ("vorbis_test", filename) ;
61
62 /* Generate float data. */
63 gen_windowed_sine_float (float_data, ARRAY_LEN (float_data), 1.0) ;
64
65 /* Set up output file type. */
66 memset (&sfinfo, 0, sizeof (sfinfo)) ;
67 sfinfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
68 sfinfo.channels = 1 ;
69 sfinfo.samplerate = SAMPLE_RATE ;
70
71 /* Write the output file. */
72
73 /* The Vorbis encoder has a bug on PowerPC and X86-64 with sample rates
74 ** <= 22050. Increasing the sample rate to 32000 avoids triggering it.
75 ** See https://trac.xiph.org/ticket/1229
76 */
77 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
78 { const char * errstr ;
79
80 errstr = sf_strerror (NULL) ;
81 if (strstr (errstr, "Sample rate chosen is known to trigger a Vorbis") == NULL)
82 { printf ("Line %d: sf_open (SFM_WRITE) failed : %s\n", __LINE__, errstr) ;
83 dump_log_buffer (NULL) ;
84 exit (1) ;
85 } ;
86
87 printf ("\n Sample rate -> 32kHz ") ;
88 sfinfo.samplerate = 32000 ;
89
90 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
91 } ;
92
93 test_write_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
94 sf_close (file) ;
95
96 memset (float_data, 0, sizeof (float_data)) ;
97
98 /* Read the file back in again. */
99 memset (&sfinfo, 0, sizeof (sfinfo)) ;
100 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
101 test_read_float_or_die (file, 0, float_data, ARRAY_LEN (float_data), __LINE__) ;
102 sf_close (file) ;
103
104 for (k = 0 ; k < ARRAY_LEN (float_data) ; k ++)
105 max_abs = max_float (max_abs, fabsf (float_data [k])) ;
106
107 exit_if_true (max_abs > 1.023,
108 "\n\nLine %d : max_abs %f should be < 1.023.\n\n", __LINE__, max_abs) ;
109
110 puts ("ok") ;
111 unlink (filename) ;
112 } /* vorbis_test */
113
114 static void
compression_size_test(int format,const char * filename)115 compression_size_test (int format, const char * filename)
116 { /*
117 ** Encode two files, one at quality 0.3 and one at quality 0.5 and then
118 ** make sure that the quality 0.3 files is the smaller of the two.
119 */
120 char q3_fname [64] ;
121 char q6_fname [64] ;
122 char test_name [64] ;
123
124 SNDFILE *q3_file, *q6_file ;
125 SF_INFO sfinfo ;
126 int q3_size, q6_size ;
127 double quality ;
128 int k ;
129
130 get_unique_test_name (&filename, CMP_TEST_PREFIX) ;
131
132 snprintf (q3_fname, sizeof (q3_fname), "q3_%s", filename) ;
133 snprintf (q6_fname, sizeof (q6_fname), "q6_%s", filename) ;
134
135 snprintf (test_name, sizeof (test_name), "q[36]_%s", filename) ;
136 print_test_name (__func__, test_name) ;
137
138 memset (&sfinfo, 0, sizeof (sfinfo)) ;
139
140 /* Set up output file type. */
141 sfinfo.format = format ;
142 sfinfo.channels = 1 ;
143 sfinfo.samplerate = SAMPLE_RATE ;
144
145 /* Write the output file. */
146 q3_file = test_open_file_or_die (q3_fname, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
147 q6_file = test_open_file_or_die (q6_fname, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
148
149 quality = 0.3 ;
150 sf_command (q3_file, SFC_SET_VBR_ENCODING_QUALITY, &quality, sizeof (quality)) ;
151 quality = 0.6 ;
152 sf_command (q6_file, SFC_SET_VBR_ENCODING_QUALITY, &quality, sizeof (quality)) ;
153
154 for (k = 0 ; k < 5 ; k++)
155 { gen_lowpass_signal_float (data_out, ARRAY_LEN (data_out)) ;
156 test_write_float_or_die (q3_file, 0, data_out, ARRAY_LEN (data_out), __LINE__) ;
157 test_write_float_or_die (q6_file, 0, data_out, ARRAY_LEN (data_out), __LINE__) ;
158 } ;
159
160 sf_close (q3_file) ;
161 sf_close (q6_file) ;
162
163 q3_size = (int) file_length (q3_fname) ;
164 q6_size = (int) file_length (q6_fname) ;
165
166 exit_if_true (q3_size >= q6_size,
167 "\n\nLine %d : q3 size (%d) >= q6 size (%d)\n\n", __LINE__, q3_size, q6_size) ;
168
169 puts ("ok") ;
170 unlink (q3_fname) ;
171 unlink (q6_fname) ;
172 } /* compression_size_test */
173
174
175
176 int
main(int argc,char * argv[])177 main (int argc, char *argv [])
178 { int all_tests = 0, tests = 0 ;
179
180 if (argc != 2)
181 { printf (
182 "Usage : %s <test>\n"
183 " Where <test> is one of:\n"
184 " vorbis - test Ogg/Vorbis\n"
185 " flac - test FLAC\n"
186 " opus - test Opus\n"
187 " mpeg - test mpeg\n"
188 " all - perform all tests\n",
189 argv [0]) ;
190 exit (0) ;
191 } ;
192
193 if (strcmp (argv [1], "all") == 0)
194 all_tests = 1 ;
195
196 if (all_tests || strcmp (argv [1], "vorbis") == 0)
197 { if (HAVE_EXTERNAL_XIPH_LIBS)
198 { vorbis_test () ;
199 compression_size_test (SF_FORMAT_OGG | SF_FORMAT_VORBIS, "vorbis.oga") ;
200 tests ++ ;
201 }
202 else
203 puts (" No Ogg Vorbis tests because support was not compiled in.") ;
204 } ;
205
206 if (all_tests || strcmp (argv [1], "flac") == 0)
207 { if (HAVE_EXTERNAL_XIPH_LIBS)
208 { compression_size_test (SF_FORMAT_FLAC | SF_FORMAT_PCM_16, "pcm16.flac") ;
209 tests ++ ;
210 }
211 else
212 puts (" No FLAC tests because support was not compiled in.") ;
213 } ;
214
215 if (all_tests || strcmp (argv [1], "opus") == 0)
216 { if (HAVE_EXTERNAL_XIPH_LIBS)
217 { compression_size_test (SF_FORMAT_OGG | SF_FORMAT_OPUS, "opus.opus") ;
218 tests ++ ;
219 }
220 else
221 puts (" No Opus tests because support was not compiled in.") ;
222 } ;
223
224 if (all_tests || strcmp (argv [1], "mpeg") == 0)
225 { if (HAVE_MPEG)
226 { compression_size_test (SF_FORMAT_MPEG | SF_FORMAT_MPEG_LAYER_III, "mpeg.mp3") ;
227 tests ++ ;
228 }
229 else
230 puts (" No MPEG tests because support was not compiled in.") ;
231 } ;
232
233 return 0 ;
234 } /* main */
235