Skip to content

Commit 30b788b

Browse files
author
Clement Champetier
committed
Remove CodecContext class
ICodec class has an AVCodecContext + a map of options.
1 parent 37687f0 commit 30b788b

File tree

12 files changed

+93
-145
lines changed

12 files changed

+93
-145
lines changed

app/optionChecker/optionChecker.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <AvTranscoder/util.hpp>
2-
#include "AvTranscoder/option/FormatContext.hpp"
3-
#include <AvTranscoder/option/CodecContext.hpp>
2+
#include <AvTranscoder/option/FormatContext.hpp>
43
#include <AvTranscoder/option/Option.hpp>
54
#include <AvTranscoder/file/InputFile.hpp>
5+
#include <AvTranscoder/codec/VideoCodec.hpp>
66

77
#include <string>
88
#include <iostream>
@@ -73,8 +73,8 @@ void optionChecker( const std::string& inputfilename )
7373
displayOptions( formatOptions );
7474

7575
// codec options
76-
avtranscoder::CodecContext codecContext( AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM );
77-
std::vector<avtranscoder::Option> codecOptions = codecContext.getOptions();
76+
avtranscoder::VideoCodec codec( avtranscoder::eCodecTypeDecoder, "mpeg2video" );
77+
std::vector<avtranscoder::Option> codecOptions = codec.getOptions();
7878
displayOptions( codecOptions );
7979

8080
// pixel formats

src/AvTranscoder/codec/AudioCodec.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ AudioCodec::AudioCodec( const ICodec& codec )
2222

2323
AudioFrameDesc AudioCodec::getAudioFrameDesc() const
2424
{
25-
assert( _codecContext != NULL );
26-
27-
AudioFrameDesc audioFrameDesc( _codecContext->getAVCodecContext().sample_rate, _codecContext->getAVCodecContext().channels, _codecContext->getAVCodecContext().sample_fmt );
25+
assert( _avCodecContext != NULL );
26+
AudioFrameDesc audioFrameDesc( _avCodecContext->sample_rate, _avCodecContext->channels, _avCodecContext->sample_fmt );
2827
// audioFrameDesc.setFps( 25 );
29-
3028
return audioFrameDesc;
3129
}
3230

@@ -37,9 +35,9 @@ void AudioCodec::setAudioParameters( const AudioFrameDesc& audioFrameDesc )
3735

3836
void AudioCodec::setAudioParameters( const size_t sampleRate, const size_t channels, const AVSampleFormat sampleFormat )
3937
{
40-
_codecContext->getAVCodecContext().sample_rate = sampleRate;
41-
_codecContext->getAVCodecContext().channels = channels;
42-
_codecContext->getAVCodecContext().sample_fmt = sampleFormat;
38+
_avCodecContext->sample_rate = sampleRate;
39+
_avCodecContext->channels = channels;
40+
_avCodecContext->sample_fmt = sampleFormat;
4341
}
4442

4543
}

src/AvTranscoder/codec/ICodec.cpp

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,34 @@ extern "C" {
1111
namespace avtranscoder {
1212

1313
ICodec::ICodec( const ECodecType type, const std::string& codecName )
14-
: _codecContext( NULL )
14+
: _avCodecContext( NULL )
1515
, _avCodec( NULL )
1616
, _type( type )
1717
{
18+
avcodec_register_all(); // TODO: call only once
19+
1820
setCodec( type, codecName );
21+
allocateContext();
22+
loadCodecOptions();
1923
}
2024

2125
ICodec::ICodec( const ECodecType type, const AVCodecID codecId )
22-
: _codecContext( NULL )
26+
: _avCodecContext( NULL )
2327
, _avCodec( NULL )
2428
, _type( type )
2529
{
30+
avcodec_register_all(); // TODO: call only once
31+
2632
setCodec( type, codecId );
33+
allocateContext();
34+
loadCodecOptions();
2735
}
2836

2937
ICodec::~ICodec()
3038
{
31-
delete _codecContext;
39+
avcodec_close( _avCodecContext );
40+
av_free( _avCodecContext );
41+
_avCodecContext = NULL;
3242
}
3343

3444
std::string ICodec::getCodecName() const
@@ -45,8 +55,18 @@ AVCodecID ICodec::getCodecId() const
4555

4656
int ICodec::getLatency() const
4757
{
48-
assert( _codecContext != NULL );
49-
return _codecContext->getAVCodecContext().delay;
58+
assert( _avCodecContext != NULL );
59+
return _avCodecContext->delay;
60+
}
61+
62+
std::vector<Option> ICodec::getOptions()
63+
{
64+
std::vector<Option> optionsArray;
65+
for( OptionMap::iterator it = _options.begin(); it != _options.end(); ++it )
66+
{
67+
optionsArray.push_back( it->second );
68+
}
69+
return optionsArray;
5070
}
5171

5272
void ICodec::setCodec( const ECodecType type, const std::string& codecName )
@@ -70,18 +90,36 @@ void ICodec::setCodec( const ECodecType type, const AVCodecID codecId )
7090
return;
7191
}
7292

73-
avcodec_register_all();
74-
7593
if( type == eCodecTypeEncoder )
7694
{
7795
_avCodec = avcodec_find_encoder( codecId );
78-
_codecContext = new CodecContext( *_avCodec, AV_OPT_FLAG_ENCODING_PARAM );
96+
if( _avCodecContext )
97+
_avCodecContext->codec = _avCodec;
7998
}
8099
else if( type == eCodecTypeDecoder )
81100
{
82101
_avCodec = avcodec_find_decoder( codecId );
83-
_codecContext = new CodecContext( *_avCodec, AV_OPT_FLAG_DECODING_PARAM );
102+
if( _avCodecContext )
103+
_avCodecContext->codec = _avCodec;
84104
}
85105
}
86106

