Mercurial > libavcodec.hg
annotate golomb.h @ 8699:450a1c5bab22 libavcodec
Use '#if defined()' for OS-specific preprocessor checks.
Avoids some warnings about undefined preprocessor directives.
author | diego |
---|---|
date | Fri, 30 Jan 2009 23:50:13 +0000 |
parents | d6faaf27eacf |
children | e9d9d946f213 |
rev | line source |
---|---|
1168 | 1 /* |
2 * exp golomb vlc stuff | |
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> | |
2215 | 4 * Copyright (c) 2004 Alex Beregszaszi |
1168 | 5 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3353
diff
changeset
|
6 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3353
diff
changeset
|
7 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3353
diff
changeset
|
8 * FFmpeg is free software; you can redistribute it and/or |
1168 | 9 * modify it under the terms of the GNU Lesser General Public |
10 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3353
diff
changeset
|
11 * version 2.1 of the License, or (at your option) any later version. |
1168 | 12 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3353
diff
changeset
|
13 * FFmpeg is distributed in the hope that it will be useful, |
1168 | 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 | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3353
diff
changeset
|
19 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
1168 | 21 */ |
2967 | 22 |
1168 | 23 /** |
24 * @file golomb.h | |
2967 | 25 * @brief |
1168 | 26 * exp golomb vlc stuff |
2215 | 27 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi |
1168 | 28 */ |
29 | |
7760 | 30 #ifndef AVCODEC_GOLOMB_H |
31 #define AVCODEC_GOLOMB_H | |
5163 | 32 |
5162 | 33 #include <stdint.h> |
34 #include "bitstream.h" | |
35 | |
1234 | 36 #define INVALID_VLC 0x80000000 |
37 | |
1168 | 38 extern const uint8_t ff_golomb_vlc_len[512]; |
39 extern const uint8_t ff_ue_golomb_vlc_code[512]; | |
40 extern const int8_t ff_se_golomb_vlc_code[512]; | |
41 extern const uint8_t ff_ue_golomb_len[256]; | |
42 | |
1250 | 43 extern const uint8_t ff_interleaved_golomb_vlc_len[256]; |
44 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256]; | |
45 extern const int8_t ff_interleaved_se_golomb_vlc_code[256]; | |
5546 | 46 extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256]; |
1250 | 47 |
2967 | 48 |
1168 | 49 /** |
50 * read unsigned exp golomb code. | |
51 */ | |
52 static inline int get_ue_golomb(GetBitContext *gb){ | |
53 unsigned int buf; | |
54 int log; | |
2967 | 55 |
1168 | 56 OPEN_READER(re, gb); |
57 UPDATE_CACHE(re, gb); | |
58 buf=GET_CACHE(re, gb); | |
2967 | 59 |
1168 | 60 if(buf >= (1<<27)){ |
61 buf >>= 32 - 9; | |
62 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); | |
63 CLOSE_READER(re, gb); | |
2967 | 64 |
1168 | 65 return ff_ue_golomb_vlc_code[buf]; |
66 }else{ | |
67 log= 2*av_log2(buf) - 31; | |
68 buf>>= log; | |
69 buf--; | |
70 LAST_SKIP_BITS(re, gb, 32 - log); | |
71 CLOSE_READER(re, gb); | |
2967 | 72 |
1168 | 73 return buf; |
74 } | |
75 } | |
76 | |
8449 | 77 /** |
78 * read unsigned exp golomb code, constraint to a max of 31 | |
79 */ | |
80 static inline int get_ue_golomb_31(GetBitContext *gb){ | |
81 unsigned int buf; | |
82 | |
83 OPEN_READER(re, gb); | |
84 UPDATE_CACHE(re, gb); | |
85 buf=GET_CACHE(re, gb); | |
86 | |
87 buf >>= 32 - 9; | |
88 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); | |
89 CLOSE_READER(re, gb); | |
90 | |
91 return ff_ue_golomb_vlc_code[buf]; | |
92 } | |
93 | |
1234 | 94 static inline int svq3_get_ue_golomb(GetBitContext *gb){ |
1250 | 95 uint32_t buf; |
1234 | 96 |
97 OPEN_READER(re, gb); | |
98 UPDATE_CACHE(re, gb); | |
1250 | 99 buf=GET_CACHE(re, gb); |
2967 | 100 |
1250 | 101 if(buf&0xAA800000){ |
102 buf >>= 32 - 8; | |
103 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]); | |
104 CLOSE_READER(re, gb); | |
2967 | 105 |
1250 | 106 return ff_interleaved_ue_golomb_vlc_code[buf]; |
107 }else{ | |
5546 | 108 int ret = 1; |
109 | |
110 while (1) { | |
111 buf >>= 32 - 8; | |
112 LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8)); | |
2087
a4d3699c6636
1000l to the ffsvq3 author, our default bitstream reader is only guranteed to be able to read 25bit at a time
michael
parents:
1812
diff
changeset
|
113 |
5546 | 114 if (ff_interleaved_golomb_vlc_len[buf] != 9){ |
115 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1; | |
116 ret |= ff_interleaved_dirac_golomb_vlc_code[buf]; | |
117 break; | |
118 } | |
119 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf]; | |
120 UPDATE_CACHE(re, gb); | |
121 buf = GET_CACHE(re, gb); | |
1250 | 122 } |
1234 | 123 |
1250 | 124 CLOSE_READER(re, gb); |
5546 | 125 return ret - 1; |
1250 | 126 } |
1234 | 127 } |
128 | |
1168 | 129 /** |
130 * read unsigned truncated exp golomb code. | |
131 */ | |
132 static inline int get_te0_golomb(GetBitContext *gb, int range){ | |
133 assert(range >= 1); | |
2967 | 134 |
1168 | 135 if(range==1) return 0; |
1169 | 136 else if(range==2) return get_bits1(gb)^1; |
1168 | 137 else return get_ue_golomb(gb); |
138 } | |
139 | |
140 /** | |
141 * read unsigned truncated exp golomb code. | |
142 */ | |
143 static inline int get_te_golomb(GetBitContext *gb, int range){ | |
144 assert(range >= 1); | |
2967 | 145 |
1169 | 146 if(range==2) return get_bits1(gb)^1; |
1168 | 147 else return get_ue_golomb(gb); |
148 } | |
149 | |
150 | |
151 /** | |
152 * read signed exp golomb code. | |
153 */ | |
154 static inline int get_se_golomb(GetBitContext *gb){ | |
155 unsigned int buf; | |
156 int log; | |
2967 | 157 |
1168 | 158 OPEN_READER(re, gb); |
159 UPDATE_CACHE(re, gb); | |
160 buf=GET_CACHE(re, gb); | |
2967 | 161 |
1168 | 162 if(buf >= (1<<27)){ |
163 buf >>= 32 - 9; | |
164 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); | |
165 CLOSE_READER(re, gb); | |
2967 | 166 |
1168 | 167 return ff_se_golomb_vlc_code[buf]; |
168 }else{ | |
169 log= 2*av_log2(buf) - 31; | |
170 buf>>= log; | |
2967 | 171 |
1168 | 172 LAST_SKIP_BITS(re, gb, 32 - log); |
173 CLOSE_READER(re, gb); | |
2967 | 174 |
1168 | 175 if(buf&1) buf= -(buf>>1); |
176 else buf= (buf>>1); | |
177 | |
178 return buf; | |
179 } | |
180 } | |
181 | |
1234 | 182 static inline int svq3_get_se_golomb(GetBitContext *gb){ |
183 unsigned int buf; | |
184 int log; | |
185 | |
186 OPEN_READER(re, gb); | |
187 UPDATE_CACHE(re, gb); | |
1250 | 188 buf=GET_CACHE(re, gb); |
1234 | 189 |
1250 | 190 if(buf&0xAA800000){ |
191 buf >>= 32 - 8; | |
192 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]); | |
193 CLOSE_READER(re, gb); | |
2967 | 194 |
1250 | 195 return ff_interleaved_se_golomb_vlc_code[buf]; |
196 }else{ | |
2439 | 197 LAST_SKIP_BITS(re, gb, 8); |
198 UPDATE_CACHE(re, gb); | |
199 buf |= 1 | (GET_CACHE(re, gb) >> 8); | |
200 | |
1250 | 201 if((buf & 0xAAAAAAAA) == 0) |
202 return INVALID_VLC; | |
1234 | 203 |
1250 | 204 for(log=31; (buf & 0x80000000) == 0; log--){ |
205 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30); | |
206 } | |
1234 | 207 |
2439 | 208 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8); |
1250 | 209 CLOSE_READER(re, gb); |
210 | |
211 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; | |
212 } | |
1234 | 213 } |
214 | |
5546 | 215 static inline int dirac_get_se_golomb(GetBitContext *gb){ |
216 uint32_t buf; | |
217 uint32_t ret; | |
218 | |
219 ret = svq3_get_ue_golomb(gb); | |
220 | |
221 if (ret) { | |
222 OPEN_READER(re, gb); | |
223 UPDATE_CACHE(re, gb); | |
224 buf = SHOW_SBITS(re, gb, 1); | |
225 LAST_SKIP_BITS(re, gb, 1); | |
226 ret = (ret ^ buf) - buf; | |
227 CLOSE_READER(re, gb); | |
228 } | |
229 | |
230 return ret; | |
231 } | |
232 | |
1306 | 233 /** |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
234 * read unsigned golomb rice code (ffv1). |
1306 | 235 */ |
236 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){ | |
237 unsigned int buf; | |
238 int log; | |
2967 | 239 |
1306 | 240 OPEN_READER(re, gb); |
241 UPDATE_CACHE(re, gb); | |
242 buf=GET_CACHE(re, gb); | |
243 | |
244 log= av_log2(buf); | |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
245 |
1306 | 246 if(log > 31-limit){ |
247 buf >>= log - k; | |
248 buf += (30-log)<<k; | |
249 LAST_SKIP_BITS(re, gb, 32 + k - log); | |
250 CLOSE_READER(re, gb); | |
2967 | 251 |
1306 | 252 return buf; |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
253 }else{ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
254 buf >>= 32 - limit - esc_len; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
255 LAST_SKIP_BITS(re, gb, esc_len + limit); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
256 CLOSE_READER(re, gb); |
2967 | 257 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
258 return buf + limit - 1; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
259 } |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
260 } |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
261 |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
262 /** |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
263 * read unsigned golomb rice code (jpegls). |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
264 */ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
265 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
266 unsigned int buf; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
267 int log; |
2967 | 268 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
269 OPEN_READER(re, gb); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
270 UPDATE_CACHE(re, gb); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
271 buf=GET_CACHE(re, gb); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
272 |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
273 log= av_log2(buf); |
2967 | 274 |
7869
cd18f66b3663
Fix regression test failure caused by golomb limit not being considered
michael
parents:
7866
diff
changeset
|
275 if(log - k >= 32-MIN_CACHE_BITS && 32-log < limit){ |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
276 buf >>= log - k; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
277 buf += (30-log)<<k; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
278 LAST_SKIP_BITS(re, gb, 32 + k - log); |
1306 | 279 CLOSE_READER(re, gb); |
2967 | 280 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
281 return buf; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
282 }else{ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
283 int i; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
284 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
285 LAST_SKIP_BITS(re, gb, 1); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
286 UPDATE_CACHE(re, gb); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
287 } |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
288 SKIP_BITS(re, gb, 1); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
289 |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
290 if(i < limit - 1){ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
291 if(k){ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
292 buf = SHOW_UBITS(re, gb, k); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
293 LAST_SKIP_BITS(re, gb, k); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
294 }else{ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
295 buf=0; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
296 } |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
297 |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
298 CLOSE_READER(re, gb); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
299 return buf + (i<<k); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
300 }else if(i == limit - 1){ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
301 buf = SHOW_UBITS(re, gb, esc_len); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
302 LAST_SKIP_BITS(re, gb, esc_len); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
303 CLOSE_READER(re, gb); |
2967 | 304 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
305 return buf + 1; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
306 }else |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
307 return -1; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
308 } |
1306 | 309 } |
310 | |
1812 | 311 /** |
2215 | 312 * read signed golomb rice code (ffv1). |
313 */ | |
2220 | 314 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){ |
2215 | 315 int v= get_ur_golomb(gb, k, limit, esc_len); |
2967 | 316 |
2215 | 317 v++; |
318 if (v&1) return v>>1; | |
319 else return -(v>>1); | |
2967 | 320 |
2215 | 321 // return (v>>1) ^ -(v&1); |
322 } | |
2220 | 323 |
2215 | 324 /** |
325 * read signed golomb rice code (flac). | |
1812 | 326 */ |
327 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){ | |
328 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len); | |
329 return (v>>1) ^ -(v&1); | |
330 } | |
331 | |
2525
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
332 /** |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
333 * read unsigned golomb rice code (shorten). |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
334 */ |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
335 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){ |
2979 | 336 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0); |
2525
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
337 } |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
338 |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
339 /** |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
340 * read signed golomb rice code (shorten). |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
341 */ |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
342 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k) |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
343 { |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
344 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0); |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
345 if (uvar & 1) |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
346 return ~(uvar >> 1); |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
347 else |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
348 return uvar >> 1; |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
349 } |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
350 |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
351 |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
352 |
1168 | 353 #ifdef TRACE |
354 | |
2438
e98b5e0de86b
compile with TRACE define patch by (Loic <lll+ffmpeg m4x org>)
michael
parents:
2220
diff
changeset
|
355 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){ |
1168 | 356 int show= show_bits(s, 24); |
357 int pos= get_bits_count(s); | |
358 int i= get_ue_golomb(s); | |
359 int len= get_bits_count(s) - pos; | |
360 int bits= show>>(24-len); | |
2967 | 361 |
1168 | 362 print_bin(bits, len); |
2967 | 363 |
2215 | 364 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); |
2967 | 365 |
1168 | 366 return i; |
367 } | |
368 | |
2438
e98b5e0de86b
compile with TRACE define patch by (Loic <lll+ffmpeg m4x org>)
michael
parents:
2220
diff
changeset
|
369 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){ |
1168 | 370 int show= show_bits(s, 24); |
371 int pos= get_bits_count(s); | |
372 int i= get_se_golomb(s); | |
373 int len= get_bits_count(s) - pos; | |
374 int bits= show>>(24-len); | |
2967 | 375 |
1168 | 376 print_bin(bits, len); |
2967 | 377 |
2215 | 378 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); |
2967 | 379 |
1168 | 380 return i; |
381 } | |
382 | |
2438
e98b5e0de86b
compile with TRACE define patch by (Loic <lll+ffmpeg m4x org>)
michael
parents:
2220
diff
changeset
|
383 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){ |
1168 | 384 int show= show_bits(s, 24); |
385 int pos= get_bits_count(s); | |
386 int i= get_te0_golomb(s, r); | |
387 int len= get_bits_count(s) - pos; | |
388 int bits= show>>(24-len); | |
2967 | 389 |
1168 | 390 print_bin(bits, len); |
2967 | 391 |
2215 | 392 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); |
2967 | 393 |
1168 | 394 return i; |
395 } | |
396 | |
397 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
398 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
399 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
400 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
401 | |
402 #endif | |
403 | |
404 /** | |
405 * write unsigned exp golomb code. | |
406 */ | |
407 static inline void set_ue_golomb(PutBitContext *pb, int i){ | |
408 int e; | |
2967 | 409 |
1168 | 410 assert(i>=0); |
411 | |
412 #if 0 | |
413 if(i=0){ | |
414 put_bits(pb, 1, 1); | |
415 return; | |
416 } | |
417 #endif | |
418 if(i<256) | |
419 put_bits(pb, ff_ue_golomb_len[i], i+1); | |
420 else{ | |
421 e= av_log2(i+1); | |
2967 | 422 |
1168 | 423 put_bits(pb, 2*e+1, i+1); |
424 } | |
425 } | |
426 | |
427 /** | |
428 * write truncated unsigned exp golomb code. | |
429 */ | |
430 static inline void set_te_golomb(PutBitContext *pb, int i, int range){ | |
431 assert(range >= 1); | |
432 assert(i<=range); | |
433 | |
1169 | 434 if(range==2) put_bits(pb, 1, i^1); |
1168 | 435 else set_ue_golomb(pb, i); |
436 } | |
437 | |
438 /** | |
2905
926ea374947f
set_se_golomb can only write 16bits, add a note about this (ok, maybe it's brain dead using it with more than 16bits, but..)
alex
parents:
2525
diff
changeset
|
439 * write signed exp golomb code. 16 bits at most. |
1168 | 440 */ |
441 static inline void set_se_golomb(PutBitContext *pb, int i){ | |
2905
926ea374947f
set_se_golomb can only write 16bits, add a note about this (ok, maybe it's brain dead using it with more than 16bits, but..)
alex
parents:
2525
diff
changeset
|
442 // if (i>32767 || i<-32767) |
2979 | 443 // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i); |
2967 | 444 #if 0 |
1168 | 445 if(i<=0) i= -2*i; |
446 else i= 2*i-1; | |
447 #elif 1 | |
448 i= 2*i-1; | |
449 if(i<0) i^= -1; //FIXME check if gcc does the right thing | |
450 #else | |
451 i= 2*i-1; | |
452 i^= (i>>31); | |
453 #endif | |
454 set_ue_golomb(pb, i); | |
455 } | |
1306 | 456 |
457 /** | |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
458 * write unsigned golomb rice code (ffv1). |
1306 | 459 */ |
460 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){ | |
461 int e; | |
2967 | 462 |
1306 | 463 assert(i>=0); |
2967 | 464 |
1306 | 465 e= i>>k; |
466 if(e<limit){ | |
467 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1))); | |
468 }else{ | |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
469 put_bits(pb, limit + esc_len, i - limit + 1); |
1306 | 470 } |
471 } | |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
472 |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
473 /** |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
474 * write unsigned golomb rice code (jpegls). |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
475 */ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
476 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
477 int e; |
2967 | 478 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
479 assert(i>=0); |
2967 | 480 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
481 e= (i>>k) + 1; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
482 if(e<limit){ |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
3036
diff
changeset
|
483 while(e > 31) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
3036
diff
changeset
|
484 put_bits(pb, 31, 0); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
3036
diff
changeset
|
485 e -= 31; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
3036
diff
changeset
|
486 } |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
487 put_bits(pb, e, 1); |
1362 | 488 if(k) |
7260
3ec34b551aae
bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents:
5830
diff
changeset
|
489 put_sbits(pb, k, i); |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
490 }else{ |
4053 | 491 while(limit > 31) { |
492 put_bits(pb, 31, 0); | |
493 limit -= 31; | |
494 } | |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
495 put_bits(pb, limit , 1); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
496 put_bits(pb, esc_len, i - 1); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
497 } |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
498 } |
2215 | 499 |
500 /** | |
501 * write signed golomb rice code (ffv1). | |
502 */ | |
2220 | 503 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){ |
2215 | 504 int v; |
505 | |
506 v = -2*i-1; | |
507 v ^= (v>>31); | |
508 | |
509 set_ur_golomb(pb, v, k, limit, esc_len); | |
510 } | |
511 | |
512 /** | |
513 * write signed golomb rice code (flac). | |
514 */ | |
515 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){ | |
516 int v; | |
517 | |
518 v = -2*i-1; | |
519 v ^= (v>>31); | |
520 | |
521 set_ur_golomb_jpegls(pb, v, k, limit, esc_len); | |
522 } | |
5163 | 523 |
7760 | 524 #endif /* AVCODEC_GOLOMB_H */ |