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.

419 lines
16KB

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