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

📄 视频编解码器view.cpp

📁 263yasuoyuv.rar是把yuv-411格式文件压缩为*.263可直看
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	  prev_recon=curr_recon;
      curr_image = FillImage(m_pImageData);
	  pic->TR+=(((frameskip+(pic->PB?98:0))*orig_frameskip)%256);
	  if(frameskip+(pic->PB?98:0)>256)
		MessageBox("Warning:frameskip>256");

     streamfile.SeekToEnd();
    if(((frame_no-1)%Pbetween)==0)
	 {
	   pic->picture_coding_type =0; // PCT_INTRA;
	   pic->QUANT=QPI;
       curr_recon = CodeOneIntra(curr_image, QPI, bits, pic);
	   AddBitsPicture(bits);
	   memcpy(intra_bits,bits,sizeof(Bits));

	 }
    else
	{ 
	  CodeOneInter(prev_image,curr_image,prev_recon,curr_recon,pic->QUANT,frameskip,bits,pic);
	  AddBitsPicture(bits); 
	}
   bits->header += alignbits (); //* pictures shall be byte aligned *

 
   if(ifPsnr)
   {	SeekPsnr(curr_image,curr_recon,352,288,psnrs);
	kk.Format("第%d帧的lum峰值信噪比为%6.4f\n",frame_no,psnrs[0]);
	m_Spsnr=kk;
	kk.Format("第%d帧的Cb峰值信噪比为%6.4f\n",frame_no,psnrs[1]);
	m_Spsnr+=kk;
	kk.Format("第%d帧的Cr峰值信噪比为%6.4f\n",frame_no,psnrs[2]);
	m_Spsnr+=kk;
	psnrfile.SeekToEnd();
	psnrfile.Write(m_Spsnr,m_Spsnr.GetLength());
   }
//率控制

   AddBits(total_bits, bits);
#ifndef OFFLINE_RATE_CONTROL
    if (pic->bit_rate != 0 && pic->PB)
      CommBacklog -= (int)
      ( DelayBetweenFramesInSeconds*pic->bit_rate ) * pdist;

    if (pic->bit_rate != 0) {
      UpdateRateControl(bits->total);

      CommBacklog += bits->total;
      frameskip = 1;
      CommBacklog -= (int)
        (frameskip * DelayBetweenFramesInSeconds *pic->bit_rate);

      while ( (int)(DelayBetweenFramesInSeconds*pic->bit_rate) <= CommBacklog)
      {
        CommBacklog -= (int) ( DelayBetweenFramesInSeconds * pic->bit_rate );
        frameskip += 1;
      }
    }
#else
    //* Aim for the targetrate with a once per frame rate control scheme *
    if (targetrate != 0)
      if (frame_no - start > (MaxFrame - start) * start_rate_control/100.0)

        pic->QUANT = FrameUpdateQP(total_bits->total + intra_bits->total,
           bits->total / (pic->PB?2:1),
           (MaxFrame-frame_no) / chosen_frameskip ,
           pic->QUANT, targetrate, seconds);
   frameskip = chosen_frameskip;
#endif
	  CString kkk;
	  kkk.Format("编码率为%d,%d帧的total_bits->total 为%d,intra_bits->total 为 %d, bits->total 为%d,new quant is %d.",
		  targetrate,frame_no,total_bits->total,intra_bits->total,bits->total,pic->QUANT);
	  if(ifPsnr)
	  {
		  psnrfile.SeekToEnd();
	      psnrfile.Write(kkk,kkk.GetLength());
	  }

	//显示进度信息
	CString str;
	str.Format("%d%% complete", frame_no*100/MaxFrame);
	bar.SetText(str);
	bar.StepIt();
    PeekAndPump(); //调用函数实现消息的转发
   }//end for frame_no
	
//  pDoc->SetModifiedFlag(TRUE);
  file.Close();
  if(ifPsnr)
  psnrfile.Close();
  streamfile.Close();
  delete prev_image;
  delete prev_recon;
  delete curr_image;
  curr_recon=NULL;
//  delete curr_recon;
  free(bits);
  free(pic);
  

    long hours,minutes,second;//计算所用的时间
    ElapsedTime = CTime::GetCurrentTime() - StartTime;
	bFlag=0;    
	hours = ElapsedTime.GetTotalHours();
    minutes = ElapsedTime.GetTotalMinutes();
    second = ElapsedTime.GetTotalSeconds();
    second = second + 60*minutes + 3600*hours;
    csTimeElapse.Format("编码%d帧视频序列,耗时:%d秒!",frame_no-1,second);
    MessageBox(csTimeElapse);
	pDoc->UpdateAllViews(NULL);
	}
	else
    MessageBox("No file is saved.","系统提示",MB_OK);
//	delete m_pImageData;

}

void CMyView::CodeBmps()
{
   //初始化
	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CProgressBar bar(_T("Encode Progress"), 50, MaxFrame);

    m_pImageData=new unsigned char[352*288+352*288/2];
	m_pImageData = OpenImageFile(m_szFilePathName, &m_orgWidth, &m_orgHeight, m_Type);

    PictImage *prev_image = NULL;
    PictImage *curr_image = NULL;
    PictImage *curr_recon = NULL;
    PictImage *prev_recon = NULL;

	int frame_no,first_frameskip=0;  
    int start=2;//从第1帧到第MaxFrame帧
    int orig_frameskip=1;//输入图像原始偏移
    int frameskip=1;//非发送帧

	long_vectors =0;//帧间编码的参数
	mv_outside_frame=0;

	Pict *pic = (Pict *)malloc(sizeof(Pict));
	Bits *bits = (Bits *)malloc(sizeof(Bits));
	pic->BQUANT = DEF_BQUANT;
    pic->seek_dist = DEF_SEEK_DIST;
    pic->use_gobsync = DEF_INSERT_SYNC;//=0
    pic->PB = 0;
    pic->TR = 0;
    pic->QP_mean = (float)0.0;
    pic->unrestricted_mv_mode = 0;
	pic->picture_coding_type =0; // PCT_INTRA;
	if((m_orgHeight==288)&&(m_orgWidth==352))
       pic->source_format = SF_CIF;
	switch (pic->source_format) {
    case (SF_SQCIF):
      fprintf(stdout, "Encoding format: SQCIF (128x96)\n");
      pels = 128;
      lines = 96;
      break;
    case (SF_QCIF):
      fprintf(stdout, "Encoding format: QCIF (176x144)\n");
      pels = 176;
      lines = 144;
      break;
    case (SF_CIF):
      fprintf(stdout, "Encoding format: CIF (352x288)\n");
      pels = 352;
      lines = 288;
      break;
    case (SF_4CIF):
      fprintf(stdout, "Encoding format: 4CIF (704x576)\n");
      pels = 704;
      lines = 576;
      break;
    case (SF_16CIF):
      fprintf(stdout, "Encoding format: 16CIF (1408x1152)\n");
      pels = 1408;
      lines = 1152;
      break;
    default:
      fprintf(stderr,"Illegal coding format\n");
      exit(-1);
	}
    cpels = pels/2;
	curr_recon = InitImage(pels*lines);

	//建立输出文件
	CString outfilename=m_szFileName.Left(m_szFileName.GetLength()-4);
	CFileDialog dlg(FALSE,".263",outfilename,OFN_OVERWRITEPROMPT,"263 Files(*.263)|*.263|",NULL);
	if (dlg.DoModal()==IDOK)
	{  
		bFlag=1;
		pDoc->UpdateAllViews(NULL);

        CString tempname;
		tempname=dlg.GetPathName();
		outfilename=tempname.Left(tempname.GetLength()-4);

	    if((streamfile.Open(tempname,CFile::modeWrite|CFile::modeCreate))==FALSE)
			AfxMessageBox("Can't create file!"); 
		streamfile.SeekToBegin();
     	initbits ();
		
		CTime StartTime=CTime::GetCurrentTime();
        CTimeSpan ElapsedTime;

	pic->QUANT=QPI;
	curr_image = FillImage(m_pImageData);
	curr_recon = CodeOneIntra(curr_image, QPI, bits, pic);
	bits->header += alignbits (); /* pictures shall be byte aligned */

	CString kk,m_Spsnr;
   if(ifPsnr)
   {
	if(psnrfile.Open(outfilename+".doc",CFile::modeWrite|CFile::modeCreate)==FALSE)
    MessageBox("Cannot create the output psnr file!", "Error",MB_ICONERROR | MB_OK);
	SeekPsnr(curr_image,curr_recon,352,288,psnrs);
	frame_no=1;
	kk.Format("第%d帧的lum峰值信噪比为%6.4f\n",frame_no,psnrs[0]);
	m_Spsnr=kk;
	kk.Format("第%d帧的Cb峰值信噪比为%6.4f\n",frame_no,psnrs[1]);
	m_Spsnr+=kk;
	kk.Format("第%d帧的Cr峰值信噪比为%6.4f\n",frame_no,psnrs[2]);
	m_Spsnr+=kk;
	MessageBox(m_Spsnr);
	psnrfile.SeekToBegin();
	psnrfile.Write(m_Spsnr,m_Spsnr.GetLength());
   }

	 //第二帧 


    for(frame_no=start+first_frameskip;frame_no<=MaxFrame;frame_no+=frameskip)
	{
  	  m_szFilePathName = GetNextFileName(m_szFilePathName,1);
   	  m_pImageData = OpenImageFile(m_szFilePathName, &m_orgWidth, &m_orgHeight, m_Type);
	  if(m_pImageData==NULL)
		  return;
	   
  	  pic->picture_coding_type =1; // PCT_INTER;
	  pic->QUANT=QP;
      prev_image=curr_image;
	  prev_recon=curr_recon;
      curr_image = FillImage(m_pImageData);
	  pic->TR+=(((frameskip+(pic->PB?98:0))*orig_frameskip)%256);
	  if(frameskip+(pic->PB?98:0)>256)
		MessageBox("Warning:frameskip>256");

     streamfile.SeekToEnd();
     if(((frame_no-1)%Pbetween)==0)
	 {
	   pic->picture_coding_type =0; // PCT_INTRA;
	   pic->QUANT=QPI;
       curr_recon = CodeOneIntra(curr_image, QPI, bits, pic);
	 }
    else
    CodeOneInter(prev_image,curr_image,prev_recon,curr_recon,QP,frameskip,bits,pic);
    bits->header += alignbits (); /* pictures shall be byte aligned */
	
   if(ifPsnr)
   {	
	SeekPsnr(curr_image,curr_recon,352,288,psnrs);
	kk.Format("第%d帧的lum峰值信噪比为%6.4f\n",frame_no,psnrs[0]);
	m_Spsnr=kk;
	kk.Format("第%d帧的Cb峰值信噪比为%6.4f\n",frame_no,psnrs[1]);
	m_Spsnr+=kk;
	kk.Format("第%d帧的Cr峰值信噪比为%6.4f\n",frame_no,psnrs[2]);
	m_Spsnr+=kk;
	psnrfile.SeekToEnd();
	psnrfile.Write(m_Spsnr,m_Spsnr.GetLength());
   }

	//显示进度信息
	CString str;
	str.Format("%d%% complete", frame_no*100/MaxFrame);
	bar.SetText(str);
	bar.StepIt();
    PeekAndPump(); //调用函数实现消息的转发

   }//end for frame_no

//  pDoc->SetModifiedFlag(TRUE);
  if(ifPsnr)
  psnrfile.Close();

  streamfile.Close();
  delete prev_image;
  delete prev_recon;
  delete curr_image;
  curr_recon=NULL;
//  delete curr_recon;
  free(bits);
  free(pic);

    long hours,minutes,second;//计算所用的时间
    ElapsedTime = CTime::GetCurrentTime() - StartTime;
	bFlag=0;
    hours = ElapsedTime.GetTotalHours();
    minutes = ElapsedTime.GetTotalMinutes();
    second = ElapsedTime.GetTotalSeconds();
    second = second + 60*minutes + 3600*hours;

    csTimeElapse.Format("编码%d帧图像序列,耗时:%d秒!",frame_no-1,second);
    MessageBox(csTimeElapse);
	pDoc->UpdateAllViews(NULL);

	}
	else
    MessageBox("No file is saved.","系统提示",MB_OK);
	delete m_pImageData;
}

