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 00027 #ifndef AVCODEC_RANGECODER_H 00028 #define AVCODEC_RANGECODER_H 00029 00030 #include <stdint.h> 00031 00032 #include "libavutil/common.h" 00033 #include "libavutil/avassert.h" 00034 00035 typedef struct RangeCoder { 00036 int low; 00037 int range; 00038 int outstanding_count; 00039 int outstanding_byte; 00040 uint8_t zero_state[256]; 00041 uint8_t one_state[256]; 00042 uint8_t *bytestream_start; 00043 uint8_t *bytestream; 00044 uint8_t *bytestream_end; 00045 } RangeCoder; 00046 00047 void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size); 00048 void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size); 00049 int ff_rac_terminate(RangeCoder *c); 00050 void ff_build_rac_states(RangeCoder *c, int factor, int max_p); 00051 00052 static inline void renorm_encoder(RangeCoder *c) 00053 { 00054 // FIXME: optimize 00055 while (c->range < 0x100) { 00056 if (c->outstanding_byte < 0) { 00057 c->outstanding_byte = c->low >> 8; 00058 } else if (c->low <= 0xFF00) { 00059 *c->bytestream++ = c->outstanding_byte; 00060 for (; c->outstanding_count; c->outstanding_count--) 00061 *c->bytestream++ = 0xFF; 00062 c->outstanding_byte = c->low >> 8; 00063 } else if (c->low >= 0x10000) { 00064 *c->bytestream++ = c->outstanding_byte + 1; 00065 for (; c->outstanding_count; c->outstanding_count--) 00066 *c->bytestream++ = 0x00; 00067 c->outstanding_byte = (c->low >> 8) & 0xFF; 00068 } else { 00069 c->outstanding_count++; 00070 } 00071 00072 c->low = (c->low & 0xFF) << 8; 00073 c->range <<= 8; 00074 } 00075 } 00076 00077 static inline int get_rac_count(RangeCoder *c) 00078 { 00079 int x = c->bytestream - c->bytestream_start + c->outstanding_count; 00080 if (c->outstanding_byte >= 0) 00081 x++; 00082 return 8 * x - av_log2(c->range); 00083 } 00084 00085 static inline void put_rac(RangeCoder *c, uint8_t *const state, int bit) 00086 { 00087 int range1 = (c->range * (*state)) >> 8; 00088 00089 av_assert2(*state); 00090 av_assert2(range1 < c->range); 00091 av_assert2(range1 > 0); 00092 if (!bit) { 00093 c->range -= range1; 00094 *state = c->zero_state[*state]; 00095 } else { 00096 c->low += c->range - range1; 00097 c->range = range1; 00098 *state = c->one_state[*state]; 00099 } 00100 00101 renorm_encoder(c); 00102 } 00103 00104 static inline void refill(RangeCoder *c) 00105 { 00106 if (c->range < 0x100) { 00107 c->range <<= 8; 00108 c->low <<= 8; 00109 if (c->bytestream < c->bytestream_end) 00110 c->low += c->bytestream[0]; 00111 c->bytestream++; 00112 } 00113 } 00114 00115 static inline int get_rac(RangeCoder *c, uint8_t *const state) 00116 { 00117 int range1 = (c->range * (*state)) >> 8; 00118 int av_unused one_mask; 00119 00120 c->range -= range1; 00121 #if 1 00122 if (c->low < c->range) { 00123 *state = c->zero_state[*state]; 00124 refill(c); 00125 return 0; 00126 } else { 00127 c->low -= c->range; 00128 *state = c->one_state[*state]; 00129 c->range = range1; 00130 refill(c); 00131 return 1; 00132 } 00133 #else 00134 one_mask = (c->range - c->low - 1) >> 31; 00135 00136 c->low -= c->range & one_mask; 00137 c->range += (range1 - c->range) & one_mask; 00138 00139 *state = c->zero_state[(*state) + (256 & one_mask)]; 00140 00141 refill(c); 00142 00143 return one_mask & 1; 00144 #endif 00145 } 00146 00147 #endif /* AVCODEC_RANGECODER_H */
1.5.8