summaryrefslogtreecommitdiff
path: root/testtremor.c
blob: 3f19f6ab50f1244768126a1f9eb8b85e30f548c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE.   *
 *                                                                  *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
 *                                                                  *
 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002    *
 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
 *                                                                  *
 ********************************************************************

 function: simple example decoder using vorbisidec

 ********************************************************************/

/* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
   stdout using vorbisfile. Using vorbisfile is much simpler than
   dealing with libvorbis. */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "ivorbiscodec.h"
#include "ivorbisfile.h"
#include "time.h"

//#define PROFILE

#define WAV_FORMAT

#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
#include <io.h>
#include <fcntl.h>
#include "windows.h"
#else
typedef int DWORD;
#endif

char pcmout[4096]; /* take 4k out of the data segment, not the stack */
char ref[4096];    /* take 4k out of the data segment, not the stack */
char text[4096];

void Output(const char *fmt, ...)
{
  va_list  ap;
#ifdef _WIN32_WCE
  char    *t = text;
  WCHAR    uni[4096];
  WCHAR   *u = uni;

  va_start(ap,fmt);
  vsprintf(text, fmt, ap);
  va_end(ap);

  while (*t != 0)
  {
      *u++ = (WCHAR)(*t++);
  }
  *u++ = 0;
  OutputDebugString(uni);
#else
  va_start(ap,fmt);
  vfprintf(stderr, fmt, ap);
  va_end(ap);
#endif
}

typedef struct
{
    FILE *in;
    FILE *out;
    FILE *refin;
    FILE *refout;
    int   max_samples;
} TestParams;

