Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /******************************************************************** |
michael@0 | 2 | * * |
michael@0 | 3 | * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. * |
michael@0 | 4 | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
michael@0 | 5 | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
michael@0 | 6 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
michael@0 | 7 | * * |
michael@0 | 8 | * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 * |
michael@0 | 9 | * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * |
michael@0 | 10 | * * |
michael@0 | 11 | ******************************************************************** |
michael@0 | 12 | |
michael@0 | 13 | function: |
michael@0 | 14 | last mod: $Id: huffdec.c 17577 2010-10-29 04:00:07Z tterribe $ |
michael@0 | 15 | |
michael@0 | 16 | ********************************************************************/ |
michael@0 | 17 | |
michael@0 | 18 | #include <stdlib.h> |
michael@0 | 19 | #include <string.h> |
michael@0 | 20 | #include <ogg/ogg.h> |
michael@0 | 21 | #include "huffdec.h" |
michael@0 | 22 | #include "decint.h" |
michael@0 | 23 | |
michael@0 | 24 | |
michael@0 | 25 | |
michael@0 | 26 | /*Instead of storing every branching in the tree, subtrees can be collapsed |
michael@0 | 27 | into one node, with a table of size 1<<nbits pointing directly to its |
michael@0 | 28 | descedents nbits levels down. |
michael@0 | 29 | This allows more than one bit to be read at a time, and avoids following all |
michael@0 | 30 | the intermediate branches with next to no increased code complexity once |
michael@0 | 31 | the collapsed tree has been built. |
michael@0 | 32 | We do _not_ require that a subtree be complete to be collapsed, but instead |
michael@0 | 33 | store duplicate pointers in the table, and record the actual depth of the |
michael@0 | 34 | node below its parent. |
michael@0 | 35 | This tells us the number of bits to advance the stream after reaching it. |
michael@0 | 36 | |
michael@0 | 37 | This turns out to be equivalent to the method described in \cite{Hash95}, |
michael@0 | 38 | without the requirement that codewords be sorted by length. |
michael@0 | 39 | If the codewords were sorted by length (so-called ``canonical-codes''), they |
michael@0 | 40 | could be decoded much faster via either Lindell and Moffat's approach or |
michael@0 | 41 | Hashemian's Condensed Huffman Code approach, the latter of which has an |
michael@0 | 42 | extremely small memory footprint. |
michael@0 | 43 | We can't use Choueka et al.'s finite state machine approach, which is |
michael@0 | 44 | extremely fast, because we can't allow multiple symbols to be output at a |
michael@0 | 45 | time; the codebook can and does change between symbols. |
michael@0 | 46 | It also has very large memory requirements, which impairs cache coherency. |
michael@0 | 47 | |
michael@0 | 48 | We store the tree packed in an array of 16-bit integers (words). |
michael@0 | 49 | Each node consists of a single word, followed consecutively by two or more |
michael@0 | 50 | indices of its children. |
michael@0 | 51 | Let n be the value of this first word. |
michael@0 | 52 | This is the number of bits that need to be read to traverse the node, and |
michael@0 | 53 | must be positive. |
michael@0 | 54 | 1<<n entries follow in the array, each an index to a child node. |
michael@0 | 55 | If the child is positive, then it is the index of another internal node in |
michael@0 | 56 | the table. |
michael@0 | 57 | If the child is negative or zero, then it is a leaf node. |
michael@0 | 58 | These are stored directly in the child pointer to save space, since they only |
michael@0 | 59 | require a single word. |
michael@0 | 60 | If a leaf node would have been encountered before reading n bits, then it is |
michael@0 | 61 | duplicated the necessary number of times in this table. |
michael@0 | 62 | Leaf nodes pack both a token value and their actual depth in the tree. |
michael@0 | 63 | The token in the leaf node is (-leaf&255). |
michael@0 | 64 | The number of bits that need to be consumed to reach the leaf, starting from |
michael@0 | 65 | the current node, is (-leaf>>8). |
michael@0 | 66 | |
michael@0 | 67 | @ARTICLE{Hash95, |
michael@0 | 68 | author="Reza Hashemian", |
michael@0 | 69 | title="Memory Efficient and High-Speed Search {Huffman} Coding", |
michael@0 | 70 | journal="{IEEE} Transactions on Communications", |
michael@0 | 71 | volume=43, |
michael@0 | 72 | number=10, |
michael@0 | 73 | pages="2576--2581", |
michael@0 | 74 | month=Oct, |
michael@0 | 75 | year=1995 |
michael@0 | 76 | }*/ |
michael@0 | 77 | |
michael@0 | 78 | |
michael@0 | 79 | |
michael@0 | 80 | /*The map from external spec-defined tokens to internal tokens. |
michael@0 | 81 | This is constructed so that any extra bits read with the original token value |
michael@0 | 82 | can be masked off the least significant bits of its internal token index. |
michael@0 | 83 | In addition, all of the tokens which require additional extra bits are placed |
michael@0 | 84 | at the start of the list, and grouped by type. |
michael@0 | 85 | OC_DCT_REPEAT_RUN3_TOKEN is placed first, as it is an extra-special case, so |
michael@0 | 86 | giving it index 0 may simplify comparisons on some architectures. |
michael@0 | 87 | These requirements require some substantial reordering.*/ |
michael@0 | 88 | static const unsigned char OC_DCT_TOKEN_MAP[TH_NDCT_TOKENS]={ |
michael@0 | 89 | /*OC_DCT_EOB1_TOKEN (0 extra bits)*/ |
michael@0 | 90 | 15, |
michael@0 | 91 | /*OC_DCT_EOB2_TOKEN (0 extra bits)*/ |
michael@0 | 92 | 16, |
michael@0 | 93 | /*OC_DCT_EOB3_TOKEN (0 extra bits)*/ |
michael@0 | 94 | 17, |
michael@0 | 95 | /*OC_DCT_REPEAT_RUN0_TOKEN (2 extra bits)*/ |
michael@0 | 96 | 88, |
michael@0 | 97 | /*OC_DCT_REPEAT_RUN1_TOKEN (3 extra bits)*/ |
michael@0 | 98 | 80, |
michael@0 | 99 | /*OC_DCT_REPEAT_RUN2_TOKEN (4 extra bits)*/ |
michael@0 | 100 | 1, |
michael@0 | 101 | /*OC_DCT_REPEAT_RUN3_TOKEN (12 extra bits)*/ |
michael@0 | 102 | 0, |
michael@0 | 103 | /*OC_DCT_SHORT_ZRL_TOKEN (3 extra bits)*/ |
michael@0 | 104 | 48, |
michael@0 | 105 | /*OC_DCT_ZRL_TOKEN (6 extra bits)*/ |
michael@0 | 106 | 14, |
michael@0 | 107 | /*OC_ONE_TOKEN (0 extra bits)*/ |
michael@0 | 108 | 56, |
michael@0 | 109 | /*OC_MINUS_ONE_TOKEN (0 extra bits)*/ |
michael@0 | 110 | 57, |
michael@0 | 111 | /*OC_TWO_TOKEN (0 extra bits)*/ |
michael@0 | 112 | 58, |
michael@0 | 113 | /*OC_MINUS_TWO_TOKEN (0 extra bits)*/ |
michael@0 | 114 | 59, |
michael@0 | 115 | /*OC_DCT_VAL_CAT2 (1 extra bit)*/ |
michael@0 | 116 | 60, |
michael@0 | 117 | 62, |
michael@0 | 118 | 64, |
michael@0 | 119 | 66, |
michael@0 | 120 | /*OC_DCT_VAL_CAT3 (2 extra bits)*/ |
michael@0 | 121 | 68, |
michael@0 | 122 | /*OC_DCT_VAL_CAT4 (3 extra bits)*/ |
michael@0 | 123 | 72, |
michael@0 | 124 | /*OC_DCT_VAL_CAT5 (4 extra bits)*/ |
michael@0 | 125 | 2, |
michael@0 | 126 | /*OC_DCT_VAL_CAT6 (5 extra bits)*/ |
michael@0 | 127 | 4, |
michael@0 | 128 | /*OC_DCT_VAL_CAT7 (6 extra bits)*/ |
michael@0 | 129 | 6, |
michael@0 | 130 | /*OC_DCT_VAL_CAT8 (10 extra bits)*/ |
michael@0 | 131 | 8, |
michael@0 | 132 | /*OC_DCT_RUN_CAT1A (1 extra bit)*/ |
michael@0 | 133 | 18, |
michael@0 | 134 | 20, |
michael@0 | 135 | 22, |
michael@0 | 136 | 24, |
michael@0 | 137 | 26, |
michael@0 | 138 | /*OC_DCT_RUN_CAT1B (3 extra bits)*/ |
michael@0 | 139 | 32, |
michael@0 | 140 | /*OC_DCT_RUN_CAT1C (4 extra bits)*/ |
michael@0 | 141 | 12, |
michael@0 | 142 | /*OC_DCT_RUN_CAT2A (2 extra bits)*/ |
michael@0 | 143 | 28, |
michael@0 | 144 | /*OC_DCT_RUN_CAT2B (3 extra bits)*/ |
michael@0 | 145 | 40 |
michael@0 | 146 | }; |
michael@0 | 147 | |
michael@0 | 148 | /*The log base 2 of number of internal tokens associated with each of the spec |
michael@0 | 149 | tokens (i.e., how many of the extra bits are folded into the token value). |
michael@0 | 150 | Increasing the maximum value beyond 3 will enlarge the amount of stack |
michael@0 | 151 | required for tree construction.*/ |
michael@0 | 152 | static const unsigned char OC_DCT_TOKEN_MAP_LOG_NENTRIES[TH_NDCT_TOKENS]={ |
michael@0 | 153 | 0,0,0,2,3,0,0,3,0,0,0,0,0,1,1,1,1,2,3,1,1,1,2,1,1,1,1,1,3,1,2,3 |
michael@0 | 154 | }; |
michael@0 | 155 | |
michael@0 | 156 | |
michael@0 | 157 | /*The size a lookup table is allowed to grow to relative to the number of |
michael@0 | 158 | unique nodes it contains. |
michael@0 | 159 | E.g., if OC_HUFF_SLUSH is 4, then at most 75% of the space in the tree is |
michael@0 | 160 | wasted (1/4 of the space must be used). |
michael@0 | 161 | Larger numbers can decode tokens with fewer read operations, while smaller |
michael@0 | 162 | numbers may save more space. |
michael@0 | 163 | With a sample file: |
michael@0 | 164 | 32233473 read calls are required when no tree collapsing is done (100.0%). |
michael@0 | 165 | 19269269 read calls are required when OC_HUFF_SLUSH is 1 (59.8%). |
michael@0 | 166 | 11144969 read calls are required when OC_HUFF_SLUSH is 2 (34.6%). |
michael@0 | 167 | 10538563 read calls are required when OC_HUFF_SLUSH is 4 (32.7%). |
michael@0 | 168 | 10192578 read calls are required when OC_HUFF_SLUSH is 8 (31.6%). |
michael@0 | 169 | Since a value of 2 gets us the vast majority of the speed-up with only a |
michael@0 | 170 | small amount of wasted memory, this is what we use. |
michael@0 | 171 | This value must be less than 128, or you could create a tree with more than |
michael@0 | 172 | 32767 entries, which would overflow the 16-bit words used to index it.*/ |
michael@0 | 173 | #define OC_HUFF_SLUSH (2) |
michael@0 | 174 | /*The root of the tree is on the fast path, and a larger value here is more |
michael@0 | 175 | beneficial than elsewhere in the tree. |
michael@0 | 176 | 7 appears to give the best performance, trading off between increased use of |
michael@0 | 177 | the single-read fast path and cache footprint for the tables, though |
michael@0 | 178 | obviously this will depend on your cache size. |
michael@0 | 179 | Using 7 here, the VP3 tables are about twice as large compared to using 2.*/ |
michael@0 | 180 | #define OC_ROOT_HUFF_SLUSH (7) |
michael@0 | 181 | |
michael@0 | 182 | |
michael@0 | 183 | |
michael@0 | 184 | /*Unpacks a Huffman codebook. |
michael@0 | 185 | _opb: The buffer to unpack from. |
michael@0 | 186 | _tokens: Stores a list of internal tokens, in the order they were found in |
michael@0 | 187 | the codebook, and the lengths of their corresponding codewords. |
michael@0 | 188 | This is enough to completely define the codebook, while minimizing |
michael@0 | 189 | stack usage and avoiding temporary allocations (for platforms |
michael@0 | 190 | where free() is a no-op). |
michael@0 | 191 | Return: The number of internal tokens in the codebook, or a negative value |
michael@0 | 192 | on error.*/ |
michael@0 | 193 | int oc_huff_tree_unpack(oc_pack_buf *_opb,unsigned char _tokens[256][2]){ |
michael@0 | 194 | ogg_uint32_t code; |
michael@0 | 195 | int len; |
michael@0 | 196 | int ntokens; |
michael@0 | 197 | int nleaves; |
michael@0 | 198 | code=0; |
michael@0 | 199 | len=ntokens=nleaves=0; |
michael@0 | 200 | for(;;){ |
michael@0 | 201 | long bits; |
michael@0 | 202 | bits=oc_pack_read1(_opb); |
michael@0 | 203 | /*Only process nodes so long as there's more bits in the buffer.*/ |
michael@0 | 204 | if(oc_pack_bytes_left(_opb)<0)return TH_EBADHEADER; |
michael@0 | 205 | /*Read an internal node:*/ |
michael@0 | 206 | if(!bits){ |
michael@0 | 207 | len++; |
michael@0 | 208 | /*Don't allow codewords longer than 32 bits.*/ |
michael@0 | 209 | if(len>32)return TH_EBADHEADER; |
michael@0 | 210 | } |
michael@0 | 211 | /*Read a leaf node:*/ |
michael@0 | 212 | else{ |
michael@0 | 213 | ogg_uint32_t code_bit; |
michael@0 | 214 | int neb; |
michael@0 | 215 | int nentries; |
michael@0 | 216 | int token; |
michael@0 | 217 | /*Don't allow more than 32 spec-tokens per codebook.*/ |
michael@0 | 218 | if(++nleaves>32)return TH_EBADHEADER; |
michael@0 | 219 | bits=oc_pack_read(_opb,OC_NDCT_TOKEN_BITS); |
michael@0 | 220 | neb=OC_DCT_TOKEN_MAP_LOG_NENTRIES[bits]; |
michael@0 | 221 | token=OC_DCT_TOKEN_MAP[bits]; |
michael@0 | 222 | nentries=1<<neb; |
michael@0 | 223 | while(nentries-->0){ |
michael@0 | 224 | _tokens[ntokens][0]=(unsigned char)token++; |
michael@0 | 225 | _tokens[ntokens][1]=(unsigned char)(len+neb); |
michael@0 | 226 | ntokens++; |
michael@0 | 227 | } |
michael@0 | 228 | code_bit=0x80000000U>>len-1; |
michael@0 | 229 | while(len>0&&(code&code_bit)){ |
michael@0 | 230 | code^=code_bit; |
michael@0 | 231 | code_bit<<=1; |
michael@0 | 232 | len--; |
michael@0 | 233 | } |
michael@0 | 234 | if(len<=0)break; |
michael@0 | 235 | code|=code_bit; |
michael@0 | 236 | } |
michael@0 | 237 | } |
michael@0 | 238 | return ntokens; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | /*Count how many tokens would be required to fill a subtree at depth _depth. |
michael@0 | 242 | _tokens: A list of internal tokens, in the order they are found in the |
michael@0 | 243 | codebook, and the lengths of their corresponding codewords. |
michael@0 | 244 | _depth: The depth of the desired node in the corresponding tree structure. |
michael@0 | 245 | Return: The number of tokens that belong to that subtree.*/ |
michael@0 | 246 | static int oc_huff_subtree_tokens(unsigned char _tokens[][2],int _depth){ |
michael@0 | 247 | ogg_uint32_t code; |
michael@0 | 248 | int ti; |
michael@0 | 249 | code=0; |
michael@0 | 250 | ti=0; |
michael@0 | 251 | do{ |
michael@0 | 252 | if(_tokens[ti][1]-_depth<32)code+=0x80000000U>>_tokens[ti++][1]-_depth; |
michael@0 | 253 | else{ |
michael@0 | 254 | /*Because of the expanded internal tokens, we can have codewords as long |
michael@0 | 255 | as 35 bits. |
michael@0 | 256 | A single recursion here is enough to advance past them.*/ |
michael@0 | 257 | code++; |
michael@0 | 258 | ti+=oc_huff_subtree_tokens(_tokens+ti,_depth+31); |
michael@0 | 259 | } |
michael@0 | 260 | } |
michael@0 | 261 | while(code<0x80000000U); |
michael@0 | 262 | return ti; |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | /*Compute the number of bits to use for a collapsed tree node at the given |
michael@0 | 266 | depth. |
michael@0 | 267 | _tokens: A list of internal tokens, in the order they are found in the |
michael@0 | 268 | codebook, and the lengths of their corresponding codewords. |
michael@0 | 269 | _ntokens: The number of tokens corresponding to this tree node. |
michael@0 | 270 | _depth: The depth of this tree node. |
michael@0 | 271 | Return: The number of bits to use for a collapsed tree node rooted here. |
michael@0 | 272 | This is always at least one, even if this was a leaf node.*/ |
michael@0 | 273 | static int oc_huff_tree_collapse_depth(unsigned char _tokens[][2], |
michael@0 | 274 | int _ntokens,int _depth){ |
michael@0 | 275 | int got_leaves; |
michael@0 | 276 | int loccupancy; |
michael@0 | 277 | int occupancy; |
michael@0 | 278 | int slush; |
michael@0 | 279 | int nbits; |
michael@0 | 280 | int best_nbits; |
michael@0 | 281 | slush=_depth>0?OC_HUFF_SLUSH:OC_ROOT_HUFF_SLUSH; |
michael@0 | 282 | /*It's legal to have a tree with just a single node, which requires no bits |
michael@0 | 283 | to decode and always returns the same token. |
michael@0 | 284 | However, no encoder actually does this (yet). |
michael@0 | 285 | To avoid a special case in oc_huff_token_decode(), we force the number of |
michael@0 | 286 | lookahead bits to be at least one. |
michael@0 | 287 | This will produce a tree that looks ahead one bit and then advances the |
michael@0 | 288 | stream zero bits.*/ |
michael@0 | 289 | nbits=1; |
michael@0 | 290 | occupancy=2; |
michael@0 | 291 | got_leaves=1; |
michael@0 | 292 | do{ |
michael@0 | 293 | int ti; |
michael@0 | 294 | if(got_leaves)best_nbits=nbits; |
michael@0 | 295 | nbits++; |
michael@0 | 296 | got_leaves=0; |
michael@0 | 297 | loccupancy=occupancy; |
michael@0 | 298 | for(occupancy=ti=0;ti<_ntokens;occupancy++){ |
michael@0 | 299 | if(_tokens[ti][1]<_depth+nbits)ti++; |
michael@0 | 300 | else if(_tokens[ti][1]==_depth+nbits){ |
michael@0 | 301 | got_leaves=1; |
michael@0 | 302 | ti++; |
michael@0 | 303 | } |
michael@0 | 304 | else ti+=oc_huff_subtree_tokens(_tokens+ti,_depth+nbits); |
michael@0 | 305 | } |
michael@0 | 306 | } |
michael@0 | 307 | while(occupancy>loccupancy&&occupancy*slush>=1<<nbits); |
michael@0 | 308 | return best_nbits; |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | /*Determines the size in words of a Huffman tree node that represents a |
michael@0 | 312 | subtree of depth _nbits. |
michael@0 | 313 | _nbits: The depth of the subtree. |
michael@0 | 314 | This must be greater than zero. |
michael@0 | 315 | Return: The number of words required to store the node.*/ |
michael@0 | 316 | static size_t oc_huff_node_size(int _nbits){ |
michael@0 | 317 | return 1+(1<<_nbits); |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | /*Produces a collapsed-tree representation of the given token list. |
michael@0 | 321 | _tree: The storage for the collapsed Huffman tree. |
michael@0 | 322 | This may be NULL to compute the required storage size instead of |
michael@0 | 323 | constructing the tree. |
michael@0 | 324 | _tokens: A list of internal tokens, in the order they are found in the |
michael@0 | 325 | codebook, and the lengths of their corresponding codewords. |
michael@0 | 326 | _ntokens: The number of tokens corresponding to this tree node. |
michael@0 | 327 | Return: The number of words required to store the tree.*/ |
michael@0 | 328 | #if defined(_MSC_VER) && _MSC_VER >= 1700 |
michael@0 | 329 | #pragma optimize( "", off ) |
michael@0 | 330 | #endif |
michael@0 | 331 | static size_t oc_huff_tree_collapse(ogg_int16_t *_tree, |
michael@0 | 332 | unsigned char _tokens[][2],int _ntokens){ |
michael@0 | 333 | ogg_int16_t node[34]; |
michael@0 | 334 | unsigned char depth[34]; |
michael@0 | 335 | unsigned char last[34]; |
michael@0 | 336 | size_t ntree; |
michael@0 | 337 | int ti; |
michael@0 | 338 | int l; |
michael@0 | 339 | depth[0]=0; |
michael@0 | 340 | last[0]=(unsigned char)(_ntokens-1); |
michael@0 | 341 | ntree=0; |
michael@0 | 342 | ti=0; |
michael@0 | 343 | l=0; |
michael@0 | 344 | do{ |
michael@0 | 345 | int nbits; |
michael@0 | 346 | nbits=oc_huff_tree_collapse_depth(_tokens+ti,last[l]+1-ti,depth[l]); |
michael@0 | 347 | node[l]=(ogg_int16_t)ntree; |
michael@0 | 348 | ntree+=oc_huff_node_size(nbits); |
michael@0 | 349 | if(_tree!=NULL)_tree[node[l]++]=(ogg_int16_t)nbits; |
michael@0 | 350 | do{ |
michael@0 | 351 | while(ti<=last[l]&&_tokens[ti][1]<=depth[l]+nbits){ |
michael@0 | 352 | if(_tree!=NULL){ |
michael@0 | 353 | ogg_int16_t leaf; |
michael@0 | 354 | int nentries; |
michael@0 | 355 | nentries=1<<depth[l]+nbits-_tokens[ti][1]; |
michael@0 | 356 | leaf=(ogg_int16_t)-(_tokens[ti][1]-depth[l]<<8|_tokens[ti][0]); |
michael@0 | 357 | while(nentries-->0)_tree[node[l]++]=leaf; |
michael@0 | 358 | } |
michael@0 | 359 | ti++; |
michael@0 | 360 | } |
michael@0 | 361 | if(ti<=last[l]){ |
michael@0 | 362 | /*We need to recurse*/ |
michael@0 | 363 | depth[l+1]=(unsigned char)(depth[l]+nbits); |
michael@0 | 364 | if(_tree!=NULL)_tree[node[l]++]=(ogg_int16_t)ntree; |
michael@0 | 365 | l++; |
michael@0 | 366 | last[l]= |
michael@0 | 367 | (unsigned char)(ti+oc_huff_subtree_tokens(_tokens+ti,depth[l])-1); |
michael@0 | 368 | break; |
michael@0 | 369 | } |
michael@0 | 370 | /*Pop back up a level of recursion.*/ |
michael@0 | 371 | else if(l-->0)nbits=depth[l+1]-depth[l]; |
michael@0 | 372 | } |
michael@0 | 373 | while(l>=0); |
michael@0 | 374 | } |
michael@0 | 375 | while(l>=0); |
michael@0 | 376 | return ntree; |
michael@0 | 377 | } |
michael@0 | 378 | #if defined(_MSC_VER) && _MSC_VER >= 1700 |
michael@0 | 379 | #pragma optimize( "", on ) |
michael@0 | 380 | #endif |
michael@0 | 381 | |
michael@0 | 382 | /*Unpacks a set of Huffman trees, and reduces them to a collapsed |
michael@0 | 383 | representation. |
michael@0 | 384 | _opb: The buffer to unpack the trees from. |
michael@0 | 385 | _nodes: The table to fill with the Huffman trees. |
michael@0 | 386 | Return: 0 on success, or a negative value on error. |
michael@0 | 387 | The caller is responsible for cleaning up any partially initialized |
michael@0 | 388 | _nodes on failure.*/ |
michael@0 | 389 | int oc_huff_trees_unpack(oc_pack_buf *_opb, |
michael@0 | 390 | ogg_int16_t *_nodes[TH_NHUFFMAN_TABLES]){ |
michael@0 | 391 | int i; |
michael@0 | 392 | for(i=0;i<TH_NHUFFMAN_TABLES;i++){ |
michael@0 | 393 | unsigned char tokens[256][2]; |
michael@0 | 394 | int ntokens; |
michael@0 | 395 | ogg_int16_t *tree; |
michael@0 | 396 | size_t size; |
michael@0 | 397 | /*Unpack the full tree into a temporary buffer.*/ |
michael@0 | 398 | ntokens=oc_huff_tree_unpack(_opb,tokens); |
michael@0 | 399 | if(ntokens<0)return ntokens; |
michael@0 | 400 | /*Figure out how big the collapsed tree will be and allocate space for it.*/ |
michael@0 | 401 | size=oc_huff_tree_collapse(NULL,tokens,ntokens); |
michael@0 | 402 | /*This should never happen; if it does it means you set OC_HUFF_SLUSH or |
michael@0 | 403 | OC_ROOT_HUFF_SLUSH too large.*/ |
michael@0 | 404 | if(size>32767)return TH_EIMPL; |
michael@0 | 405 | tree=(ogg_int16_t *)_ogg_malloc(size*sizeof(*tree)); |
michael@0 | 406 | if(tree==NULL)return TH_EFAULT; |
michael@0 | 407 | /*Construct the collapsed the tree.*/ |
michael@0 | 408 | oc_huff_tree_collapse(tree,tokens,ntokens); |
michael@0 | 409 | _nodes[i]=tree; |
michael@0 | 410 | } |
michael@0 | 411 | return 0; |
michael@0 | 412 | } |
michael@0 | 413 | |
michael@0 | 414 | /*Determines the size in words of a Huffman subtree. |
michael@0 | 415 | _tree: The complete Huffman tree. |
michael@0 | 416 | _node: The index of the root of the desired subtree. |
michael@0 | 417 | Return: The number of words required to store the tree.*/ |
michael@0 | 418 | static size_t oc_huff_tree_size(const ogg_int16_t *_tree,int _node){ |
michael@0 | 419 | size_t size; |
michael@0 | 420 | int nchildren; |
michael@0 | 421 | int n; |
michael@0 | 422 | int i; |
michael@0 | 423 | n=_tree[_node]; |
michael@0 | 424 | size=oc_huff_node_size(n); |
michael@0 | 425 | nchildren=1<<n; |
michael@0 | 426 | i=0; |
michael@0 | 427 | do{ |
michael@0 | 428 | int child; |
michael@0 | 429 | child=_tree[_node+i+1]; |
michael@0 | 430 | if(child<=0)i+=1<<n-(-child>>8); |
michael@0 | 431 | else{ |
michael@0 | 432 | size+=oc_huff_tree_size(_tree,child); |
michael@0 | 433 | i++; |
michael@0 | 434 | } |
michael@0 | 435 | } |
michael@0 | 436 | while(i<nchildren); |
michael@0 | 437 | return size; |
michael@0 | 438 | } |
michael@0 | 439 | |
michael@0 | 440 | /*Makes a copy of the given set of Huffman trees. |
michael@0 | 441 | _dst: The array to store the copy in. |
michael@0 | 442 | _src: The array of trees to copy.*/ |
michael@0 | 443 | int oc_huff_trees_copy(ogg_int16_t *_dst[TH_NHUFFMAN_TABLES], |
michael@0 | 444 | const ogg_int16_t *const _src[TH_NHUFFMAN_TABLES]){ |
michael@0 | 445 | int total; |
michael@0 | 446 | int i; |
michael@0 | 447 | total=0; |
michael@0 | 448 | for(i=0;i<TH_NHUFFMAN_TABLES;i++){ |
michael@0 | 449 | size_t size; |
michael@0 | 450 | size=oc_huff_tree_size(_src[i],0); |
michael@0 | 451 | total+=size; |
michael@0 | 452 | _dst[i]=(ogg_int16_t *)_ogg_malloc(size*sizeof(*_dst[i])); |
michael@0 | 453 | if(_dst[i]==NULL){ |
michael@0 | 454 | while(i-->0)_ogg_free(_dst[i]); |
michael@0 | 455 | return TH_EFAULT; |
michael@0 | 456 | } |
michael@0 | 457 | memcpy(_dst[i],_src[i],size*sizeof(*_dst[i])); |
michael@0 | 458 | } |
michael@0 | 459 | return 0; |
michael@0 | 460 | } |
michael@0 | 461 | |
michael@0 | 462 | /*Frees the memory used by a set of Huffman trees. |
michael@0 | 463 | _nodes: The array of trees to free.*/ |
michael@0 | 464 | void oc_huff_trees_clear(ogg_int16_t *_nodes[TH_NHUFFMAN_TABLES]){ |
michael@0 | 465 | int i; |
michael@0 | 466 | for(i=0;i<TH_NHUFFMAN_TABLES;i++)_ogg_free(_nodes[i]); |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | |
michael@0 | 470 | /*Unpacks a single token using the given Huffman tree. |
michael@0 | 471 | _opb: The buffer to unpack the token from. |
michael@0 | 472 | _node: The tree to unpack the token with. |
michael@0 | 473 | Return: The token value.*/ |
michael@0 | 474 | int oc_huff_token_decode_c(oc_pack_buf *_opb,const ogg_int16_t *_tree){ |
michael@0 | 475 | const unsigned char *ptr; |
michael@0 | 476 | const unsigned char *stop; |
michael@0 | 477 | oc_pb_window window; |
michael@0 | 478 | int available; |
michael@0 | 479 | long bits; |
michael@0 | 480 | int node; |
michael@0 | 481 | int n; |
michael@0 | 482 | ptr=_opb->ptr; |
michael@0 | 483 | window=_opb->window; |
michael@0 | 484 | stop=_opb->stop; |
michael@0 | 485 | available=_opb->bits; |
michael@0 | 486 | node=0; |
michael@0 | 487 | for(;;){ |
michael@0 | 488 | n=_tree[node]; |
michael@0 | 489 | if(n>available){ |
michael@0 | 490 | unsigned shift; |
michael@0 | 491 | shift=OC_PB_WINDOW_SIZE-available; |
michael@0 | 492 | do{ |
michael@0 | 493 | /*We don't bother setting eof because we won't check for it after we've |
michael@0 | 494 | started decoding DCT tokens.*/ |
michael@0 | 495 | if(ptr>=stop){ |
michael@0 | 496 | shift=(unsigned)-OC_LOTS_OF_BITS; |
michael@0 | 497 | break; |
michael@0 | 498 | } |
michael@0 | 499 | shift-=8; |
michael@0 | 500 | window|=(oc_pb_window)*ptr++<<shift; |
michael@0 | 501 | } |
michael@0 | 502 | while(shift>=8); |
michael@0 | 503 | /*Note: We never request more than 24 bits, so there's no need to fill in |
michael@0 | 504 | the last partial byte here.*/ |
michael@0 | 505 | available=OC_PB_WINDOW_SIZE-shift; |
michael@0 | 506 | } |
michael@0 | 507 | bits=window>>OC_PB_WINDOW_SIZE-n; |
michael@0 | 508 | node=_tree[node+1+bits]; |
michael@0 | 509 | if(node<=0)break; |
michael@0 | 510 | window<<=n; |
michael@0 | 511 | available-=n; |
michael@0 | 512 | } |
michael@0 | 513 | node=-node; |
michael@0 | 514 | n=node>>8; |
michael@0 | 515 | window<<=n; |
michael@0 | 516 | available-=n; |
michael@0 | 517 | _opb->ptr=ptr; |
michael@0 | 518 | _opb->window=window; |
michael@0 | 519 | _opb->bits=available; |
michael@0 | 520 | return node&255; |
michael@0 | 521 | } |