lencod.c

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

C
2,070
字号
  img->bitdepth_scale[0]        = 1 << (img->bitdepth_luma - 8);
  img->bitdepth_lambda_scale    = 2 * (img->bitdepth_luma - 8);
  img->bitdepth_luma_qp_scale   = 3 *  img->bitdepth_lambda_scale;
  img->dc_pred_value_comp[0]    =  1<<(img->bitdepth_luma - 1);
  img->max_imgpel_value_comp[0] = (1<<img->bitdepth_luma) - 1;
  img->max_imgpel_value_comp_sq[0] = img->max_imgpel_value_comp[0] * img->max_imgpel_value_comp[0];

  img->dc_pred_value            = img->dc_pred_value_comp[0]; // set defaults
  img->max_imgpel_value         = img->max_imgpel_value_comp[0];
  img->mb_size[0][0]            = img->mb_size[0][1] = MB_BLOCK_SIZE;

  // Initialization for RC QP parameters (could be placed in ratectl.c)
  img->RCMinQP                = params->RCMinQP[P_SLICE];
  img->RCMaxQP                = params->RCMaxQP[P_SLICE];

  // Set current residue & prediction array pointers
  img->curr_res = img->mb_rres[0];
  img->curr_prd = img->mb_pred[0];
  if (active_sps->profile_idc == 66 || active_sps->profile_idc == 77 || active_sps->profile_idc == 88)
    img->min_IPCM_value = 1;  // See Annex A for restriction in pcm sample values for pre FRExt profiles
  else
    img->min_IPCM_value = 0;

  if (img->yuv_format != YUV400)
  {
    img->bitdepth_chroma          = params->output.bit_depth[1];
    img->bitdepth_scale[1]        = 1 << (img->bitdepth_chroma - 8);
    img->dc_pred_value_comp[1]    = 1<<(img->bitdepth_chroma - 1);
    img->dc_pred_value_comp[2]    = img->dc_pred_value_comp[1];
    img->max_imgpel_value_comp[1] = (1<<img->bitdepth_chroma) - 1;
    img->max_imgpel_value_comp[2] = img->max_imgpel_value_comp[1];
    img->max_imgpel_value_comp_sq[1] = img->max_imgpel_value_comp[1] * img->max_imgpel_value_comp[1];
    img->max_imgpel_value_comp_sq[2] = img->max_imgpel_value_comp[2] * img->max_imgpel_value_comp[2];
    img->num_blk8x8_uv            = (1<<img->yuv_format)&(~(0x1));
    img->num_cdc_coeff            = img->num_blk8x8_uv << 1;

    img->mb_size[1][0] = img->mb_size[2][0] = img->mb_cr_size_x = (img->yuv_format == YUV420 || img->yuv_format == YUV422) ? 8 : 16;
    img->mb_size[1][1] = img->mb_size[2][1] = img->mb_cr_size_y = (img->yuv_format == YUV444 || img->yuv_format == YUV422) ? 16 : 8;

    img->bitdepth_chroma_qp_scale = 6*(img->bitdepth_chroma - 8);

    img->chroma_qp_offset[0] = active_pps->cb_qp_index_offset;
    img->chroma_qp_offset[1] = active_pps->cr_qp_index_offset;
  }
  else
  {
    img->bitdepth_chroma     = 0;
    img->bitdepth_scale[1]   = 0;
    img->max_imgpel_value_comp[1] = 0;
    img->max_imgpel_value_comp[2] = img->max_imgpel_value_comp[1];
    img->max_imgpel_value_comp_sq[1] = img->max_imgpel_value_comp[1] * img->max_imgpel_value_comp[1];
    img->max_imgpel_value_comp_sq[2] = img->max_imgpel_value_comp[2] * img->max_imgpel_value_comp[2];
    img->num_blk8x8_uv       = 0;
    img->num_cdc_coeff       = 0;
    img->mb_size[1][0] = img->mb_size[2][0] = img->mb_cr_size_x = 0;
    img->mb_size[1][1] = img->mb_size[2][1] = img->mb_cr_size_y = 0;

    img->bitdepth_chroma_qp_scale = 0;
    img->bitdepth_chroma_qp_scale = 0;

    img->chroma_qp_offset[0] = 0;
    img->chroma_qp_offset[1] = 0;
  }  

  //img->pic_unit_size_on_disk = (imax(img->bitdepth_luma , img->bitdepth_chroma) > 8) ? 16 : 8;
  img->pic_unit_size_on_disk = (imax(params->source.bit_depth[0], params->source.bit_depth[1]) > 8) ? 16 : 8;
  img->out_unit_size_on_disk = (imax(params->output.bit_depth[0], params->output.bit_depth[1]) > 8) ? 16 : 8;

  img->max_bitCount =  128 + 256 * img->bitdepth_luma + 2 * img->mb_cr_size_y * img->mb_cr_size_x * img->bitdepth_chroma;
  //img->max_bitCount =  (128 + 256 * img->bitdepth_luma + 2 *img->mb_cr_size_y * img->mb_cr_size_x * img->bitdepth_chroma)*2;

  img->max_qp_delta = (25 + (img->bitdepth_luma_qp_scale>>1));
  img->min_qp_delta = img->max_qp_delta + 1;

  img->num_ref_frames = active_sps->num_ref_frames;
  img->max_num_references   = active_sps->frame_mbs_only_flag ? active_sps->num_ref_frames : 2 * active_sps->num_ref_frames;

  img->buf_cycle = params->num_ref_frames;
  img->base_dist = params->jumpd + 1;  

  // Intra/IDR related parameters
  img->lastIntraNumber = 0;
  img->lastINTRA       = 0;
  img->lastIDRnumber   = 0;
  img->last_ref_idc    = 0;
  img->idr_refresh     = 0;
  img->idr_gop_number  = 0;
  img->rewind_frame    = 0;

  img->DeblockCall     = 0;
  img->framerate       = (float) params->output.frame_rate;   // The basic frame rate (of the original sequence)

  // Allocate proper memory space for different parameters (i.e. MVs, coefficients, etc)
  if (!params->IntraProfile)
  {
    get_mem_mv (&(img->all_mv));

    if (params->BiPredMotionEstimation)
    {
      get_mem_bipred_mv(&(img->bipred_mv)); 
    }
  }

  get_mem_ACcoeff (&(img->cofAC));
  get_mem_DCcoeff (&(img->cofDC));

  if (params->AdaptiveRounding)
  {
    if (img->yuv_format != 0)
    {
      get_mem4Dint(&(img->ARCofAdj4x4), 3, MAXMODE, MB_BLOCK_SIZE, MB_BLOCK_SIZE); //all modes
      get_mem4Dint(&(img->ARCofAdj8x8), img->P444_joined ? 3 : 1, MAXMODE, MB_BLOCK_SIZE, MB_BLOCK_SIZE); //modes 0, 1, 2, 3, P8x8
    }     
    else
    {
      get_mem4Dint(&(img->ARCofAdj4x4), 1, MAXMODE, MB_BLOCK_SIZE, MB_BLOCK_SIZE); //all modes
      get_mem4Dint(&(img->ARCofAdj8x8), 1, MAXMODE, MB_BLOCK_SIZE, MB_BLOCK_SIZE); //modes 0, 1, 2, 3, P8x8
    }
  }

  byte_abs_range = (imax(img->max_imgpel_value_comp[0], img->max_imgpel_value_comp[1]) + 1) * 2;

  if ((img->quad = (int*)calloc (byte_abs_range, sizeof(int))) == NULL)
    no_mem_exit ("init_img: img->quad");

  img->quad += byte_abs_range/2;
  for (i=0; i < byte_abs_range/2; ++i)
  {
    img->quad[i] = img->quad[-i] = i * i;
  }

  img->width         = (params->output.width  + img->auto_crop_right);
  img->height        = (params->output.height + img->auto_crop_bottom);
  img->width_blk     = img->width  / BLOCK_SIZE;
  img->height_blk    = img->height / BLOCK_SIZE;
  img->width_padded  = img->width  + 2 * IMG_PAD_SIZE;
  img->height_padded = img->height + 2 * IMG_PAD_SIZE;

  if (img->yuv_format != YUV400)
  {
    img->width_cr = img->width  * mb_width_cr [img->yuv_format] / 16;
    img->height_cr= img->height * mb_height_cr[img->yuv_format] / 16;
  }
  else
  {
    img->width_cr = 0;
    img->height_cr= 0;
  }

  img->height_cr_frame = img->height_cr;

  img->size = img->width * img->height;
  img->size_cr = img->width_cr * img->height_cr;

  img->PicWidthInMbs    = img->width  / MB_BLOCK_SIZE;
  img->FrameHeightInMbs = img->height / MB_BLOCK_SIZE;
  img->FrameSizeInMbs   = img->PicWidthInMbs * img->FrameHeightInMbs;

  img->PicHeightInMapUnits = ( active_sps->frame_mbs_only_flag ? img->FrameHeightInMbs : img->FrameHeightInMbs/2 );

  if(params->MbInterlace)
  {
    alloc_rddata(img, &rddata_top_frame_mb);
    alloc_rddata(img, &rddata_bot_frame_mb);
    if ( params->MbInterlace != FRAME_MB_PAIR_CODING )
    {
      alloc_rddata(img, &rddata_top_field_mb);
      alloc_rddata(img, &rddata_bot_field_mb);
    }
  }

  if (params->UseRDOQuant && params->RDOQ_QP_Num > 1)
  {
    alloc_rddata(img, &rddata_trellis_curr);
    alloc_rddata(img, &rddata_trellis_best);

    if (!params->IntraProfile)
    {          
      if (params->Transform8x8Mode && params->RDOQ_CP_MV)
      {
        get_mem5Dshort(&tmp_mv8, 2, img->max_num_references, 4, 4, 2);
        get_mem3Dint  (&motion_cost8, 2, img->max_num_references, 4);
      }
    }
  }

  if ((img->b8x8info = (Block8x8Info *) calloc(1, sizeof(Block8x8Info))) == NULL)
     no_mem_exit("init_img: img->block8x8info");

  if( IS_INDEPENDENT(params) )
  {
    for( i = 0; i < MAX_PLANE; i++ ){
      if ((img->mb_data_JV[i] = (Macroblock *) calloc(img->FrameSizeInMbs,sizeof(Macroblock))) == NULL)
        no_mem_exit("init_img: img->mb_data_JV");
    }
    img->mb_data = NULL;
  }
  else
  {
    if ((img->mb_data = (Macroblock *) calloc(img->FrameSizeInMbs, sizeof(Macroblock))) == NULL)
      no_mem_exit("init_img: img->mb_data");
  }

  if (params->UseConstrainedIntraPred)
  {
    if ((img->intra_block = (int*)calloc(img->FrameSizeInMbs, sizeof(int))) == NULL)
      no_mem_exit("init_img: img->intra_block");
  }

  if (params->CtxAdptLagrangeMult == 1)
  {
    if ((mb16x16_cost_frame = (double*)calloc(img->FrameSizeInMbs, sizeof(double))) == NULL)
    {
      no_mem_exit("init mb16x16_cost_frame");
    }
  }
  get_mem2D((byte***)&(img->ipredmode), img->height_blk, img->width_blk);        //need two extra rows at right and bottom
  get_mem2D((byte***)&(img->ipredmode8x8), img->height_blk, img->width_blk);     // help storage for ipredmode 8x8, inserted by YV
  memset(&(img->ipredmode[0][0])   , -1, img->height_blk * img->width_blk *sizeof(char));
  memset(&(img->ipredmode8x8[0][0]), -1, img->height_blk * img->width_blk *sizeof(char));


  if (params->MbInterlace)
  {
    get_mem2D((byte***)&(rddata_bot_frame_mb.ipredmode), img->height_blk, img->width_blk);
    get_mem2D((byte***)&(rddata_top_field_mb.ipredmode), img->height_blk, img->width_blk);
    get_mem2D((byte***)&(rddata_bot_field_mb.ipredmode), img->height_blk, img->width_blk);
  }

  // CAVLC mem
  get_mem3Dint(&(img->nz_coeff), img->FrameSizeInMbs, 4, 4+img->num_blk8x8_uv);
  
  get_mem2Dolm     (&(img->lambda), 10, 52 + img->bitdepth_luma_qp_scale, img->bitdepth_luma_qp_scale);
  get_mem2Dodouble (&(img->lambda_md), 10, 52 + img->bitdepth_luma_qp_scale, img->bitdepth_luma_qp_scale);
  get_mem3Dodouble (&(img->lambda_me), 10, 52 + img->bitdepth_luma_qp_scale, 3, img->bitdepth_luma_qp_scale);
  get_mem3Doint    (&(img->lambda_mf), 10, 52 + img->bitdepth_luma_qp_scale, 3, img->bitdepth_luma_qp_scale);

  if (params->CtxAdptLagrangeMult == 1)
  {
    get_mem2Dodouble(&(img->lambda_mf_factor), 10, 52 + img->bitdepth_luma_qp_scale, img->bitdepth_luma_qp_scale);
  }

  img->b_frame_to_code = 0;
  img->GopLevels = (params->NumberBFrames) ? 1 : 0;
  img->mb_y_upd=0;

  RandomIntraInit (img->PicWidthInMbs, img->FrameHeightInMbs, params->RandomIntraMBRefresh);

  InitSEIMessages(); 

  initInput(&params->source, &params->output);

  // Allocate I/O Frame memory
  AllocateFrameMemory(img, params, params->source.size);

  // Initialize filtering parameters. If sending parameters, the offsets are
  // multiplied by 2 since inputs are taken in "div 2" format.
  // If not sending parameters, all fields are cleared
  if (params->DFSendParameters)
  {
    for (j = 0; j < 2; j++)
    {
      for (i = 0; i < NUM_SLICE_TYPES; i++)
      {
        params->DFAlpha[j][i] <<= 1;
        params->DFBeta [j][i] <<= 1;
      }
    }
  }
  else
  {
    for (j = 0; j < 2; j++)
    {
      for (i = 0; i < NUM_SLICE_TYPES; i++)
      {
        params->DFDisableIdc[j][i] = 0;
        params->DFAlpha     [j][i] = 0;
        params->DFBeta      [j][i] = 0;
      }
    }
  }

  img->ChromaArrayType = params->separate_colour_plane_flag ? 0 : params->output.yuv_format;
  img->colour_plane_id = 0;

  if (params->RDPictureDecision)
    img->frm_iter = 3;
  else
    img->frm_iter = 1;

  img->frame_interval = (double) (params->frame_skip + 1);
}


