Skip to content

Commit 24eec82

Browse files
author
Clement Champetier
committed
InputFile: use FormatContext instead of AVFormatContext
* Also construct Properties classes from a FormatContext instead of an AVFormatContext. * AvInputStream: remove unnecessary private functions.
1 parent 1c04d89 commit 24eec82

19 files changed

+61
-132
lines changed

app/optionChecker/optionChecker.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <AvTranscoder/util.hpp>
2-
#include <AvTranscoder/option/Context.hpp>
32
#include "AvTranscoder/option/FormatContext.hpp"
43
#include <AvTranscoder/option/CodecContext.hpp>
54
#include <AvTranscoder/option/Option.hpp>

src/AvTranscoder/codedStream/AvInputStream.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ AvInputStream::AvInputStream( InputFile& inputFile, const size_t streamIndex )
2121
, _streamIndex( streamIndex )
2222
, _isActivated( false )
2323
{
24-
AVCodecContext* context = _inputFile->getAVFormatContext().streams[_streamIndex]->codec;
24+
AVCodecContext* context = _inputFile->getFormatContext().getAVStream( _streamIndex ).codec;
2525

2626
switch( context->codec_type )
2727
{
@@ -91,7 +91,7 @@ bool AvInputStream::readNextPacket( CodedData& data )
9191

9292
VideoCodec& AvInputStream::getVideoCodec()
9393
{
94-
assert( _streamIndex <= _inputFile->getAVFormatContext().nb_streams );
94+
assert( _streamIndex <= _inputFile->getFormatContext().getNbStreams() );
9595

9696
if( getStreamType() != AVMEDIA_TYPE_VIDEO )
9797
{
@@ -103,7 +103,7 @@ VideoCodec& AvInputStream::getVideoCodec()
103103

104104
AudioCodec& AvInputStream::getAudioCodec()
105105
{
106-
assert( _streamIndex <= _inputFile->getAVFormatContext().nb_streams );
106+
assert( _streamIndex <= _inputFile->getFormatContext().getNbStreams() );
107107

108108
if( getStreamType() != AVMEDIA_TYPE_AUDIO )
109109
{
@@ -115,7 +115,7 @@ AudioCodec& AvInputStream::getAudioCodec()
115115

116116
DataCodec& AvInputStream::getDataCodec()
117117
{
118-
assert( _streamIndex <= _inputFile->getAVFormatContext().nb_streams );
118+
assert( _streamIndex <= _inputFile->getFormatContext().getNbStreams() );
119119

120120
if( getStreamType() != AVMEDIA_TYPE_DATA )
121121
{
@@ -127,12 +127,12 @@ DataCodec& AvInputStream::getDataCodec()
127127

128128
AVMediaType AvInputStream::getStreamType() const
129129
{
130-
return getAVCodecContext().codec_type;
130+
return _inputFile->getFormatContext().getAVStream( _streamIndex ).codec->codec_type;
131131
}
132132

133133
double AvInputStream::getDuration() const
134134
{
135-
return 1.0 * _inputFile->getAVFormatContext().duration / AV_TIME_BASE;
135+
return 1.0 * _inputFile->getFormatContext().getDuration() / AV_TIME_BASE;
136136
}
137137

138138
void AvInputStream::addPacket( AVPacket& packet )
@@ -151,14 +151,4 @@ void AvInputStream::clearBuffering()
151151
_streamCache = std::queue<CodedData>();
152152
}
153153

154-
AVStream& AvInputStream::getAVStream() const
155-
{
156-
return *_inputFile->getAVFormatContext().streams[_streamIndex];
157-
}
158-
159-
AVCodecContext& AvInputStream::getAVCodecContext() const
160-
{
161-
return *getAVStream().codec;
162-
}
163-
164154
}

src/AvTranscoder/codedStream/AvInputStream.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ class AvExport AvInputStream : public IInputStream
3939
void clearBuffering();
4040
//@}
4141

42-
private:
43-
AVStream& getAVStream() const;
44-
AVCodecContext& getAVCodecContext() const;
45-
4642
private:
4743
InputFile* _inputFile; ///< Has link (no ownership)
4844
ICodec* _codec; ///< Has ownership

src/AvTranscoder/file/InputFile.cpp

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
extern "C" {
1313
#include <libavcodec/avcodec.h>
14-
#include <libavformat/avformat.h>
1514
#include <libavutil/avutil.h>
1615
#include <libavutil/pixdesc.h>
1716
}
@@ -23,32 +22,16 @@ namespace avtranscoder
2322
{
2423

2524
InputFile::InputFile( const std::string& filename )
26-
: _formatContext ( NULL )
27-
, _properties( NULL )
25+
: _formatContext( filename, AV_OPT_FLAG_DECODING_PARAM )
26+
, _properties( _formatContext )
2827
, _filename( filename )
28+
, _inputStreams()
2929
, _verbose( false )
3030
{
31-
av_register_all();
32-
if( avformat_open_input( &_formatContext, _filename.c_str(), NULL, NULL ) < 0 )
33-
{
34-
std::string msg = "unable to open file: ";
35-
msg += _filename;
36-
throw std::ios_base::failure( msg );
37-
}
38-
39-
// update format context informations from streams
40-
if( avformat_find_stream_info( _formatContext, NULL ) < 0 )
41-
{
42-
avformat_close_input( &_formatContext );
43-
_formatContext = NULL;
44-
throw std::ios_base::failure( "unable to find stream informations" );
45-
}
46-
47-
// Initialize FileProperties
48-
_properties = FileProperties( _formatContext );
31+
_formatContext.findStreamInfo();
4932

5033
// Create streams
51-
for( size_t streamIndex = 0; streamIndex < _formatContext->nb_streams; ++streamIndex )
34+
for( size_t streamIndex = 0; streamIndex < _formatContext.getNbStreams(); ++streamIndex )
5235
{
5336
_inputStreams.push_back( new AvInputStream( *this, streamIndex ) );
5437
}
@@ -60,21 +43,16 @@ InputFile::~InputFile()
6043
{
6144
delete (*it);
6245
}
63-
64-
if( _formatContext != NULL )
65-
avformat_close_input( &_formatContext );
6646
}
6747

6848
void InputFile::analyse( IProgress& progress, const EAnalyseLevel level )
6949
{
70-
assert( _formatContext != NULL );
71-
7250
if( level > eAnalyseLevelHeader )
7351
seekAtFrame( 0 );
7452

75-
for( size_t streamId = 0; streamId < _formatContext->nb_streams; streamId++ )
53+
for( size_t streamId = 0; streamId < _formatContext.getNbStreams(); streamId++ )
7654
{
77-
switch( _formatContext->streams[streamId]->codec->codec_type )
55+
switch( _formatContext.getAVStream( streamId ).codec->codec_type )
7856
{
7957
case AVMEDIA_TYPE_VIDEO:
8058
{
@@ -137,7 +115,7 @@ bool InputFile::readNextPacket( CodedData& data, const size_t streamIndex )
137115
while( ! nextPacketFound )
138116
{
139117
av_init_packet( &packet );
140-
int ret = av_read_frame( _formatContext, &packet );
118+
int ret = av_read_frame( &_formatContext.getAVFormatContext(), &packet );
141119
if( ret < 0 ) // error or end of file
142120
{
143121
av_free_packet( &packet );
@@ -165,10 +143,10 @@ void InputFile::seekAtFrame( const size_t frame )
165143
{
166144
uint64_t pos = frame / 25 * AV_TIME_BASE; // WARNING: hardcoded fps
167145

168-
if( (int)_formatContext->start_time != AV_NOPTS_VALUE )
169-
pos += _formatContext->start_time;
146+
if( (int)_formatContext.getStartTime() != AV_NOPTS_VALUE )
147+
pos += _formatContext.getStartTime();
170148

171-
if( av_seek_frame( _formatContext, -1, pos, AVSEEK_FLAG_BACKWARD ) < 0 )
149+
if( av_seek_frame( &_formatContext.getAVFormatContext(), -1, pos, AVSEEK_FLAG_BACKWARD ) < 0 )
172150
{
173151
std::cerr << "Error during seek at " << frame << " (" << pos << ") in file" << std::endl;
174152
}
@@ -202,8 +180,6 @@ AvInputStream& InputFile::getStream( size_t index )
202180

203181
void InputFile::setProfile( const ProfileLoader::Profile& profile )
204182
{
205-
Context formatContext( _formatContext, AV_OPT_FLAG_DECODING_PARAM );
206-
207183
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
208184
{
209185
if( (*it).first == constants::avProfileIdentificator ||
@@ -213,7 +189,7 @@ void InputFile::setProfile( const ProfileLoader::Profile& profile )
213189

214190
try
215191
{
216-
Option& formatOption = formatContext.getOption( (*it).first );
192+
Option& formatOption = _formatContext.getOption( (*it).first );
217193
formatOption.setString( (*it).second );
218194
}
219195
catch( std::exception& e )

src/AvTranscoder/file/InputFile.hpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,16 @@
22
#define _AV_TRANSCODER_FILE_INPUT_FILE_HPP_
33

44
#include <AvTranscoder/common.hpp>
5-
5+
#include <AvTranscoder/option/FormatContext.hpp>
66
#include <AvTranscoder/file/util.hpp>
7-
8-
#include <AvTranscoder/codec/AudioCodec.hpp>
9-
#include <AvTranscoder/codec/VideoCodec.hpp>
10-
117
#include <AvTranscoder/codedStream/AvInputStream.hpp>
12-
138
#include <AvTranscoder/mediaProperty/FileProperties.hpp>
14-
159
#include <AvTranscoder/progress/IProgress.hpp>
16-
1710
#include <AvTranscoder/ProfileLoader.hpp>
1811

1912
#include <string>
2013
#include <vector>
2114

22-
struct AVFormatContext;
23-
2415
namespace avtranscoder
2516
{
2617

@@ -83,12 +74,8 @@ class AvExport InputFile
8374
**/
8475
AvInputStream& getStream( size_t index );
8576

86-
/**
87-
* @brief Get LibAV/FFmpeg AVFormatContext
88-
* @return format context on current InputFile
89-
**/
90-
AVFormatContext& getAVFormatContext() const { return *_formatContext; }
91-
77+
FormatContext& getFormatContext() { return _formatContext; }
78+
9279
/**
9380
* @brief Set the format of the input file
9481
* @param profile: the profile of the input format
@@ -110,9 +97,9 @@ class AvExport InputFile
11097
static FileProperties analyseFile( const std::string& filename, IProgress& progress, const EAnalyseLevel level = eAnalyseLevelFirstGop );
11198

11299
protected:
113-
AVFormatContext* _formatContext;
100+
FormatContext _formatContext;
114101
FileProperties _properties;
115-
std::string _filename;
102+
std::string _filename;
116103
std::vector<AvInputStream*> _inputStreams;
117104

118105
bool _verbose;

src/AvTranscoder/mediaProperty/AttachementProperties.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace avtranscoder
44
{
55

6-
AttachementProperties::AttachementProperties( const AVFormatContext* formatContext, const size_t index )
7-
: _formatContext( formatContext )
6+
AttachementProperties::AttachementProperties( const FormatContext& formatContext, const size_t index )
7+
: _formatContext( &formatContext.getAVFormatContext() )
88
, _streamId( index )
99
{
10-
if( formatContext )
10+
if( _formatContext )
1111
detail::fillMetadataDictionnary( _formatContext->streams[index]->metadata, _metadatas );
1212
}
1313

src/AvTranscoder/mediaProperty/AttachementProperties.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33

44
#include <AvTranscoder/common.hpp>
55
#include <AvTranscoder/mediaProperty/util.hpp>
6-
7-
extern "C" {
8-
#include <libavformat/avformat.h>
9-
}
6+
#include <AvTranscoder/option/FormatContext.hpp>
107

118
namespace avtranscoder
129
{
1310

1411
class AvExport AttachementProperties
1512
{
1613
public:
17-
AttachementProperties( const AVFormatContext* formatContext, const size_t index );
14+
AttachementProperties( const FormatContext& formatContext, const size_t index );
1815

1916
size_t getStreamId() const { return _streamId; }
2017
PropertiesMap& getMetadatas() { return _metadatas; }

src/AvTranscoder/mediaProperty/AudioProperties.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ extern "C" {
1313
namespace avtranscoder
1414
{
1515

16-
AudioProperties::AudioProperties( const AVFormatContext* formatContext, const size_t index )
17-
: _formatContext( formatContext )
16+
AudioProperties::AudioProperties( const FormatContext& formatContext, const size_t index )
17+
: _formatContext( &formatContext.getAVFormatContext() )
1818
, _codecContext( NULL )
1919
, _codec( NULL )
2020
, _streamId( index )
2121
{
2222
if( _formatContext )
23-
_codecContext = formatContext->streams[index]->codec;
23+
_codecContext = _formatContext->streams[index]->codec;
2424

2525
if( _formatContext && _codecContext )
2626
_codec = avcodec_find_decoder( _codecContext->codec_id );
2727

28-
if( formatContext )
28+
if( _formatContext )
2929
detail::fillMetadataDictionnary( _formatContext->streams[index]->metadata, _metadatas );
3030
}
3131

src/AvTranscoder/mediaProperty/AudioProperties.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44
#include <AvTranscoder/common.hpp>
55
#include <AvTranscoder/mediaProperty/util.hpp>
6-
7-
extern "C" {
8-
#include <libavformat/avformat.h>
9-
#include <libavcodec/avcodec.h>
10-
}
6+
#include <AvTranscoder/option/FormatContext.hpp>
117

128
#include <string>
139

@@ -19,7 +15,7 @@ namespace avtranscoder
1915
class AvExport AudioProperties
2016
{
2117
public:
22-
AudioProperties( const AVFormatContext* formatContext, const size_t index );
18+
AudioProperties( const FormatContext& formatContext, const size_t index );
2319

2420
std::string getCodecName() const;
2521
std::string getCodecLongName() const;

src/AvTranscoder/mediaProperty/DataProperties.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ extern "C" {
1212
namespace avtranscoder
1313
{
1414

15-
DataProperties::DataProperties( const AVFormatContext* formatContext, const size_t index )
16-
: _formatContext( formatContext )
15+
DataProperties::DataProperties( const FormatContext& formatContext, const size_t index )
16+
: _formatContext( &formatContext.getAVFormatContext() )
1717
, _streamId( index )
1818
{
1919
//detectAncillaryData( _formatContext, _streamId );
2020

21-
if( formatContext )
21+
if( _formatContext )
2222
detail::fillMetadataDictionnary( _formatContext->streams[index]->metadata, _metadatas );
2323
}
2424

0 commit comments

Comments
 (0)