Skip to content

Commit 3fbef61

Browse files
author
Clement Champetier
committed
Set AVOption of AVContext: refactoring - add ParamSet
* Add class ParamSet in common file. * Remove set functions in CodedDesc. * Update setProfile of: * IntputVideo. * OutputVideo. * OutputAudio.
1 parent aa7c90b commit 3fbef61

File tree

7 files changed

+152
-137
lines changed

7 files changed

+152
-137
lines changed
Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
#include "CodedDesc.hpp"
22

3-
extern "C" {
4-
#ifndef __STDC_CONSTANT_MACROS
5-
#define __STDC_CONSTANT_MACROS
6-
#endif
7-
#include <libavcodec/avcodec.h>
8-
#include <libavutil/opt.h>
9-
#include <libavutil/error.h>
10-
}
11-
12-
#include <iostream>
133
#include <stdexcept>
14-
#include <sstream>
154
#include <cassert>
165

176
namespace avtranscoder {
@@ -83,112 +72,4 @@ void CodedDesc::initCodecContext( )
8372
}
8473
}
8574

86-
void CodedDesc::set( const std::string& key, const std::string& flag, const bool enable )
87-
{
88-
int error = 0;
89-
int64_t optVal;
90-
91-
const AVOption* flagOpt = av_opt_find( m_codecContext, flag.c_str(), key.c_str(), 0, 0 );
92-
93-
if( ! flagOpt )
94-
{
95-
std::cout << flag << std::endl << " : " << flagOpt->default_val.i64 << std::endl;
96-
throw std::runtime_error( "unknown flag " + flag );
97-
}
98-
99-
error = av_opt_get_int( m_codecContext, key.c_str(), AV_OPT_SEARCH_CHILDREN, &optVal );
100-
if( error != 0 )
101-
{
102-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
103-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
104-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
105-
throw std::runtime_error( "unknown key " + key + ": " + err );
106-
}
107-
108-
if( enable )
109-
optVal = optVal | flagOpt->default_val.i64;
110-
else
111-
optVal = optVal &~ flagOpt->default_val.i64;
112-
113-
error = av_opt_set_int( m_codecContext, key.c_str(), optVal, AV_OPT_SEARCH_CHILDREN );
114-
if( error != 0 )
115-
{
116-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
117-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
118-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
119-
throw std::runtime_error( "setting " + key + " parameter to " + flag + ": " + err );
120-
}
121-
}
122-
123-
void CodedDesc::set( const std::string& key, const bool value )
124-
{
125-
int error = av_opt_set_int( m_codecContext, key.c_str(), value, AV_OPT_SEARCH_CHILDREN );
126-
if( error != 0 )
127-
{
128-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
129-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
130-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
131-
throw std::runtime_error( "setting " + key + " parameter to " + ( value ? "true" : "false" ) + ": " + err );
132-
}
133-
}
134-
135-
void CodedDesc::set( const std::string& key, const int value )
136-
{
137-
//const AVOption* flagOpt = av_opt_find( m_codecContext, key.c_str(), NULL, 0, AV_OPT_SEARCH_CHILDREN );
138-
139-
int error = av_opt_set_int( m_codecContext, key.c_str(), value, AV_OPT_SEARCH_CHILDREN );
140-
if( error != 0 )
141-
{
142-
std::ostringstream os;
143-
os << value;
144-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
145-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
146-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
147-
throw std::runtime_error( "setting " + key + " parameter to " + os.str() + ": " + err );
148-
}
149-
}
150-
151-
void CodedDesc::set( const std::string& key, const int num, const int den )
152-
{
153-
AVRational ratio;
154-
ratio.num = num;
155-
ratio.den = den;
156-
int error = av_opt_set_q( m_codecContext, key.c_str(), ratio, AV_OPT_SEARCH_CHILDREN );
157-
if( error != 0 )
158-
{
159-
std::ostringstream os;
160-
os << num << "/" << den;
161-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
162-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
163-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
164-
throw std::runtime_error( "setting " + key + " parameter to " + os.str() + ": " + err );
165-
}
166-
}
167-
168-
void CodedDesc::set( const std::string& key, const double value )
169-
{
170-
int error = av_opt_set_double( m_codecContext, key.c_str(), value, AV_OPT_SEARCH_CHILDREN );
171-
if( error != 0 )
172-
{
173-
std::ostringstream os;
174-
os << value;
175-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
176-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
177-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
178-
throw std::runtime_error( "setting " + key + " parameter to " + os.str() + ": " + err );
179-
}
180-
}
181-
182-
void CodedDesc::set( const std::string& key, const std::string& value )
183-
{
184-
int error = av_opt_set( m_codecContext, key.c_str(), value.c_str(), AV_OPT_SEARCH_CHILDREN );
185-
if( error != 0 )
186-
{
187-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
188-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
189-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
190-
throw std::runtime_error( "setting " + key + " parameter to " + value + ": " + err );
191-
}
192-
}
193-
19475
}

src/AvTranscoder/CodedStructures/CodedDesc.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ class AvExport CodedDesc
2626
void setCodec( const std::string& codecName );
2727
void setCodec( const AVCodecID codecId );
2828

29-
void set( const std::string& key, const std::string& flag, const bool enable );
30-
void set( const std::string& key, const bool value );
31-
void set( const std::string& key, const int value );
32-
void set( const std::string& key, const int num, const int den );
33-
void set( const std::string& key, const double value );
34-
void set( const std::string& key, const std::string& value );
35-
3629
#ifndef SWIG
3730
AVCodec* getCodec() const { return m_codec; }
3831
AVCodecContext* getCodecContext() const { return m_codecContext; }

src/AvTranscoder/EssenceStream/InputVideo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void InputVideo::flushDecoder()
149149

150150
void InputVideo::setProfile( const Profile::ProfileDesc& desc )
151151
{
152-
VideoDesc videoDesc = CodedDesc( *_codec, *_codecContext );
152+
ParamSet paramSet( _codecContext );
153153

154154
for( Profile::ProfileDesc::const_iterator it = desc.begin(); it != desc.end(); ++it )
155155
{
@@ -160,11 +160,11 @@ void InputVideo::setProfile( const Profile::ProfileDesc& desc )
160160

161161
try
162162
{
163-
videoDesc.set( (*it).first, (*it).second );
163+
paramSet.set( (*it).first, (*it).second );
164164
}
165165
catch( std::exception& e )
166166
{
167-
std::cout << "warning: " << e.what() << std::endl;
167+
std::cout << "InputVideo warning: " << e.what() << std::endl;
168168
}
169169
}
170170
}

src/AvTranscoder/EssenceStream/OutputAudio.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ void OutputAudio::setProfile( const Profile::ProfileDesc& desc, const AudioFrame
185185

186186
_audioDesc.setAudioParameters( frameDesc );
187187

188+
ParamSet paramSet( _audioDesc.getCodecContext() );
189+
188190
for( Profile::ProfileDesc::const_iterator it = desc.begin(); it != desc.end(); ++it )
189191
{
190192
if( (*it).first == Profile::avProfileIdentificator ||
@@ -196,11 +198,11 @@ void OutputAudio::setProfile( const Profile::ProfileDesc& desc, const AudioFrame
196198

197199
try
198200
{
199-
_audioDesc.set( (*it).first, (*it).second );
201+
paramSet.set( (*it).first, (*it).second );
200202
}
201203
catch( std::exception& e )
202204
{
203-
std::cout << "warning: " << e.what() << std::endl;
205+
std::cout << "OutputAudio warning: " << e.what() << std::endl;
204206
}
205207
}
206208

@@ -217,11 +219,11 @@ void OutputAudio::setProfile( const Profile::ProfileDesc& desc, const AudioFrame
217219

218220
try
219221
{
220-
_audioDesc.set( (*it).first, (*it).second );
222+
paramSet.set( (*it).first, (*it).second );
221223
}
222224
catch( std::exception& e )
223225
{
224-
std::cout << "2.warning: " << e.what() << std::endl;
226+
std::cout << "OutputAudio 2.warning: " << e.what() << std::endl;
225227
}
226228
}
227229
}

src/AvTranscoder/EssenceStream/OutputVideo.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ void OutputVideo::setProfile( const Profile::ProfileDesc& desc, const avtranscod
197197

198198
_videoDesc.setImageParameters( frameDesc );
199199

200+
ParamSet paramSet( _videoDesc.getCodecContext() );
201+
200202
for( Profile::ProfileDesc::const_iterator it = desc.begin(); it != desc.end(); ++it )
201203
{
202204
if( (*it).first == Profile::avProfileIdentificator ||
@@ -209,11 +211,11 @@ void OutputVideo::setProfile( const Profile::ProfileDesc& desc, const avtranscod
209211

210212
try
211213
{
212-
_videoDesc.set( (*it).first, (*it).second );
214+
paramSet.set( (*it).first, (*it).second );
213215
}
214216
catch( std::exception& e )
215217
{
216-
std::cout << "warning: " << e.what() << std::endl;
218+
std::cout << "OutputVideo warning: " << e.what() << std::endl;
217219
}
218220
}
219221

@@ -231,11 +233,11 @@ void OutputVideo::setProfile( const Profile::ProfileDesc& desc, const avtranscod
231233

232234
try
233235
{
234-
_videoDesc.set( (*it).first, (*it).second );
236+
paramSet.set( (*it).first, (*it).second );
235237
}
236238
catch( std::exception& e )
237239
{
238-
std::cout << "2.warning: " << e.what() << std::endl;
240+
std::cout << "OutputVideo 2.warning: " << e.what() << std::endl;
239241
}
240242
}
241243
}

src/AvTranscoder/common.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,130 @@
11
#include "common.hpp"
22

3+
extern "C" {
4+
#ifndef __STDC_CONSTANT_MACROS
5+
#define __STDC_CONSTANT_MACROS
6+
#endif
7+
#include <libavcodec/avcodec.h>
8+
#include <libavutil/opt.h>
9+
#include <libavutil/error.h>
10+
}
11+
312
#include <dirent.h>
413
#include <iostream>
14+
#include <stdexcept>
15+
#include <sstream>
516

617
namespace avtranscoder
718
{
819

20+
void ParamSet::set( const std::string& key, const std::string& flag, const bool enable )
21+
{
22+
int error = 0;
23+
int64_t optVal;
24+
25+
const AVOption* flagOpt = av_opt_find( _objContext, flag.c_str(), key.c_str(), 0, 0 );
26+
27+
if( ! flagOpt )
28+
{
29+
std::cout << flag << std::endl << " : " << flagOpt->default_val.i64 << std::endl;
30+
throw std::runtime_error( "unknown flag " + flag );
31+
}
32+
33+
error = av_opt_get_int( _objContext, key.c_str(), AV_OPT_SEARCH_CHILDREN, &optVal );
34+
if( error != 0 )
35+
{
36+
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
37+
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
38+
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
39+
throw std::runtime_error( "unknown key " + key + ": " + err );
40+
}
41+
42+
if( enable )
43+
optVal = optVal | flagOpt->default_val.i64;
44+
else
45+
optVal = optVal &~ flagOpt->default_val.i64;
46+
47+
error = av_opt_set_int( _objContext, key.c_str(), optVal, AV_OPT_SEARCH_CHILDREN );
48+
if( error != 0 )
49+
{
50+
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
51+
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
52+
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
53+
throw std::runtime_error( "setting " + key + " parameter to " + flag + ": " + err );
54+
}
55+
}
56+
57+
void ParamSet::set( const std::string& key, const bool value )
58+
{
59+
int error = av_opt_set_int( _objContext, key.c_str(), value, AV_OPT_SEARCH_CHILDREN );
60+
if( error != 0 )
61+
{
62+
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
63+
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
64+
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
65+
throw std::runtime_error( "setting " + key + " parameter to " + ( value ? "true" : "false" ) + ": " + err );
66+
}
67+
}
68+
69+
void ParamSet::set( const std::string& key, const int value )
70+
{
71+
//const AVOption* flagOpt = av_opt_find( _objContext, key.c_str(), NULL, 0, AV_OPT_SEARCH_CHILDREN );
72+
73+
int error = av_opt_set_int( _objContext, key.c_str(), value, AV_OPT_SEARCH_CHILDREN );
74+
if( error != 0 )
75+
{
76+
std::ostringstream os;
77+
os << value;
78+
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
79+
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
80+
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
81+
throw std::runtime_error( "setting " + key + " parameter to " + os.str() + ": " + err );
82+
}
83+
}
84+
85+
void ParamSet::set( const std::string& key, const int num, const int den )
86+
{
87+
AVRational ratio;
88+
ratio.num = num;
89+
ratio.den = den;
90+
int error = av_opt_set_q( _objContext, key.c_str(), ratio, AV_OPT_SEARCH_CHILDREN );
91+
if( error != 0 )
92+
{
93+
std::ostringstream os;
94+
os << num << "/" << den;
95+
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
96+
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
97+
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
98+
throw std::runtime_error( "setting " + key + " parameter to " + os.str() + ": " + err );
99+
}
100+
}
101+
102+
void ParamSet::set( const std::string& key, const double value )
103+
{
104+
int error = av_opt_set_double( _objContext, key.c_str(), value, AV_OPT_SEARCH_CHILDREN );
105+
if( error != 0 )
106+
{
107+
std::ostringstream os;
108+
os << value;
109+
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
110+
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
111+
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
112+
throw std::runtime_error( "setting " + key + " parameter to " + os.str() + ": " + err );
113+
}
114+
}
115+
116+
void ParamSet::set( const std::string& key, const std::string& value )
117+
{
118+
int error = av_opt_set( _objContext, key.c_str(), value.c_str(), AV_OPT_SEARCH_CHILDREN );
119+
if( error != 0 )
120+
{
121+
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
122+
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
123+
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
124+
throw std::runtime_error( "setting " + key + " parameter to " + value + ": " + err );
125+
}
126+
}
127+
9128
void split( std::vector< std::string >& splitedString, const std::string& inputString, const std::string& splitChars )
10129
{
11130
char* part = strtok( const_cast<char*>( inputString.c_str() ), splitChars.c_str() );

src/AvTranscoder/common.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ struct Ratio
6464
size_t den;
6565
};
6666

67+
class ParamSet
68+
{
69+
public:
70+
ParamSet( void* obj )
71+
: _objContext( obj )
72+
{}
73+
74+
void set( const std::string& key, const std::string& flag, const bool enable );
75+
void set( const std::string& key, const bool value );
76+
void set( const std::string& key, const int value );
77+
void set( const std::string& key, const int num, const int den );
78+
void set( const std::string& key, const double value );
79+
void set( const std::string& key, const std::string& value );
80+
81+
private:
82+
void* _objContext;
83+
};
84+
6785
void split( std::vector< std::string >& splitedString, const std::string& inputString, const std::string& splitChars = ";" );
6886

6987
int getFilesInDir( const std::string& dir, std::vector< std::string >& files );

0 commit comments

Comments
 (0)