2 #include <libavfilter/avfiltergraph.h>
3 #include <libavfilter/buffersrc.h>
4 #if (LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 53 && LIBAVFILTER_VERSION_MINOR <= 77) || LIBAVFILTER_VERSION_MAJOR == 3
5 #include <libavfilter/avcodec.h>
6 #include <libavfilter/buffersink.h>
7 #elif LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
8 #include <libavfilter/vsrc_buffer.h>
10 #include <libavformat/avio.h>
14 #include "filter_graph.h"
15 #include "ffmpeg_compatibility.h"
17 #include "exceptions.h"
20 using std::stringstream;
23 using boost::shared_ptr;
25 FilterGraph::FilterGraph (shared_ptr<Film> film, Decoder* decoder, bool crop, Size s, AVPixelFormat p)
26 : _buffer_src_context (0)
27 , _buffer_sink_context (0)
35 size_after_crop = film->cropped_size (decoder->native_size ());
36 fs << crop_string (Position (film->crop().left, film->crop().top), size_after_crop);
38 size_after_crop = decoder->native_size ();
39 fs << crop_string (Position (0, 0), size_after_crop);
42 string filters = Filter::ffmpeg_strings (film->filters()).first;
43 if (!filters.empty ()) {
49 avfilter_register_all ();
51 AVFilterGraph* graph = avfilter_graph_alloc();
53 throw DecodeError ("Could not create filter graph.");
56 AVFilter* buffer_src = avfilter_get_by_name("buffer");
57 if (buffer_src == 0) {
58 throw DecodeError ("Could not find buffer src filter");
61 AVFilter* buffer_sink = get_sink ();
64 a << _size.width << ":"
65 << _size.height << ":"
66 << _pixel_format << ":"
67 << decoder->time_base_numerator() << ":"
68 << decoder->time_base_denominator() << ":"
69 << decoder->sample_aspect_ratio_numerator() << ":"
70 << decoder->sample_aspect_ratio_denominator();
74 if ((r = avfilter_graph_create_filter (&_buffer_src_context, buffer_src, "in", a.str().c_str(), 0, graph)) < 0) {
75 throw DecodeError ("could not create buffer source");
78 AVBufferSinkParams* sink_params = av_buffersink_params_alloc ();
79 PixelFormat* pixel_fmts = new PixelFormat[2];
80 pixel_fmts[0] = _pixel_format;
81 pixel_fmts[1] = PIX_FMT_NONE;
82 sink_params->pixel_fmts = pixel_fmts;
84 if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, "out", 0, sink_params, graph) < 0) {
85 throw DecodeError ("could not create buffer sink.");
88 AVFilterInOut* outputs = avfilter_inout_alloc ();
89 outputs->name = av_strdup("in");
90 outputs->filter_ctx = _buffer_src_context;
94 AVFilterInOut* inputs = avfilter_inout_alloc ();
95 inputs->name = av_strdup("out");
96 inputs->filter_ctx = _buffer_sink_context;
100 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
101 if (avfilter_graph_parse (graph, filters.c_str(), inputs, outputs, 0) < 0) {
102 throw DecodeError ("could not set up filter graph.");
105 if (avfilter_graph_parse (graph, filters.c_str(), &inputs, &outputs, 0) < 0) {
106 throw DecodeError ("could not set up filter graph.");
110 if (avfilter_graph_config (graph, 0) < 0) {
111 throw DecodeError ("could not configure filter graph.");
114 /* XXX: leaking `inputs' / `outputs' ? */
117 list<shared_ptr<Image> >
118 FilterGraph::process (AVFrame* frame)
120 list<shared_ptr<Image> > images;
122 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 53 && LIBAVFILTER_VERSION_MINOR <= 61
124 if (av_vsrc_buffer_add_frame (_buffer_src_context, frame, 0) < 0) {
125 throw DecodeError ("could not push buffer into filter chain.");
128 #elif LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR == 15
131 par.num = sample_aspect_ratio_numerator ();
132 par.den = sample_aspect_ratio_denominator ();
134 if (av_vsrc_buffer_add_frame (_buffer_src_context, frame, 0, par) < 0) {
135 throw DecodeError ("could not push buffer into filter chain.");
140 if (av_buffersrc_write_frame (_buffer_src_context, frame) < 0) {
141 throw DecodeError ("could not push buffer into filter chain.");
146 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 15 && LIBAVFILTER_VERSION_MINOR <= 61
147 while (avfilter_poll_frame (_buffer_sink_context->inputs[0])) {
149 while (av_buffersink_read (_buffer_sink_context, 0)) {
152 #if LIBAVFILTER_VERSION_MAJOR == 2 && LIBAVFILTER_VERSION_MINOR >= 15
154 int r = avfilter_request_frame (_buffer_sink_context->inputs[0]);
156 throw DecodeError ("could not request filtered frame");
159 AVFilterBufferRef* filter_buffer = _buffer_sink_context->inputs[0]->cur_buf;
163 AVFilterBufferRef* filter_buffer;
164 if (av_buffersink_get_buffer_ref (_buffer_sink_context, &filter_buffer, 0) < 0) {
171 /* This takes ownership of filter_buffer */
172 images.push_back (shared_ptr<Image> (new FilterBufferImage ((PixelFormat) frame->format, filter_buffer)));
180 FilterGraph::can_process (Size s, AVPixelFormat p) const
182 return (_size == s && _pixel_format == p);