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

📄 image.c

📁 avs-s最新代码,包括编码器和解码器源码
💻 C
📖 第 1 页 / 共 5 页
字号:
    
    fprintf(p_trace,"\n*********** Pic: %i (I/P) MB: %i Slice: %i Type %d **********\n", img->tr, img->current_mb_nr, img->current_slice_nr, img->type);
#endif
    
    // Initializes the current macroblock
    start_macroblock(img,inp);
	img->current_mb_nr_fld = img->current_mb_nr;
    // Get the syntax elements from the NAL
    read_one_macroblock(img,inp);
#ifdef ERROR_CONTROL
		//错误保护
		if( bBitstreamErr )
		{
			break;
		}
#endif	
    // decode one macroblock
    decode_one_macroblock(img,inp);

#ifdef ERROR_CONTROL
		//错误保护
		MBisProcessed[img->current_mb_nr] = 1;
#endif		
	
    img->current_mb_nr++;
#ifdef FLEXPICHEAD	
	last_okmb_pos = img->current_mb_nr;
#endif

  }

  free(Buf);
  DeblockFrame (img, imgY, imgUV);
	
}


void bot_field(struct img_par *img,struct inp_par *inp)
{
#ifdef ERROR_CONTROL
	//错误保护
	int tmp = 0;	
	int i = 0;
	int pixelX = 0, pixelY = 0;
	unsigned char MBisProcessed[MAX_MB_NR/2] = {0};
#endif	
  	unsigned char *Buf;
	int startcodepos,length; 
	const int mb_nr = img->current_mb_nr;
	int mb_width = img->width/16;
        Macroblock *currMB = &mb_data[mb_nr];
#ifdef FLEXPICHEAD
	int last_okmb_pos = 0;
	int last_errormb_pos = 0;
	int mb_height = img->height/16;
	
     if(part_first_slice_read_ok)
	 {
		 currStream->frame_bitoffset = currStream->frame_bitoffset;                 
		 CopyFromPreviousFrame(0, img->current_mb_nr-1);
	 }
	 else 
#endif
		 currStream->frame_bitoffset = 0;
         
	currentbitoffset = currStream->frame_bitoffset;   // jlzheng 6.30
    img->current_slice_nr = -1;    // jlzheng 6.30
	currentbitoffset = currStream->frame_bitoffset;    // jlzheng 6.30
//	currStream->frame_bitoffset = 0;   // jlzheng 6.30
	if ((Buf = (char*)calloc (MAX_CODED_FRAME_SIZE , sizeof(char))) == NULL) 
		no_mem_exit("GetAnnexbNALU: Buf");	 //jlzheng

	img->cod_counter=-1;
	
	
  while (img->current_mb_nr<img->PicSizeInMbs) // loop over macroblocks
  {
	  //decode slice header   jlzheng 6.30
	  if(slice_set_enable ||(!slice_set_enable&&img->current_mb_nr%mb_width ==0)) //added by mz, 2008.04
	  {
		  if(img->cod_counter<=0)
		  {
		#ifdef FLEXPICHEAD			
			  if(!part_first_slice_read_ok)
			  { 
		#endif
			  if(checkstartcode())
			  {
				  GetOneUnit(Buf,&startcodepos,&length);

#ifdef ERROR_CONTROL
					//错误保护
				  if (  ( Buf[startcodepos] == SEQUENCE_HEADER_CODE)
					  || ( Buf[startcodepos] == I_PICTURE_START_CODE)
					  || ( Buf[startcodepos] == PB_PICTURE_START_CODE ) )
				  {
					  prevPictureheaderloss = 1;
					  memcpy ( gBuf, Buf, length );
					  gbuflen = length;
					  gstartcodepos = startcodepos;
					  img->current_mb_nr = 0;	
		#ifdef FLEXPICHEAD						  
						  last_errormb_pos = img->PicSizeInMbs;
						  CopyFromPreviousFrame(last_okmb_pos, img->PicSizeInMbs-1);
		#endif
					  break;
				  }
		
				  //SliceHeader(Buf,startcodepos,length);
				  tmp = SliceHeader(Buf,startcodepos,length) - img->PicSizeInMbs;
		#ifdef FLEXPICHEAD
					  if(tmp>mb_width*mb_height || tmp < 0)
					  {
						  CopyFromPreviousFrame(last_okmb_pos, img->PicSizeInMbs-1);//需要处理缺帧头的情况
						  break;
					  }
		#endif
				  //if ( MBisProcessed[tmp] == 1 )
				  //if ( ( MBisProcessed[tmp] == 1 ) || ( tmp >= img->PicSizeInMbs ) )
				  if ( ( MBisProcessed[tmp] == 1 ) || ( tmp >= img->PicSizeInMbs ) || ( tmp < 0 ) )
				  {
         #ifdef FLEXPICHEAD
						  CopyFromPreviousFrame(last_okmb_pos, img->PicSizeInMbs-1);
         #endif
					  break;
				  }				  
				  				  			  
				  img->current_mb_nr = tmp;				  
				  //错误保护end
         #ifdef FLEXPICHEAD
					  if (img->current_mb_nr != last_okmb_pos)//有SLICE丢失
							CopyFromPreviousFrame(last_okmb_pos, img->current_mb_nr-1);
         #endif
#else
					SliceHeader(Buf,startcodepos,length);
#endif									  
				  img->current_slice_nr++;
				  img->cod_counter = -1; // Yulj 2004.07.15
			  }
		#ifdef FLEXPICHEAD
			  }
			   part_first_slice_read_ok = FALSE;
		#endif
		  }
	  }  //decode slice header 

    if(img->current_mb_nr == 0)
    img->current_slice_nr = 0;
	mb_data[img->current_mb_nr].slice_nr = img->current_slice_nr;   // jlzheng 6.30
	//added by mz, 2008.04
	if(slice_set_enable)
		mb_data[img->current_mb_nr].slice_set_index = img->current_slice_set_index;

#if TRACE
    // Here was the slice nr from the mb_data used.  This slice number is only set after 
    // the reconstruction of an MB and hence here not yet valid
	
    fprintf(p_trace,"\n*********** Pic: %i (I/P) MB: %i Slice: %i Type %d **********\n", img->tr, img->current_mb_nr, img->current_slice_nr, img->type);
#endif
    
    // Initializes the current macroblock
    start_macroblock(img,inp);
	img->current_mb_nr_fld = img->current_mb_nr + img->PicSizeInMbs;
    // Get the syntax elements from the NAL
    read_one_macroblock(img,inp);
    // decode one macroblock
    decode_one_macroblock(img,inp);

#ifdef ERROR_CONTROL
		//错误保护
		MBisProcessed[img->current_mb_nr] = 1;
#endif		
	
    img->current_mb_nr++;
#ifdef FLEXPICHEAD	
	last_okmb_pos = img->current_mb_nr;
#endif
 }

  free(Buf);
  DeblockFrame (img, imgY, imgUV);
	
}

