libpgf 7.26.45
PGF - Progressive Graphics File
Loading...
Searching...
No Matches
pgf::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
uint32_t m_codeBuffer [CodeBufferLen]
 output buffer for encoded bitstream
ROIBlockHeader m_header
 block header
uint32_t m_valuePos
 current buffer position
uint32_t m_maxAbsValue
 maximum absolute coefficient in each buffer
uint32_t m_codePos
 current position in encoded bitstream
int m_lastLevelIndex
 index of last encoded level: [0, nLevels); used because a level-end can occur before a buffer is full

Private Member Functions

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

Private Attributes

CEncoder * m_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 46 of file Encoder.h.

Constructor & Destructor Documentation

◆ CMacroBlock()

pgf::CEncoder::CMacroBlock::CMacroBlock ( CEncoder * encoder)
inline

Constructor: Initializes new macro block.

Parameters
encoderPointer to outer class.

Definition at line 51 of file Encoder.h.

52 : 4351 )
53 : m_value()
54 , m_codeBuffer()
55 , m_header(0)
56 , m_encoder(encoder)
58 {
59 ASSERT(m_encoder);
60 Init(-1);
61 }
bool m_sigFlagVector[BufferSize+1]
Definition Encoder.h:94
void Init(int lastLevelIndex)
Definition Encoder.h:66
ROIBlockHeader m_header
block header
Definition Encoder.h:81
DataT m_value[BufferSize]
input buffer of values with index m_valuePos
Definition Encoder.h:79
uint32_t m_codeBuffer[CodeBufferLen]
output buffer for encoded bitstream
Definition Encoder.h:80

Member Function Documentation

◆ BitplaneEncode()

void pgf::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 483 of file Encoder.cpp.

