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