00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "text_defs.h"
00041 #include "mot_code.h"
00042 #include "bitstream.h"
00043 #include "putvlc.h"
00044 #include "mot_util.h"
00045 #include "text_code_mb.h"
00046 #include "text_code.h"
00047
00048 #define SKIPP 6
00049
00050 extern FILE *ftrace;
00051
00052 Void Bits_CountMB_combined ( Int DQUANT,
00053 Int Mode,
00054 Int COD,
00055 Int ACpred_flag,
00056 Int CBP,
00057 Int vop_type,
00058 Bits *bits,
00059 Image *mottext_bitstream,
00060 Int *MB_transp_pattern
00061 );
00062 Int doDCACpred ( Int *qcoeff,
00063 Int *CBP,
00064 Int ncoeffs,
00065 Int x_pos,
00066 Int y_pos,
00067 Int ***DC_store,
00068 Int QP,
00069 Int MB_width,
00070 Int direction[],
00071 Int mid_grey
00072 );
00073 Void nullfill(Int pred[], Int mid_grey);
00074 Int Idir_c(Int val, Int QP);
00075 Int IntraDCSwitch_Decision _P_(( Int Mode,
00076 Int intra_dc_vlc_thr,
00077 Int Qp
00078 ));
00079 Int FindCBP _P_(( Int *qcoeff,
00080 Int Mode,
00081 Int ncoeffs
00082 ));
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 Void VopCodeShapeTextIntraCom(Vop *curr,
00112 Vop *rec_curr, Image *mottext_bitstream)
00113 {
00114 Int QP = GetVopIntraQuantizer(curr);
00115 Int Mode = MODE_INTRA;
00116 Int* qcoeff;
00117 Int i, j;
00118 Int CBP, COD;
00119 Int CBPY, CBPC;
00120 Int num_pixels = GetImageSizeX(GetVopY(curr));
00121 Int num_lines = GetImageSizeY(GetVopY(curr));
00122 Int vop_type;
00123 Int ***DC_store;
00124 Int MB_width = num_pixels / MB_SIZE;
00125 Int MB_height = num_lines / MB_SIZE;
00126 Int m;
00127 Int ACpred_flag=-1;
00128 Int direction[6];
00129 Int switched=0;
00130 Int DQUANT =0;
00131
00132 Bits nbits, *bits;
00133 bits = &nbits;
00134
00135 qcoeff = (Int *) malloc (sizeof (Int) * 384);
00136
00137 #ifdef _RC_DEBUG_
00138 fprintf(ftrace, "RC - VopCodeShapeTextIntraCom(): ---> CODING WITH: %d \n",QP);
00139 #endif
00140
00141 for (i = 0; i < 6; i++)
00142 direction[i] = 0;
00143
00144
00145
00146
00147 DC_store = (Int ***)calloc(MB_width*MB_height, sizeof(Int **));
00148 for (i = 0; i < MB_width*MB_height; i++)
00149 {
00150 DC_store[i] = (Int **)calloc(6, sizeof(Int *));
00151 for (j = 0; j < 6; j++)
00152 DC_store[i][j] = (Int *)calloc(15, sizeof(Int));
00153 }
00154
00155 Bits_Reset (bits);
00156 vop_type = PCT_INTRA;
00157
00158 for (j = 0; j < num_lines/MB_SIZE; j++)
00159 {
00160 for (i = 0; i < num_pixels/MB_SIZE; i++)
00161 {
00162 DQUANT = 0;
00163
00164 COD = 0;
00165 bits->no_intra++;
00166
00167 CodeMB (curr, rec_curr, NULL, i*MB_SIZE, j*MB_SIZE,
00168 num_pixels, QP+DQUANT, MODE_INTRA, qcoeff);
00169
00170 m =0;
00171
00172 DC_store[j*MB_width+i][0][m] = qcoeff[m]*cal_dc_scaler(QP+DQUANT,1);
00173 DC_store[j*MB_width+i][1][m] = qcoeff[m+64]*cal_dc_scaler(QP+DQUANT,1);
00174 DC_store[j*MB_width+i][2][m] = qcoeff[m+128]*cal_dc_scaler(QP+DQUANT,1);
00175 DC_store[j*MB_width+i][3][m] = qcoeff[m+192]*cal_dc_scaler(QP+DQUANT,1);
00176 DC_store[j*MB_width+i][4][m] = qcoeff[m+256]*cal_dc_scaler(QP+DQUANT,2);
00177 DC_store[j*MB_width+i][5][m] = qcoeff[m+320]*cal_dc_scaler(QP+DQUANT,2);
00178
00179 for (m = 1; m < 8; m++)
00180 {
00181 DC_store[j*MB_width+i][0][m] = qcoeff[m];
00182 DC_store[j*MB_width+i][1][m] = qcoeff[m+64];
00183 DC_store[j*MB_width+i][2][m] = qcoeff[m+128];
00184 DC_store[j*MB_width+i][3][m] = qcoeff[m+192];
00185 DC_store[j*MB_width+i][4][m] = qcoeff[m+256];
00186 DC_store[j*MB_width+i][5][m] = qcoeff[m+320];
00187 }
00188 for (m = 0; m < 7; m++)
00189 {
00190 DC_store[j*MB_width+i][0][m+8] = qcoeff[(m+1)*8];
00191 DC_store[j*MB_width+i][1][m+8] = qcoeff[(m+1)*8+64];
00192 DC_store[j*MB_width+i][2][m+8] = qcoeff[(m+1)*8+128];
00193 DC_store[j*MB_width+i][3][m+8] = qcoeff[(m+1)*8+192];
00194 DC_store[j*MB_width+i][4][m+8] = qcoeff[(m+1)*8+256];
00195 DC_store[j*MB_width+i][5][m+8] = qcoeff[(m+1)*8+320];
00196 }
00197
00198 CBP = FindCBP(qcoeff,Mode,64);
00199
00200
00201
00202 if (GetVopIntraACDCPredDisable(curr) == 0)
00203 {
00204 ACpred_flag = doDCACpred(qcoeff, &CBP, 64, i, j, DC_store,
00205 QP+DQUANT, MB_width,
00206 direction,GetVopMidGrey(curr));
00207 }
00208 else
00209 ACpred_flag = -1;
00210
00211 switched = IntraDCSwitch_Decision(Mode,
00212 GetVopIntraDCVlcThr(curr),
00213 QP);
00214 if (switched)
00215 CBP = FindCBP(qcoeff,MODE_INTER,64);
00216 if (DQUANT) Mode=MODE_INTRA_Q;else Mode=MODE_INTRA;
00217
00218 QP+=DQUANT;
00219
00220 CBPY = CBP >> 2;
00221 CBPY = CBPY & 15;
00222 CBPC = CBP & 3;
00223
00224 Bits_CountMB_combined (DQUANT, Mode, COD, ACpred_flag, CBP,
00225 vop_type,
00226 bits, mottext_bitstream,NULL);
00227
00228
00229 MB_CodeCoeff(bits, qcoeff, Mode, CBP, 64,
00230 GetVopIntraACDCPredDisable(curr),
00231 NULL, mottext_bitstream,
00232 NULL, direction,
00233 1 ,
00234 0 ,
00235 switched,
00236 0 );
00237 }
00238 }
00239
00240
00241 for (i = 0; i < MB_width*MB_height; i++)
00242 {
00243 for (j = 0; j < 6; j++)
00244 free(DC_store[i][j]);
00245 free(DC_store[i]);
00246 }
00247 free(DC_store);
00248
00249 free ((Char*)qcoeff);
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 Void VopShapeMotText (Vop *curr, Vop *comp,
00281 Image *MB_decisions, Image *mot_x, Image *mot_y,
00282 Int f_code_for,
00283 Int intra_acdc_pred_disable,
00284 Vop *rec_curr,
00285 Image *mottext_bitstream
00286 )
00287 {
00288 Int Mode=0;
00289 Int QP = GetVopQuantizer(curr);
00290 Int* qcoeff=NULL;
00291 Int i, j;
00292 Int CBP;
00293 Int COD;
00294 Int CBPY, CBPC;
00295 Int MB_in_width, MB_in_height, B_in_width, mbnum, boff;
00296 SInt p;
00297 SInt *ptr=NULL;
00298 Float *motx_ptr=NULL, *moty_ptr=NULL;
00299 Int num_pixels;
00300 Int num_lines;
00301 Int vop_type=PCT_INTER;
00302 Int ***DC_store=NULL;
00303 Int m, n;
00304 Int ACpred_flag=-1;
00305 Int direction[6];
00306 Int switched=0;
00307 Int DQUANT=0;
00308
00309 Bits nbits, *bits;
00310 bits = &nbits;
00311
00312 qcoeff = (Int *) malloc (sizeof (Int) * 384);
00313
00314 num_pixels = GetImageSizeX(GetVopY(curr));
00315 num_lines = GetImageSizeY(GetVopY(curr));
00316 MB_in_width = num_pixels / MB_SIZE;
00317 MB_in_height = num_lines / MB_SIZE;
00318 B_in_width = 2 * MB_in_width;
00319
00320 for (i = 0; i < 6; i++) direction[i] = 0;
00321
00322 #ifdef _RC_DEBUG_
00323 printf("RC - VopShapeMotText(): ---> CODING WITH: %d \n",QP);
00324 #endif
00325
00326
00327
00328 DC_store = (Int ***)calloc(MB_in_width*MB_in_height,
00329 sizeof(Int **));
00330 for (i = 0; i < MB_in_width*MB_in_height; i++)
00331 {
00332 DC_store[i] = (Int **)calloc(6, sizeof(Int *));
00333 for (j = 0; j < 6; j++)
00334 DC_store[i][j] = (Int *)calloc(15, sizeof(Int));
00335 }
00336
00337 Bits_Reset (bits);
00338
00339 vop_type = PCT_INTER;
00340
00341 ptr = (SInt *) GetImageData(MB_decisions);
00342 motx_ptr = (Float *) GetImageData(mot_x);
00343 moty_ptr = (Float *) GetImageData(mot_y);
00344
00345 for (j = 0; j < num_lines/MB_SIZE; j++)
00346 {
00347 for (i = 0; i < MB_in_width; i++)
00348 {
00349 switched=0;
00350 p = *ptr;
00351 DQUANT = 0;
00352
00353
00354 for (m = 0; m < 6; m++)
00355 {
00356 DC_store[j*MB_in_width+i][m][0] = GetVopMidGrey(curr)*8;
00357 for (n = 1; n < 15; n++)
00358 DC_store[j*MB_in_width+i][m][n] = 0;
00359 }
00360
00361 switch (p)
00362 {
00363
00364 case MBM_INTRA:
00365 Mode = (DQUANT == 0) ? MODE_INTRA : MODE_INTRA_Q;
00366 bits->no_intra++;
00367 break;
00368
00369 case MBM_INTER16:
00370 Mode = (DQUANT == 0) ? MODE_INTER : MODE_INTER_Q;
00371 bits->no_inter++;
00372 break;
00373
00374 case MBM_INTER8:
00375 Mode = MODE_INTER4V;
00376 bits->no_inter4v++;
00377 DQUANT = 0;
00378 break;
00379
00380 default:
00381 printf("invalid MB_decision value :%d\n", p);
00382 exit(0);
00383 }
00384
00385 CodeMB (curr, rec_curr, comp, i*MB_SIZE, j*MB_SIZE,
00386 num_pixels, QP + DQUANT, Mode, qcoeff);
00387
00388 mbnum = j*MB_in_width + i;
00389 boff = (2 * (mbnum / MB_in_width) * B_in_width
00390 + 2 * (mbnum % MB_in_width));
00391
00392 CBP = FindCBP(qcoeff,Mode,64);
00393
00394 if ((CBP == 0) && (p == 1) && (*(motx_ptr +boff) == 0.0)
00395 && (*(moty_ptr +boff) == 0.0))
00396 {
00397 COD = 1;
00398 BitstreamPutBits(mottext_bitstream, (long) (COD), 1L);
00399 bits->COD ++;
00400
00401 *ptr = SKIPP;
00402 Mode = MODE_INTER;
00403 }
00404 else
00405 {
00406 COD = 0;
00407
00408 if ((Mode == MODE_INTRA) || (Mode == MODE_INTRA_Q))
00409 {
00410
00411
00412 m =0;
00413
00414 DC_store[j*MB_in_width+i][0][m] = qcoeff[m]*cal_dc_scaler(QP+DQUANT,1);
00415 DC_store[j*MB_in_width+i][1][m] = qcoeff[m+64]*cal_dc_scaler(QP+DQUANT,1);
00416 DC_store[j*MB_in_width+i][2][m] = qcoeff[m+128]*cal_dc_scaler(QP+DQUANT,1);
00417 DC_store[j*MB_in_width+i][3][m] = qcoeff[m+192]*cal_dc_scaler(QP+DQUANT,1);
00418 DC_store[j*MB_in_width+i][4][m] = qcoeff[m+256]*cal_dc_scaler(QP+DQUANT,2);
00419 DC_store[j*MB_in_width+i][5][m] = qcoeff[m+320]*cal_dc_scaler(QP+DQUANT,2);
00420
00421 for (m = 1; m < 8; m++)
00422 {
00423 DC_store[j*MB_in_width+i][0][m] = qcoeff[m];
00424 DC_store[j*MB_in_width+i][1][m] = qcoeff[m+64];
00425 DC_store[j*MB_in_width+i][2][m] = qcoeff[m+128];
00426 DC_store[j*MB_in_width+i][3][m] = qcoeff[m+192];
00427 DC_store[j*MB_in_width+i][4][m] = qcoeff[m+256];
00428 DC_store[j*MB_in_width+i][5][m] = qcoeff[m+320];
00429 }
00430 for (m = 0; m < 7; m++)
00431 {
00432 DC_store[j*MB_in_width+i][0][m+8] = qcoeff[(m+1)*8];
00433 DC_store[j*MB_in_width+i][1][m+8] = qcoeff[(m+1)*8+64];
00434 DC_store[j*MB_in_width+i][2][m+8] = qcoeff[(m+1)*8+128];
00435 DC_store[j*MB_in_width+i][3][m+8] = qcoeff[(m+1)*8+192];
00436 DC_store[j*MB_in_width+i][4][m+8] = qcoeff[(m+1)*8+256];
00437 DC_store[j*MB_in_width+i][5][m+8] = qcoeff[(m+1)*8+320];
00438 }
00439
00440 if (intra_acdc_pred_disable == 0)
00441 ACpred_flag = doDCACpred(qcoeff, &CBP, 64, i, j,
00442 DC_store,
00443 QP+DQUANT, MB_in_width,
00444 direction,GetVopMidGrey(curr));
00445 else
00446 ACpred_flag = -1;
00447 }
00448
00449 switched = IntraDCSwitch_Decision(Mode,
00450 GetVopIntraDCVlcThr(curr),
00451 QP);
00452 if (switched)
00453 CBP = FindCBP(qcoeff,MODE_INTER,64);
00454
00455 CBPY = CBP >> 2;
00456 CBPY = CBPY & 15;
00457 CBPC = CBP & 3;
00458
00459 Bits_CountMB_combined (DQUANT, Mode, COD, ACpred_flag, CBP,
00460 vop_type, bits,
00461 mottext_bitstream,NULL);
00462
00463 Bits_CountMB_Motion( mot_x, mot_y, NULL,
00464 MB_decisions, i, j, f_code_for, 0 ,
00465 mottext_bitstream,
00466 1 , 0,
00467 (Int **)NULL, 0 );
00468
00469 MB_CodeCoeff(bits, qcoeff, Mode, CBP, 64,
00470 intra_acdc_pred_disable,
00471 NULL, mottext_bitstream,
00472 NULL, direction,
00473 1,
00474 0,
00475 switched,
00476 0 );
00477 }
00478
00479 ptr++;
00480
00481 }
00482 }
00483
00484
00485 for (i = 0; i < MB_in_width*MB_in_height; i++)
00486 {
00487
00488 for (j = 0; j < 6; j++)
00489 free(DC_store[i][j]);
00490 free(DC_store[i]);
00491 }
00492 free(DC_store);
00493
00494 free ((Char*)qcoeff);
00495
00496 }
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 Void Bits_CountMB_combined(Int DQUANT, Int Mode, Int COD, Int ACpred_flag,
00526 Int CBP, Int vop_type,
00527 Bits* bits, Image *mottext_bitstream,Int *MB_transp_pattern)
00528 {
00529 Int cbpy ,cbpc, length;
00530 Int MBtype=-1;
00531
00532 if ( Mode == MODE_INTRA ) MBtype = 3;
00533 if ( Mode == MODE_INTER ) MBtype = 0;
00534 if ( Mode == MODE_INTRA_Q) MBtype = 4;
00535 if ( Mode == MODE_INTER_Q) MBtype = 1;
00536 if ( Mode == MODE_INTER4V) MBtype = 2;
00537
00538
00539
00540
00541
00542 if ( Mode == MODE_GMC) MBtype = 0;
00543 if ( Mode == MODE_GMC_Q) MBtype = 1;
00544
00545
00546 #ifdef D_TRACE
00547 fprintf(ftrace, "DQUANT : %d\tMODE : %d\tVop Type : %d\n", DQUANT, Mode, vop_type);
00548 fprintf(ftrace, "COD : %d\tCBP : %d\tAC Pred Flag : %d\n\n", COD, CBP, ACpred_flag);
00549 #endif
00550
00551 cbpc = CBP & 3;
00552 cbpy = CBP>>2;
00553
00554
00555
00556 if (vop_type != PCT_INTRA )
00557 {
00558 if (COD)
00559 {
00560 printf("COD = 1 in Bits_CountMB_combined \n");
00561 printf("This function should not be used if COD is '1' \n");
00562 exit(1);
00563 }
00564
00565
00566 BitstreamPutBits(mottext_bitstream, (long)(COD), 1L);
00567 bits->COD++;
00568 }
00569
00570
00571
00572 if (vop_type == PCT_INTRA)
00573 length = PutMCBPC_Intra (cbpc, MBtype, mottext_bitstream);
00574 else
00575 length = PutMCBPC_Inter (cbpc, MBtype, mottext_bitstream);
00576
00577 bits->MCBPC += length;
00578
00579
00580
00581
00582
00583 if (((Mode == MODE_INTER) || (Mode == MODE_INTER_Q) || (Mode == MODE_GMC) || (Mode == MODE_GMC_Q)) && (vop_type == PCT_SPRITE))
00584
00585 {
00586 if ((Mode == MODE_INTER) || (Mode == MODE_INTER_Q))
00587 BitstreamPutBits(mottext_bitstream, (long) 0, 1L);
00588
00589
00590
00591 if ((Mode == MODE_GMC) || (Mode == MODE_GMC_Q))
00592
00593 BitstreamPutBits(mottext_bitstream, (long) 1, 1L);
00594
00595 bits->MCBPC += 1;
00596 }
00597
00598
00599
00600 if ((Mode == MODE_INTRA || Mode==MODE_INTRA_Q) && ACpred_flag != -1)
00601 {
00602 BitstreamPutBits(mottext_bitstream, (long)ACpred_flag, 1L);
00603 bits->ACpred_flag += 1;
00604 }
00605
00606
00607
00608 length = PutCBPY (cbpy, (Char)(Mode==MODE_INTRA||Mode==MODE_INTRA_Q),NULL,mottext_bitstream);
00609
00610 bits->CBPY += length;
00611
00612
00613
00614
00615
00616
00617 if ((Mode == MODE_INTER_Q) || (Mode == MODE_INTRA_Q)|| (Mode == MODE_GMC_Q))
00618
00619 {
00620 switch (DQUANT)
00621 {
00622 case -1:
00623 BitstreamPutBits(mottext_bitstream, 0L, 2L);
00624 break;
00625 case -2:
00626 BitstreamPutBits(mottext_bitstream, 1L, 2L);
00627 break;
00628 case 1:
00629 BitstreamPutBits(mottext_bitstream, 2L, 2L);
00630 break;
00631 case 2:
00632 BitstreamPutBits(mottext_bitstream, 3L, 2L);
00633 break;
00634 default:
00635 fprintf(stderr,"Invalid DQUANT\n");
00636 exit(1);
00637 }
00638 bits->DQUANT += 2;
00639 }
00640 }
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 Int doDCACpred(Int *qcoeff, Int *CBP, Int ncoeffs, Int x_pos, Int y_pos,
00673 Int ***DC_store, Int QP, Int MB_width,
00674 Int direction[], Int mid_grey )
00675 {
00676 Int i, m;
00677 Int block_A, block_B, block_C;
00678 Int Xpos[6] = {-1, 0, -1, 0, -1, -1};
00679 Int Ypos[6] = {-1, -1, 0, 0, -1, -1};
00680 Int Xtab[6] = {1, 0, 3, 2, 4, 5};
00681 Int Ytab[6] = {2, 3, 0, 1, 4, 5};
00682 Int Ztab[6] = {3, 2, 1, 0, 4, 5};
00683 Int grad_hor, grad_ver, DC_pred;
00684 Int pred_A[15], pred_C[15];
00685 Int S = 0, S1, S2;
00686 Int diff;
00687 Int pcoeff[384];
00688 Int ACpred_flag=-1;
00689
00690
00691 for (i = 0; i < (6*ncoeffs); i++)
00692 {
00693 pcoeff[i] = qcoeff[i];
00694 }
00695
00696 for (i = 0; i < 6; i++)
00697 {
00698 if ((x_pos == 0) && y_pos == 0)
00699 {
00700 block_A = (i == 1 || i == 3) ? DC_store[y_pos*MB_width+(x_pos+Xpos[i])][Xtab[i]][0] : mid_grey*8;
00701 block_B = (i == 3) ? DC_store[(y_pos+Ypos[i])*MB_width+(x_pos+Xpos[i])][Ztab[i]][0] : mid_grey*8;
00702 block_C = (i == 2 || i == 3) ? DC_store[(y_pos+Ypos[i])*MB_width+x_pos][Ytab[i]][0] : mid_grey*8;
00703 }
00704 else if (x_pos == 0)
00705 {
00706 block_A = (i == 1 || i == 3) ? DC_store[y_pos*MB_width+(x_pos+Xpos[i])][Xtab[i]][0] : mid_grey*8;
00707 block_B = (i == 1 || i == 3) ? DC_store[(y_pos+Ypos[i])*MB_width+(x_pos+Xpos[i])][Ztab[i]][0] : mid_grey*8;
00708 block_C = DC_store[(y_pos+Ypos[i])*MB_width+x_pos][Ytab[i]][0];
00709 }
00710 else if (y_pos == 0)
00711 {
00712 block_A = DC_store[y_pos*MB_width+(x_pos+Xpos[i])][Xtab[i]][0];
00713 block_B = (i == 2 || i == 3) ? DC_store[(y_pos+Ypos[i])*MB_width+(x_pos+Xpos[i])][Ztab[i]][0] : mid_grey*8;
00714 block_C = (i == 2 || i == 3) ? DC_store[(y_pos+Ypos[i])*MB_width+x_pos][Ytab[i]][0] : mid_grey*8;
00715 }
00716 else
00717 {
00718 block_A = DC_store[y_pos*MB_width+(x_pos+Xpos[i])][Xtab[i]][0];
00719 block_B = (DC_store[(y_pos+Ypos[i])*MB_width+(x_pos+Xpos[i])]
00720 [Ztab[i]][0]);
00721 block_C = DC_store[(y_pos+Ypos[i])*MB_width+x_pos][Ytab[i]][0];
00722 }
00723 grad_hor = block_B - block_C;
00724 grad_ver = block_A - block_B;
00725
00726 if ((ABS(grad_ver)) < (ABS(grad_hor)))
00727 {
00728 DC_pred = block_C;
00729 direction[i] = 2;
00730 }
00731 else
00732 {
00733 DC_pred = block_A;
00734 direction[i] = 1;
00735 }
00736
00737 pcoeff[i*ncoeffs] = qcoeff[i*ncoeffs] - (DC_pred+cal_dc_scaler(QP,(i<4)?1:2)/2)/cal_dc_scaler(QP,(i<4)?1:2);
00738
00739
00740 if ((x_pos == 0) && y_pos == 0)
00741 {
00742 if (i == 1 || i == 3)
00743 for (m = 0; m < 15; m++)
00744 pred_A[m] = Idir_c(((DC_store[y_pos*MB_width+(x_pos+Xpos[i])][Xtab[i]][m]) * QP*2) , 2*QP);
00745 else
00746 nullfill(pred_A,mid_grey);
00747 if (i == 2 || i == 3)
00748 for (m = 0; m < 15; m++)
00749 pred_C[m] = Idir_c(((DC_store[(y_pos+Ypos[i])*MB_width+x_pos][Ytab[i]][m]) * QP*2) , 2*QP);
00750 else
00751 nullfill(pred_C,mid_grey);
00752 }
00753 else if (x_pos == 0)
00754 {
00755 if (i == 1 || i == 3)
00756 for (m = 0; m < 15; m++)
00757 pred_A[m] = Idir_c(((DC_store[y_pos*MB_width+(x_pos+Xpos[i])][Xtab[i]][m]) * QP*2) , 2*QP);
00758 else
00759 nullfill(pred_A,mid_grey);
00760 for (m = 0; m < 15; m++)
00761 pred_C[m] = Idir_c(((DC_store[(y_pos+Ypos[i])*MB_width+x_pos][Ytab[i]][m]) * QP*2) , 2*QP);
00762 }
00763 else if (y_pos == 0)
00764 {
00765 for (m = 0; m < 15; m++)
00766 pred_A[m] = Idir_c(((DC_store[y_pos*MB_width+(x_pos+Xpos[i])][Xtab[i]][m]) * QP*2) , 2*QP);
00767 if (i == 2 || i == 3)
00768 for (m = 0; m < 15; m++)
00769 pred_C[m] = Idir_c(((DC_store[(y_pos+Ypos[i])*MB_width+x_pos][Ytab[i]][m]) * QP*2) , 2*QP);
00770 else
00771 nullfill(pred_C,mid_grey);
00772 }
00773 else
00774 {
00775 for (m = 0; m < 15; m++)
00776 {
00777 pred_A[m] = Idir_c(((DC_store[y_pos*MB_width+(x_pos+Xpos[i])][Xtab[i]][m]) * QP*2) , 2*QP);
00778 pred_C[m] = Idir_c(((DC_store[(y_pos+Ypos[i])*MB_width+x_pos][Ytab[i]][m]) * QP*2) , 2*QP);
00779 }
00780 }
00781
00782 #if 1
00783 S1 = 0;
00784 S2 = 0;
00785
00786 if (direction[i] == 1)
00787 {
00788 for (m = 0; m < 7; m++)
00789 {
00790 S1 += ABS(qcoeff[i*ncoeffs+(m+1)*8]);
00791 diff = pcoeff[i*ncoeffs+(m+1)*8]
00792 = qcoeff[i*ncoeffs+(m+1)*8] - pred_A[m+8];
00793 S2 += ABS(diff);
00794 }
00795 }
00796 else
00797 {
00798 for (m = 1; m < 8; m++)
00799 {
00800 S1 += ABS(qcoeff[i*ncoeffs+m]);
00801 diff = pcoeff[i*ncoeffs+m]
00802 = qcoeff[i*ncoeffs+m] - pred_C[m];
00803 S2 += ABS(diff);
00804 }
00805 }
00806 S += (S1 - S2);
00807 #endif
00808 }
00809
00810 if (S >=0)
00811 {
00812 for (i=0;i<ncoeffs*6; i++)
00813
00814
00815 if ((i%64)&&(abs(pcoeff[i])>2047))
00816 {
00817 printf("predicted AC out of range");
00818 S=-1;break;
00819 }
00820 }
00821 if (S >= 0)
00822 {
00823 ACpred_flag = 1;
00824 for (i = 0; i < ncoeffs*6; i++)
00825 {
00826 qcoeff[i] = pcoeff[i];
00827 }
00828
00829 *CBP = FindCBP(qcoeff, MODE_INTRA, 64);
00830 }
00831 else
00832 {
00833 ACpred_flag = 0;
00834 for (i = 0; i < 6; i++)
00835 {
00836 qcoeff[i*ncoeffs] = pcoeff[i*ncoeffs];
00837 direction[i] = 0;
00838 }
00839 }
00840 return ACpred_flag;
00841 }
00842
00847 Void nullfill(Int pred[], Int mid_grey)
00848 {
00849 Int i;
00850
00851 pred[0] = mid_grey*8;
00852 for (i = 1; i < 15; i++)
00853 {
00854 pred[i] = 0;
00855 }
00856 }
00857
00858 Int Idir_c(Int val, Int QP)
00859 {
00860 if (val<0) return (val-QP/2)/QP;
00861 else return (val+QP/2)/QP;
00862 }
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879 Int IntraDCSwitch_Decision(Int Mode,Int intra_dc_vlc_thr,Int Qp)
00880 {
00881 Int switched =0;
00882 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00883 {
00884 if (intra_dc_vlc_thr==0)
00885 switched=0;
00886 else if (intra_dc_vlc_thr==7)
00887 switched=1;
00888 else if (Qp>=intra_dc_vlc_thr*2+11)
00889 switched=1;
00890 }
00891
00892 return switched;
00893 }
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906 Int cal_dc_scaler (Int QP, Int type)
00907 {
00908
00909 Int dc_scaler;
00910 if (type == 1)
00911 {
00912 if (QP > 0 && QP < 5) dc_scaler = 8;
00913 else if (QP > 4 && QP < 9) dc_scaler = 2 * QP;
00914 else if (QP > 8 && QP < 25) dc_scaler = QP + 8;
00915 else dc_scaler = 2 * QP - 16;
00916 }
00917 else
00918 {
00919 if (QP > 0 && QP < 5) dc_scaler = 8;
00920 else if (QP > 4 && QP < 25) dc_scaler = (QP + 13) / 2;
00921 else dc_scaler = QP - 6;
00922 }
00923 return dc_scaler;
00924 }
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945 Int
00946 FindCBP (Int* qcoeff, Int Mode, Int ncoeffs)
00947 {
00948 Int i,j;
00949 Int CBP = 0;
00950 Int intra = (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
00951
00952
00953
00954 for (i = 0; i < 6; i++)
00955 {
00956 for (j = i*ncoeffs + intra; j < (i+1)*ncoeffs; j++)
00957 {
00958
00959 if (qcoeff[j])
00960 {
00961 if (i == 0) {CBP |= 32;}
00962 else if (i == 1) {CBP |= 16;}
00963 else if (i == 2) {CBP |= 8;}
00964 else if (i == 3) {CBP |= 4;}
00965 else if (i == 4) {CBP |= 2;}
00966 else if (i == 5) {CBP |= 1;}
00967 else
00968 {
00969 fprintf (stderr, "Error in CBP assignment\n");
00970 exit(-1);
00971 }
00972
00973 break;
00974 }
00975 }
00976 }
00977
00978 return CBP;
00979 }
00980