Remove old ffmpeg compatibility stuff.
[dcpomatic.git] / src / lib / filter_graph.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/lib/filter_graph.cc
21  *  @brief A graph of FFmpeg filters.
22  */
23
24 extern "C" {
25 #include <libavfilter/avfiltergraph.h>
26 #include <libavfilter/buffersrc.h>
27 #include <libavfilter/avcodec.h>
28 #include <libavfilter/buffersink.h>
29 #include <libavformat/avio.h>
30 }
31 #include "decoder.h"
32 #include "filter_graph.h"
33 #include "filter.h"
34 #include "exceptions.h"
35 #include "image.h"
36 #include "film.h"
37 #include "ffmpeg_decoder.h"
38
39 #include "i18n.h"
40
41 using std::stringstream;
42 using std::string;
43 using std::list;
44 using boost::shared_ptr;
45 using libdcp::Size;
46
47 /** Construct a FilterGraph for the settings in a film.
48  *  @param film Film.
49  *  @param decoder Decoder that we are using.
50  *  @param s Size of the images to process.
51  *  @param p Pixel format of the images to process.
52  */
53 FilterGraph::FilterGraph (shared_ptr<Film> film, FFmpegDecoder* decoder, libdcp::Size s, AVPixelFormat p)
54         : _buffer_src_context (0)
55         , _buffer_sink_context (0)
56         , _size (s)
57         , _pixel_format (p)
58 {
59         string filters = Filter::ffmpeg_strings (film->filters()).first;
60         if (!filters.empty ()) {
61                 filters += N_(",");
62         }
63
64         filters += crop_string (Position (film->crop().left, film->crop().top), film->cropped_size (decoder->native_size()));
65
66         AVFilterGraph* graph = avfilter_graph_alloc();
67         if (graph == 0) {
68                 throw DecodeError (N_("could not create filter graph."));
69         }
70
71         AVFilter* buffer_src = avfilter_get_by_name(N_("buffer"));
72         if (buffer_src == 0) {
73                 throw DecodeError (N_("could not find buffer src filter"));
74         }
75
76         AVFilter* buffer_sink = avfilter_get_by_name(N_("buffersink"));
77         if (buffer_sink == 0) {
78                 throw DecodeError (N_("Could not create buffer sink filter"));
79         }
80
81         stringstream a;
82         a << _size.width << N_(":")
83           << _size.height << N_(":")
84           << _pixel_format << N_(":")
85           << decoder->time_base_numerator() << N_(":")
86           << decoder->time_base_denominator() << N_(":")
87           << decoder->sample_aspect_ratio_numerator() << N_(":")
88           << decoder->sample_aspect_ratio_denominator();
89
90         int r;
91
92         if ((r = avfilter_graph_create_filter (&_buffer_src_context, buffer_src, N_("in"), a.str().c_str(), 0, graph)) < 0) {
93                 throw DecodeError (N_("could not create buffer source"));
94         }
95
96         AVBufferSinkParams* sink_params = av_buffersink_params_alloc ();
97         PixelFormat* pixel_fmts = new PixelFormat[2];
98         pixel_fmts[0] = _pixel_format;
99         pixel_fmts[1] = PIX_FMT_NONE;
100         sink_params->pixel_fmts = pixel_fmts;
101         
102         if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, N_("out"), 0, sink_params, graph) < 0) {
103                 throw DecodeError (N_("could not create buffer sink."));
104         }
105
106         AVFilterInOut* outputs = avfilter_inout_alloc ();
107         outputs->name = av_strdup(N_("in"));
108         outputs->filter_ctx = _buffer_src_context;
109         outputs->pad_idx = 0;
110         outputs->next = 0;
111
112         AVFilterInOut* inputs = avfilter_inout_alloc ();
113         inputs->name = av_strdup(N_("out"));
114         inputs->filter_ctx = _buffer_sink_context;
115         inputs->pad_idx = 0;
116         inputs->next = 0;
117
118         if (avfilter_graph_parse (graph, filters.c_str(), &inputs, &outputs, 0) < 0) {
119                 throw DecodeError (N_("could not set up filter graph."));
120         }
121         
122         if (avfilter_graph_config (graph, 0) < 0) {
123                 throw DecodeError (N_("could not configure filter graph."));
124         }
125
126         /* XXX: leaking `inputs' / `outputs' ? */
127 }
128
129 /** Take an AVFrame and process it using our configured filters, returning a
130  *  set of Images.
131  */
132 list<shared_ptr<Image> >
133 FilterGraph::process (AVFrame const * frame)
134 {
135         list<shared_ptr<Image> > images;
136         
137
138         if (av_buffersrc_write_frame (_buffer_src_context, frame) < 0) {
139                 throw DecodeError (N_("could not push buffer into filter chain."));
140         }
141
142         while (av_buffersink_read (_buffer_sink_context, 0)) {
143                 AVFilterBufferRef* filter_buffer;
144                 if (av_buffersink_get_buffer_ref (_buffer_sink_context, &filter_buffer, 0) < 0) {
145                         filter_buffer = 0;
146                 }
147
148                 if (filter_buffer) {
149                         /* This takes ownership of filter_buffer */
150                         images.push_back (shared_ptr<Image> (new FilterBufferImage ((PixelFormat) frame->format, filter_buffer)));
151                 }
152         }
153         
154         return images;
155 }
156
157 /** @param s Image size.
158  *  @param p Pixel format.
159  *  @return true if this chain can process images with `s' and `p', otherwise false.
160  */
161 bool
162 FilterGraph::can_process (libdcp::Size s, AVPixelFormat p) const
163 {
164         return (_size == s && _pixel_format == p);
165 }