ArithmeticCoding  0.0.0a
Arithmetic coding in C
Classes | Defines | Typedefs | Functions
Arithmetic Coding Internals

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 Documentation

#define B   (state->b)

Definition at line 388 of file ac.c.

#define bitsofD   (8)

Definition at line 397 of file ac.c.

#define C   (state->cdf)

Definition at line 390 of file ac.c.

#define D   (1ULL<<bitsofD)

Definition at line 398 of file ac.c.

#define DATA   (state->d.d)

Definition at line 395 of file ac.c.

#define DEFN_DECODE (   TOUT,
  TIN 
)
Value:
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);                       \
}

Definition at line 565 of file ac.c.

#define DEFN_DECODE_OUTS (   TIN)
Value:
DEFN_DECODE(u8,TIN);  \
  DEFN_DECODE(u16,TIN); \
  DEFN_DECODE(u32,TIN); \
  DEFN_DECODE(u64,TIN);

Definition at line 584 of file ac.c.

#define DEFN_DPRIME (   T)
Value:
static void dprime_##T(state_t *state, u64* v)                             \
{ size_t i;                                                               \
  *v = 0;                                                                 \
  for(i=bitsofD;i<=SHIFT;i+=bitsofD)                                      \
    *v += (1ULL<<(SHIFT-i))*pop_##T(STREAM); /*(2^8)^(P-n) = 2^(8*(P-n))*/ \
}

Definition at line 540 of file ac.c.

#define DEFN_DRENORM (   T)
Value:
static void drenorm_##T(state_t *state, u64 *v)\
{ while(L<LOWL)                               \
  { *v = ((*v<<bitsofD)&MASK)+pop_##T(STREAM); \
    L =   ( L<<bitsofD)&MASK;                 \
  }                                           \
}

Definition at line 528 of file ac.c.

#define DEFN_DSTEP (   T)
Value:
static u64 dstep_##T(state_t *state,u64 *v,int *isend) \
{                                 \
  u64 s = dselect(state,v,isend); \
  if( L<LOWL )                    \
    drenorm_##T(state,v);         \
  return s;                       \
}

Definition at line 552 of file ac.c.

#define DEFN_ENCODE (   TOUT,
  TIN 
)
Value:
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);                    \
}

Definition at line 479 of file ac.c.

#define DEFN_ENCODE_OUTS (   TIN)
Value:
DEFN_ENCODE(u1,TIN); \
  DEFN_ENCODE(u4,TIN); \
  DEFN_ENCODE(u8,TIN); \
  DEFN_ENCODE(u16,TIN);

Definition at line 491 of file ac.c.

#define DEFN_ERENORM (   T)
Value:
static void erenorm_##T(state_t *state) \
{                                      \
  const int s = SHIFT-bitsofD;         \
  while(L<LOWL)                        \
  { push_##T(STREAM, B>>s);             \
    L = (L<<bitsofD)&MASK;             \
    B = (B<<bitsofD)&MASK;             \
  }                                    \
}

Definition at line 427 of file ac.c.

#define DEFN_ESELECT (   T)
Value:
static void eselect_##T(state_t *state)                                                                \
  { u64 a;                                                                                              \
    a=B;                                                                                                \
    B=(B+(1ULL<<(SHIFT-bitsofD-1)) )&MASK; /* D^(P-1)/2: (2^8)^(4-1)/2 = 2^24/2 = 2^23 = 2^(32-8-1) */  \
    L=(1ULL<<(SHIFT-2*bitsofD))-1;         /* requires P>2 */                                           \
    if(a>B)                                                                                             \
      carry_##T(STREAM);                                                                                  \
    erenorm_##T(state);                     /* output last 2 symbols */                                  \
  }

Definition at line 451 of file ac.c.

#define DEFN_ESTEP (   T)
Value:
static void estep_##T(state_t *state,u64 s) \
  {                                          \
    update_##T(s,state);                      \
    if(L<LOWL)                               \
      erenorm_##T(state);                     \
  }

Definition at line 466 of file ac.c.

#define DEFN_UPDATE (   T)
Value:
static void update_##T(u64 s,state_t *state) \
  { u64 a,x,y;                                \
    y = L; /* End of interval */              \
    if(s!=(NSYM-1)) /*is not last symbol */   \
      y = (y*C[s+1])>>SHIFT;                  \
    a = B;                                    \
    x = (L*C[s])>>SHIFT;                      \
    B = (B+x)&MASK;                           \
    L = y-x;                                  \
    TRY(L>0);                                 \
    if(a>B)                                   \
      carry_##T(STREAM);                      \
    return;                                   \
  Error:                                      \
    abort();                                  \
  }

