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

📄 mpg2encdlg.cpp

📁 mpeg2视频编码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// mpg2encDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mpg2enc.h"
#include "mpg2encDlg.h"
#include "math.h"
#include "global.h"

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

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

/////////////////////////////////////////////////////////////////////////////
// CMpg2encDlg dialog
/////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////
void error(char *text)
{
	fprintf(stderr,text);
    putc('\n',stderr);
   MessageBox(NULL,text,NULL,MB_OK);
}
//////////////////////////////////////////////////////////////////////////
void init_fdct()
{
	int i, j;
    double s;

    for (i=0; i<8; i++)
	{
		s = (i==0) ? sqrt(0.125) : 0.5;

        for (j=0; j<8; j++)
        c[i][j] = s * cos((PI/8.0)*i*(j+0.5));
	}
}
//////////////////////////////////////////////////////////////////////////
void fdct(short *block)
{
  int i, j, k;
  double s;
  double tmp[64];

  for (i=0; i<8; i++)
    for (j=0; j<8; j++)
    {
      s = 0.0;

      for (k=0; k<8; k++)
        s += c[j][k] * block[8*i+k];

      tmp[8*i+j] = s;
    }

  for (j=0; j<8; j++)
    for (i=0; i<8; i++)
    {
      s = 0.0;

      for (k=0; k<8; k++)
        s += c[i][k] * tmp[8*k+j];

      block[8*i+j] = (int)floor(s+0.499999);
    }
}
///////////////////////////////////////////////////////////////////////
void init_idct()
{
   int i;
   iclp = iclip+512;
   for (i= -512; i<512; i++)
     iclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);
}
static void idctrow(short *blk)
{
  int x0, x1, x2, x3, x4, x5, x6, x7, x8;

  /* shortcut */
  if (!((x1 = blk[4]<<11) | (x2 = blk[6]) | (x3 = blk[2]) |
        (x4 = blk[1]) | (x5 = blk[7]) | (x6 = blk[5]) | (x7 = blk[3])))
  {
    blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
    return;
  }

  x0 = (blk[0]<<11) + 128; /* for proper rounding in the fourth stage */

  /* first stage */
  x8 = W7*(x4+x5);
  x4 = x8 + (W1-W7)*x4;
  x5 = x8 - (W1+W7)*x5;
  x8 = W3*(x6+x7);
  x6 = x8 - (W3-W5)*x6;
  x7 = x8 - (W3+W5)*x7;
  
  /* second stage */
  x8 = x0 + x1;
  x0 -= x1;
  x1 = W6*(x3+x2);
  x2 = x1 - (W2+W6)*x2;
  x3 = x1 + (W2-W6)*x3;
  x1 = x4 + x6;
  x4 -= x6;
  x6 = x5 + x7;
  x5 -= x7;
  
  /* third stage */
  x7 = x8 + x3;
  x8 -= x3;
  x3 = x0 + x2;
  x0 -= x2;
  x2 = (181*(x4+x5)+128)>>8;
  x4 = (181*(x4-x5)+128)>>8;
  
  /* fourth stage */
  blk[0] = (x7+x1)>>8;
  blk[1] = (x3+x2)>>8;
  blk[2] = (x0+x4)>>8;
  blk[3] = (x8+x6)>>8;
  blk[4] = (x8-x6)>>8;
  blk[5] = (x0-x4)>>8;
  blk[6] = (x3-x2)>>8;
  blk[7] = (x7-x1)>>8;
}

/* column (vertical) IDCT
 *
 *             7                         pi         1
 * dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
 *            l=0                        8          2
 *
 * where: c[0]    = 1/1024
 *        c[1..7] = (1/1024)*sqrt(2)
 */
