ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
TMBOY
Date:
Tue Jul 18 16:54:45 2017 +0800
Revision:
47:9e361da97763
?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TMBOY 47:9e361da97763 1 /* Copyright (C) 2002 Jean-Marc Valin */
TMBOY 47:9e361da97763 2 /**
TMBOY 47:9e361da97763 3 @file speex_bits.h
TMBOY 47:9e361da97763 4 @brief Handles bit packing/unpacking
TMBOY 47:9e361da97763 5 */
TMBOY 47:9e361da97763 6 /*
TMBOY 47:9e361da97763 7 Redistribution and use in source and binary forms, with or without
TMBOY 47:9e361da97763 8 modification, are permitted provided that the following conditions
TMBOY 47:9e361da97763 9 are met:
TMBOY 47:9e361da97763 10
TMBOY 47:9e361da97763 11 - Redistributions of source code must retain the above copyright
TMBOY 47:9e361da97763 12 notice, this list of conditions and the following disclaimer.
TMBOY 47:9e361da97763 13
TMBOY 47:9e361da97763 14 - Redistributions in binary form must reproduce the above copyright
TMBOY 47:9e361da97763 15 notice, this list of conditions and the following disclaimer in the
TMBOY 47:9e361da97763 16 documentation and/or other materials provided with the distribution.
TMBOY 47:9e361da97763 17
TMBOY 47:9e361da97763 18 - Neither the name of the Xiph.org Foundation nor the names of its
TMBOY 47:9e361da97763 19 contributors may be used to endorse or promote products derived from
TMBOY 47:9e361da97763 20 this software without specific prior written permission.
TMBOY 47:9e361da97763 21
TMBOY 47:9e361da97763 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
TMBOY 47:9e361da97763 23 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
TMBOY 47:9e361da97763 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
TMBOY 47:9e361da97763 25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
TMBOY 47:9e361da97763 26 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
TMBOY 47:9e361da97763 27 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
TMBOY 47:9e361da97763 28 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
TMBOY 47:9e361da97763 29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
TMBOY 47:9e361da97763 30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
TMBOY 47:9e361da97763 31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
TMBOY 47:9e361da97763 32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
TMBOY 47:9e361da97763 33
TMBOY 47:9e361da97763 34 */
TMBOY 47:9e361da97763 35
TMBOY 47:9e361da97763 36 #ifndef BITS_H
TMBOY 47:9e361da97763 37 #define BITS_H
TMBOY 47:9e361da97763 38 /** @defgroup SpeexBits SpeexBits: Bit-stream manipulations
TMBOY 47:9e361da97763 39 * This is the structure that holds the bit-stream when encoding or decoding
TMBOY 47:9e361da97763 40 * with Speex. It allows some manipulations as well.
TMBOY 47:9e361da97763 41 * @{
TMBOY 47:9e361da97763 42 */
TMBOY 47:9e361da97763 43
TMBOY 47:9e361da97763 44 #ifdef __cplusplus
TMBOY 47:9e361da97763 45 extern "C" {
TMBOY 47:9e361da97763 46 #endif
TMBOY 47:9e361da97763 47
TMBOY 47:9e361da97763 48 /** Bit-packing data structure representing (part of) a bit-stream. */
TMBOY 47:9e361da97763 49 typedef struct SpeexBits {
TMBOY 47:9e361da97763 50 char *chars; /**< "raw" data */
TMBOY 47:9e361da97763 51 int nbBits; /**< Total number of bits stored in the stream*/
TMBOY 47:9e361da97763 52 int charPtr; /**< Position of the byte "cursor" */
TMBOY 47:9e361da97763 53 int bitPtr; /**< Position of the bit "cursor" within the current char */
TMBOY 47:9e361da97763 54 int owner; /**< Does the struct "own" the "raw" buffer (member "chars") */
TMBOY 47:9e361da97763 55 int overflow;/**< Set to one if we try to read past the valid data */
TMBOY 47:9e361da97763 56 int buf_size;/**< Allocated size for buffer */
TMBOY 47:9e361da97763 57 int reserved1; /**< Reserved for future use */
TMBOY 47:9e361da97763 58 void *reserved2; /**< Reserved for future use */
TMBOY 47:9e361da97763 59 } SpeexBits;
TMBOY 47:9e361da97763 60
TMBOY 47:9e361da97763 61 /** Initializes and allocates resources for a SpeexBits struct */
TMBOY 47:9e361da97763 62 void speex_bits_init(SpeexBits *bits);
TMBOY 47:9e361da97763 63
TMBOY 47:9e361da97763 64 /** Initializes SpeexBits struct using a pre-allocated buffer*/
TMBOY 47:9e361da97763 65 void speex_bits_init_buffer(SpeexBits *bits, void *buff, int buf_size);
TMBOY 47:9e361da97763 66
TMBOY 47:9e361da97763 67 /** Sets the bits in a SpeexBits struct to use data from an existing buffer (for decoding without copying data) */
TMBOY 47:9e361da97763 68 void speex_bits_set_bit_buffer(SpeexBits *bits, void *buff, int buf_size);
TMBOY 47:9e361da97763 69
TMBOY 47:9e361da97763 70 /** 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.*/
TMBOY 47:9e361da97763 71 void speex_bits_destroy(SpeexBits *bits);
TMBOY 47:9e361da97763 72
TMBOY 47:9e361da97763 73 /** Resets bits to initial value (just after initialization, erasing content)*/
TMBOY 47:9e361da97763 74 void speex_bits_reset(SpeexBits *bits);
TMBOY 47:9e361da97763 75
TMBOY 47:9e361da97763 76 /** Rewind the bit-stream to the beginning (ready for read) without erasing the content */
TMBOY 47:9e361da97763 77 void speex_bits_rewind(SpeexBits *bits);
TMBOY 47:9e361da97763 78
TMBOY 47:9e361da97763 79 /** Initializes the bit-stream from the data in an area of memory */
TMBOY 47:9e361da97763 80 void speex_bits_read_from(SpeexBits *bits, char *bytes, int len);
TMBOY 47:9e361da97763 81
TMBOY 47:9e361da97763 82 /** Append bytes to the bit-stream
TMBOY 47:9e361da97763 83 *
TMBOY 47:9e361da97763 84 * @param bits Bit-stream to operate on
TMBOY 47:9e361da97763 85 * @param bytes pointer to the bytes what will be appended
TMBOY 47:9e361da97763 86 * @param len Number of bytes of append
TMBOY 47:9e361da97763 87 */
TMBOY 47:9e361da97763 88 void speex_bits_read_whole_bytes(SpeexBits *bits, char *bytes, int len);
TMBOY 47:9e361da97763 89
TMBOY 47:9e361da97763 90 /** Write the content of a bit-stream to an area of memory
TMBOY 47:9e361da97763 91 *
TMBOY 47:9e361da97763 92 * @param bits Bit-stream to operate on
TMBOY 47:9e361da97763 93 * @param bytes Memory location where to write the bits
TMBOY 47:9e361da97763 94 * @param max_len Maximum number of bytes to write (i.e. size of the "bytes" buffer)
TMBOY 47:9e361da97763 95 * @return Number of bytes written to the "bytes" buffer
TMBOY 47:9e361da97763 96 */
TMBOY 47:9e361da97763 97 int speex_bits_write(SpeexBits *bits, char *bytes, int max_len);
TMBOY 47:9e361da97763 98
TMBOY 47:9e361da97763 99 /** Like speex_bits_write, but writes only the complete bytes in the stream. Also removes the written bytes from the stream */
TMBOY 47:9e361da97763 100 int speex_bits_write_whole_bytes(SpeexBits *bits, char *bytes, int max_len);
TMBOY 47:9e361da97763 101
TMBOY 47:9e361da97763 102 /** Append bits to the bit-stream
TMBOY 47:9e361da97763 103 * @param bits Bit-stream to operate on
TMBOY 47:9e361da97763 104 * @param data Value to append as integer
TMBOY 47:9e361da97763 105 * @param nbBits number of bits to consider in "data"
TMBOY 47:9e361da97763 106 */
TMBOY 47:9e361da97763 107 void speex_bits_pack(SpeexBits *bits, int data, int nbBits);
TMBOY 47:9e361da97763 108
TMBOY 47:9e361da97763 109 /** Interpret the next bits in the bit-stream as a signed integer
TMBOY 47:9e361da97763 110 *
TMBOY 47:9e361da97763 111 * @param bits Bit-stream to operate on
TMBOY 47:9e361da97763 112 * @param nbBits Number of bits to interpret
TMBOY 47:9e361da97763 113 * @return A signed integer represented by the bits read
TMBOY 47:9e361da97763 114 */
TMBOY 47:9e361da97763 115 int speex_bits_unpack_signed(SpeexBits *bits, int nbBits);
TMBOY 47:9e361da97763 116
TMBOY 47:9e361da97763 117 /** Interpret the next bits in the bit-stream as an unsigned integer
TMBOY 47:9e361da97763 118 *
TMBOY 47:9e361da97763 119 * @param bits Bit-stream to operate on
TMBOY 47:9e361da97763 120 * @param nbBits Number of bits to interpret
TMBOY 47:9e361da97763 121 * @return An unsigned integer represented by the bits read
TMBOY 47:9e361da97763 122 */
TMBOY 47:9e361da97763 123 unsigned int speex_bits_unpack_unsigned(SpeexBits *bits, int nbBits);
TMBOY 47:9e361da97763 124
TMBOY 47:9e361da97763 125 /** Returns the number of bytes in the bit-stream, including the last one even if it is not "full"
TMBOY 47:9e361da97763 126 *
TMBOY 47:9e361da97763 127 * @param bits Bit-stream to operate on
TMBOY 47:9e361da97763 128 * @return Number of bytes in the stream
TMBOY 47:9e361da97763 129 */
TMBOY 47:9e361da97763 130 int speex_bits_nbytes(SpeexBits *bits);
TMBOY 47:9e361da97763 131
TMBOY 47:9e361da97763 132 /** Same as speex_bits_unpack_unsigned, but without modifying the cursor position
TMBOY 47:9e361da97763 133 *
TMBOY 47:9e361da97763 134 * @param bits Bit-stream to operate on
TMBOY 47:9e361da97763 135 * @param nbBits Number of bits to look for
TMBOY 47:9e361da97763 136 * @return Value of the bits peeked, interpreted as unsigned
TMBOY 47:9e361da97763 137 */
TMBOY 47:9e361da97763 138 unsigned int speex_bits_peek_unsigned(SpeexBits *bits, int nbBits);
TMBOY 47:9e361da97763 139
TMBOY 47:9e361da97763 140 /** Get the value of the next bit in the stream, without modifying the
TMBOY 47:9e361da97763 141 * "cursor" position
TMBOY 47:9e361da97763 142 *
TMBOY 47:9e361da97763 143 * @param bits Bit-stream to operate on
TMBOY 47:9e361da97763 144 * @return Value of the bit peeked (one bit only)
TMBOY 47:9e361da97763 145 */
TMBOY 47:9e361da97763 146 int speex_bits_peek(SpeexBits *bits);
TMBOY 47:9e361da97763 147
TMBOY 47:9e361da97763 148 /** Advances the position of the "bit cursor" in the stream
TMBOY 47:9e361da97763 149 *
TMBOY 47:9e361da97763 150 * @param bits Bit-stream to operate on
TMBOY 47:9e361da97763 151 * @param n Number of bits to advance
TMBOY 47:9e361da97763 152 */
TMBOY 47:9e361da97763 153 void speex_bits_advance(SpeexBits *bits, int n);
TMBOY 47:9e361da97763 154
TMBOY 47:9e361da97763 155 /** Returns the number of bits remaining to be read in a stream
TMBOY 47:9e361da97763 156 *
TMBOY 47:9e361da97763 157 * @param bits Bit-stream to operate on
TMBOY 47:9e361da97763 158 * @return Number of bits that can still be read from the stream
TMBOY 47:9e361da97763 159 */
TMBOY 47:9e361da97763 160 int speex_bits_remaining(SpeexBits *bits);
TMBOY 47:9e361da97763 161
TMBOY 47:9e361da97763 162 /** Insert a terminator so that the data can be sent as a packet while auto-detecting
TMBOY 47:9e361da97763 163 * the number of frames in each packet
TMBOY 47:9e361da97763 164 *
TMBOY 47:9e361da97763 165 * @param bits Bit-stream to operate on
TMBOY 47:9e361da97763 166 */
TMBOY 47:9e361da97763 167 void speex_bits_insert_terminator(SpeexBits *bits);
TMBOY 47:9e361da97763 168
TMBOY 47:9e361da97763 169 #ifdef __cplusplus
TMBOY 47:9e361da97763 170 }
TMBOY 47:9e361da97763 171 #endif
TMBOY 47:9e361da97763 172
TMBOY 47:9e361da97763 173 /* @} */
TMBOY 47:9e361da97763 174 #endif