]> git.sesse.net Git - kdenlive/blob - src/kiss_fft/tools/kfc.c
Merge branch 'audioAlign' of git://anongit.kde.org/kdenlive into audioAlign
[kdenlive] / src / kiss_fft / tools / kfc.c
1 #include "kfc.h"
2
3 /*
4 Copyright (c) 2003-2004, Mark Borgerding
5
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
12     * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 */
16
17
18 typedef struct cached_fft *kfc_cfg;
19
20 struct cached_fft
21 {
22     int nfft;
23     int inverse;
24     kiss_fft_cfg cfg;
25     kfc_cfg next;
26 };
27
28 static kfc_cfg cache_root=NULL;
29 static int ncached=0;
30
31 static kiss_fft_cfg find_cached_fft(int nfft,int inverse)
32 {
33     size_t len;
34     kfc_cfg  cur=cache_root;
35     kfc_cfg  prev=NULL;
36     while ( cur ) {
37         if ( cur->nfft == nfft && inverse == cur->inverse )
38             break;/*found the right node*/
39         prev = cur;
40         cur = prev->next;
41     }
42     if (cur== NULL) {
43         /* no cached node found, need to create a new one*/
44         kiss_fft_alloc(nfft,inverse,0,&len);
45         cur = (kfc_cfg)KISS_FFT_MALLOC((sizeof(struct cached_fft) + len ));
46         if (cur == NULL)
47             return NULL;
48         cur->cfg = (kiss_fft_cfg)(cur+1);
49         kiss_fft_alloc(nfft,inverse,cur->cfg,&len);
50         cur->nfft=nfft;
51         cur->inverse=inverse;
52         cur->next = NULL;
53         if ( prev )
54             prev->next = cur;
55         else
56             cache_root = cur;
57         ++ncached;
58     }
59     return cur->cfg;
60 }
61
62 void kfc_cleanup(void)
63 {
64     kfc_cfg  cur=cache_root;
65     kfc_cfg  next=NULL;
66     while (cur){
67         next = cur->next;
68         free(cur);
69         cur=next;
70     }
71     ncached=0;
72     cache_root = NULL;
73 }
74 void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
75 {
76     kiss_fft( find_cached_fft(nfft,0),fin,fout );
77 }
78
79 void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
80 {
81     kiss_fft( find_cached_fft(nfft,1),fin,fout );
82 }
83
84 #ifdef KFC_TEST
85 static void check(int nc)
86 {
87     if (ncached != nc) {
88         fprintf(stderr,"ncached should be %d,but it is %d\n",nc,ncached);
89         exit(1);
90     }
91 }
92
93 int main(void)
94 {
95     kiss_fft_cpx buf1[1024],buf2[1024];
96     memset(buf1,0,sizeof(buf1));
97     check(0);
98     kfc_fft(512,buf1,buf2);
99     check(1);
100     kfc_fft(512,buf1,buf2);
101     check(1);
102     kfc_ifft(512,buf1,buf2);
103     check(2);
104     kfc_cleanup();
105     check(0);
106     return 0;
107 }
108 #endif