📄 wvtencode.cpp
字号:
/*************************************************************************
This software module was originally developed by
Ming-Chieh Lee (mingcl@microsoft.com), Microsoft Corporation
Wei-ge Chen (wchen@microsoft.com), Microsoft Corporation
Bruce Lin (blin@microsoft.com), Microsoft Corporation
Chuang Gu (chuanggu@microsoft.com), Microsoft Corporation
Simon Winder (swinder@microsoft.com), Microsoft Corporation
in the course of development of the MPEG-4 Video (ISO/IEC 14496-2).
This software module is an implementation of a part of one or more MPEG-4 Video tools
as specified by the MPEG-4 Video.
ISO/IEC gives users of the MPEG-4 Video free license to this software module or modifications
thereof for use in hardware or software products claiming conformance to the MPEG-4 Video.
Those intending to use this software module in hardware or software products are advised that its use may infringe existing patents.
The original developer of this software module and his/her company,
the subsequent editors and their companies,
and ISO/IEC have no liability for use of this software module or modifications thereof in an implementation.
Copyright is not released for non MPEG-4 Video conforming products.
Microsoft retains full right to use the code for his/her own purpose,
assign or donate the code to a third party and to inhibit third parties from using the code for non <MPEG standard> conforming products.
This copyright notice must be included in all copies or derivative works.
Copyright (c) 1996, 1997.
Module Name:
wvtencode.cpp
Abstract:
stand-alone wavelet encoder
Revision History:
*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <fstream.h>
#include "typeapi.h"
#include "wavelet.h"
#ifdef __MFC_
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
#define new DEBUG_NEW
#endif // __MFC_
void error(char *m)
{
fprintf(stderr,"\nError: %s.\n",m);
exit(1);
}
void paramError(UInt *pnLine)
{
fprintf(stderr, "\nError: wrong parameter file format on line %d\n", *pnLine);
exit (1);
}
void rangeError(UInt *pnLine)
{
fprintf(stderr, "\nError: parameter out of range on line %d\n", *pnLine);
exit (1);
}
void nextValidParam(FILE *fp, UInt *pnLine)
{
Int c;
while((c=fgetc(fp))!=EOF)
{
if(c=='\n')
(*pnLine)++;
else if(c=='%')
{
while((c=fgetc(fp))!=EOF && c!='\n');
ungetc(c,fp);
}
else if(!isspace(c))
{
ungetc(c,fp);
break;
}
}
}
int main(Int argc, Char* argv[])
{
static Char *rgchQT[3] = {"single quant","multiple quant","bi-level quant"};
UInt nLine=1;
UInt *pnLine=&nLine;
FILE *pfsPara,*pfsSave;
Char rgchParaSourceFile[100];
Char rgchParaBitFile[100];
Char rgchParaReconFile[100];
Int iParaWidth,iParaHeight,iParaFrameStart,iParaFrameEnd,iParaFrameSample;
Int iParaDecompLevels,iParaSpatialLevels,iParaSNRLevels,iParaQuantType;
Int iParaLengthMarker,iParaSpatialMarker,iParaDCQuantLuma,iParaDCQuantChroma;
Int iParaReconSpatialLayer,iParaReconSNRLayer;
QuantType qtParaQuantType;
Int iFrame,iVal;
/* if(argc<2)
error("missing parameter file argument");*/
// open parameter file
argv[1]="d:\\film\\vm\\app\\wavelet\\wvtencode\\params.txt";
if((pfsPara=fopen(argv[1],"r"))==NULL)
error("can't open parameter file for reading");
// start reading param file
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%s",rgchParaSourceFile)!=1)
paramError(pnLine);
#if(DEBUG_PARAMS)
printf("Source file \t\t= %s\n",rgchParaSourceFile);
#endif
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaWidth)!=1)
paramError(pnLine);
if(iParaWidth<4 || (iParaWidth&3)!=0)
rangeError(pnLine);
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaHeight)!=1)
paramError(pnLine);
if(iParaHeight<4 || (iParaHeight&3)!=0)
rangeError(pnLine);
#if(DEBUG_PARAMS)
printf("Width \t\t\t= %d\nHeight \t\t\t= %d\n",iParaWidth,iParaHeight);
#endif
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaFrameStart)!=1)
paramError(pnLine);
if(iParaFrameStart<0)
rangeError(pnLine);
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaFrameEnd)!=1)
paramError(pnLine);
if(iParaFrameEnd<iParaFrameStart)
rangeError(pnLine);
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaFrameSample)!=1)
paramError(pnLine);
if(iParaFrameSample<1)
rangeError(pnLine);
#if(DEBUG_PARAMS)
printf("Frame start \t\t= %d\nFrame end \t\t= %d\nFrame sample \t\t= %d\n",
iParaFrameStart,iParaFrameEnd,iParaFrameSample);
#endif
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%s",rgchParaBitFile)!=1)
paramError(pnLine);
#if(DEBUG_PARAMS)
printf("Bitstream file \t\t= %s\n",rgchParaBitFile);
#endif
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%s",rgchParaReconFile)!=1)
paramError(pnLine);
#if(DEBUG_PARAMS)
printf("Recon file \t\t= %s\n",rgchParaReconFile);
#endif
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaDecompLevels)!=1)
paramError(pnLine);
if(iParaDecompLevels<1)
rangeError(pnLine);
#if(DEBUG_PARAMS)
printf("Decomp levels \t\t= %d\n",iParaDecompLevels);
#endif
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaSpatialLevels)!=1)
paramError(pnLine);
if(iParaSpatialLevels<1 || iParaSpatialLevels>iParaDecompLevels)
rangeError(pnLine);
#if(DEBUG_PARAMS)
printf("Spatial scale levels \t= %d\n",iParaSpatialLevels);
#endif
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaSNRLevels)!=1)
paramError(pnLine);
if(iParaSNRLevels<1)
rangeError(pnLine);
#if(DEBUG_PARAMS)
printf("Snr scale levels \t= %d\n",iParaSNRLevels);
#endif
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaQuantType)!=1)
paramError(pnLine);
if(iParaQuantType<0 || iParaQuantType>2)
rangeError(pnLine);
#if(DEBUG_PARAMS)
printf("Quantisation type \t= %d (%s)\n",iParaQuantType,rgchQT[iParaQuantType]);
#endif
switch(iParaQuantType)
{
case 0:
qtParaQuantType=QT_SINGLE;
break;
case 1:
qtParaQuantType=QT_MULTIPLE;
break;
case 2:
default:
qtParaQuantType=QT_BILEVEL;
break;
}
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaLengthMarker)!=1)
paramError(pnLine);
if(iParaLengthMarker<0 || iParaLengthMarker>1)
rangeError(pnLine);
#if(DEBUG_PARAMS)
printf("Snr marker enable \t= %d\n",iParaLengthMarker);
#endif
Bool bParaLengthMarker = (iParaLengthMarker==1)?TRUE:FALSE;
if(qtParaQuantType==QT_MULTIPLE)
bParaLengthMarker = TRUE; // length markers are needed at the decoder
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaSpatialMarker)!=1)
paramError(pnLine);
if(iParaSpatialMarker<0 || iParaSpatialMarker>1)
rangeError(pnLine);
#if(DEBUG_PARAMS)
printf("Spatial marker enable \t= %d\n",iParaSpatialMarker);
#endif
Bool bParaSpatialMarker = (iParaSpatialMarker==1)?TRUE:FALSE;
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaDCQuantLuma)!=1)
paramError(pnLine);
if(iParaDCQuantLuma<1)
rangeError(pnLine);
#if(DEBUG_PARAMS)
printf("DC quant luma \t\t= %d\n",iParaDCQuantLuma);
#endif
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaDCQuantChroma)!=1)
paramError(pnLine);
if(iParaDCQuantChroma<1)
rangeError(pnLine);
#if(DEBUG_PARAMS)
printf("DC quant chroma \t= %d\n",iParaDCQuantChroma);
#endif
CWvtCoderChannel coderChanLuma;
CWvtCoderChannel coderChanChroma;
WvtCoderParams coderParams;
coderParams.iObjectID=0;
coderParams.bLengthMarker=bParaLengthMarker;
coderParams.bSpatialMarker=bParaSpatialMarker;
coderParams.qtQuantType=qtParaQuantType;
coderParams.pCoderChanLuma=&coderChanLuma;
coderParams.pCoderChanChroma=&coderChanChroma;
coderChanLuma.createSpatialSet(iParaSpatialLevels);
coderChanLuma.setDCQuant(iParaDCQuantLuma);
coderChanChroma.createSpatialSet(iParaSpatialLevels);
coderChanChroma.setDCQuant(iParaDCQuantChroma);
#if(DEBUG_PARAMS)
printf("AC quant luma\n");
#endif
Int i,j;
for(i=0;i<iParaSpatialLevels;i++)
{
#if(DEBUG_PARAMS)
printf(" Spatial level %d: ",i);
#endif
coderChanLuma.createSNRSet(i,iParaSNRLevels);
for(j=0;j<iParaSNRLevels;j++)
{
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iVal)!=1)
paramError(pnLine);
if(iVal<1)
rangeError(pnLine);
coderChanLuma.setSNRParams(i,j,iVal,0);
#if(DEBUG_PARAMS)
printf("\t%d",iVal);
#endif
}
#if(DEBUG_PARAMS)
printf("\n");
#endif
}
#if(DEBUG_PARAMS)
printf("AC quant chroma\n");
#endif
for(i=0;i<iParaSpatialLevels;i++)
{
#if(DEBUG_PARAMS)
printf(" Spatial level %d: ",i);
#endif
coderChanChroma.createSNRSet(i,iParaSNRLevels);
for(j=0;j<iParaSNRLevels;j++)
{
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iVal)!=1)
paramError(pnLine);
if(iVal<1)
rangeError(pnLine);
coderChanChroma.setSNRParams(i,j,iVal,0);
#if(DEBUG_PARAMS)
printf("\t%d",iVal);
#endif
}
#if(DEBUG_PARAMS)
printf("\n");
#endif
}
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaReconSpatialLayer)!=1)
paramError(pnLine);
nextValidParam(pfsPara,pnLine);
if(fscanf(pfsPara,"%d",&iParaReconSNRLayer)!=1)
paramError(pnLine);
coderChanLuma.setSave(iParaReconSpatialLayer,iParaReconSNRLayer);
coderChanChroma.setSave(iParaReconSpatialLayer,iParaReconSNRLayer);
fclose(pfsPara);
// end of reading of param file
// check source file can be read
FILE *pfsTest;
if((pfsTest=fopen(rgchParaSourceFile,"rb"))==NULL)
error("can't open yuv image source file");
fclose(pfsTest);
// open yuv output file
if((pfsSave=fopen(rgchParaReconFile,"wb"))==NULL)
error("can't open yuv coded-image file for writing");
// create output bitstream
ofstream bitfile(rgchParaBitFile,ios::out|ios::binary);
//COutBitStream bs(bitfile,0,&cout);
Char *pchOutBuffer = new Char [BUFFER_SIZE];
COutBitStream bs(pchOutBuffer);
CRct rct(0,0,iParaWidth,iParaHeight);
CVideoObjectPlane rvop(rct);
CWvtTextureCoder *pWvtTextureCoder =
new CWvtTextureCoder(rct,iParaDecompLevels,DAUBE_9_3);
// encode
for(iFrame=iParaFrameStart;iFrame<=iParaFrameEnd;iFrame+=iParaFrameSample)
{
printf("\nFrame time = %d\n",iFrame);
// load the vop from file
CVideoObjectPlane vop(rgchParaSourceFile,iFrame,rct,FOUR_TWO_ZERO,0);
// encode the vop
pWvtTextureCoder->encode(&coderParams,bs,vop,pfsSave);
bs.flush();
bitfile.write(bs.str(),bs.pcount());
bs.reset();
}
pWvtTextureCoder->dumpStats();
// clean up
delete pWvtTextureCoder;
delete pchOutBuffer;
bitfile.close();
fclose(pfsSave);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -