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 12 #ifndef DBOOLHUFF_H 13 #define DBOOLHUFF_H 14 #include <stddef.h> 15 #include <limits.h> 16 #include "vpx_ports/config.h" 17 #include "vpx_ports/mem.h" 18 #include "vpx/vpx_integer.h" 19 20 typedef size_t VP8_BD_VALUE; 21 22 # define VP8_BD_VALUE_SIZE ((int)sizeof(VP8_BD_VALUE)*CHAR_BIT) 23 /*This is meant to be a large, positive constant that can still be efficiently 24 loaded as an immediate (on platforms like ARM, for example). 25 Even relatively modest values like 100 would work fine.*/ 26 # define VP8_LOTS_OF_BITS (0x40000000) 27 28 typedef struct 29 { 30 const unsigned char *user_buffer_end; 31 const unsigned char *user_buffer; 32 VP8_BD_VALUE value; 33 int count; 34 unsigned int range; 35 } BOOL_DECODER; 36 37 DECLARE_ALIGNED(16, extern const unsigned char, vp8dx_bitreader_norm[256]); 38 39 int vp8dx_start_decode(BOOL_DECODER *br, 40 const unsigned char *source, 41 unsigned int source_sz); 42 43 void vp8dx_bool_decoder_fill(BOOL_DECODER *br); 44 45 /*The refill loop is used in several places, so define it in a macro to make 46 sure they're all consistent. 47 An inline function would be cleaner, but has a significant penalty, because 48 multiple BOOL_DECODER fields must be modified, and the compiler is not smart 49 enough to eliminate the stores to those fields and the subsequent reloads 50 from them when inlining the function.*/ 51 #define VP8DX_BOOL_DECODER_FILL(_count,_value,_bufptr,_bufend) \ 52 do \ 53 { \ 54 int shift; \ 55 for(shift = VP8_BD_VALUE_SIZE - 8 - ((_count) + 8); shift >= 0; ) \ 56 { \ 57 if((_bufptr) >= (_bufend)) { \ 58 (_count) = VP8_LOTS_OF_BITS; \ 59 break; \ 60 } \ 61 (_count) += 8; \ 62 (_value) |= (VP8_BD_VALUE)*(_bufptr)++ << shift; \ 63 shift -= 8; \ 64 } \ 65 } \ 66 while(0) 67 68 69 static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) { 70 unsigned int bit = 0; 71 VP8_BD_VALUE value; 72 unsigned int split; 73 VP8_BD_VALUE bigsplit; 74 int count; 75 unsigned int range; 76 77 value = br->value; 78 count = br->count; 79 range = br->range; 80 81 split = 1 + (((range - 1) * probability) >> 8); 82 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); 83 84 range = split; 85 86 if (value >= bigsplit) 87 { 88 range = br->range - split; 89 value = value - bigsplit; 90 bit = 1; 91 } 92 93 { 94 register unsigned int shift = vp8dx_bitreader_norm[range]; 95 range <<= shift; 96 value <<= shift; 97 count -= shift; 98 } 99 br->value = value; 100 br->count = count; 101 br->range = range; 102 if(count < 0) 103 vp8dx_bool_decoder_fill(br); 104 return bit; 105 } 106 107 static int vp8_decode_value(BOOL_DECODER *br, int bits) 108 { 109 int z = 0; 110 int bit; 111 112 for (bit = bits - 1; bit >= 0; bit--) 113 { 114 z |= (vp8dx_decode_bool(br, 0x80) << bit); 115 } 116 117 return z; 118 } 119 120 static int vp8dx_bool_error(BOOL_DECODER *br) 121 { 122 /* Check if we have reached the end of the buffer. 123 * 124 * Variable 'count' stores the number of bits in the 'value' buffer, 125 * minus 8. So if count == 8, there are 16 bits available to be read. 126 * Normally, count is filled with 8 and one byte is filled into the 127 * value buffer. When we reach the end of the buffer, count is instead 128 * filled with VP8_LOTS_OF_BITS, 8 of which represent the last 8 real 129 * bits from the bitstream. So the last bit in the bitstream will be 130 * represented by count == VP8_LOTS_OF_BITS - 16. 131 */ 132 if ((br->count > VP8_BD_VALUE_SIZE) 133 && (br->count <= VP8_LOTS_OF_BITS - 16)) 134 { 135 /* We have tried to decode bits after the end of 136 * stream was encountered. 137 */ 138 return 1; 139 } 140 141 /* No error. */ 142 return 0; 143 } 144 #endif 145