Skip to content

Commit 286f602

Browse files
committed
IOutputEssence: move codec attribute to subclasses
* OutputEssence: * has a virtual function getCodec, which needed to be implemented in subclasses. * doesn't have any data, remove constructor. * AvOutputVideo / AvOutputAudio: * have a codec (VideoCodec or AudioCodec). * implement getCodec function. * have a new constructor, to create the codec based on a codecName. * avoid static cast to the right type of codec when setProfile.
1 parent 3889d71 commit 286f602

File tree

6 files changed

+36
-32
lines changed

6 files changed

+36
-32
lines changed

src/AvTranscoder/essenceStream/AvOutputAudio.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ namespace avtranscoder
1515
{
1616

1717
AvOutputAudio::AvOutputAudio()
18-
: IOutputEssence( "pcm_s16le" )
18+
: _codec( eCodecTypeEncoder, "pcm_s16le" )
1919
{
2020
}
2121

2222
void AvOutputAudio::setup()
2323
{
2424
av_register_all(); // Warning: should be called only once
2525

26-
AVCodecContext* codecContext( _codedDesc.getAVCodecContext() );
26+
AVCodecContext* codecContext( _codec.getAVCodecContext() );
2727

2828
if( codecContext == NULL )
2929
{
3030
throw std::runtime_error( "could not allocate audio codec context" );
3131
}
3232

3333
// try to open encoder with parameters.
34-
int ret = avcodec_open2( codecContext, _codedDesc.getAVCodec(), NULL );
34+
int ret = avcodec_open2( codecContext, _codec.getAVCodec(), NULL );
3535
if( ret < 0 )
3636
{
3737
char err[250];
@@ -50,7 +50,7 @@ bool AvOutputAudio::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
5050
AVFrame* frame = avcodec_alloc_frame();
5151
#endif
5252

53-
AVCodecContext* codecContext = _codedDesc.getAVCodecContext();
53+
AVCodecContext* codecContext = _codec.getAVCodecContext();
5454

5555
// Set default frame parameters
5656
#if LIBAVCODEC_VERSION_MAJOR > 54
@@ -138,7 +138,7 @@ bool AvOutputAudio::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
138138

139139
bool AvOutputAudio::encodeFrame( Frame& codedFrame )
140140
{
141-
AVCodecContext* codecContext = _codedDesc.getAVCodecContext();
141+
AVCodecContext* codecContext = _codec.getAVCodecContext();
142142

143143
AVPacket packet;
144144
av_init_packet( &packet );
@@ -179,11 +179,10 @@ void AvOutputAudio::setProfile( const Profile::ProfileDesc& desc, const AudioFra
179179
throw std::runtime_error( "The profile " + desc.find( Profile::avProfileIdentificatorHuman )->second + " is invalid." );
180180
}
181181

182-
_codedDesc.setEncoderCodec( desc.find( Profile::avProfileCodec )->second );
183-
184-
static_cast<AudioCodec>( _codedDesc ).setAudioParameters( frameDesc );
182+
_codec.setEncoderCodec( desc.find( Profile::avProfileCodec )->second );
183+
_codec.setAudioParameters( frameDesc );
185184

186-
Context codecContext( _codedDesc.getAVCodecContext() );
185+
Context codecContext( _codec.getAVCodecContext() );
187186

188187
for( Profile::ProfileDesc::const_iterator it = desc.begin(); it != desc.end(); ++it )
189188
{

src/AvTranscoder/essenceStream/AvOutputAudio.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ class AvExport AvOutputAudio : public IOutputEssence
2727

2828
void setProfile( const Profile::ProfileDesc& desc, const AudioFrameDesc& frameDesc );
2929

30-
AudioCodec getAudioCodec() { return _codedDesc; }
30+
ICodec& getCodec() { return _codec; }
31+
AudioCodec& getAudioCodec() { return _codec; }
32+
33+
private:
34+
AudioCodec _codec;
3135
};
3236

3337
}

src/AvTranscoder/essenceStream/AvOutputVideo.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ namespace avtranscoder
1616
{
1717

1818
AvOutputVideo::AvOutputVideo( )
19-
: IOutputEssence( "mpeg2video" )
19+
: _codec( eCodecTypeEncoder, "mpeg2video" )
2020
{
2121
}
2222

2323
void AvOutputVideo::setup( )
2424
{
2525
av_register_all(); // Warning: should be called only once
2626

27-
AVCodecContext* codecContext( _codedDesc.getAVCodecContext() );
27+
AVCodecContext* codecContext( _codec.getAVCodecContext() );
2828

2929
if( codecContext == NULL )
3030
{
3131
throw std::runtime_error( "could not allocate video codec context" );
3232
}
3333

3434
// try to open encoder with parameters
35-
int ret = avcodec_open2( codecContext, _codedDesc.getAVCodec(), NULL );
35+
int ret = avcodec_open2( codecContext, _codec.getAVCodec(), NULL );
3636
if( ret < 0 )
3737
{
3838
char err[250];
@@ -52,7 +52,7 @@ bool AvOutputVideo::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
5252
AVFrame* frame = avcodec_alloc_frame();
5353
#endif
5454

55-
AVCodecContext* codecContext = this->_codedDesc.getAVCodecContext();
55+
AVCodecContext* codecContext = this->_codec.getAVCodecContext();
5656

5757
// Set default frame parameters
5858
#if LIBAVCODEC_VERSION_MAJOR > 54
@@ -139,7 +139,7 @@ bool AvOutputVideo::encodeFrame( const Frame& sourceFrame, Frame& codedFrame )
139139

140140
bool AvOutputVideo::encodeFrame( Frame& codedFrame )
141141
{
142-
AVCodecContext* codecContext = _codedDesc.getAVCodecContext();
142+
AVCodecContext* codecContext = _codec.getAVCodecContext();
143143

144144
AVPacket packet;
145145
av_init_packet( &packet );
@@ -181,14 +181,14 @@ void AvOutputVideo::setProfile( const Profile::ProfileDesc& desc, const avtransc
181181
throw std::runtime_error( "The profile " + desc.find( Profile::avProfileIdentificatorHuman )->second + " is invalid." );
182182
}
183183

184-
_codedDesc.setEncoderCodec( desc.find( Profile::avProfileCodec )->second );
185-
184+
_codec.setEncoderCodec( desc.find( Profile::avProfileCodec )->second );
185+
186186
const size_t frameRate = std::strtoul( desc.find( Profile::avProfileFrameRate )->second.c_str(), NULL, 0 );
187-
static_cast<VideoCodec>( _codedDesc ).setTimeBase( 1, frameRate );
188-
189-
static_cast<VideoCodec>( _codedDesc ).setImageParameters( frameDesc );
187+
_codec.setTimeBase( 1, frameRate );
188+
189+
_codec.setImageParameters( frameDesc );
190190

191-
Context codecContext( _codedDesc.getAVCodecContext() );
191+
Context codecContext( _codec.getAVCodecContext() );
192192

193193
for( Profile::ProfileDesc::const_iterator it = desc.begin(); it != desc.end(); ++it )
194194
{

src/AvTranscoder/essenceStream/AvOutputVideo.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ class AvExport AvOutputVideo : public IOutputEssence
2727

2828
void setProfile( const Profile::ProfileDesc& desc, const avtranscoder::VideoFrameDesc& frameDesc );
2929

30-
VideoCodec getVideoCodec() { return _codedDesc; }
30+
ICodec& getCodec() { return _codec; }
31+
VideoCodec& getVideoCodec() { return _codec; }
32+
33+
private:
34+
VideoCodec _codec;
3135
};
3236

3337
}

src/AvTranscoder/essenceStream/IOutputEssence.hpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ namespace avtranscoder
1010
class AvExport IOutputEssence
1111
{
1212
public:
13-
IOutputEssence( const std::string& codecName )
14-
: _codedDesc( eCodecTypeEncoder, codecName )
15-
{}
16-
1713
virtual ~IOutputEssence()
1814
{}
1915

@@ -37,10 +33,11 @@ class AvExport IOutputEssence
3733
*/
3834
virtual bool encodeFrame( Frame& codedFrame ) = 0;
3935

40-
ICodec& getCodedDesc() { return _codedDesc; }
41-
42-
protected:
43-
ICodec _codedDesc;
36+
/**
37+
* @brief Get codec used for encoding.
38+
* @return a reference to the codec
39+
*/
40+
virtual ICodec& getCodec() = 0;
4441

4542
};
4643

src/AvTranscoder/transcoder/StreamTranscoder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ void StreamTranscoder::init()
257257
if( ! _inputEssence )
258258
return;
259259

260-
int latency = _outputEssence->getCodedDesc().getLatency();
260+
int latency = _outputEssence->getCodec().getLatency();
261261
if( _verbose )
262262
std::cout << "latency of stream: " << latency << std::endl;
263263

264264
if( ! latency ||
265-
latency < _outputEssence->getCodedDesc().getAVCodecContext()->frame_number )
265+
latency < _outputEssence->getCodec().getAVCodecContext()->frame_number )
266266
return;
267267

268268
while( ( --latency ) > 0 )

0 commit comments

Comments
 (0)