#include #include #include #include typedef struct RangeDecoder { uint32_t range,code; FILE *fh; } RangeDecoder; static void InitRangeDecoder(RangeDecoder *self,FILE *fh) { self->range=0xffffffff; self->code=0; self->fh=fh; for(int i=0;i<4;i++) self->code=(self->code<<8)|fgetc(fh); } static void NormalizeRangeDecoder(RangeDecoder *self) { while(self->range<0x1000000) { self->code=(self->code<<8)|fgetc(self->fh); self->range<<=8; } } static int ReadBitAndUpdateWeight(RangeDecoder *self,uint16_t *weight,int shift) { NormalizeRangeDecoder(self); uint32_t threshold=(self->range>>12)*(*weight); if(self->coderange=threshold; *weight+=(0x1000-*weight)>>shift; return 0; } else { self->range-=threshold; self->code-=threshold; *weight-=*weight>>shift; return 1; } } static uint32_t ReadUniversalCode(RangeDecoder *self,uint16_t *weights1,int shift1,uint16_t *weights2,int shift2) { int numbits=0; while(ReadBitAndUpdateWeight(self,&weights1[numbits],shift1)==1) numbits++; if(!numbits) return 0; uint32_t val=1; for(int i=0;i>20,(shifts>>16)&0xf,(shifts>>12)&0xf,(shifts>>8)&0xf,(shifts>>4)&0xf,shifts&0xf); fwrite(buf,size,1,stdout); }