vp9_dboolhuff.h 2.06 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 VP9_DECODER_VP9_DBOOLHUFF_H_
#define VP9_DECODER_VP9_DBOOLHUFF_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/vpx_integer.h"
John Koleszar's avatar
John Koleszar committed
20

21
typedef size_t VP9_BD_VALUE;
22

Dmitry Kovalev's avatar
Dmitry Kovalev committed
23
#define VP9_BD_VALUE_SIZE ((int)sizeof(VP9_BD_VALUE)*CHAR_BIT)
24

John Koleszar's avatar
John Koleszar committed
25
typedef struct {
26
27
28
29
30
  const uint8_t *buffer_end;
  const uint8_t *buffer;
  VP9_BD_VALUE value;
  int count;
  unsigned int range;
31
} vp9_reader;
John Koleszar's avatar
John Koleszar committed
32

33
DECLARE_ALIGNED(16, extern const uint8_t, vp9_norm[256]);
John Koleszar's avatar
John Koleszar committed
34

35
int vp9_reader_init(vp9_reader *r, const uint8_t *buffer, size_t size);
Johann's avatar
Johann committed
36

37
void vp9_reader_fill(vp9_reader *r);
38

39
const uint8_t *vp9_reader_find_end(vp9_reader *r);
40

41
static int vp9_read(vp9_reader *br, int probability) {
John Koleszar's avatar
John Koleszar committed
42
  unsigned int bit = 0;
43
44
  VP9_BD_VALUE value;
  VP9_BD_VALUE bigsplit;
John Koleszar's avatar
John Koleszar committed
45
46
  int count;
  unsigned int range;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
47
  unsigned int split = 1 + (((br->range - 1) * probability) >> 8);
John Koleszar's avatar
John Koleszar committed
48
49

  if (br->count < 0)
50
    vp9_reader_fill(br);
John Koleszar's avatar
John Koleszar committed
51
52
53
54

  value = br->value;
  count = br->count;

55
  bigsplit = (VP9_BD_VALUE)split << (VP9_BD_VALUE_SIZE - 8);
John Koleszar's avatar
John Koleszar committed
56
57
58
59
60
61
62
63
64
65

  range = split;

  if (value >= bigsplit) {
    range = br->range - split;
    value = value - bigsplit;
    bit = 1;
  }

  {
66
    register unsigned int shift = vp9_norm[range];
John Koleszar's avatar
John Koleszar committed
67
68
69
70
71
72
73
74
75
    range <<= shift;
    value <<= shift;
    count -= shift;
  }
  br->value = value;
  br->count = count;
  br->range = range;

  return bit;
John Koleszar's avatar
John Koleszar committed
76
77
}

78
static int vp9_read_bit(vp9_reader *r) {
79
80
81
  return vp9_read(r, 128);  // vp9_prob_half
}

82
static int vp9_read_literal(vp9_reader *br, int bits) {
83
  int z = 0, bit;
John Koleszar's avatar
John Koleszar committed
84

85
  for (bit = bits - 1; bit >= 0; bit--)
86
    z |= vp9_read_bit(br) << bit;
John Koleszar's avatar
John Koleszar committed
87

John Koleszar's avatar
John Koleszar committed
88
  return z;
John Koleszar's avatar
John Koleszar committed
89
}
90

91
int vp9_reader_has_error(vp9_reader *r);
92

93
#endif  // VP9_DECODER_VP9_DBOOLHUFF_H_