Skip to content

Commit 9534590

Browse files
author
Clement Champetier
committed
Merge branch 'develop' into dev_loadProfilesOnWindows
2 parents 262ef02 + f443503 commit 9534590

File tree

19 files changed

+220
-174
lines changed

19 files changed

+220
-174
lines changed

src/AvTranscoder/Option.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,19 +212,15 @@ void Option::checkFFmpegGetOption( const int ffmpegReturnCode ) const
212212
{
213213
if( ffmpegReturnCode )
214214
{
215-
char err[AV_ERROR_MAX_STRING_SIZE];
216-
av_strerror( ffmpegReturnCode, err, sizeof(err) );
217-
throw std::runtime_error( "unknown key " + getName() + ": " + err );
215+
throw std::runtime_error( "unknown key " + getName() + ": " + getDescriptionFromErrorCode( ffmpegReturnCode ) );
218216
}
219217
}
220218

221219
void Option::checkFFmpegSetOption( const int ffmpegReturnCode, const std::string& optionValue )
222220
{
223221
if( ffmpegReturnCode )
224222
{
225-
char err[AV_ERROR_MAX_STRING_SIZE];
226-
av_strerror( ffmpegReturnCode, err, sizeof(err) );
227-
throw std::runtime_error( "setting " + getName() + " parameter to " + optionValue + ": " + err );
223+
throw std::runtime_error( "setting " + getName() + " parameter to " + optionValue + ": " + getDescriptionFromErrorCode( ffmpegReturnCode ) );
228224
}
229225
}
230226

src/AvTranscoder/codec/ICodec.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ void ICodec::open()
6767
msg += ") ";
6868
avcodec_close( _avCodecContext );
6969

70-
char err[AV_ERROR_MAX_STRING_SIZE];
71-
av_strerror( ret, err, sizeof(err) );
72-
msg += err;
70+
msg += getDescriptionFromErrorCode( ret );
7371

7472
throw std::runtime_error( msg );
7573
}

src/AvTranscoder/common.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ void setLogLevel( const int level )
1717
av_log_set_level( level );
1818
}
1919

20+
std::string AvExport getDescriptionFromErrorCode( const int code )
21+
{
22+
char err[AV_ERROR_MAX_STRING_SIZE];
23+
av_strerror( code, err, sizeof(err) );
24+
return std::string( err );
25+
}
26+
2027
}

src/AvTranscoder/common.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ void AvExport preloadCodecsAndFormats();
6767
*/
6868
void AvExport setLogLevel( const int level );
6969

70+
71+
/// Get the string description corresponding to the error code provided by ffmpeg/libav
72+
std::string AvExport getDescriptionFromErrorCode( const int code );
73+
7074
}
7175

7276
#endif

src/AvTranscoder/decoder/AudioDecoder.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,38 +133,42 @@ bool AudioDecoder::decodeNextFrame()
133133

134134
bool nextPacketRead = _inputStream->readNextPacket( data );
135135
if( ! nextPacketRead ) // error or end of file
136-
return false;
136+
data.clear();
137137

138138
int ret = avcodec_decode_audio4( &_inputStream->getAudioCodec().getAVCodecContext(), _frame, &got_frame, &data.getAVPacket() );
139-
if( ret == 0 && got_frame == 0 ) // no frame could be decompressed
139+
if( ! nextPacketRead && ret == 0 && got_frame == 0 ) // no frame could be decompressed
140140
return false;
141141

142142
if( ret < 0 )
143143
{
144-
char err[AV_ERROR_MAX_STRING_SIZE];
145-
av_strerror( ret, err, sizeof(err) );
146-
throw std::runtime_error( "an error occured during audio decoding" + std::string( err ) );
144+
throw std::runtime_error( "an error occured during audio decoding" + getDescriptionFromErrorCode( ret ) );
147145
}
148146
}
149147
return true;
150148
}
151149

