QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstHwVideoInfo.cc
Go to the documentation of this file.
1#include "GstHwVideoInfo.h"
2
3#include <algorithm>
4#include <iterator>
5
6#if GST_CHECK_VERSION(1, 24, 0)
7#include <gst/video/video-info-dma.h>
8#endif
9
10namespace GstHw {
11
12namespace {
13
14int firstComponentOf(const GstVideoInfo& info, int plane)
15{
16 gint components[GST_VIDEO_MAX_COMPONENTS];
17 gst_video_format_info_component(info.finfo, static_cast<guint>(plane), components);
18 return components[0];
19}
20
21} // namespace
22
23bool dmaDrmAwareVideoInfo(GstCaps* caps, GstVideoInfo* info)
24{
25 if (!caps || !info) {
26 return false;
27 }
28#if GST_CHECK_VERSION(1, 24, 0)
29 if (gst_video_is_dma_drm_caps(caps)) {
30 GstVideoInfoDmaDrm drmInfo;
31 gst_video_info_dma_drm_init(&drmInfo);
32 return gst_video_info_dma_drm_from_caps(&drmInfo, caps) && gst_video_info_dma_drm_to_video_info(&drmInfo, info);
33 }
34#endif
35 return gst_video_info_from_caps(info, caps);
36}
37
38bool hasCpuLayout([[maybe_unused]] GstCaps* caps)
39{
40#if GST_CHECK_VERSION(1, 24, 0)
41 if (caps && gst_video_is_dma_drm_caps(caps)) {
42 // DRM_FORMAT_MOD_LINEAR; <drm_fourcc.h> is Linux-only.
43 constexpr guint64 kLinearModifier = 0;
44 GstVideoInfoDmaDrm drmInfo;
45 gst_video_info_dma_drm_init(&drmInfo);
46 return gst_video_info_dma_drm_from_caps(&drmInfo, caps) && (drmInfo.drm_modifier == kLinearModifier);
47 }
48#endif
49 return true;
50}
51
52int planeHeight(const GstVideoInfo& info, int plane)
53{
54 const int component = firstComponentOf(info, plane);
55 if (component < 0) {
56 return 0;
57 }
58 return GST_VIDEO_INFO_COMP_HEIGHT(&info, component);
59}
60
61int planeRowBytes(const GstVideoInfo& info, int plane)
62{
63 // The furthest byte any component of the plane reaches: packed 4:2:2 (YUY2, UYVY) interleaves half-width chroma
64 // with luma, so an odd-width row ends in a chroma sample past the last luma byte.
65 gint components[GST_VIDEO_MAX_COMPONENTS];
66 gst_video_format_info_component(info.finfo, static_cast<guint>(plane), components);
67 int rowBytes = 0;
68 for (const gint component : components) {
69 if (component < 0) {
70 break;
71 }
72 const int pixelStride = GST_VIDEO_INFO_COMP_PSTRIDE(&info, component);
73 if (pixelStride <= 0) {
74 // Complex packings (e.g. v210) have no per-pixel stride; the whole stride is the safe bound.
75 return GST_VIDEO_INFO_PLANE_STRIDE(&info, plane);
76 }
77 const int sampleBytes = static_cast<int>((GST_VIDEO_FORMAT_INFO_DEPTH(info.finfo, component) +
78 GST_VIDEO_FORMAT_INFO_SHIFT(info.finfo, component) + 7U) /
79 8U);
80 const int end = GST_VIDEO_INFO_COMP_POFFSET(&info, component) +
81 ((GST_VIDEO_INFO_COMP_WIDTH(&info, component) - 1) * pixelStride) + sampleBytes;
82 rowBytes = std::max(rowBytes, end);
83 }
84 return rowBytes;
85}
86
87bool setFrameSize(GstVideoInfo& info, int width, int height)
88{
89 if ((GST_VIDEO_INFO_WIDTH(&info) == width) && (GST_VIDEO_INFO_HEIGHT(&info) == height)) {
90 return true;
91 }
92 GstVideoInfo sized;
93 if ((width <= 0) || (height <= 0) ||
94 !gst_video_info_set_format(&sized, GST_VIDEO_INFO_FORMAT(&info), static_cast<guint>(width),
95 static_cast<guint>(height))) {
96 return false;
97 }
98 info.width = width;
99 info.height = height;
100 info.size = sized.size;
101 std::copy(std::begin(sized.stride), std::end(sized.stride), std::begin(info.stride));
102 std::copy(std::begin(sized.offset), std::end(sized.offset), std::begin(info.offset));
103 return true;
104}
105
106} // namespace GstHw
Defined only in builds with the DMABuf path (QGC_HAS_GST_DMABUF_GPU_PATH) on GStreamer 1....
int planeHeight(const GstVideoInfo &info, int plane)
int planeRowBytes(const GstVideoInfo &info, int plane)
bool hasCpuLayout(GstCaps *caps)
bool dmaDrmAwareVideoInfo(GstCaps *caps, GstVideoInfo *info)
bool setFrameSize(GstVideoInfo &info, int width, int height)