static DWORD run_test(void *tp)
{
  TestParams     *params      = (TestParams *)tp;
  FILE           *in          = params->in;
  FILE           *out         = params->out;
  FILE           *refin       = params->refin;
  FILE           *refout      = params->refout;
  int             max_samples = params->max_samples;
  OggVorbis_File  vf;
  int eof=0;
  int current_section;
  int maxdiff = 0;
  int countdiffs = 0;
  int samples = 0;

  if(ov_open(in, &vf, NULL, 0) < 0) {
    Output("Input does not appear to be an Ogg bitstream.\n");
    exit(1);
  }

  /* Throw the comments plus a few lines about the bitstream we're
     decoding */
  {
    char **ptr=ov_comment(&vf,-1)->user_comments;
    vorbis_info *vi=ov_info(&vf,-1);
    if (out != NULL)
    {
      while(*ptr){
        Output("%s\n",*ptr);
        ++ptr;
      }
      Output("\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
      Output("\nDecoded length: %ld samples\n",
             (long)ov_pcm_total(&vf,-1));
      Output("Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
    }
  }

  while((!eof) && (max_samples > 0)){
    long ret=ov_read(&vf,pcmout,sizeof(pcmout),&current_section);
    if (ret == 0) {
      /* EOF */
      eof=1;
    } else if (ret < 0) {
      /* error in the stream.  Not a problem, just reporting it in
	 case we (the app) cares.  In this case, we don't. */
    } else {
      /* we don't bother dealing with sample rate changes, etc, but
	 you'll have to*/
      if (out != NULL)
      {
        fwrite(pcmout,1,ret,out);
      }
      max_samples -= ret>>1;
      if (refout != NULL)
      {
        fwrite(pcmout,1,ret,refout);
        samples += ret>>1;
        //Output("%d", samples);
      }
      if (refin != NULL)
      {
        int i, diff;

        fread(ref,1,ret,refin);
        for (i=0; i<(ret>>1);i++)
        {
          diff = ((short *)pcmout)[i] - ((short *)ref)[i];
          if (diff != 0)
          {
            if (diff < 0)
              diff = -diff;
            if (diff > maxdiff)
              maxdiff = diff;
            countdiffs++;
            if (countdiffs < 50)
            {
              Output("samples differ: %x vs %x\n",
                     ((unsigned short *)pcmout)[i],
                     ((unsigned short *)ref)[i]);
            }
            else if ((countdiffs % 100) == 0)
            {
              Output("%d differences, maximum = %d\n",
                     countdiffs, maxdiff);
            }
          }
        }
      }
    }
  }

  /* cleanup */
  ov_clear(&vf);

  return 0;
}

#ifdef _WIN32
static int filetimetoms(FILETIME *time)
{
  unsigned long long l;

  l = ((unsigned long long)time->dwLowDateTime) + (((unsigned long long)time->dwHighDateTime)<<32);

  return (int)(l/10000);
}

char speedblock[32768];
void speedtest()
{
  int      readtime;
  FILETIME userStartTime, userStopTime;
  FILETIME kernelStartTime, kernelStopTime;
  FILETIME exitStartTime, exitStopTime;
  FILETIME creationStartTime, creationStopTime;

  Output("Speed test: STMIA speed\n");

  GetThreadTimes(GetCurrentThread(),
                 &creationStartTime,
                 &exitStartTime,
                 &kernelStartTime,
                 &userStartTime);
  stmiaTest(speedblock, 32768, 65536);
  GetThreadTimes(GetCurrentThread(),
                 &creationStopTime,
                 &exitStopTime,
                 &kernelStopTime,
                 &userStopTime);
  readtime = filetimetoms(&userStopTime)-filetimetoms(&userStartTime);
  Output("Speed test complete: Timing=%g\n",
          ((double)readtime)/1000);

  Output("Speed test: STR speed\n");

  GetThreadTimes(GetCurrentThread(),
                 &creationStartTime,
                 &exitStartTime,
                 &kernelStartTime,
                 &userStartTime);
  strTest(speedblock, 32768, 65536);
  GetThreadTimes(GetCurrentThread(),
                 &creationStopTime,
                 &exitStopTime,
                 &kernelStopTime,
                 &userStopTime);
  readtime = filetimetoms(&userStopTime)-filetimetoms(&userStartTime);
  Output("Speed test complete: Timing=%g\n",
          ((double)readtime)/1000);

  Output("Speed test: SMULL speed\n");

  GetThreadTimes(GetCurrentThread(),
                 &creationStartTime,
                 &exitStartTime,
                 &kernelStartTime,
                 &userStartTime);
  smullTest(speedblock, 32768, 65536);
  GetThreadTimes(GetCurrentThread(),
                 &creationStopTime,
                 &exitStopTime,
                 &kernelStopTime,
                 &userStopTime);
  readtime = filetimetoms(&userStopTime)-filetimetoms(&userStartTime);
  Output("Speed test complete: Timing=%g\n",
          ((double)readtime)/1000);
}
#endif

int main(int argc, char *argv[]){
  FILE       *in;
  FILE       *out = NULL;
  FILE       *refin = NULL;
  FILE       *refout = NULL;
  int         dectime, readtime;
#ifdef _WIN32
  FILETIME    userStartTime, userStopTime;
  FILETIME    kernelStartTime, kernelStopTime;
  FILETIME    exitStartTime, exitStopTime;
  FILETIME    creationStartTime, creationStopTime;
#else
  clock_t     startTime, stopTime;
#endif
  TestParams  params;

  if (argc < 2)
  {
    Output("Syntax: testtremor <infile> [<outfile>]\n");
    exit(EXIT_FAILURE);
  }

#ifdef PROFILE
  in = fopen(argv[1], "rb");
  if (in == NULL)
  {
    Output("Failed to open '%s' for input\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  params.in          = in;
  params.out         = NULL;
  params.refin       = NULL;
  params.refout      = NULL;
  params.max_samples = 0x7FFFFFFF;
  Profile_init(184000, 4);
  run_test(&params);
  Profile_dump();
#else
  in = fopen(argv[1], "rb");
  if (in == NULL)
  {
    Output("Failed to open '%s' for input\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  if (argc >= 3)
  {
    out = fopen(argv[2], "wb");
    if (out == NULL)
    {
      Output("Failed to open '%s' for output\n", argv[2]);
      exit(EXIT_FAILURE);
    }
#ifdef WAV_FORMAT
    { unsigned int rate = 44100, channels=2, bps=16, drate, dblock;
      fprintf(out, "RIFF%c%c%c%c",0,0,0,0); /* RIFF, chunksize */
      fprintf(out, "WAVEfmt %c%c%c%c",16,0,0,0); /* WAVEfmt, chunksize */;
      fprintf(out, "%c%c%c%c", 1,0, channels,0); /* fmt(PCM), channels) */
      fprintf(out, "%c%c%c%c", /* Sampling rate (blocks per second) */
              rate,rate>>8,rate>>16,rate>>24);
      drate = (bps>>3)*channels*rate;
      fprintf(out, "%c%c%c%c", /* data rate */
              drate,drate>>8,drate>>16,drate>>24);
      dblock = (bps>>3)*channels;
      fprintf(out, "%c%c", /* data block size(bytes) */
              dblock,dblock>>8);
      fprintf(out, "%c%c", /* bps */
              bps,bps>>8);
      fprintf(out, "data%c%c%c%c",0,0,0,0); /* sample header */
    }
#endif
  }

  if (argc >= 4)
  {
    refin = fopen(argv[3], "rb");
    if (refin == NULL)
    {
      Output("Can't find reference file. Creating instead.\n");
      refout = fopen(argv[3], "wb");
      if (refout == NULL)
      {
        Output("Failed to open '%s' as output reference file\n", argv[3]);
        exit(EXIT_FAILURE);
      }
#ifdef WAV_FORMAT
    { unsigned int rate = 44100, channels=2, bps=16, drate, dblock;
      fprintf(out, "RIFF%c%c%c%c",0,0,0,0); /* RIFF, chunksize */
      fprintf(out, "WAVEfmt %c%c%c%c",16,0,0,0); /* WAVEfmt, chunksize */;
      fprintf(out, "%c%c%c%c", 1,0, channels,0); /* fmt(PCM), channels) */
      fprintf(out, "%c%c%c%c", /* Sampling rate (blocks per second) */
              rate,rate>>8,rate>>16,rate>>24);
      drate = (bps>>3)*channels*rate;
      fprintf(out, "%c%c%c%c", /* data rate */
              drate,drate>>8,drate>>16,drate>>24);
      dblock = (bps>>3)*channels;
      fprintf(out, "%c%c", /* data block size(bytes) */
              dblock,dblock>>8);
      fprintf(out, "%c%c", /* bps */
              bps,bps>>8);
      fprintf(out, "data%c%c%c%c",0,0,0,0); /* sample header */
    }
#endif
    }
  }

  Output("First test: Decode correctness\n");
  params.in          = in;
  params.out         = out;
  params.refin       = refin;
  params.refout      = refout;
  params.max_samples = 0x7FFFFFFF;//1*1024*1024;
  run_test(&params);
  Output("First test complete\n");
  if (out != NULL)
    fclose(out);
  if (refin != NULL)
    fclose(refin);
  if (refout != NULL)
    fclose(refout);
  Output("Second test: Decode speed\n");
  in = fopen(argv[1], "rb");
  if (in == NULL)
  {
    Output("Failed to open '%s' for input\n", argv[1]);
    exit(EXIT_FAILURE);
  }
#ifdef _WIN32
  GetThreadTimes(GetCurrentThread(),
                 &creationStartTime,
                 &exitStartTime,
                 &kernelStartTime,
                 &userStartTime);
#else
  startTime = clock();
#endif
  params.in          = in;
  params.out         = NULL;
  params.refin       = NULL;
  params.refout      = NULL;
  params.max_samples = 0x7FFFFFFF;
  run_test(&params);
#ifdef _WIN32
  GetThreadTimes(GetCurrentThread(),
                 &creationStopTime,
                 &exitStopTime,
                 &kernelStopTime,
                 &userStopTime);
  dectime = filetimetoms(&userStopTime)-filetimetoms(&userStartTime);
#else
  stopTime = clock();
  dectime = stopTime-startTime;
#endif
  Output("Second test complete: Timing=%g\n",
         ((double)dectime)/1000);
  Output("Third test: File read speed\n");

  in = fopen(argv[1], "rb");
  if (in == NULL)
  {
    Output("Failed to open '%s' for input\n", argv[1]);
    exit(EXIT_FAILURE);
  }
#ifdef _WIN32
  GetThreadTimes(GetCurrentThread(),
                 &creationStartTime,
                 &exitStartTime,
                 &kernelStartTime,
                 &userStartTime);
#else
  startTime = clock();
#endif
  while (!feof(in))
  {
    fread(pcmout,1,4096,in);
  }
#ifdef _WIN32
  GetThreadTimes(GetCurrentThread(),
                 &creationStopTime,
                 &exitStopTime,
                 &kernelStopTime,
                 &userStopTime);
  readtime = filetimetoms(&userStopTime)-filetimetoms(&userStartTime);
#else
  stopTime = clock();
  dectime = stopTime-startTime;
#endif
  Output("Third test complete: Timing=%g\n",
          ((double)readtime)/1000);
  Output("Adjusted decode time: Timing=%g\n",
         ((double)(dectime-readtime))/1000);
#endif
  Output("Done.\n");
  return(0);
}

#ifdef _WIN32_WCE

#define TESTFILE 1

int WinMain(HINSTANCE h,HINSTANCE i,LPWSTR l,int n)
{
#if TESTFILE == 9
  char *argv[] = { "testtremor",
                   "\\Storage Card\\Tremolo\\infile9.ogg",
                   "\\Storage Card\\Tremolo\\output9.pcm",
#ifdef _LOW_ACCURACY_
                   "\\Storage Card\\Tremolo\\outputL9.ref",
#else
                   "\\Storage Card\\Tremolo\\output9.ref",
#endif /* _LOW_ACCURACY_ */
                   NULL };
#endif
#if TESTFILE == 2
  char *argv[] = { "testtremor",
                   "\\Storage Card\\Tremolo\\infile2.ogg",
                   "\\Storage Card\\Tremolo\\output2.pcm",
#ifdef _LOW_ACCURACY_
                   "\\Storage Card\\Tremolo\\outputL2.ref",
#else
                   "\\Storage Card\\Tremolo\\output2.ref",
#endif /* _LOW_ACCURACY_ */
                   NULL };
#endif
#if TESTFILE == 0
  char *argv[] = { "testtremor",
                   "\\Storage Card\\Tremolo\\infile.ogg",
                   "\\Storage Card\\Tremolo\\output.pcm",
#ifdef _LOW_ACCURACY_
                   "\\Storage Card\\Tremolo\\outputL.ref",
#else
                   "\\Storage Card\\Tremolo\\output.ref",
#endif /* _LOW_ACCURACY_ */
                   NULL };
#endif
#if TESTFILE == 1
  char *argv[] = { "testtremor",
                   "\\My Storage\\Tremolo\\Alarm_Classic.ogg",
                   "\\My Storage\\Tremolo\\output.pcm",
#ifdef _LOW_ACCURACY_
                   "\\My Storage\\Tremolo\\outputL.ref",
#else
                   "\\My Storage\\Tremolo\\output.ref",
#endif /* _LOW_ACCURACY_ */
                   NULL };
#endif
  return main(4, argv);
}
#endif