Fixposition SDK 0.0.0-heads/main-0-g6a396b9
Collection of c++ libraries and apps for use with Fixposition products on Linux
Loading...
Searching...
No Matches
video.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 * \endverbatim
9 *
10 * @file
11 * @brief Fixposition SDK: Video frame decoding
12 *
13 * @page FPSDK_COMMON_VIDEO Video frame decoding
14 *
15 * **API**: fpsdk_common/video.hpp and fpsdk::common::video
16 *
17 */
18#ifndef __FPSDK_COMMON_VIDEO_HPP__
19#define __FPSDK_COMMON_VIDEO_HPP__
20
21/* LIBC/STL */
22#include <cstdint>
23#include <memory>
24#include <optional>
25#include <string>
26
27/* EXTERNAL */
28
29/* PACKAGE */
30
31namespace fpsdk {
32namespace common {
33/**
34 * @brief Video frame decoding
35 */
36namespace video {
37/* ****************************************************************************************************************** */
38#if FPSDK_USE_FFMPEG
39
40/**
41 * @brief Video codec
42 */
43enum class VideoCodec : int
44{
45 UNSPECIFIED = 0, //!< Unspecified
46 H264, //!< H.264 Advanced Video Coding (AVC)
47 H265, //!< H.265 High Efficiency Video Coding (HEVC)
48};
49
50/**
51 * @brief Stringify video codec enum
52 *
53 * @param[in] codec Codec ID
54 *
55 * @returns the stringification of the codec, or "?" for bad enum values
56 */
57const char* VideoCodecToStr(const VideoCodec codec);
58
59/**
60 * @brief Convert codec ID string to enum
61 *
62 * @param[in] str Codec ID string
63 * @param[in] def Default codec ID, for bad str
64 *
65 * @returns the codec ID, or def if str was invalid
66 */
67VideoCodec VideoCodecFromStrOr(const char* str, const VideoCodec def);
68
69/**
70 * @brief Pixel format
71 */
72enum class PixelFmt : int
73{
74 UNSPECIFIED = 0, //!< Invalid
75 Y8, //!< Planar Y (greyscale), 8bpp (= FFmpeg AV_PIX_FMT_GRAY8)
76 RGB24, //!< Packed RGB 8:8:8, 24bpp, RGBRGB... (= FFmpeg AV_PIX_FMT_RGB24)
77 GBRP, //!< Planar GBR, 24bpp [R][R][R]...[G][G][G]...[B][B][B]... (~ FFmpeg AV_PIX_FMT_GBRP/AV_PIX_FMT_GBR24P)
78};
79
80/**
81 * @brief Stringify pixel format enum
82 *
83 * @param[in] fmt Pixel format
84 *
85 * @returns the stringification of the fmt, or "?" for bad enum values
86 */
87const char* PixelFmtToStr(const PixelFmt fmt);
88
89/**
90 * @brief Convert pixel format string to enum
91 *
92 * @param[in] str Pixel format string
93 * @param[in] def Default pixel format, for bad str
94 *
95 * @returns the pixel format, or def if str was invalid
96 */
97PixelFmt PixelFmtFromStrOr(const char* str, const PixelFmt def);
98
99/**
100 * @brief Scaling method (only for sw decoding)
101 */
102enum class ScalingQual : int
103{
104 UNSPECIFIED = 0, //!< Unspecified
105 BILINEAR, //!< Low quality, very fast
106 BICUBIC, //!< Medium/high quality, fast (B=0.0, C=0.5, "Catmull-Rom")
107 LANCZOS, //!< High quality, slower (radius=3.0)
108 SINC, //!< Very high quality, very slow (radius=3.0)
109};
110
111/**
112 * @brief Stringify scaling method enum
113 *
114 * @param[in] qual Scaling method
115 *
116 * @returns the stringification of the qual, or "?" for bad enum values
117 */
118const char* ScalingQualToStr(const ScalingQual qual);
119
120/**
121 * @brief Convert scaling method string to enum
122 *
123 * @param[in] str Scaling method string
124 * @param[in] def Default pixel format, for bad str
125 *
126 * @returns the scaling method, or def if str was invalid
127 */
128ScalingQual ScalingQualFromStrOr(const char* str, const ScalingQual def);
129
130/**
131 * @brief Hw acceleration
132 */
133enum class HwAccel : uint8_t
134{
135 UNSPECIFIED = 0, //!< Unspecified
136 AUTO = 1, //!< Use hw accel if possible, fallback to SW
137 SW = 2, //!< Do not use any hw acceleration, use pure software implementation
138 HW = 3, //!< Use hw acceleration (currently, Linux Video Acceleration API, VA-API)
139};
140
141/**
142 * @brief Stringify hw accel method enum
143 *
144 * @param[in] accel HW acceleration
145 *
146 * @returns the stringification of the accel, or "?" for bad enum values
147 */
148const char* HwAccelToStr(const HwAccel accel);
149
150/**
151 * @brief Convert scaling method string to enum
152 *
153 * @param[in] str HW acceleration string
154 * @param[in] def Default HW acceleration, for bad str
155 *
156 * @returns the HW acceleration, or def if str was invalid
157 */
158HwAccel HwAccelFromStrOr(const char* str, const HwAccel def);
159
160/**
161 * @brief VideoFrameDecoder params and their defaults
162 */
164{ // clang-format off
165 std::string name_; //!< Name (for debug logging)
167 PixelFmt fmt_ = PixelFmt::UNSPECIFIED; //!< Pixel format (of decoded image)
168 double scale_ = 1.0; //!< Scale factor (of decoded image), range 0.1-1.0
169 HwAccel accel_ = HwAccel::AUTO; //!< Hw acceleration
171}; // clang-format on
172
173// ---------------------------------------------------------------------------------------------------------------------
174
175/**
176 * @brief Decoded, converted and scaled image
177 */
179{
180 PixelFmt fmt_ = PixelFmt::UNSPECIFIED; //!< Pixel format (of data_)
181 int width_ = 0; //!< Width [px]
182 int height_ = 0; //!< Height [px]
183 std::vector<uint8_t> data_; //!< Image data, memory layout depends on fmt_
184
185# ifndef NDEBUG
186 double t_dec_ = 0.0;
187 double t_conv_ = 0.0;
188# endif
189};
190
191/**
192 * @brief Helper for decoding video frames
193 *
194 * Note that separate instances of this should be used to process different video streams.
195 */
197{
198 public:
199 /**
200 * @brief Constructor
201 *
202 * Don't use, use the CreateVideoFrameDecoder() factory function.
203 *
204 * @param[in] params The parameters
205 */
207
208 /**
209 * @brief Destructor
210 */
212
213 /**
214 * @brief Initialise video decoder
215 *
216 * @returns true on success, false otherwise (bad params, failed to initialse stuff, ...)
217 */
218 bool Init();
219
220 /**
221 * @brief Decode one frame
222 *
223 * - This works for data that is available frame by frame. That is, it does not work for regular video streams where
224 * the data is chunked arbitrarily. Ideally, start by supplying an I frame incl. all the necessary NAL units
225 * (NALUs) to decode a first frame. After that, the following P frames should decode fine.
226 * - For example, for HEVC (H.265) a full set of data for a I frame should contain:
227 * - NALU type 32 (VPS, Video Parameter Set)
228 * - NALU type 33 (SPS, Sequence Parameter Set)
229 * - NALU type 34 (PPS, Picture Parameter Set)
230 * - NALU type 19 (IDR, Instantaneous Decoding Refresh, and RADL, Random Access Decodable Leading) (possibly other
231 * types 16-23 (IRAP, Intra Random Access Point) NALUs might work)
232 * - The video encoder should be configured accordingly (to repeat the VPS/SPS/PPS for each I-frame, and possibly to
233 * produce all/only I frames.
234 *
235 * @param[in] data Data for one (not more, not less) video frame
236 * @param[in] size Size of data
237 *
238 * @returns the decoded, converted and scaled image, nullptr otherwise (not enough data, bad data, ...)
239 */
240 virtual std::optional<ImageData> DecodeFrame(const uint8_t* data, const std::size_t size) = 0;
241
242 /**
243 * @brief Decode video data
244 *
245 * @param[in] data Data for one (recommended) or more video frame(s)
246 *
247 * @returns the decoded, converted and scaled image, nullptr otherwise (not enough data, bad data, ...)
248 */
249 std::optional<ImageData> DecodeFrame(const std::vector<uint8_t>& data);
250
251 /**
252 * @brief Check if decoder is in error state
253 *
254 * This is useful to check when DecodeFrame() didn't return a frame. If the decoder is in error state, it cannot
255 * be used anymore and must be discarded.
256 *
257 * @returns true if the decoder is okay, false if it is in error state
258 */
259 virtual bool IsOkay() const = 0;
260
261 protected:
263};
264
265/**
266 * @brief Pointer to a VideoFrameDecoder instance, see CreateVideoFrameDecoder()
267 */
268using VideoFrameDecoderPtr = std::unique_ptr<VideoFrameDecoder>;
269
270/**
271 * @brief Create a video frame decoder
272 *
273 * @param[in] params The parameters
274 *
275 * @returns the video frame decoder instances, or nullptr if it failed (bad params)
276 */
278
279#endif // FPSDK_USE_FFMPEG
280/* ****************************************************************************************************************** */
281} // namespace video
282} // namespace common
283} // namespace fpsdk
284#endif // __FPSDK_COMMON_VIDEO_HPP__
bool Init()
Initialise video decoder.
std::optional< ImageData > DecodeFrame(const std::vector< uint8_t > &data)
Decode video data.
VideoDecoderParams params_
Params.
Definition video.hpp:262
virtual std::optional< ImageData > DecodeFrame(const uint8_t *data, const std::size_t size)=0
Decode one frame.
VideoFrameDecoder(const VideoDecoderParams &params)
Constructor.
virtual bool IsOkay() const =0
Check if decoder is in error state.
Video frame decoding.
Definition video.hpp:36
PixelFmt
Pixel format.
Definition video.hpp:73
@ RGB24
Packed RGB 8:8:8, 24bpp, RGBRGB... (= FFmpeg AV_PIX_FMT_RGB24)
Definition video.hpp:76
@ GBRP
Planar GBR, 24bpp [R][R][R]...[G][G][G]...[B][B][B]... (~ FFmpeg AV_PIX_FMT_GBRP/AV_PIX_FMT_GBR24P)
Definition video.hpp:77
@ Y8
Planar Y (greyscale), 8bpp (= FFmpeg AV_PIX_FMT_GRAY8)
Definition video.hpp:75
const char * ScalingQualToStr(const ScalingQual qual)
Stringify scaling method enum.
HwAccel HwAccelFromStrOr(const char *str, const HwAccel def)
Convert scaling method string to enum.
PixelFmt PixelFmtFromStrOr(const char *str, const PixelFmt def)
Convert pixel format string to enum.
ScalingQual ScalingQualFromStrOr(const char *str, const ScalingQual def)
Convert scaling method string to enum.
VideoCodec
Video codec.
Definition video.hpp:44
@ H265
H.265 High Efficiency Video Coding (HEVC)
Definition video.hpp:47
@ H264
H.264 Advanced Video Coding (AVC)
Definition video.hpp:46
const char * HwAccelToStr(const HwAccel accel)
Stringify hw accel method enum.
ScalingQual
Scaling method (only for sw decoding)
Definition video.hpp:103
@ LANCZOS
High quality, slower (radius=3.0)
Definition video.hpp:107
@ SINC
Very high quality, very slow (radius=3.0)
Definition video.hpp:108
@ BILINEAR
Low quality, very fast.
Definition video.hpp:105
@ BICUBIC
Medium/high quality, fast (B=0.0, C=0.5, "Catmull-Rom")
Definition video.hpp:106
std::unique_ptr< VideoFrameDecoder > VideoFrameDecoderPtr
Pointer to a VideoFrameDecoder instance, see CreateVideoFrameDecoder()
Definition video.hpp:268
const char * VideoCodecToStr(const VideoCodec codec)
Stringify video codec enum.
const char * PixelFmtToStr(const PixelFmt fmt)
Stringify pixel format enum.
VideoCodec VideoCodecFromStrOr(const char *str, const VideoCodec def)
Convert codec ID string to enum.
VideoFrameDecoderPtr CreateVideoFrameDecoder(const VideoDecoderParams &params)
Create a video frame decoder.
HwAccel
Hw acceleration.
Definition video.hpp:134
@ HW
Use hw acceleration (currently, Linux Video Acceleration API, VA-API)
Definition video.hpp:138
@ SW
Do not use any hw acceleration, use pure software implementation.
Definition video.hpp:137
@ AUTO
Use hw accel if possible, fallback to SW.
Definition video.hpp:136
Fixposition SDK: Common library.
Definition doc.hpp:21
Fixposition SDK.
Decoded, converted and scaled image.
Definition video.hpp:179
std::vector< uint8_t > data_
Image data, memory layout depends on fmt_.
Definition video.hpp:183
PixelFmt fmt_
Pixel format (of data_)
Definition video.hpp:180
VideoFrameDecoder params and their defaults.
Definition video.hpp:164
ScalingQual qual_
Scaling method.
Definition video.hpp:170
HwAccel accel_
Hw acceleration.
Definition video.hpp:169
PixelFmt fmt_
Pixel format (of decoded image)
Definition video.hpp:167
double scale_
Scale factor (of decoded image), range 0.1-1.0.
Definition video.hpp:168
std::string name_
Name (for debug logging)
Definition video.hpp:165