@@ -32,7 +32,7 @@ namespace avtranscoder
32
32
33
33
AudioTransform::AudioTransform ()
34
34
: _audioConvertContext( NULL )
35
- , _previousProcessedAudioFrameSize ( 0 )
35
+ , _nbSamplesOfPreviousFrame ( 0 )
36
36
, _isInit ( false )
37
37
{
38
38
}
@@ -69,39 +69,40 @@ bool AudioTransform::init( const Frame& srcFrame, const Frame& dstFrame )
69
69
return true ;
70
70
}
71
71
72
- void AudioTransform::initFrames ( const Frame& srcFrame , Frame& dstFrame )
72
+ void AudioTransform::updateOutputFrame ( const size_t nbInputSamples , Frame& dstFrame ) const
73
73
{
74
- const AudioFrame& src = static_cast <const AudioFrame&>( srcFrame );
75
74
AudioFrame& dst = static_cast <AudioFrame&>( dstFrame );
76
75
77
76
// resize buffer of output frame
78
77
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 );
82
80
83
81
// set nbSamples of output frame
84
- dst.setNbSamples ( src.getNbSamples () );
85
-
86
- _previousProcessedAudioFrameSize = srcFrame.getSize ();
82
+ dst.setNbSamples ( nbInputSamples );
87
83
}
88
84
89
85
void AudioTransform::convert ( const Frame& srcFrame, Frame& dstFrame )
90
86
{
91
87
if ( ! _isInit )
92
88
_isInit = init ( srcFrame, dstFrame );
93
89
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
+ }
96
97
97
98
const unsigned char * srcData = srcFrame.getData ();
98
99
unsigned char * dstData = dstFrame.getData ();
99
100
100
101
int nbOutputSamplesPerChannel;
101
102
#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 );
103
104
#else
104
- nbOutputSamplesPerChannel = swr_convert ( _audioConvertContext, &dstData, dstFrame. getSize () , &srcData, srcFrame. getSize () );
105
+ nbOutputSamplesPerChannel = swr_convert ( _audioConvertContext, &dstData, nbSamplesOfCurrentFrame , &srcData, nbSamplesOfCurrentFrame );
105
106
#endif
106
107
107
108
if ( nbOutputSamplesPerChannel < 0 )
0 commit comments