|
| 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 | +} |
0 commit comments