Logo
ApraPipes 1.0
Loading...
Searching...
No Matches
DMAAllocator.h
1#pragma once
2#include "Allocators.h"
3#include "DMAFDWrapper.h"
4#include "nvbuf_utils.h"
5#include "ImageMetadata.h"
6#include "RawImageMetadata.h"
7#include "RawImagePlanarMetadata.h"
8#include "FrameMetadataFactory.h"
9#include "ApraEGLDisplay.h"
10#include "Logger.h"
11#include <deque>
12
14{
15private:
16 std::vector<DMAFDWrapper *> mDMAFDWrapperArr;
18 NvBufferColorFormat mColorFormat;
19 EGLDisplay mEglDisplay;
21 int mWidth;
22 int mCount;
23
24 static NvBufferColorFormat getColorFormat(ImageMetadata::ImageType imageType)
25 {
26 NvBufferColorFormat colorFormat;
27 switch (imageType)
28 {
30 colorFormat = NvBufferColorFormat_UYVY;
31 break;
33 colorFormat = NvBufferColorFormat_YUYV;
34 break;
36 colorFormat = NvBufferColorFormat_ABGR32;
37 break;
39 colorFormat = NvBufferColorFormat_ARGB32;
40 break;
42 colorFormat = NvBufferColorFormat_YUV420;
43 break;
45 colorFormat = NvBufferColorFormat_NV12;
46 break;
47 default:
48 throw AIPException(AIP_FATAL, "Expected <RGBA/BGRA/UYVY/YUV420/NV12> Actual<" + std::to_string(imageType) + ">");
49 }
50
51 return colorFormat;
52 }
53
54public:
55 DMAAllocator(framemetadata_sp framemetadata) : mFreeDMACount(0), mCount(0)
56 {
57 if (!framemetadata->isSet())
58 {
59 return;
60 }
61
63
64 auto imageType = ImageMetadata::RGBA;
65
66 auto frameType = framemetadata->getFrameType();
67 switch (frameType)
68 {
70 {
71 auto inputRawMetadata = FrameMetadataFactory::downcast<RawImageMetadata>(framemetadata);
72 mWidth = inputRawMetadata->getWidth();
73 mHeight = inputRawMetadata->getHeight();
74 imageType = inputRawMetadata->getImageType();
75 }
76 break;
78 {
79 auto inputRawMetadata = FrameMetadataFactory::downcast<RawImagePlanarMetadata>(framemetadata);
80 mWidth = inputRawMetadata->getWidth(0);
81 mHeight = inputRawMetadata->getHeight(0);
82 imageType = inputRawMetadata->getImageType();
83 }
84 break;
85 default:
86 throw AIPException(AIP_FATAL, "Expected Raw Image or RAW_IMAGE_PLANAR. Actual<" + std::to_string(frameType) + ">");
87 break;
88 }
89
90 mColorFormat = getColorFormat(imageType);
91 };
92
94 {
95 for (auto wrapper : mDMAFDWrapperArr)
96 {
97 delete wrapper;
98 }
99 }
100
101 static void setMetadata(framemetadata_sp &metadata, int width, int height, ImageMetadata::ImageType imageType,size_t pitchValues[4] = nullptr, size_t offsetValues[4] = nullptr)
102 {
103 auto eglDisplay = ApraEGLDisplay::getEGLDisplay();
104 auto colorFormat = getColorFormat(imageType);
105
106 auto dmaFDWrapper = DMAFDWrapper::create(0, width, height, colorFormat, NvBufferLayout_Pitch, eglDisplay);
107 if (!dmaFDWrapper)
108 {
109 LOG_ERROR << "Failed to allocate dmaFDWrapper";
110 throw AIPException(AIP_FATAL, "Memory Allocation Failed.");
111 }
112
113 NvBufferParams fdParams;
114 if (NvBufferGetParams(dmaFDWrapper->getFd(), &fdParams))
115 {
116 throw AIPException(AIP_FATAL, "NvBufferGetParams Failed.");
117 }
118
119 LOG_DEBUG << "PixelFormat<" << fdParams.pixel_format << "> Planes<" << fdParams.num_planes << "> NvBufferSize<" << fdParams.nv_buffer_size << "> MemSize<" << fdParams.memsize << ">";
120 for (auto i = 0; i < fdParams.num_planes; i++)
121 {
122 LOG_DEBUG << "Width<" << fdParams.width[i] << "> Height<" << fdParams.height[i] << "> Pitch<" << fdParams.pitch[i] << "> Offset<" << fdParams.offset[i] << "> PSize<" << fdParams.psize[i] << "> Layout<" << fdParams.layout[i] << ">";
123 }
124
125 auto frameType = metadata->getFrameType();
126 switch (frameType)
127 {
129 {
130 int type = CV_8UC4;
131 switch (imageType)
132 {
135 type = CV_8UC4;
136 break;
139 type = CV_8UC3;
140 break;
141 default:
142 throw AIPException(AIP_FATAL, "Only Image Type accepted are UYVY or ARGB found " + std::to_string(imageType));
143 }
144 auto inputRawMetadata = FrameMetadataFactory::downcast<RawImageMetadata>(metadata);
145 RawImageMetadata rawMetadata(width, height, imageType, type, fdParams.pitch[0], CV_8U, FrameMetadata::MemType::DMABUF, false);
146 inputRawMetadata->setData(rawMetadata);
147 if(pitchValues != nullptr)
148 {
149 pitchValues[0] = fdParams.pitch[0];
150 }
151 }
152 break;
154 {
155 auto inputRawMetadata = FrameMetadataFactory::downcast<RawImagePlanarMetadata>(metadata);
156 size_t step[4] = {0, 0, 0, 0};
157 for (auto i = 0; i < fdParams.num_planes; i++)
158 {
159 step[i] = fdParams.pitch[i];
160 if(pitchValues != nullptr)
161 {
162 pitchValues[i] = fdParams.pitch[i];
163 }
164 if(offsetValues != nullptr)
165 {
166 offsetValues[i] = fdParams.offset[i];
167 }
168 }
169 RawImagePlanarMetadata rawMetadata(width, height, imageType, step, CV_8U, FrameMetadata::MemType::DMABUF);
170 inputRawMetadata->setData(rawMetadata);
171 }
172 break;
173 default:
174 throw AIPException(AIP_FATAL, "Expected Raw Image or RAW_IMAGE_PLANAR. Actual<" + std::to_string(frameType) + ">");
175 break;
176 }
177
178 delete dmaFDWrapper;
179 }
180
181 void *allocateChunks(size_t n)
182 {
183 if (mFreeDMACount == 0)
184 {
185 auto dmaFDWrapper = DMAFDWrapper::create(mCount++, mWidth, mHeight, mColorFormat, NvBufferLayout_Pitch, mEglDisplay);
186 if (!dmaFDWrapper)
187 {
188 LOG_ERROR << "Failed to allocate dmaFDWrapper";
189 throw AIPException(AIP_FATAL, "Memory Allocation Failed.");
190 }
191 mDMAFDWrapperArr.push_back(dmaFDWrapper);
192 dmaFDWrapper->tempFD = dmaFDWrapper->getFd();
194 }
195
196 auto wrapper = mDMAFDWrapperArr.front();
197 mDMAFDWrapperArr.erase(mDMAFDWrapperArr.begin());
199
200 return static_cast<void *>(wrapper);
201 }
202
203 void freeChunks(void *MemPtr, size_t n)
204 {
205 mDMAFDWrapperArr.push_back(static_cast<DMAFDWrapper *>(MemPtr));
207 }
208
210 {
211 return 1;
212 }
213};
static EGLDisplay getEGLDisplay()
Definition ApraEGLDisplay.cpp:6
Definition DMAAllocator.h:14
void freeChunks(void *MemPtr, size_t n)
Definition DMAAllocator.h:203
void * allocateChunks(size_t n)
Definition DMAAllocator.h:181
static void setMetadata(framemetadata_sp &metadata, int width, int height, ImageMetadata::ImageType imageType, size_t pitchValues[4]=nullptr, size_t offsetValues[4]=nullptr)
Definition DMAAllocator.h:101
NvBufferColorFormat mColorFormat
Definition DMAAllocator.h:18
int mCount
Definition DMAAllocator.h:22
int mFreeDMACount
Definition DMAAllocator.h:17
EGLDisplay mEglDisplay
Definition DMAAllocator.h:19
DMAAllocator(framemetadata_sp framemetadata)
Definition DMAAllocator.h:55
static NvBufferColorFormat getColorFormat(ImageMetadata::ImageType imageType)
Definition DMAAllocator.h:24
~DMAAllocator()
Definition DMAAllocator.h:93
int mHeight
Definition DMAAllocator.h:20
size_t getChunkSize()
Definition DMAAllocator.h:209
std::vector< DMAFDWrapper * > mDMAFDWrapperArr
Definition DMAAllocator.h:16
int mWidth
Definition DMAAllocator.h:21
Definition DMAFDWrapper.h:8
static DMAFDWrapper * create(int index, int width, int height, NvBufferColorFormat colorFormat, NvBufferLayout layout, EGLDisplay eglDisplay)
Definition DMAFDWrapper.cpp:9
static T * downcast(framemetadata_sp metadata)
Definition FrameMetadataFactory.h:9
@ DMABUF
Definition FrameMetadata.h:62
@ RAW_IMAGE_PLANAR
Definition FrameMetadata.h:33
@ RAW_IMAGE
Definition FrameMetadata.h:32
Definition Allocators.h:12
ImageType
Definition ImageMetadata.h:11
@ RGBA
Definition ImageMetadata.h:17
@ YUV420
Definition ImageMetadata.h:20
@ YUYV
Definition ImageMetadata.h:22
@ UYVY
Definition ImageMetadata.h:21
@ BGRA
Definition ImageMetadata.h:15
@ NV12
Definition ImageMetadata.h:23
Definition RawImageMetadata.h:10
Definition RawImagePlanarMetadata.h:8