1168
|
1 /*
|
|
2 * exp golomb vlc stuff
|
|
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
|
|
4 *
|
|
5 * This library is free software; you can redistribute it and/or
|
|
6 * modify it under the terms of the GNU Lesser General Public
|
|
7 * License as published by the Free Software Foundation; either
|
|
8 * version 2 of the License, or (at your option) any later version.
|
|
9 *
|
|
10 * This library is distributed in the hope that it will be useful,
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 * Lesser General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU Lesser General Public
|
|
16 * License along with this library; if not, write to the Free Software
|
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 *
|
|
19 */
|
|
20
|
|
21 /**
|
|
22 * @file golomb.h
|
|
23 * @brief
|
|
24 * exp golomb vlc stuff
|
|
25 * @author Michael Niedermayer <michaelni@gmx.at>
|
|
26 */
|
|
27
|
1234
|
28 #define INVALID_VLC 0x80000000
|
|
29
|
1168
|
30 extern const uint8_t ff_golomb_vlc_len[512];
|
|
31 extern const uint8_t ff_ue_golomb_vlc_code[512];
|
|
32 extern const int8_t ff_se_golomb_vlc_code[512];
|
|
33 extern const uint8_t ff_ue_golomb_len[256];
|
|
34
|
|
35
|
|
36 /**
|
|
37 * read unsigned exp golomb code.
|
|
38 */
|
|
39 static inline int get_ue_golomb(GetBitContext *gb){
|
|
40 unsigned int buf;
|
|
41 int log;
|
|
42
|
|
43 OPEN_READER(re, gb);
|
|
44 UPDATE_CACHE(re, gb);
|
|
45 buf=GET_CACHE(re, gb);
|
|
46
|
|
47 if(buf >= (1<<27)){
|
|
48 buf >>= 32 - 9;
|
|
49 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
|
|
50 CLOSE_READER(re, gb);
|
|
51
|
|
52 return ff_ue_golomb_vlc_code[buf];
|
|
53 }else{
|
|
54 log= 2*av_log2(buf) - 31;
|
|
55 buf>>= log;
|
|
56 buf--;
|
|
57 LAST_SKIP_BITS(re, gb, 32 - log);
|
|
58 CLOSE_READER(re, gb);
|
|
59
|
|
60 return buf;
|
|
61 }
|
|
62 }
|
|
63
|
1234
|
64 static inline int svq3_get_ue_golomb(GetBitContext *gb){
|
|
65 unsigned int buf;
|
|
66 int log;
|
|
67
|
|
68 OPEN_READER(re, gb);
|
|
69 UPDATE_CACHE(re, gb);
|
|
70 buf=GET_CACHE(re, gb)|1;
|
|
71
|
|
72 if((buf & 0xAAAAAAAA) == 0)
|
|
73 return INVALID_VLC;
|
|
74
|
|
75 for(log=31; (buf & 0x80000000) == 0; log--){
|
|
76 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
|
|
77 }
|
|
78
|
|
79 LAST_SKIP_BITS(re, gb, 63 - 2*log);
|
|
80 CLOSE_READER(re, gb);
|
|
81
|
|
82 return ((buf << log) >> log) - 1;
|
|
83 }
|
|
84
|
1168
|
85 /**
|
|
86 * read unsigned truncated exp golomb code.
|
|
87 */
|
|
88 static inline int get_te0_golomb(GetBitContext *gb, int range){
|
|
89 assert(range >= 1);
|
|
90
|
|
91 if(range==1) return 0;
|
1169
|
92 else if(range==2) return get_bits1(gb)^1;
|
1168
|
93 else return get_ue_golomb(gb);
|
|
94 }
|
|
95
|
|
96 /**
|
|
97 * read unsigned truncated exp golomb code.
|
|
98 */
|
|
99 static inline int get_te_golomb(GetBitContext *gb, int range){
|
|
100 assert(range >= 1);
|
|
101
|
1169
|
102 if(range==2) return get_bits1(gb)^1;
|
1168
|
103 else return get_ue_golomb(gb);
|
|
104 }
|
|
105
|
|
106
|
|
107 /**
|
|
108 * read signed exp golomb code.
|
|
109 */
|
|
110 static inline int get_se_golomb(GetBitContext *gb){
|
|
111 unsigned int buf;
|
|
112 int log;
|
|
113
|
|
114 OPEN_READER(re, gb);
|
|
115 UPDATE_CACHE(re, gb);
|
|
116 buf=GET_CACHE(re, gb);
|
|
117
|
|
118 if(buf >= (1<<27)){
|
|
119 buf >>= 32 - 9;
|
|
120 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
|
|
121 CLOSE_READER(re, gb);
|
|
122
|
|
123 return ff_se_golomb_vlc_code[buf];
|
|
124 }else{
|
|
125 log= 2*av_log2(buf) - 31;
|
|
126 buf>>= log;
|
|
127
|
|
128 LAST_SKIP_BITS(re, gb, 32 - log);
|
|
129 CLOSE_READER(re, gb);
|
|
130
|
|
131 if(buf&1) buf= -(buf>>1);
|
|
132 else buf= (buf>>1);
|
|
133
|
|
134 return buf;
|
|
135 }
|
|
136 }
|
|
137
|
1234
|
138 static inline int svq3_get_se_golomb(GetBitContext *gb){
|
|
139 unsigned int buf;
|
|
140 int log;
|
|
141
|
|
142 OPEN_READER(re, gb);
|
|
143 UPDATE_CACHE(re, gb);
|
|
144 buf=GET_CACHE(re, gb)|1;
|
|
145
|
|
146 if((buf & 0xAAAAAAAA) == 0)
|
|
147 return INVALID_VLC;
|
|
148
|
|
149 for(log=31; (buf & 0x80000000) == 0; log--){
|
|
150 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
|
|
151 }
|
|
152
|
|
153 LAST_SKIP_BITS(re, gb, 63 - 2*log);
|
|
154 CLOSE_READER(re, gb);
|
|
155
|
|
156 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
|
|
157 }
|
|
158
|
1168
|
159 #ifdef TRACE
|
|
160
|
|
161 static inline int get_ue(GetBitContext *s, char *file, char *func, int line){
|
|
162 int show= show_bits(s, 24);
|
|
163 int pos= get_bits_count(s);
|
|
164 int i= get_ue_golomb(s);
|
|
165 int len= get_bits_count(s) - pos;
|
|
166 int bits= show>>(24-len);
|
|
167
|
|
168 print_bin(bits, len);
|
|
169
|
|
170 printf("%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
|
|
171
|
|
172 return i;
|
|
173 }
|
|
174
|
|
175 static inline int get_se(GetBitContext *s, char *file, char *func, int line){
|
|
176 int show= show_bits(s, 24);
|
|
177 int pos= get_bits_count(s);
|
|
178 int i= get_se_golomb(s);
|
|
179 int len= get_bits_count(s) - pos;
|
|
180 int bits= show>>(24-len);
|
|
181
|
|
182 print_bin(bits, len);
|
|
183
|
|
184 printf("%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
|
|
185
|
|
186 return i;
|
|
187 }
|
|
188
|
|
189 static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
|
|
190 int show= show_bits(s, 24);
|
|
191 int pos= get_bits_count(s);
|
|
192 int i= get_te0_golomb(s, r);
|
|
193 int len= get_bits_count(s) - pos;
|
|
194 int bits= show>>(24-len);
|
|
195
|
|
196 print_bin(bits, len);
|
|
197
|
|
198 printf("%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
|
|
199
|
|
200 return i;
|
|
201 }
|
|
202
|
|
203 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
|
204 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
|
205 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
|
206 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
|
207
|
|
208 #endif
|
|
209
|
|
210 /**
|
|
211 * write unsigned exp golomb code.
|
|
212 */
|
|
213 static inline void set_ue_golomb(PutBitContext *pb, int i){
|
|
214 int e;
|
|
215
|
|
216 assert(i>=0);
|
|
217
|
|
218 #if 0
|
|
219 if(i=0){
|
|
220 put_bits(pb, 1, 1);
|
|
221 return;
|
|
222 }
|
|
223 #endif
|
|
224 if(i<256)
|
|
225 put_bits(pb, ff_ue_golomb_len[i], i+1);
|
|
226 else{
|
|
227 e= av_log2(i+1);
|
|
228
|
|
229 put_bits(pb, 2*e+1, i+1);
|
|
230 }
|
|
231 }
|
|
232
|
|
233 /**
|
|
234 * write truncated unsigned exp golomb code.
|
|
235 */
|
|
236 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
|
|
237 assert(range >= 1);
|
|
238 assert(i<=range);
|
|
239
|
1169
|
240 if(range==2) put_bits(pb, 1, i^1);
|
1168
|
241 else set_ue_golomb(pb, i);
|
|
242 }
|
|
243
|
|
244 /**
|
|
245 * write signed exp golomb code.
|
|
246 */
|
|
247 static inline void set_se_golomb(PutBitContext *pb, int i){
|
|
248 #if 0
|
|
249 if(i<=0) i= -2*i;
|
|
250 else i= 2*i-1;
|
|
251 #elif 1
|
|
252 i= 2*i-1;
|
|
253 if(i<0) i^= -1; //FIXME check if gcc does the right thing
|
|
254 #else
|
|
255 i= 2*i-1;
|
|
256 i^= (i>>31);
|
|
257 #endif
|
|
258 set_ue_golomb(pb, i);
|
|
259 }
|