Commit d9e6a6c6 authored by Michael Niedermayer's avatar Michael Niedermayer

golomb rice code cleanup / simplify (~0.5% compression gain and slightly faster)

jpegls style golomb rice coder

Originally committed as revision 2040 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 25bd2349
...@@ -300,6 +300,9 @@ static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int ...@@ -300,6 +300,9 @@ static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int
k++; k++;
i += i; i += i;
} }
assert(k<=8);
#if 0 // JPEG LS #if 0 // JPEG LS
if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1); if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
else code= v; else code= v;
...@@ -310,7 +313,7 @@ static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int ...@@ -310,7 +313,7 @@ static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int
code = -2*code-1; code = -2*code-1;
code^= (code>>31); code^= (code>>31);
//printf("v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code, state->bias, state->error_sum, state->drift, state->count, k); //printf("v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code, state->bias, state->error_sum, state->drift, state->count, k);
set_ur_golomb(pb, code, k, 8, 8); set_ur_golomb(pb, code, k, 12, 8);
update_vlc_state(state, v); update_vlc_state(state, v);
} }
...@@ -325,7 +328,9 @@ static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state){ ...@@ -325,7 +328,9 @@ static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state){
i += i; i += i;
} }
v= get_ur_golomb(gb, k, 8, 8); assert(k<=8);
v= get_ur_golomb(gb, k, 12, 8);
//printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k); //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
v++; v++;
......
...@@ -179,7 +179,7 @@ static inline int svq3_get_se_golomb(GetBitContext *gb){ ...@@ -179,7 +179,7 @@ static inline int svq3_get_se_golomb(GetBitContext *gb){
} }
/** /**
* read unsigned golomb rice code. * read unsigned golomb rice code (ffv1).
*/ */
static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){ static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len){
unsigned int buf; unsigned int buf;
...@@ -190,7 +190,7 @@ static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len ...@@ -190,7 +190,7 @@ static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len
buf=GET_CACHE(re, gb); buf=GET_CACHE(re, gb);
log= av_log2(buf); log= av_log2(buf);
//printf("buf:%X log:%d\n", buf, log);
if(log > 31-limit){ if(log > 31-limit){
buf >>= log - k; buf >>= log - k;
buf += (30-log)<<k; buf += (30-log)<<k;
...@@ -198,15 +198,62 @@ static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len ...@@ -198,15 +198,62 @@ static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len
CLOSE_READER(re, gb); CLOSE_READER(re, gb);
return buf; return buf;
}else if(log == 31-limit){ }else{
buf >>= log - esc_len; buf >>= 32 - limit - esc_len;
buf -= 1<<esc_len; LAST_SKIP_BITS(re, gb, esc_len + limit);
LAST_SKIP_BITS(re, gb, esc_len + limit + 1); CLOSE_READER(re, gb);
return buf + limit - 1;
}
}
/**
* read unsigned golomb rice code (jpegls).
*/
static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){
unsigned int buf;
int log;
OPEN_READER(re, gb);
UPDATE_CACHE(re, gb);
buf=GET_CACHE(re, gb);
log= av_log2(buf);
if(log > 31-12){
buf >>= log - k;
buf += (30-log)<<k;
LAST_SKIP_BITS(re, gb, 32 + k - log);
CLOSE_READER(re, gb);
return buf;
}else{
int i;
for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){
LAST_SKIP_BITS(re, gb, 1);
UPDATE_CACHE(re, gb);
}
SKIP_BITS(re, gb, 1);
if(i < limit - 1){
if(k){
buf = SHOW_UBITS(re, gb, k);
LAST_SKIP_BITS(re, gb, k);
}else{
buf=0;
}
CLOSE_READER(re, gb);
return buf + (i<<k);
}else if(i == limit - 1){
buf = SHOW_UBITS(re, gb, esc_len);
LAST_SKIP_BITS(re, gb, esc_len);
CLOSE_READER(re, gb); CLOSE_READER(re, gb);
return buf + 1; return buf + 1;
}else }else
return -1; return -1;
}
} }
#ifdef TRACE #ifdef TRACE
...@@ -312,7 +359,7 @@ static inline void set_se_golomb(PutBitContext *pb, int i){ ...@@ -312,7 +359,7 @@ static inline void set_se_golomb(PutBitContext *pb, int i){
} }
/** /**
* write unsigned golomb rice code. * write unsigned golomb rice code (ffv1).
*/ */
static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){ static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int esc_len){
int e; int e;
...@@ -323,9 +370,24 @@ static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int ...@@ -323,9 +370,24 @@ static inline void set_ur_golomb(PutBitContext *pb, int i, int k, int limit, int
if(e<limit){ if(e<limit){
put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1))); put_bits(pb, e + k + 1, (1<<k) + (i&((1<<k)-1)));
}else{ }else{
// printf("set %08X, %d\n", (1<<esc_len) + i - 1, limit + esc_len + 1); put_bits(pb, limit + esc_len, i - limit + 1);
put_bits(pb, limit + esc_len + 1, (1<<esc_len) + i - 1); }
// put_bits(pb, 1, limit + 1); }
// put_bits(pb, i - 1, esc_len);
/**
* write unsigned golomb rice code (jpegls).
*/
static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int limit, int esc_len){
int e;
assert(i>=0);
e= (i>>k) + 1;
if(e<limit){
put_bits(pb, e, 1);
put_bits(pb, k, i&((1<<k)-1));
}else{
put_bits(pb, limit , 1);
put_bits(pb, esc_len, i - 1);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment