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

📄 image.c

📁 包含了从MPEG4的视频解码到H.264的视频编码部分的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
  }

//  if (img->type == B_IMG)
//    Bframe_ctr++;

//  for (i = 0; i < img->currentSlice->max_part_nr; i++)
// ((img->currentSlice)->partArr[i]).bitstream = ((img->currentSlice)->partArr[i]).bitstream_frm;      //temp
  img->fld_flag = 0;
  code_a_picture(frame);

  if (input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->number != 0 && input->successive_Bframe != 0 && img->type != B_IMG)
  {
    copy_mv_to_or_from_save (TO_SAVE);
  }

  frame->bits_per_picture = 8 * ((((img->currentSlice)->partArr[0]).bitstream)->byte_pos);

  if (input->InterlaceCodingOption != FRAME_CODING)
    {
      find_distortion (snr, img);      
      frame->distortion_y = snr->snr_y;
      frame->distortion_u = snr->snr_u;
      frame->distortion_v = snr->snr_v;
    }
}


/*!
 ************************************************************************
 * \brief
 *    Encodes a field picture, consisting of top and bottom field
 ************************************************************************
 */
static void field_picture (Picture *top, Picture *bottom)
{
  stat->em_prev_bits_fld = 0;
  stat->em_prev_bits = &stat->em_prev_bits_fld;
  img->number *= 2;
  img->buf_cycle *= 2;
  input->no_multpred = 2 * input->no_multpred + 1;
  img->height = input->img_height / 2;
  img->height_cr = input->img_height / 4;
  img->fld_flag = 1;

  // Top field
  
//  img->bottom_field_flag = 0;
  put_buffer_top ();
  init_field ();
  if (img->type == B_IMG)       //all I- and P-frames
    nextP_tr_fld--;

  CopyTopFieldToOldImgOrgVariables (srcframe);

  if (img->type != INTRA_IMG && (input->WeightedPrediction == 1 || (input->WeightedBiprediction > 0 && (img->type == B_IMG || img->type == BS_IMG))))
  {
    estimate_weighting_factor ();
  }
  img->fld_flag = 1;
//  img->bottom_field_flag = 0;
 
  code_a_picture(top_pic);

  if (img->type != B_IMG)       //all I- and P-frames
    interpolate_frame_to_fb ();

  top->bits_per_picture = 8 * ((((img->currentSlice)->partArr[0]).bitstream)->byte_pos);

  //  Bottom field
//  img->bottom_field_flag = 0;
  put_buffer_bot ();
  img->number++;

  init_field ();

  if (img->type == B_IMG)       //all I- and P-frames
    nextP_tr_fld++;             //check once coding B field

  if (img->type == INTRA_IMG)
    img->type = INTER_IMG;

  CopyBottomFieldToOldImgOrgVariables (srcframe);

  if (img->type != INTRA_IMG && (input->WeightedPrediction == 1 || (input->WeightedBiprediction > 0 && (img->type == B_IMG || img->type == BS_IMG))))
  {
    estimate_weighting_factor ();
  }
  img->fld_flag = 1;
//  img->bottom_field_flag = 1;
  code_a_picture(bottom_pic);

  if (img->type != B_IMG)       //all I- and P-frames
    interpolate_frame_to_fb (); 

  bottom->bits_per_picture = 8 * ((((img->currentSlice)->partArr[0]).bitstream)->byte_pos);

  // the distortion for a field coded frame (consisting of top and bottom field)
  // lives in the top->distortion varaibles, thye bottom-> are dummies
  distortion_fld (&top->distortion_y, &top->distortion_u, &top->distortion_v);
}


/*!
 ************************************************************************
 * \brief
 *    Distortion Field
 ************************************************************************
 */
static void distortion_fld (float *dis_fld_y, float *dis_fld_u, float *dis_fld_v)
{

  img->number /= 2;
  img->buf_cycle /= 2;
  img->height = input->img_height;
  img->height_cr = input->img_height / 2;
  img->total_number_mb =
    (img->width * img->height) / (MB_BLOCK_SIZE * MB_BLOCK_SIZE);
  input->no_multpred = (input->no_multpred - 1) / 2;

  combine_field ();

  imgY = imgY_com;
  imgUV = imgUV_com;
  imgY_org = imgY_org_frm;
  imgUV_org = imgUV_org_frm;

  find_distortion (snr, img);   // find snr from original frame picture

  *dis_fld_y = snr->snr_y;
  *dis_fld_u = snr->snr_u;
  *dis_fld_v = snr->snr_v;
}


