1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * 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.
9 */
10
11 #ifndef VP9_DECODER_VP9_DBOOLHUFF_H_
12 #define VP9_DECODER_VP9_DBOOLHUFF_H_
13
14 #include <stddef.h>
15 #include <limits.h>
16
17 #include "./vpx_config.h"
18 #include "vpx_ports/mem.h"
19 #include "vpx/vpx_integer.h"
20
21 typedef size_t VP9_BD_VALUE;
22
23 #define BD_VALUE_SIZE ((int)sizeof(VP9_BD_VALUE)*CHAR_BIT)
24
25 typedef struct {
26 const uint8_t *buffer_end;
27 const uint8_t *buffer;
28 VP9_BD_VALUE value;
29 int count;
30 unsigned int range;
31 } vp9_reader;
32
33 DECLARE_ALIGNED(16, extern const uint8_t, vp9_norm[256]);
34
35 int vp9_reader_init(vp9_reader *r, const uint8_t *buffer, size_t size);
36
37 void vp9_reader_fill(vp9_reader *r);
38
39 const uint8_t *vp9_reader_find_end(vp9_reader *r);
40
vp9_read(vp9_reader * br,int probability)41 static int vp9_read(vp9_reader *br, int probability) {
42 unsigned int bit = 0;
43 VP9_BD_VALUE value;
44 VP9_BD_VALUE bigsplit;
45 int count;
46 unsigned int range;
47 unsigned int split = ((br->range * probability) + (256 - probability)) >> 8;
48
49 if (br->count < 0)
50 vp9_reader_fill(br);
51
52 value = br->value;
53 count = br->count;
54
55 bigsplit = (VP9_BD_VALUE)split << (BD_VALUE_SIZE - 8);
56
57 range = split;
58
59 if (value >= bigsplit) {
60 range = br->range - split;
61 value = value - bigsplit;
62 bit = 1;
63 }
64
65 {
66 register unsigned int shift = vp9_norm[range];
67 range <<= shift;
68 value <<= shift;
69 count -= shift;
70 }
71 br->value = value;
72 br->count = count;
73 br->range = range;
74
75 return bit;
76 }
77
vp9_read_bit(vp9_reader * r)78 static int vp9_read_bit(vp9_reader *r) {
79 return vp9_read(r, 128); // vp9_prob_half
80 }
81
vp9_read_literal(vp9_reader * br,int bits)82 static int vp9_read_literal(vp9_reader *br, int bits) {
83 int z = 0, bit;
84
85 for (bit = bits - 1; bit >= 0; bit--)
86 z |= vp9_read_bit(br) << bit;
87
88 return z;
89 }
90
91 int vp9_reader_has_error(vp9_reader *r);
92
93 #endif // VP9_DECODER_VP9_DBOOLHUFF_H_
94