BOOL CMyView::PeekAndPump()
{

	static MSG msg;
	while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) 
	{
		if (!AfxGetApp()->PumpMessage()) 
		{
			::PostQuitMessage(0);
			return FALSE;
		}	
	}
	return TRUE;

}

void CMyView::OnDecode() 
{	
	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CDC *pDC=GetDC();

	CFileDialog decfile(TRUE,NULL,NULL,OFN_HIDEREADONLY,"263 Files(*.263)|*.263|");
	decfile.m_ofn.lpstrTitle="打开263文件";
	if(decfile.DoModal()==IDOK)
	   DecfileName=decfile.GetPathName();//获取263文件路径
	bFlag=2;	
	pDoc->UpdateAllViews(NULL);	
}

void SeekPsnr(PictImage *curr, PictImage *recon, int width,int height, double psnr[3])
{
	int i;
	double MSE1=0;
	double MSE2=0;

    for(i=0;i<width*height;i++)
	    MSE1+=((double)recon->lum[i]-(double)curr->lum[i])
			    *((double)recon->lum[i]-(double)curr->lum[i]);
	MSE1/=(width*height);
	if(MSE1==0)
		psnr[0]=0;
	else
		psnr[0]=10*log10(255*255/MSE1);

	MSE1=0;
	for(i=0;i<((width*height)>>2);i++)
	{
		MSE1+=((double)recon->Cb[i]-(double)curr->Cb[i])
			    *((double)recon->Cb[i]-(double)curr->Cb[i]);
		MSE2+=((double)recon->Cr[i]-(double)curr->Cr[i])
			    *((double)recon->Cr[i]-(double)curr->Cr[i]);
	}  
	MSE1/=(width*height)>>2;
	MSE2/=(width*height)>>2;
	if(MSE1==0)
		psnr[1]=0;
	else
		psnr[1]=10*log10(255*255/MSE1);
	if(MSE2==0)
		psnr[2]=0;
	else
		psnr[2]=10*log10(255*255/MSE2);

}



void CMyView::OnFileOpen() 
{
	CDC* pDC;
    pDC=GetDC();

	CFile look_result;
	CString resultshow="";
	CString kk;
   	CFileDialog lookresult(TRUE,NULL,NULL,OFN_HIDEREADONLY,"文本 Files(*.txt;*.doc)|*.txt;*.doc|");
	lookresult.m_ofn.lpstrTitle="打开编码结果文件";
	lookresult.m_ofn.lpstrInitialDir="F:\\standard_pictures\\MISSUSA_raw";
	if(lookresult.DoModal()==IDOK)
	{
	    resultshow=lookresult.GetPathName();
	    if (look_result.Open(resultshow,CFile::modeRead)==NULL)
		{
	    AfxMessageBox("Can not open the result file!");
	    return;
		}
	    look_result.SeekToBegin(); 
	}
	
}

⌨️ 快捷键说明

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