1 // Copyright 2010 Google Inc.
2 //
3 // This code is licensed under the same terms as WebM:
4 // Software License Agreement: http://www.webmproject.org/license/software/
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6 // -----------------------------------------------------------------------------
7 //
8 // Boolean decoder
9 //
10 // Author: Skal (pascal.massimino@gmail.com)
11
12 #include "bits.h"
13
14 #if defined(__cplusplus) || defined(c_plusplus)
15 extern "C" {
16 #endif
17
18 //-----------------------------------------------------------------------------
19 // VP8BitReader
20
VP8InitBitReader(VP8BitReader * const br,const uint8_t * const start,const uint8_t * const end)21 void VP8InitBitReader(VP8BitReader* const br,
22 const uint8_t* const start, const uint8_t* const end) {
23 assert(br);
24 assert(start);
25 assert(start <= end);
26 br->range_ = 255 - 1;
27 br->buf_ = start;
28 br->buf_end_ = end;
29 br->value_ = 0;
30 br->missing_ = 8;
31 br->eof_ = 0;
32 }
33
34 const uint8_t kVP8Log2Range[128] = {
35 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
36 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
37 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
38 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
39 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
43 0
44 };
45
46 // range = ((range + 1) << kVP8Log2Range[range]) - 1
47 const uint8_t kVP8NewRange[128] = {
48 127, 127, 191, 127, 159, 191, 223, 127, 143, 159, 175, 191, 207, 223, 239,
49 127, 135, 143, 151, 159, 167, 175, 183, 191, 199, 207, 215, 223, 231, 239,
50 247, 127, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167, 171, 175, 179,
51 183, 187, 191, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239,
52 243, 247, 251, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149,
53 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179,
54 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209,
55 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239,
56 241, 243, 245, 247, 249, 251, 253, 127
57 };
58
59 //-----------------------------------------------------------------------------
60 // Higher-level calls
61
VP8GetValue(VP8BitReader * const br,int bits)62 uint32_t VP8GetValue(VP8BitReader* const br, int bits) {
63 uint32_t v = 0;
64 while (bits-- > 0) {
65 v |= VP8GetBit(br, 0x80) << bits;
66 }
67 return v;
68 }
69
VP8GetSignedValue(VP8BitReader * const br,int bits)70 int32_t VP8GetSignedValue(VP8BitReader* const br, int bits) {
71 const int value = VP8GetValue(br, bits);
72 return VP8Get(br) ? -value : value;
73 }
74
75 //-----------------------------------------------------------------------------
76
77 #if defined(__cplusplus) || defined(c_plusplus)
78 } // extern "C"
79 #endif
80