Skip to content

Commit de18797

Browse files
author
Clement Champetier
committed
StreamTranscoder: add offset at the beginning of the stream
* Transcoder: can specified an offset when add a StreamTranscoder. * StreamTranscoder: can set the offset. * Add documenation about the offset.
1 parent 02cbb66 commit de18797

File tree

4 files changed

+96
-48
lines changed

4 files changed

+96
-48
lines changed

src/AvTranscoder/Transcoder/StreamTranscoder.cpp

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ StreamTranscoder::StreamTranscoder(
3232
, _outputEssence( NULL )
3333
, _transform( NULL )
3434
, _subStreamIndex( -1 )
35+
, _frameProcessed( 0 )
36+
, _offset( 0 )
3537
, _transcodeStream( false )
38+
, _takeFromDummy( false )
3639
, _infiniteProcess( false )
3740
, _verbose( false )
41+
, _offsetPassed( false )
3842
{
3943
// create a re-wrapping case
4044
switch( _inputStream->getStreamType() )
@@ -58,7 +62,8 @@ StreamTranscoder::StreamTranscoder(
5862
InputStream& inputStream,
5963
OutputFile& outputFile,
6064
const Profile::ProfileDesc& profile,
61-
const int subStreamIndex
65+
const int subStreamIndex,
66+
const size_t offset
6267
)
6368
: _inputStream( &inputStream )
6469
, _outputStream( NULL )
@@ -69,9 +74,13 @@ StreamTranscoder::StreamTranscoder(
6974
, _outputEssence( NULL )
7075
, _transform( NULL )
7176
, _subStreamIndex( subStreamIndex )
77+
, _frameProcessed( 0 )
78+
, _offset( offset )
7279
, _transcodeStream( true )
80+
, _takeFromDummy( false )
7381
, _infiniteProcess( false )
7482
, _verbose( false )
83+
, _offsetPassed( false )
7584
{
7685
// create a transcode case
7786
switch( _inputStream->getStreamType() )
@@ -99,8 +108,6 @@ StreamTranscoder::StreamTranscoder(
99108
DummyVideo* dummyVideo = new DummyVideo();
100109
dummyVideo->setVideoDesc( outputVideo->getVideoDesc() );
101110
_dummyEssence = dummyVideo;
102-
103-
_currentEssence = _inputEssence;
104111

105112
break;
106113
}
@@ -137,8 +144,6 @@ StreamTranscoder::StreamTranscoder(
137144
dummyAudio->setAudioDesc( outputAudio->getAudioDesc() );
138145
_dummyEssence = dummyAudio;
139146

140-
_currentEssence = _inputEssence;
141-
142147
break;
143148
}
144149
default:
@@ -147,6 +152,8 @@ StreamTranscoder::StreamTranscoder(
147152
break;
148153
}
149154
}
155+
156+
switchEssence( offset != 0 );
150157
}
151158

152159
StreamTranscoder::StreamTranscoder(
@@ -163,9 +170,13 @@ StreamTranscoder::StreamTranscoder(
163170
, _outputEssence( NULL )
164171
, _transform( NULL )
165172
, _subStreamIndex( -1 )
173+
, _frameProcessed( 0 )
174+
, _offset( 0 )
166175
, _transcodeStream( true )
176+
, _takeFromDummy( false )
167177
, _infiniteProcess( false )
168178
, _verbose( false )
179+
, _offsetPassed( false )
169180
{
170181
// create a coding case based on a InputEssence (aka dummy reader)
171182
if( ! profile.count( Profile::avProfileType ) )
@@ -237,6 +248,7 @@ StreamTranscoder::~StreamTranscoder()
237248

238249
bool StreamTranscoder::processFrame()
239250
{
251+
++_frameProcessed;
240252
if( _transcodeStream )
241253
{
242254
if( _subStreamIndex < 0 )
@@ -297,6 +309,16 @@ bool StreamTranscoder::processTranscode()
297309
DataStream dataStream;
298310
if( _verbose )
299311
std::cout << "transcode a frame " << std::endl;
312+
313+
if( _offset &&
314+
_frameProcessed > _offset &&
315+
! _offsetPassed &&
316+
_takeFromDummy )
317+
{
318+
switchToInputEssence();
319+
_offsetPassed = true;
320+
}
321+
300322
if( _currentEssence->readNextFrame( *_sourceBuffer ) )
301323
{
302324
if( _verbose )
@@ -340,6 +362,16 @@ bool StreamTranscoder::processTranscode( const int subStreamIndex )
340362
DataStream dataStream;
341363
if( _verbose )
342364
std::cout << "transcode a frame " << std::endl;
365+
366+
if( _offset &&
367+
_frameProcessed > _offset &&
368+
! _offsetPassed &&
369+
_takeFromDummy )
370+
{
371+
switchToInputEssence();
372+
_offsetPassed = true;
373+
}
374+
343375
if( _currentEssence->readNextFrame( *_sourceBuffer, subStreamIndex ) )
344376
{
345377
if( _verbose )
@@ -368,22 +400,21 @@ bool StreamTranscoder::processTranscode( const int subStreamIndex )
368400
return true;
369401
}
370402

371-
void StreamTranscoder::switchToDummyEssence()
403+
void StreamTranscoder::switchEssence( bool swithToDummy )
372404
{
373-
if( _dummyEssence == NULL )
374-
return;
375-
_takeFromDummy = true;
376-
_currentEssence = _dummyEssence;
405+
_takeFromDummy = swithToDummy;
406+
_currentEssence = swithToDummy ? _dummyEssence : _inputEssence;
377407
assert( _currentEssence != NULL );
378408
}
379409

410+
void StreamTranscoder::switchToDummyEssence()
411+
{
412+
switchEssence( true );
413+
}
414+
380415
void StreamTranscoder::switchToInputEssence()
381416
{
382-
if( _inputEssence == NULL )
383-
return;
384-
_takeFromDummy = false;
385-
_currentEssence = _inputEssence;
386-
assert( _currentEssence != NULL );
417+
switchEssence( false );
387418
}
388419

389420
}

src/AvTranscoder/Transcoder/StreamTranscoder.hpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ class StreamTranscoder
2222
public:
2323
/**
2424
* @brief rewrap stream
25+
* @note offset feature when rewrap a stream is not supported
2526
**/
2627
StreamTranscoder( InputStream& inputStream, OutputFile& outputFile );
2728

2829
/**
2930
* @brief transcode stream
3031
**/
31-
StreamTranscoder( InputStream& inputStream, OutputFile& outputFile, const Profile::ProfileDesc& profile, const int subStreamIndex = -1 );
32+
StreamTranscoder( InputStream& inputStream, OutputFile& outputFile, const Profile::ProfileDesc& profile, const int subStreamIndex = -1, const size_t offset = 0 );
3233

3334
/**
3435
* @brief encode from dummy stream
36+
* @note offset feature has no sense here
3537
**/
3638
StreamTranscoder( InputEssence& inputEssence, OutputFile& outputFile, const Profile::ProfileDesc& profile );
3739

@@ -47,10 +49,13 @@ class StreamTranscoder
4749

4850
void setVerbose( bool verbose = true ){ _verbose = verbose; }
4951

52+
void switchEssence( bool swithToDummy = true );
5053
void switchToDummyEssence();
5154
void switchToInputEssence();
5255
void setInfinityProcess( bool infinity = true ){ _infiniteProcess = infinity; }
5356

57+
void setOffset( bool offset = true ){ _offset = offset; }
58+
5459
private:
5560
bool processRewrap();
5661
bool processRewrap( const int subStreamIndex );
@@ -72,11 +77,23 @@ class StreamTranscoder
7277
EssenceTransform* _transform;
7378

7479
int _subStreamIndex;
80+
81+
/**
82+
* @brief How many frame processed for this StreamTranscoder.
83+
*/
84+
size_t _frameProcessed;
85+
/**
86+
* @brief Offset, in frame, at the beginning of the StreamTranscoder.
87+
*/
88+
size_t _offset;
89+
7590
bool _transcodeStream;
7691
bool _takeFromDummy;
7792
bool _infiniteProcess;
7893

7994
bool _verbose;
95+
96+
bool _offsetPassed;
8097
};
8198

8299
}

src/AvTranscoder/Transcoder/Transcoder.cpp

Lines changed: 22 additions & 22 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 )
43+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName, const size_t offset )
4444
{
4545
if( profileName.length() == 0 ) // no profile, only re-wrap stream
4646
{
@@ -51,10 +51,10 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
5151
}
5252

5353
Profile::ProfileDesc& transcodeProfile = _profile.getProfile( profileName );
54-
add( filename, streamIndex, transcodeProfile );
54+
add( filename, streamIndex, transcodeProfile, offset );
5555
}
5656

57-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName, CodedDesc& essenceDesc )
57+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const std::string& profileName, CodedDesc& essenceDesc, const size_t offset )
5858
{
5959
if( profileName.length() == 0 ) // no profile, only re-wrap stream
6060
{
@@ -72,10 +72,10 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
7272
}
7373

7474
Profile::ProfileDesc& transcodeProfile = _profile.getProfile( profileName );
75-
add( filename, streamIndex, transcodeProfile, essenceDesc );
75+
add( filename, streamIndex, transcodeProfile, essenceDesc, offset );
7676
}
7777

78-
void Transcoder::add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc )
78+
void Transcoder::add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc, const size_t offset )
7979
{
8080
_profile.update( profileDesc );
8181
if( ! filename.length() )
@@ -87,10 +87,10 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, Pro
8787

8888
if( _verbose )
8989
std::cout << "add transcoding stream" << std::endl;
90-
addTranscodeStream( filename, streamIndex, profileDesc );
90+
addTranscodeStream( filename, streamIndex, profileDesc, offset );
9191
}
9292

