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