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

📄 h264decodedlg.cpp

📁 H264EncPlayer,H264协议解码与播放代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// H264DecodeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "H264Decode.h"
#include "H264DecodeDlg.h"

#include "winbase.h"

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

//全局变量
// parameters begin
int32_t total_no = 300;
char src_path[256];
char out_path[256];
char rec_path[256];
// for decoder PSNR
char ref_path[256];
static int  ref_skip;

//用来显示的全局变量
unsigned char *src[3];
BOOL PlayingVideo=FALSE;
unsigned char *clp = NULL;
unsigned char *clp1;
int width,height;
unsigned char *bufRGB;
// parameters end


//全局函数
//进行YUV->RGB转换以及显示视频的函数

//进行YUV->RGB转换的函数
long int crv_tab[256];
long int cbu_tab[256];
long int cgu_tab[256];

long int cgv_tab[256];
long int tab_76309[256];

void init_dither_tab ()
{
  long int crv, cbu, cgu, cgv;
  int i;

  crv = 104597;
  cbu = 132201;                 /* fra matrise i global.h */
  cgu = 25675;
  cgv = 53279;

  for (i = 0; i < 256; i++)
  {
    crv_tab[i] = (i - 128) * crv;
    cbu_tab[i] = (i - 128) * cbu;
    cgu_tab[i] = (i - 128) * cgu;
    cgv_tab[i] = (i - 128) * cgv;
    tab_76309[i] = 76309 * (i - 16);
  }
  if (!(clp = (unsigned char *)malloc(sizeof(unsigned char)*1024)))
  {
    AfxMessageBox(L"安排解码码表内存失败!");
  }
  clp1 = clp;

  clp += 384;

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

void ConvertYUVtoRGB (unsigned char *src0, unsigned char *src1, unsigned char *src2,
                      unsigned char *dst_ori,int width,int height)

{

  int y11, y21;
  int y12, y22;
  int y13, y23;
  int y14, y24;
  int u, v;
  int i, j;
  int c11, c21, c31, c41;
  int c12, c22, c32, c42;
  unsigned int DW;
  unsigned int *id1, *id2;
  unsigned char *py1, *py2, *pu, *pv;
  unsigned char *d1, *d2;

  d1 = dst_ori;
  d1 += width * height * 3 - width * 3;
  d2 = d1 - width * 3;

  py1 = src0;
  pu = src1;
  pv = src2;
  py2 = py1 + width;

  id1 = (unsigned int *) d1;
  id2 = (unsigned int *) d2;

  for (j = 0; j < height; j += 2)
  {
    /* line j + 0 */
    for (i = 0; i < width; i += 4)
    {
      u = *pu++;
      v = *pv++;
      c11 = crv_tab[v];
      c21 = cgu_tab[u];
      c31 = cgv_tab[v];
      c41 = cbu_tab[u];
      u = *pu++;
      v = *pv++;
      c12 = crv_tab[v];
      c22 = cgu_tab[u];
      c32 = cgv_tab[v];
      c42 = cbu_tab[u];

      y11 = tab_76309[*py1++];  /* (255/219)*65536 */
      y12 = tab_76309[*py1++];
      y13 = tab_76309[*py1++];  /* (255/219)*65536 */
      y14 = tab_76309[*py1++];

      y21 = tab_76309[*py2++];
      y22 = tab_76309[*py2++];
      y23 = tab_76309[*py2++];
      y24 = tab_76309[*py2++];

      /* RGBR */
      DW = ((clp[(y11 + c41) >> 16])) |
        ((clp[(y11 - c21 - c31) >> 16]) << 8) |
        ((clp[(y11 + c11) >> 16]) << 16) |
        ((clp[(y12 + c41) >> 16]) << 24);
      *id1++ = DW;

      /* GBRG */
      DW = ((clp[(y12 - c21 - c31) >> 16])) |
        ((clp[(y12 + c11) >> 16]) << 8) |
        ((clp[(y13 + c42) >> 16]) << 16) |
        ((clp[(y13 - c22 - c32) >> 16]) << 24);
      *id1++ = DW;

      /* BRGB */
      DW = ((clp[(y13 + c12) >> 16])) |
        ((clp[(y14 + c42) >> 16]) << 8) |
        ((clp[(y14 - c22 - c32) >> 16]) << 16) |
        ((clp[(y14 + c12) >> 16]) << 24);
      *id1++ = DW;

      /* RGBR */
      DW = ((clp[(y21 + c41) >> 16])) |
        ((clp[(y21 - c21 - c31) >> 16]) << 8) |
        ((clp[(y21 + c11) >> 16]) << 16) |
        ((clp[(y22 + c41) >> 16]) << 24);
      *id2++ = DW;

      /* GBRG */
      DW = ((clp[(y22 - c21 - c31) >> 16])) |
        ((clp[(y22 + c11) >> 16]) << 8) |
        ((clp[(y23 + c42) >> 16]) << 16) |
        ((clp[(y23 - c22 - c32) >> 16]) << 24);
      *id2++ = DW;

      /* BRGB */
      DW = ((clp[(y23 + c12) >> 16])) |
        ((clp[(y24 + c42) >> 16]) << 8) |
        ((clp[(y24 - c22 - c32) >> 16]) << 16) |
        ((clp[(y24 + c12) >> 16]) << 24);
      *id2++ = DW;
    }
    id1 -= (9 * width) >> 2;
    id2 -= (9 * width) >> 2;
    py1 += width;
    py2 += width;
  }
}

//视频显示函数
int DisplayImage(unsigned char *lum, unsigned char *Cr, unsigned char *Cb)
{
  int errFlag = 0;

  src[0] = lum;
  src[1] = Cb;
  src[2] = Cr;

  //显示一帧画面
  if(PlayingVideo)
  {
	  ConvertYUVtoRGB (src[0],src[1],src[2],bufRGB,width,height);

	  free(src[0]);
	  free(src[1]);
	  free(src[2]);
  }
  else
  {
	  free(src[0]);
	  free(src[1]);
	  free(src[2]);
  }
  return errFlag;
}

void VideoDisplay(T264_t* t, T264_frame_t* f)
{
    uint8_t* p;
    unsigned char* buffer1, *buffer2, *buffer3, *Y, *U, *V;
    unsigned char *src[3];
    int32_t i;

    src[0] = Y = buffer1 = (unsigned char*)malloc(t->width*t->height*sizeof(char));
    p = f->Y[0];
    for (i = 0 ; i < t->height ; i++)
    {
        memcpy(buffer1, p, t->width);
        buffer1 += t->width;
        p += t->edged_stride;
    }

    src[2] = V = buffer2 = (unsigned char*)malloc((t->width*t->height*sizeof(char))>>2);
    p = f->V;
    for (i = 0 ; i < (t->height >> 1); i++)
    {
        memcpy(buffer2, p, t->width >> 1);
        buffer2 += (t->width >> 1);
        p += t->edged_stride_uv;
    }

    src[1] = U = buffer3 = (unsigned char*)malloc((t->width*t->height*sizeof(char))>>2);
    p = f->U;
    for (i = 0 ; i < (t->height >> 1); i++)
    {
        memcpy(buffer3, p, t->width >> 1);
        buffer3 += (t->width >> 1);
        p += t->edged_stride_uv;
    }
#ifndef __GCC__
    DisplayImage(Y,V,U);
#else
    dither(src);
    free(src[1]);
    free(src[2]);
    free(src[0]);
#endif
}

static float dec_psnr(uint8_t* p1, uint8_t* p2, int32_t width1, int32_t width2, int32_t height, int print)
{
	float sad = 0, psnr;
	int32_t i, j, size, ii, jj, diff, iii, jjj;
	uint8_t* p11, *p22;
	for(j=0; j<height; j+=16)
	{
		for (i = 0 ; i < width2 ; i +=16)
		{
			p11 = p1+i;
			p22 = p2+i;
			diff = 0;
			for(jj=0; jj<16; jj++)
			{
				for(ii=0; ii<16; ii++)
				{
					int32_t tmp;
					tmp = (p11[ii] - p22[ii]);
					sad += tmp * tmp;
					if(tmp != 0 && diff != 1)
					{
						if(print)
							printf("%2d != %2d ", p11[ii], p22[ii]);
						iii = ii;
						jjj = jj;
						diff = 1;
					}
				}
				p11 += width1;
				p22 += width2;
			}
			if(diff && print)
			{
				printf("%4d, %2d, %2d\n", (j)*(width2>>4) + (i), iii, jjj);
				print = 0;
			}
		}
		p1 += width1*16;
		p2 += width2*16;
	}
	size = width2 * height;
	if(sad < 1e-6)
		psnr = 0.0f;
	else
		psnr = (float)(10 * log10(65025.0f * size / sad));
	return psnr;
}

void write_frame(T264_t* t,T264_frame_t *frame,FILE *f_rec)
{
    int	i;
    uint8_t* p;

    if (f_rec)
    {
        p = frame->Y[0];
        for(i = 0 ; i < t->height ; i ++)
        {
            fwrite(p, t->width, 1, f_rec);
            p += t->edged_stride;
        }
        p = frame->U;
        for(i = 0 ; i < t->height >> 1 ; i ++)
        {
            fwrite(p, t->width >> 1, 1, f_rec);
            p += t->edged_stride_uv;
        }
        p = frame->V;
        for(i = 0 ; i < t->height >> 1 ; i ++)
        {
            fwrite(p, t->width >> 1, 1, f_rec);
            p += t->edged_stride_uv;
        }
    }
}



/////////////////////////////////////////////////////////////////////////////
// CH264DecodeDlg dialog

CH264DecodeDlg::CH264DecodeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CH264DecodeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CH264DecodeDlg)
		// 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 CH264DecodeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CH264DecodeDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CH264DecodeDlg, CDialog)
	//{{AFX_MSG_MAP(CH264DecodeDlg)
	ON_BN_CLICKED(IDC_DECODE, OnDecode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CH264DecodeDlg message handlers

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

	// 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

⌨️ 快捷键说明

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