cmp_wav.c 8.8 KB

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