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

📄 lencod.c

📁 H.264编码实现
💻 C
📖 第 1 页 / 共 4 页
字号:
    free_mem_ACcoeff (rddata_trellis_best.cofAC);
    free_mem_DCcoeff (rddata_trellis_best.cofDC);

    if (!params->IntraProfile)
    {    
      free_mem_mv (rddata_trellis_curr.pred_mv);
      free_mem_mv (rddata_trellis_curr.all_mv);

      free_mem_mv (rddata_trellis_best.pred_mv);
      free_mem_mv (rddata_trellis_best.all_mv);

      if (params->Transform8x8Mode && params->RDOQ_CP_MV)
      {
        free_mem5Dshort(tmp_mv8);
        free_mem5Dshort(tmp_pmv8);
        free_mem3Dint(motion_cost8);
      }
    }
  }

  free (img->quad - (imax(img->max_imgpel_value_comp[0],img->max_imgpel_value_comp[1]) + 1));

  if (params->MbInterlace)
  {
    free_mem2D((byte**) rddata_bot_frame_mb.ipredmode);
    free_mem2D((byte**) rddata_top_field_mb.ipredmode);
    free_mem2D((byte**) rddata_bot_field_mb.ipredmode);
  }
}


/*!
 ************************************************************************
 * \brief
 *    Allocates the picture structure along with its dependent
 *    data structures
 * \return
 *    Pointer to a Picture
 ************************************************************************
 */

Picture *malloc_picture()
{
  Picture *pic;
  if ((pic = calloc (1, sizeof (Picture))) == NULL) no_mem_exit ("malloc_picture: Picture structure");
  //! Note: slice structures are allocated as needed in code_a_picture
  return pic;
}

/*!
 ************************************************************************
 * \brief
 *    Frees a picture
 * \param
 *    pic: POinter to a Picture to be freed
 ************************************************************************
 */


void free_picture(Picture *pic)
{
  if (pic != NULL)
  {
    free_slice_list(pic);
    free (pic);
  }
}



/*!
 ************************************************************************
 * \brief
 *    memory allocation for original picture buffers
 ************************************************************************
 */
int init_orig_buffers(void)
{
  int memory_size = 0;
  int nplane;

  // allocate memory for reference frame buffers: img_org_frm
  if( IS_INDEPENDENT(params) )
  {
    for( nplane=0; nplane<MAX_PLANE; nplane++ )
    {
      memory_size += get_mem2Dpel(&img_org_frm[nplane], img->height, img->width);
    }
  }
  else
  {
    memory_size += get_mem2Dpel(&img_org_frm[0], img->height, img->width);
    if (img->yuv_format != YUV400)
    {
      int i, j, k;
      memory_size += get_mem2Dpel(&img_org_frm[1], img->height_cr, img->width_cr);
      memory_size += get_mem2Dpel(&img_org_frm[2], img->height_cr, img->width_cr);
      for (k = 1; k < 3; k++)
        for (j = 0; j < img->height_cr; j++)
          for (i = 0; i < img->width_cr; i++)
            img_org_frm[k][j][i] = 128;
    }
  }

  if (!active_sps->frame_mbs_only_flag)
  {
    // allocate memory for field reference frame buffers
    memory_size += init_top_bot_planes(img_org_frm[0], img->height, img->width, &img_org_top[0], &img_org_bot[0]);

    if (img->yuv_format != YUV400)
    {

      memory_size += 4*(sizeof(imgpel**));

      memory_size += init_top_bot_planes(img_org_frm[1], img->height_cr, img->width_cr, &(img_org_top[1]), &(img_org_bot[1]));
      memory_size += init_top_bot_planes(img_org_frm[2], img->height_cr, img->width_cr, &(img_org_top[2]), &(img_org_bot[2]));
    }
  }
  return memory_size;
}

