summaryrefslogtreecommitdiffstats
path: root/vp8/common/boolcoder.h
blob: 0659d48737632ee7021b7c4142def404a1cf3f54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license and patent
 *  grant that can be found in the LICENSE file in the root of the source
 *  tree. All contributing project authors may be found in the AUTHORS
 *  file in the root of the source tree.
 */


#ifndef bool_coder_h
#define bool_coder_h 1

/* Arithmetic bool coder with largish probability range.
   Timothy S Murphy  6 August 2004 */

/* So as not to force users to drag in too much of my idiosyncratic C++ world,
   I avoid fancy storage management. */

#include <assert.h>

#include <stddef.h>
#include <stdio.h>

typedef unsigned char vp8bc_index_t; // probability index

/* There are a couple of slight variants in the details of finite-precision
   arithmetic coding.  May be safely ignored by most users. */

enum vp8bc_rounding
{
    vp8bc_down = 0,     // just like VP8
    vp8bc_down_full = 1, // handles minimum probability correctly
    vp8bc_up = 2
};

#if _MSC_VER

/* Note that msvc by default does not inline _anything_ (regardless of the
   setting of inline_depth) and that a command-line option (-Ob1 or -Ob2)
   is required to inline even the smallest functions. */

#   pragma inline_depth( 255)           // I mean it when I inline something
#   pragma warning( disable : 4099)     // No class vs. struct harassment
#   pragma warning( disable : 4250)     // dominance complaints
#   pragma warning( disable : 4284)     // operator-> in templates
#   pragma warning( disable : 4800)     // bool conversion

// don't let prefix ++,-- stand in for postfix, disaster would ensue

#   pragma warning( error : 4620 4621)

#endif  // _MSC_VER


#if __cplusplus

// Sometimes one wishes to be definite about integer lengths.

struct int_types
{
    typedef const bool cbool;
    typedef const signed char cchar;
    typedef const short cshort;
    typedef const int cint;
    typedef const int clong;

    typedef const double cdouble;
    typedef const size_t csize_t;

    typedef unsigned char uchar;    // 8 bits
    typedef const uchar cuchar;

    typedef short int16;
    typedef unsigned short uint16;
    typedef const int16 cint16;
    typedef const uint16 cuint16;

    typedef int int32;
    typedef unsigned int uint32;
    typedef const int32 cint32;
    typedef const uint32 cuint32;

    typedef unsigned int uint;
    typedef unsigned int ulong;
    typedef const uint cuint;
    typedef const ulong culong;


    // All structs consume space, may as well have a vptr.

    virtual ~int_types();
};


struct bool_coder_spec;
struct bool_coder;
struct bool_writer;
struct bool_reader;


struct bool_coder_namespace : int_types
{
    typedef vp8bc_index_t Index;
    typedef bool_coder_spec Spec;
    typedef const Spec c_spec;

    enum Rounding
    {
        Down = vp8bc_down,
        down_full = vp8bc_down_full,
        Up = vp8bc_up
    };
};


// Archivable specification of a bool coder includes rounding spec
// and probability mapping table.  The latter replaces a uchar j
// (0 <= j < 256) with an arbitrary uint16 tbl[j] = p.
// p/65536 is then the probability of a zero.

struct bool_coder_spec : bool_coder_namespace
{
    friend struct bool_coder;
    friend struct bool_writer;
    friend struct bool_reader;
    friend struct bool_coder_spec_float;
    friend struct bool_coder_spec_explicit_table;
    friend struct bool_coder_spec_exponential_table;
    friend struct BPsrc;
private:
    uint w;                 // precision
    Rounding r;

    uint ebits, mbits, ebias;
    uint32 mmask;

    Index max_index, half_index;

    uint32 mantissa(Index i) const
    {
        assert(i < half_index);
        return (1 << mbits) + (i & mmask);
    }
    uint exponent(Index i) const
    {
        assert(i < half_index);
        return ebias - (i >> mbits);
    }

    uint16 Ptbl[256];       // kinda clunky, but so is storage management.

    /* Cost in bits of encoding a zero at every probability, scaled by 2^20.
       Assumes that index is at most 8 bits wide. */

    uint32 Ctbl[256];

    uint32 split(Index i, uint32 R) const    // 1 <= split <= max( 1, R-1)
    {
        if (!ebias)
            return 1 + (((R - 1) * Ptbl[i]) >> 16);

        if (i >= half_index)
            return R - split(max_index - i, R);

        return 1 + (((R - 1) * mantissa(i)) >> exponent(i));
    }

    uint32 max_range() const
    {
        return (1 << w) - (r == down_full ? 0 : 1);
    }
    uint32 min_range() const
    {
        return (1 << (w - 1)) + (r == down_full ? 1 : 0);
    }
    uint32 Rinc() const
    {
        return r == Up ? 1 : 0;
    }

    void check_prec() const;

    bool float_init(uint Ebits, uint Mbits);

    void cost_init();

    bool_coder_spec(
        uint prec, Rounding rr, uint Ebits = 0, uint Mbits = 0
    )
        : w(prec), r(rr)
    {
        float_init(Ebits, Mbits);
    }
public:
    // Read complete spec from file.
    bool_coder_spec(FILE *);

