Merge "Copy macroblock data to a buffer before encoding it"
[profile/ivi/libvpx.git] / vp8 / decoder / dboolhuff.h
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 = VP8_BD_VALUE_SIZE - 8 - ((_count) + 8); \
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) \
61         { \
62             (_count) += VP8_LOTS_OF_BITS; \
63             loop_end = x; \
64             if(!bits_left) break; \
65         } \
66         while(shift >= loop_end) \
67         { \
68             (_count) += CHAR_BIT; \
69             (_value) |= (VP8_BD_VALUE)*(_bufptr)++ << shift; \
70             shift -= CHAR_BIT; \
71         } \
72     } \
73     while(0) \
74
75
76 static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) {
77     unsigned int bit = 0;
78     VP8_BD_VALUE value;
79     unsigned int split;
80     VP8_BD_VALUE bigsplit;
81     int count;
82     unsigned int range;
83
84     split = 1 + (((br->range - 1) * probability) >> 8);
85
86     if(br->count < 0)
87         vp8dx_bool_decoder_fill(br);
88
89     value = br->value;
90     count = br->count;
91
92     bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
93
94     range = split;
95
96     if (value >= bigsplit)
97     {
98         range = br->range - split;
99         value = value - bigsplit;
100         bit = 1;
101     }
102
103     {
104         register unsigned int shift = vp8dx_bitreader_norm[range];
105         range <<= shift;
106         value <<= shift;
107         count -= shift;
108     }
109     br->value = value;
110     br->count = count;
111     br->range = range;
112
113     return bit;
114 }
115
116 static int vp8_decode_value(BOOL_DECODER *br, int bits)
117 {
118     int z = 0;
119     int bit;
120
121     for (bit = bits - 1; bit >= 0; bit--)
122     {
123         z |= (vp8dx_decode_bool(br, 0x80) << bit);
124     }
125
126     return z;
127 }
128
129 static int vp8dx_bool_error(BOOL_DECODER *br)
130 {
131     /* Check if we have reached the end of the buffer.
132      *
133      * Variable 'count' stores the number of bits in the 'value' buffer, minus
134      * 8. The top byte is part of the algorithm, and the remainder is buffered
135      * to be shifted into it. So if count == 8, the top 16 bits of 'value' are
136      * occupied, 8 for the algorithm and 8 in the buffer.
137      *
138      * When reading a byte from the user's buffer, count is filled with 8 and
139      * one byte is filled into the value buffer. When we reach the end of the
140      * data, count is additionally filled with VP8_LOTS_OF_BITS. So when
141      * count == VP8_LOTS_OF_BITS - 1, the user's data has been exhausted.
142      */
143     if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS))
144     {
145        /* We have tried to decode bits after the end of
146         * stream was encountered.
147         */
148         return 1;
149     }
150
151     /* No error. */
152     return 0;
153 }
154 #endif