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

📄 h263dedlg.cpp

📁 vc下基于对话框的h。263文件解码并显示。263文件在目录下
💻 CPP
字号:
// h263DeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "h263De.h"
#include "h263DeDlg.h"
#include<stdio.h>
#include "tmndec.h"
#include "conio.h"
#include <iostream>
#include "string.h"
#include "decstruct.h"
#include "vfw.h"
#include "signal.h"


using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



#define IMAGE_SIZE  76032
#define FILE_SIZE   76086
#define IMAGE_WIDTH  176
#define IMAGE_HEIGHT 144


unsigned char rgbdata[176*144*3];

bool PlayFlag;
static void initdecoder();

struct bmpfileheader
{
int fsize;
int reserved;
int offset;
}bfh;

struct bmpinfoheader
{
int bisize;
int biwidth;
int biheight;
short biplanes;
short bibitcount;
int bicompression;
int bisizeimage;
int bixpixelspermeter;
int biypixelspermeter;
int biclrused;
int biclrimp;
}bih;




/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CH263DeDlg dialog

CH263DeDlg::CH263DeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CH263DeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CH263DeDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CH263DeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CH263DeDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CH263DeDlg, CDialog)
	//{{AFX_MSG_MAP(CH263DeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, OnDecode)
		ON_MESSAGE(WM_VIDEO_SHOW, OnVideoShow)  //b
	ON_MESSAGE(WM_CONNECT, OnTextShow)      //b
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CH263DeDlg message handlers

