libpgf  6.12.24
PGF - Progressive Graphics File
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CEncoder::CMacroBlock Class Reference

A macro block is an encoding unit of fixed size (uncoded) More...

Public Member Functions

 CMacroBlock (CEncoder *encoder)
 
void Init (int lastLevelIndex)
 
void BitplaneEncode ()
 

Public Attributes

DataT m_value [BufferSize]
 input buffer of values with index m_valuePos More...
 
UINT32 m_codeBuffer [CodeBufferLen]
 output buffer for encoded bitstream More...
 
ROIBlockHeader m_header
 block header More...
 
UINT32 m_valuePos
 current buffer position More...
 
UINT32 m_maxAbsValue
 maximum absolute coefficient in each buffer More...
 
UINT32 m_codePos
 current position in encoded bitstream More...
 
int m_lastLevelIndex
 index of last encoded level: [0, nLevels); used because a level-end can occur before a buffer is full More...
 

Private Member Functions

UINT32 RLESigns (UINT32 codePos, UINT32 *signBits, UINT32 signLen)
 
UINT32 DecomposeBitplane (UINT32 bufferSize, UINT32 planeMask, UINT32 codePos, UINT32 *sigBits, UINT32 *refBits, UINT32 *signBits, UINT32 &signLen, UINT32 &codeLen)
 
UINT8 NumberOfBitplanes ()
 
bool GetBitAtPos (UINT32 pos, UINT32 planeMask) const
 

Private Attributes

CEncoderm_encoder
 
bool m_sigFlagVector [BufferSize+1]
 

Detailed Description

A macro block is an encoding unit of fixed size (uncoded)

PGF encoder macro block class.

Author
C. Stamm, I. Bauersachs

Definition at line 51 of file Encoder.h.

Constructor & Destructor Documentation

CEncoder::CMacroBlock::CMacroBlock ( CEncoder encoder)
inline

Constructor: Initializes new macro block.

Parameters
encoderPointer to outer class.

Definition at line 56 of file Encoder.h.

57  : m_header(0)
58  , m_encoder(encoder)
59  {
60  ASSERT(m_encoder);
61  Init(-1);
62  }
CEncoder * m_encoder
Definition: Encoder.h:94
ROIBlockHeader m_header
block header
Definition: Encoder.h:82
void Init(int lastLevelIndex)
Definition: Encoder.h:67

Member Function Documentation

void CEncoder::CMacroBlock::BitplaneEncode ( )

Encodes this macro block into internal code buffer. Several macro blocks can be encoded in parallel. Call CEncoder::WriteMacroBlock after this method.

Definition at line 468 of file Encoder.cpp.

