21 #ifndef G_EXP_GOLOMB__H
22 #define G_EXP_GOLOMB__H
49 G_EXCEPTION( Error ,
"exp-golomb bitstream underflow" ) ;
50 template <
typename V>
struct value
53 template <
typename U,
typename T>
54 static U
decode( T & begin , T end , U underflow_value ) ;
58 template <
typename U,
typename T>
59 static U
decode( T & begin , T end ,
bool * underflow_p ) ;
62 template <
typename U,
typename T>
63 static U
decode( T & begin , T end ) ;
66 template <
typename S,
typename U>
73 template <
typename U,
typename T>
74 static U decode_imp( T & begin , T end ,
bool * , U ) ;
77 template <
typename U,
typename T>
79 U G::ExpGolomb::decode_imp( T & p , T end ,
bool * underflow_p , U underflow_value )
83 bool counting = true ;
87 if( counting && b == 0 )
90 result = 1 , counting = false ;
92 scale-- , result <<= 1 ;
94 scale-- , result <<= 1 , result++ ;
96 if( !counting && scale == 0 )
99 if( underflow_p ) *underflow_p = true ;
100 return underflow_value ;
103 template <
typename U,
typename T>
107 return decode_imp<U,T>( p , end , nullptr , underflow_value ) ;
110 template <
typename U,
typename T>
114 return decode_imp<U,T>( p , end , underflow_p , 0 ) ;
117 template <
typename U,
typename T>
121 bool underflow = false ;
122 U result = decode_imp<U,T>( p , end , &underflow , 0 ) ;
123 if( underflow )
throw Error() ;
127 template <
typename S,
typename U>
131 return u == 0U ? S(0) : ( (u & 1U) ? S((u+1U)/2U) : -S(u/2U) ) ;
static S make_signed(U)
Makes a signed value from the result of decode() so 0 maps to 0, 1 maps to 1, 2 maps to -1...
Syntactic sugar for extracting ExpGolomb-encoded values.
Does exp-golomb decoding, as used in H.264 "ue(v)" syntax elements.
static U decode(T &begin, T end, U underflow_value)
Decodes the bitstream data provided by the bit iterator.