static void idctcol(short *blk)
{
  int x0, x1, x2, x3, x4, x5, x6, x7, x8;

  /* shortcut */
  if (!((x1 = (blk[8*4]<<8)) | (x2 = blk[8*6]) | (x3 = blk[8*2]) |
        (x4 = blk[8*1]) | (x5 = blk[8*7]) | (x6 = blk[8*5]) | (x7 = blk[8*3])))
  {
    blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]=blk[8*6]=blk[8*7]=
      iclp[(blk[8*0]+32)>>6];
    return;
  }

  x0 = (blk[8*0]<<8) + 8192;

  /* first stage */
  x8 = W7*(x4+x5) + 4;
  x4 = (x8+(W1-W7)*x4)>>3;
  x5 = (x8-(W1+W7)*x5)>>3;
  x8 = W3*(x6+x7) + 4;
  x6 = (x8-(W3-W5)*x6)>>3;
  x7 = (x8-(W3+W5)*x7)>>3;
  
  /* second stage */
  x8 = x0 + x1;
  x0 -= x1;
  x1 = W6*(x3+x2) + 4;
  x2 = (x1-(W2+W6)*x2)>>3;
  x3 = (x1+(W2-W6)*x3)>>3;
  x1 = x4 + x6;
  x4 -= x6;
  x6 = x5 + x7;
  x5 -= x7;
  
  /* third stage */
  x7 = x8 + x3;
  x8 -= x3;
  x3 = x0 + x2;
  x0 -= x2;
  x2 = (181*(x4+x5)+128)>>8;
  x4 = (181*(x4-x5)+128)>>8;
  
  /* fourth stage */
  blk[8*0] = iclp[(x7+x1)>>14];
  blk[8*1] = iclp[(x3+x2)>>14];
  blk[8*2] = iclp[(x0+x4)>>14];
  blk[8*3] = iclp[(x8+x6)>>14];
  blk[8*4] = iclp[(x8-x6)>>14];
  blk[8*5] = iclp[(x0-x4)>>14];
  blk[8*6] = iclp[(x3-x2)>>14];
  blk[8*7] = iclp[(x7-x1)>>14];
}

