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

📄 newmotiondlg.cpp

📁 机器视觉的程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//	 UpdateData(FALSE);
}

void CNewmotionDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	m_ActiveMovie.DestroyWindow();//CloseWindow();//
}

UINT CNewmotionDlg::GetBits(int N)
{
 UINT val = 0;
 int i = N;
 UINT j;

 // Optimize: we are on byte boundary and want to read multiple of bytes!
 if ((bitidx == 8) && ((N & 7) == 0))
 {
   i = N >> 3;
   while (i > 0)
   {
     val = (val << 8) | bfr[byteidx];
     byteidx++;
     totbits += 8;
     if (byteidx == bufcount)
     {
       if (bufcount == BUFFER_SIZE)
         ReFillBuffer();
       else
         eobs = TRUE;
       byteidx = 0;
     }
     i--;
   }
   return val;
 }

 while (i > 0)
 {
   j = Get1Bit();
   val = (val << 1) | j;
   i--;
 }
 return val;
}

UINT CNewmotionDlg::Get1Bit()
{
	UINT bit;
	
	if (eobs)
		return 0;
	
	bit = (bfr[byteidx] & masks[bitidx - 1]) >> (bitidx - 1);
	totbits++;
	bitidx--;
	if (!bitidx)
	{
		bitidx = 8;
		byteidx++;
		if (byteidx == bufcount)
		{
			if (bufcount == BUFFER_SIZE)
				ReFillBuffer();
			else
				eobs = TRUE;
			    byteidx = 0;
		}
	}
	return bit;
	
}

void CNewmotionDlg::InitGetBits()
{

	if(bitfile.Open(PathName,CFile::modeRead | CFile::shareDenyWrite,NULL)==NULL)
	{
		MessageBox("文件无法打开!");
		return;
	}
	byteidx = 0;
	bitidx = 8;
	totbits = 0;
	bufcount = 0;
	eobs = FALSE;
	if (!ReFillBuffer())
	{
		if (eobs)
		{
			printf("Unable to read from file %s.\r\n", bitfile);
			return;
		}
	}
}


int  CNewmotionDlg::Endbs()
{
	return eobs;
	
}
/*
unsigned long CNewmotionDlg::NextBits(int i)
{
  return LookAhead(i);
}
*/
UINT CNewmotionDlg::LookAhead(int N)
{
	DWORD val = 0;
	BYTE *tmp_bfr = bfr;
	BYTE tmp_bfr1[4];
	int j = N;
	int eo_bs = eobs;
	int buf_count = bufcount;
	int bit_idx = bitidx;
	int byte_idx = byteidx;
	
	while (j > 0)
	{
		if (eo_bs)
			return 0;
		val <<= 1;
		val |= (tmp_bfr[byte_idx] & masks[bit_idx - 1]) >> (bit_idx - 1);
		bit_idx--;
		j--;
		if (!bit_idx)
		{
			bit_idx = 8;
			byte_idx++;
			if (byte_idx == buf_count)
			{
				if (buf_count == BUFFER_SIZE)
				{
					if (bitfile.Read(tmp_bfr1, 4) != 4)
						eo_bs = TRUE;
					else
						tmp_bfr = &tmp_bfr1[0];
					if (bitfile.Seek( -4, CFile::current))
					{
						printf("Unable to set file position.\r\n");
						return(1) ;
					}
				}
				else
					eo_bs = TRUE;
				byte_idx = 0;
			}
		}
	}
	return val;
	
}

BOOL CNewmotionDlg::ReFillBuffer()
{
	int t;
	
	t = bitfile.ReadHuge(bfr, BUFFER_SIZE );
	if (t <= 0)
	{
		eobs = TRUE;
		return FALSE;
	}
	bufcount = t;
	return TRUE;
	
}


int CNewmotionDlg::SeekSync(UINT sync, int N)
{
  UINT val;
  UINT maxi = (int)pow(2.0, (DWORD)N) - 1;

  while (bitidx != 8)
    Get1Bit();

  val = GetBits(N);

  while ((val & maxi) != sync)
  {
    if (eobs)
      return 0;
    val <<= 8;
    val |= GetBits(8);
  }
  return 1;
}

int CNewmotionDlg::Next_Pic_Start_Code()
{
   if(SeekSync(FULL_PICTURE_START_CODE, 32))
	   return 1;
   else
	   return 0;
  
}
//DWORD CNewmotionDlg::Sequence_Header_Code()
void CNewmotionDlg::Sequence_Header_Code()
{
   if(SeekSync(FULL_SEQUENCE_HEADER_CODE, 32))
	   return ;//FULL_SEQUENCE_HEADER_CODE;
}

int CNewmotionDlg::Next_Start_Code()
{
	if(SeekSync(START_CODE_PREFIX , 24))
	   return 1;
	else
	   return 0;
}


