Skip to content

Commit 3c71de0

Browse files
committed
Merge pull request #119 from cchampet/fix_audioTransformWhenNbSamplesChange
Fix AudioTransform: when nbSamples change between frames (MOV)
2 parents 17d4b13 + 4dea946 commit 3c71de0

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/AvTranscoder/transform/AudioTransform.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace avtranscoder
3232

3333
AudioTransform::AudioTransform()
3434
: _audioConvertContext( NULL )
35-
, _previousProcessedAudioFrameSize( 0 )
35+
, _nbSamplesOfPreviousFrame( 0 )
3636
, _isInit ( false )
3737
{
3838
}
@@ -69,39 +69,40 @@ bool AudioTransform::init( const Frame& srcFrame, const Frame& dstFrame )
6969
return true;
7070
}
7171

72-
void AudioTransform::initFrames( const Frame& srcFrame, Frame& dstFrame )
72+
void AudioTransform::updateOutputFrame( const size_t nbInputSamples, Frame& dstFrame ) const
7373
{
74-
const AudioFrame& src = static_cast<const AudioFrame&>( srcFrame );
7574
AudioFrame& dst = static_cast<AudioFrame&>( dstFrame );
7675

7776
// resize buffer of output frame
7877
const int dstSampleSize = av_get_bytes_per_sample( dst.desc().getSampleFormat() );
79-
const size_t bufferSizeNeeded = src.getNbSamples() * dst.desc().getChannels() * dstSampleSize;
80-
if( bufferSizeNeeded > dstFrame.getSize() )
81-
dstFrame.resize( bufferSizeNeeded );
78+
const size_t bufferSizeNeeded = nbInputSamples * dst.desc().getChannels() * dstSampleSize;
79+
dstFrame.resize( bufferSizeNeeded );
8280

8381
// set nbSamples of output frame
84-
dst.setNbSamples( src.getNbSamples() );
85-
86-
_previousProcessedAudioFrameSize = srcFrame.getSize();
82+
dst.setNbSamples( nbInputSamples );
8783
}
8884

8985
void AudioTransform::convert( const Frame& srcFrame, Frame& dstFrame )
9086
{
9187
if( ! _isInit )
9288
_isInit = init( srcFrame, dstFrame );
9389

94-
if( srcFrame.getSize() != _previousProcessedAudioFrameSize )
95-
initFrames( srcFrame, dstFrame );
90+
// if number of samples change from previous frame
91+
const size_t nbSamplesOfCurrentFrame = static_cast<const AudioFrame&>( srcFrame ).getNbSamples();
92+
if( nbSamplesOfCurrentFrame != _nbSamplesOfPreviousFrame )
93+
{
94+
updateOutputFrame( nbSamplesOfCurrentFrame, dstFrame );
95+
_nbSamplesOfPreviousFrame = nbSamplesOfCurrentFrame;
96+
}
9697

9798
const unsigned char* srcData = srcFrame.getData();
9899
unsigned char* dstData = dstFrame.getData();
99100

100101
int nbOutputSamplesPerChannel;
101102
#ifdef AV_RESAMPLE_LIBRARY
102-
nbOutputSamplesPerChannel = avresample_convert( _audioConvertContext, (uint8_t**)&dstData, 0, dstFrame.getSize(), (uint8_t**)&srcData, 0, srcFrame.getSize() );
103+
nbOutputSamplesPerChannel = avresample_convert( _audioConvertContext, (uint8_t**)&dstData, 0, nbSamplesOfCurrentFrame, (uint8_t**)&srcData, 0, nbSamplesOfCurrentFrame );
103104
#else
104-
nbOutputSamplesPerChannel = swr_convert( _audioConvertContext, &dstData, dstFrame.getSize(), &srcData, srcFrame.getSize() );
105+
nbOutputSamplesPerChannel = swr_convert( _audioConvertContext, &dstData, nbSamplesOfCurrentFrame, &srcData, nbSamplesOfCurrentFrame );
105106
#endif
106107

107108
if( nbOutputSamplesPerChannel < 0 )

src/AvTranscoder/transform/AudioTransform.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ class AvExport AudioTransform : public ITransform
2828
private:
2929
bool init( const Frame& srcFrame, const Frame& dstFrame );
3030

31-
/// Resize output buffer if source has a different size from the last process
32-
void initFrames( const Frame& srcFrame, Frame& dstFrame );
33-
31+
/// Update output buffer if source has a different size from the last process
32+
void updateOutputFrame( const size_t nbInputSamples, Frame& dstFrame ) const;
33+
34+
private:
3435
ResampleContext* _audioConvertContext;
35-
36-
size_t _previousProcessedAudioFrameSize;
36+
37+
size_t _nbSamplesOfPreviousFrame; ///< To check if the number of samples change between frames
3738

3839
bool _isInit;
3940
};

0 commit comments

Comments
 (0)