Skip to content

Commit c391609

Browse files
committed
Merge pull request #263 from cchampet/fix_negativeOffset
Fix negative offset
2 parents f757baf + 6331f3c commit c391609

File tree

10 files changed

+79
-13
lines changed

10 files changed

+79
-13
lines changed

src/AvTranscoder/decoder/AudioDecoder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ void AudioDecoder::setupDecoder( const ProfileLoader::Profile& profile )
6262
throw std::runtime_error( msg );
6363
}
6464

65-
LOG_INFO( "Setup audio decoder with:\n" << profile )
65+
if( ! profile.empty() )
66+
{
67+
LOG_INFO( "Setup audio decoder with:\n" << profile )
68+
}
6669

6770
AudioCodec& codec = _inputStream->getAudioCodec();
6871

src/AvTranscoder/decoder/VideoDecoder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ void VideoDecoder::setupDecoder( const ProfileLoader::Profile& profile )
6060
throw std::runtime_error( msg );
6161
}
6262

63-
LOG_INFO( "Setup video decoder with:\n" << profile )
63+
if( ! profile.empty() )
64+
{
65+
LOG_INFO( "Setup video decoder with:\n" << profile )
66+
}
6467

6568
VideoCodec& codec = _inputStream->getVideoCodec();
6669

src/AvTranscoder/encoder/AudioEncoder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ AudioEncoder::~AudioEncoder()
3737

