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

📄 image.c

📁 JM 11.0 KTA 2.1 Source Code
💻 C
📖 第 1 页 / 共 5 页
字号:
  //Rate control
  if(input->RCEnable) 
  {
    rc_update_pict(bits);

    // update the parameters of quadratic R-D model
    if((img->type==P_SLICE)&&(active_sps->frame_mbs_only_flag))
      updateRCModel();
    else if((img->type==P_SLICE) && (!active_sps->frame_mbs_only_flag) && (img->IFLAG==0))
      updateRCModel();
  }

  stats->bit_ctr_parametersets_n=0;

  if (IMG_NUMBER == 0)
    return 0;
  else
    return 1;
}


/*!
************************************************************************
* \brief
*    This function write out a picture
* \return
*    0 if OK,                                                         \n
*    1 in case of error
*
************************************************************************
*/
static int writeout_picture(Picture *pic)
{
  Bitstream *currStream;
  int partition, slice;
  Slice *currSlice;
#ifdef USE_POST_FILTER
  if(input->UsePostFilter)
    postProcessing(pic);
#endif
  img->currentPicture=pic;

  for (slice=0; slice<pic->no_slices; slice++)
  {
    currSlice = pic->slices[slice];
    for (partition=0; partition<currSlice->max_part_nr; partition++)
    {
      currStream = (currSlice->partArr[partition]).bitstream;
      assert (currStream->bits_to_go == 8);    //! should always be the case, the 
      //! byte alignment is done in terminate_slice

      // write only if the partition has content
      if (currSlice->partArr[partition].bitstream->write_flag )
        writeUnit (currStream,partition);
    }           // partition loop
  }           // slice loop

  return 0;
}


void copy_params(void)
{
  enc_picture->frame_mbs_only_flag = active_sps->frame_mbs_only_flag;
  enc_picture->frame_cropping_flag = active_sps->frame_cropping_flag;
  enc_picture->chroma_format_idc   = active_sps->chroma_format_idc;

  if (active_sps->frame_cropping_flag)
  {
    enc_picture->frame_cropping_rect_left_offset=active_sps->frame_cropping_rect_left_offset; 
    enc_picture->frame_cropping_rect_right_offset=active_sps->frame_cropping_rect_right_offset; 
    enc_picture->frame_cropping_rect_top_offset=active_sps->frame_cropping_rect_top_offset; 
    enc_picture->frame_cropping_rect_bottom_offset=active_sps->frame_cropping_rect_bottom_offset; 
  }
  else
  {
    enc_picture->frame_cropping_rect_left_offset=0; 
    enc_picture->frame_cropping_rect_right_offset=0; 
    enc_picture->frame_cropping_rect_top_offset=0; 
    enc_picture->frame_cropping_rect_bottom_offset=0; 
  }
}

