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