3838
void AudioEncoder::setupAudioEncoder( const AudioFrameDesc& frameDesc, const ProfileLoader::Profile& profile )
3939
{
40-
LOG_INFO( "Setup audio encoder with:\n" << profile )
40+
if( ! profile.empty() )
41+
{
42+
LOG_INFO( "Setup audio encoder with:\n" << profile )
43+
}
4144

4245
// set sampleRate, number of channels, sample format
4346
_codec.setAudioParameters( frameDesc );

src/AvTranscoder/encoder/VideoEncoder.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ VideoEncoder::~VideoEncoder()
3838

3939
void VideoEncoder::setupVideoEncoder( const VideoFrameDesc& frameDesc, const ProfileLoader::Profile& profile )
4040
{
41-
LOG_INFO( "Setup video encoder with:\n" << profile )
41+
if( ! profile.empty() )
42+
{
43+
LOG_INFO( "Setup video encoder with:\n" << profile )
44+
}
4245

4346
// set width, height, pixel format, fps
4447
_codec.setImageParameters( frameDesc );

src/AvTranscoder/file/FormatContext.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,9 @@ AVStream& FormatContext::addAVStream( const AVCodec& avCodec )
142142
return *stream;
143143
}
144144

145-
bool FormatContext::seek( uint64_t position, const int flag )
145+
bool FormatContext::seek( const uint64_t position, const int flag )
146146
{
147-
if( (int)getStartTime() != AV_NOPTS_VALUE )
148-
position += getStartTime();
149-
147+
LOG_INFO( "Seek in '" << _avFormatContext->filename << "' at " << position << " (in AV_TIME_BASE units)" )
150148
int err = av_seek_frame( _avFormatContext, -1, position, flag );
151149
if( err < 0 )
152150
{

src/AvTranscoder/file/FormatContext.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ class AvExport FormatContext
7777
* @brief Seek at a specific position
7878
* @param position: can be in AV_TIME_BASE units, in frames... depending on the flag value
7979
* @param flag: seeking mode (AVSEEK_FLAG_xxx)
80-
* @note before seek, add offset of start time
8180
* @return seek status
81+
* @see flushDecoder
8282
*/
83-
bool seek( uint64_t position, const int flag );
83+
bool seek( const uint64_t position, const int flag );
8484

8585
size_t getNbStreams() const { return _avFormatContext->nb_streams; }
8686
/// Get duration of the program, in seconds

src/AvTranscoder/file/InputFile.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,10 @@ void InputFile::setupUnwrapping( const ProfileLoader::Profile& profile )
173173
throw std::runtime_error( msg );
174174
}
175175

176-
// set profile
177-
LOG_INFO( "Setup unwrapping with:\n" << profile )
176+
if( ! profile.empty() )
177+
{
178+
LOG_INFO( "Setup unwrapping with:\n" << profile )
179+
}
178180

179181
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
180182
{

src/AvTranscoder/file/OutputFile.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ void OutputFile::setupWrapping( const ProfileLoader::Profile& profile )
223223
throw std::runtime_error( msg );
224224
}
225225

226-
LOG_INFO( "Setup wrapping with:\n" << profile )
226+
if( ! profile.empty() )
227+
{
228+
LOG_INFO( "Setup wrapping with:\n" << profile )
229+
}
227230

228231
// check if output format indicated is valid with the filename extension
229232
if( ! matchFormat( profile.find( constants::avProfileFormat )->second, getFilename() ) )

src/AvTranscoder/reader/IReader.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <AvTranscoder/mediaProperty/print.hpp>
44

5+
#include <cassert>
6+
57
namespace avtranscoder
68
{
79

@@ -49,6 +51,11 @@ Frame* IReader::readPrevFrame()
4951

5052
Frame* IReader::readFrameAt( const size_t frame )
5153
{
54+
assert( _decoder != NULL );
55+
assert( _transform != NULL );
56+
assert( _srcFrame != NULL );
57+
assert( _dstFrame != NULL );
58+
5259
if( (int)frame != _currentFrame + 1 )
5360
{
5461
// seek
@@ -65,6 +72,7 @@ Frame* IReader::readFrameAt( const size_t frame )
6572

6673
void IReader::printInfo()
6774
{
75+
assert( _streamProperties != NULL );
6876
std::cout << *_streamProperties << std::endl;
6977
}
7078

test/pyTest/testOffset.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def testRewrapAudioPositiveOffset():
102102

103103
# check output duration
104104
assert_equals( src_audioStream.getDuration() + offset, dst_audioStream.getDuration() )
105+
assert_equals( src_audioStream.getNbSamples() + ( offset * dst_audioStream.getSampleRate() * dst_audioStream.getChannels() ), dst_audioStream.getNbSamples() )
105106

106107

107108
def testRewrapAudioNegativeOffset():
@@ -132,6 +133,7 @@ def testRewrapAudioNegativeOffset():
132133

133134
# check output duration
134135
assert_almost_equals( src_audioStream.getDuration() + offset, dst_audioStream.getDuration(), delta=0.01 )
136+
assert_equals( src_audioStream.getNbSamples() + ( offset * dst_audioStream.getSampleRate() * dst_audioStream.getChannels() ), dst_audioStream.getNbSamples() )
135137

136138

137139
def testTranscodeVideoPositiveOffset():
@@ -222,6 +224,7 @@ def testRewrapVideoPositiveOffset():
222224

223225
# check output duration
224226
assert_equals( src_videoStream.getDuration() + offset, dst_videoStream.getDuration() )
227+
assert_equals( src_videoStream.getNbFrames() + ( offset * dst_videoStream.getFps() ), dst_videoStream.getNbFrames() )
225228

226229

227230
def testRewrapVideoNegativeOffset():
@@ -252,6 +255,7 @@ def testRewrapVideoNegativeOffset():
252255

253256
# check output duration
254257
assert_equals( src_videoStream.getDuration() + offset, dst_videoStream.getDuration() )
258+
assert_equals( src_videoStream.getNbFrames() + ( offset * dst_videoStream.getFps() ), dst_videoStream.getNbFrames() )
255259

256260

257261
def testMultipleOffsetFromSameInputFile():
@@ -276,12 +280,51 @@ def testMultipleOffsetFromSameInputFile():
276280
src_inputFile = av.InputFile( inputFileName )
277281
src_properties = src_inputFile.getProperties()
278282
src_videoStream = src_properties.getVideoProperties()[0]
283+
src_audioStream = src_properties.getAudioProperties()[0]
279284

280285
# get dst file
281286
dst_inputFile = av.InputFile( outputFileName )
282287
dst_properties = dst_inputFile.getProperties()
283288
dst_videoStream = dst_properties.getVideoProperties()[0]
289+
dst_audioStream = dst_properties.getAudioProperties()[0]
284290

285291
# check output duration
286292
assert_equals( src_videoStream.getDuration() + offset_1, dst_videoStream.getDuration() )
293+
assert_equals( src_audioStream.getDuration() + offset_1, dst_audioStream.getDuration() )
294+
295+
296+
def testMultipleOffsetFromSameStream():
297+
"""
298+
Process same stream several times with different offset at the beginning of the process.
299+
"""
300+
inputFileName = os.environ['AVTRANSCODER_TEST_AUDIO_MOV_FILE']
301+
outputFileName = "testMultipleOffsetFromSameStream.mov"
302+
offset_1 = 2
303+
offset_2 = -2
304+
305+
ouputFile = av.OutputFile( outputFileName )
306+
transcoder = av.Transcoder( ouputFile )
307+
308+
transcoder.add( inputFileName, 0, "", offset_1 )
309+
transcoder.add( inputFileName, 0, "", offset_2 )
310+
311+
progress = av.ConsoleProgress()
312+
transcoder.process( progress )
313+
314+
# get src file
315+
src_inputFile = av.InputFile( inputFileName )
316+
src_properties = src_inputFile.getProperties()
317+
src_videoStream = src_properties.getVideoProperties()[0]
318+
319+
# get dst file
320+
dst_inputFile = av.InputFile( outputFileName )
321+
dst_properties = dst_inputFile.getProperties()
322+
dst_videoStream_1 = dst_properties.getVideoProperties()[0]
323+
dst_videoStream_2 = dst_properties.getVideoProperties()[1]
324+
325+
# check output duration
326+
assert_equals( src_videoStream.getDuration() + offset_1, dst_videoStream_1.getDuration() )
327+
assert_equals( src_videoStream.getDuration() + offset_1, dst_videoStream_2.getDuration() )
328+
assert_almost_equals( src_videoStream.getNbFrames() + ( offset_1 * dst_videoStream_1.getFps() ), dst_videoStream_1.getNbFrames(), delta=0.01 )
329+
assert_almost_equals( src_videoStream.getNbFrames() + ( offset_1 * dst_videoStream_2.getFps() ), dst_videoStream_2.getNbFrames(), delta=0.01 )
287330

0 commit comments

Comments
 (0)