~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/modules/data/ext/libeep/libcnt/raw3.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************************
 
2
 *                                                                              *
 
3
 * this file is part of:                                                        *
 
4
 * libeep, the project for reading and writing avr/cnt eeg and related files    *
 
5
 *                                                                              *
 
6
 ********************************************************************************
 
7
 *                                                                              *
 
8
 * LICENSE:Copyright (c) 2003-2009,                                             *
 
9
 * Advanced Neuro Technology (ANT) B.V., Enschede, The Netherlands              *
 
10
 * Max-Planck Institute for Human Cognitive & Brain Sciences, Leipzig, Germany  *
 
11
 *                                                                              *
 
12
 ********************************************************************************
 
13
 *                                                                              *
 
14
 * This library is free software; you can redistribute it and/or modify         *
 
15
 * it under the terms of the GNU Lesser General Public License as published by  *
 
16
 * the Free Software Foundation; either version 3 of the License, or            *
 
17
 * (at your option) any later version.                                          *
 
18
 *                                                                              *
 
19
 * This library is distributed WITHOUT ANY WARRANTY; even the implied warranty  *
 
20
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the              *
 
21
 * GNU Lesser General Public License for more details.                          *
 
22
 *                                                                              *
 
23
 * You should have received a copy of the GNU Lesser General Public License     *
 
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>          *
 
25
 *                                                                              *
 
26
 *******************************************************************************/
 
27
 
 
28
#include <stdlib.h>
 
29
#include <stdio.h>
 
30
#include <string.h>
 
31
#include <math.h>
 
32
#include <assert.h>
 
33
 
 
34
#include <cnt/raw3.h>
 
35
 
 
36
#ifdef COMPILE_RCS
 
37
char RCS_raw3_h[] = RCS_RAW3_H;
 
38
char RCS_raw3_c[] = "$RCSfile: raw3.c,v $ $Revision: 2415 $";
 
39
#endif
 
40
 
 
41
 
 
42
/* #ifdef RAW3_CHECK obsolete */
 
43
/* indicator flags for the citical compression methods */
 
44
short ERR_FLAG_16 = 0;
 
45
short ERR_FLAG_0  = 0;
 
46
short ERR_FLAG_EPOCH = 0;
 
47
/* #endif */
 
48
 
 
49
void    raw3_reset_ERR_FLAG_EPOCH(void) { ERR_FLAG_EPOCH = 0; }
 
50
short   raw3_ERR_FLAG_EPOCH(void)       { return ERR_FLAG_EPOCH; }
 
51
 
 
52
unsigned char CheckVerbose = 0;
 
53
void raw3_setVerbose(int onoff)
 
54
{
 
55
    assert(onoff == 0 || onoff == 1);
 
56
    CheckVerbose = (unsigned char) onoff;
 
57
}
 
58
 
 
59
/*
 
60
  known residual prediction/encoding methods
 
61
  these codes are stored in data files - never change this!
 
62
*/
 
63
 
 
64
#define RAW3_COPY  0  /* no residuals, original 16-bit values stored */
 
65
#define RAW3_TIME  1  /* 16 bit residuals from first deviation       */
 
66
#define RAW3_TIME2 2  /* 16 bit residuals from second deviation      */
 
67
#define RAW3_CHAN  3  /* 16 bit residuals from difference of first deviation */
 
68
                      /* and first dev. of neighbor channel   */
 
69
 
 
70
#define RAW3_COPY_32   8  /* the same for 32 bit sample data */
 
71
#define RAW3_TIME_32   9
 
72
#define RAW3_TIME2_32 10
 
73
#define RAW3_CHAN_32  11
 
74
 
 
75
 
 
76
 
 
77
 
 
78
/*
 
79
  find the number of bits needed to store the passed (signed!) value
 
80
*/
 
81
 
 
82
#ifdef __GNUC__
 
83
inline
 
84
#endif
 
85
int bitc(sraw_t x)
 
86
{
 
87
  static int nbits[128] = {
 
88
/*  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 */
 
89
 
 
90
    1, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
 
91
    6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
 
92
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
 
93
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
 
94
 
 
95
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
 
96
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
 
97
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
 
98
    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
 
99
  };
 
100
 
 
101
  register int y = x;
 
102
 
 
103
  /* positive number which needs the same number of bits */
 
104
  if (y < 0) y = -(y+1);
 
105
 
 
106
  if (y < 0x00000080)
 
107
    return nbits[y];
 
108
  else if (y < 0x00008000)
 
109
    return nbits[y >> 8] + 8;
 
110
  else if (y < 0x00800000)
 
111
    return nbits[y >> 16] + 16;
 
112
  else
 
113
    return  nbits[y >> 24] + 24;
 
114
}
 
115
 
 
116
int huffman_size(int *bitfreq, int n, int *nbits, int *nexcbits)
 
