From 076cf14d02a46af17d00746d80be68f9374afacd Mon Sep 17 00:00:00 2001 From: myT Date: Sun, 5 Mar 2017 23:32:58 +0100 Subject: [PATCH] using much simpler and faster Huffman codec code when not dealing with out of band datagrams --- build/premake5.lua | 2 + build/vs2013/cnq3-server.vcxproj | 1 + build/vs2013/cnq3-server.vcxproj.filters | 3 + build/vs2013/cnq3.vcxproj | 1 + build/vs2013/cnq3.vcxproj.filters | 3 + code/qcommon/huffman.cpp | 118 +++---- code/qcommon/huffman_static.cpp | 200 ++++++++++++ code/qcommon/msg.cpp | 384 +---------------------- code/qcommon/net_chan.cpp | 2 +- code/qcommon/qcommon.h | 55 +--- code/server/sv_main.cpp | 2 +- 11 files changed, 285 insertions(+), 486 deletions(-) create mode 100644 code/qcommon/huffman_static.cpp diff --git a/build/premake5.lua b/build/premake5.lua index 8b45f66..c68d8fd 100644 --- a/build/premake5.lua +++ b/build/premake5.lua @@ -284,6 +284,7 @@ local function ApplyExeProjectSettings(exeName, server) "qcommon/cvar.cpp", "qcommon/files.cpp", "qcommon/huffman.cpp", + "qcommon/huffman_static.cpp", "qcommon/md4.cpp", "qcommon/md5.cpp", "qcommon/msg.cpp", @@ -351,6 +352,7 @@ local function ApplyExeProjectSettings(exeName, server) "qcommon/cvar.cpp", "qcommon/files.cpp", "qcommon/huffman.cpp", + "qcommon/huffman_static.cpp", "qcommon/md4.cpp", "qcommon/md5.cpp", "qcommon/msg.cpp", diff --git a/build/vs2013/cnq3-server.vcxproj b/build/vs2013/cnq3-server.vcxproj index 91ac4fb..b3d0b12 100644 --- a/build/vs2013/cnq3-server.vcxproj +++ b/build/vs2013/cnq3-server.vcxproj @@ -310,6 +310,7 @@ copy "..\..\..\.bin\release_x64\cnq3-server-x64.pdb" "$(QUAKE3DIR)" + diff --git a/build/vs2013/cnq3-server.vcxproj.filters b/build/vs2013/cnq3-server.vcxproj.filters index 269b4ca..11d0778 100644 --- a/build/vs2013/cnq3-server.vcxproj.filters +++ b/build/vs2013/cnq3-server.vcxproj.filters @@ -248,6 +248,9 @@ qcommon + + qcommon + qcommon diff --git a/build/vs2013/cnq3.vcxproj b/build/vs2013/cnq3.vcxproj index 53e2fcb..da379e2 100644 --- a/build/vs2013/cnq3.vcxproj +++ b/build/vs2013/cnq3.vcxproj @@ -337,6 +337,7 @@ copy "..\..\..\.bin\release_x64\cnq3-x64.pdb" "$(QUAKE3DIR)" + diff --git a/build/vs2013/cnq3.vcxproj.filters b/build/vs2013/cnq3.vcxproj.filters index 72ace0a..7637b52 100644 --- a/build/vs2013/cnq3.vcxproj.filters +++ b/build/vs2013/cnq3.vcxproj.filters @@ -320,6 +320,9 @@ qcommon + + qcommon + qcommon diff --git a/code/qcommon/huffman.cpp b/code/qcommon/huffman.cpp index 35821b0..a3aad44 100644 --- a/code/qcommon/huffman.cpp +++ b/code/qcommon/huffman.cpp @@ -27,32 +27,42 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "q_shared.h" #include "qcommon.h" -#define NYT HUFF_MAX /* NYT = Not Yet Transmitted */ -#define INTERNAL_NODE (HUFF_MAX+1) +#define HMAX 256 /* Maximum symbol */ +#define NYT HMAX /* NYT = Not Yet Transmitted */ +#define INTERNAL_NODE (HMAX+1) static int bloc = 0; -void Huff_putBit( int bit, byte *fout, int *offset) { - bloc = *offset; - if ((bloc&7) == 0) { - fout[(bloc>>3)] = 0; - } - fout[(bloc>>3)] |= bit << (bloc&7); - bloc++; - *offset = bloc; -} +typedef struct nodetype { + struct nodetype *left, *right, *parent; /* tree structure */ + struct nodetype *next, *prev; /* doubly-linked list */ + struct nodetype **head; /* highest ranked node in block */ + int weight; + int symbol; +} node_t; + +typedef struct { + int blocNode; + int blocPtrs; + + node_t* tree; + node_t* lhead; + node_t* ltail; + node_t* loc[HMAX + 1]; + node_t** freelist; + + node_t nodeList[768]; + node_t* nodePtrs[768]; +} huff_t; + +typedef struct { + huff_t compressor; + huff_t decompressor; +} huffman_t; -int Huff_getBit( byte *fin, int *offset) { - int t; - bloc = *offset; - t = (fin[(bloc>>3)] >> (bloc&7)) & 0x1; - bloc++; - *offset = bloc; - return t; -} /* Add a bit to the output file (buffered) */ -static void add_bit (char bit, byte *fout) { +static void add_bit(char bit, byte *fout) { if ((bloc&7) == 0) { fout[(bloc>>3)] = 0; } @@ -60,14 +70,16 @@ static void add_bit (char bit, byte *fout) { bloc++; } + /* Receive one bit from the input file (buffered) */ -static int get_bit (byte *fin) { +static int get_bit(byte *fin) { int t; t = (fin[(bloc>>3)] >> (bloc&7)) & 0x1; bloc++; return t; } + static node_t **get_ppnode(huff_t* huff) { node_t **tppnode; if (!huff->freelist) { @@ -79,13 +91,15 @@ static node_t **get_ppnode(huff_t* huff) { } } + static void free_ppnode(huff_t* huff, node_t **ppnode) { *ppnode = (node_t *)huff->freelist; huff->freelist = ppnode; } + /* Swap the location of these two nodes in the tree */ -static void swap (huff_t* huff, node_t *node1, node_t *node2) { +static void swap(huff_t* huff, node_t *node1, node_t *node2) { node_t *par1, *par2; par1 = node1->parent; @@ -115,6 +129,7 @@ static void swap (huff_t* huff, node_t *node1, node_t *node2) { node2->parent = par1; } + /* Swap these two nodes in the linked list (update ranks) */ static void swaplist(node_t *node1, node_t *node2) { node_t *par1; @@ -147,6 +162,7 @@ static void swaplist(node_t *node1, node_t *node2) { } } + /* Do the increments */ static void increment(huff_t* huff, node_t *node) { node_t *lnode; @@ -186,7 +202,8 @@ static void increment(huff_t* huff, node_t *node) { } } -void Huff_addRef(huff_t* huff, byte ch) { + +static void Huff_addRef(huff_t* huff, byte ch) { node_t *tnode, *tnode2; if (huff->loc[ch] == NULL) { /* if this is the first transmission of this node */ tnode = &(huff->nodeList[huff->blocNode++]); @@ -255,8 +272,9 @@ void Huff_addRef(huff_t* huff, byte ch) { } } + /* Get a symbol */ -int Huff_Receive (node_t *node, int *ch, byte *fin) { +static int Huff_Receive (node_t *node, int *ch, byte *fin) { while (node && node->symbol == INTERNAL_NODE) { if (get_bit(fin)) { node = node->right; @@ -266,29 +284,10 @@ int Huff_Receive (node_t *node, int *ch, byte *fin) { } if (!node) { return 0; -// Com_Error(ERR_DROP, "Illegal tree!\n"); } return (*ch = node->symbol); } -/* Get a symbol */ -void Huff_offsetReceive (node_t *node, int *ch, byte *fin, int *offset) { - bloc = *offset; - while (node && node->symbol == INTERNAL_NODE) { - if (get_bit(fin)) { - node = node->right; - } else { - node = node->left; - } - } - if (!node) { - *ch = 0; - return; -// Com_Error(ERR_DROP, "Illegal tree!\n"); - } - *ch = node->symbol; - *offset = bloc; -} /* Send the prefix code for this node */ static void send(node_t *node, node_t *child, byte *fout) { @@ -304,8 +303,9 @@ static void send(node_t *node, node_t *child, byte *fout) { } } + /* Send a symbol */ -void Huff_transmit (huff_t *huff, int ch, byte *fout) { +static void Huff_transmit (huff_t *huff, int ch, byte *fout) { int i; if (huff->loc[ch] == NULL) { /* node_t hasn't been transmitted, send a NYT, then the symbol */ @@ -318,13 +318,8 @@ void Huff_transmit (huff_t *huff, int ch, byte *fout) { } } -void Huff_offsetTransmit (huff_t *huff, int ch, byte *fout, int *offset) { - bloc = *offset; - send(huff->loc[ch], NULL, fout); - *offset = bloc; -} -void Huff_Decompress(msg_t *mbuf, int offset) { +void DynHuff_Decompress(msg_t *mbuf, int offset) { int ch, cch, i, j, size; byte seq[65536]; byte* buffer; @@ -376,7 +371,7 @@ void Huff_Decompress(msg_t *mbuf, int offset) { Com_Memcpy(mbuf->data + offset, seq, cch); } -void Huff_Compress(msg_t *mbuf, int offset) { +void DynHuff_Compress(msg_t *mbuf, int offset) { int i, ch, size; byte seq[65536]; byte* buffer; @@ -415,24 +410,3 @@ void Huff_Compress(msg_t *mbuf, int offset) { Com_Memcpy(mbuf->data+offset, seq, (bloc>>3)); } -void Huff_Init(huffman_t *huff) { - - Com_Memset(&huff->compressor, 0, sizeof(huff_t)); - Com_Memset(&huff->decompressor, 0, sizeof(huff_t)); - - // Initialize the tree & list with the NYT node - huff->decompressor.tree = huff->decompressor.lhead = huff->decompressor.ltail = huff->decompressor.loc[NYT] = &(huff->decompressor.nodeList[huff->decompressor.blocNode++]); - huff->decompressor.tree->symbol = NYT; - huff->decompressor.tree->weight = 0; - huff->decompressor.lhead->next = huff->decompressor.lhead->prev = NULL; - huff->decompressor.tree->parent = huff->decompressor.tree->left = huff->decompressor.tree->right = NULL; - - // Add the NYT (not yet transmitted) node into the tree/list */ - huff->compressor.tree = huff->compressor.lhead = huff->compressor.loc[NYT] = &(huff->compressor.nodeList[huff->compressor.blocNode++]); - huff->compressor.tree->symbol = NYT; - huff->compressor.tree->weight = 0; - huff->compressor.lhead->next = huff->compressor.lhead->prev = NULL; - huff->compressor.tree->parent = huff->compressor.tree->left = huff->compressor.tree->right = NULL; - huff->compressor.loc[NYT] = huff->compressor.tree; -} - diff --git a/code/qcommon/huffman_static.cpp b/code/qcommon/huffman_static.cpp new file mode 100644 index 0000000..0e919f3 --- /dev/null +++ b/code/qcommon/huffman_static.cpp @@ -0,0 +1,200 @@ +#include "q_shared.h" +#include "qcommon.h" + + +static const uint16_t huff_decodeTable[2048] = +{ + 2512, 2182, 512, 2763, 1859, 2808, 512, 2360, 1918, 1988, 512, 1803, 2158, 2358, 512, 2180, + 1798, 2053, 512, 1804, 2603, 1288, 512, 2166, 2285, 2167, 512, 1281, 1640, 2767, 512, 1664, + 1731, 2116, 512, 2788, 1791, 1808, 512, 1840, 2153, 1921, 512, 2708, 2723, 1549, 512, 2046, + 1893, 2717, 512, 2602, 1801, 1288, 512, 1568, 2480, 2062, 512, 1281, 2145, 2711, 512, 1543, + 1909, 2150, 512, 2077, 2338, 2762, 512, 2162, 1794, 2024, 512, 2168, 1922, 2447, 512, 2334, + 1857, 2117, 512, 2100, 2240, 1288, 512, 2186, 2321, 1908, 512, 1281, 1640, 2242, 512, 1664, + 1731, 2729, 512, 2633, 1791, 1919, 512, 2184, 1917, 1802, 512, 2710, 1795, 1549, 512, 2172, + 2375, 2789, 512, 2171, 2187, 1288, 512, 1568, 2095, 2163, 512, 1281, 1858, 1923, 512, 1543, + 2374, 2446, 512, 2181, 1859, 2160, 512, 2183, 1918, 1988, 512, 1803, 2161, 2751, 512, 2413, + 1798, 2529, 512, 1804, 2344, 1288, 512, 2404, 2156, 2786, 512, 1281, 1640, 2641, 512, 1664, + 1731, 2052, 512, 2170, 1791, 1808, 512, 1840, 2395, 1921, 512, 2586, 2319, 1549, 512, 2046, + 1893, 2101, 512, 2159, 1801, 1288, 512, 1568, 2247, 2773, 512, 1281, 2365, 2410, 512, 1543, + 1909, 2781, 512, 2097, 2411, 2740, 512, 2396, 1794, 2024, 512, 2734, 1922, 2733, 512, 2112, + 1857, 2528, 512, 2593, 2079, 1288, 512, 2648, 2143, 1908, 512, 1281, 1640, 2770, 512, 1664, + 1731, 2169, 512, 2714, 1791, 1919, 512, 2185, 1917, 1802, 512, 2398, 1795, 1549, 512, 2098, + 2801, 2361, 512, 2400, 2328, 1288, 512, 1568, 2783, 2713, 512, 1281, 1858, 1923, 512, 1543, + 2816, 2182, 512, 2497, 1859, 2397, 512, 2794, 1918, 1988, 512, 1803, 2158, 2772, 512, 2180, + 1798, 2053, 512, 1804, 2464, 1288, 512, 2166, 2285, 2167, 512, 1281, 1640, 2764, 512, 1664, + 1731, 2116, 512, 2620, 1791, 1808, 512, 1840, 2153, 1921, 512, 2716, 2384, 1549, 512, 2046, + 1893, 2448, 512, 2722, 1801, 1288, 512, 1568, 2472, 2062, 512, 1281, 2145, 2376, 512, 1543, + 1909, 2150, 512, 2077, 2366, 2709, 512, 2162, 1794, 2024, 512, 2168, 1922, 2735, 512, 2407, + 1857, 2117, 512, 2100, 2240, 1288, 512, 2186, 2779, 1908, 512, 1281, 1640, 2242, 512, 1664, + 1731, 2359, 512, 2705, 1791, 1919, 512, 2184, 1917, 1802, 512, 2642, 1795, 1549, 512, 2172, + 2394, 2645, 512, 2171, 2187, 1288, 512, 1568, 2095, 2163, 512, 1281, 1858, 1923, 512, 1543, + 2450, 2771, 512, 2181, 1859, 2160, 512, 2183, 1918, 1988, 512, 1803, 2161, 2585, 512, 2403, + 1798, 2619, 512, 1804, 2777, 1288, 512, 2355, 2156, 2362, 512, 1281, 1640, 2380, 512, 1664, + 1731, 2052, 512, 2170, 1791, 1808, 512, 1840, 2811, 1921, 512, 2402, 2601, 1549, 512, 2046, + 1893, 2101, 512, 2159, 1801, 1288, 512, 1568, 2247, 2719, 512, 1281, 2747, 2776, 512, 1543, + 1909, 2725, 512, 2097, 2445, 2765, 512, 2638, 1794, 2024, 512, 2444, 1922, 2774, 512, 2112, + 1857, 2727, 512, 2644, 2079, 1288, 512, 2800, 2143, 1908, 512, 1281, 1640, 2580, 512, 1664, + 1731, 2169, 512, 2646, 1791, 1919, 512, 2185, 1917, 1802, 512, 2588, 1795, 1549, 512, 2098, + 2322, 2504, 512, 2623, 2350, 1288, 512, 1568, 2323, 2721, 512, 1281, 1858, 1923, 512, 1543, + 2512, 2182, 512, 2746, 1859, 2798, 512, 2360, 1918, 1988, 512, 1803, 2158, 2358, 512, 2180, + 1798, 2053, 512, 1804, 2745, 1288, 512, 2166, 2285, 2167, 512, 1281, 1640, 2806, 512, 1664, + 1731, 2116, 512, 2796, 1791, 1808, 512, 1840, 2153, 1921, 512, 2582, 2761, 1549, 512, 2046, + 1893, 2793, 512, 2647, 1801, 1288, 512, 1568, 2480, 2062, 512, 1281, 2145, 2738, 512, 1543, + 1909, 2150, 512, 2077, 2338, 2715, 512, 2162, 1794, 2024, 512, 2168, 1922, 2447, 512, 2334, + 1857, 2117, 512, 2100, 2240, 1288, 512, 2186, 2321, 1908, 512, 1281, 1640, 2242, 512, 1664, + 1731, 2795, 512, 2750, 1791, 1919, 512, 2184, 1917, 1802, 512, 2732, 1795, 1549, 512, 2172, + 2375, 2604, 512, 2171, 2187, 1288, 512, 1568, 2095, 2163, 512, 1281, 1858, 1923, 512, 1543, + 2374, 2446, 512, 2181, 1859, 2160, 512, 2183, 1918, 1988, 512, 1803, 2161, 2813, 512, 2413, + 1798, 2529, 512, 1804, 2344, 1288, 512, 2404, 2156, 2743, 512, 1281, 1640, 2748, 512, 1664, + 1731, 2052, 512, 2170, 1791, 1808, 512, 1840, 2395, 1921, 512, 2637, 2319, 1549, 512, 2046, + 1893, 2101, 512, 2159, 1801, 1288, 512, 1568, 2247, 2812, 512, 1281, 2365, 2410, 512, 1543, + 1909, 2799, 512, 2097, 2411, 2802, 512, 2396, 1794, 2024, 512, 2649, 1922, 2595, 512, 2112, + 1857, 2528, 512, 2790, 2079, 1288, 512, 2634, 2143, 1908, 512, 1281, 1640, 2724, 512, 1664, + 1731, 2169, 512, 2730, 1791, 1919, 512, 2185, 1917, 1802, 512, 2398, 1795, 1549, 512, 2098, + 2605, 2361, 512, 2400, 2328, 1288, 512, 1568, 2787, 2810, 512, 1281, 1858, 1923, 512, 1543, + 2803, 2182, 512, 2497, 1859, 2397, 512, 2758, 1918, 1988, 512, 1803, 2158, 2598, 512, 2180, + 1798, 2053, 512, 1804, 2464, 1288, 512, 2166, 2285, 2167, 512, 1281, 1640, 2726, 512, 1664, + 1731, 2116, 512, 2583, 1791, 1808, 512, 1840, 2153, 1921, 512, 2712, 2384, 1549, 512, 2046, + 1893, 2448, 512, 2639, 1801, 1288, 512, 1568, 2472, 2062, 512, 1281, 2145, 2376, 512, 1543, + 1909, 2150, 512, 2077, 2366, 2731, 512, 2162, 1794, 2024, 512, 2168, 1922, 2766, 512, 2407, + 1857, 2117, 512, 2100, 2240, 1288, 512, 2186, 2809, 1908, 512, 1281, 1640, 2242, 512, 1664, + 1731, 2359, 512, 2587, 1791, 1919, 512, 2184, 1917, 1802, 512, 2643, 1795, 1549, 512, 2172, + 2394, 2635, 512, 2171, 2187, 1288, 512, 1568, 2095, 2163, 512, 1281, 1858, 1923, 512, 1543, + 2450, 2749, 512, 2181, 1859, 2160, 512, 2183, 1918, 1988, 512, 1803, 2161, 2778, 512, 2403, + 1798, 2791, 512, 1804, 2775, 1288, 512, 2355, 2156, 2362, 512, 1281, 1640, 2380, 512, 1664, + 1731, 2052, 512, 2170, 1791, 1808, 512, 1840, 2805, 1921, 512, 2402, 2741, 1549, 512, 2046, + 1893, 2101, 512, 2159, 1801, 1288, 512, 1568, 2247, 2769, 512, 1281, 2739, 2780, 512, 1543, + 1909, 2737, 512, 2097, 2445, 2596, 512, 2757, 1794, 2024, 512, 2444, 1922, 2599, 512, 2112, + 1857, 2804, 512, 2744, 2079, 1288, 512, 2707, 2143, 1908, 512, 1281, 1640, 2782, 512, 1664, + 1731, 2169, 512, 2742, 1791, 1919, 512, 2185, 1917, 1802, 512, 2718, 1795, 1549, 512, 2098, + 2322, 2504, 512, 2581, 2350, 1288, 512, 1568, 2323, 2597, 512, 1281, 1858, 1923, 512, 1543, + 2512, 2182, 512, 2763, 1859, 2808, 512, 2360, 1918, 1988, 512, 1803, 2158, 2358, 512, 2180, + 1798, 2053, 512, 1804, 2603, 1288, 512, 2166, 2285, 2167, 512, 1281, 1640, 2767, 512, 1664, + 1731, 2116, 512, 2788, 1791, 1808, 512, 1840, 2153, 1921, 512, 2708, 2723, 1549, 512, 2046, + 1893, 2717, 512, 2602, 1801, 1288, 512, 1568, 2480, 2062, 512, 1281, 2145, 2711, 512, 1543, + 1909, 2150, 512, 2077, 2338, 2762, 512, 2162, 1794, 2024, 512, 2168, 1922, 2447, 512, 2334, + 1857, 2117, 512, 2100, 2240, 1288, 512, 2186, 2321, 1908, 512, 1281, 1640, 2242, 512, 1664, + 1731, 2729, 512, 2633, 1791, 1919, 512, 2184, 1917, 1802, 512, 2710, 1795, 1549, 512, 2172, + 2375, 2789, 512, 2171, 2187, 1288, 512, 1568, 2095, 2163, 512, 1281, 1858, 1923, 512, 1543, + 2374, 2446, 512, 2181, 1859, 2160, 512, 2183, 1918, 1988, 512, 1803, 2161, 2751, 512, 2413, + 1798, 2529, 512, 1804, 2344, 1288, 512, 2404, 2156, 2786, 512, 1281, 1640, 2641, 512, 1664, + 1731, 2052, 512, 2170, 1791, 1808, 512, 1840, 2395, 1921, 512, 2586, 2319, 1549, 512, 2046, + 1893, 2101, 512, 2159, 1801, 1288, 512, 1568, 2247, 2773, 512, 1281, 2365, 2410, 512, 1543, + 1909, 2781, 512, 2097, 2411, 2740, 512, 2396, 1794, 2024, 512, 2734, 1922, 2733, 512, 2112, + 1857, 2528, 512, 2593, 2079, 1288, 512, 2648, 2143, 1908, 512, 1281, 1640, 2770, 512, 1664, + 1731, 2169, 512, 2714, 1791, 1919, 512, 2185, 1917, 1802, 512, 2398, 1795, 1549, 512, 2098, + 2801, 2361, 512, 2400, 2328, 1288, 512, 1568, 2783, 2713, 512, 1281, 1858, 1923, 512, 1543, + 3063, 2182, 512, 2497, 1859, 2397, 512, 2794, 1918, 1988, 512, 1803, 2158, 2772, 512, 2180, + 1798, 2053, 512, 1804, 2464, 1288, 512, 2166, 2285, 2167, 512, 1281, 1640, 2764, 512, 1664, + 1731, 2116, 512, 2620, 1791, 1808, 512, 1840, 2153, 1921, 512, 2716, 2384, 1549, 512, 2046, + 1893, 2448, 512, 2722, 1801, 1288, 512, 1568, 2472, 2062, 512, 1281, 2145, 2376, 512, 1543, + 1909, 2150, 512, 2077, 2366, 2709, 512, 2162, 1794, 2024, 512, 2168, 1922, 2735, 512, 2407, + 1857, 2117, 512, 2100, 2240, 1288, 512, 2186, 2779, 1908, 512, 1281, 1640, 2242, 512, 1664, + 1731, 2359, 512, 2705, 1791, 1919, 512, 2184, 1917, 1802, 512, 2642, 1795, 1549, 512, 2172, + 2394, 2645, 512, 2171, 2187, 1288, 512, 1568, 2095, 2163, 512, 1281, 1858, 1923, 512, 1543, + 2450, 2771, 512, 2181, 1859, 2160, 512, 2183, 1918, 1988, 512, 1803, 2161, 2585, 512, 2403, + 1798, 2619, 512, 1804, 2777, 1288, 512, 2355, 2156, 2362, 512, 1281, 1640, 2380, 512, 1664, + 1731, 2052, 512, 2170, 1791, 1808, 512, 1840, 2811, 1921, 512, 2402, 2601, 1549, 512, 2046, + 1893, 2101, 512, 2159, 1801, 1288, 512, 1568, 2247, 2719, 512, 1281, 2747, 2776, 512, 1543, + 1909, 2725, 512, 2097, 2445, 2765, 512, 2638, 1794, 2024, 512, 2444, 1922, 2774, 512, 2112, + 1857, 2727, 512, 2644, 2079, 1288, 512, 2800, 2143, 1908, 512, 1281, 1640, 2580, 512, 1664, + 1731, 2169, 512, 2646, 1791, 1919, 512, 2185, 1917, 1802, 512, 2588, 1795, 1549, 512, 2098, + 2322, 2504, 512, 2623, 2350, 1288, 512, 1568, 2323, 2721, 512, 1281, 1858, 1923, 512, 1543, + 2512, 2182, 512, 2746, 1859, 2798, 512, 2360, 1918, 1988, 512, 1803, 2158, 2358, 512, 2180, + 1798, 2053, 512, 1804, 2745, 1288, 512, 2166, 2285, 2167, 512, 1281, 1640, 2806, 512, 1664, + 1731, 2116, 512, 2796, 1791, 1808, 512, 1840, 2153, 1921, 512, 2582, 2761, 1549, 512, 2046, + 1893, 2793, 512, 2647, 1801, 1288, 512, 1568, 2480, 2062, 512, 1281, 2145, 2738, 512, 1543, + 1909, 2150, 512, 2077, 2338, 2715, 512, 2162, 1794, 2024, 512, 2168, 1922, 2447, 512, 2334, + 1857, 2117, 512, 2100, 2240, 1288, 512, 2186, 2321, 1908, 512, 1281, 1640, 2242, 512, 1664, + 1731, 2795, 512, 2750, 1791, 1919, 512, 2184, 1917, 1802, 512, 2732, 1795, 1549, 512, 2172, + 2375, 2604, 512, 2171, 2187, 1288, 512, 1568, 2095, 2163, 512, 1281, 1858, 1923, 512, 1543, + 2374, 2446, 512, 2181, 1859, 2160, 512, 2183, 1918, 1988, 512, 1803, 2161, 2813, 512, 2413, + 1798, 2529, 512, 1804, 2344, 1288, 512, 2404, 2156, 2743, 512, 1281, 1640, 2748, 512, 1664, + 1731, 2052, 512, 2170, 1791, 1808, 512, 1840, 2395, 1921, 512, 2637, 2319, 1549, 512, 2046, + 1893, 2101, 512, 2159, 1801, 1288, 512, 1568, 2247, 2812, 512, 1281, 2365, 2410, 512, 1543, + 1909, 2799, 512, 2097, 2411, 2802, 512, 2396, 1794, 2024, 512, 2649, 1922, 2595, 512, 2112, + 1857, 2528, 512, 2790, 2079, 1288, 512, 2634, 2143, 1908, 512, 1281, 1640, 2724, 512, 1664, + 1731, 2169, 512, 2730, 1791, 1919, 512, 2185, 1917, 1802, 512, 2398, 1795, 1549, 512, 2098, + 2605, 2361, 512, 2400, 2328, 1288, 512, 1568, 2787, 2810, 512, 1281, 1858, 1923, 512, 1543, + 2803, 2182, 512, 2497, 1859, 2397, 512, 2758, 1918, 1988, 512, 1803, 2158, 2598, 512, 2180, + 1798, 2053, 512, 1804, 2464, 1288, 512, 2166, 2285, 2167, 512, 1281, 1640, 2726, 512, 1664, + 1731, 2116, 512, 2583, 1791, 1808, 512, 1840, 2153, 1921, 512, 2712, 2384, 1549, 512, 2046, + 1893, 2448, 512, 2639, 1801, 1288, 512, 1568, 2472, 2062, 512, 1281, 2145, 2376, 512, 1543, + 1909, 2150, 512, 2077, 2366, 2731, 512, 2162, 1794, 2024, 512, 2168, 1922, 2766, 512, 2407, + 1857, 2117, 512, 2100, 2240, 1288, 512, 2186, 2809, 1908, 512, 1281, 1640, 2242, 512, 1664, + 1731, 2359, 512, 2587, 1791, 1919, 512, 2184, 1917, 1802, 512, 2643, 1795, 1549, 512, 2172, + 2394, 2635, 512, 2171, 2187, 1288, 512, 1568, 2095, 2163, 512, 1281, 1858, 1923, 512, 1543, + 2450, 2749, 512, 2181, 1859, 2160, 512, 2183, 1918, 1988, 512, 1803, 2161, 2778, 512, 2403, + 1798, 2791, 512, 1804, 2775, 1288, 512, 2355, 2156, 2362, 512, 1281, 1640, 2380, 512, 1664, + 1731, 2052, 512, 2170, 1791, 1808, 512, 1840, 2805, 1921, 512, 2402, 2741, 1549, 512, 2046, + 1893, 2101, 512, 2159, 1801, 1288, 512, 1568, 2247, 2769, 512, 1281, 2739, 2780, 512, 1543, + 1909, 2737, 512, 2097, 2445, 2596, 512, 2757, 1794, 2024, 512, 2444, 1922, 2599, 512, 2112, + 1857, 2804, 512, 2744, 2079, 1288, 512, 2707, 2143, 1908, 512, 1281, 1640, 2782, 512, 1664, + 1731, 2169, 512, 2742, 1791, 1919, 512, 2185, 1917, 1802, 512, 2718, 1795, 1549, 512, 2098, + 2322, 2504, 512, 2581, 2350, 1288, 512, 1568, 2323, 2597, 512, 1281, 1858, 1923, 512, 1543 +}; + +static const uint16_t huff_encodeTable[256] = +{ + 34, 437, 1159, 1735, 2584, 280, 263, 1014, 341, 839, 1687, 183, 311, 726, 920, 2761, + 599, 1417, 7945, 8073, 7642, 16186, 8890, 12858, 3913, 6362, 2746, 13882, 7866, 1080, 1273, 3400, + 886, 3386, 1097, 11482, 15450, 16282, 12506, 15578, 2377, 6858, 826, 330, 10010, 12042, 8009, 1928, + 631, 3128, 3832, 6521, 1336, 2840, 217, 5657, 121, 3865, 6553, 6426, 4666, 3017, 5193, 7994, + 3320, 1287, 1991, 71, 536, 1304, 2057, 1801, 5081, 1594, 11642, 14106, 6617, 10938, 7290, 13114, + 4809, 2522, 5818, 14010, 7482, 5914, 7738, 9018, 3450, 11450, 5897, 2697, 3193, 4185, 3769, 3464, + 3897, 968, 6841, 6393, 2425, 775, 1048, 5369, 454, 648, 3033, 3145, 2440, 2297, 200, 2872, + 2136, 2248, 1144, 1944, 1431, 1031, 376, 408, 1208, 3608, 2616, 1848, 1784, 1671, 135, 1623, + 502, 663, 1223, 2007, 248, 2104, 24, 2168, 1656, 3704, 1400, 1864, 7353, 7241, 2073, 1241, + 4889, 5690, 6153, 15738, 698, 5210, 1722, 986, 12986, 3994, 3642, 9306, 4794, 794, 16058, 7066, + 4425, 8090, 4922, 714, 11738, 7194, 12762, 7450, 5001, 1562, 11834, 13402, 9914, 3290, 3258, 5338, + 905, 15386, 9178, 15306, 3162, 15050, 15930, 10650, 15674, 8522, 8250, 7114, 10714, 14362, 9786, 2266, + 1352, 4153, 1496, 518, 151, 15482, 12410, 2952, 7961, 8906, 1114, 58, 4570, 7258, 13530, 474, + 9, 15258, 3546, 6170, 4314, 2970, 7386, 14666, 7130, 6474, 14554, 5514, 15322, 3098, 15834, 3978, + 3353, 2329, 2458, 12170, 570, 1818, 11578, 14618, 1175, 8986, 4218, 9754, 8762, 392, 8282, 11290, + 7546, 3850, 11354, 12298, 15642, 14986, 8666, 20491, 90, 13706, 12186, 6794, 11162, 10458, 759, 582 +}; + + +int StatHuff_ReadBit( byte* buffer, int bitIndex ) +{ + return (buffer[(bitIndex >> 3)] >> (bitIndex & 7)) & 0x1; +} + + +void StatHuff_WriteBit( int bit, byte* buffer, int bitIndex ) +{ + // is this the first bit of a new byte? + if ((bitIndex & 7) == 0) { + buffer[bitIndex >> 3] = 0; + } + buffer[bitIndex >> 3] |= bit << (bitIndex & 7); +} + + +int StatHuff_ReadSymbol( int* symbol, byte* buffer, int bitIndex ) +{ + const uint16_t code = ((*(const uint32_t*)(buffer + (bitIndex >> 3))) >> ((uint32_t)bitIndex & 7)) & 0x7FF; + const uint16_t entry = huff_decodeTable[code]; + + *symbol = (int)(entry & 0xFF); + + return (int)(entry >> 8); +} + + +int StatHuff_WriteSymbol( int symbol, byte* buffer, int bitIndex ) +{ + const uint16_t entry = huff_encodeTable[symbol]; + const int bitCount = (int)(entry & 15); + const int code = (int)((entry >> 4) & 0x7FF); + int bits = (int)code; + + for (int i = 0; i < bitCount; i++) { + StatHuff_WriteBit(bits & 1, buffer, bitIndex); + bits >>= 1; + bitIndex++; + } + + return bitCount; +} + diff --git a/code/qcommon/msg.cpp b/code/qcommon/msg.cpp index e3ad439..c36e2f5 100644 --- a/code/qcommon/msg.cpp +++ b/code/qcommon/msg.cpp @@ -22,9 +22,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "q_shared.h" #include "qcommon.h" -static huffman_t msgHuff; - -static qbool msgInit = qfalse; /* ============================================================================== @@ -35,21 +32,14 @@ Handles byte ordering and avoids alignment errors ============================================================================== */ -static void MSG_initHuffman(); void MSG_Init( msg_t *buf, byte *data, int length ) { - if (!msgInit) { - MSG_initHuffman(); - } Com_Memset (buf, 0, sizeof(*buf)); buf->data = data; buf->maxsize = length; } void MSG_InitOOB( msg_t *buf, byte *data, int length ) { - if (!msgInit) { - MSG_initHuffman(); - } Com_Memset (buf, 0, sizeof(*buf)); buf->data = data; buf->maxsize = length; @@ -100,8 +90,7 @@ bit functions // negative bit values include signs void MSG_WriteBits( msg_t *msg, int value, int bits ) { - int i; -// FILE* fp; + int i, bitIndex; // this isn't an exact overflow check, but close enough if ( msg->maxsize - msg->cursize < 4 ) { @@ -113,21 +102,6 @@ void MSG_WriteBits( msg_t *msg, int value, int bits ) { Com_Error( ERR_DROP, "MSG_WriteBits: bad bits %i", bits ); } -/* check for overflows - if ( bits != 32 ) { - if ( bits > 0 ) { - if ( value > ( ( 1 << bits ) - 1 ) || value < 0 ) { - overflows++; - } - } else { - int r = 1 << (bits-1); - if ( value > r - 1 || value < -r ) { - overflows++; - } - } - } -*/ - if ( bits < 0 ) { bits = -bits; } @@ -150,35 +124,36 @@ void MSG_WriteBits( msg_t *msg, int value, int bits ) { Com_Error(ERR_DROP, "can't write %d bits\n", bits); } } else { -// fp = fopen("c:\\netchan.bin", "a"); value &= (0xffffffff>>(32-bits)); if (bits&7) { int nbits; nbits = bits&7; + bitIndex = msg->bit; for(i=0;idata, &msg->bit); + StatHuff_WriteBit((value & 1), msg->data, bitIndex); value = (value>>1); + bitIndex++; } + msg->bit = bitIndex; bits = bits - nbits; } if (bits) { + bitIndex = msg->bit; for(i=0;idata, &msg->bit); + bitIndex += StatHuff_WriteSymbol((value & 0xff), msg->data, bitIndex); value = (value>>8); } + msg->bit = bitIndex; } msg->cursize = (msg->bit>>3)+1; -// fclose(fp); } } int MSG_ReadBits( msg_t *msg, int bits ) { int value; int get; - qbool sgn; - int i, nbits; -// FILE* fp; + qbool sgn; + int i, nbits, bitIndex; value = 0; @@ -211,19 +186,21 @@ int MSG_ReadBits( msg_t *msg, int bits ) { nbits = 0; if (bits&7) { nbits = bits&7; + bitIndex = msg->bit; for(i=0;idata, &msg->bit)<data, bitIndex) << i; + bitIndex++; } + msg->bit = bitIndex; bits = bits - nbits; } if (bits) { -// fp = fopen("c:\\netchan.bin", "a"); + bitIndex = msg->bit; for(i=0;idata, &msg->bit); -// fwrite(&get, 1, 1, fp); + bitIndex += StatHuff_ReadSymbol(&get, msg->data, bitIndex); value |= (get<<(i+nbits)); } -// fclose(fp); + msg->bit = bitIndex; } msg->readcount = (msg->bit>>3)+1; } @@ -1235,330 +1212,3 @@ void MSG_ReadDeltaPlayerstate( msg_t* msg, const playerState_t* from, playerStat } } - -/////////////////////////////////////////////////////////////// - - -static const int msg_hData[256] = { -250315, // 0 -41193, // 1 -6292, // 2 -7106, // 3 -3730, // 4 -3750, // 5 -6110, // 6 -23283, // 7 -33317, // 8 -6950, // 9 -7838, // 10 -9714, // 11 -9257, // 12 -17259, // 13 -3949, // 14 -1778, // 15 -8288, // 16 -1604, // 17 -1590, // 18 -1663, // 19 -1100, // 20 -1213, // 21 -1238, // 22 -1134, // 23 -1749, // 24 -1059, // 25 -1246, // 26 -1149, // 27 -1273, // 28 -4486, // 29 -2805, // 30 -3472, // 31 -21819, // 32 -1159, // 33 -1670, // 34 -1066, // 35 -1043, // 36 -1012, // 37 -1053, // 38 -1070, // 39 -1726, // 40 -888, // 41 -1180, // 42 -850, // 43 -960, // 44 -780, // 45 -1752, // 46 -3296, // 47 -10630, // 48 -4514, // 49 -5881, // 50 -2685, // 51 -4650, // 52 -3837, // 53 -2093, // 54 -1867, // 55 -2584, // 56 -1949, // 57 -1972, // 58 -940, // 59 -1134, // 60 -1788, // 61 -1670, // 62 -1206, // 63 -5719, // 64 -6128, // 65 -7222, // 66 -6654, // 67 -3710, // 68 -3795, // 69 -1492, // 70 -1524, // 71 -2215, // 72 -1140, // 73 -1355, // 74 -971, // 75 -2180, // 76 -1248, // 77 -1328, // 78 -1195, // 79 -1770, // 80 -1078, // 81 -1264, // 82 -1266, // 83 -1168, // 84 -965, // 85 -1155, // 86 -1186, // 87 -1347, // 88 -1228, // 89 -1529, // 90 -1600, // 91 -2617, // 92 -2048, // 93 -2546, // 94 -3275, // 95 -2410, // 96 -3585, // 97 -2504, // 98 -2800, // 99 -2675, // 100 -6146, // 101 -3663, // 102 -2840, // 103 -14253, // 104 -3164, // 105 -2221, // 106 -1687, // 107 -3208, // 108 -2739, // 109 -3512, // 110 -4796, // 111 -4091, // 112 -3515, // 113 -5288, // 114 -4016, // 115 -7937, // 116 -6031, // 117 -5360, // 118 -3924, // 119 -4892, // 120 -3743, // 121 -4566, // 122 -4807, // 123 -5852, // 124 -6400, // 125 -6225, // 126 -8291, // 127 -23243, // 128 -7838, // 129 -7073, // 130 -8935, // 131 -5437, // 132 -4483, // 133 -3641, // 134 -5256, // 135 -5312, // 136 -5328, // 137 -5370, // 138 -3492, // 139 -2458, // 140 -1694, // 141 -1821, // 142 -2121, // 143 -1916, // 144 -1149, // 145 -1516, // 146 -1367, // 147 -1236, // 148 -1029, // 149 -1258, // 150 -1104, // 151 -1245, // 152 -1006, // 153 -1149, // 154 -1025, // 155 -1241, // 156 -952, // 157 -1287, // 158 -997, // 159 -1713, // 160 -1009, // 161 -1187, // 162 -879, // 163 -1099, // 164 -929, // 165 -1078, // 166 -951, // 167 -1656, // 168 -930, // 169 -1153, // 170 -1030, // 171 -1262, // 172 -1062, // 173 -1214, // 174 -1060, // 175 -1621, // 176 -930, // 177 -1106, // 178 -912, // 179 -1034, // 180 -892, // 181 -1158, // 182 -990, // 183 -1175, // 184 -850, // 185 -1121, // 186 -903, // 187 -1087, // 188 -920, // 189 -1144, // 190 -1056, // 191 -3462, // 192 -2240, // 193 -4397, // 194 -12136, // 195 -7758, // 196 -1345, // 197 -1307, // 198 -3278, // 199 -1950, // 200 -886, // 201 -1023, // 202 -1112, // 203 -1077, // 204 -1042, // 205 -1061, // 206 -1071, // 207 -1484, // 208 -1001, // 209 -1096, // 210 -915, // 211 -1052, // 212 -995, // 213 -1070, // 214 -876, // 215 -1111, // 216 -851, // 217 -1059, // 218 -805, // 219 -1112, // 220 -923, // 221 -1103, // 222 -817, // 223 -1899, // 224 -1872, // 225 -976, // 226 -841, // 227 -1127, // 228 -956, // 229 -1159, // 230 -950, // 231 -7791, // 232 -954, // 233 -1289, // 234 -933, // 235 -1127, // 236 -3207, // 237 -1020, // 238 -927, // 239 -1355, // 240 -768, // 241 -1040, // 242 -745, // 243 -952, // 244 -805, // 245 -1073, // 246 -740, // 247 -1013, // 248 -805, // 249 -1008, // 250 -796, // 251 -996, // 252 -1057, // 253 -11457, // 254 -13504, // 255 -}; - - -static void MSG_initHuffman() -{ - int i,j; - - msgInit = qtrue; - Huff_Init(&msgHuff); - for(i=0;i<256;i++) { - for (j=0;jdata[4], 7)) { - Huff_Decompress(msg, 12); + DynHuff_Decompress(msg, 12); } const char* s = MSG_ReadStringLine( msg );