summaryrefslogtreecommitdiffstats
path: root/third_party/libwebp/bits.h
blob: e8a24e107eed8d66caa25b4fa0f67f30007ef72d (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
// Copyright 2010 Google Inc.
//
// This code is licensed under the same terms as WebM:
//  Software License Agreement:  http://www.webmproject.org/license/software/
//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
// -----------------------------------------------------------------------------
//
// Boolean decoder
//
// Author: Skal (pascal.massimino@gmail.com)

#ifndef WEBP_DECODE_BITS_H_
#define WEBP_DECODE_BITS_H_

#include <assert.h>
#include "webp/decode_vp8.h"

#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif

//-----------------------------------------------------------------------------
// Bitreader and code-tree reader

typedef struct {
  const uint8_t* buf_;        // next byte to be read
  const uint8_t* buf_end_;    // end of read buffer
  int eof_;                   // true if input is exhausted

  // boolean decoder
  uint32_t range_;            // current range minus 1. In [127, 254] interval.
  uint32_t value_;            // current value
  int left_;                  // how many unused bits (negated)
} VP8BitReader;

// Initialize the bit reader and the boolean decoder. Return true if ok.
int VP8Init(VP8BitReader* const br, const uint8_t* buf, uint32_t size);

// return the next value made of 'num_bits' bits
uint32_t VP8GetValue(VP8BitReader* const br, int num_bits);
static inline uint32_t VP8Get(VP8BitReader* const br) {
  return VP8GetValue(br, 1);
}

// return the next value with sign-extension.
int32_t VP8GetSignedValue(VP8BitReader* const br, int num_bits);

// Read a bit with proba 'prob'. Speed-critical function!
extern const uint8_t kVP8Log2Range[128];
extern const uint8_t kVP8NewRange[128];
static inline uint32_t VP8GetByte(VP8BitReader* const br) {
  assert(br);
  if (br->buf_ < br->buf_end_) {
    assert(br->buf_);
    return *br->buf_++;
  }
  br->eof_ = 1;
  return 0x80;
}

static inline void VP8Shift(VP8BitReader* const br) {
  // range_ is in [0..127] interval here.
  const int shift = kVP8Log2Range[br->range_];
  br->range_ = kVP8NewRange[br->range_];
  br->value_ <<= shift;
  br->left_ += shift;
  if (br->left_ > 0) {
    br->value_ |= VP8GetByte(br) << br->left_;
    br->left_ -= 8;
  }
}

static inline uint32_t VP8GetBit(VP8BitReader* const br, int prob) {
  const uint32_t split = (br->range_ * prob) >> 8;
  const uint32_t bit = ((br->value_ >> 8) > split);
  if (bit) {
    br->range_ -= split + 1;
    br->value_ -= (split + 1) << 8;
  } else {
    br->range_ = split;
  }
  if (br->range_ < 0x7f) {
    VP8Shift(br);
  }
  return bit;
}

static inline int VP8GetSigned(VP8BitReader* const br, int v) {
  const uint32_t split = br->range_ >> 1;
  const uint32_t bit = ((br->value_ >> 8) > split);
  if (bit) {
    br->range_ -= split + 1;
    br->value_ -= (split + 1) << 8;
    v = -v;
  } else {
    br->range_ = split;
  }
  VP8Shift(br);
  return v;
}

#if defined(__cplusplus) || defined(c_plusplus)
}    // extern "C"
#endif

#endif  // WEBP_DECODE_BITS_H_