117
{
 
118
  int lmin = 1000000000;
 
119
  int lcur;
 
120
  int  i, j, nexc;
 
121
 
 
122
 
 
123
  /* don't store with one bit per sample point */
 
124
  bitfreq[2] += bitfreq[1]; bitfreq[1] = 0;
 
125
 
 
126
  /* determine the max bits to store */
 
127
  j = 32;
 
128
  while(bitfreq[j] == 0) j--;
 
129
  *nexcbits = j;
 
130
 
 
131
  /*
 
132
  calculate the length needed for coding in i bits with exceptions for all
 
133
  the residuals out of range
 
134
  */
 
135
  for (i = 2; i <= *nexcbits; i++) {
 
136
 
 
137
    /* all residuals out of bitrange are exceptions */
 
138
    nexc = 0;
 
139
    for (j = i + 1; j <= *nexcbits; j++) {
 
140
      nexc += bitfreq[j];
 
141
    }
 
142
 
 
143
    /*
 
144
    we need one value in bitrange for exception coding, which will cause
 
145
    some more exceptions
 
146
    e.g. for 4-bit it's every 16th value; we divide it because the lower edge value
 
147
    we are going to use for this code is less frequent than the centers
 
148
    */
 
149
 
 
150
    nexc += (n / (1 << i)) / 2;
 
151
 
 
152
    lcur = (n - 1) * i + nexc * (*nexcbits);
 
153
    /*
 
154
    lcur = (n - nexc) * i + nexc * (*nexcbits + i);
 
155
            |                 |
 
156
            |                 exceptions (code + full value)
 
157
            fitting values
 
158
    */
 
159
 
 
160
    /* note the best i for return */
 
161
    if (lcur < lmin) {
 
162
      lmin = lcur;
 
163
      *nbits = i;
 
164
    }
 
165
  }
 
166
 
 
167
  /* bits 2 bytes */
 
168
  lcur = lmin / 8;
 
169
  if (lmin % 8)
 
170
    lcur++;
 
171
 
 
172
  /*
 
173
  printf("%d\t%d\n", *nbits, *nexcbits);
 
174
  */
 
175
 
 
176
  return lcur;
 
177
}
 
178
 
 
179
/* --------------------------------------------------------------------
 
180
  raw3 compressed data vectors
 
181
 
 
182
  TIME/TIME2/CHAN:| method | nbits | nexcbits | x[0] | x[1] .. x[n-1]
 
183
                      4        4         4       16    nbits or nbits + nexcbits
 
184
 
 
185
  ..._32         :| method | nbits | nexcbits | x[0] | x[1] .. x[n-1]
 
186
                      4        6         6       32    nbits or nbits + nexcbits
 
187
 
 
188
  COPY:           | method | dummy | x[0] .. x[n-1]
 
189
                      4        4         16
 
190
 
 
191
  COPY_32:        | method | dummy | x[0] .. x[n-1]
 
192
                      4        4         32
 
193
 
 
194
*/
 
195
 
 
196
int huffman(sraw_t *in, int n, int method, int nbits, int nexcbits,
 
197
              unsigned char *out)
 
198
{
 
199
  int nout = 0;
 
200
  int nin;
 
201
  int bitin, bitout;
 
202
  sraw_t inval, loval, hival;
 
203
  char outval;
 
204
  int exc, check_exc = nbits != nexcbits;
 
205
  int nbits_1 = nbits - 1;
 
206
  int nexcbits_1 = nexcbits - 1;
 
207
  /*int i,j; */
 
208
  /*static int xxxx = 0; */
 
209
 
 
210
  static unsigned char setoutbit[8] = {
 
211
    0x01, 0x02, 0x04, 0x08,
 
212
    0x10, 0x20, 0x40, 0x80
 
213
  };
 
214
 
 
215
  static unsigned int setinbit[32] = {
 
216
    0x00000001, 0x00000002, 0x00000004, 0x00000008,
 
217
    0x00000010, 0x00000020, 0x00000040, 0x00000080,
 
218
    0x00000100, 0x00000200, 0x00000400, 0x00000800,
 
219
    0x00001000, 0x00002000, 0x00004000, 0x00008000,
 
220
    0x00010000, 0x00020000, 0x00040000, 0x00080000,
 
221
    0x00100000, 0x00200000, 0x00400000, 0x00800000,
 
222
    0x01000000, 0x02000000, 0x04000000, 0x08000000,
 
223
    0x10000000, 0x20000000, 0x40000000, 0x80000000
 
224
  };
 
225
 
 
226
  /* first 4 bits for the method */
 
227
  out[0] = method << 4;
 
228
 /*
 
229
  printf("method: %d, %d, %d\n", method, out[0]>>4, out[0]);
 
230
  fflush(stdout);
 
231
 */
 
232
  /* the rest depends on the method */
 
233
  if (method != RAW3_COPY && method != RAW3_COPY_32) {
 
234
 
 
235
    /* 32 bit versions (6 bits to store number of bits, 32 bit start val.) */
 
236
    if (method & 0x08) {
 
237
      out[0] |= nbits >> 2;
 
238
      out[1] = (nbits << 6) | nexcbits;
 
239
      out[2] = in[0] >> 24;
 
240
      out[3] = in[0] >> 16;
 
241
      out[4] = in[0] >>  8;
 
242
      out[5] = in[0];
 
243
      outval = 0;
 
244
      nout = 6;
 
245
      bitout = 7;
 
246
    }
 
247
 
 
248
    /* 16 bit versions (4 bits to store number of bits, 16 bit start val.) */
 
249
    else {
 
250
      out[0] |= nbits;
 
251
      out[1] = (nexcbits << 4) | ((in[0] >> 12) & 0x0f);
 
252
      out[2] = (in[0] >> 4) & 0xff;
 
253
      outval = (in[0] & 0x0f) << 4;
 
254
      nout = 3;
 
255
      bitout = 3;
 
256
    }
 
257
 
 
258
    hival = 1 << nbits_1;
 
259
    loval = -hival;
 
260
 
 
261
    /* data stream */
 
262
    for (nin = 1; nin < n; nin++) {
 
263
      inval = in[nin];
 
264
      exc = 0;
 
265
 
 
266
      /* out of range ? - note and force exception code */
 
267
      if (check_exc && (inval <= loval || inval >= hival)) {
 
268
        exc = 1;
 
269
        inval = loval;
 
270
      }
 
271
 
 
272
      /* copy the value to output (bitwise) */
 
273
      for (bitin = nbits_1; bitin >= 0; bitin--) {
 
274
        if (inval & setinbit[bitin])
 
275
          outval |= setoutbit[bitout];
 
276
        bitout--;
 
277
        if (bitout < 0) {
 
278
          out[nout++] = outval;
 
279
          bitout = 7;
 
280
          outval = 0;
 
281
        }
 
282
      }
 
283
 
 
284
      /* exception alert - write the full value */
 
285
      if (exc) {
 
286
        inval = in[nin];
 
287
        for (bitin = nexcbits_1; bitin >= 0; bitin--) {
 
288
          if (inval & setinbit[bitin])
 
289
            outval |= setoutbit[bitout];
 
290
          bitout--;
 
291
          if (bitout < 0) {
 
292
            out[nout++] = outval;
 
293
            bitout = 7;
 
294
            outval = 0;
 
295
          }
 
296
        }
 
297
      }
 
298
    }
 
299
 
 
300
    /* don't forget to write the last bits */
 
301
    if (bitout != 7) {
 
302
    /*  printf("bitout: %d\n", bitout);*/
 
303
      out[nout++] = outval;
 
304
    }
 
305
  }
 
306
 
 
307
  else if (method == RAW3_COPY) {
 
308
    nout = 1;
 
309
    for (nin = 0; nin < n; nin++) {
 
310
      inval=in[nin];
 
311
      out[nout++] = (inval >> 8);
 
312
      out[nout++] = inval;
 
313
    }
 
314
  }
 
315
 
 
316
 
 
317
  else if (method == RAW3_COPY_32) {
 
318
    nout = 1;
 
319
    for (nin = 0; nin < n; nin++) {
 
320
      out[nout++] = (in[nin] >> 24);
 
321
      out[nout++] = (in[nin] >> 16);
 
322
      out[nout++] = (in[nin] >> 8);
 
323
      out[nout++] = in[nin];
 
324
    }
 
325
  }
 
326
 
 
327
  /* for test only
 
328
  printf("\nnout: %d\n", nout);
 
329
  */
 
330
 
 
331
  return nout;
 
332
}
 