/*!
 ************************************************************************
 * \brief
 *    Dynamic memory allocation of frame size related global buffers
 *    buffers are defined in global.h, allocated memory must be freed in
 *    void free_global_buffers()
 * \par Input:
 *    Input Parameters struct inp_par *inp,                            \n
 *    Image Parameters ImageParameters *img
 * \return Number of allocated bytes
 ************************************************************************
 */
int init_global_buffers(void)
{
  int j, memory_size=0;
#ifdef _ADAPT_LAST_GROUP_
  extern int *last_P_no_frm;
  extern int *last_P_no_fld;

//  if ((last_P_no_frm = (int*)malloc(2*img->max_num_references*sizeof(int))) == NULL)
  if ((last_P_no_frm = (int*)malloc(32*sizeof(int))) == NULL)
    no_mem_exit("init_global_buffers: last_P_no");
  if (!active_sps->frame_mbs_only_flag)
//    if ((last_P_no_fld = (int*)malloc(4*img->max_num_references*sizeof(int))) == NULL)
    if ((last_P_no_fld = (int*)malloc(64*sizeof(int))) == NULL)
      no_mem_exit("init_global_buffers: last_P_no");
#endif

  if ((enc_frame_picture = (StorablePicture**)malloc(6 * sizeof(StorablePicture*))) == NULL)
    no_mem_exit("init_global_buffers: *enc_frame_picture");

  for (j = 0; j < 6; j++)
    enc_frame_picture[j] = NULL;

  if ((enc_field_picture = (StorablePicture**)malloc(2 * sizeof(StorablePicture*))) == NULL)
    no_mem_exit("init_global_buffers: *enc_field_picture");

  for (j = 0; j < 2; j++)
    enc_field_picture[j] = NULL;

  if ((frame_pic = (Picture**)malloc(img->frm_iter * sizeof(Picture*))) == NULL)
    no_mem_exit("init_global_buffers: *frame_pic");

  for (j = 0; j < img->frm_iter; j++)
    frame_pic[j] = malloc_picture();

  if (params->si_frame_indicator || params->sp_periodicity)
  {
    si_frame_indicator=0; //indicates whether the frame is SP or SI
    number_sp2_frames=0;

    frame_pic_si = malloc_picture();//picture buffer for the encoded SI picture
    //allocation of lrec and lrec_uv for SI picture
    get_mem2Dint (&lrec, img->height, img->width);
    get_mem3Dint (&lrec_uv, 2, img->height, img->width);
  }

  // Allocate memory for field picture coding
  if (params->PicInterlace != FRAME_CODING)
  { 
    if ((field_pic = (Picture**)malloc(2 * sizeof(Picture*))) == NULL)
      no_mem_exit("init_global_buffers: *field_pic");

    for (j = 0; j < 2; j++)
      field_pic[j] = malloc_picture();
  }

  memory_size += init_orig_buffers();

  memory_size += get_mem2Dint(&PicPos, img->FrameSizeInMbs + 1, 2);

  for (j = 0; j < (int) img->FrameSizeInMbs + 1; j++)
  {
    PicPos[j][0] = (j % img->PicWidthInMbs);
    PicPos[j][1] = (j / img->PicWidthInMbs);
  }

  if (params->WeightedPrediction || params->WeightedBiprediction || params->GenerateMultiplePPS)
  {
    // Currently only use up to 32 references. Need to use different indicator such as maximum num of references in list
    memory_size += get_mem3Dint(&wp_weight, 6, MAX_REFERENCE_PICTURES, 3);
    memory_size += get_mem3Dint(&wp_offset, 6, MAX_REFERENCE_PICTURES, 3);

    memory_size += get_mem4Dint(&wbp_weight, 6, MAX_REFERENCE_PICTURES, MAX_REFERENCE_PICTURES, 3);
  }

  // allocate memory for reference frames of each block: refFrArr

  if ((params->successive_Bframe != 0) || (params->BRefPictures > 0) || (params->ProfileIDC != 66))
  {
    memory_size += get_mem3D((byte ****)(void*)(&direct_ref_idx), 2, img->height_blk, img->width_blk);
    memory_size += get_mem2D((byte ***)(void*)&direct_pdir, img->height_blk, img->width_blk);
  }

  if (params->rdopt == 3)
  {
    decs->dec_mbY = NULL;
    decs->dec_mbY8x8 = NULL;
    memory_size += get_mem3Dpel(&decs->dec_mbY, params->NoOfDecoders, 16, 16);
    memory_size += get_mem3Dpel(&decs->dec_mbY8x8, params->NoOfDecoders, 16, 16);
  }
  if (params->RestrictRef)
  {
    memory_size += get_mem2D(&pixel_map,   img->height,   img->width);
    memory_size += get_mem2D(&refresh_map, img->height/8, img->width/8);
  }

  if (!active_sps->frame_mbs_only_flag)
  {
    memory_size += get_mem2Dpel(&imgY_com, img->height, img->width);

    if (img->yuv_format != YUV400)
    {
      memory_size += get_mem3Dpel(&imgUV_com, 2, img->height_cr, img->width_cr);
    }
  }

  // allocate and set memory relating to motion estimation
  if (!params->IntraProfile)
  {  
    if (params->SearchMode == UM_HEX)
    {
      memory_size += UMHEX_get_mem();
    }
    else if (params->SearchMode == UM_HEX_SIMPLE)
    {
      smpUMHEX_init();
      memory_size += smpUMHEX_get_mem();
    }
    else if (params->SearchMode == EPZS)
      memory_size += EPZSInit(params, img);
  }

  if (params->RCEnable)
    rc_allocate_memory();

  if (params->redundant_pic_flag)
  {
    memory_size += get_mem2Dpel(&imgY_tmp, img->height, img->width);
    memory_size += get_mem2Dpel(&imgUV_tmp[0], img->height_cr, img->width_cr);
    memory_size += get_mem2Dpel(&imgUV_tmp[1], img->height_cr, img->width_cr);
  }

  memory_size += get_mem2Dint (&imgY_sub_tmp, img->height_padded, img->width_padded);

  if ( params->ChromaMCBuffer )
    chroma_mc_setup();

  img_padded_size_x       = (img->width + 2 * IMG_PAD_SIZE);
  img_padded_size_x2      = (img_padded_size_x << 1);
  img_padded_size_x4      = (img_padded_size_x << 2);
  img_padded_size_x_m8    = (img_padded_size_x - 8);
  img_padded_size_x_m8x8  = (img_padded_size_x - BLOCK_SIZE_8x8);
  img_padded_size_x_m4x4  = (img_padded_size_x - BLOCK_SIZE);
  img_cr_padded_size_x    = (img->width_cr + 2 * img_pad_size_uv_x);
  img_cr_padded_size_x2   = (img_cr_padded_size_x << 1);
  img_cr_padded_size_x4   = (img_cr_padded_size_x << 2);
  img_cr_padded_size_x_m8 = (img_cr_padded_size_x - 8);

  // RGB images for distortion calculation
  // Recommended to do this allocation (and de-allocation) in 
  // the appropriate file instead of here.
  if(params->DistortionYUVtoRGB)
  {
    memory_size += create_RGB_memory(img);
  }

  pWPX = NULL;
  if ( params->WPMCPrecision )
  {
    wpxInitWPXObject();
  }

  return memory_size;
}


