Skip to content

Commit 100ee81

Browse files
committed
Merge pull request #75 from cchampet/dev_offset_in_seconds
Offset in seconds (TODO: finish python tests)
2 parents 9ff9c91 + 5782aad commit 100ee81

File tree

9 files changed

+86
-65
lines changed

9 files changed

+86
-65
lines changed

src/AvTranscoder/codec/AudioCodec.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ AudioFrameDesc AudioCodec::getAudioFrameDesc() const
2525
{
2626
assert( _avCodecContext != NULL );
2727
AudioFrameDesc audioFrameDesc( _avCodecContext->sample_rate, _avCodecContext->channels, _avCodecContext->sample_fmt );
28-
double fps = 1.0 * _avCodecContext->time_base.den / ( _avCodecContext->time_base.num * _avCodecContext->ticks_per_frame );
29-
if( ! std::isinf( fps ) )
30-
audioFrameDesc.setFps( fps );
3128
return audioFrameDesc;
3229
}
3330

@@ -36,9 +33,6 @@ void AudioCodec::setAudioParameters( const AudioFrameDesc& audioFrameDesc )
3633
_avCodecContext->sample_rate = audioFrameDesc.getSampleRate();
3734
_avCodecContext->channels = audioFrameDesc.getChannels();
3835
_avCodecContext->sample_fmt = audioFrameDesc.getSampleFormat();
39-
_avCodecContext->time_base.num = 1;
40-
_avCodecContext->time_base.den = audioFrameDesc.getFps();
41-
_avCodecContext->ticks_per_frame = 1;
4236
}
4337

4438
}

src/AvTranscoder/decoder/AudioGenerator.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "AudioGenerator.hpp"
22

