videoeditorengine/avcedit/inc/bitbuffer.h
branchRCL_3
changeset 3 e0b5df5c0969
parent 0 951a5db380a0
child 5 4c409de21d23
equal deleted inserted replaced
0:951a5db380a0 3:e0b5df5c0969
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 * Ixonos Plc
       
    14 *
       
    15 * Description:
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #ifndef _BITBUFFER_H_
       
    21 #define _BITBUFFER_H_
       
    22 
       
    23 
       
    24 #include "globals.h"
       
    25 #include "nrctyp32.h"
       
    26 
       
    27 
       
    28 #define BIB_ERR_BIT_ERROR          -4
       
    29 #define BIB_INCORRECT_TRAILING_BIT -3
       
    30 #define BIB_ERR_NO_BITS            -2
       
    31 #define BIB_ERROR                  -1
       
    32 #define BIB_OK                      0
       
    33 
       
    34 
       
    35 typedef struct _bitbuffer_s 
       
    36 {
       
    37   u_int8 *data;     /* point to the bit buffer. The physical buffer is allocated by the main module */
       
    38   int dataLen;
       
    39   int bytePos;
       
    40   int bitpos;
       
    41   int32 currentBits;
       
    42   int errorStatus;
       
    43 } bitbuffer_s;
       
    44 
       
    45 
       
    46 #define bibRaiseError(bitbuf, error) ((bitbuf)->errorStatus = (error))
       
    47 
       
    48 #define bibGetStatus(bitbuf) (bitbuf)->errorStatus
       
    49 
       
    50 
       
    51 bitbuffer_s *bibOpen();
       
    52 
       
    53 int bibInit(bitbuffer_s *bitbuf, u_int8 *streamBytes, int length);
       
    54 
       
    55 int bibEnd(bitbuffer_s *bitbuf);
       
    56 
       
    57 void bibEndSlice(bitbuffer_s *bitbuf);
       
    58 
       
    59 void bibClose(bitbuffer_s *bitbuf);
       
    60 
       
    61 int bibGetBitFunc(bitbuffer_s *bitbuf);
       
    62 
       
    63 int32 bibGetBitsFunc(bitbuffer_s *bitbuf, int n);
       
    64 
       
    65 int32 bibShowBitsFunc(bitbuffer_s *bitbuf, int n);
       
    66 
       
    67 int bibSkipBitsFunc(bitbuffer_s *bitbuf, int n);
       
    68 
       
    69 int bibGetByte(bitbuffer_s *bitbuf, int *byteRet);
       
    70 
       
    71 int bibByteAlign(bitbuffer_s *bitbuf);
       
    72 
       
    73 int bibSkipTrailingBits(bitbuffer_s *bitbuf);
       
    74 
       
    75 int bibMoreRbspData(bitbuffer_s *bitbuf);
       
    76 
       
    77 int32 bibGetNumRemainingBits(bitbuffer_s *bitbuf);
       
    78 
       
    79 //int bibGetMax16bits(bitbuffer_s *bitbuf, TInt n, u_int32* retValue);
       
    80 void syncBitBufferBitpos(bitbuffer_s *bitbuf);
       
    81 
       
    82 
       
    83 /*
       
    84  *
       
    85  * bibGetBit
       
    86  *
       
    87  * Parameters:
       
    88  *      bitbuf                Bitbuffer object
       
    89  *      bit                   Return pointer for bit
       
    90  *
       
    91  * Function:
       
    92  *      Get next bit from bitbuffer.
       
    93  *
       
    94  * Returns:
       
    95  *      -
       
    96  *
       
    97  */
       
    98 #define bibGetBit(bitbuf, bit) \
       
    99   if ((bitbuf)->bitpos <= 0) { \
       
   100     if ((bitbuf)->bytePos < (bitbuf)->dataLen) { \
       
   101       (bitbuf)->currentBits = (bitbuf)->data[(bitbuf)->bytePos++]; \
       
   102       (bitbuf)->bitpos = 7; \
       
   103       *(bit) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & 1; \
       
   104     } \
       
   105     else { \
       
   106       (bitbuf)->errorStatus = BIB_ERR_NO_BITS; \
       
   107       *(bit) = 0; \
       
   108     } \
       
   109   } \
       
   110   else { \
       
   111     (bitbuf)->bitpos--; \
       
   112     *(bit) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & 1; \
       
   113   }
       
   114 
       
   115 
       
   116 /*
       
   117  *
       
   118  * bibGetBits
       
   119  *
       
   120  * Parameters:
       
   121  *      bitbuf                Bitbuffer object
       
   122  *      n                     Number of bits requested
       
   123  *      bits                  Return pointer for bits
       
   124  *
       
   125  * Function:
       
   126  *      Get next n bits from bitbuffer. If bitbuffer is low on bits,
       
   127  *      call bibGetBitsFunc to get more.
       
   128  *
       
   129  *      NOTE: maximum of 24 bits can be fetched
       
   130  *
       
   131  * Returns:
       
   132  *      -
       
   133  *
       
   134  */
       
   135 #define bibGetBits(bitbuf, n, bits) \
       
   136   if ((n) > (bitbuf)->bitpos) { \
       
   137     if ((bitbuf)->bytePos+2 >= (bitbuf)->dataLen) \
       
   138       *(bits) = bibGetBitsFunc(bitbuf, n); \
       
   139     else { \
       
   140       do { \
       
   141         (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \
       
   142         (bitbuf)->bitpos += 8; \
       
   143       } while ((n) > (bitbuf)->bitpos); \
       
   144       (bitbuf)->bitpos -= (n); \
       
   145       *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \
       
   146     } \
       
   147   } \
       
   148   else { \
       
   149     (bitbuf)->bitpos -= (n); \
       
   150     *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \
       
   151   }
       
   152 
       
   153 
       
   154 /*
       
   155  *
       
   156  * bibGetMax16bits
       
   157  *
       
   158  * Parameters:
       
   159  *      bitbuf                Bitbuffer object
       
   160  *      n                     Number of bits requested
       
   161  *      bits                  Return pointer for bits
       
   162  *
       
   163  * Function:
       
   164  *      Get next n bits from bitbuffer. If bitbuffer is low on bits,
       
   165  *      call bibGetBitsFunc to get more.
       
   166  *
       
   167  *      NOTE: maximum of 16 bits can be fetched
       
   168  *
       
   169  * Returns:
       
   170  *      -
       
   171  *
       
   172  */
       
   173 #define bibGetMax16bits(bitbuf, n, bits) \
       
   174   if ((n) > (bitbuf)->bitpos) { \
       
   175     if ((bitbuf)->bytePos+1 >= (bitbuf)->dataLen) \
       
   176       *(bits) = bibGetBitsFunc(bitbuf, n); \
       
   177     else { \
       
   178       (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \
       
   179       (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \
       
   180       (bitbuf)->bitpos += 16; \
       
   181       (bitbuf)->bitpos -= (n); \
       
   182       *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \
       
   183     } \
       
   184   } \
       
   185   else { \
       
   186     (bitbuf)->bitpos -= (n); \
       
   187     *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \
       
   188   }
       
   189 
       
   190 
       
   191 /*
       
   192  *
       
   193  * bibGetMax8bits
       
   194  *
       
   195  * Parameters:
       
   196  *      bitbuf                Bitbuffer object
       
   197  *      n                     Number of bits requested
       
   198  *      bits                  Return pointer for bits
       
   199  *
       
   200  * Function:
       
   201  *      Get next n bits from bitbuffer. If bitbuffer is low on bits,
       
   202  *      call bibGetBitsFunc to get more.
       
   203  *
       
   204  *      NOTE: maximum of 8 bits can be fetched
       
   205  *
       
   206  * Returns:
       
   207  *      -
       
   208  *
       
   209  */
       
   210 #define bibGetMax8bits(bitbuf, n, bits) \
       
   211   if ((n) > (bitbuf)->bitpos) { \
       
   212     if ((bitbuf)->bytePos >= (bitbuf)->dataLen) \
       
   213       *(bits) = bibGetBitsFunc(bitbuf, n); \
       
   214     else { \
       
   215       (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \
       
   216       (bitbuf)->bitpos += 8; \
       
   217       (bitbuf)->bitpos -= (n); \
       
   218       *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \
       
   219     } \
       
   220   } \
       
   221   else { \
       
   222     (bitbuf)->bitpos -= (n); \
       
   223     *(bits) = ((bitbuf)->currentBits >> (bitbuf)->bitpos) & ~(((u_int32)-1)<<(n)); \
       
   224   }
       
   225 
       
   226 
       
   227 /*
       
   228  *
       
   229  * bibShowBits
       
   230  *
       
   231  * Parameters:
       
   232  *      bitbuf                Bitbuffer object
       
   233  *      n                     Number of bits requested
       
   234  *      bits                  Return pointer for bits
       
   235  *
       
   236  * Function:
       
   237  *      Get next n bits from bitbuffer without advancing bitbuffer pointer.
       
   238  *      If bitbuffer is low on bits, call bibShowBitsFunc to get more.
       
   239  *
       
   240  *      NOTE: maximum of 24 bits can be fetched
       
   241  *
       
   242  * Returns:
       
   243  *      -
       
   244  *
       
   245  */
       
   246 #define bibShowBits(bitbuf, n, bits) \
       
   247   if ((n) > (bitbuf)->bitpos) { \
       
   248     if ((bitbuf)->bytePos+2 >= (bitbuf)->dataLen) \
       
   249       *(bits) = bibShowBitsFunc(bitbuf, n); \
       
   250     else { \
       
   251       do { \
       
   252         (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \
       
   253         (bitbuf)->bitpos += 8; \
       
   254       } while ((n) > (bitbuf)->bitpos); \
       
   255       *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n)); \
       
   256     } \
       
   257   } \
       
   258   else \
       
   259     *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n));
       
   260 
       
   261 
       
   262 /*
       
   263  *
       
   264  * bibShowMax16bits
       
   265  *
       
   266  * Parameters:
       
   267  *      bitbuf                Bitbuffer object
       
   268  *      n                     Number of bits requested
       
   269  *      bits                  Return pointer for bits
       
   270  *
       
   271  * Function:
       
   272  *      Get next n bits from bitbuffer without advancing bitbuffer pointer.
       
   273  *      If bitbuffer is low on bits, call bibShowBitsFunc to get more.
       
   274  *
       
   275  *      NOTE: maximum of 16 bits can be fetched
       
   276  *
       
   277  * Returns:
       
   278  *      -
       
   279  *
       
   280  */
       
   281 #define bibShowMax16bits(bitbuf, n, bits) \
       
   282   if ((n) > (bitbuf)->bitpos) { \
       
   283     if ((bitbuf)->bytePos+1 >= (bitbuf)->dataLen) \
       
   284       *(bits) = bibShowBitsFunc(bitbuf, n); \
       
   285     else { \
       
   286       (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \
       
   287       (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \
       
   288       (bitbuf)->bitpos += 16; \
       
   289       *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n)); \
       
   290     } \
       
   291   } \
       
   292   else \
       
   293     *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n));
       
   294 
       
   295 
       
   296 /*
       
   297  *
       
   298  * bibShowMax8bits
       
   299  *
       
   300  * Parameters:
       
   301  *      bitbuf                Bitbuffer object
       
   302  *      n                     Number of bits requested
       
   303  *      bits                  Return pointer for bits
       
   304  *
       
   305  * Function:
       
   306  *      Get next n bits from bitbuffer without advancing bitbuffer pointer.
       
   307  *      If bitbuffer is low on bits, call bibShowBitsFunc to get more.
       
   308  *
       
   309  *      NOTE: maximum of 8 bits can be fetched
       
   310  *
       
   311  * Returns:
       
   312  *      -
       
   313  *
       
   314  */
       
   315 #define bibShowMax8bits(bitbuf, n, bits) \
       
   316   if ((n) > (bitbuf)->bitpos) { \
       
   317     if ((bitbuf)->bytePos >= (bitbuf)->dataLen) \
       
   318       *(bits) = bibShowBitsFunc(bitbuf, n); \
       
   319     else { \
       
   320       (bitbuf)->currentBits = ((bitbuf)->currentBits << 8) | (bitbuf)->data[(bitbuf)->bytePos++]; \
       
   321       (bitbuf)->bitpos += 8; \
       
   322       *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n)); \
       
   323     } \
       
   324   } \
       
   325   else \
       
   326     *(bits) = ((bitbuf)->currentBits >> ((bitbuf)->bitpos-(n))) & ~(((u_int32)-1)<<(n));
       
   327 
       
   328 
       
   329 /*
       
   330  *
       
   331  * bibSkipBits
       
   332  *
       
   333  * Parameters:
       
   334  *      bitbuf                Bitbuffer object
       
   335  *      n                     Number of bits to be skipped
       
   336  *
       
   337  * Function:
       
   338  *      Called after calling bibShowBits to skip the number of bits that
       
   339  *      were actually needed. If bibShowBits fetched more bits than there were
       
   340  *      bits in bitbuf->currentBits, calls bibSkipBitsFunc to clean up.
       
   341  *
       
   342  * Returns:
       
   343  *      -
       
   344  *
       
   345  */
       
   346 #define bibSkipBits(bitbuf, n) \
       
   347   (bitbuf)->bitpos -= (n); \
       
   348   if ((bitbuf)->bitpos < 0) { \
       
   349     (bitbuf)->bitpos += (n); \
       
   350     bibSkipBitsFunc(bitbuf, n); \
       
   351   }
       
   352 
       
   353 
       
   354 
       
   355 #endif  /* #ifndef _BITBUFFER_H_ */