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.

432 lines
17KB

  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2000-2009 Josh Coalson
  3. * Copyright (C) 2011-2013 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. #if HAVE_CONFIG_H
  33. # include <config.h>
  34. #endif
  35. #include <math.h>
  36. #include <string.h>
  37. #include "include/private/bitmath.h"
  38. #include "include/private/fixed.h"
  39. #include "../assert.h"
  40. #ifndef M_LN2
  41. /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
  42. #define M_LN2 0.69314718055994530942
  43. #endif
  44. #ifdef local_abs
  45. #undef local_abs
  46. #endif
  47. #define local_abs(x) ((unsigned)((x)<0? -(x) : (x)))
  48. #ifdef FLAC__INTEGER_ONLY_LIBRARY
  49. /* rbps stands for residual bits per sample
  50. *
  51. * (ln(2) * err)
  52. * rbps = log (-----------)
  53. * 2 ( n )
  54. */
  55. static FLAC__fixedpoint local__compute_rbps_integerized(FLAC__uint32 err, FLAC__uint32 n)
  56. {
  57. FLAC__uint32 rbps;
  58. unsigned bits; /* the number of bits required to represent a number */
  59. int fracbits; /* the number of bits of rbps that comprise the fractional part */
  60. FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint));
  61. FLAC__ASSERT(err > 0);
  62. FLAC__ASSERT(n > 0);
  63. FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE);
  64. if(err <= n)
  65. return 0;
  66. /*
  67. * The above two things tell us 1) n fits in 16 bits; 2) err/n > 1.
  68. * These allow us later to know we won't lose too much precision in the
  69. * fixed-point division (err<<fracbits)/n.
  70. */
  71. fracbits = (8*sizeof(err)) - (FLAC__bitmath_ilog2(err)+1);
  72. err <<= fracbits;
  73. err /= n;
  74. /* err now holds err/n with fracbits fractional bits */
  75. /*
  76. * Whittle err down to 16 bits max. 16 significant bits is enough for
  77. * our purposes.
  78. */
  79. FLAC__ASSERT(err > 0);
  80. bits = FLAC__bitmath_ilog2(err)+1;
  81. if(bits > 16) {
  82. err >>= (bits-16);
  83. fracbits -= (bits-16);
  84. }
  85. rbps = (FLAC__uint32)err;
  86. /* Multiply by fixed-point version of ln(2), with 16 fractional bits */
  87. rbps *= FLAC__FP_LN2;
  88. fracbits += 16;
  89. FLAC__ASSERT(fracbits >= 0);
  90. /* FLAC__fixedpoint_log2 requires fracbits%4 to be 0 */
  91. {
  92. const int f = fracbits & 3;
  93. if(f) {
  94. rbps >>= f;
  95. fracbits -= f;
  96. }
  97. }
  98. rbps = FLAC__fixedpoint_log2(rbps, fracbits, (unsigned)(-1));
  99. if(rbps == 0)
  100. return 0;
  101. /*
  102. * The return value must have 16 fractional bits. Since the whole part
  103. * of the base-2 log of a 32 bit number must fit in 5 bits, and fracbits
  104. * must be >= -3, these assertion allows us to be able to shift rbps
  105. * left if necessary to get 16 fracbits without losing any bits of the
  106. * whole part of rbps.
  107. *
  108. * There is a slight chance due to accumulated error that the whole part
  109. * will require 6 bits, so we use 6 in the assertion. Really though as
  110. * long as it fits in 13 bits (32 - (16 - (-3))) we are fine.
  111. */
  112. FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6);
  113. FLAC__ASSERT(fracbits >= -3);
  114. /* now shift the decimal point into place */
  115. if(fracbits < 16)
  116. return rbps << (16-fracbits);
  117. else if(fracbits > 16)
  118. return rbps >> (fracbits-16);
  119. else
  120. return rbps;
  121. }
  122. static FLAC__fixedpoint local__compute_rbps_wide_integerized(FLAC__uint64 err, FLAC__uint32 n)
  123. {
  124. FLAC__uint32 rbps;
  125. unsigned bits; /* the number of bits required to represent a number */
  126. int fracbits; /* the number of bits of rbps that comprise the fractional part */
  127. FLAC__ASSERT(sizeof(rbps) == sizeof(FLAC__fixedpoint));
  128. FLAC__ASSERT(err > 0);
  129. FLAC__ASSERT(n > 0);
  130. FLAC__ASSERT(n <= FLAC__MAX_BLOCK_SIZE);
  131. if(err <= n)
  132. return 0;
  133. /*
  134. * The above two things tell us 1) n fits in 16 bits; 2) err/n > 1.
  135. * These allow us later to know we won't lose too much precision in the
  136. * fixed-point division (err<<fracbits)/n.
  137. */
  138. fracbits = (8*sizeof(err)) - (FLAC__bitmath_ilog2_wide(err)+1);
  139. err <<= fracbits;
  140. err /= n;
  141. /* err now holds err/n with fracbits fractional bits */
  142. /*
  143. * Whittle err down to 16 bits max. 16 significant bits is enough for
  144. * our purposes.
  145. */
  146. FLAC__ASSERT(err > 0);
  147. bits = FLAC__bitmath_ilog2_wide(err)+1;
  148. if(bits > 16) {
  149. err >>= (bits-16);
  150. fracbits -= (bits-16);
  151. }
  152. rbps = (FLAC__uint32)err;
  153. /* Multiply by fixed-point version of ln(2), with 16 fractional bits */
  154. rbps *= FLAC__FP_LN2;
  155. fracbits += 16;
  156. FLAC__ASSERT(fracbits >= 0);
  157. /* FLAC__fixedpoint_log2 requires fracbits%4 to be 0 */
  158. {
  159. const int f = fracbits & 3;
  160. if(f) {
  161. rbps >>= f;
  162. fracbits -= f;
  163. }
  164. }
  165. rbps = FLAC__fixedpoint_log2(rbps, fracbits, (unsigned)(-1));
  166. if(rbps == 0)
  167. return 0;
  168. /*
  169. * The return value must have 16 fractional bits. Since the whole part
  170. * of the base-2 log of a 32 bit number must fit in 5 bits, and fracbits
  171. * must be >= -3, these assertion allows us to be able to shift rbps
  172. * left if necessary to get 16 fracbits without losing any bits of the
  173. * whole part of rbps.
  174. *
  175. * There is a slight chance due to accumulated error that the whole part
  176. * will require 6 bits, so we use 6 in the assertion. Really though as
  177. * long as it fits in 13 bits (32 - (16 - (-3))) we are fine.
  178. */
  179. FLAC__ASSERT((int)FLAC__bitmath_ilog2(rbps)+1 <= fracbits + 6);
  180. FLAC__ASSERT(fracbits >= -3);
  181. /* now shift the decimal point into place */
  182. if(fracbits < 16)
  183. return rbps << (16-fracbits);
  184. else if(fracbits > 16)
  185. return rbps >> (fracbits-16);
  186. else
  187. return rbps;
  188. }
  189. #endif
  190. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  191. unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
  192. #else
  193. unsigned FLAC__fixed_compute_best_predictor(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
  194. #endif
  195. {
  196. FLAC__int32 last_error_0 = data[-1];
  197. FLAC__int32 last_error_1 = data[-1] - data[-2];
  198. FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
  199. FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
  200. FLAC__int32 error, save;
  201. FLAC__uint32 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
  202. unsigned i, order;
  203. for(i = 0; i < data_len; i++) {
  204. error = data[i] ; total_error_0 += local_abs(error); save = error;
  205. error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error;
  206. error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error;
  207. error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error;
  208. error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
  209. }
  210. if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
  211. order = 0;
  212. else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
  213. order = 1;
  214. else if(total_error_2 < flac_min(total_error_3, total_error_4))
  215. order = 2;
  216. else if(total_error_3 < total_error_4)
  217. order = 3;
  218. else
  219. order = 4;
  220. /* Estimate the expected number of bits per residual signal sample. */
  221. /* 'total_error*' is linearly related to the variance of the residual */
  222. /* signal, so we use it directly to compute E(|x|) */
  223. FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
  224. FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
  225. FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
  226. FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
  227. FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
  228. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  229. residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
  230. residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
  231. residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
  232. residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
  233. residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
  234. #else
  235. residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_integerized(total_error_0, data_len) : 0;
  236. residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_integerized(total_error_1, data_len) : 0;
  237. residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_integerized(total_error_2, data_len) : 0;
  238. residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_integerized(total_error_3, data_len) : 0;
  239. residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_integerized(total_error_4, data_len) : 0;
  240. #endif
  241. return order;
  242. }
  243. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  244. unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
  245. #else
  246. unsigned FLAC__fixed_compute_best_predictor_wide(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1])
  247. #endif
  248. {
  249. FLAC__int32 last_error_0 = data[-1];
  250. FLAC__int32 last_error_1 = data[-1] - data[-2];
  251. FLAC__int32 last_error_2 = last_error_1 - (data[-2] - data[-3]);
  252. FLAC__int32 last_error_3 = last_error_2 - (data[-2] - 2*data[-3] + data[-4]);
  253. FLAC__int32 error, save;
  254. /* total_error_* are 64-bits to avoid overflow when encoding
  255. * erratic signals when the bits-per-sample and blocksize are
  256. * large.
  257. */
  258. FLAC__uint64 total_error_0 = 0, total_error_1 = 0, total_error_2 = 0, total_error_3 = 0, total_error_4 = 0;
  259. unsigned i, order;
  260. for(i = 0; i < data_len; i++) {
  261. error = data[i] ; total_error_0 += local_abs(error); save = error;
  262. error -= last_error_0; total_error_1 += local_abs(error); last_error_0 = save; save = error;
  263. error -= last_error_1; total_error_2 += local_abs(error); last_error_1 = save; save = error;
  264. error -= last_error_2; total_error_3 += local_abs(error); last_error_2 = save; save = error;
  265. error -= last_error_3; total_error_4 += local_abs(error); last_error_3 = save;
  266. }
  267. if(total_error_0 < flac_min(flac_min(flac_min(total_error_1, total_error_2), total_error_3), total_error_4))
  268. order = 0;
  269. else if(total_error_1 < flac_min(flac_min(total_error_2, total_error_3), total_error_4))
  270. order = 1;
  271. else if(total_error_2 < flac_min(total_error_3, total_error_4))
  272. order = 2;
  273. else if(total_error_3 < total_error_4)
  274. order = 3;
  275. else
  276. order = 4;
  277. /* Estimate the expected number of bits per residual signal sample. */
  278. /* 'total_error*' is linearly related to the variance of the residual */
  279. /* signal, so we use it directly to compute E(|x|) */
  280. FLAC__ASSERT(data_len > 0 || total_error_0 == 0);
  281. FLAC__ASSERT(data_len > 0 || total_error_1 == 0);
  282. FLAC__ASSERT(data_len > 0 || total_error_2 == 0);
  283. FLAC__ASSERT(data_len > 0 || total_error_3 == 0);
  284. FLAC__ASSERT(data_len > 0 || total_error_4 == 0);
  285. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  286. #if defined _MSC_VER || defined __MINGW32__
  287. /* with MSVC you have to spoon feed it the casting */
  288. residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
  289. residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
  290. residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
  291. residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
  292. residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)(FLAC__int64)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
  293. #else
  294. residual_bits_per_sample[0] = (FLAC__float)((total_error_0 > 0) ? log(M_LN2 * (FLAC__double)total_error_0 / (FLAC__double)data_len) / M_LN2 : 0.0);
  295. residual_bits_per_sample[1] = (FLAC__float)((total_error_1 > 0) ? log(M_LN2 * (FLAC__double)total_error_1 / (FLAC__double)data_len) / M_LN2 : 0.0);
  296. residual_bits_per_sample[2] = (FLAC__float)((total_error_2 > 0) ? log(M_LN2 * (FLAC__double)total_error_2 / (FLAC__double)data_len) / M_LN2 : 0.0);
  297. residual_bits_per_sample[3] = (FLAC__float)((total_error_3 > 0) ? log(M_LN2 * (FLAC__double)total_error_3 / (FLAC__double)data_len) / M_LN2 : 0.0);
  298. residual_bits_per_sample[4] = (FLAC__float)((total_error_4 > 0) ? log(M_LN2 * (FLAC__double)total_error_4 / (FLAC__double)data_len) / M_LN2 : 0.0);
  299. #endif
  300. #else
  301. residual_bits_per_sample[0] = (total_error_0 > 0) ? local__compute_rbps_wide_integerized(total_error_0, data_len) : 0;
  302. residual_bits_per_sample[1] = (total_error_1 > 0) ? local__compute_rbps_wide_integerized(total_error_1, data_len) : 0;
  303. residual_bits_per_sample[2] = (total_error_2 > 0) ? local__compute_rbps_wide_integerized(total_error_2, data_len) : 0;
  304. residual_bits_per_sample[3] = (total_error_3 > 0) ? local__compute_rbps_wide_integerized(total_error_3, data_len) : 0;
  305. residual_bits_per_sample[4] = (total_error_4 > 0) ? local__compute_rbps_wide_integerized(total_error_4, data_len) : 0;
  306. #endif
  307. return order;
  308. }
  309. void FLAC__fixed_compute_residual(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[])
  310. {
  311. const int idata_len = (int)data_len;
  312. int i;
  313. switch(order) {
  314. case 0:
  315. FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
  316. memcpy(residual, data, sizeof(residual[0])*data_len);
  317. break;
  318. case 1:
  319. for(i = 0; i < idata_len; i++)
  320. residual[i] = data[i] - data[i-1];
  321. break;
  322. case 2:
  323. for(i = 0; i < idata_len; i++)
  324. #if 1 /* OPT: may be faster with some compilers on some systems */
  325. residual[i] = data[i] - (data[i-1] << 1) + data[i-2];
  326. #else
  327. residual[i] = data[i] - 2*data[i-1] + data[i-2];
  328. #endif
  329. break;
  330. case 3:
  331. for(i = 0; i < idata_len; i++)
  332. #if 1 /* OPT: may be faster with some compilers on some systems */
  333. residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3];
  334. #else
  335. residual[i] = data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3];
  336. #endif
  337. break;
  338. case 4:
  339. for(i = 0; i < idata_len; i++)
  340. #if 1 /* OPT: may be faster with some compilers on some systems */
  341. residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4];
  342. #else
  343. residual[i] = data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4];
  344. #endif
  345. break;
  346. default:
  347. FLAC__ASSERT(0);
  348. }
  349. }
  350. void FLAC__fixed_restore_signal(const FLAC__int32 residual[], unsigned data_len, unsigned order, FLAC__int32 data[])
  351. {
  352. int i, idata_len = (int)data_len;
  353. switch(order) {
  354. case 0:
  355. FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
  356. memcpy(data, residual, sizeof(residual[0])*data_len);
  357. break;
  358. case 1:
  359. for(i = 0; i < idata_len; i++)
  360. data[i] = residual[i] + data[i-1];
  361. break;
  362. case 2:
  363. for(i = 0; i < idata_len; i++)
  364. #if 1 /* OPT: may be faster with some compilers on some systems */
  365. data[i] = residual[i] + (data[i-1]<<1) - data[i-2];
  366. #else
  367. data[i] = residual[i] + 2*data[i-1] - data[i-2];
  368. #endif
  369. break;
  370. case 3:
  371. for(i = 0; i < idata_len; i++)
  372. #if 1 /* OPT: may be faster with some compilers on some systems */
  373. data[i] = residual[i] + (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) + data[i-3];
  374. #else
  375. data[i] = residual[i] + 3*data[i-1] - 3*data[i-2] + data[i-3];
  376. #endif
  377. break;
  378. case 4:
  379. for(i = 0; i < idata_len; i++)
  380. #if 1 /* OPT: may be faster with some compilers on some systems */
  381. data[i] = residual[i] + ((data[i-1]+data[i-3])<<2) - ((data[i-2]<<2) + (data[i-2]<<1)) - data[i-4];
  382. #else
  383. data[i] = residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4];
  384. #endif
  385. break;
  386. default:
  387. FLAC__ASSERT(0);
  388. }
  389. }