⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitbuffer.c

📁 Nokia H.264/AVC Encoder/Decoder Usage Manual
💻 C
字号:
/*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.*/#include <assert.h>#include "debug.h"#include "globals.h"#include "bitbuffer.h"#include "nccglob.h"/* Maximum number of zeros in RBSP */#define STR_MAX_ZEROS             2/* * * bibOpen: * * Parameters: *      bitbuf                Bitbuffer object * * Function: *      Open and initialize bitbuffer writing *      The bits are written to a byte. When the byte is full, the byte is  *      stored in array. * * Returns: *      - * */void bibOpen(bitbuffer_s *bitbuf, int initialSize){  bitbuf->bufSize = initialSize;  bitbuf->bufAddr = (u_int8 *) nccMalloc(bitbuf->bufSize);  if (bitbuf->bufAddr == NULL) {    deb0f(strerr, "Cannot allocate memory for bitbuffer");    nccExit(1);  }  bibInit(bitbuf);}/* * * bibInit: * * Parameters: *      bitbuf                Bitbuffer object * * Function: *      Set the bit buffer in empty state. * * Returns: *      - * */void bibInit(bitbuffer_s *bitbuf){  bitbuf->bytePos = 0;  bitbuf->currentBits = 0;  bitbuf->bitpos = 32;  bitbuf->numZeros = 0;}/* * * bibClose: * * Parameters: *      bitbuf                Bitbuffer object * * Function: *      Close bitbuffer * * Returns: *      - * */void bibClose(bitbuffer_s *bitbuf){  // release the buffer  nccFree(bitbuf->bufAddr);  bitbuf->bufAddr = 0;}/* * * bibPutByte: * * Parameters: *      bitbuf                Bitbuffer object *      byte                  Byte to store * * Function: *      Put one byte to the bitbuffer. If buffer is full, allocate  *      bigger buffer. * *      NOTE: does not insert start code emulation prevention bytes * * Returns: *      - */void bibPutByte(bitbuffer_s *bitbuf, int byte){  /* Check if we have enough memory and get more if not */  if (bitbuf->bytePos == bitbuf->bufSize) {    /* Increase buffer size by 20% and round up to nearest kilobyte */    bitbuf->bufSize = (12*(bitbuf->bufSize)/10 + 1023) & ~1023;    bitbuf->bufAddr = nccRealloc(bitbuf->bufAddr, bitbuf->bufSize);    if (bitbuf->bufAddr == NULL) {      deb0f(strerr, "Cannot allocate memory for bitbuffer");      nccExit(1);    }  }  /* Store byte into the bit buffer */  bitbuf->bufAddr[bitbuf->bytePos++] = (u_int8)byte;}/* * * bibPutBits: * * Parameters: *      bitbuf                Bitbuffer object *      bits                  Bits to store *      len                   Number of bits to store * * Function: *      Put number of bits to bitbuffer. Also take care of start code *      emulation prevention. * * Returns: *      Number of bits stored */int bibPutBits(bitbuffer_s *bitbuf, int bits, int len){  /* To avoid the side affect of higher bits in the input byte */  if (len != 32)    bits &= ~((unsigned int)-1 << len);  if (bitbuf->bitpos > len)  {    // make this path fast    bitbuf->bitpos -= len;    bitbuf->currentBits |= bits << bitbuf->bitpos;  }  else  {    int i, remLen;    int currentByte;    // fill the remaing bits in the buffer    remLen = len - bitbuf->bitpos;    bitbuf->currentBits |= bits >> remLen;    // send 4 bytes    for (i = 0; i < 4; i ++)    {      currentByte = bitbuf->currentBits >> 24;      bitbuf->currentBits <<= 8;      if (bitbuf->numZeros == STR_MAX_ZEROS  && (currentByte & 0xfc) == 0)      {        bibPutByte(bitbuf, 0x03);        bitbuf->numZeros = 0;      }      if (currentByte == 0)        bitbuf->numZeros ++;      else        bitbuf->numZeros = 0;      bibPutByte(bitbuf, currentByte);    }    // put the remaining bits in the buffer    bitbuf->bitpos = 32 - remLen;    bitbuf->currentBits = 0;    if (remLen)      bitbuf->currentBits = bits << bitbuf->bitpos;  }  return len;}/* * bibByteAlign: * * Parameters: *      bitbuf                Bitbuffer object * * Function: *      Align bitbuffer writing at byte boundary. * * Returns: *      The number of skipped bits */int bibByteAlign(bitbuffer_s *bitbuf){  int bitsSkipped;  while (bitbuf->bitpos < 32)  {    bibPutByte(bitbuf, bitbuf->currentBits >> 24);    bitbuf->currentBits <<= 8;    bitbuf->bitpos += 8;  }  bitsSkipped = bitbuf->bitpos - 32;  bitbuf->bitpos = 32;  // set numZeros to 0, because this function is called at the end of NAL  bitbuf->numZeros = 0;  return bitsSkipped;}/* * bibIsByteAlign: * * Parameters: *      bitbuf                Bitbuffer object * * Function: *      Check if the bitbuf is byte aligned. * * Returns: *      1: if byte aligned *      0: otherwise */int bibIsByteAlign(bitbuffer_s *bitbuf){  if (bitbuf->bitpos % 8 == 0)    return 1;  else    return 0;}/* * * bibGetNumBits: * * Parameters: *      bitbuf                Bitbuffer object * * Function: *      Get bit buffer position in bits * * Returns: *      bit buffer position. * */int bibGetNumBits(bitbuffer_s *bitbuf){  return (bitbuf->bytePos * 8 + 32 - bitbuf->bitpos);}/* * * bibTrailingBits: * * Parameters: *      bitbuf                Bitbuffer object * * Function: *      Put bitbuffer trailing bits * * Returns: *      Number of bits stored * */int bibTrailingBits(bitbuffer_s *bitbuf){  int bits = 0;  bits+=bibPutBits(bitbuf, 1, 1);         // rbsp_stop_one_bit    bits+=bibByteAlign(bitbuf);  return bits;}/* * * bibGetBuffer: * * Parameters: *      bitbuf                Bitbuffer object *      bufferPtr             Return pointer for bit buffer * * Function: *      Get bit buffer pointer and size * * Returns: *      Number of bytes in bit buffer * */int bibGetBuffer(bitbuffer_s *bitbuf, void **bufferPtr){  *bufferPtr = bitbuf->bufAddr;  return bitbuf->bytePos;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -