forked from mikrosimage/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioDecoder.cpp
More file actions
180 lines (144 loc) · 4.66 KB
/
Copy pathAudioDecoder.cpp
File metadata and controls
180 lines (144 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "AudioDecoder.hpp"
#include <AvTranscoder/codec/ICodec.hpp>
#include <AvTranscoder/stream/InputStream.hpp>
#include <AvTranscoder/frame/AudioFrame.hpp>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/pixdesc.h>
#include <libavutil/channel_layout.h>
}
#include <stdexcept>
namespace avtranscoder
{
AudioDecoder::AudioDecoder( InputStream& inputStream )
: _inputStream ( &inputStream )
, _frame ( NULL )
{
}
AudioDecoder::~AudioDecoder()
{
if( _frame != NULL )
{
#if LIBAVCODEC_VERSION_MAJOR > 54
av_frame_free( &_frame );
#else
#if LIBAVCODEC_VERSION_MAJOR > 53
avcodec_free_frame( &_frame );
#else
av_free( _frame );
#endif
#endif
_frame = NULL;
}
}
void AudioDecoder::setup()
{
_inputStream->getAudioCodec().open();
#if LIBAVCODEC_VERSION_MAJOR > 54
_frame = av_frame_alloc();
#else
_frame = avcodec_alloc_frame();
#endif
if( _frame == NULL )
{
throw std::runtime_error( "unable to setup frame buffer" );
}
}
bool AudioDecoder::decodeNextFrame( Frame& frameBuffer )
{
if( ! decodeNextFrame() )
return false;
AVCodecContext& avCodecContext = _inputStream->getAudioCodec().getAVCodecContext();
size_t decodedSize = av_samples_get_buffer_size( NULL, avCodecContext.channels, _frame->nb_samples, avCodecContext.sample_fmt, 1 );
if( decodedSize == 0 )
return false;
AudioFrame& audioBuffer = static_cast<AudioFrame&>( frameBuffer );
audioBuffer.setNbSamples( _frame->nb_samples );
audioBuffer.resize( decodedSize );
// @todo manage cases with data of frame not only on data[0] (use _frame.linesize)
unsigned char* const src = _frame->data[0];
unsigned char* dst = audioBuffer.getData();
av_samples_copy( &dst, &src, 0, 0, _frame->nb_samples, avCodecContext.channels, avCodecContext.sample_fmt );
return true;
}
bool AudioDecoder::decodeNextFrame( Frame& frameBuffer, const size_t subStreamIndex )
{
if( ! decodeNextFrame() )
return false;
AVCodecContext& avCodecContext = _inputStream->getAudioCodec().getAVCodecContext();
const int output_nbChannels = 1;
const int output_align = 1;
size_t decodedSize = av_samples_get_buffer_size(NULL, output_nbChannels, _frame->nb_samples, avCodecContext.sample_fmt, output_align);
size_t nbSubStreams = avCodecContext.channels;
size_t bytePerSample = av_get_bytes_per_sample( (AVSampleFormat)_frame->format );
if( subStreamIndex > nbSubStreams - 1 )
{
throw std::runtime_error( "The subStream doesn't exist");
}
if( decodedSize == 0 )
return false;
AudioFrame& audioBuffer = static_cast<AudioFrame&>( frameBuffer );
audioBuffer.setNbSamples( _frame->nb_samples );
audioBuffer.resize( decodedSize );
// @todo manage cases with data of frame not only on data[0] (use _frame.linesize)
unsigned char* src = _frame->data[0];
unsigned char* dst = audioBuffer.getData();
// offset
src += subStreamIndex * bytePerSample;
for( int sample = 0; sample < _frame->nb_samples; ++sample )
{
memcpy( dst, src, bytePerSample );
dst += bytePerSample;
src += bytePerSample * nbSubStreams;
}
return true;
}
bool AudioDecoder::decodeNextFrame()
{
int got_frame = 0;
while( ! got_frame )
{
CodedData data;
bool nextPacketRead = _inputStream->readNextPacket( data );
if( ! nextPacketRead ) // error or end of file
data.clear();
int ret = avcodec_decode_audio4( &_inputStream->getAudioCodec().getAVCodecContext(), _frame, &got_frame, &data.getAVPacket() );
if( ! nextPacketRead && ret == 0 && got_frame == 0 ) // no frame could be decompressed
return false;
if( ret < 0 )
{
throw std::runtime_error( "an error occured during audio decoding" + getDescriptionFromErrorCode( ret ) );
}
}
return true;
}
void AudioDecoder::setProfile( const ProfileLoader::Profile& profile )
{
AudioCodec& codec = _inputStream->getAudioCodec();
// set threads before any other options
if( profile.count( constants::avProfileThreads ) )
codec.getOption( constants::avProfileThreads ).setString( profile.at( constants::avProfileThreads ) );
else
codec.getOption( constants::avProfileThreads ).setString( "auto" );
// set decoder options
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
{
if( (*it).first == constants::avProfileIdentificator ||
(*it).first == constants::avProfileIdentificatorHuman ||
(*it).first == constants::avProfileType ||
(*it).first == constants::avProfileThreads )
continue;
try
{
Option& decodeOption = codec.getOption( (*it).first );
decodeOption.setString( (*it).second );
}
catch( std::exception& e )
{
LOG_WARN( "AudioDecoder - can't set option " << (*it).first << " to " << (*it).second << ": " << e.what() )
}
}
}
}