00001 /* 00002 * Range coder 00003 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00034 #include <string.h> 00035 00036 #include "libavutil/avassert.h" 00037 #include "avcodec.h" 00038 #include "rangecoder.h" 00039 #include "bytestream.h" 00040 00041 00042 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size){ 00043 c->bytestream_start= 00044 c->bytestream= buf; 00045 c->bytestream_end= buf + buf_size; 00046 00047 c->low= 0; 00048 c->range= 0xFF00; 00049 c->outstanding_count= 0; 00050 c->outstanding_byte= -1; 00051 } 00052 00053 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size){ 00054 /* cast to avoid compiler warning */ 00055 ff_init_range_encoder(c, (uint8_t *) buf, buf_size); 00056 00057 c->low = bytestream_get_be16(&c->bytestream); 00058 } 00059 00060 void ff_build_rac_states(RangeCoder *c, int factor, int max_p){ 00061 const int64_t one= 1LL<<32; 00062 int64_t p; 00063 int last_p8, p8, i; 00064 00065 memset(c->zero_state, 0, sizeof(c->zero_state)); 00066 memset(c-> one_state, 0, sizeof(c-> one_state)); 00067 00068 last_p8= 0; 00069 p= one/2; 00070 for(i=0; i<128; i++){ 00071 p8= (256*p + one/2) >> 32; //FIXME try without the one 00072 if(p8 <= last_p8) p8= last_p8+1; 00073 if(last_p8 && last_p8<256 && p8<=max_p) 00074 c->one_state[last_p8]= p8; 00075 00076 p+= ((one-p)*factor + one/2) >> 32; 00077 last_p8= p8; 00078 } 00079 00080 for(i=256-max_p; i<=max_p; i++){ 00081 if(c->one_state[i]) 00082 continue; 00083 00084 p= (i*one + 128) >> 8; 00085 p+= ((one-p)*factor + one/2) >> 32; 00086 p8= (256*p + one/2) >> 32; //FIXME try without the one 00087 if(p8 <= i) p8= i+1; 00088 if(p8 > max_p) p8= max_p; 00089 c->one_state[ i]= p8; 00090 } 00091 00092 for(i=1; i<255; i++) 00093 c->zero_state[i]= 256-c->one_state[256-i]; 00094 } 00095 00100 int ff_rac_terminate(RangeCoder *c){ 00101 c->range=0xFF; 00102 c->low +=0xFF; 00103 renorm_encoder(c); 00104 c->range=0xFF; 00105 renorm_encoder(c); 00106 00107 av_assert1(c->low == 0); 00108 av_assert1(c->range >= 0x100); 00109 00110 return c->bytestream - c->bytestream_start; 00111 } 00112 00113 #ifdef TEST 00114 #define SIZE 10240 00115 00116 #include "libavutil/lfg.h" 00117 00118 int main(void){ 00119 RangeCoder c; 00120 uint8_t b[9*SIZE]; 00121 uint8_t r[9*SIZE]; 00122 int i; 00123 uint8_t state[10]; 00124 AVLFG prng; 00125 00126 av_lfg_init(&prng, 1); 00127 00128 ff_init_range_encoder(&c, b, SIZE); 00129 ff_build_rac_states(&c, 0.05*(1LL<<32), 128+64+32+16); 00130 00131 memset(state, 128, sizeof(state)); 00132 00133 for(i=0; i<SIZE; i++){ 00134 r[i] = av_lfg_get(&prng) % 7; 00135 } 00136 00137 for(i=0; i<SIZE; i++){ 00138 START_TIMER 00139 put_rac(&c, state, r[i]&1); 00140 STOP_TIMER("put_rac") 00141 } 00142 00143 ff_rac_terminate(&c); 00144 00145 ff_init_range_decoder(&c, b, SIZE); 00146 00147 memset(state, 128, sizeof(state)); 00148 00149 for(i=0; i<SIZE; i++){ 00150 START_TIMER 00151 if( (r[i]&1) != get_rac(&c, state) ) 00152 av_log(NULL, AV_LOG_ERROR, "rac failure at %d\n", i); 00153 STOP_TIMER("get_rac") 00154 } 00155 00156 return 0; 00157 } 00158 #endif /* TEST */