152150
void AudioDecoder::setProfile( const ProfileLoader::Profile& profile )
153151
{
154-
// set threads if not in profile
155-
if( ! profile.count( "threads" ) )
156-
_inputStream->getAudioCodec().getOption( "threads" ).setString( "auto" );
152+
AudioCodec& codec = _inputStream->getAudioCodec();
153+
154+
// set threads before any other options
155+
if( profile.count( constants::avProfileThreads ) )
156+
codec.getOption( constants::avProfileThreads ).setString( profile.at( constants::avProfileThreads ) );
157+
else
158+
codec.getOption( constants::avProfileThreads ).setString( "auto" );
157159

160+
// set decoder options
158161
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
159162
{
160163
if( (*it).first == constants::avProfileIdentificator ||
161164
(*it).first == constants::avProfileIdentificatorHuman ||
162-
(*it).first == constants::avProfileType )
165+
(*it).first == constants::avProfileType ||
166+
(*it).first == constants::avProfileThreads )
163167
continue;
164168

165169
try
166170
{
167-
Option& decodeOption = _inputStream->getAudioCodec().getOption( (*it).first );
171+
Option& decodeOption = codec.getOption( (*it).first );
168172
decodeOption.setString( (*it).second );
169173
}
170174
catch( std::exception& e )

src/AvTranscoder/decoder/VideoDecoder.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@ bool VideoDecoder::decodeNextFrame()
8787

8888
bool nextPacketRead = _inputStream->readNextPacket( data );
8989
if( ! nextPacketRead ) // error or end of file
90-
return false;
90+
data.clear();
9191

9292
int ret = avcodec_decode_video2( &_inputStream->getVideoCodec().getAVCodecContext(), _frame, &got_frame, &data.getAVPacket() );
93-
if( ret == 0 && got_frame == 0 ) // no frame could be decompressed
93+
if( ! nextPacketRead && ret == 0 && got_frame == 0 ) // no frame could be decompressed
9494
return false;
9595

9696
if( ret < 0 )
9797
{
9898
char err[AV_ERROR_MAX_STRING_SIZE];
9999
av_strerror( ret, err, sizeof(err) );
100-
throw std::runtime_error( "an error occured during video decoding - " + std::string(err) );
100+
throw std::runtime_error( "an error occured during video decoding - " + getDescriptionFromErrorCode( ret ) );
101101
}
102102
}
103103
return true;
@@ -110,20 +110,26 @@ void VideoDecoder::flushDecoder()
110110