/*!
 ************************************************************************
 * \brief
 *    Free allocated memory of original picture buffers
 ************************************************************************
 */
void free_orig_planes(void)
{
  if( IS_INDEPENDENT(params) )
  {
    int nplane;
    for( nplane=0; nplane<MAX_PLANE; nplane++ )
    {
      free_mem2Dpel(img_org_frm[nplane]);      // free ref frame buffers
    }
  }
  else
  {
    free_mem2Dpel(img_org_frm[0]);      // free ref frame buffers
    if (img->yuv_format != YUV400)
    {
      free_mem2Dpel(img_org_frm[1]);
      free_mem2Dpel(img_org_frm[2]);
    }
  }

  if (!active_sps->frame_mbs_only_flag)
  {
    free_top_bot_planes(img_org_top[0], img_org_bot[0]);

    if (img->yuv_format != YUV400)
    {
      free_top_bot_planes(img_org_top[1], img_org_bot[1]);
      free_top_bot_planes(img_org_top[2], img_org_bot[2]);
    }
  }
}


/*!
 ************************************************************************
 * \brief
 *    Free allocated memory of frame size related global buffers
 *    buffers are defined in global.h, allocated memory is allocated in
 *    int get_mem4global_buffers()
 * \par Input:
 *    Input Parameters struct inp_par *inp,                             \n
 *    Image Parameters ImageParameters *img
 * \par Output:
 *    none
 ************************************************************************
 */
