00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00033 #include <string.h>
00034
00035 #include "avcodec.h"
00036 #include "common.h"
00037 #include "rangecoder.h"
00038
00039
00040 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size){
00041 c->bytestream_start=
00042 c->bytestream= buf;
00043 c->bytestream_end= buf + buf_size;
00044
00045 c->low= 0;
00046 c->range= 0xFF00;
00047 c->outstanding_count= 0;
00048 c->outstanding_byte= -1;
00049 }
00050
00051 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size){
00052 ff_init_range_encoder(c, buf, buf_size);
00053
00054 c->low =(*c->bytestream++)<<8;
00055 c->low+= *c->bytestream++;
00056 }
00057
00058 void ff_build_rac_states(RangeCoder *c, int factor, int max_p){
00059 const int64_t one= 1LL<<32;
00060 int64_t p;
00061 int last_p8, p8, i;
00062
00063 memset(c->zero_state, 0, sizeof(c->zero_state));
00064 memset(c-> one_state, 0, sizeof(c-> one_state));
00065
00066 #if 0
00067 for(i=1; i<256; i++){
00068 if(c->one_state[i])
00069 continue;
00070
00071 p= (i*one + 128) >> 8;
00072 last_p8= i;
00073 for(;;){
00074 p+= ((one-p)*factor + one/2) >> 32;
00075 p8= (256*p + one/2) >> 32;
00076 if(p8 <= last_p8) p8= last_p8+1;
00077 if(p8 > max_p) p8= max_p;
00078 if(p8 < last_p8)
00079 break;
00080 c->one_state[last_p8]= p8;
00081 if(p8 == last_p8)
00082 break;
00083 last_p8= p8;
00084 }
00085 }
00086 #endif
00087 #if 1
00088 last_p8= 0;
00089 p= one/2;
00090 for(i=0; i<128; i++){
00091 p8= (256*p + one/2) >> 32;
00092 if(p8 <= last_p8) p8= last_p8+1;
00093 if(last_p8 && last_p8<256 && p8<=max_p)
00094 c->one_state[last_p8]= p8;
00095
00096 p+= ((one-p)*factor + one/2) >> 32;
00097 last_p8= p8;
00098 }
00099 #endif
00100 for(i=256-max_p; i<=max_p; i++){
00101 if(c->one_state[i])
00102 continue;
00103
00104 p= (i*one + 128) >> 8;
00105 p+= ((one-p)*factor + one/2) >> 32;
00106 p8= (256*p + one/2) >> 32;
00107 if(p8 <= i) p8= i+1;
00108 if(p8 > max_p) p8= max_p;
00109 c->one_state[ i]= p8;
00110 }
00111
00112 for(i=0; i<256; i++)
00113 c->zero_state[i]= 256-c->one_state[256-i];
00114 #if 0
00115 for(i=0; i<256; i++)
00116 av_log(NULL, AV_LOG_DEBUG, "%3d %3d\n", i, c->one_state[i]);
00117 #endif
00118 }
00119
00124 int ff_rac_terminate(RangeCoder *c){
00125 c->range=0xFF;
00126 c->low +=0xFF;
00127 renorm_encoder(c);
00128 c->range=0xFF;
00129 renorm_encoder(c);
00130
00131 assert(c->low == 0);
00132 assert(c->range >= 0x100);
00133
00134 return c->bytestream - c->bytestream_start;
00135 }
00136
00137 #if 0 //selftest
00138 #define SIZE 10240
00139 int main(){
00140 RangeCoder c;
00141 uint8_t b[9*SIZE];
00142 uint8_t r[9*SIZE];
00143 int i;
00144 uint8_t state[10]= {0};
00145
00146 ff_init_range_encoder(&c, b, SIZE);
00147 ff_build_rac_states(&c, 0.05*(1LL<<32), 128+64+32+16);
00148
00149 memset(state, 128, sizeof(state));
00150
00151 for(i=0; i<SIZE; i++){
00152 r[i]= random()%7;
00153 }
00154
00155
00156 for(i=0; i<SIZE; i++){
00157 START_TIMER
00158 put_rac(&c, state, r[i]&1);
00159 STOP_TIMER("put_rac")
00160 }
00161
00162 ff_put_rac_terminate(&c);
00163
00164 ff_init_range_decoder(&c, b, SIZE);
00165
00166 memset(state, 128, sizeof(state));
00167
00168 for(i=0; i<SIZE; i++){
00169 START_TIMER
00170 if( (r[i]&1) != get_rac(&c, state) )
00171 av_log(NULL, AV_LOG_DEBUG, "rac failure at %d\n", i);
00172 STOP_TIMER("get_rac")
00173 }
00174
00175 return 0;
00176 }
00177
00178 #endif