Supporters update.
[dcpomatic.git] / src / lib / video_filter_graph.cc
1 /*
2     Copyright (C) 2012-2021 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
22 #include "compose.hpp"
23 #include "dcpomatic_assert.h"
24 #include "exceptions.h"
25 #include "image.h"
26 #include "video_filter_graph.h"
27 #include <dcp/scope_guard.h>
28 extern "C" {
29 #include <libavfilter/buffersrc.h>
30 #include <libavfilter/buffersink.h>
31 #include <libavutil/opt.h>
32 }
33
34 #include "i18n.h"
35
36
37 using std::list;
38 using std::make_pair;
39 using std::make_shared;
40 using std::pair;
41 using std::shared_ptr;
42 using std::string;
43
44
45 VideoFilterGraph::VideoFilterGraph (dcp::Size s, AVPixelFormat p, dcp::Fraction r)
46         : _size (s)
47         , _pixel_format (p)
48         , _frame_rate (r)
49 {
50
51 }
52
53
54 list<shared_ptr<const Image>>
55 VideoFilterGraph::process(shared_ptr<const Image> image)
56 {
57         if (_copy) {
58                 return { image };
59         }
60
61         auto frame = av_frame_alloc();
62         if (!frame) {
63                 throw std::bad_alloc();
64         }
65
66         dcp::ScopeGuard sg = [&frame]() { av_frame_free(&frame); };
67
68         for (int i = 0; i < image->planes(); ++i) {
69                 frame->data[i] = image->data()[i];
70                 frame->linesize[i] = image->stride()[i];
71         }
72
73         frame->width = image->size().width;
74         frame->height = image->size().height;
75         frame->format = image->pixel_format();
76
77         int r = av_buffersrc_write_frame(_buffer_src_context, frame);
78         if (r < 0) {
79                 throw DecodeError(String::compose(N_("could not push buffer into filter chain (%1)."), r));
80         }
81
82         list<shared_ptr<const Image>> images;
83
84         while (true) {
85                 if (av_buffersink_get_frame(_buffer_sink_context, _frame) < 0) {
86                         break;
87                 }
88
89                 images.push_back(make_shared<Image>(_frame, Image::Alignment::PADDED));
90                 av_frame_unref (_frame);
91         }
92
93         return images;
94 }
95
96
97 /** Take an AVFrame and process it using our configured filters, returning a
98  *  set of Images.  Caller handles memory management of the input frame.
99  */
100 list<pair<shared_ptr<const Image>, int64_t>>
101 VideoFilterGraph::process (AVFrame* frame)
102 {
103         list<pair<shared_ptr<const Image>, int64_t>> images;
104
105         if (_copy) {
106                 images.push_back (make_pair(make_shared<Image>(frame, Image::Alignment::PADDED), frame->best_effort_timestamp));
107         } else {
108                 int r = av_buffersrc_write_frame (_buffer_src_context, frame);
109                 if (r < 0) {
110                         throw DecodeError (String::compose(N_("could not push buffer into filter chain (%1)."), r));
111                 }
112
113                 while (true) {
114                         if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
115                                 break;
116                         }
117
118                         images.push_back (make_pair(make_shared<Image>(_frame, Image::Alignment::PADDED), frame->best_effort_timestamp));
119                         av_frame_unref (_frame);
120                 }
121         }
122
123         return images;
124 }
125
126
127 /** @param s Image size.
128  *  @param p Pixel format.
129  *  @return true if this chain can process images with `s' and `p', otherwise false.
130  */
131 bool
132 VideoFilterGraph::can_process (dcp::Size s, AVPixelFormat p) const
133 {
134         return (_size == s && _pixel_format == p);
135 }
136
137
138 string
139 VideoFilterGraph::src_parameters () const
140 {
141         char buffer[256];
142         snprintf (
143                 buffer, sizeof(buffer),
144                 "video_size=%dx%d:pix_fmt=%d:frame_rate=%d/%d:time_base=1/1:pixel_aspect=1/1",
145                 _size.width, _size.height,
146                 _pixel_format,
147                 _frame_rate.numerator, _frame_rate.denominator
148                 );
149         return buffer;
150 }
151
152
153 void
154 VideoFilterGraph::set_parameters (AVFilterContext* context) const
155 {
156         AVPixelFormat pix_fmts[] = { _pixel_format, AV_PIX_FMT_NONE };
157         int r = av_opt_set_int_list (context, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
158         DCPOMATIC_ASSERT (r >= 0);
159 }
160
161
162 string
163 VideoFilterGraph::src_name () const
164 {
165         return "buffer";
166 }
167
168
169 string
170 VideoFilterGraph::sink_name () const
171 {
172         return "buffersink";
173 }