Skip to content

Commit 660a5ee

Browse files
author
Clement Champetier
committed
ICodec: add open function
Call it in setup of encoders and decoders.
1 parent 3705c04 commit 660a5ee

File tree

6 files changed

+31
-74
lines changed

6 files changed

+31
-74
lines changed

src/AvTranscoder/codec/ICodec.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ ICodec::~ICodec()
4141
_avCodecContext = NULL;
4242
}
4343

44+
void ICodec::open()
45+
{
46+
if( ! _avCodecContext )
47+
throw std::runtime_error( "unable to open a codec with no codec context" );
48+
49+
int ret = avcodec_open2( _avCodecContext, _avCodec, NULL );
50+
if( ret < 0 )
51+
{
52+
std::string msg = "unable open codec: ";
53+
msg += _avCodec->long_name;
54+
msg += " (";
55+
msg += _avCodec->name;
56+
msg += ") ";
57+
avcodec_close( _avCodecContext );
58+
59+
char err[AV_ERROR_MAX_STRING_SIZE];
60+
av_strerror( ret, err, sizeof(err) );
61+
msg += err;
62+
63+
throw std::runtime_error( msg );
64+
}
65+
}
66+
4467
std::string ICodec::getCodecName() const
4568
{
4669
assert( _avCodec != NULL );

src/AvTranscoder/codec/ICodec.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class AvExport ICodec
2626

2727
virtual ~ICodec() = 0;
2828

29+
/// Initialize the codec context.
30+
void open();
31+
2932
std::string getCodecName() const;
3033
AVCodecID getCodecId() const;
3134
ECodecType getCodecType() const { return _type; }

src/AvTranscoder/decoder/AudioDecoder.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,7 @@ AudioDecoder::~AudioDecoder()
4444

4545
void AudioDecoder::setup()
4646
{
47-
AVCodecContext& avCodecContext = _inputStream->getAudioCodec().getAVCodecContext();
48-
AVCodec& avCodec = _inputStream->getAudioCodec().getAVCodec();
49-
50-
avCodecContext.channels = _inputStream->getAudioCodec().getAudioFrameDesc().getChannels();
51-
52-
int ret = avcodec_open2( &avCodecContext, &avCodec, NULL );
53-
54-
if( ret < 0 || &avCodecContext == NULL || &avCodec == NULL )
55-
{
56-
std::string msg = "unable open audio codec: ";
57-
msg += avCodec.long_name;
58-
msg += " (";
59-
msg += avCodec.name;
60-
msg += ") ";
61-
avcodec_close( &avCodecContext );
62-
63-
char err[AV_ERROR_MAX_STRING_SIZE];
64-
av_strerror( ret, err, sizeof(err) );
65-
msg += err;
66-
67-
throw std::runtime_error( msg );
68-
}
47+
_inputStream->getAudioCodec().open();
6948

7049
#if LIBAVCODEC_VERSION_MAJOR > 54
7150
_frame = av_frame_alloc();

src/AvTranscoder/decoder/VideoDecoder.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,8 @@ VideoDecoder::~VideoDecoder()
4242

4343
void VideoDecoder::setup()
4444
{
45-
AVCodecContext& avCodecContext = _inputStream->getVideoCodec().getAVCodecContext();
46-
AVCodec& avCodec = _inputStream->getVideoCodec().getAVCodec();
47-
48-
// if( avCodec->capabilities & CODEC_CAP_TRUNCATED )
49-
// avCodecContext->flags |= CODEC_FLAG_TRUNCATED;
50-
51-
int ret = avcodec_open2( &avCodecContext, &avCodec, NULL );
52-
53-
if( ret < 0 || &avCodecContext == NULL || &avCodec == NULL )
54-
{
55-
std::string msg = "unable open video codec: ";
56-
msg += avCodec.long_name;
57-
msg += " (";
58-
msg += avCodec.name;
59-
msg += ")";
60-
avcodec_close( &avCodecContext );
61-
throw std::runtime_error( msg );
62-
}
45+
46+
_inputStream->getVideoCodec().open();
6347

6448
#if LIBAVCODEC_VERSION_MAJOR > 54
6549
_frame = av_frame_alloc();

src/AvTranscoder/encoder/AudioEncoder.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,7 @@ AudioEncoder::AudioEncoder( const std::string& audioCodecName )
1919

2020
void AudioEncoder::setup()
2121
{
22-
av_register_all();
23-
24-
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
25-
const AVCodec& avCodec = _codec.getAVCodec();
26-
27-
// try to open encoder with parameters.
28-
int ret = avcodec_open2( &avCodecContext, &avCodec, NULL );
29-
if( ret < 0 )
30-
{
31-
char err[AV_ERROR_MAX_STRING_SIZE];
32-
av_strerror( ret, err, sizeof(err) );
33-
std::string msg = "could not open audio encoder " + _codec.getCodecName() +": ";
34-
msg += err;
35-
throw std::runtime_error( msg );
36-
}
22+
_codec.open();
3723
}
3824

3925
bool AudioEncoder::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )

src/AvTranscoder/encoder/VideoEncoder.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,7 @@ VideoEncoder::VideoEncoder( const std::string& videoCodecName )
2020

2121
void VideoEncoder::setup()
2222
{
23-
av_register_all();
24-
25-
AVCodecContext& avCodecContext( _codec.getAVCodecContext() );
26-
27-
if( &avCodecContext == NULL )
28-
{
29-
throw std::runtime_error( "could not allocate video codec context" );
30-
}
31-
32-
// try to open encoder with parameters
33-
int ret = avcodec_open2( &avCodecContext, &_codec.getAVCodec(), NULL );
34-
if( ret < 0 )
35-
{
36-
char err[AV_ERROR_MAX_STRING_SIZE];
37-
av_strerror( ret, err, sizeof(err) );
38-
std::string msg = "could not open video encoder " + _codec.getCodecName() +": ";
39-
msg += err;
40-
throw std::runtime_error( msg );
41-
}
23+
_codec.open();
4224
}
4325

4426

0 commit comments

Comments
 (0)