integercompressor.cpp
Go to the documentation of this file.
1 /*
2 ===============================================================================
3 
4  FILE: integercompressor.cpp
5 
6  CONTENTS:
7 
8  see corresponding header file
9 
10  PROGRAMMERS:
11 
12  martin.isenburg@gmail.com
13 
14  COPYRIGHT:
15 
16  (c) 2011, Martin Isenburg, LASSO - tools to catch reality
17 
18  This is free software; you can redistribute and/or modify it under the
19  terms of the GNU Lesser General Licence as published by the Free Software
20  Foundation. See the COPYING file for more information.
21 
22  This software is distributed WITHOUT ANY WARRANTY and without even the
23  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 
25  CHANGE HISTORY:
26 
27  see corresponding header file
28 
29 ===============================================================================
30 */
31 #include "integercompressor.hpp"
32 
33 #define COMPRESS_ONLY_K
34 #undef COMPRESS_ONLY_K
35 
36 #define CREATE_HISTOGRAMS
37 #undef CREATE_HISTOGRAMS
38 
39 #include <stdlib.h>
40 #include <assert.h>
41 
42 #ifdef CREATE_HISTOGRAMS
43 #include <math.h>
44 #endif
45 
46 IntegerCompressor::IntegerCompressor(EntropyEncoder* enc, U32 bits, U32 contexts, U32 bits_high, U32 range)
47 {
48  assert(enc);
49  this->enc = enc;
50  this->dec = 0;
51  this->bits = bits;
52  this->contexts = contexts;
53  this->bits_high = bits_high;
54  this->range = range;
55 
56  if (range) // the corrector's significant bits and range
57  {
58  corr_bits = 0;
59  corr_range = range;
60  while (range)
61  {
62  range = range >> 1;
63  corr_bits++;
64  }
65  if (corr_range == (1u << (corr_bits-1)))
66  {
67  corr_bits--;
68  }
69  // the corrector must fall into this interval
70  corr_min = -((I32)(corr_range/2));
72  }
73  else if (bits && bits < 32)
74  {
75  corr_bits = bits;
76  corr_range = 1u << bits;
77  // the corrector must fall into this interval
78  corr_min = -((I32)(corr_range/2));
80  }
81  else
82  {
83  corr_bits = 32;
84  corr_range = 0;
85  // the corrector must fall into this interval
86  corr_min = I32_MIN;
87  corr_max = I32_MAX;
88  }
89 
90  k = 0;
91 
92  mBits = 0;
93  mCorrector = 0;
94 
95 #ifdef CREATE_HISTOGRAMS
96  corr_histogram = (int**)malloc(sizeof(int*) * (corr_bits+1));
97  for (int k = 0; k <= corr_bits; k++)
98  {
99  corr_histogram[k] = (int*)malloc(sizeof(int) * ((1<<k)+1));
100  for (int c = 0; c <= (1<<k); c++)
101  {
102  corr_histogram[k][c] = 0;
103  }
104  }
105 #endif
106 }
107 
109 {
110  assert(dec);
111  this->enc = 0;
112  this->dec = dec;
113  this->bits = bits;
114  this->contexts = contexts;
115  this->bits_high = bits_high;
116  this->range = range;
117 
118  if (range) // the corrector's significant bits and range
119  {
120  corr_bits = 0;
121  corr_range = range;
122  while (range)
123  {
124  range = range >> 1;
125  corr_bits++;
126  }
127  if (corr_range == (1u << (corr_bits-1)))
128  {
129  corr_bits--;
130  }
131  // the corrector must fall into this interval
132  corr_min = -((I32)(corr_range/2));
133  corr_max = corr_min + corr_range - 1;
134  }
135  else if (bits && bits < 32)
136  {
137  corr_bits = bits;
138  corr_range = 1u << bits;
139  // the corrector must fall into this interval
140  corr_min = -((I32)(corr_range/2));
141  corr_max = corr_min + corr_range - 1;
142  }
143  else
144  {
145  corr_bits = 32;
146  corr_range = 0;
147  // the corrector must fall into this interval
148  corr_min = I32_MIN;
149  corr_max = I32_MAX;
150  }
151 
152  k = 0;
153 
154  mBits = 0;
155  mCorrector = 0;
156 }
157 
159 {
160  U32 i;
161  if (mBits)
162  {
163  for (i = 0; i < contexts; i++)
164  {
165  if (enc) enc->destroySymbolModel(mBits[i]);
166  else dec->destroySymbolModel(mBits[i]);
167  }
168  delete [] mBits;
169  }
170 #ifndef COMPRESS_ONLY_K
171  if (mCorrector)
172  {
173  if (enc) enc->destroyBitModel(mCorrector[0]);
174  else dec->destroyBitModel(mCorrector[0]);
175  for (i = 1; i <= corr_bits; i++)
176  {
179  }
180  delete [] mCorrector;
181  }
182 #endif
183 
184 #ifdef CREATE_HISTOGRAMS
185  if (end)
186  {
187  int total_number = 0;
188  double total_entropy = 0.0f;
189  double total_raw = 0.0f;
190  for (int k = 0; k <= corr_bits; k++)
191  {
192  int number = 0;
193  int different = 0;
194  for (int c = 0; c <= (1<<k); c++)
195  {
196  number += corr_histogram[k][c];
197  }
198  double prob,entropy = 0.0f;
199  for (c = 0; c <= (1<<k); c++)
200  {
201  if (corr_histogram[k][c])
202  {
203  different++;
204  prob = (double)corr_histogram[k][c]/(double)number;
205  entropy -= log(prob)*prob/log(2.0);
206  }
207  }
208  fprintf(stderr, "k: %d number: %d different: %d entropy: %lg raw: %1.1f\n",k,number,different,entropy, (float)(k?k:1));
209  total_number += number;
210  total_entropy += (entropy*number);
211  total_raw += ((k?k:1)*number);
212  }
213  fprintf(stderr, "TOTAL: number: %d entropy: %lg raw: %lg\n",total_number,total_entropy/total_number,total_raw/total_number);
214  }
215 #endif
216 }
217 
219 {
220  U32 i;
221 
222  assert(enc);
223 
224  // maybe create the models
225  if (mBits == 0)
226  {
227  mBits = new EntropyModel*[contexts];
228  for (i = 0; i < contexts; i++)
229  {
231  }
232 #ifndef COMPRESS_ONLY_K
233  mCorrector = new EntropyModel*[corr_bits+1];
234  mCorrector[0] = (EntropyModel*)enc->createBitModel();
235  for (i = 1; i <= corr_bits; i++)
236  {
237  if (i <= bits_high)
238  {
239  mCorrector[i] = enc->createSymbolModel(1<<i);
240  }
241  else
242  {
244  }
245  }
246 #endif
247  }
248 
249  // certainly init the models
250  for (i = 0; i < contexts; i++)
251  {
253  }
254 #ifndef COMPRESS_ONLY_K
256  for (i = 1; i <= corr_bits; i++)
257  {
259  }
260 #endif
261 }
262 
263 void IntegerCompressor::compress(I32 pred, I32 real, U32 context)
264 {
265  assert(enc);
266  // the corrector will be within the interval [ - (corr_range - 1) ... + (corr_range - 1) ]
267  I32 corr = real - pred;
268  // we fold the corrector into the interval [ corr_min ... corr_max ]
269  if (corr < corr_min) corr += corr_range;
270  else if (corr > corr_max) corr -= corr_range;
271  writeCorrector(corr, mBits[context]);
272 }
273 
275 {
276  U32 i;
277 
278  assert(dec);
279 
280  // maybe create the models
281  if (mBits == 0)
282  {
283  mBits = new EntropyModel*[contexts];
284  for (i = 0; i < contexts; i++)
285  {
286  mBits[i] = (EntropyModel*)dec->createSymbolModel(corr_bits+1);
287  }
288 #ifndef COMPRESS_ONLY_K
289  mCorrector = new EntropyModel*[corr_bits+1];
290  mCorrector[0] = (EntropyModel*)dec->createBitModel();
291  for (i = 1; i <= corr_bits; i++)
292  {
293  if (i <= bits_high)
294  {
295  mCorrector[i] = (EntropyModel*)dec->createSymbolModel(1<<i);
296  }
297  else
298  {
299  mCorrector[i] = (EntropyModel*)dec->createSymbolModel(1<<bits_high);
300  }
301  }
302 #endif
303  }
304 
305  // certainly init the models
306  for (i = 0; i < contexts; i++)
307  {
309  }
310 #ifndef COMPRESS_ONLY_K
312  for (i = 1; i <= corr_bits; i++)
313  {
315  }
316 #endif
317 }
318 
320 {
321  assert(dec);
322  I32 real = pred + readCorrector(mBits[context]);
323  if (real < 0) real += corr_range;
324  else if ((U32)(real) >= corr_range) real -= corr_range;
325  return real;
326 }
327 
328 /*
329 static const char log_table256[256] =
330 {
331  -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
332  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
333  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
334  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
335  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
336  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
337  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
338  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
339  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
340  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
341  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
342  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
343  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
344  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
345  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
346  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
347 };
348 
349 unsigned int v; // 32-bit word to find the log of
350 unsigned r; // r will be lg(v)
351 register unsigned int t, tt; // temporaries
352 
353 if (tt = v >> 16)
354 {
355  r = (t = tt >> 8) ? 24 + LogTable256[t] : 16 + LogTable256[tt];
356 }
357 else
358 {
359  r = (t = v >> 8) ? 8 + LogTable256[t] : LogTable256[v];
360 }
361 */
362 
364 {
365  U32 c1;
366 
367  // find the tighest interval [ - (2^k - 1) ... + (2^k) ] that contains c
368 
369  k = 0;
370 
371  // do this by checking the absolute value of c (adjusted for the case that c is 2^k)
372 
373  c1 = (c <= 0 ? -c : c-1);
374 
375  // this loop could be replaced with more efficient code
376 
377  while (c1)
378  {
379  c1 = c1 >> 1;
380  k = k + 1;
381  }
382 
383  // the number k is between 0 and corr_bits and describes the interval the corrector falls into
384  // we can compress the exact location of c within this interval using k bits
385 
386  enc->encodeSymbol(mBits, k);
387 
388 #ifdef COMPRESS_ONLY_K
389  if (k) // then c is either smaller than 0 or bigger than 1
390  {
391  assert((c != 0) && (c != 1));
392  if (k < 32)
393  {
394  // translate the corrector c into the k-bit interval [ 0 ... 2^k - 1 ]
395  if (c < 0) // then c is in the interval [ - (2^k - 1) ... - (2^(k-1)) ]
396  {
397  // so we translate c into the interval [ 0 ... + 2^(k-1) - 1 ] by adding (2^k - 1)
398  enc->writeBits(k, c + ((1<<k) - 1));
399 #ifdef CREATE_HISTOGRAMS
400  corr_histogram[k][c + ((1<<k) - 1)]++;
401 #endif
402  }
403  else // then c is in the interval [ 2^(k-1) + 1 ... 2^k ]
404  {
405  // so we translate c into the interval [ 2^(k-1) ... + 2^k - 1 ] by subtracting 1
406  enc->writeBits(k, c - 1);
407 #ifdef CREATE_HISTOGRAMS
408  corr_histogram[k][c - 1]++;
409 #endif
410  }
411  }
412  }
413  else // then c is 0 or 1
414  {
415  assert((c == 0) || (c == 1));
416  enc->writeBit(c);
417 #ifdef CREATE_HISTOGRAMS
418  corr_histogram[0][c]++;
419 #endif
420  }
421 #else // COMPRESS_ONLY_K
422  if (k) // then c is either smaller than 0 or bigger than 1
423  {
424  assert((c != 0) && (c != 1));
425  if (k < 32)
426  {
427  // translate the corrector c into the k-bit interval [ 0 ... 2^k - 1 ]
428  if (c < 0) // then c is in the interval [ - (2^k - 1) ... - (2^(k-1)) ]
429  {
430  // so we translate c into the interval [ 0 ... + 2^(k-1) - 1 ] by adding (2^k - 1)
431  c += ((1<<k) - 1);
432  }
433  else // then c is in the interval [ 2^(k-1) + 1 ... 2^k ]
434  {
435  // so we translate c into the interval [ 2^(k-1) ... + 2^k - 1 ] by subtracting 1
436  c -= 1;
437  }
438  if (k <= bits_high) // for small k we code the interval in one step
439  {
440  // compress c with the range coder
442  }
443  else // for larger k we need to code the interval in two steps
444  {
445  // figure out how many lower bits there are
446  int k1 = k-bits_high;
447  // c1 represents the lowest k-bits_high+1 bits
448  c1 = c & ((1<<k1) - 1);
449  // c represents the highest bits_high bits
450  c = c >> k1;
451  // compress the higher bits using a context table
453  // store the lower k1 bits raw
454  enc->writeBits(k1, c1);
455  }
456  }
457  }
458  else // then c is 0 or 1
459  {
460  assert((c == 0) || (c == 1));
461  enc->encodeBit(mCorrector[0],c);
462  }
463 #endif // COMPRESS_ONLY_K
464 }
465 
467 {
468  I32 c;
469 
470  // decode within which interval the corrector is falling
471 
472  k = dec->decodeSymbol(mBits);
473 
474  // decode the exact location of the corrector within the interval
475 
476 #ifdef COMPRESS_ONLY_K
477  if (k) // then c is either smaller than 0 or bigger than 1
478  {
479  if (k < 32)
480  {
481  c = dec->readBits(k);
482 
483  if (c >= (1<<(k-1))) // if c is in the interval [ 2^(k-1) ... + 2^k - 1 ]
484  {
485  // so we translate c back into the interval [ 2^(k-1) + 1 ... 2^k ] by adding 1
486  c += 1;
487  }
488  else // otherwise c is in the interval [ 0 ... + 2^(k-1) - 1 ]
489  {
490  // so we translate c back into the interval [ - (2^k - 1) ... - (2^(k-1)) ] by subtracting (2^k - 1)
491  c -= ((1<<k) - 1);
492  }
493  }
494  else
495  {
496  c = corr_min;
497  }
498  }
499  else // then c is either 0 or 1
500  {
501  c = dec->readBit();
502  }
503 #else // COMPRESS_ONLY_K
504  if (k) // then c is either smaller than 0 or bigger than 1
505  {
506  if (k < 32)
507  {
508  if (k <= bits_high) // for small k we can do this in one step
509  {
510  // decompress c with the range coder
511  c = dec->decodeSymbol(mCorrector[k]);
512  }
513  else
514  {
515  // for larger k we need to do this in two steps
516  int k1 = k-bits_high;
517  // decompress higher bits with table
518  c = dec->decodeSymbol(mCorrector[k]);
519  // read lower bits raw
520  int c1 = dec->readBits(k1);
521  // put the corrector back together
522  c = (c << k1) | c1;
523  }
524  // translate c back into its correct interval
525  if (c >= (1<<(k-1))) // if c is in the interval [ 2^(k-1) ... + 2^k - 1 ]
526  {
527  // so we translate c back into the interval [ 2^(k-1) + 1 ... 2^k ] by adding 1
528  c += 1;
529  }
530  else // otherwise c is in the interval [ 0 ... + 2^(k-1) - 1 ]
531  {
532  // so we translate c back into the interval [ - (2^k - 1) ... - (2^(k-1)) ] by subtracting (2^k - 1)
533  c -= ((1<<k) - 1);
534  }
535  }
536  else
537  {
538  c = corr_min;
539  }
540  }
541  else // then c is either 0 or 1
542  {
543  c = dec->decodeBit(mCorrector[0]);
544  }
545 #endif // COMPRESS_ONLY_K
546 
547  return c;
548 }
I32 decompress(I32 iPred, U32 context=0)
virtual void encodeSymbol(EntropyModel *model, U32 sym)=0
virtual void initBitModel(EntropyModel *model)=0
EntropyModel ** mBits
virtual U32 decodeSymbol(EntropyModel *model)=0
virtual void destroyBitModel(EntropyModel *model)=0
unsigned int U32
Definition: mydefs.hpp:39
EntropyDecoder * dec
virtual void destroyBitModel(EntropyModel *model)=0
EntropyModel ** mCorrector
#define I32_MAX
Definition: mydefs.hpp:89
virtual void destroySymbolModel(EntropyModel *model)=0
virtual void destroySymbolModel(EntropyModel *model)=0
virtual EntropyModel * createBitModel()=0
virtual void encodeBit(EntropyModel *model, U32 bit)=0
virtual void initSymbolModel(EntropyModel *model, U32 *init=0)=0
IntegerCompressor(EntropyEncoder *enc, U32 bits=16, U32 contexts=1, U32 bits_high=8, U32 range=0)
I32 readCorrector(EntropyModel *model)
virtual EntropyModel * createSymbolModel(U32 n)=0
virtual U32 decodeBit(EntropyModel *model)=0
int I32
Definition: mydefs.hpp:35
virtual EntropyModel * createSymbolModel(U32 n)=0
void writeCorrector(I32 c, EntropyModel *model)
EntropyEncoder * enc
virtual U32 readBit()=0
void compress(I32 iPred, I32 iReal, U32 context=0)
virtual void initBitModel(EntropyModel *model)=0
virtual EntropyModel * createBitModel()=0
virtual void writeBits(U32 bits, U32 sym)=0
#define I32_MIN
Definition: mydefs.hpp:88
virtual void initSymbolModel(EntropyModel *model, U32 *init=0)=0
virtual U32 readBits(U32 bits)=0
virtual void writeBit(U32 sym)=0


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:06