Commit 38b6d479 authored by Jean-Marc Valin's avatar Jean-Marc Valin
Browse files

remove prob{enc,dec,mod} that were no longer needed

parent 0c387d78
#include <stdlib.h>
#include <string.h>
#include "probdec.h"
#include "bitrdec.h"
/*Gets the cumulative frequency count between _lo and _hi, as well as the
cumulative frequency count below _lo.*/
static unsigned ec_probmod_get_total(ec_probmod *_this,unsigned *_fl,
unsigned _lo,unsigned _hi){
*_fl=ec_bitree_get_cumul(_this->bitree,_lo);
return ec_bitree_get_cumul(_this->bitree,_hi)-*_fl;
}
static int ec_probmod_find_and_update(ec_probmod *_this,ec_probsamp *_samp,
unsigned _freq){
int sym;
sym=ec_bitree_find_and_update(_this->bitree,_this->sz,_this->split,
_freq,&_samp->fl,_this->inc);
_samp->fs=ec_bitree_get_freq(_this->bitree,sym)-_this->inc;
_this->ft+=_this->inc;
if(_this->ft>_this->thresh){
ec_bitree_halve(_this->bitree,_this->sz,_this->split);
_this->ft=ec_bitree_get_cumul(_this->bitree,_this->sz);
}
return sym;
}
int ec_probmod_read(ec_probmod *_this,ec_dec *_dec){
ec_probsamp samp;
unsigned freq;
int sym;
samp.ft=_this->ft;
freq=ec_decode(_dec,samp.ft);
sym=ec_probmod_find_and_update(_this,&samp,freq);
ec_dec_update(_dec,samp.fl,samp.fl+samp.fs,samp.ft);
return sym;
}
int ec_probmod_read_range(ec_probmod *_this,ec_dec *_dec,int _lo,int _hi){
ec_probsamp samp;
unsigned freq;
unsigned base;
int sz;
int sym;
sz=_this->sz;
_lo=EC_MINI(_lo,sz);
_hi=EC_MINI(_hi,sz);
if(_hi<=_lo)return -1;
samp.ft=ec_probmod_get_total(_this,&base,_lo,_hi);
freq=ec_decode(_dec,samp.ft);
sym=ec_probmod_find_and_update(_this,&samp,freq+base);
samp.fl-=base;
ec_dec_update(_dec,samp.fl,samp.fl+samp.fs,samp.ft);
return sym;
}
#if !defined(_probdec_H)
# define _probdec_H (1)
# include "probmod.h"
# include "entdec.h"
/*Decodes a single symbol using the given probability model and entropy
decoder.
Return: The decoded symbol.*/
int ec_probmod_read(ec_probmod *_this,ec_dec *_dec);
/*Decodes a single symbol using the given probability model and entropy
decoder, restricted to a given subrange of the available symbols.
This effectively sets the frequency counts of all the symbols outside this
range to zero, decodes the symbol, then restores the counts to their
original values, and updates the model.
_lo: The first legal symbol to decode.
_hi: One greater than the last legal symbol to decode.
This must be greater than _lo.
Return: The decoded symbol.*/
int ec_probmod_read_range(ec_probmod *_this,ec_dec *_dec,int _lo,int _hi);
#endif
#include <string.h>
#include "probenc.h"
#include "bitrenc.h"
static void ec_probmod_samp_and_update(ec_probmod *_this,ec_probsamp *_samp,
unsigned _sym){
unsigned sz;
sz=_this->sz;
_samp->fs=ec_bitree_get_freq(_this->bitree,_sym);
_samp->fl=ec_bitree_get_cumul(_this->bitree,_sym);
_samp->ft=_this->ft;
ec_bitree_update(_this->bitree,sz,_sym,_this->inc);
_this->ft+=_this->inc;
if(_this->ft>_this->thresh){
ec_bitree_halve(_this->bitree,sz,_this->split);
_this->ft=ec_bitree_get_cumul(_this->bitree,sz);
}
}
static void ec_probmod_samp_and_update_range(ec_probmod *_this,
ec_probsamp *_samp,int _sym,int _lo,int _hi){
unsigned base;
int sz;
sz=_this->sz;
base=ec_bitree_get_cumul(_this->bitree,_lo);
_samp->fs=ec_bitree_get_freq(_this->bitree,_sym);
_samp->fl=ec_bitree_get_cumul(_this->bitree,_sym)-base;
_samp->ft=ec_bitree_get_cumul(_this->bitree,_hi)-base;
ec_bitree_update(_this->bitree,sz,_sym,_this->inc);
_this->ft+=_this->inc;
if(_this->ft>_this->thresh){
ec_bitree_halve(_this->bitree,sz,_this->split);
_this->ft=ec_bitree_get_cumul(_this->bitree,sz);
}
}
void ec_probmod_write(ec_probmod *_this,ec_enc *_enc,int _sym){
ec_probsamp samp;
ec_probmod_samp_and_update(_this,&samp,_sym);
ec_encode(_enc,samp.fl,samp.fl+samp.fs,samp.ft);
}
void ec_probmod_write_range(ec_probmod *_this,ec_enc *_enc,int _sym,
int _lo,int _hi){
ec_probsamp samp;
ec_probmod_samp_and_update_range(_this,&samp,_sym,_lo,_hi);
ec_encode(_enc,samp.fl,samp.fl+samp.fs,samp.ft);
}
#if !defined(_probenc_H)
# define _probenc_H (1)
# include "probmod.h"
# include "entenc.h"
/*Encodes a single symbol using the given probability model and entropy
encoder.
_sym: The symbol to encode.*/
void ec_probmod_write(ec_probmod *_this,ec_enc *_enc,int _sym);
/*Encodes a single symbol using the given probability model and entropy
encoder, restricted to a given subrange of the available symbols.
This effectively sets the frequency counts of all the symbols outside this
range to zero, encodes the symbol, then restores the counts to their
original values, and updates the models.
_sym: The symbol to encode.
The caller must ensure this falls in the range _lo<=_sym<_hi.
_lo: The first legal symbol to encode.
_hi: One greater than the last legal symbol to encode.
This must be greater than _lo.*/
void ec_probmod_write_range(ec_probmod *_this,ec_enc *_enc,int _sym,
int _lo,int _hi);
#endif
#include <stdlib.h>
#include <string.h>
#include "probmod.h"
#include "bitree.h"
void ec_probmod_init(ec_probmod *_this,unsigned _sz){
ec_probmod_init_full(_this,_sz,1U,1U<<23,NULL);
}
void ec_probmod_init_from_counts(ec_probmod *_this,unsigned _sz,
const unsigned *_counts){
ec_probmod_init_full(_this,_sz,1U,1U<<23,_counts);
}
void ec_probmod_init_full(ec_probmod *_this,unsigned _sz,unsigned _inc,
unsigned _thresh,const unsigned *_counts){
unsigned s;
_this->sz=_sz;
for(s=1;s<=_this->sz;s<<=1);
_this->split=s>>1;
_this->inc=_inc;
_this->thresh=_thresh;
_this->bitree=(unsigned *)malloc(_sz*sizeof(*_this->bitree));
if(_counts!=NULL)memcpy(_this->bitree,_counts,_sz*sizeof(*_this->bitree));
else for(s=0;s<_this->sz;s++)_this->bitree[s]=1;
ec_bitree_from_counts(_this->bitree,_sz);
_this->ft=ec_bitree_get_cumul(_this->bitree,_sz);
}
void ec_probmod_clear(ec_probmod *_this){
free(_this->bitree);
}
#if !defined(_probmod_H)
# define _probmod_H (1)
# include "entcode.h"
typedef struct ec_probsamp ec_probsamp;
typedef struct ec_probmod ec_probmod;
/*A sample from a probability distribution.
This is the information needed to encode a symbol or update the decoder
state.*/
struct ec_probsamp{
/*The cumulative frequency of all symbols preceding this one in the
alphabet.*/
unsigned fl;
/*The frequency of the symbol coded.*/
unsigned fs;
/*The total frequency of all symbols in the alphabet.*/
unsigned ft;
};
/*A simple frequency-count probability model.*/
struct ec_probmod{
/*The number of symbols in this context.*/
int sz;
/*The largest power of two less than or equal to sz.*/
int split;
/*The amount by which to increment the frequency count of an observed
symbol.*/
unsigned inc;
/*The current total frequency count.*/
unsigned ft;
/*The maximum total frequency count allowed before the counts are rescaled.
Note that this should be larger than (inc+1>>1)+sz-1, since at most one
rescaling is done per decoded symbol.
Otherwise, this threshold might be exceeded.
This must be less than 2**23 for a range coder, and 2**31 for an
arithmetic coder.*/
unsigned thresh;
/*The binary indexed tree used to keep track of the frequency counts.*/
unsigned *bitree;
};
/*Initializes a probability model of the given size.
The amount to increment and all frequency counts are initialized to 1.
The rescaling threshold is initialized to 2**23.
_sz: The number of symbols in this context.*/
void ec_probmod_init(ec_probmod *_this,unsigned _sz);
/*Initializes a probability model of the given size.
The amount to increment is initialized to 1.
The rescaling threshold is initialized to 2**23.
_sz: The number of symbols in this context.
_counts: The initial frequency count of each symbol.*/
void ec_probmod_init_from_counts(ec_probmod *_this,unsigned _sz,
const unsigned *_counts);
/*Initializes a probability model of the given size.
_sz: The number of symbols in this context.
_inc: The amount by which to increment the frequency count of an observed
symbol.
_thresh: The maximum total frequency count allowed before the counts are
rescaled.
See above for restrictions on this value.
_counts: The initial frequency count of each symbol, or NULL to initialize
each frequency count to 1.*/
void ec_probmod_init_full(ec_probmod *_this,unsigned _sz,unsigned _inc,
unsigned _thresh,const unsigned *_counts);
/*Frees all memory used by this probability model.*/
void ec_probmod_clear(ec_probmod *_this);
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment