00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00030 #ifndef AVCODEC_GOLOMB_H
00031 #define AVCODEC_GOLOMB_H
00032
00033 #include <stdint.h>
00034 #include "get_bits.h"
00035 #include "put_bits.h"
00036
00037 #define INVALID_VLC 0x80000000
00038
00039 extern const uint8_t ff_golomb_vlc_len[512];
00040 extern const uint8_t ff_ue_golomb_vlc_code[512];
00041 extern const int8_t ff_se_golomb_vlc_code[512];
00042 extern const uint8_t ff_ue_golomb_len[256];
00043
00044 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
00045 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
00046 extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
00047 extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
00048
00049
00053 static inline int get_ue_golomb(GetBitContext *gb){
00054 unsigned int buf;
00055 int log;
00056
00057 OPEN_READER(re, gb);
00058 UPDATE_CACHE(re, gb);
00059 buf=GET_CACHE(re, gb);
00060
00061 if(buf >= (1<<27)){
00062 buf >>= 32 - 9;
00063 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
00064 CLOSE_READER(re, gb);
00065
00066 return ff_ue_golomb_vlc_code[buf];
00067 }else{
00068 log= 2*av_log2(buf) - 31;
00069 buf>>= log;
00070 buf--;
00071 LAST_SKIP_BITS(re, gb, 32 - log);
00072 CLOSE_READER(re, gb);
00073
00074 return buf;
00075 }
00076 }
00077
00081 static inline unsigned get_ue_golomb_long(GetBitContext *gb)
00082 {
00083 unsigned buf, log;
00084
00085 buf = show_bits_long(gb, 32);
00086 log = 31 - av_log2(buf);
00087 skip_bits_long(gb, log);
00088
00089 return get_bits_long(gb, log + 1) - 1;
00090 }
00091
00096 static inline int get_ue_golomb_31(GetBitContext *gb){
00097 unsigned int buf;
00098
00099 OPEN_READER(re, gb);
00100 UPDATE_CACHE(re, gb);
00101 buf=GET_CACHE(re, gb);
00102
00103 buf >>= 32 - 9;
00104 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
00105 CLOSE_READER(re, gb);
00106
00107 return ff_ue_golomb_vlc_code[buf];
00108 }
00109
00110 static inline int svq3_get_ue_golomb(GetBitContext *gb){
00111 uint32_t buf;
00112
00113 OPEN_READER(re, gb);
00114 UPDATE_CACHE(re, gb);
00115 buf=GET_CACHE(re, gb);
00116
00117 if(buf&0xAA800000){
00118 buf >>= 32 - 8;
00119 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
00120 CLOSE_READER(re, gb);
00121
00122 return ff_interleaved_ue_golomb_vlc_code[buf];
00123 }else{
00124 int ret = 1;
00125
00126 do {
00127 buf >>= 32 - 8;
00128 LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
00129
00130 if (ff_interleaved_golomb_vlc_len[buf] != 9){
00131 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
00132 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
00133 break;
00134 }
00135 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
00136 UPDATE_CACHE(re, gb);
00137 buf = GET_CACHE(re, gb);
00138 } while (ret);
00139
00140 CLOSE_READER(re, gb);
00141 return ret - 1;
00142 }
00143 }
00144
00148 static inline int get_te0_golomb(GetBitContext *gb, int range){
00149 assert(range >= 1);
00150
00151 if(range==1) return 0;
00152 else if(range==2) return get_bits1(gb)^1;
00153 else return get_ue_golomb(gb);
00154 }
00155
00159 static inline int get_te_golomb(GetBitContext *gb, int range){
00160 assert(range >= 1);
00161
00162 if(range==2) return get_bits1(gb)^1;
00163 else return get_ue_golomb(gb);
00164 }
00165
00166
00170 static inline int get_se_golomb(GetBitContext *gb){
00171 unsigned int buf;
00172 int log;
00173
00174 OPEN_READER(re, gb);
00175 UPDATE_CACHE(re, gb);
00176 buf=GET_CACHE(re, gb);
00177
00178 if(buf >= (1<<27)){
00179 buf >>= 32 - 9;
00180 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
00181 CLOSE_READER(re, gb);
00182
00183 return ff_se_golomb_vlc_code[buf];
00184 }else{
00185 log= 2*av_log2(buf) - 31;
00186 buf>>= log;
00187
00188 LAST_SKIP_BITS(re, gb, 32 - log);
00189 CLOSE_READER(re, gb);
00190
00191 if(buf&1) buf= -(buf>>1);
00192 else buf= (buf>>1);
00193
00194 return buf;
00195 }
00196 }
00197
00198 static inline int svq3_get_se_golomb(GetBitContext *gb){
00199 unsigned int buf;
00200 int log;
00201
00202 OPEN_READER(re, gb);
00203 UPDATE_CACHE(re, gb);
00204 buf=GET_CACHE(re, gb);
00205
00206 if(buf&0xAA800000){
00207 buf >>= 32 - 8;
00208 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
00209 CLOSE_READER(re, gb);
00210
00211 return ff_interleaved_se_golomb_vlc_code[buf];
00212 }else{
00213 LAST_SKIP_BITS(re, gb, 8);
00214 UPDATE_CACHE(re, gb);
00215 buf |= 1 | (GET_CACHE(re, gb) >> 8);
00216
00217 if((buf & 0xAAAAAAAA) == 0)
00218 return INVALID_VLC;
00219
00220 for(log=31; (buf & 0x80000000) == 0; log--){
00221 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
00222 }
00223
00224 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
00225 CLOSE_READER(re, gb);
00226
00227 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
00228 }
00229 }
00230
00231 static inline int dirac_get_se_golomb(GetBitContext *gb){
00232 uint32_t buf;
00233 uint32_t ret;
00234
00235 ret = svq3_get_ue_golomb(gb);
00236
00237 if (ret) {
00238 OPEN_READER(re, gb);
00239 UPDATE_CACHE(re, gb);
00240 buf = SHOW_SBITS(re, gb, 1);
00241 LAST_SKIP_BITS(re, gb, 1);
00242 ret = (ret ^ buf) - buf;
00243 CLOSE_READER(re, gb);
00244 }
00245
00246 return ret;
00247 }
00248
00252 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
00253 unsigned int buf;
00254 int log;
00255
00256 OPEN_READER(re, gb);
00257 UPDATE_CACHE(re, gb);
00258 buf=GET_CACHE(re, gb);
00259
00260 log= av_log2(buf);
00261
00262 if(log > 31-limit){
00263 buf >>= log - k;
00264 buf += (30-log)<<k;
00265 LAST_SKIP_BITS(re, gb, 32 + k - log);
00266 CLOSE_READER(re, gb);
00267
00268 return buf;
00269 }else{
00270 LAST_SKIP_BITS(re, gb, limit);
00271 UPDATE_CACHE(re, gb);
00272
00273 buf = SHOW_UBITS(re, gb, esc_len);
00274
00275 LAST_SKIP_BITS(re, gb, esc_len);
00276 CLOSE_READER(re, gb);
00277
00278 return buf + limit - 1;
00279 }
00280 }
00281
00285 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
00286 unsigned int buf;
00287 int log;
00288
00289 OPEN_READER(re, gb);
00290 UPDATE_CACHE(re, gb);
00291 buf=GET_CACHE(re, gb);
00292
00293 log= av_log2(buf);
00294
00295 if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
00296 buf >>= log - k;
00297 buf += (30-log)<<k;
00298 LAST_SKIP_BITS(re, gb, 32 + k - log);
00299 CLOSE_READER(re, gb);
00300
00301 return buf;
00302 }else{
00303 int i;
00304 for (i = 0; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
00305 LAST_SKIP_BITS(re, gb, 1);
00306 UPDATE_CACHE(re, gb);
00307 }
00308 SKIP_BITS(re, gb, 1);
00309
00310 if(i < limit - 1){
00311 if(k){
00312 buf = SHOW_UBITS(re, gb, k);
00313 LAST_SKIP_BITS(re, gb, k);
00314 }else{
00315 buf=0;
00316 }
00317
00318 CLOSE_READER(re, gb);
00319 return buf + (i<<k);
00320 }else if(i == limit - 1){
00321 buf = SHOW_UBITS(re, gb, esc_len);
00322 LAST_SKIP_BITS(re, gb, esc_len);
00323 CLOSE_READER(re, gb);
00324
00325 return buf + 1;
00326 }else
00327 return -1;
00328 }
00329 }
00330
00334 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
00335 int v= get_ur_golomb(gb, k, limit, esc_len);
00336
00337 v++;
00338 if (v&1) return v>>1;
00339 else return -(v>>1);
00340
00341
00342 }
00343
00347 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
00348 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
00349 return (v>>1) ^ -(v&1);
00350 }
00351
00355 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
00356 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
00357 }
00358
00362 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
00363 {
00364 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
00365 if (uvar & 1)
00366 return ~(uvar >> 1);
00367 else
00368 return uvar >> 1;
00369 }
00370
00371
00372
00373 #ifdef TRACE
00374
00375 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
00376 int show= show_bits(s, 24);
00377 int pos= get_bits_count(s);
00378 int i= get_ue_golomb(s);
00379 int len= get_bits_count(s) - pos;
00380 int bits= show>>(24-len);
00381
00382 print_bin(bits, len);
00383
00384 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00385
00386 return i;
00387 }
00388
00389 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
00390 int show= show_bits(s, 24);
00391 int pos= get_bits_count(s);
00392 int i= get_se_golomb(s);
00393 int len= get_bits_count(s) - pos;
00394 int bits= show>>(24-len);
00395
00396 print_bin(bits, len);
00397
00398 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00399
00400 return i;
00401 }
00402
00403 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
00404 int show= show_bits(s, 24);
00405 int pos= get_bits_count(s);
00406 int i= get_te0_golomb(s, r);
00407 int len= get_bits_count(s) - pos;
00408 int bits= show>>(24-len);
00409
00410 print_bin(bits, len);
00411
00412 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00413
00414 return i;
00415 }
00416
00417 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00418 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00419 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00420 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00421
00422 #endif
00423
00427 static inline void set_ue_golomb(PutBitContext *pb, int i){
00428 int e;
00429
00430 assert(i>=0);
00431
00432 #if 0
00433 if(i=0){
00434 put_bits(pb, 1, 1);
00435 return;
00436 }
00437 #endif
00438 if(i<256)
00439 put_bits(pb, ff_ue_golomb_len[i], i+1);
00440 else{
00441 e= av_log2(i+1);
00442
00443 put_bits(pb, 2*e+1, i+1);
00444 }
00445 }
00446
00450 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
00451 assert(range >= 1);
00452 assert(i<=range);
00453
00454 if(range==2) put_bits(pb, 1, i^1);
00455 else set_ue_golomb(pb, i);
00456 }
00457
00461 static inline void set_se_golomb(PutBitContext *pb, int i){
00462
00463
00464 #if 0
00465 if(i<=0) i= -2*i;
00466 else i= 2*i-1;
00467 #elif 1
00468 i= 2*i-1;
00469 if(i<0) i^= -1;
00470 #else
00471 i= 2*i-1;
00472 i^= (i>>31);
00473 #endif
00474 set_ue_golomb(pb, i);
00475 }
00476
00480 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
00481 int e;
00482
00483 assert(i>=0);
00484
00485 e= i>>k;
00486 if(e<limit){
00487 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
00488 }else{
00489 put_bits(pb, limit + esc_len, i - limit + 1);
00490 }
00491 }
00492
00496 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
00497 int e;
00498
00499 assert(i>=0);
00500
00501 e= (i>>k) + 1;
00502 if(e<limit){
00503 while(e > 31) {
00504 put_bits(pb, 31, 0);
00505 e -= 31;
00506 }
00507 put_bits(pb, e, 1);
00508 if(k)
00509 put_sbits(pb, k, i);
00510 }else{
00511 while(limit > 31) {
00512 put_bits(pb, 31, 0);
00513 limit -= 31;
00514 }
00515 put_bits(pb, limit , 1);
00516 put_bits(pb, esc_len, i - 1);
00517 }
00518 }
00519
00523 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
00524 int v;
00525
00526 v = -2*i-1;
00527 v ^= (v>>31);
00528
00529 set_ur_golomb(pb, v, k, limit, esc_len);
00530 }
00531
00535 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
00536 int v;
00537
00538 v = -2*i-1;
00539 v ^= (v>>31);
00540
00541 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
00542 }
00543
00544 #endif