/*!
************************************************************************
* \brief
*    Generate a frame from top and bottom fields
************************************************************************
*/
void combine_field(struct img_par *img)
{
  int i;
	
  for (i=0; i<img->height; i++)
  {
    memcpy(imgY_frm[i*2], imgY_top[i], img->width);     // top field
    memcpy(imgY_frm[i*2 + 1], imgY_bot[i], img->width); // bottom field
  }
	
  for (i=0; i<img->height_cr; i++)
  {
    memcpy(imgUV_frm[0][i*2], imgUV_top[0][i], img->width_cr);
    memcpy(imgUV_frm[0][i*2 + 1], imgUV_bot[0][i], img->width_cr);
    memcpy(imgUV_frm[1][i*2], imgUV_top[1][i], img->width_cr);
    memcpy(imgUV_frm[1][i*2 + 1], imgUV_bot[1][i], img->width_cr);
  }
}


/*
*************************************************************************
* Function:Prepare field and frame buffer after frame decoding
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void frame_postprocessing(struct img_par *img)
{
  if((img->number)&&(img->type==I_IMG || img->type == P_IMG))
  {
    nextP_tr_frm = nextP_tr;
  }

  //pic dist by Grandview Semi. @ [06-07-20 15:25]
  if (img->type==I_IMG || img->type == P_IMG) {
	img->PrevPicDistanceLsb = img->pic_distance;
	img->PicDistanceMsb = img->CurrPicDistanceMsb;
  }
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/
static void rotate_buffer ()
{
  Frame *f;
  
  f = fb->picbuf_short[1];
  fb->picbuf_short[1] = fb->picbuf_short[0];
  fb->picbuf_short[0] = f;
  
  mref[0] = fb->picbuf_short[0]->mref;
  mcef[0] = fb->picbuf_short[0]->mcef;
  mref[1] = fb->picbuf_short[1]->mref;
  mcef[1] = fb->picbuf_short[1]->mcef;
}


/*!
 ************************************************************************
 * \brief
 *    Store information for use in B picture
 ************************************************************************
 */
