Skip to content

Commit 6889182

Browse files
authored
Merge pull request #298 from cchampet/dev_readerUseInputStreamDesc
readers: use InputStreamDesc class
2 parents dd8ae36 + 2ee8330 commit 6889182

16 files changed

+72
-136
lines changed

app/avPlayer/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ int main(int argc, char** argv)
8181
avtranscoder::preloadCodecsAndFormats();
8282
avtranscoder::Logger::setLogLevel(AV_LOG_QUIET);
8383

84-
avtranscoder::VideoReader reader(filename, streamIndex);
84+
avtranscoder::VideoReader reader(avtranscoder::InputStreamDesc(filename, streamIndex));
8585
if(width == 0)
8686
width = reader.getOutputWidth();
8787
if(height == 0)

src/AvTranscoder/reader/AudioReader.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,8 @@
1010
namespace avtranscoder
1111
{
1212

13-
AudioReader::AudioReader(const std::string& filename, const size_t streamIndex, const int channelIndex)
14-
: IReader(filename, streamIndex, channelIndex)
15-
, _audioStreamProperties(NULL)
16-
, _outputSampleRate(0)
17-
, _outputNbChannels(0)
18-
, _outputSampleFormat(AV_SAMPLE_FMT_S16)
19-
{
20-
init();
21-
}
22-
23-
AudioReader::AudioReader(InputFile& inputFile, const size_t streamIndex, const int channelIndex)
24-
: IReader(inputFile, streamIndex, channelIndex)
13+
AudioReader::AudioReader(const InputStreamDesc& inputDesc)
14+
: IReader(inputDesc)
2515
, _audioStreamProperties(NULL)
2616
, _outputSampleRate(0)
2717
, _outputNbChannels(0)
@@ -35,22 +25,24 @@ void AudioReader::init()
3525
// analyse InputFile
3626
avtranscoder::NoDisplayProgress p;
3727
_inputFile->analyse(p);
38-
_streamProperties = &_inputFile->getProperties().getStreamPropertiesWithIndex(_streamIndex);
28+
_streamProperties = &_inputFile->getProperties().getStreamPropertiesWithIndex(_inputDesc._streamIndex);
3929
_audioStreamProperties = static_cast<const AudioProperties*>(_streamProperties);
40-
_inputFile->activateStream(_streamIndex);
30+
_inputFile->activateStream(_inputDesc._streamIndex);
4131

4232
// setup decoder
43-
_decoder = new AudioDecoder(_inputFile->getStream(_streamIndex));
33+
_decoder = new AudioDecoder(_inputFile->getStream(_inputDesc._streamIndex));
4434
_decoder->setupDecoder();
4535
_currentDecoder = _decoder;
4636

4737
// create src frame
48-
const AudioFrameDesc srcFrameDesc = _inputFile->getStream(_streamIndex).getAudioCodec().getAudioFrameDesc();
38+
AudioFrameDesc srcFrameDesc = _inputFile->getStream(_inputDesc._streamIndex).getAudioCodec().getAudioFrameDesc();
39+
if(! _inputDesc._channelIndexArray.empty())
40+
srcFrameDesc._nbChannels = _inputDesc._channelIndexArray.size();
4941
_srcFrame = new AudioFrame(srcFrameDesc, false);
5042
AudioFrame* srcFrame = static_cast<AudioFrame*>(_srcFrame);
5143
// create dst frame
5244
_outputSampleRate = srcFrame->getSampleRate();
53-
_outputNbChannels = (_channelIndex == -1) ? srcFrame->getNbChannels() : 1;
45+
_outputNbChannels = srcFrame->getNbChannels();
5446
_dstFrame = new AudioFrame(AudioFrameDesc(_outputSampleRate, _outputNbChannels, getSampleFormatName(_outputSampleFormat)));
5547

5648
// generator

src/AvTranscoder/reader/AudioReader.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class AvExport AudioReader : public IReader
1515
//@{
1616
// @note Transform the input stream to s16 sample format (to listen).
1717
// @see updateOutput
18-
AudioReader(const std::string& filename, const size_t streamIndex = 0, const int channelIndex = -1);
19-
AudioReader(InputFile& inputFile, const size_t streamIndex = 0, const int channelIndex = -1);
18+
AudioReader(const InputStreamDesc& inputDesc);
2019
//@}
2120

2221
~AudioReader();

src/AvTranscoder/reader/IReader.cpp

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,25 @@
55
namespace avtranscoder
66
{
77

8-
IReader::IReader(const std::string& filename, const size_t streamIndex, const int channelIndex)
9-
: _inputFile(NULL)
8+
IReader::IReader(const InputStreamDesc& inputDesc)
9+
: _inputDesc(inputDesc)
10+
, _inputFile(NULL)
1011
, _streamProperties(NULL)
1112
, _decoder(NULL)
1213
, _generator(NULL)
1314
, _currentDecoder(NULL)
1415
, _srcFrame(NULL)
1516
, _dstFrame(NULL)
1617
, _transform(NULL)
17-
, _streamIndex(streamIndex)
18-
, _channelIndex(channelIndex)
1918
, _currentFrame(-1)
20-
, _inputFileAllocated(true)
21-
, _continueWithGenerator(false)
22-
{
23-
_inputFile = new InputFile(filename);
24-
}
25-
26-
IReader::IReader(InputFile& inputFile, const size_t streamIndex, const int channelIndex)
27-
: _inputFile(&inputFile)
28-
, _streamProperties(NULL)
29-
, _decoder(NULL)
30-
, _generator(NULL)
31-
, _currentDecoder(NULL)
32-
, _srcFrame(NULL)
33-
, _dstFrame(NULL)
34-
, _transform(NULL)
35-
, _streamIndex(streamIndex)
36-
, _channelIndex(channelIndex)
37-
, _currentFrame(-1)
38-
, _inputFileAllocated(false)
3919
, _continueWithGenerator(false)
4020
{
21+
_inputFile = new InputFile(_inputDesc._filename);
4122
}
4223

4324
IReader::~IReader()
4425
{
45-
if(_inputFileAllocated)
46-
delete _inputFile;
26+
delete _inputFile;
4727
}
4828

4929
IFrame* IReader::readNextFrame()
@@ -72,11 +52,9 @@ IFrame* IReader::readFrameAt(const size_t frame)
7252
_currentFrame = frame;
7353
// decode
7454
bool decodingStatus = false;
75-
if(_channelIndex != -1)
55+
if(! _inputDesc._channelIndexArray.empty())
7656
{
77-
std::vector<size_t> channelIndexArray;
78-
channelIndexArray.push_back(_channelIndex);
79-
decodingStatus = _currentDecoder->decodeNextFrame(*_srcFrame, channelIndexArray);
57+
decodingStatus = _currentDecoder->decodeNextFrame(*_srcFrame, _inputDesc._channelIndexArray);
8058
}
8159
else
8260
decodingStatus = _currentDecoder->decodeNextFrame(*_srcFrame);

src/AvTranscoder/reader/IReader.hpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <AvTranscoder/common.hpp>
55

6+
#include <AvTranscoder/transcoder/InputStreamDesc.hpp>
67
#include <AvTranscoder/file/InputFile.hpp>
78
#include <AvTranscoder/properties/StreamProperties.hpp>
89
#include <AvTranscoder/decoder/IDecoder.hpp>
@@ -19,17 +20,10 @@ class AvExport IReader
1920
{
2021
public:
2122
/**
22-
* @brief Create a new InputFile and prepare to read the stream at the given index
23-
* @param streamIndex by default read the first stream
24-
* @param channelIndex by default -1 (all channels of the stream)
23+
* @brief Prepare to read the given input.
24+
* @param inputDesc: the description of the input to read.
2525
*/
26-
IReader(const std::string& filename, const size_t streamIndex = 0, const int channelIndex = -1);
27-
28-
/**
29-
* @brief Get the existing InputFile and prepare to read the stream at the given index
30-
* @note This constructor can improve performances when you create several readers from one InputFile.
31-
*/
32-
IReader(InputFile& inputFile, const size_t streamIndex = 0, const int channelIndex = -1);
26+
IReader(const InputStreamDesc& inputDesc);
3327

3428
virtual ~IReader() = 0;
3529

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

6660
protected:
61+
const InputStreamDesc _inputDesc;
6762
InputFile* _inputFile;
6863
const StreamProperties* _streamProperties;
6964
IDecoder* _decoder;
@@ -75,12 +70,8 @@ class AvExport IReader
7570

7671
ITransform* _transform;
7772

78-
size_t _streamIndex;
79-
int _channelIndex;
80-
8173
private:
8274
int _currentFrame; ///< The current decoded frame.
83-
bool _inputFileAllocated; ///< Does the InputFile is held by the class or not (depends on the constructor called)
8475
bool _continueWithGenerator; ///< If there is no more data to decode, complete with generated data
8576
};
8677
}

src/AvTranscoder/reader/VideoReader.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,8 @@
1010
namespace avtranscoder
1111
{
1212

13-
VideoReader::VideoReader(const std::string& filename, const size_t videoStreamIndex)
14-
: IReader(filename, videoStreamIndex)
15-
, _videoStreamProperties(NULL)
16-
, _outputWidth(0)
17-
, _outputHeight(0)
18-
, _outputPixelProperties("rgb24")
19-
{
20-
init();
21-
}
22-
23-
VideoReader::VideoReader(InputFile& inputFile, const size_t videoStreamIndex)
24-
: IReader(inputFile, videoStreamIndex)
13+
VideoReader::VideoReader(const InputStreamDesc& inputDesc)
14+
: IReader(inputDesc)
2515
, _videoStreamProperties(NULL)
2616
, _outputWidth(0)
2717
, _outputHeight(0)
@@ -35,17 +25,17 @@ void VideoReader::init()
3525
// analyse InputFile
3626
avtranscoder::NoDisplayProgress p;
3727
_inputFile->analyse(p);
38-
_streamProperties = &_inputFile->getProperties().getStreamPropertiesWithIndex(_streamIndex);
28+
_streamProperties = &_inputFile->getProperties().getStreamPropertiesWithIndex(_inputDesc._streamIndex);
3929
_videoStreamProperties = static_cast<const VideoProperties*>(_streamProperties);
40-
_inputFile->activateStream(_streamIndex);
30+
_inputFile->activateStream(_inputDesc._streamIndex);
4131

4232
// setup decoder
43-
_decoder = new VideoDecoder(_inputFile->getStream(_streamIndex));
33+
_decoder = new VideoDecoder(_inputFile->getStream(_inputDesc._streamIndex));
4434
_decoder->setupDecoder();
4535
_currentDecoder = _decoder;
4636

4737
// create src frame
48-
const VideoFrameDesc srcFrameDesc = _inputFile->getStream(_streamIndex).getVideoCodec().getVideoFrameDesc();
38+
const VideoFrameDesc srcFrameDesc = _inputFile->getStream(_inputDesc._streamIndex).getVideoCodec().getVideoFrameDesc();
4939
_srcFrame = new VideoFrame(srcFrameDesc, false);
5040
VideoFrame* srcFrame = static_cast<VideoFrame*>(_srcFrame);
5141
// create dst frame

src/AvTranscoder/reader/VideoReader.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ class AvExport VideoReader : public IReader
1616
//@{
1717
// @note Transform the input stream to rgb24 pixel format (to display).
1818
// @see updateOutput
19-
VideoReader(const std::string& filename, const size_t videoStreamIndex = 0);
20-
VideoReader(InputFile& inputFile, const size_t videoStreamIndex = 0);
19+
VideoReader(const InputStreamDesc& inputDesc);
2120
//@}
2221

2322
~VideoReader();

src/AvTranscoder/transcoder/InputStreamDesc.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,26 @@ struct InputStreamDesc
3737
_channelIndexArray.push_back(channelIndex);
3838
}
3939

40+
/**
41+
* @brief Read all the channels of the indicated stream.
42+
*/
4043
InputStreamDesc(const std::string& filename, const size_t streamIndex)
4144
: _filename(filename)
4245
, _streamIndex(streamIndex)
4346
, _channelIndexArray()
4447
{
4548
}
4649

50+
/**
51+
* @brief Read all the channels of the stream at index 0.
52+
*/
53+
InputStreamDesc(const std::string& filename)
54+
: _filename(filename)
55+
, _streamIndex(0)
56+
, _channelIndexArray()
57+
{
58+
}
59+
4760
/**
4861
* @return If a demultiplexing step will be done to extract the expected data.
4962
*/

test/pyTest/testAudioReader.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
from pyAvTranscoder import avtranscoder as av
1111

1212

13-
def testAudioReaderCreateNewInputFile():
13+
def testAudioReader():
1414
"""
1515
Read a audio stream with the AudioReader.
1616
The InputFile is created inside the reader.
1717
"""
1818
inputFileName = os.environ['AVTRANSCODER_TEST_AUDIO_WAVE_FILE']
19-
reader = av.AudioReader(inputFileName)
19+
reader = av.AudioReader(av.InputStreamDesc(inputFileName))
2020

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

4343
# create reader to read all channels of the audio stream
44-
readerOfAllChannels = av.AudioReader(inputFile, streamIndex)
44+
readerOfAllChannels = av.AudioReader(av.InputStreamDesc(inputFileName, streamIndex))
4545
nbChannels = readerOfAllChannels.getOutputNbChannels()
4646
# read first frame
4747
frame = readerOfAllChannels.readNextFrame()
4848
sizeOfFrameWithAllChannels = frame.getDataSize()
4949

5050
# create reader to read one channel of the audio stream
51-
readerOfOneChannel = av.AudioReader(inputFile, streamIndex, channelIndex)
51+
readerOfOneChannel = av.AudioReader(av.InputStreamDesc(inputFileName, streamIndex, channelIndex))
5252
# read first frame
5353
frame = readerOfOneChannel.readNextFrame()
5454
sizeOfFrameWithOneChannels = frame.getDataSize()
5555

5656
assert_equals( sizeOfFrameWithAllChannels / nbChannels, sizeOfFrameWithOneChannels )
5757

58-
# Force to call the readers destructor before the inputFile destructor (which cannot happen in C++)
59-
readerOfAllChannels = None
60-
readerOfOneChannel = None
61-
6258

6359
def testAudioReaderWithGenerator():
6460
"""
6561
Read an audio stream with the AudioReader.
6662
When there is no more data to decode, switch to a generator and process some frames.
6763
"""
6864
inputFileName = os.environ['AVTRANSCODER_TEST_AUDIO_WAVE_FILE']
69-
reader = av.AudioReader(inputFileName)
65+
reader = av.AudioReader(av.InputStreamDesc(inputFileName))
7066

7167
# read all frames and check their size
7268
while True:

test/pyTest/testNbFrames.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def testNbFramesVideoRewrap():
2020
ouputFile = av.OutputFile( outputFileName )
2121
transcoder = av.Transcoder( ouputFile )
2222

23-
transcoder.addStream( av.InputStreamDesc(inputFileName, 0) )
23+
transcoder.addStream( av.InputStreamDesc(inputFileName) )
2424

2525
progress = av.ConsoleProgress()
2626
transcoder.process( progress )
@@ -47,7 +47,7 @@ def testNbFramesVideoTranscode():
4747
ouputFile = av.OutputFile( outputFileName )
4848
transcoder = av.Transcoder( ouputFile )
4949

50-
transcoder.addStream( av.InputStreamDesc(inputFileName, 0), "mpeg2" )
50+
transcoder.addStream( av.InputStreamDesc(inputFileName), "mpeg2" )
5151

5252
progress = av.ConsoleProgress()
5353
transcoder.process( progress )

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy