2
0

laplace.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Copyright (c) 2007 CSIRO
  2. Copyright (c) 2007-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 "laplace.h"
  29. #include "mathops.h"
  30. /* The minimum probability of an energy delta (out of 32768). */
  31. #define LAPLACE_LOG_MINP (0)
  32. #define LAPLACE_MINP (1<<LAPLACE_LOG_MINP)
  33. /* The minimum number of guaranteed representable energy deltas (in one
  34. direction). */
  35. #define LAPLACE_NMIN (16)
  36. /* When called, decay is positive and at most 11456. */
  37. static unsigned ec_laplace_get_freq1(unsigned fs0, int decay)
  38. {
  39. unsigned ft;
  40. ft = 32768 - LAPLACE_MINP*(2*LAPLACE_NMIN) - fs0;
  41. return ft*(opus_int32)(16384-decay)>>15;
  42. }
  43. void ec_laplace_encode(ec_enc *enc, int *value, unsigned fs, int decay)
  44. {
  45. unsigned fl;
  46. int val = *value;
  47. fl = 0;
  48. if (val)
  49. {
  50. int s;
  51. int i;
  52. s = -(val<0);
  53. val = (val+s)^s;
  54. fl = fs;
  55. fs = ec_laplace_get_freq1(fs, decay);
  56. /* Search the decaying part of the PDF.*/
  57. for (i=1; fs > 0 && i < val; i++)
  58. {
  59. fs *= 2;
  60. fl += fs+2*LAPLACE_MINP;
  61. fs = (fs*(opus_int32)decay)>>15;
  62. }
  63. /* Everything beyond that has probability LAPLACE_MINP. */
  64. if (!fs)
  65. {
  66. int di;
  67. int ndi_max;
  68. ndi_max = (32768-fl+LAPLACE_MINP-1)>>LAPLACE_LOG_MINP;
  69. ndi_max = (ndi_max-s)>>1;
  70. di = IMIN(val - i, ndi_max - 1);
  71. fl += (2*di+1+s)*LAPLACE_MINP;
  72. fs = IMIN(LAPLACE_MINP, 32768-fl);
  73. *value = (i+di+s)^s;
  74. }
  75. else
  76. {
  77. fs += LAPLACE_MINP;
  78. fl += fs&~s;
  79. }
  80. celt_assert(fl+fs<=32768);
  81. celt_assert(fs>0);
  82. }
  83. ec_encode_bin(enc, fl, fl+fs, 15);
  84. }
  85. int ec_laplace_decode(ec_dec *dec, unsigned fs, int decay)
  86. {
  87. int val=0;
  88. unsigned fl;
  89. unsigned fm;
  90. fm = ec_decode_bin(dec, 15);
  91. fl = 0;
  92. if (fm >= fs)
  93. {
  94. val++;
  95. fl = fs;
  96. fs = ec_laplace_get_freq1(fs, decay)+LAPLACE_MINP;
  97. /* Search the decaying part of the PDF.*/
  98. while(fs > LAPLACE_MINP && fm >= fl+2*fs)
  99. {
  100. fs *= 2;
  101. fl += fs;
  102. fs = ((fs-2*LAPLACE_MINP)*(opus_int32)decay)>>15;
  103. fs += LAPLACE_MINP;
  104. val++;
  105. }
  106. /* Everything beyond that has probability LAPLACE_MINP. */
  107. if (fs <= LAPLACE_MINP)
  108. {
  109. int di;
  110. di = (fm-fl)>>(LAPLACE_LOG_MINP+1);
  111. val += di;
  112. fl += 2*di*LAPLACE_MINP;
  113. }
  114. if (fm < fl+fs)
  115. val = -val;
  116. else
  117. fl += fs;
  118. }
  119. celt_assert(fl<32768);
  120. celt_assert(fs>0);
  121. celt_assert(fl<=fm);
  122. celt_assert(fm<IMIN(fl+fs,32768));
  123. ec_dec_update(dec, fl, IMIN(fl+fs,32768), 32768);
  124. return val;
  125. }