333
 
 
334
 
 
335
int dehuffman16(unsigned char *in, int n, int *method, sraw_t *out)
 
336
{
 
337
  int  nin = 0, nout = 0;
 
338
  int  nbit, nbit_1, nexcbit, nexcbit_1, check_exc;
 
339
  int  bitin;
 
340
  sraw_t excval;
 
341
  /*int i,j; */
 
342
 
 
343
  int hibytein;
 
344
  unsigned int iwork;
 
345
  sraw_t swork;
 
346
 
 
347
  static sraw_t negmask[33] = {
 
348
    0xffffffff, 0xfffffffe, 0xfffffffc, 0xfffffff8,
 
349
    0xfffffff0, 0xffffffe0, 0xffffffc0, 0xffffff80,
 
350
    0xffffff00, 0xfffffe00, 0xfffffc00, 0xfffff800,
 
351
    0xfffff000, 0xffffe000, 0xffffc000, 0xffff8000,
 
352
    0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000,
 
353
    0xfff00000, 0xffe00000, 0xffc00000, 0xff800000,
 
354
    0xff000000, 0xfe000000, 0xfc000000, 0xf8000000,
 
355
    0xf0000000, 0xe0000000, 0xc0000000, 0x80000000,
 
356
    0x00000000
 
357
  };
 
358
 
 
359
  static sraw_t posmask[33] = {
 
360
    0x00000000, 0x00000001, 0x00000003, 0x00000007,
 
361
    0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
 
362
    0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
 
363
    0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
 
364
    0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
 
365
    0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
 
366
    0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
 
367
    0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
 
368
    0xffffffff
 
369
  };
 
370
 
 
371
  static sraw_t setbit[16] = {
 
372
    0x0001, 0x0002, 0x0004, 0x0008,
 
373
    0x0010, 0x0020, 0x0040, 0x0080,
 
374
    0x0100, 0x0200, 0x0400, 0x0800,
 
375
    0x1000, 0x2000, 0x4000, 0x8000
 
376
  };
 
377
 
 
378
  /* for test only
 
379
  printf("dehuffman16: ");
 
380
  */
 
381
 
 
382
  *method = (in[0] >> 4) & 0x0f;
 
383
 
 
384
  if (*method != RAW3_COPY) {
 
385
    nbit = in[0] & 0x0f;
 
386
    /* using 4-bit coding nbit=16 is coded as zero */
 
387
    if (nbit == 0) {
 
388
      nbit = 16;
 
389
      if( CheckVerbose ) {
 
390
         fprintf(stderr,"\nlibeep: critical compression method encountered "
 
391
                     "(method %d, 16 bit)\n", *method);
 
392
       }
 
393
      ERR_FLAG_16 = 1;                  
 
394
    }           
 
395
    nbit_1 = nbit - 1;
 
396
    nexcbit = (in[1] >> 4) & 0x0f;
 
397
    /* using 4-bit coding nexcbit=16 is coded as zero */
 
398
    if (nexcbit == 0) nexcbit = 16;
 
399
    /* for test only
 
400
    printf("\nnbit: %d, nexcbit: %d, method: %d\n",nbit,nexcbit,*method);
 
401
    */
 
402
    nexcbit_1 = nexcbit - 1;
 
403
    excval = -(1 << (nbit_1));
 
404
    check_exc = (nbit != nexcbit);
 
405
 
 
406
    out[0] =   ((sraw_t) in[1] & 0x0f) << 12
 
407
             | (sraw_t) in[2] << 4
 
408
             | (((sraw_t) in[3] >> 4) & 0x0f);
 
409
    if (out[0] & 0x8000)  out[0] |= 0xffff0000;
 
410
 
 
411
    bitin = 28;
 
412
    nout = 1;
 
413
 
 
414
    /* we need to read 2 or 3 bytes from input to get all input value bits */
 
415
    if (nbit < 9) {
 
416
      while (nout < n) {
 
417
        /* index of first byte in inbytes containing bits of current value */
 
418
        hibytein = bitin >> 3;
 
419
 
 
420
        /* max 2 inbytes are needed here for all the bits */
 
421
        iwork = (in[hibytein] << 8) + in[hibytein + 1];
 
422
        swork = iwork >> (((hibytein + 2) << 3) - bitin - nbit);
 
423
 
 
424
        /* handle the sign in the work buffer */
 
425
        if (swork & setbit[nbit_1]) {
 
426
          swork |= negmask[nbit];
 
427
        }
 
428
        else {
 
429
          swork &= posmask[nbit];
 
430
        }
 
431
 
 
432
        bitin += nbit;
 
433
 
 
434
        /* exception ? - forget what we got and read again */
 
435
        if (swork == excval && check_exc) {
 
436
          /* index of first byte containing bits of current exc. value */
 
437
          hibytein = bitin >> 3;
 
438
 
 
439
          /* max 3 inbytes are needed for all the exc. bits */
 
440
          iwork = (in[hibytein] << 16) + (in[hibytein + 1] << 8) + in[hibytein + 2];
 
441
          swork = iwork >> (((hibytein + 3) << 3) - bitin - nexcbit);
 
442
          /* handle the sign in the work buffer */
 
443
          if (swork & setbit[nexcbit_1]) {
 
444
            swork |= negmask[nexcbit];
 
445
          }
 
446
          else {
 
447
            swork &= posmask[nexcbit];
 
448
          }
 
449
          bitin += nexcbit;
 
450
        }
 
451
 
 
452
        out[nout++] = swork;
 
453
      }
 
454
    }
 
455
 
 
456
    else {
 
457
      while (nout < n) {
 
458
        /* index of first byte in inbytes containing bits of current value */
 
459
        hibytein = bitin >> 3;
 
460
 
 
461
        /* max 3 inbytes are needed for all the bits */
 
462
        iwork = (in[hibytein] << 16) + (in[hibytein + 1] << 8) + in[hibytein + 2];
 
463
        swork = iwork >> (((hibytein + 3) << 3) - bitin - nbit);
 
464
 
 
465
        /* handle the sign in the work buffer */
 
466
        if (swork & setbit[nbit_1]) {
 
467
          swork |= negmask[nbit];
 
468
        }
 
469
        else {
 
470
          swork &= posmask[nbit];
 
471
        }
 
472
 
 
473
        bitin += nbit;
 
474
 
 
475
        /* exception ? - forget what we got and read again */
 
476
        if (swork == excval && nbit != nexcbit) {
 
477
          /* index of first byte containing bits of current exc. value */
 
478
          hibytein = bitin >> 3;
 
479
 
 
480
          /* max 3 inbytes are needed for all the bits */
 
481
          iwork = (in[hibytein] << 16) + (in[hibytein + 1] << 8) + in[hibytein + 2];
 
482
          swork = iwork >> (((hibytein + 3) << 3) - bitin - nexcbit);
 
483
          /* handle the sign in the work buffer */
 
484
          if (swork & setbit[nexcbit_1]) {
 
485
            swork |= negmask[nexcbit];
 
486
          }
 
487
          else {
 
488
            swork &= posmask[nexcbit];
 
489
          }
 
490
          bitin += nexcbit;
 
491
        }
 
492
 
 
493
        out[nout++] = swork;
 
494
      }
 
495
    }
 
496
    nin = bitin >> 3;
 
497
    if (bitin & 0x07) nin++;
 
498
  }
 
499
  /* RAW3_COPY mode */
 
500
  else {
 
501
    if( CheckVerbose ) {        
 
502
         fprintf(stderr,"\nlibeep: critical compression method encountered "
 
503
                   "(method 0 RAW3_COPY)\n");   
 
504
     }  
 
505
    ERR_FLAG_0 = 1;
 
506
    nin = 1;
 
507
    for (nout = 0; nout < n; nout++) {
 
508
      out[nout] = (((sraw_t) in[nin]) << 8) | in[nin + 1];
 
509
      /* read s16 instead of u16 */
 
510
      if(out[nout] > 32767) out[nout] -= 65536;
 
511
      nin += 2;
 
512
    }
 
513
  }
 
514
 
 
515
  /*
 
516
  int i;
 
517
  printf("%d | %d | %d |%d | %d\n", *method, nbit, nexcbit, nin, out[0]);
 
518
  for(i = 0; i < n; i++) {
 
519
    fprintf(stderr, "%d ", out[i]);
 
520
  }
 
521
  fprintf(stderr, "\n");
 
522
  */
 
523
 
 
524
  return nin;
 
525
}
 
