1
1
#include " AudioDesc.hpp"
2
- #include " AudioFrame.hpp"
3
2
4
- extern " C" {
5
- #ifndef __STDC_CONSTANT_MACROS
6
- #define __STDC_CONSTANT_MACROS
7
- #endif
8
- #include < libavcodec/avcodec.h>
9
- #include < libavformat/avformat.h>
10
- #include < libavutil/avutil.h>
11
- #include < libavutil/pixdesc.h>
12
- #include < libavutil/imgutils.h>
13
- #include < libavutil/mathematics.h>
14
- #include < libavutil/opt.h>
15
- #include < libavutil/error.h>
16
- }
17
- #include < iostream>
18
- #include < stdexcept>
19
- #include < sstream>
20
3
#include < cassert>
21
4
22
5
namespace avtranscoder
23
6
{
24
7
25
8
AudioDesc::AudioDesc ( const std::string& codecName )
26
- : m_codec( NULL )
27
- , m_codecContext( NULL )
9
+ : EssenceDesc( codecName )
28
10
{
29
- if ( codecName.size () )
30
- setAudioCodec ( codecName );
31
11
}
32
12
33
13
AudioDesc::AudioDesc ( const AVCodecID codecId )
34
- : m_codec( NULL )
35
- , m_codecContext( NULL )
14
+ : EssenceDesc( codecId )
36
15
{
37
- setAudioCodec ( codecId );
38
16
}
39
17
40
- void AudioDesc::setAudioCodec ( const std::string& codecName )
18
+ AudioDesc::AudioDesc ( const EssenceDesc& essenceDesc )
19
+ : EssenceDesc( essenceDesc.getCodecId() )
41
20
{
42
- avcodec_register_all (); // Warning: should be called only once
43
- m_codec = avcodec_find_encoder_by_name ( codecName.c_str () );
44
- initCodecContext ();
21
+ m_codec = essenceDesc.getCodec ();
22
+ m_codecContext = essenceDesc.getCodecContext ();
45
23
}
46
24
47
- void AudioDesc::setAudioCodec ( const AVCodecID codecId )
48
- {
49
- avcodec_register_all (); // Warning: should be called only once
50
- m_codec = avcodec_find_encoder ( codecId );
51
- initCodecContext ();
52
- }
53
-
54
- void AudioDesc::setAudioParameters ( const size_t sampleRate, const size_t channels, const AVSampleFormat sampleFormat )
55
- {
56
- m_codecContext->sample_rate = sampleRate;
57
- m_codecContext->channels = channels;
58
- m_codecContext->sample_fmt = sampleFormat;
59
- }
60
-
61
- void AudioDesc::initCodecContext ( )
62
- {
63
- if ( m_codec == NULL )
64
- {
65
- throw std::runtime_error ( " unknown audio codec" );
66
- }
67
-
68
- if ( ( m_codecContext = avcodec_alloc_context3 ( m_codec ) ) == NULL )
69
- {
70
- throw std::runtime_error ( " unable to create context for audio context" );
71
- }
72
-
73
- // Set default codec parameters
74
- if ( avcodec_get_context_defaults3 ( m_codecContext, m_codec ) != 0 )
75
- {
76
- throw std::runtime_error ( " unable to find audio codec default values" );
77
- }
78
- }
79
-
80
- void AudioDesc::set ( const std::string& key, const std::string& flag, const bool enable )
25
+ AudioFrameDesc AudioDesc::getFrameDesc () const
81
26
{
82
- int error = 0 ;
83
- int64_t optVal ;
27
+ assert ( m_codecContext != NULL ) ;
28
+ AudioFrameDesc audioFrameDesc ;
84
29
85
- const AVOption* flagOpt = av_opt_find ( m_codecContext, flag.c_str (), key.c_str (), 0 , 0 );
86
-
87
- if ( ! flagOpt )
88
- {
89
- std::cout << flag << std::endl << " : " << flagOpt->default_val .i64 << std::endl;
90
- throw std::runtime_error ( " unknown flag " + flag );
91
- }
92
-
93
- error = av_opt_get_int ( m_codecContext, key.c_str (), AV_OPT_SEARCH_CHILDREN, &optVal );
94
- if ( error != 0 )
95
- {
96
- std::string err ( " " , AV_ERROR_MAX_STRING_SIZE );
97
- // av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
98
- av_strerror ( error, const_cast <char *>(err.c_str ()), err.size () );
99
- throw std::runtime_error ( " unknown key " + key + " : " + err );
100
- }
101
-
102
- if ( enable )
103
- optVal = optVal | flagOpt->default_val .i64 ;
104
- else
105
- optVal = optVal &~ flagOpt->default_val .i64 ;
30
+ audioFrameDesc.setChannels ( m_codecContext->channels );
31
+ audioFrameDesc.setSampleRate ( m_codecContext->sample_rate );
32
+ audioFrameDesc.setSampleFormat ( m_codecContext->sample_fmt );
33
+ // audioFrameDesc.setFps( 25 );
106
34
107
- error = av_opt_set_int ( m_codecContext, key.c_str (), optVal, AV_OPT_SEARCH_CHILDREN );
108
- if ( error != 0 )
109
- {
110
- std::string err ( " " , AV_ERROR_MAX_STRING_SIZE );
111
- // av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
112
- av_strerror ( error, const_cast <char *>(err.c_str ()), err.size () );
113
- throw std::runtime_error ( " setting " + key + " parameter to " + flag + " : " + err );
114
- }
115
- }
116
-
117
- void AudioDesc::set ( const std::string& key, const bool value )
118
- {
119
- int error = av_opt_set_int ( m_codecContext, key.c_str (), value, AV_OPT_SEARCH_CHILDREN );
120
- if ( error != 0 )
121
- {
122
- std::string err ( " " , AV_ERROR_MAX_STRING_SIZE );
123
- // av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
124
- av_strerror ( error, const_cast <char *>(err.c_str ()), err.size () );
125
- throw std::runtime_error ( " setting " + key + " parameter to " + ( value ? " true" : " false" ) + " : " + err );
126
- }
127
- }
128
-
129
- void AudioDesc::set ( const std::string& key, const int value )
130
- {
131
- // const AVOption* flagOpt = av_opt_find( m_codecContext, key.c_str(), NULL, 0, AV_OPT_SEARCH_CHILDREN );
132
-
133
- int error = av_opt_set_int ( m_codecContext, key.c_str (), value, AV_OPT_SEARCH_CHILDREN );
134
- if ( error != 0 )
135
- {
136
- std::ostringstream os;
137
- os << value;
138
- std::string err ( " " , AV_ERROR_MAX_STRING_SIZE );
139
- // av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
140
- av_strerror ( error, const_cast <char *>(err.c_str ()), err.size () );
141
- throw std::runtime_error ( " setting " + key + " parameter to " + os.str () + " : " + err );
142
- }
143
- }
144
-
145
- void AudioDesc::set ( const std::string& key, const int num, const int den )
146
- {
147
- AVRational ratio;
148
- ratio.num = num;
149
- ratio.den = den;
150
- int error = av_opt_set_q ( m_codecContext, key.c_str (), ratio, AV_OPT_SEARCH_CHILDREN );
151
- if ( error != 0 )
152
- {
153
- std::ostringstream os;
154
- os << num << " /" << den;
155
- std::string err ( " " , AV_ERROR_MAX_STRING_SIZE );
156
- // av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
157
- av_strerror ( error, const_cast <char *>(err.c_str ()), err.size () );
158
- throw std::runtime_error ( " setting " + key + " parameter to " + os.str () + " : " + err );
159
- }
160
- }
161
-
162
- void AudioDesc::set ( const std::string& key, const double value )
163
- {
164
- int error = av_opt_set_double ( m_codecContext, key.c_str (), value, AV_OPT_SEARCH_CHILDREN );
165
- if ( error != 0 )
166
- {
167
- std::ostringstream os;
168
- os << value;
169
- std::string err ( " " , AV_ERROR_MAX_STRING_SIZE );
170
- // av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
171
- av_strerror ( error, const_cast <char *>(err.c_str ()), err.size () );
172
- throw std::runtime_error ( " setting " + key + " parameter to " + os.str () + " : " + err );
173
- }
174
- }
175
-
176
- void AudioDesc::set ( const std::string& key, const std::string& value )
177
- {
178
- int error = av_opt_set ( m_codecContext, key.c_str (), value.c_str (), AV_OPT_SEARCH_CHILDREN );
179
- if ( error != 0 )
180
- {
181
- std::string err ( " " , AV_ERROR_MAX_STRING_SIZE );
182
- // av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
183
- av_strerror ( error, const_cast <char *>(err.c_str ()), err.size () );
184
- throw std::runtime_error ( " setting " + key + " parameter to " + value + " : " + err );
185
- }
186
- }
187
-
188
- std::string AudioDesc::getAudioCodec () const
189
- {
190
- assert ( m_codecContext != NULL );
191
- return avcodec_descriptor_get ( m_codecContext->codec_id )->name ;
192
- }
193
-
194
- AVCodecID AudioDesc::getAudioCodecId () const
195
- {
196
- assert ( m_codecContext != NULL );
197
- return m_codecContext->codec_id ;
35
+ return audioFrameDesc;
198
36
}
199
37
200
38
const size_t AudioDesc::getSampleRate () const
@@ -215,18 +53,12 @@ const AVSampleFormat AudioDesc::getSampleFormat() const
215
53
return m_codecContext->sample_fmt ;
216
54
}
217
55
218
- AudioFrameDesc AudioDesc::getFrameDesc () const
56
+
57
+ void AudioDesc::setAudioParameters ( const size_t sampleRate, const size_t channels, const AVSampleFormat sampleFormat )
219
58
{
220
- assert ( m_codecContext != NULL );
221
- AudioFrameDesc audioFrameDesc;
222
-
223
- audioFrameDesc.setChannels ( m_codecContext->channels );
224
- audioFrameDesc.setSampleRate ( m_codecContext->sample_rate );
225
- audioFrameDesc.setSampleFormat ( m_codecContext->sample_fmt );
226
- // audioFrameDesc.setFps( 25 );
227
-
228
- return audioFrameDesc;
59
+ m_codecContext->sample_rate = sampleRate;
60
+ m_codecContext->channels = channels;
61
+ m_codecContext->sample_fmt = sampleFormat;
229
62
}
230
63
231
-
232
64
}
0 commit comments