483 {
484 uint8_t nPlanes;
485 uint32_t sigLen, codeLen = 0, wordPos, refLen, signLen;
486 uint32_t sigBits[BufferLen] = { 0 };
487 uint32_t refBits[BufferLen] = { 0 };
488 uint32_t signBits[BufferLen] = { 0 };
489 uint32_t planeMask;
490 uint32_t bufferSize = m_header.rbh.bufferSize; ASSERT(bufferSize <= BufferSize);
491 bool useRL;
492
493#ifdef TRACE
494 //printf("which thread: %d\n", omp_get_thread_num());
495#endif
496
497 // clear significance vector
498 for (uint32_t k = 0; k < bufferSize; k++) {
499 m_sigFlagVector[k] = false;
500 }
501 m_sigFlagVector[bufferSize] = true; // sentinel
502
503 // clear output buffer
504 for (uint32_t k = 0; k < bufferSize; k++) {
505 m_codeBuffer[k] = 0;
506 }
507 m_codePos = 0;
508
509 // compute number of bit planes and split buffer into separate bit planes
510 nPlanes = NumberOfBitplanes();
511
512 // write number of bit planes to m_codeBuffer
513 // <nPlanes>
516
517 // loop through all bit planes
518 if (nPlanes == 0) nPlanes = MaxBitPlanes + 1;
519 planeMask = 1 << (nPlanes - 1);
520
521 for (int plane = nPlanes - 1; plane >= 0; plane--) {
522 // clear significant bitset
523 for (uint32_t k = 0; k < BufferLen; k++) {
524 sigBits[k] = 0;
525 }
526
527 // split bitplane in significant bitset and refinement bitset
528 sigLen = DecomposeBitplane(bufferSize, planeMask, m_codePos + RLblockSizeLen + 1, sigBits, refBits, signBits, signLen, codeLen);
529
530 if (sigLen > 0 && codeLen <= MaxCodeLen && codeLen < AlignWordPos(sigLen) + AlignWordPos(signLen) + 2*RLblockSizeLen) {
531 // set RL code bit
532 // <1><codeLen>
534
535 // write length codeLen to m_codeBuffer
537 m_codePos += RLblockSizeLen + codeLen;
538 } else {
539#ifdef TRACE
540 //printf("new\n");
541 //for (uint32_t i=0; i < bufferSize; i++) {
542 // printf("%s", (GetBit(sigBits, i)) ? "1" : "_");
543 // if (i%120 == 119) printf("\n");
544 //}
545 //printf("\n");
546#endif // TRACE
547
548 // run-length coding wasn't efficient enough
549 // we don't use RL coding for sigBits
550 // <0><sigLen>
552
553 // write length sigLen to m_codeBuffer
554 ASSERT(sigLen <= MaxCodeLen);
557
558 if (m_encoder->m_favorSpeed || signLen == 0) {
559 useRL = false;
560 } else {
561 // overwrite m_codeBuffer
562 useRL = true;
563 // run-length encode m_sign and append them to the m_codeBuffer
564 codeLen = RLESigns(m_codePos + RLblockSizeLen + 1, signBits, signLen);
565 }
566
567 if (useRL && codeLen <= MaxCodeLen && codeLen < signLen) {
568 // RL encoding of m_sign was efficient
569 // <1><codeLen><codedSignBits>_
570 // write RL code bit
572
573 // write codeLen to m_codeBuffer
575
576 // compute position of sigBits
577 wordPos = NumberOfWords(m_codePos + RLblockSizeLen + codeLen);
578 ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
579 } else {
580 // RL encoding of signBits wasn't efficient
581 // <0><signLen>_<signBits>_
582 // clear RL code bit
584
585 // write signLen to m_codeBuffer
586 ASSERT(signLen <= MaxCodeLen);
588
589 // write signBits to m_codeBuffer
591 ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
592 codeLen = NumberOfWords(signLen);
593
594 for (uint32_t k = 0; k < codeLen; k++) {
595 m_codeBuffer[wordPos++] = signBits[k];
596 }
597 }
598
599 // write sigBits
600 // <sigBits>_
601 ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
602 refLen = NumberOfWords(sigLen);
603
604 for (uint32_t k = 0; k < refLen; k++) {
605 m_codeBuffer[wordPos++] = sigBits[k];
606 }
607 m_codePos = wordPos << WordWidthLog;
608 }
609
610 // append refinement bitset (aligned to word boundary)
611 // _<refBits>
612 wordPos = NumberOfWords(m_codePos);
613 ASSERT(0 <= wordPos && wordPos < CodeBufferLen);
614 refLen = NumberOfWords(bufferSize - sigLen);
615
616 for (uint32_t k = 0; k < refLen; k++) {
617 m_codeBuffer[wordPos++] = refBits[k];
618 }
619 m_codePos = wordPos << WordWidthLog;
620 planeMask >>= 1;
621 }
622 ASSERT(0 <= m_codePos && m_codePos <= CodeBufferBitLen);
623 }
constexpr uint32_t WordWidthLog
ld of WordWidth
Definition PGFplatform.h:74
uint32_t RLESigns(uint32_t codePos, uint32_t *signBits, uint32_t signLen)
Definition Encoder.cpp:775
uint32_t DecomposeBitplane(uint32_t bufferSize, uint32_t planeMask, uint32_t codePos, uint32_t *sigBits, uint32_t *refBits, uint32_t *signBits, uint32_t &signLen, uint32_t &codeLen)
Definition Encoder.cpp:635
uint32_t m_codePos
current position in encoded bitstream
Definition Encoder.h:84
bool m_favorSpeed
favor speed over size
Definition Encoder.h:221
void ClearBit(uint32_t *stream, uint32_t pos)
Definition BitStream.h:70
constexpr int MaxBitPlanes
maximum number of bit planes of m_value: 32 minus sign bit
Definition PGFtypes.h:91
constexpr uint32_t CodeBufferBitLen
max number of bits in m_codeBuffer
Definition Decoder.cpp:59
void SetBit(uint32_t *stream, uint32_t pos)
Definition BitStream.h:62
void SetValueBlock(uint32_t *stream, uint32_t pos, uint32_t val, uint32_t k)
Definition BitStream.h:116
uint32_t AlignWordPos(uint32_t pos)
Definition BitStream.h:328
constexpr int RLblockSizeLen
block size length (< 16): ld(BufferSize) < RLblockSizeLen <= 2*ld(BufferSize)
Definition PGFtypes.h:87
constexpr uint32_t CodeBufferLen
number of words in code buffer (CodeBufferLen > BufferLen)
Definition PGFtypes.h:86
constexpr int MaxBitPlanesLog
number of bits to code the maximum number of bit planes (in 32 or 16 bit mode)
Definition PGFtypes.h:95
constexpr uint32_t MaxCodeLen
max length of RL encoded block
Definition Decoder.cpp:60
uint32_t NumberOfWords(uint32_t pos)
Definition BitStream.h:337
constexpr uint32_t BufferLen
number of words per buffer
Definition PGFtypes.h:85
constexpr uint32_t BufferSize
must be a multiple of WordWidth, BufferSize <= UINT16_MAX
Definition PGFtypes.h:84
uint16_t bufferSize
number of uncoded uint32_t values in a block
Definition PGFtypes.h:219
struct pgf::ROIBlockHeader::RBH rbh
ROI block header.

