@@ -13,10 +13,13 @@ namespace avtranscoder
13
13
14
14
AudioProperties::AudioProperties ( const AVFormatContext* formatContext, const size_t index )
15
15
: _formatContext( formatContext )
16
- , _codecContext( formatContext->streams[index]->codec )
16
+ , _codecContext( NULL )
17
17
, _codec( NULL )
18
18
, _streamId( index )
19
19
{
20
+ if ( _formatContext )
21
+ _codecContext = formatContext->streams [index]->codec ;
22
+
20
23
if ( _formatContext && _codecContext )
21
24
_codec = avcodec_find_decoder ( _codecContext->codec_id );
22
25
}
@@ -38,6 +41,9 @@ std::string AudioProperties::getCodecLongName() const
38
41
39
42
std::string AudioProperties::getSampleFormatName () const
40
43
{
44
+ if ( ! _codecContext )
45
+ return " unknown codec context" ;
46
+
41
47
const char * fmtName = av_get_sample_fmt_name ( _codecContext->sample_fmt );
42
48
if ( fmtName )
43
49
return std::string ( fmtName );
@@ -46,6 +52,9 @@ std::string AudioProperties::getSampleFormatName() const
46
52
47
53
std::string AudioProperties::getSampleFormatLongName () const
48
54
{
55
+ if ( ! _codecContext )
56
+ return " unknown codec context" ;
57
+
49
58
switch ( _codecContext->sample_fmt )
50
59
{
51
60
case AV_SAMPLE_FMT_NONE:
@@ -78,13 +87,19 @@ std::string AudioProperties::getSampleFormatLongName() const
78
87
79
88
std::string AudioProperties::getChannelLayout () const
80
89
{
90
+ if ( ! _codecContext )
91
+ return " unknown codec context" ;
92
+
81
93
char buf1[1024 ];
82
94
av_get_channel_layout_string ( buf1, sizeof ( buf1 ), -1 , _codecContext->channel_layout );
83
95
return std::string ( buf1 );
84
96
}
85
97
86
98
std::string AudioProperties::getChannelName () const
87
99
{
100
+ if ( ! _codecContext )
101
+ return " unknown codec context" ;
102
+
88
103
const char * channelName = av_get_channel_name ( _codecContext->channel_layout );
89
104
if ( channelName )
90
105
return std::string ( channelName );
@@ -93,6 +108,9 @@ std::string AudioProperties::getChannelName() const
93
108
94
109
std::string AudioProperties::getChannelDescription () const
95
110
{
111
+ if ( ! _codecContext )
112
+ return " unknown codec context" ;
113
+
96
114
#ifdef FF_RESAMPLE_LIBRARY
97
115
const char * channelDescription = av_get_channel_description ( _codecContext->channel_layout );
98
116
if ( channelDescription )
0 commit comments