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<0x8000000U && HAVE_BITS_REMAINING(re, gb));
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 if (gb->size_in_bits <= re_index)
00306 return -1;
00307 LAST_SKIP_BITS(re, gb, 1);
00308 UPDATE_CACHE(re, gb);
00309 }
00310 SKIP_BITS(re, gb, 1);
00311
00312 if(i < limit - 1){
00313 if(k){
00314 buf = SHOW_UBITS(re, gb, k);
00315 LAST_SKIP_BITS(re, gb, k);
00316 }else{
00317 buf=0;
00318 }
00319
00320 CLOSE_READER(re, gb);
00321 return buf + (i<<k);
00322 }else if(i == limit - 1){
00323 buf = SHOW_UBITS(re, gb, esc_len);
00324 LAST_SKIP_BITS(re, gb, esc_len);
00325 CLOSE_READER(re, gb);
00326
00327 return buf + 1;
00328 }else
00329 return -1;
00330 }
00331 }
00332
00336 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
00337 int v= get_ur_golomb(gb, k, limit, esc_len);
00338
00339 v++;
00340 if (v&1) return v>>1;
00341 else return -(v>>1);
00342
00343
00344 }
00345
00349 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
00350 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
00351 return (v>>1) ^ -(v&1);
00352 }
00353
00357 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
00358 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
00359 }
00360
00364 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
00365 {
00366 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
00367 if (uvar & 1)
00368 return ~(uvar >> 1);
00369 else
00370 return uvar >> 1;
00371 }
00372
00373
00374
00375 #ifdef TRACE
00376
00377 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
00378 int show= show_bits(s, 24);
00379 int pos= get_bits_count(s);
00380 int i= get_ue_golomb(s);
00381 int len= get_bits_count(s) - pos;
00382 int bits= show>>(24-len);
00383
00384 print_bin(bits, len);
00385
00386 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00387
00388 return i;
00389 }
00390
00391 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
00392 int show= show_bits(s, 24);
00393 int pos= get_bits_count(s);
00394 int i= get_se_golomb(s);
00395 int len= get_bits_count(s) - pos;
00396 int bits= show>>(24-len);
00397
00398 print_bin(bits, len);
00399
00400 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00401
00402 return i;
00403 }
00404
00405 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
00406 int show= show_bits(s, 24);
00407 int pos= get_bits_count(s);
00408 int i= get_te0_golomb(s, r);
00409 int len= get_bits_count(s) - pos;
00410 int bits= show>>(24-len);
00411
00412 print_bin(bits, len);
00413
00414 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00415
00416 return i;
00417 }
00418
00419 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00420 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00421 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00422 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00423
00424 #endif
00425
00429 static inline void set_ue_golomb(PutBitContext *pb, int i){
00430 int e;
00431
00432 assert(i>=0);
00433
00434 #if 0
00435 if(i=0){
00436 put_bits(pb, 1, 1);
00437 return;
00438 }
00439 #endif
00440 if(i<256)
00441 put_bits(pb, ff_ue_golomb_len[i], i+1);
00442 else{
00443 e= av_log2(i+1);
00444
00445 put_bits(pb, 2*e+1, i+1);
00446 }
00447 }
00448
00452 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
00453 assert(range >= 1);
00454 assert(i<=range);
00455
00456 if(range==2) put_bits(pb, 1, i^1);
00457 else set_ue_golomb(pb, i);
00458 }
00459
00463 static inline void set_se_golomb(PutBitContext *pb, int i){
00464
00465
00466 #if 0
00467 if(i<=0) i= -2*i;
00468 else i= 2*i-1;
00469 #elif 1
00470 i= 2*i-1;
00471 if(i<0) i^= -1;
00472 #else
00473 i= 2*i-1;
00474 i^= (i>>31);
00475 #endif
00476 set_ue_golomb(pb, i);
00477 }
00478
00482 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
00483 int e;
00484
00485 assert(i>=0);
00486
00487 e= i>>k;
00488 if(e<limit){
00489 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
00490 }else{
00491 put_bits(pb, limit + esc_len, i - limit + 1);
00492 }
00493 }
00494
00498 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
00499 int e;
00500
00501 assert(i>=0);
00502
00503 e= (i>>k) + 1;
00504 if(e<limit){
00505 while(e > 31) {
00506 put_bits(pb, 31, 0);
00507 e -= 31;
00508 }
00509 put_bits(pb, e, 1);
00510 if(k)
00511 put_sbits(pb, k, i);
00512 }else{
00513 while(limit > 31) {
00514 put_bits(pb, 31, 0);
00515 limit -= 31;
00516 }
00517 put_bits(pb, limit , 1);
00518 put_bits(pb, esc_len, i - 1);
00519 }
00520 }
00521
00525 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
00526 int v;
00527
00528 v = -2*i-1;
00529 v ^= (v>>31);
00530
00531 set_ur_golomb(pb, v, k, limit, esc_len);
00532 }
00533
00537 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
00538 int v;
00539
00540 v = -2*i-1;
00541 v ^= (v>>31);
00542
00543 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
00544 }
00545
00546 #endif