/*!
 ***********************************************************************
 * \brief
 *    Free the Image structures
 * \par Input:
 *    Image Parameters ImageParameters *img
 ***********************************************************************
 */
void free_img ()
{
  // Delete Frame memory 
  DeleteFrameMemory();

  CloseSEIMessages(); 
  if (!params->IntraProfile)
  {
    free_mem_mv (img->all_mv);

    if (params->BiPredMotionEstimation)
    {
      free_mem_bipred_mv (img->bipred_mv);
    }
  }

  free_mem_ACcoeff (img->cofAC);
  free_mem_DCcoeff (img->cofDC);

  if (params->AdaptiveRounding)
  {
    free_mem4Dint(img->ARCofAdj4x4);
    free_mem4Dint(img->ARCofAdj8x8);
  }


  if (params->MbInterlace)
  {
    free_rddata(&rddata_top_frame_mb);
    free_rddata(&rddata_bot_frame_mb);

    if ( params->MbInterlace != FRAME_MB_PAIR_CODING )
    {
      free_rddata(&rddata_top_field_mb);
      free_rddata(&rddata_bot_field_mb);
    }
  }

  if (params->UseRDOQuant && params->RDOQ_QP_Num > 1)
  {
    free_rddata(&rddata_trellis_curr);
    free_rddata(&rddata_trellis_best);

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

  free (img->quad - (imax(img->max_imgpel_value_comp[0],img->max_imgpel_value_comp[1]) + 1));
  
  free_mem3Dpel(img->mb_pred  );
  free_mem4Dpel(img->mpr_16x16);
  free_mem4Dpel(img->mpr_8x8  );
  free_mem4Dpel(img->mpr_4x4  );
  free_mem3Dint(img->mb_ores  );
  free_mem3Dint(img->mb_rres  );
  free (img);
}


/*!
 ************************************************************************
 * \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(ImageData *imgData)
{
  int memory_size = 0;
  int nplane;

  // allocate memory for reference frame buffers: imgData->frm_data

⌨️ 快捷键说明

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