NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/service-maker/sources/modules/sample_video_feeder/sample_video_feeder.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <stdio.h>
19 #include <cuda_runtime_api.h>
20 #include <cuda.h>
21 
22 #include "data_feeder.hpp"
23 
24 namespace deepstream {
25 
26 class FileDataSource : public DataFeeder::IDataProvider {
27  public:
28  virtual ~FileDataSource() {
29  if (fp_) {
30  fclose(fp_);
31  }
32  if (buffer_) {
33  free(buffer_);
34  }
35  }
36 
37  virtual Buffer read(DataFeeder& feeder, unsigned int size, bool& eos) {
38  if (fp_ == nullptr) {
39  std::string location;
40  feeder.getProperty("location", location);
41  fp_ = fopen(location.c_str(), "r");
42  if (fp_ == nullptr) {
43  fprintf(stderr, "Failed to open file %s\n", location.c_str());
44  eos = true;
45  return Buffer();
46  }
47  }
48  do {
49  int gpu_id = 0;
50  bool use_gpu_memory = false;
51  bool use_external_memory = true;
52  int width = 0;
53  int height = 0;
54  std::string format;
56  feeder.getProperty(
57  "use-gpu-memory", use_gpu_memory, "use-external-memory", use_external_memory,
58  "frame-width", width, "frame-height", height, "format", format,
59  "gpu-id", gpu_id);
60  unsigned int frame_size = 0;
61  if (format == "RGBA") {
62  color_format = NVBUF_COLOR_FORMAT_RGBA;
63  frame_size = width*height*4;
64  } else if (format == "I420" || format == "NV12") {
65  color_format = NVBUF_COLOR_FORMAT_YUV420;
66  frame_size = width*height*1.5;
67  } else {
68  color_format = NVBUF_COLOR_FORMAT_NV12;
69  frame_size = size;
70  use_gpu_memory = false;
71  }
72  if (frame_size <= 0) {
73  fprintf(stderr, "Invalid frame size %u\n", frame_size);
74  break;
75  } else if (frame_size > size) {
76  // does the hint of data size matter?
77  fprintf(stderr, "Frame size is larger than asked %u vs %u\n", frame_size, size);
78  }
79 
80  if (use_gpu_memory) {
81  // read data to the buffer and then copy it to the GPU memory
82  if (buffer_ == nullptr) {
83  buffer_ = malloc(frame_size);
84  }
85  if (0 == fread (buffer_, 1, frame_size, fp_)) break;
86  if (use_external_memory) {
87  void *cuda_device_data;
88  if (cudaMalloc ((void **) &cuda_device_data, frame_size) != cudaSuccess) {
89  fprintf(stderr, "ERROR !! Unable to allocate device memory. \n");
90  break;
91  }
92  if (cudaMemcpy (cuda_device_data, buffer_, frame_size,
93  cudaMemcpyHostToDevice) != cudaSuccess) {
94  fprintf(stderr, "Unable to copy between device and host memories. \n");
95  break;
96  }
97  return VideoBuffer(width, height, color_format, NVBUF_MEM_CUDA_DEVICE, cuda_device_data, gpu_id);
98  } else {
99  VideoBuffer buffer(width, height, color_format, NVBUF_MEM_CUDA_DEVICE);
100  if (!buffer.write(
101  [&](void* data, size_t len) {
102  if (len != frame_size) {
103  return (size_t)0;
104  }
105  cudaMemcpy(data, buffer_, len, cudaMemcpyHostToDevice);
106  return len;
107  }
108  )) break;
109  return buffer;
110  }
111  } else {
112  if (use_external_memory) {
113  void* data = malloc(frame_size);
114  if (0 == fread (data, 1, frame_size, fp_)) break;
115  return Buffer(frame_size, data);
116  } else {
117  Buffer buffer(frame_size);
118  if (!buffer.write(
119  [&](void* data, size_t len){
120  return fread (data, 1, frame_size, fp_);
121  }
122  )) break;
123  return buffer;
124  }
125  }
126  } while (false);
127  // empty buffer returned on errors.
128  eos = true;
129  return Buffer();
130  }
131  private:
132  FILE *fp_ = nullptr;
133  void* buffer_ = nullptr;
134 };
135 }
deepstream::DataFeeder
A specific signal handler for feeding data.
Definition: service-maker/includes/data_feeder.hpp:44
deepstream::FileDataSource::read
virtual Buffer read(DataFeeder &feeder, unsigned int size, bool &eos)
Definition: 9.1/service-maker/sources/modules/sample_video_feeder/sample_video_feeder.hpp:37
NVBUF_MEM_CUDA_DEVICE
@ NVBUF_MEM_CUDA_DEVICE
Specifies CUDA Device memory type.
Definition: sources/includes/nvbufsurface.h:359
deepstream::VideoBuffer::write
virtual size_t write(std::function< size_t(void *data, size_t len)>)
NVBUF_COLOR_FORMAT_INVALID
@ NVBUF_COLOR_FORMAT_INVALID
Specifies an invalid color format.
Definition: sources/includes/nvbufsurface.h:108
deepstream
Definition: service-maker/includes/buffer.hpp:38
deepstream::Buffer
Base class of a buffer.
Definition: service-maker/includes/buffer.hpp:51
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: sources/includes/nvbufsurface.h:120
NVBUF_COLOR_FORMAT_RGBA
@ NVBUF_COLOR_FORMAT_RGBA
Specifies RGBA-8-8-8-8 single plane.
Definition: sources/includes/nvbufsurface.h:146
deepstream::VideoBuffer
Definition: service-maker/includes/buffer.hpp:177
deepstream::FileDataSource::~FileDataSource
virtual ~FileDataSource()
Definition: 9.1/service-maker/sources/modules/sample_video_feeder/sample_video_feeder.hpp:28
deepstream::Object::getProperty
Object & getProperty(const std::string &name, T &value, Args &... args)
Template for getting multiple properties.
Definition: service-maker/includes/object.hpp:176
NVBUF_COLOR_FORMAT_YUV420
@ NVBUF_COLOR_FORMAT_YUV420
Specifies BT.601 colorspace - YUV420 multi-planar.
Definition: sources/includes/nvbufsurface.h:112
NvBufSurfaceColorFormat
NvBufSurfaceColorFormat
Defines color formats for NvBufSurface.
Definition: sources/includes/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:31