Fix typo in log message.
[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 "video_filter_graph.h"
25 extern "C" {
26 #include <libavfilter/buffersrc.h>
27 #include <libavfilter/buffersink.h>
28 #include <libavutil/opt.h>
29 }
30
31 #include "i18n.h"
32
33
34 using std::list;
35 using std::make_pair;
36 using std::make_shared;
37 using std::pair;
38 using std::shared_ptr;
39 using std::string;
40
41
42 VideoFilterGraph::VideoFilterGraph (dcp::Size s, AVPixelFormat p, dcp::Fraction r)
43         : _size (s)
44         , _pixel_format (p)
45         , _frame_rate (r)
46 {
47
48 }
49
50
51 /** Take an AVFrame and process it using our configured filters, returning a
52  *  set of Images.  Caller handles memory management of the input frame.
53  */
54 list<pair<shared_ptr<Image>, int64_t>>
55 VideoFilterGraph::process (AVFrame* frame)
56 {
57         list<pair<shared_ptr<Image>, int64_t>> images;
58
59         if (_copy) {
60                 images.push_back (make_pair(make_shared<Image>(frame, Image::Alignment::PADDED), frame->best_effort_timestamp));
61         } else {
62                 int r = av_buffersrc_write_frame (_buffer_src_context, frame);
63                 if (r < 0) {
64                         throw DecodeError (String::compose(N_("could not push buffer into filter chain (%1)."), r));
65                 }
66
67                 while (true) {
68                         if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
69                                 break;
70                         }
71
72                         images.push_back (make_pair(make_shared<Image>(_frame, Image::Alignment::PADDED), frame->best_effort_timestamp));
73                         av_frame_unref (_frame);
74                 }
75         }
76
77         return images;
78 }
79
80
81 /** @param s Image size.
82  *  @param p Pixel format.
83  *  @return true if this chain can process images with `s' and `p', otherwise false.
84  */
85 bool
86 VideoFilterGraph::can_process (dcp::Size s, AVPixelFormat p) const
87 {
88         return (_size == s && _pixel_format == p);
89 }
90
91
92 string
93 VideoFilterGraph::src_parameters () const
94 {
95         char buffer[256];
96         snprintf (
97                 buffer, sizeof(buffer),
98                 "video_size=%dx%d:pix_fmt=%d:frame_rate=%d/%d:time_base=1/1:pixel_aspect=1/1",
99                 _size.width, _size.height,
100                 _pixel_format,
101                 _frame_rate.numerator, _frame_rate.denominator
102                 );
103         return buffer;
104 }
105
106
107 void
108 VideoFilterGraph::set_parameters (AVFilterContext* context) const
109 {
110         AVPixelFormat pix_fmts[] = { _pixel_format, AV_PIX_FMT_NONE };
111         int r = av_opt_set_int_list (context, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
112         DCPOMATIC_ASSERT (r >= 0);
113 }
114
115
116 string
117 VideoFilterGraph::src_name () const
118 {
119         return "buffer";
120 }
121
122
123 string
124 VideoFilterGraph::sink_name () const
125 {
126         return "buffersink";
127 }