⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 LDPC码的实现,包括编码器和解码器,使用了DCT.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//main.cpp

//------------------------------------------------------------
// David Chen*, David Varodayan, Markus Flierl, Bernd Girod
//
// Image, Video, and Multimedia Systems Group
// Information Systems Laboratory
// Stanford University
//
// *Contact: dmchen@stanford.edu
//------------------------------------------------------------

#include "common.h"
#include "encoderQuad.h"
#include "decoderQuad.h"

#include <ChenImage.h>
#include <ChenVideo.h>
#include <ChenImageMatlab.h>

int main(int argc, char** argv)
{
	//------------------------------------------------------------
	// Control parameters for coding process
	//------------------------------------------------------------
	printTitle("Control parameters for coding process");

	char pTextBuffer[1024];
	time_t nStartTime, nEndTime;
	CodingOption codingOption;
	codingOption.bSaveDecodedFrame = true;
	codingOption.bSaveMotionField = true;
	codingOption.nCodingStructure = CODING_IPPPPPPP;
	codingOption.nInterpolateMethod = INTERP_NN;
	codingOption.nReconstructMethod = RECON_CENTROID;
	codingOption.nQuantizationArray = QUANT_JPEG;
	codingOption.nQuantizationLevel = 0;
	codingOption.nStartFrame = 0;
	codingOption.nEndFrame = 7;
	codingOption.bTestMotionLearning = true;
	codingOption.bTestMotionOracle = true;
	codingOption.bTestZeroMotion = true;
	codingOption.bTestIntra = true;


	// Choose experiment folder
	sprintf(codingOption.pExperimentFolderName, "C:\\Users\\HP_Administrator\\Desktop\\TestForeman");
	sprintf(pTextBuffer, "mkdir %s", codingOption.pExperimentFolderName);
	SAFE_FUNC( ChenImageMatlab_systemCommand(pTextBuffer) );

	// Choose video file
	sprintf(codingOption.pVideoFileName, "C:\\Users\\HP_Administrator\\Desktop\\TestForeman\\foreman-qcif-400.yuv");
	FILE* pFile;
	if ((pFile = fopen(codingOption.pVideoFileName, "r")) == NULL) {
		fprintf(stderr, "Cannot open video file.");
		return BAD_RETURN;
	}
	else {
		fclose(pFile);
	}
	
	// Choose log file
	sprintf(codingOption.pLogFileName, "%s\\%s.log", codingOption.pExperimentFolderName, timeStamp());
	if ((codingOption.pLogFile = fopen(codingOption.pLogFileName, "w")) == NULL){
		fprintf(stderr, "Cannot open log file.");
		return BAD_RETURN;
	}
	sprintf(codingOption.pLogFileRDName, "%s\\%s-RD.log", codingOption.pExperimentFolderName, timeStamp());
	if ((codingOption.pLogFileRD = fopen(codingOption.pLogFileRDName, "w")) == NULL) {
		fprintf(stderr, "Cannot open log file.");
		return BAD_RETURN;
	}
	sprintf(pTextBuffer, "log file = %s \n", codingOption.pLogFileName); SCREEN_AND_LOG(pTextBuffer, codingOption.pLogFile);
	sprintf(pTextBuffer, "log file (RD) = %s \n", codingOption.pLogFileRDName); SCREEN_AND_LOG(pTextBuffer, codingOption.pLogFile);

	// Choose ladder file
	sprintf(codingOption.pLadderFileName, "C:\\Users\\HP_Administrator\\Desktop\\TestForeman\\50688_regDeg3_1on132.lad");
	sprintf(pTextBuffer, "ladder file = %s \n", codingOption.pLadderFileName); SCREEN_AND_LOG(pTextBuffer, codingOption.pLogFile);
	if ((pFile = fopen(codingOption.pLadderFileName, "r")) == NULL) {
		fprintf(stderr, "Cannot open ladder file.");
		return BAD_RETURN;
	}
	else {
		fclose(pFile);
	}

	// Print other parameters
	sprintf(pTextBuffer, "coding structure = %s \n", CodingStructureName[codingOption.nCodingStructure]); SCREEN_AND_LOG(pTextBuffer, codingOption.pLogFile);
	sprintf(pTextBuffer, "reconstruct method = %s \n", ReconstructName[codingOption.nReconstructMethod]); SCREEN_AND_LOG(pTextBuffer, codingOption.pLogFile);
	sprintf(pTextBuffer, "quantization array = %s \n", QuantizationArrayName[codingOption.nQuantizationArray]); SCREEN_AND_LOG(pTextBuffer, codingOption.pLogFile);
	sprintf(pTextBuffer, "motion field interpolation = %s \n", InterpolationName[codingOption.nInterpolateMethod == INTERP_NN]); SCREEN_AND_LOG(pTextBuffer, codingOption.pLogFile);

	//------------------------------------------------------------
	// Load video sequence
	//------------------------------------------------------------
	sprintf(pTextBuffer, "Load video sequence"); SCREEN_AND_LOG_TITLE(pTextBuffer, codingOption.pLogFile);

	sprintf(pTextBuffer, "video = %s \n", codingOption.pVideoFileName); SCREEN_AND_LOG(pTextBuffer, codingOption.pLogFile);
	short **pVideoY, **pVideoU, **pVideoV;
	int nFrames = codingOption.nEndFrame + 1;
	SAFE_FUNC( ChenVideo_loadYUVVideo(codingOption.pVideoFileName, QCIF, 1, codingOption.nEndFrame+1, pVideoY, pVideoU, pVideoV) );

	short **pVideoLearningY = new short*[nFrames]; // Motion-learning decoded
	short **pVideoLearningU = new short*[nFrames];
	short **pVideoLearningV = new short*[nFrames];
	short **pVideoOracleY = new short*[nFrames]; // Motion-oracle decoded
	short **pVideoOracleU = new short*[nFrames];
	short **pVideoOracleV = new short*[nFrames];
	short **pVideoNoCompY = new short*[nFrames]; // No-motion decoded
	short **pVideoNoCompU = new short*[nFrames];
	short **pVideoNoCompV = new short*[nFrames];
	for (int nFrame = 0; nFrame < nFrames; nFrame++) {
		pVideoLearningY[nFrame] = NULL;
		pVideoLearningU[nFrame] = NULL;
		pVideoLearningV[nFrame] = NULL;
		pVideoOracleY[nFrame] = NULL;
		pVideoOracleU[nFrame] = NULL;
		pVideoOracleV[nFrame] = NULL;
		pVideoNoCompY[nFrame] = NULL;
		pVideoNoCompU[nFrame] = NULL;
		pVideoNoCompV[nFrame] = NULL;
	}

	//------------------------------------------------------------
	// Choose quantization parameters
	//------------------------------------------------------------
	sprintf(pTextBuffer, "Choose quantization parameters"); SCREEN_AND_LOG_TITLE(pTextBuffer, codingOption.pLogFile);

	double pQFactor[NUM_Q_FACTORS] = { 0.5, 1.0, 2.0, 4.0 };
	int pQIndexJPEG[NUM_Q_FACTORS] = { 75, 50, 25, 13 };
	sprintf(pTextBuffer, "Q-factor = %.2f, Q-index = %d \n", pQFactor[codingOption.nQuantizationLevel], pQIndexJPEG[codingOption.nQuantizationLevel]); SCREEN_AND_LOG(pTextBuffer, codingOption.pLogFile);

	// Find scaled Q-factor array
	short* pScaledQArray = new short [NUM_COEFFS];
	if (codingOption.nQuantizationArray == QUANT_JPEG) {
		for (int nCoeff = 0; nCoeff < NUM_COEFFS; nCoeff++) {
			pScaledQArray[nCoeff] = (short) ceil( QArrayJPEG[nCoeff] * pQFactor[codingOption.nQuantizationLevel] );
		}
	}
	else { // QUANT_FLAT
		for (int nCoeff = 0; nCoeff < NUM_COEFFS; nCoeff++) {
			pScaledQArray[nCoeff] = (short) ceil( QArrayFlat[nCoeff] * pQFactor[codingOption.nQuantizationLevel] );
		}
	}

	//------------------------------------------------------------
	// Generate intra-coded frames
	//------------------------------------------------------------
	sprintf(pTextBuffer, "Generate intra-coded frames"); SCREEN_AND_LOG_TITLE(pTextBuffer, codingOption.pLogFile);

	short **pVideoIntraY = new short*[nFrames];
	short **pVideoIntraU = new short*[nFrames];
	short **pVideoIntraV = new short*[nFrames];
	for (int nFrame = 0; nFrame < nFrames; nFrame++) {
		pVideoIntraY[nFrame] = NULL;
		pVideoIntraU[nFrame] = NULL;
		pVideoIntraV[nFrame] = NULL;
	}
	float* pIntraRateY = new float[nFrames];
	float* pIntraRateU = new float[nFrames];
	float* pIntraRateV = new float[nFrames];
	float* pIntraPSNRY = new float[nFrames];
	float* pIntraPSNRU = new float[nFrames];
	float* pIntraPSNRV = new float[nFrames];

	int nQuality = pQIndexJPEG[codingOption.nQuantizationLevel];
	for (int nFrame = codingOption.nStartFrame; nFrame <= codingOption.nEndFrame; nFrame++) {
		sprintf(pTextBuffer, "mkdir %s\\jpeg", codingOption.pExperimentFolderName);
		SAFE_FUNC( ChenImageMatlab_systemCommand(pTextBuffer) );

		// Encode and decode Y component using JPEG
		sprintf(codingOption.pLastImageName, "%s\\jpeg\\quality-%d-frame-%d-Y.jpg", codingOption.pExperimentFolderName, nQuality, nFrame);
		SAFE_FUNC( ChenImageMatlab_saveImageJPEG(codingOption.pLastImageName, WIDTH, HEIGHT, 1, pVideoY[nFrame], nQuality) );
		int nBitsY;
		SAFE_FUNC( ChenImageMatlab_imageFileSize(codingOption.pLastImageName, &nBitsY) );
		int nWidth, nHeight, nChannels;
		SAFE_FUNC( ChenImageMatlab_loadImageFromFile(codingOption.pLastImageName, nWidth, nHeight, nChannels, pVideoIntraY[nFrame]) );
		SAFE_FUNC( ChenImage_imagePSNR(WIDTH, HEIGHT, pVideoIntraY[nFrame], pVideoY[nFrame], pIntraPSNRY+nFrame) );
		pIntraRateY[nFrame] = (float)nBitsY / (float)PIXELS;

		// Encode and decode U component using JPEG
		sprintf(codingOption.pLastImageName, "%s\\jpeg\\quality-%d-frame-%d-U.jpg", codingOption.pExperimentFolderName, nQuality, nFrame);
		SAFE_FUNC( ChenImageMatlab_saveImageJPEG(codingOption.pLastImageName, WIDTH/2, HEIGHT/2, 1, pVideoU[nFrame], nQuality) );
		int nBitsU;
		SAFE_FUNC( ChenImageMatlab_imageFileSize(codingOption.pLastImageName, &nBitsU) );
		SAFE_FUNC( ChenImageMatlab_loadImageFromFile(codingOption.pLastImageName, nWidth, nHeight, nChannels, pVideoIntraU[nFrame]) );
		SAFE_FUNC( ChenImage_imagePSNR(WIDTH/2, HEIGHT/2, pVideoIntraU[nFrame], pVideoU[nFrame], pIntraPSNRU+nFrame) );
		pIntraRateU[nFrame] = (float)nBitsU / (float)PIXELS;

		// Encode and decode V component using JPEG
		sprintf(codingOption.pLastImageName, "%s\\jpeg\\quality-%d-frame-%d-V.jpg", codingOption.pExperimentFolderName, nQuality, nFrame);
		SAFE_FUNC( ChenImageMatlab_saveImageJPEG(codingOption.pLastImageName, WIDTH/2, HEIGHT/2, 1, pVideoV[nFrame], nQuality) );
		int nBitsV;
		SAFE_FUNC( ChenImageMatlab_imageFileSize(codingOption.pLastImageName, &nBitsV) );
		SAFE_FUNC( ChenImageMatlab_loadImageFromFile(codingOption.pLastImageName, nWidth, nHeight, nChannels, pVideoIntraV[nFrame]) );
		SAFE_FUNC( ChenImage_imagePSNR(WIDTH/2, HEIGHT/2, pVideoIntraV[nFrame], pVideoV[nFrame], pIntraPSNRV+nFrame) );
		pIntraRateV[nFrame] = (float)nBitsV / (float)PIXELS;

	} // end nFrame

	sprintf(pTextBuffer, "intra-coded frames stored: %s\\jpeg \n", codingOption.pExperimentFolderName); SCREEN_AND_LOG(pTextBuffer, codingOption.pLogFile);

	//------------------------------------------------------------
	// Process inter-coded frames
	//------------------------------------------------------------
	time(&nStartTime);

	for (codingOption.nCurrFrame = codingOption.nStartFrame; codingOption.nCurrFrame <= codingOption.nEndFrame; codingOption.nCurrFrame++) 
	{
		//------------------------------------------------------------
		// Choose coding structure
		//------------------------------------------------------------
		sprintf(pTextBuffer, "Choose coding structure"); SCREEN_AND_LOG_TITLE(pTextBuffer, codingOption.pLogFile);

		// Start RD file
		fprintf(codingOption.pLogFileRD, "%d\t", codingOption.nCurrFrame); fflush(codingOption.pLogFileRD);

		int nWZFrame, nSIFrame;
		int nRepeatPrints = (codingOption.bTestMotionLearning ? 1 : 0) + (codingOption.bTestMotionOracle ? 1 : 0) + (codingOption.bTestZeroMotion ? 1 : 0) + (codingOption.bTestIntra ? 1 : 0);
		
		if (codingOption.nCodingStructure == CODING_IPPPPPPP) {

			// Intra-code and intra-decode every 8th frame as key frame
			if ((codingOption.nCurrFrame % 8) == 0) {
				sprintf(pTextBuffer, "Intra-coded frame = %d \n", codingOption.nCurrFrame); SCREEN_AND_LOG(pTextBuffer, codingOption.pLogFile);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -