00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #define CONTEXT_INI_C
00017
00018 #include <math.h>
00019
00020 #include "global.h"
00021
00022 #include "ctx_tables.h"
00023 #include "biariencode.h"
00024 #include "memalloc.h"
00025
00026 #define DEFAULT_CTX_MODEL 0
00027 #define RELIABLE_COUNT 32.0
00028 #define FRAME_TYPES 4
00029 #define FIXED 0
00030
00031
00032 static const double probabilities[64] =
00033 {
00034 0.500000, 0.474609, 0.450507, 0.427629, 0.405912, 0.385299, 0.365732, 0.347159,
00035 0.329530, 0.312795, 0.296911, 0.281833, 0.267520, 0.253935, 0.241039, 0.228799,
00036 0.217180, 0.206151, 0.195682, 0.185744, 0.176312, 0.167358, 0.158859, 0.150792,
00037 0.143134, 0.135866, 0.128966, 0.122417, 0.116200, 0.110299, 0.104698, 0.099381,
00038 0.094334, 0.089543, 0.084996, 0.080680, 0.076583, 0.072694, 0.069002, 0.065498,
00039 0.062172, 0.059014, 0.056018, 0.053173, 0.050473, 0.047909, 0.045476, 0.043167,
00040 0.040975, 0.038894, 0.036919, 0.035044, 0.033264, 0.031575, 0.029972, 0.028450,
00041 0.027005, 0.025633, 0.024332, 0.023096, 0.021923, 0.020810, 0.019753, 0.018750
00042 };
00043
00044 void create_context_memory (ImageParameters *p_Img, InputParameters *p_Inp)
00045 {
00046 int k;
00047 int num_mb = p_Img->FrameSizeInMbs;
00048 double log2 = log(2.0);
00049
00050 p_Img->num_mb_per_slice = (p_Inp->slice_mode == 1 ? p_Inp->slice_argument : num_mb);
00051 p_Img->number_of_slices = (num_mb + p_Img->num_mb_per_slice - 1) / p_Img->num_mb_per_slice;
00052 get_mem3Dint(&p_Img->initialized, 3, FRAME_TYPES, p_Img->number_of_slices);
00053
00054 memset(&p_Img->initialized[0][0][0], 0, 3 * FRAME_TYPES * p_Img->number_of_slices * sizeof(int));
00055 get_mem3Dint(&p_Img->modelNumber, 3, FRAME_TYPES, p_Img->number_of_slices);
00056
00057
00058 for( k=0; k<64; k++ )
00059 {
00060 p_Img->probability[k + 64] = probabilities[k];
00061 p_Img->probability[k] = 1.0 - probabilities[63 - k];
00062 p_Img->entropy [k] = log( p_Img->probability[ k] ) / log2;
00063 p_Img->entropy[127-k] = log( probabilities[63 - k] ) / log2;
00064 p_Img->enorm [k] = p_Img->entropy[k] - p_Img->entropy[127-k];
00065 p_Img->enorm[127 - k] = -p_Img->enorm[k];
00066 }
00067 }
00068
00069 void free_context_memory (ImageParameters *p_Img)
00070 {
00071 free_mem3Dint(p_Img->initialized);
00072 free_mem3Dint(p_Img->modelNumber);
00073 }
00074
00075 #define BIARI_CTX_INIT2(qp, ii,jj,ctx,tab) \
00076 { \
00077 for (i=0; i<ii; i++) \
00078 for (j=0; j<jj; j++) \
00079 { \
00080 biari_init_context (qp, &(ctx[i][j]), &(tab[i][j][0])); \
00081 } \
00082 }
00083
00084
00085 static inline void binary_context_init1(int qp, int jj, BiContextType *ctx, const char table[][2])
00086 {
00087 int j;
00088 for (j=0; j<jj; j++)
00089 {
00090 biari_init_context (qp, &(ctx[j]), &(table[j][0]));
00091 }
00092 }
00093
00094 static inline void binary_context_init2(int qp, int ii, int jj, BiContextType ctx[][11], const char table[][11][2])
00095 {
00096 int i, j;
00097 for (i = 0; i < ii; i++)
00098 {
00099 for (j = 0; j < jj; j++)
00100 {
00101 biari_init_context (qp, &(ctx[i][j]), &(table[i][j][0]));
00102 }
00103 }
00104 }
00105
00106 void SetCtxModelNumber (Slice *currSlice)
00107 {
00108 ImageParameters *p_Img = currSlice->p_Img;
00109 InputParameters *p_Inp = currSlice->p_Inp;
00110
00111 int frame_field = p_Img->field_picture;
00112 int img_type = currSlice->slice_type;
00113 int ctx_number = currSlice->start_mb_nr / p_Img->num_mb_per_slice;
00114
00115 if(img_type == I_SLICE)
00116 {
00117 currSlice->model_number=DEFAULT_CTX_MODEL;
00118 return;
00119 }
00120
00121 if(p_Inp->context_init_method==FIXED)
00122 {
00123 currSlice->model_number = p_Inp->model_number;
00124 return;
00125 }
00126
00127 if (p_Img->initialized [frame_field][img_type][ctx_number])
00128 {
00129 currSlice->model_number = p_Img->modelNumber[frame_field][img_type][ctx_number];
00130 }
00131 else if (ctx_number && p_Img->initialized[frame_field][img_type][ctx_number-1])
00132 {
00133 currSlice->model_number = p_Img->modelNumber[frame_field][img_type][ctx_number-1];
00134 }
00135 else
00136 {
00137 currSlice->model_number = DEFAULT_CTX_MODEL;
00138 }
00139 }
00140
00141 void init_contexts (Slice *currSlice)
00142 {
00143 MotionInfoContexts* mc = currSlice->mot_ctx;
00144 TextureInfoContexts* tc = currSlice->tex_ctx;
00145 int model_number = currSlice->model_number;
00146 int qp = imax(0, currSlice->qp);
00147 int i, j;
00148
00149 if ((currSlice->slice_type == I_SLICE) || (currSlice->slice_type == SI_SLICE))
00150 {
00151
00152 BIARI_CTX_INIT2 (qp, 3, NUM_MB_TYPE_CTX, mc->mb_type_contexts, INIT_MB_TYPE_I[model_number]);
00153
00154
00155 binary_context_init1 (qp, NUM_TRANSFORM_SIZE_CTX, tc->transform_size_contexts, INIT_TRANSFORM_SIZE_I[model_number][0]);
00156 binary_context_init1 (qp, NUM_IPR_CTX, tc->ipr_contexts, INIT_IPR_I[model_number][0]);
00157 binary_context_init1 (qp, NUM_CIPR_CTX, tc->cipr_contexts, INIT_CIPR_I[model_number][0]);
00158 BIARI_CTX_INIT2 (qp, 3, NUM_CBP_CTX, tc->cbp_contexts, INIT_CBP_I[model_number]);
00159 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_BCBP_CTX, tc->bcbp_contexts, INIT_BCBP_I[model_number]);
00160 binary_context_init1 (qp, NUM_DELTA_QP_CTX, tc->delta_qp_contexts,INIT_DELTA_QP_I[model_number][0]);
00161 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[0], INIT_MAP_I[model_number]);
00162 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[0], INIT_LAST_I[model_number]);
00163 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_ONE_CTX, tc->one_contexts, INIT_ONE_I[model_number]);
00164 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_ABS_CTX, tc->abs_contexts, INIT_ABS_I[model_number]);
00165 #if ENABLE_FIELD_CTX
00166 binary_context_init1 (qp, NUM_MB_AFF_CTX, tc->mb_aff_contexts, INIT_MB_AFF_I[model_number][0]);
00167 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[1], INIT_FLD_MAP_I[model_number]);
00168 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[1], INIT_FLD_LAST_I[model_number]);
00169 #endif
00170 }
00171 else
00172 {
00173
00174 BIARI_CTX_INIT2 (qp, 3, NUM_MB_TYPE_CTX, mc->mb_type_contexts, INIT_MB_TYPE_P[model_number]);
00175 BIARI_CTX_INIT2 (qp, 2, NUM_B8_TYPE_CTX, mc->b8_type_contexts, INIT_B8_TYPE_P[model_number]);
00176 BIARI_CTX_INIT2 (qp, 2, NUM_MV_RES_CTX, mc->mv_res_contexts, INIT_MV_RES_P[model_number]);
00177 BIARI_CTX_INIT2 (qp, 2, NUM_REF_NO_CTX, mc->ref_no_contexts, INIT_REF_NO_P[model_number]);
00178
00179
00180 binary_context_init1(qp, NUM_TRANSFORM_SIZE_CTX, tc->transform_size_contexts, INIT_TRANSFORM_SIZE_P[model_number][0]);
00181 binary_context_init1(qp, NUM_IPR_CTX, tc->ipr_contexts, INIT_IPR_P[model_number][0]);
00182 binary_context_init1(qp, NUM_CIPR_CTX, tc->cipr_contexts, INIT_CIPR_P[model_number][0]);
00183 BIARI_CTX_INIT2 (qp, 3, NUM_CBP_CTX, tc->cbp_contexts, INIT_CBP_P[model_number]);
00184 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_BCBP_CTX, tc->bcbp_contexts, INIT_BCBP_P[model_number]);
00185 binary_context_init1 (qp, NUM_DELTA_QP_CTX, tc->delta_qp_contexts,INIT_DELTA_QP_P[model_number][0]);
00186 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[0], INIT_MAP_P[model_number]);
00187 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[0], INIT_LAST_P[model_number]);
00188 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_ONE_CTX, tc->one_contexts, INIT_ONE_P[model_number]);
00189 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_ABS_CTX, tc->abs_contexts, INIT_ABS_P[model_number]);
00190 #if ENABLE_FIELD_CTX
00191 binary_context_init1 (qp, NUM_MB_AFF_CTX, tc->mb_aff_contexts, INIT_MB_AFF_P[model_number][0]);
00192 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[1], INIT_FLD_MAP_P[model_number]);
00193 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[1], INIT_FLD_LAST_P[model_number]);
00194 #endif
00195 }
00196 }
00197
00198
00199 double XRate (ImageParameters *p_Img, BiContextTypePtr ctx, const char* model)
00200 {
00201 int ctx_state, mod_state;
00202 double weight, xr = 0.0;
00203 int qp = imax(0, p_Img->qp);
00204
00205 weight = dmin (1.0, (double)ctx->count/(double)RELIABLE_COUNT);
00206
00207 mod_state = ((model[0]*qp)>>4)+model[1];
00208 mod_state = iClip3(0, 127, mod_state);
00209 ctx_state = (ctx->MPS ? 64 + ctx->state : 63 - ctx->state);
00210
00211 #if 0
00212 xr -= weight * p_Img->probability[ ctx_state] * p_Img->entropy[ mod_state];
00213 xr -= weight * p_Img->probability[127 - ctx_state] * p_Img->entropy[127 - mod_state];
00214 #else
00215
00216 xr -= weight * (p_Img->probability[ctx_state] * p_Img->enorm[mod_state] + p_Img->entropy[127 - mod_state]);
00217 #endif
00218
00219 return xr;
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 #define ADD_XRATE2(p_Img, ii,jj,ctx,tab) \
00235 { \
00236 for (i=0; i<ii; i++) \
00237 for (j=0; j<jj; j++) \
00238 { \
00239 xr += XRate (p_Img, &(ctx[i][j]), &(tab[i][j][0])); \
00240 } \
00241 }
00242
00243 #define ADD_XRATE1(p_Img, jj,ctx,tab) \
00244 { \
00245 for (j=0; j<jj; j++) \
00246 { \
00247 xr += XRate (p_Img, &(ctx[j]), &(tab[j][0])); \
00248 } \
00249 }
00250
00251 void GetCtxModelNumber (Slice *currSlice, int* mnumber, MotionInfoContexts* mc, TextureInfoContexts* tc)
00252 {
00253 ImageParameters *p_Img = currSlice->p_Img;
00254 int model, j, i;
00255 double xr, min_xr = 1e30;
00256
00257 if (currSlice->slice_type == I_SLICE)
00258 {
00259 for (model = 0; model < NUM_CTX_MODELS_I; model++)
00260 {
00261 xr = 0.0;
00262
00263 ADD_XRATE2 (p_Img, 3, NUM_MB_TYPE_CTX, mc->mb_type_contexts, INIT_MB_TYPE_I [model]);
00264
00265
00266 ADD_XRATE1 (p_Img, NUM_TRANSFORM_SIZE_CTX, tc->transform_size_contexts, INIT_TRANSFORM_SIZE_I[model][0]);
00267 ADD_XRATE1 (p_Img, NUM_IPR_CTX, tc->ipr_contexts, INIT_IPR_I [model][0]);
00268 ADD_XRATE1 (p_Img, NUM_CIPR_CTX, tc->cipr_contexts, INIT_CIPR_I[model][0]);
00269 ADD_XRATE2 (p_Img, 3, NUM_CBP_CTX, tc->cbp_contexts, INIT_CBP_I [model]);
00270 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_BCBP_CTX, tc->bcbp_contexts, INIT_BCBP_I[model]);
00271 ADD_XRATE1 (p_Img, NUM_DELTA_QP_CTX, tc->delta_qp_contexts, INIT_DELTA_QP_I[model][0]);
00272 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[0], INIT_MAP_I [model]);
00273 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[0], INIT_LAST_I[model]);
00274 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_ONE_CTX, tc->one_contexts, INIT_ONE_I [model]);
00275 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_ABS_CTX, tc->abs_contexts, INIT_ABS_I [model]);
00276 #if ENABLE_FIELD_CTX
00277 ADD_XRATE1 (p_Img, NUM_MB_AFF_CTX, tc->mb_aff_contexts, INIT_MB_AFF_I [model][0]);
00278 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[1], INIT_FLD_MAP_I [model]);
00279 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[1], INIT_FLD_LAST_I[model]);
00280 #endif
00281
00282 if (xr < min_xr)
00283 {
00284 min_xr = xr;
00285 *mnumber = model;
00286 }
00287 }
00288 }
00289 else
00290 {
00291 for (model=0; model<NUM_CTX_MODELS_P; model++)
00292 {
00293 xr = 0.0;
00294
00295 ADD_XRATE2 (p_Img, 3, NUM_MB_TYPE_CTX, mc->mb_type_contexts, INIT_MB_TYPE_P [model]);
00296 ADD_XRATE2 (p_Img, 2, NUM_B8_TYPE_CTX, mc->b8_type_contexts, INIT_B8_TYPE_P [model]);
00297 ADD_XRATE2 (p_Img, 2, NUM_MV_RES_CTX, mc->mv_res_contexts, INIT_MV_RES_P [model]);
00298 ADD_XRATE2 (p_Img, 2, NUM_REF_NO_CTX, mc->ref_no_contexts, INIT_REF_NO_P [model]);
00299
00300
00301 ADD_XRATE1 (p_Img, NUM_TRANSFORM_SIZE_CTX, tc->transform_size_contexts, INIT_TRANSFORM_SIZE_P[model][0]);
00302 ADD_XRATE1 (p_Img, NUM_IPR_CTX, tc->ipr_contexts, INIT_IPR_P [model][0]);
00303 ADD_XRATE1 (p_Img, NUM_CIPR_CTX, tc->cipr_contexts, INIT_CIPR_P [model][0]);
00304 ADD_XRATE2 (p_Img, 3, NUM_CBP_CTX, tc->cbp_contexts, INIT_CBP_P [model]);
00305 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_BCBP_CTX, tc->bcbp_contexts, INIT_BCBP_P [model]);
00306 ADD_XRATE1 (p_Img, NUM_DELTA_QP_CTX, tc->delta_qp_contexts, INIT_DELTA_QP_P[model][0]);
00307 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[0], INIT_MAP_P [model]);
00308 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[0], INIT_LAST_P [model]);
00309 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_ONE_CTX, tc->one_contexts, INIT_ONE_P [model]);
00310 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_ABS_CTX, tc->abs_contexts, INIT_ABS_P [model]);
00311 #if ENABLE_FIELD_CTX
00312 ADD_XRATE1 (p_Img, NUM_MB_AFF_CTX, tc->mb_aff_contexts, INIT_MB_AFF_P [model][0]);
00313 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[1], INIT_FLD_MAP_P [model]);
00314 ADD_XRATE2 (p_Img, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[1], INIT_FLD_LAST_P [model]);
00315 #endif
00316
00317 if (xr < min_xr)
00318 {
00319 min_xr = xr;
00320 *mnumber = model;
00321 }
00322 }
00323 }
00324 }
00325
00326 #undef ADD_XRATE2
00327 #undef ADD_XRATE1
00328
00329
00330 void store_contexts (Slice *currSlice)
00331 {
00332 if( currSlice->p_Inp->context_init_method )
00333 {
00334 ImageParameters *p_Img = currSlice->p_Img;
00335 int frame_field = p_Img->field_picture;
00336 int img_type = currSlice->slice_type;
00337 int ctx_number = currSlice->start_mb_nr / p_Img->num_mb_per_slice;
00338
00339 p_Img->initialized [frame_field][img_type][ctx_number] = 1;
00340 GetCtxModelNumber (currSlice, p_Img->modelNumber[frame_field][img_type] + ctx_number, currSlice->mot_ctx, currSlice->tex_ctx);
00341 }
00342 else
00343 {
00344
00345 }
00346 }
00347
00348
00349 void update_field_frame_contexts (ImageParameters *p_Img, int field)
00350 {
00351 int i, j;
00352
00353 if (field)
00354 {
00355
00356 for (j=0; j<FRAME_TYPES; j++)
00357 {
00358 for (i=0; i<p_Img->number_of_slices; i++)
00359 {
00360 p_Img->initialized [0][j][i] = p_Img->initialized [1][j][i>>1];
00361 p_Img->modelNumber[0][j][i] = p_Img->modelNumber[1][j][i>>1];
00362 }
00363 }
00364 }
00365 else
00366 {
00367
00368 for (j=0; j<FRAME_TYPES; j++)
00369 {
00370 for (i=0; i<((p_Img->number_of_slices+1)>>1); i++)
00371 {
00372 p_Img->initialized [1][j][i] = p_Img->initialized [0][j][i<<1];
00373 p_Img->modelNumber[1][j][i] = p_Img->modelNumber[0][j][i<<1];
00374 }
00375 }
00376 }
00377 }
00378