Skip to content

Commit 1cc630c

Browse files
committed
Merge pull request #176 from cchampet/fix_FormatContextOptions
Fix FormatContext private options
2 parents 5c6a6de + 141139f commit 1cc630c

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

src/AvTranscoder/file/FormatContext.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
namespace avtranscoder
77
{
88

9-
FormatContext::FormatContext( const std::string& filename, int req_flags )
9+
FormatContext::FormatContext( const std::string& filename, int req_flags, AVDictionary** options )
1010
: _avFormatContext( NULL )
11+
, _flags( req_flags )
1112
, _options()
1213
, _isOpen( false )
1314
{
14-
int ret = avformat_open_input( &_avFormatContext, filename.c_str(), NULL, NULL );
15+
int ret = avformat_open_input( &_avFormatContext, filename.c_str(), NULL, options );
1516
if( ret < 0 )
1617
{
1718
std::string msg = "unable to open file ";
@@ -21,11 +22,15 @@ FormatContext::FormatContext( const std::string& filename, int req_flags )
2122
throw std::ios_base::failure( msg );
2223
}
2324
_isOpen = true;
25+
2426
loadOptions( _options, _avFormatContext, req_flags );
27+
// when demuxing, priv_data of AVFormatContext is set by avformat_open_input()
28+
loadOptions( _options, _avFormatContext->priv_data, req_flags );
2529
}
2630

2731
FormatContext::FormatContext( int req_flags )
2832
: _avFormatContext( NULL )
33+
, _flags( req_flags )
2934
, _options()
3035
, _isOpen( false )
3136
{
@@ -85,6 +90,8 @@ void FormatContext::writeHeader( AVDictionary** options )
8590
{
8691
throw std::runtime_error( "could not write header: " + getDescriptionFromErrorCode( ret ) );
8792
}
93+
// when muxing, priv_data of AVFormatContext is set by avformat_write_header()
94+
loadOptions( _options, _avFormatContext->priv_data, _flags );
8895
}
8996

9097
void FormatContext::writeFrame( AVPacket& packet, bool interleaved )

src/AvTranscoder/file/FormatContext.hpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ class AvExport FormatContext
2121
FormatContext& operator=( const FormatContext& formatContext );
2222

2323
public:
24-
FormatContext( const std::string& filename, int req_flags = 0 ); ///< Allocate an AVFormatContext by opening an input file
25-
FormatContext( int req_flags = 0 ); ///< Allocate an AVFormatContext with default values
24+
/**
25+
* @brief Allocate an AVFormatContext by opening an input file
26+
*/
27+
FormatContext( const std::string& filename, int req_flags = 0, AVDictionary** options = NULL );
28+
29+
/**
30+
* @brief Allocate an AVFormatContext with default values
31+
*/
32+
FormatContext( int req_flags = 0 );
33+
2634
~FormatContext();
2735

2836
/**
@@ -43,7 +51,11 @@ class AvExport FormatContext
4351
*/
4452
void closeRessource();
4553

46-
void writeHeader( AVDictionary** options = NULL ); ///< Write the stream header to an output media file
54+
/**
55+
* @brief Write the stream header to an output media file
56+
* @note Also load options specific to the output format
57+
*/
58+
void writeHeader( AVDictionary** options = NULL );
4759

4860
/**
4961
* @brief Write a packet to an output media file
@@ -98,6 +110,7 @@ class AvExport FormatContext
98110

99111
private:
100112
AVFormatContext* _avFormatContext; ///< Has ownership
113+
const int _flags; ///< Flags with which the options are loaded (see AV_OPT_FLAG_xxx)
101114
OptionMap _options;
102115
bool _isOpen; ///< Is the AVFormatContext open (in constructor with a filename)
103116
};

src/AvTranscoder/file/OutputFile.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ OutputFile::OutputFile( const std::string& filename )
1515
, _previousProcessedStreamDuration( 0.0 )
1616
{
1717
_formatContext.setOutputFormat( _filename );
18-
_formatContext.openRessource( _filename, AVIO_FLAG_WRITE );
1918
}
2019

2120
OutputFile::~OutputFile()
@@ -90,9 +89,12 @@ bool OutputFile::beginWrap( )
9089
{
9190
LOG_DEBUG( "Begin wrap of OutputFile" )
9291

92+
_formatContext.openRessource( _filename, AVIO_FLAG_WRITE );
9393
_formatContext.writeHeader();
94+
9495
_frameCount.clear();
9596
_frameCount.resize( _outputStreams.size(), 0 );
97+
9698
return true;
9799
}
98100

src/AvTranscoder/file/OutputFile.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,16 @@ class AvExport OutputFile : public IOutputFile
3333
IOutputStream& addAudioStream( const AudioCodec& audioDesc );
3434
IOutputStream& addDataStream( const DataCodec& dataDesc );
3535

36+
/**
37+
* @brief Open ressource and write header.
38+
*/
3639
bool beginWrap();
40+
3741
IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamId );
42+
43+
/**
44+
* @brief Close ressource and write trailer.
45+
*/
3846
bool endWrap();
3947

4048
/**

0 commit comments

Comments
 (0)