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

📄 getpic.cpp

📁 mpeg2编码解码源程序代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
  /* get dct_type (frame DCT / field DCT) */  dct_type = (picture_structure==FRAME_PICTURE)             && (!frame_pred_frame_dct)             && (macroblock_type & (MACROBLOCK_PATTERN|MACROBLOCK_INTRA))             ? Get_Bits(1)             : 0;  /* return values */  *pmacroblock_type = macroblock_type;  *pstwtype = stwtype;  *pstwclass = stwclass;  *pmotion_type = motion_type;  *pmotion_vector_count = motion_vector_count;  *pmv_format = mv_format;  *pdmv = dmv;  *pmvscale = mvscale;  *pdct_type = dct_type;}/* move/add 8x8-Block from block[comp] to backward_reference_frame *//* copy reconstructed 8x8 block from block[comp] to current_frame[] * ISO/IEC 13818-2 section 7.6.8: Adding prediction and coefficient data * This stage also embodies some of the operations implied by: *   - ISO/IEC 13818-2 section 7.6.7: Combining predictions *   - ISO/IEC 13818-2 section 6.1.3: Macroblock*/static void Add_Block(int comp,int bx,int by,int dct_type,int addflag){  int cc,i, j, iincr;  unsigned char *rfp;  short *bp;    /* derive color component index */  /* equivalent to ISO/IEC 13818-2 Table 7-1 */  cc = (comp<4) ? 0 : (comp&1)+1; /* color component index */  if (cc==0)  {    /* luminance */    if (picture_structure==FRAME_PICTURE)      if (dct_type)      {        /* field DCT coding */        rfp = current_frame[0]              + Coded_Picture_Width*(by+((comp&2)>>1)) + bx + ((comp&1)<<3);        iincr = (Coded_Picture_Width<<1) - 8;      }      else      {        /* frame DCT coding */        rfp = current_frame[0]              + Coded_Picture_Width*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);        iincr = Coded_Picture_Width - 8;      }    else    {      /* field picture */      rfp = current_frame[0]            + (Coded_Picture_Width<<1)*(by+((comp&2)<<2)) + bx + ((comp&1)<<3);      iincr = (Coded_Picture_Width<<1) - 8;    }  }  else  {    /* chrominance */    /* scale coordinates */    if (chroma_format!=CHROMA444)      bx >>= 1;    if (chroma_format==CHROMA420)      by >>= 1;    if (picture_structure==FRAME_PICTURE)    {      if (dct_type && (chroma_format!=CHROMA420))      {        /* field DCT coding */        rfp = current_frame[cc]              + Chroma_Width*(by+((comp&2)>>1)) + bx + (comp&8);        iincr = (Chroma_Width<<1) - 8;      }      else      {        /* frame DCT coding */        rfp = current_frame[cc]              + Chroma_Width*(by+((comp&2)<<2)) + bx + (comp&8);        iincr = Chroma_Width - 8;      }    }    else    {      /* field picture */      rfp = current_frame[cc]            + (Chroma_Width<<1)*(by+((comp&2)<<2)) + bx + (comp&8);      iincr = (Chroma_Width<<1) - 8;    }  }  bp = ld->block[comp];  if (addflag)  {    for (i=0; i<8; i++)    {      for (j=0; j<8; j++)      {
		 if(*bp + 128<640&&*bp+128>-384)           *rfp = Clip[*bp++ + *rfp];
		 else
			bp++;
        rfp++;      }      rfp+= iincr;    }  }  else  {    for (i=0; i<8; i++)    {      for (j=0; j<8; j++)
	  {
		  if(*bp + 128<640&&*bp+128>-384)
			  *rfp++ = Clip[*bp++ + 128];
		  else
		  {
			  rfp++;
			  bp++;
		  }
	  }      rfp+= iincr;    }  }}/* ISO/IEC 13818-2 section 7.8 */static void Decode_SNR_Macroblock( int *SNRMBA,int  *SNRMBAinc,  int MBA,int  MBAmax,  int *dct_type){  int SNRmacroblock_type, SNRcoded_block_pattern, SNRdct_type, dummy;   int slice_vert_pos_ext, quantizer_scale_code, comp, code;  ld = &enhan;  if (*SNRMBAinc==0)  {    if (!SHOW_BITS(23)) /* next_start_code */    {      next_start_code();      code = SHOW_BITS(32);      if (code<SLICE_START_CODE_MIN || code>SLICE_START_CODE_MAX)      {        /* only slice headers are allowed in picture_data */       return;      }      Flush_Buffer32();      /* decode slice header (may change quantizer_scale) */      slice_vert_pos_ext = slice_header();      /* decode macroblock address increment */      *SNRMBAinc = Get_macroblock_address_increment();      /* set current location */      *SNRMBA =        ((slice_vert_pos_ext<<7) + (code&255) - 1)*mb_width + *SNRMBAinc - 1;      *SNRMBAinc = 1; /* first macroblock in slice: not skipped */    }    else /* not next_start_code */    {      if (*SNRMBA>=MBAmax)      {        return;      }      /* decode macroblock address increment */      *SNRMBAinc = Get_macroblock_address_increment();    }  }  if (*SNRMBA!=MBA)  {    /* streams out of sync */   return;  }  if (*SNRMBAinc==1) /* not skipped */  {    macroblock_modes(&SNRmacroblock_type, &dummy, &dummy,      &dummy, &dummy, &dummy, &dummy, &dummy,      &SNRdct_type);    if (SNRmacroblock_type & MACROBLOCK_PATTERN)      *dct_type = SNRdct_type;    if (SNRmacroblock_type & MACROBLOCK_QUANT)    {      quantizer_scale_code = Get_Bits(5);      ld->quantizer_scale =        ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1;    }    /* macroblock_pattern */    if (SNRmacroblock_type & MACROBLOCK_PATTERN)    {      SNRcoded_block_pattern = Get_coded_block_pattern();      if (chroma_format==CHROMA422)        SNRcoded_block_pattern = (SNRcoded_block_pattern<<2) | Get_Bits(2); /* coded_block_pattern_1 */      else if (chroma_format==CHROMA444)        SNRcoded_block_pattern = (SNRcoded_block_pattern<<6) | Get_Bits(6); /* coded_block_pattern_2 */    }    else      SNRcoded_block_pattern = 0;    /* decode blocks */    for (comp=0; comp<block_count; comp++)    {      Clear_Block(comp);      if (SNRcoded_block_pattern & (1<<(block_count-1-comp)))        Decode_MPEG2_Non_Intra_Block(comp);    }  }  else /* SNRMBAinc!=1: skipped macroblock */  {    for (comp=0; comp<block_count; comp++)      Clear_Block(comp);  }  ld = &base;}/* IMPLEMENTATION: set scratch pad macroblock to zero */static void Clear_Block(int comp){  short *Block_Ptr;  int i;  Block_Ptr = ld->block[comp];  for (i=0; i<64; i++)    *Block_Ptr++ = 0;}/* SCALABILITY: add SNR enhancement layer block data to base layer *//* ISO/IEC 13818-2 section 7.8.3.4: Addition of coefficients from the two layes */static void Sum_Block(int comp){  short *Block_Ptr1, *Block_Ptr2;  int i;  Block_Ptr1 = base.block[comp];  Block_Ptr2 = enhan.block[comp];  for (i=0; i<64; i++)    *Block_Ptr1++ += *Block_Ptr2++;}/* limit coefficients to -2048..2047 *//* ISO/IEC 13818-2 section 7.4.3 and 7.4.4: Saturation and Mismatch control */static void Saturate(short *Block_Ptr){  int i, sum, val;  sum = 0;  /* ISO/IEC 13818-2 section 7.4.3: Saturation */  for (i=0; i<64; i++)  {    val = Block_Ptr[i];    if (val>2047)      val = 2047;    else if (val<-2048)      val = -2048;    Block_Ptr[i] = val;    sum+= val;  }  /* ISO/IEC 13818-2 section 7.4.4: Mismatch control */  if ((sum&1)==0)    Block_Ptr[63]^= 1;}/* reuse old picture buffers as soon as they are no longer needed    based on life-time axioms of MPEG */static void Update_Picture_Buffers(){                             int cc;              /* color component index */  unsigned char *tmp;  /* temporary swap pointer */  for (cc=0; cc<3; cc++)  {    /* B pictures do not need to be save for future reference */    if (picture_coding_type==B_TYPE)    {      current_frame[cc] = auxframe[cc];    }    else    {      /* only update at the beginning of the coded frame */      if (!Second_Field)      {        tmp = forward_reference_frame[cc];
        /* the previously decoded reference frame is stored           coincident with the location where the backward            reference frame is stored (backwards prediction is not           needed in P pictures) */        forward_reference_frame[cc] = backward_reference_frame[cc];                /* update pointer for potential future B pictures */        backward_reference_frame[cc] = tmp;      }      /* can erase over old backward reference frame since it is not used         in a P picture, and since any subsequent B pictures will use the          previously decoded I or P frame as the backward_reference_frame */      current_frame[cc] = backward_reference_frame[cc];    }    /* IMPLEMENTATION:       one-time folding of a line offset into the pointer which stores the       memory address of the current frame saves offsets and conditional        branches throughout the remainder of the picture processing loop */    if (picture_structure==BOTTOM_FIELD)      current_frame[cc]+= (cc==0) ? Coded_Picture_Width : Chroma_Width;  }}/* store last frame */void Output_Last_Frame_of_Sequence(int Framenum,unsigned char* dst){
	memcpy(dst,backward_reference_frame[0],Coded_Picture_Width*Coded_Picture_Height);
	memcpy(dst+Coded_Picture_Width*Coded_Picture_Height,backward_reference_frame[1],Chroma_Width*Chroma_Height);
	memcpy(dst+Coded_Picture_Width*Coded_Picture_Height+Chroma_Width*Chroma_Height,backward_reference_frame[2],Chroma_Width*Chroma_Height);
}static void frame_reorder(int Bitstream_Framenum,int Sequence_Framenum){  /* tracking variables to insure proper output in spatial scalability */  static int Oldref_progressive_frame, Newref_progressive_frame;  if(Sequence_Framenum!=0)  {
    if (picture_structure==FRAME_PICTURE || Second_Field)    {      if (picture_coding_type==B_TYPE)	  {
		  
		  memcpy(Framedst,auxframe[0],horizontal_size*Coded_Picture_Height);
	      memcpy(Framedst+horizontal_size*Coded_Picture_Height,auxframe[1],horizontal_size*Coded_Picture_Height/4);
	      memcpy(Framedst+horizontal_size*Coded_Picture_Height+horizontal_size*Coded_Picture_Height/4,auxframe[2],horizontal_size*Coded_Picture_Height/4);

		//  Write_Frame(auxframe,Bitstream_Framenum-1);
	  }      else      {
		        Newref_progressive_frame = progressive_frame;        progressive_frame = Oldref_progressive_frame;

		 memcpy(Framedst,forward_reference_frame[0],horizontal_size*Coded_Picture_Height);
	     memcpy(Framedst+horizontal_size*Coded_Picture_Height,forward_reference_frame[1],horizontal_size*Coded_Picture_Height/4);
	     memcpy(Framedst+horizontal_size*Coded_Picture_Height+horizontal_size*Coded_Picture_Height/4,forward_reference_frame[2],horizontal_size*Coded_Picture_Height/4);

       // Write_Frame(forward_reference_frame,Bitstream_Framenum-1);
	        Oldref_progressive_frame = progressive_frame = Newref_progressive_frame;      }    }#ifdef DISPLAY    else if (Output_Type==T_X11)    {      if(!Display_Progressive_Flag)        Display_Second_Field();    }#endif  }  else   Oldref_progressive_frame = progressive_frame;
  

⌨️ 快捷键说明

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