Skip to content

Commit 7278daf

Browse files
author
Clement Champetier
committed
Merge branch 'develop' of https://github.com/mikrosimage/avTranscoder into dev_useFFmpegLogSystem
Conflicts: src/AvTranscoder/common.cpp src/AvTranscoder/file/FormatContext.cpp src/AvTranscoder/file/OutputFile.cpp
2 parents da0b29f + f443503 commit 7278daf

19 files changed

+217
-174
lines changed

src/AvTranscoder/Option.cpp

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

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

src/AvTranscoder/ProfileLoader.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace constants
2828
const std::string avProfileHeight = "height";
2929
const std::string avProfileSampleRate = "ar";
3030
const std::string avProfileChannel = "ac";
31+
const std::string avProfileThreads = "threads";
3132
}
3233

3334
class AvExport ProfileLoader

src/AvTranscoder/codec/ICodec.cpp

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

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

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

src/AvTranscoder/common.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ void preloadCodecsAndFormats()
1212
av_register_all();
1313
}
1414

15+
std::string getDescriptionFromErrorCode( const int code )
16+
{
17+
char err[AV_ERROR_MAX_STRING_SIZE];
18+
av_strerror( code, err, sizeof(err) );
19+
return std::string( err );
20+
}
21+
1522
void Logger::setLogLevel( const int level )
1623
{
1724
av_log_set_level( level );

src/AvTranscoder/common.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ typedef AVRational Rational;
5959
/// Register all the codecs and formats which are enabled at configuration time.
6060
void AvExport preloadCodecsAndFormats();
6161

62+
/// Get the string description corresponding to the error code provided by ffmpeg/libav
63+
std::string AvExport getDescriptionFromErrorCode( const int code );
64+
6265
#define LOG_DEBUG( ... ) { std::stringstream os; os << __VA_ARGS__; Logger::log( AV_LOG_DEBUG, os.str() ); }
6366
#define LOG_INFO( ... ) { std::stringstream os; os << __VA_ARGS__; Logger::log( AV_LOG_INFO, os.str() ); }
6467
#define LOG_WARN( ... ) { std::stringstream os; os << __VA_ARGS__; Logger::log( AV_LOG_WARNING, os.str() ); }

src/AvTranscoder/decoder/AudioDecoder.cpp

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

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

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

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

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

159+
// set decoder options
157160
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
158161
{
159162
if( (*it).first == constants::avProfileIdentificator ||
160163
(*it).first == constants::avProfileIdentificatorHuman ||
161-
(*it).first == constants::avProfileType )
164+
(*it).first == constants::avProfileType ||
165+
(*it).first == constants::avProfileThreads )
162166
continue;
163167

164168
try
165169
{
166-
Option& decodeOption = _inputStream->getAudioCodec().getOption( (*it).first );
170+
Option& decodeOption = codec.getOption( (*it).first );
167171
decodeOption.setString( (*it).second );
168172
}
169173
catch( std::exception& e )

src/AvTranscoder/decoder/VideoDecoder.cpp

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

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

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

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

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

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

123129
try
124130
{
125-
Option& decodeOption = _inputStream->getVideoCodec().getOption( (*it).first );
131+
Option& decodeOption = codec.getOption( (*it).first );
126132
decodeOption.setString( (*it).second );
127133
}
128134
catch( std::exception& e )

src/AvTranscoder/encoder/AudioEncoder.cpp

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

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

1938
void AudioEncoder::setup()
@@ -23,42 +42,32 @@ void AudioEncoder::setup()
2342

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

3447
// Set default frame parameters
3548
#if LIBAVCODEC_VERSION_MAJOR > 54
36-
av_frame_unref( frame );
49+
av_frame_unref( _frame );
3750
#else
38-
avcodec_get_frame_defaults( frame );
51+
avcodec_get_frame_defaults( _frame );
3952
#endif
4053

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

43-
frame->nb_samples = sourceAudioFrame.getNbSamples();
44-
frame->format = avCodecContext.sample_fmt;
45-
frame->channel_layout = avCodecContext.channel_layout;
56+
_frame->nb_samples = sourceAudioFrame.getNbSamples();
57+
_frame->format = avCodecContext.sample_fmt;
58+
_frame->channel_layout = avCodecContext.channel_layout;
4659

4760
// we calculate the size of the samples buffer in bytes
48-
int bufferSize = av_samples_get_buffer_size( NULL, avCodecContext.channels, frame->nb_samples, avCodecContext.sample_fmt, 0 );
61+
int bufferSize = av_samples_get_buffer_size( NULL, avCodecContext.channels, _frame->nb_samples, avCodecContext.sample_fmt, 0 );
4962
if( bufferSize < 0 )
5063
{
51-
char err[AV_ERROR_MAX_STRING_SIZE];
52-
av_strerror( bufferSize, err, sizeof(err) );
53-
throw std::runtime_error( "Encode audio frame error: buffer size < 0 - " + std::string(err) );
64+
throw std::runtime_error( "Encode audio frame error: buffer size < 0 - " + getDescriptionFromErrorCode( bufferSize ) );
5465
}
5566

56-
int retvalue = avcodec_fill_audio_frame( frame, avCodecContext.channels, avCodecContext.sample_fmt, sourceAudioFrame.getData(), bufferSize, 0 );
67+
int retvalue = avcodec_fill_audio_frame( _frame, avCodecContext.channels, avCodecContext.sample_fmt, sourceAudioFrame.getData(), bufferSize, 0 );
5768
if( retvalue < 0 )
5869
{
59-
char err[AV_ERROR_MAX_STRING_SIZE];
60-
av_strerror( retvalue, err, sizeof(err) );
61-
throw std::runtime_error( "Encode audio frame error: avcodec fill audio frame - " + std::string( err ) );
70+
throw std::runtime_error( "Encode audio frame error: avcodec fill audio frame - " + getDescriptionFromErrorCode( retvalue ) );
6271
}
6372

6473
AVPacket& packet = codedFrame.getAVPacket();
@@ -78,33 +87,21 @@ bool AudioEncoder::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
7887

7988
#if LIBAVCODEC_VERSION_MAJOR > 53
8089
int gotPacket = 0;
81-
int ret = avcodec_encode_audio2( &avCodecContext, &packet, frame, &gotPacket );
90+
int ret = avcodec_encode_audio2( &avCodecContext, &packet, _frame, &gotPacket );
8291
if( ret != 0 && gotPacket == 0 )
8392
{
84-
char err[AV_ERROR_MAX_STRING_SIZE];
85-
av_strerror( ret, err, sizeof(err) );
86-
throw std::runtime_error( "Encode audio frame error: avcodec encode audio frame - " + std::string( err ) );
93+
throw std::runtime_error( "Encode audio frame error: avcodec encode audio frame - " + getDescriptionFromErrorCode( ret ) );
8794
}
8895
#else
89-
int ret = avcodec_encode_audio( &avCodecContext, packet.data, packet.size, frame );
96+
int ret = avcodec_encode_audio( &avCodecContext, packet.data, packet.size, _frame );
9097
if( ret < 0 )
9198
{
92-
char err[AV_ERROR_MAX_STRING_SIZE];
93-
av_strerror( ret, err, sizeof(err) );
94-
throw std::runtime_error( "Encode audio frame error: avcodec encode audio frame - " + std::string( err ) );
99+
throw std::runtime_error( "Encode audio frame error: avcodec encode audio frame - " + getDescriptionFromErrorCode( ret ) );
95100
}
96101
#endif
97102

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

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

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

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

152148
// set encoder options
153149
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
@@ -156,7 +152,8 @@ void AudioEncoder::setProfile( const ProfileLoader::Profile& profile, const Audi
156152
(*it).first == constants::avProfileIdentificatorHuman ||
157153
(*it).first == constants::avProfileType ||
158154
(*it).first == constants::avProfileCodec ||
159-
(*it).first == constants::avProfileSampleFormat )
155+
(*it).first == constants::avProfileSampleFormat ||
156+
(*it).first == constants::avProfileThreads )
160157
continue;
161158

162159
try
@@ -176,7 +173,8 @@ void AudioEncoder::setProfile( const ProfileLoader::Profile& profile, const Audi
176173
(*it).first == constants::avProfileIdentificatorHuman ||
177174
(*it).first == constants::avProfileType ||
178175
(*it).first == constants::avProfileCodec ||
179-
(*it).first == constants::avProfileSampleFormat )
176+
(*it).first == constants::avProfileSampleFormat ||
177+
(*it).first == constants::avProfileThreads )
180178
continue;
181179

182180
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)