Skip to content

Commit 4425760

Browse files
committed
AvInputAudio: use AudioCodec
* Symmetry with AvOutputAudio: * use an AudioCodec. * no need to allocate and free AV codec objects ; it is done by our ICodec class.
1 parent 7ad1cc9 commit 4425760

File tree

2 files changed

+21
-39
lines changed

2 files changed

+21
-39
lines changed

src/AvTranscoder/essenceStream/AvInputAudio.cpp

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,14 @@ namespace avtranscoder
2020
AvInputAudio::AvInputAudio( AvInputStream& inputStream )
2121
: IInputEssence()
2222
, _inputStream ( &inputStream )
23-
, _codec ( NULL )
24-
, _codecContext ( NULL )
23+
, _codec( eCodecTypeDecoder, inputStream.getAudioCodec().getCodecId() )
2524
, _frame ( NULL )
2625
, _selectedStream( -1 )
2726
{
2827
}
2928

3029
AvInputAudio::~AvInputAudio()
3130
{
32-
if( _codecContext != NULL )
33-
{
34-
avcodec_close( _codecContext );
35-
av_free( _codecContext );
36-
_codecContext = NULL;
37-
}
3831
if( _frame != NULL )
3932
{
4033
#if LIBAVCODEC_VERSION_MAJOR > 54
@@ -53,32 +46,21 @@ AvInputAudio::~AvInputAudio()
5346

5447
void AvInputAudio::setup()
5548
{
56-
avcodec_register_all();
49+
AVCodecContext* avCodecContext = _codec.getAVCodecContext();
50+
AVCodec* avCodec = _codec.getAVCodec();
5751

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();
6353

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 );
7355

74-
if( ret < 0 || _codecContext == NULL || _codec == NULL )
56+
if( ret < 0 || avCodecContext == NULL || avCodec == NULL )
7557
{
7658
std::string msg = "unable open audio codec: ";
77-
msg += _codec->long_name;
59+
msg += avCodec->long_name;
7860
msg += " (";
79-
msg += _codec->name;
61+
msg += avCodec->name;
8062
msg += ")";
81-
avcodec_close( _codecContext );
63+
avcodec_close( avCodecContext );
8264

8365
char err[250];
8466

@@ -102,11 +84,13 @@ bool AvInputAudio::readNextFrame( Frame& frameBuffer )
10284
{
10385
if( ! getNextFrame() )
10486
return false;
105-
87+
88+
AVCodecContext* avCodecContext = _codec.getAVCodecContext();
89+
10690
size_t decodedSize = av_samples_get_buffer_size(
107-
NULL, _codecContext->channels,
91+
NULL, avCodecContext->channels,
10892
_frame->nb_samples,
109-
_codecContext->sample_fmt, 1 );
93+
avCodecContext->sample_fmt, 1 );
11094

11195
AudioFrame& audioBuffer = static_cast<AudioFrame&>( frameBuffer );
11296

@@ -120,8 +104,8 @@ bool AvInputAudio::readNextFrame( Frame& frameBuffer )
120104
unsigned char* dst = audioBuffer.getPtr();
121105
av_samples_copy(
122106
&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 );
125109
}
126110

127111
return true;
@@ -134,9 +118,9 @@ bool AvInputAudio::readNextFrame( Frame& frameBuffer, const size_t subStreamInde
134118

135119
const int output_nbChannels = 1;
136120
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);
138122

139-
size_t nbSubStreams = _codecContext->channels;
123+
size_t nbSubStreams = _codec.getAVCodecContext()->channels;
140124
size_t bytePerSample = av_get_bytes_per_sample( (AVSampleFormat)_frame->format );
141125

142126
if( subStreamIndex > nbSubStreams - 1 )
@@ -188,7 +172,7 @@ bool AvInputAudio::getNextFrame()
188172
packet.data = data.getPtr();
189173
packet.size = data.getSize();
190174

191-
int ret = avcodec_decode_audio4( _codecContext, _frame, &got_frame, &packet );
175+
int ret = avcodec_decode_audio4( _codec.getAVCodecContext(), _frame, &got_frame, &packet );
192176

193177
if( ret < 0 )
194178
{

src/AvTranscoder/essenceStream/AvInputAudio.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
#define _AV_TRANSCODER_ESSENCE_STREAM_AV_INPUT_AUDIO_HPP_
33

44
#include "IInputEssence.hpp"
5+
#include <AvTranscoder/codec/AudioCodec.hpp>
56

6-
struct AVCodec;
7-
struct AVCodecContext;
87
struct AVFrame;
98

109
namespace avtranscoder
@@ -27,8 +26,7 @@ class AvExport AvInputAudio : public IInputEssence
2726
bool getNextFrame();
2827

2928
AvInputStream* _inputStream;
30-
AVCodec* _codec;
31-
AVCodecContext* _codecContext;
29+
AudioCodec _codec;
3230
AVFrame* _frame;
3331

3432
int _selectedStream;

0 commit comments

Comments
 (0)