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

📄 image.c

📁 H.264编码解码器源码(c语言).zip
💻 C
📖 第 1 页 / 共 5 页
字号:
	}
	else
	{
		if (img->fld_flag)
		{
			store_picture_in_dpb(enc_top_picture);
			store_picture_in_dpb(enc_bottom_picture);
			enc_picture = enc_top_picture = enc_bottom_picture = NULL;
			if (enc_frame_picture)
			{
				free_storable_picture(enc_frame_picture);
				enc_frame_picture = NULL;
			}
		}
		else
		{
			store_picture_in_dpb(enc_frame_picture);
			enc_picture = enc_frame_picture = NULL;
			if (enc_top_picture)
			{
				free_storable_picture(enc_top_picture);
				enc_top_picture = NULL;
			}
			if (enc_bottom_picture)
			{
				free_storable_picture(enc_bottom_picture);
				enc_bottom_picture = NULL;
			}
		}
	}


#ifdef _LEAKYBUCKET_
  // Store bits used for this frame and increment counter of no. of coded frames
  Bit_Buffer[total_frame_buffer] = stat->bit_ctr - stat->bit_ctr_n;
  total_frame_buffer++;
#endif

  // POC200301: Verify that POC coding type 2 is not used if more than one consecutive 
  // non-reference frame is requested or if decoding order is different from output order
  if (img->pic_order_cnt_type == 2)
  {
    if (!img->nal_reference_idc) consecutive_non_reference_pictures++;
    else consecutive_non_reference_pictures = 0;

    if (frame_no < prev_frame_no || consecutive_non_reference_pictures>1)
      error("POC type 2 cannot be applied for the coding pattern where the encoding /decoding order of pictures are different from the output order.\n", -1);
    prev_frame_no = frame_no;
  }

  if (IMG_NUMBER == 0)
    ReportFirstframe(tmp_time);
  else
  {
    switch (img->type)
    {
    case I_SLICE:
      stat->bit_ctr_P += stat->bit_ctr - stat->bit_ctr_n;
      ReportIntra(tmp_time);
      break;
    case SP_SLICE:
      stat->bit_ctr_P += stat->bit_ctr - stat->bit_ctr_n;
      ReportSP(tmp_time);
      break;
    //case BS_IMG:
    //case BS_IMG:
    //  stat->bit_ctr_P += stat->bit_ctr - stat->bit_ctr_n;
     // ReportBS(tmp_time);
    //  break;
    case B_SLICE:
      stat->bit_ctr_B += stat->bit_ctr - stat->bit_ctr_n;
      if (img->nal_reference_idc>0)
        ReportBS(tmp_time);
      else
        ReportB(tmp_time);
      break;
    default:      // P, P_MULTPRED?
      stat->bit_ctr_P += stat->bit_ctr - stat->bit_ctr_n;
      ReportP(tmp_time);
    }
  }
  stat->bit_ctr_n = stat->bit_ctr;

  FreeSourceframe (srcframe);

  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;

  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
      writeUnit (currSlice->partArr[partition].bitstream);

    }           // partition loop
  }           // slice loop
  return 0;   
}


/*!
 ************************************************************************
 * \brief
 *    Encodes a frame picture
 ************************************************************************
 */
void frame_picture (Picture *frame)
{
  // if more than one B pictures, they will overwrite refFrArr_top, refFrArr_bot
  if (input->InterlaceCodingOption != FRAME_CODING && mb_adaptive && img->type == B_SLICE)
  {
    copy_mv_to_or_from_save (TO_SAVE);
  }

  img->structure = FRAME;
  enc_frame_picture  = alloc_storable_picture (img->structure, img->width, img->height, img->width_cr, img->height_cr);
  enc_frame_picture->poc=img->framepoc;
  enc_frame_picture->pic_num = img->frame_num;
  enc_frame_picture->coded_frame = 1;
  img->PicSizeInMbs = img->FrameSizeInMbs;

  enc_frame_picture->mb_adaptive_frame_field_flag = img->MbaffFrameFlag = (input->InterlaceCodingOption == MB_CODING);

  enc_picture=enc_frame_picture;


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

  if (img->MbaffFrameFlag)
  {
    CopyTopFieldToOldImgOrgVariables (srcframe);
    CopyBottomFieldToOldImgOrgVariables (srcframe);
  }

  if (img->type != I_SLICE && (input->WeightedPrediction == 1 || (input->WeightedBiprediction > 0 && (img->type == B_SLICE))))
  {
    estimate_weighting_factor ();
  }

  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_SLICE)
  {
    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
 ************************************************************************
 */
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->num_reference_frames = input->num_reference_frames;
  img->height = input->img_height / 2;
  img->height_cr = input->img_height / 4;
  img->fld_flag = 1;
  img->PicSizeInMbs = img->FrameSizeInMbs/2;
  // Top field
  
//  img->bottom_field_flag = 0;
  enc_top_picture  = alloc_storable_picture (img->structure, img->width, img->height, img->width_cr, img->height_cr);
  enc_top_picture->poc=img->toppoc;
  enc_top_picture->pic_num = img->frame_num;
  enc_top_picture->coded_frame = 1;
  enc_top_picture->mb_adaptive_frame_field_flag = img->MbaffFrameFlag = (input->InterlaceCodingOption == MB_CODING);
	img->ThisPOC = img->toppoc;
  enc_picture = enc_top_picture;
  put_buffer_top ();
  init_field ();
  if (img->type == B_SLICE)       //all I- and P-frames
    nextP_tr_fld--;

  CopyTopFieldToOldImgOrgVariables (srcframe);
 
	if (img->type != I_SLICE && (input->WeightedPrediction == 1 || (input->WeightedBiprediction > 0 && (img->type == B_SLICE))))  if (img->type == B_SLICE)
  {
    estimate_weighting_factor ();
  }
  img->fld_flag = 1;
//  img->bottom_field_flag = 0;
 
  code_a_picture(top_pic);
  enc_picture->structure = 1;
    
  //interpolate_frame_to_fb ();
  store_picture_in_dpb(enc_top_picture);

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

  //  Bottom field
//  img->bottom_field_flag = 0;
  enc_bottom_picture  = alloc_storable_picture (img->structure, img->width, img->height, img->width_cr, img->height_cr);
  enc_bottom_picture->poc=img->bottompoc;
  enc_bottom_picture->pic_num = img->frame_num;
  enc_bottom_picture->coded_frame = 1;
  enc_bottom_picture->mb_adaptive_frame_field_flag = img->MbaffFrameFlag = (input->InterlaceCodingOption == MB_CODING);
	img->ThisPOC = img->bottompoc;
  enc_picture = enc_bottom_picture;
  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)
    img->type = P_SLICE;

  CopyBottomFieldToOldImgOrgVariables (srcframe);
 
  if (img->type != I_SLICE && (input->WeightedPrediction == 1 || (input->WeightedBiprediction > 0 && (img->type == B_SLICE))))
  {
    estimate_weighting_factor ();
  }
  img->fld_flag = 1;
//  img->bottom_field_flag = 1;

  enc_picture->structure = 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 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);

  combine_field ();

  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 == SP_SLICE);
  int bframe = (img->type == B_SLICE);
  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_SLICE)       //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_SLICE)       //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 ();
     
    if (img->type != B_SLICE)   //all I- and P-frames
    {
      interpolate_frame ();
    }
    img->number++;
    put_buffer_bot ();
    
    if (img->type != B_SLICE)   //all I- and P-frames
    {
      interpolate_frame ();

⌨️ 快捷键说明

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