bitreader.h 3.67 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10
 */

11 12
#ifndef VPX_DSP_BITREADER_H_
#define VPX_DSP_BITREADER_H_
13

14 15
#include <stddef.h>
#include <limits.h>
Dmitry Kovalev's avatar
Dmitry Kovalev committed
16

17
#include "./vpx_config.h"
John Koleszar's avatar
John Koleszar committed
18
#include "vpx_ports/mem.h"
19
#include "vpx/vp8dx.h"
20
#include "vpx/vpx_integer.h"
21
#include "vpx_dsp/prob.h"
22

23 24 25 26
#ifdef __cplusplus
extern "C" {
#endif

27 28 29 30
typedef size_t BD_VALUE;

#define BD_VALUE_SIZE ((int)sizeof(BD_VALUE) * CHAR_BIT)

31 32 33 34 35
// This is meant to be a large, positive constant that can still be efficiently
// loaded as an immediate (on platforms like ARM, for example).
// Even relatively modest values like 100 would work fine.
#define LOTS_OF_BITS 0x40000000

John Koleszar's avatar
John Koleszar committed
36
typedef struct {
37
  // Be careful when reordering this struct, it may impact the cache negatively.
38
  BD_VALUE value;
39
  unsigned int range;
40 41 42
  int count;
  const uint8_t *buffer_end;
  const uint8_t *buffer;
43 44
  vpx_decrypt_cb decrypt_cb;
  void *decrypt_state;
45
  uint8_t clear_buffer[sizeof(BD_VALUE) + 1];
46
} vpx_reader;
John Koleszar's avatar
John Koleszar committed
47

48
int vpx_reader_init(vpx_reader *r,
49 50 51 52
                    const uint8_t *buffer,
                    size_t size,
                    vpx_decrypt_cb decrypt_cb,
                    void *decrypt_state);
Johann's avatar
Johann committed
53

54
void vpx_reader_fill(vpx_reader *r);
55

56
const uint8_t *vpx_reader_find_end(vpx_reader *r);
57

58
static INLINE int vpx_reader_has_error(vpx_reader *r) {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  // Check if we have reached the end of the buffer.
  //
  // Variable 'count' stores the number of bits in the 'value' buffer, minus
  // 8. The top byte is part of the algorithm, and the remainder is buffered
  // to be shifted into it. So if count == 8, the top 16 bits of 'value' are
  // occupied, 8 for the algorithm and 8 in the buffer.
  //
  // When reading a byte from the user's buffer, count is filled with 8 and
  // one byte is filled into the value buffer. When we reach the end of the
  // data, count is additionally filled with LOTS_OF_BITS. So when
  // count == LOTS_OF_BITS - 1, the user's data has been exhausted.
  //
  // 1 if we have tried to decode bits after the end of stream was encountered.
  // 0 No error.
  return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
}

76
static INLINE int vpx_read(vpx_reader *r, int prob) {
John Koleszar's avatar
John Koleszar committed
77
  unsigned int bit = 0;
78 79
  BD_VALUE value;
  BD_VALUE bigsplit;
John Koleszar's avatar
John Koleszar committed
80 81
  int count;
  unsigned int range;
82
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
John Koleszar's avatar
John Koleszar committed
83

84
  if (r->count < 0)
85
    vpx_reader_fill(r);
John Koleszar's avatar
John Koleszar committed
86

87 88
  value = r->value;
  count = r->count;
John Koleszar's avatar
John Koleszar committed
89

90
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
John Koleszar's avatar
John Koleszar committed
91 92 93 94

  range = split;

  if (value >= bigsplit) {
95
    range = r->range - split;
John Koleszar's avatar
John Koleszar committed
96 97 98 99 100
    value = value - bigsplit;
    bit = 1;
  }

  {
101
    register int shift = vpx_norm[range];
John Koleszar's avatar
John Koleszar committed
102 103 104 105
    range <<= shift;
    value <<= shift;
    count -= shift;
  }
106 107 108
  r->value = value;
  r->count = count;
  r->range = range;
John Koleszar's avatar
John Koleszar committed
109 110

  return bit;
John Koleszar's avatar
John Koleszar committed
111 112
}

113 114
static INLINE int vpx_read_bit(vpx_reader *r) {
  return vpx_read(r, 128);  // vpx_prob_half
115 116
}

117
static INLINE int vpx_read_literal(vpx_reader *r, int bits) {
118
  int literal = 0, bit;
John Koleszar's avatar
John Koleszar committed
119

120
  for (bit = bits - 1; bit >= 0; bit--)
121
    literal |= vpx_read_bit(r) << bit;
John Koleszar's avatar
John Koleszar committed
122

123
  return literal;
John Koleszar's avatar
John Koleszar committed
124
}
125

Yaowu Xu's avatar
Yaowu Xu committed
126
static INLINE int vpx_read_tree(vpx_reader *r, const vpx_tree_index *tree,
127
                                const vpx_prob *probs) {
Yaowu Xu's avatar
Yaowu Xu committed
128
  vpx_tree_index i = 0;
129

130
  while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0)
131 132 133 134
    continue;

  return -i;
}
135

136 137 138 139
#ifdef __cplusplus
}  // extern "C"
#endif

140
#endif  // VPX_DSP_BITREADER_H_