Mercurial > libavcodec.hg
annotate golomb.h @ 10095:ef6e027092cc libavcodec
4xm: provide 16-byte alignment where required
author | mru |
---|---|
date | Wed, 26 Aug 2009 15:22:15 +0000 |
parents | 0dce4fe6e6f3 |
children | 944071b6fcb4 |
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 /** |
8718
e9d9d946f213
Use full internal pathname in doxygen @file directives.
diego
parents:
8449
diff
changeset
|
24 * @file libavcodec/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> |
9428 | 34 #include "get_bits.h" |
9411
4cb7c65fc775
Split bitstream.h, put the bitstream writer stuff in the new file
stefano
parents:
9020
diff
changeset
|
35 #include "put_bits.h" |
5162 | 36 |
1234 | 37 #define INVALID_VLC 0x80000000 |
38 | |
1168 | 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 | |
1250 | 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]; | |
5546 | 47 extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256]; |
1250 | 48 |
2967 | 49 |
1168 | 50 /** |
51 * read unsigned exp golomb code. | |
52 */ | |
53 static inline int get_ue_golomb(GetBitContext *gb){ | |
54 unsigned int buf; | |
55 int log; | |
2967 | 56 |
1168 | 57 OPEN_READER(re, gb); |
58 UPDATE_CACHE(re, gb); | |
59 buf=GET_CACHE(re, gb); | |
2967 | 60 |
1168 | 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); | |
2967 | 65 |
1168 | 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); | |
2967 | 73 |
1168 | 74 return buf; |
75 } | |
76 } | |
77 | |
8449 | 78 /** |
8969 | 79 * read unsigned exp golomb code, constraint to a max of 31. |
80 * the return value is undefined if the stored value exceeds 31. | |
8449 | 81 */ |
82 static inline int get_ue_golomb_31(GetBitContext *gb){ | |
83 unsigned int buf; | |
84 | |
85 OPEN_READER(re, gb); | |
86 UPDATE_CACHE(re, gb); | |
87 buf=GET_CACHE(re, gb); | |
88 | |
89 buf >>= 32 - 9; | |
90 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); | |
91 CLOSE_READER(re, gb); | |
92 | |
93 return ff_ue_golomb_vlc_code[buf]; | |
94 } | |
95 | |
1234 | 96 static inline int svq3_get_ue_golomb(GetBitContext *gb){ |
1250 | 97 uint32_t buf; |
1234 | 98 |
99 OPEN_READER(re, gb); | |
100 UPDATE_CACHE(re, gb); | |
1250 | 101 buf=GET_CACHE(re, gb); |
2967 | 102 |
1250 | 103 if(buf&0xAA800000){ |
104 buf >>= 32 - 8; | |
105 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]); | |
106 CLOSE_READER(re, gb); | |
2967 | 107 |
1250 | 108 return ff_interleaved_ue_golomb_vlc_code[buf]; |
109 }else{ | |
5546 | 110 int ret = 1; |
111 | |
112 while (1) { | |
113 buf >>= 32 - 8; | |
114 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
|
115 |
5546 | 116 if (ff_interleaved_golomb_vlc_len[buf] != 9){ |
117 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1; | |
118 ret |= ff_interleaved_dirac_golomb_vlc_code[buf]; | |
119 break; | |
120 } | |
121 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf]; | |
122 UPDATE_CACHE(re, gb); | |
123 buf = GET_CACHE(re, gb); | |
1250 | 124 } |
1234 | 125 |
1250 | 126 CLOSE_READER(re, gb); |
5546 | 127 return ret - 1; |
1250 | 128 } |
1234 | 129 } |
130 | |
1168 | 131 /** |
132 * read unsigned truncated exp golomb code. | |
133 */ | |
134 static inline int get_te0_golomb(GetBitContext *gb, int range){ | |
135 assert(range >= 1); | |
2967 | 136 |
1168 | 137 if(range==1) return 0; |
1169 | 138 else if(range==2) return get_bits1(gb)^1; |
1168 | 139 else return get_ue_golomb(gb); |
140 } | |
141 | |
142 /** | |
143 * read unsigned truncated exp golomb code. | |
144 */ | |
145 static inline int get_te_golomb(GetBitContext *gb, int range){ | |
146 assert(range >= 1); | |
2967 | 147 |
1169 | 148 if(range==2) return get_bits1(gb)^1; |
1168 | 149 else return get_ue_golomb(gb); |
150 } | |
151 | |
152 | |
153 /** | |
154 * read signed exp golomb code. | |
155 */ | |
156 static inline int get_se_golomb(GetBitContext *gb){ | |
157 unsigned int buf; | |
158 int log; | |
2967 | 159 |
1168 | 160 OPEN_READER(re, gb); |
161 UPDATE_CACHE(re, gb); | |
162 buf=GET_CACHE(re, gb); | |
2967 | 163 |
1168 | 164 if(buf >= (1<<27)){ |
165 buf >>= 32 - 9; | |
166 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]); | |
167 CLOSE_READER(re, gb); | |
2967 | 168 |
1168 | 169 return ff_se_golomb_vlc_code[buf]; |
170 }else{ | |
171 log= 2*av_log2(buf) - 31; | |
172 buf>>= log; | |
2967 | 173 |
1168 | 174 LAST_SKIP_BITS(re, gb, 32 - log); |
175 CLOSE_READER(re, gb); | |
2967 | 176 |
1168 | 177 if(buf&1) buf= -(buf>>1); |
178 else buf= (buf>>1); | |
179 | |
180 return buf; | |
181 } | |
182 } | |
183 | |
1234 | 184 static inline int svq3_get_se_golomb(GetBitContext *gb){ |
185 unsigned int buf; | |
186 int log; | |
187 | |
188 OPEN_READER(re, gb); | |
189 UPDATE_CACHE(re, gb); | |
1250 | 190 buf=GET_CACHE(re, gb); |
1234 | 191 |
1250 | 192 if(buf&0xAA800000){ |
193 buf >>= 32 - 8; | |
194 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]); | |
195 CLOSE_READER(re, gb); | |
2967 | 196 |
1250 | 197 return ff_interleaved_se_golomb_vlc_code[buf]; |
198 }else{ | |
2439 | 199 LAST_SKIP_BITS(re, gb, 8); |
200 UPDATE_CACHE(re, gb); | |
201 buf |= 1 | (GET_CACHE(re, gb) >> 8); | |
202 | |
1250 | 203 if((buf & 0xAAAAAAAA) == 0) |
204 return INVALID_VLC; | |
1234 | 205 |
1250 | 206 for(log=31; (buf & 0x80000000) == 0; log--){ |
207 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30); | |
208 } | |
1234 | 209 |
2439 | 210 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8); |
1250 | 211 CLOSE_READER(re, gb); |
212 | |
213 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1; | |
214 } | |
1234 | 215 } |
216 | |
5546 | 217 static inline int dirac_get_se_golomb(GetBitContext *gb){ |
218 uint32_t buf; | |
219 uint32_t ret; | |
220 | |
221 ret = svq3_get_ue_golomb(gb); | |
222 | |
223 if (ret) { | |
224 OPEN_READER(re, gb); | |
225 UPDATE_CACHE(re, gb); | |
226 buf = SHOW_SBITS(re, gb, 1); | |
227 LAST_SKIP_BITS(re, gb, 1); | |
228 ret = (ret ^ buf) - buf; | |
229 CLOSE_READER(re, gb); | |
230 } | |
231 | |
232 return ret; | |
233 } | |
234 | |
1306 | 235 /** |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
236 * read unsigned golomb rice code (ffv1). |
1306 | 237 */ |
238 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){ | |
239 unsigned int buf; | |
240 int log; | |
2967 | 241 |
1306 | 242 OPEN_READER(re, gb); |
243 UPDATE_CACHE(re, gb); | |
244 buf=GET_CACHE(re, gb); | |
245 | |
246 log= av_log2(buf); | |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
247 |
1306 | 248 if(log > 31-limit){ |
249 buf >>= log - k; | |
250 buf += (30-log)<<k; | |
251 LAST_SKIP_BITS(re, gb, 32 + k - log); | |
252 CLOSE_READER(re, gb); | |
2967 | 253 |
1306 | 254 return buf; |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
255 }else{ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
256 buf >>= 32 - limit - esc_len; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
257 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
|
258 CLOSE_READER(re, gb); |
2967 | 259 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
260 return buf + limit - 1; |
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 |
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 * read unsigned golomb rice code (jpegls). |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
266 */ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
267 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
|
268 unsigned int buf; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
269 int log; |
2967 | 270 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
271 OPEN_READER(re, gb); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
272 UPDATE_CACHE(re, gb); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
273 buf=GET_CACHE(re, gb); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
274 |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
275 log= av_log2(buf); |
2967 | 276 |
9020 | 277 if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){ |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
278 buf >>= log - k; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
279 buf += (30-log)<<k; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
280 LAST_SKIP_BITS(re, gb, 32 + k - log); |
1306 | 281 CLOSE_READER(re, gb); |
2967 | 282 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
283 return buf; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
284 }else{ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
285 int i; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
286 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
|
287 LAST_SKIP_BITS(re, gb, 1); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
288 UPDATE_CACHE(re, gb); |
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 SKIP_BITS(re, gb, 1); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
291 |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
292 if(i < limit - 1){ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
293 if(k){ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
294 buf = SHOW_UBITS(re, gb, k); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
295 LAST_SKIP_BITS(re, gb, k); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
296 }else{ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
297 buf=0; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
298 } |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
299 |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
300 CLOSE_READER(re, gb); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
301 return buf + (i<<k); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
302 }else if(i == limit - 1){ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
303 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
|
304 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
|
305 CLOSE_READER(re, gb); |
2967 | 306 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
307 return buf + 1; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
308 }else |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
309 return -1; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
310 } |
1306 | 311 } |
312 | |
1812 | 313 /** |
2215 | 314 * read signed golomb rice code (ffv1). |
315 */ | |
2220 | 316 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){ |
2215 | 317 int v= get_ur_golomb(gb, k, limit, esc_len); |
2967 | 318 |
2215 | 319 v++; |
320 if (v&1) return v>>1; | |
321 else return -(v>>1); | |
2967 | 322 |
2215 | 323 // return (v>>1) ^ -(v&1); |
324 } | |
2220 | 325 |
2215 | 326 /** |
327 * read signed golomb rice code (flac). | |
1812 | 328 */ |
329 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){ | |
330 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len); | |
331 return (v>>1) ^ -(v&1); | |
332 } | |
333 | |
2525
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 * read unsigned golomb rice code (shorten). |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
336 */ |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
337 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){ |
2979 | 338 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
|
339 } |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
340 |
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 * read signed golomb rice code (shorten). |
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 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
|
345 { |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
346 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
|
347 if (uvar & 1) |
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 else |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
350 return uvar >> 1; |
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 |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
353 |
b47af698085e
shorten decoder by (Jeff Muizelaar <jrmuizel gmail com>)
michael
parents:
2439
diff
changeset
|
354 |
1168 | 355 #ifdef TRACE |
356 | |
2438
e98b5e0de86b
compile with TRACE define patch by (Loic <lll+ffmpeg m4x org>)
michael
parents:
2220
diff
changeset
|
357 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){ |
1168 | 358 int show= show_bits(s, 24); |
359 int pos= get_bits_count(s); | |
360 int i= get_ue_golomb(s); | |
361 int len= get_bits_count(s) - pos; | |
362 int bits= show>>(24-len); | |
2967 | 363 |
1168 | 364 print_bin(bits, len); |
2967 | 365 |
2215 | 366 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); |
2967 | 367 |
1168 | 368 return i; |
369 } | |
370 | |
2438
e98b5e0de86b
compile with TRACE define patch by (Loic <lll+ffmpeg m4x org>)
michael
parents:
2220
diff
changeset
|
371 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){ |
1168 | 372 int show= show_bits(s, 24); |
373 int pos= get_bits_count(s); | |
374 int i= get_se_golomb(s); | |
375 int len= get_bits_count(s) - pos; | |
376 int bits= show>>(24-len); | |
2967 | 377 |
1168 | 378 print_bin(bits, len); |
2967 | 379 |
2215 | 380 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); |
2967 | 381 |
1168 | 382 return i; |
383 } | |
384 | |
2438
e98b5e0de86b
compile with TRACE define patch by (Loic <lll+ffmpeg m4x org>)
michael
parents:
2220
diff
changeset
|
385 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){ |
1168 | 386 int show= show_bits(s, 24); |
387 int pos= get_bits_count(s); | |
388 int i= get_te0_golomb(s, r); | |
389 int len= get_bits_count(s) - pos; | |
390 int bits= show>>(24-len); | |
2967 | 391 |
1168 | 392 print_bin(bits, len); |
2967 | 393 |
2215 | 394 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line); |
2967 | 395 |
1168 | 396 return i; |
397 } | |
398 | |
399 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
400 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
401 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
402 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__) | |
403 | |
404 #endif | |
405 | |
406 /** | |
407 * write unsigned exp golomb code. | |
408 */ | |
409 static inline void set_ue_golomb(PutBitContext *pb, int i){ | |
410 int e; | |
2967 | 411 |
1168 | 412 assert(i>=0); |
413 | |
414 #if 0 | |
415 if(i=0){ | |
416 put_bits(pb, 1, 1); | |
417 return; | |
418 } | |
419 #endif | |
420 if(i<256) | |
421 put_bits(pb, ff_ue_golomb_len[i], i+1); | |
422 else{ | |
423 e= av_log2(i+1); | |
2967 | 424 |
1168 | 425 put_bits(pb, 2*e+1, i+1); |
426 } | |
427 } | |
428 | |
429 /** | |
430 * write truncated unsigned exp golomb code. | |
431 */ | |
432 static inline void set_te_golomb(PutBitContext *pb, int i, int range){ | |
433 assert(range >= 1); | |
434 assert(i<=range); | |
435 | |
1169 | 436 if(range==2) put_bits(pb, 1, i^1); |
1168 | 437 else set_ue_golomb(pb, i); |
438 } | |
439 | |
440 /** | |
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
|
441 * write signed exp golomb code. 16 bits at most. |
1168 | 442 */ |
443 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
|
444 // if (i>32767 || i<-32767) |
2979 | 445 // av_log(NULL,AV_LOG_ERROR,"value out of range %d\n", i); |
2967 | 446 #if 0 |
1168 | 447 if(i<=0) i= -2*i; |
448 else i= 2*i-1; | |
449 #elif 1 | |
450 i= 2*i-1; | |
451 if(i<0) i^= -1; //FIXME check if gcc does the right thing | |
452 #else | |
453 i= 2*i-1; | |
454 i^= (i>>31); | |
455 #endif | |
456 set_ue_golomb(pb, i); | |
457 } | |
1306 | 458 |
459 /** | |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
460 * write unsigned golomb rice code (ffv1). |
1306 | 461 */ |
462 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){ | |
463 int e; | |
2967 | 464 |
1306 | 465 assert(i>=0); |
2967 | 466 |
1306 | 467 e= i>>k; |
468 if(e<limit){ | |
469 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1))); | |
470 }else{ | |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
471 put_bits(pb, limit + esc_len, i - limit + 1); |
1306 | 472 } |
473 } | |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
474 |
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 * write unsigned golomb rice code (jpegls). |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
477 */ |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
478 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
|
479 int e; |
2967 | 480 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
481 assert(i>=0); |
2967 | 482 |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
483 e= (i>>k) + 1; |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
484 if(e<limit){ |
3353
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
3036
diff
changeset
|
485 while(e > 31) { |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
3036
diff
changeset
|
486 put_bits(pb, 31, 0); |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
3036
diff
changeset
|
487 e -= 31; |
5b901881d6ed
first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder
michael
parents:
3036
diff
changeset
|
488 } |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
489 put_bits(pb, e, 1); |
1362 | 490 if(k) |
7260
3ec34b551aae
bitstream: move put_sbits() from flacenc.c to bitstream.h and use it
ramiro
parents:
5830
diff
changeset
|
491 put_sbits(pb, k, i); |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
492 }else{ |
4053 | 493 while(limit > 31) { |
494 put_bits(pb, 31, 0); | |
495 limit -= 31; | |
496 } | |
1361
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
497 put_bits(pb, limit , 1); |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
498 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
|
499 } |
8479b875a989
golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)
michaelni
parents:
1306
diff
changeset
|
500 } |
2215 | 501 |
502 /** | |
503 * write signed golomb rice code (ffv1). | |
504 */ | |
2220 | 505 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){ |
2215 | 506 int v; |
507 | |
508 v = -2*i-1; | |
509 v ^= (v>>31); | |
510 | |
511 set_ur_golomb(pb, v, k, limit, esc_len); | |
512 } | |
513 | |
514 /** | |
515 * write signed golomb rice code (flac). | |
516 */ | |
517 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){ | |
518 int v; | |
519 | |
520 v = -2*i-1; | |
521 v ^= (v>>31); | |
522 | |
523 set_ur_golomb_jpegls(pb, v, k, limit, esc_len); | |
524 } | |
5163 | 525 |
7760 | 526 #endif /* AVCODEC_GOLOMB_H */ |