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

📄 image.c

📁 avs源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
      diff_u += img->quad[imgUV_org[0][j][i] - imgUV[0][j][i]];
      diff_v += img->quad[imgUV_org[1][j][i] - imgUV[1][j][i]];
    }
  }
  
  //  Collecting SNR statistics
  if (diff_y != 0)
  {
    snr->snr_y = (float) (10 * log10 (65025 * (float) impix / (float) diff_y));         // luma snr for current frame
    snr->snr_u = (float) (10 * log10 (65025 * (float) impix / (float) (4 * diff_u)));   // u croma snr for current frame, 1/4 of luma samples
    snr->snr_v = (float) (10 * log10 (65025 * (float) impix / (float) (4 * diff_v)));   // v croma snr for current frame, 1/4 of luma samples
  }
  
  if (img->number == 0)
  {
    snr->snr_y1 = (float) (10 * log10 (65025 * (float) impix / (float) diff_y));        // keep luma snr for first frame
    snr->snr_u1 = (float) (10 * log10 (65025 * (float) impix / (float) (4 * diff_u)));  // keep croma u snr for first frame
    snr->snr_v1 = (float) (10 * log10 (65025 * (float) impix / (float) (4 * diff_v)));  // keep croma v snr for first frame
    snr->snr_ya = snr->snr_y1;
    snr->snr_ua = snr->snr_u1;
    snr->snr_va = snr->snr_v1;
  }
  // B pictures
  else
  {
    snr->snr_ya = (float) (snr->snr_ya * (img->number + Bframe_ctr) + snr->snr_y) / (img->number + Bframe_ctr + 1); // average snr lume for all frames inc. first
    snr->snr_ua = (float) (snr->snr_ua * (img->number + Bframe_ctr) + snr->snr_u) / (img->number + Bframe_ctr + 1); // average snr u croma for all frames inc. first
    snr->snr_va = (float) (snr->snr_va * (img->number + Bframe_ctr) + snr->snr_v) / (img->number + Bframe_ctr + 1); // average snr v croma for all frames inc. first
  }
  
}

/*
*************************************************************************
* Function:Find distortion for all three components
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/
void find_distortion ()
{
  int i, j;
  int diff_y, diff_u, diff_v;
  int impix;
  
  //  Calculate  PSNR for Y, U and V.
  //     Luma.
  impix = img->height * img->width;
  
  diff_y = 0;
  for (i = 0; i < img->width; ++i)
  {
    for (j = 0; j < img->height; ++j)
    {
      diff_y += img->quad[abs (imgY_org[j][i] - imgY[j][i])];
    }
  }
  
  //     Chroma.
  diff_u = 0;
  diff_v = 0;
  
  for (i = 0; i < img->width_cr; i++)
  {
    for (j = 0; j < img->height_cr; j++)
    {
      diff_u += img->quad[abs (imgUV_org[0][j][i] - imgUV[0][j][i])];
      diff_v += img->quad[abs (imgUV_org[1][j][i] - imgUV[1][j][i])];
    }
  }
  
  // Calculate real PSNR at find_snr_avg()
  snr->snr_y = (float) diff_y;
  snr->snr_u = (float) diff_u;
  snr->snr_v = (float) diff_v;

}

/*!
 ************************************************************************
 * \brief
 *    RD decision of frame and field coding 
 ************************************************************************
 */
int decide_fld_frame(float snr_frame_Y, float snr_field_Y, int bit_field, int bit_frame, double lambda_picture)
{
  double cost_frame, cost_field;

  cost_frame = bit_frame * lambda_picture + snr_frame_Y;
  cost_field = bit_field * lambda_picture + snr_field_Y;

  if (cost_field > cost_frame)
    return (0);
  else
    return (1);
}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

static void ReportFirstframe(int tmp_time)
{
  int bits;

  FILE *file = fopen("stat.dat","at");
  
  fprintf(file,"\n -------------------- DEBUG_INFO_START -------------------- \n");
  
  fprintf (file,"%3d(I)  %8d %4d %7.4f %7.4f %7.4f  %5d          %3d      %3s\n",
    frame_no, stat->bit_ctr - stat->bit_ctr_n,
    img->qp, snr->snr_y, snr->snr_u, snr->snr_v, tmp_time,
    intras, img->picture_structure ? "FLD" : "FRM");
  
  fclose(file);
  
  printf ("%3d(I)  %8d %4d %7.4f %7.4f %7.4f  %5d       %s \n",
    frame_no, stat->bit_ctr - stat->bit_ctr_n,
    img->qp, snr->snr_y, snr->snr_u, snr->snr_v, tmp_time, img->picture_structure ? "FRM":"FLD" );
  
    //Rate control
  if(input->RCEnable)
  {
	  if(input->InterlaceCodingOption==0)
		  bits = stat->bit_ctr-stat->bit_ctr_n; // used for rate control update 
	  else
	  {
		  bits = stat->bit_ctr - Iprev_bits; // used for rate control update 
		  Iprev_bits = stat->bit_ctr;
	  }
  }

  stat->bitr0 = stat->bitr;
  stat->bit_ctr_0 = stat->bit_ctr;
  stat->bit_ctr = 0;

}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/
 
static void ReportIntra(int tmp_time)
{
  FILE *file = fopen("stat.dat","at");

  fprintf (file,"%3d(I)  %8d %4d %7.4f %7.4f %7.4f  %5d      \n",
    frame_no, stat->bit_ctr - stat->bit_ctr_n,
    img->qp, snr->snr_y, snr->snr_u, snr->snr_v, tmp_time );
  
  fclose(file);
  
  printf ("%3d(I)  %8d %4d %7.4f %7.4f %7.4f  %5d %3s\n",
    frame_no, stat->bit_ctr - stat->bit_ctr_n,
    img->qp, snr->snr_y, snr->snr_u, snr->snr_v, tmp_time, img->picture_structure ? "FRM":"FLD");

}
  
/*
*************************************************************************
* Function:
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

static void ReportB(int tmp_time)
{
  FILE *file = fopen("stat.dat","at");

  fprintf (file,"%3d(B)  %8d %4d %7.4f %7.4f %7.4f  %5d        \n",
    frame_no, stat->bit_ctr - stat->bit_ctr_n, img->qp,
    snr->snr_y, snr->snr_u, snr->snr_v, tmp_time);
  
  fclose(file);
  
  printf ("%3d(B)  %8d %4d %7.4f %7.4f %7.4f  %5d       %3s\n",
    frame_no, stat->bit_ctr - stat->bit_ctr_n, img->qp,
    snr->snr_y, snr->snr_u, snr->snr_v, tmp_time, img->picture_structure ? "FRM":"FLD");

}
  
/*
*************************************************************************
* Function:
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/
 
static void ReportP(int tmp_time)
{            
  FILE *file = fopen("stat.dat","at");
  char typ = (img->types==INTRA_IMG)?'I':'P';

  fprintf (file,"%3d(%c)  %8d %4d %7.4f %7.4f %7.4f  %5d        %3d\n",
    frame_no, typ, stat->bit_ctr - stat->bit_ctr_n, img->qp, snr->snr_y,
    snr->snr_u, snr->snr_v, tmp_time,
    intras);
  
  fclose(file);
  
  printf ("%3d(%c)  %8d %4d %7.4f %7.4f %7.4f  %5d       %3s     %3d    \n",
    frame_no, typ, stat->bit_ctr - stat->bit_ctr_n, img->qp, snr->snr_y,
    snr->snr_u, snr->snr_v, tmp_time,
     img->picture_structure ? "FRM":"FLD",intras);
  
}
  
/*
*************************************************************************
* Function:Copies contents of a Sourceframe structure into the old-style
*    variables imgY_org_frm and imgUV_org_frm.  No other side effects
* Input:  sf the source frame the frame is to be taken from
* Output:
* Return: 
* Attention:
*************************************************************************
*/

static void CopyFrameToOldImgOrgVariables ()
{
  int x, y;
  byte *u_buffer,*v_buffer;
  u_buffer=imgY_org_buffer+input->img_width*input->img_height;
  v_buffer=imgY_org_buffer+input->img_width*input->img_height*5/4;
  
  for (y=0; y<input->img_height; y++)
    for (x=0; x<input->img_width; x++)
      imgY_org_frm [y][x] = imgY_org_buffer[y*input->img_width+x];
	 
	for (y=0; y<input->img_height; y++)
    for (x=input->img_width; x<img->width; x++)
      imgY_org_frm [y][x] = imgY_org_frm [y][x-1];

  //padding bottom border
  for (y=input->img_height; y<img->height; y++)
    for (x=0; x<img->width; x++)
      imgY_org_frm [y][x] = imgY_org_frm [y-1][x];

  for (y=0; y<input->img_height/2; y++)
    for (x=0; x<input->img_width/2; x++)
    {
      imgUV_org_frm[0][y][x] = u_buffer[y*input->img_width/2+x];
      imgUV_org_frm[1][y][x] = v_buffer[y*input->img_width/2+x];
    }

    for (y=0; y<input->img_height/2; y++)
      for (x=input->img_width/2; x<img->width/2; x++)
      {
        imgUV_org_frm [0][y][x] = imgUV_org_frm [0][y][x-1];
        imgUV_org_frm [1][y][x] = imgUV_org_frm [1][y][x-1];
      }
     
    //padding bottom border
    for (y=input->img_width/2; y<img->width/2; y++)
      for (x=0; x<img->height/2; x++)
      {
        imgUV_org_frm [0][y][x] = imgUV_org_frm [0][y-1][x];
        imgUV_org_frm [1][y][x] = imgUV_org_frm [1][y-1][x];
      }
	if(input->InterlaceCodingOption != FRAME_CODING)
	{
		for (y=0; y<img->height; y += 2)
			for (x=0; x<img->width; x++)
			{
				imgY_org_top [y/2][x] = imgY_org_buffer[y*img->width    +x]; // !! Lum component for top field 
				imgY_org_bot [y/2][x] = imgY_org_buffer[(y+1)*img->width+x]; // !! Lum component for bot field
			}
				
		for (y=0; y<img->height/2; y += 2)
			for (x=0; x<img->width/2; x++)
			{
				imgUV_org_top[0][y/2][x] = u_buffer[y*img->width/2+x];    // !! Cr and Cb component for top field
				imgUV_org_top[1][y/2][x] = v_buffer[y*img->width/2+x];
				imgUV_org_bot[0][y/2][x] = u_buffer[(y+1)*img->width/2+x]; // !! Cr and Cb component for bot field
				imgUV_org_bot[1][y/2][x] = v_buffer[(y+1)*img->width/2+x];
			}
	}
}
  
/*
*************************************************************************
* Function: Calculates the absolute frame number in the source file out
        of various variables in img-> and input->
* Input:
* Output:
* Return: frame number in the file to be read
* Attention: \side effects
    global variable frame_no updated -- dunno, for what this one is necessary
*************************************************************************
*/


static int CalculateFrameNumber()
{
  if (img->type == B_IMG)
    frame_no = (IMG_NUMBER - 1) * (input->jumpd + 1) + img->b_interval * img->b_frame_to_code;
  else
  {
    frame_no = IMG_NUMBER * (input->jumpd + 1);
  }

  return frame_no;
}
  
/*
*************************************************************************
* Function:Reads one new frame from file
* Input: FrameNoInFile: Frame number in the source file
    HeaderSize: Number of bytes in the source file to be skipped
    xs: horizontal size of frame in pixels, must be divisible by 16
    ys: vertical size of frame in pixels, must be divisible by 16 or
        32 in case of MB-adaptive frame/field coding
    sf: Sourceframe structure to which the frame is written
* Output:
* Return: 
* Attention:
*************************************************************************
*/

static void ReadOneFrame (int FrameNoInFile, int HeaderSize, int xs, int ys)

{
  //const unsigned int  bytes_y = input->img_width *input->stuff_height; //modify by wuzhongmou 0610
  const unsigned int  bytes_y = input->img_width *input->img_height; //add by wuzhongmou 0610
  const unsigned int bytes_uv = bytes_y/4;
  const int framesize_in_bytes = bytes_y + 2*bytes_uv;
  //int stuff_height_cr = (input->img_height-input->stuff_height)/2;
  int off_y = input->img_width*input->img_height;
  int off_uv = off_y/4;
  
  assert (FrameNumberInFile == FrameNoInFile);
  
  if (fseek (p_in, framesize_in_bytes*FrameNoInFile + HeaderSize, SEEK_SET) != 0)
    error ("ReadOneFrame: cannot fseek to (Header size) in p_in", -1);
  
  if (fread (imgY_org_buffer, 1, bytes_y, p_in) != (unsigned )bytes_y)
  {
    printf ("ReadOneFrame: cannot read %d bytes from input file, unexpected EOF?, exiting", bytes_y);
    exit (-1);
  }



  if (fread (imgY_org_buffer + off_y, 1, bytes_uv, p_in) != (unsigned )bytes_uv)
  {
    printf ("ReadOneFrame: cannot read %d bytes from input file, unexpected EOF?, exiting", bytes_y);
    exit (-1);
  }


  if (fread (imgY_org_buffer + input->img_height*input->img_width + input->img_height*input->img_width/4, 1, bytes_uv, p_in) != (unsigned )bytes_uv)
  {
    printf ("ReadOneFrame: cannot read %d bytes from input file, unexpected EOF?, exiting", bytes_y);
    exit (-1);
  }


}
  
/*
*************************************************************************
* Function:point to frame coding variables 
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void put_buffer_frame()
{
	int i,j;

  imgY_org  = imgY_org_frm;
  imgUV_org = imgUV_org_frm;  
  tmp_mv    = tmp_mv_frm;

	//initialize ref index 1/4 pixel
	for(i=0;i<2;i++)
	{
		mref[i] = mref_frm[i];
	}
		
	//integer pixel for chroma
	for(i=0;i<2;i++)
	for(j=0;j<2;j++)
	{
		mcef[i][j]   = ref_frm[i][j+1];  
	}

	//integer pixel for luma
  for(i=0;i<2;i++)
  	Refbuf11[i] = &ref_frm[i][0][0][0];
		
	//current reconstructed image 
	
	imgY = imgY_frm = current_frame[0];
	imgUV = imgUV_frm =  &current_frame[1];
 	
  refFrArr    = refFrArr_frm;
  fw_refFrArr = fw_refFrArr_frm;
  bw_refFrArr = bw_refFrArr_frm;
}

/*
*************************************************************************
* Function:point to top field coding variables 
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/
void put_buffer_top()
{

⌨️ 快捷键说明

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