视频编解码器View.cpp
上传用户:szklck
上传日期:2007-01-22
资源大小:925k
文件大小:22k
源码类别:

图形图像处理

开发平台:

Visual C++

  1. // 视频编解码器View.cpp : implementation of the CMyView class
  2. //
  3. #include "stdafx.h"
  4. #include <math.h>                   
  5. #include "视频编解码器.h"
  6. #include "视频编解码器Doc.h"
  7. #include "视频编解码器View.h"
  8. #include "OpenFiles.h"
  9. #include "Global.h"
  10. #include "ProgressBar.h"
  11. #include "Display.h"
  12. #include "PropertyDlg.h"
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. int pels,lines,mv_outside_frame,long_vectors,cpels;
  19. CFile streamfile,psnrfile;
  20. static void SeekPsnr(PictImage *curr,PictImage *recon,int width,int height,double psnr[3]);
  21. #define OFFLINE_RATE_CONTROL//jwp
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CMyView
  24. IMPLEMENT_DYNCREATE(CMyView, CView)
  25. BEGIN_MESSAGE_MAP(CMyView, CView)
  26. //{{AFX_MSG_MAP(CMyView)
  27. ON_COMMAND(ID_Encode, OnEncode)
  28. ON_COMMAND(ID_Decode, OnDecode)
  29. ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  30. //}}AFX_MSG_MAP
  31. // Standard printing commands
  32. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  33. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  34. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  35. ON_COMMAND(ID_Encode, OnEncode)
  36. ON_COMMAND(ID_Decode, OnDecode)
  37. //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39. // CMyView construction/destruction
  40. CMyView::CMyView()
  41. {
  42. m_font.CreateFont(20,0,0,0,FW_THIN,FALSE,FALSE,FALSE,GB2312_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FIXED_PITCH|FF_MODERN,"宋体");
  43. bFlag=0;
  44. }
  45. CMyView::~CMyView()
  46. {
  47. m_font.DeleteObject();
  48. }
  49. BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
  50. {
  51. // TODO: Modify the Window class or styles here by modifying
  52. //  the CREATESTRUCT cs
  53. return CView::PreCreateWindow(cs);
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CMyView drawing
  57. void CMyView::OnDraw(CDC* pDC)
  58. {
  59. CBitmap bitmap;
  60. CDC dcMemory;
  61. bitmap.LoadBitmap(IDB_car);
  62. dcMemory.CreateCompatibleDC(pDC);
  63. dcMemory.SelectObject(&bitmap);
  64. pDC->SetTextColor(0x64e896);
  65. if(bFlag==1)//编码
  66. {
  67.    CFont *pOldFont;
  68.    pOldFont=pDC->SelectObject(&m_font);
  69.    pDC->SetBkMode(TRANSPARENT);
  70. pDC->StretchBlt(0,0,1024*3/4,768*3/4,&dcMemory,0,0,1024,768,SRCCOPY);
  71.         CString kk;
  72. kk.Format("正在编码!共%d帧",MaxFrame);
  73. pDC->TextOut(60,60,kk);
  74. kk.Format("每%d帧进行一次帧内编码,进度情况请看状态栏!",Pbetween);
  75.     pDC->TextOut(60,100,kk);
  76. }
  77.     else if(bFlag==2)//解码
  78. {
  79. CBrush Brush(RGB(100,200,150));//190,190,190
  80. CBrush* oldBrush=pDC->SelectObject(&Brush);
  81.     CPen pen(0,2,RGB(100,200,150));
  82. CPen *oldPen=pDC->SelectObject(&pen);
  83. pDC->Rectangle(40,40,440,370);//画矩形
  84. pDC->MoveTo(40,370); // (wide,height)
  85. pDC->LineTo(620,370);
  86. pDC->MoveTo(620,40);        
  87. pDC->LineTo(620,370);
  88. pDC->MoveTo(40,40);        
  89. pDC->LineTo(620,40);
  90. pDC->SelectObject(oldPen);
  91. pDC->SelectObject(oldBrush);
  92. //显示解码图象
  93. CDisplay m_display;
  94. m_display.play_movie(pDC,DecfileName);
  95. conclusion=m_display.conclusion;
  96. //输出解码结果
  97. pDC->SetTextColor(RGB(200,100,150));
  98. int dd=conclusion.Find(",",0);
  99. CString kk=conclusion.Right(conclusion.GetLength()-dd-1);
  100. int zz=kk.Find(",",0);
  101. pDC->TextOut(470,180,conclusion.Left(dd));
  102. pDC->TextOut(470,200,kk.Left(zz));
  103. pDC->TextOut(500,220,kk.Right(kk.GetLength()-zz-1));
  104. }
  105. }
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CMyView printing
  108. BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
  109. {
  110. // default preparation
  111. return DoPreparePrinting(pInfo);
  112. }
  113. void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  114. {
  115. // TODO: add extra initialization before printing
  116. }
  117. void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  118. {
  119. // TODO: add cleanup after printing
  120. }
  121. /////////////////////////////////////////////////////////////////////////////
  122. // CMyView diagnostics
  123. #ifdef _DEBUG
  124. void CMyView::AssertValid() const
  125. {
  126. CView::AssertValid();
  127. }
  128. void CMyView::Dump(CDumpContext& dc) const
  129. {
  130. CView::Dump(dc);
  131. }
  132. CMyDoc* CMyView::GetDocument() // non-debug version is inline
  133. {
  134. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
  135. return (CMyDoc*)m_pDocument;
  136. }
  137. #endif //_DEBUG
  138. /////////////////////////////////////////////////////////////////////////////
  139. // CMyView message handlers
  140. void CMyView::OnEncode() 
  141. {
  142. CPropertyDlg properties("编码属性设置",this,0);
  143. int ff;
  144. CString dd;
  145. if(properties.DoModal()==IDOK)
  146. {
  147. m_szFilePathName=properties.encodeDlg.m_InitDir;
  148. m_szFileName=m_szFilePathName;
  149. m_Type=properties.encodeDlg.typeindex;
  150. MaxFrame=properties.encodeDlg.m_MaxFrame;
  151.         QP=properties.encodeDlg.m_QP;
  152. QPI=properties.encodeDlg.m_QPI;
  153. ifPsnr=properties.encodeDlg.m_ifPsnr;
  154. Pbetween=properties.encodeDlg.m_Pbetween+1;
  155.         
  156. ratecontrol=properties.m_page1.m_ifratecontrol;
  157. targetrate=properties.m_page1.m_bitrate;
  158. switch(properties.m_page1.m_framerate)
  159. {
  160. case 1:
  161.             ref_frame_rate=30.0;break;
  162. case 0:
  163.             ref_frame_rate=25.0;break;
  164. case 2:
  165. ref_frame_rate=10.0;break;
  166. default:
  167. ref_frame_rate=25.0;
  168. }
  169. int kk=targetrate;
  170. ff=properties.m_page1.m_ifratecontrol;
  171. dd.Format("采用率控制,码率是%d,帧率是%f。",kk,ref_frame_rate);
  172.         if(ff)
  173. MessageBox(dd);
  174. if(m_Type==2)
  175. CodeYUV();
  176.     else
  177. CodeBmps();
  178. }
  179. else
  180. return;
  181.   
  182. }
  183. void CMyView::CodeYUV()
  184. //率控制
  185.   #ifndef OFFLINE_RATE_CONTROL
  186.     float DelayBetweenFramesInSeconds;
  187.     int CommBacklog;
  188.   #else
  189.     PictImage *stored_image = NULL;
  190.     int start_rate_control;
  191.   #endif
  192. CMyDoc* pDoc = GetDocument();
  193. ASSERT_VALID(pDoc);
  194. //初始化
  195. CProgressBar bar(_T("Encode Progress"), 50, MaxFrame);
  196.     m_pImageData=new unsigned char[352*288*3/2];
  197. CFile file;
  198. if((file.Open(m_szFilePathName,CFile::modeRead|CFile::shareDenyNone))==NULL)
  199. {
  200. AfxMessageBox("Can not open the yuv file!");
  201. return;
  202. }
  203. PictImage *prev_image = NULL;
  204.     PictImage *curr_image = NULL;
  205.     PictImage *curr_recon = NULL;
  206.     PictImage *prev_recon = NULL;
  207. int frame_no,first_frameskip=0;  
  208.     int start=1;//从第1帧到第MaxFrame帧
  209.     int orig_frameskip=1;//输入图像原始偏移
  210.     int frameskip=1;//非发送帧
  211. long_vectors =0;//帧间编码的参数
  212. mv_outside_frame=0;
  213. Pict *pic = (Pict *)malloc(sizeof(Pict));
  214. Bits *bits = (Bits *)malloc(sizeof(Bits));
  215. //率控制
  216. Bits *total_bits = (Bits *)malloc(sizeof(Bits));
  217.     Bits *intra_bits = (Bits *)malloc(sizeof(Bits));
  218. pic->BQUANT = DEF_BQUANT;
  219.     pic->seek_dist = DEF_SEEK_DIST;
  220.     pic->use_gobsync = DEF_INSERT_SYNC;//=0
  221.     pic->PB = 0;
  222.     pic->TR = 0;
  223.     pic->QP_mean = (float)0.0;
  224.     pic->unrestricted_mv_mode = 0;
  225. pic->picture_coding_type =0; // PCT_INTRA;
  226. m_orgHeight=288;
  227. m_orgWidth=352;
  228.     pic->source_format = SF_CIF;
  229. switch (pic->source_format) {
  230.     case (SF_SQCIF):
  231.       fprintf(stdout, "Encoding format: SQCIF (128x96)n");
  232.       pels = 128;
  233.       lines = 96;
  234.       break;
  235.     case (SF_QCIF):
  236.       fprintf(stdout, "Encoding format: QCIF (176x144)n");
  237.       pels = 176;
  238.       lines = 144;
  239.       break;
  240.     case (SF_CIF):
  241.       fprintf(stdout, "Encoding format: CIF (352x288)n");
  242.       pels = 352;
  243.       lines = 288;
  244.       break;
  245.     case (SF_4CIF):
  246.       fprintf(stdout, "Encoding format: 4CIF (704x576)n");
  247.       pels = 704;
  248.       lines = 576;
  249.       break;
  250.     case (SF_16CIF):
  251.       fprintf(stdout, "Encoding format: 16CIF (1408x1152)n");
  252.       pels = 1408;
  253.       lines = 1152;
  254.       break;
  255.     default:
  256.       fprintf(stderr,"Illegal coding formatn");
  257.       exit(-1);
  258. }
  259.     cpels = pels/2;
  260. curr_recon = InitImage(pels*lines);
  261. //率控制
  262.   #ifndef OFFLINE_RATE_CONTROL
  263.   // rate control variables 
  264.   pic->bit_rate = targetrate;
  265.   pic->src_frame_rate = (int)(ref_frame_rate / orig_frameskip);
  266.   DelayBetweenFramesInSeconds = (float) 1.0/(float)pic->src_frame_rate;
  267.   InitializeRateControl();
  268.   #endif
  269.   #ifdef OFFLINE_RATE_CONTROL
  270.   start_rate_control = 0;//DEF_START_RATE_CONTROL;
  271.   #else
  272.   pic->target_frame_rate = (float)DEF_TARGET_FRAME_RATE;
  273.   #endif
  274. //建立输出文件
  275. CString outfilename=m_szFileName.Left(m_szFileName.GetLength()-4);
  276. CFileDialog dlg(FALSE,".263",outfilename,OFN_OVERWRITEPROMPT,"263 Files(*.263)|*.263|",NULL);
  277. if (dlg.DoModal()==IDOK)
  278. {
  279. bFlag=1;
  280. pDoc->UpdateAllViews(NULL);
  281.  
  282. CString tempname;
  283. tempname=dlg.GetPathName();
  284. outfilename=tempname.Left(tempname.GetLength()-4);
  285.     if((streamfile.Open(tempname,CFile::modeWrite|CFile::modeCreate))==FALSE)
  286. AfxMessageBox("Can't create file!"); 
  287. streamfile.SeekToBegin();
  288.       initbits ();
  289. CTime StartTime=CTime::GetCurrentTime();
  290.         CTimeSpan ElapsedTime;
  291.     file.Read(m_pImageData,sizeof(BYTE)*352*288*3/2);
  292. pic->QUANT=QPI;
  293. curr_image = FillImage(m_pImageData);
  294. curr_recon = CodeOneIntra(curr_image, QPI, bits, pic);
  295. bits->header += alignbits (); // pictures shall be byte aligned 
  296. //率控制
  297.   AddBitsPicture(bits);
  298.   memcpy(intra_bits,bits,sizeof(Bits));
  299.   ZeroBits(total_bits);
  300.     //* number of seconds to encode *
  301.   int chosen_frameskip=1;//jwp
  302.     //* compute first frameskip *
  303.   #ifdef OFFLINE_RATE_CONTROL
  304.    float seconds = (MaxFrame - start + chosen_frameskip) * orig_frameskip/ ref_frame_rate;
  305.    first_frameskip = chosen_frameskip;
  306.    frameskip = chosen_frameskip;
  307.   #else
  308.   CommBacklog = intra_bits->total -(int)(DelayBetweenFramesInSeconds * pic->bit_rate);
  309.   if (pic->bit_rate == 0) {
  310.     frameskip = chosen_frameskip;
  311.   }
  312.   else {  //* rate control is used *
  313.     frameskip = 1;
  314.     while ( (int)(DelayBetweenFramesInSeconds*pic->bit_rate) <= CommBacklog) {
  315.       CommBacklog -= (int) ( DelayBetweenFramesInSeconds * pic->bit_rate );
  316.       frameskip += 1;
  317.     }
  318.   }
  319.   first_frameskip = frameskip;
  320.   #endif
  321. CString kk,m_Spsnr;
  322.     if(ifPsnr)
  323. {
  324. if(psnrfile.Open(outfilename+".doc",CFile::modeWrite|CFile::modeCreate)==FALSE)
  325.     MessageBox("Cannot create the output psnr file!", "Error",MB_ICONERROR | MB_OK);
  326. SeekPsnr(curr_image,curr_recon,352,288,psnrs);
  327. frame_no=1;
  328. kk.Format("第%d帧的lum峰值信噪比为%6.4fdBn",frame_no,psnrs[0]);
  329. m_Spsnr=kk;
  330. kk.Format("第%d帧的Cb峰值信噪比为%6.4fdBn",frame_no,psnrs[1]);
  331. m_Spsnr+=kk;
  332. kk.Format("第%d帧的Cr峰值信噪比为%6.4fdBn",frame_no,psnrs[2]);
  333. m_Spsnr+=kk;
  334. MessageBox(m_Spsnr);
  335. psnrfile.SeekToBegin();
  336. psnrfile.Write(m_Spsnr,m_Spsnr.GetLength());
  337. }
  338.  //第二帧 
  339. pic->QUANT=QP;
  340.    for(frame_no=start+first_frameskip;frame_no<=MaxFrame;frame_no+=frameskip)
  341. {
  342. file.Seek((frame_no-1)*352*288*3/2,SEEK_SET);
  343.     file.Read(m_pImageData,sizeof(BYTE)*352*288*3/2);
  344. if(m_pImageData==NULL)
  345.   return;
  346.    
  347.      pic->picture_coding_type =1; // PCT_INTER;
  348.   if(!ratecontrol)
  349.   pic->QUANT=QP;
  350.   
  351.       prev_image=curr_image;
  352.   prev_recon=curr_recon;
  353.       curr_image = FillImage(m_pImageData);
  354.   pic->TR+=(((frameskip+(pic->PB?98:0))*orig_frameskip)%256);
  355.   if(frameskip+(pic->PB?98:0)>256)
  356. MessageBox("Warning:frameskip>256");
  357.      streamfile.SeekToEnd();
  358.     if(((frame_no-1)%Pbetween)==0)
  359.  {
  360.    pic->picture_coding_type =0; // PCT_INTRA;
  361.    pic->QUANT=QPI;
  362.        curr_recon = CodeOneIntra(curr_image, QPI, bits, pic);
  363.    AddBitsPicture(bits);
  364.    memcpy(intra_bits,bits,sizeof(Bits));
  365.  }
  366.     else
  367.   CodeOneInter(prev_image,curr_image,prev_recon,curr_recon,pic->QUANT,frameskip,bits,pic);
  368.   AddBitsPicture(bits); 
  369. }
  370.    bits->header += alignbits (); //* pictures shall be byte aligned *
  371.  
  372.    if(ifPsnr)
  373.    { SeekPsnr(curr_image,curr_recon,352,288,psnrs);
  374. kk.Format("第%d帧的lum峰值信噪比为%6.4fn",frame_no,psnrs[0]);
  375. m_Spsnr=kk;
  376. kk.Format("第%d帧的Cb峰值信噪比为%6.4fn",frame_no,psnrs[1]);
  377. m_Spsnr+=kk;
  378. kk.Format("第%d帧的Cr峰值信噪比为%6.4fn",frame_no,psnrs[2]);
  379. m_Spsnr+=kk;
  380. psnrfile.SeekToEnd();
  381. psnrfile.Write(m_Spsnr,m_Spsnr.GetLength());
  382.    }
  383. //率控制
  384.    AddBits(total_bits, bits);
  385. #ifndef OFFLINE_RATE_CONTROL
  386.     if (pic->bit_rate != 0 && pic->PB)
  387.       CommBacklog -= (int)
  388.       ( DelayBetweenFramesInSeconds*pic->bit_rate ) * pdist;
  389.     if (pic->bit_rate != 0) {
  390.       UpdateRateControl(bits->total);
  391.       CommBacklog += bits->total;
  392.       frameskip = 1;
  393.       CommBacklog -= (int)
  394.         (frameskip * DelayBetweenFramesInSeconds *pic->bit_rate);
  395.       while ( (int)(DelayBetweenFramesInSeconds*pic->bit_rate) <= CommBacklog)
  396.       {
  397.         CommBacklog -= (int) ( DelayBetweenFramesInSeconds * pic->bit_rate );
  398.         frameskip += 1;
  399.       }
  400.     }
  401. #else
  402.     //* Aim for the targetrate with a once per frame rate control scheme *
  403.     if (targetrate != 0)
  404.       if (frame_no - start > (MaxFrame - start) * start_rate_control/100.0)
  405.         pic->QUANT = FrameUpdateQP(total_bits->total + intra_bits->total,
  406.            bits->total / (pic->PB?2:1),
  407.            (MaxFrame-frame_no) / chosen_frameskip ,
  408.            pic->QUANT, targetrate, seconds);
  409.    frameskip = chosen_frameskip;
  410. #endif
  411.   CString kkk;
  412.   kkk.Format("编码率为%d,%d帧的total_bits->total 为%d,intra_bits->total 为 %d, bits->total 为%d,new quant is %d.",
  413.   targetrate,frame_no,total_bits->total,intra_bits->total,bits->total,pic->QUANT);
  414.   if(ifPsnr)
  415.   {
  416.   psnrfile.SeekToEnd();
  417.       psnrfile.Write(kkk,kkk.GetLength());
  418.   }
  419. //显示进度信息
  420. CString str;
  421. str.Format("%d%% complete", frame_no*100/MaxFrame);
  422. bar.SetText(str);
  423. bar.StepIt();
  424.     PeekAndPump(); //调用函数实现消息的转发
  425.    }//end for frame_no
  426. //  pDoc->SetModifiedFlag(TRUE);
  427.   file.Close();
  428.   if(ifPsnr)
  429.   psnrfile.Close();
  430.   streamfile.Close();
  431.   delete prev_image;
  432.   delete prev_recon;
  433.   delete curr_image;
  434.   curr_recon=NULL;
  435. //  delete curr_recon;
  436.   free(bits);
  437.   free(pic);
  438.   
  439.     long hours,minutes,second;//计算所用的时间
  440.     ElapsedTime = CTime::GetCurrentTime() - StartTime;
  441. bFlag=0;    
  442. hours = ElapsedTime.GetTotalHours();
  443.     minutes = ElapsedTime.GetTotalMinutes();
  444.     second = ElapsedTime.GetTotalSeconds();
  445.     second = second + 60*minutes + 3600*hours;
  446.     csTimeElapse.Format("编码%d帧视频序列,耗时:%d秒!",frame_no-1,second);
  447.     MessageBox(csTimeElapse);
  448. pDoc->UpdateAllViews(NULL);
  449. }
  450. else
  451.     MessageBox("No file is saved.","系统提示",MB_OK);
  452. // delete m_pImageData;
  453. }
  454. void CMyView::CodeBmps()
  455. {
  456.    //初始化
  457. CMyDoc* pDoc = GetDocument();
  458. ASSERT_VALID(pDoc);
  459. CProgressBar bar(_T("Encode Progress"), 50, MaxFrame);
  460.     m_pImageData=new unsigned char[352*288+352*288/2];
  461. m_pImageData = OpenImageFile(m_szFilePathName, &m_orgWidth, &m_orgHeight, m_Type);
  462.     PictImage *prev_image = NULL;
  463.     PictImage *curr_image = NULL;
  464.     PictImage *curr_recon = NULL;
  465.     PictImage *prev_recon = NULL;
  466. int frame_no,first_frameskip=0;  
  467.     int start=2;//从第1帧到第MaxFrame帧
  468.     int orig_frameskip=1;//输入图像原始偏移
  469.     int frameskip=1;//非发送帧
  470. long_vectors =0;//帧间编码的参数
  471. mv_outside_frame=0;
  472. Pict *pic = (Pict *)malloc(sizeof(Pict));
  473. Bits *bits = (Bits *)malloc(sizeof(Bits));
  474. pic->BQUANT = DEF_BQUANT;
  475.     pic->seek_dist = DEF_SEEK_DIST;
  476.     pic->use_gobsync = DEF_INSERT_SYNC;//=0
  477.     pic->PB = 0;
  478.     pic->TR = 0;
  479.     pic->QP_mean = (float)0.0;
  480.     pic->unrestricted_mv_mode = 0;
  481. pic->picture_coding_type =0; // PCT_INTRA;
  482. if((m_orgHeight==288)&&(m_orgWidth==352))
  483.        pic->source_format = SF_CIF;
  484. switch (pic->source_format) {
  485.     case (SF_SQCIF):
  486.       fprintf(stdout, "Encoding format: SQCIF (128x96)n");
  487.       pels = 128;
  488.       lines = 96;
  489.       break;
  490.     case (SF_QCIF):
  491.       fprintf(stdout, "Encoding format: QCIF (176x144)n");
  492.       pels = 176;
  493.       lines = 144;
  494.       break;
  495.     case (SF_CIF):
  496.       fprintf(stdout, "Encoding format: CIF (352x288)n");
  497.       pels = 352;
  498.       lines = 288;
  499.       break;
  500.     case (SF_4CIF):
  501.       fprintf(stdout, "Encoding format: 4CIF (704x576)n");
  502.       pels = 704;
  503.       lines = 576;
  504.       break;
  505.     case (SF_16CIF):
  506.       fprintf(stdout, "Encoding format: 16CIF (1408x1152)n");
  507.       pels = 1408;
  508.       lines = 1152;
  509.       break;
  510.     default:
  511.       fprintf(stderr,"Illegal coding formatn");
  512.       exit(-1);
  513. }
  514.     cpels = pels/2;
  515. curr_recon = InitImage(pels*lines);
  516. //建立输出文件
  517. CString outfilename=m_szFileName.Left(m_szFileName.GetLength()-4);
  518. CFileDialog dlg(FALSE,".263",outfilename,OFN_OVERWRITEPROMPT,"263 Files(*.263)|*.263|",NULL);
  519. if (dlg.DoModal()==IDOK)
  520. {  
  521. bFlag=1;
  522. pDoc->UpdateAllViews(NULL);
  523.         CString tempname;
  524. tempname=dlg.GetPathName();
  525. outfilename=tempname.Left(tempname.GetLength()-4);
  526.     if((streamfile.Open(tempname,CFile::modeWrite|CFile::modeCreate))==FALSE)
  527. AfxMessageBox("Can't create file!"); 
  528. streamfile.SeekToBegin();
  529.       initbits ();
  530. CTime StartTime=CTime::GetCurrentTime();
  531.         CTimeSpan ElapsedTime;
  532. pic->QUANT=QPI;
  533. curr_image = FillImage(m_pImageData);
  534. curr_recon = CodeOneIntra(curr_image, QPI, bits, pic);
  535. bits->header += alignbits (); /* pictures shall be byte aligned */
  536. CString kk,m_Spsnr;
  537.    if(ifPsnr)
  538.    {
  539. if(psnrfile.Open(outfilename+".doc",CFile::modeWrite|CFile::modeCreate)==FALSE)
  540.     MessageBox("Cannot create the output psnr file!", "Error",MB_ICONERROR | MB_OK);
  541. SeekPsnr(curr_image,curr_recon,352,288,psnrs);
  542. frame_no=1;
  543. kk.Format("第%d帧的lum峰值信噪比为%6.4fn",frame_no,psnrs[0]);
  544. m_Spsnr=kk;
  545. kk.Format("第%d帧的Cb峰值信噪比为%6.4fn",frame_no,psnrs[1]);
  546. m_Spsnr+=kk;
  547. kk.Format("第%d帧的Cr峰值信噪比为%6.4fn",frame_no,psnrs[2]);
  548. m_Spsnr+=kk;
  549. MessageBox(m_Spsnr);
  550. psnrfile.SeekToBegin();
  551. psnrfile.Write(m_Spsnr,m_Spsnr.GetLength());
  552.    }
  553.  //第二帧 
  554.     for(frame_no=start+first_frameskip;frame_no<=MaxFrame;frame_no+=frameskip)
  555. {
  556.      m_szFilePathName = GetNextFileName(m_szFilePathName,1);
  557.       m_pImageData = OpenImageFile(m_szFilePathName, &m_orgWidth, &m_orgHeight, m_Type);
  558.   if(m_pImageData==NULL)
  559.   return;
  560.    
  561.      pic->picture_coding_type =1; // PCT_INTER;
  562.   pic->QUANT=QP;
  563.       prev_image=curr_image;
  564.   prev_recon=curr_recon;
  565.       curr_image = FillImage(m_pImageData);
  566.   pic->TR+=(((frameskip+(pic->PB?98:0))*orig_frameskip)%256);
  567.   if(frameskip+(pic->PB?98:0)>256)
  568. MessageBox("Warning:frameskip>256");
  569.      streamfile.SeekToEnd();
  570.      if(((frame_no-1)%Pbetween)==0)
  571.  {
  572.    pic->picture_coding_type =0; // PCT_INTRA;
  573.    pic->QUANT=QPI;
  574.        curr_recon = CodeOneIntra(curr_image, QPI, bits, pic);
  575.  }
  576.     else
  577.     CodeOneInter(prev_image,curr_image,prev_recon,curr_recon,QP,frameskip,bits,pic);
  578.     bits->header += alignbits (); /* pictures shall be byte aligned */
  579.    if(ifPsnr)
  580.    {
  581. SeekPsnr(curr_image,curr_recon,352,288,psnrs);
  582. kk.Format("第%d帧的lum峰值信噪比为%6.4fn",frame_no,psnrs[0]);
  583. m_Spsnr=kk;
  584. kk.Format("第%d帧的Cb峰值信噪比为%6.4fn",frame_no,psnrs[1]);
  585. m_Spsnr+=kk;
  586. kk.Format("第%d帧的Cr峰值信噪比为%6.4fn",frame_no,psnrs[2]);
  587. m_Spsnr+=kk;
  588. psnrfile.SeekToEnd();
  589. psnrfile.Write(m_Spsnr,m_Spsnr.GetLength());
  590.    }
  591. //显示进度信息
  592. CString str;
  593. str.Format("%d%% complete", frame_no*100/MaxFrame);
  594. bar.SetText(str);
  595. bar.StepIt();
  596.     PeekAndPump(); //调用函数实现消息的转发
  597.    }//end for frame_no
  598. //  pDoc->SetModifiedFlag(TRUE);
  599.   if(ifPsnr)
  600.   psnrfile.Close();
  601.   streamfile.Close();
  602.   delete prev_image;
  603.   delete prev_recon;
  604.   delete curr_image;
  605.   curr_recon=NULL;
  606. //  delete curr_recon;
  607.   free(bits);
  608.   free(pic);
  609.     long hours,minutes,second;//计算所用的时间
  610.     ElapsedTime = CTime::GetCurrentTime() - StartTime;
  611. bFlag=0;
  612.     hours = ElapsedTime.GetTotalHours();
  613.     minutes = ElapsedTime.GetTotalMinutes();
  614.     second = ElapsedTime.GetTotalSeconds();
  615.     second = second + 60*minutes + 3600*hours;
  616.     csTimeElapse.Format("编码%d帧图像序列,耗时:%d秒!",frame_no-1,second);
  617.     MessageBox(csTimeElapse);
  618. pDoc->UpdateAllViews(NULL);
  619. }
  620. else
  621.     MessageBox("No file is saved.","系统提示",MB_OK);
  622. delete m_pImageData;
  623. }
  624. BOOL CMyView::PeekAndPump()
  625. {
  626. static MSG msg;
  627. while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) 
  628. {
  629. if (!AfxGetApp()->PumpMessage()) 
  630. {
  631. ::PostQuitMessage(0);
  632. return FALSE;
  633. }
  634. }
  635. return TRUE;
  636. }
  637. void CMyView::OnDecode() 
  638. {
  639. CMyDoc* pDoc = GetDocument();
  640. ASSERT_VALID(pDoc);
  641. CDC *pDC=GetDC();
  642. CFileDialog decfile(TRUE,NULL,NULL,OFN_HIDEREADONLY,"263 Files(*.263)|*.263|");
  643. decfile.m_ofn.lpstrTitle="打开263文件";
  644. if(decfile.DoModal()==IDOK)
  645.    DecfileName=decfile.GetPathName();//获取263文件路径
  646. bFlag=2;
  647. pDoc->UpdateAllViews(NULL);
  648. }
  649. void SeekPsnr(PictImage *curr, PictImage *recon, int width,int height, double psnr[3])
  650. {
  651. int i;
  652. double MSE1=0;
  653. double MSE2=0;
  654.     for(i=0;i<width*height;i++)
  655.     MSE1+=((double)recon->lum[i]-(double)curr->lum[i])
  656.     *((double)recon->lum[i]-(double)curr->lum[i]);
  657. MSE1/=(width*height);
  658. if(MSE1==0)
  659. psnr[0]=0;
  660. else
  661. psnr[0]=10*log10(255*255/MSE1);
  662. MSE1=0;
  663. for(i=0;i<((width*height)>>2);i++)
  664. {
  665. MSE1+=((double)recon->Cb[i]-(double)curr->Cb[i])
  666.     *((double)recon->Cb[i]-(double)curr->Cb[i]);
  667. MSE2+=((double)recon->Cr[i]-(double)curr->Cr[i])
  668.     *((double)recon->Cr[i]-(double)curr->Cr[i]);
  669. }  
  670. MSE1/=(width*height)>>2;
  671. MSE2/=(width*height)>>2;
  672. if(MSE1==0)
  673. psnr[1]=0;
  674. else
  675. psnr[1]=10*log10(255*255/MSE1);
  676. if(MSE2==0)
  677. psnr[2]=0;
  678. else
  679. psnr[2]=10*log10(255*255/MSE2);
  680. }
  681. void CMyView::OnFileOpen() 
  682. {
  683. CDC* pDC;
  684.     pDC=GetDC();
  685. CFile look_result;
  686. CString resultshow="";
  687. CString kk;
  688.     CFileDialog lookresult(TRUE,NULL,NULL,OFN_HIDEREADONLY,"文本 Files(*.txt;*.doc)|*.txt;*.doc|");
  689. lookresult.m_ofn.lpstrTitle="打开编码结果文件";
  690. lookresult.m_ofn.lpstrInitialDir="F:\standard_pictures\MISSUSA_raw";
  691. if(lookresult.DoModal()==IDOK)
  692. {
  693.     resultshow=lookresult.GetPathName();
  694.     if (look_result.Open(resultshow,CFile::modeRead)==NULL)
  695. {
  696.     AfxMessageBox("Can not open the result file!");
  697.     return;
  698. }
  699.     look_result.SeekToBegin(); 
  700. }
  701. }