bool DecodeMultipleAVClipsFromVideo()

in ops/av_decoder.cc [780:860]


bool DecodeMultipleAVClipsFromVideo(
    const char* video_buffer,
    const std::string& video_filename,
    const int encoded_size,
    const Params& params,
    const int start_frm,
    const int clip_per_video,
    const bool use_local_file,
    int& height,
    int& width,
    std::vector<unsigned char*>& buffer_rgb,
    std::vector<float>& buffer_audio,
    int& number_of_frames,
    int& clip_start_frame
) {
  std::vector<std::unique_ptr<DecodedFrame>> sampledFrames;
  std::vector<float> sampledAudio;
  AVDecoder decoder;

  CallbackImpl callback;
  // decoding from buffer or file
  if (!use_local_file) {
    decoder.decodeMemory(string("Memory Buffer"), video_buffer,
        encoded_size, params, start_frm, callback, number_of_frames);
  } else {
    decoder.decodeFile(
      video_filename, params, start_frm, callback, number_of_frames);
  }
  for (auto& frame : callback.frames) {
    sampledFrames.push_back(move(frame));
  }
  int n=0;
  for (auto sample : callback.audio_samples) {
    buffer_audio.push_back(sample);
    n++;
  }

  for (int i = 0; i < buffer_rgb.size(); i++) {
    unsigned char* buff = buffer_rgb[i];
    delete []buff;
  }
  buffer_rgb.clear();

  if (params.getVideo_) {
    if (sampledFrames.size() < params.num_of_required_frame_) {
      // LOG(ERROR) << "The video seems faulty and we could not decode enough "
      //  << " frames: sampledFrames.size() << " VS "
      //  << params.num_of_required_frame_;
      FreeAVDecodedData(sampledFrames, sampledAudio);
      return true;
    }
    if (sampledFrames.size() == 0) {
      LOG(ERROR) << "The samples frames have size 0, no frame to process";
      FreeAVDecodedData(sampledFrames, sampledAudio);
      return true;
    }
    height = sampledFrames[0]->height_;
    width = sampledFrames[0]->width_;
    clip_start_frame = sampledFrames[0]->index_;
    float
        sample_stepsz = (clip_per_video <= 1) ? 0 : (float(sampledFrames.size()
        - params.num_of_required_frame_) / (clip_per_video - 1));

    int image_size = 3 * height * width;
    int clip_size = params.num_of_required_frame_ * image_size;
    // get the RGB frames for each clip
    for (int i = 0; i < clip_per_video; i++) {
      unsigned char* buffer_rgb_ptr = new unsigned char[clip_size];
      int clip_start = floor(i * sample_stepsz);
      for (int j = 0; j < params.num_of_required_frame_; j++) {
        memcpy(buffer_rgb_ptr + j * image_size,
            (unsigned char*) sampledFrames[j + clip_start]->data_.get(),
            image_size * sizeof(unsigned char));
      }
      buffer_rgb.push_back(buffer_rgb_ptr);
    }
  }
  FreeAVDecodedData(sampledFrames, sampledAudio);

  return true;
}