bitstream.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : wwbitpack *
  23. * *
  24. * $Archive:: /Commando/Code/wwbitpack/bitstream.h $*
  25. * *
  26. * Original Author:: Tom Spencer-Smith *
  27. * *
  28. * $Author:: Patrick $*
  29. * *
  30. * $Modtime:: 6/13/01 9:05a $*
  31. * *
  32. * $Revision:: 3 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #ifndef BITSTREAM_H
  38. #define BITSTREAM_H
  39. #include "bitpacker.h"
  40. #include "wwdebug.h"
  41. #include "encoderlist.h"
  42. #include "mathutil.h"
  43. #include "math.h"
  44. #include "widestring.h"
  45. #define BYTE_DEPTH(x) (sizeof(x))
  46. #define BIT_DEPTH(x) (8 * sizeof(x))
  47. /**
  48. ** BitStreamClass
  49. **
  50. ** Author: Tom Spencer-Smith
  51. ** Date: June 1998
  52. ** Description: A class for minimal bit encoding.
  53. ** Notes:
  54. ** - Uncompressed data may be included in the bitstream.
  55. ** - Compression may be disabled entirely if desired.
  56. ** - Bools are compressed to 1 bit without requiring a precision
  57. ** setup.
  58. ** - Strings and raw data are uncompressed.
  59. **
  60. ** (gth, 08/31/2000) - renamed this class to BitStreamClass (from cTypeEncoder) and
  61. ** cleaned it up to become the interface that all game and library code uses to
  62. ** package up their state variables for network transmission, converted to westwood
  63. ** naming convention since it is going to propogate to a lot of other code.
  64. */
  65. class BitStreamClass : public cBitPacker
  66. {
  67. public:
  68. BitStreamClass();
  69. BitStreamClass& operator=(const BitStreamClass& rhs);
  70. UINT Get_Uncompressed_Size_Bytes() const {return UncompressedSizeBytes;}
  71. UINT Get_Compressed_Size_Bytes() const;
  72. UINT Get_Compression_Pc() const;
  73. //
  74. // For data which may include NULL's.
  75. // Data will not be compressed.
  76. //
  77. void Add_Raw_Data(LPCSTR data, USHORT data_size);
  78. void Get_Raw_Data(char * buffer, USHORT buffer_size, USHORT data_size);
  79. //
  80. // For data terminated with NULL.
  81. // Data will not be compressed.
  82. // You may permit or disallow empty strings to be passed.
  83. //
  84. void Add_Terminated_String(LPCSTR string, bool permit_empty = false);
  85. void Get_Terminated_String(char * buffer, USHORT buffer_size, bool permit_empty = false);
  86. //
  87. // For data terminated with NULL.
  88. // Data will not be compressed.
  89. // You may permit or disallow empty strings to be passed.
  90. //
  91. void Add_Wide_Terminated_String(const WCHAR *string, bool permit_empty = false);
  92. void Get_Wide_Terminated_String (WCHAR *buffer, USHORT buffer_len, bool permit_empty = false);
  93. //
  94. // Bool is special-cased because we know that we can always
  95. // represent it as 1 bit.
  96. //
  97. void Add(bool value);
  98. bool Get(bool & value);
  99. //
  100. // For all other data types that we want to support, call into our internal
  101. // template function.
  102. //
  103. enum {NO_ENCODER = -1};
  104. void Add(BYTE val,int type = NO_ENCODER) { Internal_Add(val,type); }
  105. void Add(USHORT val,int type = NO_ENCODER) { Internal_Add(val,type); }
  106. void Add(UINT val,int type = NO_ENCODER) { Internal_Add(val,type); }
  107. void Add(ULONG val,int type = NO_ENCODER) { Internal_Add(val,type); }
  108. void Add(char val,int type = NO_ENCODER) { Internal_Add(val,type); }
  109. void Add(int val,int type = NO_ENCODER) { Internal_Add(val,type); }
  110. void Add(float val,int type = NO_ENCODER) { Internal_Add(val,type); }
  111. BYTE Get(BYTE & set_val,int type = NO_ENCODER) { return Internal_Get(set_val,type); }
  112. USHORT Get(USHORT & set_val,int type = NO_ENCODER) { return Internal_Get(set_val,type); }
  113. ULONG Get(ULONG & set_val,int type = NO_ENCODER) { return Internal_Get(set_val,type); }
  114. UINT Get(UINT & set_val,int type = NO_ENCODER) { return Internal_Get(set_val,type); }
  115. char Get(char & set_val,int type = NO_ENCODER) { return Internal_Get(set_val,type); }
  116. int Get(int & set_val,int type = NO_ENCODER) { return Internal_Get(set_val,type); }
  117. float Get(float & set_val,int type = NO_ENCODER) { return Internal_Get(set_val,type); }
  118. private:
  119. //
  120. // Add/Get for remaining atomic data types.
  121. // I really wish the following 3 methods were in the source file, but
  122. // the compiler won't accept this. Hopefully the pragma and MSVC
  123. // will prevent inlining.
  124. //
  125. #pragma auto_inline(off)
  126. //------------------------------------------------------------------------------------
  127. template<class T> void Internal_Add(T value, int type = NO_ENCODER) {
  128. if (cEncoderList::Is_Compression_Enabled() && type != NO_ENCODER) {
  129. WWASSERT(type >= 0 && type < MAX_ENCODERTYPES);
  130. cEncoderTypeEntry & entry = cEncoderList::Get_Encoder_Type_Entry(type);
  131. //
  132. // If the following assert hits then the value of the type
  133. // parameter is unknown.
  134. //
  135. WWASSERT(entry.Is_Valid());
  136. ULONG scaled_value;
  137. bool is_in_range = entry.Scale(value, scaled_value);
  138. if (!is_in_range) {
  139. //WWDEBUG_SAY(("BitStreamClass::Add : Warning: out-of-range value clamped (type %d).\n",
  140. // type));
  141. //DIE;
  142. }
  143. Add_Bits(scaled_value, entry.Get_Bit_Precision());
  144. } else {
  145. Add_Bits(*(reinterpret_cast<ULONG *>(&value)), BIT_DEPTH(T));
  146. }
  147. UncompressedSizeBytes += BYTE_DEPTH(T);
  148. }
  149. //------------------------------------------------------------------------------------
  150. template<class T> T Internal_Get(T & value, int type = NO_ENCODER) {
  151. if (cEncoderList::Is_Compression_Enabled() && type != NO_ENCODER) {
  152. WWASSERT(type >= 0 && type < MAX_ENCODERTYPES);
  153. cEncoderTypeEntry & entry = cEncoderList::Get_Encoder_Type_Entry(type);
  154. //
  155. // If the following assert hits then the value of the type
  156. // parameter is unknown.
  157. //
  158. WWASSERT(entry.Is_Valid());
  159. ULONG u_value;
  160. Get_Bits(u_value, entry.Get_Bit_Precision());
  161. double f_value = entry.Unscale(u_value);
  162. if ((::fabs(f_value - static_cast<T>(f_value)) < MISCUTIL_EPSILON)) {
  163. //
  164. // N.B. More error may be introduced here
  165. //
  166. value = static_cast<T>(f_value);
  167. } else {
  168. value = static_cast<T>(cMathUtil::Round(f_value));
  169. }
  170. WWASSERT(entry.Is_Value_In_Range(value));
  171. } else {
  172. ULONG u_value;
  173. Get_Bits(u_value, BIT_DEPTH(T));
  174. value = *(reinterpret_cast<T *>(&u_value));
  175. }
  176. return value;
  177. }
  178. //------------------------------------------------------------------------------------
  179. #pragma auto_inline(on)
  180. UINT UncompressedSizeBytes; // for statistics only
  181. };
  182. #endif // TYPEENCODER_H