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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
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
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 #include <assert.h>
00141 #include <sys/param.h>
00142 #include "all.h"
00143 #include "mtypes.h"
00144 #include "bitio.h"
00145 #include "frames.h"
00146 #include "prototypes.h"
00147 #include "param.h"
00148 #include "mheaders.h"
00149 #include "fsize.h"
00150 #include "postdct.h"
00151 #include "mpeg.h"
00152 #include "parallel.h"
00153 #include "rate.h"
00154 #include "opts.h"
00155
00156
00157
00158
00159
00160 static int32 zeroDiff;
00161 static int numPIBlocks = 0;
00162 static int numPPBlocks = 0;
00163 static int numPSkipped = 0;
00164 static int numPIBits = 0;
00165 static int numPPBits = 0;
00166 static int numFrames = 0;
00167 static int numFrameBits = 0;
00168 static int32 totalTime = 0;
00169 static int qscaleP;
00170 static float totalSNR = 0.0;
00171 static float totalPSNR = 0.0;
00172 extern Block **dct, **dctr, **dctb;
00173 extern dct_data_type **dct_data;
00174
00175
00176
00177
00178
00179 static boolean ZeroMotionBetter _ANSI_ARGS_((LumBlock currentBlock,
00180 MpegFrame *prev, int by, int bx,
00181 int my, int mx));
00182
00183 static boolean DoIntraCode _ANSI_ARGS_((LumBlock currentBlock,
00184 MpegFrame *prev, int by, int bx,
00185 int motionY, int motionX));
00186
00187 static boolean ZeroMotionSufficient _ANSI_ARGS_((LumBlock currentBlock,
00188 MpegFrame *prev,
00189 int by, int bx));
00190
00191 #ifdef BLEAH
00192 static void ComputeAndPrintPframeMAD _ANSI_ARGS_((LumBlock currentBlock,
00193 MpegFrame *prev,
00194 int by, int bx,
00195 int my, int mx,
00196 int numBlock));
00197 #endif
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 void
00217 GenPFrame(bb, current, prev)
00218 BitBucket *bb;
00219 MpegFrame *current;
00220 MpegFrame *prev;
00221 {
00222 extern int **pmvHistogram;
00223 FlatBlock fba[6], fb[6];
00224 Block dec[6];
00225 int32 y_dc_pred, cr_dc_pred, cb_dc_pred;
00226 int x, y;
00227 int motionX = 0, motionY = 0;
00228 int oldMotionX = 0, oldMotionY = 0;
00229 int offsetX, offsetY;
00230 int tempX, tempY;
00231 int motionXrem, motionXquot;
00232 int motionYrem, motionYquot;
00233 int pattern;
00234 int mbAddrInc = 1;
00235 boolean useMotion;
00236 int numIBlocks = 0;
00237 int numPBlocks = 0;
00238 int numSkipped = 0;
00239 int numIBits = 0;
00240 int numPBits = 0;
00241 int totalBits;
00242 int totalFrameBits;
00243 int32 startTime, endTime;
00244 int lastBlockX, lastBlockY;
00245 int lastX, lastY;
00246 int fy, fx;
00247 LumBlock currentBlock;
00248 register int ix, iy;
00249 int mbAddress;
00250 int slicePos;
00251 register int index;
00252 float snr[3], psnr[3];
00253 int QScale;
00254 BlockMV *info;
00255 int bitstreamMode, newQScale;
00256 int rc_blockStart = 0;
00257 boolean overflowChange = FALSE;
00258 int overflowValue = 0;
00259
00260
00261 if (collect_quant) {fprintf(collect_quant_fp, "# P\n");}
00262 if (dct==NULL) AllocDctBlocks();
00263 numFrames++;
00264 totalFrameBits = bb->cumulativeBits;
00265 startTime = time_elapsed();
00266
00267 DBG_PRINT(("Generating pframe\n"));
00268
00269 QScale = GetPQScale();
00270
00271 bitstreamMode = getRateMode();
00272 if (bitstreamMode == FIXED_RATE) {
00273 targetRateControl(current);
00274 }
00275
00276 Mhead_GenPictureHeader(bb, P_FRAME, current->id, fCodeP);
00277
00278 if (specificsOn) {
00279
00280 newQScale = SpecLookup(current->id, 0, 0 , &info , QScale);
00281 if (newQScale != -1) {
00282 QScale = newQScale;
00283 }
00284
00285 newQScale = SpecLookup(current->id, 1, 1, &info , QScale);
00286 if (newQScale != -1) {
00287 QScale = newQScale;
00288 }
00289 }
00290
00291 DBG_PRINT(("Slice Header\n"));
00292 Mhead_GenSliceHeader(bb, 1, QScale, NULL, 0);
00293
00294 if ( referenceFrame == DECODED_FRAME ) {
00295 Frame_AllocDecoded(current, TRUE);
00296 } else if ( printSNR ) {
00297 Frame_AllocDecoded(current, FALSE);
00298 }
00299
00300
00301 Frame_AllocBlocks(current);
00302 BlockifyFrame(current);
00303
00304
00305 y_dc_pred = cr_dc_pred = cb_dc_pred = 128;
00306
00307 totalBits = bb->cumulativeBits;
00308
00309 if ( (! pixelFullSearch) && (! prev->halfComputed) ) {
00310 ComputeHalfPixelData(prev);
00311 }
00312
00313 lastBlockX = Fsize_x>>3;
00314 lastBlockY = Fsize_y>>3;
00315 lastX = lastBlockX-2;
00316 lastY = lastBlockY-2;
00317 mbAddress = 0;
00318
00319
00320 for (y = 0; y < lastBlockY; y += 2) {
00321 for (x = 0; x < lastBlockX; x += 2) {
00322
00323 BLOCK_TO_FRAME_COORD(y, x, fy, fx);
00324 for ( iy = 0; iy < 16; iy++ ) {
00325 for ( ix = 0; ix < 16; ix++ ) {
00326 currentBlock[iy][ix] = (int16)current->orig_y[fy+iy][fx+ix];
00327 }
00328 }
00329
00330
00331 if (specificsOn) {
00332 (void) SpecLookup(current->id, 2, mbAddress, &info, QScale);
00333 if (info != (BlockMV*)NULL) {
00334 if (info->typ == TYP_SKIP) {
00335 motionX = motionY = 0;
00336 useMotion = TRUE;
00337 goto no_search;
00338 } else {
00339 motionX = info->fx;
00340 motionY = info->fy;
00341 useMotion = TRUE;
00342 goto no_search;
00343 }}
00344
00345 }
00346
00347
00348
00349
00350 if ( ZeroMotionSufficient(currentBlock, prev, y, x) ) {
00351 motionX = 0;
00352 motionY = 0;
00353 useMotion = TRUE;
00354 } else {
00355 useMotion = PMotionSearch(currentBlock, prev, y, x,
00356 &motionY, &motionX);
00357 if ( useMotion ) {
00358 if ( ZeroMotionBetter(currentBlock, prev, y, x, motionY,
00359 motionX) ) {
00360 motionX = 0;
00361 motionY = 0;
00362 }
00363 if (IntraPBAllowed)
00364 useMotion = (! DoIntraCode(currentBlock, prev, y, x,
00365 motionY, motionX));
00366 }
00367 }
00368
00369 no_search:
00370
00371 dct_data[y][x].useMotion = useMotion;
00372 if ( ! useMotion ) {
00373
00374 numIBlocks++;
00375
00376
00377 if (collect_quant && (collect_quant_detailed & 1)) fprintf(collect_quant_fp, "l\n");
00378 mp_fwd_dct_block2(current->y_blocks[y][x], dct[y][x]);
00379 mp_fwd_dct_block2(current->y_blocks[y][x+1], dct[y][x+1]);
00380 mp_fwd_dct_block2(current->y_blocks[y+1][x], dct[y+1][x]);
00381 mp_fwd_dct_block2(current->y_blocks[y+1][x+1], dct[y+1][x+1]);
00382 if (collect_quant && (collect_quant_detailed & 1)) fprintf(collect_quant_fp, "c\n");
00383 mp_fwd_dct_block2(current->cb_blocks[y>>1][x>>1], dctb[y>>1][x>>1]);
00384 mp_fwd_dct_block2(current->cr_blocks[y>>1][x>>1], dctr[y>>1][x>>1]);
00385
00386 } else {
00387
00388 numPBlocks++;
00389
00390 pattern = 63;
00391 ComputeDiffDCTs(current, prev, y, x, motionY, motionX,
00392 &pattern);
00393
00394 assert(motionX+searchRangeP+1 >= 0);
00395 assert(motionY+searchRangeP+1 >= 0);
00396
00397 #ifdef BLEAH
00398 if ( motionX+searchRangeP+1 > 2*searchRangeP+2 )
00399 {
00400 fprintf(stdout, "motionX = %d, searchRangeP = %d\n",
00401 motionX, searchRangeP);
00402 }
00403 #endif
00404
00405 if ( computeMVHist ) {
00406 assert(motionX+searchRangeP+1 <= 2*searchRangeP+2);
00407 assert(motionY+searchRangeP+1 <= 2*searchRangeP+2);
00408 pmvHistogram[motionX+searchRangeP+1][motionY+searchRangeP+1]++;
00409 }
00410
00411 dct_data[y][x].pattern = pattern;
00412 dct_data[y][x].fmotionX = motionX;
00413 dct_data[y][x].fmotionY = motionY;
00414
00415 }
00416 mbAddress++;
00417 }}
00418
00419 mbAddress = 0;
00420 for (y = 0; y < lastBlockY; y += 2) {
00421 for (x = 0; x < lastBlockX; x += 2) {
00422 slicePos = (mbAddress % blocksPerSlice);
00423
00424 if ( (slicePos == 0) && (mbAddress != 0) ) {
00425 if (specificsOn) {
00426
00427 newQScale = SpecLookup(current->id, 1, mbAddress/blocksPerSlice,
00428 &info , QScale);
00429 if (newQScale != -1) QScale = newQScale;
00430 }
00431
00432 Mhead_GenSliceEnder(bb);
00433 Mhead_GenSliceHeader(bb, 1+(y>>1), QScale, NULL, 0);
00434
00435
00436 oldMotionX = 0; oldMotionY = 0;
00437 y_dc_pred = cr_dc_pred = cb_dc_pred = 128;
00438
00439 mbAddrInc = 1+(x>>1);
00440 }
00441
00442
00443 if (bitstreamMode == FIXED_RATE) {
00444 rc_blockStart = bb->cumulativeBits;
00445 newQScale = needQScaleChange(qscaleP,
00446 current->y_blocks[y][x],
00447 current->y_blocks[y][x+1],
00448 current->y_blocks[y+1][x],
00449 current->y_blocks[y+1][x+1]);
00450 if (newQScale > 0) {
00451 QScale = newQScale;
00452 }
00453 }
00454
00455
00456 if (specificsOn) {
00457 newQScale = SpecLookup(current->id, 2, mbAddress, &info, QScale);
00458 if (newQScale != -1) {
00459 QScale = newQScale;
00460 }
00461 }
00462
00463 if (! dct_data[y][x].useMotion) {
00464 GEN_I_BLOCK(P_FRAME, current, bb, mbAddrInc, QScale);
00465 mbAddrInc = 1;
00466
00467 numIBits += (bb->cumulativeBits-totalBits);
00468 totalBits = bb->cumulativeBits;
00469
00470
00471 oldMotionX = 0; oldMotionY = 0;
00472
00473 if ( decodeRefFrames ) {
00474
00475 Mpost_UnQuantZigBlock(fb[0], dec[0], QScale, TRUE);
00476 Mpost_UnQuantZigBlock(fb[1], dec[1], QScale, TRUE);
00477 Mpost_UnQuantZigBlock(fb[2], dec[2], QScale, TRUE);
00478 Mpost_UnQuantZigBlock(fb[3], dec[3], QScale, TRUE);
00479 Mpost_UnQuantZigBlock(fb[4], dec[4], QScale, TRUE);
00480 Mpost_UnQuantZigBlock(fb[5], dec[5], QScale, TRUE);
00481
00482
00483 for ( index = 0; index < 6; index++ ) {
00484 mpeg_jrevdct((int16 *)dec[index]);
00485 }
00486
00487
00488 BlockToData(current->decoded_y, dec[0], y, x);
00489 BlockToData(current->decoded_y, dec[1], y, x+1);
00490 BlockToData(current->decoded_y, dec[2], y+1, x);
00491 BlockToData(current->decoded_y, dec[3], y+1, x+1);
00492 BlockToData(current->decoded_cb, dec[4], y>>1, x>>1);
00493 BlockToData(current->decoded_cr, dec[5], y>>1, x>>1);
00494 }
00495 } else {
00496 int fCode = fCodeP;
00497
00498
00499 y_dc_pred = cr_dc_pred = cb_dc_pred = 128;
00500
00501 pattern = dct_data[y][x].pattern;
00502 motionX = dct_data[y][x].fmotionX;
00503 motionY = dct_data[y][x].fmotionY;
00504
00505 #ifdef BLEAH
00506 ComputeAndPrintPframeMAD(currentBlock, prev, y, x, motionY, motionX, mbAddress);
00507 #endif
00508
00509 if ( pixelFullSearch ) {
00510 motionY /= 2;
00511 motionX /= 2;
00512 }
00513
00514
00515 offsetX = motionX - oldMotionX;
00516 offsetY = motionY - oldMotionY;
00517
00518 ENCODE_MOTION_VECTOR(offsetX, offsetY, motionXquot,
00519 motionYquot, motionXrem, motionYrem,
00520 FORW_F);
00521
00522 #ifdef BLEAH
00523 if ( (motionX != 0) || (motionY != 0) ) {
00524 fprintf(stdout, "FRAME (y, x) %d, %d (block %d)\n", y, x, mbAddress);
00525 fprintf(stdout, "motionX = %d, motionY = %d\n", motionX, motionY);
00526 fprintf(stdout, " mxq, mxr = %d, %d myq, myr = %d, %d\n",
00527 motionXquot, motionXrem, motionYquot, motionYrem);
00528 }
00529 #endif
00530
00531 oldMotionX = motionX;
00532 oldMotionY = motionY;
00533
00534 if ( pixelFullSearch ) {
00535 motionY *= 2;
00536 motionX *= 2;
00537 }
00538 calc_blocks:
00539
00540
00541
00542 DoQuant(0x20, dct[y][x], fba[0]);
00543 DoQuant(0x10, dct[y][x+1], fba[1]);
00544 DoQuant(0x08, dct[y+1][x], fba[2]);
00545 DoQuant(0x04, dct[y+1][x+1], fba[3]);
00546 DoQuant(0x02, dctb[y>>1][x>>1], fba[4]);
00547 DoQuant(0x01, dctr[y>>1][x>>1], fba[5]);
00548
00549 if ( decodeRefFrames) {
00550 for ( index = 0; index < 6; index++ ) {
00551 if ( pattern & (1 << (5-index))) {
00552 Mpost_UnQuantZigBlock(fba[index], dec[index], QScale, FALSE);
00553 mpeg_jrevdct((int16 *)dec[index]);
00554 } else {
00555 memset((char *)dec[index], 0, sizeof(Block));
00556 }
00557 }
00558
00559
00560 AddMotionBlock(dec[0], prev->decoded_y, y, x, motionY, motionX);
00561 AddMotionBlock(dec[1], prev->decoded_y, y, x+1, motionY, motionX);
00562 AddMotionBlock(dec[2], prev->decoded_y, y+1, x, motionY, motionX);
00563 AddMotionBlock(dec[3], prev->decoded_y, y+1, x+1, motionY, motionX);
00564 AddMotionBlock(dec[4], prev->decoded_cb, y>>1, x>>1, motionY/2, motionX/2);
00565 AddMotionBlock(dec[5], prev->decoded_cr, y>>1, x>>1, motionY/2, motionX/2);
00566
00567
00568 BlockToData(current->decoded_y, dec[0], y, x);
00569 BlockToData(current->decoded_y, dec[1], y, x+1);
00570 BlockToData(current->decoded_y, dec[2], y+1, x);
00571 BlockToData(current->decoded_y, dec[3], y+1, x+1);
00572 BlockToData(current->decoded_cb, dec[4], y>>1, x>>1);
00573 BlockToData(current->decoded_cr, dec[5], y>>1, x>>1);
00574 }
00575
00576 if ( (motionX == 0) && (motionY == 0) ) {
00577 if ( pattern == 0 ) {
00578
00579
00580
00581
00582
00583
00584 if ( ((y < lastY) || (x < lastX)) &&
00585 (slicePos+1 != blocksPerSlice) &&
00586 (slicePos != 0) ) {
00587 mbAddrInc++;
00588 numSkipped++;
00589 numPBlocks--;
00590 } else {
00591 Mhead_GenMBHeader(bb, 2 , mbAddrInc ,
00592 QScale ,
00593 fCode , 1 ,
00594 motionXrem , motionYrem ,
00595 0 , 0 ,
00596 1 , motionXquot ,
00597 motionYquot , 0 ,
00598 0 , 0 ,
00599 0 , 0 );
00600 mbAddrInc = 1;
00601 }
00602 } else {
00603 DBG_PRINT(("MB Header(%d,%d)\n", x, y));
00604 Mhead_GenMBHeader(bb, 2 , mbAddrInc ,
00605 QScale ,
00606 fCode , 1 ,
00607 0 , 0 ,
00608 0 , 0 ,
00609 0 , 0 ,
00610 0 , 0 ,
00611 0 , 0 ,
00612 pattern , 0 );
00613 mbAddrInc = 1;
00614 }
00615 } else {
00616
00617
00618 Mhead_GenMBHeader(bb, 2 , mbAddrInc ,
00619 QScale ,
00620 fCode , 1 ,
00621 motionXrem , motionYrem ,
00622 0 , 0 ,
00623 1 , motionXquot ,
00624 motionYquot , 0 ,
00625 0 , 0 ,
00626 pattern , 0 );
00627 mbAddrInc = 1;
00628 }
00629
00630
00631 for ( tempX = 0; tempX < 6; tempX++ ) {
00632 if ( GET_ITH_BIT(pattern, 5-tempX) ) {
00633 Mpost_RLEHuffPBlock(fba[tempX], bb);
00634 }
00635 }
00636
00637 numPBits += (bb->cumulativeBits-totalBits);
00638 totalBits = bb->cumulativeBits;
00639 }
00640
00641 if (overflowChange) {
00642
00643 overflowChange = FALSE;
00644 QScale -= overflowValue;
00645 overflowValue = 0;
00646 }
00647
00648 mbAddress++;
00649
00650 if (bitstreamMode == FIXED_RATE) {
00651 incMacroBlockBits( bb->cumulativeBits- rc_blockStart);
00652 rc_blockStart = bb->cumulativeBits;
00653 MB_RateOut(TYPE_PFRAME);
00654 }
00655 }
00656 }
00657
00658 if ( printSNR ) {
00659 BlockComputeSNR(current,snr,psnr);
00660 totalSNR += snr[0];
00661 totalPSNR += psnr[0];
00662 }
00663
00664 #ifdef BLEAHBLEAH
00665 {
00666 FILE *filePtr;
00667
00668 filePtr = fopen("PFRAME.yuv", "wb");
00669
00670 for ( y = 0; y < Fsize_y; y++ )
00671 {
00672 for ( x = 0; x < Fsize_x; x++ )
00673 fprintf(filePtr, "%d ", current->decoded_y[y][x]);
00674 fprintf(filePtr, "\n");
00675 }
00676
00677 fclose(filePtr);
00678 }
00679 #endif
00680
00681 Mhead_GenSliceEnder(bb);
00682
00683 if (bitstreamMode == FIXED_RATE) {
00684 updateRateControl(TYPE_PFRAME);
00685 }
00686
00687
00688 endTime = time_elapsed();
00689 totalTime += (endTime-startTime);
00690
00691 if ( ( ! childProcess) && showBitRatePerFrame ) {
00692
00693 fprintf(bitRateFile, "%5d\t%8d\n", current->id,
00694 30*(bb->cumulativeBits-totalFrameBits));
00695 }
00696
00697 if ( (! childProcess) && frameSummary && (! realQuiet) ) {
00698 fprintf(stdout, "FRAME %d (P): I BLOCKS: %d; P BLOCKS: %d SKIPPED: %d (%ld seconds)\n",
00699 current->id, numIBlocks, numPBlocks, numSkipped, (long)(endTime-startTime)/TIME_RATE);
00700 if ( printSNR ) {
00701 fprintf(stdout, "FRAME %d: SNR: %.1f\t%.1f\t%.1f\tPSNR: %.1f\t%.1f\t%.1f\n",
00702 current->id, snr[0], snr[1], snr[2],
00703 psnr[0], psnr[1], psnr[2]);
00704 }
00705 }
00706
00707 numFrameBits += (bb->cumulativeBits-totalFrameBits);
00708 numPIBlocks += numIBlocks;
00709 numPPBlocks += numPBlocks;
00710 numPSkipped += numSkipped;
00711 numPIBits += numIBits;
00712 numPPBits += numPBits;
00713
00714 if ( (referenceFrame == DECODED_FRAME) && NonLocalRefFrame(current->id) ) {
00715 if ( remoteIO ) {
00716 SendDecodedFrame(current);
00717 } else {
00718 WriteDecodedFrame(current);
00719 }
00720
00721 NotifyDecodeServerReady(current->id);
00722 }
00723 }
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737 void
00738 ResetPFrameStats()
00739 {
00740 numPIBlocks = 0;
00741 numPPBlocks = 0;
00742 numPSkipped = 0;
00743 numPIBits = 0;
00744 numPPBits = 0;
00745 numFrames = 0;
00746 numFrameBits = 0;
00747 totalTime = 0;
00748 }
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762 void
00763 SetPQScale(qP)
00764 int qP;
00765 {
00766 qscaleP = qP;
00767 }
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781 int
00782 GetPQScale()
00783 {
00784 return qscaleP;
00785 }
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799 float
00800 ShowPFrameSummary(inputFrameBits, totalBits, fpointer)
00801 int inputFrameBits;
00802 int32 totalBits;
00803 FILE *fpointer;
00804 {
00805 if ( numFrames == 0 ) {
00806 return 0.0;
00807 }
00808
00809 fprintf(fpointer, "-------------------------\n");
00810 fprintf(fpointer, "*****P FRAME SUMMARY*****\n");
00811 fprintf(fpointer, "-------------------------\n");
00812
00813 if ( numPIBlocks != 0 ) {
00814 fprintf(fpointer, " I Blocks: %5d (%6d bits) (%5d bpb)\n",
00815 numPIBlocks, numPIBits, numPIBits/numPIBlocks);
00816 } else {
00817 fprintf(fpointer, " I Blocks: %5d\n", 0);
00818 }
00819
00820 if ( numPPBlocks != 0 ) {
00821 fprintf(fpointer, " P Blocks: %5d (%6d bits) (%5d bpb)\n",
00822 numPPBlocks, numPPBits, numPPBits/numPPBlocks);
00823 } else {
00824 fprintf(fpointer, " P Blocks: %5d\n", 0);
00825 }
00826
00827 fprintf(fpointer, " Skipped: %5d\n", numPSkipped);
00828
00829 fprintf(fpointer, " Frames: %5d (%6d bits) (%5d bpf) (%2.1f%% of total)\n",
00830 numFrames, numFrameBits, numFrameBits/numFrames,
00831 100.0*(float)numFrameBits/(float)totalBits);
00832 fprintf(fpointer, " Compression: %3d:1 (%9.4f bpp)\n",
00833 numFrames*inputFrameBits/numFrameBits,
00834 24.0*(float)numFrameBits/(float)(numFrames*inputFrameBits));
00835 if ( printSNR )
00836 fprintf(fpointer, " Avg Y SNR/PSNR: %.1f %.1f\n",
00837 totalSNR/(float)numFrames, totalPSNR/(float)numFrames);
00838 if ( totalTime == 0 ) {
00839 fprintf(fpointer, " Seconds: NONE\n");
00840 } else {
00841 fprintf(fpointer, " Seconds: %9ld (%9.4f fps) (%9ld pps) (%9ld mps)\n",
00842 (long)(totalTime/TIME_RATE),
00843 (float)((float)(TIME_RATE*numFrames)/(float)totalTime),
00844 (long)((float)TIME_RATE*(float)numFrames*(float)inputFrameBits/(24.0*(float)totalTime)),
00845 (long)((float)TIME_RATE*(float)numFrames*(float)inputFrameBits/(256.0*24.0*(float)totalTime)));
00846 }
00847
00848 return (float)totalTime/(float)TIME_RATE;
00849 }
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863 float
00864 EstimateSecondsPerPFrame()
00865 {
00866 if ( numFrames == 0 ) {
00867 return 10.0;
00868 } else {
00869 return (float)totalTime/((float)TIME_RATE*(float)numFrames);
00870 }
00871 }
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886 void
00887 ComputeHalfPixelData(frame)
00888 MpegFrame *frame;
00889 {
00890 register int x, y;
00891
00892
00893
00894
00895
00896 if ( frame->halfX == NULL ) {
00897 Frame_AllocHalf(frame);
00898 }
00899
00900
00901 for ( y = 0; y < Fsize_y; y++ ) {
00902 for ( x = 0; x < Fsize_x-1; x++ ) {
00903 frame->halfX[y][x] = (frame->ref_y[y][x]+
00904 frame->ref_y[y][x+1]+1)>>1;
00905 }
00906 }
00907
00908
00909 for ( y = 0; y < Fsize_y-1; y++ ) {
00910 for ( x = 0; x < Fsize_x; x++ ) {
00911 frame->halfY[y][x] = (frame->ref_y[y][x]+
00912 frame->ref_y[y+1][x]+1)>>1;
00913 }
00914 }
00915
00916
00917 for ( y = 0; y < Fsize_y-1; y++ ) {
00918 for ( x = 0; x < Fsize_x-1; x++ ) {
00919 frame->halfBoth[y][x] = (frame->ref_y[y][x]+
00920 frame->ref_y[y][x+1]+
00921 frame->ref_y[y+1][x]+
00922 frame->ref_y[y+1][x+1]+2)>>2;
00923 }
00924 }
00925
00926 frame->halfComputed = TRUE;
00927 }
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954 static boolean
00955 ZeroMotionBetter(currentBlock, prev, by, bx, my, mx)
00956 LumBlock currentBlock;
00957 MpegFrame *prev;
00958 int by;
00959 int bx;
00960 int my;
00961 int mx;
00962 {
00963 int bestDiff;
00964 int CompareMode;
00965
00966
00967 CompareMode = SearchCompareMode;
00968 SearchCompareMode = DEFAULT_SEARCH;
00969 bestDiff = LumMotionError(currentBlock, prev, by, bx, my, mx, 0x7fffffff);
00970 SearchCompareMode = CompareMode;
00971
00972 if ( zeroDiff < 256*3 ) {
00973 if ( 2*bestDiff >= zeroDiff ) {
00974 return TRUE;
00975 }
00976 } else {
00977 if ( 11*bestDiff >= 10*zeroDiff ) {
00978 return TRUE;
00979 }
00980 }
00981
00982 return FALSE;
00983 }
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005 static boolean
01006 DoIntraCode(currentBlock, prev, by, bx, motionY, motionX)
01007 LumBlock currentBlock;
01008 MpegFrame *prev;
01009 int by;
01010 int bx;
01011 int motionY;
01012 int motionX;
01013 {
01014 int x, y;
01015 int32 sum = 0, vard = 0, varc = 0, dif;
01016 int32 currPixel, prevPixel;
01017 LumBlock motionBlock;
01018
01019 ComputeMotionLumBlock(prev, by, bx, motionY, motionX, motionBlock);
01020
01021 for ( y = 0; y < 16; y++ ) {
01022 for ( x = 0; x < 16; x++ ) {
01023 currPixel = currentBlock[y][x];
01024 prevPixel = motionBlock[y][x];
01025
01026 sum += currPixel;
01027 varc += currPixel*currPixel;
01028
01029 dif = currPixel - prevPixel;
01030 vard += dif*dif;
01031 }
01032 }
01033
01034 vard >>= 8;
01035 varc = (varc>>8) - (sum>>8)*(sum>>8);
01036
01037 if ( vard <= 64 ) {
01038 return FALSE;
01039 } else if ( vard < varc ) {
01040 return FALSE;
01041 } else {
01042 return TRUE;
01043 }
01044 }
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062 static boolean
01063 ZeroMotionSufficient(currentBlock, prev, by, bx)
01064 LumBlock currentBlock;
01065 MpegFrame *prev;
01066 int by;
01067 int bx;
01068 {
01069 LumBlock motionBlock;
01070 register int fy, fx;
01071 register int x, y;
01072
01073 fy = by*DCTSIZE;
01074 fx = bx*DCTSIZE;
01075 for ( y = 0; y < 16; y++ ) {
01076 for ( x = 0; x < 16; x++ ) {
01077 motionBlock[y][x] = prev->ref_y[fy+y][fx+x];
01078 }
01079 }
01080
01081 zeroDiff = LumBlockMAD(currentBlock, motionBlock, 0x7fffffff);
01082
01083 return (zeroDiff <= 256);
01084 }
01085
01086
01087 #ifdef UNUSED_PROCEDURES
01088 static void
01089 ComputeAndPrintPframeMAD(currentBlock, prev, by, bx, my, mbx, numBlock)
01090 LumBlock currentBlock;
01091 MpegFrame *prev;
01092 int by;
01093 int bx;
01094 int my;
01095 int mx;
01096 int numBlock;
01097 {
01098 LumBlock lumMotionBlock;
01099 int32 mad;
01100
01101 ComputeMotionLumBlock(prev, by, bx, my, mx, lumMotionBlock);
01102
01103 mad = LumBlockMAD(currentBlock, lumMotionBlock, 0x7fffffff);
01104
01105 if (! realQuiet) {
01106 fprintf(stdout, "%d %d\n", numBlock, mad);
01107 }
01108 }
01109 #endif