1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libvpx/vp8/decoder/dboolhuff.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,138 @@ 1.4 +/* 1.5 + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license 1.8 + * that can be found in the LICENSE file in the root of the source 1.9 + * tree. An additional intellectual property rights grant can be found 1.10 + * in the file PATENTS. All contributing project authors may 1.11 + * be found in the AUTHORS file in the root of the source tree. 1.12 + */ 1.13 + 1.14 + 1.15 +#ifndef DBOOLHUFF_H_ 1.16 +#define DBOOLHUFF_H_ 1.17 + 1.18 +#include <stddef.h> 1.19 +#include <limits.h> 1.20 + 1.21 +#include "vpx_config.h" 1.22 +#include "vpx_ports/mem.h" 1.23 +#include "vpx/vpx_integer.h" 1.24 + 1.25 +typedef size_t VP8_BD_VALUE; 1.26 + 1.27 +#define VP8_BD_VALUE_SIZE ((int)sizeof(VP8_BD_VALUE)*CHAR_BIT) 1.28 + 1.29 +/*This is meant to be a large, positive constant that can still be efficiently 1.30 + loaded as an immediate (on platforms like ARM, for example). 1.31 + Even relatively modest values like 100 would work fine.*/ 1.32 +#define VP8_LOTS_OF_BITS (0x40000000) 1.33 + 1.34 +/*Decrypt n bytes of data from input -> output, using the decrypt_state 1.35 + passed in VP8D_SET_DECRYPTOR. 1.36 +*/ 1.37 +typedef void (vp8_decrypt_cb)(void *decrypt_state, const unsigned char *input, 1.38 + unsigned char *output, int count); 1.39 + 1.40 +typedef struct 1.41 +{ 1.42 + const unsigned char *user_buffer_end; 1.43 + const unsigned char *user_buffer; 1.44 + VP8_BD_VALUE value; 1.45 + int count; 1.46 + unsigned int range; 1.47 + vp8_decrypt_cb *decrypt_cb; 1.48 + void *decrypt_state; 1.49 +} BOOL_DECODER; 1.50 + 1.51 +DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]); 1.52 + 1.53 +int vp8dx_start_decode(BOOL_DECODER *br, 1.54 + const unsigned char *source, 1.55 + unsigned int source_sz, 1.56 + vp8_decrypt_cb *decrypt_cb, 1.57 + void *decrypt_state); 1.58 + 1.59 +void vp8dx_bool_decoder_fill(BOOL_DECODER *br); 1.60 + 1.61 + 1.62 +static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) { 1.63 + unsigned int bit = 0; 1.64 + VP8_BD_VALUE value; 1.65 + unsigned int split; 1.66 + VP8_BD_VALUE bigsplit; 1.67 + int count; 1.68 + unsigned int range; 1.69 + 1.70 + split = 1 + (((br->range - 1) * probability) >> 8); 1.71 + 1.72 + if(br->count < 0) 1.73 + vp8dx_bool_decoder_fill(br); 1.74 + 1.75 + value = br->value; 1.76 + count = br->count; 1.77 + 1.78 + bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); 1.79 + 1.80 + range = split; 1.81 + 1.82 + if (value >= bigsplit) 1.83 + { 1.84 + range = br->range - split; 1.85 + value = value - bigsplit; 1.86 + bit = 1; 1.87 + } 1.88 + 1.89 + { 1.90 + register unsigned int shift = vp8_norm[range]; 1.91 + range <<= shift; 1.92 + value <<= shift; 1.93 + count -= shift; 1.94 + } 1.95 + br->value = value; 1.96 + br->count = count; 1.97 + br->range = range; 1.98 + 1.99 + return bit; 1.100 +} 1.101 + 1.102 +static int vp8_decode_value(BOOL_DECODER *br, int bits) 1.103 +{ 1.104 + int z = 0; 1.105 + int bit; 1.106 + 1.107 + for (bit = bits - 1; bit >= 0; bit--) 1.108 + { 1.109 + z |= (vp8dx_decode_bool(br, 0x80) << bit); 1.110 + } 1.111 + 1.112 + return z; 1.113 +} 1.114 + 1.115 +static int vp8dx_bool_error(BOOL_DECODER *br) 1.116 +{ 1.117 + /* Check if we have reached the end of the buffer. 1.118 + * 1.119 + * Variable 'count' stores the number of bits in the 'value' buffer, minus 1.120 + * 8. The top byte is part of the algorithm, and the remainder is buffered 1.121 + * to be shifted into it. So if count == 8, the top 16 bits of 'value' are 1.122 + * occupied, 8 for the algorithm and 8 in the buffer. 1.123 + * 1.124 + * When reading a byte from the user's buffer, count is filled with 8 and 1.125 + * one byte is filled into the value buffer. When we reach the end of the 1.126 + * data, count is additionally filled with VP8_LOTS_OF_BITS. So when 1.127 + * count == VP8_LOTS_OF_BITS - 1, the user's data has been exhausted. 1.128 + */ 1.129 + if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS)) 1.130 + { 1.131 + /* We have tried to decode bits after the end of 1.132 + * stream was encountered. 1.133 + */ 1.134 + return 1; 1.135 + } 1.136 + 1.137 + /* No error. */ 1.138 + return 0; 1.139 +} 1.140 + 1.141 +#endif // DBOOLHUFF_H_