1
2 /*!
3 *************************************************************************************
4 * \file context_ini.c
5 *
6 * \brief
7 * CABAC context initializations
8 *
9 * \author
10 * Main contributors (see contributors.h for copyright, address and affiliation details)
11 * - Detlev Marpe
12 * - Heiko Schwarz
13 **************************************************************************************
14 */
15
16 #define CONTEXT_INI_C
17
18 #include <math.h>
19
20 #include "global.h"
21
22 #include "ctx_tables.h"
23 #include "biariencode.h"
24 #include "memalloc.h"
25
26 #define DEFAULT_CTX_MODEL 0
27 #define RELIABLE_COUNT 32.0
28 #define FRAME_TYPES 4
29 #define FIXED 0
30
31 // These essentially are constants
32 static const double probabilities[64] =
33 {
34 0.500000, 0.474609, 0.450507, 0.427629, 0.405912, 0.385299, 0.365732, 0.347159,
35 0.329530, 0.312795, 0.296911, 0.281833, 0.267520, 0.253935, 0.241039, 0.228799,
36 0.217180, 0.206151, 0.195682, 0.185744, 0.176312, 0.167358, 0.158859, 0.150792,
37 0.143134, 0.135866, 0.128966, 0.122417, 0.116200, 0.110299, 0.104698, 0.099381,
38 0.094334, 0.089543, 0.084996, 0.080680, 0.076583, 0.072694, 0.069002, 0.065498,
39 0.062172, 0.059014, 0.056018, 0.053173, 0.050473, 0.047909, 0.045476, 0.043167,
40 0.040975, 0.038894, 0.036919, 0.035044, 0.033264, 0.031575, 0.029972, 0.028450,
41 0.027005, 0.025633, 0.024332, 0.023096, 0.021923, 0.020810, 0.019753, 0.018750
42 };
43
create_context_memory(VideoParameters * p_Vid,InputParameters * p_Inp)44 void create_context_memory (VideoParameters *p_Vid, InputParameters *p_Inp)
45 {
46 int k;
47 int num_mb = p_Vid->FrameSizeInMbs; // number of macroblocks for frame
48 double log2 = log(2.0);
49
50 p_Vid->num_mb_per_slice = (p_Inp->slice_mode == 1 ? p_Inp->slice_argument : num_mb);
51 p_Vid->number_of_slices = (num_mb + p_Vid->num_mb_per_slice - 1) / p_Vid->num_mb_per_slice;
52 get_mem3Dint(&p_Vid->initialized, 3, FRAME_TYPES, p_Vid->number_of_slices);
53 //===== set all context sets as "uninitialized" =====
54 memset(&p_Vid->initialized[0][0][0], 0, 3 * FRAME_TYPES * p_Vid->number_of_slices * sizeof(int));
55 get_mem3Dint(&p_Vid->modelNumber, 3, FRAME_TYPES, p_Vid->number_of_slices);
56
57 //----- init tables -----
58 for( k=0; k<64; k++ )
59 {
60 p_Vid->probability[k + 64] = probabilities[k];
61 p_Vid->probability[k] = 1.0 - probabilities[63 - k];
62 p_Vid->entropy [k] = log( p_Vid->probability[ k] ) / log2;
63 p_Vid->entropy[127-k] = log( probabilities[63 - k] ) / log2;
64 p_Vid->enorm [k] = p_Vid->entropy[k] - p_Vid->entropy[127-k];
65 p_Vid->enorm[127 - k] = -p_Vid->enorm[k];
66 }
67 }
68
free_context_memory(VideoParameters * p_Vid)69 void free_context_memory (VideoParameters *p_Vid)
70 {
71 free_mem3Dint(p_Vid->initialized);
72 free_mem3Dint(p_Vid->modelNumber);
73 }
74
75 #define BIARI_CTX_INIT2(qp, ii,jj,ctx,tab) \
76 { \
77 for (i=0; i<ii; i++) \
78 for (j=0; j<jj; j++) \
79 { \
80 biari_init_context (qp, &(ctx[i][j]), &(tab[i][j][0])); \
81 } \
82 }
83
84
binary_context_init1(int qp,int jj,BiContextType * ctx,const char table[][2])85 static inline void binary_context_init1(int qp, int jj, BiContextType *ctx, const char table[][2])
86 {
87 int j;
88 for (j=0; j<jj; j++)
89 {
90 biari_init_context (qp, &(ctx[j]), &(table[j][0]));
91 }
92 }
93
binary_context_init2(int qp,int ii,int jj,BiContextType ctx[][11],const char table[][11][2])94 static inline void binary_context_init2(int qp, int ii, int jj, BiContextType ctx[][11], const char table[][11][2])
95 {
96 int i, j;
97 for (i = 0; i < ii; i++)
98 {
99 for (j = 0; j < jj; j++)
100 {
101 biari_init_context (qp, &(ctx[i][j]), &(table[i][j][0]));
102 }
103 }
104 }
105
SetCtxModelNumber(Slice * currSlice)106 void SetCtxModelNumber (Slice *currSlice)
107 {
108 VideoParameters *p_Vid = currSlice->p_Vid;
109 InputParameters *p_Inp = currSlice->p_Inp;
110
111 int frame_field = p_Vid->field_picture;
112 int img_type = currSlice->slice_type;
113 int ctx_number = currSlice->start_mb_nr / p_Vid->num_mb_per_slice;
114
115 if(img_type == I_SLICE)
116 {
117 currSlice->model_number=DEFAULT_CTX_MODEL;
118 return;
119 }
120
121 if(p_Inp->context_init_method==FIXED)
122 {
123 currSlice->model_number = p_Inp->model_number;
124 return;
125 }
126
127 if (p_Vid->initialized [frame_field][img_type][ctx_number])
128 {
129 currSlice->model_number = p_Vid->modelNumber[frame_field][img_type][ctx_number];
130 }
131 else if (ctx_number && p_Vid->initialized[frame_field][img_type][ctx_number-1])
132 {
133 currSlice->model_number = p_Vid->modelNumber[frame_field][img_type][ctx_number-1];
134 }
135 else
136 {
137 currSlice->model_number = DEFAULT_CTX_MODEL;
138 }
139 }
140
init_contexts(Slice * currSlice)141 void init_contexts (Slice *currSlice)
142 {
143 MotionInfoContexts* mc = currSlice->mot_ctx;
144 TextureInfoContexts* tc = currSlice->tex_ctx;
145 int model_number = currSlice->model_number;
146 int qp = imax(0, currSlice->qp);
147 int i, j;
148
149 if ((currSlice->slice_type == I_SLICE) || (currSlice->slice_type == SI_SLICE))
150 {
151 //--- motion coding contexts ---
152 BIARI_CTX_INIT2 (qp, 3, NUM_MB_TYPE_CTX, mc->mb_type_contexts, INIT_MB_TYPE_I[model_number]);
153
154 //--- texture coding contexts ---
155 binary_context_init1 (qp, NUM_TRANSFORM_SIZE_CTX, tc->transform_size_contexts, INIT_TRANSFORM_SIZE_I[model_number][0]);
156 binary_context_init1 (qp, NUM_IPR_CTX, tc->ipr_contexts, INIT_IPR_I[model_number][0]);
157 binary_context_init1 (qp, NUM_CIPR_CTX, tc->cipr_contexts, INIT_CIPR_I[model_number][0]);
158 BIARI_CTX_INIT2 (qp, 3, NUM_CBP_CTX, tc->cbp_contexts, INIT_CBP_I[model_number]);
159 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_BCBP_CTX, tc->bcbp_contexts, INIT_BCBP_I[model_number]);
160 binary_context_init1 (qp, NUM_DELTA_QP_CTX, tc->delta_qp_contexts,INIT_DELTA_QP_I[model_number][0]);
161 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[0], INIT_MAP_I[model_number]);
162 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[0], INIT_LAST_I[model_number]);
163 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_ONE_CTX, tc->one_contexts, INIT_ONE_I[model_number]);
164 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_ABS_CTX, tc->abs_contexts, INIT_ABS_I[model_number]);
165 #if ENABLE_FIELD_CTX
166 binary_context_init1 (qp, NUM_MB_AFF_CTX, tc->mb_aff_contexts, INIT_MB_AFF_I[model_number][0]);
167 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[1], INIT_FLD_MAP_I[model_number]);
168 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[1], INIT_FLD_LAST_I[model_number]);
169 #endif
170 }
171 else
172 {
173 //--- motion coding contexts ---
174 BIARI_CTX_INIT2 (qp, 3, NUM_MB_TYPE_CTX, mc->mb_type_contexts, INIT_MB_TYPE_P[model_number]);
175 BIARI_CTX_INIT2 (qp, 2, NUM_B8_TYPE_CTX, mc->b8_type_contexts, INIT_B8_TYPE_P[model_number]);
176 BIARI_CTX_INIT2 (qp, 2, NUM_MV_RES_CTX, mc->mv_res_contexts, INIT_MV_RES_P[model_number]);
177 BIARI_CTX_INIT2 (qp, 2, NUM_REF_NO_CTX, mc->ref_no_contexts, INIT_REF_NO_P[model_number]);
178
179 //--- texture coding contexts ---
180 binary_context_init1(qp, NUM_TRANSFORM_SIZE_CTX, tc->transform_size_contexts, INIT_TRANSFORM_SIZE_P[model_number][0]);
181 binary_context_init1(qp, NUM_IPR_CTX, tc->ipr_contexts, INIT_IPR_P[model_number][0]);
182 binary_context_init1(qp, NUM_CIPR_CTX, tc->cipr_contexts, INIT_CIPR_P[model_number][0]);
183 BIARI_CTX_INIT2 (qp, 3, NUM_CBP_CTX, tc->cbp_contexts, INIT_CBP_P[model_number]);
184 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_BCBP_CTX, tc->bcbp_contexts, INIT_BCBP_P[model_number]);
185 binary_context_init1 (qp, NUM_DELTA_QP_CTX, tc->delta_qp_contexts,INIT_DELTA_QP_P[model_number][0]);
186 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[0], INIT_MAP_P[model_number]);
187 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[0], INIT_LAST_P[model_number]);
188 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_ONE_CTX, tc->one_contexts, INIT_ONE_P[model_number]);
189 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_ABS_CTX, tc->abs_contexts, INIT_ABS_P[model_number]);
190 #if ENABLE_FIELD_CTX
191 binary_context_init1 (qp, NUM_MB_AFF_CTX, tc->mb_aff_contexts, INIT_MB_AFF_P[model_number][0]);
192 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[1], INIT_FLD_MAP_P[model_number]);
193 BIARI_CTX_INIT2 (qp, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[1], INIT_FLD_LAST_P[model_number]);
194 #endif
195 }
196 }
197
198
XRate(VideoParameters * p_Vid,BiContextTypePtr ctx,const char * model)199 double XRate (VideoParameters *p_Vid, BiContextTypePtr ctx, const char* model)
200 {
201 int ctx_state, mod_state;
202 double weight, xr = 0.0;
203 int qp = imax(0, p_Vid->qp);
204
205 weight = dmin (1.0, (double)ctx->count/(double)RELIABLE_COUNT);
206
207 mod_state = ((model[0]*qp)>>4)+model[1];
208 mod_state = iClip3(0, 127, mod_state);
209 ctx_state = (ctx->MPS ? 64 + ctx->state : 63 - ctx->state);
210
211 xr -= weight * (p_Vid->probability[ctx_state] * p_Vid->enorm[mod_state] + p_Vid->entropy[127 - mod_state]);
212
213 return xr;
214 }
215
216 /*
217 static inline void add_xrate2(VideoParameters *p_Vid, int ii, int jj, BiContextType **ctx, const char ***tab, double *xr)
218 {
219 int i, j;
220 for (i=0; i<ii; i++)
221 for (j=0; j<jj; j++)
222 {
223 *xr += XRate (p_Vid, &(ctx[i][j]), &(tab[i][j][0]));
224 }
225 }
226 */
227
228 #define ADD_XRATE2(p_Vid, ii,jj,ctx,tab) \
229 { \
230 for (i=0; i<ii; i++) \
231 for (j=0; j<jj; j++) \
232 { \
233 xr += XRate (p_Vid, &(ctx[i][j]), &(tab[i][j][0])); \
234 } \
235 }
236
237 #define ADD_XRATE1(p_Vid, jj,ctx,tab) \
238 { \
239 for (j=0; j<jj; j++) \
240 { \
241 xr += XRate (p_Vid, &(ctx[j]), &(tab[j][0])); \
242 } \
243 }
244
GetCtxModelNumber(Slice * currSlice,int * mnumber,MotionInfoContexts * mc,TextureInfoContexts * tc)245 void GetCtxModelNumber (Slice *currSlice, int* mnumber, MotionInfoContexts* mc, TextureInfoContexts* tc)
246 {
247 VideoParameters *p_Vid = currSlice->p_Vid;
248 int model, j, i;
249 double xr, min_xr = 1e30;
250
251 if (currSlice->slice_type == I_SLICE)
252 {
253 for (model = 0; model < NUM_CTX_MODELS_I; model++)
254 {
255 xr = 0.0;
256 //--- motion coding contexts ---
257 ADD_XRATE2 (p_Vid, 3, NUM_MB_TYPE_CTX, mc->mb_type_contexts, INIT_MB_TYPE_I [model]);
258
259 //--- texture coding contexts ---
260 ADD_XRATE1 (p_Vid, NUM_TRANSFORM_SIZE_CTX, tc->transform_size_contexts, INIT_TRANSFORM_SIZE_I[model][0]);
261 ADD_XRATE1 (p_Vid, NUM_IPR_CTX, tc->ipr_contexts, INIT_IPR_I [model][0]);
262 ADD_XRATE1 (p_Vid, NUM_CIPR_CTX, tc->cipr_contexts, INIT_CIPR_I[model][0]);
263 ADD_XRATE2 (p_Vid, 3, NUM_CBP_CTX, tc->cbp_contexts, INIT_CBP_I [model]);
264 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_BCBP_CTX, tc->bcbp_contexts, INIT_BCBP_I[model]);
265 ADD_XRATE1 (p_Vid, NUM_DELTA_QP_CTX, tc->delta_qp_contexts, INIT_DELTA_QP_I[model][0]);
266 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[0], INIT_MAP_I [model]);
267 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[0], INIT_LAST_I[model]);
268 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_ONE_CTX, tc->one_contexts, INIT_ONE_I [model]);
269 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_ABS_CTX, tc->abs_contexts, INIT_ABS_I [model]);
270 #if ENABLE_FIELD_CTX
271 ADD_XRATE1 (p_Vid, NUM_MB_AFF_CTX, tc->mb_aff_contexts, INIT_MB_AFF_I [model][0]);
272 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[1], INIT_FLD_MAP_I [model]);
273 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[1], INIT_FLD_LAST_I[model]);
274 #endif
275
276 if (xr < min_xr)
277 {
278 min_xr = xr;
279 *mnumber = model;
280 }
281 }
282 }
283 else
284 {
285 for (model=0; model<NUM_CTX_MODELS_P; model++)
286 {
287 xr = 0.0;
288 //--- motion coding contexts ---
289 ADD_XRATE2 (p_Vid, 3, NUM_MB_TYPE_CTX, mc->mb_type_contexts, INIT_MB_TYPE_P [model]);
290 ADD_XRATE2 (p_Vid, 2, NUM_B8_TYPE_CTX, mc->b8_type_contexts, INIT_B8_TYPE_P [model]);
291 ADD_XRATE2 (p_Vid, 2, NUM_MV_RES_CTX, mc->mv_res_contexts, INIT_MV_RES_P [model]);
292 ADD_XRATE2 (p_Vid, 2, NUM_REF_NO_CTX, mc->ref_no_contexts, INIT_REF_NO_P [model]);
293
294 //--- texture coding contexts ---
295 ADD_XRATE1 (p_Vid, NUM_TRANSFORM_SIZE_CTX, tc->transform_size_contexts, INIT_TRANSFORM_SIZE_P[model][0]);
296 ADD_XRATE1 (p_Vid, NUM_IPR_CTX, tc->ipr_contexts, INIT_IPR_P [model][0]);
297 ADD_XRATE1 (p_Vid, NUM_CIPR_CTX, tc->cipr_contexts, INIT_CIPR_P [model][0]);
298 ADD_XRATE2 (p_Vid, 3, NUM_CBP_CTX, tc->cbp_contexts, INIT_CBP_P [model]);
299 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_BCBP_CTX, tc->bcbp_contexts, INIT_BCBP_P [model]);
300 ADD_XRATE1 (p_Vid, NUM_DELTA_QP_CTX, tc->delta_qp_contexts, INIT_DELTA_QP_P[model][0]);
301 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[0], INIT_MAP_P [model]);
302 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[0], INIT_LAST_P [model]);
303 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_ONE_CTX, tc->one_contexts, INIT_ONE_P [model]);
304 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_ABS_CTX, tc->abs_contexts, INIT_ABS_P [model]);
305 #if ENABLE_FIELD_CTX
306 ADD_XRATE1 (p_Vid, NUM_MB_AFF_CTX, tc->mb_aff_contexts, INIT_MB_AFF_P [model][0]);
307 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts[1], INIT_FLD_MAP_P [model]);
308 ADD_XRATE2 (p_Vid, NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts[1], INIT_FLD_LAST_P [model]);
309 #endif
310
311 if (xr < min_xr)
312 {
313 min_xr = xr;
314 *mnumber = model;
315 }
316 }
317 }
318 }
319
320 #undef ADD_XRATE2
321 #undef ADD_XRATE1
322
323
store_contexts(Slice * currSlice)324 void store_contexts (Slice *currSlice)
325 {
326 if( currSlice->p_Inp->context_init_method )
327 {
328 VideoParameters *p_Vid = currSlice->p_Vid;
329 int frame_field = p_Vid->field_picture;
330 int img_type = currSlice->slice_type;
331 int ctx_number = currSlice->start_mb_nr / p_Vid->num_mb_per_slice;
332
333 p_Vid->initialized [frame_field][img_type][ctx_number] = 1;
334 GetCtxModelNumber (currSlice, p_Vid->modelNumber[frame_field][img_type] + ctx_number, currSlice->mot_ctx, currSlice->tex_ctx);
335 }
336 else
337 {
338 // do nothing
339 }
340 }
341
342
update_field_frame_contexts(VideoParameters * p_Vid,int field)343 void update_field_frame_contexts (VideoParameters *p_Vid, int field)
344 {
345 int i, j;
346
347 if (field)
348 {
349 // set frame contexts
350 for (j=0; j<FRAME_TYPES; j++)
351 {
352 for (i=0; i<p_Vid->number_of_slices; i++)
353 {
354 p_Vid->initialized [0][j][i] = p_Vid->initialized [1][j][i>>1];
355 p_Vid->modelNumber[0][j][i] = p_Vid->modelNumber[1][j][i>>1];
356 }
357 }
358 }
359 else
360 {
361 // set field contexts
362 for (j=0; j<FRAME_TYPES; j++)
363 {
364 for (i=0; i<((p_Vid->number_of_slices+1)>>1); i++)
365 {
366 p_Vid->initialized [1][j][i] = p_Vid->initialized [0][j][i<<1];
367 p_Vid->modelNumber[1][j][i] = p_Vid->modelNumber[0][j][i<<1];
368 }
369 }
370 }
371 }
372
373