entenc.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (c) 2001-2011 Timothy B. Terriberry
  2. Copyright (c) 2008-2009 Xiph.Org Foundation */
  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. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  16. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #if !defined(_entenc_H)
  25. # define _entenc_H (1)
  26. # include <stddef.h>
  27. # include "entcode.h"
  28. /*Initializes the encoder.
  29. _buf: The buffer to store output bytes in.
  30. _size: The size of the buffer, in chars.*/
  31. void ec_enc_init(ec_enc *_this,unsigned char *_buf,opus_uint32 _size);
  32. /*Encodes a symbol given its frequency information.
  33. The frequency information must be discernable by the decoder, assuming it
  34. has read only the previous symbols from the stream.
  35. It is allowable to change the frequency information, or even the entire
  36. source alphabet, so long as the decoder can tell from the context of the
  37. previously encoded information that it is supposed to do so as well.
  38. _fl: The cumulative frequency of all symbols that come before the one to be
  39. encoded.
  40. _fh: The cumulative frequency of all symbols up to and including the one to
  41. be encoded.
  42. Together with _fl, this defines the range [_fl,_fh) in which the
  43. decoded value will fall.
  44. _ft: The sum of the frequencies of all the symbols*/
  45. void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft);
  46. /*Equivalent to ec_encode() with _ft==1<<_bits.*/
  47. void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _bits);
  48. /* Encode a bit that has a 1/(1<<_logp) probability of being a one */
  49. void ec_enc_bit_logp(ec_enc *_this,int _val,unsigned _logp);
  50. /*Encodes a symbol given an "inverse" CDF table.
  51. _s: The index of the symbol to encode.
  52. _icdf: The "inverse" CDF, such that symbol _s falls in the range
  53. [_s>0?ft-_icdf[_s-1]:0,ft-_icdf[_s]), where ft=1<<_ftb.
  54. The values must be monotonically non-increasing, and the last value
  55. must be 0.
  56. _ftb: The number of bits of precision in the cumulative distribution.*/
  57. void ec_enc_icdf(ec_enc *_this,int _s,const unsigned char *_icdf,unsigned _ftb);
  58. /*Encodes a raw unsigned integer in the stream.
  59. _fl: The integer to encode.
  60. _ft: The number of integers that can be encoded (one more than the max).
  61. This must be at least one, and no more than 2**32-1.*/
  62. void ec_enc_uint(ec_enc *_this,opus_uint32 _fl,opus_uint32 _ft);
  63. /*Encodes a sequence of raw bits in the stream.
  64. _fl: The bits to encode.
  65. _ftb: The number of bits to encode.
  66. This must be between 1 and 25, inclusive.*/
  67. void ec_enc_bits(ec_enc *_this,opus_uint32 _fl,unsigned _ftb);
  68. /*Overwrites a few bits at the very start of an existing stream, after they
  69. have already been encoded.
  70. This makes it possible to have a few flags up front, where it is easy for
  71. decoders to access them without parsing the whole stream, even if their
  72. values are not determined until late in the encoding process, without having
  73. to buffer all the intermediate symbols in the encoder.
  74. In order for this to work, at least _nbits bits must have already been
  75. encoded using probabilities that are an exact power of two.
  76. The encoder can verify the number of encoded bits is sufficient, but cannot
  77. check this latter condition.
  78. _val: The bits to encode (in the least _nbits significant bits).
  79. They will be decoded in order from most-significant to least.
  80. _nbits: The number of bits to overwrite.
  81. This must be no more than 8.*/
  82. void ec_enc_patch_initial_bits(ec_enc *_this,unsigned _val,unsigned _nbits);
  83. /*Compacts the data to fit in the target size.
  84. This moves up the raw bits at the end of the current buffer so they are at
  85. the end of the new buffer size.
  86. The caller must ensure that the amount of data that's already been written
  87. will fit in the new size.
  88. _size: The number of bytes in the new buffer.
  89. This must be large enough to contain the bits already written, and
  90. must be no larger than the existing size.*/
  91. void ec_enc_shrink(ec_enc *_this,opus_uint32 _size);
  92. /*Indicates that there are no more symbols to encode.
  93. All reamining output bytes are flushed to the output buffer.
  94. ec_enc_init() must be called before the encoder can be used again.*/
  95. void ec_enc_done(ec_enc *_this);
  96. #endif