Skip to content

Commit 10f1a9d

Browse files
committed
Merge pull request #42 from cchampet/fix_h264
Fix decoding problems of some codecs (h264)
2 parents 955da92 + be5fe11 commit 10f1a9d

File tree

15 files changed

+67
-101
lines changed

15 files changed

+67
-101
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
avcodec_register_all(); // TODO: call only once
@@ -25,6 +26,7 @@ ICodec::ICodec( const ECodecType type, const std::string& codecName )
2526
ICodec::ICodec( const ECodecType type, const AVCodecID codecId )
2627
: _avCodecContext( NULL )
2728
, _avCodec( NULL )
29+
, _isCodecContextAllocated( true )
2830
, _type( type )
2931
{
3032
avcodec_register_all(); // TODO: call only once
@@ -34,13 +36,50 @@ ICodec::ICodec( const ECodecType type, const AVCodecID codecId )
3436
loadCodecOptions();
3537
}
3638

39+
ICodec::ICodec( const ECodecType type, AVCodecContext& avCodecContext )
40+
: _avCodecContext( &avCodecContext )
41+
, _avCodec( NULL )
42+
, _isCodecContextAllocated( false )
43+
, _type( type )
44+
{
45+
avcodec_register_all(); // TODO: call only once
46+
47+
setCodec( type, _avCodecContext->codec_id );
48+
}
49+
3750
ICodec::~ICodec()
3851
{
52+
if( ! _isCodecContextAllocated )
53+
return;
54+
3955
avcodec_close( _avCodecContext );
4056
av_free( _avCodecContext );
4157
_avCodecContext = NULL;
4258
}
4359

60+
void ICodec::open()
61+
{
62+
if( ! _avCodecContext )
63+
throw std::runtime_error( "unable to open a codec with no codec context" );
64+
65+
int ret = avcodec_open2( _avCodecContext, _avCodec, NULL );
66+
if( ret < 0 )
67+
{
68+
std::string msg = "unable open codec: ";
69+
msg += _avCodec->long_name;
70+
msg += " (";
71+
msg += _avCodec->name;
72+
msg += ") ";
73+
avcodec_close( _avCodecContext );
74+
75+
char err[AV_ERROR_MAX_STRING_SIZE];
76+
av_strerror( ret, err, sizeof(err) );
77+
msg += err;
78+
79+
throw std::runtime_error( msg );
80+
}
81+
}
82+
4483
std::string ICodec::getCodecName() const
4584
{
4685
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)