/**
    \fn ctor
*/
changeFps::changeFps(  ADM_coreVideoFilter *previous,CONFcouple *setup) : ADM_coreVideoFilter(previous,setup)
{
    if(!setup || !ADM_paramLoad(setup,confChangeFps_param,&configuration))
    {
        // Default value
        configuration.newMode=0;
        configuration.newFpsNum=ADM_Fps1000FromUs(previous->getInfo()->frameIncrement);
        configuration.newFpsDen=1000;
        configuration.oldMode=0;
        configuration.oldFpsNum=ADM_Fps1000FromUs(previous->getInfo()->frameIncrement);
        configuration.oldFpsDen=1000;

    }
    updateTimingInfo();
}
/**
    \fn configure

*/
bool         swScaleResizeFilter::configure(void) 
{
    uint32_t fps1000=ADM_Fps1000FromUs(info.frameIncrement);
    if(true==DIA_resize(previousFilter->getInfo()->width,previousFilter->getInfo()->height,
                        fps1000,&configuration))
    {
       
        reset(configuration.width,configuration.height,configuration.algo);
        return true;
    }   
    return false;
}
/**
    \fn ctor
*/
resampleFps::resampleFps(  ADM_coreVideoFilter *previous,CONFcouple *setup) : 
        ADM_coreVideoFilterCached(3,previous,setup)
{
    baseTime=0;
    prefillDone=false;
    frames[0]=frames[1]=NULL;
    if(!setup || !ADM_paramLoad(setup,confResampleFps_param,&configuration))
    {
        // Default value
        configuration.mode=0;
        configuration.newFpsNum=ADM_Fps1000FromUs(previous->getInfo()->frameIncrement);
        configuration.newFpsDen=1000;
    }
    if(!frames[0]) frames[0]=new ADMImageDefault(info.width,info.height);
    if(!frames[1]) frames[1]=new ADMImageDefault(info.width,info.height);
    updateIncrement();
}
/**
    \fn updateTimingInfo
    \brief update the info part with new fps
*/
bool changeFps::updateTimingInfo(void)
{
    
    double fps1000=configuration.newFpsNum*1000;
    fps1000/=configuration.newFpsDen;
    // 1 update frame increment...
    info.frameIncrement=ADM_Fps1000FromUs( (uint64_t)fps1000);

    // 2 update duration
    double timing=previousFilter->getInfo()->totalDuration;
    timing*=configuration.oldFpsNum;
    timing*=configuration.newFpsDen;
    timing/=configuration.newFpsNum;
    timing/=configuration.oldFpsDen;
    info.totalDuration=(uint64_t)timing;
    return true;
}
bool avs_start(FilterInfo *info, FilterInfo *avisynth_info,
               char *fname, AVS_PIPES *avs_pipes)
{
 DEBUG_PRINTF("avsfilter : avs_start()\n");
 DEBUG_PRINTF("avsfilter : %X %X %s %X\n",
              avs_pipes[PIPE_LOADER_WRITE].hpipe,
              avs_pipes[PIPE_FILTER_WRITE].hpipe,
              fname, info);
 DEBUG_PRINTF("avsfilter : avs_start info : frameIncrement %lu totalDuration %llu\n",
              info->frameIncrement, info->totalDuration);

 ADV_Info aii, aio;
 aii.width = info->width;
 aii.height = info->height;
 aii.nb_frames = info->totalDuration / info->frameIncrement;
 aii.encoding = 1;
 aii.codec = 0;
 aii.fps1000 = ADM_Fps1000FromUs(info->frameIncrement);
 aii.orgFrame = 0;
 DEBUG_PRINTF("avsfilter : send ADV_Info to avsloader [fps1000 = %d, nb_frames = %d]\n", aii.fps1000, aii.nb_frames);
 if (!send_cmd(avs_pipes[PIPE_LOADER_WRITE].hpipe,
                LOAD_AVS_SCRIPT, fname,
                strlen(fname) + sizeof("\0")) ||
      !send_cmd(avs_pipes[PIPE_FILTER_WRITE].hpipe,
                SET_CLIP_PARAMETER, &aii,
                sizeof(aii)))
  {
    DEBUG_PRINTF_RED("avsfilter : cannot set script name or set clip parameters\n");
    deinit_pipes(avs_pipes, CMD_PIPE_NUM);
    return false;
  }

  // get avisynth frame info
  PIPE_MSG_HEADER msg;
  if (!receive_cmd(avs_pipes[PIPE_LOADER_READ].hpipe,
                   &msg) ||
      msg.avs_cmd != SET_CLIP_PARAMETER ||
      !receive_data(avs_pipes[PIPE_LOADER_READ].hpipe,
                    &msg, &aio))
  {
    DEBUG_PRINTF_RED("avsfilter : cannot receive avisynth clip parameters\n");
    deinit_pipes(avs_pipes, CMD_PIPE_NUM);
    return false;
  }

  DEBUG_PRINTF("avsfilter : receive ADV_Info from avsloader [fps1000 = %d, nb_frames = %d]\n", aio.fps1000, aio.nb_frames);
  avisynth_info->width = aio.width;
  avisynth_info->height = aio.height;
  avisynth_info->frameIncrement = ADM_UsecFromFps1000(aio.fps1000);
  avisynth_info->totalDuration = aio.nb_frames * avisynth_info->frameIncrement;

  // correct avisynth_info for span of frames, calculate fps change metrics
/*  float k_fps;
  k_fps = float(avisynth_info->frameIncrement) / float(info->frameIncrement);
  DEBUG_PRINTF("avsfilter : FPS change metrics %f\n", k_fps);
  avisynth_info->nb_frames = int (info->nb_frames * k_fps);
  avisynth_info->orgFrame = int (info->orgFrame * k_fps);
  DEBUG_PRINTF("avsfilter : Calculate new span for avisynth script [%d - %d]\n",
               avisynth_info->orgFrame,avisynth_info->orgFrame + avisynth_info->nb_frames);*/
  return true;
}