Skip to content

Commit

Permalink
roll szip back to original code again. compile bonks on global model …
Browse files Browse the repository at this point in the history
…macros as usual
  • Loading branch information
bgates747 committed Feb 9, 2025
1 parent 11a2bb2 commit b25a1c1
Show file tree
Hide file tree
Showing 16 changed files with 2,261 additions and 1,104 deletions.
391 changes: 155 additions & 236 deletions video/compression_szip.h

Large diffs are not rendered by default.

198 changes: 129 additions & 69 deletions video/szip/bitmodel.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
bitmodel implements bit indexed trees for frequency storage described
Bitmodel implements bit indexed trees for frequency storage described
by Peter Fenwick: A New Data Structure for Cumulative Probability Tables
Technical Report 88, Dep. of Computer Science, University of Auckland, NZ.
It features a fast method for cumulative frequency storage and updating.
Expand All @@ -43,131 +43,191 @@
*/

#include "bitmodel.h"
#include <stdio.h>
#include <stdlib.h>
#include "esp_heap_caps.h"
#include <stdio.h> /* NULL */
#include <stdlib.h> /* malloc, free */

static inline void build_cf(bitmodel *m) {
int i;
uint16_t *cf = m->cf;

/* constructs the b_i_t structire */
static Inline void build_cf(bitmodel *m)
{ int i;
uint2 *cf;
cf = m->cf;
m->totalfreq = 0;
for (i = 1; i <= m->n; i <<= 1) {
for (int j = i; j <= m->n; j += i << 1) {
int k;
for (i=1; i<=m->n; i<<=1)
{ int j;
for (j=i; j<=m->n; j+= i<<1)
{ int k;
#ifdef EXCLUDEONUPDATE
if (m->f[j - 1] & 0x8000)
if (m->f[j-1] & 0x8000)
cf[j] = 0;
else
#endif
m->totalfreq += (cf[j] = m->f[j - 1]);
for (k = i >> 1; k; k >>= 1)
cf[j] += cf[j - k];
}
}
m->totalfreq += cf[j] = m->f[j-1];
for (k=i>>1; k; k>>=1)
cf[j] += cf[j-k];
} /* end for j */
} /* end for i */
}

static void scale_freq(bitmodel *m) {
for (uint16_t *f = m->f, *endf = f + m->n; f < endf; f++)

/* scales the culmulative frequency tables by 0.5 and keeps nonzero values */
static void scalefreq(bitmodel *m)
{ uint2 *f, *endf;
for (f=m->f, endf = f+m->n; f<endf; f++)
#ifdef EXCLUDEONUPDATE
*f = ((1 + (*f & 0x7FFF)) >> 1) | (*f & 0x8000);
*f = ((1+(*f & 0x7fff))>>1) | (*f & 0x8000);
#else
*f = (1 + *f) >> 1;
*f = (1 + *f)>>1;
#endif
build_cf(m);
}

void initbitmodel(bitmodel *m, int n, int max_totf, int rescale, int *init) {
m->n = n;
if (max_totf < n << 1) max_totf = n << 1;

/* initialisation of bitmodel */
/* m bitmodel to be initialized */
/* n number of symbols in that model */
/* max_totf maximum allowed total frequency count */
/* rescale desired rescaling interval, must be <max_totf/2 */
/* init array of int's to be used for initialisation (NULL ok) */
void initbitmodel( bitmodel *m, int n, int max_totf, int rescale,
int *init )
{ m->n = n;
if (max_totf < n<<1) max_totf = n<<1;
m->max_totf = max_totf;
m->incr = max_totf / 2 / rescale;
m->incr = max_totf/2/rescale;
if (m->incr < 1) m->incr = 1;
m->f = (uint16_t *)heap_caps_malloc(n * sizeof(uint16_t), MALLOC_CAP_8BIT);
m->cf = (uint16_t *)heap_caps_malloc((n + 1) * sizeof(uint16_t), MALLOC_CAP_8BIT);
m->f = (uint2*) malloc(n*sizeof(uint2));
m->cf = (uint2*) malloc((n+1)*sizeof(uint2));
m->mask = 1;
while (n >>= 1)
m->mask <<= 1;
resetbitmodel(m, init);
while (n>>=1)
m->mask <<=1;
resetbitmodel(m,init);
}

void resetbitmodel(bitmodel *m, int *init) {
if (init == NULL) {
for (int i = 0; i < m->n; i++)


/* reinitialisation of bitmodel */
/* m bitmodel to be initialized */
/* init array of int's to be used for initialisation (NULL ok) */
void resetbitmodel( bitmodel *m, int *init)
{ int i;
if (init == NULL)
{ for(i=0; i<m->n; i++)
m->f[i] = 1;
m->totalfreq = m->n;
} else {
m->totalfreq = 0;
for (int i = 0; i < m->n; i++) {
m->f[i] = init[i];
} else
{ m->totalfreq = 0;
for(i=0; i<m->n; i++)
{ m->f[i] = init[i];
m->totalfreq += init[i];
}
}
while (m->totalfreq > m->max_totf)
scale_freq(m);
scalefreq(m);
build_cf(m);
}

void deletebitmodel(bitmodel *m) {
free(m->f);

/* deletion of bitmodel m */
void deletebitmodel( bitmodel *m )
{ free(m->f);
free(m->cf);
m->n = 0;
}

void bitgetfreq(bitmodel *m, int sym, int *sy_f, int *lt_f) {
int cul;
uint16_t *cf = m->cf;

/* retrieval of estimated frequencies for a symbol */
/* m bitmodel to be questioned */
/* sym symbol for which data is desired; must be <n */
/* sy_f frequency of that symbol */
/* lt_f frequency of all smaller symbols together */
/* the total frequency can be obtained with bit_totf */
void bitgetfreq( bitmodel *m, int sym, int *sy_f, int *lt_f)
{ int cul;
uint2 *cf;
*sy_f = m->f[sym];
sym++;
cf = m->cf;
cul = cf[sym];
while (sym &= sym - 1)
while (sym &= sym-1)
cul += cf[sym];
*lt_f = cul - *sy_f;
}

int bitgetsym(bitmodel *m, int lt_f) {
int sym = 0, mask = m->mask, n = m->n;
uint16_t *cf = m->cf;
do {
int x;
if ((x = sym | mask) <= n && lt_f >= cf[x]) {
lt_f -= cf[x];

/* find out symbol for a given cumulative frequency */
/* m bitmodel to be questioned */
/* lt_f cumulative frequency */
int bitgetsym( bitmodel *m, int lt_f )
{ int sym, mask, n;
uint2 *cf;
mask = m->mask;
n = m->n;
cf = m->cf;
sym = 0;
do
{ int x;
if ((x=sym|mask) <= n && lt_f >= cf[x])
{ lt_f -= cf[x];
sym = x;
}
} while (mask >>= 1);
return sym;
}

void bitupdate(bitmodel *m, int sym) {
m->f[sym] += m->incr;
m->totalfreq += m->incr;

/* update the cumulative frequency data by delta */
static Inline void bit_cfupd( bitmodel *m, int sym, int delta )
{ m->totalfreq += delta;
if (m->totalfreq > m->max_totf)
scale_freq(m);
else {
uint16_t *cf = m->cf;
scalefreq(m);
else
{ uint2 *cf;
sym++;
while (sym <= m->n) {
cf[sym] += m->incr;
sym = (sym | (sym - 1)) + 1;
cf = m->cf;
while (sym<= m->n)
{ cf[sym] += delta;
sym = (sym | (sym-1)) + 1;
}
}
}


/* update model */
/* m bitmodel to be updated */
/* sym symbol that occurred (must be <n from init) */
void bitupdate( bitmodel *m, int sym )
{ m->f[sym] += m->incr;
bit_cfupd(m, sym, m->incr);
}


#ifdef EXCLUDEONUPDATE
void bitupdate_ex(bitmodel *m, int sym) {
int delta = -m->f[sym];
/* update model and exclude symbol */
/* m bitmodel to be updated */
/* sym symbol that occurred (must be <n from init) */
void bitupdate_ex( bitmodel *m, int sym )
{ int delta;
delta = -m->f[sym];
m->f[sym] = (m->f[sym] + m->incr) | 0x8000;
m->totalfreq += delta;
if (m->totalfreq > m->max_totf)
scale_freq(m);
bit_cfupd(m, sym, delta);
}

void bitdeactivate(bitmodel *m, int sym) {
m->totalfreq -= m->f[sym];

/* deactivate symbol */
/* m bitmodel to be updated */
/* sym symbol to be reactivated */
void bitdeactivate( bitmodel *m, int sym )
{ bit_cfupd(m, sym, -m->f[sym]);
m->f[sym] |= 0x8000;
}

void bitreactivate(bitmodel *m, int sym) {
m->f[sym] &= 0x7FFF;
m->totalfreq += m->f[sym];

/* reactivate symbol */
/* m bitmodel to be updated */
/* sym symbol to be reactivated */
void bitreactivate( bitmodel *m, int sym )
{ m->f[sym] &= 0x7fff;
bit_cfupd(m, sym, m->f[sym]);
}
#endif
93 changes: 56 additions & 37 deletions video/szip/bitmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
bitmodel implements bit indexed trees for frequency storage described
Bitmodel implements bit indexed trees for frequency storage described
by Peter Fenwick: A New Data Structure for Cumulative Probability Tables
Technical Report 88, Dep. of Computer Science, University of Auckland, NZ.
It features a fast method for cumulative frequency storage and updating.
Expand All @@ -46,59 +46,78 @@
*/

#include "port.h"
#include <stdint.h>
#include <esp_heap_caps.h>

#define EXCLUDEONUPDATE

typedef struct {
int n; /* Number of symbols */
int totalfreq; /* Total frequency count (without excluded symbols) */
int max_totf; /* Maximum allowed total frequency count */
int incr; /* Increment per update */
int mask; /* Initial bitmask used for search */
uint16_t *f; /* Frequency for the symbol; first bit set if excluded */
uint16_t *cf; /* Array of cumulative frequencies */
int n, /* number of symbols */
totalfreq, /* total frequency count (without excluded symbols) */
max_totf, /* maximum allowed total frequency count */
incr, /* increment per update */
mask; /* initial bitmask used for search */
uint2 *f, /* frequency for the symbol; first bit set if excluded */
*cf; /* array of cumulative frequencies */
} bitmodel;

#ifdef __cplusplus
extern "C" {
#endif
/* initialisation of bitmodel */
/* m bitmodel to be initialized */
/* n number of symbols in that model */
/* max_totf maximum allowed total frequency count */
/* rescale desired rescaling interval, must be <max_totf/2 */
/* init array of int's to be used for initialisation (NULL ok) */
void initbitmodel( bitmodel *m, int n, int max_totf, int rescale,
int *init );

/* reinitialisation of bitmodel */
/* m bitmodel to be initialized */
/* init array of int's to be used for initialisation (NULL ok) */
void resetbitmodel( bitmodel *m, int *init);


/* Initialization of bitmodel */
void initbitmodel(bitmodel *m, int n, int max_totf, int rescale, int *init);
/* deletion of bitmodel m */
void deletebitmodel( bitmodel *m );

/* Reinitialization of bitmodel */
void resetbitmodel(bitmodel *m, int *init);

/* Deletion of bitmodel */
void deletebitmodel(bitmodel *m);
/* retrieval of estimated frequencies for a symbol */
/* m bitmodel to be questioned */
/* sym symbol for which data is desired; must be <n */
/* sy_f frequency of that symbol */
/* lt_f frequency of all smaller symbols together */
/* the total frequency can be obtained with bit_totf */
void bitgetfreq( bitmodel *m, int sym, int *sy_f, int *lt_f);

/* Retrieval of estimated frequencies for a symbol */
void bitgetfreq(bitmodel *m, int sym, int *sy_f, int *lt_f);
/* find out total frequency for a bitmodel */
/* m bitmodel to be questioned */
#define bittotf(m) ((m)->totalfreq)

/* Find total frequency */
#define bitmodel_total_freq(m) ((m)->totalfreq)
/* find out symbol for a given cumulative frequency */
/* m bitmodel to be questioned */
/* lt_f cumulative frequency */
int bitgetsym( bitmodel *m, int lt_f );

/* Find symbol for a given cumulative frequency */
int bitgetsym(bitmodel *m, int lt_f);

/* Update model */
void bitupdate(bitmodel *m, int sym);
/* update model */
/* m bitmodel to be updated */
/* sym symbol that occurred (must be <n from init) */
void bitupdate( bitmodel *m, int sym );


#ifdef EXCLUDEONUPDATE
/* Update model and exclude symbol */
void bitupdate_ex(bitmodel *m, int sym);
/* update model and exclude symbol */
/* m bitmodel to be updated */
/* sym symbol that occurred (must be <n from init) */
void bitupdate_ex( bitmodel *m, int sym );

/* Deactivate symbol */
void bitdeactivate(bitmodel *m, int sym);

/* Reactivate symbol */
void bitreactivate(bitmodel *m, int sym);
#endif
/* deactivate symbol */
/* m bitmodel to be updated */
/* sym symbol to be deactivated */
void bitdeactivate( bitmodel *m, int sym );

#ifdef __cplusplus
}
/* reactivate symbol */
/* m bitmodel to be updated */
/* sym symbol to be reactivated */
void bitreactivate( bitmodel *m, int sym );
#endif

#endif /* BITMODEL_H */
#endif
Loading

0 comments on commit b25a1c1

Please sign in to comment.