avcpp  2.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
avutils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include <deque>
6 #include <memory>
7 #include <mutex>
8 #include <sstream>
9 #include <algorithm>
10 #include <functional>
11 
12 #include "ffmpeg.h"
13 #include "avtime.h"
14 
15 extern "C" {
16 #include <libavfilter/avfilter.h>
17 #include <libavcodec/avcodec.h>
18 }
19 
20 // WA: codecpar usage need more investigation. Temporary disable it.
21 #define USE_CODECPAR ((LIBAVCODEC_VERSION_MAJOR) >= 59) // FFmpeg 5.0
22 
23 // New Audio Channel Layout API
24 #define API_NEW_CHANNEL_LAYOUT ((LIBAVUTIL_VERSION_MAJOR > 57) || (LIBAVUTIL_VERSION_MAJOR == 57 && (LIBAVUTIL_VERSION_MINOR >= 24)))
25 // `int64_t frame_num` has been added in the 60.2, in the 61.0 it should be removed
26 #define API_FRAME_NUM ((LIBAVCODEC_VERSION_MAJOR > 60) || (LIBAVCODEC_VERSION_MAJOR == 60 && LIBAVCODEC_VERSION_MINOR >= 2))
27 // use AVFormatContext::url
28 #define API_AVFORMAT_URL ((LIBAVFORMAT_VERSION_MAJOR > 58) || (LIBAVFORMAT_VERSION_MAJOR == 58 && LIBAVFORMAT_VERSION_MINOR >= 7))
29 // net key frame flags: AV_FRAME_FLAG_KEY (FFmpeg 6.1)
30 #define API_FRAME_KEY ((LIBAVUTIL_VERSION_MAJOR > 58) || (LIBAVUTIL_VERSION_MAJOR == 58 && LIBAVUTIL_VERSION_MINOR >= 29))
31 // avcodec_close() support
32 #define API_AVCODEC_CLOSE (LIBAVCODEC_VERSION_MAJOR < 61)
33 
34 #if defined(__ICL) || defined (__INTEL_COMPILER)
35 # define FF_DISABLE_DEPRECATION_WARNINGS __pragma(warning(push)) __pragma(warning(disable:1478))
36 # define FF_ENABLE_DEPRECATION_WARNINGS __pragma(warning(pop))
37 #elif defined(_MSC_VER)
38 # define FF_DISABLE_DEPRECATION_WARNINGS __pragma(warning(push)) __pragma(warning(disable:4996))
39 # define FF_ENABLE_DEPRECATION_WARNINGS __pragma(warning(pop))
40 #elif defined(__GNUC__) || defined(__clang__)
41 # define FF_DISABLE_DEPRECATION_WARNINGS _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
42 # define FF_ENABLE_DEPRECATION_WARNINGS _Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"")
43 #else
44 # define FF_DISABLE_DEPRECATION_WARNINGS
45 # define FF_ENABLE_DEPRECATION_WARNINGS
46 #endif
47 
48 //
49 // Functions
50 //
51 namespace av {
52 
53 // Basic FFmpeg constants
54 constexpr auto NoPts = static_cast<int64_t>(AV_NOPTS_VALUE);
55 constexpr auto TimeBase = static_cast<int>(AV_TIME_BASE);
56 constexpr auto TimeBaseQ = AVRational{1, AV_TIME_BASE};
57 
58 
59 template<typename R, typename T>
60 R lexical_cast(const T& v)
61 {
62  R result;
63  std::stringstream ss;
64  ss << v;
65  ss >> result;
66  return result;
67 }
68 
70 {
71 protected:
72  noncopyable() = default;
73  noncopyable( const noncopyable& ) = delete;
74  void operator=( const noncopyable& ) = delete;
75 };
76 
84 void set_logging_level(int32_t level);
85 
86 
96 void set_logging_level(const std::string& level);
97 
98 // Compat code
99 #define setFFmpegLoggingLevel set_logging_level
100 
108 void dumpBinaryBuffer(uint8_t *buffer, int buffer_size, int width = 16);
109 
110 
116 std::string error2string(int error);
117 
118 }
119 
120 
121 
122 
123 //
124 // Classes
125 //
126 namespace av {
127 
129 {
130  void operator()(void *) {}
131 };
132 
138 namespace v1 {
139 struct AvDeleter
140 {
141  bool operator() (struct SwsContext* &swsContext);
142  bool operator() (struct AVCodecContext* &codecContext);
143  bool operator() (struct AVOutputFormat* &format);
144  bool operator() (struct AVFormatContext* &formatContext);
145  bool operator() (struct AVFrame* &frame);
146  bool operator() (struct AVPacket* &packet);
147  bool operator() (struct AVDictionary* &dictionary);
148  bool operator ()(struct AVFilterInOut* &filterInOut);
149 };
150 } // ::v1
151 
152 inline namespace v2 {
154 {
155  template<typename T>
156  bool operator() (T *ptr) {
157  return v1::AvDeleter()(ptr);
158  }
159 };
160 }
161 
162 template<typename T>
163 std::unique_ptr<T, void(*)(void*)> malloc(size_t size)
164 {
165  return {static_cast<T*>(av_malloc(size)), av_free};
166 }
167 
168 template<typename T>
169 std::unique_ptr<T, void(*)(void*)> mallocz(size_t size)
170 {
171  return {static_cast<T*>(av_mallocz(size)), av_free};
172 }
173 
174 template<typename T>
175 std::unique_ptr<T, void(*)(void*)> memdup(const void *p, size_t size)
176 {
177  return {static_cast<T*>(av_memdup(p, size)), av_free};
178 }
179 
183 #if 0
184 struct AvNextElement
185 {
186  AVFilterInOut * operator()(AVFilterInOut * x) const
187  {
188  if (x)
189  return x->next;
190  else
191  return 0;
192  }
193 };
194 #endif
195 
196 
197 
210 template<typename T, typename V = T>
212 {
213 public:
219  ScopedValue(T &var, const V& outValue)
220  : var(var),
221  outValue(outValue)
222  {
223  }
224 
231  ScopedValue(T &var, const V &inValue, const V &outValue)
232  : var(var),
233  outValue(outValue)
234  {
235  this->var = inValue;
236  }
237 
239  {
240  var = outValue;
241  }
242 
243 private:
244  T& var;
245  V outValue;
246 };
247 
248 
277 {
278 public:
279  template<typename Proc>
280  ScopeOutAction(const Proc& proc)
281  : m_proc(proc)
282  {}
283 
284  template<typename Proc>
285  ScopeOutAction(Proc&& proc)
286  : m_proc(std::forward<Proc>(proc))
287  {}
288 
290  {
291  if (m_proc)
292  m_proc();
293  }
294 
295 private:
296  std::function<void()> m_proc;
297 };
298 
299 
301 
302 template<typename T>
304 {
306  : value(value)
307  {}
308 
309  bool operator() (const T& value) const
310  {
311  if (this->value == value)
312  return true;
313 
314  return false;
315  }
316 
317  const T& value;
318 };
319 
335 template<typename T, typename L, typename C>
336 T guessValue(const T& value, const L * list, C endListComparator)
337 {
338  if (!list)
339  return value;
340 
341  // move values to array
342  std::deque<T> values;
343  for (const L * ptr = list; !endListComparator(*ptr); ++ptr)
344  {
345  T v = *ptr;
346  values.push_back(v);
347  }
348 
349  // sort list
350  std::sort(values.begin(), values.end());
351 
352  // Search more appropriate range
353  int begin = 0;
354  int end = values.size() - 1;
355  while ((end - begin) > 1)
356  {
357  int mid = begin + (end - begin) / 2;
358 
359  if (value <= values[mid])
360  {
361  end = mid;
362  }
363  else
364  {
365  begin = mid;
366  }
367  }
368 
369  // distance from VALUE to BEGIN more short or VALUE less then BEGIN
370  if (value <= values[begin] || (value - values[begin]) < (values[end] - value))
371  {
372  return values[begin];
373  }
374 
375  return values[end];
376 }
377 
378 
379 template<typename T, typename Container>
380 void array_to_container(const T* array, std::size_t nelements, Container &container)
381 {
382  if (!array || nelements == 0)
383  return;
384  std::copy_n(array, array + nelements, std::back_inserter(container));
385 }
386 
387 template<typename T, typename Container, typename Callable>
388 void array_to_container(const T* array, std::size_t nelements, Container &container, Callable convert)
389 {
390  if (!array || nelements == 0)
391  return;
392  // TBD: implement in more clean way
393  //std::copy_n(array, array + nelemnts, std::back_inserter(container));
394  for (auto i = 0u; i < nelements; ++i) {
395  container.push_back(convert(array[i]));
396  }
397 }
398 
399 template<typename T, typename Container, typename Compare>
400 void array_to_container(const T* array, Container &container, Compare isEnd)
401 {
402  if (!array)
403  return;
404  T value;
405  while (!isEnd(value = *array++))
406  container.push_back(value);
407 }
408 
409 template<typename T, typename Container, typename Compare, typename Callable>
410 void array_to_container(const T* array, Container &container, Compare isEnd, Callable convert)
411 {
412  if (!array)
413  return;
414  T value;
415  while (!isEnd(value = *array++))
416  container.push_back(convert(value));
417 }
418 
419 } // ::av
420 
av::v1::AvDeleter::operator()
bool operator()(struct SwsContext *&swsContext)
Definition: avutils.cpp:203
av::error2string
string error2string(int error)
C++ verstion of the av_err2str()
Definition: avutils.cpp:195
av::EmptyDeleter::operator()
void operator()(void *)
Definition: avutils.h:130
avtime.h
av::malloc
std::unique_ptr< T, void(*)(void *)> malloc(size_t size)
Definition: avutils.h:163
ffmpeg.h
av::EqualComparator::EqualComparator
EqualComparator(const T &value)
Definition: avutils.h:305
av::v2::SmartDeleter::operator()
bool operator()(T *ptr)
Definition: avutils.h:156
av::set_logging_level
void set_logging_level(int32_t level)
This method can be used to turn up or down FFmpeg's logging level.
Definition: avutils.cpp:19
av::EmptyDeleter
Definition: avutils.h:128
av::memdup
std::unique_ptr< T, void(*)(void *)> memdup(const void *p, size_t size)
Definition: avutils.h:175
av::TimeBaseQ
constexpr auto TimeBaseQ
Definition: avutils.h:56
av::NoPts
constexpr auto NoPts
Definition: avutils.h:54
av::v2::SmartDeleter
Definition: avutils.h:153
av::noncopyable::noncopyable
noncopyable()=default
av::ScopeOutAction
The ScopeOutAction class - guard-type class that allows points callback that will be called at the sc...
Definition: avutils.h:276
av::ScopedValue::ScopedValue
ScopedValue(T &var, const V &inValue, const V &outValue)
Ctor.
Definition: avutils.h:231
av::EqualComparator
Definition: avutils.h:303
av::EqualComparator::value
const T & value
Definition: avutils.h:317
av::ScopeOutAction::ScopeOutAction
ScopeOutAction(const Proc &proc)
Definition: avutils.h:280
av::v1::AvDeleter
Definition: avutils.h:139
av::mallocz
std::unique_ptr< T, void(*)(void *)> mallocz(size_t size)
Definition: avutils.h:169
av::noncopyable::operator=
void operator=(const noncopyable &)=delete
av::array_to_container
void array_to_container(const T *array, std::size_t nelements, Container &container)
Definition: avutils.h:380
av::TimeBase
constexpr auto TimeBase
Definition: avutils.h:55
av::lexical_cast
R lexical_cast(const T &v)
Definition: avutils.h:60
av
Definition: audioresampler.cpp:8
std
Definition: averror.h:228
av::noncopyable
Definition: avutils.h:69
av::dumpBinaryBuffer
void dumpBinaryBuffer(uint8_t *buffer, int buffer_size, int width)
dump_binary_buffer Dump binary buffer to std out in HEX view
Definition: avutils.cpp:70
av::EqualComparator::operator()
bool operator()(const T &value) const
Definition: avutils.h:309
av::ScopedValue
Functor to take next element in list/array.
Definition: avutils.h:211
av::ScopedValue::~ScopedValue
~ScopedValue()
Definition: avutils.h:238
av::guessValue
T guessValue(const T &value, const L *list, C endListComparator)
Select more approptiate value from given value list.
Definition: avutils.h:336
av::ScopeOutAction::~ScopeOutAction
~ScopeOutAction()
Definition: avutils.h:289
av::ScopeOutAction::ScopeOutAction
ScopeOutAction(Proc &&proc)
Definition: avutils.h:285
av::ScopedValue::ScopedValue
ScopedValue(T &var, const V &outValue)
Ctor.
Definition: avutils.h:219