@@ -20,21 +20,14 @@ namespace avtranscoder
20
20
AvInputAudio::AvInputAudio ( AvInputStream& inputStream )
21
21
: IInputEssence()
22
22
, _inputStream ( &inputStream )
23
- , _codec ( NULL )
24
- , _codecContext ( NULL )
23
+ , _codec( eCodecTypeDecoder, inputStream.getAudioCodec().getCodecId() )
25
24
, _frame ( NULL )
26
25
, _selectedStream( -1 )
27
26
{
28
27
}
29
28
30
29
AvInputAudio::~AvInputAudio ()
31
30
{
32
- if ( _codecContext != NULL )
33
- {
34
- avcodec_close ( _codecContext );
35
- av_free ( _codecContext );
36
- _codecContext = NULL ;
37
- }
38
31
if ( _frame != NULL )
39
32
{
40
33
#if LIBAVCODEC_VERSION_MAJOR > 54
@@ -53,32 +46,21 @@ AvInputAudio::~AvInputAudio()
53
46
54
47
void AvInputAudio::setup ()
55
48
{
56
- avcodec_register_all ();
49
+ AVCodecContext* avCodecContext = _codec.getAVCodecContext ();
50
+ AVCodec* avCodec = _codec.getAVCodec ();
57
51
58
- _codec = avcodec_find_decoder ( _inputStream->getAudioCodec ().getCodecId () );
59
- if ( _codec == NULL )
60
- {
61
- throw std::runtime_error ( " codec not supported" );
62
- }
52
+ avCodecContext->channels = _inputStream->getAudioCodec ().getChannels ();
63
53
64
- _codecContext = avcodec_alloc_context3 ( _codec );
65
- if ( _codecContext == NULL )
66
- {
67
- throw std::runtime_error ( " unable to find context for codec" );
68
- }
69
-
70
- _codecContext->channels = _inputStream->getAudioCodec ().getChannels ();
71
-
72
- int ret = avcodec_open2 ( _codecContext, _codec, NULL );
54
+ int ret = avcodec_open2 ( avCodecContext, avCodec, NULL );
73
55
74
- if ( ret < 0 || _codecContext == NULL || _codec == NULL )
56
+ if ( ret < 0 || avCodecContext == NULL || avCodec == NULL )
75
57
{
76
58
std::string msg = " unable open audio codec: " ;
77
- msg += _codec ->long_name ;
59
+ msg += avCodec ->long_name ;
78
60
msg += " (" ;
79
- msg += _codec ->name ;
61
+ msg += avCodec ->name ;
80
62
msg += " )" ;
81
- avcodec_close ( _codecContext );
63
+ avcodec_close ( avCodecContext );
82
64
83
65
char err[250 ];
84
66
@@ -102,11 +84,13 @@ bool AvInputAudio::readNextFrame( Frame& frameBuffer )
102
84
{
103
85
if ( ! getNextFrame () )
104
86
return false ;
105
-
87
+
88
+ AVCodecContext* avCodecContext = _codec.getAVCodecContext ();
89
+
106
90
size_t decodedSize = av_samples_get_buffer_size (
107
- NULL , _codecContext ->channels ,
91
+ NULL , avCodecContext ->channels ,
108
92
_frame->nb_samples ,
109
- _codecContext ->sample_fmt , 1 );
93
+ avCodecContext ->sample_fmt , 1 );
110
94
111
95
AudioFrame& audioBuffer = static_cast <AudioFrame&>( frameBuffer );
112
96
@@ -120,8 +104,8 @@ bool AvInputAudio::readNextFrame( Frame& frameBuffer )
120
104
unsigned char * dst = audioBuffer.getPtr ();
121
105
av_samples_copy (
122
106
&dst, (uint8_t * const * )_frame->data , 0 ,
123
- 0 , _frame->nb_samples , _codecContext ->channels ,
124
- _codecContext ->sample_fmt );
107
+ 0 , _frame->nb_samples , avCodecContext ->channels ,
108
+ avCodecContext ->sample_fmt );
125
109
}
126
110
127
111
return true ;
@@ -134,9 +118,9 @@ bool AvInputAudio::readNextFrame( Frame& frameBuffer, const size_t subStreamInde
134
118
135
119
const int output_nbChannels = 1 ;
136
120
const int output_align = 1 ;
137
- size_t decodedSize = av_samples_get_buffer_size (NULL , output_nbChannels, _frame->nb_samples , _codecContext ->sample_fmt , output_align);
121
+ size_t decodedSize = av_samples_get_buffer_size (NULL , output_nbChannels, _frame->nb_samples , _codec. getAVCodecContext () ->sample_fmt , output_align);
138
122
139
- size_t nbSubStreams = _codecContext ->channels ;
123
+ size_t nbSubStreams = _codec. getAVCodecContext () ->channels ;
140
124
size_t bytePerSample = av_get_bytes_per_sample ( (AVSampleFormat)_frame->format );
141
125
142
126
if ( subStreamIndex > nbSubStreams - 1 )
@@ -188,7 +172,7 @@ bool AvInputAudio::getNextFrame()
188
172
packet.data = data.getPtr ();
189
173
packet.size = data.getSize ();
190
174
191
- int ret = avcodec_decode_audio4 ( _codecContext , _frame, &got_frame, &packet );
175
+ int ret = avcodec_decode_audio4 ( _codec. getAVCodecContext () , _frame, &got_frame, &packet );
192
176
193
177
if ( ret < 0 )
194
178
{
0 commit comments