📄 iframe.c
字号:
if ( length != 0 ) { if ( ydiff > 0 ) { Bitio_Write(bb, ydiff_abs, length); } else { Bitio_Write(bb, ~ydiff_abs, length); } } (*pred_term) += ydiff;}/*===========================================================================* * * EncodeCDC * * Encode the DC portion of a DCT of a chrominance block * * RETURNS: result appended to bb * * SIDE EFFECTS: updates pred_term * *===========================================================================*/voidEncodeCDC(dc_term, pred_term, bb) int32 dc_term; int32 *pred_term; BitBucket *bb;{ /* see Table B.5b -- MPEG-I doc */ static int codes[9] = { 0x0, 0x1, 0x2, 0x6, 0xe, 0x1e, 0x3e, 0x7e, 0xfe }; static int codeLengths[9] = { 2, 2, 2, 3, 4, 5, 6, 7, 8 }; int cdiff, cdiff_abs; int length; cdiff = (dc_term - (*pred_term)); if (cdiff > 255) {#ifdef BLEAHfprintf(stdout, "TRUNCATED\n"); #endif cdiff = 255; } else if (cdiff < -255) {#ifdef BLEAHfprintf(stdout, "TRUNCATED\n"); #endif cdiff = -255; } cdiff_abs = ABS(cdiff); length = lengths[cdiff_abs]; Bitio_Write(bb, codes[length], codeLengths[length]); if ( length != 0 ) { if ( cdiff > 0 ) { Bitio_Write(bb, cdiff_abs, length); } else { Bitio_Write(bb, ~cdiff_abs, length); } } (*pred_term) += cdiff;}voidBlockComputeSNR(current, snr, psnr) MpegFrame *current; float snr[]; float psnr[];{ register int32 tempInt; register int y, x; int32 varDiff[3]; double ratio[3]; double total[3]; register uint8 **origY=current->orig_y, **origCr=current->orig_cr, **origCb=current->orig_cb; register uint8 **newY=current->decoded_y, **newCr=current->decoded_cr, **newCb=current->decoded_cb; static int32 **SignalY, **NoiseY; static int32 **SignalCb, **NoiseCb; static int32 **SignalCr, **NoiseCr; static short ySize[3], xSize[3]; static boolean needs_init=TRUE; /* Init */ if (needs_init) { int ysz = (Fsize_y>>3) * sizeof(int32 *); int xsz = (Fsize_x>>3); needs_init = FALSE; for (y=0; y<3; y++) { varDiff[y] = ratio[y] = total[y] = 0.0; } ySize[0]=Fsize_y; xSize[0]=Fsize_x; ySize[1]=Fsize_y>>1; xSize[1]=Fsize_x>>1; ySize[2]=Fsize_y>>1; xSize[2]=Fsize_x>>1; SignalY = (int32 **) malloc(ysz); NoiseY = (int32 **) malloc(ysz); SignalCb = (int32 **) malloc(ysz); NoiseCb = (int32 **) malloc(ysz); SignalCr = (int32 **) malloc(ysz); NoiseCr = (int32 **) malloc(ysz); if (SignalY == NULL || NoiseY == NULL || SignalCr == NULL || NoiseCb == NULL || SignalCb == NULL || NoiseCr == NULL) { fprintf(stderr, "Out of memory in BlockComputeSNR\n"); exit(-1); } for (y = 0; y < ySize[0]>>3; y++) { SignalY[y] = (int32 *) calloc(xsz,4); SignalCr[y] = (int32 *) calloc(xsz,4); SignalCb[y] = (int32 *) calloc(xsz,4); NoiseY[y] = (int32 *) calloc(xsz,4); NoiseCr[y] = (int32 *) calloc(xsz,4); NoiseCb[y] = (int32 *) calloc(xsz,4); } } else { for (y = 0; y < ySize[0]>>3; y++) { memset((char *) &NoiseY[y][0], 0, (xSize[0]>>3) * 4); memset((char *) &SignalY[y][0], 0, (xSize[0]>>3) * 4); memset((char *) &NoiseCb[y][0], 0, (xSize[0]>>3) * 4); memset((char *) &NoiseCr[y][0], 0, (xSize[0]>>3) * 4); memset((char *) &SignalCb[y][0], 0, (xSize[0]>>3) * 4); memset((char *) &SignalCr[y][0], 0, (xSize[0]>>3) * 4); } } /* find all the signal and noise */ for (y = 0; y < ySize[0]; y++) { for (x = 0; x < xSize[0]; x++) { tempInt = (origY[y][x] - newY[y][x]); NoiseY[y>>4][x>>4] += tempInt*tempInt; total[0] += (double)abs(tempInt); tempInt = origY[y][x]; SignalY[y>>4][x>>4] += tempInt*tempInt; }} for (y = 0; y < ySize[1]; y++) { for (x = 0; x < xSize[1]; x ++) { tempInt = (origCb[y][x] - newCb[y][x]); NoiseCb[y>>3][x>>3] += tempInt*tempInt; total[1] += (double)abs(tempInt); tempInt = origCb[y][x]; SignalCb[y>>3][x>>3] += tempInt*tempInt; tempInt = (origCr[y][x]-newCr[y][x]); NoiseCr[y>>3][x>>3] += tempInt*tempInt; total[2] += (double)abs(tempInt); tempInt = origCr[y][x]; SignalCr[y>>3][x>>3] += tempInt*tempInt; }} /* Now sum up that noise */ for(y=0; y<Fsize_y>>4; y++){ for(x=0; x<Fsize_x>>4; x++){ varDiff[0] += NoiseY[y][x]; varDiff[1] += NoiseCb[y][x]; varDiff[2] += NoiseCr[y][x]; if (printMSE) printf("%4d ",(int)(NoiseY[y][x]/256.0)); } if (printMSE) puts(""); } /* Now look at those ratios! */ for(y=0; y<Fsize_y>>4; y++){ for(x=0; x<Fsize_x>>4; x++){ ratio[0] += (double)SignalY[y][x]/(double)varDiff[0]; ratio[1] += (double)SignalCb[y][x]/(double)varDiff[1]; ratio[2] += (double)SignalCr[y][x]/(double)varDiff[2]; }} for (x=0; x<3; x++) { snr[x] = 10.0*log10(ratio[x]); psnr[x] = 20.0*log10(255.0/sqrt((double)varDiff[x]/(double)(ySize[x]*xSize[x]))); if (! realQuiet) { fprintf(stdout, "Mean error[%1d]: %f\n", x, total[x]/(double)(xSize[x]*ySize[x])); } }}voidWriteDecodedFrame(frame) MpegFrame *frame;{ FILE *fpointer; char fileName[256]; int width, height; register int y; /* need to save decoded frame to disk because it might be accessed by another process */ width = Fsize_x; height = Fsize_y; sprintf(fileName, "%s.decoded.%d", outputFileName, frame->id); if (!realQuiet) { fprintf(stdout, "Outputting to %s\n", fileName); fflush(stdout); } fpointer = fopen(fileName, "wb"); for ( y = 0; y < height; y++ ) { fwrite(frame->decoded_y[y], 1, width, fpointer); } for (y = 0; y < (height >> 1); y++) { /* U */ fwrite(frame->decoded_cb[y], 1, width >> 1, fpointer); } for (y = 0; y < (height >> 1); y++) { /* V */ fwrite(frame->decoded_cr[y], 1, width >> 1, fpointer); } fflush(fpointer); fclose(fpointer);}voidPrintItoIBitRate(numBits, frameNum) int numBits; int frameNum;{ if ( showBitRatePerFrame ) { /* ASSUMES 30 FRAMES PER SECOND */ if (! realQuiet) { fprintf(stdout, "I-to-I (frames %5d to %5d) bitrate: %8d\n", lastIFrame, frameNum-1, ((numBits-lastNumBits)*30)/ (frameNum-lastIFrame)); } fprintf(bitRateFile, "I-to-I (frames %5d to %5d) bitrate: %8d\n", lastIFrame, frameNum-1, ((numBits-lastNumBits)*30)/ (frameNum-lastIFrame)); }}/*===========================================================================* * * AllocDctBlocks * * allocate memory for dct blocks * * RETURNS: nothing * * SIDE EFFECTS: creates dct, dctr, dctb * *===========================================================================*/voidAllocDctBlocks(){ int dctx, dcty; int i; dctx = Fsize_x / DCTSIZE; dcty = Fsize_y / DCTSIZE; dct = (Block **) malloc(sizeof(Block *) * dcty); ERRCHK(dct, "malloc"); for (i = 0; i < dcty; i++) { dct[i] = (Block *) malloc(sizeof(Block) * dctx); ERRCHK(dct[i], "malloc"); } dct_data = (dct_data_type **) malloc(sizeof(dct_data_type *) * dcty); ERRCHK(dct_data, "malloc"); for (i = 0; i < dcty; i++) { dct_data[i] = (dct_data_type *) malloc(sizeof(dct_data_type) * dctx); ERRCHK(dct[i], "malloc"); } dctr = (Block **) malloc(sizeof(Block *) * (dcty >> 1)); dctb = (Block **) malloc(sizeof(Block *) * (dcty >> 1)); ERRCHK(dctr, "malloc"); ERRCHK(dctb, "malloc"); for (i = 0; i < (dcty >> 1); i++) { dctr[i] = (Block *) malloc(sizeof(Block) * (dctx >> 1)); dctb[i] = (Block *) malloc(sizeof(Block) * (dctx >> 1)); ERRCHK(dctr[i], "malloc"); ERRCHK(dctb[i], "malloc"); }}/*======================================================================* * * time_elapsed * * Handle different time systems on different machines * * RETURNS number of seconds process time used * *======================================================================*/int32 time_elapsed(){#ifdef CLOCKS_PER_SEC /* ANSI C */ TIME_RATE = CLOCKS_PER_SEC; return (int32) clock();#else struct tms timeBuffer; TIME_RATE = 60; times(&timeBuffer); return timeBuffer.tms_utime + timeBuffer.tms_stime;#endif}voidCalcDistortion(current, y, x)MpegFrame *current;int y,x;{ int qscale, distort=0; Block decblk; FlatBlock fblk; int datarate = 0; for (qscale = 1; qscale < 32; qscale ++) { distort = 0; datarate = 0; Mpost_QuantZigBlock(dct[y][x], fblk, qscale, TRUE); Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE); if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk); mpeg_jrevdct((int16 *)decblk); distort += mse(current->y_blocks[y][x], decblk); Mpost_QuantZigBlock(dct[y][x+1], fblk, qscale, TRUE); Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE); if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk); mpeg_jrevdct((int16 *)decblk); distort += mse(current->y_blocks[y][x+1], decblk); Mpost_QuantZigBlock(dct[y+1][x], fblk, qscale, TRUE); Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE); if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk); mpeg_jrevdct((int16 *)decblk); distort += mse(current->y_blocks[y+1][x], decblk); Mpost_QuantZigBlock(dct[y+1][x+1], fblk, qscale, TRUE); Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE); if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk); mpeg_jrevdct((int16 *)decblk); distort += mse(current->y_blocks[y+1][x+1], decblk); Mpost_QuantZigBlock(dctb[y >> 1][x >> 1], fblk, qscale, TRUE); Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE); if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk); mpeg_jrevdct((int16 *)decblk); distort += mse(current->cb_blocks[y>>1][x>>1], decblk); Mpost_QuantZigBlock(dctr[y >> 1][x >> 1], fblk, qscale, TRUE); Mpost_UnQuantZigBlock(fblk, decblk, qscale, TRUE); if (collect_distortion_detailed) datarate += CalcRLEHuffLength(fblk); mpeg_jrevdct((int16 *)decblk); distort += mse(current->cr_blocks[y >> 1][x >> 1], decblk); if (!collect_distortion_detailed) { fprintf(distortion_fp, "\t%d\n", distort); } else if (collect_distortion_detailed == 1) { fprintf(distortion_fp, "\t%d\t%d\n", distort, datarate); } else { fprintf(fp_table_rate[qscale-1], "%d\n", datarate); fprintf(fp_table_dist[qscale-1], "%d\n", distort); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -