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

📄 image.c

📁 包含了从MPEG4的视频解码到H.264的视频编码部分的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
 ************************************************************************
 * \brief
 *    Extract bottom field from a frame
 ************************************************************************
 */
void split_field_bot(struct img_par *img)
{
  int i;

  for (i=0; i<img->height; i++)
  {
    memcpy(imgY[i], imgY_frm[i*2 + 1], img->width);
  }

  for (i=0; i<img->height_cr; i++)
  {
    memcpy(imgUV[0][i], imgUV_frm[0][i*2 + 1], img->width_cr);
    memcpy(imgUV[1][i], imgUV_frm[1][i*2 + 1], img->width_cr);
  }
}

/*!
 ************************************************************************
 * \brief
 *    Prepare field and frame buffer after field decoding
 ************************************************************************
 */
void field_postprocessing(struct img_par *img, struct inp_par *inp)
{
  int i;

  combine_field(img);

  if(img->type==I_SLICE || img->type == P_SLICE || img->type == SP_SLICE || img->type == SI_SLICE || !img->disposable_flag)
  {
    img->number++;
    imgY = imgY_bot;
    imgUV = imgUV_bot;
    copy2fb(img);  // bottom field
    img->number--;
  }
 
  fb = frm;
  mref = mref_frm;
  imgY = imgY_frm;
  imgUV = imgUV_frm;

  if((img->number>1)&&(img->type==I_SLICE || img->type == P_SLICE || img->type == SP_SLICE || img->type == SI_SLICE || !img->disposable_flag))
  {
    for (i = img->buf_cycle-1; i > 0; i--)
      last_P_no_frm[i] = last_P_no_frm[i-1];
    last_P_no_frm[0] = nextP_tr_frm;

    nextP_tr_frm = nextP_tr / 2;
    nextP_tr_fld = nextP_tr;
  }

  img->height *= 2;
  img->height_cr *= 2;
  img->buf_cycle /= 2;
  img->number /= 2;
  img->max_mb_nr = (img->width * img->height) / (MB_BLOCK_SIZE * MB_BLOCK_SIZE);
}

