Skip to content

Commit 32d99fd

Browse files
committed
Merge pull request #166 from cchampet/fix_inputDuration
Fix input duration
2 parents 469f4d8 + f92f917 commit 32d99fd

17 files changed

+246
-265
lines changed

src/AvTranscoder/file/FormatContext.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ AVStream& FormatContext::addAVStream( const AVCodec& avCodec )
133133
return *stream;
134134
}
135135

136+
void FormatContext::seek( uint64_t position )
137+
{
138+
if( (int)getStartTime() != AV_NOPTS_VALUE )
139+
position += getStartTime();
140+
141+
if( av_seek_frame( _avFormatContext, -1, position, AVSEEK_FLAG_BACKWARD ) < 0 )
142+
{
143+
LOG_ERROR( "Error when seek at " << position << " (in AV_TIME_BASE units) in file" )
144+
}
145+
}
146+
136147
std::vector<Option> FormatContext::getOptions()
137148
{
138149
std::vector<Option> optionsArray;

src/AvTranscoder/file/FormatContext.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class AvExport FormatContext
6161
void addMetaData( const std::string& key, const std::string& value );
6262
AVStream& addAVStream( const AVCodec& avCodec );
6363

64+
/**
65+
* @brief Seek at a specific position (in AV_TIME_BASE units)
66+
* @note before seek, add offset of start time
67+
* @note after seek, clear buffering of streams
68+
*/
69+
void seek( uint64_t position );
70+
6471
size_t getNbStreams() const { return _avFormatContext->nb_streams; }
6572
/// Get duration of the program, in seconds
6673
size_t getDuration() const { return _avFormatContext->duration; }

src/AvTranscoder/file/InputFile.cpp

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <AvTranscoder/mediaProperty/SubtitleProperties.hpp>
88
#include <AvTranscoder/mediaProperty/AttachementProperties.hpp>
99
#include <AvTranscoder/mediaProperty/UnknownProperties.hpp>
10-
#include <AvTranscoder/progress/NoDisplayProgress.hpp>
1110

1211
extern "C" {
1312
#include <libavcodec/avcodec.h>
@@ -29,10 +28,6 @@ InputFile::InputFile( const std::string& filename )
2928
{
3029
_formatContext.findStreamInfo();
3130

32-
// Analyse header
33-
NoDisplayProgress p;
34-
analyse( p, eAnalyseLevelHeader );
35-
3631
// Create streams
3732
for( size_t streamIndex = 0; streamIndex < _formatContext.getNbStreams(); ++streamIndex )
3833
{
@@ -50,60 +45,7 @@ InputFile::~InputFile()
5045

5146
void InputFile::analyse( IProgress& progress, const EAnalyseLevel level )
5247
{
53-
_properties.clearStreamProperties();
54-
55-
if( level > eAnalyseLevelHeader )
56-
seekAtFrame( 0 );
57-
58-
for( size_t streamIndex = 0; streamIndex < _formatContext.getNbStreams(); streamIndex++ )
59-
{
60-
switch( _formatContext.getAVStream( streamIndex ).codec->codec_type )
61-
{
62-
case AVMEDIA_TYPE_VIDEO:
63-
{
64-
VideoProperties properties( _formatContext, streamIndex, progress, level );
65-
_properties.getVideoProperties().push_back( properties );
66-
break;
67-
}
68-
case AVMEDIA_TYPE_AUDIO:
69-
{
70-
AudioProperties properties( _formatContext, streamIndex );
71-
_properties.getAudioProperties().push_back( properties );
72-
break;
73-
}
74-
case AVMEDIA_TYPE_DATA:
75-
{
76-
DataProperties properties( _formatContext, streamIndex );
77-
_properties.getDataProperties().push_back( properties );
78-
break;
79-
}
80-
case AVMEDIA_TYPE_SUBTITLE:
81-
{
82-
SubtitleProperties properties( _formatContext, streamIndex );
83-
_properties.getSubtitleProperties().push_back( properties );
84-
break;
85-
}
86-
case AVMEDIA_TYPE_ATTACHMENT:
87-
{
88-
AttachementProperties properties( _formatContext, streamIndex );
89-
_properties.getAttachementProperties().push_back( properties );
90-
break;
91-
}
92-
case AVMEDIA_TYPE_UNKNOWN:
93-
{
94-
UnknownProperties properties( _formatContext, streamIndex );
95-
_properties.getUnknownPropertiesProperties().push_back( properties );
96-
break;
97-
}
98-
case AVMEDIA_TYPE_NB:
99-
{
100-
break;
101-
}
102-
}
103-
}
104-
105-
if( level > eAnalyseLevelHeader )
106-
seekAtFrame( 0 );
48+
_properties.extractStreamProperties( progress, level );
10749
}
10850

10951
FileProperties InputFile::analyseFile( const std::string& filename, IProgress& progress, const EAnalyseLevel level )
@@ -144,29 +86,13 @@ bool InputFile::readNextPacket( CodedData& data, const size_t streamIndex )
14486
void InputFile::seekAtFrame( const size_t frame )
14587
{
14688
uint64_t position = frame / getFps() * AV_TIME_BASE;
147-
seek( position );
89+
_formatContext.seek( position );
14890
}
14991

15092
void InputFile::seekAtTime( const double time )
15193
{
15294
uint64_t position = time * AV_TIME_BASE;
153-
seek( position );
154-
}
155-
156-
void InputFile::seek( uint64_t position )
157-
{
158-
if( (int)_formatContext.getStartTime() != AV_NOPTS_VALUE )
159-
position += _formatContext.getStartTime();
160-
161-
if( av_seek_frame( &_formatContext.getAVFormatContext(), -1, position, AVSEEK_FLAG_BACKWARD ) < 0 )
162-
{
163-
LOG_ERROR( "Error when seek at " << position << " (in AV_TIME_BASE units) in file" )
164-
}
165-
166-
for( std::vector<InputStream*>::iterator it = _inputStreams.begin(); it != _inputStreams.end(); ++it )
167-
{
168-
(*it)->clearBuffering();
169-
}
95+
_formatContext.seek( position );
17096
}
17197

17298
void InputFile::activateStream( const size_t streamIndex, bool activate )

src/AvTranscoder/file/InputFile.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,6 @@ class AvExport InputFile
104104
*/
105105
double getFps();
106106

107-
/**
108-
* @brief Seek at a specific position (in AV_TIME_BASE units)
109-
* @note before seek, add offset of start time
110-
* @note after seek, clear buffering of streams
111-
*/
112-
void seek( uint64_t position );
113-
114107
protected:
115108
FormatContext _formatContext;
116109
FileProperties _properties;

src/AvTranscoder/mediaProperty/AudioProperties.cpp

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -180,31 +180,14 @@ size_t AudioProperties::getTicksPerFrame() const
180180
return _codecContext->ticks_per_frame;
181181
}
182182

183-
Rational AudioProperties::getTimeBase() const
184-
{
185-
if( ! _formatContext )
186-
throw std::runtime_error( "unknown format context" );
187-
188-
Rational timeBase = {
189-
_formatContext->streams[_streamIndex]->time_base.num,
190-
_formatContext->streams[_streamIndex]->time_base.den,
191-
};
192-
return timeBase;
193-
}
194-
195-
double AudioProperties::getDuration() const
196-
{
197-
Rational timeBase = getTimeBase();
198-
double duration = ( timeBase.num / (double) timeBase.den ) * _formatContext->streams[_streamIndex]->duration;
199-
return duration;
200-
}
201-
202183
PropertyVector AudioProperties::getPropertiesAsVector() const
203184
{
204185
PropertyVector data;
205186

206-
addProperty( data, "streamId", &AudioProperties::getStreamId );
207-
detail::add( data, "streamIndex", getStreamIndex() );
187+
// Add properties of base class
188+
PropertyVector basedProperty = StreamProperties::getPropertiesAsVector();
189+
data.insert( data.begin(), basedProperty.begin(), basedProperty.end() );
190+
208191
addProperty( data, "codecId", &AudioProperties::getCodecId );
209192
addProperty( data, "codecName", &AudioProperties::getCodecName );
210193
addProperty( data, "codecLongName", &AudioProperties::getCodecLongName );
@@ -218,13 +201,6 @@ PropertyVector AudioProperties::getPropertiesAsVector() const
218201
addProperty( data, "channelName", &AudioProperties::getChannelName );
219202
addProperty( data, "channelDescription", &AudioProperties::getChannelDescription );
220203
addProperty( data, "ticksPerFrame", &AudioProperties::getTicksPerFrame );
221-
addProperty( data, "timeBase", &AudioProperties::getTimeBase );
222-
addProperty( data, "duration", &AudioProperties::getDuration );
223-
224-
for( size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex )
225-
{
226-
detail::add( data, _metadatas.at( metadataIndex ).first, _metadatas.at( metadataIndex ).second );
227-
}
228204

229205
return data;
230206
}

src/AvTranscoder/mediaProperty/AudioProperties.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class AvExport AudioProperties : public StreamProperties
2929
size_t getNbSamples() const; ///< 0 if unknown
3030

3131
size_t getTicksPerFrame() const;
32-
Rational getTimeBase() const;
33-
double getDuration() const;
3432

3533
#ifndef SWIG
3634
AVCodecContext& getAVCodecContext() { return *_codecContext; }

0 commit comments

Comments
 (0)