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 "bitstream.h"
00035
00036 #define INVALID_VLC 0x80000000
00037
00038 extern const uint8_t ff_golomb_vlc_len[512];
00039 extern const uint8_t ff_ue_golomb_vlc_code[512];
00040 extern const int8_t ff_se_golomb_vlc_code[512];
00041 extern const uint8_t ff_ue_golomb_len[256];
00042
00043 extern const uint8_t ff_interleaved_golomb_vlc_len[256];
00044 extern const uint8_t ff_interleaved_ue_golomb_vlc_code[256];
00045 extern const int8_t ff_interleaved_se_golomb_vlc_code[256];
00046 extern const uint8_t ff_interleaved_dirac_golomb_vlc_code[256];
00047
00048
00052 static inline int get_ue_golomb(GetBitContext *gb){
00053 unsigned int buf;
00054 int log;
00055
00056 OPEN_READER(re, gb);
00057 UPDATE_CACHE(re, gb);
00058 buf=GET_CACHE(re, gb);
00059
00060 if(buf >= (1<<27)){
00061 buf >>= 32 - 9;
00062 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
00063 CLOSE_READER(re, gb);
00064
00065 return ff_ue_golomb_vlc_code[buf];
00066 }else{
00067 log= 2*av_log2(buf) - 31;
00068 buf>>= log;
00069 buf--;
00070 LAST_SKIP_BITS(re, gb, 32 - log);
00071 CLOSE_READER(re, gb);
00072
00073 return buf;
00074 }
00075 }
00076
00081 static inline int get_ue_golomb_31(GetBitContext *gb){
00082 unsigned int buf;
00083
00084 OPEN_READER(re, gb);
00085 UPDATE_CACHE(re, gb);
00086 buf=GET_CACHE(re, gb);
00087
00088 buf >>= 32 - 9;
00089 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
00090 CLOSE_READER(re, gb);
00091
00092 return ff_ue_golomb_vlc_code[buf];
00093 }
00094
00095 static inline int svq3_get_ue_golomb(GetBitContext *gb){
00096 uint32_t buf;
00097
00098 OPEN_READER(re, gb);
00099 UPDATE_CACHE(re, gb);
00100 buf=GET_CACHE(re, gb);
00101
00102 if(buf&0xAA800000){
00103 buf >>= 32 - 8;
00104 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
00105 CLOSE_READER(re, gb);
00106
00107 return ff_interleaved_ue_golomb_vlc_code[buf];
00108 }else{
00109 int ret = 1;
00110
00111 while (1) {
00112 buf >>= 32 - 8;
00113 LAST_SKIP_BITS(re, gb, FFMIN(ff_interleaved_golomb_vlc_len[buf], 8));
00114
00115 if (ff_interleaved_golomb_vlc_len[buf] != 9){
00116 ret <<= (ff_interleaved_golomb_vlc_len[buf] - 1) >> 1;
00117 ret |= ff_interleaved_dirac_golomb_vlc_code[buf];
00118 break;
00119 }
00120 ret = (ret << 4) | ff_interleaved_dirac_golomb_vlc_code[buf];
00121 UPDATE_CACHE(re, gb);
00122 buf = GET_CACHE(re, gb);
00123 }
00124
00125 CLOSE_READER(re, gb);
00126 return ret - 1;
00127 }
00128 }
00129
00133 static inline int get_te0_golomb(GetBitContext *gb, int range){
00134 assert(range >= 1);
00135
00136 if(range==1) return 0;
00137 else if(range==2) return get_bits1(gb)^1;
00138 else return get_ue_golomb(gb);
00139 }
00140
00144 static inline int get_te_golomb(GetBitContext *gb, int range){
00145 assert(range >= 1);
00146
00147 if(range==2) return get_bits1(gb)^1;
00148 else return get_ue_golomb(gb);
00149 }
00150
00151
00155 static inline int get_se_golomb(GetBitContext *gb){
00156 unsigned int buf;
00157 int log;
00158
00159 OPEN_READER(re, gb);
00160 UPDATE_CACHE(re, gb);
00161 buf=GET_CACHE(re, gb);
00162
00163 if(buf >= (1<<27)){
00164 buf >>= 32 - 9;
00165 LAST_SKIP_BITS(re, gb, ff_golomb_vlc_len[buf]);
00166 CLOSE_READER(re, gb);
00167
00168 return ff_se_golomb_vlc_code[buf];
00169 }else{
00170 log= 2*av_log2(buf) - 31;
00171 buf>>= log;
00172
00173 LAST_SKIP_BITS(re, gb, 32 - log);
00174 CLOSE_READER(re, gb);
00175
00176 if(buf&1) buf= -(buf>>1);
00177 else buf= (buf>>1);
00178
00179 return buf;
00180 }
00181 }
00182
00183 static inline int svq3_get_se_golomb(GetBitContext *gb){
00184 unsigned int buf;
00185 int log;
00186
00187 OPEN_READER(re, gb);
00188 UPDATE_CACHE(re, gb);
00189 buf=GET_CACHE(re, gb);
00190
00191 if(buf&0xAA800000){
00192 buf >>= 32 - 8;
00193 LAST_SKIP_BITS(re, gb, ff_interleaved_golomb_vlc_len[buf]);
00194 CLOSE_READER(re, gb);
00195
00196 return ff_interleaved_se_golomb_vlc_code[buf];
00197 }else{
00198 LAST_SKIP_BITS(re, gb, 8);
00199 UPDATE_CACHE(re, gb);
00200 buf |= 1 | (GET_CACHE(re, gb) >> 8);
00201
00202 if((buf & 0xAAAAAAAA) == 0)
00203 return INVALID_VLC;
00204
00205 for(log=31; (buf & 0x80000000) == 0; log--){
00206 buf = (buf << 2) - ((buf << log) >> (log - 1)) + (buf >> 30);
00207 }
00208
00209 LAST_SKIP_BITS(re, gb, 63 - 2*log - 8);
00210 CLOSE_READER(re, gb);
00211
00212 return (signed) (((((buf << log) >> log) - 1) ^ -(buf & 0x1)) + 1) >> 1;
00213 }
00214 }
00215
00216 static inline int dirac_get_se_golomb(GetBitContext *gb){
00217 uint32_t buf;
00218 uint32_t ret;
00219
00220 ret = svq3_get_ue_golomb(gb);
00221
00222 if (ret) {
00223 OPEN_READER(re, gb);
00224 UPDATE_CACHE(re, gb);
00225 buf = SHOW_SBITS(re, gb, 1);
00226 LAST_SKIP_BITS(re, gb, 1);
00227 ret = (ret ^ buf) - buf;
00228 CLOSE_READER(re, gb);
00229 }
00230
00231 return ret;
00232 }
00233
00237 static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
00238 unsigned int buf;
00239 int log;
00240
00241 OPEN_READER(re, gb);
00242 UPDATE_CACHE(re, gb);
00243 buf=GET_CACHE(re, gb);
00244
00245 log= av_log2(buf);
00246
00247 if(log > 31-limit){
00248 buf >>= log - k;
00249 buf += (30-log)<<k;
00250 LAST_SKIP_BITS(re, gb, 32 + k - log);
00251 CLOSE_READER(re, gb);
00252
00253 return buf;
00254 }else{
00255 buf >>= 32 - limit - esc_len;
00256 LAST_SKIP_BITS(re, gb, esc_len + limit);
00257 CLOSE_READER(re, gb);
00258
00259 return buf + limit - 1;
00260 }
00261 }
00262
00266 static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
00267 unsigned int buf;
00268 int log;
00269
00270 OPEN_READER(re, gb);
00271 UPDATE_CACHE(re, gb);
00272 buf=GET_CACHE(re, gb);
00273
00274 log= av_log2(buf);
00275
00276 if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
00277 buf >>= log - k;
00278 buf += (30-log)<<k;
00279 LAST_SKIP_BITS(re, gb, 32 + k - log);
00280 CLOSE_READER(re, gb);
00281
00282 return buf;
00283 }else{
00284 int i;
00285 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
00286 LAST_SKIP_BITS(re, gb, 1);
00287 UPDATE_CACHE(re, gb);
00288 }
00289 SKIP_BITS(re, gb, 1);
00290
00291 if(i < limit - 1){
00292 if(k){
00293 buf = SHOW_UBITS(re, gb, k);
00294 LAST_SKIP_BITS(re, gb, k);
00295 }else{
00296 buf=0;
00297 }
00298
00299 CLOSE_READER(re, gb);
00300 return buf + (i<<k);
00301 }else if(i == limit - 1){
00302 buf = SHOW_UBITS(re, gb, esc_len);
00303 LAST_SKIP_BITS(re, gb, esc_len);
00304 CLOSE_READER(re, gb);
00305
00306 return buf + 1;
00307 }else
00308 return -1;
00309 }
00310 }
00311
00315 static inline int get_sr_golomb(GetBitContext *gb, int k, int limit, int esc_len){
00316 int v= get_ur_golomb(gb, k, limit, esc_len);
00317
00318 v++;
00319 if (v&1) return v>>1;
00320 else return -(v>>1);
00321
00322
00323 }
00324
00328 static inline int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len){
00329 int v= get_ur_golomb_jpegls(gb, k, limit, esc_len);
00330 return (v>>1) ^ -(v&1);
00331 }
00332
00336 static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
00337 return get_ur_golomb_jpegls(gb, k, INT_MAX, 0);
00338 }
00339
00343 static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
00344 {
00345 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
00346 if (uvar & 1)
00347 return ~(uvar >> 1);
00348 else
00349 return uvar >> 1;
00350 }
00351
00352
00353
00354 #ifdef TRACE
00355
00356 static inline int get_ue(GetBitContext *s, char *file, const char *func, int line){
00357 int show= show_bits(s, 24);
00358 int pos= get_bits_count(s);
00359 int i= get_ue_golomb(s);
00360 int len= get_bits_count(s) - pos;
00361 int bits= show>>(24-len);
00362
00363 print_bin(bits, len);
00364
00365 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d ue @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00366
00367 return i;
00368 }
00369
00370 static inline int get_se(GetBitContext *s, char *file, const char *func, int line){
00371 int show= show_bits(s, 24);
00372 int pos= get_bits_count(s);
00373 int i= get_se_golomb(s);
00374 int len= get_bits_count(s) - pos;
00375 int bits= show>>(24-len);
00376
00377 print_bin(bits, len);
00378
00379 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d se @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00380
00381 return i;
00382 }
00383
00384 static inline int get_te(GetBitContext *s, int r, char *file, const char *func, int line){
00385 int show= show_bits(s, 24);
00386 int pos= get_bits_count(s);
00387 int i= get_te0_golomb(s, r);
00388 int len= get_bits_count(s) - pos;
00389 int bits= show>>(24-len);
00390
00391 print_bin(bits, len);
00392
00393 av_log(NULL, AV_LOG_DEBUG, "%5d %2d %3d te @%5d in %s %s:%d\n", bits, len, i, pos, file, func, line);
00394
00395 return i;
00396 }
00397
00398 #define get_ue_golomb(a) get_ue(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00399 #define get_se_golomb(a) get_se(a, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00400 #define get_te_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00401 #define get_te0_golomb(a, r) get_te(a, r, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00402
00403 #endif
00404
00408 static inline void set_ue_golomb(PutBitContext *pb, int i){
00409 int e;
00410
00411 assert(i>=0);
00412
00413 #if 0
00414 if(i=0){
00415 put_bits(pb, 1, 1);
00416 return;
00417 }
00418 #endif
00419 if(i<256)
00420 put_bits(pb, ff_ue_golomb_len[i], i+1);
00421 else{
00422 e= av_log2(i+1);
00423
00424 put_bits(pb, 2*e+1, i+1);
00425 }
00426 }
00427
00431 static inline void set_te_golomb(PutBitContext *pb, int i, int range){
00432 assert(range >= 1);
00433 assert(i<=range);
00434
00435 if(range==2) put_bits(pb, 1, i^1);
00436 else set_ue_golomb(pb, i);
00437 }
00438
00442 static inline void set_se_golomb(PutBitContext *pb, int i){
00443
00444
00445 #if 0
00446 if(i<=0) i= -2*i;
00447 else i= 2*i-1;
00448 #elif 1
00449 i= 2*i-1;
00450 if(i<0) i^= -1;
00451 #else
00452 i= 2*i-1;
00453 i^= (i>>31);
00454 #endif
00455 set_ue_golomb(pb, i);
00456 }
00457
00461 static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
00462 int e;
00463
00464 assert(i>=0);
00465
00466 e= i>>k;
00467 if(e<limit){
00468 put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
00469 }else{
00470 put_bits(pb, limit + esc_len, i - limit + 1);
00471 }
00472 }
00473
00477 static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
00478 int e;
00479
00480 assert(i>=0);
00481
00482 e= (i>>k) + 1;
00483 if(e<limit){
00484 while(e > 31) {
00485 put_bits(pb, 31, 0);
00486 e -= 31;
00487 }
00488 put_bits(pb, e, 1);
00489 if(k)
00490 put_sbits(pb, k, i);
00491 }else{
00492 while(limit > 31) {
00493 put_bits(pb, 31, 0);
00494 limit -= 31;
00495 }
00496 put_bits(pb, limit , 1);
00497 put_bits(pb, esc_len, i - 1);
00498 }
00499 }
00500
00504 static inline void set_sr_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
00505 int v;
00506
00507 v = -2*i-1;
00508 v ^= (v>>31);
00509
00510 set_ur_golomb(pb, v, k, limit, esc_len);
00511 }
00512
00516 static inline void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len){
00517 int v;
00518
00519 v = -2*i-1;
00520 v ^= (v>>31);
00521
00522 set_ur_golomb_jpegls(pb, v, k, limit, esc_len);
00523 }
00524
00525 #endif