Path: blob/master/dep/libchdr/src/libchdr_huffman.c
4251 views
/* license:BSD-3-Clause1* copyright-holders:Aaron Giles2****************************************************************************34huffman.c56Static Huffman compression and decompression helpers.78****************************************************************************910Maximum codelength is officially (alphabetsize - 1). This would be 255 bits11(since we use 1 byte values). However, it is also dependent upon the number12of samples used, as follows:13142 bits -> 3..4 samples153 bits -> 5..7 samples164 bits -> 8..12 samples175 bits -> 13..20 samples186 bits -> 21..33 samples197 bits -> 34..54 samples208 bits -> 55..88 samples219 bits -> 89..143 samples2210 bits -> 144..232 samples2311 bits -> 233..376 samples2412 bits -> 377..609 samples2513 bits -> 610..986 samples2614 bits -> 987..1596 samples2715 bits -> 1597..2583 samples2816 bits -> 2584..4180 samples -> note that a 4k data size guarantees codelength <= 16 bits2917 bits -> 4181..6764 samples3018 bits -> 6765..10945 samples3119 bits -> 10946..17710 samples3220 bits -> 17711..28656 samples3321 bits -> 28657..46367 samples3422 bits -> 46368..75024 samples3523 bits -> 75025..121392 samples3624 bits -> 121393..196417 samples3725 bits -> 196418..317810 samples3826 bits -> 317811..514228 samples3927 bits -> 514229..832039 samples4028 bits -> 832040..1346268 samples4129 bits -> 1346269..2178308 samples4230 bits -> 2178309..3524577 samples4331 bits -> 3524578..5702886 samples4432 bits -> 5702887..9227464 samples4546Looking at it differently, here is where powers of 2 fall into these buckets:4748256 samples -> 11 bits max49512 samples -> 12 bits max501k samples -> 14 bits max512k samples -> 15 bits max524k samples -> 16 bits max538k samples -> 18 bits max5416k samples -> 19 bits max5532k samples -> 21 bits max5664k samples -> 22 bits max57128k samples -> 24 bits max58256k samples -> 25 bits max59512k samples -> 27 bits max601M samples -> 28 bits max612M samples -> 29 bits max624M samples -> 31 bits max638M samples -> 32 bits max6465****************************************************************************6667Delta-RLE encoding works as follows:6869Starting value is assumed to be 0. All data is encoded as a delta70from the previous value, such that final[i] = final[i - 1] + delta.71Long runs of 0s are RLE-encoded as follows:72730x100 = repeat count of 8740x101 = repeat count of 9750x102 = repeat count of 10760x103 = repeat count of 11770x104 = repeat count of 12780x105 = repeat count of 13790x106 = repeat count of 14800x107 = repeat count of 15810x108 = repeat count of 16820x109 = repeat count of 32830x10a = repeat count of 64840x10b = repeat count of 128850x10c = repeat count of 256860x10d = repeat count of 512870x10e = repeat count of 1024880x10f = repeat count of 20488990Note that repeat counts are reset at the end of a row, so if a 0 run91extends to the end of a row, a large repeat count may be used.9293The reason for starting the run counts at 8 is that 0 is expected to94be the most common symbol, and is typically encoded in 1 or 2 bits.9596***************************************************************************/9798#include <stdlib.h>99#include <stdio.h>100#include <string.h>101102#include <libchdr/huffman.h>103104#define MAX(x,y) ((x) > (y) ? (x) : (y))105106/***************************************************************************107* MACROS108***************************************************************************109*/110111#define MAKE_LOOKUP(code,bits) (((code) << 5) | ((bits) & 0x1f))112113/***************************************************************************114* IMPLEMENTATION115***************************************************************************116*/117118/*-------------------------------------------------119* huffman_context_base - create an encoding/120* decoding context121*-------------------------------------------------122*/123124struct huffman_decoder* create_huffman_decoder(int numcodes, int maxbits)125{126struct huffman_decoder* decoder = NULL;127128/* limit to 24 bits */129if (maxbits > 24)130return NULL;131132decoder = (struct huffman_decoder*)malloc(sizeof(struct huffman_decoder));133decoder->numcodes = numcodes;134decoder->maxbits = maxbits;135decoder->lookup = (lookup_value*)malloc(sizeof(lookup_value) * (1 << maxbits));136decoder->huffnode = (struct node_t*)malloc(sizeof(struct node_t) * numcodes);137decoder->datahisto = NULL;138decoder->prevdata = 0;139decoder->rleremaining = 0;140return decoder;141}142143void delete_huffman_decoder(struct huffman_decoder* decoder)144{145if (decoder != NULL)146{147if (decoder->lookup != NULL)148free(decoder->lookup);149if (decoder->huffnode != NULL)150free(decoder->huffnode);151free(decoder);152}153}154155/*-------------------------------------------------156* decode_one - decode a single code from the157* huffman stream158*-------------------------------------------------159*/160161uint32_t huffman_decode_one(struct huffman_decoder* decoder, struct bitstream* bitbuf)162{163/* peek ahead to get maxbits worth of data */164uint32_t bits = bitstream_peek(bitbuf, decoder->maxbits);165166/* look it up, then remove the actual number of bits for this code */167lookup_value lookup = decoder->lookup[bits];168bitstream_remove(bitbuf, lookup & 0x1f);169170/* return the value */171return lookup >> 5;172}173174/*-------------------------------------------------175* import_tree_rle - import an RLE-encoded176* huffman tree from a source data stream177*-------------------------------------------------178*/179180enum huffman_error huffman_import_tree_rle(struct huffman_decoder* decoder, struct bitstream* bitbuf)181{182int numbits;183uint32_t curnode;184enum huffman_error error;185186/* bits per entry depends on the maxbits */187if (decoder->maxbits >= 16)188numbits = 5;189else if (decoder->maxbits >= 8)190numbits = 4;191else192numbits = 3;193194/* loop until we read all the nodes */195for (curnode = 0; curnode < decoder->numcodes; )196{197/* a non-one value is just raw */198int nodebits = bitstream_read(bitbuf, numbits);199if (nodebits != 1)200decoder->huffnode[curnode++].numbits = nodebits;201202/* a one value is an escape code */203else204{205/* a double 1 is just a single 1 */206nodebits = bitstream_read(bitbuf, numbits);207if (nodebits == 1)208decoder->huffnode[curnode++].numbits = nodebits;209210/* otherwise, we need one for value for the repeat count */211else212{213int repcount = bitstream_read(bitbuf, numbits) + 3;214if (repcount + curnode > decoder->numcodes)215return HUFFERR_INVALID_DATA;216while (repcount--)217decoder->huffnode[curnode++].numbits = nodebits;218}219}220}221222/* make sure we ended up with the right number */223if (curnode != decoder->numcodes)224return HUFFERR_INVALID_DATA;225226/* assign canonical codes for all nodes based on their code lengths */227error = huffman_assign_canonical_codes(decoder);228if (error != HUFFERR_NONE)229return error;230231/* build the lookup table */232error = huffman_build_lookup_table(decoder);233if (error != HUFFERR_NONE)234return error;235236/* determine final input length and report errors */237return bitstream_overflow(bitbuf) ? HUFFERR_INPUT_BUFFER_TOO_SMALL : HUFFERR_NONE;238}239240241/*-------------------------------------------------242* import_tree_huffman - import a huffman-encoded243* huffman tree from a source data stream244*-------------------------------------------------245*/246247enum huffman_error huffman_import_tree_huffman(struct huffman_decoder* decoder, struct bitstream* bitbuf)248{249int start;250int last = 0;251int count = 0;252int index;253uint32_t curcode;254uint8_t rlefullbits = 0;255uint32_t temp;256enum huffman_error error;257/* start by parsing the lengths for the small tree */258struct huffman_decoder* smallhuff = create_huffman_decoder(24, 6);259smallhuff->huffnode[0].numbits = bitstream_read(bitbuf, 3);260start = bitstream_read(bitbuf, 3) + 1;261for (index = 1; index < 24; index++)262{263if (index < start || count == 7)264smallhuff->huffnode[index].numbits = 0;265else266{267count = bitstream_read(bitbuf, 3);268smallhuff->huffnode[index].numbits = (count == 7) ? 0 : count;269}270}271272/* then regenerate the tree */273error = huffman_assign_canonical_codes(smallhuff);274if (error != HUFFERR_NONE)275{276delete_huffman_decoder(smallhuff);277return error;278}279error = huffman_build_lookup_table(smallhuff);280if (error != HUFFERR_NONE)281{282delete_huffman_decoder(smallhuff);283return error;284}285286/* determine the maximum length of an RLE count */287temp = decoder->numcodes - 9;288while (temp != 0)289temp >>= 1, rlefullbits++;290291/* now process the rest of the data */292for (curcode = 0; curcode < decoder->numcodes; )293{294int value = huffman_decode_one(smallhuff, bitbuf);295if (value != 0)296decoder->huffnode[curcode++].numbits = last = value - 1;297else298{299int count = bitstream_read(bitbuf, 3) + 2;300if (count == 7+2)301count += bitstream_read(bitbuf, rlefullbits);302for ( ; count != 0 && curcode < decoder->numcodes; count--)303decoder->huffnode[curcode++].numbits = last;304}305}306307/* make sure we free the local huffman decoder */308delete_huffman_decoder(smallhuff);309310/* make sure we ended up with the right number */311if (curcode != decoder->numcodes)312return HUFFERR_INVALID_DATA;313314/* assign canonical codes for all nodes based on their code lengths */315error = huffman_assign_canonical_codes(decoder);316if (error != HUFFERR_NONE)317return error;318319/* build the lookup table */320error = huffman_build_lookup_table(decoder);321if (error != HUFFERR_NONE)322return error;323324/* determine final input length and report errors */325return bitstream_overflow(bitbuf) ? HUFFERR_INPUT_BUFFER_TOO_SMALL : HUFFERR_NONE;326}327328/*-------------------------------------------------329* compute_tree_from_histo - common backend for330* computing a tree based on the data histogram331*-------------------------------------------------332*/333334enum huffman_error huffman_compute_tree_from_histo(struct huffman_decoder* decoder)335{336uint32_t i;337uint32_t lowerweight;338uint32_t upperweight;339/* compute the number of data items in the histogram */340uint32_t sdatacount = 0;341for (i = 0; i < decoder->numcodes; i++)342sdatacount += decoder->datahisto[i];343344/* binary search to achieve the optimum encoding */345lowerweight = 0;346upperweight = sdatacount * 2;347while (1)348{349/* build a tree using the current weight */350uint32_t curweight = (upperweight + lowerweight) / 2;351int curmaxbits = huffman_build_tree(decoder, sdatacount, curweight);352353/* apply binary search here */354if (curmaxbits <= decoder->maxbits)355{356lowerweight = curweight;357358/* early out if it worked with the raw weights, or if we're done searching */359if (curweight == sdatacount || (upperweight - lowerweight) <= 1)360break;361}362else363upperweight = curweight;364}365366/* assign canonical codes for all nodes based on their code lengths */367return huffman_assign_canonical_codes(decoder);368}369370/***************************************************************************371* INTERNAL FUNCTIONS372***************************************************************************373*/374375/*-------------------------------------------------376* tree_node_compare - compare two tree nodes377* by weight378*-------------------------------------------------379*/380381static int huffman_tree_node_compare(const void *item1, const void *item2)382{383const struct node_t *node1 = *(const struct node_t **)item1;384const struct node_t *node2 = *(const struct node_t **)item2;385if (node2->weight != node1->weight)386return node2->weight - node1->weight;387if (node2->bits - node1->bits == 0)388fprintf(stderr, "identical node sort keys, should not happen!\n");389return (int)node1->bits - (int)node2->bits;390}391392/*-------------------------------------------------393* build_tree - build a huffman tree based on the394* data distribution395*-------------------------------------------------396*/397398int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint32_t totalweight)399{400uint32_t curcode;401int nextalloc;402int listitems = 0;403int maxbits = 0;404/* make a list of all non-zero nodes */405struct node_t** list = (struct node_t**)malloc(sizeof(struct node_t*) * decoder->numcodes * 2);406memset(decoder->huffnode, 0, decoder->numcodes * sizeof(decoder->huffnode[0]));407for (curcode = 0; curcode < decoder->numcodes; curcode++)408if (decoder->datahisto[curcode] != 0)409{410list[listitems++] = &decoder->huffnode[curcode];411decoder->huffnode[curcode].count = decoder->datahisto[curcode];412decoder->huffnode[curcode].bits = curcode;413414/* scale the weight by the current effective length, ensuring we don't go to 0 */415decoder->huffnode[curcode].weight = ((uint64_t)decoder->datahisto[curcode]) * ((uint64_t)totalweight) / ((uint64_t)totaldata);416if (decoder->huffnode[curcode].weight == 0)417decoder->huffnode[curcode].weight = 1;418}419420#if 0421fprintf(stderr, "Pre-sort:\n");422for (int i = 0; i < listitems; i++) {423fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits);424}425#endif426427/* sort the list by weight, largest weight first */428qsort(&list[0], listitems, sizeof(list[0]), huffman_tree_node_compare);429430#if 0431fprintf(stderr, "Post-sort:\n");432for (int i = 0; i < listitems; i++) {433fprintf(stderr, "weight: %d code: %d\n", list[i]->m_weight, list[i]->m_bits);434}435fprintf(stderr, "===================\n");436#endif437438/* now build the tree */439nextalloc = decoder->numcodes;440while (listitems > 1)441{442int curitem;443/* remove lowest two items */444struct node_t* node1 = &(*list[--listitems]);445struct node_t* node0 = &(*list[--listitems]);446447/* create new node */448struct node_t* newnode = &decoder->huffnode[nextalloc++];449newnode->parent = NULL;450node0->parent = node1->parent = newnode;451newnode->weight = node0->weight + node1->weight;452453/* insert into list at appropriate location */454for (curitem = 0; curitem < listitems; curitem++)455if (newnode->weight > list[curitem]->weight)456{457memmove(&list[curitem+1], &list[curitem], (listitems - curitem) * sizeof(list[0]));458break;459}460list[curitem] = newnode;461listitems++;462}463464/* compute the number of bits in each code, and fill in another histogram */465for (curcode = 0; curcode < decoder->numcodes; curcode++)466{467struct node_t *curnode;468struct node_t* node = &decoder->huffnode[curcode];469node->numbits = 0;470node->bits = 0;471472/* if we have a non-zero weight, compute the number of bits */473if (node->weight > 0)474{475/* determine the number of bits for this node */476for (curnode = node; curnode->parent != NULL; curnode = curnode->parent)477node->numbits++;478if (node->numbits == 0)479node->numbits = 1;480481/* keep track of the max */482maxbits = MAX(maxbits, ((int)node->numbits));483}484}485return maxbits;486}487488/*-------------------------------------------------489* assign_canonical_codes - assign canonical codes490* to all the nodes based on the number of bits491* in each492*-------------------------------------------------493*/494495enum huffman_error huffman_assign_canonical_codes(struct huffman_decoder* decoder)496{497uint32_t curcode;498int codelen;499uint32_t curstart = 0;500/* build up a histogram of bit lengths */501uint32_t bithisto[33] = { 0 };502for (curcode = 0; curcode < decoder->numcodes; curcode++)503{504struct node_t* node = &decoder->huffnode[curcode];505if (node->numbits > decoder->maxbits)506return HUFFERR_INTERNAL_INCONSISTENCY;507if (node->numbits <= 32)508bithisto[node->numbits]++;509}510511/* for each code length, determine the starting code number */512for (codelen = 32; codelen > 0; codelen--)513{514uint32_t nextstart = (curstart + bithisto[codelen]) >> 1;515if (codelen != 1 && nextstart * 2 != (curstart + bithisto[codelen]))516return HUFFERR_INTERNAL_INCONSISTENCY;517bithisto[codelen] = curstart;518curstart = nextstart;519}520521/* now assign canonical codes */522for (curcode = 0; curcode < decoder->numcodes; curcode++)523{524struct node_t* node = &decoder->huffnode[curcode];525if (node->numbits > 0)526node->bits = bithisto[node->numbits]++;527}528return HUFFERR_NONE;529}530531/*-------------------------------------------------532* build_lookup_table - build a lookup table for533* fast decoding534*-------------------------------------------------535*/536537enum huffman_error huffman_build_lookup_table(struct huffman_decoder* decoder)538{539const lookup_value* lookupend = &decoder->lookup[(1u << decoder->maxbits)];540uint32_t curcode;541/* iterate over all codes */542for (curcode = 0; curcode < decoder->numcodes; curcode++)543{544/* process all nodes which have non-zero bits */545struct node_t* node = &decoder->huffnode[curcode];546if (node->numbits > 0)547{548int shift;549lookup_value *dest;550lookup_value *destend;551552/* set up the entry */553lookup_value value = MAKE_LOOKUP(curcode, node->numbits);554555/* fill all matching entries */556shift = decoder->maxbits - node->numbits;557dest = &decoder->lookup[node->bits << shift];558destend = &decoder->lookup[((node->bits + 1) << shift) - 1];559if (dest >= lookupend || destend >= lookupend || destend < dest)560return HUFFERR_INTERNAL_INCONSISTENCY;561while (dest <= destend)562*dest++ = value;563}564}565566return HUFFERR_NONE;567}568569570