testtremor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
  4. * *
  5. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  6. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  8. * *
  9. * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
  10. * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: simple example decoder using vorbisidec
  14. ********************************************************************/
  15. /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
  16. stdout using vorbisfile. Using vorbisfile is much simpler than
  17. dealing with libvorbis. */
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include "ivorbiscodec.h"
  22. #include "ivorbisfile.h"
  23. #include "time.h"
  24. #include "windows.h"
  25. #define PROFILE
  26. #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
  27. #include <io.h>
  28. #include <fcntl.h>
  29. #endif
  30. char pcmout[4096]; /* take 4k out of the data segment, not the stack */
  31. char ref[4096]; /* take 4k out of the data segment, not the stack */
  32. char text[4096];
  33. void Output(const char *fmt, ...)
  34. {
  35. #ifdef _WIN32_WCE
  36. va_list ap;
  37. char *t = text;
  38. WCHAR uni[4096];
  39. WCHAR *u = uni;
  40. va_start(ap,fmt);
  41. vsprintf(text, fmt, ap);
  42. va_end(ap);
  43. while (*t != 0)
  44. {
  45. *u++ = (WCHAR)(*t++);
  46. }
  47. *u++ = 0;
  48. OutputDebugString(uni);
  49. #else
  50. vfprintf(stderr, fmt, ap);
  51. #endif
  52. }
  53. typedef struct
  54. {
  55. FILE *in;
  56. FILE *out;
  57. FILE *refin;
  58. FILE *refout;
  59. int max_samples;
  60. } TestParams;
  61. static DWORD run_test(void *tp)
  62. {
  63. TestParams *params = (TestParams *)tp;
  64. FILE *in = params->in;
  65. FILE *out = params->out;
  66. FILE *refin = params->refin;
  67. FILE *refout = params->refout;
  68. int max_samples = params->max_samples;
  69. OggVorbis_File vf;
  70. int eof=0;
  71. int current_section;
  72. int maxdiff = 0;
  73. int countdiffs = 0;
  74. int samples = 0;
  75. if(ov_open(in, &vf, NULL, 0) < 0) {
  76. Output("Input does not appear to be an Ogg bitstream.\n");
  77. exit(1);
  78. }
  79. /* Throw the comments plus a few lines about the bitstream we're
  80. decoding */
  81. {
  82. char **ptr=ov_comment(&vf,-1)->user_comments;
  83. vorbis_info *vi=ov_info(&vf,-1);
  84. if (out != NULL)
  85. {
  86. while(*ptr){
  87. Output("%s\n",*ptr);
  88. ++ptr;
  89. }
  90. Output("\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
  91. Output("\nDecoded length: %ld samples\n",
  92. (long)ov_pcm_total(&vf,-1));
  93. Output("Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
  94. }
  95. }
  96. while((!eof) && (max_samples > 0)){
  97. long ret=ov_read(&vf,pcmout,sizeof(pcmout),&current_section);
  98. if (ret == 0) {
  99. /* EOF */
  100. eof=1;
  101. } else if (ret < 0) {
  102. /* error in the stream. Not a problem, just reporting it in
  103. case we (the app) cares. In this case, we don't. */
  104. } else {
  105. /* we don't bother dealing with sample rate changes, etc, but
  106. you'll have to*/
  107. if (out != NULL)
  108. {
  109. fwrite(pcmout,1,ret,out);
  110. }
  111. max_samples -= ret>>1;
  112. if (refout != NULL)
  113. {
  114. fwrite(pcmout,1,ret,refout);
  115. samples += ret>>1;
  116. Output("%d", samples);
  117. }
  118. if (refin != NULL)
  119. {
  120. int i, diff;
  121. fread(ref,1,ret,refin);
  122. for (i=0; i<(ret>>1);i++)
  123. {
  124. diff = ((short *)pcmout)[i] - ((short *)ref)[i];
  125. if (diff != 0)
  126. {
  127. if (diff < 0)
  128. diff = -diff;
  129. if (diff > maxdiff)
  130. maxdiff = diff;
  131. countdiffs++;
  132. if (countdiffs < 50)
  133. {
  134. Output("samples differ: %x vs %x\n",
  135. ((unsigned short *)pcmout)[i],
  136. ((unsigned short *)ref)[i]);
  137. }
  138. else if ((countdiffs % 100) == 0)
  139. {
  140. Output("%d differences, maximum = %d\n",
  141. countdiffs, maxdiff);
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. /* cleanup */
  149. ov_clear(&vf);
  150. return 0;
  151. }
  152. static int filetimetoms(FILETIME *time)
  153. {
  154. unsigned long long l;
  155. l = ((unsigned long long)time->dwLowDateTime) + (((unsigned long long)time->dwHighDateTime)<<32);
  156. return (int)(l/10000);
  157. }
  158. char speedblock[32768];
  159. void speedtest()
  160. {
  161. int readtime;
  162. FILETIME userStartTime, userStopTime;
  163. FILETIME kernelStartTime, kernelStopTime;
  164. FILETIME exitStartTime, exitStopTime;
  165. FILETIME creationStartTime, creationStopTime;
  166. Output("Speed test: STMIA speed\n");
  167. GetThreadTimes(GetCurrentThread(),
  168. &creationStartTime,
  169. &exitStartTime,
  170. &kernelStartTime,
  171. &userStartTime);
  172. stmiaTest(speedblock, 32768, 65536);
  173. GetThreadTimes(GetCurrentThread(),
  174. &creationStopTime,
  175. &exitStopTime,
  176. &kernelStopTime,
  177. &userStopTime);
  178. readtime = filetimetoms(&userStopTime)-filetimetoms(&userStartTime);
  179. Output("Speed test complete: Timing=%g\n",
  180. ((double)readtime)/1000);
  181. Output("Speed test: STR speed\n");
  182. GetThreadTimes(GetCurrentThread(),
  183. &creationStartTime,
  184. &exitStartTime,
  185. &kernelStartTime,
  186. &userStartTime);
  187. strTest(speedblock, 32768, 65536);
  188. GetThreadTimes(GetCurrentThread(),
  189. &creationStopTime,
  190. &exitStopTime,
  191. &kernelStopTime,
  192. &userStopTime);
  193. readtime = filetimetoms(&userStopTime)-filetimetoms(&userStartTime);
  194. Output("Speed test complete: Timing=%g\n",
  195. ((double)readtime)/1000);
  196. Output("Speed test: SMULL speed\n");
  197. GetThreadTimes(GetCurrentThread(),
  198. &creationStartTime,
  199. &exitStartTime,
  200. &kernelStartTime,
  201. &userStartTime);
  202. smullTest(speedblock, 32768, 65536);
  203. GetThreadTimes(GetCurrentThread(),
  204. &creationStopTime,
  205. &exitStopTime,
  206. &kernelStopTime,
  207. &userStopTime);
  208. readtime = filetimetoms(&userStopTime)-filetimetoms(&userStartTime);
  209. Output("Speed test complete: Timing=%g\n",
  210. ((double)readtime)/1000);
  211. }
  212. int main(int argc, char *argv[]){
  213. FILE *in;
  214. FILE *out = NULL;
  215. FILE *refin = NULL;
  216. FILE *refout = NULL;
  217. int dectime, readtime;
  218. FILETIME userStartTime, userStopTime;
  219. FILETIME kernelStartTime, kernelStopTime;
  220. FILETIME exitStartTime, exitStopTime;
  221. FILETIME creationStartTime, creationStopTime;
  222. TestParams params;
  223. if (argc < 2)
  224. {
  225. Output("Syntax: testtremor <infile> [<outfile>]\n");
  226. exit(EXIT_FAILURE);
  227. }
  228. #ifdef PROFILE
  229. in = fopen(argv[1], "rb");
  230. if (in == NULL)
  231. {
  232. Output("Failed to open '%s' for input\n", argv[1]);
  233. exit(EXIT_FAILURE);
  234. }
  235. params.in = in;
  236. params.out = NULL;
  237. params.refin = NULL;
  238. params.refout = NULL;
  239. params.max_samples = 0x7FFFFFFF;
  240. Profile_init(184000, 4);
  241. run_test(&params);
  242. Profile_dump();
  243. #else
  244. in = fopen(argv[1], "rb");
  245. if (in == NULL)
  246. {
  247. Output("Failed to open '%s' for input\n", argv[1]);
  248. exit(EXIT_FAILURE);
  249. }
  250. if (argc >= 3)
  251. {
  252. out = fopen(argv[2], "wb");
  253. if (out == NULL)
  254. {
  255. Output("Failed to open '%s' for output\n", argv[2]);
  256. exit(EXIT_FAILURE);
  257. }
  258. }
  259. if (argc >= 4)
  260. {
  261. refin = fopen(argv[3], "rb");
  262. if (refin == NULL)
  263. {
  264. Output("Can't find reference file. Creating instead.\n");
  265. refout = fopen(argv[3], "wb");
  266. if (refout == NULL)
  267. {
  268. Output("Failed to open '%s' as output reference file\n", argv[3]);
  269. exit(EXIT_FAILURE);
  270. }
  271. }
  272. }
  273. Output("First test: Decode correctness\n");
  274. params.in = in;
  275. params.out = out;
  276. params.refin = refin;
  277. params.refout = refout;
  278. params.max_samples = 1*1024*1024;
  279. run_test(&params);
  280. Output("First test complete\n");
  281. if (out != NULL)
  282. fclose(out);
  283. if (refin != NULL)
  284. fclose(refin);
  285. if (refout != NULL)
  286. fclose(refout);
  287. Output("Second test: Decode speed\n");
  288. in = fopen(argv[1], "rb");
  289. if (in == NULL)
  290. {
  291. Output("Failed to open '%s' for input\n", argv[1]);
  292. exit(EXIT_FAILURE);
  293. }
  294. GetThreadTimes(GetCurrentThread(),
  295. &creationStartTime,
  296. &exitStartTime,
  297. &kernelStartTime,
  298. &userStartTime);
  299. params.in = in;
  300. params.out = NULL;
  301. params.refin = NULL;
  302. params.refout = NULL;
  303. params.max_samples = 0x7FFFFFFF;
  304. run_test(&params);
  305. GetThreadTimes(GetCurrentThread(),
  306. &creationStopTime,
  307. &exitStopTime,
  308. &kernelStopTime,
  309. &userStopTime);
  310. dectime = filetimetoms(&userStopTime)-filetimetoms(&userStartTime);
  311. Output("Second test complete: Timing=%g\n",
  312. ((double)dectime)/1000);
  313. Output("Third test: File read speed\n");
  314. in = fopen(argv[1], "rb");
  315. if (in == NULL)
  316. {
  317. Output("Failed to open '%s' for input\n", argv[1]);
  318. exit(EXIT_FAILURE);
  319. }
  320. GetThreadTimes(GetCurrentThread(),
  321. &creationStartTime,
  322. &exitStartTime,
  323. &kernelStartTime,
  324. &userStartTime);
  325. while (!feof(in))
  326. {
  327. fread(pcmout,1,4096,in);
  328. }
  329. GetThreadTimes(GetCurrentThread(),
  330. &creationStopTime,
  331. &exitStopTime,
  332. &kernelStopTime,
  333. &userStopTime);
  334. readtime = filetimetoms(&userStopTime)-filetimetoms(&userStartTime);
  335. Output("Third test complete: Timing=%g\n",
  336. ((double)readtime)/1000);
  337. Output("Adjusted decode time: Timing=%g\n",
  338. ((double)(dectime-readtime))/1000);
  339. #endif
  340. Output("Done.\n");
  341. return(0);
  342. }
  343. #ifdef _WIN32_WCE
  344. #define TESTFILE 0
  345. int WinMain(HINSTANCE h,HINSTANCE i,LPWSTR l,int n)
  346. {
  347. #if TESTFILE == 9
  348. char *argv[] = { "testtremor",
  349. "\\Storage Card\\Tremolo\\infile9.ogg",
  350. "\\Storage Card\\Tremolo\\output9.pcm",
  351. #ifdef _LOW_ACCURACY_
  352. "\\Storage Card\\Tremolo\\outputL9.ref",
  353. #else
  354. "\\Storage Card\\Tremolo\\output9.ref",
  355. #endif /* _LOW_ACCURACY_ */
  356. NULL };
  357. #endif
  358. #if TESTFILE == 2
  359. char *argv[] = { "testtremor",
  360. "\\Storage Card\\Tremolo\\infile2.ogg",
  361. "\\Storage Card\\Tremolo\\output2.pcm",
  362. #ifdef _LOW_ACCURACY_
  363. "\\Storage Card\\Tremolo\\outputL2.ref",
  364. #else
  365. "\\Storage Card\\Tremolo\\output2.ref",
  366. #endif /* _LOW_ACCURACY_ */
  367. NULL };
  368. #endif
  369. #if TESTFILE == 0
  370. char *argv[] = { "testtremor",
  371. "\\Storage Card\\Tremolo\\infile.ogg",
  372. "\\Storage Card\\Tremolo\\output.pcm",
  373. #ifdef _LOW_ACCURACY_
  374. "\\Storage Card\\Tremolo\\outputL.ref",
  375. #else
  376. "\\Storage Card\\Tremolo\\output.ref",
  377. #endif /* _LOW_ACCURACY_ */
  378. NULL };
  379. #endif
  380. return main(4, argv);
  381. }
  382. #endif