Skip to content

Commit 7504d2d

Browse files
author
Clement Champetier
committed
OptionLoader: add functions to load options
* Add "loader" functions, which return the options you want: * loadFormatContextOptions. * loadCodecContextOptions. * loadOutputFormatOptions. * Update OptionChecker: correct call to "loader" functions, and move all the display to displayOptions function. * Refactoring: * add public typedef in OptionLoader class. * loadOptions function is private, and is called by all the public "loader" functions. * OptionLoader class does not store several array of options: the "loader" functions return the array of options expected.
1 parent cdb02c7 commit 7504d2d

File tree

3 files changed

+112
-43
lines changed

3 files changed

+112
-43
lines changed

app/optionChecker/optionChecker.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33

44
#include <string>
55
#include <iostream>
6+
#include <map>
7+
#include <vector>
68
#include <utility> //pair
79

8-
int optionChecker( const std::string& inputfilename )
9-
{
10-
avtranscoder::OptionLoader optionLoader;
11-
optionLoader.loadOptions( AV_OPT_FLAG_AUDIO_PARAM );
12-
13-
// display Options
14-
for( auto option : optionLoader.getOptions() )
10+
void displayOptions( avtranscoder::OptionLoader::OptionArray& options )
11+
{
12+
for( auto option : options )
1513
{
1614
std::cout << std::left;
1715
std::cout << "****************************" << std::endl;
@@ -62,6 +60,23 @@ int optionChecker( const std::string& inputfilename )
6260
}
6361
}
6462

63+
int optionChecker( const std::string& inputfilename )
64+
{
65+
avtranscoder::OptionLoader optionLoader;
66+
67+
//avtranscoder::OptionLoader::OptionArray optionsArray = optionLoader.loadOptions( AV_OPT_FLAG_AUDIO_PARAM );
68+
avtranscoder::OptionLoader::OptionMap optionsMap = optionLoader.loadOutputFormatOptions();
69+
70+
//displayOptions( optionsArray );
71+
for( avtranscoder::OptionLoader::OptionMap::iterator it = optionsMap.begin();
72+
it != optionsMap.end();
73+
++it )
74+
{
75+
std::cout << "----- " << it->first << " -----" << std::endl;
76+
displayOptions( it->second );
77+
}
78+
}
79+
6580
int main( int argc, char** argv )
6681
{
6782
if( argc <= 1 )

src/AvTranscoder/OptionLoader.cpp

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ extern "C" {
1010
#include <string>
1111
#include <map>
1212
#include <utility> //pair
13-
#include <exception>
13+
#include <iostream>
1414

1515
namespace avtranscoder
1616
{
1717

1818
OptionLoader::OptionLoader()
19-
: m_options()
20-
, m_avFormatContext( NULL )
19+
: m_avFormatContext( NULL )
2120
, m_avCodecContext( NULL )
21+
, m_outputFormat( NULL )
22+
, m_codec( NULL )
2223
{
2324
m_avFormatContext = avformat_alloc_context();
2425

@@ -38,31 +39,63 @@ OptionLoader::~OptionLoader()
3839
av_free( m_avCodecContext );
3940
}
4041

41-
void OptionLoader::loadOptions( int req_flags )
42+
OptionLoader::OptionArray OptionLoader::loadFormatContextOptions( int req_flags )
4243
{
43-
std::map<std::string, int> optionUnitToIndex;
44-
std::vector<Option> childOptions;
44+
return loadOptions( (void*)m_avFormatContext, req_flags );
45+
}
46+
47+
OptionLoader::OptionArray OptionLoader::loadCodecContextOptions( int req_flags )
48+
{
49+
return loadOptions( (void*)m_avCodecContext, req_flags );
50+
}
51+
52+
OptionLoader::OptionMap OptionLoader::loadOutputFormatOptions()
53+
{
54+
OptionMap outputFormatOptions;
4555

46-
const AVOption* avOption = NULL;
56+
m_outputFormat = av_oformat_next( NULL );
4757

48-
// get ffmpeg / libav object on which we'll scan AVOption
49-
void* av_class = NULL;
50-
if( ( req_flags & AV_OPT_FLAG_VIDEO_PARAM ) == AV_OPT_FLAG_VIDEO_PARAM ||
51-
( req_flags & AV_OPT_FLAG_AUDIO_PARAM ) == AV_OPT_FLAG_AUDIO_PARAM ||
52-
( req_flags & AV_OPT_FLAG_METADATA ) == AV_OPT_FLAG_METADATA ||
53-
( req_flags & AV_OPT_FLAG_FILTERING_PARAM ) == AV_OPT_FLAG_FILTERING_PARAM ||
54-
( req_flags & AV_OPT_FLAG_SUBTITLE_PARAM ) == AV_OPT_FLAG_SUBTITLE_PARAM )
55-
{
56-
av_class = (void*)m_avCodecContext;
57-
}
58-
else
58+
// iterate on formats
59+
while( m_outputFormat )
5960
{
60-
av_class = (void*)m_avFormatContext;
61+
// add only format with video track
62+
// m_outputFormat->audio_codec ?
63+
if( m_outputFormat->video_codec != AV_CODEC_ID_NONE )
64+
{
65+
if( m_outputFormat->priv_class )
66+
{
67+
std::string outputFormatName( m_outputFormat->name );
68+
OptionArray optionsArray = loadOptions( (void*)&m_outputFormat->priv_class );
69+
70+
outputFormatOptions.insert(
71+
std::pair< std::string, OptionArray >(
72+
outputFormatName,
73+
optionsArray )
74+
);
75+
}
76+
}
77+
m_outputFormat = av_oformat_next( m_outputFormat );
6178
}
79+
return outputFormatOptions;
80+
}
81+
82+
83+
OptionLoader::OptionArray OptionLoader::loadOptions( void* av_class, int req_flags )
84+
{
85+
OptionArray options;
86+
87+
std::map<std::string, int> optionUnitToIndex;
88+
std::vector<Option> childOptions;
89+
90+
const AVOption* avOption = NULL;
6291

6392
// iterate on options
64-
while( ( avOption = av_opt_next( av_class, avOption ) ) != NULL )
65-
{
93+
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 51, 12, 0 )
94+
while( ( avOption = av_next_option( av_class, avOption ) ) )
95+
#else
96+
while( ( avOption = av_opt_next( av_class, avOption ) ) )
97+
#endif
98+
{
6699
if( !avOption ||
67100
! avOption->name ||
68101
( avOption->flags & req_flags ) != req_flags )
@@ -80,29 +113,31 @@ void OptionLoader::loadOptions( int req_flags )
80113
}
81114
else
82115
{
83-
m_options.push_back( Option( *avOption, optionType ) );
116+
options.push_back( Option( *avOption, optionType ) );
84117
optionUnitToIndex.insert(
85118
std::pair<std::string, int>(
86119
std::string( avOption->unit ? avOption->unit : "" ),
87-
m_options.size() - 1 )
120+
options.size() - 1 )
88121
);
89122
}
90123
}
91124

92-
// iterate on childs option
125+
// iterate on child options
93126
for( std::vector<Option>::iterator it = childOptions.begin(); it != childOptions.end(); ++it )
94127
{
95128
int indexParentOption = optionUnitToIndex.at( it->getUnit() );
129+
Option& parentOption = options.at( indexParentOption );
96130

97-
m_options.at( indexParentOption ).appendChild( *it );
131+
parentOption.appendChild( *it );
98132

99133
// child of a Choice
100-
if( m_options.at( indexParentOption ).getType() == TypeChoice )
134+
if( parentOption.getType() == TypeChoice )
101135
{
102-
if( it->getDefaultValueInt() == m_options.at( indexParentOption ).getDefaultValueInt() )
103-
m_options.at( indexParentOption ).setDefaultChildIndex( m_options.at( indexParentOption ).getNbChilds() - 1 );
136+
if( it->getDefaultValueInt() == parentOption.getDefaultValueInt() )
137+
parentOption.setDefaultChildIndex( parentOption.getNbChilds() - 1 );
104138
}
105139
}
140+
return options;
106141
}
107142

108143
}

src/AvTranscoder/OptionLoader.hpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,56 @@ extern "C" {
1111
#include <libavformat/avformat.h>
1212
}
1313

14+
#include <string>
1415
#include <vector>
16+
#include <map>
17+
#include <utility> //pair
1518

1619
namespace avtranscoder
1720
{
1821

1922
/**
20-
* @brief Manage a list of Option.
21-
* The list is filled by loading options from a libav / ffmpeg object.
23+
* @brief Manage Options.
24+
* Get a list of options by calling a "load_function", depending on which FFMpeg / libAv object you want to analyse.
2225
*/
2326
class OptionLoader
2427
{
28+
public:
29+
typedef std::vector<Option> OptionArray;
30+
typedef std::map< std::string, std::vector<Option> > OptionMap;
31+
2532
public:
2633
OptionLoader();
2734
~OptionLoader();
2835

29-
std::vector<Option>& getOptions() { return m_options; }
30-
3136
const AVFormatContext* getFormatContext() const { return m_avFormatContext; }
3237
const AVCodecContext* getCodecContext() const { return m_avCodecContext; }
38+
const AVOutputFormat* getOutputFormat() const { return m_outputFormat; }
39+
const AVCodec* getCodec() const { return m_codec; }
3340

3441
/**
35-
* @brief: load array of Option depending on the flags.
36-
* @param req_flags: AVOption flags we want to load.
42+
* @param req_flags: AVOption flags (default = 0: no flag restriction)
3743
*/
38-
void loadOptions( int req_flags );
44+
OptionArray loadFormatContextOptions( int req_flags = 0 );
3945

46+
/**
47+
* @param req_flags: AVOption flags (default = 0: no flag restriction)
48+
*/
49+
OptionArray loadCodecContextOptions( int req_flags = 0 );
50+
51+
OptionMap loadOutputFormatOptions();
4052
private:
41-
std::vector<Option> m_options;
53+
/**
54+
* @brief: load array of Option depending on the flags.
55+
* @param req_flags: AVOption flags we want to load.
56+
*/
57+
OptionArray loadOptions( void* av_class, int req_flags = 0 );
4258

4359
AVFormatContext* m_avFormatContext;
4460
AVCodecContext* m_avCodecContext;
61+
62+
AVOutputFormat* m_outputFormat;
63+
AVCodec* m_codec;
4564

4665
};
4766

0 commit comments

Comments
 (0)