2 * Copyright (C) 2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY;https://www.hsr.ch/HSR-intern-Anmeldung.4409.0.html?&no_cache=1 without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "bliss_huffman_coder.h"
18 typedef struct private_bliss_huffman_coder_t private_bliss_huffman_coder_t
;
21 * Private data structure for bliss_huffman_coder_t object
23 struct private_bliss_huffman_coder_t
{
27 bliss_huffman_coder_t
public;
30 * Bitpacker to write to or read from
32 bliss_bitpacker_t
*packer
;
35 * Huffman code table to be used
37 bliss_huffman_code_t
*code
;
40 * Maximum index into tuples table
45 * Number of encoded or decoded bits
51 METHOD(bliss_huffman_coder_t
, get_bits
, size_t,
52 private_bliss_huffman_coder_t
*this)
57 METHOD(bliss_huffman_coder_t
, encode
, bool,
58 private_bliss_huffman_coder_t
*this, int32_t z1
, int16_t z2
)
64 index
= z1
* (2*this->code
->n_z2
- 1) + z2
+ this->code
->n_z2
- 1;
65 if (index
>= this->index_max
)
67 DBG1(DBG_LIB
, "index exceeded in Huffman encoding table");
70 code
= this->code
->tuples
[index
].code
;
71 bits
= this->code
->tuples
[index
].bits
;
72 if (!this->packer
->write_bits(this->packer
, code
, bits
))
74 DBG1(DBG_LIB
, "bitpacker exceeded its buffer");
82 METHOD(bliss_huffman_coder_t
, decode
, bool,
83 private_bliss_huffman_coder_t
*this, int32_t *z1
, int16_t *z2
)
85 bliss_huffman_code_node_t
*node
;
88 node
= this->code
->nodes
;
89 while (node
->tuple
== BLISS_HUFFMAN_CODE_NO_TUPLE
)
91 if (node
->node_0
== BLISS_HUFFMAN_CODE_NO_NODE
||
92 node
->node_1
== BLISS_HUFFMAN_CODE_NO_NODE
)
94 DBG1(DBG_LIB
, "error in Huffman decoding table");
97 if (!this->packer
->read_bits(this->packer
, &bit
, 1))
99 DBG1(DBG_LIB
, "bitpacker depleted its buffer");
102 node
= &this->code
->nodes
[bit
? node
->node_1
: node
->node_0
];
105 *z1
= node
->tuple
/ (2*this->code
->n_z2
- 1);
106 *z2
= node
->tuple
- (2*this->code
->n_z2
- 1) * (*z1
) - this->code
->n_z2
+ 1;
111 METHOD(bliss_huffman_coder_t
, destroy
, void,
112 private_bliss_huffman_coder_t
*this)
120 bliss_huffman_coder_t
*bliss_huffman_coder_create(bliss_huffman_code_t
*code
,
121 bliss_bitpacker_t
*packer
)
123 private_bliss_huffman_coder_t
*this;
127 .get_bits
= _get_bits
,
134 .index_max
= (2*code
->n_z2
- 1) * code
->n_z1
,
137 return &this->public;