avcpp  2.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
ffmpeg.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 
5 extern "C"
6 {
7 #include <libavutil/avutil.h>
8 #include <libavutil/parseutils.h>
9 #include <libavutil/mathematics.h>
10 #include <libavutil/opt.h>
11 #include <libavutil/pixdesc.h>
12 #include <libavdevice/avdevice.h>
13 #include <libswscale/swscale.h>
14 #include <libswresample/swresample.h>
15 #include <libavformat/version.h>
16 }
17 
18 extern "C" {
19 #include <libavfilter/avfilter.h>
20 #if LIBAVFILTER_VERSION_INT < AV_VERSION_INT(7,0,0)
21 # include <libavfilter/avfiltergraph.h>
22 #endif
23 #include <libavfilter/buffersink.h>
24 #include <libavfilter/buffersrc.h>
25 #if LIBAVFILTER_VERSION_INT <= AV_VERSION_INT(2,77,100) // 0.11.1
26 # include <libavfilter/vsrc_buffer.h>
27 #endif
28 #if LIBAVFILTER_VERSION_INT < AV_VERSION_INT(6,31,100) // 3.0
29 #include <libavfilter/avcodec.h>
30 #endif
31 }
32 
33 // Compat level
34 
35 // avcodec
36 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,59,100) // 1.0
37 inline void avcodec_free_frame(AVFrame **frame)
38 {
39  av_freep(frame);
40 }
41 #endif
42 
43 // avfilter
44 #if LIBAVFILTER_VERSION_INT < AV_VERSION_INT(3,17,100) // 1.0
45 inline const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
46 {
47  return pads[pad_idx].name;
48 }
49 
50 inline AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
51 {
52  return pads[pad_idx].type;
53 }
54 #endif
55 
56 
57 // Wrapper around av_free_packet()/av_packet_unref()
58 #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(6,31,100) // < 3.0
59 #define avpacket_unref(p) av_free_packet(p)
60 #else
61 #define avpacket_unref(p) av_packet_unref(p)
62 #endif
63 
64 #define NO_INIT_PACKET (LIBAVCODEC_VERSION_MAJOR >= 60)
65 #define DEPRECATED_INIT_PACKET (LIBAVCODEC_VERSION_MAJOR >= 58)
66 
67 template<typename T>
69 {
70  FFWrapperPtr() = default;
71  explicit FFWrapperPtr(T *raw)
72  : m_raw(raw) {}
73 
74  const T* raw() const { return m_raw; }
75  T* raw() { return m_raw; }
76  void reset(T *raw = nullptr) { m_raw = raw; }
77  bool isNull() const { return (m_raw == nullptr); }
78 
79  void _log(int level, const char *fmt) const
80  {
81  av_log(m_raw, level, fmt);
82  }
83 
84  template<typename... Args>
85  void _log(int level, const char* fmt, const Args&... args) const
86  {
87  av_log(m_raw, level, fmt, args...);
88  }
89 
90 protected:
91  T *m_raw = nullptr;
92 };
93 
94 #define RAW_GET(field, def) (m_raw ? m_raw->field : (def))
95 #define RAW_SET(field, val) if(m_raw) m_raw->field = (val)
96 
97 #define RAW_GET2(cond, field, def) (m_raw && (cond) ? m_raw->field : def)
98 #define RAW_SET2(cond, field, val) if(m_raw && (cond)) m_raw->field = (val)
99 
100 #define IF_GET(ptr, field, def) ((ptr) ? ptr->field : def)
101 #define IF_SET(ptr, field, val) (if(ptr) ptr->field = (val))
102 
103 #define IF_GET2(cond, ptr, field, def) (ptr && (cond) ? ptr->field : def)
104 #define IF_SET2(cond, ptr, field, val) (if(ptr && (cond)) ptr->field = (val))
105 
106 #if !DEPRECATED_INIT_PACKET
107 template<typename T>
109 {
110  FFWrapperRef() = default;
111  explicit FFWrapperRef(const T &raw)
112  : m_raw(raw) {}
113 
114  const T* raw() const { return &m_raw; }
115  T* raw() { return &m_raw; }
116  void reset(const T &raw = T()) { m_raw = raw; }
117  bool isNull() const {
118  static const T empty = T();
119  auto res = memcmp(&m_raw, &empty, sizeof(empty));
120  return (res != 0);
121  }
122 
123  void _log(int level, const char *fmt) const
124  {
125  av_log(&m_raw, level, fmt);
126  }
127 
128  template<typename... Args>
129  void _log(int level, const char* fmt, const Args&... args) const
130  {
131  av_log(&m_raw, level, fmt, args...);
132  }
133 
134 protected:
135  T m_raw = T();
136 };
137 #endif
138 
139 template<typename WrapperClass, typename T, T NoneValue = static_cast<T>(-1)>
141 {
142  constexpr PixSampleFmtWrapper() = default;
143  constexpr PixSampleFmtWrapper(T fmt) noexcept : m_fmt(fmt) {}
144 
145  // Access to the stored value
146  operator T() const noexcept
147  {
148  return m_fmt;
149  }
150 
151  T get() const noexcept
152  {
153  return m_fmt;
154  }
155 
156  operator T&() noexcept
157  {
158  return m_fmt;
159  }
160 
161  WrapperClass& operator=(T fmt) noexcept
162  {
163  m_fmt = fmt;
164  return *this;
165  }
166 
167  void set(T fmt) noexcept
168  {
169  m_fmt = fmt;
170  }
171 
172  // IO Stream interface
173  friend std::ostream& operator<<(std::ostream& ost, WrapperClass fmt)
174  {
175  ost << fmt.name();
176  return ost;
177  }
178 
179 protected:
180  T m_fmt = NoneValue;
181 };
182 
183 // Extended attributes
184 #if AV_GCC_VERSION_AT_LEAST(3,1)
185 # define attribute_deprecated2(x) __attribute__((deprecated(x)))
186 #elif defined(_MSC_VER)
187 # define attribute_deprecated2(x) __declspec(deprecated(x))
188 #else
189 # define attribute_deprecated2(x)
190 #endif
191 
PixSampleFmtWrapper::set
void set(T fmt) noexcept
Definition: ffmpeg.h:167
PixSampleFmtWrapper
Definition: ffmpeg.h:140
FFWrapperRef::raw
const T * raw() const
Definition: ffmpeg.h:114
FFWrapperPtr::isNull
bool isNull() const
Definition: ffmpeg.h:77
FFWrapperPtr
Definition: ffmpeg.h:68
FFWrapperPtr::FFWrapperPtr
FFWrapperPtr()=default
FFWrapperPtr::FFWrapperPtr
FFWrapperPtr(T *raw)
Definition: ffmpeg.h:71
FFWrapperRef::_log
void _log(int level, const char *fmt, const Args &... args) const
Definition: ffmpeg.h:129
FFWrapperRef::isNull
bool isNull() const
Definition: ffmpeg.h:117
FFWrapperRef::FFWrapperRef
FFWrapperRef()=default
FFWrapperRef::m_raw
T m_raw
Definition: ffmpeg.h:135
PixSampleFmtWrapper::PixSampleFmtWrapper
constexpr PixSampleFmtWrapper(T fmt) noexcept
Definition: ffmpeg.h:143
FFWrapperPtr::_log
void _log(int level, const char *fmt) const
Definition: ffmpeg.h:79
FFWrapperRef
Definition: ffmpeg.h:108
FFWrapperRef::reset
void reset(const T &raw=T())
Definition: ffmpeg.h:116
PixSampleFmtWrapper::m_fmt
T m_fmt
Definition: ffmpeg.h:180
PixSampleFmtWrapper::operator<<
friend std::ostream & operator<<(std::ostream &ost, WrapperClass fmt)
Definition: ffmpeg.h:173
FFWrapperPtr::raw
const T * raw() const
Definition: ffmpeg.h:74
FFWrapperPtr::raw
T * raw()
Definition: ffmpeg.h:75
PixSampleFmtWrapper::PixSampleFmtWrapper
constexpr PixSampleFmtWrapper()=default
FFWrapperPtr::m_raw
T * m_raw
Definition: ffmpeg.h:91
FFWrapperRef::raw
T * raw()
Definition: ffmpeg.h:115
FFWrapperPtr::reset
void reset(T *raw=nullptr)
Definition: ffmpeg.h:76
FFWrapperPtr::_log
void _log(int level, const char *fmt, const Args &... args) const
Definition: ffmpeg.h:85
PixSampleFmtWrapper::operator=
WrapperClass & operator=(T fmt) noexcept
Definition: ffmpeg.h:161
PixSampleFmtWrapper::get
T get() const noexcept
Definition: ffmpeg.h:151
FFWrapperRef::FFWrapperRef
FFWrapperRef(const T &raw)
Definition: ffmpeg.h:111
FFWrapperRef::_log
void _log(int level, const char *fmt) const
Definition: ffmpeg.h:123