• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 VPX_VPX_DSP_BITREADER_H_
12 #define VPX_VPX_DSP_BITREADER_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/vp8dx.h"
20 #include "vpx/vpx_integer.h"
21 #include "vpx_dsp/prob.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 typedef size_t BD_VALUE;
28 
29 #define BD_VALUE_SIZE ((int)sizeof(BD_VALUE) * CHAR_BIT)
30 
31 // This is meant to be a large, positive constant that can still be efficiently
32 // loaded as an immediate (on platforms like ARM, for example).
33 // Even relatively modest values like 100 would work fine.
34 #define LOTS_OF_BITS 0x40000000
35 
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   int count;
41   const uint8_t *buffer_end;
42   const uint8_t *buffer;
43   vpx_decrypt_cb decrypt_cb;
44   void *decrypt_state;
45   uint8_t clear_buffer[sizeof(BD_VALUE) + 1];
46 } vpx_reader;
47 
48 int vpx_reader_init(vpx_reader *r, const uint8_t *buffer, size_t size,
49                     vpx_decrypt_cb decrypt_cb, void *decrypt_state);
50 
51 void vpx_reader_fill(vpx_reader *r);
52 
53 const uint8_t *vpx_reader_find_end(vpx_reader *r);
54 
vpx_reader_has_error(vpx_reader * r)55 static INLINE int vpx_reader_has_error(vpx_reader *r) {
56   // Check if we have reached the end of the buffer.
57   //
58   // Variable 'count' stores the number of bits in the 'value' buffer, minus
59   // 8. The top byte is part of the algorithm, and the remainder is buffered
60   // to be shifted into it. So if count == 8, the top 16 bits of 'value' are
61   // occupied, 8 for the algorithm and 8 in the buffer.
62   //
63   // When reading a byte from the user's buffer, count is filled with 8 and
64   // one byte is filled into the value buffer. When we reach the end of the
65   // data, count is additionally filled with LOTS_OF_BITS. So when
66   // count == LOTS_OF_BITS - 1, the user's data has been exhausted.
67   //
68   // 1 if we have tried to decode bits after the end of stream was encountered.
69   // 0 No error.
70   return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
71 }
72 
vpx_read(vpx_reader * r,int prob)73 static INLINE int vpx_read(vpx_reader *r, int prob) {
74   unsigned int bit = 0;
75   BD_VALUE value;
76   BD_VALUE bigsplit;
77   int count;
78   unsigned int range;
79   unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
80 
81   if (r->count < 0) vpx_reader_fill(r);
82 
83   value = r->value;
84   count = r->count;
85 
86   bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
87 
88   range = split;
89 
90   if (value >= bigsplit) {
91     range = r->range - split;
92     value = value - bigsplit;
93     bit = 1;
94   }
95 
96   {
97     const unsigned char shift = vpx_norm[(unsigned char)range];
98     range <<= shift;
99     value <<= shift;
100     count -= shift;
101   }
102   r->value = value;
103   r->count = count;
104   r->range = range;
105 
106   return bit;
107 }
108 
vpx_read_bit(vpx_reader * r)109 static INLINE int vpx_read_bit(vpx_reader *r) {
110   return vpx_read(r, 128);  // vpx_prob_half
111 }
112 
vpx_read_literal(vpx_reader * r,int bits)113 static INLINE int vpx_read_literal(vpx_reader *r, int bits) {
114   int literal = 0, bit;
115 
116   for (bit = bits - 1; bit >= 0; bit--) literal |= vpx_read_bit(r) << bit;
117 
118   return literal;
119 }
120 
vpx_read_tree(vpx_reader * r,const vpx_tree_index * tree,const vpx_prob * probs)121 static INLINE int vpx_read_tree(vpx_reader *r, const vpx_tree_index *tree,
122                                 const vpx_prob *probs) {
123   vpx_tree_index i = 0;
124 
125   while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0) continue;
126 
127   return -i;
128 }
129 
130 #ifdef __cplusplus
131 }  // extern "C"
132 #endif
133 
134 #endif  // VPX_VPX_DSP_BITREADER_H_
135