rtpvp8: port some more to new memory API
[platform/upstream/gstreamer.git] / gst / rtp / 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 dboolhuff.LICENSE file in this directory.
6  *  See the libvpx original distribution for more information,
7  *  including patent information, and author information.
8  */
9
10
11 #ifndef DBOOLHUFF_H
12 #define DBOOLHUFF_H
13 #include <stddef.h>
14 #include <limits.h>
15 #include <glib.h>
16
17 typedef size_t VP8_BD_VALUE;
18
19 # define VP8_BD_VALUE_SIZE ((int)sizeof(VP8_BD_VALUE)*CHAR_BIT)
20 /*This is meant to be a large, positive constant that can still be efficiently
21    loaded as an immediate (on platforms like ARM, for example).
22   Even relatively modest values like 100 would work fine.*/
23 # define VP8_LOTS_OF_BITS (0x40000000)
24
25 typedef struct
26 {
27     const unsigned char *user_buffer_end;
28     const unsigned char *user_buffer;
29     VP8_BD_VALUE         value;
30     int                  count;
31     unsigned int         range;
32 } BOOL_DECODER;
33
34 extern const unsigned char vp8_norm[256] __attribute__((aligned(16)));
35
36 int vp8dx_start_decode(BOOL_DECODER *br,
37                        const unsigned char *source,
38                        unsigned int source_sz);
39
40 void vp8dx_bool_decoder_fill(BOOL_DECODER *br);
41
42 /*The refill loop is used in several places, so define it in a macro to make
43    sure they're all consistent.
44   An inline function would be cleaner, but has a significant penalty, because
45    multiple BOOL_DECODER fields must be modified, and the compiler is not smart
46    enough to eliminate the stores to those fields and the subsequent reloads
47    from them when inlining the function.*/
48 #define VP8DX_BOOL_DECODER_FILL(_count,_value,_bufptr,_bufend) \
49     do \
50     { \
51         int shift = VP8_BD_VALUE_SIZE - 8 - ((_count) + 8); \
52         int loop_end, x; \
53         size_t bits_left = ((_bufend)-(_bufptr))*CHAR_BIT; \
54         \
55         x = shift + CHAR_BIT - bits_left; \
56         loop_end = 0; \
57         if(x >= 0) \
58         { \
59             (_count) += VP8_LOTS_OF_BITS; \
60             loop_end = x; \
61             if(!bits_left) break; \
62         } \
63         while(shift >= loop_end) \
64         { \
65             (_count) += CHAR_BIT; \
66             (_value) |= (VP8_BD_VALUE)*(_bufptr)++ << shift; \
67             shift -= CHAR_BIT; \
68         } \
69     } \
70     while(0) \
71
72
73 static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) {
74     unsigned int bit = 0;
75     VP8_BD_VALUE value;
76     unsigned int split;
77     VP8_BD_VALUE bigsplit;
78     int count;
79     unsigned int range;
80
81     split = 1 + (((br->range - 1) * probability) >> 8);
82
83     if(br->count < 0)
84         vp8dx_bool_decoder_fill(br);
85
86     value = br->value;
87     count = br->count;
88
89     bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
90
91     range = split;
92
93     if (value >= bigsplit)
94     {
95         range = br->range - split;
96         value = value - bigsplit;
97         bit = 1;
98     }
99
100     {
101         register unsigned int shift = vp8_norm[range];
102         range <<= shift;
103         value <<= shift;
104         count -= shift;
105     }
106     br->value = value;
107     br->count = count;
108     br->range = range;
109
110     return bit;
111 }
112
113 static G_GNUC_UNUSED int vp8_decode_value(BOOL_DECODER *br, int bits)
114 {
115     int z = 0;
116     int bit;
117
118     for (bit = bits - 1; bit >= 0; bit--)
119     {
120         z |= (vp8dx_decode_bool(br, 0x80) << bit);
121     }
122
123     return z;
124 }
125
126 static G_GNUC_UNUSED int vp8dx_bool_error(BOOL_DECODER *br)
127 {
128     /* Check if we have reached the end of the buffer.
129      *
130      * Variable 'count' stores the number of bits in the 'value' buffer, minus
131      * 8. The top byte is part of the algorithm, and the remainder is buffered
132      * to be shifted into it. So if count == 8, the top 16 bits of 'value' are
133      * occupied, 8 for the algorithm and 8 in the buffer.
134      *
135      * When reading a byte from the user's buffer, count is filled with 8 and
136      * one byte is filled into the value buffer. When we reach the end of the
137      * data, count is additionally filled with VP8_LOTS_OF_BITS. So when
138      * count == VP8_LOTS_OF_BITS - 1, the user's data has been exhausted.
139      */
140     if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS))
141     {
142        /* We have tried to decode bits after the end of
143         * stream was encountered.
144         */
145         return 1;
146     }
147
148     /* No error. */
149     return 0;
150 }
151 #endif