avcpp 3.1.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
ffmpeg.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <iostream>
11
12#include "avcompat.h"
13
14extern "C"
15{
16#include <libavutil/avutil.h>
17#include <libavutil/parseutils.h>
18#include <libavutil/mathematics.h>
19#include <libavutil/opt.h>
20#include <libavutil/pixdesc.h>
21#include <libavutil/bswap.h>
22#include <libswscale/swscale.h>
23#include <libswresample/swresample.h>
24}
25
26
36template<typename T>
38{
42 FFWrapperPtr() = default;
48 explicit FFWrapperPtr(T *raw)
49 : m_raw(raw) {}
50
56 const T* raw() const { return m_raw; }
62 T* raw() { return m_raw; }
71 void reset(T *raw = nullptr) { m_raw = raw; }
77 bool isNull() const { return (m_raw == nullptr); }
78
85 void _log(int level, const char *fmt) const
86 {
87 av_log(m_raw, level, fmt);
88 }
89
97 template<typename... Args>
98 void _log(int level, const char* fmt, const Args&... args) const
99 {
100 av_log(m_raw, level, fmt, args...);
101 }
102
103protected:
105 T *m_raw = nullptr;
106};
107
114#define RAW_GET(field, def) (m_raw ? m_raw->field : (def))
121#define RAW_SET(field, val) if(m_raw) m_raw->field = (val)
122
130#define RAW_GET2(cond, field, def) (m_raw && (cond) ? m_raw->field : def)
138#define RAW_SET2(cond, field, val) if(m_raw && (cond)) m_raw->field = (val)
147#define IF_GET(ptr, field, def) ((ptr) ? ptr->field : def)
155#define IF_SET(ptr, field, val) (if(ptr) ptr->field = (val))
156
165#define IF_GET2(cond, ptr, field, def) (ptr && (cond) ? ptr->field : def)
174#define IF_SET2(cond, ptr, field, val) (if(ptr && (cond)) ptr->field = (val))
175
176#if !DEPRECATED_INIT_PACKET
187template<typename T>
189{
193 FFWrapperRef() = default;
199 explicit FFWrapperRef(const T &raw)
200 : m_raw(raw) {}
201
207 const T* raw() const { return &m_raw; }
213 T* raw() { return &m_raw; }
219 void reset(const T &raw = T()) { m_raw = raw; }
225 bool isNull() const {
226 static const T empty = T();
227 auto res = memcmp(&m_raw, &empty, sizeof(empty));
228 return (res == 0);
229 }
230
237 void _log(int level, const char *fmt) const
238 {
239 av_log(&m_raw, level, fmt);
240 }
241
249 template<typename... Args>
250 void _log(int level, const char* fmt, const Args&... args) const
251 {
252 av_log(&m_raw, level, fmt, args...);
253 }
254
255protected:
257 T m_raw = T();
258};
259#endif
260
272template<typename WrapperClass, typename T, T NoneValue = static_cast<T>(-1)>
274{
278 constexpr PixSampleFmtWrapper() = default;
284 constexpr PixSampleFmtWrapper(T fmt) noexcept : m_fmt(fmt) {}
285
286 // Access to the stored value
292 operator T() const noexcept
293 {
294 return m_fmt;
295 }
296
302 T get() const noexcept
303 {
304 return m_fmt;
305 }
306
315 operator T&() noexcept
316 {
317 return m_fmt;
318 }
319
327 WrapperClass& operator=(T fmt) noexcept
328 {
329 m_fmt = fmt;
330 return *this;
331 }
332
338 void set(T fmt) noexcept
339 {
340 m_fmt = fmt;
341 }
342
343 // IO Stream interface
352 friend std::ostream& operator<<(std::ostream& ost, WrapperClass fmt)
353 {
354 ost << fmt.name();
355 return ost;
356 }
357
358protected:
360 T m_fmt = NoneValue;
361};
FFmpeg version and API compatibility switches.
void reset(T *raw=nullptr)
Replace the stored pointer.
Definition ffmpeg.h:71
T * raw()
Write access to the wrapped object.
Definition ffmpeg.h:62
void _log(int level, const char *fmt, const Args &... args) const
Log through the FFmpeg log system with arguments.
Definition ffmpeg.h:98
void _log(int level, const char *fmt) const
Log through the FFmpeg log system, using this wrapper as the context.
Definition ffmpeg.h:85
AVCodecContext * m_raw
Definition ffmpeg.h:105
FFWrapperPtr()=default
Create an empty wrapper; isNull() is true.
const AVCodecContext * raw() const
Definition ffmpeg.h:56
bool isNull() const
Whether the wrapper is empty.
Definition ffmpeg.h:77
FFWrapperPtr(T *raw)
Wrap an existing object.
Definition ffmpeg.h:48
bool isNull() const
Whether the stored value equals a default-constructed one.
Definition ffmpeg.h:225
T * raw()
Write access to the stored structure.
Definition ffmpeg.h:213
void _log(int level, const char *fmt, const Args &... args) const
Log through the FFmpeg log system with arguments.
Definition ffmpeg.h:250
T m_raw
The stored value.
Definition ffmpeg.h:257
void reset(const T &raw=T())
Replace the stored value.
Definition ffmpeg.h:219
void _log(int level, const char *fmt) const
Log through the FFmpeg log system, using this wrapper as the context.
Definition ffmpeg.h:237
FFWrapperRef()=default
Create a value-initialised holder.
const T * raw() const
Read access to the stored structure.
Definition ffmpeg.h:207
FFWrapperRef(const T &raw)
Copy a structure into the wrapper.
Definition ffmpeg.h:199
AVPixelFormat m_fmt
Definition ffmpeg.h:360
T get() const noexcept
The stored enum value.
Definition ffmpeg.h:302
constexpr PixSampleFmtWrapper()=default
Create a wrapper holding NoneValue.
constexpr PixSampleFmtWrapper(T fmt) noexcept
Wrap a raw enum value.
Definition ffmpeg.h:284
friend std::ostream & operator<<(std::ostream &ost, WrapperClass fmt)
Print the format name.
Definition ffmpeg.h:352
WrapperClass & operator=(T fmt) noexcept
Assign a raw enum value.
Definition ffmpeg.h:327
void set(T fmt) noexcept
Assign a raw enum value.
Definition ffmpeg.h:338