Fixposition SDK 0.0.0-heads/main-0-ga35f963
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// clang-format off
14/**
15 * @page FPSDK_COMMON_VIDEO Video frame decoding
16 *
17 * **API**: fpsdk_common/video.hpp and fpsdk::common::video
18 *
19 * @note This is only available if compiled with FFmpeg, see @ref FPSDK_BUILD_DEPS.
20 *
21 * @section FPSDK_COMMON_VIDEO_HWACCEL Hardware vs. software decoding
22 *
23 * Hardware decoding (fpsdk::common::video::HwAccel::HW, currently VA-API) is not necessarily faster than software
24 * decoding (fpsdk::common::video::HwAccel::SW). The following are wall-clock times for "fpltool extract" of a 32 s
25 * recording with two cameras (H.265, 1024x768, 25 fps, 10 frames per GOP, 1547 frames in total) decoded to RGB24
26 * (a 3.5 GB ROS1 bag), using different numbers of parallel decoders (-j). Measured on an AMD Ryzen 7 PRO 8840HS
27 * (8 cores, 16 threads) with Radeon 780M graphics, FFmpeg 7.1, Release build, median of three runs:
28 *
29 * | Decoders | -e jsonl,raw,file,ros,cam -a SW | -e jsonl,raw,file,ros,cam -a HW | -e ros,cam -a SW | -e ros,cam -a HW |
30 * |----------|---------------------------------|---------------------------------|------------------|------------------|
31 * | -j 1 | 5.3 s | 5.9 s | 4.4 s | 5.7 s |
32 * | -j 2 | 4.7 s | 5.8 s | 4.7 s | 6.2 s |
33 * | -j 4 | 4.2 s | 6.1 s | 4.2 s | 5.6 s |
34 * | -j 8 | 4.2 s | 6.5 s | 4.0 s | 5.5 s |
35 *
36 * The following are decoding rates (average frames per second, fps) measured with a test program that only decodes
37 * (VideoFrameDecoder::DecodeFrame()) on the same machine, for the first 100 frames (10 I frames, 90 P frames) of a
38 * camera recording (H.265, 1024x768) decoded to RGB24, 10 repetitions. In "indep" mode each of the -j decoders decodes all
39 * frames (that is, -j times the work). In "gop" mode each GOP is decoded by a new decoder and the GOPs are distributed
40 * to the -j decoder threads, which is what "fpltool extract" does (with 11 GOPs this mode stops scaling at about -j 5):
41 *
42 * | Decoders | indep -a SW | indep -a HW | gop -a SW | gop -a HW |
43 * |----------|-------------|-------------|-----------|-----------|
44 * | -j 1 | 324 fps | 304 fps | 286 fps | 225 fps |
45 * | -j 2 | 521 fps | 643 fps | 476 fps | 372 fps |
46 * | -j 4 | 789 fps | 751 fps | 679 fps | 480 fps |
47 * | -j 8 | 1030 fps | 820 fps | 800 fps | 534 fps |
48 *
49 * Findings:
50 *
51 * - Software decoding is faster than hardware decoding in almost all cases. Only with 2 or 3 decoders running at the
52 * same time, each with its own continuous stream of frames, hardware decoding is slightly faster.
53 * - Decoding this kind of video is cheap on the CPU (about 1.2-1.6 ms per P frame and 8-15 ms per I frame on one core,
54 * depending on the recording). It scales with the number of decoders, but not linearly: with 8 decoders the
55 * throughput is about 3 times that of one decoder, as each decoder gets slower (for example, 1.6 ms per P frame with
56 * one decoder, 4.8 ms with 8 decoders).
57 * - The hardware decoder decodes I frames faster than software (about 3.6 ms), but P frames slower (about 3.0 ms). It
58 * is limited by getting the decoded frames back from the GPU, and the GPU does not scale with the number of decoders
59 * (the throughput stops increasing at about 800 fps with 3 decoders). The conversion to the output pixel format is
60 * done in software in both cases.
61 * - The hardware device is set up when the first frame is decoded, which costs about 10 ms (up to about 40 ms when
62 * many decoders do this at the same time). This makes hardware decoding even slower when a new decoder is used for
63 * each GOP.
64 * - The results may be different for other video (larger frames, H.264, longer GOPs), GPUs and drivers.
65 */
66// clang-format on
67#ifndef __FPSDK_COMMON_VIDEO_HPP__
68#define __FPSDK_COMMON_VIDEO_HPP__
69
70/* LIBC/STL */
71#include <cstdint>
72#include <memory>
73#include <optional>
74#include <string>
75
76/* EXTERNAL */
77
78/* PACKAGE */
79
80namespace fpsdk {
81namespace common {
82/**
83 * @brief Video frame decoding
84 */
85namespace video {
86/* ****************************************************************************************************************** */
87#if FPSDK_USE_FFMPEG || defined(_DOXYGEN_)
88
89/**
90 * @brief Video codec
91 */
92enum class VideoCodec : int
93{
94 UNSPECIFIED = 0, //!< Unspecified
95 H264, //!< H.264 Advanced Video Coding (AVC)
96 H265, //!< H.265 High Efficiency Video Coding (HEVC)
97};
98
99/**
100 * @brief Stringify video codec enum
101 *
102 * @param[in] codec Codec ID
103 *
104 * @returns the stringification of the codec, or "?" for bad enum values
105 */
106const char* VideoCodecToStr(const VideoCodec codec);
107
108/**
109 * @brief Convert codec ID string to enum
110 *
111 * @param[in] str Codec ID string
112 * @param[in] def Default codec ID, for bad str
113 *
114 * @returns the codec ID, or def if str was invalid
115 */
116VideoCodec VideoCodecFromStrOr(const char* str, const VideoCodec def);
117
118/**
119 * @brief Pixel format
120 */
121enum class PixelFmt : int
122{
123 UNSPECIFIED = 0, //!< Invalid
124 Y8, //!< Planar Y (greyscale), 8bpp (= FFmpeg AV_PIX_FMT_GRAY8)
125 RGB24, //!< Packed RGB 8:8:8, 24bpp, RGBRGB... (= FFmpeg AV_PIX_FMT_RGB24)
126 GBRP, //!< Planar GBR, 24bpp [G][G][G]...[B][B][B]...[R][R][R]... (~ FFmpeg AV_PIX_FMT_GBRP/AV_PIX_FMT_GBR24P)
127};
128
129/**
130 * @brief Stringify pixel format enum
131 *
132 * @param[in] fmt Pixel format
133 *
134 * @returns the stringification of the fmt, or "?" for bad enum values
135 */
136const char* PixelFmtToStr(const PixelFmt fmt);
137
138/**
139 * @brief Convert pixel format string to enum
140 *
141 * @param[in] str Pixel format string
142 * @param[in] def Default pixel format, for bad str
143 *
144 * @returns the pixel format, or def if str was invalid
145 */
146PixelFmt PixelFmtFromStrOr(const char* str, const PixelFmt def);
147
148/**
149 * @brief Scaling method (only for sw decoding)
150 */
151enum class ScalingQual : int
152{
153 UNSPECIFIED = 0, //!< Unspecified
154 BILINEAR, //!< Low quality, very fast
155 BICUBIC, //!< Medium/high quality, fast (B=0.0, C=0.5, "Catmull-Rom")
156 LANCZOS, //!< High quality, slower (radius=3.0)
157 SINC, //!< Very high quality, very slow (radius=3.0)
158};
159
160/**
161 * @brief Stringify scaling method enum
162 *
163 * @param[in] qual Scaling method
164 *
165 * @returns the stringification of the qual, or "?" for bad enum values
166 */
167const char* ScalingQualToStr(const ScalingQual qual);
168
169/**
170 * @brief Convert scaling method string to enum
171 *
172 * @param[in] str Scaling method string
173 * @param[in] def Default pixel format, for bad str
174 *
175 * @returns the scaling method, or def if str was invalid
176 */
177ScalingQual ScalingQualFromStrOr(const char* str, const ScalingQual def);
178
179/**
180 * @brief Hw acceleration
181 */
182enum class HwAccel : uint8_t
183{
184 UNSPECIFIED = 0, //!< Unspecified
185 AUTO = 1, //!< Use hw accel if possible, fallback to SW
186 SW = 2, //!< Do not use any hw acceleration, use pure software implementation
187 HW = 3, //!< Use hw acceleration (currently, Linux Video Acceleration API, VA-API)
188};
189
190/**
191 * @brief Stringify hw accel method enum
192 *
193 * @param[in] accel HW acceleration
194 *
195 * @returns the stringification of the accel, or "?" for bad enum values
196 */
197const char* HwAccelToStr(const HwAccel accel);
198
199/**
200 * @brief Convert scaling method string to enum
201 *
202 * @param[in] str HW acceleration string
203 * @param[in] def Default HW acceleration, for bad str
204 *
205 * @returns the HW acceleration, or def if str was invalid
206 */
207HwAccel HwAccelFromStrOr(const char* str, const HwAccel def);
208
209/**
210 * @brief VideoFrameDecoder params and their defaults
211 */
213{ // clang-format off
214 std::string name_; //!< Name (for debug logging)
216 PixelFmt fmt_ = PixelFmt::UNSPECIFIED; //!< Pixel format (of decoded image)
217 double scale_ = 1.0; //!< Scale factor (of decoded image), range 0.1-1.0
218 HwAccel accel_ = HwAccel::AUTO; //!< Hw acceleration
220}; // clang-format on
221
222// ---------------------------------------------------------------------------------------------------------------------
223
224/**
225 * @brief Decoded, converted and scaled image
226 */
228{
229 PixelFmt fmt_ = PixelFmt::UNSPECIFIED; //!< Pixel format (of data_)
230 int width_ = 0; //!< Width [px]
231 int height_ = 0; //!< Height [px]
232 std::vector<uint8_t> data_; //!< Image data, memory layout depends on fmt_
233
234# ifndef NDEBUG
235 double t_dec_ = 0.0;
236 double t_conv_ = 0.0;
237# endif
238};
239
240/**
241 * @brief Helper for decoding video frames
242 *
243 * @note This is only available if compiled with FFmpeg, see @ref FPSDK_BUILD_DEPS.
244 */
246{
247 public:
248 /**
249 * @brief Constructor
250 *
251 * Don't use, use the CreateVideoFrameDecoder() factory function.
252 *
253 * @param[in] params The parameters
254 */
255 explicit VideoFrameDecoder(const VideoDecoderParams& params);
256
257 /**
258 * @brief Destructor
259 */
261
262 /**
263 * @brief Decode one frame
264 *
265 * - This works for data that is available frame by frame. That is, it does not work for regular video streams where
266 * the data is chunked arbitrarily. Ideally, start by supplying an I frame incl. all the necessary NAL units
267 * (NALUs) to decode a first frame. After that, the following P frames should decode fine.
268 * - For example, for HEVC (H.265) a full set of data for a I frame should contain:
269 * - NALU type 32 (VPS, Video Parameter Set)
270 * - NALU type 33 (SPS, Sequence Parameter Set)
271 * - NALU type 34 (PPS, Picture Parameter Set)
272 * - NALU type 19 (IDR, Instantaneous Decoding Refresh, and RADL, Random Access Decodable Leading) (possibly other
273 * types 16-23 (IRAP, Intra Random Access Point) NALUs might work)
274 * - The video encoder should be configured accordingly (to repeat the VPS/SPS/PPS for each I-frame, and possibly to
275 * produce all/only I frames.
276 * - Separate instances of this should be used to process different video streams
277 *
278 * @param[in] data Data for one (not more, not less) video frame
279 * @param[in] size Size of data
280 *
281 * @returns the decoded, converted and scaled image, nullptr otherwise (not enough data, bad data, ...)
282 */
283 virtual std::optional<ImageData> DecodeFrame(const uint8_t* data, const std::size_t size) = 0;
284
285 /**
286 * @brief Decode video data
287 *
288 * @param[in] data Data for one (recommended) or more video frame(s)
289 *
290 * @returns the decoded, converted and scaled image, nullptr otherwise (not enough data, bad data, ...)
291 */
292 std::optional<ImageData> DecodeFrame(const std::vector<uint8_t>& data);
293
294 /**
295 * @brief Check if decoder is in error state
296 *
297 * This is useful to check when DecodeFrame() didn't return a frame. If the decoder is in error state, it cannot
298 * be used anymore and must be discarded.
299 *
300 * @returns true if the decoder is okay, false if it is in error state
301 */
302 virtual bool IsOkay() const = 0;
303
304 protected:
306};
307
308/**
309 * @brief Pointer to a VideoFrameDecoder instance, see CreateVideoFrameDecoder()
310 */
311using VideoFrameDecoderPtr = std::unique_ptr<VideoFrameDecoder>;
312
313/**
314 * @brief Create a video frame decoder
315 *
316 * @param[in] params The parameters
317 *
318 * @returns the video frame decoder instances, or nullptr if it failed (bad params)
319 */
321
322#endif // FPSDK_USE_FFMPEG
323/* ****************************************************************************************************************** */
324} // namespace video
325} // namespace common
326} // namespace fpsdk
327#endif // __FPSDK_COMMON_VIDEO_HPP__
std::optional< ImageData > DecodeFrame(const std::vector< uint8_t > &data)
Decode video data.
VideoDecoderParams params_
Params.
Definition video.hpp:305
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:85
PixelFmt
Pixel format.
Definition video.hpp:122
@ RGB24
Packed RGB 8:8:8, 24bpp, RGBRGB... (= FFmpeg AV_PIX_FMT_RGB24).
Definition video.hpp:125
@ GBRP
Planar GBR, 24bpp [G][G][G]...[B][B][B]...[R][R][R]... (~ FFmpeg AV_PIX_FMT_GBRP/AV_PIX_FMT_GBR24P).
Definition video.hpp:126
@ Y8
Planar Y (greyscale), 8bpp (= FFmpeg AV_PIX_FMT_GRAY8).
Definition video.hpp:124
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:93
@ H265
H.265 High Efficiency Video Coding (HEVC).
Definition video.hpp:96
@ H264
H.264 Advanced Video Coding (AVC).
Definition video.hpp:95
const char * HwAccelToStr(const HwAccel accel)
Stringify hw accel method enum.
ScalingQual
Scaling method (only for sw decoding).
Definition video.hpp:152
@ LANCZOS
High quality, slower (radius=3.0).
Definition video.hpp:156
@ SINC
Very high quality, very slow (radius=3.0).
Definition video.hpp:157
@ BILINEAR
Low quality, very fast.
Definition video.hpp:154
@ BICUBIC
Medium/high quality, fast (B=0.0, C=0.5, "Catmull-Rom").
Definition video.hpp:155
std::unique_ptr< VideoFrameDecoder > VideoFrameDecoderPtr
Pointer to a VideoFrameDecoder instance, see CreateVideoFrameDecoder().
Definition video.hpp:311
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:183
@ HW
Use hw acceleration (currently, Linux Video Acceleration API, VA-API).
Definition video.hpp:187
@ SW
Do not use any hw acceleration, use pure software implementation.
Definition video.hpp:186
@ AUTO
Use hw accel if possible, fallback to SW.
Definition video.hpp:185
Fixposition SDK: Common library.
Definition doc.hpp:21
Fixposition SDK.
Decoded, converted and scaled image.
Definition video.hpp:228
std::vector< uint8_t > data_
Image data, memory layout depends on fmt_.
Definition video.hpp:232
PixelFmt fmt_
Pixel format (of data_).
Definition video.hpp:229
VideoFrameDecoder params and their defaults.
Definition video.hpp:213
ScalingQual qual_
Scaling method.
Definition video.hpp:219
HwAccel accel_
Hw acceleration.
Definition video.hpp:218
PixelFmt fmt_
Pixel format (of decoded image).
Definition video.hpp:216
double scale_
Scale factor (of decoded image), range 0.1-1.0.
Definition video.hpp:217
std::string name_
Name (for debug logging).
Definition video.hpp:214