void free_global_buffers(void)
{
  int  i,j;

#ifdef _ADAPT_LAST_GROUP_
  extern int *last_P_no_frm;
  extern int *last_P_no_fld;
  free (last_P_no_frm);
  free (last_P_no_fld);
#endif

  if (enc_frame_picture)
    free (enc_frame_picture);
  if (frame_pic)
  {
    for (j = 0; j < img->frm_iter; j++)
    {
      if (frame_pic[j])
        free_picture (frame_pic[j]);
    }
    free (frame_pic);
  }

  if (enc_field_picture)
    free (enc_field_picture);
  if (field_pic)
  {
    for (j = 0; j < 2; j++)
    {
      if (field_pic[j])
        free_picture (field_pic[j]);
    }
    free (field_pic);
  }

  // Deallocation of SI picture related memory
  if (params->si_frame_indicator || params->sp_periodicity)
  {
    free_picture (frame_pic_si);
    //deallocation of lrec and lrec_uv for SI frames
    free_mem2Dint (lrec);
    free_mem3Dint (lrec_uv);
  }

  free_orig_planes();
  // free lookup memory which helps avoid divides with PicWidthInMbs
  free_mem2Dint(PicPos);
  // Free Qmatrices and offsets
  free_QMatrix();
  free_QOffsets();

  if ( params->WPMCPrecision )
  {
    wpxFreeWPXObject();
  }

  if (params->WeightedPrediction || params->WeightedBiprediction || params->GenerateMultiplePPS)
  {
    free_mem3Dint(wp_weight );
    free_mem3Dint(wp_offset );
    free_mem4Dint(wbp_weight);
  }

  if ((stats->successive_Bframe != 0) || (params->BRefPictures > 0)||(params->ProfileIDC != 66))
  {
    free_mem3D((byte ***)direct_ref_idx);
    free_mem2D((byte **) direct_pdir);
  } // end if B frame

  if (imgY_sub_tmp) // free temp quarter pel frame buffers
  {
    free_mem2Dint (imgY_sub_tmp);
    imgY_sub_tmp=NULL;
  }

  // free mem, allocated in init_img()
  // free intra pred mode buffer for blocks
  free_mem2D((byte**)img->ipredmode);
  free_mem2D((byte**)img->ipredmode8x8);
  free(img->b8x8info);

  if( IS_INDEPENDENT(params) )
  {
    for( i=0; i<MAX_PLANE; i++ ){
      free(img->mb_data_JV[i]);
    }
  }
  else
  {
    free(img->mb_data);
  }

  free_mem2D((byte**)rddata_top_frame_mb.ipredmode);
  free_mem2D((byte**)rddata_trellis_curr.ipredmode);
  free_mem2D((byte**)rddata_trellis_best.ipredmode);

  if(params->UseConstrainedIntraPred)
  {
    free (img->intra_block);
  }

  if (params->CtxAdptLagrangeMult == 1)
  {
    free(mb16x16_cost_frame);
  }

  if (params->rdopt == 3)
  {
    if (decs->dec_mbY)
    {
       free_mem3Dpel(decs->dec_mbY);
    }
    if (decs->dec_mbY8x8)
    {	
       free_mem3Dpel(decs->dec_mbY8x8);
    }

⌨️ 快捷键说明

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