www.pudn.com > av3dec_20050318.zip > sam_bitbuffer.c
/* *********************************************************************** * COPYRIGHT AND WARRANTY INFORMATION * * Copyright 2004, Audio Video Coding Standard, Part III * * This software module was originally developed by * * JungHoe Kim (kjh94@samsung.com), Samsung AIT * * edited by * * Lei Miao (win.miaolei@samsung.com), Samsung AIT * * DISCLAIMER OF WARRANTY * * These software programs are available to the users without any * license fee or royalty on an "as is" basis. The AVS disclaims * any and all warranties, whether express, implied, or statutory, * including any implied warranties of merchantability or of fitness * for a particular purpose. In no event shall the contributors or * the AVS be liable for any incidental, punitive, or consequential * damages of any kind whatsoever arising from the use of this program. * * This disclaimer of warranty extends to the user of this program * and user's customers, employees, agents, transferees, successors, * and assigns. * * The AVS does not represent or warrant that the program furnished * hereunder are free of infringement of any third-party patents. * Commercial implementations of AVS, including shareware, may be * subject to royalty fees to patent holders. Information regarding * the AVS patent policy is available from the AVS Web site at * http://www.avs.org.cn * * THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY. ************************************************************************ */ #include#include #include #include #include "sam_cbc_dec.h" #define MINIMUM 3 #define MAX_LENGTH 32 #define ALIGNING 8 static unsigned char bs_buf[4096]; /* bit stream buffer */ static int bs_buf_size; /* size of buffer (in number of bytes) */ static long bs_totbit; /* bit counter of bit stream */ static int bs_buf_byte_idx; /* pointer to top byte in buffer */ static int bs_buf_bit_idx; /* pointer to top bit of top byte in buffer */ static int bs_eob; /* end of buffer index */ static int bs_eobs; /* end of bit stream flag */ /* open the device to read the bit stream from it */ void init_layer_buf() { int i; bs_buf_byte_idx=4095; bs_buf_bit_idx=0; bs_totbit=0; bs_eob = FALSE; bs_eobs = FALSE; bs_buf_size = 4096; for(i = 0; i < bs_buf_size; i++) bs_buf[i] = 0; } void setRBitBufPos(int pos) { bs_buf_byte_idx = pos / 8; bs_buf_bit_idx = 8 - (pos % 8); } static int putmask[9]={0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff}; static int mask[8]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80}; /*read N bit from the bit stream */ unsigned int getbitsfrombuf(int N) { unsigned long val=0; register int i; register int j = N; register int k, tmp; if(N <= 0) return 0; if (N > MAX_LENGTH) printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); bs_totbit += N; while (j > 0) { if (!bs_buf_bit_idx) { bs_buf_bit_idx = 8; bs_buf_byte_idx++; if ((bs_buf_byte_idx >= (bs_buf_size - MINIMUM)) || (bs_buf_byte_idx == bs_eob)) { if (bs_eob) { bs_eobs = TRUE; return 0; } else { for (i=bs_buf_byte_idx; i > (bs_buf_bit_idx-k)) << (j-k); bs_buf_bit_idx -= k; j -= k; } return(val); } /*write N bits into the bit stream */ int putbits2buf( unsigned int val, int N) { register int j = N; register int k, tmp; if (N < 1) return 0; if (N > MAX_LENGTH) { printf("Cannot read or write more than %d bits[%d] at a time.\n", MAX_LENGTH, val); return 0; } while (j > 0) { k = (j < bs_buf_bit_idx) ? j : bs_buf_bit_idx; tmp = val >> (j-k); bs_buf[bs_buf_byte_idx] |= (tmp&putmask[k]) << (bs_buf_bit_idx-k); bs_buf_bit_idx -= k; if (!bs_buf_bit_idx) { bs_buf_bit_idx = 8; bs_buf_byte_idx++; if (bs_buf_byte_idx > bs_buf_size-1) { fprintf(stderr, "SAM: bitstream buffer overflow!\n"); } } j -= k; } return N; }