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