bool DecodeClipFromVideoFileFlex()

in caffe2_customized_ops/video/customized_video_io.cc [654:731]


bool DecodeClipFromVideoFileFlex(
    std::string filename,
    const int start_frm,
    const int length,
    int & height,
    int & width,
    const int sampling_rate,
    float*& buffer,
    std::mt19937* randgen,
    const int sample_times
  ) {
  Params params;
  std::vector<std::unique_ptr<DecodedFrame>> sampledFrames;
  CustomVideoDecoder decoder;

  params.outputHeight_ = -1;
  params.outputWidth_ = -1;
  params.maximumOutputFrames_ = MAX_DECODING_FRAMES;

  // decode all frames with defaul sampling rate
  decoder.decodeFile(filename, params, sampledFrames);

  buffer = nullptr;
  int offset = 0;
  int channel_size = 0;
  int image_size = 0;
  int data_size = 0;
  CAFFE_ENFORCE_LT(1, sampledFrames.size(), "video cannot be empty");

  int use_start_frm = start_frm;
  if (start_frm < 0) { // perform temporal jittering
    if ((int)(sampledFrames.size() - length * sampling_rate) > 0) {
      use_start_frm = std::uniform_int_distribution<>(
          0, (int)(sampledFrames.size() - length * sampling_rate))(*randgen);
    } else { use_start_frm = 0; }
  }
  else
  {
    int num_of_frames = (int)(sampledFrames.size());
    float frame_gaps = (float)(num_of_frames) / (float)(sample_times);
    use_start_frm = ((int)(frame_gaps * start_frm)) % num_of_frames;
  }


  height = (int)sampledFrames[0]->height_;
  width  = (int)sampledFrames[0]->width_;

  for (int idx = 0; idx < length; idx ++){
    int i = use_start_frm + idx * sampling_rate;
    i = i % (int)(sampledFrames.size());
    if (idx == 0) {
      image_size = sampledFrames[i]->height_ * sampledFrames[i]->width_;
      channel_size = image_size * length;
      data_size = channel_size * 3;
      buffer = new float[data_size];
    }

    for (int c = 0; c < 3; c++) {
      ImageDataToBuffer(
          (unsigned char*)sampledFrames[i]->data_.get(),
          sampledFrames[i]->height_,
          sampledFrames[i]->width_,
          buffer + c * channel_size + offset,
          c);
    }
    offset += image_size;
  }
  CAFFE_ENFORCE(offset == channel_size, "Wrong offset size");

  // free the sampledFrames
  for (int i = 0; i < sampledFrames.size(); i++) {
    DecodedFrame* p = sampledFrames[i].release();
    delete p;
  }
  sampledFrames.clear();

  return true;
}