summaryrefslogtreecommitdiff
path: root/lib/zita-convolver-3.1.0/zita-convolver.h
blob: f19b6dabef57737038b442615c5db2bb479f146e (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
// ----------------------------------------------------------------------------
//
//  Copyright (C) 2006-2011 Fons Adriaensen <fons@linuxaudio.org>
//    
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------


#ifndef _ZITA_CONVOLVER_H
#define _ZITA_CONVOLVER_H

#define _POSIX_PTHREAD_SEMANTICS

#include <errno.h>
#include <pthread.h>
#include <fftw3.h>


#define ZITA_CONVOLVER_MAJOR_VERSION 3
#define ZITA_CONVOLVER_MINOR_VERSION 1


extern int zita_convolver_major_version (void);
extern int zita_convolver_minor_version (void);


// ----------------------------------------------------------------------------


#ifdef ZCSEMA_IS_IMPLEMENTED
#undef ZCSEMA_IS_IMPLEMENTED
#endif


#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__GNU__)

// NOTE: __FreeBSD_kernel__  and __GNU__ were added by the Debian maintainers
// (the latter for the HURD version of Debian). Things are reported to work
// with some applications but probably have not been tested in depth.

#include <semaphore.h>

class ZCsema
{
public:

    ZCsema (void) { init (0, 0); }
    ~ZCsema (void) { sem_destroy (&_sema); }

    ZCsema (const ZCsema&); // disabled
    ZCsema& operator= (const ZCsema&); // disabled

    int init (int s, int v) { return sem_init (&_sema, s, v); }
    int post (void) { return sem_post (&_sema); }
    int wait (void) { return sem_wait (&_sema); }
    int trywait (void) { return sem_trywait (&_sema); }

private:

    sem_t  _sema;
};

#define ZCSEMA_IS_IMPLEMENTED
#endif


// ADDED BY DZ TO MAKE IT COMPILE FOR >= 10.5
#ifdef __APPLE__
inline int posix_memalign(void **memptr, size_t alignment, size_t size)
{
   if ((alignment && !(alignment & (alignment - 1))) || (alignment > 16))
       return EINVAL;
   *memptr = malloc(size);
   if (*memptr == NULL)
       return ENOMEM;
   return 0;
}
#endif


#ifdef __APPLE__

// NOTE:  ***** I DO NOT REPEAT NOT PROVIDE SUPPORT FOR OSX *****
// 
// The following code partially emulates the POSIX sem_t for which
// OSX has only a crippled implementation. It may or may not compile,
// and if it compiles it may or may not work correctly. Blame APPLE
// for not following POSIX standards.

class ZCsema
{
public:

    ZCsema (void) : _count (0)
    {
        init (0, 0);
    }

    ~ZCsema (void)
    {
        pthread_mutex_destroy (&_mutex);
        pthread_cond_destroy (&_cond);
    }

    ZCsema (const ZCsema&); // disabled
    ZCsema& operator= (const ZCsema&); // disabled

    int init (int s, int v)
    {
	_count = v;
        return pthread_mutex_init (&_mutex, 0) || pthread_cond_init (&_cond, 0);
    }

    int post (void)
    {
	pthread_mutex_lock (&_mutex);
	_count++;
	if (_count == 1) pthread_cond_signal (&_cond);
	pthread_mutex_unlock (&_mutex);
	return 0;
    }

    int wait (void)
    {
	pthread_mutex_lock (&_mutex);
	while (_count < 1) pthread_cond_wait (&_cond, &_mutex);
	_count--;
	pthread_mutex_unlock (&_mutex);
	return 0;
    }

    int trywait (void)
    {
	if (pthread_mutex_trylock (&_mutex)) return -1;
	if (_count < 1)
	{
	    pthread_mutex_unlock (&_mutex);
	    return -1;
	}
        _count--;
        pthread_mutex_unlock (&_mutex);
        return 0;
    }

private:

    int              _count;
    pthread_mutex_t  _mutex;
    pthread_cond_t   _cond;
};

#define ZCSEMA_IS_IMPLEMENTED
#endif


#ifndef ZCSEMA_IS_IMPLEMENTED
#error "The ZCsema class is not implemented."
#endif


// ----------------------------------------------------------------------------


class Inpnode   
{
private:

    friend class Convlevel;

    Inpnode        *_next;
    fftwf_complex **_ffta;
    unsigned int    _inp;
};


class Macnode  
{
private:

    friend class Convlevel;

    Macnode        *_next;
    Inpnode        *_inpn;
    fftwf_complex **_fftb;
    bool            _copy;
};


class Outnode   
{
private:

    friend class Convlevel;

    Outnode        *_next;
    Macnode        *_list;
    float          *_buff [3];
    unsigned int    _out;
};


class Converror
{
public:

    enum
    {
	BAD_STATE = -1,
	BAD_PARAM = -2,
        MEM_ALLOC = -3
    };

private:

    friend class Convlevel;
    friend class Convproc;

    Converror (int error) : _error (error) {}

    int _error;
};


class Convlevel
{
private:

    friend class Convproc;

    enum 
    {
        OPT_FFTW_MEASURE = 1,
        OPT_VECTOR_MODE  = 2
    };

    enum
    {
        ST_IDLE,
        ST_TERM,
        ST_PROC
    };

    Convlevel (void);
    ~Convlevel (void);

    void *alloc_aligned (size_t size);

    void configure (int prio,
                    unsigned int offs,
                    unsigned int npar,
                    unsigned int parsize,
		    unsigned int options);

    void impdata_create (unsigned int inp,
                         unsigned int out,
                         unsigned int step,
                         float *data,
                         int ind0,
                         int ind1); 

    void impdata_update (unsigned int inp,
                         unsigned int out,
                         unsigned int step,
                         float *data,
                         int ind0,
                         int ind1); 

    void impdata_copy (unsigned int inp1,
                       unsigned int out1,
                       unsigned int inp2,
                       unsigned int out2);

    void reset (unsigned int inpsize,
                unsigned int outsize,
	        float **inpbuff,
	        float **outbuff);

    void start (int absprio, int policy);

    void process (bool sync);

    int  readout (bool sync, unsigned int skipcnt);

    void stop (void);

    void cleanup (void);

    void fftswap (fftwf_complex *p);

    void print (FILE *F);

    static void *static_main (void *arg);

    void main (void);

    Macnode *findmacnode (unsigned int inp, unsigned int out, bool create);

    volatile unsigned int _stat;           // current processing state
    int                   _prio;           // relative priority
    unsigned int          _offs;           // offset from start of impulse response
    unsigned int          _npar;           // number of partitions
    unsigned int          _parsize;        // partition and outbut buffer size
    unsigned int          _outsize;        // step size for output buffer
    unsigned int          _outoffs;        // offset into output buffer
    unsigned int          _inpsize;        // size of shared input buffer 
    unsigned int          _inpoffs;        // offset into input buffer
    unsigned int          _options;        // various options
    unsigned int          _ptind;          // rotating partition index
    unsigned int          _opind;          // rotating output buffer index
    int                   _bits;           // bit identifiying this level
    int                   _wait;           // number of unfinished cycles
    pthread_t             _pthr;           // posix thread executing this level
    ZCsema                _trig;           // sema used to trigger a cycle
    ZCsema                _done;           // sema used to wait for a cycle
    Inpnode              *_inp_list;       // linked list of active inputs
    Outnode              *_out_list;       // linked list of active outputs
    fftwf_plan            _plan_r2c;       // FFTW plan, forward FFT
    fftwf_plan            _plan_c2r;       // FFTW plan, inverse FFT
    float                *_time_data;      // workspace
    float                *_prep_data;      // workspace
    fftwf_complex        *_freq_data;      // workspace
    float               **_inpbuff;        // array of shared input buffers
    float               **_outbuff;        // array of shared output buffers
};


// ----------------------------------------------------------------------------


class Convproc
{
public:

    Convproc (void);
    ~Convproc (void);

    enum
    {
        ST_IDLE,
	ST_STOP,
        ST_WAIT,
        ST_PROC
    };

    enum
    {
        FL_LATE = 0x0000FFFF,
	FL_LOAD = 0x01000000
    };

    enum 
    {
        OPT_FFTW_MEASURE = Convlevel::OPT_FFTW_MEASURE, 
        OPT_VECTOR_MODE  = Convlevel::OPT_VECTOR_MODE
    };

    enum
    {
	MAXINP   = 64,
	MAXOUT   = 64,
	MAXLEV   = 8,
	MINPART  = 64,
	MAXPART  = 8192,
	MAXDIVIS = 16,
	MINQUANT = 16,
	MAXQUANT = 8192
    };

    unsigned int state (void) const
    {
	return _state;
    }

    float *inpdata (unsigned int inp) const
    {
	return _inpbuff [inp] + _inpoffs;
    }

    float *outdata (unsigned int out) const
    {
	return _outbuff [out] + _outoffs;
    }

    void set_density (float density);

    void set_options (unsigned int options);

    void set_skipcnt (unsigned int skipcnt);

    int configure (unsigned int ninp,
                   unsigned int nout,
                   unsigned int maxsize,
                   unsigned int quantum,
                   unsigned int minpart,
                   unsigned int maxpart);

    int impdata_create (unsigned int inp,
                        unsigned int out,
                        unsigned int step,
                        float       *data,
                        int          ind0,
                        int          ind1); 

    int impdata_update (unsigned int inp,
                        unsigned int out,
                        unsigned int step,
                        float       *data,
                        int          ind0,
                        int          ind1); 

    int impdata_copy (unsigned int inp1,
                      unsigned int out1,
                      unsigned int inp2,
                      unsigned int out2);

    int reset (void);

    int start_process (int abspri, int policy);

    int process (bool sync = false);

    int stop_process (void);

    bool check_stop (void);

    int cleanup (void);

    void print (FILE *F = stdout);

    static float _mac_cost;
    static float _fft_cost;

private:

    unsigned int    _state;                   // current state
    float          *_inpbuff [MAXINP];        // input buffers
    float          *_outbuff [MAXOUT];        // output buffers
    unsigned int    _inpoffs;                 // current offset in input buffers
    unsigned int    _outoffs;                 // current offset in output buffers
    unsigned int    _options;                 // option bits
    unsigned int    _skipcnt;                 // number of frames to skip 
    float           _density;                 // matrix density hint
    unsigned int    _ninp;                    // number of inputs
    unsigned int    _nout;                    // number of outputs
    unsigned int    _quantum;                 // processing block size
    unsigned int    _minpart;                 // smallest partition size
    unsigned int    _maxpart;                 // largest allowed partition size
    unsigned int    _nlevels;                 // number of partition sizes
    unsigned int    _inpsize;                 // size of input buffers
    unsigned int    _latecnt;                 // count of cycles ending too late
    Convlevel      *_convlev [MAXLEV];        // array of processors 
    void           *_dummy [64];
};


// ----------------------------------------------------------------------------


#endif