Skip to content

Commit 05ad0da

Browse files
author
Clement Champetier
committed
StreamTranscoder: manage offset in seconds
StreamTranscoder: * remove _frameProcessed * offset is a double
1 parent ed1f7f8 commit 05ad0da

File tree

5 files changed

+37
-42
lines changed

5 files changed

+37
-42
lines changed

src/AvTranscoder/file/InputFile.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,10 @@ bool InputFile::readNextPacket( CodedData& data, const size_t streamIndex )
143143

144144
void InputFile::seekAtFrame( const size_t frame )
145145
{
146-
// Get Fps from first video stream or first audio stream if no video
147146
double fps = 1;
147+
// Get Fps from first video stream
148148
if( _properties.getNbVideoStreams() )
149149
fps = _properties.getVideoProperties().at( 0 ).getFps();
150-
else if( _properties.getNbAudioStreams() )
151-
fps = _properties.getAudioProperties().at( 0 ).getFps();
152150

153151
uint64_t pos = frame / fps * AV_TIME_BASE;
154152

src/AvTranscoder/transcoder/StreamTranscoder.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ StreamTranscoder::StreamTranscoder(
3434
, _outputEncoder( NULL )
3535
, _transform( NULL )
3636
, _subStreamIndex( -1 )
37-
, _frameProcessed( 0 )
3837
, _offset( 0 )
3938
, _takeFromGenerator( false )
4039
, _verbose( false )
@@ -69,7 +68,7 @@ StreamTranscoder::StreamTranscoder(
6968
IOutputFile& outputFile,
7069
const ProfileLoader::Profile& profile,
7170
const int subStreamIndex,
72-
const size_t offset
71+
const double offset
7372
)
7473
: _inputStream( &inputStream )
7574
, _outputStream( NULL )
@@ -81,7 +80,6 @@ StreamTranscoder::StreamTranscoder(
8180
, _outputEncoder( NULL )
8281
, _transform( NULL )
8382
, _subStreamIndex( subStreamIndex )
84-
, _frameProcessed( 0 )
8583
, _offset( offset )
8684
, _takeFromGenerator( false )
8785
, _verbose( false )
@@ -176,7 +174,6 @@ StreamTranscoder::StreamTranscoder(
176174
, _outputEncoder( NULL )
177175
, _transform( NULL )
178176
, _subStreamIndex( -1 )
179-
, _frameProcessed( 0 )
180177
, _offset( 0 )
181178
, _takeFromGenerator( false )
182179
, _verbose( false )
@@ -275,8 +272,6 @@ void StreamTranscoder::preProcessCodecLatency()
275272

276273
bool StreamTranscoder::processFrame()
277274
{
278-
++_frameProcessed;
279-
280275
if( ! _inputDecoder )
281276
{
282277
return processRewrap();
@@ -328,13 +323,17 @@ bool StreamTranscoder::processTranscode( const int subStreamIndex )
328323
if( _verbose )
329324
std::cout << "transcode a frame " << std::endl;
330325

331-
if( _offset &&
332-
_frameProcessed > _offset &&
333-
! _offsetPassed &&
334-
_takeFromGenerator )
326+
// check offset
327+
if( _offset )
335328
{
336-
switchToInputEssence();
337-
_offsetPassed = true;
329+
bool endOfOffset = _outputStream->getStreamDuration() >= _offset ? true : false;
330+
if( endOfOffset )
331+
{
332+
// switch to essence from input stream
333+
switchToInputEssence();
334+
// reset offset
335+
_offset = 0;
336+
}
338337
}
339338

340339
bool decodingStatus = false;
@@ -406,9 +405,7 @@ double StreamTranscoder::getDuration() const
406405
{
407406
if( _inputStream )
408407
{
409-
double totalDuration = 0;
410-
totalDuration += _inputStream->getDuration();
411-
// @todo add offset
408+
double totalDuration = _inputStream->getDuration() + _offset;
412409
return totalDuration;
413410
}
414411
else

src/AvTranscoder/transcoder/StreamTranscoder.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class AvExport StreamTranscoder
3030
/**
3131
* @brief transcode stream
3232
**/
33-
StreamTranscoder( IInputStream& inputStream, IOutputFile& outputFile, const ProfileLoader::Profile& profile, const int subStreamIndex = -1, const size_t offset = 0 );
33+
StreamTranscoder( IInputStream& inputStream, IOutputFile& outputFile, const ProfileLoader::Profile& profile, const int subStreamIndex = -1, const double offset = 0 );
3434

3535
/**
3636
* @brief encode from a generated stream
@@ -92,8 +92,7 @@ class AvExport StreamTranscoder
9292

9393
int _subStreamIndex; ///< Index of channel that is processed from the input stream (-1 if no demultiplexing).
9494

95-
size_t _frameProcessed; ///< How many frame processed for this StreamTranscoder.
96-
size_t _offset; ///< Offset, in frame, at the beginning of the StreamTranscoder.
95+
double _offset; ///< Offset, in seconds, at the beginning of the StreamTranscoder.
9796

9897
bool _takeFromGenerator; ///< Is the data processed are taken from a generator.
9998
bool _verbose;

src/AvTranscoder/transcoder/Transcoder.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Transcoder::~Transcoder()
4040
}
4141
}
4242

43-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName, const size_t offset )
43+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName, const double offset )
4444
{
4545
// Re-wrap
4646
if( profileName.length() == 0 )
@@ -62,7 +62,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
6262
}
6363
}
6464

65-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName, ICodec& codec, const size_t offset )
65+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName, ICodec& codec, const double offset )
6666
{
6767
// Re-wrap
6868
if( profileName.length() == 0 )
@@ -84,7 +84,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
8484
}
8585
}
8686

87-
void Transcoder::add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, const size_t offset )
87+
void Transcoder::add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, const double offset )
8888
{
8989
// Check filename
9090
if( ! filename.length() )
@@ -95,7 +95,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, Pro
9595
addTranscodeStream( filename, streamIndex, -1, profile, offset );
9696
}
9797

98-
void Transcoder::add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, ICodec& codec, const size_t offset )
98+
void Transcoder::add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, ICodec& codec, const double offset )
9999
{
100100
// Generator
101101
if( ! filename.length() )
@@ -113,7 +113,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, Pro
113113
}
114114
}
115115

116-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, const size_t offset )
116+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, const double offset )
117117
{
118118
// No subStream selected
119119
if( subStreamIndex < 0 )
@@ -148,7 +148,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
148148
}
149149
}
150150

151-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, ICodec& codec, const size_t offset )
151+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, ICodec& codec, const double offset )
152152
{
153153
// No subStream selected
154154
if( subStreamIndex < 0 )
@@ -184,7 +184,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
184184
}
185185
}
186186

187-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, const size_t offset )
187+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, const double offset )
188188
{
189189
// No subStream selected
190190
if( subStreamIndex < 0 )
@@ -202,7 +202,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
202202
addTranscodeStream( filename, streamIndex, subStreamIndex, profile, offset );
203203
}
204204

205-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, ICodec& codec, const size_t offset )
205+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, ICodec& codec, const double offset )
206206
{
207207
// No subStream selected
208208
if( subStreamIndex < 0 )
@@ -328,7 +328,7 @@ void Transcoder::addRewrapStream( const std::string& filename, const size_t stre
328328
_streamTranscoders.push_back( _streamTranscodersAllocated.back() );
329329
}
330330

331-
void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, const size_t offset )
331+
void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, const double offset )
332332
{
333333
InputFile* referenceFile = addInputFile( filename, streamIndex );
334334

@@ -379,7 +379,7 @@ void Transcoder::addTranscodeStream( const std::string& filename, const size_t s
379379
}
380380
}
381381

382-
void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, ProfileLoader::Profile& profile, const size_t offset )
382+
void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, ProfileLoader::Profile& profile, const double offset )
383383
{
384384
// Add profile
385385
_profileLoader.loadProfile( profile );

src/AvTranscoder/transcoder/Transcoder.hpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,46 +47,47 @@ class AvExport Transcoder
4747
/**
4848
* @brief Add a stream and set a profile
4949
* @note If profileName is empty, rewrap.
50+
* @note offset in seconds
5051
*/
51-
void add( const std::string& filename, const size_t streamIndex, const std::string& profileName = "", const size_t offset = 0 );
52+
void add( const std::string& filename, const size_t streamIndex, const std::string& profileName = "", const double offset = 0 );
5253
/*
5354
* @note If filename is empty, add a generated stream.
5455
* @note If filename is empty, profileName can't be empty (no sens to rewrap a generated stream).
5556
*/
56-
void add( const std::string& filename, const size_t streamIndex, const std::string& profileName, ICodec& codec, const size_t offset = 0 );
57+
void add( const std::string& filename, const size_t streamIndex, const std::string& profileName, ICodec& codec, const double offset = 0 );
5758

5859
/**
5960
* @brief Add a stream and set a custom profile
6061
* @note Profile will be updated, be sure to pass unique profile name.
6162
*/
62-
void add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, const size_t offset = 0 );
63+
void add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, const double offset = 0 );
6364
/*
6465
* @note If filename is empty, add a generated stream.
6566
*/
66-
void add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, ICodec& codec, const size_t offset = 0 );
67+
void add( const std::string& filename, const size_t streamIndex, ProfileLoader::Profile& profile, ICodec& codec, const double offset = 0 );
6768

6869
/**
6970
* @brief Add a stream and set a profile
7071
* @note If profileName is empty, rewrap.
71-
* @note If subStreamIndex is negative, no substream is selected it's the stream.
72+
* @note If subStreamIndex is negative, no substream is selected it's the stream.
7273
*/
73-
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName = "", const size_t offset = 0 );
74+
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName = "", const double offset = 0 );
7475
/**
7576
* @note If filename is empty, add a generated stream.
7677
* @note If filename is empty, profileName can't be empty (no sens to rewrap a generated stream).
7778
*/
78-
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, ICodec& codec, const size_t offset = 0 );
79+
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, ICodec& codec, const double offset = 0 );
7980

8081
/**
8182
* @brief Add a stream and set a custom profile
8283
* @note Profile will be updated, be sure to pass unique profile name.
8384
* @note If subStreamIndex is negative, no substream is selected it's the stream.
8485
*/
85-
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, const size_t offset = 0 );
86+
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, const double offset = 0 );
8687
/**
8788
* @note If filename is empty, add a generated stream.
8889
*/
89-
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, ICodec& codec, const size_t offset = 0 );
90+
void add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, ProfileLoader::Profile& profile, ICodec& codec, const double offset = 0 );
9091

9192
/**
9293
* @brief Add the stream
@@ -137,8 +138,8 @@ class AvExport Transcoder
137138
private:
138139
void addRewrapStream( const std::string& filename, const size_t streamIndex );
139140

140-
void addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, const size_t offset ); ///< Get profile from input
141-
void addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, ProfileLoader::Profile& profile, const size_t offset = 0 );
141+
void addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, const double offset ); ///< Get profile from input
142+
void addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, ProfileLoader::Profile& profile, const double offset = 0 );
142143

143144
void addDummyStream( const ProfileLoader::Profile& profile, const ICodec& codec );
144145

0 commit comments

Comments
 (0)