Skip to content

Commit 4b7dc83

Browse files
author
Clement Champetier
committed
Added FilterGraph
The class handles a list of filters.
1 parent 3d4b67f commit 4b7dc83

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "FilterGraph.hpp"
2+
3+
extern "C" {
4+
#include <libavfilter/avfilter.h>
5+
}
6+
7+
#include <stdexcept>
8+
9+
10+
namespace avtranscoder
11+
{
12+
13+
FilterGraph::FilterGraph()
14+
: _graph( avfilter_graph_alloc() )
15+
, _filters()
16+
{
17+
if( ! _graph )
18+
throw std::runtime_error( "Unable to create filter graph: out of memory.");
19+
}
20+
21+
FilterGraph::~FilterGraph()
22+
{
23+
for( std::vector< Filter >::iterator it = _filters.begin(); it < _filters.end(); ++it )
24+
{
25+
avfilter_free( it->second );
26+
}
27+
avfilter_graph_free( &_graph );
28+
}
29+
30+
void FilterGraph::addFilter( const std::string& filtername )
31+
{
32+
AVFilter* filter = avfilter_get_by_name( filtername.c_str() );
33+
if( filter )
34+
{
35+
AVFilterContext* context = NULL;
36+
const int err = avfilter_graph_create_filter( &context, filter, NULL, filtername.c_str(), NULL, _graph );
37+
if( err < 0 )
38+
{
39+
std::string msg( "Cannot add filter " );
40+
msg += filtername;
41+
msg += " to the graph: ";
42+
msg += getDescriptionFromErrorCode(err);
43+
throw std::runtime_error( msg );
44+
}
45+
else
46+
_filters.push_back( std::make_pair(filter, context) );
47+
}
48+
else
49+
{
50+
throw std::runtime_error( "Cannot find filter " + filtername );
51+
}
52+
}
53+
54+
void FilterGraph::connectFilters()
55+
{
56+
// connecting filters
57+
for( size_t index = 0; index < _filters.size()+1; index+=2 )
58+
{
59+
const int err = avfilter_link( _filters.at(index).second, 0, _filters.at(index+1).second, 0);
60+
if( err < 0 )
61+
{
62+
throw std::runtime_error( "Error connecting filters." );
63+
}
64+
}
65+
// configuring
66+
const int err = avfilter_graph_config( _graph, NULL );
67+
if( err < 0 )
68+
throw std::runtime_error( "Error configuring the filter graph.");
69+
}
70+
71+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef _AV_TRANSCODER_FILTER_FILTER_GRAPH_HPP_
2+
#define _AV_TRANSCODER_FILTER_FILTER_GRAPH_HPP_
3+
4+
#include <AvTranscoder/common.hpp>
5+
6+
#include <vector>
7+
#include <utility>
8+
9+
struct AVFilterGraph;
10+
struct AVFilter;
11+
struct AVFilterContext;
12+
13+
namespace avtranscoder
14+
{
15+
16+
/**
17+
* @brief
18+
**/
19+
class AvExport FilterGraph
20+
{
21+
private:
22+
FilterGraph( const FilterGraph& otherFilterGraph );
23+
FilterGraph& operator=( const FilterGraph& otherFilterGraph );
24+
25+
public:
26+
typedef std::pair<AVFilter*, AVFilterContext*> Filter;
27+
28+
public:
29+
FilterGraph();
30+
~FilterGraph();
31+
32+
void addFilter( const std::string& filtername );
33+
Filter& getFirstFilter() { return _filters.at(0); }
34+
Filter& getLastFilter() { return _filters.at(_filters.size()-1); }
35+
36+
bool hasFilters() const { return ! _filters.empty(); }
37+
38+
void connectFilters();
39+
40+
private:
41+
AVFilterGraph* _graph;
42+
std::vector< Filter > _filters;
43+
};
44+
45+
}
46+
47+
#endif

0 commit comments

Comments
 (0)