00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "contributors.h"
00023
00024 #include <math.h>
00025
00026 #include "global.h"
00027 #include "enc_statistics.h"
00028 #include "memalloc.h"
00029 #include "image.h"
00030 #include "mb_access.h"
00031 #include "block.h"
00032 #include "vlc.h"
00033 #include "transform.h"
00034 #include "mc_prediction.h"
00035 #include "q_offsets.h"
00036 #include "q_matrix.h"
00037 #include "quant4x4.h"
00038 #include "quantChroma.h"
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #define P_X (PredPel[0])
00053 #define P_A (PredPel[1])
00054 #define P_B (PredPel[2])
00055 #define P_C (PredPel[3])
00056 #define P_D (PredPel[4])
00057 #define P_E (PredPel[5])
00058 #define P_F (PredPel[6])
00059 #define P_G (PredPel[7])
00060 #define P_H (PredPel[8])
00061 #define P_I (PredPel[9])
00062 #define P_J (PredPel[10])
00063 #define P_K (PredPel[11])
00064 #define P_L (PredPel[12])
00065
00066
00067 static const byte COEFF_COST4x4[2][16] =
00068 {
00069 {3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0},
00070 {9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9}
00071 };
00072
00073 static const byte SCAN_YUV420 [4][2] =
00074 {
00075 {0,0},
00076 {0,1},
00077 {0,2},
00078 {0,3}
00079 };
00080
00081
00082 static const byte SCAN_YUV422 [8][2] =
00083 {
00084 {0,0},{0,1},
00085 {1,0},{0,2},
00086 {0,3},{1,1},
00087 {1,2},{1,3}
00088 };
00089
00090
00091 static const unsigned char hor_offset[4][4][4] = {
00092 {
00093 {0, 0, 0, 0},
00094 {0, 0, 0, 0},
00095 {0, 0, 0, 0},
00096 {0, 0, 0, 0}
00097 },
00098 {
00099 {0, 4, 0, 4},
00100 {0, 0, 0, 0},
00101 {0, 0, 0, 0},
00102 {0, 0, 0, 0}
00103 },
00104 {
00105 {0, 4, 0, 4},
00106 {0, 4, 0, 4},
00107 {0, 0, 0, 0},
00108 {0, 0, 0, 0}
00109 },
00110 {
00111 {0, 4, 0, 4},
00112 {8,12, 8,12},
00113 {0, 4, 0, 4},
00114 {8,12, 8,12}
00115 }
00116 };
00117
00118 static const unsigned char ver_offset[4][4][4] = {
00119 {
00120 {0, 0, 0, 0},
00121 {0, 0, 0, 0},
00122 {0, 0, 0, 0},
00123 {0, 0, 0, 0}
00124 },
00125 {
00126 {0, 0, 4, 4},
00127 {0, 0, 0, 0},
00128 {0, 0, 0, 0},
00129 {0, 0, 0, 0}
00130 },
00131 {
00132 {0, 0, 4, 4},
00133 {8, 8,12,12},
00134 {0, 0, 0, 0},
00135 {0, 0, 0, 0}
00136 },
00137 {
00138 {0, 0, 4, 4},
00139 {0, 0, 4, 4},
00140 {8, 8,12,12},
00141 {8, 8,12,12}
00142 }
00143 };
00144
00145 static const int A[4][4] = {
00146 { 16, 20, 16, 20},
00147 { 20, 25, 20, 25},
00148 { 16, 20, 16, 20},
00149 { 20, 25, 20, 25}
00150 };
00151
00152 static unsigned char cbp_blk_chroma[8][4] = {
00153 {16, 17, 18, 19},
00154 {20, 21, 22, 23},
00155 {24, 25, 26, 27},
00156 {28, 29, 30, 31},
00157 {32, 33, 34, 35},
00158 {36, 37, 38, 39},
00159 {40, 41, 42, 43},
00160 {44, 45, 46, 47}
00161 };
00162
00163
00164 const byte SNGL_SCAN[16][2] =
00165 {
00166 {0,0},{1,0},{0,1},{0,2},
00167 {1,1},{2,0},{3,0},{2,1},
00168 {1,2},{0,3},{1,3},{2,2},
00169 {3,1},{3,2},{2,3},{3,3}
00170 };
00171
00172
00173 const byte FIELD_SCAN[16][2] =
00174 {
00175 {0,0},{0,1},{1,0},{0,2},
00176 {0,3},{1,1},{1,2},{1,3},
00177 {2,0},{2,1},{2,2},{2,3},
00178 {3,0},{3,1},{3,2},{3,3}
00179 };
00180
00181
00182 extern int ****ptLevelOffset4x4;
00183 static int **levelscale = NULL, **leveloffset = NULL;
00184 static int **invlevelscale = NULL;
00185 static int **levelscale_sp = NULL, **leveloffset_sp = NULL;
00186 static int **invlevelscale_sp = NULL;
00187 static int **fadjust4x4 = NULL;
00188 static int **fadjust2x2 = NULL;
00189 static int **fadjust4x2 = NULL;
00190 static int **levelscaleDC = NULL, **leveloffsetDC = NULL;
00191 static int **invlevelscaleDC = NULL;
00192
00193 static int **M1 = NULL;
00194 static int **M4 = NULL;
00195
00196
00197 static int Residual_DPCM_4x4_for_Intra16x16(int **mb_ores, int ipmode);
00198 static int Inv_Residual_DPCM_4x4_for_Intra16x16(int **mb_ores, int ipmode);
00199 static int Residual_DPCM_4x4(int ipmode, int **mb_ores, int **mb_rres, int block_y, int block_x);
00200 static int Inv_Residual_DPCM_4x4(int **m7, int block_y, int block_x);
00201
00202 int allocate_block_mem(void)
00203 {
00204 int alloc_size = 0;
00205 alloc_size += get_mem2Dint(&M4, BLOCK_SIZE, BLOCK_SIZE);
00206 alloc_size += get_mem2Dint(&M1, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
00207
00208 return (alloc_size);
00209 }
00210
00211
00212 void free_block_mem(void)
00213 {
00214 free_mem2Dint(M1);
00215 free_mem2Dint(M4);
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 void intrapred_4x4(Macroblock *currMB, ColorPlane pl, int img_x,int img_y, int *left_available, int *up_available, int *all_available)
00234 {
00235 int i,j;
00236 int s0;
00237 imgpel PredPel[13];
00238 imgpel **img_enc = enc_picture->p_curr_img;
00239 imgpel *img_pel;
00240 imgpel **cur_pred;
00241 imgpel ***curr_mpr_4x4 = img->mpr_4x4[pl];
00242 unsigned int dc_pred_value = img->dc_pred_value;
00243
00244 int ioff = (img_x & 15);
00245 int joff = (img_y & 15);
00246
00247 PixelPos pix_a[4];
00248 PixelPos pix_b, pix_c, pix_d;
00249
00250 int block_available_up;
00251 int block_available_left;
00252 int block_available_up_left;
00253 int block_available_up_right;
00254 int *mb_size = img->mb_size[IS_LUMA];
00255
00256 for (i=0;i<4;i++)
00257 {
00258 getNeighbour(currMB, ioff -1 , joff +i , mb_size, &pix_a[i]);
00259 }
00260
00261 getNeighbour(currMB, ioff , joff -1 , mb_size, &pix_b);
00262 getNeighbour(currMB, ioff +4 , joff -1 , mb_size, &pix_c);
00263 getNeighbour(currMB, ioff -1 , joff -1 , mb_size, &pix_d);
00264
00265 pix_c.available = pix_c.available && !((ioff==4) && ((joff==4)||(joff==12)));
00266
00267 if (params->UseConstrainedIntraPred)
00268 {
00269 for (i=0, block_available_left=1; i<4;i++)
00270 block_available_left &= pix_a[i].available ? img->intra_block[pix_a[i].mb_addr]: 0;
00271 block_available_up = pix_b.available ? img->intra_block [pix_b.mb_addr] : 0;
00272 block_available_up_right = pix_c.available ? img->intra_block [pix_c.mb_addr] : 0;
00273 block_available_up_left = pix_d.available ? img->intra_block [pix_d.mb_addr] : 0;
00274 }
00275 else
00276 {
00277 block_available_left = pix_a[0].available;
00278 block_available_up = pix_b.available;
00279 block_available_up_right = pix_c.available;
00280 block_available_up_left = pix_d.available;
00281 }
00282
00283 *left_available = block_available_left;
00284 *up_available = block_available_up;
00285 *all_available = block_available_up && block_available_left && block_available_up_left;
00286
00287 i = (img_x & 15);
00288 j = (img_y & 15);
00289
00290
00291 if (block_available_up)
00292 {
00293 img_pel = &img_enc[pix_b.pos_y][pix_b.pos_x];
00294 P_A = *(img_pel++);
00295 P_B = *(img_pel++);
00296 P_C = *(img_pel++);
00297 P_D = *(img_pel);
00298
00299 }
00300 else
00301 {
00302 P_A = P_B = P_C = P_D = dc_pred_value;
00303 }
00304
00305 if (block_available_up_right)
00306 {
00307 img_pel = &img_enc[pix_c.pos_y][pix_c.pos_x];
00308 P_E = *(img_pel++);
00309 P_F = *(img_pel++);
00310 P_G = *(img_pel++);
00311 P_H = *(img_pel);
00312 }
00313 else
00314 {
00315 P_E = P_F = P_G = P_H = P_D;
00316 }
00317
00318 if (block_available_left)
00319 {
00320 P_I = img_enc[pix_a[0].pos_y][pix_a[0].pos_x];
00321 P_J = img_enc[pix_a[1].pos_y][pix_a[1].pos_x];
00322 P_K = img_enc[pix_a[2].pos_y][pix_a[2].pos_x];
00323 P_L = img_enc[pix_a[3].pos_y][pix_a[3].pos_x];
00324 }
00325 else
00326 {
00327 P_I = P_J = P_K = P_L = dc_pred_value;
00328 }
00329
00330 if (block_available_up_left)
00331 {
00332 P_X = img_enc[pix_d.pos_y][pix_d.pos_x];
00333 }
00334 else
00335 {
00336 P_X = dc_pred_value;
00337 }
00338 for(i=0;i<9;i++)
00339 curr_mpr_4x4[i][0][0]=-1;
00340
00341
00342
00343
00344 s0 = 0;
00345 if (block_available_up && block_available_left)
00346 {
00347
00348 s0 = (P_A + P_B + P_C + P_D + P_I + P_J + P_K + P_L + 4) >> (BLOCK_SHIFT + 1);
00349 }
00350 else if (!block_available_up && block_available_left)
00351 {
00352
00353 s0 = (P_I + P_J + P_K + P_L + 2) >> BLOCK_SHIFT;;
00354 }
00355 else if (block_available_up && !block_available_left)
00356 {
00357
00358 s0 = (P_A + P_B + P_C + P_D + 2) >> BLOCK_SHIFT;
00359 }
00360 else
00361 {
00362
00363 s0 = dc_pred_value;
00364 }
00365
00366
00367 cur_pred = curr_mpr_4x4[DC_PRED];
00368 for (j=0; j < BLOCK_SIZE; j++)
00369 {
00370 for (i=0; i < BLOCK_SIZE; i++)
00371 cur_pred[j][i] = (imgpel) s0;
00372 }
00373
00374
00375
00376
00377
00378
00379 cur_pred = curr_mpr_4x4[VERT_PRED];
00380 for (i=0; i < BLOCK_SIZE; i++)
00381 {
00382 cur_pred[0][i] =
00383 cur_pred[1][i] =
00384 cur_pred[2][i] =
00385 cur_pred[3][i] = (imgpel) (&P_A)[i];
00386 }
00387 if(!block_available_up)
00388 cur_pred [0][0]=-1;
00389
00390
00391 cur_pred = curr_mpr_4x4[HOR_PRED];
00392 for (i=0; i < BLOCK_SIZE; i++)
00393 {
00394 cur_pred[i][0] =
00395 cur_pred[i][1] =
00396 cur_pred[i][2] =
00397 cur_pred[i][3] = (imgpel) (&P_I)[i];
00398 }
00399 if(!block_available_left)
00400 cur_pred[0][0]=-1;
00401
00402 if (block_available_up)
00403 {
00404
00405 cur_pred = curr_mpr_4x4[DIAG_DOWN_LEFT_PRED];
00406 cur_pred[0][0] = (imgpel) ((P_A + P_C + ((P_B)<<1) + 2) >> 2);
00407 cur_pred[0][1] =
00408 cur_pred[1][0] = (imgpel) ((P_B + P_D + ((P_C)<<1) + 2) >> 2);
00409 cur_pred[0][2] =
00410 cur_pred[1][1] =
00411 cur_pred[2][0] = (imgpel) ((P_C + P_E + ((P_D)<<1) + 2) >> 2);
00412 cur_pred[0][3] =
00413 cur_pred[1][2] =
00414 cur_pred[2][1] =
00415 cur_pred[3][0] = (imgpel) ((P_D + P_F + ((P_E)<<1) + 2) >> 2);
00416 cur_pred[1][3] =
00417 cur_pred[2][2] =
00418 cur_pred[3][1] = (imgpel) ((P_E + P_G + ((P_F)<<1) + 2) >> 2);
00419 cur_pred[2][3] =
00420 cur_pred[3][2] = (imgpel) ((P_F + P_H + ((P_G)<<1) + 2) >> 2);
00421 cur_pred[3][3] = (imgpel) ((P_G + 3*(P_H) + 2) >> 2);
00422
00423
00424 cur_pred = curr_mpr_4x4[VERT_LEFT_PRED];
00425 cur_pred[0][0] = (imgpel) ((P_A + P_B + 1) >> 1);
00426 cur_pred[0][1] =
00427 cur_pred[2][0] = (imgpel) ((P_B + P_C + 1) >> 1);
00428 cur_pred[0][2] =
00429 cur_pred[2][1] = (imgpel) ((P_C + P_D + 1) >> 1);
00430 cur_pred[0][3] =
00431 cur_pred[2][2] = (imgpel) ((P_D + P_E + 1) >> 1);
00432 cur_pred[2][3] = (imgpel) ((P_E + P_F + 1) >> 1);
00433 cur_pred[1][0] = (imgpel) ((P_A + ((P_B)<<1) + P_C + 2) >> 2);
00434 cur_pred[1][1] =
00435 cur_pred[3][0] = (imgpel) ((P_B + ((P_C)<<1) + P_D + 2) >> 2);
00436 cur_pred[1][2] =
00437 cur_pred[3][1] = (imgpel) ((P_C + ((P_D)<<1) + P_E + 2) >> 2);
00438 cur_pred[1][3] =
00439 cur_pred[3][2] = (imgpel) ((P_D + ((P_E)<<1) + P_F + 2) >> 2);
00440 cur_pred[3][3] = (imgpel) ((P_E + ((P_F)<<1) + P_G + 2) >> 2);
00441
00442 }
00443
00444
00445 if (block_available_left)
00446 {
00447
00448 cur_pred = curr_mpr_4x4[HOR_UP_PRED];
00449 cur_pred[0][0] = (imgpel) ((P_I + P_J + 1) >> 1);
00450 cur_pred[0][1] = (imgpel) ((P_I + 2*P_J + P_K + 2) >> 2);
00451 cur_pred[0][2] =
00452 cur_pred[1][0] = (imgpel) ((P_J + P_K + 1) >> 1);
00453 cur_pred[0][3] =
00454 cur_pred[1][1] = (imgpel) ((P_J + 2*P_K + P_L + 2) >> 2);
00455 cur_pred[1][2] =
00456 cur_pred[2][0] = (imgpel) ((P_K + P_L + 1) >> 1);
00457 cur_pred[1][3] =
00458 cur_pred[2][1] = (imgpel) ((P_K + 2*P_L + P_L + 2) >> 2);
00459 cur_pred[3][0] =
00460 cur_pred[2][2] =
00461 cur_pred[2][3] =
00462 cur_pred[3][1] =
00463 cur_pred[3][2] =
00464 cur_pred[3][3] = (imgpel) P_L;
00465 }
00466
00467
00468 if (block_available_up && block_available_left && block_available_up_left)
00469 {
00470
00471 cur_pred = curr_mpr_4x4[DIAG_DOWN_RIGHT_PRED];
00472 cur_pred[3][0] = (imgpel) ((P_L + 2*P_K + P_J + 2) >> 2);
00473 cur_pred[2][0] =
00474 cur_pred[3][1] = (imgpel) ((P_K + 2*P_J + P_I + 2) >> 2);
00475 cur_pred[1][0] =
00476 cur_pred[2][1] =
00477 cur_pred[3][2] = (imgpel) ((P_J + 2*P_I + P_X + 2) >> 2);
00478 cur_pred[0][0] =
00479 cur_pred[1][1] =
00480 cur_pred[2][2] =
00481 cur_pred[3][3] = (imgpel) ((P_I + 2*P_X + P_A + 2) >> 2);
00482 cur_pred[0][1] =
00483 cur_pred[1][2] =
00484 cur_pred[2][3] = (imgpel) ((P_X + 2*P_A + P_B + 2) >> 2);
00485 cur_pred[0][2] =
00486 cur_pred[1][3] = (imgpel) ((P_A + 2*P_B + P_C + 2) >> 2);
00487 cur_pred[0][3] = (imgpel) ((P_B + 2*P_C + P_D + 2) >> 2);
00488
00489
00490 cur_pred = curr_mpr_4x4[VERT_RIGHT_PRED];
00491 cur_pred[0][0] =
00492 cur_pred[2][1] = (imgpel) ((P_X + P_A + 1) >> 1);
00493 cur_pred[0][1] =
00494 cur_pred[2][2] = (imgpel) ((P_A + P_B + 1) >> 1);
00495 cur_pred[0][2] =
00496 cur_pred[2][3] = (imgpel) ((P_B + P_C + 1) >> 1);
00497 cur_pred[0][3] = (imgpel) ((P_C + P_D + 1) >> 1);
00498 cur_pred[1][0] =
00499 cur_pred[3][1] = (imgpel) ((P_I + 2*P_X + P_A + 2) >> 2);
00500 cur_pred[1][1] =
00501 cur_pred[3][2] = (imgpel) ((P_X + 2*P_A + P_B + 2) >> 2);
00502 cur_pred[1][2] =
00503 cur_pred[3][3] = (imgpel) ((P_A + 2*P_B + P_C + 2) >> 2);
00504 cur_pred[1][3] = (imgpel) ((P_B + 2*P_C + P_D + 2) >> 2);
00505 cur_pred[2][0] = (imgpel) ((P_X + 2*P_I + P_J + 2) >> 2);
00506 cur_pred[3][0] = (imgpel) ((P_I + 2*P_J + P_K + 2) >> 2);
00507
00508
00509 cur_pred = curr_mpr_4x4[HOR_DOWN_PRED];
00510 cur_pred[0][0] =
00511 cur_pred[1][2] = (imgpel) ((P_X + P_I + 1) >> 1);
00512 cur_pred[0][1] =
00513 cur_pred[1][3] = (imgpel) ((P_I + 2*P_X + P_A + 2) >> 2);
00514 cur_pred[0][2] = (imgpel) ((P_X + 2*P_A + P_B + 2) >> 2);
00515 cur_pred[0][3] = (imgpel) ((P_A + 2*P_B + P_C + 2) >> 2);
00516 cur_pred[1][0] =
00517 cur_pred[2][2] = (imgpel) ((P_I + P_J + 1) >> 1);
00518 cur_pred[1][1] =
00519 cur_pred[2][3] = (imgpel) ((P_X + 2*P_I + P_J + 2) >> 2);
00520 cur_pred[2][0] =
00521 cur_pred[3][2] = (imgpel) ((P_J + P_K + 1) >> 1);
00522 cur_pred[2][1] =
00523 cur_pred[3][3] = (imgpel) ((P_I + 2*P_J + P_K + 2) >> 2);
00524 cur_pred[3][0] = (imgpel) ((P_K + P_L + 1) >> 1);
00525 cur_pred[3][1] = (imgpel) ((P_J + 2*P_K + P_L + 2) >> 2);
00526 }
00527 }
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542 void intrapred_16x16(Macroblock *currMB, ColorPlane pl)
00543 {
00544 int s0=0,s1,s2;
00545 imgpel s[2][16];
00546 int i,j;
00547
00548 int ih,iv;
00549 int ib,ic,iaa;
00550
00551 imgpel **img_enc = enc_picture->p_curr_img;
00552 imgpel ***curr_mpr_16x16 = img->mpr_16x16[pl];
00553 unsigned int dc_pred_value = img->dc_pred_value;
00554
00555 PixelPos up;
00556 PixelPos left[17];
00557
00558 int up_avail, left_avail, left_up_avail;
00559 int *mb_size = img->mb_size[IS_LUMA];
00560
00561 for (i=0;i<17;i++)
00562 {
00563 getNeighbour(currMB, -1, i-1, mb_size, &left[i]);
00564 }
00565
00566 getNeighbour(currMB, 0, -1, mb_size, &up);
00567
00568 if (!(params->UseConstrainedIntraPred))
00569 {
00570 up_avail = up.available;
00571 left_avail = left[1].available;
00572 left_up_avail = left[0].available;
00573 }
00574 else
00575 {
00576 up_avail = up.available ? img->intra_block[up.mb_addr] : 0;
00577 for (i=1, left_avail=1; i<17;i++)
00578 left_avail &= left[i].available ? img->intra_block[left[i].mb_addr]: 0;
00579 left_up_avail = left[0].available ? img->intra_block[left[0].mb_addr]: 0;
00580 }
00581
00582 s1=s2=0;
00583
00584 if (up_avail)
00585 {
00586 for (i=up.pos_x; i < up.pos_x + MB_BLOCK_SIZE; i++)
00587 s1 += img_enc[up.pos_y][i];
00588 }
00589
00590 if (left_avail)
00591 {
00592 for (i=1; i < MB_BLOCK_SIZE + 1; i++)
00593 s2 += img_enc[left[i].pos_y][left[i].pos_x];
00594 }
00595
00596 if (up_avail)
00597 {
00598 s0= left_avail
00599 ? rshift_rnd_sf((s1+s2),(MB_BLOCK_SHIFT + 1))
00600 : rshift_rnd_sf(s1, MB_BLOCK_SHIFT);
00601 }
00602 else
00603 {
00604 s0=left_avail
00605 ? rshift_rnd_sf(s2, MB_BLOCK_SHIFT)
00606 : dc_pred_value;
00607 }
00608
00609
00610 if (up_avail)
00611 memcpy(s[0], &img_enc[up.pos_y][up.pos_x], MB_BLOCK_SIZE * sizeof(imgpel));
00612
00613
00614 if (left_avail)
00615 {
00616 for (i=1; i < MB_BLOCK_SIZE + 1; i++)
00617 s[1][i - 1]=img_enc[left[i].pos_y][left[i].pos_x];
00618 }
00619
00620 for (j=0; j < MB_BLOCK_SIZE; j++)
00621 {
00622 memcpy(curr_mpr_16x16[VERT_PRED_16][j], s[0], MB_BLOCK_SIZE * sizeof(imgpel));
00623 for (i=0; i < MB_BLOCK_SIZE; i++)
00624 {
00625 curr_mpr_16x16[HOR_PRED_16 ][j][i] = s[1][j];
00626 curr_mpr_16x16[DC_PRED_16 ][j][i] = s0;
00627 }
00628 }
00629
00630 if (!up_avail || !left_avail || !left_up_avail)
00631 return;
00632
00633
00634
00635 ih=0;
00636 iv=0;
00637 for (i=1;i<9;i++)
00638 {
00639 if (i<8)
00640 ih += i*(img_enc[up.pos_y][up.pos_x+7+i] - img_enc[up.pos_y][up.pos_x+7-i]);
00641 else
00642 ih += i*(img_enc[up.pos_y][up.pos_x+7+i] - img_enc[left[0].pos_y][left[0].pos_x]);
00643
00644 iv += i*(img_enc[left[8+i].pos_y][left[8+i].pos_x] - img_enc[left[8-i].pos_y][left[8-i].pos_x]);
00645 }
00646 ib=(5*ih+32)>>6;
00647 ic=(5*iv+32)>>6;
00648
00649 iaa=16*(img_enc[up.pos_y][up.pos_x+15]+img_enc[left[16].pos_y][left[16].pos_x]);
00650
00651 for (j=0;j< MB_BLOCK_SIZE;j++)
00652 {
00653 for (i=0;i< MB_BLOCK_SIZE;i++)
00654 {
00655 curr_mpr_16x16[PLANE_16][j][i]= iClip1( img->max_imgpel_value, rshift_rnd_sf((iaa+(i-7)*ib +(j-7)*ic), 5));
00656 }
00657 }
00658 }
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673 int dct_16x16(Macroblock *currMB, ColorPlane pl, int new_intra_mode, int is_cavlc)
00674 {
00675 int i,j;
00676 int ii,jj;
00677
00678 int ac_coef = 0;
00679 static imgpel *img_Y, *predY;
00680 int nonzero = FALSE;
00681
00682 int jpos, ipos;
00683 int b8, b4;
00684
00685
00686 int pl_off = pl<<2;
00687 int* DCLevel = img->cofDC[pl][0];
00688 int* DCRun = img->cofDC[pl][1];
00689 int ****cofAC = &img->cofAC[pl_off];
00690 int* ACLevel;
00691 int* ACRun;
00692 int coeff_cost;
00693 imgpel **img_enc = enc_picture->p_curr_img;
00694 int max_imgpel_value = img->max_imgpel_value;
00695 int qp = currMB->qp_scaled[pl];
00696 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
00697 imgpel ***curr_mpr_16x16 = img->mpr_16x16[pl];
00698
00699 int qp_per = qp_per_matrix[qp];
00700 int qp_rem = qp_rem_matrix[qp];
00701
00702
00703 levelscale = LevelScale4x4Comp[pl][1][qp_rem];
00704 invlevelscale = InvLevelScale4x4Comp[pl][1][qp_rem];
00705 leveloffset = ptLevelOffset4x4 [1][qp];
00706
00707 fadjust4x4 = img->AdaptiveRounding ? (&img->ARCofAdj4x4[pl][I16MB][0]): NULL;
00708
00709 for (j = 0; j < 16; j++)
00710 {
00711 predY = curr_mpr_16x16[new_intra_mode][j];
00712 img_Y = &pCurImg[img->opix_y + j][img->opix_x];
00713 for (i = 0; i < 16; i++)
00714 {
00715 M1[j][i] = img_Y[i] - predY[i];
00716 }
00717 }
00718
00719
00720 for (j = 0; j < 16; j+=4)
00721 {
00722 for (i = 0;i < 16; i+=4)
00723 {
00724 forward4x4(M1, M1, j, i);
00725 }
00726 }
00727
00728
00729 for (j = 0; j < 4; j++)
00730 for (i = 0; i < 4; i++)
00731 M4[j][i]= M1[j << 2][i << 2];
00732
00733
00734 hadamard4x4(M4, M4);
00735
00736 nonzero = quant_dc4x4(&M4[0], qp, DCLevel, DCRun, levelscale[0][0], invlevelscale[0][0], leveloffset[0][0], pos_scan, is_cavlc);
00737
00738
00739
00740 if (nonzero)
00741 {
00742 ihadamard4x4(M4, M4);
00743
00744
00745 for (j = 0; j < MB_BLOCK_SIZE; j += BLOCK_SIZE)
00746 {
00747 for (i = 0; i < MB_BLOCK_SIZE;i += BLOCK_SIZE)
00748 {
00749 M1[j][i] = rshift_rnd_sf(((M4[j>>2][i>>2]) * invlevelscale[0][0]) << qp_per, 6);
00750 }
00751 }
00752 }
00753 else
00754 {
00755 for (j = 0; j < MB_BLOCK_SIZE; j += BLOCK_SIZE)
00756 {
00757 for (i = 0; i < MB_BLOCK_SIZE; i += BLOCK_SIZE)
00758 {
00759 M1[j][i] = 0;
00760 }
00761 }
00762 }
00763
00764
00765 for (jj=0;jj<4;jj++)
00766 {
00767 jpos = (jj << 2);
00768 for (ii=0;ii<4;ii++)
00769 {
00770 ipos = (ii << 2);
00771
00772 b8 = 2*(jj >> 1) + (ii >> 1);
00773 b4 = 2*(jj & 0x01) + (ii & 0x01);
00774 ACLevel = cofAC[b8][b4][0];
00775 ACRun = cofAC[b8][b4][1];
00776 img->subblock_y = jj;
00777 img->subblock_x = ii;
00778
00779
00780 nonzero = quant_ac4x4(&M1[jpos], jpos, ipos, qp, ACLevel, ACRun, &fadjust4x4[jpos],
00781 levelscale, invlevelscale, leveloffset, &coeff_cost, pos_scan, COEFF_COST4x4[params->disthres], LUMA_16AC, is_cavlc);
00782
00783 if (nonzero)
00784 ac_coef = 15;
00785
00786
00787 if (M1[jpos][ipos]!= 0 || nonzero)
00788 inverse4x4(M1, M1, jpos, ipos);
00789 }
00790 }
00791
00792
00793
00794 SampleReconstruct (img_enc, curr_mpr_16x16[new_intra_mode], M1, 0, 0, img->pix_y, img->pix_x, 16, 16, max_imgpel_value, DQ_BITS);
00795
00796 if(img->type == SP_SLICE)
00797 {
00798 for (j = img->pix_y; j < img->pix_y + 16;j++)
00799 for (i = img->pix_x; i < img->pix_x + 16;i++)
00800 lrec[j][i]=-16;
00801 }
00802
00803 return ac_coef;
00804 }
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819 int dct_16x16_ls(Macroblock *currMB, ColorPlane pl, int new_intra_mode, int is_cavlc)
00820 {
00821 int i,j;
00822 int ii,jj;
00823
00824 int run,scan_pos,coeff_ctr;
00825 int ac_coef = 0;
00826 static imgpel *img_Y, *predY;
00827
00828 int b8, b4;
00829
00830
00831 int pl_off = pl<<2;
00832 int* DCLevel = img->cofDC[pl][0];
00833 int* DCRun = img->cofDC[pl][1];
00834 int* ACLevel;
00835 int* ACRun;
00836 imgpel **img_enc = enc_picture->p_curr_img;
00837 int *m7;
00838
00839 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
00840 imgpel ***curr_mpr_16x16 = img->mpr_16x16[pl];
00841
00842 for (j = 0; j < 16; j++)
00843 {
00844 predY = curr_mpr_16x16[new_intra_mode][j];
00845 img_Y = &pCurImg[img->opix_y + j][img->opix_x];
00846 for (i = 0; i < 16; i++)
00847 {
00848 M1[j][i] = img_Y[i] - predY[i];
00849 }
00850 }
00851
00852 for (j = 0; j < 4;j++)
00853 for (i = 0; i < 4;i++)
00854 M4[j][i]= M1[j << 2][i << 2];
00855
00856 run=-1;
00857 scan_pos=0;
00858
00859 for (coeff_ctr=0;coeff_ctr<16;coeff_ctr++)
00860 {
00861 i=pos_scan[coeff_ctr][0];
00862 j=pos_scan[coeff_ctr][1];
00863
00864 run++;
00865
00866 m7 = &M4[j][i];
00867
00868 if (*m7 != 0)
00869 {
00870 if (is_cavlc)
00871 *m7 = iClip3(-CAVLC_LEVEL_LIMIT, CAVLC_LEVEL_LIMIT, *m7);
00872
00873 DCLevel[scan_pos ] = *m7;
00874 DCRun [scan_pos++] = run;
00875 run=-1;
00876 }
00877 }
00878 DCLevel[scan_pos]=0;
00879
00880
00881 for (j = 0; j < 4;j++)
00882 for (i = 0; i < 4;i++)
00883 M1[j << 2][i << 2] = M4[j][i];
00884
00885
00886 for (jj=0;jj<4;jj++)
00887 {
00888 for (ii=0;ii<4;ii++)
00889 {
00890 for (j=0;j<4;j++)
00891 {
00892 memcpy(M4[j],&M1[(jj<<2)+j][(ii<<2)], BLOCK_SIZE * sizeof(int));
00893 }
00894
00895
00896 if(new_intra_mode < 2)
00897 {
00898 Residual_DPCM_4x4_for_Intra16x16(M4, new_intra_mode);
00899 }
00900
00901 run = -1;
00902 scan_pos = 0;
00903 b8 = 2*(jj >> 1) + (ii >> 1);
00904 b4 = 2*(jj & 0x01) + (ii & 0x01);
00905 ACLevel = img->cofAC [b8+pl_off][b4][0];
00906 ACRun = img->cofAC [b8+pl_off][b4][1];
00907
00908 for (coeff_ctr=1;coeff_ctr<16;coeff_ctr++)
00909 {
00910 i=pos_scan[coeff_ctr][0];
00911 j=pos_scan[coeff_ctr][1];
00912
00913 run++;
00914 m7 = &M4[j][i];
00915
00916 if (*m7 != 0)
00917 {
00918 if (is_cavlc)
00919 *m7 = iClip3(-CAVLC_LEVEL_LIMIT, CAVLC_LEVEL_LIMIT, *m7);
00920
00921 ac_coef = 15;
00922 ACLevel[scan_pos ] = *m7;
00923 ACRun [scan_pos++] = run;
00924 run=-1;
00925 }
00926
00927 }
00928 ACLevel[scan_pos] = 0;
00929
00930
00931 if(new_intra_mode<2)
00932 {
00933 Inv_Residual_DPCM_4x4_for_Intra16x16(M4, new_intra_mode);
00934 }
00935 for (j=0;j<4;j++)
00936 memcpy(&M1[(jj<<2)+j][(ii<<2)],M4[j], BLOCK_SIZE * sizeof(int));
00937 }
00938 }
00939
00940 for (j = 0; j < 16; j++)
00941 {
00942 img_Y = &img_enc[img->pix_y + j][img->pix_x];
00943 predY = curr_mpr_16x16[new_intra_mode][j];
00944 for (i = 0; i < 16; i++)
00945 img_Y[i]=(imgpel)(M1[j][i] + predY[i]);
00946 }
00947
00948 if(img->type == SP_SLICE)
00949 {
00950 for (j = img->pix_y; j < img->pix_y + 16;j++)
00951 for (i = img->pix_x; i < img->pix_x + 16;i++)
00952 lrec[j][i]=-16;
00953 }
00954
00955 return ac_coef;
00956 }
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977 int dct_4x4(Macroblock *currMB, ColorPlane pl, int block_x,int block_y, int *coeff_cost, int intra, int is_cavlc)
00978 {
00979 int j;
00980
00981 int nonzero = FALSE;
00982
00983 int pos_x = block_x >> BLOCK_SHIFT;
00984 int pos_y = block_y >> BLOCK_SHIFT;
00985 int b8 = 2*(pos_y >> 1) + (pos_x >> 1) + (pl<<2);
00986 int b4 = 2*(pos_y & 0x01) + (pos_x & 0x01);
00987 int* ACLevel = img->cofAC[b8][b4][0];
00988 int* ACRun = img->cofAC[b8][b4][1];
00989
00990 imgpel **img_enc = enc_picture->p_curr_img;
00991 imgpel **mb_pred = img->mb_pred[pl];
00992 int **mb_ores = img->mb_ores[pl];
00993 int **mb_rres = img->mb_rres[pl];
00994
00995 int max_imgpel_value = img->max_imgpel_value;
00996 int qp = currMB->qp_scaled[pl];
00997 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
00998
00999 int qp_rem = qp_rem_matrix[qp];
01000
01001
01002 levelscale = LevelScale4x4Comp[pl][intra][qp_rem];
01003 invlevelscale = InvLevelScale4x4Comp[pl][intra][qp_rem];
01004 leveloffset = ptLevelOffset4x4[intra][qp];
01005
01006 fadjust4x4 = img->AdaptiveRounding ? (&img->ARCofAdj4x4[pl][currMB->ar_mode][block_y]) : NULL;
01007 img->subblock_x = ((b8&0x1)==0) ? (((b4&0x1)==0)? 0: 1) : (((b4&0x1)==0)? 2: 3);
01008 img->subblock_y = (b8<2) ? ((b4<2) ? 0: 1) : ((b4<2) ? 2: 3);
01009
01010
01011 forward4x4(mb_ores, M1, block_y, block_x);
01012
01013
01014 nonzero = quant_4x4(&M1[block_y], block_y, block_x, qp, ACLevel, ACRun, fadjust4x4,
01015 levelscale, invlevelscale, leveloffset, coeff_cost, pos_scan, COEFF_COST4x4[params->disthres], is_cavlc);
01016
01017
01018 if (nonzero)
01019 {
01020
01021 inverse4x4(M1, mb_rres, block_y, block_x);
01022
01023
01024 SampleReconstruct (img_enc, mb_pred, mb_rres, block_y, block_x, img->pix_y, img->pix_x + block_x, BLOCK_SIZE, BLOCK_SIZE, max_imgpel_value, DQ_BITS);
01025 }
01026 else
01027 {
01028 for (j=block_y; j < block_y + BLOCK_SIZE; j++)
01029 {
01030 memcpy(&(img_enc[img->pix_y + j][img->pix_x + block_x]),&(mb_pred[j][block_x]), BLOCK_SIZE * sizeof(imgpel));
01031 }
01032 }
01033
01034 return nonzero;
01035 }
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059 int dct_4x4_ls(Macroblock *currMB, ColorPlane pl, int block_x,int block_y,int *coeff_cost, int intra, int is_cavlc)
01060 {
01061 int i,j, coeff_ctr;
01062 int run = -1;
01063 int nonzero = FALSE;
01064
01065 int pos_x = block_x >> BLOCK_SHIFT;
01066 int pos_y = block_y >> BLOCK_SHIFT;
01067 int b8 = 2*(pos_y >> 1) + (pos_x >> 1) + (pl<<2);
01068 int b4 = 2*(pos_y & 0x01) + (pos_x & 0x01);
01069
01070 int* ACL = &img->cofAC[b8][b4][0][0];
01071 int* ACR = &img->cofAC[b8][b4][1][0];
01072
01073 int pix_y, pix_x;
01074 imgpel **img_enc = enc_picture->p_curr_img;
01075 imgpel **mb_pred = img->mb_pred[pl];
01076 int **mb_ores = img->mb_ores[pl];
01077 int **mb_rres = img->mb_rres[pl];
01078 int *m7;
01079
01080 const byte *p_scan = currMB->is_field_mode ? &FIELD_SCAN[0][0] : &SNGL_SCAN[0][0];
01081
01082
01083 fadjust4x4 = img->AdaptiveRounding ? (&img->ARCofAdj4x4[pl][currMB->ar_mode][block_y]) : NULL;
01084
01085 if( (ipmode_DPCM < 2) && (intra))
01086 {
01087 Residual_DPCM_4x4(ipmode_DPCM, mb_ores, mb_rres, block_y, block_x);
01088 }
01089 else
01090 {
01091 for (j=block_y; j < block_y + BLOCK_SIZE; j++)
01092 for (i=block_x; i < block_x + BLOCK_SIZE; i++)
01093 mb_rres[j][i] = mb_ores[j][i];
01094 }
01095
01096 for (coeff_ctr=0;coeff_ctr < 16;coeff_ctr++)
01097 {
01098 i = *p_scan++;
01099 j = *p_scan++;
01100
01101 run++;
01102
01103 m7 = &mb_rres[block_y + j][block_x + i];
01104
01105 if (img->AdaptiveRounding)
01106 fadjust4x4[j][block_x+i] = 0;
01107
01108 if (*m7 != 0)
01109 {
01110 if (is_cavlc)
01111 *m7 = iClip3(-CAVLC_LEVEL_LIMIT, CAVLC_LEVEL_LIMIT, *m7);
01112
01113 nonzero=TRUE;
01114 *coeff_cost += MAX_VALUE;
01115 *ACL++ = *m7;
01116 *ACR++ = run;
01117 run=-1;
01118 }
01119 }
01120 *ACL = 0;
01121
01122 if( (ipmode_DPCM < 2) && (intra))
01123 {
01124 Inv_Residual_DPCM_4x4(mb_rres, block_y, block_x);
01125 }
01126
01127 for (j=0; j < BLOCK_SIZE; j++)
01128 {
01129 pix_y = img->pix_y + block_y + j;
01130 pix_x = img->pix_x+block_x;
01131 for (i=0; i < BLOCK_SIZE; i++)
01132 {
01133 img_enc[pix_y][pix_x+i] = mb_rres[j+block_y][i+block_x] + mb_pred[j+block_y][i+block_x];
01134 }
01135 }
01136
01137 return nonzero;
01138 }
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149 static int Residual_DPCM_4x4(int ipmode, int **mb_ores, int **mb_rres, int block_y, int block_x)
01150 {
01151 int i;
01152 int temp[4][4];
01153
01154 if(ipmode==VERT_PRED)
01155 {
01156 for (i=0; i<4; i++)
01157 {
01158 temp[0][i] = mb_ores[block_y + 0][block_x + i];
01159 temp[1][i] = mb_ores[block_y + 1][block_x + i] - mb_ores[block_y ][block_x + i];
01160 temp[2][i] = mb_ores[block_y + 2][block_x + i] - mb_ores[block_y + 1][block_x + i];
01161 temp[3][i] = mb_ores[block_y + 3][block_x + i] - mb_ores[block_y + 2][block_x + i];
01162 }
01163
01164 for (i = 0; i < 4; i++)
01165 {
01166 mb_rres[block_y + 0][block_x + i] = temp[0][i];
01167 mb_rres[block_y + 1][block_x + i] = temp[1][i];
01168 mb_rres[block_y + 2][block_x + i] = temp[2][i];
01169 mb_rres[block_y + 3][block_x + i] = temp[3][i];
01170 }
01171 }
01172 else
01173 {
01174 for (i=0; i<4; i++)
01175 {
01176 temp[i][0] = mb_ores[block_y + i][block_x];
01177 temp[i][1] = mb_ores[block_y + i][block_x + 1] - mb_ores[block_y + i][block_x ];
01178 temp[i][2] = mb_ores[block_y + i][block_x + 2] - mb_ores[block_y + i][block_x + 1];
01179 temp[i][3] = mb_ores[block_y + i][block_x + 3] - mb_ores[block_y + i][block_x + 2];
01180 }
01181
01182 for (i=0; i<4; i++)
01183 {
01184 mb_rres[block_y + i][block_x + 0] = temp[i][0];
01185 mb_rres[block_y + i][block_x + 1] = temp[i][1];
01186 mb_rres[block_y + i][block_x + 2] = temp[i][2];
01187 mb_rres[block_y + i][block_x + 3] = temp[i][3];
01188 }
01189 }
01190 return 0;
01191 }
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203 static int Inv_Residual_DPCM_4x4(int **m7, int block_y, int block_x)
01204 {
01205 int i;
01206 int temp[4][4];
01207
01208 if(ipmode_DPCM==VERT_PRED)
01209 {
01210 for(i=0; i<4; i++)
01211 {
01212 temp[0][i] = m7[block_y + 0][block_x + i];
01213 temp[1][i] = m7[block_y + 1][block_x + i] + temp[0][i];
01214 temp[2][i] = m7[block_y + 2][block_x + i] + temp[1][i];
01215 temp[3][i] = m7[block_y + 3][block_x + i] + temp[2][i];
01216 }
01217 for(i=0; i<4; i++)
01218 {
01219 m7[block_y + 0][block_x + i] = temp[0][i];
01220 m7[block_y + 1][block_x + i] = temp[1][i];
01221 m7[block_y + 2][block_x + i] = temp[2][i];
01222 m7[block_y + 3][block_x + i] = temp[3][i];
01223 }
01224 }
01225 else
01226 {
01227 for(i=0; i<4; i++)
01228 {
01229 temp[i][0] = m7[block_y + i][block_x + 0];
01230 temp[i][1] = m7[block_y + i][block_x + 1] + temp[i][0];
01231 temp[i][2] = m7[block_y + i][block_x + 2] + temp[i][1];
01232 temp[i][3] = m7[block_y + i][block_x + 3] + temp[i][2];
01233 }
01234 for(i=0; i<4; i++)
01235 {
01236 m7[block_y+i][block_x ] = temp[i][0];
01237 m7[block_y+i][block_x+1] = temp[i][1];
01238 m7[block_y+i][block_x+2] = temp[i][2];
01239 m7[block_y+i][block_x+3] = temp[i][3];
01240 }
01241 }
01242 return 0;
01243 }
01244
01245
01246
01247
01248
01249
01250
01251
01252 static int Residual_DPCM_4x4_for_Intra16x16(int **m7, int ipmode)
01253 {
01254 int i,j;
01255 int temp[4][4];
01256
01257 if(ipmode==VERT_PRED)
01258 {
01259 for (i=1; i<4; i++)
01260 for (j=0; j<4; j++)
01261 temp[i][j] = m7[i][j] - m7[i-1][j];
01262
01263 for (i=1; i<4; i++)
01264 for (j=0; j<4; j++)
01265 m7[i][j] = temp[i][j];
01266 }
01267 else
01268 {
01269 for (i=0; i<4; i++)
01270 for (j=1; j<4; j++)
01271 temp[i][j] = m7[i][j] - m7[i][j-1];
01272
01273 for (i=0; i<4; i++)
01274 for (j=1; j<4; j++)
01275 m7[i][j] = temp[i][j];
01276 }
01277 return 0;
01278 }
01279
01280
01281
01282
01283
01284
01285
01286 static int Inv_Residual_DPCM_4x4_for_Intra16x16(int **m7, int ipmode)
01287 {
01288 int i;
01289 int temp[4][4];
01290
01291 if(ipmode==VERT_PRED)
01292 {
01293 for (i=0; i<4; i++)
01294 {
01295 temp[0][i] = m7[0][i];
01296 temp[1][i] = m7[1][i] + temp[0][i];
01297 temp[2][i] = m7[2][i] + temp[1][i];
01298 temp[3][i] = m7[3][i] + temp[2][i];
01299 }
01300
01301 for (i=0; i<4; i++)
01302 {
01303 m7[1][i] = temp[1][i];
01304 m7[2][i] = temp[2][i];
01305 m7[3][i] = temp[3][i];
01306 }
01307 }
01308 else
01309 {
01310 for(i=0; i<4; i++)
01311 {
01312 temp[i][0] = m7[i][0];
01313 temp[i][1] = m7[i][1] + temp[i][0];
01314 temp[i][2] = m7[i][2] + temp[i][1];
01315 temp[i][3] = m7[i][3] + temp[i][2];
01316 }
01317 for (i=0; i<4; i++)
01318 {
01319 m7[i][1] = temp[i][1];
01320 m7[i][2] = temp[i][2];
01321 m7[i][3] = temp[i][3];
01322 }
01323 }
01324 return 0;
01325 }
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343 int dct_chroma(Macroblock *currMB, int uv, int cr_cbp, int is_cavlc)
01344 {
01345 int i, j, n2, n1, coeff_ctr;
01346 static int *m1;
01347 int coeff_cost = 0;
01348 int cr_cbp_tmp = 0;
01349 int DCzero = FALSE;
01350 int nonzero[4][4] = {{FALSE}};
01351 int nonezero = FALSE;
01352 const byte *c_cost = COEFF_COST4x4[params->disthres];
01353
01354 int b4;
01355 int* DCLevel = img->cofDC[uv+1][0];
01356 int* DCRun = img->cofDC[uv+1][1];
01357 int* ACLevel;
01358 int* ACRun;
01359 int intra = IS_INTRA (currMB);
01360 int uv_scale = uv * (img->num_blk8x8_uv >> 1);
01361
01362
01363 static const int64 cbpblk_pattern[4]={0, 0xf0000, 0xff0000, 0xffff0000};
01364 int yuv = img->yuv_format;
01365 int b8;
01366
01367 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
01368 int cur_qp = currMB->qpc[uv] + img->bitdepth_chroma_qp_scale;
01369
01370 int qp_rem = qp_rem_matrix[cur_qp];
01371
01372 int max_imgpel_value_uv = img->max_imgpel_value_comp[uv + 1];
01373
01374 int **mb_rres = img->mb_rres[uv + 1];
01375 int **mb_ores = img->mb_ores[uv + 1];
01376 imgpel **mb_pred = img->mb_pred[uv + 1];
01377
01378 levelscale = LevelScale4x4Comp [uv + 1][intra][qp_rem];
01379 leveloffset = LevelOffset4x4Comp [uv + 1][intra][cur_qp];
01380 invlevelscale = InvLevelScale4x4Comp[uv + 1][intra][qp_rem];
01381
01382 if (currMB->mb_type == P8x8 && currMB->luma_transform_size_8x8_flag)
01383 {
01384
01385 fadjust4x4 = img->AdaptiveRounding ? img->ARCofAdj4x4[uv + 1][4] : NULL;
01386 }
01387 else
01388 {
01389 fadjust4x4 = img->AdaptiveRounding ? img->ARCofAdj4x4[uv + 1][currMB->ar_mode] : NULL;
01390 }
01391
01392 img->is_v_block = uv;
01393
01394
01395 for (n2=0; n2 < img->mb_cr_size_y; n2 += BLOCK_SIZE)
01396 {
01397 for (n1=0; n1 < img->mb_cr_size_x; n1 += BLOCK_SIZE)
01398 {
01399 forward4x4(mb_ores, mb_rres, n2, n1);
01400 }
01401 }
01402
01403 if (yuv == YUV420)
01404 {
01405 m1 = M4[0];
01406
01407
01408
01409 hadamard2x2(mb_rres, m1);
01410
01411
01412 DCzero = quant_dc_cr(&m1, cur_qp, DCLevel, DCRun, fadjust2x2, levelscale[0][0], invlevelscale[0][0], leveloffset[0][0], SCAN_YUV420, is_cavlc);
01413
01414 if (DCzero)
01415 {
01416 currMB->cbp_blk |= 0xf0000 << (uv << 2) ;
01417 cr_cbp=imax(1,cr_cbp);
01418 }
01419
01420
01421 ihadamard2x2(m1, m1);
01422
01423 mb_rres[0][0] = m1[0] >> 5;
01424 mb_rres[0][4] = m1[1] >> 5;
01425 mb_rres[4][0] = m1[2] >> 5;
01426 mb_rres[4][4] = m1[3] >> 5;
01427 }
01428 else if (yuv == YUV422)
01429 {
01430
01431 int cur_qp_dc = currMB->qpc[uv] + 3 + img->bitdepth_chroma_qp_scale;
01432 int qp_rem_dc = qp_rem_matrix[cur_qp_dc];
01433
01434 invlevelscaleDC = InvLevelScale4x4Comp[uv + 1][intra][qp_rem_dc];
01435 levelscaleDC = LevelScale4x4Comp [uv + 1][intra][qp_rem_dc];
01436 leveloffsetDC = LevelOffset4x4Comp [uv + 1][intra][cur_qp_dc];
01437
01438
01439
01440 for (j=0; j < img->mb_cr_size_y; j+=BLOCK_SIZE)
01441 {
01442 for (i=0; i < img->mb_cr_size_x; i+=BLOCK_SIZE)
01443 M4[i>>2][j>>2]= mb_rres[j][i];
01444 }
01445
01446
01447 hadamard4x2(M4, M4);
01448
01449
01450 DCzero = quant_dc_cr(M4, cur_qp_dc, DCLevel, DCRun, fadjust4x2, levelscaleDC[0][0], invlevelscaleDC[0][0], leveloffsetDC[0][0], SCAN_YUV422, is_cavlc);
01451
01452 if (DCzero)
01453 {
01454 currMB->cbp_blk |= 0xff0000 << (uv << 3) ;
01455 cr_cbp=imax(1,cr_cbp);
01456 }
01457
01458
01459 ihadamard4x2(M4, M4);
01460
01461
01462 for (j = 0; j < 4; j++)
01463 {
01464 mb_rres[j << 2 ][0] = rshift_rnd_sf(M4[j][0], 6);
01465 mb_rres[j << 2 ][4] = rshift_rnd_sf(M4[j][1], 6);
01466 }
01467 }
01468
01469
01470 for (b8=0; b8 < (img->num_blk8x8_uv >> 1); b8++)
01471 {
01472 for (b4=0; b4 < 4; b4++)
01473 {
01474 int64 uv_cbpblk = ((int64)1) << cbp_blk_chroma[b8 + uv_scale][b4];
01475 n1 = hor_offset[yuv][b8][b4];
01476 n2 = ver_offset[yuv][b8][b4];
01477 ACLevel = img->cofAC[4 + b8 + uv_scale][b4][0];
01478 ACRun = img->cofAC[4 + b8 + uv_scale][b4][1];
01479 img->subblock_y = subblk_offset_y[img->yuv_format - 1][b8][b4]>>2;
01480 img->subblock_x = subblk_offset_x[img->yuv_format - 1][b8][b4]>>2;
01481
01482 nonzero[n2>>2][n1>>2] = quant_ac4x4cr(&mb_rres[n2], n2, n1, cur_qp, ACLevel, ACRun, &fadjust4x4[n2],
01483 levelscale, invlevelscale, leveloffset, &coeff_cost, pos_scan, c_cost, CHROMA_AC, is_cavlc);
01484
01485 if (nonzero[n2>>2][n1>>2])
01486 {
01487 currMB->cbp_blk |= uv_cbpblk;
01488 cr_cbp_tmp = 2;
01489 nonezero = TRUE;
01490 }
01491 }
01492 }
01493
01494
01495
01496 if(nonezero && coeff_cost < _CHROMA_COEFF_COST_)
01497 {
01498 int64 uv_cbpblk = ((int64)cbpblk_pattern[yuv] << (uv << (1+yuv)));
01499 cr_cbp_tmp = 0;
01500
01501 for (b8 = 0; b8 < (img->num_blk8x8_uv >> 1); b8++)
01502 {
01503 for (b4 = 0; b4 < 4; b4++)
01504 {
01505 n1 = hor_offset[yuv][b8][b4];
01506 n2 = ver_offset[yuv][b8][b4];
01507 if (nonzero[n2>>2][n1>>2] == TRUE)
01508 {
01509 nonzero[n2>>2][n1>>2] = FALSE;
01510 ACLevel = img->cofAC[4 + b8 + uv_scale][b4][0];
01511 ACRun = img->cofAC[4 + b8 + uv_scale][b4][1];
01512
01513 if (DCzero == 0)
01514 currMB->cbp_blk &= ~(uv_cbpblk);
01515
01516 ACLevel[0] = 0;
01517
01518 for (coeff_ctr=1; coeff_ctr < 16; coeff_ctr++)
01519 {
01520 mb_rres[n2 + pos_scan[coeff_ctr][1]][n1 + pos_scan[coeff_ctr][0]] = 0;
01521 ACLevel[coeff_ctr] = 0;
01522 }
01523 }
01524 }
01525 }
01526 }
01527
01528
01529
01530 if(cr_cbp_tmp == 2)
01531 cr_cbp = 2;
01532
01533 nonezero = FALSE;
01534 for (n2=0; n2 < img->mb_cr_size_y; n2 += BLOCK_SIZE)
01535 {
01536 for (n1=0; n1 < img->mb_cr_size_x; n1 += BLOCK_SIZE)
01537 {
01538 if (mb_rres[n2][n1] != 0 || nonzero[n2>>2][n1>>2] == TRUE)
01539 {
01540 inverse4x4(mb_rres, mb_rres, n2, n1);
01541 nonezero = TRUE;
01542 }
01543 }
01544 }
01545
01546
01547 if (nonezero == TRUE)
01548 {
01549 SampleReconstruct (enc_picture->imgUV[uv], mb_pred, mb_rres, 0, 0, img->pix_c_y, img->pix_c_x, img->mb_cr_size_x, img->mb_cr_size_y, max_imgpel_value_uv, DQ_BITS);
01550 }
01551 else
01552 {
01553 for (j=0; j < img->mb_cr_size_y; j++)
01554 {
01555 memcpy(&enc_picture->imgUV[uv][img->pix_c_y + j][img->pix_c_x], mb_pred[j], img->mb_cr_size_x * sizeof(imgpel));
01556 }
01557 }
01558
01559 return cr_cbp;
01560 }
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579 int dct_chroma_ls(Macroblock *currMB, int uv, int cr_cbp, int is_cavlc)
01580 {
01581 int i,j,n2,n1,coeff_ctr,level ,scan_pos,run;
01582 static int m1[BLOCK_SIZE];
01583 int coeff_cost;
01584 int cr_cbp_tmp;
01585 int nonzero = FALSE;
01586 static imgpel *orig_img, *pred_img;
01587
01588 int b4;
01589 int* DCLevel = img->cofDC[uv+1][0];
01590 int* DCRun = img->cofDC[uv+1][1];
01591 int* ACLevel;
01592 int* ACRun;
01593 int uv_scale = uv * (img->num_blk8x8_uv >> 1);
01594
01595
01596 int yuv = img->yuv_format;
01597 int b8;
01598 static int *m7;
01599 static int m3[4][4];
01600
01601 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
01602
01603 int **mb_rres = img->mb_rres[uv + 1];
01604 int **mb_ores = img->mb_ores[uv + 1];
01605 imgpel **mb_pred = img->mb_pred[uv + 1];
01606
01607 if (currMB->mb_type == P8x8 && currMB->luma_transform_size_8x8_flag)
01608 {
01609
01610 fadjust4x4 = img->AdaptiveRounding ? img->ARCofAdj4x4[uv + 1][4] : NULL;
01611 }
01612 else
01613 {
01614 fadjust4x4 = img->AdaptiveRounding ? img->ARCofAdj4x4[uv + 1][currMB->ar_mode] : NULL;
01615 }
01616
01617 if (yuv == YUV420)
01618 {
01619
01620
01621 run=-1;
01622 scan_pos=0;
01623 m1[0] = mb_rres[0][0] = mb_ores[0][0];
01624 m1[1] = mb_rres[0][4] = mb_ores[0][4];
01625 m1[2] = mb_rres[4][0] = mb_ores[4][0];
01626 m1[3] = mb_rres[4][4] = mb_ores[4][4];
01627
01628 for (coeff_ctr=0; coeff_ctr < 4; coeff_ctr++)
01629 {
01630 run++;
01631
01632 level =iabs(m1[coeff_ctr]);
01633
01634 if (level != 0)
01635 {
01636 if (is_cavlc)
01637 level = imin(level, CAVLC_LEVEL_LIMIT);
01638
01639 currMB->cbp_blk |= 0xf0000 << (uv << 2) ;
01640 cr_cbp=imax(1, cr_cbp);
01641 nonzero = TRUE;
01642 level = isignab(level, m1[coeff_ctr]);
01643 DCLevel[scan_pos ] = level;
01644 DCRun [scan_pos++] = run;
01645 run=-1;
01646 }
01647 }
01648 DCLevel[scan_pos] = 0;
01649 }
01650 else if(yuv == YUV422)
01651 {
01652
01653
01654
01655
01656
01657 for (j=0; j < img->mb_cr_size_y; j+=BLOCK_SIZE)
01658 {
01659 for (i=0; i < img->mb_cr_size_x; i+=BLOCK_SIZE)
01660 {
01661 m3[i>>2][j>>2] = mb_ores[j][i];
01662 mb_rres[j][i] = mb_ores[j][i];
01663 }
01664 }
01665
01666
01667 run=-1;
01668 scan_pos=0;
01669
01670
01671 for (coeff_ctr=0;coeff_ctr<8;coeff_ctr++)
01672 {
01673 i=SCAN_YUV422[coeff_ctr][0];
01674 j=SCAN_YUV422[coeff_ctr][1];
01675
01676 run++;
01677
01678 level = iabs(m3[i][j]);
01679 M4[i][j]=m3[i][j];
01680
01681 if (level != 0)
01682 {
01683
01684 currMB->cbp_blk |= 0xff0000 << (uv << 3) ;
01685 cr_cbp=imax(1,cr_cbp);
01686 nonzero = TRUE;
01687
01688 DCLevel[scan_pos ] = isignab(level,M4[i][j]);
01689 DCRun [scan_pos++] = run;
01690 run=-1;
01691 }
01692 }
01693 DCLevel[scan_pos]=0;
01694
01695
01696
01697 }
01698
01699
01700 coeff_cost=0;
01701 cr_cbp_tmp=0;
01702
01703 for (b8=0; b8 < (img->num_blk8x8_uv >> 1); b8++)
01704 {
01705 for (b4=0; b4 < 4; b4++)
01706 {
01707 int64 uv_cbpblk = ((int64)1) << cbp_blk_chroma[b8 + uv_scale][b4];
01708 n1 = hor_offset[yuv][b8][b4];
01709 n2 = ver_offset[yuv][b8][b4];
01710 ACLevel = img->cofAC[4 + b8 + uv_scale][b4][0];
01711 ACRun = img->cofAC[4 + b8 + uv_scale][b4][1];
01712 run=-1;
01713 scan_pos=0;
01714
01715 for (coeff_ctr=1; coeff_ctr < 16; coeff_ctr++)
01716 {
01717 i=pos_scan[coeff_ctr][0];
01718 j=pos_scan[coeff_ctr][1];
01719
01720 ++run;
01721
01722 level = iabs(mb_ores[n2+j][n1+i]);
01723 mb_rres[n2+j][n1+i] = mb_ores[n2+j][n1+i];
01724
01725 if (img->AdaptiveRounding)
01726 {
01727 fadjust4x4[n2+j][n1+i] = 0;
01728 }
01729
01730 if (level != 0)
01731 {
01732 currMB->cbp_blk |= uv_cbpblk;
01733 coeff_cost += MAX_VALUE;
01734
01735 cr_cbp_tmp=2;
01736 ACLevel[scan_pos ] = isignab(level, mb_ores[n2+j][n1+i]);
01737 ACRun [scan_pos++] = run;
01738 run=-1;
01739
01740 level = isignab(level, mb_ores[n2+j][n1+i]);
01741 }
01742 }
01743 ACLevel[scan_pos] = 0;
01744 }
01745 }
01746
01747 for (j=0; j < img->mb_cr_size_y; j++)
01748 {
01749 orig_img = &enc_picture->imgUV[uv][img->pix_c_y + j][img->pix_c_x];
01750 m7 = mb_rres[j];
01751 pred_img = mb_pred[j];
01752 for (i=0; i < img->mb_cr_size_x; i++)
01753 {
01754 orig_img[i] = (imgpel) m7[i] + pred_img[i];
01755 }
01756 }
01757
01758 return cr_cbp;
01759 }
01760
01761
01762
01763
01764
01765
01766
01767
01768
01769
01770
01771
01772
01773
01774
01775
01776
01777
01778 int dct_4x4_sp(Macroblock *currMB, ColorPlane pl, int block_x,int block_y,int *coeff_cost, int intra, int is_cavlc)
01779 {
01780 int i,j,coeff_ctr;
01781 int qp_const,ilev, level,scan_pos = 0,run = -1;
01782 int nonzero = FALSE;
01783
01784 imgpel **img_enc = enc_picture->p_curr_img;
01785 imgpel **mb_pred = img->mb_pred[pl];
01786 int **mb_rres = img->mb_rres[pl];
01787 int **mb_ores = img->mb_ores[pl];
01788 int c_err,qp_const2;
01789
01790 int qp = currMB->qp_scaled[pl];
01791 int qp_sp = (currMB->qpsp);
01792
01793 const byte *c_cost = COEFF_COST4x4[params->disthres];
01794 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
01795
01796 int pos_x = block_x >> BLOCK_SHIFT;
01797 int pos_y = block_y >> BLOCK_SHIFT;
01798 int b8 = 2*(pos_y >> 1) + (pos_x >> 1);
01799 int b4 = 2*(pos_y & 0x01) + (pos_x & 0x01);
01800 int* ACLevel = img->cofAC[b8][b4][0];
01801 int* ACRun = img->cofAC[b8][b4][1];
01802
01803
01804 int c_err1, c_err2, level1, level2;
01805 double D_dis1, D_dis2;
01806 int len, info;
01807 double lambda_mode = 0.85 * pow (2, (qp - SHIFT_QP)/3.0) * 4;
01808
01809 int qp_per = qp_per_matrix[qp];
01810 int qp_rem = qp_rem_matrix[qp];
01811 int q_bits = Q_BITS + qp_per;
01812 int qp_per_sp = qp_per_matrix[qp_sp];
01813 int qp_rem_sp = qp_rem_matrix[qp_sp];
01814 int q_bits_sp = Q_BITS + qp_per_sp;
01815
01816 levelscale = LevelScale4x4Comp[pl][intra][qp_rem];
01817 invlevelscale = InvLevelScale4x4Comp[pl][intra][qp_rem];
01818 leveloffset = ptLevelOffset4x4[intra][qp];
01819
01820 levelscale_sp = LevelScale4x4Comp[pl][intra][qp_rem_sp];
01821 invlevelscale_sp = InvLevelScale4x4Comp[pl][intra][qp_rem_sp];
01822 leveloffset_sp = ptLevelOffset4x4[intra][qp_sp];
01823
01824 qp_const = (1<<q_bits)/6;
01825 qp_const2 = (1<<q_bits_sp)/2;
01826
01827
01828 for (j=block_y; j< block_x + BLOCK_SIZE; j++)
01829 {
01830 for (i=block_x; i< block_x + BLOCK_SIZE; i++)
01831 {
01832 mb_rres[j][i] = mb_ores[j][i];
01833 mb_rres[j][i]+=mb_pred[j][i];
01834 M1[j][i] = mb_pred[j][i];
01835 }
01836 }
01837
01838
01839 forward4x4(mb_rres, mb_rres, block_y, block_x);
01840 forward4x4(M1, M1, block_y, block_x);
01841
01842 for (coeff_ctr = 0;coeff_ctr < 16;coeff_ctr++)
01843 {
01844 i = pos_scan[coeff_ctr][0];
01845 j = pos_scan[coeff_ctr][1];
01846
01847 run++;
01848 ilev=0;
01849
01850
01851
01852
01853 level1 = (iabs (M1[j][i]) * levelscale_sp[j][i] + qp_const2) >> q_bits_sp;
01854 level1 = (level1 << q_bits_sp) / levelscale_sp[j][i];
01855 c_err1 = mb_rres[j][i] - isignab(level1, M1[j][i]);
01856 level1 = (iabs (c_err1) * levelscale[j][i] + qp_const) >> q_bits;
01857
01858
01859 c_err2 = mb_rres[j][i] - M1[j][i];
01860 level2 = (iabs (c_err2) * levelscale[j][i] + qp_const) >> q_bits;
01861
01862
01863 if ((level1 != level2) && (level1 != 0) && (level2 != 0))
01864 {
01865 D_dis1 = mb_rres[j][i] - ((isignab(level1,c_err1) * invlevelscale[j][i] * A[j][i]<< qp_per) >>6) - M1[j][i];
01866 levrun_linfo_inter(level1, run, &len, &info);
01867 D_dis1 = D_dis1 * D_dis1 + lambda_mode * len;
01868
01869 D_dis2 = mb_rres[j][i] - ((isignab(level2,c_err2)*invlevelscale[j][i] * A[j][i]<< qp_per) >>6) - M1[j][i];
01870 levrun_linfo_inter(level2, run, &len, &info);
01871 D_dis2 = D_dis2 * D_dis2 + lambda_mode * len;
01872
01873 if (D_dis1 == D_dis2)
01874 level = (iabs(level1) < iabs(level2)) ? level1 : level2;
01875 else if (D_dis1 < D_dis2)
01876 level = level1;
01877 else
01878 level = level2;
01879
01880 c_err = (level == level1) ? c_err1 : c_err2;
01881 }
01882 else if (level1 == level2)
01883 {
01884 level = level1;
01885 c_err = c_err1;
01886 }
01887 else
01888 {
01889 level = (level1 == 0) ? level1 : level2;
01890 c_err = (level1 == 0) ? c_err1 : c_err2;
01891 }
01892
01893 if (level != 0)
01894 {
01895 nonzero = TRUE;
01896
01897 *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
01898
01899 level = isignab(level,c_err);
01900 ACLevel[scan_pos] = level;
01901 ACRun [scan_pos] = run;
01902 ++scan_pos;
01903 run=-1;
01904 ilev=((level * invlevelscale[j][i] * A[j][i] << qp_per) >>6);
01905 }
01906
01907 ilev += M1[j][i];
01908
01909 if(!si_frame_indicator && !sp2_frame_indicator)
01910 {
01911 lrec[img->pix_y+block_y+j][img->pix_x+block_x+i]=
01912 isignab((iabs(ilev) * levelscale_sp[j][i] + qp_const2) >> q_bits_sp, ilev);
01913 }
01914 mb_rres[j][i] = isignab((iabs(ilev) * levelscale_sp[j][i] + qp_const2)>> q_bits_sp, ilev) * invlevelscale_sp[j][i] << qp_per_sp;
01915 }
01916 ACLevel[scan_pos] = 0;
01917
01918
01919
01920 inverse4x4(M1, mb_rres, 0, 0);
01921
01922 for (j=0; j < BLOCK_SIZE; j++)
01923 for (i=0; i < BLOCK_SIZE; i++)
01924 {
01925
01926 mb_rres[j][i] = iClip1 (img->max_imgpel_value, rshift_rnd_sf(mb_rres[j][i], DQ_BITS));
01927
01928 }
01929
01930
01931 for (j=0; j < BLOCK_SIZE; j++)
01932 {
01933 for (i=0; i < BLOCK_SIZE; i++)
01934 {
01935 img_enc[img->pix_y+block_y+j][img->pix_x+block_x+i]= (imgpel) mb_rres[j][i];
01936
01937 }
01938 }
01939 return nonzero;
01940 }
01941
01942
01943
01944
01945
01946
01947
01948
01949
01950
01951
01952
01953
01954
01955
01956
01957
01958 int dct_chroma_sp(Macroblock *currMB, int uv,int cr_cbp, int is_cavlc)
01959 {
01960 int i, j, n2, n1, coeff_ctr;
01961 static int *m1;
01962 int coeff_cost = 0;
01963 int cr_cbp_tmp = 0;
01964 int DCzero = FALSE;
01965 int nonzero[4][4] = {{FALSE}};
01966 int nonezero = FALSE;
01967
01968 const byte *c_cost = COEFF_COST4x4[params->disthres];
01969
01970 int b4;
01971 int* DCLevel = img->cofDC[uv+1][0];
01972 int* DCRun = img->cofDC[uv+1][1];
01973 int* ACLevel;
01974 int* ACRun;
01975 int intra = IS_INTRA (currMB);
01976 int uv_scale = uv * (img->num_blk8x8_uv >> 1);
01977
01978
01979 static const int64 cbpblk_pattern[4]={0, 0xf0000, 0xff0000, 0xffff0000};
01980 int yuv = img->yuv_format;
01981 int b8;
01982
01983 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
01984 int cur_qp = currMB->qpc[uv] + img->bitdepth_chroma_qp_scale;
01985
01986 int qp_rem = qp_rem_matrix[cur_qp];
01987
01988 int max_imgpel_value_uv = img->max_imgpel_value_comp[uv + 1];
01989
01990 int **mb_rres = img->mb_rres[uv + 1];
01991 int **mb_ores = img->mb_ores[uv + 1];
01992 imgpel **mb_pred = img->mb_pred[uv + 1];
01993
01994 levelscale = LevelScale4x4Comp [uv + 1][intra][qp_rem];
01995 leveloffset = LevelOffset4x4Comp [uv + 1][intra][cur_qp];
01996 invlevelscale = InvLevelScale4x4Comp[uv + 1][intra][qp_rem];
01997 if (currMB->mb_type == P8x8 && currMB->luma_transform_size_8x8_flag)
01998 {
01999
02000 fadjust4x4 = img->AdaptiveRounding ? img->ARCofAdj4x4[uv + 1][4] : NULL;
02001 }
02002 else
02003 {
02004 fadjust4x4 = img->AdaptiveRounding ? img->ARCofAdj4x4[uv + 1][currMB->ar_mode] : NULL;
02005 }
02006
02007
02008 for (n2=0; n2 < img->mb_cr_size_y; n2 += BLOCK_SIZE)
02009 {
02010 for (n1=0; n1 < img->mb_cr_size_x; n1 += BLOCK_SIZE)
02011 {
02012 forward4x4(mb_ores, mb_rres, n2, n1);
02013 }
02014 }
02015
02016 if (yuv == YUV420)
02017 {
02018 m1 = M4[0];
02019
02020
02021
02022 hadamard2x2(mb_rres, m1);
02023
02024
02025 DCzero = quant_dc_cr(&m1, cur_qp, DCLevel, DCRun, fadjust2x2, levelscale[0][0], invlevelscale[0][0], leveloffset[0][0], pos_scan, is_cavlc);
02026
02027 if (DCzero)
02028 {
02029 currMB->cbp_blk |= 0xf0000 << (uv << 2) ;
02030 cr_cbp=imax(1,cr_cbp);
02031 }
02032
02033
02034 ihadamard2x2(m1, m1);
02035
02036 mb_rres[0][0] = m1[0];
02037 mb_rres[0][4] = m1[1];
02038 mb_rres[4][0] = m1[2];
02039 mb_rres[4][4] = m1[3];
02040 }
02041 else if (yuv == YUV422)
02042 {
02043
02044 int cur_qp_dc = currMB->qpc[uv] + 3 + img->bitdepth_chroma_qp_scale;
02045 int qp_rem_dc = qp_rem_matrix[cur_qp_dc];
02046
02047 invlevelscaleDC = InvLevelScale4x4Comp[uv + 1][intra][qp_rem_dc];
02048 levelscaleDC = LevelScale4x4Comp [uv + 1][intra][qp_rem_dc];
02049 leveloffsetDC = LevelOffset4x4Comp [uv + 1][intra][cur_qp_dc];
02050
02051
02052
02053 for (j=0; j < img->mb_cr_size_y; j+=BLOCK_SIZE)
02054 {
02055 for (i=0; i < img->mb_cr_size_x; i+=BLOCK_SIZE)
02056 M4[i>>2][j>>2]= mb_rres[j][i];
02057 }
02058
02059
02060 hadamard4x2(M4, M4);
02061
02062
02063 DCzero = quant_dc_cr(M4, cur_qp_dc, DCLevel, DCRun, fadjust4x2, levelscaleDC[0][0], invlevelscaleDC[0][0], leveloffsetDC[0][0], SCAN_YUV422, is_cavlc);
02064
02065 if (DCzero)
02066 {
02067 currMB->cbp_blk |= 0xff0000 << (uv << 3) ;
02068 cr_cbp=imax(1,cr_cbp);
02069 }
02070
02071
02072 ihadamard4x2(M4, M4);
02073
02074
02075 for (j = 0; j < 4; j++)
02076 {
02077 mb_rres[j << 2 ][0] = M4[j][0];
02078 mb_rres[j << 2 ][4] = M4[j][1];
02079 }
02080 }
02081
02082
02083 for (b8=0; b8 < (img->num_blk8x8_uv >> 1); b8++)
02084 {
02085 for (b4=0; b4 < 4; b4++)
02086 {
02087 int64 uv_cbpblk = ((int64)1) << cbp_blk_chroma[b8 + uv_scale][b4];
02088 n1 = hor_offset[yuv][b8][b4];
02089 n2 = ver_offset[yuv][b8][b4];
02090 ACLevel = img->cofAC[4 + b8 + uv_scale][b4][0];
02091 ACRun = img->cofAC[4 + b8 + uv_scale][b4][1];
02092
02093
02094 nonzero[n2>>2][n1>>2] = quant_ac4x4cr(&mb_rres[n2], n2, n1, cur_qp, ACLevel, ACRun, &fadjust4x4[n2],
02095 levelscale, invlevelscale, leveloffset, &coeff_cost, pos_scan, c_cost, CHROMA_AC, is_cavlc);
02096
02097 if (nonzero[n2>>2][n1>>2])
02098 {
02099 currMB->cbp_blk |= uv_cbpblk;
02100 cr_cbp_tmp = 2;
02101 nonezero = TRUE;
02102 }
02103 }
02104 }
02105
02106
02107
02108 if(nonezero && coeff_cost < _CHROMA_COEFF_COST_)
02109 {
02110 int64 uv_cbpblk = ((int64)cbpblk_pattern[yuv] << (uv << (1+yuv)));
02111 cr_cbp_tmp = 0;
02112
02113 for (b8 = 0; b8 < (img->num_blk8x8_uv >> 1); b8++)
02114 {
02115 for (b4 = 0; b4 < 4; b4++)
02116 {
02117 n1 = hor_offset[yuv][b8][b4];
02118 n2 = ver_offset[yuv][b8][b4];
02119 if (nonzero[n2>>2][n1>>2] == TRUE)
02120 {
02121 nonzero[n2>>2][n1>>2] = FALSE;
02122 ACLevel = img->cofAC[4 + b8 + uv_scale][b4][0];
02123 ACRun = img->cofAC[4 + b8 + uv_scale][b4][1];
02124
02125 if (DCzero == 0)
02126 currMB->cbp_blk &= ~(uv_cbpblk);
02127
02128 ACLevel[0] = 0;
02129
02130 for (coeff_ctr=1; coeff_ctr < 16; coeff_ctr++)
02131 {
02132 mb_rres[n2 + pos_scan[coeff_ctr][1]][n1 + pos_scan[coeff_ctr][0]] = 0;
02133 ACLevel[coeff_ctr] = 0;
02134 }
02135 }
02136 }
02137 }
02138 }
02139
02140
02141
02142 if(cr_cbp_tmp == 2)
02143 cr_cbp = 2;
02144
02145 nonezero = FALSE;
02146 for (n2=0; n2 < img->mb_cr_size_y; n2 += BLOCK_SIZE)
02147 {
02148 for (n1=0; n1 < img->mb_cr_size_x; n1 += BLOCK_SIZE)
02149 {
02150 if (mb_rres[n2][n1] != 0 || nonzero[n2>>2][n1>>2] == TRUE)
02151 {
02152 inverse4x4(mb_rres, mb_rres, n2, n1);
02153 nonezero = TRUE;
02154 }
02155 }
02156 }
02157
02158
02159 if (nonezero == TRUE)
02160 {
02161 SampleReconstruct (enc_picture->imgUV[uv], mb_pred, mb_rres, 0, 0, img->pix_c_y, img->pix_c_x, img->mb_cr_size_x, img->mb_cr_size_y, max_imgpel_value_uv, DQ_BITS);
02162 }
02163 else
02164 {
02165 for (j=0; j < img->mb_cr_size_y; j++)
02166 {
02167 memcpy(&enc_picture->imgUV[uv][img->pix_c_y + j][img->pix_c_x], mb_pred[j], img->mb_cr_size_x * sizeof(imgpel));
02168 }
02169 }
02170
02171 return cr_cbp;
02172 }
02173
02174 int dct_chroma_sp_old(Macroblock *currMB, int uv,int cr_cbp, int is_cavlc)
02175 {
02176 int i,j,ilev,n2,n1,coeff_ctr,c_err,level ,scan_pos,run;
02177 int m1[BLOCK_SIZE];
02178 int coeff_cost;
02179 int cr_cbp_tmp;
02180 int mp1[BLOCK_SIZE];
02181 const byte *c_cost = COEFF_COST4x4[params->disthres];
02182 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
02183
02184 int intra = IS_INTRA (currMB);
02185
02186 int b4;
02187 int* DCLevel = img->cofDC[uv+1][0];
02188 int* DCRun = img->cofDC[uv+1][1];
02189 int* ACLevel;
02190 int* ACRun;
02191
02192 int c_err1, c_err2, level1, level2;
02193 int len, info;
02194 double D_dis1, D_dis2;
02195 double lambda_mode = 0.85 * pow (2, (currMB->qp -SHIFT_QP)/3.0) * 4;
02196 int max_imgpel_value_uv = img->max_imgpel_value_comp[1];
02197
02198 int qpChroma = currMB->qpc[uv] + img->bitdepth_chroma_qp_scale;
02199 int qpChromaSP=iClip3(-img->bitdepth_chroma_qp_scale, 51, currMB->qpsp + active_pps->chroma_qp_index_offset);
02200 int **mb_rres = img->mb_rres[uv + 1];
02201 int **mb_ores = img->mb_ores[uv + 1];
02202 imgpel **mb_pred = img->mb_pred[uv + 1];
02203
02204 int qp_per = qp_per_matrix[qpChroma];
02205 int qp_rem = qp_rem_matrix[qpChroma];
02206 int q_bits = Q_BITS + qp_per;
02207 int qp_const = (1<<q_bits)/6;
02208 int qp_per_sp = qp_per_matrix[qpChromaSP];
02209 int qp_rem_sp = qp_rem_matrix[qpChromaSP];
02210 int q_bits_sp = Q_BITS + qp_per_sp;
02211 int qp_const2 = (1<<q_bits_sp)/2;
02212
02213 levelscale = LevelScale4x4Comp [uv + 1][intra][qp_rem];
02214 invlevelscale = InvLevelScale4x4Comp[uv + 1][intra][qp_rem];
02215 leveloffset = LevelOffset4x4Comp [uv + 1][intra][qpChroma];
02216
02217 levelscale_sp = LevelScale4x4Comp [uv + 1][intra][qp_rem_sp];
02218 invlevelscale_sp = InvLevelScale4x4Comp[uv + 1][intra][qp_rem_sp];
02219 leveloffset_sp = LevelOffset4x4Comp [uv + 1][intra][qpChromaSP];
02220
02221 for (j=0; j < img->mb_cr_size_y; j++)
02222 {
02223 for (i=0; i < img->mb_cr_size_x; i++)
02224 {
02225 mb_rres[j][i] = mb_ores[j][i];
02226 mb_rres[j][i] += mb_pred[j][i];
02227 M1[j][i] = mb_pred[j][i];
02228 }
02229 }
02230
02231 for (n2=0; n2 < img->mb_cr_size_y; n2 += BLOCK_SIZE)
02232 {
02233 for (n1=0; n1 < img->mb_cr_size_x; n1 += BLOCK_SIZE)
02234 {
02235 forward4x4(mb_rres, mb_rres, n2, n1);
02236 forward4x4(M1, M1, n2, n1);
02237 }
02238 }
02239
02240
02241 hadamard2x2(mb_rres, m1);
02242 hadamard2x2(M1, mp1);
02243
02244 run=-1;
02245 scan_pos=0;
02246
02247 for (coeff_ctr = 0; coeff_ctr < 4; coeff_ctr++)
02248 {
02249 run++;
02250 ilev=0;
02251
02252
02253 c_err1 = (iabs (mp1[coeff_ctr]) * levelscale_sp[0][0] + 2 * qp_const2) >> (q_bits_sp + 1);
02254 c_err1 = (c_err1 << (q_bits_sp + 1)) / levelscale_sp[0][0];
02255 c_err1 = m1[coeff_ctr] - isignab(c_err1, mp1[coeff_ctr]);
02256 level1 = (iabs(c_err1) * levelscale[0][0] + 2 * qp_const) >> (q_bits+1);
02257
02258
02259 c_err2 = m1[coeff_ctr] - mp1[coeff_ctr];
02260 level2 = (iabs(c_err2) * levelscale[0][0] + 2 * qp_const) >> (q_bits+1);
02261
02262 if (level1 != level2 && level1 != 0 && level2 != 0)
02263 {
02264 D_dis1 = m1[coeff_ctr] - ((isignab(level1,c_err1)*invlevelscale[0][0] * A[0][0]<< qp_per) >>5)- mp1[coeff_ctr];
02265 levrun_linfo_c2x2(level1, run, &len, &info);
02266 D_dis1 = D_dis1 * D_dis1 + lambda_mode * len;
02267
02268 D_dis2 = m1[coeff_ctr] - ((isignab(level2,c_err2)*invlevelscale[0][0] * A[0][0]<< qp_per) >>5)- mp1[coeff_ctr];
02269 levrun_linfo_c2x2(level2, run, &len, &info);
02270 D_dis2 = D_dis2 * D_dis2 + lambda_mode * len;
02271
02272 if (D_dis1 == D_dis2)
02273 level = (iabs(level1) < iabs(level2)) ? level1 : level2;
02274 else if (D_dis1 < D_dis2)
02275 level = level1;
02276 else
02277 level = level2;
02278
02279 c_err = (level == level1) ? c_err1 : c_err2;
02280 }
02281 else if (level1 == level2)
02282 {
02283 level = level1;
02284 c_err = c_err1;
02285 }
02286 else
02287 {
02288 level = (level1 == 0) ? level1 : level2;
02289 c_err = (level1 == 0) ? c_err1 : c_err2;
02290 }
02291
02292 if (level != 0)
02293 {
02294 if (is_cavlc)
02295 level = imin(level, CAVLC_LEVEL_LIMIT);
02296
02297 currMB->cbp_blk |= 0xf0000 << (uv << 2) ;
02298 cr_cbp = imax(1, cr_cbp);
02299 DCLevel[scan_pos ] = isignab(level ,c_err);
02300 DCRun [scan_pos++] = run;
02301 run=-1;
02302 ilev=((isignab(level,c_err)*invlevelscale[0][0]*A[0][0]<< qp_per) >>5);
02303 }
02304
02305 ilev+= mp1[coeff_ctr];
02306 m1[coeff_ctr]=isignab((iabs(ilev) * levelscale_sp[0][0] + 2 * qp_const2) >> (q_bits_sp+1), ilev) * invlevelscale_sp[0][0] << qp_per_sp;
02307 if(!si_frame_indicator && !sp2_frame_indicator)
02308 lrec_uv[uv][img->pix_c_y+4*(coeff_ctr%2)][img->pix_c_x+4*(coeff_ctr/2)]=isignab((iabs(ilev) * levelscale_sp[0][0] + 2 * qp_const2) >> (q_bits_sp+1), ilev);
02309 }
02310 DCLevel[scan_pos] = 0;
02311
02312
02313 ihadamard2x2(m1, m1);
02314
02315 mb_rres[0][0]=(m1[0])>>1;
02316 mb_rres[0][4]=(m1[1])>>1;
02317 mb_rres[4][0]=(m1[2])>>1;
02318 mb_rres[4][4]=(m1[3])>>1;
02319
02320
02321 coeff_cost=0;
02322 cr_cbp_tmp=0;
02323
02324 for (n2=0; n2 <= BLOCK_SIZE; n2 += BLOCK_SIZE)
02325 {
02326 for (n1=0; n1 <= BLOCK_SIZE; n1 += BLOCK_SIZE)
02327 {
02328 b4 = 2*(n2 >> 2) + (n1 >> 2);
02329 ACLevel = img->cofAC[uv+4][b4][0];
02330 ACRun = img->cofAC[uv+4][b4][1];
02331
02332 run = -1;
02333 scan_pos = 0;
02334
02335 for (coeff_ctr=1; coeff_ctr < 16; coeff_ctr++)
02336 {
02337 i=pos_scan[coeff_ctr][0];
02338 j=pos_scan[coeff_ctr][1];
02339
02340 ++run;
02341 ilev=0;
02342
02343
02344 c_err1 = (iabs(M1[n2+j][n1+i]) * levelscale_sp[j][i] + qp_const2) >> q_bits_sp;
02345 c_err1 = (c_err1 << q_bits_sp) / levelscale_sp[j][i];
02346 c_err1 = mb_rres[n2+j][n1+i] - isignab(c_err1, M1[n2+j][n1+i]);
02347 level1 = (iabs(c_err1) * levelscale[j][i] + qp_const) >> q_bits;
02348
02349
02350 c_err2 = mb_rres[n2+j][n1+i] - M1[n2+j][n1+i];
02351 level2 = (iabs(c_err2) * levelscale[j][i] + qp_const) >> q_bits;
02352
02353 if (level1 != level2 && level1 != 0 && level2 != 0)
02354 {
02355 D_dis1 = mb_rres[n2+j][n1+i] - ((isignab(level1,c_err1)*invlevelscale[j][i]*A[j][i]<< qp_per) >>6) - M1[n2+j][n1+i];
02356
02357 levrun_linfo_inter(level1, run, &len, &info);
02358 D_dis1 = D_dis1 * D_dis1 + lambda_mode * len;
02359
02360 D_dis2 = mb_rres[n2+j][n1+i] - ((isignab(level2,c_err2)*invlevelscale[j][i]*A[j][i]<< qp_per) >>6) - M1[n2+j][n1+i];
02361 levrun_linfo_inter(level2, run, &len, &info);
02362 D_dis2 = D_dis2 * D_dis2 + lambda_mode * len;
02363
02364 if (D_dis1 == D_dis2)
02365 level = (iabs(level1) < iabs(level2)) ? level1 : level2;
02366 else
02367 {
02368 if (D_dis1 < D_dis2)
02369 level = level1;
02370 else
02371 level = level2;
02372 }
02373 c_err = (level == level1) ? c_err1 : c_err2;
02374 }
02375 else if (level1 == level2)
02376 {
02377 level = level1;
02378 c_err = c_err1;
02379 }
02380 else
02381 {
02382 level = (level1 == 0) ? level1 : level2;
02383 c_err = (level1 == 0) ? c_err1 : c_err2;
02384 }
02385
02386 if (level != 0)
02387 {
02388 currMB->cbp_blk |= (int64)1 << (16 + (uv << 2) + ((n2 >> 1) + (n1 >> 2))) ;
02389
02390 coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
02391
02392 cr_cbp_tmp=2;
02393 level = isignab(level,c_err);
02394 ACLevel[scan_pos] = level;
02395 ACRun [scan_pos] = run;
02396 ++scan_pos;
02397 run=-1;
02398 ilev=((level * invlevelscale[j][i]*A[j][i]<< qp_per) >>6);
02399 }
02400 ilev+=M1[n2+j][n1+i];
02401 if(!si_frame_indicator && !sp2_frame_indicator)
02402 if(!( (n2+j) % 4==0 && (n1+i)%4 ==0 ))
02403 lrec_uv[uv][img->pix_c_y+n1+j][img->pix_c_x+n2+i]=isignab((iabs(ilev) * levelscale_sp[j][i] + qp_const2) >> q_bits_sp,ilev);
02404 mb_rres[n2+j][n1+i] = isignab((iabs(ilev) * levelscale_sp[j][i] + qp_const2) >> q_bits_sp,ilev) * invlevelscale_sp[j][i] << qp_per_sp;
02405 }
02406 ACLevel[scan_pos] = 0;
02407 }
02408 }
02409
02410
02411
02412 if(cr_cbp_tmp==2)
02413 cr_cbp=2;
02414
02415
02416
02417 for (n2=0; n2 <= BLOCK_SIZE; n2 += BLOCK_SIZE)
02418 {
02419 for (n1=0; n1 <= BLOCK_SIZE; n1 += BLOCK_SIZE)
02420 {
02421 inverse4x4(mb_rres, mb_rres, n2, n1);
02422
02423 for (j=0; j < BLOCK_SIZE; j++)
02424 for (i=0; i < BLOCK_SIZE; i++)
02425 {
02426 mb_rres[n2+j][n1+i] = iClip1 (max_imgpel_value_uv,rshift_rnd_sf(mb_rres[n2+j][n1+i], DQ_BITS));
02427 }
02428 }
02429 }
02430
02431
02432 for (j=0; j < BLOCK_SIZE*2; j++)
02433 for (i=0; i < BLOCK_SIZE*2; i++)
02434 {
02435 enc_picture->imgUV[uv][img->pix_c_y+j][img->pix_c_x+i]= (imgpel) mb_rres[j][i];
02436 }
02437
02438 return cr_cbp;
02439 }
02440
02441
02442
02443
02444
02445
02446
02447
02448
02449
02450
02451
02452
02453
02454
02455
02456 void copyblock_sp(Macroblock *currMB, ColorPlane pl, int block_x,int block_y)
02457 {
02458 int i, j;
02459
02460 int cur_qp = currMB->qpsp + img->bitdepth_luma_qp_scale;
02461 int qp_per = qp_per_matrix[cur_qp];
02462 int qp_rem = qp_rem_matrix[cur_qp];
02463 int q_bits = Q_BITS + qp_per;
02464 int qp_const2=(1<<q_bits)/2;
02465 imgpel **img_enc = enc_picture->p_curr_img;
02466 imgpel **mb_pred = img->mb_pred[pl];
02467 int **mb_ores = img->mb_ores[pl];
02468 int **mb_rres = img->mb_rres[pl];
02469
02470 levelscale = LevelScale4x4Comp[pl][0][qp_rem];
02471 invlevelscale = InvLevelScale4x4Comp[pl][0][qp_rem];
02472 leveloffset = ptLevelOffset4x4[0][cur_qp];
02473
02474
02475 for (j=0; j< BLOCK_SIZE; j++)
02476 {
02477 for (i=0; i< BLOCK_SIZE; i++)
02478 {
02479 mb_rres[j+block_y][i+block_x] = mb_ores[j+block_y][i+block_x];
02480 M1[i][j]=mb_pred[j+block_y][i+block_x];
02481 }
02482 }
02483
02484 forward4x4(M1, M1, 0, 0);
02485
02486
02487 for (j=0;j < BLOCK_SIZE; j++)
02488 {
02489 for (i=0; i < BLOCK_SIZE; i++)
02490 {
02491 mb_rres[j][i]=isignab((iabs(M1[i][j])* levelscale[i][j]+qp_const2)>> q_bits,M1[i][j])*levelscale[i][j]<<qp_per;
02492 if(!si_frame_indicator && !sp2_frame_indicator)
02493 {
02494 lrec[img->pix_y+block_y+j][img->pix_x+block_x+i] =
02495 isignab((iabs(M1[i][j]) * levelscale[i][j] + qp_const2) >> q_bits, M1[i][j]);
02496 }
02497 }
02498 }
02499
02500
02501
02502 inverse4x4(mb_rres, mb_rres, 0, 0);
02503
02504
02505 for (j=0; j < BLOCK_SIZE; j++)
02506 {
02507 for (i=0; i < BLOCK_SIZE; i++)
02508 {
02509 mb_rres[j][i] = iClip1 (img->max_imgpel_value, mb_rres[j][i]);
02510 img_enc[img->pix_y+block_y+j][img->pix_x+block_x+i]=(imgpel) mb_rres[j][i];
02511 }
02512 }
02513 }
02514
02515
02516
02517
02518
02519
02520
02521
02522
02523
02524
02525
02526
02527
02528
02529
02530
02531
02532
02533
02534
02535
02536
02537 int dct_4x4_sp2(Macroblock *currMB, ColorPlane pl, int block_x,int block_y,int *coeff_cost, int intra, int is_cavlc)
02538 {
02539 int i,j,ilev,coeff_ctr;
02540 int qp_const,level,scan_pos = 0,run = -1;
02541 int nonzero = FALSE;
02542
02543 imgpel **img_enc = enc_picture->p_curr_img;
02544 imgpel **mb_pred = img->mb_pred[pl];
02545
02546 int **mb_rres = img->mb_rres[pl];
02547 int c_err,qp_const2;
02548
02549 int pos_x = block_x >> BLOCK_SHIFT;
02550 int pos_y = block_y >> BLOCK_SHIFT;
02551 int b8 = 2*(pos_y >> 1) + (pos_x >> 1);
02552 int b4 = 2*(pos_y & 0x01) + (pos_x & 0x01);
02553 int* ACLevel = img->cofAC[b8][b4][0];
02554 int* ACRun = img->cofAC[b8][b4][1];
02555 const byte *c_cost = COEFF_COST4x4[params->disthres];
02556 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
02557
02558 int level1;
02559
02560 int qp = (currMB->qpsp);
02561 int qp_sp = (currMB->qpsp);
02562
02563 int qp_per = qp_per_matrix[qp];
02564 int qp_rem = qp_rem_matrix[qp];
02565 int q_bits = Q_BITS + qp_per;
02566 int qp_per_sp = qp_per_matrix[qp_sp];
02567 int qp_rem_sp = qp_rem_matrix[qp_sp];
02568 int q_bits_sp = Q_BITS + qp_per_sp;
02569
02570 levelscale = LevelScale4x4Comp[pl][intra][qp_rem];
02571 invlevelscale = InvLevelScale4x4Comp[pl][intra][qp_rem];
02572 leveloffset = ptLevelOffset4x4[intra][qp];
02573
02574 levelscale_sp = LevelScale4x4Comp[pl][intra][qp_rem_sp];
02575 invlevelscale_sp = InvLevelScale4x4Comp[pl][intra][qp_rem_sp];
02576 leveloffset_sp = ptLevelOffset4x4[intra][qp_sp];
02577
02578 qp_const=(1<<q_bits)/6;
02579 qp_const2=(1<<q_bits_sp)/2;
02580
02581 for (j=0; j< BLOCK_SIZE; j++)
02582 {
02583 for (i=0; i< BLOCK_SIZE; i++)
02584 {
02585
02586 mb_rres[j][i] = lrec[img->pix_y+block_y+j][img->pix_x+block_x+i];
02587
02588 M1[j][i]=mb_pred[j+block_y][i+block_x];
02589 }
02590 }
02591
02592 forward4x4(M1, M1, 0, 0);
02593
02594 for (coeff_ctr=0;coeff_ctr < 16;coeff_ctr++)
02595 {
02596
02597 i=pos_scan[coeff_ctr][0];
02598 j=pos_scan[coeff_ctr][1];
02599
02600 run++;
02601 ilev=0;
02602
02603
02604 level1 = (iabs (M1[j][i]) * levelscale_sp[j][i] + qp_const2) >> q_bits_sp;
02605
02606 c_err = mb_rres[j][i]-isignab(level1, M1[j][i]);
02607
02608 level = iabs(c_err);
02609 if (level != 0)
02610 {
02611 nonzero=TRUE;
02612
02613 *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
02614
02615 ACLevel[scan_pos] = isignab(level,c_err);
02616 ACRun [scan_pos] = run;
02617 ++scan_pos;
02618 run=-1;
02619 }
02620
02621 ilev=c_err + isignab(level1,M1[j][i]) ;
02622 mb_rres[j][i] = ilev * invlevelscale_sp[j][i] << qp_per_sp;
02623
02624 }
02625 ACLevel[scan_pos] = 0;
02626
02627
02628 inverse4x4(mb_rres, mb_rres, 0, 0);
02629
02630 for (j=0; j < BLOCK_SIZE; j++)
02631 for (i=0; i < BLOCK_SIZE; i++)
02632 img_enc[img->pix_y+block_y+j][img->pix_x+block_x+i] =iClip3 (0, img->max_imgpel_value,rshift_rnd_sf(mb_rres[j][i], DQ_BITS));
02633
02634 return nonzero;
02635 }
02636
02637
02638
02639
02640
02641
02642
02643
02644
02645
02646
02647
02648
02649
02650
02651
02652
02653
02654
02655
02656
02657 int dct_chroma_sp2(Macroblock *currMB, int uv,int cr_cbp, int is_cavlc)
02658 {
02659 int i,j,ilev,n2,n1,coeff_ctr,c_err,level ,scan_pos = 0,run = -1;
02660 int m1[BLOCK_SIZE];
02661 int coeff_cost;
02662 int cr_cbp_tmp;
02663 int mp1[BLOCK_SIZE];
02664 const byte *c_cost = COEFF_COST4x4[params->disthres];
02665 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
02666
02667 int b4;
02668 int* DCLevel = img->cofDC[uv+1][0];
02669 int* DCRun = img->cofDC[uv+1][1];
02670 int* ACLevel;
02671 int* ACRun;
02672 int level1;
02673 int **mb_rres = img->mb_rres[uv + 1];
02674
02675 imgpel **mb_pred = img->mb_pred[uv + 1];
02676 int intra = IS_INTRA (currMB);
02677
02678 int qpChroma = currMB->qpc[uv] + img->bitdepth_chroma_qp_scale;
02679 int qpChromaSP = iClip3(-img->bitdepth_chroma_qp_scale, 51, currMB->qpsp + active_pps->chroma_qp_index_offset);
02680
02681 int qp_per = qp_per_matrix[qpChroma];
02682 int qp_rem = qp_rem_matrix[qpChroma];
02683
02684 int qp_per_sp = qp_per_matrix[qpChromaSP];
02685 int qp_rem_sp = qp_rem_matrix[qpChromaSP];
02686 int q_bits_sp = Q_BITS + qp_per;
02687 int qp_const2 = (1 << q_bits_sp)/2;
02688
02689 levelscale = LevelScale4x4Comp[uv + 1][intra][qp_rem];
02690 invlevelscale = InvLevelScale4x4Comp[uv + 1][intra][qp_rem];
02691 leveloffset = ptLevelOffset4x4[intra][qpChroma];
02692
02693 levelscale_sp = LevelScale4x4Comp[uv + 1][intra][qp_rem_sp];
02694 invlevelscale_sp = InvLevelScale4x4Comp[uv + 1][intra][qp_rem_sp];
02695 leveloffset_sp = ptLevelOffset4x4[intra][qpChromaSP];
02696
02697 for (j=0; j < MB_BLOCK_SIZE>>1; j++)
02698 {
02699 for (i=0; i < MB_BLOCK_SIZE>>1; i++)
02700 {
02701 M1[j][i]=mb_pred[j][i];
02702 mb_rres[j][i]=lrec_uv[uv][img->pix_c_y+j][img->pix_c_x+i];
02703 }
02704 }
02705
02706
02707 for (n2=0; n2 <= BLOCK_SIZE; n2 += BLOCK_SIZE)
02708 {
02709 for (n1=0; n1 <= BLOCK_SIZE; n1 += BLOCK_SIZE)
02710 {
02711 forward4x4(M1, M1, n2, n1);
02712 }
02713 }
02714
02715
02716 m1[0]= mb_rres[0][0];
02717 m1[1]= mb_rres[0][4];
02718 m1[2]= mb_rres[4][0];
02719 m1[3]= mb_rres[4][4];
02720
02721
02722 hadamard2x2(M1, mp1);
02723
02724 for (coeff_ctr=0; coeff_ctr < 4; coeff_ctr++)
02725 {
02726 run++;
02727 ilev=0;
02728
02729
02730 level1 = (iabs (mp1[coeff_ctr]) * levelscale_sp[0][0] + 2 * qp_const2) >> (q_bits_sp + 1);
02731
02732 c_err = m1[coeff_ctr] - isignab(level1, mp1[coeff_ctr]);
02733 level = iabs(c_err);
02734
02735 if (level != 0)
02736 {
02737 currMB->cbp_blk |= 0xf0000 << (uv << 2) ;
02738 cr_cbp=imax(1,cr_cbp);
02739 DCLevel[scan_pos] = isignab(level ,c_err);
02740 DCRun [scan_pos] = run;
02741 scan_pos++;
02742 run=-1;
02743 }
02744
02745
02746 ilev = c_err + isignab(level1,mp1[coeff_ctr]) ;
02747
02748 m1[coeff_ctr]= ilev * invlevelscale_sp[0][0] << qp_per_sp;
02749
02750 }
02751 DCLevel[scan_pos] = 0;
02752
02753
02754 ihadamard2x2(m1, m1);
02755
02756 mb_rres[0][0]=m1[0]/2;
02757 mb_rres[0][4]=m1[1]/2;
02758 mb_rres[4][0]=m1[2]/2;
02759 mb_rres[4][4]=m1[3]/2;
02760
02761
02762 coeff_cost=0;
02763 cr_cbp_tmp=0;
02764
02765 for (n2=0; n2 <= BLOCK_SIZE; n2 += BLOCK_SIZE)
02766 {
02767 for (n1=0; n1 <= BLOCK_SIZE; n1 += BLOCK_SIZE)
02768 {
02769 b4 = 2*(n2/4) + (n1/4);
02770 ACLevel = img->cofAC[uv+4][b4][0];
02771 ACRun = img->cofAC[uv+4][b4][1];
02772
02773 run = -1;
02774 scan_pos = 0;
02775
02776 for (coeff_ctr=1; coeff_ctr < 16; coeff_ctr++)
02777 {
02778
02779 i=pos_scan[coeff_ctr][0];
02780 j=pos_scan[coeff_ctr][1];
02781
02782 ++run;
02783 ilev=0;
02784
02785 level1 = (iabs(M1[n2+j][n1+i]) * levelscale_sp[j][i] + qp_const2) >> q_bits_sp;
02786
02787 c_err = mb_rres[n2+j][n1+i] - isignab(level1, M1[n2+j][n1+i]);
02788 level = iabs(c_err) ;
02789
02790 if (level != 0)
02791 {
02792 currMB->cbp_blk |= (int64)1 << (16 + (uv << 2) + ((n2 >> 1) + (n1 >> 2))) ;
02793
02794 coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
02795
02796 cr_cbp_tmp=2;
02797 ACLevel[scan_pos] = isignab(level,c_err);
02798 ACRun [scan_pos] = run;
02799 ++scan_pos;
02800 run=-1;
02801 }
02802
02803
02804 ilev=c_err + isignab(level1,M1[n2+j][n1+i]);
02805 mb_rres[n2+j][n1+i] = ilev * invlevelscale_sp[j][i] << qp_per_sp;
02806 }
02807 ACLevel[scan_pos] = 0;
02808 }
02809 }
02810
02811
02812 if(cr_cbp_tmp==2)
02813 cr_cbp=2;
02814
02815
02816
02817 for (n2=0; n2 <= BLOCK_SIZE; n2 += BLOCK_SIZE)
02818 {
02819 for (n1=0; n1 <= BLOCK_SIZE; n1 += BLOCK_SIZE)
02820 {
02821 inverse4x4(mb_rres, mb_rres, n2, n1);
02822
02823
02824 for (j=0; j < BLOCK_SIZE; j++)
02825 {
02826 for (i=0; i < BLOCK_SIZE; i++)
02827 {
02828 enc_picture->imgUV[uv][img->pix_c_y+j+n2][img->pix_c_x+i +n1 ] = iClip3 (0, img->max_imgpel_value,rshift_rnd_sf(mb_rres[n2+j][n1+i], DQ_BITS));
02829 }
02830 }
02831 }
02832 }
02833
02834 return cr_cbp;
02835 }
02836
02837
02838 void select_dct(ImageParameters *img, Macroblock *currMB)
02839 {
02840 if (img->type!=SP_SLICE)
02841 {
02842 if (img->lossless_qpprime_flag == 1)
02843 {
02844 if (currMB->qp_scaled[img->colour_plane_id] == 0)
02845 {
02846 pDCT_4x4 = dct_4x4_ls;
02847 pDCT_16x16 = dct_16x16_ls;
02848 pDCT_8x8 = dct_8x8_ls;
02849 }
02850 else
02851 {
02852 pDCT_4x4 = dct_4x4;
02853 pDCT_16x16 = dct_16x16;
02854 if (img->currentSlice->symbol_mode == CAVLC)
02855 pDCT_8x8 = dct_8x8_cavlc;
02856 else
02857 pDCT_8x8 = dct_8x8;
02858 dct_cr_4x4[0] = dct_chroma;
02859 dct_cr_4x4[1] = dct_chroma;
02860 }
02861
02862 if (currMB->qp_scaled[1] == 0)
02863 {
02864 dct_cr_4x4[0] = dct_chroma_ls;
02865 }
02866 if (currMB->qp_scaled[2] == 0)
02867 {
02868 dct_cr_4x4[1] = dct_chroma_ls;
02869 }
02870 }
02871 else
02872 {
02873 pDCT_4x4 = dct_4x4;
02874 pDCT_16x16 = dct_16x16;
02875 if (img->currentSlice->symbol_mode == CAVLC)
02876 pDCT_8x8 = dct_8x8_cavlc;
02877 else
02878 pDCT_8x8 = dct_8x8;
02879
02880 dct_cr_4x4[0] = dct_chroma;
02881 dct_cr_4x4[1] = dct_chroma;
02882 }
02883 }
02884 else if(!si_frame_indicator && !sp2_frame_indicator)
02885 {
02886 pDCT_4x4 = dct_4x4_sp;
02887 pDCT_16x16 = dct_16x16;
02888 if (img->currentSlice->symbol_mode == CAVLC)
02889 pDCT_8x8 = dct_8x8_cavlc;
02890 else
02891 pDCT_8x8 = dct_8x8;
02892
02893 dct_cr_4x4[0] = dct_chroma_sp;
02894 dct_cr_4x4[1] = dct_chroma_sp;
02895 }
02896 else
02897 {
02898 pDCT_4x4 = dct_4x4_sp2;
02899 pDCT_16x16 = dct_16x16;
02900 if (img->currentSlice->symbol_mode == CAVLC)
02901 pDCT_8x8 = dct_8x8_cavlc;
02902 else
02903 pDCT_8x8 = dct_8x8;
02904
02905 dct_cr_4x4[0] = dct_chroma_sp2;
02906 dct_cr_4x4[1] = dct_chroma_sp2;
02907 }
02908 }