526
 
 
527
int dehuffman32(unsigned char *in, int n, int *method, sraw_t *out)
 
528
{
 
529
  int nin = 0, nout = 0;
 
530
  int  nbit, nbit_1, nexcbit, nexcbit_1, check_exc;
 
531
  int  bitin, bitout;
 
532
  char inval; sraw_t outval, excval;
 
533
  /* int i; */
 
534
 
 
535
  static unsigned char setinbit[8] = {
 
536
    0x01, 0x02, 0x04, 0x08,
 
537
    0x10, 0x20, 0x40, 0x80
 
538
  };
 
539
 
 
540
  static unsigned int setoutbit[32] = {
 
541
    0x00000001, 0x00000002, 0x00000004, 0x00000008,
 
542
    0x00000010, 0x00000020, 0x00000040, 0x00000080,
 
543
    0x00000100, 0x00000200, 0x00000400, 0x00000800,
 
544
    0x00001000, 0x00002000, 0x00004000, 0x00008000,
 
545
    0x00010000, 0x00020000, 0x00040000, 0x00080000,
 
546
    0x00100000, 0x00200000, 0x00400000, 0x00800000,
 
547
    0x01000000, 0x02000000, 0x04000000, 0x08000000,
 
548
    0x10000000, 0x20000000, 0x40000000, 0x80000000
 
549
  };
 
550
 
 
551
  static unsigned int negmask[33] = {
 
552
    0xffffffff, 0xfffffffe, 0xfffffffc, 0xfffffff8,
 
553
    0xfffffff0, 0xffffffe0, 0xffffffc0, 0xffffff80,
 
554
    0xffffff00, 0xfffffe00, 0xfffffc00, 0xfffff800,
 
555
    0xfffff000, 0xffffe000, 0xffffc000, 0xffff8000,
 
556
    0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000,
 
557
    0xfff00000, 0xffe00000, 0xffc00000, 0xff800000,
 
558
    0xff000000, 0xfe000000, 0xfc000000, 0xf8000000,
 
559
    0xf0000000, 0xe0000000, 0xc0000000, 0x80000000,
 
560
    0x00000000
 
561
  };
 
562
 
 
563
  *method = (in[0] >> 4) & 0x0f;
 
564
 
 
565
  if (*method != RAW3_COPY_32) {
 
566
    nbit = ((in[0] << 2) & 0x3c) | ((in[1] >> 6) & 0x03);
 
567
    nbit_1 = nbit - 1;
 
568
    nexcbit = in[1] & 0x3f;
 
569
    /* if (nexcbit == 0) nexcbit = 64; */
 
570
    nexcbit_1 = nexcbit - 1;
 
571
 
 
572
    out[0] =   ((sraw_t) in[2] << 24)
 
573
             | ((sraw_t) in[3] << 16)
 
574
             | ((sraw_t) in[4] <<  8)
 
575
             | ((sraw_t) in[5]);
 
576
 
 
577
    nin = 6;
 
578
    bitin = 7;
 
579
    inval = in[nin];
 
580
    nout = 1;
 
581
    bitout = nbit_1;
 
582
    outval = 0;
 
583
    excval = -(1 << (nbit_1));
 
584
    check_exc = (nbit != nexcbit);
 
585
 
 
586
    while (nout < n) {
 
587
 
 
588
      /* transfer bitwise from in to out */
 
589
      if (inval & setinbit[bitin]) {
 
590
        outval |= setoutbit[bitout];
 
591
        /* handle the sign in out according to sign bit in in */
 
592
        if (bitout == nbit_1) {
 
593
          outval |= negmask[nbit];
 
594
        }
 
595
      }
 
596
 
 
597
      bitin--;
 
598
      if (bitin < 0) {
 
599
        nin++;
 
600
        bitin = 7;
 
601
        inval = in[nin];
 
602
      }
 
603
 
 
604
      bitout--;
 
605
 
 
606
      /* is a residual complete ? */
 
607
      if (bitout < 0) {
 
608
 
 
609
        /* its an exception ? - forget this and read the large value which follows */
 
610
        if (outval == excval && check_exc) {
 
611
          outval = 0;
 
612
          for (bitout = nexcbit_1; bitout >= 0; bitout--) {
 
613
            if (inval & setinbit[bitin]) {
 
614
              outval |= setoutbit[bitout];
 
615
              if (bitout == nexcbit_1) {
 
616
                outval |= negmask[nexcbit];
 
617
              }
 
618
            }
 
619
            bitin--;
 
620
            if (bitin < 0) {
 
621
              nin++;
 
622
              bitin = 7;
 
623
              inval = in[nin];
 
624
            }
 
625
          }
 
626
        }
 
627
 
 
628
        /* now we really have the value, store it and set up for the next one */
 
629
        out[nout++] = outval;
 
630
        outval = 0;
 
631
        bitout = nbit - 1;
 
632
      }
 
633
    }
 
634
    /* count last incomplete input byte */
 
635
    if (bitin != 7) nin++;
 
636
  }
 
637
 
 
638
  /* RAW3_COPY_32 */
 
639
  else {
 
640
    nin = 1;
 
641
    for (nout = 0; nout < n; nout++) {
 
642
      out[nout] =   ((sraw_t) in[nin]   << 24)
 
643
                  | ((sraw_t) in[nin+1] << 16)
 
644
                  | ((sraw_t) in[nin+2] <<  8)
 
645
                  | ((sraw_t) in[nin+3]      );
 
646
      nin += 4;
 
647
    }
 
648
  }
 
649
 
 
650
  /*
 
651
  fprintf(stderr, "%d\t%d\t%ld\t%d\t%d\n", nbit, nexcbit, nin, *method, out[0]);
 
652
  for (i = 0; i < n; i++) {
 
653
    fprintf(stderr, "%d ", out[i]);
 
654
  }
 
655
  fprintf(stderr, "\n");
 
656
  */
 
657
 
 
658
  return nin;
 
659
}
 
