00001
00021 #include "global.h"
00022 #include "memalloc.h"
00023 #include "biaridecod.h"
00024
00025
00026 #define B_BITS 10 // Number of bits to represent the whole coding interval
00027 #define HALF 0x01FE //(1 << (B_BITS-1)) - 2
00028 #define QUARTER 0x0100 //(1 << (B_BITS-2))
00029
00030
00031
00032
00033
00034
00035
00036
00037
00046 DecodingEnvironmentPtr arideco_create_decoding_environment()
00047 {
00048 DecodingEnvironmentPtr dep;
00049
00050 if ((dep = calloc(1,sizeof(DecodingEnvironment))) == NULL)
00051 no_mem_exit("arideco_create_decoding_environment: dep");
00052 return dep;
00053 }
00054
00055
00062 void arideco_delete_decoding_environment(DecodingEnvironmentPtr dep)
00063 {
00064 if (dep == NULL)
00065 {
00066 snprintf(errortext, ET_SIZE, "Error freeing dep (NULL pointer)");
00067 error (errortext, 200);
00068 }
00069 else
00070 free(dep);
00071 }
00072
00079 void arideco_done_decoding(DecodingEnvironmentPtr dep)
00080 {
00081 (*dep->Dcodestrm_len)++;
00082 #if(TRACE==2)
00083 fprintf(p_trace, "done_decoding: %d\n", *dep->Dcodestrm_len);
00084 #endif
00085 }
00086
00093 unsigned int getbyte(DecodingEnvironmentPtr dep)
00094 {
00095 #if(TRACE==2)
00096 fprintf(p_trace, "get_byte: %d\n", (*dep->Dcodestrm_len));
00097 #endif
00098 return dep->Dcodestrm[(*dep->Dcodestrm_len)++];
00099 }
00100
00107 unsigned int getword(DecodingEnvironmentPtr dep)
00108 {
00109 int d = *dep->Dcodestrm_len;
00110 #if(TRACE==2)
00111 fprintf(p_trace, "get_byte: %d\n", d);
00112 fprintf(p_trace, "get_byte: %d\n", d + 1);
00113 #endif
00114 *dep->Dcodestrm_len += 2;
00115 return ((dep->Dcodestrm[d]<<8) | dep->Dcodestrm[d+1]);
00116 }
00123 void arideco_start_decoding(DecodingEnvironmentPtr dep, unsigned char *code_buffer,
00124 int firstbyte, int *code_len)
00125 {
00126
00127 dep->Dcodestrm = code_buffer;
00128 dep->Dcodestrm_len = code_len;
00129 *dep->Dcodestrm_len = firstbyte;
00130
00131 dep->Dvalue = getbyte(dep);
00132 dep->Dvalue = (dep->Dvalue << 16) | getword(dep);
00133
00134 dep->DbitsLeft = 15;
00135 dep->Drange = HALF;
00136
00137 #if (2==TRACE)
00138 fprintf(p_trace, "value: %d firstbyte: %d code_len: %d\n", dep->Dvalue >> dep->DbitsLeft, firstbyte, *code_len);
00139 #endif
00140 }
00141
00142
00149 int arideco_bits_read(DecodingEnvironmentPtr dep)
00150 {
00151 int tmp = ((*dep->Dcodestrm_len) << 3) - dep->DbitsLeft;
00152
00153 #if (2==TRACE)
00154 fprintf(p_trace, "tmp: %d\n", tmp);
00155 #endif
00156 return tmp;
00157 }
00158
00159
00168 unsigned int biari_decode_symbol(DecodingEnvironmentPtr dep, BiContextTypePtr bi_ct )
00169 {
00170 unsigned int state = bi_ct->state;
00171 unsigned int bit = bi_ct->MPS;
00172 unsigned int value = dep->Dvalue;
00173 unsigned int range = dep->Drange;
00174 int renorm = 1;
00175 unsigned int rLPS = rLPS_table_64x4[state][(range>>6) & 0x03];
00176
00177 range -= rLPS;
00178
00179 if(value < (range << dep->DbitsLeft))
00180 {
00181 bi_ct->state = AC_next_state_MPS_64[state];
00182
00183 if( range >= QUARTER )
00184 {
00185 dep->Drange = range;
00186 return (bit);
00187 }
00188 else
00189 range <<= 1;
00190
00191 }
00192 else
00193 {
00194 value -= (range << dep->DbitsLeft);
00195 bit ^= 0x01;
00196
00197 if (!state)
00198 bi_ct->MPS ^= 0x01;
00199
00200 bi_ct->state = AC_next_state_LPS_64[state];
00201
00202 renorm = renorm_table_32[(rLPS>>3) & 0x1F];
00203 range = (rLPS << renorm);
00204 }
00205
00206 dep->Drange = range;
00207 dep->DbitsLeft -= renorm;
00208 if( dep->DbitsLeft > 0 )
00209 {
00210 dep->Dvalue = value;
00211 return(bit);
00212 }
00213
00214 dep->Dvalue = (value << 16) | getword(dep);
00215
00216 dep->DbitsLeft += 16;
00217
00218 return(bit);
00219 }
00220
00221
00230 unsigned int biari_decode_symbol_eq_prob(DecodingEnvironmentPtr dep)
00231 {
00232 int tmp_value;
00233 int value = dep->Dvalue;
00234
00235 if(--(dep->DbitsLeft) == 0)
00236 {
00237 value = (value << 16) | getword( dep );
00238
00239 dep->DbitsLeft = 16;
00240 }
00241 tmp_value = value - (dep->Drange << dep->DbitsLeft);
00242
00243 if (tmp_value < 0)
00244 {
00245 dep->Dvalue = value;
00246 return 0;
00247 }
00248 else
00249 {
00250 dep->Dvalue = tmp_value;
00251 return 1;
00252 }
00253 }
00254
00263 unsigned int biari_decode_final(DecodingEnvironmentPtr dep)
00264 {
00265 unsigned int range = dep->Drange - 2;
00266 int value = dep->Dvalue;
00267 value -= (range << dep->DbitsLeft);
00268
00269 if (value < 0)
00270 {
00271 if( range >= QUARTER )
00272 {
00273 dep->Drange = range;
00274 return 0;
00275 }
00276 else
00277 {
00278 dep->Drange = (range << 1);
00279 if( --(dep->DbitsLeft) > 0 )
00280 return 0;
00281 else
00282 {
00283 dep->Dvalue = (dep->Dvalue << 16) | getword( dep );
00284
00285 dep->DbitsLeft = 16;
00286 return 0;
00287 }
00288 }
00289 }
00290 else
00291 {
00292 return 1;
00293 }
00294 }
00295
00302 void biari_init_context (int qp, BiContextTypePtr ctx, const char* ini)
00303 {
00304 int pstate = ((ini[0]* qp )>>4) + ini[1];
00305 pstate = iClip3(1, 126, pstate);
00306
00307 if ( pstate >= 64 )
00308 {
00309 ctx->state = (uint16) (pstate - 64);
00310 ctx->MPS = 1;
00311 }
00312 else
00313 {
00314 ctx->state = (uint16) (63 - pstate);
00315 ctx->MPS = 0;
00316 }
00317 }
00318