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 0x0200 //(1 << (B_BITS-1))
00028 #define QUARTER 0x0100 //(1 << (B_BITS-2))
00029
00030
00031 static const byte rLPS_table_64x4[64][4]=
00032 {
00033 { 128, 176, 208, 240},
00034 { 128, 167, 197, 227},
00035 { 128, 158, 187, 216},
00036 { 123, 150, 178, 205},
00037 { 116, 142, 169, 195},
00038 { 111, 135, 160, 185},
00039 { 105, 128, 152, 175},
00040 { 100, 122, 144, 166},
00041 { 95, 116, 137, 158},
00042 { 90, 110, 130, 150},
00043 { 85, 104, 123, 142},
00044 { 81, 99, 117, 135},
00045 { 77, 94, 111, 128},
00046 { 73, 89, 105, 122},
00047 { 69, 85, 100, 116},
00048 { 66, 80, 95, 110},
00049 { 62, 76, 90, 104},
00050 { 59, 72, 86, 99},
00051 { 56, 69, 81, 94},
00052 { 53, 65, 77, 89},
00053 { 51, 62, 73, 85},
00054 { 48, 59, 69, 80},
00055 { 46, 56, 66, 76},
00056 { 43, 53, 63, 72},
00057 { 41, 50, 59, 69},
00058 { 39, 48, 56, 65},
00059 { 37, 45, 54, 62},
00060 { 35, 43, 51, 59},
00061 { 33, 41, 48, 56},
00062 { 32, 39, 46, 53},
00063 { 30, 37, 43, 50},
00064 { 29, 35, 41, 48},
00065 { 27, 33, 39, 45},
00066 { 26, 31, 37, 43},
00067 { 24, 30, 35, 41},
00068 { 23, 28, 33, 39},
00069 { 22, 27, 32, 37},
00070 { 21, 26, 30, 35},
00071 { 20, 24, 29, 33},
00072 { 19, 23, 27, 31},
00073 { 18, 22, 26, 30},
00074 { 17, 21, 25, 28},
00075 { 16, 20, 23, 27},
00076 { 15, 19, 22, 25},
00077 { 14, 18, 21, 24},
00078 { 14, 17, 20, 23},
00079 { 13, 16, 19, 22},
00080 { 12, 15, 18, 21},
00081 { 12, 14, 17, 20},
00082 { 11, 14, 16, 19},
00083 { 11, 13, 15, 18},
00084 { 10, 12, 15, 17},
00085 { 10, 12, 14, 16},
00086 { 9, 11, 13, 15},
00087 { 9, 11, 12, 14},
00088 { 8, 10, 12, 14},
00089 { 8, 9, 11, 13},
00090 { 7, 9, 11, 12},
00091 { 7, 9, 10, 12},
00092 { 7, 8, 10, 11},
00093 { 6, 8, 9, 11},
00094 { 6, 7, 9, 10},
00095 { 6, 7, 8, 9},
00096 { 2, 2, 2, 2}
00097 };
00098
00099
00100 static const byte AC_next_state_MPS_64[64] =
00101 {
00102 1,2,3,4,5,6,7,8,9,10,
00103 11,12,13,14,15,16,17,18,19,20,
00104 21,22,23,24,25,26,27,28,29,30,
00105 31,32,33,34,35,36,37,38,39,40,
00106 41,42,43,44,45,46,47,48,49,50,
00107 51,52,53,54,55,56,57,58,59,60,
00108 61,62,62,63
00109 };
00110
00111
00112 static const byte AC_next_state_LPS_64[64] =
00113 {
00114 0, 0, 1, 2, 2, 4, 4, 5, 6, 7,
00115 8, 9, 9,11,11,12,13,13,15,15,
00116 16,16,18,18,19,19,21,21,22,22,
00117 23,24,24,25,26,26,27,27,28,29,
00118 29,30,30,30,31,32,32,33,33,33,
00119 34,34,35,35,35,36,36,36,37,37,
00120 37,38,38,63
00121 };
00122
00123 static const byte renorm_table_32[32]={6,5,4,4,3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
00124
00125 #if TRACE
00126 extern int symbolCount;
00127 #endif
00128
00129
00130
00131
00132
00133
00134
00135
00144 DecodingEnvironmentPtr arideco_create_decoding_environment()
00145 {
00146 DecodingEnvironmentPtr dep;
00147
00148 if ((dep = calloc(1,sizeof(DecodingEnvironment))) == NULL)
00149 no_mem_exit("arideco_create_decoding_environment: dep");
00150 return dep;
00151 }
00152
00153
00160 void arideco_delete_decoding_environment(DecodingEnvironmentPtr dep)
00161 {
00162 if (dep == NULL)
00163 {
00164 snprintf(errortext, ET_SIZE, "Error freeing dep (NULL pointer)");
00165 error (errortext, 200);
00166 }
00167 else
00168 free(dep);
00169 }
00170
00177 void arideco_done_decoding(DecodingEnvironmentPtr dep)
00178 {
00179 (*dep->Dcodestrm_len)++;
00180 #if(TRACE==2)
00181 fprintf(p_trace, "done_decoding: %d\n", *dep->Dcodestrm_len);
00182 #endif
00183 }
00184
00191 unsigned int getbyte(DecodingEnvironmentPtr dep)
00192 {
00193 #if(TRACE==2)
00194 fprintf(p_trace, "get_byte: %d\n", (*dep->Dcodestrm_len));
00195 #endif
00196 return dep->Dcodestrm[(*dep->Dcodestrm_len)++];
00197 }
00198
00205 unsigned int getword(DecodingEnvironmentPtr dep)
00206 {
00207 int d = *dep->Dcodestrm_len;
00208 #if(TRACE==2)
00209 fprintf(p_trace, "get_byte: %d\n", d);
00210 fprintf(p_trace, "get_byte: %d\n", d + 1);
00211 #endif
00212 *dep->Dcodestrm_len += 2;
00213 return ((dep->Dcodestrm[d]<<8) | dep->Dcodestrm[d+1]);
00214 }
00221 void arideco_start_decoding(DecodingEnvironmentPtr dep, unsigned char *code_buffer,
00222 int firstbyte, int *code_len)
00223 {
00224
00225 dep->Dcodestrm = code_buffer;
00226 dep->Dcodestrm_len = code_len;
00227 *dep->Dcodestrm_len = firstbyte;
00228
00229 dep->Dvalue = getbyte(dep);
00230 dep->Dvalue = (dep->Dvalue << 16) | getword(dep);
00231
00232 dep->DbitsLeft = 15;
00233 dep->Drange = HALF-2;
00234
00235 #if (2==TRACE)
00236 fprintf(p_trace, "value: %d firstbyte: %d code_len: %d\n", dep->Dvalue >> dep->DbitsLeft, firstbyte, *code_len);
00237 #endif
00238 }
00239
00240
00247 int arideco_bits_read(DecodingEnvironmentPtr dep)
00248 {
00249 int tmp = ((*dep->Dcodestrm_len) << 3) - dep->DbitsLeft;
00250
00251 #if (2==TRACE)
00252 fprintf(p_trace, "tmp: %d\n", tmp);
00253 #endif
00254 return tmp;
00255 }
00256
00257
00266 unsigned int biari_decode_symbol(DecodingEnvironmentPtr dep, BiContextTypePtr bi_ct )
00267 {
00268 register unsigned int state = bi_ct->state;
00269 register unsigned int bit = bi_ct->MPS;
00270 register unsigned int value = dep->Dvalue;
00271 register unsigned int range = dep->Drange;
00272 register int renorm = 1;
00273 register unsigned int rLPS = rLPS_table_64x4[state][(range>>6) & 0x03];
00274
00275 range -= rLPS;
00276
00277 if(value < (range << dep->DbitsLeft))
00278 {
00279 bi_ct->state = AC_next_state_MPS_64[state];
00280
00281 if( range >= QUARTER )
00282 {
00283 dep->Drange = range;
00284 return (bit);
00285 }
00286 else
00287 range <<= 1;
00288
00289 }
00290 else
00291 {
00292 value -= (range << dep->DbitsLeft);
00293 bit ^= 0x01;
00294
00295 if (!state)
00296 bi_ct->MPS ^= 0x01;
00297
00298 bi_ct->state = AC_next_state_LPS_64[state];
00299
00300 renorm = renorm_table_32[(rLPS>>3) & 0x1F];
00301 range = (rLPS << renorm);
00302 }
00303
00304 dep->Drange = range;
00305 dep->DbitsLeft -= renorm;
00306 if( dep->DbitsLeft > 0 )
00307 {
00308 dep->Dvalue = value;
00309 return(bit);
00310 }
00311
00312 dep->Dvalue = (value << 16) | getword(dep);
00313
00314 dep->DbitsLeft += 16;
00315
00316 return(bit);
00317 }
00318
00319
00328 unsigned int biari_decode_symbol_eq_prob(DecodingEnvironmentPtr dep)
00329 {
00330 register int tmp_value;
00331 register int value = dep->Dvalue;
00332
00333 if(--(dep->DbitsLeft) == 0)
00334 {
00335 value = (value << 16) | getword( dep );
00336
00337 dep->DbitsLeft = 16;
00338 }
00339 tmp_value = value - (dep->Drange << dep->DbitsLeft);
00340
00341 if (tmp_value < 0)
00342 {
00343 dep->Dvalue = value;
00344 return 0;
00345 }
00346 else
00347 {
00348 dep->Dvalue = tmp_value;
00349 return 1;
00350 }
00351 }
00352
00361 unsigned int biari_decode_final(DecodingEnvironmentPtr dep)
00362 {
00363 register unsigned int range = dep->Drange - 2;
00364 register int value = dep->Dvalue;
00365 value -= (range << dep->DbitsLeft);
00366
00367 if (value < 0)
00368 {
00369 if( range >= QUARTER )
00370 {
00371 dep->Drange = range;
00372 return 0;
00373 }
00374 else
00375 {
00376 dep->Drange = (range << 1);
00377 if( --(dep->DbitsLeft) > 0 )
00378 return 0;
00379 else
00380 {
00381 dep->Dvalue = (dep->Dvalue << 16) | getword( dep );
00382
00383 dep->DbitsLeft = 16;
00384 return 0;
00385 }
00386 }
00387 }
00388 else
00389 {
00390 return 1;
00391 }
00392 }
00393
00400 void biari_init_context (int qp, BiContextTypePtr ctx, const char* ini)
00401 {
00402 int pstate = ((ini[0]* qp )>>4) + ini[1];
00403 pstate = iClip3(1, 126, pstate);
00404
00405 if ( pstate >= 64 )
00406 {
00407 ctx->state = (unsigned short) (pstate - 64);
00408 ctx->MPS = 1;
00409 }
00410 else
00411 {
00412 ctx->state = (unsigned short) (63 - pstate);
00413 ctx->MPS = 0;
00414 }
00415 }
00416