/*!
************************************************************************
* \brief
*    Encodes a frame picture
************************************************************************
*/
void frame_picture (Picture *frame, int rd_pass)
{
  img->structure = FRAME;
  img->PicSizeInMbs = img->FrameSizeInMbs;

#ifdef SWITCHED_FILTERS
  if((rd_pass == 0) && (input->UseHPFilter == HPF_SIFO))
      img->filterParam = SIFO_FIRST_PASS;
#endif  // SWITCHED_FILTERS

  if (rd_pass == 2)
  {
#ifdef SWITCHED_FILTERS
    if((enc_frame_picture3) && (input->UseHPFilter == HPF_SIFO))
    {
      free_storable_picture(enc_frame_picture3); 
      free_slice_list(frame_pic_3);
    }
#endif  // SWITCHED_FILTERS

    enc_frame_picture3  = alloc_storable_picture ((PictureStructure)img->structure, img->width, img->height, img->width_cr, img->height_cr);
    img->ThisPOC=enc_frame_picture3->poc=img->framepoc;
    enc_frame_picture3->top_poc    = img->toppoc;
    enc_frame_picture3->bottom_poc = img->bottompoc;

    enc_frame_picture3->frame_poc = img->framepoc;

    enc_frame_picture3->pic_num = img->frame_num;
    enc_frame_picture3->frame_num = img->frame_num;
    enc_frame_picture3->coded_frame = 1;

    enc_frame_picture3->MbaffFrameFlag = img->MbaffFrameFlag = (input->MbInterlace != FRAME_CODING);

    enc_picture=enc_frame_picture3;
    copy_params();
  }
  else if (rd_pass == 1)
  {
#ifdef SWITCHED_FILTERS
    if((enc_frame_picture2) && (input->UseHPFilter == HPF_SIFO))
    {
      free_storable_picture(enc_frame_picture2); 
      free_slice_list(frame_pic_2);
    }
#endif  // SWITCHED_FILTERS

    enc_frame_picture2  = alloc_storable_picture ((PictureStructure)img->structure, img->width, img->height, img->width_cr, img->height_cr);
    img->ThisPOC=enc_frame_picture2->poc=img->framepoc;
    enc_frame_picture2->top_poc    = img->toppoc;
    enc_frame_picture2->bottom_poc = img->bottompoc;

    enc_frame_picture2->frame_poc = img->framepoc;

    enc_frame_picture2->pic_num = img->frame_num;
    enc_frame_picture2->frame_num = img->frame_num;
    enc_frame_picture2->coded_frame = 1;

    enc_frame_picture2->MbaffFrameFlag = img->MbaffFrameFlag = (input->MbInterlace != FRAME_CODING);

    enc_picture=enc_frame_picture2;
    copy_params();
  }
#ifdef ADAPTIVE_FILTER
  else if (rd_pass == 3)    // adaptive filter
  {
    enc_frame_picture_aif  = alloc_storable_picture ((PictureStructure)img->structure, img->width, img->height, img->width_cr, img->height_cr);
    img->ThisPOC=enc_frame_picture_aif->poc=img->framepoc;
    enc_frame_picture_aif->top_poc    = img->toppoc;
    enc_frame_picture_aif->bottom_poc = img->bottompoc;

    enc_frame_picture_aif->frame_poc = img->framepoc;

    enc_frame_picture_aif->pic_num = img->frame_num;
    enc_frame_picture_aif->frame_num = img->frame_num;
    enc_frame_picture_aif->coded_frame = 1;

    enc_frame_picture_aif->MbaffFrameFlag = img->MbaffFrameFlag = (input->MbInterlace != FRAME_CODING);

    enc_picture=enc_frame_picture_aif;
    copy_params();
  }
#endif
#ifdef ADAPTIVE_QUANTIZATION
  else if (rd_pass == 4)    // adaptive quantization matrix
  {
    enc_frame_picture_aqms  = alloc_storable_picture ((PictureStructure)img->structure, img->width, img->height, img->width_cr, img->height_cr);
    img->ThisPOC=enc_frame_picture_aqms->poc=img->framepoc;
    enc_frame_picture_aqms->top_poc    = img->toppoc;
    enc_frame_picture_aqms->bottom_poc = img->bottompoc;

    enc_frame_picture_aqms->frame_poc = img->framepoc;

    enc_frame_picture_aqms->pic_num = img->frame_num;
    enc_frame_picture_aqms->frame_num = img->frame_num;
    enc_frame_picture_aqms->coded_frame = 1;

    enc_frame_picture_aqms->MbaffFrameFlag = img->MbaffFrameFlag = (input->MbInterlace != FRAME_CODING);

    enc_picture=enc_frame_picture_aqms;
    copy_params();
  }
#endif
  else
  {
    enc_frame_picture  = alloc_storable_picture ((PictureStructure)img->structure, img->width, img->height, img->width_cr, img->height_cr);
    img->ThisPOC=enc_frame_picture->poc=img->framepoc;
    enc_frame_picture->top_poc    = img->toppoc;
    enc_frame_picture->bottom_poc = img->bottompoc;

    enc_frame_picture->frame_poc = img->framepoc;

    enc_frame_picture->pic_num = img->frame_num;
    enc_frame_picture->frame_num = img->frame_num;
    enc_frame_picture->coded_frame = 1;

    enc_frame_picture->MbaffFrameFlag = img->MbaffFrameFlag = (input->MbInterlace != FRAME_CODING);

    enc_picture=enc_frame_picture;
    copy_params();
  }


  stats->em_prev_bits_frm = 0;
  stats->em_prev_bits = &stats->em_prev_bits_frm;

  img->fld_flag = 0;

#ifdef ADAPTIVE_FD_SD_CODING
  img->APEC_in_FD_and_SD         = input->APEC_in_FD_and_SD;
#endif
  code_a_picture(frame);

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

  if (img->structure==FRAME)
  {
    find_distortion ();      
    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
************************************************************************
*/
void field_picture (Picture *top, Picture *bottom)
{
  //Rate control
  int old_pic_type;              // picture type of top field used for rate control    
  int TopFieldBits;

  //Rate control
  old_pic_type = img->type;

  stats->em_prev_bits_fld = 0;
  stats->em_prev_bits = &stats->em_prev_bits_fld;
  img->number *= 2;
  img->buf_cycle *= 2;
  img->height = (input->img_height+img->auto_crop_bottom) / 2; 
  img->height_cr = img->height_cr_frame / 2;
  img->fld_flag = 1;
  img->PicSizeInMbs = img->FrameSizeInMbs/2;
  // Top field

  enc_top_picture  = alloc_storable_picture ((PictureStructure)img->structure, img->width, img->height, img->width_cr, img->height_cr);
  enc_top_picture->poc=img->toppoc;
  enc_top_picture->frame_poc = img->toppoc;
  enc_top_picture->pic_num = img->frame_num;
  enc_top_picture->frame_num = img->frame_num;
  enc_top_picture->coded_frame = 0;
  enc_top_picture->MbaffFrameFlag = img->MbaffFrameFlag = FALSE;
  img->ThisPOC = img->toppoc;

  img->structure = TOP_FIELD;
  enc_picture = enc_top_picture;
  copy_params();

  put_buffer_top ();
  init_field ();
  if (img->type == B_SLICE)       //all I- and P-frames
    nextP_tr_fld--;


  img->fld_flag = 1;

  //Rate control
  if(input->RCEnable)
  {
    img->BasicUnit=input->basicunit;

    if(input->PicInterlace==FIELD_CODING)
      rc_init_pict(0,1,1); 
    else
      rc_init_pict(0,1,0);

    img->qp  = updateQuantizationParameter(1); 
  }
  img->TopFieldFlag=1;
#ifdef ADAPTIVE_FD_SD_CODING
  img->APEC_in_FD_and_SD         = input->APEC_in_FD_and_SD;
#endif
  code_a_picture(top_pic);
  enc_picture->structure = (PictureStructure)1;

  store_picture_in_dpb(enc_top_picture);

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

  //Rate control
  TopFieldBits=top->bits_per_picture;

  //  Bottom field
  enc_bottom_picture  = alloc_storable_picture ((PictureStructure)img->structure, img->width, img->height, img->width_cr, img->height_cr);
  enc_bottom_picture->poc=img->bottompoc;
  enc_bottom_picture->frame_poc = img->bottompoc;
  enc_bottom_picture->pic_num = img->frame_num;
  enc_bottom_picture->frame_num = img->frame_num;
  enc_bottom_picture->coded_frame = 0;
  enc_bottom_picture->MbaffFrameFlag = img->MbaffFrameFlag = FALSE;
  img->ThisPOC = img->bottompoc;
  img->structure = BOTTOM_FIELD;
  enc_picture = enc_bottom_picture;
  copy_params();
  put_buffer_bot ();
  img->number++;

  init_field ();

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

  if (img->type == I_SLICE && input->IntraBottom!=1)
    img->type = (input->BRefPictures == 2) ? B_SLICE : P_SLICE;

  img->fld_flag = 1;

  //Rate control
  if(input->RCEnable)
  {
    setbitscount(TopFieldBits);
    rc_init_pict(0,0,0); 
    img->qp  = updateQuantizationParameter(0); 
  }
  img->TopFieldFlag=0;
#ifdef ADAPTIVE_FD_SD_CODING
  img->APEC_in_FD_and_SD         = input->APEC_in_FD_and_SD;
#endif
  enc_picture->structure = (PictureStructure)2;
  code_a_picture(bottom_pic);

  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 variables, the 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->auto_crop_bottom);
  img->height_cr = img->height_cr_frame;

  combine_field ();

  imgY_org = imgY_org_frm;
  imgUV_org = imgUV_org_frm;

  find_distortion ();   // 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 bframe = (img->type == B_SLICE);
  float snr_frame, snr_field;
  int bit_frame, bit_field;

  lambda_picture = 0.68 * pow (2, img->bitdepth_lambda_scale + ((img->qp - SHIFT_QP) / 3.0)) * (bframe ? 1 : 1);

⌨️ 快捷键说明

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