◆ DecomposeBitplane()

uint32_t pgf::CEncoder::CMacroBlock::DecomposeBitplane ( uint32_t bufferSize,
uint32_t planeMask,
uint32_t codePos,
uint32_t * sigBits,
uint32_t * refBits,
uint32_t * signBits,
uint32_t & signLen,
uint32_t & codeLen )
private

Definition at line 635 of file Encoder.cpp.

635 {
636 ASSERT(sigBits);
637 ASSERT(refBits);
638 ASSERT(signBits);
639 ASSERT(codePos < CodeBufferBitLen);
640
641 uint32_t sigPos = 0;
642 uint32_t valuePos = 0, valueEnd;
643 uint32_t refPos = 0;
644
645 // set output value
646 signLen = 0;
647
648 // prepare RLE of Sigs and Signs
649 const uint32_t outStartPos = codePos;
650 uint32_t k = 3;
651 uint32_t runlen = 1 << k; // = 2^k
652 uint32_t count = 0;
653
654 while (valuePos < bufferSize) {
655 // search next 1 in m_sigFlagVector using searching with sentinel
656 valueEnd = valuePos;
657 while (!m_sigFlagVector[valueEnd]) { valueEnd++; }
658
659 // search 1's in m_value[plane][valuePos..valueEnd)
660 // these 1's are significant bits
661 while (valuePos < valueEnd) {
662 if (GetBitAtPos(valuePos, planeMask)) {
663 // RLE encoding
664 // encode run of count 0's followed by a 1
665 // with codeword: 1<count>(signBits[signPos])
666 SetBit(m_codeBuffer, codePos++);
667 if (k > 0) {
668 SetValueBlock(m_codeBuffer, codePos, count, k);
669 codePos += k;
670
671 // adapt k (half the zero run-length)
672 k--;
673 runlen >>= 1;
674 }
675
676 // copy and write sign bit
677 if (m_value[valuePos] < 0) {
678 SetBit(signBits, signLen++);
679 SetBit(m_codeBuffer, codePos++);
680 } else {
681 ClearBit(signBits, signLen++);
682 ClearBit(m_codeBuffer, codePos++);
683 }
684
685 // write a 1 to sigBits
686 SetBit(sigBits, sigPos++);
687
688 // update m_sigFlagVector
689 m_sigFlagVector[valuePos] = true;
690
691 // prepare for next run
692 count = 0;
693 } else {
694 // RLE encoding
695 count++;
696 if (count == runlen) {
697 // encode run of 2^k zeros by a single 0
698 ClearBit(m_codeBuffer, codePos++);
699 // adapt k (double the zero run-length)
700 if (k < WordWidth) {
701 k++;
702 runlen <<= 1;
703 }
704
705 // prepare for next run
706 count = 0;
707 }
708
709 // write 0 to sigBits
710 sigPos++;
711 }
712 valuePos++;
713 }
714 // refinement bit
715 if (valuePos < bufferSize) {
716 // write one refinement bit
717 if (GetBitAtPos(valuePos++, planeMask)) {
718 SetBit(refBits, refPos);
719 } else {
720 ClearBit(refBits, refPos);
721 }
722 refPos++;
723 }
724 }
725 // RLE encoding of the rest of the plane
726 // encode run of count 0's followed by a 1
727 // with codeword: 1<count>(signBits[signPos])
728 SetBit(m_codeBuffer, codePos++);
729 if (k > 0) {
730 SetValueBlock(m_codeBuffer, codePos, count, k);
731 codePos += k;
732 }
733 // write dmmy sign bit
734 SetBit(m_codeBuffer, codePos++);
735
736 // write word filler zeros
737
738 ASSERT(sigPos <= bufferSize);
739 ASSERT(refPos <= bufferSize);
740 ASSERT(signLen <= bufferSize);
741 ASSERT(valuePos == bufferSize);
742 ASSERT(codePos >= outStartPos && codePos < CodeBufferBitLen);
743 codeLen = codePos - outStartPos;
744
745 return sigPos;
746 }
constexpr uint32_t WordWidth
WordBytes*8.
Definition PGFplatform.h:73
bool GetBitAtPos(uint32_t pos, uint32_t planeMask) const
Definition Encoder.h:91

◆ GetBitAtPos()

bool pgf::CEncoder::CMacroBlock::GetBitAtPos ( uint32_t pos,
uint32_t planeMask ) const
inlineprivate

Definition at line 91 of file Encoder.h.

91{ return (abs(m_value[pos]) & planeMask) > 0; }

