rate.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (c) 2007-2008 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. #ifndef RATE_H
  26. #define RATE_H
  27. #define MAX_PSEUDO 40
  28. #define LOG_MAX_PSEUDO 6
  29. #define CELT_MAX_PULSES 128
  30. #define MAX_FINE_BITS 8
  31. #define FINE_OFFSET 21
  32. #define QTHETA_OFFSET 4
  33. #define QTHETA_OFFSET_TWOPHASE 16
  34. #include "cwrs.h"
  35. #include "modes.h"
  36. void compute_pulse_cache(CELTMode *m, int LM);
  37. static OPUS_INLINE int get_pulses(int i)
  38. {
  39. return i<8 ? i : (8 + (i&7)) << ((i>>3)-1);
  40. }
  41. static OPUS_INLINE int bits2pulses(const CELTMode *m, int band, int LM, int bits)
  42. {
  43. int i;
  44. int lo, hi;
  45. const unsigned char *cache;
  46. LM++;
  47. cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
  48. lo = 0;
  49. hi = cache[0];
  50. bits--;
  51. for (i=0;i<LOG_MAX_PSEUDO;i++)
  52. {
  53. int mid = (lo+hi+1)>>1;
  54. /* OPT: Make sure this is implemented with a conditional move */
  55. if ((int)cache[mid] >= bits)
  56. hi = mid;
  57. else
  58. lo = mid;
  59. }
  60. if (bits- (lo == 0 ? -1 : (int)cache[lo]) <= (int)cache[hi]-bits)
  61. return lo;
  62. else
  63. return hi;
  64. }
  65. static OPUS_INLINE int pulses2bits(const CELTMode *m, int band, int LM, int pulses)
  66. {
  67. const unsigned char *cache;
  68. LM++;
  69. cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
  70. return pulses == 0 ? 0 : cache[pulses]+1;
  71. }
  72. /** Compute the pulse allocation, i.e. how many pulses will go in each
  73. * band.
  74. @param m mode
  75. @param offsets Requested increase or decrease in the number of bits for
  76. each band
  77. @param total Number of bands
  78. @param pulses Number of pulses per band (returned)
  79. @return Total number of bits allocated
  80. */
  81. int clt_compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stereo,
  82. opus_int32 total, opus_int32 *balance, int *pulses, int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev, int signalBandwidth);
  83. #endif