Skip to content

Commit 8a338d6

Browse files
author
Clement Champetier
committed
Fix audio transcoding
* AudioTransform: dstFrame.resize takes number of channels. * AudioGenerator: need to specify nbSamples at each decodeNextFrame.
1 parent 969efbc commit 8a338d6

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

src/AvTranscoder/decoder/AudioGenerator.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ bool AudioGenerator::decodeNextFrame( Frame& frameBuffer )
2727
// Generate silent
2828
if( ! _inputFrame )
2929
{
30+
AudioFrame& audioBuffer = static_cast<AudioFrame&>( frameBuffer );
31+
audioBuffer.setNbSamples( _frameDesc.getSampleRate() / _frameDesc.getFps() );
32+
3033
// Generate the silent only once
3134
if( ! _silent )
3235
{
@@ -35,10 +38,9 @@ bool AudioGenerator::decodeNextFrame( Frame& frameBuffer )
3538
// input of convert
3639
AudioFrame intermediateBuffer( _frameDesc );
3740
intermediateBuffer.assign( _frameDesc.getDataSize(), fillChar );
41+
intermediateBuffer.setNbSamples( audioBuffer.getNbSamples() );
3842

3943
// output of convert
40-
AudioFrame& audioBuffer = static_cast<AudioFrame&>( frameBuffer );
41-
audioBuffer.setNbSamples( 1.0 * _frameDesc.getSampleRate() / _frameDesc.getFps() );
4244
_silent = new AudioFrame( audioBuffer.desc() );
4345

4446
// convert and store the silence

src/AvTranscoder/transform/AudioTransform.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,15 @@ AudioTransform::AudioTransform()
3939

4040
bool AudioTransform::init( const Frame& srcFrame, const Frame& dstFrame )
4141
{
42-
const AudioFrame& src = static_cast<const AudioFrame&>( srcFrame );
43-
const AudioFrame& dst = static_cast<const AudioFrame&>( dstFrame );
44-
4542
_audioConvertContext = AllocResampleContext();
46-
4743
if( !_audioConvertContext )
4844
{
4945
throw std::runtime_error( "unable to create audio convert context" );
5046
}
51-
47+
48+
const AudioFrame& src = static_cast<const AudioFrame&>( srcFrame );
49+
const AudioFrame& dst = static_cast<const AudioFrame&>( dstFrame );
50+
5251
av_opt_set_int( _audioConvertContext, "in_channel_layout", av_get_default_channel_layout( src.desc().getChannels() ), 0 );
5352
av_opt_set_int( _audioConvertContext, "out_channel_layout", av_get_default_channel_layout( dst.desc().getChannels() ), 0 );
5453
av_opt_set_int( _audioConvertContext, "in_sample_rate", src.desc().getSampleRate(), 0 );
@@ -65,15 +64,19 @@ bool AudioTransform::init( const Frame& srcFrame, const Frame& dstFrame )
6564
return true;
6665
}
6766

68-
bool AudioTransform::initFrames( const Frame& srcFrame, Frame& dstFrame )
67+
void AudioTransform::initFrames( const Frame& srcFrame, Frame& dstFrame )
6968
{
7069
const AudioFrame& src = static_cast<const AudioFrame&>( srcFrame );
71-
const AudioFrame& dst = static_cast<const AudioFrame&>( dstFrame );
70+
AudioFrame& dst = static_cast<AudioFrame&>( dstFrame );
7271

72+
// resize buffer of output frame
7373
int dstSampleSize = av_get_bytes_per_sample( dst.desc().getSampleFormat() );
74-
dstFrame.resize( src.getNbSamples() * dstSampleSize );
74+
dstFrame.resize( src.getNbSamples() * src.desc().getChannels() * dstSampleSize );
75+
76+
// set nbSamples of output frame
77+
dst.setNbSamples( src.getNbSamples() );
78+
7579
_previousProcessedAudioFrameSize = srcFrame.getSize();
76-
return true;
7780
}
7881

7982
void AudioTransform::convert( const Frame& srcFrame, Frame& dstFrame )
@@ -98,12 +101,6 @@ void AudioTransform::convert( const Frame& srcFrame, Frame& dstFrame )
98101
{
99102
throw std::runtime_error( "unable to convert audio samples" );
100103
}
101-
102-
size_t nbOutputSamples = nbOutputSamplesPerChannel * static_cast<const AudioFrame&>( dstFrame ).desc().getChannels();
103-
104-
if( dstFrame.getSize() != nbOutputSamples )
105-
dstFrame.resize( nbOutputSamples );
106-
static_cast<AudioFrame&>( dstFrame ).setNbSamples( static_cast<const AudioFrame&>( srcFrame ).getNbSamples() );
107104
}
108105

109106
}

src/AvTranscoder/transform/AudioTransform.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class AvExport AudioTransform : public ITransform
2626

2727
private:
2828
bool init( const Frame& srcFrame, const Frame& dstFrame );
29-
bool initFrames( const Frame& srcFrame, Frame& dstFrame );
29+
30+
/// Resize output buffer if source has a different size from the last process
31+
void initFrames( const Frame& srcFrame, Frame& dstFrame );
3032

3133
ResampleContext* _audioConvertContext;
3234

0 commit comments

Comments
 (0)