comparison golomb.h @ 1168:5af9aeadbdc3 libavcodec

H264 decoder & demuxer
author michaelni
date Fri, 04 Apr 2003 14:42:28 +0000
parents
children 4e891257d3e2
comparison
equal deleted inserted replaced
1167:35b80080b2db 1168:5af9aeadbdc3
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
28 extern const uint8_t ff_golomb_vlc_len[512];
29 extern const uint8_t ff_ue_golomb_vlc_code[512];
30 extern const int8_t ff_se_golomb_vlc_code[512];
31 extern const uint8_t ff_ue_golomb_len[256];
32
33
34 /**
35 * read unsigned exp golomb code.
36 */
37 static inline int get_ue_golomb(GetBitContext *gb){
38 unsigned int buf;
39 int log;
40
41 OPEN_READER(re, gb);
42 UPDATE_CACHE(re, gb);
43 buf=GET_CACHE(re, gb);
44
45 if(buf >= (1<<27)){
46 buf >>= 32 - 9;
47 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
48 CLOSE_READER(re, gb);
49
50 return ff_ue_golomb_vlc_code[buf];
51 }else{
52 log= 2*av_log2(buf) - 31;
53 buf>>= log;
54 buf--;
55 LAST_SKIP_BITS(re, gb, 32 - log);
56 CLOSE_READER(re, gb);
57
58 return buf;
59 }
60 }
61
62 /**
63 * read unsigned truncated exp golomb code.
64 */
65 static inline int get_te0_golomb(GetBitContext *gb, int range){
66 assert(range >= 1);
67
68 if(range==1) return 0;
69 else if(range==2) return get_bits1(gb);
70 else return get_ue_golomb(gb);
71 }
72
73 /**
74 * read unsigned truncated exp golomb code.
75 */
76 static inline int get_te_golomb(GetBitContext *gb, int range){
77 assert(range >= 1);
78
79 if(range==2) return get_bits1(gb);
80 else return get_ue_golomb(gb);
81 }
82
83
84 /**
85 * read signed exp golomb code.
86 */
87 static inline int get_se_golomb(GetBitContext *gb){
88 unsigned int buf;
89 int log;
90
91 OPEN_READER(re, gb);
92 UPDATE_CACHE(re, gb);
93 buf=GET_CACHE(re, gb);
94
95 if(buf >= (1<<27)){
96 buf >>= 32 - 9;
97 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
98 CLOSE_READER(re, gb);
99
100 return ff_se_golomb_vlc_code[buf];
101 }else{
102 log= 2*av_log2(buf) - 31;
103 buf>>= log;
104
105 LAST_SKIP_BITS(re, gb, 32 - log);
106 CLOSE_READER(re, gb);
107
108 if(buf&1) buf= -(buf>>1);
109 else buf= (buf>>1);
110
111 return buf;
112 }
113 }
114
115 #ifdef TRACE
116
117 static inline int get_ue(GetBitContext *s, char *file, char *func, int line){
118 int show= show_bits(s, 24);
119 int pos= get_bits_count(s);
120 int i= get_ue_golomb(s);
121 int len= get_bits_count(s) - pos;
122 int bits= show>>(24-len);
123
124 print_bin(bits, len);
125
126 printf("%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
127
128 return i;
129 }
130
131 static inline int get_se(GetBitContext *s, char *file, char *func, int line){
132 int show= show_bits(s, 24);
133 int pos= get_bits_count(s);
134 int i= get_se_golomb(s);
135 int len= get_bits_count(s) - pos;
136 int bits= show>>(24-len);
137
138 print_bin(bits, len);
139
140 printf("%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
141
142 return i;
143 }
144
145 static inline int get_te(GetBitContext *s, int r, char *file, char *func, int line){
146 int show= show_bits(s, 24);
147 int pos= get_bits_count(s);
148 int i= get_te0_golomb(s, r);
149 int len= get_bits_count(s) - pos;
150 int bits= show>>(24-len);
151
152 print_bin(bits, len);
153
154 printf("%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
155
156 return i;
157 }
158
159 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
160 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
161 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
162 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
163
164 #endif
165
166 /**
167 * write unsigned exp golomb code.
168 */
169 static inline void set_ue_golomb(PutBitContext *pb, int i){
170 int e;
171
172 assert(i>=0);
173
174 #if 0
175 if(i=0){
176 put_bits(pb, 1, 1);
177 return;
178 }
179 #endif
180 if(i<256)
181 put_bits(pb, ff_ue_golomb_len[i], i+1);
182 else{
183 e= av_log2(i+1);
184
185 put_bits(pb, 2*e+1, i+1);
186 }
187 }
188
189 /**
190 * write truncated unsigned exp golomb code.
191 */
192 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
193 assert(range >= 1);
194 assert(i<=range);
195
196 if(range==2) put_bits(pb, 1, i);
197 else set_ue_golomb(pb, i);
198 }
199
200 /**
201 * write signed exp golomb code.
202 */
203 static inline void set_se_golomb(PutBitContext *pb, int i){
204 #if 0
205 if(i<=0) i= -2*i;
206 else i= 2*i-1;
207 #elif 1
208 i= 2*i-1;
209 if(i<0) i^= -1; //FIXME check if gcc does the right thing
210 #else
211 i= 2*i-1;
212 i^= (i>>31);
213 #endif
214 set_ue_golomb(pb, i);
215 }