1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
11
12#ifndef bool_coder_h
13#define bool_coder_h 1
14
15/* Arithmetic bool coder with largish probability range.
16   Timothy S Murphy  6 August 2004 */
17
18/* So as not to force users to drag in too much of my idiosyncratic C++ world,
19   I avoid fancy storage management. */
20
21#include <assert.h>
22
23#include <stddef.h>
24#include <stdio.h>
25
26typedef unsigned char vp8bc_index_t; // probability index
27
28/* There are a couple of slight variants in the details of finite-precision
29   arithmetic coding.  May be safely ignored by most users. */
30
31enum vp8bc_rounding
32{
33    vp8bc_down = 0,     // just like VP8
34    vp8bc_down_full = 1, // handles minimum probability correctly
35    vp8bc_up = 2
36};
37
38#if _MSC_VER
39
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. */
43
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
49
50// don't let prefix ++,-- stand in for postfix, disaster would ensue
51
52#   pragma warning( error : 4620 4621)
53
54#endif  // _MSC_VER
55
56
57#if __cplusplus
58
59// Sometimes one wishes to be definite about integer lengths.
60
61struct int_types
62{
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;
68
69    typedef const double cdouble;
70    typedef const size_t csize_t;
71
72    typedef unsigned char uchar;    // 8 bits
73    typedef const uchar cuchar;
74
75    typedef short int16;
76    typedef unsigned short uint16;
77    typedef const int16 cint16;
78    typedef const uint16 cuint16;
79
80    typedef int int32;
81    typedef unsigned int uint32;
82    typedef const int32 cint32;
83    typedef const uint32 cuint32;
84
85    typedef unsigned int uint;
86    typedef unsigned int ulong;
87    typedef const uint cuint;
88    typedef const ulong culong;
89
90
91    // All structs consume space, may as well have a vptr.
92
93    virtual ~int_types();
94};
95
96
97struct bool_coder_spec;
98struct bool_coder;
99struct bool_writer;
100struct bool_reader;
101
102
103struct bool_coder_namespace : int_types
104{
105    typedef vp8bc_index_t Index;
106    typedef bool_coder_spec Spec;
107    typedef const Spec c_spec;
108
109    enum Rounding
110    {
111        Down = vp8bc_down,
112        down_full = vp8bc_down_full,
113        Up = vp8bc_up
114    };
115};
116
117
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.
122
123struct bool_coder_spec : bool_coder_namespace
124{
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;
131    friend struct BPsrc;
132private:
133    uint w;                 // precision
134    Rounding r;
135
136    uint ebits, mbits, ebias;
137    uint32 mmask;
138
139    Index max_index, half_index;
140
141    uint32 mantissa(Index i) const
142    {
143        assert(i < half_index);
144        return (1 << mbits) + (i & mmask);
145    }
146    uint exponent(Index i) const
147    {
148        assert(i < half_index);
149        return ebias - (i >> mbits);
150    }
151
152    uint16 Ptbl[256];       // kinda clunky, but so is storage management.
153
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. */
156
157    uint32 Ctbl[256];
158
159    uint32 split(Index i, uint32 R) const    // 1 <= split <= max( 1, R-1)
160    {
161        if (!ebias)
162            return 1 + (((R - 1) * Ptbl[i]) >> 16);
163
164        if (i >= half_index)
165            return R - split(max_index - i, R);
166
167        return 1 + (((R - 1) * mantissa(i)) >> exponent(i));
168    }
169
170    uint32 max_range() const
171    {
172        return (1 << w) - (r == down_full ? 0 : 1);
173    }
174    uint32 min_range() const
175    {
176        return (1 << (w - 1)) + (r == down_full ? 1 : 0);
177    }
178    uint32 Rinc() const
179    {
180        return r == Up ? 1 : 0;
181    }
182
183    void check_prec() const;
184
185    bool float_init(uint Ebits, uint Mbits);
186
187    void cost_init();
188
189    bool_coder_spec(
190        uint prec, Rounding rr, uint Ebits = 0, uint Mbits = 0
191    )
192        : w(prec), r(rr)
193    {
194        float_init(Ebits, Mbits);
195    }
196public:
197    // Read complete spec from file.
198    bool_coder_spec(FILE *);
199
200    // Write spec to file.
201    void dump(FILE *) const;
202
203    // return probability index best approximating prob.
204    Index operator()(double prob) const;
205
206    // probability corresponding to index
207    double operator()(Index i) const;
208
209    Index complement(Index i) const
210    {
211        return max_index - i;
212    }
213
214    Index max_index() const
215    {
216        return max_index;
217    }
218    Index half_index() const
219    {
220        return half_index;
221    }
222
223    uint32 cost_zero(Index i) const
224    {
225        return Ctbl[i];
226    }
227    uint32 cost_one(Index i) const
228    {
229        return Ctbl[ max_index - i];
230    }
231    uint32 cost_bit(Index i, bool b) const
232    {
233        return Ctbl[b? max_index-i:i];
234    }
235};
236
237
238/* Pseudo floating-point probability specification.
239
240   At least one of Ebits and Mbits must be nonzero.
241
242   Since all arithmetic is done at 32 bits, Ebits is at most 5.
243
244   Total significant bits in index is Ebits + Mbits + 1.
245
246   Below the halfway point (i.e. when the top significant bit is 0),
247   the index is (e << Mbits) + m.
248
249   The exponent e is between 0 and (2**Ebits) - 1,
250   the mantissa m is between 0 and (2**Mbits) - 1.
251
252   Prepending an implicit 1 to the mantissa, the probability is then
253
254        (2**Mbits + m) >> (e - 2**Ebits - 1 - Mbits),
255
256   which has (1/2)**(2**Ebits + 1) as a minimum
257   and (1/2) * [1 - 2**(Mbits + 1)] as a maximum.
258
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.
261
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.
265
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).
270
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.
274*/
275
276struct bool_coder_spec_float : bool_coder_spec
277{
278    bool_coder_spec_float(
279        uint Ebits = 3, uint Mbits = 4, Rounding rr = down_full, uint prec = 12
280    )
281        : bool_coder_spec(prec, rr, Ebits, Mbits)
282    {
283        cost_init();
284    }
285};
286
287
288struct bool_coder_spec_explicit_table : bool_coder_spec
289{
290    bool_coder_spec_explicit_table(
291        cuint16 probability_table[256] = 0,  // default is tbl[i] = i << 8.
292        Rounding = down_full,
293        uint precision = 16
294    );
295};
296
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].
303
304struct bool_coder_spec_exponential_table : bool_coder_spec
305{
306    bool_coder_spec_exponential_table(uint x, Rounding = down_full, uint prec = 16);
307};
308
309
310// Commonalities between writer and reader.
311
312struct bool_coder : bool_coder_namespace
313{
314    friend struct bool_writer;
315    friend struct bool_reader;
316    friend struct BPsrc;
317private:
318    uint32 Low, Range;
319    cuint32 min_range;
320    cuint32 rinc;
321    c_spec spec;
322
323    void _reset()
324    {
325        Low = 0;
326        Range = spec.max_range();
327    }
328
329    bool_coder(c_spec &s)
330        :  min_range(s.min_range()),
331           rinc(s.Rinc()),
332           spec(s)
333    {
334        _reset();
335    }
336
337    uint32 half() const
338    {
339        return 1 + ((Range - 1) >> 1);
340    }
341public:
342    c_spec &Spec() const
343    {
344        return spec;
345    }
346};
347
348
349struct bool_writer : bool_coder
350{
351    friend struct BPsrc;
352private:
353    uchar *Bstart, *Bend, *B;
354    int bit_lag;
355    bool is_toast;
356    void carry();
357    void reset()
358    {
359        _reset();
360        bit_lag = 32 - spec.w;
361        is_toast = 0;
362    }
363    void raw(bool value, uint32 split);
364public:
365    bool_writer(c_spec &, uchar *Dest, size_t Len);
366    virtual ~bool_writer();
367
368    void operator()(Index p, bool v)
369    {
370        raw(v, spec.split(p, Range));
371    }
372
373    uchar *buf() const
374    {
375        return Bstart;
376    }
377    size_t bytes_written() const
378    {
379        return B - Bstart;
380    }
381
382    // Call when done with input, flushes internal state.
383    // DO NOT write any more data after calling this.
384
385    bool_writer &flush();
386
387    void write_bits(int n, uint val)
388    {
389        if (n)
390        {
391            uint m = 1 << (n - 1);
392
393            do
394            {
395                raw((bool)(val & m), half());
396            }
397            while (m >>= 1);
398        }
399    }
400
401#   if 0
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 ...
405
406    virtual void overflow(uint Len) const;
407
408    // ... this function copies already-written data into new buffer
409    // and retains new buffer location.
410
411    void new_buffer(uchar *dest, uint Len);
412
413    // Note that storage management is the user's responsibility.
414#   endif
415};
416
417
418// This could be adjusted to use a little less lookahead.
419
420struct bool_reader : bool_coder
421{
422    friend struct BPsrc;
423private:
424    cuchar *const Bstart;   // for debugging
425    cuchar *B;
426    cuchar *const Bend;
427    cuint shf;
428    uint bct;
429    bool raw(uint32 split);
430public:
431    bool_reader(c_spec &s, cuchar *src, size_t Len);
432
433    bool operator()(Index p)
434    {
435        return raw(spec.split(p, Range));
436    }
437
438    uint read_bits(int num_bits)
439    {
440        uint v = 0;
441
442        while (--num_bits >= 0)
443            v += v + (raw(half()) ? 1 : 0);
444
445        return v;
446    }
447};
448
449extern "C" {
450
451#endif /*  __cplusplus */
452
453
454    /* C interface */
455
456    typedef struct bool_coder_spec bool_coder_spec;
457    typedef struct bool_writer bool_writer;
458    typedef struct bool_reader bool_reader;
459
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;
463
464
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. */
468
469    struct vp8bc_prec
470    {
471        enum vp8bc_rounding r;      /* see top header file for def */
472        unsigned int prec;          /* range precision in bits */
473    };
474
475    typedef const struct vp8bc_prec vp8bc_c_prec;
476
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. */
483
484    bool_coder_spec *vp8bc_vp8spec(); // just like vp8
485
486    bool_coder_spec *vp8bc_literal_spec(
487        const unsigned short prob_map[256],  // 0 is like vp8 w/more precision
488        vp8bc_c_prec*
489    );
490
491    bool_coder_spec *vp8bc_float_spec(
492        unsigned int exponent_bits, unsigned int mantissa_bits, vp8bc_c_prec*
493    );
494
495    bool_coder_spec *vp8bc_exponential_spec(unsigned int min_exp, vp8bc_c_prec *);
496
497    bool_coder_spec *vp8bc_spec_from_file(FILE *);
498
499
500    void vp8bc_destroy_spec(c_bool_coder_spec *);
501
502    void vp8bc_spec_to_file(c_bool_coder_spec *, FILE *);
503
504
505    /* Nearest index to supplied probability of zero, 0 <= prob <= 1. */
506
507    vp8bc_index_t vp8bc_index(c_bool_coder_spec *, double prob);
508
509    vp8bc_index_t vp8bc_index_from_counts(
510        c_bool_coder_spec *p, unsigned int zero_ct, unsigned int one_ct
511    );
512
513    /* In case you want to look */
514
515    double vp8bc_probability(c_bool_coder_spec *, vp8bc_index_t);
516
517    /* Opposite index */
518
519    vp8bc_index_t vp8bc_complement(c_bool_coder_spec *, vp8bc_index_t);
520
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). */
523
524    unsigned int vp8bc_cost_zero(c_bool_coder_spec *, vp8bc_index_t);
525
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);
528
529
530    /* bool_writer interface */
531
532    /* Length = 0 disables checking for writes beyond buffer end. */
533
534    bool_writer *vp8bc_create_writer(
535        c_bool_coder_spec *, unsigned char *Destination, size_t Length
536    );
537
538    /* Flushes out any buffered data and returns total # of bytes written. */
539
540    size_t vp8bc_destroy_writer(bool_writer *);
541
542    void vp8bc_write_bool(bool_writer *, int boolean_val, vp8bc_index_t false_prob);
543
544    void vp8bc_write_bits(
545        bool_writer *, unsigned int integer_value, int number_of_bits
546    );
547
548    c_bool_coder_spec *vp8bc_writer_spec(c_bool_writer *);
549
550
551    /* bool_reader interface */
552
553    /* Length = 0 disables checking for reads beyond buffer end. */
554
555    bool_reader *vp8bc_create_reader(
556        c_bool_coder_spec *, const unsigned char *Source, size_t Length
557    );
558    void vp8bc_destroy_reader(bool_reader *);
559
560    int vp8bc_read_bool(bool_reader *, vp8bc_index_t false_prob);
561
562    unsigned int vp8bc_read_bits(bool_reader *, int number_of_bits);
563
564    c_bool_coder_spec *vp8bc_reader_spec(c_bool_reader *);
565
566#if __cplusplus
567}
568#endif
569
570#endif  /* bool_coder_h */
571