Skip to content

Commit 73b9335

Browse files
author
Clement Champetier
committed
ICodec: can be instanciate from an external AVCodecContext
* ICodec: add attribute to check if the AVCodecContext is allocated by the class or not. * InputStream: create the corresponding codec from the existing AVCodecContext. * This commit fixes decoding problems of some codecs (h264...), which need specific values of the AVCodecContext, already set by the AVFormatContext.
1 parent 660a5ee commit 73b9335

File tree

9 files changed

+39
-12
lines changed

9 files changed

+39
-12
lines changed

src/AvTranscoder/codec/AudioCodec.cpp

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

18+
AudioCodec::AudioCodec( const ECodecType type, AVCodecContext& avCodecContext )
19+
: ICodec( type, avCodecContext )
20+
{
21+
}
22+
1823
AudioCodec::AudioCodec( const ICodec& codec )
1924
: ICodec( codec )
2025
{

src/AvTranscoder/codec/AudioCodec.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +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 ECodecType type, AVCodecContext& avCodecContext );
1516
AudioCodec( const ICodec& codec );
1617

1718
AudioFrameDesc getAudioFrameDesc() const;

src/AvTranscoder/codec/DataCodec.cpp

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

16+
DataCodec::DataCodec( const ECodecType type, AVCodecContext& avCodecContext )
17+
: ICodec( type, avCodecContext )
18+
{
19+
}
20+
1621
DataCodec::DataCodec( const ICodec& codec )
1722
: ICodec( codec )
1823
{

src/AvTranscoder/codec/DataCodec.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +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 ECodecType type, AVCodecContext& avCodecContext );
1415
DataCodec( const ICodec& codec );
1516
};
1617

src/AvTranscoder/codec/ICodec.cpp

Lines changed: 16 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,8 +36,22 @@ 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;

src/AvTranscoder/codec/ICodec.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ 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

@@ -55,6 +56,7 @@ class AvExport ICodec
5556
protected:
5657
AVCodecContext* _avCodecContext; ///< Full codec instance description (has ownership)
5758
AVCodec* _avCodec; ///< Codec abstract description
59+
const bool _isCodecContextAllocated; ///< Is the AVCodecContext allocated by the class
5860

5961
ECodecType _type;
6062

src/AvTranscoder/codec/VideoCodec.cpp

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

18+
VideoCodec::VideoCodec( const ECodecType type, AVCodecContext& avCodecContext )
19+
: ICodec( type, avCodecContext )
20+
{
21+
}
22+
1823
VideoCodec::VideoCodec( const ICodec& codec )
1924
: ICodec( codec )
2025
{

src/AvTranscoder/codec/VideoCodec.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +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 ECodecType type, AVCodecContext& avCodecContext );
1718
VideoCodec( const ICodec& codec );
1819

1920
VideoFrameDesc getVideoFrameDesc() const;

src/AvTranscoder/stream/InputStream.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,17 @@ InputStream::InputStream( InputFile& inputFile, const size_t streamIndex )
2727
{
2828
case AVMEDIA_TYPE_VIDEO:
2929
{
30-
VideoCodec* videoCodec = new VideoCodec( eCodecTypeDecoder, context->codec_id );
31-
videoCodec->setImageParameters( context->width, context->height, context->pix_fmt );
32-
videoCodec->setTimeBase( context->time_base.num, context->time_base.den, context->ticks_per_frame );
33-
34-
_codec = videoCodec;
30+
_codec = new VideoCodec( eCodecTypeDecoder, *context );
3531
break;
3632
}
3733
case AVMEDIA_TYPE_AUDIO:
3834
{
39-
AudioCodec* audioCodec = new AudioCodec( eCodecTypeDecoder, context->codec_id );
40-
audioCodec->setAudioParameters( context->sample_rate, context->channels, context->sample_fmt );
41-
42-
_codec = audioCodec;
35+
_codec = new AudioCodec( eCodecTypeDecoder, *context );
4336
break;
4437
}
4538
case AVMEDIA_TYPE_DATA:
4639
{
47-
DataCodec* dataCodec = new DataCodec( eCodecTypeDecoder, context->codec_id );
48-
49-
_codec= dataCodec;
40+
_codec = new DataCodec( eCodecTypeDecoder, *context );
5041
break;
5142
}
5243
default:

0 commit comments

Comments
 (0)