2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
13 #define bool_coder_h 1
15 /* Arithmetic bool coder with largish probability range.
16 Timothy S Murphy 6 August 2004 */
18 /* So as not to force users to drag in too much of my idiosyncratic C++ world,
19 I avoid fancy storage management. */
26 typedef unsigned char vp8bc_index_t
; // probability index
28 /* There are a couple of slight variants in the details of finite-precision
29 arithmetic coding. May be safely ignored by most users. */
33 vp8bc_down
= 0, // just like VP8
34 vp8bc_down_full
= 1, // handles minimum probability correctly
40 /* Note that msvc by default does not inline _anything_ (regardless of the
41 setting of inline_depth) and that a command-line option (-Ob1 or -Ob2)
42 is required to inline even the smallest functions. */
44 # pragma inline_depth( 255) // I mean it when I inline something
45 # pragma warning( disable : 4099) // No class vs. struct harassment
46 # pragma warning( disable : 4250) // dominance complaints
47 # pragma warning( disable : 4284) // operator-> in templates
48 # pragma warning( disable : 4800) // bool conversion
50 // don't let prefix ++,-- stand in for postfix, disaster would ensue
52 # pragma warning( error : 4620 4621)
59 // Sometimes one wishes to be definite about integer lengths.
63 typedef const bool cbool
;
64 typedef const signed char cchar
;
65 typedef const short cshort
;
66 typedef const int cint
;
67 typedef const int clong
;
69 typedef const double cdouble
;
70 typedef const size_t csize_t
;
72 typedef unsigned char uchar
; // 8 bits
73 typedef const uchar cuchar
;
76 typedef unsigned short uint16
;
77 typedef const int16 cint16
;
78 typedef const uint16 cuint16
;
81 typedef unsigned int uint32
;
82 typedef const int32 cint32
;
83 typedef const uint32 cuint32
;
85 typedef unsigned int uint
;
86 typedef unsigned int ulong
;
87 typedef const uint cuint
;
88 typedef const ulong culong
;
91 // All structs consume space, may as well have a vptr.
97 struct bool_coder_spec
;
103 struct bool_coder_namespace
: int_types
105 typedef vp8bc_index_t Index
;
106 typedef bool_coder_spec Spec
;
107 typedef const Spec c_spec
;
112 down_full
= vp8bc_down_full
,
118 // Archivable specification of a bool coder includes rounding spec
119 // and probability mapping table. The latter replaces a uchar j
120 // (0 <= j < 256) with an arbitrary uint16 tbl[j] = p.
121 // p/65536 is then the probability of a zero.
123 struct bool_coder_spec
: bool_coder_namespace
125 friend struct bool_coder
;
126 friend struct bool_writer
;
127 friend struct bool_reader
;
128 friend struct bool_coder_spec_float
;
129 friend struct bool_coder_spec_explicit_table
;
130 friend struct bool_coder_spec_exponential_table
;
136 uint ebits
, mbits
, ebias
;
139 Index max_index
, half_index
;
141 uint32
mantissa(Index i
) const
143 assert(i
< half_index
);
144 return (1 << mbits
) + (i
& mmask
);
146 uint
exponent(Index i
) const
148 assert(i
< half_index
);
149 return ebias
- (i
>> mbits
);
152 uint16 Ptbl
[256]; // kinda clunky, but so is storage management.
154 /* Cost in bits of encoding a zero at every probability, scaled by 2^20.
155 Assumes that index is at most 8 bits wide. */
159 uint32
split(Index i
, uint32 R
) const // 1 <= split <= max( 1, R-1)
162 return 1 + (((R
- 1) * Ptbl
[i
]) >> 16);
165 return R
- split(max_index
- i
, R
);
167 return 1 + (((R
- 1) * mantissa(i
)) >> exponent(i
));
170 uint32
max_range() const
172 return (1 << w
) - (r
== down_full
? 0 : 1);
174 uint32
min_range() const
176 return (1 << (w
- 1)) + (r
== down_full
? 1 : 0);
180 return r
== Up
? 1 : 0;
183 void check_prec() const;
185 bool float_init(uint Ebits
, uint Mbits
);
190 uint prec
, Rounding rr
, uint Ebits
= 0, uint Mbits
= 0
194 float_init(Ebits
, Mbits
);
197 // Read complete spec from file.
198 bool_coder_spec(FILE *);
200 // Write spec to file.
201 void dump(FILE *) const;
203 // return probability index best approximating prob.
204 Index
operator()(double prob
) const;
206 // probability corresponding to index
207 double operator()(Index i
) const;
209 Index
complement(Index i
) const
211 return max_index
- i
;
214 Index
max_index() const
218 Index
half_index() const
223 uint32
cost_zero(Index i
) const
227 uint32
cost_one(Index i
) const
229 return Ctbl
[ max_index
- i
];
231 uint32
cost_bit(Index i
, bool b
) const
233 return Ctbl
[b
? max_index
-i
:i
];
238 /* Pseudo floating-point probability specification.
240 At least one of Ebits and Mbits must be nonzero.
242 Since all arithmetic is done at 32 bits, Ebits is at most 5.
244 Total significant bits in index is Ebits + Mbits + 1.
246 Below the halfway point (i.e. when the top significant bit is 0),
247 the index is (e << Mbits) + m.
249 The exponent e is between 0 and (2**Ebits) - 1,
250 the mantissa m is between 0 and (2**Mbits) - 1.
252 Prepending an implicit 1 to the mantissa, the probability is then
254 (2**Mbits + m) >> (e - 2**Ebits - 1 - Mbits),
256 which has (1/2)**(2**Ebits + 1) as a minimum
257 and (1/2) * [1 - 2**(Mbits + 1)] as a maximum.
259 When the index is above the halfway point, the probability is the
260 complement of the probability associated to the complement of the index.
262 Note that the probability increases with the index and that, because of
263 the symmetry, we cannot encode probability exactly 1/2; though we
264 can get as close to 1/2 as we like, provided we have enough Mbits.
266 The latter is of course not a problem in practice, one never has
267 exact probabilities and entropy errors are second order, that is, the
268 "overcoding" of a zero will be largely compensated for by the
269 "undercoding" of a one (or vice-versa).
271 Compared to arithmetic probability specs (a la VP8), this will do better
272 at very high and low probabilities and worse at probabilities near 1/2,
273 as well as facilitating the usage of wider or narrower probability indices.
276 struct bool_coder_spec_float
: bool_coder_spec
278 bool_coder_spec_float(
279 uint Ebits
= 3, uint Mbits
= 4, Rounding rr
= down_full
, uint prec
= 12
281 : bool_coder_spec(prec
, rr
, Ebits
, Mbits
)
288 struct bool_coder_spec_explicit_table
: bool_coder_spec
290 bool_coder_spec_explicit_table(
291 cuint16 probability_table
[256] = 0, // default is tbl[i] = i << 8.
292 Rounding
= down_full
,
297 // Contruct table via multiplicative interpolation between
298 // p[128] = 1/2 and p[0] = (1/2)^x.
299 // Since we are working with 16-bit precision, x is at most 16.
300 // For probabilities to increase with i, we must have x > 1.
301 // For 0 <= i <= 128, p[i] = (1/2)^{ 1 + [1 - (i/128)]*[x-1] }.
302 // Finally, p[128+i] = 1 - p[128 - i].
304 struct bool_coder_spec_exponential_table
: bool_coder_spec
306 bool_coder_spec_exponential_table(uint x
, Rounding
= down_full
, uint prec
= 16);
310 // Commonalities between writer and reader.
312 struct bool_coder
: bool_coder_namespace
314 friend struct bool_writer
;
315 friend struct bool_reader
;
326 Range
= spec
.max_range();
329 bool_coder(c_spec
&s
)
330 : min_range(s
.min_range()),
339 return 1 + ((Range
- 1) >> 1);
349 struct bool_writer
: bool_coder
353 uchar
*Bstart
, *Bend
, *B
;
360 bit_lag
= 32 - spec
.w
;
363 void raw(bool value
, uint32 split
);
365 bool_writer(c_spec
&, uchar
*Dest
, size_t Len
);
366 virtual ~bool_writer();
368 void operator()(Index p
, bool v
)
370 raw(v
, spec
.split(p
, Range
));
377 size_t bytes_written() const
382 // Call when done with input, flushes internal state.
383 // DO NOT write any more data after calling this.
385 bool_writer
&flush();
387 void write_bits(int n
, uint val
)
391 uint m
= 1 << (n
- 1);
395 raw((bool)(val
& m
), half());
402 // We are agnostic about storage management.
403 // By default, overflows throw an assert but user can
404 // override to provide an expanding buffer using ...
406 virtual void overflow(uint Len
) const;
408 // ... this function copies already-written data into new buffer
409 // and retains new buffer location.
411 void new_buffer(uchar
*dest
, uint Len
);
413 // Note that storage management is the user's responsibility.
418 // This could be adjusted to use a little less lookahead.
420 struct bool_reader
: bool_coder
424 cuchar
*const Bstart
; // for debugging
429 bool raw(uint32 split
);
431 bool_reader(c_spec
&s
, cuchar
*src
, size_t Len
);
433 bool operator()(Index p
)
435 return raw(spec
.split(p
, Range
));
438 uint
read_bits(int num_bits
)
442 while (--num_bits
>= 0)
443 v
+= v
+ (raw(half()) ? 1 : 0);
451 #endif /* __cplusplus */
456 typedef struct bool_coder_spec bool_coder_spec
;
457 typedef struct bool_writer bool_writer
;
458 typedef struct bool_reader bool_reader
;
460 typedef const bool_coder_spec c_bool_coder_spec
;
461 typedef const bool_writer c_bool_writer
;
462 typedef const bool_reader c_bool_reader
;
465 /* Optionally override default precision when constructing coder_specs.
466 Just pass a zero pointer if you don't care.
467 Precision is at most 16 bits for table specs, at most 23 otherwise. */
471 enum vp8bc_rounding r
; /* see top header file for def */
472 unsigned int prec
; /* range precision in bits */
475 typedef const struct vp8bc_prec vp8bc_c_prec
;
477 /* bool_coder_spec contains mapping of uchars to actual probabilities
478 (16 bit uints) as well as (usually immaterial) selection of
479 exact finite-precision algorithm used (for now, the latter can only
480 be overridden using the C++ interface).
481 See comments above the corresponding C++ constructors for discussion,
482 especially of exponential probability table generation. */
484 bool_coder_spec
*vp8bc_vp8spec(); // just like vp8
486 bool_coder_spec
*vp8bc_literal_spec(
487 const unsigned short prob_map
[256], // 0 is like vp8 w/more precision
491 bool_coder_spec
*vp8bc_float_spec(
492 unsigned int exponent_bits
, unsigned int mantissa_bits
, vp8bc_c_prec
*
495 bool_coder_spec
*vp8bc_exponential_spec(unsigned int min_exp
, vp8bc_c_prec
*);
497 bool_coder_spec
*vp8bc_spec_from_file(FILE *);
500 void vp8bc_destroy_spec(c_bool_coder_spec
*);
502 void vp8bc_spec_to_file(c_bool_coder_spec
*, FILE *);
505 /* Nearest index to supplied probability of zero, 0 <= prob <= 1. */
507 vp8bc_index_t
vp8bc_index(c_bool_coder_spec
*, double prob
);
509 vp8bc_index_t
vp8bc_index_from_counts(
510 c_bool_coder_spec
*p
, unsigned int zero_ct
, unsigned int one_ct
513 /* In case you want to look */
515 double vp8bc_probability(c_bool_coder_spec
*, vp8bc_index_t
);
519 vp8bc_index_t
vp8bc_complement(c_bool_coder_spec
*, vp8bc_index_t
);
521 /* Cost in bits of encoding a zero at given probability, scaled by 2^20.
522 (assumes that an int holds at least 32 bits). */
524 unsigned int vp8bc_cost_zero(c_bool_coder_spec
*, vp8bc_index_t
);
526 unsigned int vp8bc_cost_one(c_bool_coder_spec
*, vp8bc_index_t
);
527 unsigned int vp8bc_cost_bit(c_bool_coder_spec
*, vp8bc_index_t
, int);
530 /* bool_writer interface */
532 /* Length = 0 disables checking for writes beyond buffer end. */
534 bool_writer
*vp8bc_create_writer(
535 c_bool_coder_spec
*, unsigned char *Destination
, size_t Length
538 /* Flushes out any buffered data and returns total # of bytes written. */
540 size_t vp8bc_destroy_writer(bool_writer
*);
542 void vp8bc_write_bool(bool_writer
*, int boolean_val
, vp8bc_index_t false_prob
);
544 void vp8bc_write_bits(
545 bool_writer
*, unsigned int integer_value
, int number_of_bits
548 c_bool_coder_spec
*vp8bc_writer_spec(c_bool_writer
*);
551 /* bool_reader interface */
553 /* Length = 0 disables checking for reads beyond buffer end. */
555 bool_reader
*vp8bc_create_reader(
556 c_bool_coder_spec
*, const unsigned char *Source
, size_t Length
558 void vp8bc_destroy_reader(bool_reader
*);
560 int vp8bc_read_bool(bool_reader
*, vp8bc_index_t false_prob
);
562 unsigned int vp8bc_read_bits(bool_reader
*, int number_of_bits
);
564 c_bool_coder_spec
*vp8bc_reader_spec(c_bool_reader
*);
570 #endif /* bool_coder_h */