📄 ac_bitplanecoding.c
字号:
DWORD32 TempWord = 0;
RemainderBits = PtrCoding->Bits->SegBitCounter
- (PtrCoding->PtrHeader->Header.Part2.SegByteLimit_27Bits <<3);
while(RemainderBits > 0)
{
BitsRead(PtrCoding, &TempWord, 1);
if(PtrCoding->SegmentFull == TRUE)
return;
}
}
}
return;
}
void ACGaggleDecoding(StructCodingPara *PtrCoding,
BitPlaneBits *BlockInfo,
int StartIndex,
int gaggles,
int Max_k,
short ID_Length)
{
short i;
short j;
UCHAR8 temp_bit = 0;
UCHAR8 min_k;
DWORD32 TempWord =0;
int counter = 0;
UCHAR8 ones = ~0;
BOOL uncoded = FALSE;
if (gaggles == 0)
return;
// makes the least ID_Length bits to ones.
ones = ((ones << (8 - ID_Length)) >> ((8 - ID_Length)));
BitsRead(PtrCoding, &TempWord, ID_Length);
min_k = (UCHAR8) TempWord;
if((ID_Length == 1 && min_k == 1) ||
(ID_Length == 2 && min_k == 3) ||
(ID_Length == 3 && min_k == 7) ||
(ID_Length == 4 && min_k == 15))
uncoded = TRUE;
if(StartIndex == 1)
{// first read the reference
BitsRead(PtrCoding, &TempWord, PtrCoding->N);
BlockInfo[0].MappedAC = (WORD16) TempWord;
}
if(PtrCoding->RateReached == TRUE)
return;
// if uncoded. read the symbols directly.
if(uncoded == TRUE)
{
for (i = StartIndex; i < StartIndex + gaggles; i ++)
{
BitsRead(PtrCoding, &TempWord, PtrCoding->N);
BlockInfo[i].MappedAC = (WORD16)TempWord;
}
return;
}
// If coded. read the first part of each DC first
for( i = StartIndex; i < StartIndex + gaggles; i ++)
{
counter = 0;
// rice decoding. determined 0s
for(;;)
{
BitsRead(PtrCoding, &TempWord, 1);
if(TempWord == 0)
{
counter ++;
if(PtrCoding->RateReached == TRUE)
break;
}
else
break;
}
if(PtrCoding->RateReached == TRUE)
break;
BlockInfo[i].MappedAC = counter;
BlockInfo[i].MappedAC <<= min_k;
}
if(PtrCoding->RateReached == TRUE)
return;
// then the second part.
for( i = StartIndex; i < StartIndex + gaggles; i ++)
{
j = BitsRead(PtrCoding, &TempWord, min_k);
BlockInfo[i].MappedAC += (WORD16)TempWord;
if(PtrCoding->RateReached == TRUE)
break;
}
return;
}
void DPCM_ACDeMapper(BitPlaneBits *BlockCodingInfo, //UCHAR8 *ACmax_blocks,
int size,
short N)
{
short * diff_AC;
short theta = 0;
long i;
int X_Min = 0;
int X_Max = ((1 << N) - 1) ;
diff_AC = (short *)calloc(size,sizeof(short));
BlockCodingInfo[0].BitMaxAC = (UCHAR8)BlockCodingInfo[0].MappedAC;
for ( i = 1; i < size; i ++)
{
theta = min(BlockCodingInfo[i-1].BitMaxAC - X_Min, X_Max - BlockCodingInfo[i-1].BitMaxAC );
if((float)BlockCodingInfo[i].MappedAC / 2 == BlockCodingInfo[i].MappedAC / 2)
{
diff_AC[i] = (short)(BlockCodingInfo[i].MappedAC / 2);
if(diff_AC[i] >= 0 && diff_AC[i] <= theta)
{
BlockCodingInfo[i].BitMaxAC = diff_AC[i] + BlockCodingInfo[i - 1].BitMaxAC;
continue;
}
}
else
{
diff_AC[i] = - (short)((BlockCodingInfo[i].MappedAC + 1) / 2) ;
if(diff_AC[i] <= 0 && diff_AC[i] >= -theta)
{
BlockCodingInfo[i].BitMaxAC = diff_AC[i] + BlockCodingInfo[i - 1].BitMaxAC;
continue;
}
}
diff_AC[i] = (short)(BlockCodingInfo[i].MappedAC - theta);
BlockCodingInfo[i].BitMaxAC = diff_AC[i] + BlockCodingInfo[i - 1].BitMaxAC;
if( BlockCodingInfo[i].BitMaxAC < X_Min || BlockCodingInfo[i].BitMaxAC > X_Max)
{
diff_AC[i] = -diff_AC[i];
BlockCodingInfo[i].BitMaxAC = diff_AC[i] + BlockCodingInfo[i - 1].BitMaxAC;
}
}
free(diff_AC);
return;
}
short ACDepthDecoder(StructCodingPara *PtrCoding,
BitPlaneBits *BlockInfo)
{
SINT Max_k = 0;
UINT32 GaggleStartIndex = 0;
UCHAR8 ID_Length = 0;
WORD16 gaggles = 0;
DOUBLE4 temp = 0;
temp = log10(1 + PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits)/log10(2);
if (temp - (short)temp > 0 )
PtrCoding->N = (UCHAR8)(temp + 1);
else
PtrCoding->N = (UCHAR8)temp;
if ( PtrCoding->N == 2)
{
Max_k = 0;
ID_Length = 1;
}
else if (PtrCoding->N <=4)
{
Max_k = 2;
ID_Length = 2;
}
else if (PtrCoding->N <=5)
{
Max_k = 6;
ID_Length = 3;
}
else
{
ErrorMsg(BPE_DATA_ERROR);
}
gaggles = GAGGLE_SIZE - 1;
PtrCoding->PtrHeader->Header.Part3.S_20Bits;
if (PtrCoding->PtrHeader->Header.Part3.S_20Bits < gaggles)
gaggles = (WORD16) PtrCoding->PtrHeader->Header.Part3.S_20Bits - 1;
GaggleStartIndex = 1;
if(PtrCoding->RateReached == TRUE)
return BPE_OK;
ACGaggleDecoding(PtrCoding, BlockInfo, GaggleStartIndex, gaggles, Max_k, ID_Length);
GaggleStartIndex += gaggles;
gaggles = GAGGLE_SIZE;
while(PtrCoding->PtrHeader->Header.Part3.S_20Bits - GaggleStartIndex >= gaggles)
{
ACGaggleDecoding(PtrCoding, BlockInfo, GaggleStartIndex,
gaggles, Max_k, ID_Length);
GaggleStartIndex += gaggles;
}
if(PtrCoding->RateReached == TRUE)
return BPE_OK;
gaggles = (WORD16)(PtrCoding->PtrHeader->Header.Part3.S_20Bits - GaggleStartIndex);
ACGaggleDecoding(PtrCoding, BlockInfo, GaggleStartIndex,
gaggles, Max_k, ID_Length);
if(PtrCoding->RateReached == TRUE)
return BPE_OK;
DPCM_ACDeMapper(BlockInfo, PtrCoding->PtrHeader->Header.Part3.S_20Bits, PtrCoding->N);
return BPE_OK;
}
void ACBpeDecoding(StructCodingPara *PtrCoding,
BitPlaneBits *BlockCodingInfo)
{
UINT32 i = 0;
DWORD32 TempWord = 0;
UCHAR8 BitPlane = 0;
/////////// 3. Organize the AC component/////////////////////////////
// 3.1 Specify the number of bit planes in each block.
if(PtrCoding->RateReached == TRUE)
return;
if (PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits != 0)
// Bit plane scan is not necessary if PtrHeader->Header.Part1.BitDepthAC_5Bits = 0.
{
if (PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits == 1)
{ //only the lowest bit plane has valud AC component manitude.
DWORD32 TempWord;
for ( i = 0; i < PtrCoding->PtrHeader->Header.Part3.S_20Bits; i++)
{
BitsRead(PtrCoding, &TempWord, 1);
BlockCodingInfo[i].BitMaxAC = (UCHAR8)TempWord;
}
}
else
ACDepthDecoder(PtrCoding, BlockCodingInfo);
}
if(PtrCoding->SegmentFull == TRUE)
return ;
// 3.2 Family tree scaning sequence in a segment.
if (PtrCoding->PtrHeader->Header.Part4.DWTType == INTEGER_WAVELET)
{
short k = 0;
UINT32 p = 0;
short DCPlanes = 0;
if(PtrCoding->QuantizationFactorQ - PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits > 0)
{
if (PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits > PtrCoding->PtrHeader->Header.Part4.CustomWtLL3_2bits)
{
DCPlanes = (PtrCoding->QuantizationFactorQ - PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits);
for ( k = DCPlanes; k > 0; k --)
{
for( p = 0; p < PtrCoding->PtrHeader->Header.Part3.S_20Bits; p++)
{
BitsRead(PtrCoding, &TempWord, 1);
BlockCodingInfo[p].DecodingDCRemainder += (WORD16)(TempWord << (k + PtrCoding->PtrHeader->Header.Part4.CustomWtLL3_2bits -1)) ;
if(PtrCoding->SegmentFull == TRUE)
return;
}
}
}
else if (PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits < PtrCoding->PtrHeader->Header.Part4.CustomWtLL3_2bits)
{
DCPlanes = (PtrCoding->QuantizationFactorQ - PtrCoding->PtrHeader->Header.Part4.CustomWtLL3_2bits);
for ( k = DCPlanes; k > 0; k --)
{
for( p = 0; p < PtrCoding->PtrHeader->Header.Part3.S_20Bits; p++)
{
BitsRead(PtrCoding, &TempWord, 1);
BlockCodingInfo[p].DecodingDCRemainder += (WORD16)(TempWord << (k + PtrCoding->PtrHeader->Header.Part4.CustomWtLL3_2bits -1)) ;
}
}
}
}
}
else
{
short k = 0;
UINT32 p = 0;
for ( k = PtrCoding->QuantizationFactorQ - PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits; k > 0; k --)
{
for( p = 0; p < PtrCoding->PtrHeader->Header.Part3.S_20Bits; p++)
{
BitsRead(PtrCoding, &TempWord, 1);
BlockCodingInfo[p].DecodingDCRemainder += (WORD16)(TempWord << ( k + PtrCoding->PtrHeader->Header.Part4.CustomWtLL3_2bits - 1)) ;
}
}
}
for ( BitPlane = PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits; BitPlane > 0; BitPlane --)
{
if(PtrCoding->SegmentFull == TRUE || PtrCoding->RateReached == TRUE)
return;
PtrCoding->BitPlane = BitPlane;
if (PtrCoding->PtrHeader->Header.Part4.DWTType == INTEGER_WAVELET)
{
if ((BitPlane <= PtrCoding->QuantizationFactorQ) &&
(PtrCoding->QuantizationFactorQ >
PtrCoding->PtrHeader->Header.Part4.CustomWtLL3_2bits ) &&
PtrCoding->PtrHeader->Header.Part4.CustomWtLL3_2bits < BitPlane)
{
// output the DC bits that have been shifted out
for(i = 0; i < PtrCoding->PtrHeader->Header.Part3.S_20Bits;i++)
{
if(PtrCoding->SegmentFull == TRUE)
break;
BitsRead(PtrCoding, &TempWord, 1);
BlockCodingInfo[i].DecodingDCRemainder += (WORD16)(TempWord << (BitPlane - 1)) ;
}
}
}
else
{
if (BitPlane <= PtrCoding->QuantizationFactorQ)
{
// output the DC bits that have been shifted out
for(i = 0; i < PtrCoding->PtrHeader->Header.Part3.S_20Bits;i++)
{
if(PtrCoding->SegmentFull == TRUE)
break;
BitsRead(PtrCoding, &TempWord, 1);
BlockCodingInfo[i].DecodingDCRemainder += (WORD16)(TempWord << (BitPlane - 1)) ;
}
}
}
if (BitPlane <= 3)
BitPlane = BitPlane;
if(PtrCoding->SegmentFull != TRUE)
StagesDeCoding(PtrCoding, BlockCodingInfo);
}
CheckUsefill(PtrCoding);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -