| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef UTILS_H |
| 22 |
#define UTILS_H |
| 23 |
|
| 24 |
#include "ffms.h" |
| 25 |
|
| 26 |
extern "C" { |
| 27 |
#include <libavutil/mem.h> |
| 28 |
#include <libavutil/opt.h> |
| 29 |
#include <libavformat/avformat.h> |
| 30 |
#include <libavcodec/avcodec.h> |
| 31 |
#include <libswscale/swscale.h> |
| 32 |
#include <libswresample/swresample.h> |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
#include "ffmscompat.h" |
| 37 |
|
| 38 |
#include <cstdio> |
| 39 |
#include <memory> |
| 40 |
#include <string> |
| 41 |
#include <vector> |
| 42 |
#include <map> |
| 43 |
|
| 44 |
class FFMS_Exception { |
| 45 |
std::string _Message; |
| 46 |
int _ErrorType; |
| 47 |
int _SubType; |
| 48 |
|
| 49 |
public: |
| 50 |
FFMS_Exception(int ErrorType, int SubType, const char *Message = ""); |
| 51 |
FFMS_Exception(int ErrorType, int SubType, const std::string &Message); |
| 52 |
const std::string &GetErrorMessage() const { return _Message; } |
| 53 |
int CopyOut(FFMS_ErrorInfo *ErrorInfo) const; |
| 54 |
}; |
| 55 |
|
| 56 |
template<typename T, typename... Args> |
| 57 |
std::unique_ptr<T> make_unique(Args&&... args) { |
| 58 |
return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| 59 |
} |
| 60 |
|
| 61 |
void ClearErrorInfo(FFMS_ErrorInfo *ErrorInfo); |
| 62 |
void FillAP(FFMS_AudioProperties &AP, AVCodecContext *CTX, FFMS_Track &Frames); |
| 63 |
|
| 64 |
void LAVFOpenFile(const char *SourceFile, AVFormatContext *&FormatContext, int Track, const std::map<std::string, std::string> &LAVFOpts); |
| 65 |
|
| 66 |
namespace optdetail { |
| 67 |
template<typename T> |
| 68 |
T get_av_opt(void *v, const char *name) { |
| 69 |
int64_t value = 0; |
| 70 |
av_opt_get_int(v, name, 0, &value); |
| 71 |
return static_cast<T>(value); |
| 72 |
} |
| 73 |
|
| 74 |
template<> |
| 75 |
inline double get_av_opt<double>(void *v, const char *name) { |
| 76 |
double value = 0.0; |
| 77 |
av_opt_get_double(v, name, 0, &value); |
| 78 |
return value; |
| 79 |
} |
| 80 |
|
| 81 |
template<typename T> |
| 82 |
void set_av_opt(void *v, const char *name, T value) { |
| 83 |
av_opt_set_int(v, name, value, 0); |
| 84 |
} |
| 85 |
|
| 86 |
template<> |
| 87 |
inline void set_av_opt<double>(void *v, const char *name, double value) { |
| 88 |
av_opt_set_double(v, name, value, 0); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
template<typename FFMS_Struct> |
| 93 |
class OptionMapper { |
| 94 |
struct OptionMapperBase { |
| 95 |
virtual void ToOpt(FFMS_Struct const& src, void *dst) const = 0; |
| 96 |
virtual void FromOpt(FFMS_Struct &dst, void *src) const = 0; |
| 97 |
virtual ~OptionMapperBase() = default; |
| 98 |
}; |
| 99 |
|
| 100 |
template<typename T> |
| 101 |
class OptionMapperImpl : public OptionMapperBase { |
| 102 |
T(FFMS_Struct::*ptr); |
| 103 |
const char *name; |
| 104 |
|
| 105 |
public: |
| 106 |
OptionMapperImpl(T(FFMS_Struct::*ptr), const char *name) : ptr(ptr), name(name) {} |
| 107 |
void ToOpt(FFMS_Struct const& src, void *dst) const { optdetail::set_av_opt(dst, name, src.*ptr); } |
| 108 |
void FromOpt(FFMS_Struct &dst, void *src) const { dst.*ptr = optdetail::get_av_opt<T>(src, name); } |
| 109 |
}; |
| 110 |
|
| 111 |
std::unique_ptr<OptionMapperBase> impl; |
| 112 |
|
| 113 |
public: |
| 114 |
template<typename T> |
| 115 |
OptionMapper(const char *opt_name, T(FFMS_Struct::*member)) : impl(new OptionMapperImpl<T>(member, opt_name)) {} |
| 116 |
|
| 117 |
void ToOpt(FFMS_Struct const& src, void *dst) const { impl->ToOpt(src, dst); } |
| 118 |
void FromOpt(FFMS_Struct &dst, void *src) const { impl->FromOpt(dst, src); } |
| 119 |
}; |
| 120 |
|
| 121 |
template<typename T, int N> |
| 122 |
std::unique_ptr<T> ReadOptions(void *opt, OptionMapper<T>(&options)[N]) { |
| 123 |
auto ret = make_unique<T>(); |
| 124 |
for (int i = 0; i < N; ++i) |
| 125 |
options[i].FromOpt(*ret, opt); |
| 126 |
return ret; |
| 127 |
} |
| 128 |
|
| 129 |
template<typename T, int N> |
| 130 |
void SetOptions(T const& src, void *opt, OptionMapper<T>(&options)[N]) { |
| 131 |
for (int i = 0; i < N; ++i) |
| 132 |
options[i].ToOpt(src, opt); |
| 133 |
} |
| 134 |
|
| 135 |
int ResizerNameToSWSResizer(const char *ResizerName); |
| 136 |
bool IsSamePath(const char *p1, const char *p2); |
| 137 |
bool IsIOError(int error); |
| 138 |
std::string AVErrorToString(int ret); |
| 139 |
|
| 140 |
#endif |