468  {
469  UINT8 nPlanes;
470  UINT32 sigLen, codeLen = 0, wordPos, refLen, signLen;
471  UINT32 sigBits[BufferLen] = { 0 };
472  UINT32 refBits[BufferLen] = { 0 };
473  UINT32 signBits[BufferLen] = { 0 };
474  UINT32 planeMask;
475  UINT32 bufferSize = m_header.rbh.bufferSize; ASSERT(bufferSize <= BufferSize);
476  bool useRL;
477 
478 #ifdef TRACE
479  //printf("which thread: %d\n", omp_get_thread_num());
480 #endif
481 
482  // clear significance vector
483  for (UINT32 k=0; k < bufferSize; k++) {
484  m_sigFlagVector[k] = false;
485  }
486  m_sigFlagVector[bufferSize] = true; // sentinel
487 
488  // clear output buffer
489  for (UINT32 k=0; k < bufferSize; k++) {
490  m_codeBuffer[k] = 0;
491  }
492  m_codePos = 0;
493 
494  // compute number of bit planes and split buffer into separate bit planes
495  nPlanes = NumberOfBitplanes();
496 
497  // write number of bit planes to m_codeBuffer
498  // <nPlanes>
501 
502  // loop through all bit planes
503  if (nPlanes == 0) nPlanes = MaxBitPlanes + 1;
504  planeMask = 1 << (nPlanes - 1);
505 
506  for (int plane = nPlanes - 1; plane >= 0; plane--) {
507  // clear significant bitset
508  for (UINT32 k=0; k < BufferLen; k++) {
509  sigBits[k] = 0;
510  }
511 
512  // split bitplane in significant bitset and refinement bitset
513  sigLen = DecomposeBitplane(bufferSize, planeMask, m_codePos + RLblockSizeLen + 1, sigBits, refBits, signBits, signLen, codeLen);
514 
515  if (sigLen > 0 && codeLen <= MaxCodeLen && codeLen < AlignWordPos(sigLen) + AlignWordPos(signLen) + 2*RLblockSizeLen) {
516  // set RL code bit
517  // <1><codeLen>
519 
520  // write length codeLen to m_codeBuffer
522  m_codePos += RLblockSizeLen + codeLen;
523  } else {
524  #ifdef TRACE
525  //printf("new\n");
526  //for (UINT32 i=0; i < bufferSize; i++) {
527  // printf("%s", (GetBit(sigBits, i)) ? "1" : "_");
528  // if (i%120 == 119) printf("\n");
529  //}
530  //printf("\n");
531  #endif // TRACE
532 
533  // run-length coding wasn't efficient enough
534  // we don't use RL coding for sigBits
535  // <0><sigLen>
537 
538  // write length sigLen to m_codeBuffer
539  ASSERT(sigLen <= MaxCodeLen);
542 
543  if (m_encoder->m_favorSpeed || signLen == 0) {
544  useRL = false;
545  } else {
546  // overwrite m_codeBuffer
547  useRL = true;
548  // run-length encode m_sign and append them to the m_codeBuffer
549  codeLen = RLESigns(m_codePos + RLblockSizeLen + 1, signBits, signLen);
550  }
551 
552  if (useRL && codeLen <= MaxCodeLen && codeLen < signLen) {
553  // RL encoding of m_sign was efficient
554  // <1><codeLen><codedSignBits>_
555  // write RL code bit
557 
558  // write codeLen to m_codeBuffer
560 
561  // compute position of sigBits
562  wordPos = NumberOfWords(m_codePos + RLblockSizeLen + codeLen);
563  ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
564  } else {
565  // RL encoding of signBits wasn't efficient
566  // <0><signLen>_<signBits>_
567  // clear RL code bit
569 
570  // write signLen to m_codeBuffer
571  ASSERT(signLen <= MaxCodeLen);
573 
574  // write signBits to m_codeBuffer
576  ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
577  codeLen = NumberOfWords(signLen);
578 
579  for (UINT32 k=0; k < codeLen; k++) {
580  m_codeBuffer[wordPos++] = signBits[k];
581  }
582  }
583 
584  // write sigBits
585  // <sigBits>_
586  ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
587  refLen = NumberOfWords(sigLen);
588 
589  for (UINT32 k=0; k < refLen; k++) {
590  m_codeBuffer[wordPos++] = sigBits[k];
591  }
592  m_codePos = wordPos << WordWidthLog;
593  }
594 
595  // append refinement bitset (aligned to word boundary)
596  // _<refBits>
597  wordPos = NumberOfWords(m_codePos);
598  ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
599  refLen = NumberOfWords(bufferSize - sigLen);
600 
601  for (UINT32 k=0; k < refLen; k++) {
602  m_codeBuffer[wordPos++] = refBits[k];
603  }
604  m_codePos = wordPos << WordWidthLog;
605  planeMask >>= 1;
606  }
607  ASSERT(0 <= m_codePos && m_codePos <= CodeBufferBitLen);
608 }
struct ROIBlockHeader::RBH rbh
ROI block header.
UINT16 bufferSize
number of uncoded UINT32 values in a block
Definition: PGFtypes.h:167
UINT8 NumberOfBitplanes()
Definition: Encoder.cpp:736
UINT32 AlignWordPos(UINT32 pos)
Definition: BitStream.h:260
#define BufferSize
must be a multiple of WordWidth
Definition: PGFtypes.h:77
UINT32 m_codePos
current position in encoded bitstream
Definition: Encoder.h:85
UINT32 RLESigns(UINT32 codePos, UINT32 *signBits, UINT32 signLen)
Definition: Encoder.cpp:760
#define RLblockSizeLen
block size length (< 16): ld(BufferSize) < RLblockSizeLen <= 2*ld(BufferSize)
Definition: PGFtypes.h:78
bool m_sigFlagVector[BufferSize+1]
Definition: Encoder.h:95
#define WordWidthLog
ld of WordWidth
Definition: PGFplatform.h:74
#define BufferLen
number of words per buffer
Definition: Decoder.h:39
void ClearBit(UINT32 *stream, UINT32 pos)
Definition: BitStream.h:56
#define MaxBitPlanesLog
number of bits to code the maximum number of bit planes (in 32 or 16 bit mode)
Definition: PGFtypes.h:86
UINT32 DecomposeBitplane(UINT32 bufferSize, UINT32 planeMask, UINT32 codePos, UINT32 *sigBits, UINT32 *refBits, UINT32 *signBits, UINT32 &signLen, UINT32 &codeLen)
Definition: Encoder.cpp:620
CEncoder * m_encoder
Definition: Encoder.h:94
UINT32 m_codeBuffer[CodeBufferLen]
output buffer for encoded bitstream
Definition: Encoder.h:81
void SetBit(UINT32 *stream, UINT32 pos)
Definition: BitStream.h:48
UINT32 NumberOfWords(UINT32 pos)
Definition: BitStream.h:269
#define MaxBitPlanes
maximum number of bit planes of m_value: 32 minus sign bit
Definition: PGFtypes.h:82
bool m_favorSpeed
favor speed over size
Definition: Encoder.h:218
void SetValueBlock(UINT32 *stream, UINT32 pos, UINT32 val, UINT32 k)
Definition: BitStream.h:102
ROIBlockHeader m_header
block header
Definition: Encoder.h:82
#define CodeBufferLen
number of words in code buffer (CodeBufferLen > BufferLen)
Definition: Decoder.h:40
#define MaxCodeLen
max length of RL encoded block
Definition: Encoder.cpp:59
#define CodeBufferBitLen
max number of bits in m_codeBuffer
Definition: Encoder.cpp:58
UINT32 CEncoder::CMacroBlock::DecomposeBitplane ( UINT32  bufferSize,
UINT32  planeMask,
UINT32  codePos,
UINT32 *  sigBits,
UINT32 *  refBits,
UINT32 *  signBits,
UINT32 &  signLen,
UINT32 &  codeLen 
)
private