    // Write spec to file.
    void dump(FILE *) const;

    // return probability index best approximating prob.
    Index operator()(double prob) const;

    // probability corresponding to index
    double operator()(Index i) const;

    Index complement(Index i) const
    {
        return max_index - i;
    }

    Index max_index() const
    {
        return max_index;
    }
    Index half_index() const
    {
        return half_index;
    }

    uint32 cost_zero(Index i) const
    {
        return Ctbl[i];
    }
    uint32 cost_one(Index i) const
    {
        return Ctbl[ max_index - i];
    }
    uint32 cost_bit(Index i, bool b) const
    {
        return Ctbl[b? max_index-i:i];
    }
};


/* Pseudo floating-point probability specification.

   At least one of Ebits and Mbits must be nonzero.

   Since all arithmetic is done at 32 bits, Ebits is at most 5.

   Total significant bits in index is Ebits + Mbits + 1.

   Below the halfway point (i.e. when the top significant bit is 0),
   the index is (e << Mbits) + m.

   The exponent e is between 0 and (2**Ebits) - 1,
   the mantissa m is between 0 and (2**Mbits) - 1.

   Prepending an implicit 1 to the mantissa, the probability is then

        (2**Mbits + m) >> (e - 2**Ebits - 1 - Mbits),

   which has (1/2)**(2**Ebits + 1) as a minimum
   and (1/2) * [1 - 2**(Mbits + 1)] as a maximum.

   When the index is above the halfway point, the probability is the
   complement of the probability associated to the complement of the index.

   Note that the probability increases with the index and that, because of
   the symmetry, we cannot encode probability exactly 1/2; though we
   can get as close to 1/2 as we like, provided we have enough Mbits.

   The latter is of course not a problem in practice, one never has
   exact probabilities and entropy errors are second order, that is, the
   "overcoding" of a zero will be largely compensated for by the
   "undercoding" of a one (or vice-versa).

   Compared to arithmetic probability specs (a la VP8), this will do better
   at very high and low probabilities and worse at probabilities near 1/2,
   as well as facilitating the usage of wider or narrower probability indices.
*/

struct bool_coder_spec_float : bool_coder_spec
{
    bool_coder_spec_float(
        uint Ebits = 3, uint Mbits = 4, Rounding rr = down_full, uint prec = 12
    )
        : bool_coder_spec(prec, rr, Ebits, Mbits)
    {
        cost_init();
    }
};


struct bool_coder_spec_explicit_table : bool_coder_spec
{
    bool_coder_spec_explicit_table(
        cuint16 probability_table[256] = 0,  // default is tbl[i] = i << 8.
        Rounding = down_full,
        uint precision = 16
    );
};

// Contruct table via multiplicative interpolation between
// p[128] = 1/2  and p[0] = (1/2)^x.
// Since we are working with 16-bit precision, x is at most 16.
// For probabilities to increase with i, we must have x > 1.
// For 0 <= i <= 128, p[i] = (1/2)^{ 1 + [1 - (i/128)]*[x-1] }.
// Finally, p[128+i] = 1 - p[128 - i].

struct bool_coder_spec_exponential_table : bool_coder_spec
{
    bool_coder_spec_exponential_table(uint x, Rounding = down_full, uint prec = 16);
};


// Commonalities between writer and reader.

struct bool_coder : bool_coder_namespace
{
    friend struct bool_writer;
    friend struct bool_reader;
    friend struct BPsrc;
private:
    uint32 Low, Range;
    cuint32 min_range;
    cuint32 rinc;
    c_spec spec;

    void _reset()
    {
        Low = 0;
        Range = spec.max_range();
    }

    bool_coder(c_spec &s)
        :  min_range(s.min_range()),
           rinc(s.Rinc()),
           spec(s)
    {
        _reset();
    }

    uint32 half() const
    {
        return 1 + ((Range - 1) >> 1);
    }
public:
    c_spec &Spec() const
    {
        return spec;
    }
};


struct bool_writer : bool_coder
{
    friend struct BPsrc;
private:
    uchar *Bstart, *Bend, *B;
    int bit_lag;
    bool is_toast;
    void carry();
    void reset()
    {
        _reset();
        bit_lag = 32 - spec.w;
        is_toast = 0;
    }
    void raw(bool value, uint32 split);
public:
    bool_writer(c_spec &, uchar *Dest, size_t Len);
    virtual ~bool_writer();

    void operator()(Index p, bool v)
    {
        raw(v, spec.split(p, Range));
    }

    uchar *buf() const
    {
        return Bstart;
    }
    size_t bytes_written() const
    {
        return B - Bstart;
    }

    // Call when done with input, flushes internal state.
    // DO NOT write any more data after calling this.

    bool_writer &flush();

    void write_bits(int n, uint val)
    {
        if (n)
        {
            uint m = 1 << (n - 1);

            do
            {
                raw((bool)(val & m), half());
            }
            while (m >>= 1);
        }
    }

#   if 0
    // We are agnostic about storage management.
    // By default, overflows throw an assert but user can
    // override to provide an expanding buffer using ...

