HuffmanVLC.cpp
1 /* 2 * libsmackerdec - Smacker video decoder 3 * Copyright (C) 2011 Barry Duncan 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 9 * 10 * This library 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 GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 #include <HuffmanVLC.h> 21 22 namespace SmackerCommon { 23 24 uint16_t VLC_GetCodeBits(BitReader &bits, VLCtable &table) 25 { 26 uint32_t codeBits = 0; 27 28 // search each length array 29 for (uint32_t i = 0; i < table.size(); i++) 30 { 31 // get and add a new bit to codeBits 32 uint32_t theBit = bits.GetBit() << i; 33 codeBits |= theBit; 34 35 // search for a code match 36 for (uint32_t j = 0; j < table[i].size(); j++) 37 { 38 if (codeBits == table[i][j].code) 39 { 40 return table[i][j].symbol; 41 } 42 } 43 } 44 45 // shouldn't get here.. 46 return 0; 47 } 48 49 void VLC_InitTable(VLCtable &table, uint32_t maxLength, uint32_t size, int *lengths, uint32_t *bits) 50 { 51 table.resize(maxLength); 52 53 for (uint32_t i = 0; i < size; i++) 54 { 55 VLC newCode; 56 newCode.symbol = i; 57 newCode.code = bits[i]; 58 59 uint32_t codeLength = lengths[i]; 60 61 if (codeLength) 62 table[codeLength - 1].push_back(newCode); 63 } 64 } 65 66 uint32_t VLC_GetSize(VLCtable &table) 67 { 68 return table.size(); 69 } 70 71 } // close namespace SmackerCommon