📄 bitbuffer.h
字号:
/*COPYRIGHT, LICENSE AND WARRANTY INFORMATIONThis software module has been originally developed by Nokia Corporation. Provided that a person, entity or a company willing to use the Software (hereinafter Licensee) comply with all the terms and conditions of this Statement and subject to the limitations set forth in this Statement Nokia grants to such Licensee a non-exclusive, sub-licensable, worldwide, limited license under copyrights owned by Nokia to use the Software for the sole purpose of creating, manufacturing, selling, marketing, or distributing (including the right to make modifications to the Software) a fully compliant decoder implementation (hereinafter "Decoder") of ITU-T Recommendation H.264 / ISO/IEC International Standard 14496-10 and an encoder implementation producing output that is decodable with the Decoder.Nokia retains the ownership of copyrights to the Software. There is no patent nor other intellectual property right of Nokia licensed under this Statement (except the copyright license above). Licensee hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if patent licenses are required, it is their responsibility to acquire the license before utilizing the Software.The license by Nokia is subject to that the Licensee grants to Nokia the non-exclusive, worldwide, royalty-free, perpetual and irrevocable covenant that the Licensee(s) shall not bring a suit before any court or administrative agency or otherwise assert a claim for infringement under the Licensee intellectual property rights that, but for a license, would be infringed by the Software against (a) Nokia or Nokia's Affiliate; or (b) other recipient of a license and covenant not to sue with respect to the Software from Nokia; or (c) contractor, customer or distributor of a party listed above in a or b, which suit or claim is related to the Software or use thereof.The Licensee(s) further agrees to grant a reciprocal license to Nokia (as granted by Nokia to the Licensee(s) on the modifications made by Licensee(s) to the Software. THE SOFTWARE IS PROVIDED "AS IS" AND THE ORIGINAL DEVELOPER DISCLAIMS ANY AND ALL WARRANTIES WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. THOSE INTENDING TO USE THE SOFTWARE ARE EXPRESSLY ADVISED THAT ITS USE MAY INFRINGE EXISTING PATENTS AND BE SUBJECT TO ROYALTY PAYMENTS TO PATENT OWNERS. ANYONE USING THE SOFTWARE ON THE BASIS OF THIS LICENSE AGREES TO OBTAIN THE NECESSARY PERMISSIONS FROM ANY AND ALL APPLICABLE PATENT OWNERS FOR SUCH USE.IN NO EVENT SHALL THE ORIGINAL DEVELOPER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.This copyright, license and warranty information notice must be retained in all copies and derivative works of the Software or substantial portions thereof.*/#ifndef _BITBUFFER_H_#define _BITBUFFER_H_#include "globals.h"#define BIB_ERR_BIT_ERROR -4#define BIB_INCORRECT_TRAILING_BIT -3#define BIB_ERR_NO_BITS -2#define BIB_ERROR -1#define BIB_OK 0typedef struct _bitbuffer_s { u_int8 *data; /* point to the bit buffer. The physical buffer is allocated by the main module */ int dataLen; int bytePos; int bitpos; int32 currentBits; int errorStatus;} bitbuffer_s;#define bibRaiseError(bitbuf, error) ((bitbuf)->errorStatus = (error))#define bibGetStatus(bitbuf) (bitbuf)->errorStatusbitbuffer_s *bibOpen();int bibInit(bitbuffer_s *bitbuf, u_int8 *streamBytes, int length);void bibClose(bitbuffer_s *bitbuf);int bibGetBitFunc(bitbuffer_s *bitbuf);int32 bibGetBitsFunc(bitbuffer_s *bitbuf, int n);int32 bibShowBitsFunc(bitbuffer_s *bitbuf, int n);int bibSkipBitsFunc(bitbuffer_s *bitbuf, int n);int bibGetByte(bitbuffer_s *bitbuf, int *byteRet);int bibByteAlign(bitbuffer_s *bitbuf);int bibSkipTrailingBits(bitbuffer_s *bitbuf);int bibMoreRbspData(bitbuffer_s *bitbuf);int32 bibGetNumRemainingBits(bitbuffer_s *bitbuf);/* * * bibGetBit * * Parameters: * bitbuf Bitbuffer object * bit Return pointer for bit * * Function: * Get next bit from bitbuffer. * * Returns: * - * */#define bibGetBit(bitbuf, bit) \ if ((bitbuf)->bitpos <= 0) { \ if ((bitbuf)->bytePos < (bitbuf)->dataLen) { \ (bitbuf)->currentBits = (bitbuf)->data[(bitbuf)->bytePos++]; \ (bitbuf)->bitpos = 7; \ *(bit) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & 1; \ } \ else { \ (bitbuf)->errorStatus = BIB_ERR_NO_BITS; \ *(bit) = 0; \ } \ } \ else { \ (bitbuf)->bitpos--; \ *(bit) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & 1; \ }/* * * bibGetBits * * Parameters: * bitbuf Bitbuffer object * n Number of bits requested * bits Return pointer for bits * * Function: * Get next n bits from bitbuffer. If bitbuffer is low on bits, * call bibGetBitsFunc to get more. * * NOTE: maximum of 24 bits can be fetched * * Returns: * - * */#define bibGetBits(bitbuf, n, bits) \ if ((n) > (bitbuf)->bitpos) { \ if ((bitbuf)->bytePos+2 >= (bitbuf)->dataLen) \ *(bits) = bibGetBitsFunc(bitbuf, n); \ else { \ do { \ (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \ (bitbuf)->bitpos += 8; \ } while ((n) > (bitbuf)->bitpos); \ (bitbuf)->bitpos -= (n); \ *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \ } \ } \ else { \ (bitbuf)->bitpos -= (n); \ *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \ }/* * * bibGetMax16bits * * Parameters: * bitbuf Bitbuffer object * n Number of bits requested * bits Return pointer for bits * * Function: * Get next n bits from bitbuffer. If bitbuffer is low on bits, * call bibGetBitsFunc to get more. * * NOTE: maximum of 16 bits can be fetched * * Returns: * - * */#define bibGetMax16bits(bitbuf, n, bits) \ if ((n) > (bitbuf)->bitpos) { \ if ((bitbuf)->bytePos+1 >= (bitbuf)->dataLen) \ *(bits) = bibGetBitsFunc(bitbuf, n); \ else { \ (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \ (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \ (bitbuf)->bitpos += 16; \ (bitbuf)->bitpos -= (n); \ *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \ } \ } \ else { \ (bitbuf)->bitpos -= (n); \ *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \ }/* * * bibGetMax8bits * * Parameters: * bitbuf Bitbuffer object * n Number of bits requested * bits Return pointer for bits * * Function: * Get next n bits from bitbuffer. If bitbuffer is low on bits, * call bibGetBitsFunc to get more. * * NOTE: maximum of 8 bits can be fetched * * Returns: * - * */#define bibGetMax8bits(bitbuf, n, bits) \ if ((n) > (bitbuf)->bitpos) { \ if ((bitbuf)->bytePos >= (bitbuf)->dataLen) \ *(bits) = bibGetBitsFunc(bitbuf, n); \ else { \ (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \ (bitbuf)->bitpos += 8; \ (bitbuf)->bitpos -= (n); \ *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \ } \ } \ else { \ (bitbuf)->bitpos -= (n); \ *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \ }/* * * bibShowBits * * Parameters: * bitbuf Bitbuffer object * n Number of bits requested * bits Return pointer for bits * * Function: * Get next n bits from bitbuffer without advancing bitbuffer pointer. * If bitbuffer is low on bits, call bibShowBitsFunc to get more. * * NOTE: maximum of 24 bits can be fetched * * Returns: * - * */#define bibShowBits(bitbuf, n, bits) \ if ((n) > (bitbuf)->bitpos) { \ if ((bitbuf)->bytePos+2 >= (bitbuf)->dataLen) \ *(bits) = bibShowBitsFunc(bitbuf, n); \ else { \ do { \ (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \ (bitbuf)->bitpos += 8; \ } while ((n) > (bitbuf)->bitpos); \ *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n)); \ } \ } \ else \ *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n));/* * * bibShowMax16bits * * Parameters: * bitbuf Bitbuffer object * n Number of bits requested * bits Return pointer for bits * * Function: * Get next n bits from bitbuffer without advancing bitbuffer pointer. * If bitbuffer is low on bits, call bibShowBitsFunc to get more. * * NOTE: maximum of 16 bits can be fetched * * Returns: * - * */#define bibShowMax16bits(bitbuf, n, bits) \ if ((n) > (bitbuf)->bitpos) { \ if ((bitbuf)->bytePos+1 >= (bitbuf)->dataLen) \ *(bits) = bibShowBitsFunc(bitbuf, n); \ else { \ (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \ (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \ (bitbuf)->bitpos += 16; \ *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n)); \ } \ } \ else \ *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n));/* * * bibShowMax8bits * * Parameters: * bitbuf Bitbuffer object * n Number of bits requested * bits Return pointer for bits * * Function: * Get next n bits from bitbuffer without advancing bitbuffer pointer. * If bitbuffer is low on bits, call bibShowBitsFunc to get more. * * NOTE: maximum of 8 bits can be fetched * * Returns: * - * */#define bibShowMax8bits(bitbuf, n, bits) \ if ((n) > (bitbuf)->bitpos) { \ if ((bitbuf)->bytePos >= (bitbuf)->dataLen) \ *(bits) = bibShowBitsFunc(bitbuf, n); \ else { \ (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \ (bitbuf)->bitpos += 8; \ *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n)); \ } \ } \ else \ *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n));/* * * bibSkipBits * * Parameters: * bitbuf Bitbuffer object * n Number of bits to be skipped * * Function: * Called after calling bibShowBits to skip the number of bits that * were actually needed. If bibShowBits fetched more bits than there were * bits in bitbuf->currentBits, calls bibSkipBitsFunc to clean up. * * Returns: * - * */#define bibSkipBits(bitbuf, n) \ (bitbuf)->bitpos -= (n); \ if ((bitbuf)->bitpos < 0) { \ (bitbuf)->bitpos += (n); \ bibSkipBitsFunc(bitbuf, n); \ }#endif /* #ifndef _BITBUFFER_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -