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

📄 configfile.c

📁 JM 11.0 KTA 2.1 Source Code
💻 C
📖 第 1 页 / 共 3 页
字号:
***********************************************************************
* \brief
*    Returns the index number from Map[] for a given parameter name.
* \param s
*    parameter name string
* \return
*    the index number if the string is a valid parameter name,         \n
*    -1 for error
***********************************************************************
*/
static int ParameterNameToMapIndex (char *s)
{
  int i = 0;
  
  while (Map[i].TokenName != NULL)
    if (0==strcasecmp (Map[i].TokenName, s))
      return i;
    else
      i++;
    return -1;
}

/*!
***********************************************************************
* \brief
*    Sets initial values for encoding parameters.
* \return
*    -1 for error
***********************************************************************
*/
static int InitEncoderParams(void)
{
  int i = 0;
  
  while (Map[i].TokenName != NULL)
  {
    if (Map[i].Type == 0)
      * (int *) (Map[i].Place) = (int) Map[i].Default;
    else if (Map[i].Type == 2)
      * (double *) (Map[i].Place) = Map[i].Default;
    i++;
  }
  return -1;
}

/*!
***********************************************************************
* \brief
*    Validates encoding parameters.
* \return
*    -1 for error
***********************************************************************
*/
static int TestEncoderParams(int bitdepth_qp_scale)
{
  int i = 0;
  
  while (Map[i].TokenName != NULL)
  {
    if (Map[i].param_limits == 1)
    {
      if (Map[i].Type == 0)
      {
        if ( * (int *) (Map[i].Place) < (int) Map[i].min_limit || * (int *) (Map[i].Place) > (int) Map[i].max_limit )
        {
          snprintf(errortext, ET_SIZE, "Error in input parameter %s. Check configuration file. Value should be in [%d, %d] range.", Map[i].TokenName, (int) Map[i].min_limit,(int)Map[i].max_limit );
          error (errortext, 400);
        }
        
      }
      else if (Map[i].Type == 2)
      {
        if ( * (double *) (Map[i].Place) < Map[i].min_limit || * (double *) (Map[i].Place) > Map[i].max_limit )
        {
          snprintf(errortext, ET_SIZE, "Error in input parameter %s. Check configuration file. Value should be in [%.2f, %.2f] range.", Map[i].TokenName,Map[i].min_limit ,Map[i].max_limit );
          error (errortext, 400);
        }        
      }            
    }
    else if (Map[i].param_limits == 2)
    {
      if (Map[i].Type == 0)
      {
        if ( * (int *) (Map[i].Place) < (int) Map[i].min_limit )
        {
          snprintf(errortext, ET_SIZE, "Error in input parameter %s. Check configuration file. Value should not be smaller than %d.", Map[i].TokenName, (int) Map[i].min_limit);
          error (errortext, 400);
        }
        
      }
      else if (Map[i].Type == 2)
      {
        if ( * (double *) (Map[i].Place) < Map[i].min_limit )
        {
          snprintf(errortext, ET_SIZE, "Error in input parameter %s. Check configuration file. Value should not be smaller than %2.f.", Map[i].TokenName,Map[i].min_limit);
          error (errortext, 400);
        }        
      }
    }
    else if (Map[i].param_limits == 3) // Only used for QPs
    {
      if (Map[i].Type == 0)
      {
        if ( * (int *) (Map[i].Place) < (int) (Map[i].min_limit - bitdepth_qp_scale) || * (int *) (Map[i].Place) > (int) Map[i].max_limit )
        {
          snprintf(errortext, ET_SIZE, "Error in input parameter %s. Check configuration file. Value should be in [%d, %d] range.", Map[i].TokenName, (int) (Map[i].min_limit - bitdepth_qp_scale),(int)Map[i].max_limit );
          error (errortext, 400);
        }
        
      }
    }
    
    i++;
  }
  return -1;
}



/*!
***********************************************************************
* \brief
*    Outputs encoding parameters.
* \return
*    -1 for error
***********************************************************************
*/
static int DisplayEncoderParams(void)
{
  int i = 0;
  
  printf("******************************************************\n");
  printf("*               Encoder Parameters                   *\n");
  printf("******************************************************\n");
  while (Map[i].TokenName != NULL)
  {
    if (Map[i].Type == 0)
      printf("Parameter %s = %d\n",Map[i].TokenName,* (int *) (Map[i].Place));
    else if (Map[i].Type == 1)
      printf("Parameter %s = ""%s""\n",Map[i].TokenName,(char *)  (Map[i].Place));
    else if (Map[i].Type == 2)
      printf("Parameter %s = %.2f\n",Map[i].TokenName,* (double *) (Map[i].Place));
    i++;
  }
  printf("******************************************************\n");
  return -1;
}

/*!
************************************************************************
* \brief
*    calculate Ceil(Log2(uiVal))
************************************************************************
*/
unsigned CeilLog2( unsigned uiVal)
{
  unsigned uiTmp = uiVal-1;
  unsigned uiRet = 0;
  
  while( uiTmp != 0 )
  {
    uiTmp >>= 1;
    uiRet++;
  }
  return uiRet;
}


/*!
***********************************************************************
* \brief
*    Checks the input parameters for consistency.
***********************************************************************
*/
static void PatchInp (void)
{
  int bitdepth_qp_scale = 6*(input->BitDepthLuma - 8);
  
  // These variables are added for FMO
  FILE * sgfile=NULL;
  int i,j;
  int frame_mb_only;
  int mb_width, mb_height, mapunit_height;
  int storedBplus1;
  
  TestEncoderParams(bitdepth_qp_scale);
  
  if (input->FrameRate == 0.0)
    input->FrameRate = INIT_FRAME_RATE;
  
  // Set block sizes
  
  // Skip/Direct16x16
  input->part_size[0][0] = 4;
  input->part_size[0][1] = 4;
  // 16x16
  input->part_size[1][0] = 4;
  input->part_size[1][1] = 4;
  // 16x8
  input->part_size[2][0] = 4;
  input->part_size[2][1] = 2;
  // 8x16
  input->part_size[3][0] = 2;
  input->part_size[3][1] = 4;
  // 8x8
  input->part_size[4][0] = 2;
  input->part_size[4][1] = 2;
  // 8x4
  input->part_size[5][0] = 2;
  input->part_size[5][1] = 1;
  // 4x8
  input->part_size[6][0] = 1;
  input->part_size[6][1] = 2;
  // 4x4
  input->part_size[7][0] = 1;
  input->part_size[7][1] = 1;
  
  for (j = 0; j<8;j++)
  {
    for (i = 0; i<2; i++) 
    {
      input->blc_size[j][i] = input->part_size[j][i] * BLOCK_SIZE;
    }
  }
  
  // set proper log2_max_frame_num_minus4.
  storedBplus1 = (input->BRefPictures ) ? input->successive_Bframe + 1: 1;
  
  if (input->Log2MaxFNumMinus4 == -1)
    log2_max_frame_num_minus4 = Clip3(0,12, (int) (CeilLog2(input->no_frames * storedBplus1) - 4));
  else 
    log2_max_frame_num_minus4 = input->Log2MaxFNumMinus4;
  
  if (log2_max_frame_num_minus4 == 0 && input->num_ref_frames == 16)
  {
    snprintf(errortext, ET_SIZE, " NumberReferenceFrames=%d and Log2MaxFNumMinus4=%d may lead to an invalid value of frame_num.", input->num_ref_frames, input-> Log2MaxFNumMinus4);
    error (errortext, 500);
  } 
  
  // set proper log2_max_pic_order_cnt_lsb_minus4.
  if (input->Log2MaxPOCLsbMinus4 == - 1)
    log2_max_pic_order_cnt_lsb_minus4 = Clip3(0,12, (int) (CeilLog2( 2*input->no_frames * (input->jumpd + 1)) - 4));
  else 
    log2_max_pic_order_cnt_lsb_minus4 = input->Log2MaxPOCLsbMinus4;
  
  if (((1<<(log2_max_pic_order_cnt_lsb_minus4 + 3)) < input->jumpd * 4) && input->Log2MaxPOCLsbMinus4 != -1)
    error("log2_max_pic_order_cnt_lsb_minus4 might not be sufficient for encoding. Increase value.",400);
  
  // B picture consistency check
  if(input->successive_Bframe > input->jumpd)
  {
    snprintf(errortext, ET_SIZE, "Number of B-frames %d can not exceed the number of frames skipped", input->successive_Bframe);
    error (errortext, 400);
  }
  
  // Direct Mode consistency check
  if(input->successive_Bframe && input->direct_spatial_mv_pred_flag != DIR_SPATIAL && input->direct_spatial_mv_pred_flag != DIR_TEMPORAL)
  {
    snprintf(errortext, ET_SIZE, "Unsupported direct mode=%d, use TEMPORAL=0 or SPATIAL=1", input->direct_spatial_mv_pred_flag);
    error (errortext, 400);
  }
  
  if (input->PicInterlace>0 || input->MbInterlace>0)
  {
    if (input->directInferenceFlag==0)
      printf("\nDirectInferenceFlag set to 1 due to interlace coding.");
    input->directInferenceFlag=1;
  }
  
  if (input->PicInterlace>0)
  {
    if (input->IntraBottom!=0 && input->IntraBottom!=1)
    {
      snprintf(errortext, ET_SIZE, "Incorrect value %d for IntraBottom. Use 0 (disable) or 1 (enable).", input->IntraBottom);
      error (errortext, 400);
    }
  } 
  // Cabac/UVLC consistency check
  if (input->symbol_mode != UVLC && input->symbol_mode != CABAC)
  {
    snprintf (errortext, ET_SIZE, "Unsupported symbol mode=%d, use UVLC=0 or CABAC=1",input->symbol_mode);
    error (errortext, 400);
  }
  
  // Open Files
  if ((p_in=open(input->infile, OPENFLAGS_READ))==-1)
  {
    snprintf(errortext, ET_SIZE, "Input file %s does not exist",input->infile);
    error (errortext, 500);
  }
  
#ifdef USE_POST_FILTER
  if(input->UsePostFilter)
  {
    if ((p_dec=open("prepost.yuv", OPENFLAGS_WRITE, OPEN_PERMISSIONS))==-1)
    {
      snprintf(errortext, ET_SIZE, "Error open file %s", "prepost.yuv");
      error (errortext, 500);
    }
    if (strlen (input->ReconFile) > 0 && (p_post_dec=open(input->ReconFile, OPENFLAGS_WRITE, OPEN_PERMISSIONS))==-1)
    {
      snprintf(errortext, ET_SIZE, "Error open file %s", input->ReconFile);
      error (errortext, 500);
    }
  }
  else
  {
    if (strlen (input->ReconFile) > 0 && (p_dec=open(input->ReconFile, OPENFLAGS_WRITE, OPEN_PERMISSIONS))==-1)
    {
      snprintf(errortext, ET_SIZE, "Error open file %s", input->ReconFile);
      error (errortext, 500);
    }
  }
#else
  if (strlen (input->ReconFile) > 0 && (p_dec=open(input->ReconFile, OPENFLAGS_WRITE, OPEN_PERMISSIONS))==-1)
  {
    snprintf(errortext, ET_SIZE, "Error open file %s", input->ReconFile);
    error (errortext, 500);
  }
#endif
  
#if TRACE
  if (strlen (input->TraceFile) > 0 && (p_trace=fopen(input->TraceFile,"w"))==NULL)
  {
    snprintf(errortext, ET_SIZE, "Error open file %s", input->TraceFile);
    error (errortext, 500);
  }
#endif
  
  if (input->img_width % 16 != 0)
  {
    img->auto_crop_right = 16-(input->img_width % 16);
  }
  else
  {
    img->auto_crop_right=0;
  }
  if (input->PicInterlace || input->MbInterlace)
  {
    if (input->img_height % 2 != 0)
    {
      error ("even number of lines required for interlaced coding", 500);
    }
    if (input->img_height % 32 != 0)
    {
      img->auto_crop_bottom = 32-(input->img_height % 32);
    }
    else
    {
      img->auto_crop_bottom=0;
    }
  }
  else
  {
    if (input->img_height % 16 != 0)
    {
      img->auto_crop_bottom = 16-(input->img_height % 16);
    }
    else
    {
      img->auto_crop_bottom=0;
    }
  }
  if (img->auto_crop_bottom || img->auto_crop_right)
  {
    fprintf (stderr, "Warning: Automatic cropping activated: Coded frame Size: %dx%d\n", input->img_width+img->auto_crop_right, input->img_height+img->auto_crop_bottom);
  }
  
  if ((input->slice_mode==1)&&(input->MbInterlace!=0))
  {
    if ((input->slice_argument%2)!=0)
    {
      fprintf ( stderr, "Warning: slice border within macroblock pair. ");
      if (input->slice_argument > 1)
      {
        input->slice_argument--;
      }
      else
      {
        input->slice_argument++;
      }
      fprintf ( stderr, "Using %d MBs per slice.\n", input->slice_argument);
    }
  }
  /*
  // add check for MAXSLICEGROUPIDS
  if(input->num_slice_groups_minus1>=MAXSLICEGROUPIDS)
  {
  snprintf(errortext, ET_SIZE, "num_slice_groups_minus1 exceeds MAXSLICEGROUPIDS");
  error (errortext, 500);
  }
  */
  
  // Following codes are to read slice group configuration from SliceGroupConfigFileName for slice group type 0,2 or 6
  if( (input->num_slice_groups_minus1!=0)&&
    ((input->slice_group_map_type == 0) || (input->slice_group_map_type == 2) || (input->slice_group_map_type == 6)) )
  { 
    if (strlen (input->SliceGroupConfigFileName) > 0 && (sgfile=fopen(input->SliceGroupConfigFileName,"r"))==NULL)
    {
      snprintf(errortext, ET_SIZE, "Error open file %s", input->SliceGroupConfigFileName);
      error (errortext, 500);
    }
    else
    {
      if (input->slice_group_map_type == 0) 
      {
        input->run_length_minus1=(int *)malloc(sizeof(int)*(input->num_slice_groups_minus1+1));
        if (NULL==input->run_length_minus1) 
          no_mem_exit("PatchInp: input->run_length_minus1");
        
        // each line contains one 'run_length_minus1' value
        for(i=0;i<=input->num_slice_groups_minus1;i++)
        {
          fscanf(sgfile,"%d",(input->run_length_minus1+i));
          fscanf(sgfile,"%*[^\n]");
          
        }
      }
      else if (input->slice_group_map_type == 2)
      {
        input->top_left=(int *)malloc(sizeof(int)*input->num_slice_groups_minus1);
        input->bottom_right=(int *)malloc(sizeof(int)*input->num_slice_groups_minus1);
        if (NULL==input->top_left) 
          no_mem_exit("PatchInp: input->top_left");
        if (NULL==input->bottom_right) 
          no_mem_exit("PatchInp: input->bottom_right");
        
        // every two lines contain 'top_left' and 'bottom_right' value
        for(i=0;i<input->num_slice_groups_minus1;i++)
        {
          fscanf(sgfile,"%d",(input->top_left+i));
          fscanf(sgfile,"%*[^\n]");
          fscanf(sgfile,"%d",(input->bottom_right+i));
          fscanf(sgfile,"%*[^\n]");
        }
        
      }
      else if (input->slice_group_map_type == 6)
      {
        int tmp;
        
        frame_mb_only = !(input->PicInterlace || input->MbInterlace);
        mb_width= (input->img_width+img->auto_crop_right)/16;
        mb_height= (input->img_height+img->auto_crop_bottom)/16;
        mapunit_height=mb_height/(2-frame_mb_only);
        
        input->slice_group_id=(byte * ) malloc(sizeof(byte)*mapunit_height*mb_width);
        if (NULL==input->slice_group_id) 
          no_mem_exit("PatchInp: input->slice_group_id");
        
        // each line contains slice_group_id for one Macroblock

⌨️ 快捷键说明

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