Scanner C++ API
nvidia_video_decoder.h
1 /* Copyright 2016 Carnegie Mellon University, NVIDIA Corporation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include "scanner/api/kernel.h"
19 #include "scanner/util/common.h"
20 #include "scanner/util/queue.h"
21 #include "scanner/video/video_decoder.h"
22 
23 #include <cuda.h>
24 #include <cuda_runtime.h>
25 #if CUDA_VERSION >= 9000
26 #include "scanner/video/nvidia/nvcuvid.h"
27 #else
28 #include <nvcuvid.h>
29 #endif
30 
31 namespace scanner {
32 namespace internal {
33 
37  public:
38  NVIDIAVideoDecoder(int device_id, DeviceType output_type,
39  CUcontext cuda_context);
40 
42 
43  void configure(const FrameInfo& metadata) override;
44 
45  bool feed(const u8* encoded_buffer, size_t encoded_size,
46  bool discontinuity = false) override;
47 
48  bool discard_frame() override;
49 
50  bool get_frame(u8* decoded_buffer, size_t decoded_size) override;
51 
52  int decoded_frames_buffered() override;
53 
54  void wait_until_frames_copied() override;
55 
56  private:
57  static int cuvid_handle_video_sequence(void* opaque, CUVIDEOFORMAT* format);
58 
59  static int cuvid_handle_picture_decode(void* opaque,
60  CUVIDPICPARAMS* picparams);
61 
62  static int cuvid_handle_picture_display(void* opaque,
63  CUVIDPARSERDISPINFO* dispinfo);
64 
65  int device_id_;
66  DeviceType output_type_;
67  CUcontext cuda_context_;
68  static const int max_output_frames_ = 32;
69  static const int max_mapped_frames_ = 8;
70  std::vector<cudaStream_t> streams_;
71 
72  i32 frame_width_;
73  i32 frame_height_;
74  std::vector<char> metadata_packets_;
75  CUvideoparser parser_;
76  CUvideodecoder decoder_;
77 
78  i32 last_displayed_frame_;
79  volatile i32 frame_in_use_[max_output_frames_];
80  volatile i32 undisplayed_frames_[max_output_frames_];
81  volatile i32 invalid_frames_[max_output_frames_];
82 
83  std::mutex frame_queue_mutex_;
84  CUVIDPARSERDISPINFO frame_queue_[max_output_frames_];
85  i32 frame_queue_read_pos_;
86  i32 frame_queue_elements_;
87 
88  CUdeviceptr mapped_frames_[max_mapped_frames_];
89 };
90 }
91 }
VideoDecoder.
Definition: video_decoder.h:38
Definition: database.cpp:36
FrameInfo.
Definition: frame.h:34
NVIDIAVideoDecoder.
Definition: nvidia_video_decoder.h:36