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
00082 static inline int get_ue_golomb_31(GetBitContext *gb){
00083 unsigned int buf;
00084
00085 OPEN_READER(re, gb);
00086 UPDATE_CACHE(re, gb);
00087 buf=GET_CACHE(re, gb);
00088
00089 buf >>= 32 - 9;
00090 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
00091 CLOSE_READER(re, gb);
00092
00093 return ff_ue_golomb_vlc_code[buf];
00094 }
00095
00096 static inline int svq3_get_ue_golomb(GetBitContext *gb){
00097 uint32_t buf;
00098
00099 OPEN_READER(re, gb);
00100 UPDATE_CACHE(re, gb);
00101 buf=GET_CACHE(re, gb);
00102
00103 if(buf&0xAA800000){
00104 buf >>= 32 - 8;
00105 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
00106 CLOSE_READER(re, gb);
00107
00108 return ff_interleaved_ue_golomb_vlc_code[buf];
00109 }else{
00110 int ret = 1;
00111
00112 while (1) {
00113 buf >>= 32 - 8;
00114 LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
00115
00116 if (ff_interleaved_golomb_vlc_len[buf] != 9){
00117 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
00118 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
00119 break;
00120 }
00121 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
00122 UPDATE_CACHE(re, gb);
00123 buf = GET_CACHE(re, gb);
00124 }
00125
00126 CLOSE_READER(re, gb);
00127 return ret - 1;
00128 }
00129 }
00130
00134 static inline int get_te0_golomb(GetBitContext *gb, int range){
00135 assert(range >= 1);
00136
00137 if(range==1) return 0;
00138 else if(range==2) return get_bits1(gb)^1;
00139 else return get_ue_golomb(gb);
00140 }
00141
00145 static inline int get_te_golomb(GetBitContext *gb, int range){
00146 assert(range >= 1);
00147
00148 if(range==2) return get_bits1(gb)^1;
00149 else return get_ue_golomb(gb);
00150 }
00151
00152
00156 static inline int get_se_golomb(GetBitContext *gb){
00157 unsigned int buf;
00158 int log;
00159
00160 OPEN_READER(re, gb);
00161 UPDATE_CACHE(re, gb);
00162 buf=GET_CACHE(re, gb);
00163
00164 if(buf >= (1<<27)){
00165 buf >>= 32 - 9;
00166 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
00167 CLOSE_READER(re, gb);
00168
00169 return ff_se_golomb_vlc_code[buf];
00170 }else{
00171 log= 2*av_log2(buf) - 31;
00172 buf>>= log;
00173
00174 LAST_SKIP_BITS(re, gb, 32 - log);
00175 CLOSE_READER(re, gb);
00176
00177 if(buf&1) buf= -(buf>>1);
00178 else buf= (buf>>1);
00179
00180 return buf;
00181 }
00182 }
00183
00184 static inline int svq3_get_se_golomb(GetBitContext *gb){
00185 unsigned int buf;
00186 int log;
00187
00188 OPEN_READER(re, gb);
00189 UPDATE_CACHE(re, gb);
00190 buf=GET_CACHE(re, gb);
00191
00192 if(buf&0xAA800000){
00193 buf >>= 32 - 8;
00194 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
00195 CLOSE_READER(re, gb);
00196
00197 return ff_interleaved_se_golomb_vlc_code[buf];
00198 }else{
00199 LAST_SKIP_BITS(re, gb, 8);
00200 UPDATE_CACHE(re, gb);
00201 buf |= 1 | (GET_CACHE(re, gb) >> 8);
00202
00203 if((buf & 0xAAAAAAAA) == 0)
00204 return INVALID_VLC;
00205
00206 for(log=31; (buf & 0x80000000) == 0; log--){
00207 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
00208 }
00209
00210 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
00211 CLOSE_READER(re, gb);
00212
00213 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
00214 }
00215 }
00216
00217 static inline int dirac_get_se_golomb(GetBitContext *gb){
00218 uint32_t buf;
00219 uint32_t ret;
00220
00221 ret = svq3_get_ue_golomb(gb);
00222
00223 if (ret) {
00224 OPEN_READER(re, gb);
00225 UPDATE_CACHE(re, gb);
00226 buf = SHOW_SBITS(re, gb, 1);
00227 LAST_SKIP_BITS(re, gb, 1);
00228 ret = (ret ^ buf) - buf;
00229 CLOSE_READER(re, gb);
00230 }
00231
00232 return ret;
00233 }
00234
00238 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
00239 unsigned int buf;
00240 int log;
00241
00242 OPEN_READER(re, gb);
00243 UPDATE_CACHE(re, gb);
00244 buf=GET_CACHE(re, gb);
00245
00246 log= av_log2(buf);
00247
00248 if(log > 31-limit){
00249 buf >>= log - k;
00250 buf += (30-log)<<k;
00251 LAST_SKIP_BITS(re, gb, 32 + k - log);
00252 CLOSE_READER(re, gb);
00253
00254 return buf;
00255 }else{
00256 LAST_SKIP_BITS(re, gb, limit);
00257 UPDATE_CACHE(re, gb);
00258
00259 buf = SHOW_UBITS(re, gb, esc_len);
00260
00261 LAST_SKIP_BITS(re, gb, esc_len);
00262 CLOSE_READER(re, gb);
00263
00264 return buf + limit - 1;
00265 }
00266 }
00267
00271 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
00272 unsigned int buf;
00273 int log;
00274
00275 OPEN_READER(re, gb);
00276 UPDATE_CACHE(re, gb);
00277 buf=GET_CACHE(re, gb);
00278
00279 log= av_log2(buf);
00280
00281 if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
00282 buf >>= log - k;
00283 buf += (30-log)<<k;
00284 LAST_SKIP_BITS(re, gb, 32 + k - log);
00285 CLOSE_READER(re, gb);
00286
00287 return buf;
00288 }else{
00289 int i;
00290 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
00291 LAST_SKIP_BITS(re, gb, 1);
00292 UPDATE_CACHE(re, gb);
00293 }
00294 SKIP_BITS(re, gb, 1);
00295
00296 if(i < limit - 1){
00297 if(k){
00298 buf = SHOW_UBITS(re, gb, k);
00299 LAST_SKIP_BITS(re, gb, k);
00300 }else{
00301 buf=0;
00302 }
00303
00304 CLOSE_READER(re, gb);
00305 return buf + (i<<k);
00306 }else if(i == limit - 1){
00307 buf = SHOW_UBITS(re, gb, esc_len);
00308 LAST_SKIP_BITS(re, gb, esc_len);
00309 CLOSE_READER(re, gb);
00310
00311 return buf + 1;
00312 }else
00313 return -1;
00314 }
00315 }
00316
00320 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
00321 int v= get_ur_golomb(gb, k, limit, esc_len);
00322
00323 v++;
00324 if (v&1) return v>>1;
00325 else return -(v>>1);
00326
00327
00328 }
00329
00333 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
00334 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
00335 return (v>>1) ^ -(v&1);
00336 }
00337
00341 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
00342 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
00343 }
00344
00348 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
00349 {
00350 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
00351 if (uvar & 1)
00352 return ~(uvar >> 1);
00353 else
00354 return uvar >> 1;
00355 }
00356
00357
00358
00359 #ifdef TRACE
00360
00361 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
00362 int show= show_bits(s, 24);
00363 int pos= get_bits_count(s);
00364 int i= get_ue_golomb(s);
00365 int len= get_bits_count(s) - pos;
00366 int bits= show>>(24-len);
00367
00368 print_bin(bits, len);
00369
00370 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00371
00372 return i;
00373 }
00374
00375 static inline int get_se(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_se_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 se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00385
00386 return i;
00387 }
00388
00389 static inline int get_te(GetBitContext *s, int r, 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_te0_golomb(s, r);
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 te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00399
00400 return i;
00401 }
00402
00403 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00404 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00405 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00406 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00407
00408 #endif
00409
00413 static inline void set_ue_golomb(PutBitContext *pb, int i){
00414 int e;
00415
00416 assert(i>=0);
00417
00418 #if 0
00419 if(i=0){
00420 put_bits(pb, 1, 1);
00421 return;
00422 }
00423 #endif
00424 if(i<256)
00425 put_bits(pb, ff_ue_golomb_len[i], i+1);
00426 else{
00427 e= av_log2(i+1);
00428
00429 put_bits(pb, 2*e+1, i+1);
00430 }
00431 }
00432
00436 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
00437 assert(range >= 1);
00438 assert(i<=range);
00439
00440 if(range==2) put_bits(pb, 1, i^1);
00441 else set_ue_golomb(pb, i);
00442 }
00443
00447 static inline void set_se_golomb(PutBitContext *pb, int i){
00448
00449
00450 #if 0
00451 if(i<=0) i= -2*i;
00452 else i= 2*i-1;
00453 #elif 1
00454 i= 2*i-1;
00455 if(i<0) i^= -1;
00456 #else
00457 i= 2*i-1;
00458 i^= (i>>31);
00459 #endif
00460 set_ue_golomb(pb, i);
00461 }
00462
00466 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
00467 int e;
00468
00469 assert(i>=0);
00470
00471 e= i>>k;
00472 if(e<limit){
00473 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
00474 }else{
00475 put_bits(pb, limit + esc_len, i - limit + 1);
00476 }
00477 }
00478
00482 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
00483 int e;
00484
00485 assert(i>=0);
00486
00487 e= (i>>k) + 1;
00488 if(e<limit){
00489 while(e > 31) {
00490 put_bits(pb, 31, 0);
00491 e -= 31;
00492 }
00493 put_bits(pb, e, 1);
00494 if(k)
00495 put_sbits(pb, k, i);
00496 }else{
00497 while(limit > 31) {
00498 put_bits(pb, 31, 0);
00499 limit -= 31;
00500 }
00501 put_bits(pb, limit , 1);
00502 put_bits(pb, esc_len, i - 1);
00503 }
00504 }
00505
00509 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
00510 int v;
00511
00512 v = -2*i-1;
00513 v ^= (v>>31);
00514
00515 set_ur_golomb(pb, v, k, limit, esc_len);
00516 }
00517
00521 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
00522 int v;
00523
00524 v = -2*i-1;
00525 v ^= (v>>31);
00526
00527 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
00528 }
00529
00530 #endif