660
 
 
661
int dehuffman(unsigned char *in, int n, int *method, sraw_t *out)
 
662
{
 
663
  /* method bit 3 indicates 16 or 32 bit compression */
 
664
  if (in[0] & (unsigned char) 0x80) {
 
665
    return dehuffman32(in, n, method, out);
 
666
  }
 
667
  else {
 
668
    return dehuffman16(in, n, method, out);
 
669
  }
 
670
}
 
671
 
 
672
 
 
673
/* ---------------------------------------------------------------------
 
674
  take native raw data vectors (neighbors)
 
675
  calc the residuals using the supported methods
 
676
  find the best among them and compress
 
677
  write the compressed output to buffer
 
678
*/
 
679
int compchan(raw3_t *raw3, sraw_t *last, sraw_t *cur, int n, char *out)
 
680
{
 
681
  int i, imin, mi, short_method = RAW3_COPY;
 
682
  int sample;
 
683
  int lmin, length;
 
684
  raw3res_t *rc;
 
685
  sraw_t *res, *restime=NULL;
 
686
  int *hst;
 
687
 
 
688
 
 
689
  /* don't care about short vectors in each loop - force copy instead */
 
690
  if (n < 8) {
 
691
 
 
692
    short_method = RAW3_COPY;
 
693
 
 
694
    for (i = 0; i < n; i++) {
 
695
      if (cur[i] >= 32768 || cur[i] < -32768) {
 
696
        short_method = RAW3_COPY_32;
 
697
        break;
 
698
      }
 
699
    }
 
700
    length = huffman(cur, n, short_method, 0, 0, (unsigned char *) out);
 
701
  }
 
702
 
 
703
  /* calc residuals and their bit distribution
 
704
     using different methods
 
705
     select the best one and compress
 
706
  */
 
707
  else {
 
708
    for (mi = 0; mi < RAW3_METHODC; mi++) {
 
709
 
 
710
      /* prepare access to residual data table for this method */
 
711
      rc = &raw3->rc[mi];
 
712
      res = rc->res;
 
713
      hst = rc->hst;
 
714
      memset(hst, 0, 33 * sizeof(int));
 
715
 
 
716
 
 
717
      switch (mi) {
 
718
        case 0:
 
719
          rc->method = RAW3_TIME;
 
720
          res[0] = cur[0];
 
721
          for (sample = 1; sample < n; sample++)
 
722
            hst[bitc(res[sample] = cur[sample] - cur[sample - 1])]++;
 
723
 
 
724
          rc->length = huffman_size(hst, n - 1, &rc->nbits, &rc->nexcbits);
 
725
          restime = res;
 
726
          break;
 
727
 
 
728
        case 1:
 
729
          rc->method = RAW3_TIME2;
 
730
          res[0] = cur[0];      
 
731
          hst[bitc(res[1] = restime[1])]++;
 
732
          for (sample = 2; sample < n; sample++)
 
733
            hst[bitc(res[sample] = restime[sample] - restime[sample - 1])]++;
 
734
 
 
735
          rc->length = huffman_size(hst, n - 1, &rc->nbits, &rc->nexcbits);
 
736
          break;
 
737
 
 
738
        case 2:
 
739
          rc->method = RAW3_CHAN;
 
740
          res[0] = cur[0];
 
741
          for (sample = 1; sample < n; sample++)
 
742
            hst[bitc(res[sample] = restime[sample] - last[sample] + last[sample - 1])]++;
 
743
 
 
744
          rc->length = huffman_size(hst, n - 1, &rc->nbits, &rc->nexcbits);
 
745
          break;
 
746
      }
 
747
    }
 
748
 
 
749
    /* find the best residual method */
 
750
    lmin = 1000000;
 
751
    for (mi = 0; mi < RAW3_METHODC; mi++) {
 
752
    /*
 
753
      printf("\nmethod: %d, nbits: %d, nexcbits: %d, length: %d\n",
 
754
        raw3->rc[mi].method, raw3->rc[mi].nbits, raw3->rc[mi].nexcbits,
 
755
        raw3->rc[mi].length);
 
756
    */
 
757
      if (raw3->rc[mi].length < lmin) {
 
758
        imin = mi;
 
759
        lmin = raw3->rc[mi].length;
 
760
      }
 
761
    }
 
762
    rc = &raw3->rc[imin];
 
763
 
 
764
    /* need 32 bit storage ? */
 
765
    if (rc->nexcbits > 16 || rc->res[0] < -32768 || rc->res[0] >= 32768 )
 
766
    {
 
767
      rc->method |= 0x08;
 
768
      short_method = RAW3_COPY_32;
 
769
    }
 
770
    /* is the best compression better than store only ? */
 
771
    if (rc->nbits < 32 && lmin + 3 < n * 4) {
 
772
      length = huffman(rc->res, n, rc->method, rc->nbits, rc->nexcbits,
 
773
                        (unsigned char *) out);
 
774
      short_method = -1;
 
775
     }
 
776
    else
 
777
      length = huffman(cur, n, short_method, 0, 0,
 
778
                        (unsigned char *) out);
 
779
 
 
780
  }
 
781
 
 
782
 /*
 
783
  if(short_method < 0)
 
784
  printf("\nmethod: %d, nbits: %d, nexcbits: %d, length: %d\n",
 
785
      rc->method, rc->nbits, rc->nexcbits, length);
 
786
  else printf("\nmethod: %d\n", short_method);
 
787
 
 
788
 */
 
789
  return length;
 
790
}
 
