808
|
1 /*
|
|
2 * exp golomb vlc stuff
|
|
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
|
|
4 * Copyright (c) 2004 Alex Beregszaszi
|
|
5 *
|
|
6 * This file is part of FFmpeg.
|
|
7 *
|
|
8 * FFmpeg is free software; you can redistribute it and/or
|
|
9 * modify it under the terms of the GNU Lesser General Public
|
|
10 * License as published by the Free Software Foundation; either
|
|
11 * version 2.1 of the License, or (at your option) any later version.
|
|
12 *
|
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 * Lesser General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU Lesser General Public
|
|
19 * License along with FFmpeg; if not, write to the Free Software
|
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
21 *
|
|
22 */
|
|
23
|
|
24 /**
|
|
25 * @file golomb.h
|
|
26 * @brief
|
|
27 * exp golomb vlc stuff
|
|
28 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
|
|
29 */
|
|
30
|
|
31 #define INVALID_VLC 0x80000000
|
|
32
|
|
33 extern const uint8_t ff_golomb_vlc_len[512];
|
|
34 extern const uint8_t ff_ue_golomb_vlc_code[512];
|
|
35 extern const int8_t ff_se_golomb_vlc_code[512];
|
|
36 extern const uint8_t ff_ue_golomb_len[256];
|
|
37
|
|
38 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
|
|
39 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
|
|
40 extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
|
|
41
|
|
42
|
|
43 /**
|
|
44 * read unsigned exp golomb code.
|
|
45 */
|
|
46 static inline int get_ue_golomb(GetBitContext *gb){
|
|
47 unsigned int buf;
|
|
48 int log;
|
|
49
|
|
50 OPEN_READER(re, gb);
|
|
51 UPDATE_CACHE(re, gb);
|
|
52 buf=GET_CACHE(re, gb);
|
|
53
|
|
54 if(buf >= (1<<27)){
|
|
55 buf >>= 32 - 9;
|
|
56 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
|
|
57 CLOSE_READER(re, gb);
|
|
58
|
|
59 return ff_ue_golomb_vlc_code[buf];
|
|
60 }else{
|
|
61 log= 2*av_log2(buf) - 31;
|
|
62 buf>>= log;
|
|
63 buf--;
|
|
64 LAST_SKIP_BITS(re, gb, 32 - log);
|
|
65 CLOSE_READER(re, gb);
|
|
66
|
|
67 return buf;
|
|
68 }
|
|
69 }
|
|
70
|
|
71 static inline int svq3_get_ue_golomb(GetBitContext *gb){
|
|
72 uint32_t buf;
|
|
73 int log;
|
|
74
|
|
75 OPEN_READER(re, gb);
|
|
76 UPDATE_CACHE(re, gb);
|
|
77 buf=GET_CACHE(re, gb);
|
|
78
|
|
79 if(buf&0xAA800000){
|
|
80 buf >>= 32 - 8;
|
|
81 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
|
|
82 CLOSE_READER(re, gb);
|
|
83
|
|
84 return ff_interleaved_ue_golomb_vlc_code[buf];
|
|
85 }else{
|
|
86 LAST_SKIP_BITS(re, gb, 8);
|
|
87 UPDATE_CACHE(re, gb);
|
|
88 buf |= 1 | (GET_CACHE(re, gb) >> 8);
|
|
89
|
|
90 if((buf & 0xAAAAAAAA) == 0)
|
|
91 return INVALID_VLC;
|
|
92
|
|
93 for(log=31; (buf & 0x80000000) == 0; log--){
|
|
94 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
|
|
95 }
|
|
96
|
|
97 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
|
|
98 CLOSE_READER(re, gb);
|
|
99
|
|
100 return ((buf << log) >> log) - 1;
|
|
101 }
|
|
102 }
|
|
103
|
|
104 /**
|
|
105 * read unsigned truncated exp golomb code.
|
|
106 */
|
|
107 static inline int get_te0_golomb(GetBitContext *gb, int range){
|
|
108 assert(range >= 1);
|
|
109
|
|
110 if(range==1) return 0;
|
|
111 else if(range==2) return get_bits1(gb)^1;
|
|
112 else return get_ue_golomb(gb);
|
|
113 }
|
|
114
|
|
115 /**
|
|
116 * read unsigned truncated exp golomb code.
|
|
117 */
|
|
118 static inline int get_te_golomb(GetBitContext *gb, int range){
|
|
119 assert(range >= 1);
|
|
120
|
|
121 if(range==2) return get_bits1(gb)^1;
|
|
122 else return get_ue_golomb(gb);
|
|
123 }
|
|
124
|
|
125
|
|
126 /**
|
|
127 * read signed exp golomb code.
|
|
128 */
|
|
129 static inline int get_se_golomb(GetBitContext *gb){
|
|
130 unsigned int buf;
|
|
131 int log;
|
|
132
|
|
133 OPEN_READER(re, gb);
|
|
134 UPDATE_CACHE(re, gb);
|
|
135 buf=GET_CACHE(re, gb);
|
|
136
|
|
137 if(buf >= (1<<27)){
|
|
138 buf >>= 32 - 9;
|
|
139 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
|
|
140 CLOSE_READER(re, gb);
|
|
141
|
|
142 return ff_se_golomb_vlc_code[buf];
|
|
143 }else{
|
|
144 log= 2*av_log2(buf) - 31;
|
|
145 buf>>= log;
|
|
146
|
|
147 LAST_SKIP_BITS(re, gb, 32 - log);
|
|
148 CLOSE_READER(re, gb);
|
|
149
|
|
150 if(buf&1) buf= -(buf>>1);
|
|
151 else buf= (buf>>1);
|
|
152
|
|
153 return buf;
|
|
154 }
|
|
155 }
|
|
156
|
|
157 static inline int svq3_get_se_golomb(GetBitContext *gb){
|
|
158 unsigned int buf;
|
|
159 int log;
|
|
160
|
|
161 OPEN_READER(re, gb);
|
|
162 UPDATE_CACHE(re, gb);
|
|
163 buf=GET_CACHE(re, gb);
|
|
164
|
|
165 if(buf&0xAA800000){
|
|
166 buf >>= 32 - 8;
|
|
167 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
|
|
168 CLOSE_READER(re, gb);
|
|
169
|
|
170 return ff_interleaved_se_golomb_vlc_code[buf];
|
|
171 }else{
|
|
172 LAST_SKIP_BITS(re, gb, 8);
|
|
173 UPDATE_CACHE(re, gb);
|
|
174 buf |= 1 | (GET_CACHE(re, gb) >> 8);
|
|
175
|
|
176 if((buf & 0xAAAAAAAA) == 0)
|
|
177 return INVALID_VLC;
|
|
178
|
|
179 for(log=31; (buf & 0x80000000) == 0; log--){
|
|
180 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
|
|
181 }
|
|
182
|
|
183 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
|
|
184 CLOSE_READER(re, gb);
|
|
185
|
|
186 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
|
|
187 }
|
|
188 }
|
|
189
|
|
190 /**
|
|
191 * read unsigned golomb rice code (ffv1).
|
|
192 */
|
|
193 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
|
|
194 unsigned int buf;
|
|
195 int log;
|
|
196
|
|
197 OPEN_READER(re, gb);
|
|
198 UPDATE_CACHE(re, gb);
|
|
199 buf=GET_CACHE(re, gb);
|
|
200
|
|
201 log= av_log2(buf);
|
|
202
|
|
203 if(log > 31-limit){
|
|
204 buf >>= log - k;
|
|
205 buf += (30-log)<<k;
|
|
206 LAST_SKIP_BITS(re, gb, 32 + k - log);
|
|
207 CLOSE_READER(re, gb);
|
|
208
|
|
209 return buf;
|
|
210 }else{
|
|
211 buf >>= 32 - limit - esc_len;
|
|
212 LAST_SKIP_BITS(re, gb, esc_len + limit);
|
|
213 CLOSE_READER(re, gb);
|
|
214
|
|
215 return buf + limit - 1;
|
|
216 }
|
|
217 }
|
|
218
|
|
219 /**
|
|
220 * read unsigned golomb rice code (jpegls).
|
|
221 */
|
|
222 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
|
|
223 unsigned int buf;
|
|
224 int log;
|
|
225
|
|
226 OPEN_READER(re, gb);
|
|
227 UPDATE_CACHE(re, gb);
|
|
228 buf=GET_CACHE(re, gb);
|
|
229
|
|
230 log= av_log2(buf);
|
|
231
|
|
232 if(log > 31-11){
|
|
233 buf >>= log - k;
|
|
234 buf += (30-log)<<k;
|
|
235 LAST_SKIP_BITS(re, gb, 32 + k - log);
|
|
236 CLOSE_READER(re, gb);
|
|
237
|
|
238 return buf;
|
|
239 }else{
|
|
240 int i;
|
|
241 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
|
|
242 LAST_SKIP_BITS(re, gb, 1);
|
|
243 UPDATE_CACHE(re, gb);
|
|
244 }
|
|
245 SKIP_BITS(re, gb, 1);
|
|
246
|
|
247 if(i < limit - 1){
|
|
248 if(k){
|
|
249 buf = SHOW_UBITS(re, gb, k);
|
|
250 LAST_SKIP_BITS(re, gb, k);
|
|
251 }else{
|
|
252 buf=0;
|
|
253 }
|
|
254
|
|
255 CLOSE_READER(re, gb);
|
|
256 return buf + (i<<k);
|
|
257 }else if(i == limit - 1){
|
|
258 buf = SHOW_UBITS(re, gb, esc_len);
|
|
259 LAST_SKIP_BITS(re, gb, esc_len);
|
|
260 CLOSE_READER(re, gb);
|
|
261
|
|
262 return buf + 1;
|
|
263 }else
|
|
264 return -1;
|
|
265 }
|
|
266 }
|
|
267
|
|
268 /**
|
|
269 * read signed golomb rice code (ffv1).
|
|
270 */
|
|
271 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
|
|
272 int v= get_ur_golomb(gb, k, limit, esc_len);
|
|
273
|
|
274 v++;
|
|
275 if (v&1) return v>>1;
|
|
276 else return -(v>>1);
|
|
277
|
|
278 // return (v>>1) ^ -(v&1);
|
|
279 }
|
|
280
|
|
281 /**
|
|
282 * read signed golomb rice code (flac).
|
|
283 */
|
|
284 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
|
|
285 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
|
|
286 return (v>>1) ^ -(v&1);
|
|
287 }
|
|
288
|
|
289 /**
|
|
290 * read unsigned golomb rice code (shorten).
|
|
291 */
|
|
292 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
|
|
293 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
|
|
294 }
|
|
295
|
|
296 /**
|
|
297 * read signed golomb rice code (shorten).
|
|
298 */
|
|
299 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
|
|
300 {
|
|
301 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
|
|
302 if (uvar & 1)
|
|
303 return ~(uvar >> 1);
|
|
304 else
|
|
305 return uvar >> 1;
|
|
306 }
|
|
307
|
|
308
|
|
309
|
|
310 #ifdef TRACE
|
|
311
|
|
312 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
|
|
313 int show= show_bits(s, 24);
|
|
314 int pos= get_bits_count(s);
|
|
315 int i= get_ue_golomb(s);
|
|
316 int len= get_bits_count(s) - pos;
|
|
317 int bits= show>>(24-len);
|
|
318
|
|
319 print_bin(bits, len);
|
|
320
|
|
321 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
|
|
322
|
|
323 return i;
|
|
324 }
|
|
325
|
|
326 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
|
|
327 int show= show_bits(s, 24);
|
|
328 int pos= get_bits_count(s);
|
|
329 int i= get_se_golomb(s);
|
|
330 int len= get_bits_count(s) - pos;
|
|
331 int bits= show>>(24-len);
|
|
332
|
|
333 print_bin(bits, len);
|
|
334
|
|
335 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
|
|
336
|
|
337 return i;
|
|
338 }
|
|
339
|
|
340 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
|
|
341 int show= show_bits(s, 24);
|
|
342 int pos= get_bits_count(s);
|
|
343 int i= get_te0_golomb(s, r);
|
|
344 int len= get_bits_count(s) - pos;
|
|
345 int bits= show>>(24-len);
|
|
346
|
|
347 print_bin(bits, len);
|
|
348
|
|
349 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
|
|
350
|
|
351 return i;
|
|
352 }
|
|
353
|
|
354 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
|
355 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
|
356 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
|
357 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
|
|
358
|
|
359 #endif
|
|
360
|
|
361 /**
|
|
362 * write unsigned exp golomb code.
|
|
363 */
|
|
364 static inline void set_ue_golomb(PutBitContext *pb, int i){
|
|
365 int e;
|
|
366
|
|
367 assert(i>=0);
|
|
368
|
|
369 #if 0
|
|
370 if(i=0){
|
|
371 put_bits(pb, 1, 1);
|
|
372 return;
|
|
373 }
|
|
374 #endif
|
|
375 if(i<256)
|
|
376 put_bits(pb, ff_ue_golomb_len[i], i+1);
|
|
377 else{
|
|
378 e= av_log2(i+1);
|
|
379
|
|
380 put_bits(pb, 2*e+1, i+1);
|
|
381 }
|
|
382 }
|
|
383
|
|
384 /**
|
|
385 * write truncated unsigned exp golomb code.
|
|
386 */
|
|
387 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
|
|
388 assert(range >= 1);
|
|
389 assert(i<=range);
|
|
390
|
|
391 if(range==2) put_bits(pb, 1, i^1);
|
|
392 else set_ue_golomb(pb, i);
|
|
393 }
|
|
394
|
|
395 /**
|
|
396 * write signed exp golomb code. 16 bits at most.
|
|
397 */
|
|
398 static inline void set_se_golomb(PutBitContext *pb, int i){
|
|
399 // if (i>32767 || i<-32767)
|
|
400 // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i);
|
|
401 #if 0
|
|
402 if(i<=0) i= -2*i;
|
|
403 else i= 2*i-1;
|
|
404 #elif 1
|
|
405 i= 2*i-1;
|
|
406 if(i<0) i^= -1; //FIXME check if gcc does the right thing
|
|
407 #else
|
|
408 i= 2*i-1;
|
|
409 i^= (i>>31);
|
|
410 #endif
|
|
411 set_ue_golomb(pb, i);
|
|
412 }
|
|
413
|
|
414 /**
|
|
415 * write unsigned golomb rice code (ffv1).
|
|
416 */
|
|
417 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
|
|
418 int e;
|
|
419
|
|
420 assert(i>=0);
|
|
421
|
|
422 e= i>>k;
|
|
423 if(e<limit){
|
|
424 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
|
|
425 }else{
|
|
426 put_bits(pb, limit + esc_len, i - limit + 1);
|
|
427 }
|
|
428 }
|
|
429
|
|
430 /**
|
|
431 * write unsigned golomb rice code (jpegls).
|
|
432 */
|
|
433 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
|
|
434 int e;
|
|
435
|
|
436 assert(i>=0);
|
|
437
|
|
438 e= (i>>k) + 1;
|
|
439 if(e<limit){
|
|
440 while(e > 31) {
|
|
441 put_bits(pb, 31, 0);
|
|
442 e -= 31;
|
|
443 }
|
|
444 put_bits(pb, e, 1);
|
|
445 if(k)
|
|
446 put_bits(pb, k, i&((1<<k)-1));
|
|
447 }else{
|
|
448 while(limit > 31) {
|
|
449 put_bits(pb, 31, 0);
|
|
450 limit -= 31;
|
|
451 }
|
|
452 put_bits(pb, limit , 1);
|
|
453 put_bits(pb, esc_len, i - 1);
|
|
454 }
|
|
455 }
|
|
456
|
|
457 /**
|
|
458 * write signed golomb rice code (ffv1).
|
|
459 */
|
|
460 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
|
|
461 int v;
|
|
462
|
|
463 v = -2*i-1;
|
|
464 v ^= (v>>31);
|
|
465
|
|
466 set_ur_golomb(pb, v, k, limit, esc_len);
|
|
467 }
|
|
468
|
|
469 /**
|
|
470 * write signed golomb rice code (flac).
|
|
471 */
|
|
472 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
|
|
473 int v;
|
|
474
|
|
475 v = -2*i-1;
|
|
476 v ^= (v>>31);
|
|
477
|
|
478 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
|
|
479 }
|