Skip to content

Commit e967cdc

Browse files
author
Clement Champetier
committed
VideoProperties: get AVCodec attribute
This removes multi call to avcodec_find_decoder().
1 parent 9fe1359 commit e967cdc

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/AvTranscoder/mediaProperty/VideoProperties.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ namespace avtranscoder
2121

2222
VideoProperties::VideoProperties( const AVFormatContext* formatContext, const size_t index, IProgress& progress, const EAnalyseLevel level )
2323
: _formatContext( formatContext )
24-
, _codecContext( formatContext->streams[index]->codec )
24+
, _codecContext( NULL )
25+
, _codec( NULL )
2526
, _pixFmt( NULL )
2627
, _streamId( index )
2728
, _isInterlaced( false )
2829
, _isTopFieldFirst( false )
2930
, _gopStructure()
3031
{
32+
if( _formatContext )
33+
_codecContext = formatContext->streams[index]->codec;
34+
35+
if( _formatContext && _codecContext )
36+
_codec = avcodec_find_decoder( _codecContext->codec_id );
37+
3138
// Skip decoding for selected frames
3239
_codecContext->skip_frame = AVDISCARD_NONE;
3340

@@ -46,42 +53,41 @@ VideoProperties::VideoProperties( const AVFormatContext* formatContext, const si
4653

4754
std::string VideoProperties::getCodecName() const
4855
{
49-
AVCodec* codec = NULL;
50-
if( ( codec = avcodec_find_decoder( _codecContext->codec_id ) ) != NULL )
56+
if( _codecContext && _codec )
5157
{
52-
if( codec->capabilities & CODEC_CAP_TRUNCATED )
58+
if( _codec->capabilities & CODEC_CAP_TRUNCATED )
5359
_codecContext->flags|= CODEC_FLAG_TRUNCATED;
5460

55-
return std::string( codec->name );
61+
if( _codec->name )
62+
return std::string( _codec->name );
5663
}
5764
return "unknown codec";
5865
}
5966

6067
std::string VideoProperties::getCodecLongName() const
6168
{
62-
AVCodec* codec = NULL;
63-
if( ( codec = avcodec_find_decoder( _codecContext->codec_id ) ) != NULL )
69+
if( _codecContext && _codec )
6470
{
65-
if( codec->capabilities & CODEC_CAP_TRUNCATED )
71+
if( _codec->capabilities & CODEC_CAP_TRUNCATED )
6672
_codecContext->flags|= CODEC_FLAG_TRUNCATED;
6773

68-
return std::string( codec->long_name );
74+
if( _codec->long_name )
75+
return std::string( _codec->long_name );
6976
}
7077
return "unknown codec";
7178
}
7279

7380
std::string VideoProperties::getProfileName() const
7481
{
75-
AVCodec* codec = NULL;
76-
if( ( codec = avcodec_find_decoder( _codecContext->codec_id ) ) != NULL )
82+
if( _codecContext && _codec )
7783
{
78-
if( codec->capabilities & CODEC_CAP_TRUNCATED )
84+
if( _codec->capabilities & CODEC_CAP_TRUNCATED )
7985
_codecContext->flags|= CODEC_FLAG_TRUNCATED;
8086

8187
if( _codecContext->profile != -99 )
8288
{
8389
const char* profile;
84-
if( ( profile = av_get_profile_name( codec, _codecContext->profile ) ) != NULL )
90+
if( ( profile = av_get_profile_name( _codec, _codecContext->profile ) ) != NULL )
8591
return std::string( profile );
8692
}
8793
}
@@ -363,10 +369,9 @@ bool VideoProperties::hasAlpha() const
363369

364370
void VideoProperties::analyseGopStructure( IProgress& progress )
365371
{
366-
AVCodec* codec = NULL;
367-
if( ( codec = avcodec_find_decoder( _codecContext->codec_id ) ) != NULL )
372+
if( _formatContext && _codecContext && _codec )
368373
{
369-
if( codec->capabilities & CODEC_CAP_TRUNCATED )
374+
if( _codec->capabilities & CODEC_CAP_TRUNCATED )
370375
_codecContext->flags|= CODEC_FLAG_TRUNCATED;
371376

372377
if( _codecContext->width && _codecContext->height )
@@ -380,7 +385,7 @@ void VideoProperties::analyseGopStructure( IProgress& progress )
380385
#endif
381386

382387
av_init_packet( &pkt );
383-
avcodec_open2( _codecContext, codec, NULL );
388+
avcodec_open2( _codecContext, _codec, NULL );
384389

385390
int count = 0;
386391
int gotFrame = 0;

src/AvTranscoder/mediaProperty/VideoProperties.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class AvExport VideoProperties
109109
private:
110110
const AVFormatContext* _formatContext; ///< Has link (no ownership)
111111
AVCodecContext* _codecContext; ///< Has link (no ownership)
112+
AVCodec* _codec; ///< Has link (no ownership)
112113
const AVPixFmtDescriptor* _pixFmt; ///< Has link (no ownership)
113114

114115
size_t _streamId;

0 commit comments

Comments
 (0)