    virtual void overflow(uint Len) const;

    // ... this function copies already-written data into new buffer
    // and retains new buffer location.

    void new_buffer(uchar *dest, uint Len);

    // Note that storage management is the user's responsibility.
#   endif
};


// This could be adjusted to use a little less lookahead.

struct bool_reader : bool_coder
{
    friend struct BPsrc;
private:
    cuchar *const Bstart;   // for debugging
    cuchar *B;
    cuchar *const Bend;
    cuint shf;
    uint bct;
    bool raw(uint32 split);
public:
    bool_reader(c_spec &s, cuchar *src, size_t Len);

    bool operator()(Index p)
    {
        return raw(spec.split(p, Range));
    }

    uint read_bits(int num_bits)
    {
        uint v = 0;

        while (--num_bits >= 0)
            v += v + (raw(half()) ? 1 : 0);

        return v;
    }
};

extern "C" {

#endif /*  __cplusplus */


    /* C interface */

    typedef struct bool_coder_spec bool_coder_spec;
    typedef struct bool_writer bool_writer;
    typedef struct bool_reader bool_reader;

    typedef const bool_coder_spec c_bool_coder_spec;
    typedef const bool_writer c_bool_writer;
    typedef const bool_reader c_bool_reader;


    /* Optionally override default precision when constructing coder_specs.
       Just pass a zero pointer if you don't care.
       Precision is at most 16 bits for table specs, at most 23 otherwise. */

    struct vp8bc_prec
    {
        enum vp8bc_rounding r;      /* see top header file for def */
        unsigned int prec;          /* range precision in bits */
    };

    typedef const struct vp8bc_prec vp8bc_c_prec;

    /* bool_coder_spec contains mapping of uchars to actual probabilities
       (16 bit uints) as well as (usually immaterial) selection of
       exact finite-precision algorithm used (for now, the latter can only
       be overridden using the C++ interface).
       See comments above the corresponding C++ constructors for discussion,
       especially of exponential probability table generation. */

    bool_coder_spec *vp8bc_vp8spec(); // just like vp8

    bool_coder_spec *vp8bc_literal_spec(
        const unsigned short prob_map[256],  // 0 is like vp8 w/more precision
        vp8bc_c_prec*
    );

    bool_coder_spec *vp8bc_float_spec(
        unsigned int exponent_bits, unsigned int mantissa_bits, vp8bc_c_prec*
    );

    bool_coder_spec *vp8bc_exponential_spec(unsigned int min_exp, vp8bc_c_prec *);

    bool_coder_spec *vp8bc_spec_from_file(FILE *);


    void vp8bc_destroy_spec(c_bool_coder_spec *);

    void vp8bc_spec_to_file(c_bool_coder_spec *, FILE *);


    /* Nearest index to supplied probability of zero, 0 <= prob <= 1. */

    vp8bc_index_t vp8bc_index(c_bool_coder_spec *, double prob);

    vp8bc_index_t vp8bc_index_from_counts(
        c_bool_coder_spec *p, unsigned int zero_ct, unsigned int one_ct
    );

    /* In case you want to look */

    double vp8bc_probability(c_bool_coder_spec *, vp8bc_index_t);

    /* Opposite index */

    vp8bc_index_t vp8bc_complement(c_bool_coder_spec *, vp8bc_index_t);

    /* Cost in bits of encoding a zero at given probability, scaled by 2^20.
       (assumes that an int holds at least 32 bits). */

    unsigned int vp8bc_cost_zero(c_bool_coder_spec *, vp8bc_index_t);

    unsigned int vp8bc_cost_one(c_bool_coder_spec *, vp8bc_index_t);
    unsigned int vp8bc_cost_bit(c_bool_coder_spec *, vp8bc_index_t, int);


    /* bool_writer interface */

    /* Length = 0 disables checking for writes beyond buffer end. */

    bool_writer *vp8bc_create_writer(
        c_bool_coder_spec *, unsigned char *Destination, size_t Length
    );

    /* Flushes out any buffered data and returns total # of bytes written. */

    size_t vp8bc_destroy_writer(bool_writer *);

    void vp8bc_write_bool(bool_writer *, int boolean_val, vp8bc_index_t false_prob);

    void vp8bc_write_bits(
        bool_writer *, unsigned int integer_value, int number_of_bits
    );

    c_bool_coder_spec *vp8bc_writer_spec(c_bool_writer *);


    /* bool_reader interface */

    /* Length = 0 disables checking for reads beyond buffer end. */

    bool_reader *vp8bc_create_reader(
        c_bool_coder_spec *, const unsigned char *Source, size_t Length
    );
    void vp8bc_destroy_reader(bool_reader *);

    int vp8bc_read_bool(bool_reader *, vp8bc_index_t false_prob);

    unsigned int vp8bc_read_bits(bool_reader *, int number_of_bits);

    c_bool_coder_spec *vp8bc_reader_spec(c_bool_reader *);

#if __cplusplus
}
#endif

#endif  /* bool_coder_h */