791
 
 
792
int decompchan(raw3_t *raw3, sraw_t *last, sraw_t *cur, int n, char *in)
 
793
{
 
794
  int method, sample, sample_1, sample_2;
 
795
  int length;
 
796
  sraw_t *res = raw3->rc[0].res;
 
797
 
 
798
  /* restore the residuals */
 
799
 
 
800
  length = dehuffman((unsigned char *) in, n, &method, res);
 
801
 
 
802
  /* build the values using residuals and method */
 
803
 
 
804
  switch (method & 0x07) {
 
805
    case RAW3_TIME:
 
806
      cur[0] = res[0];
 
807
      sample_1 = 0;
 
808
      for (sample = 1; sample < n; sample++) {
 
809
        cur[sample] = cur[sample_1] + res[sample];
 
810
        sample_1++;
 
811
      }
 
812
      break;
 
813
 
 
814
    case RAW3_TIME2:
 
815
      cur[0] = res[0];
 
816
      cur[1] = cur[0] + res[1];
 
817
      sample_2 = 0;
 
818
      sample_1 = 1;
 
819
      for (sample = 2; sample < n; sample++) {
 
820
        cur[sample] =
 
821
          2 * cur[sample_1] - cur[sample_2] + res[sample];
 
822
        sample_1++;
 
823
        sample_2++;
 
824
      }
 
825
      break;
 
826
 
 
827
    case RAW3_CHAN:
 
828
      cur[0] = res[0];
 
829
      sample_1 = 0;
 
830
      for (sample = 1; sample < n; sample++) {
 
831
        cur[sample] = cur[sample_1] + last[sample] - last[sample_1] + res[sample];
 
832
        sample_1++;
 
833
      }
 
834
      break;
 
835
 
 
836
    case RAW3_COPY:
 
837
      memcpy(cur, res, n * sizeof(sraw_t));
 
838
      break;
 
839
 
 
840
    default:
 
841
#if !defined(WIN32) || defined(__CYGWIN__)
 
842
      fprintf(stderr, "raw3: unknown compression method!\n");
 
843
#endif
 
844
      break;
 
845
  }
 
846
 
 
847
  return length;
 
848
}
 
