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

📄 image.c

📁 G729、h263、h264、MPEG4四种最流行的音频和视频标准的压缩和解压算法的源代码.rar
💻 C
📖 第 1 页 / 共 5 页
字号:
          img->qp+= (1 + img->rd_pass);
        
        if (img->type == P_SLICE && input->GenerateMultiplePPS && (intras * 100 )/img->FrameSizeInMbs >=80)
        {
          img->type=I_SLICE;
          active_pps = &PicParSet[0];
        }

        img->write_macroblock = 0;
        
        frame_picture (frame_pic3,2);

        if (img->rd_pass==0)
          img->rd_pass=2*picture_coding_decision(frame_pic, frame_pic3, rd_qp);
        else
          img->rd_pass+=picture_coding_decision(frame_pic2, frame_pic3, rd_qp);

        //update_rd_picture_contexts (img->rd_pass); 
        if (img->rd_pass==0)
        {
          enc_picture=enc_frame_picture;
          if (input->GenerateMultiplePPS)
            active_pps = &PicParSet[0];
          else        
            img->qp=rd_qp;
        }
        else if (img->rd_pass==1)
        {
          enc_picture=enc_frame_picture2;
          if (input->GenerateMultiplePPS)
            active_pps = &PicParSet[1];
          else        
            img->qp=rd_qp-1;
        }       
      }
    }         
    
    // For field coding, turn MB level field/frame coding flag off
    if (input->MbInterlace)
      mb_adaptive = 0;
    
    if (input->PicInterlace == ADAPTIVE_CODING)
    {
      //Rate control
      img->FieldControl=1;
      img->write_macroblock = 0;
      img->bot_MB = 0;

      img->field_picture = 1;  // we encode fields
      field_picture (top_pic, bottom_pic);
      
      //! Note: the distortion for a field coded picture is stored in the top field
      //! the distortion values in the bottom field are dummies
      dis_fld = top_pic->distortion_y + top_pic->distortion_u + top_pic->distortion_v;
      dis_frm = frame_pic->distortion_y + frame_pic->distortion_u + frame_pic->distortion_v;
      
      img->fld_flag = picture_structure_decision (frame_pic, top_pic, bottom_pic);
      update_field_frame_contexts (img->fld_flag);

      //Rate control
      if(img->fld_flag==0)
        img->FieldFrame=1;
      /*the current choice is field coding*/
      else
        img->FieldFrame=0;
    }
    else
   
      img->fld_flag = 0;
  }

  if (img->fld_flag)
    stats->bit_ctr_emulationprevention += stats->em_prev_bits_fld;
  else
    stats->bit_ctr_emulationprevention += stats->em_prev_bits_frm;

  if (img->type != B_SLICE)
  {
    img->pstruct_next_P = img->fld_flag;
  }

  // Here, img->structure may be either FRAME or BOTTOM FIELD depending on whether AFF coding is used
  // The picture structure decision changes really only the fld_flag

  if (img->fld_flag)            // field mode (use field when fld_flag=1 only)
  {
    field_mode_buffer (bits_fld, dis_fld_y, dis_fld_u, dis_fld_v);
    writeout_picture (top_pic);
    writeout_picture (bottom_pic);
  }
  else                          //frame mode
  {
    frame_mode_buffer (bits_frm, dis_frm_y, dis_frm_u, dis_frm_v);
    
    if (input->RDPictureDecision && img->rd_pass == 2)
      writeout_picture (frame_pic3);
    else if (input->RDPictureDecision && img->rd_pass == 1)
      writeout_picture (frame_pic2);
    else
      writeout_picture (frame_pic);
  }

  if (frame_pic3)
    free_slice_list(frame_pic3);  
  if (frame_pic2)
    free_slice_list(frame_pic2);  
  if (frame_pic)
    free_slice_list(frame_pic);
  if (top_pic)
    free_slice_list(top_pic);
  if (bottom_pic)
    free_slice_list(bottom_pic);

  /*
  // Tian Dong (Sept 2002)
  // in frame mode, the newly reconstructed frame has been inserted to the mem buffer
  // and it is time to prepare the spare picture SEI payload.
  if (input->InterlaceCodingOption == FRAME_CODING
      && input->SparePictureOption && img->type != B_SLICE)
    CalculateSparePicture ();
*/

  //Rate control
  if(input->RCEnable)
  {
    bits = stats->bit_ctr-stats->bit_ctr_n;
    rc_update_pict_frame(bits);
  }

