58 #define CodeBufferBitLen (CodeBufferLen*WordWidth) 59 #define MaxCodeLen ((1 << RLblockSizeLen) - 1) 74 , m_nLevels(header.nLevels)
76 , m_forceWriting(
false)
77 #ifdef __PGFROISUPPORT__ 88 #ifdef LIBPGF_USE_OPENMP 89 m_macroBlockLen = omp_get_num_procs();
94 if (useOMP && m_macroBlockLen > 1) {
95 #ifdef LIBPGF_USE_OPENMP 96 omp_set_num_threads(m_macroBlockLen);
99 m_macroBlocks =
new(std::nothrow) CMacroBlock*[m_macroBlockLen];
100 if (!m_macroBlocks) ReturnWithError(InsufficientMemory);
101 for (
int i=0; i < m_macroBlockLen; i++) m_macroBlocks[i] =
new CMacroBlock(
this);
102 m_currentBlock = m_macroBlocks[m_lastMacroBlock++];
106 m_currentBlock =
new CMacroBlock(
this);
110 m_startPosition = m_stream->GetPos();
113 preHeader.hSize =
__VAL(preHeader.hSize);
115 m_stream->Write(&count, &preHeader);
118 header.height =
__VAL(header.height);
119 header.width =
__VAL(header.width);
121 m_stream->Write(&count, &header);
127 m_stream->Write(&count, (
void *)postHeader.clut);
130 userDataPos = m_stream->GetPos();
131 if (postHeader.userDataLen) {
132 if (postHeader.userData) {
134 count = postHeader.userDataLen;
135 m_stream->Write(&count, postHeader.userData);
137 m_stream->SetPos(FSFromCurrent, count);
142 m_levelLengthPos = m_stream->GetPos();
161 UINT64 curPos = m_stream->GetPos();
165 m_stream->SetPos(FSFromStart, m_startPosition);
166 preHeader.hSize =
__VAL(preHeader.hSize);
167 m_stream->Write(&count, &preHeader);
169 m_stream->SetPos(FSFromStart, curPos);
179 delete[] levelLength;
180 levelLength =
new(std::nothrow) UINT32[m_nLevels];
181 if (!levelLength) ReturnWithError(InsufficientMemory);
182 for (UINT8 l = 0; l < m_nLevels; l++) levelLength[l] = 0;
183 m_levelLength = levelLength;
186 m_levelLengthPos = m_stream->GetPos();
190 m_stream->Write(&count, m_levelLength);
209 #ifdef PGF_USE_BIG_ENDIAN 221 #endif //PGF_USE_BIG_ENDIAN 252 const int wr = pitch - ww.rem;
253 int pos, base = startPos, base2;
256 for (
int i=0; i < hh.quot; i++) {
259 for (
int j=0; j < ww.quot; j++) {
263 WriteValue(band, pos);
273 for (
int x=0; x < ww.rem; x++) {
274 WriteValue(band, pos);
283 for (
int j=0; j < ww.quot; j++) {
286 for (
int y=0; y < hh.rem; y++) {
288 WriteValue(band, pos);
297 for (
int y=0; y < hh.rem; y++) {
299 for (
int x=0; x < ww.rem; x++) {
300 WriteValue(band, pos);
327 if (m_currentBlock->m_valuePos ==
BufferSize) {
330 DataT val = m_currentBlock->m_value[m_currentBlock->m_valuePos++] = band->GetData(bandPos);
332 if (v > m_currentBlock->m_maxAbsValue) m_currentBlock->m_maxAbsValue = v;
342 ASSERT(m_currentBlock);
343 #ifdef __PGFROISUPPORT__ 348 m_currentBlock->m_header = h;
351 if (m_macroBlockLen == 1) {
352 m_currentBlock->BitplaneEncode();
353 WriteMacroBlock(m_currentBlock);
356 int lastLevelIndex = m_currentBlock->m_lastLevelIndex;
358 if (m_forceWriting || m_lastMacroBlock == m_macroBlockLen) {
383 #ifdef LIBPGF_USE_OPENMP 384 #pragma omp parallel for default(shared) //no declared exceptions in next block 386 for (
int i=0; i < m_lastMacroBlock; i++) {
387 m_macroBlocks[i]->BitplaneEncode();
389 for (
int i=0; i < m_lastMacroBlock; i++) {
390 WriteMacroBlock(m_macroBlocks[i]);
394 m_forceWriting =
false;
395 m_lastMacroBlock = 0;
398 m_currentBlock = m_macroBlocks[m_lastMacroBlock++];
399 m_currentBlock->Init(lastLevelIndex);
408 #ifdef __PGFROISUPPORT__ 412 int count =
sizeof(UINT16);
419 #ifdef PGF_USE_BIG_ENDIAN 421 UINT16 wl =
__VAL(wordLen);
422 m_stream->Write(&count, &wl); ASSERT(count ==
sizeof(UINT16));
424 #ifdef __PGFROISUPPORT__ 428 m_stream->Write(&count, &h.
val); ASSERT(count ==
sizeof(UINT16));
430 #endif // __PGFROISUPPORT__ 433 for (
int i=0; i < wordLen; i++) {
434 block->m_codeBuffer[i] =
__VAL(block->m_codeBuffer[i]);
438 m_stream->Write(&count, &wordLen); ASSERT(count ==
sizeof(UINT16));
440 #ifdef __PGFROISUPPORT__ 443 m_stream->Write(&count, &h.
val); ASSERT(count ==
sizeof(UINT16));
445 #endif // __PGFROISUPPORT__ 446 #endif // PGF_USE_BIG_ENDIAN 450 m_stream->Write(&count, block->m_codeBuffer);
456 ASSERT(m_currLevelIndex < m_nLevels);
457 m_levelLength[m_currLevelIndex] += (UINT32)ComputeBufferLength();
458 m_currLevelIndex = block->m_lastLevelIndex + 1;
466 block->m_valuePos = 0;
467 block->m_maxAbsValue = 0;
482 UINT32 sigLen, codeLen = 0, wordPos, refLen, signLen;
495 for (UINT32 k=0; k < bufferSize; k++) {
501 for (UINT32 k=0; k < bufferSize; k++) {
516 planeMask = 1 << (nPlanes - 1);
518 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
564 if (useRL && codeLen <=
MaxCodeLen && codeLen < signLen) {
591 for (UINT32 k=0; k < codeLen; k++) {
601 for (UINT32 k=0; k < refLen; k++) {
613 for (UINT32 k=0; k < refLen; k++) {
639 UINT32 valuePos = 0, valueEnd;
646 const UINT32 outStartPos = codePos;
648 UINT32 runlen = 1 << k;
651 while (valuePos < bufferSize) {
654 while(!m_sigFlagVector[valueEnd]) { valueEnd++; }
658 while (valuePos < valueEnd) {
659 if (GetBitAtPos(valuePos, planeMask)) {
663 SetBit(m_codeBuffer, codePos++);
674 if (m_value[valuePos] < 0) {
675 SetBit(signBits, signLen++);
676 SetBit(m_codeBuffer, codePos++);
683 SetBit(sigBits, sigPos++);
686 m_sigFlagVector[valuePos] =
true;
693 if (count == runlen) {
712 if (valuePos < bufferSize) {
714 if (GetBitAtPos(valuePos++, planeMask)) {
725 SetBit(m_codeBuffer, codePos++);
731 SetBit(m_codeBuffer, codePos++);
735 ASSERT(sigPos <= bufferSize);
736 ASSERT(refPos <= bufferSize);
737 ASSERT(signLen <= bufferSize);
738 ASSERT(valuePos == bufferSize);
740 codeLen = codePos - outStartPos;
752 if (m_maxAbsValue > 0) {
753 while (m_maxAbsValue > 0) {
754 m_maxAbsValue >>= 1; cnt++;
777 const UINT32 outStartPos = codePos;
779 UINT32 runlen = 1 << k;
783 while (signPos < signLen) {
787 if (count == runlen) {
790 SetBit(m_codeBuffer, codePos++);
799 signPos += count + 1;
812 ASSERT(signPos == signLen || signPos == signLen + 1);
814 return codePos - outStartPos;
819 void CEncoder::DumpBuffer()
const {
UINT8 NumberOfBitplanes()
int m_macroBlockLen
array length
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.
#define LinBlockSize
side length of a coefficient block in a HH or LL subband
#define BufferSize
must be a multiple of WordWidth
UINT32 m_codePos
current position in encoded bitstream
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)
bool m_sigFlagVector[BufferSize+1]
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)
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
UINT32 m_codeBuffer[CodeBufferLen]
output buffer for encoded bitstream
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
bool m_favorSpeed
favor speed over size
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
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 UpdatePostHeaderSize(PGFPreHeader preHeader) THROW_
#define CodeBufferBitLen
max number of bits in m_codeBuffer