01領域のフラグをRangeCoderで圧縮
Golomb-Rice Coder撤去
@@ -110,106 +110,11 @@ | ||
110 | 110 | const unsigned char* initialSrc_; |
111 | 111 | }; |
112 | 112 | |
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 | - | |
208 | 113 | void findZeroOneInfos( |
209 | 114 | size_t hBlockCount, |
210 | 115 | size_t vBlockCount, |
211 | 116 | const int* src, |
212 | - unsigned char* dst, | |
117 | + int* dst, | |
213 | 118 | size_t limit |
214 | 119 | ) |
215 | 120 | { |
@@ -219,7 +124,7 @@ | ||
219 | 124 | // pass DC0 |
220 | 125 | from += totalBlockCount; |
221 | 126 | |
222 | - std::fill(dst, dst+totalBlockCount, 255); | |
127 | + std::fill(dst, dst+totalBlockCount, 1); | |
223 | 128 | |
224 | 129 | for (size_t i=1; i<8; ++i) { |
225 | 130 | unsigned char blockSize = 1 + i*2; |
@@ -249,7 +154,6 @@ | ||
249 | 154 | int phists[1024]; |
250 | 155 | int mhists[1024]; |
251 | 156 | int zeroRepeatHist[1024*4]; |
252 | - unsigned char riceCoderParam; | |
253 | 157 | |
254 | 158 | size_t zeroOneOnlyAreaHist[2]; |
255 | 159 | size_t nonZeroOneOnlyAreaHist[1024]; |
@@ -258,14 +162,12 @@ | ||
258 | 162 | size_t srcCount; |
259 | 163 | size_t signFlagsCount; |
260 | 164 | size_t initialCompressedLen; |
261 | - size_t riceCodedLen; | |
262 | - size_t repeationCompressedLen; | |
263 | 165 | size_t totalLen; |
264 | 166 | }; |
265 | 167 | |
266 | 168 | size_t compressSub( |
267 | 169 | int* src, |
268 | - const unsigned char* pZeroOneInfos, | |
170 | + const int* pZeroOneInfos, | |
269 | 171 | size_t blockCount, size_t blockSize, |
270 | 172 | unsigned char* dest, size_t destLen, |
271 | 173 | unsigned char* tmp, |
@@ -305,7 +207,7 @@ | ||
305 | 207 | } |
306 | 208 | encodedValueSizes[0] = blockCount*blockSize; |
307 | 209 | Encode(&values[0], values.size(), 1, 0, tmp2, encodedValueSizes[0]); |
308 | - | |
210 | + | |
309 | 211 | values.clear(); |
310 | 212 | int maxV = 0; |
311 | 213 | for (size_t i=0; i<blockCount; ++i) { |
@@ -337,60 +239,12 @@ | ||
337 | 239 | } |
338 | 240 | |
339 | 241 | } |
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; | |
387 | 245 | dest += 4; |
388 | - dest += len; | |
246 | + dest += initialCompressedLen; | |
389 | 247 | |
390 | - cinfo.riceCoderParam = b; | |
391 | - cinfo.riceCodedLen = riceCodedLen; | |
392 | - cinfo.repeationCompressedLen = repeationCompressedLen; | |
393 | - | |
394 | 248 | label_end: |
395 | 249 | size_t destDiff = dest - initialDest; |
396 | 250 |
@@ -564,7 +418,7 @@ | ||
564 | 418 | size_t compress( |
565 | 419 | size_t hBlockCount, |
566 | 420 | size_t vBlockCount, |
567 | - const unsigned char* pZeroOneInfos, | |
421 | + const int* pZeroOneInfos, | |
568 | 422 | size_t zeroOneLimit, |
569 | 423 | CompressInfo compressInfos[8], |
570 | 424 | int* src, |
@@ -580,7 +434,7 @@ | ||
580 | 434 | size_t progress = 0; |
581 | 435 | |
582 | 436 | 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; | |
584 | 438 | size_t blockSize = 1 + i * 2; |
585 | 439 | progress += compressSub(from, p01, totalBlockCount, blockSize, dest+progress, destLen-progress, tmp1, tmp2, tmp3, compressInfos[i]); |
586 | 440 | from += totalBlockCount * blockSize; |
@@ -591,7 +445,7 @@ | ||
591 | 445 | |
592 | 446 | size_t decompressSub( |
593 | 447 | const unsigned char* src, |
594 | - const unsigned char* pZeroOneInfos, | |
448 | + const int* pZeroOneInfos, | |
595 | 449 | unsigned char* tmp, |
596 | 450 | int* tmp2, |
597 | 451 | int* dest, |
@@ -605,36 +459,19 @@ | ||
605 | 459 | unsigned char compressFlag = *src++; |
606 | 460 | |
607 | 461 | if (compressFlag != 3) { |
608 | - | |
609 | - unsigned char b = *src++; | |
610 | 462 | size_t len = *(size_t*)src; |
611 | 463 | src += 4; |
612 | 464 | |
613 | 465 | int len2 = 0; |
614 | 466 | switch (compressFlag) { |
615 | - case 0: | |
616 | - memcpy(tmp, src, len); | |
617 | - len2 = len; | |
618 | - break; | |
619 | 467 | case 1: |
620 | 468 | len2 = destLen*4; |
621 | - Decode((unsigned char*)src, len, (int*)tmp, len2); | |
469 | + Decode((unsigned char*)src, len, (int*)dest, len2); | |
622 | 470 | break; |
623 | - case 2: | |
624 | - len2 = repeationDecompress(src, len, tmp); | |
625 | - break; | |
471 | + default: | |
472 | + assert(false); | |
626 | 473 | } |
627 | 474 | 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 | - } | |
638 | 475 | }else { |
639 | 476 | size_t sizes[2] = {0}; |
640 | 477 | int resultSize = destLen * 4; |
@@ -997,7 +834,7 @@ | ||
997 | 834 | size_t decompress( |
998 | 835 | size_t hBlockCount, |
999 | 836 | size_t vBlockCount, |
1000 | - const unsigned char* pZeroOneInfos, | |
837 | + const int* pZeroOneInfos, | |
1001 | 838 | size_t zeroOneLimit, |
1002 | 839 | const unsigned char* src, size_t srcLen, |
1003 | 840 | unsigned char* tmp, int* tmp2, |
@@ -1010,7 +847,7 @@ | ||
1010 | 847 | int* to = dest; |
1011 | 848 | |
1012 | 849 | 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; | |
1014 | 851 | size_t blockSize = 1 + i * 2; |
1015 | 852 | from += decompressSub(from, p01, tmp, tmp2, to, totalBlockCount, blockSize); |
1016 | 853 | to += totalBlockCount * blockSize; |
@@ -437,6 +437,7 @@ | ||
437 | 437 | while (cnt < result_size) { |
438 | 438 | operation_buffer = readBits(operation_buffer, base-shift, code); |
439 | 439 | ID = operation_buffer/product; |
440 | + assert(0<=ID && ID<=Denominator); | |
440 | 441 | operation_buffer -= product * cumulative[symbol[ID]]; |
441 | 442 | product = SafeProduct(product, frequency[symbol[ID]], mask, shift); |
442 | 443 | result[cnt] = symbol[ID] + min_value - 1; |
@@ -64,7 +64,7 @@ | ||
64 | 64 | CompressInfo compressInfos[8] = {0}; |
65 | 65 | |
66 | 66 | Quantizer quantizer; |
67 | - quantizer.init(6*8+0, 0, 0, true); | |
67 | + quantizer.init(6*8+0, 0, 0, false); | |
68 | 68 | |
69 | 69 | size_t totalLen = 0; |
70 | 70 |
@@ -99,20 +99,19 @@ | ||
99 | 99 | size_t signFlagCount = collectInfos(pWork2, totalBlockCount, pSignFlags, compressInfos); |
100 | 100 | |
101 | 101 | 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]; | |
104 | 104 | |
105 | 105 | // quantizing zero one flags |
106 | 106 | *dest++ = zeroOneLimit; |
107 | 107 | 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; | |
114 | 112 | dest += 4; |
115 | - dest += bw.nBytes(); | |
113 | + memcpy(dest, &work3[0], encodedSize); | |
114 | + dest += encodedSize; | |
116 | 115 | }else { |
117 | 116 | pZeroOneInfos = 0; |
118 | 117 | } |
@@ -149,17 +148,15 @@ | ||
149 | 148 | |
150 | 149 | // zero one flags |
151 | 150 | 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]; | |
154 | 153 | if (zeroOneLimit != 0) { |
155 | 154 | uint32_t zeroOneFlagBytes = *(uint32_t*)src; |
156 | 155 | src += 4; |
157 | 156 | { |
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); | |
163 | 160 | } |
164 | 161 | src += zeroOneFlagBytes; |
165 | 162 | }else { |