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