int CNewmotionDlg::Get_macroblock_address_increment()
{
  int code, val;

  val = 0;

  // code= GetBits(11);
  while ((code = LookAhead(11))<24)
  {
    if (code!=15) /* if not macroblock_stuffing */
	{
      if (code==8) /* if macroblock_escape */
  
         val+= 33;
	  else 

	  { Fault_Flag = 1;
	    return 1;
	  }
    }
	else 
//		GetBits(11);
	GetBits(11);

  }

  /* macroblock_address_increment == 1 */
  /* ('1' is in the MSB position of the lookahead) */
   
   if (code>=1024)
  {
    GetBits(1);
    return val + 1;
  }

  /* codes 00010 ... 011xx */
   if (code>=128)
  {
    /* remove leading zeros */
    code >>= 6;
    GetBits(MBAtab1[code].len);
    
    return val + MBAtab1[code].val;
  }
  
  /* codes 00000011000 ... 0000111xxxx */
    code-= 24; /* remove common base */
    GetBits(MBAtab2[code].len);

  return val + MBAtab2[code].val;
}



int CNewmotionDlg::Get_macroblock_type()
{
  int code;

  if ((code = LookAhead(6))>=8)
  {
    code >>= 3;
    GetBits(PMBtab0[code].len);
    return PMBtab0[code].val;
  }

  if (code==0)
  {
    //if (!Quiet_Flag)
   //   printf("Invalid macroblock_type code\n");
   // Fault_Flag = 1;
    return 0;
  }

  	  GetBits(PMBtab1[code].len);
      return PMBtab1[code].val;

}


/*
BOOL CNewmotionDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	delete[] bfr;
	return CDialog::DestroyWindow();
}
*/
void CNewmotionDlg::FinishGetBits()
{
  if (bitfile.m_hFile)
  {
   bitfile.Close();
   bitfile.m_hFile=NULL;
  }
}

int CNewmotionDlg::decode_macroblock(int *macroblock_type, int *stwtype, int *stwclass,  int *motion_type, 
					  int *dct_type, int PMV[2][2][2],int dc_dct_pred[3],
					  int motion_vertical_field_select[2][2],int dmvector[2])

