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 int quant_intra_default[16] = {
00027 6,13,20,28,
00028 13,20,28,32,
00029 20,28,32,37,
00030 28,32,37,42
00031 };
00032
00033 int quant_inter_default[16] = {
00034 10,14,20,24,
00035 14,20,24,27,
00036 20,24,27,30,
00037 24,27,30,34
00038 };
00039
00040 int quant8_intra_default[64] = {
00041 6,10,13,16,18,23,25,27,
00042 10,11,16,18,23,25,27,29,
00043 13,16,18,23,25,27,29,31,
00044 16,18,23,25,27,29,31,33,
00045 18,23,25,27,29,31,33,36,
00046 23,25,27,29,31,33,36,38,
00047 25,27,29,31,33,36,38,40,
00048 27,29,31,33,36,38,40,42
00049 };
00050
00051 int quant8_inter_default[64] = {
00052 9,13,15,17,19,21,22,24,
00053 13,13,17,19,21,22,24,25,
00054 15,17,19,21,22,24,25,27,
00055 17,19,21,22,24,25,27,28,
00056 19,21,22,24,25,27,28,30,
00057 21,22,24,25,27,28,30,32,
00058 22,24,25,27,28,30,32,33,
00059 24,25,27,28,30,32,33,35
00060 };
00061
00062 int quant_org[16] = {
00063 16,16,16,16,
00064 16,16,16,16,
00065 16,16,16,16,
00066 16,16,16,16
00067 };
00068
00069 int quant8_org[64] = {
00070 16,16,16,16,16,16,16,16,
00071 16,16,16,16,16,16,16,16,
00072 16,16,16,16,16,16,16,16,
00073 16,16,16,16,16,16,16,16,
00074 16,16,16,16,16,16,16,16,
00075 16,16,16,16,16,16,16,16,
00076 16,16,16,16,16,16,16,16,
00077 16,16,16,16,16,16,16,16
00078 };
00079
00086 void init_qp_process(ImageParameters *p_Img)
00087 {
00088 int bitdepth_qp_scale = imax(p_Img->bitdepth_luma_qp_scale,p_Img->bitdepth_chroma_qp_scale);
00089 int i;
00090
00091
00092
00093 if (p_Img->qp_per_matrix == NULL)
00094 if ((p_Img->qp_per_matrix = (int*)malloc((MAX_QP + 1 + bitdepth_qp_scale)*sizeof(int))) == NULL)
00095 no_mem_exit("init_qp_process: p_Img->qp_per_matrix");
00096
00097 if (p_Img->qp_rem_matrix == NULL)
00098 if ((p_Img->qp_rem_matrix = (int*)malloc((MAX_QP + 1 + bitdepth_qp_scale)*sizeof(int))) == NULL)
00099 no_mem_exit("init_qp_process: p_Img->qp_rem_matrix");
00100
00101 for (i = 0; i < MAX_QP + bitdepth_qp_scale + 1; i++)
00102 {
00103 p_Img->qp_per_matrix[i] = i / 6;
00104 p_Img->qp_rem_matrix[i] = i % 6;
00105 }
00106 }
00107
00108 void free_qp_matrices(ImageParameters *p_Img)
00109 {
00110 if (p_Img->qp_per_matrix != NULL)
00111 {
00112 free (p_Img->qp_per_matrix);
00113 p_Img->qp_per_matrix = NULL;
00114 }
00115
00116 if (p_Img->qp_rem_matrix != NULL)
00117 {
00118 free (p_Img->qp_rem_matrix);
00119 p_Img->qp_rem_matrix = NULL;
00120 }
00121 }
00122
00137 void assign_quant_params(Slice *currSlice, pic_parameter_set_rbsp_t* pps, seq_parameter_set_rbsp_t* sps)
00138 {
00139 int i;
00140 int n_ScalingList;
00141
00142 if(!pps->pic_scaling_matrix_present_flag && !sps->seq_scaling_matrix_present_flag)
00143 {
00144 for(i=0; i<12; i++)
00145 currSlice->qmatrix[i] = (i < 6) ? quant_org : quant8_org;
00146 }
00147 else
00148 {
00149 n_ScalingList = (sps->chroma_format_idc != YUV444) ? 8 : 12;
00150 if(sps->seq_scaling_matrix_present_flag)
00151 {
00152 for(i=0; i<n_ScalingList; i++)
00153 {
00154 if(i<6)
00155 {
00156 if(!sps->seq_scaling_list_present_flag[i])
00157 {
00158 if(i==0)
00159 currSlice->qmatrix[i] = quant_intra_default;
00160 else if(i==3)
00161 currSlice->qmatrix[i] = quant_inter_default;
00162 else
00163 currSlice->qmatrix[i] = currSlice->qmatrix[i-1];
00164 }
00165 else
00166 {
00167 if(sps->UseDefaultScalingMatrix4x4Flag[i])
00168 currSlice->qmatrix[i] = (i<3) ? quant_intra_default : quant_inter_default;
00169 else
00170 currSlice->qmatrix[i] = sps->ScalingList4x4[i];
00171 }
00172 }
00173 else
00174 {
00175 if(!sps->seq_scaling_list_present_flag[i])
00176 {
00177 if(i==6)
00178 currSlice->qmatrix[i] = quant8_intra_default;
00179 else if(i==7)
00180 currSlice->qmatrix[i] = quant8_inter_default;
00181 else
00182 currSlice->qmatrix[i] = currSlice->qmatrix[i-2];
00183 }
00184 else
00185 {
00186 if(sps->UseDefaultScalingMatrix8x8Flag[i-6])
00187 currSlice->qmatrix[i] = (i==6 || i==8 || i==10) ? quant8_intra_default:quant8_inter_default;
00188 else
00189 currSlice->qmatrix[i] = sps->ScalingList8x8[i-6];
00190 }
00191 }
00192 }
00193 }
00194
00195 if(pps->pic_scaling_matrix_present_flag)
00196 {
00197 for(i=0; i<n_ScalingList; i++)
00198 {
00199 if(i<6)
00200 {
00201 if(!pps->pic_scaling_list_present_flag[i])
00202 {
00203 if (i==0)
00204 {
00205 if(!sps->seq_scaling_matrix_present_flag)
00206 currSlice->qmatrix[i] = quant_intra_default;
00207 }
00208 else if (i==3)
00209 {
00210 if(!sps->seq_scaling_matrix_present_flag)
00211 currSlice->qmatrix[i] = quant_inter_default;
00212 }
00213 else
00214 currSlice->qmatrix[i] = currSlice->qmatrix[i-1];
00215 }
00216 else
00217 {
00218 if(pps->UseDefaultScalingMatrix4x4Flag[i])
00219 currSlice->qmatrix[i] = (i<3) ? quant_intra_default:quant_inter_default;
00220 else
00221 currSlice->qmatrix[i] = pps->ScalingList4x4[i];
00222 }
00223 }
00224 else
00225 {
00226 if(!pps->pic_scaling_list_present_flag[i])
00227 {
00228 if (i==6)
00229 {
00230 if(!sps->seq_scaling_matrix_present_flag)
00231 currSlice->qmatrix[i] = quant8_intra_default;
00232 }
00233 else if(i==7)
00234 {
00235 if(!sps->seq_scaling_matrix_present_flag)
00236 currSlice->qmatrix[i] = quant8_inter_default;
00237 }
00238 else
00239 currSlice->qmatrix[i] = currSlice->qmatrix[i-2];
00240 }
00241 else
00242 {
00243 if(pps->UseDefaultScalingMatrix8x8Flag[i-6])
00244 currSlice->qmatrix[i] = (i==6 || i==8 || i==10) ? quant8_intra_default:quant8_inter_default;
00245 else
00246 currSlice->qmatrix[i] = pps->ScalingList8x8[i-6];
00247 }
00248 }
00249 }
00250 }
00251 }
00252
00253 CalculateQuant4x4Param(currSlice);
00254 if(pps->transform_8x8_mode_flag)
00255 CalculateQuant8x8Param(currSlice);
00256 }
00257
00265 void CalculateQuant4x4Param(Slice *currSlice)
00266 {
00267 int i, j, k, temp;
00268
00269 for(k=0; k<6; k++)
00270 {
00271 for(i=0; i<4; i++)
00272 {
00273 for(j=0; j<4; j++)
00274 {
00275 temp = (i<<2)+j;
00276 currSlice->InvLevelScale4x4_Intra[0][k][i][j] = dequant_coef[k][i][j] * currSlice->qmatrix[0][temp];
00277 currSlice->InvLevelScale4x4_Intra[1][k][i][j] = dequant_coef[k][i][j] * currSlice->qmatrix[1][temp];
00278 currSlice->InvLevelScale4x4_Intra[2][k][i][j] = dequant_coef[k][i][j] * currSlice->qmatrix[2][temp];
00279
00280 currSlice->InvLevelScale4x4_Inter[0][k][i][j] = dequant_coef[k][i][j] * currSlice->qmatrix[3][temp];
00281 currSlice->InvLevelScale4x4_Inter[1][k][i][j] = dequant_coef[k][i][j] * currSlice->qmatrix[4][temp];
00282 currSlice->InvLevelScale4x4_Inter[2][k][i][j] = dequant_coef[k][i][j] * currSlice->qmatrix[5][temp];
00283 }
00284 }
00285 }
00286 }
00287
00295 void CalculateQuant8x8Param(Slice *currSlice)
00296 {
00297 ImageParameters *p_Img = currSlice->p_Img;
00298 int i, j, k, temp;
00299
00300 for(k=0; k<6; k++)
00301 {
00302 for(i=0; i<8; i++)
00303 {
00304 for(j=0; j<8; j++)
00305 {
00306 temp = (i<<3)+j;
00307 currSlice->InvLevelScale8x8_Intra[0][k][i][j] = dequant_coef8[k][i][j] * currSlice->qmatrix[6][temp];
00308 currSlice->InvLevelScale8x8_Inter[0][k][i][j] = dequant_coef8[k][i][j] * currSlice->qmatrix[7][temp];
00309 }
00310 }
00311 }
00312
00313 if( p_Img->active_sps->chroma_format_idc == 3 )
00314 {
00315 for(k=0; k<6; k++)
00316 {
00317 for(i=0; i<8; i++)
00318 {
00319 for(j=0; j<8; j++)
00320 {
00321 temp = (i<<3)+j;
00322 currSlice->InvLevelScale8x8_Intra[1][k][i][j] = dequant_coef8[k][i][j] * currSlice->qmatrix[8][temp];
00323 currSlice->InvLevelScale8x8_Inter[1][k][i][j] = dequant_coef8[k][i][j] * currSlice->qmatrix[9][temp];
00324 currSlice->InvLevelScale8x8_Intra[2][k][i][j] = dequant_coef8[k][i][j] * currSlice->qmatrix[10][temp];
00325 currSlice->InvLevelScale8x8_Inter[2][k][i][j] = dequant_coef8[k][i][j] * currSlice->qmatrix[11][temp];
00326 }
00327 }
00328 }
00329 }
00330 }