849
 
 
850
int compepoch_mux(raw3_t *raw3, sraw_t *in, int length, char *out)
 
851
{
 
852
  int chan;
 
853
  int sample;
 
854
  sraw_t *tmp, *chanbase, *cur, *last;
 
855
  int samplepos;
 
856
  int outsize = 0, outsizealt;
 
857
 
 
858
  cur = raw3->cur;
 
859
  last = raw3->last;
 
860
  memset(last, 0, length * sizeof(sraw_t));
 
861
 
 
862
  for (chan = 0; chan < raw3->chanc; chan++) {
 
863
 
 
864
    /* extract one vector from MUX buffer */
 
865
    chanbase = &in[raw3->chanv[chan]];
 
866
    samplepos = 0;
 
867
    for (sample = 0; sample < length; sample++) {
 
868
      cur[sample] = chanbase[samplepos];
 
869
      samplepos += raw3->chanc;
 
870
    }
 
871
    /* calculate compression and its statistics */
 
872
    outsizealt = outsize;
 
873
    outsize += compchan(raw3, last, cur, length, &out[outsize]);
 
874
    decompchan(raw3,last,cur,length,&out[outsizealt]);
 
875
    /* prepare for reading next channel */
 
876
    tmp = cur; cur = last; last = tmp;
 
877
  }
 
878
 
 
879
  return outsize;
 
880
}
 
881
 
 
882
int decompepoch_mux(raw3_t *raw3, char *in, int length, sraw_t *out)
 
883
{
 
884
  int chan;
 
885
  int sample;
 
886
  sraw_t *tmp, *chanbase, *last, *cur;
 
887
  int samplepos;
 
888
  int insize = 0;
 
889
 
 
890
 
 
891
  cur = raw3->cur;
 
892
  last = raw3->last;
 
893
  memset(last, 0, length * sizeof(sraw_t));
 
894
 
 
895
  for (chan = 0; chan < raw3->chanc; chan++) {
 
896
 
 
897
    /* uncompress */
 
898
    insize += decompchan(raw3, last, cur, length, &in[insize]);
 
899
 
 
900
    /* mangle into MUX buffer */
 
901
    chanbase = &out[raw3->chanv[chan]];
 
902
    samplepos = 0;
 
903
    for (sample = 0; sample < length; sample++) {
 
904
      chanbase[samplepos] = cur[sample];
 
905
      samplepos += raw3->chanc;
 
906
    }
 
907
 
 
908
    /* prepare for reading next channel */
 
909
    tmp = cur; cur = last; last = tmp;
 
910
  }
 
911
 
 
912
  return insize;
 
913
}
 
914
 
 
915
/* #ifdef RAW3_CHECK obsolete */
 
916
#include <cnt/cnt.h>
 
917
int decompepoch_mux_RAW3_CHECK(eeg_t *EEG, raw3_t *raw3, char *in, int length, sraw_t *out)
 
918
{
 
919
  int chan;
 
920
  int sample;
 
921
  sraw_t *tmp, *chanbase, *last, *cur;
 
922
  int samplepos;
 
923
  int insize = 0;
 
924
 
 
925
 
 
926
  cur = raw3->cur;
 
927
  last = raw3->last;
 
928
  memset(last, 0, length * sizeof(sraw_t));
 
929
 
 
930
  for (chan = 0; chan < raw3->chanc; chan++) {
 
931
 
 
932
    /* uncompress */
 
933
    insize += decompchan(raw3, last, cur, length, &in[insize]);
 
934
 
 
935
    /* #ifdef RAW3_CHECK obsolete */
 
936
    {
 
937
      char *label;
 
938
      int n = 0;   /* count chars in output line */
 
939
      char buf[128];
 
940
      int j;
 
941
 
 
942
      /*fflush(stderr); fflush(stdout);*/
 
943
      if(ERR_FLAG_16) {
 
944
        if( EEG && eep_get_chanc(EEG) > 0 ) {
 
945
          /* print label of affected channel */
 
946
          label = eep_get_chan_label(EEG, raw3->chanv[chan]);
 
947
              sprintf(buf,"channel %s", label );
 
948
          fprintf(stderr, buf);
 
949
          n = strlen(buf);
 
950
 
 
951
          /* the following channels will probably be also affected */
 
952
          /* wrap lines after 77 chars */
 
953
              if(chan<raw3->chanc-1) {
 
954
                 fprintf(stderr," ("); n+=2;
 
955
             for(j=chan+1; j < raw3->chanc; j++) {
 
956
                  label = eep_get_chan_label(EEG, raw3->chanv[j]);
 
957
              if( (n+strlen(label)) < 78) {
 
958
                      fprintf(stderr," %s", label );
 
959
                  n += strlen(label) + 1;
 
960
               } else {
 
961
                  fprintf(stderr,"\n%s", label);
 
962
                  n = strlen(label);
 
963
               }                                        
 
964
              }
 
965
                 fprintf(stderr," )\n");
 
966
              } else  fprintf(stderr,"\n");
 
967
        }       
 
968
        ERR_FLAG_EPOCH = 1;
 
969
        ERR_FLAG_16 = 0;
 
970
       }
 
971
       if(ERR_FLAG_0) {
 
972
       if( EEG && eep_get_chanc(EEG) > 0 ) {
 
973
           label = eep_get_chan_label(EEG, raw3->chanv[chan]);
 
974
          fprintf(stderr,"channel %s\n", label );
 
975
        }
 
976
        ERR_FLAG_EPOCH = 1;
 
977
        ERR_FLAG_0 = 0;
 
978
       }
 
979
     }
 
980
 
 
981
     /* mangle into MUX buffer */
 
982
     chanbase = &out[raw3->chanv[chan]];
 
983
     samplepos = 0;
 
984
     for (sample = 0; sample < length; sample++) {
 
985
       chanbase[samplepos] = cur[sample];
 
986
       samplepos += raw3->chanc;
 
987
     }
 
988
 
 
989
     /* prepare for reading next channel */
 
990
     tmp = cur; cur = last; last = tmp;
 
991
  }
 
992
 
 
993
  return insize;
 
994
}
 
