lencod.c

来自「the newest JM software by h.264 JVT offi」· C语言 代码 · 共 2,070 行 · 第 1/5 页

C
2,070
字号
  if( IS_INDEPENDENT(params) )
  {
    for( nplane=0; nplane<MAX_PLANE; nplane++ )
    {
      imgData->format = params->output;
      imgData->format.width = img->width;
      imgData->format.height = img->height;
      imgData->format.width_cr = img->width_cr;
      imgData->format.height_cr = img->height_cr;
      memory_size += get_mem2Dpel(&(imgData->frm_data[nplane]), img->height, img->width);
    }
  }
  else
  {
    //imgData->format = params->input_file1.format;
    imgData->format           = params->output;
    imgData->format.width     = img->width;    
    imgData->format.height    = img->height;
    imgData->format.width_cr  = img->width_cr;
    imgData->format.height_cr = img->height_cr;
    memory_size += get_mem2Dpel(&(imgData->frm_data[0]), img->height, img->width);
    
    if (img->yuv_format != YUV400)
    {
      int i, j, k;
      memory_size += get_mem2Dpel(&(imgData->frm_data[1]), img->height_cr, img->width_cr);
      memory_size += get_mem2Dpel(&(imgData->frm_data[2]), img->height_cr, img->width_cr);

      if (sizeof(imgpel) == sizeof(unsigned char))
      {
        for (k = 1; k < 3; k++)
          memset(&(imgData->frm_data[k][0][0]), 128, img->height_cr * img->width_cr * sizeof(imgpel));
      }
      else
      {
        for (k = 1; k < 3; k++)
          for (j = 0; j < img->height_cr; j++)
            for (i = 0; i < img->width_cr; i++)
              imgData->frm_data[k][j][i] = 128;
      }
    }
  }

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

    if (img->yuv_format != YUV400)
    {

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

      memory_size += init_top_bot_planes(imgData->frm_data[1], img->height_cr, img->width_cr, &(imgData->top_data[1]), &(imgData->bot_data[1]));
      memory_size += init_top_bot_planes(imgData->frm_data[2], img->height_cr, img->width_cr, &(imgData->top_data[2]), &(imgData->bot_data[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;

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

  // Init memory data for input & encoded images
  memory_size += init_orig_buffers(&imgData);
  memory_size += init_orig_buffers(&imgData1);
  
  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->NumberBFrames != 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)
  {
    memory_size += allocate_errdo_mem(params);
  }

  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(ImageData *imgData)
{
  if( IS_INDEPENDENT(params) )
  {
    int nplane;
    for( nplane=0; nplane<MAX_PLANE; nplane++ )
    {
      free_mem2Dpel(imgData->frm_data[nplane]);      // free ref frame buffers
    }
  }
  else
  {
    free_mem2Dpel(imgData->frm_data[0]);      // free ref frame buffers
    
    if (img->yuv_format != YUV400)
    {
      free_mem2Dpel(imgData->frm_data[1]);
      free_mem2Dpel(imgData->frm_data[2]);
    }
  }

  if (!active_sps->frame_mbs_only_flag)
  {
    free_top_bot_planes(imgData->top_data[0], imgData->bot_data[0]);

    if (img->yuv_format != YUV400)
    {
      free_top_bot_planes(imgData->top_data[1], imgData->bot_data[1]);
      free_top_bot_planes(imgData->top_data[2], imgData->bot_data[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;

  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(&imgData);
  free_orig_planes(&imgData1);

  // 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->NumberBFrames != 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);
  }

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

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

  if (params->rdopt == 3)
  {
    free_errdo_mem();
  }

  if (params->RestrictRef)
  {
    free(pixel_map[0]);
    free(pixel_map);
    free(refresh_map[0]);
    free(refresh_map);
  }

  if (!active_sps->frame_mbs_only_flag)
  {
    free_mem2Dpel(imgY_com);
    
    if (img->yuv_format != YUV400)
    {
      free_mem3Dpel(imgUV_com);
    }
  }

  free_mem3Dint(img->nz_coeff);

  free_mem2Dolm     (img->lambda, img->bitdepth_luma_qp_scale);
  free_mem2Dodouble (img->lambda_md, img->bitdepth_luma_qp_scale);
  free_mem3Dodouble (img->lambda_me, 10, 52 + img->bitdepth_luma_qp_scale, img->bitdepth_luma_qp_scale);
  free_mem3Doint    (img->lambda_mf, 10, 52 + img->bitdepth_luma_qp_scale, img->bitdepth_luma_qp_scale);

  if (params->CtxAdptLagrangeMult == 1)
  {
    free_mem2Dodouble(img->lambda_mf_factor, img->bitdepth_luma_qp_scale);
  }

  if (!params->IntraProfile)
  {
    if (params->SearchMode == UM_HEX)
    {

⌨️ 快捷键说明

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