Skip to content

Commit 5481a5b

Browse files
author
Clement Champetier
committed
Merge branch 'develop' of https://github.com/mikrosimage/avTranscoder into dev_preloadCodecsAndFormats
Conflicts: src/AvTranscoder/encoder/AudioEncoder.cpp src/AvTranscoder/encoder/VideoEncoder.cpp
2 parents 3ef900e + 1b94232 commit 5481a5b

File tree

17 files changed

+86
-118
lines changed

17 files changed

+86
-118
lines changed

ressource/v_h264.prf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
avProfileName=h264
2+
avProfileLongName=h264 High
3+
avProfileType=avProfileTypeVideo
4+
codec=h264
5+
profile=High
6+
r=25

ressource/v_x264.prf

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

src/AvTranscoder/codec/AudioCodec.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ AudioCodec::AudioCodec( const ECodecType type, const AVCodecID codecId )
1515
{
1616
}
1717

18-
AudioCodec::AudioCodec( const ICodec& codec )
19-
: ICodec( codec )
18+
AudioCodec::AudioCodec( const ECodecType type, AVCodecContext& avCodecContext )
19+
: ICodec( type, avCodecContext )
2020
{
2121
}
2222

src/AvTranscoder/codec/AudioCodec.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AvExport AudioCodec : public ICodec
1212
public:
1313
AudioCodec( const ECodecType type, const std::string& codecName = "" );
1414
AudioCodec( const ECodecType type, const AVCodecID codecId );
15-
AudioCodec( const ICodec& codec );
15+
AudioCodec( const ECodecType type, AVCodecContext& avCodecContext );
1616

1717
AudioFrameDesc getAudioFrameDesc() const;
1818

src/AvTranscoder/codec/DataCodec.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ DataCodec::DataCodec( const ECodecType type, const AVCodecID codecId )
1313
{
1414
}
1515

16-
DataCodec::DataCodec( const ICodec& codec )
17-
: ICodec( codec )
16+
DataCodec::DataCodec( const ECodecType type, AVCodecContext& avCodecContext )
17+
: ICodec( type, avCodecContext )
1818
{
1919
}
2020

src/AvTranscoder/codec/DataCodec.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class AvExport DataCodec : public ICodec
1111
public:
1212
DataCodec( const ECodecType type, const std::string& codecName = "" );
1313
DataCodec( const ECodecType type, const AVCodecID codecId );
14-
DataCodec( const ICodec& codec );
14+
DataCodec( const ECodecType type, AVCodecContext& avCodecContext );
1515
};
1616

1717
}

src/AvTranscoder/codec/ICodec.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace avtranscoder {
1313
ICodec::ICodec( const ECodecType type, const std::string& codecName )
1414
: _avCodecContext( NULL )
1515
, _avCodec( NULL )
16+
, _isCodecContextAllocated( true )
1617
, _type( type )
1718
{
1819
setCodec( type, codecName );
@@ -23,20 +24,58 @@ ICodec::ICodec( const ECodecType type, const std::string& codecName )
2324
ICodec::ICodec( const ECodecType type, const AVCodecID codecId )
2425
: _avCodecContext( NULL )
2526
, _avCodec( NULL )
27+
, _isCodecContextAllocated( true )
2628
, _type( type )
2729
{
2830
setCodec( type, codecId );
2931
allocateContext();
3032
loadCodecOptions();
3133
}
3234

35+
ICodec::ICodec( const ECodecType type, AVCodecContext& avCodecContext )
36+
: _avCodecContext( &avCodecContext )
37+
, _avCodec( NULL )
38+
, _isCodecContextAllocated( false )
39+
, _type( type )
40+
{
41+
avcodec_register_all(); // TODO: call only once
42+
43+
setCodec( type, _avCodecContext->codec_id );
44+
}
45+
3346
ICodec::~ICodec()
3447
{
48+
if( ! _isCodecContextAllocated )
49+
return;
50+
3551
avcodec_close( _avCodecContext );
3652
av_free( _avCodecContext );
3753
_avCodecContext = NULL;
3854
}
3955

56+
void ICodec::open()
57+
{
58+
if( ! _avCodecContext )
59+
throw std::runtime_error( "unable to open a codec with no codec context" );
60+
61+
int ret = avcodec_open2( _avCodecContext, _avCodec, NULL );
62+
if( ret < 0 )
63+
{
64+
std::string msg = "unable open codec: ";
65+
msg += _avCodec->long_name;
66+
msg += " (";
67+
msg += _avCodec->name;
68+
msg += ") ";
69+
avcodec_close( _avCodecContext );
70+
71+
char err[AV_ERROR_MAX_STRING_SIZE];
72+
av_strerror( ret, err, sizeof(err) );
73+
msg += err;
74+
75+
throw std::runtime_error( msg );
76+
}
77+
}
78+
4079
std::string ICodec::getCodecName() const
4180
{
4281
assert( _avCodec != NULL );

src/AvTranscoder/codec/ICodec.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ class AvExport ICodec
2323
public:
2424
ICodec( const ECodecType type, const std::string& codecName );
2525
ICodec( const ECodecType type, const AVCodecID codecId );
26+
ICodec( const ECodecType type, AVCodecContext& avCodecContext );
2627

2728
virtual ~ICodec() = 0;
2829

30+
/// Initialize the codec context.
31+
void open();
32+
2933
std::string getCodecName() const;
3034
AVCodecID getCodecId() const;
3135
ECodecType getCodecType() const { return _type; }
@@ -52,6 +56,7 @@ class AvExport ICodec
5256
protected:
5357
AVCodecContext* _avCodecContext; ///< Full codec instance description (has ownership)
5458
AVCodec* _avCodec; ///< Codec abstract description
59+
const bool _isCodecContextAllocated; ///< Is the AVCodecContext allocated by the class
5560

5661
ECodecType _type;
5762

src/AvTranscoder/codec/VideoCodec.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ VideoCodec::VideoCodec( const ECodecType type, const AVCodecID codecId )
1515
{
1616
}
1717

18-
VideoCodec::VideoCodec( const ICodec& codec )
19-
: ICodec( codec )
18+
VideoCodec::VideoCodec( const ECodecType type, AVCodecContext& avCodecContext )
19+
: ICodec( type, avCodecContext )
2020
{
2121
}
2222

src/AvTranscoder/codec/VideoCodec.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AvExport VideoCodec : public ICodec
1414
public:
1515
VideoCodec( const ECodecType type, const std::string& codecName = "" );
1616
VideoCodec( const ECodecType type, const AVCodecID codecId );
17-
VideoCodec( const ICodec& codec );
17+
VideoCodec( const ECodecType type, AVCodecContext& avCodecContext );
1818

1919
VideoFrameDesc getVideoFrameDesc() const;
2020
std::pair< size_t, size_t > getTimeBase() const;

0 commit comments

Comments
 (0)