/*!
 ************************************************************************
 * \brief
 *    Picture Structure Decision
 ************************************************************************
 */
static int picture_structure_decision (Picture *frame, Picture *top, Picture *bot)
{
  double lambda_picture;
  int spframe = (img->type == INTER_IMG && img->types == SP_IMG);
  int bframe = (img->type == B_IMG);
  float snr_frame, snr_field;
  int bit_frame, bit_field;

  lambda_picture = 0.85 * pow (2, (img->qp - SHIFT_QP) / 3.0) * (bframe
                                                                 || spframe ?
                                                                 4 : 1);

  snr_frame = frame->distortion_y + frame->distortion_u + frame->distortion_v;
  //! all distrortions of a field picture are accumulated in the top field
  snr_field = top->distortion_y + top->distortion_u + top->distortion_v;
  bit_field = top->bits_per_picture + bot->bits_per_picture;
  bit_frame = frame->bits_per_picture;

  return decide_fld_frame (snr_frame, snr_field, bit_field, bit_frame, lambda_picture);
}


/*!
 ************************************************************************
 * \brief
 *    Field Mode Buffer
 ************************************************************************
 */
static void field_mode_buffer (int bit_field, float snr_field_y, float snr_field_u, float snr_field_v)
{
  put_buffer_frame ();
  imgY = imgY_com;
  imgUV = imgUV_com;

  if (img->type != B_IMG)       //all I- and P-frames 
    interpolate_frame_to_fb ();

  input->no_fields += 1;

  snr->snr_y = snr_field_y;
  snr->snr_u = snr_field_u;
  snr->snr_v = snr_field_v;
}


/*!
 ************************************************************************
 * \brief
 *    Frame Mode Buffer
 ************************************************************************
 */
static void frame_mode_buffer (int bit_frame, float snr_frame_y, float snr_frame_u, float snr_frame_v)
{
  put_buffer_frame ();

  if (img->type != B_IMG)       //all I- and P-frames
    interpolate_frame_to_fb ();

  if (input->InterlaceCodingOption != FRAME_CODING)
    {
      img->height = img->height / 2;
      img->height_cr = img->height_cr / 2;
      img->number *= 2;
      img->buf_cycle *= 2;

      put_buffer_top ();
      split_field_top ();

      if (img->type != B_IMG)   //all I- and P-frames
        {
          rotate_buffer ();
          interpolate_frame ();
        }
      img->number++;
      put_buffer_bot ();
      split_field_bot ();

      if (img->type != B_IMG)   //all I- and P-frames
        {
          rotate_buffer ();
          interpolate_frame ();
        }

      img->number /= 2;         // reset the img->number to field
      img->buf_cycle /= 2;
      img->height = input->img_height;
      img->height_cr = input->img_height / 2;
      img->total_number_mb =
        (img->width * img->height) / (MB_BLOCK_SIZE * MB_BLOCK_SIZE);

      snr->snr_y = snr_frame_y;
      snr->snr_u = snr_frame_u;
      snr->snr_v = snr_frame_v;
      put_buffer_frame ();

    }
}


/*!
 ************************************************************************
 * \brief
 *    mmco initializations should go here
 ************************************************************************
 */
static void init_mmco()
{
  img->mmco_buffer=NULL;
}


/*!
 ************************************************************************
 * \brief
 *    Initializes the parameters for a new frame
 ************************************************************************
 */
