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  std::uint64_t pts{0}; /* echoed from the SendPacket call that built this
66  frame -- see the frame identity contract above */
67  };
68 
76  {
77  public:
78  virtual ~VideoDecoder() = default;
79 
94  virtual int SendPacket(const std::uint8_t* data,
95  int size,
96  std::uint64_t pts) = 0;
97 
110  virtual int ReceiveFrame(VideoFrame& out) = 0;
111 
113  virtual int
115  {
116  return 0;
117  }
118 
120  [[nodiscard]] virtual std::string
121  LastError() const
122  {
123  return {};
124  }
125 
126  protected:
127  VideoDecoder() = default;
128  VideoDecoder(const VideoDecoder&) = default;
129  VideoDecoder& operator=(const VideoDecoder&) = default;
130  VideoDecoder(VideoDecoder&&) = default;
131  VideoDecoder& operator=(VideoDecoder&&) = default;
132  };
133 
142  {
143  public:
144  virtual ~VideoDecoderFactory() = default;
145 
147  [[nodiscard]] virtual std::string Name() const = 0;
148 
154  [[nodiscard]] virtual bool IsAvailable() const = 0;
155 
157  [[nodiscard]] virtual bool SupportsCodec(VideoCodec codec) const = 0;
158 
162  virtual std::unique_ptr<VideoDecoder> CreateDecoder(VideoCodec codec) = 0;
163 
168  [[nodiscard]] virtual std::string
170  {
171  return {};
172  }
173 
174  protected:
175  VideoDecoderFactory() = default;
176  VideoDecoderFactory(const VideoDecoderFactory&) = default;
177  VideoDecoderFactory& operator=(const VideoDecoderFactory&) = default;
179  VideoDecoderFactory& operator=(VideoDecoderFactory&&) = default;
180  };
181 
182 } // namespace ifm3d::rtsp
183 
184 #endif /* IFM3D_RTSP_VIDEO_DECODER_H */
Abstract decoder implementation (factory).
Definition: video_decoder.h:142
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:169
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:76
virtual int SendPacket(const std::uint8_t *data, int size, std::uint64_t pts)=0
Push one unit of compressed data into the decoder.
virtual int ReceiveFrame(VideoFrame &out)=0
Pull a decoded frame.
virtual std::string LastError() const
Optional: a human-readable description of the last error.
Definition: video_decoder.h:121
virtual int Flush()
Optional: flush buffered frames at end-of-stream.
Definition: video_decoder.h:114
Decoded frame view.
Definition: video_decoder.h:59