ArithmeticCoding
0.0.0a
Arithmetic coding in C
|
Classes | |
struct | _state_t |
Encoder/Decoder state. More... | |
Defines | |
#define | B (state->b) |
#define | L (state->l) |
#define | C (state->cdf) |
#define | SHIFT (state->shift) |
#define | NSYM (state->nsym) |
#define | MASK (state->mask) |
#define | STREAM (&(state->d)) |
#define | DATA (state->d.d) |
#define | OFLOW (STREAM->overflow) |
#define | bitsofD (8) |
#define | D (1ULL<<bitsofD) |
#define | LOWL (state->lowl) |
#define | DEFN_UPDATE(T) |
#define | DEFN_ERENORM(T) |
#define | DEFN_ESELECT(T) |
#define | DEFN_ESTEP(T) |
#define | DEFN_ENCODE(TOUT, TIN) |
#define | DEFN_ENCODE_OUTS(TIN) |
#define | DEFN_DRENORM(T) |
#define | DEFN_DPRIME(T) |
#define | DEFN_DSTEP(T) |
#define | DEFN_DECODE(TOUT, TIN) |
#define | DEFN_DECODE_OUTS(TIN) |
#define | DEFN_VENCODE(T) |
#define | DEFN_VDECODE(T) |
Typedefs | |
typedef struct _state_t | state_t |
Encoder/Decoder state. | |
Functions | |
static void | init_common (state_t *state, u8 *buf, size_t nbuf, real *cdf, size_t nsym) |
A helper function that initializes the parts of the state_t structure that do not depend on stream type. | |
static void | init_u1 (state_t *state, u8 *buf, size_t nbuf, real *cdf, size_t nsym) |
Initialize the state_t structure for u1 streams. | |
static void | init_u4 (state_t *state, u8 *buf, size_t nbuf, real *cdf, size_t nsym) |
Initialize the state_t structure for u4 streams. | |
static void | init_u8 (state_t *state, u8 *buf, size_t nbuf, real *cdf, size_t nsym) |
Initialize the state_t structure for u8 streams. | |
static void | init_u16 (state_t *state, u8 *buf, size_t nbuf, real *cdf, size_t nsym) |
Initialize the state_t structure for u16 streams. | |
static void | free_internal (state_t *state) |
Releases resources held by the state_t structure. | |
static u32 | maximum (u32 *s, size_t n) |
Array maximum. | |
void | cdf_build (real **cdf, size_t *M, u32 *s, size_t N) |
Build a cumulative distribution function (CDF) from an input u32 array. | |
void | carry_null (stream_t *s) |
void | push_null (stream_t *s) |
DEFN_UPDATE (u1) | |
DEFN_UPDATE (u4) | |
DEFN_UPDATE (u8) | |
DEFN_UPDATE (null) | |
DEFN_ERENORM (u1) | |
DEFN_ERENORM (u4) | |
DEFN_ERENORM (u8) | |
static void | erenorm_null (state_t *state) |
DEFN_ESELECT (u1) | |
DEFN_ESELECT (u4) | |
DEFN_ESELECT (u8) | |
DEFN_ESTEP (u1) | |
DEFN_ESTEP (u4) | |
DEFN_ESTEP (u8) | |
DEFN_ESTEP (null) | |
DEFN_ENCODE_OUTS (u8) | |
DEFN_ENCODE_OUTS (u32) | |
DEFN_ENCODE_OUTS (u64) | |
static u64 | dselect (state_t *state, u64 *v, int *isend) |
DEFN_DRENORM (u1) | |
DEFN_DRENORM (u4) | |
DEFN_DRENORM (u8) | |
DEFN_DPRIME (u1) | |
DEFN_DPRIME (u4) | |
DEFN_DPRIME (u8) | |
DEFN_DSTEP (u1) | |
DEFN_DSTEP (u4) | |
DEFN_DSTEP (u8) | |
DEFN_DECODE_OUTS (u1) | |
DEFN_DECODE_OUTS (u4) | |
DEFN_DECODE_OUTS (u8) | |
void | sync (stream_t *dest, stream_t *src) |
void | vdecode1 (u8 **out, size_t *nout, u8 *in, size_t nin, real *cdf, size_t nsym, real *tcdf, size_t tsym) |
DEFN_VENCODE (u8) | |
DEFN_VENCODE (u32) | |
DEFN_VENCODE (u64) | |
DEFN_VDECODE (u8) | |
DEFN_VDECODE (u32) | |
DEFN_VDECODE (u64) |
#define DEFN_DECODE | ( | TOUT, | |
TIN | |||
) |
void decode_##TOUT##_##TIN(TOUT **out, size_t *nout, u8 *in, size_t nin, real *cdf, size_t nsym) \ { state_t s; \ stream_t d={0}; \ u64 v,x; \ size_t i=0; \ int isend=0; \ attach(&d,*out,*nout*sizeof(TOUT)); \ init_##TIN(&s,in,nin,cdf,nsym); \ dprime_##TIN(&s,&v); \ x=dstep_##TIN(&s,&v,&isend); \ while(!isend) \ { push_##TOUT(&d,x); \ x=dstep_##TIN(&s,&v,&isend); \ } \ free_internal(&s); \ detach(&d,(void**)out,nout); \ *nout /= sizeof(TOUT); \ }
#define DEFN_DECODE_OUTS | ( | TIN | ) |
#define DEFN_DPRIME | ( | T | ) |
#define DEFN_DRENORM | ( | T | ) |
#define DEFN_DSTEP | ( | T | ) |
#define DEFN_ENCODE | ( | TOUT, | |
TIN | |||
) |
void encode_##TOUT##_##TIN(void **out, size_t *nout, TIN *in, size_t nin, real *cdf, size_t nsym) \ { size_t i; \ state_t s; \ init_##TOUT(&s,*out,*nout,cdf,nsym); \ for(i=0;i<nin;++i) \ estep_##TOUT(&s,in[i]); \ estep_##TOUT(&s,s.nsym-1); \ eselect_##TOUT(&s); \ detach(&s.d,out,nout); \ free_internal(&s); \ }
#define DEFN_ENCODE_OUTS | ( | TIN | ) |
DEFN_ENCODE(u1,TIN); \ DEFN_ENCODE(u4,TIN); \ DEFN_ENCODE(u8,TIN); \ DEFN_ENCODE(u16,TIN);
#define DEFN_ERENORM | ( | T | ) |
#define DEFN_ESELECT | ( | T | ) |
#define DEFN_ESTEP | ( | T | ) |
#define DEFN_UPDATE | ( | T | ) |
#define DEFN_VDECODE | ( | T | ) |
void vdecode_##T(T **out, size_t *nout, size_t noutsym, u8 *in, size_t nin, size_t ninsym, real *cdf) \ { u8 *t=NULL; \ size_t i,n=0; \ real *tcdf; \ TRY( tcdf=malloc(sizeof(*tcdf)*(ninsym+1)) ); \ { size_t i; \ real v = 1.0/(real)ninsym; \ for(i=0;i<=ninsym;++i) \ tcdf[i] = i*v; \ } \ { void *buf=0; \ n = 0; \ encode_u8_u8(&buf,&n,in,nin,tcdf,ninsym); \ decode_##T##_u8(out,nout,buf,n,cdf,noutsym); \ free(buf); \ } \ free(tcdf); \ return; \ Error: \ abort(); \ }
#define DEFN_VENCODE | ( | T | ) |
void vencode_##T(u8 **out, size_t *nout, size_t noutsym, T *in, size_t nin, size_t ninsym, real *cdf) \ { u8 *t=NULL; \ size_t i,n=0; \ real *tcdf; \ TRY( tcdf=malloc(sizeof(*tcdf)*(noutsym+1)) ); \ { size_t i; \ real v = 1.0/(real)noutsym; \ for(i=0;i<=noutsym;++i) \ tcdf[i] = i*v; \ } \ { void *buf=0; \ n = 0; \ encode_u8_##T(&buf,&n,in,nin,cdf,ninsym); \ vdecode1(out,nout,buf,n,cdf,ninsym,tcdf,noutsym); \ free(buf); \ } \ free(tcdf); \ return; \ Error: \ abort(); \ }
void carry_null | ( | stream_t * | s | ) |
Build a cumulative distribution function (CDF) from an input u32 array.
As provided, this really just serves as a reference implementation showing how the CDF should be computed. Namely:
1. cdf
[0] must be zero (0.0).
2. The probability density for symbol i should be cdf
[i+1]-cdf[i].
This implies:
cdf
[(*M)+1]==1.0 (within floating point precision)[in,out] | cdf | The cumulative distribution function computed over s. If *cdf is not null, will realloc() if necessary. |
[out] | M | The number of symbols in the array s. |
[in] | s | The input message formated as a u32 array of N elements. |
[in] | N | The number of elements in the array s. |
DEFN_DECODE_OUTS | ( | u1 | ) |
DEFN_DECODE_OUTS | ( | u4 | ) |
DEFN_DECODE_OUTS | ( | u8 | ) |
DEFN_DPRIME | ( | u1 | ) |
DEFN_DPRIME | ( | u4 | ) |
DEFN_DPRIME | ( | u8 | ) |
DEFN_DRENORM | ( | u1 | ) |
DEFN_DRENORM | ( | u4 | ) |
DEFN_DRENORM | ( | u8 | ) |
DEFN_DSTEP | ( | u1 | ) |
DEFN_DSTEP | ( | u4 | ) |
DEFN_DSTEP | ( | u8 | ) |
DEFN_ENCODE_OUTS | ( | u8 | ) |
DEFN_ENCODE_OUTS | ( | u32 | ) |
DEFN_ENCODE_OUTS | ( | u64 | ) |
DEFN_ERENORM | ( | u1 | ) |
DEFN_ERENORM | ( | u4 | ) |
DEFN_ERENORM | ( | u8 | ) |
DEFN_ESELECT | ( | u1 | ) |
DEFN_ESELECT | ( | u4 | ) |
DEFN_ESELECT | ( | u8 | ) |
DEFN_ESTEP | ( | u1 | ) |
DEFN_ESTEP | ( | u4 | ) |
DEFN_ESTEP | ( | u8 | ) |
DEFN_ESTEP | ( | null | ) |
DEFN_UPDATE | ( | u1 | ) |
DEFN_UPDATE | ( | u4 | ) |
DEFN_UPDATE | ( | u8 | ) |
DEFN_UPDATE | ( | null | ) |
DEFN_VDECODE | ( | u8 | ) |
DEFN_VDECODE | ( | u32 | ) |
DEFN_VDECODE | ( | u64 | ) |
DEFN_VENCODE | ( | u8 | ) |
DEFN_VENCODE | ( | u32 | ) |
DEFN_VENCODE | ( | u64 | ) |
static void erenorm_null | ( | state_t * | state | ) | [static] |
static void free_internal | ( | state_t * | state | ) | [static] |
static void init_common | ( | state_t * | state, |
u8 * | buf, | ||
size_t | nbuf, | ||
real * | cdf, | ||
size_t | nsym | ||
) | [static] |