BOOL CH263DeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CH263DeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CH263DeDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CH263DeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CH263DeDlg::OnDecode() 
{    PlayFlag=1;
	::AfxBeginThread(ReceiveThreadProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
}


/**********************************
 *
 *  分线程,接收数据
 *
 ***********************************/
UINT ReceiveThreadProc(LPVOID pParam)
{
int framenum;


string s;
FILE *bmp,*yuv;
char tstr[]="BM";


	
   //InitH263Decoder();
  
     framerate=0;  // not used...
     outtype=0;    // YUV 
	 temp_ref = 0;
     prev_temp_ref= -1; 
     ld = &base;   
     initbits();    // Setup source buffer pointers
	 InitConvertTable();

 //     yuv=fopen("out.yuv","wb");



 if ((base.infile=open("output.263",O_RDONLY|O_BINARY))<0)
 {
 }
  
   	    yp=NULL;
		up=NULL;
		vp=NULL;
  /**********************/
int  first = 1;
do {
	 // if (base.infile!=0)
     // lseek(base.infile,0l,0);
  
    initbits();
    framenum = 0;
    temp_ref = 0;
    prev_temp_ref -1; 
	while (getheader())
	{
     if (first) 
	 {
        initdecoder();
		first=0;
	 }
     getpicture(&framenum);
	 framenum++;



//	 fwrite(yp,1,IMAGE_WIDTH*IMAGE_HEIGHT,yuv);
//	 fwrite(up,1,IMAGE_WIDTH*IMAGE_HEIGHT/4,yuv);
//	 fwrite(vp,1,IMAGE_WIDTH*IMAGE_HEIGHT/4,yuv);

	 
	 ConvertYUV2RGB(yp,vp,up,rgbdata,176,144);
	 ////////////
	    unsigned char tmp;
        // 反转数组
        for(int i=0;i<IMAGE_HEIGHT/2;i++)
		{
		    for(int j=0;j<IMAGE_WIDTH;j++)
			{

			    tmp=*(rgbdata+(IMAGE_HEIGHT-i-1)*IMAGE_WIDTH*3+3*j);
                *(rgbdata+(IMAGE_HEIGHT-i-1)*IMAGE_WIDTH*3+3*j)=*(rgbdata+i*IMAGE_WIDTH*3+3*j);
                *(rgbdata+i*IMAGE_WIDTH*3+3*j)=tmp;

			    tmp=*(rgbdata+(IMAGE_HEIGHT-i-1)*IMAGE_WIDTH*3+3*j+1);
                *(rgbdata+(IMAGE_HEIGHT-i-1)*IMAGE_WIDTH*3+3*j+1)=*(rgbdata+i*IMAGE_WIDTH*3+3*j+1);
                *(rgbdata+i*IMAGE_WIDTH*3+3*j+1)=tmp;

			    tmp=*(rgbdata+(IMAGE_HEIGHT-i-1)*IMAGE_WIDTH*3+3*j+2);
                *(rgbdata+(IMAGE_HEIGHT-i-1)*IMAGE_WIDTH*3+3*j+2)=*(rgbdata+i*IMAGE_WIDTH*3+3*j+2);
                *(rgbdata+i*IMAGE_WIDTH*3+3*j+2)=tmp;

			}
		}
		//////////
	
     Sleep(35);
	 if(PlayFlag==1)
	{	
	   ::SendMessage((HWND)pParam,WM_VIDEO_SHOW,(WPARAM)rgbdata, 0);//red
	}

    ::SendMessage((HWND)pParam,WM_CONNECT,(WPARAM)framenum,0);

	
	}
	

}while (0);

//close(base.infile);

  ExitH263Decoder();
//  fclose(yuv); 
	
return 1;
}

/**********************************
 *   在对话框中显示图像
 *   wParam传入图像数据指针
 ***********************************/
LRESULT CH263DeDlg::OnVideoShow(WPARAM wParam, LPARAM lParam)
{	
	HDRAWDIB hdd=::DrawDibOpen();
    HDC hdc=::GetDC(m_hWnd);

	BITMAPINFOHEADER pBmpInfoHeader = 
	{
	  40, IMAGE_WIDTH, IMAGE_HEIGHT, 1, 24, 0, IMAGE_WIDTH*IMAGE_HEIGHT*3*sizeof(unsigned char), 0, 0, 0, 0
	};

	::DrawDibDraw(hdd,hdc,0,0,IMAGE_WIDTH,IMAGE_HEIGHT,&pBmpInfoHeader,rgbdata,0,0,IMAGE_WIDTH,IMAGE_HEIGHT,NULL);
	if( hdd != NULL) { DrawDibClose( hdd);}
	if(hdc!=NULL) {DeleteDC(hdc);}


	return 1;
}

/**********************************
 *
 *   在dialog中显示当前接收祯数
 *
 ***********************************/
LRESULT CH263DeDlg::OnTextShow(WPARAM wParam)
{	
	CString s;
    int l;
	l=(int)wParam;
    s.Format("解码%d 祯",l);
	GetDlgItem(IDC_EDITMSG)->SetWindowText(s);
    return 1;
}
/**
*    Initialize decoder
*
*
*/
void initdecoder()
{
  int i, cc, size;


  /* clip table */
  if (!(clp=(unsigned char *)malloc(1024)))
    error("malloc failed\n");

  clp += 384;

  for (i=-384; i<640; i++)
    clp[i] = (i<0) ? 0 : ((i>255) ? 255 : i);

  /* MPEG-1 = TMN parameters */
  matrix_coefficients = 5;


  switch (source_format) 
  {
  
	case (SF_SQCIF):
      horizontal_size = 128;
      vertical_size = 96;
      break;
    case (SF_QCIF):
      horizontal_size = 176;
      vertical_size = 144;
      break;
    case (SF_CIF):
      horizontal_size = 352;
      vertical_size = 288;
      break;
    case (SF_4CIF):
      horizontal_size = 704;
      vertical_size = 576;
      break;
    case (SF_16CIF):
      horizontal_size = 1408;
      vertical_size = 1152;
      break;
    default:
      if(trace)
	  fputs("ERROR: Illegal input format\n",dlog);
      return;
      break;
  }


  mb_width = horizontal_size/16;
  mb_height = vertical_size/16;
  coded_picture_width = horizontal_size;
  coded_picture_height = vertical_size;
  chrom_width =  coded_picture_width>>1;
  chrom_height = coded_picture_height>>1;
  blk_cnt = 6;

  for (cc=0; cc<3; cc++) 
  
  {
  
	// Find out size...
	 if (cc==0)
      size = coded_picture_width*coded_picture_height;
    else
      size = chrom_width*chrom_height;


	// Allocate memory
    if (!(refframe[cc] = (unsigned char *)malloc(size)))
      error("malloc failed\n");


    if (!(oldrefframe[cc] = (unsigned char *)malloc(size)))
      error("malloc failed\n");

    if (!(bframe[cc] = (unsigned char *)malloc(size)))
      error("malloc failed\n");
  }

  for (cc=0; cc<3; cc++) 
  {
  
	  if (cc==0) 
	  {
		  size = (coded_picture_width+64)*(coded_picture_height+64);
      
		  if (!(edgeframeorig[cc] = (unsigned char *)malloc(size)))
		  error("malloc failed\n");
      
		  
		  edgeframe[cc] = edgeframeorig[cc] + (coded_picture_width+64) * 32 + 32;
	  }
      else 
	  {
      size = (chrom_width+32)*(chrom_height+32);
      
	  if (!(edgeframeorig[cc] = (unsigned char *)malloc(size)))
      error("malloc failed\n");
      
	  edgeframe[cc] = edgeframeorig[cc] + (chrom_width+32) * 16 + 16;
	  }
    
  }

  
  /* IDCT */
  if (refidct)
    init_idctref();
  else
    init_idct();
}

⌨️ 快捷键说明

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