Path: blob/a-new-beginning/SharedDependencies/Sources/libchdr/include/huffman.h
2 views
/* license:BSD-3-Clause1* copyright-holders:Aaron Giles2***************************************************************************34huffman.h56Static Huffman compression and decompression helpers.78***************************************************************************/910#pragma once1112#ifndef __HUFFMAN_H__13#define __HUFFMAN_H__1415#include "bitstream.h"161718/***************************************************************************19* CONSTANTS20***************************************************************************21*/2223enum huffman_error24{25HUFFERR_NONE = 0,26HUFFERR_TOO_MANY_BITS,27HUFFERR_INVALID_DATA,28HUFFERR_INPUT_BUFFER_TOO_SMALL,29HUFFERR_OUTPUT_BUFFER_TOO_SMALL,30HUFFERR_INTERNAL_INCONSISTENCY,31HUFFERR_TOO_MANY_CONTEXTS32};3334/***************************************************************************35* TYPE DEFINITIONS36***************************************************************************37*/3839typedef uint16_t lookup_value;4041/* a node in the huffman tree */42struct node_t43{44struct node_t* parent; /* pointer to parent node */45uint32_t count; /* number of hits on this node */46uint32_t weight; /* assigned weight of this node */47uint32_t bits; /* bits used to encode the node */48uint8_t numbits; /* number of bits needed for this node */49};5051/* ======================> huffman_context_base */5253/* context class for decoding */54struct huffman_decoder55{56/* internal state */57uint32_t numcodes; /* number of total codes being processed */58uint8_t maxbits; /* maximum bits per code */59uint8_t prevdata; /* value of the previous data (for delta-RLE encoding) */60int rleremaining; /* number of RLE bytes remaining (for delta-RLE encoding) */61lookup_value * lookup; /* pointer to the lookup table */62struct node_t * huffnode; /* array of nodes */63uint32_t * datahisto; /* histogram of data values */6465/* array versions of the info we need */66#if 067node_t* huffnode_array; /* [_NumCodes]; */68lookup_value* lookup_array; /* [1 << _MaxBits]; */69#endif70};7172/* ======================> huffman_decoder */7374struct huffman_decoder* create_huffman_decoder(int numcodes, int maxbits);7576/* single item operations */77uint32_t huffman_decode_one(struct huffman_decoder* decoder, struct bitstream* bitbuf);7879enum huffman_error huffman_import_tree_rle(struct huffman_decoder* decoder, struct bitstream* bitbuf);80enum huffman_error huffman_import_tree_huffman(struct huffman_decoder* decoder, struct bitstream* bitbuf);8182int huffman_build_tree(struct huffman_decoder* decoder, uint32_t totaldata, uint32_t totalweight);83enum huffman_error huffman_assign_canonical_codes(struct huffman_decoder* decoder);84enum huffman_error huffman_compute_tree_from_histo(struct huffman_decoder* decoder);8586void huffman_build_lookup_table(struct huffman_decoder* decoder);8788#endif899091