Definition at line 620 of file Encoder.cpp.

620  {
621  ASSERT(sigBits);
622  ASSERT(refBits);
623  ASSERT(signBits);
624  ASSERT(codePos < CodeBufferBitLen);
625 
626  UINT32 sigPos = 0;
627  UINT32 valuePos = 0, valueEnd;
628  UINT32 refPos = 0;
629 
630  // set output value
631  signLen = 0;
632 
633  // prepare RLE of Sigs and Signs
634  const UINT32 outStartPos = codePos;
635  UINT32 k = 3;
636  UINT32 runlen = 1 << k; // = 2^k
637  UINT32 count = 0;
638 
639  while (valuePos < bufferSize) {
640  // search next 1 in m_sigFlagVector using searching with sentinel
641  valueEnd = valuePos;
642  while(!m_sigFlagVector[valueEnd]) { valueEnd++; }
643 
644  // search 1's in m_value[plane][valuePos..valueEnd)
645  // these 1's are significant bits
646  while (valuePos < valueEnd) {
647  if (GetBitAtPos(valuePos, planeMask)) {
648  // RLE encoding
649  // encode run of count 0's followed by a 1
650  // with codeword: 1<count>(signBits[signPos])
651  SetBit(m_codeBuffer, codePos++);
652  if (k > 0) {
653  SetValueBlock(m_codeBuffer, codePos, count, k);
654  codePos += k;
655 
656  // adapt k (half the zero run-length)
657  k--;
658  runlen >>= 1;
659  }
660 
661  // copy and write sign bit
662  if (m_value[valuePos] < 0) {
663  SetBit(signBits, signLen++);
664  SetBit(m_codeBuffer, codePos++);
665  } else {
666  ClearBit(signBits, signLen++);
667  ClearBit(m_codeBuffer, codePos++);
668  }
669 
670  // write a 1 to sigBits
671  SetBit(sigBits, sigPos++);
672 
673  // update m_sigFlagVector
674  m_sigFlagVector[valuePos] = true;
675 
676  // prepare for next run
677  count = 0;
678  } else {
679  // RLE encoding
680  count++;
681  if (count == runlen) {
682  // encode run of 2^k zeros by a single 0
683  ClearBit(m_codeBuffer, codePos++);
684  // adapt k (double the zero run-length)
685  if (k < WordWidth) {
686  k++;
687  runlen <<= 1;
688  }
689 
690  // prepare for next run
691  count = 0;
692  }
693 
694  // write 0 to sigBits
695  sigPos++;
696  }
697  valuePos++;
698  }
699  // refinement bit
700  if (valuePos < bufferSize) {
701  // write one refinement bit
702  if (GetBitAtPos(valuePos++, planeMask)) {
703  SetBit(refBits, refPos);
704  } else {
705  ClearBit(refBits, refPos);
706  }
707  refPos++;
708  }
709  }
710  // RLE encoding of the rest of the plane
711  // encode run of count 0's followed by a 1
712  // with codeword: 1<count>(signBits[signPos])
713  SetBit(m_codeBuffer, codePos++);
714  if (k > 0) {
715  SetValueBlock(m_codeBuffer, codePos, count, k);
716  codePos += k;
717  }
718  // write dmmy sign bit
719  SetBit(m_codeBuffer, codePos++);
720 
721  // write word filler zeros
722 
723  ASSERT(sigPos <= bufferSize);
724  ASSERT(refPos <= bufferSize);
725  ASSERT(signLen <= bufferSize);
726  ASSERT(valuePos == bufferSize);
727  ASSERT(codePos >= outStartPos && codePos < CodeBufferBitLen);
728  codeLen = codePos - outStartPos;
729 
730  return sigPos;
731 }
bool GetBitAtPos(UINT32 pos, UINT32 planeMask) const
Definition: Encoder.h:92
bool m_sigFlagVector[BufferSize+1]
Definition: Encoder.h:95
void ClearBit(UINT32 *stream, UINT32 pos)
Definition: BitStream.h:56
UINT32 m_codeBuffer[CodeBufferLen]
output buffer for encoded bitstream
Definition: Encoder.h:81
void SetBit(UINT32 *stream, UINT32 pos)
Definition: BitStream.h:48
void SetValueBlock(UINT32 *stream, UINT32 pos, UINT32 val, UINT32 k)
Definition: BitStream.h:102
DataT m_value[BufferSize]
input buffer of values with index m_valuePos
Definition: Encoder.h:80
#define WordWidth
WordBytes*8.
Definition: PGFplatform.h:73
#define CodeBufferBitLen
max number of bits in m_codeBuffer
Definition: Encoder.cpp:58
bool CEncoder::CMacroBlock::GetBitAtPos ( UINT32  pos,
UINT32  planeMask 
) const
inlineprivate

