Skip to content

readers: use InputStreamDesc class #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/avPlayer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int main(int argc, char** argv)
avtranscoder::preloadCodecsAndFormats();
avtranscoder::Logger::setLogLevel(AV_LOG_QUIET);

avtranscoder::VideoReader reader(filename, streamIndex);
avtranscoder::VideoReader reader(avtranscoder::InputStreamDesc(filename, streamIndex));
if(width == 0)
width = reader.getOutputWidth();
if(height == 0)
Expand Down
26 changes: 9 additions & 17 deletions src/AvTranscoder/reader/AudioReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,8 @@
namespace avtranscoder
{

AudioReader::AudioReader(const std::string& filename, const size_t streamIndex, const int channelIndex)
: IReader(filename, streamIndex, channelIndex)
, _audioStreamProperties(NULL)
, _outputSampleRate(0)
, _outputNbChannels(0)
, _outputSampleFormat(AV_SAMPLE_FMT_S16)
{
init();
}

AudioReader::AudioReader(InputFile& inputFile, const size_t streamIndex, const int channelIndex)
: IReader(inputFile, streamIndex, channelIndex)
AudioReader::AudioReader(const InputStreamDesc& inputDesc)
: IReader(inputDesc)
, _audioStreamProperties(NULL)
, _outputSampleRate(0)
, _outputNbChannels(0)
Expand All @@ -35,22 +25,24 @@ void AudioReader::init()
// analyse InputFile
avtranscoder::NoDisplayProgress p;
_inputFile->analyse(p);
_streamProperties = &_inputFile->getProperties().getStreamPropertiesWithIndex(_streamIndex);
_streamProperties = &_inputFile->getProperties().getStreamPropertiesWithIndex(_inputDesc._streamIndex);
_audioStreamProperties = static_cast<const AudioProperties*>(_streamProperties);
_inputFile->activateStream(_streamIndex);
_inputFile->activateStream(_inputDesc._streamIndex);

// setup decoder
_decoder = new AudioDecoder(_inputFile->getStream(_streamIndex));
_decoder = new AudioDecoder(_inputFile->getStream(_inputDesc._streamIndex));
_decoder->setupDecoder();
_currentDecoder = _decoder;

// create src frame
const AudioFrameDesc srcFrameDesc = _inputFile->getStream(_streamIndex).getAudioCodec().getAudioFrameDesc();
AudioFrameDesc srcFrameDesc = _inputFile->getStream(_inputDesc._streamIndex).getAudioCodec().getAudioFrameDesc();
if(! _inputDesc._channelIndexArray.empty())
srcFrameDesc._nbChannels = _inputDesc._channelIndexArray.size();
_srcFrame = new AudioFrame(srcFrameDesc, false);
AudioFrame* srcFrame = static_cast<AudioFrame*>(_srcFrame);
// create dst frame
_outputSampleRate = srcFrame->getSampleRate();
_outputNbChannels = (_channelIndex == -1) ? srcFrame->getNbChannels() : 1;
_outputNbChannels = srcFrame->getNbChannels();
_dstFrame = new AudioFrame(AudioFrameDesc(_outputSampleRate, _outputNbChannels, getSampleFormatName(_outputSampleFormat)));

// generator
Expand Down
3 changes: 1 addition & 2 deletions src/AvTranscoder/reader/AudioReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class AvExport AudioReader : public IReader
//@{
// @note Transform the input stream to s16 sample format (to listen).
// @see updateOutput
AudioReader(const std::string& filename, const size_t streamIndex = 0, const int channelIndex = -1);
AudioReader(InputFile& inputFile, const size_t streamIndex = 0, const int channelIndex = -1);
AudioReader(const InputStreamDesc& inputDesc);
//@}

~AudioReader();
Expand Down
36 changes: 7 additions & 29 deletions src/AvTranscoder/reader/IReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,25 @@
namespace avtranscoder
{

IReader::IReader(const std::string& filename, const size_t streamIndex, const int channelIndex)
: _inputFile(NULL)
IReader::IReader(const InputStreamDesc& inputDesc)
: _inputDesc(inputDesc)
, _inputFile(NULL)
, _streamProperties(NULL)
, _decoder(NULL)
, _generator(NULL)
, _currentDecoder(NULL)
, _srcFrame(NULL)
, _dstFrame(NULL)
, _transform(NULL)
, _streamIndex(streamIndex)
, _channelIndex(channelIndex)
, _currentFrame(-1)
, _inputFileAllocated(true)
, _continueWithGenerator(false)
{
_inputFile = new InputFile(filename);
}

IReader::IReader(InputFile& inputFile, const size_t streamIndex, const int channelIndex)
: _inputFile(&inputFile)
, _streamProperties(NULL)
, _decoder(NULL)
, _generator(NULL)
, _currentDecoder(NULL)
, _srcFrame(NULL)
, _dstFrame(NULL)
, _transform(NULL)
, _streamIndex(streamIndex)
, _channelIndex(channelIndex)
, _currentFrame(-1)
, _inputFileAllocated(false)
, _continueWithGenerator(false)
{
_inputFile = new InputFile(_inputDesc._filename);
}

IReader::~IReader()
{
if(_inputFileAllocated)
delete _inputFile;
delete _inputFile;
}

IFrame* IReader::readNextFrame()
Expand Down Expand Up @@ -72,11 +52,9 @@ IFrame* IReader::readFrameAt(const size_t frame)
_currentFrame = frame;
// decode
bool decodingStatus = false;
if(_channelIndex != -1)
if(! _inputDesc._channelIndexArray.empty())
{
std::vector<size_t> channelIndexArray;
channelIndexArray.push_back(_channelIndex);
decodingStatus = _currentDecoder->decodeNextFrame(*_srcFrame, channelIndexArray);
decodingStatus = _currentDecoder->decodeNextFrame(*_srcFrame, _inputDesc._channelIndexArray);
}
else
decodingStatus = _currentDecoder->decodeNextFrame(*_srcFrame);
Expand Down
19 changes: 5 additions & 14 deletions src/AvTranscoder/reader/IReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <AvTranscoder/common.hpp>

#include <AvTranscoder/transcoder/InputStreamDesc.hpp>
#include <AvTranscoder/file/InputFile.hpp>
#include <AvTranscoder/properties/StreamProperties.hpp>
#include <AvTranscoder/decoder/IDecoder.hpp>
Expand All @@ -19,17 +20,10 @@ class AvExport IReader
{
public:
/**
* @brief Create a new InputFile and prepare to read the stream at the given index
* @param streamIndex by default read the first stream
* @param channelIndex by default -1 (all channels of the stream)
* @brief Prepare to read the given input.
* @param inputDesc: the description of the input to read.
*/
IReader(const std::string& filename, const size_t streamIndex = 0, const int channelIndex = -1);

/**
* @brief Get the existing InputFile and prepare to read the stream at the given index
* @note This constructor can improve performances when you create several readers from one InputFile.
*/
IReader(InputFile& inputFile, const size_t streamIndex = 0, const int channelIndex = -1);
IReader(const InputStreamDesc& inputDesc);

virtual ~IReader() = 0;

Expand Down Expand Up @@ -64,6 +58,7 @@ class AvExport IReader
void continueWithGenerator(const bool continueWithGenerator = true) { _continueWithGenerator = continueWithGenerator; }

protected:
const InputStreamDesc _inputDesc;
InputFile* _inputFile;
const StreamProperties* _streamProperties;
IDecoder* _decoder;
Expand All @@ -75,12 +70,8 @@ class AvExport IReader

ITransform* _transform;

size_t _streamIndex;
int _channelIndex;

private:
int _currentFrame; ///< The current decoded frame.
bool _inputFileAllocated; ///< Does the InputFile is held by the class or not (depends on the constructor called)
bool _continueWithGenerator; ///< If there is no more data to decode, complete with generated data
};
}
Expand Down
22 changes: 6 additions & 16 deletions src/AvTranscoder/reader/VideoReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,8 @@
namespace avtranscoder
{

VideoReader::VideoReader(const std::string& filename, const size_t videoStreamIndex)
: IReader(filename, videoStreamIndex)
, _videoStreamProperties(NULL)
, _outputWidth(0)
, _outputHeight(0)
, _outputPixelProperties("rgb24")
{
init();
}

VideoReader::VideoReader(InputFile& inputFile, const size_t videoStreamIndex)
: IReader(inputFile, videoStreamIndex)
VideoReader::VideoReader(const InputStreamDesc& inputDesc)
: IReader(inputDesc)
, _videoStreamProperties(NULL)
, _outputWidth(0)
, _outputHeight(0)
Expand All @@ -35,17 +25,17 @@ void VideoReader::init()
// analyse InputFile
avtranscoder::NoDisplayProgress p;
_inputFile->analyse(p);
_streamProperties = &_inputFile->getProperties().getStreamPropertiesWithIndex(_streamIndex);
_streamProperties = &_inputFile->getProperties().getStreamPropertiesWithIndex(_inputDesc._streamIndex);
_videoStreamProperties = static_cast<const VideoProperties*>(_streamProperties);
_inputFile->activateStream(_streamIndex);
_inputFile->activateStream(_inputDesc._streamIndex);

// setup decoder
_decoder = new VideoDecoder(_inputFile->getStream(_streamIndex));
_decoder = new VideoDecoder(_inputFile->getStream(_inputDesc._streamIndex));
_decoder->setupDecoder();
_currentDecoder = _decoder;

// create src frame
const VideoFrameDesc srcFrameDesc = _inputFile->getStream(_streamIndex).getVideoCodec().getVideoFrameDesc();
const VideoFrameDesc srcFrameDesc = _inputFile->getStream(_inputDesc._streamIndex).getVideoCodec().getVideoFrameDesc();
_srcFrame = new VideoFrame(srcFrameDesc, false);
VideoFrame* srcFrame = static_cast<VideoFrame*>(_srcFrame);
// create dst frame
Expand Down
3 changes: 1 addition & 2 deletions src/AvTranscoder/reader/VideoReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class AvExport VideoReader : public IReader
//@{
// @note Transform the input stream to rgb24 pixel format (to display).
// @see updateOutput
VideoReader(const std::string& filename, const size_t videoStreamIndex = 0);
VideoReader(InputFile& inputFile, const size_t videoStreamIndex = 0);
VideoReader(const InputStreamDesc& inputDesc);
//@}

~VideoReader();
Expand Down
13 changes: 13 additions & 0 deletions src/AvTranscoder/transcoder/InputStreamDesc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,26 @@ struct InputStreamDesc
_channelIndexArray.push_back(channelIndex);
}

/**
* @brief Read all the channels of the indicated stream.
*/
InputStreamDesc(const std::string& filename, const size_t streamIndex)
: _filename(filename)
, _streamIndex(streamIndex)
, _channelIndexArray()
{
}

/**
* @brief Read all the channels of the stream at index 0.
*/
InputStreamDesc(const std::string& filename)
: _filename(filename)
, _streamIndex(0)
, _channelIndexArray()
{
}

/**
* @return If a demultiplexing step will be done to extract the expected data.
*/
Expand Down
14 changes: 5 additions & 9 deletions test/pyTest/testAudioReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
from pyAvTranscoder import avtranscoder as av


def testAudioReaderCreateNewInputFile():
def testAudioReader():
"""
Read a audio stream with the AudioReader.
The InputFile is created inside the reader.
"""
inputFileName = os.environ['AVTRANSCODER_TEST_AUDIO_WAVE_FILE']
reader = av.AudioReader(inputFileName)
reader = av.AudioReader(av.InputStreamDesc(inputFileName))

# read all frames and check their size
while True:
Expand All @@ -41,32 +41,28 @@ def testAudioReaderChannelsExtraction():
channelIndex = 0

# create reader to read all channels of the audio stream
readerOfAllChannels = av.AudioReader(inputFile, streamIndex)
readerOfAllChannels = av.AudioReader(av.InputStreamDesc(inputFileName, streamIndex))
nbChannels = readerOfAllChannels.getOutputNbChannels()
# read first frame
frame = readerOfAllChannels.readNextFrame()
sizeOfFrameWithAllChannels = frame.getDataSize()

# create reader to read one channel of the audio stream
readerOfOneChannel = av.AudioReader(inputFile, streamIndex, channelIndex)
readerOfOneChannel = av.AudioReader(av.InputStreamDesc(inputFileName, streamIndex, channelIndex))
# read first frame
frame = readerOfOneChannel.readNextFrame()
sizeOfFrameWithOneChannels = frame.getDataSize()

assert_equals( sizeOfFrameWithAllChannels / nbChannels, sizeOfFrameWithOneChannels )

# Force to call the readers destructor before the inputFile destructor (which cannot happen in C++)
readerOfAllChannels = None
readerOfOneChannel = None


def testAudioReaderWithGenerator():
"""
Read an audio stream with the AudioReader.
When there is no more data to decode, switch to a generator and process some frames.
"""
inputFileName = os.environ['AVTRANSCODER_TEST_AUDIO_WAVE_FILE']
reader = av.AudioReader(inputFileName)
reader = av.AudioReader(av.InputStreamDesc(inputFileName))

# read all frames and check their size
while True:
Expand Down
4 changes: 2 additions & 2 deletions test/pyTest/testNbFrames.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def testNbFramesVideoRewrap():
ouputFile = av.OutputFile( outputFileName )
transcoder = av.Transcoder( ouputFile )

transcoder.addStream( av.InputStreamDesc(inputFileName, 0) )
transcoder.addStream( av.InputStreamDesc(inputFileName) )

progress = av.ConsoleProgress()
transcoder.process( progress )
Expand All @@ -47,7 +47,7 @@ def testNbFramesVideoTranscode():
ouputFile = av.OutputFile( outputFileName )
transcoder = av.Transcoder( ouputFile )

transcoder.addStream( av.InputStreamDesc(inputFileName, 0), "mpeg2" )
transcoder.addStream( av.InputStreamDesc(inputFileName), "mpeg2" )

progress = av.ConsoleProgress()
transcoder.process( progress )
Expand Down
4 changes: 2 additions & 2 deletions test/pyTest/testNbSamples.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def testNbSamplesAudioRewrap():
ouputFile = av.OutputFile( outputFileName )
transcoder = av.Transcoder( ouputFile )

transcoder.addStream( av.InputStreamDesc(inputFileName, 0) )
transcoder.addStream( av.InputStreamDesc(inputFileName) )

progress = av.ConsoleProgress()
transcoder.process( progress )
Expand Down Expand Up @@ -54,7 +54,7 @@ def testNbSamplesAudioTranscode():
customProfile[av.avProfileType] = av.avProfileTypeAudio
customProfile[av.avProfileCodec] = "pcm_s16le"

transcoder.addStream( av.InputStreamDesc(inputFileName, 0), customProfile )
transcoder.addStream( av.InputStreamDesc(inputFileName), customProfile )

progress = av.ConsoleProgress()
transcoder.process( progress )
Expand Down
Loading