/*
    
  if (input->InterlaceCodingOption == FRAME_CODING)
  {
    if (input->rdopt == 3 && img->type != B_SLICE)
      UpdateDecoders ();      // simulate packet losses and move decoded image to reference buffers
    
    if (input->RestrictRef)
      UpdatePixelMap ();
  }
*/

  find_snr ();

  time (&ltime2);               // end time sec
#ifdef WIN32
  _ftime (&tstruct2);           // end time ms
#else
  ftime (&tstruct2);            // end time ms
#endif

  tmp_time = (ltime2 * 1000 + tstruct2.millitm) - (ltime1 * 1000 + tstruct1.millitm);
  tot_time = tot_time + tmp_time;

  if (input->PicInterlace == ADAPTIVE_CODING)
  {
    if (img->fld_flag)
    {
      // store bottom field
      store_picture_in_dpb(enc_bottom_picture);
      free_storable_picture(enc_frame_picture);
    }
    else
    {
      // replace top with frame
      replace_top_pic_with_frame(enc_frame_picture);
      free_storable_picture(enc_bottom_picture);
    }
  }
  else
  {
    if (img->fld_flag)
    {
      store_picture_in_dpb(enc_bottom_picture);
    }
    else
    {
      if (img->rd_pass==2)
        store_picture_in_dpb(enc_frame_picture3);
      else if (img->rd_pass==1)
        store_picture_in_dpb(enc_frame_picture2);
      else
        store_picture_in_dpb(enc_frame_picture);
    }
  }


#ifdef _LEAKYBUCKET_
  // Store bits used for this frame and increment counter of no. of coded frames
  Bit_Buffer[total_frame_buffer] = stats->bit_ctr - stats->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 (stats->bit_ctr_parametersets_n!=0)
    ReportNALNonVLCBits(tmp_time, me_time);

  if (IMG_NUMBER == 0)
    ReportFirstframe(tmp_time,me_time);
    //ReportFirstframe(tmp_time);
  else
  {
    //Rate control
    if(input->RCEnable)
    {
      if((!input->PicInterlace)&&(!input->MbInterlace))
        bits=stats->bit_ctr-stats->bit_ctr_n;
      else
      {
        bits = stats->bit_ctr -Pprev_bits; // used for rate control update */
        Pprev_bits = stats->bit_ctr;
      }
    }

    switch (img->type)
    {
    case I_SLICE:
      stats->bit_ctr_P += stats->bit_ctr - stats->bit_ctr_n;
      ReportIntra(tmp_time,me_time);
      break;
    case SP_SLICE:
      stats->bit_ctr_P += stats->bit_ctr - stats->bit_ctr_n;
      ReportSP(tmp_time,me_time);
      break;
    case B_SLICE:
      stats->bit_ctr_B += stats->bit_ctr - stats->bit_ctr_n;
      if (img->nal_reference_idc>0)
        ReportRB(tmp_time,me_time);
      else
        ReportB(tmp_time,me_time);

      break;
    default:      // P
      stats->bit_ctr_P += stats->bit_ctr - stats->bit_ctr_n;
      ReportP(tmp_time,me_time);
    }
  }
  stats->bit_ctr_n = stats->bit_ctr;

  //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;

  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);

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


void copy_params()
{
  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;

  if (rd_pass == 2)
  {
    enc_frame_picture3  = alloc_storable_picture (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)
  {
    enc_frame_picture2  = alloc_storable_picture (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();
  }
  else
  {
    enc_frame_picture  = alloc_storable_picture (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;
  code_a_picture(frame);

  frame->bits_per_picture = 8 * ((((img->currentSlice)->partArr[0]).bitstream)->byte_pos);
  
  if (img->structure==FRAME)
  {
    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
 ************************************************************************

⌨️ 快捷键说明

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