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

📄 image.c

📁 压缩JM12.3d的完整的全部C语言的代码文档,用于嵌入式系统的压缩编解码
💻 C
📖 第 1 页 / 共 5 页
字号:


/*!
 ************************************************************************
 * \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];
    img->current_mb_nr = currSlice->start_mb_nr;
    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
 *    Prepare and allocate an encoded frame picture structure
 ************************************************************************
 */
void prepare_enc_frame_picture (StorablePicture **stored_pic)
{
  (*stored_pic)              = alloc_storable_picture ((PictureStructure) img->structure, img->width, img->height, img->width_cr, img->height_cr);
  
  img->ThisPOC               = img->framepoc;
  (*stored_pic)->poc         = img->framepoc;
  (*stored_pic)->top_poc     = img->toppoc;
  (*stored_pic)->bottom_poc  = img->bottompoc;
  (*stored_pic)->frame_poc   = img->framepoc;
  (*stored_pic)->pic_num     = img->frame_num;
  (*stored_pic)->frame_num   = img->frame_num;
  (*stored_pic)->coded_frame = 1;
  (*stored_pic)->MbaffFrameFlag = img->MbaffFrameFlag = (input->MbInterlace != FRAME_CODING);
  
  get_mb_block_pos           = img->MbaffFrameFlag ? get_mb_block_pos_mbaff : get_mb_block_pos_normal;
  getNeighbour               = img->MbaffFrameFlag ? getAffNeighbour : getNonAffNeighbour;
  enc_picture                = *stored_pic;

  copy_params();
}

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

  if ( rd_pass == 2 )
    prepare_enc_frame_picture( &enc_frame_picture[2] );
  else if ( rd_pass == 1 )
    prepare_enc_frame_picture( &enc_frame_picture[1] );
  else
  {
    if( IS_INDEPENDENT(input) )
    {
      for( nplane=0; nplane<MAX_PLANE; nplane++ )
      {
        enc_frame_picture_JV[nplane]  = alloc_storable_picture ((PictureStructure) img->structure, img->width, img->height, img->width_cr, img->height_cr);
        img->ThisPOC=enc_frame_picture_JV[nplane]->poc=img->framepoc;
        enc_frame_picture_JV[nplane]->top_poc    = img->toppoc;
        enc_frame_picture_JV[nplane]->bottom_poc = img->bottompoc;
        enc_frame_picture_JV[nplane]->frame_poc = img->framepoc;

        enc_frame_picture_JV[nplane]->pic_num = img->frame_num;
        enc_frame_picture_JV[nplane]->frame_num = img->frame_num;
        enc_frame_picture_JV[nplane]->coded_frame = 1;

        enc_frame_picture_JV[nplane]->MbaffFrameFlag = img->MbaffFrameFlag = (input->MbInterlace != FRAME_CODING);
        get_mb_block_pos = img->MbaffFrameFlag ? get_mb_block_pos_mbaff : get_mb_block_pos_normal;
        getNeighbour = img->MbaffFrameFlag ? getAffNeighbour : getNonAffNeighbour;

        enc_picture=enc_frame_picture_JV[nplane];
        copy_params();
      }
    }
    else
    {
      prepare_enc_frame_picture( &enc_frame_picture[0] );
     }
  }

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

  img->fld_flag = 0;
  code_a_picture(frame);
  if( IS_INDEPENDENT(input) )
  {
    make_frame_picture_JV();
  }

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

  if (img->structure==FRAME)
  {
    if( IS_INDEPENDENT(input) )
    {
      find_distortion_JV ();
    }
    else
    {
      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;
  img->SumFrameQP = 0;

  //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_field_picture[0]              = alloc_storable_picture ((PictureStructure) img->structure, img->width, img->height, img->width_cr, img->height_cr);
  enc_field_picture[0]->poc         = img->toppoc;
  enc_field_picture[0]->frame_poc   = img->toppoc;
  enc_field_picture[0]->pic_num     = img->frame_num;
  enc_field_picture[0]->frame_num   = img->frame_num;
  enc_field_picture[0]->coded_frame = 0;
  enc_field_picture[0]->MbaffFrameFlag = img->MbaffFrameFlag = FALSE;
  get_mb_block_pos = get_mb_block_pos_normal;
  getNeighbour = getNonAffNeighbour;
  img->ThisPOC = img->toppoc;

  img->structure = TOP_FIELD;
  enc_picture = enc_field_picture[0];
  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(quadratic_RC, 0, 1, 1, 1.0F); 
      else
        rc_init_pict(quadratic_RC, 0, 1, 0, 1.0F);
    }
    img->qp = updateQP(quadratic_RC, 1);

    generic_RC->TopFieldFlag=1;
  }

  code_a_picture(field_pic[0]);
  enc_picture->structure = (PictureStructure) 1;

  store_picture_in_dpb(enc_field_picture[0]);

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

  //Rate control
  TopFieldBits=top->bits_per_picture;

  //  Bottom field
  enc_field_picture[1]  = alloc_storable_picture ((PictureStructure) img->structure, img->width, img->height, img->width_cr, img->height_cr);
  enc_field_picture[1]->poc=img->bottompoc;
  enc_field_picture[1]->frame_poc = img->bottompoc;
  enc_field_picture[1]->pic_num = img->frame_num;
  enc_field_picture[1]->frame_num = img->frame_num;
  enc_field_picture[1]->coded_frame = 0;
  enc_field_picture[1]->MbaffFrameFlag = img->MbaffFrameFlag = FALSE;
  get_mb_block_pos = get_mb_block_pos_normal;
  getNeighbour = getNonAffNeighbour;

  img->ThisPOC = img->bottompoc;
  img->structure = BOTTOM_FIELD;
  enc_picture = enc_field_picture[1];
  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)
  {
    quadratic_RC->bits_topfield = TopFieldBits;
    rc_init_pict(quadratic_RC, 0,0,0, 1.0F); 
    img->qp  = updateQP(quadratic_RC, 0); 
    generic_RC->TopFieldFlag = 0;
  }

  enc_picture->structure = (PictureStructure) 2;
  code_a_picture(field_pic[1]);

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

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

  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 ((input->PicInterlace != FRAME_CODING)||(input->MbInterlace != FRAME_CODING))
  {
    img->height = img->height / 2;
    img->height_cr = img->height_cr / 2;
    img->number *= 2;

    put_buffer_top ();

    img->number++;
    put_buffer_bot ();

    img->number /= 2;         // reset the img->number to field
    img->height = (input->img_height+img->auto_crop_bottom);
    img->height_cr = img->height_cr_frame;

    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_dec_ref_pic_marking_buffer(void)
{
  img->dec_ref_pic_marking_buffer=NULL;
}


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

⌨️ 快捷键说明

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