Skip to content

Commit 1c04d89

Browse files
author
Clement Champetier
committed
FormatContext could allocate a default context, or open one from a filename
* FormatContext: add functions to wrap libav functions. * Context: remove default constructor. Clean the class. * Update optionChecker app.
1 parent 5ca4839 commit 1c04d89

File tree

5 files changed

+89
-15
lines changed

5 files changed

+89
-15
lines changed

app/optionChecker/optionChecker.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ void optionChecker( const std::string& inputfilename )
6969
avtranscoder::InputFile file( inputfilename );
7070

7171
// format options
72-
AVFormatContext* avFormatContext = avformat_alloc_context();
73-
avtranscoder::FormatContext formatContext( *avFormatContext );
72+
avtranscoder::FormatContext formatContext;
7473
std::vector<avtranscoder::Option> formatOptions = formatContext.getOptions();
7574
displayOptions( formatOptions );
7675

src/AvTranscoder/option/Context.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ extern "C" {
1414
namespace avtranscoder
1515
{
1616

17+
Context::Context( void* avContext, int req_flags )
18+
: _avContext( avContext )
19+
{
20+
if( avContext )
21+
loadOptions( avContext, req_flags );
22+
}
23+
24+
Context::~Context()
25+
{}
26+
1727
std::vector<Option> Context::getOptions()
1828
{
1929
std::vector<Option> optionsArray;

src/AvTranscoder/option/Context.hpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ typedef std::vector<Option> OptionArray;
1515
typedef std::map<std::string, Option> OptionMap; ///< Key: option name / value: option
1616

1717
/**
18-
* @brief Wrapper of AVContext.
18+
* @brief This class manages options of 'AVContext' objects (AVFormatContext, AVCodecContext).
1919
* Can access Options through the corresponding context.
2020
*/
2121
class AvExport Context
2222
{
2323
public:
24-
Context() {}
25-
2624
/**
2725
* @param avContext: could be an AVFormatContext or an AVCodecContext.
2826
* @param req_flags: filter the AVOption loaded by the Context (default = 0: no flag restriction).
@@ -34,11 +32,8 @@ class AvExport Context
3432
* AV_OPT_FLAG_VIDEO_PARAM
3533
* AV_OPT_FLAG_SUBTITLE_PARAM
3634
*/
37-
Context( void* avContext, int req_flags = 0 )
38-
: _avContext( avContext )
39-
{
40-
loadOptions( avContext, req_flags );
41-
}
35+
Context( void* avContext, int req_flags = 0 );
36+
virtual ~Context();
4237

4338
OptionArray getOptions(); ///< Get options as array
4439
OptionMap& getOptionsMap() { return _options; } ///< Get options as map
@@ -50,7 +45,7 @@ class AvExport Context
5045

5146
protected:
5247
OptionMap _options;
53-
void* _avContext; ///< Has link (no ownership)
48+
void* _avContext;
5449
};
5550

5651
}
Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,68 @@
11
#include "FormatContext.hpp"
22

3+
#include <stdexcept>
4+
#include <sstream>
5+
36
namespace avtranscoder
47
{
58

6-
FormatContext::FormatContext( AVFormatContext& avFormatContext, int req_flags )
7-
: Context( &avFormatContext, req_flags )
8-
{}
9+
FormatContext::FormatContext( const std::string& filename, int req_flags )
10+
: Context( NULL, req_flags )
11+
, _isOpen( true )
12+
{
13+
av_register_all(); // TODO: call it once
14+
15+
int err = avformat_open_input( reinterpret_cast<AVFormatContext**>( &_avContext ), filename.c_str(), NULL, NULL );
16+
if( err < 0 )
17+
{
18+
std::string msg = "unable to open file: ";
19+
msg += filename;
20+
throw std::ios_base::failure( msg );
21+
}
22+
loadOptions( _avContext, req_flags );
23+
}
24+
25+
FormatContext::FormatContext( int req_flags )
26+
: Context( NULL, req_flags )
27+
, _isOpen( false )
28+
{
29+
av_register_all(); // TODO: call it once
30+
31+
_avContext = avformat_alloc_context();
32+
loadOptions( _avContext, req_flags );
33+
}
34+
35+
FormatContext::~FormatContext()
36+
{
37+
if( ! _avContext )
38+
return;
39+
40+
if( _isOpen )
41+
avformat_close_input( reinterpret_cast<AVFormatContext**>( &_avContext ) );
42+
else
43+
avformat_free_context( static_cast<AVFormatContext*>( _avContext ) );
44+
}
45+
46+
void FormatContext::findStreamInfo( AVDictionary** options )
47+
{
48+
int err = avformat_find_stream_info( static_cast<AVFormatContext*>( _avContext ), options );
49+
if( err < 0 )
50+
{
51+
avformat_close_input( reinterpret_cast<AVFormatContext**>( &_avContext ) );
52+
throw std::ios_base::failure( "unable to find stream informations" );
53+
}
54+
}
55+
56+
AVStream& FormatContext::getAVStream( size_t index ) const
57+
{
58+
if( index >= getNbStreams() )
59+
{
60+
std::stringstream msg;
61+
msg << "Can't acces stream at index ";
62+
msg << index;
63+
throw std::runtime_error( msg.str() );
64+
}
65+
return *getAVFormatContext().streams[index];
66+
}
967

1068
}

src/AvTranscoder/option/FormatContext.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,23 @@ namespace avtranscoder
1616
class FormatContext : public Context
1717
{
1818
public:
19-
FormatContext( AVFormatContext& avFormatContext, int req_flags = 0 );
19+
FormatContext( const std::string& filename, int req_flags = 0 ); ///< Allocate an AVFormatContext by opening an input file
20+
FormatContext( int req_flags = 0 ); ///< Allocate an AVFormatContext with default values
21+
~FormatContext();
22+
23+
void findStreamInfo( AVDictionary** options = NULL ); ///< Read packets of a media file to get stream information.
24+
25+
size_t getNbStreams() const { return getAVFormatContext().nb_streams; }
26+
size_t getDuration() const { return getAVFormatContext().duration; }
27+
size_t getStartTime() const { return getAVFormatContext().start_time; }
2028

2129
#ifndef SWIG
2230
AVFormatContext& getAVFormatContext() const { return *static_cast<AVFormatContext*>( _avContext ); }
31+
AVStream& getAVStream( size_t index ) const;
2332
#endif
33+
34+
private:
35+
bool _isOpen; ///< Is the AVFormatContext open (in constructor with a filename)
2436
};
2537

2638
}

0 commit comments

Comments
 (0)