00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "global.h"
00013 #include "memalloc.h"
00014 #include "q_matrix.h"
00015
00016 extern char *GetConfigFileContent (char *Filename, int error_type);
00017
00018 #define MAX_ITEMS_TO_PARSE 1000
00019
00020 static const int quant_coef[6][4][4] = {
00021 {{13107, 8066,13107, 8066},{ 8066, 5243, 8066, 5243},{13107, 8066,13107, 8066},{ 8066, 5243, 8066, 5243}},
00022 {{11916, 7490,11916, 7490},{ 7490, 4660, 7490, 4660},{11916, 7490,11916, 7490},{ 7490, 4660, 7490, 4660}},
00023 {{10082, 6554,10082, 6554},{ 6554, 4194, 6554, 4194},{10082, 6554,10082, 6554},{ 6554, 4194, 6554, 4194}},
00024 {{ 9362, 5825, 9362, 5825},{ 5825, 3647, 5825, 3647},{ 9362, 5825, 9362, 5825},{ 5825, 3647, 5825, 3647}},
00025 {{ 8192, 5243, 8192, 5243},{ 5243, 3355, 5243, 3355},{ 8192, 5243, 8192, 5243},{ 5243, 3355, 5243, 3355}},
00026 {{ 7282, 4559, 7282, 4559},{ 4559, 2893, 4559, 2893},{ 7282, 4559, 7282, 4559},{ 4559, 2893, 4559, 2893}}
00027 };
00028
00029 const int dequant_coef[6][4][4] = {
00030 {{10, 13, 10, 13},{ 13, 16, 13, 16},{10, 13, 10, 13},{ 13, 16, 13, 16}},
00031 {{11, 14, 11, 14},{ 14, 18, 14, 18},{11, 14, 11, 14},{ 14, 18, 14, 18}},
00032 {{13, 16, 13, 16},{ 16, 20, 16, 20},{13, 16, 13, 16},{ 16, 20, 16, 20}},
00033 {{14, 18, 14, 18},{ 18, 23, 18, 23},{14, 18, 14, 18},{ 18, 23, 18, 23}},
00034 {{16, 20, 16, 20},{ 20, 25, 20, 25},{16, 20, 16, 20},{ 20, 25, 20, 25}},
00035 {{18, 23, 18, 23},{ 23, 29, 23, 29},{18, 23, 18, 23},{ 23, 29, 23, 29}}
00036 };
00037
00038 static const int quant_coef8[6][8][8] =
00039 {
00040 {
00041 {13107, 12222, 16777, 12222, 13107, 12222, 16777, 12222},
00042 {12222, 11428, 15481, 11428, 12222, 11428, 15481, 11428},
00043 {16777, 15481, 20972, 15481, 16777, 15481, 20972, 15481},
00044 {12222, 11428, 15481, 11428, 12222, 11428, 15481, 11428},
00045 {13107, 12222, 16777, 12222, 13107, 12222, 16777, 12222},
00046 {12222, 11428, 15481, 11428, 12222, 11428, 15481, 11428},
00047 {16777, 15481, 20972, 15481, 16777, 15481, 20972, 15481},
00048 {12222, 11428, 15481, 11428, 12222, 11428, 15481, 11428}
00049 },
00050 {
00051 {11916, 11058, 14980, 11058, 11916, 11058, 14980, 11058},
00052 {11058, 10826, 14290, 10826, 11058, 10826, 14290, 10826},
00053 {14980, 14290, 19174, 14290, 14980, 14290, 19174, 14290},
00054 {11058, 10826, 14290, 10826, 11058, 10826, 14290, 10826},
00055 {11916, 11058, 14980, 11058, 11916, 11058, 14980, 11058},
00056 {11058, 10826, 14290, 10826, 11058, 10826, 14290, 10826},
00057 {14980, 14290, 19174, 14290, 14980, 14290, 19174, 14290},
00058 {11058, 10826, 14290, 10826, 11058, 10826, 14290, 10826}
00059 },
00060 {
00061 {10082, 9675, 12710, 9675, 10082, 9675, 12710, 9675},
00062 {9675, 8943, 11985, 8943, 9675, 8943, 11985, 8943},
00063 {12710, 11985, 15978, 11985, 12710, 11985, 15978, 11985},
00064 {9675, 8943, 11985, 8943, 9675, 8943, 11985, 8943},
00065 {10082, 9675, 12710, 9675, 10082, 9675, 12710, 9675},
00066 {9675, 8943, 11985, 8943, 9675, 8943, 11985, 8943},
00067 {12710, 11985, 15978, 11985, 12710, 11985, 15978, 11985},
00068 {9675, 8943, 11985, 8943, 9675, 8943, 11985, 8943}
00069 },
00070 {
00071 {9362, 8931, 11984, 8931, 9362, 8931, 11984, 8931},
00072 {8931, 8228, 11259, 8228, 8931, 8228, 11259, 8228},
00073 {11984, 11259, 14913, 11259, 11984, 11259, 14913, 11259},
00074 {8931, 8228, 11259, 8228, 8931, 8228, 11259, 8228},
00075 {9362, 8931, 11984, 8931, 9362, 8931, 11984, 8931},
00076 {8931, 8228, 11259, 8228, 8931, 8228, 11259, 8228},
00077 {11984, 11259, 14913, 11259, 11984, 11259, 14913, 11259},
00078 {8931, 8228, 11259, 8228, 8931, 8228, 11259, 8228}
00079 },
00080 {
00081 {8192, 7740, 10486, 7740, 8192, 7740, 10486, 7740},
00082 {7740, 7346, 9777, 7346, 7740, 7346, 9777, 7346},
00083 {10486, 9777, 13159, 9777, 10486, 9777, 13159, 9777},
00084 {7740, 7346, 9777, 7346, 7740, 7346, 9777, 7346},
00085 {8192, 7740, 10486, 7740, 8192, 7740, 10486, 7740},
00086 {7740, 7346, 9777, 7346, 7740, 7346, 9777, 7346},
00087 {10486, 9777, 13159, 9777, 10486, 9777, 13159, 9777},
00088 {7740, 7346, 9777, 7346, 7740, 7346, 9777, 7346}
00089 },
00090 {
00091 {7282, 6830, 9118, 6830, 7282, 6830, 9118, 6830},
00092 {6830, 6428, 8640, 6428, 6830, 6428, 8640, 6428},
00093 {9118, 8640, 11570, 8640, 9118, 8640, 11570, 8640},
00094 {6830, 6428, 8640, 6428, 6830, 6428, 8640, 6428},
00095 {7282, 6830, 9118, 6830, 7282, 6830, 9118, 6830},
00096 {6830, 6428, 8640, 6428, 6830, 6428, 8640, 6428},
00097 {9118, 8640, 11570, 8640, 9118, 8640, 11570, 8640},
00098 {6830, 6428, 8640, 6428, 6830, 6428, 8640, 6428}
00099 }
00100 };
00101
00102
00103
00104 static const int dequant_coef8[6][8][8] =
00105 {
00106 {
00107 {20, 19, 25, 19, 20, 19, 25, 19},
00108 {19, 18, 24, 18, 19, 18, 24, 18},
00109 {25, 24, 32, 24, 25, 24, 32, 24},
00110 {19, 18, 24, 18, 19, 18, 24, 18},
00111 {20, 19, 25, 19, 20, 19, 25, 19},
00112 {19, 18, 24, 18, 19, 18, 24, 18},
00113 {25, 24, 32, 24, 25, 24, 32, 24},
00114 {19, 18, 24, 18, 19, 18, 24, 18}
00115 },
00116 {
00117 {22, 21, 28, 21, 22, 21, 28, 21},
00118 {21, 19, 26, 19, 21, 19, 26, 19},
00119 {28, 26, 35, 26, 28, 26, 35, 26},
00120 {21, 19, 26, 19, 21, 19, 26, 19},
00121 {22, 21, 28, 21, 22, 21, 28, 21},
00122 {21, 19, 26, 19, 21, 19, 26, 19},
00123 {28, 26, 35, 26, 28, 26, 35, 26},
00124 {21, 19, 26, 19, 21, 19, 26, 19}
00125 },
00126 {
00127 {26, 24, 33, 24, 26, 24, 33, 24},
00128 {24, 23, 31, 23, 24, 23, 31, 23},
00129 {33, 31, 42, 31, 33, 31, 42, 31},
00130 {24, 23, 31, 23, 24, 23, 31, 23},
00131 {26, 24, 33, 24, 26, 24, 33, 24},
00132 {24, 23, 31, 23, 24, 23, 31, 23},
00133 {33, 31, 42, 31, 33, 31, 42, 31},
00134 {24, 23, 31, 23, 24, 23, 31, 23}
00135 },
00136 {
00137 {28, 26, 35, 26, 28, 26, 35, 26},
00138 {26, 25, 33, 25, 26, 25, 33, 25},
00139 {35, 33, 45, 33, 35, 33, 45, 33},
00140 {26, 25, 33, 25, 26, 25, 33, 25},
00141 {28, 26, 35, 26, 28, 26, 35, 26},
00142 {26, 25, 33, 25, 26, 25, 33, 25},
00143 {35, 33, 45, 33, 35, 33, 45, 33},
00144 {26, 25, 33, 25, 26, 25, 33, 25}
00145 },
00146 {
00147 {32, 30, 40, 30, 32, 30, 40, 30},
00148 {30, 28, 38, 28, 30, 28, 38, 28},
00149 {40, 38, 51, 38, 40, 38, 51, 38},
00150 {30, 28, 38, 28, 30, 28, 38, 28},
00151 {32, 30, 40, 30, 32, 30, 40, 30},
00152 {30, 28, 38, 28, 30, 28, 38, 28},
00153 {40, 38, 51, 38, 40, 38, 51, 38},
00154 {30, 28, 38, 28, 30, 28, 38, 28}
00155 },
00156 {
00157 {36, 34, 46, 34, 36, 34, 46, 34},
00158 {34, 32, 43, 32, 34, 32, 43, 32},
00159 {46, 43, 58, 43, 46, 43, 58, 43},
00160 {34, 32, 43, 32, 34, 32, 43, 32},
00161 {36, 34, 46, 34, 36, 34, 46, 34},
00162 {34, 32, 43, 32, 34, 32, 43, 32},
00163 {46, 43, 58, 43, 46, 43, 58, 43},
00164 {34, 32, 43, 32, 34, 32, 43, 32}
00165 }
00166 };
00167
00168
00169 int matrix4x4_check[6] = {0, 0, 0, 0, 0, 0};
00170 int matrix8x8_check[6] = {0, 0, 0, 0, 0, 0};
00171
00172 static const char MatrixType4x4[6][20] =
00173 {
00174 "INTRA4X4_LUMA",
00175 "INTRA4X4_CHROMAU",
00176 "INTRA4X4_CHROMAV",
00177 "INTER4X4_LUMA",
00178 "INTER4X4_CHROMAU",
00179 "INTER4X4_CHROMAV"
00180 };
00181
00182 static const char MatrixType8x8[6][20] =
00183 {
00184 "INTRA8X8_LUMA",
00185 "INTER8X8_LUMA",
00186 "INTRA8X8_CHROMAU",
00187 "INTER8X8_CHROMAU",
00188 "INTRA8X8_CHROMAV",
00189 "INTER8X8_CHROMAV"
00190 };
00191
00192
00193 static const short Quant_intra_default[16] =
00194 {
00195 6,13,20,28,
00196 13,20,28,32,
00197 20,28,32,37,
00198 28,32,37,42
00199 };
00200
00201 static const short Quant_inter_default[16] =
00202 {
00203 10,14,20,24,
00204 14,20,24,27,
00205 20,24,27,30,
00206 24,27,30,34
00207 };
00208
00209 static const short Quant8_intra_default[64] =
00210 {
00211 6,10,13,16,18,23,25,27,
00212 10,11,16,18,23,25,27,29,
00213 13,16,18,23,25,27,29,31,
00214 16,18,23,25,27,29,31,33,
00215 18,23,25,27,29,31,33,36,
00216 23,25,27,29,31,33,36,38,
00217 25,27,29,31,33,36,38,40,
00218 27,29,31,33,36,38,40,42
00219 };
00220
00221 static const short Quant8_inter_default[64] =
00222 {
00223 9,13,15,17,19,21,22,24,
00224 13,13,17,19,21,22,24,25,
00225 15,17,19,21,22,24,25,27,
00226 17,19,21,22,24,25,27,28,
00227 19,21,22,24,25,27,28,30,
00228 21,22,24,25,27,28,30,32,
00229 22,24,25,27,28,30,32,33,
00230 24,25,27,28,30,32,33,35
00231 };
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 int CheckParameterName (char *s, int *type)
00248 {
00249 int i = 0;
00250
00251 *type = 0;
00252 while ((MatrixType4x4[i] != NULL) && (i<6))
00253 {
00254 if (0==strcmp (MatrixType4x4[i], s))
00255 return i;
00256 else
00257 i++;
00258 }
00259
00260 i = 0;
00261 *type = 1;
00262 while ((MatrixType8x8[i] != NULL) && (i<6))
00263 {
00264 if (0==strcmp (MatrixType8x8[i], s))
00265 return i;
00266 else
00267 i++;
00268 }
00269
00270 return -1;
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 void ParseMatrix (ImageParameters *p_Img, char *buf, int bufsize)
00286 {
00287 ScaleParameters *p_QScale = p_Img->p_QScale;
00288 char *items[MAX_ITEMS_TO_PARSE] = {NULL};
00289 int MapIdx;
00290 int item = 0;
00291 int InString = 0, InItem = 0;
00292 char *p = buf;
00293 char *bufend = &buf[bufsize];
00294 int IntContent;
00295 int i, j, range, type, cnt;
00296 short *ScalingList;
00297
00298 while (p < bufend)
00299 {
00300 switch (*p)
00301 {
00302 case 13:
00303 p++;
00304 break;
00305 case '#':
00306 *p = '\0';
00307 while (*p != '\n' && p < bufend)
00308 p++;
00309 InString = 0;
00310 InItem = 0;
00311 break;
00312 case '\n':
00313 InItem = 0;
00314 InString = 0;
00315 *p++='\0';
00316 break;
00317 case ' ':
00318 case '\t':
00319 if (InString)
00320 p++;
00321 else
00322 {
00323 *p++ = '\0';
00324 InItem = 0;
00325 }
00326 break;
00327
00328 case '"':
00329 *p++ = '\0';
00330 if (!InString)
00331 {
00332 items[item++] = p;
00333 InItem = ~InItem;
00334 }
00335 else
00336 InItem = 0;
00337 InString = ~InString;
00338 break;
00339
00340 case ',':
00341 p++;
00342 InItem = 0;
00343 break;
00344
00345 default:
00346 if (!InItem)
00347 {
00348 items[item++] = p;
00349 InItem = ~InItem;
00350 }
00351 p++;
00352 }
00353 }
00354
00355 item--;
00356
00357 for (i=0; i<item; i+=cnt)
00358 {
00359 cnt=0;
00360 if (0 > (MapIdx = CheckParameterName (items[i+cnt], &type)))
00361 {
00362 snprintf (errortext, ET_SIZE, " Parsing error in quantization matrix config file: Parameter Name '%s' not recognized.", items[i+cnt]);
00363 error (errortext, 300);
00364 }
00365 cnt++;
00366 if (strcmp ("=", items[i+cnt]))
00367 {
00368 snprintf (errortext, ET_SIZE, " Parsing error in quantization matrix config file: '=' expected as the second token in each item.");
00369 error (errortext, 300);
00370 }
00371 cnt++;
00372
00373 if (!type)
00374 {
00375 range = 16;
00376 ScalingList = p_QScale->ScalingList4x4input[MapIdx];
00377 matrix4x4_check[MapIdx] = 1;
00378 }
00379 else
00380 {
00381 range = 64;
00382 ScalingList = p_QScale->ScalingList8x8input[MapIdx];
00383 matrix8x8_check[MapIdx] = 1;
00384 }
00385
00386 for(j=0; j<range; j++)
00387 {
00388 if (1 != sscanf (items[i+cnt+j], "%d", &IntContent))
00389 {
00390 snprintf (errortext, ET_SIZE, " Parsing error in quantization matrix file: Expected numerical value for Parameter of %s, found '%s'.", items[i], items[i+cnt+j]);
00391 error (errortext, 300);
00392 }
00393
00394 ScalingList[j] = (short)IntContent;
00395 }
00396 cnt+=j;
00397 printf (".");
00398 }
00399 }
00400
00401
00402
00403
00404
00405
00406
00407
00408 void PatchMatrix(ImageParameters *p_Img, InputParameters *p_Inp)
00409 {
00410 ScaleParameters *p_QScale = p_Img->p_QScale;
00411 short *ScalingList;
00412 int i, cnt, fail;
00413
00414 for(i=0; i<6; i++)
00415 {
00416 if(p_Inp->ScalingListPresentFlag[i])
00417 {
00418 ScalingList = p_QScale->ScalingList4x4input[i];
00419 if(matrix4x4_check[i])
00420 {
00421 fail=0;
00422 for(cnt=0; cnt<16; cnt++)
00423 {
00424 if(ScalingList[cnt]<0 || ScalingList[cnt]>255)
00425 {
00426 fail=1;
00427 break;
00428 }
00429 }
00430
00431 if(fail)
00432 {
00433 printf("\n%s value exceed range. (Value must be 1 to 255)\n", MatrixType4x4[i]);
00434 printf("Setting default values for this matrix.");
00435 if(i>2)
00436 memcpy(ScalingList, Quant_inter_default, sizeof(short)*16);
00437 else
00438 memcpy(ScalingList, Quant_intra_default, sizeof(short)*16);
00439 }
00440 }
00441 else
00442 {
00443 printf("\n%s matrix definition not found. Setting default values.", MatrixType4x4[i]);
00444 if(i>2)
00445 memcpy(ScalingList, Quant_inter_default, sizeof(short)*16);
00446 else
00447 memcpy(ScalingList, Quant_intra_default, sizeof(short)*16);
00448 }
00449 }
00450
00451 if(p_Inp->ScalingListPresentFlag[i+6])
00452 {
00453 ScalingList = p_QScale->ScalingList8x8input[i];
00454 if(matrix8x8_check[i])
00455 {
00456 fail=0;
00457 for(cnt=0; cnt<64; cnt++)
00458 {
00459 if(ScalingList[cnt]<0 || ScalingList[cnt]>255)
00460 {
00461 fail=1;
00462 break;
00463 }
00464 }
00465
00466 if(fail)
00467 {
00468 printf("\n%s value exceed range. (Value must be 1 to 255)\n", MatrixType8x8[i]);
00469 printf("Setting default values for this matrix.");
00470 if(i==1 || i==3 || i==5)
00471 memcpy(ScalingList, Quant8_inter_default, sizeof(short)*64);
00472 else
00473 memcpy(ScalingList, Quant8_intra_default, sizeof(short)*64);
00474 }
00475 }
00476 else
00477 {
00478 printf("\n%s matrix definition not found. Setting default values.", MatrixType8x8[i]);
00479 if(i==1 || i==3 || i==5)
00480 memcpy(ScalingList, Quant8_inter_default, sizeof(short)*64);
00481 else
00482 memcpy(ScalingList, Quant8_intra_default, sizeof(short)*64);
00483 }
00484 }
00485 }
00486 }
00487
00488
00489
00490
00491
00492
00493
00494 void allocate_QMatrix (QuantParameters *p_Quant, InputParameters *p_Inp)
00495 {
00496 int max_bitdepth = imax(p_Inp->output.bit_depth[0], p_Inp->output.bit_depth[1]);
00497 int max_qp = (3 + 6*(max_bitdepth));
00498
00499 int bitdepth_qp_scale = 6*(p_Inp->output.bit_depth[0] - 8);
00500 int i;
00501
00502 get_mem5Dquant(&p_Quant->q_params_4x4, 3, 2, max_qp + 1, 4, 4);
00503 get_mem5Dquant(&p_Quant->q_params_8x8, 3, 2, max_qp + 1, 8, 8);
00504
00505 if ((p_Quant->qp_per_matrix = (int*)malloc((MAX_QP + 1 + bitdepth_qp_scale)*sizeof(int))) == NULL)
00506 no_mem_exit("allocate_QMatrix: p_Quant->qp_per_matrix");
00507 if ((p_Quant->qp_rem_matrix = (int*)malloc((MAX_QP + 1 + bitdepth_qp_scale)*sizeof(int))) == NULL)
00508 no_mem_exit("allocate_QMatrix: p_Quant->qp_per_matrix");
00509
00510 for (i = 0; i < MAX_QP + bitdepth_qp_scale + 1; i++)
00511 {
00512 p_Quant->qp_per_matrix[i] = i / 6;
00513 p_Quant->qp_rem_matrix[i] = i % 6;
00514 }
00515 }
00516
00517
00518
00519
00520
00521
00522
00523 void free_QMatrix (QuantParameters *p_Quant)
00524 {
00525 free_mem5Dquant(p_Quant->q_params_4x4);
00526 free_mem5Dquant(p_Quant->q_params_8x8);
00527
00528 free(p_Quant->qp_rem_matrix);
00529 free(p_Quant->qp_per_matrix);
00530 }
00531
00532
00533
00534
00535
00536
00537
00538
00539 void Init_QMatrix (ImageParameters *p_Img, InputParameters *p_Inp)
00540 {
00541 QuantParameters *p_Quant = p_Img->p_Quant;
00542 ScaleParameters *p_QScale = p_Img->p_QScale;
00543 char *content;
00544
00545 allocate_QMatrix (p_Quant, p_Inp);
00546
00547 if(p_Inp->ScalingMatrixPresentFlag)
00548 {
00549 printf ("Parsing QMatrix file %s ", p_Inp->QmatrixFile);
00550 content = GetConfigFileContent(p_Inp->QmatrixFile, 0);
00551 if(content!='\0')
00552 ParseMatrix(p_Img, content, strlen (content));
00553 else
00554 printf("\nError: %s\nProceeding with default values for all matrices.", errortext);
00555
00556 PatchMatrix(p_Img, p_Inp);
00557 printf("\n");
00558
00559 memset(p_QScale->UseDefaultScalingMatrix4x4Flag, 0, 6 * sizeof(short));
00560 memset(p_QScale->UseDefaultScalingMatrix8x8Flag, 0, 6 * sizeof(short));
00561
00562 free(content);
00563 }
00564 }
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578 void CalculateQuant4x4Param(ImageParameters *p_Img)
00579 {
00580 QuantParameters *p_Quant = p_Img->p_Quant;
00581 ScaleParameters *p_QScale = p_Img->p_QScale;
00582
00583 pic_parameter_set_rbsp_t *active_pps = p_Img->active_pps;
00584 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
00585
00586 int i, j, k, temp;
00587 int k_mod;
00588 int present[6];
00589 int no_q_matrix=FALSE;
00590
00591 int max_bitdepth = imax(p_Img->bitdepth_luma, p_Img->bitdepth_chroma);
00592 int max_qp = (3 + 6*(max_bitdepth));
00593
00594
00595 if(!active_sps->seq_scaling_matrix_present_flag && !active_pps->pic_scaling_matrix_present_flag)
00596 no_q_matrix=TRUE;
00597 else
00598 {
00599 memset(present, 0, 6 * sizeof(int));
00600
00601 if(active_sps->seq_scaling_matrix_present_flag)
00602 for(i=0; i<6; i++)
00603 present[i] = active_sps->seq_scaling_list_present_flag[i];
00604
00605 if(active_pps->pic_scaling_matrix_present_flag)
00606 for(i=0; i<6; i++)
00607 {
00608 if((i==0) || (i==3))
00609 present[i] |= active_pps->pic_scaling_list_present_flag[i];
00610 else
00611 present[i] = active_pps->pic_scaling_list_present_flag[i];
00612 }
00613 }
00614
00615 if(no_q_matrix==TRUE)
00616 {
00617 for(k_mod=0; k_mod<max_qp; k_mod++)
00618 {
00619 k = k_mod % 6;
00620 for(j=0; j<4; j++)
00621 {
00622 for(i=0; i<4; i++)
00623 {
00624 p_Quant->q_params_4x4[0][1][k_mod][j][i].ScaleComp = quant_coef[k][j][i];
00625 p_Quant->q_params_4x4[0][1][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]<<4;
00626
00627 p_Quant->q_params_4x4[1][1][k_mod][j][i].ScaleComp = quant_coef[k][j][i];
00628 p_Quant->q_params_4x4[1][1][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]<<4;
00629
00630 p_Quant->q_params_4x4[2][1][k_mod][j][i].ScaleComp = quant_coef[k][j][i];
00631 p_Quant->q_params_4x4[2][1][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]<<4;
00632
00633
00634 p_Quant->q_params_4x4[0][0][k_mod][j][i].ScaleComp = quant_coef[k][j][i];
00635 p_Quant->q_params_4x4[0][0][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]<<4;
00636
00637 p_Quant->q_params_4x4[1][0][k_mod][j][i].ScaleComp = quant_coef[k][j][i];
00638 p_Quant->q_params_4x4[1][0][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]<<4;
00639
00640 p_Quant->q_params_4x4[2][0][k_mod][j][i].ScaleComp = quant_coef[k][j][i];
00641 p_Quant->q_params_4x4[2][0][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]<<4;
00642 }
00643 }
00644 }
00645 }
00646 else
00647 {
00648 for(k_mod=0; k_mod<max_qp; k_mod++)
00649 {
00650 k = k_mod % 6;
00651 for(j=0; j<4; j++)
00652 {
00653 for(i=0; i<4; i++)
00654 {
00655 temp = (j<<2)+i;
00656 if((!present[0]) || p_QScale->UseDefaultScalingMatrix4x4Flag[0])
00657 {
00658 p_Quant->q_params_4x4[0][1][k_mod][j][i].ScaleComp = (quant_coef[k][j][i]<<4)/Quant_intra_default[temp];
00659 p_Quant->q_params_4x4[0][1][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]*Quant_intra_default[temp];
00660 }
00661 else
00662 {
00663 p_Quant->q_params_4x4[0][1][k_mod][j][i].ScaleComp = (quant_coef[k][j][i]<<4)/p_QScale->ScalingList4x4[0][temp];
00664 p_Quant->q_params_4x4[0][1][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]*p_QScale->ScalingList4x4[0][temp];
00665 }
00666
00667 if(!present[1])
00668 {
00669 p_Quant->q_params_4x4[1][1][k_mod][j][i].ScaleComp = p_Quant->q_params_4x4[0][1][k_mod][j][i].ScaleComp;
00670 p_Quant->q_params_4x4[1][1][k_mod][j][i].InvScaleComp = p_Quant->q_params_4x4[0][1][k_mod][j][i].InvScaleComp;
00671 }
00672 else
00673 {
00674 p_Quant->q_params_4x4[1][1][k_mod][j][i].ScaleComp = (quant_coef[k][j][i]<<4)/(p_QScale->UseDefaultScalingMatrix4x4Flag[1] ? Quant_intra_default[temp]:p_QScale->ScalingList4x4[1][temp]);
00675 p_Quant->q_params_4x4[1][1][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]*(p_QScale->UseDefaultScalingMatrix4x4Flag[1] ? Quant_intra_default[temp]:p_QScale->ScalingList4x4[1][temp]);
00676 }
00677
00678 if(!present[2])
00679 {
00680 p_Quant->q_params_4x4[2][1][k_mod][j][i].ScaleComp = p_Quant->q_params_4x4[1][1][k_mod][j][i].ScaleComp;
00681 p_Quant->q_params_4x4[2][1][k_mod][j][i].InvScaleComp = p_Quant->q_params_4x4[1][1][k_mod][j][i].InvScaleComp;
00682 }
00683 else
00684 {
00685 p_Quant->q_params_4x4[2][1][k_mod][j][i].ScaleComp = (quant_coef[k][j][i]<<4)/(p_QScale->UseDefaultScalingMatrix4x4Flag[2] ? Quant_intra_default[temp]:p_QScale->ScalingList4x4[2][temp]);
00686 p_Quant->q_params_4x4[2][1][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]*(p_QScale->UseDefaultScalingMatrix4x4Flag[2] ? Quant_intra_default[temp]:p_QScale->ScalingList4x4[2][temp]);
00687 }
00688
00689 if((!present[3]) || p_QScale->UseDefaultScalingMatrix4x4Flag[3])
00690 {
00691 p_Quant->q_params_4x4[0][0][k_mod][j][i].ScaleComp = (quant_coef[k][j][i]<<4)/Quant_inter_default[temp];
00692 p_Quant->q_params_4x4[0][0][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]*Quant_inter_default[temp];
00693 }
00694 else
00695 {
00696 p_Quant->q_params_4x4[0][0][k_mod][j][i].ScaleComp = (quant_coef[k][j][i]<<4)/p_QScale->ScalingList4x4[3][temp];
00697 p_Quant->q_params_4x4[0][0][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]*p_QScale->ScalingList4x4[3][temp];
00698 }
00699
00700 if(!present[4])
00701 {
00702 p_Quant->q_params_4x4[1][0][k_mod][j][i].ScaleComp = p_Quant->q_params_4x4[0][0][k_mod][j][i].ScaleComp;
00703 p_Quant->q_params_4x4[1][0][k_mod][j][i].InvScaleComp = p_Quant->q_params_4x4[0][0][k_mod][j][i].InvScaleComp;
00704 }
00705 else
00706 {
00707 p_Quant->q_params_4x4[1][0][k_mod][j][i].ScaleComp = (quant_coef[k][j][i]<<4)/(p_QScale->UseDefaultScalingMatrix4x4Flag[4] ? Quant_inter_default[temp]:p_QScale->ScalingList4x4[4][temp]);
00708 p_Quant->q_params_4x4[1][0][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]*(p_QScale->UseDefaultScalingMatrix4x4Flag[4] ? Quant_inter_default[temp]:p_QScale->ScalingList4x4[4][temp]);
00709 }
00710
00711 if(!present[5])
00712 {
00713 p_Quant->q_params_4x4[2][0][k_mod][j][i].ScaleComp = p_Quant->q_params_4x4[1][0][k_mod][j][i].ScaleComp;
00714 p_Quant->q_params_4x4[2][0][k_mod][j][i].InvScaleComp = p_Quant->q_params_4x4[1][0][k_mod][j][i].InvScaleComp;
00715 }
00716 else
00717 {
00718 p_Quant->q_params_4x4[2][0][k_mod][j][i].ScaleComp = (quant_coef[k][j][i]<<4)/(p_QScale->UseDefaultScalingMatrix4x4Flag[5] ? Quant_inter_default[temp]:p_QScale->ScalingList4x4[5][temp]);
00719 p_Quant->q_params_4x4[2][0][k_mod][j][i].InvScaleComp = dequant_coef[k][j][i]*(p_QScale->UseDefaultScalingMatrix4x4Flag[5] ? Quant_inter_default[temp]:p_QScale->ScalingList4x4[5][temp]);
00720 }
00721 }
00722 }
00723 }
00724 }
00725 }
00726
00727
00728
00729
00730
00731
00732
00733
00734 void CalculateQuant8x8Param(ImageParameters *p_Img)
00735 {
00736 QuantParameters *p_Quant = p_Img->p_Quant;
00737 ScaleParameters *p_QScale = p_Img->p_QScale;
00738
00739 pic_parameter_set_rbsp_t *active_pps = p_Img->active_pps;
00740 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
00741
00742 int i, j, k, temp;
00743 int k_mod;
00744 int n_ScalingList8x8;
00745 int present[6];
00746 int no_q_matrix=FALSE;
00747 int max_bitdepth = imax(p_Img->bitdepth_luma, p_Img->bitdepth_chroma);
00748 int max_qp = (3 + 6*(max_bitdepth));
00749
00750
00751
00752 n_ScalingList8x8 = ( active_sps->chroma_format_idc != 3 ) ? 2 : 6;
00753
00754 if(!active_sps->seq_scaling_matrix_present_flag && !active_pps->pic_scaling_matrix_present_flag)
00755 no_q_matrix=TRUE;
00756 else
00757 {
00758 memset(present, 0, sizeof(int)*n_ScalingList8x8);
00759
00760 if(active_sps->seq_scaling_matrix_present_flag)
00761 {
00762 for(i=0; i<n_ScalingList8x8; i++)
00763 present[i] = active_sps->seq_scaling_list_present_flag[i+6];
00764 }
00765
00766 if(active_pps->pic_scaling_matrix_present_flag)
00767 {
00768 for(i=0; i<n_ScalingList8x8; i++)
00769 {
00770 if( i==0 || i==1 )
00771 present[i] |= active_pps->pic_scaling_list_present_flag[i+6];
00772 else
00773 present[i] = active_pps->pic_scaling_list_present_flag[i+6];
00774 }
00775 }
00776 }
00777
00778 if(no_q_matrix==TRUE)
00779 {
00780 for(k = 0; k < max_qp; k++)
00781 {
00782 k_mod = k %6;
00783 for(j=0; j<8; j++)
00784 {
00785 for(i=0; i<8; i++)
00786 {
00787
00788 p_Quant->q_params_8x8[0][1][k][j][i].ScaleComp = quant_coef8[k_mod][j][i];
00789 p_Quant->q_params_8x8[0][1][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]<<4;
00790
00791
00792 p_Quant->q_params_8x8[0][0][k][j][i].ScaleComp = quant_coef8[k_mod][j][i];
00793 p_Quant->q_params_8x8[0][0][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]<<4;
00794
00795 if( n_ScalingList8x8 > 2 )
00796 {
00797
00798 p_Quant->q_params_8x8[1][1][k][j][i].ScaleComp = quant_coef8[k_mod][j][i];
00799 p_Quant->q_params_8x8[1][1][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]<<4;
00800
00801
00802 p_Quant->q_params_8x8[2][1][k][j][i].ScaleComp = quant_coef8[k_mod][j][i];
00803 p_Quant->q_params_8x8[2][1][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]<<4;
00804
00805
00806 p_Quant->q_params_8x8[1][0][k][j][i].ScaleComp = quant_coef8[k_mod][j][i];
00807 p_Quant->q_params_8x8[1][0][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]<<4;
00808
00809
00810 p_Quant->q_params_8x8[2][0][k][j][i].ScaleComp = quant_coef8[k_mod][j][i];
00811 p_Quant->q_params_8x8[2][0][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]<<4;
00812
00813 }
00814 }
00815 }
00816 }
00817 }
00818 else
00819 {
00820 for(k = 0; k < max_qp; k++)
00821 {
00822 k_mod = k %6;
00823 for(j=0; j<8; j++)
00824 {
00825 for(i=0; i<8; i++)
00826 {
00827 temp = (j<<3)+i;
00828 if((!present[0]) || p_QScale->UseDefaultScalingMatrix8x8Flag[0])
00829 {
00830 p_Quant->q_params_8x8[0][1][k][j][i].ScaleComp = (quant_coef8[k_mod][j][i]<<4)/Quant8_intra_default[temp];
00831 p_Quant->q_params_8x8[0][1][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]*Quant8_intra_default[temp];
00832 }
00833 else
00834 {
00835 p_Quant->q_params_8x8[0][1][k][j][i].ScaleComp = (quant_coef8[k_mod][j][i]<<4)/p_QScale->ScalingList8x8[0][temp];
00836 p_Quant->q_params_8x8[0][1][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]*p_QScale->ScalingList8x8[0][temp];
00837 }
00838
00839 if((!present[1]) || p_QScale->UseDefaultScalingMatrix8x8Flag[1])
00840 {
00841 p_Quant->q_params_8x8[0][0][k][j][i].ScaleComp = (quant_coef8[k_mod][j][i]<<4)/Quant8_inter_default[temp];
00842 p_Quant->q_params_8x8[0][0][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]*Quant8_inter_default[temp];
00843 }
00844 else
00845 {
00846 p_Quant->q_params_8x8[0][0][k][j][i].ScaleComp = (quant_coef8[k_mod][j][i]<<4)/p_QScale->ScalingList8x8[1][temp];
00847 p_Quant->q_params_8x8[0][0][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]*p_QScale->ScalingList8x8[1][temp];
00848 }
00849
00850 if( n_ScalingList8x8 > 2 )
00851 {
00852
00853
00854 if(!present[2])
00855 {
00856 p_Quant->q_params_8x8[1][1][k][j][i].ScaleComp = p_Quant->q_params_8x8[0][1][k][j][i].ScaleComp;
00857 p_Quant->q_params_8x8[1][1][k][j][i].InvScaleComp = p_Quant->q_params_8x8[0][1][k][j][i].InvScaleComp;
00858 }
00859 else
00860 {
00861 p_Quant->q_params_8x8[1][1][k][j][i].ScaleComp = (quant_coef8[k_mod][j][i]<<4)/(p_QScale->UseDefaultScalingMatrix8x8Flag[2] ? Quant8_intra_default[temp]:p_QScale->ScalingList8x8[2][temp]);
00862 p_Quant->q_params_8x8[1][1][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]*(p_QScale->UseDefaultScalingMatrix8x8Flag[2] ? Quant8_intra_default[temp]:p_QScale->ScalingList8x8[2][temp]);
00863 }
00864
00865
00866 if(!present[3])
00867 {
00868 p_Quant->q_params_8x8[1][0][k][j][i].ScaleComp = p_Quant->q_params_8x8[0][0][k][j][i].ScaleComp;
00869 p_Quant->q_params_8x8[1][0][k][j][i].InvScaleComp = p_Quant->q_params_8x8[0][0][k][j][i].InvScaleComp;
00870 }
00871 else
00872 {
00873 p_Quant->q_params_8x8[1][0][k][j][i].ScaleComp = (quant_coef8[k_mod][j][i]<<4)/(p_QScale->UseDefaultScalingMatrix8x8Flag[3] ? Quant8_inter_default[temp]:p_QScale->ScalingList8x8[3][temp]);
00874 p_Quant->q_params_8x8[1][0][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]*(p_QScale->UseDefaultScalingMatrix8x8Flag[3] ? Quant8_inter_default[temp]:p_QScale->ScalingList8x8[3][temp]);
00875 }
00876
00877
00878 if(!present[4])
00879 {
00880 p_Quant->q_params_8x8[2][1][k][j][i].ScaleComp = p_Quant->q_params_8x8[1][1][k][j][i].ScaleComp;
00881 p_Quant->q_params_8x8[2][1][k][j][i].InvScaleComp = p_Quant->q_params_8x8[1][1][k][j][i].InvScaleComp;
00882 }
00883 else
00884 {
00885 p_Quant->q_params_8x8[2][1][k][j][i].ScaleComp = (quant_coef8[k_mod][j][i]<<4)/(p_QScale->UseDefaultScalingMatrix8x8Flag[4] ? Quant8_intra_default[temp]:p_QScale->ScalingList8x8[4][temp]);
00886 p_Quant->q_params_8x8[2][1][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i]*(p_QScale->UseDefaultScalingMatrix8x8Flag[4] ? Quant8_intra_default[temp]:p_QScale->ScalingList8x8[4][temp]);
00887 }
00888
00889
00890 if(!present[5])
00891 {
00892 p_Quant->q_params_8x8[2][0][k][j][i].ScaleComp = p_Quant->q_params_8x8[1][0][k][j][i].ScaleComp;
00893 p_Quant->q_params_8x8[2][0][k][j][i].InvScaleComp = p_Quant->q_params_8x8[1][0][k][j][i].InvScaleComp;
00894 }
00895 else
00896 {
00897 p_Quant->q_params_8x8[2][0][k][j][i].ScaleComp = (quant_coef8[k_mod][j][i]<<4)/(p_QScale->UseDefaultScalingMatrix8x8Flag[5] ? Quant8_inter_default[temp]:p_QScale->ScalingList8x8[5][temp]);
00898 p_Quant->q_params_8x8[2][0][k][j][i].InvScaleComp = dequant_coef8[k_mod][j][i] * (p_QScale->UseDefaultScalingMatrix8x8Flag[5] ? Quant8_inter_default[temp]:p_QScale->ScalingList8x8[5][temp]);
00899 }
00900 }
00901 }
00902 }
00903 }
00904 }
00905 }
00906