16-bit int fixes.

This fixes a number of issues for platforms with a 16-bit int, but
 by no means all of them.
The type change for ec_window (for platforms where sizeof(size_t)==2)
 will break ABI (but not API) compatibility with libsilk and libopus,
 and reduce speed on x86-64, but allows the code to work in real-mode
 DOS without using the huge memory model, which is useful for testing
 16-bit int compliance.
This commit is contained in:
Timothy B. Terriberry 2011-02-06 13:29:00 -08:00 committed by Jean-Marc Valin
parent b570f1a910
commit 285bc372ca
5 changed files with 17 additions and 16 deletions

View file

@ -872,7 +872,7 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, i
{ {
#endif #endif
int i, c, N; int i, c, N;
int bits; celt_int32 bits;
ec_enc _enc; ec_enc _enc;
VARDECL(celt_sig, in); VARDECL(celt_sig, in);
VARDECL(celt_sig, freq); VARDECL(celt_sig, freq);
@ -1439,8 +1439,8 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, i
ALLOC(pulses, st->mode->nbEBands, int); ALLOC(pulses, st->mode->nbEBands, int);
ALLOC(fine_priority, st->mode->nbEBands, int); ALLOC(fine_priority, st->mode->nbEBands, int);
/* bits = packet size - where we are - safety*/ /* bits = packet size - where we are - safety*/
bits = (nbCompressedBytes*8<<BITRES) - ec_tell_frac(enc) - 1; bits = ((celt_int32)nbCompressedBytes*8<<BITRES) - ec_tell_frac(enc) - 1;
anti_collapse_rsv = isTransient&&LM>=2&&bits>=(LM+2<<BITRES) ? (1<<BITRES) : 0; anti_collapse_rsv = isTransient&&LM>=2&&bits>=(LM+2<<BITRES) ? (1<<BITRES) : 0;
bits -= anti_collapse_rsv; bits -= anti_collapse_rsv;
codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap, codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
@ -2151,7 +2151,7 @@ int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *da
#endif #endif
int c, i, N; int c, i, N;
int spread_decision; int spread_decision;
int bits; celt_int32 bits;
ec_dec _dec; ec_dec _dec;
VARDECL(celt_sig, freq); VARDECL(celt_sig, freq);
VARDECL(celt_norm, X); VARDECL(celt_norm, X);
@ -2367,7 +2367,7 @@ int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *da
alloc_trim = tell+(6<<BITRES) <= total_bits ? alloc_trim = tell+(6<<BITRES) <= total_bits ?
ec_dec_icdf(dec, trim_icdf, 7) : 5; ec_dec_icdf(dec, trim_icdf, 7) : 5;
bits = (len*8<<BITRES) - ec_tell_frac(dec) - 1; bits = ((celt_int32)len*8<<BITRES) - ec_tell_frac(dec) - 1;
anti_collapse_rsv = isTransient&&LM>=2&&bits>=(LM+2<<BITRES) ? (1<<BITRES) : 0; anti_collapse_rsv = isTransient&&LM>=2&&bits>=(LM+2<<BITRES) ? (1<<BITRES) : 0;
bits -= anti_collapse_rsv; bits -= anti_collapse_rsv;
codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap, codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,

View file

@ -41,14 +41,15 @@
typedef celt_int32 ec_int32; typedef celt_int32 ec_int32;
typedef celt_uint32 ec_uint32; typedef celt_uint32 ec_uint32;
typedef size_t ec_window; typedef celt_uint32 ec_window;
typedef struct ec_ctx ec_ctx; typedef struct ec_ctx ec_ctx;
typedef struct ec_ctx ec_enc; typedef struct ec_ctx ec_enc;
typedef struct ec_ctx ec_dec; typedef struct ec_ctx ec_dec;
/*This must be at least 32 bits.*/ /*OPT: This must be at least 32 bits, but if you have fast arithmetic on a
larger type, you can speed up the decoder by using it for ec_window.*/
# define EC_WINDOW_SIZE ((int)sizeof(ec_window)*CHAR_BIT) # define EC_WINDOW_SIZE ((int)sizeof(ec_window)*CHAR_BIT)
/*The number of bits to use for the range-coded part of unsigned integers.*/ /*The number of bits to use for the range-coded part of unsigned integers.*/

View file

@ -222,7 +222,7 @@ ec_uint32 ec_dec_uint(ec_dec *_this,ec_uint32 _ft){
ft=(unsigned)(_ft>>ftb)+1; ft=(unsigned)(_ft>>ftb)+1;
s=ec_decode(_this,ft); s=ec_decode(_this,ft);
ec_dec_update(_this,s,s+1,ft); ec_dec_update(_this,s,s+1,ft);
t=s<<ftb|ec_dec_bits(_this,ftb); t=(ec_uint32)s<<ftb|ec_dec_bits(_this,ftb);
if(t<=_ft)return t; if(t<=_ft)return t;
_this->error=1; _this->error=1;
return _ft; return _ft;

View file

@ -252,11 +252,11 @@ void compute_pulse_cache(CELTMode *m, int LM)
#define ALLOC_STEPS 6 #define ALLOC_STEPS 6
static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start, static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start,
const int *bits1, const int *bits2, const int *thresh, const int *cap, int total, celt_int32 *_balance, const int *bits1, const int *bits2, const int *thresh, const int *cap, celt_int32 total, celt_int32 *_balance,
int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits, int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits,
int *ebits, int *fine_priority, int _C, int LM, ec_ctx *ec, int encode, int prev) int *ebits, int *fine_priority, int _C, int LM, ec_ctx *ec, int encode, int prev)
{ {
int psum; celt_int32 psum;
int lo, hi; int lo, hi;
int i, j; int i, j;
int logM; int logM;
@ -264,7 +264,7 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int
int stereo; int stereo;
int codedBands=-1; int codedBands=-1;
int alloc_floor; int alloc_floor;
int left, percoeff; celt_int32 left, percoeff;
int done; int done;
int balance; int balance;
SAVE_STACK; SAVE_STACK;
@ -282,7 +282,7 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int
done = 0; done = 0;
for (j=end;j-->start;) for (j=end;j-->start;)
{ {
int tmp = bits1[j] + (mid*bits2[j]>>ALLOC_STEPS); int tmp = bits1[j] + (mid*(celt_int32)bits2[j]>>ALLOC_STEPS);
if (tmp >= thresh[j] || done) if (tmp >= thresh[j] || done)
{ {
done = 1; done = 1;
@ -344,7 +344,7 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int
left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0); rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0);
band_width = m->eBands[codedBands]-m->eBands[j]; band_width = m->eBands[codedBands]-m->eBands[j];
band_bits = bits[j] + percoeff*band_width + rem; band_bits = (int)(bits[j] + percoeff*band_width + rem);
/*Only code a skip decision if we're above the threshold for this band. /*Only code a skip decision if we're above the threshold for this band.
Otherwise it is force-skipped. Otherwise it is force-skipped.
This ensures that we have enough bits to code the skip flag.*/ This ensures that we have enough bits to code the skip flag.*/
@ -420,10 +420,10 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int
percoeff = left/(m->eBands[codedBands]-m->eBands[start]); percoeff = left/(m->eBands[codedBands]-m->eBands[start]);
left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
for (j=start;j<codedBands;j++) for (j=start;j<codedBands;j++)
bits[j] += percoeff*(m->eBands[j+1]-m->eBands[j]); bits[j] += (int)(percoeff*(m->eBands[j+1]-m->eBands[j]));
for (j=start;j<codedBands;j++) for (j=start;j<codedBands;j++)
{ {
int tmp = IMIN(left, m->eBands[j+1]-m->eBands[j]); int tmp = (int)IMIN(left, m->eBands[j+1]-m->eBands[j]);
bits[j] += tmp; bits[j] += tmp;
left -= tmp; left -= tmp;
} }

View file

@ -105,7 +105,7 @@ celt_int16 **compute_alloc_cache(CELTMode *m, int M);
@return Total number of bits allocated @return Total number of bits allocated
*/ */
int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stero, int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stero,
int total, celt_int32 *balance, int *pulses, int *ebits, int *fine_priority, int _C, int LM, ec_ctx *ec, int encode, int prev); celt_int32 total, celt_int32 *balance, int *pulses, int *ebits, int *fine_priority, int _C, int LM, ec_ctx *ec, int encode, int prev);
#endif #endif