static void init_frame ()
{
  int i, j, k;
  int prevP_no, nextP_no;
  if (input->InterlaceCodingOption >= MB_CODING)
    img->pstruct = 3;
  else
    img->pstruct = 0;           //frame coding

  last_P_no = last_P_no_frm;

  img->current_mb_nr = 0;
  img->current_slice_nr = 0;
  stat->bit_slice = 0;

  img->mb_y = img->mb_x = 0;
  img->block_y = img->pix_y = img->pix_c_y = 0; 
  img->block_x = img->pix_x = img->block_c_x = img->pix_c_x = 0;

  // Tian Dong: June 7, 2002 JVT-B042
  // Initiate the actually number of ref frames that can be used.
  fb->num_short_used = fb->short_used;

  if (img->type != B_IMG)
  {
    img->refPicID_frm++;
    img->refPicID = img->refPicID_frm;
    
    img->tr = start_tr_in_this_IGOP + IMG_NUMBER * (input->jumpd + 1);
    
    img->imgtr_last_P_frm = img->imgtr_next_P_frm;
    img->imgtr_next_P_frm = img->tr;
    
#ifdef _ADAPT_LAST_GROUP_
    if (input->last_frame && img->number + 1 == input->no_frames)
      img->tr = input->last_frame;
#endif
    
    if (IMG_NUMBER != 0 && input->successive_Bframe != 0)     // B pictures to encode
      nextP_tr_frm = img->tr;
    
    if (img->type == INTRA_IMG)
      img->qp = input->qp0;   // set quant. parameter for I-frame
    else
    {
#ifdef _CHANGE_QP_
      if (input->qp2start > 0 && img->tr >= input->qp2start)
        img->qp = input->qpN2;
      else
#endif
        img->qp = input->qpN;
      
      if (img->types == SP_IMG)
      {
        img->qp = input->qpsp;
        img->qpsp = input->qpsp_pred;
      }
      
    }

    img->mb_y_intra = img->mb_y_upd;  //  img->mb_y_intra indicates which GOB to intra code for this frame
    
    if (input->intra_upd > 0) // if error robustness, find next GOB to update
    {
      img->mb_y_upd = (IMG_NUMBER / input->intra_upd) % (img->height / MB_BLOCK_SIZE);
    }
  }
  else
  {
    img->p_interval = input->jumpd + 1;
    prevP_no = start_tr_in_this_IGOP + (IMG_NUMBER - 1) * img->p_interval;
    nextP_no = start_tr_in_this_IGOP + (IMG_NUMBER) * img->p_interval;
    
#ifdef _ADAPT_LAST_GROUP_
    last_P_no[0] = prevP_no;
    for (i = 1; i < img->buf_cycle; i++)
      last_P_no[i] = last_P_no[i - 1] - img->p_interval;
    
    if (input->last_frame && img->number + 1 == input->no_frames)
    {
      nextP_no = input->last_frame;
      img->p_interval = nextP_no - prevP_no;
    }
#endif
    
    img->b_interval =
      (int) ((float) (input->jumpd + 1) / (input->successive_Bframe + 1.0) +
      0.49999);
    
    img->tr = prevP_no + img->b_interval * img->b_frame_to_code;      // from prev_P

    if (img->tr >= nextP_no)
      img->tr = nextP_no - 1;
    
#ifdef _CHANGE_QP_
    if (input->qp2start > 0 && img->tr >= input->qp2start)
      img->qp = input->qpB2;
    else
#endif
      img->qp = input->qpB;

    // initialize arrays
    for (k = 0; k < 2; k++)
      for (i = 0; i < img->height / BLOCK_SIZE; i++)
        for (j = 0; j < img->width / BLOCK_SIZE + 4; j++)
        {
          tmp_fwMV[k][i][j] = 0;
          tmp_bwMV[k][i][j] = 0;
          dfMV[k][i][j] = 0;
          dbMV[k][i][j] = 0;
        }
    for (i = 0; i < img->height / BLOCK_SIZE; i++)
      for (j = 0; j < img->width / BLOCK_SIZE; j++)
      {
        fw_refFrArr[i][j] = bw_refFrArr[i][j] = -1;
      }

  }
  
  UpdateSubseqInfo (img->layer);        // Tian Dong (Sept 2002)
  UpdateSceneInformation (0, 0, 0, -1); // JVT-D099, scene information SEI, nothing included by default

  //! Commented out by StW, needs fixing in SEI.h to keep the trace file clean
  //  PrepareAggregationSEIMessage ();

  // JVT-D097
  if (img->type != B_IMG && input->num_slice_groups_minus1 == 1 && input->FmoType > 3)
    {
      if (fmo_evlv_NewPeriod)
        FmoInitEvolvingMBAmap (input->FmoType, img->width / 16,
                               img->height / 16, MBAmap);

      FmoUpdateEvolvingMBAmap (input->FmoType, img->width / 16,
                               img->height / 16, MBAmap);
    }
  // End JVT-D097
  img->total_number_mb = (img->width * img->height) / (MB_BLOCK_SIZE * MB_BLOCK_SIZE);

  img->no_output_of_prior_pics_flag = 0;
  img->long_term_reference_flag = 0;

  init_mmco();
}