/*!
 ************************************************************************
 * \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);
  }
}

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

  if(img->type==I_SLICE || img->type == P_SLICE || img->type == SP_SLICE || img->type == SI_SLICE || !img->disposable_flag)
  {
    if (img->structure != FRAME)
    {
      for (i=0 ; i<img->width/4+4 ; i++)
      {
        for (j=0 ; j<img->height/8 ; j++)
        {
          img->mv_frm[i][2*j][0] = img->mv_frm[i][2*j+1][0] = img->mv_top[i][j][0];
          img->mv_frm[i][2*j][0] = img->mv_frm[i][2*j+1][0] = img->mv_top[i][j][0];
          img->mv_frm[i][2*j][1] = img->mv_frm[i][2*j+1][1] = img->mv_top[i][j][1]*2;
          img->mv_frm[i][2*j][1] = img->mv_frm[i][2*j+1][1] = img->mv_top[i][j][1]*2;

          if (i<img->width/4)
          {          
            moving_block_frm[2*j+1][i]=moving_block_frm[2*j][i]=
              ((refFrArr_top[j][i]!=0) || (refFrArr_bot[j][i]!=0) 
              || (abs(img->mv_top[i+4][j][0])>>1) || (abs(img->mv_top[i+4][j][1])>>1) 
              || (abs(img->mv_bot[i+4][j][0])>>1) || (abs(img->mv_bot[i+4][j][1])>>1));

            moving_block_top[j][i]=((refFrArr_top[j][i]!=0) 
              || (abs(img->mv_top[i+4][j][0])>>1) || (abs(img->mv_top[i+4][j][1])>>1));

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

          if (i<img->width/4)
          {
            moving_block_top[j][i]=moving_block_bot[j][i]=
              ((refFrArr_frm[2*j][i]!=0) || (refFrArr_frm[2*j + 1][i]!=0) 
              || (abs(img->mv_frm[i+4][2*j][0])>>1) || (abs(img->mv_frm[i+4][2*j][1])>>1) 
              || (abs(img->mv_frm[i+4][2*j+1][0])>>1) || (abs(img->mv_frm[i+4][2*j+1][1])>>1));
            
            moving_block_frm[2*j][i]=((refFrArr_frm[2*j][i]!=0) 
              || (abs(img->mv_frm[i+4][2*j][0])>>1) || (abs(img->mv_frm[i+4][2*j][1])>>1) );

            moving_block_frm[2*j+1][i]=((refFrArr_frm[2*j+1][i]!=0) 
              || (abs(img->mv_frm[i+4][2*j+1][0])>>1) || (abs(img->mv_frm[i+4][2*j+1][1])>>1));
          }

          if ((i%2 == 0) && (j%2 == 0) && (i<img->width/4))
          {
            if (refFrArr_frm[2*j][i] == -1)
            {
              refFrArr_top[j][i] = refFrArr_bot[j][i] = -1;
              refFrArr_top[j+1][i] = refFrArr_bot[j+1][i] = -1;
              refFrArr_top[j][i+1] = refFrArr_bot[j][i+1] = -1;
              refFrArr_top[j+1][i+1] = refFrArr_bot[j+1][i+1] = -1;
            }
            else
            {
              refFrArr_top[j][i] = refFrArr_bot[j][i] = refFrArr_frm[2*j][i]*2;
              refFrArr_top[j+1][i] = refFrArr_bot[j+1][i] = refFrArr_frm[2*j][i]*2;
              refFrArr_top[j][i+1] = refFrArr_bot[j][i+1] = refFrArr_frm[2*j][i]*2;
              refFrArr_top[j+1][i+1] = refFrArr_bot[j+1][i+1] = refFrArr_frm[2*j][i]*2;
            }
          }
        }
      }
    }
  }
}

void copy_stored_B_motion_info(struct img_par *img)
{
  int i,j;
  // copy motion information of stored B-picture for direct mode 
  if (img->structure != FRAME)
  {
    if (img->mb_frame_field_flag)
    {
      for (i=0 ; i<img->width/4+4 ; i++)
      {
        for (j=0 ; j<img->height/8 ; j++)
        {
          img->mv_top[i][j][0] = img->fw_mv_top[i][j][0];
          img->mv_top[i][j][1] = img->fw_mv_top[i][j][1];        
          img->mv_bot[i][j][0] = img->fw_mv_bot[i][j][0];
          img->mv_bot[i][j][1] = img->fw_mv_bot[i][j][1];        
          if (i<img->width/4)
          {
            refFrArr_top[j][i] = img->fw_refFrArr_top[j][i];
            refFrArr_bot[j][i] = img->fw_refFrArr_bot[j][i];
          }
        }
      }
    }
    else
    {
      for (i=0 ; i<img->width/4+4 ; i++)
      {
        for (j=0 ; j<img->height/8 ; j++)
        {
          img->mv_bot[i][j][0] = img->fw_mv[i][j][0];
          img->mv_bot[i][j][1] = img->fw_mv[i][j][1];        
          if (i<img->width/4)
          {
            refFrArr_bot[j][i] = img->fw_refFrArr[j][i];
          }
        }
      }
    }
  }
  else
  {
    for (i=0 ; i<img->width/4+4 ; i++)
    {
      for (j=0 ; j<img->height/4 ; j++)
      {
        img->mv_frm[i][j][0] = img->fw_mv[i][j][0];
        img->mv_frm[i][j][1] = img->fw_mv[i][j][1];        
        if (i<img->width/4)
        {
          refFrArr_frm[j][i] = img->fw_refFrArr[j][i];
        }
      }
    }
  }
}

void reset_wp_params(struct img_par *img)
{
  int i,comp;
  int log_weight_denom;

  for (i=0; i<MAX_REFERENCE_PICTURES; i++)
  {
    for (comp=0; comp<3; comp++)
    {
      log_weight_denom = (comp == 0) ? img->luma_log_weight_denom : img->chroma_log_weight_denom;
      img->wp_weight[0][i][comp] = 1<<log_weight_denom;
      img->wp_weight[1][i][comp] = 1<<log_weight_denom;
    }
  }
}


void fill_wp_params(struct img_par *img)
{
  int i, j, n;
  int comp;
  int log_weight_denom;
  int p0, pt;
//  int p1;
  int bframe = (img->type==B_SLICE);
  int fwd_ref[MAX_REFERENCE_PICTURES], bwd_ref[MAX_REFERENCE_PICTURES];
  int index;
  int max_bwd_ref, max_fwd_ref;
  int x,z;

  max_bwd_ref = img->num_ref_pic_active_bwd;
  max_fwd_ref = img->num_ref_pic_active_fwd;


if ((img->weighted_bipred_idc > 0) && (img->type == B_SLICE))
  {
    if (!img->disposable_flag )
    {
      for (index = 0; index < MAX_REFERENCE_PICTURES; index++)
      {
        fwd_ref[index] = index;
        if (index == 0)
          n = 1;
        else if (index == 1)
          n = 0;
        else
          n = index;
        bwd_ref[index] = n;
      }
    }
    else 
    {
       for (index = 0; index < MAX_REFERENCE_PICTURES - 1; index++)
       {
         fwd_ref[index] = index+1;
       }
       bwd_ref[0] = 0; // only one possible backwards ref for traditional B picture in current software
    }
  }      

  if (img->weighted_bipred_idc == 2 && bframe)
  {
    img->luma_log_weight_denom = 5;
    img->chroma_log_weight_denom = 5;
    img->wp_round_luma = 16;
    img->wp_round_chroma = 16;

    for (i=0; i<MAX_REFERENCE_PICTURES; i++)
    {
      for (comp=0; comp<3; comp++)
      {
        log_weight_denom = (comp == 0) ? img->luma_log_weight_denom : img->chroma_log_weight_denom;
        img->wp_weight[0][i][comp] = 1<<log_weight_denom;
        img->wp_weight[1][i][comp] = 1<<log_weight_denom;
      }
        }
  }

  if (bframe)
  {

    for (i=0; i<max_fwd_ref; i++)
    {
      for (j=0; j<max_bwd_ref; j++)
      {
        for (comp = 0; comp<3; comp++)
        {
          log_weight_denom = (comp == 0) ? img->luma_log_weight_denom : img->chroma_log_weight_denom;
          if (img->weighted_bipred_idc == 1)
          {
            img->wbp_weight[0][i][j][comp] =  img->wp_weight[0][i][comp];
            img->wbp_weight[1][i][j][comp] =  img->wp_weight[1][j][comp];
          }
          else if (img->weighted_bipred_idc == 2)
          {
            pt = poc_distance (fwd_ref[i], bwd_ref[j]);
            if (pt == 0)
            {
              img->wbp_weight[0][i][j][comp] =   32;
              img->wbp_weight[1][i][j][comp] =   32;
            }
            else
            {
              p0 = poc_distance (fwd_ref[i], -1);
 //             p1 = poc_distance (-1, bwd_ref[j]);

			  x = (16384 + (pt>>1))/pt;
			  z = Clip3(-1024, 1023, (x*p0 + 32 )>>6);
              img->wbp_weight[1][i][j][comp] = z >> 2;
              img->wbp_weight[0][i][j][comp] = 64 - img->wbp_weight[1][i][j][comp];
			  if (img->wbp_weight[1][i][j][comp] < -64 || img->wbp_weight[1][i][j][comp] > 128)
			  {
				  img->wbp_weight[1][i][j][comp] = 32;
				  img->wbp_weight[0][i][j][comp] = 32;
			  }


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

⌨️ 快捷键说明

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