93-
void Transcoder::add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc )
93+
void Transcoder::add( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc, const size_t offset )
9494
{
9595
_profile.update( profileDesc );
9696
if( ! filename.length() )
@@ -103,14 +103,14 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, Pro
103103

104104
if( _verbose )
105105
std::cout << "add transcoding stream" << std::endl;
106-
addTranscodeStream( filename, streamIndex, profileDesc );
106+
addTranscodeStream( filename, streamIndex, profileDesc, offset );
107107
}
108108

109-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName )
109+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, const size_t offset )
110110
{
111111
if( subStreamIndex < 0 )
112112
{
113-
add( filename, streamIndex, profileName );
113+
add( filename, streamIndex, profileName, offset );
114114
return;
115115
}
116116

@@ -124,10 +124,10 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
124124
}
125125

126126
Profile::ProfileDesc& transcodeProfile = _profile.getProfile( profileName );
127-
add( filename, streamIndex, subStreamIndex, transcodeProfile );
127+
add( filename, streamIndex, subStreamIndex, transcodeProfile, offset );
128128
}
129129

130-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, CodedDesc& essenceDesc )
130+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, const std::string& profileName, CodedDesc& essenceDesc, const size_t offset )
131131
{
132132
if( subStreamIndex < 0 )
133133
{
@@ -151,16 +151,16 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
151151
}
152152

153153
Profile::ProfileDesc& transcodeProfile = _profile.getProfile( profileName );
154-
add( filename, streamIndex, subStreamIndex, transcodeProfile, essenceDesc );
154+
add( filename, streamIndex, subStreamIndex, transcodeProfile, essenceDesc, offset );
155155
}
156156

