Skip to content

Commit

Permalink
Prettify HCB decoding
Browse files Browse the repository at this point in the history
Insipiered by #192. We do not actually need out-of bounds checks,
because Huffman tables are pretty well defined and proven to be
"self-contained", i.e. no input bit sequence can escape the table.
  • Loading branch information
eustas committed Dec 11, 2023
1 parent 216f00e commit cb9a92d
Show file tree
Hide file tree
Showing 14 changed files with 1,217 additions and 1,187 deletions.
14 changes: 3 additions & 11 deletions libfaad/codebook/hcb.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ extern "C" {
#define INTENSITY_HCB2 14
#define INTENSITY_HCB 15

/* Maximal accessed codebook index. */
#define LAST_CB_IDX 11

/* 1st step table */
typedef struct
{
Expand Down Expand Up @@ -115,16 +118,6 @@ typedef struct
int8_t data[2];
} hcb_bin_pair;

hcb *hcb_table[];
hcb_2_quad *hcb_2_quad_table[];
hcb_2_pair *hcb_2_pair_table[];
hcb_bin_pair *hcb_bin_table[];
uint8_t hcbN[];
uint8_t unsigned_cb[];
int hcb_2_quad_table_size[];
int hcb_2_pair_table_size[];
int hcb_bin_table_size[];

#include "codebook/hcb_1.h"
#include "codebook/hcb_2.h"
#include "codebook/hcb_3.h"
Expand All @@ -138,7 +131,6 @@ int hcb_bin_table_size[];
#include "codebook/hcb_11.h"
#include "codebook/hcb_sf.h"


#ifdef __cplusplus
}
#endif
Expand Down
9 changes: 7 additions & 2 deletions libfaad/codebook/hcb_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
*
* Used to find offset into 2nd step table and number of extra bits to get
*/
static hcb hcb1_1[] = {
static hcb hcb1_1[32] = {
/* 1 bit codeword */
{ /* 00000 */ 0, 0 },
{ /* */ 0, 0 },
{ /* */ 0, 0 },
Expand All @@ -53,6 +54,8 @@ static hcb hcb1_1[] = {
{ /* */ 0, 0 },
{ /* */ 0, 0 },
{ /* */ 0, 0 },

/* 5 bit codewords */
{ /* 10000 */ 1, 0 },
{ /* 10001 */ 2, 0 },
{ /* 10010 */ 3, 0 },
Expand All @@ -75,13 +78,15 @@ static hcb hcb1_1[] = {

/* 9/10/11 bit codewords */
{ /* 11111 */ 49, 6 }

/* Size of second level table is 49 + 64 = 113 */
};

/* 2nd step table
*
* Gives size of codeword and actual data (x,y,v,w)
*/
static hcb_2_quad hcb1_2[] = {
static hcb_2_quad hcb1_2[113] = {
/* 1 bit codeword */
{ 1, 0, 0, 0, 0 },

Expand Down
6 changes: 4 additions & 2 deletions libfaad/codebook/hcb_10.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* Used to find offset into 2nd step table and number of extra bits to get
*/
static hcb hcb10_1[] = {
static hcb hcb10_1[64] = {
/* 4 bit codewords */
{ /* 000000 */ 0, 0 },
{ /* */ 0, 0 },
Expand Down Expand Up @@ -112,13 +112,15 @@ static hcb hcb10_1[] = {
{ /* 111110 */ 129, 4 },
/* 10/11/12 bit codewords */
{ /* 111111 */ 145, 6 }

/* Size of second level table is 145 + 64 = 209 */
};

/* 2nd step table
*
* Gives size of codeword and actual data (x,y,v,w)
*/
static hcb_2_pair hcb10_2[] = {
static hcb_2_pair hcb10_2[209] = {
/* 4 bit codewords */
{ 4, 1, 1 },
{ 4, 1, 2 },
Expand Down
6 changes: 4 additions & 2 deletions libfaad/codebook/hcb_11.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* Used to find offset into 2nd step table and number of extra bits to get
*/
static hcb hcb11_1[] = {
static hcb hcb11_1[32] = {
/* 4 bits */
{ /* 00000 */ 0, 0 },
{ /* */ 0, 0 },
Expand Down Expand Up @@ -92,13 +92,15 @@ static hcb hcb11_1[] = {

/* 10/11/12 bits */
{ /* 11111 */ 246, 7 }

/* Size of second level table is 246 + 128 = 374 */
};

/* 2nd step table
*
* Gives size of codeword and actual data (x,y,v,w)
*/
static hcb_2_pair hcb11_2[] = {
static hcb_2_pair hcb11_2[374] = {
/* 4 */
{ 4, 0, 0 },
{ 4, 1, 1 },
Expand Down
15 changes: 11 additions & 4 deletions libfaad/codebook/hcb_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@
*
* Used to find offset into 2nd step table and number of extra bits to get
*/
static hcb hcb2_1[] = {
static hcb hcb2_1[32] = {
/* 3 bit codeword */
{ /* 00000 */ 0, 0 },
{ /* */ 0, 0 },
{ /* */ 0, 0 },
{ /* */ 0, 0 },
{ /* 00011 */ 0, 0 },

/* 4 bit codeword */
{ /* 00100 */ 1, 0 },
{ /* */ 1, 0 },
{ /* 00101 */ 1, 0 },

/* 5 bit codewords */
{ /* 00110 */ 2, 0 },
{ /* 00111 */ 3, 0 },
{ /* 01000 */ 4, 0 },
Expand Down Expand Up @@ -79,13 +84,15 @@ static hcb hcb2_1[] = {

/* 8/9 bit codewords */
{ /* 11111 */ 69, 4 }

/* Size of second level table is 69 + 16 = 85 */
};

/* 2nd step table
*
* Gives size of codeword and actual data (x,y,v,w)
*/
static hcb_2_quad hcb2_2[] = {
static hcb_2_quad hcb2_2[85] = {
/* 3 bit codeword */
{ 3, 0, 0, 0, 0 },

Expand Down
Loading

0 comments on commit cb9a92d

Please sign in to comment.