/* two dimensional inverse discrete cosine transform */
void idct(short *block)
{
  int i;

  for (i=0; i<8; i++)
    idctrow(block+8*i);

  for (i=0; i<8; i++)
    idctcol(block+i);
}
//////////////////////////////////////////////////////////////////////
//initialize buffer, call once before first putbits or alignbits
void initbits()
{
  outcnt = 8;
  bytecnt = 0;
}
//write rightmost n (0<=n<=32) bits of val to outfile
void putbits(int val,int n)
{
  int i;
  unsigned int mask;

  mask = 1 << (n-1);//selects first (leftmost) bit

  for (i=0; i<n; i++)
  {
    outbfr <<= 1;

    if (val & mask)
      outbfr|= 1;

    mask >>= 1; /* select next bit */
    outcnt--;

    if (outcnt==0) /* 8 bit buffer full */
    {
      putc(outbfr,outfile);
      outcnt = 8;
      bytecnt++;
    }
  }
}
/* zero bit stuffing to next byte boundary (5.2.3, 6.2.1) */
void alignbits()
{
  if (outcnt!=8)
    putbits(0,outcnt);
}
/* return total number of generated bits */
int bitcount()
{
  return 8*bytecnt + (8-outcnt);
}
///////////////////////////////////////////////////////////////////
void range_checks()
{
  int i;
  /* range and value checks */

  if (horizontal_size<1 || horizontal_size>16383)
    error("horizontal_size must be between 1 and 16383");
  if (mpeg1 && horizontal_size>4095)
    error("horizontal_size must be less than 4096 (MPEG-1)");
  if ((horizontal_size&4095)==0)
    error("horizontal_size must not be a multiple of 4096");
  if (chroma_format!=CHROMA444 && horizontal_size%2 != 0)
    error("horizontal_size must be a even (4:2:0 / 4:2:2)");

  if (vertical_size<1 || vertical_size>16383)
    error("vertical_size must be between 1 and 16383");
  if (mpeg1 && vertical_size>4095)
    error("vertical size must be less than 4096 (MPEG-1)");
  if ((vertical_size&4095)==0)
    error("vertical_size must not be a multiple of 4096");
  if (chroma_format==CHROMA420 && vertical_size%2 != 0)
    error("vertical_size must be a even (4:2:0)");
  if(fieldpic)
  {
    if (vertical_size%2 != 0)
      error("vertical_size must be a even (field pictures)");
    if (chroma_format==CHROMA420 && vertical_size%4 != 0)
      error("vertical_size must be a multiple of 4 (4:2:0 field pictures)");
  }

  if (mpeg1)
  {
    if (aspectratio<1 || aspectratio>14)
      error("pel_aspect_ratio must be between 1 and 14 (MPEG-1)");
  }
  else
  {
    if (aspectratio<1 || aspectratio>4)
      error("aspect_ratio_information must be 1, 2, 3 or 4");
  }

  if (frame_rate_code<1 || frame_rate_code>8)
    error("frame_rate code must be between 1 and 8");

  if (bit_rate<=0.0)
    error("bit_rate must be positive");
  if (bit_rate > ((1<<30)-1)*400.0)
    error("bit_rate must be less than 429 Gbit/s");
  if (mpeg1 && bit_rate > ((1<<18)-1)*400.0)
    error("bit_rate must be less than 104 Mbit/s (MPEG-1)");

  if (vbv_buffer_size<1 || vbv_buffer_size>0x3ffff)
    error("vbv_buffer_size must be in range 1..(2^18-1)");
  if (mpeg1 && vbv_buffer_size>=1024)
    error("vbv_buffer_size must be less than 1024 (MPEG-1)");

  if (chroma_format<CHROMA420 || chroma_format>CHROMA444)
    error("chroma_format must be in range 1...3");

  if (video_format<0 || video_format>4)
    error("video_format must be in range 0...4");

  if (color_primaries<1 || color_primaries>7 || color_primaries==3)
    error("color_primaries must be in range 1...2 or 4...7");

  if (transfer_characteristics<1 || transfer_characteristics>7
      || transfer_characteristics==3)
    error("transfer_characteristics must be in range 1...2 or 4...7");

  if (matrix_coefficients<1 || matrix_coefficients>7 || matrix_coefficients==3)
    error("matrix_coefficients must be in range 1...2 or 4...7");

  if (display_horizontal_size<0 || display_horizontal_size>16383)
    error("display_horizontal_size must be in range 0...16383");
  if (display_vertical_size<0 || display_vertical_size>16383)
    error("display_vertical_size must be in range 0...16383");

  if (dc_prec<0 || dc_prec>3)
    error("intra_dc_precision must be in range 0...3");

  for (i=0; i<M; i++)
  {
    if (motion_data[i].forw_hor_f_code<1 || motion_data[i].forw_hor_f_code>9)
      error("f_code must be between 1 and 9");
    if (motion_data[i].forw_vert_f_code<1 || motion_data[i].forw_vert_f_code>9)
      error("f_code must be between 1 and 9");
    if (mpeg1 && motion_data[i].forw_hor_f_code>7)
      error("f_code must be le less than 8");
    if (mpeg1 && motion_data[i].forw_vert_f_code>7)
      error("f_code must be le less than 8");
    if (motion_data[i].sxf<=0)
      error("search window must be positive"); /* doesn't belong here */
    if (motion_data[i].syf<=0)
      error("search window must be positive");
    if (i!=0)
    {
      if (motion_data[i].back_hor_f_code<1 || motion_data[i].back_hor_f_code>9)
        error("f_code must be between 1 and 9");
      if (motion_data[i].back_vert_f_code<1 || motion_data[i].back_vert_f_code>9)
        error("f_code must be between 1 and 9");
      if (mpeg1 && motion_data[i].back_hor_f_code>7)
        error("f_code must be le less than 8");
      if (mpeg1 && motion_data[i].back_vert_f_code>7)
        error("f_code must be le less than 8");
      if (motion_data[i].sxb<=0)
        error("search window must be positive");
      if (motion_data[i].syb<=0)
        error("search window must be positive");
    }
  }
}
/////////////////////////////////////////////////////////////////////////////////////////////
void profile_and_level_checks()
{
  int i;
  struct level_limits *maxval;

  if (profile<0 || profile>15)
    error("profile must be between 0 and 15");

  if (level<0 || level>15)
    error("level must be between 0 and 15");

  if (profile>=8)
  {
    if (!quiet)
      fprintf(stderr,"Warning: profile uses a reserved value, conformance checks skipped\n");
    return;
  }

  if (profile<HP || profile>SP)
    error("undefined Profile");

  if (profile==SNR || profile==SPAT)
    error("This encoder currently generates no scalable bitstreams");

  if (level<HL || level>LL || level&1)
    error("undefined Level");

  maxval = &maxval_tab[(level-4) >> 1];

  /* check profile@level combination */
  if(!profile_level_defined[profile-1][(level-4) >> 1])
    error("undefined profile@level combination");
  

  /* profile (syntax) constraints */

  if (profile==SP && M!=1)
    error("Simple Profile does not allow B pictures");

⌨️ 快捷键说明

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