• R/O
  • SSH
  • HTTPS

jyeipegyuu: 提交


Commit MetaInfo

修订版15 (tree)
时间2009-09-07 23:51:32
作者berupon

Log Message

01領域のフラグをRangeCoderで圧縮
Golomb-Rice Coder撤去

更改概述

差异

--- misc.h (revision 14)
+++ misc.h (revision 15)
@@ -110,106 +110,11 @@
110110 const unsigned char* initialSrc_;
111111 };
112112
113-class RiceCoder
114-{
115-public:
116- RiceCoder(size_t shift)
117- :
118- shift_(shift)
119- {
120- }
121-
122- void Encode(size_t value, BitWriter& writer)
123- {
124- size_t p = value >> shift_;
125- for (size_t i=0; i<p; ++i) {
126- writer.putBit(false);
127- }
128- writer.putBit(true);
129- int v = 1;
130- for (size_t i=0; i<shift_; ++i) {
131- writer.putBit(value & v);
132- v <<= 1;
133- }
134- }
135-
136- size_t Decode(BitReader& reader)
137- {
138- size_t q = 0;
139- while (!reader.getBit()) ++q;
140- size_t ret = q << shift_;
141- for (size_t i=0; i<shift_; ++i) {
142- if (reader.getBit()) {
143- ret += 1 << i;
144- }
145- }
146- return ret;
147- }
148-
149-private:
150- size_t shift_;
151-};
152-
153-enum RiceCoderFlag {
154- None = 0xF,
155-};
156-
157-size_t repeationCompress(
158- const unsigned char* src, size_t srcLen,
159- unsigned char* tmp
160- )
161-{
162- int repeatHist[1024*4] = {0};
163- int repeat = 0;
164-
165- BitWriter bitWriter(tmp);
166- RiceCoder riceCoder(2);
167-
168- for (size_t i=0; i<srcLen; ++i) {
169- unsigned char d = src[i];
170- for (size_t j=0; j<8; ++j) {
171- if (d & (1 << (7-j))) {
172- ++repeat;
173- }else {
174- riceCoder.Encode(repeat, bitWriter);
175- ++repeatHist[repeat];
176- repeat = 0;
177- }
178- }
179- }
180- if (repeat) {
181- riceCoder.Encode(repeat, bitWriter);
182- ++repeatHist[repeat];
183- repeat = 0;
184- }
185-
186- size_t len = bitWriter.nBytes();
187- return len;
188-}
189-
190-size_t repeationDecompress(
191- const unsigned char* src, size_t srcLen,
192- unsigned char* dst
193- )
194-{
195- RiceCoder riceCoder(2);
196- BitReader bitReader(src);
197- BitWriter bitWriter(dst);
198- while (bitReader.nBytes() <= srcLen) {
199- size_t v = riceCoder.Decode(bitReader);
200- for (size_t i=0; i<v; ++i) {
201- bitWriter.putBit(true);
202- }
203- bitWriter.putBit(false);
204- }
205- return bitWriter.nBytes();
206-}
207-
208113 void findZeroOneInfos(
209114 size_t hBlockCount,
210115 size_t vBlockCount,
211116 const int* src,
212- unsigned char* dst,
117+ int* dst,
213118 size_t limit
214119 )
215120 {
@@ -219,7 +124,7 @@
219124 // pass DC0
220125 from += totalBlockCount;
221126
222- std::fill(dst, dst+totalBlockCount, 255);
127+ std::fill(dst, dst+totalBlockCount, 1);
223128
224129 for (size_t i=1; i<8; ++i) {
225130 unsigned char blockSize = 1 + i*2;
@@ -249,7 +154,6 @@
249154 int phists[1024];
250155 int mhists[1024];
251156 int zeroRepeatHist[1024*4];
252- unsigned char riceCoderParam;
253157
254158 size_t zeroOneOnlyAreaHist[2];
255159 size_t nonZeroOneOnlyAreaHist[1024];
@@ -258,14 +162,12 @@
258162 size_t srcCount;
259163 size_t signFlagsCount;
260164 size_t initialCompressedLen;
261- size_t riceCodedLen;
262- size_t repeationCompressedLen;
263165 size_t totalLen;
264166 };
265167
266168 size_t compressSub(
267169 int* src,
268- const unsigned char* pZeroOneInfos,
170+ const int* pZeroOneInfos,
269171 size_t blockCount, size_t blockSize,
270172 unsigned char* dest, size_t destLen,
271173 unsigned char* tmp,
@@ -305,7 +207,7 @@
305207 }
306208 encodedValueSizes[0] = blockCount*blockSize;
307209 Encode(&values[0], values.size(), 1, 0, tmp2, encodedValueSizes[0]);
308-
210+
309211 values.clear();
310212 int maxV = 0;
311213 for (size_t i=0; i<blockCount; ++i) {
@@ -337,60 +239,12 @@
337239 }
338240
339241 }
340- int b = 0;
341-
342- if (max > 2048) {
343- b = 7;
344- }else if (max > 1024) {
345- b = 6;
346- }else if (max > 512) {
347- b = 5;
348- }else if (max > 256) {
349- b = 4;
350- }else if (max > 128+64) {
351- b = 3;
352- }else if (max > 96) {
353- b = 2;
354- }else if (max > 32) {
355- b = 1;
356- }else {
357- b = 0;
358- }
359-
360- RiceCoder riceCoder(b);
361- BitWriter bitWriter(tmp2);
362- for (size_t i=0; i<blockCount; ++i) {
363- for (size_t j=0; j<blockSize; ++j) {
364- riceCoder.Encode(*from++, bitWriter);
365- }
366- }
367- size_t riceCodedLen = bitWriter.nBytes();
368- size_t repeationCompressedLen = repeationCompress(tmp2, riceCodedLen, dest+6);
369-
370- size_t len = 0;
371- if (initialCompressedLen < riceCodedLen && initialCompressedLen < repeationCompressedLen) {
372- *dest++ = 1;
373- *dest++ = RiceCoderFlag::None;
374- len = initialCompressedLen;
375- memcpy(dest+4, tmp, len);
376- }else if (riceCodedLen < repeationCompressedLen) {
377- *dest++ = 0;
378- *dest++ = b;
379- len = riceCodedLen;
380- memcpy(dest+4, tmp2, len);
381- }else {
382- *dest++ = 2;
383- *dest++ = b;
384- len = repeationCompressedLen;
385- }
386- *((size_t*)dest) = len;
242+ *dest++ = 1;
243+ memcpy(dest+4, tmp, initialCompressedLen);
244+ *((size_t*)dest) = initialCompressedLen;
387245 dest += 4;
388- dest += len;
246+ dest += initialCompressedLen;
389247
390- cinfo.riceCoderParam = b;
391- cinfo.riceCodedLen = riceCodedLen;
392- cinfo.repeationCompressedLen = repeationCompressedLen;
393-
394248 label_end:
395249 size_t destDiff = dest - initialDest;
396250
@@ -564,7 +418,7 @@
564418 size_t compress(
565419 size_t hBlockCount,
566420 size_t vBlockCount,
567- const unsigned char* pZeroOneInfos,
421+ const int* pZeroOneInfos,
568422 size_t zeroOneLimit,
569423 CompressInfo compressInfos[8],
570424 int* src,
@@ -580,7 +434,7 @@
580434 size_t progress = 0;
581435
582436 for (size_t i=0; i<8; ++i) {
583- const unsigned char* p01 = (i < zeroOneLimit) ? 0 : pZeroOneInfos;
437+ const int* p01 = (i < zeroOneLimit) ? 0 : pZeroOneInfos;
584438 size_t blockSize = 1 + i * 2;
585439 progress += compressSub(from, p01, totalBlockCount, blockSize, dest+progress, destLen-progress, tmp1, tmp2, tmp3, compressInfos[i]);
586440 from += totalBlockCount * blockSize;
@@ -591,7 +445,7 @@
591445
592446 size_t decompressSub(
593447 const unsigned char* src,
594- const unsigned char* pZeroOneInfos,
448+ const int* pZeroOneInfos,
595449 unsigned char* tmp,
596450 int* tmp2,
597451 int* dest,
@@ -605,36 +459,19 @@
605459 unsigned char compressFlag = *src++;
606460
607461 if (compressFlag != 3) {
608-
609- unsigned char b = *src++;
610462 size_t len = *(size_t*)src;
611463 src += 4;
612464
613465 int len2 = 0;
614466 switch (compressFlag) {
615- case 0:
616- memcpy(tmp, src, len);
617- len2 = len;
618- break;
619467 case 1:
620468 len2 = destLen*4;
621- Decode((unsigned char*)src, len, (int*)tmp, len2);
469+ Decode((unsigned char*)src, len, (int*)dest, len2);
622470 break;
623- case 2:
624- len2 = repeationDecompress(src, len, tmp);
625- break;
471+ default:
472+ assert(false);
626473 }
627474 src += len;
628-
629- if (b == RiceCoderFlag::None) {
630- memcpy(dest, tmp, len2*4);
631- }else {
632- RiceCoder riceCoder(b);
633- BitReader bitReader(tmp);
634- for (size_t i=0; i<destLen; ++i) {
635- dest[i] = riceCoder.Decode(bitReader);
636- }
637- }
638475 }else {
639476 size_t sizes[2] = {0};
640477 int resultSize = destLen * 4;
@@ -997,7 +834,7 @@
997834 size_t decompress(
998835 size_t hBlockCount,
999836 size_t vBlockCount,
1000- const unsigned char* pZeroOneInfos,
837+ const int* pZeroOneInfos,
1001838 size_t zeroOneLimit,
1002839 const unsigned char* src, size_t srcLen,
1003840 unsigned char* tmp, int* tmp2,
@@ -1010,7 +847,7 @@
1010847 int* to = dest;
1011848
1012849 for (size_t i=0; i<8; ++i) {
1013- const unsigned char* p01 = (i < zeroOneLimit) ? 0 : pZeroOneInfos;
850+ const int* p01 = (i < zeroOneLimit) ? 0 : pZeroOneInfos;
1014851 size_t blockSize = 1 + i * 2;
1015852 from += decompressSub(from, p01, tmp, tmp2, to, totalBlockCount, blockSize);
1016853 to += totalBlockCount * blockSize;
--- RanCode.h (revision 14)
+++ RanCode.h (revision 15)
@@ -437,6 +437,7 @@
437437 while (cnt < result_size) {
438438 operation_buffer = readBits(operation_buffer, base-shift, code);
439439 ID = operation_buffer/product;
440+ assert(0<=ID && ID<=Denominator);
440441 operation_buffer -= product * cumulative[symbol[ID]];
441442 product = SafeProduct(product, frequency[symbol[ID]], mask, shift);
442443 result[cnt] = symbol[ID] + min_value - 1;
--- main.cpp (revision 14)
+++ main.cpp (revision 15)
@@ -64,7 +64,7 @@
6464 CompressInfo compressInfos[8] = {0};
6565
6666 Quantizer quantizer;
67- quantizer.init(6*8+0, 0, 0, true);
67+ quantizer.init(6*8+0, 0, 0, false);
6868
6969 size_t totalLen = 0;
7070
@@ -99,20 +99,19 @@
9999 size_t signFlagCount = collectInfos(pWork2, totalBlockCount, pSignFlags, compressInfos);
100100
101101 size_t zeroOneLimit = 2;
102- std::vector<unsigned char> zeroOneInfos(totalBlockCount);
103- unsigned char* pZeroOneInfos = &zeroOneInfos[0];
102+ std::vector<int> zeroOneInfos(totalBlockCount);
103+ int* pZeroOneInfos = &zeroOneInfos[0];
104104
105105 // quantizing zero one flags
106106 *dest++ = zeroOneLimit;
107107 if (zeroOneLimit != 0) {
108- findZeroOneInfos(hBlockCount, vBlockCount, pWork2, &zeroOneInfos[0], zeroOneLimit);
109- BitWriter bw(dest+4);
110- for (size_t i=0; i<totalBlockCount; ++i) {
111- bw.putBit(pZeroOneInfos[i]);
112- }
113- *((uint32_t*)dest) = bw.nBytes();
108+ findZeroOneInfos(hBlockCount, vBlockCount, pWork2, pZeroOneInfos, zeroOneLimit);
109+ int encodedSize = totalBlockCount/4;
110+ Encode(pZeroOneInfos, totalBlockCount, 1, 0, &work3[0], encodedSize);
111+ *((uint32_t*)dest) = encodedSize;
114112 dest += 4;
115- dest += bw.nBytes();
113+ memcpy(dest, &work3[0], encodedSize);
114+ dest += encodedSize;
116115 }else {
117116 pZeroOneInfos = 0;
118117 }
@@ -149,17 +148,15 @@
149148
150149 // zero one flags
151150 unsigned char zeroOneLimit = *src++;
152- std::vector<unsigned char> zeroOneFlags(totalBlockCount);
153- unsigned char* pZeroOneFlags = &zeroOneFlags[0];
151+ std::vector<int> zeroOneFlags(totalBlockCount*2);
152+ int* pZeroOneFlags = &zeroOneFlags[0];
154153 if (zeroOneLimit != 0) {
155154 uint32_t zeroOneFlagBytes = *(uint32_t*)src;
156155 src += 4;
157156 {
158- BitReader reader(src);
159- assert(totalBlockCount <= zeroOneFlagBytes*8);
160- for (size_t i=0; i<zeroOneFlagBytes*8; ++i) {
161- zeroOneFlags[i] = reader.getBit();
162- }
157+ int flagCount = totalBlockCount;
158+ Decode(src, zeroOneFlagBytes, pZeroOneFlags, flagCount);
159+ assert(flagCount == totalBlockCount);
163160 }
164161 src += zeroOneFlagBytes;
165162 }else {
Show on old repository browser