00001
00016 #include "contributors.h"
00017
00018 #include "global.h"
00019 #include "memalloc.h"
00020 #include "block.h"
00021 #include "image.h"
00022 #include "mb_access.h"
00023 #include "transform.h"
00024 #include "quant.h"
00025
00026 const int dequant_coef8[6][8][8] =
00027 {
00028 {
00029 {20, 19, 25, 19, 20, 19, 25, 19},
00030 {19, 18, 24, 18, 19, 18, 24, 18},
00031 {25, 24, 32, 24, 25, 24, 32, 24},
00032 {19, 18, 24, 18, 19, 18, 24, 18},
00033 {20, 19, 25, 19, 20, 19, 25, 19},
00034 {19, 18, 24, 18, 19, 18, 24, 18},
00035 {25, 24, 32, 24, 25, 24, 32, 24},
00036 {19, 18, 24, 18, 19, 18, 24, 18}
00037 },
00038 {
00039 {22, 21, 28, 21, 22, 21, 28, 21},
00040 {21, 19, 26, 19, 21, 19, 26, 19},
00041 {28, 26, 35, 26, 28, 26, 35, 26},
00042 {21, 19, 26, 19, 21, 19, 26, 19},
00043 {22, 21, 28, 21, 22, 21, 28, 21},
00044 {21, 19, 26, 19, 21, 19, 26, 19},
00045 {28, 26, 35, 26, 28, 26, 35, 26},
00046 {21, 19, 26, 19, 21, 19, 26, 19}
00047 },
00048 {
00049 {26, 24, 33, 24, 26, 24, 33, 24},
00050 {24, 23, 31, 23, 24, 23, 31, 23},
00051 {33, 31, 42, 31, 33, 31, 42, 31},
00052 {24, 23, 31, 23, 24, 23, 31, 23},
00053 {26, 24, 33, 24, 26, 24, 33, 24},
00054 {24, 23, 31, 23, 24, 23, 31, 23},
00055 {33, 31, 42, 31, 33, 31, 42, 31},
00056 {24, 23, 31, 23, 24, 23, 31, 23}
00057 },
00058 {
00059 {28, 26, 35, 26, 28, 26, 35, 26},
00060 {26, 25, 33, 25, 26, 25, 33, 25},
00061 {35, 33, 45, 33, 35, 33, 45, 33},
00062 {26, 25, 33, 25, 26, 25, 33, 25},
00063 {28, 26, 35, 26, 28, 26, 35, 26},
00064 {26, 25, 33, 25, 26, 25, 33, 25},
00065 {35, 33, 45, 33, 35, 33, 45, 33},
00066 {26, 25, 33, 25, 26, 25, 33, 25}
00067 },
00068 {
00069 {32, 30, 40, 30, 32, 30, 40, 30},
00070 {30, 28, 38, 28, 30, 28, 38, 28},
00071 {40, 38, 51, 38, 40, 38, 51, 38},
00072 {30, 28, 38, 28, 30, 28, 38, 28},
00073 {32, 30, 40, 30, 32, 30, 40, 30},
00074 {30, 28, 38, 28, 30, 28, 38, 28},
00075 {40, 38, 51, 38, 40, 38, 51, 38},
00076 {30, 28, 38, 28, 30, 28, 38, 28}
00077 },
00078 {
00079 {36, 34, 46, 34, 36, 34, 46, 34},
00080 {34, 32, 43, 32, 34, 32, 43, 32},
00081 {46, 43, 58, 43, 46, 43, 58, 43},
00082 {34, 32, 43, 32, 34, 32, 43, 32},
00083 {36, 34, 46, 34, 36, 34, 46, 34},
00084 {34, 32, 43, 32, 34, 32, 43, 32},
00085 {46, 43, 58, 43, 46, 43, 58, 43},
00086 {34, 32, 43, 32, 34, 32, 43, 32}
00087 }
00088 };
00089
00091 const int dequant_coef[6][4][4] = {
00092 {
00093 { 10, 13, 10, 13},
00094 { 13, 16, 13, 16},
00095 { 10, 13, 10, 13},
00096 { 13, 16, 13, 16}},
00097 {
00098 { 11, 14, 11, 14},
00099 { 14, 18, 14, 18},
00100 { 11, 14, 11, 14},
00101 { 14, 18, 14, 18}},
00102 {
00103 { 13, 16, 13, 16},
00104 { 16, 20, 16, 20},
00105 { 13, 16, 13, 16},
00106 { 16, 20, 16, 20}},
00107 {
00108 { 14, 18, 14, 18},
00109 { 18, 23, 18, 23},
00110 { 14, 18, 14, 18},
00111 { 18, 23, 18, 23}},
00112 {
00113 { 16, 20, 16, 20},
00114 { 20, 25, 20, 25},
00115 { 16, 20, 16, 20},
00116 { 20, 25, 20, 25}},
00117 {
00118 { 18, 23, 18, 23},
00119 { 23, 29, 23, 29},
00120 { 18, 23, 18, 23},
00121 { 23, 29, 23, 29}}
00122 };
00123
00124 const int quant_coef[6][4][4] = {
00125 {
00126 { 13107, 8066, 13107, 8066},
00127 { 8066, 5243, 8066, 5243},
00128 { 13107, 8066, 13107, 8066},
00129 { 8066, 5243, 8066, 5243}},
00130 {
00131 { 11916, 7490, 11916, 7490},
00132 { 7490, 4660, 7490, 4660},
00133 { 11916, 7490, 11916, 7490},
00134 { 7490, 4660, 7490, 4660}},
00135 {
00136 { 10082, 6554, 10082, 6554},
00137 { 6554, 4194, 6554, 4194},
00138 { 10082, 6554, 10082, 6554},
00139 { 6554, 4194, 6554, 4194}},
00140 {
00141 { 9362, 5825, 9362, 5825},
00142 { 5825, 3647, 5825, 3647},
00143 { 9362, 5825, 9362, 5825},
00144 { 5825, 3647, 5825, 3647}},
00145 {
00146 { 8192, 5243, 8192, 5243},
00147 { 5243, 3355, 5243, 3355},
00148 { 8192, 5243, 8192, 5243},
00149 { 5243, 3355, 5243, 3355}},
00150 {
00151 { 7282, 4559, 7282, 4559},
00152 { 4559, 2893, 4559, 2893},
00153 { 7282, 4559, 7282, 4559},
00154 { 4559, 2893, 4559, 2893}}
00155 };
00156
00157 const int A[4][4] = {
00158 { 16, 20, 16, 20},
00159 { 20, 25, 20, 25},
00160 { 16, 20, 16, 20},
00161 { 20, 25, 20, 25}
00162 };
00163
00164 int quant_intra_default[16] = {
00165 6,13,20,28,
00166 13,20,28,32,
00167 20,28,32,37,
00168 28,32,37,42
00169 };
00170
00171 int quant_inter_default[16] = {
00172 10,14,20,24,
00173 14,20,24,27,
00174 20,24,27,30,
00175 24,27,30,34
00176 };
00177
00178 int quant8_intra_default[64] = {
00179 6,10,13,16,18,23,25,27,
00180 10,11,16,18,23,25,27,29,
00181 13,16,18,23,25,27,29,31,
00182 16,18,23,25,27,29,31,33,
00183 18,23,25,27,29,31,33,36,
00184 23,25,27,29,31,33,36,38,
00185 25,27,29,31,33,36,38,40,
00186 27,29,31,33,36,38,40,42
00187 };
00188
00189 int quant8_inter_default[64] = {
00190 9,13,15,17,19,21,22,24,
00191 13,13,17,19,21,22,24,25,
00192 15,17,19,21,22,24,25,27,
00193 17,19,21,22,24,25,27,28,
00194 19,21,22,24,25,27,28,30,
00195 21,22,24,25,27,28,30,32,
00196 22,24,25,27,28,30,32,33,
00197 24,25,27,28,30,32,33,35
00198 };
00199
00200 int quant_org[16] = {
00201 16,16,16,16,
00202 16,16,16,16,
00203 16,16,16,16,
00204 16,16,16,16
00205 };
00206
00207 int quant8_org[64] = {
00208 16,16,16,16,16,16,16,16,
00209 16,16,16,16,16,16,16,16,
00210 16,16,16,16,16,16,16,16,
00211 16,16,16,16,16,16,16,16,
00212 16,16,16,16,16,16,16,16,
00213 16,16,16,16,16,16,16,16,
00214 16,16,16,16,16,16,16,16,
00215 16,16,16,16,16,16,16,16
00216 };
00217
00218 int *qp_per_matrix;
00219 int *qp_rem_matrix;
00220
00221 int InvLevelScale4x4_Intra[3][6][4][4];
00222 int InvLevelScale4x4_Inter[3][6][4][4];
00223 int InvLevelScale8x8_Intra[3][6][8][8];
00224 int InvLevelScale8x8_Inter[3][6][8][8];
00225
00226 int *qmatrix[12];
00227
00234 void init_qp_process(ImageParameters *img)
00235 {
00236 int bitdepth_qp_scale = imax(img->bitdepth_luma_qp_scale,img->bitdepth_chroma_qp_scale);
00237 int i;
00238
00239
00240
00241 if (qp_per_matrix == NULL)
00242 if ((qp_per_matrix = (int*)malloc((MAX_QP + 1 + bitdepth_qp_scale)*sizeof(int))) == NULL)
00243 no_mem_exit("init_qp_process: qp_per_matrix");
00244
00245 if (qp_rem_matrix == NULL)
00246 if ((qp_rem_matrix = (int*)malloc((MAX_QP + 1 + bitdepth_qp_scale)*sizeof(int))) == NULL)
00247 no_mem_exit("init_qp_process: qp_per_matrix");
00248
00249 for (i = 0; i < MAX_QP + bitdepth_qp_scale + 1; i++)
00250 {
00251 qp_per_matrix[i] = i / 6;
00252 qp_rem_matrix[i] = i % 6;
00253 }
00254 }
00255
00256 void free_qp_matrices(void)
00257 {
00258 if (qp_per_matrix != NULL)
00259 free (qp_per_matrix);
00260
00261 if (qp_rem_matrix != NULL)
00262 free (qp_rem_matrix);
00263 }
00264
00277 void AssignQuantParam(pic_parameter_set_rbsp_t* pps, seq_parameter_set_rbsp_t* sps)
00278 {
00279 int i;
00280 int n_ScalingList;
00281
00282 if(!pps->pic_scaling_matrix_present_flag && !sps->seq_scaling_matrix_present_flag)
00283 {
00284 for(i=0; i<12; i++)
00285 qmatrix[i] = (i < 6) ? quant_org : quant8_org;
00286 }
00287 else
00288 {
00289 n_ScalingList = (sps->chroma_format_idc != YUV444) ? 8 : 12;
00290 if(sps->seq_scaling_matrix_present_flag)
00291 {
00292 for(i=0; i<n_ScalingList; i++)
00293 {
00294 if(i<6)
00295 {
00296 if(!sps->seq_scaling_list_present_flag[i])
00297 {
00298 if(i==0)
00299 qmatrix[i] = quant_intra_default;
00300 else if(i==3)
00301 qmatrix[i] = quant_inter_default;
00302 else
00303 qmatrix[i] = qmatrix[i-1];
00304 }
00305 else
00306 {
00307 if(sps->UseDefaultScalingMatrix4x4Flag[i])
00308 qmatrix[i] = (i<3) ? quant_intra_default : quant_inter_default;
00309 else
00310 qmatrix[i] = sps->ScalingList4x4[i];
00311 }
00312 }
00313 else
00314 {
00315 if(!sps->seq_scaling_list_present_flag[i])
00316 {
00317 if(i==6)
00318 qmatrix[i] = quant8_intra_default;
00319 else if(i==7)
00320 qmatrix[i] = quant8_inter_default;
00321 else
00322 qmatrix[i] = qmatrix[i-2];
00323 }
00324 else
00325 {
00326 if(sps->UseDefaultScalingMatrix8x8Flag[i-6])
00327 qmatrix[i] = (i==6 || i==8 || i==10) ? quant8_intra_default:quant8_inter_default;
00328 else
00329 qmatrix[i] = sps->ScalingList8x8[i-6];
00330 }
00331 }
00332 }
00333 }
00334
00335 if(pps->pic_scaling_matrix_present_flag)
00336 {
00337 for(i=0; i<n_ScalingList; i++)
00338 {
00339 if(i<6)
00340 {
00341 if(!pps->pic_scaling_list_present_flag[i])
00342 {
00343 if (i==0)
00344 {
00345 if(!sps->seq_scaling_matrix_present_flag)
00346 qmatrix[i] = quant_intra_default;
00347 }
00348 else if (i==3)
00349 {
00350 if(!sps->seq_scaling_matrix_present_flag)
00351 qmatrix[i] = quant_inter_default;
00352 }
00353 else
00354 qmatrix[i] = qmatrix[i-1];
00355 }
00356 else
00357 {
00358 if(pps->UseDefaultScalingMatrix4x4Flag[i])
00359 qmatrix[i] = (i<3) ? quant_intra_default:quant_inter_default;
00360 else
00361 qmatrix[i] = pps->ScalingList4x4[i];
00362 }
00363 }
00364 else
00365 {
00366 if(!pps->pic_scaling_list_present_flag[i])
00367 {
00368 if (i==6)
00369 {
00370 if(!sps->seq_scaling_matrix_present_flag)
00371 qmatrix[i] = quant8_intra_default;
00372 }
00373 else if(i==7)
00374 {
00375 if(!sps->seq_scaling_matrix_present_flag)
00376 qmatrix[i] = quant8_inter_default;
00377 }
00378 else
00379 qmatrix[i] = qmatrix[i-2];
00380 }
00381 else
00382 {
00383 if(pps->UseDefaultScalingMatrix8x8Flag[i-6])
00384 qmatrix[i] = (i==6 || i==8 || i==10) ? quant8_intra_default:quant8_inter_default;
00385 else
00386 qmatrix[i] = pps->ScalingList8x8[i-6];
00387 }
00388 }
00389 }
00390 }
00391 }
00392
00393 CalculateQuantParam();
00394 if(pps->transform_8x8_mode_flag)
00395 CalculateQuant8Param();
00396 }
00397
00405 void CalculateQuantParam()
00406 {
00407 int i, j, k, temp;
00408
00409 for(k=0; k<6; k++)
00410 {
00411 for(i=0; i<4; i++)
00412 {
00413 for(j=0; j<4; j++)
00414 {
00415 temp = (i<<2)+j;
00416 InvLevelScale4x4_Intra[0][k][i][j] = dequant_coef[k][i][j] * qmatrix[0][temp];
00417 InvLevelScale4x4_Intra[1][k][i][j] = dequant_coef[k][i][j] * qmatrix[1][temp];
00418 InvLevelScale4x4_Intra[2][k][i][j] = dequant_coef[k][i][j] * qmatrix[2][temp];
00419
00420 InvLevelScale4x4_Inter[0][k][i][j] = dequant_coef[k][i][j] * qmatrix[3][temp];
00421 InvLevelScale4x4_Inter[1][k][i][j] = dequant_coef[k][i][j] * qmatrix[4][temp];
00422 InvLevelScale4x4_Inter[2][k][i][j] = dequant_coef[k][i][j] * qmatrix[5][temp];
00423 }
00424 }
00425 }
00426 }
00427
00435 void CalculateQuant8Param()
00436 {
00437 int i, j, k, temp;
00438
00439 for(k=0; k<6; k++)
00440 {
00441 for(i=0; i<8; i++)
00442 {
00443 for(j=0; j<8; j++)
00444 {
00445 temp = (i<<3)+j;
00446 InvLevelScale8x8_Intra[0][k][i][j] = dequant_coef8[k][i][j] * qmatrix[6][temp];
00447 InvLevelScale8x8_Inter[0][k][i][j] = dequant_coef8[k][i][j] * qmatrix[7][temp];
00448 }
00449 }
00450 }
00451
00452 if( active_sps->chroma_format_idc == 3 )
00453 {
00454 for(k=0; k<6; k++)
00455 {
00456 for(i=0; i<8; i++)
00457 {
00458 for(j=0; j<8; j++)
00459 {
00460 temp = (i<<3)+j;
00461 InvLevelScale8x8_Intra[1][k][i][j] = dequant_coef8[k][i][j] * qmatrix[8][temp];
00462 InvLevelScale8x8_Inter[1][k][i][j] = dequant_coef8[k][i][j] * qmatrix[9][temp];
00463 InvLevelScale8x8_Intra[2][k][i][j] = dequant_coef8[k][i][j] * qmatrix[10][temp];
00464 InvLevelScale8x8_Inter[2][k][i][j] = dequant_coef8[k][i][j] * qmatrix[11][temp];
00465 }
00466 }
00467 }
00468 }
00469 }