ifm3d
video_decoder.h
1 /*
2  * Copyright (C) 2026-present ifm electronic, gmbh
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * ifm3d RTSP video decoder interface.
6  *
7  * Decoder implementations are compiled directly into the RTSP
8  * module. Each implements the abstract VideoDecoder / VideoDecoderFactory C++
9  * interfaces below; DecoderManager lists the available implementations and
10  * selects one for a given codec. An implementation is free to load its own
11  * heavy runtime dependencies lazily (e.g. the ffmpeg decoder resolves
12  * libavcodec/libavutil at runtime) and to report, through IsAvailable(),
13  * whether it can actually be used on the current system.
14  *
15  * Thread-safety contract
16  * ----------------------
17  * All calls (SendPacket, ReceiveFrame, Flush, ...) for a given VideoDecoder
18  * instance MUST be made from a single thread at a time. Decoders are not
19  * required to be thread-safe across instances.
20  *
21  * Buffer ownership
22  * ----------------
23  * - Buffers passed to SendPacket are owned by the caller and are only valid
24  * for the duration of that call; decoders must copy if they retain.
25  * - VideoFrame plane pointers returned by ReceiveFrame are owned by the
26  * decoder and remain valid until the next call to ReceiveFrame, Flush, or
27  * the decoder's destruction.
28  */
29 
30 #ifndef IFM3D_RTSP_VIDEO_DECODER_H
31 #define IFM3D_RTSP_VIDEO_DECODER_H
32 
33 #include <cstdint>
34 #include <memory>
35 #include <string>
36 
37 namespace ifm3d::rtsp
38 {
40  enum VideoCodec
41  {
42  VIDEO_CODEC_H264 = 1
43  };
44 
46  enum VideoFormat
47  {
48  VIDEO_FORMAT_YUV420P = 1
49  };
50 
58  struct VideoFrame
59  {
60  std::uint8_t* planes[4]{};
61  int linesize[4]{};
62  int width{0};
63  int height{0};
64  int format{0}; /* VideoFormat value, e.g. VIDEO_FORMAT_YUV420P */
65  };
66 
74  {
75  public:
76  virtual ~VideoDecoder() = default;
77 
82  virtual int SendPacket(const std::uint8_t* data, int size) = 0;
83 
89  virtual int ReceiveFrame(VideoFrame& out) = 0;
90 
92  virtual int
94  {
95  return 0;
96  }
97 
99  [[nodiscard]] virtual std::string
100  LastError() const
101  {
102  return {};
103  }
104 
105  protected:
106  VideoDecoder() = default;
107  VideoDecoder(const VideoDecoder&) = default;
108  VideoDecoder& operator=(const VideoDecoder&) = default;
109  VideoDecoder(VideoDecoder&&) = default;
110  VideoDecoder& operator=(VideoDecoder&&) = default;
111  };
112 
121  {
122  public:
123  virtual ~VideoDecoderFactory() = default;
124 
126  [[nodiscard]] virtual std::string Name() const = 0;
127 
133  [[nodiscard]] virtual bool IsAvailable() const = 0;
134 
136  [[nodiscard]] virtual bool SupportsCodec(VideoCodec codec) const = 0;
137 
141  virtual std::unique_ptr<VideoDecoder> CreateDecoder(VideoCodec codec) = 0;
142 
147  [[nodiscard]] virtual std::string
149  {
150  return {};
151  }
152 
153  protected:
154  VideoDecoderFactory() = default;
155  VideoDecoderFactory(const VideoDecoderFactory&) = default;
156  VideoDecoderFactory& operator=(const VideoDecoderFactory&) = default;
158  VideoDecoderFactory& operator=(VideoDecoderFactory&&) = default;
159  };
160 
161 } // namespace ifm3d::rtsp
162 
163 #endif /* IFM3D_RTSP_VIDEO_DECODER_H */
Abstract decoder implementation (factory).
Definition: video_decoder.h:121
virtual bool SupportsCodec(VideoCodec codec) const =0
Whether this implementation can decode codec.
virtual std::string AvailabilityError() const
Optional human-readable reason why IsAvailable() is false; empty when the decoder is available.
Definition: video_decoder.h:148
virtual bool IsAvailable() const =0
Whether this implementation can actually be used on the current system.
virtual std::unique_ptr< VideoDecoder > CreateDecoder(VideoCodec codec)=0
Create a decoder for codec, or nullptr if unsupported/unavailable.
virtual std::string Name() const =0
Stable, human-readable name (e.g.
Abstract H.264 (etc.) video decoder.
Definition: video_decoder.h:74
virtual int ReceiveFrame(VideoFrame &out)=0
Pull a decoded frame.
virtual int SendPacket(const std::uint8_t *data, int size)=0
Push one unit of compressed data into the decoder.
virtual std::string LastError() const
Optional: a human-readable description of the last error.
Definition: video_decoder.h:100
virtual int Flush()
Optional: flush buffered frames at end-of-stream.
Definition: video_decoder.h:93
Decoded frame view.
Definition: video_decoder.h:59