111111
void VideoDecoder::setProfile( const ProfileLoader::Profile& profile )
112112
{
113-
// set threads if not in profile
114-
if( ! profile.count( "threads" ) )
115-
_inputStream->getVideoCodec().getOption( "threads" ).setString( "auto" );
113+
VideoCodec& codec = _inputStream->getVideoCodec();
114+
115+
// set threads before any other options
116+
if( profile.count( constants::avProfileThreads ) )
117+
codec.getOption( constants::avProfileThreads ).setString( profile.at( constants::avProfileThreads ) );
118+
else
119+
codec.getOption( constants::avProfileThreads ).setString( "auto" );
116120

121+
// set decoder options
117122
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
118123
{
119124
if( (*it).first == constants::avProfileIdentificator ||
120125
(*it).first == constants::avProfileIdentificatorHuman ||
121-
(*it).first == constants::avProfileType )
126+
(*it).first == constants::avProfileType ||
127+
(*it).first == constants::avProfileThreads )
122128
continue;
123129

124130
try
125131
{
126-
Option& decodeOption = _inputStream->getVideoCodec().getOption( (*it).first );
132+
Option& decodeOption = codec.getOption( (*it).first );
127133
decodeOption.setString( (*it).second );
128134
}
129135
catch( std::exception& e )

src/AvTranscoder/encoder/AudioEncoder.cpp

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,26 @@ namespace avtranscoder
1414

1515
AudioEncoder::AudioEncoder( const std::string& audioCodecName )
1616
: _codec( eCodecTypeEncoder, audioCodecName )
17+
, _frame( NULL )
1718
{
19+
#if LIBAVCODEC_VERSION_MAJOR > 54
20+
_frame = av_frame_alloc();
21+
#else
22+
_frame = avcodec_alloc_frame();
23+
#endif
24+
}
25+
26+
AudioEncoder::~AudioEncoder()
27+
{
28+
#if LIBAVCODEC_VERSION_MAJOR > 54
29+
av_frame_free( &_frame );
30+
#else
31+
#if LIBAVCODEC_VERSION_MAJOR > 53
32+
avcodec_free_frame( &_frame );
33+
#else
34+
av_free( _frame );
35+
#endif
36+
#endif
1837
}
1938

2039
void AudioEncoder::setup()
@@ -24,42 +43,32 @@ void AudioEncoder::setup()
2443

2544
bool AudioEncoder::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
2645
{
27-
#if LIBAVCODEC_VERSION_MAJOR > 54
28-
AVFrame* frame = av_frame_alloc();
29-
#else
30-
AVFrame* frame = avcodec_alloc_frame();
31-
#endif
32-
3346
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
3447

3548
// Set default frame parameters
3649
#if LIBAVCODEC_VERSION_MAJOR > 54
37-
av_frame_unref( frame );
50+
av_frame_unref( _frame );
3851
#else
39-
avcodec_get_frame_defaults( frame );
52+
avcodec_get_frame_defaults( _frame );
4053
#endif
4154

4255
const AudioFrame& sourceAudioFrame = static_cast<const AudioFrame&>( sourceFrame );
4356

44-
frame->nb_samples = sourceAudioFrame.getNbSamples();
45-
frame->format = avCodecContext.sample_fmt;
46-
frame->channel_layout = avCodecContext.channel_layout;
57+
_frame->nb_samples = sourceAudioFrame.getNbSamples();
58+
_frame->format = avCodecContext.sample_fmt;
59+
_frame->channel_layout = avCodecContext.channel_layout;
4760

4861
// we calculate the size of the samples buffer in bytes
49-
int bufferSize = av_samples_get_buffer_size( NULL, avCodecContext.channels, frame->nb_samples, avCodecContext.sample_fmt, 0 );
62+
int bufferSize = av_samples_get_buffer_size( NULL, avCodecContext.channels, _frame->nb_samples, avCodecContext.sample_fmt, 0 );
5063
if( bufferSize < 0 )
5164
{
52-
char err[AV_ERROR_MAX_STRING_SIZE];
53-
av_strerror( bufferSize, err, sizeof(err) );
54-
throw std::runtime_error( "Encode audio frame error: buffer size < 0 - " + std::string(err) );
65+
throw std::runtime_error( "Encode audio frame error: buffer size < 0 - " + getDescriptionFromErrorCode( bufferSize ) );
5566
}
5667

57-
int retvalue = avcodec_fill_audio_frame( frame, avCodecContext.channels, avCodecContext.sample_fmt, sourceAudioFrame.getData(), bufferSize, 0 );
68+
int retvalue = avcodec_fill_audio_frame( _frame, avCodecContext.channels, avCodecContext.sample_fmt, sourceAudioFrame.getData(), bufferSize, 0 );
5869
if( retvalue < 0 )
5970
{
60-
char err[AV_ERROR_MAX_STRING_SIZE];
61-
av_strerror( retvalue, err, sizeof(err) );
62-
throw std::runtime_error( "Encode audio frame error: avcodec fill audio frame - " + std::string( err ) );
71+
throw std::runtime_error( "Encode audio frame error: avcodec fill audio frame - " + getDescriptionFromErrorCode( retvalue ) );
6372
}
6473

6574
AVPacket& packet = codedFrame.getAVPacket();
@@ -79,33 +88,21 @@ bool AudioEncoder::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
7988

8089
#if LIBAVCODEC_VERSION_MAJOR > 53
8190
int gotPacket = 0;
82-
int ret = avcodec_encode_audio2( &avCodecContext, &packet, frame, &gotPacket );
91+
int ret = avcodec_encode_audio2( &avCodecContext, &packet, _frame, &gotPacket );
8392
if( ret != 0 && gotPacket == 0 )
8493
{
85-
char err[AV_ERROR_MAX_STRING_SIZE];
86-
av_strerror( ret, err, sizeof(err) );
87-
throw std::runtime_error( "Encode audio frame error: avcodec encode audio frame - " + std::string( err ) );
94+
throw std::runtime_error( "Encode audio frame error: avcodec encode audio frame - " + getDescriptionFromErrorCode( ret ) );
8895
}
8996
#else
90-
int ret = avcodec_encode_audio( &avCodecContext, packet.data, packet.size, frame );
97+
int ret = avcodec_encode_audio( &avCodecContext, packet.data, packet.size, _frame );
9198
if( ret < 0 )
9299
{
93-
char err[AV_ERROR_MAX_STRING_SIZE];
94-
av_strerror( ret, err, sizeof(err) );
95-
throw std::runtime_error( "Encode audio frame error: avcodec encode audio frame - " + std::string( err ) );
100+
throw std::runtime_error( "Encode audio frame error: avcodec encode audio frame - " + getDescriptionFromErrorCode( ret ) );
96101
}
97102
#endif
98103

99-
#if LIBAVCODEC_VERSION_MAJOR > 54
100-
av_frame_free( &frame );
104+
#if LIBAVCODEC_VERSION_MAJOR > 53
101105
return ret == 0 && gotPacket == 1;
102-
#else
103-
#if LIBAVCODEC_VERSION_MAJOR > 53
104-
avcodec_free_frame( &frame );
105-
return ret == 0 && gotPacket == 1;
106-
#else
107-
av_free( frame );
108-
#endif
109106
#endif
110107
return ret == 0;
111108
}
@@ -122,19 +119,15 @@ bool AudioEncoder::encodeFrame( Frame& codedFrame )
122119
int ret = avcodec_encode_audio2( &avCodecContext, &packet, NULL, &gotPacket );
123120
if( ret != 0 && gotPacket == 0 )
124121
{
125-
char err[AV_ERROR_MAX_STRING_SIZE];
126-
av_strerror( ret, err, sizeof(err) );
127-
throw std::runtime_error( "Encode audio frame error: avcodec encode last audio frame - " + std::string( err ) );
122+
throw std::runtime_error( "Encode audio frame error: avcodec encode last audio frame - " + getDescriptionFromErrorCode( ret ) );
128123
}
129124
return ret == 0 && gotPacket == 1;
130125

131126
#else
132127
int ret = avcodec_encode_audio( &avCodecContext, packet.data, packet.size, NULL );
133128
if( ret < 0 )
134129
{
135-
char err[AV_ERROR_MAX_STRING_SIZE];
136-
av_strerror( ret, err, sizeof(err) );
137-
throw std::runtime_error( "Encode audio frame error: avcodec encode last audio frame - " + std::string( err ) );
130+
throw std::runtime_error( "Encode audio frame error: avcodec encode last audio frame - " + getDescriptionFromErrorCode( ret ) );
138131
}
139132
return ret == 0;
140133

@@ -146,9 +139,12 @@ void AudioEncoder::setProfile( const ProfileLoader::Profile& profile, const Audi
146139
// set sampleRate, number of channels, sample format
147140
_codec.setAudioParameters( frameDesc );
148141

149-
// set threads if not in profile
150-
if( ! profile.count( "threads" ) )
151-
_codec.getOption( "threads" ).setString( "auto" );
142+
// set threads before any other options
143+
if( profile.count( constants::avProfileThreads ) )
144+
_codec.getOption( constants::avProfileThreads ).setString( profile.at( constants::avProfileThreads ) );
145+
else
146+
_codec.getOption( constants::avProfileThreads ).setString( "auto" );
147+
152148

153149
// set encoder options
154150
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
@@ -157,7 +153,8 @@ void AudioEncoder::setProfile( const ProfileLoader::Profile& profile, const Audi
157153
(*it).first == constants::avProfileIdentificatorHuman ||
158154
(*it).first == constants::avProfileType ||
159155
(*it).first == constants::avProfileCodec ||
160-
(*it).first == constants::avProfileSampleFormat )
156+
(*it).first == constants::avProfileSampleFormat ||
157+
(*it).first == constants::avProfileThreads )
161158
continue;
162159

163160
try
@@ -177,7 +174,8 @@ void AudioEncoder::setProfile( const ProfileLoader::Profile& profile, const Audi
177174
(*it).first == constants::avProfileIdentificatorHuman ||
178175
(*it).first == constants::avProfileType ||
179176
(*it).first == constants::avProfileCodec ||
180-
(*it).first == constants::avProfileSampleFormat )
177+
(*it).first == constants::avProfileSampleFormat ||
178+
(*it).first == constants::avProfileThreads )
181179
continue;
182180

183181
try

src/AvTranscoder/encoder/AudioEncoder.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class AvExport AudioEncoder : public IEncoder
1212
{
1313
public:
1414
AudioEncoder( const std::string& audioCodecName );
15+
~AudioEncoder();
1516

1617
void setup();
1718

@@ -25,6 +26,7 @@ class AvExport AudioEncoder : public IEncoder
2526

2627
private:
2728
AudioCodec _codec;
29+
AVFrame* _frame; ///< Contains the encoded data to pass to the Frame when encodeFrame
2830
};
2931

3032
}

0 commit comments

Comments
 (0)