dump_modes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* Copyright (c) 2008 CSIRO
  2. Copyright (c) 2008-2009 Xiph.Org Foundation
  3. Written by Jean-Marc Valin */
  4. /*
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  17. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include "modes.h"
  31. #include "celt.h"
  32. #include "rate.h"
  33. #include "dump_modes_arch.h"
  34. #define INT16 "%d"
  35. #define INT32 "%d"
  36. #define FLOAT "%#0.8gf"
  37. #ifdef FIXED_POINT
  38. #define WORD16 INT16
  39. #define WORD32 INT32
  40. #else
  41. #define WORD16 FLOAT
  42. #define WORD32 FLOAT
  43. #endif
  44. void dump_modes(FILE *file, CELTMode **modes, int nb_modes)
  45. {
  46. int i, j, k;
  47. int mdct_twiddles_size;
  48. fprintf(file, "/* The contents of this file was automatically generated by dump_modes.c\n");
  49. fprintf(file, " with arguments:");
  50. for (i=0;i<nb_modes;i++)
  51. {
  52. CELTMode *mode = modes[i];
  53. fprintf(file, " %d %d",mode->Fs,mode->shortMdctSize*mode->nbShortMdcts);
  54. }
  55. fprintf(file, "\n It contains static definitions for some pre-defined modes. */\n");
  56. fprintf(file, "#include \"modes.h\"\n");
  57. fprintf(file, "#include \"rate.h\"\n");
  58. fprintf(file, "\n#ifdef HAVE_ARM_NE10\n");
  59. fprintf(file, "#define OVERRIDE_FFT 1\n");
  60. fprintf(file, "#include \"%s\"\n", ARM_NE10_ARCH_FILE_NAME);
  61. fprintf(file, "#endif\n");
  62. fprintf(file, "\n");
  63. for (i=0;i<nb_modes;i++)
  64. {
  65. CELTMode *mode = modes[i];
  66. int mdctSize;
  67. int standard, framerate;
  68. mdctSize = mode->shortMdctSize*mode->nbShortMdcts;
  69. standard = (mode->Fs == 400*(opus_int32)mode->shortMdctSize);
  70. framerate = mode->Fs/mode->shortMdctSize;
  71. if (!standard)
  72. {
  73. fprintf(file, "#ifndef DEF_EBANDS%d_%d\n", mode->Fs, mdctSize);
  74. fprintf(file, "#define DEF_EBANDS%d_%d\n", mode->Fs, mdctSize);
  75. fprintf (file, "static const opus_int16 eBands%d_%d[%d] = {\n", mode->Fs, mdctSize, mode->nbEBands+2);
  76. for (j=0;j<mode->nbEBands+2;j++)
  77. fprintf (file, "%d, ", mode->eBands[j]);
  78. fprintf (file, "};\n");
  79. fprintf(file, "#endif\n");
  80. fprintf(file, "\n");
  81. }
  82. fprintf(file, "#ifndef DEF_WINDOW%d\n", mode->overlap);
  83. fprintf(file, "#define DEF_WINDOW%d\n", mode->overlap);
  84. fprintf (file, "static const opus_val16 window%d[%d] = {\n", mode->overlap, mode->overlap);
  85. for (j=0;j<mode->overlap;j++)
  86. fprintf (file, WORD16 ",%c", mode->window[j],(j+6)%5==0?'\n':' ');
  87. fprintf (file, "};\n");
  88. fprintf(file, "#endif\n");
  89. fprintf(file, "\n");
  90. if (!standard)
  91. {
  92. fprintf(file, "#ifndef DEF_ALLOC_VECTORS%d_%d\n", mode->Fs, mdctSize);
  93. fprintf(file, "#define DEF_ALLOC_VECTORS%d_%d\n", mode->Fs, mdctSize);
  94. fprintf (file, "static const unsigned char allocVectors%d_%d[%d] = {\n", mode->Fs, mdctSize, mode->nbEBands*mode->nbAllocVectors);
  95. for (j=0;j<mode->nbAllocVectors;j++)
  96. {
  97. for (k=0;k<mode->nbEBands;k++)
  98. fprintf (file, "%2d, ", mode->allocVectors[j*mode->nbEBands+k]);
  99. fprintf (file, "\n");
  100. }
  101. fprintf (file, "};\n");
  102. fprintf(file, "#endif\n");
  103. fprintf(file, "\n");
  104. }
  105. fprintf(file, "#ifndef DEF_LOGN%d\n", framerate);
  106. fprintf(file, "#define DEF_LOGN%d\n", framerate);
  107. fprintf (file, "static const opus_int16 logN%d[%d] = {\n", framerate, mode->nbEBands);
  108. for (j=0;j<mode->nbEBands;j++)
  109. fprintf (file, "%d, ", mode->logN[j]);
  110. fprintf (file, "};\n");
  111. fprintf(file, "#endif\n");
  112. fprintf(file, "\n");
  113. /* Pulse cache */
  114. fprintf(file, "#ifndef DEF_PULSE_CACHE%d\n", mode->Fs/mdctSize);
  115. fprintf(file, "#define DEF_PULSE_CACHE%d\n", mode->Fs/mdctSize);
  116. fprintf (file, "static const opus_int16 cache_index%d[%d] = {\n", mode->Fs/mdctSize, (mode->maxLM+2)*mode->nbEBands);
  117. for (j=0;j<mode->nbEBands*(mode->maxLM+2);j++)
  118. fprintf (file, "%d,%c", mode->cache.index[j],(j+16)%15==0?'\n':' ');
  119. fprintf (file, "};\n");
  120. fprintf (file, "static const unsigned char cache_bits%d[%d] = {\n", mode->Fs/mdctSize, mode->cache.size);
  121. for (j=0;j<mode->cache.size;j++)
  122. fprintf (file, "%d,%c", mode->cache.bits[j],(j+16)%15==0?'\n':' ');
  123. fprintf (file, "};\n");
  124. fprintf (file, "static const unsigned char cache_caps%d[%d] = {\n", mode->Fs/mdctSize, (mode->maxLM+1)*2*mode->nbEBands);
  125. for (j=0;j<(mode->maxLM+1)*2*mode->nbEBands;j++)
  126. fprintf (file, "%d,%c", mode->cache.caps[j],(j+16)%15==0?'\n':' ');
  127. fprintf (file, "};\n");
  128. fprintf(file, "#endif\n");
  129. fprintf(file, "\n");
  130. /* FFT twiddles */
  131. fprintf(file, "#ifndef FFT_TWIDDLES%d_%d\n", mode->Fs, mdctSize);
  132. fprintf(file, "#define FFT_TWIDDLES%d_%d\n", mode->Fs, mdctSize);
  133. fprintf (file, "static const kiss_twiddle_cpx fft_twiddles%d_%d[%d] = {\n",
  134. mode->Fs, mdctSize, mode->mdct.kfft[0]->nfft);
  135. for (j=0;j<mode->mdct.kfft[0]->nfft;j++)
  136. fprintf (file, "{" WORD16 ", " WORD16 "},%c", mode->mdct.kfft[0]->twiddles[j].r, mode->mdct.kfft[0]->twiddles[j].i,(j+3)%2==0?'\n':' ');
  137. fprintf (file, "};\n");
  138. #ifdef OVERRIDE_FFT
  139. dump_mode_arch(mode);
  140. #endif
  141. /* FFT Bitrev tables */
  142. for (k=0;k<=mode->mdct.maxshift;k++)
  143. {
  144. fprintf(file, "#ifndef FFT_BITREV%d\n", mode->mdct.kfft[k]->nfft);
  145. fprintf(file, "#define FFT_BITREV%d\n", mode->mdct.kfft[k]->nfft);
  146. fprintf (file, "static const opus_int16 fft_bitrev%d[%d] = {\n",
  147. mode->mdct.kfft[k]->nfft, mode->mdct.kfft[k]->nfft);
  148. for (j=0;j<mode->mdct.kfft[k]->nfft;j++)
  149. fprintf (file, "%d,%c", mode->mdct.kfft[k]->bitrev[j],(j+16)%15==0?'\n':' ');
  150. fprintf (file, "};\n");
  151. fprintf(file, "#endif\n");
  152. fprintf(file, "\n");
  153. }
  154. /* FFT States */
  155. for (k=0;k<=mode->mdct.maxshift;k++)
  156. {
  157. fprintf(file, "#ifndef FFT_STATE%d_%d_%d\n", mode->Fs, mdctSize, k);
  158. fprintf(file, "#define FFT_STATE%d_%d_%d\n", mode->Fs, mdctSize, k);
  159. fprintf (file, "static const kiss_fft_state fft_state%d_%d_%d = {\n",
  160. mode->Fs, mdctSize, k);
  161. fprintf (file, "%d, /* nfft */\n", mode->mdct.kfft[k]->nfft);
  162. fprintf (file, WORD16 ", /* scale */\n", mode->mdct.kfft[k]->scale);
  163. #ifdef FIXED_POINT
  164. fprintf (file, "%d, /* scale_shift */\n", mode->mdct.kfft[k]->scale_shift);
  165. #endif
  166. fprintf (file, "%d, /* shift */\n", mode->mdct.kfft[k]->shift);
  167. fprintf (file, "{");
  168. for (j=0;j<2*MAXFACTORS;j++)
  169. fprintf (file, "%d, ", mode->mdct.kfft[k]->factors[j]);
  170. fprintf (file, "}, /* factors */\n");
  171. fprintf (file, "fft_bitrev%d, /* bitrev */\n", mode->mdct.kfft[k]->nfft);
  172. fprintf (file, "fft_twiddles%d_%d, /* bitrev */\n", mode->Fs, mdctSize);
  173. fprintf (file, "#ifdef OVERRIDE_FFT\n");
  174. fprintf (file, "(arch_fft_state *)&cfg_arch_%d,\n", mode->mdct.kfft[k]->nfft);
  175. fprintf (file, "#else\n");
  176. fprintf (file, "NULL,\n");
  177. fprintf(file, "#endif\n");
  178. fprintf (file, "};\n");
  179. fprintf(file, "#endif\n");
  180. fprintf(file, "\n");
  181. }
  182. fprintf(file, "#endif\n");
  183. fprintf(file, "\n");
  184. /* MDCT twiddles */
  185. mdct_twiddles_size = mode->mdct.n-(mode->mdct.n/2>>mode->mdct.maxshift);
  186. fprintf(file, "#ifndef MDCT_TWIDDLES%d\n", mdctSize);
  187. fprintf(file, "#define MDCT_TWIDDLES%d\n", mdctSize);
  188. fprintf (file, "static const opus_val16 mdct_twiddles%d[%d] = {\n",
  189. mdctSize, mdct_twiddles_size);
  190. for (j=0;j<mdct_twiddles_size;j++)
  191. fprintf (file, WORD16 ",%c", mode->mdct.trig[j],(j+6)%5==0?'\n':' ');
  192. fprintf (file, "};\n");
  193. fprintf(file, "#endif\n");
  194. fprintf(file, "\n");
  195. /* Print the actual mode data */
  196. fprintf(file, "static const CELTMode mode%d_%d_%d = {\n", mode->Fs, mdctSize, mode->overlap);
  197. fprintf(file, INT32 ", /* Fs */\n", mode->Fs);
  198. fprintf(file, "%d, /* overlap */\n", mode->overlap);
  199. fprintf(file, "%d, /* nbEBands */\n", mode->nbEBands);
  200. fprintf(file, "%d, /* effEBands */\n", mode->effEBands);
  201. fprintf(file, "{");
  202. for (j=0;j<4;j++)
  203. fprintf(file, WORD16 ", ", mode->preemph[j]);
  204. fprintf(file, "}, /* preemph */\n");
  205. if (standard)
  206. fprintf(file, "eband5ms, /* eBands */\n");
  207. else
  208. fprintf(file, "eBands%d_%d, /* eBands */\n", mode->Fs, mdctSize);
  209. fprintf(file, "%d, /* maxLM */\n", mode->maxLM);
  210. fprintf(file, "%d, /* nbShortMdcts */\n", mode->nbShortMdcts);
  211. fprintf(file, "%d, /* shortMdctSize */\n", mode->shortMdctSize);
  212. fprintf(file, "%d, /* nbAllocVectors */\n", mode->nbAllocVectors);
  213. if (standard)
  214. fprintf(file, "band_allocation, /* allocVectors */\n");
  215. else
  216. fprintf(file, "allocVectors%d_%d, /* allocVectors */\n", mode->Fs, mdctSize);
  217. fprintf(file, "logN%d, /* logN */\n", framerate);
  218. fprintf(file, "window%d, /* window */\n", mode->overlap);
  219. fprintf(file, "{%d, %d, {", mode->mdct.n, mode->mdct.maxshift);
  220. for (k=0;k<=mode->mdct.maxshift;k++)
  221. fprintf(file, "&fft_state%d_%d_%d, ", mode->Fs, mdctSize, k);
  222. fprintf (file, "}, mdct_twiddles%d}, /* mdct */\n", mdctSize);
  223. fprintf(file, "{%d, cache_index%d, cache_bits%d, cache_caps%d}, /* cache */\n",
  224. mode->cache.size, mode->Fs/mdctSize, mode->Fs/mdctSize, mode->Fs/mdctSize);
  225. fprintf(file, "};\n");
  226. }
  227. fprintf(file, "\n");
  228. fprintf(file, "/* List of all the available modes */\n");
  229. fprintf(file, "#define TOTAL_MODES %d\n", nb_modes);
  230. fprintf(file, "static const CELTMode * const static_mode_list[TOTAL_MODES] = {\n");
  231. for (i=0;i<nb_modes;i++)
  232. {
  233. CELTMode *mode = modes[i];
  234. int mdctSize;
  235. mdctSize = mode->shortMdctSize*mode->nbShortMdcts;
  236. fprintf(file, "&mode%d_%d_%d,\n", mode->Fs, mdctSize, mode->overlap);
  237. }
  238. fprintf(file, "};\n");
  239. }
  240. void dump_header(FILE *file, CELTMode **modes, int nb_modes)
  241. {
  242. int i;
  243. int channels = 0;
  244. int frame_size = 0;
  245. int overlap = 0;
  246. fprintf (file, "/* This header file is generated automatically*/\n");
  247. for (i=0;i<nb_modes;i++)
  248. {
  249. CELTMode *mode = modes[i];
  250. if (frame_size==0)
  251. frame_size = mode->shortMdctSize*mode->nbShortMdcts;
  252. else if (frame_size != mode->shortMdctSize*mode->nbShortMdcts)
  253. frame_size = -1;
  254. if (overlap==0)
  255. overlap = mode->overlap;
  256. else if (overlap != mode->overlap)
  257. overlap = -1;
  258. }
  259. if (channels>0)
  260. {
  261. fprintf (file, "#define CHANNELS(mode) %d\n", channels);
  262. if (channels==1)
  263. fprintf (file, "#define DISABLE_STEREO\n");
  264. }
  265. if (frame_size>0)
  266. {
  267. fprintf (file, "#define FRAMESIZE(mode) %d\n", frame_size);
  268. }
  269. if (overlap>0)
  270. {
  271. fprintf (file, "#define OVERLAP(mode) %d\n", overlap);
  272. }
  273. }
  274. #ifdef FIXED_POINT
  275. #define BASENAME "static_modes_fixed"
  276. #else
  277. #define BASENAME "static_modes_float"
  278. #endif
  279. int main(int argc, char **argv)
  280. {
  281. int i, nb;
  282. FILE *file;
  283. CELTMode **m;
  284. if (argc%2 != 1 || argc<3)
  285. {
  286. fprintf (stderr, "Usage: %s rate frame_size [rate frame_size] [rate frame_size]...\n",argv[0]);
  287. return 1;
  288. }
  289. nb = (argc-1)/2;
  290. m = malloc(nb*sizeof(CELTMode*));
  291. for (i=0;i<nb;i++)
  292. {
  293. int Fs, frame;
  294. Fs = atoi(argv[2*i+1]);
  295. frame = atoi(argv[2*i+2]);
  296. m[i] = opus_custom_mode_create(Fs, frame, NULL);
  297. if (m[i]==NULL)
  298. {
  299. fprintf(stderr,"Error creating mode with Fs=%s, frame_size=%s\n",
  300. argv[2*i+1],argv[2*i+2]);
  301. return EXIT_FAILURE;
  302. }
  303. }
  304. file = fopen(BASENAME ".h", "w");
  305. #ifdef OVERRIDE_FFT
  306. dump_modes_arch_init(m, nb);
  307. #endif
  308. dump_modes(file, m, nb);
  309. fclose(file);
  310. #ifdef OVERRIDE_FFT
  311. dump_modes_arch_finalize();
  312. #endif
  313. for (i=0;i<nb;i++)
  314. opus_custom_mode_destroy(m[i]);
  315. free(m);
  316. return 0;
  317. }