| OLD | NEW | 
|---|
| 1 /* | 1 /* | 
| 2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved. | 
| 3  * | 3  * | 
| 4  *  Use of this source code is governed by a BSD-style license | 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 | 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 | 6  *  tree. An additional intellectual property rights grant can be found | 
| 7  *  in the file PATENTS.  All contributing project authors may | 7  *  in the file PATENTS.  All contributing project authors may | 
| 8  *  be found in the AUTHORS file in the root of the source tree. | 8  *  be found in the AUTHORS file in the root of the source tree. | 
| 9  */ | 9  */ | 
| 10 | 10 | 
| (...skipping 16 matching lines...) Expand all  Loading... | 
| 27 | 27 | 
| 28 typedef struct | 28 typedef struct | 
| 29 { | 29 { | 
| 30     const unsigned char *user_buffer_end; | 30     const unsigned char *user_buffer_end; | 
| 31     const unsigned char *user_buffer; | 31     const unsigned char *user_buffer; | 
| 32     VP8_BD_VALUE         value; | 32     VP8_BD_VALUE         value; | 
| 33     int                  count; | 33     int                  count; | 
| 34     unsigned int         range; | 34     unsigned int         range; | 
| 35 } BOOL_DECODER; | 35 } BOOL_DECODER; | 
| 36 | 36 | 
| 37 DECLARE_ALIGNED(16, extern const unsigned char, vp8dx_bitreader_norm[256]); | 37 DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]); | 
| 38 | 38 | 
| 39 int vp8dx_start_decode(BOOL_DECODER *br, | 39 int vp8dx_start_decode(BOOL_DECODER *br, | 
| 40                        const unsigned char *source, | 40                        const unsigned char *source, | 
| 41                        unsigned int source_sz); | 41                        unsigned int source_sz); | 
| 42 | 42 | 
| 43 void vp8dx_bool_decoder_fill(BOOL_DECODER *br); | 43 void vp8dx_bool_decoder_fill(BOOL_DECODER *br); | 
| 44 | 44 | 
| 45 /*The refill loop is used in several places, so define it in a macro to make | 45 /*The refill loop is used in several places, so define it in a macro to make | 
| 46    sure they're all consistent. | 46    sure they're all consistent. | 
| 47   An inline function would be cleaner, but has a significant penalty, because | 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 | 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 | 49    enough to eliminate the stores to those fields and the subsequent reloads | 
| 50    from them when inlining the function.*/ | 50    from them when inlining the function.*/ | 
| 51 #define VP8DX_BOOL_DECODER_FILL(_count,_value,_bufptr,_bufend) \ | 51 #define VP8DX_BOOL_DECODER_FILL(_count,_value,_bufptr,_bufend) \ | 
| 52     do \ | 52     do \ | 
| 53     { \ | 53     { \ | 
| 54         int shift; \ | 54         int shift = VP8_BD_VALUE_SIZE - 8 - ((_count) + 8); \ | 
| 55         for(shift = VP8_BD_VALUE_SIZE - 8 - ((_count) + 8); shift >= 0; ) \ | 55         int loop_end, x; \ | 
|  | 56         size_t bits_left = ((_bufend)-(_bufptr))*CHAR_BIT; \ | 
|  | 57         \ | 
|  | 58         x = shift + CHAR_BIT - bits_left; \ | 
|  | 59         loop_end = 0; \ | 
|  | 60         if(x >= 0) \ | 
| 56         { \ | 61         { \ | 
| 57             if((_bufptr) >= (_bufend)) { \ | 62             (_count) += VP8_LOTS_OF_BITS; \ | 
| 58                 (_count) = VP8_LOTS_OF_BITS; \ | 63             loop_end = x; \ | 
| 59                 break; \ | 64             if(!bits_left) break; \ | 
| 60             } \ | 65         } \ | 
| 61             (_count) += 8; \ | 66         while(shift >= loop_end) \ | 
|  | 67         { \ | 
|  | 68             (_count) += CHAR_BIT; \ | 
| 62             (_value) |= (VP8_BD_VALUE)*(_bufptr)++ << shift; \ | 69             (_value) |= (VP8_BD_VALUE)*(_bufptr)++ << shift; \ | 
| 63             shift -= 8; \ | 70             shift -= CHAR_BIT; \ | 
| 64         } \ | 71         } \ | 
| 65     } \ | 72     } \ | 
| 66     while(0) | 73     while(0) \ | 
| 67 | 74 | 
| 68 | 75 | 
| 69 static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) { | 76 static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) { | 
| 70     unsigned int bit = 0; | 77     unsigned int bit = 0; | 
| 71     VP8_BD_VALUE value; | 78     VP8_BD_VALUE value; | 
| 72     unsigned int split; | 79     unsigned int split; | 
| 73     VP8_BD_VALUE bigsplit; | 80     VP8_BD_VALUE bigsplit; | 
| 74     int count; | 81     int count; | 
| 75     unsigned int range; | 82     unsigned int range; | 
| 76 | 83 | 
|  | 84     split = 1 + (((br->range - 1) * probability) >> 8); | 
|  | 85 | 
|  | 86     if(br->count < 0) | 
|  | 87         vp8dx_bool_decoder_fill(br); | 
|  | 88 | 
| 77     value = br->value; | 89     value = br->value; | 
| 78     count = br->count; | 90     count = br->count; | 
| 79     range = br->range; |  | 
| 80 | 91 | 
| 81     split = 1 + (((range - 1) * probability) >> 8); |  | 
| 82     bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); | 92     bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); | 
| 83 | 93 | 
| 84     range = split; | 94     range = split; | 
| 85 | 95 | 
| 86     if (value >= bigsplit) | 96     if (value >= bigsplit) | 
| 87     { | 97     { | 
| 88         range = br->range - split; | 98         range = br->range - split; | 
| 89         value = value - bigsplit; | 99         value = value - bigsplit; | 
| 90         bit = 1; | 100         bit = 1; | 
| 91     } | 101     } | 
| 92 | 102 | 
| 93     { | 103     { | 
| 94         register unsigned int shift = vp8dx_bitreader_norm[range]; | 104         register unsigned int shift = vp8_norm[range]; | 
| 95         range <<= shift; | 105         range <<= shift; | 
| 96         value <<= shift; | 106         value <<= shift; | 
| 97         count -= shift; | 107         count -= shift; | 
| 98     } | 108     } | 
| 99     br->value = value; | 109     br->value = value; | 
| 100     br->count = count; | 110     br->count = count; | 
| 101     br->range = range; | 111     br->range = range; | 
| 102     if(count < 0) | 112 | 
| 103         vp8dx_bool_decoder_fill(br); |  | 
| 104     return bit; | 113     return bit; | 
| 105 } | 114 } | 
| 106 | 115 | 
| 107 static int vp8_decode_value(BOOL_DECODER *br, int bits) | 116 static int vp8_decode_value(BOOL_DECODER *br, int bits) | 
| 108 { | 117 { | 
| 109     int z = 0; | 118     int z = 0; | 
| 110     int bit; | 119     int bit; | 
| 111 | 120 | 
| 112     for (bit = bits - 1; bit >= 0; bit--) | 121     for (bit = bits - 1; bit >= 0; bit--) | 
| 113     { | 122     { | 
| 114         z |= (vp8dx_decode_bool(br, 0x80) << bit); | 123         z |= (vp8dx_decode_bool(br, 0x80) << bit); | 
| 115     } | 124     } | 
| 116 | 125 | 
| 117     return z; | 126     return z; | 
| 118 } | 127 } | 
| 119 | 128 | 
| 120 static int vp8dx_bool_error(BOOL_DECODER *br) | 129 static int vp8dx_bool_error(BOOL_DECODER *br) | 
| 121 { | 130 { | 
| 122   /* Check if we have reached the end of the buffer. | 131     /* Check if we have reached the end of the buffer. | 
| 123    * | 132      * | 
| 124    * Variable 'count' stores the number of bits in the 'value' buffer, | 133      * Variable 'count' stores the number of bits in the 'value' buffer, minus | 
| 125    * minus 8. So if count == 8, there are 16 bits available to be read. | 134      * 8. The top byte is part of the algorithm, and the remainder is buffered | 
| 126    * Normally, count is filled with 8 and one byte is filled into the | 135      * to be shifted into it. So if count == 8, the top 16 bits of 'value' are | 
| 127    * value buffer. When we reach the end of the buffer, count is instead | 136      * occupied, 8 for the algorithm and 8 in the buffer. | 
| 128    * filled with VP8_LOTS_OF_BITS, 8 of which represent the last 8 real | 137      * | 
| 129    * bits from the bitstream. So the last bit in the bitstream will be | 138      * When reading a byte from the user's buffer, count is filled with 8 and | 
| 130    * represented by count == VP8_LOTS_OF_BITS - 16. | 139      * one byte is filled into the value buffer. When we reach the end of the | 
| 131    */ | 140      * data, count is additionally filled with VP8_LOTS_OF_BITS. So when | 
| 132     if ((br->count > VP8_BD_VALUE_SIZE) | 141      * count == VP8_LOTS_OF_BITS - 1, the user's data has been exhausted. | 
| 133         && (br->count <= VP8_LOTS_OF_BITS - 16)) | 142      */ | 
|  | 143     if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS)) | 
| 134     { | 144     { | 
| 135        /* We have tried to decode bits after the end of | 145        /* We have tried to decode bits after the end of | 
| 136         * stream was encountered. | 146         * stream was encountered. | 
| 137         */ | 147         */ | 
| 138         return 1; | 148         return 1; | 
| 139     } | 149     } | 
| 140 | 150 | 
| 141     /* No error. */ | 151     /* No error. */ | 
| 142     return 0; | 152     return 0; | 
| 143 } | 153 } | 
| 144 #endif | 154 #endif | 
| OLD | NEW | 
|---|