Skip to content

Commit 249bcfb

Browse files
author
Clement Champetier
committed
AudioFrame: refactor how to get a default number of samples
* Add a private method. * Add doc about this tricky case.
1 parent d9b2f41 commit 249bcfb

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/AvTranscoder/data/decoded/AudioFrame.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ AudioFrame::AudioFrame(const AudioFrameDesc& desc, const bool forceDataAllocatio
4949
av_frame_set_channels(_frame, desc._nbChannels);
5050
av_frame_set_channel_layout(_frame, av_get_default_channel_layout(desc._nbChannels));
5151
_frame->format = desc._sampleFormat;
52-
_frame->nb_samples = desc._sampleRate / 25.; // cannot be known before calling avcodec_decode_audio4
52+
_frame->nb_samples = getDefaultNbSamples();
5353

5454
if(forceDataAllocation)
5555
allocateData();
@@ -103,7 +103,7 @@ void AudioFrame::allocateData()
103103
av_frame_set_channels(_frame, _desc._nbChannels);
104104
av_frame_set_channel_layout(_frame, av_get_default_channel_layout(_desc._nbChannels));
105105
_frame->format = _desc._sampleFormat;
106-
_frame->nb_samples = _desc._sampleRate / 25.; // cannot be known before calling avcodec_decode_audio4
106+
_frame->nb_samples = getDefaultNbSamples();
107107

108108
// Allocate data
109109
const int align = 0;
@@ -143,4 +143,10 @@ void AudioFrame::assignBuffer(const unsigned char* ptrValue)
143143
throw std::runtime_error(os.str());
144144
}
145145
}
146+
147+
size_t AudioFrame::getDefaultNbSamples() const
148+
{
149+
return _desc._sampleRate / 25.;
150+
}
151+
146152
}

src/AvTranscoder/data/decoded/AudioFrame.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ class AvExport AudioFrame : public IFrame
6767

6868
void assignBuffer(const unsigned char* ptrValue);
6969

70+
private:
71+
/**
72+
* @brief The number of samples of a frame cannot be known before calling avcodec_decode_audio4,
73+
* and can be variable in a same stream. Because we need to allocate some frames without knowing this parameter,
74+
* we access here a default number of samples.
75+
* @note This value depends on the sample rate (excample: 1920 samples at 48kHz).
76+
* @return the number of samples of our default AudioFrame.
77+
* @see setNbSamplesPerChannel
78+
*/
79+
size_t getDefaultNbSamples() const;
80+
7081
private:
7182
/**
7283
* @brief Description of the frame to allocate.

0 commit comments

Comments
 (0)