995
 
 
996
void compchanv_mux(sraw_t *buf, int length,
 
997
                   short chanc, short *chanv)
 
998
{
 
999
  int chan, i, j, jmax, sample;
 
1000
  float **rvv, rmax;
 
1001
  float sumi, sumii, sumj, sumjj, sumij, x, y;
 
1002
 
 
1003
  /* create a [chanc x chanc] square matrix */
 
1004
  rvv = (float **) malloc(chanc * sizeof(float *));
 
1005
  for (i = 0; i < chanc; i++)
 
1006
    rvv[i] = (float *) malloc(chanc * sizeof(float));
 
1007
 
 
1008
  /* calc a full matrix of correlation coefficients */
 
1009
  for (i = 0; i < chanc; i++) {
 
1010
    for (j = 0; j <= i; j++) {
 
1011
      if (i == j) {
 
1012
        rvv[i][j] = 1.0;
 
1013
      }
 
1014
      else {
 
1015
 
 
1016
        /* we need sums, squaresums, cosums */
 
1017
        sumi = sumii = sumj = sumjj = sumij = 0.0;
 
1018
        for (sample = 0; sample < length; sample++) {
 
1019
          sumi += x = buf[i + chanc * sample];
 
1020
          sumii += x * x;
 
1021
          sumj += y = buf[j + chanc * sample];
 
1022
          sumjj += y * y;
 
1023
          sumij += x * y;
 
1024
        }
 
1025
        sumi /= length; sumii /= length;
 
1026
        sumj /= length; sumjj /= length;
 
1027
        sumij /= length;
 
1028
 
 
1029
        /* calc the correlation coeff. from sums, avoid fp exceptions here */
 
1030
        x = (sumii - sumi * sumi) * (sumjj - sumj * sumj);
 
1031
        y = x > 0.0 ? sqrt(x) : 0.0;
 
1032
        rvv[i][j] = y > 1e-6 ? (sumij - sumi * sumj) / y : 0.0;
 
1033
        rvv[j][i] = rvv[i][j];
 
1034
      }
 
1035
    }
 
1036
  }
 
1037
 
 
1038
  /* find a channel-similar channel-... path trough the matrix */
 
1039
  chanv[0] = 0;
 
1040
  for (chan = 1; chan < chanc; chan++) {
 
1041
    /* mark used channels in matrix - set column to a unreachable min correlation*/
 
1042
    for (i = 0; i < chanc; i++)
 
1043
      rvv[i][chanv[chan - 1]] = -2.0;
 
1044
 
 
1045
    /* find next chan to use - most similar to last -> max. correlation in row*/
 
1046
    rmax = -2.0;
 
1047
    i = chanv[chan - 1];
 
1048
    for (j = 0; j < chanc; j++) {
 
1049
      if (rvv[i][j] > rmax) {
 
1050
        rmax = rvv[i][j];
 
1051
        jmax = j;
 
1052
      }
 
1053
    }
 
1054
 
 
1055
    /* register it */
 
1056
    chanv[chan] = jmax;
 
1057
  }
 
1058
 
 
1059
  for(i = 0; i < chanc; i++)
 
1060
    free((char *) rvv[i]);
 
1061
  free((char *) rvv);
 
1062
}
 
1063
 
 
1064
raw3_t *raw3_init(int chanc, short *chanv, int length)
 
1065
{
 
1066
  int i;
 
1067
  raw3_t *raw3 = (raw3_t *) malloc(sizeof(raw3_t));
 
1068
 
 
1069
  if (!raw3)
 
1070
    return NULL;
 
1071
 
 
1072
  raw3->chanc = chanc;
 
1073
  raw3->chanv = (short *) malloc(chanc * sizeof(sraw_t));
 
1074
 
 
1075
  for (i = 0; i < 3; i++)
 
1076
    raw3->rc[i].res = (sraw_t *) malloc(length * sizeof(sraw_t));
 
1077
  raw3->last = (sraw_t *) malloc(length * sizeof(sraw_t));
 
1078
  raw3->cur = (sraw_t *) malloc(length * sizeof(sraw_t));
 
1079
 
 
1080
  if (!raw3->cur || !raw3->last || !raw3->chanv) {
 
1081
    raw3_free(raw3);
 
1082
    return NULL;
 
1083
  }
 
1084
  memcpy(raw3->chanv, chanv, chanc * sizeof(short));
 
1085
 
 
1086
  return raw3;
 
1087
}
 
1088
 
 
1089
void    raw3_free(raw3_t *raw3)
 
1090
{
 
1091
  int i;
 
1092
 
 
1093
  if (raw3) {
 
1094
    if (raw3->chanv) free(raw3->chanv);
 
1095
    for (i = 0; i < 3; i++)
 
1096
      if (raw3->rc[i].res) free(raw3->rc[i].res);
 
1097
    if (raw3->last) free(raw3->last);
 
1098
    if (raw3->cur) free(raw3->cur);
 
1099
    free(raw3);
 
1100
  }
 
1101
}