◆ Init()

void pgf::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 66 of file Encoder.h.

66 { // initialize for reusage
67 m_valuePos = 0;
68 m_maxAbsValue = 0;
69 m_codePos = 0;
70 m_lastLevelIndex = lastLevelIndex;
71 }
uint32_t m_valuePos
current buffer position
Definition Encoder.h:82
uint32_t m_maxAbsValue
maximum absolute coefficient in each buffer
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:85

◆ NumberOfBitplanes()

uint8_t pgf::CEncoder::CMacroBlock::NumberOfBitplanes ( )
private

Definition at line 751 of file Encoder.cpp.

751 {
752 uint8_t cnt = 0;
753
754 // determine number of bitplanes for max value
755 if (m_maxAbsValue > 0) {
756 while (m_maxAbsValue > 0) {
757 m_maxAbsValue >>= 1; cnt++;
758 }
759 if (cnt == MaxBitPlanes + 1) cnt = 0;
760 // end cs
761 ASSERT(cnt <= MaxBitPlanes);
762 ASSERT((cnt >> MaxBitPlanesLog) == 0);
763 return cnt;
764 } else {
765 return 1;
766 }
767 }

◆ RLESigns()

uint32_t pgf::CEncoder::CMacroBlock::RLESigns ( uint32_t codePos,
uint32_t * signBits,
uint32_t signLen )
private

Definition at line 775 of file Encoder.cpp.

775 {
776 ASSERT(signBits);
777 ASSERT(0 <= codePos && codePos < CodeBufferBitLen);
778 ASSERT(0 < signLen && signLen <= BufferSize);
779
780 const uint32_t outStartPos = codePos;
781 uint32_t k = 0;
782 uint32_t runlen = 1 << k; // = 2^k
783 uint32_t count = 0;
784 uint32_t signPos = 0;
785
786 while (signPos < signLen) {
787 // search next 0 in signBits starting at position signPos
788 count = SeekBit1Range(signBits, signPos, __min(runlen, signLen - signPos));
789 // count 1's found
790 if (count == runlen) {
791 // encode run of 2^k ones by a single 1
792 signPos += count;
793 SetBit(m_codeBuffer, codePos++);
794 // adapt k (double the 1's run-length)
795 if (k < WordWidth) {
796 k++;
797 runlen <<= 1;
798 }
799 } else {
800 // encode run of count 1's followed by a 0
801 // with codeword: 0(count)
802 signPos += count + 1;
803 ClearBit(m_codeBuffer, codePos++);
804 if (k > 0) {
805 SetValueBlock(m_codeBuffer, codePos, count, k);
806 codePos += k;
807 }
808 // adapt k (half the 1's run-length)
809 if (k > 0) {
810 k--;
811 runlen >>= 1;
812 }
813 }
814 }
815 ASSERT(signPos == signLen || signPos == signLen + 1);
816 ASSERT(codePos >= outStartPos && codePos < CodeBufferBitLen);
817 return codePos - outStartPos;
818 }
#define __min(x, y)
uint32_t SeekBit1Range(uint32_t *stream, uint32_t pos, uint32_t len)
Definition BitStream.h:249

Member Data Documentation

◆ m_codeBuffer

uint32_t pgf::CEncoder::CMacroBlock::m_codeBuffer[CodeBufferLen]

output buffer for encoded bitstream

Definition at line 80 of file Encoder.h.

◆ m_codePos

uint32_t pgf::CEncoder::CMacroBlock::m_codePos

current position in encoded bitstream

Definition at line 84 of file Encoder.h.

◆ m_encoder

CEncoder* pgf::CEncoder::CMacroBlock::m_encoder
private

Definition at line 93 of file Encoder.h.

◆ m_header

ROIBlockHeader pgf::CEncoder::CMacroBlock::m_header

block header

Definition at line 81 of file Encoder.h.

◆ m_lastLevelIndex

int pgf::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 85 of file Encoder.h.

◆ m_maxAbsValue

uint32_t pgf::CEncoder::CMacroBlock::m_maxAbsValue

maximum absolute coefficient in each buffer

Definition at line 83 of file Encoder.h.

◆ m_sigFlagVector

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

Definition at line 94 of file Encoder.h.

◆ m_value

DataT pgf::CEncoder::CMacroBlock::m_value[BufferSize]

input buffer of values with index m_valuePos

Definition at line 79 of file Encoder.h.

◆ m_valuePos

uint32_t pgf::CEncoder::CMacroBlock::m_valuePos

current buffer position

Definition at line 82 of file Encoder.h.


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