Updated our source code header to explicitly mention that we are GPL v2 or
[Rockbox.git] / apps / codecs / libffmpegFLAC / golomb.h
blob57e84f4e57c22dddafcc60574d3df9e43aeb7773
1 /*
2 * exp golomb vlc stuff
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Alex Beregszaszi
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <limits.h>
24 /* From libavutil/common.h */
25 extern const uint8_t ff_log2_tab[256];
27 static inline int av_log2(unsigned int v)
29 int n;
31 n = 0;
32 if (v & 0xffff0000) {
33 v >>= 16;
34 n += 16;
36 if (v & 0xff00) {
37 v >>= 8;
38 n += 8;
40 n += ff_log2_tab[v];
42 return n;
45 /**
46 * @file golomb.h
47 * @brief
48 * exp golomb vlc stuff
49 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
53 /**
54 * read unsigned golomb rice code (jpegls).
56 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
57 unsigned int buf;
58 int log;
60 OPEN_READER(re, gb);
61 UPDATE_CACHE(re, gb);
62 buf=GET_CACHE(re, gb);
64 log= av_log2(buf);
66 if(log > 31-11){
67 buf >>= log - k;
68 buf += (30-log)<<k;
69 LAST_SKIP_BITS(re, gb, 32 + k - log);
70 CLOSE_READER(re, gb);
72 return buf;
73 }else{
74 int i;
75 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
76 LAST_SKIP_BITS(re, gb, 1);
77 UPDATE_CACHE(re, gb);
79 SKIP_BITS(re, gb, 1);
81 if(i < limit - 1){
82 if(k){
83 buf = SHOW_UBITS(re, gb, k);
84 LAST_SKIP_BITS(re, gb, k);
85 }else{
86 buf=0;
89 CLOSE_READER(re, gb);
90 return buf + (i<<k);
91 }else if(i == limit - 1){
92 buf = SHOW_UBITS(re, gb, esc_len);
93 LAST_SKIP_BITS(re, gb, esc_len);
94 CLOSE_READER(re, gb);
96 return buf + 1;
97 }else
98 return -1;
103 * read signed golomb rice code (flac).
105 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
106 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
107 return (v>>1) ^ -(v&1);
111 * read unsigned golomb rice code (shorten).
113 #define get_ur_golomb_shorten(gb, k) get_ur_golomb_jpegls(gb, k, INT_MAX, 0)
115 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
116 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
121 * read signed golomb rice code (shorten).
123 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
125 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
126 if (uvar & 1)
127 return ~(uvar >> 1);
128 else
129 return uvar >> 1;