Skip to content

Commit

Permalink
Merge pull request #166 from cchampet/fix_inputDuration
Browse files Browse the repository at this point in the history
Fix input duration
  • Loading branch information
valnoel committed Apr 22, 2015
2 parents 469f4d8 + f92f917 commit 32d99fd
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 265 deletions.
11 changes: 11 additions & 0 deletions src/AvTranscoder/file/FormatContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ AVStream& FormatContext::addAVStream( const AVCodec& avCodec )
return *stream;
}

void FormatContext::seek( uint64_t position )
{
if( (int)getStartTime() != AV_NOPTS_VALUE )
position += getStartTime();

if( av_seek_frame( _avFormatContext, -1, position, AVSEEK_FLAG_BACKWARD ) < 0 )
{
LOG_ERROR( "Error when seek at " << position << " (in AV_TIME_BASE units) in file" )
}
}

std::vector<Option> FormatContext::getOptions()
{
std::vector<Option> optionsArray;
Expand Down
7 changes: 7 additions & 0 deletions src/AvTranscoder/file/FormatContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class AvExport FormatContext
void addMetaData( const std::string& key, const std::string& value );
AVStream& addAVStream( const AVCodec& avCodec );

/**
* @brief Seek at a specific position (in AV_TIME_BASE units)
* @note before seek, add offset of start time
* @note after seek, clear buffering of streams
*/
void seek( uint64_t position );

size_t getNbStreams() const { return _avFormatContext->nb_streams; }
/// Get duration of the program, in seconds
size_t getDuration() const { return _avFormatContext->duration; }
Expand Down
80 changes: 3 additions & 77 deletions src/AvTranscoder/file/InputFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <AvTranscoder/mediaProperty/SubtitleProperties.hpp>
#include <AvTranscoder/mediaProperty/AttachementProperties.hpp>
#include <AvTranscoder/mediaProperty/UnknownProperties.hpp>
#include <AvTranscoder/progress/NoDisplayProgress.hpp>

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

// Analyse header
NoDisplayProgress p;
analyse( p, eAnalyseLevelHeader );

// Create streams
for( size_t streamIndex = 0; streamIndex < _formatContext.getNbStreams(); ++streamIndex )
{
Expand All @@ -50,60 +45,7 @@ InputFile::~InputFile()

void InputFile::analyse( IProgress& progress, const EAnalyseLevel level )
{
_properties.clearStreamProperties();

if( level > eAnalyseLevelHeader )
seekAtFrame( 0 );

for( size_t streamIndex = 0; streamIndex < _formatContext.getNbStreams(); streamIndex++ )
{
switch( _formatContext.getAVStream( streamIndex ).codec->codec_type )
{
case AVMEDIA_TYPE_VIDEO:
{
VideoProperties properties( _formatContext, streamIndex, progress, level );
_properties.getVideoProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_AUDIO:
{
AudioProperties properties( _formatContext, streamIndex );
_properties.getAudioProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_DATA:
{
DataProperties properties( _formatContext, streamIndex );
_properties.getDataProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_SUBTITLE:
{
SubtitleProperties properties( _formatContext, streamIndex );
_properties.getSubtitleProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_ATTACHMENT:
{
AttachementProperties properties( _formatContext, streamIndex );
_properties.getAttachementProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_UNKNOWN:
{
UnknownProperties properties( _formatContext, streamIndex );
_properties.getUnknownPropertiesProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_NB:
{
break;
}
}
}

if( level > eAnalyseLevelHeader )
seekAtFrame( 0 );
_properties.extractStreamProperties( progress, level );
}

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

void InputFile::seekAtTime( const double time )
{
uint64_t position = time * AV_TIME_BASE;
seek( position );
}

void InputFile::seek( uint64_t position )
{
if( (int)_formatContext.getStartTime() != AV_NOPTS_VALUE )
position += _formatContext.getStartTime();

if( av_seek_frame( &_formatContext.getAVFormatContext(), -1, position, AVSEEK_FLAG_BACKWARD ) < 0 )
{
LOG_ERROR( "Error when seek at " << position << " (in AV_TIME_BASE units) in file" )
}

for( std::vector<InputStream*>::iterator it = _inputStreams.begin(); it != _inputStreams.end(); ++it )
{
(*it)->clearBuffering();
}
_formatContext.seek( position );
}

void InputFile::activateStream( const size_t streamIndex, bool activate )
Expand Down
7 changes: 0 additions & 7 deletions src/AvTranscoder/file/InputFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ class AvExport InputFile
*/
double getFps();

/**
* @brief Seek at a specific position (in AV_TIME_BASE units)
* @note before seek, add offset of start time
* @note after seek, clear buffering of streams
*/
void seek( uint64_t position );

protected:
FormatContext _formatContext;
FileProperties _properties;
Expand Down
32 changes: 4 additions & 28 deletions src/AvTranscoder/mediaProperty/AudioProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,31 +180,14 @@ size_t AudioProperties::getTicksPerFrame() const
return _codecContext->ticks_per_frame;
}

Rational AudioProperties::getTimeBase() const
{
if( ! _formatContext )
throw std::runtime_error( "unknown format context" );

Rational timeBase = {
_formatContext->streams[_streamIndex]->time_base.num,
_formatContext->streams[_streamIndex]->time_base.den,
};
return timeBase;
}

double AudioProperties::getDuration() const
{
Rational timeBase = getTimeBase();
double duration = ( timeBase.num / (double) timeBase.den ) * _formatContext->streams[_streamIndex]->duration;
return duration;
}

PropertyVector AudioProperties::getPropertiesAsVector() const
{
PropertyVector data;

addProperty( data, "streamId", &AudioProperties::getStreamId );
detail::add( data, "streamIndex", getStreamIndex() );
// Add properties of base class
PropertyVector basedProperty = StreamProperties::getPropertiesAsVector();
data.insert( data.begin(), basedProperty.begin(), basedProperty.end() );

addProperty( data, "codecId", &AudioProperties::getCodecId );
addProperty( data, "codecName", &AudioProperties::getCodecName );
addProperty( data, "codecLongName", &AudioProperties::getCodecLongName );
Expand All @@ -218,13 +201,6 @@ PropertyVector AudioProperties::getPropertiesAsVector() const
addProperty( data, "channelName", &AudioProperties::getChannelName );
addProperty( data, "channelDescription", &AudioProperties::getChannelDescription );
addProperty( data, "ticksPerFrame", &AudioProperties::getTicksPerFrame );
addProperty( data, "timeBase", &AudioProperties::getTimeBase );
addProperty( data, "duration", &AudioProperties::getDuration );

for( size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex )
{
detail::add( data, _metadatas.at( metadataIndex ).first, _metadatas.at( metadataIndex ).second );
}

return data;
}
Expand Down
2 changes: 0 additions & 2 deletions src/AvTranscoder/mediaProperty/AudioProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class AvExport AudioProperties : public StreamProperties
size_t getNbSamples() const; ///< 0 if unknown

size_t getTicksPerFrame() const;
Rational getTimeBase() const;
double getDuration() const;

#ifndef SWIG
AVCodecContext& getAVCodecContext() { return *_codecContext; }
Expand Down
Loading

0 comments on commit 32d99fd

Please sign in to comment.