Definition at line 92 of file Encoder.h.

92 { return (abs(m_value[pos]) & planeMask) > 0; }
DataT m_value[BufferSize]
input buffer of values with index m_valuePos
Definition: Encoder.h:80
void CEncoder::CMacroBlock::Init ( int  lastLevelIndex)
inline

Reinitialzes this macro block (allows reusage).

Parameters
lastLevelIndexLevel length directory index of last encoded level: [0, nLevels)

Definition at line 67 of file Encoder.h.

67  { // initialize for reusage
68  m_valuePos = 0;
69  m_maxAbsValue = 0;
70  m_codePos = 0;
71  m_lastLevelIndex = lastLevelIndex;
72  }
UINT32 m_valuePos
current buffer position
Definition: Encoder.h:83
int m_lastLevelIndex
index of last encoded level: [0, nLevels); used because a level-end can occur before a buffer is full...
Definition: Encoder.h:86
UINT32 m_maxAbsValue
maximum absolute coefficient in each buffer
Definition: Encoder.h:84
UINT32 m_codePos
current position in encoded bitstream
Definition: Encoder.h:85
UINT8 CEncoder::CMacroBlock::NumberOfBitplanes ( )
private

Definition at line 736 of file Encoder.cpp.

736  {
737  UINT8 cnt = 0;
738 
739  // determine number of bitplanes for max value
740  if (m_maxAbsValue > 0) {
741  while (m_maxAbsValue > 0) {
742  m_maxAbsValue >>= 1; cnt++;
743  }
744  if (cnt == MaxBitPlanes + 1) cnt = 0;
745  // end cs
746  ASSERT(cnt <= MaxBitPlanes);
747  ASSERT((cnt >> MaxBitPlanesLog) == 0);
748  return cnt;
749  } else {
750  return 1;
751  }
752 }
UINT32 m_maxAbsValue
maximum absolute coefficient in each buffer
Definition: Encoder.h:84
#define MaxBitPlanesLog
number of bits to code the maximum number of bit planes (in 32 or 16 bit mode)
Definition: PGFtypes.h:86
#define MaxBitPlanes
maximum number of bit planes of m_value: 32 minus sign bit
Definition: PGFtypes.h:82
UINT32 CEncoder::CMacroBlock::RLESigns ( UINT32  codePos,
UINT32 *  signBits,
UINT32  signLen 
)
private