void store_field_MV(struct img_par *img)
{
  int i, j;

  if(img->type!=B_IMG)
  {
    if (img->picture_structure != FRAME)
    {
      for (i=0 ; i<img->width/8 ; i++)
      {
        for (j=0 ; j<img->height/16 ; j++)
        {
          img->mv_frm[i+4][2*j][0] = img->mv_frm[i+4][2*j+1][0] = img->mv_top[i+4][j][0];
          img->mv_frm[i+4][2*j][0] = img->mv_frm[i+4][2*j+1][0] = img->mv_top[i+4][j][0];
          img->mv_frm[i+4][2*j][1] = img->mv_frm[i+4][2*j+1][1] = img->mv_top[i+4][j][1]*2;
          img->mv_frm[i+4][2*j][1] = img->mv_frm[i+4][2*j+1][1] = img->mv_top[i+4][j][1]*2;
         

          if (refFrArr_top[j][i] == -1)
          {
            refFrArr_frm[2*j][i] = refFrArr_frm[2*j+1][i] = -1;
          }
          else
          {
            refFrArr_frm[2*j][i] = refFrArr_frm[2*j+1][i] = (int)(refFrArr_top[j][i]/2);
          }
        }
      }
    }
    else
    {
      for (i=0 ; i<img->width/8; i++)
      {
        for (j=0 ; j<img->height/16 ; j++)
        {
          img->mv_top[i+4][j][0] = img->mv_bot[i+4][j][0] = (int)(img->mv_frm[i+4][2*j][0]);
          img->mv_top[i+4][j][1] = img->mv_bot[i+4][j][1] = (int)((img->mv_frm[i+4][2*j][1])/2);

            if (refFrArr_frm[2*j][i] == -1)
            {
              refFrArr_top[j][i] = refFrArr_bot[j][i] = -1;
            }
            else
            {
             // refFrArr_top[j][i] = refFrArr_bot[j][i] = refFrArr_frm[2*j][i]*2;
				 refFrArr_top[j][i] = refFrArr_frm[2*j][i]*2;
              refFrArr_bot[j][i] = refFrArr_frm[2*j][i]*2 + 1;
                     //by oliver 0512
            }
        }
      }
    }
  }
}


