flac_fixed.c 17 KB

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