|
1 | 1 | #include "FilterGraph.hpp"
|
2 | 2 |
|
| 3 | +#include <AvTranscoder/util.hpp> |
| 4 | +#include <AvTranscoder/data/decoded/AudioFrame.hpp> |
| 5 | +#include <AvTranscoder/data/decoded/VideoFrame.hpp> |
| 6 | + |
3 | 7 | extern "C" {
|
4 | 8 | #include <libavfilter/avfilter.h>
|
| 9 | +#include <libavfilter/buffersrc.h> |
| 10 | +#include <libavfilter/buffersink.h> |
5 | 11 | }
|
6 | 12 |
|
7 | 13 | #include <stdexcept>
|
| 14 | +#include <sstream> |
8 | 15 |
|
9 | 16 | namespace avtranscoder
|
10 | 17 | {
|
11 | 18 |
|
12 |
| -FilterGraph::FilterGraph() |
| 19 | +FilterGraph::FilterGraph(const ICodec& codec) |
13 | 20 | : _graph(avfilter_graph_alloc())
|
14 | 21 | , _filters()
|
| 22 | + , _codec(codec) |
| 23 | + , _isInit(false) |
15 | 24 | {
|
16 | 25 | if(!_graph)
|
17 | 26 | throw std::runtime_error("Unable to create filter graph: out of memory.");
|
18 | 27 | }
|
19 | 28 |
|
20 | 29 | FilterGraph::~FilterGraph()
|
21 | 30 | {
|
22 |
| - for(std::vector<Filter>::iterator it = _filters.begin(); it < _filters.end(); ++it) |
| 31 | + for(std::vector<Filter*>::iterator it = _filters.begin(); it < _filters.end(); ++it) |
23 | 32 | {
|
24 |
| - avfilter_free(it->second); |
| 33 | + it = _filters.erase(it); |
25 | 34 | }
|
26 | 35 | avfilter_graph_free(&_graph);
|
27 | 36 | }
|
28 | 37 |
|
29 |
| -void FilterGraph::addFilter(const std::string& filtername) |
| 38 | +void FilterGraph::process(Frame& frame) |
30 | 39 | {
|
31 |
| - AVFilter* filter = avfilter_get_by_name(filtername.c_str()); |
32 |
| - if(filter) |
| 40 | + if(!hasFilters()) |
33 | 41 | {
|
34 |
| - AVFilterContext* context = NULL; |
35 |
| - const int err = avfilter_graph_create_filter(&context, filter, NULL, filtername.c_str(), NULL, _graph); |
36 |
| - if(err < 0) |
37 |
| - { |
38 |
| - std::string msg("Cannot add filter "); |
39 |
| - msg += filtername; |
40 |
| - msg += " to the graph: "; |
41 |
| - msg += getDescriptionFromErrorCode(err); |
42 |
| - throw std::runtime_error(msg); |
43 |
| - } |
44 |
| - else |
45 |
| - _filters.push_back(std::make_pair(filter, context)); |
| 42 | + LOG_DEBUG("No filter to process.") |
| 43 | + return; |
46 | 44 | }
|
47 |
| - else |
| 45 | + |
| 46 | + // init filter graph |
| 47 | + if(!_isInit) |
| 48 | + init(frame); |
| 49 | + |
| 50 | + // setup source frame |
| 51 | + int ret = av_buffersrc_write_frame(_filters.at(0)->getAVFilterContext(), &frame.getAVFrame()); |
| 52 | + if(ret < 0) |
48 | 53 | {
|
49 |
| - throw std::runtime_error("Cannot find filter " + filtername); |
| 54 | + throw std::runtime_error("Error when adding a frame to the source buffer used to start to process filters: " + |
| 55 | + getDescriptionFromErrorCode(ret)); |
| 56 | + } |
| 57 | + |
| 58 | + // pull filtered data from the filter graph |
| 59 | + for(;;) |
| 60 | + { |
| 61 | + ret = av_buffersink_get_frame(_filters.at(_filters.size() - 1)->getAVFilterContext(), &frame.getAVFrame()); |
| 62 | + if(ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) |
| 63 | + break; |
| 64 | + if(ret < 0) |
| 65 | + { |
| 66 | + throw std::runtime_error("Error reading buffer from buffersink: " + getDescriptionFromErrorCode(ret)); |
| 67 | + } |
50 | 68 | }
|
51 | 69 | }
|
52 | 70 |
|
53 |
| -void FilterGraph::connectFilters() |
| 71 | +Filter& FilterGraph::addFilter(const std::string& filterName, const std::string& filterOptions, |
| 72 | + const std::string& instanceName) |
| 73 | +{ |
| 74 | + LOG_INFO("Add filter " << filterName << " to the graph.") |
| 75 | + Filter* filter = new Filter(filterName, filterOptions, instanceName); |
| 76 | + _filters.push_back(filter); |
| 77 | + return *_filters.back(); |
| 78 | +} |
| 79 | + |
| 80 | +void FilterGraph::init(const Frame& frame) |
54 | 81 | {
|
55 |
| - // connecting filters |
56 |
| - for(size_t index = 0; index < _filters.size() + 1; index += 2) |
| 82 | + // push filters to the graph |
| 83 | + pushInBuffer(frame); |
| 84 | + for(size_t i = 1; i < _filters.size(); ++i) |
57 | 85 | {
|
58 |
| - const int err = avfilter_link(_filters.at(index).second, 0, _filters.at(index + 1).second, 0); |
| 86 | + pushFilter(*_filters.at(i)); |
| 87 | + } |
| 88 | + pushOutBuffer(frame); |
| 89 | + |
| 90 | + // connect filters |
| 91 | + for(size_t index = 0; index < _filters.size() - 1; ++index) |
| 92 | + { |
| 93 | + LOG_INFO("Connect filter " << _filters.at(index)->getName() << " to filter " << _filters.at(index + 1)->getName()) |
| 94 | + const int err = |
| 95 | + avfilter_link(_filters.at(index)->getAVFilterContext(), 0, _filters.at(index + 1)->getAVFilterContext(), 0); |
59 | 96 | if(err < 0)
|
60 | 97 | {
|
61 |
| - throw std::runtime_error("Error connecting filters."); |
| 98 | + throw std::runtime_error("Error when connecting filters."); |
62 | 99 | }
|
63 | 100 | }
|
64 |
| - // configuring |
| 101 | + |
| 102 | + // configuring the graph |
| 103 | + LOG_INFO("Configuring filter graph.") |
65 | 104 | const int err = avfilter_graph_config(_graph, NULL);
|
66 | 105 | if(err < 0)
|
67 |
| - throw std::runtime_error("Error configuring the filter graph."); |
| 106 | + { |
| 107 | + throw std::runtime_error("Error configuring the filter graph: " + getDescriptionFromErrorCode(err)); |
| 108 | + } |
| 109 | + |
| 110 | + _isInit = true; |
| 111 | +} |
| 112 | + |
| 113 | +void FilterGraph::pushFilter(Filter& filter) |
| 114 | +{ |
| 115 | + AVFilterContext* context = NULL; |
| 116 | + const int err = avfilter_graph_create_filter(&context, &filter.getAVFilter(), filter.getInstanceName().c_str(), |
| 117 | + filter.getOptions().c_str(), NULL, _graph); |
| 118 | + filter.setAVFilterContext(context); |
| 119 | + if(err < 0) |
| 120 | + { |
| 121 | + std::string msg("Cannot add filter "); |
| 122 | + msg += filter.getName(); |
| 123 | + msg += " (instance="; |
| 124 | + msg += filter.getInstanceName(); |
| 125 | + msg += ") to the graph: "; |
| 126 | + msg += getDescriptionFromErrorCode(err); |
| 127 | + throw std::runtime_error(msg); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +void FilterGraph::pushInBuffer(const Frame& frame) |
| 132 | +{ |
| 133 | + std::string filterName; |
| 134 | + std::stringstream filterOptions; |
| 135 | + // audio frame |
| 136 | + if(frame.isAudioFrame()) |
| 137 | + { |
| 138 | + filterName = "abuffer"; |
| 139 | + const AudioFrame& audioFrame = dynamic_cast<const AudioFrame&>(frame); |
| 140 | + filterOptions << "time_base=" << _codec.getAVCodecContext().time_base.num << "/" |
| 141 | + << _codec.getAVCodecContext().time_base.den << ":"; |
| 142 | + filterOptions << "sample_rate=" << audioFrame.getSampleRate() << ":"; |
| 143 | + filterOptions << "sample_fmt=" << getSampleFormatName(audioFrame.getSampleFormat()) << ":"; |
| 144 | + filterOptions << "channel_layout=0x" << audioFrame.getChannelLayout(); |
| 145 | + } |
| 146 | + // video frame |
| 147 | + else if(frame.isVideoFrame()) |
| 148 | + { |
| 149 | + filterName = "buffer"; |
| 150 | + const VideoFrame& videoFrame = dynamic_cast<const VideoFrame&>(frame); |
| 151 | + filterOptions << "video_size=" << videoFrame.getWidth() << "x" << videoFrame.getHeight() << ":"; |
| 152 | + filterOptions << "pix_fmt=" << getPixelFormatName(videoFrame.getPixelFormat()) << ":"; |
| 153 | + filterOptions << "time_base=" << _codec.getAVCodecContext().time_base.num << "/" |
| 154 | + << _codec.getAVCodecContext().time_base.den << ":"; |
| 155 | + filterOptions << "pixel_aspect=" << _codec.getAVCodecContext().sample_aspect_ratio.num << "/" |
| 156 | + << _codec.getAVCodecContext().sample_aspect_ratio.den; |
| 157 | + } |
| 158 | + // invalid frame |
| 159 | + else |
| 160 | + throw std::runtime_error("Cannot create input buffer of filter graph: the given frame is invalid."); |
| 161 | + |
| 162 | + // add in buffer |
| 163 | + Filter* in = new Filter(filterName, filterOptions.str(), "in"); |
| 164 | + LOG_INFO("Add filter '" << filterName << "' at the beginning of the graph.") |
| 165 | + _filters.insert(_filters.begin(), in); |
| 166 | + pushFilter(*in); |
| 167 | +} |
| 168 | + |
| 169 | +void FilterGraph::pushOutBuffer(const Frame& frame) |
| 170 | +{ |
| 171 | + std::string filterName; |
| 172 | + |
| 173 | + if(frame.isAudioFrame()) |
| 174 | + filterName = "abuffersink"; |
| 175 | + else if(frame.isVideoFrame()) |
| 176 | + filterName = "buffersink"; |
| 177 | + else |
| 178 | + throw std::runtime_error("Cannot create output buffer of filter graph: the given frame is invalid."); |
| 179 | + |
| 180 | + // add out buffer |
| 181 | + Filter& out = addFilter(filterName, "", "out"); |
| 182 | + pushFilter(out); |
68 | 183 | }
|
69 | 184 | }
|
0 commit comments