The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
9.3KB

  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2004-2009 Josh Coalson
  3. * Copyright (C) 2011-2014 Xiph.Org Foundation
  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. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * - Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * - Neither the name of the Xiph.org Foundation nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifdef HAVE_CONFIG_H
  33. # include <config.h>
  34. #endif
  35. #include "../assert.h"
  36. #include "../compat.h"
  37. #include "include/private/float.h"
  38. #ifdef FLAC__INTEGER_ONLY_LIBRARY
  39. const FLAC__fixedpoint FLAC__FP_ZERO = 0;
  40. const FLAC__fixedpoint FLAC__FP_ONE_HALF = 0x00008000;
  41. const FLAC__fixedpoint FLAC__FP_ONE = 0x00010000;
  42. const FLAC__fixedpoint FLAC__FP_LN2 = 45426;
  43. const FLAC__fixedpoint FLAC__FP_E = 178145;
  44. /* Lookup tables for Knuth's logarithm algorithm */
  45. #define LOG2_LOOKUP_PRECISION 16
  46. static const FLAC__uint32 log2_lookup[][LOG2_LOOKUP_PRECISION] = {
  47. {
  48. /*
  49. * 0 fraction bits
  50. */
  51. /* undefined */ 0x00000000,
  52. /* lg(2/1) = */ 0x00000001,
  53. /* lg(4/3) = */ 0x00000000,
  54. /* lg(8/7) = */ 0x00000000,
  55. /* lg(16/15) = */ 0x00000000,
  56. /* lg(32/31) = */ 0x00000000,
  57. /* lg(64/63) = */ 0x00000000,
  58. /* lg(128/127) = */ 0x00000000,
  59. /* lg(256/255) = */ 0x00000000,
  60. /* lg(512/511) = */ 0x00000000,
  61. /* lg(1024/1023) = */ 0x00000000,
  62. /* lg(2048/2047) = */ 0x00000000,
  63. /* lg(4096/4095) = */ 0x00000000,
  64. /* lg(8192/8191) = */ 0x00000000,
  65. /* lg(16384/16383) = */ 0x00000000,
  66. /* lg(32768/32767) = */ 0x00000000
  67. },
  68. {
  69. /*
  70. * 4 fraction bits
  71. */
  72. /* undefined */ 0x00000000,
  73. /* lg(2/1) = */ 0x00000010,
  74. /* lg(4/3) = */ 0x00000007,
  75. /* lg(8/7) = */ 0x00000003,
  76. /* lg(16/15) = */ 0x00000001,
  77. /* lg(32/31) = */ 0x00000001,
  78. /* lg(64/63) = */ 0x00000000,
  79. /* lg(128/127) = */ 0x00000000,
  80. /* lg(256/255) = */ 0x00000000,
  81. /* lg(512/511) = */ 0x00000000,
  82. /* lg(1024/1023) = */ 0x00000000,
  83. /* lg(2048/2047) = */ 0x00000000,
  84. /* lg(4096/4095) = */ 0x00000000,
  85. /* lg(8192/8191) = */ 0x00000000,
  86. /* lg(16384/16383) = */ 0x00000000,
  87. /* lg(32768/32767) = */ 0x00000000
  88. },
  89. {
  90. /*
  91. * 8 fraction bits
  92. */
  93. /* undefined */ 0x00000000,
  94. /* lg(2/1) = */ 0x00000100,
  95. /* lg(4/3) = */ 0x0000006a,
  96. /* lg(8/7) = */ 0x00000031,
  97. /* lg(16/15) = */ 0x00000018,
  98. /* lg(32/31) = */ 0x0000000c,
  99. /* lg(64/63) = */ 0x00000006,
  100. /* lg(128/127) = */ 0x00000003,
  101. /* lg(256/255) = */ 0x00000001,
  102. /* lg(512/511) = */ 0x00000001,
  103. /* lg(1024/1023) = */ 0x00000000,
  104. /* lg(2048/2047) = */ 0x00000000,
  105. /* lg(4096/4095) = */ 0x00000000,
  106. /* lg(8192/8191) = */ 0x00000000,
  107. /* lg(16384/16383) = */ 0x00000000,
  108. /* lg(32768/32767) = */ 0x00000000
  109. },
  110. {
  111. /*
  112. * 12 fraction bits
  113. */
  114. /* undefined */ 0x00000000,
  115. /* lg(2/1) = */ 0x00001000,
  116. /* lg(4/3) = */ 0x000006a4,
  117. /* lg(8/7) = */ 0x00000315,
  118. /* lg(16/15) = */ 0x0000017d,
  119. /* lg(32/31) = */ 0x000000bc,
  120. /* lg(64/63) = */ 0x0000005d,
  121. /* lg(128/127) = */ 0x0000002e,
  122. /* lg(256/255) = */ 0x00000017,
  123. /* lg(512/511) = */ 0x0000000c,
  124. /* lg(1024/1023) = */ 0x00000006,
  125. /* lg(2048/2047) = */ 0x00000003,
  126. /* lg(4096/4095) = */ 0x00000001,
  127. /* lg(8192/8191) = */ 0x00000001,
  128. /* lg(16384/16383) = */ 0x00000000,
  129. /* lg(32768/32767) = */ 0x00000000
  130. },
  131. {
  132. /*
  133. * 16 fraction bits
  134. */
  135. /* undefined */ 0x00000000,
  136. /* lg(2/1) = */ 0x00010000,
  137. /* lg(4/3) = */ 0x00006a40,
  138. /* lg(8/7) = */ 0x00003151,
  139. /* lg(16/15) = */ 0x000017d6,
  140. /* lg(32/31) = */ 0x00000bba,
  141. /* lg(64/63) = */ 0x000005d1,
  142. /* lg(128/127) = */ 0x000002e6,
  143. /* lg(256/255) = */ 0x00000172,
  144. /* lg(512/511) = */ 0x000000b9,
  145. /* lg(1024/1023) = */ 0x0000005c,
  146. /* lg(2048/2047) = */ 0x0000002e,
  147. /* lg(4096/4095) = */ 0x00000017,
  148. /* lg(8192/8191) = */ 0x0000000c,
  149. /* lg(16384/16383) = */ 0x00000006,
  150. /* lg(32768/32767) = */ 0x00000003
  151. },
  152. {
  153. /*
  154. * 20 fraction bits
  155. */
  156. /* undefined */ 0x00000000,
  157. /* lg(2/1) = */ 0x00100000,
  158. /* lg(4/3) = */ 0x0006a3fe,
  159. /* lg(8/7) = */ 0x00031513,
  160. /* lg(16/15) = */ 0x00017d60,
  161. /* lg(32/31) = */ 0x0000bb9d,
  162. /* lg(64/63) = */ 0x00005d10,
  163. /* lg(128/127) = */ 0x00002e59,
  164. /* lg(256/255) = */ 0x00001721,
  165. /* lg(512/511) = */ 0x00000b8e,
  166. /* lg(1024/1023) = */ 0x000005c6,
  167. /* lg(2048/2047) = */ 0x000002e3,
  168. /* lg(4096/4095) = */ 0x00000171,
  169. /* lg(8192/8191) = */ 0x000000b9,
  170. /* lg(16384/16383) = */ 0x0000005c,
  171. /* lg(32768/32767) = */ 0x0000002e
  172. },
  173. {
  174. /*
  175. * 24 fraction bits
  176. */
  177. /* undefined */ 0x00000000,
  178. /* lg(2/1) = */ 0x01000000,
  179. /* lg(4/3) = */ 0x006a3fe6,
  180. /* lg(8/7) = */ 0x00315130,
  181. /* lg(16/15) = */ 0x0017d605,
  182. /* lg(32/31) = */ 0x000bb9ca,
  183. /* lg(64/63) = */ 0x0005d0fc,
  184. /* lg(128/127) = */ 0x0002e58f,
  185. /* lg(256/255) = */ 0x0001720e,
  186. /* lg(512/511) = */ 0x0000b8d8,
  187. /* lg(1024/1023) = */ 0x00005c61,
  188. /* lg(2048/2047) = */ 0x00002e2d,
  189. /* lg(4096/4095) = */ 0x00001716,
  190. /* lg(8192/8191) = */ 0x00000b8b,
  191. /* lg(16384/16383) = */ 0x000005c5,
  192. /* lg(32768/32767) = */ 0x000002e3
  193. },
  194. {
  195. /*
  196. * 28 fraction bits
  197. */
  198. /* undefined */ 0x00000000,
  199. /* lg(2/1) = */ 0x10000000,
  200. /* lg(4/3) = */ 0x06a3fe5c,
  201. /* lg(8/7) = */ 0x03151301,
  202. /* lg(16/15) = */ 0x017d6049,
  203. /* lg(32/31) = */ 0x00bb9ca6,
  204. /* lg(64/63) = */ 0x005d0fba,
  205. /* lg(128/127) = */ 0x002e58f7,
  206. /* lg(256/255) = */ 0x001720da,
  207. /* lg(512/511) = */ 0x000b8d87,
  208. /* lg(1024/1023) = */ 0x0005c60b,
  209. /* lg(2048/2047) = */ 0x0002e2d7,
  210. /* lg(4096/4095) = */ 0x00017160,
  211. /* lg(8192/8191) = */ 0x0000b8ad,
  212. /* lg(16384/16383) = */ 0x00005c56,
  213. /* lg(32768/32767) = */ 0x00002e2b
  214. }
  215. };
  216. #if 0
  217. static const FLAC__uint64 log2_lookup_wide[] = {
  218. {
  219. /*
  220. * 32 fraction bits
  221. */
  222. /* undefined */ 0x00000000,
  223. /* lg(2/1) = */ FLAC__U64L(0x100000000),
  224. /* lg(4/3) = */ FLAC__U64L(0x6a3fe5c6),
  225. /* lg(8/7) = */ FLAC__U64L(0x31513015),
  226. /* lg(16/15) = */ FLAC__U64L(0x17d60497),
  227. /* lg(32/31) = */ FLAC__U64L(0x0bb9ca65),
  228. /* lg(64/63) = */ FLAC__U64L(0x05d0fba2),
  229. /* lg(128/127) = */ FLAC__U64L(0x02e58f74),
  230. /* lg(256/255) = */ FLAC__U64L(0x01720d9c),
  231. /* lg(512/511) = */ FLAC__U64L(0x00b8d875),
  232. /* lg(1024/1023) = */ FLAC__U64L(0x005c60aa),
  233. /* lg(2048/2047) = */ FLAC__U64L(0x002e2d72),
  234. /* lg(4096/4095) = */ FLAC__U64L(0x00171600),
  235. /* lg(8192/8191) = */ FLAC__U64L(0x000b8ad2),
  236. /* lg(16384/16383) = */ FLAC__U64L(0x0005c55d),
  237. /* lg(32768/32767) = */ FLAC__U64L(0x0002e2ac)
  238. },
  239. {
  240. /*
  241. * 48 fraction bits
  242. */
  243. /* undefined */ 0x00000000,
  244. /* lg(2/1) = */ FLAC__U64L(0x1000000000000),
  245. /* lg(4/3) = */ FLAC__U64L(0x6a3fe5c60429),
  246. /* lg(8/7) = */ FLAC__U64L(0x315130157f7a),
  247. /* lg(16/15) = */ FLAC__U64L(0x17d60496cfbb),
  248. /* lg(32/31) = */ FLAC__U64L(0xbb9ca64ecac),
  249. /* lg(64/63) = */ FLAC__U64L(0x5d0fba187cd),
  250. /* lg(128/127) = */ FLAC__U64L(0x2e58f7441ee),
  251. /* lg(256/255) = */ FLAC__U64L(0x1720d9c06a8),
  252. /* lg(512/511) = */ FLAC__U64L(0xb8d8752173),
  253. /* lg(1024/1023) = */ FLAC__U64L(0x5c60aa252e),
  254. /* lg(2048/2047) = */ FLAC__U64L(0x2e2d71b0d8),
  255. /* lg(4096/4095) = */ FLAC__U64L(0x1716001719),
  256. /* lg(8192/8191) = */ FLAC__U64L(0xb8ad1de1b),
  257. /* lg(16384/16383) = */ FLAC__U64L(0x5c55d640d),
  258. /* lg(32768/32767) = */ FLAC__U64L(0x2e2abcf52)
  259. }
  260. };
  261. #endif
  262. FLAC__uint32 FLAC__fixedpoint_log2(FLAC__uint32 x, unsigned fracbits, unsigned precision)
  263. {
  264. const FLAC__uint32 ONE = (1u << fracbits);
  265. const FLAC__uint32 *table = log2_lookup[fracbits >> 2];
  266. FLAC__ASSERT(fracbits < 32);
  267. FLAC__ASSERT((fracbits & 0x3) == 0);
  268. if(x < ONE)
  269. return 0;
  270. if(precision > LOG2_LOOKUP_PRECISION)
  271. precision = LOG2_LOOKUP_PRECISION;
  272. /* Knuth's algorithm for computing logarithms, optimized for base-2 with lookup tables */
  273. {
  274. FLAC__uint32 y = 0;
  275. FLAC__uint32 z = x >> 1, k = 1;
  276. while (x > ONE && k < precision) {
  277. if (x - z >= ONE) {
  278. x -= z;
  279. z = x >> k;
  280. y += table[k];
  281. }
  282. else {
  283. z >>= 1;
  284. k++;
  285. }
  286. }
  287. return y;
  288. }
  289. }
  290. #endif /* defined FLAC__INTEGER_ONLY_LIBRARY */