speex_bits.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright (C) 2002 Jean-Marc Valin */
  2. /**
  3. @file speex_bits.h
  4. @brief Handles bit packing/unpacking
  5. */
  6. /*
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions
  9. are met:
  10. - Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  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. - 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. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  22. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef BITS_H
  31. #define BITS_H
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /** Bit-packing data structure representing (part of) a bit-stream. */
  36. typedef struct SpeexBits {
  37. char *chars; /**< "raw" data */
  38. int nbBits; /**< Total number of bits stored in the stream*/
  39. int charPtr; /**< Position of the byte "cursor" */
  40. int bitPtr; /**< Position of the bit "cursor" within the current char */
  41. int owner; /**< Does the struct "own" the "raw" buffer (member "chars") */
  42. int overflow;/**< Set to one if we try to read past the valid data */
  43. int buf_size;/**< Allocated size for buffer */
  44. int reserved1; /**< Reserved for future use */
  45. void *reserved2; /**< Reserved for future use */
  46. } SpeexBits;
  47. /** Initializes and allocates resources for a SpeexBits struct */
  48. void speex_bits_init(SpeexBits *bits);
  49. /** Initializes SpeexBits struct using a pre-allocated buffer*/
  50. void speex_bits_init_buffer(SpeexBits *bits, void *buff, int buf_size);
  51. /** Frees all resources associated to a SpeexBits struct. Right now this does nothing since no resources are allocated, but this could change in the future.*/
  52. void speex_bits_destroy(SpeexBits *bits);
  53. /** Resets bits to initial value (just after initialization, erasing content)*/
  54. void speex_bits_reset(SpeexBits *bits);
  55. /** Rewind the bit-stream to the beginning (ready for read) without erasing the content */
  56. void speex_bits_rewind(SpeexBits *bits);
  57. /** Initializes the bit-stream from the data in an area of memory */
  58. void speex_bits_read_from(SpeexBits *bits, char *bytes, int len);
  59. /** Append bytes to the bit-stream
  60. * @param bits Bit-stream to operate on
  61. * @param bytes pointer to the bytes what will be appended
  62. * @param len Number of bytes of append
  63. */
  64. void speex_bits_read_whole_bytes(SpeexBits *bits, char *bytes, int len);
  65. /** Write the content of a bit-stream to an area of memory */
  66. int speex_bits_write(SpeexBits *bits, char *bytes, int max_len);
  67. /** Like speex_bits_write, but writes only the complete bytes in the stream. Also removes the written bytes from the stream */
  68. int speex_bits_write_whole_bytes(SpeexBits *bits, char *bytes, int max_len);
  69. /** Append bits to the bit-stream
  70. * @param bits Bit-stream to operate on
  71. * @param data Value to append as integer
  72. * @param nbBits number of bits to consider in "data"
  73. */
  74. void speex_bits_pack(SpeexBits *bits, int data, int nbBits);
  75. /** Interpret the next bits in the bit-stream as a signed integer
  76. *
  77. * @param bits Bit-stream to operate on
  78. * @param nbBits Number of bits to interpret
  79. * @return A signed integer represented by the bits read
  80. */
  81. int speex_bits_unpack_signed(SpeexBits *bits, int nbBits);
  82. /** Interpret the next bits in the bit-stream as an unsigned integer
  83. *
  84. * @param bits Bit-stream to operate on
  85. * @param nbBits Number of bits to interpret
  86. * @return An unsigned integer represented by the bits read
  87. */
  88. unsigned int speex_bits_unpack_unsigned(SpeexBits *bits, int nbBits);
  89. /** Returns the number of bytes in the bit-stream, including the last one even if it is not "full"
  90. *
  91. * @param bits Bit-stream to operate on
  92. * @return Number of bytes in the stream
  93. */
  94. int speex_bits_nbytes(SpeexBits *bits);
  95. /** Same as speex_bits_unpack_unsigned, but without modifying the cursor position */
  96. unsigned int speex_bits_peek_unsigned(SpeexBits *bits, int nbBits);
  97. /** Get the value of the next bit in the stream, without modifying the
  98. * "cursor" position
  99. *
  100. * @param bits Bit-stream to operate on
  101. */
  102. int speex_bits_peek(SpeexBits *bits);
  103. /** Advances the position of the "bit cursor" in the stream
  104. *
  105. * @param bits Bit-stream to operate on
  106. * @param n Number of bits to advance
  107. */
  108. void speex_bits_advance(SpeexBits *bits, int n);
  109. /** Returns the number of bits remaining to be read in a stream
  110. *
  111. * @param bits Bit-stream to operate on
  112. */
  113. int speex_bits_remaining(SpeexBits *bits);
  114. /** Insert a terminator so that the data can be sent as a packet while auto-detecting
  115. * the number of frames in each packet
  116. *
  117. * @param bits Bit-stream to operate on
  118. */
  119. void speex_bits_insert_terminator(SpeexBits *bits);
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif
粤ICP备19079148号