home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource1 / mpegplay / util.h < prev    next >
C/C++ Source or Header  |  1993-02-02  |  22KB  |  363 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21.  
  22. /* Status codes for bit stream i/o operations. */
  23.  
  24. #define NO_VID_STREAM -1
  25. #define UNDERFLOW -2
  26. #define OK 1
  27.  
  28. /* Size increment of extension data buffers. */
  29.  
  30. #define EXT_BUF_SIZE 1024
  31.  
  32. /* External declarations for bitstream i/o operations. */
  33. extern unsigned int bitMask[];
  34. extern unsigned int nBitMask[];
  35. extern unsigned int rBitMask[];
  36. extern unsigned int bitTest[];
  37.  
  38. /* External declarations of bitstream global variables. */
  39. extern unsigned int curBits;
  40. extern int bitOffset;
  41. extern int bufLength;
  42. extern unsigned int *bitBuffer;
  43.  
  44. /* Macro for updating bit counter if analysis tool is on. */
  45. #ifdef ANALYSIS
  46. #define UPDATE_COUNT(numbits) bitCount += numbits
  47. #else
  48. #define UPDATE_COUNT(numbits)
  49. #endif
  50.  
  51. #ifdef NO_SANITY_CHECKS
  52. #define get_bits1(result)                                                 \
  53. {                                                                         \
  54.   UPDATE_COUNT(1);                                                        \
  55.   result = ((curBits & 0x80000000) != 0);                              \
  56.   curBits <<= 1;                                                          \
  57.   bitOffset++;                                                            \
  58.                                                                           \
  59.   if (bitOffset & 0x20) {                                                 \
  60.     bitOffset = 0;                                                        \
  61.     bitBuffer++;                                                          \
  62.     curBits = *bitBuffer;                                                 \
  63.     bufLength--;                                                          \
  64.   }                                                                       \
  65. }
  66.  
  67. #define get_bits2(result)                                                 \
  68. {                                                                         \
  69.   UPDATE_COUNT(2);                                                        \
  70.   bitOffset += 2;                                                         \
  71.                                                                           \
  72.   if (bitOffset & 0x20) {                                                 \
  73.     bitOffset -= 32;                                                      \
  74.     bitBuffer++;                                                          \
  75.     bufLength--;                                                          \
  76.     if (bitOffset) {                                                      \
  77.       curBits |= (*bitBuffer >> (2 - bitOffset));                         \
  78.     }                                                                     \
  79.     result = ((curBits & 0xc0000000) >> 30);                           \
  80.     curBits = *bitBuffer << bitOffset;                                    \
  81.   }                                                                       \
  82.                                                                           \
  83.   result = ((curBits & 0xc0000000) >> 30);                             \
  84.   curBits <<= 2;                                                          \
  85. }
  86.  
  87. #define get_bitsX(num, mask, shift,  result)                              \
  88. {                                                                         \
  89.   UPDATE_COUNT(num);                                                      \
  90.   bitOffset += num;                                                       \
  91.                                                                           \
  92.   if (bitOffset & 0x20) {                                                 \
  93.     bitOffset -= 32;                                                      \
  94.     bitBuffer++;                                                          \
  95.     bufLength--;                                                          \
  96.     if (bitOffset) {                                                      \
  97.       curBits |= (*bitBuffer >> (num - bitOffset));                       \
  98.     }                                                                     \
  99.     result = ((curBits & mask) >> shift);                              \
  100.     curBits = *bitBuffer << bitOffset;                                    \
  101.   }                                                                       \
  102.   else {                                                                  \
  103.     result = ((curBits & mask) >> shift);                              \
  104.     curBits <<= num;                                                      \
  105.   }                                                                       \
  106. }
  107. #else
  108.  
  109. #define get_bits1(result)                                                 \
  110. {                                                                         \
  111.   /* Check for underflow. */                                              \
  112.                                                                           \
  113.   if (bufLength < 2) {                                                    \
  114.     correct_underflow();                                                  \
  115.   }                                                                       \
  116.   UPDATE_COUNT(1);                                                        \
  117.   result = ((curBits & 0x80000000) != 0);                              \
  118.   curBits <<= 1;                                                          \
  119.   bitOffset++;                                                            \
  120.                                                                           \
  121.   if (bitOffset & 0x20) {                                                 \
  122.     bitOffset = 0;                                                        \
  123.     bitBuffer++;                                                          \
  124.     curBits = *bitBuffer;                                                 \
  125.     bufLength--;                                                          \
  126.   }                                                                       \
  127. }
  128.  
  129. #define get_bits2(result)                                                 \
  130. {                                                                         \
  131.   /* Check for underflow. */                                              \
  132.                                                                           \
  133.   if (bufLength < 2) {                                                    \
  134.     correct_underflow();                                                  \
  135.   }                                                                       \
  136.   UPDATE_COUNT(2);                                                        \
  137.   bitOffset += 2;                                                         \
  138.                                                                           \
  139.   if (bitOffset & 0x20) {                                                 \
  140.     bitOffset -= 32;                                                      \
  141.     bitBuffer++;                                                          \
  142.     bufLength--;                                                          \
  143.     if (bitOffset) {                                                      \
  144.       curBits |= (*bitBuffer >> (2 - bitOffset));                         \