bitreader.h 3.72 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar's avatar
John Koleszar committed
10 11
 */

Adrian Grange's avatar
Adrian Grange committed
12 13
#ifndef AOM_DSP_BITREADER_H_
#define AOM_DSP_BITREADER_H_
14

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

Adrian Grange's avatar
Adrian Grange committed
18
#include "./aom_config.h"
Yaowu Xu's avatar
Yaowu Xu committed
19
#include "aom_ports/mem.h"
Adrian Grange's avatar
Adrian Grange committed
20
#include "aom/aomdx.h"
Adrian Grange's avatar
Adrian Grange committed
21
#include "aom/aom_integer.h"
Yaowu Xu's avatar
Yaowu Xu committed
22
#include "aom_dsp/prob.h"
23

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

28 29 30 31
typedef size_t BD_VALUE;

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

James Zern's avatar
James Zern committed
32 33 34 35 36
// 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
37
typedef struct {
38
  // Be careful when reordering this struct, it may impact the cache negatively.
39
  BD_VALUE value;
40
  unsigned int range;
41 42 43
  int count;
  const uint8_t *buffer_end;
  const uint8_t *buffer;
Adrian Grange's avatar
Adrian Grange committed
44
  aom_decrypt_cb decrypt_cb;
45
  void *decrypt_state;
46
  uint8_t clear_buffer[sizeof(BD_VALUE) + 1];
Adrian Grange's avatar
Adrian Grange committed
47
} aom_reader;
John Koleszar's avatar
John Koleszar committed
48

Adrian Grange's avatar
Adrian Grange committed
49 50
int aom_reader_init(aom_reader *r, const uint8_t *buffer, size_t size,
                    aom_decrypt_cb decrypt_cb, void *decrypt_state);
Johann's avatar
Johann committed
51

Adrian Grange's avatar
Adrian Grange committed
52
void aom_reader_fill(aom_reader *r);
53

Adrian Grange's avatar
Adrian Grange committed
54
const uint8_t *aom_reader_find_end(aom_reader *r);
55

Adrian Grange's avatar
Adrian Grange committed
56
static INLINE int aom_reader_has_error(aom_reader *r) {
James Zern's avatar
James Zern committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  // 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;
}

Adrian Grange's avatar
Adrian Grange committed
74
static INLINE int aom_read(aom_reader *r, int prob) {
John Koleszar's avatar
John Koleszar committed
75
  unsigned int bit = 0;
76 77
  BD_VALUE value;
  BD_VALUE bigsplit;
John Koleszar's avatar
John Koleszar committed
78 79
  int count;
  unsigned int range;
80
  unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
John Koleszar's avatar
John Koleszar committed
81

Adrian Grange's avatar
Adrian Grange committed
82
  if (r->count < 0) aom_reader_fill(r);
John Koleszar's avatar
John Koleszar committed
83

84 85
  value = r->value;
  count = r->count;
John Koleszar's avatar
John Koleszar committed
86

87
  bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
John Koleszar's avatar
John Koleszar committed
88 89 90 91

  range = split;

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

  {
Adrian Grange's avatar
Adrian Grange committed
98
    register int shift = aom_norm[range];
John Koleszar's avatar
John Koleszar committed
99 100 101 102
    range <<= shift;
    value <<= shift;
    count -= shift;
  }
103 104 105
  r->value = value;
  r->count = count;
  r->range = range;
John Koleszar's avatar
John Koleszar committed
106 107

  return bit;
John Koleszar's avatar
John Koleszar committed
108 109
}

Adrian Grange's avatar
Adrian Grange committed
110 111
static INLINE int aom_read_bit(aom_reader *r) {
  return aom_read(r, 128);  // aom_prob_half
112 113
}

Adrian Grange's avatar
Adrian Grange committed
114
static INLINE int aom_read_literal(aom_reader *r, int bits) {
115
  int literal = 0, bit;
John Koleszar's avatar
John Koleszar committed
116

Adrian Grange's avatar
Adrian Grange committed
117
  for (bit = bits - 1; bit >= 0; bit--) literal |= aom_read_bit(r) << bit;
John Koleszar's avatar
John Koleszar committed
118

119
  return literal;
John Koleszar's avatar
John Koleszar committed
120
}
121

Adrian Grange's avatar
Adrian Grange committed
122 123 124
static INLINE int aom_read_tree(aom_reader *r, const aom_tree_index *tree,
                                const aom_prob *probs) {
  aom_tree_index i = 0;
125

Adrian Grange's avatar
Adrian Grange committed
126
  while ((i = tree[i + aom_read(r, probs[i >> 1])]) > 0) continue;
127 128 129

  return -i;
}
130

131 132 133 134
#ifdef __cplusplus
}  // extern "C"
#endif

Adrian Grange's avatar
Adrian Grange committed
135
#endif  // AOM_DSP_BITREADER_H_