58 #define CodeBufferBitLen (CodeBufferLen*WordWidth) 59 #define MaxCodeLen ((1 << RLblockSizeLen) - 1) 74 , m_nLevels(header.nLevels)
76 , m_forceWriting(
false)
77 #ifdef __PGFROISUPPORT__ 86 #ifdef LIBPGF_USE_OPENMP 87 m_macroBlockLen = omp_get_num_procs();
92 if (useOMP && m_macroBlockLen > 1) {
93 #ifdef LIBPGF_USE_OPENMP 94 omp_set_num_threads(m_macroBlockLen);
97 m_macroBlocks =
new(std::nothrow) CMacroBlock*[m_macroBlockLen];
98 if (!m_macroBlocks) ReturnWithError(InsufficientMemory);
99 for (
int i=0; i < m_macroBlockLen; i++) m_macroBlocks[i] =
new CMacroBlock(
this);
100 m_lastMacroBlock = 0;
101 m_currentBlock = m_macroBlocks[m_lastMacroBlock++];
105 m_currentBlock =
new CMacroBlock(
this);
109 m_startPosition = m_stream->GetPos();
112 preHeader.hSize =
__VAL(preHeader.hSize);
114 m_stream->Write(&count, &preHeader);
117 header.height =
__VAL(header.height);
118 header.width =
__VAL(header.width);
120 m_stream->Write(&count, &header);
126 m_stream->Write(&count, (
void *)postHeader.clut);
129 userDataPos = m_stream->GetPos();
130 if (postHeader.userDataLen) {
131 if (postHeader.userData) {
133 count = postHeader.userDataLen;
134 m_stream->Write(&count, postHeader.userData);
136 m_stream->SetPos(FSFromCurrent, count);
141 m_levelLengthPos = m_stream->GetPos();
161 preHeader.hSize =
__VAL(preHeader.hSize);
174 delete[] levelLength;
175 levelLength =
new(std::nothrow) UINT32[
m_nLevels];
176 if (!levelLength) ReturnWithError(InsufficientMemory);
177 for (UINT8 l = 0; l <
m_nLevels; l++) levelLength[l] = 0;
204 #ifdef PGF_USE_BIG_ENDIAN 216 #endif //PGF_USE_BIG_ENDIAN 247 const int wr = pitch - ww.rem;
248 int pos, base = startPos, base2;
251 for (
int i=0; i < hh.quot; i++) {
254 for (
int j=0; j < ww.quot; j++) {
268 for (
int x=0; x < ww.rem; x++) {
278 for (
int j=0; j < ww.quot; j++) {
281 for (
int y=0; y < hh.rem; y++) {
292 for (
int y=0; y < hh.rem; y++) {
294 for (
int x=0; x < ww.rem; x++) {
338 #ifdef __PGFROISUPPORT__ 374 #pragma omp parallel for default(shared) //no declared exceptions in next block 384 m_lastMacroBlock = 0;
400 int count =
sizeof(UINT16);
407 #ifdef PGF_USE_BIG_ENDIAN 409 UINT16 wl =
__VAL(wordLen);
410 m_stream->
Write(&count, &wl); ASSERT(count ==
sizeof(UINT16));
412 #ifdef __PGFROISUPPORT__ 418 #endif // __PGFROISUPPORT__ 421 for (
int i=0; i < wordLen; i++) {
422 block->m_codeBuffer[i] =
__VAL(block->m_codeBuffer[i]);
426 m_stream->
Write(&count, &wordLen); ASSERT(count ==
sizeof(UINT16));
428 #ifdef __PGFROISUPPORT__ 433 #endif // __PGFROISUPPORT__ 434 #endif // PGF_USE_BIG_ENDIAN 454 block->m_valuePos = 0;
455 block->m_maxAbsValue = 0;
470 UINT32 sigLen, codeLen = 0, wordPos, refLen, signLen;
475 UINT32 bufferSize = m_header.rbh.bufferSize; ASSERT(bufferSize <=
BufferSize);
483 for (UINT32 k=0; k < bufferSize; k++) {
484 m_sigFlagVector[k] =
false;
486 m_sigFlagVector[bufferSize] =
true;
489 for (UINT32 k=0; k < bufferSize; k++) {
495 nPlanes = NumberOfBitplanes();
504 planeMask = 1 << (nPlanes - 1);
506 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
513 sigLen = DecomposeBitplane(bufferSize, planeMask, m_codePos +
RLblockSizeLen + 1, sigBits, refBits, signBits, signLen, codeLen);
518 SetBit(m_codeBuffer, m_codePos++);
536 ClearBit(m_codeBuffer, m_codePos++);
543 if (m_encoder->m_favorSpeed || signLen == 0) {
549 codeLen = RLESigns(m_codePos +
RLblockSizeLen + 1, signBits, signLen);
552 if (useRL && codeLen <=
MaxCodeLen && codeLen < signLen) {
556 SetBit(m_codeBuffer, m_codePos++);
568 ClearBit(m_codeBuffer, m_codePos++);
579 for (UINT32 k=0; k < codeLen; k++) {
580 m_codeBuffer[wordPos++] = signBits[k];
589 for (UINT32 k=0; k < refLen; k++) {
590 m_codeBuffer[wordPos++] = sigBits[k];
601 for (UINT32 k=0; k < refLen; k++) {
602 m_codeBuffer[wordPos++] = refBits[k];
627 UINT32 valuePos = 0, valueEnd;
634 const UINT32 outStartPos = codePos;
636 UINT32 runlen = 1 << k;
639 while (valuePos < bufferSize) {
642 while(!m_sigFlagVector[valueEnd]) { valueEnd++; }
646 while (valuePos < valueEnd) {
647 if (GetBitAtPos(valuePos, planeMask)) {
651 SetBit(m_codeBuffer, codePos++);
662 if (m_value[valuePos] < 0) {
663 SetBit(signBits, signLen++);
664 SetBit(m_codeBuffer, codePos++);
671 SetBit(sigBits, sigPos++);
674 m_sigFlagVector[valuePos] =
true;
681 if (count == runlen) {
700 if (valuePos < bufferSize) {
702 if (GetBitAtPos(valuePos++, planeMask)) {
713 SetBit(m_codeBuffer, codePos++);
719 SetBit(m_codeBuffer, codePos++);
723 ASSERT(sigPos <= bufferSize);
724 ASSERT(refPos <= bufferSize);
725 ASSERT(signLen <= bufferSize);
726 ASSERT(valuePos == bufferSize);
728 codeLen = codePos - outStartPos;
740 if (m_maxAbsValue > 0) {
741 while (m_maxAbsValue > 0) {
742 m_maxAbsValue >>= 1; cnt++;
765 const UINT32 outStartPos = codePos;
767 UINT32 runlen = 1 << k;
771 while (signPos < signLen) {
775 if (count == runlen) {
778 SetBit(m_codeBuffer, codePos++);
787 signPos += count + 1;
800 ASSERT(signPos == signLen || signPos == signLen + 1);
802 return codePos - outStartPos;
807 void CEncoder::DumpBuffer()
const {
UINT8 NumberOfBitplanes()
int m_macroBlockLen
array length
void SetBufferStartPos()
Save current stream position as beginning of current level.
virtual void Write(int *count, void *buffer)=0
UINT32 AlignWordPos(UINT32 pos)
void WriteMacroBlock(CMacroBlock *block) THROW_
UINT32 m_valuePos
current buffer position
Abstract stream base class.
int m_lastLevelIndex
index of last encoded level: [0, nLevels); used because a level-end can occur before a buffer is full...
#define LinBlockSize
side length of a coefficient block in a HH or LL subband
#define BufferSize
must be a multiple of WordWidth
UINT32 m_maxAbsValue
maximum absolute coefficient in each buffer
UINT32 RLESigns(UINT32 codePos, UINT32 *signBits, UINT32 signLen)
virtual void SetPos(short posMode, INT64 posOff)=0
UINT32 WriteLevelLength(UINT32 *&levelLength) THROW_
#define RLblockSizeLen
block size length (< 16): ld(BufferSize) < RLblockSizeLen <= 2*ld(BufferSize)
UINT32 * m_levelLength
temporary saves the level index
void EncodeBuffer(ROIBlockHeader h) THROW_
#define BufferLen
number of words per buffer
void ClearBit(UINT32 *stream, UINT32 pos)
#define MaxBitPlanesLog
number of bits to code the maximum number of bit planes (in 32 or 16 bit mode)
UINT32 DecomposeBitplane(UINT32 bufferSize, UINT32 planeMask, UINT32 codePos, UINT32 *sigBits, UINT32 *refBits, UINT32 *signBits, UINT32 &signLen, UINT32 &codeLen)
INT64 ComputeBufferLength() const
void Partition(CSubband *band, int width, int height, int startPos, int pitch) THROW_
bool m_forceWriting
all macro blocks have to be written into the stream
UINT32 SeekBit1Range(UINT32 *stream, UINT32 pos, UINT32 len)
CMacroBlock * m_currentBlock
current macro block (used by main thread)
UINT32 UpdateLevelLength() THROW_
CMacroBlock ** m_macroBlocks
array of macroblocks
UINT64 m_startPosition
stream position of PGF start (PreHeader)
A macro block is an encoding unit of fixed size (uncoded)
void SetBit(UINT32 *stream, UINT32 pos)
virtual UINT64 GetPos() const =0
CPGFStream * m_stream
output PMF stream
UINT64 m_levelLengthPos
stream position of Metadata
void WriteValue(CSubband *band, int bandPos) THROW_
UINT32 NumberOfWords(UINT32 pos)
#define MaxBitPlanes
maximum number of bit planes of m_value: 32 minus sign bit
int m_currLevelIndex
counts where (=index) to save next value
void SetValueBlock(UINT32 *stream, UINT32 pos, UINT32 val, UINT32 k)
DataT m_value[BufferSize]
input buffer of values with index m_valuePos
ROIBlockHeader m_header
block header
int m_lastMacroBlock
array index of the last created macro block
UINT8 m_nLevels
number of levels
CEncoder(CPGFStream *stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader &postHeader, UINT64 &userDataPos, bool useOMP) THROW_
#define CodeBufferLen
number of words in code buffer (CodeBufferLen > BufferLen)
#define MaxCodeLen
max length of RL encoded block
void Init(int lastLevelIndex)
void UpdatePostHeaderSize(PGFPreHeader preHeader) THROW_
#define CodeBufferBitLen
max number of bits in m_codeBuffer