00001 /* 00002 * Range coder 00003 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 * 00019 */ 00020 00026 typedef struct RangeCoder{ 00027 int low; 00028 int range; 00029 int outstanding_count; 00030 int outstanding_byte; 00031 uint8_t zero_state[256]; 00032 uint8_t one_state[256]; 00033 uint8_t *bytestream_start; 00034 uint8_t *bytestream; 00035 uint8_t *bytestream_end; 00036 }RangeCoder; 00037 00038 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size); 00039 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size); 00040 int ff_rac_terminate(RangeCoder *c); 00041 void ff_build_rac_states(RangeCoder *c, int factor, int max_p); 00042 00043 static inline void renorm_encoder(RangeCoder *c){ 00044 //FIXME optimize 00045 while(c->range < 0x100){ 00046 if(c->outstanding_byte < 0){ 00047 c->outstanding_byte= c->low>>8; 00048 }else if(c->low <= 0xFF00){ 00049 *c->bytestream++ = c->outstanding_byte; 00050 for(;c->outstanding_count; c->outstanding_count--) 00051 *c->bytestream++ = 0xFF; 00052 c->outstanding_byte= c->low>>8; 00053 }else if(c->low >= 0x10000){ 00054 *c->bytestream++ = c->outstanding_byte + 1; 00055 for(;c->outstanding_count; c->outstanding_count--) 00056 *c->bytestream++ = 0x00; 00057 c->outstanding_byte= (c->low>>8) & 0xFF; 00058 }else{ 00059 c->outstanding_count++; 00060 } 00061 00062 c->low = (c->low & 0xFF)<<8; 00063 c->range <<= 8; 00064 } 00065 } 00066 00067 static inline void put_rac(RangeCoder *c, uint8_t * const state, int bit){ 00068 int range1= (c->range * (*state)) >> 8; 00069 00070 assert(*state); 00071 assert(range1 < c->range); 00072 assert(range1 > 0); 00073 if(!bit){ 00074 c->range -= range1; 00075 *state= c->zero_state[*state]; 00076 }else{ 00077 c->low += c->range - range1; 00078 c->range = range1; 00079 *state= c->one_state[*state]; 00080 } 00081 00082 renorm_encoder(c); 00083 } 00084 00085 static inline void refill(RangeCoder *c){ 00086 if(c->range < 0x100){ 00087 c->range <<= 8; 00088 c->low <<= 8; 00089 if(c->bytestream < c->bytestream_end) 00090 c->low+= c->bytestream[0]; 00091 c->bytestream++; 00092 } 00093 } 00094 00095 static inline int get_rac(RangeCoder *c, uint8_t * const state){ 00096 int range1= (c->range * (*state)) >> 8; 00097 int attribute_unused one_mask; 00098 00099 c->range -= range1; 00100 #if 1 00101 if(c->low < c->range){ 00102 *state= c->zero_state[*state]; 00103 refill(c); 00104 return 0; 00105 }else{ 00106 c->low -= c->range; 00107 *state= c->one_state[*state]; 00108 c->range = range1; 00109 refill(c); 00110 return 1; 00111 } 00112 #else 00113 one_mask= (c->range - c->low-1)>>31; 00114 00115 c->low -= c->range & one_mask; 00116 c->range += (range1 - c->range) & one_mask; 00117 00118 *state= c->zero_state[(*state) + (256&one_mask)]; 00119 00120 refill(c); 00121 00122 return one_mask&1; 00123 #endif 00124 } 00125
1.5.5