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 #include "avcompat.h"
6 
7 extern "C"
8 {
9 #include <libavutil/avutil.h>
10 #include <libavutil/parseutils.h>
11 #include <libavutil/mathematics.h>
12 #include <libavutil/opt.h>
13 #include <libavutil/pixdesc.h>
14 #include <libavutil/bswap.h>
15 #include <libswscale/swscale.h>
16 #include <libswresample/swresample.h>
17 }
18 
19 
20 template<typename T>
22 {
23  FFWrapperPtr() = default;
24  explicit FFWrapperPtr(T *raw)
25  : m_raw(raw) {}
26 
27  const T* raw() const { return m_raw; }
28  T* raw() { return m_raw; }
29  void reset(T *raw = nullptr) { m_raw = raw; }
30  bool isNull() const { return (m_raw == nullptr); }
31 
32  void _log(int level, const char *fmt) const
33  {
34  av_log(m_raw, level, fmt);
35  }
36 
37  template<typename... Args>
38  void _log(int level, const char* fmt, const Args&... args) const
39  {
40  av_log(m_raw, level, fmt, args...);
41  }
42 
43 protected:
44  T *m_raw = nullptr;
45 };
46 
47 #define RAW_GET(field, def) (m_raw ? m_raw->field : (def))
48 #define RAW_SET(field, val) if(m_raw) m_raw->field = (val)
49 
50 #define RAW_GET2(cond, field, def) (m_raw && (cond) ? m_raw->field : def)
51 #define RAW_SET2(cond, field, val) if(m_raw && (cond)) m_raw->field = (val)
52 
53 #define IF_GET(ptr, field, def) ((ptr) ? ptr->field : def)
54 #define IF_SET(ptr, field, val) (if(ptr) ptr->field = (val))
55 
56 #define IF_GET2(cond, ptr, field, def) (ptr && (cond) ? ptr->field : def)
57 #define IF_SET2(cond, ptr, field, val) (if(ptr && (cond)) ptr->field = (val))
58 
59 #if !DEPRECATED_INIT_PACKET
60 template<typename T>
62 {
63  FFWrapperRef() = default;
64  explicit FFWrapperRef(const T &raw)
65  : m_raw(raw) {}
66 
67  const T* raw() const { return &m_raw; }
68  T* raw() { return &m_raw; }
69  void reset(const T &raw = T()) { m_raw = raw; }
70  bool isNull() const {
71  static const T empty = T();
72  auto res = memcmp(&m_raw, &empty, sizeof(empty));
73  return (res != 0);
74  }
75 
76  void _log(int level, const char *fmt) const
77  {
78  av_log(&m_raw, level, fmt);
79  }
80 
81  template<typename... Args>
82  void _log(int level, const char* fmt, const Args&... args) const
83  {
84  av_log(&m_raw, level, fmt, args...);
85  }
86 
87 protected:
88  T m_raw = T();
89 };
90 #endif
91 
92 template<typename WrapperClass, typename T, T NoneValue = static_cast<T>(-1)>
94 {
95  constexpr PixSampleFmtWrapper() = default;
96  constexpr PixSampleFmtWrapper(T fmt) noexcept : m_fmt(fmt) {}
97 
98  // Access to the stored value
99  operator T() const noexcept
100  {
101  return m_fmt;
102  }
103 
104  T get() const noexcept
105  {
106  return m_fmt;
107  }
108 
109  operator T&() noexcept
110  {
111  return m_fmt;
112  }
113 
114  WrapperClass& operator=(T fmt) noexcept
115  {
116  m_fmt = fmt;
117  return *this;
118  }
119 
120  void set(T fmt) noexcept
121  {
122  m_fmt = fmt;
123  }
124 
125  // IO Stream interface
126  friend std::ostream& operator<<(std::ostream& ost, WrapperClass fmt)
127  {
128  ost << fmt.name();
129  return ost;
130  }
131 
132 protected:
133  T m_fmt = NoneValue;
134 };
Definition: ffmpeg.h:22
void reset(T *raw=nullptr)
Definition: ffmpeg.h:29
void _log(int level, const char *fmt, const Args &... args) const
Definition: ffmpeg.h:38
void _log(int level, const char *fmt) const
Definition: ffmpeg.h:32
T * m_raw
Definition: ffmpeg.h:44
T * raw()
Definition: ffmpeg.h:28
const T * raw() const
Definition: ffmpeg.h:27
FFWrapperPtr()=default
bool isNull() const
Definition: ffmpeg.h:30
FFWrapperPtr(T *raw)
Definition: ffmpeg.h:24
Definition: ffmpeg.h:62
bool isNull() const
Definition: ffmpeg.h:70
void _log(int level, const char *fmt, const Args &... args) const
Definition: ffmpeg.h:82
T m_raw
Definition: ffmpeg.h:88
void reset(const T &raw=T())
Definition: ffmpeg.h:69
T * raw()
Definition: ffmpeg.h:68
const T * raw() const
Definition: ffmpeg.h:67
void _log(int level, const char *fmt) const
Definition: ffmpeg.h:76
FFWrapperRef()=default
FFWrapperRef(const T &raw)
Definition: ffmpeg.h:64
Definition: ffmpeg.h:94
T m_fmt
Definition: ffmpeg.h:133
T get() const noexcept
Definition: ffmpeg.h:104
friend std::ostream & operator<<(std::ostream &ost, WrapperClass fmt)
Definition: ffmpeg.h:126
WrapperClass & operator=(T fmt) noexcept
Definition: ffmpeg.h:114
constexpr PixSampleFmtWrapper()=default
constexpr PixSampleFmtWrapper(T fmt) noexcept
Definition: ffmpeg.h:96
void set(T fmt) noexcept
Definition: ffmpeg.h:120