157-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc )
157+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc, const size_t offset )
158158
{
159159
_profile.update( profileDesc );
160160

161161
if( subStreamIndex < 0 )
162162
{
163-
add( filename, streamIndex, profileDesc );
163+
add( filename, streamIndex, profileDesc, offset );
164164
return;
165165
}
166166

@@ -173,10 +173,10 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
173173

174174
if( _verbose )
175175
std::cout << "add transcoding stream for substream " << subStreamIndex << std::endl;
176-
addTranscodeStream( filename, streamIndex, subStreamIndex, profileDesc );
176+
addTranscodeStream( filename, streamIndex, subStreamIndex, profileDesc, offset );
177177
}
178178

179-
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc )
179+
void Transcoder::add( const std::string& filename, const size_t streamIndex, const int subStreamIndex, Profile::ProfileDesc& profileDesc, CodedDesc& essenceDesc, const size_t offset )
180180
{
181181
_profile.update( profileDesc );
182182

@@ -196,7 +196,7 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex, con
196196

197197
if( _verbose )
198198
std::cout << "add transcoding stream for substream " << subStreamIndex << std::endl;
199-
addTranscodeStream( filename, streamIndex, subStreamIndex, profileDesc );
199+
addTranscodeStream( filename, streamIndex, subStreamIndex, profileDesc, offset );
200200
}
201201

202202
void Transcoder::add( StreamTranscoder& stream )
@@ -344,7 +344,7 @@ void Transcoder::addRewrapStream( const std::string& filename, const size_t stre
344344
_inputStreams.push_back( &referenceFile->getStream( streamIndex ) );
345345
}
346346

347-
void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profile )
347+
void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, Profile::ProfileDesc& profile, const size_t offset )
348348
{
349349
InputFile* referenceFile = addInputFile( filename, streamIndex );
350350

@@ -353,7 +353,7 @@ void Transcoder::addTranscodeStream( const std::string& filename, const size_t s
353353
case AVMEDIA_TYPE_VIDEO:
354354
case AVMEDIA_TYPE_AUDIO:
355355
{
356-
_streamTranscoders.push_back( new StreamTranscoder( referenceFile->getStream( streamIndex ), _outputFile, profile ) );
356+
_streamTranscoders.push_back( new StreamTranscoder( referenceFile->getStream( streamIndex ), _outputFile, profile, -1 , offset ) );
357357
_inputStreams.push_back( &referenceFile->getStream( streamIndex ) );
358358
break;
359359
}
@@ -367,7 +367,7 @@ void Transcoder::addTranscodeStream( const std::string& filename, const size_t s
367367
}
368368
}
369369

370-
void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, Profile::ProfileDesc& profile )
370+
void Transcoder::addTranscodeStream( const std::string& filename, const size_t streamIndex, const size_t subStreamIndex, Profile::ProfileDesc& profile, const size_t offset )
371371
{
372372
InputFile* referenceFile = addInputFile( filename, streamIndex );
373373

@@ -376,7 +376,7 @@ void Transcoder::addTranscodeStream( const std::string& filename, const size_t s
376376
case AVMEDIA_TYPE_VIDEO:
377377
case AVMEDIA_TYPE_AUDIO:
378378
{
379-
_streamTranscoders.push_back( new StreamTranscoder( referenceFile->getStream( streamIndex ), _outputFile, profile, subStreamIndex ) );
379+
_streamTranscoders.push_back( new StreamTranscoder( referenceFile->getStream( streamIndex ), _outputFile, profile, subStreamIndex, offset ) );
380380
_inputStreams.push_back( &referenceFile->getStream( streamIndex ) );
381381
break;
382382
}

0 commit comments

Comments
 (0)