{
  /* locals */
  int quantizer_scale_code; 
//  int comp;

  int motion_vector_count; 
  int mv_format; 
  int dmv; 
  int mvscale;
  int coded_block_pattern;
 
 

  /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
  macroblock_modes(macroblock_type, stwtype, stwclass,
    motion_type, &motion_vector_count, &mv_format, &dmv, &mvscale,dct_type);

  if (Fault_Flag) return(0);  /* trigger: go to next slice */

  if (*macroblock_type & MACROBLOCK_QUANT)
  {
    quantizer_scale_code = GetBits(5);

	ld->quantizer_scale = quantizer_scale_code;
  }

  /* motion vectors */


  /* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */

  /* decode forward motion vectors */
  if ((*macroblock_type & MACROBLOCK_MOTION_FORWARD) 
    || ((*macroblock_type & MACROBLOCK_INTRA) 
    && concealment_motion_vectors))
  {
      motion_vector(PMV[0][0],dmvector,
      forward_f_code-1,forward_f_code-1,0,0,full_pel_forward_vector);
  }

  if (Fault_Flag) return(0);  /* trigger: go to next slice */

  /* decode backward motion vectors */
  if (*macroblock_type & MACROBLOCK_MOTION_BACKWARD)
  {
      motion_vector(PMV[0][1],dmvector,
        backward_f_code-1,backward_f_code-1,0,0,full_pel_backward_vector);
  }

  if (Fault_Flag) return(0);  /* trigger: go to next slice */

  if ((*macroblock_type & MACROBLOCK_INTRA) && concealment_motion_vectors)
       Get1Bit(); /* remove marker_bit */

  if (base.scalable_mode==SC_DP && base.priority_breakpoint==3)
    ld = &enhan;

  /* macroblock_pattern */
  /* ISO/IEC 13818-2 section 6.3.17.4: Coded block pattern */
  if (*macroblock_type & MACROBLOCK_PATTERN)
  {
    coded_block_pattern = Get_coded_block_pattern();

    if (chroma_format==CHROMA422)
    {
      /* coded_block_pattern_1 */
      coded_block_pattern = (coded_block_pattern<<2) | GetBits(2); 
/*
#ifdef TRACE
       if (Trace_Flag)
       {
         printf("coded_block_pattern_1: ");
         Print_Bits(coded_block_pattern,2,2);
         printf(" (%d)\n",coded_block_pattern&3);
       }
#endif /* TRACE */
     }
     else if (chroma_format==CHROMA444)
     {
      /* coded_block_pattern_2 */
      coded_block_pattern = (coded_block_pattern<<6) | GetBits(6); 
/*
#ifdef TRACE
      if (Trace_Flag)
      {
        printf("coded_block_pattern_2: ");
        Print_Bits(coded_block_pattern,6,6);
        printf(" (%d)\n",coded_block_pattern&63);
      }
#endif /* TRACE */
    }
  }
  else
    coded_block_pattern = (*macroblock_type & MACROBLOCK_INTRA) ? 
      (1<<block_count)-1 : 0;

  if (Fault_Flag) return(0);  /* trigger: go to next slice */

  /* decode blocks */
  for (int comp=0; comp<block_count; comp++)
  {
    /* SCALABILITY: Data Partitioning */
   if (base.scalable_mode==SC_DP)
    ld = &base;

    Clear_Block(comp);

    if (coded_block_pattern & (1<<(block_count-1-comp)))
    {
      if (*macroblock_type & MACROBLOCK_INTRA)
      {
        //if (ld->MPEG2_Flag)
        //  Decode_MPEG2_Intra_Block(comp,dc_dct_pred);
     //   else
          Decode_MPEG1_Intra_Block(comp,dc_dct_pred);
      }
      else
      {
       // if (ld->MPEG2_Flag)
         // Decode_MPEG2_Non_Intra_Block(comp);
        //else
          Decode_MPEG1_Non_Intra_Block(comp);
      }

      if (Fault_Flag) return(0);  // trigger: go to next slice 
    }
  }

  if(CodeType==D_TYPE)
  {
    /* remove end_of_macroblock (always 1, prevents startcode emulation) */
    /* ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
    //marker_bit("D picture end_of_macroblock bit");
    Get1Bit();	  
  }

  /* reset intra_dc predictors */
  /* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
  if (!(*macroblock_type & MACROBLOCK_INTRA))
    dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;

  /* reset motion vector predictors */
  if ((*macroblock_type & MACROBLOCK_INTRA) && !concealment_motion_vectors)
  {
    /* intra mb without concealment motion vectors */
    /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
    PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
    PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
  }

  /* special "No_MC" macroblock_type case */
  /* ISO/IEC 13818-2 section 7.6.3.5: Prediction in P pictures */
  if ((CodeType==P_TYPE) 
    && !(*macroblock_type & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_INTRA)))
  {
    /* non-intra mb without forward mv in a P picture */
    /* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
    PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;

    /* derive motion_type */
    /* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes, frame_motion_type */
    if (picture_structure==FRAME_PICTURE)
      *motion_type = MC_FRAME;
    else
    {
      *motion_type = MC_FIELD;
      /* predict from field of same parity */
      motion_vertical_field_select[0][0] = (picture_structure==BOTTOM_FIELD);
    }
  }

  if (*stwclass==4)
  {
    /* purely spatially predicted macroblock */
    /* ISO/IEC 13818-2 section 7.7.5.1: Resetting motion vector predictions */
    PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
    PMV[0][1][0]=PMV[0][1][1]=PMV[1][1][0]=PMV[1][1][1]=0;
  }

  /* successfully decoded macroblock */
  /* PMV_out[MB_Num][0]=PMV[0][0][0];
   PMV_out[MB_Num][1]=PMV[0][0][1];
   MB_Num++;*/

  return(1);

} /* decode_macroblock */


void CNewmotionDlg::motion_vector(int *PMV,int *dmvector,  int h_r_size,int v_r_size,int dmv,
								  int mvscale,int full_pel_vector)

{
  int motion_code, motion_residual;

/* horizontal component */
  /* ISO/IEC 13818-2 Table B-10 */
  motion_code = Get_motion_code();

  motion_residual = (h_r_size!=0 && motion_code!=0) ? GetBits(h_r_size) : 0;
/*
#ifdef TRACE
  if (Trace_Flag)
  {
    if (h_r_size!=0 && motion_code!=0)
    {
      printf("motion_residual (");
      Print_Bits(motion_residual,h_r_size,h_r_size);
      printf("): %d\n",motion_residual);
    }
  }
#endif /* TRACE */


  decode_motion_vector(&PMV[0],h_r_size,motion_code,motion_residual,full_pel_vector);

  if (dmv)
    dmvector[0] = Get_dmvector();


  /* vertical component */
  motion_code     = Get_motion_code();
  motion_residual = (v_r_size!=0 && motion_code!=0) ? GetBits(v_r_size) : 0;
/*
#ifdef TRACE
  if (Trace_Flag)
  {
    if (v_r_size!=0 && motion_code!=0)
    {
      printf("motion_residual (");
      Print_Bits(motion_residual,v_r_size,v_r_size);
      printf("): %d\n",motion_residual);
    }
  }
#endif /* TRACE */

  if (mvscale)
    PMV[1] >>= 1; /* DIV 2 */

  decode_motion_vector(&PMV[1],v_r_size,motion_code,motion_residual,full_pel_vector);

  if (mvscale)
    PMV[1] <<= 1;

  if (dmv)
    dmvector[1] = Get_dmvector();
/*
#ifdef TRACE
  if (Trace_Flag)
    printf("PMV = %d,%d\n",PMV[0],PMV[1]);
#endif /* TRACE */

⌨️ 快捷键说明

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