3-
#include <AvTranscoder/transform/AudioTransform.hpp>
4-
53
namespace avtranscoder
64
{
75

@@ -20,6 +18,7 @@ AudioGenerator::~AudioGenerator()
2018
void AudioGenerator::setAudioFrameDesc( const AudioFrameDesc& frameDesc )
2119
{
2220
_frameDesc = frameDesc;
21+
_frameDesc.setFps( 25. );
2322
}
2423

2524
void AudioGenerator::setFrame( Frame& inputFrame )
@@ -40,17 +39,9 @@ bool AudioGenerator::decodeNextFrame( Frame& frameBuffer )
4039
{
4140
int fillChar = 0;
4241

43-
// input of convert
44-
AudioFrame intermediateBuffer( _frameDesc );
45-
intermediateBuffer.assign( _frameDesc.getDataSize(), fillChar );
46-
intermediateBuffer.setNbSamples( audioBuffer.getNbSamples() );
47-
48-
// output of convert
4942
_silent = new AudioFrame( audioBuffer.desc() );
50-
51-
// convert and store the silence
52-
AudioTransform audioTransform;
53-
audioTransform.convert( intermediateBuffer, *_silent );
43+
_silent->assign( _frameDesc.getDataSize(), fillChar );
44+
_silent->setNbSamples( audioBuffer.getNbSamples() );
5445
}
5546
frameBuffer.refData( *_silent );
5647
}

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/frame/AudioFrame.hpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,18 @@ namespace avtranscoder
1717
class AvExport AudioFrameDesc
1818
{
1919
public:
20-
/**
21-
* @warning FPS value is set to 25 by default
22-
*/
2320
AudioFrameDesc( const size_t sampleRate = 0, const size_t channels = 0, const AVSampleFormat sampleFormat = AV_SAMPLE_FMT_NONE )
2421
: _sampleRate( sampleRate )
2522
, _channels( channels )
2623
, _sampleFormat( sampleFormat )
27-
, _fps( 25. )
24+
, _fps( 1. )
2825
{}
29-
/**
30-
* @warning FPS value is set to 25 by default
31-
*/
26+
3227
AudioFrameDesc( const size_t sampleRate, const size_t channels, const std::string& sampleFormat )
3328
: _sampleRate( sampleRate )
3429
, _channels( channels )
3530
, _sampleFormat( av_get_sample_fmt( sampleFormat.c_str() ) )
36-
, _fps( 25. )
31+
, _fps( 1. )
3732
{}
3833

3934
size_t getSampleRate() const { return _sampleRate; }
@@ -51,7 +46,7 @@ class AvExport AudioFrameDesc
5146
if( _sampleFormat == AV_SAMPLE_FMT_NONE )
5247
throw std::runtime_error( "incorrect sample format" );
5348

54-
size_t size = _sampleRate * _channels * av_get_bytes_per_sample( _sampleFormat );
49+
size_t size = ( _sampleRate / _fps ) * _channels * av_get_bytes_per_sample( _sampleFormat );
5550
if( size == 0 )
5651
throw std::runtime_error( "unable to determine audio buffer size" );
5752

src/AvTranscoder/transcoder/StreamTranscoder.cpp

Lines changed: 11 additions & 11 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
, _canSwitchToGenerator( false )
4039
, _verbose( false )
@@ -67,7 +66,7 @@ StreamTranscoder::StreamTranscoder(
6766
IOutputFile& outputFile,
6867
const ProfileLoader::Profile& profile,
6968
const int subStreamIndex,
70-
const size_t offset
69+
const double offset
7170
)
7271
: _inputStream( &inputStream )
7372
, _outputStream( NULL )
@@ -79,7 +78,6 @@ StreamTranscoder::StreamTranscoder(
7978
, _outputEncoder( NULL )
8079
, _transform( NULL )
8180
, _subStreamIndex( subStreamIndex )
82-
, _frameProcessed( 0 )
8381
, _offset( offset )
8482
, _canSwitchToGenerator( false )
8583
, _verbose( false )
@@ -177,7 +175,6 @@ StreamTranscoder::StreamTranscoder(
177175
, _outputEncoder( NULL )
178176
, _transform( NULL )
179177
, _subStreamIndex( -1 )
180-
, _frameProcessed( 0 )
181178
, _offset( 0 )
182179
, _canSwitchToGenerator( false )
183180
, _verbose( false )
@@ -274,8 +271,6 @@ void StreamTranscoder::preProcessCodecLatency()
274271

275272
bool StreamTranscoder::processFrame()
276273
{
277-
++_frameProcessed;
278-
279274
if( ! _inputDecoder )
280275
{
281276
return processRewrap();
@@ -327,9 +322,16 @@ bool StreamTranscoder::processTranscode( const int subStreamIndex )
327322
std::cout << "transcode a frame " << std::endl;
328323

329324
// check offset
330-
if( _offset && _frameProcessed == _offset + 1 )
325+
if( _offset )
331326
{
332-
switchToInputDecoder();
327+
bool endOfOffset = _outputStream->getStreamDuration() >= _offset;
328+
if( endOfOffset )
329+
{
330+
// switch to essence from input stream
331+
switchToInputDecoder();
332+
// reset offset
333+
_offset = 0;
334+
}
333335
}
334336

335337
bool decodingStatus = false;
@@ -396,9 +398,7 @@ double StreamTranscoder::getDuration() const
396398
{
397399
if( _inputStream )
398400
{
399-
double totalDuration = 0;
400-
totalDuration += _inputStream->getDuration();
401-
// @todo add offset
401+
double totalDuration = _inputStream->getDuration() + _offset;
402402
return totalDuration;
403403
}
404404
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
@@ -98,8 +98,7 @@ class AvExport StreamTranscoder
9898

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

101-
size_t _frameProcessed; ///< How many frames have been processed by the StreamTranscoder.
102-
size_t _offset; ///< Offset, in frame, at the beginning of the StreamTranscoder.
101+
double _offset; ///< Offset, in seconds, at the beginning of the StreamTranscoder.
103102

104103
bool _canSwitchToGenerator; ///< Automatically switch to a generator at the end of the stream
105104
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

test/pyTest/testTranscoderOffset.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
3+
from nose.tools import *
4+
5+
from pyAvTranscoder import avtranscoder as av
6+
7+
av.preloadCodecsAndFormats()
8+
9+
10+
def testTranscodeAudioOffset():
11+
"""
12+
Transcode one audio stream (profile wave24b48kmono) with offset at the beginning of the transcode.
13+
"""
14+
inputFileName = os.environ['AVTRANSCODER_TEST_AUDIO_FILE']
15+
outputFileName = "testTranscodeAudioOffset.wav"
16+
17+
ouputFile = av.OutputFile( outputFileName )
18+
transcoder = av.Transcoder( ouputFile )
19+
20+
transcoder.add( inputFileName, 0, "wave24b48kmono", 10 )
21+
22+
progress = av.ConsoleProgress()
23+
transcoder.process( progress )
24+
25+
# TODO: check output duration
26+
27+
28+
def testTranscodeVideoOffset():
29+
"""
30+
Transcode one video stream (profile mpeg2) with offset at the beginning of the transcode.
31+
"""
32+
inputFileName = os.environ['AVTRANSCODER_TEST_VIDEO_FILE']
33+
outputFileName = "testTranscodeVideoOffset.mov"
34+
35+
ouputFile = av.OutputFile( outputFileName )
36+
transcoder = av.Transcoder( ouputFile )
37+
38+
transcoder.add( inputFileName, 0, "mpeg2", 10 )
39+
40+
progress = av.ConsoleProgress()
41+
transcoder.process( progress )
42+
43+
# TODO: check output duration

0 commit comments

Comments
 (0)