#ifdef FLEXPICHEAD
//对差错宏块直接从前一帧的相同位置拷贝
void CopyFromPreviousFrame(int startmbpos, int endmbpos)
{
	int tmp_block[8][8];
	int block8x8;
	int i, j, mv_mul, i4, j4;
	int currmbpos, mbx, mby;
	int vec1_x, vec1_y;
	int ioff, joff, ii, jj;
	int b8_x, b8_y;
 	int xx,yy,uv,jf,if1;
	int i1,j1, f1,f2, f3, f4;
	int ii0, ii1, jj0, jj1, jf0,if0, jf1;	

	for (currmbpos = startmbpos; currmbpos < endmbpos+1; currmbpos ++)
	{
		mbx=(currmbpos)%(img->width/MB_BLOCK_SIZE);
        mby=(currmbpos)/(img->width/MB_BLOCK_SIZE);

		  mv_mul=4;
		  f1=8;
		  f2=7;
  
		  f3=f1*f1;//64
		  f4=f3/2; //32

		img->block_x = mbx * BLOCK_SIZE/2;
		img->block_y = mby * BLOCK_SIZE/2;
		img->pix_x   = mbx * MB_BLOCK_SIZE;   
		img->pix_y   = mby * MB_BLOCK_SIZE;   
		img->pix_c_x = mbx * MB_BLOCK_SIZE/2;
		img->pix_c_y = mby * MB_BLOCK_SIZE/2;
		
		for (block8x8=0; block8x8<4; block8x8++)
		{
			i = block8x8%2;
			j = block8x8/2;

			ioff=i*8;			
			joff=j*8;
			i4=img->block_x+i;
		    j4=img->block_y+j;
			
			vec1_x = i4*8*mv_mul;// + mv_array[i4+BLOCK_SIZE][j4][0];
			vec1_y = j4*8*mv_mul;//+ mv_array[i4+BLOCK_SIZE][j4][1];

			get_block(0, vec1_x, vec1_y, img, tmp_block,mref[0]);

			for(ii=0;ii<8;ii++)
			   for(jj=0;jj<8;jj++)  
			       img->mpr[ii+ioff][jj+joff] = tmp_block[ii][jj];

			b8_y = (block8x8 / 2) << 3;
            b8_x = (block8x8 % 2) << 3;
    
		    for(yy=0;yy<8;yy++)
		    	for(xx=0;xx<8;xx++)
				{
				  img->m7[xx][yy] = clamp(img->mpr[b8_x+xx][b8_y+yy],0,255);
				  imgY[img->pix_y+b8_y+yy][img->pix_x+b8_x+xx] = (unsigned char)img->m7[xx][yy];	  
				}
		}

		for(uv=0;uv<2;uv++)
		{						
		   for (j=4;j<6;j++)
			{
			  joff=(j-4)*4;
			  j4=img->pix_c_y+joff;
					
			  for(i=0;i<2;i++)
			  {
				ioff=i*4;
				i4=img->pix_c_x+ioff;
				for(jj=0;jj<4;jj++)
				{
					jf=(j4+jj)/4;
					for(ii=0;ii<4;ii++)
					{
						if1=(i4+ii)/4;

						i1=(img->pix_c_x+ii+ioff)*f1;
					    j1=(img->pix_c_y+jj+joff)*f1;
       							
					    ii0=max (0, min (i1/f1, img->width_cr-1));
					    jj0=max (0, min (j1/f1, img->height_cr-1));
					    ii1=max (0, min ((i1+f2)/f1, img->width_cr-1));
					    jj1=max (0, min ((j1+f2)/f1, img->height_cr-1));				
              
					    if1=(i1 & f2);
					    jf1=(j1 & f2);
					    if0=f1-if1;
				        jf0=f1-jf1;

						img->mpr[ii+ioff][jj+joff]=(if0*jf0*mcef[0][uv][jj0][ii0]+
					    if1*jf0*mcef[0][uv][jj0][ii1]+
					    if0*jf1*mcef[0][uv][jj1][ii0]+
					    if1*jf1*mcef[0][uv][jj1][ii1]+f4)/f3;
					}
				}
        

			  }
		   }

           for(yy=0;yy<8;yy++)
			 for(xx=0;xx<8;xx++)
			    imgUV[uv][img->pix_c_y+yy][img->pix_c_x+xx] = (unsigned char)(clamp(img->mpr[xx][yy],0,255));
		}
	}
}


#endif

⌨️ 快捷键说明

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