NVIDIA DeepStream SDK API Reference

8.0 Release
sample_video_feeder.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 #include <stdio.h>
14 #include <cuda_runtime_api.h>
15 #include <cuda.h>
16 
17 #include "data_feeder.hpp"
18 
19 namespace deepstream {
20 
22  public:
23  virtual ~FileDataSource() {
24  if (fp_) {
25  fclose(fp_);
26  }
27  if (buffer_) {
28  free(buffer_);
29  }
30  }
31 
32  virtual Buffer read(DataFeeder& feeder, unsigned int size, bool& eos) {
33  if (fp_ == nullptr) {
34  std::string location;
35  feeder.getProperty("location", location);
36  fp_ = fopen(location.c_str(), "r");
37  if (fp_ == nullptr) {
38  fprintf(stderr, "Failed to open file %s\n", location.c_str());
39  eos = true;
40  return Buffer();
41  }
42  }
43  do {
44  int gpu_id = 0;
45  bool use_gpu_memory = false;
46  bool use_external_memory = true;
47  int width = 0;
48  int height = 0;
49  std::string format;
51  feeder.getProperty(
52  "use-gpu-memory", use_gpu_memory, "use-external-memory", use_external_memory,
53  "frame-width", width, "frame-height", height, "format", format,
54  "gpu-id", gpu_id);
55  unsigned int frame_size = 0;
56  if (format == "RGBA") {
57  color_format = NVBUF_COLOR_FORMAT_RGBA;
58  frame_size = width*height*4;
59  } else if (format == "I420" || format == "NV12") {
60  color_format = NVBUF_COLOR_FORMAT_YUV420;
61  frame_size = width*height*1.5;
62  } else {
63  color_format = NVBUF_COLOR_FORMAT_NV12;
64  frame_size = size;
65  use_gpu_memory = false;
66  }
67  if (frame_size <= 0) {
68  fprintf(stderr, "Invalid frame size %u\n", frame_size);
69  break;
70  } else if (frame_size > size) {
71  // does the hint of data size matter?
72  fprintf(stderr, "Frame size is larger than asked %u vs %u\n", frame_size, size);
73  }
74 
75  if (use_gpu_memory) {
76  // read data to the buffer and then copy it to the GPU memory
77  if (buffer_ == nullptr) {
78  buffer_ = malloc(frame_size);
79  }
80  if (0 == fread (buffer_, 1, frame_size, fp_)) break;
81  if (use_external_memory) {
82  void *cuda_device_data;
83  if (cudaMalloc ((void **) &cuda_device_data, frame_size) != cudaSuccess) {
84  fprintf(stderr, "ERROR !! Unable to allocate device memory. \n");
85  break;
86  }
87  if (cudaMemcpy (cuda_device_data, buffer_, frame_size,
88  cudaMemcpyHostToDevice) != cudaSuccess) {
89  fprintf(stderr, "Unable to copy between device and host memories. \n");
90  break;
91  }
92  return VideoBuffer(width, height, color_format, NVBUF_MEM_CUDA_DEVICE, cuda_device_data, gpu_id);
93  } else {
94  VideoBuffer buffer(width, height, color_format, NVBUF_MEM_CUDA_DEVICE);
95  if (!buffer.write(
96  [&](void* data, size_t len) {
97  if (len != frame_size) {
98  return (size_t)0;
99  }
100  cudaMemcpy(data, buffer_, len, cudaMemcpyHostToDevice);
101  return len;
102  }
103  )) break;
104  return buffer;
105  }
106  } else {
107  if (use_external_memory) {
108  void* data = malloc(frame_size);
109  if (0 == fread (data, 1, frame_size, fp_)) break;
110  return Buffer(frame_size, data);
111  } else {
112  Buffer buffer(frame_size);
113  if (!buffer.write(
114  [&](void* data, size_t len){
115  return fread (data, 1, frame_size, fp_);
116  }
117  )) break;
118  return buffer;
119  }
120  }
121  } while (false);
122  // empty buffer returned on errors.
123  eos = true;
124  return Buffer();
125  }
126  private:
127  FILE *fp_ = nullptr;
128  void* buffer_ = nullptr;
129 };
130 }
deepstream::DataFeeder::IDataProvider
required interface for a data feeder
Definition: data_feeder.hpp:47
deepstream::DataFeeder
A specific signal handler for feeding data.
Definition: data_feeder.hpp:39
deepstream::FileDataSource::read
virtual Buffer read(DataFeeder &feeder, unsigned int size, bool &eos)
Definition: sample_video_feeder.hpp:32
NVBUF_MEM_CUDA_DEVICE
@ NVBUF_MEM_CUDA_DEVICE
Specifies CUDA Device memory type.
Definition: nvbufsurface.h:341
deepstream::FileDataSource
Definition: sample_video_feeder.hpp:21
deepstream::VideoBuffer::write
virtual size_t write(std::function< size_t(void *data, size_t len)>)
data_feeder.hpp
NVBUF_COLOR_FORMAT_INVALID
@ NVBUF_COLOR_FORMAT_INVALID
Specifies an invalid color format.
Definition: nvbufsurface.h:108
deepstream
Definition: buffer.hpp:33
deepstream::Buffer
Base class of a buffer.
Definition: buffer.hpp:46
deepstream::Buffer::write
virtual size_t write(std::function< size_t(void *data, size_t len)> callable)
Write data to the buffer.
NVBUF_COLOR_FORMAT_NV12
@ NVBUF_COLOR_FORMAT_NV12
Specifies BT.601 colorspace - Y/CbCr 4:2:0 multi-planar.
Definition: nvbufsurface.h:120
NVBUF_COLOR_FORMAT_RGBA
@ NVBUF_COLOR_FORMAT_RGBA
Specifies RGBA-8-8-8-8 single plane.
Definition: nvbufsurface.h:146
deepstream::VideoBuffer
Definition: buffer.hpp:169
deepstream::FileDataSource::~FileDataSource
virtual ~FileDataSource()
Definition: sample_video_feeder.hpp:23
deepstream::Object::getProperty
Object & getProperty(const std::string &name, T &value, Args &... args)
Template for getting multiple properties.
Definition: object.hpp:171
NVBUF_COLOR_FORMAT_YUV420
@ NVBUF_COLOR_FORMAT_YUV420
Specifies BT.601 colorspace - YUV420 multi-planar.
Definition: nvbufsurface.h:112
NvBufSurfaceColorFormat
NvBufSurfaceColorFormat
Defines color formats for NvBufSurface.
Definition: nvbufsurface.h:105
ds3d::v2xinfer::format
static std::string format(const char *fmt,...)
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:26