58 #define CodeBufferBitLen (CodeBufferLen*WordWidth) 59 #define MaxCodeLen ((1 << RLblockSizeLen) - 1) 74 , m_nLevels(header.nLevels)
76 , m_forceWriting(false)
85 m_levelLength =
nullptr;
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();
115 m_stream->Write(&count, &preHeader);
121 m_stream->Write(&count, &header);
127 m_stream->Write(&count, (
void *)postHeader.
clut);
130 userDataPos = m_stream->GetPos();
135 m_stream->Write(&count, postHeader.
userData);
137 m_stream->SetPos(FSFromCurrent, count);
142 m_levelLengthPos = m_stream->GetPos();
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;
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++) {
273 for (
int x=0; x < ww.rem; x++) {
283 for (
int j=0; j < ww.quot; j++) {
286 for (
int y=0; y < hh.rem; y++) {
297 for (
int y=0; y < hh.rem; y++) {
299 for (
int x=0; x < ww.rem; x++) {
343 #ifdef __PGFROISUPPORT__ 383 #ifdef LIBPGF_USE_OPENMP 384 #pragma omp parallel for default(shared) //no declared exceptions in next block 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__ 431 #endif // __PGFROISUPPORT__ 434 for (
int i=0; i < wordLen; i++) {
439 m_stream->
Write(&count, &wordLen); ASSERT(count ==
sizeof(UINT16));
441 #ifdef __PGFROISUPPORT__ 447 #endif // __PGFROISUPPORT__ 448 #endif // PGF_USE_BIG_ENDIAN 484 UINT32 sigLen, codeLen = 0, wordPos, refLen, signLen;
497 for (UINT32 k=0; k < bufferSize; k++) {
503 for (UINT32 k=0; k < bufferSize; k++) {
518 planeMask = 1 << (nPlanes - 1);
520 for (
int plane = nPlanes - 1; plane >= 0; plane--) {
566 if (useRL && codeLen <=
MaxCodeLen && codeLen < signLen) {
593 for (UINT32 k=0; k < codeLen; k++) {
603 for (UINT32 k=0; k < refLen; k++) {
615 for (UINT32 k=0; k < refLen; k++) {
641 UINT32 valuePos = 0, valueEnd;
648 const UINT32 outStartPos = codePos;
650 UINT32 runlen = 1 << k;
653 while (valuePos < bufferSize) {
656 while(!m_sigFlagVector[valueEnd]) { valueEnd++; }
660 while (valuePos < valueEnd) {
661 if (GetBitAtPos(valuePos, planeMask)) {
665 SetBit(m_codeBuffer, codePos++);
676 if (m_value[valuePos] < 0) {
677 SetBit(signBits, signLen++);
678 SetBit(m_codeBuffer, codePos++);
685 SetBit(sigBits, sigPos++);
688 m_sigFlagVector[valuePos] =
true;
695 if (count == runlen) {
714 if (valuePos < bufferSize) {
716 if (GetBitAtPos(valuePos++, planeMask)) {
727 SetBit(m_codeBuffer, codePos++);
733 SetBit(m_codeBuffer, codePos++);
737 ASSERT(sigPos <= bufferSize);
738 ASSERT(refPos <= bufferSize);
739 ASSERT(signLen <= bufferSize);
740 ASSERT(valuePos == bufferSize);
742 codeLen = codePos - outStartPos;
754 if (m_maxAbsValue > 0) {
755 while (m_maxAbsValue > 0) {
756 m_maxAbsValue >>= 1; cnt++;
779 const UINT32 outStartPos = codePos;
781 UINT32 runlen = 1 << k;
785 while (signPos < signLen) {
789 if (count == runlen) {
792 SetBit(m_codeBuffer, codePos++);
801 signPos += count + 1;
814 ASSERT(signPos == signLen || signPos == signLen + 1);
816 return codePos - outStartPos;
821 void CEncoder::DumpBuffer()
const {
CEncoder(CPGFStream *stream, PGFPreHeader preHeader, PGFHeader header, const PGFPostHeader &postHeader, UINT64 &userDataPos, bool useOMP)
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)
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
DataT GetData(UINT32 pos) const
void EncodeBuffer(ROIBlockHeader h)
#define BufferSize
must be a multiple of WordWidth, BufferSize <= UINT16_MAX
UINT32 m_maxAbsValue
maximum absolute coefficient in each buffer
UINT32 m_codePos
current position in encoded bitstream
UINT32 RLESigns(UINT32 codePos, UINT32 *signBits, UINT32 signLen)
virtual void SetPos(short posMode, INT64 posOff)=0
#define RLblockSizeLen
block size length (< 16): ld(BufferSize) < RLblockSizeLen <= 2*ld(BufferSize)
UINT32 WriteLevelLength(UINT32 *&levelLength)
bool m_sigFlagVector[BufferSize+1]
UINT32 * m_levelLength
temporary saves the level index
void WriteMacroBlock(CMacroBlock *block)
#define BufferLen
number of words per buffer
void ClearBit(UINT32 *stream, UINT32 pos)
void UpdatePostHeaderSize(PGFPreHeader preHeader)
#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
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)
CMacroBlock ** m_macroBlocks
array of macroblocks
void Partition(CSubband *band, int width, int height, int startPos, int pitch)
void SetStreamPosToStart()
Resets stream position to beginning of PGF pre-header.
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
UINT32 NumberOfWords(UINT32 pos)
UINT32 UpdateLevelLength()
#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
void WriteValue(CSubband *band, int bandPos)
ROIBlockHeader m_header
block header
int m_lastMacroBlock
array index of the last created macro block
UINT8 m_nLevels
number of levels
#define CodeBufferLen
number of words in code buffer (CodeBufferLen > BufferLen)
#define MaxCodeLen
max length of RL encoded block
void Init(int lastLevelIndex)
#define CodeBufferBitLen
max number of bits in m_codeBuffer