Definition at line 760 of file Encoder.cpp.

760  {
761  ASSERT(signBits);
762  ASSERT(0 <= codePos && codePos < CodeBufferBitLen);
763  ASSERT(0 < signLen && signLen <= BufferSize);
764 
765  const UINT32 outStartPos = codePos;
766  UINT32 k = 0;
767  UINT32 runlen = 1 << k; // = 2^k
768  UINT32 count = 0;
769  UINT32 signPos = 0;
770 
771  while (signPos < signLen) {
772  // search next 0 in signBits starting at position signPos
773  count = SeekBit1Range(signBits, signPos, __min(runlen, signLen - signPos));
774  // count 1's found
775  if (count == runlen) {
776  // encode run of 2^k ones by a single 1
777  signPos += count;
778  SetBit(m_codeBuffer, codePos++);
779  // adapt k (double the 1's run-length)
780  if (k < WordWidth) {
781  k++;
782  runlen <<= 1;
783  }
784  } else {
785  // encode run of count 1's followed by a 0
786  // with codeword: 0(count)
787  signPos += count + 1;
788  ClearBit(m_codeBuffer, codePos++);
789  if (k > 0) {
790  SetValueBlock(m_codeBuffer, codePos, count, k);
791  codePos += k;
792  }
793  // adapt k (half the 1's run-length)
794  if (k > 0) {
795  k--;
796  runlen >>= 1;
797  }
798  }
799  }
800  ASSERT(signPos == signLen || signPos == signLen + 1);
801  ASSERT(codePos >= outStartPos && codePos < CodeBufferBitLen);
802  return codePos - outStartPos;
803 }
#define BufferSize
must be a multiple of WordWidth
Definition: PGFtypes.h:77
void ClearBit(UINT32 *stream, UINT32 pos)
Definition: BitStream.h:56
UINT32 SeekBit1Range(UINT32 *stream, UINT32 pos, UINT32 len)
Definition: BitStream.h:235
#define __min(x, y)
Definition: PGFplatform.h:91
UINT32 m_codeBuffer[CodeBufferLen]
output buffer for encoded bitstream
Definition: Encoder.h:81
void SetBit(UINT32 *stream, UINT32 pos)
Definition: BitStream.h:48
void SetValueBlock(UINT32 *stream, UINT32 pos, UINT32 val, UINT32 k)
Definition: BitStream.h:102
#define WordWidth
WordBytes*8.
Definition: PGFplatform.h:73
#define CodeBufferBitLen
max number of bits in m_codeBuffer
Definition: Encoder.cpp:58

Member Data Documentation

UINT32 CEncoder::CMacroBlock::m_codeBuffer[CodeBufferLen]

output buffer for encoded bitstream

Definition at line 81 of file Encoder.h.

UINT32 CEncoder::CMacroBlock::m_codePos

current position in encoded bitstream

Definition at line 85 of file Encoder.h.

CEncoder* CEncoder::CMacroBlock::m_encoder
private

Definition at line 94 of file Encoder.h.

ROIBlockHeader CEncoder::CMacroBlock::m_header

block header

Definition at line 82 of file Encoder.h.

int CEncoder::CMacroBlock::m_lastLevelIndex

index of last encoded level: [0, nLevels); used because a level-end can occur before a buffer is full

Definition at line 86 of file Encoder.h.

UINT32 CEncoder::CMacroBlock::m_maxAbsValue

maximum absolute coefficient in each buffer

Definition at line 84 of file Encoder.h.

bool CEncoder::CMacroBlock::m_sigFlagVector[BufferSize+1]
private

Definition at line 95 of file Encoder.h.

DataT CEncoder::CMacroBlock::m_value[BufferSize]

input buffer of values with index m_valuePos

Definition at line 80 of file Encoder.h.

UINT32 CEncoder::CMacroBlock::m_valuePos

current buffer position

Definition at line 83 of file Encoder.h.


The documentation for this class was generated from the following files: