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

📄 image.c

📁 jm61 的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:

      if (dc_ref[n] != 0)
        weight[n][0] =
          (int) (default_weight * (double) dc_org / (double) dc_ref[n] + 0.5);
      else
        weight[n][0] = 2*default_weight;  // only used when reference picture is black

	  printf("dc_org = %d, dc_ref = %d, weight[%d] = %d\n",dc_org, dc_ref[n],n,weight[n][0]);

      /* for now always use default weight for chroma weight */
      weight[n][1] = default_weight_chroma;
      weight[n][2] = default_weight_chroma;



  /* store weighted reference pic for motion estimation */
  for (i = 0; i < img->height * img->width; i++)
  {
    ref_pic_w[i] =
    Clip (0, 255, ((int) ref_pic[i] * weight[n][0] + wp_luma_round) / default_weight);
  }
  for (i = 0; i < 4*(img->height + 2*IMG_PAD_SIZE) ; i++)
  {
    for (j = 0; j< 4*(img->width + 2*IMG_PAD_SIZE); j++)
    {
       mref_w[n][i][j] =   Clip (0, 255, ((int) mref[n][i][j] * weight[n][0] + wp_luma_round) / default_weight);
     }
  }
}

  if (img->type == INTER_IMG)
  {
	  num_bwd_ref = 0;
	  num_fwd_ref = num_ref;
  }
  else
  {
    num_bwd_ref = (img->type == BS_IMG) ? num_ref : 1;
    num_fwd_ref = (img->type == BS_IMG) ? num_ref+1 : num_ref;
  }

//	printf("num_fwd_ref = %d num_bwd_ref = %d\n",num_fwd_ref,num_bwd_ref);

  {                             /* forward list */
    if (img->type == INTER_IMG && input->WeightedPrediction)
      {
        for (index = 0; index < num_ref; index++)
          {
            wp_weight[0][index][0] = weight[index][0];
            wp_weight[0][index][1] = weight[index][1];
            wp_weight[0][index][2] = weight[index][2];
  //              printf ("wp weight[%d] = %d  \n", index,
  //                      wp_weight[0][index][0]);
          }
      }
    else if (img->type == BS_IMG && (input->WeightedBiprediction == 1))
      {
        for (index = 0; index < num_ref; index++)
          {
            wp_weight[0][index][0] = weight[index][0];
            wp_weight[0][index][1] = weight[index][1];
            wp_weight[0][index][2] = weight[index][2];
          }
        for (index = 0; index < num_ref; index++)
          {                     /* backward list */
            if (index == 0)
              n = 1;
            else if (index == 1)
              n = 0;
            else
              n = index;
          }
      }
    else if (img->type == B_IMG && (input->WeightedBiprediction == 1))
      {
        for (index = 0; index < num_ref - 1; index++)
          {
            wp_weight[0][index][0] = weight[index + 1][0];
            wp_weight[0][index][1] = weight[index + 1][1];
            wp_weight[0][index][2] = weight[index + 1][2];
          }
        wp_weight[1][0][0] = weight[0][0];
        wp_weight[1][0][1] = weight[0][1];
        wp_weight[1][0][2] = weight[0][2];
        }
    else
        {
          for (index = 0; index < num_ref; index++)
            {
              wp_weight[0][index][0] = 1<<luma_log_weight_denom;
              wp_weight[0][index][1] = 1<<chroma_log_weight_denom;
              wp_weight[0][index][2] = 1<<chroma_log_weight_denom;
              wp_weight[1][index][0] = 1<<luma_log_weight_denom;
              wp_weight[1][index][1] = 1<<chroma_log_weight_denom;
              wp_weight[1][index][2] = 1<<chroma_log_weight_denom;
            }
        }




  if (input->WeightedBiprediction > 0 && (img->type == B_IMG || img->type == BS_IMG))
  {
    if (img->type == BS_IMG )
    {
      for (index = 0; index < num_fwd_ref; index++)
      {
        fwd_ref[index] = index;
      if (index == 0)
        n = 1;
      else if (index == 1)
        n = 0;
      else
        n = index;
      bwd_ref[index] = n;
	  }
    }
    else if (img->type == B_IMG)
    {
      for (index = 0; index < num_fwd_ref; index++)
      {
        fwd_ref[index] = index+1;
      }
      bwd_ref[0] = 0; // only one possible backwards ref for traditional B picture in current software
    }
  }      


  if (img->type == B_IMG || img->type == BS_IMG) // need to fill in wbp_weight values
  { 

    for (i = 0; i < num_fwd_ref; i++)
    {
      for (j = 0; j < num_bwd_ref; j++)
      {
        for (comp = 0; comp < 3; comp++)
        {
          log_weight_denom = (comp == 0) ? luma_log_weight_denom : chroma_log_weight_denom;
          if (input->WeightedBiprediction == 1)
          {
            wbp_weight[0][i][j][comp] = wp_weight[0][i][comp];
            wbp_weight[1][i][j][comp] = wp_weight[1][j][comp];
          }
          else if (input->WeightedBiprediction == 2)
          { // implicit mode
            pt = poc_distance (fwd_ref[i], bwd_ref[j]);
            p0 = poc_distance (fwd_ref[i], -1);
            if (pt == 0)
            {
              wbp_weight[1][i][j][comp] =  32 ;
              wbp_weight[0][i][j][comp] = 32;
            }	
            else
            {
			  x = (16384 + (pt>>1))/pt;
			  z = Clip(-1024, 1023, (x*p0 + 32 )>>6);
              wbp_weight[1][i][j][comp] = z>>2;
			  if (wbp_weight[1][i][j][comp] < -64 || wbp_weight[1][i][j][comp] >128)
				  wbp_weight[1][i][j][comp] = 32;
              wbp_weight[0][i][j][comp] = 64 - wbp_weight[1][i][j][comp];

              }
 //              if (comp == 0 )
  //              printf ("bpw weight[%d][%d] = %d  , %d \n", i, j,
  //                      wbp_weight[0][i][j][0], wbp_weight[1][i][j][0]);
           }
          }
          }
      }
    }
  }
}


/*!
 ************************************************************************
 * \brief
 *     Writes reconstructed image(s) to file
 *     This can be done more elegant!
 ************************************************************************
 */
static void write_reconstructed_image ()
{
    int i, j, k;
    int start = 0, inc = 1;

    if (p_dec != NULL)
      {
        if (img->type != B_IMG)
          {
            // write reconstructed image (IPPP)
            if (input->successive_Bframe == 0)
              {
                for (i = start; i < img->height; i += inc)
                  for (j = 0; j < img->width; j++)
                    fputc (imgY[i][j], p_dec);

                for (k = 0; k < 2; ++k)
                  for (i = start; i < img->height / 2; i += inc)
                    for (j = 0; j < img->width / 2; j++)
                      fputc (imgUV[k][i][j], p_dec);
              }

            // write reconstructed image (IBPBP) : only intra written
            else if (IMG_NUMBER == 0 && input->successive_Bframe != 0)
              {
                for (i = start; i < img->height; i += inc)
                  for (j = 0; j < img->width; j++)
                    fputc (imgY[i][j], p_dec);

                for (k = 0; k < 2; ++k)
                  for (i = start; i < img->height / 2; i += inc)
                    for (j = 0; j < img->width / 2; j++)
                      fputc (imgUV[k][i][j], p_dec);
              }

            // next P picture. This is saved with recon B picture after B picture coding
            if (IMG_NUMBER != 0 && input->successive_Bframe != 0)
              {
                for (i = start; i < img->height; i += inc)
                  for (j = 0; j < img->width; j++)
                    nextP_imgY[i][j] = imgY[i][j];
                for (k = 0; k < 2; ++k)
                  for (i = start; i < img->height / 2; i += inc)
                    for (j = 0; j < img->width / 2; j++)
                      nextP_imgUV[k][i][j] = imgUV[k][i][j];
              }
          }
        else
          {
            for (i = start; i < img->height; i += inc)
              for (j = 0; j < img->width; j++)
                fputc (imgY[i][j], p_dec);
            for (k = 0; k < 2; ++k)
              for (i = start; i < img->height / 2; i += inc)
                for (j = 0; j < img->width / 2; j++)
                  fputc (imgUV[k][i][j], p_dec);

            // If this is last B frame also store P frame
            if (img->b_frame_to_code == input->successive_Bframe)
              {
                // save P picture
                for (i = start; i < img->height; i += inc)
                  for (j = 0; j < img->width; j++)
                    fputc (nextP_imgY[i][j], p_dec);
                for (k = 0; k < 2; ++k)
                  for (i = start; i < img->height / 2; i += inc)
                    for (j = 0; j < img->width / 2; j++)
                      fputc (nextP_imgUV[k][i][j], p_dec);
              }
          }
      }
}

/*!
 ************************************************************************
 * \brief
 *    Choose interpolation method depending on MV-resolution
 ************************************************************************
 */
static void interpolate_frame_to_fb ()
{                             // write to mref[]
  add_frame (img);
  init_mref ();
  init_Refbuf (img);
  
  UnifiedOneForthPix (imgY, imgUV[0], imgUV[1], mref[0], mcef[0][0], mcef[0][1], Refbuf11[0]);
}

/*!
 ************************************************************************
 * \brief
 *    Choose interpolation method depending on MV-resolution
 ************************************************************************
 */
static void interpolate_frame ()
{                             // write to mref[]
  init_mref ();
  init_Refbuf (img);
  
  UnifiedOneForthPix (imgY, imgUV[0], imgUV[1],
    mref[0], mcef[0][0], mcef[0][1], Refbuf11[0]);
  
}

static void GenerateFullPelRepresentation (pel_t ** Fourthpel,
                                           pel_t * Fullpel, int xsize,
                                           int ysize)
{
  int x, y;
  
  for (y = 0; y < ysize; y++)
    for (x = 0; x < xsize; x++)
      PutPel_11 (Fullpel, y, x, FastPelY_14 (Fourthpel, y * 4, x * 4));
}


/*!
 ************************************************************************
 * \brief
 *    Upsample 4 times, store them in out4x.  Color is simply copied
 *
 * \par Input:
 *    srcy, srcu, srcv, out4y, out4u, out4v
 *
 * \par Side Effects_
 *    Uses (writes) img4Y_tmp.  This should be moved to a static variable
 *    in this module
 ************************************************************************/
static void UnifiedOneForthPix (pel_t ** imgY, pel_t ** imgU, pel_t ** imgV,
                                pel_t ** out4Y, pel_t ** outU, pel_t ** outV,
                                pel_t * ref11)
{
   int is;
    int i, j, j4;
    int ie2, je2, jj, maxy;


    for (j = -IMG_PAD_SIZE; j < img->height + IMG_PAD_SIZE; j++)
      {
        for (i = -IMG_PAD_SIZE; i < img->width + IMG_PAD_SIZE; i++)
          {
            jj = max (0, min (img->height - 1, j));
            is =
              (ONE_FOURTH_TAP[0][0] *
               (imgY[jj][max (0, min (img->width - 1, i))] +
                imgY[jj][max (0, min (img->width - 1, i + 1))]) +
               ONE_FOURTH_TAP[1][0] *
               (imgY[jj][max (0, min (img->width - 1, i - 1))] +
                imgY[jj][max (0, min (img->width - 1, i + 2))]) +
               ONE_FOURTH_TAP[2][0] *
               (imgY[jj][max (0, min (img->width - 1, i - 2))] +
                imgY[jj][max (0, min (img->width - 1, i + 3))]));
            img4Y_tmp[j + IMG_PAD_SIZE][(i + IMG_PAD_SIZE) * 2] = imgY[jj][max (0, min (img->width - 1, i))] * 1024;    // 1/1 pix pos
            img4Y_tmp[j + IMG_PAD_SIZE][(i + IMG_PAD_SIZE) * 2 + 1] = is * 32;  // 1/2 pix pos
          }
      }

    for (i = 0; i < (img->width + 2 * IMG_PAD_SIZE) * 2; i++)
      {
        for (j = 0; j < img->height + 2 * IMG_PAD_SIZE; j++)
          {
            j4 = j * 4;
            maxy = img->height + 2 * IMG_PAD_SIZE - 1;
            // change for TML4, use 6 TAP vertical filter
            is =
              (ONE_FOURTH_TAP[0][0] *
               (img4Y_tmp[j][i] + img4Y_tmp[min (maxy, j + 1)][i]) +
               ONE_FOURTH_TAP[1][0] * (img4Y_tmp[max (0, j - 1)][i] +
                                       img4Y_tmp[min (maxy, j + 2)][i]) +
               ONE_FOURTH_TAP[2][0] * (img4Y_tmp[max (0, j - 2)][i] +
                                       img4Y_tmp[min (maxy, j + 3)][i])) / 32;

            PutPel_14 (out4Y, (j - IMG_PAD_SIZE) * 4, (i - IMG_PAD_SIZE * 2) * 2, (pel_t) max (0, min (255, (int) ((img4Y_tmp[j][i] + 512) / 1024))));  // 1/2 pix
            PutPel_14 (out4Y, (j - IMG_PAD_SIZE) * 4 + 2, (i - IMG_PAD_SIZE * 2) * 2, (pel_t) max (0, min (255, (int) ((is + 512) / 1024))));   // 1/2 pix
          }
      }

    /* 1/4 pix */
    /* luma */
    ie2 = (img->width + 2 * IMG_PAD_SIZE - 1) * 4;
    je2 = (img->height + 2 * IMG_PAD_SIZE - 1) * 4;

    for (j = 0; j < je2 + 4; j += 2)
      for (i = 0; i < ie2 + 3; i += 2)
        {
          /*  '-'  */
          PutPel_14 (out4Y, j - IMG_PAD_SIZE * 4, i - IMG_PAD_SIZE * 4 + 1,
                     (pel_t) (max
                              (0,
                               min (255,
                                    (int) (FastPelY_14
                                           (out4Y, j - IMG_PAD_SIZE * 4,
                                            i - IMG_PAD_SIZE * 4) +
                                           FastPelY_14 (out4Y,
                                                        j - IMG_PAD_SIZE * 4,
                                                        min (ie2 + 2,
                                                             i + 2) -
                                                        IMG_PAD_SIZE * 4)+1) /
                                    2))));
        }

⌨️ 快捷键说明

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