uploaded original spice-server-0.12.4 and celt-0.5.1.3
[sdk/emulator/libs/spice-server.git] / lib / celt-0.5.1.3 / libcelt / dump_modes.c
1 /* (C) 2008 Jean-Marc Valin, CSIRO
2 */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7    
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10    
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14    
15    - Neither the name of the Xiph.org Foundation nor the names of its
16    contributors may be used to endorse or promote products derived from
17    this software without specific prior written permission.
18    
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <stdio.h>
37 #include "modes.h"
38 #include "celt.h"
39 #include "rate.h"
40
41 #define INT16 "%d"
42 #define INT32 "%d"
43 #define FLOAT "%f"
44
45 #ifdef FIXED_POINT
46 #define WORD16 INT16
47 #define WORD32 INT32
48 #else
49 #define WORD16 FLOAT
50 #define WORD32 FLOAT
51 #endif
52
53
54 void dump_modes(FILE *file, CELTMode **modes, int nb_modes)
55 {
56    int i, j;
57    fprintf(file, "/* The contents of this file is automatically generated and contains static\n");
58    fprintf(file, "   definitions for some pre-defined modes */\n");
59    fprintf(file, "#include \"modes.h\"\n");
60    fprintf(file, "#include \"rate.h\"\n");
61
62    fprintf(file, "\n");
63    for (i=0;i<nb_modes;i++)
64    {
65       CELTMode *mode = modes[i];
66       fprintf(file, "#ifndef DEF_EBANDS%d_%d\n", mode->Fs, mode->mdctSize);
67       fprintf(file, "#define DEF_EBANDS%d_%d\n", mode->Fs, mode->mdctSize);
68       fprintf (file, "static const celt_int16_t eBands%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbEBands+2);
69       for (j=0;j<mode->nbEBands+2;j++)
70          fprintf (file, "%d, ", mode->eBands[j]);
71       fprintf (file, "};\n");
72       fprintf(file, "#endif\n");
73       fprintf(file, "\n");
74       
75       
76       fprintf(file, "#ifndef DEF_PBANDS%d_%d\n", mode->Fs, mode->mdctSize);
77       fprintf(file, "#define DEF_PBANDS%d_%d\n", mode->Fs, mode->mdctSize);
78       fprintf (file, "static const celt_int16_t pBands%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbPBands+2);
79       for (j=0;j<mode->nbPBands+2;j++)
80          fprintf (file, "%d, ", mode->pBands[j]);
81       fprintf (file, "};\n");
82       fprintf(file, "#endif\n");
83       fprintf(file, "\n");
84       
85       
86       fprintf(file, "#ifndef DEF_WINDOW%d\n", mode->overlap);
87       fprintf(file, "#define DEF_WINDOW%d\n", mode->overlap);
88       fprintf (file, "static const celt_word16_t window%d[%d] = {\n", mode->overlap, mode->overlap);
89       for (j=0;j<mode->overlap;j++)
90          fprintf (file, WORD16 ", ", mode->window[j]);
91       fprintf (file, "};\n");
92       fprintf(file, "#endif\n");
93       fprintf(file, "\n");
94       
95       fprintf(file, "#ifndef DEF_PSY%d\n", mode->Fs);
96       fprintf(file, "#define DEF_PSY%d\n", mode->Fs);
97       fprintf (file, "static const celt_word16_t psy_decayR_%d[%d] = {\n", mode->Fs, MAX_PERIOD/2);
98       for (j=0;j<MAX_PERIOD/2;j++)
99          fprintf (file, WORD16 ", ", mode->psy.decayR[j]);
100       fprintf (file, "};\n");
101       fprintf(file, "#endif\n");
102       fprintf(file, "\n");
103
104       
105       fprintf(file, "#ifndef DEF_ALLOC_VECTORS%d_%d_%d\n", mode->Fs, mode->mdctSize, mode->nbChannels);
106       fprintf(file, "#define DEF_ALLOC_VECTORS%d_%d_%d\n", mode->Fs, mode->mdctSize, mode->nbChannels);
107       fprintf (file, "static const celt_int16_t allocVectors%d_%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbChannels, mode->nbEBands*mode->nbAllocVectors);
108       for (j=0;j<mode->nbAllocVectors;j++)
109       {
110          int k;
111          for (k=0;k<mode->nbEBands;k++)
112             fprintf (file, "%2d, ", mode->allocVectors[j*mode->nbEBands+k]);
113          fprintf (file, "\n");
114       }
115       fprintf (file, "};\n");
116       fprintf(file, "#endif\n");
117       fprintf(file, "\n");
118
119       fprintf(file, "#ifndef DEF_ALLOC_ENERGY%d_%d_%d\n", mode->Fs, mode->mdctSize, mode->nbChannels);
120       fprintf(file, "#define DEF_ALLOC_ENERGY%d_%d_%d\n", mode->Fs, mode->mdctSize, mode->nbChannels);
121       fprintf (file, "static const celt_int16_t allocEnergy%d_%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbChannels, mode->nbEBands*mode->nbAllocVectors);
122       for (j=0;j<mode->nbAllocVectors;j++)
123       {
124          int k;
125          for (k=0;k<mode->nbEBands;k++)
126             fprintf (file, "%2d, ", mode->energy_alloc[j*mode->nbEBands+k]);
127          fprintf (file, "\n");
128       }
129       fprintf (file, "};\n");
130       fprintf(file, "#endif\n");
131       fprintf(file, "\n");
132
133       fprintf(file, "#ifndef DEF_ALLOC_CACHE%d_%d_%d\n", mode->Fs, mode->mdctSize, mode->nbChannels);
134       fprintf(file, "#define DEF_ALLOC_CACHE%d_%d_%d\n", mode->Fs, mode->mdctSize, mode->nbChannels);
135       for (j=0;j<mode->nbEBands;j++)
136       {
137          int k;
138          if (j==0 || (mode->bits[j] != mode->bits[j-1]))
139          {
140             fprintf (file, "static const celt_int16_t allocCache_band%d_%d_%d_%d[MAX_PULSES] = {\n", j, mode->Fs, mode->mdctSize, mode->nbChannels);
141             for (k=0;k<MAX_PULSES;k++)
142                fprintf (file, "%2d, ", mode->bits[j][k]);
143             fprintf (file, "};\n");
144          } else {
145             fprintf (file, "#define allocCache_band%d_%d_%d_%d allocCache_band%d_%d_%d_%d\n", j, mode->Fs, mode->mdctSize, mode->nbChannels, j-1, mode->Fs, mode->mdctSize, mode->nbChannels);
146          }
147       }
148       fprintf (file, "static const celt_int16_t *allocCache%d_%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbChannels, mode->nbEBands);
149       for (j=0;j<mode->nbEBands;j++)
150       {
151          fprintf (file, "allocCache_band%d_%d_%d_%d, ", j, mode->Fs, mode->mdctSize, mode->nbChannels);
152       }
153       fprintf (file, "};\n");
154       fprintf(file, "#endif\n");
155       fprintf(file, "\n");
156
157
158       if (mode->bits_stereo)
159       {
160          fprintf(file, "#ifndef DEF_ALLOC_STEREO_CACHE%d_%d_%d\n", mode->Fs, mode->mdctSize, mode->nbChannels);
161          fprintf(file, "#define DEF_ALLOC_STEREO_CACHE%d_%d_%d\n", mode->Fs, mode->mdctSize, mode->nbChannels);
162          for (j=0;j<mode->nbEBands;j++)
163          {
164             int k;
165             if (j==0 || (mode->bits[j] != mode->bits_stereo[j-1]))
166             {
167                fprintf (file, "static const celt_int16_t allocStereoCache_band%d_%d_%d_%d[MAX_PULSES] = {\n", j, mode->Fs, mode->mdctSize, mode->nbChannels);
168                for (k=0;k<MAX_PULSES;k++)
169                   fprintf (file, "%2d, ", mode->bits_stereo[j][k]);
170                fprintf (file, "};\n");
171             } else {
172                fprintf (file, "#define allocStereoCache_band%d_%d_%d_%d allocStereoCache_band%d_%d_%d_%d\n", j, mode->Fs, mode->mdctSize, mode->nbChannels, j-1, mode->Fs, mode->mdctSize, mode->nbChannels);
173             }
174          }
175          fprintf (file, "static const celt_int16_t *allocStereoCache%d_%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbChannels, mode->nbEBands);
176          for (j=0;j<mode->nbEBands;j++)
177          {
178             fprintf (file, "allocStereoCache_band%d_%d_%d_%d, ", j, mode->Fs, mode->mdctSize, mode->nbChannels);
179          }
180          fprintf (file, "};\n");
181          fprintf(file, "#endif\n");
182          fprintf(file, "\n");
183       }
184
185
186       fprintf(file, "static const CELTMode mode%d_%d_%d_%d = {\n", mode->Fs, mode->nbChannels, mode->mdctSize, mode->overlap);
187       fprintf(file, "0x%x,\t/* marker */\n", 0xa110ca7e);
188       fprintf(file, INT32 ",\t/* Fs */\n", mode->Fs);
189       fprintf(file, "%d,\t/* overlap */\n", mode->overlap);
190       fprintf(file, "%d,\t/* mdctSize */\n", mode->mdctSize);
191       fprintf(file, "%d,\t/* nbChannels */\n", mode->nbChannels);
192       fprintf(file, "%d,\t/* nbEBands */\n", mode->nbEBands);
193       fprintf(file, "%d,\t/* nbPBands */\n", mode->nbPBands);
194       fprintf(file, "%d,\t/* pitchEnd */\n", mode->pitchEnd);
195       fprintf(file, "eBands%d_%d,\t/* eBands */\n", mode->Fs, mode->mdctSize);
196       fprintf(file, "pBands%d_%d,\t/* pBands */\n", mode->Fs, mode->mdctSize);
197       fprintf(file, WORD16 ",\t/* ePredCoef */\n", mode->ePredCoef);
198       fprintf(file, "%d,\t/* nbAllocVectors */\n", mode->nbAllocVectors);
199       fprintf(file, "allocVectors%d_%d_%d,\t/* allocVectors */\n", mode->Fs, mode->mdctSize, mode->nbChannels);
200       fprintf(file, "allocCache%d_%d_%d,\t/* bits */\n", mode->Fs, mode->mdctSize, mode->nbChannels);
201       if (mode->bits_stereo)
202          fprintf(file, "allocStereoCache%d_%d_%d,\t/* bits_stereo */\n", mode->Fs, mode->mdctSize, mode->nbChannels);
203       else
204          fprintf(file, "0,\t/* bits_stereo */\n");
205       fprintf(file, "{%d, 0, 0},\t/* mdct */\n", 2*mode->mdctSize);
206       fprintf(file, "0,\t/* fft */\n");
207       fprintf(file, "window%d,\t/* window */\n", mode->overlap);
208       fprintf(file, "%d,\t/* nbShortMdcts */\n", mode->nbShortMdcts);
209       fprintf(file, "%d,\t/* shortMdctSize */\n", mode->shortMdctSize);
210       fprintf(file, "{%d, 0, 0},\t/* shortMdct */\n", 2*mode->mdctSize);
211       fprintf(file, "window%d,\t/* shortWindow */\n", mode->overlap);
212       fprintf(file, "{psy_decayR_%d},\t/* psy */\n", mode->Fs);
213       fprintf(file, "0,\t/* prob */\n");
214       fprintf(file, "allocEnergy%d_%d_%d,\t/* energy_alloc */\n", mode->Fs, mode->mdctSize, mode->nbChannels);
215       fprintf(file, "0x%x,\t/* marker */\n", 0xa110ca7e);
216       fprintf(file, "};\n");
217    }
218    fprintf(file, "\n");
219    fprintf(file, "/* List of all the available modes */\n");
220    fprintf(file, "#define TOTAL_MODES %d\n", nb_modes);
221    fprintf(file, "static const CELTMode * const static_mode_list[TOTAL_MODES] = {\n");
222    for (i=0;i<nb_modes;i++)
223    {
224       CELTMode *mode = modes[i];
225       fprintf(file, "&mode%d_%d_%d_%d,\n", mode->Fs, mode->nbChannels, mode->mdctSize, mode->overlap);
226    }
227    fprintf(file, "};\n");
228 }
229
230 void dump_header(FILE *file, CELTMode **modes, int nb_modes)
231 {
232    int i;
233    int channels = 0;
234    int frame_size = 0;
235    int overlap = 0;
236    fprintf (file, "/* This header file is generated automatically*/\n");
237    for (i=0;i<nb_modes;i++)
238    {
239       CELTMode *mode = modes[i];
240       if (channels==0)
241          channels = mode->nbChannels;
242       else if (channels != mode->nbChannels)
243          channels = -1;
244       if (frame_size==0)
245          frame_size = mode->mdctSize;
246       else if (frame_size != mode->mdctSize)
247          frame_size = -1;
248       if (overlap==0)
249          overlap = mode->overlap;
250       else if (overlap != mode->overlap)
251          overlap = -1;
252    }
253    if (channels>0)
254    {
255       fprintf (file, "#define CHANNELS(mode) %d\n", channels);
256       if (channels==1)
257          fprintf (file, "#define DISABLE_STEREO\n");
258    }
259    if (frame_size>0)
260    {
261       fprintf (file, "#define FRAMESIZE(mode) %d\n", frame_size);
262    }
263    if (overlap>0)
264    {
265       fprintf (file, "#define OVERLAP(mode) %d\n", overlap);
266    }
267 }
268
269 int main(int argc, char **argv)
270 {
271    int i, nb;
272    FILE *file;
273    CELTMode **m;
274    if (argc%3 != 1)
275    {
276       fprintf (stderr, "must have a multiple of 4 arguments\n");
277       return 1;
278    }
279    nb = (argc-1)/3;
280    m = malloc(nb*sizeof(CELTMode*));
281    for (i=0;i<nb;i++)
282    {
283       int Fs, ch, frame;
284       Fs      = atoi(argv[3*i+1]);
285       ch      = atoi(argv[3*i+2]);
286       frame   = atoi(argv[3*i+3]);
287       m[i] = celt051_mode_create(Fs, ch, frame, NULL);
288    }
289    file = fopen("static_modes.c", "w");
290    dump_modes(file, m, nb);
291    fclose(file);
292    file = fopen("static_modes.h", "w");
293    dump_header(file, m, nb);
294    fclose(file);
295    for (i=0;i<nb;i++)
296       celt051_mode_destroy(m[i]);
297    free(m);
298    return 0;
299 }