Definition at line 404 of file ac.c.

#define DEFN_VDECODE (   T)
Value:
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();                                             \
}

Definition at line 654 of file ac.c.

#define DEFN_VENCODE (   T)
Value:
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();                                             \
}

Definition at line 627 of file ac.c.

#define L   (state->l)

Definition at line 389 of file ac.c.

#define LOWL   (state->lowl)

Definition at line 399 of file ac.c.

#define MASK   (state->mask)

Definition at line 393 of file ac.c.

#define NSYM   (state->nsym)

Definition at line 392 of file ac.c.

#define OFLOW   (STREAM->overflow)

Definition at line 396 of file ac.c.

#define SHIFT   (state->shift)

Definition at line 391 of file ac.c.

#define STREAM   (&(state->d))

Definition at line 394 of file ac.c.


Typedef Documentation

typedef struct _state_t state_t

Encoder/Decoder state.

Used internally. Not externally visible.


Function Documentation

void carry_null ( stream_t s)

Definition at line 401 of file ac.c.

void cdf_build ( real **  cdf,
size_t *  M,
u32 s,
size_t  N 
)

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 has M+1 elements.
  • cdf[(*M)+1]==1.0 (within floating point precision)
Parameters:
[in,out]cdfThe cumulative distribution function computed over s. If *cdf is not null, will realloc() if necessary.
[out]MThe number of symbols in the array s.
[in]sThe input message formated as a u32 array of N elements.
[in]NThe number of elements in the array s.

Definition at line 365 of file ac.c.

DEFN_DECODE_OUTS ( u1  )
DEFN_DECODE_OUTS ( u4  )
DEFN_DPRIME ( u1  )
DEFN_DPRIME ( u4  )
DEFN_DPRIME ( u8  )
DEFN_DRENORM ( u1  )
DEFN_DRENORM ( u4  )
DEFN_DSTEP ( u1  )
DEFN_DSTEP ( u4  )
DEFN_DSTEP ( u8  )
DEFN_ERENORM ( u1  )
DEFN_ERENORM ( u4  )
DEFN_ESELECT ( u1  )
DEFN_ESELECT ( u4  )
DEFN_ESTEP ( u1  )
DEFN_ESTEP ( u4  )
DEFN_ESTEP ( u8  )
DEFN_ESTEP ( null  )
DEFN_UPDATE ( u1  )
DEFN_UPDATE ( u4  )
DEFN_UPDATE ( u8  )
DEFN_UPDATE ( null  )
static u64 dselect ( state_t state,
u64 v,
int *  isend 
) [static]

Definition at line 505 of file ac.c.

static void erenorm_null ( state_t state) [static]

Definition at line 441 of file ac.c.

static void free_internal ( state_t state) [static]

Releases resources held by the state_t structure.

Definition at line 322 of file ac.c.

static void init_common ( state_t state,
u8 buf,
size_t  nbuf,
real cdf,
size_t  nsym 
) [static]

A helper function that initializes the parts of the state_t structure that do not depend on stream type.

Definition at line 265 of file ac.c.

static void init_u1 ( state_t state,
u8 buf,
size_t  nbuf,
real cdf,
size_t  nsym 
) [static]

Initialize the state_t structure for u1 streams.

Definition at line 290 of file ac.c.

static void init_u16 ( state_t state,
u8 buf,
size_t  nbuf,
real cdf,
size_t  nsym 
) [static]

Initialize the state_t structure for u16 streams.

Definition at line 314 of file ac.c.

static void init_u4 ( state_t state,
u8 buf,
size_t  nbuf,
real cdf,
size_t  nsym 
) [static]

Initialize the state_t structure for u4 streams.

Definition at line 298 of file ac.c.

static void init_u8 ( state_t state,
u8 buf,
size_t  nbuf,
real cdf,
size_t  nsym 
) [static]

Initialize the state_t structure for u8 streams.

Definition at line 306 of file ac.c.

static u32 maximum ( u32 s,
size_t  n 
) [static]

Array maximum.

Returns:
the maximum value in a u32 array, s, with n elements.

Definition at line 334 of file ac.c.

void push_null ( stream_t s)

Definition at line 402 of file ac.c.

void sync ( stream_t dest,
stream_t src 
)

Definition at line 598 of file ac.c.

void vdecode1 ( u8 **  out,
size_t *  nout,
u8 in,
size_t  nin,
real cdf,
size_t  nsym,
real tcdf,
size_t  tsym 
)

Definition at line 602 of file ac.c.

 All Classes Files Functions Variables Typedefs Defines