From 8035b6589dcbb10e67080088ea6ac837ed3a346d Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Thu, 27 May 2010 16:23:52 -0400 Subject: [PATCH] Adds a range coder call to encode a single bit with arbitrary probability --- libcelt/entdec.c | 18 ++++++++++++++++++ libcelt/entdec.h | 3 +++ libcelt/entenc.c | 11 +++++++++++ libcelt/entenc.h | 3 +++ 4 files changed, 35 insertions(+) diff --git a/libcelt/entdec.c b/libcelt/entdec.c index 5b8846bc..191d3ee8 100644 --- a/libcelt/entdec.c +++ b/libcelt/entdec.c @@ -111,3 +111,21 @@ ec_uint32 ec_dec_uint(ec_dec *_this,ec_uint32 _ft){ return t; } } + +/* The probability of having a "one" is given in 1/256 */ +int ec_dec_bit_prob(ec_dec *_this,int _prob) +{ + int val; + int fl=0, fh=256-_prob; + val = ec_decode_bin(_this, 8); + if (val >= fh) + { + val = 1; + fl=fh; + fh=256; + } else { + val = 0; + } + ec_dec_update(_this,fl,fh,256); + return val; +} diff --git a/libcelt/entdec.h b/libcelt/entdec.h index d006a97c..008b6c8d 100644 --- a/libcelt/entdec.h +++ b/libcelt/entdec.h @@ -111,6 +111,9 @@ ec_uint32 ec_dec_bits(ec_dec *_this,int _ftb); Return: The decoded bits.*/ ec_uint32 ec_dec_uint(ec_dec *_this,ec_uint32 _ft); +/* Decode a bit that has a _prob/256 probability of being a one */ +int ec_dec_bit_prob(ec_dec *_this,int _prob); + /*Returns the number of bits "used" by the decoded symbols so far. The actual number of bits may be larger, due to rounding to whole bytes, or smaller, due to trailing zeros that were be stripped, so this is not an diff --git a/libcelt/entenc.c b/libcelt/entenc.c index 7e67a7fb..44353f5b 100644 --- a/libcelt/entenc.c +++ b/libcelt/entenc.c @@ -101,3 +101,14 @@ void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft){ } } +/* The probability of having a "one" is given in 1/256 */ +void ec_enc_bit_prob(ec_enc *_this,int val,int _prob) +{ + int fl=0, fh=256-_prob; + if (val) + { + fl=fh; + fh=256; + } + ec_encode_bin(_this,fl,fh,8); +} diff --git a/libcelt/entenc.h b/libcelt/entenc.h index 73f67e75..a547e5de 100644 --- a/libcelt/entenc.h +++ b/libcelt/entenc.h @@ -91,6 +91,9 @@ void ec_enc_bits(ec_enc *_this,ec_uint32 _fl,int _ftb); This must be at least one, and no more than 2**32-1.*/ void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft); +/* Encode a bit that has a _prob/256 probability of being a one */ +void ec_enc_bit_prob(ec_enc *_this,int val,int _prob); + /*Returns the number of bits "used" by the encoded symbols so far. The actual number of bits may be larger, due to rounding to whole bytes, or smaller, due to trailing zeros that can be stripped, so this is not an