/** returns into word a pointer to wcsa->vocq.zone, for the i-th qgram */ void getWord(twcsa *wcsa, uint ith, unsigned char **word, uint *lenword) { register uint len, tmplen; len = bitread (wcsa->wordsData.words, (ith+1)* wcsa->wordsData.elemSize , wcsa->wordsData.elemSize); tmplen = bitread (wcsa->wordsData.words, (ith )* wcsa->wordsData.elemSize , wcsa->wordsData.elemSize); len -=tmplen; *word= (unsigned char *) wcsa->wordsData.wordsZoneMem.zone + tmplen; *lenword = len; }
uint * decompressFT(FTRep * listRep, uint n){ uint mult=0; register uint i; register uint j; uint partialSum=0; uint nLevels=listRep->nLevels; uint * level=listRep->levels; uint readByte; uint * list = (uint *) malloc(sizeof(uint)*n); uint * cont = (uint *) malloc(sizeof(byte*)*listRep->nLevels); uint * pos = (uint *) malloc(sizeof(uint)*listRep->nLevels); for(j=0;j<nLevels;j++){ cont[j]=listRep->iniLevel[j]; pos[j]=listRep->levelsIndex[j]; } for(i=0;i<n;i++){ partialSum=0; j=0; mult=0; readByte = bitread(level,cont[j],listRep->base_bits[j]); cont[j]+=listRep->base_bits[j]; while((!bitget(listRep->bS->data,pos[j]))){ pos[j]++; partialSum = partialSum+ (readByte<<mult); mult+=listRep->base_bits[j]; j++; readByte = bitread(level,cont[j],listRep->base_bits[j]); cont[j]+=listRep->base_bits[j]; if(j==nLevels-1){ break; } } if(j<nLevels-1){ pos[j]++; } partialSum = partialSum + (readByte<<mult) + listRep->tablebase[j]; list[i]=partialSum; } free(cont); free(pos); return list; }
uint accessFT(FTRep * listRep,uint param){ uint mult=0; register uint j; uint partialSum=0; uint ini = param-1; uint nLevels=listRep->nLevels; uint * level; uint readByte; uint cont,pos, rankini; partialSum=0; j=0; level=listRep->levels ; pos=listRep->levelsIndex[j]+ini; mult=0; cont = listRep->iniLevel[j]+ini*listRep->base_bits[j]; readByte = bitread(level,cont,listRep->base_bits[j]); if(nLevels == 1){ return readByte; } while((!bitget(listRep->bS->data,pos))){ rankini = rank(listRep->bS, listRep->levelsIndex[j]+ini-1) - listRep->rankLevels[j]; ini = ini-rankini; partialSum = partialSum+ (readByte<<mult); mult+=listRep->base_bits[j]; j++; cont = listRep->iniLevel[j]+ini*listRep->base_bits[j]; pos=listRep->levelsIndex[j]+ini; readByte = bitread(level,cont,listRep->base_bits[j]); if(j==nLevels-1){ break; } } partialSum = partialSum + (readByte<<mult) + listRep->tablebase[j]; return partialSum; }
/**************************************************************************** * * ROUTINE : ExtractMVectorComponentA * * INPUTS : None. * * OUTPUTS : None. * * RETURNS : None. * * FUNCTION : Extracts a motion vector component coded with method A. * * SPECIAL NOTES : None. * * * ERRORS : None. * ****************************************************************************/ INT32 ExtractMVectorComponentA(PB_INSTANCE *pbi) { INT32 MVectComponent; // temp storage for motion vector UINT32 MVCode = 0; // Temporary storage while decoding the MV UINT32 ExtraBits = 0; // Get group to which coded component belongs MVCode = bitread( &pbi->br, 3 ); // Now extract the appropriate number of bits to identify the component switch ( MVCode ) { case 0: MVectComponent = 0; break; case 1: MVectComponent = 1; break; case 2: MVectComponent = -1; break; case 3: if ( bitread1( &pbi->br )) MVectComponent = -2; else MVectComponent = 2; break; case 4: if ( bitread1( &pbi->br ) ) MVectComponent = -3; else MVectComponent = 3; break; case 5: ExtraBits = bitread( &pbi->br, 2 ); MVectComponent = 4 + ExtraBits; if ( bitread1( &pbi->br ) ) MVectComponent = -MVectComponent; break; case 6: ExtraBits = bitread( &pbi->br, 3 ); MVectComponent = 8 + ExtraBits; if ( bitread1( &pbi->br )) MVectComponent = -MVectComponent; break; case 7: ExtraBits = bitread( &pbi->br, 4 ); MVectComponent = 16 + ExtraBits; if ( bitread1( &pbi->br ) ) MVectComponent = -MVectComponent; break; } return MVectComponent; }
unsigned int getHuffmanPsiValue(HuffmanCompressedPsi *cPsi, size_t position) { register size_t index; size_t sampleIndex, positionsSinceSample, ptr; unsigned int psiValue, absolute_value, huffmanCode; unsigned int binaryLenght, runLenght; unsigned int runLenghtStart = cPsi->nS - 64 - cPsi->T; unsigned int negStart = cPsi->nS - 64; unsigned int bigStart = cPsi->nS - 32; sampleIndex = position / cPsi->T; psiValue = bitread(cPsi->samples,sampleIndex*cPsi->sampleSize,cPsi->sampleSize); ptr = bitread64(cPsi->samplePointers,sampleIndex*cPsi->pointerSize,cPsi->pointerSize); positionsSinceSample = position%cPsi->T; for(index=0;index<positionsSinceSample;index++) { ptr = decodeHuff(&cPsi->diffsHT,&huffmanCode,cPsi->stream,ptr); if(huffmanCode < runLenghtStart) { // Incremento directo psiValue += huffmanCode; } else if(huffmanCode < negStart) { // Estamos nun run runLenght = huffmanCode - runLenghtStart; if(index+runLenght>=positionsSinceSample) return psiValue+positionsSinceSample-index; else { psiValue += runLenght; index += runLenght-1; } } else if(huffmanCode < bigStart) { // Negativo binaryLenght = huffmanCode-negStart+1; absolute_value = bitread(cPsi->stream,ptr,binaryLenght); ptr += binaryLenght; psiValue -= absolute_value; } else { // Grande binaryLenght = huffmanCode-bigStart+1; absolute_value = bitread(cPsi->stream,ptr,binaryLenght); ptr += binaryLenght; psiValue += absolute_value; } } return psiValue; }
BOOL LoadFrameHeader(PB_INSTANCE *pbi) { UINT8 VersionByte0; // Must be 0 for VP30b and later UINT8 DctQMask; UINT8 SpareBits; // Spare cfg bits UINT8 Unused; BOOL RetVal = TRUE; // Is the frame and inter frame or a key frame pbi->FrameType = bitread1(&pbi->br); // unused bit Unused = bitread1(&pbi->br); // Quality (Q) index DctQMask = (UINT8)bitread( &pbi->br, 6 ); // If the frame was a base frame then read the frame dimensions and build a bitmap structure. if ( (pbi->FrameType == BASE_FRAME) ) { // Read the frame dimensions bytes (0,0 indicates vp31 or later) VersionByte0 = (UINT8)bitread( &pbi->br, 8 ); pbi->Vp3VersionNo = (UINT8)bitread( &pbi->br, 5 ); if(pbi->Vp3VersionNo > CURRENT_DECODE_VERSION) { RetVal = FALSE; return RetVal; } // Initialise version specific quantiser values InitQTables( pbi ); // Read the type / coding method for the key frame. pbi->KeyFrameType = (UINT8)bitread( &pbi->br, 1 ); SpareBits = (UINT8)bitread( &pbi->br, 2 ); // Select huffman tables SelectHuffmanSet( pbi ); } // Set this frame quality value from Q Index pbi->ThisFrameQualityValue = pbi->QThreshTable[DctQMask]; return RetVal; }
/**************************************************************************** * * ROUTINE : ReadNextBlockPattern * * INPUTS : None. * * OUTPUTS : None. * * RETURNS : A pattern of coded or uncoded blocks for a macro block. * * FUNCTION : Returns the block pattern for the next macro block. * * SPECIAL NOTES : None. * * * ERRORS : None. * ****************************************************************************/ UINT8 ReadNextBlockPattern (PB_INSTANCE *pbi) { UINT8 BlockPattern = 0; UINT8 Bitpattern = 0; UINT32 BitCount = 3; // Read three bits and test to see if we have a valid token. Bitpattern = bitread(&pbi->br, 3); // Test pattern to see if is a valid token. BlockPattern = BlockDecode1[pbi->BlockPatternPredictor][Bitpattern]; // if pattern was not a valid token if ( !BlockPattern ) { BitCount++; Bitpattern = (Bitpattern << 1) + bitread1(&pbi->br); // Test pattern to see if is a valid token. BlockPattern = BlockDecode2[pbi->BlockPatternPredictor][Bitpattern]; if ( !BlockPattern ) { BitCount++; Bitpattern = (Bitpattern << 1) + bitread1(&pbi->br); BlockPattern = BlockDecode3[pbi->BlockPatternPredictor][Bitpattern]; } } // Update the entropy predictor for next time. pbi->BlockPatternPredictor = BPPredictor[BlockPattern]; return BlockPattern; }
int getGonzaloPsiValue(GonzaloCompressedPsi *compressedPsi, unsigned int position) { uint *cPsi = compressedPsi->cPsi; uint samplen = compressedPsi->samplen; uint *bposS = compressedPsi->bposS; uint bplen = compressedPsi->bplen; uint pslen = compressedPsi->pslen; THuff *Hacc = &compressedPsi->Hacc; THuff *Hlen = &compressedPsi->Hlen; uint sampj,cptr,val,dval,rlen,head,hlen; sampj = (position/samplen)*samplen; cptr = bitread(bposS,(sampj/samplen)*bplen,bplen); head = cptr; val = bitread(cPsi,head,pslen); head += pslen; while (sampj < position) { head = decodeHuff(Hacc,&dval,cPsi,head); if (dval == 0) { val = bitread(cPsi,head,pslen); head += pslen; sampj++; } else if (dval == 1) { head = decodeHuff(Hlen,&rlen,cPsi,head); rlen++; if (sampj + rlen >= position) return val + (position-sampj); val += rlen; sampj += rlen; } else { val += dval; sampj++; } } return val; }
/**************************************************************************** * * ROUTINE : ExtractMVectorComponentB * * INPUTS : None. * * OUTPUTS : None. * * RETURNS : None. * * FUNCTION : Extracts an MV component coded using the fallback method * * SPECIAL NOTES : None. * * * ERRORS : None. * ****************************************************************************/ INT32 ExtractMVectorComponentB(PB_INSTANCE *pbi) { INT32 MVectComponent; // temp storage for motion vector // Get group to which coded component belongs MVectComponent = bitread( &pbi->br, 5 ); if ( bitread1( &pbi->br ) ) MVectComponent = -MVectComponent; return MVectComponent; }
void __attribute__((interrupt, auto_psv)) _OC1Interrupt(void) { disable_interrupts(); bitclear(&IFS0, 2); OC1R = bitread(&leds_state[(uint16_t)(bitcount/8)], 7-bitcount%8) ? LEDS_HIGH_R : LEDS_LOW_R; bitcount++; if (bitcount == 24*LEDS_NUM+1) { OC1R = 0; bitclear(&IEC0, 2); // disable OC1 interrupt oc_free(&oc1); bitcount = 0; } enable_interrupts(); }
/*malloc a block.*/ void *kcache_malloc(struct kcache_struct *pcs) { if (0==pcs->c_avaliable){ return NULL; } else { size_t i; int d; unsigned char *p=NULL; get_spinlock(pcs->c_bmaplck); for (i=0;i<pcs->c_bcnt;i++) { d=bitread(i,pcs->c_bmap); if (0==d) { bitset(i,pcs->c_bmap); p=pcs->c_bpool+i*pcs->c_bsize; break; } } release_spinlock(pcs->c_bmaplck); return (void*)p; } }
/**************************************************************************** * * ROUTINE : UnpackAndExpandAcToken * * INPUTS : INT8 * ExpandedBlock * Pointer to block structure into which the token * should be expanded. * * UINT32 * CoeffIndex * Where we are in the current block. * * OUTPUTS : * * RETURNS : The number of bits decoded * * FUNCTION : Unpacks and expands a AC DCT token. * * SPECIAL NOTES : * * * ERRORS : None. * ****************************************************************************/ void UnpackAndExpandAcToken( PB_INSTANCE *pbi, Q_LIST_ENTRY * ExpandedBlock, UINT8 * CoeffIndex ) { INT32 ExtraBits; UINT32 Token; #ifdef DEBUG ExtraBits = 0; #endif // Extract a token. Token = ExtractToken(&pbi->br, pbi->HuffRoot_VP3x[pbi->ACHuffChoice]); /* Now.. if we are using the DCT optimised coding system, extract any * assosciated additional bits token. */ if ( ExtraBitLengths_VP31[Token] > 0 ) { /* Extract the appropriate number of extra bits. */ ExtraBits = bitread(&pbi->br,ExtraBitLengths_VP31[Token]); //0; } // Take token dependant action if ( Token >= DCT_SHORT_ZRL_TOKEN ) { // "Value", "zero run" and "zero run value" tokens ExpandToken( pbi, ExpandedBlock, CoeffIndex, Token, ExtraBits ); if ( *CoeffIndex >= BLOCK_SIZE ) pbi->BlocksToDecode --; } else if ( Token == DCT_EOB_TOKEN ) { *CoeffIndex = BLOCK_SIZE; pbi->BlocksToDecode --; } else { // Special action and EOB tokens switch ( Token ) { case DCT_EOB_PAIR_TOKEN: pbi->EOB_Run = 1; *CoeffIndex = BLOCK_SIZE; pbi->BlocksToDecode --; break; case DCT_EOB_TRIPLE_TOKEN: pbi->EOB_Run = 2; *CoeffIndex = BLOCK_SIZE; pbi->BlocksToDecode --; break; case DCT_REPEAT_RUN_TOKEN: pbi->EOB_Run = ExtraBits + 3; *CoeffIndex = BLOCK_SIZE; pbi->BlocksToDecode --; break; case DCT_REPEAT_RUN2_TOKEN: pbi->EOB_Run = ExtraBits + 7; *CoeffIndex = BLOCK_SIZE; pbi->BlocksToDecode --; break; case DCT_REPEAT_RUN3_TOKEN: pbi->EOB_Run = ExtraBits + 15; *CoeffIndex = BLOCK_SIZE; pbi->BlocksToDecode --; break; case DCT_REPEAT_RUN4_TOKEN: pbi->EOB_Run = ExtraBits - 1; *CoeffIndex = BLOCK_SIZE; pbi->BlocksToDecode --; break; } } }
int fsearchPREV(void * index, int target, uint idforList) { twcsa* wcsa = (twcsa *) index; uint l,r,m,val,num; uint pos,k; uint lenList = wcsa->lenList[idforList]; uint sampleK = wcsa->occs.sampleK; uint bucksize = wcsa->occs.occsBucketSize; uint absBits = wcsa->occs.absOffsetSize; uint bcBits = wcsa->occs.bcOffsetSize; uint firstpos = wcsa->occs.occs[idforList]; uint *zoneoccs = wcsa->occs.zoneOccs; byte *bcs = wcsa->occs.bcs; l = firstpos/bucksize; r= l+ (lenList +sampleK -1)/sampleK -1; // printf("\n=============================================: target = %d, lenList = %d,%s ==",target,lenList,wcsa->words[idforList]); // printf("\n firstpos = %d, lenList = %d, sampleK = %d, numbucks=%d, bucksize = %d",firstpos, lenList,sampleK, wcsa->occs.numBuckets,bucksize);fflush(stdout); // printf("\n l= %d, r=%d, target = %d",l,r,target);fflush(stdout); //After the last sampled absolute pos val = bitread (zoneoccs, r*bucksize, absBits); //the last Abs sample printf("\n ###valor en r*bucksize %d",val); if (val <= target) { printf("\n ###ULTIMO CACHO: target = %d, val = %d",target,val); if (val ==target) return 1; pos = bitread (zoneoccs, r*bucksize + absBits , bcBits); l=0; r =lenList%sampleK; printf("\n\t\t l= %d, r=%d, target = %d",l,r,target);fflush(stdout); while (l<r) { //decodes sequentially BC_TO_DEC(bcs, pos, num); val +=num; printf("\n\t\t\t %d pos",val); if (val ==target) { printf("\n **found!!"); return 1; // found!!. } if (val > target) { printf("\n **not found!! %d",val); return 0; //not found } l++; } printf("\n ***not found!!"); return 0; } //Before the last sampled absolute pos //printf("\nTO BIN SEARCH:: l= %d, r=%d, val = %d, target =%d",l,r,val,target);fflush(stdout); //m = (l + r) / 2; while (r>(l+1)) { m = (l + r) / 2; pos = m*bucksize; val = bitread (zoneoccs, pos, absBits); if (val < target) l=m; else if (val==target) return 1; else r=m; //printf("\nINTO BIN SEARCH:: l= %d, m= %d, r=%d, val = %d, target =%d",l,m,r,val,target);fflush(stdout); } //if (val==target) return 1; //printf("\nend BIN SEARCH:: l= %d, m= %d, r=%d, val = %d, target =%d",l,m,r,val,target);fflush(stdout); pos = l*bucksize; val = bitread (zoneoccs, pos, absBits); if (target ==val) return 1; //printf("\nPREV DEC BCS:: l= %d, val = %d, target =%d",l,val,target);fflush(stdout); //sequential decompression of bcs pos = bitread (zoneoccs, pos + absBits , bcBits); r=sampleK-1; for (k=0;k< r; k++) { BC_TO_DEC(bcs, pos, num); //num is the gap val +=num; //printf("\n decoding BCS:: gap= %d, val = %d, target =%d, k = %d,r = %d",num,val,target,k,r);fflush(stdout); if (val == target) return 1; if (val > target) return 0; //not found } return 0; }
uint8_t spi_transfer(_SPI *self, uint8_t val) { *(self->SPIxBUF) = (uint16_t)val; while (bitread(self->SPIxSTAT, 0)==0) {} return (uint8_t)(*(self->SPIxBUF)); }
/*------------------------------------------------------------------ * Given a Sequence of words, retuns the position in "words[] array" of each word. * memory for integerPattern is not allocate, sizeIntegers is usually a constant, and bounded previously * Returns ids of the words, and the number of ids in "integerPattern[]" ------------------------------------------------------------------*/ void parseTextIntoIntegers(twcsa *wcsa, byte *textPattern, uint patLen, uint *integerPattern, uint *sizeIntegers) { byte *pbeg,*pend,*wordstart,*aWord; register unsigned long size; uint index =0; pbeg = textPattern; pend = pbeg + patLen; while (pbeg <pend) { //parsing either a word or separator. size=0; wordstart = pbeg; if (_Valid[*pbeg]) { //alphanumerical data while ( (size<MAX_SIZE_OF_WORD) && (pbeg<pend)&& ( _Valid[*pbeg] )) {size++;pbeg++;} } else { if (*pbeg != ' ') { //a separator comes starting in ' ' comes, so it is a new word while ( (size<MAX_SIZE_OF_WORD) && (pbeg<pend) && (!_Valid[*pbeg] )) {size++;pbeg++;} } else { //a SPACE comes, so we have to test if next character is alphanumerical or not pbeg++; if (pbeg >= pend) {size++;} // a unique BLANK at the end of the file. else { if (_Valid [*pbeg] ) { wordstart = pbeg; //So skipping 1 blank character while ( (size<MAX_SIZE_OF_WORD) && (pbeg<pend)&& ( _Valid[*pbeg] )) {size++;pbeg++;} } else { // a "separator word" ... size++; //the prev BLANK... while ( (size<MAX_SIZE_OF_WORD) && (pbeg<pend) && (!_Valid[*pbeg] )) {size++;pbeg++;} }//else { // a "separator word" }//else ... not a unique BLANK AT THE END. }//else ... starting by a BLANK... } //The parsed word is "aWord", and its length is "size"... aWord=wordstart; // Binary search on the canonical words (wordsData) { uint len, tmplen; uchar *wcsaWord; register uint min,max,p; min = 0; max = (wcsa->nwords) - 1; while(min < max) { p = (min+max)/2; {//preparing for strcompL len = bitread (wcsa->wordsData.words, (p+1)* wcsa->wordsData.elemSize , wcsa->wordsData.elemSize); tmplen = bitread (wcsa->wordsData.words, (p )* wcsa->wordsData.elemSize , wcsa->wordsData.elemSize); len -=tmplen; wcsaWord= (byte *) wcsa->wordsData.wordsZoneMem.zone + tmplen; } //if(strncmp((char*)aWord, (char*)wcsa->wordsData[p].word,size) > 0) min = p+1; if(wtstrcompL(aWord, wcsaWord, size, len) > 0) min = p+1; else max = p; // { //SHOW PROGRESS // fprintf(stderr,"\n Patron = <<%s>>, curposWord= %d ==><<",aWord,p); // printWord(wcsaWord,len); fprintf(stderr,">> len =%d",len); // } } {//preparing for strcompL len = bitread (wcsa->wordsData.words, (min+1)* wcsa->wordsData.elemSize, wcsa->wordsData.elemSize); tmplen = bitread (wcsa->wordsData.words, ( min )* wcsa->wordsData.elemSize, wcsa->wordsData.elemSize); len -=tmplen; wcsaWord= (byte *) wcsa->wordsData.wordsZoneMem.zone + tmplen; } // if(!strncmp((char*)aWord, (char*)wcsa->wordsData[min].word, size)) { if(!wtstrcompL(aWord, wcsaWord, size, len)) { integerPattern[index++] = min ;//+1 ; //<-- } else {*sizeIntegers = 0; return;} // a valid word that does not appear in the source text. } }// end while *sizeIntegers = index; // //shows the parsed words: // {uint i; // printf("\n\n >>%s>> HA SIDO PARSEADO COMO:",textPattern); // for (i=0; i<index;i++) { // printf("<<%s>>",wcsa->wordsData[integerPattern[i] -1].word); // } // // } }
/**************************************************************************** * * ROUTINE : UnPackVideo * * INPUTS : None. * * OUTPUTS : None. * * RETURNS : None. * * FUNCTION : Upacks and decodes the video stream. * * SPECIAL NOTES : None. * * * ERRORS : None. * ****************************************************************************/ void UnPackVideo (PB_INSTANCE *pbi) { INT32 EncodedCoeffs = 1; INT32 FragIndex; INT32 * CodedBlockListPtr; INT32 * CodedBlockListEnd; UINT8 AcHuffIndex1; UINT8 AcHuffIndex2; UINT8 AcHuffChoice1; UINT8 AcHuffChoice2; UINT8 DcHuffChoice1; UINT8 DcHuffChoice2; // Bail out immediately if a decode error has already been reported. if ( pbi->DecoderErrorCode != NO_DECODER_ERROR ) return; // Clear down the array that indicates the current coefficient index for each block. memset(pbi->FragCoeffs, 0, pbi->UnitFragments); memset(pbi->FragCoefEOB, 0, pbi->UnitFragments); // Clear down the pbi->QFragData structure for all coded blocks. pbi->ClearDownQFrag(pbi); // Note the number of blocks to decode pbi->BlocksToDecode = pbi->CodedBlockIndex; // Get the DC huffman table choice for Y and then UV DcHuffChoice1 = (UINT8)bitread( &pbi->br, DC_HUFF_CHOICE_BITS ) + DC_HUFF_OFFSET; DcHuffChoice2 = (UINT8)bitread( &pbi->br, DC_HUFF_CHOICE_BITS ) + DC_HUFF_OFFSET; // UnPack DC coefficients / tokens CodedBlockListPtr = pbi->CodedBlockList; CodedBlockListEnd = &pbi->CodedBlockList[pbi->CodedBlockIndex]; while ( CodedBlockListPtr < CodedBlockListEnd ) { // Get the block data index FragIndex = *CodedBlockListPtr; pbi->FragCoefEOB[FragIndex] = pbi->FragCoeffs[FragIndex]; // Select the appropriate huffman table offset according to whether the token is fro am Y or UV block if ( FragIndex < (INT32)pbi->YPlaneFragments ) pbi->DcHuffChoice = DcHuffChoice1; else pbi->DcHuffChoice = DcHuffChoice2; // If we are in the middle of an EOB run if ( pbi->EOB_Run ) { // Mark the current block as fully expanded and decrement EOB_RUN count pbi->FragCoeffs[FragIndex] = BLOCK_SIZE; pbi->EOB_Run --; pbi->BlocksToDecode --; } // Else unpack a DC token else { UnpackAndExpandDcToken( pbi, pbi->QFragData[FragIndex], &pbi->FragCoeffs[FragIndex] ); } CodedBlockListPtr++; } // Get the AC huffman table choice for Y and then for UV. AcHuffIndex1 = (UINT8) bitread( &pbi->br, AC_HUFF_CHOICE_BITS ) + AC_HUFF_OFFSET; AcHuffIndex2 = (unsigned char) bitread( &pbi->br, AC_HUFF_CHOICE_BITS ) + AC_HUFF_OFFSET; // Unpack Lower AC coefficients. while ( EncodedCoeffs < 64 ) { // Repeatedly scan through the list of blocks. CodedBlockListPtr = pbi->CodedBlockList; CodedBlockListEnd = &pbi->CodedBlockList[pbi->CodedBlockIndex]; // Huffman table selection based upon which AC coefficient we are on if ( EncodedCoeffs <= AC_TABLE_2_THRESH ) { AcHuffChoice1 = AcHuffIndex1; AcHuffChoice2 = AcHuffIndex2; } else if ( EncodedCoeffs <= AC_TABLE_3_THRESH ) { AcHuffChoice1 = AcHuffIndex1 + AC_HUFF_CHOICES; AcHuffChoice2 = AcHuffIndex2 + AC_HUFF_CHOICES; } else if ( EncodedCoeffs <= AC_TABLE_4_THRESH ) { AcHuffChoice1 = AcHuffIndex1 + (AC_HUFF_CHOICES * 2); AcHuffChoice2 = AcHuffIndex2 + (AC_HUFF_CHOICES * 2); } else { AcHuffChoice1 = AcHuffIndex1 + (AC_HUFF_CHOICES * 3); AcHuffChoice2 = AcHuffIndex2 + (AC_HUFF_CHOICES * 3); } while( CodedBlockListPtr < CodedBlockListEnd ) { // Get the linear index for the current fragment. FragIndex = *CodedBlockListPtr; // Should we decode a token for this block on this pass. if ( pbi->FragCoeffs[FragIndex] <= EncodedCoeffs ) { pbi->FragCoefEOB[FragIndex] = pbi->FragCoeffs[FragIndex]; // If we are in the middle of an EOB run if ( pbi->EOB_Run ) { // Mark the current block as fully expanded and decrement EOB_RUN count pbi->FragCoeffs[FragIndex] = BLOCK_SIZE; pbi->EOB_Run --; pbi->BlocksToDecode --; } // Else unpack an AC token else { // Work out which huffman table to use, then decode a token if ( FragIndex < (INT32)pbi->YPlaneFragments ) pbi->ACHuffChoice = AcHuffChoice1; else pbi->ACHuffChoice = AcHuffChoice2; UnpackAndExpandAcToken( pbi, pbi->QFragData[FragIndex], &pbi->FragCoeffs[FragIndex] ); } } CodedBlockListPtr++; } // Test for condition where there are no blocks left with any tokesn to decode if ( !pbi->BlocksToDecode ) break; EncodedCoeffs ++; } }
/** * Binary searches a target value in the list of the idforList-th word. */ int fsearch(void * index, int target, uint idforList) { twcsa* wcsa = (twcsa *) index; uint l,r,m,val,num; uint pos,k; uint lenList = wcsa->lenList[idforList]; uint sampleK = wcsa->occs.sampleK; uint bucksize = wcsa->occs.occsBucketSize; uint absBits = wcsa->occs.absOffsetSize; uint bcBits = wcsa->occs.bcOffsetSize; uint firstpos = wcsa->occs.occs[idforList]; uint *zoneoccs = wcsa->occs.zoneOccs; byte *bcs = wcsa->occs.bcs; l = firstpos/bucksize; r= l+ (lenList +sampleK -1)/sampleK -1; //After the last sampled absolute pos: ==> sequential decompression after there. val = bitread (zoneoccs, r*bucksize, absBits); //the last Abs sample if (val <= target) { if (val ==target) return 1; pos = bitread (zoneoccs, r*bucksize + absBits , bcBits); l = 0; r = lenList%sampleK; //number of codewords to decode (at most) while (l<r) { //decodes sequentially BC_TO_DEC(bcs, pos, num); val += num; if (val ==target) return 1; // found!!. if (val > target) return 0; //not found l++; } return 0; } //Before the last sampled absolute pos:: //printf("\nTO BIN SEARCH:: l= %d, r=%d, val = %d, target =%d",l,r,val,target);fflush(stdout); while (r>(l+1)) { m = (l + r) / 2; pos = m * bucksize; val = bitread (zoneoccs, pos, absBits); if (val < target) l = m; else if (val == target) return 1; else r = m; } //decoding from pos = l * bucksize; val = bitread (zoneoccs, pos, absBits); if (target ==val) return 1; //printf("\nPREV DEC BCS:: l= %d, val = %d, target =%d",l,val,target);fflush(stdout); //sequential decompression of bcs pos = bitread (zoneoccs, pos + absBits , bcBits); r=sampleK-1; for (k=0;k< r; k++) { BC_TO_DEC(bcs, pos, num); //num is the gap val +=num; //printf("\n decoding BCS:: gap= %d, val = %d, target =%d, k = %d,r = %d",num,val,target,k,r);fflush(stdout); if (val == target) return 1; if (val > target) return 0; //not found } return 0; }
/**************************************************************************** * * ROUTINE : DecodeModes * * INPUTS : None. * * OUTPUTS : Reconstructed frame. * * RETURNS : None. * * FUNCTION : Decodes the coding mode list for this frame. * * SPECIAL NOTES : None. * * * ERRORS : None. * ****************************************************************************/ void DecodeModes( PB_INSTANCE *pbi, UINT32 SBRows, UINT32 SBCols, UINT32 HExtra, UINT32 VExtra ) { INT32 FragIndex; // Fragment number UINT32 MB; // Macro-Block, Block indices UINT32 SBrow; // Super-Block row number UINT32 SBcol; // Super-Block row number UINT32 SB=0; // Super-Block index CODING_MODE CodingMethod; // Temp Storage for coding mode. UINT32 UVRow; UINT32 UVColumn; UINT32 UVFragOffset; UINT32 CodingScheme; // Coding scheme used to code modes. UINT32 MBListIndex = 0; UINT32 i; // If the frame is an intra frame then all blocks have mode intra. if ( GetFrameType(pbi) == BASE_FRAME ) { for ( i = 0; i < pbi->UnitFragments; i++ ) { pbi->FragCodingMethod[i] = CODE_INTRA; } } else { UINT32 ModeEntry; // Mode bits read // Read the coding method CodingScheme = bitread( &pbi->br, MODE_METHOD_BITS ); // If the coding method is method 0 then we have to read in a custom coding scheme if ( CodingScheme == 0 ) { // Read the coding scheme. for ( i = 0; i < MAX_MODES; i++ ) { ModeAlphabet[0][ bitread( &pbi->br, MODE_BITS) ] = (CODING_MODE)i; } } // Unravel the quad-tree for ( SBrow=0; SBrow<SBRows; SBrow++ ) { for ( SBcol=0; SBcol<SBCols; SBcol++ ) { for ( MB=0; MB<4; MB++ ) { // There may be MB's lying out of frame // which must be ignored. For these MB's // top left block will have a negative Fragment Index. if ( QuadMapToMBTopLeft(pbi->BlockMap, SB,MB) >= 0 ) { // Is the Macro-Block coded: if ( pbi->MBCodedFlags[MBListIndex++] ) { // Upack the block level modes and motion vectors FragIndex = QuadMapToMBTopLeft( pbi->BlockMap, SB, MB ); // Unpack the mode. if ( CodingScheme == (MODE_METHODS-1) ) { // This is the fall back coding scheme. // Simply MODE_BITS bits per mode entry. CodingMethod = (CODING_MODE)bitread( &pbi->br, MODE_BITS ); } else { ModeEntry = FrArrayUnpackMode(pbi); CodingMethod = ModeAlphabet[CodingScheme][ ModeEntry ]; } // Note the coding mode for each block in macro block. pbi->FragCodingMethod[FragIndex] = CodingMethod; pbi->FragCodingMethod[FragIndex + 1] = CodingMethod; pbi->FragCodingMethod[FragIndex + pbi->HFragments] = CodingMethod; pbi->FragCodingMethod[FragIndex + pbi->HFragments + 1] = CodingMethod; // Matching fragments in the U and V planes UVRow = (FragIndex / (pbi->HFragments * 2)); UVColumn = (FragIndex % pbi->HFragments) / 2; UVFragOffset = (UVRow * (pbi->HFragments / 2)) + UVColumn; pbi->FragCodingMethod[pbi->YPlaneFragments + UVFragOffset] = CodingMethod; pbi->FragCodingMethod[pbi->YPlaneFragments + pbi->UVPlaneFragments + UVFragOffset] = CodingMethod; } } } // Next Super-Block SB++; } } } }