b6b3c2668095c444d77c74fd6ae9b4d59ac9a777
[dcpomatic.git] / src / lib / video_filter_graph.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "video_filter_graph.h"
22 #include "image.h"
23 #include "compose.hpp"
24 extern "C" {
25 #include <libavfilter/buffersrc.h>
26 #include <libavfilter/buffersink.h>
27 }
28 #include <boost/make_shared.hpp>
29
30 #include "i18n.h"
31
32 using std::list;
33 using std::pair;
34 using std::vector;
35 using std::string;
36 using std::make_pair;
37 using boost::shared_ptr;
38 using boost::make_shared;
39
40 VideoFilterGraph::VideoFilterGraph (dcp::Size s, AVPixelFormat p)
41         : _size (s)
42         , _pixel_format (p)
43 {
44
45 }
46
47 /** Take an AVFrame and process it using our configured filters, returning a
48  *  set of Images.  Caller handles memory management of the input frame.
49  */
50 list<pair<shared_ptr<Image>, int64_t> >
51 VideoFilterGraph::process (AVFrame* frame)
52 {
53         list<pair<shared_ptr<Image>, int64_t> > images;
54
55         if (_copy) {
56                 images.push_back (make_pair (make_shared<Image> (frame), av_frame_get_best_effort_timestamp (frame)));
57         } else {
58                 int r = av_buffersrc_write_frame (_buffer_src_context, frame);
59                 if (r < 0) {
60                         throw DecodeError (String::compose (N_("could not push buffer into filter chain (%1)."), r));
61                 }
62
63                 while (true) {
64                         if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
65                                 break;
66                         }
67
68                         images.push_back (make_pair (make_shared<Image> (_frame), av_frame_get_best_effort_timestamp (_frame)));
69                         av_frame_unref (_frame);
70                 }
71         }
72
73         return images;
74 }
75
76 /** @param s Image size.
77  *  @param p Pixel format.
78  *  @return true if this chain can process images with `s' and `p', otherwise false.
79  */
80 bool
81 VideoFilterGraph::can_process (dcp::Size s, AVPixelFormat p) const
82 {
83         return (_size == s && _pixel_format == p);
84 }
85
86 string
87 VideoFilterGraph::src_parameters () const
88 {
89         SafeStringStream a;
90
91         a << "video_size=" << _size.width << "x" << _size.height << ":"
92           << "pix_fmt=" << _pixel_format << ":"
93           << "time_base=1/1:"
94           << "pixel_aspect=1/1";
95
96         return a.str ();
97 }
98
99 void *
100 VideoFilterGraph::sink_parameters () const
101 {
102         AVBufferSinkParams* sink_params = av_buffersink_params_alloc ();
103         AVPixelFormat* pixel_fmts = new AVPixelFormat[2];
104         pixel_fmts = new AVPixelFormat[2];
105         pixel_fmts[0] = _pixel_format;
106         pixel_fmts[1] = AV_PIX_FMT_NONE;
107         sink_params->pixel_fmts = pixel_fmts;
108         return sink_params;
109 }
110
111 string
112 VideoFilterGraph::src_name () const
113 {
114         return "buffer";
115 }
116
117 string
118 VideoFilterGraph::sink_name () const
119 {
120         return "buffersink";
121 }