/*!
 ************************************************************************
 * \brief
 *    Initializes the parameters for a new field
 ************************************************************************
 */
static void init_field ()
{
  int i, j, k;
  int prevP_no, nextP_no;

  last_P_no = last_P_no_fld;

  //picture structure
  if (!img->fld_type)
    img->pstruct = 1;
  else
    img->pstruct = 2;

  img->current_mb_nr = 0;
  img->current_slice_nr = 0;
  stat->bit_slice = 0;

  input->jumpd *= 2;
  input->successive_Bframe *= 2;
  img->number /= 2;
  img->buf_cycle /= 2;

  img->mb_y = img->mb_x = 0;
  img->block_y = img->pix_y = img->pix_c_y = 0; // define vertical positions
  img->block_x = img->pix_x = img->block_c_x = img->pix_c_x = 0;        // define horizontal positions

  if (img->type != B_IMG)
    {

      if (!img->fld_type)
        img->refPicID_fld++;    //increment by 1 for field 1 only.
      img->refPicID = img->refPicID_fld;

      img->tr = img->number * (input->jumpd + 2) + img->fld_type;

      if (!img->fld_type)
        {
          img->imgtr_last_P_fld = img->imgtr_next_P_fld;
          img->imgtr_next_P_fld = img->tr;
        }

#ifdef _ADAPT_LAST_GROUP_
      if (input->last_frame && img->number + 1 == input->no_frames)
        img->tr = input->last_frame;
#endif
      if (img->number != 0 && input->successive_Bframe != 0)    // B pictures to encode
        nextP_tr_fld = img->tr;

      if (img->type == INTRA_IMG)
        img->qp = input->qp0;   // set quant. parameter for I-frame
      else
        {
#ifdef _CHANGE_QP_
          if (input->qp2start > 0 && img->tr >= input->qp2start)
            img->qp = input->qpN2;
          else
#endif
            img->qp = input->qpN;
          if (img->types == SP_IMG)
            {
              img->qp = input->qpsp;
              img->qpsp = input->qpsp_pred;
            }

        }

      img->mb_y_intra = img->mb_y_upd;  //  img->mb_y_intra indicates which GOB to intra code for this frame

      if (input->intra_upd > 0) // if error robustness, find next GOB to update
        {
          img->mb_y_upd =
            (img->number / input->intra_upd) % (img->width / MB_BLOCK_SIZE);
        }
    }
  else
    {
      img->p_interval = input->jumpd + 2;
      prevP_no = (img->number - 1) * img->p_interval + img->fld_type;
      nextP_no = img->number * img->p_interval + img->fld_type;
#ifdef _ADAPT_LAST_GROUP_
      if (!img->fld_type)       // top field
        {
          last_P_no[0] = prevP_no + 1;
          last_P_no[1] = prevP_no;
          for (i = 1; i <= img->buf_cycle; i++)
            {
              last_P_no[2 * i] = last_P_no[2 * i - 2] - img->p_interval;
              last_P_no[2 * i + 1] = last_P_no[2 * i - 1] - img->p_interval;
            }
        }
      else                      // bottom field
        {
          last_P_no[0] = nextP_no - 1;
          last_P_no[1] = prevP_no;
          for (i = 1; i <= img->buf_cycle; i++)
            {
              last_P_no[2 * i] = last_P_no[2 * i - 2] - img->p_interval;
              last_P_no[2 * i + 1] = last_P_no[2 * i - 1] - img->p_interval;

⌨️ 快捷键说明

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