Debian Codegraph @

Xpjproject/tests/pjsua/tools/cmp_wav.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pjmedia.h>
20 #include <pjlib-util.h>
21 #include <pjlib.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #define app_perror(a,b,c) printf("%s: %s (%d)", a, b, c)
26
27
28 /* For logging purpose. */
29 #define THIS_FILE "cmp_wav.c"
30 #define BYTES_PER_FRAME 512
31
32 static const char *desc =
33 " FILE \n"
34 " \n"
35 " cmp_wav.c \n"
36 " \n"
37 " PURPOSE \n"
38 " \n"
39 " Compare two WAV files. \n"
40 " \n"
41 " USAGE \n"
42 " \n"
43 " cmp_wav ORIGINAL_WAV DEGRADED_WAV [TIME] [DETAIL] \n"
44 " \n"
45 " ORIGINAL_WAV The original WAV file as reference. \n"
46 " DEGRADED_WAV The degraded WAV file. \n"
47 " TIME Compare only some part of the files \n"
48 " (in ms, since the beginning). \n"
49 " Specify 0 (default) to compare the whole time. \n"
50 " DETAIL Show detail result, 1 or 0 (default=0, means no)\n"
51 " \n"
52 " Both files must have same clock rate and must contain \n"
53 " uncompressed (i.e. 16bit) PCM. \n";
54
55
56 /* Sum of multiplication of corresponding samples in buf1 & buf2 */
57 double sum_mult_sig(pj_int16_t *buf1, pj_int16_t *buf2, unsigned nsamples)
58 {
59 double mag = 0;
60
61 while (nsamples--)
62 mag += (double)*buf1++ * (double)*buf2++;
63
64 return mag;
65 }
66
67
68 /*
69 * main()
70 */
71 int main(int argc, char *argv[])
72 {
73 pj_caching_pool cp;
74 pjmedia_endpt *med_endpt;
75 pj_pool_t *pool;
76 pjmedia_port *file_ori_port;
77 pjmedia_port *file_deg_port;
78 pj_status_t status;
79 unsigned first_nsamples = 0;
80 unsigned samples_compared = 0;
81
82 char buf1[BYTES_PER_FRAME];
83 char buf2[BYTES_PER_FRAME];
84
85 double ref_mag = 0;
86 double deg_mag = 0;
87 double mix_mag = 0;
88
89 int detail = 0;
90 int res_deg, res_mix, res_overall;
91
92 if (argc < 3) {
93 puts("Error: original & degraded filename required");
94 puts(desc);
95 return 1;
96 }
97
98 /* Set log level. */
99 pj_log_set_level(3);
100
101 /* Must init PJLIB first: */
102 status = pj_init();
103 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
104
105 /* Must create a pool factory before we can allocate any memory. */
106 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
107
108 /*
109 * Initialize media endpoint.
110 * This will implicitly initialize PJMEDIA too.
111 */
112 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
113 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
114
115 /* Create memory pool for our file player */
116 pool = pj_pool_create( &cp.factory, /* pool factory */
117 "wav", /* pool name. */
118 4000, /* init size */
119 4000, /* increment size */
120 NULL /* callback on error */
121 );
122
123 /* Create file media port from the original WAV file */
124 status = pjmedia_wav_player_port_create( pool, /* memory pool */
125 argv[1], /* file to play */
126 40, /* ptime. */
127 PJMEDIA_FILE_NO_LOOP, /* flags */
128 0, /* default buffer */
129 &file_ori_port/* returned port */
130 );
131 if (status != PJ_SUCCESS) {
132 app_perror(THIS_FILE, "Unable to use WAV file", status);
133 return 1;
134 }
135
136 /* Create file media port from the degraded WAV file */
137 status = pjmedia_wav_player_port_create( pool, /* memory pool */
138 argv[2], /* file to play */
139 40, /* ptime. */
140 PJMEDIA_FILE_NO_LOOP, /* flags */
141 0, /* default buffer */
142 &file_deg_port/* returned port */
143 );
144 if (status != PJ_SUCCESS) {
145 app_perror(THIS_FILE, "Unable to use WAV file", status);
146 return 1;
147 }
148
149 if (file_ori_port->info.fmt.det.aud.clock_rate !=
150 file_deg_port->info.fmt.det.aud.clock_rate)
151 {
152 app_perror(THIS_FILE, "Clock rates must be same.", PJ_EINVAL);
153 return 1;
154 }
155
156 if (argc > 3)
157 first_nsamples = atoi(argv[3]) *
158 file_ori_port->info.fmt.det.aud.clock_rate / 1000;
159
160 if (argc > 4)
161 detail = atoi(argv[4]);
162
163 while (1) {
164 pjmedia_frame f1, f2;
165
166 f1.buf = buf1;
167 f1.size = BYTES_PER_FRAME;
168 f2.buf = buf2;
169 f2.size = BYTES_PER_FRAME;
170
171 status = pjmedia_port_get_frame(file_ori_port, &f1);
172 if (status == PJ_EEOF) {
173 break;
174 } else if (status != PJ_SUCCESS) {
175 app_perror(THIS_FILE, "Error occured while reading file", status);
176 break;
177 }
178 status = pjmedia_port_get_frame(file_deg_port, &f2);
179 if (status == PJ_EEOF) {
180 break;
181 } else if (status != PJ_SUCCESS) {
182 app_perror(THIS_FILE, "Error occured while reading file", status);
183 break;
184 }
185
186 /* Calculate magnitudes */
187 ref_mag += sum_mult_sig(f1.buf, f1.buf, BYTES_PER_FRAME >> 1);
188 deg_mag += sum_mult_sig(f2.buf, f2.buf, BYTES_PER_FRAME >> 1);
189 mix_mag += sum_mult_sig(f1.buf, f2.buf, BYTES_PER_FRAME >> 1);
190
191 samples_compared += BYTES_PER_FRAME >> 1;
192 if (first_nsamples && samples_compared >= first_nsamples)
193 break;
194 }
195
196 /* Degraded magnitude compared to reference magnitude
197 */
198 res_deg = (int) (deg_mag / ref_mag * 100.0);
199 if (res_deg < 0)
200 res_deg = -1;
201 else if (res_deg >= 81)
202 res_deg = 9;
203 else
204 res_deg = pj_isqrt(res_deg);
205
206 /* Mixed magnitude (don't know what this is actually :D) compared to
207 * reference magnitude
208 */
209 res_mix = (int) (mix_mag / ref_mag * 100.0);
210 if (res_mix < 0)
211 res_mix = -1;
212 else if (res_mix >= 81)
213 res_mix = 9;
214 else
215 res_mix = pj_isqrt(res_mix);
216
217 /* Overall score.
218 * If mixed score is -1, then overall score should be -1 as well.
219 * Apply no weighting (1:1) for now.
220 */
221 if (res_mix == -1)
222 res_overall = -1;
223 else
224 res_overall = (res_mix*1 + res_deg*1) / 2;
225
226 if (detail) {
227 printf("Reference = %.0f\n", ref_mag);
228 printf("Degraded = %.0f\n", deg_mag);
229 printf("Mixed = %.0f\n", mix_mag);
230
231 printf("\n");
232
233 printf("Score 1 = %d\n", res_deg);
234 printf("Score 2 = %d\n", res_mix);
235
236 printf("\n");
237 }
238
239 printf("Overall = %d\n", res_overall);
240
241 /* Destroy file port */
242 status = pjmedia_port_destroy( file_ori_port );
243 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
244
245 status = pjmedia_port_destroy( file_deg_port );
246 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
247
248 /* Release application pool */
249 pj_pool_release( pool );
250
251 /* Destroy media endpoint. */
252 pjmedia_endpt_destroy( med_endpt );
253
254 /* Destroy pool factory */
255 pj_caching_pool_destroy( &cp );
256
257 /* Shutdown PJLIB */
258 pj_shutdown();
259
260
261 /* Done. */
262 return 0;
263 }
264