AvTranscoder  0.9.4
C++APIforLibav/FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Option.hpp
Go to the documentation of this file.
1 #ifndef _AV_TRANSCODER_OPTION_HPP
2 #define _AV_TRANSCODER_OPTION_HPP
3 
5 
6 extern "C" {
7 #include <libavutil/opt.h>
8 }
9 
10 #include <vector>
11 #include <string>
12 #include <map>
13 #include <utility>
14 
15 namespace avtranscoder
16 {
17 
19 {
27  eOptionBaseTypeChild, // Option which belongs to Choice or Group
29 };
30 
31 /**
32  * @brief Wrapper of AVOption.
33  * Get its type to know what the option is about: Int, Double, Ratio, Choice...
34  * Parse its array of options to get the potential childs (Choice and Group).
35  */
36 class AvExport Option
37 {
38 public:
39  Option(AVOption& avOption, void* avContext);
40  ~Option() {}
41 
42  EOptionBaseType getType() const;
43 
44  std::string getName() const { return std::string(_avOption->name ? _avOption->name : ""); }
45  std::string getHelp() const { return std::string(_avOption->help ? _avOption->help : ""); }
46  std::string getUnit() const { return std::string(_avOption->unit ? _avOption->unit : ""); }
47  int getOffset() const { return _avOption->offset; }
48  double getMin() const { return _avOption->min; }
49  double getMax() const { return _avOption->max; }
50 
51  // flags
52  int getFlags() const { return _avOption->flags; }
53  bool isEncodingOpt() const { return (_avOption->flags & AV_OPT_FLAG_ENCODING_PARAM) == AV_OPT_FLAG_ENCODING_PARAM; }
54  bool isDecodingOpt() const { return (_avOption->flags & AV_OPT_FLAG_DECODING_PARAM) == AV_OPT_FLAG_DECODING_PARAM; }
55  bool isAudioOpt() const { return (_avOption->flags & AV_OPT_FLAG_AUDIO_PARAM) == AV_OPT_FLAG_AUDIO_PARAM; }
56  bool isVideoOpt() const { return (_avOption->flags & AV_OPT_FLAG_VIDEO_PARAM) == AV_OPT_FLAG_VIDEO_PARAM; }
57  bool isSubtitleOpt() const { return (_avOption->flags & AV_OPT_FLAG_SUBTITLE_PARAM) == AV_OPT_FLAG_SUBTITLE_PARAM; }
58 
59  // get default value
60  bool getDefaultBool() const;
61  int getDefaultInt() const;
62  double getDefaultDouble() const;
63  std::string getDefaultString() const;
64  std::pair<int, int> getDefaultRatio() const;
65 
66  // get value
67  bool getBool() const;
68  int getInt() const;
69  double getDouble() const;
70  std::string getString() const;
71  std::pair<int, int> getRatio() const;
72 
73  // set value
74  void setFlag(const std::string& flag, const bool enable);
75  void setBool(const bool value);
76  void setInt(const int value);
77  void setDouble(const double value);
78  void setString(const std::string& value);
79  void setRatio(const int num, const int den);
80 
81  // array of childs
82  bool hasChild() const { return _childOptions.size(); }
83  const std::vector<Option>& getChilds() const { return _childOptions; }
84  const Option& getChildAtIndex(const size_t index) const { return _childOptions.at(index); }
85  int getDefaultChildIndex() const { return _defaultChildIndex; }
86 
87  void setDefaultChildIndex(size_t index) { _defaultChildIndex = index; }
88  void appendChild(const Option& child);
89 
90 private:
91  EOptionBaseType getTypeFromAVOption(const std::string& unit, const AVOptionType avType);
92 
93  /**
94  * @brief Check the return value from FFmpeg functions
95  * @note Throw run_time exception if error
96  */
97  void checkFFmpegGetOption(const int ffmpegReturnCode) const;
98  void checkFFmpegSetOption(const int ffmpegReturnCode, const std::string& optionValue);
99 
100 private:
101  AVOption* _avOption; ///< Has link (no ownership)
103 
104  /**
105  * @brief Pointer to the corresponding context.
106  * Need it to set and get the option values.
107  */
108  void* _avContext; ///< Has link (no ownership)
109 
110  /**
111  * If the option corresponds to a Choice or a Group, it can contain childs,
112  * which are also options.
113  */
114  std::vector<Option> _childOptions;
116 };
117 
118 typedef std::vector<Option> OptionArray;
119 typedef std::map<std::string, Option> OptionMap; ///< Key: option name / value: option
120 
121 #ifndef SWIG
122 /**
123  * @param outOptions: map or array of options
124  * @param av_class: a libav context (could be an AVFormatContext or an AVCodecContext).
125  * @param req_flags: libav flag (AV_OPT_FLAG_XXX), which is a filter for AVOption loaded by the Context (default = 0: no flag
126  * restriction).
127  */
128 void AvExport loadOptions(OptionMap& outOptions, void* av_class, int req_flags = 0);
129 void AvExport loadOptions(OptionArray& outOptions, void* av_class, int req_flags = 0);
130 #endif
131 }
132 
133 #endif
bool isVideoOpt() const
Definition: Option.hpp:56
const Option & getChildAtIndex(const size_t index) const
Definition: Option.hpp:84
void loadOptions(OptionMap &outOptions, void *av_class, int req_flags)
Definition: Option.cpp:227
bool isEncodingOpt() const
Definition: Option.hpp:53
std::map< std::string, Option > OptionMap
Key: option name / value: option.
Definition: Option.hpp:119
std::vector< Option > OptionArray
Definition: Option.hpp:118
double getMin() const
Definition: Option.hpp:48
bool isSubtitleOpt() const
Definition: Option.hpp:57
AVOption * _avOption
Has link (no ownership)
Definition: Option.hpp:101
bool hasChild() const
Definition: Option.hpp:82
size_t _defaultChildIndex
Definition: Option.hpp:115
int getDefaultChildIndex() const
Definition: Option.hpp:85
std::string getName() const
Definition: Option.hpp:44
void setDefaultChildIndex(size_t index)
Definition: Option.hpp:87
Wrapper of AVOption. Get its type to know what the option is about: Int, Double, Ratio, Choice... Parse its array of options to get the potential childs (Choice and Group).
Definition: Option.hpp:36
std::vector< Option > _childOptions
Definition: Option.hpp:114
int getFlags() const
Definition: Option.hpp:52
int getOffset() const
Definition: Option.hpp:47
void * _avContext
Pointer to the corresponding context. Need it to set and get the option values.
Definition: Option.hpp:108
std::string getHelp() const
Definition: Option.hpp:45
double getMax() const
Definition: Option.hpp:49
std::string getUnit() const
Definition: Option.hpp:46
EOptionBaseType _type
Definition: Option.hpp:102
bool isAudioOpt() const
Definition: Option.hpp:55
const std::vector< Option > & getChilds() const
Definition: Option.hpp:83
bool isDecodingOpt() const
Definition: Option.hpp:54