view libavcodec/golomb.h @ 2:897f711a7157

rearrange to work with autoconf
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Tue, 25 Sep 2012 15:55:33 +0200
parents
children
line source
1 /*
2 * exp golomb vlc stuff
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Alex Beregszaszi
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
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
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 /**
24 * @file
25 * @brief
26 * exp golomb vlc stuff
27 * @author Michael Niedermayer <michaelni@gmx.at> and Alex Beregszaszi
28 */
30 #ifndef AVCODEC_GOLOMB_H
31 #define AVCODEC_GOLOMB_H
33 #include <stdint.h>
34 #include "get_bits.h"
36 #define INVALID_VLC 0x80000000
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];
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];
46 extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
49 /**
50 * read unsigned exp golomb code.
51 */
52 static inline int get_ue_golomb(GetBitContext *gb){
53 unsigned int buf;
54 int log;
56 OPEN_READER(re, gb);
57 UPDATE_CACHE(re, gb);
58 buf=GET_CACHE(re, gb);
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);
65 return ff_ue_golomb_vlc_code[buf];
66 }else{
67 log= 2*av_log2_c(buf) - 31;
68 buf>>= log;
69 buf--;
70 LAST_SKIP_BITS(re, gb, 32 - log);
71 CLOSE_READER(re, gb);
73 return buf;
74 }
75 }
77 /**
78 * read unsigned exp golomb code, constraint to a max of 31.
79 * the return value is undefined if the stored value exceeds 31.
80 */
81 static inline int get_ue_golomb_31(GetBitContext *gb){
82 unsigned int buf;
84 OPEN_READER(re, gb);
85 UPDATE_CACHE(re, gb);
86 buf=GET_CACHE(re, gb);
88 buf >>= 32 - 9;
89 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
90 CLOSE_READER(re, gb);
92 return ff_ue_golomb_vlc_code[buf];
93 }
95 static inline int svq3_get_ue_golomb(GetBitContext *gb){
96 uint32_t buf;
98 OPEN_READER(re, gb);
99 UPDATE_CACHE(re, gb);
100 buf=GET_CACHE(re, gb);
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);
107 return ff_interleaved_ue_golomb_vlc_code[buf];
108 }else{
109 int ret = 1;
111 while (1) {
112 buf >>= 32 - 8;
113 LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
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);
123 }
125 CLOSE_READER(re, gb);
126 return ret - 1;
127 }
128 }
130 /**
131 * read unsigned truncated exp golomb code.
132 */
133 static inline int get_te0_golomb(GetBitContext *gb, int range){
134 assert(range >= 1);
136 if(range==1) return 0;
137 else if(range==2) return get_bits1(gb)^1;
138 else return get_ue_golomb(gb);
139 }
141 /**
142 * read unsigned truncated exp golomb code.
143 */
144 static inline int get_te_golomb(GetBitContext *gb, int range){
145 assert(range >= 1);
147 if(range==2) return get_bits1(gb)^1;
148 else return get_ue_golomb(gb);
149 }
152 /**
153 * read signed exp golomb code.
154 */
155 static inline int get_se_golomb(GetBitContext *gb){
156 unsigned int buf;
157 int log;
159 OPEN_READER(re, gb);
160 UPDATE_CACHE(re, gb);
161 buf=GET_CACHE(re, gb);
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);
168 return ff_se_golomb_vlc_code[buf];
169 }else{
170 log= 2*av_log2_c(buf) - 31;
171 buf>>= log;
173 LAST_SKIP_BITS(re, gb, 32 - log);
174 CLOSE_READER(re, gb);
176 if(buf&1) buf= -(buf>>1);
177 else buf= (buf>>1);
179 return buf;
180 }
181 }
183 static inline int svq3_get_se_golomb(GetBitContext *gb){
184 unsigned int buf;
185 int log;
187 OPEN_READER(re, gb);
188 UPDATE_CACHE(re, gb);
189 buf=GET_CACHE(re, gb);
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);
196 return ff_interleaved_se_golomb_vlc_code[buf];
197 }else{
198 LAST_SKIP_BITS(re, gb, 8);
199 UPDATE_CACHE(re, gb);
200 buf |= 1 | (GET_CACHE(re, gb) >> 8);
202 if((buf & 0xAAAAAAAA) == 0)
203 return INVALID_VLC;
205 for(log=31; (buf & 0x80000000) == 0; log--){
206 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
207 }
209 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
210 CLOSE_READER(re, gb);
212 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
213 }
214 }
216 static inline int dirac_get_se_golomb(GetBitContext *gb){
217 uint32_t buf;
218 uint32_t ret;
220 ret = svq3_get_ue_golomb(gb);
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 }
231 return ret;
232 }
234 /**
235 * read unsigned golomb rice code (ffv1).
236 */
237 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
238 unsigned int buf;
239 int log;
241 OPEN_READER(re, gb);
242 UPDATE_CACHE(re, gb);
243 buf=GET_CACHE(re, gb);
245 log= av_log2_c(buf);
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);
253 return buf;
254 }else{
255 LAST_SKIP_BITS(re, gb, limit);
256 UPDATE_CACHE(re, gb);
258 buf = SHOW_UBITS(re, gb, esc_len);
260 LAST_SKIP_BITS(re, gb, esc_len);
261 CLOSE_READER(re, gb);
263 return buf + limit - 1;
264 }
265 }
267 /**
268 * read unsigned golomb rice code (jpegls).
269 */
270 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
271 unsigned int buf;
272 int log;
274 OPEN_READER(re, gb);
275 UPDATE_CACHE(re, gb);
276 buf=GET_CACHE(re, gb);
278 log= av_log2_c(buf);
280 if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
281 buf >>= log - k;
282 buf += (30-log)<<k;
283 LAST_SKIP_BITS(re, gb, 32 + k - log);
284 CLOSE_READER(re, gb);
286 return buf;
287 }else{
288 int i;
289 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
290 LAST_SKIP_BITS(re, gb, 1);
291 UPDATE_CACHE(re, gb);
292 }
293 SKIP_BITS(re, gb, 1);
295 if(i < limit - 1){
296 if(k){
297 buf = SHOW_UBITS(re, gb, k);
298 LAST_SKIP_BITS(re, gb, k);
299 }else{
300 buf=0;
301 }
303 CLOSE_READER(re, gb);
304 return buf + (i<<k);
305 }else if(i == limit - 1){
306 buf = SHOW_UBITS(re, gb, esc_len);
307 LAST_SKIP_BITS(re, gb, esc_len);
308 CLOSE_READER(re, gb);
310 return buf + 1;
311 }else
312 return -1;
313 }
314 }
316 /**
317 * read signed golomb rice code (ffv1).
318 */
319 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
320 int v= get_ur_golomb(gb, k, limit, esc_len);
322 v++;
323 if (v&1) return v>>1;
324 else return -(v>>1);
326 // return (v>>1) ^ -(v&1);
327 }
329 /**
330 * read signed golomb rice code (flac).
331 */
332 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
333 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
334 return (v>>1) ^ -(v&1);
335 }
337 /**
338 * read unsigned golomb rice code (shorten).
339 */
340 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
341 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
342 }
344 /**
345 * read signed golomb rice code (shorten).
346 */
347 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
348 {
349 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
350 if (uvar & 1)
351 return ~(uvar >> 1);
352 else
353 return uvar >> 1;
354 }
358 #ifdef TRACE
360 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
361 int show= show_bits(s, 24);
362 int pos= get_bits_count(s);
363 int i= get_ue_golomb(s);
364 int len= get_bits_count(s) - pos;
365 int bits= show>>(24-len);
367 print_bin(bits, len);
369 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
371 return i;
372 }
374 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
375 int show= show_bits(s, 24);
376 int pos= get_bits_count(s);
377 int i= get_se_golomb(s);
378 int len= get_bits_count(s) - pos;
379 int bits= show>>(24-len);
381 print_bin(bits, len);
383 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
385 return i;
386 }
388 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
389 int show= show_bits(s, 24);
390 int pos= get_bits_count(s);
391 int i= get_te0_golomb(s, r);
392 int len= get_bits_count(s) - pos;
393 int bits= show>>(24-len);
395 print_bin(bits, len);
397 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
399 return i;
400 }
402 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
403 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
404 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
405 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
407 #endif
410 #endif /* AVCODEC_GOLOMB_H */