<NME> nvidia_video_decoder.h
<BEF> /* Copyright 2016 Carnegie Mellon University, NVIDIA Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "scanner/api/kernel.h"
#include "scanner/util/common.h"
#include "scanner/util/queue.h"
#include "scanner/video/video_decoder.h"

#include <cuda.h>
#include <cuda_runtime.h>
#if CUDA_VERSION >= 9000
#include "scanner/video/nvidia/nvcuvid.h"
#else
#include <nvcuvid.h>
#endif

namespace scanner {
namespace internal {

///////////////////////////////////////////////////////////////////////////////
/// NVIDIAVideoDecoder
class NVIDIAVideoDecoder : public VideoDecoder {
 public:
  NVIDIAVideoDecoder(int device_id, DeviceType output_type,
                     CUcontext cuda_context);

  ~NVIDIAVideoDecoder();

  void configure(const FrameInfo& metadata) override;

  bool feed(const u8* encoded_buffer, size_t encoded_size,
            bool discontinuity = false) override;

  bool discard_frame() override;

  bool get_frame(u8* decoded_buffer, size_t decoded_size) override;

  int decoded_frames_buffered() override;

  void wait_until_frames_copied() override;

 private:
  static int cuvid_handle_video_sequence(void* opaque, CUVIDEOFORMAT* format);

  static int cuvid_handle_picture_decode(void* opaque,
                                         CUVIDPICPARAMS* picparams);

  static int cuvid_handle_picture_display(void* opaque,
                                          CUVIDPARSERDISPINFO* dispinfo);

  int device_id_;
  DeviceType output_type_;
  CUcontext cuda_context_;
  static const int max_output_frames_ = 32;
  static const int max_mapped_frames_ = 8;
  CUvideodecoder decoder_;

  Queue<CUVIDPARSERDISPINFO> frame_queue_;
  std::vector<bool> frame_in_use_;
  std::vector<CUdeviceptr> mapped_frames_;
  int last_displayed_frame_;
  int undisplayed_frames_;
  int invalid_frames_;
  int wait_for_iframe_;
};
}
  volatile i32 invalid_frames_[max_output_frames_];

  std::mutex frame_queue_mutex_;
  CUVIDPARSERDISPINFO frame_queue_[max_output_frames_];
  i32 frame_queue_read_pos_;
  i32 frame_queue_elements_;

  CUdeviceptr mapped_frames_[max_mapped_frames_];
};
}
}

<MSG> Fix discard wrong frames in NVIDIA decoder

<DFF> @@ -70,11 +70,11 @@ class NVIDIAVideoDecoder : public VideoDecoder {
   CUvideodecoder decoder_;
 
   Queue<CUVIDPARSERDISPINFO> frame_queue_;
-  std::vector<bool> frame_in_use_;
+  std::vector<u8> frame_in_use_;
   std::vector<CUdeviceptr> mapped_frames_;
   int last_displayed_frame_;
-  int undisplayed_frames_;
-  int invalid_frames_;
+  std::vector<u8> undisplayed_frames_;
+  std::vector<u8> invalid_frames_;
   int wait_for_iframe_;
 };
 }

{'commit': 'diff --git a/scanner/engine.cpp b/scanner/engine.cpp\nindex 39a0fc87..02236128 100644\n--- a/scanner/engine.cpp\n+++ b/scanner/engine.cpp\n@@ -17,7 +17,6 @@\n \n #include "scanner/evaluators/serialize.h"\n \n-#include "jpegwrapper/JPEGWriter.h"\n #include "scanner/util/common.h"\n #include "scanner/util/db.h"\n #include "scanner/util/memory.h"\n@@ -1250,9 +1249,7 @@ void run_job(storehouse::StorageConfig* config, const std::string& dataset_name,\n     if (evaluator_caps.front().can_overlap) {\n       factory_groups.push_back({evaluator_factories.front()});\n       evaluator_offset_start++;\n-    } else {\n-      main_factories.push_back(evaluator_factories.front());\n-    }\n+    } \n     main_factories.insert(main_factories.end(),\n                           evaluator_factories.begin() + evaluator_offset_start,\n                           evaluator_factories.begin() + evaluator_offset_end);\ndiff --git a/scanner/eval/pipeline_description.h b/scanner/eval/pipeline_description.h\nindex 981ba9fd..9de0a8ea 100644\n--- a/scanner/eval/pipeline_description.h\n+++ b/scanner/eval/pipeline_description.h\n@@ -46,18 +46,34 @@ struct SequenceSamples {\n  * of consecutive frames (Sampling::SequenceGather).\n  */\n struct PipelineDescription {\n-  // Columns to grab from the input job\n+  /**\n+   * @brief Columns to grab from the input table\n+   */\n   std::vector<std::string> input_columns;\n \n+  /**\n+   * @brief Sampling strategy\n+   */\n   Sampling sampling = Sampling::All;\n-  // For strided sampling\n+\n+  /**\n+   * @brief For strided sampling\n+   */\n   i32 stride;\n-  // For gather sampling\n+\n+  /**\n+   * @brief For gather sampling\n+   */\n   std::vector<PointSamples> gather_points;\n-  // For sequence gather sampling\n+\n+  /**\n+   * @brief For sequencegather sampling\n+   */\n   std::vector<SequenceSamples> gather_sequences;\n \n-  // The chain of evaluators which will be executed over the input\n+  /**\n+   * @brief The chain of evaluators which will be executed over the input\n+   */\n   std::vector<std::unique_ptr<EvaluatorFactory>> evaluator_factories;\n };\n \ndiff --git a/scanner/video/nvidia/nvidia_video_decoder.cpp b/scanner/video/nvidia/nvidia_video_decoder.cpp\nindex f60cfb4d..aac1633b 100644\n--- a/scanner/video/nvidia/nvidia_video_decoder.cpp\n+++ b/scanner/video/nvidia/nvidia_video_decoder.cpp\n@@ -40,8 +40,8 @@ NVIDIAVideoDecoder::NVIDIAVideoDecoder(int device_id, DeviceType output_type,\n       frame_in_use_(max_output_frames_, false),\n       mapped_frames_(max_mapped_frames_, 0),\n       last_displayed_frame_(-1),\n-      undisplayed_frames_(0),\n-      invalid_frames_(0) {\n+      undisplayed_frames_(max_output_frames_, false),\n+      invalid_frames_(max_output_frames_, false) {\n   CUcontext dummy;\n \n   CUD_CHECK(cuCtxPushCurrent(cuda_context_));\n@@ -160,7 +160,8 @@ bool NVIDIAVideoDecoder::feed(const u8* encoded_buffer, size_t encoded_size,\n     CUD_CHECK(cuvidParseVideoData(parser_, &cupkt));\n \n     last_displayed_frame_ = -1;\n-    invalid_frames_ += undisplayed_frames_;\n+    invalid_frames_ = undisplayed_frames_;\n+    undisplayed_frames_ = std::vector<u8>(max_output_frames_, false);\n     while (discard_frame()) {\n       // Empty queue because we have a new section of frames\n     }\n@@ -287,26 +288,23 @@ int NVIDIAVideoDecoder::cuvid_handle_picture_decode(void* opaque,\n \n   CUresult result = cuvidDecodePicture(decoder.decoder_, picparams);\n   CUD_CHECK(result);\n-  decoder.undisplayed_frames_++;\n-  // printf("deocde %d\\n", picparams->CurrPicIdx);\n+  decoder.undisplayed_frames_[picparams->CurrPicIdx] = true;\n   return result == CUDA_SUCCESS;\n }\n \n int NVIDIAVideoDecoder::cuvid_handle_picture_display(\n     void* opaque, CUVIDPARSERDISPINFO* dispinfo) {\n   NVIDIAVideoDecoder& decoder = *reinterpret_cast<NVIDIAVideoDecoder*>(opaque);\n-  if (decoder.invalid_frames_ == 0) {\n+  if (decoder.invalid_frames_[dispinfo->picture_index] == false) {\n     decoder.frame_queue_.push(*dispinfo);\n     decoder.last_displayed_frame_++;\n     // printf("valid frame %d, display %d\\n", decoder.last_displayed_frame_,\n     //        dispinfo->picture_index);\n     decoder.frame_in_use_[dispinfo->picture_index] = true;\n   } else {\n-    // printf("invalid frame %d, display %d\\n", decoder.last_displayed_frame_,\n-    //        dispinfo->picture_index);\n-    decoder.invalid_frames_--;\n+    decoder.invalid_frames_[dispinfo->picture_index] = false;\n   }\n-  decoder.undisplayed_frames_--;\n+  decoder.undisplayed_frames_[dispinfo->picture_index] = false;\n   return true;\n }\n }\ndiff --git a/scanner/video/nvidia/nvidia_video_decoder.h b/scanner/video/nvidia/nvidia_video_decoder.h\nindex 386df4ab..227d5015 100644\n--- a/scanner/video/nvidia/nvidia_video_decoder.h\n+++ b/scanner/video/nvidia/nvidia_video_decoder.h\n@@ -70,11 +70,11 @@ class NVIDIAVideoDecoder : public VideoDecoder {\n   CUvideodecoder decoder_;\n \n   Queue<CUVIDPARSERDISPINFO> frame_queue_;\n-  std::vector<bool> frame_in_use_;\n+  std::vector<u8> frame_in_use_;\n   std::vector<CUdeviceptr> mapped_frames_;\n   int last_displayed_frame_;\n-  int undisplayed_frames_;\n-  int invalid_frames_;\n+  std::vector<u8> undisplayed_frames_;\n+  std::vector<u8> invalid_frames_;\n   int wait_for_iframe_;\n };\n }\n', 'commit_subject': 'Fix discard wrong frames in NVIDIA decoder', 'repo_name': 'scanner-research/scanner', 'committer': {'name': 'Alex Poms', 'email': 'fc2af9f17c7c9eab3755d1bde9c39a07f754fe11@gmail.com', 'time_sec': '1479590225', 'tz_offset': '-300', 'date': {'seconds': '1479590225'}}, 'commit_tree': 'a2356efdfc316c0599ae2042588aa2f6981adba9', 'commit_message': 'Fix discard wrong frames in NVIDIA decoder\n', 'language_name': 'Shell', 'license': 'apache-2.0', 'hunks': '@@ -70,11 +70,11 @@ class NVIDIAVideoDecoder : public VideoDecoder {\n   CUvideodecoder decoder_;\n \n   Queue<CUVIDPARSERDISPINFO> frame_queue_;\n-  std::vector<bool> frame_in_use_;\n+  std::vector<u8> frame_in_use_;\n   std::vector<CUdeviceptr> mapped_frames_;\n   int last_displayed_frame_;\n-  int undisplayed_frames_;\n-  int invalid_frames_;\n+  std::vector<u8> undisplayed_frames_;\n+  std::vector<u8> invalid_frames_;\n   int wait_for_iframe_;\n };\n }\n', 'addition_count': 3, 'deletion_count': 3, 'src_file': 'a/scanner/video/nvidia/nvidia_video_decoder.h', 'tgt_file': 'b/scanner/video/nvidia/nvidia_video_decoder.h', 'file_extension': '.h', 'before_file': '/* Copyright 2016 Carnegie Mellon University, NVIDIA Corporation\n *\n * Licensed under the Apache License, Version 2.0 (the "License");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an "AS IS" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n#pragma once\n\n#include "scanner/api/kernel.h"\n#include "scanner/util/common.h"\n#include "scanner/util/queue.h"\n#include "scanner/video/video_decoder.h"\n\n#include <cuda.h>\n#include <cuda_runtime.h>\n#if CUDA_VERSION >= 9000\n#include "scanner/video/nvidia/nvcuvid.h"\n#else\n#include <nvcuvid.h>\n#endif\n\nnamespace scanner {\nnamespace internal {\n\n///////////////////////////////////////////////////////////////////////////////\n/// NVIDIAVideoDecoder\nclass NVIDIAVideoDecoder : public VideoDecoder {\n public:\n  NVIDIAVideoDecoder(int device_id, DeviceType output_type,\n                     CUcontext cuda_context);\n\n  ~NVIDIAVideoDecoder();\n\n  void configure(const FrameInfo& metadata) override;\n\n  bool feed(const u8* encoded_buffer, size_t encoded_size,\n            bool discontinuity = false) override;\n\n  bool discard_frame() override;\n\n  bool get_frame(u8* decoded_buffer, size_t decoded_size) override;\n\n  int decoded_frames_buffered() override;\n\n  void wait_until_frames_copied() override;\n\n private:\n  static int cuvid_handle_video_sequence(void* opaque, CUVIDEOFORMAT* format);\n\n  static int cuvid_handle_picture_decode(void* opaque,\n                                         CUVIDPICPARAMS* picparams);\n\n  static int cuvid_handle_picture_display(void* opaque,\n                                          CUVIDPARSERDISPINFO* dispinfo);\n\n  int device_id_;\n  DeviceType output_type_;\n  CUcontext cuda_context_;\n  static const int max_output_frames_ = 32;\n  static const int max_mapped_frames_ = 8;\n  CUvideodecoder decoder_;\n\n  Queue<CUVIDPARSERDISPINFO> frame_queue_;\n  std::vector<bool> frame_in_use_;\n  std::vector<CUdeviceptr> mapped_frames_;\n  int last_displayed_frame_;\n  int undisplayed_frames_;\n  int invalid_frames_;\n  int wait_for_iframe_;\n};\n}\n  volatile i32 invalid_frames_[max_output_frames_];\n\n  std::mutex frame_queue_mutex_;\n  CUVIDPARSERDISPINFO frame_queue_[max_output_frames_];\n  i32 frame_queue_read_pos_;\n  i32 frame_queue_elements_;\n\n  CUdeviceptr mapped_frames_[max_mapped_frames_];\n};\n}\n}\n', 'message': 'Fix discard wrong frames in NVIDIA decoder\n', 'author_name': 'Alex Poms', 'lang': 'h'}