107+
108+
void ICodec::allocateContext()
109+
{
110+
_avCodecContext = avcodec_alloc_context3( _avCodec );
111+
if( ! _avCodecContext )
112+
{
113+
throw std::runtime_error( "unable to allocate the codecContext and set its fields to default values" );
114+
}
115+
}
116+
117+
void ICodec::loadCodecOptions()
118+
{
119+
if( _type == eCodecTypeEncoder )
120+
loadOptions( _options, _avCodecContext, AV_OPT_FLAG_ENCODING_PARAM );
121+
else if( _type == eCodecTypeDecoder )
122+
loadOptions( _options, _avCodecContext, AV_OPT_FLAG_DECODING_PARAM );
123+
}
124+
87125
}

src/AvTranscoder/codec/ICodec.hpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _AV_TRANSCODER_CODEC_ICODEC_HPP_
33

44
#include <AvTranscoder/common.hpp>
5-
#include <AvTranscoder/option/CodecContext.hpp>
5+
#include <AvTranscoder/option/Option.hpp>
66

77
#include <string>
88

@@ -31,21 +31,32 @@ class AvExport ICodec
3131
ECodecType getCodecType() const { return _type; }
3232
int getLatency() const;
3333

34-
CodecContext& getCodecContext() const { return *_codecContext; }
34+
OptionArray getOptions(); ///< Get options as array
35+
OptionMap& getOptionsMap() { return _options; } ///< Get options as map
36+
37+
Option& getOption( const std::string& optionName ) { return _options.at(optionName); }
3538

3639
void setCodec( const ECodecType type, const std::string& codecName );
3740
void setCodec( const ECodecType type, const AVCodecID codecId );
3841

3942
#ifndef SWIG
40-
AVCodec& getAVCodec() const { return *_avCodec; }
41-
AVCodecContext& getAVCodecContext() const { return _codecContext->getAVCodecContext(); }
43+
AVCodecContext& getAVCodecContext() { return *_avCodecContext; }
44+
const AVCodecContext& getAVCodecContext() const { return *_avCodecContext; }
45+
AVCodec& getAVCodec() { return *_avCodec; }
46+
const AVCodec& getAVCodec() const { return *_avCodec; }
4247
#endif
4348

49+
private:
50+
void allocateContext();
51+
void loadCodecOptions();
52+
4453
protected:
45-
CodecContext* _codecContext; ///< Contains the AVCodecContext, which is the full codec instance description (has ownership)
54+
AVCodecContext* _avCodecContext; ///< Full codec instance description (has ownership)
4655
AVCodec* _avCodec; ///< Codec abstract description
4756

4857
ECodecType _type;
58+
59+
OptionMap _options;
4960
};
5061

5162
}

src/AvTranscoder/codec/VideoCodec.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@ VideoCodec::VideoCodec( const ICodec& codec )
2222

2323
VideoFrameDesc VideoCodec::getVideoFrameDesc() const
2424
{
25-
assert( _codecContext != NULL );
26-
27-
VideoFrameDesc videoFrameDesc( _codecContext->getAVCodecContext().width, _codecContext->getAVCodecContext().height, _codecContext->getAVCodecContext().pix_fmt );
25+
assert( _avCodecContext != NULL );
26+
VideoFrameDesc videoFrameDesc( _avCodecContext->width, _avCodecContext->height, _avCodecContext->pix_fmt );
2827
return videoFrameDesc;
2928
}
3029

3130
std::pair< size_t, size_t > VideoCodec::getTimeBase() const
3231
{
33-
assert( _codecContext != NULL );
32+
assert( _avCodecContext != NULL );
3433
std::pair< size_t, size_t > timeBase;
35-
timeBase.first = _codecContext->getAVCodecContext().time_base.num;
36-
timeBase.second = _codecContext->getAVCodecContext().time_base.den;
34+
timeBase.first = _avCodecContext->time_base.num;
35+
timeBase.second = _avCodecContext->time_base.den;
3736
return timeBase;
3837
}
3938

@@ -44,16 +43,16 @@ void VideoCodec::setImageParameters( const VideoFrameDesc& videoFrameDesc )
4443

4544
void VideoCodec::setImageParameters( const size_t width, const size_t height, const AVPixelFormat& pixel )
4645
{
47-
_codecContext->getAVCodecContext().width = width;
48-
_codecContext->getAVCodecContext().height = height;
49-
_codecContext->getAVCodecContext().pix_fmt = pixel;
46+
_avCodecContext->width = width;
47+
_avCodecContext->height = height;
48+
_avCodecContext->pix_fmt = pixel;
5049
}
5150

5251
void VideoCodec::setTimeBase( const size_t num, const size_t den, const size_t ticksPerFrame )
5352
{
54-
_codecContext->getAVCodecContext().time_base.num = num;
55-
_codecContext->getAVCodecContext().time_base.den = den;
56-
_codecContext->getAVCodecContext().ticks_per_frame = ticksPerFrame;
53+
_avCodecContext->time_base.num = num;
54+
_avCodecContext->time_base.den = den;
55+
_avCodecContext->ticks_per_frame = ticksPerFrame;
5756
}
5857

5958
}

src/AvTranscoder/essenceStream/AvInputVideo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void AvInputVideo::setProfile( const ProfileLoader::Profile& profile )
147147

148148
try
149149
{
150-
Option& decodeOption = _inputStream->getVideoCodec().getCodecContext().getOption( (*it).first );
150+
Option& decodeOption = _inputStream->getVideoCodec().getOption( (*it).first );
151151
decodeOption.setString( (*it).second );
152152
}
153153
catch( std::exception& e )

src/AvTranscoder/essenceStream/AvOutputAudio.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,11 @@ void AvOutputAudio::setup()
2121
{
2222
av_register_all();
2323

24-
AVCodecContext& avCodecContext( _codec.getAVCodecContext() );
24+
AVCodecContext& avCodecContext = _codec.getAVCodecContext();
25+
const AVCodec& avCodec = _codec.getAVCodec();
2526

26-
if( &avCodecContext == NULL )
27-
{
28-
throw std::runtime_error( "could not allocate audio codec context" );
29-
}
30-
3127
// try to open encoder with parameters.
32-
int ret = avcodec_open2( &avCodecContext, &_codec.getAVCodec(), NULL );
28+
int ret = avcodec_open2( &avCodecContext, &avCodec, NULL );
3329
if( ret < 0 )
3430
{
3531
char err[AV_ERROR_MAX_STRING_SIZE];
@@ -183,7 +179,7 @@ void AvOutputAudio::setProfile( const ProfileLoader::Profile& profile, const Aud
183179

184180
try
185181
{
186-
Option& encodeOption = _codec.getCodecContext().getOption( (*it).first );
182+
Option& encodeOption = _codec.getOption( (*it).first );
187183
encodeOption.setString( (*it).second );
188184
}
189185
catch( std::exception& e )
@@ -202,7 +198,7 @@ void AvOutputAudio::setProfile( const ProfileLoader::Profile& profile, const Aud
202198

203199
try
204200
{
205-
Option& encodeOption = _codec.getCodecContext().getOption( (*it).first );
201+
Option& encodeOption = _codec.getOption( (*it).first );
206202
encodeOption.setString( (*it).second );
207203
}
208204
catch( std::exception& e )

src/AvTranscoder/essenceStream/AvOutputVideo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void AvOutputVideo::setProfile( const ProfileLoader::Profile& profile, const avt
172172

173173
try
174174
{
175-
Option& encodeOption = _codec.getCodecContext().getOption( (*it).first );
175+
Option& encodeOption = _codec.getOption( (*it).first );
176176
encodeOption.setString( (*it).second );
177177
}
178178
catch( std::exception& e )
@@ -192,7 +192,7 @@ void AvOutputVideo::setProfile( const ProfileLoader::Profile& profile, const avt
192192

193193
try
194194
{
195-
Option& encodeOption = _codec.getCodecContext().getOption( (*it).first );
195+
Option& encodeOption = _codec.getOption( (*it).first );
196196
encodeOption.setString( (*it).second );
197197
}
198198
catch( std::exception& e )

src/AvTranscoder/option/CodecContext.cpp

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)