]> git.sesse.net Git - ffmpeg/blob - libavcodec/golomb.h
Merge commit '2cd4068071b9a8908823a3107f97e938211045ce'
[ffmpeg] / libavcodec / golomb.h
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  * @file
25  * @brief
26  *     exp golomb vlc stuff
27  * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
28  */
29
30 #ifndef AVCODEC_GOLOMB_H
31 #define AVCODEC_GOLOMB_H
32
33 #include <stdint.h>
34 #include "get_bits.h"
35 #include "put_bits.h"
36
37 #define INVALID_VLC           0x80000000
38
39 extern const uint8_t ff_golomb_vlc_len[512];
40 extern const uint8_t ff_ue_golomb_vlc_code[512];
41 extern const  int8_t ff_se_golomb_vlc_code[512];
42 extern const uint8_t ff_ue_golomb_len[256];
43
44 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
45 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
46 extern const  int8_t ff_interleaved_se_golomb_vlc_code[256];
47 extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
48
49
50  /**
51  * read unsigned exp golomb code.
52  */
53 static inline int get_ue_golomb(GetBitContext *gb){
54     unsigned int buf;
55     int log;
56
57     OPEN_READER(re, gb);
58     UPDATE_CACHE(re, gb);
59     buf=GET_CACHE(re, gb);
60
61     if(buf >= (1<<27)){
62         buf >>= 32 - 9;
63         LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
64         CLOSE_READER(re, gb);
65
66         return ff_ue_golomb_vlc_code[buf];
67     }else{
68         log= 2*av_log2(buf) - 31;
69         LAST_SKIP_BITS(re, gb, 32 - log);
70         CLOSE_READER(re, gb);
71         if (CONFIG_FTRAPV && log < 0) {
72             av_log(0, AV_LOG_ERROR, "Invalid UE golomb code\n");
73             return AVERROR_INVALIDDATA;
74         }
75         buf>>= log;
76         buf--;
77
78         return buf;
79     }
80 }
81
82 /**
83  * Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
84  */
85 static inline unsigned get_ue_golomb_long(GetBitContext *gb)
86 {
87     unsigned buf, log;
88
89     buf = show_bits_long(gb, 32);
90     log = 31 - av_log2(buf);
91     skip_bits_long(gb, log);
92
93     return get_bits_long(gb, log + 1) - 1;
94 }
95
96  /**
97  * read unsigned exp golomb code, constraint to a max of 31.
98  * the return value is undefined if the stored value exceeds 31.
99  */
100 static inline int get_ue_golomb_31(GetBitContext *gb){
101     unsigned int buf;
102
103     OPEN_READER(re, gb);
104     UPDATE_CACHE(re, gb);
105     buf=GET_CACHE(re, gb);
106
107     buf >>= 32 - 9;
108     LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
109     CLOSE_READER(re, gb);
110
111     return ff_ue_golomb_vlc_code[buf];
112 }
113
114 static inline unsigned svq3_get_ue_golomb(GetBitContext *gb)
115 {
116     uint32_t buf;
117
118     OPEN_READER(re, gb);
119     UPDATE_CACHE(re, gb);
120     buf=GET_CACHE(re, gb);
121
122     if(buf&0xAA800000){
123         buf >>= 32 - 8;
124         LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
125         CLOSE_READER(re, gb);
126
127         return ff_interleaved_ue_golomb_vlc_code[buf];
128     }else{
129         unsigned ret = 1;
130
131         do {
132             buf >>= 32 - 8;
133             LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
134
135             if (ff_interleaved_golomb_vlc_len[buf] != 9){
136                 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
137                 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
138                 break;
139             }
140             ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
141             UPDATE_CACHE(re, gb);
142             buf = GET_CACHE(re, gb);
143         } while (ret<0x8000000U && HAVE_BITS_REMAINING(re, gb));
144
145         CLOSE_READER(re, gb);
146         return ret - 1;
147     }
148 }
149
150 /**
151  * read unsigned truncated exp golomb code.
152  */
153 static inline int get_te0_golomb(GetBitContext *gb, int range){
154     av_assert2(range >= 1);
155
156     if(range==1)      return 0;
157     else if(range==2) return get_bits1(gb)^1;
158     else              return get_ue_golomb(gb);
159 }
160
161 /**
162  * read unsigned truncated exp golomb code.
163  */
164 static inline int get_te_golomb(GetBitContext *gb, int range){
165     av_assert2(range >= 1);
166
167     if(range==2) return get_bits1(gb)^1;
168     else         return get_ue_golomb(gb);
169 }
170
171
172 /**
173  * read signed exp golomb code.
174  */
175 static inline int get_se_golomb(GetBitContext *gb){
176     unsigned int buf;
177     int log;
178
179     OPEN_READER(re, gb);
180     UPDATE_CACHE(re, gb);
181     buf=GET_CACHE(re, gb);
182
183     if(buf >= (1<<27)){
184         buf >>= 32 - 9;
185         LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
186         CLOSE_READER(re, gb);
187
188         return ff_se_golomb_vlc_code[buf];
189     }else{
190         log= 2*av_log2(buf) - 31;
191         buf>>= log;
192
193         LAST_SKIP_BITS(re, gb, 32 - log);
194         CLOSE_READER(re, gb);
195
196         if(buf&1) buf= -(buf>>1);
197         else      buf=  (buf>>1);
198
199         return buf;
200     }
201 }
202
203 static inline int svq3_get_se_golomb(GetBitContext *gb){
204     unsigned int buf;
205     int log;
206
207     OPEN_READER(re, gb);
208     UPDATE_CACHE(re, gb);
209     buf=GET_CACHE(re, gb);
210
211     if(buf&0xAA800000){
212         buf >>= 32 - 8;
213         LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
214         CLOSE_READER(re, gb);
215
216         return ff_interleaved_se_golomb_vlc_code[buf];
217     }else{
218         LAST_SKIP_BITS(re, gb, 8);
219         UPDATE_CACHE(re, gb);
220         buf |= 1 | (GET_CACHE(re, gb) >> 8);
221
222         if((buf & 0xAAAAAAAA) == 0)
223             return INVALID_VLC;
224
225         for(log=31; (buf & 0x80000000) == 0; log--){
226             buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
227         }
228
229         LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
230         CLOSE_READER(re, gb);
231
232         return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
233     }
234 }
235
236 static inline int dirac_get_se_golomb(GetBitContext *gb){
237     uint32_t buf;
238     uint32_t ret;
239
240     ret = svq3_get_ue_golomb(gb);
241
242     if (ret) {
243         OPEN_READER(re, gb);
244         UPDATE_CACHE(re, gb);
245         buf = SHOW_SBITS(re, gb, 1);
246         LAST_SKIP_BITS(re, gb, 1);
247         ret = (ret ^ buf) - buf;
248         CLOSE_READER(re, gb);
249     }
250
251     return ret;
252 }
253
254 /**
255  * read unsigned golomb rice code (ffv1).
256  */
257 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
258     unsigned int buf;
259     int log;
260
261     OPEN_READER(re, gb);
262     UPDATE_CACHE(re, gb);
263     buf=GET_CACHE(re, gb);
264
265     log= av_log2(buf);
266
267     if(log > 31-limit){
268         buf >>= log - k;
269         buf += (30-log)<<k;
270         LAST_SKIP_BITS(re, gb, 32 + k - log);
271         CLOSE_READER(re, gb);
272
273         return buf;
274     }else{
275         LAST_SKIP_BITS(re, gb, limit);
276         UPDATE_CACHE(re, gb);
277
278         buf = SHOW_UBITS(re, gb, esc_len);
279
280         LAST_SKIP_BITS(re, gb, esc_len);
281         CLOSE_READER(re, gb);
282
283         return buf + limit - 1;
284     }
285 }
286
287 /**
288  * read unsigned golomb rice code (jpegls).
289  */
290 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
291     unsigned int buf;
292     int log;
293
294     OPEN_READER(re, gb);
295     UPDATE_CACHE(re, gb);
296     buf=GET_CACHE(re, gb);
297
298     log= av_log2(buf);
299
300     if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
301         buf >>= log - k;
302         buf += (30-log)<<k;
303         LAST_SKIP_BITS(re, gb, 32 + k - log);
304         CLOSE_READER(re, gb);
305
306         return buf;
307     }else{
308         int i;
309         for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
310             if (gb->size_in_bits <= re_index)
311                 return -1;
312             LAST_SKIP_BITS(re, gb, 1);
313             UPDATE_CACHE(re, gb);
314         }
315         SKIP_BITS(re, gb, 1);
316
317         if(i < limit - 1){
318             if(k){
319                 buf = SHOW_UBITS(re, gb, k);
320                 LAST_SKIP_BITS(re, gb, k);
321             }else{
322                 buf=0;
323             }
324
325             CLOSE_READER(re, gb);
326             return buf + (i<<k);
327         }else if(i == limit - 1){
328             buf = SHOW_UBITS(re, gb, esc_len);
329             LAST_SKIP_BITS(re, gb, esc_len);
330             CLOSE_READER(re, gb);
331
332             return buf + 1;
333         }else
334             return -1;
335     }
336 }
337
338 /**
339  * read signed golomb rice code (ffv1).
340  */
341 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
342     int v= get_ur_golomb(gb, k, limit, esc_len);
343
344     v++;
345     if (v&1) return v>>1;
346     else return -(v>>1);
347
348 //    return (v>>1) ^ -(v&1);
349 }
350
351 /**
352  * read signed golomb rice code (flac).
353  */
354 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
355     int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
356     return (v>>1) ^ -(v&1);
357 }
358
359 /**
360  * read unsigned golomb rice code (shorten).
361  */
362 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
363         return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
364 }
365
366 /**
367  * read signed golomb rice code (shorten).
368  */
369 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
370 {
371     int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
372     if (uvar & 1)
373         return ~(uvar >> 1);
374     else
375         return uvar >> 1;
376 }
377
378
379
380 #ifdef TRACE
381
382 static inline int get_ue(GetBitContext *s, const char *file, const char *func,
383                          int line)
384 {
385     int show= show_bits(s, 24);
386     int pos= get_bits_count(s);
387     int i= get_ue_golomb(s);
388     int len= get_bits_count(s) - pos;
389     int bits= show>>(24-len);
390
391     print_bin(bits, len);
392
393     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue  @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
394
395     return i;
396 }
397
398 static inline int get_se(GetBitContext *s, const char *file, const char *func,
399                          int line)
400 {
401     int show= show_bits(s, 24);
402     int pos= get_bits_count(s);
403     int i= get_se_golomb(s);
404     int len= get_bits_count(s) - pos;
405     int bits= show>>(24-len);
406
407     print_bin(bits, len);
408
409     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se  @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
410
411     return i;
412 }
413
414 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
415     int show= show_bits(s, 24);
416     int pos= get_bits_count(s);
417     int i= get_te0_golomb(s, r);
418     int len= get_bits_count(s) - pos;
419     int bits= show>>(24-len);
420
421     print_bin(bits, len);
422
423     av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te  @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
424
425     return i;
426 }
427
428 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
429 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
430 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
431 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
432
433 #endif
434
435 /**
436  * write unsigned exp golomb code.
437  */
438 static inline void set_ue_golomb(PutBitContext *pb, int i){
439     int e;
440
441     av_assert2(i>=0);
442
443 #if 0
444     if(i=0){
445         put_bits(pb, 1, 1);
446         return;
447     }
448 #endif
449     if(i<256)
450         put_bits(pb, ff_ue_golomb_len[i], i+1);
451     else{
452         e= av_log2(i+1);
453
454         put_bits(pb, 2*e+1, i+1);
455     }
456 }
457
458 /**
459  * write truncated unsigned exp golomb code.
460  */
461 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
462     av_assert2(range >= 1);
463     av_assert2(i<=range);
464
465     if(range==2) put_bits(pb, 1, i^1);
466     else         set_ue_golomb(pb, i);
467 }
468
469 /**
470  * write signed exp golomb code. 16 bits at most.
471  */
472 static inline void set_se_golomb(PutBitContext *pb, int i){
473 #if 0
474     if(i<=0) i= -2*i;
475     else     i=  2*i-1;
476 #elif 1
477     i= 2*i-1;
478     if(i<0) i^= -1; //FIXME check if gcc does the right thing
479 #else
480     i= 2*i-1;
481     i^= (i>>31);
482 #endif
483     set_ue_golomb(pb, i);
484 }
485
486 /**
487  * write unsigned golomb rice code (ffv1).
488  */
489 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
490     int e;
491
492     av_assert2(i>=0);
493
494     e= i>>k;
495     if(e<limit){
496         put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
497     }else{
498         put_bits(pb, limit + esc_len, i - limit + 1);
499     }
500 }
501
502 /**
503  * write unsigned golomb rice code (jpegls).
504  */
505 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
506     int e;
507
508     av_assert2(i>=0);
509
510     e= (i>>k) + 1;
511     if(e<limit){
512         while(e > 31) {
513             put_bits(pb, 31, 0);
514             e -= 31;
515         }
516         put_bits(pb, e, 1);
517         if(k)
518             put_sbits(pb, k, i);
519     }else{
520         while(limit > 31) {
521             put_bits(pb, 31, 0);
522             limit -= 31;
523         }
524         put_bits(pb, limit  , 1);
525         put_bits(pb, esc_len, i - 1);
526     }
527 }
528
529 /**
530  * write signed golomb rice code (ffv1).
531  */
532 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
533     int v;
534
535     v = -2*i-1;
536     v ^= (v>>31);
537
538     set_ur_golomb(pb, v, k, limit, esc_len);
539 }
540
541 /**
542  * write signed golomb rice code (flac).
543  */
544 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
545     int v;
546
547     v = -2*i-1;
548     v ^= (v>>31);
549
550     set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
551 }
552
553 #endif /* AVCODEC_GOLOMB_H */