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